Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

how to rotate camera around three JS object

var canvas = document.getElementById('canvas');
var scene = new THREE.Scene();
var renderer = new THREE.WebGLRenderer({canvas: canvas, antialias: true});
var camera = new THREE.PerspectiveCamera(45, canvas.clientWidth / canvas.clientWidth, 1, 1000);

var geometry = new THREE.BoxGeometry(50, 50, 50);
var material = new THREE.MeshBasicMaterial({color: '#f00'});
var box = new THREE.Mesh(geometry, material);
scene.add(box);

// important, otherwise the camera will spin on the spot.
camera.position.z = 200;

var period = 5; // rotation time in seconds
var clock = new THREE.Clock();
var matrix = new THREE.Matrix4(); // Pre-allocate empty matrix for performance. Don't want to make one of these every frame.
render();

function render() {
  requestAnimationFrame(render);
  if (canvas.width !== canvas.clientWidth || canvas.height !== canvas.clientHeight) {
    // This stuff in here is just for auto-resizing.
    renderer.setSize(canvas.clientWidth, canvas.clientHeight, false);
    camera.aspect = canvas.clientWidth /  canvas.clientHeight;
    camera.updateProjectionMatrix();
  }

  // Create a generic rotation matrix that will rotate an object
  // The math here just makes it rotate every 'period' seconds.
  matrix.makeRotationY(clock.getDelta() * 2 * Math.PI / period);

  // Apply matrix like this to rotate the camera.
  camera.position.applyMatrix4(matrix);

  // Make camera look at the box.
  camera.lookAt(box.position);

  // Render.
  renderer.render(scene, camera);
}
Comment

rotate camera three js

t = Math.min(t + 0.01, 1);
camera.position.copy(startPosition).lerp(endPosition, t);
camera.lookAt(0, 0, 0);
Comment

PREVIOUS NEXT
Code Example
Javascript :: mlutiple css jquery 
Javascript :: remove attribute disabled javascript 
Javascript :: expo textinput caret style 
Javascript :: sort an array by the laster letter of element javascript 
Javascript :: javascript delete row by id 
Javascript :: ajax call with form data 
Javascript :: create a link javascript 
Javascript :: how to use componentdidmount in functional component 
Javascript :: laravel javascript array from blade 
Javascript :: bootstrap selectpicker get selected value 
Javascript :: bq show pretty json 
Javascript :: how do you remove a remove element from array in javascript 
Javascript :: javascript code to refresh page automatically 
Javascript :: javascript check if first of type 
Javascript :: How to focus on the marker position with zoom in react using react-google-maps 
Javascript :: react execute code after set 
Javascript :: .replace is not a function 
Javascript :: creare component in anglar 
Javascript :: condition in string interpolation angular 
Javascript :: brightness javascript onload 
Javascript :: angular pipe json error 
Javascript :: JavaScript does not protect the property name hasOwnProperty 
Javascript :: how to sort array alphabetically in javascript 
Javascript :: angular find value in json array 
Javascript :: store array in local storage js 
Javascript :: checkbox is checked jquery 
Javascript :: mongoose pull each 
Javascript :: javascript append how first element 
Javascript :: set focus javascript 
Javascript :: body-parser deprecated bodyParser 
ADD CONTENT
Topic
Content
Source link
Name
8+9 =