Prompt specification
Build or customize the Three.js 3D scene "Pocket door disappearance sequence" 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: Pocket door disappearance sequence
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/pocket-door-disappearance-sequence-x0234
Scene Description Prompt:
Make a standalone HTML scene with Three.js from cdnjs that depicts sliding and hinged doors. Animate a thin door panel moving into an exposed wall pocket; keep guide tracks visible and stop its travel before the handle enters the cavity. Voxel style.
Working Three.js Starter (pocket-door-disappearance-sequence-x0234):
```html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Voxel Pocket and Hinged Doors</title>
<style>
:root {
color-scheme: dark;
font-family: "Courier New", monospace;
--panel: rgba(13, 20, 29, 0.88);
--border: rgba(146, 185, 202, 0.24);
--muted: #8ea5b3;
--text: #edf7fb;
--cyan: #55d6c7;
--amber: #f0ae58;
--red: #ff6961;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #101720;
}
#scene {
position: fixed;
inset: 0;
display: block;
width: 100%;
height: 100%;
touch-action: none;
outline: none;
}
.hud {
position: fixed;
top: 18px;
right: 18px;
width: min(360px, calc(100vw - 36px));
padding: 18px;
color: var(--text);
background: var(--panel);
border: 1px solid var(--border);
border-radius: 14px;
box-shadow: 0 16px 45px rgba(0, 0, 0, 0.28);
backdrop-filter: blur(12px);
pointer-events: none;
}
.eyebrow {
margin-bottom: 5px;
color: var(--cyan);
font-size: 11px;
letter-spacing: 0.2em;
text-transform: uppercase;
}
h1 {
margin: 0;
font-size: clamp(22px, 4vw, 30px);
letter-spacing: -0.05em;
line-height: 1;
}
.subtitle {
margin: 10px 0 16px;
color: var(--muted);
font-size: 12px;
line-height: 1.55;
}
.metrics {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 7px;
}
.metric {
min-width: 0;
padding: 10px 8px;
background: rgba(255, 255, 255, 0.045);
border: 1px solid rgba(255, 255, 255, 0.07);
border-radius: 8px;
}
.metric span {
display: block;
overflow: hidden;
color: var(--muted);
font-size: 9px;
letter-spacing: 0.08em;
text-overflow: ellipsis;
white-space: nowrap;
}
.metric strong {
display: block;
margin-top: 7px;
color: var(--text);
font-size: 15px;
white-space: nowrap;
}
.state {
display: flex;
align-items: center;
gap: 8px;
margin-top: 12px;
color: var(--amber);
font-size: 11px;
line-height: 1.4;
}
.state::before {
width: 8px;
height: 8px;
flex: 0 0 auto;
content: "";
background: var(--amber);
border-radius: 50%;
box-shadow: 0 0 12px currentColor;
}
.state.safe {
color: var(--cyan);
}
.state.safe::before {
background: var(--cyan);
}
.controls {
position: fixed;
right: 18px;
bottom: 18px;
display: flex;
flex-wrap: wrap;
justify-content: flex-end;
gap: 8px;
max-width: calc(100vw - 36px);
pointer-events: auto;
}
button {
min-height: 40px;
padding: 0 14px;
color: var(--text);
font: 700 11px "Courier New", monospace;
letter-spacing: 0.04em;
text-transform: uppercase;
background: rgba(13, 20, 29, 0.9);
border: 1px solid var(--border);
border-radius: 9px;
cursor: pointer;
transition:
color 140ms ease,
border-color 140ms ease,
background 140ms ease,
transform 140ms ease;
}
button:hover,
button:focus-visible {
color: var(--cyan);
background: rgba(30, 53, 63, 0.95);
border-color: rgba(85, 214, 199, 0.65);
outline: none;
transform: translateY(-1px);
}
button.primary {
color: #081412;
background: var(--cyan);
border-color: var(--cyan);
}
button.primary:hover {
color: #081412;
background: #79eadf;
}
.hint {
position: fixed;
bottom: 22px;
left: 20px;
color: rgba(224, 241, 247, 0.64);
font-size: 11px;
line-height: 1.5;
pointer-events: none;
}
#loading {
position: fixed;
inset: 0;
z-index: 20;
display: grid;
place-items: center;
color: var(--cyan);
font-size: 12px;
letter-spacing: 0.18em;
text-transform: uppercase;
background: #101720;
transition: opacity 250ms ease;
}
#loading.hidden {
opacity: 0;
pointer-events: none;
}
@media (max-width: 700px) {
.hud {
top: 10px;
right: 10px;
width: calc(100vw - 20px);
padding: 14px;
}
.subtitle {
margin-bottom: 11px;
}
.controls {
right: 10px;
bottom: 10px;
left: 10px;
justify-content: stretch;
}
button {
flex: 1 1 42%;
}
.hint {
display: none;
}
}
</style>
</head>
<body>
<canvas id="scene" aria-label="Interactive voxel scene with a sliding pocket door and a hinged door"></canvas>
<section class="hud" aria-live="polite">
<div class="eyebrow">Voxel Door Study</div>
<h1>POCKET / HINGE</h1>
<p class="subtitle">
A thin sliding panel enters the exposed wall cavity.
The orange catcher limits travel so the handle remains outside.
</p>
<div class="metrics">
<div class="metric">
<span>SLIDE TRAVEL</span>
<strong id="travelReadout">0.00 m</strong>
</div>
<div class="metric">
<span>HANDLE CLEARANCE</span>
<strong id="clearanceReadout">1.37 m</strong>
</div>
<div class="metric">
<span>HINGE ANGLE</span>
<strong id="hingeReadout">0°</strong>
</div>
</div>
<div class="state" id="stateReadout">Traveling toward the safe stop</div>
</section>
<nav class="controls" aria-label="Scene controls">
<button class="primary" id="playButton" type="button">Pause</button>
<button id="replayButton" type="button">Replay</button>
<button id="stopButton" type="button">Inspect stop</button>
<button id="cameraButton" type="button">Reset view</button>
</nav>
<div class="hint">
DRAG TO ORBIT · SCROLL TO ZOOM<br>
DOUBLE-CLICK TO RESET THE CAMERA
</div>
<div id="loading">Building voxel scene…</div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// -------------------------------------------------------------------------
// Renderer, scene, camera, and lights
// -------------------------------------------------------------------------
const canvas = document.querySelector("#scene");
const renderer = new THREE.WebGLRenderer({
canvas,
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;
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x101720);
scene.fog = new THREE.Fog(0x101720, 18, 34);
const camera = new THREE.PerspectiveCamera(
48,
window.innerWidth / window.innerHeight,
0.1,
100
);
const cameraTarget = new THREE.Vector3(-0.1, 1.85, 0);
const cameraState = {
yaw: 0.61,
pitch: 0.29,
radius: 13.7
};
function updateCamera() {
const { yaw, pitch, radius } = cameraState;
const horizontal = Math.cos(pitch) * radius;
camera.position.set(
cameraTarget.x + Math.sin(yaw) * horizontal,
cameraTarget.y + Math.sin(pitch) * radius,
cameraTarget.z + Math.cos(yaw) * horizontal
);
camera.lookAt(cameraTarget);
}
updateCamera();
const hemisphere = new THREE.HemisphereLight(0xd9f2ff, 0x493b32, 1.65);
scene.add(hemisphere);
const sun = new THREE.DirectionalLight(0xfff0d7, 3.4);
sun.position.set(5.5, 10, 8);
sun.castShadow = true;
sun.shadow.mapSize.set(2048, 2048);
sun.shadow.camera.left = -10;
sun.shadow.camera.right = 10;
sun.shadow.camera.top = 10;
sun.shadow.camera.bottom = -7;
sun.shadow.camera.near = 1;
sun.shadow.camera.far = 30;
sun.shadow.normalBias = 0.035;
scene.add(sun);
const pocketLight = new THREE.PointLight(0x61d9cf, 32, 7, 2);
pocketLight.position.set(-3.1, 2.7, 0.65);
scene.add(pocketLight);
const hingeLight = new THREE.PointLight(0xffb767, 24, 7, 2);
hingeLight.position.set(2.8, 2.7, 1.2);
scene.add(hingeLight);
// -------------------------------------------------------------------------
// Materials and voxel construction helpers
// -------------------------------------------------------------------------
const materials = {
wall: new THREE.MeshStandardMaterial({
color: 0xb7b0a2,
roughness: 0.94,
flatShading: true
}),
wallDark: new THREE.MeshStandardMaterial({
color: 0x666d73,
roughness: 0.9,
flatShading: true
}),
cavity: new THREE.MeshStandardMaterial({
color: 0x293541,
roughness: 0.9,
flatShading: true
}),
cavityAlt: new THREE.MeshStandardMaterial({
color: 0x354654,
roughness: 0.9,
flatShading: true
}),
rail: new THREE.MeshStandardMaterial({
color: 0x18242e,
roughness: 0.55,
metalness: 0.55,
flatShading: true
}),
railEdge: new THREE.MeshStandardMaterial({
color: 0x5c7a88,
roughness: 0.45,
metalness: 0.65,
flatShading: true
}),
cyan: new THREE.MeshStandardMaterial({
color: 0x55d6c7,
roughness: 0.58,
flatShading: true
}),
cyanDark: new THREE.MeshStandardMaterial({
color: 0x236f69,
roughness: 0.7,
flatShading: true
}),
sliding: new THREE.MeshStandardMaterial({
color: 0x45b9ad,
roughness: 0.67,
flatShading: true
}),
amber: new THREE.MeshStandardMaterial({
color: 0xe5a34e,
roughness: 0.69,
flatShading: true
}),
amberDark: new THREE.MeshStandardMaterial({
color: 0x8f5a31,
roughness: 0.8,
flatShading: true
}),
glass: new THREE.MeshStandardMaterial({
color: 0x193b4c,
roughness: 0.22,
metalness: 0.25,
flatShading: true
}),
dark: new THREE.MeshStandardMaterial({
color: 0x14202a,
roughness: 0.45,
metalness: 0.5,
flatShading: true
}),
stop: new THREE.MeshStandardMaterial({
color: 0xff625b,
emissive: 0x5f0906,
emissiveIntensity: 0.45,
roughness: 0.55,
flatShading: true
})
};
const edgeMaterial = new THREE.LineBasicMaterial({
color: 0x111a22,
transparent: true,
opacity: 0.32,
depthWrite: false
});
function block(size, position, material, parent = scene, outlined = false, cast = true) {
const geometry = new THREE.BoxGeometry(...size);
const mesh = new THREE.Mesh(geometry, material);
mesh.position.set(...position);
mesh.castShadow = cast;
mesh.receiveShadow = true;
parent.add(mesh);
if (outlined) {
const edges = new THREE.LineSegments(
new THREE.EdgesGeometry(geometry),
edgeMaterial
);
edges.renderOrder = 2;
mesh.add(edges);
}
return mesh;
}
function makeLabel(text, accent = "#55d6c7", width = 2.2) {
const labelCanvas = document.createElement("canvas");
labelCanvas.width = 512;
labelCanvas.height = 96;
const context = labelCanvas.getContext("2d");
context.clearRect(0, 0, 512, 96);
context.fillStyle = "rgba(10, 17, 23, 0.84)";
context.fillRect(2, 8, 508, 80);
context.fillStyle = accent;
context.fillRect(2, 8, 7, 80);
context.font = "700 30px monospace";
context.textAlign = "center";
context.textBaseline = "middle";
context.fillText(text, 258, 49);
const texture = new THREE.CanvasTexture(labelCanvas);
texture.colorSpace = THREE.SRGBColorSpace;
texture.minFilter = THREE.NearestFilter;
texture.magFilter = THREE.NearestFilter;
texture.generateMipmaps = false;
const material = new THREE.SpriteMaterial({
map: texture,
transparent: true,
depthTest: false,
depthWrite: false,
fog: false
});
const sprite = new THREE.Sprite(material);
sprite.scale.set(width, width * 96 / 512, 1);
sprite.renderOrder = 10;
return sprite;
}
// -------------------------------------------------------------------------
// Voxel floor
// -------------------------------------------------------------------------
block([15.8, 0.32, 10], [0, -0.2, 0.3], materials.wallDark, scene, false, false);
const floorA = new THREE.MeshStandardMaterial({
color: 0x52616a,
roughness: 0.95,
flatShading: true
});
const floorB = new THREE.MeshStandardMaterial({
color: 0x47555f,
roughness: 0.95,
flatShading: true
});
for (let ix = 0; ix < 11; ix++) {
for (let iz = 0; iz < 8; iz++) {
const x = -6.5 + ix * 1.3;
const z = -4.35 + iz * 1.25;
const material = (ix + iz) % 2 === 0 ? floorA : floorB;
block(
[1.24, 0.055, 1.19],
[x, -0.005, z],
material,
scene,
false,
false
);
}
}
// -------------------------------------------------------------------------
// Facade: two openings, a pier, and the exposed pocket
// -------------------------------------------------------------------------
// Far-left wall section.
block([0.75, 4.25, 0.5], [-4.825, 2.125, -0.25], materials.wall, scene, true);
// Top facade above the exposed pocket.
block([3.2, 0.8, 0.5], [-2.85, 3.85, -0.25], materials.wall, scene, true);
// Lintels above each doorway.
block([1.7, 0.8, 0.5], [-0.4, 3.85, -0.25], materials.wall, scene, true);
block([1.7, 0.8, 0.5], [1.6, 3.85, -0.25], materials.wall, scene, true);
// Center masonry pier.
block([0.3, 4.25, 0.5], [0.6, 2.125, -0.25], materials.wall, scene, true);
// Right wall section.
block([2.75, 4.25, 0.5], [3.825, 2.125, -0.25], materials.wall, scene, true);
// Dark room interiors visible through both openings.
block([1.7, 3.36, 0.16], [-0.4, 1.72, -0.72], materials.cavity, scene, true);
block([1.7, 3.36, 0.16], [1.6, 1.72, -0.72], materials.cavity, scene, true);
// Threshold blocks.
block([1.7, 0.08, 0.62], [-0.4, 0.04, -0.06], materials.wallDark, scene, true);
block([1.7, 0.08, 0.62], [1.6, 0.04, -0.06], materials.wallDark, scene, true);
// Pocket cavity: back, closed end, roof, and floor. Its front is intentionally
// omitted so the viewer can see the door entering.
block([3.1, 3.2, 0.2], [-2.85, 1.75, -0.66], materials.cavity, scene, true);
block([0.2, 3.2, 0.82], [-4.38, 1.75, -0.22], materials.wallDark, scene, true);
block([3.1, 0.18, 0.82], [-2.85, 0.09, -0.22], materials.wallDark, scene, true);
block([3.1, 0.18, 0.82], [-2.85, 3.43, -0.22], materials.wallDark, scene, true);
// Pixel-like masonry inside the pocket.
for (let row = 0; row < 4; row++) {
for (let col = 0; col < 4; col++) {
block(
[0.64, 0.42, 0.07],
[-4.02 + col * 0.76, 0.48 + row * 0.73, -0.52],
(row + col) % 2 === 0 ? materials.cavityAlt : materials.cavity,
scene,
true,
false
);
}
}
// Structural ribs visible inside the exposed cavity.
for (const x of [-3.55, -2.75, -1.95]) {
block([0.12, 2.95, 0.12], [x, 1.75, -0.44], materials.railEdge, scene, true);
}
// -------------------------------------------------------------------------
// Exposed guide tracks
// -------------------------------------------------------------------------
const guideStart = -4.35;
const guideEnd = 0.45;
const guideLength = guideEnd - guideStart;
const guideCenter = (guideStart + guideEnd) / 2;
// Upper U-channel.
block(
[guideLength, 0.1, 0.055],
[guideCenter, 3.46, -0.04],
materials.rail,
scene,
true
);
block(
[guideLength, 0.055, 0.22],
[guideCenter, 3.39, 0.075],
materials.railEdge,
scene,
true
);
block(
[guideLength - 0.15, 0.026, 0.045],
[guideCenter, 3.385, 0.195],
materials.cyan,
scene,
false,
false
);
// Lower guide rail.
block(
[guideLength, 0.055, 0.24],
[guideCenter, 0.12, 0.035],
materials.rail,
scene,
true
);
block(
[guideLength - 0.15, 0.022, 0.04],
[guideCenter, 0.153, 0.16],
materials.cyan,
scene,
false,
false
);
// Small exposed track markers.
for (const x of [-3.75, -3.0, -2.25, -1.5]) {
block([0.09, 0.075, 0.07], [x, 3.39, 0.205], materials.stop, scene, false, false);
}
// -------------------------------------------------------------------------
// Sliding door
// -------------------------------------------------------------------------
const closedSliderX = -0.4;
const maxTravel = 1.2;
const pocketMouthX = -1.25;
const handleOffset = 0.6;
const handleAssemblyHalfWidth = 0.08;
const sliderGroup = new THREE.Group();
sliderGroup.position.set(closedSliderX, 0, 0.03);
scene.add(sliderGroup);
// Thin sliding slab.
block(
[1.55, 3.08, 0.14],
[0, 1.74, 0],
materials.sliding,
sliderGroup,
true
);
// Voxel frame.
block([0.11, 3.0, 0.06], [-0.7, 1.74, 0.095], materials.cyanDark, sliderGroup, true);
block([0.11, 3.0, 0.06], [0.7, 1.74, 0.095], materials.cyanDark, sliderGroup, true);
block([1.43, 0.11, 0.06], [0, 3.23, 0.095], materials.cyanDark, sliderGroup, true);
block([1.43, 0.11, 0.06], [0, 0.25, 0.095], materials.cyanDark, sliderGroup, true);
// Decorative inset panels.
block([0.58, 0.64, 0.035], [-0.08, 2.35, 0.1], materials.glass, sliderGroup, true);
block([0.08, 0.64, 0.045], [-0.08, 2.35, 0.125], materials.cyanDark, sliderGroup, true);
block([0.58, 0.08, 0.045], [-0.08, 2.35, 0.125], materials.cyanDark, sliderGroup, true);
block([0.46, 0.42, 0.035], [-0.29, 1.13, 0.1], materials.cyanDark, sliderGroup, true);
block([0.46, 0.42, 0.035], [0.29, 1.13, 0.1], materials.cyanDark, sliderGroup, true);
// Handle and its two blocky mounts.
block(
[0.16, 0.12, 0.14],
[handleOffset, 1.36, 0.16],
materials.dark,
sliderGroup,
true
);
block(
[0.16, 0.12, 0.14],
[handleOffset, 2.04, 0.16],
materials.dark,
sliderGroup,
true
);
block(
[0.12, 0.7, 0.14],
[handleOffset, 1.7, 0.22],
materials.dark,
sliderGroup,
true
);
// Chunky trolley blocks at the top of the sliding panel.
block([0.2, 0.15, 0.18], [-0.46, 3.32, 0.01], materials.railEdge, sliderGroup, true);
block([0.2, 0.15, 0.18], [0.46, 3.32, 0.01], materials.railEdge, sliderGroup, true);
// Orange catcher at the pocket mouth. Its right edge is exactly where the
// handle mounts stop, leaving the handle outside the cavity.
const stopX = -1.12;
const stopWidth = 0.08;
block(
[stopWidth, 0.72, 0.12],
[stopX, 1.7, 0.18],
materials.stop,
scene,
true
);
block(
[0.18, 0.12, 0.18],
[stopX, 2.1, 0.18],
materials.stop,
scene,
true
);
// -------------------------------------------------------------------------
// Hinged door
// -------------------------------------------------------------------------
const hingePivot = new THREE.Group();
hingePivot.position.set(2.42, 0, 0.04);
scene.add(hingePivot);
block(
[1.55, 3.08, 0.14],
[-0.775, 1.74, 0],
materials.amber,
hingePivot,
true
);
block([0.11, 3.0, 0.06], [-0.04, 1.74, 0.095], materials.amberDark, hingePivot, true);
block([0.11, 3.0, 0.06], [-1.51, 1.74, 0.095], materials.amberDark, hingePivot, true);
block([1.5, 0.11, 0.06], [-0.775, 3.23, 0.095], materials.amberDark, hingePivot, true);
block([1.5, 0.11, 0.06], [-0.775, 0.25, 0.095], materials.amberDark, hingePivot, true);
block([0.72, 1.0, 0.035], [-0.78, 2.27, 0.1], materials.glass, hingePivot, true);
block([0.08, 1.0, 0.045], [-0.78, 2.27, 0.125], materials.amberDark, hingePivot, true);
block([0.72, 0.08, 0.045], [-0.78, 2.27, 0.125], materials.amberDark, hingePivot, true);
block([0.47, 0.42, 0.035], [-0.78, 1.12, 0.1], materials.amberDark, hingePivot, true);
// Hinged door handle.
block([0.14, 0.12, 0.13], [-1.34, 1.36, 0.15], materials.dark, hingePivot, true);
block([0.14, 0.12, 0.13], [-1.34, 2.04, 0.15], materials.dark, hingePivot, true);
block([0.11, 0.7, 0.13], [-1.34, 1.7, 0.21], materials.dark, hingePivot, true);
// Fixed hinge blocks.
block([0.22, 0.28, 0.28], [2.42, 0.72, 0.11], materials.dark, scene, true);
block([0.22, 0.28, 0.28], [2.42, 2.76, 0.11], materials.dark, scene, true);
// -------------------------------------------------------------------------
// Labels and floor direction marker
// -------------------------------------------------------------------------
const pocketLabel = makeLabel("POCKET CUTAWAY", "#55d6c7", 2.45);
pocketLabel.position.set(-2.85, 3.88, 0.35);
scene.add(pocketLabel);
const slideLabel = makeLabel("SLIDE", "#55d6c7", 1.1);
slideLabel.position.set(-0.4, 3.88, 0.35);
scene.add(slideLabel);
const hingeLabel = makeLabel("HINGE", "#f0ae58", 1.1);
hingeLabel.position.set(1.6, 3.88, 0.35);
scene.add(hingeLabel);
const stopLabel = makeLabel("SAFE STOP", "#ff6961", 1.45);
stopLabel.position.set(-1.12, 2.68, 0.45);
scene.add(stopLabel);
// Block-built left arrow on the floor.
block([0.86, 0.035, 0.12], [-0.54, 0.06, 0.87], materials.cyan, scene, false, false);
block([0.18, 0.045, 0.42], [-1.02, 0.065, 0.87], materials.cyan, scene, false, false);
block([0.18, 0.04, 0.28], [-0.87, 0.06, 0.87], materials.cyan, scene, false, false);
// -------------------------------------------------------------------------
// Animation
// -------------------------------------------------------------------------
let simulationTime = 0;
let playing = true;
let sliderTravel = 0;
let hingeAngle = 0;
function smoothstep(value) {
const t = THREE.MathUtils.clamp(value, 0, 1);
return t * t * (3 - 2 * t);
}
function getSliderTravel(time) {
const phase = time % 10;
if (phase < 1) return 0;
if (phase < 5) return smoothstep((phase - 1) / 4);
if (phase < 8) return 1;
return 1 - smoothstep((phase - 8) / 2);
}
function getHingeAngle(time) {
const phase = time % 8;
if (phase < 1) return 0;
if (phase < 3.4) return 68 * smoothstep((phase - 1) / 2.4);
if (phase < 5.8) return 68;
if (phase < 7.8) return 68 * (1 - smoothstep((phase - 5.8) / 2));
return 0;
}
const travelReadout = document.querySelector("#travelReadout");
const clearanceReadout = document.querySelector("#clearanceReadout");
const hingeReadout = document.querySelector("#hingeReadout");
const stateReadout = document.querySelector("#stateReadout");
const playButton = document.querySelector("#playButton");
function updateScene() {
sliderTravel = getSliderTravel(simulationTime);
hingeAngle = getHingeAngle(simulationTime);
sliderGroup.position.x = closedSliderX - sliderTravel * maxTravel;
hingePivot.rotation.y = THREE.MathUtils.degToRad(hingeAngle);
const handleAssemblyLeft =
sliderGroup.position.x + handleOffset - handleAssemblyHalfWidth;
const clearance = Math.max(
0,
handleAssemblyLeft - pocketMouthX
);
travelReadout.textContent = `${(sliderTravel * maxTravel).toFixed(2)} m`;
clearanceReadout.textContent = `${clearance.toFixed(2)} m`;
hingeReadout.textContent = `${Math.round(hingeAngle)}°`;
if (sliderTravel > 0.995) {
stateReadout.textContent = "Safe stop reached — handle remains outside";
stateReadout.classList.add("safe");
} else {
stateReadout.textContent = "Traveling toward the safe stop";
stateReadout.classList.remove("safe");
}
}
// -------------------------------------------------------------------------
// Direct camera controls, implemented without camera-control addons
// -------------------------------------------------------------------------
const pointer = {
active: false,
x: 0,
y: 0
};
canvas.addEventListener("pointerdown", (event) => {
if (event.button !== 0) return;
pointer.active = true;
pointer.x = event.clientX;
pointer.y = event.clientY;
canvas.setPointerCapture(event.pointerId);
});
canvas.addEventListener("pointermove", (event) => {
if (!pointer.active) return;
const dx = event.clientX - pointer.x;
const dy = event.clientY - pointer.y;
pointer.x = event.clientX;
pointer.y = event.clientY;
cameraState.yaw -= dx * 0.006;
cameraState.pitch = THREE.MathUtils.clamp(
cameraState.pitch + dy * 0.0045,
-0.08,
1.08
);
updateCamera();
});
function releasePointer(event) {
pointer.active = false;
if (event.pointerId !== undefined && canvas.hasPointerCapture(event.pointerId)) {
canvas.releasePointerCapture(event.pointerId);
}
}
canvas.addEventListener("pointerup", releasePointer);
canvas.addEventListener("pointercancel", releasePointer);
canvas.addEventListener("contextmenu", (event) => event.preventDefault());
canvas.addEventListener(
"wheel",
(event) => {
event.preventDefault();
cameraState.radius = THREE.MathUtils.clamp(
cameraState.radius * Math.exp(event.deltaY * 0.001),
7,
20
);
updateCamera();
},
{ passive: false }
);
function resetCamera() {
cameraState.yaw = 0.61;
cameraState.pitch = 0.29;
cameraState.radius = 13.7;
updateCamera();
}
canvas.addEventListener("dblclick", resetCamera);
// -------------------------------------------------------------------------
// UI controls
// -------------------------------------------------------------------------
playButton.addEventListener("click", () => {
playing = !playing;
playButton.textContent = playing ? "Pause" : "Resume";
});
document.querySelector("#replayButton").addEventListener("click", () => {
simulationTime = 0;
playing = true;
playButton.textContent = "Pause";
});
document.querySelector("#stopButton").addEventListener("click", () => {
// Five seconds places the slider at its fully extended safe stop while
// the hinged leaf remains open for inspection.
simulationTime = 5;
playing = false;
playButton.textContent = "Resume";
updateScene();
});
document.querySelector("#cameraButton").addEventListener("click", resetCamera);
window.addEventListener("keydown", (event) => {
if (event.code === "Space") {
event.preventDefault();
playButton.click();
}
if (event.code === "Escape") {
resetCamera();
}
});
window.addEventListener("resize", () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
});
// -------------------------------------------------------------------------
// Render loop
// -------------------------------------------------------------------------
const clock = new THREE.Clock();
function animate() {
requestAnimationFrame(animate);
const delta = Math.min(clock.getDelta(), 0.05);
if (playing) {
simulationTime += delta;
}
updateScene();
renderer.render(scene, camera);
}
updateScene();
renderer.render(scene, camera);
document.querySelector("#loading").classList.add("hidden");
animate();
</script>
</body>
</html>
```