开发者

html5 canvas drawImage without alpha

开发者 https://www.devze.com 2023-03-28 04:28 出处:网络
I\'d like to know if it possible to not开发者_开发知识库 use the alpha channel but keeping transparency.

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);
0

精彩评论

暂无评论...
验证码 换一张
取 消