【发布时间】:2011-08-14 01:40:34
【问题描述】:
我正在使用画布制作一个基本的 Web 应用程序,该应用程序可以在窗口调整大小时动态更改画布大小。我已经让它静态工作而没有任何缺陷,但是当我创建一个对象以使其动态生成时,它会引发错误
在 chrome 中的错误是:
Uncaught TypeError: Object [object Object] has no method 'getContext'
在 Firefox 中是:
this.element.getContext 不是函数
我在网上搜索过,这似乎是一个常见问题,但提到的修复都没有任何区别。
有问题的代码如下:
layer['background'] = new canvasLayer("body","background");
function canvasLayer(location,id){
$(location).append("<canvas id='"+id+"'>unsupported browser</canvas>");
this.element=$(id);
this.context = this.element.getContext("2d"); //this is the line that throws the error
this.width=$(window).width(); //change the canvas size
this.height=$(window).height();
$(id).width($(window).width()); //change the canvas tag size to maintain proper scale
$(id).height($(window).height());
}
提前致谢。
【问题讨论】:
标签: javascript jquery html canvas