Vanilla JavaScript save canvas as an image

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.
Thanks Damilare, glad you like it!
In this series we will get started with the canvas element and use JavaScript to make some amazing art.
So far, we have learned the basics of the canvas, and how to export it as an image. But it was all plain looking, so let's go ahead and explore our colouring options for the canvas. Today we'll learn how to make the following; https://codepen.io/rebe...
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 got started on our basic canvas course. Thinking about the project I want to do, I need to export the canvas as an image.
So how do we convert the canvas to an image?
There are actually two ways of doing this, and we will explore both.
See the end result on this Codepen.
Everyone knows this option, but we can just right-click to save on the canvas.
This will only work in certain browsers, so it's not the most valid way of saving it.

Note: Keep in mind the canvas has no background!
The other option is to add a download button to our page. This download button will then export the canvas content en open the base64 image in another tab.
Adding the button:
<canvas id="canvas" height="200"></canvas>
<br />
<button id="download">Download</button>
Now let's add the button variable to our JavaScript
const download = document.getElementById('download');
Awesome, now we need to add a eventListener to it and listen to the click command.
download.addEventListener('click', function(e) {
var link = document.createElement('a');
link.download = 'download.png';
link.href = canvas.toDataURL()
link.click();
link.delete;
});
We create a temporary ahref on which we will place the canvas's data url and then click it.
We are using the toDataURL function which returns a base64 string that looks something like this:
// "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNby
// blAAAADElEQVQImWNgoBMAAABpAAFEI8ARAAAAAElFTkSuQmCC"
The canvas element is well supported these days and is defiantly a good option if you want to draw vectors on screen.

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