This is the bocoup web log with posts from Al, Boaz, Rick, Sam, Nate, Nick & Pete. You should also make sure to checkout code.bocoup.com, where we keep the finished versions of ideas we kick around here.


Jun 17, 2010

Processing Pixel Data with Web Workers…

One of the questions that came up in the Processing track was: “Can you pass the image data from a Canvas to a Web-Worker for processing?” My assumption was “yes” as the specs declare you can paste any complex object to and from workers so long as you’re not trying to pass a DOM element.

But then again… you never know what you can and cant do with all these security folk lurking around on browser development teams — making life difficult for the unclean web developer massive. So I went about creating a quick demo to prove, or disprove the point.

 
  // The script inside my HTML document...
 
  // Create a new Worker thread
  var myPixelWorker = new Worker( 'pixelWorker.js' );
 
  // When the Worker posts a message back to the Window
  myPixelWorker.onmessage = function( event ){
 
    // Log the message that was passed back
    console.log( event.data );
 
  };
 
  // When the Document has loaded...
  addEventListener( 'DOMContentLoaded', function(){ 
 
    // Get the Canvas and Context
    var canvas = document.getElementById( 'myCanvas' );
    var context = canvas.getContext( '2d' );
 
    // Paint the Canvas green
    context.fillStyle = "rgb(0,255,0);"
    context.fillRect( 0, 0, 10, 10 );
 
    // Get 100 pixels of the green Canvas
    var data = context.getImageData( 0, 0, 10, 10 ).data;
 
    // Post the 100 pixel array to the Worker thread
    myPixelWorker.postMessage( data );
 
  }, false );

The script creates a Worker thread from the file “pixelWorker.js” and then adds an onmessage handler for logging what was passed between the Window and the Thread. The when the document has loaded, the JavaScript paints the canvas green, grabs the pixel data from the canvas, and passes the data to the worker.

 
  // The script inside my Worker thread
 
  onmessage = function( event ){
 
    postMessage( event.data );
 
  }

The worker then takes the incoming message and fires it straight back to the Window where the result is logged in the Firebug console. Here are the results:

[0, 255, 0, 255, 0, 255, 0, 255, 0, 255, 0, 255, 0, 255, 0, 255, 0, 255, 0, 255, 0, 255, 0, 255, 0, 255, 0, 255, 0, 255, 0, 255, 0, 255, 0, 255, 0, 255, 0, 255, 0, 255, 0, 255, 0, 255, 0, 255, 0, 255, 0, 255, 0, 255, 0, 255, 0, 255, 0, 255, 0, 255, 0, 255, 0, 255, 0, 255, 0, 255, 0, 255, 0, 255, 0, 255, 0, 255, 0, 255, 0, 255, 0, 255, 0, 255, 0, 255, 0, 255, 0, 255, 0, 255, 0, 255, 0, 255, 0, 255, 0, 255, 0, 255, 0, 255, 0, 255, 0, 255, 0, 255, 0, 255, 0, 255, 0, 255, 0, 255, 0, 255, 0, 255, 0, 255, 0, 255, 0, 255, 0, 255, 0, 255, 0, 255, 0, 255, 0, 255, 0, 255, 0, 255, 0, 255, 0, 255, 0, 255, 0, 255, 0, 255, 0, 255, 0, 255, 0, 255, 0, 255, 0, 255, 0, 255, 0, 255, 0, 255, 0, 255, 0, 255, 0, 255, 0, 255, 0, 255, 0, 255, 0, 255, 0, 255, 0, 255, 0, 255, 0, 255, 0, 255, 0, 255, 0, 255, 0, 255, 0, 255, 0, 255, 0, 255, 0, 255, 0, 255, 0, 255, 0, 255, 0, 255, 0, 255, 0, 255, 0, 255, 0, 255, 0, 255, 0, 255, 0, 255, 0, 255, 0, 255, 0, 255, 0, 255, 0, 255, 0, 255, 0, 255, 0, 255, 0, 255, 0, 255, 0, 255, 0, 255, 0, 255, 0, 255, 0, 255, 0, 255, 0, 255, 0, 255, 0, 255, 0, 255, 0, 255, 0, 255, 0, 255, 0, 255, 0, 255, 0, 255, 0, 255, 0, 255, 0, 255, 0, 255, 0, 255, 0, 255, 0, 255, 0, 255, 0, 255, 100 more...]

BINGO! Kind of obvious, but it needed to be blogged somewhere… that passing Canvas pixels to a Worker thread, definitely does work. :)



Comments:

5 Comments

  1. Posted June 17, 2010 at 6:39 pm | Permalink

    This is excellent news. As for your choice of event attribute assignment, we’ll discuss that tomorrow at the Loft.

  2. Posted June 17, 2010 at 6:39 pm | Permalink

    Nice one! so much potential and promise with HTML5.

  3. Posted June 17, 2010 at 6:58 pm | Permalink

    Hahah, I *knew* you’d pick up on that! ;)

  4. Posted June 17, 2010 at 7:34 pm | Permalink

    Nice write-up. I used a web worker thread to process image data and generate a histogram in this demo.

    While developing this, I discovered that you cannot pass using postMessage() the ImageData object returned by context.getImageData(), as that is considered a DOM element. As your example shows, you must post the members of the ImageData object.

  5. Posted June 18, 2010 at 12:27 pm | Permalink

    @Joe Turgeon, good point, I forgot to mention that. Very nice demo too btw!

    @eepmon, yeah totally agree, HTML5 is changing the way we work and play.

One Trackback

  1. [...] This post was mentioned on Twitter by Alistair MacDonald, Bocoup, LLC. Bocoup, LLC said: Processing Canvas pixel data in a Worker thread definitely does work: http://weblog.bocoup.com/processing-pixel-data-with-web-workers [...]




Please send your questions to this address or call Bocoup at 617-379-2752
This web page is proudly maintained by Bocoup and hosted by (mt) Media Temple
All code on this website is Open Source