Prompt specification
Build or customize the Three.js 3D scene "Harmonic component ribbon stack" 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: Harmonic component ribbon stack
Category: Science & Physics · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/harmonic-component-ribbon-stack-x1545
Scene Description Prompt:
Build a self-contained HTML scene with Three.js from cdnjs featuring Fourier components and musical harmonics. Build several sine-wave ribbons with integer-related frequencies above their summed waveform; align time markers so each component's contribution remains inspectable. Low-poly.
Technical checks: Label 880 Hz relative to 220 Hz as a 4:1 frequency ratio spanning two octaves. Keep harmonic labels, the summed waveform, and the phase control consistent.
Return one complete HTML document that works with the cdnjs Three.js r160 module inside a sandbox with scripts allowed. Make the requested subject visible at the default camera. Use procedural assets and local button handlers. Do not use native form submission, pointer lock, external assets or runtime requests other than Three.js.
Working Three.js Starter (harmonic-component-ribbon-stack-x1545):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Fourier Harmonics Lab</title>
<style>
:root {
color-scheme: dark;
--bg: #06101a;
--panel: #0a1724;
--panel-2: #0e1e2d;
--line: #20364a;
--text: #e9f4ff;
--muted: #8da6ba;
--cyan: #58e0d3;
--violet: #9b8cff;
--gold: #ffd166;
--coral: #ff8f70;
--pass: #7ee2a8;
--fail: #ff7b7b;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
}
body {
overflow: hidden;
background: var(--bg);
color: var(--text);
font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}
button,
input {
font: inherit;
}
button {
color: inherit;
}
#app {
display: grid;
grid-template-columns: minmax(0, 1fr) 350px;
width: 100%;
height: 100%;
}
#stage {
position: relative;
min-width: 0;
min-height: 0;
overflow: hidden;
background:
radial-gradient(circle at 50% 38%, rgba(35, 78, 104, 0.34), transparent 48%),
linear-gradient(155deg, #071522 0%, #050c14 70%);
}
#stage canvas {
display: block;
width: 100%;
height: 100%;
touch-action: none;
outline: none;
}
#stage canvas:focus-visible {
box-shadow: inset 0 0 0 2px var(--cyan);
}
.scene-heading {
position: absolute;
z-index: 3;
top: 20px;
left: 22px;
pointer-events: none;
text-shadow: 0 2px 14px #000;
}
.scene-heading .eyebrow {
color: var(--cyan);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.2em;
text-transform: uppercase;
}
.scene-heading h1 {
margin: 5px 0 2px;
font-size: clamp(20px, 2.2vw, 32px);
line-height: 1;
letter-spacing: -0.035em;
}
.scene-heading p {
margin: 5px 0 0;
color: #9db3c6;
font-size: 12px;
}
.scene-badge {
position: absolute;
z-index: 3;
top: 22px;
right: 20px;
padding: 7px 10px;
border: 1px solid rgba(88, 224, 211, 0.3);
border-radius: 999px;
background: rgba(7, 22, 34, 0.72);
color: #b8d4e7;
font-size: 10px;
font-weight: 700;
letter-spacing: 0.08em;
text-transform: uppercase;
backdrop-filter: blur(8px);
pointer-events: none;
}
.scene-hint {
position: absolute;
z-index: 3;
left: 22px;
bottom: 18px;
color: #6f8ba1;
font-size: 11px;
pointer-events: none;
}
aside {
position: relative;
z-index: 5;
min-height: 0;
overflow-y: auto;
padding: 22px 20px 26px;
border-left: 1px solid var(--line);
background:
linear-gradient(180deg, rgba(16, 34, 50, 0.98), rgba(7, 18, 29, 0.99));
box-shadow: -20px 0 60px rgba(0, 0, 0, 0.18);
scrollbar-width: thin;
scrollbar-color: #29475e transparent;
}
.brand-row {
display: flex;
align-items: center;
justify-content: space-between;
gap: 10px;
}
.brand {
margin: 0;
font-size: 22px;
line-height: 1;
letter-spacing: -0.04em;
}
.brand span {
color: var(--cyan);
}
.subtitle {
margin: 8px 0 0;
color: var(--muted);
font-size: 12px;
line-height: 1.5;
}
.check-badge {
flex: 0 0 auto;
min-width: 82px;
padding: 6px 8px;
border: 1px solid #294357;
border-radius: 6px;
background: #0a1926;
color: #92a9bb;
font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
font-size: 9px;
font-weight: 700;
text-align: center;
letter-spacing: 0.04em;
}
.check-badge.pass {
border-color: rgba(126, 226, 168, 0.45);
color: var(--pass);
background: rgba(126, 226, 168, 0.07);
}
.check-badge.fail {
border-color: rgba(255, 123, 123, 0.55);
color: var(--fail);
background: rgba(255, 123, 123, 0.08);
}
section {
margin-top: 22px;
padding-top: 19px;
border-top: 1px solid var(--line);
}
.section-title {
display: flex;
align-items: center;
justify-content: space-between;
gap: 10px;
margin-bottom: 12px;
}
.section-title h2 {
margin: 0;
color: #c8d9e8;
font-size: 10px;
font-weight: 800;
letter-spacing: 0.16em;
text-transform: uppercase;
}
.mini-button {
padding: 5px 8px;
border: 1px solid #2a465c;
border-radius: 5px;
background: #102536;
color: #a9c0d2;
font-size: 10px;
font-weight: 700;
cursor: pointer;
}
.mini-button:hover {
border-color: #46718e;
color: white;
}
.control-block + .control-block {
margin-top: 17px;
}
.control-label {
display: flex;
align-items: baseline;
justify-content: space-between;
gap: 12px;
margin-bottom: 8px;
font-size: 12px;
}
.control-label label {
color: #c4d5e4;
font-weight: 650;
}
output {
color: var(--cyan);
font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
font-size: 11px;
}
input[type="range"] {
width: 100%;
height: 18px;
margin: 0;
appearance: none;
background: transparent;
cursor: pointer;
}
input[type="range"]::-webkit-slider-runnable-track {
height: 4px;
border-radius: 999px;
background: linear-gradient(90deg, #315b70, #183044);
}
input[type="range"]::-webkit-slider-thumb {
width: 14px;
height: 14px;
margin-top: -5px;
appearance: none;
border: 2px solid #d8fffb;
border-radius: 50%;
background: var(--cyan);
box-shadow: 0 0 0 4px rgba(88, 224, 211, 0.12);
}
input[type="range"]::-moz-range-track {
height: 4px;
border-radius: 999px;
background: #24475c;
}
input[type="range"]::-moz-range-thumb {
width: 12px;
height: 12px;
border: 2px solid #d8fffb;
border-radius: 50%;
background: var(--cyan);
}
.hint {
margin: 7px 0 0;
color: #708ca2;
font-size: 10px;
line-height: 1.45;
}
.component-list {
display: grid;
gap: 7px;
}
.component {
display: grid;
grid-template-columns: 10px minmax(0, 1fr) auto;
align-items: center;
gap: 10px;
width: 100%;
padding: 9px 10px;
border: 1px solid #243c50;
border-radius: 7px;
background: #0c1d2b;
color: #dceaf5;
text-align: left;
cursor: pointer;
transition: border-color 140ms ease, background 140ms ease, opacity 140ms ease;
}
.component:hover {
border-color: #3b617c;
background: #102638;
}
.component:focus-visible,
button:focus-visible,
input:focus-visible {
outline: 2px solid var(--cyan);
outline-offset: 2px;
}
.component .swatch {
width: 8px;
height: 24px;
border-radius: 2px;
background: var(--component-color);
box-shadow: 0 0 13px color-mix(in srgb, var(--component-color) 45%, transparent);
}
.component-copy {
display: grid;
gap: 2px;
min-width: 0;
}
.component-copy strong {
font-size: 12px;
}
.component-copy span {
color: #809caf;
font-size: 10px;
}
.component .harmonic-number {
display: grid;
place-items: center;
width: 24px;
height: 24px;
border: 1px solid #2e4b60;
border-radius: 5px;
color: var(--component-color);
font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
font-size: 11px;
font-weight: 800;
}
.component:not(.is-on) {
opacity: 0.46;
}
.component:not(.is-on) .swatch {
background: transparent;
border: 1px solid var(--component-color);
box-shadow: none;
}
.button-row {
display: grid;
grid-template-columns: 1fr 1fr 1fr;
gap: 7px;
margin-top: 11px;
}
.action {
min-height: 34px;
padding: 8px 6px;
border: 1px solid #2b485d;
border-radius: 6px;
background: #102536;
color: #b7cbdc;
font-size: 10px;
font-weight: 750;
cursor: pointer;
}
.action:hover {
border-color: #48738f;
color: white;
background: #143047;
}
.action.primary {
border-color: rgba(88, 224, 211, 0.5);
background: rgba(88, 224, 211, 0.12);
color: #a9fff5;
}
.inspector {
overflow: hidden;
border: 1px solid #243c50;
border-radius: 7px;
background: #081824;
}
.inspector-row {
display: grid;
grid-template-columns: 68px 1fr auto;
align-items: center;
gap: 8px;
min-height: 34px;
padding: 6px 9px;
border-bottom: 1px solid #1d3345;
font-size: 11px;
}
.inspector-row:last-child {
border-bottom: 0;
}
.inspector-row .name {
color: #a8bfd1;
}
.inspector-row .state {
color: #638096;
font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
font-size: 8px;
letter-spacing: 0.08em;
}
.inspector-row .value {
color: var(--row-color, #dceaf5);
font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
font-size: 11px;
font-weight: 750;
text-align: right;
}
.inspector-row.total {
background: rgba(255, 255, 255, 0.035);
}
.theory-card {
padding: 13px;
border: 1px solid rgba(255, 209, 102, 0.3);
border-radius: 7px;
background:
linear-gradient(135deg, rgba(255, 209, 102, 0.09), rgba(255, 209, 102, 0.025));
}
.theory-card strong {
display: block;
margin-bottom: 5px;
color: var(--gold);
font-size: 13px;
}
.theory-card p {
margin: 0;
color: #a9bdcc;
font-size: 11px;
line-height: 1.55;
}
.ratio-equation {
display: flex;
align-items: center;
justify-content: space-between;
gap: 10px;
margin-top: 10px;
padding-top: 9px;
border-top: 1px solid rgba(255, 209, 102, 0.18);
color: #f5d98e;
font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
font-size: 11px;
}
.ratio-equation span:last-child {
color: var(--gold);
font-weight: 800;
}
@media (max-width: 860px) {
body {
overflow: auto;
}
#app {
display: block;
height: auto;
}
#stage {
height: 64vh;
min-height: 430px;
}
aside {
border-top: 1px solid var(--line);
border-left: 0;
overflow: visible;
}
.scene-badge {
top: auto;
right: 14px;
bottom: 14px;
}
}
</style>
</head>
<body>
<main id="app">
<div id="stage">
<div class="scene-heading">
<div class="eyebrow">Fourier / Musical Harmonics</div>
<h1>Four voices, one sum</h1>
<p>Integer harmonics on one shared time axis.</p>
</div>
<div class="scene-badge">Low-poly harmonic ribbons</div>
<div class="scene-hint">Drag to orbit · Wheel to zoom · Arrow keys when focused</div>
</div>
<aside aria-label="Fourier scene controls">
<div class="brand-row">
<h2 class="brand">Harmonic <span>Lab</span></h2>
<div id="check-badge" class="check-badge" aria-live="polite">CHECKING…</div>
</div>
<p class="subtitle">
Toggle integer harmonics, change their phase, and inspect every instantaneous contribution.
</p>
<section>
<div class="section-title">
<h2>Controls</h2>
</div>
<div class="control-block">
<div class="control-label">
<label for="phase">Fundamental phase φ</label>
<output id="phase-value" for="phase">0°</output>
</div>
<input id="phase" type="range" min="-180" max="180" step="1" value="0">
<p class="hint">Harmonic n receives phase nφ. The summed waveform is rebuilt from the same sampled data.</p>
</div>
<div class="control-block">
<div class="control-label">
<label for="cursor">Inspection time</label>
<output id="cursor-value" for="cursor">3.000 ms</output>
</div>
<input id="cursor" type="range" min="0" max="12000" step="1" value="3000">
<p class="hint">The bright vertical guide and all colored markers share exactly the same time coordinate.</p>
</div>
<div class="button-row">
<button id="play" class="action primary" type="button" aria-pressed="false">Play</button>
<button id="reset" class="action" type="button">Reset</button>
<button id="verify" class="action" type="button">Verify data</button>
</div>
</section>
<section>
<div class="section-title">
<h2>Harmonic components</h2>
<button id="enable-all" class="mini-button" type="button">Enable all</button>
</div>
<div class="component-list">
<button class="component is-on" data-id="h1" style="--component-color:#58e0d3" type="button" aria-pressed="true">
<span class="swatch"></span>
<span class="component-copy">
<strong>220 Hz</strong>
<span class="relation">1× · fundamental</span>
</span>
<span class="harmonic-number">1</span>
</button>
<button class="component is-on" data-id="h2" style="--component-color:#9b8cff" type="button" aria-pressed="true">
<span class="swatch"></span>
<span class="component-copy">
<strong>440 Hz</strong>
<span class="relation">2× · one octave</span>
</span>
<span class="harmonic-number">2</span>
</button>
<button class="component is-on" data-id="h3" style="--component-color:#ff8f70" type="button" aria-pressed="true">
<span class="swatch"></span>
<span class="component-copy">
<strong>660 Hz</strong>
<span class="relation">3× · octave + perfect fifth</span>
</span>
<span class="harmonic-number">3</span>
</button>
<button class="component is-on" data-id="h4" style="--component-color:#ffd166" type="button" aria-pressed="true">
<span class="swatch"></span>
<span class="component-copy">
<strong>880 Hz</strong>
<span class="relation">4× · two octaves</span>
</span>
<span class="harmonic-number">4</span>
</button>
</div>
<p class="hint">A disabled ribbon remains visible as a ghost for inspection, but it is excluded from the enabled sum.</p>
</section>
<section>
<div class="section-title">
<h2>Contribution at cursor</h2>
</div>
<div class="inspector" aria-live="polite">
<div class="inspector-row" style="--row-color:#58e0d3">
<span class="name">H1 · 220</span>
<span id="state-h1" class="state">ON</span>
<span id="value-h1" class="value">—</span>
</div>
<div class="inspector-row" style="--row-color:#9b8cff">
<span class="name">H2 · 440</span>
<span id="state-h2" class="state">ON</span>
<span id="value-h2" class="value">—</span>
</div>
<div class="inspector-row" style="--row-color:#ff8f70">
<span class="name">H3 · 660</span>
<span id="state-h3" class="state">ON</span>
<span id="value-h3" class="value">—</span>
</div>
<div class="inspector-row" style="--row-color:#ffd166">
<span class="name">H4 · 880</span>
<span id="state-h4" class="state">ON</span>
<span id="value-h4" class="value">—</span>
</div>
<div class="inspector-row total" style="--row-color:#ffffff">
<span class="name">Σ enabled</span>
<span class="state">SUM</span>
<span id="value-sum" class="value">—</span>
</div>
</div>
</section>
<section>
<div class="section-title">
<h2>Frequency relationship</h2>
</div>
<div class="theory-card">
<strong>Why 4× is two octaves</strong>
<p>
<strong>880 / 220 = 4.</strong> An octave doubles a frequency:
220 → 440 → 880. The 4× harmonic therefore spans exactly two octaves.
</p>
<div class="ratio-equation">
<span>880 Hz ÷ 220 Hz = 4×</span>
<span>TWO OCTAVES</span>
</div>
</div>
</section>
</aside>
</main>
<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 phaseInput = document.getElementById("phase");
const cursorInput = document.getElementById("cursor");
const phaseValue = document.getElementById("phase-value");
const cursorValue = document.getElementById("cursor-value");
const playButton = document.getElementById("play");
const resetButton = document.getElementById("reset");
const verifyButton = document.getElementById("verify");
const enableAllButton = document.getElementById("enable-all");
const checkBadge = document.getElementById("check-badge");
const TAU = Math.PI * 2;
const BASE_FREQUENCY = 220;
const DOMAIN_SECONDS = 0.012;
const SAMPLE_COUNT = 145;
const X_MIN = -7.2;
const X_MAX = 7.2;
const SUM_BASE_Y = -1.75;
let phaseDegrees = 0;
let phaseRadians = 0;
let cursorMilliseconds = 3000;
let playing = false;
const components = [
{
id: "h1",
harmonic: 1,
frequency: 220,
amplitude: 0.34,
baseY: 1.25,
color: 0x58e0d3,
css: "#58e0d3",
enabled: true,
samples: new Float32Array(SAMPLE_COUNT + 1)
},
{
id: "h2",
harmonic: 2,
frequency: 440,
amplitude: 0.42,
baseY: 2.65,
color: 0x9b8cff,
css: "#9b8cff",
enabled: true,
samples: new Float32Array(SAMPLE_COUNT + 1)
},
{
id: "h3",
harmonic: 3,
frequency: 660,
amplitude: 0.28,
baseY: 4.05,
color: 0xff8f70,
css: "#ff8f70",
enabled: true,
samples: new Float32Array(SAMPLE_COUNT + 1)
},
{
id: "h4",
harmonic: 4,
frequency: 880,
amplitude: 0.48,
baseY: 5.45,
color: 0xffd166,
css: "#ffd166",
enabled: true,
samples: new Float32Array(SAMPLE_COUNT + 1)
}
];
const scene = new THREE.Scene();
const renderer = new THREE.WebGLRenderer({
antialias: true,
alpha: true,
powerPreference: "high-performance"
});
renderer.setClearColor(0x06101a, 0);
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.18;
renderer.setPixelRatio(Math.min(window.devicePixelRatio || 1, 1.8));
stage.prepend(renderer.domElement);
renderer.domElement.tabIndex = 0;
renderer.domElement.setAttribute(
"aria-label",
"Interactive three-dimensional plot of four harmonic sine waves and their sum"
);
const camera = new THREE.PerspectiveCamera(42, 1, 0.1, 100);
const cameraTarget = new THREE.Vector3(0, 1.15, 0);
let cameraYaw = 0;
let cameraPitch = 0.08;
let cameraZoom = 1;
let baseCameraRadius = 21;
function updateCamera() {
const radius = baseCameraRadius * cameraZoom;
camera.position.set(
cameraTarget.x + radius * Math.sin(cameraYaw) * Math.cos(cameraPitch),
cameraTarget.y + radius * Math.sin(cameraPitch),
cameraTarget.z + radius * Math.cos(cameraYaw) * Math.cos(cameraPitch)
);
camera.lookAt(cameraTarget);
}
scene.add(new THREE.HemisphereLight(0xa7e9ff, 0x07111b, 1.55));
const keyLight = new THREE.DirectionalLight(0xffffff, 2.4);
keyLight.position.set(-5, 9, 11);
scene.add(keyLight);
const rimLight = new THREE.DirectionalLight(0x58e0d3, 1.15);
rimLight.position.set(9, 4, 7);
scene.add(rimLight);
function roundedRect(ctx, x, y, width, height, radius) {
const r = Math.min(radius, width / 2, height / 2);
ctx.beginPath();
ctx.moveTo(x + r, y);
ctx.lineTo(x + width - r, y);
ctx.quadraticCurveTo(x + width, y, x + width, y + r);
ctx.lineTo(x + width, y + height - r);
ctx.quadraticCurveTo(x + width, y + height, x + width - r, y + height);
ctx.lineTo(x + r, y + height);
ctx.quadraticCurveTo(x, y + height, x, y + height - r);
ctx.lineTo(x, y + r);
ctx.quadraticCurveTo(x, y, x + r, y);
ctx.closePath();
}
function makeTextSprite(text, color = "#dcecf8", options = {}) {
const canvas = document.createElement("canvas");
canvas.width = options.canvasWidth || 768;
canvas.height = 128;
const ctx = canvas.getContext("2d");
ctx.clearRect(0, 0, canvas.width, canvas.height);
if (options.background) {
roundedRect(ctx, 8, 8, canvas.width - 16, canvas.height - 16, 22);
ctx.fillStyle = options.background;
ctx.fill();
}
if (options.border) {
roundedRect(ctx, 8, 8, canvas.width - 16, canvas.height - 16, 22);
ctx.strokeStyle = options.border;
ctx.lineWidth = 3;
ctx.stroke();
}
ctx.font = `${options.weight || 700} ${options.fontSize || 44}px system-ui, sans-serif`;
ctx.fillStyle = color;
ctx.textAlign = "center";
ctx.textBaseline = "middle";
ctx.fillText(text, canvas.width / 2, canvas.height / 2 + 2);
const texture = new THREE.CanvasTexture(canvas);
texture.colorSpace = THREE.SRGBColorSpace;
texture.needsUpdate = true;
const material = new THREE.SpriteMaterial({
map: texture,
transparent: true,
depthTest: false,
depthWrite: false
});
const sprite = new THREE.Sprite(material);
const scale = options.scale || [2, 0.4];
sprite.scale.set(scale[0], scale[1], 1);
sprite.renderOrder = 20;
return sprite;
}
const panelGeometry = new THREE.PlaneGeometry(18, 11.2, 12, 8);
const panelMaterial = new THREE.MeshStandardMaterial({
color: 0x081522,
roughness: 0.96,
metalness: 0.02,
flatShading: true
});
const panel = new THREE.Mesh(panelGeometry, panelMaterial);
panel.position.set(0, 1.25, -1.15);
scene.add(panel);
const wirePanel = new THREE.Mesh(
panelGeometry,
new THREE.MeshBasicMaterial({
color: 0x31536a,
wireframe: true,
transparent: true,
opacity: 0.09,
depthWrite: false
})
);
wirePanel.position.set(0, 1.25, -1.12);
scene.add(wirePanel);
const floor = new THREE.Mesh(
new THREE.PlaneGeometry(28, 22, 14, 11),
new THREE.MeshStandardMaterial({
color: 0x07111b,
roughness: 1,
metalness: 0,
flatShading: true
})
);
floor.rotation.x = -Math.PI / 2;
floor.position.set(0, -3.84, -1);
scene.add(floor);
const floorGrid = new THREE.GridHelper(24, 24, 0x27475e, 0x12293b);
floorGrid.position.y = -3.80;
floorGrid.material.transparent = true;
floorGrid.material.opacity = 0.33;
scene.add(floorGrid);
function addLaneRail(y, color, opacity = 0.17) {
const rail = new THREE.Mesh(
new THREE.BoxGeometry(15.3, 0.018, 0.025),
new THREE.MeshBasicMaterial({
color,
transparent: true,
opacity,
depthWrite: false
})
);
rail.position.set(0, y, -0.35);
scene.add(rail);
return rail;
}
function makeRibbon(color, baseY, thickness = 0.11) {
const positions = new Float32Array((SAMPLE_COUNT + 1) * 2 * 3);
const indices = [];
for (let i = 0; i < SAMPLE_COUNT; i++) {
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.BufferAttribute(positions, 3));
geometry.setIndex(indices);
const material = new THREE.MeshStandardMaterial({
color,
emissive: new THREE.Color(color).multiplyScalar(0.13),
roughness: 0.62,
metalness: 0.04,
flatShading: true,
transparent: true,
opacity: 0.92,
side: THREE.DoubleSide
});
const mesh = new THREE.Mesh(geometry, material);
mesh.frustumCulled = false;
mesh.renderOrder = 2;
scene.add(mesh);
const lineGeometry = new THREE.BufferGeometry();
lineGeometry.setAttribute(
"position",
new THREE.BufferAttribute(new Float32Array((SAMPLE_COUNT + 1) * 3), 3)
);
const line = new THREE.Line(
lineGeometry,
new THREE.LineBasicMaterial({
color,
transparent: true,
opacity: 1
})
);
line.frustumCulled = false;
line.renderOrder = 4;
scene.add(line);
return { mesh, line, baseY, thickness, samples: new Float32Array(SAMPLE_COUNT + 1) };
}
function makeSegment(color, opacity = 0.8) {
const geometry = new THREE.BufferGeometry();
geometry.setAttribute("position", new THREE.BufferAttribute(new Float32Array(6), 3));
const material = new THREE.LineBasicMaterial({
color,
transparent: true,
opacity,
depthTest: false,
depthWrite: false
});
const line = new THREE.Line(geometry, material);
line.frustumCulled = false;
line.renderOrder = 7;
scene.add(line);
return line;
}
function makeMarker(color, size = 0.15) {
const marker = new THREE.Mesh(
new THREE.OctahedronGeometry(size, 0),
new THREE.MeshStandardMaterial({
color,
emissive: new THREE.Color(color).multiplyScalar(0.55),
roughness: 0.42,
metalness: 0.08,
flatShading: true,
transparent: true,
opacity: 1,
depthTest: false
})
);
marker.renderOrder = 10;
scene.add(marker);
return marker;
}
const sumRibbon = makeRibbon(0xf4fbff, SUM_BASE_Y, 0.16);
sumRibbon.mesh.material.emissive.setHex(0x203b49);
sumRibbon.mesh.material.opacity = 0.96;
for (const component of components) {
component.ribbon = makeRibbon(component.color, component.baseY, 0.105);
component.stem = makeSegment(component.color, 0.78);
component.marker = makeMarker(component.color, 0.15);
component.rail = addLaneRail(component.baseY, component.color, 0.15);
const label = makeTextSprite(
`H${component.harmonic} · ${component.frequency} Hz`,
component.css,
{
background: "rgba(5, 16, 26, 0.82)",
border: "rgba(85, 125, 151, 0.34)",
scale: [2.15, 0.43]
}
);
label.position.set(-8.08, component.baseY, 0.2);
scene.add(label);
component.label = label;
}
const sumRail = addLaneRail(SUM_BASE_Y, 0xe9f6ff, 0.22);
const sumLabel = makeTextSprite("Σ ENABLED", "#f1fbff", {
background: "rgba(5, 16, 26, 0.88)",
border: "rgba(153, 196, 219, 0.4)",
scale: [1.9, 0.43]
});
sumLabel.position.set(-8.08, SUM_BASE_Y, 0.2);
scene.add(sumLabel);
const relationLabel = makeTextSprite(
"880 / 220 = 4× · TWO OCTAVES",
"#ffd166",
{
background: "rgba(29, 24, 11, 0.90)",
border: "rgba(255, 209, 102, 0.55)",
fontSize: 39,
scale: [4.8, 0.57]
}
);
relationLabel.position.set(3.65, 6.33, 0.35);
scene.add(relationLabel);
const gridPositions = [];
for (let milliseconds = 0; milliseconds <= 12000; milliseconds += 2000) {
const fraction = milliseconds / 1000 / DOMAIN_SECONDS;
const x = THREE.MathUtils.lerp(X_MIN, X_MAX, fraction);
gridPositions.push(x, -3.43, -0.45, x, 6.12, -0.45);
const tick = makeTextSprite(`${milliseconds / 1000} ms`, "#718ca2", {
canvasWidth: 512,
fontSize: 38,
scale: [0.88, 0.34]
});
tick.position.set(x, -3.52, 0.15);
scene.add(tick);
}
const gridGeometry = new THREE.BufferGeometry();
gridGeometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(gridPositions, 3)
);
const gridLines = new THREE.LineSegments(
gridGeometry,
new THREE.LineDashedMaterial({
color: 0x36546b,
transparent: true,
opacity: 0.32,
dashSize: 0.13,
gapSize: 0.11,
depthWrite: false
})
);
gridLines.computeLineDistances();
scene.add(gridLines);
const cursorLine = makeSegment(0xffffff, 0.56);
const sumStem = makeSegment(0xffffff, 0.85);
const sumMarker = makeMarker(0xffffff, 0.19);
function componentValue(component, timeSeconds) {
return component.amplitude * Math.sin(
TAU * component.frequency * timeSeconds + component.harmonic * phaseRadians
);
}
function writeRibbon(target, values) {
const positions = target.mesh.geometry.attributes.position.array;
const linePositions = target.line.geometry.attributes.position.array;
for (let i = 0; i <= SAMPLE_COUNT; i++) {
const x = THREE.MathUtils.lerp(X_MIN, X_MAX, i / SAMPLE_COUNT);
const centerY = target.baseY + values[i];
const offset = i * 6;
positions[offset] = x;
positions[offset + 1] = centerY + target.thickness * 0.5;
positions[offset + 2] = 0;
positions[offset + 3] = x;
positions[offset + 4] = centerY - target.thickness * 0.5;
positions[offset + 5] = 0;
const lineOffset = i * 3;
linePositions[lineOffset] = x;
linePositions[lineOffset + 1] = centerY;
linePositions[lineOffset + 2] = 0.08;
}
target.mesh.geometry.attributes.position.needsUpdate = true;
target.mesh.geometry.computeVertexNormals();
target.mesh.geometry.computeBoundingSphere();
target.line.geometry.attributes.position.needsUpdate = true;
target.line.geometry.computeBoundingSphere();
}
function updateSegment(line, x, startY, endY, z = 0.2) {
const positions = line.geometry.attributes.position.array;
positions[0] = x;
positions[1] = startY;
positions[2] = z;
positions[3] = x;
positions[4] = endY;
positions[5] = z;
line.geometry.attributes.position.needsUpdate = true;
line.geometry.computeBoundingSphere();
}
function rebuildGeometry() {
const sumSamples = new Float32Array(SAMPLE_COUNT + 1);
for (let i = 0; i <= SAMPLE_COUNT; i++) {
const timeSeconds = (i / SAMPLE_COUNT) * DOMAIN_SECONDS;
let sum = 0;
for (const component of components) {
const value = componentValue(component, timeSeconds);
component.samples[i] = value;
if (component.enabled) sum += value;
}
sumSamples[i] = sum;
}
for (const component of components) {
writeRibbon(component.ribbon, component.samples);
}
writeRibbon(sumRibbon, sumSamples);
sumRibbon.samples = sumSamples;
}
function updateEnabledStyles() {
for (const component of components) {
const button = document.querySelector(`[data-id="${component.id}"]`);
button.classList.toggle("is-on", component.enabled);
button.setAttribute("aria-pressed", String(component.enabled));
component.ribbon.mesh.material.opacity = component.enabled ? 0.92 : 0.16;
component.ribbon.line.material.opacity = component.enabled ? 1 : 0.20;
component.stem.material.opacity = component.enabled ? 0.78 : 0.18;
component.marker.material.opacity = component.enabled ? 1 : 0.28;
component.rail.material.opacity = component.enabled ? 0.15 : 0.055;
component.label.material.opacity = component.enabled ? 1 : 0.38;
document.getElementById(`state-${component.id}`).textContent =
component.enabled ? "ON" : "OFF";
}
}
function signed(value) {
const sign = value >= 0 ? "+" : "−";
return `${sign}${Math.abs(value).toFixed(3)}`;
}
function updateCursor() {
const timeSeconds = cursorMilliseconds / 1000;
const fraction = THREE.MathUtils.clamp(timeSeconds / DOMAIN_SECONDS, 0, 1);
const x = THREE.MathUtils.lerp(X_MIN, X_MAX, fraction);
let enabledSum = 0;
updateSegment(cursorLine, x, -3.43, 6.15, -0.19);
for (const component of components) {
const value = componentValue(component, timeSeconds);
const markerY = component.baseY + value;
component.marker.position.set(x, markerY, 0.25);
updateSegment(component.stem, x, component.baseY, markerY, 0.19);
if (component.enabled) enabledSum += value;
document.getElementById(`value-${component.id}`).textContent = signed(value);
}
const sumY = SUM_BASE_Y + enabledSum;
sumMarker.position.set(x, sumY, 0.27);
updateSegment(sumStem, x, SUM_BASE_Y, sumY, 0.22);
document.getElementById("value-sum").textContent = signed(enabledSum);
cursorValue.textContent = `${cursorMilliseconds.toFixed(3)} ms`;
}
function verifySceneState(updateBadge = true) {
let maximumError = 0;
for (let i = 0; i <= SAMPLE_COUNT; i++) {
const timeSeconds = (i / SAMPLE_COUNT) * DOMAIN_SECONDS;
let expectedSum = 0;
for (const component of components) {
const expected = componentValue(component, timeSeconds);
const positions = component.ribbon.mesh.geometry.attributes.position.array;
const geometryValue =
((positions[i * 6 + 1] + positions[i * 6 + 4]) * 0.5) - component.baseY;
maximumError = Math.max(maximumError, Math.abs(geometryValue - expected));
if (component.enabled) expectedSum += expected;
}
const sumPositions = sumRibbon.mesh.geometry.attributes.position.array;
const geometrySum =
((sumPositions[i * 6 + 1] + sumPositions[i * 6 + 4]) * 0.5) - SUM_BASE_Y;
maximumError = Math.max(maximumError, Math.abs(geometrySum - expectedSum));
}
const timeSeconds = cursorMilliseconds / 1000;
let markerSum = 0;
for (const component of components) {
const expectedY = component.baseY + componentValue(component, timeSeconds);
maximumError = Math.max(
maximumError,
Math.abs(component.marker.position.y - expectedY)
);
if (component.enabled) markerSum += componentValue(component, timeSeconds);
}
maximumError = Math.max(
maximumError,
Math.abs(sumMarker.position.y - (SUM_BASE_Y + markerSum))
);
const ratiosAreIntegers = components.every(
(component) =>
component.frequency / BASE_FREQUENCY === component.harmonic &&
Number.isInteger(component.frequency / BASE_FREQUENCY)
);
const h4Relation = document
.querySelector('[data-id="h4"] .relation')
.textContent
.toLowerCase();
const labelIsCorrect =
h4Relation.includes("two octaves") &&
!h4Relation.includes("fourth") &&
880 / 220 === 4;
const passed = ratiosAreIntegers && labelIsCorrect && maximumError < 0.00002;
if (updateBadge) {
checkBadge.classList.toggle("pass", passed);
checkBadge.classList.toggle("fail", !passed);
checkBadge.textContent = passed
? `PASS Δ ${maximumError.toFixed(6)}`
: `FAIL Δ ${maximumError.toFixed(6)}`;
}
return { passed, maximumError };
}
function refreshAll() {
updateEnabledStyles();
rebuildGeometry();
updateCursor();
verifySceneState(true);
}
function setPlaying(value) {
playing = value;
playButton.textContent = playing ? "Pause" : "Play";
playButton.setAttribute("aria-pressed", String(playing));
}
phaseInput.addEventListener("input", () => {
phaseDegrees = Number(phaseInput.value);
phaseRadians = THREE.MathUtils.degToRad(phaseDegrees);
phaseValue.textContent = `${phaseDegrees > 0 ? "+" : ""}${phaseDegrees}°`;
rebuildGeometry();
updateCursor();
verifySceneState(true);
});
cursorInput.addEventListener("input", () => {
setPlaying(false);
cursorMilliseconds = Number(cursorInput.value);
updateCursor();
verifySceneState(true);
});
playButton.addEventListener("click", () => {
setPlaying(!playing);
});
resetButton.addEventListener("click", () => {
setPlaying(false);
phaseDegrees = 0;
phaseRadians = 0;
cursorMilliseconds = 3000;
phaseInput.value = "0";
cursorInput.value = "3000";
phaseValue.textContent = "0°";
for (const component of components) component.enabled = true;
cameraYaw = 0;
cameraPitch = 0.08;
cameraZoom = 1;
updateCamera();
refreshAll();
});
verifyButton.addEventListener("click", () => {
verifySceneState(true);
checkBadge.animate(
[
{ transform: "scale(1)" },
{ transform: "scale(1.08)" },
{ transform: "scale(1)" }
],
{ duration: 260, easing: "ease-out" }
);
});
enableAllButton.addEventListener("click", () => {
for (const component of components) component.enabled = true;
refreshAll();
});
for (const component of components) {
const button = document.querySelector(`[data-id="${component.id}"]`);
button.addEventListener("click", () => {
component.enabled = !component.enabled;
refreshAll();
});
}
let dragging = false;
let previousPointerX = 0;
let previousPointerY = 0;
renderer.domElement.addEventListener("pointerdown", event => {
dragging = true;
previousPointerX = event.clientX;
previousPointerY = event.clientY;
renderer.domElement.setPointerCapture(event.pointerId);
renderer.domElement.style.cursor = "grabbing";
});
renderer.domElement.addEventListener("pointermove", event => {
if (!dragging) return;
const deltaX = event.clientX - previousPointerX;
const deltaY = event.clientY - previousPointerY;
previousPointerX = event.clientX;
previousPointerY = event.clientY;
cameraYaw = THREE.MathUtils.clamp(cameraYaw - deltaX * 0.0045, -0.55, 0.55);
cameraPitch = THREE.MathUtils.clamp(cameraPitch + deltaY * 0.0035, -0.08, 0.36);
updateCamera();
});
function endDrag(event) {
dragging = false;
renderer.domElement.style.cursor = "grab";
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("pointerleave", event => {
if (dragging) endDrag(event);
});
renderer.domElement.addEventListener("wheel", event => {
event.preventDefault();
cameraZoom = THREE.MathUtils.clamp(
cameraZoom * Math.exp(event.deltaY * 0.001),
0.72,
1.35
);
updateCamera();
}, { passive: false });
renderer.domElement.addEventListener("dblclick", () => {
cameraYaw = 0;
cameraPitch = 0.08;
cameraZoom = 1;
updateCamera();
});
renderer.domElement.addEventListener("keydown", event => {
let handled = true;
if (event.key === "ArrowLeft") cameraYaw = THREE.MathUtils.clamp(cameraYaw - 0.08, -0.55, 0.55);
else if (event.key === "ArrowRight") cameraYaw = THREE.MathUtils.clamp(cameraYaw + 0.08, -0.55, 0.55);
else if (event.key === "ArrowUp") cameraPitch = THREE.MathUtils.clamp(cameraPitch + 0.06, -0.08, 0.36);
else if (event.key === "ArrowDown") cameraPitch = THREE.MathUtils.clamp(cameraPitch - 0.06, -0.08, 0.36);
else if (event.key === "+" || event.key === "=") cameraZoom = THREE.MathUtils.clamp(cameraZoom * 0.92, 0.72, 1.35);
else if (event.key === "-") cameraZoom = THREE.MathUtils.clamp(cameraZoom * 1.08, 0.72, 1.35);
else handled = false;
if (handled) {
event.preventDefault();
updateCamera();
}
});
function resize() {
const width = Math.max(1, stage.clientWidth);
const height = Math.max(1, stage.clientHeight);
const aspect = width / height;
renderer.setPixelRatio(Math.min(window.devicePixelRatio || 1, 1.8));
renderer.setSize(width, height, false);
camera.aspect = aspect;
camera.updateProjectionMatrix();
const verticalTangent = Math.tan(THREE.MathUtils.degToRad(camera.fov * 0.5));
const neededHalfWidth = 9.7;
const neededHalfHeight = 5.8;
baseCameraRadius = Math.max(
18.5,
neededHalfHeight / verticalTangent,
neededHalfWidth / (verticalTangent * aspect)
);
updateCamera();
}
const resizeObserver = new ResizeObserver(resize);
resizeObserver.observe(stage);
window.addEventListener("resize", resize);
renderer.domElement.style.cursor = "grab";
resize();
refreshAll();
let previousTime = performance.now();
function animate(now) {
const deltaSeconds = Math.min((now - previousTime) / 1000, 0.05);
previousTime = now;
if (playing) {
cursorMilliseconds = (cursorMilliseconds + deltaSeconds * 1200) % 12000;
cursorInput.value = String(cursorMilliseconds);
updateCursor();
verifySceneState(true);
}
for (const component of components) {
component.marker.rotation.y += 0.012;
component.marker.rotation.x += 0.006;
}
sumMarker.rotation.y -= 0.009;
renderer.render(scene, camera);
requestAnimationFrame(animate);
}
requestAnimationFrame(animate);
</script>
</body>
</html>
```