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: Canvas Particle Effect washes its Trails a whole number of times at the fixed 15%, so faint leftovers keep their color at every refresh rate

matin75f91fe2a8d5
diff --git a/animations/06-3d-advanced/canvas-particle-effect/README.md b/animations/06-3d-advanced/canvas-particle-effect/README.md
index c4b5f742..9a2253bb 100644
--- a/animations/06-3d-advanced/canvas-particle-effect/README.md
+++ b/animations/06-3d-advanced/canvas-particle-effect/README.md
@@ -38,7 +38,10 @@ class Particle {
const steps = Math.min(elapsed, 50) / (1000 / 60); // elapsed: ms since the last drawn frame; a long gap counts for at most 50 ms
const k = steps * (slow ? 1 / 3 : 1); // Slow motion slows the dots ...
particles.forEach(p => p.update(k));
-draw(1 - Math.pow(1 - 0.15, steps)); // ... but not the Trails fade, which follows the frame's time alone
+owed += steps; // ... but not the Trails fade: one wash of 15% for every 1/60 s
+const washes = Math.round(owed); // (Math.round, so a frame a little short of 1/60 s still washes once)
+owed -= washes;
+draw(washes); // lays the wash over the picture that many times, then draws the dots
```
**Connections** — O(n²) distance check per frame:
@@ -82,7 +85,7 @@ Stroking each link on its own costs one draw call per line, thousands a frame; g
- **Phones and tablets**: the demo draws 30 to 60 dots on phones (screens 600px wide or less, or 500px tall or less when held sideways) and 60 to 120 on other screens up to 1024px wide, and strokes the links in six batches instead of one call per line.
- **Canvas vs DOM**: `<canvas>` is mandatory for 50+ particles. DOM elements at that density create thousands of layout calculations per frame — the browser cannot keep up.
- **Particles.js / tsParticles**: the dominant production library. Handles everything in this demo plus themes, shape variety, responsive density, and performance at high counts.
-- **`ctx.clearRect` vs `fillRect`**: using `fillRect` with a semi-transparent background instead of `clearRect` creates a motion-trail effect where older frames linger (turn on Trails in the demo). The wash's opacity is 1 - 0.85^steps, from the frame's time alone (Slow motion slows the dots, not the fade), so the streaks last as long on a 30 Hz phone as on a 60 Hz screen.
+- **`ctx.clearRect` vs `fillRect`**: using `fillRect` with a semi-transparent background instead of `clearRect` creates a motion-trail effect where older frames linger (turn on Trails in the demo). The demo washes once for every 1/60 s of frame time (twice on a 30 Hz screen), always at 15% and whatever Slow motion does, so the streaks last as long on a 30 Hz phone as on a 60 Hz screen. One smaller wash 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, not frames**: a fixed step per frame follows the screen's refresh rate, so the dots would drift twice as fast at 120 Hz and half as fast at 30 Hz. Cap the time of one frame (this demo uses 50 ms) so a tab that comes back does not lurch, and let a returning tab start from its next frame.
- **Device pixel ratio**: size the backing store to `clientWidth * devicePixelRatio` (capped at 2) and scale the context, or sub-pixel dots and 0.5px connection lines blur on retina screens.
- **`prefers-reduced-motion`**: stop all particle movement. Consider keeping the static dot layout visible as a texture.
diff --git a/animations/06-3d-advanced/canvas-particle-effect/index.html b/animations/06-3d-advanced/canvas-particle-effect/index.html
index 5fe2b06b..ba11fc1c 100644
--- a/animations/06-3d-advanced/canvas-particle-effect/index.html
+++ b/animations/06-3d-advanced/canvas-particle-effect/index.html
@@ -210,9 +210,9 @@ function spawn(){
for(let i=0;i<COUNT;i++)particles.push(new P());
}
-// fade is how much of the old picture the Trails wash covers: .15 for a frame at 60 Hz
-function draw(fade=.15){
- if(TRAILS){ctx.fillStyle=`rgba(6,10,16,${fade})`;ctx.fillRect(0,0,W,H);}
+// washes is how many times the Trails wash (15% of the old picture) is laid over it: once for a frame at 60 Hz
+function draw(washes=1){
+ if(TRAILS){ctx.fillStyle='rgba(6,10,16,.15)';for(let i=0;i<washes;i++)ctx.fillRect(0,0,W,H);}
else{ctx.clearRect(0,0,W,H);}
const hex=COLOR,r=parseInt(hex.slice(1,3),16),g=parseInt(hex.slice(3,5),16),b=parseInt(hex.slice(5,7),16);
@@ -248,9 +248,12 @@ function draw(fade=.15){
// One step is 1/60 s, so the dots drift at the same speed whatever the screen's refresh rate.
const FRAME=1000/60;
// The loop keeps exactly one pending frame in raf. Each drawn frame moves the dots 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 Trails wash covers the same share of the old
-// picture per second at any refresh rate. At most one picture is drawn per 16 ms. A tab that comes back to the front starts
-// again from its next frame, so the time away adds no step.
+// counts for at most 50 ms), and by a third of that while Slow motion is on. The Trails wash is laid over the picture once for
+// every 1/60 s of frame time (twice on a 30 Hz screen), always at the same 15%, and Slow motion does not slow it: a smaller
+// wash is rounded differently by the 8-bit canvas, and the faint leftovers of a trail would end up another color. "owed" carries
+// the part of a wash a frame did not use (Math.round, so a frame a little short of 1/60 s still washes once). At most one
+// picture is drawn per 16 ms. 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 loop(t){
raf=requestAnimationFrame(loop);
if(last===null){last=t;return;} // the first frame after a start only takes the time
@@ -258,10 +261,11 @@ function loop(t){
last=t;
const steps=Math.min(dt,50)/FRAME,k=steps*(slowTog.checked?1/3:1);
particles.forEach(p=>p.update(k));
- draw(1-Math.pow(1-.15,steps));
+ owed+=steps;const washes=Math.round(owed);owed-=washes;
+ draw(washes);
}
-function start(){if(raf)return;last=null;raf=requestAnimationFrame(loop);}
-document.addEventListener('visibilitychange',()=>{last=null;});
+function start(){if(raf)return;last=null;owed=0;raf=requestAnimationFrame(loop);}
+document.addEventListener('visibilitychange',()=>{last=null;owed=0;});
// While paused a setting or a resize draws the picture again, so the change shows without anything moving. The canvas is
// cleared first: with Trails on, draw() only lays a thin wash over the old picture, which would stay showing under the new one.
function redraw(){if(paused){ctx.clearRect(0,0,W,H);draw();}}