Working With Canvas in HTML5

Introduction

 
The canvas element is used for drawing graphics using JavaScript. Its default size is 300 pixels wide and 150 pixel high. Now we use the canvas element on our page. We write the following code:
  1. <!DOCTYPE HTML>  
  2. <html>  
  3.     <body>  
  4.         <canvas></canvas>  
  5.     </body>  
  6. </html>  
We look at this page in the browser. The canvas will be not visible because by default the canvas area is invisible. So, now we set a border and write the following code
  1. <!DOCTYPE HTML>  
  2. <html>  
  3.     <body>  
  4.         <canvas style="border:2px solid #33C5FF;"></canvas>  
  5.     </body>  
  6. </html>  
Now the canvas area is visible. The page will look like the below figure:
 
canvas-area
 

Adding Graphics in Canvas

 
Now we try to draw a rectangle on the canvas. We use JavaScript for drawing an object in the canvas area. With the fillRect method, we can draw a rectangle. We pass four parameter values as fillRect(X,Y,Width,Height). X and Y define (x,y) co-ordinate and width, height for Height and Width respectively. We write the following code
  1. <!DOCTYPE HTML>  
  2. <html>  
  3.     <body>  
  4.         <canvas id="CAN" style="border:2px solid #33C5FF ;"></canvas>  
  5.         <script type="text/javascript">  
  6.      var VAR1 = document.getElementById("CAN");  
  7.      var VAR2 = VAR1.getContext("2d");  
  8.      VAR2.fillRect(10, 10, 100, 90);  
  9.  </script>  
  10.     </body>  
  11. </html>  
We run this code. The page will look like the below figure:
 
drawing-rectangle-in-canvas
 
Here we note that the background color of the rectangle is black. It is the default color. Now we set a background color for it. We add a fill style property to assign a  color to it.
 
The fill style property accepts a hexadecimal color value. We write the following code:
  1. <!DOCTYPE HTML>  
  2. <html>  
  3.     <body>  
  4.         <canvas id="CAN" style="border:2px solid #33C5FF ;"></canvas>  
  5.         <script type="text/javascript">  
  6.     var VAR1 = document.getElementById("CAN");  
  7.     var VAR2 = VAR1.getContext("2d");  
  8.     VAR2.fillStyle = "#E0D5DD";  
  9.     VAR2.fillRect(10, 10, 100, 90);  
  10. </script>  
  11.     </body>  
  12. </html>  
We run this code. The page will look like the  below figure:
 
 fill-color
 
We can draw a custom shape also. We start it with beginpath() then we draw our custom shape and finish it with closepath(). Like the following code:
  1. <!doctype html><html><body><canvas id="CAN" style="border:2px solid #33C5FF ;"></canvas><script type="text/javascript">  
  2.      var VAR1 = document.getElementById("CAN");  
  3.      var VAR2 = VAR1.getContext("2d");  
  4.      VAR2.fillStyle = "#807C58";  
  5.      VAR2.strokeStyle = "#f00";  
  6.      VAR2.lineWidth = 6;  
  7.      VAR2.beginPath();  
  8.      VAR2.moveTo(20, 20);  
  9.      VAR2.lineTo(120, 60);  
  10.      VAR2.lineTo(160, 90);  
  11.      VAR2.lineTo(80, 120);  
  12.      VAR2.lineTo(20, 20);  
  13.      VAR2.fill();  
  14.      VAR2.stroke();  
  15.      VAR2.closePath();  
  16.  </script></body></html>  
We run this code. The page will look like the below figure:
 
custom-shape
 
We can also set a shadow to the shape of a canvas element by assigning a color value to the property - shadowColor. Like the following code:
  1. <!doctype html><html><body><canvas id="CAN" style="border:2px solid #33C5FF ;"></canvas><script type="text/javascript">  
  2.      var VAR1 = document.getElementById("CAN");  
  3.      var VAR2 = VAR1.getContext("2d");  
  4.      VAR2.shadowOffsetX = 10;  
  5.      VAR2.shadowOffsetY = 12;  
  6.      VAR2.shadowBlur = 6;  
  7.      VAR2.fillStyle = "#807C58";  
  8.      VAR2.shadowColor = "#7B51C4";  
  9.      VAR2.beginPath();  
  10.      VAR2.moveTo(20, 20);  
  11.      VAR2.lineTo(120, 60);  
  12.      VAR2.lineTo(160, 90);  
  13.      VAR2.lineTo(80, 120);  
  14.      VAR2.lineTo(20, 20);  
  15.      VAR2.fill();  
  16.      VAR2.stroke();  
  17.      VAR2.closePath();  
  18.  </script></body></html>  
Then, We run this code. The page will look like the below figure:
 
custom-shape-shadow