fix: calculate the size of marbles using the proportion of marbles area to fix: marble is too large in a square screen

This commit is contained in:
Usu171
2025-12-14 22:52:06 +08:00
parent 0a6c24a509
commit 455c54b3cf
3 changed files with 8 additions and 16 deletions

View File

@@ -9,7 +9,8 @@ export const MARBLE_CONFIG = {
size: {
base: 192,
min: 96,
maxScreenRatio: 0.25,
marbleCount: 19,
marbleArea: 0.5,
},
// Marble initial speed configuration
@@ -57,4 +58,3 @@ export const MARBLE_CONFIG = {
} as const;
export const AVATAR_BASE_URL = "https://avatar.awfufu.com/qq/";

View File

@@ -17,10 +17,11 @@ export class MarbleFactory {
}
// Calculate marble size (responsive)
private calculateMarbleSize(): number {
const { base, min, maxScreenRatio } = MARBLE_CONFIG.size;
const quarter =
Math.min(window.innerWidth, window.innerHeight) * maxScreenRatio;
public calculateMarbleSize(): number {
const { base, min, marbleCount, marbleArea } = MARBLE_CONFIG.size;
const fieldArea = this.fieldWidth * this.fieldHeight;
const areaPerMarble = (fieldArea * marbleArea) / marbleCount;
const quarter = Math.sqrt((4 * areaPerMarble) / Math.PI);
const capped = Math.min(base, quarter || base);
return Math.max(min, Math.floor(capped)) * this.zoomLevel;
}

View File

@@ -291,7 +291,7 @@ export class MarbleSystem {
// Update marble size (zoom)
public updateMarbleSize(zoomLevel: number): void {
this.factory.setZoomLevel(zoomLevel);
const size = this.calculateMarbleSize(zoomLevel);
const size = this.factory.calculateMarbleSize();
const radius = size / 2;
for (const m of this.marbles) {
@@ -304,15 +304,6 @@ export class MarbleSystem {
}
}
// Calculate marble size
private calculateMarbleSize(zoomLevel: number): number {
const { base, min, maxScreenRatio } = MARBLE_CONFIG.size;
const quarter =
Math.min(window.innerWidth, window.innerHeight) * maxScreenRatio;
const capped = Math.min(base, quarter || base);
return Math.max(min, Math.floor(capped)) * zoomLevel;
}
/**
* Request device motion permission
*/