Categories
JavaScript Answers

How to take screenshot of a div with JavaScript?

Spread the love

To take screenshot of a div with JavaScript, we use the html2canvas library.

For instance, we write

const c = document.querySelector(".chartContainer");
const canvas = await html2canvas(c);
const t = canvas.toDataURL().replace("data:image/png;base64,", "");

to get the element with querySelector.

Then we call html2canvas with the c element to get a promise with the canvas with the element’s content inside.

Next, we call canvas.toDataURL to return the base64 URL string version of the canvas’ contents.

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 *