Workshop › Breakout › Step 7 of 39
Draw a round ball
What we are doing
There is no "fill a circle" command. We describe a circle with arc and then fill it. And we call resetBall() once
at the start so the ball exists.
```js
ctx.fillStyle = '#f8fafc'
ctx.beginPath()
ctx.arc(ball.x, ball.y, BALL_R, 0, Math.PI * 2)
ctx.fill()
resetBall()
```
What it means
beginPath() starts a new shape; arc(x, y, r, start, end) adds a circle around (x, y); fill() paints it.
- Angles are in radians: from
0 to Math.PI * 2 is one full turn.
resetBall() above requestAnimationFrame(loop) sets the ball before the first frame is drawn.
Your turn
- In
draw, under the paddle's fillRect line, leave an empty line and write the four ball lines.
- Write
resetBall() on its own line just above the last requestAnimationFrame(loop).
Skills: Drawing on a canvas
← A ball with a velocity Move the ball →
All steps
- The field and the paddle
- Draw again and again
- Keep a number inside limits
- Follow the mouse
- A canvas shown at another size
- A ball with a velocity
- Draw a round ball
- Move the ball
- Bounce off the side walls
- Bounce off the ceiling
- The open floor
- Bounce off the paddle
- Aim with the paddle
- A row of bricks
- Draw the bricks
- A whole wall: a loop inside a loop
- A color for every row
- Circle meets box
- Break a brick
- Hitting the side of a brick
- Wait on the paddle
- Ride along with the paddle
- Launch the ball
- Click or press Space
- Remember which keys are held
- Steer with the arrow keys
- Three lives
- Lives and a hint on screen
- Game over
- Keep score
- Show the score
- Win the game
- One function for a new game
- Play again
- A burst of particles
- Break into sparks
- Move, fall and disappear
- Fade out
- Build it yourself: tough bricks