getContext 不是一个函数
我正在努力使用画布制作一个基本的 Web 应用程序,当窗口调整大小时,该应用程序会动态更改画布大小。我已经让它静态工作而没有任何缺陷,但是当我创建一个对象使其动态化时,它会
在 chrome 中抛出错误,错误是:
未捕获类型错误:对象 [object Object] 没有方法“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());
}
I'm working on making a basic web application using canvas that dynamically changes the canvas size when the window resizes. I've gotten it to work statically without any flaws but when I create an object to make it dynamically it throws an error
in chrome the error is:
Uncaught TypeError: Object [object Object] has no method 'getContext'
and in firefox it is:
this.element.getContext is not a function
I've searched the web and it seems like a common problem but none of the fixes mentioned make any difference.
The code is in question is as follows:
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());
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
这是一个普通问题,所以要解决这个问题只需编写即可。
每次使用 this.context.drawImage() 时,我的意思是您正在使用的任何函数。
使用
this.context
而不是变量来使用它......因为它在我的中给出了错误。因此,如果您使用纯 javascript、html 文件,请执行上述操作。
示例:
或者:
像这样,我希望这对您有帮助
This is a Vanilla problem so to solve this just write.
And every time use
this.context.drawImage()
I mean whatever functions you are using.Use it using
this.context
instead of variable ...because its giving error in mine.So if you are using pure javascript,html file then do the above.
Example:
Or:
Like this, I hope this will help you
您的值:
是一个 jQuery 对象,而不是纯 Canvas 元素。
要将其返回,以便您可以调用
getContext()
、调用this.element.get(0)
,或者更好的是存储真实元素而不是 jQuery 对象:请参阅在 http://jsfiddle.net/alnitak/zbaMh/ 运行代码,最好使用 Chrome Javascript 控制台这样您就可以在调试输出中看到生成的对象。
Your value:
is a jQuery object, not a pure Canvas element.
To turn it back so you can call
getContext()
, callthis.element.get(0)
, or better yet store the real element and not the jQuery object:See running code at http://jsfiddle.net/alnitak/zbaMh/, ideally using the Chrome Javascript Console so you can see the resulting object in the debug output.
或者您可以使用:
Alternatively you can use:
实际上,当我们在 JavaScript 中创建画布时,也会遇到此错误,如下所示。
document.createElement('canvas');
这里需要注意的是,我们必须正确提供参数名称作为“画布”而不是其他任何东西。
谢谢
Actually we get this error also when we create canvas in javascript as below.
document.createElement('canvas');
Here point to be noted we have to provide argument name correctly as 'canvas' not anything else.
Thanks
这可能看起来很明显,但您可能认为一切都是正确的,例如:带有 id 的 canvas 元素,但在我的例子中,我也有一个具有相同 id 的 div 元素,这导致了我的错误。
This might seem obvious, but you might think you have everything correct, eg: canvas element with an id, but in my case, I had a div element with the same id as well, which caused my error.
我最近收到此错误,因为拼写错误,我写了“canavas”而不是“canvas”,希望这可以帮助正在搜索此内容的人。
I recently got this error because the typo, I write 'canavas' instead of 'canvas', hope this could help someone who is searching for this.
当您要求运行 getContext 函数的元素不是真正定义的画布或根本不是画布时,就会发生此错误,因此您必须检查您的元素。
This error happens when the element you asked to run
getContext
function for isn't true defined canvas or isn't canvas at all so you must check your element.该值将位于数组的第 0 个位置。
所以我们需要做
variable = $("#id") use variable[0]
The value will be at array 0th position.
So we need to do
variable = $("#id") use variable[0]
我遇到了同样的错误,因为我不小心使用了
而不是
作为我尝试调用
getContext
的元素>。I got the same error because I had accidentally used
<div>
instead of<canvas>
as the element on which I attempt to callgetContext
.