As mentioned at the end of last section, we want to make the ball bounce within the canvas "walls". In order to do so, it is important to remember that the position of the ball is that of its centre, and that the part that hits a wall is its edge, located a distance equal to the radius away from the centre.


Find the bug!

With the ball moving as fast as it does, it might be a bit difficult to see what is going on. If you are curious, I suggest that you change BALL_dx and BALL_dy in the html editor to larger values and change the animation frame rate, from 1000 / 60 (i.e. 60 frames per 1000 milliseconds, or 60 frames per second) to something like 10 or even only 1 frame per second.

Do it before me!

Next, I will fix the bug, making the ball bounce "correctly".


Your notebook

Keep your personal notes here. These notes are stored by your browser on your own computer, and will be available from any page on this site. You can choose to use html syntax for formatting.


HTML Add Note


  1. Introduction
  2. Drawing a Ball
  3. Defining Ball Variables
  4. Animate!
  5. Stop!
  6. Pause
  7. Bounce!
  8. Bouncing correctly
  9. Adding a paddle