Sketchfab Viewer API: Camera Limits + Annotations

by James Green

HTML

<script src="https://static.sketchfab.com/api/sketchfab-viewer-1.12.1.js"></script>
<!-- Learn about this code on sketchfab: https://sketchfab.com/developers/viewer -->


<div class="container">
  <div class="iframe-container">
    <iframe src="" id="api-frame"></iframe>
  </div>
  <div class="controls" id="controls">
    <pre id="Info"></pre>
  </div>
</div>

CSS

html,
body {
  padding: 0;
  margin: 0;
  width: 100vw;
  height: 100vh;
  -webkit-font-smoothing: antialiased;
  -moz-font-smoothing: grayscale;
  font-family: Open Sans, sans-serif;
}

* {
  box-sizing: border-box;
}

.container {
  display: flex;
  flex-direction: column;
  height: 100%;
}

.iframe-container {
  position: relative;
  width: 100%;
  flex: 1;
  display: flex;
}

.iframe-container > iframe {
  border: 0;
  width: 100%;
  flex: 1;
}

.controls {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  flex-shrink: 0;
  min-height: 50px;
  padding: 15px 0 0 15px;
  background-color: #F2F2F2;
  border-top: 1px solid #e7e7e7;
}

.controls > * {
  margin-right: 15px;
  margin-bottom: 15px;
}

button {
  display: inline-flex;
  justify-content: center;
  align-items: center;
  position: relative;
  border: 2px solid transparent;
  border-radius: 4px;
  padding: 0 13px;
  font-family: "Open Sans", sans-serif;
  font-weight: 600;
  text-align: center;
  text-shadow: none;
  text-transform: uppercase;
  line-height: 1.2;
  cursor: pointer;
  outline: none;
  transition: background 0.2s;
  color: white;
  background-color: #1caad9;
  height: 25px;
  font-size: 12px;
}

JavaScript

// Sketchfab Viewer API: Camera Limits + Annotations
const version = '1.12.1';
const uid = '5681177d82ce46be9fbb8983ee991e79';
const iframe = document.getElementById('api-frame');
const client = new window.Sketchfab(version, iframe);
let api;

const error = () => {
  window.console.error('Sketchfab API error');
};

const success = apiClient => {
  api = apiClient;
  api.start();
  api.addEventListener( 'viewerready', function() {
    window.console.log('viewer ready');

    document.getElementById('create-annotation').addEventListener('click', function() {
        api.getCameraLookAt(function(err, camera) {
          api.createAnnotation(
            camera.position,
            camera.target,
            camera.position,
            camera.target,
            'title',
            'text',
            function(){api.getAnnotationList(function(err, annotations) {
              console.log(annotations);
          	})}
          );
      });
    });
  });
};


client.init(uid, {
  annotation: 0, // Usage: Setting to [1 – 100] will automatically load that annotation when the viewer starts.
  annotations_visible: 1, // Usage: Setting to 0 will hide annotations by default.
  autospin: 0, // Usage: Setting to any other number will cause the model to automatically spin around the z-axis after loading.
  autostart: 1, // Usage: Setting to 1 will make the model load immediately once the page is ready, rather than waiting for a user to click the Play button.
  cardboard: 0, // Usage: Start the viewer in stereoscopic VR Mode built for Google Cardboard and similar devices.
  camera: 0, // Usage: Setting to 0 will skip the initial animation that occurs when a model is loaded, and immediately show the model in its default position.
  preload: 1, // Usage: Setting to 1 will force all resources (textures) to download before the scene is displayed.
  ui_stop: 0, // Usage: Setting to 0 will hide the "Disable Viewer" button in the top right so that users cannot stop the 3D render...