1

I'm using Webworker to process array of pixels from Canvas and after returning it back - assign to ImageData array. Firefox works great, but Chromium puts an empty pixel array to the Canvas. Investigation showed that array copying isnt working, resulting array has nulled elements. Array slicing didnt help either, only going through each element with for in helped, but I wonder what is the problem here?

imgd = ctx.createImageData(w,h);
worker.onmessage = function (e) {
  imgd.data = e.data; 
  console.log(imgd.data === e.data); // true in FF, false in Chromium  

  img.data = e.data.slice(0); 
  console.log(imgd.data); // correct in FF, empty array in Chromium
};
  • Are you passing `e` and setting `e.data`, or is this a custom data property of an event object that is unrelated to image data? – Phrogz Dec 07 '10 at 15:13
  • http://stackoverflow.com/questions/7072295/use-web-worker-to-getimagedata-from-a-file This will solve – kongaraju Aug 06 '12 at 10:36

3 Answers3

3

Chrome:

> var e = document.createElement('canvas').getContext('2d').createImageData(10, 10).data;
undefined
> Object.prototype.toString.call(e)
"[object ImageData]"
> e.slice
undefined

FX4:

>>> var e = document.createElement('canvas').getContext('2d').createImageData(10, 10).data;
undefined
>>> Object.prototype.toString.call(e)
"[object Uint8ClampedArray]"
>>> e.slice
slice()

Opera:

>>> var e = document.createElement('canvas').getContext('2d').createImageData(10, 10).data;
undefined
>>> Object.prototype.toString.call(e)
"[object CanvasPixelArray]"
>>> e.slice
undefined

That tells us what? Well the spec nowhere states that the pixel data array should have array like methods.

Ergo, only use .slice when available, otherwise do a for loop copy, oh and also test in more than 2 Browsers.

Ivo Wetzel
  • 46,459
  • 16
  • 98
  • 112
0

Calling slice with just the start parameter is a Spidermonkey extension. Try specifying the start and end instead:

e.data.slice(0, e.data.length)

Edit    createImageData returns an ImageData object which attributes are read-only. So you can’t change data. Use createImageData to create an ImageData object in your CanvasRenderingContext2D from another ImageData object:

worker.onmessage = function (e) {
    ctx.createImageData(e.data);
};
Gumbo
  • 643,351
  • 109
  • 780
  • 844
  • same result, empty array in Chrome –  Dec 07 '10 at 13:37
  • @user525032: It seems that the issue is somewhere else. See my edit. – Gumbo Dec 07 '10 at 13:43
  • thanks, so that means FF implementation is not correct (according to specs) http://jsfiddle.net/rCZjL/1/ (works in FF, doesnt work in Chrome) –  Dec 07 '10 at 13:43
0

Instead of createImageData(), I think you want to use getImageData(). (That's Mozilla documentation but Chrome also has this method on contexts.)

Himanshu
  • 31,810
  • 31
  • 111
  • 133
Jess Austin
  • 338
  • 2
  • 11