Workshop › Minesweeper › Step 3 of 26
Every cell is a 40-pixel grey square, below a 40-pixel strip at the top that will hold the counter and the timer.
grid.flat() gives all 81 cells in one list, and a for ... of loop draws each.
```js
const CELL = 40
const TOP = 40 // room for the mine counter and the timer
function draw() {
ctx.fillStyle = '#1e293b'
ctx.fillRect(0, 0, canvas.width, canvas.height)
for (const cell of grid.flat()) {
const x = cell.col * CELL
const y = TOP + cell.row * CELL
ctx.fillStyle = '#94a3b8'
ctx.fillRect(x + 1, y + 1, CELL - 2, CELL - 2)
}
}
newGame()
draw()
```fillRect(x, y, w, h) fills a rectangle; (0, 0) is the top-left corner.grid.flat() turns the 2D array into one list of 81 cells; for (const cell of list) runs once per cell.col * CELL from the left and TOP + row * CELL from the top. Drawing it 1 pixel in on every
side (CELL - 2 wide) leaves thin lines between cells.const SIZE = 9, write CELL and TOP.newGame() at the bottom, write draw and leave an empty line. Under newGame() write draw().Skills: Drawing on a canvas, Loops
← The canvas and the pen Draw every frame →