HTML Canvas Basics


canvas examples

The <canvas> tag in HTML is employed to create drawings and graphics directly within a web page.

The image on the left is made using the<canvas>element. It displays four things: a red rectangle, a fading color rectangle, a rectangle with multiple colors, and text with multiple colors.


What is HTML Canvas?

The <canvas> tag in HTML is employed with JavaScript to create graphics dynamically.

The <canvas>tag is like an empty space for graphics. To create graphics there, you need to use JavaScript.

The canvas has various methods to draw paths, shapes like boxes and circles, text, and insert images.


Canvas Examples

A canvas is like a rectangle on a webpage. It doesn't have borders or anything inside it by default.

The markup is like this:

<canvas id="myCanvas" width="200" height="100"></canvas>

Note: Remember to include an id attribute (which is used in scripts) and width and height attributes to set the canvas size. If you want a border, use the style attribute.

Here is basic example of empty canvas:


Add a JavaScript

Once you've made the rectangular canvas area, you need to include JavaScript to handle the drawing process.

Here are some examples:

Draw a Line


Draw a Circle


Draw a Text


Stroke Text


Draw Linear Gradient


Draw Circular Gradient