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 draws only when a Trails wash is due, snapping near-whole steps and rounding with a small bias, so its drawing stays near 60 pictures a second at any refresh rate

matin7c81f1051898
diff --git a/animations/06-3d-advanced/canvas-particle-effect/README.md b/animations/06-3d-advanced/canvas-particle-effect/README.md
index 9a2253bb..2a1c12f3 100644
--- a/animations/06-3d-advanced/canvas-particle-effect/README.md
+++ b/animations/06-3d-advanced/canvas-particle-effect/README.md
@@ -35,15 +35,25 @@ 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));
-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
+let owed = 0; // steps of Trails wash not laid yet: 0 to start with, and cleared again when the loop restarts (Play, a tab coming back)
+
+function frame(elapsed) { // called for every frame the screen offers; elapsed: ms since the last frame that was drawn
+ const steps = Math.min(elapsed, 50) / (1000 / 60); // a long gap counts for at most 50 ms
+ const whole = Math.max(1, Math.round(steps));
+ const due = Math.abs(steps - whole) < 0.05 ? whole : steps; // within 5% of a whole number counts as that number
+ if (Math.round(owed + due + 0.05) < 1) return; // no wash due: draw nothing and change nothing, the time waits for the next frame
+
+ const k = steps * (slow ? 1 / 3 : 1);
+ particles.forEach(p => p.update(k)); // Slow motion slows the dots ...
+ owed += due; // ... but not the Trails fade: one wash of 15% for every step
+ const washes = Math.round(owed + 0.05); // (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
+}
```
+The wash count is worked out apart from the movement so that a screen a little off 60 Hz (59.94 or 60.06), or one whose frames arrive a little unevenly, still washes exactly once a frame. The 0.05 added before rounding keeps half a step, which is what a frame is at exactly 120 or 240 Hz, from sitting on a tie that timestamp noise could decide either way. A frame that comes before a wash is due draws nothing, which also keeps the drawing near 60 pictures a second on a faster screen.
+
**Connections** — O(n²) distance check per frame:
```js
@@ -85,7 +95,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 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.
+- **`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, once on each drawn frame from 59.94 Hz up), 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 ba11fc1c..0492656f 100644
--- a/animations/06-3d-advanced/canvas-particle-effect/index.html
+++ b/animations/06-3d-advanced/canvas-particle-effect/index.html
@@ -247,21 +247,25 @@ function draw(washes=1){
// 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 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.
+// The loop looks at every frame the screen offers and draws only when a Trails wash is due. "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 washes exactly once a frame. "owed"
+// adds the steps up, and a wash is due once Math.round(owed + .05) reaches 1 (twice on a 30 Hz screen); the .05 keeps half a step,
+// which is what a frame is at exactly 120 or 240 Hz, from sitting on a tie that timestamp noise could decide either way. A frame
+// with no wash due draws nothing and changes nothing, so its time carries to the next frame; that keeps the drawing near 60
+// pictures a second on faster screens. A drawn frame moves the dots by all the time since the last one, and by a third of that while Slow motion is on, and
+// lays the wash over the picture once for every step due, always at the same 15%; 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. 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
- const dt=t-last;if(dt<16)return;
+ const steps=Math.min(t-last,50)/FRAME,whole=Math.max(1,Math.round(steps)),due=Math.abs(steps-whole)<.05?whole:steps;
+ if(Math.round(owed+due+.05)<1)return;
last=t;
- const steps=Math.min(dt,50)/FRAME,k=steps*(slowTog.checked?1/3:1);
- particles.forEach(p=>p.update(k));
- owed+=steps;const washes=Math.round(owed);owed-=washes;
+ particles.forEach(p=>p.update(steps*(slowTog.checked?1/3:1)));
+ owed+=due;const washes=Math.round(owed+.05);owed-=washes;
draw(washes);
}
function start(){if(raf)return;last=null;owed=0;raf=requestAnimationFrame(loop);}