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.
HTML ai-promptsanimationcss-animationsfrontendgithub-pagesjavascriptmicrointeractionsmotion-designno-dependenciesreferencescroll-animationui-animationweb-animationweb-developmentwebgl
fix: draw Starfield only when a whole cover is due, so every drawn frame covers exactly once
matin2b70c74fe723
diff --git a/animations/07-ambient-background/starfield/README.md b/animations/07-ambient-background/starfield/README.mdindex 2747fe92..c0d3173e 100644--- a/animations/07-ambient-background/starfield/README.md+++ b/animations/07-ambient-background/starfield/README.md@@ -59,29 +59,32 @@ class DriftStar {this.depth = Math.random() * 0.8 + 0.2; // 0.2 (far) to 1.0 (near)this.size = this.depth * 2;}- update(k) { this.x -= this.depth * SPEED * k; if (this.x < 0) this.x = W; }+ update(k) { this.x -= this.depth * SPEED * 2 * k; if (this.x < 0) this.x = W; }}```-`k` is the number of 60 Hz frames the last frame stands for, so a 30 Hz phone and a 144 Hz monitor show the same speed. 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. The stars move by `k`, or by a third of it in slow motion:+`n` is the number of 60 Hz frames since the last drawn frame, so a 30 Hz phone and a 144 Hz monitor show the same speed. 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. The stars move by `k`, which is `n`, or a third of it in slow motion:```js-const dt = last === null ? 0 : Math.min(now - last, 50); // ms since the last frame-last = now;-const k = dt / (1000 / 60); // 1 at 60 Hz, 2 at 30 Hz+const n = Math.min(now - last, 50) / (1000 / 60); // 60ths of a second since the last drawn frame: 1 at 60 Hz, 2 at 30 Hz+const k = n * (slow ? 1 / 3 : 1); // what update(k) gets```-The short trails come from covering the canvas with a see-through black layer instead of clearing it. The layer is always the 0.85 one of a 60 Hz frame, and it goes on once for every 60th of a second that has passed (twice on a 30 Hz frame), so the trails last as long in seconds on every screen. Slow motion does not slow them. A running fraction carries the rest to the next frame, and rounding rather than "at least one" keeps a 60 Hz screen at exactly one cover per frame even when its frame times are uneven:+The short trails come from covering the canvas with a see-through black layer instead of clearing it. The layer is always the 0.85 one of a 60 Hz frame, and it goes on once for every 60th of a second (twice on a 30 Hz frame), so the trails last as long in seconds on every screen. Slow motion does not slow them. An `n` within 5% of a whole number counts as that number for the cover, so a 59.94 or 60.06 Hz screen, or a little noise in the timestamps, still gets exactly one cover per frame, and `fa` carries the fraction that is left over. Rounding leans up by 0.05, so a screen whose frames land exactly half-way between two covers, such as 120 or 240 Hz, keeps a regular draw rhythm instead of one decided by timestamp noise. When no whole cover is due yet, as on the first of two frames of a 120 Hz screen, the frame draws nothing and its time carries to the next one. That keeps drawing at about 60 frames a second whatever the screen, with one cover on every drawn frame:```js-owed += k; const covers = Math.round(owed); owed -= covers;-for (let i = 0; i < covers; i++) {+const w = Math.max(1, Math.round(n));+const f = fa + (Math.abs(n - w) < 0.05 ? w : n); // n within 5% of a whole number counts as that number+const nf = Math.round(f + 0.05); // whole covers due; the 0.05 keeps a half-way tie off the rounding edge+if (!nf) return; // none yet: draw nothing, the time carries on+fa = f - nf; last = now; // the fraction left over carries too+for (let i = 0; i < nf; i++) {ctx.fillStyle = 'rgba(0,0,0,0.85)'; ctx.fillRect(0, 0, W, H);if (haze) drawHaze(); // the nebula haze goes on after each cover, as it does at 60 Hz}```-One stronger cover, `1 - 0.15 ** k`, would fade the same amount in theory, but an 8-bit canvas rounds every cover, so the faint leftovers, such as the haze, would settle to different colors on different screens. Repeating the same cover makes them settle to the same colors everywhere.+One stronger cover, `1 - 0.15 ** n`, would fade the same amount in theory, but an 8-bit canvas rounds every cover, so the faint leftovers, such as the haze, would settle to different colors on different screens. Repeating the same cover makes them settle to the same colors everywhere.## Key parameters| Parameter | Default | Effect |@@ -97,7 +100,7 @@ One stronger cover, `1 - 0.15 ** k`, would fade the same amount in theory, but a- **Canvas vs DOM**: DOM elements at star counts above 50 cause heavy layout recalculation. Canvas is the right tool for this effect.- **Phones**: the demo draws at most 300 stars on phone-sized screens (up to 600px wide, or up to 500px tall for a phone held sideways); each star is a separate fill, so the count is the main cost.- **`ctx.fillStyle` caching**: setting `fillStyle` per star is expensive. Group stars by opacity bucket and set fillStyle once per bucket (color batching) to reduce canvas state changes.-- **Same speed on every screen**: a fixed step per `requestAnimationFrame` frame runs twice as fast on a 120Hz display and half as fast on a 30Hz phone. The demo scales each step by the time since the last frame (in 60ths of a second, capped at 50 ms), so the stars move at the same speed everywhere, and it draws at most once every 16 ms so a fast display does no extra work.+- **Same speed on every screen**: a fixed step per `requestAnimationFrame` frame runs twice as fast on a 120Hz display and half as fast on a 30Hz phone. The demo scales each step by the time since the last drawn frame (in 60ths of a second, capped at 50 ms), so the stars move at the same speed everywhere, and it draws only when a whole 60th of a second is due, about 60 times a second on any display, so a fast display does no extra work.- **Nebula background pairing**: adding a subtle radial gradient (deep purple in one quadrant, deep blue in another) behind the stars dramatically increases realism with minimal performance cost.- **Three.js `Points` geometry**: production starfields use Three.js `BufferGeometry` with `PointsMaterial`. Each star is a vertex; the position buffer is updated each frame. This approach scales to 100,000+ stars.- **`prefers-reduced-motion`**: keep stars static (no animation loop) or limit to a very slow drift at 10% of normal speed.diff --git a/animations/07-ambient-background/starfield/index.html b/animations/07-ambient-background/starfield/index.htmlindex be5aa38c..598f05df 100644--- a/animations/07-ambient-background/starfield/index.html+++ b/animations/07-ambient-background/starfield/index.html@@ -240,11 +240,9 @@ctx.fillStyle=g2;ctx.fillRect(0,0,W,H);}- // One picture. paint(true,n,nf) is a frame of the loop for n 60 Hz frames (1 at 60 Hz, 2 at 30 Hz): a see-through cover- // leaves short trails, put on nf times (see loop()) with the haze redrawn after each, so trails last as long and faint- // leftovers settle to the same colors on any screen; then the stars move by n (a third of it in slow motion, while the- // cover does not slow) and are drawn. paint(false) draws the picture where it stands, for the first frame, after a resize- // and after a setting changes while paused; nothing moved, so it covers the old picture completely.+ // One picture. paint(true,n,nf) is a loop frame for n 60 Hz frames: nf covers (the haze redrawn after each), then the stars move by n+ // (a third of it in slow motion; the covers do not slow) and are drawn. paint(false) redraws the picture where it stands, opaque:+ // for the first frame, a resize and a setting changed while paused.function paint(move,n,nf){for(let i=0,c=move?nf:1;i<c;i++){ctx.fillStyle=move?'rgba(0,0,0,.85)':'#000';ctx.fillRect(0,0,W,H);@@ -256,20 +254,16 @@// While paused, a setting that changes the picture draws it again so the change showsfunction still(){if(paused)paint(false);}- // loop() keeps exactly one frame requested in raf; Pause cancels it and Play starts one loop again. At most one frame per- // 16 ms, each for the time since the last one in 60ths of a second, never more than 50 ms. The cover goes on once per- // whole 60th: fa carries the fraction over, and rounding (not "at least 1") keeps a 60 Hz screen at one cover per frame- // even with uneven frame times. The first frame after a start has nothing to measure from and leaves the picture alone.+ // loop() keeps one frame requested in raf. A drawn frame stands for the time since the last drawn one (n, at most 50 ms) and covers once+ // per whole 60th, fa carrying the rest; with no cover due nothing is drawn and the time carries on, which also caps drawing near 60 a second.function loop(ts){raf=requestAnimationFrame(loop);- if(lastT!==null&&ts-lastT<16)return;- const dt=lastT===null?0:Math.min(ts-lastT,50);- lastT=ts;- if(dt){- const n=dt/FRAME;- fa+=n;const nf=Math.round(fa);fa-=nf;- paint(true,n,nf);- }+ if(lastT===null){lastT=ts;return;}+ const n=Math.min(ts-lastT,50)/FRAME,w=Math.max(1,Math.round(n));+ const f=fa+(Math.abs(n-w)<.05?w:n),nf=Math.round(f+.05); // within 5% of a whole number counts as it; the .05 keeps a half-way tie off the edge+ if(!nf)return;+ fa=f-nf;lastT=ts;+ paint(true,n,nf);}function start(){if(raf)return;lastT=null;fa=0;raf=requestAnimationFrame(loop);}