Skip to content

Instantly share code, notes, and snippets.

@plugn
Last active August 29, 2015 14:15
Show Gist options
  • Save plugn/d49c6ea0f48e72e547dc to your computer and use it in GitHub Desktop.
Save plugn/d49c6ea0f48e72e547dc to your computer and use it in GitHub Desktop.
canvas filter
function process(img) {
var oCanvas = document.getElementById('canvas'),
oCtx = oCanvas.getContext("2d"),
width, height, imgData, dataURL;
width = img.offsetWidth;
height = img.offsetHeight;
oCanvas.width = width;
oCanvas.height = height;
oCtx.filter = 'invert(100%)';
oCtx.drawImage(img, 0, 0);
// ImageData.data is PNG BLOB usable as shown @ http://stackoverflow.com/a/26595628
// imgData = oCtx.getImageData(0, 0, width, height);
dataURL = oCanvas.toDataURL();
return dataURL;
}
function start() {
var src = document.querySelector('img'),
pre = document.querySelector('pre');
if (src) {
pre.innerHTML = process(src);
} else {
console.warn(' Something Went Wrong ');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment