title: Get cursor location of a rectangle inside a canvas
body: <p>i have a canvas, inside of which i have a board/grid. When a user highlights their mouse over an intersection of the grid, i want it to show where their game peice will go. This worked perfectly fine when the board was the exact size of the canvas. I made it abit smaller by x all the way round.</p>
  
  <p>So as you can see in the picture below, the green shows the canvas and the grid is the board. I put my cursor at the very bottom right corner of the green to show when it triggers. The only one that works fine is the middle one because regardless how big i make the board, the middle will always be the middle.</p>
  
  <p>Any easy fix would just be to make the area with the mouseover event, the dimensions of the board instead of the canvas but the event listener is on the canvas. My code is below the image</p>
  
  <p><a href="https://i.stack.imgur.com/6ozOt.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/6ozOt.png" alt="enter image description here"></a></p>
  
  <p>Variables:</p>
  
  <pre><code>var canvas = document.getElementById("game-canvas");
  var context = canvas.getContext("2d");
  
  var boardSize = 13;
  var border = canvas.width / 20;
  var boardWidth = canvas.width - (border * 2);
  var boardHeight = canvas.height - (border * 2);
  
  var cellWidth = boardWidth / (boardSize - 1);
  var cellHeight = boardHeight / (boardSize - 1);
  
  var lastX;
  var lastY;
  </code></pre>
  
  <p>Mouse over event: </p>
  
  <pre><code>canvas.addEventListener('mousemove', function(evt) 
  {
      var position = getGridPoint(evt);
  
      if ((position.x != lastX) || (position.y != lastY))
      {
          placeStone((position.x * cellWidth) + border, (position.y * cellWidth) + border, 'rgba(0, 0, 0, 0.2)');         
      }
  
      lastX = position.x;
      lastY = position.y;     
  });
  </code></pre>
  
  <p>Gets the point on the grid and converts that into a number 0 - 13 (in this case)</p>
  
  <pre><code>function getGridPoint(evt)
  {
      var rect = canvas.getBoundingClientRect();
  
      var x = Math.round((evt.clientX-rect.left)/(rect.right-rect.left)*boardWidth);
      var y = Math.round((evt.clientY-rect.top)/(rect.bottom-rect.top)*boardHeight);
  
      var roundX = Math.round(x / cellWidth);
      var roundY = Math.round(y / cellHeight);
  
      return {
        x: roundX,
        y: roundY
      };
  }
  </code></pre>
  
  <p>And finally draws the piece on the board:</p>
  
  <pre><code>function placeStone(x, y, color)
  {
      var radius = cellWidth / 2;
  
      context.beginPath();
      context.arc(x, y, radius, 0, 2 * Math.PI, false);
      context.fillStyle = color;  
      context.fill();
      context.lineWidth = 5;
  }
  </code></pre>
  
  <p>I left a couple bits out like how the grid refreshs so its not a string of circles following your mouse and stuff, to keep it as short as i can, im hoping its just a simple asnwer and nobody needs to recreate it but if you do i can include the function that refreshes the grid and draws everything. Thankyou for any advice</p>