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
perf(parallax-depth-of-field): smooth the scroll-driven motion
matincc339d3359fe
diff --git a/animations/01-scroll-based/parallax-depth-of-field/README.md b/animations/01-scroll-based/parallax-depth-of-field/README.mdindex c87300b0..d0b02926 100644--- a/animations/01-scroll-based/parallax-depth-of-field/README.md+++ b/animations/01-scroll-based/parallax-depth-of-field/README.md@@ -36,6 +36,17 @@ layer.el.style.transform = `translateY(${layer.speed * f * maxOffset}px)`;Layers closer to the viewer travel farther, reinforcing the sense of depth.+Driving `f` straight from `scrollY` is what makes most parallax feel rough: a+mouse wheel arrives as a single ~100px jump, so the layers jump with it. Instead+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+```++The loop only runs while the two values differ, so an idle page costs nothing.+## Key parameters| Parameter | Default | Effect |@@ -44,14 +55,28 @@ Layers closer to the viewer travel farther, reinforcing the sense of depth.| `depth` | 0.0–1.0 | Fixed depth per layer; 0 = far, 1 = near || `speed` | 0.0–0.70 | Fraction of `maxOffset` the layer travels over full scroll || `maxOffset`| 120px | Maximum translateY distance (applied at nearest layer) |+| `EASE` | 0.14 | Share of the remaining distance covered per frame. Lower = smoother but laggier; above ~0.3 the stepping returns |+| `BLUR_STEP`| 0.5px | Granularity the blur radius snaps to, so the compositor can reuse a cached texture |## Production notes-- `filter: blur()` triggers compositing. Add `will-change: filter` to each- layer element to promote it to its own compositor layer and avoid per-frame- paints.+- Never read layout inside a scroll handler. `stage.offsetHeight` or+ `window.innerHeight` there forces a synchronous reflow on every event, and+ writing styles immediately after produces layout thrashing. Measure once, cache+ the result, and refresh it on `resize`.+- Scroll events fire more often than frames. Store the position in the handler+ and do all DOM writing inside one `requestAnimationFrame` callback, otherwise+ the same frame is styled several times over.+- `will-change: filter` is a trap here. It promotes the layer but does nothing+ about the real cost: a changing blur radius forces the GPU to re-rasterize the+ whole surface. Hint `will-change: transform` only, and quantize the radius so+ it holds still across most frames. Skipping writes when the value is unchanged+ matters more than any hint.- Blurring large SVGs is GPU-friendly; blurring large raster images is not —test on mobile before shipping.+- Reduce motion by zeroing the parallax offset rather than disabling the whole+ effect. The layer translation is the vestibular trigger; the focal-plane blur+ is not, and keeping it leaves the interface intact instead of inert.- For production scroll choreography, GSAP ScrollTrigger with `scrub: true`gives smoother performance and easing control. Framer Motion's `useScroll` +`useTransform` is the React equivalent.diff --git a/animations/01-scroll-based/parallax-depth-of-field/index.html b/animations/01-scroll-based/parallax-depth-of-field/index.htmlindex 2b1b1853..b34c73d4 100644--- a/animations/01-scroll-based/parallax-depth-of-field/index.html+++ b/animations/01-scroll-based/parallax-depth-of-field/index.html@@ -50,7 +50,10 @@.layer {position: absolute; top: 0; left: 0; width: 100%; height: 100%;- will-change: transform, filter;+ /* promote for transform only — hinting `filter` keeps the blur pass hot+ and costs more memory than it saves */+ will-change: transform;+ transform: translate3d(0, 0, 0);}/* ── Sidebar ── */@@ -240,46 +243,93 @@ body{padding-top:46px}const slider = document.getElementById('focalSlider');const stage = document.querySelector('.scroll-stage');const VALUES = ROWS.map(r => r.querySelector('.val'));- const reducedMotion = window.matchMedia('(prefers-reduced-motion: reduce)').matches;- const isMobile = window.innerWidth <= 600;- const MAX_BLUR = isMobile ? 8 : 14;- const MAX_OFFSET = isMobile ? 60 : 120;- let manualOverride = false;+ const reduceMQ = window.matchMedia('(prefers-reduced-motion: reduce)');+ let reduced = reduceMQ.matches;- function applyFrame(f) {- if (reducedMotion) return;- let closestIdx = 0, closestDist = Infinity;- LAYERS.forEach((layer, i) => {- const dist = Math.abs(layer.depth - f);- if (dist < closestDist) { closestDist = dist; closestIdx = i; }- const blur = Math.round(MAX_BLUR * dist * 10) / 10;- const offset = layer.speed * f * MAX_OFFSET;- layer.el.style.filter = `blur(${blur}px)`;- layer.el.style.transform = `translateY(${offset}px)`;- VALUES[i].textContent = `${blur}px`;- });- ROWS.forEach((r, i) => r.classList.toggle('focused', i === closestIdx));+ // 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;+ const BLUR_STEP = 0.5;++ let MAX_BLUR, MAX_OFFSET, maxScroll;+ // Layout reads live here only — never in the scroll handler, where each one+ // would force a synchronous reflow before the styles below are written.+ function measure() {+ const mobile = window.innerWidth <= 600;+ MAX_BLUR = mobile ? 8 : 14;+ MAX_OFFSET = mobile ? 60 : 120;+ maxScroll = Math.max(1, stage.offsetHeight - window.innerHeight);+ }++ let target = 0, current = 0, raf = null, manual = false;+ let lastFocus = -1, lastSlider = -1;+ const lastBlur = LAYERS.map(() => -1), lastOffset = LAYERS.map(() => NaN);++ function render(f) {+ let idx = 0, best = Infinity;+ for (let i = 0; i < LAYERS.length; i++) {+ const layer = LAYERS[i], dist = Math.abs(layer.depth - f);+ if (dist < best) { best = dist; idx = i; }+ // Snapping blur to 0.5px steps lets the compositor reuse the blurred texture+ // instead of re-rasterising five full-screen layers on every single frame.+ const blur = Math.round(MAX_BLUR * dist / BLUR_STEP) * BLUR_STEP;+ if (blur !== lastBlur[i]) {+ layer.el.style.filter = blur > 0 ? `blur(${blur}px)` : 'none';+ VALUES[i].textContent = `${blur}px`;+ lastBlur[i] = blur;+ }+ const offset = reduced ? 0 : layer.speed * f * MAX_OFFSET;+ if (offset !== lastOffset[i]) {+ layer.el.style.transform = `translate3d(0, ${offset.toFixed(2)}px, 0)`;+ lastOffset[i] = offset;+ }+ }+ if (idx !== lastFocus) {+ for (let i = 0; i < ROWS.length; i++) ROWS[i].classList.toggle('focused', i === idx);+ lastFocus = idx;+ }+ }++ function tick() {+ const diff = target - current;+ if (Math.abs(diff) < 0.0002) { current = target; render(current); raf = null; return; }+ current += diff * EASE;+ 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);}function onScroll() {- if (manualOverride) return;- const max = stage.offsetHeight - window.innerHeight;- const f = Math.min(1, Math.max(0, window.scrollY / max));- slider.value = f * 100;- applyFrame(f);+ if (manual) return;+ target = Math.min(1, Math.max(0, window.scrollY / maxScroll));+ const v = Math.round(target * 100);+ if (v !== lastSlider) { slider.value = v; lastSlider = v; }+ schedule();}slider.addEventListener('input', () => {- manualOverride = true;- applyFrame(slider.value / 100);+ manual = true; lastSlider = +slider.value; target = slider.value / 100; schedule();});+ window.addEventListener('scroll', () => { manual = false; onScroll(); }, { passive: true });- window.addEventListener('scroll', () => {- manualOverride = false;- onScroll();- }, { passive: true });+ let rt;+ window.addEventListener('resize', () => {+ clearTimeout(rt);+ rt = setTimeout(() => {+ measure(); lastBlur.fill(-1); lastOffset.fill(NaN); onScroll(); render(current);+ }, 150);+ });+ reduceMQ.addEventListener('change', e => { reduced = e.matches; lastOffset.fill(NaN); schedule(); });- applyFrame(0);+ // Seed from wherever the page actually starts — no fly-in on load.+ measure();+ target = current = Math.min(1, Math.max(0, window.scrollY / maxScroll));+ slider.value = lastSlider = Math.round(target * 100);+ render(current);</script><script>(function(){var b=document.getElementById("ah-copy");if(!b)return;b.addEventListener("click",function(){fetch(location.href).then(function(r){return r.text();}).catch(function(){return document.documentElement.outerHTML;}).then(function(t){return navigator.clipboard.writeText(t);}).then(function(){b.textContent="Copied!";b.classList.add("ah-copied");setTimeout(function(){b.textContent="Copy source";b.classList.remove("ah-copied");},1500);}).catch(function(){b.textContent="Copy failed";setTimeout(function(){b.textContent="Copy source";},1500);});});})();</script></body>