-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中,您還可以在我的圖像中看到。 –