我有一个方形的背景,我试图将画布“切”成一个圆形。我试过使用clip(),但这只能在将东西绘制到画布上之前起作用。在画布上绘制完所有内容后,是否有任何方法可以对其进行裁剪?
您可以用来制作context.globalCompositeOperation='destination-in'
“事后”剪辑。
示例代码和演示:
var canvas=document.getElementById("canvas");
var ctx=canvas.getContext("2d");
var cw=canvas.width;
var ch=canvas.height;
var img=new Image();
img.onload=start;
img.src="https://i.stack.imgur.com/oURrw.png";
function start(){
var cw,ch;
cw=canvas.width=img.width;
ch=canvas.height=img.height;
ctx.drawImage(img,0,0);
ctx.globalCompositeOperation='destination-in';
ctx.beginPath();
ctx.arc(cw/2,ch/2,ch/2,0,Math.PI*2);
ctx.closePath();
ctx.fill();
}
body{ background-color: ivory; }
canvas{border:1px solid red;}
Original Image:<br>
<img src='https://i.stack.imgur.com/oURrw.png'><br>
Clip existing content into circle with Compositing<br>
<canvas id="canvas" width=300 height=300></canvas>
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句