有没有办法将不透明度设置为画布中的0,而不是用颜色绘制?

发布于 2025-02-13 05:23:14 字数 457 浏览 1 评论 0原文

我有以下功能:

function draw(event) {
        context.beginPath();
        context.lineWidth = 20;
        context.lineCap = 'round';
        context.strokeStyle = 'rgb(255, 255, 255)';
        context.moveTo(coord.x, coord.y);
        reposition(event);
        context.lineTo(coord.x, coord.y);
        context.stroke();
    }

我想知道是否有可能(而不是在震动中具有颜色),而是将不透明度降低到0,以便我本质上是“擦除”帆布元素并显示出的元素后面。

很抱歉,如果以前有人问过,我在任何地方都找不到任何答案。

I have the following function:

function draw(event) {
        context.beginPath();
        context.lineWidth = 20;
        context.lineCap = 'round';
        context.strokeStyle = 'rgb(255, 255, 255)';
        context.moveTo(coord.x, coord.y);
        reposition(event);
        context.lineTo(coord.x, coord.y);
        context.stroke();
    }

And I would like to know if it's possible to, instead of having a color in the strokeStyle, to just reduce the opacity to 0 so that I am essentially "erasing" the canvas element and show the element that is behind it.

I am sorry if this has been asked before, I couldn't find any answer to this anywhere.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

属性 2025-02-20 05:23:14

为此,您需要将GlobalComposeporation设置为一种操作类型,该操作类型在画出画布上时会删除现有颜色。
的小提琴: https://jsfiddle.net/twc1xern/

我做了一个说明这一点 画布,一个在另一个面前。前画布充满红色,后帆布设置为蓝色。设置操作时:

foreground.globalCompositeOperation = 'destination-out'

绘制一条线从前景画布上删除颜色数据,显示下面的蓝色画布。

To do this, you need to set the globalCompositeOperation to an operation type that removes the existing colors when something is drawn to the canvas.
I've made a fiddle that demonstrates this: https://jsfiddle.net/twc1xern/

It has two canvases, one in front of the other. The front canvas is filled with red and the back canvas is set to blue. When the operation is set:

foreground.globalCompositeOperation = 'destination-out'

Drawing a line removes the color data from the foreground canvas, showing the blue canvas underneath.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文