raf - requestAnimationFrame polyfill
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
-
TypeError: raf is not a function
cause This error typically occurs when `require('raf/polyfill')` or `import 'raf/polyfill';` is used to polyfill the global scope, and then the developer attempts to call `raf()` as if it were a locally imported function, rather than accessing `window.requestAnimationFrame` or `global.requestAnimationFrame`.fixIf you are only polyfilling the global scope, access the function via `window.requestAnimationFrame` (or `global.requestAnimationFrame` in Node). If you intend to use `raf` as a local function, ensure you explicitly `import raf from 'raf';` or `const raf = require('raf');`. -
ReferenceError: window is not defined
cause Attempting to access `window.requestAnimationFrame` or `window.raf` directly in a Node.js environment where the `window` object does not exist, before `raf/polyfill` has been correctly applied to polyfill the `global` object.fixIn Node.js, ensure `require('raf/polyfill');` or `import 'raf/polyfill';` is executed early in your application's lifecycle to polyfill `global.requestAnimationFrame`. Alternatively, use the `raf` function returned by `require('raf')` directly without relying on global `window`.
Warnings
- breaking The stream and event emitter logic that was part of `raf` in versions prior to 1.0.0 has been removed. This functionality was extracted and moved to a separate package, `raf-stream`.
- gotcha There's a distinction between importing the `raf` function directly (e.g., `import raf from 'raf'`) and applying the polyfill to the global scope (e.g., `import 'raf/polyfill'`). The former provides a local utility, while the latter modifies `window.requestAnimationFrame` (or `global.requestAnimationFrame` in Node).
- gotcha The `raf` package provides different consumption patterns for various environments: CommonJS (Node, bundlers), AMD (RequireJS), and direct `<script>` inclusion. Mis-matching the import/require method with your environment can lead to errors.
Install
-
npm install raf -
yarn add raf -
pnpm add raf
Imports
- raf
import { raf } from 'raf';import raf from 'raf';
- raf (CommonJS)
import raf from 'raf';
const raf = require('raf'); - raf.polyfill
import { polyfill } from 'raf';import 'raf/polyfill';
- raf (global)
window.raf(...);
Quickstart
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);