使用 HTML Canvas 的问题Greasemonkey 无法在 2D 上下文中绘制Image()
在我的 Greasemonkey 脚本中,当我获取要与 HTML Canvas 一起使用的 HTMLImageElement 的句柄时,我在 Firefox 的错误控制台 中收到以下错误(我认为这是因为它包含在 XPCNativeWrapper 中):
Error: Component returned failure code: 0x80040111 (NS_ERROR_NOT_AVAILABLE)
[nsIDOMCanvasRenderingContext2D.drawImage]
在我的代码中放置 GM_log()
语句,我已经跟踪了我尝试使用的图像对象,从它的初始分配一直到我尝试将它与 HTML Canvas 一起使用。
它总是包装在 XPCNativeWrapper 中:
[object XPCNativeWrapper [object HTMLImageElement]]
我通过使用 image.wrappedJSObject
获取对 HTMLImageElement
的引用来解开 HTMLImageElement
的包装。
我的画布代码:
var canvas = document.createElement("canvas");
canvas.width = image.width;
canvas.height = image.height;
var ctx = canvas.getContext("2d");
ctx.drawImage(image, 0, 0);
您知道为什么 Firefox 会抛出上述组件故障代码吗?
In my Greasemonkey script, when I obtain a handle on an HTMLImageElement I want to use with an HTML Canvas, I get the following error in Firefox's Error Console (I assume it's because it's enclosed in an XPCNativeWrapper):
Error: Component returned failure code: 0x80040111 (NS_ERROR_NOT_AVAILABLE)
[nsIDOMCanvasRenderingContext2D.drawImage]
Putting GM_log()
statements throughout my code, I have traced the image object I'm trying to use from it's initial assignment through until I try to use it with an HTML Canvas.
It's always wrapped in an XPCNativeWrapper:
[object XPCNativeWrapper [object HTMLImageElement]]
I've unwrapped the HTMLImageElement
by obtaining reference to it with image.wrappedJSObject
.
My canvas code:
var canvas = document.createElement("canvas");
canvas.width = image.width;
canvas.height = image.height;
var ctx = canvas.getContext("2d");
ctx.drawImage(image, 0, 0);
Any ideas why Firefox is throwing the above component failure code?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我应该更彻底地在谷歌上查看。
作品。
I should have looked more thoroughly on Google.
Works.
这就是我所做的。希望有帮助
this is what i did. hope it helps
我刚刚遇到了同样的问题。当我对图像使用绝对/完整 URL 时,错误消失了。另外,正如其他人指出的那样,请确保首先加载图像,或者首先在 javascript 中创建一个新的 Image() 。
I just had the same problem. The error went away when I used an absolute/complete URL for the image. Also, as someone else had noted, make sure the image is loaded first of all, or just create a new Image() in javascript first of all.
firefox 3.6.16 的工作示例(我有类似的问题):
http://userscripts.org/scripts/review /106800
Working example for firefox 3.6.16 (I had similar problem):
http://userscripts.org/scripts/review/106800