我所要求的可能非常简单,但我在获得预期结果时遇到了很多麻烦。
我想要一个形状(在这个例子中它是正方形,但应该与其他形状如圆形等一起使用)在它离开另一个形状的边界时将其自身切断。
基本上,顶部图像是我想要的,底部是我目前拥有的: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);