three.js
three.js is a powerful, lightweight, cross-browser JavaScript library for creating and displaying animated 3D graphics in a web browser using WebGL, WebGPU, and other renderers. It abstracts away the complexities of low-level graphics APIs, providing a high-level scene graph, cameras, lights, materials, and geometries. The library is actively maintained with frequent minor releases (currently at 0.184.0), typically on a monthly cadence, focusing on performance, new features, and the removal of deprecated APIs. Its key differentiators include a vast ecosystem of examples, extensive documentation, and a strong community, making it a go-to choice for interactive 3D web experiences, virtual reality, and augmented reality applications.
Common errors
-
Uncaught TypeError: Cannot read properties of undefined (reading 'Scene') OR Uncaught ReferenceError: THREE is not defined
cause Attempting to use `THREE` global or properties of `THREE` when the library hasn't been properly loaded as an ES module or via a script tag, often due to using `require()` or incorrect script order.fixEnsure you are using `import * as THREE from 'three';` for modern ES module environments or that the `three.js` script is loaded *before* your application script in a browser environment using `<script src="three.min.js"></script>`. -
Failed to resolve module specifier 'three/examples/jsm/controls/OrbitControls'. Relative references may require a leading './', '../', or '/'
cause Your module bundler or environment is unable to find the `OrbitControls` module because the path is incomplete or the `.js` extension is missing.fixExplicitly include the `.js` extension in your import statement: `import { OrbitControls } from 'three/examples/jsm/controls/OrbitControls.js';`
Warnings
- breaking three.js has a rapid release cycle, typically monthly, and frequently removes deprecated APIs and legacy code. Users are strongly advised to consult the 'Migration Guide' on the GitHub Wiki for each release when upgrading.
- breaking Modern versions of the 'three' npm package (e.g., r100+) are published as ES Modules (ESM). Direct CommonJS 'require()' will likely fail, especially for core modules.
- gotcha Addon modules (like controls, loaders, post-processing effects) are located in `three/examples/jsm/`. Importing these files often requires a module bundler to correctly resolve file paths and extensions.
- breaking The internal format version for Objects and Scenes (used in JSON serialization) may increase with new releases, potentially making scenes saved with older versions incompatible or requiring manual adjustments.
Install
-
npm install three -
yarn add three -
pnpm add three
Imports
- THREE (all exports)
const THREE = require('three');import * as THREE from 'three';
- Specific components (e.g., Scene, Camera, Renderer)
import Scene from 'three/src/Scene';
import { Scene, PerspectiveCamera, WebGLRenderer } from 'three'; - Addon modules (e.g., OrbitControls, GLTFLoader)
import { OrbitControls } from 'three/src/controls/OrbitControls';import { OrbitControls } from 'three/examples/jsm/controls/OrbitControls.js';
Quickstart
import * as THREE from 'three';
const width = window.innerWidth, height = window.innerHeight;
// init
const camera = new THREE.PerspectiveCamera( 70, width / height, 0.01, 10 );
camera.position.z = 1;
const scene = new THREE.Scene();
const geometry = new THREE.BoxGeometry( 0.2, 0.2, 0.2 );
const material = new THREE.MeshNormalMaterial();
const mesh = new THREE.Mesh( geometry, material );
sce.add( mesh );
const renderer = new THREE.WebGLRenderer( { antialias: true } );
renderer.setSize( width, height );
renderer.setAnimationLoop( animate );
document.body.appendChild( renderer.domElement );
// animation
function animate( time ) {
mesh.rotation.x = time / 2000;
mesh.rotation.y = time / 1000;
renderer.render( scene, camera );
}