canvas fillStyle in interval with transparent/opacity

user951114 picture user951114 · Sep 18, 2011 · Viewed 40.8k times · Source

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?

Answer

Mubashar Shahzad picture Mubashar Shahzad · Feb 7, 2017

I think this will resolve your issue

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