how to draw grid using html5 and (canvas or svg)

后端 未结 4 1799
北恋
北恋 2020-12-12 10:00

I want to draw a grid as shown in image but I totally don\'t have idea that where should I begin. Should I use SVG or should I use Canvas w

相关标签:
4条回答
  • 2020-12-12 10:14

    it's very easy to do using canvas, that's what I recommend. I'm responding quickly on mobile here, but you should get the idea even if the psuedocode below isn't EXACTLY right:

    you'll have a loop something like:

    // "Ctx" is your canvas context
    // "Width," "Height," and other vars that start with a capital letter are set according
    //   to your canvas size or preference
    
    var i;
    for (i=0; i < Height; i += GridSize) {
       ctx.lineWidth(1.0+((i%10)==0));
       ctx.moveTo(0,i);
       ctx.lineTo(Width,i);
       ctx.stroke();
    }
    for (i=0; i < Width; i += GridSize) {
       ctx.lineWidth(1.0+((i%10)==0));
       ctx.moveTo(i,0);
       ctx.lineTo(i,Height);
       ctx.stroke();
    }
    
    0 讨论(0)
  • 2020-12-12 10:23

    SVG can do this nicely using patterns:

    <svg width="100%" height="100%" xmlns="http://www.w3.org/2000/svg">
      <defs>
        <pattern id="smallGrid" width="8" height="8" patternUnits="userSpaceOnUse">
          <path d="M 8 0 L 0 0 0 8" fill="none" stroke="gray" stroke-width="0.5"/>
        </pattern>
        <pattern id="grid" width="80" height="80" patternUnits="userSpaceOnUse">
          <rect width="80" height="80" fill="url(#smallGrid)"/>
          <path d="M 80 0 L 0 0 0 80" fill="none" stroke="gray" stroke-width="1"/>
        </pattern>
      </defs>
    
      <rect width="100%" height="100%" fill="url(#grid)" />
    </svg>
    

    I set width and height to 100%, so you can define the actual width and height on use, either for inline SVG:

    <div style="width:400px;height:300px">
      <svg width="100%" height="100%" xmlns="http://www.w3.org/2000/svg">
        <defs>
          <pattern id="smallGrid" width="8" height="8" patternUnits="userSpaceOnUse">
            <path d="M 8 0 L 0 0 0 8" fill="none" stroke="gray" stroke-width="0.5"/>
          </pattern>
          <pattern id="grid" width="80" height="80" patternUnits="userSpaceOnUse">
            <rect width="80" height="80" fill="url(#smallGrid)"/>
            <path d="M 80 0 L 0 0 0 80" fill="none" stroke="gray" stroke-width="1"/>
          </pattern>
        </defs>
    
        <rect width="100%" height="100%" fill="url(#grid)" />
      </svg>
    </div>
    

    or an <img> element:

    <img src="https://svgshare.com/i/9Eo.svg" width="700" height="200"/>
    

    results in:

    <img src="https://svgshare.com/i/9Eo.svg" width="241" height="401"/>
    

    results in

    Note that for this particular grid you have to use widths and heights of the form n x 80 + 1 (with n being any integer) if you want the grid to start and end with a thick stroke.

    0 讨论(0)
  • 2020-12-12 10:26

    In the interest of coverage, how about a CSS based approach?

    <!DOCTYPE html>
    <html>
      <head>
          <style>
          html {
            height: 100%;
          }
    
          body {
            margin: 0;
            padding: 0;
            height: 100%;
            background-color: #434343;    
            background-size: 75px 75px;
            background-image: linear-gradient(0deg, transparent 24%, rgba(255, 255, 255, .05) 25%, rgba(255, 255, 255, .05) 26%, transparent 27%, transparent 74%, rgba(255, 255, 255, .05) 75%, rgba(255, 255, 255, .05) 76%, transparent 77%, transparent), linear-gradient(90deg, transparent 24%, rgba(255, 255, 255, .05) 25%, rgba(255, 255, 255, .05) 26%, transparent 27%, transparent 74%, rgba(255, 255, 255, .05) 75%, rgba(255, 255, 255, .05) 76%, transparent 77%, transparent);
          }
    
          canvas {
              width:100%;
              height:100%;
              position:absolute;
    
              background-color: transparent;
              background-size: 15px 15px;
              background-image: linear-gradient(0deg, transparent 24%, rgba(255, 255, 255, .05) 25%, rgba(255, 255, 255, .05) 26%, transparent 27%, transparent 74%, rgba(255, 255, 255, .05) 75%, rgba(255, 255, 255, .05) 76%, transparent 77%, transparent), linear-gradient(90deg, transparent 24%, rgba(255, 255, 255, .05) 25%, rgba(255, 255, 255, .05) 26%, transparent 27%, transparent 74%, rgba(255, 255, 255, .05) 75%, rgba(255, 255, 255, .05) 76%, transparent 77%, transparent);
          }
    
          </style>
      </head>
      <body>
          <canvas></canvas>
      </body>
    </html>
    
    0 讨论(0)
  • 2020-12-12 10:35

    I am posting my code using canvas here on SO but I am also creating a working sample on JSFiddle here.

    <!DOCTYPE html>
    <html>
    <head>
        <title>StackOverflow test bed</title>
        <script type="text/javascript">
            function drawGrid() {
                var cnv = document.getElementById("cnv");
    
                var gridOptions = {
                    minorLines: {
                        separation: 5,
                        color: '#00FF00'
                    },
                    majorLines: {
                        separation: 30,
                        color: '#FF0000'
                    }
                };
    
                drawGridLines(cnv, gridOptions.minorLines);
                drawGridLines(cnv, gridOptions.majorLines);
    
                return;
            }
    
            function drawGridLines(cnv, lineOptions) {
    
    
                var iWidth = cnv.width;
                var iHeight = cnv.height;
    
                var ctx = cnv.getContext('2d');
    
                ctx.strokeStyle = lineOptions.color;
                ctx.strokeWidth = 1;
    
                ctx.beginPath();
    
                var iCount = null;
                var i = null;
                var x = null;
                var y = null;
    
                iCount = Math.floor(iWidth / lineOptions.separation);
    
                for (i = 1; i <= iCount; i++) {
                    x = (i * lineOptions.separation);
                    ctx.moveTo(x, 0);
                    ctx.lineTo(x, iHeight);
                    ctx.stroke();
                }
    
    
                iCount = Math.floor(iHeight / lineOptions.separation);
    
                for (i = 1; i <= iCount; i++) {
                    y = (i * lineOptions.separation);
                    ctx.moveTo(0, y);
                    ctx.lineTo(iWidth, y);
                    ctx.stroke();
                }
    
                ctx.closePath();
    
                return;
            }
    
        </script>
    </head>
    <body onload="drawGrid()">
        <canvas id="cnv" width="500" height="500"></canvas>
    </body>
    </html>
    

    Using the canvas approach you can make the grid size dynamic by changing the separation parameter.

    However, if your grid size is going to be static I feel that maybe you don't need to draw the grid. Just for the sake of displaying a grid to the user you could use CSS to repeat a background image as demonstrated in the fiddle here. That will also be good on page performance.

    0 讨论(0)
提交回复
热议问题