Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
367 changes: 367 additions & 0 deletions dolly-zoom/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,367 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Dolly Zoom Demo</title>
<style>
html, body {
margin: 0;
padding: 0;
overflow: hidden;
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Helvetica, Arial, sans-serif;
background: #87ceeb;
color: #fff;
}
canvas { display: block; }

#info {
position: absolute;
top: 16px;
left: 16px;
background: rgba(0, 0, 0, 0.55);
padding: 12px 14px;
border-radius: 10px;
font-size: 13px;
line-height: 1.45;
max-width: 320px;
pointer-events: none;
}
#info strong { font-size: 14px; }

#controls {
position: absolute;
bottom: 24px;
left: 50%;
transform: translateX(-50%);
background: rgba(0, 0, 0, 0.6);
padding: 14px 22px;
border-radius: 14px;
display: flex;
align-items: center;
gap: 14px;
min-width: 360px;
box-shadow: 0 8px 24px rgba(0, 0, 0, 0.25);
}
#controls label {
font-size: 13px;
letter-spacing: 0.04em;
text-transform: uppercase;
opacity: 0.85;
}
#zoom { flex: 1; accent-color: #ff7043; }
#zoomVal {
font-variant-numeric: tabular-nums;
min-width: 56px;
text-align: right;
font-size: 15px;
font-weight: 600;
}

#stats {
position: absolute;
top: 16px;
right: 16px;
background: rgba(0, 0, 0, 0.55);
padding: 10px 14px;
border-radius: 10px;
font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
font-size: 12px;
line-height: 1.6;
pointer-events: none;
}
#stats span { color: #ffd180; }
</style>
</head>
<body>
<div id="info">
<strong>Dolly Zoom Demo</strong><br>
The foreground person stays the same size on screen.<br>
As you zoom in, the camera dollies back to compensate.<br>
Watch the second person — 100&nbsp;m behind — appear to grow,
and the surroundings compress.
</div>

<div id="stats">
FOV:&nbsp;&nbsp;&nbsp;&nbsp;<span id="fovVal">60.0°</span><br>
Cam&nbsp;dist:&nbsp;<span id="distVal">5.0 m</span><br>
To&nbsp;person&nbsp;2:&nbsp;<span id="dist2Val">105.0 m</span>
</div>

<div id="controls">
<label for="zoom">Zoom</label>
<input id="zoom" type="range" min="1" max="20" step="0.05" value="1">
<span id="zoomVal">1.00×</span>
</div>

<script type="importmap">
{
"imports": {
"three": "./three.module.js"
}
}
</script>
<script type="module">
import * as THREE from 'three';

const BASE_FOV = 60; // degrees, at zoom = 1
const BASE_DISTANCE = 5; // metres from camera to subject at zoom = 1
const CAM_HEIGHT = 1.65; // eye height
const PERSON2_OFFSET = 100; // metres behind subject

const scene = new THREE.Scene();
scene.background = new THREE.Color(0x9bd3ee);
scene.fog = new THREE.Fog(0xb8dff0, 220, 900);

const camera = new THREE.PerspectiveCamera(BASE_FOV, innerWidth / innerHeight, 0.1, 2000);
camera.position.set(0, CAM_HEIGHT, BASE_DISTANCE);
camera.lookAt(0, CAM_HEIGHT, 0);

const renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setPixelRatio(Math.min(devicePixelRatio, 2));
renderer.setSize(innerWidth, innerHeight);
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
document.body.appendChild(renderer.domElement);

// --- Lighting -----------------------------------------------------------
const sun = new THREE.DirectionalLight(0xfff2d6, 1.15);
sun.position.set(60, 100, 40);
sun.castShadow = true;
sun.shadow.mapSize.set(2048, 2048);
const s = sun.shadow.camera;
s.left = -120; s.right = 120; s.top = 120; s.bottom = -120;
s.near = 1; s.far = 400;
scene.add(sun);
scene.add(new THREE.AmbientLight(0xffffff, 0.45));
scene.add(new THREE.HemisphereLight(0x9bd3ee, 0x4a6b2a, 0.45));

// --- Ground -------------------------------------------------------------
const ground = new THREE.Mesh(
new THREE.PlaneGeometry(2000, 2000),
new THREE.MeshLambertMaterial({ color: 0x6fa84a })
);
ground.rotation.x = -Math.PI / 2;
ground.receiveShadow = true;
scene.add(ground);

// Dirt path running from in front of subject far back past person 2
const path = new THREE.Mesh(
new THREE.PlaneGeometry(3.2, 320),
new THREE.MeshLambertMaterial({ color: 0x927043 })
);
path.rotation.x = -Math.PI / 2;
path.position.set(0, 0.01, -150);
path.receiveShadow = true;
scene.add(path);

// --- Helpers ------------------------------------------------------------
const rand = (a, b) => a + Math.random() * (b - a);

function makePerson(shirtColor, pantsColor = 0x223366, hatColor = null) {
const g = new THREE.Group();
const skin = new THREE.MeshLambertMaterial({ color: 0xf2c79c });
const shirt = new THREE.MeshLambertMaterial({ color: shirtColor });
const pants = new THREE.MeshLambertMaterial({ color: pantsColor });

const lLeg = new THREE.Mesh(new THREE.CylinderGeometry(0.12, 0.12, 0.85, 12), pants);
lLeg.position.set(-0.13, 0.43, 0);
const rLeg = lLeg.clone(); rLeg.position.x = 0.13;
g.add(lLeg, rLeg);

const body = new THREE.Mesh(new THREE.CylinderGeometry(0.28, 0.32, 0.78, 16), shirt);
body.position.y = 1.24;
g.add(body);

const lArm = new THREE.Mesh(new THREE.CylinderGeometry(0.09, 0.09, 0.72, 12), shirt);
lArm.position.set(-0.37, 1.24, 0);
const rArm = lArm.clone(); rArm.position.x = 0.37;
g.add(lArm, rArm);

const head = new THREE.Mesh(new THREE.SphereGeometry(0.18, 16, 16), skin);
head.position.y = 1.81;
g.add(head);

if (hatColor !== null) {
const hat = new THREE.Mesh(
new THREE.ConeGeometry(0.22, 0.26, 16),
new THREE.MeshLambertMaterial({ color: hatColor })
);
hat.position.y = 2.04;
g.add(hat);
}

g.traverse(o => { if (o.isMesh) { o.castShadow = true; o.receiveShadow = true; } });
return g;
}

function makeTree(scale = 1) {
const g = new THREE.Group();
const trunk = new THREE.Mesh(
new THREE.CylinderGeometry(0.28 * scale, 0.38 * scale, 2.4 * scale, 8),
new THREE.MeshLambertMaterial({ color: 0x5a3a1e })
);
trunk.position.y = 1.2 * scale;
g.add(trunk);

const greens = [0x2e7d32, 0x388e3c, 0x4a8b3a, 0x356b29, 0x4f9b48];
const foliage = new THREE.Mesh(
new THREE.ConeGeometry(2 * scale, 5 * scale, 12),
new THREE.MeshLambertMaterial({ color: greens[Math.floor(Math.random() * greens.length)] })
);
foliage.position.y = (2.4 + 5 / 2) * scale;
g.add(foliage);

g.traverse(o => { if (o.isMesh) { o.castShadow = true; o.receiveShadow = true; } });
return g;
}

function makeHouse() {
const g = new THREE.Group();
const w = rand(5, 8);
const d = rand(5, 8);
const h = rand(3, 4.5);
const wallColor = new THREE.Color().setHSL(0.07 + Math.random() * 0.08, 0.45, 0.7);
const walls = new THREE.Mesh(
new THREE.BoxGeometry(w, h, d),
new THREE.MeshLambertMaterial({ color: wallColor })
);
walls.position.y = h / 2;
g.add(walls);

const roofColors = [0x8a3324, 0x5d4037, 0x6d4c41, 0x7b3f00];
const roof = new THREE.Mesh(
new THREE.ConeGeometry(Math.max(w, d) * 0.78, h * 0.75, 4),
new THREE.MeshLambertMaterial({ color: roofColors[Math.floor(Math.random() * roofColors.length)] })
);
roof.position.y = h + h * 0.375;
roof.rotation.y = Math.PI / 4;
g.add(roof);

// Door
const door = new THREE.Mesh(
new THREE.PlaneGeometry(0.9, 1.6),
new THREE.MeshLambertMaterial({ color: 0x3e2a14 })
);
door.position.set(0, 0.8, d / 2 + 0.01);
g.add(door);

// Windows
const winMat = new THREE.MeshLambertMaterial({ color: 0xfff7c2, emissive: 0x222200 });
const w1 = new THREE.Mesh(new THREE.PlaneGeometry(0.8, 0.8), winMat);
w1.position.set(-w / 3, h / 2 + 0.2, d / 2 + 0.01);
const w2 = w1.clone(); w2.position.x = w / 3;
g.add(w1, w2);

g.traverse(o => { if (o.isMesh) { o.castShadow = true; o.receiveShadow = true; } });
return g;
}

// --- Scene populating ---------------------------------------------------
// Subject (foreground person), facing camera
const subject = makePerson(0xd24343, 0x223366);
subject.position.set(0, 0, 0);
subject.rotation.y = Math.PI; // face +z (towards camera)
scene.add(subject);

// Second person, 100 m behind subject, offset slightly to one side so
// they're never fully occluded by the foreground person, and facing camera.
const person2 = makePerson(0x3d6cb4, 0x222222, 0xf6c84c);
person2.position.set(1.4, 0, -PERSON2_OFFSET);
person2.rotation.y = Math.PI;
scene.add(person2);

// Trees scattered along both sides, well behind the subject
// (kept out of the immediate space between camera and subject)
// Keep a clear corridor on the path centerline so person 2 stays visible
// even at the narrow FOVs reached at high zoom.
for (let i = 0; i < 90; i++) {
const t = makeTree(0.85 + Math.random() * 0.6);
const side = Math.random() < 0.5 ? -1 : 1;
const xOff = side * rand(8, 70);
const z = rand(-220, -2);
t.position.set(xOff, 0, z);
t.rotation.y = Math.random() * Math.PI * 2;
scene.add(t);
}

// Distant tree line
for (let i = 0; i < 80; i++) {
const t = makeTree(1 + Math.random() * 0.9);
t.position.set(rand(-260, 260), 0, rand(-320, -230));
scene.add(t);
}

// Houses scattered along the sides
const housePositions = [
[-18, -12], [22, -25], [-30, -55], [28, -68], [-24, -90],
[32, -105], [-38, -130], [26, -148], [-20, -170], [34, -190],
[-44, -210], [40, -40], [-15, -78], [18, -125]
];
housePositions.forEach(([x, z]) => {
const h = makeHouse();
h.position.set(x, 0, z);
h.rotation.y = (x > 0 ? -1 : 1) * (Math.PI / 2 + rand(-0.4, 0.4));
scene.add(h);
});

// A few fence posts along the path for extra depth cue
const fenceMat = new THREE.MeshLambertMaterial({ color: 0x8a6a3c });
for (let z = -10; z >= -210; z -= 6) {
for (const side of [-1, 1]) {
const post = new THREE.Mesh(
new THREE.BoxGeometry(0.12, 1.1, 0.12),
fenceMat
);
post.position.set(side * 2.4, 0.55, z);
post.castShadow = true;
scene.add(post);
}
}

// --- Resize -------------------------------------------------------------
addEventListener('resize', () => {
camera.aspect = innerWidth / innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(innerWidth, innerHeight);
});

// --- Dolly zoom ---------------------------------------------------------
// Keep subject screen size constant: d * tan(fov/2) = const
// Increasing zoom -> narrower fov + camera moves back proportionally.
const baseHalfTan = Math.tan(THREE.MathUtils.degToRad(BASE_FOV) / 2);

const slider = document.getElementById('zoom');
const zoomVal = document.getElementById('zoomVal');
const fovVal = document.getElementById('fovVal');
const distVal = document.getElementById('distVal');
const dist2Val = document.getElementById('dist2Val');

function applyZoom(z) {
const halfFovRad = Math.atan(baseHalfTan / z);
const fovDeg = THREE.MathUtils.radToDeg(halfFovRad * 2);
const distance = BASE_DISTANCE * z;

camera.fov = fovDeg;
camera.position.set(0, CAM_HEIGHT, distance);
camera.lookAt(0, CAM_HEIGHT, 0);
camera.updateProjectionMatrix();

zoomVal.textContent = z.toFixed(2) + '×';
fovVal.textContent = fovDeg.toFixed(1) + '°';
distVal.textContent = distance.toFixed(1) + ' m';
dist2Val.textContent = (distance + PERSON2_OFFSET).toFixed(1) + ' m';
}

slider.addEventListener('input', () => applyZoom(parseFloat(slider.value)));
applyZoom(1);

// --- Render loop --------------------------------------------------------
function tick() {
requestAnimationFrame(tick);
renderer.render(scene, camera);
}
tick();
</script>
</body>
</html>
Binary file added dolly-zoom/preview-strip.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added dolly-zoom/preview.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading