html5 canvas 一般性能技巧

发布于 2024-10-19 04:59:23 字数 240 浏览 2 评论 0 原文

我正在开发一个 html5 canvas 游戏,主要针对移动设备。画布大小已调整为最大可用分辨率,因此几乎可以制作全屏游戏。

在 iPad 上,这将是 1024x786 的画布;在这样的分辨率下,我注意到帧速率显着下降。在 iPhone 上的 480x320 等较小分辨率下,游戏运行流畅!我想这是因为该设备的填充率有限。

无论如何,我想尽可能地优化。如果您能发布任何有关 html5 canvas 开发的一般性能技巧,我将不胜感激。

I am developing a game for html5 canvas while mainly targeting mobile devices. The canvas is resized to the biggest available resolution, so that it almost makes a fullscreen game.

On an ipad that would be a 1024x786 canvas; at a resolution like this I notice a significant drop in framerate. On smaller resolution like 480x320 on iphone the game runs smooth! I guess this is because the device is fillrate limited.

Anyhow I would like to optimize as much as possible. I would be very grateful if you could post any general performance tips that you have for html5 canvas development.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

绻影浮沉 2024-10-26 04:59:23

另请参阅这篇关于 html5rocks 的 canvas 性能优化 文章,其中包含 jsperf 数据,其中包括移动浏览器。

Also see this canvas performance optimization article on html5rocks, which contains jsperf data which includes mobile browsers.

玩物 2024-10-26 04:59:23

您可以阅读 制作 iPad HTML5 应用程序和让它变得非常快 作者:Thomas Fuchs

他提出的要点:

  1. 图像极大地减慢了速度 - 摆脱它们
  2. 避免 text-shadow & ; box-shadow
  3. 硬件加速是相当新的......并且有缺陷(并发动画有限
  4. 如果可能,请避免不透明度有时会干扰硬件加速渲染
  5. 使用translate3d,而不是translate后者不是硬加速

其他一些可以改进的点显着提高性能:

  • 尽可能不频繁地使用 getImageData(速度大幅下降)[2]
  • 组合多个画布,以便重新绘制更频繁更改的较小部分。

您还可以基准测试带有 Chrome/Firebug Profile 的应用程序可以向您显示哪些功能运行缓慢。

[2] http://ajaxian.com/archives/canvas-image-data-optimization-tip

You can read Making an iPad HTML5 App & making it really fast by Thomas Fuchs

The key points he make:

  1. Images slow things down immensely– get rid of them
  2. Avoid text-shadow & box-shadow
  3. Hardware-acceleration is quite new… and buggy (concurrent animations is limited)
  4. Avoid opacity if possible (sometimes interferes with hardware-accelerated rendering)
  5. Use translate3d, not translate (the latter is not hard-accelerated)

Some other points that can improve performance significantly:

  • use getImageData as infrequently as possible (major slowdown) [2]
  • combine more than one canvas in order to repaint smaller parts that are changing more frequently

You can also benchmark your app with Chrome/Firebug Profile can show you which functions are slow.

[2] http://ajaxian.com/archives/canvas-image-data-optimization-tip

命硬 2024-10-26 04:59:23

更多链接:

由于我对你们的游戏还不够熟悉,所以很难给出任何具体的提示。不过,您可能希望将渲染分割为多个层。如果那里有一些静态元素,这尤其有意义。通过这种方式,您可以避免一些清理并最终得到更好的整体代码。

A couple of more links:

It's hard to give any specific tips as I'm not familiar enough with your game. You might want to split up rendering to multiple layers, though. This makes sense particularly if you have some static elements there. This way you can avoid some clearing and end up with nicer code overall.

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