简体   繁体   中英

JavaScript error when generating A-Frame screenshots

I'm trying to generate screenshots from A-Frame but keep getting this error:

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

Here's the test code:

<!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>

The line of JavaScript is from the official docs: https://aframe.io/docs/0.8.0/components/screenshot.html

and here's the A-Frame source: https://github.com/aframevr/aframe/blob/master/src/components/scene/screenshot.js

The screenshot component is not initialized by the time you try to access its member function.

Try creating a new component consisting of your code:

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

HTML

<a-scene screenshot foo>

like i did here


You could also listen for the a-scene s loaded event, or use a setTimeout() if you want.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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