Prompt specification
Build or customize the Three.js 3D scene "Resonance sweep exploration rig" 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: Resonance sweep exploration rig
Category: Science & Physics · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/resonance-sweep-exploration-rig-x1580
Scene Description Prompt:
Explore a low-poly driven oscillator's frequency and damping, showing amplitude and phase while separating startup from steady response. Three.js from cdnjs, one HTML file.
Working Three.js Starter (resonance-sweep-exploration-rig-x1580):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Driven Oscillator Lab</title>
<style>
:root {
color-scheme: dark;
--bg: #080b11;
--panel: #10151e;
--panel-2: #151c27;
--line: #25303e;
--text: #edf3fb;
--muted: #8997aa;
--cyan: #50d9ff;
--cyan-soft: #1e91b5;
--amber: #ff9f52;
--white: #f5f8fc;
--green: #66e3a6;
--radius: 15px;
}
* {
box-sizing: border-box;
}
html,
body {
margin: 0;
width: 100%;
min-height: 100%;
background: var(--bg);
color: var(--text);
font-family:
Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
body {
overflow: hidden;
}
button,
input {
font: inherit;
}
button {
color: inherit;
}
.app {
height: 100vh;
display: flex;
flex-direction: column;
}
.header {
height: 64px;
flex: 0 0 64px;
display: flex;
align-items: center;
justify-content: space-between;
gap: 20px;
padding: 0 22px;
border-bottom: 1px solid var(--line);
background: rgba(10, 14, 21, 0.94);
position: relative;
z-index: 10;
}
.brand {
display: flex;
align-items: center;
gap: 12px;
min-width: 0;
}
.brand-mark {
width: 34px;
height: 34px;
border: 1px solid #315267;
border-radius: 10px;
display: grid;
place-items: center;
color: var(--cyan);
background:
linear-gradient(135deg, rgba(80, 217, 255, 0.18), transparent),
#111923;
box-shadow: inset 0 0 18px rgba(80, 217, 255, 0.06);
}
.brand-mark svg {
width: 22px;
height: 22px;
}
.brand h1 {
margin: 0;
font-size: 16px;
font-weight: 720;
letter-spacing: 0.01em;
white-space: nowrap;
}
.brand p {
margin: 2px 0 0;
color: var(--muted);
font-size: 11px;
letter-spacing: 0.08em;
text-transform: uppercase;
white-space: nowrap;
}
.header-status {
display: flex;
align-items: center;
gap: 10px;
color: var(--muted);
font: 11px ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
}
.status-dot {
width: 7px;
height: 7px;
border-radius: 50%;
background: var(--green);
box-shadow: 0 0 12px rgba(102, 227, 166, 0.7);
}
.main {
flex: 1;
min-height: 0;
display: grid;
grid-template-columns: minmax(0, 1fr) 338px;
}
.visual {
min-width: 0;
min-height: 0;
display: grid;
grid-template-rows: minmax(300px, 1fr) 246px;
gap: 12px;
padding: 12px;
}
.stage {
min-height: 0;
position: relative;
overflow: hidden;
border: 1px solid var(--line);
border-radius: var(--radius);
background: #0a0f16;
box-shadow: 0 18px 50px rgba(0, 0, 0, 0.2);
}
#threeCanvas {
position: absolute;
inset: 0;
width: 100%;
height: 100%;
display: block;
touch-action: none;
cursor: grab;
}
#threeCanvas:active {
cursor: grabbing;
}
.stage-heading {
position: absolute;
top: 16px;
left: 17px;
z-index: 2;
pointer-events: none;
}
.eyebrow {
color: var(--muted);
font-size: 10px;
font-weight: 700;
letter-spacing: 0.15em;
text-transform: uppercase;
}
.stage-heading h2 {
margin: 5px 0 0;
font-size: 19px;
font-weight: 650;
letter-spacing: -0.02em;
}
.stage-heading p {
margin: 4px 0 0;
color: var(--muted);
font-size: 11px;
}
.legend {
position: absolute;
top: 16px;
right: 16px;
z-index: 2;
display: flex;
flex-wrap: wrap;
justify-content: flex-end;
gap: 8px;
max-width: 300px;
pointer-events: none;
}
.legend-item {
display: inline-flex;
align-items: center;
gap: 6px;
padding: 5px 8px;
border: 1px solid rgba(66, 81, 101, 0.68);
border-radius: 7px;
background: rgba(9, 14, 21, 0.78);
backdrop-filter: blur(8px);
color: #bdc8d8;
font-size: 10px;
}
.legend-line {
width: 14px;
height: 2px;
border-radius: 2px;
background: var(--white);
}
.legend-line.steady {
background: var(--cyan);
}
.legend-line.transient {
background: var(--amber);
}
.phase-pill {
position: absolute;
left: 17px;
bottom: 92px;
z-index: 2;
padding: 6px 10px;
border: 1px solid rgba(255, 159, 82, 0.34);
border-radius: 999px;
background: rgba(60, 39, 24, 0.78);
color: #ffc18a;
font: 700 10px ui-monospace, SFMono-Regular, Menlo, monospace;
letter-spacing: 0.1em;
pointer-events: none;
}
.phase-pill.steady {
color: #9ceaff;
border-color: rgba(80, 217, 255, 0.34);
background: rgba(19, 56, 70, 0.8);
}
.stage-readouts {
position: absolute;
left: 16px;
right: 16px;
bottom: 15px;
z-index: 2;
display: flex;
align-items: stretch;
gap: 8px;
pointer-events: none;
}
.readout {
min-width: 105px;
padding: 8px 11px;
border: 1px solid rgba(63, 79, 99, 0.7);
border-radius: 9px;
background: rgba(9, 14, 21, 0.78);
backdrop-filter: blur(10px);
}
.readout span {
display: block;
color: var(--muted);
font-size: 9px;
font-weight: 700;
letter-spacing: 0.1em;
text-transform: uppercase;
}
.readout strong {
display: block;
margin-top: 3px;
color: #f7fbff;
font: 600 14px ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
}
.camera-hint {
position: absolute;
right: 17px;
bottom: 18px;
z-index: 2;
color: #78869a;
font-size: 10px;
pointer-events: none;
}
.scene-label {
position: absolute;
z-index: 3;
padding: 4px 7px;
border: 1px solid rgba(77, 94, 116, 0.65);
border-radius: 6px;
background: rgba(8, 13, 20, 0.8);
color: #c5d0df;
font: 9px ui-monospace, SFMono-Regular, Menlo, monospace;
letter-spacing: 0.06em;
text-transform: uppercase;
pointer-events: none;
transform: translate(-50%, -50%);
white-space: nowrap;
}
.charts {
display: grid;
grid-template-columns: repeat(3, minmax(0, 1fr));
gap: 12px;
min-width: 0;
}
.chart-card {
min-width: 0;
overflow: hidden;
border: 1px solid var(--line);
border-radius: var(--radius);
background: linear-gradient(180deg, #111721, #0e131b);
}
.chart-header {
height: 48px;
display: flex;
align-items: center;
justify-content: space-between;
gap: 8px;
padding: 0 13px;
border-bottom: 1px solid rgba(43, 53, 68, 0.8);
}
.chart-title {
min-width: 0;
}
.chart-title strong {
display: block;
overflow: hidden;
color: #dce5f1;
font-size: 11px;
font-weight: 650;
text-overflow: ellipsis;
white-space: nowrap;
}
.chart-title span {
display: block;
margin-top: 2px;
color: #6f7d91;
font-size: 9px;
}
.chart-badge {
flex: 0 0 auto;
padding: 4px 6px;
border-radius: 5px;
background: rgba(80, 217, 255, 0.08);
color: var(--cyan);
font: 9px ui-monospace, SFMono-Regular, Menlo, monospace;
}
.chart-badge.amber {
color: var(--amber);
background: rgba(255, 159, 82, 0.08);
}
.chart-card canvas {
display: block;
width: 100%;
height: calc(100% - 48px);
}
.sidebar {
min-height: 0;
overflow-y: auto;
padding: 12px 12px 18px 0;
scrollbar-width: thin;
scrollbar-color: #2d3948 transparent;
}
.panel {
margin-bottom: 12px;
padding: 15px;
border: 1px solid var(--line);
border-radius: var(--radius);
background: linear-gradient(180deg, #111721, #0f141d);
}
.panel-title {
display: flex;
align-items: center;
justify-content: space-between;
gap: 8px;
margin-bottom: 13px;
}
.panel-title h3 {
margin: 0;
font-size: 12px;
font-weight: 680;
letter-spacing: 0.02em;
}
.panel-title span {
color: #6f7d91;
font: 9px ui-monospace, SFMono-Regular, Menlo, monospace;
text-transform: uppercase;
letter-spacing: 0.08em;
}
.mode-switch {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 4px;
padding: 4px;
border: 1px solid #252f3d;
border-radius: 10px;
background: #0c1118;
}
.mode-switch button {
height: 33px;
border: 0;
border-radius: 7px;
background: transparent;
color: #8190a4;
cursor: pointer;
font-size: 10px;
font-weight: 650;
transition: 150ms ease;
}
.mode-switch button:hover {
color: #dce6f3;
}
.mode-switch button.active {
color: #ecf9ff;
background: #1b3c49;
box-shadow: inset 0 0 0 1px rgba(80, 217, 255, 0.18);
}
.mode-switch button[data-mode="startup"].active {
color: #fff0e5;
background: #49301f;
box-shadow: inset 0 0 0 1px rgba(255, 159, 82, 0.18);
}
.mode-switch button[data-mode="steady"].active {
color: #ecfaff;
background: #1b5060;
}
.mode-note {
min-height: 32px;
margin: 10px 2px 0;
color: #77869a;
font-size: 10px;
line-height: 1.55;
}
.control {
margin-top: 17px;
}
.control:first-of-type {
margin-top: 0;
}
.control-label {
display: flex;
align-items: baseline;
justify-content: space-between;
gap: 10px;
margin-bottom: 9px;
}
.control-label label {
color: #c7d1df;
font-size: 11px;
font-weight: 600;
}
.control-value {
color: var(--cyan);
font: 600 11px ui-monospace, SFMono-Regular, Menlo, monospace;
}
input[type="range"] {
--fill: 50%;
width: 100%;
height: 4px;
margin: 0;
border-radius: 999px;
outline: none;
appearance: none;
-webkit-appearance: none;
background: linear-gradient(
to right,
var(--cyan) 0,
var(--cyan) var(--fill),
#27313f var(--fill),
#27313f 100%
);
cursor: pointer;
}
input[type="range"]::-webkit-slider-thumb {
width: 15px;
height: 15px;
border: 3px solid #0d141c;
border-radius: 50%;
appearance: none;
-webkit-appearance: none;
background: #dff7ff;
box-shadow: 0 0 0 1px var(--cyan), 0 0 12px rgba(80, 217, 255, 0.22);
}
input[type="range"]::-moz-range-thumb {
width: 10px;
height: 10px;
border: 3px solid #0d141c;
border-radius: 50%;
background: #dff7ff;
box-shadow: 0 0 0 1px var(--cyan);
}
.range-meta {
display: flex;
justify-content: space-between;
margin-top: 7px;
color: #607084;
font: 9px ui-monospace, SFMono-Regular, Menlo, monospace;
}
.metric-grid {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 8px;
margin-top: 14px;
}
.metric {
min-width: 0;
padding: 10px;
border: 1px solid #25303e;
border-radius: 9px;
background: #0c1118;
}
.metric span {
display: block;
color: #718095;
font-size: 8px;
font-weight: 700;
letter-spacing: 0.1em;
text-transform: uppercase;
}
.metric strong {
display: block;
overflow: hidden;
margin-top: 5px;
color: #e8f1fb;
font: 600 13px ui-monospace, SFMono-Regular, Menlo, monospace;
text-overflow: ellipsis;
white-space: nowrap;
}
.actions {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 8px;
}
.button {
height: 38px;
border: 1px solid #2b3746;
border-radius: 9px;
background: #151c26;
color: #cbd6e3;
cursor: pointer;
font-size: 10px;
font-weight: 650;
transition: 140ms ease;
}
.button:hover {
border-color: #3c4b5f;
background: #1a2330;
transform: translateY(-1px);
}
.button.primary {
border-color: rgba(80, 217, 255, 0.35);
background: rgba(80, 217, 255, 0.12);
color: #bfefff;
}
.button.wide {
grid-column: 1 / -1;
}
.preset-grid {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 8px;
}
.preset {
min-height: 49px;
padding: 8px 9px;
border: 1px solid #283442;
border-radius: 9px;
background: #0d131b;
text-align: left;
cursor: pointer;
transition: 140ms ease;
}
.preset:hover {
border-color: #405268;
background: #121a24;
}
.preset strong {
display: block;
color: #cfdae8;
font-size: 10px;
font-weight: 650;
}
.preset span {
display: block;
margin-top: 3px;
color: #68778b;
font: 9px ui-monospace, SFMono-Regular, Menlo, monospace;
}
.equation {
padding: 11px;
border: 1px solid #283442;
border-radius: 9px;
background: #0b1017;
color: #b9c7d8;
font: 11px/1.6 ui-monospace, SFMono-Regular, Menlo, monospace;
text-align: center;
}
.equation .note {
display: block;
margin-top: 5px;
color: #657488;
font: 9px/1.5 Inter, ui-sans-serif, system-ui, sans-serif;
}
@media (max-width: 1050px) {
.main {
grid-template-columns: minmax(0, 1fr) 305px;
}
.camera-hint {
display: none;
}
.stage-readouts {
right: auto;
max-width: calc(100% - 32px);
}
}
@media (max-width: 820px) {
body {
overflow: auto;
}
.app {
height: auto;
min-height: 100vh;
}
.main {
display: block;
}
.visual {
grid-template-rows: 58vh 360px;
}
.charts {
grid-template-columns: 1fr;
}
.chart-card {
min-height: 230px;
}
.sidebar {
padding: 0 12px 20px;
overflow: visible;
}
.header-status {
display: none;
}
}
@media (max-width: 520px) {
.header {
padding: 0 13px;
}
.brand p {
display: none;
}
.visual {
grid-template-rows: 520px auto;
padding: 8px;
}
.stage-heading h2 {
font-size: 16px;
}
.legend {
top: 72px;
left: 16px;
right: auto;
justify-content: flex-start;
}
.stage-readouts {
gap: 5px;
}
.readout {
min-width: 0;
flex: 1;
padding: 7px;
}
.readout strong {
font-size: 11px;
}
.phase-pill {
bottom: 82px;
}
}
</style>
</head>
<body>
<div class="app">
<header class="header">
<div class="brand">
<div class="brand-mark" aria-hidden="true">
<svg viewBox="0 0 24 24" fill="none">
<path d="M3 12h3l2-5 4 10 3-8 2 3h4"
stroke="currentColor" stroke-width="1.7"
stroke-linecap="round" stroke-linejoin="round"/>
</svg>
</div>
<div>
<h1>Driven Oscillator Lab</h1>
<p>Startup / steady-state decomposition</p>
</div>
</div>
<div class="header-status">
<span class="status-dot"></span>
<span id="headerTime">t = 0.0 s</span>
<span>•</span>
<span>ω₀ = 1 rad/s</span>
</div>
</header>
<main class="main">
<section class="visual">
<div class="stage" id="stage">
<canvas id="threeCanvas"></canvas>
<div class="stage-heading">
<div class="eyebrow">Interactive response</div>
<h2>Low-poly forced oscillator</h2>
<p>Mass–spring–damper driven at a sinusoidal force</p>
</div>
<div class="legend" aria-label="Response legend">
<span class="legend-item">
<span class="legend-line"></span>Total
</span>
<span class="legend-item">
<span class="legend-line steady"></span>Steady
</span>
<span class="legend-item">
<span class="legend-line transient"></span>Startup
</span>
</div>
<div class="phase-pill" id="phasePill">STARTUP REGIME</div>
<div class="stage-readouts">
<div class="readout">
<span>Frequency ratio</span>
<strong id="stageRatio">0.85 ×</strong>
</div>
<div class="readout">
<span>Selected x(t)</span>
<strong id="stagePosition">0.000 m</strong>
</div>
<div class="readout">
<span>Amplitude</span>
<strong id="stageAmplitude">0.000 m</strong>
</div>
<div class="readout">
<span>Phase lag</span>
<strong id="stagePhase">0.0°</strong>
</div>
</div>
<div class="camera-hint">Drag to orbit · Wheel to zoom · Double-click to reset</div>
<div class="scene-label" id="massLabel">Mass</div>
<div class="scene-label" id="springLabel">Spring</div>
<div class="scene-label" id="driveLabel">Drive</div>
<div class="scene-label" id="phaseLabel">Phase dial</div>
</div>
<div class="charts">
<article class="chart-card">
<div class="chart-header">
<div class="chart-title">
<strong>Amplitude response</strong>
<span>Steady-state magnitude</span>
</div>
<span class="chart-badge">|X|</span>
</div>
<canvas id="amplitudeChart"></canvas>
</article>
<article class="chart-card">
<div class="chart-header">
<div class="chart-title">
<strong>Phase response</strong>
<span>Force-to-displacement lag</span>
</div>
<span class="chart-badge">φ</span>
</div>
<canvas id="phaseChart"></canvas>
</article>
<article class="chart-card">
<div class="chart-header">
<div class="chart-title">
<strong>Time decomposition</strong>
<span>Total, startup, and steady terms</span>
</div>
<span class="chart-badge amber">x(t)</span>
</div>
<canvas id="timeChart"></canvas>
</article>
</div>
</section>
<aside class="sidebar">
<section class="panel">
<div class="panel-title">
<h3>Response decomposition</h3>
<span>Display mode</span>
</div>
<div class="mode-switch" role="group" aria-label="Response display mode">
<button class="active" data-mode="combined" aria-pressed="true">
Combined
</button>
<button data-mode="startup" aria-pressed="false">
Startup
</button>
<button data-mode="steady" aria-pressed="false">
Steady
</button>
</div>
<p class="mode-note" id="modeNote">
The white response is the complete motion. Cyan is the periodic
steady-state term; amber is the decaying startup transient.
</p>
</section>
<section class="panel">
<div class="panel-title">
<h3>Experiment parameters</h3>
<span>Changes restart run</span>
</div>
<div class="control">
<div class="control-label">
<label for="frequency">Frequency ratio</label>
<span class="control-value" id="frequencyValue">0.85 × ω₀</span>
</div>
<input id="frequency" type="range" min="0.1" max="3" step="0.01" value="0.85" />
<div class="range-meta">
<span>0.10</span>
<span id="frequencyHz">0.135 Hz</span>
<span>3.00</span>
</div>
</div>
<div class="control">
<div class="control-label">
<label for="damping">Damping ratio</label>
<span class="control-value" id="dampingValue">ζ = 0.18</span>
</div>
<input id="damping" type="range" min="0.01" max="1.5" step="0.01" value="0.18" />
<div class="range-meta">
<span>0.01</span>
<span id="dampingType">Underdamped</span>
<span>1.50</span>
</div>
</div>
<div class="control">
<div class="control-label">
<label for="drive">Drive acceleration</label>
<span class="control-value" id="driveValue">a₀ = 1.00 m/s²</span>
</div>
<input id="drive" type="range" min="0.2" max="2" step="0.05" value="1" />
<div class="range-meta">
<span>0.20</span>
<span>normalized mass</span>
<span>2.00</span>
</div>
</div>
<div class="metric-grid">
<div class="metric">
<span>Steady amplitude</span>
<strong id="metricAmplitude">—</strong>
</div>
<div class="metric">
<span>Phase lag</span>
<strong id="metricPhase">—</strong>
</div>
<div class="metric">
<span>Transient envelope</span>
<strong id="metricTransient">—</strong>
</div>
<div class="metric">
<span>≈ 5% settling</span>
<strong id="metricSettle">—</strong>
</div>
</div>
</section>
<section class="panel">
<div class="panel-title">
<h3>Run controls</h3>
<span>Simulation</span>
</div>
<div class="actions">
<button class="button primary" id="togglePlay">Pause</button>
<button class="button" id="resetRun">Restart</button>
<button class="button wide" id="resetCamera">Reset camera view</button>
</div>
</section>
<section class="panel">
<div class="panel-title">
<h3>Quick presets</h3>
<span>Experiment states</span>
</div>
<div class="preset-grid">
<button class="preset" data-r="0.85" data-z="0.18">
<strong>Gentle drive</strong>
<span>r 0.85 · ζ 0.18</span>
</button>
<button class="preset" data-r="1" data-z="0.08">
<strong>Sharp resonance</strong>
<span>r 1.00 · ζ 0.08</span>
</button>
<button class="preset" data-r="1" data-z="1">
<strong>Critical damping</strong>
<span>r 1.00 · ζ 1.00</span>
</button>
<button class="preset" data-r="2.4" data-z="0.25">
<strong>High frequency</strong>
<span>r 2.40 · ζ 0.25</span>
</button>
</div>
</section>
<section class="panel">
<div class="panel-title">
<h3>Model</h3>
<span>Unit mass</span>
</div>
<div class="equation">
ẍ + 2ζω₀ẋ + ω₀²x = a₀ cos(Ωt)
<span class="note">
ω₀ = 1 rad/s. The oscillator starts from rest when the periodic
drive is applied.
</span>
</div>
</section>
</aside>
</main>
</div>
<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 state = {
ratio: 0.85,
zeta: 0.18,
drive: 1,
mode: "combined",
playing: true,
time: 0,
timeScale: 1.35,
settleTime: 10,
viewGain: 1
};
const CYAN = 0x50d9ff;
const AMBER = 0xff9f52;
const WHITE = 0xf5f8fc;
const stage = $("stage");
const canvas = $("threeCanvas");
const renderer = new THREE.WebGLRenderer({
canvas,
antialias: true,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio || 1, 2));
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.08;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x0a0f16);
scene.fog = new THREE.Fog(0x0a0f16, 11, 25);
const camera = new THREE.PerspectiveCamera(42, 1, 0.1, 100);
const cameraTarget = new THREE.Vector3(0, 0.8, 0);
const cameraState = {
theta: 0.58,
phi: 1.17,
radius: 10.6
};
function updateCamera() {
const { theta, phi, radius } = cameraState;
const horizontal = radius * Math.sin(phi);
camera.position.set(
cameraTarget.x + horizontal * Math.cos(theta),
cameraTarget.y + radius * Math.cos(phi),
cameraTarget.z + horizontal * Math.sin(theta)
);
camera.lookAt(cameraTarget);
}
updateCamera();
const hemisphere = new THREE.HemisphereLight(0xb9dcff, 0x18202b, 1.7);
scene.add(hemisphere);
const keyLight = new THREE.DirectionalLight(0xe8f4ff, 3.1);
keyLight.position.set(-3, 7, 5);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(1024, 1024);
keyLight.shadow.camera.left = -8;
keyLight.shadow.camera.right = 8;
keyLight.shadow.camera.top = 8;
keyLight.shadow.camera.bottom = -8;
keyLight.shadow.bias = -0.0008;
scene.add(keyLight);
const rimLight = new THREE.PointLight(0x2aa9e8, 22, 11, 2);
rimLight.position.set(3.2, 3.4, -3.2);
scene.add(rimLight);
const warmLight = new THREE.PointLight(0xff7b32, 15, 9, 2);
warmLight.position.set(-3.6, 2.5, 3.1);
scene.add(warmLight);
const floor = new THREE.Mesh(
new THREE.PlaneGeometry(18, 12),
new THREE.MeshStandardMaterial({
color: 0x101720,
roughness: 0.94,
metalness: 0.04
})
);
floor.rotation.x = -Math.PI / 2;
floor.position.y = -0.025;
floor.receiveShadow = true;
scene.add(floor);
const grid = new THREE.GridHelper(18, 18, 0x30475b, 0x1c2733);
grid.position.y = 0.002;
grid.material.transparent = true;
grid.material.opacity = 0.62;
scene.add(grid);
const platform = new THREE.Mesh(
new THREE.BoxGeometry(8.8, 0.12, 2.25),
new THREE.MeshStandardMaterial({
color: 0x161f2a,
roughness: 0.75,
metalness: 0.18
})
);
platform.position.set(-0.25, 0.05, 0);
platform.receiveShadow = true;
scene.add(platform);
const rail = new THREE.Mesh(
new THREE.BoxGeometry(7.8, 0.075, 0.16),
new THREE.MeshStandardMaterial({
color: 0x4b5d70,
roughness: 0.35,
metalness: 0.75
})
);
rail.position.set(-0.25, 0.16, 0);
rail.receiveShadow = true;
scene.add(rail);
const equilibriumX = -1.55;
const anchorX = -3.5;
const anchor = new THREE.Group();
scene.add(anchor);
const anchorBase = new THREE.Mesh(
new THREE.BoxGeometry(0.78, 0.48, 1.45),
new THREE.MeshStandardMaterial({
color: 0x283646,
roughness: 0.65,
metalness: 0.42,
flatShading: true
})
);
anchorBase.position.set(anchorX, 0.31, 0);
anchorBase.castShadow = true;
anchorBase.receiveShadow = true;
anchor.add(anchorBase);
const anchorPost = new THREE.Mesh(
new THREE.CylinderGeometry(0.17, 0.22, 1.15, 7),
new THREE.MeshStandardMaterial({
color: 0x607387,
roughness: 0.42,
metalness: 0.65,
flatShading: true
})
);
anchorPost.position.set(anchorX, 0.98, 0);
anchorPost.castShadow = true;
anchor.add(anchorPost);
const anchorCap = new THREE.Mesh(
new THREE.CylinderGeometry(0.26, 0.26, 0.19, 8),
new THREE.MeshStandardMaterial({
color: 0x50d9ff,
emissive: 0x123745,
emissiveIntensity: 0.55,
roughness: 0.42,
metalness: 0.4,
flatShading: true
})
);
anchorCap.position.set(anchorX, 1.54, 0);
anchorCap.castShadow = true;
anchor.add(anchorCap);
const mass = new THREE.Mesh(
new THREE.IcosahedronGeometry(0.55, 1),
new THREE.MeshStandardMaterial({
color: 0xe9f1fa,
roughness: 0.42,
metalness: 0.22,
flatShading: true
})
);
mass.position.set(equilibriumX, 1.12, 0);
mass.castShadow = true;
scene.add(mass);
const massCore = new THREE.Mesh(
new THREE.OctahedronGeometry(0.23, 0),
new THREE.MeshStandardMaterial({
color: 0x173747,
emissive: 0x102e3b,
emissiveIntensity: 0.6,
roughness: 0.4,
metalness: 0.55,
flatShading: true
})
);
mass.add(massCore);
const springMaterial = new THREE.MeshStandardMaterial({
color: 0x6edfff,
emissive: 0x123c49,
emissiveIntensity: 0.4,
roughness: 0.36,
metalness: 0.6,
flatShading: true
});
const springSegments = [];
const springSegmentGeometry = new THREE.CylinderGeometry(1, 1, 1, 5);
for (let i = 0; i < 38; i++) {
const segment = new THREE.Mesh(springSegmentGeometry, springMaterial);
segment.castShadow = true;
scene.add(segment);
springSegments.push(segment);
}
const damperOuterMaterial = new THREE.MeshStandardMaterial({
color: 0x56687a,
roughness: 0.44,
metalness: 0.7,
flatShading: true
});
const damperRodMaterial = new THREE.MeshStandardMaterial({
color: 0xff9f52,
emissive: 0x48230d,
emissiveIntensity: 0.35,
roughness: 0.35,
metalness: 0.58,
flatShading: true
});
const unitCylinder = new THREE.CylinderGeometry(1, 1, 1, 7);
const damperOuter = new THREE.Mesh(unitCylinder, damperOuterMaterial);
const damperRod = new THREE.Mesh(unitCylinder, damperRodMaterial);
damperOuter.castShadow = true;
damperRod.castShadow = true;
scene.add(damperOuter, damperRod);
const equilibriumGeometry = new THREE.BufferGeometry().setFromPoints([
new THREE.Vector3(equilibriumX, 0.19, -0.92),
new THREE.Vector3(equilibriumX, 1.9, -0.92)
]);
const equilibriumLine = new THREE.Line(
equilibriumGeometry,
new THREE.LineDashedMaterial({
color: 0x667b90,
dashSize: 0.08,
gapSize: 0.07,
transparent: true,
opacity: 0.62
})
);
equilibriumLine.computeLineDistances();
scene.add(equilibriumLine);
const ghostGeometry = new THREE.IcosahedronGeometry(0.58, 1);
const ghostMaterial = new THREE.MeshBasicMaterial({
color: CYAN,
wireframe: true,
transparent: true,
opacity: 0.2,
depthWrite: false
});
const ghostLeft = new THREE.Mesh(ghostGeometry, ghostMaterial);
const ghostRight = new THREE.Mesh(ghostGeometry, ghostMaterial);
ghostLeft.position.y = 1.12;
ghostRight.position.y = 1.12;
scene.add(ghostLeft, ghostRight);
const envelopePositions = new Float32Array([
0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0
]);
const envelopeGeometry = new THREE.BufferGeometry();
envelopeGeometry.setAttribute(
"position",
new THREE.BufferAttribute(envelopePositions, 3)
);
const envelopeLine = new THREE.LineSegments(
envelopeGeometry,
new THREE.LineBasicMaterial({
color: CYAN,
transparent: true,
opacity: 0.52
})
);
scene.add(envelopeLine);
const motorBody = new THREE.Mesh(
new THREE.BoxGeometry(1.05, 0.92, 0.92),
new THREE.MeshStandardMaterial({
color: 0x3e2a24,
roughness: 0.66,
metalness: 0.42,
flatShading: true
})
);
motorBody.position.set(3.0, 0.55, -0.35);
motorBody.castShadow = true;
motorBody.receiveShadow = true;
scene.add(motorBody);
const motorPivot = new THREE.Group();
motorPivot.position.set(2.82, 1.18, -0.02);
scene.add(motorPivot);
const motorWheel = new THREE.Mesh(
new THREE.CylinderGeometry(0.52, 0.52, 0.16, 12),
new THREE.MeshStandardMaterial({
color: 0x7f4529,
roughness: 0.45,
metalness: 0.55,
flatShading: true
})
);
motorWheel.rotation.x = Math.PI / 2;
motorWheel.castShadow = true;
motorPivot.add(motorWheel);
const motorRing = new THREE.Mesh(
new THREE.TorusGeometry(0.43, 0.055, 6, 24),
new THREE.MeshStandardMaterial({
color: 0xff9f52,
emissive: 0x4a210a,
emissiveIntensity: 0.5,
roughness: 0.36,
metalness: 0.55,
flatShading: true
})
);
motorRing.position.z = 0.1;
motorPivot.add(motorRing);
for (let i = 0; i < 3; i++) {
const spoke = new THREE.Mesh(
new THREE.BoxGeometry(0.78, 0.06, 0.055),
motorRing.material
);
spoke.rotation.z = (i * Math.PI) / 3;
spoke.position.z = 0.12;
motorPivot.add(spoke);
}
const forceArrow = new THREE.Group();
scene.add(forceArrow);
const arrowShaft = new THREE.Mesh(
new THREE.CylinderGeometry(0.045, 0.045, 0.95, 6),
new THREE.MeshStandardMaterial({
color: 0xffb16e,
emissive: 0x5b250a,
emissiveIntensity: 0.55,
roughness: 0.4,
metalness: 0.4
})
);
arrowShaft.rotation.z = -Math.PI / 2;
arrowShaft.position.x = 0.53;
forceArrow.add(arrowShaft);
const arrowHead = new THREE.Mesh(
new THREE.ConeGeometry(0.13, 0.32, 6),
arrowShaft.material
);
arrowHead.rotation.z = -Math.PI / 2;
arrowHead.position.x = 1.14;
forceArrow.add(arrowHead);
const phaseDialCenter = new THREE.Vector3(1.25, 0.075, 1.72);
const phaseRing = new THREE.Mesh(
new THREE.TorusGeometry(0.61, 0.025, 6, 36),
new THREE.MeshBasicMaterial({
color: 0x60758a,
transparent: true,
opacity: 0.75
})
);
phaseRing.position.copy(phaseDialCenter);
phaseRing.rotation.x = Math.PI / 2;
scene.add(phaseRing);
const driveReference = new THREE.Mesh(
new THREE.BoxGeometry(0.84, 0.026, 0.04),
new THREE.MeshBasicMaterial({ color: AMBER })
);
driveReference.position.copy(phaseDialCenter);
driveReference.position.y += 0.025;
scene.add(driveReference);
const phaseNeedlePivot = new THREE.Group();
phaseNeedlePivot.position.copy(phaseDialCenter);
phaseNeedlePivot.position.y += 0.035;
scene.add(phaseNeedlePivot);
const phaseNeedle = new THREE.Mesh(
new THREE.BoxGeometry(0.54, 0.028, 0.045),
new THREE.MeshBasicMaterial({ color: CYAN })
);
phaseNeedle.position.x = 0.27;
phaseNeedlePivot.add(phaseNeedle);
const phaseNeedleTip = new THREE.Mesh(
new THREE.SphereGeometry(0.055, 8, 5),
new THREE.MeshBasicMaterial({ color: CYAN })
);
phaseNeedleTip.position.x = 0.55;
phaseNeedlePivot.add(phaseNeedleTip);
const labelAnchors = {
mass: new THREE.Vector3(),
spring: new THREE.Vector3(),
drive: new THREE.Vector3(),
phase: phaseDialCenter.clone().add(new THREE.Vector3(0, 0.12, 0))
};
function setCylinderBetween(mesh, start, end, radius) {
const direction = new THREE.Vector3().subVectors(end, start);
const length = direction.length();
mesh.position.copy(start).add(end).multiplyScalar(0.5);
mesh.scale.set(radius, Math.max(length, 0.001), radius);
mesh.quaternion.setFromUnitVectors(
new THREE.Vector3(0, 1, 0),
direction.normalize()
);
}
function updateSpring(start, end) {
const points = [];
const count = springSegments.length;
const turns = 6.5;
const radius = 0.245;
for (let i = 0; i <= count; i++) {
const t = i / count;
const taper = 0.28 + 0.72 * Math.sin(Math.PI * t);
const angle = t * turns * Math.PI * 2;
points.push(
new THREE.Vector3(
THREE.MathUtils.lerp(start.x, end.x, t),
1.18 + Math.cos(angle) * radius * taper,
Math.sin(angle) * radius * taper
)
);
}
for (let i = 0; i < count; i++) {
setCylinderBetween(
springSegments[i],
points[i],
points[i + 1],
0.043
);
}
}
function amplitudeAt(ratio = state.ratio) {
const denominator = Math.sqrt(
Math.pow(1 - ratio * ratio, 2) +
Math.pow(2 * state.zeta * ratio, 2)
);
return state.drive / Math.max(denominator, 1e-9);
}
function phaseAt(ratio = state.ratio) {
let phase = Math.atan2(
2 * state.zeta * ratio,
1 - ratio * ratio
);
if (phase < 0) phase += Math.PI * 2;
return phase;
}
function coefficientData() {
const ratio = state.ratio;
const amplitude = amplitudeAt(ratio);
const phase = phaseAt(ratio);
const omega = ratio;
const c = -amplitude * Math.cos(phase);
const zDot0 = -amplitude * omega * Math.sin(phase);
const zeta = state.zeta;
if (zeta < 1 - 1e-8) {
const wd = Math.sqrt(1 - zeta * zeta);
const d = (zDot0 + zeta * c) / wd;
return {
amplitude,
phase,
c,
d,
wd,
decay: zeta,
envelope: Math.hypot(c, d),
kind: "under"
};
}
if (Math.abs(zeta - 1) < 1e-8) {
const d = zDot0 + c;
return {
amplitude,
phase,
c,
d,
wd: 0,
decay: 1,
envelope: Math.hypot(c, Math.abs(d)),
kind: "critical"
};
}
const root = Math.sqrt(zeta * zeta - 1);
const r1 = -zeta + root;
const r2 = -zeta - root;
const c2 = (zDot0 - r1 * c) / (r2 - r1);
const c1 = c - c2;
return {
amplitude,
phase,
c1,
c2,
r1,
r2,
decay: -r2,
envelope: Math.abs(c1) + Math.abs(c2),
kind: "over"
};
}
function responseAt(time) {
const data = coefficientData();
const omega = state.ratio;
const steady =
data.amplitude * Math.cos(omega * time - data.phase);
let transient;
if (data.kind === "under") {
transient =
Math.exp(-data.decay * time) *
(data.c * Math.cos(data.wd * time) +
data.d * Math.sin(data.wd * time));
} else if (data.kind === "critical") {
transient =
Math.exp(-data.decay * time) *
(data.c + data.d * time);
} else {
transient =
data.c1 * Math.exp(data.r1 * time) +
data.c2 * Math.exp(data.r2 * time);
}
return {
total: steady + transient,
steady,
transient,
data
};
}
function calculateSettlingTime() {
const decay = coefficientData().decay;
if (decay <= 1e-6) return 30;
return THREE.MathUtils.clamp(
Math.log(20) / decay,
0.5,
30
);
}
function selectedResponse(response) {
if (state.mode === "startup") return response.transient;
if (state.mode === "steady") return response.steady;
return response.total;
}
function updateScene(response) {
const selected = selectedResponse(response);
const visualDisplacement = selected * state.viewGain;
const massX = equilibriumX + visualDisplacement;
const amplitudeVisual = response.data.amplitude * state.viewGain;
mass.position.x = massX;
ghostLeft.position.x = equilibriumX - amplitudeVisual;
ghostRight.position.x = equilibriumX + amplitudeVisual;
const start = new THREE.Vector3(anchorX + 0.1, 1.18, 0);
const end = new THREE.Vector3(massX - 0.43, 1.18, 0);
updateSpring(start, end);
const damperStart = new THREE.Vector3(anchorX + 0.05, 0.56, 0.66);
const damperEnd = new THREE.Vector3(massX - 0.32, 0.56, 0.66);
const outerEnd = damperStart.clone().add(new THREE.Vector3(1.05, 0, 0));
setCylinderBetween(damperOuter, damperStart, outerEnd, 0.13);
setCylinderBetween(
damperRod,
damperStart.clone().add(new THREE.Vector3(0.86, 0, 0)),
damperEnd,
0.066
);
const p = envelopeGeometry.attributes.position.array;
const z = -0.73;
p.set([
equilibriumX - amplitudeVisual, 0.09, z,
equilibriumX + amplitudeVisual, 0.09, z,
equilibriumX - amplitudeVisual, 0.09, z - 0.08,
equilibriumX - amplitudeVisual, 0.09, z + 0.08,
equilibriumX + amplitudeVisual, 0.09, z - 0.08,
equilibriumX + amplitudeVisual, 0.09, z + 0.08
]);
envelopeGeometry.attributes.position.needsUpdate = true;
envelopeGeometry.computeBoundingSphere();
const force = state.drive * Math.cos(state.ratio * state.time);
forceArrow.position.set(massX + 0.22, 1.16, 0);
forceArrow.rotation.y = force >= 0 ? 0 : Math.PI;
forceArrow.scale.x = 0.75 + 0.35 * Math.min(Math.abs(force) / 2, 1);
forceArrow.visible = Math.abs(force) > 0.025;
motorPivot.rotation.z = -state.ratio * state.time;
phaseNeedlePivot.rotation.y = -response.data.phase;
labelAnchors.mass.set(massX, 1.91, 0);
labelAnchors.spring.set(
(anchorX + massX) * 0.5,
1.62,
0
);
labelAnchors.drive.set(2.9, 1.91, -0.02);
}
function projectLabel(element, worldPosition) {
const projected = worldPosition.clone().project(camera);
const width = stage.clientWidth;
const height = stage.clientHeight;
if (
projected.z < -1 ||
projected.z > 1 ||
projected.x < -1.2 ||
projected.x > 1.2 ||
projected.y < -1.2 ||
projected.y > 1.2
) {
element.style.opacity = "0";
return;
}
const x = (projected.x * 0.5 + 0.5) * width;
const y = (-projected.y * 0.5 + 0.5) * height;
element.style.opacity = "1";
element.style.left = `${x}px`;
element.style.top = `${y}px`;
}
function updateLabels() {
projectLabel($("massLabel"), labelAnchors.mass);
projectLabel($("springLabel"), labelAnchors.spring);
projectLabel($("driveLabel"), labelAnchors.drive);
projectLabel($("phaseLabel"), labelAnchors.phase);
}
// Direct camera controls: no Three.js addons are used.
let dragging = false;
let lastPointerX = 0;
let lastPointerY = 0;
canvas.addEventListener("pointerdown", (event) => {
dragging = true;
lastPointerX = event.clientX;
lastPointerY = event.clientY;
canvas.setPointerCapture(event.pointerId);
});
canvas.addEventListener("pointermove", (event) => {
if (!dragging) return;
const dx = event.clientX - lastPointerX;
const dy = event.clientY - lastPointerY;
lastPointerX = event.clientX;
lastPointerY = event.clientY;
cameraState.theta -= dx * 0.007;
cameraState.phi = THREE.MathUtils.clamp(
cameraState.phi - dy * 0.006,
0.36,
1.46
);
});
function endDrag(event) {
dragging = false;
if (event.pointerId !== undefined) {
try {
canvas.releasePointerCapture(event.pointerId);
} catch (_) {}
}
}
canvas.addEventListener("pointerup", endDrag);
canvas.addEventListener("pointercancel", endDrag);
canvas.addEventListener(
"wheel",
(event) => {
event.preventDefault();
cameraState.radius = THREE.MathUtils.clamp(
cameraState.radius + event.deltaY * 0.008,
6.3,
16.5
);
},
{ passive: false }
);
canvas.addEventListener("dblclick", resetCamera);
function resetCamera() {
cameraState.theta = 0.58;
cameraState.phi = 1.17;
cameraState.radius = 10.6;
}
$("resetCamera").addEventListener("click", resetCamera);
// Chart helpers
function prepareCanvas(target) {
const rect = target.getBoundingClientRect();
const dpr = Math.min(window.devicePixelRatio || 1, 2);
const width = Math.max(1, Math.round(rect.width * dpr));
const height = Math.max(1, Math.round(rect.height * dpr));
if (target.width !== width || target.height !== height) {
target.width = width;
target.height = height;
}
const context = target.getContext("2d");
context.setTransform(dpr, 0, 0, dpr, 0, 0);
context.clearRect(0, 0, rect.width, rect.height);
return {
context,
width: rect.width,
height: rect.height
};
}
function drawChartFrame(context, width, height, margins) {
const {
left,
right,
top,
bottom
} = margins;
context.save();
context.strokeStyle = "rgba(96, 113, 134, 0.22)";
context.lineWidth = 1;
context.strokeRect(
left + 0.5,
top + 0.5,
width - left - right - 1,
height - top - bottom - 1
);
context.restore();
}
function drawAmplitudeChart() {
const { context: ctx, width, height } = prepareCanvas(
$("amplitudeChart")
);
const margins = { left: 42, right: 13, top: 16, bottom: 28 };
const plotW = width - margins.left - margins.right;
const plotH = height - margins.top - margins.bottom;
if (plotW <= 0 || plotH <= 0) return;
const minRatio = 0.1;
const maxRatio = 3;
const minLog = -2;
let maxLog = 0;
for (let i = 0; i <= 300; i++) {
const ratio = THREE.MathUtils.lerp(minRatio, maxRatio, i / 300);
maxLog = Math.max(maxLog, Math.log10(amplitudeAt(ratio)));
}
maxLog = Math.max(0, Math.ceil(maxLog));
const xMap = (r) =>
margins.left + ((r - minRatio) / (maxRatio - minRatio)) * plotW;
const yMap = (amplitude) => {
const log = Math.log10(Math.max(amplitude, 0.01));
const normalized = (log - minLog) / (maxLog - minLog);
return margins.top + plotH - normalized * plotH;
};
ctx.font = "9px ui-monospace, SFMono-Regular, Menlo, monospace";
ctx.textBaseline = "middle";
for (let decade = minLog; decade <= maxLog; decade++) {
const value = Math.pow(10, decade);
const y = yMap(value);
ctx.strokeStyle = "rgba(94, 111, 132, 0.15)";
ctx.beginPath();
ctx.moveTo(margins.left, y);
ctx.lineTo(width - margins.right, y);
ctx.stroke();
ctx.fillStyle = "#69778a";
ctx.textAlign = "right";
ctx.fillText(
decade === 0 ? "1" : `10${superscript(decade)}`,
margins.left - 7,
y
);
}
for (const ratio of [0.1, 1, 2, 3]) {
const x = xMap(ratio);
ctx.strokeStyle = "rgba(94, 111, 132, 0.13)";
ctx.beginPath();
ctx.moveTo(x, margins.top);
ctx.lineTo(x, height - margins.bottom);
ctx.stroke();
ctx.fillStyle = "#69778a";
ctx.textAlign = "center";
ctx.fillText(
ratio === 1 ? "1×" : ratio.toFixed(ratio % 1 ? 1 : 0),
x,
height - 11
);
}
ctx.beginPath();
for (let i = 0; i <= 320; i++) {
const ratio = THREE.MathUtils.lerp(minRatio, maxRatio, i / 320);
const x = xMap(ratio);
const y = yMap(amplitudeAt(ratio));
if (i === 0) ctx.moveTo(x, y);
else ctx.lineTo(x, y);
}
ctx.strokeStyle = "#50d9ff";
ctx.lineWidth = 1.8;
ctx.stroke();
const markerX = xMap(state.ratio);
const markerY = yMap(amplitudeAt());
ctx.strokeStyle = "rgba(255,255,255,0.25)";
ctx.lineWidth = 1;
ctx.beginPath();
ctx.moveTo(markerX, margins.top);
ctx.lineTo(markerX, height - margins.bottom);
ctx.stroke();
ctx.fillStyle = "#f4fbff";
ctx.beginPath();
ctx.arc(markerX, markerY, 3.5, 0, Math.PI * 2);
ctx.fill();
ctx.fillStyle = "#738196";
ctx.textAlign = "left";
ctx.fillText("Ω/ω₀", margins.left + 2, height - 11);
drawChartFrame(ctx, width, height, margins);
}
function superscript(value) {
const map = {
"-": "⁻",
0: "⁰",
1: "¹",
2: "²",
3: "³",
4: "⁴",
5: "⁵",
6: "⁶",
7: "⁷",
8: "⁸",
9: "⁹"
};
return String(value)
.split("")
.map((character) => map[character] || character)
.join("");
}
function drawPhaseChart() {
const { context: ctx, width, height } = prepareCanvas(
$("phaseChart")
);
const margins = { left: 42, right: 13, top: 16, bottom: 28 };
const plotW = width - margins.left - margins.right;
const plotH = height - margins.top - margins.bottom;
if (plotW <= 0 || plotH <= 0) return;
const xMap = (r) =>
margins.left + ((r - 0.1) / 2.9) * plotW;
const yMap = (degrees) =>
margins.top + plotH - (degrees / 180) * plotH;
ctx.font = "9px ui-monospace, SFMono-Regular, Menlo, monospace";
ctx.textBaseline = "middle";
for (const degrees of [0, 45, 90, 135, 180]) {
const y = yMap(degrees);
ctx.strokeStyle = "rgba(94, 111, 132, 0.15)";
ctx.beginPath();
ctx.moveTo(margins.left, y);
ctx.lineTo(width - margins.right, y);
ctx.stroke();
ctx.fillStyle = "#69778a";
ctx.textAlign = "right";
ctx.fillText(`${degrees}°`, margins.left - 7, y);
}
for (const ratio of [0.1, 1, 2, 3]) {
const x = xMap(ratio);
ctx.strokeStyle = "rgba(94, 111, 132, 0.13)";
ctx.beginPath();
ctx.moveTo(x, margins.top);
ctx.lineTo(x, height - margins.bottom);
ctx.stroke();
ctx.fillStyle = "#69778a";
ctx.textAlign = "center";
ctx.fillText(
ratio === 1 ? "1×" : ratio.toFixed(ratio % 1 ? 1 : 0),
x,
height - 11
);
}
ctx.beginPath();
for (let i = 0; i <= 320; i++) {
const ratio = THREE.MathUtils.lerp(0.1, 3, i / 320);
const degrees = (phaseAt(ratio) * 180) / Math.PI;
const x = xMap(ratio);
const y = yMap(degrees);
if (i === 0) ctx.moveTo(x, y);
else ctx.lineTo(x, y);
}
ctx.strokeStyle = "#ff9f52";
ctx.lineWidth = 1.8;
ctx.stroke();
const markerX = xMap(state.ratio);
const markerY = yMap((phaseAt() * 180) / Math.PI);
ctx.strokeStyle = "rgba(255,255,255,0.25)";
ctx.lineWidth = 1;
ctx.beginPath();
ctx.moveTo(markerX, margins.top);
ctx.lineTo(markerX, height - margins.bottom);
ctx.stroke();
ctx.fillStyle = "#fff2e7";
ctx.beginPath();
ctx.arc(markerX, markerY, 3.5, 0, Math.PI * 2);
ctx.fill();
ctx.fillStyle = "#738196";
ctx.textAlign = "left";
ctx.fillText("Ω/ω₀", margins.left + 2, height - 11);
drawChartFrame(ctx, width, height, margins);
}
function drawTimeChart() {
const { context: ctx, width, height } = prepareCanvas(
$("timeChart")
);
const margins = { left: 42, right: 13, top: 16, bottom: 28 };
const plotW = width - margins.left - margins.right;
const plotH = height - margins.top - margins.bottom;
if (plotW <= 0 || plotH <= 0) return;
const timeEnd = Math.max(12, Math.min(30, state.settleTime + 1));
const samples = 500;
let maxValue = 0.2;
for (let i = 0; i <= samples; i++) {
const time = (i / samples) * timeEnd;
const response = responseAt(time);
maxValue = Math.max(
maxValue,
Math.abs(response.total),
Math.abs(response.steady),
Math.abs(response.transient)
);
}
maxValue *= 1.08;
const xMap = (t) =>
margins.left + (t / timeEnd) * plotW;
const yMap = (value) =>
margins.top + plotH * 0.5 - (value / maxValue) * plotH * 0.45;
const settleX = xMap(Math.min(state.settleTime, timeEnd));
ctx.fillStyle = "rgba(255, 159, 82, 0.055)";
ctx.fillRect(margins.left, margins.top, settleX - margins.left, plotH);
ctx.fillStyle = "rgba(80, 217, 255, 0.045)";
ctx.fillRect(settleX, margins.top, width - margins.right - settleX, plotH);
ctx.strokeStyle = "rgba(94, 111, 132, 0.16)";
ctx.lineWidth = 1;
ctx.beginPath();
ctx.moveTo(margins.left, yMap(0));
ctx.lineTo(width - margins.right, yMap(0));
ctx.stroke();
ctx.setLineDash([3, 4]);
ctx.strokeStyle = "rgba(255, 159, 82, 0.5)";
ctx.beginPath();
ctx.moveTo(settleX, margins.top);
ctx.lineTo(settleX, height - margins.bottom);
ctx.stroke();
ctx.setLineDash([]);
const paths = [
{
key: "transient",
color: "#ff9f52",
width: state.mode === "startup" ? 2.4 : 1.15,
alpha: state.mode === "startup" ? 1 : 0.8
},
{
key: "steady",
color: "#50d9ff",
width: state.mode === "steady" ? 2.4 : 1.25,
alpha: state.mode === "steady" ? 1 : 0.9
},
{
key: "total",
color: "#f4f7fb",
width: state.mode === "combined" ? 2.2 : 0.9,
alpha: state.mode === "combined" ? 0.95 : 0.38
}
];
for (const path of paths) {
ctx.beginPath();
for (let i = 0; i <= samples; i++) {
const time = (i / samples) * timeEnd;
const response = responseAt(time);
const x = xMap(time);
const y = yMap(response[path.key]);
if (i === 0) ctx.moveTo(x, y);
else ctx.lineTo(x, y);
}
ctx.strokeStyle = path.color;
ctx.globalAlpha = path.alpha;
ctx.lineWidth = path.width;
ctx.stroke();
ctx.globalAlpha = 1;
}
const cursorX = xMap(Math.min(state.time, timeEnd));
ctx.strokeStyle = "rgba(255,255,255,0.38)";
ctx.lineWidth = 1;
ctx.beginPath();
ctx.moveTo(cursorX, margins.top);
ctx.lineTo(cursorX, height - margins.bottom);
ctx.stroke();
const currentResponse = responseAt(state.time);
const currentY = yMap(selectedResponse(currentResponse));
ctx.fillStyle =
state.mode === "startup"
? "#ff9f52"
: state.mode === "steady"
? "#50d9ff"
: "#ffffff";
ctx.beginPath();
ctx.arc(cursorX, currentY, 3.5, 0, Math.PI * 2);
ctx.fill();
ctx.font = "9px ui-monospace, SFMono-Regular, Menlo, monospace";
ctx.textBaseline = "middle";
ctx.fillStyle = "#69778a";
ctx.textAlign = "right";
ctx.fillText(`+${maxValue.toFixed(1)}`, margins.left - 7, yMap(maxValue));
ctx.fillText("0", margins.left - 7, yMap(0));
ctx.fillText(`−${maxValue.toFixed(1)}`, margins.left - 7, yMap(-maxValue));
ctx.textAlign = "center";
ctx.fillText("0 s", margins.left, height - 11);
ctx.fillText(`${timeEnd.toFixed(0)} s`, width - margins.right, height - 11);
ctx.textAlign = "left";
ctx.fillStyle = "rgba(255,159,82,0.75)";
ctx.fillText("STARTUP", margins.left + 6, margins.top + 8);
ctx.fillStyle = "rgba(80,217,255,0.75)";
ctx.fillText("STEADY", settleX + 6, margins.top + 8);
drawChartFrame(ctx, width, height, margins);
}
function drawResponseCharts() {
drawAmplitudeChart();
drawPhaseChart();
}
function drawAllCharts() {
drawResponseCharts();
drawTimeChart();
}
function updateRangeFill(input) {
const min = Number(input.min);
const max = Number(input.max);
const value = Number(input.value);
input.style.setProperty(
"--fill",
`${((value - min) / (max - min)) * 100}%`
);
}
function formatSigned(value, digits = 3) {
const sign = value >= 0 ? "+" : "−";
return `${sign}${Math.abs(value).toFixed(digits)}`;
}
function updateReadouts(response) {
const data = response.data;
const selected = selectedResponse(response);
const phaseDegrees = (data.phase * 180) / Math.PI;
$("headerTime").textContent = `t = ${state.time.toFixed(1)} s`;
$("stageRatio").textContent = `${state.ratio.toFixed(2)} ×`;
$("stagePosition").textContent = `${formatSigned(selected)} m`;
$("stageAmplitude").textContent = `${data.amplitude.toFixed(3)} m`;
$("stagePhase").textContent = `${phaseDegrees.toFixed(1)}°`;
$("metricAmplitude").textContent = `${data.amplitude.toFixed(3)} m`;
$("metricPhase").textContent = `${phaseDegrees.toFixed(1)}°`;
$("metricTransient").textContent = `${data.envelope.toFixed(3)} m`;
$("metricSettle").textContent =
state.settleTime >= 30
? "> 30 s"
: `≈ ${state.settleTime.toFixed(1)} s`;
const inStartup = state.time < state.settleTime;
const phasePill = $("phasePill");
phasePill.textContent = inStartup
? "STARTUP REGIME"
: "STEADY REGIME";
phasePill.classList.toggle("steady", !inStartup);
}
function refreshParameters(reset = true) {
state.ratio = Number($("frequency").value);
state.zeta = Number($("damping").value);
state.drive = Number($("drive").value);
$("frequencyValue").textContent = `${state.ratio.toFixed(2)} × ω₀`;
$("frequencyHz").textContent = `${(state.ratio / (2 * Math.PI)).toFixed(3)} Hz`;
$("dampingValue").textContent = `ζ = ${state.zeta.toFixed(2)}`;
$("dampingType").textContent =
state.zeta < 0.995
? "Underdamped"
: state.zeta <= 1.005
? "Critical"
: "Overdamped";
$("driveValue").textContent = `a₀ = ${state.drive.toFixed(2)} m/s²`;
for (const id of ["frequency", "damping", "drive"]) {
updateRangeFill($(id));
}
const data = coefficientData();
state.settleTime = calculateSettlingTime();
state.viewGain = THREE.MathUtils.clamp(
1.8 / Math.max(data.amplitude, data.envelope, 0.15),
0.038,
1.35
);
if (reset) state.time = 0;
drawResponseCharts();
drawTimeChart();
updateReadouts(responseAt(state.time));
}
$("frequency").addEventListener("input", () => refreshParameters(true));
$("damping").addEventListener("input", () => refreshParameters(true));
$("drive").addEventListener("input", () => refreshParameters(true));
document.querySelectorAll(".mode-switch button").forEach((button) => {
button.addEventListener("click", () => {
state.mode = button.dataset.mode;
document.querySelectorAll(".mode-switch button").forEach((other) => {
const selected = other === button;
other.classList.toggle("active", selected);
other.setAttribute("aria-pressed", String(selected));
});
const notes = {
combined:
"The white response is the complete motion. Cyan is the periodic steady-state term; amber is the decaying startup transient.",
startup:
"Only the homogeneous startup transient is shown. It provides the motion needed to join the periodic steady-state solution with the initial rest condition.",
steady:
"Only the periodic steady-state term is shown. This is the response that remains after the startup transient has decayed."
};
$("modeNote").textContent = notes[state.mode];
drawTimeChart();
updateReadouts(responseAt(state.time));
});
});
$("togglePlay").addEventListener("click", () => {
state.playing = !state.playing;
$("togglePlay").textContent = state.playing ? "Pause" : "Play";
});
$("resetRun").addEventListener("click", () => {
state.time = 0;
drawTimeChart();
updateReadouts(responseAt(state.time));
});
document.querySelectorAll(".preset").forEach((button) => {
button.addEventListener("click", () => {
$("frequency").value = button.dataset.r;
$("damping").value = button.dataset.z;
refreshParameters(true);
});
});
function resizeThree() {
const width = stage.clientWidth;
const height = stage.clientHeight;
renderer.setSize(width, height, false);
camera.aspect = width / Math.max(height, 1);
camera.updateProjectionMatrix();
}
const stageObserver = new ResizeObserver(resizeThree);
stageObserver.observe(stage);
for (const id of ["amplitudeChart", "phaseChart", "timeChart"]) {
const observer = new ResizeObserver(() => {
if (id === "timeChart") drawTimeChart();
else drawResponseCharts();
});
observer.observe($(id));
}
refreshParameters(false);
resizeThree();
let previousTime = performance.now();
let lastChartUpdate = 0;
let lastReadoutUpdate = 0;
function animate(now) {
const delta = Math.min((now - previousTime) / 1000, 0.05);
previousTime = now;
if (state.playing) {
state.time += delta * state.timeScale;
}
updateCamera();
const response = responseAt(state.time);
updateScene(response);
updateLabels();
renderer.render(scene, camera);
if (now - lastChartUpdate > 32) {
drawTimeChart();
lastChartUpdate = now;
}
if (now - lastReadoutUpdate > 90) {
updateReadouts(response);
lastReadoutUpdate = now;
}
requestAnimationFrame(animate);
}
requestAnimationFrame(animate);
</script>
</body>
</html>
```