Prompt specification
Build or customize the Three.js 3D scene "Tight-space repair vignette" 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: Tight-space repair vignette
Category: Everyday & Industrial · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/tight-space-repair-vignette-x0295
Scene Description Prompt:
Use Three.js from cdnjs to compose a shelf bracket repair with a restricted tool arc; a ratchet turns the fastener through repeated short strokes without requiring the handle to complete full circles. Deliver one self-contained HTML file with stylized solid shapes.
Working Three.js Starter (tight-space-repair-vignette-x0295):
```html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Restricted-Arc Shelf Bracket Repair</title>
<style>
:root {
color-scheme: light;
--ink: #17323c;
--muted: #64777d;
--panel: rgba(248, 250, 247, 0.9);
--line: rgba(23, 50, 60, 0.14);
--green: #21a77b;
--orange: #ed623e;
}
* { box-sizing: border-box; }
html, body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #dfe7e6;
color: var(--ink);
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%;
cursor: grab;
touch-action: none;
}
canvas.dragging { cursor: grabbing; }
.overlay {
position: fixed;
inset: 0;
pointer-events: none;
}
.brand {
position: absolute;
top: clamp(22px, 4vw, 42px);
left: clamp(22px, 4vw, 48px);
max-width: 430px;
text-shadow: 0 1px 0 rgba(255,255,255,.5);
}
.eyebrow {
display: flex;
align-items: center;
gap: 9px;
margin-bottom: 9px;
color: var(--green);
font-size: 11px;
font-weight: 800;
letter-spacing: .19em;
text-transform: uppercase;
}
.eyebrow::before {
content: "";
width: 26px;
height: 3px;
border-radius: 3px;
background: var(--green);
}
h1 {
margin: 0;
font-size: clamp(28px, 4.5vw, 54px);
line-height: .98;
letter-spacing: -.045em;
font-weight: 850;
}
.brand p {
max-width: 390px;
margin: 15px 0 0;
color: var(--muted);
font-size: 13px;
line-height: 1.55;
}
.hud {
position: absolute;
right: clamp(18px, 3vw, 36px);
top: 50%;
width: 278px;
padding: 18px;
border: 1px solid rgba(255,255,255,.72);
border-radius: 18px;
background: var(--panel);
box-shadow:
0 18px 55px rgba(34, 57, 62, .14),
inset 0 0 0 1px var(--line);
backdrop-filter: blur(15px);
pointer-events: auto;
transform: translateY(-50%);
}
.hud-title {
display: flex;
align-items: center;
justify-content: space-between;
padding-bottom: 13px;
border-bottom: 1px solid var(--line);
font-size: 12px;
font-weight: 800;
letter-spacing: .11em;
text-transform: uppercase;
}
.live-dot {
width: 8px;
height: 8px;
border-radius: 50%;
background: var(--green);
box-shadow: 0 0 0 5px rgba(33,167,123,.12);
animation: pulse 1.6s ease-in-out infinite;
}
@keyframes pulse {
50% { box-shadow: 0 0 0 8px rgba(33,167,123,0); }
}
.metrics {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 10px;
margin: 14px 0;
}
.metric {
padding: 11px;
border-radius: 11px;
background: rgba(255,255,255,.68);
border: 1px solid rgba(23,50,60,.07);
}
.metric small {
display: block;
margin-bottom: 4px;
color: var(--muted);
font-size: 9px;
font-weight: 800;
letter-spacing: .11em;
text-transform: uppercase;
}
.metric strong {
display: block;
font-size: 18px;
letter-spacing: -.025em;
}
#phase {
color: var(--green);
font-size: 13px;
}
.stroke-track {
height: 5px;
overflow: hidden;
margin: 0 0 14px;
border-radius: 5px;
background: rgba(23,50,60,.1);
}
#strokeProgress {
width: 0;
height: 100%;
border-radius: inherit;
background: var(--green);
transition: background-color .15s ease;
}
.button-row {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 8px;
margin-bottom: 12px;
}
button {
min-height: 38px;
padding: 9px 10px;
border: 1px solid rgba(23,50,60,.12);
border-radius: 10px;
background: rgba(255,255,255,.8);
color: var(--ink);
font: inherit;
font-size: 11px;
font-weight: 750;
cursor: pointer;
transition: transform .15s ease, background .15s ease;
}
button:hover {
transform: translateY(-1px);
background: white;
}
button.primary {
grid-column: 1 / -1;
background: var(--ink);
color: white;
border-color: var(--ink);
}
.slider-row {
padding-top: 12px;
border-top: 1px solid var(--line);
}
.slider-row label {
display: flex;
justify-content: space-between;
margin-bottom: 8px;
color: var(--muted);
font-size: 10px;
font-weight: 750;
letter-spacing: .07em;
text-transform: uppercase;
}
input[type="range"] {
width: 100%;
accent-color: var(--green);
}
.toggle {
display: flex;
align-items: center;
gap: 8px;
margin-top: 12px;
color: var(--muted);
font-size: 11px;
cursor: pointer;
}
.toggle input {
accent-color: var(--green);
}
.hint {
position: absolute;
left: clamp(22px, 4vw, 48px);
bottom: clamp(20px, 4vw, 40px);
display: flex;
align-items: center;
gap: 12px;
color: var(--muted);
font-size: 11px;
font-weight: 650;
}
.mouse {
position: relative;
width: 23px;
height: 31px;
border: 2px solid rgba(23,50,60,.46);
border-radius: 12px;
}
.mouse::after {
content: "";
position: absolute;
top: 5px;
left: 50%;
width: 3px;
height: 7px;
border-radius: 2px;
background: rgba(23,50,60,.55);
transform: translateX(-50%);
}
.world-label {
position: absolute;
z-index: 3;
padding: 7px 10px;
border: 1px solid rgba(33,167,123,.28);
border-radius: 999px;
background: rgba(242,255,249,.92);
color: #137c5d;
box-shadow: 0 7px 24px rgba(22, 84, 64, .13);
font-size: 9px;
font-weight: 850;
letter-spacing: .12em;
text-transform: uppercase;
white-space: nowrap;
transform: translate(-50%, -50%);
pointer-events: none;
}
.world-label::after {
content: "";
position: absolute;
top: 50%;
right: calc(100% + 1px);
width: 25px;
height: 1px;
background: rgba(33,167,123,.55);
}
.legend {
position: absolute;
right: clamp(18px, 3vw, 36px);
bottom: clamp(18px, 3vw, 34px);
display: flex;
gap: 16px;
color: var(--muted);
font-size: 10px;
font-weight: 700;
}
.legend span {
display: flex;
align-items: center;
gap: 6px;
}
.swatch {
width: 9px;
height: 9px;
border-radius: 50%;
background: var(--green);
}
.swatch.orange { background: var(--orange); }
@media (max-width: 760px) {
.brand p, .hint, .legend { display: none; }
.brand {
top: 20px;
left: 20px;
}
.hud {
top: auto;
right: 12px;
bottom: 12px;
left: 12px;
width: auto;
padding: 13px;
transform: none;
}
.metrics { margin: 10px 0; }
.metric { padding: 8px; }
.button-row { margin-bottom: 8px; }
.slider-row { padding-top: 8px; }
}
</style>
</head>
<body>
<div id="stage"></div>
<div class="overlay">
<header class="brand">
<div class="eyebrow">Field repair / 04</div>
<h1>Short-stroke<br>ratchet</h1>
<p>
A restricted service arc lets the handle reciprocate without completing
a full circle. The fastener advances only during the power stroke.
</p>
</header>
<aside class="hud">
<div class="hud-title">
<span>Repair cycle</span>
<span class="live-dot" id="liveDot"></span>
</div>
<div class="metrics">
<div class="metric">
<small>Phase</small>
<strong id="phase">Power stroke</strong>
</div>
<div class="metric">
<small>Fastener turn</small>
<strong id="turn">0°</strong>
</div>
<div class="metric">
<small>Completed strokes</small>
<strong id="strokes">0</strong>
</div>
<div class="metric">
<small>Tool arc</small>
<strong>53°</strong>
</div>
</div>
<div class="stroke-track"><div id="strokeProgress"></div></div>
<div class="button-row">
<button class="primary" id="toggle">Pause motion</button>
<button id="resetJoint">Reset joint</button>
<button id="resetView">Reset view</button>
</div>
<div class="slider-row">
<label for="speed"><span>Cycle speed</span><span id="speedValue">1.0×</span></label>
<input id="speed" type="range" min="0.35" max="1.8" step="0.05" value="1">
</div>
<label class="toggle">
<input id="showArc" type="checkbox" checked>
Show restricted working arc
</label>
</aside>
<div class="world-label" id="arcLabel">53° restricted arc</div>
<div class="hint">
<span class="mouse"></span>
<span>Drag to orbit · Scroll to zoom · Double-click to reset</span>
</div>
<div class="legend">
<span><i class="swatch"></i> Power stroke</span>
<span><i class="swatch orange"></i> Repair fastener</span>
</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.getElementById('stage');
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xdfe7e6);
scene.fog = new THREE.Fog(0xdfe7e6, 10, 19);
const renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.12;
stage.appendChild(renderer.domElement);
const camera = new THREE.PerspectiveCamera(
37,
window.innerWidth / window.innerHeight,
0.1,
40
);
const target = new THREE.Vector3(-0.05, 1.42, -0.62);
const defaultView = { theta: 0.36, phi: 1.17, radius: 7.7 };
const desired = { ...defaultView };
const current = { ...defaultView };
function updateCamera() {
const sinPhi = Math.sin(current.phi);
camera.position.set(
target.x + current.radius * sinPhi * Math.sin(current.theta),
target.y + current.radius * Math.cos(current.phi),
target.z + current.radius * sinPhi * Math.cos(current.theta)
);
camera.lookAt(target);
}
updateCamera();
const hemi = new THREE.HemisphereLight(0xf8ffff, 0x617173, 2.15);
scene.add(hemi);
const key = new THREE.DirectionalLight(0xfff4df, 3.3);
key.position.set(-3.8, 7.4, 5.8);
key.castShadow = true;
key.shadow.mapSize.set(2048, 2048);
key.shadow.camera.left = -5;
key.shadow.camera.right = 5;
key.shadow.camera.top = 6;
key.shadow.camera.bottom = -3;
key.shadow.camera.near = 0.5;
key.shadow.camera.far = 18;
key.shadow.normalBias = 0.025;
scene.add(key);
const fill = new THREE.DirectionalLight(0x9ed9ff, 1.15);
fill.position.set(5, 3, -1);
scene.add(fill);
const mat = {
wall: new THREE.MeshStandardMaterial({
color: 0xd5dfdf, roughness: 0.96, metalness: 0
}),
wallInset: new THREE.MeshStandardMaterial({
color: 0xc8d5d5, roughness: 0.95
}),
floor: new THREE.MeshStandardMaterial({
color: 0xb8c7c7, roughness: 0.92
}),
shelf: new THREE.MeshStandardMaterial({
color: 0xc98a55, roughness: 0.72
}),
shelfEdge: new THREE.MeshStandardMaterial({
color: 0x9f633c, roughness: 0.78
}),
bracket: new THREE.MeshStandardMaterial({
color: 0x1d5361, roughness: 0.46, metalness: 0.42
}),
bracketMuted: new THREE.MeshStandardMaterial({
color: 0x62777b, roughness: 0.57, metalness: 0.32
}),
steel: new THREE.MeshStandardMaterial({
color: 0xb9c5c8, roughness: 0.3, metalness: 0.78
}),
steelDark: new THREE.MeshStandardMaterial({
color: 0x35464d, roughness: 0.4, metalness: 0.65
}),
rubber: new THREE.MeshStandardMaterial({
color: 0x20343a, roughness: 0.86, metalness: 0.05
}),
orange: new THREE.MeshStandardMaterial({
color: 0xef633f, roughness: 0.62, metalness: 0.05
}),
green: new THREE.MeshStandardMaterial({
color: 0x29ad7f, roughness: 0.55
}),
yellow: new THREE.MeshStandardMaterial({
color: 0xe3b74f, roughness: 0.72
}),
hole: new THREE.MeshStandardMaterial({
color: 0x172c32, roughness: 0.85
})
};
function box(parent, size, position, material, shadow = true) {
const mesh = new THREE.Mesh(
new THREE.BoxGeometry(size.x, size.y, size.z),
material
);
mesh.position.copy(position);
mesh.castShadow = shadow;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
function cylinderZ(parent, radius, depth, position, material, segments = 32) {
const mesh = new THREE.Mesh(
new THREE.CylinderGeometry(radius, radius, depth, segments),
material
);
mesh.rotation.x = Math.PI / 2;
mesh.position.copy(position);
mesh.castShadow = true;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
function beamBetween(parent, a, b, width, depth, material) {
const direction = b.clone().sub(a);
const length = direction.length();
const midpoint = a.clone().add(b).multiplyScalar(0.5);
const beam = new THREE.Mesh(
new THREE.BoxGeometry(width, length, depth),
material
);
beam.position.copy(midpoint);
beam.quaternion.setFromUnitVectors(
new THREE.Vector3(0, 1, 0),
direction.normalize()
);
beam.castShadow = true;
beam.receiveShadow = true;
parent.add(beam);
return beam;
}
// Room and wall.
box(scene, new THREE.Vector3(14, 0.12, 14),
new THREE.Vector3(0, -0.08, 1.5), mat.floor);
box(scene, new THREE.Vector3(14, 7, 0.18),
new THREE.Vector3(0, 3.4, -2.25), mat.wall);
box(scene, new THREE.Vector3(14, 0.22, 0.12),
new THREE.Vector3(0, 0.11, -2.12), mat.wallInset);
for (let x = -5; x <= 5; x += 1.25) {
box(scene, new THREE.Vector3(0.022, 6.6, 0.012),
new THREE.Vector3(x, 3.35, -2.145), mat.wallInset, false);
}
// Shelf plank.
box(scene, new THREE.Vector3(4.85, 0.18, 2.45),
new THREE.Vector3(0.25, 2.31, -0.9), mat.shelf);
box(scene, new THREE.Vector3(4.87, 0.19, 0.065),
new THREE.Vector3(0.25, 2.31, 0.337), mat.shelfEdge);
for (let i = 0; i < 4; i++) {
box(scene, new THREE.Vector3(3.8, 0.012, 0.012),
new THREE.Vector3(0.25, 2.19 + i * 0.037, 0.374),
new THREE.MeshStandardMaterial({
color: 0x8f5939, roughness: 0.9
}), false);
}
function createBracket(x, material, primary = false) {
const bracket = new THREE.Group();
scene.add(bracket);
box(bracket, new THREE.Vector3(0.74, 1.48, 0.12),
new THREE.Vector3(x, 1.46, -2.08), material);
box(bracket, new THREE.Vector3(0.74, 0.12, 1.98),
new THREE.Vector3(x, 2.16, -1.10), material);
box(bracket, new THREE.Vector3(0.74, 0.2, 0.09),
new THREE.Vector3(x, 2.18, -0.16), material);
beamBetween(
bracket,
new THREE.Vector3(x + 0.27, 0.82, -2.00),
new THREE.Vector3(x + 0.27, 2.11, -0.25),
0.095, 0.11,
material
);
[0.9, 1.38, 1.98].forEach((y) => {
cylinderZ(
bracket, 0.065, 0.018,
new THREE.Vector3(x, y, -2.011),
mat.hole, 20
);
});
if (!primary) {
[1.38, 1.98].forEach((y) => {
cylinderZ(
bracket, 0.145, 0.10,
new THREE.Vector3(x, y, -1.95),
mat.steel, 6
);
});
}
return bracket;
}
const activeX = -0.62;
createBracket(activeX, mat.bracket, true);
createBracket(1.68, mat.bracketMuted, false);
// A compact repair case on the shelf.
const caseGroup = new THREE.Group();
caseGroup.position.set(1.65, 2.40, -0.68);
scene.add(caseGroup);
box(caseGroup, new THREE.Vector3(1.03, 0.31, 0.67),
new THREE.Vector3(0, 0.16, 0), mat.yellow);
box(caseGroup, new THREE.Vector3(1.06, 0.07, 0.69),
new THREE.Vector3(0, 0.34, 0), mat.shelfEdge);
const caseHandle = new THREE.Mesh(
new THREE.TorusGeometry(0.19, 0.035, 8, 24, Math.PI),
mat.steelDark
);
caseHandle.position.set(0, 0.42, 0);
caseHandle.castShadow = true;
caseGroup.add(caseHandle);
// Visible repair fastener, with an orange witness mark.
const fastener = new THREE.Group();
fastener.position.set(activeX, 1.38, -1.98);
scene.add(fastener);
cylinderZ(fastener, 0.235, 0.045,
new THREE.Vector3(0, 0, 0), mat.steelDark, 32);
cylinderZ(fastener, 0.175, 0.145,
new THREE.Vector3(0, 0, 0.095), mat.steel, 6);
const witness = box(fastener,
new THREE.Vector3(0.043, 0.12, 0.016),
new THREE.Vector3(0.075, 0.083, 0.173), mat.orange);
witness.rotation.z = 0.26;
const repairRing = new THREE.Mesh(
new THREE.TorusGeometry(0.278, 0.025, 8, 48),
mat.orange
);
repairRing.position.set(activeX, 1.38, -1.944);
scene.add(repairRing);
// Ratchet socket.
const socketSpin = new THREE.Group();
socketSpin.position.set(activeX, 1.38, -1.69);
scene.add(socketSpin);
cylinderZ(socketSpin, 0.17, 0.37,
new THREE.Vector3(0, 0, 0), mat.steel, 12);
box(socketSpin, new THREE.Vector3(0.115, 0.115, 0.13),
new THREE.Vector3(0, 0, -0.20), mat.steelDark);
// Ratchet head and visible teeth.
const headPosition = new THREE.Vector3(activeX, 1.38, -1.48);
const gear = new THREE.Group();
gear.position.copy(headPosition);
scene.add(gear);
cylinderZ(gear, 0.31, 0.14,
new THREE.Vector3(0, 0, 0), mat.steelDark, 32);
const gearFace = cylinderZ(gear, 0.255, 0.015,
new THREE.Vector3(0, 0, 0.078), mat.steel, 32);
for (let i = 0; i < 14; i++) {
const angle = (i / 14) * Math.PI * 2;
const tooth = box(gear,
new THREE.Vector3(0.12, 0.12, 0.115),
new THREE.Vector3(
Math.cos(angle) * 0.315,
Math.sin(angle) * 0.315,
0
),
mat.steelDark);
tooth.rotation.z = angle;
}
cylinderZ(gear, 0.105, 0.19,
new THREE.Vector3(0, 0, 0.07), mat.steelDark, 6);
// Handle pivots around the socket and never leaves the restricted arc.
const handle = new THREE.Group();
handle.position.set(activeX, 1.38, -1.37);
scene.add(handle);
cylinderZ(handle, 0.18, 0.18,
new THREE.Vector3(0, 0, 0), mat.steel, 24);
box(handle, new THREE.Vector3(1.10, 0.13, 0.15),
new THREE.Vector3(0.61, 0, 0), mat.steelDark);
const grip = cylinderZ(handle, 0.135, 0.51,
new THREE.Vector3(1.25, 0, 0), mat.orange, 20);
grip.rotation.set(0, 0, Math.PI / 2);
const gripEndA = new THREE.Mesh(
new THREE.SphereGeometry(0.135, 16, 12),
mat.orange
);
gripEndA.position.set(0.995, 0, 0);
gripEndA.castShadow = true;
handle.add(gripEndA);
const gripEndB = gripEndA.clone();
gripEndB.position.x = 1.505;
handle.add(gripEndB);
// Restricted tool arc.
const arcStart = -0.98;
const arcEnd = -0.06;
const arcSpan = arcEnd - arcStart;
const arcGroup = new THREE.Group();
arcGroup.position.set(activeX, 1.38, -1.245);
scene.add(arcGroup);
const arcSectorMaterial = new THREE.MeshBasicMaterial({
color: 0x24b783,
transparent: true,
opacity: 0.105,
side: THREE.DoubleSide,
depthWrite: false
});
const arcSector = new THREE.Mesh(
new THREE.RingGeometry(1.16, 1.64, 72, 1, arcStart, arcSpan),
arcSectorMaterial
);
arcGroup.add(arcSector);
const arcLineMaterial = new THREE.MeshBasicMaterial({
color: 0x15966b,
transparent: true,
opacity: 0.9,
depthWrite: false
});
const arcLine = new THREE.Mesh(
new THREE.TorusGeometry(1.49, 0.025, 8, 72, arcSpan),
arcLineMaterial
);
arcLine.rotation.z = arcStart;
arcGroup.add(arcLine);
[arcStart, arcEnd].forEach((angle) => {
const endpoint = new THREE.Mesh(
new THREE.SphereGeometry(0.055, 16, 12),
mat.green
);
endpoint.position.set(
Math.cos(angle) * 1.49,
Math.sin(angle) * 1.49,
0
);
arcGroup.add(endpoint);
const boundary = box(
arcGroup,
new THREE.Vector3(1.34, 0.018, 0.018),
new THREE.Vector3(
Math.cos(angle) * 0.86,
Math.sin(angle) * 0.86,
0
),
arcLineMaterial,
false
);
boundary.rotation.z = angle;
boundary.material.opacity = 0.28;
});
const arrowAngle = -0.56;
const arrow = new THREE.Mesh(
new THREE.ConeGeometry(0.075, 0.18, 3),
mat.green
);
arrow.position.set(
Math.cos(arrowAngle) * 1.49,
Math.sin(arrowAngle) * 1.49,
0.01
);
const tangent = new THREE.Vector3(
-Math.sin(arrowAngle),
Math.cos(arrowAngle),
0
);
arrow.quaternion.setFromUnitVectors(
new THREE.Vector3(0, 1, 0),
tangent.normalize()
);
arcGroup.add(arrow);
// Animation state.
let playing = true;
let speed = 1;
let mode = 'drive';
let cycleTime = 0;
let handleAngle = arcStart;
let nutAngle = 0;
let strokeCount = 0;
const driveDuration = 1.15;
const resetDuration = 0.82;
const phaseElement = document.getElementById('phase');
const turnElement = document.getElementById('turn');
const strokesElement = document.getElementById('strokes');
const progressElement = document.getElementById('strokeProgress');
const toggleButton = document.getElementById('toggle');
const liveDot = document.getElementById('liveDot');
const speedSlider = document.getElementById('speed');
const speedValue = document.getElementById('speedValue');
const showArc = document.getElementById('showArc');
const smooth = (t) => t * t * (3 - 2 * t);
function updateRepair(delta) {
if (!playing) return;
cycleTime += delta * speed;
if (mode === 'drive') {
const t = Math.min(cycleTime / driveDuration, 1);
handleAngle = THREE.MathUtils.lerp(arcStart, arcEnd, smooth(t));
if (t >= 1) {
nutAngle += arcSpan * 0.62;
strokeCount++;
mode = 'reset';
cycleTime = 0;
}
} else {
const t = Math.min(cycleTime / resetDuration, 1);
handleAngle = THREE.MathUtils.lerp(arcEnd, arcStart, smooth(t));
if (t >= 1) {
mode = 'drive';
cycleTime = 0;
}
}
}
function updateHud() {
const isDrive = mode === 'drive';
const progress = isDrive
? Math.min(cycleTime / driveDuration, 1)
: Math.min(cycleTime / resetDuration, 1);
phaseElement.textContent = isDrive ? 'Power stroke' : 'Ratchet reset';
phaseElement.style.color = isDrive ? '#168f68' : '#b47a24';
const degrees = ((nutAngle * 180 / Math.PI) % 360 + 360) % 360;
turnElement.textContent = `${Math.round(degrees)}°`;
strokesElement.textContent = String(strokeCount);
progressElement.style.width = `${progress * 100}%`;
progressElement.style.backgroundColor = isDrive ? '#21a77b' : '#d5a03f';
arcSectorMaterial.opacity = isDrive ? 0.14 : 0.07;
}
toggleButton.addEventListener('click', () => {
playing = !playing;
toggleButton.textContent = playing ? 'Pause motion' : 'Resume motion';
liveDot.style.background = playing ? '#21a77b' : '#c28b37';
liveDot.style.animationPlayState = playing ? 'running' : 'paused';
});
document.getElementById('resetJoint').addEventListener('click', () => {
nutAngle = 0;
strokeCount = 0;
mode = 'drive';
cycleTime = 0;
handleAngle = arcStart;
});
function resetView() {
desired.theta = defaultView.theta;
desired.phi = defaultView.phi;
desired.radius = defaultView.radius;
}
document.getElementById('resetView').addEventListener('click', resetView);
speedSlider.addEventListener('input', () => {
speed = Number(speedSlider.value);
speedValue.textContent = `${speed.toFixed(2).replace(/0$/, '')}×`;
});
showArc.addEventListener('change', () => {
arcGroup.visible = showArc.checked;
document.getElementById('arcLabel').style.display =
showArc.checked ? 'block' : 'none';
});
// Direct camera controls: no addons.
let dragging = false;
let lastX = 0;
let lastY = 0;
renderer.domElement.addEventListener('pointerdown', (event) => {
dragging = true;
lastX = event.clientX;
lastY = event.clientY;
renderer.domElement.setPointerCapture(event.pointerId);
renderer.domElement.classList.add('dragging');
});
renderer.domElement.addEventListener('pointermove', (event) => {
if (!dragging) return;
const dx = event.clientX - lastX;
const dy = event.clientY - lastY;
lastX = event.clientX;
lastY = event.clientY;
desired.theta -= dx * 0.006;
desired.phi = THREE.MathUtils.clamp(
desired.phi - dy * 0.005,
0.42,
1.47
);
});
function endDrag(event) {
dragging = false;
renderer.domElement.classList.remove('dragging');
if (event.pointerId !== undefined &&
renderer.domElement.hasPointerCapture(event.pointerId)) {
renderer.domElement.releasePointerCapture(event.pointerId);
}
}
renderer.domElement.addEventListener('pointerup', endDrag);
renderer.domElement.addEventListener('pointercancel', endDrag);
renderer.domElement.addEventListener('wheel', (event) => {
event.preventDefault();
desired.radius = THREE.MathUtils.clamp(
desired.radius + event.deltaY * 0.006,
4.5,
11.5
);
}, { passive: false });
renderer.domElement.addEventListener('dblclick', resetView);
const labelAnchor = new THREE.Vector3();
const arcLabel = document.getElementById('arcLabel');
function positionArcLabel() {
labelAnchor.set(
activeX + Math.cos(arcEnd) * 1.63,
1.38 + Math.sin(arcEnd) * 1.63,
-1.18
).project(camera);
const x = (labelAnchor.x * 0.5 + 0.5) * window.innerWidth;
const y = (-labelAnchor.y * 0.5 + 0.5) * window.innerHeight;
arcLabel.style.left = `${x}px`;
arcLabel.style.top = `${y - 18}px`;
arcLabel.style.visibility =
labelAnchor.z > -1 && labelAnchor.z < 1 ? 'visible' : 'hidden';
}
window.addEventListener('resize', () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
});
const clock = new THREE.Clock();
renderer.setAnimationLoop(() => {
const delta = Math.min(clock.getDelta(), 0.05);
updateRepair(delta);
const damping = 1 - Math.exp(-delta * 11);
current.theta += (desired.theta - current.theta) * damping;
current.phi += (desired.phi - current.phi) * damping;
current.radius += (desired.radius - current.radius) * damping;
updateCamera();
handle.rotation.z = handleAngle;
gear.rotation.z = handleAngle;
fastener.rotation.z = nutAngle;
socketSpin.rotation.z = nutAngle;
updateHud();
positionArcLabel();
renderer.render(scene, camera);
});
</script>
</body>
</html>
```