我想画一个像甜甜圈的东西,所以中间有个洞。我尝试使用ctx.clip(),但是我意识到它将路径限制在内部,并且希望它限制路径到外部。
要注意的事情:
this.lineWidth是指“边缘”或外部部分有多厚
ctx.beginPath();
// this should be the hole
ctx.arc(this.x,this.y,this.r,0,Math.PI*2,false);
ctx.clip();
// this should be the outside part
ctx.arc(this.x,this.y,this.r+this.lineWidth,0,Math.PI*2,false);
ctx.fillStyle = "#00ff00";
ctx.fill();相反,我得到了一个填充的圆,因为它限制了路径在较小的弧内,而不是在它的外面。是否有另一种方法与clip()相反?
发布于 2016-05-04 22:15:00
我找到了这个解决方案http://jsfiddle.net/Hnw6a/
var can = document.getElementById('canvas1');
var ctx = can.getContext('2d');
//ctx.arc(x,y,radius,startAngle,endAngle, anticlockwise);
ctx.beginPath()
ctx.arc(100,100,100,0,Math.PI*2, false); // outer (filled)
ctx.arc(100,100,55,0,Math.PI*2, true); // outer (unfills it)
ctx.fill();使用以下画布节点:
<canvas id="canvas1" width="500" height="500"></canvas>发布于 2016-05-04 22:21:00
将线宽设置为所需的宽度,绘制圆圈,并使用"ctx.stroke();“。请注意,这不允许您用颜色填充内圆。
https://stackoverflow.com/questions/37038937
复制相似问题