views:

703

answers:

2

I am trying to do some dynamic visual effects using the HTML 5 canvas' pixel manipulation, but I am running into a problem where setting pixels in the CanvasPixelArray is ridiculously slow.

For example if I have code like:

imageData = ctx.getImageData(0, 0, 500, 500);

for (var i = 0; i < imageData.length; i += 4){
    imageData.data[i] = buffer[i];
    imageData.data[i + 1] = buffer[i + 1];
    imageData.data[i + 2] = buffer[i + 2];
}

ctx.putImageData(imageData, 0, 0);

Profiling with Chrome reveals, it runs 44% slower than the following code where CanvasPixelArray is not used.

tempArray = new Array(500 * 500 * 4);
imageData = ctx.getImageData(0, 0, 500, 500);

for (var i = 0; i < imageData.length; i += 4){
    tempArray[i] = buffer[i];
    tempArray[i + 1] = buffer[i + 1];
    tempArray[i + 2] = buffer[i + 2];
}

ctx.putImageData(imageData, 0, 0);

My guess is that the reason for this slowdown is due to the conversion between the Javascript doubles and the internal unsigned 8bit integers, used by the CanvasPixelArray.

  1. Is this guess correct?
  2. Is there anyway to reduce the time spent setting values in the CanvasPixelArray?
A: 

Looks like you're doing some kind of "blitting", so maybe drawImage or all-at-once putImageData could help. Looping a quarter million times to copy pixels individually, rather than using massive "blitting" operations, tends to be much slower -- and not just in Javascript;-).

Alex Martelli
The actual drawing happens all at once when putImageData is called. The method which processes the buffer data by looping over each "pixel" (block in the buffer) and doing some advance operations on each of them is reasonably fast, at least MUCH faster than the method described which basically is intended to get the data onto the screen. It shocks me that processing the data takes a lot less time then simply copying it into the canvasPixelArray for drawing.
Nixuz
+2  A: 

Try caching a reference to the data pixel array. Your slowdown could be attributed to the additional property accesses to imageData.data. See this article for more explanation.

E.g. This should be faster that what you currently have.

var imageData = ctx.getImageData(0, 0, 500, 500),
    data = imageData.data,
    len = data.length;

for (var i = 0; i < len; i += 4){
 data[i] = buffer[i];
 data[i + 1] = buffer[i + 1];
 data[i + 2] = buffer[i + 2];
}

ctx.putImageData(imageData, 0, 0);
jimr
You will also find a reference to the above article on ajaxian, with more comments suggesting that it is an issue of reference access. http://ajaxian.com/archives/canvas-image-data-optimization-tip
jhominal