Prompt specification
Build or customize the Three.js 3D scene "Button and reinforced buttonhole" 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: Button and reinforced buttonhole
Category: Everyday & Industrial · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/button-and-reinforced-buttonhole-x0569
Scene Description Prompt:
Please make a stylized Three.js scene in one standalone HTML file using cdnjs. Show a round button above a stitched slit in layered fabric; expose the button's stem and the reinforced opening so their retaining relationship is understandable.
Working Three.js Starter (button-and-reinforced-buttonhole-x0569):
```html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Button and Reinforced Slit — Three.js Study</title>
<style>
:root {
--ink: #172331;
--muted: #66727d;
--paper: #f4eee3;
--navy: #293b55;
--teal: #4f9b96;
--clay: #c87358;
--gold: #d7a84c;
--cream: #f5dfb0;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background:
radial-gradient(circle at 46% 38%, #fffaf0 0%, #eee5d7 66%, #ddd1c1 100%);
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;
}
#title-card {
position: absolute;
top: 26px;
left: 28px;
width: min(330px, calc(100vw - 56px));
pointer-events: none;
z-index: 5;
}
.eyebrow {
margin-bottom: 8px;
color: #9b7650;
font-size: 11px;
font-weight: 800;
letter-spacing: 0.2em;
text-transform: uppercase;
}
h1 {
margin: 0;
font-family: Georgia, "Times New Roman", serif;
font-size: clamp(28px, 3.2vw, 46px);
font-weight: 500;
line-height: 0.98;
letter-spacing: -0.035em;
}
#title-card p {
max-width: 300px;
margin: 13px 0 0;
color: var(--muted);
font-size: 13px;
line-height: 1.55;
}
.layer-key {
display: flex;
gap: 13px;
margin-top: 16px;
font-size: 10px;
font-weight: 750;
letter-spacing: 0.1em;
text-transform: uppercase;
}
.layer-key span {
display: inline-flex;
align-items: center;
gap: 6px;
}
.swatch {
width: 9px;
height: 9px;
border-radius: 50%;
box-shadow: 0 0 0 2px rgb(255 255 255 / 55%);
}
#controls {
position: absolute;
left: 50%;
bottom: 24px;
z-index: 10;
display: flex;
align-items: center;
gap: 18px;
width: min(620px, calc(100vw - 32px));
padding: 14px 16px 13px 18px;
border: 1px solid rgb(23 35 49 / 10%);
border-radius: 18px;
background: rgb(255 252 246 / 84%);
box-shadow: 0 16px 45px rgb(61 49 35 / 15%);
backdrop-filter: blur(16px);
transform: translateX(-50%);
}
.control-copy {
min-width: 190px;
flex: 1;
}
#state {
display: block;
margin-bottom: 9px;
font-size: 11px;
font-weight: 800;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.range-row {
display: flex;
align-items: center;
gap: 10px;
color: var(--muted);
font-size: 10px;
font-weight: 750;
letter-spacing: 0.09em;
text-transform: uppercase;
}
input[type="range"] {
width: 100%;
accent-color: var(--teal);
cursor: pointer;
}
#toggle {
flex: 0 0 auto;
min-width: 116px;
padding: 12px 15px;
border: 0;
border-radius: 12px;
background: var(--ink);
color: #fffaf1;
font: inherit;
font-size: 12px;
font-weight: 750;
cursor: pointer;
transition: transform 160ms ease, background 160ms ease;
}
#toggle:hover {
background: #294056;
transform: translateY(-1px);
}
#toggle:active {
transform: translateY(0);
}
#view-tools {
position: absolute;
top: 25px;
right: 26px;
z-index: 10;
display: flex;
gap: 8px;
}
.tool {
padding: 9px 12px;
border: 1px solid rgb(23 35 49 / 12%);
border-radius: 10px;
background: rgb(255 252 246 / 75%);
color: var(--ink);
font: inherit;
font-size: 11px;
font-weight: 750;
cursor: pointer;
backdrop-filter: blur(10px);
}
.tool:hover {
background: #fffaf1;
}
#camera-hint {
position: absolute;
right: 27px;
bottom: 30px;
z-index: 4;
color: rgb(23 35 49 / 55%);
font-size: 10px;
font-weight: 750;
letter-spacing: 0.08em;
text-transform: uppercase;
pointer-events: none;
}
#annotations {
position: absolute;
inset: 0;
width: 100%;
height: 100%;
overflow: visible;
pointer-events: none;
z-index: 3;
}
#annotations line {
stroke: rgb(23 35 49 / 48%);
stroke-width: 1;
}
#annotations circle {
fill: #fffaf1;
stroke: rgb(23 35 49 / 62%);
stroke-width: 1.5;
}
.callout {
position: absolute;
z-index: 4;
max-width: 160px;
padding: 6px 9px;
border: 1px solid rgb(23 35 49 / 10%);
border-radius: 8px;
background: rgb(255 252 246 / 88%);
box-shadow: 0 6px 20px rgb(61 49 35 / 9%);
color: var(--ink);
font-size: 9px;
font-weight: 850;
letter-spacing: 0.11em;
line-height: 1.35;
text-align: center;
text-transform: uppercase;
pointer-events: none;
transform: translate(-50%, -50%);
backdrop-filter: blur(8px);
}
.callout::after {
position: absolute;
right: 6px;
bottom: -5px;
width: 8px;
height: 8px;
border-right: 1px solid rgb(23 35 49 / 10%);
border-bottom: 1px solid rgb(23 35 49 / 10%);
background: rgb(255 252 246 / 88%);
content: "";
transform: rotate(45deg);
}
@media (max-width: 760px) {
#title-card {
top: 18px;
left: 18px;
}
#title-card p,
.layer-key,
#camera-hint,
.callout,
#annotations {
display: none;
}
#view-tools {
top: 18px;
right: 18px;
}
#controls {
bottom: 14px;
gap: 10px;
padding: 12px;
}
.control-copy {
min-width: 0;
}
#toggle {
min-width: 98px;
padding-inline: 10px;
}
}
</style>
</head>
<body>
<div id="title-card">
<div class="eyebrow">Construction study · 01</div>
<h1>Button /<br>Reinforced slit</h1>
<p>
An exploded view of a shank button. Slide downward to seat the stem;
the wider capture bar bears against the underside of the opening.
</p>
<div class="layer-key" aria-label="Fabric layer colors">
<span><i class="swatch" style="background:#293b55"></i>Face</span>
<span><i class="swatch" style="background:#4f9b96"></i>Support</span>
<span><i class="swatch" style="background:#c87358"></i>Base</span>
</div>
</div>
<div id="view-tools">
<button class="tool" id="reset" type="button">Reset view</button>
</div>
<svg id="annotations" aria-hidden="true"></svg>
<div id="controls">
<div class="control-copy">
<span id="state">Exploded · stem aligned with opening</span>
<label class="range-row" for="assembly">
<span>Lift</span>
<input id="assembly" type="range" min="0" max="100" value="0">
<span>Seat</span>
</label>
</div>
<button id="toggle" type="button">Seat button</button>
</div>
<div id="camera-hint">Drag to orbit · Scroll to zoom</div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// ------------------------------------------------------------
// Renderer and scene
// ------------------------------------------------------------
const renderer = new THREE.WebGLRenderer({
antialias: true,
alpha: true,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.08;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.domElement.setAttribute("aria-label", "Interactive button and fabric scene");
document.body.prepend(renderer.domElement);
const scene = new THREE.Scene();
scene.fog = new THREE.Fog(0xeee6d9, 8.5, 16);
const camera = new THREE.PerspectiveCamera(
38,
window.innerWidth / window.innerHeight,
0.1,
100
);
const cameraTarget = new THREE.Vector3(0, 0.85, 0);
let cameraTheta = 0.67;
let cameraPhi = 0.83;
let cameraRadius = 6.7;
let targetTheta = cameraTheta;
let targetPhi = cameraPhi;
let targetRadius = cameraRadius;
scene.add(new THREE.HemisphereLight(0xfff7e8, 0x76808b, 2.05));
const keyLight = new THREE.DirectionalLight(0xffe6bd, 4.4);
keyLight.position.set(4.5, 7.5, 4.2);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(2048, 2048);
keyLight.shadow.camera.left = -5;
keyLight.shadow.camera.right = 5;
keyLight.shadow.camera.top = 5;
keyLight.shadow.camera.bottom = -5;
keyLight.shadow.camera.near = 0.5;
keyLight.shadow.camera.far = 18;
keyLight.shadow.bias = -0.00035;
scene.add(keyLight);
const rimLight = new THREE.DirectionalLight(0x9ccfe3, 2.0);
rimLight.position.set(-5, 4, -3);
scene.add(rimLight);
const warmFill = new THREE.PointLight(0xffbd7d, 28, 9, 2);
warmFill.position.set(2.8, 2.8, -2.7);
scene.add(warmFill);
// ------------------------------------------------------------
// Procedural woven textures
// ------------------------------------------------------------
function makeFabricTexture(base, light, dark) {
const canvas = document.createElement("canvas");
canvas.width = canvas.height = 256;
const ctx = canvas.getContext("2d");
ctx.fillStyle = base;
ctx.fillRect(0, 0, 256, 256);
for (let y = 0; y < 256; y += 4) {
ctx.strokeStyle = y % 8 === 0 ? light : dark;
ctx.globalAlpha = y % 8 === 0 ? 0.2 : 0.13;
ctx.lineWidth = 1;
ctx.beginPath();
ctx.moveTo(0, y + 0.5);
ctx.lineTo(256, y + 0.5);
ctx.stroke();
}
for (let x = 0; x < 256; x += 4) {
ctx.strokeStyle = x % 8 === 0 ? light : dark;
ctx.globalAlpha = x % 8 === 0 ? 0.16 : 0.1;
ctx.lineWidth = 1;
ctx.beginPath();
ctx.moveTo(x + 0.5, 0);
ctx.lineTo(x + 0.5, 256);
ctx.stroke();
}
ctx.globalAlpha = 0.12;
for (let i = 0; i < 90; i++) {
const x = (i * 47) % 256;
const y = (i * 83) % 256;
ctx.fillStyle = i % 2 ? light : dark;
ctx.fillRect(x, y, 7, 1);
}
const texture = new THREE.CanvasTexture(canvas);
texture.wrapS = texture.wrapT = THREE.RepeatWrapping;
texture.repeat.set(3.2, 3.2);
texture.colorSpace = THREE.SRGBColorSpace;
texture.anisotropy = renderer.capabilities.getMaxAnisotropy();
return texture;
}
const navyTexture = makeFabricTexture("#293b55", "#65768d", "#101d31");
const tealTexture = makeFabricTexture("#4f9b96", "#9bc9bd", "#2d6d70");
const clayTexture = makeFabricTexture("#c87358", "#e6a48b", "#8f493b");
const navyMaterial = new THREE.MeshStandardMaterial({
map: navyTexture,
bumpMap: navyTexture,
bumpScale: 0.018,
roughness: 0.95,
metalness: 0
});
const tealMaterial = new THREE.MeshStandardMaterial({
map: tealTexture,
bumpMap: tealTexture,
bumpScale: 0.015,
roughness: 0.96,
metalness: 0
});
const clayMaterial = new THREE.MeshStandardMaterial({
map: clayTexture,
bumpMap: clayTexture,
bumpScale: 0.016,
roughness: 0.97,
metalness: 0
});
const navyEdge = new THREE.MeshStandardMaterial({
color: 0x16263c,
roughness: 1
});
const tealEdge = new THREE.MeshStandardMaterial({
color: 0x2f6e6d,
roughness: 1
});
const clayEdge = new THREE.MeshStandardMaterial({
color: 0x884536,
roughness: 1
});
// ------------------------------------------------------------
// Shape and geometry helpers
// ------------------------------------------------------------
function roundedRectShape(width, height, radius) {
const x = -width / 2;
const y = -height / 2;
const shape = new THREE.Shape();
shape.moveTo(x + radius, y);
shape.lineTo(x + width - radius, y);
shape.quadraticCurveTo(x + width, y, x + width, y + radius);
shape.lineTo(x + width, y + height - radius);
shape.quadraticCurveTo(x + width, y + height, x + width - radius, y + height);
shape.lineTo(x + radius, y + height);
shape.quadraticCurveTo(x, y + height, x, y + height - radius);
shape.lineTo(x, y + radius);
shape.quadraticCurveTo(x, y, x + radius, y);
return shape;
}
function extrudedPatch(shape, depth, material, edgeMaterial) {
const geometry = new THREE.ExtrudeGeometry(shape, {
depth,
steps: 1,
curveSegments: 28,
bevelEnabled: true,
bevelThickness: 0.025,
bevelSize: 0.022,
bevelSegments: 3
});
geometry.rotateX(-Math.PI / 2);
geometry.computeVertexNormals();
return new THREE.Mesh(
geometry,
edgeMaterial ? [material, edgeMaterial] : material
);
}
function borderStitch(shape, y, color, dashSize = 0.11, gapSize = 0.075) {
const points = shape.getPoints(20).map(
point => new THREE.Vector3(point.x, y, -point.y)
);
points.push(points[0].clone());
const geometry = new THREE.BufferGeometry().setFromPoints(points);
const material = new THREE.LineDashedMaterial({
color,
dashSize,
gapSize,
transparent: true,
opacity: 0.72
});
const line = new THREE.Line(geometry, material);
line.computeLineDistances();
return line;
}
// ------------------------------------------------------------
// Layered fabric
// ------------------------------------------------------------
const bottomShape = roundedRectShape(5.25, 3.7, 0.3);
const middleShape = roundedRectShape(4.8, 3.25, 0.26);
const topShape = roundedRectShape(4.35, 1.68, 0.22);
const bottom = extrudedPatch(bottomShape, 0.13, clayMaterial, clayEdge);
bottom.position.y = 0;
bottom.castShadow = true;
bottom.receiveShadow = true;
scene.add(bottom);
const middle = extrudedPatch(middleShape, 0.1, tealMaterial, tealEdge);
middle.position.y = 0.15;
middle.castShadow = true;
middle.receiveShadow = true;
scene.add(middle);
// Add the actual opening to the top layer.
const openingRadiusX = 0.68;
const openingRadiusZ = 0.29;
const opening = new THREE.Path();
opening.absellipse(
0, 0,
openingRadiusX, openingRadiusZ,
0, Math.PI * 2,
true
);
topShape.holes.push(opening);
const top = extrudedPatch(topShape, 0.11, navyMaterial, navyEdge);
top.position.y = 0.31;
top.castShadow = true;
top.receiveShadow = true;
scene.add(top);
scene.add(borderStitch(bottomShape, 0.146, 0xf0c6a8, 0.13, 0.09));
scene.add(borderStitch(middleShape, 0.263, 0xb8e0cc, 0.11, 0.08));
scene.add(borderStitch(topShape, 0.437, 0xe8d6aa, 0.10, 0.075));
// A support oval visible through the actual hole.
const supportMaterial = new THREE.MeshStandardMaterial({
color: 0x68aaa0,
roughness: 0.92,
metalness: 0
});
const support = new THREE.Mesh(
new THREE.CircleGeometry(1, 64),
supportMaterial
);
support.rotation.x = -Math.PI / 2;
support.scale.set(0.86, 0.45, 1);
support.position.y = 0.257;
support.receiveShadow = true;
scene.add(support);
const supportStitchPoints = [];
for (let i = 0; i < 64; i++) {
const angle = (i / 64) * Math.PI * 2;
supportStitchPoints.push(
new THREE.Vector3(
0.84 * Math.cos(angle),
0.264,
0.43 * Math.sin(angle)
)
);
}
const supportStitchGeometry = new THREE.BufferGeometry().setFromPoints(
supportStitchPoints
);
const supportStitch = new THREE.Line(
supportStitchGeometry,
new THREE.LineDashedMaterial({
color: 0xcce5c7,
dashSize: 0.07,
gapSize: 0.05,
transparent: true,
opacity: 0.82
})
);
supportStitch.computeLineDistances();
scene.add(supportStitch);
// ------------------------------------------------------------
// Dense buttonhole stitching
// ------------------------------------------------------------
const threadMaterial = new THREE.MeshStandardMaterial({
color: 0xf0d79b,
roughness: 0.74,
metalness: 0.02
});
const stitchGeometry = new THREE.BoxGeometry(0.105, 0.026, 0.027);
const stitchCount = 56;
const stitchRadiusX = 0.78;
const stitchRadiusZ = 0.40;
for (let i = 0; i < stitchCount; i++) {
const angle = (i / stitchCount) * Math.PI * 2;
const x = stitchRadiusX * Math.cos(angle);
const z = stitchRadiusZ * Math.sin(angle);
const tangentX = -stitchRadiusX * Math.sin(angle);
const tangentZ = stitchRadiusZ * Math.cos(angle);
const rotationY = Math.atan2(-tangentZ, tangentX);
const stitch = new THREE.Mesh(stitchGeometry, threadMaterial);
stitch.position.set(x, 0.444, z);
stitch.rotation.y = rotationY;
stitch.castShadow = true;
scene.add(stitch);
}
// Bar-tack reinforcement at both ends of the opening.
const tackGeometry = new THREE.BoxGeometry(0.025, 0.028, 0.19);
for (const side of [-1, 1]) {
for (let i = 0; i < 3; i++) {
const tack = new THREE.Mesh(tackGeometry, threadMaterial);
tack.position.set(side * (0.69 + i * 0.055), 0.446, 0);
tack.castShadow = true;
scene.add(tack);
}
}
// ------------------------------------------------------------
// Round button and exposed shank
// ------------------------------------------------------------
const buttonGroup = new THREE.Group();
scene.add(buttonGroup);
const goldMaterial = new THREE.MeshStandardMaterial({
color: 0xd7a84c,
roughness: 0.25,
metalness: 0.78
});
const goldEdgeMaterial = new THREE.MeshStandardMaterial({
color: 0x9d6c26,
roughness: 0.3,
metalness: 0.75
});
const steelMaterial = new THREE.MeshStandardMaterial({
color: 0x7f9aa5,
roughness: 0.25,
metalness: 0.82
});
const button = new THREE.Mesh(
new THREE.CylinderGeometry(0.72, 0.72, 0.22, 72),
[goldMaterial, goldEdgeMaterial]
);
button.castShadow = true;
button.receiveShadow = true;
buttonGroup.add(button);
const buttonRim = new THREE.Mesh(
new THREE.TorusGeometry(0.61, 0.038, 14, 72),
goldMaterial
);
buttonRim.rotation.x = Math.PI / 2;
buttonRim.position.y = 0.113;
buttonRim.castShadow = true;
buttonGroup.add(buttonRim);
const inset = new THREE.Mesh(
new THREE.CylinderGeometry(0.57, 0.57, 0.018, 72),
new THREE.MeshStandardMaterial({
color: 0xe2b95e,
roughness: 0.3,
metalness: 0.72
})
);
inset.position.y = 0.116;
inset.castShadow = true;
buttonGroup.add(inset);
// Four dark recessed button holes.
const holeMaterial = new THREE.MeshStandardMaterial({
color: 0x49351d,
roughness: 0.75,
metalness: 0.2
});
const buttonHoleGeometry = new THREE.CylinderGeometry(
0.088, 0.088, 0.016, 28
);
for (const x of [-0.235, 0.235]) {
for (const z of [-0.235, 0.235]) {
const hole = new THREE.Mesh(buttonHoleGeometry, holeMaterial);
hole.position.set(x, 0.13, z);
buttonGroup.add(hole);
}
}
// Narrow stem under the button.
const stem = new THREE.Mesh(
new THREE.CylinderGeometry(0.095, 0.105, 0.64, 28),
steelMaterial
);
stem.position.y = -0.42;
stem.castShadow = true;
buttonGroup.add(stem);
const stemCollar = new THREE.Mesh(
new THREE.CylinderGeometry(0.145, 0.145, 0.075, 28),
steelMaterial
);
stemCollar.position.y = -0.135;
stemCollar.castShadow = true;
buttonGroup.add(stemCollar);
// Wide capture bar: wider than the short dimension of the slit.
const barShape = roundedRectShape(0.17, 0.74, 0.075);
const barGeometry = new THREE.ExtrudeGeometry(barShape, {
depth: 0.09,
bevelEnabled: true,
bevelThickness: 0.012,
bevelSize: 0.012,
bevelSegments: 3,
curveSegments: 16
});
barGeometry.rotateX(-Math.PI / 2);
barGeometry.computeVertexNormals();
const captureBar = new THREE.Mesh(barGeometry, steelMaterial);
captureBar.position.y = -0.83;
captureBar.castShadow = true;
buttonGroup.add(captureBar);
// ------------------------------------------------------------
// Insertion guide
// ------------------------------------------------------------
const guideGeometry = new THREE.BufferGeometry().setFromPoints([
new THREE.Vector3(0, 0.445, 0),
new THREE.Vector3(0, 1.5, 0)
]);
const guide = new THREE.Line(
guideGeometry,
new THREE.LineDashedMaterial({
color: 0x537c84,
dashSize: 0.09,
gapSize: 0.075,
transparent: true,
opacity: 0.65
})
);
guide.computeLineDistances();
scene.add(guide);
const guideHalo = new THREE.Mesh(
new THREE.RingGeometry(0.72, 0.755, 64),
new THREE.MeshBasicMaterial({
color: 0x8ac7bc,
transparent: true,
opacity: 0.28,
side: THREE.DoubleSide
})
);
guideHalo.rotation.x = -Math.PI / 2;
guideHalo.scale.y = 0.58;
guideHalo.position.y = 0.449;
scene.add(guideHalo);
// ------------------------------------------------------------
// Ground plane
// ------------------------------------------------------------
const ground = new THREE.Mesh(
new THREE.PlaneGeometry(30, 30),
new THREE.ShadowMaterial({
color: 0x6a5948,
opacity: 0.15
})
);
ground.rotation.x = -Math.PI / 2;
ground.position.y = -0.035;
ground.receiveShadow = true;
scene.add(ground);
// ------------------------------------------------------------
// Annotation system
// ------------------------------------------------------------
const annotationDefinitions = [
{
id: "button",
text: "Round button",
color: "#d7a84c",
offset: [-175, -62],
anchor: () => new THREE.Vector3(-0.46, 0.10, 0).add(buttonGroup.position)
},
{
id: "stem",
text: "Narrow stem",
color: "#7f9aa5",
offset: [165, -26],
anchor: () => new THREE.Vector3(0.11, -0.40, 0.06).add(buttonGroup.position)
},
{
id: "bar",
text: "Wide capture bar",
color: "#537c84",
offset: [-170, 55],
anchor: () => new THREE.Vector3(0.06, -0.79, 0.12).add(buttonGroup.position)
},
{
id: "opening",
text: "Stitched reinforced opening",
color: "#f0d79b",
offset: [165, 72],
anchor: () => new THREE.Vector3(0.69, 0.445, 0)
}
];
const svg = document.getElementById("annotations");
for (const definition of annotationDefinitions) {
const line = document.createElementNS(
"http://www.w3.org/2000/svg",
"line"
);
const circle = document.createElementNS(
"http://www.w3.org/2000/svg",
"circle"
);
circle.setAttribute("r", "3.5");
svg.append(line, circle);
const label = document.createElement("div");
label.className = "callout";
label.textContent = definition.text;
document.body.append(label);
definition.line = line;
definition.circle = circle;
definition.label = label;
}
function updateAnnotations() {
const width = renderer.domElement.clientWidth;
const height = renderer.domElement.clientHeight;
const scale = Math.min(1, width / 850);
for (const definition of annotationDefinitions) {
const projected = definition.anchor().project(camera);
const anchorX = (projected.x * 0.5 + 0.5) * width;
const anchorY = (-projected.y * 0.5 + 0.5) * height;
let endX = anchorX + definition.offset[0] * scale;
let endY = anchorY + definition.offset[1] * scale;
endX = THREE.MathUtils.clamp(endX, 90, width - 90);
endY = THREE.MathUtils.clamp(endY, 80, height - 115);
definition.line.setAttribute("x1", anchorX);
definition.line.setAttribute("y1", anchorY);
definition.line.setAttribute("x2", endX);
definition.line.setAttribute("y2", endY);
definition.circle.setAttribute("cx", anchorX);
definition.circle.setAttribute("cy", anchorY);
definition.label.style.left = `${endX}px`;
definition.label.style.top = `${endY}px`;
}
}
// ------------------------------------------------------------
// Assembly interaction
// ------------------------------------------------------------
const range = document.getElementById("assembly");
const toggle = document.getElementById("toggle");
const stateLabel = document.getElementById("state");
let assemblyTarget = 0;
let assembly = 0;
function setTarget(value) {
assemblyTarget = THREE.MathUtils.clamp(value, 0, 1);
range.value = Math.round(assemblyTarget * 100);
if (assemblyTarget > 0.92) {
stateLabel.textContent = "Seated · capture bar bears under the slit";
} else if (assemblyTarget < 0.08) {
stateLabel.textContent = "Exploded · stem aligned with opening";
} else {
stateLabel.textContent = "Moving · stem entering the reinforced opening";
}
toggle.textContent = assemblyTarget > 0.5 ? "Lift button" : "Seat button";
}
range.addEventListener("input", () => {
setTarget(Number(range.value) / 100);
});
toggle.addEventListener("click", () => {
setTarget(assemblyTarget > 0.5 ? 0 : 1);
});
// ------------------------------------------------------------
// Custom camera controls — no addons
// ------------------------------------------------------------
let dragging = false;
let previousPointerX = 0;
let previousPointerY = 0;
renderer.domElement.addEventListener("pointerdown", event => {
dragging = true;
previousPointerX = event.clientX;
previousPointerY = event.clientY;
renderer.domElement.setPointerCapture(event.pointerId);
});
renderer.domElement.addEventListener("pointermove", event => {
if (!dragging) return;
const deltaX = event.clientX - previousPointerX;
const deltaY = event.clientY - previousPointerY;
previousPointerX = event.clientX;
previousPointerY = event.clientY;
targetTheta -= deltaX * 0.008;
targetPhi = THREE.MathUtils.clamp(
targetPhi + deltaY * 0.006,
0.42,
1.32
);
});
function stopDragging(event) {
dragging = false;
if (event && renderer.domElement.hasPointerCapture(event.pointerId)) {
renderer.domElement.releasePointerCapture(event.pointerId);
}
}
renderer.domElement.addEventListener("pointerup", stopDragging);
renderer.domElement.addEventListener("pointercancel", stopDragging);
renderer.domElement.addEventListener(
"wheel",
event => {
event.preventDefault();
targetRadius = THREE.MathUtils.clamp(
targetRadius + event.deltaY * 0.0045,
4.3,
9.5
);
},
{ passive: false }
);
document.getElementById("reset").addEventListener("click", () => {
targetTheta = 0.67;
targetPhi = 0.83;
targetRadius = 6.7;
});
// ------------------------------------------------------------
// Resize and animation loop
// ------------------------------------------------------------
function resize() {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
}
window.addEventListener("resize", resize);
function animate() {
requestAnimationFrame(animate);
assembly += (assemblyTarget - assembly) * 0.075;
cameraTheta += (targetTheta - cameraTheta) * 0.08;
cameraPhi += (targetPhi - cameraPhi) * 0.08;
cameraRadius += (targetRadius - cameraRadius) * 0.08;
const explodedY = 2.32;
const seatedY = 1.06;
buttonGroup.position.y = THREE.MathUtils.lerp(
explodedY,
seatedY,
assembly
);
// Update the dashed insertion guide.
const guideEndY = buttonGroup.position.y - 0.86;
guide.geometry.attributes.position.setY(1, guideEndY);
guide.geometry.attributes.position.needsUpdate = true;
guide.computeLineDistances();
guide.visible = assembly < 0.94;
guideHalo.material.opacity = 0.28 * (1 - assembly * 0.7);
const spherical = new THREE.Spherical(
cameraRadius,
cameraPhi,
cameraTheta
);
camera.position.setFromSpherical(spherical).add(cameraTarget);
camera.lookAt(cameraTarget);
camera.updateMatrixWorld();
renderer.render(scene, camera);
updateAnnotations();
}
setTarget(0);
animate();
</script>
</body>
</html>
```