HTML | canvas Tag

The <canvas> tag in HTML is used to draw graphics on web page using JavaScript. It can be used to draw paths, boxes, texts, gradient and adding images. By default it does not contains border and text.

Note: <canvas> tag is new in HTML5.

Syntax:

<canvas id = "script"> Contents... <canvas>

Attributes: The canvas tag contains two attributes which are listed below:

  • height: This attribute is used to set the height of canvas.
  • width: This attribute is used to set the width of canvas.

Example 1:



filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
    <head>
        <title>canvas Tag</title>
    </head>
    <body>
          
        <!-- canvas Tag starts here -->
        <canvas id = "GeeksforGeeks" width = "200" 
            height = "100" style = "border:1px solid black">
        </canvas>
        <!-- canvas Tag ends here -->
          
    </body>
</html>                    

chevron_right


Output:

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

<!-- HTML code to illustrate canvas tag -->
<!DOCTYPE html>
<html>
    <head>
        <title>canvas Tag</title>
    </head>
      
    <body>
        <canvas id = "geeks" height = "200" width = "200"
            style = "border:1px solid black">
        </canvas>
          
        <script >
            var c = document.getElementById("geeks");
            var cx = c.getContext("2d");
            cx.beginPath();
            cx.arc(100, 100, 90, 0, 2 * Math.PI);
            cx.stroke();
        </script >
    </body>
</html>                    

chevron_right


Output:

Example 3:

filter_none

edit
close

play_arrow

link
brightness_4
code

<!-- HTML code to illustrate canvas tag -->
<!DOCTYPE html>
<html>
    <head>
        <title>canvas Tag</title>
    </head>
      
    <body>
          
        <!-- canvas tag starts here -->
        <canvas id = "geeks" width = "200" height = "200"
            style = "border:1px solid black">
        </canvas>
        <!-- canvas tag ends here -->
          
        <script>
            var c=document.getElementById("geeks");
            var cx = c.getContext("2d");
            var grd = cx.createRadialGradient(100, 
                            100, 5, 100, 100, 100);
            grd.addColorStop(0, "red");
            grd.addColorStop(1, "green");
            cx.fillStyle = grd;
            cx.fillRect(0, 0, 200, 200);
        </script>
    </body>
</html>                    

chevron_right


Output:

Supported Browsers: The browser supported by <canvas> tag are listed below:

  • Google Chrome 4.0
  • Internet Explorer 9.0
  • Firefox 2.0
  • Safari 3.1
  • Opera 9.0


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.