-1
我有一张图像,我用ctx.drawImage绘图,它具有透明度。我想让图像变暗,所以我使用了rgba(0,0,0,0.5)的fillRect,但是也会使图像的透明部分变暗。所以我正在研究使用ctx.globalCompositeOperation ='atop atop'(包括使用ctx.save和还原),但现在使整个画布背景变成白色,并且只通过图像/ fillrect显示背景。JavaScript canvas使透明图像更暗
ctx.globalCompositeOperation = '目标之上' 或 '源式' 前:
后:
这里是我的代码:
/*above is drawing the background, I only want to merge the fillRect with the drawImage*/
ctx.save();
ctx.drawImage(o.image, x, y);
ctx.fillStyle = 'rgba(0,0,0,'+amount+')';
ctx.globalCompositeOperation = 'source-in';
ctx.fillRect(x, y, w, h);
ctx.globalCompositeOperation = 'source-over';
ctx.restore();
仍然使背景变白。它会使图像变暗而不透明,但会使背景变白。也许我应该把这些放在一个新的画布元素层上。 –
您没有提供足够的信息。上面的代码不能使背景变成白色,因为“multiply”会使所有像素变暗,而“destination-in”只会影响alpha通道。我将mod的回答使其更清晰 – Blindman67
好吧,我确实说过我正在绘制背景,因为在fillRect中,您还可以在我的图像中看到。 –