Prompt specification
Build or customize the Three.js 3D scene "Sofa layout transformation" 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: Sofa layout transformation
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/sofa-layout-transformation-x0202
Scene Description Prompt:
Please create one self-contained HTML file with Three.js from cdnjs showing modular sofas. Animate the same modules changing from a straight row to an L-shape; move each separately along visible paths and preserve its identity throughout the rearrangement. Voxel style.
Working Three.js Starter (sofa-layout-transformation-x0202):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Voxel Modular — Sofa Rearrangement</title>
<style>
:root {
--ink: #20242a;
--muted: #697078;
--panel: rgba(249, 247, 242, 0.88);
--border: rgba(32, 36, 42, 0.13);
--shadow: 0 14px 36px rgba(30, 34, 40, 0.12);
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #ebe7df;
color: var(--ink);
font-family:
Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
#app,
#app canvas {
width: 100%;
height: 100%;
display: block;
}
#app canvas {
touch-action: none;
cursor: grab;
}
#app canvas:active {
cursor: grabbing;
}
.hud {
position: fixed;
inset: 0;
z-index: 10;
pointer-events: none;
}
.panel {
background: var(--panel);
border: 1px solid var(--border);
box-shadow: var(--shadow);
backdrop-filter: blur(14px);
-webkit-backdrop-filter: blur(14px);
}
.brand {
position: absolute;
top: 22px;
left: 22px;
width: min(350px, calc(100vw - 44px));
padding: 18px 20px;
border-radius: 18px;
}
.eyebrow {
display: flex;
align-items: center;
gap: 8px;
margin-bottom: 8px;
color: var(--muted);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.17em;
text-transform: uppercase;
}
.eyebrow::before {
content: "";
width: 8px;
height: 8px;
border-radius: 2px;
background: #e07a5f;
box-shadow: 12px 0 0 #2a9d8f, 24px 0 0 #e9c46a;
margin-right: 24px;
}
h1 {
margin: 0;
font-size: clamp(22px, 3vw, 31px);
line-height: 1;
letter-spacing: -0.045em;
}
.brand p {
margin: 10px 0 0;
color: var(--muted);
font-size: 13px;
line-height: 1.5;
}
.status {
position: absolute;
top: 22px;
right: 22px;
width: 285px;
padding: 16px;
border-radius: 18px;
}
.status-heading {
display: flex;
justify-content: space-between;
align-items: center;
gap: 10px;
margin-bottom: 13px;
}
.status-title {
color: var(--muted);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.14em;
text-transform: uppercase;
}
#arrangementState {
font-size: 10px;
font-weight: 900;
letter-spacing: 0.08em;
color: #252a31;
}
.module-list {
display: grid;
gap: 9px;
}
.module-row {
--accent: #888;
display: grid;
grid-template-columns: 26px 1fr auto;
align-items: center;
gap: 9px;
min-height: 25px;
}
.module-number {
display: grid;
place-items: center;
width: 25px;
height: 22px;
border-radius: 6px;
color: white;
background: var(--accent);
font-size: 10px;
font-weight: 900;
}
.module-progress {
position: relative;
height: 5px;
overflow: hidden;
border-radius: 99px;
background: rgba(30, 35, 40, 0.1);
}
.module-progress > span {
position: absolute;
inset: 0;
transform: scaleX(0);
transform-origin: left center;
background: var(--accent);
border-radius: inherit;
}
.module-state {
min-width: 47px;
color: var(--muted);
font-size: 9px;
font-weight: 800;
letter-spacing: 0.07em;
text-align: right;
}
.hint {
position: absolute;
left: 24px;
bottom: 104px;
padding: 8px 11px;
border-radius: 9px;
color: rgba(32, 36, 42, 0.66);
background: rgba(249, 247, 242, 0.7);
border: 1px solid rgba(32, 36, 42, 0.08);
font-size: 10px;
font-weight: 700;
letter-spacing: 0.04em;
}
.controls {
position: absolute;
left: 50%;
bottom: 20px;
transform: translateX(-50%);
display: flex;
align-items: center;
gap: 10px;
width: max-content;
max-width: calc(100vw - 36px);
padding: 10px;
border-radius: 16px;
pointer-events: auto;
}
button {
min-height: 38px;
border: 0;
border-radius: 10px;
padding: 0 13px;
color: #f8f7f3;
background: #252a31;
cursor: pointer;
font: inherit;
font-size: 11px;
font-weight: 800;
letter-spacing: 0.03em;
transition:
transform 120ms ease,
background 120ms ease;
}
button:hover {
background: #101317;
transform: translateY(-1px);
}
button:active {
transform: translateY(0);
}
button.secondary {
color: var(--ink);
background: rgba(32, 36, 42, 0.08);
}
button.secondary:hover {
background: rgba(32, 36, 42, 0.13);
}
#playButton {
min-width: 104px;
}
.divider {
width: 1px;
height: 28px;
background: var(--border);
}
.speed-control {
display: flex;
align-items: center;
gap: 8px;
color: var(--muted);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.06em;
text-transform: uppercase;
}
input[type="range"] {
width: 86px;
accent-color: #252a31;
cursor: pointer;
}
.route-toggle {
display: flex;
align-items: center;
gap: 7px;
color: var(--muted);
cursor: pointer;
font-size: 10px;
font-weight: 800;
letter-spacing: 0.06em;
text-transform: uppercase;
white-space: nowrap;
}
.route-toggle input {
width: 15px;
height: 15px;
accent-color: #252a31;
}
#speedValue {
width: 31px;
color: var(--ink);
text-align: right;
}
#loading {
position: fixed;
inset: 0;
z-index: 100;
display: grid;
place-items: center;
background: #ebe7df;
color: #343a41;
font-size: 12px;
font-weight: 800;
letter-spacing: 0.12em;
text-transform: uppercase;
}
@media (max-width: 760px) {
.brand {
top: 12px;
left: 12px;
width: calc(100vw - 24px);
padding: 14px 16px;
}
.brand p {
display: none;
}
.status {
top: 112px;
right: 12px;
width: 210px;
padding: 12px;
}
.module-state {
display: none;
}
.module-row {
grid-template-columns: 26px 1fr;
}
.controls {
bottom: 10px;
width: calc(100vw - 20px);
flex-wrap: wrap;
justify-content: center;
border-radius: 14px;
}
.hint {
display: none;
}
.divider {
display: none;
}
}
@media (max-width: 430px) {
.status {
top: 104px;
width: 190px;
}
#playButton {
min-width: 88px;
}
button {
padding: 0 10px;
}
.speed-control > span:first-child {
display: none;
}
input[type="range"] {
width: 68px;
}
}
</style>
</head>
<body>
<div id="app"></div>
<div id="loading">Loading modular scene…</div>
<div class="hud">
<header class="brand panel">
<div class="eyebrow">Voxel furniture study</div>
<h1>Modular / 05</h1>
<p>
Five individual modules travel along visible routes and retain their
color, number, and identity while changing from a row into an L-shape.
</p>
</header>
<aside class="status panel">
<div class="status-heading">
<span class="status-title">Arrangement</span>
<span id="arrangementState" aria-live="polite">STRAIGHT ROW</span>
</div>
<div id="moduleList" class="module-list"></div>
</aside>
<div class="hint">Drag to orbit · Shift-drag to pan · Scroll to zoom</div>
<footer class="controls panel">
<button id="playButton" type="button">
<span id="playIcon">❚❚</span>
<span id="playText">Pause</span>
</button>
<button id="resetButton" class="secondary" type="button">Reset</button>
<div class="divider"></div>
<label class="speed-control">
<span>Speed</span>
<input
id="speedInput"
type="range"
min="0.5"
max="2"
step="0.1"
value="1"
/>
<span id="speedValue">1.0×</span>
</label>
<div class="divider"></div>
<label class="route-toggle">
<input id="routeToggle" type="checkbox" checked />
Routes
</label>
<button id="cameraButton" class="secondary" type="button">
Reset camera
</button>
</footer>
</div>
<script type="module">
import * as THREE from "https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
const app = document.getElementById("app");
const loading = document.getElementById("loading");
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xebe7df);
scene.fog = new THREE.Fog(0xebe7df, 19, 35);
const camera = new THREE.PerspectiveCamera(
42,
innerWidth / innerHeight,
0.1,
100
);
let renderer;
try {
renderer = new THREE.WebGLRenderer({
antialias: true,
powerPreference: "high-performance"
});
} catch (error) {
loading.textContent = "WebGL could not be initialized.";
throw error;
}
renderer.setPixelRatio(Math.min(devicePixelRatio, 2));
renderer.setSize(innerWidth, innerHeight);
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.03;
renderer.domElement.setAttribute(
"aria-label",
"Interactive 3D animation of five modular voxel sofas"
);
app.appendChild(renderer.domElement);
// ---------------------------------------------------------------------
// Lighting and stage
// ---------------------------------------------------------------------
const hemisphere = new THREE.HemisphereLight(0xffffff, 0x827b70, 2.15);
scene.add(hemisphere);
const keyLight = new THREE.DirectionalLight(0xfff4df, 3.1);
keyLight.position.set(7, 12, 8);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(2048, 2048);
keyLight.shadow.camera.left = -11;
keyLight.shadow.camera.right = 11;
keyLight.shadow.camera.top = 11;
keyLight.shadow.camera.bottom = -11;
keyLight.shadow.camera.near = 1;
keyLight.shadow.camera.far = 30;
keyLight.shadow.bias = -0.0005;
scene.add(keyLight);
const fillLight = new THREE.DirectionalLight(0xdce9ff, 1.15);
fillLight.position.set(-8, 6, -7);
scene.add(fillLight);
const floorMaterial = new THREE.MeshStandardMaterial({
color: 0xe5dfd4,
roughness: 0.96,
metalness: 0
});
const floor = new THREE.Mesh(
new THREE.BoxGeometry(18, 0.36, 18),
floorMaterial
);
floor.position.y = -0.18;
floor.receiveShadow = true;
scene.add(floor);
const grid = new THREE.GridHelper(18, 18, 0x8c9298, 0xb8b4ac);
grid.position.y = 0.012;
grid.material.transparent = true;
grid.material.opacity = 0.27;
grid.material.depthWrite = false;
scene.add(grid);
// A subtle voxel-like border around the stage.
const borderMaterial = new THREE.MeshStandardMaterial({
color: 0xcfc8bc,
roughness: 0.9
});
const borderGeometry = new THREE.BoxGeometry(18.4, 0.12, 0.16);
for (const z of [-9, 9]) {
const border = new THREE.Mesh(borderGeometry, borderMaterial);
border.position.set(0, -0.04, z);
border.receiveShadow = true;
scene.add(border);
}
const sideBorderGeometry = new THREE.BoxGeometry(0.16, 0.12, 18.4);
for (const x of [-9, 9]) {
const border = new THREE.Mesh(sideBorderGeometry, borderMaterial);
border.position.set(x, -0.04, 0);
border.receiveShadow = true;
scene.add(border);
}
// ---------------------------------------------------------------------
// Module definitions. The same objects are reused for the entire move.
// ---------------------------------------------------------------------
const moduleConfigs = [
{
id: "01",
color: "#e07a5f",
start: [-4.0, 0, 0],
end: [-1.85, 0, 0],
endRotation: 0,
delay: 0.0,
path: [
[-4.0, 0, 0],
[-3.65, 0, -0.65],
[-2.85, 0, -1.15],
[-2.20, 0, -0.45],
[-1.85, 0, 0]
]
},
{
id: "02",
color: "#2a9d8f",
start: [-2.0, 0, 0],
end: [0, 0, 0],
endRotation: 0,
delay: 0.6,
path: [
[-2.0, 0, 0],
[-1.55, 0, 0.85],
[-0.75, 0, 1.10],
[-0.15, 0, 0.45],
[0, 0, 0]
]
},
{
id: "03",
color: "#e9c46a",
start: [0, 0, 0],
end: [1.85, 0, 0],
endRotation: 0,
delay: 1.2,
path: [
[0, 0, 0],
[0.50, 0, -0.85],
[1.25, 0, -1.00],
[1.70, 0, -0.40],
[1.85, 0, 0]
]
},
{
id: "04",
color: "#8d6e9f",
start: [2.0, 0, 0],
end: [1.85, 0, 1.42],
endRotation: -Math.PI / 2,
delay: 0.3,
path: [
[2.0, 0, 0],
[2.55, 0, 0.35],
[2.55, 0, 0.95],
[2.15, 0, 1.42],
[1.85, 0, 1.42]
]
},
{
id: "05",
color: "#4d83c4",
start: [4.0, 0, 0],
end: [1.85, 0, 3.27],
endRotation: -Math.PI / 2,
delay: 1.5,
path: [
[4.0, 0, 0],
[4.55, 0, 0.75],
[4.05, 0, 2.00],
[3.20, 0, 2.80],
[2.45, 0, 3.20],
[1.85, 0, 3.27]
]
}
];
const moduleList = document.getElementById("moduleList");
moduleList.innerHTML = moduleConfigs
.map(
(config) => `
<div class="module-row" style="--accent:${config.color}">
<span class="module-number">${config.id}</span>
<div class="module-progress"><span></span></div>
<span class="module-state">QUEUED</span>
</div>
`
)
.join("");
const moduleRows = [...moduleList.querySelectorAll(".module-row")];
const routeGroup = new THREE.Group();
scene.add(routeGroup);
const modules = [];
function addBox(parent, width, height, depth, x, y, z, material) {
const mesh = new THREE.Mesh(
new THREE.BoxGeometry(width, height, depth),
material
);
mesh.position.set(x, y, z);
mesh.castShadow = true;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
function createIdentityLabel(id, color) {
const canvas = document.createElement("canvas");
canvas.width = 256;
canvas.height = 128;
const context = canvas.getContext("2d");
context.clearRect(0, 0, 256, 128);
context.fillStyle = "rgba(31, 35, 40, 0.94)";
context.fillRect(8, 12, 240, 104);
context.fillStyle = color;
context.fillRect(8, 12, 15, 104);
context.strokeStyle = "rgba(255,255,255,0.2)";
context.lineWidth = 3;
context.strokeRect(9.5, 13.5, 237, 101);
context.fillStyle = "#ffffff";
context.font = "900 62px system-ui, sans-serif";
context.textAlign = "center";
context.textBaseline = "middle";
context.fillText(id, 138, 65);
const texture = new THREE.CanvasTexture(canvas);
texture.colorSpace = THREE.SRGBColorSpace;
texture.minFilter = THREE.LinearFilter;
const material = new THREE.SpriteMaterial({
map: texture,
transparent: true,
depthTest: false,
depthWrite: false
});
const sprite = new THREE.Sprite(material);
sprite.position.set(0, 1.62, 0);
sprite.scale.set(0.76, 0.38, 1);
sprite.renderOrder = 20;
return sprite;
}
function createVoxelSofa(config) {
const group = new THREE.Group();
const baseColor = new THREE.Color(config.color);
const darkColor = baseColor.clone().offsetHSL(0, 0.02, -0.17);
const lightColor = baseColor.clone().offsetHSL(0, -0.03, 0.08);
const fabric = new THREE.MeshStandardMaterial({
color: baseColor,
roughness: 0.92,
metalness: 0
});
const lightFabric = new THREE.MeshStandardMaterial({
color: lightColor,
roughness: 0.92,
metalness: 0
});
const darkFabric = new THREE.MeshStandardMaterial({
color: darkColor,
roughness: 0.95,
metalness: 0
});
const accent = new THREE.MeshStandardMaterial({
color: baseColor,
emissive: baseColor,
emissiveIntensity: 0.09,
roughness: 0.72,
metalness: 0
});
const feetMaterial = new THREE.MeshStandardMaterial({
color: 0x4b4036,
roughness: 0.82,
metalness: 0
});
// Four block feet.
for (const x of [-0.66, 0.66]) {
for (const z of [-0.40, 0.40]) {
addBox(group, 0.16, 0.20, 0.16, x, 0.10, z, feetMaterial);
}
}
// Main base.
addBox(group, 1.68, 0.30, 1.12, 0, 0.35, 0, darkFabric);
// Two separate seat cushions.
addBox(group, 0.73, 0.23, 0.90, -0.39, 0.615, 0.04, fabric);
addBox(group, 0.73, 0.23, 0.90, 0.39, 0.615, 0.04, fabric);
// Voxel back and two back cushions.
addBox(group, 1.68, 0.57, 0.20, 0, 0.95, -0.46, darkFabric);
addBox(group, 0.73, 0.36, 0.15, -0.39, 1.00, -0.30, lightFabric);
addBox(group, 0.73, 0.36, 0.15, 0.39, 1.00, -0.30, lightFabric);
// Narrow side arms.
addBox(group, 0.16, 0.51, 1.12, -0.84, 0.78, 0, fabric);
addBox(group, 0.16, 0.51, 1.12, 0.84, 0.78, 0, fabric);
// Accent top caps.
addBox(group, 0.17, 0.065, 1.12, -0.84, 1.065, 0, accent);
addBox(group, 0.17, 0.065, 1.12, 0.84, 1.065, 0, accent);
// Front identity strip.
addBox(group, 0.46, 0.075, 0.028, 0, 0.39, 0.574, accent);
const label = createIdentityLabel(config.id, config.color);
group.add(label);
group.position.set(...config.start);
scene.add(group);
return group;
}
function createTargetOutline(config) {
const width = 1.84;
const depth = 1.12;
const y = 0.035;
const points = [
new THREE.Vector3(-width / 2, y, -depth / 2),
new THREE.Vector3(width / 2, y, -depth / 2),
new THREE.Vector3(width / 2, y, depth / 2),
new THREE.Vector3(-width / 2, y, depth / 2)
];
const quaternion = new THREE.Quaternion().setFromAxisAngle(
new THREE.Vector3(0, 1, 0),
config.endRotation
);
const center = new THREE.Vector3(...config.end);
points.forEach((point) => {
point.applyQuaternion(quaternion).add(center);
});
const geometry = new THREE.BufferGeometry().setFromPoints(points);
const material = new THREE.LineBasicMaterial({
color: config.color,
transparent: true,
opacity: 0.38,
depthWrite: false
});
const outline = new THREE.LineLoop(geometry, material);
outline.renderOrder = 2;
routeGroup.add(outline);
}
function createRoute(config) {
const points = config.path.map(
(point) => new THREE.Vector3(point[0], 0.045, point[2])
);
const curve = new THREE.CatmullRomCurve3(
points,
false,
"centripetal"
);
const lineGeometry = new THREE.BufferGeometry().setFromPoints(
curve.getPoints(140)
);
const lineMaterial = new THREE.LineDashedMaterial({
color: config.color,
dashSize: 0.20,
gapSize: 0.12,
transparent: true,
opacity: 0.9,
depthWrite: false
});
const line = new THREE.Line(lineGeometry, lineMaterial);
line.computeLineDistances();
line.renderOrder = 3;
routeGroup.add(line);
const markerMaterial = new THREE.MeshBasicMaterial({
color: config.color,
transparent: true,
opacity: 0.72,
depthWrite: false
});
// Block-built directional arrows.
const arrowTimes = [0.13, 0.32, 0.52, 0.72, 0.88];
for (const time of arrowTimes) {
const arrow = new THREE.Group();
const tangent = curve.getTangentAt(time).normalize();
addBox(
arrow,
0.075,
0.026,
0.22,
0,
0,
-0.05,
markerMaterial
);
const leftWing = addBox(
arrow,
0.07,
0.026,
0.17,
-0.055,
0,
0.12,
markerMaterial
);
leftWing.rotation.y = 0.65;
const rightWing = addBox(
arrow,
0.07,
0.026,
0.17,
0.055,
0,
0.12,
markerMaterial
);
rightWing.rotation.y = -0.65;
arrow.position.copy(curve.getPointAt(time));
arrow.rotation.y = Math.atan2(tangent.x, tangent.z);
arrow.renderOrder = 4;
routeGroup.add(arrow);
}
createTargetOutline(config);
return {
curve,
line,
lineMaterial,
markerMaterial
};
}
moduleConfigs.forEach((config, index) => {
const group = createVoxelSofa(config);
const route = createRoute(config);
modules.push({
group,
route,
config,
index
});
});
// ---------------------------------------------------------------------
// Custom orbit, pan, and zoom controls. No external controls are used.
// ---------------------------------------------------------------------
const cameraState = {
theta: 0.73,
phi: 1.02,
radius: 15.2,
target: new THREE.Vector3(0, 0.55, 0.65)
};
function updateCamera() {
const sinPhi = Math.sin(cameraState.phi);
camera.position.set(
cameraState.target.x +
cameraState.radius * sinPhi * Math.sin(cameraState.theta),
cameraState.target.y + cameraState.radius * Math.cos(cameraState.phi),
cameraState.target.z +
cameraState.radius * sinPhi * Math.cos(cameraState.theta)
);
camera.lookAt(cameraState.target);
}
function resetCamera() {
cameraState.theta = 0.73;
cameraState.phi = 1.02;
cameraState.radius = 15.2;
cameraState.target.set(0, 0.55, 0.65);
updateCamera();
}
const pointers = new Map();
const canvas = renderer.domElement;
canvas.addEventListener("contextmenu", (event) => event.preventDefault());
canvas.addEventListener("pointerdown", (event) => {
canvas.setPointerCapture(event.pointerId);
pointers.set(event.pointerId, {
x: event.clientX,
y: event.clientY
});
});
canvas.addEventListener("pointermove", (event) => {
const previous = pointers.get(event.pointerId);
if (!previous) return;
const dx = event.clientX - previous.x;
const dy = event.clientY - previous.y;
previous.x = event.clientX;
previous.y = event.clientY;
if (pointers.size === 1) {
const panMode = event.shiftKey || event.buttons === 2;
if (panMode) {
const right = new THREE.Vector3(
Math.cos(cameraState.theta),
0,
-Math.sin(cameraState.theta)
);
const forward = new THREE.Vector3(
-Math.sin(cameraState.theta),
0,
-Math.cos(cameraState.theta)
);
const panScale = cameraState.radius * 0.0012;
cameraState.target.addScaledVector(right, -dx * panScale);
cameraState.target.addScaledVector(forward, -dy * panScale);
} else {
cameraState.theta -= dx * 0.006;
cameraState.phi = THREE.MathUtils.clamp(
cameraState.phi - dy * 0.005,
0.42,
1.38
);
}
}
updateCamera();
});
function releasePointer(event) {
pointers.delete(event.pointerId);
}
canvas.addEventListener("pointerup", releasePointer);
canvas.addEventListener("pointercancel", releasePointer);
canvas.addEventListener(
"wheel",
(event) => {
event.preventDefault();
cameraState.radius = THREE.MathUtils.clamp(
cameraState.radius * Math.exp(event.deltaY * 0.001),
8,
25
);
updateCamera();
},
{ passive: false }
);
canvas.addEventListener("dblclick", resetCamera);
// ---------------------------------------------------------------------
// Animation and interface
// ---------------------------------------------------------------------
const DURATION = 7.8;
const MAX_DELAY = Math.max(...moduleConfigs.map((config) => config.delay));
const TOTAL_DURATION = DURATION + MAX_DELAY;
let elapsed = 0;
let playing = true;
let speed = 1;
const playButton = document.getElementById("playButton");
const playIcon = document.getElementById("playIcon");
const playText = document.getElementById("playText");
const resetButton = document.getElementById("resetButton");
const speedInput = document.getElementById("speedInput");
const speedValue = document.getElementById("speedValue");
const routeToggle = document.getElementById("routeToggle");
const cameraButton = document.getElementById("cameraButton");
const arrangementState = document.getElementById("arrangementState");
function updatePlayButton() {
if (elapsed >= TOTAL_DURATION) {
playIcon.textContent = "↻";
playText.textContent = "Replay";
} else if (playing) {
playIcon.textContent = "❚❚";
playText.textContent = "Pause";
} else {
playIcon.textContent = "▶";
playText.textContent = "Play";
}
}
function resetAnimation(shouldPlay = false) {
elapsed = 0;
playing = shouldPlay;
updatePlayButton();
}
playButton.addEventListener("click", () => {
if (elapsed >= TOTAL_DURATION) {
resetAnimation(true);
} else {
playing = !playing;
updatePlayButton();
}
});
resetButton.addEventListener("click", () => {
resetAnimation(false);
});
speedInput.addEventListener("input", () => {
speed = Number(speedInput.value);
speedValue.textContent = `${speed.toFixed(1)}×`;
});
routeToggle.addEventListener("change", () => {
routeGroup.visible = routeToggle.checked;
});
cameraButton.addEventListener("click", resetCamera);
window.addEventListener("keydown", (event) => {
if (
event.target instanceof HTMLInputElement ||
event.target instanceof HTMLButtonElement
) {
return;
}
if (event.code === "Space") {
event.preventDefault();
playButton.click();
}
if (event.key.toLowerCase() === "r") {
resetAnimation(false);
}
});
function updateModulePositions() {
modules.forEach((entry, index) => {
const { group, route, config } = entry;
const rawProgress = THREE.MathUtils.clamp(
(elapsed - config.delay) / DURATION,
0,
1
);
const easedProgress =
rawProgress * rawProgress * (3 - 2 * rawProgress);
const position = route.curve.getPointAt(easedProgress);
const lift = Math.sin(Math.PI * easedProgress) * 0.13;
group.position.set(position.x, position.y + lift, position.z);
group.rotation.y = THREE.MathUtils.lerp(
0,
config.endRotation,
easedProgress
);
const row = moduleRows[index];
const progressBar = row.querySelector(".module-progress > span");
const moduleState = row.querySelector(".module-state");
progressBar.style.transform = `scaleX(${rawProgress})`;
if (rawProgress <= 0) {
moduleState.textContent = "QUEUED";
} else if (rawProgress >= 1) {
moduleState.textContent = "PLACED";
} else {
moduleState.textContent = "MOVING";
}
route.markerMaterial.opacity =
0.62 + Math.sin(elapsed * 3 + index * 0.8) * 0.15;
});
if (elapsed <= 0) {
arrangementState.textContent = "STRAIGHT ROW";
} else if (elapsed >= TOTAL_DURATION) {
arrangementState.textContent = "L-SHAPE";
} else {
arrangementState.textContent = playing
? "REARRANGING"
: "PAUSED";
}
}
const clock = new THREE.Clock();
function animate() {
requestAnimationFrame(animate);
const delta = Math.min(clock.getDelta(), 0.05);
if (playing) {
elapsed += delta * speed;
if (elapsed >= TOTAL_DURATION) {
elapsed = TOTAL_DURATION;
playing = false;
updatePlayButton();
}
}
updateModulePositions();
renderer.render(scene, camera);
}
window.addEventListener("resize", () => {
camera.aspect = innerWidth / innerHeight;
camera.updateProjectionMatrix();
renderer.setPixelRatio(Math.min(devicePixelRatio, 2));
renderer.setSize(innerWidth, innerHeight);
});
resetCamera();
updatePlayButton();
updateModulePositions();
loading.remove();
animate();
</script>
</body>
</html>
```