I have a React app that displays a toolbar and 2 canvases built-in Three.js. I would like to take a screenshot of the entire app. I tried already niklasvh/html2canvas
const element = document.getElementsByClassName("contentContainer-5")[0] as HTMLElement;
const url = html2canvas(element)
.then((canvas)=> {
return canvas.toDataURL();
})
}
const screenshotObject = {
url: url,
width: 128,
height: 64,
}
return screenshotObject
}
and BLOB html5
takeScreenshot() {
const screenshot = document.documentElement
.cloneNode(true) as Element;
const blob = new Blob([screenshot.outerHTML], {
type: 'text/html'
});
return blob;
}
generate() {
window.URL = window.URL || window.webkitURL;
window.open(window.URL
.createObjectURL(this.takeScreenshot()));
}
In the first case, the screenshot's URL is very long but the image is empty. In the second case, HTML and CSS is perfectly snapshotted but canvases are empty.
.cloneNode()only copies the DOM and canvasses are not a part of DOM. A workround could be done with the cloneNode function (as done here)Note:
cloneNodehas to be called on the canvas directly, and not the parent node.