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 :: using html forms to define javascript variables 
Javascript :: jshint ignore line 
Javascript :: cm to feet javascript 
Javascript :: js create p element with text 
Javascript :: unix to date in javascript 
Javascript :: base href angular 
Javascript :: how to install jsonwebtoken in next js 
Javascript :: html to pdf javascript 
Javascript :: js debouncing 
Javascript :: Visible, non-interactive elements with click handlers must have at least one keyboard listener jsx-a11y/click-events-have-key-events 
Javascript :: javascript parse date dd/mm/yyyy hh:mm:ss 
Javascript :: hammer js 
Javascript :: date js add days 
Javascript :: how to get variable value outside function in javascript 
Javascript :: visual studio code create react component shortcut 
Javascript :: callback hell javascript 
Javascript :: passportjs serializeuser 
Javascript :: finding prime numbers in javascript 
Javascript :: redux toolkit with redux persist 
Javascript :: randomize an array in javascript 
Javascript :: js reverse int in descending order 
Javascript :: round innerhtml up javascript 
Javascript :: Node -Cron Run every minute 
Javascript :: convert date dd/mm/yyyy to date object js 
Javascript :: change array of object to object without index value 
Javascript :: reverse json.stringify 
Javascript :: javascript add event listenner for multiple events 
Javascript :: reload page after form submit javascript 
Javascript :: filter object js 
Javascript :: js returns the number of true values there are in an array 
ADD CONTENT
Topic
Content
Source link
Name
9+3 =