Workshop › Tic-tac-toe › Step 16 of 33
The canvas is 300 pixels inside, but the page may show it bigger or smaller (on a phone, or in a wide panel). We
scale the click by canvas.width / rect.width so it always lands in the right cell.
```js
// The canvas may be displayed at a different size than its 300×300 pixels, so scale the click.
const rect = canvas.getBoundingClientRect()
const x = (event.clientX - rect.left) * (canvas.width / rect.width)
const y = (event.clientY - rect.top) * (canvas.height / rect.height)
```rect.width is the size the canvas is shown at; canvas.width is its real size (300).300 / 600 is 0.5: a click 400 pixels in becomes 200, the right cell.Change the x and y lines as shown and add the comment line above const rect. Press Run.
Skills: Keyboard and mouse input
← Which cell was clicked? The eight winning lines →