Workshop › Rhythm Lanes › Step 5 of 35

A function that draws

What we are doing

The notes will move, so the picture must be drawn again many times a second. We pack the drawing into a function named draw, so one word repaints everything.

```js
function draw() {
  ctx.fillStyle = '#0f172a'
  ctx.fillRect(0, 0, canvas.width, canvas.height)
  for (let lane = 0; lane < LANES; lane++) {
    const x = LEFT + lane * LANE_W
    ctx.fillStyle = '#1e293b'
    ctx.fillRect(x + 2, 0, LANE_W - 4, canvas.height)
    ctx.fillStyle = COLORS[lane]
    ctx.fillRect(x + 6, HIT_Y - 4, LANE_W - 12, 8)
  }
}

draw()
```

What it means

Your turn

Wrap the background lines and the loop in function draw() { ... } (two more spaces of indent) and call draw() after it.

Skills: Functions

All steps

  1. A stage for the notes
  2. The first lane
  3. Four lanes with a loop
  4. The hit line
  5. A function that draws
  6. The song as text
  7. From text to notes
  8. The clock decides where
  9. Draw the notes
  10. A clock that ticks
  11. The game loop
  12. Lanes that light up
  13. Keys for the lanes
  14. Show the keys
  15. Hit the closest note
  16. Close enough in time
  17. One press, one hit
  18. A message on the screen
  19. Say Hit
  20. Let the message go
  21. Perfect or good
  22. Keep count
  23. Missed notes
  24. Score
  25. Hits in a row
  26. The multiplier
  27. The song ends
  28. Stop the clock
  29. Accuracy
  30. The grade
  31. The results
  32. Play again
  33. Best score
  34. Lanes you can tap
  35. Build it yourself: a speed setting