繁体   English   中英

生成A-Frame屏幕截图时出现JavaScript错误

[英]JavaScript error when generating A-Frame screenshots

我正在尝试从A-Frame生成屏幕截图,但一直收到此错误:

TypeError: document.querySelector(...).components.screenshot is undefined

这是测试代码:

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <script src="https://aframe.io/releases/0.8.0/aframe.min.js"></script>
  </head>
  <body>
    <a-scene screenshot="width: 640; height: 320">
      <a-entity id="box" geometry="primitive: box; width: 1; depth: 1; height: 1" position="-1 0.5 -3" rotation="0 45 0" material="color: #4CC3D9"></a-entity>
      <a-entity id="sphere" geometry="primitive: sphere; radius: 1.25" material="color: #EF2D5E" position="0 1.25 -5"></a-entity>
      <a-entity id="cylinder" geometry="primitive: cylinder; radius: 0.5; height: 1.5" position="1 0.75 -3" material="color: #FFC65D"></a-entity>
      <a-entity id="plane" position="0 0 -4" rotation="-90 0 0" geometry="primitive: plane; width: 4; height: 4" material="color: #7BC8A4"></a-entity>
      <a-entity id="sky" geometry="primitive: sphere; radius: 100" material="color: #ECECEC; shader: flat; side: back"></a-entity>
    </a-scene>

    <script>
      document.querySelector('a-scene').components.screenshot.capture('perspective')
    </script>
  </body>
</html>

JavaScript系列来自官方文档: https//aframe.io/docs/0.8.0/components/screenshot.html

这里是A-Frame来源: https//github.com/aframevr/aframe/blob/master/src/components/scene/screenshot.js

您尝试访问其成员函数时,不会初始化屏幕截图组件。

尝试创建一个包含代码的新组件:

AFRAME.registerComponent("foo", 
  init: function() {
    document.querySelector('a-scene').components.screenshot.capture('perspective')
  }
})

HTML

<a-scene screenshot foo>

就像我在这里做的那样


您也可以监听a-sceneloaded事件,或者如果需要也可以使用setTimeout()。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM