Skip to content

Instantly share code, notes, and snippets.

@thc282
Last active August 21, 2024 08:52
Show Gist options
  • Save thc282/affe429706550ddcdc6905199ac911d0 to your computer and use it in GitHub Desktop.
Save thc282/affe429706550ddcdc6905199ac911d0 to your computer and use it in GitHub Desktop.
canvas capture (three js)

⚠⚠⚠⚠⚠this must be set when new a renderer object⚠⚠⚠⚠⚠
{preserveDrawingBuffer: true}

//⚠⚠⚠⚠⚠this must be set when new a renderer object⚠⚠⚠⚠⚠
//{preserveDrawingBuffer: true}
const renderer = new THREE.WebGLRenderer({preserveDrawingBuffer: true});
//set a id for getting the canvas
renderer.domElement.id = 'screenshot';

//to get canvas and turn to base64URL
const canvas = document.getElementById("screenshot");
const dataURL = canvas.toDataURL();
//console.log(dataURL);

//create a fake link to download
var a = document.createElement("a");
a.setAttribute("download", "screenshot");
a.setAttribute("href", dataURL);
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment