如何更改新图像对象的不透明度



我正试图降低画布上绘制的图像的不透明度,但不起作用

var img = new Image();
const canvas = document.getElementById("myCanvas");
const ctx = canvas.getContext('2d');
img.src = './picture.jpg';
canvas.width = img.width;
canvas.height = img.height;
img.style.opacity = 0.5;
ctx.drawImage(img, 30, 30, canvas.width, canvas.height);
/// only image will have alpha affected:
context.globalAlpha = 0.5;
context.drawImage(image, x, y);
context.globalAlpha = 1.0;

https://stackoverflow.com/a/18949160/9161582

最新更新