Skip to content

Instantly share code, notes, and snippets.

@nbw
Created July 30, 2018 23:21
Show Gist options
  • Save nbw/dfd534fbc280968dd417827b384c9750 to your computer and use it in GitHub Desktop.
Save nbw/dfd534fbc280968dd417827b384c9750 to your computer and use it in GitHub Desktop.
threshold effect
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