怎么把Three.js的渲染转换成.png文件?

duk*_*pie 3 three.js

如何将渲染图像转换为.png图像?

我已经逛了一会儿了,但是什么也没做。

2ph*_*pha 5

这是我使用的功能,还有一个小提琴显示了它的工作原理

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();
}
Run Code Online (Sandbox Code Playgroud)