Home > OS >  Js canvas context gives undefined in object
Js canvas context gives undefined in object

Time:06-27

So... I have this code here. It is meant to get the context of a canvas:

let room ={
    canvas: document.getElementById("room"),
    ctx: this.canvas.getContext("2d")
}

but it gives me this error: Cannot read property of undefined (reading 'canvas'). Why?

CodePudding user response:

Try Define The Canvas First

let cvs = document.getElementById("room");
let room = {
    ctx: cvs.getContext("2d")
}
  • Related