<bdo id='toJ0Y'></bdo><ul id='toJ0Y'></ul>

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

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

  • <tfoot id='toJ0Y'></tfoot>

        <legend id='toJ0Y'><style id='toJ0Y'><dir id='toJ0Y'><q id='toJ0Y'></q></dir></style></legend>
      1. HTML5、JavaScript 和在画布中绘制多个矩形

        HTML5, JavaScript and drawing multiple rectangles in a canvas(HTML5、JavaScript 和在画布中绘制多个矩形)
        <legend id='JsuDC'><style id='JsuDC'><dir id='JsuDC'><q id='JsuDC'></q></dir></style></legend>

        <tfoot id='JsuDC'></tfoot>

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

              <tbody id='JsuDC'></tbody>

              • <i id='JsuDC'><tr id='JsuDC'><dt id='JsuDC'><q id='JsuDC'><span id='JsuDC'><b id='JsuDC'><form id='JsuDC'><ins id='JsuDC'></ins><ul id='JsuDC'></ul><sub id='JsuDC'></sub></form><legend id='JsuDC'></legend><bdo id='JsuDC'><pre id='JsuDC'><center id='JsuDC'></center></pre></bdo></b><th id='JsuDC'></th></span></q></dt></tr></i><div id='JsuDC'><tfoot id='JsuDC'></tfoot><dl id='JsuDC'><fieldset id='JsuDC'></fieldset></dl></div>
                  <bdo id='JsuDC'></bdo><ul id='JsuDC'></ul>
                  本文介绍了HTML5、JavaScript 和在画布中绘制多个矩形的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  为什么我的多个矩形不能在画布中绘制?

                  Why won't my multiple rectangles draw in the canvas?

                  <!DOCTYPE html>
                  <html xmlns="http://www.w3.org/1999/xhtml">
                    <head>
                      <title></title>
                      <script src="Scripts/jquery-1.9.1.min.js"></script>
                    </head>
                    <body>
                      <canvas id="myCanvas" width="500" height="500" style="border:1px solid red">
                        <p>Your browser doesn't support canvas.</p>
                      </canvas>
                    </body>
                  </html>
                  
                  <script type ="text/javascript">  
                    function Shape(x, y, w, h, fill) {
                      this.x = x;
                      this.y = y;
                      this.w = w;
                      this.h = h;
                      this.fill = fill;
                    }
                  
                    // get canvas element.
                    var elem = document.getElementById('myCanvas');
                  
                    // check if context exist
                    if (elem.getContext) {
                  
                      var myRect = [];
                  
                      myRect.push(new Shape(10, 0, 25, 25, "#333"))
                      myRect.push(new Shape(0, 40, 39, 25, "#333"))
                      myRect.push(new Shape(0, 80, 100, 25, "#333"))
                  
                      context = elem.getContext('2d');
                      for (i in myRect) {
                  
                        //console.log(x);
                  
                        context.fillRect(i.x, i.y, i.w, i.h)
                      }
                      //// x, y, width, height
                      //context.fillRect(0, 0, 50, 50);
                  
                      //// x, y, width, height      
                      //context.fillRect(75, 0, 50, 50);
                    }
                  </script>  
                  

                  感谢您的帮助.

                  推荐答案

                  好的,你就快到了.当您遍历矩形数组时,您是在遍历数组 key 而不是对象本身(请参阅 如何以对象为成员循环遍历纯 JavaScript 对象?).另外,正如@jimjimmy1995 指出的那样,您需要使用 .fillStyle 设置填充.fillRect 没有填充参数.

                  Ok, so you were almost there. When you iterate round your array of rectangles, you are iterating over the array key not the objects themselves (see How to Loop through plain JavaScript object with objects as members?). Plus, as @jimjimmy1995 pointed out, you need to set the fill using .fillStyle as .fillRect does not have a fill parameter.

                  此代码将起作用:

                  function Shape(x, y, w, h, fill) {
                      this.x = x;
                      this.y = y;
                      this.w = w;
                      this.h = h;
                      this.fill = fill;
                  }
                  
                  // get canvas element.
                  var elem = document.getElementById('myCanvas');
                  
                  // check if context exist
                  if (elem.getContext) {
                  
                      var myRect = [];
                  
                      myRect.push(new Shape(10, 0, 25, 25, "#333"));
                      myRect.push(new Shape(0, 40, 39, 25, "#333"));
                      myRect.push(new Shape(0, 80, 100, 25, "#333"));
                  
                      context = elem.getContext('2d');
                      for (var i in myRect) {
                          oRec = myRect[i];
                          context.fillStyle = oRec.fill;
                          context.fillRect(oRec.x, oRec.y, oRec.w, oRec.h);
                  
                      }
                  
                  }
                  

                  这篇关于HTML5、JavaScript 和在画布中绘制多个矩形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                  相关文档推荐

                  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 画布中)
                  • <bdo id='9RCPp'></bdo><ul id='9RCPp'></ul>

                      <small id='9RCPp'></small><noframes id='9RCPp'>

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

                        <legend id='9RCPp'><style id='9RCPp'><dir id='9RCPp'><q id='9RCPp'></q></dir></style></legend>