[英]How to access canvas for a screenshot when using react-stl-obj-viewer component
I'm using a component react-stl-obj-viewer to render a 3d stl image.我正在使用组件react-stl-obj-viewer来渲染 3d stl 图像。 I can render the 3d stl image correctly.我可以正确渲染 3d stl 图像。 Once the image is rendered, I'm trying to move it around and have a button to take a screenshot of it.渲染图像后,我会尝试移动它并有一个按钮来截取它的屏幕截图。
<div>
<STLViewer
onSceneRendered={(element) => {
console.log(element);
}}
sceneClassName="test-scene"
file={this.state.selectedFile}
modelColor="#073FE9"
/>
</div>
<div style={{ display: "inline-block" }}>
<Button
id="thumbnail"
style={{ zIndex: "-1", margin: "20px 0px 20px 0px" }}
onClick={(e) => {
this.save3dRender(e, webGlContextExists);
}}
>
Save Frame as Thumbnail
</Button>
{!this.state.showThumbnail ? (
<Container>
<h2>Did you get this image</h2>
<Image src={this.state.thumbnailFile} />
</Container>
) : null}
</div>
In order to take a screenshot of the image, I was trying to use.toDataURL("image/png").为了截取图片,我尝试使用.toDataURL("image/png")。 My button does this when pressed.我的按钮在按下时执行此操作。
save3dRender = (e, geeL) => {
e.preventDefault();
var testThumbnail = geeL.canvas.toDataURL("image/png");
this.state.thumbnailFile = testThumbnail;
this.state.thumbnailFileRender = true;
this.state.showThumbnail = false;
};
I am somewhat new at this but thought I was on the right track.我对此有些陌生,但认为我走在正确的轨道上。 Since the STLViewer component is creating the canvas, I was trying to access it this way with.canvas.由于 STLViewer 组件正在创建 canvas,因此我试图以这种方式使用 .canvas 访问它。 Not sure if that is correct but I do get a data:image/png;base64 address.不确定这是否正确,但我确实得到了一个 data:image/png;base64 地址。 However it is a blank image.然而,它是一个空白图像。
Am I accessing the correct canvas?我访问的是正确的 canvas 吗? Is there something I need to do to the image after getting it as data:/image/png;base64.在将图像作为 data:/image/png;base64 后,我需要对图像做些什么吗?
Fixed the issue.解决了这个问题。 It was an array that I needed to reference.这是一个我需要引用的数组。
var testThumbnail = geeL[0].toDataURL("image/png");
instead of var testThumbnail = geeL.canvas.toDataURL("image/png");
而不是var testThumbnail = geeL.canvas.toDataURL("image/png");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.