HTML canvas clip() method
Definition and Usage
clip()
The method cuts any shape and size from the original canvas.
Tip:Once an area is cut, all subsequent drawings will be restricted to the cut area (cannot access other areas on the canvas). You can also save the current canvas area before using the clip() method using the save() method and restore it at any time later (using the restore() method).
Example
Cut a 200*120 pixel rectangle area from the canvas. Then, draw a green rectangle. Only the green rectangle part within the cut area is visible:
JavaScript:
var c=document.getElementById("myCanvas"); var ctx=c.getContext("2d"); // 剪切矩形区域 ctx.rect(50,20,200,120); ctx.stroke(); ctx.clip(); // 在 clip() 之后绘制绿色矩形 ctx.fillStyle="green"; ctx.fillRect(0,0,150,100);
语法
context.clip();
浏览器支持
表中的数字注明了首个完全支持该属性的浏览器版本。
Chrome | Edge | Firefox | Safari | Opera |
---|---|---|---|---|
Chrome | Edge | Firefox | Safari | Opera |
4.0 | 9.0 | 3.6 | 4.0 | 10.1 |
注释:Internet Explorer 8 以及更早的版本不支持