Creating 2-D Animations on Canvas - bouncing ball

In this tutorial, the following will be looked into on HTML5 canvas:

  • How to create a ball on HTML5 Canvas.
  • How to control its movement both horizontally and vertically.
  • How to stop the motion when the ball hits the floor.
Finding a good book to master HTML5 can be very challenging: there are so many around - most with eye-catching titles and very complex substance.
Therefore, Vivax Solutions strongly recommends Core HTML5 Canvas for those who really want to delve into HTML5.
Please click the image to access Amazon:

Interactive Bouncing Ball

In this example, several methods of context object are used to create a bouncing ball animation on canvas. It is fully interactive too.

The vertical movement of the ball is controlled by the value of a variable which goes down with the time. At the same time, the horizontal movement of the ball is controlled by another variable, which, increases with the time.





The Code for Interactive Bouncing Ball is as follows:

<script type="text/javascript">
var xx; var a=0; var b=10; var c=100; var x=0;
var canvas = document.getElementById('Canvas_One');
var context = canvas.getContext('2d');
resetting();
// print the title of the animation on canvas
function titling(){
context.font = '20pt Calibri';
context.fillStyle = 'green';
context.fillText('The Bouncing Ball', 100, 30);
}
// code for resetting
function resetting(){
c=100;a=0;x=0;
context.clearRect(0,0,400,300);
titling();
context.beginPath();
context.arc(80,100,20,0,2*Math.PI,false);
var grd = context.createRadialGradient(80, 100, 3, 80, 100, 18);
grd.addColorStop(0, '#8ED6FF');
grd.addColorStop(1, '#004CB3');
context.fillStyle = grd;
context.fill();
context.beginPath();
context.moveTo(0,122);
context.lineTo(100,122);
context.lineWidth=4;
context.strokeStyle="red";
context.stroke();
}
// code for dropping the ball
function dropping(){
a=a+b;
x=x+2.5;
if(a==c){b=-10;}
if(a==0){b=10;c=c-10;}
context.beginPath();
context.moveTo(0,122);
context.lineTo(100,142);
context.lineWidth=4;
context.strokeStyle="green";
context.stroke();
context.clearRect(0,0,400,300);
context.font = '20pt Calibri';
context.fillStyle = 'green';
context.fillText('The Bouncing Ball', 100, 30);
context.beginPath();
context.arc(80+x,180+(100-a),20,0,2*Math.PI,false);
var grd = context.createRadialGradient(80+x, 180+(100-a), 3, 80+x, 180+(100-a), 18);
grd.addColorStop(0, '#8ED6FF');
grd.addColorStop(1, '#004CB3');
context.fillStyle = grd;
context.fill();
if(c==20){window.clearInterval(xx);}
var aud=document.getElementById('audio1');
if(a==0 || a==10){ aud.play();}
}
// code for using the timer
function start_one(){
xx=window.setInterval('dropping()',80);
}
</script>

 

 

Resources at Fingertips

There is a significant selection of tutorials here, covering ASP.Net, HTML5, CSS3 and JavaScript. They are categorized clearly for you to access them easily on any device - desktops/laptops, smartphones and laptops.
There are quite a few of them on HTML5 Canvas and CSS3. In addition, there are tutorials on JavaScript and ASP.Net too.

Stand Out - from the crowd

students

"There's no such thing as a free lunch."

The best things in nature are free with no strings attached - fresh air, breathtakingly warm sunshine, scene of meadow on the horizon...

Vivax Solutions, while mimicking nature, offers a huge set of tutorials along with interactive tools for free.

Please use them and excel in the sphere of science education.

Everything is free; not even registration is required.

 

Recommended Reading

 

The best book to master HTML5 canvas animations. The author tells you how to manipulate the canvas element with JavaScript in a progressive way - starting from the easier to harder worked examples. It is ideal for anyone who wants to be a game developer in HTML5.

Amazon Product Search

Sponsors