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 steps in fixed one-step passes from an accumulator, so the Attract look is the same on every screen

matin8eecae450d0c
diff --git a/animations/06-3d-advanced/gpgpu-particle-system/README.md b/animations/06-3d-advanced/gpgpu-particle-system/README.md
index 30db8d96..b8f815dd 100644
--- a/animations/06-3d-advanced/gpgpu-particle-system/README.md
+++ b/animations/06-3d-advanced/gpgpu-particle-system/README.md
@@ -20,7 +20,7 @@ precision highp float;
uniform sampler2D uPos; // read previous frame's positions
uniform float uTime, uSpeed;
uniform int uBehavior;
-uniform float uStep; // steps of 1/60 s since the last frame: 1 at 60 Hz, a third of that in slow motion
+uniform float uStep; // one 60 Hz step per pass: 1, or 1/3 in slow motion
out vec4 outColor; // write new positions
float hash(float n) { return fract(sin(n) * 43758.5); }
@@ -50,7 +50,7 @@ void main() {
}
```
-**Time step** — the page passes `uStep`, the time since the last drawn frame counted in steps of 1/60 s (capped at 50 ms, and a third of it in slow motion), so the particles move at the same speed on a 30, 60 or 144 Hz screen. A long frame is run as several passes of about one step each, two on a 30 Hz screen: the attractor pulls hard near the middle, and one long step there lets particles overshoot and keep circling instead of settling. The damping `pow(0.985, uStep)` is the right amount for a step of any length, and the trail fade is scaled the same way.
+**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 is scaled by the frame's time, `1 - 0.88^(time / (1000 / 60))`, and not by slow motion, so the streaks last as long on every screen and slow motion slows the particles but not the fade.
**Texture ping-pong** — alternate which texture is the read source each pass:
@@ -94,7 +94,7 @@ void main() {
## Production notes
- **WebGL2 required**: `RGBA32F` float framebuffer targets require WebGL2 (or the `WEBGL_color_buffer_float` extension in WebGL1, which is less reliably available). WebGL2 is supported in all modern browsers (Chrome 56+, Firefox 51+, Safari 15+).
-- **Time, not frames**: a fixed step per frame would move the particles twice as fast on a 120 Hz screen and half as fast on a 30 Hz phone, and fade the trails at the same wrong rate. Scale the step and the fade by the time since the last frame, cap that time (this demo uses 50 ms), and split a long frame into passes of about one step so a strong pull near a point stays stable.
+- **Time, not frames**: a fixed step per frame would move the particles twice as fast on a 120 Hz screen and half as fast on a 30 Hz phone, and fade the trails at the same wrong rate. Step by a fixed 1/60 s from an accumulator, as above, and cap the time of one frame (this demo uses 50 ms) and the passes per frame, so a strong pull near a point settles the same on every screen. Scale the trail fade by the time since the last frame.
- **Three.js `GPUComputationRenderer`**: Three.js includes `GPUComputationRenderer` (in the `three/examples/jsm` path) which encapsulates the entire ping-pong texture pattern. It's the idiomatic production approach.
- **WebGPU compute shaders**: WebGPU provides proper `@compute` shader stages for GPGPU, eliminating the "fragment shader as compute" workaround. As of 2024, WebGPU is available in Chrome 113+ but not yet in Firefox stable or Safari.
- **Wrap only survives a divergence-free field**: this demo's flow field pushes particles away from the x-axis, so it has a net outward drift. An earlier version wrapped at ±1.2 while only ±1.0 is visible; particles drifted into that invisible ring faster than they came back and the canvas faded to black after about ten seconds. Tightening the wrap to ±1.0 only moved the symptom — a divergent field teleports the escapee straight back to the edge it just left, so the particles crust along the top and bottom instead. Respawning escapees at a random point near the middle is the fix, and it is what keeps the population steady here. Nothing throws in either failure, and the first second looks correct in all three versions, so this class of bug is only visible if you leave the demo running.
diff --git a/animations/06-3d-advanced/gpgpu-particle-system/index.html b/animations/06-3d-advanced/gpgpu-particle-system/index.html
index b4d0f2fe..8ea7a4c3 100644
--- a/animations/06-3d-advanced/gpgpu-particle-system/index.html
+++ b/animations/06-3d-advanced/gpgpu-particle-system/index.html
@@ -301,22 +301,23 @@ 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;
-// The loop keeps exactly one pending frame in raf. Each drawn frame moves the particles by the time since the last drawn one
-// (a gap counts for at most 50 ms), and by a third of that while Slow motion is on; the clock adds up the same time, so a
-// pause never makes the particles jump. A frame longer than 1.5 steps (a 30 Hz screen) is moved in several passes of about
-// one step each: the Attract rule pulls hard near the middle, and one long step there lets the particles overshoot and keep
-// circling instead of settling. The Trails fade covers the same share of the old picture per second at any refresh rate.
-// At most one picture is drawn per 12 ms. A tab that comes back to the front starts again from its next frame, so the time
-// away adds no step.
+// 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 covers the same
+// share of the old picture per second at any refresh rate, and Slow motion does not slow it. 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;
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),slow=slowTog.checked;
- const steps=dt/FRAME*(slow?1/3:1),passes=Math.max(1,Math.round(steps)),clock=dt/1000/(slow?3:1);
- for(let i=1;i<=passes;i++)physics(simT+clock*i/passes,steps/passes);
- simT+=clock;
+ const dt=Math.min(gap,50),unit=slowTog.checked?1/3:1;
+ behind+=dt;
+ for(let n=0;behind>=FRAME-1&&n<3;n++){simT+=unit/60;physics(simT,unit);behind-=FRAME;}
paint(simT,false,dt/FRAME);
}
@@ -370,8 +371,8 @@ function paint(t,fresh,steps=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;raf=requestAnimationFrame(render);}
-document.addEventListener('visibilitychange',()=>{last=null;});
+function start(){if(raf!==null||!gl)return;last=null;behind=0;raf=requestAnimationFrame(render);}
+document.addEventListener('visibilitychange',()=>{last=null;behind=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);}