Problems using HTML Canvas & Greasemonkey not able to drawImage() on 2D context

1

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?

javascript
firefox
html
canvas
greasemonkey
asked on Stack Overflow Jan 17, 2010 by Greg K • edited Jan 17, 2010 by Greg K

4 Answers

1

I should have looked more thoroughly on Google.

image.wrappedJSObject;

Works.

answered on Stack Overflow Jan 17, 2010 by Greg K
0
var canvas = document.createElement("canvas").wrappedJSObject,
ctx = canvas.getContext("2d");

canvas.width = image.width;
canvas.height = image.height;

ctx.drawImage(image, 0, 0);

this is what i did. hope it helps

answered on Stack Overflow Aug 7, 2010 by w35l3y
0

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.

answered on Stack Overflow Mar 15, 2011 by Alex Holsgrove
0

Working example for firefox 3.6.16 (I had similar problem):

http://userscripts.org/scripts/review/106800

answered on Stack Overflow Jul 15, 2011 by Mikhail Proxy

User contributions licensed under CC BY-SA 3.0