Prompt specification
Build or customize the Three.js 3D scene "Tuning-fork pair comparison" 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: Tuning-fork pair comparison
Category: Science & Physics · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/tuning-fork-pair-comparison-x1522
Scene Description Prompt:
Build a self-contained HTML scene with Three.js from cdnjs featuring two stylized solid tuning forks on separate wooden blocks. Put their exaggerated tine motions above a nearby display of two sinusoidal traces and their sum. One setting gives the forks equal frequencies; another introduces a small difference while holding the amplitudes fixed. Slow the visual time scale enough to see the summed envelope grow and shrink. The displayed displacement is enlarged for inspection and is not a literal measurement of fork motion. Provide optional silent playback throughout: the changing traces and synchronized fork gestures should communicate the comparison without depending on audio output.
Working Three.js Starter (tuning-fork-pair-comparison-x1522):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Tuning Fork Beat Lab</title>
<style>
:root {
color-scheme: dark;
--bg: #071019;
--panel: rgba(7, 16, 27, 0.82);
--border: rgba(151, 210, 255, 0.19);
--text: #eaf6ff;
--muted: #9db0c1;
--cyan: #43dcff;
--orange: #ff9e45;
--white: #f6f7ff;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: var(--bg);
font-family:
Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
color: var(--text);
}
#stage {
position: fixed;
inset: 0;
}
#stage canvas {
display: block;
width: 100%;
height: 100%;
touch-action: none;
cursor: grab;
}
#stage canvas.dragging {
cursor: grabbing;
}
.hud {
position: fixed;
z-index: 10;
top: 18px;
left: 18px;
width: min(350px, calc(100vw - 36px));
padding: 18px;
border: 1px solid var(--border);
border-radius: 18px;
background: var(--panel);
box-shadow:
0 20px 55px rgba(0, 0, 0, 0.38),
inset 0 1px rgba(255, 255, 255, 0.035);
backdrop-filter: blur(16px);
-webkit-backdrop-filter: blur(16px);
user-select: none;
}
.eyebrow {
margin-bottom: 6px;
color: var(--cyan);
font-size: 11px;
font-weight: 800;
letter-spacing: 0.18em;
text-transform: uppercase;
}
h1 {
margin: 0;
font-size: 25px;
line-height: 1.05;
letter-spacing: -0.035em;
}
.intro {
margin: 10px 0 15px;
color: var(--muted);
font-size: 12.5px;
line-height: 1.5;
}
.section-label {
margin: 0 0 7px;
color: #8095a8;
font-size: 10px;
font-weight: 800;
letter-spacing: 0.13em;
text-transform: uppercase;
}
.segmented {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 7px;
}
button {
appearance: none;
border: 1px solid rgba(160, 210, 255, 0.17);
border-radius: 10px;
padding: 10px 11px;
background: rgba(255, 255, 255, 0.045);
color: var(--text);
font: inherit;
font-size: 12px;
font-weight: 750;
cursor: pointer;
transition:
background 140ms ease,
border-color 140ms ease,
transform 140ms ease;
}
button:hover {
background: rgba(255, 255, 255, 0.085);
border-color: rgba(160, 210, 255, 0.32);
}
button:active {
transform: translateY(1px);
}
button.active {
color: #06101a;
background: linear-gradient(135deg, #7ceaff, #43dcff);
border-color: transparent;
box-shadow: 0 5px 18px rgba(67, 220, 255, 0.18);
}
.readouts {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 7px;
margin: 12px 0;
}
.readout {
min-width: 0;
padding: 9px 8px;
border: 1px solid rgba(160, 210, 255, 0.11);
border-radius: 10px;
background: rgba(2, 8, 15, 0.38);
}
.readout span {
display: block;
margin-bottom: 3px;
color: #7890a5;
font-size: 9px;
font-weight: 750;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.readout strong {
display: block;
overflow: hidden;
color: #eaf7ff;
font-size: 13px;
font-variant-numeric: tabular-nums;
text-overflow: ellipsis;
white-space: nowrap;
}
.transport {
display: grid;
grid-template-columns: 1fr auto;
gap: 7px;
}
.primary {
background: rgba(67, 220, 255, 0.12);
border-color: rgba(67, 220, 255, 0.26);
color: #bdf5ff;
}
.icon-button {
min-width: 43px;
padding-inline: 12px;
}
.speed-row {
display: grid;
grid-template-columns: 1fr auto;
gap: 12px;
align-items: center;
margin-top: 13px;
}
.speed-row label {
color: var(--muted);
font-size: 11px;
font-weight: 700;
}
output {
min-width: 47px;
color: var(--white);
font-size: 12px;
font-weight: 800;
font-variant-numeric: tabular-nums;
text-align: right;
}
input[type="range"] {
width: 100%;
accent-color: var(--cyan);
cursor: pointer;
}
.note {
margin: 12px 0 0;
padding-top: 11px;
border-top: 1px solid rgba(160, 210, 255, 0.1);
color: #8197aa;
font-size: 10.5px;
line-height: 1.48;
}
.hint {
position: fixed;
z-index: 9;
right: 18px;
top: 18px;
padding: 9px 12px;
border: 1px solid var(--border);
border-radius: 999px;
background: rgba(7, 16, 27, 0.7);
color: #9fb5c8;
font-size: 11px;
backdrop-filter: blur(12px);
pointer-events: none;
}
.status {
position: fixed;
z-index: 9;
right: 18px;
bottom: 18px;
display: flex;
align-items: center;
gap: 8px;
padding: 9px 12px;
border: 1px solid var(--border);
border-radius: 999px;
background: rgba(7, 16, 27, 0.7);
color: #a9bdce;
font-size: 10px;
font-weight: 800;
letter-spacing: 0.1em;
text-transform: uppercase;
backdrop-filter: blur(12px);
pointer-events: none;
}
.status-dot {
width: 7px;
height: 7px;
border-radius: 50%;
background: var(--cyan);
box-shadow: 0 0 12px var(--cyan);
}
@media (max-width: 720px) {
.hud {
top: 10px;
left: 10px;
width: min(325px, calc(100vw - 20px));
padding: 14px;
max-height: calc(100vh - 20px);
overflow-y: auto;
}
h1 {
font-size: 21px;
}
.intro {
margin-bottom: 11px;
}
.hint {
display: none;
}
.status {
right: 10px;
bottom: 10px;
}
}
</style>
</head>
<body>
<div id="stage"></div>
<aside class="hud" aria-label="Beat demonstration controls">
<div class="eyebrow">Acoustic Mechanics / Visual Lab</div>
<h1>Tuning Fork Beat Lab</h1>
<p class="intro">
Compare two fixed-amplitude tuning forks. The traces and fork gestures
share the same phase, while the time scale is deliberately slowed.
</p>
<div class="section-label">Frequency relationship</div>
<div class="segmented">
<button id="equalBtn" class="active" aria-pressed="true">
Equal frequencies
</button>
<button id="detunedBtn" aria-pressed="false">
Small detune
</button>
</div>
<div class="readouts">
<div class="readout">
<span>Fork A</span>
<strong id="freqA">440.0 Hz</strong>
</div>
<div class="readout">
<span>Fork B</span>
<strong id="freqB">440.0 Hz</strong>
</div>
<div class="readout">
<span>Difference</span>
<strong id="deltaF">0.0 Hz</strong>
</div>
</div>
<div class="transport">
<button id="playBtn" class="primary">Pause silent animation</button>
<button id="restartBtn" class="icon-button" title="Restart comparison">
↺
</button>
<button id="viewBtn" class="icon-button" title="Reset camera view">
⌂
</button>
</div>
<div class="speed-row">
<div>
<label for="speed">Visual time slowdown</label>
<input
id="speed"
type="range"
min="60"
max="240"
step="30"
value="120"
/>
</div>
<output id="speedValue" for="speed">1/120</output>
</div>
<p class="note">
The trace displacement is enlarged for inspection and is schematic, not
a literal measurement of fork motion. The translucent band around the
sum shows its beat envelope. No audio is generated.
</p>
</aside>
<div class="hint">Drag to orbit · wheel to zoom · double-click to reset</div>
<div class="status">
<span class="status-dot"></span>
<span>Silent visualization</span>
</div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// -------------------------------------------------------------------------
// DOM and state
// -------------------------------------------------------------------------
const stage = document.querySelector("#stage");
const equalBtn = document.querySelector("#equalBtn");
const detunedBtn = document.querySelector("#detunedBtn");
const freqAEl = document.querySelector("#freqA");
const freqBEl = document.querySelector("#freqB");
const deltaFEl = document.querySelector("#deltaF");
const playBtn = document.querySelector("#playBtn");
const restartBtn = document.querySelector("#restartBtn");
const viewBtn = document.querySelector("#viewBtn");
const speedInput = document.querySelector("#speed");
const speedValue = document.querySelector("#speedValue");
const TAU = Math.PI * 2;
const FREQUENCIES = {
equal: [440, 440],
detuned: [440, 452]
};
const state = {
mode: "equal",
playing: true,
modelTime: 0,
timeScale: 1 / 120
};
const COLORS = {
cyan: 0x43dcff,
orange: 0xff9e45,
white: 0xf6f7ff,
envelope: 0xa98cff
};
// -------------------------------------------------------------------------
// Renderer, scene, camera, lighting
// -------------------------------------------------------------------------
const renderer = new THREE.WebGLRenderer({
antialias: true,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.12;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
stage.appendChild(renderer.domElement);
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x071019);
scene.fog = new THREE.Fog(0x071019, 18, 38);
const camera = new THREE.PerspectiveCamera(
42,
window.innerWidth / window.innerHeight,
0.1,
80
);
// Custom orbit controls; no addons are used.
const orbit = {
target: new THREE.Vector3(0.35, 1.65, 0),
radius: 15.8,
theta: 0.17,
phi: 1.22,
dragging: false,
lastX: 0,
lastY: 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);
}
function resetCamera() {
orbit.target.set(0.35, 1.65, 0);
orbit.radius = 15.8;
orbit.theta = 0.17;
orbit.phi = 1.22;
updateCamera();
}
updateCamera();
renderer.domElement.addEventListener("pointerdown", (event) => {
orbit.dragging = true;
orbit.lastX = event.clientX;
orbit.lastY = event.clientY;
renderer.domElement.setPointerCapture(event.pointerId);
renderer.domElement.classList.add("dragging");
});
renderer.domElement.addEventListener("pointermove", (event) => {
if (!orbit.dragging) return;
const dx = event.clientX - orbit.lastX;
const dy = event.clientY - orbit.lastY;
orbit.lastX = event.clientX;
orbit.lastY = event.clientY;
orbit.theta -= dx * 0.006;
orbit.phi = THREE.MathUtils.clamp(
orbit.phi + dy * 0.005,
0.48,
1.48
);
updateCamera();
});
function endDrag(event) {
orbit.dragging = false;
renderer.domElement.classList.remove("dragging");
if (renderer.domElement.hasPointerCapture(event.pointerId)) {
renderer.domElement.releasePointerCapture(event.pointerId);
}
}
renderer.domElement.addEventListener("pointerup", endDrag);
renderer.domElement.addEventListener("pointercancel", endDrag);
renderer.domElement.addEventListener(
"wheel",
(event) => {
event.preventDefault();
orbit.radius = THREE.MathUtils.clamp(
orbit.radius * Math.exp(event.deltaY * 0.001),
9,
25
);
updateCamera();
},
{ passive: false }
);
renderer.domElement.addEventListener("dblclick", resetCamera);
const hemisphere = new THREE.HemisphereLight(0xbce8ff, 0x392416, 1.65);
scene.add(hemisphere);
const keyLight = new THREE.DirectionalLight(0xd9f3ff, 3.2);
keyLight.position.set(-5, 10, 7);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(2048, 2048);
keyLight.shadow.camera.left = -12;
keyLight.shadow.camera.right = 12;
keyLight.shadow.camera.top = 12;
keyLight.shadow.camera.bottom = -12;
keyLight.shadow.camera.near = 1;
keyLight.shadow.camera.far = 30;
keyLight.shadow.bias = -0.00035;
scene.add(keyLight);
const warmLight = new THREE.PointLight(0xffad65, 18, 14, 2);
warmLight.position.set(5.5, 4.5, 4.5);
scene.add(warmLight);
const coolLight = new THREE.PointLight(0x54dfff, 15, 13, 2);
coolLight.position.set(-5, 4, 1.5);
scene.add(coolLight);
// -------------------------------------------------------------------------
// Environment
// -------------------------------------------------------------------------
const floorMaterial = new THREE.MeshStandardMaterial({
color: 0x101a23,
roughness: 0.92,
metalness: 0.08
});
const floor = new THREE.Mesh(
new THREE.PlaneGeometry(40, 40),
floorMaterial
);
floor.rotation.x = -Math.PI / 2;
floor.receiveShadow = true;
scene.add(floor);
const grid = new THREE.GridHelper(
30,
30,
0x315064,
0x18303f
);
grid.position.y = 0.012;
grid.material.transparent = true;
grid.material.opacity = 0.22;
scene.add(grid);
const wall = new THREE.Mesh(
new THREE.PlaneGeometry(40, 18),
new THREE.MeshStandardMaterial({
color: 0x08131d,
roughness: 1
})
);
wall.position.set(0, 7, -7.2);
wall.receiveShadow = true;
scene.add(wall);
// -------------------------------------------------------------------------
// Procedural textures and labels
// -------------------------------------------------------------------------
function makeWoodTexture() {
const canvas = document.createElement("canvas");
canvas.width = 512;
canvas.height = 256;
const ctx = canvas.getContext("2d");
ctx.fillStyle = "#a86c3f";
ctx.fillRect(0, 0, canvas.width, canvas.height);
let seed = 4913;
function random() {
seed = (seed * 16807) % 2147483647;
return (seed - 1) / 2147483646;
}
for (let i = 0; i < 130; i++) {
const y = random() * canvas.height;
const amplitude = 1 + random() * 7;
const alpha = 0.025 + random() * 0.09;
ctx.beginPath();
ctx.strokeStyle = `rgba(55, 27, 12, ${alpha})`;
ctx.lineWidth = 0.5 + random() * 2.4;
ctx.moveTo(0, y);
for (let x = 0; x <= canvas.width; x += 12) {
const wave =
y +
Math.sin(x * 0.025 + i) * amplitude +
Math.sin(x * 0.071 + i * 0.3) * amplitude * 0.35;
ctx.lineTo(x, wave);
}
ctx.stroke();
}
const gradient = ctx.createLinearGradient(0, 0, 0, canvas.height);
gradient.addColorStop(0, "rgba(255,220,160,0.12)");
gradient.addColorStop(0.5, "rgba(255,255,255,0)");
gradient.addColorStop(1, "rgba(30,10,0,0.18)");
ctx.fillStyle = gradient;
ctx.fillRect(0, 0, canvas.width, canvas.height);
const texture = new THREE.CanvasTexture(canvas);
texture.colorSpace = THREE.SRGBColorSpace;
texture.wrapS = THREE.RepeatWrapping;
texture.wrapT = THREE.RepeatWrapping;
texture.repeat.set(1.6, 1);
texture.anisotropy = renderer.capabilities.getMaxAnisotropy();
return texture;
}
const woodTexture = makeWoodTexture();
const woodMaterial = new THREE.MeshStandardMaterial({
map: woodTexture,
color: 0xffffff,
roughness: 0.68,
metalness: 0.03
});
const woodTopMaterial = new THREE.MeshStandardMaterial({
map: woodTexture,
color: 0xe0aa74,
roughness: 0.61,
metalness: 0.03
});
function roundRect(ctx, x, y, w, h, r) {
ctx.beginPath();
ctx.moveTo(x + r, y);
ctx.arcTo(x + w, y, x + w, y + h, r);
ctx.arcTo(x + w, y + h, x, y + h, r);
ctx.arcTo(x, y + h, x, y, r);
ctx.arcTo(x, y, x + w, y, r);
ctx.closePath();
}
function makePlaque(text, accent) {
const canvas = document.createElement("canvas");
canvas.width = 512;
canvas.height = 128;
const ctx = canvas.getContext("2d");
ctx.clearRect(0, 0, canvas.width, canvas.height);
roundRect(ctx, 8, 8, 496, 112, 20);
ctx.fillStyle = "rgba(19, 15, 12, 0.94)";
ctx.fill();
ctx.strokeStyle = accent;
ctx.lineWidth = 4;
ctx.stroke();
ctx.fillStyle = accent;
ctx.font = "800 52px system-ui";
ctx.textAlign = "center";
ctx.textBaseline = "middle";
ctx.fillText(text, 256, 66);
const texture = new THREE.CanvasTexture(canvas);
texture.colorSpace = THREE.SRGBColorSpace;
return new THREE.Mesh(
new THREE.PlaneGeometry(1.2, 0.3),
new THREE.MeshBasicMaterial({
map: texture,
transparent: true
})
);
}
function makeSprite(initialText, accent) {
const sprite = new THREE.Sprite(
new THREE.SpriteMaterial({
transparent: true,
depthTest: false,
depthWrite: false
})
);
sprite.scale.set(2.2, 0.44, 1);
sprite.renderOrder = 20;
setSpriteText(sprite, initialText, accent);
return sprite;
}
function setSpriteText(sprite, text, accent) {
const canvas = document.createElement("canvas");
canvas.width = 640;
canvas.height = 128;
const ctx = canvas.getContext("2d");
ctx.clearRect(0, 0, canvas.width, canvas.height);
roundRect(ctx, 8, 8, 624, 112, 26);
ctx.fillStyle = "rgba(5, 13, 22, 0.9)";
ctx.fill();
ctx.strokeStyle = accent;
ctx.lineWidth = 3;
ctx.stroke();
ctx.fillStyle = accent;
ctx.font = "800 42px system-ui";
ctx.textAlign = "center";
ctx.textBaseline = "middle";
ctx.fillText(text, 320, 65);
const oldTexture = sprite.material.map;
const texture = new THREE.CanvasTexture(canvas);
texture.colorSpace = THREE.SRGBColorSpace;
sprite.material.map = texture;
sprite.material.needsUpdate = true;
if (oldTexture) oldTexture.dispose();
}
// -------------------------------------------------------------------------
// Wooden blocks and solid tuning forks
// -------------------------------------------------------------------------
const metalMaterial = new THREE.MeshStandardMaterial({
color: 0xb9d2dc,
roughness: 0.24,
metalness: 0.78
});
const darkMetalMaterial = new THREE.MeshStandardMaterial({
color: 0x263b47,
roughness: 0.34,
metalness: 0.66
});
const screwMaterial = new THREE.MeshStandardMaterial({
color: 0x30383b,
roughness: 0.35,
metalness: 0.8
});
function makeBlock(x, z, letter, accent) {
const group = new THREE.Group();
group.position.set(x, 0, z);
const body = new THREE.Mesh(
new THREE.BoxGeometry(2.65, 0.64, 2.2),
woodMaterial
);
body.position.y = 0.32;
body.castShadow = true;
body.receiveShadow = true;
group.add(body);
const top = new THREE.Mesh(
new THREE.BoxGeometry(2.58, 0.14, 2.12),
woodTopMaterial
);
top.position.y = 0.7;
top.castShadow = true;
top.receiveShadow = true;
group.add(top);
const plaque = makePlaque(letter, accent);
plaque.position.set(0, 0.36, 1.107);
group.add(plaque);
const screwGeometry = new THREE.CylinderGeometry(0.055, 0.055, 0.025, 20);
for (const sx of [-1.05, 1.05]) {
for (const sz of [-0.8, 0.8]) {
const screw = new THREE.Mesh(screwGeometry, screwMaterial);
screw.position.set(sx, 0.782, sz);
screw.castShadow = true;
group.add(screw);
}
}
const halo = new THREE.Mesh(
new THREE.CircleGeometry(1.72, 64),
new THREE.MeshBasicMaterial({
color: accent,
transparent: true,
opacity: 0.07,
depthWrite: false
})
);
halo.rotation.x = -Math.PI / 2;
halo.position.y = 0.018;
group.add(halo);
scene.add(group);
return group;
}
function makeFork(blockX, blockZ, accent) {
const group = new THREE.Group();
group.position.set(blockX, 0.86, blockZ);
const mount = new THREE.Mesh(
new THREE.CylinderGeometry(0.48, 0.54, 0.14, 40),
darkMetalMaterial
);
mount.position.y = -0.08;
mount.castShadow = true;
mount.receiveShadow = true;
group.add(mount);
const handle = new THREE.Mesh(
new THREE.SphereGeometry(0.25, 24, 16),
metalMaterial
);
handle.scale.set(0.9, 1.1, 0.9);
handle.position.y = 0.15;
handle.castShadow = true;
group.add(handle);
const stem = new THREE.Mesh(
new THREE.BoxGeometry(0.38, 1.08, 0.38),
metalMaterial
);
stem.position.y = 0.74;
stem.castShadow = true;
group.add(stem);
const yoke = new THREE.Mesh(
new THREE.BoxGeometry(1.3, 0.3, 0.44),
metalMaterial
);
yoke.position.y = 1.32;
yoke.castShadow = true;
group.add(yoke);
const stripeMaterial = new THREE.MeshStandardMaterial({
color: accent,
emissive: accent,
emissiveIntensity: 0.65,
roughness: 0.35,
metalness: 0.25
});
function makeProng(side) {
const pivot = new THREE.Group();
pivot.position.set(side * 0.43, 1.47, 0);
group.add(pivot);
const length = 2.15;
const shaft = new THREE.Mesh(
new THREE.BoxGeometry(0.32, length, 0.43),
metalMaterial
);
shaft.position.y = length * 0.5;
shaft.castShadow = true;
pivot.add(shaft);
const cap = new THREE.Mesh(
new THREE.SphereGeometry(0.18, 24, 16),
metalMaterial
);
cap.scale.set(1, 1.12, 1);
cap.position.y = length;
cap.castShadow = true;
pivot.add(cap);
const stripe = new THREE.Mesh(
new THREE.BoxGeometry(0.045, 1.35, 0.02),
stripeMaterial
);
stripe.position.set(side * -0.115, 1.05, 0.225);
pivot.add(stripe);
return pivot;
}
const leftPivot = makeProng(-1);
const rightPivot = makeProng(1);
scene.add(group);
return { group, leftPivot, rightPivot };
}
makeBlock(-3.6, 0.35, "A", "#43dcff");
makeBlock(-0.6, 0.05, "B", "#ff9e45");
const forkA = makeFork(-3.6, 0.35, COLORS.cyan);
const forkB = makeFork(-0.6, 0.05, COLORS.orange);
const labelA = makeSprite("A • 440 Hz", "#43dcff");
labelA.position.set(-3.6, 4.7, 0.35);
scene.add(labelA);
const labelB = makeSprite("B • 440 Hz", "#ff9e45");
labelB.position.set(-0.6, 4.7, 0.05);
scene.add(labelB);
function animateFork(fork, signal) {
// Exaggerated, schematic lateral tine motion.
const angle = signal * 0.075;
fork.leftPivot.rotation.z = angle;
fork.rightPivot.rotation.z = -angle;
}
// -------------------------------------------------------------------------
// Display panel and dynamic traces
// -------------------------------------------------------------------------
const panelGroup = new THREE.Group();
panelGroup.position.set(4.2, 2.55, -0.8);
panelGroup.rotation.y = 0.2;
scene.add(panelGroup);
const panelBody = new THREE.Mesh(
new THREE.BoxGeometry(6.55, 4.25, 0.3),
new THREE.MeshStandardMaterial({
color: 0x101b25,
roughness: 0.48,
metalness: 0.48
})
);
panelBody.castShadow = true;
panelBody.receiveShadow = true;
panelGroup.add(panelBody);
const frameMaterial = new THREE.MeshStandardMaterial({
color: 0x38515f,
roughness: 0.34,
metalness: 0.7
});
function addFrame(w, h, x, y) {
const bar = new THREE.Mesh(
new THREE.BoxGeometry(w, h, 0.09),
frameMaterial
);
bar.position.set(x, y, 0.17);
panelGroup.add(bar);
}
addFrame(6.58, 0.085, 0, 2.085);
addFrame(6.58, 0.085, 0, -2.085);
addFrame(0.085, 4.25, -3.24, 0);
addFrame(0.085, 4.25, 3.24, 0);
const screenCanvas = document.createElement("canvas");
screenCanvas.width = 1200;
screenCanvas.height = 760;
const screenCtx = screenCanvas.getContext("2d");
const screenTexture = new THREE.CanvasTexture(screenCanvas);
screenTexture.colorSpace = THREE.SRGBColorSpace;
const screen = new THREE.Mesh(
new THREE.PlaneGeometry(6.15, 3.9),
new THREE.MeshBasicMaterial({
map: screenTexture
})
);
screen.position.z = 0.16;
panelGroup.add(screen);
const stand = new THREE.Mesh(
new THREE.BoxGeometry(0.46, 1.2, 0.46),
darkMetalMaterial
);
stand.position.set(4.2, 0.6, -0.95);
stand.castShadow = true;
scene.add(stand);
const standFoot = new THREE.Mesh(
new THREE.BoxGeometry(1.65, 0.12, 1.05),
darkMetalMaterial
);
standFoot.position.set(4.2, 0.06, -0.95);
standFoot.castShadow = true;
standFoot.receiveShadow = true;
scene.add(standFoot);
const TRACE_SAMPLES = 320;
const TRACE_WINDOW = 8;
const TRACE_AMPLITUDE = 0.235;
const PLOT_LEFT = -2.306;
const PLOT_RIGHT = 2.806;
const LANE_Y = [1.206, 0, -1.206];
function drawPanel() {
const ctx = screenCtx;
const width = screenCanvas.width;
const height = screenCanvas.height;
const [f1, f2] = FREQUENCIES[state.mode];
const gradient = ctx.createLinearGradient(0, 0, width, height);
gradient.addColorStop(0, "#081722");
gradient.addColorStop(0.55, "#07131d");
gradient.addColorStop(1, "#0b1520");
ctx.fillStyle = gradient;
ctx.fillRect(0, 0, width, height);
ctx.strokeStyle = "rgba(102, 190, 225, 0.13)";
ctx.lineWidth = 1;
for (let x = 150; x <= 1150; x += 100) {
ctx.beginPath();
ctx.moveTo(x, 92);
ctx.lineTo(x, 660);
ctx.stroke();
}
for (const y of [145, 380, 615]) {
ctx.beginPath();
ctx.moveTo(150, y);
ctx.lineTo(1150, y);
ctx.stroke();
}
ctx.fillStyle = "#dff7ff";
ctx.font = "800 31px system-ui";
ctx.textAlign = "left";
ctx.textBaseline = "alphabetic";
ctx.fillText("DISPLACEMENT MONITOR", 72, 52);
ctx.fillStyle = "#7896aa";
ctx.font = "600 17px system-ui";
ctx.fillText(
state.mode === "equal"
? "EQUAL FREQUENCIES / CONSTANT SUM"
: "SMALL DETUNE / BEAT ENVELOPE",
72,
78
);
const labels = [
{ text: `A ${f1.toFixed(1)} Hz`, color: "#43dcff" },
{ text: `B ${f2.toFixed(1)} Hz`, color: "#ff9e45" },
{ text: "SUM", color: "#f6f7ff" }
];
labels.forEach((label, index) => {
const y = [145, 380, 615][index];
ctx.fillStyle = label.color;
ctx.font = "800 24px system-ui";
ctx.textAlign = "left";
ctx.fillText(label.text, 27, y + 8);
ctx.setLineDash([7, 9]);
ctx.strokeStyle = "rgba(190, 220, 235, 0.17)";
ctx.lineWidth = 1;
ctx.beginPath();
ctx.moveTo(150, y);
ctx.lineTo(1150, y);
ctx.stroke();
ctx.setLineDash([]);
});
ctx.fillStyle = "rgba(222, 241, 249, 0.7)";
ctx.font = "700 13px system-ui";
ctx.textAlign = "right";
ctx.fillText("NOW", 1150, 108);
ctx.fillStyle = "#8199ad";
ctx.font = "600 16px system-ui";
ctx.textAlign = "left";
ctx.fillText(
`A₁ = A₂ = 1.00 • Δf = ${(f2 - f1).toFixed(1)} Hz • visual rate 1/${Math.round(1 / state.timeScale)}`,
72,
694
);
ctx.fillStyle = "#647f93";
ctx.font = "600 14px system-ui";
ctx.fillText(
"AMPLIFIED / SCHEMATIC DISPLACEMENT — NOT A LITERAL MEASUREMENT",
72,
724
);
screenTexture.needsUpdate = true;
}
// Dynamic ribbons are used instead of one-pixel lines.
function createStroke(color, width, opacity, z, order, additive = false) {
const geometry = new THREE.BufferGeometry();
const positions = new THREE.BufferAttribute(
new Float32Array((TRACE_SAMPLES + 1) * 2 * 3),
3
);
geometry.setAttribute("position", positions);
const indices = [];
for (let i = 0; i < TRACE_SAMPLES; 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);
}
geometry.setIndex(indices);
const material = new THREE.MeshBasicMaterial({
color,
transparent: true,
opacity,
side: THREE.DoubleSide,
depthWrite: false,
blending: additive
? THREE.AdditiveBlending
: THREE.NormalBlending
});
const mesh = new THREE.Mesh(geometry, material);
mesh.frustumCulled = false;
mesh.renderOrder = order;
mesh.userData.width = width;
mesh.userData.z = z;
panelGroup.add(mesh);
return mesh;
}
function writeStroke(mesh, points) {
const positions = mesh.geometry.attributes.position.array;
const halfWidth = mesh.userData.width * 0.5;
const z = mesh.userData.z;
for (let i = 0; i < points.length; i++) {
const previous = points[Math.max(0, i - 1)];
const next = points[Math.min(points.length - 1, i + 1)];
const point = points[i];
const dx = next.x - previous.x;
const dy = next.y - previous.y;
const length = Math.hypot(dx, dy) || 1;
const nx = (-dy / length) * halfWidth;
const ny = (dx / length) * halfWidth;
const offset = i * 6;
positions[offset] = point.x + nx;
positions[offset + 1] = point.y + ny;
positions[offset + 2] = z;
positions[offset + 3] = point.x - nx;
positions[offset + 4] = point.y - ny;
positions[offset + 5] = z;
}
mesh.geometry.attributes.position.needsUpdate = true;
}
const strokes = {
aGlow: createStroke(COLORS.cyan, 0.065, 0.12, 0.202, 5, true),
bGlow: createStroke(COLORS.orange, 0.065, 0.12, 0.203, 5, true),
sumGlow: createStroke(COLORS.white, 0.09, 0.12, 0.207, 8, true),
a: createStroke(COLORS.cyan, 0.018, 0.95, 0.225, 6),
b: createStroke(COLORS.orange, 0.018, 0.95, 0.226, 7),
sum: createStroke(COLORS.white, 0.026, 1, 0.235, 9)
};
// Translucent beat-envelope band around the sum trace.
const envelopeGeometry = new THREE.BufferGeometry();
envelopeGeometry.setAttribute(
"position",
new THREE.BufferAttribute(
new Float32Array((TRACE_SAMPLES + 1) * 2 * 3),
3
)
);
const envelopeIndices = [];
for (let i = 0; i < TRACE_SAMPLES; i++) {
const a = i * 2;
const b = a + 1;
const c = a + 2;
const d = a + 3;
envelopeIndices.push(a, b, c, b, d, c);
}
envelopeGeometry.setIndex(envelopeIndices);
const envelopeMesh = new THREE.Mesh(
envelopeGeometry,
new THREE.MeshBasicMaterial({
color: COLORS.envelope,
transparent: true,
opacity: 0.105,
side: THREE.DoubleSide,
depthWrite: false,
blending: THREE.AdditiveBlending
})
);
envelopeMesh.frustumCulled = false;
envelopeMesh.renderOrder = 4;
panelGroup.add(envelopeMesh);
const nowLine = new THREE.Mesh(
new THREE.BoxGeometry(0.012, 3.43, 0.008),
new THREE.MeshBasicMaterial({
color: 0xdff8ff,
transparent: true,
opacity: 0.24,
depthWrite: false
})
);
nowLine.position.set(PLOT_RIGHT, 0, 0.213);
panelGroup.add(nowLine);
function makePointArray() {
return Array.from({ length: TRACE_SAMPLES + 1 }, () => ({
x: 0,
y: 0,
z: 0
}));
}
const pointsA = makePointArray();
const pointsB = makePointArray();
const pointsSum = makePointArray();
function updateTraces() {
const [f1, f2] = FREQUENCIES[state.mode];
for (let i = 0; i <= TRACE_SAMPLES; i++) {
const u = i / TRACE_SAMPLES;
const visualOffset = (u - 1) * TRACE_WINDOW;
const sampleModelTime = state.modelTime + visualOffset * state.timeScale;
const phaseA = TAU * f1 * sampleModelTime;
const phaseB = TAU * f2 * sampleModelTime;
const signalA = Math.sin(phaseA);
const signalB = Math.sin(phaseB);
const signalSum = signalA + signalB;
const x = THREE.MathUtils.lerp(PLOT_LEFT, PLOT_RIGHT, u);
pointsA[i].x = x;
pointsA[i].y = LANE_Y[0] + signalA * TRACE_AMPLITUDE;
pointsB[i].x = x;
pointsB[i].y = LANE_Y[1] + signalB * TRACE_AMPLITUDE;
pointsSum[i].x = x;
pointsSum[i].y = LANE_Y[2] + signalSum * TRACE_AMPLITUDE;
// For close tones:
// sin(w1t) + sin(w2t) = 2 sin(avg(w)t) cos(delta(w)t / 2).
const envelope =
2 *
Math.abs(
Math.cos(Math.PI * (f2 - f1) * sampleModelTime)
);
const positions = envelopeGeometry.attributes.position.array;
const offset = i * 6;
positions[offset] = x;
positions[offset + 1] =
LANE_Y[2] + envelope * TRACE_AMPLITUDE;
positions[offset + 2] = 0.196;
positions[offset + 3] = x;
positions[offset + 4] =
LANE_Y[2] - envelope * TRACE_AMPLITUDE;
positions[offset + 5] = 0.196;
}
envelopeGeometry.attributes.position.needsUpdate = true;
writeStroke(strokes.aGlow, pointsA);
writeStroke(strokes.bGlow, pointsB);
writeStroke(strokes.sumGlow, pointsSum);
writeStroke(strokes.a, pointsA);
writeStroke(strokes.b, pointsB);
writeStroke(strokes.sum, pointsSum);
}
// -------------------------------------------------------------------------
// UI behavior
// -------------------------------------------------------------------------
function updateUI() {
const [f1, f2] = FREQUENCIES[state.mode];
freqAEl.textContent = `${f1.toFixed(1)} Hz`;
freqBEl.textContent = `${f2.toFixed(1)} Hz`;
deltaFEl.textContent = `${(f2 - f1).toFixed(1)} Hz`;
equalBtn.classList.toggle("active", state.mode === "equal");
detunedBtn.classList.toggle("active", state.mode === "detuned");
equalBtn.setAttribute(
"aria-pressed",
String(state.mode === "equal")
);
detunedBtn.setAttribute(
"aria-pressed",
String(state.mode === "detuned")
);
playBtn.textContent = state.playing
? "Pause silent animation"
: "Play silent animation";
setSpriteText(
labelA,
`A • ${f1.toFixed(0)} Hz`,
"#43dcff"
);
setSpriteText(
labelB,
`B • ${f2.toFixed(0)} Hz`,
"#ff9e45"
);
drawPanel();
}
function setMode(mode) {
state.mode = mode;
state.modelTime = 0;
updateUI();
}
equalBtn.addEventListener("click", () => setMode("equal"));
detunedBtn.addEventListener("click", () => setMode("detuned"));
playBtn.addEventListener("click", () => {
state.playing = !state.playing;
updateUI();
});
restartBtn.addEventListener("click", () => {
state.modelTime = 0;
});
viewBtn.addEventListener("click", resetCamera);
speedInput.addEventListener("input", () => {
const denominator = Number(speedInput.value);
state.timeScale = 1 / denominator;
speedValue.textContent = `1/${denominator}`;
drawPanel();
});
window.addEventListener("keydown", (event) => {
if (event.code === "Space" && event.target === document.body) {
event.preventDefault();
state.playing = !state.playing;
updateUI();
}
});
updateUI();
// -------------------------------------------------------------------------
// Animation loop
// -------------------------------------------------------------------------
let previousTime = performance.now();
function animate(now) {
requestAnimationFrame(animate);
const delta = Math.min((now - previousTime) / 1000, 0.05);
previousTime = now;
if (state.playing) {
state.modelTime += delta * state.timeScale;
}
const [f1, f2] = FREQUENCIES[state.mode];
const signalA = Math.sin(TAU * f1 * state.modelTime);
const signalB = Math.sin(TAU * f2 * state.modelTime);
animateFork(forkA, signalA);
animateFork(forkB, signalB);
updateTraces();
renderer.render(scene, camera);
}
requestAnimationFrame(animate);
window.addEventListener("resize", () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
});
</script>
</body>
</html>
```