HTML - <canvas> Tag



One of the HTML5 components is the <canvas> tag. It designates a section of the webpage where various objects, graphics, animations, and photo compositions can be made using scripts. The <canvas> tag is only a container for visuals, thus you should use a script if you want to draw them.

When working with canvas, it's crucial to make the distinction between frequently conflated ideas like the canvas element and the context of an element. A canvas context is an object with its own set of attributes and rendering strategy. Context may be 2D or 3D. There can only be one context for the canvas element.

Syntax

Following is the syntax for HTML <canvas> tag −

<canvas id = "script"> Contents... </canvas>

Example

Let’s look at the following example, where we are going to draw the circle using the <canvas> tag.

<!DOCTYPE html>
<html>
<body>
   <canvas id = "tutorial" height = "200" width = "210"
      style = "border:2px solid #8E44AD ">
   </canvas>
   <script>
      var x = document.getElementById("tutorial");
      var y = x.getContext("2d");
      y.beginPath();
      y.arc(100, 90, 90, 0, 2 * Math.PI);
      y.stroke();
   </script>
</body>
</html>

On running the above code, the output window will pop up, displaying the circle drawn on the webpage.

Example

Consider the following example, where we are going to draw the text on the canvas using the strokeText() method.

<!DOCTYPE html>
<html>
<body>
   <center>
      <canvas id="tutorial" width="1000" height="100"></canvas>
		<script>
			var x = document.getElementById("tutorial");
			var y = x.getContext("2d");
			y.font = "60px verdana";
			y.strokeStyle = "green";
			y.strokeText("TUTORIALSPOINT", 20, 60);
		</script>
	</center>
</body>
</html>

When we execute the above code, it will generate an output consisting of text applied with a stroke that is displayed on the webpage.

Example

Following is the example where we are going to make the linear gradient and fill the rectangle with the gradient.

<!DOCTYPE html>
<html>
<body>
   <canvas id="tutorial" width="600" height="150" style="border:2px solid #D2B4DE;"></canvas>
   <script>
      var x = document.getElementById("tutorial");
      if (x.getContext) {
         var y = x.getContext("2d");
         var gradient = y.createLinearGradient(11, 91, 210, 89);
         gradient.addColorStop(0, '#DE3163');
         gradient.addColorStop(1, '#D5F5E3 ');
         y.fillStyle = gradient;
         y.fillRect(11, 12, 200, 100);
      }
   </script>
</body>
</html>

On running the above code, the output window will pop up, consisting of the linear gradient drawn on the canvas displayed on the webpage.

Example

In the following example, we are going to use the fillText() method to draw text on the canvas.

<!DOCTYPE html>
<html>
<body>
   <canvas id="tutorial" width="500" height="200" style="border:3px solid #27AE60">
   </canvas>
   <script>
      var x = document.getElementById("tutorial");
      var y = x.getContext("2d");
      y.font = "bold 35px solid";
      y.fillText("TUTORIALSPOINT", 100, 100);
   </script>
</body>
</html>

When we execute the above code, it will generate an output consisting of the text that gets filled on the canvas and is displayed on the webpage.

html_tags_reference.htm
Advertisements