50

If we use multiple <canvas> on a single html page does it hamper the performance of the application that is being developed and does the code get very bulky and require more time to load the page?

5 Answers 5

55

Sometimes multiple canvases results in better performance. It's best to test if you can afford the time.

Say you are making a program that has items on the screen and allows the user to draw a selection box.

With one canvas, to draw the selection box you'd have to redraw all of the elements over and over to update the selection box that the user sees since they are all on the same canvas.

Or, you can have two canvases, one with the objects and then another one in front for things like "tools" (like the selection box graphics). Here two canvases may be more efficient.

Other times you may want to have a background that changes very rarely and foreground objects that change all the time. Instead of redrawing all of them at 60 frames per second, you make a background canvas and foreground canvas, and only have the foreground's objects redraw at the fast speed. Here two canvases ought to be more efficient than one, but it may be more optimal to "cache" that background canvas as an image and drawing the image first each frame.

Sign up to request clarification or add additional context in comments.

2 Comments

oftentimes? Then under what circumstances multiple canvases results in worse performance?
On extremely large canvases it may be better to simply redraw simple background and foreground scenes (or save them to images) than to have 3+ large canvases.
5

I've used dozens of canvases on the same page display different graphs using a javascript graphing library. The graphs are quite fast, it's gathering the data for them that's a bit slow in our case.

If you want you can wait to do all your drawing until the rest of the page loads by kicking it off from the onLoad function.

1 Comment

you said : using a javascript graphing library. can you tell me the name of this ?
3

According to Mark Pilgrim, it's a good idea to use multiple canvases.

See This Link

Using multiple canvases can simplify things on your end, by isolating regions of the screen to update and isolating input events. If your page is well-suited for dividing-up regions of the screen, I say go for it.

3 Comments

I can't see this concrete cite currently in this article, and the Great WayBack Machine have't archived it :(. It was removed from there with time? Why?
It's a nice tutorial, but I can't find any content related to multiple canvases in there.
3

Also, HTML5Rocks says it is a best approach:

If your game or multimedia app can be split up into a foreground and background, consider rendering these on separate canvases to get a significant performance boost.

You can often take advantage of imperfect human perception and render the background just once or at a slower speed compared to the foreground (which is likely to occupy most of your user’s attention). For example, you can render the foreground every time you render, but render the background only every Nth frame.

this approach generalizes well for any number of composite canvases

Comments

2

A single instance runs smoothly, more does not affect rendering on page. Data is the factor of slowing canvas down. In order to increase page loading time, you can simply call canvas rendering methods after page loading.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.