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: GPGPU Particle System fades its Trails once per physics pass at the fixed 12% and draws nothing on a frame with no pass
matin29eb77494744
diff --git a/animations/06-3d-advanced/gpgpu-particle-system/README.md b/animations/06-3d-advanced/gpgpu-particle-system/README.mdindex b8f815dd..805b1261 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 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.+**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.**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. 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.+- **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. Fade once per physics pass, always by the same amount.- **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.htmlindex 8ea7a4c3..f07d01d0 100644--- a/animations/06-3d-advanced/gpgpu-particle-system/index.html+++ b/animations/06-3d-advanced/gpgpu-particle-system/index.html@@ -165,7 +165,7 @@ void main(){float x=p.x,y=p.y,vx=p.z,vy=p.w;float age=fract(p.w*0.01+uT*0.001); // reuse w channel for agefloat spd=uSpd*.0002;- // uStep counts steps of 1/60 s (1 at 60 Hz, 2 at 30 Hz, a third of that in slow motion): every change of velocity and every move is scaled by it+ // uStep is 1, or 1/3 in slow motion: every change of velocity and every move is scaled by itif(uBehavior==0){// flow fieldfloat angle=sin(x*2.+uT*.3)*cos(y*2.+uT*.2)+atan(y,x)*.5;vx+=cos(angle)*spd*uStep;vy+=sin(angle)*spd*uStep;@@ -215,10 +215,10 @@ void main(){const RENDER_FS=`#version 300 esprecision mediump float;in vec4 vCol;out vec4 fragColor;void main(){fragColor=vCol;}`;-// Trails: a translucent full-screen quad fades the previous frame instead of clearing it; uA is how much of it the quad covers+// Trails: a translucent full-screen quad fades the previous frame instead of clearing itconst FADE_FS=`#version 300 es-precision mediump float;uniform float uA;out vec4 fragColor;-void main(){fragColor=vec4(.024,.039,.063,uA);}`;+precision mediump float;out vec4 fragColor;+void main(){fragColor=vec4(.024,.039,.063,.12);}`;const stage=document.getElementById('stage'),cvs=document.getElementById('cvs'),noWgl2=document.getElementById('no-wgl2');const slowTog=document.getElementById('slow-tog');@@ -305,8 +305,10 @@ const FRAME=1000/60;// 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+// 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;@@ -317,8 +319,9 @@ function render(ts){last=ts;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);+ let passes=0;+ for(;behind>=FRAME-1&&passes<3;passes++){simT+=unit/60;physics(simT,unit);behind-=FRAME;}+ if(passes)paint(simT,false,passes);}// ── Physics pass (render to texture) ──@@ -345,16 +348,15 @@ function settle(){for(let i=0;i<120;i++){simT+=1/60;physics(simT,1);}}// ── Render pass (draw points). A picture drawn on its own starts from a cleared canvas ("fresh"): with Trails on, a// normal frame only lays a thin fade over the last one, which would leave the old picture showing under a new setting.-// steps is the length of the frame in steps of 1/60 s; the fade covers .12 of the old picture per step. ──-function paint(t,fresh,steps=1){+// fades is how many times that fade is laid over it: once for every physics pass of the frame. ──+function paint(t,fresh,fades=1){gl.bindFramebuffer(gl.FRAMEBUFFER,null);gl.viewport(0,0,W,H);if(TRAILS&&!fresh){// fade the previous frame instead of clearing itgl.enable(gl.BLEND);gl.blendFunc(gl.SRC_ALPHA,gl.ONE_MINUS_SRC_ALPHA);gl.useProgram(fadeProg);gl.bindBuffer(gl.ARRAY_BUFFER,qBuf);- gl.uniform1f(gl.getUniformLocation(fadeProg,'uA'),1-Math.pow(1-.12,steps));const fa=gl.getAttribLocation(fadeProg,'a');gl.enableVertexAttribArray(fa);gl.vertexAttribPointer(fa,2,gl.FLOAT,false,0,0);- gl.drawArrays(gl.TRIANGLE_STRIP,0,4);+ for(let i=0;i<fades;i++)gl.drawArrays(gl.TRIANGLE_STRIP,0,4);}else{gl.clearColor(0.024,0.039,0.063,1);gl.clear(gl.COLOR_BUFFER_BIT);}gl.enable(gl.BLEND);gl.blendFunc(gl.SRC_ALPHA,gl.ONE);gl.useProgram(renderProg);