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: Parallax Depth-of-Field layers settle in the same time on every screen

matin94297717660d
diff --git a/animations/01-scroll-based/parallax-depth-of-field/README.md b/animations/01-scroll-based/parallax-depth-of-field/README.md
index 4cb93e79..c4e65900 100644
--- a/animations/01-scroll-based/parallax-depth-of-field/README.md
+++ b/animations/01-scroll-based/parallax-depth-of-field/README.md
@@ -42,9 +42,17 @@ the scroll position is stored as a *target* and the rendered value eases toward
it a fraction at a time, which turns those discrete steps into continuous motion:
```js
-current += (target - current) * EASE; // inside a requestAnimationFrame loop
+const dt = last ? Math.min(now - last, 50) : FRAME; // FRAME = 1000 / 60; time since the last frame
+current += (target - current) * (1 - Math.pow(1 - EASE, dt / FRAME)); // inside a requestAnimationFrame loop
```
+`EASE` is the share of the distance covered in 1/60 s, and a frame that lasted
+`dt` covers `1 - Math.pow(1 - EASE, dt / FRAME)` of what is left. The layers
+therefore settle in the same time on a 30, 60 or 120Hz screen; a fixed share per
+frame would settle twice as fast at 120Hz and half as fast at 30Hz. The first
+frame after the loop restarts counts as 1/60 s, and `dt` is capped at 50ms so a
+hidden tab does not make the layers jump.
+
The loop only runs while the two values differ, so an idle page costs nothing.
Back to top, and Play starting again from the top, move the box in one jump; a
@@ -58,7 +66,7 @@ layers do not rewind through the whole focus pull.
| Focus | Follows the scroll | Sharp on the sky at the top of the scroll and on the front ridge at the end; each layer blurs by its distance from it |
| Strongest blur | 14px (8px on phones) | The blur of a layer as far from the focus as a layer can be |
| Layer travel | 19% of the box's height (12.5% on phones) | What the layers' sinking is measured against: over the whole scroll the front ridge sinks 70% of it, the hills 45%, the mountains 25%, the far peaks 10% and the sky not at all |
-| Easing | 14% a frame | How much of the remaining distance the layers cover each frame; lower is smoother but lags more |
+| Easing | 14% per sixtieth of a second | How much of the remaining distance the layers cover in a sixtieth of a second; a longer frame covers more, so the layers settle in the same time on every screen; lower is smoother but lags more |
| Pinned scene | Three box heights of scrolling | How much scrolling the whole focus pull takes |
## Production notes
diff --git a/animations/01-scroll-based/parallax-depth-of-field/index.html b/animations/01-scroll-based/parallax-depth-of-field/index.html
index de5aea5d..b30230b8 100644
--- a/animations/01-scroll-based/parallax-depth-of-field/index.html
+++ b/animations/01-scroll-based/parallax-depth-of-field/index.html
@@ -127,7 +127,7 @@
<div><h2 id="hb-prompt-title">Copy the prompt</h2><p>Paste it into an AI assistant such as ChatGPT, Claude or Cursor to build this in your own project.</p></div>
</div>
<div class="hb-card">
- <p class="hb-prompt">Add a parallax depth-of-field effect to [your layered scene or illustration]. Split the scene into layers from far to near and hold it in place while the visitor scrolls through a taller section. As they scroll, move each layer by an amount that grows with its nearness, and move a point of sharp focus from the farthest layer to the nearest: blur each layer by how far it sits from that focus. Ease the layers toward the scroll position on every frame, so a mouse wheel's jumps turn into smooth movement. If the visitor has reduced motion turned on, keep the layers still and only shift the focus.</p>
+ <p class="hb-prompt">Add a parallax depth-of-field effect to [your layered scene or illustration]. Split the scene into layers from far to near and hold it in place while the visitor scrolls through a taller section. As they scroll, move each layer by an amount that grows with its nearness, and move a point of sharp focus from the farthest layer to the nearest: blur each layer by how far it sits from that focus. Ease the layers toward the scroll position on every frame by a share of the distance left, exponential in the time since the last frame, so a wheel's jumps turn into smooth movement at the same pace on every screen. If the visitor has reduced motion turned on, keep the layers still and only shift the focus.</p>
<div class="hb-chips-box" hidden>
<p class="hb-chips-title">Your settings</p>
<ul class="hb-chips"></ul>
@@ -172,7 +172,8 @@
// A wheel notch arrives as one ~100px jump, so mapping scrollY straight onto the
// layers makes them step. Easing toward the target each frame turns steps into glide.
- const EASE = 0.14;
+ // EASE is the share of the distance covered in 1/60 s, so the glide lasts as long on a 30, 60 or 120Hz screen.
+ const EASE = 0.14, FRAME = 1000 / 60;
const BLUR_STEP = 0.5;
let MAX_BLUR, MAX_OFFSET, maxScroll;
@@ -186,7 +187,7 @@
maxScroll = Math.max(1, stage.scrollHeight - stage.clientHeight);
}
- let target = 0, current = 0, raf = null;
+ let target = 0, current = 0, raf = null, last = 0; // last: time of the previous frame, 0 after a restart
const lastBlur = LAYERS.map(() => -1), lastOffset = LAYERS.map(() => NaN);
function render(f) {
@@ -207,17 +208,18 @@
}
}
- function tick() {
+ function tick(now) {
const diff = target - current;
if (Math.abs(diff) < 0.0002) { current = target; render(current); raf = null; return; }
- current += diff * EASE;
+ const dt = last ? Math.min(now - last, 50) : FRAME; last = now; // the first frame after a restart counts as 1/60 s
+ current += diff * (1 - Math.pow(1 - EASE, dt / FRAME));
render(current);
raf = requestAnimationFrame(tick);
}
// Every input collapses into one frame, and the loop idles once nothing moves.
function schedule() {
if (reduced) { current = target; render(current); return; }
- if (raf === null) raf = requestAnimationFrame(tick);
+ if (raf === null) { last = 0; raf = requestAnimationFrame(tick); }
}
function onScroll() {