Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does requestAnimationFrame function accept an element as an argument?

I am just trying to understand why the hell window.requestAnimationFrame is accepting the second parameter as an element, what is the reason behind that?

I am curious to know the underlying execution for this function....

like image 997
Software Enthusiastic Avatar asked Sep 20 '11 15:09

Software Enthusiastic


People also ask

What does requestAnimationFrame return?

requestAnimationFrame returns an ID you can use to cancel it, just like setTimeout or setInterval does. jQuery used here only to demonstrate a simple animation and bind events.

Why should I use requestAnimationFrame?

To optimize system and browser resources, it is recommended to use requestAnimationFrame , which requests the browser to execute the code during the next repaint cycle. This allows the system to optimize resources and frame-rate to reduce unnecessary reflow/repaint calls.

What is requestAnimationFrame in Javascript?

requestAnimationFrame() method tells the browser that you wish to perform an animation and requests that the browser calls 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.

Is requestAnimationFrame asynchronous?

As now you know that the rAF is a Web API, that means the callback will be called asynchronously. Unlike setInterval , requestAnimationFrame does not accept delay argument, instead, it only calls the callback function when the browser is ready to perform the next paint operation.


1 Answers

It's added so that when an element is out of view (because of scrolling for example), the animation is not run.

From the specs:

Editorial note

ISSUE-4 Do we want to allow an Element to be passed to requestAnimationFrame, so that animations affecting the given element are throttled or paused when scrolled out of view?

More information about this issue can be found here.

like image 157
pimvdb Avatar answered Oct 21 '22 19:10

pimvdb