Prompt specification
Build or customize the Three.js 3D scene "Sliding Bridge Monochord" 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: Sliding Bridge Monochord
Category: Art & Culture · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/sliding-bridge-monochord-x3326
Scene Description Prompt:
Please use Three.js from cdnjs for a cutaway scene based on this idea: sliding bridge monochord. Model a monochord with a movable bridge and a visual standing-wave mode. State the ideal-string assumptions and update frequency ratios when vibrating length changes. Return one self-contained HTML file.
Working Three.js Starter (sliding-bridge-monochord-x3326):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Sliding Bridge Monochord</title>
<style>
:root {
color-scheme: dark;
--bg: #071018;
--panel: #0c1720;
--panel-2: #111f29;
--line: rgba(151, 196, 214, 0.16);
--text: #e6f4f8;
--muted: #90aab5;
--cyan: #58e5f2;
--cyan-soft: #a8f6fb;
--amber: #ffb45b;
--wood: #a8663d;
}
* {
box-sizing: border-box;
}
html,
body {
margin: 0;
width: 100%;
height: 100%;
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,
select {
font: inherit;
}
button,
select {
color: var(--text);
}
#app {
display: grid;
grid-template-columns: minmax(0, 1fr) 370px;
width: 100%;
height: 100dvh;
}
#stage {
position: relative;
min-width: 0;
min-height: 0;
overflow: hidden;
background:
radial-gradient(circle at 48% 35%, rgba(32, 77, 93, 0.3), transparent 45%),
#071018;
}
#scene {
display: block;
width: 100%;
height: 100%;
outline: none;
cursor: grab;
touch-action: none;
}
#scene:active {
cursor: grabbing;
}
.stage-hud {
position: absolute;
top: 20px;
left: 22px;
right: 22px;
display: flex;
align-items: flex-start;
justify-content: space-between;
gap: 16px;
pointer-events: none;
}
.stage-title {
max-width: 440px;
padding: 12px 14px;
border: 1px solid rgba(88, 229, 242, 0.22);
border-radius: 10px;
background: rgba(7, 16, 24, 0.72);
backdrop-filter: blur(12px);
}
.eyebrow {
margin-bottom: 5px;
color: var(--cyan);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.19em;
text-transform: uppercase;
}
.stage-title h1 {
margin: 0;
font-size: clamp(17px, 2vw, 24px);
font-weight: 650;
letter-spacing: -0.025em;
}
.stage-title p {
margin: 6px 0 0;
color: var(--muted);
font-size: 11px;
line-height: 1.45;
}
.view-badge {
flex: 0 0 auto;
padding: 9px 11px;
border: 1px solid rgba(255, 180, 91, 0.38);
border-radius: 999px;
color: #ffd7a7;
background: rgba(52, 32, 15, 0.62);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.12em;
text-transform: uppercase;
}
.stage-footer {
position: absolute;
left: 22px;
right: 22px;
bottom: 18px;
display: flex;
align-items: flex-end;
justify-content: space-between;
gap: 12px;
pointer-events: none;
}
.legend {
display: flex;
flex-wrap: wrap;
gap: 12px;
padding: 9px 12px;
border: 1px solid var(--line);
border-radius: 9px;
background: rgba(7, 16, 24, 0.72);
backdrop-filter: blur(12px);
color: var(--muted);
font-size: 10px;
}
.legend span {
display: inline-flex;
align-items: center;
gap: 6px;
}
.swatch {
width: 16px;
height: 3px;
border-radius: 2px;
background: var(--cyan);
box-shadow: 0 0 9px rgba(88, 229, 242, 0.65);
}
.swatch.amber {
background: var(--amber);
box-shadow: 0 0 8px rgba(255, 180, 91, 0.45);
}
.swatch.gray {
background: #71848e;
box-shadow: none;
}
.camera-help {
padding: 8px 10px;
border-radius: 8px;
color: #9cb2bd;
background: rgba(7, 16, 24, 0.64);
font-size: 10px;
white-space: nowrap;
}
.world-label {
position: absolute;
z-index: 3;
transform: translate(-50%, -50%);
padding: 5px 8px;
border: 1px solid rgba(88, 229, 242, 0.36);
border-radius: 6px;
color: var(--cyan-soft);
background: rgba(7, 16, 24, 0.83);
box-shadow: 0 5px 18px rgba(0, 0, 0, 0.25);
font-size: 10px;
font-weight: 700;
letter-spacing: 0.025em;
pointer-events: none;
white-space: nowrap;
}
.world-label.bridge {
border-color: rgba(255, 180, 91, 0.5);
color: #ffd4a0;
}
.world-label.interior {
border-color: rgba(162, 183, 192, 0.25);
color: #b8c8cf;
font-weight: 500;
}
#panel {
min-height: 0;
overflow-y: auto;
padding: 24px 22px 30px;
border-left: 1px solid var(--line);
background:
linear-gradient(180deg, rgba(17, 31, 41, 0.92), rgba(9, 19, 27, 0.98)),
var(--panel);
box-shadow: -18px 0 50px rgba(0, 0, 0, 0.16);
scrollbar-width: thin;
scrollbar-color: #2e4d5b transparent;
}
.panel-kicker {
color: var(--cyan);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.16em;
text-transform: uppercase;
}
#panel h2 {
margin: 6px 0 8px;
font-size: 25px;
line-height: 1.08;
letter-spacing: -0.035em;
}
.intro {
margin: 0 0 20px;
color: var(--muted);
font-size: 12px;
line-height: 1.6;
}
.card {
margin-top: 14px;
padding: 15px;
border: 1px solid var(--line);
border-radius: 12px;
background: rgba(16, 30, 40, 0.82);
}
.card h3 {
margin: 0 0 12px;
color: #dcecf1;
font-size: 11px;
font-weight: 800;
letter-spacing: 0.11em;
text-transform: uppercase;
}
.control {
margin-bottom: 15px;
}
.control:last-child {
margin-bottom: 0;
}
.control-row {
display: flex;
align-items: center;
justify-content: space-between;
gap: 12px;
margin-bottom: 7px;
font-size: 12px;
}
.control-row output {
color: var(--cyan-soft);
font-variant-numeric: tabular-nums;
font-weight: 700;
}
input[type="range"] {
width: 100%;
height: 4px;
margin: 4px 0;
border-radius: 99px;
appearance: none;
background: #263b46;
cursor: pointer;
}
input[type="range"]::-webkit-slider-thumb {
width: 16px;
height: 16px;
border: 3px solid #d7fbff;
border-radius: 50%;
appearance: none;
background: var(--cyan);
box-shadow: 0 0 0 4px rgba(88, 229, 242, 0.12);
}
input[type="range"]::-moz-range-thumb {
width: 12px;
height: 12px;
border: 3px solid #d7fbff;
border-radius: 50%;
background: var(--cyan);
}
.mode-buttons {
display: grid;
grid-template-columns: repeat(5, 1fr);
gap: 6px;
}
.mode-buttons button,
.action-buttons button,
select {
border: 1px solid var(--line);
border-radius: 8px;
background: #10212b;
cursor: pointer;
transition:
border-color 140ms ease,
background 140ms ease,
color 140ms ease,
transform 140ms ease;
}
.mode-buttons button {
padding: 10px 0;
color: #a9bec7;
font-size: 12px;
font-weight: 750;
}
.mode-buttons button:hover,
.action-buttons button:hover,
select:hover {
border-color: rgba(88, 229, 242, 0.45);
}
.mode-buttons button.active {
border-color: var(--cyan);
color: #061219;
background: var(--cyan);
box-shadow: 0 0 18px rgba(88, 229, 242, 0.2);
}
.action-buttons {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 7px;
margin-top: 10px;
}
.action-buttons button {
padding: 9px 7px;
color: #c5d7de;
font-size: 11px;
}
.action-buttons button.primary {
border-color: rgba(88, 229, 242, 0.5);
color: var(--cyan-soft);
}
select {
width: 100%;
padding: 9px 10px;
outline: none;
font-size: 12px;
}
.readout-grid {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 8px;
}
.readout {
padding: 11px;
border: 1px solid rgba(151, 196, 214, 0.11);
border-radius: 9px;
background: rgba(6, 15, 22, 0.5);
}
.readout .label {
margin-bottom: 5px;
color: var(--muted);
font-size: 9px;
font-weight: 700;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.readout .value {
color: #f1fbfd;
font-size: 19px;
font-variant-numeric: tabular-nums;
font-weight: 680;
}
.readout .unit {
margin-left: 3px;
color: #78929d;
font-size: 10px;
font-weight: 600;
}
.formula {
margin: 0 0 10px;
color: #f3fbfd;
font-family: Georgia, "Times New Roman", serif;
font-size: 20px;
text-align: center;
}
.formula-sub {
margin-top: 3px;
color: var(--muted);
font-family: Inter, ui-sans-serif, system-ui, sans-serif;
font-size: 10px;
text-align: center;
}
.assumptions {
margin: 0;
padding-left: 17px;
color: #9fb3bc;
font-size: 11px;
line-height: 1.55;
}
.assumptions li + li {
margin-top: 5px;
}
.ratio-callout {
margin-top: 12px;
padding: 11px 12px;
border-left: 3px solid var(--amber);
border-radius: 4px;
color: #d7c4aa;
background: rgba(95, 56, 20, 0.18);
font-size: 11px;
line-height: 1.55;
}
.ratio-callout strong {
color: #ffd6a3;
}
table {
width: 100%;
border-collapse: collapse;
font-size: 11px;
font-variant-numeric: tabular-nums;
}
th,
td {
padding: 7px 5px;
border-bottom: 1px solid rgba(151, 196, 214, 0.09);
text-align: right;
}
th:first-child,
td:first-child {
text-align: left;
}
th {
color: #79939e;
font-size: 9px;
font-weight: 750;
letter-spacing: 0.06em;
text-transform: uppercase;
}
td {
color: #b7c9d0;
}
tr.selected td {
color: var(--cyan-soft);
background: rgba(88, 229, 242, 0.07);
}
.fine-print {
margin: 9px 0 0;
color: #708a95;
font-size: 10px;
line-height: 1.5;
}
.ratio-box {
display: grid;
grid-template-columns: 1fr auto;
align-items: center;
gap: 12px;
padding: 12px;
border: 1px solid rgba(255, 180, 91, 0.18);
border-radius: 9px;
background: rgba(76, 45, 18, 0.13);
}
.ratio-box .caption {
color: #a9957e;
font-size: 10px;
line-height: 1.4;
}
.ratio-box .ratio {
color: #ffd3a0;
font-size: 22px;
font-variant-numeric: tabular-nums;
font-weight: 750;
}
@media (max-width: 860px) {
html,
body {
height: auto;
overflow: auto;
}
#app {
display: block;
height: auto;
}
#stage {
height: 62dvh;
min-height: 430px;
}
#panel {
min-height: 0;
overflow: visible;
border-top: 1px solid var(--line);
border-left: 0;
}
.camera-help {
display: none;
}
}
@media (max-width: 520px) {
.stage-hud {
top: 12px;
left: 12px;
right: 12px;
}
.stage-title {
padding: 9px 10px;
}
.stage-title p,
.view-badge {
display: none;
}
.stage-footer {
left: 12px;
right: 12px;
bottom: 12px;
}
.legend {
gap: 8px;
}
#panel {
padding: 22px 16px 28px;
}
}
</style>
</head>
<body>
<div id="app">
<main id="stage" aria-label="Interactive three-dimensional sliding bridge monochord">
<canvas id="scene" tabindex="0" aria-label="Drag to orbit, scroll or pinch to zoom"></canvas>
<div class="stage-hud">
<div class="stage-title">
<div class="eyebrow">Interactive acoustics laboratory</div>
<h1>Sliding Bridge Monochord</h1>
<p>
Move the bridge to change the sounding length. Choose a harmonic to
reveal its nodes and antinodes.
</p>
</div>
<div class="view-badge">Open-front cutaway</div>
</div>
<div id="label-fixed" class="world-label">Fixed end / nut</div>
<div id="label-bridge" class="world-label bridge">Movable bridge</div>
<div id="label-length" class="world-label">L = 120 cm</div>
<div id="label-interior" class="world-label interior">Internal braces and resonators</div>
<div id="label-tail" class="world-label interior">Inactive tail</div>
<div class="stage-footer">
<div class="legend">
<span><i class="swatch"></i> Vibrating string</span>
<span><i class="swatch amber"></i> Bridge / antinodes</span>
<span><i class="swatch gray"></i> Damped tail / equilibrium</span>
</div>
<div class="camera-help">Drag: orbit · Wheel/pinch: zoom · Double-click: reset</div>
</div>
</main>
<aside id="panel">
<div class="panel-kicker">Wave mechanics · mode demonstration</div>
<h2>One string.<br />Many standing waves.</h2>
<p class="intro">
The bridge creates a fixed node, so the distance from the nut to the
bridge is the vibrating length. The remainder of the string is shown as
an inactive, damped tail.
</p>
<section class="card">
<h3>String parameters</h3>
<div class="control">
<div class="control-row">
<label for="length">Vibrating length</label>
<output id="lengthOut" for="length">120 cm</output>
</div>
<input id="length" type="range" min="60" max="160" step="1" value="120" />
</div>
<div class="control">
<div class="control-row">
<label for="tension">String tension</label>
<output id="tensionOut" for="tension">72 N</output>
</div>
<input id="tension" type="range" min="20" max="120" step="1" value="72" />
</div>
<div class="control">
<div class="control-row">
<label for="amplitude">Visual amplitude</label>
<output id="amplitudeOut" for="amplitude">0.28</output>
</div>
<input id="amplitude" type="range" min="0.12" max="0.42" step="0.01" value="0.28" />
</div>
</section>
<section class="card">
<h3>Normal mode</h3>
<div id="modeButtons" class="mode-buttons" role="group" aria-label="Normal mode">
<button type="button" data-mode="1" class="active">1</button>
<button type="button" data-mode="2">2</button>
<button type="button" data-mode="3">3</button>
<button type="button" data-mode="4">4</button>
<button type="button" data-mode="5">5</button>
</div>
<div class="action-buttons">
<button id="playPause" type="button">Pause motion</button>
<button id="resetCamera" type="button">Reset camera</button>
<button id="audition" class="primary" type="button">Audition selected mode</button>
</div>
</section>
<section class="card">
<h3>Measured result</h3>
<div class="readout-grid">
<div class="readout">
<div class="label">Selected frequency</div>
<div class="value"><span id="frequencyValue">102.1</span><span class="unit">Hz</span></div>
</div>
<div class="readout">
<div class="label">Wave speed</div>
<div class="value"><span id="speedValue">244.9</span><span class="unit">m/s</span></div>
</div>
<div class="readout">
<div class="label">Mode ratio fₙ:f₁</div>
<div class="value" id="modeRatioValue">1 : 1</div>
</div>
<div class="readout">
<div class="label">Fundamental f₁</div>
<div class="value"><span id="fundamentalValue">102.1</span><span class="unit">Hz</span></div>
</div>
</div>
</section>
<section class="card">
<h3>Frequency law</h3>
<div class="formula">
fₙ = <span class="fraction">n v / 2L</span>
</div>
<div class="formula-sub">v = √(T/μ), where μ = 0.0012 kg/m</div>
<div class="ratio-box">
<div class="caption">
Current mode frequency relative to the same mode at the reference length
</div>
<div class="ratio" id="lengthRatioValue">× 1.000</div>
</div>
<div class="control" style="margin-top: 13px">
<div class="control-row">
<label for="referenceLength">Reference length</label>
</div>
<select id="referenceLength">
<option value="60">60 cm</option>
<option value="90">90 cm</option>
<option value="120" selected>120 cm</option>
<option value="150">150 cm</option>
</select>
</div>
<div class="ratio-callout">
<strong>Changing only the vibrating length:</strong><br />
If the bridge moves from Lₐ to Lᵦ while tension and linear density
remain constant, then<br />
<strong>fₙ(Lᵦ) / fₙ(Lₐ) = Lₐ / Lᵦ.</strong><br />
Every harmonic scales by the same inverse-length factor.
</div>
</section>
<section class="card">
<h3>Harmonics at the current length</h3>
<table>
<thead>
<tr>
<th>Mode</th>
<th>Frequency</th>
<th>fₙ / f₁</th>
</tr>
</thead>
<tbody id="modeTable"></tbody>
</table>
<p class="fine-print">
At a fixed length, f₁:f₂:f₃:f₄:f₅ = 1:2:3:4:5. Changing L changes the
absolute frequencies, but not these integer mode ratios.
</p>
</section>
<section class="card">
<h3>Ideal-string assumptions</h3>
<ul class="assumptions">
<li>The string is perfectly flexible, uniform, and inextensible.</li>
<li>Mass per unit length μ is constant at 0.0012 kg/m.</li>
<li>Tension T is uniform along the string and is held constant during a length change.</li>
<li>Bending stiffness, string damping, and end losses are neglected.</li>
<li>The nut and movable bridge are fixed nodes; the vibrating interval is L.</li>
<li>Small transverse displacement gives the linear wave equation.</li>
</ul>
<p class="fine-print">
The standing-wave animation is slowed by approximately 200× so that
acoustic frequencies remain visible.
</p>
</section>
</aside>
</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 canvas = document.getElementById("scene");
const lengthInput = document.getElementById("length");
const tensionInput = document.getElementById("tension");
const amplitudeInput = document.getElementById("amplitude");
const referenceInput = document.getElementById("referenceLength");
const playPauseButton = document.getElementById("playPause");
const state = {
lengthCm: 120,
tensionN: 72,
linearDensity: 0.0012,
mode: 1,
amplitude: 0.28,
referenceCm: 120,
playing: !window.matchMedia("(prefers-reduced-motion: reduce)").matches
};
const renderer = new THREE.WebGLRenderer({
canvas,
antialias: true,
alpha: true,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio || 1, 2));
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.15;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x071018);
scene.fog = new THREE.Fog(0x071018, 15, 29);
const camera = new THREE.PerspectiveCamera(40, 1, 0.1, 100);
// Camera controls are implemented directly here; no external controls are used.
const cameraTarget = new THREE.Vector3(0, 0.95, 0);
const desiredCamera = {
yaw: 0.58,
pitch: 0.43,
radius: 12.6,
target: cameraTarget.clone()
};
const currentCamera = {
yaw: desiredCamera.yaw,
pitch: desiredCamera.pitch,
radius: desiredCamera.radius,
target: desiredCamera.target.clone()
};
function defaultRadius() {
return stage.clientWidth / stage.clientHeight < 1.08 ? 15.2 : 12.6;
}
function resetCamera() {
desiredCamera.yaw = 0.58;
desiredCamera.pitch = 0.43;
desiredCamera.radius = defaultRadius();
desiredCamera.target.set(0, 0.95, 0);
}
const hemisphere = new THREE.HemisphereLight(0xbfefff, 0x25180f, 1.7);
scene.add(hemisphere);
const keyLight = new THREE.DirectionalLight(0xffe2c1, 3.3);
keyLight.position.set(4.5, 9, 6.5);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(1536, 1536);
keyLight.shadow.camera.left = -8;
keyLight.shadow.camera.right = 8;
keyLight.shadow.camera.top = 7;
keyLight.shadow.camera.bottom = -7;
keyLight.shadow.camera.near = 1;
keyLight.shadow.camera.far = 25;
keyLight.shadow.bias = -0.00035;
scene.add(keyLight);
const rimLight = new THREE.DirectionalLight(0x5be8ff, 2.2);
rimLight.position.set(-6, 4, -5);
scene.add(rimLight);
const warmFill = new THREE.PointLight(0xff9f45, 18, 9, 2);
warmFill.position.set(4, 2.5, 2.5);
scene.add(warmFill);
function makeWoodTexture(dark = false) {
const textureCanvas = document.createElement("canvas");
textureCanvas.width = 512;
textureCanvas.height = 128;
const ctx = textureCanvas.getContext("2d");
ctx.fillStyle = dark ? "#55301f" : "#9b5c35";
ctx.fillRect(0, 0, 512, 128);
for (let i = 0; i < 100; i += 1) {
const y = Math.random() * 128;
const alpha = dark ? 0.08 + Math.random() * 0.13 : 0.06 + Math.random() * 0.16;
ctx.strokeStyle = `rgba(35, 16, 8, ${alpha})`;
ctx.lineWidth = 0.4 + Math.random() * 1.4;
ctx.beginPath();
ctx.moveTo(0, y);
for (let x = 0; x <= 512; x += 24) {
ctx.lineTo(x, y + Math.sin(x * 0.025 + i) * (1.5 + Math.random() * 3));
}
ctx.stroke();
}
const texture = new THREE.CanvasTexture(textureCanvas);
texture.colorSpace = THREE.SRGBColorSpace;
texture.wrapS = THREE.RepeatWrapping;
texture.wrapT = THREE.RepeatWrapping;
texture.repeat.set(2.4, 1);
return texture;
}
const materials = {
wood: new THREE.MeshStandardMaterial({
map: makeWoodTexture(false),
color: 0xffffff,
roughness: 0.62,
metalness: 0.02
}),
darkWood: new THREE.MeshStandardMaterial({
map: makeWoodTexture(true),
color: 0xffffff,
roughness: 0.72,
metalness: 0.02
}),
interior: new THREE.MeshStandardMaterial({
color: 0x2b2019,
roughness: 0.9
}),
cutEdge: new THREE.MeshStandardMaterial({
color: 0xd39259,
roughness: 0.58
}),
brass: new THREE.MeshStandardMaterial({
color: 0xc59a48,
roughness: 0.3,
metalness: 0.78
}),
darkMetal: new THREE.MeshStandardMaterial({
color: 0x26343a,
roughness: 0.38,
metalness: 0.65
}),
orange: new THREE.MeshStandardMaterial({
color: 0xffa43f,
emissive: 0x7a2c00,
emissiveIntensity: 0.4,
roughness: 0.34,
metalness: 0.25
}),
node: new THREE.MeshStandardMaterial({
color: 0xb9fbff,
emissive: 0x22c7d7,
emissiveIntensity: 1.7,
roughness: 0.28
}),
antinode: new THREE.MeshStandardMaterial({
color: 0xffca82,
emissive: 0xff7b13,
emissiveIntensity: 1.5,
roughness: 0.3
})
};
function addBox(size, position, material, castShadow = true) {
const mesh = new THREE.Mesh(
new THREE.BoxGeometry(size[0], size[1], size[2]),
material
);
mesh.position.set(position[0], position[1], position[2]);
mesh.castShadow = castShadow;
mesh.receiveShadow = true;
scene.add(mesh);
return mesh;
}
const ground = new THREE.Mesh(
new THREE.PlaneGeometry(40, 40),
new THREE.MeshStandardMaterial({
color: 0x09151c,
roughness: 0.96,
metalness: 0.02
})
);
ground.rotation.x = -Math.PI / 2;
ground.position.y = -0.045;
ground.receiveShadow = true;
scene.add(ground);
const grid = new THREE.GridHelper(30, 30, 0x2b5866, 0x18323b);
grid.position.y = -0.035;
grid.material.transparent = true;
grid.material.opacity = 0.24;
scene.add(grid);
// Open-front wooden soundbox: the near wall and top are sectioned away.
addBox([9.55, 0.28, 2.7], [0, 0.12, 0], materials.darkWood);
addBox([9.12, 0.08, 2.3], [0, 0.30, 0], materials.interior);
addBox([9.55, 1.12, 0.18], [0, 0.82, -1.27], materials.wood);
addBox([9.55, 0.34, 0.18], [0, 0.45, 1.27], materials.wood);
addBox([0.18, 1.12, 2.7], [-4.69, 0.82, 0], materials.wood);
addBox([0.18, 1.12, 2.7], [4.69, 0.82, 0], materials.wood);
// Bright exposed cut faces.
addBox([9.56, 0.055, 0.19], [0, 0.635, 1.27], materials.cutEdge);
addBox([0.19, 0.055, 2.7], [4.69, 1.39, 0], materials.cutEdge);
addBox([9.62, 0.12, 0.24], [0, 1.41, -1.27], materials.darkWood);
addBox([9.62, 0.12, 0.24], [0, 1.41, -1.27], materials.darkWood);
// Interior ribs, spine, resonator chambers, and sound post.
for (const x of [-3.45, -0.1, 3.42]) {
addBox([0.16, 0.76, 2.14], [x, 0.68, 0], materials.wood);
}
addBox([8.6, 0.16, 0.22], [0, 0.56, -0.66], materials.darkWood);
const resonatorGeometry = new THREE.CylinderGeometry(0.38, 0.48, 1.75, 24, 1, false);
for (const x of [-2.28, 1.88]) {
const resonator = new THREE.Mesh(resonatorGeometry, materials.darkWood);
resonator.rotation.z = Math.PI / 2;
resonator.position.set(x, 0.69, 0.08);
resonator.castShadow = true;
resonator.receiveShadow = true;
scene.add(resonator);
}
const soundPost = new THREE.Mesh(
new THREE.CylinderGeometry(0.11, 0.13, 0.86, 20),
materials.brass
);
soundPost.position.set(0.05, 0.77, 0.05);
soundPost.castShadow = true;
scene.add(soundPost);
// Sliding track.
addBox([8.75, 0.055, 1.75], [0, 0.39, 0], materials.darkMetal);
addBox([8.55, 0.025, 0.08], [0, 0.43, 0.61], materials.brass, false);
const FIXED_X = -4.36;
const TAIL_X = 4.36;
const STRING_Y = 1.83;
const CENTIMETERS_PER_SCENE_UNIT = 20;
function makeSupport(x) {
const group = new THREE.Group();
const post = new THREE.Mesh(
new THREE.BoxGeometry(0.22, 1.24, 1.38),
materials.darkMetal
);
post.position.y = 1.03;
post.castShadow = true;
post.receiveShadow = true;
group.add(post);
const foot = new THREE.Mesh(
new THREE.BoxGeometry(0.65, 0.12, 1.75),
materials.brass
);
foot.position.y = 0.45;
foot.castShadow = true;
group.add(foot);
const saddle = new THREE.Mesh(
new THREE.CylinderGeometry(0.1, 0.1, 1.82, 24),
materials.brass
);
saddle.rotation.x = Math.PI / 2;
saddle.position.y = 1.73;
saddle.castShadow = true;
group.add(saddle);
group.position.x = x;
scene.add(group);
return group;
}
makeSupport(FIXED_X);
makeSupport(TAIL_X);
// Movable bridge with a tapered wooden body and a brass saddle.
const bridge = new THREE.Group();
const bridgeShape = new THREE.Shape();
bridgeShape.moveTo(-0.4, 0);
bridgeShape.lineTo(0.4, 0);
bridgeShape.lineTo(0.17, 1.1);
bridgeShape.lineTo(-0.17, 1.1);
bridgeShape.closePath();
const bridgeBody = new THREE.Mesh(
new THREE.ExtrudeGeometry(bridgeShape, {
depth: 1.45,
bevelEnabled: true,
bevelSize: 0.025,
bevelThickness: 0.025,
bevelSegments: 2,
steps: 1
}),
materials.darkWood
);
bridgeBody.position.z = -0.725;
bridgeBody.castShadow = true;
bridgeBody.receiveShadow = true;
bridge.add(bridgeBody);
const bridgeSaddle = new THREE.Mesh(
new THREE.CylinderGeometry(0.09, 0.09, 1.78, 24),
materials.brass
);
bridgeSaddle.rotation.x = Math.PI / 2;
bridgeSaddle.position.y = 1.73;
bridgeSaddle.castShadow = true;
bridge.add(bridgeSaddle);
const bridgeMarker = new THREE.Mesh(
new THREE.SphereGeometry(0.105, 18, 12),
materials.orange
);
bridgeMarker.position.set(0, 0.9, 0.79);
bridgeMarker.castShadow = true;
bridge.add(bridgeMarker);
bridge.position.y = 0.44;
scene.add(bridge);
// Dimension line for the sounding length.
const dimensionGeometry = new THREE.BufferGeometry();
const dimensionPositions = new Float32Array(18);
dimensionGeometry.setAttribute(
"position",
new THREE.BufferAttribute(dimensionPositions, 3)
);
const dimensionLine = new THREE.LineSegments(
dimensionGeometry,
new THREE.LineBasicMaterial({
color: 0x58e5f2,
transparent: true,
opacity: 0.72
})
);
scene.add(dimensionLine);
// Dashed equilibrium string.
const equilibriumGeometry = new THREE.BufferGeometry();
equilibriumGeometry.setAttribute(
"position",
new THREE.BufferAttribute(new Float32Array(6), 3)
);
const equilibriumLine = new THREE.Line(
equilibriumGeometry,
new THREE.LineDashedMaterial({
color: 0x91aab4,
dashSize: 0.13,
gapSize: 0.1,
transparent: true,
opacity: 0.5
})
);
scene.add(equilibriumLine);
// Static, damped continuation beyond the movable bridge.
const tailGeometry = new THREE.BufferGeometry();
tailGeometry.setAttribute(
"position",
new THREE.BufferAttribute(new Float32Array(6), 3)
);
const tailLine = new THREE.Line(
tailGeometry,
new THREE.LineBasicMaterial({
color: 0x71848e,
transparent: true,
opacity: 0.72
})
);
scene.add(tailLine);
/*
A small custom shader expands a line into a camera-facing ribbon.
This keeps the vibrating string visible without importing Line2/addons.
*/
const WAVE_SEGMENTS = 180;
const waveGeometry = new THREE.BufferGeometry();
const wavePositions = new Float32Array((WAVE_SEGMENTS + 1) * 3);
const waveSides = new Float32Array(WAVE_SEGMENTS + 1);
const waveParameters = new Float32Array(WAVE_SEGMENTS + 1);
for (let i = 0; i <= WAVE_SEGMENTS; i += 1) {
waveSides[i] = i % 2 === 0 ? -1 : 1;
waveParameters[i] = i / WAVE_SEGMENTS;
}
waveGeometry.setAttribute(
"position",
new THREE.BufferAttribute(wavePositions, 3).setUsage(THREE.DynamicDrawUsage)
);
waveGeometry.setAttribute("aSide", new THREE.BufferAttribute(waveSides, 1));
waveGeometry.setAttribute("aU", new THREE.BufferAttribute(waveParameters, 1));
function createWaveMaterial(width, opacity) {
return new THREE.ShaderMaterial({
uniforms: {
uResolution: { value: new THREE.Vector2(1, 1) },
uWidth: { value: width }
},
vertexShader: `
attribute float aSide;
attribute float aU;
uniform vec2 uResolution;
uniform float uWidth;
varying float vSide;
varying float vU;
void main() {
vSide = aSide;
vU = aU;
vec4 currentClip = projectionMatrix * modelViewMatrix * vec4(position, 1.0);
vec4 nextClip = projectionMatrix * modelViewMatrix *
vec4(position + vec3(0.025, 0.0, 0.0), 1.0);
vec2 currentNdc = currentClip.xy / currentClip.w;
vec2 nextNdc = nextClip.xy / nextClip.w;
vec2 direction = normalize((nextNdc - currentNdc) * uResolution);
vec2 normal = vec2(-direction.y, direction.x);
float pixelsToNdc = 2.0 / uResolution.y;
currentClip.xy += normal * aSide * uWidth * pixelsToNdc * currentClip.w;
gl_Position = currentClip;
}
`,
fragmentShader: `
varying float vSide;
varying float vU;
uniform float uWidth;
void main() {
float edge = 1.0 - smoothstep(0.68, 1.0, abs(vSide));
vec3 outer = vec3(0.10, 0.78, 0.92);
vec3 core = vec3(0.79, 1.0, 1.0);
float mixAmount = pow(1.0 - abs(vSide), 5.0);
vec3 color = mix(outer, core, mixAmount);
gl_FragColor = vec4(color, edge * 0.94);
}
`,
transparent: true,
depthWrite: false,
side: THREE.DoubleSide
});
}
const waveGlowMaterial = createWaveMaterial(7.0, 0.25);
const waveCoreMaterial = createWaveMaterial(2.2, 0.98);
const waveGlow = new THREE.Mesh(waveGeometry, waveGlowMaterial);
const waveCore = new THREE.Mesh(waveGeometry, waveCoreMaterial);
waveGlow.frustumCulled = false;
waveCore.frustumCulled = false;
waveGlow.renderOrder = 4;
waveCore.renderOrder = 5;
scene.add(waveGlow, waveCore);
// Node and antinode markers.
const nodeMarkers = [];
const antinodeMarkers = [];
for (let i = 0; i < 6; i += 1) {
const node = new THREE.Mesh(
new THREE.SphereGeometry(0.075, 18, 12),
materials.node
);
node.castShadow = true;
scene.add(node);
nodeMarkers.push(node);
}
for (let i = 0; i < 3; i += 1) {
const antinode = new THREE.Mesh(
new THREE.SphereGeometry(0.09, 18, 12),
materials.antinode
);
antinode.castShadow = true;
scene.add(antinode);
antinodeMarkers.push(antinode);
}
let bridgeX = FIXED_X + state.lengthCm / CENTIMETERS_PER_SCENE_UNIT;
let phase = 0;
function physicsValues() {
const lengthM = state.lengthCm / 100;
const speed = Math.sqrt(state.tensionN / state.linearDensity);
const fundamental = speed / (2 * lengthM);
return {
lengthM,
speed,
fundamental,
selected: fundamental * state.mode
};
}
function updateStaticStringGeometry() {
bridgeX = FIXED_X + state.lengthCm / CENTIMETERS_PER_SCENE_UNIT;
bridge.position.x = bridgeX;
const tailPositions = tailGeometry.attributes.position.array;
tailPositions[0] = bridgeX;
tailPositions[1] = STRING_Y;
tailPositions[2] = 0;
tailPositions[3] = TAIL_X;
tailPositions[4] = STRING_Y;
tailPositions[5] = 0;
tailGeometry.attributes.position.needsUpdate = true;
const equilibriumPositions = equilibriumGeometry.attributes.position.array;
equilibriumPositions[0] = FIXED_X;
equilibriumPositions[1] = STRING_Y;
equilibriumPositions[2] = 0;
equilibriumPositions[3] = bridgeX;
equilibriumPositions[4] = STRING_Y;
equilibriumPositions[5] = 0;
equilibriumGeometry.attributes.position.needsUpdate = true;
equilibriumLine.computeLineDistances();
const dimension = dimensionPositions;
const y = 2.27;
const z = 1.08;
dimension.set([
FIXED_X, y, z,
bridgeX, y, z,
FIXED_X, y - 0.12, z,
FIXED_X, y + 0.12, z,
bridgeX, y - 0.12, z,
bridgeX, y + 0.12, z
]);
dimensionGeometry.attributes.position.needsUpdate = true;
document.getElementById("label-length").textContent =
`L = ${state.lengthCm.toFixed(0)} cm`;
}
function updateWave() {
const position = waveGeometry.attributes.position.array;
const mode = state.mode;
const visualAmplitude = state.amplitude / Math.sqrt(mode);
const temporalFactor = Math.cos(phase);
for (let i = 0; i <= WAVE_SEGMENTS; i += 1) {
const u = i / WAVE_SEGMENTS;
const x = FIXED_X + (bridgeX - FIXED_X) * u;
const envelope = Math.sin(mode * Math.PI * u);
const y = STRING_Y + visualAmplitude * envelope * temporalFactor;
const index = i * 3;
position[index] = x;
position[index + 1] = y;
position[index + 2] = 0;
}
waveGeometry.attributes.position.needsUpdate = true;
for (let i = 0; i < nodeMarkers.length; i += 1) {
const marker = nodeMarkers[i];
marker.visible = i <= mode;
if (marker.visible) {
const u = i / mode;
marker.position.set(
FIXED_X + (bridgeX - FIXED_X) * u,
STRING_Y,
0
);
}
}
let antinodeIndex = 0;
for (let k = 0; k < mode; k += 1) {
const u = (2 * k + 1) / (2 * mode);
const marker = antinodeMarkers[antinodeIndex];
marker.visible = true;
const envelope = Math.sin(mode * Math.PI * u);
marker.position.set(
FIXED_X + (bridgeX - FIXED_X) * u,
STRING_Y + visualAmplitude * envelope * temporalFactor,
0
);
antinodeIndex += 1;
}
for (let i = antinodeIndex; i < antinodeMarkers.length; i += 1) {
antinodeMarkers[i].visible = false;
}
}
function updateReadouts() {
const { speed, fundamental, selected } = physicsValues();
const referenceRatio = state.referenceCm / state.lengthCm;
document.getElementById("lengthOut").textContent =
`${state.lengthCm.toFixed(0)} cm`;
document.getElementById("tensionOut").textContent =
`${state.tensionN.toFixed(0)} N`;
document.getElementById("amplitudeOut").textContent =
state.amplitude.toFixed(2);
document.getElementById("frequencyValue").textContent =
selected.toFixed(1);
document.getElementById("fundamentalValue").textContent =
fundamental.toFixed(1);
document.getElementById("speedValue").textContent =
speed.toFixed(1);
document.getElementById("modeRatioValue").textContent =
`${state.mode} : 1`;
document.getElementById("lengthRatioValue").textContent =
`× ${referenceRatio.toFixed(3)}`;
const table = document.getElementById("modeTable");
table.innerHTML = "";
for (let mode = 1; mode <= 5; mode += 1) {
const frequency = fundamental * mode;
const row = document.createElement("tr");
if (mode === state.mode) {
row.className = "selected";
}
row.innerHTML = `
<td>${mode}</td>
<td>${frequency.toFixed(1)} Hz</td>
<td>${mode}:1</td>
`;
table.appendChild(row);
}
}
function updatePlayButton() {
playPauseButton.textContent = state.playing ? "Pause motion" : "Play motion";
playPauseButton.setAttribute("aria-pressed", String(!state.playing));
}
lengthInput.addEventListener("input", () => {
state.lengthCm = Number(lengthInput.value);
updateStaticStringGeometry();
updateReadouts();
});
tensionInput.addEventListener("input", () => {
state.tensionN = Number(tensionInput.value);
updateReadouts();
});
amplitudeInput.addEventListener("input", () => {
state.amplitude = Number(amplitudeInput.value);
updateReadouts();
});
referenceInput.addEventListener("change", () => {
state.referenceCm = Number(referenceInput.value);
updateReadouts();
});
const modeButtons = [
...document.querySelectorAll("#modeButtons button")
];
modeButtons.forEach((button) => {
button.addEventListener("click", () => {
state.mode = Number(button.dataset.mode);
modeButtons.forEach((item) => {
item.classList.toggle("active", item === button);
});
updateReadouts();
});
});
playPauseButton.addEventListener("click", () => {
state.playing = !state.playing;
updatePlayButton();
});
document.getElementById("resetCamera").addEventListener("click", resetCamera);
let audioContext = null;
document.getElementById("audition").addEventListener("click", async () => {
try {
const AudioContextClass =
window.AudioContext || window.webkitAudioContext;
if (!AudioContextClass) {
return;
}
if (!audioContext) {
audioContext = new AudioContextClass();
}
await audioContext.resume();
const { selected } = physicsValues();
const oscillator = audioContext.createOscillator();
const gain = audioContext.createGain();
oscillator.type = "sine";
oscillator.frequency.setValueAtTime(selected, audioContext.currentTime);
gain.gain.setValueAtTime(0.0001, audioContext.currentTime);
gain.gain.exponentialRampToValueAtTime(
0.16,
audioContext.currentTime + 0.025
);
gain.gain.exponentialRampToValueAtTime(
0.0001,
audioContext.currentTime + 1.25
);
oscillator.connect(gain);
gain.connect(audioContext.destination);
oscillator.start();
oscillator.stop(audioContext.currentTime + 1.3);
} catch (error) {
console.warn("Audio preview was not available:", error);
}
});
// Direct pointer orbit and pinch-to-zoom controls.
const pointers = new Map();
let lastPointerX = 0;
let lastPointerY = 0;
let lastPinchDistance = 0;
canvas.addEventListener("pointerdown", (event) => {
canvas.setPointerCapture(event.pointerId);
pointers.set(event.pointerId, { x: event.clientX, y: event.clientY });
if (pointers.size === 1) {
lastPointerX = event.clientX;
lastPointerY = event.clientY;
} else if (pointers.size === 2) {
const points = [...pointers.values()];
lastPinchDistance = Math.hypot(
points[0].x - points[1].x,
points[0].y - points[1].y
);
}
});
canvas.addEventListener("pointermove", (event) => {
if (!pointers.has(event.pointerId)) {
return;
}
pointers.set(event.pointerId, { x: event.clientX, y: event.clientY });
if (pointers.size === 1) {
const dx = event.clientX - lastPointerX;
const dy = event.clientY - lastPointerY;
desiredCamera.yaw -= dx * 0.005;
desiredCamera.pitch = THREE.MathUtils.clamp(
desiredCamera.pitch + dy * 0.0045,
0.12,
1.15
);
lastPointerX = event.clientX;
lastPointerY = event.clientY;
} else if (pointers.size === 2) {
const points = [...pointers.values()];
const distance = Math.hypot(
points[0].x - points[1].x,
points[0].y - points[1].y
);
if (lastPinchDistance > 0 && distance > 0) {
desiredCamera.radius = THREE.MathUtils.clamp(
desiredCamera.radius * (lastPinchDistance / distance),
6.5,
22
);
}
lastPinchDistance = distance;
}
});
function releasePointer(event) {
pointers.delete(event.pointerId);
if (pointers.size < 2) {
lastPinchDistance = 0;
}
if (pointers.size === 1) {
const remaining = [...pointers.values()][0];
lastPointerX = remaining.x;
lastPointerY = remaining.y;
}
}
canvas.addEventListener("pointerup", releasePointer);
canvas.addEventListener("pointercancel", releasePointer);
canvas.addEventListener(
"wheel",
(event) => {
event.preventDefault();
desiredCamera.radius = THREE.MathUtils.clamp(
desiredCamera.radius * Math.exp(event.deltaY * 0.001),
6.5,
22
);
},
{ passive: false }
);
canvas.addEventListener("dblclick", resetCamera);
canvas.addEventListener("keydown", (event) => {
if (event.code === "Space") {
event.preventDefault();
state.playing = !state.playing;
updatePlayButton();
} else if (event.code === "ArrowLeft") {
desiredCamera.yaw += 0.1;
} else if (event.code === "ArrowRight") {
desiredCamera.yaw -= 0.1;
} else if (event.code === "ArrowUp") {
desiredCamera.pitch = THREE.MathUtils.clamp(
desiredCamera.pitch + 0.07,
0.12,
1.15
);
} else if (event.code === "ArrowDown") {
desiredCamera.pitch = THREE.MathUtils.clamp(
desiredCamera.pitch - 0.07,
0.12,
1.15
);
} else if (event.key === "+" || event.key === "=") {
desiredCamera.radius = Math.max(6.5, desiredCamera.radius - 0.6);
} else if (event.key === "-") {
desiredCamera.radius = Math.min(22, desiredCamera.radius + 0.6);
}
});
function resize() {
const width = Math.max(1, stage.clientWidth);
const height = Math.max(1, stage.clientHeight);
renderer.setSize(width, height, false);
camera.aspect = width / height;
camera.updateProjectionMatrix();
waveCoreMaterial.uniforms.uResolution.value.set(width, height);
waveGlowMaterial.uniforms.uResolution.value.set(width, height);
}
const resizeObserver = new ResizeObserver(resize);
resizeObserver.observe(stage);
resize();
const labels = {
fixed: document.getElementById("label-fixed"),
bridge: document.getElementById("label-bridge"),
length: document.getElementById("label-length"),
interior: document.getElementById("label-interior"),
tail: document.getElementById("label-tail")
};
const projected = new THREE.Vector3();
function placeLabel(element, x, y, z) {
projected.set(x, y, z).project(camera);
const visible = projected.z > -1 && projected.z < 1;
element.style.display = visible ? "block" : "none";
if (!visible) {
return;
}
const screenX = (projected.x * 0.5 + 0.5) * stage.clientWidth;
const screenY = (-projected.y * 0.5 + 0.5) * stage.clientHeight;
element.style.left = `${screenX}px`;
element.style.top = `${screenY}px`;
}
function updateLabels() {
placeLabel(labels.fixed, FIXED_X, 2.12, 0);
placeLabel(labels.bridge, bridgeX, 2.12, 0);
placeLabel(
labels.length,
FIXED_X + (bridgeX - FIXED_X) * 0.5,
2.48,
1.08
);
placeLabel(labels.interior, 1.05, 0.79, 0.08);
placeLabel(labels.tail, 3.98, 2.02, 0);
}
const clock = new THREE.Clock();
function animate() {
requestAnimationFrame(animate);
const delta = Math.min(clock.getDelta(), 0.05);
const smoothing = 1 - Math.exp(-delta * 9);
currentCamera.yaw = THREE.MathUtils.lerp(
currentCamera.yaw,
desiredCamera.yaw,
smoothing
);
currentCamera.pitch = THREE.MathUtils.lerp(
currentCamera.pitch,
desiredCamera.pitch,
smoothing
);
currentCamera.radius = THREE.MathUtils.lerp(
currentCamera.radius,
desiredCamera.radius,
smoothing
);
currentCamera.target.lerp(desiredCamera.target, smoothing);
const horizontalRadius =
currentCamera.radius * Math.cos(currentCamera.pitch);
camera.position.set(
currentCamera.target.x +
horizontalRadius * Math.sin(currentCamera.yaw),
currentCamera.target.y +
currentCamera.radius * Math.sin(currentCamera.pitch),
currentCamera.target.z +
horizontalRadius * Math.cos(currentCamera.yaw)
);
camera.lookAt(currentCamera.target);
camera.updateMatrixWorld();
if (state.playing) {
const { selected } = physicsValues();
const visualFrequency = selected / 200;
phase =
(phase + delta * Math.PI * 2 * visualFrequency) %
(Math.PI * 2);
}
updateWave();
updateLabels();
renderer.render(scene, camera);
}
updateStaticStringGeometry();
updateReadouts();
updatePlayButton();
animate();
</script>
</body>
</html>
```