拍摄 modelviewer .glb 文件的截图。

huangapple go评论87阅读模式
英文:

Take a screenshot of modelviewer .glb file

问题

我正在尝试截取 model-viewer 显示的 .glb 文件的屏幕截图,但我找不到解决方案。

我在某处读到的关键是将 model-viewer 转换为 Blob 对象,然后下载图像。

<div class="button">
    <a id="download" href="#">下载按钮</a>
</div>

<script>
function download() {
  const modelViewer =  document.querySelector("#modelviewertest");
  const screenshot = URL.createObjectURL(
      modelViewer.toBlob("image/jpeg")
    );
  const downloadLink = document.querySelector("#download");
  downloadLink.download = screenshot;
  downloadLink.href = screenshot;
}
</script>

我尝试使用 html2canvas 来截取屏幕截图,但不起作用。我想将加载在 model-viewer 上的 .glb 模型的屏幕截图保存到我的服务器上。

英文:

I'm trying to take a screenshot of a .glb file displayed by model-viewer. But I can't arrive to a solution.
I've read somewhere that the key is to convert the modelviewer to a blob object and then download an image.

&lt;div class=&quot;button&quot;&gt;
    &lt;a id=&quot;download&quot; href=&quot;#&quot;&gt;Download button&lt;/a&gt;
  &lt;/div&gt;
  
&lt;script&gt;

function download() {
  const modelViewer =  document.querySelector(&quot;#modelviewertest&quot;);
  const screenshot = URL.createObjectURL(
      modelViewer.toBlob(&quot;image/jpeg&quot;)
    );
  const donwloadLink = document.querySelector(&quot;#download&quot;);
  donwloadLink.download = screenshot;
  donwloadLink.href = data;
}

I have tried to take a screenshot with html2canvas but not working. I would like to save into my server a screenshot of the glb model loaded on model-viewer.

答案1

得分: 2

以下是您要的内容的中文翻译:

这里有一个关于如何截图并下载的示例链接。如果链接无效,下面是代码。感谢GitHub用户 @milesgreen 的贡献,他通过这个问题发布了这个方法。

const modelViewer = document.getElementById("viewer");

async function downloadPosterToBlob() {
    const blob = await modelViewer.toBlob({ idealAspect: false });
    const url = URL.createObjectURL(blob);
    const a = document.createElement("a");
    a.href = url;
    a.download = "modelViewer_toBlob.png";
    a.click();
    URL.revokeObjectURL(url);
}

function downloadPosterToDataURL() {
    const url = modelViewer.toDataURL();
    const a = document.createElement("a");
    a.href = url;
    a.download = "modelViewer_toDataURL.png";
    a.click();
    URL.revokeObjectURL(url);
}

上面的JavaScript代码引用了一个具有ID设置为 "viewer" 的HTML元素。

<model-viewer id="viewer"
              alt="两缸发动机"
              src="models/2CylinderEngine.glb"
              shadow-intensity="1"
              camera-controls touch-action="pan-y">
</model-viewer>

在HTML中添加一个按钮,并将其与上面的下载函数连接到其“click”事件:

document.querySelector("#download-button").addEventListener("click", downloadPosterToBlob);
<button id="download-button">下载</button>

参考链接:model-viewer web组件的 toBlob() 方法

英文:

There's a sample of taking a screenshot and downloading it here. The code is listed here in case the link goes down. Attribution goes to @milesgreen on GitHub, posted through this issue.

const modelViewer = document.getElementById(&quot;viewer&quot;);

async function downloadPosterToBlob() {
    const blob = await modelViewer.toBlob({ idealAspect: false });
    const url = URL.createObjectURL(blob);
    const a = document.createElement(&quot;a&quot;);
    a.href = url;
    a.download = &quot;modelViewer_toBlob.png&quot;;
    a.click();
    URL.revokeObjectURL(url);
}

function downloadPosterToDataURL() {
    const url = modelViewer.toDataURL();
    const a = document.createElement(&quot;a&quot;);
    a.href = url;
    a.download = &quot;modelViewer_toDataURL.png&quot;;
    a.click();
    URL.revokeObjectURL(url);
}

The JavaScript above references an HTML element that has an ID set to "viewer".

&lt;model-viewer id=&quot;viewer&quot;
              alt=&quot;Two cylinder engine&quot;
              src=&quot;models/2CylinderEngine.glb&quot;
              shadow-intensity=&quot;1&quot;
              camera-controls touch-action=&quot;pan-y&quot;&gt;
&lt;/model-viewer&gt;

Add a button in HTML and connect to its click event to trigger the download function above:

document.querySelector(&quot;#download-button&quot;).addEventListener(&quot;click&quot;, downloadPosterToBlob);

&lt;button id=&quot;download-button&quot;&gt;Download&lt;/button&gt;

Reference to the model-viewer web component's toBlob() method.

huangapple
  • 本文由 发表于 2023年3月7日 23:04:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/75663686.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定