I am currently attempting to save a screenshot of a specific area in ThreeJS to a file.
Initial attempts involved displaying the image in a new tab using:
window.open(renderer.domElement.toDataURL("image/png"));
The renderer object used has preserveDrawingBuffer set to true.
However, since this captures the entire scene, I switched to:
var gl = renderer.getContext();
var pixels = new Uint8Array(width * height * 4);
gl.readPixels(x, y, width, height, gl.RGBA, gl.UNSIGNED_BYTE, pixels);
window.open("data:image/png;base64," + btoa(String.fromCharCode.apply(null, pixels)));
After making this change, only a grey outlined square is rendered on screen.