Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <html>
  <head>
    <title>A-Frame: Look around handsfree</title>
    <script src="https://aframe.io/releases/1.1.0/aframe.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/gsap/3.5.1/gsap.min.js" integrity="sha512-IQLehpLoVS4fNzl7IfH8Iowfm5+RiMGtHykgZJl9AWMgqx0AmJ6cRWcB+GaGVtIsnC4voMfm8f2vwtY+6oPjpQ==" crossorigin="anonymous"></script>
    <!-- Include Handsfree.js in head -->
    <link rel="stylesheet" href="https://unpkg.com/handsfree@8.0.7/build/lib/assets/handsfree.css" />
    <script src="https://unpkg.com/handsfree@8.0.7/build/lib/handsfree.js"></script>
  </head>
  <body>
    <!-- Our cover/info card -->
    <div id="cover" class="handsfree-show-when-stopped">
      <h1>Look around <a href="https://aframe.io/">A-Frame</a> handsfree</h1>
      <p><img src="https://media1.giphy.com/media/YOPrRX6vTy6tb3frgt/giphy.gif"></p>
      <ul>
        <li>Rotate head around to rotate camera</li>
        <li>Move head around to move camera (slowly 😅)</li>
        <li>Explore the walkthrough for this pen at <a href="https://handsfree.js.org/integration/aframe/look-around-handsfree" target="_blank">Handsfree.js.org</a></li>
      </ul>

        <!-- Button. Notice the helper classes :) -->
    <div align=center>
      <button onclick="handsfree.start()" class="handsfree-show-when-stopped handsfree-hide-when-loading">Start webcam</button>
      <button class="handsfree-show-when-loading">Loading...</button>
      <button onclick="handsfree.stop()" class="handsfree-show-when-started">Stop webcam</button>
    </div>
  </div>
    
    <!-- Aframe -->
    <a-scene fog="type: exponential; color: #000; far: 30; density: 0.075">
      <a-assets>
        <img src="https://cdn.glitch.com/a2469ad6-a9ce-4918-8347-7348024d9f06%2F25P1geh.png?1543692382066" id="grid" crossorigin="anonymous">
      </a-assets>
      
      <!-- Camera Rig: This is what we control handsfree -->
      <a-entity id="rig" position="0 1 -5">
        <a-camera></a-camera>
      </a-entity>

      <!-- Walls -->
      <a-entity id="wall-bottom" geometry="primitive: plane; width: 10000; height: 10000;" rotation="-90 0 0" position="0 -2 0" material="src: #grid; repeat: 10000 10000;"></a-entity>
      <a-entity id="wall-top" geometry="primitive: plane; width: 10000; height: 10000;" rotation="90 0 0" position="0 7 0" material="src: #grid; repeat: 10000 10000;"></a-entity>
      <a-entity id="wall-right" geometry="primitive: plane; width: 10000; height: 10000;" rotation="0 -90 0" position="8 0 0" material="src: #grid; repeat: 10000 10000;"></a-entity>
      <a-entity id="wall-left" geometry="primitive: plane; width: 10000; height: 10000;" rotation="0 90 0" position="-8 0 0" material="src: #grid; repeat: 10000 10000;"></a-entity>
      <a-entity id="wall-back" geometry="primitive: plane; width: 10000; height: 10000;" rotation="0 0 0" position="0 0 -15" material="src: #grid; repeat: 10000 10000;"></a-entity>

      <!-- Some lights -->
      <a-entity light="color: #000; intensity: 1; type: ambient;" visible=""></a-entity>
      <a-entity light="color: #000; intensity: 1.5" position="5 5 5"></a-entity>
      <a-entity light="color: #000; intensity: 0.5" position="-5 5 15"></a-entity>
      <a-entity light="color: #aaa; type: ambient;"></a-entity>
    </a-scene>
  </body>
</html>
              
            
!

CSS

              
                button {font-size: 2.5em; cursor: pointer;}

#cover {
  position: fixed;
  z-index: 10;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  
  background: #ccc;
  padding: 20px;
  border-radius: 3px;
  box-shadow: 3px 3px 3px rgba(0,0,0,0.35);
}
              
            
!

JS

              
                $rig = document.querySelector('#rig')

// Let's use weboji. See: https://handsfree.js.org/ref/model/weboji
window.handsfree = new Handsfree({weboji: true})

// Used to hold tween values (without this things will be jerky)
tween = {yaw: 0, pitch: 0, roll: 0, x: 0, y: 0, z: 0}

// Create a new "plugin" to hook into the main loop
// @see https://handsfree.js.org/guide/the-loop
handsfree.use('lookHandsfree', ({weboji}) => {
  if (!weboji?.degree?.[0]) return

  // Calculate rotation
  const rot = weboji.degree
  rot[0] -= 15

  // Calculate position
  const pos = {
    x: (weboji.translation[0] - .5) * 10,
    y: (weboji.translation[1] - .5) * 5,
    z: 5 - weboji.translation[2] * 30
  }

  // Tween this values
  TweenMax.to(tween, 1, {
    yaw: -rot[0] * 1.5,
    pitch: -rot[1] * 1.5,
    roll: rot[2] * 1.5,
    x: pos.x,
    y: pos.y,
    z: pos.z
  })

  // Use the tweened values instead of the actual current values from webcam
  $rig.setAttribute('rotation', `${tween.yaw} ${tween.pitch} ${tween.roll}`)
  $rig.setAttribute('position', `${tween.x} ${tween.y} ${tween.z}`)
})

              
            
!
999px

Console