How do I make a transparent canvas in html5?

前端 未结 6 1508
执笔经年
执笔经年 2020-11-27 13:35

How can I make a canvas transparent? I need to because I want to put two canvases on top of one another.

相关标签:
6条回答
  • 2020-11-27 13:51

    Just set the background of the canvas to transparent.

    #canvasID{
        background:transparent;
    }
    
    0 讨论(0)
  • 2020-11-27 13:52

    Iif you want a particular <canvas id="canvasID"> to be always transparent you just have to set

    #canvasID{
        opacity:0.5;
    }
    

    Instead, if you want some particular elements inside the canvas area to be transparent, you have to set transparency when you draw, i.e.

    context.fillStyle = "rgba(0, 0, 200, 0.5)";
    
    0 讨论(0)
  • 2020-11-27 13:55

    I believe you are trying to do exactly what I just tried to do: I want two stacked canvases... the bottom one has a static image and the top one contains animated sprites. Because of the animation, you need to clear the background of the top layer to transparent at the start of rendering every new frame. I finally found the answer: it's not using globalAlpha, and it's not using a rgba() color. The simple, effective answer is:

    context.clearRect(0,0,width,height);
    
    0 讨论(0)
  • 2020-11-27 13:56

    Canvases are transparent by default.

    Try setting a page background image, and then put a canvas over it. If nothing is drawn on the canvas, you can fully see the page background.

    Think of a canvas as like painting on a glass plate.

    0 讨论(0)
  • 2020-11-27 14:05

    Can't comment the last answer but the fix is relatively easy. Just set the background color of your opaque canvas:

    #canvas1 { background-color: black; } //opaque canvas
    #canvas2 { ... } //transparent canvas
    

    I'm not sure but it looks like that the background-color is inherited as transparent from the body.

    0 讨论(0)
  • 2020-11-27 14:11

    Paint your two canvases onto a third canvas.

    I had this same problem and none of the solutions here solved my problem. I had one opaque canvas with another transparent canvas above it. The opaque canvas was completely invisible but the background of the page body was visible. The drawings from the transparent canvas on top were visible while the opaque canvas below it was not.

    0 讨论(0)
提交回复
热议问题