How do you convert a color image to black/white using only Javascript?
AND, also make it cross compatible in most browsers because I hear Internet E
Some image filters are available in CSS and supported by all major browsers, but you can have much more options using the HTML5 Canvas and Javascript.
However, when using Canvas based image filtering, you don't need implement the filters algorithm by yourself. Just use an image processing or Canvas manipulation library.
Examples:
In the examples below I used MarvinJ.
Loading image:
image = new MarvinImage();
image.load("https://i.imgur.com/B33rKWi.png", imageLoaded);
Gray Scale:
Marvin.grayScale(image.clone(), image);
Black and White:
Marvin.blackAndWhite(image.clone(), image, 5);
Black and White 2:
Marvin.blackAndWhite(image.clone(), image, 40);
Halftone:
Marvin.halftoneErrorDiffusion(image.clone(), image);
Runnable Example:
var canvas = document.getElementById("canvas");
image = new MarvinImage();
image.load("https://i.imgur.com/B33rKWi.png", imageLoaded);
function imageLoaded(){
// GrayScale
//Marvin.grayScale(image.clone(), image);
//image.draw(canvas);
// Black and White
Marvin.blackAndWhite(image.clone(), image, 5);
image.draw(canvas);
// Black and White 2
//Marvin.blackAndWhite(image.clone(), image, 40);
//image.draw(canvas);
// Error Diffusion
//Marvin.halftoneErrorDiffusion(image.clone(), image);
//image.draw(canvas);
}