简体   繁体   English

HTML5 Canvas:globalCompositeOperation(橡皮擦)

[英]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.0ctx.strokeStyle=//default black我也建议使用ctx.save()ctx.restore()这样你就不用担心重置你的行属性了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 HTML 5 canvas globalCompositeOperation(橡皮擦)问题 - HTML 5 canvas globalCompositeOperation (eraser) issues HTML5 Canvas:如何伪造globalCompositeOperation =“darker” - HTML5 Canvas: How to fake globalCompositeOperation=“darker” HTML5 Canvas冲突检测的“ globalCompositeOperation”性能 - HTML5 Canvas Collision Detection “globalCompositeOperation” performance HTML5 Canvas globalCompositeOperation同时更轻便且源代码优先 - HTML5 Canvas globalCompositeOperation lighter and source-atop at same time HTML5 Canvas 用于叠加梯度的 globalCompositeOperation 没有加起来达到更高的强度? - HTML5 Canvas globalCompositeOperation for overlaying gradients not adding up to higher intensity? 试图让“橡皮擦”与HTML5 Canvas和Vaadin一起使用,但是它只是清除了 - Trying to get an “eraser” working with the HTML5 Canvas and Vaadin, but it just clears 画布 HTML 5 中的橡皮擦 - Eraser in Canvas HTML 5 HTML Canvas - 背景橡皮擦无法单击一次 - HTML Canvas - background eraser not working on a single click JS:如何获取受支持的HTML画布globalCompositeOperation类型的列表 - JS: How to get list of supported HTML canvas globalCompositeOperation types 画布(带盖bg的属性globalCompositeOperation) - Canvas (property globalCompositeOperation with cover bg)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM