How to capture HTML Canvas as gif/jpg/png/pdf with JavaScript?

Spread the love

Sometimes, we want to capture HTML Canvas as gif/jpg/png/pdf with JavaScript.

In this article, we’ll look at how to capture HTML Canvas as gif/jpg/png/pdf with JavaScript.

How to capture HTML Canvas as gif/jpg/png/pdf with JavaScript?

To capture HTML Canvas as gif/jpg/png/pdf with JavaScript, we use the canvas toDataURL method.

For instance, we write

const canvas = document.getElementById("mycanvas");
const img = canvas.toDataURL("image/png");

to get the canvas with getElementById.

Then we call canvas.toDataURL with the MIME type of the image to return to return the image as a base64 URL string.

Conclusion

To capture HTML Canvas as gif/jpg/png/pdf with JavaScript, we use the canvas toDataURL method.

Leave a Reply

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