Prompt specification
Build or customize the Three.js 3D scene "Swiveling flat mop head" 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: Swiveling flat mop head
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/swiveling-flat-mop-head-x0061
Scene Description Prompt:
A voxel flat mop with a removable pad and visibly articulated two-axis handle joint. Three.js from cdnjs, one HTML file.
Working Three.js Starter (swiveling-flat-mop-head-x0061):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Voxel Flat Mop</title>
<style>
:root {
--ink: #17324a;
--muted: #64778a;
--paper: rgba(248, 252, 255, 0.9);
--line: rgba(23, 50, 74, 0.13);
--teal: #16b9a5;
--mint: #7de2c4;
--orange: #ffad42;
--purple: #786ee8;
}
* { box-sizing: border-box; }
html, body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #dfeaf1;
color: var(--ink);
font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
canvas {
display: block;
width: 100%;
height: 100%;
touch-action: none;
cursor: grab;
}
canvas:active { cursor: grabbing; }
.brand {
position: fixed;
z-index: 5;
top: 24px;
left: 26px;
pointer-events: none;
}
.eyebrow {
display: flex;
align-items: center;
gap: 9px;
margin-bottom: 6px;
color: var(--teal);
font-size: 11px;
font-weight: 800;
letter-spacing: 0.19em;
text-transform: uppercase;
}
.eyebrow::before {
content: "";
width: 24px;
height: 4px;
background: var(--teal);
box-shadow: 6px 0 0 var(--orange);
}
h1 {
margin: 0;
font-size: clamp(25px, 3vw, 42px);
line-height: 0.95;
letter-spacing: -0.045em;
}
.brand p {
margin: 9px 0 0;
color: var(--muted);
font-size: 13px;
}
.panel {
position: fixed;
z-index: 6;
top: 22px;
right: 22px;
width: min(330px, calc(100vw - 44px));
padding: 18px;
border: 1px solid rgba(255, 255, 255, 0.78);
border-radius: 22px;
background: var(--paper);
box-shadow:
0 22px 60px rgba(29, 59, 80, 0.15),
inset 0 1px 0 rgba(255, 255, 255, 0.85);
backdrop-filter: blur(18px);
-webkit-backdrop-filter: blur(18px);
}
.panel-head {
display: flex;
align-items: center;
justify-content: space-between;
gap: 12px;
margin-bottom: 16px;
}
.panel-head strong {
font-size: 13px;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.live {
display: inline-flex;
align-items: center;
gap: 7px;
color: var(--muted);
font-size: 11px;
font-weight: 700;
}
.live::before {
content: "";
width: 7px;
height: 7px;
border-radius: 50%;
background: var(--teal);
box-shadow: 0 0 0 5px rgba(22, 185, 165, 0.12);
}
.control {
padding: 13px 0;
border-top: 1px solid var(--line);
}
.control:first-of-type { padding-top: 0; }
.control-title {
display: flex;
align-items: center;
justify-content: space-between;
margin-bottom: 11px;
font-size: 12px;
font-weight: 750;
}
.control-title span:first-child {
display: flex;
align-items: center;
gap: 8px;
}
.axis-dot {
width: 10px;
height: 10px;
background: var(--orange);
box-shadow: 0 0 0 4px rgba(255, 173, 66, 0.15);
}
.axis-dot.purple {
background: var(--purple);
box-shadow: 0 0 0 4px rgba(120, 110, 232, 0.13);
}
output {
min-width: 56px;
padding: 4px 8px;
border-radius: 8px;
background: rgba(23, 50, 74, 0.07);
color: var(--ink);
text-align: right;
font-size: 11px;
font-variant-numeric: tabular-nums;
}
input[type="range"] {
width: 100%;
height: 5px;
margin: 0;
border-radius: 999px;
outline: none;
appearance: none;
-webkit-appearance: none;
background: linear-gradient(
90deg,
var(--teal) 0%,
var(--teal) var(--fill, 50%),
rgba(23, 50, 74, 0.12) var(--fill, 50%),
rgba(23, 50, 74, 0.12) 100%
);
cursor: pointer;
}
input[type="range"]::-webkit-slider-thumb {
width: 18px;
height: 18px;
border: 4px solid white;
border-radius: 50%;
appearance: none;
-webkit-appearance: none;
background: var(--teal);
box-shadow: 0 2px 8px rgba(23, 50, 74, 0.25);
}
input[type="range"]::-moz-range-thumb {
width: 11px;
height: 11px;
border: 4px solid white;
border-radius: 50%;
background: var(--teal);
box-shadow: 0 2px 8px rgba(23, 50, 74, 0.25);
}
.button-row {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 9px;
margin-top: 14px;
}
button {
min-height: 42px;
border: 0;
border-radius: 12px;
background: var(--ink);
color: white;
font: inherit;
font-size: 12px;
font-weight: 750;
cursor: pointer;
transition: transform 150ms ease, opacity 150ms ease, background 150ms ease;
}
button:hover { transform: translateY(-1px); }
button:active { transform: translateY(1px); }
button.secondary {
border: 1px solid var(--line);
background: rgba(255, 255, 255, 0.68);
color: var(--ink);
}
button.active {
background: var(--teal);
color: white;
}
button.full { grid-column: 1 / -1; }
.help {
position: fixed;
z-index: 5;
left: 50%;
bottom: 21px;
transform: translateX(-50%);
display: flex;
align-items: center;
gap: 14px;
padding: 10px 15px;
border: 1px solid rgba(255, 255, 255, 0.75);
border-radius: 999px;
background: rgba(248, 252, 255, 0.78);
box-shadow: 0 10px 30px rgba(29, 59, 80, 0.12);
backdrop-filter: blur(14px);
-webkit-backdrop-filter: blur(14px);
color: var(--muted);
font-size: 11px;
white-space: nowrap;
pointer-events: none;
}
.help b {
color: var(--ink);
font-weight: 750;
}
.key {
display: inline-grid;
min-width: 25px;
height: 23px;
place-items: center;
margin-right: 4px;
border: 1px solid rgba(23, 50, 74, 0.16);
border-radius: 6px;
background: white;
color: var(--ink);
font-size: 10px;
}
.scene-label {
position: fixed;
z-index: 4;
display: flex;
align-items: center;
gap: 7px;
padding: 7px 10px;
border: 1px solid rgba(255, 255, 255, 0.75);
border-radius: 10px;
background: rgba(248, 252, 255, 0.82);
box-shadow: 0 8px 24px rgba(29, 59, 80, 0.12);
backdrop-filter: blur(10px);
color: var(--ink);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.07em;
text-transform: uppercase;
pointer-events: none;
transform: translate(-50%, -50%);
opacity: 0;
transition: opacity 200ms ease;
}
.scene-label.visible { opacity: 1; }
.scene-label i {
width: 7px;
height: 7px;
background: var(--orange);
}
.scene-label.pad i { background: var(--purple); }
.loading {
position: fixed;
z-index: 20;
inset: 0;
display: grid;
place-items: center;
background: #dfeaf1;
color: var(--ink);
font-size: 12px;
font-weight: 800;
letter-spacing: 0.15em;
text-transform: uppercase;
transition: opacity 350ms ease;
}
.loading.hidden { opacity: 0; pointer-events: none; }
@media (max-width: 720px) {
.brand { top: 16px; left: 17px; }
.brand p { display: none; }
.panel {
top: auto;
right: 12px;
bottom: 12px;
width: calc(100vw - 24px);
padding: 14px;
}
.panel-head { margin-bottom: 10px; }
.control { padding: 9px 0; }
.help { display: none; }
.scene-label { display: none; }
}
</style>
</head>
<body>
<div class="loading" id="loading">Building voxel mop…</div>
<header class="brand">
<div class="eyebrow">Utility / 02</div>
<h1>FLAT MOP<br>VOXEL EDITION</h1>
<p>Dual-axis articulation · tool-free removable pad</p>
</header>
<aside class="panel" aria-label="Mop controls">
<div class="panel-head">
<strong>Joint console</strong>
<span class="live">Interactive</span>
</div>
<div class="control">
<div class="control-title">
<span><i class="axis-dot"></i>360° swivel</span>
<output id="swivelOut">0°</output>
</div>
<input id="swivel" type="range" min="-180" max="180" value="0" step="1"
aria-label="Swivel angle">
</div>
<div class="control">
<div class="control-title">
<span><i class="axis-dot purple"></i>Fold hinge</span>
<output id="foldOut">0°</output>
</div>
<input id="fold" type="range" min="-110" max="110" value="0" step="1"
aria-label="Fold angle">
</div>
<div class="button-row">
<button id="remove" class="full">Remove pad</button>
<button id="demo">Auto demo</button>
<button id="axes" class="secondary active">Axes on</button>
<button id="reset" class="secondary">Reset view</button>
</div>
</aside>
<div class="help">
<span><span class="key">LMB</span><b>Orbit</b></span>
<span><span class="key">RMB</span><b>Pan</b></span>
<span><span class="key">Wheel</span><b>Zoom</b></span>
</div>
<div class="scene-label" id="jointLabel"><i></i>Dual-axis joint</div>
<div class="scene-label pad" id="padLabel"><i></i>Removable pad</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 and lighting
// ---------------------------------------------------------------------
const renderer = new THREE.WebGLRenderer({ antialias: true, alpha: false });
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.05;
document.body.prepend(renderer.domElement);
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xdfeaf1);
scene.fog = new THREE.Fog(0xdfeaf1, 13, 29);
const camera = new THREE.PerspectiveCamera(
38,
window.innerWidth / window.innerHeight,
0.05,
60
);
const hemi = new THREE.HemisphereLight(0xf7fbff, 0x6b8192, 2.2);
scene.add(hemi);
const sun = new THREE.DirectionalLight(0xffffff, 3.6);
sun.position.set(-5, 9, 6);
sun.castShadow = true;
sun.shadow.mapSize.set(2048, 2048);
sun.shadow.camera.left = -7;
sun.shadow.camera.right = 7;
sun.shadow.camera.top = 8;
sun.shadow.camera.bottom = -5;
sun.shadow.camera.near = 0.5;
sun.shadow.camera.far = 25;
sun.shadow.bias = -0.0004;
scene.add(sun);
const rim = new THREE.DirectionalLight(0x7de2c4, 1.5);
rim.position.set(5, 5, -5);
scene.add(rim);
// ---------------------------------------------------------------------
// Materials
// ---------------------------------------------------------------------
const material = (color, roughness = 0.72, metalness = 0.0) =>
new THREE.MeshStandardMaterial({ color, roughness, metalness });
const mat = {
navy: material(0x17324a, 0.66),
dark: material(0x10283b, 0.8),
teal: material(0x18b9a5, 0.58),
mint: material(0x79dfc1, 0.72),
orange: material(0xffad42, 0.48),
purple: material(0x786ee8, 0.83),
lavender: material(0x9e96f3, 0.88),
white: material(0xf4f8fb, 0.78),
pale: material(0xd8e7ef, 0.9),
floor: material(0xcbd9e2, 0.93),
puddle: new THREE.MeshStandardMaterial({
color: 0x78b7d5,
roughness: 0.15,
metalness: 0.12,
transparent: true,
opacity: 0.58
})
};
function box(parent, size, position, meshMaterial, cast = true) {
const mesh = new THREE.Mesh(
new THREE.BoxGeometry(size[0], size[1], size[2]),
meshMaterial
);
mesh.position.set(position[0], position[1], position[2]);
mesh.castShadow = cast;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
// ---------------------------------------------------------------------
// Floor and simple voxel set dressing
// ---------------------------------------------------------------------
const floor = new THREE.Mesh(
new THREE.PlaneGeometry(40, 40),
mat.floor
);
floor.rotation.x = -Math.PI / 2;
floor.receiveShadow = true;
scene.add(floor);
const grid = new THREE.GridHelper(30, 30, 0x92a9b8, 0xa9bbc7);
grid.position.y = 0.006;
grid.material.transparent = true;
grid.material.opacity = 0.28;
scene.add(grid);
// A stepped voxel water spill.
const puddle = new THREE.Group();
const puddleTiles = [
[-0.72, 0.54, 0.46, 0.30],
[-0.38, 0.54, 0.46, 0.30],
[-0.20, 0.54, 0.46, 0.30],
[-0.55, 0.24, 0.46, 0.30],
[-0.15, 0.24, 0.46, 0.30],
[-0.45, -0.06, 0.46, 0.30],
[0.05, -0.06, 0.46, 0.30]
];
for (const [x, z, w, d] of puddleTiles) {
box(puddle, [w, 0.018, d], [x, 0.018, z], mat.puddle, false);
}
puddle.position.set(-0.75, 0, 1.05);
scene.add(puddle);
// Small utility blocks in the distance.
const setGroup = new THREE.Group();
setGroup.position.set(3.8, 0, -2.6);
scene.add(setGroup);
box(setGroup, [1.15, 0.12, 0.82], [0, 0.06, 0], mat.white);
box(setGroup, [0.78, 0.42, 0.58], [0, 0.32, 0], mat.teal);
box(setGroup, [0.86, 0.13, 0.66], [0, 0.59, 0], mat.navy);
box(setGroup, [0.12, 0.38, 0.12], [-0.29, 0.78, 0.18], mat.orange);
box(setGroup, [0.12, 0.38, 0.12], [0.29, 0.78, 0.18], mat.orange);
// ---------------------------------------------------------------------
// Main mop assembly
// ---------------------------------------------------------------------
const mop = new THREE.Group();
mop.position.set(-0.15, 0, 0);
scene.add(mop);
// Head mount remains attached when the pad is removed.
const mount = new THREE.Group();
mount.position.set(0, 0, -0.31);
mop.add(mount);
box(mount, [0.68, 0.11, 0.46], [0, 0.30, 0], mat.navy);
box(mount, [0.52, 0.17, 0.30], [0, 0.20, 0], mat.orange);
box(mount, [0.34, 0.20, 0.22], [0, 0.39, 0], mat.white);
box(mount, [0.19, 0.19, 0.19], [0, 0.47, 0], mat.dark);
// Lower segmented handle.
const lowerHandle = new THREE.Group();
lowerHandle.position.set(0, 0, -0.31);
mop.add(lowerHandle);
for (let i = 0; i < 7; i++) {
const y = 0.49 + i * 0.285;
box(
lowerHandle,
[0.15, 0.255, 0.15],
[0, y, 0],
i % 3 === 0 ? mat.mint : mat.teal
);
box(
lowerHandle,
[0.17, 0.045, 0.17],
[0, y - 0.125, 0],
mat.navy
);
}
// Fixed swivel pedestal.
box(lowerHandle, [0.42, 0.18, 0.42], [0, 2.45, 0], mat.navy);
box(lowerHandle, [0.48, 0.07, 0.48], [0, 2.55, 0], mat.orange);
// Swivel stage: rotation around the vertical Y axis.
const swivelGroup = new THREE.Group();
swivelGroup.position.set(0, 2.52, -0.31);
mop.add(swivelGroup);
box(swivelGroup, [0.48, 0.13, 0.48], [0, 0.02, 0], mat.orange);
box(swivelGroup, [0.35, 0.25, 0.33], [0, 0.16, 0], mat.navy);
// Hinge cheeks and cross axle.
box(swivelGroup, [0.13, 0.48, 0.32], [-0.25, 0.32, 0], mat.teal);
box(swivelGroup, [0.13, 0.48, 0.32], [0.25, 0.32, 0], mat.teal);
box(swivelGroup, [0.78, 0.12, 0.12], [0, 0.31, 0], mat.orange);
box(swivelGroup, [0.10, 0.24, 0.24], [-0.43, 0.31, 0], mat.orange);
box(swivelGroup, [0.10, 0.24, 0.24], [0.43, 0.31, 0], mat.orange);
// Fold stage: rotation around the local X axis.
const foldGroup = new THREE.Group();
foldGroup.position.set(0, 0.31, 0);
swivelGroup.add(foldGroup);
box(foldGroup, [0.31, 0.29, 0.29], [0, 0.07, 0], mat.orange);
box(foldGroup, [0.19, 0.19, 0.36], [0, 0.18, 0], mat.dark);
// Upper segmented shaft.
for (let i = 0; i < 7; i++) {
const y = 0.36 + i * 0.285;
box(
foldGroup,
[0.145, 0.255, 0.145],
[0, y, 0],
i % 3 === 1 ? mat.mint : mat.teal
);
box(
foldGroup,
[0.165, 0.04, 0.165],
[0, y + 0.125, 0],
mat.navy
);
}
// Voxelized ergonomic grip.
box(foldGroup, [0.21, 0.24, 0.21], [0, 2.30, 0], mat.navy);
for (let i = 0; i < 5; i++) {
box(
foldGroup,
[0.225, 0.035, 0.225],
[0, 2.21 + i * 0.075, 0],
i % 2 ? mat.dark : mat.navy
);
}
// Orange release button on the handle collar.
box(foldGroup, [0.09, 0.11, 0.09], [0.13, 2.31, 0], mat.orange);
// ---------------------------------------------------------------------
// Removable voxel microfiber pad
// ---------------------------------------------------------------------
const pad = new THREE.Group();
mop.add(pad);
box(pad, [1.96, 0.07, 0.96], [0, 0, 0], mat.dark);
const padMaterials = [mat.purple, mat.lavender, mat.mint, mat.teal];
for (let row = 0; row < 4; row++) {
for (let col = 0; col < 8; col++) {
const x = -0.805 + col * 0.23;
const z = -0.33 + row * 0.22;
const colorIndex =
(row * 2 + col + (col % 2)) % padMaterials.length;
box(
pad,
[0.205, 0.105, 0.195],
[x, 0.078, z],
padMaterials[colorIndex]
);
}
}
// A darker front edge emphasizes the flat cleaning surface.
for (let col = 0; col < 8; col++) {
box(
pad,
[0.205, 0.07, 0.075],
[-0.805 + col * 0.23, 0.035, 0.425],
mat.navy
);
}
// ---------------------------------------------------------------------
// Segmented voxel axis guides
// ---------------------------------------------------------------------
const axisGuides = new THREE.Group();
mop.add(axisGuides);
const guideMaterial = new THREE.MeshBasicMaterial({
color: 0xffad42,
transparent: true,
opacity: 0.78
});
// Vertical swivel guide.
for (let i = 0; i < 16; i++) {
const a = (i / 16) * Math.PI * 2;
box(
axisGuides,
[0.10, 0.055, 0.10],
[Math.cos(a) * 0.46, 2.61, -0.31 + Math.sin(a) * 0.46],
guideMaterial,
false
);
}
// Fold guide lives in the swivel stage so it follows the first axis.
const foldGuide = new THREE.Group();
foldGuide.position.set(0, 0.31, 0);
swivelGroup.add(foldGuide);
for (let i = 0; i < 16; i++) {
const a = (i / 16) * Math.PI * 2;
box(
foldGuide,
[0.065, 0.095, 0.095],
[0.52, Math.cos(a) * 0.39, Math.sin(a) * 0.39],
guideMaterial,
false
);
}
// ---------------------------------------------------------------------
// DOM labels projected from 3D points
// ---------------------------------------------------------------------
const jointLabel = document.getElementById("jointLabel");
const padLabel = document.getElementById("padLabel");
const labelPoint = new THREE.Vector3();
function placeLabel(element, worldPosition, offsetY = 0) {
labelPoint.copy(worldPosition).project(camera);
const x = (labelPoint.x * 0.5 + 0.5) * window.innerWidth;
const y = (-labelPoint.y * 0.5 + 0.5) * window.innerHeight + offsetY;
element.style.left = `${x}px`;
element.style.top = `${y}px`;
element.classList.toggle(
"visible",
labelPoint.z > -1 && labelPoint.z < 1 &&
x > 20 && x < window.innerWidth - 20 &&
y > 20 && y < window.innerHeight - 20
);
}
// ---------------------------------------------------------------------
// Custom orbit controls — no addons
// ---------------------------------------------------------------------
const controls = {
target: new THREE.Vector3(0, 2.18, 0),
desiredTarget: new THREE.Vector3(0, 2.18, 0),
radius: 7.7,
desiredRadius: 7.7,
theta: 0.63,
desiredTheta: 0.63,
phi: 1.28,
desiredPhi: 1.28
};
const pointer = new THREE.Vector2();
let dragging = false;
let dragButton = 0;
let lastX = 0;
let lastY = 0;
renderer.domElement.addEventListener("contextmenu", event => event.preventDefault());
renderer.domElement.addEventListener("pointerdown", event => {
dragging = true;
dragButton = event.button;
lastX = event.clientX;
lastY = event.clientY;
renderer.domElement.setPointerCapture(event.pointerId);
});
renderer.domElement.addEventListener("pointermove", event => {
if (!dragging) return;
const dx = event.clientX - lastX;
const dy = event.clientY - lastY;
lastX = event.clientX;
lastY = event.clientY;
if (dragButton === 2 || event.shiftKey) {
const scale = controls.desiredRadius * 0.0014;
const right = new THREE.Vector3(
Math.cos(controls.desiredTheta),
0,
-Math.sin(controls.desiredTheta)
);
const up = new THREE.Vector3(0, 1, 0);
controls.desiredTarget.addScaledVector(right, -dx * scale);
controls.desiredTarget.addScaledVector(up, dy * scale);
} else {
controls.desiredTheta -= dx * 0.006;
controls.desiredPhi = THREE.MathUtils.clamp(
controls.desiredPhi - dy * 0.005,
0.35,
1.48
);
}
});
function endDrag(event) {
dragging = false;
if (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();
controls.desiredRadius = THREE.MathUtils.clamp(
controls.desiredRadius * Math.exp(event.deltaY * 0.001),
3.7,
13
);
}, { passive: false });
function resetView() {
controls.desiredTarget.set(0, 2.18, 0);
controls.desiredRadius = 7.7;
controls.desiredTheta = 0.63;
controls.desiredPhi = 1.28;
}
renderer.domElement.addEventListener("dblclick", resetView);
// ---------------------------------------------------------------------
// UI state
// ---------------------------------------------------------------------
const swivelInput = document.getElementById("swivel");
const foldInput = document.getElementById("fold");
const swivelOut = document.getElementById("swivelOut");
const foldOut = document.getElementById("foldOut");
const removeButton = document.getElementById("remove");
const demoButton = document.getElementById("demo");
const axesButton = document.getElementById("axes");
const resetButton = document.getElementById("reset");
const state = {
swivel: 0,
targetSwivel: 0,
fold: 0,
targetFold: 0,
padRemoved: false,
demo: false,
axes: true
};
function updateRangeFill(input) {
const percent =
(Number(input.value) - Number(input.min)) /
(Number(input.max) - Number(input.min)) * 100;
input.style.setProperty("--fill", `${percent}%`);
}
swivelInput.addEventListener("input", () => {
state.demo = false;
demoButton.classList.remove("active");
demoButton.textContent = "Auto demo";
state.targetSwivel = Number(swivelInput.value);
swivelOut.textContent = `${state.targetSwivel}°`;
updateRangeFill(swivelInput);
});
foldInput.addEventListener("input", () => {
state.demo = false;
demoButton.classList.remove("active");
demoButton.textContent = "Auto demo";
state.targetFold = Number(foldInput.value);
foldOut.textContent = `${state.targetFold}°`;
updateRangeFill(foldInput);
});
removeButton.addEventListener("click", () => {
state.padRemoved = !state.padRemoved;
removeButton.textContent = state.padRemoved ? "Reinstall pad" : "Remove pad";
removeButton.classList.toggle("active", state.padRemoved);
});
demoButton.addEventListener("click", () => {
state.demo = !state.demo;
demoButton.classList.toggle("active", state.demo);
demoButton.textContent = state.demo ? "Stop demo" : "Auto demo";
});
axesButton.addEventListener("click", () => {
state.axes = !state.axes;
axisGuides.visible = state.axes;
foldGuide.visible = state.axes;
axesButton.classList.toggle("active", state.axes);
axesButton.textContent = state.axes ? "Axes on" : "Axes off";
});
resetButton.addEventListener("click", resetView);
updateRangeFill(swivelInput);
updateRangeFill(foldInput);
// ---------------------------------------------------------------------
// Animation
// ---------------------------------------------------------------------
const clock = new THREE.Clock();
const attachedPosition = new THREE.Vector3(0, 0.065, 0);
const detachedPosition = new THREE.Vector3(2.55, 0.065, 0.95);
function damp(current, target, lambda, dt) {
return THREE.MathUtils.lerp(current, target, 1 - Math.exp(-lambda * dt));
}
function animate() {
const dt = Math.min(clock.getDelta(), 0.05);
const time = clock.elapsedTime;
if (state.demo) {
state.targetSwivel = Math.sin(time * 0.65) * 155;
state.targetFold = 62 + Math.sin(time * 0.43) * 42;
swivelInput.value = Math.round(state.targetSwivel);
foldInput.value = Math.round(state.targetFold);
swivelOut.textContent = `${Math.round(state.targetSwivel)}°`;
foldOut.textContent = `${Math.round(state.targetFold)}°`;
updateRangeFill(swivelInput);
updateRangeFill(foldInput);
}
state.swivel = damp(state.swivel, state.targetSwivel, 8, dt);
state.fold = damp(state.fold, state.targetFold, 8, dt);
swivelGroup.rotation.y = THREE.MathUtils.degToRad(state.swivel);
foldGroup.rotation.x = THREE.MathUtils.degToRad(state.fold);
const padTarget = state.padRemoved ? detachedPosition : attachedPosition;
pad.position.lerp(padTarget, 1 - Math.exp(-5.5 * dt));
const targetYaw = state.padRemoved ? -0.42 : 0;
const currentYaw = pad.rotation.y;
pad.rotation.y = damp(currentYaw, targetYaw, 5.5, dt);
// Custom camera damping.
controls.radius = damp(controls.radius, controls.desiredRadius, 8, dt);
controls.theta = damp(controls.theta, controls.desiredTheta, 9, dt);
controls.phi = damp(controls.phi, controls.desiredPhi, 9, dt);
controls.target.lerp(controls.desiredTarget, 1 - Math.exp(-9 * dt));
const sinPhi = Math.sin(controls.phi);
camera.position.set(
controls.target.x + controls.radius * sinPhi * Math.sin(controls.theta),
controls.target.y + controls.radius * Math.cos(controls.phi),
controls.target.z + controls.radius * sinPhi * Math.cos(controls.theta)
);
camera.lookAt(controls.target);
// Labels.
swivelGroup.getWorldPosition(labelPoint);
placeLabel(jointLabel, labelPoint, -42);
pad.getWorldPosition(labelPoint);
placeLabel(padLabel, labelPoint, 46);
renderer.render(scene, camera);
requestAnimationFrame(animate);
}
window.addEventListener("resize", () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
});
animate();
requestAnimationFrame(() => {
document.getElementById("loading").classList.add("hidden");
});
</script>
</body>
</html>
```