<canvas> Tag in HTML

The <canvas> tag is used to draw graphics on a web page using JavaScript. It allows you to draw shapes, images, and other graphical elements on the fly. The canvas element is often used for creating dynamic graphics, such as charts, graphs, animations, and game graphics.

Key Points on <canvas> Tag:

Syntax of <canvas> Tag:

Syntax Example

<canvas width="200" height="200"></canvas>

Example of <canvas> Tag in HTML:

This example demonstrates how to create a basic canvas and draw a rectangle on it using JavaScript.

Code Example


    <canvas id="myCanvas" width="200" height="200"></canvas>
    
    <script>
    var canvas = document.getElementById("myCanvas");
    var ctx = canvas.getContext("2d");
    ctx.fillStyle = "#FF0000";
    ctx.fillRect(20, 20, 150, 100);
    </script>

Output

Canvas Attributes:

Canvas Methods:

The <canvas> tag is extremely powerful for creating complex graphics, animations, and games directly in the browser. By using JavaScript, you can manipulate pixels and create dynamic, interactive content that responds to user input.