Prompt specification
Build or customize the Three.js 3D scene "Telephone diaphragm and receiver cutaway" 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: Telephone diaphragm and receiver cutaway
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/telephone-diaphragm-and-receiver-cutaway-x2545
Scene Description Prompt:
Model a simplified early telephone diaphragm and receiver housing. Show the membrane moving with an illustrative sound wave while keeping the electrical mechanism separately labeled. Build this with Three.js from cdnjs as a voxel scene in one self-contained HTML file.
Working Three.js Starter (telephone-diaphragm-and-receiver-cutaway-x2545):
```html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Early Telephone Receiver — Voxel Study</title>
<style>
:root {
--bg: #07111a;
--panel: rgba(8, 19, 29, 0.78);
--border: rgba(176, 215, 232, 0.18);
--text: #e8f4f7;
--muted: #93aab5;
--cyan: #61e7ff;
--orange: #ff9b57;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: var(--bg);
color: var(--text);
font-family: Inter, ui-sans-serif, system-ui, -apple-system,
BlinkMacSystemFont, "Segoe UI", sans-serif;
}
#stage,
#stage canvas {
position: fixed;
inset: 0;
width: 100%;
height: 100%;
display: block;
}
#stage canvas {
touch-action: none;
cursor: grab;
}
#stage canvas:active {
cursor: grabbing;
}
.header {
position: fixed;
top: 24px;
left: 28px;
z-index: 10;
width: min(470px, calc(100vw - 220px));
pointer-events: none;
}
.eyebrow {
margin-bottom: 7px;
color: var(--cyan);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.22em;
text-transform: uppercase;
}
h1 {
margin: 0;
font-size: clamp(22px, 3vw, 36px);
font-weight: 720;
letter-spacing: -0.035em;
line-height: 1.04;
}
.subtitle {
margin: 10px 0 0;
max-width: 430px;
color: var(--muted);
font-size: 12px;
line-height: 1.55;
}
.controls {
position: fixed;
top: 24px;
right: 28px;
z-index: 12;
display: flex;
gap: 8px;
}
button {
appearance: none;
border: 1px solid var(--border);
border-radius: 7px;
padding: 9px 12px;
background: var(--panel);
color: var(--text);
font: inherit;
font-size: 11px;
font-weight: 700;
letter-spacing: 0.03em;
cursor: pointer;
backdrop-filter: blur(12px);
transition:
border-color 160ms ease,
background 160ms ease,
transform 160ms ease;
}
button:hover {
border-color: rgba(97, 231, 255, 0.55);
background: rgba(16, 36, 49, 0.9);
}
button:active {
transform: translateY(1px);
}
.legend {
position: fixed;
bottom: 24px;
left: 28px;
z-index: 10;
display: flex;
flex-wrap: wrap;
gap: 9px 18px;
max-width: calc(100vw - 56px);
color: var(--muted);
font-size: 10px;
font-weight: 750;
letter-spacing: 0.09em;
text-transform: uppercase;
pointer-events: none;
}
.legend-item {
display: flex;
align-items: center;
gap: 7px;
}
.swatch {
width: 8px;
height: 8px;
border-radius: 2px;
box-shadow: 0 0 12px currentColor;
}
.swatch.cyan {
color: var(--cyan);
background: var(--cyan);
}
.swatch.orange {
color: var(--orange);
background: var(--orange);
}
.swatch.white {
color: #e4d3aa;
background: #e4d3aa;
}
.hint {
position: fixed;
right: 28px;
bottom: 24px;
z-index: 10;
color: rgba(181, 201, 210, 0.58);
font-size: 10px;
font-weight: 650;
letter-spacing: 0.08em;
text-transform: uppercase;
pointer-events: none;
}
#labels {
position: fixed;
inset: 0;
z-index: 8;
pointer-events: none;
transition: opacity 180ms ease;
}
#labels.off {
opacity: 0;
}
#leaders {
position: absolute;
inset: 0;
width: 100%;
height: 100%;
overflow: visible;
}
#leaders line {
stroke-width: 1;
stroke-dasharray: 3 4;
opacity: 0.65;
}
#leaders circle {
stroke-width: 1;
opacity: 0.85;
}
.tag {
position: absolute;
transform: translate(-50%, -50%);
padding: 6px 8px;
border: 1px solid var(--border);
border-left: 3px solid #d8c38e;
border-radius: 4px;
background: rgba(5, 14, 22, 0.84);
color: #dce9ed;
font-size: 9px;
font-weight: 800;
letter-spacing: 0.095em;
line-height: 1;
text-transform: uppercase;
white-space: nowrap;
box-shadow: 0 7px 22px rgba(0, 0, 0, 0.22);
backdrop-filter: blur(8px);
}
.tag::before {
content: "";
display: inline-block;
width: 4px;
height: 4px;
margin-right: 6px;
border-radius: 1px;
background: currentColor;
vertical-align: 1px;
}
.tag.acoustic {
border-left-color: var(--cyan);
color: var(--cyan);
}
.tag.electrical {
border-left-color: var(--orange);
color: var(--orange);
}
.tag.section {
padding: 7px 10px;
font-size: 10px;
letter-spacing: 0.13em;
}
.status {
position: fixed;
left: 28px;
bottom: 52px;
z-index: 10;
color: rgba(181, 201, 210, 0.55);
font-size: 9px;
font-weight: 700;
letter-spacing: 0.08em;
text-transform: uppercase;
pointer-events: none;
}
.status::before {
content: "";
display: inline-block;
width: 5px;
height: 5px;
margin-right: 6px;
border-radius: 50%;
background: var(--cyan);
box-shadow: 0 0 9px var(--cyan);
vertical-align: 1px;
}
@media (max-width: 720px) {
.header {
top: 18px;
left: 18px;
width: calc(100vw - 36px);
}
.subtitle {
display: none;
}
.controls {
top: auto;
right: 18px;
bottom: 58px;
}
button {
padding: 8px 9px;
font-size: 9px;
}
.legend {
left: 18px;
bottom: 18px;
gap: 7px 12px;
}
.hint,
.status {
display: none;
}
.tag {
font-size: 8px;
}
}
</style>
</head>
<body>
<div id="stage"></div>
<header class="header">
<div class="eyebrow">Voxel mechanics study · 01</div>
<h1>Early telephone receiver</h1>
<p class="subtitle">
A simplified acoustic diaphragm and receiver housing. The electromagnetic
assembly is detached and labeled separately to clarify the electrical stage.
</p>
</header>
<nav class="controls" aria-label="Scene controls">
<button id="pause" type="button" aria-pressed="false">Pause motion</button>
<button id="toggle-labels" type="button" aria-pressed="true">Hide labels</button>
<button id="reset" type="button">Reset view</button>
</nav>
<div id="labels" aria-hidden="true">
<svg id="leaders" xmlns="http://www.w3.org/2000/svg"></svg>
</div>
<div class="status" id="motion-state">Animation running</div>
<div class="legend">
<div class="legend-item">
<span class="swatch cyan"></span>
Sound / mechanical
</div>
<div class="legend-item">
<span class="swatch orange"></span>
Electrical mechanism
</div>
<div class="legend-item">
<span class="swatch white"></span>
Receiver housing
</div>
</div>
<div class="hint">Drag to orbit · Scroll or pinch to zoom</div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// ------------------------------------------------------------
// Renderer and scene
// ------------------------------------------------------------
const stage = document.querySelector("#stage");
const renderer = new THREE.WebGLRenderer({
antialias: true,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.12;
stage.appendChild(renderer.domElement);
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x07111a);
scene.fog = new THREE.Fog(0x07111a, 17, 32);
const camera = new THREE.PerspectiveCamera(
40,
window.innerWidth / window.innerHeight,
0.1,
80
);
const target = new THREE.Vector3(0, 0.45, 0);
const view = {
azimuth: -0.64,
polar: 1.16,
radius: 14.8
};
const initialView = { ...view };
function applyCamera() {
const sinPolar = Math.sin(view.polar);
camera.position.set(
target.x + view.radius * sinPolar * Math.sin(view.azimuth),
target.y + view.radius * Math.cos(view.polar),
target.z + view.radius * sinPolar * Math.cos(view.azimuth)
);
camera.lookAt(target);
}
applyCamera();
// ------------------------------------------------------------
// Lighting
// ------------------------------------------------------------
scene.add(new THREE.HemisphereLight(0x93dcff, 0x101219, 1.55));
const keyLight = new THREE.DirectionalLight(0xfff0d8, 3.1);
keyLight.position.set(-5, 10, 8);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(2048, 2048);
keyLight.shadow.camera.left = -11;
keyLight.shadow.camera.right = 11;
keyLight.shadow.camera.top = 9;
keyLight.shadow.camera.bottom = -7;
keyLight.shadow.camera.near = 1;
keyLight.shadow.camera.far = 30;
keyLight.shadow.normalBias = 0.035;
scene.add(keyLight);
const cyanLight = new THREE.PointLight(0x53dcff, 18, 8, 2);
cyanLight.position.set(-3.3, 2.1, 3.2);
scene.add(cyanLight);
const electricalLight = new THREE.PointLight(0xff8b47, 11, 7, 2);
electricalLight.position.set(4.0, 1.8, 2.4);
scene.add(electricalLight);
// ------------------------------------------------------------
// Shared geometry and material helpers
// ------------------------------------------------------------
const unitBox = new THREE.BoxGeometry(1, 1, 1);
const materialCache = new Map();
function getMaterial(color, metalness = 0.12, roughness = 0.68) {
const key = `${color}-${metalness}-${roughness}`;
if (!materialCache.has(key)) {
materialCache.set(
key,
new THREE.MeshStandardMaterial({
color,
metalness,
roughness
})
);
}
return materialCache.get(key);
}
function addBox(
parent,
size,
position,
color,
options = {}
) {
const material =
options.material ||
getMaterial(
color,
options.metalness ?? 0.12,
options.roughness ?? 0.68
);
const mesh = new THREE.Mesh(unitBox, material);
mesh.scale.set(size[0], size[1], size[2]);
mesh.position.set(position[0], position[1], position[2]);
mesh.castShadow = options.castShadow !== false;
mesh.receiveShadow = options.receiveShadow !== false;
parent.add(mesh);
return mesh;
}
function addInstancedBoxes(
parent,
cells,
material,
castShadow = true
) {
const mesh = new THREE.InstancedMesh(
unitBox,
material,
cells.length
);
const dummy = new THREE.Object3D();
const color = new THREE.Color();
cells.forEach((cell, index) => {
dummy.position.set(cell.x, cell.y, cell.z);
dummy.scale.set(cell.sx, cell.sy, cell.sz);
dummy.rotation.set(0, 0, 0);
dummy.updateMatrix();
mesh.setMatrixAt(index, dummy.matrix);
if (cell.color !== undefined) {
color.set(cell.color);
mesh.setColorAt(index, color);
}
});
mesh.instanceMatrix.needsUpdate = true;
if (mesh.instanceColor) {
mesh.instanceColor.needsUpdate = true;
}
mesh.castShadow = castShadow;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
function addVoxelPath(parent, points, color, size = 0.09) {
const cells = [];
const sampleStep = size * 0.85;
for (let segment = 0; segment < points.length - 1; segment++) {
const a = points[segment];
const b = points[segment + 1];
const dx = b[0] - a[0];
const dy = b[1] - a[1];
const dz = b[2] - a[2];
const length = Math.hypot(dx, dy, dz);
const steps = Math.max(1, Math.ceil(length / sampleStep));
for (let i = 0; i < steps; i++) {
const t = i / steps;
cells.push({
x: a[0] + dx * t,
y: a[1] + dy * t,
z: a[2] + dz * t,
sx: size,
sy: size,
sz: size,
color
});
}
}
const material = new THREE.MeshStandardMaterial({
color: 0xffffff,
metalness: 0.35,
roughness: 0.48
});
return addInstancedBoxes(parent, cells, material);
}
// Deterministic random values for stable scene details.
let randomSeed = 12731;
function random() {
randomSeed =
(randomSeed * 1664525 + 1013904223) >>> 0;
return randomSeed / 4294967296;
}
// ------------------------------------------------------------
// Voxel floor
// ------------------------------------------------------------
const floorCells = [];
const floorMaterial = new THREE.MeshStandardMaterial({
color: 0xffffff,
metalness: 0.04,
roughness: 0.95
});
for (let ix = -15; ix <= 15; ix++) {
for (let iz = -10; iz <= 10; iz++) {
const lightness = 0.075 + random() * 0.022;
const color = new THREE.Color().setHSL(
0.56,
0.22,
lightness
);
floorCells.push({
x: ix * 0.55,
y: -1.46,
z: iz * 0.55,
sx: 0.522,
sy: 0.08,
sz: 0.522,
color
});
}
}
addInstancedBoxes(scene, floorCells, floorMaterial, false);
// ------------------------------------------------------------
// Receiver housing
// Local front direction is -X, so the sound wave can approach
// the diaphragm along the X axis.
// ------------------------------------------------------------
const receiver = new THREE.Group();
receiver.position.set(-0.4, 0.65, 0);
scene.add(receiver);
// Rear casing made from square voxels.
const casingCells = [];
for (let iy = -3; iy <= 3; iy++) {
for (let iz = -3; iz <= 3; iz++) {
casingCells.push({
x: 0.47,
y: iy * 0.56,
z: iz * 0.56,
sx: 0.35,
sy: 0.53,
sz: 0.53,
color: new THREE.Color().setHSL(
0.105,
0.22,
0.24 + random() * 0.055
)
});
}
}
const casingMaterial = new THREE.MeshStandardMaterial({
color: 0xffffff,
metalness: 0.48,
roughness: 0.54
});
addInstancedBoxes(receiver, casingCells, casingMaterial);
// Square outer frame.
addBox(receiver, [0.56, 0.26, 3.9], [0.18, 1.92, 0], 0xb88b4c, {
metalness: 0.55,
roughness: 0.48
});
addBox(receiver, [0.56, 0.26, 3.9], [0.18, -1.92, 0], 0xb88b4c, {
metalness: 0.55,
roughness: 0.48
});
addBox(receiver, [0.56, 3.9, 0.26], [0.18, 0, 1.92], 0xa97c41, {
metalness: 0.55,
roughness: 0.5
});
addBox(receiver, [0.56, 3.9, 0.26], [0.18, 0, -1.92], 0xa97c41, {
metalness: 0.55,
roughness: 0.5
});
// Circular voxel rim.
const rimCells = [];
const rimStep = 0.27;
for (let iy = -7; iy <= 7; iy++) {
for (let iz = -7; iz <= 7; iz++) {
const y = iy * rimStep;
const z = iz * rimStep;
const radius = Math.hypot(y, z);
if (radius >= 1.32 && radius <= 1.88) {
rimCells.push({
x: -0.08,
y,
z,
sx: 0.38,
sy: 0.255,
sz: 0.255,
color: new THREE.Color().setHSL(
0.105,
0.48,
0.45 + random() * 0.09
)
});
}
}
}
const rimMaterial = new THREE.MeshStandardMaterial({
color: 0xffffff,
metalness: 0.62,
roughness: 0.38
});
addInstancedBoxes(receiver, rimCells, rimMaterial);
// Small mounting lugs.
addBox(receiver, [0.72, 0.28, 1.05], [0.12, 2.12, 0], 0xd1a85d, {
metalness: 0.56
});
addBox(receiver, [0.72, 0.28, 1.05], [0.12, -2.12, 0], 0xd1a85d, {
metalness: 0.56
});
// ------------------------------------------------------------
// Flexible voxel diaphragm
// ------------------------------------------------------------
const membraneCells = [];
const membraneStep = 0.23;
for (let iy = -5; iy <= 5; iy++) {
for (let iz = -5; iz <= 5; iz++) {
const y = iy * membraneStep;
const z = iz * membraneStep;
const radius = Math.hypot(y, z);
if (radius <= 1.2) {
const centerMix = 1 - radius / 1.25;
const color = new THREE.Color(0xe39a43).lerp(
new THREE.Color(0xf05a4f),
centerMix * 0.8
);
membraneCells.push({
x: -0.34,
y,
z,
radius,
sx: 0.12,
sy: 0.21,
sz: 0.21,
color
});
}
}
}
const membraneMaterial = new THREE.MeshStandardMaterial({
color: 0xffffff,
metalness: 0.24,
roughness: 0.46,
emissive: 0x391006,
emissiveIntensity: 0.18
});
const membraneMesh = addInstancedBoxes(
receiver,
membraneCells,
membraneMaterial
);
membraneMesh.instanceMatrix.setUsage(THREE.DynamicDrawUsage);
membraneMesh.frustumCulled = false;
// ------------------------------------------------------------
// Illustrative traveling sound wave
// ------------------------------------------------------------
const waveCount = 64;
const waveCells = [];
const waveStart = -5.5;
const waveEnd = -1.02;
for (let i = 0; i < waveCount; i++) {
const t = i / (waveCount - 1);
const x = THREE.MathUtils.lerp(waveStart, waveEnd, t);
waveCells.push({
x,
y: 0.65,
z: 0,
phase: (x - waveStart) * 3.25,
sx: 0.105,
sy: 0.105,
sz: 0.105,
color: new THREE.Color(0x62e8ff).lerp(
new THREE.Color(0xb9f7ff),
(Math.sin(t * Math.PI) * 0.5 + 0.5) * 0.55
)
});
}
const waveMaterial = new THREE.MeshBasicMaterial({
color: 0xffffff,
transparent: true,
opacity: 0.84
});
const waveMesh = addInstancedBoxes(
scene,
waveCells,
waveMaterial,
false
);
waveMesh.instanceMatrix.setUsage(THREE.DynamicDrawUsage);
waveMesh.frustumCulled = false;
// Small voxel direction marker.
addBox(scene, [0.18, 0.08, 0.08], [-5.55, 1.34, 0], 0x61e7ff);
addBox(scene, [0.18, 0.08, 0.08], [-5.38, 1.46, 0], 0x61e7ff);
addBox(scene, [0.18, 0.08, 0.08], [-5.38, 1.22, 0], 0x61e7ff);
// ------------------------------------------------------------
// Separate electrical mechanism
// ------------------------------------------------------------
const mechanism = new THREE.Group();
mechanism.position.set(3.45, 0, 0);
scene.add(mechanism);
// Base and outlined electrical section.
addBox(
mechanism,
[3.35, 0.16, 2.55],
[0.25, -1.12, 0],
0x132a35,
{ metalness: 0.35, roughness: 0.62 }
);
const outlinePoints = [
new THREE.Vector3(-1.38, -1.025, -1.18),
new THREE.Vector3(1.88, -1.025, -1.18),
new THREE.Vector3(1.88, -1.025, 1.18),
new THREE.Vector3(-1.38, -1.025, 1.18),
new THREE.Vector3(-1.38, -1.025, -1.18)
];
const outline = new THREE.Line(
new THREE.BufferGeometry().setFromPoints(outlinePoints),
new THREE.LineDashedMaterial({
color: 0xff9b57,
dashSize: 0.12,
gapSize: 0.09,
transparent: true,
opacity: 0.55
})
);
outline.computeLineDistances();
mechanism.add(outline);
// Horseshoe magnet, represented by blue/red pole blocks.
addBox(
mechanism,
[0.34, 1.65, 0.60],
[-0.72, 0.44, 0],
0x287fc1,
{ metalness: 0.42, roughness: 0.42 }
);
addBox(
mechanism,
[0.34, 1.65, 0.60],
[0.72, 0.44, 0],
0xd84b46,
{ metalness: 0.42, roughness: 0.42 }
);
addBox(
mechanism,
[0.86, 0.34, 0.60],
[-0.47, 1.37, 0],
0x287fc1,
{ metalness: 0.42, roughness: 0.42 }
);
addBox(
mechanism,
[0.86, 0.34, 0.60],
[0.47, 1.37, 0],
0xd84b46,
{ metalness: 0.42, roughness: 0.42 }
);
// Pole shoes facing the coil.
addBox(
mechanism,
[0.42, 0.28, 0.68],
[-0.54, -0.25, 0],
0x3aa1df,
{ metalness: 0.58 }
);
addBox(
mechanism,
[0.42, 0.28, 0.68],
[0.54, -0.25, 0],
0xe15c52,
{ metalness: 0.58 }
);
// Copper coil wound around the Z axis.
const coilCells = [];
const coilTurns = 7;
const coilSteps = 112;
for (let i = 0; i < coilSteps; i++) {
const t = i / (coilSteps - 1);
const angle = t * coilTurns * Math.PI * 2;
const radius = 0.42;
coilCells.push({
x: Math.cos(angle) * radius,
y: 0.31 + Math.sin(angle) * radius,
z: -0.39 + t * 0.78,
sx: 0.115,
sy: 0.115,
sz: 0.115,
color: new THREE.Color().setHSL(
0.075,
0.68,
0.43 + random() * 0.09
)
});
}
const coilMaterial = new THREE.MeshStandardMaterial({
color: 0xffffff,
metalness: 0.72,
roughness: 0.34,
emissive: 0x5a1d05,
emissiveIntensity: 0.08
});
addInstancedBoxes(mechanism, coilCells, coilMaterial);
// Iron core.
addBox(
mechanism,
[0.22, 0.22, 0.96],
[0, 0.31, 0],
0x78909a,
{ metalness: 0.78, roughness: 0.32 }
);
// Moving armature and contact.
const armature = new THREE.Group();
armature.position.set(0, 1.05, 0);
mechanism.add(armature);
addBox(
armature,
[1.03, 0.12, 0.29],
[0, 0, 0],
0xaebdc2,
{ metalness: 0.8, roughness: 0.27 }
);
addBox(
armature,
[0.18, 0.20, 0.18],
[0.49, 0.12, 0],
0xd6dedc,
{ metalness: 0.82, roughness: 0.24 }
);
const contactMaterial = new THREE.MeshStandardMaterial({
color: 0xffc15a,
metalness: 0.68,
roughness: 0.3,
emissive: 0xff5a1f,
emissiveIntensity: 0.35
});
addBox(
mechanism,
[0.16, 0.16, 0.18],
[0.64, 1.18, 0],
0xffffff,
{ material: contactMaterial }
);
// Battery.
addBox(
mechanism,
[0.82, 0.48, 0.62],
[1.55, -0.76, 0.95],
0xd7c69d,
{ metalness: 0.16, roughness: 0.72 }
);
addBox(
mechanism,
[0.19, 0.10, 0.19],
[1.39, -0.46, 0.95],
0xd84b46,
{ metalness: 0.7 }
);
addBox(
mechanism,
[0.19, 0.10, 0.19],
[1.71, -0.46, 0.95],
0x287fc1,
{ metalness: 0.7 }
);
// Voxel circuit paths.
addVoxelPath(
mechanism,
[
[0, -0.05, 0.18],
[0, -0.66, 0.56],
[1.39, -0.67, 0.95]
],
0xc87535,
0.095
);
addVoxelPath(
mechanism,
[
[1.71, -0.64, 0.95],
[1.71, 0.60, 0.86],
[1.22, 1.12, 0.32],
[0.68, 1.18, 0.08]
],
0x287fc1,
0.095
);
// ------------------------------------------------------------
// DOM labels and projected leader lines
// ------------------------------------------------------------
const labelsLayer = document.querySelector("#labels");
const leaders = document.querySelector("#leaders");
const labels = [];
function addLabel(text, anchor, offset, kind = "") {
const element = document.createElement("div");
element.className = `tag ${kind}`.trim();
element.textContent = text;
labelsLayer.appendChild(element);
const line = document.createElementNS(
"http://www.w3.org/2000/svg",
"line"
);
const dot = document.createElementNS(
"http://www.w3.org/2000/svg",
"circle"
);
dot.setAttribute("r", "2.5");
const color =
kind === "acoustic"
? "#61e7ff"
: kind === "electrical"
? "#ff9b57"
: "#d8c38e";
line.setAttribute("stroke", color);
dot.setAttribute("fill", color);
dot.setAttribute("stroke", color);
leaders.appendChild(line);
leaders.appendChild(dot);
labels.push({
text,
element,
line,
dot,
anchor: anchor.clone(),
offset
});
}
addLabel(
"Acoustic / mechanical",
new THREE.Vector3(-3.0, 2.1, 0),
new THREE.Vector2(0, -48),
"acoustic section"
);
addLabel(
"Illustrative sound wave",
new THREE.Vector3(-3.15, 1.35, 0),
new THREE.Vector2(-15, -65),
"acoustic"
);
addLabel(
"Receiver housing",
receiver.localToWorld(new THREE.Vector3(0.42, 1.82, 0.7)),
new THREE.Vector2(108, -42)
);
addLabel(
"Vibrating diaphragm",
receiver.localToWorld(new THREE.Vector3(-0.34, 0.25, 1.0)),
new THREE.Vector2(105, 55),
"acoustic"
);
addLabel(
"Separate electrical mechanism",
mechanism.localToWorld(new THREE.Vector3(0.3, 1.88, 0)),
new THREE.Vector2(35, -42),
"electrical section"
);
addLabel(
"Permanent magnet",
mechanism.localToWorld(new THREE.Vector3(-0.72, 0.66, 0.32)),
new THREE.Vector2(-92, 25),
"electrical"
);
addLabel(
"Electromagnet coil",
mechanism.localToWorld(new THREE.Vector3(0, 0.28, 0.42)),
new THREE.Vector2(102, 45),
"electrical"
);
addLabel(
"Armature / contact",
mechanism.localToWorld(new THREE.Vector3(0.18, 1.08, 0.12)),
new THREE.Vector2(90, -35),
"electrical"
);
addLabel(
"Battery / circuit",
mechanism.localToWorld(new THREE.Vector3(1.55, -0.72, 0.95)),
new THREE.Vector2(70, 48),
"electrical"
);
const projected = new THREE.Vector3();
function updateLabels() {
const width = window.innerWidth;
const height = window.innerHeight;
for (const label of labels) {
projected.copy(label.anchor).project(camera);
const visible =
projected.z > -1 &&
projected.z < 1 &&
projected.x > -1.35 &&
projected.x < 1.35 &&
projected.y > -1.35 &&
projected.y < 1.35;
label.element.style.display = visible ? "block" : "none";
label.line.style.display = visible ? "block" : "none";
label.dot.style.display = visible ? "block" : "none";
if (!visible) continue;
const anchorX = (projected.x * 0.5 + 0.5) * width;
const anchorY = (-projected.y * 0.5 + 0.5) * height;
const targetX = THREE.MathUtils.clamp(
anchorX + label.offset.x,
100,
width - 100
);
const targetY = THREE.MathUtils.clamp(
anchorY + label.offset.y,
95,
height - 65
);
label.element.style.left = `${targetX}px`;
label.element.style.top = `${targetY}px`;
const rect = label.element.getBoundingClientRect();
let endX = rect.left + rect.width / 2;
let endY = rect.top + rect.height / 2;
if (Math.abs(endX - anchorX) > Math.abs(endY - anchorY)) {
endX = endX > anchorX ? rect.left : rect.right;
endY = THREE.MathUtils.clamp(
anchorY,
rect.top,
rect.bottom
);
} else {
endY = endY > anchorY ? rect.top : rect.bottom;
endX = THREE.MathUtils.clamp(
anchorX,
rect.left,
rect.right
);
}
label.line.setAttribute("x1", anchorX);
label.line.setAttribute("y1", anchorY);
label.line.setAttribute("x2", endX);
label.line.setAttribute("y2", endY);
label.dot.setAttribute("cx", anchorX);
label.dot.setAttribute("cy", anchorY);
}
}
// ------------------------------------------------------------
// Animation
// ------------------------------------------------------------
const membraneDummy = new THREE.Object3D();
const waveDummy = new THREE.Object3D();
let simulationTime = 0;
let paused = false;
let labelsVisible = true;
let previousTime = performance.now();
function updateMembrane(time) {
membraneCells.forEach((cell, index) => {
const normalizedRadius = cell.radius / 1.25;
const centerWeight = 0.42 + (1 - normalizedRadius) * 0.58;
const oscillation = Math.sin(
time * 3.2 - cell.radius * 2.35
);
membraneDummy.position.set(
cell.x + oscillation * 0.15 * centerWeight,
cell.y,
cell.z
);
const localScale = 1 + oscillation * 0.018 * centerWeight;
membraneDummy.scale.set(
cell.sx,
cell.sy * localScale,
cell.sz * localScale
);
membraneDummy.rotation.set(0, 0, 0);
membraneDummy.updateMatrix();
membraneMesh.setMatrixAt(index, membraneDummy.matrix);
});
membraneMesh.instanceMatrix.needsUpdate = true;
}
function updateWave(time) {
waveCells.forEach((cell, index) => {
const phase = cell.phase - time * 3.1;
const vertical = Math.sin(phase);
const crest = Math.max(0, Math.cos(phase));
const size = 0.085 + crest * 0.045;
waveDummy.position.set(
cell.x,
0.65 + vertical * 0.52,
cell.z
);
waveDummy.scale.set(size, size, size);
waveDummy.rotation.set(0, 0, 0);
waveDummy.updateMatrix();
waveMesh.setMatrixAt(index, waveDummy.matrix);
});
waveMesh.instanceMatrix.needsUpdate = true;
}
function animate(now) {
const delta = Math.min((now - previousTime) / 1000, 0.05);
previousTime = now;
if (!paused) {
simulationTime += delta;
}
updateMembrane(simulationTime);
updateWave(simulationTime);
const pulse = 0.5 + 0.5 * Math.sin(simulationTime * 3.2);
armature.position.y = 1.05 + pulse * 0.055;
coilMaterial.emissiveIntensity = 0.06 + pulse * 0.13;
contactMaterial.emissiveIntensity = 0.22 + pulse * 0.65;
electricalLight.intensity = 8 + pulse * 8;
cyanLight.intensity = 15 + Math.sin(simulationTime * 1.7) * 3;
applyCamera();
updateLabels();
renderer.render(scene, camera);
requestAnimationFrame(animate);
}
requestAnimationFrame(animate);
// ------------------------------------------------------------
// Direct camera controls: orbit, wheel zoom, and pinch zoom
// ------------------------------------------------------------
const pointers = new Map();
renderer.domElement.addEventListener("pointerdown", (event) => {
renderer.domElement.setPointerCapture(event.pointerId);
pointers.set(event.pointerId, {
x: event.clientX,
y: event.clientY
});
});
renderer.domElement.addEventListener("pointermove", (event) => {
if (!pointers.has(event.pointerId)) return;
const previous = pointers.get(event.pointerId);
if (pointers.size === 1) {
const dx = event.clientX - previous.x;
const dy = event.clientY - previous.y;
view.azimuth -= dx * 0.006;
view.polar = THREE.MathUtils.clamp(
view.polar - dy * 0.005,
0.55,
1.48
);
} else if (pointers.size === 2) {
const ids = [...pointers.keys()];
const a = pointers.get(ids[0]);
const b = pointers.get(ids[1]);
const oldDistance = Math.hypot(
b.x - a.x,
b.y - a.y
);
pointers.set(event.pointerId, {
x: event.clientX,
y: event.clientY
});
const nextA = pointers.get(ids[0]);
const nextB = pointers.get(ids[1]);
const newDistance = Math.hypot(
nextB.x - nextA.x,
nextB.y - nextA.y
);
if (oldDistance > 0 && newDistance > 0) {
view.radius = THREE.MathUtils.clamp(
view.radius * (oldDistance / newDistance),
9,
22
);
}
}
});
function removePointer(event) {
pointers.delete(event.pointerId);
}
renderer.domElement.addEventListener("pointerup", removePointer);
renderer.domElement.addEventListener("pointercancel", removePointer);
renderer.domElement.addEventListener("lostpointercapture", removePointer);
renderer.domElement.addEventListener(
"wheel",
(event) => {
event.preventDefault();
view.radius = THREE.MathUtils.clamp(
view.radius + event.deltaY * 0.01,
9,
22
);
},
{ passive: false }
);
renderer.domElement.addEventListener("dblclick", resetView);
// ------------------------------------------------------------
// UI controls
// ------------------------------------------------------------
const pauseButton = document.querySelector("#pause");
const labelsButton = document.querySelector("#toggle-labels");
const resetButton = document.querySelector("#reset");
const motionState = document.querySelector("#motion-state");
function resetView() {
view.azimuth = initialView.azimuth;
view.polar = initialView.polar;
view.radius = initialView.radius;
applyCamera();
}
pauseButton.addEventListener("click", () => {
paused = !paused;
pauseButton.textContent = paused ? "Resume motion" : "Pause motion";
pauseButton.setAttribute("aria-pressed", String(paused));
motionState.textContent = paused
? "Animation paused"
: "Animation running";
});
labelsButton.addEventListener("click", () => {
labelsVisible = !labelsVisible;
labelsLayer.classList.toggle("off", !labelsVisible);
labelsButton.textContent = labelsVisible
? "Hide labels"
: "Show labels";
labelsButton.setAttribute(
"aria-pressed",
String(labelsVisible)
);
});
resetButton.addEventListener("click", resetView);
window.addEventListener("keydown", (event) => {
if (event.code === "Space") {
event.preventDefault();
pauseButton.click();
}
if (event.key.toLowerCase() === "r") {
resetView();
}
});
window.addEventListener("resize", () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setPixelRatio(
Math.min(window.devicePixelRatio, 2)
);
renderer.setSize(
window.innerWidth,
window.innerHeight
);
leaders.setAttribute(
"viewBox",
`0 0 ${window.innerWidth} ${window.innerHeight}`
);
});
leaders.setAttribute(
"viewBox",
`0 0 ${window.innerWidth} ${window.innerHeight}`
);
</script>
</body>
</html>
```