mobile jumping background fix
This commit is contained in:
@@ -6,4 +6,5 @@ canvas {
|
||||
|
||||
width: 100vw;
|
||||
height: 100vh;
|
||||
height: 100svh;
|
||||
}
|
||||
|
||||
@@ -20,6 +20,10 @@ export class DotBackground implements OnDestroy {
|
||||
private readonly COLORS = ['#6366f1', '#8b5cf6', '#a855f7', '#3b82f6'];
|
||||
private readonly MOUSE_RADIUS = 150;
|
||||
|
||||
private lastWidth = 0;
|
||||
private lastHeight = 0;
|
||||
private readonly RESIZE_THRESHOLD = 100; // ignore small height changes (browser chrome)
|
||||
|
||||
constructor(private ngZone: NgZone) {
|
||||
afterNextRender(() => {
|
||||
this.init();
|
||||
@@ -49,24 +53,27 @@ export class DotBackground implements OnDestroy {
|
||||
|
||||
private resize = () => {
|
||||
const canvas = this.canvasRef.nativeElement;
|
||||
const viewport = window.visualViewport;
|
||||
const width = window.innerWidth;
|
||||
const height = window.innerHeight;
|
||||
|
||||
if (viewport) {
|
||||
// Modern browsers
|
||||
canvas.width = Math.round(viewport.width * viewport.scale);
|
||||
canvas.height = Math.round(viewport.height * viewport.scale);
|
||||
} else {
|
||||
// Fallback
|
||||
canvas.width = window.innerWidth;
|
||||
canvas.height = window.innerHeight;
|
||||
const widthChanged = width !== this.lastWidth;
|
||||
const heightDiff = Math.abs(height - this.lastHeight);
|
||||
const significantHeightChange = heightDiff > this.RESIZE_THRESHOLD;
|
||||
|
||||
// Only resize on width change or significant height change (orientation flip, not browser chrome)
|
||||
if (!widthChanged && !significantHeightChange && this.lastWidth !== 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Clamp dots to new bounds
|
||||
this.lastWidth = width;
|
||||
this.lastHeight = height;
|
||||
|
||||
canvas.width = width;
|
||||
canvas.height = height;
|
||||
|
||||
for (const dot of this.dots) {
|
||||
dot.x = Math.min(dot.x, canvas.width - dot.radius);
|
||||
dot.y = Math.min(dot.y, canvas.height - dot.radius);
|
||||
dot.x = Math.max(dot.x, dot.radius);
|
||||
dot.y = Math.max(dot.y, dot.radius);
|
||||
dot.x = Math.max(dot.radius, Math.min(width - dot.radius, dot.x));
|
||||
dot.y = Math.max(dot.radius, Math.min(height - dot.radius, dot.y));
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user