Skip to content Skip to sidebar Skip to footer

A Color Change In Transparency

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

Solution 1:

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)


Post a Comment for "A Color Change In Transparency"