Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ 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

Auto Save

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

              
                <div class="sidebar">
  <button id="left">Left</button>
  <button id="right">Right</button>
  <button id="rightbounce">Right Bounce</button>
</div>
<iframe id="api-frame"></iframe>
              
            
!

CSS

              
                html,
body {
  padding: 0;
  margin: 0;
  width: 100vw;
  height: 100vh;
  overflow: hidden;
}
iframe {
  width: 100%;
  height: 100%;
  border: 0;
}
.sidebar {
  position: absolute;
  width: 180px;
  left: 0px;
  top: 0px;
  padding: 20px;
  text-align: left;
  display: flex;
  flex-direction: column;
  pointer-events: none;
}
div.sidebar button {
  pointer-events: all;
  margin-bottom: 8px;
  height: 40px;
}

              
            
!

JS

              
                const iframe = document.getElementById("api-frame");
const client = new Sketchfab(iframe);
let api;

const cameraData = {
  left: {
    position: [20, 400, 100],
    target: [0, 80, 0]
  },
  right: {
    position: [20, -400, 100],
    target: [0, 0, 80]
  }
};

const subtractPositions = (position1, position2) => {
  const dx = position2[0] - position1[0];
  const dy = position2[1] - position1[1];
  const dz = position2[2] - position1[2];
  return [dx, dy, dz];
};

const toLatLon = (position, target) => {
  const vector = subtractPositions(target, position);
  const lat = Math.atan2(
    vector[2],
    Math.sqrt(vector[0] * vector[0] + vector[1] * vector[1])
  );
  const lon = Math.atan2(vector[1], vector[0]);
  return [lat, lon];
};

const getDistance = (position1, position2) => {
  const dx = position2[0] - position1[0];
  const dy = position2[1] - position1[1];
  const dz = position2[2] - position1[2];
  return Math.sqrt(dx * dx + dy * dy + dz * dz);
};

const toPosition = (lat, lon, distance, target) => {
  const sinLat = Math.sin(lat);
  const cosLat = Math.cos(lat);
  const sinLon = Math.sin(lon);
  const cosLon = Math.cos(lon);

  const x = cosLat * cosLon;
  const y = cosLat * sinLon;
  const z = sinLat;

  return [
    target[0] + x * distance,
    target[1] + y * distance,
    target[2] + z * distance
  ];
};

function easeInOutQuad(x) {
  return x < 0.5 ? 2 * x * x : 1 - Math.pow(-2 * x + 2, 2) / 2;
}
function easeOutBounce(x) {
  const n1 = 7.5625;
  const d1 = 2.75;

  if (x < 1 / d1) {
    return n1 * x * x;
  } else if (x < 2 / d1) {
    return n1 * (x -= 1.5 / d1) * x + 0.75;
  } else if (x < 2.5 / d1) {
    return n1 * (x -= 2.25 / d1) * x + 0.9375;
  } else {
    return n1 * (x -= 2.625 / d1) * x + 0.984375;
  }
}
const state = {
  from: {
    latlon: [0, 0],
    target: [0, 0, 0],
    radius: 0
  },
  to: {
    latlon: [0, 0],
    target: [0, 0, 0],
    radius: 0
  },
  delta: {
    latlon: [0, 0],
    target: [0, 0, 0],
    radius: 0
  },
  distance: 0,
  steps: 50,
  step: 0,
  easing: "quad"
};

const stepCamera = () => {
  let factor = state.step / state.steps;
  if (state.easing === "quad") {
    factor = easeInOutQuad(state.step / state.steps);
  } else if (state.easing === "bounce") {
    factor = easeOutBounce(state.step / state.steps);
  }
  const target = [
    state.from.target[0] + state.delta.target[0] * factor,
    state.from.target[1] + state.delta.target[1] * factor,
    state.from.target[2] + state.delta.target[2] * factor
  ];
  const position = toPosition(
    state.from.latlon[0] + state.delta.latlon[0] * factor,
    state.from.latlon[1] + state.delta.latlon[1] * factor,
    state.from.radius + state.delta.radius * factor,
    target
  );
  api.setCameraLookAt(position, target, 0);
  if (state.step < state.steps) {
    state.step += 1;
    requestAnimationFrame(stepCamera);
  }
};

const collectCameraData = (camFrom, camTo) => {
  // calculate latlon, radius and target for the start location
  state.from.latlon = toLatLon(camFrom.position, camFrom.target);
  state.from.target = camFrom.target;
  state.from.radius = getDistance(camFrom.position, camFrom.target);

  // calculate latlon, radius and target for the destination
  state.to.latlon = toLatLon(camTo.position, camTo.target);
  state.to.target = camTo.target;
  state.to.radius = getDistance(camTo.position, camTo.target);

  // Calculate the difference between the start and destination
  state.delta.latlon = [
    state.to.latlon[0] - state.from.latlon[0],
    state.to.latlon[1] - state.from.latlon[1]
  ];
  state.delta.target = subtractPositions(state.from.target, state.to.target);
  state.delta.radius = state.to.radius - state.from.radius;

  // The amount of steps should depend on the distance traveled.
  // If both locations are really close, we need less steps than
  // when the distance is large. In other words, short distances
  // take less time than long distances.
  state.distance = getDistance(camTo.position, camFrom.position);
  state.steps = Math.floor(state.distance / 12);
  if (state.steps < 30) state.steps = 30;
  state.step = 0;
};

const setCamera = (api, cameraName) => {
  api.getCameraLookAt((err, camFrom) => {
    const camTo = cameraData[cameraName];
    collectCameraData(camFrom, camTo);
    requestAnimationFrame(stepCamera);
  });
};

client.init("c2fb3b0dbd4c4071bf9b4656ed41a432", {
  autostart: 0,
  ui_controls: 0,
  ui_infos: 0,
  ui_watermark: 0,
  ui_stop: 0,
  success: (_api) => {
    api = _api;
    api.addEventListener("viewerready", () => {
      document.getElementById("left").addEventListener("click", () => {
        state.easing = "quad";
        setCamera(api, "left");
      });
      document.getElementById("right").addEventListener("click", () => {
        state.easing = "quad";
        setCamera(api, "right");
      });
      document.getElementById("rightbounce").addEventListener("click", () => {
        state.easing = "bounce";
        setCamera(api, "right");
      });
    });
  },
  error: () => console.error("Sketchfab API error")
});

              
            
!
999px

Console