HTML - <canvas> Tag



HTML <canvas> tag is used to draw graphics. It designates a section of the webpage where various objects, graphics, animations, and photo compositions can be made using scripts.

This is a new tag included in HTML5. HTML <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

<canvas> 
  ...
</canvas>

Attribute

HTML canvas tag supports Global and Event attributes of HTML. Accept some specipic attributes as well which are listed bellow.

Attribute Value Description
height pixels Specifies the height of the created canvas and the default value is 150.
width pixels Specifies the width of the created canvas and Default value is 300.

Examples of HTML canvas Tag

Bellow examples will illustrate the usage of canvas tag. Where, when and how to use it to create graphics using canvas tag and how we can style that graphics using CSS.

Creating a Graphics using Canvas tag

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, 100, 90, 0, 2 * Math.PI);
      y.stroke();
   </script>
</body>
</html>

Creating a Texual Graphics

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

<!DOCTYPE html>
<html>
<body>
   <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>
</body>
</html>

Styling Graphic Element

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, 570, 120);
      }
   </script>
</body>
</html>

Nested Graphics

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>

Supported Browsers

Tag Chrome Edge Firefox Safari Opera
canvas Yes 4.0 Yes 9.0 Yes 2.0 Yes 3.1 Yes 9.0
html_tags_reference.htm
Advertisements