Prompt specification
Build or customize the Three.js 3D scene "Open and Closed Hinged Box" 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: Open and Closed Hinged Box
Category: Art & Culture · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/open-and-closed-hinged-box-x3433
Scene Description Prompt:
Create a single self-contained HTML file using Three.js from cdnjs that shows verbs grounded in object actions and state changes. Show one box in closed and open states with a visible hinge axis. Make the interior appear only through actual lid rotation rather than a color change. Low-poly.
Working Three.js Starter (open-and-closed-hinged-box-x3433):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Hinged Box — Object State Study</title>
<style>
:root {
--ink: #172120;
--muted: #62706d;
--paper: #ece8df;
--panel: rgba(248, 246, 240, 0.88);
--line: rgba(23, 33, 32, 0.14);
--accent: #d65f3d;
--open: #26736d;
--closed: #bd5839;
--moving: #c28a25;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: var(--paper);
color: var(--ink);
font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
button,
input {
font: inherit;
}
#scene {
position: fixed;
inset: 0;
}
#scene canvas {
display: block;
width: 100%;
height: 100%;
touch-action: none;
cursor: grab;
outline: none;
}
.overlay {
position: fixed;
inset: 0;
pointer-events: none;
}
.header {
position: absolute;
top: 28px;
left: 30px;
max-width: 430px;
}
.eyebrow {
margin-bottom: 10px;
color: var(--muted);
font: 700 11px/1.2 ui-monospace, SFMono-Regular, Menlo, monospace;
letter-spacing: 0.16em;
text-transform: uppercase;
}
h1 {
margin: 0;
font-size: clamp(34px, 5vw, 68px);
font-weight: 750;
line-height: 0.9;
letter-spacing: -0.055em;
}
.subtitle {
max-width: 360px;
margin: 16px 0 0;
color: var(--muted);
font-size: 13px;
line-height: 1.55;
}
.state-card {
position: absolute;
top: 28px;
right: 30px;
width: 276px;
padding: 17px 18px;
border: 1px solid var(--line);
border-radius: 16px;
background: var(--panel);
box-shadow: 0 14px 45px rgba(45, 39, 29, 0.08);
backdrop-filter: blur(16px);
pointer-events: auto;
}
.state-heading {
display: flex;
align-items: center;
justify-content: space-between;
margin-bottom: 14px;
}
.state-heading span:first-child {
color: var(--muted);
font: 700 10px/1 ui-monospace, SFMono-Regular, Menlo, monospace;
letter-spacing: 0.14em;
}
.state-dot {
width: 9px;
height: 9px;
border-radius: 50%;
background: var(--moving);
box-shadow: 0 0 0 4px rgba(194, 138, 37, 0.13);
transition: background 180ms ease, box-shadow 180ms ease;
}
.state-name {
margin-bottom: 15px;
font-size: 29px;
font-weight: 760;
letter-spacing: -0.04em;
}
.metric {
display: flex;
align-items: baseline;
justify-content: space-between;
padding: 8px 0;
border-top: 1px solid var(--line);
color: var(--muted);
font: 700 10px/1.2 ui-monospace, SFMono-Regular, Menlo, monospace;
letter-spacing: 0.1em;
}
.metric strong {
color: var(--ink);
font-size: 12px;
letter-spacing: 0.04em;
}
.action-copy {
min-height: 38px;
margin: 13px 0 0;
color: var(--muted);
font-size: 12px;
line-height: 1.5;
}
.controls {
position: absolute;
left: 50%;
bottom: 42px;
width: min(680px, calc(100vw - 40px));
padding: 14px;
transform: translateX(-50%);
border: 1px solid var(--line);
border-radius: 18px;
background: var(--panel);
box-shadow: 0 18px 55px rgba(45, 39, 29, 0.1);
backdrop-filter: blur(18px);
pointer-events: auto;
}
.control-row {
display: grid;
grid-template-columns: 1fr 1fr 1.15fr;
gap: 9px;
}
.verb-button {
display: flex;
min-height: 50px;
align-items: center;
justify-content: space-between;
gap: 10px;
padding: 0 15px;
border: 1px solid rgba(23, 33, 32, 0.15);
border-radius: 11px;
background: rgba(255, 255, 255, 0.45);
color: var(--ink);
cursor: pointer;
transition:
transform 140ms ease,
background 140ms ease,
border-color 140ms ease;
}
.verb-button:hover {
transform: translateY(-1px);
border-color: rgba(23, 33, 32, 0.32);
background: rgba(255, 255, 255, 0.8);
}
.verb-button:active {
transform: translateY(0);
}
.verb-button.active {
border-color: var(--ink);
background: var(--ink);
color: #f7f5ef;
}
.verb-button span {
font: 800 12px/1 ui-monospace, SFMono-Regular, Menlo, monospace;
letter-spacing: 0.1em;
}
.verb-button small {
opacity: 0.6;
font: 700 10px/1 ui-monospace, SFMono-Regular, Menlo, monospace;
}
.range-row {
display: grid;
grid-template-columns: auto 1fr auto;
align-items: center;
gap: 13px;
margin-top: 13px;
color: var(--muted);
font: 700 10px/1 ui-monospace, SFMono-Regular, Menlo, monospace;
letter-spacing: 0.1em;
}
#angleSlider {
width: 100%;
height: 4px;
margin: 0;
appearance: none;
border-radius: 10px;
background: linear-gradient(
to right,
var(--accent) 0%,
var(--accent) var(--fill, 100%),
rgba(23, 33, 32, 0.12) var(--fill, 100%),
rgba(23, 33, 32, 0.12) 100%
);
cursor: ew-resize;
}
#angleSlider::-webkit-slider-thumb {
width: 16px;
height: 16px;
appearance: none;
border: 3px solid var(--panel);
border-radius: 50%;
background: var(--accent);
box-shadow: 0 0 0 1px rgba(23, 33, 32, 0.24);
}
#angleSlider::-moz-range-thumb {
width: 12px;
height: 12px;
border: 3px solid var(--panel);
border-radius: 50%;
background: var(--accent);
box-shadow: 0 0 0 1px rgba(23, 33, 32, 0.24);
}
#rangeAngle {
min-width: 37px;
color: var(--ink);
text-align: right;
}
.hint {
position: absolute;
right: 30px;
bottom: 24px;
color: rgba(23, 33, 32, 0.48);
font: 700 9px/1.4 ui-monospace, SFMono-Regular, Menlo, monospace;
letter-spacing: 0.11em;
text-align: right;
}
.reset-button {
margin-top: 9px;
padding: 6px 9px;
border: 1px solid var(--line);
border-radius: 7px;
background: transparent;
color: var(--muted);
font: 700 9px/1 ui-monospace, SFMono-Regular, Menlo, monospace;
letter-spacing: 0.08em;
cursor: pointer;
}
.reset-button:hover {
border-color: rgba(23, 33, 32, 0.35);
color: var(--ink);
}
@media (max-width: 760px) {
.header {
top: 20px;
left: 20px;
}
.subtitle {
display: none;
}
.state-card {
top: 96px;
right: 16px;
width: 218px;
padding: 14px;
}
.state-name {
font-size: 23px;
}
.controls {
bottom: 20px;
width: calc(100vw - 24px);
}
.control-row {
grid-template-columns: 1fr 1fr;
}
.verb-button:last-child {
grid-column: 1 / -1;
min-height: 40px;
}
.hint {
display: none;
}
}
</style>
</head>
<body>
<div id="scene"></div>
<div class="overlay">
<header class="header">
<div class="eyebrow">Object study 01 · one box · two states</div>
<h1>HINGED<br />BOX</h1>
<p class="subtitle">
A low-poly container whose visible interior is revealed only by physically
rotating the lid around its X-axis hinge.
</p>
</header>
<aside class="state-card" aria-live="polite">
<div class="state-heading">
<span>OBJECT STATE</span>
<i class="state-dot" id="stateDot"></i>
</div>
<div class="state-name" id="stateName">CLOSED</div>
<div class="metric">
<span>LID ANGLE</span>
<strong id="angleValue">0°</strong>
</div>
<div class="metric">
<span>HINGE</span>
<strong>VISIBLE X-AXIS</strong>
</div>
<p class="action-copy" id="actionCopy">
CLOSED — the lid rests on the box at 0°.
</p>
<button class="reset-button" id="resetView" type="button">
RESET VIEW
</button>
</aside>
<section class="controls" aria-label="Box state controls">
<div class="control-row">
<button class="verb-button active" id="closeButton" type="button">
<span>CLOSE</span>
<small>0°</small>
</button>
<button class="verb-button" id="openButton" type="button">
<span>OPEN</span>
<small>108°</small>
</button>
<button class="verb-button" id="playButton" type="button">
<span id="playLabel">PLAY STATES</span>
<small>DEMO</small>
</button>
</div>
<label class="range-row" for="angleSlider">
<span>LID ROTATION</span>
<input
id="angleSlider"
type="range"
min="0"
max="108"
step="1"
value="0"
aria-label="Lid rotation angle"
/>
<span id="rangeAngle">0°</span>
</label>
</section>
<div class="hint">
DRAG TO ORBIT · WHEEL TO ZOOM · CLICK LID TO TOGGLE<br />
KEYBOARD: O OPEN / C CLOSE / SPACE PLAY
</div>
</div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
const sceneHost = document.getElementById("scene");
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xece8df);
scene.fog = new THREE.Fog(0xece8df, 13, 27);
const camera = new THREE.PerspectiveCamera(
38,
window.innerWidth / window.innerHeight,
0.1,
60
);
const renderer = new THREE.WebGLRenderer({
antialias: true,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.08;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.domElement.setAttribute(
"aria-label",
"Interactive low-poly hinged box. Drag to orbit, scroll to zoom, or click the lid."
);
renderer.domElement.tabIndex = 0;
sceneHost.appendChild(renderer.domElement);
const hemiLight = new THREE.HemisphereLight(0xfff7e9, 0x697d7a, 2.35);
scene.add(hemiLight);
const keyLight = new THREE.DirectionalLight(0xfff0d7, 4.1);
keyLight.position.set(5.5, 8, 6.5);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(2048, 2048);
keyLight.shadow.camera.left = -6;
keyLight.shadow.camera.right = 6;
keyLight.shadow.camera.top = 7;
keyLight.shadow.camera.bottom = -5;
keyLight.shadow.camera.near = 0.5;
keyLight.shadow.camera.far = 20;
keyLight.shadow.normalBias = 0.025;
scene.add(keyLight);
const fillLight = new THREE.DirectionalLight(0x8cc9c4, 1.55);
fillLight.position.set(-5, 4, -4);
scene.add(fillLight);
// Flat-shaded, low-poly material palette.
const exteriorMaterial = new THREE.MeshStandardMaterial({
color: 0xc65d3b,
roughness: 0.84,
metalness: 0.02,
flatShading: true
});
const exteriorTopMaterial = new THREE.MeshStandardMaterial({
color: 0xdf7950,
roughness: 0.8,
metalness: 0.02,
flatShading: true
});
const interiorMaterial = new THREE.MeshStandardMaterial({
color: 0x315b59,
roughness: 0.92,
metalness: 0,
flatShading: true
});
const lidUndersideMaterial = new THREE.MeshStandardMaterial({
color: 0x244b4b,
roughness: 0.88,
metalness: 0.02,
flatShading: true
});
const detailMaterial = new THREE.MeshStandardMaterial({
color: 0x183536,
roughness: 0.78,
metalness: 0.08,
flatShading: true
});
const hingeMaterial = new THREE.MeshStandardMaterial({
color: 0xf0b64c,
roughness: 0.36,
metalness: 0.58,
flatShading: true
});
const edgeMaterial = new THREE.LineBasicMaterial({
color: 0x552f29,
transparent: true,
opacity: 0.34
});
const groundMaterial = new THREE.MeshStandardMaterial({
color: 0xe3ded4,
roughness: 1,
metalness: 0,
flatShading: true
});
function addPanel(size, position, materials, parent = scene) {
const geometry = new THREE.BoxGeometry(...size);
const mesh = new THREE.Mesh(geometry, materials);
mesh.position.set(...position);
mesh.castShadow = true;
mesh.receiveShadow = true;
parent.add(mesh);
const edges = new THREE.LineSegments(
new THREE.EdgesGeometry(geometry, 20),
edgeMaterial
);
mesh.add(edges);
return mesh;
}
// Ground plane.
const ground = new THREE.Mesh(
new THREE.PlaneGeometry(40, 40),
groundMaterial
);
ground.rotation.x = -Math.PI / 2;
ground.position.y = -0.005;
ground.receiveShadow = true;
scene.add(ground);
// The box is assembled as one logical open container.
// Face order for BoxGeometry: +X, -X, +Y, -Y, +Z, -Z.
const floor = addPanel(
[3.4, 0.18, 2.5],
[0, 0.12, 0],
[
exteriorMaterial,
exteriorMaterial,
interiorMaterial,
exteriorMaterial,
exteriorMaterial,
exteriorMaterial
]
);
const rightWall = addPanel(
[0.14, 1.25, 2.5],
[1.63, 0.835, 0],
[
exteriorMaterial,
interiorMaterial,
exteriorMaterial,
exteriorMaterial,
exteriorMaterial,
exteriorMaterial
]
);
const leftWall = addPanel(
[0.14, 1.25, 2.5],
[-1.63, 0.835, 0],
[
interiorMaterial,
exteriorMaterial,
exteriorMaterial,
exteriorMaterial,
exteriorMaterial,
exteriorMaterial
]
);
const frontWall = addPanel(
[3.12, 1.25, 0.14],
[0, 0.835, 1.18],
[
exteriorMaterial,
exteriorMaterial,
exteriorMaterial,
exteriorMaterial,
exteriorMaterial,
interiorMaterial
]
);
const backWall = addPanel(
[3.12, 1.25, 0.14],
[0, 0.835, -1.18],
[
exteriorMaterial,
exteriorMaterial,
exteriorMaterial,
exteriorMaterial,
interiorMaterial,
exteriorMaterial
]
);
// The lid is a child of a pivot located directly on the visible hinge axis.
const lidPivot = new THREE.Group();
lidPivot.position.set(0, 1.49, -1.23);
scene.add(lidPivot);
const lidSlab = addPanel(
[3.4, 0.22, 2.68],
[0, 0.11, 1.23],
[
exteriorMaterial,
exteriorMaterial,
exteriorTopMaterial,
lidUndersideMaterial,
exteriorMaterial,
exteriorMaterial
],
lidPivot
);
// Recessed lid underside and low-poly reinforcing bars.
addPanel(
[2.68, 0.025, 1.92],
[0, -0.0125, 1.23],
lidUndersideMaterial,
lidPivot
);
addPanel(
[2.55, 0.045, 0.11],
[0, -0.047, 0.72],
detailMaterial,
lidPivot
);
addPanel(
[2.55, 0.045, 0.11],
[0, -0.047, 1.74],
detailMaterial,
lidPivot
);
// A simple front pull, rotating as part of the lid.
const handle = addPanel(
[0.86, 0.16, 0.16],
[0, -0.035, 2.59],
detailMaterial,
lidPivot
);
// Visible hinge axis: a brass-colored cylinder running along local X.
const hingeAxis = new THREE.Mesh(
new THREE.CylinderGeometry(0.055, 0.055, 3.72, 12, 1, false),
hingeMaterial
);
hingeAxis.rotation.z = Math.PI / 2;
hingeAxis.position.set(0, 1.49, -1.23);
hingeAxis.castShadow = true;
scene.add(hingeAxis);
// Hinge collars make the physical axis easier to read.
[-1.25, 0, 1.25].forEach((x) => {
const collar = new THREE.Mesh(
new THREE.CylinderGeometry(0.105, 0.105, 0.34, 12, 1, false),
hingeMaterial
);
collar.rotation.z = Math.PI / 2;
collar.position.set(x, 1.49, -1.23);
collar.castShadow = true;
scene.add(collar);
});
// Arrowhead identifies the X direction of the hinge.
const axisArrow = new THREE.Mesh(
new THREE.ConeGeometry(0.115, 0.27, 10),
hingeMaterial
);
axisArrow.rotation.z = -Math.PI / 2;
axisArrow.position.set(1.98, 1.49, -1.23);
axisArrow.castShadow = true;
scene.add(axisArrow);
function createLabelSprite(text) {
const labelCanvas = document.createElement("canvas");
labelCanvas.width = 640;
labelCanvas.height = 160;
const context = labelCanvas.getContext("2d");
context.fillStyle = "rgba(22, 31, 30, 0.91)";
context.fillRect(0, 0, labelCanvas.width, labelCanvas.height);
context.strokeStyle = "rgba(240, 182, 76, 0.85)";
context.lineWidth = 6;
context.strokeRect(3, 3, labelCanvas.width - 6, labelCanvas.height - 6);
context.fillStyle = "#f2c66e";
context.font = "700 31px monospace";
context.textAlign = "center";
context.textBaseline = "middle";
context.fillText(text, labelCanvas.width / 2, labelCanvas.height / 2 + 2);
const texture = new THREE.CanvasTexture(labelCanvas);
texture.colorSpace = THREE.SRGBColorSpace;
texture.minFilter = THREE.LinearFilter;
const sprite = new THREE.Sprite(
new THREE.SpriteMaterial({
map: texture,
transparent: true,
depthTest: false,
depthWrite: false
})
);
sprite.scale.set(1.85, 0.46, 1);
sprite.renderOrder = 10;
return sprite;
}
const hingeLabel = createLabelSprite("HINGE AXIS / X");
hingeLabel.position.set(1.55, 1.94, -1.23);
scene.add(hingeLabel);
// Custom camera controls: no Three.js addons are used.
const cameraTarget = new THREE.Vector3(0, 1.28, 0);
let cameraTheta = 0.67;
let cameraPhi = 1.17;
let cameraRadius = 8.35;
function updateCamera() {
const sinPhi = Math.sin(cameraPhi);
camera.position.set(
cameraTarget.x + cameraRadius * sinPhi * Math.sin(cameraTheta),
cameraTarget.y + cameraRadius * Math.cos(cameraPhi),
cameraTarget.z + cameraRadius * sinPhi * Math.cos(cameraTheta)
);
camera.lookAt(cameraTarget);
}
function resetCamera() {
cameraTheta = 0.67;
cameraPhi = 1.17;
cameraRadius = 8.35;
updateCamera();
}
updateCamera();
const raycaster = new THREE.Raycaster();
const pointer = new THREE.Vector2();
function pointerHitsLid(event) {
const bounds = renderer.domElement.getBoundingClientRect();
pointer.x =
((event.clientX - bounds.left) / bounds.width) * 2 - 1;
pointer.y =
-((event.clientY - bounds.top) / bounds.height) * 2 + 1;
raycaster.setFromCamera(pointer, camera);
return raycaster.intersectObject(lidSlab, false).length > 0;
}
let dragging = false;
let dragMoved = false;
let lastPointerX = 0;
let lastPointerY = 0;
renderer.domElement.addEventListener("pointerdown", (event) => {
if (event.button !== 0) return;
dragging = true;
dragMoved = false;
lastPointerX = event.clientX;
lastPointerY = event.clientY;
renderer.domElement.setPointerCapture(event.pointerId);
renderer.domElement.style.cursor = "grabbing";
});
renderer.domElement.addEventListener("pointermove", (event) => {
if (dragging) {
const deltaX = event.clientX - lastPointerX;
const deltaY = event.clientY - lastPointerY;
if (Math.abs(event.clientX - lastPointerX) > 2 ||
Math.abs(event.clientY - lastPointerY) > 2) {
dragMoved = true;
}
cameraTheta -= deltaX * 0.006;
cameraPhi = THREE.MathUtils.clamp(
cameraPhi - deltaY * 0.005,
0.47,
1.43
);
lastPointerX = event.clientX;
lastPointerY = event.clientY;
updateCamera();
} else {
renderer.domElement.style.cursor =
pointerHitsLid(event) ? "pointer" : "grab";
}
});
renderer.domElement.addEventListener("pointerup", (event) => {
if (dragging && !dragMoved && pointerHitsLid(event)) {
toggleLid();
}
dragging = false;
renderer.domElement.style.cursor = "grab";
});
renderer.domElement.addEventListener("pointercancel", () => {
dragging = false;
renderer.domElement.style.cursor = "grab";
});
renderer.domElement.addEventListener(
"wheel",
(event) => {
event.preventDefault();
cameraRadius = THREE.MathUtils.clamp(
cameraRadius * Math.exp(event.deltaY * 0.001),
5.2,
12
);
updateCamera();
},
{ passive: false }
);
renderer.domElement.addEventListener("contextmenu", (event) => {
event.preventDefault();
});
// State and action UI.
const OPEN_ANGLE = -THREE.MathUtils.degToRad(108);
const closeButton = document.getElementById("closeButton");
const openButton = document.getElementById("openButton");
const playButton = document.getElementById("playButton");
const playLabel = document.getElementById("playLabel");
const angleSlider = document.getElementById("angleSlider");
const rangeAngle = document.getElementById("rangeAngle");
const angleValue = document.getElementById("angleValue");
const stateName = document.getElementById("stateName");
const stateDot = document.getElementById("stateDot");
const actionCopy = document.getElementById("actionCopy");
const resetViewButton = document.getElementById("resetView");
let currentAngle = 0;
let targetAngle = 0;
let demoActive = false;
let demoStage = null;
let demoStartedAt = 0;
function updateSlider(angle) {
const degrees = Math.round(
THREE.MathUtils.radToDeg(Math.abs(angle))
);
angleSlider.value = String(degrees);
rangeAngle.textContent = `${degrees}°`;
angleSlider.style.setProperty(
"--fill",
`${(degrees / 108) * 100}%`
);
}
function stopDemo() {
demoActive = false;
demoStage = null;
playLabel.textContent = "PLAY STATES";
}
function setTarget(angle) {
targetAngle = angle;
updateSlider(targetAngle);
const atOpen = Math.abs(targetAngle - OPEN_ANGLE) < 0.002;
const atClosed = Math.abs(targetAngle) < 0.002;
closeButton.classList.toggle("active", atClosed);
openButton.classList.toggle("active", atOpen);
}
function toggleLid() {
stopDemo();
const isMostlyOpen = Math.abs(currentAngle) > Math.abs(OPEN_ANGLE) * 0.45;
setTarget(isMostlyOpen ? 0 : OPEN_ANGLE);
}
closeButton.addEventListener("click", () => {
stopDemo();
setTarget(0);
});
openButton.addEventListener("click", () => {
stopDemo();
setTarget(OPEN_ANGLE);
});
angleSlider.addEventListener("input", () => {
stopDemo();
const degrees = Number(angleSlider.value);
setTarget(-THREE.MathUtils.degToRad(degrees));
});
playButton.addEventListener("click", () => {
if (demoActive) {
stopDemo();
return;
}
demoActive = true;
demoStage = "open";
demoStartedAt = performance.now();
playLabel.textContent = "STOP";
setTarget(OPEN_ANGLE);
});
resetViewButton.addEventListener("click", resetCamera);
window.addEventListener("keydown", (event) => {
if (event.target instanceof HTMLInputElement) return;
if (event.key.toLowerCase() === "o") {
stopDemo();
setTarget(OPEN_ANGLE);
} else if (event.key.toLowerCase() === "c") {
stopDemo();
setTarget(0);
} else if (event.code === "Space") {
event.preventDefault();
playButton.click();
} else if (event.key.toLowerCase() === "r") {
resetCamera();
}
});
function updateStateUI() {
const actualDegrees = Math.round(
THREE.MathUtils.radToDeg(Math.abs(currentAngle))
);
angleValue.textContent = `${actualDegrees}°`;
const targetOpen = Math.abs(targetAngle - OPEN_ANGLE) < 0.002;
const targetClosed = Math.abs(targetAngle) < 0.002;
const moving = Math.abs(targetAngle - currentAngle) > 0.012;
let label;
let color;
let copy;
if (targetOpen && moving) {
label = "OPENING";
color = "var(--moving)";
copy = "OPEN — the lid rotates around the visible X-axis hinge.";
} else if (targetClosed && moving) {
label = "CLOSING";
color = "var(--moving)";
copy = "CLOSE — the lid returns toward its seated position.";
} else if (targetOpen) {
label = "OPEN";
color = "var(--open)";
copy = "OPEN — the lid is rotated 108°; the interior is exposed.";
} else if (targetClosed) {
label = "CLOSED";
color = "var(--closed)";
copy = "CLOSED — the lid rests on the box at 0°.";
} else {
label = "MOVING";
color = "var(--moving)";
copy = "ADJUST — the lid follows the rotation input around the hinge.";
}
stateName.textContent = label;
stateDot.style.background = color;
stateDot.style.boxShadow = `0 0 0 4px color-mix(in srgb, ${color} 16%, transparent)`;
actionCopy.textContent = copy;
}
function updateDemo(now) {
if (!demoActive) return;
const elapsed = now - demoStartedAt;
if (demoStage === "open" && elapsed > 1900) {
demoStage = "close";
demoStartedAt = now;
setTarget(0);
} else if (demoStage === "close" && elapsed > 1900) {
demoStage = "reopen";
demoStartedAt = now;
setTarget(OPEN_ANGLE);
} else if (demoStage === "reopen" && elapsed > 1900) {
stopDemo();
}
}
const clock = new THREE.Clock();
function animate(now) {
requestAnimationFrame(animate);
const delta = Math.min(clock.getDelta(), 0.05);
const smoothing = 1 - Math.exp(-delta * 7.5);
currentAngle += (targetAngle - currentAngle) * smoothing;
if (Math.abs(targetAngle - currentAngle) < 0.0001) {
currentAngle = targetAngle;
}
// This is the only mechanism that reveals the interior.
lidPivot.rotation.x = currentAngle;
updateDemo(now);
updateStateUI();
renderer.render(scene, camera);
}
requestAnimationFrame(animate);
// Begin with a short closed hold, then physically open the box.
setTimeout(() => {
setTarget(OPEN_ANGLE);
}, 650);
window.addEventListener("resize", () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
});
</script>
</body>
</html>
```