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.