Skip to content

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.

fix: run Heart Burst at the same speed on every screen refresh rate

matinfe394dcef397
diff --git a/animations/04-micro-interactions/heart-burst/README.md b/animations/04-micro-interactions/heart-burst/README.md
index 678b3509..495ab88a 100644
--- a/animations/04-micro-interactions/heart-burst/README.md
+++ b/animations/04-micro-interactions/heart-burst/README.md
@@ -10,9 +10,12 @@ A like button that celebrates the moment you like something. The heart quickly s
- Not for destructive or neutral toggles — the celebration implies approval
## How it works
-The heart itself is an inline SVG whose fill color transitions on a `.on` class, plus a keyframed pop for the overshoot. The burst is a particle system drawn on a `<canvas>` overlay stretched across the stage. On like, you spawn N particles at the heart's center, each with a random angle and velocity; every frame you advance them, apply gravity and a little sideways drag, and decrement life until they fade out:
+The heart itself is an inline SVG whose fill color transitions on a `.on` class, plus a keyframed pop for the overshoot. The burst is a particle system drawn on a `<canvas>` overlay stretched across the stage. On like, you spawn N particles at the heart's center, each with a random angle and velocity; every frame you advance them by the time that has passed, apply gravity and a little sideways drag, and decrement life until they fade out. 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 the burst lasts about three quarters of a second 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 spawn() {
const b = btn.getBoundingClientRect(), s = stage.getBoundingClientRect();
const ox = b.left + b.width/2 - s.left, oy = b.top + b.height/2 - s.top;
@@ -20,14 +23,18 @@ function spawn() {
const a = Math.random() * Math.PI * 2, v = spread * (0.5 + Math.random()*0.5) / 12;
particles.push({ x: ox, y: oy, vx: Math.cos(a)*v, vy: Math.sin(a)*v - 1, 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);
for (const p of particles) {
- p.x += p.vx; p.y += p.vy; p.vy += 0.12; p.vx *= 0.98; p.life -= 0.022; // move, gravity, drag, fade
+ p.x += p.vx * k; p.y += p.vy * k; p.vy += 0.12 * k; // move, gravity
+ p.vx *= 0.98 ** k; p.life -= 0.022 * k; // drag, fade
ctx.globalAlpha = p.life;
/* draw heart or dot at p.x, p.y */
}
@@ -35,6 +42,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, and Slow motion multiplies `k` by a third.
+
The `-1` bias on initial `vy` makes the burst lean upward before gravity pulls it down. The pop is retriggered by removing and re-adding the animation class (`btn.classList.remove('pop'); void btn.offsetWidth; btn.classList.add('pop')`) so repeated likes always re-fire.
## Key parameters
@@ -45,6 +54,7 @@ The `-1` bias on initial `vy` makes the burst lean upward before gravity pulls i
## Production notes
- **Canvas over DOM particles**: 18–40 short-lived DOM `<span>`s per tap creates layout/GC churn if the user spams the button. One canvas with an array of plain objects stays flat and cheap.
+- **Time, not frames**: scale every per-frame amount by the time since the last frame, and raise a drag factor to the same power (`0.98 ** k`). Without it a burst lasts half as long on a 120 Hz screen and twice as long on a 30 Hz phone, and anything timed around it, such as the demo's Show me, falls out of step.
- **Idle the loop**: stop `requestAnimationFrame` when `particles.length === 0` and restart it on the next spawn. A permanently running rAF wastes battery for a control that fires occasionally.
- **DPR scaling**: size the canvas backing store by `devicePixelRatio` (capped at ~2) and `ctx.setTransform(dpr,0,0,dpr,0,0)` so hearts stay crisp on retina without over-allocating on 3x phones.
- **Reduced motion**: skip the pop and the burst entirely — just toggle the fill color. The state change must still be conveyed, only the celebration is dropped.
diff --git a/animations/04-micro-interactions/heart-burst/index.html b/animations/04-micro-interactions/heart-burst/index.html
index 7609b1a1..016bd89f 100644
--- a/animations/04-micro-interactions/heart-burst/index.html
+++ b/animations/04-micro-interactions/heart-burst/index.html
@@ -139,7 +139,8 @@
const stage=document.querySelector('.stage'),btn=document.querySelector('.like-btn'),heart=btn.querySelector('.heart'),
cv=document.getElementById('cv'),ctx=cv.getContext('2d'),countEl=document.getElementById('count'),
slowTog=document.getElementById('slow-tog'),reduce=window.matchMedia('(prefers-reduced-motion: reduce)');
- let liked=false,count=128,particles=[],raf=null,dpr=1,pCount=18,spread=90,runTimers=[];
+ const FRAME=1000/60; // one frame of a 60 Hz screen, in ms: every step below is measured in these
+ let liked=false,count=128,particles=[],raf=null,last=null,dpr=1,pCount=18,spread=90,runTimers=[];
// The canvas has one pixel per screen pixel (two at most per CSS pixel), so the hearts stay sharp on dense screens.
function resize(){
@@ -158,15 +159,19 @@
particles.push({x:ox,y:oy,vx:Math.cos(a)*v,vy:Math.sin(a)*v-1,life:1,size:4+Math.random()*5,
hue:['#ff5470','#ff9d5c','#ffd166','#ff85a1'][i%4],heart:Math.random()>0.4});
}
- 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);
- // Slow motion: each frame moves every piece a third of its usual step. It is read every frame, so it applies from the next one.
- const k=slowTog.checked?1/3:1,drag=0.98**k;
+ // 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 a burst 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.
+ const dt=last===null?FRAME:Math.min(ts-last,50);last=ts;
+ // Slow motion: every piece moves a third of its usual step. It is read every frame, so it applies from the next one.
+ // 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 k=dt/FRAME*(slowTog.checked?1/3:1),parts=k>1.5?2:1,h=k/parts,drag=0.98**h;
for(const p of particles){
- p.x+=p.vx*k;p.y+=p.vy*k;p.vy+=0.12*k;p.vx*=drag;p.life-=0.022*k;
+ for(let i=0;i<parts;i++){p.x+=p.vx*h;p.y+=p.vy*h;p.vy+=0.12*h;p.vx*=drag;p.life-=0.022*h}
ctx.globalAlpha=Math.max(p.life,0);ctx.fillStyle=p.hue;
if(p.heart){drawHeart(p.x,p.y,p.size);}
else{ctx.beginPath();ctx.arc(p.x,p.y,p.size*0.5,0,Math.PI*2);ctx.fill();}