Prompt specification
Build or customize the Three.js 3D scene "Cable length and slack demo" 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: Cable length and slack demo
Category: Architecture & Cities · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/cable-length-and-slack-demo-x0174
Scene Description Prompt:
Please use Three.js from cdnjs for a low-poly scene based on this idea: cable length and slack demo. Slide a desk drawer beside a flexible cable loop; sufficient slack permits full travel, while an illustrated short route stops before the drawer opens completely. Return one self-contained HTML file.
Working Three.js Starter (cable-length-and-slack-demo-x0174):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Cable & Drawer — Slack vs. Short Route</title>
<style>
:root {
--ink: #173b3a;
--muted: #617572;
--paper: rgba(250, 251, 247, 0.88);
--line: rgba(23, 59, 58, 0.14);
--teal: #087f80;
--teal-light: #d9eeea;
--orange: #df6548;
--orange-light: #f7e4dc;
--shadow: 0 18px 50px rgba(39, 65, 61, 0.12);
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #eef1eb;
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;
width: 100%;
height: 100%;
display: block;
touch-action: none;
cursor: grab;
outline: none;
}
.glass {
background: var(--paper);
border: 1px solid rgba(255, 255, 255, 0.7);
box-shadow: var(--shadow);
backdrop-filter: blur(16px) saturate(1.1);
-webkit-backdrop-filter: blur(16px) saturate(1.1);
}
.brand {
position: fixed;
z-index: 10;
top: 24px;
left: 24px;
width: min(360px, calc(100vw - 48px));
padding: 19px 21px 18px;
border-radius: 20px;
pointer-events: none;
}
.eyebrow {
display: flex;
align-items: center;
gap: 8px;
margin-bottom: 8px;
color: var(--teal);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.16em;
text-transform: uppercase;
}
.eyebrow::before {
width: 22px;
height: 2px;
content: "";
background: currentColor;
border-radius: 2px;
}
h1 {
margin: 0;
font-size: clamp(24px, 3vw, 35px);
font-weight: 760;
letter-spacing: -0.045em;
line-height: 1.02;
}
.brand p {
margin: 9px 0 0;
max-width: 310px;
color: var(--muted);
font-size: 13px;
line-height: 1.5;
}
.route-panel {
position: fixed;
z-index: 10;
top: 24px;
right: 24px;
width: 326px;
padding: 13px;
border-radius: 20px;
}
.panel-label {
margin: 2px 5px 10px;
color: var(--muted);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.14em;
text-transform: uppercase;
}
.route-options {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 8px;
}
.route-button {
min-height: 68px;
padding: 11px 12px;
text-align: left;
color: var(--ink);
background: rgba(255, 255, 255, 0.55);
border: 1px solid var(--line);
border-radius: 14px;
cursor: pointer;
transition: 160ms ease;
}
.route-button:hover {
transform: translateY(-1px);
border-color: rgba(8, 127, 128, 0.35);
}
.route-button.active {
color: white;
background: var(--teal);
border-color: var(--teal);
box-shadow: 0 8px 18px rgba(8, 127, 128, 0.2);
}
body[data-route="short"] .route-button.active {
background: var(--orange);
border-color: var(--orange);
box-shadow: 0 8px 18px rgba(223, 101, 72, 0.22);
}
.route-button strong {
display: block;
margin-bottom: 4px;
font-size: 13px;
}
.route-button span {
display: block;
opacity: 0.75;
font-size: 10px;
line-height: 1.35;
}
.travel-panel {
position: fixed;
z-index: 10;
bottom: 24px;
left: 24px;
width: 354px;
padding: 17px 18px 16px;
border-radius: 20px;
}
.travel-heading {
display: flex;
align-items: center;
justify-content: space-between;
gap: 12px;
margin-bottom: 13px;
}
.travel-heading label {
font-size: 12px;
font-weight: 800;
letter-spacing: 0.03em;
}
#travelValue {
min-width: 48px;
color: var(--teal);
font-size: 18px;
font-weight: 800;
text-align: right;
}
body[data-route="short"] #travelValue {
color: var(--orange);
}
.range-wrap {
position: relative;
padding: 2px 0 7px;
}
input[type="range"] {
width: 100%;
height: 6px;
margin: 0;
appearance: none;
-webkit-appearance: none;
background: linear-gradient(
90deg,
var(--teal) 0%,
var(--teal) var(--progress, 0%),
rgba(23, 59, 58, 0.13) var(--progress, 0%),
rgba(23, 59, 58, 0.13) 100%
);
border-radius: 999px;
cursor: pointer;
}
body[data-route="short"] input[type="range"] {
background: linear-gradient(
90deg,
var(--orange) 0%,
var(--orange) var(--progress, 0%),
rgba(23, 59, 58, 0.13) var(--progress, 0%),
rgba(23, 59, 58, 0.13) 100%
);
}
input[type="range"]::-webkit-slider-thumb {
width: 19px;
height: 19px;
appearance: none;
-webkit-appearance: none;
background: white;
border: 5px solid var(--teal);
border-radius: 50%;
box-shadow: 0 3px 10px rgba(23, 59, 58, 0.22);
}
input[type="range"]::-moz-range-thumb {
width: 10px;
height: 10px;
background: white;
border: 5px solid var(--teal);
border-radius: 50%;
box-shadow: 0 3px 10px rgba(23, 59, 58, 0.22);
}
body[data-route="short"] input[type="range"]::-webkit-slider-thumb {
border-color: var(--orange);
}
body[data-route="short"] input[type="range"]::-moz-range-thumb {
border-color: var(--orange);
}
.range-labels {
display: flex;
justify-content: space-between;
margin-top: 5px;
color: var(--muted);
font-size: 9px;
font-weight: 700;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.travel-actions {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 8px;
margin-top: 13px;
}
.action-button {
padding: 10px 12px;
color: var(--ink);
background: white;
border: 1px solid var(--line);
border-radius: 11px;
font-size: 11px;
font-weight: 800;
cursor: pointer;
transition: 150ms ease;
}
.action-button.primary {
color: white;
background: var(--ink);
border-color: var(--ink);
}
.action-button:hover {
transform: translateY(-1px);
}
.status {
display: flex;
align-items: center;
gap: 8px;
margin-top: 12px;
color: var(--muted);
font-size: 10px;
font-weight: 700;
}
.status-dot {
width: 7px;
height: 7px;
flex: 0 0 auto;
background: var(--teal);
border-radius: 50%;
box-shadow: 0 0 0 4px rgba(8, 127, 128, 0.1);
}
body[data-route="short"] .status-dot {
background: var(--orange);
box-shadow: 0 0 0 4px rgba(223, 101, 72, 0.11);
}
.insight {
position: fixed;
z-index: 10;
right: 24px;
bottom: 24px;
width: 326px;
padding: 17px 18px 16px;
border-radius: 20px;
}
.insight h2 {
margin: 2px 0 6px;
font-size: 17px;
letter-spacing: -0.025em;
}
.insight p {
min-height: 48px;
margin: 0;
color: var(--muted);
font-size: 11px;
line-height: 1.48;
}
.illustration {
margin-top: 13px;
padding: 9px 10px 5px;
background: rgba(255, 255, 255, 0.62);
border: 1px solid var(--line);
border-radius: 13px;
}
.illustration-label {
display: flex;
justify-content: space-between;
color: var(--muted);
font-size: 8px;
font-weight: 800;
letter-spacing: 0.11em;
text-transform: uppercase;
}
.illustration svg {
display: block;
width: 100%;
height: 80px;
overflow: visible;
}
#schematicShort,
body[data-route="short"] #schematicSlack {
display: none;
}
body[data-route="short"] #schematicShort {
display: inline;
}
.hint {
position: fixed;
z-index: 8;
left: 50%;
bottom: 28px;
padding: 8px 13px;
color: rgba(23, 59, 58, 0.66);
background: rgba(255, 255, 255, 0.58);
border: 1px solid rgba(255, 255, 255, 0.66);
border-radius: 999px;
font-size: 9px;
font-weight: 700;
letter-spacing: 0.02em;
pointer-events: none;
transform: translateX(-50%);
white-space: nowrap;
}
.pin {
position: fixed;
z-index: 7;
padding: 7px 9px;
color: var(--ink);
background: rgba(255, 255, 255, 0.91);
border: 1px solid rgba(23, 59, 58, 0.13);
border-radius: 9px;
box-shadow: 0 7px 20px rgba(23, 59, 58, 0.1);
font-size: 9px;
font-weight: 800;
letter-spacing: 0.02em;
pointer-events: none;
transform: translate(-50%, calc(-100% - 10px));
white-space: nowrap;
}
.pin::after {
position: absolute;
bottom: -7px;
left: 50%;
width: 1px;
height: 7px;
content: "";
background: rgba(23, 59, 58, 0.28);
}
.pin span {
margin-right: 5px;
color: var(--teal);
font-size: 8px;
}
body[data-route="short"] .pin span {
color: var(--orange);
}
#loading {
position: fixed;
z-index: 30;
inset: 0;
display: grid;
place-items: center;
color: var(--muted);
background: #eef1eb;
font-size: 12px;
font-weight: 800;
letter-spacing: 0.12em;
text-transform: uppercase;
}
#loading.hidden {
display: none;
}
@media (max-width: 900px) {
.brand {
width: 300px;
}
.brand p {
display: none;
}
.insight {
width: 286px;
}
.hint {
display: none;
}
}
@media (max-width: 700px) {
.brand {
top: 12px;
left: 12px;
width: calc(100vw - 24px);
padding: 14px 16px;
}
.brand h1 {
font-size: 23px;
}
.brand .eyebrow {
margin-bottom: 5px;
}
.route-panel {
top: 104px;
right: 12px;
width: calc(100vw - 24px);
padding: 9px;
}
.route-panel .panel-label {
display: none;
}
.route-button {
min-height: 54px;
padding: 8px 10px;
}
.route-button strong {
font-size: 11px;
}
.route-button span {
font-size: 9px;
}
.travel-panel {
bottom: 12px;
left: 12px;
width: calc(100vw - 24px);
padding: 13px 14px;
}
.insight {
display: none;
}
.pin {
display: none;
}
}
</style>
</head>
<body data-route="slack">
<canvas id="scene" aria-label="Interactive low-poly scene showing a drawer and flexible cable"></canvas>
<header class="brand glass">
<div class="eyebrow">Kinematic study 01</div>
<h1>Slack, or short route?</h1>
<p>A fixed-length cable links the cabinet to the moving drawer. Compare a generous loop with a route that runs out early.</p>
</header>
<section class="route-panel glass" aria-label="Cable route selection">
<div class="panel-label">Choose a cable route</div>
<div class="route-options">
<button class="route-button active" data-mode="slack" aria-pressed="true">
<strong>Sufficient slack</strong>
<span>3.15u hanging loop</span>
</button>
<button class="route-button" data-mode="short" aria-pressed="false">
<strong>Short route</strong>
<span>1.55u limited path</span>
</button>
</div>
</section>
<section class="travel-panel glass" aria-label="Drawer controls">
<div class="travel-heading">
<label for="travel">Drawer travel</label>
<output id="travelValue" for="travel">0%</output>
</div>
<div class="range-wrap">
<input id="travel" type="range" min="0" max="100" step="1" value="0" />
<div class="range-labels">
<span>Closed</span>
<span>Fully open</span>
</div>
</div>
<div class="travel-actions">
<button id="replay" class="action-button primary">Replay travel</button>
<button id="reset" class="action-button">Reset scene</button>
</div>
<div class="status" aria-live="polite">
<span class="status-dot"></span>
<span id="statusText">Ready to travel</span>
</div>
</section>
<aside class="insight glass">
<div class="eyebrow" id="insightEyebrow">Illustrated route</div>
<h2 id="insightTitle">The loop pays out the difference.</h2>
<p id="insightText">
With 3.15 model units of cable, the hanging loop absorbs the motion and the drawer reaches 100% travel.
</p>
<div class="illustration">
<div class="illustration-label">
<span>Fixed anchor</span>
<span id="diagramResult">100% available</span>
</div>
<svg viewBox="0 0 300 90" role="img" aria-label="Schematic of the cable route">
<path d="M18 64 H282" fill="none" stroke="#cbd5d0" stroke-width="4" stroke-linecap="round"/>
<path d="M18 58 V70 M282 58 V70" fill="none" stroke="#8ca09a" stroke-width="2" stroke-linecap="round"/>
<g id="schematicSlack">
<path d="M28 20 C4 79 75 87 122 48 S195 7 244 29"
fill="none" stroke="#087f80" stroke-width="4" stroke-linecap="round"/>
<circle cx="28" cy="20" r="5" fill="#173b3a"/>
<circle cx="244" cy="29" r="5" fill="#087f80"/>
</g>
<g id="schematicShort">
<path d="M28 20 C63 69 119 68 180 36"
fill="none" stroke="#df6548" stroke-width="4" stroke-linecap="round"/>
<circle cx="28" cy="20" r="5" fill="#173b3a"/>
<circle cx="180" cy="36" r="5" fill="#df6548"/>
<path d="M180 27 V69" fill="none" stroke="#df6548" stroke-width="2" stroke-dasharray="4 4"/>
<path d="M171 32 L180 22 L189 32" fill="none" stroke="#df6548" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>
</g>
<rect x="217" y="38" width="55" height="19" rx="4" fill="#f7f8f4" stroke="#8ca09a" stroke-width="2"/>
<path d="M225 33 H264" stroke="#173b3a" stroke-width="3" stroke-linecap="round"/>
</svg>
</div>
</aside>
<div class="hint">Drag the handle to slide · drag the scene to orbit · scroll to zoom</div>
<div id="pinAnchor" class="pin"><span>01</span>Fixed anchor</div>
<div id="pinMoving" class="pin"><span>02</span>Moving plug</div>
<div id="pinCable" class="pin"><span>↘</span><span id="pinCableText">Slack loop</span></div>
<div id="loading">Building the cable study…</div>
<script type="module">
import * as THREE from "https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
const canvas = document.querySelector("#scene");
const loading = document.querySelector("#loading");
const renderer = new THREE.WebGLRenderer({
canvas,
antialias: true,
alpha: false,
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.05;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xeef1eb);
scene.fog = new THREE.Fog(0xeef1eb, 13, 25);
const camera = new THREE.PerspectiveCamera(
38,
window.innerWidth / window.innerHeight,
0.1,
50
);
const cameraState = {
yaw: 0.66,
pitch: 0.39,
radius: 9.4,
target: new THREE.Vector3(2.05, 1.22, 1.12)
};
function updateCamera() {
const narrow = camera.aspect < 0.78 ? 1.15 : 1;
const radius = cameraState.radius * narrow;
const { yaw, pitch, target } = cameraState;
camera.position.set(
target.x + radius * Math.sin(yaw) * Math.cos(pitch),
target.y + radius * Math.sin(pitch),
target.z + radius * Math.cos(yaw) * Math.cos(pitch)
);
camera.lookAt(target);
camera.updateMatrixWorld();
}
const hemisphere = new THREE.HemisphereLight(0xffffff, 0x80938a, 2.25);
scene.add(hemisphere);
const sun = new THREE.DirectionalLight(0xfff5df, 3.2);
sun.position.set(4.5, 9, 6);
sun.castShadow = true;
sun.shadow.mapSize.set(2048, 2048);
sun.shadow.camera.left = -8;
sun.shadow.camera.right = 8;
sun.shadow.camera.top = 8;
sun.shadow.camera.bottom = -8;
sun.shadow.camera.near = 0.5;
sun.shadow.camera.far = 22;
sun.shadow.bias = -0.0006;
scene.add(sun);
const fill = new THREE.DirectionalLight(0xb8e0df, 1.1);
fill.position.set(-5, 4, -3);
scene.add(fill);
function standard(color, options = {}) {
return new THREE.MeshStandardMaterial({
color,
roughness: 0.78,
metalness: 0.02,
flatShading: true,
...options
});
}
const materials = {
floor: standard(0xe4e8df),
cabinet: standard(0x91a99f),
cabinetDark: standard(0x617d75),
wood: standard(0xd7b78d),
drawer: standard(0xf4f0e5),
inside: standard(0xc7b89e),
dark: standard(0x244d49),
teal: standard(0x087f80, { roughness: 0.48 }),
orange: standard(0xdf6548, { roughness: 0.5 }),
cream: standard(0xfff8e8),
metal: standard(0x315b58, { roughness: 0.42, metalness: 0.18 }),
paper: standard(0xfaf5e8),
coral: standard(0xdc795d)
};
function box(width, height, depth, material, x, y, z, parent = scene) {
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;
}
// Ground and subtle grid.
const ground = new THREE.Mesh(
new THREE.PlaneGeometry(40, 40),
materials.floor
);
ground.rotation.x = -Math.PI / 2;
ground.position.y = -0.025;
ground.receiveShadow = true;
scene.add(ground);
const grid = new THREE.GridHelper(24, 32, 0xb6c4bc, 0xcbd4cc);
grid.position.y = -0.018;
grid.material.transparent = true;
grid.material.opacity = 0.18;
scene.add(grid);
// Desk and cabinet.
box(4.35, 0.18, 2.9, materials.wood, 2.5, 2.68, 0.05);
box(0.16, 2.5, 2.4, materials.cabinet, 0.65, 1.34, 0);
box(0.16, 2.5, 2.4, materials.cabinet, 4.35, 1.34, 0);
box(3.55, 2.35, 0.14, materials.cabinetDark, 2.5, 1.3, -1.12);
box(3.55, 0.16, 2.22, materials.cabinetDark, 2.5, 0.27, -0.02);
box(3.58, 1.9, 0.08, materials.dark, 2.5, 1.38, -0.95);
// Small desk details.
box(0.78, 0.06, 0.55, materials.paper, 3.42, 2.8, -0.52).rotation.y = -0.11;
box(0.72, 0.04, 0.49, materials.cream, 3.46, 2.85, -0.53).rotation.y = -0.05;
const cup = new THREE.Mesh(
new THREE.CylinderGeometry(0.24, 0.19, 0.48, 8),
materials.coral
);
cup.position.set(1.05, 3.02, -0.65);
cup.castShadow = true;
scene.add(cup);
for (let i = 0; i < 3; i++) {
const pencil = new THREE.Mesh(
new THREE.CylinderGeometry(0.025, 0.025, 0.7, 6),
i === 1 ? materials.teal : materials.wood
);
pencil.position.set(0.96 + i * 0.09, 3.25, -0.65);
pencil.rotation.z = (i - 1) * 0.12;
pencil.castShadow = true;
scene.add(pencil);
}
// Moving drawer.
const drawer = new THREE.Group();
drawer.position.z = 0;
scene.add(drawer);
box(3.38, 1.47, 0.16, materials.drawer, 2.5, 1.35, 0.03, drawer);
box(3.18, 0.12, 1.67, materials.inside, 2.5, 0.64, -0.8, drawer);
box(0.12, 0.66, 1.67, materials.inside, 0.97, 0.98, -0.8, drawer);
box(0.12, 0.66, 1.67, materials.inside, 4.03, 0.98, -0.8, drawer);
box(3.18, 0.66, 0.12, materials.inside, 2.5, 0.98, -1.58, drawer);
const handleGroup = new THREE.Group();
drawer.add(handleGroup);
const handleBar = new THREE.Mesh(
new THREE.CylinderGeometry(0.055, 0.055, 1.18, 8),
materials.teal
);
handleBar.rotation.z = Math.PI / 2;
handleBar.position.set(2.5, 1.38, 0.22);
handleBar.castShadow = true;
handleGroup.add(handleBar);
const handlePostGeometry = new THREE.CylinderGeometry(0.052, 0.052, 0.2, 8);
for (const x of [1.97, 3.03]) {
const post = new THREE.Mesh(handlePostGeometry, materials.teal);
post.rotation.x = Math.PI / 2;
post.position.set(x, 1.38, 0.13);
post.castShadow = true;
handleGroup.add(post);
}
// Cable terminal on the drawer.
const plug = box(0.42, 0.18, 0.18, materials.dark, 4.35, 1.3, -0.18, drawer);
const connectorCap = new THREE.Mesh(
new THREE.CylinderGeometry(0.075, 0.075, 0.12, 8),
materials.teal
);
connectorCap.rotation.z = Math.PI / 2;
connectorCap.position.set(4.56, 1.3, -0.18);
connectorCap.castShadow = true;
drawer.add(connectorCap);
// Fixed cable terminal.
const anchorBracket = box(0.16, 0.3, 0.28, materials.dark, 4.47, 1.3, 0.55);
const anchorCap = new THREE.Mesh(
new THREE.CylinderGeometry(0.075, 0.075, 0.12, 8),
materials.teal
);
anchorCap.rotation.z = Math.PI / 2;
anchorCap.position.set(4.56, 1.3, 0.55);
anchorCap.castShadow = true;
scene.add(anchorCap);
const anchorPosition = new THREE.Vector3(4.56, 1.3, 0.55);
const connectorLocal = new THREE.Vector3(4.56, 1.3, -0.18);
// A fixed-length circular arc is used as an inextensible cable.
class CableArc extends THREE.Curve {
constructor(a, b, length) {
super();
this.a = a.clone();
this.b = b.clone();
this.length = length;
const chord = this.b.clone().sub(this.a);
const distance = chord.length();
const ratio = Math.max(1, length / distance);
let theta;
if (ratio <= 1.000001) {
theta = Math.PI;
} else {
let low = 0.00001;
let high = Math.PI * 2 - 0.00001;
for (let i = 0; i < 54; i++) {
const mid = (low + high) * 0.5;
const arcRatio = mid / (2 * Math.sin(mid * 0.5));
if (arcRatio < ratio) low = mid;
else high = mid;
}
theta = (low + high) * 0.5;
}
this.e = chord.normalize();
this.p = new THREE.Vector3(0, -1, 0);
this.p.addScaledVector(this.e, -this.p.dot(this.e)).normalize();
this.theta = theta;
this.radius = length / theta;
this.h = this.radius * Math.cos(theta * 0.5);
this.center = this.a.clone().add(this.b).multiplyScalar(0.5);
this.center.addScaledVector(this.p, this.h);
this.angleA = Math.atan2(-this.h, -distance * 0.5);
}
getPoint(t, target = new THREE.Vector3()) {
const angle = this.angleA + this.theta * t;
const radial = this.e.clone().multiplyScalar(Math.cos(angle));
radial.addScaledVector(this.p, Math.sin(angle));
return target.copy(this.center).addScaledVector(radial, this.radius);
}
}
const cableMaterial = standard(0x087f80, {
roughness: 0.42,
metalness: 0.02,
emissive: 0x043f40,
emissiveIntensity: 0.12
});
let cableMesh = null;
let currentCableCurve = null;
const stopMarker = new THREE.Mesh(
new THREE.TorusGeometry(0.14, 0.035, 6, 14),
standard(0xdf6548, {
roughness: 0.4,
emissive: 0x7d1f0c,
emissiveIntensity: 0.18
})
);
stopMarker.rotation.y = Math.PI / 2;
stopMarker.position.set(4.56, 1.3, 2.12);
stopMarker.visible = false;
scene.add(stopMarker);
const FULL_TRAVEL = 1.4;
const ANCHOR_Z = 0.55;
const CONNECTOR_Z = 1.1;
const SHORT_CABLE_LENGTH = 1.55;
const SHORT_TRAVEL = SHORT_CABLE_LENGTH - (CONNECTOR_Z - ANCHOR_Z);
const SHORT_PERCENT = Math.round(100 * SHORT_TRAVEL / FULL_TRAVEL);
const routes = {
slack: {
name: "Sufficient slack",
cableLength: 3.15,
maxTravel: FULL_TRAVEL,
color: 0x087f80
},
short: {
name: "Short route",
cableLength: SHORT_CABLE_LENGTH,
maxTravel: SHORT_TRAVEL,
color: 0xdf6548
}
};
let routeMode = "slack";
let openAmount = 0;
let desiredOpen = 0;
let playing = false;
let playElapsed = 0;
let stopFlash = 0;
const travelInput = document.querySelector("#travel");
const travelValue = document.querySelector("#travelValue");
const statusText = document.querySelector("#statusText");
const routeButtons = [...document.querySelectorAll(".route-button")];
const pinAnchor = document.querySelector("#pinAnchor");
const pinMoving = document.querySelector("#pinMoving");
const pinCable = document.querySelector("#pinCable");
const pinCableText = document.querySelector("#pinCableText");
function setRoute(mode, shouldReplay = true) {
routeMode = mode;
document.body.dataset.route = mode;
routeButtons.forEach((button) => {
const active = button.dataset.mode === mode;
button.classList.toggle("active", active);
button.setAttribute("aria-pressed", String(active));
});
cableMaterial.color.setHex(routes[mode].color);
cableMaterial.emissive.setHex(mode === "short" ? 0x6d1c0b : 0x043f40);
stopMarker.visible = mode === "short";
stopMarker.position.z = CONNECTOR_Z + routes.short.maxTravel;
pinCableText.textContent = mode === "short" ? "Short route" : "Slack loop";
if (mode === "short") {
document.querySelector("#insightTitle").textContent = "The short route runs out.";
document.querySelector("#insightText").textContent =
`Only 1.55 model units of cable are available. The plug reaches its stop at ${SHORT_PERCENT}% travel, before the drawer is fully open.`;
document.querySelector("#diagramResult").textContent = `${SHORT_PERCENT}% limit`;
} else {
document.querySelector("#insightTitle").textContent = "The loop pays out the difference.";
document.querySelector("#insightText").textContent =
"With 3.15 model units of cable, the hanging loop absorbs the motion and the drawer reaches 100% travel.";
document.querySelector("#diagramResult").textContent = "100% available";
}
desiredOpen = Math.min(desiredOpen, routes[mode].maxTravel);
if (shouldReplay) startDemo();
}
function startDemo() {
openAmount = 0;
desiredOpen = 0;
playing = true;
playElapsed = 0;
drawer.position.z = 0;
}
routeButtons.forEach((button) => {
button.addEventListener("click", () => setRoute(button.dataset.mode, true));
});
travelInput.addEventListener("input", () => {
playing = false;
desiredOpen = (Number(travelInput.value) / 100) * FULL_TRAVEL;
});
document.querySelector("#replay").addEventListener("click", startDemo);
document.querySelector("#reset").addEventListener("click", () => {
playing = false;
openAmount = 0;
desiredOpen = 0;
drawer.position.z = 0;
cameraState.yaw = 0.66;
cameraState.pitch = 0.39;
cameraState.radius = 9.4;
updateCamera();
});
// Custom camera controls: pointer orbit and wheel zoom.
const raycaster = new THREE.Raycaster();
const pointer = new THREE.Vector2();
const dragPlane = new THREE.Plane(new THREE.Vector3(0, 1, 0), -1.38);
const dragPoint = new THREE.Vector3();
let draggingDrawer = false;
let orbiting = false;
let dragOffsetZ = 0;
let lastPointerX = 0;
let lastPointerY = 0;
function setPointerFromEvent(event) {
const rect = canvas.getBoundingClientRect();
pointer.x = ((event.clientX - rect.left) / rect.width) * 2 - 1;
pointer.y = -((event.clientY - rect.top) / rect.height) * 2 + 1;
raycaster.setFromCamera(pointer, camera);
}
canvas.addEventListener("pointerdown", (event) => {
if (event.button !== 0) return;
setPointerFromEvent(event);
const handleHits = raycaster.intersectObjects(handleGroup.children, true);
canvas.setPointerCapture(event.pointerId);
lastPointerX = event.clientX;
lastPointerY = event.clientY;
if (handleHits.length && raycaster.ray.intersectPlane(dragPlane, dragPoint)) {
draggingDrawer = true;
dragOffsetZ = drawer.position.z - dragPoint.z;
canvas.style.cursor = "ns-resize";
} else {
orbiting = true;
canvas.style.cursor = "grabbing";
}
});
canvas.addEventListener("pointermove", (event) => {
if (draggingDrawer) {
setPointerFromEvent(event);
if (raycaster.ray.intersectPlane(dragPlane, dragPoint)) {
const max = routes[routeMode].maxTravel;
const next = THREE.MathUtils.clamp(dragPoint.z + dragOffsetZ, 0, max);
if (routeMode === "short" && next > max - 0.005) stopFlash = 1;
openAmount = next;
desiredOpen = next;
playing = false;
}
} else if (orbiting) {
const dx = event.clientX - lastPointerX;
const dy = event.clientY - lastPointerY;
cameraState.yaw -= dx * 0.005;
cameraState.pitch = THREE.MathUtils.clamp(
cameraState.pitch + dy * 0.0045,
0.12,
1.05
);
lastPointerX = event.clientX;
lastPointerY = event.clientY;
updateCamera();
}
});
function endPointer(event) {
draggingDrawer = false;
orbiting = false;
canvas.style.cursor = "grab";
if (canvas.hasPointerCapture(event.pointerId)) {
canvas.releasePointerCapture(event.pointerId);
}
}
canvas.addEventListener("pointerup", endPointer);
canvas.addEventListener("pointercancel", endPointer);
canvas.addEventListener(
"wheel",
(event) => {
event.preventDefault();
cameraState.radius = THREE.MathUtils.clamp(
cameraState.radius + event.deltaY * 0.006,
6.2,
13.5
);
updateCamera();
},
{ passive: false }
);
function updateCable() {
const connectorWorld = connectorLocal.clone();
connectorWorld.z += drawer.position.z;
currentCableCurve = new CableArc(
anchorPosition,
connectorWorld,
routes[routeMode].cableLength
);
const geometry = new THREE.TubeGeometry(
currentCableCurve,
72,
0.048,
6,
false
);
if (!cableMesh) {
cableMesh = new THREE.Mesh(geometry, cableMaterial);
cableMesh.castShadow = true;
cableMesh.receiveShadow = true;
scene.add(cableMesh);
} else {
cableMesh.geometry.dispose();
cableMesh.geometry = geometry;
}
}
function positionPin(element, worldPosition) {
const projected = worldPosition.clone().project(camera);
const visible = projected.z > -1 && projected.z < 1;
element.style.display = visible ? "block" : "none";
if (!visible) return;
element.style.left = `${(projected.x * 0.5 + 0.5) * window.innerWidth}px`;
element.style.top = `${(-projected.y * 0.5 + 0.5) * window.innerHeight}px`;
}
function updateInterface() {
const route = routes[routeMode];
const percent = THREE.MathUtils.clamp((openAmount / FULL_TRAVEL) * 100, 0, 100);
const targetPercent = THREE.MathUtils.clamp(
(Math.min(desiredOpen, route.maxTravel) / FULL_TRAVEL) * 100,
0,
100
);
travelValue.textContent = `${Math.round(percent)}%`;
travelInput.value = String(Math.round(targetPercent));
travelInput.style.setProperty("--progress", `${targetPercent}%`);
const atLimit =
routeMode === "short" && openAmount >= route.maxTravel - 0.015;
if (atLimit) {
statusText.textContent = playing
? "Holding at the cable limit"
: `Stopped at ${SHORT_PERCENT}% — cable is taut`;
} else if (playing) {
statusText.textContent = "Opening drawer…";
} else if (openAmount < 0.01) {
statusText.textContent = "Ready to travel";
} else if (routeMode === "slack") {
statusText.textContent = "Full travel available";
} else {
statusText.textContent = "Short route in motion";
}
}
function resize() {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
updateCamera();
}
window.addEventListener("resize", resize);
resize();
setRoute("slack", false);
updateCable();
const clock = new THREE.Clock();
function animate() {
requestAnimationFrame(animate);
const dt = Math.min(clock.getDelta(), 0.05);
const route = routes[routeMode];
if (playing) {
playElapsed += dt;
desiredOpen = FULL_TRAVEL * Math.min(playElapsed / 4.2, 1);
if (playElapsed >= 4.2) playing = false;
}
const targetOpen = THREE.MathUtils.clamp(
desiredOpen,
0,
route.maxTravel
);
if (!draggingDrawer) {
const smoothing = 1 - Math.exp(-dt * 9);
openAmount += (targetOpen - openAmount) * smoothing;
if (Math.abs(targetOpen - openAmount) < 0.0004) {
openAmount = targetOpen;
}
}
drawer.position.z = openAmount;
updateCable();
stopFlash = Math.max(0, stopFlash - dt * 1.8);
const pulse = 1 + Math.sin(performance.now() * 0.004) * 0.08 + stopFlash * 0.3;
stopMarker.scale.setScalar(pulse);
updateInterface();
const connectorWorld = connectorLocal.clone();
connectorWorld.z += drawer.position.z;
const cableMidpoint = currentCableCurve.getPoint(0.5);
positionPin(pinAnchor, anchorPosition);
positionPin(pinMoving, connectorWorld);
positionPin(pinCable, cableMidpoint);
renderer.render(scene, camera);
}
animate();
loading.classList.add("hidden");
// Run the sufficient-slack demonstration once unless reduced motion is requested.
if (!window.matchMedia("(prefers-reduced-motion: reduce)").matches) {
window.setTimeout(startDemo, 650);
}
</script>
</body>
</html>
```