0

the error:

Uncaught TypeError: Failed to execute 'drawImage' on 'CanvasRenderingContext2D': The provided value is not of type '(CSSImageValue or HTMLCanvasElement or HTMLImageElement or HTMLVideoElement or ImageBitmap or OffscreenCanvas or SVGImageElement or VideoFrame)'.

here is the player value in the editor:

let player = {
    x: 100,
    y: 100,
    h: 0,
    vh: 0,
    v: 0,
    hp: 100,
    controls: {
        turnLeft: false,
        turnRight: false,
        accelerate: false,
        deccelerate: false,
    },
    sprite: document.getElementById("fighter3"),
}

here is the player value in the console:

controls: 
{turnLeft: false, turnRight: false, accelerate: false, deccelerate: false}
h: 0
hp: 100
sprite: null
v: 0
vh: 0
x: 100
y: 100

and here is the draw command:

ctx.drawImage(player.sprite, player.x, player.y);

I am trying to draw a sprite to canvas and when I use context.drawImage I get an error, I narrowed it down to the fact that document.getElementById("fighter3") returns null for some reason (the html id is correct).

  • 1
    Maybe your code runs in the ``, before the HTML element even exists? See https://stackoverflow.com/questions/14028959/why-does-jquery-or-a-dom-method-such-as-getelementbyid-not-find-the-element – CherryDT Oct 30 '22 at 20:36
  • thanks @CherryDT, the – Gabe9230 Oct 30 '22 at 20:41
  • 1
    Voted to close as a dupe based on comments. – Yogi Oct 30 '22 at 20:48

0 Answers0