简体   繁体   English

分层多个画布的效率如何?

[英]How efficient is layering multiple canvases?

I've been wondering if layering canvases is actually an efficient means of increasing performance on a game. 我一直在想,对画布进行分层是否实际上是一种提高游戏性能的有效方法。

Eg, 2 or 3 canvases of the same size on top of each other. 例如,2或3个相同大小的画布彼此重叠。

It's difficult to find results of testing from this, but from what I understand about drawing, if the layer on top of the others is changed, the browser would have to repaint the area to pixels anyway. 很难从中找到测试结果,但是根据我对绘图的理解,如果更改了其他图层之上的图层,则浏览器无论如何都必须将区域重新绘制为像素。 Furthermore if those canvases all had to move at the same time, you've multiplied the amount of pixels that would need to be calculated. 此外,如果所有画布都必须同时移动,那么您需要乘以需要计算的像素数量。

I was wondering how the browser handles painting canvases beneath the others if an element above changes. 我想知道如果上方的元素发生变化,浏览器如何处理其他下方的绘画画布。

Is it more efficient due to not having to call the canvas methods themselves again? 是否由于不必再次调用canvas方法本身而效率更高? Wouldn't they still need painting? 他们还不需要画画吗?

My experience is anecdotal, so you really need to test your idea to see if its better. 我的经验是轶事,所以您真的需要测试您的想法,看看它是否更好。

You're probably going to get the best performance by creating offscreen (in memory) canvases and compositing the different layers of your game with those into a single visible canvas where the game is viewed. 通过创建屏幕外(内存中)画布并将游戏的不同图层与这些图层合成到一个用于查看游戏的可见画布中,您可能会获得最佳性能。

I'm not sure if this will be very helpful, but I'll tell you anyway. 我不确定这是否会很有帮助,但是我还是会告诉您。 Let's say that you've got 2 canvases and you want your game to switch between them under certain condition. 假设您有2张画布,并且您希望游戏在特定条件下在它们之间切换。 This might be possible by using some HTML, CSS and JavaScript. 这可以通过使用一些HTML,CSS和JavaScript来实现。

 <canvas id="canvas1" width=500 height=500 style="border: 2px solid #00F"></canvas> <canvas id="canvas2" width=500 height=500 style="border: 2px solid #F00"></canvas> <button id="switcher" onclick="switchcanvas=true">Click here to switch the canvas!</button> <style> #canvas1 { left: 0%; right: 0%; display: block; } #canvas2 { left: 0%; right: 0%; display: none; } </style> <script> var switchcanvas = false; var canvas1 = document.getElementById("canvas1"); var canvas2 = document.getElementById("canvas2"); function update() { if (switchcanvas) { if (canvas1.style.display === "block") { canvas1.style.display = "none"; canvas2.style.display = "block"; switchcanvas = false; //if you set this to true or remove it the canvases will keep switching. } else { canvas1.style.display = "block"; canvas2.style.display = "none"; switchcanvas = false; } } } update(); setInterval(update, 1); //function update will update every ms, you can change that though. </script> 

I tested it and it worked. 我对其进行了测试,并且效果良好。 And yes, it should improve your game's performance since each canvas can have its own context. 是的,它可以提高游戏的性能,因为每个画布都可以有自己的上下文。

I don't know if this is correct, but in my experience if you use two Canvases with the same frame (refresh) rate the performance improvement would be minimal, if there is any at all. 我不知道这是否正确,但是根据我的经验,如果您使用两个具有相同帧(刷新)速率的画布,则性能提升将很小,如果有的话。 You would get an improvement if the refesh rate for Canvas 1 is lower than Canvas 2. A good use case for this would be a grid and a moving object. 如果Canvas 1的刷新率低于Canvas 2,您将得到改善。一个很好的用例是网格和移动的对象。 If the grid doesn't change, you don't have to redraw it. 如果网格没有变化,则不必重新绘制网格。 This does not apply for the moving object, because you want to see it moving. 这不适用于移动的对象,因为您希望看到它在移动。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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