The CanvasRenderingContext2D
.clip()
method of the Canvas 2D API turns the current or given path into the current clipping region. It replaces any previous clipping region.
In the image below, the red outline represents a clipping region shaped like a star. Only those parts of the checkerboard pattern that are within the clipping region get drawn.
Note: Be aware that the clipping region is only constructed from shapes added to the path. It doesn't work with shape primitives drawn directly to the canvas, such as fillRect()
. Instead, you'd have to use rect()
to add a rectangular shape to the path before calling clip()
.
void ctx.clip([fillRule]); void ctx.clip(path [, fillRule]);
fillRule
"nonzero"
: The non-zero winding rule. Default rule."evenodd"
: The even-odd winding rule.path
Path2D
path to use as the clipping region.This example uses the clip()
method to create a clipping region according to the shape of a circular arc. Two rectangles are then drawn; only those parts within the clipping region are rendered.
<canvas id="canvas"></canvas>
The clipping region is a full circle, with its center at (100, 75), and a radius of 50.
const canvas = document.getElementById('canvas'); const ctx = canvas.getContext('2d'); // Create circular clipping region ctx.beginPath(); ctx.arc(100, 75, 50, 0, Math.PI * 2); ctx.clip(); // Draw stuff that gets clipped ctx.fillStyle = 'blue'; ctx.fillRect(0, 0, canvas.width, canvas.height); ctx.fillStyle = 'orange'; ctx.fillRect(0, 0, 100, 100);
This example saves two rectangles to a Path2D object, which is then made the current clipping region using the clip()
method. The "evenodd"
rule creates a hole where the clipping rectangles intersect; by default (with the "nonzero"
rule), there would be no hole.
<canvas id="canvas"></canvas>
const canvas = document.getElementById('canvas'); const ctx = canvas.getContext('2d'); // Create clipping path let region = new Path2D(); region.rect(80, 10, 20, 130); region.rect(40, 50, 100, 50); ctx.clip(region, "evenodd"); // Draw stuff that gets clipped ctx.fillStyle = 'blue'; ctx.fillRect(0, 0, canvas.width, canvas.height);
Specification | Status | Comment |
---|---|---|
HTML Living Standard The definition of 'CanvasRenderingContext2D.clip' in that specification. | Living Standard |
Desktop | ||||||
---|---|---|---|---|---|---|
Chrome | Edge | Firefox | Internet Explorer | Opera | Safari | |
Basic support | Yes | 12 | Yes | Yes | Yes | Yes |
Path parameter |
? | Yes | 31 | No | ? | No |
Mobile | |||||||
---|---|---|---|---|---|---|---|
Android webview | Chrome for Android | Edge Mobile | Firefox for Android | Opera for Android | iOS Safari | Samsung Internet | |
Basic support | Yes | Yes | Yes | Yes | Yes | Yes | Yes |
Path parameter |
? | ? | Yes | 31 | ? | ? | ? |
CanvasRenderingContext2D
© 2005–2018 Mozilla Developer Network and individual contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/clip