首页 > 解决方案 > 如何更改新图像对象的不透明度

问题描述

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

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

标签: javascripthtmljavascript-objects

解决方案


/// 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


推荐阅读