更新时间:2023-11-24 10:27:46
问题是 image
异步地,所以如果你设置源并绘制它立即然后图像位还不可用,没有绘制。
The problem is that image
is loaded asynchronously, so if you set the source and draw it immediately then the image bits are not yet available and nothing is drawn.
您必须等待图像加载之前
You have to wait for the image to load before being able to draw it on a canvas.
将代码更改为
image.onload = function() {
ctx.drawImage(image, 5, 5);
};
image.src = "a.png";
应按预期工作。