a color change in transparency

2019-09-12 02:53发布

I have an image generated in the javascript HTML5 canvas. I would now like to say that all the px of a certain color (red for example) have all become transparent

1条回答
做个烂人
2楼-- · 2019-09-12 03:24
var imgd = context.getImageData(0,0, canvas.widht, canvas.height);
var pix = imgd.data;

// Loop over each pixel and set alpha channel to 255 for every red pixel
for (var i = 0; n = pix.length, i < n; i += 4) {
  if ( pix[i  ] > 240 && pix[i+1 ] < 15 && pix[i+2] < 15 ) // it is kind of red
      pix[i+3] = 255; // alpha channel
}

// Draw the ImageData object at the given (x,y) coordinates.
context.putImageData(imgd, 0,0);

I did not test the code but it should work (you have the global idea if it does not)

查看更多
登录 后发表回答