Open In App

What is the use of <canvas> Tag in HTML ?

HTML <canvas> Tag allows you to create graphics, animations, and interactive content directly on a web page using JavaScript. It provides a drawing surface where you can use various JavaScript functions to create dynamic and visually appealing content.

Here are key aspects of the <canvas> element:



Syntax

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

Example: Implementation of the canvas element in HTML.




<!DOCTYPE html>
<html lang="en">
  
<head>
    <meta charset="UTF-8">
    <meta name="viewport" 
          content="width=device-width, initial-scale=1.0">
    <title>Canvas Element</title>
</head>
  
<body>
    <canvas id="myCanvas" width="200" 
            height="200" style="border:2px solid black;">
      </canvas>
  
    <script>
        let canvas = document.getElementById("myCanvas");
        let ctx = canvas.getContext("2d");
        ctx.fillStyle = "green";
        ctx.fillRect(50, 50, 100, 100);
    </script>
</body>
  
</html>

Output:



Article Tags :