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: clear before a paused redraw and use phone dot counts on short screens in Canvas Particle Effect

matine5d436d6a1d0
diff --git a/animations/06-3d-advanced/canvas-particle-effect/README.md b/animations/06-3d-advanced/canvas-particle-effect/README.md
index 1b62d372..ad2b5dea 100644
--- a/animations/06-3d-advanced/canvas-particle-effect/README.md
+++ b/animations/06-3d-advanced/canvas-particle-effect/README.md
@@ -74,7 +74,7 @@ Stroking each link on its own costs one draw call per line, thousands a frame; g
## Production notes
- **O(n²) limit**: distance checks between all pairs scale quadratically. Above ~500 particles the loop drops frames. Fix: spatial partitioning (quadtree, uniform grid) reduces checks to O(n log n). For 1000+ particles, switch to WebGL.
-- **Phones and tablets**: the demo draws 30 to 60 dots on screens 600px wide or less and 60 to 120 up to 1024px, and strokes the links in six batches instead of one call per line.
+- **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).
diff --git a/animations/06-3d-advanced/canvas-particle-effect/index.html b/animations/06-3d-advanced/canvas-particle-effect/index.html
index 1d4b4b23..e1b51c92 100644
--- a/animations/06-3d-advanced/canvas-particle-effect/index.html
+++ b/animations/06-3d-advanced/canvas-particle-effect/index.html
@@ -202,9 +202,10 @@ class P{
}
}
-// The number of dots depends on the width of the screen, checked at each spawn(): far fewer on phones and tablets.
+// The number of dots depends on the size of the screen, checked at each spawn(): far fewer on tablets and on phones,
+// which are up to 600px wide, or up to 500px tall when held sideways.
function spawn(){
- COUNT=COUNTS[innerWidth<=600?2:innerWidth<=1024?1:0][level];
+ COUNT=COUNTS[innerWidth<=600||innerHeight<=500?2:innerWidth<=1024?1:0][level];
particles=[];
for(let i=0;i<COUNT;i++)particles.push(new P());
}
@@ -254,8 +255,9 @@ function loop(t){
draw();
}
function start(){if(raf)return;last=null;raf=requestAnimationFrame(loop);}
-// While paused a setting or a resize draws the picture again, so the change shows without anything moving.
-function redraw(){if(paused)draw();}
+// 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();}}
// Registered at the top level, before the shared script (deferred) can run: under reduced motion it pauses the
// loop on arrival, which this listener must catch. Pause cancels the pending frame and the picture stays;