Html5 Book

HTML5, the lates version of HTML, the formatting language that produces web pages - including the one you read this on, of course - has taken the web development community by storm.

On one hand, it is easy to learn; On the other hand, it has the potential to evolve and then pose a serious threat to Flash, which is more or less synonymous with impressive animations.

HTML5 has come about thanks to the cooperation between the World Wide Web Consortium (W3C) and the Web Hypertext Application Technology Working Group (WHATWG), in their collective pursuit to make the web a true treasure trove for mankind.

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:



Creating 2-D Animations on Canvas

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.

     





<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>




<-Go to Fifth Lesson Go to Seventh Lesson->