Prompt specification
Build or customize the Three.js 3D scene "Snap-tab enclosure section" 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: Snap-tab enclosure section
Category: Everyday & Industrial · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/snap-tab-enclosure-section-x0305
Scene Description Prompt:
Make a standalone HTML scene with Three.js from cdnjs that depicts snap-fit connectors. Model two plastic enclosure halves with one flexible cantilever tab and matching recess; show an enlarged side section exposing the retaining shoulder and entry ramp. Stylized solid forms.
Working Three.js Starter (snap-tab-enclosure-section-x0305):
```html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Snap-Fit Connector Study</title>
<style>
:root {
--ink: #17343b;
--muted: #60777b;
--teal: #1c9298;
--coral: #f06d50;
--gold: #ffc857;
--paper: #f5f8f6;
--line: rgba(23, 52, 59, 0.24);
}
* { box-sizing: border-box; }
html, body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #e8efec;
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; }
.header {
position: fixed;
inset: 24px 26px auto 28px;
z-index: 10;
display: flex;
justify-content: space-between;
align-items: flex-start;
pointer-events: none;
}
.eyebrow {
margin: 0 0 7px;
color: var(--teal);
font-size: 11px;
font-weight: 800;
letter-spacing: 0.18em;
text-transform: uppercase;
}
h1 {
margin: 0;
max-width: 470px;
font-size: clamp(25px, 3.2vw, 48px);
line-height: 0.98;
letter-spacing: -0.045em;
font-weight: 800;
}
.subtitle {
margin: 11px 0 0;
max-width: 430px;
color: var(--muted);
font-size: 13px;
line-height: 1.5;
}
.controls {
display: flex;
gap: 8px;
pointer-events: auto;
flex-wrap: wrap;
justify-content: flex-end;
}
button {
border: 1px solid rgba(23, 52, 59, 0.15);
border-radius: 999px;
padding: 9px 13px;
background: rgba(250, 252, 251, 0.82);
color: var(--ink);
font: inherit;
font-size: 11px;
font-weight: 750;
letter-spacing: 0.03em;
cursor: pointer;
box-shadow: 0 6px 24px rgba(28, 67, 69, 0.08);
backdrop-filter: blur(10px);
}
button:hover {
background: #fff;
transform: translateY(-1px);
}
button:active { transform: translateY(0); }
#leaders {
position: fixed;
inset: 0;
z-index: 4;
width: 100%;
height: 100%;
pointer-events: none;
overflow: visible;
}
#leaders line {
stroke: rgba(23, 52, 59, 0.42);
stroke-width: 1.1;
stroke-dasharray: 3 4;
}
#leaders circle {
fill: var(--paper);
stroke: rgba(23, 52, 59, 0.55);
stroke-width: 1.2;
}
.callout {
position: fixed;
z-index: 6;
width: 158px;
min-height: 48px;
display: flex;
align-items: center;
gap: 10px;
padding: 9px 11px;
border: 1px solid rgba(23, 52, 59, 0.13);
border-radius: 12px;
background: rgba(250, 252, 251, 0.9);
box-shadow: 0 10px 30px rgba(23, 52, 59, 0.11);
backdrop-filter: blur(12px);
pointer-events: none;
transition: opacity 160ms ease;
}
.callout .number {
display: grid;
place-items: center;
width: 25px;
height: 25px;
flex: 0 0 auto;
border-radius: 8px;
background: var(--ink);
color: white;
font-size: 9px;
font-weight: 850;
letter-spacing: 0.04em;
}
.callout strong {
display: block;
font-size: 11px;
line-height: 1.2;
letter-spacing: -0.01em;
}
.callout small {
display: block;
margin-top: 3px;
color: var(--muted);
font-size: 9px;
line-height: 1.2;
}
.callout.gold .number { background: var(--gold); color: var(--ink); }
.callout.teal .number { background: var(--teal); color: white; }
body.labels-hidden .callout,
body.labels-hidden #leaders {
opacity: 0;
}
.footer {
position: fixed;
z-index: 8;
left: 28px;
bottom: 22px;
display: flex;
align-items: center;
gap: 12px;
color: var(--muted);
font-size: 10px;
letter-spacing: 0.04em;
pointer-events: none;
}
.footer .dot {
width: 6px;
height: 6px;
border-radius: 50%;
background: var(--coral);
box-shadow: 0 0 0 5px rgba(240, 109, 80, 0.13);
}
.badge {
position: fixed;
z-index: 7;
right: 28px;
bottom: 22px;
padding: 8px 11px;
border: 1px solid rgba(23, 52, 59, 0.12);
border-radius: 999px;
background: rgba(250, 252, 251, 0.78);
color: var(--muted);
font-size: 9px;
font-weight: 800;
letter-spacing: 0.12em;
text-transform: uppercase;
pointer-events: none;
backdrop-filter: blur(8px);
}
@media (max-width: 760px) {
.header { inset: 18px 16px auto; }
.controls {
position: fixed;
top: auto;
right: 16px;
bottom: 52px;
max-width: 250px;
}
.subtitle { display: none; }
.callout, #leaders { display: none; }
.footer { left: 16px; bottom: 18px; }
.badge { display: none; }
h1 { max-width: 270px; }
}
</style>
</head>
<body>
<div class="header">
<div>
<p class="eyebrow">Mechanical study / 01</p>
<h1>Snap-fit enclosure joint</h1>
<p class="subtitle">
Two molded plastic halves, one flexible cantilever, and an enlarged
section through the positive retaining geometry.
</p>
</div>
<div class="controls">
<button id="assembleButton" type="button">Assemble halves</button>
<button id="labelsButton" type="button">Hide labels</button>
<button id="resetButton" type="button">Reset view</button>
</div>
</div>
<svg id="leaders" aria-hidden="true"></svg>
<div class="callout gold" id="cantileverCallout">
<span class="number">01</span>
<div>
<strong>Flexible cantilever</strong>
<small>single molded tongue</small>
</div>
</div>
<div class="callout teal" id="recessCallout">
<span class="number">02</span>
<div>
<strong>Matching recess</strong>
<small>receiver opening</small>
</div>
</div>
<div class="callout teal" id="shoulderCallout">
<span class="number">03</span>
<div>
<strong>Retaining shoulder</strong>
<small>positive snap surface</small>
</div>
</div>
<div class="callout" id="rampCallout">
<span class="number">04</span>
<div>
<strong>Entry ramp</strong>
<small>lead-in / flexing face</small>
</div>
</div>
<div class="footer">
<span class="dot"></span>
<span>DRAG TO ORBIT · SCROLL TO ZOOM · DOUBLE-CLICK TO RESET</span>
</div>
<div class="badge">Solid geometry / Section A–A</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 scene = new THREE.Scene();
scene.background = new THREE.Color(0xe8efec);
scene.fog = new THREE.Fog(0xe8efec, 17, 30);
const camera = new THREE.PerspectiveCamera(
40,
innerWidth / innerHeight,
0.1,
60
);
const renderer = new THREE.WebGLRenderer({
antialias: true,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(devicePixelRatio, 2));
renderer.setSize(innerWidth, innerHeight);
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.06;
document.body.prepend(renderer.domElement);
renderer.domElement.setAttribute(
"aria-label",
"Interactive three-dimensional model of a snap-fit enclosure connector"
);
// -----------------------------------------------------------------------
// Materials
// -----------------------------------------------------------------------
const materials = {
teal: new THREE.MeshStandardMaterial({
color: 0x1c9298,
roughness: 0.42,
metalness: 0.02
}),
tealDark: new THREE.MeshStandardMaterial({
color: 0x126a72,
roughness: 0.48,
metalness: 0.02
}),
tealLight: new THREE.MeshStandardMaterial({
color: 0x9bd9d2,
roughness: 0.58,
metalness: 0
}),
tealCut: new THREE.MeshStandardMaterial({
color: 0xb8e3dc,
roughness: 0.7,
metalness: 0
}),
coral: new THREE.MeshStandardMaterial({
color: 0xf06d50,
roughness: 0.38,
metalness: 0.02
}),
coralDark: new THREE.MeshStandardMaterial({
color: 0xc94d39,
roughness: 0.46,
metalness: 0.02
}),
coralLight: new THREE.MeshStandardMaterial({
color: 0xffa186,
roughness: 0.48,
metalness: 0
}),
gold: new THREE.MeshStandardMaterial({
color: 0xffc857,
roughness: 0.34,
metalness: 0.02
}),
goldDark: new THREE.MeshStandardMaterial({
color: 0xd89a28,
roughness: 0.42,
metalness: 0.02
}),
dark: new THREE.MeshStandardMaterial({
color: 0x17343b,
roughness: 0.68,
metalness: 0
}),
pale: new THREE.MeshStandardMaterial({
color: 0xe4ece8,
roughness: 0.85,
metalness: 0
}),
section: new THREE.MeshStandardMaterial({
color: 0xd8e3df,
roughness: 0.9,
metalness: 0
}),
ground: new THREE.MeshStandardMaterial({
color: 0xdfe8e4,
roughness: 0.96,
metalness: 0
})
};
// -----------------------------------------------------------------------
// Geometry helpers
// -----------------------------------------------------------------------
function roundedRectangleShape(width, height, radius) {
const w = width / 2;
const h = height / 2;
const r = Math.max(0.001, Math.min(radius, w, h));
const shape = new THREE.Shape();
shape.moveTo(-w + r, -h);
shape.lineTo(w - r, -h);
shape.quadraticCurveTo(w, -h, w, -h + r);
shape.lineTo(w, h - r);
shape.quadraticCurveTo(w, h, w - r, h);
shape.lineTo(-w + r, h);
shape.quadraticCurveTo(-w, h, -w, h - r);
shape.lineTo(-w, -h + r);
shape.quadraticCurveTo(-w, -h, -w + r, -h);
return shape;
}
function roundedBoxGeometry(width, height, depth, radius = 0.12) {
const bevel = Math.min(
radius,
width * 0.18,
height * 0.18,
depth * 0.18
);
const shape = roundedRectangleShape(
width - bevel * 2,
height - bevel * 2,
Math.max(0.012, radius - bevel)
);
const geometry = new THREE.ExtrudeGeometry(shape, {
depth: Math.max(0.001, depth - bevel * 2),
bevelEnabled: true,
bevelThickness: bevel,
bevelSize: bevel,
bevelOffset: 0,
bevelSegments: 3,
curveSegments: 8,
steps: 1
});
geometry.center();
return geometry;
}
function addRounded(
parent,
size,
position,
material,
radius = 0.12
) {
const mesh = new THREE.Mesh(
roundedBoxGeometry(size[0], size[1], size[2], radius),
material
);
mesh.position.set(position[0], position[1], position[2]);
mesh.castShadow = true;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
function addCylinder(
parent,
radii,
height,
position,
material,
segments = 24
) {
const mesh = new THREE.Mesh(
new THREE.CylinderGeometry(
radii[0],
radii[1],
height,
segments
),
material
);
mesh.position.set(position[0], position[1], position[2]);
mesh.castShadow = true;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
function polygonGeometry(points, depth, bevel = 0.025) {
const shape = new THREE.Shape();
shape.moveTo(points[0][0], points[0][1]);
for (let i = 1; i < points.length; i++) {
shape.lineTo(points[i][0], points[i][1]);
}
shape.closePath();
const geometry = new THREE.ExtrudeGeometry(shape, {
depth: Math.max(0.001, depth - bevel * 2),
bevelEnabled: bevel > 0,
bevelThickness: bevel,
bevelSize: bevel,
bevelOffset: 0,
bevelSegments: 2,
steps: 1
});
geometry.translate(0, 0, -depth / 2);
return geometry;
}
function addPolygon(
parent,
points,
depth,
frontMaterial,
sideMaterial,
bevel = 0.025
) {
const mesh = new THREE.Mesh(
polygonGeometry(points, depth, bevel),
[frontMaterial, sideMaterial]
);
mesh.castShadow = true;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
// -----------------------------------------------------------------------
// Lighting and ground
// -----------------------------------------------------------------------
const hemisphere = new THREE.HemisphereLight(0xf7fffb, 0x648084, 2.0);
scene.add(hemisphere);
const key = new THREE.DirectionalLight(0xfff8e8, 3.7);
key.position.set(-4, 10, 8);
key.castShadow = true;
key.shadow.mapSize.set(2048, 2048);
key.shadow.camera.left = -12;
key.shadow.camera.right = 12;
key.shadow.camera.top = 10;
key.shadow.camera.bottom = -10;
key.shadow.camera.near = 1;
key.shadow.camera.far = 30;
key.shadow.normalBias = 0.025;
scene.add(key);
const fill = new THREE.DirectionalLight(0xbfe8ee, 1.7);
fill.position.set(8, 4, -7);
scene.add(fill);
const rim = new THREE.DirectionalLight(0xffb596, 1.25);
rim.position.set(-7, 3, -5);
scene.add(rim);
const ground = new THREE.Mesh(
new THREE.PlaneGeometry(34, 24),
materials.ground
);
ground.rotation.x = -Math.PI / 2;
ground.position.y = -1.5;
ground.receiveShadow = true;
scene.add(ground);
// -----------------------------------------------------------------------
// Main exploded enclosure assembly
// -----------------------------------------------------------------------
const assembly = new THREE.Group();
scene.add(assembly);
const upperHalf = new THREE.Group();
assembly.add(upperHalf);
// Lower shell floor.
addRounded(
assembly,
[5.2, 0.38, 3.5],
[-2.7, -1.26, 0],
materials.teal,
0.18
);
addRounded(
assembly,
[4.82, 0.09, 3.12],
[-2.7, -1.035, 0],
materials.tealDark,
0.1
);
// Front wall is intentionally lower to expose the molded interior.
addRounded(
assembly,
[5.2, 0.73, 0.26],
[-2.7, -0.735, 1.62],
materials.teal,
0.1
);
addRounded(
assembly,
[5.2, 1.02, 0.26],
[-2.7, -0.55, -1.62],
materials.teal,
0.1
);
addRounded(
assembly,
[0.26, 1.02, 3.0],
[-5.18, -0.55, 0],
materials.teal,
0.1
);
// Right wall is segmented to create an actual matching recess.
addRounded(
assembly,
[0.28, 1.02, 1.8],
[-0.1, -0.55, -0.72],
materials.teal,
0.08
);
addRounded(
assembly,
[0.28, 1.02, 0.36],
[-0.1, -0.55, 1.44],
materials.teal,
0.08
);
addRounded(
assembly,
[0.28, 0.28, 1.08],
[-0.1, -0.16, 0.72],
materials.teal,
0.07
);
addRounded(
assembly,
[0.28, 0.36, 1.08],
[-0.1, -0.90, 0.72],
materials.teal,
0.07
);
// Dark interior visible through the snap opening.
addRounded(
assembly,
[0.06, 0.38, 0.96],
[-0.31, -0.51, 0.72],
materials.dark,
0.025
);
// Visible retaining ledge in the lower receptacle.
addRounded(
assembly,
[0.43, 0.12, 0.9],
[-0.035, -0.705, 0.72],
materials.tealDark,
0.035
);
// Entry ramp beneath the upper edge of the receptacle.
addPolygon(
assembly,
[
[-0.25, -0.30],
[0.04, -0.30],
[0.04, -0.405],
[-0.13, -0.475]
],
0.88,
materials.tealLight,
materials.teal,
0.018
);
// Interior strengthening ribs.
for (const x of [-4.15, -2.75, -1.35]) {
addRounded(
assembly,
[0.13, 0.48, 2.35],
[x, -0.79, -0.12],
materials.teal,
0.045
);
}
// Two molded screw bosses.
for (const x of [-4.35, -1.05]) {
addCylinder(
assembly,
[0.2, 0.23,],
0.53,
[x, -0.79, -0.98],
materials.teal
);
addCylinder(
assembly,
[0.09, 0.09],
0.025,
[x, -0.515, -0.98],
materials.dark
);
}
// Upper enclosure half.
addRounded(
upperHalf,
[5.2, 0.34, 3.5],
[-2.7, 1.68, 0],
materials.coral,
0.18
);
addRounded(
upperHalf,
[4.34, 0.08, 2.64],
[-2.7, 1.88, 0],
materials.coralLight,
0.12
);
addRounded(
upperHalf,
[5.2, 1.15, 0.26],
[-2.7, 0.98, 1.62],
materials.coral,
0.1
);
addRounded(
upperHalf,
[5.2, 1.15, 0.26],
[-2.7, 0.98, -1.62],
materials.coral,
0.1
);
addRounded(
upperHalf,
[0.26, 1.15, 3.0],
[-5.18, 0.98, 0],
materials.coral,
0.1
);
addRounded(
upperHalf,
[0.26, 1.15, 3.0],
[-0.1, 0.98, 0],
materials.coral,
0.1
);
// Molded ribs under the lid.
for (const z of [-0.82, 0, 0.82]) {
addRounded(
upperHalf,
[3.65, 0.22, 0.12],
[-2.7, 1.39, z],
materials.coralDark,
0.04
);
}
// The single flexible cantilever tab.
addRounded(
upperHalf,
[0.18, 1.15, 0.52],
[0.16, 0.58, 0.72],
materials.gold,
0.055
);
// Angled snap barb at the free end of the cantilever.
addPolygon(
upperHalf,
[
[0.06, 0.10],
[0.36, 0.10],
[0.36, -0.10],
[0.22, -0.23],
[0.06, -0.16]
],
0.54,
materials.gold,
materials.goldDark,
0.025
);
// Fine assembly-axis guides.
const guideMaterial = new THREE.LineDashedMaterial({
color: 0x6d8588,
dashSize: 0.12,
gapSize: 0.10,
transparent: true,
opacity: 0.55
});
for (const z of [0.32, 1.10]) {
const guide = new THREE.Line(
new THREE.BufferGeometry().setFromPoints([
new THREE.Vector3(0.29, -0.28, z),
new THREE.Vector3(0.29, 1.36, z)
]),
guideMaterial
);
guide.computeLineDistances();
scene.add(guide);
}
// -----------------------------------------------------------------------
// Enlarged side section
// -----------------------------------------------------------------------
const section = new THREE.Group();
section.position.y = 0.5;
scene.add(section);
// Pale presentation plate behind the section.
addRounded(
section,
[3.72, 3.78, 0.14],
[3.7, -0.25, -0.63],
materials.section,
0.22
);
// Small pedestal.
addRounded(
section,
[3.85, 0.14, 1.55],
[3.7, -1.94, 0],
materials.pale,
0.1
);
// Receiver body with a true open channel.
addPolygon(
section,
[
[2.25, -2.00],
[5.15, -2.00],
[5.15, -0.28],
[4.45, -0.28],
[4.45, -1.18],
[3.15, -1.18],
[3.15, -0.28],
[2.25, -0.28]
],
0.78,
materials.tealCut,
materials.teal,
0.035
);
// Entry ramp in the enlarged receiver.
addPolygon(
section,
[
[3.15, -0.28],
[3.76, -0.28],
[3.76, -0.59],
[3.15, -0.47]
],
0.80,
materials.tealLight,
materials.teal,
0.025
);
// Retaining shoulder / undercut.
addRounded(
section,
[0.48, 0.24, 0.80],
[4.26, -0.82, 0],
materials.tealDark,
0.055
);
// Enlarged lid portion.
addRounded(
section,
[2.42, 0.68, 0.78],
[3.72, 0.95, 0],
materials.coral,
0.17
);
addRounded(
section,
[1.95, 0.08, 0.52],
[3.72, 1.30, 0],
materials.coralLight,
0.08
);
// Enlarged flexible cantilever.
addRounded(
section,
[0.35, 1.34, 0.52],
[3.94, 0.10, 0],
materials.gold,
0.065
);
// Enlarged snap head.
addPolygon(
section,
[
[3.75, -0.59],
[4.55, -0.59],
[4.55, -0.72],
[4.16, -0.87],
[3.75, -0.79]
],
0.54,
materials.gold,
materials.goldDark,
0.028
);
// Subtle section hatching on the backing plate.
const hatchPositions = [];
for (let i = 0; i < 13; i++) {
const x = 2.12 + i * 0.27;
hatchPositions.push(x, -1.93, -0.548, x + 0.34, -1.40, -0.548);
}
const hatchGeometry = new THREE.BufferGeometry();
hatchGeometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(hatchPositions, 3)
);
const hatch = new THREE.LineSegments(
hatchGeometry,
new THREE.LineBasicMaterial({
color: 0xaebfba,
transparent: true,
opacity: 0.38
})
);
section.add(hatch);
// -----------------------------------------------------------------------
// Camera controls implemented directly in this HTML
// -----------------------------------------------------------------------
const cameraState = {
yaw: 0.62,
pitch: 0.43,
distance: 13.8,
target: new THREE.Vector3(0.0, -0.05, 0)
};
function updateCamera() {
const horizontal = Math.cos(cameraState.pitch) * cameraState.distance;
camera.position.set(
cameraState.target.x + Math.sin(cameraState.yaw) * horizontal,
cameraState.target.y + Math.sin(cameraState.pitch) * cameraState.distance,
cameraState.target.z + Math.cos(cameraState.yaw) * horizontal
);
camera.lookAt(cameraState.target);
}
function resetCamera() {
cameraState.yaw = 0.62;
cameraState.pitch = 0.43;
cameraState.distance = innerWidth < 760 ? 18 : 13.8;
updateCamera();
}
let dragging = false;
let pointerX = 0;
let pointerY = 0;
renderer.domElement.addEventListener("pointerdown", event => {
dragging = true;
pointerX = event.clientX;
pointerY = event.clientY;
renderer.domElement.setPointerCapture(event.pointerId);
});
renderer.domElement.addEventListener("pointermove", event => {
if (!dragging) return;
const dx = event.clientX - pointerX;
const dy = event.clientY - pointerY;
pointerX = event.clientX;
pointerY = event.clientY;
cameraState.yaw -= dx * 0.006;
cameraState.pitch = THREE.MathUtils.clamp(
cameraState.pitch + dy * 0.005,
0.12,
1.18
);
updateCamera();
});
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();
cameraState.distance = THREE.MathUtils.clamp(
cameraState.distance * Math.exp(event.deltaY * 0.001),
7.5,
21
);
updateCamera();
},
{ passive: false }
);
renderer.domElement.addEventListener("dblclick", resetCamera);
// -----------------------------------------------------------------------
// Assembly animation and UI
// -----------------------------------------------------------------------
let assemblyTarget = 0;
const assembleButton = document.getElementById("assembleButton");
assembleButton.addEventListener("click", () => {
assemblyTarget = assemblyTarget === 0 ? -0.95 : 0;
assembleButton.textContent =
assemblyTarget === 0 ? "Assemble halves" : "Explode halves";
});
const labelsButton = document.getElementById("labelsButton");
labelsButton.addEventListener("click", () => {
const hidden = document.body.classList.toggle("labels-hidden");
labelsButton.textContent = hidden ? "Show labels" : "Hide labels";
});
document.getElementById("resetButton").addEventListener("click", resetCamera);
// -----------------------------------------------------------------------
// Screen-space callouts
// -----------------------------------------------------------------------
const callouts = [
{
element: document.getElementById("cantileverCallout"),
anchor: new THREE.Vector3(3.94, 0.58, 0.28),
offset: new THREE.Vector2(-66, -86)
},
{
element: document.getElementById("recessCallout"),
anchor: new THREE.Vector3(3.62, -0.25, 0.43),
offset: new THREE.Vector2(-178, 28)
},
{
element: document.getElementById("shoulderCallout"),
anchor: new THREE.Vector3(4.31, -0.32, 0.43),
offset: new THREE.Vector2(128, 40)
},
{
element: document.getElementById("rampCallout"),
anchor: new THREE.Vector3(3.36, 0.13, 0.43),
offset: new THREE.Vector2(-184, 112)
}
];
const svgNS = "http://www.w3.org/2000/svg";
for (const item of callouts) {
item.line = document.createElementNS(svgNS, "line");
item.dot = document.createElementNS(svgNS, "circle");
item.dot.setAttribute("r", "3.2");
document.getElementById("leaders").append(item.line, item.dot);
}
function updateCallouts() {
const projected = new THREE.Vector3();
for (const item of callouts) {
projected.copy(item.anchor).project(camera);
const px = (projected.x * 0.5 + 0.5) * innerWidth;
const py = (-projected.y * 0.5 + 0.5) * innerHeight;
const card = item.element;
const width = card.offsetWidth;
const height = card.offsetHeight;
let left = px + item.offset.x;
let top = py + item.offset.y;
left = THREE.MathUtils.clamp(left, 12, innerWidth - width - 12);
top = THREE.MathUtils.clamp(top, 12, innerHeight - height - 12);
card.style.left = `${left}px`;
card.style.top = `${top}px`;
const centerX = left + width / 2;
const centerY = top + height / 2;
item.line.setAttribute("x1", centerX);
item.line.setAttribute("y1", centerY);
item.line.setAttribute("x2", px);
item.line.setAttribute("y2", py);
item.dot.setAttribute("cx", px);
item.dot.setAttribute("cy", py);
const visible = projected.z > -1 && projected.z < 1;
card.style.visibility = visible ? "visible" : "hidden";
item.line.style.visibility = visible ? "visible" : "hidden";
item.dot.style.visibility = visible ? "visible" : "hidden";
}
}
// -----------------------------------------------------------------------
// Resize and render loop
// -----------------------------------------------------------------------
function resize() {
camera.aspect = innerWidth / innerHeight;
camera.updateProjectionMatrix();
renderer.setPixelRatio(Math.min(devicePixelRatio, 2));
renderer.setSize(innerWidth, innerHeight);
const svg = document.getElementById("leaders");
svg.setAttribute("viewBox", `0 0 ${innerWidth} ${innerHeight}`);
}
addEventListener("resize", resize);
resize();
updateCamera();
renderer.setAnimationLoop(() => {
upperHalf.position.y += (assemblyTarget - upperHalf.position.y) * 0.075;
scene.updateMatrixWorld();
updateCallouts();
renderer.render(scene, camera);
});
</script>
</body>
</html>
```