Prompt specification
Build or customize the Three.js 3D scene "Capacitance geometry exploration tool" 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: Capacitance geometry exploration tool
Category: Science & Physics · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/capacitance-geometry-exploration-tool-x1608
Scene Description Prompt:
Please use Three.js from cdnjs for a low-poly scene based on this idea: capacitance geometry exploration tool. Allow adjusting plate area, spacing, and dielectric constant; compute ideal capacitance and stored energy under a chosen voltage constraint, clearly identifying what is held fixed. Return one self-contained HTML file.
Working Three.js Starter (capacitance-geometry-exploration-tool-x1608):
```html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Capacitance Geometry Explorer</title>
<style>
:root {
color-scheme: dark;
--bg: #071016;
--panel: #0b151d;
--panel-2: #101f29;
--border: rgba(157, 201, 214, 0.14);
--text: #e9f3f5;
--muted: #8da5af;
--cyan: #4dd8e8;
--amber: #ffad68;
--yellow: #f7db76;
--green: #8ce5b5;
--danger: #ff8f82;
}
* {
box-sizing: border-box;
}
html,
body {
margin: 0;
min-height: 100%;
background: var(--bg);
color: var(--text);
font-family:
Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
button,
input {
font: inherit;
}
.app {
display: grid;
grid-template-columns: 380px minmax(0, 1fr);
height: 100dvh;
overflow: hidden;
}
.panel {
position: relative;
z-index: 10;
overflow-y: auto;
padding: 24px 22px 30px;
background:
linear-gradient(180deg, rgba(13, 27, 36, 0.98), rgba(8, 17, 24, 0.98));
border-right: 1px solid var(--border);
box-shadow: 18px 0 50px rgba(0, 0, 0, 0.2);
scrollbar-width: thin;
scrollbar-color: #28404c transparent;
}
.eyebrow {
margin: 0 0 9px;
color: var(--cyan);
font-size: 0.72rem;
font-weight: 800;
letter-spacing: 0.16em;
text-transform: uppercase;
}
h1 {
margin: 0;
font-size: clamp(1.65rem, 3vw, 2.15rem);
line-height: 1.08;
letter-spacing: -0.04em;
}
.intro {
margin: 12px 0 22px;
color: var(--muted);
font-size: 0.91rem;
line-height: 1.55;
}
.constraint-card {
position: relative;
overflow: hidden;
padding: 16px;
margin-bottom: 21px;
border: 1px solid rgba(77, 216, 232, 0.27);
border-radius: 15px;
background:
radial-gradient(circle at 100% 0%, rgba(77, 216, 232, 0.14), transparent 45%),
linear-gradient(145deg, rgba(20, 49, 59, 0.82), rgba(12, 29, 38, 0.86));
}
.constraint-card::after {
content: "";
position: absolute;
width: 110px;
height: 110px;
right: -48px;
bottom: -54px;
border: 1px solid rgba(77, 216, 232, 0.18);
border-radius: 50%;
pointer-events: none;
}
.constraint-top {
display: flex;
align-items: center;
justify-content: space-between;
gap: 12px;
}
.fixed-badge {
display: inline-flex;
align-items: center;
gap: 7px;
color: var(--cyan);
font-size: 0.67rem;
font-weight: 850;
letter-spacing: 0.13em;
text-transform: uppercase;
}
.fixed-badge::before {
content: "";
width: 7px;
height: 7px;
border-radius: 50%;
background: var(--cyan);
box-shadow: 0 0 12px var(--cyan);
}
.constraint-value {
margin: 10px 0 12px;
font-size: 1.8rem;
font-weight: 760;
letter-spacing: -0.035em;
}
.constraint-note {
margin: 0;
color: #b4c8ce;
font-size: 0.78rem;
line-height: 1.5;
}
.constraint-note strong {
color: var(--text);
}
.section-title {
display: flex;
align-items: center;
gap: 10px;
margin: 0 0 15px;
color: #d7e6e9;
font-size: 0.76rem;
font-weight: 800;
letter-spacing: 0.12em;
text-transform: uppercase;
}
.section-title::after {
content: "";
flex: 1;
height: 1px;
background: var(--border);
}
.control {
margin-bottom: 17px;
}
.control-heading {
display: flex;
align-items: baseline;
justify-content: space-between;
gap: 12px;
margin-bottom: 8px;
}
.control label {
color: #c7d8dc;
font-size: 0.85rem;
font-weight: 650;
}
output {
color: var(--text);
font-variant-numeric: tabular-nums;
font-size: 0.88rem;
font-weight: 750;
}
input[type="range"] {
width: 100%;
height: 5px;
margin: 5px 0 0;
border-radius: 999px;
background: #213540;
accent-color: var(--cyan);
cursor: pointer;
}
#voltage {
accent-color: var(--amber);
}
.range-limits {
display: flex;
justify-content: space-between;
margin-top: 5px;
color: #607984;
font-size: 0.64rem;
font-variant-numeric: tabular-nums;
}
.control-hint {
margin: 6px 0 0;
color: #718b95;
font-size: 0.69rem;
line-height: 1.4;
}
.results {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 9px;
margin-top: 22px;
}
.result {
min-width: 0;
padding: 13px;
border: 1px solid var(--border);
border-radius: 12px;
background: rgba(255, 255, 255, 0.025);
}
.result-label {
display: block;
margin-bottom: 6px;
color: var(--muted);
font-size: 0.68rem;
font-weight: 700;
letter-spacing: 0.07em;
text-transform: uppercase;
}
.result strong {
display: block;
overflow-wrap: anywhere;
color: var(--text);
font-size: 1.2rem;
font-variant-numeric: tabular-nums;
letter-spacing: -0.03em;
}
.result.capacitance strong {
color: var(--cyan);
}
.result.energy strong {
color: var(--amber);
}
.result.field strong {
color: var(--yellow);
}
.result.charge strong {
color: var(--green);
}
.equations {
margin-top: 13px;
padding: 13px 14px;
border-left: 2px solid rgba(77, 216, 232, 0.55);
border-radius: 0 9px 9px 0;
background: rgba(77, 216, 232, 0.045);
color: #9fb6be;
font-size: 0.72rem;
line-height: 1.7;
}
.equations code {
color: #d7eef2;
font-family: "SFMono-Regular", Consolas, monospace;
font-size: 0.71rem;
}
.model-note {
margin: 15px 0 0;
color: #6f8791;
font-size: 0.68rem;
line-height: 1.55;
}
.viewport {
position: relative;
min-width: 0;
overflow: hidden;
background:
radial-gradient(circle at 50% 44%, rgba(33, 76, 88, 0.25), transparent 42%),
radial-gradient(circle at 80% 12%, rgba(255, 174, 104, 0.08), transparent 30%),
linear-gradient(145deg, #09151d, #050c11 75%);
}
.scene-canvas {
position: absolute;
inset: 0;
width: 100%;
height: 100%;
display: block;
touch-action: none;
cursor: grab;
outline: none;
}
.scene-canvas:active {
cursor: grabbing;
}
.scene-canvas:focus-visible {
outline: 2px solid rgba(77, 216, 232, 0.65);
outline-offset: -3px;
}
.viewport-hud {
position: absolute;
z-index: 4;
top: 20px;
left: 22px;
pointer-events: none;
}
.viewport-hud .eyebrow {
margin-bottom: 5px;
}
.viewport-hud p {
margin: 0;
color: #78919b;
font-size: 0.73rem;
}
.legend {
position: absolute;
z-index: 4;
right: 20px;
bottom: 20px;
display: grid;
gap: 8px;
padding: 12px 14px;
border: 1px solid var(--border);
border-radius: 11px;
background: rgba(7, 15, 21, 0.74);
backdrop-filter: blur(8px);
pointer-events: none;
}
.legend-item {
display: flex;
align-items: center;
gap: 9px;
color: #a6bbc2;
font-size: 0.69rem;
}
.swatch {
width: 17px;
height: 4px;
border-radius: 5px;
}
.swatch.positive {
background: var(--amber);
box-shadow: 0 0 8px rgba(255, 173, 104, 0.45);
}
.swatch.negative {
background: var(--cyan);
box-shadow: 0 0 8px rgba(77, 216, 232, 0.45);
}
.swatch.dielectric {
background: rgba(247, 219, 118, 0.65);
}
.swatch.field {
background: var(--yellow);
}
.camera-hint {
position: absolute;
z-index: 4;
left: 22px;
bottom: 20px;
padding: 9px 11px;
border: 1px solid var(--border);
border-radius: 9px;
color: #78919b;
background: rgba(7, 15, 21, 0.65);
font-size: 0.68rem;
pointer-events: none;
}
.scene-label {
position: absolute;
z-index: 3;
top: 0;
left: 0;
padding: 6px 9px;
border: 1px solid rgba(173, 207, 216, 0.17);
border-radius: 7px;
color: #c7dadd;
background: rgba(7, 16, 22, 0.78);
box-shadow: 0 6px 20px rgba(0, 0, 0, 0.18);
font-size: 0.66rem;
font-weight: 700;
letter-spacing: 0.035em;
white-space: nowrap;
transform: translate(-50%, -50%);
pointer-events: none;
backdrop-filter: blur(5px);
}
.scene-label.positive {
color: #ffd1aa;
border-color: rgba(255, 173, 104, 0.35);
}
.scene-label.negative {
color: #a8f2fa;
border-color: rgba(77, 216, 232, 0.35);
}
.scene-label.dielectric {
color: #f9e9a7;
border-color: rgba(247, 219, 118, 0.28);
}
.loading {
position: absolute;
z-index: 20;
inset: 0;
display: grid;
place-items: center;
color: #9bb1ba;
background: #071016;
font-size: 0.8rem;
letter-spacing: 0.08em;
text-transform: uppercase;
transition: opacity 300ms ease;
}
.loading.hidden {
opacity: 0;
pointer-events: none;
}
@media (max-width: 880px) {
.app {
display: flex;
flex-direction: column;
height: auto;
min-height: 100dvh;
overflow: visible;
}
.viewport {
order: 0;
height: 58dvh;
min-height: 430px;
}
.panel {
order: 1;
overflow: visible;
border-right: 0;
border-top: 1px solid var(--border);
}
.camera-hint {
display: none;
}
.legend {
right: 12px;
bottom: 12px;
}
.viewport-hud {
top: 14px;
left: 15px;
}
}
@media (max-width: 460px) {
.panel {
padding: 20px 16px 26px;
}
.viewport {
min-height: 390px;
}
.results {
grid-template-columns: 1fr 1fr;
}
.scene-label {
font-size: 0.59rem;
padding: 5px 7px;
}
}
</style>
</head>
<body>
<main class="app">
<aside class="panel">
<p class="eyebrow">Electrostatic Explorer</p>
<h1>Capacitance<br>Geometry Lab</h1>
<p class="intro">
Explore an ideal parallel-plate capacitor while its voltage difference
remains constrained.
</p>
<section class="constraint-card" aria-labelledby="constraintTitle">
<div class="constraint-top">
<span class="fixed-badge" id="constraintTitle">Fixed quantity</span>
<span class="fixed-badge" style="color: var(--amber)">ΔV</span>
</div>
<div class="constraint-value" id="voltageOut">120 V</div>
<input
id="voltage"
type="range"
min="1"
max="500"
step="1"
value="120"
aria-label="Fixed voltage difference"
>
<div class="range-limits">
<span>1 V</span>
<span>500 V</span>
</div>
<p class="constraint-note">
<strong>Held fixed:</strong> voltage difference.
<strong>User-adjustable:</strong> area, spacing, and dielectric constant.
Charge, field, and stored energy are calculated outputs.
</p>
</section>
<h2 class="section-title">Geometry & material</h2>
<div class="control">
<div class="control-heading">
<label for="area">Plate area</label>
<output id="areaOut" for="area">100 cm²</output>
</div>
<input
id="area"
type="range"
min="25"
max="1000"
step="5"
value="100"
>
<div class="range-limits">
<span>25 cm²</span>
<span>1,000 cm²</span>
</div>
<p class="control-hint">Square parallel plates; area is A = a × a.</p>
</div>
<div class="control">
<div class="control-heading">
<label for="spacing">Plate spacing</label>
<output id="spacingOut" for="spacing">1.0 mm</output>
</div>
<input
id="spacing"
type="range"
min="0.1"
max="10"
step="0.1"
value="1"
>
<div class="range-limits">
<span>0.1 mm</span>
<span>10 mm</span>
</div>
</div>
<div class="control">
<div class="control-heading">
<label for="er">Relative permittivity, εr</label>
<output id="erOut" for="er">1.0</output>
</div>
<input
id="er"
type="range"
min="1"
max="10"
step="0.1"
value="1"
>
<div class="range-limits">
<span>1.0 · vacuum-like</span>
<span>10.0</span>
</div>
<p class="control-hint" id="permittivityHint">
Absolute permittivity: ε = ε₀.
</p>
</div>
<h2 class="section-title">Calculated outputs</h2>
<div class="results" aria-live="polite">
<div class="result capacitance">
<span class="result-label">Capacitance</span>
<strong id="capValue">—</strong>
</div>
<div class="result energy">
<span class="result-label">Stored energy</span>
<strong id="energyValue">—</strong>
</div>
<div class="result field">
<span class="result-label">Electric field</span>
<strong id="fieldValue">—</strong>
</div>
<div class="result charge">
<span class="result-label">Charge</span>
<strong id="chargeValue">—</strong>
</div>
</div>
<div class="equations">
<div><code>C = ε₀ εr A / d</code></div>
<div><code>U = ½ C ΔV²</code></div>
<div><code>E = ΔV / d</code> <code>Q = C ΔV</code></div>
</div>
<p class="model-note">
Ideal model: uniform field, no fringing, negligible plate thickness, and
linear dielectric behavior. Dielectric breakdown and losses are not
modeled. The 3D spacing and area use a schematic visual scale.
</p>
</aside>
<section class="viewport" id="viewport" aria-label="Interactive capacitor visualization">
<div class="viewport-hud">
<p class="eyebrow">Parallel-Plate Capacitor</p>
<p>Fixed-voltage constraint</p>
</div>
<div class="scene-label positive" id="positiveLabel">+ PLATE</div>
<div class="scene-label negative" id="negativeLabel">− PLATE</div>
<div class="scene-label" id="areaLabel">A = 100 cm²</div>
<div class="scene-label" id="gapLabel">d = 1.0 mm</div>
<div class="scene-label dielectric" id="dielectricLabel">εr = 1.0</div>
<div class="legend" aria-hidden="true">
<div class="legend-item">
<span class="swatch positive"></span>
Positive electrode
</div>
<div class="legend-item">
<span class="swatch negative"></span>
Negative electrode
</div>
<div class="legend-item">
<span class="swatch dielectric"></span>
Dielectric region
</div>
<div class="legend-item">
<span class="swatch field"></span>
Field direction
</div>
</div>
<div class="camera-hint">
Drag to orbit · Wheel to zoom · Double-click to reset
</div>
<div class="loading" id="loading">Loading 3D scene…</div>
</section>
</main>
<script type="module">
import * as THREE from "https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
const EPSILON_0 = 8.8541878128e-12;
const $ = (id) => document.getElementById(id);
const viewport = $("viewport");
const controls = {
area: $("area"),
spacing: $("spacing"),
er: $("er"),
voltage: $("voltage")
};
const scene = new THREE.Scene();
scene.fog = new THREE.FogExp2(0x071016, 0.025);
const camera = new THREE.PerspectiveCamera(
43,
viewport.clientWidth / viewport.clientHeight,
0.1,
100
);
const renderer = new THREE.WebGLRenderer({
antialias: true,
alpha: true,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setClearColor(0x000000, 0);
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.domElement.className = "scene-canvas";
renderer.domElement.tabIndex = 0;
renderer.domElement.setAttribute(
"aria-label",
"Interactive 3D capacitor. Drag to orbit, scroll to zoom, or use arrow keys."
);
viewport.appendChild(renderer.domElement);
// Lighting
scene.add(new THREE.HemisphereLight(0xb9e5ef, 0x15212a, 1.5));
const keyLight = new THREE.DirectionalLight(0xffffff, 3.0);
keyLight.position.set(6, 9, 7);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(1024, 1024);
keyLight.shadow.camera.left = -10;
keyLight.shadow.camera.right = 10;
keyLight.shadow.camera.top = 10;
keyLight.shadow.camera.bottom = -10;
keyLight.shadow.camera.near = 0.5;
keyLight.shadow.camera.far = 30;
keyLight.shadow.bias = -0.0004;
scene.add(keyLight);
const rimLight = new THREE.PointLight(0x45d6e8, 25, 22, 2);
rimLight.position.set(-6, 3, -5);
scene.add(rimLight);
const warmLight = new THREE.PointLight(0xff9b57, 18, 18, 2);
warmLight.position.set(5, 1, 4);
scene.add(warmLight);
// Low-poly display platform
const platform = new THREE.Mesh(
new THREE.CylinderGeometry(7.8, 8.05, 0.22, 12),
new THREE.MeshStandardMaterial({
color: 0x0d2029,
roughness: 0.92,
metalness: 0.12,
flatShading: true
})
);
platform.position.y = -2.8;
platform.receiveShadow = true;
scene.add(platform);
const grid = new THREE.GridHelper(16, 32, 0x2a7782, 0x16333c);
grid.position.y = -2.675;
grid.material.transparent = true;
grid.material.opacity = 0.26;
scene.add(grid);
const apparatus = new THREE.Group();
scene.add(apparatus);
const unitBox = new THREE.BoxGeometry(1, 1, 1);
function createPlate(color, edgeColor) {
const group = new THREE.Group();
const material = new THREE.MeshStandardMaterial({
color,
roughness: 0.32,
metalness: 0.68,
flatShading: true
});
const mesh = new THREE.Mesh(unitBox, material);
mesh.castShadow = true;
mesh.receiveShadow = true;
group.add(mesh);
const edgeMaterial = new THREE.LineBasicMaterial({
color: edgeColor,
transparent: true,
opacity: 0.9
});
const edges = new THREE.LineSegments(
new THREE.EdgesGeometry(unitBox),
edgeMaterial
);
mesh.add(edges);
return group;
}
const positivePlate = createPlate(0xd98143, 0xffc08d);
const negativePlate = createPlate(0x2ca9ba, 0x8cf2ff);
apparatus.add(positivePlate, negativePlate);
const dielectricMaterial = new THREE.MeshStandardMaterial({
color: 0xf1d064,
transparent: true,
opacity: 0.17,
roughness: 0.38,
metalness: 0,
side: THREE.DoubleSide,
depthWrite: false,
flatShading: true
});
const dielectric = new THREE.Mesh(unitBox, dielectricMaterial);
dielectric.renderOrder = 1;
apparatus.add(dielectric);
const dielectricEdges = new THREE.LineSegments(
new THREE.EdgesGeometry(unitBox),
new THREE.LineBasicMaterial({
color: 0xf7db76,
transparent: true,
opacity: 0.42
})
);
dielectric.add(dielectricEdges);
// Low-poly particles give the dielectric region a visible material texture.
const particleCount = 54;
const particles = new THREE.InstancedMesh(
new THREE.TetrahedronGeometry(0.065, 0),
new THREE.MeshStandardMaterial({
color: 0xffd86b,
roughness: 0.78,
metalness: 0.04,
transparent: true,
opacity: 0.55,
flatShading: true
}),
particleCount
);
particles.instanceMatrix.setUsage(THREE.DynamicDrawUsage);
particles.renderOrder = 2;
apparatus.add(particles);
const dummy = new THREE.Object3D();
// Electric field arrows.
const fieldArrows = [];
const arrowPositions = [];
for (const x of [-0.31, -0.105, 0.105, 0.31]) {
for (const z of [-0.28, 0.28]) {
arrowPositions.push([x, z]);
const arrow = new THREE.ArrowHelper(
new THREE.Vector3(0, 1, 0),
new THREE.Vector3(),
1,
0xf7db76,
0.15,
0.09
);
arrow.line.material.transparent = true;
arrow.line.material.opacity = 0.82;
arrow.cone.material.transparent = true;
arrow.cone.material.opacity = 0.9;
arrow.renderOrder = 3;
apparatus.add(arrow);
fieldArrows.push(arrow);
}
}
// Gap dimension line.
const gapLineGeometry = new THREE.BufferGeometry();
gapLineGeometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(new Float32Array(18), 3)
);
const gapLine = new THREE.LineSegments(
gapLineGeometry,
new THREE.LineBasicMaterial({
color: 0x8ca8b1,
transparent: true,
opacity: 0.6
})
);
apparatus.add(gapLine);
let visualWidth = 4;
let visualDepth = 4;
let visualGap = 1.5;
function pseudoRandom(index) {
const value = Math.sin(index * 12.9898 + 78.233) * 43758.5453;
return value - Math.floor(value);
}
function formatCapacitance(value) {
if (value >= 1e-3) return `${(value / 1e-3).toFixed(3)} mF`;
if (value >= 1e-6) return `${(value / 1e-6).toFixed(3)} µF`;
if (value >= 1e-9) return `${(value / 1e-9).toFixed(3)} nF`;
if (value >= 1e-12) return `${(value / 1e-12).toFixed(3)} pF`;
return `${(value / 1e-15).toFixed(3)} fF`;
}
function formatEnergy(value) {
if (value >= 1e-3) return `${(value / 1e-3).toFixed(3)} mJ`;
if (value >= 1e-6) return `${(value / 1e-6).toFixed(3)} µJ`;
if (value >= 1e-9) return `${(value / 1e-9).toFixed(3)} nJ`;
return `${(value / 1e-12).toFixed(3)} pJ`;
}
function formatField(value) {
if (value >= 1e6) return `${(value / 1e6).toFixed(3)} MV/m`;
if (value >= 1e3) return `${(value / 1e3).toFixed(3)} kV/m`;
return `${value.toFixed(2)} V/m`;
}
function formatCharge(value) {
if (value >= 1e-3) return `${(value / 1e-3).toFixed(3)} mC`;
if (value >= 1e-6) return `${(value / 1e-6).toFixed(3)} µC`;
if (value >= 1e-9) return `${(value / 1e-9).toFixed(3)} nC`;
return `${(value / 1e-12).toFixed(3)} pC`;
}
function updateScene(areaCm2, spacingMm, relativePermittivity) {
// Separate, readable visual scales; physical calculations remain SI-based.
visualWidth = 2.4 + 1.5 * Math.sqrt(areaCm2 / 100);
visualDepth = visualWidth;
visualGap =
0.65 + 1.65 * (Math.log10(spacingMm / 0.1) / 2);
const plateThickness = 0.14;
const topY = visualGap / 2 + plateThickness / 2;
const bottomY = -topY;
positivePlate.scale.set(visualWidth, plateThickness, visualDepth);
positivePlate.position.y = topY;
negativePlate.scale.set(visualWidth, plateThickness, visualDepth);
negativePlate.position.y = bottomY;
dielectric.scale.set(
visualWidth * 0.985,
visualGap,
visualDepth * 0.985
);
dielectric.position.y = 0;
// Update dielectric particles.
for (let i = 0; i < particleCount; i++) {
const u = pseudoRandom(i * 3 + 1);
const v = pseudoRandom(i * 3 + 2);
const w = pseudoRandom(i * 3 + 3);
dummy.position.set(
(u - 0.5) * visualWidth * 0.84,
(w - 0.5) * visualGap * 0.72,
(v - 0.5) * visualDepth * 0.84
);
dummy.rotation.set(u * Math.PI, v * Math.PI, i * 0.37);
dummy.scale.setScalar(0.55 + w * 0.85);
dummy.updateMatrix();
particles.setMatrixAt(i, dummy.matrix);
}
particles.instanceMatrix.needsUpdate = true;
// Update field arrows.
const arrowLength = visualGap * 0.7;
const headLength = Math.min(0.19, visualGap * 0.2);
const headWidth = Math.min(0.105, visualGap * 0.12);
fieldArrows.forEach((arrow, index) => {
const [xFactor, zFactor] = arrowPositions[index];
arrow.position.set(
xFactor * visualWidth,
-visualGap * 0.37,
zFactor * visualDepth
);
arrow.setLength(arrowLength, headLength, headWidth);
});
// Gap dimension line and end ticks.
const x = visualWidth / 2 + 0.42;
const positions = gapLine.geometry.attributes.position.array;
positions.set([
x, -visualGap / 2, 0,
x, visualGap / 2, 0,
x, -visualGap / 2, 0,
x - 0.13, -visualGap / 2, 0,
x, visualGap / 2, 0,
x - 0.13, visualGap / 2, 0
]);
gapLine.geometry.attributes.position.needsUpdate = true;
gapLine.geometry.computeBoundingSphere();
}
function updateCalculations() {
const areaCm2 = Number(controls.area.value);
const spacingMm = Number(controls.spacing.value);
const relativePermittivity = Number(controls.er.value);
const voltage = Number(controls.voltage.value);
const areaM2 = areaCm2 * 1e-4;
const spacingM = spacingMm * 1e-3;
const permittivity = EPSILON_0 * relativePermittivity;
const capacitance =
(permittivity * areaM2) / spacingM;
const energy = 0.5 * capacitance * voltage * voltage;
const field = voltage / spacingM;
const charge = capacitance * voltage;
$("areaOut").textContent = `${areaCm2.toFixed(0)} cm²`;
$("spacingOut").textContent = `${spacingMm.toFixed(1)} mm`;
$("erOut").textContent = relativePermittivity.toFixed(1);
$("voltageOut").textContent = `${voltage.toFixed(0)} V`;
$("capValue").textContent = formatCapacitance(capacitance);
$("energyValue").textContent = formatEnergy(energy);
$("fieldValue").textContent = formatField(field);
$("chargeValue").textContent = formatCharge(charge);
$("permittivityHint").textContent =
`Absolute permittivity: ε = ${permittivity.toExponential(3)} F/m.`;
$("areaLabel").textContent = `A = ${areaCm2.toFixed(0)} cm²`;
$("gapLabel").textContent = `d = ${spacingMm.toFixed(1)} mm`;
$("dielectricLabel").textContent = `εr = ${relativePermittivity.toFixed(1)}`;
$("positiveLabel").textContent =
`+ PLATE · +${(voltage / 2).toFixed(0)} V`;
$("negativeLabel").textContent =
`− PLATE · −${(voltage / 2).toFixed(0)} V`;
updateScene(areaCm2, spacingMm, relativePermittivity);
}
Object.values(controls).forEach((control) => {
control.addEventListener("input", updateCalculations);
});
// Camera controls implemented locally without Three.js addons.
const cameraTarget = new THREE.Vector3(0, -0.05, 0);
const initialCamera = {
yaw: 0.72,
pitch: 0.47,
radius: 13.6
};
let yaw = initialCamera.yaw;
let pitch = initialCamera.pitch;
let radius = initialCamera.radius;
const pointers = new Map();
let orbitPointer = null;
let previousX = 0;
let previousY = 0;
let previousPinchDistance = 0;
function pointerDistance() {
const values = [...pointers.values()];
if (values.length < 2) return 0;
return Math.hypot(
values[0].x - values[1].x,
values[0].y - values[1].y
);
}
renderer.domElement.addEventListener("pointerdown", (event) => {
renderer.domElement.setPointerCapture(event.pointerId);
pointers.set(event.pointerId, { x: event.clientX, y: event.clientY });
if (pointers.size === 1) {
orbitPointer = event.pointerId;
previousX = event.clientX;
previousY = event.clientY;
} else if (pointers.size === 2) {
previousPinchDistance = pointerDistance();
}
});
renderer.domElement.addEventListener("pointermove", (event) => {
if (!pointers.has(event.pointerId)) return;
pointers.set(event.pointerId, {
x: event.clientX,
y: event.clientY
});
if (pointers.size === 1 && orbitPointer === event.pointerId) {
const dx = event.clientX - previousX;
const dy = event.clientY - previousY;
yaw -= dx * 0.006;
pitch = THREE.MathUtils.clamp(pitch + dy * 0.005, 0.08, 1.35);
previousX = event.clientX;
previousY = event.clientY;
} else if (pointers.size === 2) {
const distance = pointerDistance();
if (previousPinchDistance > 0 && distance > 0) {
radius = THREE.MathUtils.clamp(
radius * (previousPinchDistance / distance),
6.5,
24
);
}
previousPinchDistance = distance;
}
});
function endPointer(event) {
pointers.delete(event.pointerId);
if (orbitPointer === event.pointerId) {
orbitPointer = null;
}
if (pointers.size < 2) {
previousPinchDistance = 0;
}
}
renderer.domElement.addEventListener("pointerup", endPointer);
renderer.domElement.addEventListener("pointercancel", endPointer);
renderer.domElement.addEventListener(
"wheel",
(event) => {
event.preventDefault();
radius = THREE.MathUtils.clamp(
radius * Math.exp(event.deltaY * 0.001),
6.5,
24
);
},
{ passive: false }
);
function resetCamera() {
yaw = initialCamera.yaw;
pitch = initialCamera.pitch;
radius = initialCamera.radius;
}
renderer.domElement.addEventListener("dblclick", resetCamera);
renderer.domElement.addEventListener("keydown", (event) => {
let handled = true;
switch (event.key) {
case "ArrowLeft":
yaw += 0.08;
break;
case "ArrowRight":
yaw -= 0.08;
break;
case "ArrowUp":
pitch = Math.min(1.35, pitch + 0.06);
break;
case "ArrowDown":
pitch = Math.max(0.08, pitch - 0.06);
break;
case "+":
case "=":
radius = Math.max(6.5, radius - 0.5);
break;
case "-":
case "_":
radius = Math.min(24, radius + 0.5);
break;
default:
handled = false;
}
if (handled) event.preventDefault();
});
const projected = new THREE.Vector3();
function positionLabel(element, worldPosition) {
projected.copy(worldPosition).project(camera);
const visible =
projected.z >= -1 &&
projected.z <= 1 &&
projected.x >= -1.25 &&
projected.x <= 1.25 &&
projected.y >= -1.25 &&
projected.y <= 1.25;
element.style.opacity = visible ? "1" : "0";
const x = (projected.x * 0.5 + 0.5) * viewport.clientWidth;
const y = (-projected.y * 0.5 + 0.5) * viewport.clientHeight;
element.style.left = `${x}px`;
element.style.top = `${y}px`;
}
function updateLabels() {
positionLabel(
$("positiveLabel"),
new THREE.Vector3(
-visualWidth / 2 - 0.22,
visualGap / 2 + 0.15,
-visualDepth * 0.25
)
);
positionLabel(
$("negativeLabel"),
new THREE.Vector3(
visualWidth / 2 + 0.22,
-visualGap / 2 - 0.15,
-visualDepth * 0.25
)
);
positionLabel(
$("areaLabel"),
new THREE.Vector3(
0,
-visualGap / 2 - 0.42,
visualDepth * 0.42
)
);
positionLabel(
$("gapLabel"),
new THREE.Vector3(
visualWidth / 2 + 0.72,
0,
0
)
);
positionLabel(
$("dielectricLabel"),
new THREE.Vector3(
-visualWidth * 0.24,
visualGap * 0.12,
visualDepth * 0.31
)
);
}
function resize() {
const width = viewport.clientWidth;
const height = viewport.clientHeight;
renderer.setSize(width, height, false);
camera.aspect = width / height;
camera.updateProjectionMatrix();
}
const resizeObserver = new ResizeObserver(resize);
resizeObserver.observe(viewport);
resize();
updateCalculations();
let firstFrame = true;
function animate() {
requestAnimationFrame(animate);
const horizontalRadius = radius * Math.cos(pitch);
camera.position.set(
cameraTarget.x + horizontalRadius * Math.sin(yaw),
cameraTarget.y + radius * Math.sin(pitch),
cameraTarget.z + horizontalRadius * Math.cos(yaw)
);
camera.lookAt(cameraTarget);
camera.updateMatrixWorld();
updateLabels();
renderer.render(scene, camera);
if (firstFrame) {
$("loading").classList.add("hidden");
firstFrame = false;
}
}
animate();
</script>
</body>
</html>
```