【发布时间】:2016-01-24 10:39:51
【问题描述】:
我所要求的可能非常简单,但我在获得预期结果时遇到了很多麻烦。
我想要一个形状(在本例中是正方形,但应与其他形状如圆形等一起使用)在离开另一个形状的边界时自行切断。
基本上,顶部图像是我想要的,底部是我目前拥有的: http://imgur.com/a/oQkzG
我听说这可以通过 globalCompositeOperation 来完成,但我正在寻找任何能够提供所需结果的解决方案。
这是当前代码,如果你不能使用 JSFiddle:
// Fill the background
ctx.fillStyle = '#0A2E36';
ctx.fillRect(0, 0, canvas.width, canvas.height);
// Fill the parent rect
ctx.fillStyle = '#CCA43B';
ctx.fillRect(100, 100, 150, 150);
// Fill the child rect
ctx.fillStyle = 'red';
ctx.fillRect(200, 200, 70, 70);
// And fill a rect that should not be affected
ctx.fillStyle = 'green';
ctx.fillRect(80, 80, 50, 50);
【问题讨论】:
-
问题是我无法弄清楚将它们放在哪里或使用哪些,即使在阅读了该指南之后我也无法获得预期的效果。
-
ctx.globalCompositeOperation='source-over' ctx.fillStyle='yellow'; ctx.fillRect(10,10,50,50); ctx.fillStyle='green' ctx.fillRect(0,0,20,20) ctx.globalCompositeOperation='source-atop'; ctx.fillStyle='red' ctx.fillRect(40,40,20,20)
标签: javascript html canvas html5-canvas globalcompositeoperation