Workshop › Wordle-style Word Guess › Step 19 of 26

Where the keys go

What we are doing

A phone has no keyboard for a canvas, so the game draws its own. keyRects() turns three strings (one per row, like a real keyboard) into a list of rectangles, each row centered. The same list will be used to draw the keys and to find the key under a tap.

```js
const KEY_ROWS = ['qwertyuiop', 'asdfghjkl', 'zxcvbnm']
const KEY_W = 32
const KEY_H = 44
const KEYS_TOP = 408

// The on-screen keyboard: each row is centered.
function keyRects() {
  const rects = []
  KEY_ROWS.forEach((row, r) => {
    const total = row.length * KEY_W + (row.length - 1) * 4
    let x = (canvas.width - total) / 2
    ;[...row].forEach((k) => {
      rects.push({ key: k, label: k, x, y: KEYS_TOP + r * (KEY_H + 6), w: KEY_W, h: KEY_H })
      x += KEY_W + 4
    })
  })
  return rects
}
```

What it means

Your turn

  1. Under COLORS, write the four keyboard constants.
  2. Above function draw() {, write the comment and keyRects, and leave an empty line. Press Run.

Skills: Arrays and objects

All steps

  1. The canvas and the pen
  2. Paint the background
  3. A function that draws
  4. A row of five tiles
  5. Six rows
  6. Draw again and again
  7. The typed letters
  8. Type with the keyboard
  9. Show the letters
  10. Backspace
  11. Leave shortcuts alone
  12. A secret word
  13. Right letter, right place
  14. Right letter, wrong place
  15. Enter sends the guess
  16. Colour the guesses
  17. Winning and losing
  18. A message line
  19. Where the keys go
  20. Draw the keyboard
  21. Tap a key
  22. Enter and Delete keys
  23. Keys that remember
  24. Keep statistics
  25. Show the statistics
  26. Build it yourself: only real words