three.js - OrbitControls

Rotate the camera around an object with three.js r86

by Thanos Saringelos

HTML

<script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/86/three.js"></script>
<script data-name="orbitControls.js">
  /**
   * @author qiao / https://github.com/qiao
   * @author mrdoob / http://mrdoob.com
   * @author alteredq / http://alteredqualia.com/
   * @author WestLangley / http://github.com/WestLangley
   * @author erich666 / http://erichaines.com
   */

  // This set of controls performs orbiting, dollying (zooming), and panning.
  // Unlike TrackballControls, it maintains the "up" direction object.up (+Y by default).
  //
  //    Orbit - left mouse / touch: one finger move
  //    Zoom - middle mouse, or mousewheel / touch: two finger spread or squish
  //    Pan - right mouse, or arrow keys / touch: three finger swipe

  THREE.OrbitControls = function(object, domElement) {

    this.object = object;

    this.domElement = (domElement !== undefined) ? domElement : document;

    // Set to false to disable this control
    this.enabled = true;

    // "target" sets the location of focus, where the object orbits around
    this.target = new THREE.Vector3();

    // How far you can dolly in and out ( PerspectiveCamera only )
    this.minDistance = 0;
    this.maxDistance = Infinity;

    // How far you can zoom in and out ( OrthographicCamera only )
    this.minZoom = 0;
    this.maxZoom = Infinity;

    // How far you can orbit vertically, upper and lower limits.
    // Range is 0 to Math.PI radians.
    this.minPolarAngle = 0; // radians
    this.maxPolarAngle = Math.PI; // radians

    // How far you can orbit horizontally, upper and lower limits.
    // If set, must be a sub-interval of the interval [ - Math.PI, Math.PI ].
    this.minAzimuthAngle = -Infinity; // radians
    this.maxAzimuthAngle = Infinity; // radians

    // Set to true to enable damping (inertia)
    // If damping is enabled, you must call controls.update() in your animation loop
    this.enableDamping = false;
    this.dampingFactor = 0.25;

    // This option actually...

CSS

body,
canvas {
  margin: 0;
  padding: 0;
}

body {
  overflow: hidden
}

JavaScript

'use strict';

var camera, scene, renderer;
var cube, cube_geometry, cube_material;
var controls;

init();
render();

function init() {

  scene = new THREE.Scene();

  // renderer

  renderer = new THREE.WebGLRenderer({
    alpha: true
  });
  renderer.setSize(window.innerWidth, window.innerHeight);
  document.body.appendChild(renderer.domElement);

  // camera

  camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);
  camera.position.z = 12;

  // controls

  controls = new THREE.OrbitControls(camera, renderer.domElement);
  controls.addEventListener('change', render);
  controls.enableZoom = false;

  // mesh - cube

  cube_geometry = new THREE.CubeGeometry(5, 5, 5);

  for (var i = 0; i < cube_geometry.faces.length; i += 2) {

    var color = Math.random() * 0xffffff;

    cube_geometry.faces[i].color.setHex(color);
    cube_geometry.faces[i + 1].color.setHex(color);
  }

  cube_material = new THREE.MeshLambertMaterial({
    color: 0xffffff,
    vertexColors: THREE.FaceColors
  });

  cube = new THREE.Mesh(cube_geometry, cube_material);
  scene.add(cube);
  // xyz axes
  let xAxis = new THREE.Geometry();
  xAxis.vertices.push(
    new THREE.Vector3(1000, 0, 0),
    new THREE.Vector3(0, 0, 0)
  );

  let yAxis = new THREE.Geometry();
  yAxis.vertices.push(
    new THREE.Vector3(0, 1000, 0),
    new THREE.Vector3(0, 0, 0)
  );

  let zAxis = new THREE.Geometry();
  zAxis.vertices.push(
    new THREE.Vector3(0, 0, 1000),
    new THREE.Vector3(0, 0, 0)
  );

  let xAxisLine = new THREE.Line(xAxis, new THREE.LineBasicMaterial({
    color: "#6F6222"
  }));
  let yAxisLine = new THREE.Line(yAxis, new THREE.LineBasicMaterial({
    color: "#FF0000"
  }));
  let zAxisLine = new THREE.Line(zAxis, new THREE.LineBasicMaterial({
    color: "#6F6299"
  }));

  scene.add(xAxisLine);
    scene.add(yAxisLine);
    scene.add(zAxisLine);
  // Lights

  var light = new THREE.DirectionalLight(0xffffff);
  light.position.set(1, 1, 1);
 ...