Workshop › Conway's Game of Life › Step 25 of 32

Paint or erase?

What we are doing

Next we want to drag and paint a whole line. First we remember what the press decided: painting holds the value being painted, 1 when you pressed on a dead cell and 0 (erase) when you pressed on a live one.

```js
let painting = null // while the pointer is down: the value being painted, 1 or 0

// Pressing on a dead cell paints live cells as you drag; pressing on a live one erases.
canvas.addEventListener('pointerdown', (event) => {
  const cell = cellAt(toCanvas(event))
  if (!cell) return
  painting = grid[cell.r][cell.c] ? 0 : 1
  grid[cell.r][cell.c] = painting
})
```

What it means

Your turn

  1. Under let frames write the painting line.
  2. Above the pointerdown listener write the comment; inside it, replace the last line with the two painting lines.

Skills: Game states

All steps

  1. The board
  2. Measure the grid
  3. One living cell
  4. A function that draws
  5. The world in memory
  6. Draw every living cell
  7. A random soup
  8. Count the neighbours
  9. Four rules
  10. Draw again and again
  11. Next generation with N
  12. Count the generations
  13. Show the generation
  14. A world without edges
  15. Count the living
  16. A generation every few frames
  17. Let it live
  18. Running or not
  19. One place for the buttons
  20. Space to play, N to step
  21. Clear and shuffle
  22. Where is the pointer?
  23. Which cell?
  24. Click a cell
  25. Paint or erase?
  26. Drag to paint
  27. Patterns from text
  28. The glider gun
  29. Fire the gun
  30. Buttons on the screen
  31. Tap the buttons
  32. Build it yourself: a world at rest