Workshop › Whack-a-Mole › Step 3 of 18
All the drawing goes into a draw function: first the field, then a dark brown circle for every hole.
```js
function draw() {
ctx.fillStyle = '#65a30d'
ctx.fillRect(0, 0, canvas.width, canvas.height)
for (const hole of holes) {
ctx.fillStyle = '#3f2d1d'
ctx.beginPath()
ctx.arc(hole.x, hole.y, HOLE_R, 0, Math.PI * 2)
ctx.fill()
}
}
draw()
```for (const hole of holes) repeats once for each hole in the list.beginPath starts a new shape, arc describes the circle (middle, radius, from angle
0 to a full turn Math.PI * 2), fill paints it.draw() at the end runs the function once.Wrap the two background lines in function draw() { ... }, add the holes loop inside it, and call draw() at the end.
Skills: Drawing on a canvas, Functions
← Nine holes A clock and a loop →