Create the foundation of a three.js app: module loading, the
scene/camera/renderer trio, the render loop, responsive resizing, and camera
controls. Patterns target r184. Read the installed three version before
changing an existing project because examples and addons move across releases.
OrbitControls.package.json depends on three and code does import * as THREE from 'three'.When not to use: loading .gltf/.glb models or skinned animation →
threejs-gltf-loading. Materials, lights, shadows, environment maps →
threejs-materials-lighting. 2D rendering → pixijs-rendering.
'three' and 'three/addons/' must be mapped (in HTML or by a
bundler). Addons (controls, loaders) live under three/addons/....Scene (root of the graph), a PerspectiveCamera(fov, aspect, near, far) moved back from the origin, and a WebGLRenderer whose
domElement is in the DOM. Set size and pixelRatio.new Mesh(geometry, material) and scene.add(mesh). With a
lit material you also need a light (see threejs-materials-lighting).renderer.setAnimationLoop(fn). It's the modern,
WebXR-/WebGPU-safe replacement for hand-rolled requestAnimationFrame. Use a
Clock for delta time.camera.aspect, call updateProjectionMatrix(), and renderer.setSize(...).OrbitControls for orbit/pan/zoom while developing. Confirm something
actually renders (a lit cube, the controls responding) before assuming success.<canvas id="c"></canvas>
<script type="importmap">
{
"imports": {
"three": "https://cdn.jsdelivr.net/npm/three@0.184.0/build/three.module.js",
"three/addons/": "https://cdn.jsdelivr.net/npm/three@0.184.0/examples/jsm/"
}
}
</script>
<script type="module" src="./main.js"></script>
With a bundler (Vite/webpack), skip the import map and just
npm i three; the same import statements resolve.
// main.js
import * as THREE from 'three';
const canvas = document.querySelector('#c');
const renderer = new THREE.WebGLRenderer({ canvas, antialias: true });
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2)); // cap for perf
renderer.setSize(window.innerWidth, window.innerHeight);
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x101018);
const camera = new THREE.PerspectiveCamera(
60, // vertical field of view (degrees)
window.innerWidth / window.innerHeight, // aspect
0.1, // near
100 // far
);
camera.position.set(3, 2, 5);
camera.lookAt(0, 0, 0);
const cube = new THREE.Mesh(
new THREE.BoxGeometry(1, 1, 1),
new THREE.MeshNormalMaterial() // unlit; shows orientation without a light
);
scene.add(cube);
const clock = new THREE.Clock();
renderer.setAnimationLoop(() => {
const dt = clock.getDelta(); // seconds since last frame
cube.rotation.x += dt; // frame-rate independent
cube.rotation.y += dt * 0.7;
renderer.render(scene, camera);
});
// renderer.setAnimationLoop(null); // stop the loop
function onResize() {
const w = window.innerWidth, h = window.innerHeight;
camera.aspect = w / h;
camera.updateProjectionMatrix(); // required after changing aspect
renderer.setSize(w, h);
}
window.addEventListener('resize', onResize);
import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
const controls = new OrbitControls(camera, renderer.domElement);
controls.enableDamping = true; // inertial feel
controls.target.set(0, 0, 0);
renderer.setAnimationLoop(() => {
controls.update(); // needed every frame when damping is on
renderer.render(scene, camera);
});
Failed to resolve module specifier "three" → missing import map (or bundler
config). Map both "three" and "three/addons/"; addon paths must end with /.MeshStandardMaterial) with no light. Move the camera
back; use MeshNormalMaterial/MeshBasicMaterial to verify geometry first.renderer.render inside the loop, or you
call setAnimationLoop but render outside it.camera.aspect + updateProjectionMatrix().renderer.setPixelRatio(...); cap it (≈2) so
4K/retina screens don't tank performance.enableDamping = true you must call
controls.update() every frame.<script src="three.min.js"> → since r147 three.js ships
ES modules only; use type="module" + import maps.Group, parent/child transforms,
Object3D add/remove), OrthographicCamera for 2.5D, and disposing of
geometries/materials/textures to avoid leaks, read references/scene-graph.md.threejs-materials-lighting — give surfaces a lit look (lights, shadows, PBR).threejs-gltf-loading — load 3D models and play their animations.pixijs-rendering — 2D rendering in the browser.fps-shooter — a 3D genre template that composes three.js skills.