The window.requestAnimationFrame()
method tells the browser that you wish to perform an animation and requests that the browser call a specified function to update an animation before the next repaint. The method takes a callback as an argument to be invoked before the repaint.
requestAnimationFrame()
if you want to animate another frame at the next repaint.You should call this method whenever you're ready to update your animation onscreen. This will request that your animation function be called before the browser performs the next repaint. The number of callbacks is usually 60 times per second, but will generally match the display refresh rate in most web browsers as per W3C recommendation. requestAnimationFrame()
calls are paused in most browsers when running in background tabs or hidden <iframe>
s in order to improve performance and battery life.
The callback method is passed a single argument, a DOMHighResTimeStamp
, which indicates the current time when callbacks queued by requestAnimationFrame()
begin to fire. Multiple callbacks in a single frame, therefore, each receive the same timestamp even though time has passed during the computation of every previous callback's workload. This timestamp is a decimal number, in milliseconds, but with a minimal precision of 1ms (1000 µs).
window.requestAnimationFrame(callback);
callback
DOMHighResTimeStamp
returned from performance.now()
, indicating the point in time when requestAnimationFrame()
starts to execute callback functions.A long
integer value, the request id, that uniquely identifies the entry in the callback list. This is a non-zero value, but you may not make any other assumptions about its value. You can pass this value to window.cancelAnimationFrame()
to cancel the refresh callback request.
var start = null; var element = document.getElementById('SomeElementYouWantToAnimate'); element.style.position = 'absolute'; function step(timestamp) { if (!start) start = timestamp; var progress = timestamp - start; element.style.left = Math.min(progress / 10, 200) + 'px'; if (progress < 2000) { window.requestAnimationFrame(step); } } window.requestAnimationFrame(step);
Specification | Status | Comment |
---|---|---|
HTML Living Standard The definition of 'requestAnimationFrame' in that specification. | Living Standard | No change, supersedes the previous one. |
Timing control for script-based animations The definition of 'requestAnimationFrame' in that specification. | Obsolete | Initial definition |
Desktop | ||||||
---|---|---|---|---|---|---|
Chrome | Edge | Firefox | Internet Explorer | Opera | Safari | |
Basic support | 24
|
Yes | 23
|
10 | 15
|
6.1
|
Return value | 23 | Yes | 11 | 10 | 15 | 6.1 |
Mobile | |||||||
---|---|---|---|---|---|---|---|
Android webview | Chrome for Android | Edge Mobile | Firefox for Android | Opera for Android | iOS Safari | Samsung Internet | |
Basic support | Yes | 25
|
Yes | 23
|
15 | 7.1
|
? |
Return value | Yes | 25 | Yes | 14 | 15 | 6.1 | ? |
Window.mozAnimationStartTime
Window.cancelAnimationFrame()
© 2005–2018 Mozilla Developer Network and individual contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/API/window/requestAnimationFrame