Workshop › Hangman › Step 33 of 36
Center the last row
What we are doing
The last row has only 8 keys. Each row now works out its own left edge from how many keys it has, so every row is
centered.
```js
// The on-screen keyboard: rows of 9 keys, centered.
function keyRect(i) {
const row = Math.floor(i / 9)
const inRow = row === 2 ? 8 : 9
const left = (canvas.width - inRow * (KEY_W + 4) + 4) / 2
return { x: left + (i % 9) * (KEY_W + 4), y: KEYS_Y + row * (KEY_H + 6) }
}
```
What it means
inRow is how many keys this row has: 8 in the third row, 9 otherwise.
- The keys and the gaps between them take
inRow * 52 - 4 pixels. What is left of the 480, halved, is the left edge.
- For 9 keys that is 8, as before; for 8 keys it is 34.
Your turn
In keyRect, write the comment above it, add the inRow and left lines under row, and change 8 + to left +.
Skills: Drawing on a canvas
← Draw the keys Keys that remember →
All steps
- A list of secret words
- The canvas and the pen
- Paint the paper
- A function that draws
- Pick a random word
- Write the word
- Hide it behind blanks
- Remember the guesses
- Draw again and again
- A guess
- Guess with the keyboard
- Only letters count
- Count the misses
- The same letter twice is free
- Six misses at most
- Show the misses
- List the wrong letters
- Draw a line
- The gallows
- The first miss: a head
- Body, arms and legs
- The parts in a list
- Losing
- Winning
- Show the word you missed
- The end message
- Play again
- Never the same word twice
- A winning streak
- Remember the best streak
- Where each key goes
- Draw the keys
- Center the last row
- Keys that remember
- Tap a key
- Build it yourself: a hint button