Skip to content

Instantly share code, notes, and snippets.

@fightbulc
Created October 9, 2012 14:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fightbulc/3859044 to your computer and use it in GitHub Desktop.
Save fightbulc/3859044 to your computer and use it in GitHub Desktop.
canvas transparency
canvas = document.getElementById('canvasImage');
context = canvas.getContext('2d');
containerWidth = window.innerWidth;
containerHeight = 90;
// set width/height
context.canvas.width = containerWidth;
context.canvas.height = containerHeight;
// set image
image = new Image();
imageWidth = containerWidth;
imageHeight = containerHeight*2;
image.onload = function() { context.drawImage(image, 0, 0, imageWidth, imageHeight) }
image.src = '<?php echo $sourceImage ?>';
// get image pixels
imgData = context.getImageData(0, 0, imageWidth, imageHeight);
pixels = imgData.data;
i = 0;
length = pixels.length;
// Loop over each pixel and invert the color.
while(i<length) {
red = pixels[i];
green = pixels[i+1];
blue = pixels[i+2];
greyRgb = '239:239:239';
currentRgb = red+':'+green+':'+blue;
if(i < 100)
{
log(['RGB', greyRgb, currentRgb, pixels[i+3]]);
}
if(greyRgb == currentRgb) {
pixels[i+3] = 0;
}
// next pixel
i += 4;
}
// Draw the
context.putImageData(imgData, 0, 0);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment