Drawing PNG to a canvas element — not showing transparency

前端 未结 5 1138
耶瑟儿~
耶瑟儿~ 2020-12-16 10:24

I\'m trying to use drawImage to draw a semi-transparent PNG on a canvas element. However, it draws the image as completely opaque. When I look at the resource that\'s being

5条回答
  •  自闭症患者
    2020-12-16 11:04

    Don't forget to add an event listener to the image's load event. Image loading is something that happens in the background, so when the JavaScript interpreter gets to the canvas.drawImage part it is most likely the image probably will not have loaded yet and is just an empty image object, without content.

    drawing = new Image();
    drawing.src = "draw.png"; // can also be a remote URL e.g. http://
    drawing.onload = function() {
       context.drawImage(drawing,0,0);
    };
    

提交回复
热议问题