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: run Particle Constellation at the same speed on every screen
matinaa69723994e9
diff --git a/animations/07-ambient-background/particle-constellation/README.md b/animations/07-ambient-background/particle-constellation/README.mdindex a367d2df..f2dc3514 100644--- a/animations/07-ambient-background/particle-constellation/README.md+++ b/animations/07-ambient-background/particle-constellation/README.md@@ -30,7 +30,19 @@ ctx.globalAlpha = 1;Every line shares one stroke color and gets its fade from `globalAlpha`; building a new color string for each line costs several times more.-Optional mouse attraction nudges each node's velocity toward the pointer, so the mesh gathers where the cursor rests. Each node remembers the velocity it started with and eases back to it every frame, so a pull from the pointer fades away while the slow drift goes on. On touch screens the pointer's position also comes from `pointerdown`, so a resting finger works too.+Optional mouse attraction nudges each node's velocity toward the pointer, so the mesh gathers where the cursor rests. Each node remembers the velocity it started with and eases back to it by 1% for every 60th of a second, so a pull from the pointer fades away while the slow drift goes on. On touch screens the pointer's position also comes from `pointerdown`, so a resting finger works too.++The drift, the pull and the easing move by the time that has passed, not by a fixed amount per frame, so a 30 Hz phone and a 144 Hz monitor show the same speed. `k` counts how many 60 Hz frames the last frame stands for, and a third of that in slow motion. The first frame after a start, a pause or a return from a hidden tab counts as none, and a long gap between frames counts for at most 50 ms:++```js+const dt = last === null ? 0 : Math.min(now - last, 50); // ms since the last frame+last = now;+const k = dt / (1000 / 60) * (slow ? 1 / 3 : 1); // 1 at 60 Hz, 2 at 30 Hz++node.x += node.vx * SPEED * k; // drift+node.vx += (dx / d) * 0.04 * k; // pull toward the pointer, when it is near+node.vx += (node.cvx - node.vx) * 0.01 * k; // ease back to the cruising velocity+```## Key parameters| Parameter | Default | Effect |@@ -43,7 +55,7 @@ Optional mouse attraction nudges each node's velocity toward the pointer, so the## Production notes- **The n² wall**: connection testing is O(n²). At ~150 nodes you are doing >11,000 distance checks per frame. This demo caps nodes at 60 on phone-sized screens (up to 600px wide, or up to 500px tall for a phone held sideways) to hold 60fps on mid-range phones. For larger fields, bucket nodes into a spatial grid and only test neighboring cells.-- **Velocity easing**: each frame, a node's velocity eases 1% of the way back to the velocity it started with. That keeps pulls from the pointer from building into runaway speeds, and unlike multiplying the velocity by 0.99, which slows every node to a stop within seconds, it never lets the drift die out.+- **Velocity easing**: for every 60th of a second, a node's velocity eases 1% of the way back to the velocity it started with. That keeps pulls from the pointer from building into runaway speeds, and unlike multiplying the velocity by 0.99 every 60th of a second, which slows every node to a stop within seconds, it never lets the drift die out. Scaling the 1% by `k` is a linear shortcut that differs from the exact `1 - 0.99^k` by about 1% at most, on a 50 ms frame.- **Reduced motion**: the demo starts paused, showing one still frame, until the visitor presses Play. In production, show these visitors the still frame.- **Retina**: for crisp lines on high-DPI screens, scale the canvas backing store by `devicePixelRatio` and the context by the same factor. Omitted here to keep fill rate low on mobile.- **Library equivalents**: [tsParticles](https://github.com/matteobruni/tsparticles) ships this exact effect (`links` mode) with presets; [three.js](https://threejs.org) can push the same idea to tens of thousands of GPU points with `LineSegments`.diff --git a/animations/07-ambient-background/particle-constellation/index.html b/animations/07-ambient-background/particle-constellation/index.htmlindex 81b248eb..3fa0c36d 100644--- a/animations/07-ambient-background/particle-constellation/index.html+++ b/animations/07-ambient-background/particle-constellation/index.html@@ -154,7 +154,8 @@const PAL={cyan:[90,209,255],violet:[168,130,255],mint:[122,255,200],mono:[220,220,225]};const PHONE_DOTS=60; // phones and short screens draw at most this many dots: every pair is tested each frameconst phone=matchMedia('(max-width:600px),(max-height:500px)');- let W=0,H=0,raf=null,lastT=0,paused=false;+ const FRAME=1000/60; // one step is what a 60 Hz frame adds, so a faster screen or a slow frame does not change the speed+ let W=0,H=0,raf=null,lastT=null,paused=false;let COUNT=90,LINK=130,SPD=0.6,COL='cyan',MOUSE=true;let nodes=[],mx=-999,my=-999;@@ -167,7 +168,8 @@});}- // k is 1, or a third in slow motion: the drift, the pull toward the pointer and the easing all shrink by it+ // k is how many 60 Hz frames this step stands for (1 at 60 Hz, 2 at 30 Hz), or a third of that in slow motion: the drift,+ // the pull toward the pointer and the easing back to the cruising velocity all grow and shrink with itfunction step(k){for(const p of nodes){p.x+=p.vx*SPD*k;p.y+=p.vy*SPD*k;@@ -204,14 +206,17 @@for(const p of nodes){ctx.beginPath();ctx.arc(p.x,p.y,p.r,0,Math.PI*2);ctx.fill();}}- // loop() keeps exactly one frame requested in raf; Pause cancels it and Play starts one loop again. At most one picture per 16 ms.+ // loop() keeps exactly one frame requested in raf; Pause cancels it and Play starts one loop again. At most one picture per+ // 16 ms, and each moves the dots by the time since the last one (in 60ths of a second, at most 50 ms's worth, so a background+ // tab does not leap). The first frame after a start has nothing to measure from and moves nothing.function loop(ts){raf=requestAnimationFrame(loop);- if(ts-lastT<16)return;+ if(lastT!==null&&ts-lastT<16)return;+ const dt=lastT===null?0:Math.min(ts-lastT,50);lastT=ts;- step(slowTog.checked?1/3:1);draw();+ step(dt/FRAME*(slowTog.checked?1/3:1));draw();}- function start(){if(raf)return;lastT=0;raf=requestAnimationFrame(loop);}+ function start(){if(raf)return;lastT=null;raf=requestAnimationFrame(loop);}// 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. The dots on screen stay where they are while paused.@@ -220,6 +225,8 @@if(paused){cancelAnimationFrame(raf);raf=null;}else start();});+ // A tab that comes back after being hidden has no earlier frame to measure from, so its first frame moves nothing+ document.addEventListener('visibilitychange',()=>{lastT=null;});// Setting the canvas size wipes it, so a real change of the stage's size scatters the dots and draws them at once,// playing or paused. A resize that leaves the stage as it was (a phone's address bar sliding in or out) does nothing.