Prompt specification
Build or customize the Three.js 3D scene "Beat envelope ribbon model" 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: Beat envelope ribbon model
Category: Science & Physics · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/beat-envelope-ribbon-model-x1521
Scene Description Prompt:
I'd like to see this as a low-poly Three.js scene: beat envelope ribbon model. Show two nearby-frequency sine waves and their sum as aligned ribbons; overlay the slow amplitude envelope and label the difference-frequency relationship clearly. Use cdnjs and return one self-contained HTML file.
Working Three.js Starter (beat-envelope-ribbon-model-x1521):
```html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="color-scheme" content="dark" />
<title>Beat Envelope — Low-Poly Ribbon Model</title>
<style>
:root {
--bg: #07111f;
--panel: rgba(9, 21, 37, 0.82);
--border: rgba(161, 204, 233, 0.17);
--text: #eaf4ff;
--muted: #8da7bd;
--cyan: #52dcff;
--coral: #ff758c;
--sum: #e8efff;
--gold: #ffc857;
}
* {
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;
}
body {
user-select: none;
}
#app,
canvas {
position: fixed;
inset: 0;
width: 100%;
height: 100%;
}
canvas {
display: block;
touch-action: none;
cursor: grab;
}
canvas.dragging {
cursor: grabbing;
}
.hud {
position: fixed;
z-index: 10;
pointer-events: none;
}
.title-block {
top: 24px;
left: 28px;
max-width: 430px;
}
.eyebrow {
margin-bottom: 8px;
color: var(--cyan);
font-size: 11px;
font-weight: 800;
letter-spacing: 0.2em;
text-transform: uppercase;
}
h1 {
margin: 0;
font-size: clamp(30px, 4vw, 54px);
font-weight: 760;
letter-spacing: -0.045em;
line-height: 0.98;
}
.subtitle {
max-width: 390px;
margin: 12px 0 0;
color: var(--muted);
font-size: 13px;
line-height: 1.55;
}
.equation-panel {
top: 24px;
right: 24px;
width: min(360px, calc(100vw - 48px));
padding: 18px;
border: 1px solid var(--border);
border-radius: 16px;
background:
linear-gradient(145deg, rgba(22, 43, 66, 0.76), rgba(7, 17, 31, 0.9)),
var(--panel);
box-shadow: 0 18px 60px rgba(0, 0, 0, 0.28);
backdrop-filter: blur(16px);
pointer-events: auto;
}
.panel-heading {
display: flex;
align-items: center;
justify-content: space-between;
margin-bottom: 14px;
color: var(--muted);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.17em;
text-transform: uppercase;
}
.live-dot {
display: inline-block;
width: 7px;
height: 7px;
margin-right: 7px;
border-radius: 50%;
background: var(--gold);
box-shadow: 0 0 12px rgba(255, 200, 87, 0.8);
}
.frequencies {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 9px;
margin-bottom: 13px;
}
.frequency {
padding: 9px 10px;
border: 1px solid rgba(255, 255, 255, 0.08);
border-radius: 10px;
background: rgba(255, 255, 255, 0.035);
color: var(--muted);
font-size: 11px;
}
.frequency strong {
display: block;
margin-top: 2px;
color: var(--text);
font-size: 16px;
font-variant-numeric: tabular-nums;
}
.formula {
padding: 12px;
border-radius: 11px;
background: rgba(1, 8, 17, 0.38);
color: #d9e8f8;
font-family: Georgia, "Times New Roman", serif;
font-size: 14px;
line-height: 1.6;
}
.formula .lhs {
display: block;
color: var(--text);
}
.formula .rhs {
display: block;
color: #c8d8eb;
}
.metrics {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 9px;
margin-top: 12px;
}
.metric {
padding: 10px;
border-left: 2px solid rgba(82, 220, 255, 0.55);
background: rgba(82, 220, 255, 0.045);
}
.metric.gold {
border-left-color: var(--gold);
background: rgba(255, 200, 87, 0.055);
}
.metric span {
display: block;
color: var(--muted);
font-size: 9px;
font-weight: 750;
letter-spacing: 0.11em;
text-transform: uppercase;
}
.metric strong {
display: block;
margin-top: 4px;
font-size: 15px;
font-variant-numeric: tabular-nums;
}
.envelope-note {
margin-top: 12px;
color: var(--muted);
font-size: 11px;
line-height: 1.55;
}
.envelope-note b {
color: var(--gold);
}
.legend {
left: 28px;
bottom: 24px;
display: flex;
flex-wrap: wrap;
gap: 8px;
max-width: min(660px, calc(100vw - 56px));
}
.legend-item {
display: inline-flex;
align-items: center;
gap: 7px;
padding: 8px 10px;
border: 1px solid var(--border);
border-radius: 999px;
background: rgba(8, 20, 35, 0.72);
color: #c8d8e8;
font-size: 10px;
font-weight: 650;
letter-spacing: 0.03em;
backdrop-filter: blur(10px);
}
.swatch {
width: 17px;
height: 4px;
border-radius: 5px;
background: currentColor;
box-shadow: 0 0 10px currentColor;
}
.cyan { color: var(--cyan); }
.coral { color: var(--coral); }
.sum { color: var(--sum); }
.gold { color: var(--gold); }
.controls-hint {
right: 26px;
bottom: 24px;
color: #7891a8;
font-size: 10px;
letter-spacing: 0.04em;
text-align: right;
}
#cursor-readout {
position: fixed;
z-index: 10;
left: 50%;
bottom: 24px;
transform: translateX(-50%);
padding: 8px 12px;
border: 1px solid var(--border);
border-radius: 999px;
background: rgba(7, 17, 31, 0.76);
color: #b9ccdf;
font-size: 10px;
font-variant-numeric: tabular-nums;
letter-spacing: 0.08em;
pointer-events: none;
backdrop-filter: blur(10px);
}
#labels {
position: fixed;
z-index: 8;
inset: 0;
overflow: hidden;
pointer-events: none;
}
.scene-label {
position: absolute;
display: flex;
align-items: center;
gap: 8px;
min-width: max-content;
padding: 7px 10px;
border: 1px solid rgba(255, 255, 255, 0.14);
border-radius: 8px;
background: rgba(6, 15, 27, 0.82);
box-shadow: 0 7px 25px rgba(0, 0, 0, 0.2);
color: var(--text);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.08em;
text-transform: uppercase;
transform: translate(-50%, -50%);
backdrop-filter: blur(9px);
}
.scene-label::before {
content: "";
width: 7px;
height: 7px;
flex: 0 0 auto;
border-radius: 50%;
background: currentColor;
box-shadow: 0 0 11px currentColor;
}
.scene-label small {
display: block;
margin-top: 2px;
color: #9eb4c9;
font-size: 8px;
font-weight: 650;
letter-spacing: 0.04em;
text-transform: none;
}
.time-label {
position: absolute;
color: #668198;
font-size: 9px;
font-variant-numeric: tabular-nums;
transform: translate(-50%, -50%);
}
#boot {
position: fixed;
z-index: 50;
inset: 0;
display: grid;
place-items: center;
background: var(--bg);
color: #91a9bf;
font-size: 11px;
font-weight: 750;
letter-spacing: 0.18em;
text-transform: uppercase;
transition: opacity 350ms ease;
}
#boot.hidden {
opacity: 0;
pointer-events: none;
}
@media (max-width: 780px) {
.title-block {
top: 16px;
left: 16px;
}
.title-block .subtitle {
display: none;
}
.equation-panel {
top: auto;
right: 12px;
bottom: 12px;
width: calc(100vw - 24px);
padding: 13px;
}
.panel-heading,
.frequencies,
.envelope-note {
display: none;
}
.formula {
padding: 9px;
font-size: 12px;
}
.metrics {
margin-top: 8px;
}
.legend,
.controls-hint,
#cursor-readout {
display: none;
}
.scene-label {
padding: 5px 7px;
font-size: 8px;
}
.scene-label small {
font-size: 7px;
}
}
</style>
</head>
<body>
<div id="app">
<canvas id="scene"></canvas>
</div>
<header class="hud title-block">
<div class="eyebrow">Low-poly harmonic study / 01</div>
<h1>Beat Envelope</h1>
<p class="subtitle">
Two nearby tones, one shared time axis. Their sum becomes a fast carrier
whose amplitude is shaped by the slower difference-frequency envelope.
</p>
</header>
<aside class="hud equation-panel">
<div class="panel-heading">
<span><i class="live-dot"></i>Difference-frequency relationship</span>
<span>Δf = 1 Hz</span>
</div>
<div class="frequencies">
<div class="frequency">WAVE 1<strong>f₁ = 3 Hz</strong></div>
<div class="frequency">WAVE 2<strong>f₂ = 4 Hz</strong></div>
</div>
<div class="formula">
<span class="lhs">sin(2πf₁t) + sin(2πf₂t)</span>
<span class="rhs">= 2 sin(2πf̄t) cos(πΔft)</span>
</div>
<div class="metrics">
<div class="metric">
<span>Average carrier</span>
<strong>f̄ = 3.5 Hz</strong>
</div>
<div class="metric gold">
<span>Beat rate</span>
<strong>Δf = |f₂ − f₁| = 1 Hz</strong>
</div>
</div>
<div class="envelope-note">
Gold boundary: <b>A<sub>env</sub>(t) = 2A|cos(πΔft)|</b>.
The white sum ribbon is shown unscaled and rides inside this slow envelope.
</div>
</aside>
<div class="hud legend">
<div class="legend-item"><i class="swatch cyan"></i>f₁ · 3 Hz</div>
<div class="legend-item"><i class="swatch coral"></i>f₂ · 4 Hz</div>
<div class="legend-item"><i class="swatch sum"></i>f₁ + f₂</div>
<div class="legend-item"><i class="swatch gold"></i>± amplitude envelope</div>
</div>
<div class="hud controls-hint">
Drag to orbit · Scroll to zoom<br />
Double-click or press R to reset
</div>
<div id="cursor-readout">shared time · t = 0.00 s</div>
<div id="labels"></div>
<div id="boot">Constructing ribbon model…</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 labelsLayer = document.querySelector("#labels");
const boot = document.querySelector("#boot");
const cursorReadout = document.querySelector("#cursor-readout");
// Model parameters.
const DURATION = 8;
const F1 = 3;
const F2 = 4;
const DELTA_F = Math.abs(F2 - F1);
const AVERAGE_F = (F1 + F2) / 2;
const AMPLITUDE = 0.7;
const SEGMENTS = 256;
const X_START = -8;
const X_END = 8;
const LANE_Y = {
wave1: 4.35,
wave2: 2.55,
sum: 0.15
};
const xForTime = (t) =>
THREE.MathUtils.lerp(X_START, X_END, t / DURATION);
const wave1 = (t) =>
AMPLITUDE * Math.sin(2 * Math.PI * F1 * t);
const wave2 = (t) =>
AMPLITUDE * Math.sin(2 * Math.PI * F2 * t);
const sumWave = (t) => wave1(t) + wave2(t);
const envelopeMagnitude = (t) =>
2 * AMPLITUDE *
Math.abs(
Math.cos(
Math.PI *
(F2 - F1) *
t
)
);
// Renderer and scene.
const renderer = new THREE.WebGLRenderer({
canvas,
antialias: true,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio || 1, 2));
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.12;
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x07111f);
scene.fog = new THREE.FogExp2(0x07111f, 0.026);
const camera = new THREE.PerspectiveCamera(
40,
window.innerWidth / window.innerHeight,
0.1,
100
);
const orbit = {
theta: 0.10,
phi: 1.19,
radius: window.innerWidth < 700 ? 23 : 18.5,
target: new THREE.Vector3(0, 1.35, 0),
dragging: false,
pointerX: 0,
pointerY: 0
};
function updateCamera() {
const sinPhi = Math.sin(orbit.phi);
camera.position.set(
orbit.target.x + orbit.radius * sinPhi * Math.sin(orbit.theta),
orbit.target.y + orbit.radius * Math.cos(orbit.phi),
orbit.target.z + orbit.radius * sinPhi * Math.cos(orbit.theta)
);
camera.lookAt(orbit.target);
camera.updateMatrixWorld();
}
function resetCamera() {
orbit.theta = 0.10;
orbit.phi = 1.19;
orbit.radius = window.innerWidth < 700 ? 23 : 18.5;
updateCamera();
}
// Lighting.
scene.add(new THREE.HemisphereLight(0xa9e5ff, 0x07101d, 1.8));
const keyLight = new THREE.DirectionalLight(0xffffff, 2.7);
keyLight.position.set(-5, 10, 8);
scene.add(keyLight);
const cyanLight = new THREE.PointLight(0x35cfff, 18, 18, 2);
cyanLight.position.set(-5, 4, 4);
scene.add(cyanLight);
const coralLight = new THREE.PointLight(0xff526f, 14, 16, 2);
coralLight.position.set(5, 2.5, 3);
scene.add(coralLight);
const goldLight = new THREE.PointLight(0xffbd42, 12, 14, 2);
goldLight.position.set(3, 0, 4);
scene.add(goldLight);
// Ground and reference grid.
const floor = new THREE.Mesh(
new THREE.PlaneGeometry(42, 34),
new THREE.MeshStandardMaterial({
color: 0x081321,
roughness: 1,
metalness: 0
})
);
floor.rotation.x = -Math.PI / 2;
floor.position.y = -2.12;
scene.add(floor);
const grid = new THREE.GridHelper(
30,
30,
0x31516d,
0x142a3d
);
grid.position.y = -2.095;
grid.material.transparent = true;
grid.material.opacity = 0.46;
scene.add(grid);
const timeAxisGeometry = new THREE.BufferGeometry().setFromPoints([
new THREE.Vector3(X_START, -2.06, 0.82),
new THREE.Vector3(X_END, -2.06, 0.82)
]);
const timeAxis = new THREE.Line(
timeAxisGeometry,
new THREE.LineBasicMaterial({
color: 0x5d819f,
transparent: true,
opacity: 0.5
})
);
scene.add(timeAxis);
function createRibbon({
valueAt,
baseY = 0,
width = 1.15,
zOffset = 0,
colorA,
colorB,
material
}) {
const positions = [];
const colors = [];
const indices = [];
const cA = new THREE.Color(colorA);
const cB = new THREE.Color(colorB);
const color = new THREE.Color();
for (let i = 0; i <= SEGMENTS; i++) {
const u = i / SEGMENTS;
const t = u * DURATION;
const x = THREE.MathUtils.lerp(X_START, X_END, u);
const y = baseY + valueAt(t);
color.copy(cA).lerp(cB, 0.5 - 0.5 * Math.cos(Math.PI * u));
positions.push(
x, y, zOffset - width / 2,
x, y, zOffset + width / 2
);
colors.push(
color.r, color.g, color.b,
color.r, color.g, color.b
);
if (i < SEGMENTS) {
const a = i * 2;
const b = a + 1;
const c = a + 2;
const d = a + 3;
indices.push(a, b, c, b, d, c);
}
}
const geometry = new THREE.BufferGeometry();
geometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(positions, 3)
);
geometry.setAttribute(
"color",
new THREE.Float32BufferAttribute(colors, 3)
);
geometry.setIndex(indices);
geometry.computeVertexNormals();
geometry.computeBoundingSphere();
return new THREE.Mesh(geometry, material);
}
function createEdgeLines(valueAt, baseY, width, color) {
const group = new THREE.Group();
const edgeMaterial = new THREE.LineBasicMaterial({
color,
transparent: true,
opacity: 0.72
});
for (const side of [-1, 1]) {
const points = [];
for (let i = 0; i <= SEGMENTS; i++) {
const u = i / SEGMENTS;
const t = u * DURATION;
points.push(
new THREE.Vector3(
THREE.MathUtils.lerp(X_START, X_END, u),
baseY + valueAt(t),
side * width / 2
)
);
}
const geometry = new THREE.BufferGeometry().setFromPoints(points);
group.add(new THREE.Line(geometry, edgeMaterial));
}
return group;
}
function createLane({
baseY,
valueAt,
colorA,
colorB,
emissive,
width = 1.15
}) {
const group = new THREE.Group();
const material = new THREE.MeshStandardMaterial({
vertexColors: true,
flatShading: true,
side: THREE.DoubleSide,
roughness: 0.72,
metalness: 0.08,
emissive: new THREE.Color(emissive),
emissiveIntensity: 0.25
});
const ribbon = createRibbon({
valueAt,
baseY,
width,
colorA,
colorB,
material
});
group.add(ribbon);
group.add(
createEdgeLines(
valueAt,
baseY,
width,
new THREE.Color(colorB).lerp(new THREE.Color(0xffffff), 0.25)
)
);
const baseline = new THREE.Line(
new THREE.BufferGeometry().setFromPoints([
new THREE.Vector3(X_START, baseY, width / 2 + 0.09),
new THREE.Vector3(X_END, baseY, width / 2 + 0.09)
]),
new THREE.LineDashedMaterial({
color: new THREE.Color(colorB),
transparent: true,
opacity: 0.24,
dashSize: 0.16,
gapSize: 0.12
})
);
baseline.computeLineDistances();
group.add(baseline);
scene.add(group);
return group;
}
createLane({
baseY: LANE_Y.wave1,
valueAt: wave1,
colorA: 0x3bd9ff,
colorB: 0x3278ff,
emissive: 0x063d56
});
createLane({
baseY: LANE_Y.wave2,
valueAt: wave2,
colorA: 0xff6c86,
colorB: 0xffb257,
emissive: 0x531424
});
createLane({
baseY: LANE_Y.sum,
valueAt: sumWave,
colorA: 0xf7fbff,
colorB: 0x9caeff,
emissive: 0x26365f,
width: 1.12
});
// Gold, slow amplitude envelope around the sum ribbon.
const envelopeGroup = new THREE.Group();
const envelopeZ = 0.78;
function createEnvelopeCurtain(z, opacity) {
const positions = [];
const indices = [];
for (let i = 0; i <= SEGMENTS; i++) {
const u = i / SEGMENTS;
const t = u * DURATION;
const x = THREE.MathUtils.lerp(X_START, X_END, u);
const amount = envelopeMagnitude(t);
positions.push(
x, LANE_Y.sum + amount, z,
x, LANE_Y.sum - amount, z
);
if (i < SEGMENTS) {
const a = i * 2;
const b = a + 1;
const c = a + 2;
const d = a + 3;
indices.push(a, b, c, b, d, c);
}
}
const geometry = new THREE.BufferGeometry();
geometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(positions, 3)
);
geometry.setIndex(indices);
const mesh = new THREE.Mesh(
geometry,
new THREE.MeshBasicMaterial({
color: 0xffc857,
transparent: true,
opacity,
side: THREE.DoubleSide,
depthWrite: false
})
);
mesh.renderOrder = 4;
return mesh;
}
envelopeGroup.add(createEnvelopeCurtain(envelopeZ, 0.075));
envelopeGroup.add(createEnvelopeCurtain(-envelopeZ, 0.035));
const goldMaterial = new THREE.MeshBasicMaterial({
color: 0xffc857,
side: THREE.DoubleSide,
transparent: true,
opacity: 0.96
});
for (const z of [-envelopeZ, envelopeZ]) {
for (const sign of [-1, 1]) {
const boundary = createRibbon({
valueAt: (t) => sign * envelopeMagnitude(t),
baseY: LANE_Y.sum,
width: 0.042,
zOffset: z,
colorA: 0xffd979,
colorB: 0xffa62f,
material: goldMaterial
});
boundary.renderOrder = 5;
envelopeGroup.add(boundary);
}
}
scene.add(envelopeGroup);
// Time ticks.
const tickMaterial = new THREE.MeshBasicMaterial({
color: 0x6389a7,
transparent: true,
opacity: 0.55
});
for (let t = 0; t <= DURATION; t += 1) {
const major = t % 2 === 0;
const tick = new THREE.Mesh(
new THREE.BoxGeometry(
0.025,
0.025,
major ? 0.72 : 0.42
),
tickMaterial
);
tick.position.set(xForTime(t), -2.055, 0.82);
scene.add(tick);
}
// A subtle moving playhead reinforces the shared time alignment.
const playheadGeometry = new THREE.BufferGeometry().setFromPoints([
new THREE.Vector3(X_START, -2.02, -1.02),
new THREE.Vector3(X_START, 5.45, -1.02)
]);
const playhead = new THREE.Line(
playheadGeometry,
new THREE.LineBasicMaterial({
color: 0xc9e8ff,
transparent: true,
opacity: 0.18,
depthWrite: false
})
);
scene.add(playhead);
const playheadMarker = new THREE.Mesh(
new THREE.OctahedronGeometry(0.075, 0),
new THREE.MeshBasicMaterial({ color: 0x9fe9ff })
);
playheadMarker.position.set(X_START, 5.45, -1.02);
scene.add(playheadMarker);
// HTML labels projected from world-space anchors.
function createSceneLabel(className, html, anchor) {
const element = document.createElement("div");
element.className = `scene-label ${className}`;
element.innerHTML = html;
labelsLayer.appendChild(element);
return { element, anchor };
}
const sceneLabels = [
createSceneLabel(
"cyan",
"WAVE 1<small>f₁(t) = A sin(2π · 3t)</small>",
new THREE.Vector3(-6.65, 5.38, 0.63)
),
createSceneLabel(
"coral",
"WAVE 2<small>f₂(t) = A sin(2π · 4t)</small>",
new THREE.Vector3(-6.65, 3.58, 0.63)
),
createSceneLabel(
"sum",
"SUM RIBBON<small>f₁(t) + f₂(t) · unscaled</small>",
new THREE.Vector3(-6.65, -0.92, 0.63)
),
createSceneLabel(
"gold",
"BEAT ENVELOPE<small>Δf = |f₂ − f₁| = 1 Hz → beat rate</small>",
new THREE.Vector3(xForTime(6), 1.98, envelopeZ)
)
];
const timeLabels = [];
for (let t = 0; t <= DURATION; t += 2) {
const element = document.createElement("div");
element.className = "time-label";
element.textContent = `${t}s`;
labelsLayer.appendChild(element);
timeLabels.push({
element,
anchor: new THREE.Vector3(xForTime(t), -1.94, 1.35)
});
}
function projectLabel(label) {
const p = label.anchor.clone().project(camera);
const visible = p.z > -1 && p.z < 1;
if (!visible) {
label.element.style.display = "none";
return;
}
label.element.style.display = "";
label.element.style.left =
`${(p.x * 0.5 + 0.5) * window.innerWidth}px`;
label.element.style.top =
`${(-p.y * 0.5 + 0.5) * window.innerHeight}px`;
}
// Camera controls implemented directly, without addons.
canvas.addEventListener("pointerdown", (event) => {
if (event.button !== 0) return;
orbit.dragging = true;
orbit.pointerX = event.clientX;
orbit.pointerY = event.clientY;
canvas.classList.add("dragging");
canvas.setPointerCapture(event.pointerId);
});
canvas.addEventListener("pointermove", (event) => {
if (!orbit.dragging) return;
const dx = event.clientX - orbit.pointerX;
const dy = event.clientY - orbit.pointerY;
orbit.theta -= dx * 0.006;
orbit.phi = THREE.MathUtils.clamp(
orbit.phi + dy * 0.005,
0.38,
1.48
);
orbit.pointerX = event.clientX;
orbit.pointerY = event.clientY;
updateCamera();
});
function endDrag(event) {
orbit.dragging = false;
canvas.classList.remove("dragging");
if (event.pointerId !== undefined &&
canvas.hasPointerCapture(event.pointerId)) {
canvas.releasePointerCapture(event.pointerId);
}
}
canvas.addEventListener("pointerup", endDrag);
canvas.addEventListener("pointercancel", endDrag);
canvas.addEventListener(
"wheel",
(event) => {
event.preventDefault();
orbit.radius = THREE.MathUtils.clamp(
orbit.radius * Math.exp(event.deltaY * 0.001),
10,
31
);
updateCamera();
},
{ passive: false }
);
canvas.addEventListener("dblclick", resetCamera);
window.addEventListener("keydown", (event) => {
if (event.key.toLowerCase() === "r") {
resetCamera();
}
});
function resize() {
const width = window.innerWidth;
const height = window.innerHeight;
renderer.setPixelRatio(
Math.min(window.devicePixelRatio || 1, width < 700 ? 1.7 : 2)
);
renderer.setSize(width, height, false);
camera.aspect = width / height;
camera.updateProjectionMatrix();
}
window.addEventListener("resize", resize);
resize();
updateCamera();
const clock = new THREE.Clock();
let firstFrame = true;
function animate() {
requestAnimationFrame(animate);
const elapsed = clock.getElapsedTime();
const t = elapsed % DURATION;
const x = xForTime(t);
const playheadPositions =
playhead.geometry.attributes.position;
playheadPositions.setX(0, x);
playheadPositions.setX(1, x);
playheadPositions.needsUpdate = true;
playheadMarker.position.x = x;
cursorReadout.textContent =
`shared time · t = ${t.toFixed(2)} s`;
for (const label of sceneLabels) {
projectLabel(label);
}
for (const label of timeLabels) {
projectLabel(label);
}
renderer.render(scene, camera);
if (firstFrame) {
firstFrame = false;
boot.classList.add("hidden");
setTimeout(() => boot.remove(), 400);
}
}
animate();
</script>
</body>
</html>
```