Questions tagged [requestanimationframe]

HTML5 window.requestAnimationFrame API is an alternative to setTimeout for animations or applications running in a loop.

The HTML5 window.requestAnimationFrame(callback) function sends a request to the browser that the callback() function is to be called before the next repaint of the window.

When the callback() function is invoked, it is passed a DOMHighResTimeStamp parameter, indicating the time in milliseconds at which the callback function was called.

To continue an animation, call the requestAnimationFrame() function from within itself:

function nextFrame(timeStamp) {
// animation
requestAnimationFrame(nextFrame); // repeats the animation
}

requestAnimationFrame(nextFrame); // starts the animation

The browser handles the repaint.

Specification

Resources

804 questions
215
votes
15 answers

Controlling fps with requestAnimationFrame?

It seems like requestAnimationFrame is the de facto way to animate things now. It worked pretty well for me for the most part, but right now I'm trying to do some canvas animations and I was wondering: Is there any way to make sure it runs at a…
97
votes
1 answer

scroll events: requestAnimationFrame VS requestIdleCallback VS passive event listeners

As we know, it's often advised to debounce scroll listeners so that UX is better when the user is scrolling. However, I've often found libraries and articles where influential people like Paul Lewis recommend using requestAnimationFrame. However as…
76
votes
3 answers

Why doesn't jQuery use requestAnimationFrame?

Some browsers support requestAnimationFrame, so why not use it? After all, it's been supported since Google Chrome 10. Despite that, jQuery does not seem to be using it. I've found a bug report about it, but no real explanation was given? I'm sure…
Randomblue
  • 112,777
  • 145
  • 353
  • 547
66
votes
4 answers

Multiple requestAnimationFrame performance

If I’m doing multiple animations, is it OK performance-wise to add multiple requestAnimationFrame callbacks? F.ex: function anim1() { // animate element 1 } function anim2() { // animate element 2 } function anim3() { // animate…
David Hellsing
  • 106,495
  • 44
  • 176
  • 212
52
votes
3 answers

How to use requestAnimationFrame?

I'm new to animation, but I have recently created an animation using setTimeout. The FPS was too low, so I found a solution to use requestAnimationFrame, described in this link. So far, my code is: //shim layer with setTimeout fallback …
einstein
  • 13,389
  • 27
  • 80
  • 110
46
votes
12 answers

Calculate FPS in Canvas using requestAnimationFrame

How could I calculate the FPS of a canvas game application? I've seen some examples, but none of them use requestAnimationFrame, and im not sure how to apply their solutions there. This is my code: (function(window, document, undefined){ …
Enrique Moreno Tent
  • 24,127
  • 34
  • 104
  • 189
46
votes
1 answer

$(window).scroll in vanilla JavaScript

What's the equivalent of the following in plain JS? $(window).scroll(function() { }); I'm also looking to animate scroll, e.g.: $('html, body').animate({scrollTop:1750}, 'slow'); Should I be using…
Neo
  • 1,027
  • 3
  • 12
  • 30
40
votes
6 answers

Limiting framerate in Three.js to increase performance, requestAnimationFrame?

I was thinking that for some projects I do 60fps is not totally needed. I figured I could have more objects and things that ran at 30fps if I could get it to run smoothly at that framerate. I figured if I edited the requestAnimationFrame shim inside…
Cory Gross
  • 36,833
  • 17
  • 68
  • 80
31
votes
3 answers

How can I pass argument with requestAnimationFrame?

In the main program I randomly choose an object which I'd like to animate, so I call the function with the object as the argument. The first loop is okay, x is finely set, but in the next turn it becomes undefined. Something like this: var anim = { …
Torfiks
  • 417
  • 1
  • 5
  • 8
30
votes
3 answers

requestAnimationFrame garbage collection

I'm profiling the following code's memory usage using the Timeline in Chrome Dev Tools v27. RAF
25
votes
2 answers

When will requestAnimationFrame be executed?

Browser reads and runs a JavaScript file, the synchronous tasks written in the file immediately become in-mid-execution task, setTimeout callbacks become macrotasks, and promise callbacks become microtasks. Everything is good. I thought I mastered…
25
votes
1 answer

Does React use requestAnimationFrame? If so, how does it use it?

I found 2 sources where it kind of says that React is using requestAnimationFrame On this blog post about Om, a ClojureScript framework on top of React. Also on the comments of this SO answer. I'm not sure it's part of React or if people are using…
Sebastien Lorber
  • 89,644
  • 67
  • 288
  • 419
23
votes
4 answers

requestAnimationFrame at beginning or end of function?

If I have a loop using requestAnimationFrame like this: function render() { // Rendering code requestAnimationFrame(render); } Will there be any difference if I put the requestAnimationFrame in the beginning of the function, like…
user2039981
23
votes
2 answers

Chrome requestAnimationFrame issues

Related topic: requestAnimationFrame garbage collection I've been working toward smooth animations in a widget that I'm building for touch devices, and one of the tools I found to help me with this has been the Chrome Memory Timeline screen. It's…
Steven Lu
  • 41,389
  • 58
  • 210
  • 364
22
votes
1 answer

How to observe DOM element position changes

I need to observe a DOM element position as I need to show a popup panel relative to it (but not in the same container) and the panel should follow the element. How I should implement such logic? Here is a snippet where you can see the opening of…
1
2 3
53 54