1

I'm using canvas and i'm trying to draw an image to the canvas but it doesn't work. I found that I need to use eventListener but it didn't fix it. can someone tell me what's wrong.

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);
}
Golden
  • 93
  • 9
AAA
  • 23
  • 5
  • Does this answer your question? [CanvasContext2D drawImage() issue \[onload and CORS\]](https://stackoverflow.com/questions/32880641/canvascontext2d-drawimage-issue-onload-and-cors) – Golden Feb 15 '20 at 12:32

1 Answers1

2

change:

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

to:

groundImg.addEventListener("load", startDraw);
Golden
  • 93
  • 9