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: Scroll-Driven 3D Rotation glides to its pose by elapsed time, so the cube catches up in the same time on every screen
matinff431d64212a
diff --git a/animations/06-3d-advanced/scroll-driven-3d-rotation/README.md b/animations/06-3d-advanced/scroll-driven-3d-rotation/README.mdindex 9def420f..31059de6 100644--- a/animations/06-3d-advanced/scroll-driven-3d-rotation/README.md+++ b/animations/06-3d-advanced/scroll-driven-3d-rotation/README.md@@ -45,25 +45,31 @@ The demo's keyframes are four poses, one for each quarter of the scroll: a quart**Smooth follow** adds a lerp each frame to ease between the raw scroll value and the applied rotation — this gives a cinematic feel:```js-function animate() {- currentRx += (targetRx - currentRx) * 0.1;- currentRy += (targetRy - currentRy) * 0.1;+const FRAME = 1000 / 60; // one frame on a 60 Hz screen, in milliseconds+function animate(now) {+ const ease = 1 - Math.pow(0.9, Math.min(now - last, 50) / FRAME); // 0.1 at 60 Hz, 0.19 at 30 Hz+ last = now;+ currentRx += (targetRx - currentRx) * ease;+ currentRy += (targetRy - currentRy) * ease;obj.style.transform = `rotateX(${currentRx}deg) rotateY(${currentRy}deg)`;requestAnimationFrame(animate);}```-The demo asks for another frame only while the cube is still catching up, and stops once it has settled. The Turn amount setting multiplies the angles of every pose by 0.6, 1 or 1.6, and with reduced motion turned on the demo skips the gliding and sets each pose at once.+The cube closes a tenth of the gap in every 1/60 s, so the share to close in one frame depends on how long the frame took. The cube then catches up in the same time on a 30, 60 or 144 Hz screen; a fixed 0.1 a frame would take twice as long on a 30 Hz phone and half as long at 120 Hz.++The demo asks for another frame only while the cube is still catching up, and stops once it has settled. The first frame after it starts again has no earlier frame to measure from, so it counts as one 1/60 s. The Turn amount setting multiplies the angles of every pose by 0.6, 1 or 1.6, and with reduced motion turned on the demo skips the gliding and sets each pose at once.## Key parameters| Parameter | Default | Effect ||-----------|---------|--------|| Turn amount | Normal | How far every pose turns the cube: less is 0.6, normal 1 and more 1.6 times the planned angles; at normal the last pose shows the cube's back |-| Smooth follow | on | The cube closes a tenth of the gap to its pose every frame, so it glides after the scroll; off, it follows the scroll exactly |+| Smooth follow | on | The cube closes a tenth of the gap to its pose for every sixtieth of a second, so it glides after the scroll and catches up in the same time on every screen; off, it follows the scroll exactly |## Production notes- **CSS Scroll-Driven Animations API** (Chrome 115+): `animation-timeline: scroll()` and `animation-range` can drive CSS transforms directly without JavaScript. The spec covers this natively, but browser support is still catching up for complex choreography.- **GSAP ScrollTrigger**: in production, `gsap.to(obj, { rotateY: 360, scrollTrigger: { trigger, scrub: 1 } })` is the idiomatic implementation. `scrub: 1` adds a 1-second smoothing lag.+- **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).- **Performance**: CSS `transform` on a 3D element with `will-change: transform` runs on the compositor thread — scroll-driven rotation does not trigger layout or paint.- **Mobile scroll budget**: 400vh of scroll on mobile means the user must scroll a lot. Consider reducing section height for mobile, or switching to a swipe-driven (touch-drag) interaction.diff --git a/animations/06-3d-advanced/scroll-driven-3d-rotation/index.html b/animations/06-3d-advanced/scroll-driven-3d-rotation/index.htmlindex 0f6b70c5..b45e9a13 100644--- a/animations/06-3d-advanced/scroll-driven-3d-rotation/index.html+++ b/animations/06-3d-advanced/scroll-driven-3d-rotation/index.html@@ -173,10 +173,17 @@ function render(){objWrap.style.transform=`rotateX(${currRx.toFixed(1)}deg) rotateY(${currRy.toFixed(1)}deg) scale(${currSc.toFixed(3)})`;}-// Smooth follow: keep easing toward the target each frame until it settles, then stop the loop-function tick(){+// Smooth follow: keep easing toward the target until it settles, then stop the loop. The cube closes a tenth of the gap+// in every 1/60 s (FRAME), so it catches up in the same time on every screen. A long gap between frames counts for at most+// 50 ms, and the first frame after the loop starts again has no earlier frame to measure from, so it counts as one 1/60 s.+const FRAME=1000/60;+let lastT=null;+function tick(now){raf=null;- currRx=lerp(currRx,tgtRx,.1);currRy=lerp(currRy,tgtRy,.1);currSc=lerp(currSc,tgtSc,.1);+ const dt=lastT===null?FRAME:Math.min(now-lastT,50);+ lastT=now;+ const ease=1-Math.pow(.9,dt/FRAME);+ currRx=lerp(currRx,tgtRx,ease);currRy=lerp(currRy,tgtRy,ease);currSc=lerp(currSc,tgtSc,ease);if(Math.abs(tgtRx-currRx)>.05||Math.abs(tgtRy-currRy)>.05||Math.abs(tgtSc-currSc)>.001){render();raf=requestAnimationFrame(tick);}else{currRx=tgtRx;currRy=tgtRy;currSc=tgtSc;render();}}@@ -187,7 +194,7 @@ function applyRotation(p){const k=mult/10;tgtRx=lerp(c0.rx,c1.rx,t)*k;tgtRy=lerp(c0.ry,c1.ry,t)*k;tgtSc=lerp(c0.sc,c1.sc,t);const smooth=smoothTog.checked&&!reduceMotion.matches;- if(smooth){if(!raf)raf=requestAnimationFrame(tick);}+ if(smooth){if(!raf){lastT=null;raf=requestAnimationFrame(tick);}}else{if(raf){cancelAnimationFrame(raf);raf=null;}currRx=tgtRx;currRy=tgtRy;currSc=tgtSc;render();}}