Three.js — JavaScript 3D Library for the Web
Three.js is the most popular 3D library for JavaScript — WebGL/WebGPU rendering made accessible. Build 3D scenes, models, shaders, physics, VR/AR, and interactive experiences without touching low-level graphics APIs.
What it is
Three.js is a JavaScript library that makes WebGL and WebGPU rendering accessible. Instead of writing low-level shader code and managing GPU buffers, you work with high-level abstractions: scenes, cameras, lights, geometries, and materials. It is the most widely used 3D library for the web.
Three.js serves frontend developers who need 3D visualization (product configurators, data visualization, games), creative developers building interactive experiences, and teams adding 3D elements to existing web applications.
How it saves time or tokens
Raw WebGL requires hundreds of lines of boilerplate for a spinning cube: shader compilation, buffer binding, matrix math, and render loop management. Three.js reduces this to a dozen lines. The built-in loader system handles glTF, OBJ, FBX, and other 3D file formats without custom parsing code.
The ecosystem of examples, addons, and community extensions means most common 3D patterns (orbit controls, post-processing, physics) are already solved.
How to use
- Install Three.js:
npm install three
- Create a basic scene:
import * as THREE from 'three';
const scene = new THREE.Scene();
const camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);
const renderer = new THREE.WebGLRenderer();
renderer.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(renderer.domElement);
- Add a mesh and render:
const geometry = new THREE.BoxGeometry();
const material = new THREE.MeshStandardMaterial({ color: 0x00ff00 });
const cube = new THREE.Mesh(geometry, material);
scene.add(cube);
camera.position.z = 5;
function animate() {
cube.rotation.x += 0.01;
cube.rotation.y += 0.01;
renderer.render(scene, camera);
}
renderer.setAnimationLoop(animate);
Example
Loading a glTF model with orbit controls:
import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js';
import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
const controls = new OrbitControls(camera, renderer.domElement);
const loader = new GLTFLoader();
loader.load('model.glb', (gltf) => {
scene.add(gltf.scene);
});
This gives you a draggable, zoomable 3D model viewer in under 20 lines of code.
Related on TokRepo
- AI tools for design -- Tools for visual and 3D design workflows
- AI tools for coding -- JavaScript development tools and libraries
Common pitfalls
- Not disposing of geometries and materials when removing objects. Three.js does not garbage-collect GPU resources automatically. Call
.dispose()on geometries, materials, and textures to prevent memory leaks. - Using uncompressed textures. Large PNG/JPG textures consume GPU memory. Use KTX2/Basis compressed textures for production applications.
- Ignoring the render loop frame budget. At 60fps you have 16ms per frame. Complex scenes with many draw calls need optimization (instancing, LOD, frustum culling).
Frequently Asked Questions
Yes. Three.js added experimental WebGPU support via the WebGPURenderer. As browsers ship WebGPU by default, Three.js provides a migration path from WebGL. The API remains largely the same; you swap the renderer.
Both are full-featured 3D libraries for the web. Three.js has a larger community, more examples, and a thinner API surface. Babylon.js includes a built-in physics engine, GUI system, and scene editor. Three.js is more modular; Babylon.js is more batteries-included.
Yes. React Three Fiber (R3F) is a React renderer for Three.js that lets you write 3D scenes as JSX components. It handles the render loop, cleanup, and React integration. It is the standard way to use Three.js in React applications.
Three.js supports glTF/GLB (recommended), OBJ, FBX, STL, PLY, USDZ, and more via loader addons. glTF is the recommended format because it is compact, well-specified, and supports PBR materials, animations, and scene hierarchy.
Yes. Three.js powers 3D experiences on major websites including product configurators, real estate tours, and data visualizations. Performance depends on scene complexity and optimization. Use techniques like instanced rendering, texture compression, and LOD for large scenes.
Citations (3)
- Three.js GitHub— Three.js is the most popular JavaScript 3D library
- Khronos Group glTF Spec— glTF is the recommended 3D format for web applications
- W3C WebGPU Specification— WebGPU API specification for next-generation GPU access
Related on TokRepo
Discussion
Related Assets
NAPI-RS — Build Node.js Native Addons in Rust
Write high-performance Node.js native modules in Rust with automatic TypeScript type generation and cross-platform prebuilt binaries.
Mamba — Fast Cross-Platform Package Manager
A drop-in conda replacement written in C++ that resolves environments in seconds instead of minutes.
Plasmo — The Browser Extension Framework
Build, test, and publish browser extensions for Chrome, Firefox, and Edge using React or Vue with hot-reload and automatic manifest generation.