【发布时间】:2014-12-19 09:24:26
【问题描述】:
我在我的上下文中画了很多东西。形状、文本、图像..
我想在globalCompositeOperation 的上下文中使用clip 方法来达到相同的效果(使用clip 对我来说更难执行,我不知道文本是否可能)
用户可以绘制几个形状。然后开始一个掩码阶段。绘制更多的形状,文本..将绘制到蒙版中,然后下一次绘制将在蒙版阶段被剪辑。然后继续常规绘图...
例如
用户绘制此图
然后开始蒙版模式,画了这两条红线
然后他停止在蒙版中绘制,并开始绘制矩形以考虑蒙版
最后应用了蒙版剪辑,结果应该是这样的
如果没有早期的图纸,我已经设法用线条剪裁矩形。
// Starting the mask phase
ctx.strokeStyle = 'red';
ctx.lineWidth = 5;
ctx.beginPath();
ctx.moveTo(20, 20);
ctx.lineTo(240, 140);
ctx.moveTo(80, 20);
ctx.lineTo(300, 140);
ctx.stroke();
ctx.globalCompositeOperation = 'source-out';
ctx.fillStyle = 'cyan';
ctx.fillRect(50, 70, 250, 20);
// Setting the composition back
ctx.globalCompositeOperation = 'source-over';
但是当我在代码的开头添加我的图纸时,构图也会考虑它。
ctx.fillStyle = 'brown';
ctx.beginPath();
ctx.arc(80, 80, 50, 50, 0, Math.PI * 2);
ctx.fill();
ctx.fillStyle = 'yellow';
ctx.fillRect(80, 60, 150, 40);
ctx.fillStyle = 'black';
ctx.font = '40pt arial';
ctx.fillText('Hello', 130, 110);
// How to tell the context to start from here the compisition ???
如果可能的话,如何告诉上下文从某个点开始合成?
我可以创建另一个画布并在那里绘制蒙版……然后在主画布上绘制新的画布。但是有更好的解决方案吗?
【问题讨论】: