Draw and Fill a polygon and triangle in HTML5

In HTML 5, we can draw the 2D shape very easily with the help of the canvas element. In the following example demonstrate how to draw a simple polygon shape.
 

Draw Polygon

 

  1. <!DOCTYPE HTML>
  2. <html>
  3. <head>
  4. <title>An example to draw an polygon</title>
  5. <script type="text/javascript">
  6.     function drawPolygon() {
  7.         var canvas = document.getElementById('canvasbox');
  8.         if (canvas.getContext) {
  9.             var objctx = canvas.getContext('2d');
  10.  
  11.             objctx.beginPath();
  12.             objctx.moveTo(75, 50);
  13.             objctx.lineTo(175, 50);
  14.             objctx.lineTo(200, 75);
  15.             objctx.lineTo(175, 100);
  16.             objctx.lineTo(75, 100);
  17.             objctx.lineTo(50, 75);
  18.             objctx.closePath();
  19.             objctx.fillStyle = "rgb(200,0,0)";
  20.             objctx.fill();
  21.            
  22.  
  23.         } else {
  24.             alert('You need HTML5 compatible browser to see this demo.');
  25.         }
  26.     }
  27. </script>
  28. </head>
  29. <body onload="drawPolygon();">
  30.    <canvas id="canvasbox"></canvas>
  31. </body>
  32. </html>

The explanation

The above example is using the html 5′s canvas element, you can see from the above how we create an object of the 2d context from the canvas. Others methods which we are using are as follows:

beginPath() paths are stored as a list of sub-paths (lines, arcs, etc) which together form a shape. Every time this method is called, the list is reset and we can start drawing new shapes.
 
drawpolygon

 
moveTo(x, y) this will set the startup position of the path. In the above example we are drawing path from (75,50) point.
 
lineTo(x, y) will draw the straight line where x and y will be end points. This method takes two arguments – x and y, – which are the coordinates of the line’s end point and starting point is dependent on previous drawn path’s end points or moveto(x,y) coordinate. for example
 
objctx.lineTo(175, 50) will draw straight line from startpoint(75,50) to endpoint(175,50).
 
closePath() will close the shape by drawing a straight line from the current point to the start. If the shape has already been closed or there’s only one point in the list, this function does nothing.
 
fillStyle will set the background property but will not fill.

fill() will fill the shape according to fillStyle. If there is no fillstyle then it will
fill with black.
 
So above are the explanation for what we are doing for drawing the simple polygon. Now we will draw a new shape tringle.
 

Draw Triangle

 

  1. <!DOCTYPE HTML>
  2. <html>
  3. <head>
  4. <title>An example to draw an polygon</title>
  5. <script type="text/javascript">
  6.     function drawPolygon() {
  7.         var canvas = document.getElementById('canvasbox');
  8.         if (canvas.getContext) {
  9.             var objctx = canvas.getContext('2d');
  10.             objctx.beginPath();
  11.             objctx.moveTo(50, 50);
  12.             objctx.lineTo(50, 125);
  13.             objctx.lineTo(150, 125);
  14.             objctx.closePath();
  15.             objctx.stroke();
  16.  
  17.         } else {
  18.             alert('You need HTML5 compatible browser to see this demo.');
  19.         }
  20.     }
  21. </script>
  22. </head>
  23. <body onload="drawPolygon();">
  24.    <canvas id="canvasbox"></canvas>
  25. </body>
  26. </html>

facebooktwittergoogle_plusredditpinterestlinkedinmail

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>