views:

216

answers:

2

Are web workers allowed to access a canvas object?

+1  A: 

No.

The postMessage spec was updated a few months back to allow you to post ImageData objects but as yet no one has implemented that behaviour (we're all getting there). The problem with canvas itself is that it's a DOM element and so doesn't work in a worker (there's no DOM).

This was raised recently on either the whatwg or web-apps mailing lists so i suspect we'll start looking at whether it's possible to provide a CanvasRenderingContext2D-like api in workers.

olliej
I was under the impression the WebWorker wouldn't allow any interaction with a DOM as that would run into possible problems if multiple webworkers are making changes.
James Black
The problem is that the DOM has no concept of concurrency, so Workers don't allow any shared state. The only way of communicating with a worker is with postMessage, and that performs a clone according to the "internal structured cloning algorithm" which can basically be thought of as JSON but with additional support for a few key types (File, FileList, ImageData, Blob, Date and RegExp)
olliej
+3  A: 

Small update, as the question is now more than half a year old:

In Chrome/Chromium 6 you can now send a canvas' ImageData object to a web worker, let the web worker make changes to the object and then write it back to the canvas using putImageData(..).

Google's Chromabrush does it this way, the source-code can be found here:

Update:

The latest development snapshots of Opera (10.70) and Firefox (4.0b1) also support passing ImageData objects to a web worker.

toobb