The Wayback Machine - https://web.archive.org/web/20240119133541/https://www.geeksforgeeks.org/html-canvas-clip-method/
Open In App
Related Articles

HTML canvas clip() Method

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report

The clip() method is used to clip a region/part of any shape and size from the given/original canvas. After clipping a region, the further drawings can be applied only on the clipped region. Although save() and restore() method can be used to get back to the previous canvas.

Syntax:

context.clip();

Example:




<!DOCTYPE html>
<html>
  
<head>
    <title>
        HTML canvas clip() Method
    </title>
</head>
  
<body style="text-align:left;">
  
    <h1>GeeksforGeeks</h1>
  
    <h2>HTML canvas clip() Method</h2>
    <h2>Before clip():</h2>
    <canvas id="GFG" width="500" height="200" style="border:2px solid">
    </canvas>
  
    <script>
        var doc_id = document.getElementById("GFG");
        var context = doc_id.getContext("2d");
  
        // Draw a rectangle
        context.rect(100, 20, 200, 120);
        context.stroke();
  
        // Draw green rectangle
        context.fillStyle = "green";
        context.fillRect(50, 50, 150, 100);
    </script>
    <h2>After clip():</h2>
    <canvas id="GFG2" width="500" height="200" style="border:2px solid ;">
    </canvas>
  
    <script>
        var doc_id = document.getElementById("GFG2");
        var context = doc_id.getContext("2d");
        // Clip a region
        context.rect(100, 20, 200, 120);
        context.stroke();
        context.clip();
  
        // Draw green rectangle after clip()
        context.fillStyle = "green";
        context.fillRect(50, 50, 150, 100);
    </script>
</body>
  
</html>


Output:

Supported Browsers: The browser supported by HTML canvas clip() Method are listed below:

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

Learn to code easily with our course Coding for Everyone. This course is accessible and designed for everyone, even if you're new to coding. Start today and join millions on a journey to improve your skills!

Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Commit to GfG's Three-90 Challenge! Purchase a course, complete 90% in 90 days, and save 90% cost click here to explore.
Last Updated : 09 Jun, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads
Complete Tutorials