[英]HTML5 Canvas: globalCompositeOperation (eraser)
How can I use globalCompositeOperation for erasing something? 如何使用globalCompositeOperation删除某些内容?
http://canvaspaint.org/ has an eraser, but that's just a white line - ok only if your background is white... http://canvaspaint.org/有一个橡皮擦,但这只是一条白线 - 只有你的背景是白色的......
I know you can use ctx.clearRect(). 我知道你可以使用ctx.clearRect()。 But it didn't really worked for me because while dragging a mouse with eraser (set to 8x8px) it only makes unconnected 8x8px squares - not really a smooth line.
但它并没有真正起作用,因为在拖动带有橡皮擦的鼠标(设置为8x8px)时,它只会产生未连接的8x8px正方形 - 实际上并不是一条平滑的线条。
Is there a way how to use globalCompositeOperation as an eraser? 有没有办法如何使用globalCompositeOperation作为橡皮擦?
Something like: 就像是:
ctx.globalCompositeOperation = "___something___";
ctx.beginPath();
ctx.lineTo(mouseX , mouseY);
ctx.closePath();
Thank you. 谢谢。
Yes you can erase using globalCompositeOperation as described here . 是的,您可以使用globalCompositeOperation擦除,如此处所述 。 Just set it to
"copy"
and use eg strokeStyle = "rgba(0,0,0,0)"
and that will clear the canvas in the stroke. 只需将其设置为
"copy"
并使用例如strokeStyle = "rgba(0,0,0,0)"
,这将清除笔画中的画布。
Update: thanks for pointing out this doesn't work now @will-huang. 更新:感谢指出现在这不起作用@ will-huang。 You should as mentioned have globalCompositeOperation set to
destination-out
and strokeStyle set to rgba(0,0,0,1)
. 您应该如上所述将globalCompositeOperation设置为
destination-out
并将strokeStyle设置为rgba(0,0,0,1)
。 (Actually you can have any RGB values, just you need the alpha set to 1.0 to fully erase the contents of the stroke.) (实际上你可以有任何RGB值,只需要将alpha设置为1.0就可以完全擦除笔画的内容。)
Here's an example of erasing: http://jsfiddle.net/FGcrq/1/ 这是一个擦除的例子: http : //jsfiddle.net/FGcrq/1/
ctx.globalCompositeOperation = "destination-out";
That should do the trick. 这应该够了吧。 You can also use "xor" for erasing goodness.
您也可以使用“xor”来消除善意。
I don't think so. 我不这么认为。 But just change the line color to whatever the background color is.
但只需将线条颜色更改为背景颜色即可。 Also, if you want different sizes of eraser increase the line size.
此外,如果您想要不同尺寸的橡皮擦增加线条尺寸。
ctx.lineWidth=//default 1.0
and ctx.strokeStyle=//default black
I would also suggest using ctx.save()
and ctx.restore()
so you don't have to worry about resetting your line attributes. ctx.lineWidth=//default 1.0
和ctx.strokeStyle=//default black
我也建议使用ctx.save()
和ctx.restore()
这样你就不用担心重置你的行属性了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.