25.02.2013 Views

Peter Lubbers - Pro HTML 5 Programming

Pro HTML 5 Programming

Pro HTML 5 Programming

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Figure 8-4. <strong>HTML</strong>5 Web Worker–based web page with image-blurring filter<br />

CHAPTER 8 ■ USING THE <strong>HTML</strong>5 WEB WORKERS API<br />

This application sends image data from a canvas to several Web Workers (you can specify how<br />

many). The Web Workers then process the image with a simple box-blur filter. This may take several<br />

seconds, depending on the size of the image and the computational resources available (even machines<br />

with fast CPUs may have load from other processes, causing JavaScript execution to take more wallclock<br />

time to complete).<br />

However, because the heavy lifting takes place in Web Workers, there is no danger of slow-script<br />

warnings and, therefore, no need to manually partition the task into scheduled slices—something you<br />

would have to consider if you could not use Web Workers.<br />

Coding the blur.js Helper Script<br />

Inside the blur.js application page, we can use a straightforward implementation of a blur filter that<br />

loops until it has completely processed its input, as shown in Listing 8-4.<br />

201

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!