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?

Greg K
  • 10,770
  • 10
  • 45
  • 62
  • I've not appended this canvas element to the DOM / body - would this cause a problem? – Greg K Jan 17 '10 at 14:09
  • Looking at other example code I tried `unsafeWindow.document.createElement("canvas")` but never managed to get this working correctly. Changed my approach, a bit more clumsey, re-requested the image via AJAX with forced MIME type to "text" to read the image data. – Greg K Jan 17 '10 at 18:02
  • I can't reproduce any bug, because I don't know what `image` is. – NVI Jan 21 '10 at 20:11
  • It's just a regular image in an HTML document, mine was 100 x 80 pixels. – Greg K Jan 24 '10 at 17:09
  • Are you sure the image has finished loading? In other words: is image.complete? It looks like the image data is NOT_AVAILABLE. –  Aug 26 '10 at 05:31

4 Answers4

1

I should have looked more thoroughly on Google.

image.wrappedJSObject;

Works.

Greg K
  • 10,770
  • 10
  • 45
  • 62
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

w35l3y
  • 8,613
  • 3
  • 39
  • 51
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.

Alexander Holsgrove
  • 1,795
  • 3
  • 25
  • 54
0

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

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