I am making a very simple game engine, and I'm working on a function to make sprites and draw them onto the canvas, but it seems to not draw. The image loads and it shows up in the chrome sources, but does not want to draw.
Here is the error:
Neutrino.js:44 Uncaught TypeError: Failed to execute 'drawImage' on 'CanvasRenderingContext2D': The provided value is not of type '(CSSImageValue or HTMLImageElement or SVGImageElement or HTMLVideoElement or HTMLCanvasElement or ImageBitmap or OffscreenCanvas)' at Image.Sprite.img.onload
And here is my code:
var canvas = document.createElement("canvas");
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
document.body.appendChild(canvas);
var c = canvas.getContext("2d");
var Sprite = function(src,x,y,w,h,ctx) {
this.x = x;
this.y = y;
this.w = w;
this.h = h;
this.src = src;
this.img = new Image(this.w,this.h);
this.img.onload = function() {
ctx.drawImage(this.img,this.x,this.y);
};
this.img.src = this.src;
return this;
}
var puppy = new Sprite("puppy.jpg",100,100,100,100,c);
"puppy.jpg" is in the current web directory also.