Bouncing balls in a box – JavaScript

bouncing balls

screen shot

As it is clear from the title, this post is about creating a box with balls bouncing after hitting the walls using JavaScript and HTML5 canvas. More precisely the box is initially empty, balls are added eventually by clicking the surface of box. Apparently i can’t embed JS in the wordpress post, so check this plunk┬áto visualize it. I will go through the code briefly here.

HTML

Create a canvas element with onclick event handler, add some styles to it.

 

JavaScript

Continue reading