Here’s what I’m trying to do:
- Get image A, and image B. Image B is a black and white mask image.
- Replace image A’s alpha channel with image B’s red channel.
- Draw image C on the canvas.
- Draw image A on top of image C.
Everything seems ok until step 4. Image C isn’t visible at all and where image A should be transparent there’s white color.
cx.putImageData(imageA, 0, 0);
var resultData = cx.getImageData(0, 0, view.width, view.height);
for (var h=0; h<resultData.data.length; h+=4) {
resultData.data[h+3] = imageB.data[h];
}
cx.putImageData(imageC, 0, 0);
cx.putImageData(resultData, 0, 0);
Simon is right: the
putImageDatamethod does not pay any attention to compositing; it merely copies pixel values. In order to get compositing, we need to use drawing operations.We need to mess with the channels (turn red into alpha) with the pixel data, put that changed pixel data into an image, and then use a composite operation to get the desired masking.
jsfiddle example
A doodle with the example: