Prompt specification
Build or customize the Three.js 3D scene "Parallel-plate capacitor sectional model" 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: Parallel-plate capacitor sectional model
Category: Science & Physics · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/parallel-plate-capacitor-sectional-model-x1605
Scene Description Prompt:
Show a low-poly capacitor’s separated plates, opposite charges, and electric field; distinguish stored charge from net charge. Three.js from cdnjs, standalone HTML.
Working Three.js Starter (parallel-plate-capacitor-sectional-model-x1605):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Low-Poly Capacitor</title>
<style>
:root {
color-scheme: dark;
--bg: #07101d;
--panel: rgba(10, 20, 35, 0.86);
--border: rgba(148, 163, 184, 0.22);
--text: #e5edf8;
--muted: #94a3b8;
--positive: #ff6376;
--negative: #48bfff;
--field: #ffd166;
--net: #63e6be;
}
* { box-sizing: border-box; }
html, body {
margin: 0;
width: 100%;
height: 100%;
overflow: hidden;
background: var(--bg);
color: var(--text);
font-family:
Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
canvas {
display: block;
width: 100%;
height: 100%;
touch-action: none;
cursor: grab;
}
canvas:active { cursor: grabbing; }
.header {
position: fixed;
top: 20px;
left: 22px;
z-index: 10;
pointer-events: none;
}
.eyebrow {
margin-bottom: 7px;
color: var(--field);
font-size: 11px;
font-weight: 800;
letter-spacing: 0.18em;
text-transform: uppercase;
}
h1 {
margin: 0;
font-size: clamp(24px, 4vw, 42px);
line-height: 1;
letter-spacing: -0.035em;
}
.subtitle {
max-width: 560px;
margin-top: 9px;
color: var(--muted);
font-size: 13px;
line-height: 1.45;
}
.panel {
position: fixed;
z-index: 20;
right: 20px;
top: 20px;
width: min(350px, calc(100vw - 40px));
padding: 18px;
border: 1px solid var(--border);
border-radius: 18px;
background: var(--panel);
box-shadow: 0 18px 50px rgba(0, 0, 0, 0.28);
backdrop-filter: blur(14px);
}
.panel-top {
display: flex;
align-items: center;
justify-content: space-between;
gap: 12px;
margin-bottom: 15px;
}
.panel h2 {
margin: 0;
font-size: 16px;
letter-spacing: -0.01em;
}
.state {
padding: 5px 9px;
border: 1px solid rgba(255, 209, 102, 0.35);
border-radius: 999px;
color: var(--field);
background: rgba(255, 209, 102, 0.08);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.12em;
}
.state.discharged {
border-color: rgba(148, 163, 184, 0.25);
color: var(--muted);
background: rgba(148, 163, 184, 0.07);
}
.charge-row {
display: grid;
grid-template-columns: 1fr auto;
gap: 12px;
align-items: center;
padding: 12px 0;
border-top: 1px solid var(--border);
}
.charge-row:first-of-type { border-top: 0; }
.label {
margin-bottom: 4px;
color: var(--muted);
font-size: 11px;
font-weight: 700;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.value {
font-size: 16px;
font-weight: 750;
line-height: 1.35;
}
.positive { color: var(--positive); }
.negative { color: var(--negative); }
.field-color { color: var(--field); }
.net-color { color: var(--net); }
.equation {
margin: 13px 0 15px;
padding: 13px;
border: 1px solid rgba(99, 230, 190, 0.22);
border-radius: 12px;
background: rgba(99, 230, 190, 0.06);
text-align: center;
font-size: 20px;
font-weight: 800;
}
.note {
margin: 0 0 15px;
color: var(--muted);
font-size: 12px;
line-height: 1.5;
}
.controls {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 9px;
}
button {
min-height: 39px;
padding: 9px 11px;
border: 1px solid var(--border);
border-radius: 10px;
color: var(--text);
background: rgba(148, 163, 184, 0.09);
font: inherit;
font-size: 12px;
font-weight: 750;
cursor: pointer;
transition: background 150ms ease, border-color 150ms ease, transform 150ms ease;
}
button:hover {
border-color: rgba(255, 255, 255, 0.3);
background: rgba(148, 163, 184, 0.16);
}
button:active { transform: translateY(1px); }
button.primary {
color: #111827;
border-color: transparent;
background: var(--field);
}
button.primary:hover { background: #ffdb83; }
.hint {
margin-top: 12px;
color: #64748b;
font-size: 10px;
text-align: center;
}
.legend {
position: fixed;
z-index: 10;
left: 22px;
bottom: 22px;
display: flex;
flex-wrap: wrap;
gap: 8px;
max-width: calc(100vw - 44px);
pointer-events: none;
}
.chip {
display: inline-flex;
align-items: center;
gap: 7px;
padding: 7px 10px;
border: 1px solid var(--border);
border-radius: 999px;
background: rgba(10, 20, 35, 0.76);
color: #cbd5e1;
font-size: 11px;
backdrop-filter: blur(8px);
}
.dot {
width: 9px;
height: 9px;
border-radius: 50%;
box-shadow: 0 0 12px currentColor;
}
.dot.positive { color: var(--positive); background: currentColor; }
.dot.negative { color: var(--negative); background: currentColor; }
.dot.field { color: var(--field); background: currentColor; }
.dot.net { color: var(--net); background: currentColor; }
.world-label {
position: fixed;
z-index: 5;
display: none;
transform: translate(-50%, -50%);
padding: 6px 9px;
border: 1px solid rgba(255, 255, 255, 0.18);
border-radius: 8px;
background: rgba(7, 16, 29, 0.83);
box-shadow: 0 8px 20px rgba(0, 0, 0, 0.25);
font-size: 11px;
font-weight: 800;
pointer-events: none;
white-space: nowrap;
}
#negativeLabel { color: var(--negative); }
#positiveLabel { color: var(--positive); }
#fieldLabel {
color: var(--field);
font-size: 10px;
letter-spacing: 0.08em;
}
@media (max-width: 760px) {
.header {
top: 14px;
left: 14px;
}
.subtitle { display: none; }
.panel {
top: auto;
right: 12px;
bottom: 12px;
width: calc(100vw - 24px);
padding: 14px;
}
.legend {
display: none;
}
.charge-row { padding: 9px 0; }
.equation { margin: 10px 0 12px; }
.note { margin-bottom: 11px; }
}
</style>
</head>
<body>
<header class="header">
<div class="eyebrow">Electrostatics / Capacitor</div>
<h1>Charged, but neutral</h1>
<div class="subtitle">
Equal and opposite charges are stored on separated plates. Their electric
field exists even though the capacitor's total net charge is zero.
</div>
</header>
<aside class="panel">
<div class="panel-top">
<h2>Charge accounting</h2>
<span id="state" class="state">CHARGED</span>
</div>
<div class="charge-row">
<div>
<div class="label">Stored charge</div>
<div id="storedValue" class="value">
<span class="negative">−Q</span> on one plate,
<span class="positive">+Q</span> on the other
</div>
</div>
<div id="storedSymbol" class="value field-color">±Q</div>
</div>
<div class="charge-row">
<div>
<div class="label">Electric field</div>
<div id="fieldValue" class="value field-color">
Directed from + to − between the plates
</div>
</div>
<div class="value field-color">E</div>
</div>
<div class="charge-row">
<div>
<div class="label">Net charge of capacitor</div>
<div class="value net-color">Always zero in this model</div>
</div>
<div class="value net-color">0</div>
</div>
<div id="equation" class="equation">
<span class="negative">(−Q)</span> +
<span class="positive">(+Q)</span> =
<span class="net-color">0</span>
</div>
<p id="note" class="note">
“Stored charge” describes the separated charges on each plate.
“Net charge” is their algebraic sum. A neutral capacitor can still
produce an electric field.
</p>
<div class="controls">
<button id="dischargeButton" class="primary" type="button">
Discharge capacitor
</button>
<button id="resetButton" type="button">Reset view</button>
</div>
<div class="hint">Drag to orbit · Scroll to zoom · Double-click to reset</div>
</aside>
<div class="legend">
<div class="chip"><span class="dot positive"></span>Positive charge</div>
<div class="chip"><span class="dot negative"></span>Negative charge</div>
<div class="chip"><span class="dot field"></span>Electric field</div>
<div class="chip"><span class="dot net"></span>Net charge: 0</div>
</div>
<div id="negativeLabel" class="world-label">−Q plate</div>
<div id="positiveLabel" class="world-label">+Q plate</div>
<div id="fieldLabel" class="world-label">E → from + to −</div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// ------------------------------------------------------------
// Renderer, scene, camera
// ------------------------------------------------------------
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x07101d);
scene.fog = new THREE.Fog(0x07101d, 15, 34);
const camera = new THREE.PerspectiveCamera(
43,
innerWidth / innerHeight,
0.1,
100
);
const renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setPixelRatio(Math.min(devicePixelRatio, 2));
renderer.setSize(innerWidth, innerHeight);
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.15;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
document.body.prepend(renderer.domElement);
scene.add(new THREE.HemisphereLight(0xbfd7ff, 0x182033, 2.1));
const keyLight = new THREE.DirectionalLight(0xffffff, 3.1);
keyLight.position.set(6, 9, 8);
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;
scene.add(keyLight);
const rimLight = new THREE.DirectionalLight(0x4cc9ff, 2.0);
rimLight.position.set(-7, 3, -5);
scene.add(rimLight);
const warmLight = new THREE.PointLight(0xffb45c, 24, 15);
warmLight.position.set(5, 2, 4);
scene.add(warmLight);
// ------------------------------------------------------------
// Custom camera controls: no addons
// ------------------------------------------------------------
const target = new THREE.Vector3(0, 0.1, 0);
const defaultView = {
radius: 12.2,
theta: 0.30,
phi: 1.18
};
const view = { ...defaultView };
let dragging = false;
let previousX = 0;
let previousY = 0;
function updateCamera() {
const sinPhi = Math.sin(view.phi);
camera.position.set(
target.x + view.radius * sinPhi * Math.sin(view.theta),
target.y + view.radius * Math.cos(view.phi),
target.z + view.radius * sinPhi * Math.cos(view.theta)
);
camera.lookAt(target);
}
function resetView() {
Object.assign(view, defaultView);
updateCamera();
}
renderer.domElement.addEventListener("pointerdown", (event) => {
dragging = true;
previousX = event.clientX;
previousY = event.clientY;
renderer.domElement.setPointerCapture(event.pointerId);
});
renderer.domElement.addEventListener("pointermove", (event) => {
if (!dragging) return;
const dx = event.clientX - previousX;
const dy = event.clientY - previousY;
previousX = event.clientX;
previousY = event.clientY;
view.theta -= dx * 0.007;
view.phi = THREE.MathUtils.clamp(
view.phi - dy * 0.006,
0.28,
Math.PI - 0.28
);
updateCamera();
});
function endDrag(event) {
dragging = false;
if (renderer.domElement.hasPointerCapture(event.pointerId)) {
renderer.domElement.releasePointerCapture(event.pointerId);
}
}
renderer.domElement.addEventListener("pointerup", endDrag);
renderer.domElement.addEventListener("pointercancel", endDrag);
renderer.domElement.addEventListener(
"wheel",
(event) => {
event.preventDefault();
view.radius = THREE.MathUtils.clamp(
view.radius + event.deltaY * 0.008,
7.2,
19
);
updateCamera();
},
{ passive: false }
);
renderer.domElement.addEventListener("dblclick", resetView);
window.addEventListener("keydown", (event) => {
if (event.key === "ArrowLeft") view.theta -= 0.08;
if (event.key === "ArrowRight") view.theta += 0.08;
if (event.key === "ArrowUp") view.phi -= 0.06;
if (event.key === "ArrowDown") view.phi += 0.06;
if (event.key === "+" || event.key === "=") view.radius -= 0.4;
if (event.key === "-") view.radius += 0.4;
view.phi = THREE.MathUtils.clamp(view.phi, 0.28, Math.PI - 0.28);
view.radius = THREE.MathUtils.clamp(view.radius, 7.2, 19);
updateCamera();
});
resetView();
// ------------------------------------------------------------
// Floor and grid
// ------------------------------------------------------------
const floor = new THREE.Mesh(
new THREE.PlaneGeometry(40, 40),
new THREE.MeshStandardMaterial({
color: 0x0a1423,
roughness: 0.95,
metalness: 0.05
})
);
floor.rotation.x = -Math.PI / 2;
floor.position.y = -2.05;
floor.receiveShadow = true;
scene.add(floor);
const grid = new THREE.GridHelper(24, 24, 0x35506a, 0x1d3044);
grid.position.y = -2.035;
grid.material.transparent = true;
grid.material.opacity = 0.42;
scene.add(grid);
// ------------------------------------------------------------
// Low-poly capacitor plates
// ------------------------------------------------------------
const plateGap = 1.58;
const plateGeometry = new THREE.BoxGeometry(0.30, 3.55, 2.85, 1, 2, 2);
const copperMaterial = new THREE.MeshStandardMaterial({
color: 0xb87946,
metalness: 0.72,
roughness: 0.34,
flatShading: true,
transparent: true,
opacity: 0.91
});
const negativePlate = new THREE.Group();
const positivePlate = new THREE.Group();
negativePlate.position.x = -plateGap;
positivePlate.position.x = plateGap;
scene.add(negativePlate, positivePlate);
function addPlate(group, accent) {
const body = new THREE.Mesh(plateGeometry, copperMaterial);
body.castShadow = true;
body.receiveShadow = true;
group.add(body);
const edges = new THREE.LineSegments(
new THREE.EdgesGeometry(plateGeometry, 20),
new THREE.LineBasicMaterial({
color: accent,
transparent: true,
opacity: 0.8
})
);
group.add(edges);
// Four low-poly fasteners along the plate edges.
const boltGeometry = new THREE.CylinderGeometry(
0.085, 0.085, 0.36, 6
);
const boltMaterial = new THREE.MeshStandardMaterial({
color: 0xd6e2ee,
metalness: 0.88,
roughness: 0.25,
flatShading: true
});
for (const y of [-1.38, 1.38]) {
for (const z of [-1.08, 1.08]) {
const bolt = new THREE.Mesh(boltGeometry, boltMaterial);
bolt.rotation.z = Math.PI / 2;
bolt.position.set(0, y, z);
bolt.castShadow = true;
group.add(bolt);
}
}
}
addPlate(negativePlate, 0x48bfff);
addPlate(positivePlate, 0xff6376);
// ------------------------------------------------------------
// Surface-charge markers
// ------------------------------------------------------------
const chargeItems = [];
const positiveMaterial = new THREE.MeshStandardMaterial({
color: 0xff6376,
emissive: 0x7c1026,
emissiveIntensity: 0.72,
roughness: 0.42,
metalness: 0.08,
flatShading: true,
transparent: true,
opacity: 1,
depthTest: false
});
const negativeMaterial = new THREE.MeshStandardMaterial({
color: 0x48bfff,
emissive: 0x073e67,
emissiveIntensity: 0.72,
roughness: 0.42,
metalness: 0.08,
flatShading: true,
transparent: true,
opacity: 1,
depthTest: false
});
const chargeGeometry = new THREE.IcosahedronGeometry(0.115, 0);
function addChargeMarkers(group, material, innerX) {
for (let row = 0; row < 4; row++) {
for (let column = 0; column < 5; column++) {
const charge = new THREE.Mesh(chargeGeometry, material);
charge.position.set(
innerX,
-1.08 + row * 0.72,
-1.02 + column * 0.51
);
charge.renderOrder = 20;
group.add(charge);
chargeItems.push(charge);
}
}
}
// Negative plate is on the left; positive plate is on the right.
addChargeMarkers(negativePlate, negativeMaterial, 0.25);
addChargeMarkers(positivePlate, positiveMaterial, -0.25);
// Large + and − symbols on the facing sides.
const symbolMaterialPositive = new THREE.MeshBasicMaterial({
color: 0xffa6b1,
transparent: true,
depthTest: false
});
const symbolMaterialNegative = new THREE.MeshBasicMaterial({
color: 0xa5e6ff,
transparent: true,
depthTest: false
});
function addSign(parent, type, innerX, material) {
const group = new THREE.Group();
group.position.set(innerX, 1.34, 0.95);
const vertical = new THREE.Mesh(
new THREE.BoxGeometry(0.035, 0.52, 0.105),
material
);
const horizontal = new THREE.Mesh(
new THREE.BoxGeometry(0.035, 0.105, 0.52),
material
);
horizontal.renderOrder = 25;
vertical.renderOrder = 25;
group.add(horizontal);
if (type === "plus") {
group.add(vertical);
}
parent.add(group);
group.traverse((object) => {
if (object.isMesh) chargeItems.push(object);
});
}
addSign(positivePlate, "plus", -0.19, symbolMaterialPositive);
addSign(negativePlate, "minus", 0.19, symbolMaterialNegative);
// ------------------------------------------------------------
// Electric-field lines and moving field pulses
// ------------------------------------------------------------
const fieldGroup = new THREE.Group();
scene.add(fieldGroup);
const fieldMaterials = [];
const fieldPulses = [];
const lineStartX = plateGap - 0.34;
const lineEndX = -plateGap + 0.34;
const yPositions = [-1.12, 0, 1.12];
const zPositions = [-0.92, 0, 0.92];
function registerFieldMaterial(material, baseOpacity) {
material.transparent = true;
material.opacity = baseOpacity;
material.userData.baseOpacity = baseOpacity;
fieldMaterials.push(material);
}
const lineMaterial = new THREE.LineBasicMaterial({
color: 0xffd166,
transparent: true,
opacity: 0.56
});
registerFieldMaterial(lineMaterial, 0.56);
const arrowMaterial = new THREE.MeshBasicMaterial({
color: 0xffd166,
transparent: true,
opacity: 0.9
});
registerFieldMaterial(arrowMaterial, 0.9);
const pulseMaterial = new THREE.MeshBasicMaterial({
color: 0xfff1ad,
transparent: true,
opacity: 0.95,
depthWrite: false
});
registerFieldMaterial(pulseMaterial, 0.95);
const arrowGeometry = new THREE.ConeGeometry(0.105, 0.27, 6);
const pulseGeometry = new THREE.IcosahedronGeometry(0.065, 0);
const up = new THREE.Vector3(0, 1, 0);
let lineIndex = 0;
for (const y of yPositions) {
for (const z of zPositions) {
const points = [];
for (let i = 0; i <= 32; i++) {
const t = i / 32;
const x = THREE.MathUtils.lerp(lineStartX, lineEndX, t);
// Slight low-poly curvature suggests a nonuniform field.
const bend =
Math.sin(t * Math.PI) *
Math.sin(lineIndex * 1.7) *
0.055;
points.push(new THREE.Vector3(x, y, z + bend));
}
const curve = new THREE.CatmullRomCurve3(points);
const geometry = new THREE.BufferGeometry().setFromPoints(points);
const line = new THREE.Line(geometry, lineMaterial);
fieldGroup.add(line);
// Arrowheads point in the curve direction: positive to negative.
for (const t of [0.32, 0.70]) {
const arrow = new THREE.Mesh(arrowGeometry, arrowMaterial);
arrow.position.copy(curve.getPointAt(t));
arrow.quaternion.setFromUnitVectors(
up,
curve.getTangentAt(t).normalize()
);
arrow.renderOrder = 12;
fieldGroup.add(arrow);
}
const pulse = new THREE.Mesh(pulseGeometry, pulseMaterial);
pulse.renderOrder = 13;
fieldGroup.add(pulse);
fieldPulses.push({ mesh: pulse, curve, offset: lineIndex / 9 });
lineIndex++;
}
}
// A subtle illuminated region makes the field-filled gap easy to see.
const fieldVolume = new THREE.Mesh(
new THREE.BoxGeometry(plateGap * 1.65, 3.05, 2.35),
new THREE.MeshBasicMaterial({
color: 0xffd166,
transparent: true,
opacity: 0.025,
depthWrite: false,
side: THREE.DoubleSide
})
);
fieldGroup.add(fieldVolume);
// ------------------------------------------------------------
// DOM labels projected into the 3D scene
// ------------------------------------------------------------
const negativeLabel = document.querySelector("#negativeLabel");
const positiveLabel = document.querySelector("#positiveLabel");
const fieldLabel = document.querySelector("#fieldLabel");
const negativeAnchor = new THREE.Vector3();
const positiveAnchor = new THREE.Vector3();
const fieldAnchor = new THREE.Vector3(0, 2.25, 0.6);
function placeLabel(element, worldPosition) {
const projected = worldPosition.clone().project(camera);
const visible = projected.z > -1 && projected.z < 1;
element.style.display = visible ? "block" : "none";
if (!visible) return;
const x = (projected.x * 0.5 + 0.5) * innerWidth;
const y = (-projected.y * 0.5 + 0.5) * innerHeight;
element.style.left = `${x}px`;
element.style.top = `${y}px`;
}
// ------------------------------------------------------------
// Charged / discharged interaction
// ------------------------------------------------------------
let charged = true;
let chargeAmount = 1;
let fieldAmount = 1;
const stateElement = document.querySelector("#state");
const storedValue = document.querySelector("#storedValue");
const storedSymbol = document.querySelector("#storedSymbol");
const fieldValue = document.querySelector("#fieldValue");
const equation = document.querySelector("#equation");
const note = document.querySelector("#note");
const dischargeButton = document.querySelector("#dischargeButton");
const resetButton = document.querySelector("#resetButton");
dischargeButton.addEventListener("click", () => {
charged = !charged;
if (charged) {
stateElement.textContent = "CHARGED";
stateElement.classList.remove("discharged");
storedValue.innerHTML =
'<span class="negative">−Q</span> on one plate, ' +
'<span class="positive">+Q</span> on the other';
storedSymbol.textContent = "±Q";
storedSymbol.className = "value field-color";
fieldValue.textContent =
"Directed from + to − between the plates";
equation.innerHTML =
'<span class="negative">(−Q)</span> + ' +
'<span class="positive">(+Q)</span> = ' +
'<span class="net-color">0</span>';
note.textContent =
"Stored charge describes the separated charges on each plate. " +
"Net charge is their algebraic sum. A neutral capacitor can " +
"still produce an electric field.";
dischargeButton.textContent = "Discharge capacitor";
} else {
stateElement.textContent = "DISCHARGED";
stateElement.classList.add("discharged");
storedValue.textContent =
"No separated charge remains on either plate";
storedSymbol.textContent = "0";
storedSymbol.className = "value net-color";
fieldValue.textContent =
"No field in the idealized discharged state";
equation.innerHTML =
'<span class="net-color">0</span> + ' +
'<span class="net-color">0</span> = ' +
'<span class="net-color">0</span>';
note.textContent =
"Discharging lets the opposite charges recombine or neutralize. " +
"The capacitor still has zero net charge; it simply no longer " +
"stores separated charge.";
dischargeButton.textContent = "Recharge capacitor";
}
});
resetButton.addEventListener("click", resetView);
// ------------------------------------------------------------
// Resize and animation
// ------------------------------------------------------------
window.addEventListener("resize", () => {
camera.aspect = innerWidth / innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(innerWidth, innerHeight);
renderer.setPixelRatio(Math.min(devicePixelRatio, 2));
});
const clock = new THREE.Clock();
function animate() {
requestAnimationFrame(animate);
const elapsed = clock.getElapsedTime();
const delta = Math.min(clock.getDelta(), 0.05);
// Smoothly animate the charged and field states.
const chargeTarget = charged ? 1 : 0;
const fieldTarget = charged ? 1 : 0;
chargeAmount = THREE.MathUtils.damp(
chargeAmount, chargeTarget, 5.5, delta
);
fieldAmount = THREE.MathUtils.damp(
fieldAmount, fieldTarget, 5.0, delta
);
const chargeScale = Math.max(0.001, chargeAmount);
for (const item of chargeItems) {
item.scale.setScalar(chargeScale);
}
positiveMaterial.opacity = chargeAmount;
negativeMaterial.opacity = chargeAmount;
symbolMaterialPositive.opacity = chargeAmount;
symbolMaterialNegative.opacity = chargeAmount;
for (const material of fieldMaterials) {
material.opacity = material.userData.baseOpacity * fieldAmount;
}
fieldVolume.material.opacity = 0.025 * fieldAmount;
// Pulses travel from the positive plate to the negative plate.
for (const pulse of fieldPulses) {
const t = (elapsed * 0.26 + pulse.offset) % 1;
pulse.mesh.position.copy(pulse.curve.getPointAt(t));
pulse.mesh.scale.setScalar(0.75 + Math.sin(t * Math.PI) * 0.55);
}
// Update labels after plate positions are final.
negativeAnchor.set(0, 2.12, 0);
positiveAnchor.set(0, 2.12, 0);
negativePlate.localToWorld(negativeAnchor);
positivePlate.localToWorld(positiveAnchor);
placeLabel(negativeLabel, negativeAnchor);
placeLabel(positiveLabel, positiveAnchor);
placeLabel(fieldLabel, fieldAnchor);
fieldLabel.style.opacity = String(fieldAmount);
negativeLabel.style.opacity = String(0.25 + chargeAmount * 0.75);
positiveLabel.style.opacity = String(0.25 + chargeAmount * 0.75);
renderer.render(scene, camera);
}
animate();
</script>
</body>
</html>
```