【问题标题】:How to export an image from a scene from threeJS如何从threeJS的场景中导出图像
【发布时间】:2021-03-25 09:59:33
【问题描述】:

我想通过单击我的 html 页面上的按钮来导出我的场景的 2d 图像。我应该将哪一行代码添加到我的函数中?它似乎不起作用并下载图像。 (我使用chrome作为浏览器)

const exportLink = document.getElementById( 'exportLink' );
exportLink.addEventListener( 'click', () => {

    rendererExport.render( scene, camera );
    const dataURL = rendererExport.domElement.toDataURL( 'image/png' );

    exportLink.href = dataURL;
    exportLink.download = 'export.png';

} );
}

这是我的渲染函数

function render() {

renderer.render(scene, camera);
  document.getElementById("colore").onclick= function(){
    somma= +slider.value + +slider1.value + +slider2.value + +slider3.value + +slider4.value;
    console.log(somma);

    if(somma<=70){
      customMaterial1.uniforms.glowColor.value.set( 0x668072 );
      customMaterial2.uniforms.glowColor.value.set( 0x4f6749 );
      customMaterial3.uniforms.glowColor.value.set( 0x491520 );
      customMaterial4.uniforms.glowColor.value.set( 0xb3c753 );
      customMaterial5.uniforms.glowColor.value.set( 0xe61f59 );
    }
}

【问题讨论】:

    标签: javascript html three.js 3d export


    【解决方案1】:

    您的按钮已配置为执行您共享的 JavaScript 函数。但是,下载需要执行导航操作才能触发下载。执行此操作的常用方法是使用临时锚 (&lt;a&gt;)。

    const exportLink = document.getElementById( 'exportLink' );
    exportLink.addEventListener( 'click', () => {
    
        rendererExport.render( scene, camera );
        const dataURL = rendererExport.domElement.toDataURL( 'image/png' );
    
        let a = document.createElement( 'a' ); // Create a temporary anchor.
        a.href = dataURL;
        a.download = 'export.png';
        a.click(); // Perform the navigation action to trigger the download.
    
    } );
    

    【讨论】:

    • 感谢您的回答。它仍然不会触发快照的下载。我的 id 为“exportLink”的元素是一个

      。这可能是它不起作用的原因吗?

    • @LabSintesi exportLink 可以是任何东西。您已指定它使用您的函数响应 click 事件,但仅此而已。在我的代码中,我创建了一个等于:&lt;a href="--the dataURL--" download="export.png"&gt;&lt;/a&gt; 的新元素,然后单击该元素。如果这没有触发下载,请检查您的控制台 (F12) 是否有错误,或者查看您的浏览器是否阻止了下载。
    猜你喜欢
    • 1970-01-01
    • 2013-12-06
    • 2021-03-15
    • 1970-01-01
    • 2021-11-01
    • 2014-02-22
    • 2013-08-23
    • 2017-10-21
    • 1970-01-01
    相关资源
    最近更新 更多