Categories
JavaScript Answers

How to save canvas as png image with JavaScript?

Spread the love

Sometimes, we want to save canvas as png image with JavaScript.

In this article, we’ll look at how to save canvas as png image with JavaScript.

How to save canvas as png image with JavaScript?

To save canvas as png image with JavaScript, we can use the FileSaver.js library.

We install it with

npm install file-saver --save

Then we use it by writing

import { saveAs } from "file-saver";

const canvas = document.getElementById("my-canvas");

//...

canvas.toBlob((blob) => {
  saveAs(blob, "image.png");
});

We select the canvas with getElementById.

Then we call canvas.toBlob with a callback that calls saveAs to save the blob with the canvas content to image.png.

Conclusion

To save canvas as png image with JavaScript, we can use the FileSaver.js library.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

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