是否需要太多画布来重绘游戏循环内的所有内容?

发布于 2024-11-03 08:45:54 字数 279 浏览 1 评论 0原文

我在 Canvas 中开始了打砖块游戏

目前,我只对块和播放器的显示进行了编码。

当游戏需要自我更新(每 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

贩梦商人 2024-11-10 08:45:54

首先:是的,更改代码以仅绘制正在更改的部分可能会有很大帮助,但您应该始终使用自己的代码测试特定的改进,因为任何一项优化的性能都会因应用程序而异(有时很大)。

但导致速度缓慢的不仅仅是绘图。例如,请确保您不会重新计算/重建绘图循环中永远不会改变的任何内容。

另外,如果您有很多对象,除非必要,否则不要设置 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.

还不是爱你 2024-11-10 08:45:54

你必须尝试确定,但我不同意这里的两个答案。在我的简单测试中,尝试清除并重绘画布的特定区域会产生稍差的性能,而不是更好。

您可以在这里看到我的测试: 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.

冷血 2024-11-10 08:45:54

每帧重新绘制整个画布不是一个好主意吗?

不,有时这是个好主意

它的性能开始成为一个问题

,但不是那时。

尽管如此,您的代码并没有那么复杂,也没有什么会导致明显的大规模减速。您如何衡量其性能?

Is it never a good idea to repaint the entire canvas per frame?

No, sometimes it's a perfectly good idea

Its performance is starting to become an issue

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?

音盲 2024-11-10 08:45:54

首先,我将从 init() 中取出所有这些:

canvas = document.createElement('canvas');
canvas.width = 400;
canvas.height = 300;
ctx = canvas.getContext('2d');
document.body.appendChild(canvas);

没有必要每毫秒都处理它!

其次,看看这个: http://paulirish.com/2011/requestanimationframe-for-智能动画/

First I'd take all this out of init():

canvas = document.createElement('canvas');
canvas.width = 400;
canvas.height = 300;
ctx = canvas.getContext('2d');
document.body.appendChild(canvas);

There's no point dealing with that every millisecond!

Secondly, look at this: http://paulirish.com/2011/requestanimationframe-for-smart-animating/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文