Skip to content

Instantly share code, notes, and snippets.

@saposki saposki/invert.js
Created May 1, 2016

Embed
What would you like to do?
//invert.js
$scope.addInvertFilter = function(){
var image = new Image();
image.src = $scope.userPost.imageURI;
var canvas = document.createElement('canvas');
canvas.height = canvas.width;
document.body.appendChild(canvas);
var ctx = canvas.getContext('2d');
ctx.drawImage(image, 0, 0);
image.style.display = 'none';
var imageData = ctx.getImageData(0,0, canvas.width, canvas.height);
var data = imageData.data;
for (var i = 0; i < data.length; i += 4) {
data[i] = 255 - data[i]; // red
data[i + 1] = 255 - data[i + 1]; // green
data[i + 2] = 255 - data[i + 2]; // blue
}
ctx.putImageData(imageData, 0, 0);
$scope.userPost.imageURI = canvas.toDataURL();
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.