Download Canvas As Image In Javascript – Simple Example

Welcome to a quick tutorial and example on how to download a canvas as an image in Javascript. So you have an HTML canvas that you want to offer as a download – No problem, we can do that with a few lines of code. Read on!

ⓘ I have included a zip file with all the source code at the start of this tutorial, so you don’t have to copy-paste everything… Or if you just want to dive straight in.

 

 

TABLE OF CONTENTS

Download & Notes Download Canvas Useful Bits & Links
The End

 

DOWNLOAD & NOTES

Firstly, here is the download link to the example code as promised.

 

EXAMPLE CODE DOWNLOAD

Click here to download all the example source code, I have released it under the MIT license, so feel free to build on top of it or use it in your own project.

 

QUICK NOTES

If you spot a bug, please feel free to comment below. I try to answer questions too, but it is one person versus the entire world… If you need answers urgently, please check out my list of websites to get help with programming.

 

 

DOWNLOAD CANVAS

All right, let us now get into the example of how to download a canvas as an image.

 

1) CREATE DUMMY CANVAS

download.html
<!-- (A) CREATE DUMMY CANVAS -->
<!-- (A1) HTML -->
<canvas id="demoA" width="200" height="200"></canvas>

<!-- (A2) DRAW BLACK SQUARE -->
<script>
let canvas = document.getElementById("demoA"),
    ctx = canvas.getContext("2d");
ctx.fillRect(0, 0, 200, 200);
</script>

First, we start with creating a dummy canvas. Nothing much to see here, just a black square.

 

 

2) CREATE DOWNLOAD LINK

download.html
<!-- (B) GENERATE DOWNLOAD LINK -->
<!-- (B1) INSERT LINK HERE -->
<div id="demoB"></div>
 
<!-- (B2) CREATE DATA URL -->
<script>
var anchor = document.createElement("a");
anchor.innerHTML = "Click To Download";
// image/png, image/gif, image/jpeg
anchor.href = canvas.toDataURL("image/png");
anchor.download = "image.png";
document.getElementById("demoB").appendChild(anchor);
</script>

Generating the download link is a little tricky:

  • We create an HTML anchor link in Javascript – var anchor = document.createElement("a")
  • Set the “download as” filename – anchor.download = "image.png"
  • The magic happens when we do anchor.href = canvas.toDataURL(MIME-TYPE). Yes, the browser creates the image from the canvas and assigns a temporary URL to it.
  • Lastly, we attach the download link to the page itself – document.getElementById("demoB").appendChild(anchor)

Some of you sharp code ninjas may be thinking “can’t we just directly force a download”? Yes, we can try anchor.click() instead of adding the link to the HTML page. But take extra note – Clicking on a link programmatically may not work on some browsers due to security restrictions.

 

 

3) THE RESULT

 

USEFUL BITS & LINKS

That’s all for the tutorial, and here is a small section on some extras and links that may be useful to you.

 

INFOGRAPHIC CHEAT SHEET

Download Canvas As Image (Click To Enlarge)

 

LINKS & REFERENCES

 

THE END

Thank you for reading, and we have come to the end. I hope that it has helped you to better understand, and if you want to share anything with this guide, please feel free to comment below. Good luck and happy coding!

Leave a Comment

Your email address will not be published. Required fields are marked *