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: Morphing Blob's drop closes on the pointer by elapsed time, so it arrives in the same time on every screen

matin27827c47134d
diff --git a/animations/06-3d-advanced/morphing-blob/README.md b/animations/06-3d-advanced/morphing-blob/README.md
index 74f01a21..9638e063 100644
--- a/animations/06-3d-advanced/morphing-blob/README.md
+++ b/animations/06-3d-advanced/morphing-blob/README.md
@@ -25,10 +25,12 @@ Each blob is a normal SVG `<circle>`. The whole group is passed through a "goo"
The last row of the matrix (`0 0 0 20 -9`) is the trick: it multiplies alpha by 20 and subtracts 9, so mid-range (blurred) alpha collapses to 0 or 1 — a hard silhouette. A requestAnimationFrame loop runs two small functions every frame. `step(dt)` moves time on: it advances the drift phase and eases one "droplet" circle toward the pointer. `place()` then works out where every circle is (the background circles ride sine paths driven by the phase) and writes it to the circle's attributes:
```js
-function step(dt){
- phase += dt * speed; // integrated per frame
- drop.x += (pointer.x - drop.x) * 0.09; // attraction toward the cursor
- drop.y += (pointer.y - drop.y) * 0.09; // (the pointer comes from Pointer Events → mouse, pen, touch)
+const FRAME = 1000 / 60; // one frame on a 60 Hz screen, in milliseconds
+function step(dt){ // dt: milliseconds since the last frame, at most 50
+ phase += dt / 1000 * speed; // integrated: dt is time, not a frame count
+ const pull = 1 - Math.pow(1 - 0.09, dt / FRAME); // 0.09 of the gap per 1/60 s, on any screen
+ drop.x += (pointer.x - drop.x) * pull; // attraction toward the cursor
+ drop.y += (pointer.y - drop.y) * pull; // (the pointer comes from Pointer Events → mouse, pen, touch)
}
function place(){
for (const b of blobs){
@@ -41,18 +43,24 @@ function place(){
}
```
-The drift phase is integrated (`phase += dt * speed`) rather than computed from
+The drift phase is integrated (`phase += dt / 1000 * speed`) rather than computed from
absolute time. Multiplying the elapsed time by the speed makes every blob jump to
a new point on its sine path the moment the speed changes; accumulating the phase
changes the rate and leaves the positions continuous.
+The drop closes 0.09 of its distance to the pointer in every 1/60 s. For a frame of
+another length, `1 - Math.pow(1 - 0.09, dt / FRAME)` is the share to close: 0.09 at
+60 Hz, about 0.17 at 30 Hz and about 0.04 at 144 Hz. The drop therefore arrives in
+the same time on every screen; a fixed 0.09 a frame would take twice as long on a
+30 Hz phone and half as long at 120 Hz.
+
Because a drifting circle's position depends only on the phase, `place()` can also
draw the blob without moving anything. The demo calls it once at load, so the first
picture is already a blob and Play carries on from exactly there, and again when the
number of blobs changes while it is paused. Pausing cancels the pending frame; Play
-forgets the time of the last frame, so the first frame after it adds nothing.
-Slow motion adds a third of the phase and lets the droplet close 0.03 of the gap
-instead of 0.09.
+forgets the time of the last frame, so the first frame after it adds nothing, and so
+does a tab that comes back to the front. Slow motion adds a third of the phase and
+lets the droplet close 0.03 of the gap in every 1/60 s instead of 0.09.
The pointer's position is turned into the drawing's own units with the SVG's screen
matrix (`svg.getScreenCTM().inverse()`), so the droplet stays under the pointer
@@ -72,6 +80,7 @@ shape of the stage.
## Production notes
- **Filter performance is the gotcha.** SVG filters rasterise the filter region every frame on the CPU/GPU compositor; a large blurred area over a big viewport can drop frames on mobile. Keep the filtered `<g>` region as small as the design allows, cap `stdDeviation`, and reduce blob count on small screens. Test on a mid-range phone, not just desktop.
- **Safari and blur units.** `stdDeviation` is in user (viewBox) units, not pixels — the visual blur scales with your `viewBox`, so pin the viewBox size and let CSS scale the SVG rather than resizing the viewBox.
+- **Time, not frames**: easing that closes a fixed share of the gap on every frame follows the screen's refresh rate. Scale the share by the time since the last frame, as above, and cap that time (this demo uses 50 ms) so a tab that comes back does not lurch.
- **`will-change: transform`** on the SVG element can promote it to its own layer and smooth things out, but watch memory on low-end devices.
- **Reduced motion.** Under `prefers-reduced-motion: reduce` the demo starts paused, showing the blob still, until the visitor presses Play. In production, never leave an autonomous churning shape running for users who opted out: show one settled arrangement.
- **Alternatives / library equivalents:** for a single animated outline instead of metaballs, interpolate an SVG `<path>`'s control points (the approach tools like **blobmaker** / **Haikei** export). For heavy, truly 3D goo, **three.js** MarchingCubes (metaballs) or a ray-marched SDF in a fragment shader gives real volume at GPU cost. **Framer Motion** can tween a `path`'s `d` attribute for path-based blobs.
diff --git a/animations/06-3d-advanced/morphing-blob/index.html b/animations/06-3d-advanced/morphing-blob/index.html
index da85c278..900c6b1b 100644
--- a/animations/06-3d-advanced/morphing-blob/index.html
+++ b/animations/06-3d-advanced/morphing-blob/index.html
@@ -182,13 +182,15 @@ function build(n){
}
}
-// Time moves on here and nowhere else: the drift phase, and the drop closing in on the pointer.
-// Integrating the phase instead of multiplying absolute time means a new speed changes the rate
-// without teleporting the circles. Slow motion adds a third of the phase and a third of the pull.
+// Time moves on here and nowhere else: the drift phase, and the drop closing in on the pointer. dt is the time in
+// milliseconds since the last frame. Integrating the phase instead of multiplying absolute time means a new speed
+// changes the rate without teleporting the circles. The drop closes 0.09 of its gap to the pointer in every 1/60 s
+// (FRAME), so it takes the same time to arrive on any screen. Slow motion adds a third of the phase and closes 0.03.
+const FRAME=1000/60;
function step(dt){
const slow=slowTog.checked;
- phase+=dt*speed/(slow?3:1);
- const pull=slow?0.03:0.09;
+ phase+=dt/1000*speed/(slow?3:1);
+ const pull=1-Math.pow(1-(slow?0.03:0.09),dt/FRAME);
drop.x+=(pointer.x-drop.x)*pull;
drop.y+=(pointer.y-drop.y)*pull;
}
@@ -208,14 +210,16 @@ function place(){
}
// The loop keeps exactly one pending frame in raf. The first frame after a start adds nothing, and a long
-// gap between frames (a background tab) counts for at most a tenth of a second.
+// gap between frames counts for at most 50 ms. A tab that comes back to the front starts again from its next
+// frame, so the time away adds nothing either.
function frame(ts){
raf=requestAnimationFrame(frame);
- const dt=last===null?0:Math.min(ts-last,100)/1000;
+ const dt=last===null?0:Math.min(ts-last,50);
last=ts;
step(dt);place();
}
function start(){if(raf)return;last=null;raf=requestAnimationFrame(frame);}
+document.addEventListener('visibilitychange',()=>{last=null;});
// 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;