Created
July 30, 2018 23:21
-
-
Save nbw/dfd534fbc280968dd417827b384c9750 to your computer and use it in GitHub Desktop.
threshold effect
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function(context, threshold, width, height) { | |
let image, data, r, g, b, color; | |
image = context.getImageData(0, 0, width, height); | |
data = image.data; | |
for (let i = 0; i< data.length; i = i+4) { | |
r = data[i]; | |
g = data[i+1]; | |
b = data[i+2]; | |
if ((r + b + g) / 3 < threshold) { | |
color = 0; // black | |
} else { | |
color = 255; // white | |
} | |
data[i] = data[i+1] = data[i+2] = color; | |
} | |
image.data = data; | |
context.putImageData(image, 0, 0); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment