是否需要太多画布来重绘游戏循环内的所有内容?
目前,我只对块和播放器的显示进行了编码。
当游戏需要自我更新(每 10 毫秒左右)时,它需要调用 draw()
,它当前将根据玩家、块和球的当前状态重新绘制整个画布。
它的性能开始成为一个问题。
每帧重新绘制整个画布不是一个好主意吗?我是否应该更改代码以仅绘制正在更改的部分?
I've started a Breakout game in Canvas.
At the moment, I've only coded the display of the blocks and player.
When the game needs to update itself (every 10ms or so) it will need to call draw()
which is currently going to repaint the entire canvas based on the current state of player, blocks and ball.
Its performance is starting to become an issue.
Is it never a good idea to repaint the entire canvas per frame? Should I be altering my code to only paint the sections which are changing?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
首先:是的,更改代码以仅绘制正在更改的部分可能会有很大帮助,但您应该始终使用自己的代码测试特定的改进,因为任何一项优化的性能都会因应用程序而异(有时很大)。
但导致速度缓慢的不仅仅是绘图。例如,请确保您不会重新计算/重建绘图循环中永远不会改变的任何内容。
另外,如果您有很多对象,除非必要,否则不要设置 fillStyle,这意味着可以通过将填充设置为一种颜色来绘制该颜色的所有对象来进行优化,然后设置第二个填充颜色等。
最后,我建议编写整个游戏(或大部分),然后返回并进行优化。
Canvas 需要进行大量优化。我最近开始编写一本关于游戏相关性能增强的指南,希望能在今年年底完成。
First off: Yes, altering your code to only paint the sections that are changing may help lots, but you should always be testing specific improvements with your own code as the performance of any one optimization varies by app (sometimes greatly).
But its not only drawing that can cause slowness. Make sure, for instance, that you aren't recomputing/reconstructing anything in your draw loop that never changes.
Also, if you have a lot of objects, don't set
fillStyle
unless you have to, which means there's an optimization to be had by setting the fill to one color, drawing all the objects of that color, and then setting the second fill color, etc.Finally, I'd suggest writing your entire game (or most of it) and then going back and doing optimizations.
There are loads of optimizations to be had with Canvas. I've recently begun a guidebook on game-related performance enhancements, hopefully it'll be done by the year's end.
你必须尝试确定,但我不同意这里的两个答案。在我的简单测试中,尝试清除并重绘画布的特定区域会产生稍差的性能,而不是更好。
您可以在这里看到我的测试: http://phrogz.net/tmp/image_move_sprites_canvas.html
这不过,这取决于您的需求。如果您有数百个不需要更新的项目,并且画布中只有一小部分会更改每个帧,那么也许只清除并重新绘制该部分会很好。
You will have to try to be sure, but I disagree with both answers here. In my simple tests, attempting to clear and redraw just particular regions of the canvas produces slightly worse performance, not better.
You can see my test here: http://phrogz.net/tmp/image_move_sprites_canvas.html
This depends on your needs, however. If you have hundreds of items that do not need to be updated and only a small portion of your canvas changes each frame, perhaps clearing and redrawing only that section will be good.
不,有时这是个好主意
,但不是那时。
尽管如此,您的代码并没有那么复杂,也没有什么会导致明显的大规模减速。您如何衡量其性能?
No, sometimes it's a perfectly good idea
But not then.
That said, though, your code is not that complicated and there's nothing to cause obvious massive slowdowns. How are you measuring its performance?
首先,我将从 init() 中取出所有这些:
没有必要每毫秒都处理它!
其次,看看这个: http://paulirish.com/2011/requestanimationframe-for-智能动画/
First I'd take all this out of init():
There's no point dealing with that every millisecond!
Secondly, look at this: http://paulirish.com/2011/requestanimationframe-for-smart-animating/