HTML5 Canvas

The HTML5 Canvas element is not just another HTML tag like <a> or <table<. It is the most significant addition to HTML5, as it completely revolutionized the graphical landscape as we knew it before its arrival.

HTML5 earned the near-mythical status of the Flash Killer due to the role of the canvas element.

HTML5 canvas can be manipulated with JavaScript to produce impressive interactive graphics - along with other HTML5 elements, of course.

The process is as follows:

  1. In order to manipulate the HTML5 Canvas, place the canvas tag inside the HTML document
  2. Then access the canvas tag with JavaScript, create a context, and then make use of the HTML5 Canvas API to draw anything you like.
  3. When using canvas, you must make a distinction between the canvas element and the canvas context; they are not the same.
  4. Canvas Element
    - the DOM element inside the HTML document
    Canvas Context
    - the object with properties and methods to manipulate Canvas element by using JavaScript
    getContext() method
    - for referring to the Context object in the canvas element
  5. Each canvas element has just one Context object. If you have many canvas elements in a web page, they must be referred to through their individual context objects.

 

 

This is a 400 X 300 canvas element with an olive border. The message inside was created using the context object of the canvas element with JavaScript.

The code is as follows:

<canvas id="CanvasOne" width="400" height="300" style="border:5px olive dashed;" >
</canvas>
<script>
var canvas = document.getElementById('CanvasOne');   // refer to canvas element
var context = canvas.getContext('2d');    // refer to the context object
context.font = "30ptCalibri";
context.fillStyle = 'olive';
   // colour context.fillText('Welcome!', 120, 150);    // coordinates relative to canvas element
</script>

Real Power of HTML5 Canvas Element

The following is a more complex animation made on the canvas element.

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

Slow



Medium



Fast

 

Please stop the spring before changing the speed.

 

The source code for the animation is given below:

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

 

In order to learn HTML5 Canvas a bit more comprehensively, please use the following link:

HTML5 Canvas Tutorial

 

Previous