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 Flow Field only when a whole dim is due, so every drawn frame dims exactly once
matina16972122283
diff --git a/animations/07-ambient-background/flow-field/README.md b/animations/07-ambient-background/flow-field/README.mdindex a09cff30..ec02cdf3 100644--- a/animations/07-ambient-background/flow-field/README.md+++ b/animations/07-ambient-background/flow-field/README.md@@ -23,17 +23,21 @@ function angleAt(x, y, time) {return vnoise(x/SCALE, y/SCALE + time*0.15) * Math.PI * 4;}-// per frame: fade, then advance every particle along its local angle-// s is how many 60 Hz frames this one stands for: 1 at 60 Hz, 2 at 30 Hz-const s = Math.min(now - last, 50) / (1000 / 60);-owed += s; const washes = Math.round(owed); owed -= washes; // whole 60ths of a second to fade; the rest carries over+// per drawn frame: fade, then advance every particle along its local angle+// s is how many 60 Hz frames have passed since the last drawn frame: 1 at 60 Hz, 2 at 30 Hz+const s = Math.min(now - last, 50) / (1000 / 60), w = Math.max(1, Math.round(s));+const f = fa + (Math.abs(s - w) < 0.05 ? w : s); // a step within 5% of a whole number counts as that number+const nf = Math.round(f + 0.05); // whole dims due; the 0.05 keeps a half-way tie off the rounding edge+if (!nf) return; // none yet: draw nothing, the time carries to the next frame+fa = f - nf; last = now; // the fraction left over carries tooctx.fillStyle = `rgba(5,6,10,${TRAIL})`;-for (let i = 0; i < washes; i++) ctx.fillRect(0, 0, W, H);+for (let i = 0; i < nf; i++) ctx.fillRect(0, 0, W, H);+const k = s * (slow ? 1 / 3 : 1); // slow motion shortens the step, not the dimconst a = angleAt(p.x, p.y, t);-p.x += Math.cos(a) * SPD * s; p.y += Math.sin(a) * SPD * s;+p.x += Math.cos(a) * SPD * k; p.y += Math.sin(a) * SPD * k;```-The step grows with the time since the last frame, and so does the fade, in whole units: the canvas is dimmed once for every 60th of a second that has passed, each time by the same `TRAIL` as at 60 Hz. A 30 Hz frame dims it twice; on a 144 Hz screen, where the demo draws every third frame, it is dimmed five times in every four frames. So a 30 Hz phone and a 144 Hz monitor show particles at the same speed with trails of the same length in seconds. `owed` keeps the fraction that is left over, and rounding rather than "at least one" gives a 60 Hz screen exactly one dim per frame even when its frame times are uneven. One dim of `1 - (1 - TRAIL)^s` would fade the same amount in theory, but an 8-bit canvas rounds every dim, so the faint leftovers would settle to different colors on different screens; repeating the same dim makes them settle to the same colors everywhere. The first frame after a start, a pause or a return from a hidden tab adds nothing, and a long gap counts for at most 50 ms. The still picture drawn on arrival runs 40 steps of `s = 1` with one dim each, so it looks the same on every screen.+The step grows with the time since the last drawn frame, and so does the fade, in whole units: the canvas is dimmed once for every 60th of a second, each time by the same `TRAIL` as at 60 Hz, so a 30 Hz frame dims it twice. A step within 5% of a whole number counts as that number for the dim, so a 59.94 or 60.06 Hz screen, or a little noise in the timestamps, still gets exactly one dim 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 dims, such as 120 or 240 Hz, keeps a regular draw rhythm instead of one decided by timestamp noise. When no whole dim 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, and every drawn frame at one dim, so the trails do not pulse. A 30 Hz phone and a 144 Hz monitor show particles at the same speed with trails of the same length in seconds. One dim of `1 - (1 - TRAIL)^s` would fade the same amount in theory, but an 8-bit canvas rounds every dim, so the faint leftovers would settle to different colors on different screens; repeating the same dim makes them settle to the same colors everywhere. The first frame after a start, a pause or a return from a hidden tab adds nothing, and a long gap counts for at most 50 ms. The still picture drawn on arrival runs 40 steps of `s = 1` with one dim each, so it looks the same on every screen.## Key parameters| Parameter | Default | Effect |diff --git a/animations/07-ambient-background/flow-field/index.html b/animations/07-ambient-background/flow-field/index.htmlindex 166c5e6e..fe2b8622 100644--- a/animations/07-ambient-background/flow-field/index.html+++ b/animations/07-ambient-background/flow-field/index.html@@ -188,10 +188,8 @@return HUES[Math.floor((p.x/W*120+t*20)%360)];}- // One frame: dim the canvas a little instead of clearing it (nf times, see loop()), then move every particle one step- // along the field and draw that step. s is how many 60 Hz frames this one stands for (1 at 60 Hz, 2 at 30 Hz). k is s, or- // a third of it in slow motion: the step and the field's time shrink by it, the fade does not. The dim is always today's,- // once per 60th of a second, so trails last as long and faint leftovers settle to the same colors on any screen.+ // One frame: dim the canvas nf times, then move every particle one step along the field and draw that step. s is how many+ // 60 Hz frames it stands for; k is s, or a third of it in slow motion, and shrinks the step and the field's time but not the dim.function frame(s,nf){const k=s*(slowTog.checked?1/3:1);ctx.fillStyle=`rgba(5,6,10,${TRAIL})`;@@ -214,20 +212,16 @@unseen=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 dim goes on once per- // whole 60th: fa carries the fraction over, and rounding (not "at least 1") keeps a 60 Hz screen at one dim 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 (s, at most 50 ms) and dims once per+ // whole 60th, fa carrying the rest; with no dim 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 s=dt/FRAME;- fa+=s;const nf=Math.round(fa);fa-=nf;- frame(s,nf);unseen=false;- }+ if(lastT===null){lastT=ts;return;}+ const s=Math.min(ts-lastT,50)/FRAME,w=Math.max(1,Math.round(s));+ const f=fa+(Math.abs(s-w)<.05?w:s),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;+ frame(s,nf);unseen=false;}function start(){if(raf)return;lastT=null;fa=0;raf=requestAnimationFrame(loop);}