Draw Shapes With HTML5 Canvas

Introduction

 
In this article, we see the basic shapes with HTML5 Canvas. We will learn how to draw shapes with HTML5 Canvas. The canvas element is just like any other block-level element on a web page, consisting of height and width definitions. By default, the height of a canvas element is set to 150 pixels, and the width is set to 300 pixels. We can create many shapes like rectangle, circle, squares, cone, and other custom shapes. HTML5 provides us many elements to create different shapes like Canvas curves and Canvas paths.
 
In this article, we are trying to create some basic shapes of the HTML5 Canvas.
 
First, we define the Canvas in the body tag on which we create shapes.
  1. <body>  
  2. <canvas id="landscape" width="800" height="350"></canvas>  
  3. <canvas id="landscape1" width="800" height="150"></canvas>  
  4. </body> 
Here we define some JavaScript code to create shapes: Code to create a Circle shape with a HTML5 Canvas:
  1. ctx.fillStyle = "rgba(255, 150, 255, 255)";  
  2. ctx.beginPath();  
  3. ctx.arc(50, 50, 25, 0, Math.PI * 2, true);  
  4. ctx.closePath();  
  5. ctx.fill(); 
Then, we create a star shape with HTML5 Canvas:
  1. ctx.fillStyle = "red";  
  2. ctx.beginPath();  
  3. ctx.moveTo(107.8, 0.0);  
  4. ctx.lineTo(141.2, 67.5);  
  5. ctx.lineTo(215.7, 78.3);  
  6. ctx.lineTo(161.8, 130.9);  
  7. ctx.lineTo(174.5, 205.1);  
  8. ctx.lineTo(107.8, 170.1);  
  9. ctx.lineTo(41.2, 205.1);  
  10. ctx.lineTo(53.9, 130.9);  
  11. ctx.lineTo(0.0, 78.3);  
  12. ctx.lineTo(74.5, 67.5);  
  13. ctx.lineTo(107.8, 0.0);  
  14. ctx.closePath();  
  15. ctx.fill(); 
Then, we create a Rectangle shape with HTML5 Canvas:
  1. ctx.fillStyle = "green";  
  2. ctx.beginPath();  
  3. ctx.moveTo(0, 250);  
  4. ctx.lineTo(500, 250);  
  5. ctx.lineTo(500, 300);  
  6. ctx.lineTo(0, 300);  
  7. ctx.lineTo(0, 300);  
  8. ctx.fill();  
  9. ctx.closePath();  
  10. ctx.fillStyle = "black";  
  11. ctx.beginPath();  
  12. ctx.moveTo(0, 350);  
  13. ctx.lineTo(500, 250);  
  14. ctx.lineTo(500, 300);  
  15. ctx.lineTo(0, 300);  
  16. ctx.lineTo(0, 300);  
  17. ctx.fill();  
  18. ctx.closePath(); 
Then, we create a Mountain shape in the form of a cone with HTML5 Canvas:
  1. ctx.fillStyle = "#67ff30";  
  2. ctx.beginPath();  
  3. ctx.moveTo(150, 250);  
  4. ctx.lineTo(300, 20);  
  5. ctx.lineTo(450, 250);  
  6. ctx.lineTo(150, 250);  
  7. ctx.fill();  
  8. ctx.closePath();  
  9. ctx.fillStyle = "Brown";  
  10. ctx.beginPath();  
  11. ctx.moveTo(400, 250);  
  12. ctx.lineTo(450, 80);  
  13. ctx.lineTo(600, 250);  
  14. ctx.lineTo(400, 250);  
  15. ctx.fill();  
  16. ctx.closePath();  
  17. ctx.beginPath(); 
Then, we create a shape like a cloud with the HTML5 Canvas:
  1. ctx.beginPath();  
  2. ctx.fillStyle = "black";  
  3. ctx.moveTo(75, 25);  
  4. ctx.quadraticCurveTo(25, 25, 25, 62.5);  
  5. ctx.quadraticCurveTo(25, 100, 50, 100);  
  6. ctx.quadraticCurveTo(50, 120, 30, 125);  
  7. ctx.quadraticCurveTo(60, 120, 65, 100);  
  8. ctx.quadraticCurveTo(125, 100, 125, 62.5);  
  9. ctx.quadraticCurveTo(125, 25, 75, 25);  
  10. ctx.stroke();  
  11. ctx.beginPath(); 
Here is the Complete Code:
  1. <html xmlns="http://www.w3.org/1999/xhtml">  
  2. <head>  
  3.     <title></title>  
  4.     <script language="javascript">  
  5.         window.onload = function () {  
  6.             var landscape_canvas = document.getElementById("landscape");  
  7.             var ctx = landscape_canvas.getContext("2d");  
  8.             ctx.fillStyle = "Blue";  
  9.             ctx.fillRect(0, 0, 800, 850);  
  10.             // Circle  
  11.             ctx.fillStyle = "rgba(255, 150, 255, 255)";  
  12.             ctx.beginPath();  
  13.             ctx.arc(50, 50, 25, 0, Math.PI * 2, true);  
  14.             ctx.closePath();  
  15.             ctx.fill();  
  16.             // Star  
  17.             ctx.fillStyle = "red";  
  18.             ctx.beginPath();  
  19.             ctx.moveTo(107.8, 0.0);  
  20.             ctx.lineTo(141.2, 67.5);  
  21.             ctx.lineTo(215.7, 78.3);  
  22.             ctx.lineTo(161.8, 130.9);  
  23.             ctx.lineTo(174.5, 205.1);  
  24.             ctx.lineTo(107.8, 170.1);  
  25.             ctx.lineTo(41.2, 205.1);  
  26.             ctx.lineTo(53.9, 130.9);  
  27.             ctx.lineTo(0.0, 78.3);  
  28.             ctx.lineTo(74.5, 67.5);  
  29.             ctx.lineTo(107.8, 0.0);  
  30.             ctx.closePath();  
  31.             ctx.fill();  
  32.             //Rectangle  
  33.             ctx.fillStyle = "green";  
  34.             ctx.beginPath();  
  35.             ctx.moveTo(0, 250);  
  36.             ctx.lineTo(500, 250);  
  37.             ctx.lineTo(500, 300);  
  38.             ctx.lineTo(0, 300);  
  39.             ctx.lineTo(0, 300);  
  40.             ctx.fill();  
  41.             ctx.closePath();  
  42.             ctx.fillStyle = "black";  
  43.             ctx.beginPath();  
  44.             ctx.moveTo(0, 350);  
  45.             ctx.lineTo(500, 250);  
  46.             ctx.lineTo(500, 300);  
  47.             ctx.lineTo(0, 300);  
  48.             ctx.lineTo(0, 300);  
  49.             ctx.fill();  
  50.             ctx.closePath();  
  51.             //Cone  
  52.             ctx.fillStyle = "#67ff30";  
  53.             ctx.beginPath();  
  54.             ctx.moveTo(150, 250);  
  55.             ctx.lineTo(300, 20);  
  56.             ctx.lineTo(450, 250);  
  57.             ctx.lineTo(150, 250);  
  58.             ctx.fill();  
  59.             ctx.closePath();  
  60.             ctx.fillStyle = "Brown";  
  61.             ctx.beginPath();  
  62.             ctx.moveTo(400, 250);  
  63.             ctx.lineTo(450, 80);  
  64.             ctx.lineTo(600, 250);  
  65.             ctx.lineTo(400, 250);  
  66.             ctx.fill();  
  67.             ctx.closePath();  
  68.             ctx.beginPath();  
  69.             // get 2 canvas  
  70.             var landscape_canvas = document.getElementById("landscape1");  
  71.             var ctx = landscape_canvas.getContext("2d");  
  72.             ctx.fillStyle = "blue";  
  73.             ctx.fillRect(0, 0, 600, 450);  
  74.             ctx.beginPath();  
  75.             ctx.fillStyle = "black";  
  76.             ctx.moveTo(75, 25);  
  77.             ctx.quadraticCurveTo(25, 25, 25, 62.5);  
  78.             ctx.quadraticCurveTo(25, 100, 50, 100);  
  79.             ctx.quadraticCurveTo(50, 120, 30, 125);  
  80.             ctx.quadraticCurveTo(60, 120, 65, 100);  
  81.             ctx.quadraticCurveTo(125, 100, 125, 62.5);  
  82.             ctx.quadraticCurveTo(125, 25, 75, 25);  
  83.             ctx.stroke();  
  84.             ctx.beginPath();  
  85.         }  
  86.     </script>  
  87. </head>  
  88. <body>  
  89. <canvas id="landscape" width="800" height="350"></canvas>  
  90. <canvas id="landscape1" width="800" height="150"></canvas>  
  91. </body>  
  92. </html>  
Press F5 and see the output:
 
Clipboard07.jpg