Prompt specification
Build or customize the Three.js 3D scene "Vibration along a string" 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: Vibration along a string
Category: Games & Play · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/vibration-along-a-string-x0382
Scene Description Prompt:
Make a standalone HTML scene with Three.js from cdnjs that depicts a string telephone. Animate an enlarged schematic wave traveling from one cup to the other; compare taut and slack states with labels stating the qualitative nature of the demonstration. Low-poly.
Working Three.js Starter (vibration-along-a-string-x0382):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>String Telephone — Taut vs. Slack</title>
<style>
:root {
--bg: #07111d;
--panel: rgba(10, 22, 36, 0.82);
--line: rgba(190, 220, 235, 0.16);
--text: #eef7fb;
--muted: #9db2bf;
--cyan: #65e7ee;
--orange: #ffb477;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: var(--bg);
color: var(--text);
font-family: Inter, ui-sans-serif, system-ui, -apple-system,
BlinkMacSystemFont, "Segoe UI", sans-serif;
}
canvas {
display: block;
width: 100%;
height: 100%;
outline: none;
touch-action: none;
}
#hud {
position: fixed;
z-index: 10;
top: 18px;
left: 18px;
width: min(370px, calc(100vw - 36px));
padding: 18px 18px 16px;
border: 1px solid var(--line);
border-radius: 18px;
background: var(--panel);
box-shadow: 0 18px 60px rgba(0, 0, 0, 0.28);
backdrop-filter: blur(14px);
pointer-events: none;
}
.eyebrow {
margin-bottom: 7px;
color: var(--cyan);
font-size: 11px;
font-weight: 800;
letter-spacing: 0.18em;
text-transform: uppercase;
}
h1 {
margin: 0;
font-size: clamp(24px, 3vw, 34px);
line-height: 1.05;
letter-spacing: -0.035em;
}
#intro {
margin: 10px 0 15px;
color: var(--muted);
font-size: 13px;
line-height: 1.5;
}
.controls {
display: flex;
gap: 8px;
margin-bottom: 14px;
pointer-events: auto;
}
button {
appearance: none;
border: 1px solid rgba(255, 255, 255, 0.16);
border-radius: 10px;
padding: 9px 12px;
background: rgba(255, 255, 255, 0.07);
color: var(--text);
font: inherit;
font-size: 12px;
font-weight: 750;
cursor: pointer;
transition: background 160ms ease, transform 160ms ease;
}
button:hover {
background: rgba(255, 255, 255, 0.13);
transform: translateY(-1px);
}
button:focus-visible {
outline: 2px solid var(--cyan);
outline-offset: 2px;
}
.comparison {
display: grid;
gap: 8px;
}
.comparison-row {
display: grid;
grid-template-columns: 10px 74px 1fr;
align-items: center;
gap: 9px;
padding-top: 8px;
border-top: 1px solid var(--line);
font-size: 12px;
}
.comparison-row:first-child {
padding-top: 0;
border-top: 0;
}
.dot {
width: 8px;
height: 8px;
border-radius: 50%;
box-shadow: 0 0 14px currentColor;
}
.dot.cyan {
color: var(--cyan);
background: var(--cyan);
}
.dot.orange {
color: var(--orange);
background: var(--orange);
}
.comparison-row b {
font-size: 12px;
}
.comparison-row span:last-child {
color: var(--muted);
line-height: 1.35;
}
#legend {
position: fixed;
z-index: 10;
left: 50%;
bottom: 18px;
transform: translateX(-50%);
display: flex;
align-items: center;
gap: 14px;
max-width: calc(100vw - 36px);
padding: 10px 14px;
border: 1px solid var(--line);
border-radius: 999px;
background: rgba(8, 19, 31, 0.78);
color: var(--muted);
font-size: 11px;
line-height: 1.35;
text-align: center;
backdrop-filter: blur(12px);
pointer-events: none;
}
.pulse-key {
display: inline-flex;
align-items: center;
gap: 7px;
color: var(--text);
white-space: nowrap;
font-weight: 700;
}
.pulse-key::before {
content: "";
width: 14px;
height: 14px;
border: 2px solid var(--cyan);
border-radius: 50%;
box-shadow: 0 0 12px rgba(101, 231, 238, 0.6);
}
#help {
position: fixed;
z-index: 10;
right: 18px;
bottom: 18px;
padding: 9px 12px;
border: 1px solid var(--line);
border-radius: 10px;
background: rgba(8, 19, 31, 0.7);
color: var(--muted);
font-size: 10px;
line-height: 1.5;
text-align: right;
backdrop-filter: blur(10px);
pointer-events: none;
}
#loading {
position: fixed;
z-index: 50;
inset: 0;
display: grid;
place-items: center;
background: var(--bg);
color: var(--muted);
font-size: 13px;
letter-spacing: 0.08em;
text-transform: uppercase;
transition: opacity 350ms ease;
}
#loading.hidden {
opacity: 0;
pointer-events: none;
}
@media (max-width: 760px) {
#hud {
top: 10px;
left: 10px;
width: min(330px, calc(100vw - 20px));
padding: 13px;
}
#intro {
display: none;
}
.comparison {
margin-top: 10px;
}
#help {
display: none;
}
#legend {
bottom: 10px;
width: calc(100vw - 20px);
border-radius: 12px;
}
}
</style>
</head>
<body>
<div id="hud">
<div class="eyebrow">Mechanics / Qualitative Comparison</div>
<h1>String telephone</h1>
<p id="intro">
Watch an enlarged pulse travel between paired cups. A taut string
preserves the pulse more clearly; a slack string couples less strongly
and delivers a weaker, delayed response.
</p>
<div class="controls">
<button id="pauseButton" type="button">Pause</button>
<button id="restartButton" type="button">Restart</button>
<button id="viewButton" type="button">Reset view</button>
</div>
<div class="comparison">
<div class="comparison-row">
<i class="dot cyan"></i>
<b>Taut</b>
<span>Fast, direct, well-defined pulse</span>
</div>
<div class="comparison-row">
<i class="dot orange"></i>
<b>Slack</b>
<span>Slower, weaker, less coupling</span>
</div>
</div>
</div>
<div id="legend">
<span class="pulse-key">Enlarged pulse packet</span>
<span>Schematic and exaggerated — not a scale simulation</span>
</div>
<div id="help">
Drag: orbit · Shift/right-drag: pan<br>
Wheel: zoom · Double-click: reset view
</div>
<div id="loading">Loading low-poly scene…</div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// -------------------------------------------------------------------------
// Renderer and scene
// -------------------------------------------------------------------------
const renderer = new THREE.WebGLRenderer({ antialias: true });
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.tabIndex = 0;
document.body.prepend(renderer.domElement);
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x07111d);
scene.fog = new THREE.Fog(0x07111d, 17, 35);
const camera = new THREE.PerspectiveCamera(
45,
window.innerWidth / window.innerHeight,
0.1,
100
);
const cameraTarget = new THREE.Vector3(0, 1.2, 0);
const defaultCamera = {
radius: 14.2,
theta: 0,
phi: 1.23,
target: new THREE.Vector3(0, 1.2, 0)
};
let cameraRadius = defaultCamera.radius;
let cameraTheta = defaultCamera.theta;
let cameraPhi = defaultCamera.phi;
let userChangedCamera = false;
function updateCamera() {
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() {
cameraRadius = defaultCamera.radius;
cameraTheta = defaultCamera.theta;
cameraPhi = defaultCamera.phi;
cameraTarget.copy(defaultCamera.target);
userChangedCamera = false;
updateCamera();
}
updateCamera();
// Lighting
scene.add(new THREE.HemisphereLight(0xc9ecff, 0x142131, 2.0));
const keyLight = new THREE.DirectionalLight(0xffffff, 3.2);
keyLight.position.set(-5, 10, 8);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(1024, 1024);
keyLight.shadow.camera.left = -12;
keyLight.shadow.camera.right = 12;
keyLight.shadow.camera.top = 10;
keyLight.shadow.camera.bottom = -8;
keyLight.shadow.camera.near = 1;
keyLight.shadow.camera.far = 30;
scene.add(keyLight);
const cyanLight = new THREE.PointLight(0x43dce8, 9, 8);
cyanLight.position.set(-3.5, 2.2, 2);
scene.add(cyanLight);
const orangeLight = new THREE.PointLight(0xff9c55, 8, 8);
orangeLight.position.set(3.5, 2.0, 2);
scene.add(orangeLight);
// -------------------------------------------------------------------------
// Low-poly environment
// -------------------------------------------------------------------------
const floor = new THREE.Mesh(
new THREE.PlaneGeometry(38, 24),
new THREE.MeshStandardMaterial({
color: 0x0a1826,
roughness: 0.96,
metalness: 0.02
})
);
floor.rotation.x = -Math.PI / 2;
floor.position.y = -0.08;
floor.receiveShadow = true;
scene.add(floor);
function makePlatform(x, accent) {
const group = new THREE.Group();
const platform = new THREE.Mesh(
new THREE.BoxGeometry(5.45, 0.18, 3.75),
new THREE.MeshStandardMaterial({
color: 0x102334,
roughness: 0.78,
metalness: 0.14,
flatShading: true
})
);
platform.position.set(x, 0.01, 0);
platform.receiveShadow = true;
group.add(platform);
const edges = new THREE.LineSegments(
new THREE.EdgesGeometry(platform.geometry),
new THREE.LineBasicMaterial({
color: 0x3a596b,
transparent: true,
opacity: 0.45
})
);
edges.position.copy(platform.position);
group.add(edges);
const accentStrip = new THREE.Mesh(
new THREE.BoxGeometry(4.85, 0.035, 0.075),
new THREE.MeshBasicMaterial({ color: accent })
);
accentStrip.position.set(x, 0.13, 1.52);
group.add(accentStrip);
scene.add(group);
}
makePlatform(-3.5, 0x65e7ee);
makePlatform(3.5, 0xffb477);
const grid = new THREE.GridHelper(30, 30, 0x294255, 0x172a39);
grid.position.y = -0.065;
grid.material.transparent = true;
grid.material.opacity = 0.25;
scene.add(grid);
// -------------------------------------------------------------------------
// Cups
// -------------------------------------------------------------------------
const cupBodyMaterial = new THREE.MeshStandardMaterial({
color: 0xc5d0d4,
roughness: 0.64,
metalness: 0.3,
flatShading: true,
side: THREE.DoubleSide
});
const cupInteriorMaterial = new THREE.MeshStandardMaterial({
color: 0x101a20,
roughness: 0.9,
metalness: 0.05,
side: THREE.DoubleSide
});
function makeCup(endpoint, inwardDirection, accent, isSource) {
const group = new THREE.Group();
group.position.copy(endpoint);
// The cup mouth is placed at the endpoint, with the body extending away.
if (inwardDirection > 0) {
group.rotation.z = -Math.PI / 2;
} else {
group.rotation.z = Math.PI / 2;
}
const bodyGeometry = new THREE.CylinderGeometry(
0.58,
0.42,
0.65,
12,
1,
true
);
bodyGeometry.translate(0, -0.325, 0);
const body = new THREE.Mesh(bodyGeometry, cupBodyMaterial);
body.castShadow = true;
body.receiveShadow = true;
group.add(body);
const mouthRim = new THREE.Mesh(
new THREE.TorusGeometry(0.58, 0.045, 6, 16),
new THREE.MeshStandardMaterial({
color: 0xe7eff1,
roughness: 0.46,
metalness: 0.45,
flatShading: true
})
);
mouthRim.rotation.x = Math.PI / 2;
mouthRim.position.y = 0.012;
mouthRim.castShadow = true;
group.add(mouthRim);
const accentBand = new THREE.Mesh(
new THREE.TorusGeometry(0.55, 0.035, 5, 14),
new THREE.MeshStandardMaterial({
color: accent,
roughness: 0.52,
metalness: 0.25,
flatShading: true
})
);
accentBand.rotation.x = Math.PI / 2;
accentBand.position.y = -0.14;
group.add(accentBand);
const interior = new THREE.Mesh(
new THREE.CircleGeometry(0.525, 12),
cupInteriorMaterial
);
interior.rotation.x = -Math.PI / 2;
interior.position.y = -0.09;
group.add(interior);
const bottom = new THREE.Mesh(
new THREE.CylinderGeometry(0.42, 0.42, 0.07, 12),
cupBodyMaterial
);
bottom.position.y = -0.655;
bottom.castShadow = true;
group.add(bottom);
const connector = new THREE.Mesh(
new THREE.OctahedronGeometry(0.07, 0),
new THREE.MeshStandardMaterial({
color: accent,
emissive: accent,
emissiveIntensity: 0.6,
roughness: 0.45,
metalness: 0.2,
flatShading: true
})
);
connector.position.y = 0.015;
group.add(connector);
scene.add(group);
// Small faceted stand beneath the outer body.
const outward = -inwardDirection;
const standX = endpoint.x + outward * 0.325;
const post = new THREE.Mesh(
new THREE.CylinderGeometry(0.075, 0.09, 0.88, 8),
new THREE.MeshStandardMaterial({
color: 0x617582,
roughness: 0.72,
metalness: 0.42,
flatShading: true
})
);
post.position.set(standX, 0.58, 0);
post.castShadow = true;
scene.add(post);
const base = new THREE.Mesh(
new THREE.CylinderGeometry(0.32, 0.37, 0.09, 10),
new THREE.MeshStandardMaterial({
color: 0x263946,
roughness: 0.8,
metalness: 0.28,
flatShading: true
})
);
base.position.set(standX, 0.16, 0);
base.castShadow = true;
base.receiveShadow = true;
scene.add(base);
group.userData.isSource = isSource;
group.userData.baseScale = 1;
return group;
}
// -------------------------------------------------------------------------
// Animated strings
// -------------------------------------------------------------------------
class AnimatedString {
constructor({ start, end, color, sag, amplitude, width, frequency, segments = 72 }) {
this.start = start.clone();
this.end = end.clone();
this.sag = sag;
this.amplitude = amplitude;
this.width = width;
this.frequency = frequency;
this.segments = segments;
this.geometry = new THREE.CylinderGeometry(
0.023,
0.023,
1,
6,
1,
false
);
this.material = new THREE.MeshStandardMaterial({
color,
roughness: 0.72,
metalness: 0.06,
flatShading: true
});
this.mesh = new THREE.InstancedMesh(
this.geometry,
this.material,
segments
);
this.mesh.instanceMatrix.setUsage(THREE.DynamicDrawUsage);
this.mesh.castShadow = true;
this.mesh.frustumCulled = false;
scene.add(this.mesh);
this.dummy = new THREE.Object3D();
this.p0 = new THREE.Vector3();
this.p1 = new THREE.Vector3();
this.direction = new THREE.Vector3();
this.up = new THREE.Vector3(0, 1, 0);
}
pointAt(t, progress, target = new THREE.Vector3()) {
const baseX = THREE.MathUtils.lerp(this.start.x, this.end.x, t);
const baseY = THREE.MathUtils.lerp(this.start.y, this.end.y, t);
const baseZ = THREE.MathUtils.lerp(this.start.z, this.end.z, t);
const curve = 4 * t * (1 - t);
const q = t - progress;
const envelope = Math.exp(-Math.pow(q / this.width, 2));
const wave = Math.cos(q * Math.PI * 2 * this.frequency);
const depthWave = Math.sin(q * Math.PI * 2 * this.frequency);
target.set(
baseX,
baseY - this.sag * curve + this.amplitude * envelope * wave,
baseZ + this.amplitude * 0.45 * envelope * depthWave
);
return target;
}
update(progress) {
for (let i = 0; i < this.segments; i++) {
const t0 = i / this.segments;
const t1 = (i + 1) / this.segments;
this.pointAt(t0, progress, this.p0);
this.pointAt(t1, progress, this.p1);
this.direction.subVectors(this.p1, this.p0);
const length = this.direction.length();
this.dummy.position.copy(this.p0).add(this.p1).multiplyScalar(0.5);
this.dummy.quaternion.setFromUnitVectors(
this.up,
this.direction.normalize()
);
this.dummy.scale.set(1, length, 1);
this.dummy.updateMatrix();
this.mesh.setMatrixAt(i, this.dummy.matrix);
}
this.mesh.instanceMatrix.needsUpdate = true;
}
}
class PulseMarker {
constructor(color, squashDepth = 1) {
this.group = new THREE.Group();
const ringMaterial = new THREE.MeshBasicMaterial({
color,
transparent: true,
opacity: 0.95,
depthWrite: false
});
const haloMaterial = new THREE.MeshBasicMaterial({
color,
transparent: true,
opacity: 0.22,
depthWrite: false
});
this.ring = new THREE.Mesh(
new THREE.TorusGeometry(0.28, 0.035, 7, 24),
ringMaterial
);
this.ring.rotation.y = Math.PI / 2;
this.group.add(this.ring);
this.halo = new THREE.Mesh(
new THREE.TorusGeometry(0.39, 0.012, 5, 28),
haloMaterial
);
this.halo.rotation.y = Math.PI / 2;
this.group.add(this.halo);
this.core = new THREE.Mesh(
new THREE.OctahedronGeometry(0.09, 0),
new THREE.MeshBasicMaterial({ color: 0xffffff })
);
this.group.add(this.core);
this.squashDepth = squashDepth;
scene.add(this.group);
}
update(position, progress) {
this.group.position.copy(position);
const edgeFade = Math.pow(Math.sin(Math.PI * progress), 0.28);
const breathing = 1 + Math.sin(progress * Math.PI * 8) * 0.06;
this.group.scale.set(
breathing,
breathing,
this.squashDepth * breathing
);
this.ring.material.opacity = 0.95 * edgeFade;
this.halo.material.opacity = 0.22 * edgeFade;
this.core.scale.setScalar(0.7 + edgeFade * 0.4);
}
}
function makeImpactHalo(position, color) {
const material = new THREE.MeshBasicMaterial({
color,
transparent: true,
opacity: 0,
depthWrite: false
});
const halo = new THREE.Mesh(
new THREE.TorusGeometry(0.66, 0.025, 6, 28),
material
);
halo.position.copy(position);
halo.rotation.y = Math.PI / 2;
scene.add(halo);
return halo;
}
const tautStart = new THREE.Vector3(-5.0, 1.38, 0);
const tautEnd = new THREE.Vector3(-2.0, 1.38, 0);
const slackStart = new THREE.Vector3(1.0, 1.38, 0);
const slackEnd = new THREE.Vector3(4.0, 1.38, 0);
const tautString = new AnimatedString({
start: tautStart,
end: tautEnd,
color: 0xc9edf1,
sag: 0,
amplitude: 0.17,
width: 0.105,
frequency: 3.6
});
const slackString = new AnimatedString({
start: slackStart,
end: slackEnd,
color: 0xd8b99e,
sag: 0.72,
amplitude: 0.066,
width: 0.18,
frequency: 2.15
});
const tautMarker = new PulseMarker(0x65e7ee, 1);
const slackMarker = new PulseMarker(0xffb477, 0.62);
const tautSourceCup = makeCup(tautStart, 1, 0x65e7ee, true);
const tautTargetCup = makeCup(tautEnd, -1, 0x65e7ee, false);
const slackSourceCup = makeCup(slackStart, 1, 0xffb477, true);
const slackTargetCup = makeCup(slackEnd, -1, 0xffb477, false);
const tautSourceHalo = makeImpactHalo(tautStart, 0x65e7ee);
const tautTargetHalo = makeImpactHalo(tautEnd, 0x65e7ee);
const slackSourceHalo = makeImpactHalo(slackStart, 0xffb477);
const slackTargetHalo = makeImpactHalo(slackEnd, 0xffb477);
// -------------------------------------------------------------------------
// Direction arrows and labels
// -------------------------------------------------------------------------
function makeArrow(startX, endX, color, z = 1.0) {
const group = new THREE.Group();
const material = new THREE.MeshBasicMaterial({ color });
const shaft = new THREE.Mesh(
new THREE.CylinderGeometry(0.018, 0.018, endX - startX - 0.24, 6),
material
);
shaft.rotation.z = -Math.PI / 2;
shaft.position.x = (startX + endX) / 2 - 0.06;
group.add(shaft);
const arrowHead = new THREE.Mesh(
new THREE.ConeGeometry(0.095, 0.25, 6),
material
);
arrowHead.rotation.z = -Math.PI / 2;
arrowHead.position.x = endX - 0.08;
group.add(arrowHead);
group.position.y = 0.18;
group.position.z = z;
scene.add(group);
}
makeArrow(-4.9, -2.15, 0x65e7ee);
makeArrow(1.1, 3.85, 0xffb477);
function roundedRect(ctx, x, y, w, h, radius) {
const r = Math.min(radius, w / 2, h / 2);
ctx.beginPath();
ctx.moveTo(x + r, y);
ctx.lineTo(x + w - r, y);
ctx.quadraticCurveTo(x + w, y, x + w, y + r);
ctx.lineTo(x + w, y + h - r);
ctx.quadraticCurveTo(x + w, y + h, x + w - r, y + h);
ctx.lineTo(x + r, y + h);
ctx.quadraticCurveTo(x, y + h, x, y + h - r);
ctx.lineTo(x, y + r);
ctx.quadraticCurveTo(x, y, x + r, y);
ctx.closePath();
}
function makeLabel(title, subtitle, accent, position) {
const canvas = document.createElement("canvas");
canvas.width = 1200;
canvas.height = 300;
const ctx = canvas.getContext("2d");
ctx.clearRect(0, 0, canvas.width, canvas.height);
roundedRect(ctx, 8, 8, 1184, 284, 38);
ctx.fillStyle = "rgba(7, 17, 29, 0.90)";
ctx.fill();
ctx.strokeStyle = accent;
ctx.lineWidth = 5;
ctx.stroke();
roundedRect(ctx, 30, 35, 13, 230, 7);
ctx.fillStyle = accent;
ctx.fill();
ctx.fillStyle = "#eef7fb";
ctx.font = "800 66px Inter, Arial, sans-serif";
ctx.textBaseline = "middle";
ctx.fillText(title, 75, 103);
ctx.fillStyle = "#a9bdc9";
ctx.font = "500 34px Inter, Arial, sans-serif";
ctx.fillText(subtitle, 76, 188);
const texture = new THREE.CanvasTexture(canvas);
texture.colorSpace = THREE.SRGBColorSpace;
texture.minFilter = THREE.LinearFilter;
const material = new THREE.SpriteMaterial({
map: texture,
transparent: true,
depthTest: false,
depthWrite: false,
toneMapped: false
});
const sprite = new THREE.Sprite(material);
sprite.position.copy(position);
sprite.scale.set(4.85, 1.21, 1);
sprite.renderOrder = 20;
scene.add(sprite);
}
makeLabel(
"TAUT / DIRECT",
"straight path • faster, clearer pulse",
"#65e7ee",
new THREE.Vector3(-3.5, 2.82, 0)
);
makeLabel(
"SLACK / DELAYED",
"loose curve • weaker, less coupled pulse",
"#ffb477",
new THREE.Vector3(3.5, 2.82, 0)
);
// -------------------------------------------------------------------------
// Animation
// -------------------------------------------------------------------------
let elapsed = 0;
let paused = false;
let previousTime = performance.now();
const markerPosition = new THREE.Vector3();
function updateSystem(system, marker, progress, sourceHalo, targetHalo,
sourceCup, targetCup) {
system.update(progress);
system.pointAt(progress, progress, markerPosition);
marker.update(markerPosition, progress);
const sourceImpact = Math.exp(
-Math.pow((progress - 0.018) / 0.038, 2)
);
const targetImpact = Math.exp(
-Math.pow((progress - 0.982) / 0.038, 2)
);
sourceHalo.material.opacity = sourceImpact * 0.75;
targetHalo.material.opacity = targetImpact * 0.75;
sourceHalo.scale.setScalar(0.85 + sourceImpact * 0.32);
targetHalo.scale.setScalar(0.85 + targetImpact * 0.38);
sourceCup.scale.setScalar(1 + sourceImpact * 0.018);
targetCup.scale.setScalar(1 + targetImpact * 0.022);
}
function animate(now) {
requestAnimationFrame(animate);
const delta = Math.min((now - previousTime) / 1000, 0.05);
previousTime = now;
if (!paused) {
elapsed += delta;
}
// The two demonstrations use different qualitative transit speeds.
const tautProgress = (elapsed / 4.2) % 1;
const slackProgress = (elapsed / 6.6) % 1;
updateSystem(
tautString,
tautMarker,
tautProgress,
tautSourceHalo,
tautTargetHalo,
tautSourceCup,
tautTargetCup
);
updateSystem(
slackString,
slackMarker,
slackProgress,
slackSourceHalo,
slackTargetHalo,
slackSourceCup,
slackTargetCup
);
renderer.render(scene, camera);
}
requestAnimationFrame(animate);
// -------------------------------------------------------------------------
// Direct camera controls — no addons
// -------------------------------------------------------------------------
let dragging = false;
let dragMode = "orbit";
let lastPointerX = 0;
let lastPointerY = 0;
renderer.domElement.addEventListener("contextmenu", (event) => {
event.preventDefault();
});
renderer.domElement.addEventListener("pointerdown", (event) => {
dragging = true;
dragMode =
event.button === 2 || event.shiftKey || event.ctrlKey
? "pan"
: "orbit";
lastPointerX = event.clientX;
lastPointerY = event.clientY;
renderer.domElement.setPointerCapture(event.pointerId);
userChangedCamera = true;
});
renderer.domElement.addEventListener("pointermove", (event) => {
if (!dragging) return;
const dx = event.clientX - lastPointerX;
const dy = event.clientY - lastPointerY;
lastPointerX = event.clientX;
lastPointerY = event.clientY;
if (dragMode === "orbit") {
cameraTheta -= dx * 0.005;
cameraPhi = THREE.MathUtils.clamp(
cameraPhi - dy * 0.005,
0.35,
1.48
);
} else {
const scale = cameraRadius * 0.0012;
cameraTarget.x -= dx * scale;
cameraTarget.y += dy * scale;
cameraTarget.x = THREE.MathUtils.clamp(
cameraTarget.x,
-7,
7
);
cameraTarget.y = THREE.MathUtils.clamp(
cameraTarget.y,
0.2,
4.2
);
}
updateCamera();
});
function endDrag(event) {
dragging = false;
if (
renderer.domElement.hasPointerCapture &&
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 * Math.exp(event.deltaY * 0.001),
7.5,
26
);
userChangedCamera = true;
updateCamera();
},
{ passive: false }
);
renderer.domElement.addEventListener("dblclick", resetCamera);
window.addEventListener("keydown", (event) => {
if (event.code === "Space") {
event.preventDefault();
togglePause();
} else if (event.key.toLowerCase() === "r") {
elapsed = 0;
} else if (event.key === "ArrowLeft") {
cameraTheta -= 0.12;
updateCamera();
} else if (event.key === "ArrowRight") {
cameraTheta += 0.12;
updateCamera();
} else if (event.key === "ArrowUp") {
cameraPhi = Math.max(0.35, cameraPhi - 0.08);
updateCamera();
} else if (event.key === "ArrowDown") {
cameraPhi = Math.min(1.48, cameraPhi + 0.08);
updateCamera();
}
});
// -------------------------------------------------------------------------
// Interface and resize
// -------------------------------------------------------------------------
const pauseButton = document.getElementById("pauseButton");
function togglePause() {
paused = !paused;
pauseButton.textContent = paused ? "Resume" : "Pause";
pauseButton.setAttribute("aria-pressed", String(paused));
}
pauseButton.addEventListener("click", togglePause);
document.getElementById("restartButton").addEventListener("click", () => {
elapsed = 0;
});
document.getElementById("viewButton").addEventListener("click", resetCamera);
function onResize() {
const width = window.innerWidth;
const height = window.innerHeight;
const aspect = width / height;
camera.aspect = aspect;
camera.updateProjectionMatrix();
renderer.setSize(width, height);
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
if (!userChangedCamera && aspect < 0.95) {
cameraRadius = Math.max(defaultCamera.radius, 13.2 / aspect);
updateCamera();
}
}
window.addEventListener("resize", onResize);
onResize();
requestAnimationFrame(() => {
document.getElementById("loading").classList.add("hidden");
});
</script>
</body>
</html>
```