画布 fillStyle 间隔透明/不透明

发布于 2024-12-05 07:38:22 字数 399 浏览 7 评论 0原文

这个小代码间隔清除旧画布数据:

// start interval
ctx.save();
    ctx.fillStyle = "rgba(0, 0, 0, 0.2)";
    ctx.fillRect(0, 0, ctx.width, ctx.height);
ctx.restore();
//some draw code for new graph
...
//end interval

我的工作区域变黑,因为我将黑色设置为填充颜色(rgba(0, 0, 0, .2)),但我需要透明背景,不是黑色。

我尝试使用 globalAlphaimagePutData 但失败了。

我怎样才能做到这一点?

This small code clear old canvas data in interval:

// start interval
ctx.save();
    ctx.fillStyle = "rgba(0, 0, 0, 0.2)";
    ctx.fillRect(0, 0, ctx.width, ctx.height);
ctx.restore();
//some draw code for new graph
...
//end interval

My work area become black, because I set black as fill color (rgba(0, 0, 0, .2)), but I need a transparent background, not black.

I tried use globalAlpha and imagePutData but I failed.

How I can do this?

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

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

发布评论

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

评论(4

妥活 2024-12-12 07:38:22

我认为这会解决你的问题

ctx.fillStyle = "rgba(255, 255, 255, 0.5)";

I think this will resolve your issue

ctx.fillStyle = "rgba(255, 255, 255, 0.5)";
荒岛晴空 2024-12-12 07:38:22

使用 rgba(0,0,0,.2) fillStyle 和 fillRect() 在 chrome 和 firefox 上都适用 - 它会绘制半透明的黑色填充。检查以确保您没有做其他导致某种完全不透明的油漆的事情。

Using an rgba(0,0,0,.2) fillStyle and fillRect() works for me on both chrome and firefox - it paints a semi-transparent black fill. Check to make sure you're not doing something else that's causing a fully opaque paint of some sort.

西瑶 2024-12-12 07:38:22

尝试 ctx.canvas.width,而不是 ctx.width

try ctx.canvas.width, instead of ctx.width

千里故人稀 2024-12-12 07:38:22

您的问题解决了吗

我在使用 Windows 计算机时遇到了这个问题

我使用 opacity: .99 解决了这个问题

Has your problem been solved

I encountered this problem while using Windows computer

I used opacity: .99 solved this problem

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