Skip to content

176 web animation techniques, each a live demo with plain settings and a ready-made prompt for your AI assistant. No dependencies, no build step.

fix: run Synthwave Grid at the same speed on every screen

matin6af96ce076b3
diff --git a/animations/07-ambient-background/synthwave-grid/README.md b/animations/07-ambient-background/synthwave-grid/README.md
index b6f3d01b..5837a67d 100644
--- a/animations/07-ambient-background/synthwave-grid/README.md
+++ b/animations/07-ambient-background/synthwave-grid/README.md
@@ -22,6 +22,14 @@ for (let i = 0; i < DENS; i++) {
}
```
+The offset grows with time, not with the number of frames: every 60th of a second adds `speed * 0.02` (a third of that in slow motion), however many frames that takes, so the floor rolls at the same speed on a 30 Hz phone and a 144 Hz monitor. At normal speed that is 1.2 lines a second. The first frame after a start, a pause or a return from a hidden tab adds nothing, and a long gap between frames counts for at most 50 ms:
+
+```js
+const dt = last === null ? 0 : Math.min(now - last, 50); // ms since the last frame
+last = now;
+offset += speed * 0.02 * dt / (1000 / 60) * (slow ? 1 / 3 : 1);
+```
+
The glow is a canvas `shadowBlur` set to the grid colour; the sun is a clipped semicircle filled with a vertical gradient and striped with background-coloured gaps.
## Key parameters
diff --git a/animations/07-ambient-background/synthwave-grid/index.html b/animations/07-ambient-background/synthwave-grid/index.html
index 111714d0..63ca0fe3 100644
--- a/animations/07-ambient-background/synthwave-grid/index.html
+++ b/animations/07-ambient-background/synthwave-grid/index.html
@@ -133,7 +133,8 @@
const cvs=document.getElementById('cvs'),ctx=cvs.getContext('2d');
const stage=document.querySelector('.stage');
const slowTog=document.getElementById('slow-tog');
- let W=0,H=0,raf=null,lastT=0,offset=0;
+ const FRAME=1000/60; // one step is what a 60 Hz frame adds, so a faster screen or a slow frame does not change the speed
+ let W=0,H=0,raf=null,lastT=null,offset=0;
let SPD=1,DENS=16,GLOW='#ff2fb0';
function horizon(){return H*0.42;}
@@ -181,16 +182,18 @@
// The whole picture at the current offset. It paints every pixel, so nothing of an earlier picture stays.
function frame(){drawSky();drawGrid();}
- // loop() keeps exactly one frame requested in raf; Pause cancels it and Play starts one loop again.
- // At most one step per 16 ms; a step is Speed x 0.02, or a third of that in slow motion.
+ // loop() keeps exactly one frame requested in raf; Pause cancels it and Play starts one loop again. At most one frame per
+ // 16 ms. A step is Speed x 0.02, or a third of that in slow motion, for every 60th of a second since the last frame (at
+ // most 50 ms's worth, so a background tab does not leap); the first frame after a start has nothing to measure from and adds none.
function loop(ts){
raf=requestAnimationFrame(loop);
- if(ts-lastT<16)return;
+ if(lastT!==null&&ts-lastT<16)return;
+ const dt=lastT===null?0:Math.min(ts-lastT,50);
lastT=ts;
- offset+=SPD*0.02*(slowTog.checked?1/3:1);
+ offset+=SPD*0.02*dt/FRAME*(slowTog.checked?1/3:1);
frame();
}
- function start(){if(raf)return;lastT=0;raf=requestAnimationFrame(loop);}
+ function start(){if(raf)return;lastT=null;raf=requestAnimationFrame(loop);}
// Registered at the top level, before the shared script (deferred) can run: under reduced motion it pauses the
// loop on arrival, which this listener must catch. The picture on screen stays as it is while paused.
@@ -198,6 +201,8 @@
if(e.detail.paused){cancelAnimationFrame(raf);raf=null;}
else start();
});
+ // A tab that comes back after being hidden has no earlier frame to measure from, so its first frame adds no step
+ document.addEventListener('visibilitychange',()=>{lastT=null;});
// Setting the canvas size wipes it, so a real change of the stage's size draws the picture again at once,
// playing or paused. A resize that leaves the stage as it was (a phone's address bar sliding in or out) does nothing.