简体   繁体   English

clearRect()撤消画布的透明度

[英]clearRect() undoes transparency of canvas

I have two canvases overlaid on top of each other like so: 我有两张彼此重叠的画布,如下所示:

 <canvas id="myCanvas" style="z-index: 1; position:absolute; left:0px; top:0px; " width="720" height="480"></canvas> <canvas id="trailCanvas" style="z-index: 0; position:absolute; left:0px; top:0px; " width="720" height="480"></canvas> 

But the problem is that when I call clearRect() on the top most canvas, like so: 但是问题是,当我在最顶层的画布上调用clearRect()时,如下所示:

  ctx.clearRect(0, 0, canvas.width, canvas.height); 

then it makes my topmost canvas look grey not transparent anymore! 然后它使我最上面的画布看起来不再是灰色了! Isn't clearRect() supposed to make the whole canvas clear and transparent again? clearRect()是否应该使整个画布再次变得清晰透明? What's going on? 这是怎么回事? Would really appreciate any help! 非常感谢您的帮助!

Edit: Realized my mistake was in the CSS: 编辑:意识到我的错误是在CSS中:

  <style> * { padding: 0; margin: 0; } canvas { background: #eee; display: block; margin: 0 auto; } </style> 

which makes the canvas background grey instead of transparent. 这会使画布背景变成灰色而不是透明。

As illustrated by this fiddle , clearRect successfully resets the alpha. 该小提琴所示clearRect成功重置了Alpha。

Chances are your problem has other sources, one of the most common being adding a background-color to a canvas in the CSS. 您的问题可能还有其他来源,最常见的问题之一就是在CSS中向画布添加background-color To check for this, inspect the canvas with your devtools of choice, and see what the background properties are 要检查这一点,请使用您选择的devtools检查画布,并查看背景属性是什么。

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

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