Nowadays, various web development technologies are introduced to create elegant web pages. While talking about elegant web pages, one of the most important things that come to mind is animation. There are many ways to generate them, modern CSS is one of them but JavaScript has always been a power-packed option to do so.
The requestAnimationFrame() is one of the methods present in JavaScript to powerfully incorporate amazing and simple animations within our project. Earlier methods like setTimeout() or setInterval() were used which was okay but they slow down the whole process. The main problem with them was of synchronization. The transition time was very slow and didn’t match with the user’s system perfectly.
Here, requestAnimationFrame() came into the picture. Basically, requestAnimationFrame() method easily syncs in with your browser timings and generate a call to perform the specific animation before the actual loading of the screen. Further, it also slows down its process when the animation is actually not in the use thus saving resources.
Syntax:
window.requestAnimationFrame( callback );
Parameter: This method accepts single parameter as mentioned above and described below:
- callback: Unless you want the animation to stop, you should write the callback function so that it calls itself so that a request to the next frame is made. Callback function takes timestamp or simply a time value at which it should start executing.
Return Values: This method returns a non zero long integer value that acts as a unique identity for the animation entry in callback function.
Below examples illustrate the requestAnimationFrame() method in Web API:
Example 1:
<!DOCTYPE html> <html> <head> <title> Window.requestAnimationFrame() Method </title> </head> <body> <div id="gfg"> <h1 style="color:green;">GeeksforGeeks</h1> <h4>Window.requestAnimation()</h4> </div> <script type="text/javascript"> // Setting the start point for animation var start = null; var element = document.getElementById('gfg'); function startAnim(timestamp) { // Timestamp is an integer that represents the number // of seconds elapsed since January 1 1970. if (!start) start = timestamp; // Setting the difference between timestamp // and the set start point as our progress var progress = timestamp - start; // Moving our div element element.style.transform = 'translateX(' + Math.min(progress / 10, 700) + 'px)'; window.requestAnimationFrame(startAnim); } window.requestAnimationFrame(startAnim); </script> </body> </html> |
Output:

Example 2:
<!DOCTYPE html> <html> <head> <style> div { position: absolute; left: 10px; top: 50px; padding: auto; color: white } h1 { color: green; } </style> </head> <body> <div id="gfg"> <h1>GeeksforGeeks</h1></div> <center> <button onclick="start()">Start the animation</button> </center> <script type="text/javascript"> var x = document.getElementById("gfg"); // Initializing variables var requestId; var stopped; var starttime; function startAnim(time) { // Set left style to a function of time if it is not stopped if (!stopped) { // We use the difference between time returned // by Data.now() and the animation starttime x.style.left = ((Date.now() - starttime) / 10 % 700) + "px"; requestId = window.requestAnimationFrame(startAnim); } } function start() { // Return the number of milliseconds since 1970/01/01: starttime = Date.now(); // Starting point of animation requestId = window.requestAnimationFrame(startAnim); stopped = false; // Means animation will not stop } </script> </body> </html> |
Output:

Supported Browsers: The browsers supported by Window.requestAnimationFrame() Method are listed below:
- Google Chrome 23.0
- Internet Explorer 10.0
- Firefox 11.0
- Opera 10.0
- Safari 6.1
Recommended Posts:
- Javascript | Window Open() & Window Close Method
- Javascript | Window Blur() and Window Focus() Method
- Web Window API | Window locationbar property
- Web Window API | Window scrollbars property
- Web Window API | DOM Window fullscreen property
- Web Window API | Window personalbar property
- Web Window API | Window statusbar property
- Web Window API | Window toolbar property
- Web Window API | Window menubar property
- JavaScript | Window print() Method
- HTML | Window alert( ) Method
- HTML | Window atob( ) Method
- HTML | DOM Window btoa() Method
- HTML | Window moveBy() Method
- Javascript | Window scrollTo() Method
- Javascript | Window prompt() Method
- Javascript | Window confirm() Method
- HTML | DOM Window stop() Method
- JavaScript | Window getComputedStyle() Method
- HTML | Window resizeTo() Method
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


