• <bdo id='elAQB'></bdo><ul id='elAQB'></ul>
    <i id='elAQB'><tr id='elAQB'><dt id='elAQB'><q id='elAQB'><span id='elAQB'><b id='elAQB'><form id='elAQB'><ins id='elAQB'></ins><ul id='elAQB'></ul><sub id='elAQB'></sub></form><legend id='elAQB'></legend><bdo id='elAQB'><pre id='elAQB'><center id='elAQB'></center></pre></bdo></b><th id='elAQB'></th></span></q></dt></tr></i><div id='elAQB'><tfoot id='elAQB'></tfoot><dl id='elAQB'><fieldset id='elAQB'></fieldset></dl></div>
      <legend id='elAQB'><style id='elAQB'><dir id='elAQB'><q id='elAQB'></q></dir></style></legend>

      <small id='elAQB'></small><noframes id='elAQB'>

      <tfoot id='elAQB'></tfoot>
      1. 使用 rect() 的 HTML5 Canvas 性能非常差

        HTML5 Canvas performance very poor using rect()(使用 rect() 的 HTML5 Canvas 性能非常差)

        • <small id='6687C'></small><noframes id='6687C'>

          <i id='6687C'><tr id='6687C'><dt id='6687C'><q id='6687C'><span id='6687C'><b id='6687C'><form id='6687C'><ins id='6687C'></ins><ul id='6687C'></ul><sub id='6687C'></sub></form><legend id='6687C'></legend><bdo id='6687C'><pre id='6687C'><center id='6687C'></center></pre></bdo></b><th id='6687C'></th></span></q></dt></tr></i><div id='6687C'><tfoot id='6687C'></tfoot><dl id='6687C'><fieldset id='6687C'></fieldset></dl></div>
          <legend id='6687C'><style id='6687C'><dir id='6687C'><q id='6687C'></q></dir></style></legend>

            <tfoot id='6687C'></tfoot>
                  <tbody id='6687C'></tbody>
                  <bdo id='6687C'></bdo><ul id='6687C'></ul>
                  本文介绍了使用 rect() 的 HTML5 Canvas 性能非常差的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我正在编写一个游戏,它以以下方式在屏幕顶部显示分数:

                  I'm writing a game, which displays the score at the top of the screen in the following fashion:

                      canvasContext.fillStyle = "#FCEB77";
                      canvasContext.fillText('  Score: ' + Math.floor(score) + '  Lives: ' + Math.floor(lives) + ' other info: ' + Math.floor(otherInfo));
                  

                  效果很好.然后我想做的是在该文本周围画一个框;所以我尝试了以下方法:

                  Which works fine. What I then wanted to do was to draw a box around that text; so I tried the following:

                      canvasContext.rect(2, 1, 210, 30);
                      canvasContext.rect(2, 1, 80, 30);
                      canvasContext.rect(80, 1, 70, 30);
                      canvasContext.strokeStyle = "#FCEB77";
                      canvasContext.stroke();
                  

                  当我运行游戏时,性能的影响令人难以置信.我每帧都清除整个画布,但绘制三个矩形似乎会影响性能.谁能告诉我为什么,以及如何解决这个问题?

                  And when I ran the game the impact of performance was unbelievable. I'm clearing the entire canvas each frame, but drawing three rectangles seems to kill the performance. Can anyone tell me why, and how to get around this?

                  推荐答案

                  现场演示

                  尝试添加 beginPath 方法,如下代码:

                  Try add the beginPath method, like the following code:

                  canvasContext.beginPath();
                  canvasContext.rect(2, 1, 210, 30);
                  canvasContext.rect(2, 1, 80, 30);
                  canvasContext.rect(80, 1, 70, 30);
                  canvasContext.strokeStyle = "#FCEB77";
                  canvasContext.stroke();
                  canvasContext.closePath();
                  

                  使用路径绘图时,您使用的是虚拟笔"或指针".如果没有路径,将导致画布状态机直接更改,从而使事情变慢.

                  When drawing using a path, you are using a virtual "pen" or "pointer". Without the path, will cause direct changes on canvas state machine which make things slow.

                  closePath 在这种情况下并不是真正需要的,但可以用来说明用法.

                  closePath is not really necessary in this case, but is there to illustrate the usage.

                  尝试 demo 使用和不使用 (begin/close)Path 并比较性能.我提供了一个粗略的 fps 计数器,但足以看到性能下降.

                  Try demo with and without the (begin/close)Path and compare the performance. I provided a rough fps counter but it is sufficient to see the decrease in performance.

                  您可能需要在其他浏览器(包括手机)上进行检查,因此我设置了此 JSPerf 测试.

                  You might need to check this on other browsers, including mobiles, so I set this JSPerf test.

                  这篇关于使用 rect() 的 HTML5 Canvas 性能非常差的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  How to make rooftext effect and valley text effect in HTML5 (or Fabric.js)(如何在 HTML5(或 Fabric.js)中制作屋顶文字效果和山谷文字效果)
                  Draw border around nontransparent part of image on canvas(在画布上的图像不透明部分周围绘制边框)
                  dragging and resizing an image on html5 canvas(在 html5 画布上拖动图像并调整其大小)
                  What#39;s the difference between a boolean as primitive and a boolean as property of an object?(作为原始对象的布尔值和作为对象属性的布尔值有什么区别?)
                  I want to do animation of an object along a particular path(我想沿特定路径对对象进行动画处理)
                  How to upload image into HTML5 canvas(如何将图像上传到 HTML5 画布中)

                  <small id='uZis9'></small><noframes id='uZis9'>

                  <i id='uZis9'><tr id='uZis9'><dt id='uZis9'><q id='uZis9'><span id='uZis9'><b id='uZis9'><form id='uZis9'><ins id='uZis9'></ins><ul id='uZis9'></ul><sub id='uZis9'></sub></form><legend id='uZis9'></legend><bdo id='uZis9'><pre id='uZis9'><center id='uZis9'></center></pre></bdo></b><th id='uZis9'></th></span></q></dt></tr></i><div id='uZis9'><tfoot id='uZis9'></tfoot><dl id='uZis9'><fieldset id='uZis9'></fieldset></dl></div>
                  <tfoot id='uZis9'></tfoot>
                  • <bdo id='uZis9'></bdo><ul id='uZis9'></ul>

                    <legend id='uZis9'><style id='uZis9'><dir id='uZis9'><q id='uZis9'></q></dir></style></legend>

                            <tbody id='uZis9'></tbody>