Search code examples
javascripthtmlhtml5-canvashtml5-animationrequestanimationframe

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....


Solution

  • 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.