Prompt specification
Build or customize the Three.js 3D scene "Snap latch engagement animation" 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 latch engagement animation
Category: Everyday & Industrial · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/snap-latch-engagement-animation-x0306
Scene Description Prompt:
Make a standalone HTML scene with Three.js from cdnjs that depicts snap-fit connectors. Animate the tab flexing over the entry ramp and returning into the recess; keep its root attached and show separate release motion before disassembly. Low-poly.
Working Three.js Starter (snap-latch-engagement-animation-x0306):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Snap-Fit Connector — Motion Study</title>
<style>
:root {
color-scheme: dark;
--ink: #eaf4f5;
--muted: #91a7ad;
--panel: rgba(12, 24, 31, 0.88);
--line: rgba(168, 208, 215, 0.18);
--orange: #ff7549;
--mint: #63e6c2;
--yellow: #ffd166;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #0a141a;
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;
}
.panel {
position: absolute;
z-index: 10;
border: 1px solid var(--line);
background: var(--panel);
backdrop-filter: blur(14px);
-webkit-backdrop-filter: blur(14px);
box-shadow: 0 18px 60px rgba(0, 0, 0, 0.25);
}
#title-card {
top: 24px;
left: 24px;
width: min(390px, calc(100vw - 48px));
padding: 18px 20px 17px;
border-radius: 4px;
}
.eyebrow {
color: var(--mint);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.2em;
text-transform: uppercase;
}
h1 {
margin: 7px 0 8px;
font-size: clamp(22px, 3vw, 32px);
line-height: 1;
letter-spacing: -0.045em;
}
.subtitle {
margin: 0;
max-width: 330px;
color: var(--muted);
font-size: 12px;
line-height: 1.55;
}
#phase-card {
top: 24px;
right: 24px;
width: 300px;
padding: 16px 18px;
border-radius: 4px;
}
.phase-row {
display: flex;
align-items: center;
justify-content: space-between;
gap: 14px;
}
#phase-number {
color: var(--orange);
font-size: 11px;
font-weight: 800;
letter-spacing: 0.14em;
text-transform: uppercase;
}
#phase-title {
color: var(--muted);
font-size: 11px;
font-weight: 700;
letter-spacing: 0.12em;
text-transform: uppercase;
}
#phase-description {
min-height: 38px;
margin: 12px 0 0;
color: var(--ink);
font-size: 12px;
line-height: 1.5;
}
.phase-progress {
height: 2px;
margin-top: 14px;
overflow: hidden;
background: rgba(255, 255, 255, 0.08);
}
#phase-progress-fill {
width: 0;
height: 100%;
background: var(--orange);
transition: width 80ms linear;
}
#legend {
left: 24px;
bottom: 136px;
display: flex;
flex-wrap: wrap;
gap: 8px;
max-width: calc(100vw - 48px);
padding: 10px 12px;
border-radius: 4px;
}
.legend-item {
display: flex;
align-items: center;
gap: 7px;
color: var(--muted);
font-size: 10px;
font-weight: 700;
letter-spacing: 0.06em;
text-transform: uppercase;
}
.swatch {
width: 9px;
height: 9px;
border-radius: 2px;
box-shadow: 0 0 0 1px rgba(255, 255, 255, 0.15);
}
#timeline {
right: 24px;
bottom: 24px;
left: 24px;
display: grid;
grid-template-columns: 1fr auto;
gap: 14px 18px;
align-items: center;
padding: 14px 16px;
border-radius: 4px;
}
#steps {
display: grid;
grid-template-columns: repeat(5, 1fr);
gap: 8px;
min-width: 0;
}
.step {
position: relative;
padding-top: 12px;
color: #71878d;
font-size: 9px;
font-weight: 800;
letter-spacing: 0.09em;
text-transform: uppercase;
transition: color 160ms ease;
}
.step::before,
.step::after {
position: absolute;
top: 0;
left: 0;
height: 3px;
content: "";
}
.step::before {
width: 100%;
background: rgba(255, 255, 255, 0.08);
}
.step::after {
width: var(--progress, 0%);
background: var(--orange);
}
.step.active {
color: var(--ink);
}
.step:nth-child(4)::after {
background: var(--mint);
}
#controls {
display: flex;
gap: 7px;
}
button {
min-width: 40px;
height: 34px;
padding: 0 11px;
border: 1px solid rgba(174, 216, 223, 0.18);
border-radius: 3px;
background: rgba(255, 255, 255, 0.045);
color: var(--ink);
font: inherit;
font-size: 10px;
font-weight: 800;
letter-spacing: 0.08em;
text-transform: uppercase;
cursor: pointer;
transition:
background 140ms ease,
border-color 140ms ease;
}
button:hover {
border-color: rgba(174, 216, 223, 0.42);
background: rgba(255, 255, 255, 0.09);
}
#hint {
position: absolute;
z-index: 8;
right: 26px;
bottom: 112px;
color: rgba(216, 236, 239, 0.52);
font-size: 9px;
font-weight: 700;
letter-spacing: 0.1em;
text-transform: uppercase;
pointer-events: none;
}
.callout {
position: absolute;
z-index: 7;
top: 0;
left: 0;
display: flex;
align-items: center;
gap: 7px;
padding: 6px 9px;
border: 1px solid rgba(193, 226, 231, 0.22);
border-radius: 3px;
background: rgba(9, 20, 26, 0.84);
color: #dcebed;
font-size: 9px;
font-weight: 800;
letter-spacing: 0.08em;
text-transform: uppercase;
white-space: nowrap;
pointer-events: none;
opacity: 0;
transition: opacity 130ms ease;
}
.callout::after {
position: absolute;
top: 100%;
left: 50%;
width: 1px;
height: 16px;
background: rgba(193, 226, 231, 0.4);
content: "";
}
.callout::before {
width: 6px;
height: 6px;
border-radius: 50%;
background: var(--orange);
content: "";
box-shadow: 0 0 10px rgba(255, 117, 73, 0.65);
}
#label-root::before {
background: var(--yellow);
}
#label-ramp::before {
background: var(--yellow);
}
#label-release::before {
background: var(--mint);
}
#label-recess::before {
background: #7e9298;
}
@media (max-width: 760px) {
#title-card {
top: 12px;
left: 12px;
width: calc(100vw - 24px);
padding: 13px 15px;
}
.subtitle {
display: none;
}
#phase-card {
top: 105px;
right: 12px;
width: 235px;
padding: 12px 14px;
}
#phase-description {
min-height: 0;
font-size: 11px;
}
#legend {
display: none;
}
#timeline {
right: 12px;
bottom: 12px;
left: 12px;
grid-template-columns: 1fr;
gap: 10px;
padding: 11px 12px;
}
#steps {
gap: 5px;
}
.step {
font-size: 7px;
letter-spacing: 0.03em;
}
#hint {
display: none;
}
}
</style>
</head>
<body>
<canvas id="scene" aria-label="Animated low-poly snap-fit connector"></canvas>
<section id="title-card" class="panel">
<div class="eyebrow">Mechanical motion study / 01</div>
<h1>Dual Snap-Fit Connector</h1>
<p class="subtitle">
A sectioned low-poly assembly showing cantilever flex, automatic recess
capture, deliberate release, and only then separation.
</p>
</section>
<section id="phase-card" class="panel">
<div class="phase-row">
<span id="phase-number">01 / Approach</span>
<span id="phase-title">Mating half</span>
</div>
<p id="phase-description">The mating half approaches the sectioned housing.</p>
<div class="phase-progress">
<div id="phase-progress-fill"></div>
</div>
</section>
<section id="legend" class="panel">
<span class="legend-item">
<span class="swatch" style="background:#ff7549"></span>
Flexible tabs
</span>
<span class="legend-item">
<span class="swatch" style="background:#ffd166"></span>
Entry ramp
</span>
<span class="legend-item">
<span class="swatch" style="background:#63e6c2"></span>
Release tool
</span>
<span class="legend-item">
<span class="swatch" style="background:#243c45"></span>
Fixed housing
</span>
</section>
<div id="hint">Drag to orbit / wheel to zoom / space to pause</div>
<section id="timeline" class="panel">
<div id="steps">
<div class="step active">Approach</div>
<div class="step">Flex + Return</div>
<div class="step">Locked</div>
<div class="step">Release</div>
<div class="step">Separate</div>
</div>
<div id="controls">
<button id="play-button" type="button">Pause</button>
<button id="replay-button" type="button">Replay</button>
<button id="speed-button" type="button">1.0×</button>
</div>
</section>
<div id="label-root" class="callout">Fixed root</div>
<div id="label-ramp" class="callout">Entry ramp</div>
<div id="label-flex" class="callout">Cantilever flex</div>
<div id="label-recess" class="callout">Capture recess</div>
<div id="label-release" class="callout">Release press</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 renderer = new THREE.WebGLRenderer({
canvas,
antialias: 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.12;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x0a141a);
scene.fog = new THREE.Fog(0x0a141a, 19, 36);
const camera = new THREE.OrthographicCamera(-8, 8, 5.3, -5.3, 0.1, 80);
const cameraTarget = new THREE.Vector3(-0.9, -0.25, 0);
let cameraYaw = 0.48;
let cameraPitch = 0.39;
let cameraZoom = 1;
const cameraRadius = 17;
function updateCamera() {
const horizontal = Math.cos(cameraPitch) * cameraRadius;
camera.position.set(
cameraTarget.x + Math.sin(cameraYaw) * horizontal,
cameraTarget.y + Math.sin(cameraPitch) * cameraRadius,
cameraTarget.z + Math.cos(cameraYaw) * horizontal
);
camera.lookAt(cameraTarget);
camera.updateProjectionMatrix();
camera.updateMatrixWorld();
}
function resize() {
const width = window.innerWidth;
const height = window.innerHeight;
const aspect = width / height;
const viewHeight = (aspect < 1 ? 15.2 : 10.6) / cameraZoom;
camera.left = (-viewHeight * aspect) / 2;
camera.right = (viewHeight * aspect) / 2;
camera.top = viewHeight / 2;
camera.bottom = -viewHeight / 2;
renderer.setSize(width, height);
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
updateCamera();
}
window.addEventListener("resize", resize);
// Direct camera controls: no addons or example modules.
let dragging = false;
let lastPointerX = 0;
let lastPointerY = 0;
canvas.addEventListener("pointerdown", (event) => {
dragging = true;
lastPointerX = event.clientX;
lastPointerY = event.clientY;
canvas.setPointerCapture(event.pointerId);
});
canvas.addEventListener("pointermove", (event) => {
if (!dragging) return;
const dx = event.clientX - lastPointerX;
const dy = event.clientY - lastPointerY;
lastPointerX = event.clientX;
lastPointerY = event.clientY;
cameraYaw -= dx * 0.006;
cameraPitch = THREE.MathUtils.clamp(
cameraPitch + dy * 0.0045,
0.16,
1.12
);
updateCamera();
});
canvas.addEventListener("pointerup", (event) => {
dragging = false;
if (canvas.hasPointerCapture(event.pointerId)) {
canvas.releasePointerCapture(event.pointerId);
}
});
canvas.addEventListener("pointercancel", () => {
dragging = false;
});
canvas.addEventListener(
"wheel",
(event) => {
event.preventDefault();
cameraZoom = THREE.MathUtils.clamp(
cameraZoom * Math.exp(-event.deltaY * 0.001),
0.72,
1.75
);
resize();
},
{ passive: false }
);
// Lighting.
scene.add(new THREE.HemisphereLight(0xc9f5ff, 0x253039, 1.85));
const keyLight = new THREE.DirectionalLight(0xfff0d7, 3.3);
keyLight.position.set(4, 10, 8);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(2048, 2048);
keyLight.shadow.camera.left = -13;
keyLight.shadow.camera.right = 13;
keyLight.shadow.camera.top = 10;
keyLight.shadow.camera.bottom = -10;
keyLight.shadow.camera.near = 1;
keyLight.shadow.camera.far = 35;
keyLight.shadow.bias = -0.00035;
scene.add(keyLight);
const rimLight = new THREE.DirectionalLight(0x6bbdff, 1.45);
rimLight.position.set(-7, 5, -8);
scene.add(rimLight);
const edgeLight = new THREE.PointLight(0x61e6c2, 14, 12, 2);
edgeLight.position.set(-3, 1, 5);
scene.add(edgeLight);
// Faceted materials.
function material(color, options = {}) {
return new THREE.MeshStandardMaterial({
color,
roughness: 0.68,
metalness: 0.08,
flatShading: true,
...options
});
}
const materials = {
housing: material(0x29434c),
housingLight: material(0x3d5961),
housingDark: material(0x172b33),
plug: material(0xcfc8b6),
plugSide: material(0xb7af9e),
plugDark: material(0x756f65),
tab: material(0xff7549, { roughness: 0.52 }),
tabLight: material(0xff9b71),
ramp: material(0xffd166, { roughness: 0.48 }),
recess: material(0x172127),
tool: material(0x63e6c2, {
emissive: 0x123f37,
emissiveIntensity: 0.35,
roughness: 0.45
}),
floor: material(0x101d24, { roughness: 0.92, metalness: 0 }),
mint: material(0x63e6c2),
white: material(0xeaf4f5)
};
const edgeMaterial = new THREE.LineBasicMaterial({
color: 0x091217,
transparent: true,
opacity: 0.6
});
function addEdges(mesh, opacity = 0.6) {
const line = new THREE.LineSegments(
new THREE.EdgesGeometry(mesh.geometry, 24),
edgeMaterial.clone()
);
line.material.opacity = opacity;
mesh.add(line);
return mesh;
}
function box(parent, size, position, mat, outlined = true) {
const mesh = new THREE.Mesh(
new THREE.BoxGeometry(size[0], size[1], size[2]),
mat
);
mesh.position.set(position[0], position[1], position[2]);
mesh.castShadow = true;
mesh.receiveShadow = true;
parent.add(mesh);
if (outlined) addEdges(mesh);
return mesh;
}
function cylinder(parent, radius, height, position, mat, axis = "y") {
const mesh = new THREE.Mesh(
new THREE.CylinderGeometry(radius, radius, height, 8, 1),
mat
);
mesh.position.set(position[0], position[1], position[2]);
if (axis === "z") mesh.rotation.x = Math.PI / 2;
if (axis === "x") mesh.rotation.z = Math.PI / 2;
mesh.castShadow = true;
mesh.receiveShadow = true;
parent.add(mesh);
addEdges(mesh, 0.45);
return mesh;
}
// Ground and technical grid.
const floor = new THREE.Mesh(
new THREE.PlaneGeometry(60, 40),
materials.floor
);
floor.rotation.x = -Math.PI / 2;
floor.position.y = -1.82;
floor.receiveShadow = true;
scene.add(floor);
const grid = new THREE.GridHelper(40, 40, 0x35505a, 0x1b2d34);
grid.position.set(-1, -1.805, 0);
grid.material.transparent = true;
grid.material.opacity = 0.42;
scene.add(grid);
box(
scene,
[15.5, 0.13, 5.3],
[-1.4, -1.75, 0],
material(0x15262e),
false
);
// Fixed, sectioned female housing.
const receiver = new THREE.Group();
scene.add(receiver);
box(receiver, [8.2, 0.42, 3.7], [1.0, -1.49, 0], materials.housing);
box(receiver, [8.2, 2.72, 0.25], [1.0, -0.04, -1.75], materials.housing);
box(receiver, [8.2, 0.53, 0.25], [1.0, -1.12, 1.75], materials.housingLight);
// Cutaway ribs.
for (let x = -2.5; x <= 4.6; x += 1.15) {
box(
receiver,
[0.13, 2.18, 0.14],
[x, -0.12, -1.57],
materials.housingLight,
false
);
}
// Mouth markers and an end frame.
box(receiver, [0.15, 0.75, 3.55], [-3.02, -0.91, 0], materials.housingLight);
box(receiver, [0.34, 0.28, 3.55], [4.86, 1.05, 0], materials.housingLight);
box(receiver, [0.34, 0.3, 3.55], [4.86, -1.28, 0], materials.housingDark);
// Small section-facing accent strips.
for (const z of [-1.6, 1.6]) {
box(
receiver,
[7.8, 0.06, 0.08],
[0.9, 0.88, z],
materials.mint,
false
);
}
// Two articulated cantilever snap tabs.
const segmentLength = 0.72;
const segmentCount = 5;
const tabPivots = [];
function createSnapTab(z) {
const root = new THREE.Group();
root.position.set(4.05, -0.08, z);
receiver.add(root);
// A fixed bracket and root pin make the attachment explicit.
box(
receiver,
[0.68, 0.38, 0.48],
[4.28, -0.18, z],
materials.housingLight
);
cylinder(receiver, 0.19, 0.55, [4.05, -0.08, z], materials.tab, "z");
let parent = root;
let lastPivot = null;
for (let i = 0; i < segmentCount; i++) {
const pivot = new THREE.Group();
if (i === 0) {
pivot.position.set(0, 0, 0);
} else {
pivot.position.set(-segmentLength, 0, 0);
parent.add(pivot);
}
const segment = box(
pivot,
[segmentLength, 0.18, 0.29],
[-segmentLength / 2, 0, 0],
i === 0 ? materials.tabLight : materials.tab
);
segment.userData.segmentIndex = i;
if (i === segmentCount - 1) {
// Downward capture lip at the free end.
box(
pivot,
[0.22, 0.34, 0.34],
[-segmentLength - 0.06, -0.16, 0],
materials.tabLight
);
}
tabPivots.push(pivot);
lastPivot = pivot;
parent = pivot;
}
const tipAnchor = new THREE.Object3D();
tipAnchor.position.set(-segmentLength - 0.08, 0.08, 0);
lastPivot.add(tipAnchor);
return { root, tipAnchor };
}
const frontTab = createSnapTab(0.9);
const rearTab = createSnapTab(-0.9);
function setTabFlex(amount) {
for (const pivot of tabPivots) {
const index = pivot.userData.segmentIndex ?? 0;
pivot.rotation.z = -amount * 0.095 * index;
}
}
// The tab pivots were not given user data above, so assign stable indices.
tabPivots.forEach((pivot, index) => {
pivot.userData.segmentIndex = index;
});
// Moving male/mating connector.
const plug = new THREE.Group();
plug.position.x = -6.4;
scene.add(plug);
// Bottom rail and separate upper pieces allow an actual lowered recess.
box(plug, [6.6, 0.27, 3.1], [-0.1, -1.57, 0], materials.plugDark);
box(plug, [2.0, 1.37, 3.1], [-2.4, -0.935, 0], materials.plug);
box(plug, [1.55, 1.37, 3.1], [-0.625, -0.935, 0], materials.plug);
box(plug, [2.1, 1.37, 3.1], [2.15, -0.935, 0], materials.plug);
// Lowered pocket floor and center divider create two capture channels.
box(plug, [0.95, 1.11, 3.1], [0.625, -1.005, 0], materials.plugSide);
box(plug, [0.95, 1.08, 1.24], [0.625, -0.92, 0], materials.plug);
for (const z of [-0.9, 0.9]) {
box(
plug,
[0.77, 0.035, 0.54],
[0.625, -0.438, z],
materials.recess,
false
);
}
// Triangular entry ramp: rises in the insertion direction.
const rampShape = new THREE.Shape();
rampShape.moveTo(-1.4, -0.25);
rampShape.lineTo(0.15, 0.28);
rampShape.lineTo(0.15, -0.25);
rampShape.closePath();
const rampGeometry = new THREE.ExtrudeGeometry(rampShape, {
depth: 3.1,
bevelEnabled: false,
steps: 1,
curveSegments: 1
});
rampGeometry.translate(0, 0, -1.55);
const ramp = new THREE.Mesh(rampGeometry, materials.ramp);
ramp.castShadow = true;
ramp.receiveShadow = true;
plug.add(ramp);
addEdges(ramp, 0.45);
// Side details.
for (const z of [-1.58, 1.58]) {
box(plug, [1.0, 0.12, 0.08], [-2.35, -0.39, z], materials.ramp, false);
box(plug, [0.42, 0.56, 0.08], [2.05, -0.91, z], materials.plugDark, false);
}
// A small insertion arrow rides with the moving half.
const insertionArrow = new THREE.ArrowHelper(
new THREE.Vector3(1, 0, 0),
new THREE.Vector3(-3.65, -1.05, 1.67),
1.05,
0x63e6c2,
0.26,
0.15
);
plug.add(insertionArrow);
// Separate release tool: a crossbar with two contact pads and a stem.
const releaseTool = new THREE.Group();
releaseTool.position.set(0.48, 3.1, 0);
scene.add(releaseTool);
box(releaseTool, [0.34, 0.26, 4.0], [0, 0, 0.45], materials.tool);
box(releaseTool, [0.28, 2.25, 0.28], [0, 1.08, 2.22], materials.tool);
for (const z of [-0.9, 0.9]) {
box(
releaseTool,
[0.47, 0.17, 0.4],
[0, -0.18, z],
materials.tool
);
}
const downArrow = new THREE.Mesh(
new THREE.ConeGeometry(0.19, 0.43, 6),
materials.tool
);
downArrow.position.set(0, -0.47, 2.22);
downArrow.rotation.z = Math.PI;
releaseTool.add(downArrow);
// Motion timing.
const duration = 8.2;
const clock = new THREE.Clock();
let elapsed = 0;
let playing = true;
let playbackSpeed = 1;
function smooth(a, b, value) {
const x = THREE.MathUtils.clamp((value - a) / (b - a), 0, 1);
return x * x * (3 - 2 * x);
}
function mix(a, b, amount) {
return a + (b - a) * amount;
}
function rampFlexAt(t) {
if (t < 1.35) return 0;
if (t < 2.05) return smooth(1.35, 2.05, t);
if (t < 2.7) return 1 - smooth(2.05, 2.7, t);
return 0;
}
function releaseFlexAt(t) {
if (t < 3.9) return 0;
if (t < 4.8) return smooth(3.9, 4.8, t) * 0.78;
if (t < 7.0) return 0.78;
if (t < 7.65) return 0.78 * (1 - smooth(7.0, 7.65, t));
return 0;
}
function plugPositionAt(t) {
if (t < 1.35) {
return mix(-6.4, -0.65, smooth(0, 1.35, t));
}
if (t < 2.05) {
return mix(-0.65, 0.25, smooth(1.35, 2.05, t));
}
if (t < 2.7) {
return mix(0.25, 0, smooth(2.05, 2.7, t));
}
if (t < 5.4) return 0;
if (t < 7.2) {
return mix(0, -5.9, smooth(5.4, 7.2, t));
}
return -5.9;
}
function toolHeightAt(t) {
if (t < 3.9) return 3.1;
if (t < 4.8) return mix(3.1, 0.48, smooth(3.9, 4.8, t));
if (t < 7.0) return 0.48;
if (t < 7.65) return mix(0.48, 3.1, smooth(7.0, 7.65, t));
return 3.1;
}
const phaseNumber = document.querySelector("#phase-number");
const phaseTitle = document.querySelector("#phase-title");
const phaseDescription = document.querySelector("#phase-description");
const phaseProgressFill = document.querySelector("#phase-progress-fill");
const stepElements = [...document.querySelectorAll(".step")];
const phaseRanges = [
[0, 1.35],
[1.35, 2.7],
[2.7, 3.9],
[3.9, 5.4],
[5.4, 7.2]
];
function updatePhaseUI(t) {
let phase;
let title;
let description;
if (t < 1.35) {
phase = 0;
title = "Mating half";
description = "The mating half approaches the sectioned receiver.";
} else if (t < 2.05) {
phase = 1;
title = "Ramp flex";
description = "The entry ramp lifts both cantilever tabs while their roots remain fixed.";
} else if (t < 2.7) {
phase = 1;
title = "Recess return";
description = "The ramp clears; both tabs spring downward into their capture recesses.";
} else if (t < 3.9) {
phase = 2;
title = "Locked";
description = "The mating halves are positively captured by both snap tabs.";
} else if (t < 4.8) {
phase = 3;
title = "Release press";
description = "A separate tool deliberately flexes the tabs out of the recesses.";
} else if (t < 5.4) {
phase = 3;
title = "Released";
description = "The release motion finishes before any separation begins.";
} else if (t < 7.2) {
phase = 4;
title = "Disassembly";
description = "With the tabs held clear, the mating half withdraws from the housing.";
} else {
phase = 4;
title = "Separated";
description = "The released tabs return toward rest; the mechanism is now disassembled.";
}
phaseNumber.textContent = `${String(phase + 1).padStart(2, "0")} / ${title}`;
phaseTitle.textContent = title;
phaseDescription.textContent = description;
const [start, end] = phaseRanges[phase];
const localProgress = THREE.MathUtils.clamp((t - start) / (end - start), 0, 1);
phaseProgressFill.style.width = `${localProgress * 100}%`;
stepElements.forEach((element, index) => {
const [stepStart, stepEnd] = phaseRanges[index];
const stepProgress = THREE.MathUtils.clamp(
(t - stepStart) / (stepEnd - stepStart),
0,
1
);
element.style.setProperty("--progress", `${stepProgress * 100}%`);
element.classList.toggle("active", index === phase);
});
}
// Projected HTML labels.
const labels = {
root: document.querySelector("#label-root"),
ramp: document.querySelector("#label-ramp"),
flex: document.querySelector("#label-flex"),
recess: document.querySelector("#label-recess"),
release: document.querySelector("#label-release")
};
function placeLabel(element, point, visible) {
if (!visible) {
element.style.opacity = "0";
return;
}
const projected = point.clone().project(camera);
if (projected.z < -1 || projected.z > 1) {
element.style.opacity = "0";
return;
}
const x = (projected.x * 0.5 + 0.5) * window.innerWidth;
const y = (-projected.y * 0.5 + 0.5) * window.innerHeight;
element.style.opacity = "1";
element.style.transform =
`translate3d(${x}px, ${y}px, 0) translate(-50%, -100%)`;
}
const tempPoint = new THREE.Vector3();
function updateLabels(t) {
placeLabel(
labels.root,
new THREE.Vector3(4.08, 0.42, 1.18),
true
);
const rampPoint = plug.localToWorld(new THREE.Vector3(-0.58, 0.52, 1.62));
placeLabel(labels.ramp, rampPoint, t > 1.05 && t < 2.8);
frontTab.tipAnchor.getWorldPosition(tempPoint);
tempPoint.y += 0.3;
placeLabel(
labels.flex,
tempPoint,
t > 1.2 && t < 4.85
);
const recessPoint = plug.localToWorld(new THREE.Vector3(0.64, -0.15, 1.72));
placeLabel(labels.recess, recessPoint, t > 1.9 && t < 4.0);
const releasePoint = releaseTool.localToWorld(
new THREE.Vector3(0, 0.45, 2.2)
);
placeLabel(labels.release, releasePoint, t > 3.65 && t < 5.45);
}
const playButton = document.querySelector("#play-button");
const replayButton = document.querySelector("#replay-button");
const speedButton = document.querySelector("#speed-button");
playButton.addEventListener("click", () => {
if (elapsed >= duration) elapsed = 0;
playing = !playing;
playButton.textContent = playing ? "Pause" : "Play";
});
replayButton.addEventListener("click", () => {
elapsed = 0;
playing = true;
playButton.textContent = "Pause";
});
speedButton.addEventListener("click", () => {
const speeds = [0.5, 0.75, 1, 1.25, 1.5];
const index = (speeds.indexOf(playbackSpeed) + 1) % speeds.length;
playbackSpeed = speeds[index];
speedButton.textContent = `${playbackSpeed.toFixed(2).replace(/0$/, "")}×`;
});
window.addEventListener("keydown", (event) => {
if (event.code === "Space") {
event.preventDefault();
playButton.click();
}
if (event.code === "KeyR") {
replayButton.click();
}
});
function updateAnimation(t) {
plug.position.x = plugPositionAt(t);
const flex = Math.max(rampFlexAt(t), releaseFlexAt(t));
setTabFlex(flex);
releaseTool.position.y = toolHeightAt(t);
// The tool remains pressed through the beginning of withdrawal.
if (t > 5.4 && t < 7.0) {
setTabFlex(0.78);
}
updatePhaseUI(t);
updateLabels(t);
}
resize();
updateAnimation(0);
renderer.setAnimationLoop(() => {
const delta = Math.min(clock.getDelta(), 0.05);
if (playing) {
elapsed = Math.min(duration, elapsed + delta * playbackSpeed);
if (elapsed >= duration) {
playing = false;
playButton.textContent = "Play";
}
}
updateAnimation(elapsed);
renderer.render(scene, camera);
});
</script>
</body>
</html>
```