Three.js 3D website: How to Create (Step-by-Step Guide)

Threejs Blog Post

Introduction: Why Go 3D?

For years, the web has been dominated by two dimensions. But today, with powerful browser technologies, you can transform a flat website into an immersive, interactive Three.js 3D website experience. This isn’t just a gimmick—it’s a way to build dynamic product showcases, engaging digital portfolios, and memorable storytelling environments.

The magic that makes this possible is WebGL (Web Graphics Library), an API that allows JavaScript to render interactive 2D and 3D graphics within any compatible web browser without using plug-ins. While WebGL is powerful, coding directly with it can be complex. That’s where Three.js comes in.

This guide will walk you through setting up your first 3D website from scratch using the industry-standard library, Three.js, to harness the power of WebGL.


Step 1: Setting Up Your Project Environment

Before we dive into the code, let’s create a basic foundation for our 3D website.

1. File Structure

Create a new folder named my-3d-website with the following files:

  • index.html (The structure)
  • styles.css (The styling)
  • script.js (The 3D logic)

2. Basic HTML

In your index.html, set up the page and link your files. We’ll add a simple canvas-like container for the 3D scene, which Three.js will manage.

HTML

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>My First 3D Website</title>
    <link rel="stylesheet" href="styles.css">
</head>
<body>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/r128/three.min.js"></script>
    
    <script src="script.js"></script>
</body>
</html>

3. Essential CSS

To ensure the 3D scene fills the entire viewport, add this minimal CSS to styles.css. This is crucial for a full-screen 3D website.

CSS

body {
    margin: 0;
    overflow: hidden; /* Hide scrollbars */
}
canvas {
    display: block; /* Remove default padding/margin issues */
}

Step 2: The Four Pillars of a Three.js Scene

Every Three.js scene relies on four core components. We’ll initialize them in script.js.

1. Scene

The Scene acts as a container, holding all your objects, cameras, and lights.

JavaScript

// 1. SCENE: Container for all objects
const scene = new THREE.Scene();

2. Camera

The Camera defines what the user sees. The PerspectiveCamera mimics human vision.

  • 75 (Field of View): How much of the scene is visible.
  • window.innerWidth / window.innerHeight (Aspect Ratio): Crucial for preventing distortion.
  • 0.1 and 1000 (Clipping Planes): Objects closer than 0.1 or farther than 1000 won’t be rendered.

JavaScript

// 2. CAMERA: Defines the viewer's perspective
const camera = new THREE.PerspectiveCamera(
    75, // FOV
    window.innerWidth / window.innerHeight, // Aspect Ratio
    0.1, // Near Clipping Plane
    1000 // Far Clipping Plane
);
camera.position.z = 5; // Move the camera back 5 units so we can see the origin (0,0,0)

3. Renderer

The Renderer draws the scene onto the canvas using WebGL.

JavaScript

// 3. RENDERER: Renders the scene using WebGL
const renderer = new THREE.WebGLRenderer();
renderer.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(renderer.domElement); // Add the <canvas> to the HTML

4. Mesh (Geometry + Material)

A Mesh is a visible object in your scene. It combines Geometry (the shape) and Material (the look/color).

JavaScript

// 4. MESH: Our 3D Object
// Geometry (Shape): A simple BoxGeometry
const geometry = new THREE.BoxGeometry(1, 1, 1);
// Material (Look): A basic color material
const material = new THREE.MeshBasicMaterial({ color: 0x00ff00 }); // Green color
// Mesh (Object): Combine geometry and material
const cube = new THREE.Mesh(geometry, material);

scene.add(cube); // Add the cube to the scene

Step 3: Creating the Animation Loop

A static scene isn’t much fun. To make our cube move or to allow the camera to orbit, we need an animation loop. This loop is a function that calls itself repeatedly, about 60 times per second (60 frames per second or FPS).

We use requestAnimationFrame for an efficient loop that pauses when the user switches tabs.

JavaScript

// The core animation function
function animate() {
    // 1. Request the next frame (recursively calls animate)
    requestAnimationFrame(animate);

    // 2. Update object properties (Animation Logic)
    cube.rotation.x += 0.01;
    cube.rotation.y += 0.01;

    // 3. Render the scene from the camera's perspective
    renderer.render(scene, camera);
}

// Start the animation loop
animate();

If you open your index.html file now, you’ll see a green, spinning cube—you’ve officially built your first 3D website component! 🎉


Step 4: Making It Responsive

3D scenes must adapt to screen size changes (e.g., when a user rotates their phone or resizes the browser window).

Add an event listener to handle window resizing:

JavaScript

window.addEventListener('resize', () => {
    // 1. Update Camera Aspect Ratio
    camera.aspect = window.innerWidth / window.innerHeight;
    camera.updateProjectionMatrix(); // Recalculate the camera's view frustum

    // 2. Update Renderer Size
    renderer.setSize(window.innerWidth, window.innerHeight);

    // 3. Update Pixel Ratio (for sharp rendering on high-DPI screens)
    renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
});

Summary and Next Steps

You’ve successfully created a foundation for a 3D website by mastering the core concepts: Scene, Camera, Renderer, and Mesh. The simple cube and the animation loop demonstrate the fundamental principles of 3D web development using Three.js.

To take your 3D website to the next level, explore these concepts:

  1. Lights: Use THREE.AmbientLight and THREE.DirectionalLight and switch from MeshBasicMaterial to MeshStandardMaterial to add depth and realistic shading.
  2. Controls: Implement OrbitControls to let the user navigate the scene with their mouse.
  3. Complex Models: Import models created in software like Blender (using GLTF format) to build professional-grade 3D experiences.
  4. Physics: Integrate libraries like Cannon.js to add real-world physics to your objects.

Share this Article

Let's get Started

Make your brand unforgettable with 3D, VR, and interactive solutions.

Let’s collaborate to transform your vision into an experience that engages, inspires, and delivers results.

Threejs Blog Post

Three.js 3D website: How to Create (Step-by-Step Guide)

Let's get Started

Make your brand unforgettable with 3D, VR, and interactive solutions.

Let’s collaborate to transform your vision into an experience that engages, inspires, and delivers results.

Leave A Comment

Up

Experience a free demo or a consultation

Share a few details so we can tailor everything to your needs

Get a Demo