简体   繁体   English

多个html5 canvas元素:性能问题?

[英]Multiple html5 canvas elements: performance issue?

Regardless of the javascript, is there a performance problem when adding multiple html5 canvas elements for the purpose of displaying canvas made icon? 无论javascript如何,为了显示画布制图标而添加多个html5画布元素是否存在性能问题? I mean is there a difference performancewise between a div and canvas sematic element? 我的意思是div和canvas sematic元素之间在性能上有区别吗?

I am trying to step away from images, svg and even fontawesome, thats why im asking. 我试图远离图像,svg甚至fontawesome,这就是为什么我要求。

Based on the request, I have this short code: 根据请求,我有这个简短的代码:

 var canvas = document.createElement("canvas"); canvas.width = 250; canvas.height = 80; var ctx = canvas.getContext("2d"); ctx.fillStyle = "#000"; ctx.fillRect(0, 0, 250, 80); //draw a red box ctx.fillStyle = "#FF0000"; ctx.font = "30px Tahoma"; ctx.fillText("Hello World", 45, 50); var dataURL = canvas.toDataURL(); document.getElementById("img-data").innerHTML = dataURL; document.getElementById("canvas-mirror").src = dataURL; 
 <div id="img-data"></div> <img id="canvas-mirror"></div> 

In short, 简而言之,

  1. Create canvas element with javascript. 使用javascript创建canvas元素。 You don't have to include it into html, why would you? 你不必将它包含在html中,为什么会这样? This way it might be a little bit faster. 这样它可能会快一点。
  2. Use canvas.getContext 2d, WebGL or whatever you want to do. 使用canvas.getContext 2d,WebGL或任何你想做的事情。 This is essential, once you choose method, you shouldnt change it! 这是必不可少的,一旦你选择方法,你不应该改变它! (Dont use it twice on same canvas.) (不要在同一个画布上使用它两次。)
  3. Draw anything you want. 画任何你想要的东西。
  4. Get data from canvas back. 从画布中获取数据。 Now the most known way is to use "high level" method toDataUrl. 现在最着名的方法是使用“高级”方法toDataUrl。 This is good and easy to do. 这很好,很容易做到。 But in case of more complicated application, you might choose also different methods, for example webgl has readPixels which is faster, like really fast, you get smaller data and you can also use scissors before, but it is also much harder to code. 但是在更复杂的应用程序的情况下,你可能也会选择不同的方法,例如webgl有readPixels更快,就像真的很快,你得到的数据更小,你也可以使用剪刀,但它也更难编码。
  5. Use data for image. 使用图像数据。 In case of base64 it is valid param for img src property. 对于base64,它是img src属性的有效参数。 WebGL readPixels must be transformed first. 必须首先转换WebGL readPixels。 It might be binary blob or base64. 它可能是二进制blob或base64。

Yes, 是,

Performance depends on the total area of all canvases, try to keep it( total area of canvas) small. 性能取决于所有画布的总面积,尽量保持它(画布的总面积)小。

If you are planning to use canvas then try to use clipping option , it will increase performance. 如果您打算使用canvas然后尝试使用裁剪选项,它将提高性能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM