Prompt specification
Build or customize the Three.js 3D scene "Branch-current and voltage 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: Branch-current and voltage comparison
Category: Science & Physics · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/branch-current-and-voltage-comparison-x1666
Scene Description Prompt:
Compare stylized ideal series and parallel resistor circuits, showing shared current versus shared branch voltage. Use Three.js from cdnjs in one HTML file.
Working Three.js Starter (branch-current-and-voltage-comparison-x1666):
```html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Series vs. Parallel Resistor Circuits</title>
<style>
:root {
color-scheme: dark;
--bg: #07101d;
--panel: rgba(15, 29, 48, 0.82);
--border: rgba(148, 163, 184, 0.18);
--text: #eef6ff;
--muted: #9fb1c8;
--cyan: #35e7ff;
--pink: #ff4fd8;
--amber: #ffca68;
}
* {
box-sizing: border-box;
}
body {
margin: 0;
min-height: 100vh;
color: var(--text);
font-family:
Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
background:
radial-gradient(circle at 18% 10%, rgba(26, 104, 145, 0.2), transparent 32%),
radial-gradient(circle at 84% 20%, rgba(145, 39, 119, 0.15), transparent 30%),
linear-gradient(145deg, #07101d 0%, #090d18 48%, #100c1c 100%);
}
.app {
width: min(1480px, 100%);
margin: 0 auto;
padding: 28px 22px 42px;
}
header {
display: flex;
align-items: flex-end;
justify-content: space-between;
gap: 24px;
margin-bottom: 18px;
}
.eyebrow {
margin: 0 0 7px;
color: var(--cyan);
font-size: 0.72rem;
font-weight: 800;
letter-spacing: 0.2em;
text-transform: uppercase;
}
h1 {
margin: 0;
font-size: clamp(1.85rem, 4vw, 3.6rem);
line-height: 1;
letter-spacing: -0.055em;
}
.intro {
max-width: 650px;
margin: 12px 0 0;
color: var(--muted);
line-height: 1.55;
}
.ideal-pill {
flex: 0 0 auto;
padding: 9px 13px;
border: 1px solid var(--border);
border-radius: 999px;
color: #c7d6e8;
background: rgba(255, 255, 255, 0.035);
font-size: 0.78rem;
font-weight: 700;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.toolbar {
display: flex;
flex-wrap: wrap;
align-items: center;
gap: 12px 20px;
margin-bottom: 12px;
padding: 12px 14px;
border: 1px solid var(--border);
border-radius: 16px;
background: var(--panel);
backdrop-filter: blur(16px);
}
button {
border: 1px solid rgba(53, 231, 255, 0.3);
border-radius: 10px;
padding: 9px 13px;
color: var(--text);
background: rgba(53, 231, 255, 0.08);
font: inherit;
font-size: 0.82rem;
font-weight: 750;
cursor: pointer;
transition:
background 160ms ease,
border-color 160ms ease,
transform 160ms ease;
}
button:hover {
border-color: rgba(53, 231, 255, 0.7);
background: rgba(53, 231, 255, 0.15);
transform: translateY(-1px);
}
.source-control {
display: grid;
grid-template-columns: auto 120px auto;
align-items: center;
gap: 10px;
min-width: 310px;
}
.source-control label,
.toggle {
color: #cbd8e8;
font-size: 0.82rem;
font-weight: 700;
}
output {
min-width: 54px;
color: var(--amber);
font-variant-numeric: tabular-nums;
font-weight: 800;
text-align: right;
}
input[type="range"] {
width: 100%;
accent-color: var(--cyan);
cursor: pointer;
}
.toggles {
display: flex;
flex-wrap: wrap;
gap: 12px;
}
.toggle {
display: inline-flex;
align-items: center;
gap: 7px;
cursor: pointer;
}
.toggle input {
accent-color: var(--cyan);
}
.toggle.current input {
accent-color: var(--cyan);
}
.toggle.voltage input {
accent-color: var(--pink);
}
.stage-shell {
position: relative;
height: clamp(560px, 68vh, 720px);
overflow: hidden;
border: 1px solid var(--border);
border-radius: 22px;
background:
radial-gradient(circle at 50% 45%, rgba(27, 66, 91, 0.18), transparent 48%),
linear-gradient(180deg, rgba(12, 22, 37, 0.86), rgba(8, 13, 24, 0.94));
box-shadow: 0 24px 70px rgba(0, 0, 0, 0.3);
}
#stage {
position: absolute;
inset: 0;
}
#stage canvas {
display: block;
width: 100%;
height: 100%;
touch-action: none;
cursor: grab;
}
#stage canvas:active {
cursor: grabbing;
}
.stage-hint {
position: absolute;
right: 14px;
bottom: 13px;
padding: 7px 10px;
border: 1px solid var(--border);
border-radius: 999px;
color: #aebed0;
background: rgba(5, 12, 23, 0.64);
font-size: 0.72rem;
pointer-events: none;
backdrop-filter: blur(10px);
}
.legend {
position: absolute;
left: 14px;
bottom: 13px;
display: flex;
gap: 14px;
padding: 7px 11px;
border: 1px solid var(--border);
border-radius: 999px;
color: #b9c8d9;
background: rgba(5, 12, 23, 0.64);
font-size: 0.72rem;
pointer-events: none;
backdrop-filter: blur(10px);
}
.legend span {
display: inline-flex;
align-items: center;
gap: 6px;
}
.dot {
width: 8px;
height: 8px;
border-radius: 50%;
box-shadow: 0 0 12px currentColor;
}
.dot.cyan {
color: var(--cyan);
background: var(--cyan);
}
.dot.pink {
color: var(--pink);
background: var(--pink);
}
.metric-grid {
display: grid;
grid-template-columns: repeat(2, minmax(0, 1fr));
gap: 14px;
margin-top: 14px;
}
.metric-card {
position: relative;
overflow: hidden;
padding: 19px 20px;
border: 1px solid var(--border);
border-radius: 17px;
background: var(--panel);
backdrop-filter: blur(16px);
}
.metric-card::before {
position: absolute;
inset: 0 auto 0 0;
width: 3px;
content: "";
background: var(--cyan);
box-shadow: 0 0 24px rgba(53, 231, 255, 0.5);
}
.metric-card.parallel::before {
background: var(--pink);
box-shadow: 0 0 24px rgba(255, 79, 216, 0.5);
}
.metric-card h2 {
margin: 0 0 5px;
font-size: 1.05rem;
}
.metric-card > p {
margin: 0 0 15px;
color: var(--muted);
font-size: 0.85rem;
}
.values {
display: grid;
grid-template-columns: repeat(3, minmax(0, 1fr));
gap: 9px;
}
.value {
min-width: 0;
padding: 11px;
border: 1px solid rgba(148, 163, 184, 0.13);
border-radius: 11px;
background: rgba(255, 255, 255, 0.025);
}
.value small {
display: block;
margin-bottom: 5px;
color: var(--muted);
font-size: 0.66rem;
font-weight: 750;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.value strong {
display: block;
overflow: hidden;
color: #f1f8ff;
font-size: clamp(0.92rem, 1.6vw, 1.2rem);
font-variant-numeric: tabular-nums;
text-overflow: ellipsis;
white-space: nowrap;
}
.takeaway {
display: grid;
grid-template-columns: repeat(2, minmax(0, 1fr));
gap: 14px;
margin-top: 14px;
}
.takeaway > div {
padding: 15px 17px;
border: 1px solid var(--border);
border-radius: 14px;
color: #b9c9da;
background: rgba(255, 255, 255, 0.025);
font-size: 0.86rem;
line-height: 1.5;
}
.takeaway strong {
color: var(--text);
}
.takeaway .cyan-text {
color: var(--cyan);
}
.takeaway .pink-text {
color: var(--pink);
}
footer {
margin-top: 17px;
color: #71859d;
font-size: 0.76rem;
line-height: 1.5;
}
@media (max-width: 760px) {
.app {
padding: 20px 12px 30px;
}
header {
align-items: flex-start;
flex-direction: column;
gap: 12px;
}
.ideal-pill {
display: none;
}
.toolbar {
align-items: stretch;
}
.source-control {
grid-template-columns: auto 1fr auto;
width: 100%;
min-width: 0;
}
.stage-shell {
height: 680px;
}
.metric-grid,
.takeaway {
grid-template-columns: 1fr;
}
.values {
grid-template-columns: 1fr;
}
.stage-hint {
display: none;
}
.legend {
right: 14px;
justify-content: center;
}
}
</style>
</head>
<body>
<main class="app">
<header>
<div>
<p class="eyebrow">Interactive circuit comparison</p>
<h1>Series vs. parallel</h1>
<p class="intro">
A series circuit gives every component the same current. A parallel
circuit gives every branch the same voltage. Change the source voltage
and watch both relationships update.
</p>
</div>
<div class="ideal-pill">Ideal resistors · Ohm's law</div>
</header>
<section class="toolbar" aria-label="Simulation controls">
<button id="pauseButton" type="button">Pause motion</button>
<div class="source-control">
<label for="sourceVoltage">Source voltage</label>
<input
id="sourceVoltage"
type="range"
min="6"
max="18"
step="0.5"
value="12"
/>
<output id="sourceOutput" for="sourceVoltage">12.0 V</output>
</div>
<div class="toggles">
<label class="toggle current">
<input id="currentToggle" type="checkbox" checked />
Current flow
</label>
<label class="toggle voltage">
<input id="voltageToggle" type="checkbox" checked />
Voltage
</label>
</div>
<button id="resetButton" type="button">Reset view</button>
</section>
<section class="stage-shell" aria-label="Interactive 3D circuit comparison">
<div id="stage"></div>
<div class="legend" aria-hidden="true">
<span><i class="dot cyan"></i> Current</span>
<span><i class="dot pink"></i> Voltage</span>
</div>
<div class="stage-hint">Drag to orbit · Scroll to zoom · Double-click to reset</div>
</section>
<section class="metric-grid">
<article class="metric-card">
<h2>Series connection</h2>
<p>One path, so the same current passes through both resistors.</p>
<div class="values">
<div class="value">
<small>Shared current</small>
<strong id="seriesCurrent">40.0 mA</strong>
</div>
<div class="value">
<small>R1 voltage</small>
<strong id="seriesV1">4.0 V</strong>
</div>
<div class="value">
<small>R2 voltage</small>
<strong id="seriesV2">8.0 V</strong>
</div>
</div>
</article>
<article class="metric-card parallel">
<h2>Parallel connection</h2>
<p>Two branches, so each resistor has the same branch voltage.</p>
<div class="values">
<div class="value">
<small>Shared voltage</small>
<strong id="parallelVoltage">12.0 V</strong>
</div>
<div class="value">
<small>R1 branch current</small>
<strong id="parallelI1">120.0 mA</strong>
</div>
<div class="value">
<small>R2 branch current</small>
<strong id="parallelI2">60.0 mA</strong>
</div>
</div>
</article>
</section>
<section class="takeaway">
<div>
<strong class="cyan-text">Series:</strong>
current is shared, but voltage divides in proportion to resistance:
<strong>V1 / V2 = R1 / R2</strong>.
</div>
<div>
<strong class="pink-text">Parallel:</strong>
voltage is shared, but current divides in inverse proportion to
resistance: <strong>I1 / I2 = R2 / R1</strong>.
</div>
</section>
<footer>
R1 = 100 Ω and R2 = 200 Ω. Current animation speed is proportional to
current magnitude. The three-dimensional camera can be dragged and zoomed
directly; no camera-control addons are used.
</footer>
</main>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// -------------------------------------------------------------------------
// DOM and renderer
// -------------------------------------------------------------------------
const stage = document.getElementById("stage");
const sourceInput = document.getElementById("sourceVoltage");
const sourceOutput = document.getElementById("sourceOutput");
const pauseButton = document.getElementById("pauseButton");
const currentToggle = document.getElementById("currentToggle");
const voltageToggle = document.getElementById("voltageToggle");
const resetButton = document.getElementById("resetButton");
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.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.15;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.domElement.setAttribute(
"aria-label",
"Three-dimensional series and parallel resistor circuit comparison"
);
stage.appendChild(renderer.domElement);
const scene = new THREE.Scene();
scene.fog = new THREE.FogExp2(0x07101d, 0.018);
const camera = new THREE.PerspectiveCamera(42, 1, 0.1, 100);
const cameraTarget = new THREE.Vector3(0, 0, 0);
scene.add(new THREE.HemisphereLight(0xbfe9ff, 0x172033, 2.1));
const keyLight = new THREE.DirectionalLight(0xffffff, 3.2);
keyLight.position.set(4, 8, 11);
scene.add(keyLight);
const cyanLight = new THREE.PointLight(0x35e7ff, 18, 18, 2);
cyanLight.position.set(-5, 0, 5);
scene.add(cyanLight);
const pinkLight = new THREE.PointLight(0xff4fd8, 16, 18, 2);
pinkLight.position.set(5, 1, 5);
scene.add(pinkLight);
const seriesGroup = new THREE.Group();
const parallelGroup = new THREE.Group();
scene.add(seriesGroup, parallelGroup);
const R1 = 100;
const R2 = 200;
const CYAN = 0x35e7ff;
const PINK = 0xff4fd8;
const UP = new THREE.Vector3(0, 1, 0);
const wireMaterial = new THREE.MeshStandardMaterial({
color: 0x839bb4,
metalness: 0.82,
roughness: 0.25
});
const darkMetalMaterial = new THREE.MeshStandardMaterial({
color: 0x172235,
metalness: 0.65,
roughness: 0.32
});
const resistorMaterial = new THREE.MeshStandardMaterial({
color: 0xe9eef5,
roughness: 0.58,
metalness: 0.05
});
const positiveMaterial = new THREE.MeshStandardMaterial({
color: 0xff536b,
emissive: 0x7a1021,
emissiveIntensity: 0.35,
roughness: 0.38
});
const negativeMaterial = new THREE.MeshStandardMaterial({
color: 0x3e8dff,
emissive: 0x092b67,
emissiveIntensity: 0.4,
roughness: 0.38
});
// -------------------------------------------------------------------------
// Small geometry helpers
// -------------------------------------------------------------------------
function cylinderBetween(a, b, radius, material, segments = 12) {
const direction = new THREE.Vector3().subVectors(b, a);
const length = direction.length();
const geometry = new THREE.CylinderGeometry(
radius,
radius,
length,
segments
);
const mesh = new THREE.Mesh(geometry, material);
mesh.position.copy(a).add(b).multiplyScalar(0.5);
mesh.quaternion.setFromUnitVectors(UP, direction.normalize());
mesh.castShadow = true;
mesh.receiveShadow = true;
return mesh;
}
function roundedLoopPoints(width, height, radius, z = 0.05) {
const points = [];
function arc(cx, cy, start, end, segments = 9) {
for (let i = 0; i <= segments; i++) {
const angle = start + (end - start) * (i / segments);
points.push(
new THREE.Vector3(
cx + Math.cos(angle) * radius,
cy + Math.sin(angle) * radius,
z
)
);
}
}
// Clockwise path: top left → top right → bottom right → bottom left.
arc(-width / 2 + radius, height / 2 - radius, Math.PI, Math.PI / 2);
arc(width / 2 - radius, height / 2 - radius, Math.PI / 2, 0);
arc(width / 2 - radius, -height / 2 + radius, 0, -Math.PI / 2);
arc(-width / 2 + radius, -height / 2 + radius, -Math.PI / 2, -Math.PI);
return points;
}
function roundedBoardShape(width, height, radius) {
const shape = new THREE.Shape();
const left = -width / 2;
const right = width / 2;
const bottom = -height / 2;
const top = height / 2;
shape.moveTo(left + radius, bottom);
shape.lineTo(right - radius, bottom);
shape.quadraticCurveTo(right, bottom, right, bottom + radius);
shape.lineTo(right, top - radius);
shape.quadraticCurveTo(right, top, right - radius, top);
shape.lineTo(left + radius, top);
shape.quadraticCurveTo(left, top, left, top - radius);
shape.lineTo(left, bottom + radius);
shape.quadraticCurveTo(left, bottom, left + radius, bottom);
return shape;
}
function makeBoard(parent, width = 5.4, height = 5.0) {
const shape = roundedBoardShape(width, height, 0.34);
const geometry = new THREE.ExtrudeGeometry(shape, {
depth: 0.12,
bevelEnabled: true,
bevelSegments: 3,
bevelSize: 0.045,
bevelThickness: 0.035,
curveSegments: 16
});
const boardMaterial = new THREE.MeshStandardMaterial({
color: 0x101d30,
metalness: 0.18,
roughness: 0.68
});
const board = new THREE.Mesh(geometry, boardMaterial);
board.position.z = -0.52;
board.receiveShadow = true;
board.castShadow = true;
parent.add(board);
const outlinePoints = shape.getPoints(100).map(
(point) => new THREE.Vector3(point.x, point.y, -0.355)
);
const outline = new THREE.Line(
new THREE.BufferGeometry().setFromPoints(outlinePoints),
new THREE.LineBasicMaterial({
color: 0x44627e,
transparent: true,
opacity: 0.42
})
);
parent.add(outline);
const gridPositions = [];
const gridStep = 0.5;
for (
let x = -width / 2 + 0.25;
x <= width / 2 - 0.2;
x += gridStep
) {
gridPositions.push(
x, -height / 2 + 0.15, -0.335,
x, height / 2 - 0.15, -0.335
);
}
for (
let y = -height / 2 + 0.25;
y <= height / 2 - 0.2;
y += gridStep
) {
gridPositions.push(
-width / 2 + 0.15, y, -0.335,
width / 2 - 0.15, y, -0.335
);
}
const gridGeometry = new THREE.BufferGeometry();
gridGeometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(gridPositions, 3)
);
parent.add(
new THREE.LineSegments(
gridGeometry,
new THREE.LineBasicMaterial({
color: 0x8bb2d2,
transparent: true,
opacity: 0.07
})
)
);
return board;
}
function canvasRoundRect(ctx, x, y, width, height, radius) {
const r = Math.min(radius, width / 2, height / 2);
ctx.beginPath();
ctx.moveTo(x + r, y);
ctx.lineTo(x + width - r, y);
ctx.quadraticCurveTo(x + width, y, x + width, y + r);
ctx.lineTo(x + width, y + height - r);
ctx.quadraticCurveTo(
x + width,
y + height,
x + width - r,
y + height
);
ctx.lineTo(x + r, y + height);
ctx.quadraticCurveTo(x, y + height, x, y + height - r);
ctx.lineTo(x, y + r);
ctx.quadraticCurveTo(x, y, x + r, y);
ctx.closePath();
}
function makeBadge(
text,
{
accent = "#35e7ff",
background = "rgba(8, 17, 30, 0.92)",
scale = [3.8, 0.68]
} = {}
) {
const canvas = document.createElement("canvas");
canvas.width = 1024;
canvas.height = 192;
const ctx = canvas.getContext("2d");
const texture = new THREE.CanvasTexture(canvas);
texture.colorSpace = THREE.SRGBColorSpace;
texture.anisotropy = renderer.capabilities.getMaxAnisotropy();
const material = new THREE.SpriteMaterial({
map: texture,
transparent: true,
depthTest: false,
depthWrite: false,
fog: false
});
const sprite = new THREE.Sprite(material);
sprite.scale.set(scale[0], scale[1], 1);
sprite.renderOrder = 20;
function draw(value) {
ctx.clearRect(0, 0, canvas.width, canvas.height);
canvasRoundRect(
ctx,
7,
7,
canvas.width - 14,
canvas.height - 14,
30
);
ctx.fillStyle = background;
ctx.fill();
ctx.strokeStyle = "rgba(160, 190, 220, 0.22)";
ctx.lineWidth = 3;
ctx.stroke();
ctx.fillStyle = accent;
canvasRoundRect(ctx, 24, 31, 10, 130, 5);
ctx.fill();
let fontSize = 48;
ctx.font = `700 ${fontSize}px ui-sans-serif, system-ui, sans-serif`;
while (
ctx.measureText(value).width > canvas.width - 95 &&
fontSize > 24
) {
fontSize -= 2;
ctx.font = `700 ${fontSize}px ui-sans-serif, system-ui, sans-serif`;
}
ctx.fillStyle = "#eef7ff";
ctx.textBaseline = "middle";
ctx.textAlign = "left";
ctx.fillText(value, 54, canvas.height / 2 + 1);
texture.needsUpdate = true;
}
draw(text);
sprite.userData.setText = draw;
return sprite;
}
function makeBattery(parent, x, y) {
const group = new THREE.Group();
group.position.set(x, y, 0);
parent.add(group);
const body = new THREE.Mesh(
new THREE.CylinderGeometry(0.24, 0.24, 0.68, 24),
darkMetalMaterial
);
body.position.z = 0.2;
body.castShadow = true;
group.add(body);
const positive = new THREE.Mesh(
new THREE.CylinderGeometry(0.13, 0.13, 0.18, 20),
positiveMaterial
);
positive.position.set(0, 0.43, 0.2);
group.add(positive);
const negative = new THREE.Mesh(
new THREE.CylinderGeometry(0.13, 0.13, 0.18, 20),
negativeMaterial
);
negative.position.set(0, -0.43, 0.2);
group.add(negative);
return group;
}
function makeResistor(parent, x, y, orientation = "horizontal") {
const group = new THREE.Group();
group.position.set(x, y, 0);
parent.add(group);
const horizontal = orientation === "horizontal";
const body = new THREE.Mesh(
new THREE.CylinderGeometry(0.18, 0.18, 0.76, 28),
resistorMaterial
);
if (horizontal) {
body.rotation.z = Math.PI / 2;
}
body.position.z = 0.17;
body.castShadow = true;
group.add(body);
const bandColors = [0x293241, 0xd97706, 0xef4444];
const bandPositions = [-0.23, 0, 0.23];
bandPositions.forEach((position, index) => {
const band = new THREE.Mesh(
new THREE.TorusGeometry(0.184, 0.024, 8, 28),
new THREE.MeshStandardMaterial({
color: bandColors[index],
roughness: 0.45
})
);
if (horizontal) {
band.rotation.y = Math.PI / 2;
band.position.set(position, 0, 0.17);
} else {
band.rotation.x = Math.PI / 2;
band.position.set(0, position, 0.17);
}
group.add(band);
});
for (const sign of [-1, 1]) {
const cap = new THREE.Mesh(
new THREE.CylinderGeometry(0.205, 0.205, 0.12, 24),
darkMetalMaterial
);
if (horizontal) {
cap.rotation.z = Math.PI / 2;
cap.position.set(sign * 0.43, 0, 0.17);
} else {
cap.position.set(0, sign * 0.43, 0.17);
}
group.add(cap);
const leadStart = new THREE.Vector3(
horizontal ? sign * 0.49 : 0,
horizontal ? 0 : sign * 0.49,
0.08
);
const leadEnd = new THREE.Vector3(
horizontal ? sign * 0.8 : 0,
horizontal ? 0 : sign * 0.8,
0.08
);
group.add(
cylinderBetween(leadStart, leadEnd, 0.043, wireMaterial, 10)
);
}
return group;
}
// -------------------------------------------------------------------------
// Voltage arrows
// -------------------------------------------------------------------------
function createVoltageArrow(parent) {
const group = new THREE.Group();
parent.add(group);
const material = new THREE.MeshBasicMaterial({ color: PINK });
const shaft = new THREE.Mesh(
new THREE.CylinderGeometry(0.016, 0.016, 1, 10),
material
);
const headGeometry = new THREE.ConeGeometry(0.075, 0.16, 14);
const headA = new THREE.Mesh(headGeometry, material);
const headB = new THREE.Mesh(headGeometry, material);
group.add(shaft, headA, headB);
function setEnds(a, b) {
const direction = new THREE.Vector3().subVectors(b, a);
const length = Math.max(direction.length(), 0.001);
direction.normalize();
shaft.position.copy(a).add(b).multiplyScalar(0.5);
shaft.quaternion.setFromUnitVectors(UP, direction);
shaft.scale.set(1, Math.max(0.01, length - 0.1), 1);
headA.position.copy(a).addScaledVector(direction, 0.035);
headA.quaternion.setFromUnitVectors(UP, direction.clone().negate());
headB.position.copy(b).addScaledVector(direction, -0.035);
headB.quaternion.setFromUnitVectors(UP, direction);
}
return { group, setEnds };
}
// -------------------------------------------------------------------------
// Current animation
// -------------------------------------------------------------------------
function createCurrentTrain(parent, curve, count, closed = false) {
const group = new THREE.Group();
parent.add(group);
const sphereGeometry = new THREE.SphereGeometry(0.073, 12, 12);
const coneGeometry = new THREE.ConeGeometry(0.058, 0.19, 12);
const sphereMaterial = new THREE.MeshBasicMaterial({ color: CYAN });
const coneMaterial = new THREE.MeshBasicMaterial({ color: CYAN });
const markers = [];
for (let i = 0; i < count; i++) {
const sphere = new THREE.Mesh(sphereGeometry, sphereMaterial);
const cone = new THREE.Mesh(coneGeometry, coneMaterial);
group.add(sphere, cone);
markers.push({ sphere, cone });
}
return {
group,
curve,
count,
closed,
speed: 0.4,
markers
};
}
function updateCurrentTrain(train) {
for (let i = 0; i < train.markers.length; i++) {
const marker = train.markers[i];
const t = (simulationTime * train.speed + i / train.count) % 1;
const point = train.curve.getPointAt(t);
const tangent = train.curve.getTangentAt(t).normalize();
marker.sphere.position.copy(point);
marker.cone.position.copy(point).addScaledVector(tangent, 0.14);
marker.cone.quaternion.setFromUnitVectors(UP, tangent);
}
}
// -------------------------------------------------------------------------
// Series circuit
// -------------------------------------------------------------------------
makeBoard(seriesGroup);
const seriesCurrentGroup = new THREE.Group();
const seriesVoltageGroup = new THREE.Group();
seriesGroup.add(seriesCurrentGroup, seriesVoltageGroup);
const seriesTitle = makeBadge("SERIES / ONE CURRENT PATH", {
scale: [4.05, 0.72]
});
seriesTitle.position.set(0, 2.2, 0.4);
seriesGroup.add(seriesTitle);
const seriesSharedBadge = makeBadge("SHARED CURRENT I = 40.0 mA", {
scale: [4.05, 0.68]
});
seriesSharedBadge.position.set(0, -2.15, 0.4);
seriesGroup.add(seriesSharedBadge);
const seriesCurve = new THREE.CatmullRomCurve3(
roundedLoopPoints(4.8, 3.0, 0.32),
true,
"centripetal"
);
const seriesWire = new THREE.Mesh(
new THREE.TubeGeometry(seriesCurve, 220, 0.048, 8, true),
wireMaterial
);
seriesWire.castShadow = true;
seriesGroup.add(seriesWire);
makeBattery(seriesGroup, -2.4, 0);
makeResistor(seriesGroup, -0.85, 1.5, "horizontal");
makeResistor(seriesGroup, 0.85, 1.5, "horizontal");
const seriesR1Badge = makeBadge("R1 100 Ω", {
accent: "#8ea7c1",
scale: [1.8, 0.34]
});
seriesR1Badge.position.set(-0.85, 0.92, 0.38);
seriesGroup.add(seriesR1Badge);
const seriesR2Badge = makeBadge("R2 200 Ω", {
accent: "#8ea7c1",
scale: [1.8, 0.34]
});
seriesR2Badge.position.set(0.85, 0.92, 0.38);
seriesGroup.add(seriesR2Badge);
const seriesVoltage1Arrow = createVoltageArrow(seriesVoltageGroup);
const seriesVoltage2Arrow = createVoltageArrow(seriesVoltageGroup);
const seriesVoltage1Label = makeBadge("V1 = 4.0 V", {
accent: "#ff4fd8",
scale: [1.75, 0.34]
});
const seriesVoltage2Label = makeBadge("V2 = 8.0 V", {
accent: "#ff4fd8",
scale: [1.75, 0.34]
});
seriesVoltageGroup.add(seriesVoltage1Label, seriesVoltage2Label);
const seriesTrain = createCurrentTrain(seriesCurrentGroup, seriesCurve, 10, true);
// -------------------------------------------------------------------------
// Parallel circuit
// -------------------------------------------------------------------------
makeBoard(parallelGroup);
const parallelCurrentGroup = new THREE.Group();
const parallelVoltageGroup = new THREE.Group();
parallelGroup.add(parallelCurrentGroup, parallelVoltageGroup);
const parallelTitle = makeBadge("PARALLEL / TWO BRANCHES", {
accent: "#ff4fd8",
scale: [4.05, 0.72]
});
parallelTitle.position.set(0, 2.2, 0.4);
parallelGroup.add(parallelTitle);
const parallelSharedBadge = makeBadge(
"SHARED BRANCH VOLTAGE V = 12.0 V",
{
accent: "#ff4fd8",
scale: [4.05, 0.68]
}
);
parallelSharedBadge.position.set(0, -2.15, 0.4);
parallelGroup.add(parallelSharedBadge);
const railLeft = -1.65;
const railRight = 1.5;
const railTop = 1.5;
const railBottom = -1.5;
parallelGroup.add(
cylinderBetween(
new THREE.Vector3(railLeft, railTop, 0.05),
new THREE.Vector3(railRight, railTop, 0.05),
0.048,
wireMaterial
),
cylinderBetween(
new THREE.Vector3(railLeft, railBottom, 0.05),
new THREE.Vector3(railRight, railBottom, 0.05),
0.048,
wireMaterial
)
);
makeBattery(parallelGroup, railLeft, 0);
makeResistor(parallelGroup, -0.65, 0.2, "vertical");
makeResistor(parallelGroup, 0.65, 0.2, "vertical");
const branchCurves = [];
for (const x of [-0.65, 0.65]) {
const curve = new THREE.LineCurve3(
new THREE.Vector3(x, railTop, 0.05),
new THREE.Vector3(x, railBottom, 0.05)
);
branchCurves.push(curve);
const branchWire = new THREE.Mesh(
new THREE.TubeGeometry(curve, 40, 0.048, 8, false),
wireMaterial
);
branchWire.castShadow = true;
parallelGroup.add(branchWire);
for (const y of [railTop, railBottom]) {
const node = new THREE.Mesh(
new THREE.SphereGeometry(0.085, 14, 14),
wireMaterial
);
node.position.set(x, y, 0.06);
parallelGroup.add(node);
}
}
const parallelBranch1Badge = makeBadge(
"R1 100 Ω | I1 120 mA",
{
accent: "#35e7ff",
scale: [2.7, 0.46]
}
);
parallelBranch1Badge.position.set(-0.65, -0.78, 0.38);
parallelGroup.add(parallelBranch1Badge);
const parallelBranch2Badge = makeBadge(
"R2 200 Ω | I2 60 mA",
{
accent: "#35e7ff",
scale: [2.7, 0.46]
}
);
parallelBranch2Badge.position.set(0.65, -0.78, 0.38);
parallelGroup.add(parallelBranch2Badge);
const parallelVoltage1Arrow = createVoltageArrow(parallelVoltageGroup);
const parallelVoltage2Arrow = createVoltageArrow(parallelVoltageGroup);
const parallelVoltage1Label = makeBadge("V1 = 12.0 V", {
accent: "#ff4fd8",
scale: [1.75, 0.34]
});
const parallelVoltage2Label = makeBadge("V2 = 12.0 V", {
accent: "#ff4fd8",
scale: [1.75, 0.34]
});
parallelVoltageGroup.add(
parallelVoltage1Label,
parallelVoltage2Label
);
const parallelTrain1 = createCurrentTrain(
parallelCurrentGroup,
branchCurves[0],
6,
false
);
const parallelTrain2 = createCurrentTrain(
parallelCurrentGroup,
branchCurves[1],
6,
false
);
// -------------------------------------------------------------------------
// Numerical model and dynamic labels
// -------------------------------------------------------------------------
let simulationTime = 0;
let paused = false;
function updateCircuitValues(sourceVoltage) {
const seriesCurrentAmps = sourceVoltage / (R1 + R2);
const seriesCurrentMilliamps = seriesCurrentAmps * 1000;
const seriesV1 = seriesCurrentAmps * R1;
const seriesV2 = seriesCurrentAmps * R2;
const parallelV1 = sourceVoltage;
const parallelV2 = sourceVoltage;
const parallelI1Milliamps = (sourceVoltage / R1) * 1000;
const parallelI2Milliamps = (sourceVoltage / R2) * 1000;
sourceOutput.textContent = `${sourceVoltage.toFixed(1)} V`;
seriesSharedBadge.userData.setText(
`SHARED CURRENT I = ${seriesCurrentMilliamps.toFixed(1)} mA`
);
parallelSharedBadge.userData.setText(
`SHARED BRANCH VOLTAGE V = ${sourceVoltage.toFixed(1)} V`
);
seriesVoltage1Label.userData.setText(
`V1 = ${seriesV1.toFixed(1)} V`
);
seriesVoltage2Label.userData.setText(
`V2 = ${seriesV2.toFixed(1)} V`
);
parallelVoltage1Label.userData.setText(
`V1 = ${parallelV1.toFixed(1)} V`
);
parallelVoltage2Label.userData.setText(
`V2 = ${parallelV2.toFixed(1)} V`
);
parallelBranch1Badge.userData.setText(
`R1 100 Ω | I1 ${parallelI1Milliamps.toFixed(1)} mA`
);
parallelBranch2Badge.userData.setText(
`R2 200 Ω | I2 ${parallelI2Milliamps.toFixed(1)} mA`
);
// Series voltage arrows are proportional to their resistor drops.
const seriesArrowLength1 = 0.55 + 0.8 * (seriesV1 / sourceVoltage);
const seriesArrowLength2 = 0.55 + 0.8 * (seriesV2 / sourceVoltage);
seriesVoltage1Arrow.setEnds(
new THREE.Vector3(
-0.85 - seriesArrowLength1 / 2,
1.91,
0.36
),
new THREE.Vector3(
-0.85 + seriesArrowLength1 / 2,
1.91,
0.36
)
);
seriesVoltage2Arrow.setEnds(
new THREE.Vector3(
0.85 - seriesArrowLength2 / 2,
1.91,
0.36
),
new THREE.Vector3(
0.85 + seriesArrowLength2 / 2,
1.91,
0.36
)
);
seriesVoltage1Label.position.set(
-0.85 + seriesArrowLength1 / 2 + 0.33,
1.91,
0.4
);
seriesVoltage2Label.position.set(
0.85 + seriesArrowLength2 / 2 + 0.33,
1.91,
0.4
);
// Both parallel branches receive exactly the same voltage.
const parallelArrowLength = 1.18;
parallelVoltage1Arrow.setEnds(
new THREE.Vector3(-1.25, 0.2 - parallelArrowLength / 2, 0.36),
new THREE.Vector3(-1.25, 0.2 + parallelArrowLength / 2, 0.36)
);
parallelVoltage2Arrow.setEnds(
new THREE.Vector3(1.25, 0.2 - parallelArrowLength / 2, 0.36),
new THREE.Vector3(1.25, 0.2 + parallelArrowLength / 2, 0.36)
);
parallelVoltage1Label.position.set(-1.65, 0.95, 0.4);
parallelVoltage2Label.position.set(1.65, 0.95, 0.4);
// Animation speed is proportional to current in milliamps.
seriesTrain.speed = seriesCurrentMilliamps * 0.01;
parallelTrain1.speed = parallelI1Milliamps * 0.01;
parallelTrain2.speed = parallelI2Milliamps * 0.01;
document.getElementById("seriesCurrent").textContent =
`${seriesCurrentMilliamps.toFixed(1)} mA`;
document.getElementById("seriesV1").textContent =
`${seriesV1.toFixed(1)} V`;
document.getElementById("seriesV2").textContent =
`${seriesV2.toFixed(1)} V`;
document.getElementById("parallelVoltage").textContent =
`${sourceVoltage.toFixed(1)} V`;
document.getElementById("parallelI1").textContent =
`${parallelI1Milliamps.toFixed(1)} mA`;
document.getElementById("parallelI2").textContent =
`${parallelI2Milliamps.toFixed(1)} mA`;
}
sourceInput.addEventListener("input", () => {
updateCircuitValues(Number(sourceInput.value));
});
currentToggle.addEventListener("change", () => {
seriesCurrentGroup.visible = currentToggle.checked;
parallelCurrentGroup.visible = currentToggle.checked;
});
voltageToggle.addEventListener("change", () => {
seriesVoltageGroup.visible = voltageToggle.checked;
parallelVoltageGroup.visible = voltageToggle.checked;
});
pauseButton.addEventListener("click", () => {
paused = !paused;
pauseButton.textContent = paused ? "Resume motion" : "Pause motion";
pauseButton.setAttribute("aria-pressed", String(paused));
});
// -------------------------------------------------------------------------
// Custom camera controls — no addon imports
// -------------------------------------------------------------------------
let yaw = -0.035;
let pitch = 0.09;
let zoom = 1;
let baseDistance = 14.5;
let dragging = false;
let previousX = 0;
let previousY = 0;
function updateCamera() {
const distance = baseDistance * zoom;
const horizontalDistance = Math.cos(pitch) * distance;
camera.position.set(
cameraTarget.x + Math.sin(yaw) * horizontalDistance,
cameraTarget.y + Math.sin(pitch) * distance,
cameraTarget.z + Math.cos(yaw) * horizontalDistance
);
camera.lookAt(cameraTarget);
}
function resetCamera() {
yaw = -0.035;
pitch = 0.09;
zoom = 1;
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 deltaX = event.clientX - previousX;
const deltaY = event.clientY - previousY;
previousX = event.clientX;
previousY = event.clientY;
yaw -= deltaX * 0.005;
pitch = THREE.MathUtils.clamp(
pitch + deltaY * 0.004,
-0.45,
0.65
);
updateCamera();
});
function endDrag(event) {
dragging = false;
if (
event.pointerId !== undefined &&
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();
zoom = THREE.MathUtils.clamp(
zoom * (1 + event.deltaY * 0.001),
0.65,
1.75
);
updateCamera();
},
{ passive: false }
);
renderer.domElement.addEventListener("dblclick", resetCamera);
resetButton.addEventListener("click", resetCamera);
// -------------------------------------------------------------------------
// Responsive layout
// -------------------------------------------------------------------------
function resize() {
const width = stage.clientWidth;
const height = stage.clientHeight;
const mobile = width < 760;
renderer.setSize(width, height, false);
camera.aspect = width / height;
camera.fov = mobile ? 45 : 42;
camera.updateProjectionMatrix();
if (mobile) {
seriesGroup.position.set(0, 3.05, 0);
parallelGroup.position.set(0, -3.05, 0);
baseDistance = 19.2;
} else {
seriesGroup.position.set(-4.25, 0, 0);
parallelGroup.position.set(4.25, 0, 0);
baseDistance = 14.5;
}
updateCamera();
}
window.addEventListener("resize", resize);
// -------------------------------------------------------------------------
// Render loop
// -------------------------------------------------------------------------
const clock = new THREE.Clock();
function animate() {
const delta = Math.min(clock.getDelta(), 0.05);
if (!paused) {
simulationTime += delta;
}
updateCurrentTrain(seriesTrain);
updateCurrentTrain(parallelTrain1);
updateCurrentTrain(parallelTrain2);
renderer.render(scene, camera);
}
updateCircuitValues(Number(sourceInput.value));
resize();
renderer.setAnimationLoop(animate);
</script>
</body>
</html>
```