[html画布的context.drawImage()问题

问题描述 投票:-1回答:1

我正在使用画布,并且试图在画布上绘制图像,但是它不起作用。我发现我需要使用eventListener,但它没有解决它。有人可以告诉我怎么了。

const c = document.getElementById("myCanvas");
const ctx = c.getContext("2d");

const groundImg = new Image();
groundImg.src = "ground.png";

groundImg.addEventListener("load", startDraw());

function startDraw() {
    ctx.drawImage(groundImg, 0, 0, c.width, c.height);
}
javascript canvas drawimage
1个回答
0
投票

更改:

groundImg.addEventListener("load", startDraw());

至:

groundImg.addEventListener("load", startDraw);
© www.soinside.com 2019 - 2024. All rights reserved.