HTML and CSS Reference
In-Depth Information
var imageData = JSON.parse(event.data),
worker = null;
pendingWorkers = getNumberOfWorkers(imageData.width
¬ / workingWidth);
// reset any old results
results = {};
for (var i = 0; i < pendingWorkers; i++) {
worker = new Worker('photofilter.js');
worker.postMessage(JSON.stringify({
imageData: imageData,
x: i * workingWidth,
width: workingWidth
}));
worker.onmessage = storeResult;
}
};
function storeResult(event) {
var result = JSON.parse(event.data);
buildUpImageData(result);
pendingWorkers--;
if (pendingWorkers <= 0) {
postMessage(JSON.stringify(results));
}
}
When the message is received from the sub-worker, the main
worker above decreases the number of outstanding sub-workers.
Once all the sub-workers have returned their slice of the image
data, the flnal result is returned to the parent document.
The sub-worker, photofilter.js worker, would contain the following
code to handle processing just a small region of the image data:
onmessage = function (event) {
var data = JSON.parse(event.data);
// perform some amaing feat of image processing
var imageData = amaingImageProcess(data.imageData,
¬ data.x, data.width);
postMessage(JSON.stringify({
 
Search WWH ::




Custom Search