Dennis Grinch Dennis Grinch - 1 year ago 177
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")

Answer Source

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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download