Book Image

Three.js Cookbook

By : Jos Dirksen
Book Image

Three.js Cookbook

By: Jos Dirksen

Overview of this book

Table of Contents (15 chapters)
Three.js Cookbook
Credits
About the Author
Acknowledgments
About the Reviewers
www.PacktPub.com
Preface
Index

Rotating the camera around a scene


In Chapter 2, Geometries and Meshes, we already showed you a number of recipes that explained how to rotate objects. In this recipe, we'll show you how to rotate the camera around a scene while the camera will keep looking at the center of the scene.

Getting ready

For this recipe, we'll use the standard THREE.PerspectiveCamera object, which we rotate around a simple scene. To see the final result, open the 03.08-rotate-camera-around-scene-y-axis.html example in your browser.

On this web page, you can see that the camera rotates around the scene while the floor, box, and lights stay at the same position.

How to do it...

To accomplish this, we only need to perform a couple of very simple steps:

  1. The first thing we need to do is create THREE.PerspectiveCamera and position it somewhere in the scene:

      // create a camera, which defines where we're looking at.
      camera = new THREE.PerspectiveCamera(45,window.innerWidth / window.innerHeight, 0.1, 1000);
      // position...