背景画布与常规画布的性能
不久前,webkit(以及 Safari)开始支持元素的 CSS canvas-backgrounds(来源:http://www.webkit.org/blog/176/css-canvas-drawing/)。
这可以极大地简化游戏和多媒体的创建,因为您不需要将画布标签注入 DIV(例如),而只需直接挂接到 DIV 的背景即可。也许是这样的:
<div id="gameview"
style="background: -webkit-canvas(myscreen); width: 320px; height: 480px;">
</div>
<script>
var target = document.getElementById("gameview");
var wd = target.clientWidth;
var hd = target.clientHeight;
var context = document.getCSSCanvasContext("2d", "myscreen", wd, hd);
/* draw stuff here */
</script>
我想知道,这是否涉及任何速度处罚?理论上,我认为绘制到背景画布应该比绘制到画布标签更快,特别是如果目标元素为空。
有人测试过它用于高速演示或游戏吗?
A while back webkit (and thus Safari) began to support CSS canvas-backgrounds for elements (Source: http://www.webkit.org/blog/176/css-canvas-drawing/).
This could greatly simplify the creation of games and multimedia, in that you dont need to inject a canvas-tag into a DIV (for instance), but simply hook into the background of the DIV directly. Something like this perhaps:
<div id="gameview"
style="background: -webkit-canvas(myscreen); width: 320px; height: 480px;">
</div>
<script>
var target = document.getElementById("gameview");
var wd = target.clientWidth;
var hd = target.clientHeight;
var context = document.getCSSCanvasContext("2d", "myscreen", wd, hd);
/* draw stuff here */
</script>
I was wondering, are there any speed penalties involved in this? In theory i think drawing to a background canvas should be faster than drawing to a canvas tag, especially if the target element is empty.
Have anyone tested this for high-speed demos or games?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
根据我的测试(也运行以相反的顺序),原始
canvas
元素比背景canvas
稍稍慢,但始终慢。Chromium 17 绘制棋盘 10000 次:
canvas
元素上约 520 毫秒Safari 5 显示类似动力学。
尝试将迭代次数设置为100000,结果应该与上面一致。
半年后更新
我们在一个项目中尝试了背景画布的方式(作为小幅优化的尝试),结果与我们的预期截然相反。整个事情(两层:一层 - 带有背景画布的
div
,另一层 - 常规画布)变得稍微变慢。事实上,当我们引入背景画布时,应用程序变得非常慢。在 Chrome 21 中进行了测试。我绝对不会保证背景画布在所有情况下都更快。
According to my tests (also run in reversed order), original
canvas
element is slightly but consistently slower than the backgroundcanvas
.Chromium 17 draws a chess-board 10000 times in:
canvas
elementSafari 5 shows similar dynamics.
Try setting the number of iterations to 100000, results should be consistent with the above.
Update half a year later
We tried the background canvas approach in one project (as an attempt for a minor optimization), and the results were dramatically opposite to our expectations. The whole thing (two layers: one – a
div
with background canvas, the other – a regular canvas) became marginally slower. In fact, when we introduced the background canvas, the app became slow as hell. Tested in Chrome 21.I definitely would not vouch for the background canvas to be faster in all situations.
在我在 Linux debian 中对 chrome 进行的测试中,与背景画布相比,常规似乎表现不佳,这是使用的代码(也添加到 http:// /jsfiddle.net/hDPVr/ )
所以我为测试所做的唯一事情是 fillRect,但在某些情况下它仍然至少好 10%
Regular seems to underperform compared to background canvas in my tests on chrome in linux debian, heres the code used ( also added to http://jsfiddle.net/hDPVr/ )
So the only thing that I did for testing is fillRect, but it's still at least 10% better in some cases