Three.js Setup — Creative Web

Install three (0.186.1) with npm or an import map, then import * as THREE from 'three'. Three required parts: the Scene (the container; scene.add fills…

The triangle in the WebGL Concepts lesson took 55 lines: shaders, buffers, a program, all written by hand. three.js, a JavaScript library, prepares all of that for you. You just say "there's a box here, the camera is there", and three.js deals with the GPU. In this lesson we set up the first 3D stage in Raka's shop: the Coffee Print Tee box, spinning.

It works much like a product photo in Raka's studio. There's a table where the product is arranged, a camera with its lens and distance, and a photographer who presses the button so a photo comes out. In three.js the table is the Scene, the camera is the Camera, and the photographer is the Renderer, which "photographs" the scene through the camera onto a <canvas>. Miss any one of them and there's no picture.

Installing three.js

The version used in this lesson: 0.186.1 (r186, as of October 2026). With a bundler like Vite:

npm install three
import * as THREE from 'three';

Without a bundler, the three.js docs use an import map in <head>, so the browser knows where 'three' comes from:

<script type="importmap">
{
  "imports": {
    "three": "https://cdn.jsdelivr.net/npm/[email protected]/build/three.module.js",
    "three/addons/": "https://cdn.jsdelivr.net/npm/[email protected]/examples/jsm/"
  }
}
</script>
<script type="module" src="main.js"></script>

Scene, camera, renderer

<div id="shop-3d" style="width: 320px; height: 240px"></div>
import * as THREE from 'three';

const container = document.getElementById('shop-3d');

// 1. Scene: the stage where everything is placed
const scene = new THREE.Scene();
scene.background = new THREE.Color('#1c1917');

// 2. Camera: fov 75°, aspect width / height, near 0.1, far 4
const camera = new THREE.PerspectiveCamera(75, container.clientWidth / container.clientHeight, 0.1, 4);
camera.position.z = 2;

// 3. Renderer: draws onto a <canvas> of its own
const renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(container.clientWidth, container.clientHeight);
container.appendChild(renderer.domElement);

// The Coffee Print Tee box: 1.6 × 1 × 0.4
const box = new THREE.Mesh(
  new THREE.BoxGeometry(1.6, 1, 0.4),
  new THREE.MeshNormalMaterial()
);
box.rotation.x = 0.4;
scene.add(box);

Other options for the renderer:

Renderer When to pick it
WebGLRenderer (used in this lesson) almost every project. It runs in every browser with WebGL 2, and it has the most examples. Pick this if unsure
WebGPURenderer from three/webgpu you need TSL (the three.js shader language, written in JavaScript) or compute shaders (calculations on the GPU). If the browser has no WebGPU yet, this renderer switches to WebGL 2 by itself. Before the first render(), wait for await renderer.init(), or use setAnimationLoop, which waits by itself. Without it, three.js throws the error .render() called before the backend is initialized

How much you can see

The height of the world visible at distance d is 2 × d × tan(fov / 2). A box 1 tall at distance 2, on a canvas 240 px tall:

fov Visible height The box fills Roughly
30° 1.07 93% 224 px, like a telephoto lens
75° 3.07 33% 78 px
110° 5.71 18% 42 px, like a wide lens

fov is the parameter that changes the look most: the bigger it is, the more fits in and the smaller each object. far is deliberately 4 here, enough for a box at distance 2: push the camera to z = 4 and the back half of the box goes past far, so it gets cut off.

The render loop

One renderer.render(scene, camera) makes one photo. To make it move, render every frame:

renderer.setAnimationLoop((time) => {
  box.rotation.y = time / 1000; // 1 radian per second
  renderer.render(scene, camera);
});

Resize

The 3D container in Raka's shop (#shop-3d, the element the canvas sits in) narrows from 320 to 160 px (its height stays 240). If the resize code changes camera.aspect and renderer.setSize but forgets to call camera.updateProjectionMatrix(), does the box look normal, stretched, or squashed? Guess first, then look at the right code:

new ResizeObserver(() => {
  camera.aspect = container.clientWidth / container.clientHeight;
  camera.updateProjectionMatrix();
  renderer.setSize(container.clientWidth, container.clientHeight);
  renderer.render(scene, camera); // draw again, also while the loop is stopped
}).observe(container);

The camera keeps its projection maths in a matrix, and that matrix is only recomputed when updateProjectionMatrix() is called. Without it, a 160 × 240 container (aspect 0.67) is still drawn with the old aspect 1.33: everything is squashed to half its width. The same goes after changing camera.fov. If the canvas is as wide as the window, use window.addEventListener('resize', ...) with window.innerWidth and window.innerHeight.

⚠️ Black screens, stretched pictures and failed imports

TL;DR: Install three (0.186.1) with npm or an import map, then import * as THREE from 'three'. Three required parts: the Scene (the container; scene.add fills scene.children), the Camera (PerspectiveCamera(fov, aspect, near, far): fov in vertical degrees, aspect width / height, near and far the distance limits; move it back with position.z), and the Renderer (WebGLRenderer, setPixelRatio(Math.min(devicePixelRatio, 2)), setSize, appendChild(renderer.domElement)). A mesh = geometry + material. Move things with renderer.setAnimationLoop((time) => { ...; renderer.render(scene, camera) }), angles from the time. When the size changes: camera.aspect, camera.updateProjectionMatrix(), renderer.setSize.

⚡ Table, camera, photographer

Write:

const scene = new THREE.Scene();
const camera = new THREE.PerspectiveCamera(75, container.clientWidth / container.clientHeight, 0.1, 4);
camera.position.z = 2;
const renderer = new THREE.WebGLRenderer({ antialias: true });
container.appendChild(renderer.domElement);

Meaning: Three required parts: the Scene (the table where things are arranged), the Camera (the point of view), and the Renderer (the photographer who photographs the scene through the camera onto a <canvas>). Miss any one and there's no picture. The camera is moved back with position.z, because it starts at (0, 0, 0) together with the objects.

⚡ The render loop

Write:

renderer.setAnimationLoop((time) => {
  box.rotation.y = time / 1000; // 1 radian per second
  renderer.render(scene, camera);
});

Meaning: One render is one photo. To make it move, render every frame. The angle is computed from the time, so the speed is the same on 60 Hz and 120 Hz screens.

⚡ When the size changes

Write:

camera.aspect = container.clientWidth / container.clientHeight;
camera.updateProjectionMatrix();
renderer.setSize(container.clientWidth, container.clientHeight);
Guess first: the container narrows from 320 to 160 px, but updateProjectionMatrix() is never called. What does the box look like?

Squashed to half its width. The camera's matrix still uses the old aspect, 1.33.

Meaning: Every time aspect or fov changes, call updateProjectionMatrix(), then the renderer's setSize.

What you will learn