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.

In this tutorial, you will learn:

  • How to draw a simple pendulum on HTML5 Canvas.
  • How to animate it using Window.setInterval function.
  • How to change the speed of oscillation.

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

The string of the pendulum is drawn as a straight line. Its bottom is changing while using a sine values of an angle so that it gets the oscillating effect.

The pendulum bob is created as a circle, filled with a gradient to give it the appearance of a sphere.

The speed is controlled by window.setInterval() method while passing on three different intervals as arguments.

The canvas is cleared up after each interval by context.clearRect() method to give the animated effect.



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_One" width="400" height="300" style="border:2px solid red;" class="imgforcontent">
</canvas>
<!----radio buttons for speed control---------------------------->
<form name="pendulum" action="">
<input id="R1" name="speed" value="Slow" type="radio" />Slow<br /><br />
<input id="R2" name="speed" value="Medium" type="radio" />Medium<br /><br />
<input id="R3" 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="anim()" id="Animation" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<input type="button" style="float:right;margin-right:10px;" value="Stop" onclick="window.clearInterval(x)" id="Stopping" >
</div>
<script type="text/javascript">
var canvas = document.getElementById('Canvas_One');
var context = canvas.getContext('2d');
var i;
var x;
var sp;
//code for static string - grey colour
context.beginPath();
context.moveTo(200, 0);
context.lineTo(200, 135);
context.lineWidth = 5;
context.strokeStyle = "grey";
context.stroke();
// code for the static ball - gradient blue
context.beginPath();
context.arc(200, 150, 20, 0, 2 * Math.PI, false);
var grd = context.createRadialGradient(200, 150, 2, 200, 150, 18);
// light blue
grd.addColorStop(0, '#8ED6FF');
// dark blue
grd.addColorStop(1, '#0000ff');
context.fillStyle = grd;
context.fill();
// function for the animation
function circ() {
context.clearRect(0, 0, canvas.width, canvas.height);
i = eval(document.getElementById('h').value) + 10;
context.beginPath();
context.moveTo(200, 0); context.lineTo(200 + 40 * Math.sin(i * Math.PI / 180), 135);
context.lineWidth = 5;
context.strokeStyle = "grey";
context.stroke();
context.beginPath();
context.arc(200 + 40 * Math.sin(i * Math.PI / 180), 150, 20, 0, 2 * Math.PI, false);
if (i == 360 || i > 360) { i = 0; }
//alert(i);
//alert(i);
document.getElementById('h').value = i;
//create radial gradient
var grd = context.createRadialGradient(200 + 40 * Math.sin(i * Math.PI / 180), 150, 2, 200 + 40 * Math.sin(i * Math.PI / 180), 150, 18);
// light blue
grd.addColorStop(0, '#8ED6FF');
// dark blue
grd.addColorStop(1, '#0000ff');
context.fillStyle = grd;
context.fill();
}
// function for timing
function anim() {
if
(document.getElementById('R1').checked)
{ sp = 200; }
else if (document.getElementById('R2').checked) { sp = 100; }
else if (document.getElementById('R3').checked) { sp = 80; }
else
{ sp = 200; }
x = window.setInterval('circ()', sp);
}
</script>



<-Go to Third Lesson Go to Fifth Lesson->