raf - requestAnimationFrame polyfill

3.4.1 · active · verified Tue Apr 21

The `raf` package provides a `requestAnimationFrame` polyfill, making the browser animation API available in non-browser environments like Node.js and ensuring broader compatibility across older browser versions. The current stable version is 3.4.1. It maintains a simple, direct API for scheduling animation callbacks, distinct from event emitter or stream-based approaches which were moved to the separate `raf-stream` package in versions prior to 1.0.0. Its primary differentiator is its lightweight nature and dedicated support for Node.js environments without relying on heavy browser emulation. Release cadence is infrequent, typical for a stable polyfill, with updates primarily addressing compatibility issues or minor bug fixes, rather than introducing new features. This package is a foundational utility for cross-environment animation loops.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates a basic animation loop using `raf` to schedule a function (`animate`) to run on subsequent animation frames, logging progress until a predefined frame limit is reached.

import raf from 'raf';

let frameCount = 0;
const maxFrames = 100;

console.log('Starting animation loop...');

function animate() {
  // Perform animation logic here
  console.log(`Processing animation frame: ${frameCount}`);
  frameCount++;

  if (frameCount < maxFrames) {
    raf(animate); // Schedule the next frame
  } else {
    console.log('Animation finished after', maxFrames, 'frames.');
  }
}

// Start the initial animation frame
raf(animate);

view raw JSON →