Search code examples
javascriptthree.jsloadaframewebvr

A-frame detect when scene is loaded


I have a scene using A-frame set up with a few assets that require a loading screen before entering the scene. What I'm wondering is how can I detect by logging a message in the console when my assets/scene has loaded? Code for a basic scene:

<html>
  <head>
    <script src="https://aframe.io/releases/1.2.0/aframe.min.js"></script>
  </head>
  <body>
<script>
//When scene is loaded
console.log ("loaded");
</script>
    <a-scene>
      <a-box position="-1 0.5 -3" rotation="0 45 0" color="#4CC3D9"></a-box>
      <a-sphere position="0 1.25 -5" radius="1.25" color="#EF2D5E"></a-sphere>
      <a-cylinder position="1 0.75 -3" radius="0.5" height="1.5" color="#FFC65D"></a-cylinder>
      <a-plane position="0 0 -4" rotation="-90 0 0" width="4" height="4" color="#7BC8A4"></a-plane>
      <a-sky color="#ECECEC"></a-sky>
    </a-scene>
  </body>
</html>

Solution

  • Listen for this event on the scene element:

    https://aframe.io/docs/1.2.0/core/scene.html#events_loaded

    Sample code...

    <html>
      <head>
        <script src="https://aframe.io/releases/1.2.0/aframe.min.js"></script>
        <script>
          AFRAME.registerComponent("log", {
            init: function () {
              this.el.addEventListener("loaded", () => {
                console.log ("loaded");
              });
            }
          });
        </script>
      </head>
      <body>
    
        <a-scene log>
          <a-box position="-1 0.5 -3" rotation="0 45 0" color="#4CC3D9"></a-box>
          <a-sphere position="0 1.25 -5" radius="1.25" color="#EF2D5E"></a-sphere>
          <a-cylinder position="1 0.75 -3" radius="0.5" height="1.5" color="#FFC65D"></a-cylinder>
          <a-plane position="0 0 -4" rotation="-90 0 0" width="4" height="4" color="#7BC8A4"></a-plane>
          <a-sky color="#ECECEC"></a-sky>
        </a-scene>
      </body>
    </html>