简体   繁体   中英

RequestAnimationFrame for multiple canvases

I've got a page with layered <canvas> elements like explained in the answer here . The canvases together make up an animation, so each is cleared and redrawn as necessary.

Now I'm trying to incorporate requestAnimationFrame by using the cross browser shim . But I don't really know what requestAnimationFrame is doing behind the scenes.

Is it okay to have it update multiple canvases in each loop? Should each canvas have its own loop? Is the answer browser dependent?

Updating all the canvases in a single requestAnimationFrame is perfectly okay.

If the canvases are independent from each other and appear on different sections of the page, then you might want to use individual requestAnimationFrame handlers, passing the canvas element as the second argument. That way, only the currently visible canvases get updated. (Passing an element as the second argument is WebKit-specific, though.)

What requestAnimationFrame does is tell the browser that you would like to update the appearance of the page. The browser calls the callback when the page or element is next up for a redraw. And that's going to happen when the page/element is visible, and never more often than the screen refresh rate.

Using requestAnimationFrame simply lets the browser control when reflows/repaints on the page happen.

It would be better to alter all the canvases in one callback, so the browser can repaint them in one go.

This explanation of requestAnimationFrame was pretty helpful

The shim merely falls back to a timer if it is not available.

You should be fine. Update all of them in the same single loop.

requestAnimFrame isn't "tied" to a canvas or anything, just the function you pass it. So you can use it with three canvases or zero canvases just fine.

use canvas in this format js file so RequestAnimationFrame for multiple canvas will work

(function(){ ... })();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM