requestAnimationFrame & VSync vs Scroll-Driven Animations
rAF scroll loops sample scrollTop on the main thread, while Scroll-Driven Animations bind timelines to scroll on the compositor.
Structured Comparison Matrix
| Dimension | requestAnimationFrame & VSync | Scroll-Driven Animations |
|---|---|---|
| Mental Model | requestAnimationFrame schedules callbacks right before the browser paints the next VSync frame, matching display refresh rates. | animation-timeline: scroll() and view() bind CSS and WAAPI animations directly to scroll progress on the Compositor Thread. |
| Maturity & Domain | Baseline · Widely · Rendering Pipeline Chrome 24+ · Firefox 23+ · Safari 6.1+ | Experimental · Rendering Pipeline Chrome 115+ · Firefox Behind flag (layout.css.scroll-driven-animations.enabled) · Safari 26+ |
| Prerequisites | ||
| When Not to Use | When not to use requestAnimationFrame: Do not use rAF for simple transform or opacity transitions; use CSS transitions, the Web Animations API, or Scroll-Driven Animations so they run off the main thread. | When not to use Scroll-Driven Animations: Do not use them for critical navigation state or binary sticky-header triggers where IntersectionObserver or position: sticky suffices. |
Engine Mechanics & Trade-Offs
Compositor ticks on display VSync (60Hz/120Hz), triggers BeginMainFrame, drains rAF callbacks, and commits the frame.
Key Pitfalls: Frame-rate-dependent speed: Incrementing x += 2 per rAF callback runs twice as fast on a 120Hz screen as on a 60Hz screen; always use timestamp deltas. Multiple rAF loops: Spawning independent rAF loops per component fragments main-thread timing and invites Layout Thrashing.
Compositor Thread reads scroll offset directly and samples the linked animation timeline at 60/120fps without waking JS.
Key Pitfalls: Missing @supports guard: Setting opacity: 0 at 0% leaves elements permanently invisible in browsers lacking animation-timeline support. Animating width instead of scaleX(): Using width forces Layout on every scroll tick, forfeiting compositor offloading.