Prompt specification
Build or customize the Three.js 3D scene "Current continuity around one loop" 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: Current continuity around one loop
Category: Science & Physics · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/current-continuity-around-one-loop-x1614
Scene Description Prompt:
Please use Three.js from cdnjs for a stylized scene based on this idea: current continuity around one loop. Animate symbolic charge markers around a series circuit at consistent steady current; explain that slow markers illustrate charge flow rather than real signal-propagation speed. Return one self-contained HTML file.
Working Three.js Starter (current-continuity-around-one-loop-x1614):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Continuous Current — A Stylized Series Loop</title>
<style>
:root {
--bg: #07151c;
--panel: rgba(8, 24, 31, 0.82);
--panel-border: rgba(157, 255, 217, 0.17);
--text: #ecf8f4;
--muted: #9cb4b1;
--lime: #d9ff78;
--cyan: #71ead2;
--copper: #ffc857;
--coral: #ff806f;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: var(--bg);
color: var(--text);
font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
body {
overscroll-behavior: none;
}
#scene {
position: fixed;
inset: 0;
}
#scene canvas {
display: block;
width: 100%;
height: 100%;
touch-action: none;
cursor: grab;
outline: none;
}
#scene canvas:active {
cursor: grabbing;
}
.vignette {
position: fixed;
inset: 0;
pointer-events: none;
background:
radial-gradient(circle at 50% 48%, transparent 36%, rgba(2, 9, 13, 0.28) 100%),
linear-gradient(180deg, rgba(4, 14, 19, 0.28), transparent 28%, transparent 72%, rgba(4, 14, 19, 0.32));
z-index: 2;
}
.hero {
position: fixed;
z-index: 5;
top: clamp(22px, 4.4vw, 58px);
left: clamp(22px, 4.5vw, 66px);
width: min(470px, calc(100vw - 44px));
pointer-events: none;
}
.eyebrow {
display: flex;
align-items: center;
gap: 10px;
margin-bottom: 15px;
color: var(--cyan);
font-size: 0.71rem;
font-weight: 760;
letter-spacing: 0.19em;
text-transform: uppercase;
}
.eyebrow::before {
width: 27px;
height: 1px;
content: "";
background: currentColor;
box-shadow: 0 0 10px rgba(113, 234, 210, 0.65);
}
h1 {
max-width: 440px;
margin: 0;
font-size: clamp(2.35rem, 5vw, 4.7rem);
font-weight: 670;
letter-spacing: -0.057em;
line-height: 0.96;
}
h1 span {
color: var(--lime);
}
.intro {
max-width: 405px;
margin: 20px 0 0;
color: var(--muted);
font-size: clamp(0.94rem, 1.3vw, 1.06rem);
line-height: 1.65;
}
.status {
display: inline-flex;
align-items: center;
gap: 8px;
margin-top: 22px;
padding: 8px 11px;
border: 1px solid rgba(217, 255, 120, 0.2);
border-radius: 999px;
background: rgba(217, 255, 120, 0.055);
color: var(--lime);
font-size: 0.68rem;
font-weight: 750;
letter-spacing: 0.13em;
text-transform: uppercase;
}
.status-dot {
width: 7px;
height: 7px;
border-radius: 50%;
background: currentColor;
box-shadow: 0 0 12px currentColor;
}
.explain {
position: fixed;
z-index: 6;
right: clamp(18px, 3.6vw, 52px);
bottom: clamp(18px, 3.5vw, 46px);
width: min(390px, calc(100vw - 36px));
padding: 22px;
border: 1px solid var(--panel-border);
border-radius: 22px;
background: var(--panel);
box-shadow: 0 20px 70px rgba(0, 0, 0, 0.28);
backdrop-filter: blur(18px) saturate(125%);
-webkit-backdrop-filter: blur(18px) saturate(125%);
}
.panel-kicker {
color: var(--cyan);
font-size: 0.66rem;
font-weight: 760;
letter-spacing: 0.17em;
text-transform: uppercase;
}
.explain h2 {
margin: 7px 0 15px;
font-size: 1.28rem;
font-weight: 650;
letter-spacing: -0.025em;
}
.read-row {
display: grid;
grid-template-columns: 31px 1fr;
gap: 11px;
align-items: start;
margin: 13px 0;
}
.read-icon {
display: grid;
width: 31px;
height: 31px;
place-items: center;
border: 1px solid rgba(113, 234, 210, 0.18);
border-radius: 10px;
background: rgba(113, 234, 210, 0.07);
color: var(--cyan);
font-size: 0.82rem;
font-weight: 800;
}
.read-icon.marker {
color: var(--lime);
border-color: rgba(217, 255, 120, 0.18);
background: rgba(217, 255, 120, 0.07);
}
.read-row strong {
display: block;
margin-bottom: 3px;
font-size: 0.85rem;
font-weight: 700;
}
.read-row p {
margin: 0;
color: var(--muted);
font-size: 0.79rem;
line-height: 1.52;
}
.slow-note {
margin-top: 16px;
padding: 13px 14px;
border-left: 2px solid var(--coral);
border-radius: 0 10px 10px 0;
background: rgba(255, 128, 111, 0.075);
color: #d7c7c3;
font-size: 0.78rem;
line-height: 1.55;
}
.slow-note strong {
color: #ffd4cd;
}
.legend {
position: fixed;
z-index: 5;
left: clamp(22px, 4.5vw, 66px);
bottom: clamp(24px, 4vw, 52px);
display: flex;
align-items: center;
gap: 16px;
pointer-events: none;
}
.legend-item {
display: flex;
align-items: center;
gap: 8px;
color: #b8cbc7;
font-size: 0.73rem;
letter-spacing: 0.02em;
}
.legend-marker {
position: relative;
width: 19px;
height: 8px;
border-radius: 999px;
background: var(--lime);
box-shadow: 0 0 12px rgba(217, 255, 120, 0.75);
}
.legend-marker::after {
position: absolute;
top: 50%;
right: -5px;
width: 0;
height: 0;
content: "";
border-top: 4px solid transparent;
border-bottom: 4px solid transparent;
border-left: 6px solid var(--lime);
transform: translateY(-50%);
}
.hint {
color: #718986;
font-size: 0.69rem;
}
button {
display: inline-flex;
align-items: center;
gap: 8px;
min-height: 34px;
padding: 7px 11px;
border: 1px solid rgba(217, 255, 120, 0.2);
border-radius: 999px;
background: rgba(217, 255, 120, 0.075);
color: var(--lime);
font: inherit;
font-size: 0.69rem;
font-weight: 730;
letter-spacing: 0.025em;
cursor: pointer;
transition: background 160ms ease, transform 160ms ease;
}
button:hover {
background: rgba(217, 255, 120, 0.14);
transform: translateY(-1px);
}
button:focus-visible {
outline: 2px solid var(--lime);
outline-offset: 3px;
}
.button-icon {
font-size: 0.85rem;
line-height: 1;
}
#labels {
position: fixed;
inset: 0;
z-index: 4;
pointer-events: none;
}
.scene-label {
position: absolute;
display: flex;
align-items: center;
gap: 7px;
padding: 6px 9px;
border: 1px solid rgba(157, 255, 217, 0.16);
border-radius: 999px;
background: rgba(6, 20, 26, 0.76);
color: #c7dbd6;
font-size: 0.61rem;
font-weight: 720;
letter-spacing: 0.1em;
text-transform: uppercase;
white-space: nowrap;
transform: translate(-50%, -50%);
backdrop-filter: blur(8px);
}
.scene-label::before {
width: 5px;
height: 5px;
content: "";
border-radius: 50%;
background: var(--cyan);
box-shadow: 0 0 8px rgba(113, 234, 210, 0.8);
}
.scene-label.current::before {
background: var(--lime);
box-shadow: 0 0 8px rgba(217, 255, 120, 0.8);
}
@media (max-width: 760px) {
.hero {
top: 20px;
left: 20px;
}
h1 {
max-width: 330px;
font-size: clamp(2.1rem, 10vw, 3.3rem);
}
.intro {
max-width: 330px;
margin-top: 13px;
font-size: 0.84rem;
line-height: 1.5;
}
.status {
margin-top: 13px;
}
.explain {
right: 12px;
bottom: 12px;
width: calc(100vw - 24px);
padding: 16px;
border-radius: 17px;
}
.explain h2 {
margin: 5px 0 10px;
font-size: 1.05rem;
}
.read-row {
margin: 9px 0;
}
.read-row p {
font-size: 0.72rem;
}
.slow-note {
margin-top: 11px;
padding: 10px 11px;
font-size: 0.71rem;
}
.legend {
display: none;
}
#labels {
display: none;
}
}
@media (max-height: 700px) and (min-width: 761px) {
.intro {
display: none;
}
.status {
margin-top: 14px;
}
.explain {
padding: 17px;
}
.read-row {
margin: 8px 0;
}
.slow-note {
margin-top: 10px;
}
}
</style>
</head>
<body>
<div id="scene"></div>
<div class="vignette"></div>
<header class="hero">
<div class="eyebrow">Field note 01 · Direct current</div>
<h1>Current in<br><span>one continuous loop.</span></h1>
<p class="intro">
A stylized DC series circuit. Every symbolic charge marker advances at the
same speed, keeping the circuit evenly populated from source to load.
</p>
<div class="status"><span class="status-dot"></span>Steady current</div>
</header>
<aside class="explain" aria-label="How to interpret the animation">
<div class="panel-kicker">How to read it</div>
<h2>Steady current, slowed for clarity</h2>
<div class="read-row">
<div class="read-icon marker">↗</div>
<div>
<strong>The markers advance together</strong>
<p>
Their equal spacing and common speed represent a constant current
circulating continuously around the series loop.
</p>
</div>
</div>
<div class="read-row">
<div class="read-icon">≈</div>
<div>
<strong>Continuity is the important idea</strong>
<p>
The loop has no interruption. Conventional current leaves the source’s
positive terminal and returns to its negative terminal.
</p>
</div>
</div>
<div class="slow-note">
<strong>These markers are intentionally slowed.</strong>
They illustrate charge flow, not real signal-propagation speed. In a real
circuit, the electromagnetic disturbance travels much faster than the
individual charge carriers drift.
</div>
</aside>
<div class="legend" aria-hidden="true">
<div class="legend-item">
<span class="legend-marker"></span>
<span>Symbolic charge markers</span>
</div>
<button id="motionButton" type="button" aria-label="Pause charge marker animation">
<span class="button-icon">Ⅱ</span>
<span>Pause markers</span>
</button>
<span class="hint">Drag to orbit · Scroll to zoom</span>
</div>
<div id="labels" aria-hidden="true"></div>
<script type="module">
import * as THREE from "https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
const container = document.getElementById("scene");
const labelLayer = document.getElementById("labels");
const motionButton = document.getElementById("motionButton");
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x07151c);
scene.fog = new THREE.Fog(0x07151c, 13, 30);
const camera = new THREE.PerspectiveCamera(
38,
innerWidth / innerHeight,
0.1,
80
);
const renderer = new THREE.WebGLRenderer({
antialias: true,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(devicePixelRatio, 2));
renderer.setSize(innerWidth, innerHeight);
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.12;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.domElement.setAttribute(
"aria-label",
"Interactive 3D diagram of a closed series circuit with moving symbolic charge markers"
);
container.appendChild(renderer.domElement);
const hemisphere = new THREE.HemisphereLight(0xb9f6ed, 0x0a171d, 1.8);
scene.add(hemisphere);
const keyLight = new THREE.DirectionalLight(0xfff3dc, 3.4);
keyLight.position.set(4.5, 9, 5.5);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(2048, 2048);
keyLight.shadow.camera.left = -8;
keyLight.shadow.camera.right = 8;
keyLight.shadow.camera.top = 7;
keyLight.shadow.camera.bottom = -7;
keyLight.shadow.camera.near = 1;
keyLight.shadow.camera.far = 25;
keyLight.shadow.bias = -0.00035;
scene.add(keyLight);
const rimLight = new THREE.PointLight(0x4de8d2, 24, 15, 2);
rimLight.position.set(-4.5, 3.5, 3.2);
scene.add(rimLight);
const warmLight = new THREE.PointLight(0xffb84d, 17, 12, 2);
warmLight.position.set(4.2, 2.6, -2.8);
scene.add(warmLight);
const ground = new THREE.Mesh(
new THREE.PlaneGeometry(60, 60),
new THREE.MeshStandardMaterial({
color: 0x061117,
roughness: 0.96,
metalness: 0.02
})
);
ground.rotation.x = -Math.PI / 2;
ground.position.y = -0.46;
ground.receiveShadow = true;
scene.add(ground);
const grid = new THREE.GridHelper(32, 32, 0x2b746b, 0x17423f);
grid.position.y = -0.445;
grid.material.transparent = true;
grid.material.opacity = 0.11;
scene.add(grid);
function roundedRectShape(width, height, radius) {
const x = -width / 2;
const y = -height / 2;
const shape = new THREE.Shape();
shape.moveTo(x + radius, y);
shape.lineTo(x + width - radius, y);
shape.quadraticCurveTo(x + width, y, x + width, y + radius);
shape.lineTo(x + width, y + height - radius);
shape.quadraticCurveTo(
x + width,
y + height,
x + width - radius,
y + height
);
shape.lineTo(x + radius, y + height);
shape.quadraticCurveTo(x, y + height, x, y + height - radius);
shape.lineTo(x, y + radius);
shape.quadraticCurveTo(x, y, x + radius, y);
return shape;
}
const boardShape = roundedRectShape(10.8, 6.9, 0.68);
const boardGeometry = new THREE.ExtrudeGeometry(boardShape, {
depth: 0.22,
steps: 1,
bevelEnabled: true,
bevelSegments: 3,
bevelSize: 0.07,
bevelThickness: 0.055,
curveSegments: 16
});
const board = new THREE.Mesh(
boardGeometry,
new THREE.MeshPhysicalMaterial({
color: 0x103b39,
roughness: 0.56,
metalness: 0.18,
clearcoat: 0.32,
clearcoatRoughness: 0.42
})
);
board.rotation.x = -Math.PI / 2;
board.position.y = -0.275;
board.receiveShadow = true;
board.castShadow = true;
scene.add(board);
const borderPoints = boardShape.getPoints(100).map(
(point) => new THREE.Vector3(point.x, 0.018, -point.y)
);
const border = new THREE.LineLoop(
new THREE.BufferGeometry().setFromPoints(borderPoints),
new THREE.LineBasicMaterial({
color: 0x58d9c0,
transparent: true,
opacity: 0.18
})
);
scene.add(border);
class RoundedRectangleCurve extends THREE.Curve {
constructor(halfWidth, halfHeight, radius, y = 0.1) {
super();
this.halfWidth = halfWidth;
this.halfHeight = halfHeight;
this.radius = radius;
this.y = y;
this.flatX = halfWidth - radius;
this.flatZ = halfHeight - radius;
this.arcLength = (Math.PI * radius) / 2;
this.segmentLengths = [
2 * this.flatX,
this.arcLength,
2 * this.flatZ,
this.arcLength,
2 * this.flatX,
this.arcLength,
2 * this.flatZ,
this.arcLength
];
this.totalLength = this.segmentLengths.reduce(
(sum, length) => sum + length,
0
);
}
getPoint(t, target = new THREE.Vector3()) {
let distance = THREE.MathUtils.euclideanModulo(t, 1) * this.totalLength;
let segment = 0;
while (
segment < this.segmentLengths.length - 1 &&
distance > this.segmentLengths[segment]
) {
distance -= this.segmentLengths[segment];
segment++;
}
const hw = this.halfWidth;
const hh = this.halfHeight;
const r = this.radius;
const fx = this.flatX;
const fz = this.flatZ;
let x = 0;
let z = 0;
switch (segment) {
case 0:
x = -fx + distance;
z = -hh;
break;
case 1: {
const angle = -Math.PI / 2 + distance / r;
x = fx + Math.cos(angle) * r;
z = -fz + Math.sin(angle) * r;
break;
}
case 2:
x = hw;
z = -fz + distance;
break;
case 3: {
const angle = distance / r;
x = fx + Math.cos(angle) * r;
z = fz + Math.sin(angle) * r;
break;
}
case 4:
x = fx - distance;
z = hh;
break;
case 5: {
const angle = Math.PI / 2 + distance / r;
x = -fx + Math.cos(angle) * r;
z = fz + Math.sin(angle) * r;
break;
}
case 6:
x = -hw;
z = fz - distance;
break;
case 7: {
const angle = Math.PI + distance / r;
x = -fx + Math.cos(angle) * r;
z = -fz + Math.sin(angle) * r;
break;
}
}
return target.set(x, this.y, z);
}
}
const circuitCurve = new RoundedRectangleCurve(4.2, 2.5, 0.9, 0.105);
const traceMaterial = new THREE.MeshStandardMaterial({
color: 0xffc857,
roughness: 0.28,
metalness: 0.76,
emissive: 0x704000,
emissiveIntensity: 0.5
});
const trace = new THREE.Mesh(
new THREE.TubeGeometry(circuitCurve, 320, 0.075, 10, true),
traceMaterial
);
trace.castShadow = true;
scene.add(trace);
const traceGlow = new THREE.Mesh(
new THREE.TubeGeometry(circuitCurve, 320, 0.145, 10, true),
new THREE.MeshBasicMaterial({
color: 0xffbd45,
transparent: true,
opacity: 0.075,
blending: THREE.AdditiveBlending,
depthWrite: false
})
);
scene.add(traceGlow);
const silverMaterial = new THREE.MeshStandardMaterial({
color: 0xbfc9c6,
roughness: 0.25,
metalness: 0.85
});
function cylinderBetween(a, b, radius, material, radialSegments = 16) {
const direction = new THREE.Vector3().subVectors(b, a);
const length = direction.length();
const mesh = new THREE.Mesh(
new THREE.CylinderGeometry(radius, radius, length, radialSegments),
material
);
mesh.position.copy(a).add(b).multiplyScalar(0.5);
mesh.quaternion.setFromUnitVectors(
new THREE.Vector3(0, 1, 0),
direction.normalize()
);
mesh.castShadow = true;
mesh.receiveShadow = true;
scene.add(mesh);
return mesh;
}
// Resistor, aligned with the upper branch.
const resistor = new THREE.Mesh(
new THREE.CylinderGeometry(0.21, 0.21, 1.35, 28),
new THREE.MeshStandardMaterial({
color: 0xe5a05d,
roughness: 0.4,
metalness: 0.06,
emissive: 0x351300,
emissiveIntensity: 0.25
})
);
resistor.rotation.z = Math.PI / 2;
resistor.position.set(0, 0.225, -2.5);
resistor.castShadow = true;
scene.add(resistor);
cylinderBetween(
new THREE.Vector3(-1.2, 0.15, -2.5),
new THREE.Vector3(-0.67, 0.15, -2.5),
0.045,
silverMaterial
);
cylinderBetween(
new THREE.Vector3(0.67, 0.15, -2.5),
new THREE.Vector3(1.2, 0.15, -2.5),
0.045,
silverMaterial
);
const bandColors = [0x5b2020, 0xe7d4a1, 0x703222, 0x2f7770];
[-0.46, -0.16, 0.16, 0.46].forEach((x, index) => {
const band = new THREE.Mesh(
new THREE.CylinderGeometry(0.218, 0.218, 0.07, 28),
new THREE.MeshStandardMaterial({
color: bandColors[index],
roughness: 0.48,
metalness: 0.08
})
);
band.rotation.z = Math.PI / 2;
band.position.set(x, 0.225, -2.5);
scene.add(band);
});
// DC source on the left branch.
const sourceBody = new THREE.Mesh(
new THREE.CylinderGeometry(0.3, 0.3, 1.35, 28),
new THREE.MeshStandardMaterial({
color: 0x153943,
roughness: 0.38,
metalness: 0.48
})
);
sourceBody.rotation.x = Math.PI / 2;
sourceBody.position.set(-4.2, 0.23, 0);
sourceBody.castShadow = true;
scene.add(sourceBody);
const sourceCapMaterial = new THREE.MeshStandardMaterial({
color: 0x273c45,
roughness: 0.32,
metalness: 0.58
});
cylinderBetween(
new THREE.Vector3(-4.2, 0.23, -0.66),
new THREE.Vector3(-4.2, 0.23, -0.82),
0.14,
sourceCapMaterial
);
cylinderBetween(
new THREE.Vector3(-4.2, 0.23, 0.66),
new THREE.Vector3(-4.2, 0.23, 0.82),
0.14,
new THREE.MeshStandardMaterial({
color: 0xff806f,
roughness: 0.3,
metalness: 0.48,
emissive: 0x5b100a,
emissiveIntensity: 0.4
})
);
function makeTextSprite(text, color) {
const canvas = document.createElement("canvas");
canvas.width = 128;
canvas.height = 128;
const context = canvas.getContext("2d");
context.clearRect(0, 0, 128, 128);
context.font = "800 78px system-ui, sans-serif";
context.textAlign = "center";
context.textBaseline = "middle";
context.shadowColor = color;
context.shadowBlur = 15;
context.fillStyle = color;
context.fillText(text, 64, 66);
const texture = new THREE.CanvasTexture(canvas);
texture.colorSpace = THREE.SRGBColorSpace;
const sprite = new THREE.Sprite(
new THREE.SpriteMaterial({
map: texture,
transparent: true,
depthTest: false,
depthWrite: false
})
);
sprite.scale.set(0.42, 0.42, 1);
return sprite;
}
const positiveSprite = makeTextSprite("+", "#ff806f");
positiveSprite.position.set(-4.2, 0.87, 0.79);
scene.add(positiveSprite);
const negativeSprite = makeTextSprite("−", "#71ead2");
negativeSprite.position.set(-4.2, 0.87, -0.79);
scene.add(negativeSprite);
// Mounting holes and subtle solder pads.
const holeMaterial = new THREE.MeshStandardMaterial({
color: 0x051015,
roughness: 0.9,
metalness: 0.1
});
const ringMaterial = new THREE.MeshStandardMaterial({
color: 0x76918d,
roughness: 0.35,
metalness: 0.72
});
[
[-4.7, -2.83],
[4.7, -2.83],
[-4.7, 2.83],
[4.7, 2.83]
].forEach(([x, z]) => {
const hole = new THREE.Mesh(
new THREE.CylinderGeometry(0.13, 0.13, 0.035, 24),
holeMaterial
);
hole.position.set(x, 0.025, z);
scene.add(hole);
const ring = new THREE.Mesh(
new THREE.TorusGeometry(0.16, 0.035, 8, 28),
ringMaterial
);
ring.rotation.x = Math.PI / 2;
ring.position.set(x, 0.026, z);
scene.add(ring);
});
// A restrained board legend.
const decalCanvas = document.createElement("canvas");
decalCanvas.width = 640;
decalCanvas.height = 160;
const decalContext = decalCanvas.getContext("2d");
decalContext.clearRect(0, 0, 640, 160);
decalContext.fillStyle = "rgba(161, 235, 216, 0.68)";
decalContext.font = "700 35px system-ui, sans-serif";
decalContext.letterSpacing = "6px";
decalContext.fillText("CLOSED SERIES LOOP", 16, 58);
decalContext.fillStyle = "rgba(161, 235, 216, 0.34)";
decalContext.font = "500 25px system-ui, sans-serif";
decalContext.fillText("DIRECT CURRENT / CONSTANT I", 18, 112);
const decalTexture = new THREE.CanvasTexture(decalCanvas);
decalTexture.colorSpace = THREE.SRGBColorSpace;
const decal = new THREE.Mesh(
new THREE.PlaneGeometry(3.2, 0.8),
new THREE.MeshBasicMaterial({
map: decalTexture,
transparent: true,
depthWrite: false
})
);
decal.rotation.x = -Math.PI / 2;
decal.position.set(2.25, 0.022, 2.92);
scene.add(decal);
// Symbolic charge markers. Arc-length sampling keeps their spacing and
// speed uniform, including through the rounded corners.
const markerCount = 24;
const markerSpeed = 0.045;
const markerGeometry = new THREE.SphereGeometry(0.1, 12, 8);
const markerHaloGeometry = new THREE.SphereGeometry(0.17, 12, 8);
const markerHeadGeometry = new THREE.ConeGeometry(0.07, 0.17, 10);
const markerMaterial = new THREE.MeshStandardMaterial({
color: 0xeaff8f,
roughness: 0.2,
metalness: 0.12,
emissive: 0xaaff36,
emissiveIntensity: 3.2
});
const markerHaloMaterial = new THREE.MeshBasicMaterial({
color: 0xbaff4e,
transparent: true,
opacity: 0.105,
blending: THREE.AdditiveBlending,
depthWrite: false
});
const markers = [];
for (let index = 0; index < markerCount; index++) {
const group = new THREE.Group();
const core = new THREE.Mesh(markerGeometry, markerMaterial);
core.scale.set(0.9, 1.35, 0.9);
core.position.y = -0.025;
group.add(core);
const head = new THREE.Mesh(markerHeadGeometry, markerMaterial);
head.position.y = 0.15;
group.add(head);
const halo = new THREE.Mesh(markerHaloGeometry, markerHaloMaterial);
halo.scale.set(0.85, 1.25, 0.85);
halo.position.y = -0.025;
group.add(halo);
scene.add(group);
markers.push(group);
}
// DOM labels are projected from fixed positions in the 3D scene.
const sceneLabels = [];
function addSceneLabel(text, position, current = false) {
const element = document.createElement("div");
element.className = `scene-label${current ? " current" : ""}`;
element.textContent = text;
labelLayer.appendChild(element);
sceneLabels.push({ element, position });
}
addSceneLabel("DC source", new THREE.Vector3(-4.2, 1.05, 0));
addSceneLabel("Resistor R", new THREE.Vector3(0, 0.95, -2.5));
addSceneLabel("Conventional current", new THREE.Vector3(1.9, 0.8, 2.5), true);
// Direct camera controls, implemented without addons.
const orbit = {
azimuth: 0.59,
polar: 0.88,
radius: innerWidth < 760 ? 21 : 12.7,
targetAzimuth: 0.59,
targetPolar: 0.88,
targetRadius: innerWidth < 760 ? 21 : 12.7,
target: new THREE.Vector3(0, 0, 0)
};
let dragging = false;
let previousX = 0;
let previousY = 0;
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;
orbit.targetAzimuth -= dx * 0.0055;
orbit.targetPolar = THREE.MathUtils.clamp(
orbit.targetPolar - dy * 0.0045,
0.48,
1.3
);
});
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();
orbit.targetRadius = THREE.MathUtils.clamp(
orbit.targetRadius + event.deltaY * 0.008,
innerWidth < 760 ? 16 : 8.5,
innerWidth < 760 ? 26 : 18
);
},
{ passive: false }
);
let running = !window.matchMedia("(prefers-reduced-motion: reduce)").matches;
let elapsed = 0;
function updateMotionButton() {
const icon = motionButton.querySelector(".button-icon");
const text = motionButton.querySelector("span:last-child");
icon.textContent = running ? "Ⅱ" : "▶";
text.textContent = running ? "Pause markers" : "Resume markers";
motionButton.setAttribute(
"aria-label",
running ? "Pause charge marker animation" : "Resume charge marker animation"
);
}
function toggleMotion() {
running = !running;
updateMotionButton();
}
motionButton.addEventListener("click", toggleMotion);
window.addEventListener("keydown", (event) => {
if (event.code === "Space") {
event.preventDefault();
toggleMotion();
}
});
updateMotionButton();
const clock = new THREE.Clock();
const tangent = new THREE.Vector3();
const axisY = new THREE.Vector3(0, 1, 0);
const projected = new THREE.Vector3();
function updateLabels() {
for (const label of sceneLabels) {
projected.copy(label.position).project(camera);
const x = (projected.x * 0.5 + 0.5) * innerWidth;
const y = (-projected.y * 0.5 + 0.5) * innerHeight;
const visible = projected.z > -1 && projected.z < 1;
label.element.style.left = `${x}px`;
label.element.style.top = `${y}px`;
label.element.style.opacity = visible ? "1" : "0";
}
}
function animate() {
const delta = Math.min(clock.getDelta(), 0.05);
if (running) {
elapsed += delta;
}
// The parameter decreases so conventional current leaves the positive
// terminal, travels through the external loop, and returns to negative.
for (let index = 0; index < markers.length; index++) {
const phase = THREE.MathUtils.euclideanModulo(
index / markers.length - elapsed * markerSpeed,
1
);
markers[index].position.copy(circuitCurve.getPointAt(phase));
markers[index].position.y = 0.56;
circuitCurve.getTangentAt(phase, tangent);
tangent.y = 0;
tangent.normalize().negate();
markers[index].quaternion.setFromUnitVectors(axisY, tangent);
}
const damping = 1 - Math.exp(-delta * 8);
orbit.azimuth += (orbit.targetAzimuth - orbit.azimuth) * damping;
orbit.polar += (orbit.targetPolar - orbit.polar) * damping;
orbit.radius += (orbit.targetRadius - orbit.radius) * damping;
const horizontalRadius = Math.sin(orbit.polar) * orbit.radius;
camera.position.set(
orbit.target.x + horizontalRadius * Math.sin(orbit.azimuth),
orbit.target.y + Math.cos(orbit.polar) * orbit.radius,
orbit.target.z + horizontalRadius * Math.cos(orbit.azimuth)
);
camera.lookAt(orbit.target);
updateLabels();
renderer.render(scene, camera);
}
renderer.setAnimationLoop(animate);
window.addEventListener("resize", () => {
camera.aspect = innerWidth / innerHeight;
camera.fov = innerWidth < 760 ? 50 : 38;
camera.updateProjectionMatrix();
renderer.setPixelRatio(Math.min(devicePixelRatio, 2));
renderer.setSize(innerWidth, innerHeight);
});
</script>
</body>
</html>
```