HTML canvas clip() 方法
定义和用法
clip()
方法从原始画布中剪切任意形状和尺寸。
提示:一旦剪切了某个区域,则所有之后的绘图都会被限制在被剪切的区域内(不能访问画布上的其他区域)。您也可以在使用clip()方法前通过使用save()方法对当前画布区域进行保存,并在以后的任意时间对其进行恢复(通过restore()方法)。
实例
从画布中剪切一个200*120像素的矩形区域。然后,绘制绿色矩形。只有被剪切区域内的绿色矩形部分是可见的:
JavaScript:
var c=document.getElementById("myCanvas"); var ctx=c.getContext("2d"); // Clip rectangular area ctx.rect(50,20,200,120); ctx.stroke(); ctx.clip(); // Draw a green rectangle after clip() ctx.fillStyle="green"; ctx.fillRect(0,0,150,100);
Syntax
context.clip();
Browser Support
The numbers in the table indicate the first browser version that fully supports this property.
Chrome | Edge | Firefox | Safari | Opera |
---|---|---|---|---|
Chrome | Edge | Firefox | Safari | Opera |
4.0 | 9.0 | 3.6 | 4.0 | 10.1 |
Note:Internet Explorer 8 and earlier versions do not support the <canvas> element.