通过 CSS 与 HTML5 Canvas 在网页上移动图像的性能
我有一个图像并将其移动到我的网页(JavaScript)中,如下所示:
satelliteImage.style.top = coordinates.top + "px";
satelliteImage.style.left = coordinates.left + "px";
不幸的是,除了 Chrome 之外,所有浏览器的性能都很差。瓶颈在于渲染速度。我对IE不抱希望,但我至少想改进Firefox。与样式更改相比,有人有过移动图像时 HTML5 Canvas 性能的经验吗?
I have an image and move it around my web page (JavaScript) like this:
satelliteImage.style.top = coordinates.top + "px";
satelliteImage.style.left = coordinates.left + "px";
Unfortunately, the performance is quite bad in all browsers except Chrome. The bottleneck is rendering speed. I have no hope for IE, but I want to improve Firefox at least. Does anyone have experience with performance of HTML5 Canvas while moving an image, compared to Style change?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我创建了等效的测试来比较通过 CSS 移动图像与在 HTML 画布上绘制图像的帧速率。以下是测试:
通过 CSS 移动 20 个精灵 在1024x768 以下是 FPS 结果(有关测试详细信息,请参阅 URL):
如您所见
编辑:添加了在背景上移动 20 个小动画精灵的测试。结论保持不变。
I have created equivalent tests to compare frame rates for moving an image via CSS versus drawing it on an HTML canvas. Here are the tests:
And here are the FPS results (see URL for test details):
As you can see:
likelypossibly doing something wrong if you're only getting 5fps.Edit: Added tests for moving 20 small animated sprites over a background. The conclusions remain the same.
现在已经过去两年多了,我决定进行这些测试,看看这是否仍然成立。确实如此……但也没有。
Firefox 桌面版和移动版运行 CSS 动画的速度均明显快于画布。
Chrome 桌面版运行的画布和 CSS 动画大致相同
Chrome 移动版(在 Nexus 7 上)的做法完全相反:画布的运行速度明显快于 CSS!< /p>
(在 Nexus 7 上使用 Firefox Android 以及 Linux 上的桌面浏览器,分辨率为 1920x1080)
其他人会得到什么?有人可以测试 IE9、10 吗?
It's now been over 2 years and I decided to run these tests to see if this still holds true. It does...and it doesn't.
Firefox Desktop and mobile both run CSS animations significantly faster than canvas.
Chrome desktop runs canvas and CSS animations about the same
Chrome Mobile (on Nexus 7) does the exact opposite: canvas runs significantly faster than CSS!
(Using Firefox Android with Nexus 7 and desktop browsers on Linux with 1920x1080 resolution)
What does everyone else get? Can anyone test IE9, 10 for this?
我想我会用我在第三代 iPad 上的发现来更新这个老问题:
Canvas 以 2:1 获胜,精灵动画平均约为 120 fps,背景清除双向切换。 CSS 勉强能达到 60 fps。
对于单张图片来说,CSS 肯定更快。
Figured I'd update this old question with my findings on a 3rd generation iPad:
Canvas wins 2:1 with the sprite animations an average of about 120 fps with background clearing toggled both ways. CSS could barely meet 60 fps.
As for the single image, CSS was definitely quicker.
根据我使用 Canvas 的经验,您应该能够在 Firefox 上获得良好的 50 fps,甚至在 iOS 上获得良好的 15 fps。 IE9 可能是最快的浏览器,其他版本并没有真正实现 Canvas。
In my experience with Canvas you should be able to get a good 50 fps on Firefox and even a good 15 fps on iOS. IE9 will probably be the fastest browser, other versions don't really implement Canvas.
进一步了解 MyNameIsKo 对 iPad 3 性能的调查结果。我想知道这是否与 CSS DOM 方法必须担心在 iPad 3 的视网膜屏幕上绘制而画布将以较低的分辨率绘制然后 blt 到屏幕这一事实有关。 iPad 1 的 CSS 更新速度明显快于 iPad3!
我还对画布 JavaScript 进行了一些更改,以便能够绘制到视网膜分辨率画布。我在 canv.height = h; 之后添加了以下代码在 bg.onload 函数中:
这极大地降低了性能...
iPad 1 (iOS 5.5.1)
iPad 3 (iOS 6.1.3)
Further to MyNameIsKo findings on iPad 3 performance. I was wondering if it was to do with the fact that the CSS DOM method had to worry about drawing on the retina screen of iPad 3 whereas the canvas would be drawn to at lower resolution and then blt'd to screen. An iPad 1 is significantly faster for CSS updates than the iPad3!
I also made some changes to the canvas javascript to be able to draw to a retina resolution canvas. I added the following code after canv.height = h; in the bg.onload function:
which made a huge reduction in performance...
iPad 1 (iOS 5.5.1)
iPad 3 (iOS 6.1.3)