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 is required to process package imports. If you need a different preprocessor remove all packages first.

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

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

              
                
<script src="//cdn.rawgit.com/mrdoob/three.js/master/build/three.min.js"></script>

<div id="container"></div>


              
            
!

CSS

              
                
              
            
!

JS

              
                  // register events
  function registerEvent ( camera, items, type, wait ) {

    var listener = function (event) {

      var mouse = {
        x: (( event.clientX - 1) / window.innerWidth ) * 2 - 1,
        y: -(( event.clientY - 1) / window.innerHeight ) * 2 + 1
      };

      var vector = new THREE.Vector3();
          vector.set ( mouse.x, mouse.y, 0.5 );
          vector.unproject( camera );

          raycaster.ray.set( camera.position, vector.sub( camera.position ).normalize () );

      var target = raycaster.intersectObjects ( items );

      if ( target.length ) {
        target[ 0 ].type = type;
        target[ 0 ].object.dispatchEvent ( target[0] );
      }

    };

    if ( !wait ) {

      document.addEventListener ( type, listener, false );

    } else {

      document.addEventListener ( type, debounce( listener, wait ), false );

    }
  }

  // simple grid array
  var gridData = [

      5, 1, 6, 1, 7, 1, 8, 1, 9, 1, 10, 1, 11, 1,
      5, 2, 6, 2, 7, 2, 8, 2, 9, 2, 10, 2, 11, 2, 
      5, 3, 6, 3, 7, 3, 8, 3, 9, 3, 10, 3, 11, 3,
      5, 4, 6, 4, 7, 4, 8, 4, 9, 4, 10, 4, 11, 4,
      5, 5, 6, 5, 7, 5, 8, 5, 9, 5, 10, 5, 11, 5,
      5, 6, 6, 6, 7, 6, 8, 6, 9, 6, 10, 6, 11, 6,

  ];
            
  // setup globals and mats
  var raycaster = new THREE.Raycaster();	
  var container, camera, scene, renderer, controls;
  var objects = [], targets = { cubeGrid: [] };

  var cubeMaterial1 = new THREE.MeshLambertMaterial ( { 
      color: 0x000099,
      opacity: 0.4, 
      transparent: true, 
      depthWrite: false 
  } );

 // cube geometry for base cube
 var geometry = new THREE.CubeGeometry ( 250, 150, 1, 0, 0, 0 );
 var newNumber =  new THREE.Mesh ( geometry, cubeMaterial1 );
 var aspectRatio = window.innerWidth / window.innerHeight;
 var ambientLight = new THREE.AmbientLight ( 0xffffff, 1 );
 var raycaster = new THREE.Raycaster ();
			
 init ();
 animate ();
 function init () {
  
    camera = new THREE.PerspectiveCamera ( 45, aspectRatio, 1, 10000 );
    camera.position.z = 4000;

    scene = new THREE.Scene ();
    scene.add ( camera );
    scene.add ( ambientLight );

    // the patient
    function fitCameraToObject ( event ) {

      var object = this;		
      var offset = 0.01;
      var boundingBox = new THREE.Box3 ();
      var	center = new THREE.Vector3 ();
      

          // bb of object
          boundingBox.setFromObject ( object );
      scene.add( new THREE.BoxHelper( object ) );

      var center = boundingBox.getCenter ( center );
      var size = boundingBox.getSize ( center );
      var height = size.x;
      var width  = size.y;

      // get the max side of the bounding box (fits to width OR height as needed )
      var maxDim = Math.max ( height, width, size.z );
      var fov = camera.fov * ( Math.PI / 180 );

      // things tried
      var horizontal_FOV = 2 * Math.atan ( Math.tan ( fov / 2 ) * aspectRatio );
      var distance_vertical = height / ( 2 * Math.tan ( fov / 2 ) );
      var distance_horizontal = width / ( 2 * Math.tan ( horizontal_FOV / 2 ) );
      var z_distance = distance_vertical >= distance_horizontal? distance_vertical : distance_horizontal;

      // distance
      var cameraZ = maxDim / 2 / Math.tan ( horizontal_FOV / 2);
      
      
      var thisX = camera.x - object.x;
      //setTimeout ( function () {
        new TWEEN.Tween ( camera.position ).to ( { 
          x: camera.x, /* ? */
          y: camera.y, /* ? */
          z: cameraZ * offset }, 500 ).start ();
        
        new TWEEN.Tween ( camera.rotation ).to ( { 
          x: 0, /* ? */
          y: 0, /* ? */
          z: cameraZ * offset }, 500 ).start ();

      //}, 0 );
      
      //camera.rotation, speed, { x: 0, y: 0, z: 0
    }

   // begin content cubes
   var i = 0, cubeGridLen = gridData.length -1;
   for ( i; i < cubeGridLen; i += 2 ) {

    var number = newNumber.clone ();

        scene.add ( number );
        objects.push ( number );

        //add click eventListener
        number.addEventListener ( 'click', fitCameraToObject );
        registerEvent ( camera, [ number ], 'click' );	

        number = new THREE.Object3D ( number );
        number.position.x = ( gridData[ i ] * 247 ) - 2000;
        number.position.y = - ( gridData[ i + 1 ] * 147 ) + 500;
    
        targets.cubeGrid.push( number );									
  }

  // initialize cube grid
  transform( targets.cubeGrid, 1000 );

  // init renderer
  renderer = new THREE.WebGLRenderer ( { antialias: true } );
  renderer.toneMapping = THREE.LinearToneMapping;
  renderer.setPixelRatio ( window.devicePixelRatio );
  renderer.setSize ( window.innerWidth, window.innerHeight );
  // append domElement
  document.getElementById ( 'container' ).appendChild( renderer.domElement  );

  // init trackball controls 
  controls = new THREE.TrackballControls ( camera, renderer.domElement );
  controls.rotateSpeed = 0.5;
  controls.minDistance = 300;
  controls.maxDistance = 1200;
  controls.addEventListener ( 'change', render );
  controls.enabled = true;

  window.addEventListener ( 'resize', onWindowResize, false );

}


function transform ( targets, duration, scale ) {

  TWEEN.removeAll ();

  for ( var i = 0; i < objects.length; i ++ ) {

    var object = objects[ i ];
    var target = targets[ i ];

    new TWEEN.Tween ( object.position )
      .to ( { x: target.position.x, y: target.position.y, z: target.position.z }, Math.random () * duration + duration )
      .easing ( TWEEN.Easing.Exponential.InOut )
      .start ();

    new TWEEN.Tween( object.rotation )
      .to( { x: target.rotation.x, y: target.rotation.y, z: target.rotation.z }, Math.random () * duration + duration )
      .easing ( TWEEN.Easing.Exponential.InOut )
      .start ();
  }

  new TWEEN.Tween ( this )
    .to ( {}, duration * 2 )
    .onUpdate ( render )
    .start ();

}

function onWindowResize () {

  camera.aspect = window.innerWidth / window.innerHeight;
  camera.updateProjectionMatrix ();
  renderer.setSize ( window.innerWidth, window.innerHeight );
  render ();

}

function animate () {

  requestAnimationFrame ( animate );
  TWEEN.update ();        
  controls.update ();

}

function render () {

  renderer.render ( scene, camera );

}

              
            
!
999px

Console