我正在使用画布,并且试图在画布上绘制图像,但是它不起作用。我发现我需要使用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);
}
更改:
groundImg.addEventListener("load", startDraw());
至:
groundImg.addEventListener("load", startDraw);