【发布时间】:2014-06-10 11:35:14
【问题描述】:
我正在尝试为包含 2 个画布元素的区域编写一个 javascript 类,1 个用于背景,1 个用于前景。 但是当我点击画布时,我得到了错误:
“未捕获的类型错误:无法读取未定义的属性 '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();
}
【问题讨论】:
-
Here's a great related question 解释了
this在 JavaScript 中的工作原理。
标签: javascript html class canvas drawimage