admin管理员组文章数量:1323694
I am working on a mobile web dice simulator. The initial prototype is here:
I currently have one large canvas where I draw all the dice. I am planning to re-write the code in way that is more MVC and easier for me to update. I think it would be easier for me to generate a small canvas for each die object than to draw all of the dice on the big canvas and keep updating that big canvas.
My question is if there is a bad performance hit in having the browser create lots of little canvases vs one big one. It's hard to test locally, I was hoping someone here might know what the best practice is.
I am working on a mobile web dice simulator. The initial prototype is here: http://dicewalla.
I currently have one large canvas where I draw all the dice. I am planning to re-write the code in way that is more MVC and easier for me to update. I think it would be easier for me to generate a small canvas for each die object than to draw all of the dice on the big canvas and keep updating that big canvas.
My question is if there is a bad performance hit in having the browser create lots of little canvases vs one big one. It's hard to test locally, I was hoping someone here might know what the best practice is.
Share Improve this question asked Aug 9, 2012 at 12:21 Ben BrophyBen Brophy 2811 silver badge8 bronze badges 3- are you using setInterval to control animation on your new build? I have this same question. Although - in my case - I have a constant animation that is being controlled by setInterval. Having several setIntervals on one page would be way less desirable than having all animation in one canvas - so the single canvas approach seems like a more efficient approach. – Joel Commented Aug 9, 2012 at 12:32
- I'd say, use 6 canvas objects to draw the sides,and then use them as background images for the rolled dice. There is use a huge number of canvases since you aren't modifying the grafics afterwards. Heck, you could do this even without canvas pletely, just going for radial gradients and styled divs. – Torsten Walter Commented Aug 9, 2012 at 12:35
- Follow up - I tried both approaches. While I don't have exact measurements, using multiple canvases preformed noticeably better. I am not using animations, just redrawing canvases when a button is clicked. – Ben Brophy Commented Oct 10, 2012 at 17:54
2 Answers
Reset to default 4Multiple canvases usually allow for better performance as you're able to selectively re-render.
If you have only one canvas and want to update one die, you'll typically have to redraw the entire canvas. On the other hand, multiple canvases allow you to update only the dice that need to be redrawn. That's an increase in efficiency.
Further, you shouldn't see any noticeable difference in loading 1 canvas versus 100.
In terms of performance, like was mentioned earlier, there should be little difference between 1-100 canvas elements if you're not updating the graphics on a regular basis. (ie: static graphics / no animation)
Most of the references around the net regarding multiple canvases tend to deal with cases where you have multiple layers and need to handle drawing on top of other things with transparency.
That being said, what you're doing with dicewalla doesn't look like it will gain anything from having multiple canvases.
Also you can selectively redraw the regions of a single canvas to get better performance if updating the entire canvas is a bottleneck. This gives you the performance benefits of having multiple canvases without having to deal with managing and creating those elements.
本文标签:
版权声明:本文标题:javascript - Is it better to have one big canvas or up to 100 small dynamically generated canvases? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742136041a2422373.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论