Prompt specification
Build or customize the Three.js 3D scene "Virtual resistor voltage-current bench" 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: Virtual resistor voltage-current bench
Category: Science & Physics · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/virtual-resistor-voltage-current-bench-x1657
Scene Description Prompt:
Please use Three.js from cdnjs for a low-poly scene based on this idea: virtual resistor voltage-current bench. Build a resistor proxy between a variable ideal source and current indicator; plot current against voltage for a fixed resistance and label the linear-model assumption. Return one self-contained HTML file.
Working Three.js Starter (virtual-resistor-voltage-current-bench-x1657):
```html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="color-scheme" content="light">
<title>Virtual Resistor Voltage–Current Bench</title>
<style>
:root {
--ink: #17343b;
--muted: #687e7d;
--teal: #238f83;
--teal-bright: #65cabb;
--orange: #eea348;
--paper: #f7faf7;
--line: rgba(23, 52, 59, .12);
--shadow: 0 18px 45px rgba(22, 54, 57, .13);
}
* { box-sizing: border-box; }
html, body {
width: 100%;
height: 100%;
margin: 0;
}
body {
overflow: hidden;
color: var(--ink);
background: #dfe9e5;
font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
button, input { font: inherit; }
.topbar {
height: 72px;
display: flex;
align-items: center;
justify-content: space-between;
gap: 18px;
padding: 0 22px;
background: rgba(247, 250, 247, .96);
border-bottom: 1px solid var(--line);
}
.brand {
display: flex;
align-items: center;
gap: 13px;
min-width: 0;
}
.brand-mark {
width: 38px;
height: 38px;
display: grid;
place-items: center;
flex: 0 0 auto;
color: white;
background: var(--ink);
border-radius: 11px;
box-shadow: 0 7px 16px rgba(23, 52, 59, .18);
}
.brand-mark svg { width: 25px; height: 25px; }
.brand h1 {
margin: 0;
font-size: clamp(15px, 1.8vw, 20px);
line-height: 1.1;
letter-spacing: -.025em;
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
}
.brand p {
margin: 5px 0 0;
color: var(--muted);
font-size: 10px;
font-weight: 800;
letter-spacing: .17em;
text-transform: uppercase;
}
.chips {
display: flex;
align-items: center;
gap: 8px;
flex: 0 0 auto;
}
.chip {
display: inline-flex;
align-items: center;
gap: 7px;
min-height: 30px;
padding: 6px 10px;
color: #36565a;
background: #e9f1ed;
border: 1px solid rgba(23, 52, 59, .08);
border-radius: 999px;
font-size: 11px;
font-weight: 750;
letter-spacing: .03em;
}
.chip::before {
content: "";
width: 6px;
height: 6px;
border-radius: 50%;
background: var(--teal);
box-shadow: 0 0 0 4px rgba(35, 143, 131, .11);
}
.chip.orange::before {
background: var(--orange);
box-shadow: 0 0 0 4px rgba(238, 163, 72, .14);
}
#app {
height: calc(100vh - 72px);
display: grid;
grid-template-columns: minmax(0, 1fr) 390px;
min-height: 0;
}
#stage {
position: relative;
min-width: 0;
min-height: 0;
overflow: hidden;
background: #dfe9e5;
}
#stage canvas {
display: block;
width: 100%;
height: 100%;
touch-action: none;
outline: none;
}
#stage canvas:focus-visible {
outline: 3px solid rgba(35, 143, 131, .55);
outline-offset: -4px;
}
.stage-vignette {
position: absolute;
inset: 0;
pointer-events: none;
background:
linear-gradient(180deg, rgba(255,255,255,.13), transparent 24%),
radial-gradient(circle at 50% 45%, transparent 48%, rgba(38, 70, 70, .09));
}
.glass-panel {
background: rgba(249, 252, 249, .9);
border: 1px solid rgba(255,255,255,.82);
box-shadow: var(--shadow);
backdrop-filter: blur(15px) saturate(125%);
-webkit-backdrop-filter: blur(15px) saturate(125%);
}
.controls {
position: absolute;
left: 20px;
bottom: 20px;
width: min(320px, calc(100% - 40px));
padding: 17px;
border-radius: 18px;
user-select: none;
}
.panel-kicker {
margin: 0 0 10px;
color: var(--muted);
font-size: 10px;
font-weight: 850;
letter-spacing: .17em;
text-transform: uppercase;
}
.voltage-row {
display: flex;
align-items: flex-end;
justify-content: space-between;
gap: 15px;
margin-bottom: 13px;
}
.voltage-row label {
color: #38565a;
font-size: 13px;
font-weight: 700;
}
.voltage-value {
display: flex;
align-items: baseline;
gap: 5px;
font-variant-numeric: tabular-nums;
}
.voltage-value strong {
font-size: 35px;
line-height: 1;
letter-spacing: -.06em;
}
.voltage-value span {
color: var(--muted);
font-size: 15px;
font-weight: 800;
}
.range-wrap {
position: relative;
padding: 4px 0 8px;
}
input[type="range"] {
--pct: 50%;
width: 100%;
height: 7px;
margin: 0;
border: 0;
border-radius: 999px;
appearance: none;
-webkit-appearance: none;
background: linear-gradient(
to right,
var(--teal) 0,
var(--teal) var(--pct),
rgba(23, 52, 59, .12) var(--pct),
rgba(23, 52, 59, .12) 100%
);
cursor: pointer;
}
input[type="range"]::-webkit-slider-thumb {
width: 20px;
height: 20px;
border: 4px solid white;
border-radius: 50%;
appearance: none;
-webkit-appearance: none;
background: var(--teal);
box-shadow: 0 3px 9px rgba(23, 52, 59, .28);
}
input[type="range"]::-moz-range-thumb {
width: 13px;
height: 13px;
border: 4px solid white;
border-radius: 50%;
background: var(--teal);
box-shadow: 0 3px 9px rgba(23, 52, 59, .28);
}
.range-labels {
display: flex;
justify-content: space-between;
color: #78908e;
font-size: 10px;
font-weight: 750;
}
.preset-row {
display: grid;
grid-template-columns: repeat(5, 1fr);
gap: 6px;
margin-top: 13px;
}
.preset-row button,
.stepper button {
border: 1px solid rgba(23, 52, 59, .11);
color: #466367;
background: rgba(255,255,255,.72);
cursor: pointer;
transition: transform .16s ease, background .16s ease, color .16s ease;
}
.preset-row button {
min-height: 31px;
padding: 5px 2px;
border-radius: 9px;
font-size: 11px;
font-weight: 800;
}
.preset-row button:hover,
.stepper button:hover {
transform: translateY(-1px);
background: white;
}
.preset-row button.active {
color: white;
background: var(--teal);
border-color: var(--teal);
}
.circuit-readout {
display: grid;
grid-template-columns: 1fr auto;
gap: 12px;
align-items: center;
margin-top: 14px;
padding-top: 13px;
border-top: 1px solid var(--line);
}
.circuit-readout span {
display: block;
color: var(--muted);
font-size: 10px;
font-weight: 800;
letter-spacing: .08em;
text-transform: uppercase;
}
.circuit-readout strong {
display: block;
margin-top: 3px;
font-size: 15px;
font-variant-numeric: tabular-nums;
}
.stepper {
display: flex;
gap: 5px;
}
.stepper button {
width: 30px;
height: 30px;
border-radius: 9px;
font-size: 18px;
line-height: 1;
}
.orbit-hint {
position: absolute;
right: 17px;
bottom: 15px;
padding: 7px 10px;
color: rgba(28, 57, 61, .72);
background: rgba(248, 251, 248, .68);
border: 1px solid rgba(255,255,255,.65);
border-radius: 999px;
font-size: 10px;
font-weight: 700;
pointer-events: none;
backdrop-filter: blur(8px);
}
.chart-panel {
min-width: 0;
min-height: 0;
margin: 16px 16px 16px 0;
padding: 19px;
overflow: auto;
border-radius: 20px;
}
.chart-heading {
display: flex;
justify-content: space-between;
align-items: flex-start;
gap: 12px;
}
.chart-heading h2 {
margin: 0;
font-size: 18px;
letter-spacing: -.025em;
}
.chart-heading p {
margin: 5px 0 0;
color: var(--muted);
font-size: 11px;
line-height: 1.45;
}
.linear-badge {
flex: 0 0 auto;
padding: 6px 8px;
color: #25776f;
background: rgba(35, 143, 131, .1);
border-radius: 8px;
font-size: 9px;
font-weight: 850;
letter-spacing: .09em;
text-transform: uppercase;
}
#plot {
display: block;
width: 100%;
height: 250px;
margin-top: 14px;
}
.model-card {
padding: 13px 14px;
background: #edf5f1;
border: 1px solid rgba(35, 143, 131, .11);
border-radius: 13px;
}
.equation {
display: flex;
align-items: center;
justify-content: space-between;
gap: 10px;
margin-bottom: 8px;
}
.equation strong {
color: var(--teal);
font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
font-size: 16px;
}
.equation span {
color: var(--muted);
font-size: 10px;
font-weight: 800;
letter-spacing: .06em;
text-transform: uppercase;
}
.model-card p {
margin: 0;
color: #5e7775;
font-size: 11px;
line-height: 1.55;
}
.chart-legend {
display: flex;
justify-content: space-between;
gap: 12px;
margin-top: 12px;
color: var(--muted);
font-size: 10px;
font-weight: 700;
}
.legend-item {
display: inline-flex;
align-items: center;
gap: 6px;
}
.legend-swatch {
width: 17px;
height: 3px;
border-radius: 999px;
background: var(--teal);
}
.legend-dot {
width: 7px;
height: 7px;
border-radius: 50%;
background: var(--orange);
box-shadow: 0 0 0 3px rgba(238, 163, 72, .16);
}
#loading {
position: absolute;
inset: 0;
z-index: 10;
display: grid;
place-items: center;
color: #476766;
background: #dfe9e5;
font-size: 11px;
font-weight: 850;
letter-spacing: .16em;
text-transform: uppercase;
transition: opacity .3s ease, visibility .3s ease;
}
#loading.hidden {
opacity: 0;
visibility: hidden;
}
@media (max-width: 980px) {
#app { grid-template-columns: minmax(0, 1fr) 330px; }
.chart-panel { padding: 15px; }
#plot { height: 225px; }
.chip.model-chip { display: none; }
}
@media (max-width: 760px) {
body { overflow: auto; height: auto; min-height: 100%; }
.topbar { height: auto; min-height: 68px; padding: 12px 14px; }
.brand p { display: none; }
.chips .chip:not(:first-child) { display: none; }
#app { display: block; height: auto; }
#stage { height: 67vh; min-height: 520px; }
.chart-panel { margin: 0 12px 12px; border-radius: 17px; }
#plot { height: 220px; }
.controls { left: 12px; bottom: 12px; width: min(310px, calc(100% - 24px)); }
.orbit-hint { display: none; }
}
@media (prefers-reduced-motion: reduce) {
* { scroll-behavior: auto !important; transition: none !important; }
}
</style>
</head>
<body>
<header class="topbar">
<div class="brand">
<div class="brand-mark" aria-hidden="true">
<svg viewBox="0 0 32 32" fill="none">
<path d="M2 16h5l3-8 5 16 4-11 3 3h8"
stroke="currentColor" stroke-width="2.2"
stroke-linecap="round" stroke-linejoin="round"/>
</svg>
</div>
<div>
<h1>Virtual Resistor Voltage–Current Bench</h1>
<p>Variable ideal source · fixed resistance proxy · live current indicator</p>
</div>
</div>
<div class="chips">
<span class="chip">R = 220 Ω</span>
<span class="chip orange model-chip">Linear model</span>
</div>
</header>
<main id="app">
<section id="stage" aria-label="Interactive low-poly electrical bench">
<div class="stage-vignette"></div>
<div id="loading">Initializing bench…</div>
<section class="controls glass-panel" aria-label="Source controls">
<p class="panel-kicker">Source control</p>
<div class="voltage-row">
<label for="voltage">Ideal source voltage</label>
<output class="voltage-value" for="voltage">
<strong id="voltageValue">6.0</strong><span>V</span>
</output>
</div>
<div class="range-wrap">
<input id="voltage" type="range" min="0" max="12" step="0.1" value="6"
aria-label="Ideal source voltage">
<div class="range-labels"><span>0 V</span><span>12 V</span></div>
</div>
<div class="preset-row" aria-label="Voltage presets">
<button type="button" data-voltage="0">0 V</button>
<button type="button" data-voltage="3">3 V</button>
<button type="button" data-voltage="6" class="active">6 V</button>
<button type="button" data-voltage="9">9 V</button>
<button type="button" data-voltage="12">12 V</button>
</div>
<div class="circuit-readout">
<div>
<span>Measured current</span>
<strong id="currentReadout">27.3 mA</strong>
</div>
<div class="stepper">
<button id="minus" type="button" aria-label="Decrease voltage">−</button>
<button id="plus" type="button" aria-label="Increase voltage">+</button>
</div>
</div>
</section>
<div class="orbit-hint">Drag empty space to orbit · wheel to zoom · drag orange knob</div>
</section>
<aside class="chart-panel glass-panel" aria-label="Current versus voltage graph">
<div class="chart-heading">
<div>
<p class="panel-kicker">Characteristic curve</p>
<h2>Current against voltage</h2>
<p>A fixed 220 Ω resistor connected to an ideal source.</p>
</div>
<span class="linear-badge">Linear</span>
</div>
<canvas id="plot" role="img"
aria-label="Current versus voltage graph for a fixed 220 ohm resistor"></canvas>
<div class="model-card">
<div class="equation">
<strong>I = V / R</strong>
<span id="slopeLabel">4.55 mA/V</span>
</div>
<p><b>Linear-model assumption:</b> resistance remains constant at 220 Ω.
Source and meter effects, tolerance, limits, and self-heating are ignored.</p>
</div>
<div class="chart-legend">
<span class="legend-item"><i class="legend-swatch"></i> Ideal I–V model</span>
<span class="legend-item"><i class="legend-dot"></i> Current operating point</span>
</div>
</aside>
</main>
<script type="module">
import * as THREE from "https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
const RESISTANCE = 220;
const MAX_CURRENT_MA = 60;
const stage = document.getElementById("stage");
const loading = document.getElementById("loading");
const slider = document.getElementById("voltage");
const voltageValue = document.getElementById("voltageValue");
const currentReadout = document.getElementById("currentReadout");
const presetButtons = [...document.querySelectorAll("[data-voltage]")];
const plot = document.getElementById("plot");
let voltage = Number(slider.value);
let currentMA = voltage / RESISTANCE * 1000;
let targetNeedleAngle = -120;
let currentNeedleAngle = -120;
// -------------------------------------------------------------------------
// Renderer, camera and direct orbit controls
// -------------------------------------------------------------------------
const renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.08;
renderer.domElement.tabIndex = 0;
renderer.domElement.setAttribute(
"aria-label",
"3D resistor bench. Drag to orbit, use the wheel to zoom, or drag the orange source knob."
);
stage.prepend(renderer.domElement);
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xdfe9e5);
scene.fog = new THREE.Fog(0xdfe9e5, 19, 34);
const camera = new THREE.PerspectiveCamera(38, 1, 0.1, 80);
const cameraTarget = new THREE.Vector3(0, 0.62, 0);
const orbit = { radius: 12.4, theta: 0.36, phi: 1.03 };
function updateCamera() {
const spherical = new THREE.Spherical(orbit.radius, orbit.phi, orbit.theta);
camera.position.setFromSpherical(spherical).add(cameraTarget);
camera.lookAt(cameraTarget);
}
updateCamera();
const hemi = new THREE.HemisphereLight(0xf7fff9, 0x60716c, 2.15);
scene.add(hemi);
const keyLight = new THREE.DirectionalLight(0xfff5df, 3.1);
keyLight.position.set(-5, 10, 8);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(2048, 2048);
keyLight.shadow.camera.left = -10;
keyLight.shadow.camera.right = 10;
keyLight.shadow.camera.top = 9;
keyLight.shadow.camera.bottom = -8;
keyLight.shadow.camera.near = 1;
keyLight.shadow.camera.far = 30;
keyLight.shadow.bias = -0.0005;
scene.add(keyLight);
const fillLight = new THREE.DirectionalLight(0xaadbd8, 1.15);
fillLight.position.set(7, 5, -6);
scene.add(fillLight);
const flatMaterial = (color, roughness = 0.72, metalness = 0) =>
new THREE.MeshStandardMaterial({
color,
roughness,
metalness,
flatShading: true
});
const materials = {
bench: flatMaterial(0x244751, 0.82),
benchEdge: flatMaterial(0x17343d, 0.8),
ground: flatMaterial(0xcbded8, 0.95),
source: flatMaterial(0x277f79, 0.62),
sourcePanel: flatMaterial(0xc4e3dc, 0.65),
meter: flatMaterial(0x345b65, 0.6),
meterPanel: flatMaterial(0xdbe8e3, 0.7),
orange: flatMaterial(0xeea348, 0.52),
resistor: flatMaterial(0xdcc79b, 0.78),
brass: flatMaterial(0xc89c4b, 0.36, 0.52),
red: flatMaterial(0xe65c4d, 0.55),
returnWire: flatMaterial(0x416169, 0.65),
dark: flatMaterial(0x18343b, 0.75)
};
function addBox(parent, w, h, d, material, x = 0, y = 0, z = 0) {
const mesh = new THREE.Mesh(new THREE.BoxGeometry(w, h, d), material);
mesh.position.set(x, y, z);
mesh.castShadow = true;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
function addCylinder(parent, radius, length, material, x = 0, y = 0, z = 0, sides = 12) {
const mesh = new THREE.Mesh(
new THREE.CylinderGeometry(radius, radius, length, sides),
material
);
mesh.position.set(x, y, z);
mesh.castShadow = true;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
// Ground, bench and grid.
const ground = new THREE.Mesh(new THREE.PlaneGeometry(50, 50), materials.ground);
ground.rotation.x = -Math.PI / 2;
ground.position.y = -1.16;
ground.receiveShadow = true;
scene.add(ground);
addBox(scene, 12.1, 0.34, 5.8, materials.bench, 0, 0, 0);
addBox(scene, 12.25, 0.12, 5.95, materials.benchEdge, 0, -0.19, 0);
const legPositions = [
[-5.25, -0.7, -2.25], [5.25, -0.7, -2.25],
[-5.25, -0.7, 2.25], [5.25, -0.7, 2.25]
];
for (const [x, y, z] of legPositions) {
addBox(scene, 0.38, 1.05, 0.38, materials.benchEdge, x, y, z);
}
const grid = new THREE.GridHelper(12, 12, 0x8ab0aa, 0x8ab0aa);
grid.position.y = 0.178;
grid.scale.z = 0.47;
grid.material.transparent = true;
grid.material.opacity = 0.17;
scene.add(grid);
// A few low-poly background forms.
const rockMaterial = flatMaterial(0xa9c9c0, 0.95);
[
[-7.8, -0.85, -4.7, 1.1],
[7.2, -0.9, -5.2, 1.45],
[8.4, -0.9, -3.6, 0.8]
].forEach(([x, y, z, s]) => {
const rock = new THREE.Mesh(new THREE.IcosahedronGeometry(s, 0), rockMaterial);
rock.position.set(x, y, z);
rock.scale.y = 0.65;
rock.castShadow = true;
scene.add(rock);
});
// -------------------------------------------------------------------------
// Canvas-based labels and instrument displays
// -------------------------------------------------------------------------
function createLabel(text, color = "#dff6ef") {
const canvas = document.createElement("canvas");
const context = canvas.getContext("2d");
context.font = "800 38px system-ui";
canvas.width = Math.ceil(context.measureText(text).width + 54);
canvas.height = 74;
const ctx = canvas.getContext("2d");
ctx.fillStyle = "rgba(19,49,56,.86)";
ctx.fillRect(0, 0, canvas.width, canvas.height);
ctx.fillStyle = color;
ctx.fillRect(0, 0, 6, canvas.height);
ctx.font = "800 34px system-ui";
ctx.textAlign = "center";
ctx.textBaseline = "middle";
ctx.fillText(text, canvas.width / 2, canvas.height / 2 + 1);
const texture = new THREE.CanvasTexture(canvas);
texture.colorSpace = THREE.SRGBColorSpace;
const material = new THREE.SpriteMaterial({
map: texture,
transparent: true,
depthTest: false,
depthWrite: false
});
const sprite = new THREE.Sprite(material);
sprite.scale.set(Math.min(3.25, Math.max(1.9, text.length * 0.145)), 0.39, 1);
sprite.renderOrder = 20;
return sprite;
}
function createDynamicDisplay(title, unit, width, height) {
const canvas = document.createElement("canvas");
canvas.width = 512;
canvas.height = 220;
const context = canvas.getContext("2d");
const texture = new THREE.CanvasTexture(canvas);
texture.colorSpace = THREE.SRGBColorSpace;
texture.anisotropy = Math.min(8, renderer.capabilities.getMaxAnisotropy());
const mesh = new THREE.Mesh(
new THREE.PlaneGeometry(width, height),
new THREE.MeshBasicMaterial({ map: texture, toneMapped: false })
);
function set(value, accent = "#75dfcc") {
context.clearRect(0, 0, canvas.width, canvas.height);
context.fillStyle = "#102d33";
context.fillRect(0, 0, canvas.width, canvas.height);
context.strokeStyle = "rgba(255,255,255,.13)";
context.lineWidth = 5;
context.strokeRect(8, 8, 496, 204);
context.fillStyle = "#9bb7b6";
context.font = "700 25px system-ui";
context.textAlign = "left";
context.textBaseline = "top";
context.fillText(title, 28, 25);
context.fillStyle = accent;
context.font = "700 94px ui-monospace, SFMono-Regular, Menlo, monospace";
context.textAlign = "right";
context.textBaseline = "alphabetic";
context.fillText(value, 402, 157);
context.fillStyle = "#d8eeea";
context.font = "700 32px system-ui";
context.textAlign = "left";
context.fillText(unit, 417, 118);
texture.needsUpdate = true;
}
set("0.0");
return { mesh, set };
}
// -------------------------------------------------------------------------
// Variable ideal source
// -------------------------------------------------------------------------
const source = new THREE.Group();
scene.add(source);
addBox(source, 2.2, 1.6, 1.65, materials.source, -3.55, 0.95, 0);
addBox(source, 1.94, 1.34, 0.08, materials.sourcePanel, -3.55, 0.95, 0.855);
addBox(source, 2.26, 0.13, 1.71, materials.dark, -3.55, 1.79, 0);
const sourceDisplay = createDynamicDisplay("IDEAL SOURCE", "V", 1.55, 0.58);
sourceDisplay.mesh.position.set(-3.55, 1.21, 0.905);
source.add(sourceDisplay.mesh);
const knobGroup = new THREE.Group();
knobGroup.position.set(-3.55, 0.52, 0.96);
source.add(knobGroup);
const knobBody = new THREE.Mesh(
new THREE.CylinderGeometry(0.28, 0.28, 0.19, 18),
materials.orange
);
knobBody.rotation.x = Math.PI / 2;
knobBody.castShadow = true;
knobGroup.add(knobBody);
const knobRing = new THREE.Mesh(
new THREE.TorusGeometry(0.34, 0.035, 6, 20),
materials.dark
);
knobRing.position.z = 0.06;
knobGroup.add(knobRing);
const knobPointer = addBox(
knobGroup, 0.045, 0.17, 0.025,
flatMaterial(0xfff5df, 0.55), 0, 0.105, 0.105
);
const sourceLabel = createLabel("VARIABLE IDEAL SOURCE");
sourceLabel.position.set(-3.55, 2.48, 0);
scene.add(sourceLabel);
function addTerminal(x, z, colorMaterial) {
const group = new THREE.Group();
group.position.set(x, 1.83, z);
addCylinder(group, 0.16, 0.12, materials.dark, 0, 0, 0, 10);
addCylinder(group, 0.075, 0.27, materials.brass, 0, 0.17, 0, 10);
addCylinder(group, 0.13, 0.08, colorMaterial, 0, 0.34, 0, 10);
scene.add(group);
return group;
}
addTerminal(-2.65, 0.35, materials.red);
addTerminal(-4.45, 0.35, materials.dark);
// -------------------------------------------------------------------------
// Fixed resistor proxy
// -------------------------------------------------------------------------
const resistor = new THREE.Group();
scene.add(resistor);
const resistorBody = new THREE.Mesh(
new THREE.CylinderGeometry(0.34, 0.34, 1.5, 12),
materials.resistor
);
resistorBody.rotation.z = Math.PI / 2;
resistorBody.position.set(0, 0.72, 0);
resistorBody.castShadow = true;
resistorBody.receiveShadow = true;
resistor.add(resistorBody);
const bandMaterials = [
flatMaterial(0x754b32, 0.7),
flatMaterial(0x25363a, 0.7),
flatMaterial(0xc24f3e, 0.7)
];
[-0.25, 0, 0.25].forEach((x, index) => {
const band = new THREE.Mesh(
new THREE.CylinderGeometry(0.348, 0.348, 0.13, 12),
bandMaterials[index]
);
band.rotation.z = Math.PI / 2;
band.position.set(x, 0.72, 0);
band.castShadow = true;
resistor.add(band);
});
[-1.01, 1.01].forEach(x => {
const lead = new THREE.Mesh(
new THREE.CylinderGeometry(0.045, 0.045, 0.54, 8),
materials.brass
);
lead.rotation.z = Math.PI / 2;
lead.position.set(x, 0.72, 0);
lead.castShadow = true;
resistor.add(lead);
});
const resistorLabel = createLabel("FIXED R = 220 Ω", "#ffe1a6");
resistorLabel.position.set(0, 1.48, 0);
scene.add(resistorLabel);
// -------------------------------------------------------------------------
// Current indicator / analog meter
// -------------------------------------------------------------------------
const meter = new THREE.Group();
scene.add(meter);
addBox(meter, 2.0, 1.6, 1.45, materials.meter, 3.55, 0.95, 0);
addBox(meter, 1.78, 1.35, 0.08, materials.meterPanel, 3.55, 0.96, 0.755);
addBox(meter, 2.06, 0.12, 1.5, materials.dark, 3.55, 1.79, 0);
const gaugeCanvas = document.createElement("canvas");
gaugeCanvas.width = 512;
gaugeCanvas.height = 512;
const gaugeContext = gaugeCanvas.getContext("2d");
gaugeContext.fillStyle = "#f5efd9";
gaugeContext.fillRect(0, 0, 512, 512);
const gaugeCenter = { x: 256, y: 256 };
const gaugePoint = (degrees, radius) => {
const radians = degrees * Math.PI / 180;
return {
x: gaugeCenter.x + Math.sin(radians) * radius,
y: gaugeCenter.y - Math.cos(radians) * radius
};
};
gaugeContext.strokeStyle = "#294950";
gaugeContext.lineWidth = 7;
gaugeContext.beginPath();
gaugeContext.arc(gaugeCenter.x, gaugeCenter.y, 221, 0, Math.PI * 2);
gaugeContext.stroke();
gaugeContext.strokeStyle = "#238f83";
gaugeContext.lineWidth = 11;
gaugeContext.beginPath();
for (let a = -120; a < 120; a += 2) {
const p1 = gaugePoint(a, 190);
const p2 = gaugePoint(a + 2, 190);
if (a === -120) gaugeContext.moveTo(p1.x, p1.y);
gaugeContext.lineTo(p2.x, p2.y);
}
gaugeContext.stroke();
gaugeContext.strokeStyle = "#496267";
gaugeContext.fillStyle = "#496267";
gaugeContext.font = "700 25px system-ui";
gaugeContext.textAlign = "center";
gaugeContext.textBaseline = "middle";
for (let i = 0; i <= 12; i++) {
const angle = -120 + i * 20;
const p1 = gaugePoint(angle, 174);
const p2 = gaugePoint(angle, 147);
gaugeContext.lineWidth = 4;
gaugeContext.beginPath();
gaugeContext.moveTo(p1.x, p1.y);
gaugeContext.lineTo(p2.x, p2.y);
gaugeContext.stroke();
if (i % 2 === 0) {
const p = gaugePoint(angle, 126);
const value = Math.round(i * 5);
gaugeContext.fillText(String(value), p.x, p.y);
}
}
gaugeContext.fillStyle = "#294950";
gaugeContext.font = "800 29px system-ui";
gaugeContext.fillText("mA", 256, 386);
const gaugeTexture = new THREE.CanvasTexture(gaugeCanvas);
gaugeTexture.colorSpace = THREE.SRGBColorSpace;
gaugeTexture.anisotropy = Math.min(8, renderer.capabilities.getMaxAnisotropy());
const gaugeHousing = new THREE.Mesh(
new THREE.CylinderGeometry(0.66, 0.66, 0.18, 24),
materials.dark
);
gaugeHousing.rotation.x = Math.PI / 2;
gaugeHousing.position.set(3.55, 1.22, 0.84);
gaugeHousing.castShadow = true;
scene.add(gaugeHousing);
const gaugeFace = new THREE.Mesh(
new THREE.CircleGeometry(0.59, 40),
new THREE.MeshBasicMaterial({ map: gaugeTexture, toneMapped: false })
);
gaugeFace.position.set(3.55, 1.22, 0.935);
scene.add(gaugeFace);
const needleGroup = new THREE.Group();
needleGroup.position.set(3.55, 1.22, 0.96);
scene.add(needleGroup);
const needle = addBox(
needleGroup, 0.035, 0.46, 0.028,
flatMaterial(0xe65c4d, 0.5), 0, 0.17, 0
);
needle.rotation.z = 0;
const needleHub = new THREE.Mesh(
new THREE.CylinderGeometry(0.075, 0.075, 0.035, 14),
materials.dark
);
needleHub.rotation.x = Math.PI / 2;
needleHub.position.z = 0.02;
needleGroup.add(needleHub);
const meterDisplay = createDynamicDisplay("CURRENT", "mA", 1.25, 0.36);
meterDisplay.mesh.position.set(3.55, 0.48, 0.81);
scene.add(meterDisplay.mesh);
const meterLabel = createLabel("CURRENT INDICATOR");
meterLabel.position.set(3.55, 2.48, 0);
scene.add(meterLabel);
addTerminal(2.72, 0.35, materials.red);
addTerminal(4.38, 0.35, materials.dark);
// -------------------------------------------------------------------------
// Low-poly circuit wiring and moving current markers
// -------------------------------------------------------------------------
const positivePath = [
[-2.65, 2.17, 0.35],
[-1.75, 2.17, 0.35],
[-1.75, 0.72, 0.35],
[-1.28, 0.72, 0.20],
[-1.28, 0.72, 0],
[-1.15, 0.72, 0]
];
const meterPath = [
[1.15, 0.72, 0],
[2.18, 0.72, 0.35],
[2.18, 1.35, 0.35],
[2.72, 1.35, 0.35],
[2.72, 2.17, 0.35]
];
const returnPath = [
[4.38, 2.17, 0.35],
[4.68, 1.15, 0.35],
[4.68, 0.42, -0.95],
[-4.68, 0.42, -0.95],
[-4.68, 1.15, 0.35],
[-4.45, 1.15, 0.35],
[-4.45, 2.17, 0.35]
];
function makeWire(points, material, radius = 0.045) {
const group = new THREE.Group();
const jointGeometry = new THREE.SphereGeometry(radius * 1.08, 8, 6);
for (let i = 0; i < points.length - 1; i++) {
const a = new THREE.Vector3(...points[i]);
const b = new THREE.Vector3(...points[i + 1]);
const direction = new THREE.Vector3().subVectors(b, a);
const length = direction.length();
const segment = new THREE.Mesh(
new THREE.CylinderGeometry(radius, radius, length, 8),
material
);
segment.position.copy(a).add(b).multiplyScalar(0.5);
segment.quaternion.setFromUnitVectors(
new THREE.Vector3(0, 1, 0),
direction.clone().normalize()
);
segment.castShadow = true;
segment.receiveShadow = true;
group.add(segment);
if (i > 0) {
const joint = new THREE.Mesh(jointGeometry, material);
joint.position.copy(a);
joint.castShadow = true;
group.add(joint);
}
}
scene.add(group);
return group;
}
makeWire(positivePath, materials.red);
makeWire(meterPath, materials.red);
makeWire(returnPath, materials.returnWire, 0.047);
const flowPoints = [
...positivePath,
[-0.75, 0.72, 0],
[0.75, 0.72, 0],
...meterPath.slice(1)
];
const flowCurve = new THREE.CurvePath();
for (let i = 0; i < flowPoints.length - 1; i++) {
flowCurve.add(new THREE.LineCurve3(
new THREE.Vector3(...flowPoints[i]),
new THREE.Vector3(...flowPoints[i + 1])
));
}
const beadMaterial = new THREE.MeshBasicMaterial({
color: 0xffef9c,
transparent: true,
opacity: 0.92,
toneMapped: false
});
const beads = [];
for (let i = 0; i < 8; i++) {
const bead = new THREE.Mesh(new THREE.SphereGeometry(0.065, 8, 6), beadMaterial);
bead.visible = false;
scene.add(bead);
beads.push(bead);
}
// -------------------------------------------------------------------------
// Plot
// -------------------------------------------------------------------------
let chartWidth = 0;
let chartHeight = 0;
let chartDPR = 1;
function resizeChart() {
const rect = plot.getBoundingClientRect();
chartWidth = Math.max(260, rect.width);
chartHeight = Math.max(180, rect.height);
chartDPR = Math.min(window.devicePixelRatio, 2);
plot.width = Math.round(chartWidth * chartDPR);
plot.height = Math.round(chartHeight * chartDPR);
drawChart();
}
function drawChart() {
if (!chartWidth || !chartHeight) return;
const ctx = plot.getContext("2d");
ctx.setTransform(chartDPR, 0, 0, chartDPR, 0, 0);
ctx.clearRect(0, 0, chartWidth, chartHeight);
const margin = { left: 48, right: 18, top: 20, bottom: 42 };
const width = chartWidth - margin.left - margin.right;
const height = chartHeight - margin.top - margin.bottom;
const xMax = 12;
const yMax = 60;
const px = value => margin.left + value / xMax * width;
const py = value => margin.top + height - value / yMax * height;
ctx.lineWidth = 1;
ctx.strokeStyle = "rgba(23,52,59,.11)";
ctx.fillStyle = "#728783";
ctx.font = "600 10px system-ui";
ctx.textAlign = "center";
ctx.textBaseline = "middle";
for (let value = 0; value <= 12; value += 3) {
const x = px(value);
ctx.beginPath();
ctx.moveTo(x, margin.top);
ctx.lineTo(x, margin.top + height);
ctx.stroke();
ctx.fillText(String(value), x, margin.top + height + 15);
}
ctx.textAlign = "right";
for (let value = 0; value <= 60; value += 15) {
const y = py(value);
ctx.beginPath();
ctx.moveTo(margin.left, y);
ctx.lineTo(margin.left + width, y);
ctx.stroke();
ctx.fillText(String(value), margin.left - 9, y);
}
ctx.strokeStyle = "rgba(23,52,59,.28)";
ctx.lineWidth = 1.2;
ctx.beginPath();
ctx.moveTo(margin.left, margin.top);
ctx.lineTo(margin.left, margin.top + height);
ctx.lineTo(margin.left + width, margin.top + height);
ctx.stroke();
ctx.strokeStyle = "#238f83";
ctx.lineWidth = 3;
ctx.lineCap = "round";
ctx.beginPath();
ctx.moveTo(px(0), py(0));
ctx.lineTo(px(12), py(12 / RESISTANCE * 1000));
ctx.stroke();
const pointX = px(voltage);
const pointY = py(currentMA);
ctx.fillStyle = "rgba(238,163,72,.2)";
ctx.beginPath();
ctx.arc(pointX, pointY, 9, 0, Math.PI * 2);
ctx.fill();
ctx.fillStyle = "#eea348";
ctx.strokeStyle = "white";
ctx.lineWidth = 2.5;
ctx.beginPath();
ctx.arc(pointX, pointY, 5.5, 0, Math.PI * 2);
ctx.fill();
ctx.stroke();
ctx.fillStyle = "#526c6b";
ctx.font = "700 10px system-ui";
ctx.textAlign = "center";
ctx.fillText("Source voltage (V)", margin.left + width / 2, chartHeight - 9);
ctx.save();
ctx.translate(13, margin.top + height / 2);
ctx.rotate(-Math.PI / 2);
ctx.textAlign = "center";
ctx.fillText("Current (mA)", 0, 0);
ctx.restore();
plot.setAttribute(
"aria-label",
`Current versus voltage graph. At ${voltage.toFixed(1)} volts, ` +
`the ideal 220 ohm model gives ${currentMA.toFixed(1)} milliamperes.`
);
}
// -------------------------------------------------------------------------
// State and UI updates
// -------------------------------------------------------------------------
function updateState(nextVoltage) {
voltage = THREE.MathUtils.clamp(Number(nextVoltage), 0, 12);
currentMA = voltage / RESISTANCE * 1000;
targetNeedleAngle = -120 + 240 * THREE.MathUtils.clamp(currentMA / MAX_CURRENT_MA, 0, 1);
slider.value = voltage.toFixed(1);
voltageValue.textContent = voltage.toFixed(1);
currentReadout.textContent = `${currentMA.toFixed(1)} mA`;
const percentage = (voltage / 12) * 100;
slider.style.setProperty("--pct", `${percentage}%`);
sourceDisplay.set(voltage.toFixed(1), "#75dfcc");
meterDisplay.set(currentMA.toFixed(1), "#ffd27d");
const knobAngle = THREE.MathUtils.lerp(-135, 135, voltage / 12);
knobGroup.rotation.z = THREE.MathUtils.degToRad(knobAngle);
knobPointer.position.y = 0.105;
presetButtons.forEach(button => {
button.classList.toggle("active", Math.abs(Number(button.dataset.voltage) - voltage) < 0.05);
});
drawChart();
}
slider.addEventListener("input", event => updateState(event.target.value));
presetButtons.forEach(button => {
button.addEventListener("click", () => updateState(button.dataset.voltage));
});
document.getElementById("minus").addEventListener("click", () => {
updateState(voltage - 0.5);
});
document.getElementById("plus").addEventListener("click", () => {
updateState(voltage + 0.5);
});
// -------------------------------------------------------------------------
// Custom camera controls and draggable 3D source knob
// -------------------------------------------------------------------------
const raycaster = new THREE.Raycaster();
const pointer = new THREE.Vector2();
let orbiting = false;
let draggingKnob = false;
let lastX = 0;
let lastY = 0;
let dragStartY = 0;
let dragStartVoltage = 0;
function setPointerFromEvent(event) {
const rect = renderer.domElement.getBoundingClientRect();
pointer.x = ((event.clientX - rect.left) / rect.width) * 2 - 1;
pointer.y = -((event.clientY - rect.top) / rect.height) * 2 + 1;
}
renderer.domElement.addEventListener("pointerdown", event => {
if (event.button !== 0) return;
setPointerFromEvent(event);
raycaster.setFromCamera(pointer, camera);
const hits = raycaster.intersectObject(knobGroup, true);
if (hits.length) {
draggingKnob = true;
dragStartY = event.clientY;
dragStartVoltage = voltage;
renderer.domElement.style.cursor = "ns-resize";
} else {
orbiting = true;
lastX = event.clientX;
lastY = event.clientY;
renderer.domElement.style.cursor = "grabbing";
}
renderer.domElement.setPointerCapture(event.pointerId);
});
renderer.domElement.addEventListener("pointermove", event => {
if (draggingKnob) {
const delta = (dragStartY - event.clientY) * 0.05;
updateState(dragStartVoltage + delta);
return;
}
if (orbiting) {
const dx = event.clientX - lastX;
const dy = event.clientY - lastY;
lastX = event.clientX;
lastY = event.clientY;
orbit.theta -= dx * 0.006;
orbit.phi = THREE.MathUtils.clamp(orbit.phi - dy * 0.005, 0.47, 1.39);
updateCamera();
} else {
setPointerFromEvent(event);
raycaster.setFromCamera(pointer, camera);
renderer.domElement.style.cursor =
raycaster.intersectObject(knobGroup, true).length ? "ns-resize" : "grab";
}
});
function endPointer(event) {
orbiting = false;
draggingKnob = false;
renderer.domElement.style.cursor = "grab";
if (event.pointerId !== undefined &&
renderer.domElement.hasPointerCapture(event.pointerId)) {
renderer.domElement.releasePointerCapture(event.pointerId);
}
}
renderer.domElement.addEventListener("pointerup", endPointer);
renderer.domElement.addEventListener("pointercancel", endPointer);
renderer.domElement.addEventListener("pointerleave", event => {
if (!orbiting && !draggingKnob) renderer.domElement.style.cursor = "grab";
});
renderer.domElement.addEventListener("wheel", event => {
event.preventDefault();
orbit.radius = THREE.MathUtils.clamp(orbit.radius + event.deltaY * 0.008, 8.2, 17.5);
updateCamera();
}, { passive: false });
renderer.domElement.addEventListener("dblclick", () => {
orbit.radius = 12.4;
orbit.theta = 0.36;
orbit.phi = 1.03;
updateCamera();
});
renderer.domElement.style.cursor = "grab";
// -------------------------------------------------------------------------
// Resize and animation
// -------------------------------------------------------------------------
function resizeScene() {
const width = Math.max(1, stage.clientWidth);
const height = Math.max(1, stage.clientHeight);
renderer.setSize(width, height, false);
camera.aspect = width / height;
camera.updateProjectionMatrix();
}
const sceneResizeObserver = new ResizeObserver(resizeScene);
sceneResizeObserver.observe(stage);
const chartResizeObserver = new ResizeObserver(resizeChart);
chartResizeObserver.observe(plot.parentElement);
const clock = new THREE.Clock();
let firstFrame = true;
function animate() {
requestAnimationFrame(animate);
const elapsed = clock.getElapsedTime();
currentNeedleAngle = THREE.MathUtils.lerp(
currentNeedleAngle,
targetNeedleAngle,
0.12
);
needleGroup.rotation.z = -THREE.MathUtils.degToRad(currentNeedleAngle);
const currentNormalized = THREE.MathUtils.clamp(currentMA / MAX_CURRENT_MA, 0, 1);
const particlesVisible = currentMA > 0.01;
beadMaterial.opacity = 0.3 + currentNormalized * 0.7;
const flowSpeed = 0.025 + currentNormalized * 0.14;
beads.forEach((bead, index) => {
bead.visible = particlesVisible;
if (!particlesVisible) return;
const u = (elapsed * flowSpeed + index / beads.length) % 1;
bead.position.copy(flowCurve.getPointAt(u));
});
renderer.render(scene, camera);
if (firstFrame) {
firstFrame = false;
loading.classList.add("hidden");
}
}
updateState(voltage);
resizeScene();
resizeChart();
animate();
</script>
</body>
</html>
```