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: Flocking: a long frame is flown as moves of one 60 Hz frame, and one bird's view links only the neighbors it follows
matin3fe96341cb3b
diff --git a/animations/06-3d-advanced/flocking/README.md b/animations/06-3d-advanced/flocking/README.mdindex d665d7ba..849e340a 100644--- a/animations/06-3d-advanced/flocking/README.md+++ b/animations/06-3d-advanced/flocking/README.md@@ -13,7 +13,7 @@ Flocking makes a crowd of simple dots move like one flock of birds or a school oEvery move, each bird gathers the birds within its view radius `R` that are in front of it or beside it (a bird has a blind spot right behind it), and turns by up to three steering pulls:```js-function step(k) { // k: 60 Hz frames since the last move (a third of that in slow motion)+function step(k) { // k: the length of this move in 60 Hz frames, about 1 at mostfor (const b of birds) {let n = 0, cx = 0, cy = 0, ax = 0, ay = 0, sx = 0, sy = 0;const vb = Math.hypot(b.vx, b.vy);@@ -52,7 +52,11 @@ function steer(b, dx, dy, cap, weight) {The demo adds two more pulls. Inside a margin along each edge a bird turns back, harder the deeper it is in the margin, so the flock curves away from the edges instead of piling up against them. With Flees the pointer on, a bird near the pointer or a finger steers straight away from it with four times the usual turn.-**Time, not frames.** The loop draws at most once per 16 ms, and `k` is the time since the last drawn frame in 60 Hz frames (capped at 50 ms), so the birds fly at the same speed on a 30, 60 or 120 Hz screen. Slow motion multiplies `k` by a third.+**Time, not frames.** The loop draws at most once per 16 ms and flies the birds for the time since the last drawn frame, counted in 60 Hz frames (capped at 50 ms), so they fly at the same speed on a 30, 60 or 120 Hz screen; slow motion counts a third of it. A frame longer than one 60 Hz frame is flown as equal moves of at most one 60 Hz frame each (two moves on a 30 Hz screen), so a bird never turns harder in one move than it would at 60 Hz, and a 30 Hz screen flies exactly the 60 Hz path:++```js+function advance(k) { const n = Math.max(1, Math.ceil(k - 0.05)); for (let i = 0; i < n; i++) step(k / n); }+```**Drawing.** Every bird is an arrowhead pointed along its velocity; all of them go into one path that is filled once per frame.diff --git a/animations/06-3d-advanced/flocking/index.html b/animations/06-3d-advanced/flocking/index.htmlindex 8abc4935..d0ac8eb9 100644--- a/animations/06-3d-advanced/flocking/index.html+++ b/animations/06-3d-advanced/flocking/index.html@@ -211,9 +211,10 @@ function draw(){ctx.fillStyle=BIRD;ctx.fill();if(!VIEW||!birds.length)return;// One bird's view: the ring it looks within, the dashed room it keeps, and a line to every neighbor it follows- const b=birds[0],R=50*S;+ const b=birds[0],R=50*S,vb=Math.hypot(b.vx,b.vy);ctx.strokeStyle=LEAD;ctx.lineWidth=1;ctx.globalAlpha=.55;ctx.beginPath();- for(const o of birds)if(o!==b&&(o.x-b.x)**2+(o.y-b.y)**2<R*R){ctx.moveTo(b.x,b.y);ctx.lineTo(o.x,o.y);}+ for(const o of birds){const dx=o.x-b.x,dy=o.y-b.y,d2=dx*dx+dy*dy; // the same test as in step(): within R, not right behind+ if(o!==b&&d2<=R*R&&dx*b.vx+dy*b.vy>=-.5*vb*Math.sqrt(d2)){ctx.moveTo(b.x,b.y);ctx.lineTo(o.x,o.y);}}ctx.stroke();ctx.beginPath();ctx.arc(b.x,b.y,R,0,Math.PI*2);ctx.stroke();ctx.setLineDash([3,4]);ctx.beginPath();ctx.arc(b.x,b.y,RULES.sep*S,0,Math.PI*2);ctx.stroke();ctx.setLineDash([]);@@ -238,9 +239,11 @@ function loop(ts){if(lastT!==null&&ts-lastT<16)return;const dt=lastT===null?0:Math.min(ts-lastT,50);lastT=ts;- if(dt)step(dt/FRAME*(slowTog.checked?1/3:1));+ if(dt)advance(dt/FRAME*(slowTog.checked?1/3:1));draw();}+// A long frame is flown as equal moves of at most one 60 Hz frame each, so a bird turns no harder in one move on a 30 Hz screen+function advance(k){const n=Math.max(1,Math.ceil(k-.05));for(let i=0;i<n;i++)step(k/n);}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