Vanilla JavaScript canvas images to black and white

I'm a full-stack developer from South Africa 🇿🇦. I love writing about JavaScript, HTML and CSS.
Search for a command to run...

I'm a full-stack developer from South Africa 🇿🇦. I love writing about JavaScript, HTML and CSS.
Awesome glad you find them useful 🔥
In this series we will get started with the canvas element and use JavaScript to make some amazing art.
Lately, we have been on a journey with Canvas and have learned the following elements to it: Getting started with the HTML canvas Vanilla JavaScript save canvas as an image Vanilla JavaScript colouring our canvas elements 🌈 Vanilla JavaScript image...
Most of you know me for my consistency, a golden arrow in my blog series. I've written 1000 articles in 1008 days! Almost an article a day, and my honeymoon was the only holiday I ever took. I'm super proud of this achievement; it has been a fantasti...

It's not the first time I'll be talking about community. I think it's an essential aspect of any successful tool. This shows in my previous explorations of Astro, Medusa, and now Vendure as well. All these products thrive in a super open, welcoming, ...

The cool part about Vendure is how easy it is to set up and how abstract each layer is. Basically, we get the following elements: External database Server Worker Admin UI Frontend While this is amazing, it also brings a bit of complexity when it co...

The previous article looked at customizing Vendure on a data and process level. In this article, we'll look at customizing emails, as they are often a big part of a webshop system. We'll be looking at two different layers of customization for customi...

Even though Vendure is a pretty significant project out of the box, in some cases, we might want to go in and modify some elements to work to our specific use case. In this article, I'll take a high-level look at some elements we can customize within...

Yesterday, we saw how to use images on our canvas and even invert the colours.
But what if we want to convert them to only three colour options?
The colour options we will be using are;
This will abstract our image and teaches us how to create grayscale images manually.
Today's end result will look like this:

As you could see in yesterday's article as well, we are using the getImageData function.
const img = document.getElementById("eeveelutions");
const canvas = document.getElementById("canvas");
const ctx = canvas.getContext("2d");
img.onload = function () {
ctx.drawImage(img, 0, 0);
const imgData = ctx.getImageData(0, 0, canvas.width, canvas.height);
// Code comes here
};
This returns rgba values so as yesterday we need to loop over every 4th child.
for (i = 0; i < imgData.data.length; i += 4) {
}
Ok, so now what we get 4-pixel values, being rgba.
The alpha we won't use, but we want to get one combined value for the rgb.
Let's add up the three values for red green and blue.
let count = imgData.data[i] + imgData.data[i + 1] + imgData.data[i + 2];
This will give us a pixel number between 0 (black) and 765 (white).
In our case, we also add one grayscale layer, so we get the following three calculations:
That being said we can have the following code:
let colour = 0;
if (count > 510) colour = 255;
else if (count > 255) colour = 127.5;
Here we defined our default colour to be black (0), our white (255) and our gray (127.5)
We can then append our colour to the first three values of the pixel, and 255 to our alpha layer.
imgData.data[i] = colour;
imgData.data[i + 1] = colour;
imgData.data[i + 2] = colour;
imgData.data[i + 3] = 255;
Then we need to put the data back to our canvas.
ctx.putImageData(imgData, 0, 0);
There we go, we just converted our image into three colours!
Have a play around on this Codepen.
We can even make it pure black and white by using the following calculations:
And it will result in the following loop:
for (i = 0; i < imgData.data.length; i += 4) {
let count = imgData.data[i] + imgData.data[i + 1] + imgData.data[i + 2];
let colour = 0;
if (count > 383) colour = 255;
imgData.data[i] = colour;
imgData.data[i + 1] = colour;
imgData.data[i + 2] = colour;
imgData.data[i + 3] = 255;
}
Find this example on the following Codepen.
The imageData API, as well as canvas, have very good support!

Thank you for reading my blog. Feel free to subscribe to my email newsletter and connect on Facebook or Twitter