通过 CSS 与 HTML5 Canvas 在网页上移动图像的性能

发布于 2024-10-14 20:57:13 字数 282 浏览 2 评论 0原文

我有一个图像并将其移动到我的网页(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 技术交流群。

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

发布评论

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

评论(5

╭⌒浅淡时光〆 2024-10-21 20:57:13

我创建了等效的测试来比较通过 CSS 移动图像与在 HTML 画布上绘制图像的帧速率。以下是测试:

通过 CSS 移动 20 个精灵 在1024x768 以下是 FPS 结果(有关测试详细信息,请参阅 URL):

                  Image  Image  Sprite  Sprite
        Browser  Canvas    CSS  Canvas     CSS
----------------------------------------------
  Safari v5.0.3      59     95      59      89
Firefox v3.6.13      59     95      60      90
 Firefox v4.0b8      75     89      78      82
    Chrome v8.0     108    230     120     204
    iPad, Horiz      17     44       2      14
     iPad, Vert       4     75       2      15

如您所见

  1. :将图像作为 HTML 元素移动总是比重新绘制画布的一部分获得更好的结果,并且
  2. 如果您只得到5 帧/秒。

编辑:添加了在背景上移动 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):

                  Image  Image  Sprite  Sprite
        Browser  Canvas    CSS  Canvas     CSS
----------------------------------------------
  Safari v5.0.3      59     95      59      89
Firefox v3.6.13      59     95      60      90
 Firefox v4.0b8      75     89      78      82
    Chrome v8.0     108    230     120     204
    iPad, Horiz      17     44       2      14
     iPad, Vert       4     75       2      15

As you can see:

  1. You're always going to get better results moving an image as an HTML element than redrawing a portion of the canvas, and
  2. You're likely possibly 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.

跨年 2024-10-21 20:57:13

现在已经过去两年多了,我决定进行这些测试,看看这是否仍然成立。确实如此……但也没有。

  1. Firefox 桌面版和移动版运行 CSS 动画的速度均明显快于画布。

  2. Chrome 桌面版运行的画布和 CSS 动画大致相同

  3. Chrome 移动版(在 Nexus 7 上)的做法完全相反:画布的运行速度明显快于 CSS!< /p>

(在 Nexus 7 上使用 Firefox Android 以及 Linux 上的桌面浏览器,分辨率为 1920x1080)

Browser/OS          Canvas Image   CSS IMage   Canvas Sprites   CSS Sprites    
-----------         ------------   ----------  --------------   -----------
Firefox 16          56.7fps        215.6 fps   59.2fps          203.6fps
Firefox 16 Android  17.1 fps       179.6fps    11.5fps          35.7
Chrome 22           192.3fps       223.5fps    170.1fps         164.3fps
Chrome Android      48.3fps        39.9fps     92.8fps          13.1fps

其他人会得到什么?有人可以测试 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.

  1. Firefox Desktop and mobile both run CSS animations significantly faster than canvas.

  2. Chrome desktop runs canvas and CSS animations about the same

  3. 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)

Browser/OS          Canvas Image   CSS IMage   Canvas Sprites   CSS Sprites    
-----------         ------------   ----------  --------------   -----------
Firefox 16          56.7fps        215.6 fps   59.2fps          203.6fps
Firefox 16 Android  17.1 fps       179.6fps    11.5fps          35.7
Chrome 22           192.3fps       223.5fps    170.1fps         164.3fps
Chrome Android      48.3fps        39.9fps     92.8fps          13.1fps

What does everyone else get? Can anyone test IE9, 10 for this?

枯叶蝶 2024-10-21 20:57:13

我想我会用我在第三代 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.

她比我温柔 2024-10-21 20:57:13

根据我使用 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.

微暖i 2024-10-21 20:57:13

进一步了解 MyNameIsKo 对 iPad 3 性能的调查结果。我想知道这是否与 CSS DOM 方法必须担心在 iPad 3 的视网膜屏幕上绘制而画布将以较低的分辨率绘制然后 blt 到屏幕这一事实有关。 iPad 1 的 CSS 更新速度明显快于 iPad3!

我还对画布 JavaScript 进行了一些更改,以便能够绘制到视网膜分辨率画布。我在 canv.height = h; 之后添加了以下代码在 bg.onload 函数中:

if (window.devicePixelRatio) {
    ctx.canvas.style.width = w + "px";
    ctx.canvas.style.height = h + "px";
    ctx.canvas.height = h * window.devicePixelRatio;
    ctx.canvas.width = w * window.devicePixelRatio;
    ctx.scale(window.devicePixelRatio, window.devicePixelRatio);
}

这极大地降低了性能...

iPad 1 (iOS 5.5.1)

iPad 3 (iOS 6.1.3)

                      CSS Sprite        Canvas Sprites
-----------------------------------------------------
iPad 1                   90                  100
iPad 3                   55                  120
iPad 1(canvas changes)   n/a                 100
iPad 3(canvas changes)   n/a                 35

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:

if (window.devicePixelRatio) {
    ctx.canvas.style.width = w + "px";
    ctx.canvas.style.height = h + "px";
    ctx.canvas.height = h * window.devicePixelRatio;
    ctx.canvas.width = w * window.devicePixelRatio;
    ctx.scale(window.devicePixelRatio, window.devicePixelRatio);
}

which made a huge reduction in performance...

iPad 1 (iOS 5.5.1)

iPad 3 (iOS 6.1.3)

                      CSS Sprite        Canvas Sprites
-----------------------------------------------------
iPad 1                   90                  100
iPad 3                   55                  120
iPad 1(canvas changes)   n/a                 100
iPad 3(canvas changes)   n/a                 35
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文