简体   繁体   中英

Uncaught TypeError: Type error when using onload

Ok I am stuck. I am trying to insert an image into a canvas and Chrome keeps giving me Uncaught TypeError: Type error for the onload method. Here is my javascript:

letterBase = new Image();
letterBase.onload = function() {
    context.drawImage(letterBase, 0, 0);
    console.log("drawing image");
  };
  letterBase.src = "/assets/letter/" + Character.letter + ".png";
  console.log(letterBase);

It is weird because I can see that the image is loading in the console from console.log(letterBase) but I really don't see what is going wrong here. Any ideas?

在此处输入图片说明

Edit: I don't think this is where the problem lies, but I have the code setting up the context as follows:

$(document).ready(function() {
    // some other init functions...
    canvas = document.getElementById("characterCanvas");
    context = canvas.getContext("2d");
    // ...

I figured out what the problem was. After setting the source and loading the images I was clearing the variable

letterBase.src = ""

which was causing the onload to fire a second time but this time with no source. Noob mistake.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM