Dennis Grinch Dennis Grinch - 6 months ago 19
Javascript Question

Uncaught TypeError: Cannot read property 'getContext' of undefined

I get this error "Uncaught TypeError: Cannot read property 'getContext' of undefined " while running script. It seems that variable "canvas" is undefined but I can't figure out why.

var world = {
canvas: document.getElementById("myCanvas"),
context: this.canvas.getContext("2d"),
centerX: this.canvas.width / 2,
centerY: this.canvas.height / 2,
drawShape: function (shape) {
if (typeof shape.draw() === "function")
shape.draw();
}
};

Answer

I declared variable canvas outside of world literal and it is working