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: Canvas Particle Effect moves its dots and fades its trails by elapsed time, so it runs at the same speed on every screen
matin673bd5997438
diff --git a/animations/06-3d-advanced/canvas-particle-effect/README.md b/animations/06-3d-advanced/canvas-particle-effect/README.mdindex ad2b5dea..515e3643 100644--- a/animations/06-3d-advanced/canvas-particle-effect/README.md+++ b/animations/06-3d-advanced/canvas-particle-effect/README.md@@ -10,11 +10,11 @@ A canvas particle effect draws hundreds of small dots on one canvas and moves th- Any context where "technology / interconnected systems" is the visual language## How it works-Each particle stores position, velocity, radius, and opacity. Per frame, positions update by velocity; velocity is nudged by mouse force when within the interaction radius:+Each particle stores position, velocity, radius, and opacity. Every frame, positions update by velocity, and velocity is nudged by mouse force when within the interaction radius. Both are multiplied by `k`, the number of 1/60 s steps since the last frame (1 on a 60 Hz screen, 2 on a 30 Hz one, a third of that in slow motion), so the dots drift at the same speed on every screen:```jsclass Particle {- update() {+ update(k) {// Mouse repel/attractconst dx = this.x - mouse.x, dy = this.y - mouse.y;const d2 = dx * dx + dy * dy;@@ -22,18 +22,21 @@ class Particle {const d = Math.sqrt(d2);// (dx, dy) / d points away from the cursor, so subtracting FORCE// makes a negative value repel and a positive value attract.- this.vx -= FORCE * (dx / d) / d * 0.8;- this.vy -= FORCE * (dy / d) / d * 0.8;+ this.vx -= FORCE * (dx / d) / d * 0.8 * k;+ this.vy -= FORCE * (dy / d) / d * 0.8 * k;}// Speed capconst sp = Math.sqrt(this.vx**2 + this.vy**2);if (sp > MAX_SPEED) { this.vx = this.vx/sp * MAX_SPEED; this.vy = this.vy/sp * MAX_SPEED; }// Bounce off walls- this.x += this.vx; this.y += this.vy;+ this.x += this.vx * k; this.y += this.vy * k;if (this.x < 0 || this.x > W) this.vx *= -1;if (this.y < 0 || this.y > H) this.vy *= -1;}}++const k = Math.min(elapsed, 50) / (1000 / 60); // elapsed: ms since the last drawn frame; a long gap counts for at most 50 ms+particles.forEach(p => p.update(k));```**Connections** — O(n²) distance check per frame:@@ -68,7 +71,7 @@ Stroking each link on its own costs one draw call per line, thousands a frame; g| Pointer effect | Push | What the pointer or a finger does to dots within 120px: pushes them away, pulls them in, or nothing || Number of particles | Medium | Few is 250, medium 500 and many 800 dots on computers; tablets draw 60, 90 and 120, and phones 30, 45 and 60, which keeps them smooth || Link distance | Medium | How close two dots must be to join with a line: short is 70px, medium 100px and long 150px; none draws no lines |-| Speed | Normal | How fast the dots drift: slow is 0.5, normal 0.8 and fast 1.3 (the fastest dots' pixels a frame); slow feels calm, fast looks busy |+| Speed | Normal | How fast the dots drift: slow is 0.5, normal 0.8 and fast 1.3 (the fastest dots' pixels for every sixtieth of a second, on any screen); slow feels calm, fast looks busy || Color | Blue | The color of the dots and lines || Trails | off | Each dot leaves a fading streak, because the canvas is only partly cleared between frames |@@ -77,7 +80,8 @@ 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).+- **`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^k, so the streaks last as long on a 30 Hz phone as on a 60 Hz screen.+- **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.htmlindex 8f5b04b4..5fe2b06b 100644--- a/animations/06-3d-advanced/canvas-particle-effect/index.html+++ b/animations/06-3d-advanced/canvas-particle-effect/index.html@@ -187,7 +187,7 @@ class P{this.vx=Math.cos(a)*s;this.vy=Math.sin(a)*s;this.r=Math.random()*1.5+.5;this.op=.3+Math.random()*.7;}- // k is 1, or a third while Slow motion is on+ // k counts steps of 1/60 s: 1 for a frame on a 60 Hz screen, more for a longer frame, and a third of that while Slow motion is onupdate(k){const dx=this.x-mouse.x,dy=this.y-mouse.y;const d2=dx*dx+dy*dy;@@ -210,8 +210,9 @@ function spawn(){for(let i=0;i<COUNT;i++)particles.push(new P());}-function draw(){- if(TRAILS){ctx.fillStyle='rgba(6,10,16,.15)';ctx.fillRect(0,0,W,H);}+// 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);}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);@@ -244,17 +245,23 @@ function draw(){});}-// The loop keeps exactly one pending frame in raf.+// 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.function loop(t){raf=requestAnimationFrame(loop);if(last===null){last=t;return;} // the first frame after a start only takes the time- if(t-last<16)return;+ const dt=t-last;if(dt<16)return;last=t;- const k=slowTog.checked?1/3:1;+ const steps=Math.min(dt,50)/FRAME,k=steps*(slowTog.checked?1/3:1);particles.forEach(p=>p.update(k));- draw();+ draw(1-Math.pow(1-.15,steps));}function start(){if(raf)return;last=null;raf=requestAnimationFrame(loop);}+document.addEventListener('visibilitychange',()=>{last=null;});// 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();}}