html5 canvas drawImage without alpha
I'd like to know if it possible to not开发者_开发知识库 use the alpha channel but keeping transparency. What I need is to draw or not draw pixel (of my png file) depending on the alpha value
If alpha channel > 128 draw Pixel else don't draw pixel !
Thank's
There are probably a few ways to go about that.
One way is just to draw the image, then call getImageData
Go over every pixel in the image data, and if the alpha component is <= 128, make that pixel fully transparent.
Then put the modified imageData back with putImageData
This is from memory so I might have missed something:
var imageData = ctx.getImageData(0,0,picwidth, picheight);
var pixels = imageData.data;
var numPixels = pixels.length;
ctx.clearRect(0, 0, can.width, can.height);
for (var i = 0; i < numPixels; i++) {
if (pixels[i*4+3] <= 128) pixels[i*4+3] = 0;
}
ctx.putImageData(imageData, 0, 0);
精彩评论