简体   繁体   English

canvas fillStyle 间隔透明/不透明

[英]canvas fillStyle in interval with transparent/opacity

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.我的工作区域变成黑色,因为我将黑色设置为填充颜色( rgba(0, 0, 0, .2) ),但我需要一个透明的背景,而不是黑色。

I tried use globalAlpha and imagePutData but i failed.我尝试使用 globalAlpha 和 imagePutData 但我失败了。

How i can do this?我怎么能做到这一点?

我认为这将解决您的问题

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

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.使用rgba(0,0,0,.2) fillStyle 和fillRect()在 chrome 和 firefox 上都适用于我 - 它绘制了半透明的黑色填充。 Check to make sure you're not doing something else that's causing a fully opaque paint of some sort.检查以确保您没有做其他导致某种完全不透明油漆的事情。

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

Has your problem been solved你的问题解决了吗

I encountered this problem while using Windows computer我在使用 Windows 计算机时遇到了这个问题

I used opacity: .99 solved this problem我用opacity: .99解决了这个问题

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM