getContext 不是一个函数

发布于 2024-11-03 12:51:34 字数 896 浏览 1 评论 0原文

我正在努力使用画布制作一个基本的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

内心激荡 2024-11-10 12:51:37

这是一个普通问题,所以要解决这个问题只需编写即可。

this.element = document.getElementById('whatever canvas id');
this.context = this.element.getContext("2d");

每次使用 this.context.drawImage() 时,我的意思是您正在使用的任何函数。
使用 this.context 而不是变量来使用它......因为它在我的中给出了错误。
因此,如果您使用纯 javascript、html 文件,请执行上述操作。

示例:

this.context.clearRect(0, 0, CANVAS_WIDTH, CANVAS_HEIGHT);

或者:

this.context.drawImage(playerImage, frameX, frameY, spriteWidth, spriteHeight, 0, 0, spriteWidth, spriteHeight);

像这样,我希望这对您有帮助

This is a Vanilla problem so to solve this just write.

this.element = document.getElementById('whatever canvas id');
this.context = this.element.getContext("2d");

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:

this.context.clearRect(0, 0, CANVAS_WIDTH, CANVAS_HEIGHT);

Or:

this.context.drawImage(playerImage, frameX, frameY, spriteWidth, spriteHeight, 0, 0, spriteWidth, spriteHeight);

Like this, I hope this will help you

淡看悲欢离合 2024-11-10 12:51:36

您的值:

this.element = $(id);

是一个 jQuery 对象,而不是纯 Canvas 元素。

要将其返回,以便您可以调用 getContext()、调用 this.element.get(0),或者更好的是存储真实元素而不是 jQuery 对象:

function canvasLayer(location, id) {

    this.width = $(window).width();
    this.height = $(window).height();
    this.element = document.createElement('canvas');

    $(this.element)
       .attr('id', id)
       .text('unsupported browser')
       .attr('width', this.width)       // for pixels
       .attr('height', this.height)
       .width(this.width)               // for CSS scaling
       .height(this.height)
       .appendTo(location);

    this.context = this.element.getContext("2d");
}

请参阅在 http://jsfiddle.net/alnitak/zbaMh/ 运行代码,最好使用 Chrome Javascript 控制台这样您就可以在调试输出中看到生成的对象。

Your value:

this.element = $(id);

is a jQuery object, not a pure Canvas element.

To turn it back so you can call getContext(), call this.element.get(0), or better yet store the real element and not the jQuery object:

function canvasLayer(location, id) {

    this.width = $(window).width();
    this.height = $(window).height();
    this.element = document.createElement('canvas');

    $(this.element)
       .attr('id', id)
       .text('unsupported browser')
       .attr('width', this.width)       // for pixels
       .attr('height', this.height)
       .width(this.width)               // for CSS scaling
       .height(this.height)
       .appendTo(location);

    this.context = this.element.getContext("2d");
}

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.

仙女山的月亮 2024-11-10 12:51:36

或者您可以使用:

this.element=$(id)[0];

Alternatively you can use:

this.element=$(id)[0];
潜移默化 2024-11-10 12:51:36

实际上,当我们在 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

潜移默化 2024-11-10 12:51:36

这可能看起来很明显,但您可能认为一切都是正确的,例如:带有 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.

蓝天白云 2024-11-10 12:51:36

我最近收到此错误,因为拼写错误,我写了“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.

温暖的光 2024-11-10 12:51:36

当您要求运行 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.

递刀给你 2024-11-10 12:51:36

该值将位于数组的第 0 个位置。
所以我们需要做 variable = $("#id") use variable[0]

The value will be at array 0th position.
So we need to do variable = $("#id") use variable[0]

西瓜 2024-11-10 12:51:35

我遇到了同样的错误,因为我不小心使用了

而不是 作为我尝试调用 getContext 的元素>。

I got the same error because I had accidentally used <div> instead of <canvas> as the element on which I attempt to call getContext.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文