Prompt specification
Build or customize the Three.js 3D scene "Shirt folding into a rectangle" inside the active workspace.
Engineering Constraints:
1. Runtime & Orbit: Use Three.js r160 with OrbitControls (damping enabled) and ACESFilmicToneMapping.
2. Frame Timing: Drive all motion strictly with clock.getDelta() so animation speed is FPS-independent.
3. Viewport Hygiene: Handle window resize cleanly, cap DPR at Math.min(window.devicePixelRatio, 2), and dispose geometries/materials/renderer on unmount.
Scene Metadata:
Scene: Shirt folding into a rectangle
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/shirt-folding-into-a-rectangle-x0574
Scene Description Prompt:
Create a standalone voxel scene in one HTML file using Three.js from cdnjs. Animate sleeves folding inward, sides narrowing, and hem lifting in order; use hinged fabric panels while preserving the shirt's recognizable structure at every step.
Working Three.js Starter (shirt-folding-into-a-rectangle-x0574):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Voxel Fold — Articulated Shirt</title>
<style>
:root {
color-scheme: dark;
--bg: #07111f;
--panel: rgba(9, 22, 37, 0.78);
--border: rgba(157, 224, 236, 0.17);
--text: #e8f8fb;
--muted: #87a9b5;
--cyan: #53e0d0;
--gold: #f6b85d;
--green: #86efac;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: var(--bg);
font-family:
Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
color: var(--text);
}
body {
background:
radial-gradient(circle at 52% 38%, #16344a 0%, #091827 43%, #050c16 100%);
}
#scene {
position: fixed;
inset: 0;
width: 100%;
height: 100%;
display: block;
touch-action: none;
cursor: grab;
}
#scene.dragging {
cursor: grabbing;
}
.hud {
position: fixed;
z-index: 2;
pointer-events: none;
}
.panel {
border: 1px solid var(--border);
background: var(--panel);
box-shadow:
0 20px 70px rgba(0, 0, 0, 0.28),
inset 0 1px rgba(255, 255, 255, 0.035);
backdrop-filter: blur(16px);
-webkit-backdrop-filter: blur(16px);
}
.top-panel {
top: 22px;
left: 22px;
width: min(650px, calc(100vw - 44px));
padding: 18px;
border-radius: 16px;
}
.eyebrow {
display: flex;
align-items: center;
justify-content: space-between;
gap: 20px;
margin-bottom: 10px;
color: var(--cyan);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.2em;
text-transform: uppercase;
}
.live-badge {
display: inline-flex;
align-items: center;
gap: 7px;
color: var(--muted);
letter-spacing: 0.12em;
}
.live-badge::before {
width: 6px;
height: 6px;
border-radius: 50%;
background: var(--green);
box-shadow: 0 0 12px var(--green);
content: "";
}
h1 {
margin: 0;
font-size: clamp(23px, 3vw, 34px);
font-weight: 760;
letter-spacing: -0.045em;
line-height: 1;
}
.subtitle {
margin: 8px 0 16px;
color: var(--muted);
font-size: 12px;
line-height: 1.5;
}
.steps {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 8px;
}
.step {
min-width: 0;
padding: 10px 11px;
border: 1px solid rgba(157, 224, 236, 0.12);
border-radius: 10px;
background: rgba(255, 255, 255, 0.025);
transition:
border-color 220ms ease,
background 220ms ease,
transform 220ms ease;
}
.step.active {
border-color: rgba(83, 224, 208, 0.65);
background: rgba(83, 224, 208, 0.08);
transform: translateY(-2px);
}
.step.complete {
border-color: rgba(134, 239, 172, 0.35);
}
.step-number {
display: block;
margin-bottom: 4px;
color: var(--muted);
font: 700 9px/1 ui-monospace, SFMono-Regular, Menlo, monospace;
letter-spacing: 0.12em;
}
.step.active .step-number {
color: var(--cyan);
}
.step.complete .step-number {
color: var(--green);
}
.step strong {
display: block;
overflow: hidden;
font-size: 12px;
font-weight: 700;
text-overflow: ellipsis;
white-space: nowrap;
}
.step small {
display: block;
margin-top: 3px;
overflow: hidden;
color: var(--muted);
font-size: 10px;
text-overflow: ellipsis;
white-space: nowrap;
}
.side-note {
top: 24px;
right: 24px;
max-width: 220px;
padding: 13px 15px;
border-radius: 12px;
color: var(--muted);
font-size: 10px;
line-height: 1.55;
}
.side-note strong {
display: block;
margin-bottom: 5px;
color: var(--text);
font-size: 10px;
letter-spacing: 0.13em;
text-transform: uppercase;
}
.legend-dot {
display: inline-block;
width: 7px;
height: 7px;
margin-right: 6px;
border-radius: 2px;
background: var(--gold);
box-shadow: 0 0 10px rgba(246, 184, 93, 0.5);
}
.controls {
right: 22px;
bottom: 22px;
left: 22px;
width: min(790px, calc(100vw - 44px));
margin: auto;
padding: 14px 16px 12px;
border-radius: 16px;
pointer-events: auto;
}
.control-row {
display: flex;
align-items: center;
gap: 9px;
}
button {
min-height: 36px;
padding: 0 13px;
border: 1px solid rgba(157, 224, 236, 0.17);
border-radius: 9px;
background: rgba(255, 255, 255, 0.045);
color: var(--text);
font: 700 11px/1 inherit;
cursor: pointer;
transition:
background 160ms ease,
border-color 160ms ease,
transform 160ms ease;
}
button:hover {
border-color: rgba(83, 224, 208, 0.55);
background: rgba(83, 224, 208, 0.09);
}
button:active {
transform: translateY(1px);
}
button.primary {
min-width: 76px;
border-color: rgba(83, 224, 208, 0.4);
background: var(--cyan);
color: #062520;
}
button.primary:hover {
background: #70ebdc;
}
.phase-status {
min-width: 0;
margin-left: 5px;
overflow: hidden;
color: var(--muted);
font: 700 10px/1.2 ui-monospace, SFMono-Regular, Menlo, monospace;
letter-spacing: 0.1em;
text-overflow: ellipsis;
text-transform: uppercase;
white-space: nowrap;
}
.timeline {
display: grid;
grid-template-columns: auto 1fr auto;
align-items: center;
gap: 12px;
margin-top: 12px;
color: var(--muted);
font: 700 9px/1 ui-monospace, SFMono-Regular, Menlo, monospace;
}
input[type="range"] {
width: 100%;
height: 18px;
margin: 0;
appearance: none;
background: transparent;
cursor: pointer;
}
input[type="range"]::-webkit-slider-runnable-track {
height: 3px;
border-radius: 10px;
background: linear-gradient(
90deg,
var(--cyan) 0%,
var(--cyan) var(--progress, 0%),
rgba(157, 224, 236, 0.15) var(--progress, 0%),
rgba(157, 224, 236, 0.15) 100%
);
}
input[type="range"]::-webkit-slider-thumb {
width: 13px;
height: 13px;
margin-top: -5px;
appearance: none;
border: 2px solid #d9fffa;
border-radius: 50%;
background: var(--cyan);
box-shadow: 0 0 12px rgba(83, 224, 208, 0.55);
}
input[type="range"]::-moz-range-track {
height: 3px;
border-radius: 10px;
background: rgba(157, 224, 236, 0.15);
}
input[type="range"]::-moz-range-progress {
height: 3px;
border-radius: 10px;
background: var(--cyan);
}
input[type="range"]::-moz-range-thumb {
width: 11px;
height: 11px;
border: 2px solid #d9fffa;
border-radius: 50%;
background: var(--cyan);
}
.help {
margin-top: 9px;
color: rgba(135, 169, 181, 0.72);
font-size: 9px;
letter-spacing: 0.04em;
text-align: right;
}
@media (max-width: 760px) {
.top-panel {
top: 12px;
left: 12px;
width: calc(100vw - 24px);
padding: 13px;
}
.subtitle,
.side-note {
display: none;
}
.steps {
gap: 5px;
}
.step {
padding: 8px;
}
.step small {
display: none;
}
.controls {
right: 12px;
bottom: 12px;
left: 12px;
width: calc(100vw - 24px);
padding: 11px 12px 10px;
}
.phase-status {
font-size: 8px;
}
button {
padding: 0 10px;
}
.help {
display: none;
}
}
@media (max-width: 430px) {
.eyebrow {
font-size: 8px;
}
.live-badge {
display: none;
}
.control-row {
gap: 6px;
}
.phase-status {
display: none;
}
button {
font-size: 10px;
}
}
</style>
</head>
<body>
<canvas id="scene" aria-label="Interactive animated voxel shirt"></canvas>
<header class="hud top-panel panel">
<div class="eyebrow">
<span>Voxel garment study / 01</span>
<span class="live-badge">Three-panel rig</span>
</div>
<h1>Fold, without losing the shirt.</h1>
<p class="subtitle">
The torso, collar, and shoulders remain anchored while hinged voxel
panels perform a strictly ordered garment sequence.
</p>
<div class="steps">
<div class="step active" data-step="0">
<span class="step-number">01 / HINGE</span>
<strong>Sleeves inward</strong>
<small>Forearms fold over the chest</small>
</div>
<div class="step" data-step="1">
<span class="step-number">02 / HINGE</span>
<strong>Sides narrow</strong>
<small>Side gussets rotate rearward</small>
</div>
<div class="step" data-step="2">
<span class="step-number">03 / HINGE</span>
<strong>Hem lifts</strong>
<small>Lower band swings upward</small>
</div>
</div>
</header>
<aside class="hud side-note panel">
<strong>Construction</strong>
<span class="legend-dot"></span>Amber stitches mark hinge axes.<br />
The central chest and collar never translate, preserving the shirt
silhouette throughout the sequence.
</aside>
<section class="hud controls panel" aria-label="Animation controls">
<div class="control-row">
<button id="play" class="primary" type="button">Pause</button>
<button id="replay" type="button">Replay</button>
<button id="view" type="button">Reset view</button>
<div id="status" class="phase-status" aria-live="polite">
01 / Sleeves folding inward
</div>
</div>
<div class="timeline">
<span>00:00</span>
<input
id="timeline"
type="range"
min="0"
max="7000"
step="1"
value="0"
aria-label="Animation timeline"
/>
<span id="timecode">07:00</span>
</div>
<div class="help">Drag to orbit · Scroll to zoom · Space to pause</div>
</section>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// ------------------------------------------------------------
// Renderer and scene
// ------------------------------------------------------------
const canvas = document.querySelector("#scene");
const renderer = new THREE.WebGLRenderer({
canvas,
antialias: true,
alpha: true,
powerPreference: "high-performance",
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.08;
const scene = new THREE.Scene();
scene.fog = new THREE.Fog(0x07111f, 10, 20);
const camera = new THREE.PerspectiveCamera(
38,
window.innerWidth / window.innerHeight,
0.1,
50
);
const target = new THREE.Vector3(0, 0.2, 0);
const spherical = new THREE.Spherical();
let cameraTouched = false;
function defaultCameraRadius() {
return window.innerWidth < 700 ? 12.2 : 8.2;
}
function resetCamera() {
spherical.set(
defaultCameraRadius(),
1.19,
0.38
);
cameraTouched = false;
updateCamera();
}
function updateCamera() {
spherical.makeSafe();
camera.position.setFromSpherical(spherical).add(target);
camera.lookAt(target);
}
resetCamera();
scene.add(new THREE.HemisphereLight(0xa6e9ff, 0x142033, 2.2));
const keyLight = new THREE.DirectionalLight(0xfff4dc, 3.1);
keyLight.position.set(4.5, 7.5, 6.5);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(2048, 2048);
keyLight.shadow.camera.left = -5;
keyLight.shadow.camera.right = 5;
keyLight.shadow.camera.top = 5;
keyLight.shadow.camera.bottom = -5;
keyLight.shadow.camera.near = 1;
keyLight.shadow.camera.far = 18;
keyLight.shadow.bias = -0.0005;
scene.add(keyLight);
const rimLight = new THREE.DirectionalLight(0x42d9e8, 1.6);
rimLight.position.set(-5, 3.2, -4);
scene.add(rimLight);
const warmLight = new THREE.PointLight(0xffb766, 9, 12, 2);
warmLight.position.set(3, -0.2, 4);
scene.add(warmLight);
// ------------------------------------------------------------
// Voxel construction helpers
// ------------------------------------------------------------
const VOXEL = 0.17;
const STEP = 0.188;
const voxelGeometry = new THREE.BoxGeometry(VOXEL, VOXEL, VOXEL);
const voxelMaterial = new THREE.MeshStandardMaterial({
color: 0xffffff,
roughness: 0.78,
metalness: 0.025,
});
const shirtPalette = [
0x20bdb4,
0x28c8bd,
0x38d1c4,
0x1ca9a3,
0x45d8ca,
0x169b98,
0x56ded0,
];
const collarColor = 0x0b5964;
const cuffColor = 0x137b82;
const hemColor = 0x158d91;
const seamColor = 0x0e747c;
const dummy = new THREE.Object3D();
function addVoxels(parent, specs) {
const mesh = new THREE.InstancedMesh(
voxelGeometry,
voxelMaterial,
specs.length
);
specs.forEach((spec, index) => {
dummy.position.set(spec.x, spec.y, spec.z ?? 0);
dummy.scale.set(
spec.sx ?? 1,
spec.sy ?? 1,
spec.sz ?? 1
);
dummy.rotation.set(0, 0, 0);
dummy.updateMatrix();
mesh.setMatrixAt(index, dummy.matrix);
const color = new THREE.Color(
spec.color ?? shirtPalette[(index * 5 + 3) % shirtPalette.length]
);
mesh.setColorAt(index, color);
});
mesh.instanceMatrix.needsUpdate = true;
if (mesh.instanceColor) mesh.instanceColor.needsUpdate = true;
mesh.castShadow = true;
mesh.receiveShadow = true;
mesh.computeBoundingSphere();
parent.add(mesh);
return mesh;
}
const shirt = new THREE.Group();
scene.add(shirt);
// ------------------------------------------------------------
// Anchored torso and collar
//
// The torso remains static for the entire animation. It is the
// structural anchor that keeps the garment recognizable.
// ------------------------------------------------------------
const torsoSpecs = [];
for (let row = -3; row <= 7; row++) {
for (let column = -5; column <= 5; column++) {
// A stepped U-shaped neck opening.
if (row === 7 && Math.abs(column) < 2) continue;
if (row === 6 && column === 0) continue;
let color;
if (
(row === 7 && Math.abs(column) === 2) ||
(row === 6 && Math.abs(column) === 1)
) {
color = collarColor;
} else if (row === -3 && Math.abs(column) === 5) {
color = seamColor;
} else {
color = shirtPalette[
(column * 3 + row * 7 + 14) % shirtPalette.length
];
}
torsoSpecs.push({
x: column * STEP,
y: row * STEP,
z: 0,
color,
});
}
}
addVoxels(shirt, torsoSpecs);
// Recessed dark voxels visible through the neck opening.
addVoxels(shirt, [
{
x: 0,
y: 6 * STEP,
z: -0.18,
sx: 0.9,
sy: 0.9,
sz: 0.8,
color: 0x07333d,
},
]);
// ------------------------------------------------------------
// Shoulder sections: static structural anchors
// ------------------------------------------------------------
const leftShoulder = new THREE.Group();
const rightShoulder = new THREE.Group();
shirt.add(leftShoulder, rightShoulder);
function buildShoulder(group, side) {
group.position.set(side * 0.95, 0.94, 0);
const specs = [];
const upperY = [0.16, 0.06, -0.04, -0.14];
for (let k = 0; k < 4; k++) {
const x = side * (0.05 + STEP * k);
specs.push({
x,
y: upperY[k],
color:
k === 0
? seamColor
: shirtPalette[(k * 3 + 5) % shirtPalette.length],
});
// Lower edge creates a tapered short-sleeve silhouette.
if (k < 2) {
specs.push({
x,
y: upperY[k] - 0.15,
color: shirtPalette[(k * 2 + 2) % shirtPalette.length],
});
}
}
addVoxels(group, specs);
}
buildShoulder(leftShoulder, -1);
buildShoulder(rightShoulder, 1);
// ------------------------------------------------------------
// Forearm sections: hinged at the elbows
//
// Each forearm rotates around a vertical local Y axis. The
// shoulder section remains in place so the garment never loses
// its recognizable T-shirt outline.
// ------------------------------------------------------------
const leftForearm = new THREE.Group();
const rightForearm = new THREE.Group();
shirt.add(leftForearm, rightForearm);
function buildForearm(group, side) {
group.position.set(side * 1.61, 0.845, 0);
const specs = [];
const cuffY = [-0.04, -0.10, -0.16, -0.22, -0.28];
for (let k = 0; k < 5; k++) {
const x = side * (0.05 + STEP * k);
specs.push({
x,
y: cuffY[k],
color:
k === 4
? cuffColor
: shirtPalette[(k * 4 + 1) % shirtPalette.length],
});
if (k < 3) {
specs.push({
x,
y: cuffY[k] - 0.14,
color: shirtPalette[(k * 3 + 6) % shirtPalette.length],
});
}
}
addVoxels(group, specs);
}
buildForearm(leftForearm, -1);
buildForearm(rightForearm, 1);
// ------------------------------------------------------------
// Side gussets: hinged around vertical seams
//
// These panels fold behind the torso, reducing the projected
// width without collapsing the central chest.
// ------------------------------------------------------------
const leftSide = new THREE.Group();
const rightSide = new THREE.Group();
shirt.add(leftSide, rightSide);
function buildSide(group, side) {
group.position.set(side * 0.95, 0, 0);
const specs = [];
for (let row = -2; row <= 5; row++) {
for (let k = 1; k <= 2; k++) {
specs.push({
x: side * STEP * k,
y: row * STEP,
color:
k === 1
? seamColor
: shirtPalette[(row * 2 + k * 4) % shirtPalette.length],
});
}
}
addVoxels(group, specs);
}
buildSide(leftSide, -1);
buildSide(rightSide, 1);
// ------------------------------------------------------------
// Lower hem: hinged along a horizontal waist seam
//
// Rotating this lower band upward and toward the camera makes
// the hem visibly rise while leaving the upper torso intact.
// ------------------------------------------------------------
const hem = new THREE.Group();
hem.position.set(0, -3 * STEP, 0.02);
shirt.add(hem);
const hemSpecs = [];
for (let row = -5; row <= -4; row++) {
const minColumn = row === -5 ? -4 : -5;
const maxColumn = row === -5 ? 4 : 5;
for (let column = minColumn; column <= maxColumn; column++) {
hemSpecs.push({
x: column * STEP,
y: (row + 3) * STEP,
color:
row === -5
? hemColor
: shirtPalette[(column * 3 + 9) % shirtPalette.length],
});
}
}
addVoxels(hem, hemSpecs);
// ------------------------------------------------------------
// Amber hinge stitches
// ------------------------------------------------------------
const hingeMaterial = new THREE.MeshStandardMaterial({
color: 0xf6b85d,
roughness: 0.45,
metalness: 0.2,
emissive: 0x4b2807,
emissiveIntensity: 0.3,
});
const hingeGeometry = new THREE.BoxGeometry(0.05, 0.05, 0.05);
const hingeSpecs = [];
// Elbow hinges.
for (const side of [-1, 1]) {
for (let k = -1; k <= 1; k++) {
hingeSpecs.push({
x: side * 1.61,
y: 0.845 + k * 0.065,
z: 0.108,
});
}
}
// Vertical side seams.
for (const side of [-1, 1]) {
for (let k = 0; k < 6; k++) {
hingeSpecs.push({
x: side * 0.95,
y: -0.32 + k * 0.24,
z: 0.108,
});
}
}
// Horizontal hem seam.
for (let k = -4; k <= 4; k++) {
hingeSpecs.push({
x: k * 0.19,
y: -3 * STEP,
z: 0.108,
});
}
const hingeMesh = new THREE.InstancedMesh(
hingeGeometry,
hingeMaterial,
hingeSpecs.length
);
hingeSpecs.forEach((spec, index) => {
dummy.position.set(spec.x, spec.y, spec.z);
dummy.scale.set(1, 1, 1);
dummy.updateMatrix();
hingeMesh.setMatrixAt(index, dummy.matrix);
});
hingeMesh.instanceMatrix.needsUpdate = true;
hingeMesh.castShadow = true;
shirt.add(hingeMesh);
// ------------------------------------------------------------
// Floor and presentation grid
// ------------------------------------------------------------
const floor = new THREE.Mesh(
new THREE.PlaneGeometry(30, 30),
new THREE.MeshStandardMaterial({
color: 0x091522,
roughness: 0.94,
metalness: 0,
})
);
floor.rotation.x = -Math.PI / 2;
floor.position.y = -1.34;
floor.receiveShadow = true;
scene.add(floor);
const platform = new THREE.Mesh(
new THREE.BoxGeometry(4.6, 0.12, 3.1),
new THREE.MeshStandardMaterial({
color: 0x102a39,
roughness: 0.72,
metalness: 0.08,
})
);
platform.position.set(0, -1.25, 0);
platform.receiveShadow = true;
platform.castShadow = true;
scene.add(platform);
const grid = new THREE.GridHelper(16, 32, 0x2b7180, 0x173746);
grid.position.y = -1.175;
grid.material.transparent = true;
grid.material.opacity = 0.28;
scene.add(grid);
// ------------------------------------------------------------
// Ordered animation
// ------------------------------------------------------------
const DURATION = 7;
const timelineInput = document.querySelector("#timeline");
const playButton = document.querySelector("#play");
const replayButton = document.querySelector("#replay");
const resetViewButton = document.querySelector("#view");
const status = document.querySelector("#status");
const timecode = document.querySelector("#timecode");
const stepElements = [...document.querySelectorAll(".step")];
let timeline = 0;
let playing = !window.matchMedia("(prefers-reduced-motion: reduce)").matches;
let currentStep = -1;
const clamp01 = (value) => THREE.MathUtils.clamp(value, 0, 1);
function smoothstep(value) {
const t = clamp01(value);
return t * t * (3 - 2 * t);
}
function phase(time, start, end) {
return smoothstep((time - start) / (end - start));
}
function applyTimeline(time) {
// Phase 1: forearms fold inward across the chest.
const sleeveProgress = phase(time, 0.25, 2.25);
// Phase 2: side gussets fold rearward to narrow the silhouette.
const sideProgress = phase(time, 2.55, 4.25);
// Phase 3: lower hem rotates upward around its horizontal hinge.
const hemProgress = phase(time, 4.55, 6.25);
const sleeveAngle = THREE.MathUtils.degToRad(164) * sleeveProgress;
const sideAngle = THREE.MathUtils.degToRad(70) * sideProgress;
const hemAngle = THREE.MathUtils.degToRad(95) * hemProgress;
rightForearm.rotation.y = -sleeveAngle;
leftForearm.rotation.y = sleeveAngle;
rightSide.rotation.y = sideAngle;
leftSide.rotation.y = -sideAngle;
hem.rotation.x = -hemAngle;
updateHud(time);
}
function updateHud(time) {
const percent = (time / DURATION) * 100;
timelineInput.style.setProperty("--progress", `${percent}%`);
timelineInput.value = String(Math.round(time * 1000));
timecode.textContent = formatTime(DURATION - time);
let step;
let label;
if (time < 2.4) {
step = 0;
label = "01 / Sleeves folding inward";
} else if (time < 4.45) {
step = 1;
label = "02 / Side panels narrowing";
} else if (time < 6.35) {
step = 2;
label = "03 / Hem lifting";
} else {
step = 3;
label = "Fold complete / structure retained";
}
status.textContent = label;
if (step !== currentStep) {
currentStep = step;
stepElements.forEach((element, index) => {
element.classList.toggle("active", index === step);
element.classList.toggle("complete", index < step);
});
}
}
function formatTime(seconds) {
const wholeSeconds = Math.max(0, Math.ceil(seconds));
const minutes = Math.floor(wholeSeconds / 60);
const remainder = wholeSeconds % 60;
return `${String(minutes).padStart(2, "0")}:${String(remainder).padStart(2, "0")}`;
}
function setPlaying(value) {
playing = value;
playButton.textContent = playing ? "Pause" : "Play";
playButton.setAttribute(
"aria-label",
playing ? "Pause animation" : "Play animation"
);
}
playButton.addEventListener("click", () => {
if (!playing && timeline >= DURATION) {
timeline = 0;
applyTimeline(timeline);
}
setPlaying(!playing);
});
replayButton.addEventListener("click", () => {
timeline = 0;
applyTimeline(timeline);
setPlaying(true);
});
resetViewButton.addEventListener("click", resetCamera);
timelineInput.addEventListener("input", () => {
setPlaying(false);
timeline = Number(timelineInput.value) / 1000;
applyTimeline(timeline);
});
window.addEventListener("keydown", (event) => {
if (
event.target instanceof HTMLInputElement ||
event.target instanceof HTMLButtonElement
) {
return;
}
if (event.code === "Space") {
event.preventDefault();
setPlaying(!playing);
}
if (event.key.toLowerCase() === "r") {
timeline = 0;
applyTimeline(timeline);
setPlaying(true);
}
});
// ------------------------------------------------------------
// Direct camera controls — no external control addons
// ------------------------------------------------------------
let dragging = false;
let previousX = 0;
let previousY = 0;
canvas.addEventListener("pointerdown", (event) => {
dragging = true;
cameraTouched = true;
previousX = event.clientX;
previousY = event.clientY;
canvas.setPointerCapture(event.pointerId);
canvas.classList.add("dragging");
});
canvas.addEventListener("pointermove", (event) => {
if (!dragging) return;
const deltaX = event.clientX - previousX;
const deltaY = event.clientY - previousY;
previousX = event.clientX;
previousY = event.clientY;
spherical.theta -= deltaX * 0.006;
spherical.phi = THREE.MathUtils.clamp(
spherical.phi - deltaY * 0.005,
0.45,
1.48
);
updateCamera();
});
function endDrag(event) {
dragging = false;
canvas.classList.remove("dragging");
if (canvas.hasPointerCapture(event.pointerId)) {
canvas.releasePointerCapture(event.pointerId);
}
}
canvas.addEventListener("pointerup", endDrag);
canvas.addEventListener("pointercancel", endDrag);
canvas.addEventListener(
"wheel",
(event) => {
event.preventDefault();
cameraTouched = true;
spherical.radius = THREE.MathUtils.clamp(
spherical.radius * Math.exp(event.deltaY * 0.001),
5.2,
15
);
updateCamera();
},
{ passive: false }
);
// ------------------------------------------------------------
// Resize and render loop
// ------------------------------------------------------------
function resize() {
const width = window.innerWidth;
const height = window.innerHeight;
camera.aspect = width / height;
camera.updateProjectionMatrix();
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(width, height);
if (!cameraTouched) {
spherical.radius = defaultCameraRadius();
updateCamera();
}
}
window.addEventListener("resize", resize);
const clock = new THREE.Clock();
function animate() {
requestAnimationFrame(animate);
const delta = Math.min(clock.getDelta(), 0.05);
if (playing) {
timeline = Math.min(DURATION, timeline + delta);
if (timeline >= DURATION) {
setPlaying(false);
}
}
applyTimeline(timeline);
renderer.render(scene, camera);
}
setPlaying(playing);
applyTimeline(0);
animate();
</script>
</body>
</html>
```