Prompt specification
Build or customize the Three.js 3D scene "Replacement handle alignment" 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: Replacement handle alignment
Category: Everyday & Industrial · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/replacement-handle-alignment-x0270
Scene Description Prompt:
Make a standalone HTML scene with Three.js from cdnjs that depicts cabinet handle attachments. Move candidate handles toward a drawer front; valid spacing fits both holes simultaneously, while mismatches remain visibly offset without forcing parts through the panel. Low-poly.
Working Three.js Starter (replacement-handle-alignment-x0270):
```html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Cabinet Handle Fit Lab</title>
<style>
:root {
color-scheme: dark;
--ink: #f4f7f5;
--muted: #aebcb6;
--glass: rgba(23, 34, 32, 0.88);
--line: rgba(255, 255, 255, 0.12);
--green: #8fe0c1;
--orange: #f08a64;
--yellow: #f0cf75;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #cbd6d1;
font-family:
Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
button {
font: inherit;
}
#scene {
position: fixed;
inset: 0;
width: 100%;
height: 100%;
display: block;
touch-action: none;
cursor: grab;
}
#scene.dragging {
cursor: grabbing;
}
.brand {
position: fixed;
z-index: 10;
top: 25px;
left: 28px;
display: flex;
align-items: center;
gap: 12px;
color: #19302a;
pointer-events: none;
}
.brand-mark {
position: relative;
width: 43px;
height: 43px;
border: 2px solid #24463d;
border-radius: 13px;
background: rgba(244, 249, 246, 0.62);
box-shadow: 0 8px 25px rgba(29, 54, 47, 0.12);
}
.brand-mark::before,
.brand-mark::after {
content: "";
position: absolute;
top: 19px;
width: 8px;
height: 8px;
border: 2px solid #24463d;
border-radius: 50%;
}
.brand-mark::before {
left: 7px;
}
.brand-mark::after {
right: 7px;
}
.brand-mark span {
position: absolute;
top: 17px;
left: 10px;
width: 19px;
height: 5px;
border-radius: 3px;
background: #24463d;
}
.brand b {
display: block;
font-size: 15px;
letter-spacing: 0.02em;
}
.brand small {
display: block;
margin-top: 2px;
color: #527068;
font-size: 11px;
letter-spacing: 0.14em;
text-transform: uppercase;
}
.control-panel {
position: fixed;
z-index: 20;
top: 24px;
right: 24px;
width: 342px;
padding: 22px;
color: var(--ink);
background: var(--glass);
border: 1px solid var(--line);
border-radius: 22px;
box-shadow: 0 24px 70px rgba(20, 35, 31, 0.28);
backdrop-filter: blur(18px);
}
.eyebrow {
margin: 0 0 7px;
color: var(--green);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.19em;
text-transform: uppercase;
}
h1 {
margin: 0;
font-size: 27px;
line-height: 1.08;
letter-spacing: -0.035em;
}
.intro {
margin: 11px 0 19px;
color: var(--muted);
font-size: 13px;
line-height: 1.55;
}
.panel-spec {
display: flex;
align-items: center;
justify-content: space-between;
margin-bottom: 17px;
padding: 11px 12px;
border: 1px solid rgba(143, 224, 193, 0.2);
border-radius: 12px;
background: rgba(143, 224, 193, 0.07);
}
.panel-spec span {
color: var(--muted);
font-size: 11px;
}
.panel-spec b {
color: var(--green);
font-size: 14px;
font-variant-numeric: tabular-nums;
}
.candidate-list {
display: grid;
gap: 8px;
}
.candidate {
width: 100%;
display: grid;
grid-template-columns: 51px 1fr auto;
align-items: center;
gap: 10px;
padding: 11px;
color: var(--ink);
text-align: left;
background: rgba(255, 255, 255, 0.035);
border: 1px solid rgba(255, 255, 255, 0.09);
border-radius: 13px;
cursor: pointer;
transition:
background 160ms ease,
border-color 160ms ease,
transform 160ms ease;
}
.candidate:hover {
transform: translateY(-1px);
background: rgba(255, 255, 255, 0.07);
}
.candidate.active {
border-color: rgba(143, 224, 193, 0.75);
background: rgba(143, 224, 193, 0.1);
}
.candidate[data-id="narrow"].active,
.candidate[data-id="wide"].active {
border-color: rgba(240, 138, 100, 0.75);
background: rgba(240, 138, 100, 0.1);
}
.handle-icon {
position: relative;
width: 46px;
height: 25px;
}
.handle-icon::before {
content: "";
position: absolute;
top: 10px;
left: 8px;
right: 8px;
height: 5px;
border-radius: 3px;
background: #d9e1dd;
}
.handle-icon::after {
content: "";
position: absolute;
top: 5px;
left: 4px;
width: 7px;
height: 7px;
border: 2px solid #d9e1dd;
border-radius: 50%;
box-shadow: 26px 0 0 -2px #d9e1dd, 26px 0 0 0 #d9e1dd;
}
.handle-icon.narrow::before,
.handle-icon.narrow::after {
transform: scaleX(0.78);
transform-origin: center;
}
.handle-icon.wide::before,
.handle-icon.wide::after {
transform: scaleX(1.18);
transform-origin: center;
}
.candidate-name {
display: block;
font-size: 13px;
font-weight: 750;
}
.candidate-note {
display: block;
margin-top: 3px;
color: var(--muted);
font-size: 10px;
}
.candidate-badge {
min-width: 54px;
padding: 5px 7px;
color: #dce7e2;
text-align: center;
font-size: 9px;
font-weight: 800;
letter-spacing: 0.08em;
text-transform: uppercase;
border-radius: 8px;
background: rgba(255, 255, 255, 0.08);
}
.candidate[data-id="match"] .candidate-badge {
color: #153a30;
background: var(--green);
}
.readout {
--signal: var(--yellow);
margin-top: 17px;
padding: 15px;
border: 1px solid color-mix(in srgb, var(--signal) 28%, transparent);
border-radius: 14px;
background:
linear-gradient(
135deg,
color-mix(in srgb, var(--signal) 9%, transparent),
transparent 70%
);
}
.readout[data-state="fit"] {
--signal: var(--green);
}
.readout[data-state="blocked"] {
--signal: var(--orange);
}
.state-head {
display: flex;
align-items: center;
gap: 8px;
color: var(--signal);
font-size: 9px;
font-weight: 850;
letter-spacing: 0.16em;
text-transform: uppercase;
}
.status-dot {
width: 7px;
height: 7px;
border-radius: 50%;
background: var(--signal);
box-shadow: 0 0 0 4px color-mix(in srgb, var(--signal) 13%, transparent);
}
.readout strong {
display: block;
margin-top: 9px;
font-size: 16px;
}
.readout p {
min-height: 54px;
margin: 7px 0 12px;
color: var(--muted);
font-size: 11px;
line-height: 1.55;
}
.metric-grid {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 7px;
}
.metric {
padding: 8px;
border-radius: 9px;
background: rgba(0, 0, 0, 0.14);
}
.metric span {
display: block;
color: #82958d;
font-size: 8px;
letter-spacing: 0.1em;
text-transform: uppercase;
}
.metric b {
display: block;
margin-top: 4px;
color: #e8efeb;
font-size: 11px;
font-variant-numeric: tabular-nums;
}
.progress-track {
height: 3px;
margin-top: 13px;
overflow: hidden;
border-radius: 3px;
background: rgba(255, 255, 255, 0.08);
}
#progressFill {
width: 0%;
height: 100%;
background: var(--signal);
border-radius: inherit;
}
.replay {
width: 100%;
margin-top: 12px;
padding: 10px 14px;
color: #dfe9e4;
font-size: 11px;
font-weight: 750;
letter-spacing: 0.04em;
border: 1px solid rgba(255, 255, 255, 0.11);
border-radius: 10px;
background: rgba(255, 255, 255, 0.055);
cursor: pointer;
}
.replay:hover {
background: rgba(255, 255, 255, 0.1);
}
.world-label {
position: fixed;
z-index: 8;
display: none;
transform: translate(-50%, -50%);
pointer-events: none;
white-space: nowrap;
color: #f3f8f5;
font-size: 10px;
font-weight: 800;
letter-spacing: 0.07em;
text-transform: uppercase;
border: 1px solid rgba(255, 255, 255, 0.17);
border-radius: 8px;
padding: 7px 9px;
background: rgba(22, 36, 32, 0.84);
box-shadow: 0 7px 20px rgba(16, 31, 26, 0.16);
backdrop-filter: blur(8px);
}
.world-label.green {
color: #18392f;
border-color: rgba(255, 255, 255, 0.24);
background: rgba(143, 224, 193, 0.9);
}
.world-label.orange {
color: #32170d;
border-color: rgba(255, 255, 255, 0.2);
background: rgba(240, 138, 100, 0.92);
}
.hint {
position: fixed;
z-index: 10;
left: 28px;
bottom: 24px;
display: flex;
align-items: center;
gap: 12px;
color: #29463d;
font-size: 11px;
font-weight: 650;
pointer-events: none;
}
.hint span {
padding: 7px 10px;
border: 1px solid rgba(34, 68, 58, 0.18);
border-radius: 8px;
background: rgba(241, 247, 244, 0.62);
backdrop-filter: blur(8px);
}
#resetView {
pointer-events: auto;
color: #29463d;
cursor: pointer;
}
@media (max-width: 760px) {
.brand {
top: 15px;
left: 15px;
}
.brand small {
display: none;
}
.control-panel {
top: auto;
right: 10px;
bottom: 10px;
left: 10px;
width: auto;
max-height: 48vh;
padding: 15px;
overflow-y: auto;
border-radius: 17px;
}
.control-panel h1,
.intro,
.panel-spec {
display: none;
}
.candidate-list {
grid-template-columns: repeat(3, 1fr);
}
.candidate {
grid-template-columns: 1fr;
justify-items: center;
padding: 8px 4px;
text-align: center;
}
.handle-icon {
transform: scale(0.82);
}
.candidate-note,
.candidate-badge {
display: none;
}
.readout {
margin-top: 10px;
}
.readout p {
min-height: 0;
}
.metric-grid {
display: none;
}
.hint {
display: none;
}
}
</style>
</head>
<body>
<canvas id="scene" aria-label="Interactive low-poly cabinet handle fit demonstration"></canvas>
<div class="brand">
<div class="brand-mark"><span></span></div>
<div>
<b>Cabinetry Lab</b>
<small>Hardware fit study</small>
</div>
</div>
<section class="control-panel" aria-label="Handle fit controls">
<p class="eyebrow">Low-poly hardware test</p>
<h1>Handle fit check</h1>
<p class="intro">
Select a handle and watch it approach the drawer. Correct spacing aligns
with both holes; incorrect spacing stops at solid wood.
</p>
<div class="panel-spec">
<span>Drawer hole spacing</span>
<b>192 mm</b>
</div>
<div class="candidate-list" role="group" aria-label="Candidate handle spacing">
<button class="candidate" data-id="narrow" aria-pressed="false">
<span class="handle-icon narrow" aria-hidden="true"></span>
<span>
<span class="candidate-name">160 mm</span>
<span class="candidate-note">32 mm narrower · 16 mm per stem</span>
</span>
<span class="candidate-badge">Offset</span>
</button>
<button class="candidate active" data-id="match" aria-pressed="true">
<span class="handle-icon" aria-hidden="true"></span>
<span>
<span class="candidate-name">192 mm</span>
<span class="candidate-note">Matches both mounting centers</span>
</span>
<span class="candidate-badge">Exact</span>
</button>
<button class="candidate" data-id="wide" aria-pressed="false">
<span class="handle-icon wide" aria-hidden="true"></span>
<span>
<span class="candidate-name">224 mm</span>
<span class="candidate-note">32 mm wider · 16 mm per stem</span>
</span>
<span class="candidate-badge">Offset</span>
</button>
</div>
<div class="readout" id="readout" data-state="testing" aria-live="polite">
<div class="state-head">
<span class="status-dot"></span>
<span id="stateKicker">Checking alignment</span>
</div>
<strong id="resultTitle">Preparing the fit test</strong>
<p id="resultText">
The candidate handle will move toward the drawer front.
</p>
<div class="metric-grid">
<div class="metric">
<span>Handle</span>
<b id="metricHandle">192 mm</b>
</div>
<div class="metric">
<span>Per stem</span>
<b id="metricOffset">0 mm</b>
</div>
<div class="metric">
<span>Result</span>
<b id="metricResult">Testing</b>
</div>
</div>
<div class="progress-track">
<div id="progressFill"></div>
</div>
</div>
<button class="replay" id="replay">Replay approach</button>
</section>
<div class="world-label green" id="holeLabel">192 mm between holes</div>
<div class="world-label" id="handleLabel">192 mm handle</div>
<div class="hint">
<span>Drag to orbit · Scroll to zoom</span>
<span id="resetView">Reset view</span>
</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 readout = document.querySelector("#readout");
const stateKicker = document.querySelector("#stateKicker");
const resultTitle = document.querySelector("#resultTitle");
const resultText = document.querySelector("#resultText");
const metricHandle = document.querySelector("#metricHandle");
const metricOffset = document.querySelector("#metricOffset");
const metricResult = document.querySelector("#metricResult");
const progressFill = document.querySelector("#progressFill");
const handleLabel = document.querySelector("#handleLabel");
const holeLabel = document.querySelector("#holeLabel");
const candidateButtons = [...document.querySelectorAll(".candidate")];
// Scene constants. One world unit represents 80 mm.
const MM_PER_UNIT = 80;
const TARGET_SPACING_MM = 192;
const TARGET_SPACING = TARGET_SPACING_MM / MM_PER_UNIT;
const HOLE_Y = 0.2;
const PANEL_FRONT = 0.055;
const START_Z = 3.25;
const SEATED_Z = 0.63;
const BLOCKED_Z = 0.795;
const candidates = {
narrow: {
id: "narrow",
spacingMM: 160,
spacing: 160 / MM_PER_UNIT,
valid: false,
color: 0xc96948
},
match: {
id: "match",
spacingMM: 192,
spacing: TARGET_SPACING,
valid: true,
color: 0x253c37
},
wide: {
id: "wide",
spacingMM: 224,
spacing: 224 / MM_PER_UNIT,
valid: false,
color: 0xc96948
}
};
// Renderer.
const renderer = new THREE.WebGLRenderer({
canvas,
antialias: true,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight, false);
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.08;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xcbd6d1);
scene.fog = new THREE.Fog(0xcbd6d1, 11, 22);
const camera = new THREE.PerspectiveCamera(
39,
window.innerWidth / window.innerHeight,
0.1,
50
);
// Direct camera controls: no addons are used.
const cameraState = {
target: new THREE.Vector3(0, 0.05, 0.15),
theta: 0.2,
phi: 1.22,
radius: 9.2,
desiredTheta: 0.2,
desiredPhi: 1.22,
desiredRadius: 9.2
};
function updateCamera() {
cameraState.theta +=
(cameraState.desiredTheta - cameraState.theta) * 0.11;
cameraState.phi +=
(cameraState.desiredPhi - cameraState.phi) * 0.11;
cameraState.radius +=
(cameraState.desiredRadius - cameraState.radius) * 0.11;
const sinPhi = Math.sin(cameraState.phi);
const cosPhi = Math.cos(cameraState.phi);
camera.position.set(
cameraState.target.x +
cameraState.radius * sinPhi * Math.sin(cameraState.theta),
cameraState.target.y + cameraState.radius * cosPhi,
cameraState.target.z +
cameraState.radius * sinPhi * Math.cos(cameraState.theta)
);
camera.lookAt(cameraState.target);
camera.updateMatrixWorld();
}
function resetCamera() {
cameraState.desiredTheta = 0.2;
cameraState.desiredPhi = 1.22;
cameraState.desiredRadius = 9.2;
}
const pointer = {
active: false,
x: 0,
y: 0,
pointerId: null
};
canvas.addEventListener("pointerdown", (event) => {
if (event.button !== 0) return;
pointer.active = true;
pointer.x = event.clientX;
pointer.y = event.clientY;
pointer.pointerId = event.pointerId;
canvas.setPointerCapture(event.pointerId);
canvas.classList.add("dragging");
});
canvas.addEventListener("pointermove", (event) => {
if (!pointer.active) return;
const dx = event.clientX - pointer.x;
const dy = event.clientY - pointer.y;
pointer.x = event.clientX;
pointer.y = event.clientY;
cameraState.desiredTheta -= dx * 0.005;
cameraState.desiredPhi = THREE.MathUtils.clamp(
cameraState.desiredPhi - dy * 0.004,
0.78,
1.48
);
});
function endPointer(event) {
if (!pointer.active) return;
pointer.active = false;
canvas.classList.remove("dragging");
if (
pointer.pointerId !== null &&
canvas.hasPointerCapture(pointer.pointerId)
) {
canvas.releasePointerCapture(pointer.pointerId);
}
pointer.pointerId = null;
}
canvas.addEventListener("pointerup", endPointer);
canvas.addEventListener("pointercancel", endPointer);
canvas.addEventListener(
"wheel",
(event) => {
event.preventDefault();
cameraState.desiredRadius = THREE.MathUtils.clamp(
cameraState.desiredRadius + event.deltaY * 0.005,
6.3,
12
);
},
{ passive: false }
);
canvas.addEventListener("dblclick", resetCamera);
document.querySelector("#resetView").addEventListener("click", resetCamera);
// Lighting.
const hemisphere = new THREE.HemisphereLight(
0xf1faf5,
0x655642,
2.15
);
scene.add(hemisphere);
const keyLight = new THREE.DirectionalLight(0xfff1d7, 3.1);
keyLight.position.set(4.5, 7, 6.5);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(2048, 2048);
keyLight.shadow.camera.left = -6;
keyLight.shadow.camera.right = 6;
keyLight.shadow.camera.top = 6;
keyLight.shadow.camera.bottom = -6;
keyLight.shadow.camera.near = 0.5;
keyLight.shadow.camera.far = 18;
keyLight.shadow.bias = -0.0003;
scene.add(keyLight);
const fillLight = new THREE.DirectionalLight(0xc8e6f5, 1.2);
fillLight.position.set(-5, 3, 4);
scene.add(fillLight);
// Materials.
const woodMaterial = new THREE.MeshStandardMaterial({
color: 0x9e6c47,
roughness: 0.88,
metalness: 0.02,
flatShading: true
});
const darkWoodMaterial = new THREE.MeshStandardMaterial({
color: 0x67442f,
roughness: 0.92,
metalness: 0.01,
flatShading: true
});
const faceMaterial = new THREE.MeshStandardMaterial({
color: 0xb9875e,
roughness: 0.86,
metalness: 0.01,
flatShading: true
});
const insetMaterial = new THREE.MeshStandardMaterial({
color: 0xc19369,
roughness: 0.82,
metalness: 0.01,
flatShading: true
});
const interiorMaterial = new THREE.MeshStandardMaterial({
color: 0x342b25,
roughness: 1,
flatShading: true
});
const metalMaterial = new THREE.MeshStandardMaterial({
color: 0xaeb7b5,
roughness: 0.32,
metalness: 0.78,
flatShading: true
});
const floorMaterial = new THREE.MeshStandardMaterial({
color: 0xb8c6c0,
roughness: 1,
metalness: 0
});
function addBox(parent, size, position, material, castShadow = true) {
const mesh = new THREE.Mesh(
new THREE.BoxGeometry(size[0], size[1], size[2]),
material
);
mesh.position.set(position[0], position[1], position[2]);
mesh.castShadow = castShadow;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
// Ground.
const floor = new THREE.Mesh(
new THREE.PlaneGeometry(30, 30),
floorMaterial
);
floor.rotation.x = -Math.PI / 2;
floor.position.y = -1.62;
floor.receiveShadow = true;
scene.add(floor);
// Drawer carcass and front.
const cabinet = new THREE.Group();
scene.add(cabinet);
addBox(
cabinet,
[5.95, 3.28, 1.62],
[0, 0, -1.02],
darkWoodMaterial
);
addBox(
cabinet,
[5.65, 3.08, 0.28],
[0, 0, -0.12],
woodMaterial
);
addBox(
cabinet,
[5.2, 2.68, 0.055],
[0, 0, 0.0275],
insetMaterial
);
addBox(
cabinet,
[0.28, 3.42, 1.78],
[-3.03, 0.02, -0.92],
woodMaterial
);
addBox(
cabinet,
[0.28, 3.42, 1.78],
[3.03, 0.02, -0.92],
woodMaterial
);
addBox(
cabinet,
[6.34, 0.3, 1.83],
[0, 1.7, -0.95],
woodMaterial
);
addBox(
cabinet,
[5.65, 0.26, 1.6],
[0, -1.49, -0.98],
woodMaterial
);
addBox(
cabinet,
[0.72, 0.18, 1.0],
[-2.35, -1.69, -0.75],
darkWoodMaterial
);
addBox(
cabinet,
[0.72, 0.18, 1.0],
[2.35, -1.69, -0.75],
darkWoodMaterial
);
// Sparse wood grain, intentionally simple and low-poly.
const grainMaterial = new THREE.LineBasicMaterial({
color: 0x704a33,
transparent: true,
opacity: 0.22
});
const grainPositions = [];
const grainXs = [-2.15, -1.55, -0.85, -0.15, 0.55, 1.35, 2.05];
for (const x of grainXs) {
grainPositions.push(
x, -1.24, 0.058,
x + 0.035, 1.24, 0.058
);
}
const grainGeometry = new THREE.BufferGeometry();
grainGeometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(grainPositions, 3)
);
cabinet.add(new THREE.LineSegments(grainGeometry, grainMaterial));
// Actual cabinet holes.
const holeDiskMaterial = new THREE.MeshStandardMaterial({
color: 0x171b19,
roughness: 0.95,
metalness: 0.05,
flatShading: true
});
const holeRingMaterial = new THREE.MeshStandardMaterial({
color: 0x6e4d37,
roughness: 0.75,
metalness: 0.05,
flatShading: true
});
const holeXs = [-TARGET_SPACING / 2, TARGET_SPACING / 2];
for (const x of holeXs) {
const disk = new THREE.Mesh(
new THREE.CylinderGeometry(0.112, 0.112, 0.018, 12),
holeDiskMaterial
);
disk.rotation.x = Math.PI / 2;
disk.position.set(x, HOLE_Y, 0.058);
cabinet.add(disk);
const ring = new THREE.Mesh(
new THREE.TorusGeometry(0.115, 0.018, 6, 12),
holeRingMaterial
);
ring.position.set(x, HOLE_Y, 0.069);
cabinet.add(ring);
}
// Dimension line on the drawer front.
const dimensionMaterial = new THREE.LineBasicMaterial({
color: 0xf0dfc5,
transparent: true,
opacity: 0.74
});
const dimensionY = -0.5;
const dimensionPositions = [
-TARGET_SPACING / 2, dimensionY, 0.075,
TARGET_SPACING / 2, dimensionY, 0.075,
-TARGET_SPACING / 2, dimensionY - 0.09, 0.075,
-TARGET_SPACING / 2, dimensionY + 0.09, 0.075,
TARGET_SPACING / 2, dimensionY - 0.09, 0.075,
TARGET_SPACING / 2, dimensionY + 0.09, 0.075
];
const dimensionGeometry = new THREE.BufferGeometry();
dimensionGeometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(dimensionPositions, 3)
);
cabinet.add(new THREE.LineSegments(dimensionGeometry, dimensionMaterial));
// Guide rails show the required 192 mm hole centers.
const guideMaterial = new THREE.LineDashedMaterial({
color: 0x8fe0c1,
dashSize: 0.1,
gapSize: 0.075,
transparent: true,
opacity: 0.27,
depthWrite: false
});
for (const x of holeXs) {
const geometry = new THREE.BufferGeometry().setFromPoints([
new THREE.Vector3(x, HOLE_Y, 0.1),
new THREE.Vector3(x, HOLE_Y, START_Z + 0.18)
]);
const line = new THREE.Line(geometry, guideMaterial);
line.computeLineDistances();
scene.add(line);
}
// A translucent ideal handle remains visible during mismatch tests.
const ghostGroup = new THREE.Group();
ghostGroup.position.set(0, HOLE_Y, SEATED_Z);
scene.add(ghostGroup);
const ghostBarMaterial = new THREE.MeshBasicMaterial({
color: 0x8fe0c1,
transparent: true,
opacity: 0.075,
depthWrite: false
});
const ghostStemMaterial = new THREE.MeshBasicMaterial({
color: 0x8fe0c1,
transparent: true,
opacity: 0.25,
depthWrite: false
});
const ghostBar = new THREE.Mesh(
new THREE.CylinderGeometry(0.13, 0.13, TARGET_SPACING + 0.65, 8),
ghostBarMaterial
);
ghostBar.rotation.z = Math.PI / 2;
ghostGroup.add(ghostBar);
for (const x of holeXs) {
const stem = new THREE.Mesh(
new THREE.CylinderGeometry(0.055, 0.055, 0.7, 8),
ghostStemMaterial
);
stem.rotation.x = Math.PI / 2;
stem.position.set(x, 0, -0.35);
ghostGroup.add(stem);
}
// Red collision markers and dashed offset lines.
const contactMarkers = [];
const offsetLines = [];
for (let i = 0; i < 2; i++) {
const markerMaterial = new THREE.MeshBasicMaterial({
color: 0xef7954,
transparent: true,
opacity: 0,
depthWrite: false,
side: THREE.DoubleSide
});
const marker = new THREE.Group();
const disk = new THREE.Mesh(
new THREE.CircleGeometry(0.088, 12),
markerMaterial
);
disk.position.z = 0.073;
marker.add(disk);
const ring = new THREE.Mesh(
new THREE.TorusGeometry(0.092, 0.016, 6, 12),
markerMaterial
);
ring.position.z = 0.076;
marker.add(ring);
marker.position.y = HOLE_Y;
scene.add(marker);
contactMarkers.push({ marker, material: markerMaterial });
const lineMaterial = new THREE.LineDashedMaterial({
color: 0xef7954,
dashSize: 0.055,
gapSize: 0.04,
transparent: true,
opacity: 0,
depthWrite: false
});
const line = new THREE.Line(
new THREE.BufferGeometry().setFromPoints([
new THREE.Vector3(),
new THREE.Vector3()
]),
lineMaterial
);
scene.add(line);
offsetLines.push({ line, material: lineMaterial });
}
// Active candidate handle.
let activeCandidate = candidates.match;
let handleGroup = null;
let motionStart = performance.now();
let motionDuration = 1550;
let motionComplete = false;
function disposeGroup(group) {
if (!group) return;
scene.remove(group);
group.traverse((object) => {
if (object.geometry) object.geometry.dispose();
if (object.material) {
const materials = Array.isArray(object.material)
? object.material
: [object.material];
for (const material of materials) material.dispose();
}
});
}
function createHandle(candidate) {
const group = new THREE.Group();
group.position.set(0, HOLE_Y, START_Z);
const barMaterial = new THREE.MeshStandardMaterial({
color: candidate.color,
roughness: 0.42,
metalness: 0.48,
flatShading: true
});
const collarMaterial = new THREE.MeshStandardMaterial({
color: candidate.valid ? 0x8fe0c1 : 0xee815e,
roughness: 0.5,
metalness: 0.2,
flatShading: true
});
const barLength = candidate.spacing + 0.65;
const bar = new THREE.Mesh(
new THREE.CylinderGeometry(0.125, 0.125, barLength, 8),
barMaterial
);
bar.rotation.z = Math.PI / 2;
bar.castShadow = true;
group.add(bar);
for (const side of [-1, 1]) {
const cap = new THREE.Mesh(
new THREE.IcosahedronGeometry(0.128, 0),
barMaterial
);
cap.position.x = side * (barLength / 2 - 0.11);
cap.castShadow = true;
group.add(cap);
const stem = new THREE.Mesh(
new THREE.CylinderGeometry(0.055, 0.055, 0.72, 8),
metalMaterial
);
stem.rotation.x = Math.PI / 2;
stem.position.set(side * candidate.spacing / 2, 0, -0.36);
stem.castShadow = true;
group.add(stem);
const collar = new THREE.Mesh(
new THREE.CylinderGeometry(0.115, 0.115, 0.075, 8),
collarMaterial
);
collar.rotation.x = Math.PI / 2;
collar.position.set(side * candidate.spacing / 2, 0, -0.075);
collar.castShadow = true;
group.add(collar);
}
scene.add(group);
return group;
}
function setReadoutState(state, title, detail) {
readout.dataset.state = state;
stateKicker.textContent =
state === "fit"
? "Fit confirmed"
: state === "blocked"
? "Motion safely stopped"
: "Checking alignment";
resultTitle.textContent = title;
resultText.textContent = detail;
}
function updateCandidateUI() {
const perStemOffset = Math.abs(
activeCandidate.spacingMM - TARGET_SPACING_MM
) / 2;
metricHandle.textContent = `${activeCandidate.spacingMM} mm`;
metricOffset.textContent = `${perStemOffset} mm`;
metricResult.textContent = activeCandidate.valid ? "Fit" : "Blocked";
handleLabel.textContent = activeCandidate.valid
? `${activeCandidate.spacingMM} mm · both holes aligned`
: `${activeCandidate.spacingMM} mm · ${perStemOffset} mm offset per stem`;
handleLabel.className = `world-label ${
activeCandidate.valid ? "green" : "orange"
}`;
for (const button of candidateButtons) {
const selected = button.dataset.id === activeCandidate.id;
button.classList.toggle("active", selected);
button.setAttribute("aria-pressed", String(selected));
}
}
function selectCandidate(id) {
activeCandidate = candidates[id];
disposeGroup(handleGroup);
handleGroup = createHandle(activeCandidate);
motionStart = performance.now();
motionDuration = activeCandidate.valid ? 1550 : 1350;
motionComplete = false;
progressFill.style.width = "0%";
updateCandidateUI();
if (activeCandidate.valid) {
setReadoutState(
"testing",
"Aligning with both mounting holes",
"The 192 mm centers are being matched to the two cabinet holes."
);
} else {
setReadoutState(
"testing",
"Approaching the drawer front",
"The handle is moving toward the panel. Its stem centers will be checked before insertion."
);
}
// Move collision markers to the candidate's actual stem centers.
for (let i = 0; i < 2; i++) {
const sign = i === 0 ? -1 : 1;
const candidateX = sign * activeCandidate.spacing / 2;
const holeX = holeXs[i];
contactMarkers[i].marker.position.x = candidateX;
const positions = offsetLines[i].line.geometry.attributes.position;
positions.setXYZ(
0,
holeX,
HOLE_Y,
PANEL_FRONT + 0.025
);
positions.setXYZ(
1,
candidateX,
HOLE_Y,
PANEL_FRONT + 0.025
);
positions.needsUpdate = true;
offsetLines[i].line.geometry.computeBoundingSphere();
offsetLines[i].line.computeLineDistances();
}
}
candidateButtons.forEach((button) => {
button.addEventListener("click", () => {
selectCandidate(button.dataset.id);
});
});
document.querySelector("#replay").addEventListener("click", () => {
selectCandidate(activeCandidate.id);
});
function easeOutCubic(value) {
return 1 - Math.pow(1 - value, 3);
}
function smoothRange(value, start, end) {
const t = THREE.MathUtils.clamp((value - start) / (end - start), 0, 1);
return t * t * (3 - 2 * t);
}
const projected = new THREE.Vector3();
function positionLabel(element, worldPosition) {
projected.copy(worldPosition).project(camera);
const visible =
projected.z > -1 &&
projected.z < 1 &&
Math.abs(projected.x) < 1.25 &&
Math.abs(projected.y) < 1.25;
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 resize() {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight, false);
}
window.addEventListener("resize", resize);
// Begin with the exact-fit candidate seated on both holes.
selectCandidate("match");
updateCamera();
function animate(now) {
requestAnimationFrame(animate);
const progress = THREE.MathUtils.clamp(
(now - motionStart) / motionDuration,
0,
1
);
const eased = easeOutCubic(progress);
if (!motionComplete) {
const targetZ = activeCandidate.valid ? SEATED_Z : BLOCKED_Z;
handleGroup.position.z = THREE.MathUtils.lerp(
START_Z,
targetZ,
eased
);
progressFill.style.width = `${progress * 100}%`;
if (progress >= 1) {
motionComplete = true;
if (activeCandidate.valid) {
setReadoutState(
"fit",
"Seated on both holes",
"The 192 mm stem spacing matches both cabinet holes. Both mounting stems enter their holes at the same time."
);
} else {
const direction =
activeCandidate.spacingMM < TARGET_SPACING_MM
? "inboard of"
: "outboard of";
setReadoutState(
"blocked",
"Stopped before the panel",
`Each stem is 16 mm ${direction} its hole. The stems meet solid wood, so the handle remains visibly offset instead of being forced through the drawer.`
);
}
}
}
const collisionVisibility = activeCandidate.valid
? 0
: smoothRange(progress, 0.58, 0.94);
for (const entry of contactMarkers) {
entry.material.opacity = collisionVisibility * 0.82;
entry.marker.visible = collisionVisibility > 0.002;
}
for (const entry of offsetLines) {
entry.material.opacity = collisionVisibility * 0.88;
entry.line.visible = collisionVisibility > 0.002;
}
// Keep a subtle ideal envelope during failed tests.
ghostGroup.visible = !activeCandidate.valid;
ghostBarMaterial.opacity = activeCandidate.valid ? 0 : 0.075;
ghostStemMaterial.opacity = activeCandidate.valid ? 0 : 0.25;
updateCamera();
positionLabel(
holeLabel,
new THREE.Vector3(0, dimensionY - 0.2, PANEL_FRONT + 0.02)
);
positionLabel(
handleLabel,
new THREE.Vector3(
0,
HOLE_Y + 0.64,
handleGroup.position.z + 0.05
)
);
renderer.render(scene, camera);
}
requestAnimationFrame(animate);
</script>
</body>
</html>
```