Prompt specification
Build or customize the Three.js 3D scene "Plate area and spacing comparison" inside the active workspace.
Engineering Constraints:
1. Runtime & Orbit: Use Three.js r160 with OrbitControls (damping enabled) and ACESFilmicToneMapping.
2. Frame Timing: Drive all motion strictly with clock.getDelta() so animation speed is FPS-independent.
3. Viewport Hygiene: Handle window resize cleanly, cap DPR at Math.min(window.devicePixelRatio, 2), and dispose geometries/materials/renderer on unmount.
Scene Metadata:
Scene: Plate area and spacing comparison
Category: Science & Physics · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/plate-area-and-spacing-comparison-x1606
Scene Description Prompt:
Use Three.js from cdnjs to place ideal parallel-plate capacitors with differing areas and gaps; compare capacitance under a stated uniform dielectric approximation and label edge effects omitted. Deliver one self-contained HTML file with stylized solid shapes.
Working Three.js Starter (plate-area-and-spacing-comparison-x1606):
```html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Parallel-Plate Capacitor Lab</title>
<style>
:root {
color-scheme: dark;
--bg: #07111a;
--panel: rgba(11, 25, 38, 0.84);
--panel-strong: rgba(15, 33, 48, 0.94);
--line: rgba(154, 211, 238, 0.16);
--text: #eef8ff;
--muted: #8ea9b9;
--cyan: #58d5ff;
--orange: #ff8b69;
--violet: #a695ff;
--yellow: #f6d66f;
--green: #72e3b1;
--shadow: 0 22px 70px rgba(0, 0, 0, 0.34);
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background:
radial-gradient(circle at 48% 38%, #132b3c 0, #081722 42%, #050b11 100%);
color: var(--text);
font-family:
Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
button,
input {
font: inherit;
}
button {
color: inherit;
}
#stage,
#stage canvas,
#labels {
position: fixed;
inset: 0;
width: 100%;
height: 100%;
}
#stage {
z-index: 0;
}
#stage canvas {
display: block;
touch-action: none;
outline: none;
}
#labels {
z-index: 4;
overflow: hidden;
pointer-events: none;
}
.topbar {
position: fixed;
z-index: 20;
top: 0;
left: 0;
right: 0;
min-height: 80px;
display: grid;
grid-template-columns: minmax(250px, 1fr) auto minmax(250px, 1fr);
align-items: center;
gap: 18px;
padding: 16px 22px;
pointer-events: none;
background:
linear-gradient(180deg, rgba(4, 12, 19, 0.94), rgba(4, 12, 19, 0.55) 72%, transparent);
}
.brand {
display: flex;
align-items: center;
gap: 13px;
min-width: 0;
}
.brand-mark {
position: relative;
width: 43px;
height: 43px;
flex: 0 0 auto;
border: 1px solid rgba(88, 213, 255, 0.38);
border-radius: 13px;
background:
linear-gradient(145deg, rgba(88, 213, 255, 0.16), rgba(166, 149, 255, 0.08));
box-shadow: inset 0 0 22px rgba(88, 213, 255, 0.06);
}
.brand-mark::before,
.brand-mark::after {
content: "";
position: absolute;
left: 8px;
right: 8px;
height: 5px;
border-radius: 3px;
}
.brand-mark::before {
top: 9px;
background: var(--orange);
box-shadow: 0 3px 10px rgba(255, 139, 105, 0.3);
}
.brand-mark::after {
bottom: 9px;
background: var(--cyan);
box-shadow: 0 3px 10px rgba(88, 213, 255, 0.3);
}
.eyebrow {
margin: 0 0 3px;
color: var(--cyan);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.18em;
text-transform: uppercase;
}
h1 {
margin: 0;
font-size: clamp(17px, 1.6vw, 23px);
font-weight: 740;
letter-spacing: -0.035em;
white-space: nowrap;
}
.formula-card {
justify-self: center;
display: flex;
align-items: center;
gap: 14px;
padding: 10px 14px;
border: 1px solid var(--line);
border-radius: 14px;
background: rgba(9, 23, 35, 0.68);
backdrop-filter: blur(14px);
}
.formula {
font-family: Georgia, "Times New Roman", serif;
font-size: 19px;
white-space: nowrap;
}
.formula small {
color: var(--muted);
font-family: inherit;
font-size: 12px;
}
.edge-badge {
padding: 7px 9px;
border: 1px solid rgba(246, 214, 111, 0.32);
border-radius: 8px;
background: rgba(246, 214, 111, 0.08);
color: var(--yellow);
font-size: 9px;
font-weight: 850;
letter-spacing: 0.12em;
white-space: nowrap;
text-transform: uppercase;
}
.toolbar {
justify-self: end;
display: flex;
gap: 8px;
pointer-events: auto;
}
.tool-button,
.small-button {
border: 1px solid var(--line);
background: rgba(13, 29, 43, 0.82);
border-radius: 10px;
cursor: pointer;
transition:
border-color 160ms ease,
background 160ms ease,
transform 160ms ease;
}
.tool-button {
padding: 10px 12px;
color: #c8dbe7;
font-size: 11px;
font-weight: 750;
}
.tool-button:hover,
.small-button:hover {
border-color: rgba(88, 213, 255, 0.48);
background: rgba(24, 48, 66, 0.9);
}
.tool-button:active,
.small-button:active {
transform: translateY(1px);
}
.tool-button.active {
color: var(--yellow);
border-color: rgba(246, 214, 111, 0.38);
background: rgba(246, 214, 111, 0.08);
}
.panel {
position: fixed;
z-index: 12;
border: 1px solid var(--line);
border-radius: 18px;
background:
linear-gradient(145deg, rgba(17, 37, 53, 0.9), rgba(8, 22, 33, 0.86));
box-shadow: var(--shadow);
backdrop-filter: blur(18px) saturate(125%);
overflow: hidden;
}
.panel::before {
content: "";
position: absolute;
inset: 0;
pointer-events: none;
background:
linear-gradient(120deg, rgba(255, 255, 255, 0.045), transparent 28%);
}
.control-panel {
top: 102px;
left: 22px;
width: 310px;
padding: 18px;
}
.comparison-panel {
top: 102px;
right: 22px;
width: 322px;
padding: 18px;
}
.panel-heading {
position: relative;
display: flex;
align-items: flex-start;
justify-content: space-between;
gap: 14px;
margin-bottom: 15px;
}
.panel-heading h2 {
margin: 0;
font-size: 15px;
letter-spacing: -0.02em;
}
.panel-heading p {
margin: 5px 0 0;
color: var(--muted);
font-size: 11px;
line-height: 1.45;
}
.selected-letter {
display: grid;
place-items: center;
width: 38px;
height: 38px;
flex: 0 0 auto;
border-radius: 11px;
background: linear-gradient(145deg, var(--cyan), #378be8);
color: #03111b;
font-size: 18px;
font-weight: 900;
box-shadow: 0 8px 24px rgba(88, 213, 255, 0.2);
}
.cap-tabs {
position: relative;
display: grid;
grid-template-columns: repeat(4, 1fr);
gap: 6px;
margin-bottom: 15px;
}
.cap-tab {
height: 31px;
border: 1px solid var(--line);
border-radius: 8px;
background: rgba(255, 255, 255, 0.025);
color: var(--muted);
cursor: pointer;
font-size: 11px;
font-weight: 800;
}
.cap-tab.active {
border-color: rgba(88, 213, 255, 0.55);
background: rgba(88, 213, 255, 0.12);
color: var(--cyan);
}
.result-box {
position: relative;
margin-bottom: 16px;
padding: 14px;
border: 1px solid rgba(88, 213, 255, 0.18);
border-radius: 13px;
background:
radial-gradient(circle at 92% 0, rgba(88, 213, 255, 0.12), transparent 42%),
rgba(4, 15, 24, 0.44);
}
.result-caption {
color: var(--muted);
font-size: 10px;
font-weight: 750;
letter-spacing: 0.1em;
text-transform: uppercase;
}
.result-value {
margin: 3px 0 1px;
color: var(--cyan);
font-size: 31px;
font-weight: 780;
letter-spacing: -0.055em;
}
.result-value span {
margin-left: 4px;
color: #b9d5e3;
font-size: 13px;
font-weight: 650;
letter-spacing: 0;
}
.result-sub {
color: var(--muted);
font-size: 10px;
}
.stats-grid {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 7px;
margin-bottom: 15px;
}
.stat {
min-width: 0;
padding: 9px 10px;
border: 1px solid rgba(255, 255, 255, 0.055);
border-radius: 9px;
background: rgba(255, 255, 255, 0.025);
}
.stat-label {
display: block;
margin-bottom: 3px;
color: var(--muted);
font-size: 9px;
font-weight: 750;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.stat-value {
display: block;
overflow: hidden;
color: #dcecf5;
font-size: 11px;
font-weight: 700;
text-overflow: ellipsis;
white-space: nowrap;
}
.control-group {
position: relative;
margin: 0 0 13px;
}
.control-label {
display: flex;
align-items: center;
justify-content: space-between;
margin-bottom: 7px;
color: #b9cbd6;
font-size: 10px;
font-weight: 700;
}
.control-value {
color: var(--cyan);
font-variant-numeric: tabular-nums;
}
input[type="range"] {
width: 100%;
height: 4px;
margin: 0;
border-radius: 999px;
appearance: none;
background: linear-gradient(
90deg,
rgba(88, 213, 255, 0.75),
rgba(88, 213, 255, 0.12)
);
cursor: pointer;
}
input[type="range"]::-webkit-slider-thumb {
width: 15px;
height: 15px;
border: 2px solid #dff7ff;
border-radius: 50%;
appearance: none;
background: #3dbff1;
box-shadow: 0 0 0 4px rgba(88, 213, 255, 0.1);
}
input[type="range"]::-moz-range-thumb {
width: 12px;
height: 12px;
border: 2px solid #dff7ff;
border-radius: 50%;
background: #3dbff1;
box-shadow: 0 0 0 4px rgba(88, 213, 255, 0.1);
}
.button-row {
position: relative;
display: flex;
gap: 7px;
margin-top: 4px;
}
.small-button {
flex: 1;
padding: 8px 10px;
color: #b9ceda;
font-size: 10px;
font-weight: 750;
}
.assumption {
position: relative;
margin-top: 14px;
padding: 11px 12px;
border-left: 2px solid var(--violet);
border-radius: 0 9px 9px 0;
background: rgba(166, 149, 255, 0.065);
color: #aabccc;
font-size: 10px;
line-height: 1.5;
}
.assumption strong {
color: #d9d3ff;
}
.comparison-list {
position: relative;
display: grid;
gap: 8px;
}
.comparison-row {
display: grid;
grid-template-columns: 27px minmax(0, 1fr) auto;
align-items: center;
gap: 9px;
width: 100%;
padding: 9px;
border: 1px solid transparent;
border-radius: 10px;
background: rgba(255, 255, 255, 0.018);
cursor: pointer;
text-align: left;
transition: 150ms ease;
}
.comparison-row:hover {
background: rgba(255, 255, 255, 0.045);
}
.comparison-row.selected {
border-color: rgba(88, 213, 255, 0.24);
background: rgba(88, 213, 255, 0.075);
}
.comparison-id {
display: grid;
place-items: center;
width: 27px;
height: 27px;
border-radius: 8px;
background: rgba(88, 213, 255, 0.1);
color: var(--cyan);
font-size: 11px;
font-weight: 900;
}
.comparison-main {
min-width: 0;
}
.comparison-name {
display: flex;
justify-content: space-between;
gap: 8px;
margin-bottom: 6px;
color: #c9dbe6;
font-size: 9px;
font-weight: 700;
}
.comparison-name span:last-child {
color: var(--muted);
font-weight: 550;
}
.bar-track {
height: 5px;
overflow: hidden;
border-radius: 999px;
background: rgba(255, 255, 255, 0.065);
}
.bar-fill {
height: 100%;
border-radius: inherit;
background: linear-gradient(90deg, #3bbcf1, #8e85ff);
box-shadow: 0 0 12px rgba(88, 213, 255, 0.2);
transition: width 220ms ease;
}
.comparison-value {
min-width: 58px;
color: #e4f3fa;
font-size: 11px;
font-weight: 780;
font-variant-numeric: tabular-nums;
text-align: right;
}
.comparison-note {
position: relative;
margin-top: 14px;
padding-top: 13px;
border-top: 1px solid var(--line);
color: var(--muted);
font-size: 10px;
line-height: 1.5;
}
.comparison-note strong {
color: var(--yellow);
}
.legend {
position: fixed;
z-index: 11;
left: 50%;
bottom: 22px;
display: flex;
align-items: center;
gap: 15px;
padding: 9px 13px;
border: 1px solid var(--line);
border-radius: 12px;
background: rgba(8, 20, 30, 0.76);
color: var(--muted);
font-size: 9px;
font-weight: 700;
backdrop-filter: blur(12px);
transform: translateX(-50%);
}
.legend-item {
display: flex;
align-items: center;
gap: 6px;
white-space: nowrap;
}
.swatch {
width: 9px;
height: 9px;
border-radius: 3px;
}
.swatch.top {
background: var(--orange);
}
.swatch.bottom {
background: var(--cyan);
}
.swatch.dielectric {
background: var(--violet);
}
.swatch.field {
background: var(--yellow);
}
.gesture-hint {
padding-left: 13px;
border-left: 1px solid var(--line);
color: #718d9e;
white-space: nowrap;
}
.scene-label {
position: absolute;
min-width: 132px;
padding: 8px 10px;
border: 1px solid rgba(154, 211, 238, 0.18);
border-radius: 10px;
background: rgba(6, 18, 28, 0.86);
box-shadow: 0 10px 28px rgba(0, 0, 0, 0.22);
opacity: 0;
transform: translate(-50%, -100%);
transition:
border-color 160ms ease,
background 160ms ease;
backdrop-filter: blur(10px);
}
.scene-label.selected {
border-color: rgba(88, 213, 255, 0.48);
background: rgba(10, 30, 44, 0.94);
}
.scene-label-top {
display: flex;
align-items: center;
justify-content: space-between;
gap: 12px;
margin-bottom: 3px;
}
.scene-label-id {
color: var(--cyan);
font-size: 10px;
font-weight: 900;
}
.scene-label-cap {
color: #effaff;
font-size: 11px;
font-weight: 800;
font-variant-numeric: tabular-nums;
}
.scene-label-dims {
color: var(--muted);
font-size: 8px;
white-space: nowrap;
}
@media (max-width: 1050px) {
.topbar {
grid-template-columns: 1fr auto;
}
.formula-card {
display: none;
}
.control-panel {
width: 286px;
}
.comparison-panel {
width: 292px;
}
.legend {
left: 50%;
}
}
@media (max-width: 760px) {
.topbar {
min-height: 68px;
padding: 12px;
}
.brand-mark {
width: 37px;
height: 37px;
}
.brand .eyebrow,
.brand h1 {
display: none;
}
.tool-button {
padding: 9px;
font-size: 9px;
}
.control-panel {
top: auto;
right: auto;
bottom: 12px;
left: 12px;
width: min(285px, calc(100vw - 24px));
max-height: 45vh;
overflow-y: auto;
}
.comparison-panel {
top: 78px;
right: 12px;
width: 235px;
padding: 12px;
}
.comparison-name span:last-child,
.comparison-note {
display: none;
}
.legend {
display: none;
}
}
</style>
</head>
<body>
<div id="stage" aria-label="Interactive 3D parallel-plate capacitor array"></div>
<div id="labels" aria-hidden="true"></div>
<header class="topbar">
<div class="brand">
<div class="brand-mark" aria-hidden="true"></div>
<div>
<p class="eyebrow">Electric Field Laboratory</p>
<h1>Parallel-Plate Array</h1>
</div>
</div>
<div class="formula-card">
<div class="formula">
C ≈ ε<sub>0</sub> ε<sub>r</sub> A / d
<small>uniform dielectric</small>
</div>
<div class="edge-badge">Edge effects omitted</div>
</div>
<div class="toolbar">
<button id="fieldToggle" class="tool-button active" aria-pressed="true">
Field arrows
</button>
<button id="resetView" class="tool-button">
Reset view
</button>
</div>
</header>
<section class="panel control-panel" aria-labelledby="controlTitle">
<div class="panel-heading">
<div>
<h2 id="controlTitle">Tune selected capacitor</h2>
<p>Click a model in the scene or choose a letter.</p>
</div>
<div id="selectedLetter" class="selected-letter">A</div>
</div>
<div id="capTabs" class="cap-tabs" aria-label="Select capacitor"></div>
<div class="result-box">
<div class="result-caption">Capacitance</div>
<div class="result-value">
<b id="capValue">53.12</b><span>pF</span>
</div>
<div id="resultSub" class="result-sub">Uniform dielectric approximation</div>
</div>
<div class="stats-grid">
<div class="stat">
<span class="stat-label">Plate area</span>
<span id="areaValue" class="stat-value">0.0120 m²</span>
</div>
<div class="stat">
<span class="stat-label">Capacity rank</span>
<span id="rankValue" class="stat-value">1 of 4</span>
</div>
<div class="stat">
<span class="stat-label">Relative to min.</span>
<span id="ratioValue" class="stat-value">1.80×</span>
</div>
<div class="stat">
<span class="stat-label">Charge at 10 V</span>
<span id="chargeValue" class="stat-value">0.531 nC</span>
</div>
</div>
<div class="control-group">
<label class="control-label" for="widthSlider">
<span>Plate width</span>
<span id="widthValue" class="control-value">120 mm</span>
</label>
<input id="widthSlider" type="range" min="40" max="180" step="1" value="120">
</div>
<div class="control-group">
<label class="control-label" for="depthSlider">
<span>Plate depth</span>
<span id="depthValue" class="control-value">100 mm</span>
</label>
<input id="depthSlider" type="range" min="40" max="140" step="1" value="100">
</div>
<div class="control-group">
<label class="control-label" for="gapSlider">
<span>Separation d</span>
<span id="gapValue" class="control-value">2.0 mm</span>
</label>
<input id="gapSlider" type="range" min="0.5" max="6" step="0.1" value="2">
</div>
<div class="control-group">
<label class="control-label" for="epsilonSlider">
<span>Dielectric relative permittivity εr</span>
<span id="epsilonValue" class="control-value">1.0</span>
</label>
<input id="epsilonSlider" type="range" min="1" max="6" step="0.1" value="1">
</div>
<div class="button-row">
<button id="resetSelected" class="small-button">Reset model</button>
<button id="resetAll" class="small-button">Reset array</button>
</div>
<div class="assumption">
<strong>Stated approximation.</strong>
Each gap is treated as a homogeneous dielectric of relative permittivity
ε<sub>r</sub>. Fringing fields, plate-edge corrections, parasitics, and
breakdown are omitted. Plate separations are enlarged in the 3D view
for readability.
</div>
</section>
<section class="panel comparison-panel" aria-labelledby="comparisonTitle">
<div class="panel-heading">
<div>
<h2 id="comparisonTitle">Capacitance comparison</h2>
<p>Values update as geometry or εr changes.</p>
</div>
</div>
<div id="comparisonList" class="comparison-list"></div>
<div class="comparison-note">
<strong>Ideal parallel-plate model.</strong>
C = ε<sub>0</sub> ε<sub>r</sub>A/d. Larger area increases capacitance;
larger separation decreases it. Bars are normalized to the largest value.
</div>
</section>
<div class="legend" aria-hidden="true">
<div class="legend-item"><span class="swatch top"></span>Positive plate</div>
<div class="legend-item"><span class="swatch bottom"></span>Negative plate</div>
<div class="legend-item"><span class="swatch dielectric"></span>Dielectric</div>
<div class="legend-item"><span class="swatch field"></span>E field</div>
<div class="gesture-hint">Drag to orbit · wheel to zoom</div>
</div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// ------------------------------------------------------------
// Physical model and state
// ------------------------------------------------------------
const EPSILON_0 = 8.8541878128e-12;
const BASE_MODELS = [
{
id: "A",
name: "Broad / thin",
width: 120,
depth: 100,
gap: 2.0,
x: -4.5,
z: 1.6
},
{
id: "B",
name: "Compact / close",
width: 80,
depth: 60,
gap: 1.2,
x: 4.4,
z: 1.6
},
{
id: "C",
name: "Largest / widest",
width: 160,
depth: 120,
gap: 4.0,
x: -4.5,
z: -5.1
},
{
id: "D",
name: "Moderate / open",
width: 100,
depth: 100,
gap: 3.0,
x: 4.4,
z: -5.1
}
];
const state = {
models: BASE_MODELS.map(model => ({ ...model })),
epsilonR: 1,
selected: 0,
showFields: true
};
const stage = document.querySelector("#stage");
const labelsLayer = document.querySelector("#labels");
function capacitance(model) {
const areaM2 = (model.width / 1000) * (model.depth / 1000);
const gapM = model.gap / 1000;
return EPSILON_0 * state.epsilonR * areaM2 / gapM;
}
function formatPF(model) {
return capacitance(model) * 1e12;
}
function visualGap(gapMM) {
// Physical values remain in millimetres; display separation is exaggerated.
return 0.72 + gapMM * 0.11;
}
// ------------------------------------------------------------
// Renderer, scene, camera, and lighting
// ------------------------------------------------------------
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x07111a);
scene.fog = new THREE.Fog(0x07111a, 24, 45);
const camera = new THREE.PerspectiveCamera(
43,
innerWidth / innerHeight,
0.1,
100
);
const renderer = new THREE.WebGLRenderer({
antialias: true,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(devicePixelRatio, 2));
renderer.setSize(innerWidth, 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 hemisphere = new THREE.HemisphereLight(0xa7dcff, 0x07101a, 2.1);
scene.add(hemisphere);
const keyLight = new THREE.DirectionalLight(0xfff0dc, 3.2);
keyLight.position.set(7, 14, 9);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(2048, 2048);
keyLight.shadow.camera.left = -16;
keyLight.shadow.camera.right = 16;
keyLight.shadow.camera.top = 15;
keyLight.shadow.camera.bottom = -15;
keyLight.shadow.camera.near = 1;
keyLight.shadow.camera.far = 42;
keyLight.shadow.bias = -0.00035;
scene.add(keyLight);
const rimLight = new THREE.DirectionalLight(0x6e8cff, 1.4);
rimLight.position.set(-11, 7, -11);
scene.add(rimLight);
const warmPoint = new THREE.PointLight(0xff795b, 15, 18, 2);
warmPoint.position.set(-7, 5, 3);
scene.add(warmPoint);
const coolPoint = new THREE.PointLight(0x43c9ff, 13, 18, 2);
coolPoint.position.set(8, 4, -6);
scene.add(coolPoint);
const floorMaterial = new THREE.MeshStandardMaterial({
color: 0x08141e,
roughness: 0.84,
metalness: 0.12
});
const floor = new THREE.Mesh(
new THREE.PlaneGeometry(42, 34),
floorMaterial
);
floor.rotation.x = -Math.PI / 2;
floor.position.set(0, -0.08, -2);
floor.receiveShadow = true;
scene.add(floor);
const grid = new THREE.GridHelper(38, 38, 0x2b5263, 0x17313d);
grid.position.set(0, -0.065, -2);
grid.material.transparent = true;
grid.material.opacity = 0.28;
scene.add(grid);
const stagePad = new THREE.Mesh(
new THREE.BoxGeometry(22, 0.13, 16),
new THREE.MeshStandardMaterial({
color: 0x0a1924,
roughness: 0.72,
metalness: 0.18
})
);
stagePad.position.set(0, -0.12, -1.75);
stagePad.receiveShadow = true;
scene.add(stagePad);
// ------------------------------------------------------------
// Custom orbit camera controls, implemented without addons
// ------------------------------------------------------------
const cameraTarget = new THREE.Vector3(0, 0.65, -2.1);
const cameraAngles = {
theta: 0.63,
phi: 1.03,
radius: 22
};
function updateCamera() {
cameraAngles.phi = THREE.MathUtils.clamp(
cameraAngles.phi,
0.34,
1.43
);
cameraAngles.radius = THREE.MathUtils.clamp(
cameraAngles.radius,
12,
36
);
const sinPhi = Math.sin(cameraAngles.phi);
camera.position.set(
cameraTarget.x +
cameraAngles.radius * sinPhi * Math.sin(cameraAngles.theta),
cameraTarget.y +
cameraAngles.radius * Math.cos(cameraAngles.phi),
cameraTarget.z +
cameraAngles.radius * sinPhi * Math.cos(cameraAngles.theta)
);
camera.lookAt(cameraTarget);
}
function resetCamera() {
cameraAngles.theta = 0.63;
cameraAngles.phi = 1.03;
cameraAngles.radius = 22;
cameraTarget.set(0, 0.65, -2.1);
updateCamera();
}
updateCamera();
const canvas = renderer.domElement;
const raycaster = new THREE.Raycaster();
const pointer = new THREE.Vector2();
const dragState = {
active: false,
pointerId: null,
startX: 0,
startY: 0,
lastX: 0,
lastY: 0,
moved: false
};
canvas.addEventListener("pointerdown", event => {
if (event.button !== 0) return;
dragState.active = true;
dragState.pointerId = event.pointerId;
dragState.startX = event.clientX;
dragState.startY = event.clientY;
dragState.lastX = event.clientX;
dragState.lastY = event.clientY;
dragState.moved = false;
canvas.setPointerCapture(event.pointerId);
canvas.style.cursor = "grabbing";
});
canvas.addEventListener("pointermove", event => {
if (!dragState.active || event.pointerId !== dragState.pointerId) return;
const dx = event.clientX - dragState.lastX;
const dy = event.clientY - dragState.lastY;
dragState.lastX = event.clientX;
dragState.lastY = event.clientY;
if (
Math.hypot(
event.clientX - dragState.startX,
event.clientY - dragState.startY
) > 4
) {
dragState.moved = true;
}
cameraAngles.theta -= dx * 0.006;
cameraAngles.phi += dy * 0.005;
updateCamera();
});
canvas.addEventListener("pointerup", event => {
if (!dragState.active || event.pointerId !== dragState.pointerId) return;
canvas.releasePointerCapture(event.pointerId);
canvas.style.cursor = "grab";
if (!dragState.moved) {
selectFromPointer(event.clientX, event.clientY);
}
dragState.active = false;
dragState.pointerId = null;
});
canvas.addEventListener("pointercancel", () => {
dragState.active = false;
dragState.pointerId = null;
canvas.style.cursor = "grab";
});
canvas.addEventListener("wheel", event => {
event.preventDefault();
cameraAngles.radius *= Math.exp(event.deltaY * 0.001);
updateCamera();
}, { passive: false });
canvas.addEventListener("dblclick", resetCamera);
canvas.style.cursor = "grab";
// ------------------------------------------------------------
// Stylized solid capacitor models
// ------------------------------------------------------------
const capGroups = new Map();
const labelElements = new Map();
function roundedRectangleShape(width, height, radius) {
const w = width / 2;
const h = height / 2;
const r = Math.min(radius, w * 0.24, h * 0.24);
const shape = new THREE.Shape();
shape.moveTo(-w + r, -h);
shape.lineTo(w - r, -h);
shape.quadraticCurveTo(w, -h, w, -h + r);
shape.lineTo(w, h - r);
shape.quadraticCurveTo(w, h, w - r, h);
shape.lineTo(-w + r, h);
shape.quadraticCurveTo(-w, h, -w, h - r);
shape.lineTo(-w, -h + r);
shape.quadraticCurveTo(-w, -h, -w + r, -h);
return shape;
}
function plateGeometry(width, depth, thickness) {
const shape = roundedRectangleShape(width, depth, 0.28);
const geometry = new THREE.ExtrudeGeometry(shape, {
depth: thickness,
steps: 1,
bevelEnabled: true,
bevelSegments: 2,
bevelSize: 0.045,
bevelThickness: 0.035,
curveSegments: 8
});
geometry.rotateX(-Math.PI / 2);
geometry.translate(0, -thickness / 2, 0);
geometry.computeVertexNormals();
return geometry;
}
function addFieldArrow(fieldGroup, x, z, gapHeight, fieldMaterial) {
const shaftLength = Math.max(0.28, gapHeight * 0.66);
const shaft = new THREE.Mesh(
new THREE.CylinderGeometry(0.018, 0.018, shaftLength, 8),
fieldMaterial
);
shaft.position.set(x, 0.52 + gapHeight * 0.54, z);
fieldGroup.add(shaft);
const arrow = new THREE.Mesh(
new THREE.ConeGeometry(0.075, 0.19, 10),
fieldMaterial
);
arrow.rotation.z = Math.PI;
arrow.position.set(x, 0.52 + 0.12, z);
fieldGroup.add(arrow);
}
function createCapModel(model, index) {
const group = new THREE.Group();
group.position.set(model.x, 0, model.z);
group.userData.capIndex = index;
const worldWidth = model.width / 20;
const worldDepth = model.depth / 20;
const gapHeight = visualGap(model.gap);
const thickness = 0.18;
const topMaterial = new THREE.MeshStandardMaterial({
color: 0xff8b69,
roughness: 0.25,
metalness: 0.66,
emissive: 0xff593a,
emissiveIntensity: index === state.selected ? 0.16 : 0.035
});
const bottomMaterial = new THREE.MeshStandardMaterial({
color: 0x50caff,
roughness: 0.27,
metalness: 0.68,
emissive: 0x168fce,
emissiveIntensity: index === state.selected ? 0.18 : 0.04
});
const dielectricMaterial = new THREE.MeshPhysicalMaterial({
color: 0xa695ff,
roughness: 0.18,
metalness: 0,
transparent: true,
opacity: 0.24,
transmission: 0.08,
thickness: 0.5,
depthWrite: false,
side: THREE.DoubleSide
});
const pedestalMaterial = new THREE.MeshStandardMaterial({
color: 0x172c3b,
roughness: 0.48,
metalness: 0.56
});
const ringMaterial = new THREE.MeshStandardMaterial({
color: 0x3a7188,
emissive: 0x174b61,
emissiveIntensity: 0.35,
roughness: 0.3,
metalness: 0.65
});
const fieldMaterial = new THREE.MeshStandardMaterial({
color: 0xf6d66f,
emissive: 0xb48d20,
emissiveIntensity: 0.85,
roughness: 0.34,
transparent: true,
opacity: 0.82
});
const pedestalRadius = Math.min(worldWidth, worldDepth) * 0.23;
const pedestal = new THREE.Mesh(
new THREE.CylinderGeometry(
pedestalRadius,
pedestalRadius * 1.06,
0.28,
40
),
pedestalMaterial
);
pedestal.position.y = 0.14;
pedestal.castShadow = true;
pedestal.receiveShadow = true;
group.add(pedestal);
const pedestalRing = new THREE.Mesh(
new THREE.TorusGeometry(pedestalRadius * 0.84, 0.024, 8, 56),
ringMaterial
);
pedestalRing.rotation.x = Math.PI / 2;
pedestalRing.position.y = 0.292;
group.add(pedestalRing);
const halo = new THREE.Mesh(
new THREE.TorusGeometry(pedestalRadius * 1.08, 0.026, 8, 64),
new THREE.MeshBasicMaterial({
color: 0x58d5ff,
transparent: true,
opacity: 0.82
})
);
halo.rotation.x = Math.PI / 2;
halo.position.y = 0.035;
halo.visible = index === state.selected;
group.add(halo);
const bottomPlate = new THREE.Mesh(
plateGeometry(worldWidth, worldDepth, thickness),
bottomMaterial
);
bottomPlate.position.y = 0.52;
bottomPlate.castShadow = true;
bottomPlate.receiveShadow = true;
group.add(bottomPlate);
const topPlate = new THREE.Mesh(
plateGeometry(worldWidth, worldDepth, thickness),
topMaterial
);
topPlate.position.y = 0.52 + gapHeight;
topPlate.castShadow = true;
topPlate.receiveShadow = true;
group.add(topPlate);
const dielectricHeight = Math.max(0.22, gapHeight - 0.15);
const dielectric = new THREE.Mesh(
new THREE.BoxGeometry(
worldWidth * 0.92,
dielectricHeight,
worldDepth * 0.92
),
dielectricMaterial
);
dielectric.position.y = 0.52 + gapHeight / 2;
dielectric.renderOrder = 1;
group.add(dielectric);
const fieldGroup = new THREE.Group();
fieldGroup.visible = state.showFields;
const columns = THREE.MathUtils.clamp(
Math.round(worldWidth / 1.65),
2,
4
);
const rows = THREE.MathUtils.clamp(
Math.round(worldDepth / 1.65),
2,
4
);
for (let ix = 0; ix < columns; ix++) {
for (let iz = 0; iz < rows; iz++) {
const x =
columns === 1
? 0
: -worldWidth * 0.31 + (ix / (columns - 1)) * worldWidth * 0.62;
const z =
rows === 1
? 0
: -worldDepth * 0.31 + (iz / (rows - 1)) * worldDepth * 0.62;
addFieldArrow(fieldGroup, x, z, gapHeight, fieldMaterial);
}
}
group.add(fieldGroup);
group.userData.topMaterial = topMaterial;
group.userData.bottomMaterial = bottomMaterial;
group.userData.dielectricMaterial = dielectricMaterial;
group.userData.fieldGroup = fieldGroup;
group.userData.halo = halo;
scene.add(group);
capGroups.set(index, group);
}
function disposeGroup(group) {
group.traverse(object => {
if (object.geometry) object.geometry.dispose();
if (object.material) {
const materials = Array.isArray(object.material)
? object.material
: [object.material];
materials.forEach(material => material.dispose());
}
});
scene.remove(group);
}
function rebuildCap(index) {
const oldGroup = capGroups.get(index);
if (oldGroup) disposeGroup(oldGroup);
createCapModel(state.models[index], index);
}
function rebuildAllCaps() {
for (const group of capGroups.values()) disposeGroup(group);
capGroups.clear();
state.models.forEach((model, index) => {
createCapModel(model, index);
});
}
// ------------------------------------------------------------
// Screen-space labels
// ------------------------------------------------------------
for (const model of state.models) {
const element = document.createElement("div");
element.className = "scene-label";
element.innerHTML = `
<div class="scene-label-top">
<span class="scene-label-id">${model.id}</span>
<span class="scene-label-cap"></span>
</div>
<div class="scene-label-dims"></div>
`;
labelsLayer.appendChild(element);
labelElements.set(model.id, element);
}
const projected = new THREE.Vector3();
function updateLabels() {
state.models.forEach(model => {
const label = labelElements.get(model.id);
const anchorY = 0.52 + visualGap(model.gap) + 0.72;
projected.set(model.x, anchorY, model.z).project(camera);
const visible =
projected.z > -1 &&
projected.z < 1 &&
projected.x > -1.15 &&
projected.x < 1.15 &&
projected.y > -1.15 &&
projected.y < 1.15;
label.style.opacity = visible ? "1" : "0";
label.style.left = `${(projected.x * 0.5 + 0.5) * innerWidth}px`;
label.style.top = `${(-projected.y * 0.5 + 0.5) * innerHeight}px`;
});
}
// ------------------------------------------------------------
// Selection and UI
// ------------------------------------------------------------
const capTabs = document.querySelector("#capTabs");
const comparisonList = document.querySelector("#comparisonList");
const selectedLetter = document.querySelector("#selectedLetter");
const capValue = document.querySelector("#capValue");
const resultSub = document.querySelector("#resultSub");
const areaValue = document.querySelector("#areaValue");
const rankValue = document.querySelector("#rankValue");
const ratioValue = document.querySelector("#ratioValue");
const chargeValue = document.querySelector("#chargeValue");
const widthSlider = document.querySelector("#widthSlider");
const depthSlider = document.querySelector("#depthSlider");
const gapSlider = document.querySelector("#gapSlider");
const epsilonSlider = document.querySelector("#epsilonSlider");
const widthValue = document.querySelector("#widthValue");
const depthValue = document.querySelector("#depthValue");
const gapValue = document.querySelector("#gapValue");
const epsilonValue = document.querySelector("#epsilonValue");
function selectCap(index) {
state.selected = index;
capGroups.forEach((group, groupIndex) => {
const selected = groupIndex === index;
group.userData.topMaterial.emissiveIntensity = selected ? 0.16 : 0.035;
group.userData.bottomMaterial.emissiveIntensity = selected ? 0.18 : 0.04;
group.userData.halo.visible = selected;
});
renderUI();
}
function selectFromPointer(clientX, clientY) {
pointer.x = (clientX / innerWidth) * 2 - 1;
pointer.y = -(clientY / innerHeight) * 2 + 1;
raycaster.setFromCamera(pointer, camera);
const intersections = raycaster.intersectObjects(
Array.from(capGroups.values()),
true
);
if (intersections.length === 0) return;
let object = intersections[0].object;
while (object && object.userData.capIndex === undefined) {
object = object.parent;
}
if (object) selectCap(object.userData.capIndex);
}
function renderUI() {
const model = state.models[state.selected];
const selectedCapacitance = capacitance(model);
selectedLetter.textContent = model.id;
capValue.textContent = formatPF(model).toFixed(2);
resultSub.textContent = `${model.name} · C ≈ ε₀ εᵣ A/d`;
const areaM2 = (model.width / 1000) * (model.depth / 1000);
areaValue.textContent = `${areaM2.toFixed(4)} m²`;
const values = state.models.map(capacitance);
const sorted = [...values].sort((a, b) => b - a);
const rank = sorted.indexOf(selectedCapacitance) + 1;
const minimum = Math.min(...values);
rankValue.textContent = `${rank} of ${state.models.length}`;
ratioValue.textContent = `${(selectedCapacitance / minimum).toFixed(2)}×`;
chargeValue.textContent = `${(selectedCapacitance * 10 * 1e9).toFixed(3)} nC`;
widthSlider.value = model.width;
depthSlider.value = model.depth;
gapSlider.value = model.gap;
epsilonSlider.value = state.epsilonR;
widthValue.textContent = `${model.width.toFixed(0)} mm`;
depthValue.textContent = `${model.depth.toFixed(0)} mm`;
gapValue.textContent = `${model.gap.toFixed(1)} mm`;
epsilonValue.textContent = state.epsilonR.toFixed(1);
capTabs.innerHTML = state.models.map((item, index) => `
<button
class="cap-tab ${index === state.selected ? "active" : ""}"
data-index="${index}"
aria-pressed="${index === state.selected}"
>${item.id}</button>
`).join("");
const maximum = Math.max(...values);
comparisonList.innerHTML = state.models.map((item, index) => {
const value = values[index];
const percent = Math.max(2, value / maximum * 100);
return `
<button
class="comparison-row ${index === state.selected ? "selected" : ""}"
data-index="${index}"
>
<span class="comparison-id">${item.id}</span>
<span class="comparison-main">
<span class="comparison-name">
<span>${item.name}</span>
<span>${(value / minimum).toFixed(2)}× min.</span>
</span>
<span class="bar-track">
<span class="bar-fill" style="width:${percent}%"></span>
</span>
</span>
<span class="comparison-value">${(value * 1e12).toFixed(1)} pF</span>
</button>
`;
}).join("");
state.models.forEach(item => {
const label = labelElements.get(item.id);
const index = state.models.indexOf(item);
label.classList.toggle("selected", index === state.selected);
label.querySelector(".scene-label-cap").textContent =
`${(capacitance(item) * 1e12).toFixed(1)} pF`;
label.querySelector(".scene-label-dims").textContent =
`${item.width.toFixed(0)} × ${item.depth.toFixed(0)} mm · d ${item.gap.toFixed(1)} mm`;
});
capGroups.forEach((group, index) => {
const dielectricMaterial = group.userData.dielectricMaterial;
const normalized = (state.epsilonR - 1) / 5;
dielectricMaterial.opacity = 0.19 + normalized * 0.12;
dielectricMaterial.color.setHSL(
0.69 - normalized * 0.16,
0.78,
0.67
);
});
}
capTabs.addEventListener("click", event => {
const button = event.target.closest("[data-index]");
if (button) selectCap(Number(button.dataset.index));
});
comparisonList.addEventListener("click", event => {
const button = event.target.closest("[data-index]");
if (button) selectCap(Number(button.dataset.index));
});
widthSlider.addEventListener("input", event => {
state.models[state.selected].width = Number(event.target.value);
rebuildCap(state.selected);
renderUI();
});
depthSlider.addEventListener("input", event => {
state.models[state.selected].depth = Number(event.target.value);
rebuildCap(state.selected);
renderUI();
});
gapSlider.addEventListener("input", event => {
state.models[state.selected].gap = Number(event.target.value);
rebuildCap(state.selected);
renderUI();
});
epsilonSlider.addEventListener("input", event => {
state.epsilonR = Number(event.target.value);
renderUI();
});
document.querySelector("#resetSelected").addEventListener("click", () => {
const base = BASE_MODELS[state.selected];
const current = state.models[state.selected];
current.width = base.width;
current.depth = base.depth;
current.gap = base.gap;
rebuildCap(state.selected);
renderUI();
});
document.querySelector("#resetAll").addEventListener("click", () => {
state.models = BASE_MODELS.map(model => ({ ...model }));
state.epsilonR = 1;
state.selected = 0;
rebuildAllCaps();
selectCap(0);
resetCamera();
});
document.querySelector("#resetView").addEventListener("click", resetCamera);
const fieldToggle = document.querySelector("#fieldToggle");
fieldToggle.addEventListener("click", () => {
state.showFields = !state.showFields;
fieldToggle.classList.toggle("active", state.showFields);
fieldToggle.setAttribute("aria-pressed", String(state.showFields));
fieldToggle.textContent = state.showFields ? "Field arrows" : "Fields hidden";
capGroups.forEach(group => {
group.userData.fieldGroup.visible = state.showFields;
});
});
// ------------------------------------------------------------
// Resize and render loop
// ------------------------------------------------------------
function resize() {
camera.aspect = innerWidth / innerHeight;
camera.updateProjectionMatrix();
renderer.setPixelRatio(Math.min(devicePixelRatio, 2));
renderer.setSize(innerWidth, innerHeight);
}
addEventListener("resize", resize);
rebuildAllCaps();
selectCap(0);
renderer.setAnimationLoop(() => {
updateLabels();
renderer.render(scene, camera);
});
</script>
</body>
</html>
```