Skip to content

Instantly share code, notes, and snippets.

@thc282
Last active June 26, 2024 04:22
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)
const renderer = new THREE.WebGLRenderer({preserveDrawingBuffer: true});
//set a id for search
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