【发布时间】:2014-08-26 14:39:12
【问题描述】:
我想绘制一个具有多个剪切区域的区域。
这是屏幕截图:
我写一个绘图示例:http://jsfiddle.net/younyzhU/zR9hg/1/
总面积应该是四个green 区域 + 中心的白色矩形。
从基础教程中,我知道绘制剪辑平面就像绘制其他东西一样。
- 创建剪辑区域
- context.clip()
但是,创建一个剪切区域,我们有多个区域,如何组合,有什么建议吗? 谢谢。
下面是一些代码:
ctx.save(); // save the context so we don't mess up others
ctx.beginPath();
ctx.fillStyle = "#ffffff";
ctx.rect(this.x, this.y, this.w, this.h);
ctx.fill();
ctx.restore(); // restore context to what it was on entry
ctx.fillStyle = "#00ff00";//Color for four surrounded area.
ctx.save(); // save the context so we don't mess up others
ctx.beginPath();
r = Math.sqrt((this.h/2)*(this.h/2) + 16 *this.w * this.w);
thea = Math.atan(this.h/this.w/8);
ctx.arc(this.x + this.w*4, this.y + this.h/2, r , Math.PI - thea, Math.PI+thea, false);
ctx.closePath();
ctx.fill();
//ctx.clip();
ctx.restore(); // restore context to what it was on entry
ctx.save(); // save the context so we don't mess up others
ctx.beginPath();
r = Math.sqrt((this.h/2)*(this.h/2) + 16 *this.w * this.w);
thea = Math.atan(this.h/this.w/8);
ctx.arc(this.x- this.w*3, this.y + this.h/2, r , -thea, thea, false);
ctx.closePath();
ctx.fill();
//ctx.clip();
ctx.restore(); // restore context to what it was on entry
ctx.save(); // save the context so we don't mess up others
ctx.beginPath();
r = Math.sqrt((this.w/2)*(this.w/2) + 16 * this.h * this.h);
thea = Math.atan(this.w/this.h/8);
ctx.arc(this.x + this.w/2, this.y + 4 * this.h, r , Math.PI*3/2-thea, Math.PI*3/2 + thea, false);
ctx.closePath();
ctx.fill();
ctx.restore(); // restore context to what it was on entry
ctx.save(); // save the context so we don't mess up others
ctx.beginPath();
r = Math.sqrt((this.w/2)*(this.w/2) + 16*this.h*this.h);
thea = Math.atan(this.w/this.h/8);
ctx.arc(this.x + this.w/2, this.y-3*this.h , r , Math.PI/2-thea, Math.PI/2 + thea, false);
ctx.closePath();
ctx.fill();
ctx.restore(); // restore context to what it was on entry
ctx.save();
【问题讨论】:
标签: javascript html5-canvas 2d clipping html2canvas