Html5 Book

HTML5, the latest 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 Oscillating Spring

The spring is produced by manipulating Math.sin method of Math object.

The sphere is produced by context.arc method; the radial gradient produces the spherical appearance of the circle.

The speed is controlled by giving three different time intervals to window.setInterval() method.



Please choose the speed, and then click the Run button.

Slow

Medium

Fast

Make sure you stop the animation before choosing the speed to see a smooth effect.


     





<!--hidden element to store the values for animation--------------->
<input type="hidden" id="h" value="0" />
<canvas id="Canvas_Two" width="400" height="300" style="border:2px solid red;" class="imgforcontent">
</canvas>
<!----radio buttons for speed control---------------------------->
<form name="spring" action="">
<input id="R4" name="speed" value="Slow" type="radio" />Slow<br /><br />
<input id="R5" name="speed" value="Medium" type="radio" />Medium<br /><br />
<input id="R6" name="speed" value="Fast" type="radio" />Fast<br /><br />
</form>
<!---------------buttons to start and stop the animation------------------------>
<div style="width:400px;">
<input type="button" style="float:left;margin-left:10px;" value="Run" onclick="anim1()" id="Animation" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<input type="button" style="float:right;margin-right:10px;" value="Stop" onclick="window.clearInterval(xx)" id="Stopping" >
</div>
<canvas id="Canvas_Two" width="400" height="300" style="border:5px solid red"></canvas> <script> var canvas = document.getElementById('Canvas_Two');
var context1 = canvas.getContext('2d');
//code for static spring in grey colour
context1.beginPath();
context1.moveTo(200,0);
for(var i=0;i<=220;i++)
{
context1.lineTo(200 +20*Math.sin(5*i*Math.PI/180),i*0.5);
context1.lineWidth=5;
context1.strokeStyle='grey';
context1.stroke();
}
// code for the ball in blue colour
context1.beginPath();
context1.arc(200,100,35,0,2*Math.PI,false);
var grd = context1.createRadialGradient(200, 100, 5, 200, 100, 30);
// light blue
grd.addColorStop(0, '#8ED6FF');
// dark blue
grd.addColorStop(1, '#004CB3');
context1.fillStyle=grd;
context1.fill();
var a=1;
//code for the motion of the spring and the ball
function move(){
var x=eval(document.getElementById('h').value);
x=x+a
if (x==8){a=-1;}
if (x==4){a=1;}
context1.clearRect(0,0,400,300);
context1.beginPath();
context1.moveTo(200,0);
for(var i=0;i<=220;i++)
{
context1.lineTo(200 +20*Math.sin(5*i*Math.PI/180),i*x*0.1);
context1.lineWidth=5;
context1.strokeStyle='grey';
context1.stroke();
}
context1.beginPath();
context1.arc(200,x*20+40,35,0,2*Math.PI,false);
var grd = context1.createRadialGradient(200, x*20+40, 5, 200, x*20+40, 30);
// light blue
grd.addColorStop(0, '#8ED6FF');
// dark blue
grd.addColorStop(1, '#004CB3');
context1.fillStyle=grd;
context1.fill();
document.getElementById('h').value=x;
}
// code for timing
function anim1() {
if
(document.getElementById('R4').checked)
{ sp = 400; }
else if (document.getElementById('R5').checked) { sp = 300; }
else if (document.getElementById('R6').checked) { sp = 200; }
else
{ sp = 500; }
xx = window.setInterval('move()', sp);
}
// code for stopping
function stop1() {
//empty the hidden element on exit
document.getElementById('h2').value = 4;
window.clearInterval(xx);
}
</script>




<-Go to Seventh Lesson Go to Ninth Lesson->