html

Canvas in HTML | Explained with Examples

In HTML, <canvas> tag can be utilized to draw the graphics on the websites. Canvas act as a container for the graphics, and to draw the graphics we utilize a script like JavaScript. Therefore, in the <canvas> tag an id attribute must be used that refers the <canvas> element to the JavaScript. It can be used to draw 2-dimensional graphics, images, animations, etc. This write-up presents a comprehensive guide for the <canvas> tag with help of some code snippets.

In this article we will learn the following concepts related to the <canvas> tag:

  • Basics of <canvas> Tag
  • How to create <canvas> element
  • How to Use Canvas with JavaScript
  • How to Draw Basic Shapes on Canvas

Let’s start!

Basics of <canvas> Tag

An id, height, and width attributes are necessary for any <canvas> element. The id attribute is used for referring to the canvas in JavaScript while the height and width attributes determine the size of the canvas. On a single HMTL document, we can have several <canvas> tags.

How to Create <canvas> Element

Let’s consider the below-given snippet to understand how to create a canvas element:

<canvas id="Canvas" width="250" height="150" style="border: 2px solid blue">

Here, id=canvas which will be utilized in the script to find the canvas element, width and height attributes set the canvas width and height as 250px and style attribute is used to style the canvas element.

How to Use Canvas with JavaScript

In this section, we will provide a step-by-step guide for using the <canvas> element with JavaScript. We have to follow the below-mentioned steps to proceed:

Firstly we have to get the <canvas> element and for this purpose “getElementId()” method will be utilized as shown in the below code snippet:

Next, we will create a drawing object using a build-in object “getContext()” and inside this object, we will specify a dimension like 2-dimensional as shown in the below figure:

var objContext = can.getContext("2d");

The final step is to draw on the canvas, we will specify a “fillStyle” property to set a color for the drawing:

objContext.fillStyle='grey';

How to Draw Basic Shapes on Canvas

This section will elaborate how to draw a rectangle, line, and circle on canvas:

Rectangle

To draw a rectangle on the canvas, we will utilize a fillRect() method. It will take four values: first for x-coordinate, second for y-coordinate, third for the width, and fourth for the height.

objContext.fillRect(10,10,175,85);

Here is our complete code snippet to draw a rectangle on canvas:

<body>

<canvas id="Canvas" width="250" height="150" style="border: 2px solid blue">

<script>

var can = document.getElementById("Canvas");

var objContext = can.getContext("2d");

objContext.fillStyle='grey';

objContext.fillRect(10,10,175,85);

</script>

</body>

For the above code, we will get the following output:

As we specify 10px space for x, y coordinates so in the output the space around the top and left side verifies that our code works properly.

Line

Now let’s consider another simple example to understand how to draw a line on a canvas. We utilize the moveTo() and lineTo() method to specify the initial and final points of the line respectively. To draw the line we have to use a method stroke() method:

<body>

<canvas id="Canvas" width="250" height="150" style="border: 2px solid blue">

<script>

var can = document.getElementById("Canvas");

var objContext = can.getContext("2d");

objContext.moveTo(0,0);

objContext.lineTo(250,150);

objContext.stroke();

</script>

</body>

On successful execution of the code we will get the following output:

A line is drawn on the canvas starting from (0, 0) and ending at (250, 150).

Circle

To draw a circle, we will utilize beginPath() and arc() methods to begin a path and create a circle respectively. The arc method will take values for the x, y coordinates, the radius of the circle, start angle, and end angle.

<body>

<canvas id="Canvas" width="250" height="150" style="border: 2px solid blue">

<script>

var can = document.getElementById("Canvas");

var objContext = can.getContext("2d");

objContext.beginPath();

objContext.arc(120, 70, 50, 0, 2*Math.PI);

objContext.stroke();

</script>

</body>

Math.PI property is utilized as the endpoint of the circle and the following will be the output:

In this section, we have learned how to draw basic shapes on canvas such as a line, circle, and rectangle.

Conclusion

The <canvas> tag is used to draw graphics on websites using JavaScript and to draw a shape on canvas four essential steps are required i.e. create your canvas, fetch the canvas element using a build-in method “getElementById()”, create your object using getContext() object, and finally draw the shapes on the canvas. The first half of this write-up demonstrated how to create and use canvas elements and the latter half of this tutorial explains how to draw basic shapes with canvas such as a line, circle, and rectangle.

About the author

Anees Asghar

I am a self-motivated IT professional having more than one year of industry experience in technical writing. I am passionate about writing on the topics related to web development.