Prompt specification
Build or customize the Three.js 3D scene "Hand Winch Teaching Workbench" 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: Hand Winch Teaching Workbench
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/hand-winch-teaching-workbench-x2055
Scene Description Prompt:
Show a schematic voxel hand winch with drum, rope, and ratchet, distinguishing lifting from holding. One HTML file with Three.js from cdnjs.
Working Three.js Starter (hand-winch-teaching-workbench-x2055):
```html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Voxel Hand Winch</title>
<style>
:root {
--panel: rgba(11, 25, 33, 0.82);
--line: rgba(181, 223, 229, 0.18);
--text: #eaf7f7;
--muted: #91abb2;
--lift: #63e6a7;
--hold: #ffb45e;
--force: #ff6f73;
}
* { box-sizing: border-box; }
html, body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background:
radial-gradient(circle at 50% 42%, #19333d 0%, #0b1921 58%, #071117 100%);
color: var(--text);
font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
#stage {
position: fixed;
inset: 0;
}
canvas {
display: block;
width: 100%;
height: 100%;
touch-action: none;
cursor: grab;
}
canvas:active { cursor: grabbing; }
#hud {
position: fixed;
inset: 0;
pointer-events: none;
}
.heading {
position: absolute;
top: 28px;
left: 30px;
max-width: 330px;
text-shadow: 0 2px 18px rgba(0, 0, 0, 0.45);
}
.eyebrow {
margin-bottom: 8px;
color: #70d9cc;
font-size: 11px;
font-weight: 800;
letter-spacing: 0.18em;
text-transform: uppercase;
}
h1 {
margin: 0;
font-size: clamp(27px, 3vw, 42px);
line-height: 1;
letter-spacing: -0.045em;
}
.heading p {
margin: 12px 0 0;
color: var(--muted);
font-size: 13px;
line-height: 1.5;
}
.panel {
position: absolute;
top: 26px;
right: 26px;
width: 310px;
padding: 16px;
border: 1px solid var(--line);
border-radius: 14px;
background: var(--panel);
box-shadow: 0 18px 55px rgba(0, 0, 0, 0.28);
backdrop-filter: blur(12px);
pointer-events: auto;
}
.controls {
display: grid;
grid-template-columns: 1fr 1fr 0.76fr;
gap: 7px;
}
button {
appearance: none;
border: 1px solid rgba(190, 225, 229, 0.18);
border-radius: 9px;
padding: 10px 8px;
color: #bad0d4;
background: rgba(255, 255, 255, 0.045);
font: inherit;
font-size: 11px;
font-weight: 800;
letter-spacing: 0.08em;
cursor: pointer;
transition: 160ms ease;
}
button:hover {
color: white;
border-color: rgba(190, 225, 229, 0.4);
transform: translateY(-1px);
}
button:focus-visible {
outline: 2px solid #70d9cc;
outline-offset: 2px;
}
button.active {
color: #09201c;
border-color: transparent;
background: var(--lift);
}
button[data-mode="hold"].active {
color: #291506;
background: var(--hold);
}
.status {
display: grid;
grid-template-columns: 12px 1fr;
gap: 11px;
align-items: start;
margin-top: 17px;
padding-top: 15px;
border-top: 1px solid var(--line);
}
.status-dot {
width: 10px;
height: 10px;
margin-top: 4px;
border-radius: 2px;
background: var(--lift);
box-shadow: 0 0 16px rgba(99, 230, 167, 0.8);
}
#state-title {
display: block;
margin-bottom: 4px;
font-size: 15px;
letter-spacing: 0.05em;
}
#state-copy {
display: block;
min-height: 36px;
color: var(--muted);
font-size: 12px;
line-height: 1.45;
}
.meter-label {
display: flex;
justify-content: space-between;
margin-top: 14px;
color: var(--muted);
font-size: 9px;
font-weight: 800;
letter-spacing: 0.12em;
text-transform: uppercase;
}
.meter {
height: 5px;
margin-top: 7px;
overflow: hidden;
border-radius: 4px;
background: rgba(255, 255, 255, 0.08);
}
#meter-fill {
width: 0;
height: 100%;
border-radius: inherit;
background: linear-gradient(90deg, #52cdbf, #63e6a7);
transition: width 80ms linear;
}
.legend {
position: absolute;
right: 28px;
bottom: 25px;
display: flex;
gap: 18px;
padding: 10px 13px;
border: 1px solid var(--line);
border-radius: 9px;
color: var(--muted);
background: rgba(8, 20, 27, 0.68);
font-size: 10px;
font-weight: 700;
letter-spacing: 0.05em;
backdrop-filter: blur(8px);
}
.legend span {
display: flex;
align-items: center;
gap: 7px;
}
.legend i {
width: 8px;
height: 8px;
border-radius: 2px;
background: var(--lift);
}
.legend i.hold { background: var(--hold); }
.legend i.force { background: var(--force); }
.world-label {
position: absolute;
z-index: 3;
min-width: 84px;
padding: 7px 9px;
border: 1px solid rgba(177, 221, 225, 0.22);
border-radius: 7px;
color: #e5f5f5;
background: rgba(7, 20, 27, 0.78);
box-shadow: 0 8px 20px rgba(0, 0, 0, 0.2);
opacity: 0;
transform: translate(-50%, -50%);
white-space: nowrap;
backdrop-filter: blur(5px);
}
.world-label::before {
content: "";
position: absolute;
left: -4px;
top: 50%;
width: 6px;
height: 6px;
border-radius: 1px;
background: #72d9cd;
transform: translateY(-50%);
}
.world-label b {
display: block;
font-size: 9px;
letter-spacing: 0.13em;
}
.world-label span {
display: block;
margin-top: 3px;
color: #91abb2;
font-size: 9px;
}
.world-label.dynamic {
padding: 6px 8px;
border-color: rgba(99, 230, 167, 0.5);
color: var(--lift);
font-size: 9px;
font-weight: 900;
letter-spacing: 0.1em;
}
.world-label.dynamic.force {
border-color: rgba(255, 111, 115, 0.55);
color: var(--force);
}
@media (max-width: 760px) {
.heading {
top: 18px;
left: 18px;
}
.heading p { display: none; }
.panel {
top: auto;
right: 14px;
bottom: 14px;
left: 14px;
width: auto;
padding: 12px;
}
.status {
margin-top: 11px;
padding-top: 10px;
}
#state-copy { min-height: 0; }
.legend { display: none; }
.world-label:not(.dynamic) { display: none; }
}
</style>
</head>
<body>
<div id="stage"></div>
<div id="hud">
<header class="heading">
<div class="eyebrow">Mechanism study / 01</div>
<h1>Voxel Hand Winch</h1>
<p>Drag to orbit · Scroll to zoom<br>Lift the load, then engage the ratchet.</p>
</header>
<section class="panel" aria-label="Winch controls">
<div class="controls">
<button class="active" data-mode="lift" aria-pressed="true">LIFT</button>
<button data-mode="hold" aria-pressed="false">HOLD</button>
<button id="reset" title="Reset the demonstration">RESET</button>
</div>
<div class="status" aria-live="polite">
<div class="status-dot" id="status-dot"></div>
<div>
<strong id="state-title">LIFTING</strong>
<span id="state-copy">
The crank drives the drum. Rope is taken up while the pawl rides over the teeth.
</span>
</div>
</div>
<div class="meter-label">
<span>Load height</span>
<span id="height-value">0%</span>
</div>
<div class="meter"><div id="meter-fill"></div></div>
</section>
<div class="legend">
<span><i></i>Take-up motion</span>
<span><i class="hold"></i>Pawl locked</span>
<span><i class="force"></i>Load force</span>
</div>
<div class="world-label" id="label-drum">
<b>DRUM</b><span>rope storage</span>
</div>
<div class="world-label" id="label-rope">
<b>ROPE</b><span>vertical run</span>
</div>
<div class="world-label" id="label-ratchet">
<b>RATCHET</b><span>one-way lock</span>
</div>
<div class="world-label dynamic" id="label-takeup">TAKE-UP ↑</div>
<div class="world-label dynamic force" id="label-force">LOAD FORCE ↓</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 stage = document.querySelector("#stage");
const renderer = new THREE.WebGLRenderer({
antialias: true,
alpha: true
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.setClearColor(0x000000, 0);
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.domElement.setAttribute(
"aria-label",
"Interactive schematic of a voxel hand winch"
);
stage.appendChild(renderer.domElement);
const scene = new THREE.Scene();
const camera = new THREE.OrthographicCamera(-6, 6, 3.5, -3.5, 0.1, 100);
const cameraTarget = new THREE.Vector3(0, 2.05, 0);
const orbit = {
theta: 0.58,
phi: 1.04,
zoom: 1.06
};
const hemi = new THREE.HemisphereLight(0xc9f3ff, 0x17252c, 2.4);
scene.add(hemi);
const key = new THREE.DirectionalLight(0xfff4d4, 4.1);
key.position.set(5, 9, 7);
key.castShadow = true;
key.shadow.mapSize.set(2048, 2048);
key.shadow.camera.left = -8;
key.shadow.camera.right = 8;
key.shadow.camera.top = 9;
key.shadow.camera.bottom = -3;
key.shadow.camera.near = 0.5;
key.shadow.camera.far = 25;
key.shadow.bias = -0.0005;
scene.add(key);
const rim = new THREE.DirectionalLight(0x58d8d0, 2.2);
rim.position.set(-6, 4, -5);
scene.add(rim);
const warm = new THREE.PointLight(0xffa85c, 16, 9, 2);
warm.position.set(4, 3.2, 2.8);
scene.add(warm);
const materials = {
frame: new THREE.MeshStandardMaterial({
color: 0xf1b84b, roughness: 0.72, metalness: 0.18
}),
frameDark: new THREE.MeshStandardMaterial({
color: 0x9d6a20, roughness: 0.78, metalness: 0.22
}),
base: new THREE.MeshStandardMaterial({
color: 0x18303a, roughness: 0.82, metalness: 0.35
}),
drum: new THREE.MeshStandardMaterial({
color: 0x46bdb6, roughness: 0.42, metalness: 0.62
}),
axle: new THREE.MeshStandardMaterial({
color: 0x8ba3aa, roughness: 0.32, metalness: 0.8
}),
dark: new THREE.MeshStandardMaterial({
color: 0x10242d, roughness: 0.7, metalness: 0.4
}),
rope: new THREE.MeshStandardMaterial({
color: 0xd49a51, roughness: 0.94, metalness: 0
}),
ratchet: new THREE.MeshStandardMaterial({
color: 0xff705f, roughness: 0.48, metalness: 0.32
}),
grip: new THREE.MeshStandardMaterial({
color: 0x15272e, roughness: 0.9, metalness: 0.08
}),
weight: new THREE.MeshStandardMaterial({
color: 0x58717a, roughness: 0.65, metalness: 0.55
}),
lift: new THREE.MeshStandardMaterial({
color: 0x63e6a7,
emissive: 0x1b8f5e,
emissiveIntensity: 0.45,
roughness: 0.45
}),
force: new THREE.MeshStandardMaterial({
color: 0xff6f73,
emissive: 0x9b252b,
emissiveIntensity: 0.45,
roughness: 0.45
}),
pawl: new THREE.MeshStandardMaterial({
color: 0xffbd55,
emissive: 0x4c2800,
emissiveIntensity: 0.25,
roughness: 0.45,
metalness: 0.3
})
};
const edgeMaterial = new THREE.LineBasicMaterial({
color: 0x07151b,
transparent: true,
opacity: 0.52
});
function addBox(parent, size, position, material, outlined = true) {
const geometry = new THREE.BoxGeometry(...size);
const mesh = new THREE.Mesh(geometry, material);
mesh.position.set(...position);
mesh.castShadow = true;
mesh.receiveShadow = true;
parent.add(mesh);
if (outlined) {
const edges = new THREE.LineSegments(
new THREE.EdgesGeometry(geometry),
edgeMaterial
);
mesh.add(edges);
}
return mesh;
}
// A block-built cylinder whose axis runs along local X.
function voxelCylinderX(radius, length, material, parent, cell = 0.145) {
const nx = Math.max(1, Math.round(length / cell));
const ny = Math.max(1, Math.round((radius * 2) / cell));
const nz = ny;
const dx = length / nx;
const dy = (radius * 2) / ny;
const dz = (radius * 2) / nz;
const cells = [];
for (let ix = 0; ix < nx; ix++) {
for (let iy = 0; iy < ny; iy++) {
for (let iz = 0; iz < nz; iz++) {
const y = -radius + (iy + 0.5) * dy;
const z = -radius + (iz + 0.5) * dz;
if (y * y + z * z <= radius * radius) {
cells.push([
-length / 2 + (ix + 0.5) * dx,
y,
z
]);
}
}
}
}
const geometry = new THREE.BoxGeometry(dx * 0.91, dy * 0.91, dz * 0.91);
const mesh = new THREE.InstancedMesh(geometry, material, cells.length);
const matrix = new THREE.Matrix4();
cells.forEach((cellPosition, index) => {
matrix.makeTranslation(...cellPosition);
mesh.setMatrixAt(index, matrix);
});
mesh.instanceMatrix.needsUpdate = true;
mesh.castShadow = true;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
// Ground and schematic grid.
const floor = new THREE.Mesh(
new THREE.PlaneGeometry(40, 40),
new THREE.MeshStandardMaterial({
color: 0x0b1b23,
roughness: 1,
transparent: true,
opacity: 0.88
})
);
floor.rotation.x = -Math.PI / 2;
floor.position.y = -0.04;
floor.receiveShadow = true;
scene.add(floor);
const grid = new THREE.GridHelper(24, 48, 0x31515b, 0x18313a);
grid.position.y = -0.025;
grid.material.transparent = true;
grid.material.opacity = 0.42;
scene.add(grid);
// Winch base.
addBox(scene, [5.25, 0.22, 2.45], [0, 0.15, 0], materials.base);
addBox(scene, [4.55, 0.10, 0.12], [0, 0.31, -0.98], materials.frameDark);
addBox(scene, [4.55, 0.10, 0.12], [0, 0.31, 0.98], materials.frameDark);
for (const x of [-2.42, 2.42]) {
for (const z of [-1.02, 1.02]) {
addBox(scene, [0.45, 0.22, 0.45], [x, 0.13, z], materials.dark);
}
}
// Twin bearing frames.
for (const x of [-1.68, 1.68]) {
for (const z of [-0.82, 0.82]) {
addBox(scene, [0.66, 0.22, 0.66], [x, 0.39, z], materials.frame);
addBox(scene, [0.31, 2.00, 0.31], [x, 1.42, z], materials.frame);
addBox(scene, [0.16, 0.16, 0.16], [x, 2.16, z + 0.18], materials.axle);
}
addBox(scene, [0.34, 0.29, 1.96], [x, 2.55, 0], materials.frame);
addBox(scene, [0.43, 0.49, 0.49], [x, 2.55, 0], materials.dark);
addBox(scene, [0.48, 0.15, 0.54], [x, 2.55, 0], materials.axle);
}
// Axle.
addBox(
scene,
[4.38, 0.17, 0.17],
[0.15, 2.55, 0],
materials.axle
);
// Rotating drum and voxel rope wraps.
const drum = new THREE.Group();
drum.position.set(0, 2.55, 0);
scene.add(drum);
voxelCylinderX(0.58, 2.05, materials.drum, drum);
voxelCylinderX(0.76, 0.14, materials.axle, drum).position.x = -1.12;
voxelCylinderX(0.76, 0.14, materials.axle, drum).position.x = 1.12;
const coilGeometry = new THREE.BoxGeometry(0.19, 0.115, 0.15);
for (const x of [-0.72, -0.36, 0, 0.36, 0.72]) {
for (let i = 0; i < 18; i++) {
const angle = (i / 18) * Math.PI * 2;
const radius = 0.665;
const segment = new THREE.Mesh(coilGeometry, materials.rope);
segment.position.set(
x,
Math.cos(angle) * radius,
Math.sin(angle) * radius
);
segment.rotation.x = angle;
segment.castShadow = true;
drum.add(segment);
}
}
// Ratchet wheel.
const ratchet = new THREE.Group();
ratchet.position.set(1.92, 2.55, 0);
scene.add(ratchet);
voxelCylinderX(0.57, 0.16, materials.ratchet, ratchet);
for (let i = 0; i < 14; i++) {
const angle = (i / 14) * Math.PI * 2;
const tooth = addBox(
ratchet,
[0.20, 0.29, 0.18],
[0, Math.cos(angle) * 0.69, Math.sin(angle) * 0.69],
materials.ratchet
);
tooth.rotation.x = angle;
}
addBox(ratchet, [0.25, 0.30, 0.30], [0, 0, 0], materials.axle);
// Crank and hand grip.
const crank = new THREE.Group();
crank.position.set(2.27, 2.55, 0);
scene.add(crank);
addBox(crank, [0.20, 1.16, 0.20], [0, -0.49, 0], materials.frame);
addBox(crank, [0.38, 0.38, 0.38], [0, 0, 0], materials.axle);
addBox(crank, [0.62, 0.23, 0.23], [0.05, -1.10, 0], materials.grip);
addBox(crank, [0.12, 0.29, 0.29], [0.32, -1.10, 0], materials.ratchet);
// Pawl and its mount.
addBox(
scene,
[0.32, 0.18, 0.34],
[2.03, 3.79, 0.17],
materials.frameDark
);
const pawlPivot = new THREE.Group();
pawlPivot.position.set(2.06, 3.74, 0.17);
scene.add(pawlPivot);
addBox(pawlPivot, [0.16, 0.48, 0.16], [0, -0.23, 0], materials.pawl);
addBox(pawlPivot, [0.21, 0.16, 0.21], [0, -0.47, 0], materials.pawl);
const lockLamp = addBox(
scene,
[0.16, 0.16, 0.16],
[2.20, 3.90, 0.17],
materials.lift
);
lockLamp.visible = false;
// Suspended voxel load.
const load = new THREE.Group();
load.position.set(0.72, 0.72, 0.68);
scene.add(load);
addBox(load, [0.74, 0.64, 0.74], [0, 0, 0], materials.weight);
addBox(load, [0.82, 0.12, 0.82], [0, 0.36, 0], materials.frame);
addBox(load, [0.13, 0.35, 0.13], [0, 0.58, 0], materials.frame);
addBox(load, [0.30, 0.12, 0.13], [-0.09, 0.78, 0], materials.frame);
addBox(load, [0.12, 0.25, 0.13], [-0.22, 0.63, 0], materials.frame);
// Free vertical rope run.
const ropeGeometry = new THREE.BoxGeometry(0.10, 0.12, 0.10);
const ropeSegments = [];
for (let i = 0; i < 30; i++) {
const segment = new THREE.Mesh(ropeGeometry, materials.rope);
segment.castShadow = true;
scene.add(segment);
ropeSegments.push(segment);
}
function makeVoxelArrow(material) {
const group = new THREE.Group();
addBox(group, [0.11, 0.48, 0.11], [0, -0.08, 0], material, false);
addBox(group, [0.42, 0.12, 0.11], [0, 0.22, 0], material, false);
addBox(group, [0.31, 0.12, 0.11], [0, 0.34, 0], material, false);
addBox(group, [0.18, 0.12, 0.11], [0, 0.46, 0], material, false);
return group;
}
const takeupArrow = makeVoxelArrow(materials.lift);
scene.add(takeupArrow);
const forceArrow = makeVoxelArrow(materials.force);
forceArrow.rotation.x = Math.PI;
scene.add(forceArrow);
// State and animation variables.
const MIN_LOAD_Y = 0.72;
const MAX_LOAD_Y = 1.47;
const ROPE_TOP = 2.55;
const ROPE_SPACING = 0.105;
let mode = "lift";
let loadY = MIN_LOAD_Y;
let crankAngle = 0;
let drumAngle = 0;
let crankSpeed = 0;
let lockBlend = 0;
const title = document.querySelector("#state-title");
const copy = document.querySelector("#state-copy");
const statusDot = document.querySelector("#status-dot");
const meterFill = document.querySelector("#meter-fill");
const heightValue = document.querySelector("#height-value");
const takeupLabel = document.querySelector("#label-takeup");
const forceLabel = document.querySelector("#label-force");
function updateModeUI() {
const lifting = mode === "lift";
const color = lifting ? "#63e6a7" : "#ffb45e";
title.textContent = lifting ? "LIFTING" : "HOLDING";
title.style.color = color;
statusDot.style.background = color;
statusDot.style.boxShadow = `0 0 16px ${color}`;
copy.textContent = lifting
? "The crank drives the drum. Rope is taken up while the pawl rides over the teeth."
: "The pawl engages the ratchet. The drum is locked and the downward load force is resisted.";
document.querySelectorAll("[data-mode]").forEach(button => {
const active = button.dataset.mode === mode;
button.classList.toggle("active", active);
button.setAttribute("aria-pressed", String(active));
});
takeupLabel.style.display = lifting ? "block" : "none";
forceLabel.style.display = lifting ? "none" : "block";
}
function setMode(nextMode) {
mode = nextMode;
updateModeUI();
}
document.querySelectorAll("[data-mode]").forEach(button => {
button.addEventListener("click", () => setMode(button.dataset.mode));
});
document.querySelector("#reset").addEventListener("click", () => {
loadY = MIN_LOAD_Y;
crankAngle = 0;
drumAngle = 0;
crankSpeed = 0;
setMode("lift");
});
updateModeUI();
// Direct camera controls: orbit with drag, zoom with wheel.
const canvas = renderer.domElement;
let dragging = false;
let lastPointerX = 0;
let lastPointerY = 0;
function updateCamera() {
const radius = 10;
const sinPhi = Math.sin(orbit.phi);
camera.position.set(
cameraTarget.x + radius * sinPhi * Math.sin(orbit.theta),
cameraTarget.y + radius * Math.cos(orbit.phi),
cameraTarget.z + radius * sinPhi * Math.cos(orbit.theta)
);
camera.lookAt(cameraTarget);
camera.zoom = orbit.zoom;
camera.updateProjectionMatrix();
}
canvas.addEventListener("pointerdown", event => {
dragging = true;
lastPointerX = event.clientX;
lastPointerY = event.clientY;
canvas.setPointerCapture(event.pointerId);
});
canvas.addEventListener("pointermove", event => {
if (!dragging) return;
const dx = event.clientX - lastPointerX;
const dy = event.clientY - lastPointerY;
orbit.theta -= dx * 0.007;
orbit.phi = THREE.MathUtils.clamp(
orbit.phi - dy * 0.006,
0.62,
1.43
);
lastPointerX = event.clientX;
lastPointerY = event.clientY;
updateCamera();
});
function endDrag(event) {
dragging = false;
if (event.pointerId !== undefined && canvas.hasPointerCapture(event.pointerId)) {
canvas.releasePointerCapture(event.pointerId);
}
}
canvas.addEventListener("pointerup", endDrag);
canvas.addEventListener("pointercancel", endDrag);
canvas.addEventListener("wheel", event => {
event.preventDefault();
orbit.zoom = THREE.MathUtils.clamp(
orbit.zoom * Math.exp(-event.deltaY * 0.001),
0.72,
1.75
);
updateCamera();
}, { passive: false });
canvas.addEventListener("dblclick", () => {
orbit.theta = 0.58;
orbit.phi = 1.04;
orbit.zoom = 1.06;
updateCamera();
});
function resize() {
const width = window.innerWidth;
const height = window.innerHeight;
const aspect = width / height;
const viewHeight = Math.max(6.2, 7.15 / aspect);
camera.left = -viewHeight * aspect / 2;
camera.right = viewHeight * aspect / 2;
camera.top = viewHeight / 2;
camera.bottom = -viewHeight / 2;
renderer.setSize(width, height);
updateCamera();
}
window.addEventListener("resize", resize);
resize();
// Screen-space labels tied to world-space anchors.
const labelDefinitions = [
{
element: document.querySelector("#label-drum"),
anchor: new THREE.Vector3(-0.35, 3.18, 0),
offsetX: -88,
offsetY: -36
},
{
element: document.querySelector("#label-rope"),
anchor: new THREE.Vector3(0.72, 1.38, 0.68),
offsetX: 78,
offsetY: 8
},
{
element: document.querySelector("#label-ratchet"),
anchor: new THREE.Vector3(2.02, 2.88, 0),
offsetX: 76,
offsetY: -42
}
];
const takeupAnchor = new THREE.Vector3();
const forceAnchor = new THREE.Vector3();
function projectElement(element, anchor, offsetX = 0, offsetY = 0) {
const projected = anchor.clone().project(camera);
const visible = projected.z > -1 && projected.z < 1;
const x = (projected.x * 0.5 + 0.5) * window.innerWidth + offsetX;
const y = (-projected.y * 0.5 + 0.5) * window.innerHeight + offsetY;
element.style.left = `${x}px`;
element.style.top = `${y}px`;
element.style.opacity = visible ? "1" : "0";
}
const clock = new THREE.Clock();
function animate() {
requestAnimationFrame(animate);
const dt = Math.min(clock.getDelta(), 0.04);
const time = clock.elapsedTime;
// The load rises only while the mechanism is in the lifting state.
if (mode === "lift") {
loadY += (MAX_LOAD_Y - loadY) * (1 - Math.exp(-dt * 1.55));
if (MAX_LOAD_Y - loadY < 0.008) {
loadY = MAX_LOAD_Y;
setMode("hold");
}
}
const targetSpeed = mode === "lift" ? 2.45 : 0;
crankSpeed += (targetSpeed - crankSpeed) * (1 - Math.exp(-dt * 9));
crankAngle += crankSpeed * dt;
drumAngle -= crankSpeed * dt * 0.82;
crank.rotation.x = crankAngle;
ratchet.rotation.x = crankAngle;
drum.rotation.x = drumAngle;
// Pawl drops into the teeth when holding and lifts clear while lifting.
const targetLock = mode === "hold" ? 1 : 0;
lockBlend += (targetLock - lockBlend) * (1 - Math.exp(-dt * 10));
pawlPivot.rotation.x = -0.78 * (1 - lockBlend);
const pawlFreeColor = new THREE.Color(0xffbd55);
const pawlLockedColor = new THREE.Color(0x63e6a7);
materials.pawl.color.lerp(
pawlFreeColor.clone().lerp(pawlLockedColor, lockBlend),
1 - Math.exp(-dt * 12)
);
materials.pawl.emissiveIntensity = 0.18 + lockBlend * 0.45;
lockLamp.visible = lockBlend > 0.55;
// Move the load and shorten the exposed rope run.
load.position.y = loadY;
const ropeBottom = loadY + 0.84;
const ropeLength = Math.max(0.04, ROPE_TOP - ropeBottom);
const visibleSegments = Math.min(
ropeSegments.length,
Math.ceil(ropeLength / ROPE_SPACING)
);
ropeSegments.forEach((segment, index) => {
const y = ROPE_TOP - (index + 0.5) * ROPE_SPACING;
segment.visible = index < visibleSegments && y >= ropeBottom - 0.025;
if (segment.visible) {
const sway = mode === "lift"
? Math.sin(time * 3.1 + index * 0.46) * 0.012 *
(1 - index / visibleSegments)
: 0;
segment.position.set(0.72, y, 0.68 + sway);
}
});
// Directional indicators distinguish take-up from load force.
const arrowPulse = 1 + Math.sin(time * 5) * 0.07;
takeupArrow.visible = mode === "lift";
takeupArrow.position.set(1.34, loadY + 1.33, 0.68);
takeupArrow.scale.setScalar(arrowPulse);
forceArrow.visible = mode === "hold";
forceArrow.position.set(-0.04, loadY + 0.82, 0.68);
forceArrow.scale.setScalar(arrowPulse);
const heightPercent = THREE.MathUtils.clamp(
((loadY - MIN_LOAD_Y) / (MAX_LOAD_Y - MIN_LOAD_Y)) * 100,
0,
100
);
meterFill.style.width = `${heightPercent}%`;
heightValue.textContent = `${Math.round(heightPercent)}%`;
labelDefinitions.forEach(definition => {
projectElement(
definition.element,
definition.anchor,
definition.offsetX,
definition.offsetY
);
});
takeupAnchor.set(1.34, loadY + 1.88, 0.68);
forceAnchor.set(-0.04, loadY + 1.22, 0.68);
projectElement(takeupLabel, takeupAnchor);
projectElement(forceLabel, forceAnchor);
renderer.render(scene, camera);
}
animate();
</script>
</body>
</html>
```