ChatGPT解决这个技术问题 Extra ChatGPT

How do I make a transparent canvas in html5?

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


O
Omiod

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.

To clear a canvas after having drawn on it, just use clearRect:

const context = canvas.getContext('2d');
context.clearRect(0, 0, canvas.width, canvas.height);

Canvas has a black background on mobile so layering canvases doesn't work there. (At least on Chrome for Android)
So much for standards, as usual.
I think these answers are missing the point. Similar to the above question, I want to have two layered canvases: the bottom one has a static image, but the top one will have animated sprites. This top layer needs to have a transparent background, but also has to be 'cleared' and redrawn with every animation frame. Yes, it starts transparent by default, but how do you reset it to transparent and the start of every new animation frame?
This is how to clear a canvas anytime : stackoverflow.com/questions/2142535/…
Since you got soo many upvotes Can you at least answer the question too? How do you make a non transparent canvas transparent?
J
J Sprague

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);

s
stecb

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)";

FYI: opacity changes will have no effect if there is a background fill in the canvas.
C
Community

Just set the background of the canvas to transparent.

#canvasID{
    background:transparent;
}

This answer does not work in all situations, context.clearRect(0, 0, width, height) is the solution that worked for me
C
Chris

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.


1
1000Gbps

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.


S
Samuel

fillStyle might not be what you are looking for because it can't really clear the canvas; it will either paint it with a solid color or with a transparent color which doesn't paint anything.

The trick that did for me relies on an implementation detail about the <canvas></canvas>. They "reset" when resized (tested on Chrome and Firefox):

canvas.width = canvas.width

This phenomenon initially struck me as a very annoying behavior, but it also became the only way I know to hard reset the canvas.


T
Thales Kenne

If you're exporting your canvas, remember to export as png!!

Been there, failed at that xD


g
ggorlen

Here's a minimal proof of concept of the default transparency of canvases, and using position: absolute to stack them on top of each other:

const canvases = [...Array(4)] .map(() => document.createElement("canvas")); canvases.forEach((canvas, i) => { document.body.appendChild(canvas); const ctx = canvas.getContext("2d"); const saturation = 100 / canvases.length * (i + 1); ctx.strokeStyle = `hsl(160, ${saturation}%, 60%)`; ctx.lineWidth = 10; ctx.strokeRect(i * 50 + 10, i * 15 + 10, 100, 80); }); canvas { position: absolute; border: 1px solid black; }