Workshop › Typing Rain › Step 8 of 33
spawn adds a random word from the WORDS list at the top. Its x is random too, but not so far right that the word
hangs off the edge: the canvas can measure how wide a text will be.
```js
const WORDS = [
'cat', 'sun', 'code', 'game', 'jump', 'fast', 'loop', 'byte', 'star', 'tree', 'rain', 'blue', 'fire', 'wind', 'moon',
'array', 'pixel', 'mouse', 'score', 'level', 'robot', 'light', 'music', 'space', 'river', 'green', 'cloud', 'train',
'planet', 'rocket', 'string', 'number', 'button', 'screen', 'window', 'random', 'object', 'player', 'dragon', 'puzzle',
'keyboard', 'function', 'variable', 'computer', 'triangle', 'elephant', 'mountain', 'sandwich',
]
function spawn() {
const text = WORDS[Math.floor(Math.random() * WORDS.length)]
ctx.font = FONT
const width = ctx.measureText(text).width
words.push({ text, x: 10 + Math.random() * (canvas.width - 20 - width), y: 30 })
}
```Math.random() is a random number from 0 up to 1; times WORDS.length, rounded down by Math.floor, it is a
random position in the list.ctx.measureText(text).width is the text's width in pixels with the current font.x goes from 10 to canvas.width - 10 - width, so the word always fits. push adds the new word to the list.{ text, ... } is short for { text: text, ... }.const ctx = ... and its empty line, above GROUND, write WORDS.function reset() { write spawn, with an empty line after it.Skills: Arrays and objects