requestAnimationFrame()
The window.requestAnimationFrame() method tells the browser you wish to perform an animation. It requests the browser to call a user-supplied callback function before the next repaint.
The frequency of calls to the callback function will generally match the display refresh rate. The most common refresh rate is 60hz, (60 cycles/frames per second), though 75hz, 120hz, and 144hz are also widely used. requestAnimationFrame() calls are paused in most browsers when running in background tabs or hidden <iframe>s, in order to improve performance and battery life.
Browser support
| Feature | Desktop | Mobile | ||||
|---|---|---|---|---|---|---|
| Chrome | Edge | Firefox | Safari | Chrome Android | Safari iOS | |
| 24 | 12 | 23 | 7 | 25 | 7 | |
| The window.cancelAnimationFrame() method cancels an animation frame request previously scheduled through a call to window.requestAnimationFrame(). | 24 | 12 | 23 | 7 | 25 | 7 |
1+Supported (version) Not supported ※Has note Sub-feature descriptions sourced from MDN Web Docs (CC BY-SA 2.5)
Notes 1 item(s)
Compatibility
- Available with a vendor prefix: webkit (10)
Notes 1 item(s)
Implementation note
- Before version 17, Edge does not reliably fire `requestAnimationFrame` before the paint cycle.
Notes 7 item(s)
Implementation note
- Callback parameter is a `DOMHighResTimestamp`. This means ten microsecond precision and zero time as `performance.now()`.
- Callback parameter is a `DOMTimestamp`. This means millisecond precision and zero time as `Date.now()`.
- Could be called with no input parameters.
Removed
- This feature was removed in a later browser version (42)
- This feature was removed in a later browser version (11)
Compatibility
- Available with a vendor prefix: moz (11)
- Available with a vendor prefix: moz (4)
Notes 1 item(s)
Compatibility
- Available with a vendor prefix: webkit (6)
Notes 1 item(s)
Compatibility
- Available with a vendor prefix: webkit (18)
Notes 1 item(s)
Compatibility
- Available with a vendor prefix: webkit (6)
Notes 2 item(s)
Removed
- This feature was removed in a later browser version (23)
Compatibility
- Available with a vendor prefix: moz (11)
Notes 2 item(s)
Removed
- This feature was removed in a later browser version (7)
Compatibility
- Available with a vendor prefix: webkit (6)
Notes 2 item(s)
Removed
- This feature was removed in a later browser version (7)
Compatibility
- Available with a vendor prefix: webkit (6)
Syntax
JAVASCRIPT
const element = document.getElementById("some-element-you-want-to-animate");
let start;
function step(timestamp) {
if (start === undefined) {
start = timestamp;
}
const elapsed = timestamp - start;
// Math.min() is used here to make sure the element stops at exactly 200px
const shift = Math.min(0.1 * elapsed, 200);
element.style.transform = `translateX(${shift}px)`;
if (shift < 200) {
requestAnimationFrame(step);
}
}
requestAnimationFrame(step); Use cases
-
Using requestAnimationFrame()
The window.
Cautions
- May not be supported in older browsers.
Implementation notes
- Some APIs require a secure context (HTTPS) or user activation. Check the requirements before use.