SVG Animation and requestAnimationFrame Loops in JS

JavaScript animation libraries rely on the requestAnimationFrame (rAF) API to drive smooth, synchronized updates to Scalable Vector Graphics (SVG). By orchestrating a centralized animation loop, these libraries calculate value deltas over time, batch DOM updates to prevent layout thrashing, and directly manipulate SVG attributes and transform matrices at optimal points during the browser’s render pipeline.

The Centralized rAF Ticker

Instead of spawning isolated requestAnimationFrame callbacks for each animated element, libraries like GSAP, Anime.js, and Velocity use a single, shared “ticker” or loop manager. A global clock monitors elapsed time (performance.now()), calculating the delta between frames. This ensures that hundreds of simultaneous SVG element transformations stay synchronized to the same frame rate (typically 60Hz or 120Hz) and automatically pauses execution when the browser tab loses focus to conserve CPU and GPU resources.

Decoupled Calculation and Rendering Phases

To maintain 60 frames per second, libraries decouple calculation from DOM manipulation:

  1. Interpolation Phase: The library computes easing functions, normalized progress (\(t \in [0, 1]\)), and intermediate values purely in memory. Complex calculations—such as Bézier curve path interpolations or color conversions—occur without touching the DOM.
  2. Batching Phase: The library groups all SVG attribute updates into a write phase. Reading layout properties (such as getBBox() or getBoundingClientRect()) is strictly prohibited during this step because interleaving reads and writes forces browser reflows (layout thrashing). Any necessary measurements are cached prior to loop execution.

Direct SVG Attribute and Property Updates

Unlike standard HTML elements, which frequently rely on CSS classes or 3D hardware-accelerated transforms, SVG rendering requires specialized handling:

Garbage Collection and Memory Management

Memory allocations inside a 60fps loop trigger garbage collection pauses, causing visible frame stutter (jank). Production animation libraries pre-allocate objects, recycle transform matrices, and mutate reusable typed arrays rather than instantiating new objects on every tick of requestAnimationFrame.