Workshop › Minesweeper › Step 16 of 26
Players mark cells they are sure hide a mine with a flag, using the right mouse button. A right click fires a
contextmenu event; event.preventDefault() stops the browser's menu from opening.
```js
Array.from({ length: SIZE }, (_, col) => ({ row, col, mine: false, count: 0, revealed: false, flagged: false })),
function toggleFlag(cell) {
if (cell.revealed || state === 'lost') return
cell.flagged = !cell.flagged
}
canvas.addEventListener('contextmenu', (event) => {
event.preventDefault() // no browser menu: right click places a flag
const cell = cellAt(event)
if (cell) toggleFlag(cell)
})
```cell.flagged = !cell.flagged flips the flag: !true is false, !false is true.cellAt to find the cell under the pointer.newGame, add flagged: false to the cell.function lose(), write toggleFlag.function draw(), write the contextmenu listener. Press Run.Skills: Keyboard and mouse input
← Flood fill A flag protects its cell →