HTML | DOM Canvas Object

The DOM Canvas Object is used to represent the HTML <Canvas> element. The <canvas> tag is used to draw a graphics in the document using javascript. It is new in HTML5. The canvas element is accessed by getElementById().

Syntax:

accessed by getElementById("id"). 

Where “id” is the ID assigned to the Canvas tag.



Example-1:

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
  
<head>
    <title>canvas Tag</title>
</head>
  
<body>
    <center>
        <h1 style="color:green; Font-size:35px;" >
           GeeksForGeeks
        </h1>
        <h2>DOM Canvas Object</h2>
        
        <!-- canvas Tag starts here -->
        <canvas id="geeks" height="200" width="200" 
                style="border:1px solid green">
        </canvas>
        <!-- canvas Tag ends here -->
        
        <br>
        <br>
        
        <button onclick="myGeeks()">Submit</button>
         
      <script>
            function myGeeks() {
  
                var gfg = document.getElementById("geeks");
                
                var sudo = gfg.getContext("2d");
                sudo.beginPath();
                sudo.arc(100, 100, 90, 0, 2 * Math.PI);
                sudo.stroke();
            }
        </script>
</body>
  
</html>

chevron_right


Output:
Before Clicking On Button:

After Clicking on Button:

Example-2: Canvas Object can be created by using the document.createElement method.

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
  
<head>
    <style>
        canvas {
            border: 3px solid black;
        }
    </style>
</head>
  
<body>
    <h1 style = "color: green; font-size:40px;" >
      GeeksForGeeks
    </h1>
    <h2>DOM Canvas Object</h2>
    
    <button onclick="myGeeks()">
      Submit
    </button>
  
    <script>
        function myGeeks() {
            var geeks = document.createElement("CANVAS");
            var gfg = geeks.getContext("2d");
            gfg.fillStyle = "green";
            gfg.fillRect(40, 40, 200, 100);
            document.body.appendChild(geeks);
        }
    </script>
  
</body>
  
</html>

chevron_right


Output:
Before Clicking on Button:

After Clicking on Button:

Supported Browsers: The browser supported by DOM Canvas Object are listed below:

  • Google Chrome
  • Internet Explorer
  • Firefox
  • Opera
  • Safari


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.