Vanilla JavaScript images in canvas

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.
No comments yet. Be the first to comment.
In this series we will get started with the canvas element and use JavaScript to make some amazing art.
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; black white grey (only 1 type!) This will abstract our image a...
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...

Another day of canvas exploration, and today we'll be looking at using images in our canvas.
We use the getImageData function to read an image, which will return an imageData object that copies pixel data.
For each pixel, we will get the rgba values.
Today we will be exploring getting these values from an image and inverting them.
The end result is this Codepen.
You can find my other articles about canvas modifying on the following links:
Let's first setup our HTML structure so we have an image and a canvas to render our new image in.
<img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1600016358126/bMK5IddO3.jpeg" id="eeveelutions" />
<canvas id="canvas" width="200" height="200">
There we go, we have our image, which is 200x200 and our canvas which I made the same for this exercise.
Next we need to define our image and canvas in JavaScript.
const img = document.getElementById('eeveelutions');
const canvas = document.getElementById('canvas');
const ctx = canvas.getContext('2d');
Now we can render the image as is in our canvas:
Note: We have to wait for the image to load, else we will see a white image
img.onload = function () {
ctx.drawImage(img, 0, 0);
};
But that's for this point not really useful, so let's get the imageData.
const imgData = ctx.getImageData(0, 0, canvas.width, canvas.height);
Note: For Codepen I have to add
img.crossOrigin="anonymous";for CORS issues.
We then get a imageData object that looks something like this:

As mentioned before these are rgba values so every four records is one pixel value containing:
So to invert each pixels value, we need to do the following calculation for each of the three colours (alpha will keep 255)
In code it will look like this:
for (i = 0; i < imgData.data.length; i += 4) {
imgData.data[i] = 255 - imgData.data[i];
imgData.data[i + 1] = 255 - imgData.data[i + 1];
imgData.data[i + 2] = 255 - imgData.data[i + 2];
imgData.data[i + 3] = 255;
}
The last step is then to put this modified data back on our canvas.
ctx.putImageData(imgData, 0, 0);
There we go, we learned how to place an image in a canvas, and even how to modify it's pixel data! 🔥
Image credit Zerochan
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