Workshop › Memory › Step 14 of 25

Click to turn a card

What we are doing

On a click, we turn the click into canvas pixels, find the card there, and turn it face up. The loop draws the change.

```js
canvas.addEventListener('click', (event) => {
  // The canvas may be displayed at a different size than its own 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)
  const card = cardAt(x, y)
  if (card) card.faceUp = true
})
```

What it means

Your turn

Above function draw, write the click listener, then an empty line. Run and click a few cards.

Skills: Keyboard and mouse input

All steps

  1. Get the canvas and the pen
  2. The table
  3. One card
  4. A row of cards
  5. Four rows
  6. Every fruit twice
  7. Cards that know themselves
  8. A card's corner
  9. Draw the cards from the list
  10. Draw again and again
  11. The face of a card
  12. A fair shuffle
  13. Which card is at a point?
  14. Click to turn a card
  15. One place for the rules
  16. Two cards at a time
  17. Pair or no pair
  18. Count the moves
  19. Show moves and pairs
  20. All pairs found
  21. Play again
  22. A number that follows
  23. Swap sides halfway
  24. Animate the turn
  25. Build it yourself: your best game