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: GPGPU Particle System draws only when a pass is due, measuring from the last drawn frame and snapping near-whole steps, so it paints one pass and one fade per picture from 59.94 Hz up

matin05fc84e4b4db
diff --git a/animations/06-3d-advanced/gpgpu-particle-system/README.md b/animations/06-3d-advanced/gpgpu-particle-system/README.md
index 805b1261..7e1e4d6a 100644
--- a/animations/06-3d-advanced/gpgpu-particle-system/README.md
+++ b/animations/06-3d-advanced/gpgpu-particle-system/README.md
@@ -50,7 +50,7 @@ void main() {
}
```
-**Time step** — every physics pass moves the particles by exactly one 60 Hz step: `uStep` is 1 (a third of that in slow motion), on every screen. The page keeps the time since the last drawn frame that has not been stepped yet (a gap counts for at most 50 ms), runs one pass for every 1/60 s of it (allowing 1 ms of slack, and at most three passes a frame) and carries the rest over: one pass a frame on a 60 Hz screen, two on a 30 Hz one. The particles move at the same speed on every screen, and the attractor, which pulls hard near the middle and settles differently for a longer or a shorter step, looks the same too. The trail fade follows the passes: one fade of 12% after every pass (two on a 30 Hz screen), and none on a frame that ran no pass, which draws nothing because nothing moved. The streaks therefore last as long on every screen, and slow motion, which still runs 60 passes a second, slows the particles but not the fade. One smaller fade would fade the same amount, but an 8-bit canvas rounds it differently, and the faint leftovers of a trail would end up another color.
+**Time step** — every physics pass moves the particles by exactly one 60 Hz step: `uStep` is 1 (a third of that in slow motion), on every screen. The page looks at every frame the screen offers and measures the time since the last drawn one in steps of 1/60 s (a gap counts for at most 50 ms, and a value within 5% of a whole number of steps counts as that number, so a screen a little off 60 Hz, such as 59.94 or 119.88 Hz, still runs exactly one pass a frame). It runs one pass for every whole step it has added up (allowing 1 ms of slack, and at most three passes a frame) and carries the rest over: one pass a frame on a 60 Hz screen, two on a 30 Hz one, and none on a frame that comes too soon, which changes nothing and draws nothing, so the drawing stays near 60 pictures a second on a faster screen. The particles move at the same speed on every screen, and the attractor, which pulls hard near the middle and settles differently for a longer or a shorter step, looks the same too. A drawn frame lays the trail fade over the picture once for every pass it ran, after the passes, each time 12% of the old picture; slow motion still runs 60 passes a second, so it slows the particles but not the fade. One smaller fade would fade the same amount, but an 8-bit canvas rounds it differently, and the faint leftovers of a trail would end up another color.
**Texture ping-pong** — alternate which texture is the read source each pass:
diff --git a/animations/06-3d-advanced/gpgpu-particle-system/index.html b/animations/06-3d-advanced/gpgpu-particle-system/index.html
index f07d01d0..f787026b 100644
--- a/animations/06-3d-advanced/gpgpu-particle-system/index.html
+++ b/animations/06-3d-advanced/gpgpu-particle-system/index.html
@@ -302,26 +302,28 @@ function resize(){
// One step is 1/60 s: the rule moves the particles by uSpd x 0.0002 in that time, whatever the screen's refresh rate.
const FRAME=1000/60;
// Every physics pass is exactly one 60 Hz step (uStep 1, or 1/3 in Slow motion), never a longer or a shorter one: the Attract
-// rule pulls hard near the middle, and how tightly the particles settle there depends on the size of the step. "behind" is the
-// time since the last drawn frame that has not been stepped yet. Each frame adds its time (a gap counts for at most 50 ms), runs
-// one pass for every 1/60 s behind (1 ms of slack, at most 3 passes) and carries the rest: one pass a frame on a 60 Hz screen,
-// two on a 30 Hz one. The clock adds up the passes, so a pause never makes the particles jump. The Trails fade follows the
-// passes: one fade, always the same 12%, for every pass (60 a second, in Slow motion too), so the streaks last as long on every
-// screen, the faint leftovers of a trail end up the same color (a smaller fade is rounded differently by the 8-bit canvas), and
-// Slow motion does not slow them. A frame that ran no pass changes nothing and draws nothing. The loop keeps exactly one
-// pending frame in raf, and draws at most one picture per 12 ms. A tab that comes back to the front starts again from its next
-// frame, so the time away adds no step.
-let behind=0;
+// rule pulls hard near the middle, and how tightly the particles settle there depends on the size of the step. The loop looks at
+// every frame the screen offers. "steps" is the time since the last drawn frame in steps of 1/60 s (a gap counts for at most 50
+// ms); "due" is the same, except that a value within 5% of a whole number counts as that number, so a screen a little off 60 Hz,
+// or a little unsteady, still runs exactly one pass a frame. "owed" carries the part of a step not run yet, and a frame runs one
+// pass for every whole step owed (1 ms of slack, at most 3 passes): one on a 60 Hz screen, two on a 30 Hz one. A frame with no
+// pass due changes nothing and draws nothing, and its time carries to the next frame; that keeps the drawing near 60 pictures a
+// second on faster screens. A drawn frame lays the Trails fade over the picture once for every pass, always at the same 12%:
+// the streaks last as long on every screen, the faint leftovers of a trail end up the same color (a smaller fade is rounded
+// differently by the 8-bit canvas), and Slow motion, which still runs 60 passes a second, does not slow them. The clock adds up
+// the passes, so a pause never makes the particles jump. A tab that comes back to the front starts again from its next frame,
+// so the time away adds no step.
+let owed=0;
function render(ts){
raf=requestAnimationFrame(render);
if(last===null){last=ts;return;} // the first frame after a start only takes the time
- const gap=ts-last;if(gap<12)return;
- last=ts;
- const dt=Math.min(gap,50),unit=slowTog.checked?1/3:1;
- behind+=dt;
- let passes=0;
- for(;behind>=FRAME-1&&passes<3;passes++){simT+=unit/60;physics(simT,unit);behind-=FRAME;}
- if(passes)paint(simT,false,passes);
+ const steps=Math.min(ts-last,50)/FRAME,whole=Math.max(1,Math.round(steps)),due=Math.abs(steps-whole)<.05?whole:steps;
+ const passes=Math.min(3,Math.floor(owed+due+1/FRAME));
+ if(!passes)return;
+ last=ts;owed+=due-passes;
+ const unit=slowTog.checked?1/3:1;
+ for(let i=0;i<passes;i++){simT+=unit/60;physics(simT,unit);}
+ paint(simT,false,passes);
}
// ── Physics pass (render to texture) ──
@@ -373,8 +375,8 @@ function paint(t,fresh,fades=1){
}
// Play carries on from the clock and the particles where they stopped; it never stacks a second loop.
-function start(){if(raf!==null||!gl)return;last=null;behind=0;raf=requestAnimationFrame(render);}
-document.addEventListener('visibilitychange',()=>{last=null;behind=0;});
+function start(){if(raf!==null||!gl)return;last=null;owed=0;raf=requestAnimationFrame(render);}
+document.addEventListener('visibilitychange',()=>{last=null;owed=0;});
// While paused a setting or a resize draws the picture again (no physics step), so the change shows without anything moving.
function repaint(){if(paused&&gl)paint(simT,true);}