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 and 2D Physics: the README and comments match the code, and Flocking is trimmed to 300 lines
matin0fd264ea194a
diff --git a/animations/06-3d-advanced/flocking/README.md b/animations/06-3d-advanced/flocking/README.mdindex 849e340a..0de30337 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: the length of this move in 60 Hz frames, about 1 at most+function step(k) { // k: the length of this move in 60 Hz frames, at most 1.05for (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,7 @@ 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 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:+**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 more than 5% longer than one 60 Hz frame is flown as several equal moves, none longer than 1.05 of a 60 Hz frame (the 5% keeps a frame that comes a little late to one move). So a bird never turns much harder in one move than it would at 60 Hz, and a 30 Hz screen, with two moves of exactly one frame, flies exactly the 60 Hz path:```jsfunction advance(k) { const n = Math.max(1, Math.ceil(k - 0.05)); for (let i = 0; i < n; i++) step(k / n); }diff --git a/animations/06-3d-advanced/flocking/index.html b/animations/06-3d-advanced/flocking/index.htmlindex d0ac8eb9..2ccd8512 100644--- a/animations/06-3d-advanced/flocking/index.html+++ b/animations/06-3d-advanced/flocking/index.html@@ -168,9 +168,8 @@ function steer(b,dx,dy,vmax,F,w){if(l>F){x*=F/l;y*=F/l;}acc.x+=x*w;acc.y+=y*w;}-// One move of the flock, k 60 Hz frames long. Each bird looks only at the birds within R in front of and beside it (not-// right behind) and follows the three rules: keep apart (from those within sep), match their direction, and stay close-// (to their middle). Inside the margin M it turns back, the harder the closer it is to the edge.+// One move, k 60 Hz frames long. Each bird heeds only the birds within R, not right behind it: it keeps apart (from those within+// sep), matches their direction and stays close (to their middle). Inside the margin M it turns back, harder nearer the edge.function step(k){const R=50*S,SEP=RULES.sep*S,vmax=SPD*S,vmin=vmax*.55,F=.045*SPD*S,M=70*S,E=F*2.4/M,RF=90*S;for(const b of birds){@@ -195,8 +194,7 @@ function step(k){}}-// A bird is an arrowhead pointing where it flies, added to the current path-function birdPath(b,L,w){+function birdPath(b,L,w){ // an arrowhead pointing where the bird flies, added to the current pathconst sp=Math.hypot(b.vx,b.vy)||1,ux=b.vx/sp,uy=b.vy/sp;ctx.moveTo(b.x+ux*L,b.y+uy*L);ctx.lineTo(b.x-ux*L*.75-uy*w,b.y-uy*L*.75+ux*w);@@ -231,9 +229,8 @@ function recount(){if(paused)draw();}-// 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 move stands for the time since the last frame in 60 Hz frames (a third of it in slow motion), never more than-// 50 ms, so a background tab does not leap; the first frame after a start has nothing to measure from and moves nothing.+// loop() keeps exactly one frame requested in raf; Pause cancels it and Play starts one loop again. At most one frame per 16 ms+// flies the time since the last one (at most 50 ms, a third in slow motion); a first frame after a start only takes the time.function loop(ts){raf=requestAnimationFrame(loop);if(lastT!==null&&ts-lastT<16)return;@@ -242,12 +239,11 @@ function loop(ts){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+// A long frame is flown as equal moves of at most 1.05 of a 60 Hz frame, so a bird turns no harder in one move on a 30 Hz screenfunction 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-// loop on arrival, which this listener must catch. The birds on screen stay where they are while paused.+// At the top level, before the deferred shared script runs: under reduced motion it pauses the loop on arrival. Paused birds stay put.document.addEventListener('hb:pause',e=>{paused=e.detail.paused;if(paused){cancelAnimationFrame(raf);raf=null;}@@ -256,9 +252,8 @@ document.addEventListener('hb:pause',e=>{// A tab that comes back after being hidden has no earlier frame to measure from, so its first frame moves nothingdocument.addEventListener('visibilitychange',()=>{lastT=null;});-// Setting the canvas size wipes it, so a real change of the stage's size draws the flock again at once, playing or paused:-// the first one starts the flock, later ones stretch it to the new size. A resize that leaves the stage as it was (a phone's-// address bar sliding in or out) does nothing.+// Setting the canvas size wipes it, so a real change of the stage's size draws the flock again at once: the first starts the flock,+// later ones stretch it. A resize that leaves the stage as it was (a phone's address bar sliding in or out) does nothing.function resize(){const w=stage.clientWidth,h=stage.clientHeight;if(w===W&&h===H)return;diff --git a/animations/06-3d-advanced/physics-2d/index.html b/animations/06-3d-advanced/physics-2d/index.htmlindex d8051c49..75cc22ac 100644--- a/animations/06-3d-advanced/physics-2d/index.html+++ b/animations/06-3d-advanced/physics-2d/index.html@@ -249,8 +249,8 @@ function drop(x,y){}// Pointer Events cover mouse, touch and pen. Pressing a ball picks up the nearest one in reach (its radius plus 8px, and at least-// a 44px circle); pressing anywhere else drops a new one. While a ball is-// held, the pointer's speed is followed (in px per 1/60 s), and letting go throws the ball with it if the pointer was still moving.+// a 44px circle); pressing anywhere else drops a new one. While a ball is held, the pointer's speed is followed (in px per+// 1/60 s), and letting go throws the ball with it if the pointer was still moving.const local=e=>{const r=cvs.getBoundingClientRect();return {x:e.clientX-r.left,y:e.clientY-r.top};};cvs.addEventListener('pointerdown',e=>{if(!e.isPrimary||e.button!==0)return;