Prompt specification
Build or customize the Three.js 3D scene "Pendulum length comparison frame" 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: Pendulum length comparison frame
Category: Science & Physics · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/pendulum-length-comparison-frame-x1413
Scene Description Prompt:
Create a single self-contained HTML file using Three.js from cdnjs that shows pendulums and their oscillation periods. Hang two small-angle pendulums of different lengths from one support and show their periods under the ideal formula; keep initial angles small and identical. Low-poly.
Working Three.js Starter (pendulum-length-comparison-frame-x1413):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Pendulum Period Lab</title>
<style>
:root {
color-scheme: dark;
--bg: #07111b;
--panel: rgba(12, 26, 39, 0.82);
--line: rgba(173, 216, 235, 0.16);
--text: #eef8ff;
--muted: #8fa8b8;
--cyan: #58e2d1;
--amber: #ffb85c;
}
* {
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;
}
body {
color: var(--text);
}
canvas {
position: fixed;
inset: 0;
display: block;
width: 100%;
height: 100%;
touch-action: none;
cursor: grab;
}
canvas:active {
cursor: grabbing;
}
.vignette {
position: fixed;
inset: 0;
z-index: 1;
pointer-events: none;
background:
radial-gradient(circle at 50% 44%, transparent 28%, rgba(1, 7, 12, 0.18) 70%),
linear-gradient(to bottom, rgba(3, 10, 16, 0.2), transparent 28%);
}
.topbar {
position: fixed;
z-index: 4;
top: 22px;
left: 24px;
right: 24px;
display: flex;
align-items: flex-start;
justify-content: space-between;
gap: 20px;
pointer-events: none;
}
.brand {
display: flex;
align-items: flex-start;
gap: 13px;
}
.brand-mark {
position: relative;
width: 38px;
height: 38px;
flex: 0 0 38px;
border: 1px solid rgba(88, 226, 209, 0.34);
border-radius: 11px;
background: rgba(12, 29, 42, 0.74);
box-shadow: inset 0 0 20px rgba(88, 226, 209, 0.06);
}
.brand-mark::before {
content: "";
position: absolute;
left: 18px;
top: 8px;
width: 2px;
height: 19px;
background: var(--cyan);
transform: rotate(19deg);
transform-origin: top;
border-radius: 2px;
}
.brand-mark::after {
content: "";
position: absolute;
left: 12px;
bottom: 6px;
width: 8px;
height: 8px;
border-radius: 50%;
background: var(--amber);
box-shadow: 0 0 10px rgba(255, 184, 92, 0.45);
}
.eyebrow {
margin: 0 0 5px;
color: var(--cyan);
font-size: 10px;
font-weight: 750;
letter-spacing: 0.19em;
text-transform: uppercase;
}
h1 {
margin: 0;
font-size: clamp(22px, 2.3vw, 34px);
font-weight: 660;
letter-spacing: -0.035em;
line-height: 1.05;
}
.subtitle {
margin: 8px 0 0;
color: var(--muted);
font-size: 13px;
line-height: 1.45;
}
.formula-panel {
width: 285px;
padding: 16px 18px;
border: 1px solid var(--line);
border-radius: 16px;
background: var(--panel);
box-shadow: 0 18px 60px rgba(0, 0, 0, 0.2);
backdrop-filter: blur(16px);
}
.formula-panel .eyebrow {
color: #9eb6c4;
font-size: 9px;
}
.formula {
margin-top: 7px;
font-family: Georgia, "Times New Roman", serif;
font-size: 30px;
letter-spacing: 0.01em;
white-space: nowrap;
}
.formula .fraction {
display: inline-flex;
flex-direction: column;
align-items: center;
vertical-align: middle;
margin-left: 3px;
font-size: 18px;
line-height: 0.9;
}
.formula .fraction span:first-child {
width: 100%;
text-align: center;
border-bottom: 1px solid currentColor;
padding-bottom: 2px;
}
.formula-meta {
display: flex;
justify-content: space-between;
gap: 12px;
margin-top: 10px;
color: var(--muted);
font-size: 10px;
}
.formula-meta strong {
color: #d8e9f2;
font-weight: 600;
}
.world-label {
position: fixed;
z-index: 2;
pointer-events: none;
transform: translate(-50%, -50%);
white-space: nowrap;
opacity: 0;
transition: opacity 150ms ease;
}
.world-label.visible {
opacity: 1;
}
.length-label {
padding: 5px 8px;
border: 1px solid rgba(167, 207, 225, 0.16);
border-radius: 7px;
background: rgba(5, 15, 24, 0.74);
color: #c5d9e4;
font-size: 10px;
font-weight: 650;
letter-spacing: 0.04em;
backdrop-filter: blur(8px);
}
.support-label {
color: rgba(201, 220, 230, 0.62);
font-size: 9px;
font-weight: 750;
letter-spacing: 0.18em;
text-transform: uppercase;
}
.bottom-wrap {
position: fixed;
z-index: 5;
left: 50%;
bottom: 20px;
width: min(960px, calc(100vw - 28px));
transform: translateX(-50%);
}
.deck {
display: grid;
grid-template-columns: 175px 1fr 1fr 175px;
gap: 10px;
padding: 11px;
border: 1px solid var(--line);
border-radius: 20px;
background: rgba(8, 21, 32, 0.86);
box-shadow: 0 22px 70px rgba(0, 0, 0, 0.34);
backdrop-filter: blur(20px);
}
.status,
.controls {
padding: 8px 10px;
border-radius: 13px;
background: rgba(255, 255, 255, 0.025);
}
.status {
display: flex;
flex-direction: column;
justify-content: center;
}
.live-row {
display: flex;
align-items: center;
gap: 7px;
color: #dcecf4;
font-size: 9px;
font-weight: 750;
letter-spacing: 0.13em;
text-transform: uppercase;
}
.live-dot {
width: 7px;
height: 7px;
border-radius: 50%;
background: var(--cyan);
box-shadow: 0 0 12px rgba(88, 226, 209, 0.7);
}
.live-dot.paused {
background: var(--amber);
box-shadow: 0 0 12px rgba(255, 184, 92, 0.55);
}
.release-angle {
margin-top: 8px;
color: var(--muted);
font-size: 10px;
line-height: 1.45;
}
.release-angle strong {
color: #e5f3fa;
font-weight: 650;
}
.sim-time {
margin-top: 9px;
color: #8fb8c9;
font-variant-numeric: tabular-nums;
font-size: 11px;
}
.pendulum-card {
--accent: var(--cyan);
position: relative;
overflow: hidden;
min-width: 0;
padding: 11px 13px 9px;
border: 1px solid rgba(255, 255, 255, 0.075);
border-radius: 13px;
background:
linear-gradient(135deg, color-mix(in srgb, var(--accent) 9%, transparent), transparent 55%),
rgba(255, 255, 255, 0.025);
}
.pendulum-card::before {
content: "";
position: absolute;
top: 0;
left: 0;
width: 3px;
height: 100%;
background: var(--accent);
opacity: 0.8;
}
.card-head {
display: flex;
align-items: center;
justify-content: space-between;
gap: 8px;
color: var(--muted);
font-size: 9px;
font-weight: 750;
letter-spacing: 0.12em;
text-transform: uppercase;
}
.card-name {
display: flex;
align-items: center;
gap: 7px;
}
.swatch {
width: 7px;
height: 7px;
border-radius: 50%;
background: var(--accent);
box-shadow: 0 0 10px color-mix(in srgb, var(--accent) 55%, transparent);
}
.length {
color: #9fb7c5;
font-variant-numeric: tabular-nums;
letter-spacing: 0.04em;
}
.period-row {
display: flex;
align-items: baseline;
gap: 8px;
margin-top: 2px;
}
.period-value {
color: var(--accent);
font-size: 31px;
font-weight: 640;
font-variant-numeric: tabular-nums;
letter-spacing: -0.055em;
line-height: 1.15;
}
.period-unit {
color: #9fb7c5;
font-size: 12px;
}
.frequency {
color: #a9bfcc;
font-size: 10px;
font-variant-numeric: tabular-nums;
}
.wave {
display: block;
width: 100%;
height: 31px;
margin-top: 1px;
overflow: visible;
}
.wave .guide {
stroke: rgba(174, 208, 224, 0.12);
stroke-width: 1;
}
.wave .path {
fill: none;
stroke: var(--accent);
stroke-width: 1.6;
opacity: 0.72;
}
.wave .marker {
fill: var(--accent);
filter: drop-shadow(0 0 4px var(--accent));
}
.card-foot {
display: flex;
justify-content: space-between;
gap: 8px;
color: #7693a4;
font-size: 9px;
font-variant-numeric: tabular-nums;
}
.controls {
display: flex;
flex-direction: column;
justify-content: center;
gap: 8px;
}
.button-row {
display: flex;
gap: 6px;
}
button {
flex: 1;
min-width: 0;
padding: 8px 7px;
border: 1px solid rgba(171, 212, 230, 0.16);
border-radius: 8px;
background: rgba(155, 205, 226, 0.08);
color: #d9edf5;
font: inherit;
font-size: 10px;
font-weight: 650;
cursor: pointer;
transition: background 140ms ease, transform 140ms ease;
}
button:hover {
background: rgba(155, 205, 226, 0.15);
}
button:active {
transform: translateY(1px);
}
.speed-label {
display: flex;
justify-content: space-between;
color: #89a5b5;
font-size: 9px;
letter-spacing: 0.06em;
text-transform: uppercase;
}
input[type="range"] {
width: 100%;
height: 4px;
margin: 5px 0 0;
accent-color: var(--cyan);
cursor: pointer;
}
.camera-hint {
margin-top: 12px;
color: rgba(148, 174, 187, 0.55);
text-align: center;
font-size: 9px;
letter-spacing: 0.05em;
}
@media (max-width: 820px) {
.deck {
grid-template-columns: 1fr 1fr;
}
.status,
.controls {
min-height: 65px;
}
.status {
grid-column: 1;
}
.controls {
grid-column: 2;
}
}
@media (max-width: 600px) {
.topbar {
top: 14px;
left: 14px;
right: 14px;
}
.brand-mark {
width: 33px;
height: 33px;
flex-basis: 33px;
}
.brand-mark::before {
left: 16px;
top: 7px;
height: 17px;
}
.brand-mark::after {
left: 10px;
bottom: 5px;
}
h1 {
font-size: 21px;
}
.subtitle {
display: none;
}
.formula-panel {
position: fixed;
top: 75px;
right: 14px;
width: 218px;
padding: 11px 13px;
}
.formula {
font-size: 24px;
}
.formula-meta {
font-size: 9px;
}
.ratio-line {
display: none;
}
.bottom-wrap {
bottom: 10px;
width: calc(100vw - 20px);
}
.deck {
gap: 7px;
padding: 8px;
border-radius: 15px;
}
.pendulum-card {
padding: 9px 10px 8px;
}
.period-value {
font-size: 26px;
}
.wave {
height: 25px;
}
.status,
.controls {
padding: 7px 8px;
}
.release-angle {
font-size: 9px;
}
.camera-hint {
display: none;
}
.length-label {
font-size: 8px;
padding: 4px 6px;
}
}
</style>
</head>
<body>
<div class="vignette"></div>
<header class="topbar">
<div class="brand">
<div class="brand-mark" aria-hidden="true"></div>
<div>
<p class="eyebrow">Physics / Oscillation</p>
<h1>Pendulum Period Lab</h1>
<p class="subtitle">
Same release angle. Different lengths. Different natural rhythms.
</p>
</div>
</div>
<aside class="formula-panel" aria-label="Ideal pendulum period formula">
<p class="eyebrow">Ideal small-angle model</p>
<div class="formula">
T = 2π√<span class="fraction"><span>L</span><span>g</span></span>
</div>
<div class="formula-meta">
<span><strong>g</strong> = 9.81 m/s²</span>
<span class="ratio-line" id="ratioLine"></span>
</div>
</aside>
</header>
<div id="lengthLabel1" class="world-label length-label">L₁ = 1.60 m</div>
<div id="lengthLabel2" class="world-label length-label">L₂ = 2.40 m</div>
<div id="supportLabel" class="world-label support-label">One common support</div>
<section class="bottom-wrap" aria-label="Pendulum measurements and controls">
<div class="deck">
<div class="status">
<div class="live-row">
<span id="liveDot" class="live-dot"></span>
<span id="modelStatus">Live model</span>
</div>
<div class="release-angle">
Both released at<br />
<strong>θ₀ = +8.0°</strong>
</div>
<div class="sim-time" id="simTime">t = 0.00 s</div>
</div>
<article class="pendulum-card" style="--accent: var(--cyan)">
<div class="card-head">
<span class="card-name"><span class="swatch"></span>P₁ / short</span>
<span class="length">L = 1.60 m</span>
</div>
<div class="period-row">
<span class="period-value" id="period1">2.54</span>
<span class="period-unit">seconds / period</span>
</div>
<div class="frequency">Frequency = <span id="frequency1">0.394</span> Hz</div>
<svg class="wave" viewBox="0 0 180 30" preserveAspectRatio="none" aria-hidden="true">
<line class="guide" x1="0" y1="15" x2="180" y2="15"></line>
<path class="path" d="M0 4 C45 4 45 26 90 26 S135 4 180 4"></path>
<circle id="marker1" class="marker" cx="0" cy="4" r="3"></circle>
</svg>
<div class="card-foot">
<span id="phase1">phase 0.00</span>
<span id="cycle1">cycle 0</span>
</div>
</article>
<article class="pendulum-card" style="--accent: var(--amber)">
<div class="card-head">
<span class="card-name"><span class="swatch"></span>P₂ / long</span>
<span class="length">L = 2.40 m</span>
</div>
<div class="period-row">
<span class="period-value" id="period2">3.10</span>
<span class="period-unit">seconds / period</span>
</div>
<div class="frequency">Frequency = <span id="frequency2">0.322</span> Hz</div>
<svg class="wave" viewBox="0 0 180 30" preserveAspectRatio="none" aria-hidden="true">
<line class="guide" x1="0" y1="15" x2="180" y2="15"></line>
<path class="path" d="M0 4 C45 4 45 26 90 26 S135 4 180 4"></path>
<circle id="marker2" class="marker" cx="0" cy="4" r="3"></circle>
</svg>
<div class="card-foot">
<span id="phase2">phase 0.00</span>
<span id="cycle2">cycle 0</span>
</div>
</article>
<div class="controls">
<div class="button-row">
<button id="pauseBtn" type="button" aria-pressed="false">Pause</button>
<button id="resetBtn" type="button">Reset</button>
</div>
<div>
<div class="speed-label">
<span>Time speed</span>
<span id="speedValue">1.00×</span>
</div>
<input
id="speedInput"
type="range"
min="0.25"
max="1.5"
step="0.25"
value="1"
aria-label="Simulation speed"
/>
</div>
</div>
</div>
<div class="camera-hint">Drag to orbit · Scroll to zoom · Double-click to reset the view</div>
</section>
<script type="module">
import * as THREE from "https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
const $ = (id) => document.getElementById(id);
const GRAVITY = 9.81;
const INITIAL_ANGLE = THREE.MathUtils.degToRad(8);
const PIVOT_Y = 4.08;
const pendulums = [
{
id: 1,
length: 1.60,
pivotX: -1.15,
color: 0x58e2d1,
cssColor: "#58e2d1"
},
{
id: 2,
length: 2.40,
pivotX: 1.15,
color: 0xffb85c,
cssColor: "#ffb85c"
}
];
for (const p of pendulums) {
p.period = 2 * Math.PI * Math.sqrt(p.length / GRAVITY);
p.frequency = 1 / p.period;
$(`period${p.id}`).textContent = p.period.toFixed(2);
$(`frequency${p.id}`).textContent = p.frequency.toFixed(3);
}
$("ratioLine").textContent =
`T₂/T₁ = ${(pendulums[1].period / pendulums[0].period).toFixed(2)}`;
// Scene ------------------------------------------------------------------
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x07111b);
scene.fog = new THREE.Fog(0x07111b, 13, 25);
const camera = new THREE.PerspectiveCamera(42, 1, 0.1, 60);
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.08;
renderer.domElement.setAttribute("aria-label", "Interactive three-dimensional pendulum simulation");
document.body.prepend(renderer.domElement);
const hemisphere = new THREE.HemisphereLight(0xa8ddff, 0x071019, 1.75);
scene.add(hemisphere);
const keyLight = new THREE.DirectionalLight(0xfff2dc, 3.2);
keyLight.position.set(4.5, 8.5, 5.5);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(2048, 2048);
keyLight.shadow.camera.left = -7;
keyLight.shadow.camera.right = 7;
keyLight.shadow.camera.top = 8;
keyLight.shadow.camera.bottom = -4;
keyLight.shadow.camera.near = 0.5;
keyLight.shadow.camera.far = 25;
keyLight.shadow.bias = -0.00025;
scene.add(keyLight);
const rimLight = new THREE.DirectionalLight(0x6bbcff, 1.4);
rimLight.position.set(-5, 4, -4);
scene.add(rimLight);
const warmFill = new THREE.PointLight(0xffa953, 14, 11, 2);
warmFill.position.set(4.5, 2.5, -2.5);
scene.add(warmFill);
const coolFill = new THREE.PointLight(0x4ce4d2, 12, 10, 2);
coolFill.position.set(-4, 3, -2);
scene.add(coolFill);
// Materials --------------------------------------------------------------
const supportMaterial = new THREE.MeshStandardMaterial({
color: 0x263b49,
roughness: 0.38,
metalness: 0.62,
flatShading: true
});
const darkMetalMaterial = new THREE.MeshStandardMaterial({
color: 0x142630,
roughness: 0.42,
metalness: 0.68,
flatShading: true
});
const boltMaterial = new THREE.MeshStandardMaterial({
color: 0x91a7b2,
roughness: 0.3,
metalness: 0.85,
flatShading: true
});
const floorMaterial = new THREE.MeshStandardMaterial({
color: 0x0a1a25,
roughness: 0.9,
metalness: 0.08,
flatShading: true
});
function mesh(geometry, material, x = 0, y = 0, z = 0, cast = true, receive = true) {
const object = new THREE.Mesh(geometry, material);
object.position.set(x, y, z);
object.castShadow = cast;
object.receiveShadow = receive;
scene.add(object);
return object;
}
function cylinderBetween(start, end, radius, material, radialSegments = 6) {
const direction = new THREE.Vector3().subVectors(end, start);
const length = direction.length();
const midpoint = new THREE.Vector3()
.addVectors(start, end)
.multiplyScalar(0.5);
const object = new THREE.Mesh(
new THREE.CylinderGeometry(radius, radius, length, radialSegments),
material
);
object.position.copy(midpoint);
object.quaternion.setFromUnitVectors(
new THREE.Vector3(0, 1, 0),
direction.normalize()
);
object.castShadow = true;
object.receiveShadow = true;
scene.add(object);
return object;
}
// Low-poly platform and grid --------------------------------------------
const platform = mesh(
new THREE.CylinderGeometry(4.25, 4.42, 0.16, 16),
floorMaterial,
0,
0,
0,
false,
true
);
const grid = new THREE.GridHelper(14, 28, 0x315065, 0x193240);
grid.position.y = 0.086;
grid.material.transparent = true;
grid.material.opacity = 0.24;
scene.add(grid);
// Common support --------------------------------------------------------
const support = new THREE.Group();
scene.add(support);
function addSupportBox(width, height, depth, x, y, z = 0, material = supportMaterial) {
const object = new THREE.Mesh(
new THREE.BoxGeometry(width, height, depth),
material
);
object.position.set(x, y, z);
object.castShadow = true;
object.receiveShadow = true;
support.add(object);
return object;
}
addSupportBox(4.7, 0.17, 0.38, 0, 4.35);
addSupportBox(0.18, 4.18, 0.25, -2.08, 2.17);
addSupportBox(0.18, 4.18, 0.25, 2.08, 2.17);
addSupportBox(0.72, 0.12, 0.72, -2.08, 0.15);
addSupportBox(0.72, 0.12, 0.72, 2.08, 0.15);
cylinderBetween(
new THREE.Vector3(-2.08, 0.3, -0.35),
new THREE.Vector3(-0.55, 4.22, -0.35),
0.065,
darkMetalMaterial
);
cylinderBetween(
new THREE.Vector3(2.08, 0.3, -0.35),
new THREE.Vector3(0.55, 4.22, -0.35),
0.065,
darkMetalMaterial
);
for (const x of [-2.08, 2.08]) {
const footBolt = new THREE.Mesh(
new THREE.CylinderGeometry(0.055, 0.055, 0.04, 10),
boltMaterial
);
footBolt.position.set(x, 0.225, 0.23);
footBolt.castShadow = true;
support.add(footBolt);
}
// Pendulums --------------------------------------------------------------
for (const p of pendulums) {
// Hanger and axle.
const hanger = new THREE.Mesh(
new THREE.CylinderGeometry(0.045, 0.045, 0.19, 8),
darkMetalMaterial
);
hanger.position.set(p.pivotX, 4.19, 0);
hanger.castShadow = true;
support.add(hanger);
const axle = new THREE.Mesh(
new THREE.CylinderGeometry(0.115, 0.115, 0.31, 12),
boltMaterial
);
axle.rotation.x = Math.PI / 2;
axle.position.set(p.pivotX, PIVOT_Y, 0);
axle.castShadow = true;
support.add(axle);
const axleCap = new THREE.Mesh(
new THREE.CylinderGeometry(0.065, 0.065, 0.34, 10),
darkMetalMaterial
);
axleCap.rotation.x = Math.PI / 2;
axleCap.position.set(p.pivotX, PIVOT_Y, 0);
axleCap.castShadow = true;
support.add(axleCap);
// Vertical reference line shows the selected length.
const referenceGeometry = new THREE.BufferGeometry().setFromPoints([
new THREE.Vector3(p.pivotX, PIVOT_Y, -0.04),
new THREE.Vector3(p.pivotX, PIVOT_Y - p.length, -0.04)
]);
const reference = new THREE.Line(
referenceGeometry,
new THREE.LineDashedMaterial({
color: 0x79909d,
dashSize: 0.08,
gapSize: 0.075,
transparent: true,
opacity: 0.32
})
);
reference.computeLineDistances();
scene.add(reference);
// Moving pendulum assembly.
p.group = new THREE.Group();
p.group.position.set(p.pivotX, PIVOT_Y, 0);
scene.add(p.group);
const rodMaterial = new THREE.MeshStandardMaterial({
color: 0xb8cbd3,
roughness: 0.28,
metalness: 0.76,
flatShading: true
});
const rod = new THREE.Mesh(
new THREE.CylinderGeometry(0.032, 0.043, p.length, 6),
rodMaterial
);
rod.position.y = -p.length / 2;
rod.castShadow = true;
rod.receiveShadow = true;
p.group.add(rod);
const bobMaterial = new THREE.MeshStandardMaterial({
color: p.color,
emissive: p.color,
emissiveIntensity: 0.15,
roughness: 0.3,
metalness: 0.45,
flatShading: true
});
p.bob = new THREE.Group();
p.bob.position.y = -p.length;
p.group.add(p.bob);
const weight = new THREE.Mesh(
new THREE.IcosahedronGeometry(0.235, 0),
bobMaterial
);
weight.castShadow = true;
weight.receiveShadow = true;
p.bob.add(weight);
const collar = new THREE.Mesh(
new THREE.CylinderGeometry(0.072, 0.09, 0.13, 8),
darkMetalMaterial
);
collar.position.y = 0.205;
collar.castShadow = true;
p.bob.add(collar);
const ring = new THREE.Mesh(
new THREE.TorusGeometry(0.285, 0.017, 6, 20),
bobMaterial
);
ring.position.z = 0.015;
ring.castShadow = true;
p.bob.add(ring);
const bobLight = new THREE.PointLight(p.color, 3.2, 2.1, 2);
p.bob.add(bobLight);
// A small live angle arc at each pivot.
p.arcPositions = new Float32Array(25 * 3);
p.arcGeometry = new THREE.BufferGeometry();
p.arcGeometry.setAttribute(
"position",
new THREE.BufferAttribute(p.arcPositions, 3)
);
p.arc = new THREE.Line(
p.arcGeometry,
new THREE.LineBasicMaterial({
color: p.color,
transparent: true,
opacity: 0.65
})
);
p.arc.position.set(p.pivotX, PIVOT_Y, 0.08);
scene.add(p.arc);
// Label anchor.
const side = p.id === 1 ? -1 : 1;
p.labelAnchor = new THREE.Vector3(
p.pivotX + side * 0.38,
PIVOT_Y - p.length * 0.52,
0.08
);
}
// Camera controls implemented directly without addons -------------------
const cameraState = {
yaw: 0.16,
pitch: 0.11,
radius: 9.7,
target: new THREE.Vector3(0, 2.28, 0)
};
let userZoomed = false;
let dragging = false;
let previousX = 0;
let previousY = 0;
function updateCamera() {
const { yaw, pitch, radius, target } = cameraState;
const horizontalRadius = Math.cos(pitch) * radius;
camera.position.set(
target.x + Math.sin(yaw) * horizontalRadius,
target.y + Math.sin(pitch) * radius,
target.z + Math.cos(yaw) * horizontalRadius
);
camera.lookAt(target);
}
function resetCamera() {
cameraState.yaw = 0.16;
cameraState.pitch = 0.11;
cameraState.radius = window.innerWidth < 700 ? 11.6 : 9.7;
userZoomed = false;
updateCamera();
}
const canvas = renderer.domElement;
canvas.addEventListener("pointerdown", (event) => {
dragging = true;
previousX = event.clientX;
previousY = event.clientY;
canvas.setPointerCapture(event.pointerId);
});
canvas.addEventListener("pointermove", (event) => {
if (!dragging) return;
const dx = event.clientX - previousX;
const dy = event.clientY - previousY;
previousX = event.clientX;
previousY = event.clientY;
cameraState.yaw -= dx * 0.005;
cameraState.pitch = THREE.MathUtils.clamp(
cameraState.pitch + dy * 0.0035,
-0.12,
0.62
);
});
function endDrag(event) {
dragging = false;
if (canvas.hasPointerCapture(event.pointerId)) {
canvas.releasePointerCapture(event.pointerId);
}
}
canvas.addEventListener("pointerup", endDrag);
canvas.addEventListener("pointercancel", endDrag);
canvas.addEventListener(
"wheel",
(event) => {
event.preventDefault();
cameraState.radius = THREE.MathUtils.clamp(
cameraState.radius * Math.exp(event.deltaY * 0.001),
6.2,
16
);
userZoomed = true;
},
{ passive: false }
);
canvas.addEventListener("dblclick", resetCamera);
// Labels are projected manually into screen space.
const projected = new THREE.Vector3();
function positionLabel(element, worldPosition) {
projected.copy(worldPosition).project(camera);
const visible = projected.z > -1 && projected.z < 1;
element.classList.toggle("visible", visible);
if (!visible) return;
const x = (projected.x * 0.5 + 0.5) * window.innerWidth;
const y = (-projected.y * 0.5 + 0.5) * window.innerHeight;
element.style.left = `${x}px`;
element.style.top = `${y}px`;
}
const supportAnchor = new THREE.Vector3(0, 4.53, 0.05);
// Simulation ------------------------------------------------------------
let simulationTime = 0;
let playing = true;
let speed = 1;
function updatePendulum(p) {
const omega = (2 * Math.PI) / p.period;
const theta = INITIAL_ANGLE * Math.cos(omega * simulationTime);
p.group.rotation.z = theta;
// Update the angle arc.
for (let i = 0; i < 25; i++) {
const angle = theta * (i / 24);
p.arcPositions[i * 3] = 0.43 * Math.sin(angle);
p.arcPositions[i * 3 + 1] = -0.43 * Math.cos(angle);
p.arcPositions[i * 3 + 2] = 0;
}
p.arcGeometry.attributes.position.needsUpdate = true;
const phase = ((simulationTime / p.period) % 1 + 1) % 1;
const markerX = phase * 180;
const markerY = 15 - 11 * Math.cos(phase * Math.PI * 2);
$(`marker${p.id}`).setAttribute("cx", markerX.toFixed(2));
$(`marker${p.id}`).setAttribute("cy", markerY.toFixed(2));
$(`phase${p.id}`).textContent = `phase ${phase.toFixed(2)}`;
$(`cycle${p.id}`).textContent =
`cycle ${Math.floor(simulationTime / p.period)}`;
}
function updateReadouts() {
$("simTime").textContent = `t = ${simulationTime.toFixed(2)} s`;
}
function setPlaying(nextPlaying) {
playing = nextPlaying;
$("pauseBtn").textContent = playing ? "Pause" : "Resume";
$("pauseBtn").setAttribute("aria-pressed", String(!playing));
$("modelStatus").textContent = playing ? "Live model" : "Paused";
$("liveDot").classList.toggle("paused", !playing);
}
$("pauseBtn").addEventListener("click", () => setPlaying(!playing));
$("resetBtn").addEventListener("click", () => {
simulationTime = 0;
updateReadouts();
});
$("speedInput").addEventListener("input", (event) => {
speed = Number(event.target.value);
$("speedValue").textContent = `${speed.toFixed(2)}×`;
});
window.addEventListener("keydown", (event) => {
const tag = document.activeElement?.tagName;
if (tag === "INPUT" || tag === "BUTTON") return;
if (event.code === "Space") {
event.preventDefault();
setPlaying(!playing);
}
if (event.key.toLowerCase() === "r") {
simulationTime = 0;
updateReadouts();
}
});
// Responsive camera and renderer.
function resize() {
const width = window.innerWidth;
const height = window.innerHeight;
renderer.setSize(width, height);
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
camera.aspect = width / height;
camera.fov = width < 700 ? 55 : 42;
camera.updateProjectionMatrix();
if (!userZoomed) {
cameraState.radius = width < 700 ? 11.6 : 9.7;
}
updateCamera();
}
window.addEventListener("resize", resize);
resize();
// Main loop.
const clock = new THREE.Clock();
function animate() {
requestAnimationFrame(animate);
const delta = Math.min(clock.getDelta(), 0.05);
if (playing) {
simulationTime += delta * speed;
}
for (const p of pendulums) {
updatePendulum(p);
}
updateReadouts();
updateCamera();
for (const p of pendulums) {
positionLabel($(`lengthLabel${p.id}`), p.labelAnchor);
}
positionLabel($("supportLabel"), supportAnchor);
renderer.render(scene, camera);
}
animate();
</script>
</body>
</html>
```