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: run Success Confetti at the same speed on every screen refresh rate
matinee134dba870f
diff --git a/animations/04-micro-interactions/success-confetti/README.md b/animations/04-micro-interactions/success-confetti/README.mdindex c812fb94..54ab9b5d 100644--- a/animations/04-micro-interactions/success-confetti/README.md+++ b/animations/04-micro-interactions/success-confetti/README.md@@ -10,9 +10,12 @@ A burst of confetti that marks a finished task, such as placing an order. The bu- Once per event, on genuine milestones — confetti on every click trains users to ignore it## How it works-A `<canvas>` overlay covers the stage. On click you spawn a batch of confetti pieces at the button's center, each fanned around a straight-up base direction by a configurable spread angle. Every frame applies gravity to `vy`, advances position, spins the piece, and drains its life; pieces are retired once faded or off-screen:+A `<canvas>` overlay covers the stage. On click you spawn a batch of confetti pieces at the button's center, each fanned around a straight-up base direction by a configurable spread angle. Every frame applies gravity to `vy`, advances position, spins the piece, and drains its life; pieces are retired once faded or off-screen. Every step is multiplied by `k`, the time since the last frame measured in 60 Hz frames: 1 on a 60 Hz screen, 0.5 on a 120 Hz one and 2 on a 30 Hz one, so a piece fades out in about two seconds on all of them:```js+const FRAME = 1000 / 60; // one frame of a 60 Hz screen, in ms+let last = null; // time of the previous frame; null until a burst's first frame+function fire() {const half = (spreadDeg * Math.PI/180) / 2;for (let i = 0; i < pCount; i++) {@@ -22,14 +25,19 @@ function fire() {w: 6+Math.random()*6, h: 3+Math.random()*5, rot: Math.random()*Math.PI,spin: (Math.random()-0.5)*0.3, color: COLORS[i % COLORS.length], life: 1 });}- if (!raf) raf = requestAnimationFrame(tick);+ if (!raf) { last = null; raf = requestAnimationFrame(tick); }}-function tick() {+function tick(ts) {+ const dt = last === null ? FRAME : Math.min(ts - last, 50); // ms since the last frame, at most 50+ last = ts;+ const k = dt / FRAME;ctx.clearRect(0, 0, cv.width, cv.height);particles = particles.filter(p => p.life > 0 && p.y < stage.clientHeight + 40);for (const p of particles) {- p.vy += gravity; p.vx *= 0.99; p.x += p.vx; p.y += p.vy; p.rot += p.spin; p.life -= 0.008;+ p.vy += gravity * k; p.vx *= 0.99 ** k; // gravity, drag+ p.x += p.vx * k; p.y += p.vy * k; p.rot += p.spin * k; // move, spin+ p.life -= 0.008 * k; // fadectx.save(); ctx.translate(p.x, p.y); ctx.rotate(p.rot);ctx.globalAlpha = p.life; ctx.fillStyle = p.color;ctx.fillRect(-p.w/2, -p.h/2, p.w, p.h);@@ -39,6 +47,8 @@ function tick() {}```+A burst's first frame counts as one 60 Hz frame, and a gap longer than 50ms (a tab coming back from the background) counts as 50ms, so nothing jumps. The demo also takes a step longer than 1.5 frames, which is a 30 Hz screen, in two halves, so the arc stays close to the one a 60 Hz screen draws.+Because the loop reads live `pCount`, `spreadDeg`, and `gravity` values, changing a setting changes the very next burst. Clicking again simply calls `fire()` again — replay is implicit.## Key parameters@@ -49,6 +59,7 @@ Because the loop reads live `pCount`, `spreadDeg`, and `gravity` values, changin| Fall speed | Normal | How strongly the pieces are pulled down: floaty, normal or heavy; heavy falls fast, floaty drifts |## Production notes+- **Time, not frames**: scale every per-frame amount (gravity, spin, fade) by the time since the last frame, and raise the drag factor to the same power (`0.99 ** k`). Without it the burst runs twice as fast on a 120 Hz screen and half as fast on a 30 Hz phone, and anything timed around it, such as the demo's Show me, falls out of step.- **Off-screen culling**: filter out pieces once `p.y` passes the stage bottom, not just when faded — otherwise fast-falling confetti keeps consuming CPU below the fold.- **One rAF, gated**: start the loop on the first burst and stop it when the array empties. Restart on the next click. Never leave a permanent rAF running for an occasional event.- **DPR-aware canvas**: scale the backing store by `devicePixelRatio` (cap ~2) so rectangles have clean edges on retina without quadrupling fill cost on 3x displays.diff --git a/animations/04-micro-interactions/success-confetti/index.html b/animations/04-micro-interactions/success-confetti/index.htmlindex f45a2e29..36858b1d 100644--- a/animations/04-micro-interactions/success-confetti/index.html+++ b/animations/04-micro-interactions/success-confetti/index.html@@ -143,7 +143,8 @@lbl=document.getElementById('btn-lbl'),cv=document.getElementById('cv'),ctx=cv.getContext('2d'),reduce=window.matchMedia('(prefers-reduced-motion: reduce)');const COLORS=['#ff9d5c','#ff5470','#3fb950','#58a6ff','#ffd166','#c792ea'];- let particles=[],raf=null,dpr=1,pCount=120,spreadDeg=110,gravity=0.28,runTimers=[];+ const FRAME=1000/60; // one frame of a 60 Hz screen, in ms: every step below is measured in these+ let particles=[],raf=null,last=null,dpr=1,pCount=120,spreadDeg=110,gravity=0.28,runTimers=[];// The canvas has one pixel per screen pixel (two at most per CSS pixel), so the pieces stay sharp on dense screens.function resize(){@@ -167,13 +168,18 @@w:6+Math.random()*6,h:3+Math.random()*5,rot:Math.random()*Math.PI,spin:(Math.random()-0.5)*0.3,color:COLORS[i%COLORS.length],life:1});}- if(!raf)raf=requestAnimationFrame(tick);+ if(!raf){last=null;raf=requestAnimationFrame(tick)}}- function tick(){+ function tick(ts){ctx.clearRect(0,0,cv.width,cv.height);particles=particles.filter(p=>p.life>0&&p.y<stage.clientHeight+40);+ // Each step is scaled by the time since the last frame, in 60 Hz frames (1 at 60 Hz, 0.5 at 120 Hz, 2 at 30 Hz), so the confetti+ // lasts the same on every screen. A burst's first frame counts as one 60 Hz frame, and no gap counts as more than 50 ms.+ // A step over 1.5 frames (a 30 Hz screen) is taken in two halves, which keeps the arc close to the one a 60 Hz screen draws.+ const dt=last===null?FRAME:Math.min(ts-last,50);last=ts;+ const k=dt/FRAME,parts=k>1.5?2:1,h=k/parts,drag=0.99**h;for(const p of particles){- p.vy+=gravity;p.vx*=0.99;p.x+=p.vx;p.y+=p.vy;p.rot+=p.spin;p.life-=0.008;+ for(let i=0;i<parts;i++){p.vy+=gravity*h;p.vx*=drag;p.x+=p.vx*h;p.y+=p.vy*h;p.rot+=p.spin*h;p.life-=0.008*h}ctx.save();ctx.translate(p.x,p.y);ctx.rotate(p.rot);ctx.globalAlpha=Math.max(p.life,0);ctx.fillStyle=p.color;ctx.fillRect(-p.w/2,-p.h/2,p.w,p.h);ctx.restore();