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: fade Flow Field's trails by repeating today's dim once per 60th of a second
matinf66f26b20b62
diff --git a/animations/07-ambient-background/flow-field/README.md b/animations/07-ambient-background/flow-field/README.mdindex 3918526b..a09cff30 100644--- a/animations/07-ambient-background/flow-field/README.md+++ b/animations/07-ambient-background/flow-field/README.md@@ -26,12 +26,14 @@ function angleAt(x, y, time) {// 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 Hzconst s = Math.min(now - last, 50) / (1000 / 60);-ctx.fillStyle = `rgba(5,6,10,${1 - Math.pow(1 - TRAIL, s)})`; ctx.fillRect(0, 0, W, H);+owed += s; const washes = Math.round(owed); owed -= washes; // whole 60ths of a second to fade; the rest carries over+ctx.fillStyle = `rgba(5,6,10,${TRAIL})`;+for (let i = 0; i < washes; i++) ctx.fillRect(0, 0, W, H);const a = angleAt(p.x, p.y, t);p.x += Math.cos(a) * SPD * s; p.y += Math.sin(a) * SPD * s;```-Both the step and the fade grow with the time since the last frame, so a 30 Hz phone and a 144 Hz monitor show particles at the same speed with trails of the same length in seconds. A frame that stands for two 60 Hz frames moves each particle twice as far and dims the canvas as much as two fades would (`1 - (1 - TRAIL)^s`, the same as `TRAIL` when `s` is 1). 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`, so it looks the same on every screen.+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.## 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 e040d3ef..166c5e6e 100644--- a/animations/07-ambient-background/flow-field/index.html+++ b/animations/07-ambient-background/flow-field/index.html@@ -121,7 +121,7 @@<div><h2 id="hb-prompt-title">Copy the prompt</h2><p>Paste it into an AI assistant such as ChatGPT, Claude or Cursor to build this in your own project.</p></div></div><div class="hb-card">- <p class="hb-prompt">Add a flow field background to [the section you want it behind]. Fill a canvas with many particles and give every point a direction that changes smoothly from spot to spot (smooth random noise does this). On every frame, move each particle a small step along the direction under it, scaled by the time since the last frame so it runs at the same speed on every screen. Instead of clearing the canvas, cover it each frame with a see-through dark layer, stronger after a longer frame, so each particle leaves a fading trail. Let the directions change slowly over time, and use fewer particles on phones. If the visitor has reduced motion turned on, show one still frame of trails. Match the settings listed below.</p>+ <p class="hb-prompt">Add a flow field background to [the section you want it behind]. Fill a canvas with many particles and give every point a direction that changes smoothly from spot to spot (smooth random noise does this). Each frame, move each particle a small step along the direction under it, scaled by the time since the last frame so it runs at the same speed on every screen. Instead of clearing the canvas, cover it with a see-through dark layer once for every 60th of a second that passes, so each particle leaves a fading trail. Let the directions change slowly over time, and use fewer particles on phones. If the visitor has reduced motion turned on, show one still frame of trails. Match the settings listed below.</p><div class="hb-chips-box" hidden><p class="hb-chips-title">Your settings</p><ul class="hb-chips"></ul>@@ -159,7 +159,7 @@const PHONE_PARTS=500; // phones and short screens draw at most this many particlesconst phone=matchMedia('(max-width:600px),(max-height:500px)');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,paused=false,unseen=false,t=0;+ let W=0,H=0,raf=null,lastT=null,fa=0,paused=false,unseen=false,t=0; // fa: the part of a dim not yet appliedlet COUNT=900,SCALE=34,SPD=1.0,TRAIL=0.06,COL='mint';let parts=[];@@ -188,13 +188,14 @@return HUES[Math.floor((p.x/W*120+t*20)%360)];}- // One frame: dim the canvas a little instead of clearing it, 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): the dim, the step and the- // field's time all grow with it, so the trails last as long and the particles travel as far in a second on any screen.- // 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.- function frame(s){+ // 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.+ function frame(s,nf){const k=s*(slowTog.checked?1/3:1);- ctx.fillStyle=`rgba(5,6,10,${1-Math.pow(1-TRAIL,s)})`;ctx.fillRect(0,0,W,H);+ ctx.fillStyle=`rgba(5,6,10,${TRAIL})`;+ for(let i=0;i<nf;i++)ctx.fillRect(0,0,W,H);for(const p of parts){const a=angleAt(p.x,p.y,t);const nx=p.x+Math.cos(a)*SPD*k,ny=p.y+Math.sin(a)*SPD*k;@@ -209,21 +210,26 @@// is covered completely first, because a see-through dim leaves a faint ghost of whatever was there beforefunction prerender(){ctx.fillStyle='#05060a';ctx.fillRect(0,0,W,H);- for(let i=0;i<40;i++)frame(1);+ for(let i=0;i<40;i++)frame(1,1);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, and each stands for the time since the last one in 60ths of a second, never more than 50 ms (a background tab- // does not leap). The first frame after a start has nothing to measure from, so it leaves the picture as it is.+ // 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.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){frame(dt/FRAME);unseen=false;}+ if(dt){+ const s=dt/FRAME;+ fa+=s;const nf=Math.round(fa);fa-=nf;+ frame(s,nf);unseen=false;+ }}- function start(){if(raf)return;lastT=null;raf=requestAnimationFrame(loop);}+ function start(){if(raf)return;lastT=null;fa=0;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, unless a@@ -234,7 +240,7 @@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;});+ document.addEventListener('visibilitychange',()=>{lastT=null;fa=0;});// Setting the canvas size wipes it, so a real change of the stage's size starts the particles again with a settled// picture 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.