Menu
×
   ❮     
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C C++ C# BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS R TYPESCRIPT ANGULAR GIT POSTGRESQL MONGODB ASP AI GO KOTLIN SASS VUE DSA GEN AI SCIPY AWS CYBERSECURITY DATA SCIENCE
     ❯   

HTML Canvas Clipping


The clip() Method

The clip() method turns the current path into the current clipping region.

When a region is clipped, future drawing is only visible inside the clipped region.

The clip() method has the following parameters:

Parameter Description
fillrule Is a point inside or outside the clipping region? Possible values: nonzero|evenodd
path A path to use as the clipping region

Let's look at some examples:

Example

First, create a circular clipping region. Then draw two rectangles; only those parts that lies inside the clipping region are visible:

Your browser does not support the HTML5 canvas tag.
<script>
const canvas = document.getElementById("myCanvas");
const ctx = canvas.getContext("2d");

// Create a circular clipping region
ctx.beginPath();
ctx.arc(100, 75, 70, 0, Math.PI * 2);
ctx.clip();

// Draw two rectangles
ctx.fillStyle = "blue";
ctx.fillRect(0, 0, 300, 150);
ctx.fillStyle = "red";
ctx.fillRect(0, 0, 90, 90);
</script>
Try it Yourself »

Example

First, create a triangle-shaped clipping region. Then draw two rectangles; only those parts that lies inside the clipping region are visible:

Your browser does not support the HTML5 canvas tag.
<script>
const canvas = document.getElementById("myCanvas");
const ctx = canvas.getContext("2d");

// Create a triangle-shaped clipping region
ctx.beginPath();
ctx.moveTo(100,20);
ctx.lineTo(180,100);
ctx.lineTo(20,100);
ctx.lineTo(100,20);
ctx.clip();

// Draw two rectangles
ctx.fillStyle = "blue";
ctx.fillRect(0, 0, 300, 150);
ctx.fillStyle = "red";
ctx.fillRect(0, 0, 90, 90);
</script>
Try it Yourself »

Example

First, create a circular clipping region. Then draw an image onto the canvas; again - only those parts that lies inside the clipping region are visible:

Your browser does not support the HTML5 canvas tag.
<script>
const canvas = document.getElementById("myCanvas");
const ctx = canvas.getContext("2d");
const image = document.getElementById("scream");

image.addEventListener("load", (e) => {
  // Create a circular clipping region
  ctx.beginPath();
  ctx.arc(110, 145, 75, 0, Math.PI * 2);
  ctx.clip();
  // Draw image onto canvas
  ctx.drawImage(image, 0, 0);
});
</script>
Try it Yourself »

Example

First, we save two rectangles to a Path2D() object, this will be the clipping region. The "evenodd" rule creates a hole where the clipping rectangles intersect: If we had used the default "nonzero" rule, there would be no hole:

Your browser does not support the HTML5 canvas tag.
<script>
const canvas = document.getElementById("myCanvas");
const ctx = canvas.getContext("2d");

// Create clipping path (two rectangles)
let r = new Path2D();
r.rect(80,10, 45,130);
r.rect(40,50, 120,50);
ctx.clip(r, "evenodd");

// Draw a rectangle
ctx.fillStyle = "red";
ctx.fillRect(0, 0, 300, 150);
</script>
Try it Yourself »

Example

Same example as above, but with the "nonzero" rule (does not create a hole where the clipping rectangles intersect):

Your browser does not support the HTML5 canvas tag.
<script>
const canvas = document.getElementById("myCanvas");
const ctx = canvas.getContext("2d");

// Create clipping path (two rectangles)
let r = new Path2D();
r.rect(80,10, 45,130);
r.rect(40,50, 120,50);
ctx.clip(r, "nonzero");

// Draw a rectangle
ctx.fillStyle = "red";
ctx.fillRect(0, 0, 300, 150);
</script>
Try it Yourself »

×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
sales@w3schools.com

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
help@w3schools.com

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookie and privacy policy.

Copyright 1999-2024 by Refsnes Data. All Rights Reserved. W3Schools is Powered by W3.CSS.