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: Scroll Image Sequence catches up with the scroll at the same pace on every screen

matin01b2a7260a01
diff --git a/animations/01-scroll-based/scroll-image-sequence/README.md b/animations/01-scroll-based/scroll-image-sequence/README.md
index c22b7fa1..47283e89 100644
--- a/animations/01-scroll-based/scroll-image-sequence/README.md
+++ b/animations/01-scroll-based/scroll-image-sequence/README.md
@@ -24,12 +24,16 @@ stage.addEventListener('scroll', updateTarget, {passive:true}); // fires for whe
The render loop either snaps to the target frame or eases toward it (the Glides between frames setting):
```js
-if (smoothing) shownFrame += (targetFrame - shownFrame) * 0.18; // lerp
+const dt = last ? Math.min(now - last, 50) : FRAME; // FRAME = 1000 / 60; time since the last frame
+last = now;
+if (smoothing) shownFrame += (targetFrame - shownFrame) * (1 - Math.pow(1 - EASE, dt / FRAME)); // lerp; EASE 0.18
else shownFrame = targetFrame; // snap
const index = Math.round(shownFrame);
if (index !== drawn) { drawFrame(index); drawn = index; } // skip a picture that is already on the canvas
```
+`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, so the picture catches up with the scroll in the same time on a 30, 60 or 120Hz screen (a fixed share per frame would take half as long at 120Hz and twice as long at 30Hz). The loop never stops, so `last` is kept from frame to frame; `dt` is capped at 50ms, so a hidden tab does not make the picture jump.
+
`drawFrame(index)` is a pure function of the index — same index, same picture — which is exactly why reverse scrubbing rewinds cleanly. It is also why the loop can skip a picture that is already on the canvas, which keeps the page cheap on phones. Back to top, and Play starting again from the top, move the box in one jump; a click on either button sets the shown frame to the target at once, so the picture does not play backwards.
## Key parameters
diff --git a/animations/01-scroll-based/scroll-image-sequence/index.html b/animations/01-scroll-based/scroll-image-sequence/index.html
index 6bc23643..a6439922 100644
--- a/animations/01-scroll-based/scroll-image-sequence/index.html
+++ b/animations/01-scroll-based/scroll-image-sequence/index.html
@@ -189,10 +189,12 @@ function updateTarget(){
targetFrame=p*(TOTAL-1);
}
-let raf=null;
-function loop(){
+const EASE=0.18,FRAME=1000/60; // EASE: the share of the distance covered in 1/60 s, so the glide lasts as long on any screen
+let raf=null,last=0; // last: time of the previous frame; the loop never stops, so it is kept from frame to frame
+function loop(now=0){
+ const dt=last?Math.min(now-last,50):FRAME;last=now; // a long gap, such as a hidden tab, counts as 50 ms
const smoothing=smoothEl.checked&&!reduce;
- if(smoothing) shownFrame+=(targetFrame-shownFrame)*0.18;
+ if(smoothing) shownFrame+=(targetFrame-shownFrame)*(1-Math.pow(1-EASE,dt/FRAME));
else shownFrame=targetFrame;
const idx=Math.round(shownFrame);
if(idx!==drawn){drawFrame(idx);drawn=idx} // a picture already on the canvas is not drawn again