Я пытаюсь написать класс javascript для области, содержащей 2 элемента холста, 1 для фона и 1 для переднего плана. Но когда я нажимаю на холсте я получаю сообщение об ошибке:HTML5 canvas + javascript class = error
«неперехваченным TypeError: Не удается прочитать свойство„DrawImage“неопределенных»
Любая помощь будет оценена ...
function GAMEAREA(canvasElement, bgElement)
{
this.canvas = document.getElementById(canvasElement);
this.context = this.canvas.getContext("2d");
this.bgCanvas = document.getElementById(bgElement);
this.bgContext = this.bgCanvas.getContext("2d");
this.pawnImg = document.getElementById("pawnImg");
this.init = function()
{
this.adjustSize();
this.canvas.addEventListener("mousedown", this.onClick, false);
};
this.onClick = function(e)
{
this.context.drawImage(this.pawnImg, 0, 0);
};
this.adjustSize = function()
{
this.canvas.height = window.innerHeight - ($("#headerDiv").outerHeight() + $("#footerDiv").outerHeight());
if (this.canvas.height > window.innerWidth) {
this.canvas.height = window.innerWidth;
this.canvas.width = this.canvas.height;
}
else this.canvas.width = this.canvas.height;
this.bgCanvas.height = this.canvas.height;
this.bgCanvas.width = this.canvas.width;
this.bgCanvas.style.display = "block";
this.canvas.style.display = "block";
};
this.init();
}
[Вот отличный вопрос] (http://stackoverflow.com/questions/3127429/javascript-this-keyword), в котором объясняется, как это работает в JavaScript. – voithos