title: Honeycomb hexagonal grid?
body: <p>I'm trying to draw a hexagonal grid in a honeycomb shape.
  So far I'm able to draw it in a rectangular shape but I don't know how to convert my for loop to make a honeycomb shape instead.</p>
  <p>This is what I currently have</p>
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;html&gt;
  
  &lt;body&gt;
      &lt;canvas width='1080' height='720' id='hexmap'&gt;&lt;/canvas&gt;
  &lt;/body&gt;
  &lt;script&gt;
  
      window.addEventListener('DOMContentLoaded', (event) =&gt; {
  
          var canvas = document.getElementById('hexmap');
  
      var hexHeight,
          hexRadius,
          hexRectangleHeight,
          hexRectangleWidth,
          hexagonAngle = 0.523598776, // 30 degrees in radians
          sideLength = 36,
          boardWidth = 10,
          boardHeight = 10;
  
      hexHeight = Math.sin(hexagonAngle) * sideLength;
      hexRadius = Math.cos(hexagonAngle) * sideLength;
      hexRectangleHeight = sideLength + 2 * hexHeight;
      hexRectangleWidth = 2 * hexRadius;
      var ctx = canvas.getContext('2d');
  
          ctx.fillStyle = "#000000";
          ctx.strokeStyle = "#CCCCCC";
          ctx.lineWidth = 1;
  
      drawBoard(ctx, boardWidth, boardHeight);
      
      function drawBoard(canvasContext, width, height) {
  
          var i,j;
          //this loop generates a rectangular hexagon grid
          for(i = 0; i &lt; width; ++i) {
              for(j = 0; j &lt; height; ++j) {
                  drawHexagon(
                      ctx, 
                      i * hexRectangleWidth + ((j % 2) * hexRadius), 
                      j * (sideLength + hexHeight), 
                      false
                  );
              }
          }
      }
  
      function drawHexagon(canvasContext, x, y, fill) {           
          var fill = fill || false;
  
          canvasContext.beginPath();
          canvasContext.moveTo(x + hexRadius, y);
          canvasContext.lineTo(x + hexRectangleWidth, y + hexHeight);
          canvasContext.lineTo(x + hexRectangleWidth, y + hexHeight + sideLength);
          canvasContext.lineTo(x + hexRadius, y + hexRectangleHeight);
          canvasContext.lineTo(x, y + sideLength + hexHeight);
          canvasContext.lineTo(x, y + hexHeight);
          canvasContext.closePath();
  
          if(fill) {
              canvasContext.fill();
          } else {
              canvasContext.stroke();
          }
      }
  
  })
  &lt;/script&gt;
  
  &lt;/html&gt;</code></pre>
  </div>
  </div>
  
  Which results in this shape
  <a href="https://i.stack.imgur.com/SPTsb.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/SPTsb.png" alt="enter image description here" /></a></p>
  <p>What I'd like to achieve though is a shape like this
  <a href="https://i.stack.imgur.com/cfd49.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/cfd49.png" alt="enter image description here" /></a></p>
  <p>I was able to do it using like 13 separate for loops, shifting the hexagon over manually each time but it wasn't very practical nor automated.</p>