【问题标题】:Draw Grid / Table on Canvas HTML5在 Canvas HTML5 上绘制网格/表格
【发布时间】:2012-07-31 08:15:31
【问题描述】:

我一直在到处搜索,但找不到如何在 HTML5 画布上绘制网格/表格。我是 HTML5 和画布的新手。

我知道如何绘制形状,但是这个绘图网格需要很长时间才能理解。

有人可以帮我解决这个问题吗?非常感谢您的时间。

【问题讨论】:

    标签: html canvas html5-canvas


    【解决方案1】:

    答案取自这里Grid drawn using a <canvas> element looking stretched

    稍微修改了一下,希望对你有帮助

    <html>
    <head>
    <script type="text/javascript" language="javascript">
    // Box width
    var bw = 400;
    // Box height
    var bh = 400;
    // Padding
    var p = 10;
    
    var canvas = document.getElementById("canvas");
    var context = canvas.getContext("2d");
    function drawBoard(){
        for (var x = 0; x <= bw; x += 40) {
            context.moveTo(0.5 + x + p, p);
            context.lineTo(0.5 + x + p, bh + p);
        }
    
        for (var x = 0; x <= bh; x += 40) {
            context.moveTo(p, 0.5 + x + p);
            context.lineTo(bw + p, 0.5 + x + p);
        }
        context.strokeStyle = "black";
        context.stroke();
    }
    
    drawBoard();
    </script>
    </head>
    <body style=" background: lightblue;">
        <canvas id="canvas" width="420px" height="420px" style="background: #fff; margin:20px"></canvas>
    </body>
    </html>
    

    【讨论】:

    • 我将如何使用它来创建 8 x 6 表?
    【解决方案2】:

    这也可以写成:

    <html>
    <head>
    
    </head>
    <body style=" background: lightblue;">
        <canvas id="canvas" width="420px" height="420px" style="background: #fff;     magrin:20px;"></canvas>
        <script type="text/javascript" language="javascript">
        var bw = 400;
        var bh = 400;
        var p = 10;
        var cw = bw + (p*2) + 1;
        var ch = bh + (p*2) + 1;
    
        var canvas = document.getElementById("canvas");
        var context = canvas.getContext("2d");
        function drawBoard(){
            for (var x = 0; x <= bw; x += 40) {
                context.moveTo(0.5 + x + p, p);
                context.lineTo(0.5 + x + p, bh + p);
            }
    
            for (var x = 0; x <= bh; x += 40) {
                context.moveTo(p, 0.5 + x + p);
                context.lineTo(bw + p, 0.5 + x + p);
            }
    
            context.strokeStyle = "black";
            context.stroke();
        }
    
        drawBoard();
        </script>
    </body>
    </html>
    

    【讨论】:

      【解决方案3】:
      // Box width
      var bw = 270;
      // Box height
      var bh = 180;
      var canvas = document.getElementById("canvas");
      var context = canvas.getContext("2d");
      function drawBoard(){
          context.lineWidth = 10;
          context.strokeStyle = "rgb(2,7,159)";
          for (var x = 0; x < bw; x += 90) {
              for (var y = 0; y < bh; y += 90) {
                 context.strokeRect(x+10, y+10, 90, 90); 
              }
          }
      }
      drawBoard();
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-06-08
        相关资源
        最近更新 更多