Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert render from three.js to .png file?

Tags:

three.js

How would I convert a render to a .png image?

I've been looking around for awhile but nothing has worked.

like image 780
dukeispie Avatar asked Feb 06 '23 18:02

dukeispie


1 Answers

Here is a function I use and a fiddle that shows it working.

function takeScreenshot() {
    // For screenshots to work with WebGL renderer, preserveDrawingBuffer should be set to true.
    // open in new window like this
    var w = window.open('', '');
    w.document.title = "Screenshot";
    //w.document.body.style.backgroundColor = "red";
    var img = new Image();
    img.src = renderer.domElement.toDataURL();
    w.document.body.appendChild(img);

    // download file like this.
    //var a = document.createElement('a');
    //a.href = renderer.domElement.toDataURL().replace("image/png", "image/octet-stream");
    //a.download = 'canvas.png'
    //a.click();
}
like image 160
2pha Avatar answered Feb 16 '23 02:02

2pha