Prompt specification
Build or customize the Three.js 3D scene "Ideal transformer ratio explorer" 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: Ideal transformer ratio explorer
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/ideal-transformer-ratio-explorer-x1676
Scene Description Prompt:
Create a single self-contained HTML file using Three.js from cdnjs that shows an ideal two-coil transformer. Allow changing primary and secondary turns and a resistive virtual load; compute ideal voltage and current relationships while clearly excluding losses, saturation, and safety design. Voxel style.
Working Three.js Starter (ideal-transformer-ratio-explorer-x1676):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Voxel Transformer Lab</title>
<style>
:root {
color-scheme: dark;
--bg: #07101d;
--panel: #0b1525;
--panel-2: #101d30;
--line: #22334a;
--text: #eef5ff;
--muted: #91a4bb;
--primary: #ffb547;
--secondary: #9b8cff;
--flux: #35d6ff;
--green: #6ee7a8;
--warning: #ff8c66;
}
* {
box-sizing: border-box;
}
html,
body {
margin: 0;
min-height: 100%;
background: var(--bg);
color: var(--text);
font-family:
Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
body {
overflow: hidden;
}
button,
input {
font: inherit;
}
button {
color: inherit;
}
.app-header {
height: 76px;
display: flex;
align-items: center;
justify-content: space-between;
padding: 0 24px;
border-bottom: 1px solid var(--line);
background: rgba(7, 16, 29, 0.96);
position: relative;
z-index: 2;
}
.brand {
display: flex;
align-items: center;
gap: 14px;
min-width: 0;
}
.voxel-mark {
width: 42px;
height: 42px;
position: relative;
flex: 0 0 auto;
transform: rotate(-8deg);
}
.voxel-mark span {
position: absolute;
width: 19px;
height: 19px;
border-radius: 3px;
box-shadow: inset -4px -4px 0 rgba(0, 0, 0, 0.16);
}
.voxel-mark span:nth-child(1) {
left: 1px;
top: 2px;
background: var(--primary);
}
.voxel-mark span:nth-child(2) {
left: 21px;
top: 2px;
background: var(--secondary);
}
.voxel-mark span:nth-child(3) {
left: 11px;
top: 22px;
background: var(--flux);
}
.eyebrow {
margin: 0 0 3px;
color: var(--muted);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.18em;
text-transform: uppercase;
}
h1 {
margin: 0;
font-size: clamp(19px, 2vw, 25px);
line-height: 1;
letter-spacing: -0.025em;
}
.header-chips {
display: flex;
align-items: center;
gap: 8px;
}
.chip {
border: 1px solid var(--line);
border-radius: 999px;
padding: 7px 11px;
background: var(--panel-2);
color: #bdcbe0;
font-size: 10px;
font-weight: 800;
letter-spacing: 0.12em;
white-space: nowrap;
}
.chip.ideal {
color: var(--green);
border-color: rgba(110, 231, 168, 0.3);
background: rgba(110, 231, 168, 0.08);
}
.layout {
height: calc(100vh - 76px);
display: grid;
grid-template-columns: minmax(0, 1fr) 390px;
}
.viewport {
min-width: 0;
min-height: 0;
position: relative;
overflow: hidden;
background:
radial-gradient(circle at 48% 35%, rgba(54, 88, 125, 0.25), transparent 44%),
radial-gradient(circle at 80% 90%, rgba(155, 140, 255, 0.08), transparent 35%),
linear-gradient(145deg, #0a1627, #050b14);
}
#scene {
width: 100%;
height: 100%;
display: block;
cursor: grab;
touch-action: none;
outline: none;
}
#scene:focus-visible {
box-shadow: inset 0 0 0 2px var(--flux);
}
#scene.dragging {
cursor: grabbing;
}
.scene-hud {
position: absolute;
inset: 18px 18px auto;
display: flex;
justify-content: space-between;
align-items: flex-start;
gap: 12px;
pointer-events: none;
}
.hud-block {
border: 1px solid rgba(92, 119, 151, 0.22);
border-radius: 10px;
background: rgba(7, 15, 27, 0.76);
backdrop-filter: blur(9px);
padding: 10px 12px;
}
.hud-title {
color: #8fa4bd;
font-size: 9px;
font-weight: 800;
letter-spacing: 0.16em;
text-transform: uppercase;
}
.hud-value {
margin-top: 4px;
font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
font-size: 13px;
font-weight: 800;
}
.ratio-badge {
color: var(--secondary);
}
.scene-legend {
position: absolute;
left: 18px;
bottom: 18px;
display: flex;
flex-wrap: wrap;
gap: 7px;
max-width: calc(100% - 36px);
pointer-events: none;
}
.legend-item {
display: flex;
align-items: center;
gap: 7px;
border: 1px solid rgba(92, 119, 151, 0.24);
border-radius: 7px;
background: rgba(7, 15, 27, 0.78);
backdrop-filter: blur(8px);
padding: 7px 9px;
color: #b9c8db;
font-size: 10px;
font-weight: 700;
}
.legend-dot {
width: 8px;
height: 8px;
border-radius: 2px;
box-shadow: 0 0 10px currentColor;
}
.scene-hint {
position: absolute;
right: 18px;
bottom: 18px;
color: #8093aa;
font-size: 10px;
pointer-events: none;
text-align: right;
line-height: 1.55;
}
.loading {
position: absolute;
inset: 0;
display: grid;
place-items: center;
background: #07101d;
color: var(--muted);
font-size: 13px;
letter-spacing: 0.08em;
z-index: 5;
}
[hidden] {
display: none !important;
}
.sidebar {
min-height: 0;
overflow-y: auto;
border-left: 1px solid var(--line);
background: linear-gradient(180deg, #0c1727, #09121f);
scrollbar-width: thin;
scrollbar-color: #2b3d55 transparent;
}
.sidebar-inner {
padding: 18px;
}
.scope-note {
border: 1px solid rgba(255, 140, 102, 0.28);
border-left: 3px solid var(--warning);
border-radius: 10px;
padding: 12px 13px;
background: rgba(255, 140, 102, 0.06);
color: #c6d0df;
font-size: 11px;
line-height: 1.55;
}
.scope-note strong {
display: block;
margin-bottom: 4px;
color: #ffad8f;
font-size: 11px;
letter-spacing: 0.05em;
text-transform: uppercase;
}
.section {
margin-top: 20px;
padding-top: 18px;
border-top: 1px solid var(--line);
}
.section-title {
margin: 0 0 14px;
display: flex;
align-items: center;
justify-content: space-between;
gap: 10px;
color: #dbe7f5;
font-size: 12px;
font-weight: 800;
letter-spacing: 0.09em;
text-transform: uppercase;
}
.section-title small {
color: var(--muted);
font-size: 9px;
font-weight: 700;
letter-spacing: 0.05em;
}
.control {
margin-bottom: 17px;
}
.control:last-child {
margin-bottom: 0;
}
.control-head {
display: flex;
align-items: baseline;
justify-content: space-between;
gap: 10px;
margin-bottom: 8px;
}
.control label {
color: #c9d7e8;
font-size: 12px;
font-weight: 650;
}
.control output {
min-width: 62px;
padding: 4px 7px;
border: 1px solid var(--line);
border-radius: 6px;
background: #0a1422;
color: var(--text);
font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
font-size: 11px;
font-weight: 800;
text-align: right;
}
.control.primary output {
color: var(--primary);
border-color: rgba(255, 181, 71, 0.28);
}
.control.secondary output {
color: var(--secondary);
border-color: rgba(155, 140, 255, 0.28);
}
.control.load output {
color: var(--flux);
border-color: rgba(53, 214, 255, 0.28);
}
input[type="range"] {
width: 100%;
height: 5px;
display: block;
margin: 7px 0 0;
border-radius: 999px;
appearance: none;
background: #25364c;
cursor: pointer;
}
input[type="range"]::-webkit-slider-thumb {
width: 17px;
height: 17px;
border: 3px solid #07101d;
border-radius: 5px;
appearance: none;
background: var(--accent, #d8e8fb);
box-shadow: 0 0 0 1px var(--accent, #d8e8fb);
}
input[type="range"]::-moz-range-thumb {
width: 12px;
height: 12px;
border: 3px solid #07101d;
border-radius: 5px;
background: var(--accent, #d8e8fb);
box-shadow: 0 0 0 1px var(--accent, #d8e8fb);
}
.range-limits {
display: flex;
justify-content: space-between;
margin-top: 6px;
color: #61758d;
font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
font-size: 9px;
}
.presets {
display: flex;
gap: 7px;
flex-wrap: wrap;
margin-top: 12px;
}
.preset {
flex: 1 1 auto;
border: 1px solid var(--line);
border-radius: 7px;
padding: 7px 9px;
background: #101d30;
color: #aebed2;
cursor: pointer;
font-size: 10px;
font-weight: 800;
transition:
border-color 120ms ease,
color 120ms ease,
background 120ms ease;
}
.preset:hover,
.preset:focus-visible {
border-color: #55708f;
color: white;
background: #15263c;
outline: none;
}
.toggle-row {
display: flex;
align-items: center;
justify-content: space-between;
gap: 12px;
margin-top: 14px;
color: #9eb0c5;
font-size: 11px;
}
.toggle-row label {
display: flex;
align-items: center;
gap: 8px;
cursor: pointer;
}
.toggle-row input {
accent-color: var(--flux);
}
.small-button {
border: 1px solid var(--line);
border-radius: 7px;
padding: 6px 9px;
background: #101d30;
color: #b9c8da;
cursor: pointer;
font-size: 10px;
font-weight: 800;
}
.small-button:hover,
.small-button:focus-visible {
border-color: #55708f;
color: white;
outline: none;
}
.metrics {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 9px;
}
.metric {
min-height: 94px;
border: 1px solid var(--line);
border-radius: 10px;
padding: 11px;
background:
linear-gradient(145deg, rgba(255, 255, 255, 0.035), transparent),
#0d1929;
}
.metric-label {
display: flex;
align-items: center;
justify-content: space-between;
color: #8fa2b9;
font-size: 9px;
font-weight: 800;
letter-spacing: 0.06em;
text-transform: uppercase;
}
.metric-label span {
color: #4f647c;
font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
font-size: 10px;
}
.metric-value {
margin-top: 9px;
color: white;
font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
font-size: clamp(17px, 2vw, 21px);
font-weight: 850;
letter-spacing: -0.04em;
}
.metric-foot {
margin-top: 5px;
color: #647991;
font-size: 9px;
}
.metric.voltage .metric-value {
color: var(--secondary);
}
.metric.current .metric-value {
color: var(--flux);
}
.metric.power .metric-value {
color: var(--green);
}
.metric.input .metric-value {
color: var(--primary);
}
.result-strip {
display: flex;
justify-content: space-between;
gap: 10px;
margin-top: 10px;
padding: 10px 11px;
border: 1px solid rgba(53, 214, 255, 0.18);
border-radius: 8px;
background: rgba(53, 214, 255, 0.045);
color: #91a8bf;
font-size: 10px;
line-height: 1.5;
}
.result-strip strong {
color: #d8f8ff;
font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
}
.equations {
display: grid;
gap: 7px;
}
.equation {
display: grid;
grid-template-columns: minmax(115px, auto) 1fr;
gap: 12px;
align-items: center;
border-radius: 7px;
padding: 9px 10px;
background: #0a1524;
border: 1px solid #18283d;
}
.equation code {
color: #d6e4f5;
font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
font-size: 11px;
font-weight: 800;
}
.equation span {
color: #71869e;
font-size: 10px;
line-height: 1.4;
}
.exclusions {
margin: 0;
padding-left: 17px;
color: #899db4;
font-size: 10px;
line-height: 1.7;
}
.exclusions li::marker {
color: var(--warning);
}
.final-warning {
margin-top: 13px;
padding: 10px;
border-radius: 8px;
background: rgba(255, 140, 102, 0.07);
color: #d8a28e;
font-size: 10px;
line-height: 1.55;
}
@media (max-width: 920px) {
body {
overflow: auto;
}
.app-header {
height: 70px;
padding: 0 16px;
}
.header-chips {
display: none;
}
.layout {
height: auto;
grid-template-columns: 1fr;
}
.viewport {
height: 58vh;
min-height: 430px;
}
.sidebar {
border-left: 0;
border-top: 1px solid var(--line);
overflow: visible;
}
.sidebar-inner {
max-width: 700px;
margin: 0 auto;
padding: 18px;
}
.scene-hint {
display: none;
}
}
@media (max-width: 540px) {
.voxel-mark {
display: none;
}
.viewport {
min-height: 390px;
}
.scene-hud {
inset: 12px 12px auto;
}
.hud-block {
padding: 8px 9px;
}
.scene-legend {
left: 12px;
bottom: 12px;
gap: 5px;
}
.legend-item {
padding: 6px 7px;
font-size: 9px;
}
.metrics {
gap: 7px;
}
.metric {
min-height: 88px;
padding: 9px;
}
}
</style>
</head>
<body>
<header class="app-header">
<div class="brand">
<div class="voxel-mark" aria-hidden="true">
<span></span><span></span><span></span>
</div>
<div>
<p class="eyebrow">Interactive Electromagnetics</p>
<h1>Voxel Transformer Lab</h1>
</div>
</div>
<div class="header-chips">
<span class="chip ideal">Ideal model</span>
<span class="chip">AC · RMS</span>
<span class="chip">60 Hz</span>
</div>
</header>
<main class="layout">
<section class="viewport" id="viewport" aria-label="Interactive three-dimensional transformer model">
<canvas
id="scene"
tabindex="0"
aria-label="3D transformer. Drag to orbit, scroll to zoom, and use arrow keys to rotate."
></canvas>
<div class="loading" id="loading">ASSEMBLING VOXEL TRANSFORMER…</div>
<div class="scene-hud">
<div class="hud-block">
<div class="hud-title">3D voxel model</div>
<div class="hud-value" id="turnsBadge">48 : 24 TURNS</div>
</div>
<div class="hud-block">
<div class="hud-title">Transformation</div>
<div class="hud-value ratio-badge" id="ratioBadge">2 : 1 · STEP-DOWN</div>
</div>
</div>
<div class="scene-legend">
<div class="legend-item">
<span class="legend-dot" style="background:#ffb547;color:#ffb547"></span>
Primary winding
</div>
<div class="legend-item">
<span class="legend-dot" style="background:#9b8cff;color:#9b8cff"></span>
Secondary winding
</div>
<div class="legend-item">
<span class="legend-dot" style="background:#35d6ff;color:#35d6ff"></span>
Illustrative flux
</div>
</div>
<div class="scene-hint">
Drag to orbit · Wheel to zoom · Double-click to reset<br />
Flux animation is slowed and qualitative
</div>
</section>
<aside class="sidebar">
<div class="sidebar-inner">
<div class="scope-note">
<strong>Idealized educational model</strong>
Assumes a sinusoidal transformer and a purely resistive load. It deliberately
excludes losses, saturation, and safety design. Do not use the displayed
values as equipment ratings or as a construction plan.
</div>
<section class="section">
<h2 class="section-title">
Model inputs
<small>Live parameters</small>
</h2>
<div class="control primary">
<div class="control-head">
<label for="primaryTurns">Primary turns</label>
<output id="primaryTurnsOut" for="primaryTurns">48</output>
</div>
<input
id="primaryTurns"
type="range"
min="10"
max="80"
step="1"
value="48"
style="--accent:#ffb547"
/>
<div class="range-limits">
<span>10</span>
<span>Nₚ</span>
<span>80</span>
</div>
</div>
<div class="control secondary">
<div class="control-head">
<label for="secondaryTurns">Secondary turns</label>
<output id="secondaryTurnsOut" for="secondaryTurns">24</output>
</div>
<input
id="secondaryTurns"
type="range"
min="10"
max="80"
step="1"
value="24"
style="--accent:#9b8cff"
/>
<div class="range-limits">
<span>10</span>
<span>Nₛ</span>
<span>80</span>
</div>
</div>
<div class="control">
<div class="control-head">
<label for="sourceVoltage">Source RMS voltage</label>
<output id="sourceVoltageOut" for="sourceVoltage">120 V</output>
</div>
<input
id="sourceVoltage"
type="range"
min="24"
max="240"
step="1"
value="120"
style="--accent:#d8e8fb"
/>
<div class="range-limits">
<span>24 V</span>
<span>60 Hz</span>
<span>240 V</span>
</div>
</div>
<div class="control load">
<div class="control-head">
<label for="loadResistance">Resistive load</label>
<output id="loadResistanceOut" for="loadResistance">20 Ω</output>
</div>
<input
id="loadResistance"
type="range"
min="5"
max="500"
step="1"
value="20"
style="--accent:#35d6ff"
/>
<div class="range-limits">
<span>5 Ω</span>
<span>Rₗ only</span>
<span>500 Ω</span>
</div>
</div>
<div class="presets" aria-label="Turns ratio presets">
<button class="preset" type="button" data-preset="equal">1 : 1</button>
<button class="preset" type="button" data-preset="down">2 : 1 ↓</button>
<button class="preset" type="button" data-preset="up">1 : 2 ↑</button>
</div>
<div class="presets" aria-label="Load presets">
<button class="preset" type="button" data-load="5">Heavy · 5 Ω</button>
<button class="preset" type="button" data-load="20">Nominal · 20 Ω</button>
<button class="preset" type="button" data-load="200">Light · 200 Ω</button>
</div>
<div class="toggle-row">
<label>
<input id="autoRotate" type="checkbox" checked />
Auto orbit
</label>
<button id="resetView" class="small-button" type="button">Reset view</button>
</div>
</section>
<section class="section">
<h2 class="section-title">
Ideal results
<small>RMS magnitudes</small>
</h2>
<div class="metrics" id="results" aria-live="polite">
<div class="metric voltage">
<div class="metric-label">Secondary voltage <span>Vₛ</span></div>
<div class="metric-value" id="secondaryVoltage">60.0 V</div>
<div class="metric-foot">Across the load</div>
</div>
<div class="metric current">
<div class="metric-label">Load current <span>Iₛ</span></div>
<div class="metric-value" id="secondaryCurrent">3.00 A</div>
<div class="metric-foot">Resistive current</div>
</div>
<div class="metric power">
<div class="metric-label">Output power <span>Pₒᵤₜ</span></div>
<div class="metric-value" id="outputPower">180 W</div>
<div class="metric-foot">Vₛ × Iₛ</div>
</div>
<div class="metric input">
<div class="metric-label">Primary current <span>Iₚ</span></div>
<div class="metric-value" id="primaryCurrent">1.50 A</div>
<div class="metric-foot">Ideal input power / Vₚ</div>
</div>
</div>
<div class="result-strip">
<span>Reflected input resistance</span>
<strong id="reflectedResistance">80.0 Ω</strong>
</div>
<div class="result-strip">
<span>Power balance / ideal efficiency</span>
<strong id="powerBalance">180 W / 100%</strong>
</div>
<div class="result-strip">
<span>Ratio and load character</span>
<strong id="ratioDetail">Nₛ/Nₚ = 0.500 · PF = 1</strong>
</div>
</section>
<section class="section">
<h2 class="section-title">
Ideal relationships
<small>Magnitudes only</small>
</h2>
<div class="equations">
<div class="equation">
<code>Vₛ = Vₚ Nₛ/Nₚ</code>
<span>Ideal turns-ratio voltage relation</span>
</div>
<div class="equation">
<code>Iₛ = Vₛ/Rₗ</code>
<span>Ohm’s law for the virtual resistor</span>
</div>
<div class="equation">
<code>Pₒᵤₜ = VₛIₛ</code>
<span>Real power delivered to the resistive load</span>
</div>
<div class="equation">
<code>Iₚ = Pₒᵤₜ/Vₚ</code>
<span>Ideal power balance: Pᵢₙ = Pₒᵤₜ</span>
</div>
<div class="equation">
<code>Rᵢₙ = Rₗ(Nₚ/Nₛ)²</code>
<span>Load resistance reflected to the primary</span>
</div>
</div>
</section>
<section class="section">
<h2 class="section-title">
Explicit exclusions
<small>Not modeled</small>
</h2>
<ul class="exclusions">
<li>Copper winding resistance and I²R losses</li>
<li>Core hysteresis and eddy-current losses</li>
<li>Magnetizing current and finite core reluctance</li>
<li>Leakage inductance, leakage flux, and phase shift</li>
<li>Saturation, hysteresis, and nonlinear magnetization</li>
<li>Frequency, thermal, transient, and dielectric limits</li>
<li>Insulation, creepage, clearance, enclosure, or protection design</li>
<li>Shock, fire, arc-flash, environmental, or regulatory assessment</li>
</ul>
<div class="final-warning">
A high ideal turns ratio can produce a high calculated secondary voltage.
These mathematical results do not imply that any real transformer is safe,
suitably rated, compliant, or appropriate for construction.
</div>
</section>
</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 $ = (id) => document.getElementById(id);
const viewport = $("viewport");
const canvas = $("scene");
const COLORS = {
primary: 0xffb547,
secondary: 0x9b8cff,
flux: 0x35d6ff,
green: 0x6ee7a8,
warning: 0xff8c66
};
const SOURCE_FREQUENCY = 60;
const MAX_TURNS = 80;
const COIL_MIN_Y = 1.13;
const COIL_MAX_Y = 4.25;
// -------------------------------------------------------------------------
// Renderer, scene, and custom orthographic camera controls
// -------------------------------------------------------------------------
const renderer = new THREE.WebGLRenderer({
canvas,
antialias: true,
alpha: true,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio || 1, 2));
renderer.setClearColor(0x000000, 0);
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
const scene = new THREE.Scene();
const camera = new THREE.OrthographicCamera(-8, 8, 5, -5, 0.1, 100);
const cameraTarget = new THREE.Vector3(0, 2.15, 0);
const view = {
azimuth: 0.62,
polar: 1.02,
zoom: 1,
dragging: false,
pointerX: 0,
pointerY: 0
};
function updateCamera() {
const radius = 16;
const horizontal = Math.sin(view.polar) * radius;
camera.position.set(
cameraTarget.x + Math.sin(view.azimuth) * horizontal,
cameraTarget.y + Math.cos(view.polar) * radius,
cameraTarget.z + Math.cos(view.azimuth) * horizontal
);
camera.lookAt(cameraTarget);
camera.zoom = view.zoom;
camera.updateProjectionMatrix();
}
function resetCamera() {
view.azimuth = 0.62;
view.polar = 1.02;
view.zoom = 1;
updateCamera();
}
canvas.addEventListener("pointerdown", (event) => {
view.dragging = true;
view.pointerX = event.clientX;
view.pointerY = event.clientY;
canvas.setPointerCapture(event.pointerId);
canvas.classList.add("dragging");
});
canvas.addEventListener("pointermove", (event) => {
if (!view.dragging) return;
const dx = event.clientX - view.pointerX;
const dy = event.clientY - view.pointerY;
view.pointerX = event.clientX;
view.pointerY = event.clientY;
view.azimuth -= dx * 0.007;
view.polar = THREE.MathUtils.clamp(
view.polar + dy * 0.006,
0.48,
1.42
);
});
function endPointer(event) {
view.dragging = false;
canvas.classList.remove("dragging");
if (event.pointerId !== undefined && canvas.hasPointerCapture(event.pointerId)) {
canvas.releasePointerCapture(event.pointerId);
}
}
canvas.addEventListener("pointerup", endPointer);
canvas.addEventListener("pointercancel", endPointer);
canvas.addEventListener(
"wheel",
(event) => {
event.preventDefault();
view.zoom = THREE.MathUtils.clamp(
view.zoom * Math.exp(-event.deltaY * 0.001),
0.72,
1.75
);
updateCamera();
},
{ passive: false }
);
canvas.addEventListener("dblclick", resetCamera);
canvas.addEventListener("keydown", (event) => {
let handled = true;
switch (event.key) {
case "ArrowLeft":
view.azimuth -= 0.08;
break;
case "ArrowRight":
view.azimuth += 0.08;
break;
case "ArrowUp":
view.polar = THREE.MathUtils.clamp(view.polar - 0.06, 0.48, 1.42);
break;
case "ArrowDown":
view.polar = THREE.MathUtils.clamp(view.polar + 0.06, 0.48, 1.42);
break;
case "+":
case "=":
view.zoom = THREE.MathUtils.clamp(view.zoom + 0.08, 0.72, 1.75);
break;
case "-":
view.zoom = THREE.MathUtils.clamp(view.zoom - 0.08, 0.72, 1.75);
break;
case "r":
case "R":
resetCamera();
break;
default:
handled = false;
}
if (handled) {
event.preventDefault();
updateCamera();
}
});
function resize() {
const rect = viewport.getBoundingClientRect();
const width = Math.max(1, rect.width);
const height = Math.max(1, rect.height);
const aspect = width / height;
renderer.setSize(width, height, false);
const frustumHeight = Math.max(8.8, 13.8 / aspect);
camera.left = (-frustumHeight * aspect) / 2;
camera.right = (frustumHeight * aspect) / 2;
camera.top = frustumHeight / 2;
camera.bottom = -frustumHeight / 2;
updateCamera();
}
new ResizeObserver(resize).observe(viewport);
resize();
// -------------------------------------------------------------------------
// Lighting and voxel environment
// -------------------------------------------------------------------------
const hemisphere = new THREE.HemisphereLight(0xb9d8ff, 0x172033, 2.1);
scene.add(hemisphere);
const keyLight = new THREE.DirectionalLight(0xffffff, 3.1);
keyLight.position.set(5, 10, 7);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(2048, 2048);
keyLight.shadow.camera.left = -10;
keyLight.shadow.camera.right = 10;
keyLight.shadow.camera.top = 10;
keyLight.shadow.camera.bottom = -8;
keyLight.shadow.camera.near = 1;
keyLight.shadow.camera.far = 35;
keyLight.shadow.bias = -0.0005;
scene.add(keyLight);
const rimLight = new THREE.DirectionalLight(0x8c80ff, 1.4);
rimLight.position.set(-7, 5, -8);
scene.add(rimLight);
const warmLight = new THREE.PointLight(0xffa64d, 18, 12, 2);
warmLight.position.set(-5, 3, 3);
scene.add(warmLight);
const coolLight = new THREE.PointLight(0x7b8fff, 17, 12, 2);
coolLight.position.set(5, 3, 3);
scene.add(coolLight);
const unitBox = new THREE.BoxGeometry(1, 1, 1);
const unitEdges = new THREE.EdgesGeometry(unitBox);
const edgeMaterial = new THREE.LineBasicMaterial({
color: 0x9eb3ca,
transparent: true,
opacity: 0.18
});
function makeMaterial(color, options = {}) {
return new THREE.MeshStandardMaterial({
color,
roughness: 0.68,
metalness: 0.12,
...options
});
}
const materials = {
ground: makeMaterial(0x091321, { roughness: 0.94, metalness: 0 }),
plinth: makeMaterial(0x17263a, { roughness: 0.82 }),
dark: makeMaterial(0x172337, { roughness: 0.68 }),
darker: makeMaterial(0x0c1524, { roughness: 0.78 }),
copper: makeMaterial(0xd59055, { roughness: 0.4, metalness: 0.55 }),
ceramic: makeMaterial(0xbcb7ab, {
roughness: 0.88,
emissive: 0xff3b0a,
emissiveIntensity: 0.04
}),
band: makeMaterial(0xc75b3d, { roughness: 0.75 }),
sourceScreen: makeMaterial(0x4fd8b4, {
roughness: 0.32,
emissive: 0x28e0ad,
emissiveIntensity: 0.7
}),
primaryCable: makeMaterial(COLORS.primary, {
roughness: 0.42,
metalness: 0.28,
emissive: COLORS.primary,
emissiveIntensity: 0.08
}),
secondaryCable: makeMaterial(COLORS.secondary, {
roughness: 0.42,
metalness: 0.28,
emissive: COLORS.secondary,
emissiveIntensity: 0.08
})
};
function addBox(parent, size, position, material, outlined = false) {
const mesh = new THREE.Mesh(unitBox, material);
mesh.scale.set(size[0], size[1], size[2]);
mesh.position.set(position[0], position[1], position[2]);
mesh.castShadow = true;
mesh.receiveShadow = true;
parent.add(mesh);
if (outlined) {
const edges = new THREE.LineSegments(unitEdges, edgeMaterial);
mesh.add(edges);
}
return mesh;
}
const floor = addBox(
scene,
[42, 0.18, 42],
[0, -0.89, 0],
materials.ground
);
floor.castShadow = false;
const grid = new THREE.GridHelper(40, 40, 0x31506e, 0x182a40);
grid.position.y = -0.79;
grid.material.transparent = true;
grid.material.opacity = 0.38;
scene.add(grid);
const model = new THREE.Group();
scene.add(model);
addBox(model, [7.7, 0.38, 3.05], [0, -0.52, 0], materials.plinth, true);
addBox(model, [6.9, 0.24, 2.65], [0, -0.19, 0], materials.darker, true);
// Four low feet reinforce the voxel/diorama construction.
for (const x of [-3.2, 3.2]) {
for (const z of [-1.05, 1.05]) {
addBox(model, [0.62, 0.24, 0.62], [x, -0.67, z], materials.dark, true);
}
}
// -------------------------------------------------------------------------
// Laminated-looking voxel magnetic core
// -------------------------------------------------------------------------
const coreBlocks = [];
for (const x of [-1.65, 1.65]) {
for (let y = 0.99; y < 4.15; y += 0.43) {
coreBlocks.push({
position: [x, y, 0],
scale: [1.16, 0.39, 1.25]
});
}
}
for (const y of [0.45, 4.6]) {
for (let x = -1.5; x <= 1.5001; x += 0.5) {
coreBlocks.push({
position: [x, y, 0],
scale: [0.48, 1.0, 1.25]
});
}
}
const coreMaterial = makeMaterial(0xffffff, {
roughness: 0.78,
metalness: 0.16
});
const coreMesh = new THREE.InstancedMesh(
unitBox,
coreMaterial,
coreBlocks.length
);
coreMesh.castShadow = true;
coreMesh.receiveShadow = true;
const dummy = new THREE.Object3D();
const instanceColor = new THREE.Color();
const corePalette = [0x35475d, 0x3d5068, 0x2c3c51, 0x435771];
coreBlocks.forEach((block, index) => {
dummy.position.set(...block.position);
dummy.scale.set(...block.scale);
dummy.updateMatrix();
coreMesh.setMatrixAt(index, dummy.matrix);
instanceColor.setHex(corePalette[index % corePalette.length]);
coreMesh.setColorAt(index, instanceColor);
});
coreMesh.instanceMatrix.needsUpdate = true;
if (coreMesh.instanceColor) coreMesh.instanceColor.needsUpdate = true;
model.add(coreMesh);
// -------------------------------------------------------------------------
// Instanced square voxel windings
// -------------------------------------------------------------------------
function createCoilMaterial(baseColor, emissiveColor) {
return makeMaterial(0xffffff, {
roughness: 0.43,
metalness: 0.3,
emissive: emissiveColor,
emissiveIntensity: 0.14
});
}
const primaryCoil = new THREE.InstancedMesh(
unitBox,
createCoilMaterial(COLORS.primary, 0x7b3700),
MAX_TURNS * 4
);
const secondaryCoil = new THREE.InstancedMesh(
unitBox,
createCoilMaterial(COLORS.secondary, 0x271978),
MAX_TURNS * 4
);
for (const coil of [primaryCoil, secondaryCoil]) {
coil.castShadow = true;
coil.receiveShadow = true;
coil.frustumCulled = false;
model.add(coil);
}
function updateCoil(mesh, legX, turns, baseColor) {
const radius = 0.84;
const step = (COIL_MAX_Y - COIL_MIN_Y) / turns;
const thickness = THREE.MathUtils.clamp(step * 0.78, 0.028, 0.08);
const color = new THREE.Color(baseColor);
let instance = 0;
for (let turn = 0; turn < turns; turn++) {
const y = COIL_MIN_Y + (turn + 0.5) * step;
const segments = [
{
p: [legX, y, -radius],
s: [radius * 2, thickness, 0.105]
},
{
p: [legX, y, radius],
s: [radius * 2, thickness, 0.105]
},
{
p: [legX - radius, y, 0],
s: [0.105, thickness, radius * 2]
},
{
p: [legX + radius, y, 0],
s: [0.105, thickness, radius * 2]
}
];
for (const segment of segments) {
dummy.position.set(...segment.p);
dummy.scale.set(...segment.s);
dummy.updateMatrix();
mesh.setMatrixAt(instance, dummy.matrix);
mesh.setColorAt(instance, color);
instance++;
}
}
mesh.count = instance;
mesh.instanceMatrix.needsUpdate = true;
if (mesh.instanceColor) mesh.instanceColor.needsUpdate = true;
}
// -------------------------------------------------------------------------
// Source, resistive load, and block-style wiring
// -------------------------------------------------------------------------
addBox(model, [2.0, 0.34, 1.8], [-4.8, -0.56, 0], materials.plinth, true);
addBox(model, [2.0, 0.34, 1.8], [4.8, -0.56, 0], materials.plinth, true);
const sourceGroup = new THREE.Group();
sourceGroup.position.x = -4.8;
model.add(sourceGroup);
addBox(sourceGroup, [1.6, 1.25, 1.42], [0, 0.25, 0], materials.dark, true);
addBox(
sourceGroup,
[0.98, 0.52, 0.09],
[0, 0.43, 0.755],
materials.sourceScreen,
true
);
for (const z of [-0.45, 0.45]) {
addBox(
sourceGroup,
[0.36, 0.3, 0.34],
[0.97, 0.6, z],
materials.copper,
true
);
}
const loadGroup = new THREE.Group();
loadGroup.position.x = 4.8;
model.add(loadGroup);
addBox(loadGroup, [1.6, 1.18, 1.42], [0, 0.25, 0], materials.darker, true);
addBox(
loadGroup,
[1.18, 0.72, 1.04],
[0, 0.62, 0.06],
materials.ceramic,
true
);
for (const x of [-0.38, 0, 0.38]) {
addBox(
loadGroup,
[0.16, 0.76, 1.08],
[x, 0.62, 0.06],
materials.band
);
}
for (const z of [-0.45, 0.45]) {
addBox(
loadGroup,
[0.36, 0.3, 0.34],
[-0.97, 0.6, z],
materials.copper,
true
);
}
function addCableSegment(parent, start, end, material, thickness = 0.1) {
const delta = new THREE.Vector3(
end[0] - start[0],
end[1] - start[1],
end[2] - start[2]
);
const length = delta.length();
const midpoint = new THREE.Vector3(
(start[0] + end[0]) / 2,
(start[1] + end[1]) / 2,
(start[2] + end[2]) / 2
);
const mesh = new THREE.Mesh(unitBox, material);
mesh.scale.set(length, thickness, thickness);
mesh.position.copy(midpoint);
mesh.castShadow = true;
parent.add(mesh);
return mesh;
}
// Primary source leads.
addCableSegment(model, [-3.65, 0.6, -0.45], [-2.49, 0.6, -0.45], materials.primaryCable);
addCableSegment(model, [-2.49, 0.6, -0.45], [-2.49, 0.6, -0.84], materials.primaryCable);
addCableSegment(model, [-2.49, 0.6, -0.84], [-2.49, 1.13, -0.84], materials.primaryCable);
addCableSegment(model, [-3.65, 0.6, 0.45], [-2.49, 0.6, 0.45], materials.primaryCable);
addCableSegment(model, [-2.49, 0.6, 0.45], [-2.49, 0.6, 0.84], materials.primaryCable);
addCableSegment(model, [-2.49, 0.6, 0.84], [-2.49, 1.13, 0.84], materials.primaryCable);
// Secondary load leads. They remain electrically separate from the source.
addCableSegment(model, [2.49, 1.13, -0.84], [2.49, 0.6, -0.84], materials.secondaryCable);
addCableSegment(model, [2.49, 0.6, -0.84], [2.49, 0.6, -0.45], materials.secondaryCable);
addCableSegment(model, [2.49, 0.6, -0.45], [3.65, 0.6, -0.45], materials.secondaryCable);
addCableSegment(model, [2.49, 1.13, 0.84], [2.49, 0.6, 0.84], materials.secondaryCable);
addCableSegment(model, [2.49, 0.6, 0.84], [2.49, 0.6, 0.45], materials.secondaryCable);
addCableSegment(model, [2.49, 0.6, 0.45], [3.65, 0.6, 0.45], materials.secondaryCable);
// -------------------------------------------------------------------------
// Canvas sprite labels
// -------------------------------------------------------------------------
function roundedRect(context, x, y, width, height, radius) {
context.beginPath();
context.moveTo(x + radius, y);
context.lineTo(x + width - radius, y);
context.quadraticCurveTo(x + width, y, x + width, y + radius);
context.lineTo(x + width, y + height - radius);
context.quadraticCurveTo(
x + width,
y + height,
x + width - radius,
y + height
);
context.lineTo(x + radius, y + height);
context.quadraticCurveTo(x, y + height, x, y + height - radius);
context.lineTo(x, y + radius);
context.quadraticCurveTo(x, y, x + radius, y);
context.closePath();
}
function createLabelSprite(title, value, accent, width = 2.85) {
const labelCanvas = document.createElement("canvas");
labelCanvas.width = 512;
labelCanvas.height = 160;
const context = labelCanvas.getContext("2d");
const texture = new THREE.CanvasTexture(labelCanvas);
texture.colorSpace = THREE.SRGBColorSpace;
texture.minFilter = THREE.LinearFilter;
const material = new THREE.SpriteMaterial({
map: texture,
transparent: true,
depthTest: false,
depthWrite: false
});
const sprite = new THREE.Sprite(material);
sprite.scale.set(width, width / 3.2, 1);
sprite.renderOrder = 20;
sprite.userData = {
canvas: labelCanvas,
context,
texture,
title,
value,
accent
};
drawLabel(sprite);
return sprite;
}
function drawLabel(sprite) {
const { canvas, context, texture, title, value, accent } = sprite.userData;
const accentHex = `#${new THREE.Color(accent).getHexString()}`;
context.clearRect(0, 0, canvas.width, canvas.height);
roundedRect(context, 5, 5, 502, 150, 20);
context.fillStyle = "rgba(7, 15, 28, 0.88)";
context.fill();
context.lineWidth = 4;
context.strokeStyle = accentHex;
context.stroke();
context.textAlign = "center";
context.textBaseline = "middle";
context.fillStyle = accentHex;
context.font = "800 26px ui-monospace, monospace";
context.fillText(title.toUpperCase(), 256, 45, 465);
context.fillStyle = "#f2f7ff";
const fontSize = value.length > 19 ? 36 : value.length > 14 ? 40 : 47;
context.font = `800 ${fontSize}px ui-monospace, monospace`;
context.fillText(value, 256, 99, 465);
texture.needsUpdate = true;
}
const primaryLabel = createLabelSprite(
"Primary winding",
"48 TURNS",
COLORS.primary,
2.65
);
primaryLabel.position.set(-1.65, 5.65, 0);
model.add(primaryLabel);
const secondaryLabel = createLabelSprite(
"Secondary winding",
"24 TURNS · 60 V",
COLORS.secondary,
2.85
);
secondaryLabel.position.set(1.65, 5.65, 0);
model.add(secondaryLabel);
const sourceLabel = createLabelSprite(
"Ideal source",
"120 V RMS",
COLORS.green,
2.35
);
sourceLabel.position.set(-4.8, 1.55, 0);
model.add(sourceLabel);
const loadLabel = createLabelSprite(
"Virtual resistor",
"20 Ω",
COLORS.warning,
2.4
);
loadLabel.position.set(4.8, 1.65, 0);
model.add(loadLabel);
// -------------------------------------------------------------------------
// Qualitative magnetic-flux cubes
// -------------------------------------------------------------------------
const fluxCount = 28;
const fluxMaterial = makeMaterial(COLORS.flux, {
roughness: 0.28,
metalness: 0.05,
emissive: COLORS.flux,
emissiveIntensity: 1.1,
transparent: true,
opacity: 0.82
});
const fluxMesh = new THREE.InstancedMesh(unitBox, fluxMaterial, fluxCount);
fluxMesh.frustumCulled = false;
model.add(fluxMesh);
const fluxPath = [
new THREE.Vector3(-1.65, 0.45, 0.67),
new THREE.Vector3(-1.65, 4.6, 0.67),
new THREE.Vector3(1.65, 4.6, 0.67),
new THREE.Vector3(1.65, 0.45, 0.67)
];
const fluxSegmentLengths = [];
let totalFluxLength = 0;
for (let i = 0; i < fluxPath.length; i++) {
const length = fluxPath[i].distanceTo(fluxPath[(i + 1) % fluxPath.length]);
fluxSegmentLengths.push(length);
totalFluxLength += length;
}
function pointOnFluxPath(progress) {
let distance = ((progress % 1) + 1) % 1 * totalFluxLength;
for (let i = 0; i < fluxPath.length; i++) {
const length = fluxSegmentLengths[i];
if (distance <= length) {
const t = length === 0 ? 0 : distance / length;
return new THREE.Vector3().lerpVectors(
fluxPath[i],
fluxPath[(i + 1) % fluxPath.length],
t
);
}
distance -= length;
}
return fluxPath[0].clone();
}
// -------------------------------------------------------------------------
// Calculations and UI
// -------------------------------------------------------------------------
function formatVoltage(value) {
if (Math.abs(value) >= 1000) {
return `${(value / 1000).toFixed(2)} kV`;
}
return `${value.toFixed(value < 10 ? 2 : 1)} V`;
}
function formatCurrent(value) {
if (Math.abs(value) < 1) {
const milliamps = value * 1000;
return `${milliamps.toFixed(milliamps < 10 ? 1 : 0)} mA`;
}
return `${value.toFixed(2)} A`;
}
function formatPower(value) {
if (Math.abs(value) >= 1_000_000) {
return `${(value / 1_000_000).toFixed(2)} MW`;
}
if (Math.abs(value) >= 1000) {
return `${(value / 1000).toFixed(2)} kW`;
}
return `${value.toFixed(value < 10 ? 2 : 0)} W`;
}
function formatResistance(value) {
if (value >= 1000) {
return `${(value / 1000).toFixed(2)} kΩ`;
}
return `${value.toFixed(value < 100 ? 1 : 0)} Ω`;
}
function updateModel() {
const np = Number($("primaryTurns").value);
const ns = Number($("secondaryTurns").value);
const vp = Number($("sourceVoltage").value);
const rl = Number($("loadResistance").value);
const turnsRatio = ns / np;
const vs = vp * turnsRatio;
const iSec = vs / rl;
const pOut = vs * iSec;
const iPrimary = pOut / vp;
const reflectedR = rl * Math.pow(np / ns, 2);
$("primaryTurnsOut").textContent = `${np}`;
$("secondaryTurnsOut").textContent = `${ns}`;
$("sourceVoltageOut").textContent = `${vp} V`;
$("loadResistanceOut").textContent = `${rl} Ω`;
$("secondaryVoltage").textContent = formatVoltage(vs);
$("secondaryCurrent").textContent = formatCurrent(iSec);
$("outputPower").textContent = formatPower(pOut);
$("primaryCurrent").textContent = formatCurrent(iPrimary);
$("reflectedResistance").textContent = formatResistance(reflectedR);
$("powerBalance").textContent = `${formatPower(pOut)} / 100%`;
$("ratioDetail").textContent =
`Nₛ/Nₚ = ${turnsRatio.toFixed(3)} · PF = 1`;
$("turnsBadge").textContent = `${np} : ${ns} TURNS`;
if (ns === np) {
$("ratioBadge").textContent = "1 : 1 · BILATERAL";
} else if (ns < np) {
$("ratioBadge").textContent = `${np} : ${ns} · STEP-DOWN`;
} else {
$("ratioBadge").textContent = `${np} : ${ns} · STEP-UP`;
}
updateCoil(primaryCoil, -1.65, np, COLORS.primary);
updateCoil(secondaryCoil, 1.65, ns, COLORS.secondary);
primaryLabel.userData.value = `${np} TURNS`;
drawLabel(primaryLabel);
secondaryLabel.userData.value = `${ns} TURNS · ${formatVoltage(vs)}`;
drawLabel(secondaryLabel);
sourceLabel.userData.value = `${vp} V RMS`;
drawLabel(sourceLabel);
loadLabel.userData.value = `${rl} Ω`;
drawLabel(loadLabel);
const heat = THREE.MathUtils.clamp(pOut / 600, 0, 1);
materials.ceramic.emissiveIntensity = 0.04 + heat * 1.25;
materials.sourceScreen.emissiveIntensity = 0.6 + Math.min(vp / 240, 1) * 0.45;
model.userData.loadHeat = heat;
}
for (const id of [
"primaryTurns",
"secondaryTurns",
"sourceVoltage",
"loadResistance"
]) {
$(id).addEventListener("input", updateModel);
}
document.querySelectorAll("[data-preset]").forEach((button) => {
button.addEventListener("click", () => {
const preset = button.dataset.preset;
if (preset === "equal") {
$("primaryTurns").value = 60;
$("secondaryTurns").value = 60;
} else if (preset === "down") {
$("primaryTurns").value = 80;
$("secondaryTurns").value = 40;
} else if (preset === "up") {
$("primaryTurns").value = 40;
$("secondaryTurns").value = 80;
}
updateModel();
});
});
document.querySelectorAll("[data-load]").forEach((button) => {
button.addEventListener("click", () => {
$("loadResistance").value = button.dataset.load;
updateModel();
});
});
$("resetView").addEventListener("click", resetCamera);
// -------------------------------------------------------------------------
// Animation
// -------------------------------------------------------------------------
const clock = new THREE.Clock();
let elapsed = 0;
function animate() {
const delta = Math.min(clock.getDelta(), 0.05);
elapsed += delta;
if ($("autoRotate").checked && !view.dragging) {
view.azimuth += delta * 0.075;
updateCamera();
}
for (let i = 0; i < fluxCount; i++) {
const progress = (i / fluxCount + elapsed * 0.075) % 1;
const point = pointOnFluxPath(progress);
dummy.position.copy(point);
dummy.scale.set(0.14, 0.14, 0.14);
dummy.updateMatrix();
fluxMesh.setMatrixAt(i, dummy.matrix);
}
fluxMesh.instanceMatrix.needsUpdate = true;
fluxMaterial.emissiveIntensity =
0.85 + 0.4 * (0.5 + 0.5 * Math.sin(elapsed * 2.2));
const heat = model.userData.loadHeat || 0;
materials.ceramic.emissiveIntensity =
(0.04 + heat * 1.25) * (0.9 + 0.1 * Math.sin(elapsed * 3.1));
renderer.render(scene, camera);
}
updateModel();
renderer.setAnimationLoop(animate);
$("loading").hidden = true;
</script>
</body>
</html>
```