Prompt specification
Build or customize the Three.js 3D scene "Inside versus outside span" 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: Inside versus outside span
Category: Everyday & Industrial · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/inside-versus-outside-span-x0282
Scene Description Prompt:
Create a single self-contained HTML file using Three.js from cdnjs that shows a measuring tape. Measure a box's exterior width and interior opening using the same tape; animate reference contact changes and keep the displayed endpoints attached to the measured surfaces. Low-poly.
Working Three.js Starter (inside-versus-outside-span-x0282):
```html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>One Tape — Exterior and Interior Measurements</title>
<style>
:root {
color-scheme: dark;
--bg: #11191f;
--panel: rgba(15, 24, 30, 0.84);
--line: rgba(255, 255, 255, 0.13);
--muted: #9caeb7;
--text: #f4f7f7;
--yellow: #ffd044;
--orange: #f18a3b;
--cyan: #63dfd1;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: var(--bg);
font-family:
Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
color: var(--text);
}
canvas {
display: block;
width: 100%;
height: 100%;
touch-action: none;
}
button {
font: inherit;
}
.glass {
background: var(--panel);
border: 1px solid var(--line);
box-shadow: 0 18px 50px rgba(0, 0, 0, 0.28);
backdrop-filter: blur(14px);
-webkit-backdrop-filter: blur(14px);
}
.brand {
position: fixed;
z-index: 10;
top: 24px;
left: 24px;
padding: 16px 18px;
border-radius: 16px;
pointer-events: none;
}
.brand-row {
display: flex;
align-items: center;
gap: 11px;
}
.brand-mark {
display: grid;
width: 38px;
height: 38px;
place-items: center;
border-radius: 10px;
color: #171a1c;
background: var(--yellow);
font-size: 19px;
font-weight: 900;
transform: rotate(-4deg);
box-shadow: 0 6px 20px rgba(255, 208, 68, 0.2);
}
.brand h1 {
margin: 0;
font-size: 16px;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.brand p {
margin: 5px 0 0 49px;
color: var(--muted);
font-size: 11px;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.readout {
position: fixed;
z-index: 10;
top: 24px;
right: 24px;
width: 316px;
padding: 19px;
border-radius: 18px;
pointer-events: none;
}
.eyebrow {
color: var(--muted);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.18em;
text-transform: uppercase;
}
.reading {
display: flex;
align-items: baseline;
gap: 8px;
margin-top: 6px;
}
#readoutValue {
font-size: 56px;
font-weight: 780;
line-height: 1;
letter-spacing: -0.055em;
font-variant-numeric: tabular-nums;
}
.unit {
color: var(--yellow);
font-size: 16px;
font-weight: 800;
}
#readoutName {
margin-top: 7px;
font-size: 13px;
font-weight: 700;
}
#readoutStatus {
margin-top: 4px;
color: var(--muted);
font-size: 11px;
}
.contacts {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 8px;
margin-top: 16px;
}
.contact {
display: flex;
align-items: center;
gap: 7px;
padding: 9px 10px;
border: 1px solid var(--line);
border-radius: 10px;
color: #7f919a;
font-size: 10px;
font-weight: 800;
letter-spacing: 0.07em;
text-transform: uppercase;
}
.contact-dot {
width: 7px;
height: 7px;
flex: 0 0 auto;
border-radius: 50%;
background: #526069;
}
.contact.locked {
color: var(--text);
border-color: rgba(99, 223, 209, 0.35);
background: rgba(99, 223, 209, 0.07);
}
.contact.locked .contact-dot {
background: var(--cyan);
box-shadow: 0 0 12px rgba(99, 223, 209, 0.8);
}
.progress {
height: 3px;
margin-top: 15px;
overflow: hidden;
border-radius: 3px;
background: rgba(255, 255, 255, 0.09);
}
#progressFill {
display: block;
width: 100%;
height: 100%;
transform: scaleX(0);
transform-origin: left;
background: linear-gradient(90deg, var(--cyan), var(--yellow));
}
.controls-wrap {
position: fixed;
z-index: 12;
right: 24px;
bottom: 24px;
left: 24px;
display: flex;
flex-direction: column;
align-items: center;
gap: 10px;
pointer-events: none;
}
.phase {
padding: 9px 14px;
border-radius: 999px;
color: #dce6e8;
font-size: 11px;
font-weight: 750;
letter-spacing: 0.04em;
text-align: center;
}
.controls {
display: flex;
flex-wrap: wrap;
justify-content: center;
gap: 8px;
padding: 8px;
border-radius: 16px;
pointer-events: auto;
}
.control {
min-height: 40px;
padding: 0 15px;
border: 1px solid var(--line);
border-radius: 11px;
color: #dce5e8;
background: rgba(255, 255, 255, 0.045);
cursor: pointer;
font-size: 12px;
font-weight: 750;
transition:
background 160ms ease,
border-color 160ms ease,
color 160ms ease,
transform 160ms ease;
}
.control:hover:not(:disabled) {
transform: translateY(-1px);
border-color: rgba(255, 255, 255, 0.28);
background: rgba(255, 255, 255, 0.09);
}
.control:disabled {
cursor: wait;
opacity: 0.48;
}
.control.active {
color: #171a1c;
border-color: var(--yellow);
background: var(--yellow);
}
.control.auto.active {
color: #10201e;
border-color: var(--cyan);
background: var(--cyan);
}
.help {
color: rgba(218, 229, 232, 0.66);
font-size: 10px;
letter-spacing: 0.05em;
text-shadow: 0 1px 4px #000;
}
#labels {
position: fixed;
z-index: 8;
inset: 0;
overflow: hidden;
pointer-events: none;
}
.endpoint {
position: absolute;
min-width: 132px;
padding: 9px 11px;
border: 1px solid rgba(255, 255, 255, 0.16);
border-radius: 10px;
background: rgba(12, 19, 23, 0.9);
box-shadow: 0 9px 30px rgba(0, 0, 0, 0.3);
opacity: 0;
transition: opacity 160ms ease;
will-change: left, top;
}
.endpoint::after {
position: absolute;
bottom: -12px;
width: 1px;
height: 12px;
background: rgba(255, 255, 255, 0.55);
content: "";
}
#labelA {
transform: translate(calc(-100% - 11px), calc(-100% - 8px));
}
#labelA::after {
right: 14px;
}
#labelB {
transform: translate(11px, calc(-100% - 8px));
}
#labelB::after {
left: 14px;
}
.tag-kicker {
color: var(--cyan);
font-size: 9px;
font-weight: 850;
letter-spacing: 0.12em;
text-transform: uppercase;
}
#labelB .tag-kicker {
color: var(--yellow);
}
.tag-value {
display: block;
margin-top: 3px;
font-size: 15px;
font-weight: 780;
font-variant-numeric: tabular-nums;
}
.tag-surface {
display: block;
margin-top: 2px;
color: var(--muted);
font-size: 9px;
}
@media (max-width: 720px) {
.brand {
top: 12px;
left: 12px;
padding: 12px 13px;
}
.brand h1 {
font-size: 13px;
}
.brand p {
display: none;
}
.readout {
top: 12px;
right: 12px;
width: 220px;
padding: 14px;
}
#readoutValue {
font-size: 42px;
}
.contacts {
gap: 5px;
margin-top: 11px;
}
.contact {
padding: 7px;
font-size: 8px;
}
.controls-wrap {
right: 10px;
bottom: 10px;
left: 10px;
}
.control {
min-height: 36px;
padding: 0 11px;
font-size: 10px;
}
.phase {
max-width: 94vw;
font-size: 9px;
}
.help {
display: none;
}
.endpoint {
min-width: 105px;
padding: 7px 9px;
}
.tag-value {
font-size: 12px;
}
.tag-surface {
display: none;
}
}
</style>
</head>
<body>
<div class="brand glass">
<div class="brand-row">
<div class="brand-mark">↔</div>
<h1>One Tape / Box Study</h1>
</div>
<p>Exterior + interior reference transfer</p>
</div>
<section class="readout glass" aria-live="polite">
<div class="eyebrow">Active dimension</div>
<div class="reading">
<span id="readoutValue">120.0</span>
<span class="unit">mm</span>
</div>
<div id="readoutName">Exterior width</div>
<div id="readoutStatus">Preparing contacts</div>
<div class="contacts">
<div id="contactA" class="contact">
<span class="contact-dot"></span>
<span>A / Reference</span>
</div>
<div id="contactB" class="contact">
<span class="contact-dot"></span>
<span>B / Measuring tip</span>
</div>
</div>
<div class="progress"><i id="progressFill"></i></div>
</section>
<div id="labels" aria-hidden="true">
<div id="labelA" class="endpoint">
<span class="tag-kicker">A · Reference</span>
<strong class="tag-value">0.0 mm</strong>
<span id="labelASurface" class="tag-surface">Outer left face</span>
</div>
<div id="labelB" class="endpoint">
<span class="tag-kicker">B · Measuring tip</span>
<strong id="labelBValue" class="tag-value">120.0 mm</strong>
<span id="labelBSurface" class="tag-surface">Outer right face</span>
</div>
</div>
<div class="controls-wrap">
<div id="phase" class="phase glass" aria-live="polite">
Preparing the reference contact
</div>
<nav class="controls glass" aria-label="Measurement controls">
<button id="outerButton" class="control active">Exterior · 120 mm</button>
<button id="innerButton" class="control">Interior · 90 mm</button>
<button id="autoButton" class="control auto active" aria-pressed="true">
Auto cycle: On
</button>
<button id="resetButton" class="control">Reset view</button>
</nav>
<div class="help">Drag to orbit · Scroll to zoom · Same physical tape in both measurements</div>
</div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
const $ = (id) => document.getElementById(id);
// ---------------------------------------------------------------------
// Scene
// ---------------------------------------------------------------------
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x11191f);
scene.fog = new THREE.Fog(0x11191f, 13, 25);
const camera = new THREE.PerspectiveCamera(
38,
window.innerWidth / window.innerHeight,
0.1,
100
);
const renderer = new THREE.WebGLRenderer({ antialias: true });
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;
renderer.domElement.setAttribute(
"aria-label",
"Interactive low-poly box and measuring tape"
);
document.body.prepend(renderer.domElement);
const hemisphere = new THREE.HemisphereLight(0xc7e4ed, 0x443526, 2.05);
scene.add(hemisphere);
const keyLight = new THREE.DirectionalLight(0xfff0d7, 3.1);
keyLight.position.set(5.5, 9, 6.5);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(2048, 2048);
keyLight.shadow.camera.left = -8;
keyLight.shadow.camera.right = 8;
keyLight.shadow.camera.top = 8;
keyLight.shadow.camera.bottom = -8;
keyLight.shadow.camera.near = 0.5;
keyLight.shadow.camera.far = 25;
keyLight.shadow.bias = -0.0008;
scene.add(keyLight);
const rimLight = new THREE.DirectionalLight(0x70c9d3, 1.35);
rimLight.position.set(-5, 4, -5);
scene.add(rimLight);
const ground = new THREE.Mesh(
new THREE.PlaneGeometry(30, 30),
new THREE.MeshStandardMaterial({
color: 0x172229,
roughness: 0.96,
metalness: 0.02
})
);
ground.rotation.x = -Math.PI / 2;
ground.position.y = -0.012;
ground.receiveShadow = true;
scene.add(ground);
const grid = new THREE.GridHelper(20, 20, 0x52717b, 0x31434c);
grid.position.y = -0.004;
grid.material.transparent = true;
grid.material.opacity = 0.22;
scene.add(grid);
// ---------------------------------------------------------------------
// Low-poly open-front box
// ---------------------------------------------------------------------
const MM_PER_UNIT = 25;
const OUTER_WIDTH = 4.8; // 120 mm
const INNER_WIDTH = 3.6; // 90 mm
const BOX_HEIGHT = 2.7;
const BOX_DEPTH = 2.8;
const WALL = (OUTER_WIDTH - INNER_WIDTH) / 2;
const FRONT_Z = BOX_DEPTH / 2;
const cardboard = new THREE.MeshStandardMaterial({
color: 0x9a846a,
roughness: 0.91,
metalness: 0,
flatShading: true
});
const cardboardLight = new THREE.MeshStandardMaterial({
color: 0xac9679,
roughness: 0.93,
metalness: 0,
flatShading: true
});
const cardboardDark = new THREE.MeshStandardMaterial({
color: 0x77624c,
roughness: 0.95,
metalness: 0,
flatShading: true
});
const edgeMaterial = new THREE.LineBasicMaterial({
color: 0x30291f,
transparent: true,
opacity: 0.5
});
function addPanel(width, height, depth, x, y, z, material) {
const mesh = new THREE.Mesh(
new THREE.BoxGeometry(width, height, depth),
material
);
mesh.position.set(x, y, z);
mesh.castShadow = true;
mesh.receiveShadow = true;
const edges = new THREE.LineSegments(
new THREE.EdgesGeometry(mesh.geometry),
edgeMaterial
);
mesh.add(edges);
scene.add(mesh);
return mesh;
}
// Base and lid.
addPanel(
OUTER_WIDTH, 0.22, BOX_DEPTH,
0, 0.11, 0,
cardboardDark
);
addPanel(
OUTER_WIDTH, 0.22, BOX_DEPTH,
0, BOX_HEIGHT - 0.11, 0,
cardboardLight
);
// Full-depth side walls.
addPanel(
WALL, BOX_HEIGHT - 0.44, BOX_DEPTH,
-(OUTER_WIDTH / 2 - WALL / 2),
BOX_HEIGHT / 2,
0,
cardboard
);
addPanel(
WALL, BOX_HEIGHT - 0.44, BOX_DEPTH,
OUTER_WIDTH / 2 - WALL / 2,
BOX_HEIGHT / 2,
0,
cardboard
);
// Rear wall leaves an open front.
addPanel(
INNER_WIDTH, BOX_HEIGHT - 0.44, 0.22,
0,
BOX_HEIGHT / 2,
-BOX_DEPTH / 2 + 0.11,
cardboardDark
);
// A small inset base lip emphasizes the interior opening.
addPanel(
INNER_WIDTH + 0.035, 0.055, 0.055,
0,
0.245,
FRONT_Z + 0.012,
cardboardLight
);
// ---------------------------------------------------------------------
// One reusable tape assembly
// ---------------------------------------------------------------------
const tapeCanvas = document.createElement("canvas");
tapeCanvas.width = 1024;
tapeCanvas.height = 128;
const tapeContext = tapeCanvas.getContext("2d");
const tapeTexture = new THREE.CanvasTexture(tapeCanvas);
tapeTexture.colorSpace = THREE.SRGBColorSpace;
tapeTexture.anisotropy = Math.min(
8,
renderer.capabilities.getMaxAnisotropy()
);
let lastTapeLengthMM = -1;
function drawTape(lengthMM) {
if (Math.abs(lengthMM - lastTapeLengthMM) < 0.2) return;
lastTapeLengthMM = lengthMM;
const ctx = tapeContext;
const width = tapeCanvas.width;
const height = tapeCanvas.height;
ctx.clearRect(0, 0, width, height);
ctx.fillStyle = "#f6c832";
ctx.fillRect(0, 0, width, height);
const gradient = ctx.createLinearGradient(0, 0, 0, height);
gradient.addColorStop(0, "rgba(255,255,255,0.25)");
gradient.addColorStop(0.5, "rgba(255,255,255,0)");
gradient.addColorStop(1, "rgba(90,55,0,0.16)");
ctx.fillStyle = gradient;
ctx.fillRect(0, 0, width, height);
ctx.strokeStyle = "rgba(95,64,10,0.22)";
ctx.lineWidth = 2;
ctx.beginPath();
ctx.moveTo(0, height - 3);
ctx.lineTo(width, height - 3);
ctx.stroke();
const safeLength = Math.max(lengthMM, 1);
for (let mm = 0; mm <= safeLength; mm += 2.5) {
const x = Math.min(width - 1, (mm / safeLength) * width);
const isMajor = Math.abs(mm % 10) < 0.001;
const isMedium = Math.abs(mm % 5) < 0.001;
const tickHeight = isMajor ? 62 : isMedium ? 43 : 27;
ctx.strokeStyle = isMajor ? "#25272a" : "#3f3a2e";
ctx.lineWidth = isMajor ? 4 : isMedium ? 3 : 2;
ctx.beginPath();
ctx.moveTo(x, height);
ctx.lineTo(x, height - tickHeight);
ctx.stroke();
if (isMajor && mm > 0 && mm < safeLength - 0.5) {
ctx.fillStyle = "#25272a";
ctx.font = "700 28px ui-sans-serif, system-ui";
ctx.textAlign = "left";
ctx.fillText(String(mm), x + 7, 32);
}
}
// Red 100 mm index.
if (safeLength >= 100) {
const x = (100 / safeLength) * width;
ctx.fillStyle = "#c63c31";
ctx.fillRect(x - 3, 0, 6, height);
}
tapeTexture.needsUpdate = true;
}
drawTape(120);
const tapeMaterial = new THREE.MeshStandardMaterial({
map: tapeTexture,
color: 0xffffff,
roughness: 0.68,
metalness: 0.02,
side: THREE.DoubleSide
});
const tapeBlade = new THREE.Mesh(
new THREE.BoxGeometry(1, 0.13, 0.035),
tapeMaterial
);
tapeBlade.castShadow = true;
tapeBlade.receiveShadow = true;
scene.add(tapeBlade);
const housing = new THREE.Group();
scene.add(housing);
const housingMaterial = new THREE.MeshStandardMaterial({
color: 0xef873b,
roughness: 0.58,
metalness: 0.08,
flatShading: true
});
const housingBody = new THREE.Mesh(
new THREE.BoxGeometry(0.44, 0.48, 0.30),
housingMaterial
);
housingBody.castShadow = true;
housing.add(housingBody);
const housingNose = new THREE.Mesh(
new THREE.BoxGeometry(0.13, 0.24, 0.33),
housingMaterial
);
housingNose.position.x = 0.18;
housingNose.castShadow = true;
housing.add(housingNose);
const reelPivot = new THREE.Group();
reelPivot.position.set(-0.045, 0.015, 0.175);
housing.add(reelPivot);
const reel = new THREE.Mesh(
new THREE.CylinderGeometry(0.145, 0.145, 0.055, 12),
new THREE.MeshStandardMaterial({
color: 0x27333a,
roughness: 0.7,
metalness: 0.2,
flatShading: true
})
);
reel.rotation.x = Math.PI / 2;
reelPivot.add(reel);
const reelHub = new THREE.Mesh(
new THREE.CylinderGeometry(0.052, 0.052, 0.065, 10),
new THREE.MeshStandardMaterial({
color: 0xd4d9d8,
roughness: 0.45,
metalness: 0.45,
flatShading: true
})
);
reelHub.rotation.x = Math.PI / 2;
reelHub.position.z = 0.006;
reelPivot.add(reelHub);
const housingButton = new THREE.Mesh(
new THREE.BoxGeometry(0.19, 0.075, 0.11),
new THREE.MeshStandardMaterial({
color: 0x29343a,
roughness: 0.65,
metalness: 0.12,
flatShading: true
})
);
housingButton.position.set(-0.035, 0.275, 0.02);
housing.add(housingButton);
const hook = new THREE.Group();
scene.add(hook);
const metalMaterial = new THREE.MeshStandardMaterial({
color: 0xc9d2d1,
roughness: 0.36,
metalness: 0.72,
flatShading: true
});
const hookUpright = new THREE.Mesh(
new THREE.BoxGeometry(0.035, 0.20, 0.08),
metalMaterial
);
hookUpright.position.z = 0.025;
hookUpright.castShadow = true;
hook.add(hookUpright);
const hookLip = new THREE.Mesh(
new THREE.BoxGeometry(0.11, 0.035, 0.08),
metalMaterial
);
hookLip.position.set(0.04, -0.075, 0.025);
hookLip.castShadow = true;
hook.add(hookLip);
function createContactRing(color) {
const ring = new THREE.Mesh(
new THREE.TorusGeometry(0.105, 0.019, 6, 16),
new THREE.MeshBasicMaterial({
color,
transparent: true,
opacity: 0.95,
depthTest: false
})
);
ring.renderOrder = 10;
scene.add(ring);
return ring;
}
const referenceRing = createContactRing(0x63dfd1);
const tipRing = createContactRing(0xffd044);
// ---------------------------------------------------------------------
// Measurement state and animation sequence
// ---------------------------------------------------------------------
const targets = {
outer: {
left: new THREE.Vector3(
-OUTER_WIDTH / 2,
2.05,
FRONT_Z + 0.035
),
right: new THREE.Vector3(
OUTER_WIDTH / 2,
2.05,
FRONT_Z + 0.035
),
value: 120,
name: "Exterior width",
leftSurface: "Outer left face",
rightSurface: "Outer right face"
},
inner: {
left: new THREE.Vector3(
-INNER_WIDTH / 2,
1.25,
FRONT_Z + 0.035
),
right: new THREE.Vector3(
INNER_WIDTH / 2,
1.25,
FRONT_Z + 0.035
),
value: 90,
name: "Interior opening",
leftSurface: "Inner left jamb",
rightSurface: "Inner right jamb"
}
};
const state = {
left: targets.outer.left.clone(),
right: targets.outer.left.clone(),
contactA: true,
contactB: false,
mode: "outer",
targetMode: "outer",
busy: false,
completed: false
};
let pulseA = 1;
let pulseB = 0;
let autoPlay = true;
let autoTimer = null;
const segmentQueue = [];
let activeSegment = null;
let segmentElapsed = 0;
function cloneState() {
return {
left: state.left.clone(),
right: state.right.clone(),
contactA: state.contactA,
contactB: state.contactB
};
}
function smoothStep(t) {
return t * t * (3 - 2 * t);
}
function enqueueSegment(duration, label, onStart, update, onDone) {
segmentQueue.push({
duration,
label,
onStart,
update,
onDone
});
}
function beginNextSegment() {
if (segmentQueue.length === 0) {
finishSequence();
return;
}
activeSegment = segmentQueue.shift();
segmentElapsed = 0;
activeSegment.from = cloneState();
activeSegment.onStart?.(state);
$("phase").textContent = activeSegment.label;
}
function updateSequence(dt) {
if (!activeSegment) return;
segmentElapsed += dt;
const rawT = Math.min(1, segmentElapsed / activeSegment.duration);
const easedT = smoothStep(rawT);
activeSegment.update(state, activeSegment.from, easedT, rawT);
if (rawT >= 1) {
activeSegment.onDone?.(state);
activeSegment = null;
beginNextSegment();
}
}
function addRetractSegment(sourceName) {
enqueueSegment(
0.56,
`Releasing tip B from the ${sourceName}`,
(s) => {
s.contactB = false;
},
(s, from, eased) => {
s.left.copy(from.left);
s.right.lerpVectors(from.right, from.left, eased);
}
);
}
function addReferenceTransferSegment(sourceMode, targetMode) {
const sourceName =
sourceMode === "outer" ? "outer face" : "interior jamb";
const targetName =
targetMode === "outer" ? "outer face" : "interior jamb";
enqueueSegment(
1.02,
`Changing reference A · ${sourceName} → ${targetName}`,
(s) => {
s.contactA = false;
s.contactB = false;
},
(s, from, eased, raw) => {
s.left.lerpVectors(from.left, targets[targetMode].left, eased);
s.right.lerpVectors(from.right, targets[targetMode].left, eased);
// Lift and swing the reel around the front edge during transfer.
const arc = Math.sin(Math.PI * raw);
s.left.y += arc * 0.28;
s.left.z += arc * 0.58;
s.right.y += arc * 0.28;
s.right.z += arc * 0.58;
},
(s) => {
s.contactA = true;
pulseA = 1;
}
);
}
function addExtendSegment(targetMode) {
const label =
targetMode === "outer"
? "Extending tip B across the exterior width"
: "Extending tip B across the interior opening";
enqueueSegment(
0.96,
label,
(s) => {
s.contactB = false;
},
(s, from, eased) => {
s.left.copy(from.left);
s.right.lerpVectors(from.right, targets[targetMode].right, eased);
},
(s) => {
s.contactB = true;
pulseB = 1;
}
);
}
function finishSequence() {
state.mode = state.targetMode;
state.busy = false;
state.completed = true;
activeSegment = null;
$("phase").textContent =
"Both contacts locked · same tape · measurement ready";
if (autoPlay) scheduleNext();
}
function startMeasurement(targetMode) {
if (state.busy) return;
clearTimeout(autoTimer);
segmentQueue.length = 0;
state.busy = true;
state.completed = false;
state.targetMode = targetMode;
const sourceMode = state.mode;
const sourceName =
sourceMode === "outer" ? "exterior edge" : "interior edge";
addRetractSegment(sourceName);
if (sourceMode !== targetMode) {
addReferenceTransferSegment(sourceMode, targetMode);
}
addExtendSegment(targetMode);
beginNextSegment();
}
function scheduleNext(delay = 1650) {
clearTimeout(autoTimer);
if (!autoPlay) return;
autoTimer = setTimeout(() => {
if (!autoPlay || state.busy) return;
startMeasurement(state.mode === "outer" ? "inner" : "outer");
}, delay);
}
// ---------------------------------------------------------------------
// Camera controls implemented directly without examples or addons
// ---------------------------------------------------------------------
const cameraTarget = new THREE.Vector3(0, 1.24, 0);
let cameraTheta = 0.66;
let cameraPhi = 1.16;
let cameraRadius = 8.5;
let dragging = false;
let lastPointerX = 0;
let lastPointerY = 0;
function applyCamera() {
const sinPhi = Math.sin(cameraPhi);
camera.position.set(
cameraTarget.x + cameraRadius * sinPhi * Math.sin(cameraTheta),
cameraTarget.y + cameraRadius * Math.cos(cameraPhi),
cameraTarget.z + cameraRadius * sinPhi * Math.cos(cameraTheta)
);
camera.lookAt(cameraTarget);
}
function resetCamera() {
cameraTheta = 0.66;
cameraPhi = 1.16;
cameraRadius = 8.5;
applyCamera();
}
renderer.domElement.addEventListener("pointerdown", (event) => {
dragging = true;
lastPointerX = event.clientX;
lastPointerY = event.clientY;
renderer.domElement.setPointerCapture(event.pointerId);
});
renderer.domElement.addEventListener("pointermove", (event) => {
if (!dragging) return;
const dx = event.clientX - lastPointerX;
const dy = event.clientY - lastPointerY;
lastPointerX = event.clientX;
lastPointerY = event.clientY;
cameraTheta -= dx * 0.006;
cameraPhi = THREE.MathUtils.clamp(
cameraPhi + dy * 0.005,
0.52,
1.47
);
});
function endDrag(event) {
dragging = false;
if (
event.pointerId !== undefined &&
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();
cameraRadius = THREE.MathUtils.clamp(
cameraRadius + event.deltaY * 0.007,
5.2,
13.5
);
},
{ passive: false }
);
renderer.domElement.addEventListener("dblclick", resetCamera);
// ---------------------------------------------------------------------
// UI
// ---------------------------------------------------------------------
$("outerButton").addEventListener("click", () => {
startMeasurement("outer");
});
$("innerButton").addEventListener("click", () => {
startMeasurement("inner");
});
$("autoButton").addEventListener("click", () => {
autoPlay = !autoPlay;
clearTimeout(autoTimer);
$("autoButton").classList.toggle("active", autoPlay);
$("autoButton").setAttribute("aria-pressed", String(autoPlay));
$("autoButton").textContent = `Auto cycle: ${autoPlay ? "On" : "Off"}`;
if (autoPlay && state.completed) scheduleNext(350);
});
$("resetButton").addEventListener("click", resetCamera);
window.addEventListener("keydown", (event) => {
if (event.key.toLowerCase() === "r") resetCamera();
});
function updateUI() {
const target = targets[state.targetMode];
$("readoutValue").textContent = target.value.toFixed(1);
$("readoutName").textContent = target.name;
$("readoutStatus").textContent = state.busy
? "Animated contact transfer in progress"
: "Both contacts locked";
$("contactA").classList.toggle("locked", state.contactA);
$("contactB").classList.toggle("locked", state.contactB);
$("outerButton").classList.toggle("active", state.targetMode === "outer");
$("innerButton").classList.toggle("active", state.targetMode === "inner");
$("outerButton").disabled = state.busy;
$("innerButton").disabled = state.busy;
const progress = activeSegment
? Math.min(1, segmentElapsed / activeSegment.duration)
: state.busy
? 0
: 1;
$("progressFill").style.transform = `scaleX(${progress})`;
$("labelASurface").textContent = target.leftSurface;
$("labelBSurface").textContent = target.rightSurface;
$("labelBValue").textContent = `${target.value.toFixed(1)} mm`;
}
// ---------------------------------------------------------------------
// Per-frame tape, labels, and rendering
// ---------------------------------------------------------------------
const xAxis = new THREE.Vector3(1, 0, 0);
const tapeDirection = new THREE.Vector3();
const tapeMidpoint = new THREE.Vector3();
const projected = new THREE.Vector3();
function updateTape(dt) {
pulseA = Math.max(0, pulseA - dt * 1.35);
pulseB = Math.max(0, pulseB - dt * 1.35);
tapeDirection.subVectors(state.right, state.left);
const length = tapeDirection.length();
if (length > 0.035) {
tapeBlade.visible = true;
tapeMidpoint.addVectors(state.left, state.right).multiplyScalar(0.5);
tapeBlade.position.copy(tapeMidpoint);
tapeBlade.quaternion.setFromUnitVectors(
xAxis,
tapeDirection.normalize()
);
tapeBlade.scale.set(length, 1, 1);
drawTape(length * MM_PER_UNIT);
hook.visible = true;
hook.position.copy(state.right);
hook.position.z += 0.055;
} else {
tapeBlade.visible = false;
hook.visible = false;
}
// The reel housing is always attached to reference contact A.
housing.position.copy(state.left);
housing.position.x -= 0.19;
housing.position.y -= 0.035;
housing.position.z += 0.085;
reelPivot.rotation.z = -length * 1.45;
referenceRing.visible = state.contactA;
tipRing.visible = state.contactB;
referenceRing.position.copy(state.left);
referenceRing.position.z += 0.026;
tipRing.position.copy(state.right);
tipRing.position.z += 0.026;
const pulseScaleA = 1 + Math.sin((1 - pulseA) * Math.PI) * 0.48;
const pulseScaleB = 1 + Math.sin((1 - pulseB) * Math.PI) * 0.48;
referenceRing.scale.setScalar(pulseScaleA);
tipRing.scale.setScalar(pulseScaleB);
}
function placeLabel(element, worldPosition, visible) {
if (!visible) {
element.style.opacity = "0";
return;
}
projected.copy(worldPosition).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.left = `${x}px`;
element.style.top = `${y}px`;
element.style.opacity = "1";
}
function updateLabels() {
placeLabel($("labelA"), state.left, state.contactA);
placeLabel($("labelB"), state.right, state.contactB);
}
function onResize() {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
}
window.addEventListener("resize", onResize);
const clock = new THREE.Clock();
function animate() {
const dt = Math.min(clock.getDelta(), 0.05);
updateSequence(dt);
updateTape(dt);
applyCamera();
updateLabels();
updateUI();
renderer.render(scene, camera);
requestAnimationFrame(animate);
}
resetCamera();
startMeasurement("outer");
animate();
</script>
</body>
</html>
```