HTML - <canvas> Tag
Introduction to <canvas> Tag
The HTML <canvas> tag is used to draw the graphics on a webpage, such as lines, shapes and animations through JavaScript. It provides a drawable region defined by width and height attributes. By default, the <canvas> tag is invisible and requires JavaScript to render visual elements.
The <canvas> tag can also be used for creating charts, data visualizations and other graphical content. However, it does not include any built-in drawing capabilities, everything needs to be programmatically specified.
Syntax
Following is the syntax of HTML <canvas> tag −.
<canvas> ... </canvas>
Attributes
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. |
Example : Drawing a Circle
Lets 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>
Example : Creating a Textual Graphics
Consider the following example, where we are going to draw the text on the canvas using the canvas tag along with the 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>
Example : Applying Linear Gradient
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>
Example : Applying with fillText()
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 | ![]() |
![]() |
![]() |
![]() |
![]() |
|---|---|---|---|---|---|
| canvas | Yes 4.0 | Yes 9.0 | Yes 2.0 | Yes 3.1 | Yes 9.0 |




