Prompt specification
Build or customize the Three.js 3D scene "Longitudinal and transverse bead chains" 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: Longitudinal and transverse bead chains
Category: Space & Earth · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/longitudinal-and-transverse-bead-chains-x1182
Scene Description Prompt:
Animate two stylized bead chains: longitudinal compression beside transverse oscillation, with fixed propagation arrows. Build one HTML file using Three.js from cdnjs.
Working Three.js Starter (longitudinal-and-transverse-bead-chains-x1182):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Two Modes of Wave Propagation</title>
<style>
:root {
--cyan: #67e8f9;
--cyan-soft: rgba(103, 232, 249, 0.16);
--pink: #f472b6;
--pink-soft: rgba(244, 114, 182, 0.16);
--text: #eef8ff;
--muted: #91a7b9;
--line: rgba(164, 211, 235, 0.17);
--panel: rgba(8, 19, 31, 0.72);
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background:
radial-gradient(circle at 22% 24%, rgba(8, 145, 170, 0.17), transparent 31%),
radial-gradient(circle at 77% 24%, rgba(190, 55, 132, 0.14), transparent 31%),
radial-gradient(circle at 50% 100%, rgba(29, 78, 116, 0.13), transparent 46%),
#050b13;
color: var(--text);
font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
#stage {
position: fixed;
inset: 0;
}
#stage canvas {
display: block;
width: 100%;
height: 100%;
touch-action: none;
cursor: grab;
}
#stage canvas:active {
cursor: grabbing;
}
.header {
position: fixed;
top: 28px;
left: 30px;
z-index: 5;
pointer-events: none;
max-width: 480px;
}
.eyebrow {
display: flex;
align-items: center;
gap: 9px;
color: var(--cyan);
font-size: 10px;
font-weight: 750;
letter-spacing: 0.2em;
text-transform: uppercase;
}
.live-dot {
width: 6px;
height: 6px;
border-radius: 50%;
background: var(--cyan);
box-shadow: 0 0 14px var(--cyan);
animation: pulse 2.2s ease-in-out infinite;
}
@keyframes pulse {
0%, 100% { opacity: 0.45; transform: scale(0.8); }
50% { opacity: 1; transform: scale(1.15); }
}
h1 {
margin: 12px 0 7px;
font-size: clamp(25px, 3vw, 42px);
line-height: 1.02;
letter-spacing: -0.045em;
font-weight: 680;
}
.subtitle {
margin: 0;
color: var(--muted);
font-size: 12px;
line-height: 1.55;
letter-spacing: 0.015em;
}
.axis-badge {
position: fixed;
top: 30px;
right: 30px;
z-index: 5;
display: flex;
align-items: center;
gap: 10px;
padding: 9px 12px;
border: 1px solid var(--line);
border-radius: 999px;
background: rgba(7, 16, 27, 0.58);
backdrop-filter: blur(12px);
color: #bfd0de;
font-size: 9px;
font-weight: 700;
letter-spacing: 0.16em;
pointer-events: none;
}
.axis-badge b {
color: var(--text);
font-size: 14px;
line-height: 1;
}
#labels {
position: fixed;
inset: 0;
z-index: 4;
pointer-events: none;
}
.chain-label {
position: absolute;
width: min(300px, 42vw);
transform: translate(-50%, calc(-100% - 8px));
padding: 12px 14px 11px;
border: 1px solid var(--line);
border-radius: 11px;
background:
linear-gradient(135deg, rgba(10, 24, 38, 0.91), rgba(5, 13, 23, 0.76));
box-shadow: 0 16px 45px rgba(0, 0, 0, 0.2);
backdrop-filter: blur(13px);
will-change: left, top;
}
.chain-label::after {
content: "";
position: absolute;
left: 50%;
top: 100%;
width: 1px;
height: 15px;
background: linear-gradient(var(--accent), transparent);
opacity: 0.65;
}
.chain-label .index {
color: var(--accent);
font-size: 9px;
font-weight: 800;
letter-spacing: 0.18em;
text-transform: uppercase;
}
.chain-label h2 {
margin: 5px 0 5px;
font-size: clamp(13px, 1.35vw, 17px);
line-height: 1.1;
letter-spacing: -0.02em;
font-weight: 720;
}
.chain-label p {
margin: 0;
color: var(--muted);
font-size: 10px;
line-height: 1.45;
}
.label-meta {
display: flex;
justify-content: space-between;
align-items: center;
gap: 10px;
margin-top: 9px;
padding-top: 8px;
border-top: 1px solid var(--line);
color: #b8c8d4;
font-size: 8px;
font-weight: 750;
letter-spacing: 0.12em;
text-transform: uppercase;
}
.label-meta .direction {
color: var(--accent);
white-space: nowrap;
}
.footer {
position: fixed;
left: 30px;
right: 30px;
bottom: 24px;
z-index: 6;
display: flex;
justify-content: space-between;
align-items: flex-end;
gap: 20px;
pointer-events: none;
}
.legend {
display: flex;
flex-wrap: wrap;
align-items: center;
gap: 18px;
color: var(--muted);
font-size: 9px;
font-weight: 650;
letter-spacing: 0.09em;
text-transform: uppercase;
}
.legend span {
display: inline-flex;
align-items: center;
gap: 7px;
}
.legend i {
display: inline-block;
width: 8px;
height: 8px;
border: 1px solid currentColor;
border-radius: 50%;
}
.legend .equilibrium {
color: #7f98aa;
}
.legend .compression {
color: #fbbf24;
}
.legend .transverse {
color: var(--pink);
}
.controls {
display: flex;
align-items: center;
gap: 13px;
color: var(--muted);
font-size: 9px;
letter-spacing: 0.1em;
text-transform: uppercase;
}
#reset {
pointer-events: auto;
cursor: pointer;
padding: 8px 12px;
color: #c8d8e3;
font: inherit;
border: 1px solid var(--line);
border-radius: 999px;
background: rgba(8, 18, 29, 0.76);
transition: border-color 160ms ease, color 160ms ease, background 160ms ease;
}
#reset:hover {
color: white;
border-color: rgba(103, 232, 249, 0.55);
background: rgba(14, 35, 49, 0.9);
}
.fallback {
position: fixed;
inset: 0;
display: none;
place-items: center;
z-index: 20;
padding: 30px;
color: white;
text-align: center;
background: #07111d;
}
@media (max-width: 760px) {
.header {
top: 20px;
left: 18px;
max-width: 76vw;
}
h1 {
font-size: 25px;
}
.subtitle {
font-size: 10px;
}
.axis-badge {
top: 20px;
right: 18px;
padding: 8px 10px;
}
.axis-badge .desktop-only {
display: none;
}
.chain-label {
width: 165px;
padding: 9px 10px;
}
.chain-label h2 {
font-size: 11px;
}
.chain-label p {
display: none;
}
.label-meta {
margin-top: 6px;
padding-top: 6px;
font-size: 7px;
}
.footer {
left: 18px;
right: 18px;
bottom: 17px;
}
.legend {
max-width: 48vw;
gap: 9px;
font-size: 7px;
}
.controls .hint {
display: none;
}
}
</style>
</head>
<body>
<main id="stage" aria-label="Interactive 3D animation comparing longitudinal and transverse bead-chain waves"></main>
<header class="header">
<div class="eyebrow"><span class="live-dot"></span> Wave mechanics / 01</div>
<h1>One direction.<br />Two ways to move.</h1>
<p class="subtitle">
Compare compression along the chain with oscillation across it.
Both patterns travel toward the fixed +X arrows.
</p>
</header>
<div class="axis-badge">
<b>+X</b>
<span class="desktop-only">Fixed propagation direction</span>
</div>
<div id="labels">
<article
id="longitudinal-label"
class="chain-label"
style="--accent: var(--cyan);"
>
<div class="index">01 / Parallel displacement</div>
<h2>Longitudinal compression</h2>
<p>The beads shift along the chain, alternately compressing and separating it.</p>
<div class="label-meta">
<span>Motion ∥ travel</span>
<span class="direction">→ Fixed propagation</span>
</div>
</article>
<article
id="transverse-label"
class="chain-label"
style="--accent: var(--pink);"
>
<div class="index">02 / Perpendicular displacement</div>
<h2>Transverse oscillation</h2>
<p>The beads move up and down while the chain’s overall direction remains fixed.</p>
<div class="label-meta">
<span>Motion ⟂ travel</span>
<span class="direction">→ Fixed propagation</span>
</div>
</article>
</div>
<footer class="footer">
<div class="legend">
<span><i class="equilibrium"></i> Equilibrium marker</span>
<span><i class="compression"></i> Compression zone</span>
<span><i class="transverse"></i> Transverse motion</span>
</div>
<div class="controls">
<span class="hint">Drag to orbit · Scroll to zoom</span>
<button id="reset" type="button">Reset view</button>
</div>
</footer>
<div class="fallback" id="fallback">
<div>
<h2>WebGL is required</h2>
<p>This visualization could not initialize a 3D rendering context.</p>
</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 stage = document.getElementById("stage");
const resetButton = document.getElementById("reset");
let renderer;
try {
renderer = new THREE.WebGLRenderer({
antialias: true,
alpha: true,
powerPreference: "high-performance"
});
} catch (error) {
document.getElementById("fallback").style.display = "grid";
throw error;
}
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.setClearColor(0x000000, 0);
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.18;
stage.appendChild(renderer.domElement);
const scene = new THREE.Scene();
const camera = new THREE.PerspectiveCamera(
42,
window.innerWidth / window.innerHeight,
0.1,
150
);
const hemi = new THREE.HemisphereLight(0xb9e6ff, 0x07101a, 1.65);
scene.add(hemi);
const key = new THREE.DirectionalLight(0xf3fbff, 3.2);
key.position.set(-4, 9, 9);
key.castShadow = true;
key.shadow.mapSize.set(2048, 2048);
key.shadow.camera.left = -13;
key.shadow.camera.right = 13;
key.shadow.camera.top = 8;
key.shadow.camera.bottom = -8;
key.shadow.camera.near = 1;
key.shadow.camera.far = 35;
key.shadow.bias = -0.00025;
scene.add(key);
const cyanLight = new THREE.PointLight(0x22d3ee, 18, 13, 2);
cyanLight.position.set(-5.2, 1.1, 3.4);
scene.add(cyanLight);
const pinkLight = new THREE.PointLight(0xf472b6, 18, 13, 2);
pinkLight.position.set(5.0, 1.5, 3.1);
scene.add(pinkLight);
const rimLight = new THREE.DirectionalLight(0x7dd3fc, 1.15);
rimLight.position.set(3, 2, -8);
scene.add(rimLight);
const floor = new THREE.Mesh(
new THREE.PlaneGeometry(80, 80),
new THREE.MeshStandardMaterial({
color: 0x06101b,
roughness: 1,
metalness: 0.05,
transparent: true,
opacity: 0.88
})
);
floor.rotation.x = -Math.PI / 2;
floor.position.y = -1.735;
floor.receiveShadow = true;
scene.add(floor);
const grid = new THREE.GridHelper(38, 38, 0x25465d, 0x142a3d);
grid.position.y = -1.728;
grid.material.transparent = true;
grid.material.opacity = 0.23;
scene.add(grid);
const Y_AXIS = new THREE.Vector3(0, 1, 0);
const tempDirection = new THREE.Vector3();
const tempPoint = new THREE.Vector3();
function createPanel(centerX, accent) {
const backdropGeometry = new THREE.BoxGeometry(8.4, 3.05, 0.12);
const backdrop = new THREE.Mesh(
backdropGeometry,
new THREE.MeshStandardMaterial({
color: 0x091827,
roughness: 0.86,
metalness: 0.08
})
);
backdrop.position.set(centerX, 0.05, -0.62);
backdrop.receiveShadow = true;
scene.add(backdrop);
const edges = new THREE.LineSegments(
new THREE.EdgesGeometry(backdropGeometry),
new THREE.LineBasicMaterial({
color: accent,
transparent: true,
opacity: 0.23
})
);
edges.position.copy(backdrop.position);
scene.add(edges);
const base = new THREE.Mesh(
new THREE.BoxGeometry(8.68, 0.25, 3.15),
new THREE.MeshStandardMaterial({
color: 0x07121f,
roughness: 0.72,
metalness: 0.18
})
);
base.position.set(centerX, -1.60, 0.24);
base.castShadow = true;
base.receiveShadow = true;
scene.add(base);
const baseTop = new THREE.Mesh(
new THREE.BoxGeometry(8.4, 0.025, 2.9),
new THREE.MeshStandardMaterial({
color: 0x0c1e2d,
emissive: accent,
emissiveIntensity: 0.035,
roughness: 0.65,
metalness: 0.12
})
);
baseTop.position.set(centerX, -1.465, 0.24);
scene.add(baseTop);
const stripMaterial = new THREE.MeshBasicMaterial({
color: accent,
transparent: true,
opacity: 0.52
});
for (const side of [-1, 1]) {
const strip = new THREE.Mesh(
new THREE.BoxGeometry(0.025, 2.62, 0.025),
stripMaterial
);
strip.position.set(centerX + side * 4.07, 0.02, -0.545);
scene.add(strip);
}
}
function createFixedArrow(centerX, color) {
const originX = centerX - 1.55;
const y = 1.17;
const z = -0.28;
const length = 3.1;
const headLength = 0.28;
const shaftLength = length - headLength;
const material = new THREE.MeshStandardMaterial({
color,
emissive: color,
emissiveIntensity: 1.25,
roughness: 0.34,
metalness: 0.1
});
const shaft = new THREE.Mesh(
new THREE.CylinderGeometry(0.024, 0.024, shaftLength, 12),
material
);
shaft.rotation.z = -Math.PI / 2;
shaft.position.set(originX + shaftLength / 2, y, z);
scene.add(shaft);
const head = new THREE.Mesh(
new THREE.ConeGeometry(0.09, headLength, 16),
material
);
head.rotation.z = -Math.PI / 2;
head.position.set(originX + length - headLength / 2, y, z);
scene.add(head);
const tail = new THREE.Mesh(
new THREE.SphereGeometry(0.04, 12, 8),
material
);
tail.position.set(originX, y, z);
scene.add(tail);
}
createPanel(-4.35, 0x67e8f9);
createPanel(4.35, 0xf472b6);
createFixedArrow(-4.35, 0x67e8f9);
createFixedArrow(4.35, 0xf472b6);
function updateCylinder(mesh, a, b) {
tempDirection.subVectors(b, a);
const length = Math.max(tempDirection.length(), 0.0001);
mesh.position.copy(a).add(b).multiplyScalar(0.5);
mesh.quaternion.setFromUnitVectors(Y_AXIS, tempDirection.normalize());
mesh.scale.set(1, length, 1);
}
function makeTether(color) {
const geometry = new THREE.BufferGeometry();
geometry.setAttribute(
"position",
new THREE.BufferAttribute(new Float32Array(6), 3)
);
const line = new THREE.Line(
geometry,
new THREE.LineBasicMaterial({
color,
transparent: true,
opacity: 0.3,
depthWrite: false
})
);
line.frustumCulled = false;
scene.add(line);
return line;
}
function setTether(line, ax, ay, az, bx, by, bz) {
const positions = line.geometry.attributes.position.array;
positions[0] = ax;
positions[1] = ay;
positions[2] = az;
positions[3] = bx;
positions[4] = by;
positions[5] = bz;
line.geometry.attributes.position.needsUpdate = true;
}
function createChain(centerX, accent, mode) {
const count = 15;
const spacing = 0.48;
const chainZ = 0.16;
const equilibriumY = 0.07;
const beadGeometry = new THREE.SphereGeometry(0.135, 24, 16);
const linkGeometry = new THREE.CylinderGeometry(0.034, 0.034, 1, 12);
const linkMaterial = new THREE.MeshStandardMaterial({
color: mode === "longitudinal" ? 0xa9e8f5 : 0xeab4dc,
roughness: 0.28,
metalness: 0.58,
emissive: mode === "longitudinal" ? 0x164957 : 0x54203f,
emissiveIntensity: 0.24
});
const ghostMaterial = new THREE.MeshBasicMaterial({
color: accent,
transparent: true,
opacity: 0.23,
depthWrite: false
});
const ghostGeometry = new THREE.SphereGeometry(0.055, 12, 8);
const baselineGeometry = new THREE.BufferGeometry().setFromPoints([
new THREE.Vector3(centerX - 3.36, equilibriumY, chainZ),
new THREE.Vector3(centerX + 3.36, equilibriumY, chainZ)
]);
const baseline = new THREE.Line(
baselineGeometry,
new THREE.LineBasicMaterial({
color: accent,
transparent: true,
opacity: 0.24,
depthWrite: false
})
);
scene.add(baseline);
const beads = [];
const links = [];
const ghosts = [];
const tethers = [];
for (let i = 0; i < count; i++) {
const xLocal = (i - (count - 1) / 2) * spacing;
const material = new THREE.MeshStandardMaterial({
color: accent,
roughness: 0.22,
metalness: 0.48,
emissive: accent,
emissiveIntensity: 0.09
});
const bead = new THREE.Mesh(beadGeometry, material);
bead.castShadow = true;
bead.receiveShadow = true;
bead.position.set(centerX + xLocal, equilibriumY, chainZ);
scene.add(bead);
beads.push(bead);
const ghost = new THREE.Mesh(ghostGeometry, ghostMaterial);
ghost.position.set(centerX + xLocal, equilibriumY, chainZ);
scene.add(ghost);
ghosts.push(ghost);
const tether = makeTether(accent);
tethers.push(tether);
if (i < count - 1) {
const link = new THREE.Mesh(linkGeometry, linkMaterial);
link.castShadow = true;
scene.add(link);
links.push(link);
}
}
return {
centerX,
mode,
count,
spacing,
chainZ,
equilibriumY,
beads,
links,
ghosts,
tethers
};
}
const longitudinal = createChain(-4.35, 0x67e8f9, "longitudinal");
const transverse = createChain(4.35, 0xf472b6, "transverse");
const cyanColor = new THREE.Color(0x67e8f9);
const amberColor = new THREE.Color(0xfbbf24);
const pinkColor = new THREE.Color(0xf472b6);
const violetColor = new THREE.Color(0xc084fc);
function animateChain(chain, elapsed) {
const waveNumber = 1.55;
const phase = elapsed * 1.65;
for (let i = 0; i < chain.count; i++) {
const xLocal = (i - (chain.count - 1) / 2) * chain.spacing;
const wave = waveNumber * xLocal - phase;
const bead = chain.beads[i];
if (chain.mode === "longitudinal") {
const amplitude = 0.33;
const displacement = amplitude * Math.sin(wave);
const derivative = amplitude * waveNumber * Math.cos(wave);
const localSpacing = 1 + derivative;
const compression = THREE.MathUtils.clamp(
(1 / Math.max(localSpacing, 0.25) - 1) * 2.35,
0,
1
);
const x = chain.centerX + xLocal + displacement;
bead.position.set(x, chain.equilibriumY, chain.chainZ);
bead.material.color.lerpColors(cyanColor, amberColor, compression * 0.92);
bead.material.emissive.copy(bead.material.color);
bead.material.emissiveIntensity = 0.075 + compression * 0.19;
chain.ghosts[i].position.set(
chain.centerX + xLocal,
chain.equilibriumY,
chain.chainZ
);
setTether(
chain.tethers[i],
chain.centerX + xLocal,
chain.equilibriumY,
chain.chainZ,
x,
chain.equilibriumY,
chain.chainZ
);
} else {
const amplitude = 0.68;
const y = chain.equilibriumY + amplitude * Math.sin(wave);
bead.position.set(
chain.centerX + xLocal,
y,
chain.chainZ
);
const hueMix = 0.5 + 0.5 * Math.sin(wave);
bead.material.color.lerpColors(violetColor, pinkColor, hueMix);
bead.material.emissive.copy(bead.material.color);
bead.material.emissiveIntensity = 0.095 + 0.07 * hueMix;
chain.ghosts[i].position.set(
chain.centerX + xLocal,
chain.equilibriumY,
chain.chainZ
);
setTether(
chain.tethers[i],
chain.centerX + xLocal,
chain.equilibriumY,
chain.chainZ,
chain.centerX + xLocal,
y,
chain.chainZ
);
}
}
for (let i = 0; i < chain.links.length; i++) {
updateCylinder(
chain.links[i],
chain.beads[i].position,
chain.beads[i + 1].position
);
}
}
// Custom orbit controls implemented directly, without add-ons.
const orbit = {
theta: 0.025,
phi: 1.13,
radius: 14.6,
target: new THREE.Vector3(0, -0.08, 0),
interacted: false
};
function defaultRadius() {
const halfWidth = 9.15;
const verticalHalfAngle = THREE.MathUtils.degToRad(camera.fov * 0.5);
return Math.max(14.6, halfWidth / (Math.tan(verticalHalfAngle) * camera.aspect));
}
function applyCamera() {
camera.position.setFromSphericalCoords(
orbit.radius,
orbit.phi,
orbit.theta
).add(orbit.target);
camera.lookAt(orbit.target);
camera.updateMatrixWorld();
}
function resetView() {
orbit.theta = 0.025;
orbit.phi = 1.13;
orbit.target.set(0, -0.08, 0);
orbit.radius = defaultRadius();
orbit.interacted = false;
applyCamera();
}
let pointerDown = false;
let pointerId = null;
let lastX = 0;
let lastY = 0;
renderer.domElement.addEventListener("pointerdown", (event) => {
if (event.pointerType === "mouse" && event.button !== 0) return;
pointerDown = true;
pointerId = event.pointerId;
lastX = event.clientX;
lastY = event.clientY;
orbit.interacted = true;
renderer.domElement.setPointerCapture(event.pointerId);
});
renderer.domElement.addEventListener("pointermove", (event) => {
if (!pointerDown || event.pointerId !== pointerId) return;
const dx = event.clientX - lastX;
const dy = event.clientY - lastY;
lastX = event.clientX;
lastY = event.clientY;
if (event.shiftKey) {
const right = new THREE.Vector3().setFromMatrixColumn(camera.matrix, 0);
const up = new THREE.Vector3().setFromMatrixColumn(camera.matrix, 1);
const panScale = orbit.radius * 0.0009;
orbit.target.addScaledVector(right, -dx * panScale);
orbit.target.addScaledVector(up, dy * panScale);
orbit.target.x = THREE.MathUtils.clamp(orbit.target.x, -4, 4);
orbit.target.y = THREE.MathUtils.clamp(orbit.target.y, -2, 3);
} else {
orbit.theta -= dx * 0.005;
orbit.phi = THREE.MathUtils.clamp(orbit.phi - dy * 0.004, 0.25, 1.46);
}
applyCamera();
});
function endPointer(event) {
if (event.pointerId !== pointerId) return;
pointerDown = false;
pointerId = null;
}
renderer.domElement.addEventListener("pointerup", endPointer);
renderer.domElement.addEventListener("pointercancel", endPointer);
renderer.domElement.addEventListener(
"wheel",
(event) => {
event.preventDefault();
orbit.interacted = true;
orbit.radius = THREE.MathUtils.clamp(
orbit.radius * Math.exp(event.deltaY * 0.001),
8.5,
58
);
applyCamera();
},
{ passive: false }
);
renderer.domElement.addEventListener("dblclick", resetView);
resetButton.addEventListener("click", resetView);
window.addEventListener("keydown", (event) => {
const key = event.key.toLowerCase();
let handled = true;
if (key === "arrowleft") orbit.theta += 0.08;
else if (key === "arrowright") orbit.theta -= 0.08;
else if (key === "arrowup") orbit.phi = THREE.MathUtils.clamp(orbit.phi - 0.06, 0.25, 1.46);
else if (key === "arrowdown") orbit.phi = THREE.MathUtils.clamp(orbit.phi + 0.06, 0.25, 1.46);
else if (key === "+" || key === "=") orbit.radius = Math.max(8.5, orbit.radius - 0.8);
else if (key === "-") orbit.radius = Math.min(58, orbit.radius + 0.8);
else handled = false;
if (handled) {
event.preventDefault();
orbit.interacted = true;
applyCamera();
}
});
function resize() {
const width = window.innerWidth;
const height = window.innerHeight;
camera.aspect = width / height;
camera.fov = camera.aspect < 1 ? 55 : 42;
camera.updateProjectionMatrix();
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(width, height);
if (!orbit.interacted) {
orbit.radius = defaultRadius();
applyCamera();
}
}
window.addEventListener("resize", resize);
const labels = [
{
element: document.getElementById("longitudinal-label"),
anchor: new THREE.Vector3(-4.35, 1.72, -0.1)
},
{
element: document.getElementById("transverse-label"),
anchor: new THREE.Vector3(4.35, 1.72, -0.1)
}
];
function updateLabels() {
const width = window.innerWidth;
const height = window.innerHeight;
for (const label of labels) {
tempPoint.copy(label.anchor).project(camera);
const x = (tempPoint.x * 0.5 + 0.5) * width;
const y = (-tempPoint.y * 0.5 + 0.5) * height;
const visible =
tempPoint.z > -1 &&
tempPoint.z < 1 &&
x > -220 &&
x < width + 220 &&
y > -100 &&
y < height + 160;
label.element.style.left = `${x}px`;
label.element.style.top = `${y}px`;
label.element.style.opacity = visible ? "1" : "0";
}
}
resize();
resetView();
const clock = new THREE.Clock();
let elapsed = 0;
renderer.setAnimationLoop(() => {
elapsed += Math.min(clock.getDelta(), 0.05);
animateChain(longitudinal, elapsed);
animateChain(transverse, elapsed);
applyCamera();
updateLabels();
renderer.render(scene, camera);
});
</script>
</body>
</html>
```