---
title: "Three.js: Real-Time 3D You Build With Code"
author: "Ecem Karaman"
source: "https://aiwithecem.com/guides/threejs-3d-website"
published: 2026-11-10
tools: ["Claude","Codex","Multi-Tool"]
topics: ["Building","Creative"]
---

# Three.js: Real-Time 3D You Build With Code

Three.js is a JavaScript library for making 3D that runs right in the browser. It's behind a lot of things you've probably clicked around in: product viewers, data you can fly through, browser games, VR scenes, and the [compass on my homepage](/). You don't have to be a graphics programmer to use it, especially if a coding agent is doing the typing. It does help to know the basic pieces, though, so you can ask for the right thing and spot when something's off. Below is how a scene fits together, how to keep it fast, and some sites worth studying.

> **In this guide**
>
> 1. [What Three.js does](#what-threejs-does)
> 2. [The six pieces of every scene](#the-six-pieces-of-every-scene)
> 3. [How they work together](#how-they-work-together)
> 4. [Pick your setup](#pick-your-setup)
> 5. [Ship it without slowing your site](#ship-it-without-slowing-your-site)
> 6. [Sites worth studying](#sites-worth-studying)
> 7. [Your first build](#your-first-build)

## What Three.js does

Browsers can already draw 3D through WebGL (and now WebGPU), but the raw API is low-level: buffers, shaders, matrices. Three.js wraps it in objects you can reason about, like "a box with a matte green surface, lit from the top left, seen from here."

The key difference from a video or GIF: the scene is drawn live, many times a second. That means it can react to a click, a scroll, the mouse, or your data. If nothing on your page needs to react, a video is simpler.

## The six pieces of every scene

| Piece | What it is | Think of it as |
| ----- | ---------- | -------------- |
| **Scene** | The container for everything you want to show | The stage |
| **Camera** | Decides what part of the scene is visible. `PerspectiveCamera` takes a field of view, aspect ratio, and near/far limits | Where the viewer stands |
| **Geometry** | The shape: points and faces. `BoxGeometry`, `SphereGeometry`, or a model you load | The skeleton |
| **Material** | How the surface looks: color, roughness, metalness, texture | The paint |
| **Mesh** | One geometry plus one material, which you can place, rotate and scale | An actor on stage |
| **Renderer** | Draws the scene from the camera onto a `<canvas>` | The projector |

Two more you'll meet early: **lights** (most realistic materials, like `MeshStandardMaterial`, render black without one) and **loaders** like `GLTFLoader`, which bring in models made in Blender or another 3D tool as `.glb` files.

## How they work together

Every Three.js page follows the same loop. Here's the official minimal example, a spinning cube:

```js
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);
renderer.setAnimationLoop(animate);
document.body.appendChild(renderer.domElement);

const cube = new THREE.Mesh(
  new THREE.BoxGeometry(1, 1, 1),
  new THREE.MeshBasicMaterial({ color: 0x00ff00 })
);
scene.add(cube);
camera.position.z = 5;

function animate(time) {
  cube.rotation.x = time / 2000;
  cube.rotation.y = time / 1000;
  renderer.render(scene, camera);
}
```

Read it as three steps:

1. **Build the world.** Make meshes from geometry and material, then `scene.add()` them. Objects can be nested, so moving a parent moves its children (a lid hinged to a box, a planet with its moon).
2. **Point the camera.** Everything starts at (0, 0, 0), so the camera moves back to see the cube instead of sitting inside it.
3. **Render in a loop.** `setAnimationLoop` calls `animate` on every screen refresh. Change positions or rotations there, then render. That's all animation is: small changes, redrawn fast.

Interaction plugs into the same loop. A mouse move or button click updates a value, and the next frame draws it.

## Pick your setup

| If your site is... | Use | Why |
| ------------------ | --- | --- |
| Plain HTML or a no-code builder | **Three.js directly** | One script, no framework needed |
| React or Next.js | **React Three Fiber** (+ Drei for ready-made helpers) | Write the scene as components; it creates the scene, camera and renderer for you |
| A scroll-driven story | **Three.js + GSAP ScrollTrigger** | GSAP ties animation to scroll position |
| Design-first, little code | **Spline** | Build visually and embed; less control over custom behavior |

React Three Fiber is a React renderer *for* Three.js, not a replacement. Version matters: R3F v9 pairs with React 19, and v8 with React 18. My site is Next.js, so both the compass and the [Skill Machine](/products) use R3F v9.

## Ship it without slowing your site

These are the settings I'd ask a coding agent for on any small 3D feature. All of them are in my site's components.

- **Load it on the client only.** In Next.js, import the scene with `dynamic(..., { ssr: false })`. There's no GPU on the server.
- **Render on demand.** For something that only moves when clicked, R3F's `frameloop="demand"` draws a frame only when something changes, instead of 60 times a second forever.
- **Cap the pixel ratio.** `dpr={[1, 1.5]}` stops high-density phone screens from rendering three or four times the pixels.
- **Respect reduced motion.** Check `prefers-reduced-motion` and skip straight to the end state.
- **Have a fallback.** Some devices can't run WebGL. Show a static version so the content still works without the 3D.
- **Use primitives before models.** Boxes and cylinders load instantly. A downloaded model needs compression and can quickly outweigh the rest of your page.

Few triangles doesn't guarantee speed. A heavy material or a full-resolution canvas on a phone can be slower than a detailed model.

## Sites worth studying

- **[Bruno Simon's portfolio](https://bruno-simon.com/):** you drive a little car around a world to find his projects. The [source is open](https://github.com/brunosimon/folio-2025) (MIT), including how assets go from Blender to the browser. Study it for navigation as play.
- **[Realistic water simulation](https://water-simulation.vercel.app/):** a pool with ripples, light patterns on the floor and droplets on the camera, built with React Three Fiber and shaders. The [builder's breakdown](https://andersonmancini.dev/blog/realistic-water-simulation) shows how each layer adds one visual cue.
- **[3D Force Graph](https://vasturiano.github.io/3d-force-graph/example/large-graph/):** a library for networks you can orbit and click. Useful for showing how ideas, tools or people connect. In 3D, nodes can hide behind each other, so use 2D if people need to read the data precisely.
- **[Three.js examples](https://threejs.org/examples/):** hundreds of official demos, each with source. The fastest way to find "the effect I want" and hand its code to an agent.
- **[Awwwards Three.js collection](https://www.awwwards.com/websites/three-js/):** award-winning sites built with it, for design inspiration.

## Your first build

Before you start, ask: would this be easier to understand, explore or remember in 3D than as a normal card or button? If not, skip the 3D.

If yes, start with one small object and one interaction. Paste this into Claude Code or Codex from your project folder:

```text
Add a small interactive 3D element to [page]: a [simple object, e.g. a box, globe, compass] that [interaction, e.g. spins and reveals a link when clicked]. Inspect the project first and use Three.js directly, or React Three Fiber if this is a React/Next.js site. Use simple built-in geometry and my existing colors, no external models. Load it client-side only, render on demand, cap the pixel ratio at 1.5, support prefers-reduced-motion, and show a static fallback if WebGL isn't available. Keep the change isolated, run it locally, and stop.
```

Then open the [Three.js Editor](https://threejs.org/editor/) in another tab. Add a box, a light and a camera, and move them around. Ten minutes there makes the code above much easier to read.

**Details checked against the [Three.js manual](https://threejs.org/manual/pages/creating-a-scene), the [React Three Fiber docs](https://r3f.docs.pmnd.rs/getting-started/introduction) and the [r187 release](https://github.com/mrdoob/three.js/releases/tag/r187) on November 10, 2026.**

**Go deeper:** [Three.js manual](https://threejs.org/manual/) · [Drei helpers](https://pmndrs.github.io/drei) · [GSAP ScrollTrigger](https://gsap.com/docs/v3/Plugins/ScrollTrigger/) · [What to build with GPT-6 Astra](/guides/build-with-astra)
