Prompt specification
Build or customize the Three.js 3D scene "Moving magnet and moving coil comparison" inside the active workspace.
Engineering Constraints:
1. Runtime & Orbit: Use Three.js r160 with OrbitControls (damping enabled) and ACESFilmicToneMapping.
2. Frame Timing: Drive all motion strictly with clock.getDelta() so animation speed is FPS-independent.
3. Viewport Hygiene: Handle window resize cleanly, cap DPR at Math.min(window.devicePixelRatio, 2), and dispose geometries/materials/renderer on unmount.
Scene Metadata:
Scene: Moving magnet and moving coil comparison
Category: Science & Physics · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/moving-magnet-and-moving-coil-comparison-x1634
Scene Description Prompt:
Compare stylized moving-magnet, moving-coil, and stationary setups to show when magnetic flux changes. Build standalone HTML with Three.js from cdnjs.
Working Three.js Starter (moving-magnet-and-moving-coil-comparison-x1634):
```html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Magnetic Flux: What Changes?</title>
<style>
:root {
color-scheme: dark;
--bg: #050912;
--panel: #0b1320;
--panel-2: #101b2b;
--border: rgba(151, 190, 224, 0.16);
--text: #eef6ff;
--muted: #8da3b9;
--cyan: #52ddff;
--orange: #ff9b70;
--purple: #b7a0ff;
}
* {
box-sizing: border-box;
}
body {
margin: 0;
min-width: 320px;
min-height: 100vh;
background:
radial-gradient(circle at 12% 0%, rgba(52, 125, 189, 0.18), transparent 34rem),
radial-gradient(circle at 90% 12%, rgba(111, 79, 176, 0.13), transparent 30rem),
var(--bg);
color: var(--text);
font-family:
Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
button,
input {
font: inherit;
}
button,
input[type="range"],
input[type="checkbox"] {
accent-color: var(--cyan);
}
.app {
width: min(1480px, calc(100% - 32px));
margin: 0 auto;
padding: 42px 0 36px;
}
.hero {
display: grid;
grid-template-columns: minmax(0, 1fr) auto;
gap: 32px;
align-items: end;
margin-bottom: 24px;
}
.eyebrow {
margin: 0 0 10px;
color: var(--cyan);
font-size: 0.72rem;
font-weight: 800;
letter-spacing: 0.18em;
text-transform: uppercase;
}
h1 {
max-width: 900px;
margin: 0;
font-size: clamp(2.1rem, 5vw, 4.65rem);
line-height: 0.98;
letter-spacing: -0.055em;
}
.hero-copy {
max-width: 720px;
margin: 18px 0 0;
color: var(--muted);
font-size: clamp(1rem, 1.7vw, 1.15rem);
line-height: 1.65;
}
.hero-copy strong {
color: var(--text);
font-weight: 650;
}
.equation-card {
min-width: 245px;
padding: 18px 20px;
border: 1px solid var(--border);
border-radius: 18px;
background: linear-gradient(145deg, rgba(20, 34, 52, 0.92), rgba(8, 15, 26, 0.86));
box-shadow: 0 18px 70px rgba(0, 0, 0, 0.22);
}
.equation-card .label {
color: var(--muted);
font-size: 0.72rem;
font-weight: 700;
letter-spacing: 0.12em;
text-transform: uppercase;
}
.equation {
margin-top: 9px;
font-family: Georgia, "Times New Roman", serif;
font-size: 1.55rem;
letter-spacing: 0.02em;
}
.equation small {
display: block;
margin-top: 7px;
color: var(--muted);
font-family: inherit;
font-size: 0.77rem;
line-height: 1.45;
}
.controls {
display: flex;
flex-wrap: wrap;
align-items: center;
gap: 12px 18px;
margin-bottom: 14px;
padding: 13px 15px;
border: 1px solid var(--border);
border-radius: 16px;
background: rgba(10, 18, 30, 0.82);
backdrop-filter: blur(16px);
}
.control-button {
min-width: 92px;
padding: 9px 15px;
border: 1px solid rgba(82, 221, 255, 0.25);
border-radius: 10px;
background: rgba(82, 221, 255, 0.11);
color: var(--text);
cursor: pointer;
transition: background 150ms ease, transform 150ms ease;
}
.control-button:hover {
background: rgba(82, 221, 255, 0.19);
transform: translateY(-1px);
}
.control-button.secondary {
border-color: var(--border);
background: rgba(255, 255, 255, 0.035);
}
.slider-control {
display: grid;
grid-template-columns: auto minmax(100px, 210px) 56px;
align-items: center;
gap: 10px;
min-width: min(100%, 350px);
}
.slider-control label {
color: var(--muted);
font-size: 0.82rem;
font-weight: 650;
white-space: nowrap;
}
input[type="range"] {
width: 100%;
cursor: pointer;
}
output {
color: var(--text);
font-size: 0.82rem;
font-variant-numeric: tabular-nums;
text-align: right;
}
.check-control {
display: flex;
align-items: center;
gap: 8px;
margin-left: auto;
color: var(--muted);
font-size: 0.82rem;
white-space: nowrap;
}
.stage-shell {
overflow: hidden;
border: 1px solid var(--border);
border-radius: 24px;
background: #080e17;
box-shadow: 0 30px 100px rgba(0, 0, 0, 0.35);
}
.stage {
position: relative;
width: 100%;
height: 510px;
overflow: hidden;
isolation: isolate;
}
#sceneCanvas {
position: absolute;
inset: 0;
display: block;
width: 100%;
height: 100%;
}
.views {
position: absolute;
inset: 0;
display: grid;
grid-template-columns: repeat(3, minmax(0, 1fr));
pointer-events: none;
}
.viewport {
position: relative;
min-width: 0;
overflow: hidden;
border-right: 1px solid var(--border);
pointer-events: auto;
touch-action: none;
}
.viewport:last-child {
border-right: 0;
}
.viewport::after {
position: absolute;
inset: 0;
content: "";
pointer-events: none;
background:
linear-gradient(to bottom, rgba(3, 7, 13, 0.34), transparent 30%),
linear-gradient(to top, rgba(3, 7, 13, 0.32), transparent 38%);
}
.scene-heading {
position: absolute;
z-index: 2;
top: 16px;
left: 16px;
right: 16px;
pointer-events: none;
}
.scene-heading .mode {
display: inline-flex;
align-items: center;
gap: 7px;
margin-bottom: 8px;
padding: 5px 8px;
border: 1px solid rgba(131, 177, 214, 0.18);
border-radius: 999px;
background: rgba(5, 10, 18, 0.7);
color: #b9c9d8;
font-size: 0.64rem;
font-weight: 800;
letter-spacing: 0.12em;
text-transform: uppercase;
backdrop-filter: blur(12px);
}
.mode-dot {
width: 6px;
height: 6px;
border-radius: 50%;
background: var(--cyan);
box-shadow: 0 0 12px var(--cyan);
}
.scene-heading h2 {
margin: 0;
font-size: clamp(1.15rem, 2vw, 1.65rem);
letter-spacing: -0.025em;
}
.scene-heading p {
margin: 5px 0 0;
color: #8ea5ba;
font-size: 0.78rem;
}
.hud {
position: absolute;
z-index: 2;
right: 12px;
bottom: 12px;
left: 12px;
padding: 12px;
border: 1px solid rgba(147, 187, 220, 0.15);
border-radius: 15px;
background: rgba(5, 10, 18, 0.78);
box-shadow: 0 10px 35px rgba(0, 0, 0, 0.2);
backdrop-filter: blur(14px);
pointer-events: none;
}
.status-row {
display: flex;
align-items: center;
justify-content: space-between;
gap: 8px;
margin-bottom: 9px;
}
.status {
display: inline-flex;
align-items: center;
gap: 6px;
color: #95a9bb;
font-size: 0.62rem;
font-weight: 850;
letter-spacing: 0.1em;
text-transform: uppercase;
}
.status::before {
width: 6px;
height: 6px;
border-radius: 50%;
content: "";
background: currentColor;
box-shadow: 0 0 10px currentColor;
}
.status.changing {
color: var(--orange);
}
.status.constant {
color: var(--cyan);
}
.speed-label {
color: #71879c;
font-size: 0.62rem;
font-variant-numeric: tabular-nums;
}
.readouts {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 9px;
}
.readout {
min-width: 0;
padding: 8px 9px;
border-radius: 10px;
background: rgba(255, 255, 255, 0.035);
}
.readout span {
display: block;
margin-bottom: 3px;
color: #8298ad;
font-size: 0.62rem;
white-space: nowrap;
}
.readout strong {
display: block;
overflow: hidden;
font-size: clamp(0.85rem, 1.5vw, 1.02rem);
font-variant-numeric: tabular-nums;
text-overflow: ellipsis;
white-space: nowrap;
}
.flux-track {
position: relative;
height: 7px;
margin-top: 9px;
overflow: hidden;
border-radius: 999px;
background:
linear-gradient(
to right,
rgba(255, 255, 255, 0.07) 0 49.5%,
rgba(255, 255, 255, 0.4) 49.5% 50.5%,
rgba(255, 255, 255, 0.07) 50.5% 100%
);
}
.flux-fill {
position: absolute;
top: 0;
bottom: 0;
left: 50%;
width: 0;
border-radius: 999px;
transition: background 100ms linear;
}
.plot-label {
display: flex;
justify-content: space-between;
margin: 9px 1px 2px;
color: #687e93;
font-size: 0.58rem;
letter-spacing: 0.06em;
text-transform: uppercase;
}
.plot {
display: block;
width: 100%;
height: 44px;
overflow: visible;
}
.plot .zero {
stroke: rgba(151, 190, 224, 0.18);
stroke-width: 1;
stroke-dasharray: 3 4;
}
.plot .flux-path {
fill: none;
stroke: var(--cyan);
stroke-width: 2;
stroke-linecap: round;
stroke-linejoin: round;
vector-effect: non-scaling-stroke;
}
.plot .flux-dot {
fill: var(--cyan);
filter: drop-shadow(0 0 4px rgba(82, 221, 255, 0.8));
}
.stage-note {
display: flex;
flex-wrap: wrap;
justify-content: space-between;
gap: 10px 24px;
padding: 12px 18px;
border-top: 1px solid var(--border);
background: rgba(5, 10, 18, 0.9);
color: #7990a5;
font-size: 0.72rem;
line-height: 1.45;
}
.stage-note .interaction {
color: #9db0c1;
}
.takeaway {
display: grid;
grid-template-columns: auto 1fr;
gap: 18px;
align-items: start;
margin-top: 18px;
padding: 20px 22px;
border: 1px solid var(--border);
border-radius: 18px;
background:
linear-gradient(120deg, rgba(82, 221, 255, 0.08), transparent 42%),
rgba(10, 18, 30, 0.78);
}
.takeaway-icon {
display: grid;
width: 40px;
height: 40px;
place-items: center;
border: 1px solid rgba(82, 221, 255, 0.25);
border-radius: 12px;
background: rgba(82, 221, 255, 0.1);
color: var(--cyan);
font-size: 1.2rem;
font-weight: 800;
}
.takeaway h2 {
margin: 0 0 6px;
font-size: 1rem;
}
.takeaway p {
margin: 0;
color: #91a6b9;
font-size: 0.86rem;
line-height: 1.6;
}
.takeaway strong {
color: var(--text);
}
@media (max-width: 920px) {
.hero {
grid-template-columns: 1fr;
}
.equation-card {
width: min(100%, 360px);
}
.stage {
height: 1320px;
}
.views {
grid-template-columns: 1fr;
grid-template-rows: repeat(3, minmax(0, 1fr));
}
.viewport {
border-right: 0;
border-bottom: 1px solid var(--border);
}
.viewport:last-child {
border-bottom: 0;
}
}
@media (max-width: 580px) {
.app {
width: min(100% - 20px, 1480px);
padding-top: 26px;
}
.hero-copy {
font-size: 0.96rem;
}
.controls {
align-items: stretch;
}
.slider-control {
grid-template-columns: 1fr;
width: 100%;
}
.slider-control output {
text-align: left;
}
.check-control {
margin-left: 0;
}
.stage {
height: 1420px;
}
.takeaway {
grid-template-columns: 1fr;
}
}
</style>
</head>
<body>
<main class="app">
<header class="hero">
<div>
<p class="eyebrow">Electromagnetic induction laboratory</p>
<h1>What actually changes the flux?</h1>
<p class="hero-copy">
A magnetic field can pass through a coil without inducing a voltage.
The important condition is a <strong>changing magnetic flux</strong>.
Compare moving the magnet, moving the coil, and leaving both still.
</p>
</div>
<aside class="equation-card" aria-label="Faraday's law">
<div class="label">Faraday's law</div>
<div class="equation">
ε = −dΦ/dt
<small>
EMF depends on the rate of flux change,<br />
not on whether the magnet or the coil moves.
</small>
</div>
</aside>
</header>
<section class="controls" aria-label="Simulation controls">
<button class="control-button" id="toggleBtn" type="button">Pause</button>
<button class="control-button secondary" id="resetBtn" type="button">Reset</button>
<div class="slider-control">
<label for="position">Relative offset</label>
<input
id="position"
type="range"
min="-2.65"
max="2.65"
step="0.01"
value="2.37"
aria-label="Relative offset between magnet and coil"
>
<output id="positionValue" for="position">+2.37</output>
</div>
<div class="slider-control">
<label for="speed">Playback speed</label>
<input
id="speed"
type="range"
min="0.25"
max="2"
step="0.05"
value="1"
aria-label="Playback speed"
>
<output id="speedValue" for="speed">1.00×</output>
</div>
<label class="check-control">
<input id="fieldToggle" type="checkbox" checked>
Show field lines
</label>
</section>
<section class="stage-shell" aria-label="Three magnetic flux comparison views">
<div class="stage" id="stage">
<canvas
id="sceneCanvas"
aria-label="Interactive three-dimensional comparison of magnetic flux experiments"
></canvas>
<div class="views">
<article class="viewport" id="view0">
<div class="scene-heading">
<div class="mode">
<span class="mode-dot"></span>
Moving magnet
</div>
<h2>The magnet moves</h2>
<p>The coil stays fixed at the origin.</p>
</div>
<div class="hud">
<div class="status-row">
<span class="status changing" id="status0">Flux changing</span>
<span class="speed-label" id="speedLabel0">RELATIVE SPEED</span>
</div>
<div class="readouts">
<div class="readout">
<span>Flux Φ</span>
<strong id="flux0">+0.00</strong>
</div>
<div class="readout">
<span>EMF ε (arb.)</span>
<strong id="emf0">+0.00</strong>
</div>
</div>
<div class="flux-track"><i class="flux-fill" id="fill0"></i></div>
<div class="plot-label">
<span>Flux versus time</span>
<span>−1 0 +1</span>
</div>
<svg class="plot" viewBox="0 0 240 54" preserveAspectRatio="none">
<line class="zero" x1="0" y1="27" x2="240" y2="27"></line>
<path class="flux-path" id="graph0"></path>
<circle class="flux-dot" id="dot0" cx="240" cy="27" r="3"></circle>
</svg>
</div>
</article>
<article class="viewport" id="view1">
<div class="scene-heading">
<div class="mode">
<span class="mode-dot"></span>
Moving coil
</div>
<h2>The coil moves</h2>
<p>The magnet and its field stay fixed.</p>
</div>
<div class="hud">
<div class="status-row">
<span class="status changing" id="status1">Flux changing</span>
<span class="speed-label" id="speedLabel1">RELATIVE SPEED</span>
</div>
<div class="readouts">
<div class="readout">
<span>Flux Φ</span>
<strong id="flux1">+0.00</strong>
</div>
<div class="readout">
<span>EMF ε (arb.)</span>
<strong id="emf1">+0.00</strong>
</div>
</div>
<div class="flux-track"><i class="flux-fill" id="fill1"></i></div>
<div class="plot-label">
<span>Flux versus time</span>
<span>−1 0 +1</span>
</div>
<svg class="plot" viewBox="0 0 240 54" preserveAspectRatio="none">
<line class="zero" x1="0" y1="27" x2="240" y2="27"></line>
<path class="flux-path" id="graph1"></path>
<circle class="flux-dot" id="dot1" cx="240" cy="27" r="3"></circle>
</svg>
</div>
</article>
<article class="viewport" id="view2">
<div class="scene-heading">
<div class="mode">
<span class="mode-dot" style="background:#8298ad;box-shadow:none"></span>
Stationary
</div>
<h2>Both objects are still</h2>
<p>The flux may be present, but it does not change.</p>
</div>
<div class="hud">
<div class="status-row">
<span class="status constant" id="status2">Constant flux · no EMF</span>
<span class="speed-label">NO RELATIVE SPEED</span>
</div>
<div class="readouts">
<div class="readout">
<span>Flux Φ</span>
<strong id="flux2">+0.37</strong>
</div>
<div class="readout">
<span>EMF ε (arb.)</span>
<strong id="emf2">0.00</strong>
</div>
</div>
<div class="flux-track"><i class="flux-fill" id="fill2"></i></div>
<div class="plot-label">
<span>Flux versus time</span>
<span>−1 0 +1</span>
</div>
<svg class="plot" viewBox="0 0 240 54" preserveAspectRatio="none">
<line class="zero" x1="0" y1="27" x2="240" y2="27"></line>
<path class="flux-path" id="graph2"></path>
<circle class="flux-dot" id="dot2" cx="240" cy="27" r="3"></circle>
</svg>
</div>
</article>
</div>
</div>
<div class="stage-note">
<span>
Stylized dipole field and axial-flux model. Values are normalized for comparison.
</span>
<span class="interaction">Drag a view to orbit · scroll to zoom</span>
</div>
</section>
<section class="takeaway">
<div class="takeaway-icon">!</div>
<div>
<h2>Relative motion is the key</h2>
<p>
The moving-magnet and moving-coil examples produce the same changing
flux for the same relative separation: <strong>the coil moves into a
different part of the magnet's field</strong>. In the stationary case,
the flux can be nonzero, but <strong>dΦ/dt = 0</strong>, so no induced
EMF appears.
</p>
</div>
</section>
</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 canvas = $("sceneCanvas");
const stage = $("stage");
const viewElements = [$("view0"), $("view1"), $("view2")];
const renderer = new THREE.WebGLRenderer({
canvas,
antialias: true,
alpha: false,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio || 1, 2));
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.1;
const COIL_RADIUS = 0.72;
const AMPLITUDE = 2.65;
const MOTION_FREQUENCY = 0.62;
const STATIONARY_OFFSET = 0.95;
// Normalized axial dipole field model:
// f(d) = d / (R² + d²)^(5/2)
function rawFlux(distance) {
const r2 = COIL_RADIUS * COIL_RADIUS;
return distance / Math.pow(r2 + distance * distance, 2.5);
}
// The maximum of the model occurs at d = R / 2.
const RAW_FLUX_MAX = rawFlux(COIL_RADIUS / 2);
function normalizedFlux(distance) {
return rawFlux(distance) / RAW_FLUX_MAX;
}
function normalizedFluxDerivative(distance) {
const r2 = COIL_RADIUS * COIL_RADIUS;
return (
(r2 - 4 * distance * distance) /
Math.pow(r2 + distance * distance, 3.5) /
RAW_FLUX_MAX
);
}
function makeTextSprite(text, color) {
const labelCanvas = document.createElement("canvas");
labelCanvas.width = 128;
labelCanvas.height = 64;
const context = labelCanvas.getContext("2d");
context.clearRect(0, 0, 128, 64);
context.fillStyle = "rgba(5, 10, 18, 0.75)";
context.beginPath();
context.roundRect(10, 8, 108, 48, 20);
context.fill();
context.fillStyle = color;
context.font = "800 36px system-ui, sans-serif";
context.textAlign = "center";
context.textBaseline = "middle";
context.fillText(text, 64, 33);
const texture = new THREE.CanvasTexture(labelCanvas);
texture.colorSpace = THREE.SRGBColorSpace;
const sprite = new THREE.Sprite(
new THREE.SpriteMaterial({
map: texture,
transparent: true,
depthWrite: false
})
);
sprite.scale.set(0.42, 0.21, 1);
return sprite;
}
function createMagnet() {
const root = new THREE.Group();
const northMaterial = new THREE.MeshStandardMaterial({
color: 0xef4e61,
metalness: 0.28,
roughness: 0.34,
emissive: 0x3b050b,
emissiveIntensity: 0.35
});
const southMaterial = new THREE.MeshStandardMaterial({
color: 0x397cf2,
metalness: 0.3,
roughness: 0.32,
emissive: 0x041a4d,
emissiveIntensity: 0.4
});
const geometry = new THREE.CylinderGeometry(0.38, 0.38, 0.66, 32);
const north = new THREE.Mesh(geometry, northMaterial);
const south = new THREE.Mesh(geometry, southMaterial);
north.rotation.z = Math.PI / 2;
south.rotation.z = Math.PI / 2;
north.position.x = 0.33;
south.position.x = -0.33;
root.add(north, south);
const band = new THREE.Mesh(
new THREE.TorusGeometry(0.385, 0.035, 8, 48),
new THREE.MeshStandardMaterial({
color: 0xb7c8d7,
metalness: 0.85,
roughness: 0.22
})
);
band.rotation.y = Math.PI / 2;
root.add(band);
const northLabel = makeTextSprite("N", "#ff9aaa");
const southLabel = makeTextSprite("S", "#9fc6ff");
northLabel.position.set(0.45, 0.52, 0.18);
southLabel.position.set(-0.45, 0.52, 0.18);
root.add(northLabel, southLabel);
return root;
}
function createField(animateParticles = false) {
const root = new THREE.Group();
const particles = [];
const lineMaterial = new THREE.LineBasicMaterial({
color: 0x54c9ff,
transparent: true,
opacity: 0.17,
depthWrite: false
});
for (let i = 0; i < 7; i++) {
const a = 1.13 + (i % 3) * 0.13;
const b = 0.75 + (i % 2) * 0.13;
const rotation = (i / 7) * Math.PI * 2;
const points = [];
for (let j = 0; j <= 128; j++) {
const t = (j / 128) * Math.PI * 2;
points.push(
new THREE.Vector3(
a * Math.cos(t),
b * Math.sin(t) * Math.cos(rotation),
b * Math.sin(t) * Math.sin(rotation)
)
);
}
const geometry = new THREE.BufferGeometry().setFromPoints(points);
const line = new THREE.Line(geometry, lineMaterial);
root.add(line);
if (animateParticles) {
const particle = new THREE.Mesh(
new THREE.SphereGeometry(0.045, 10, 8),
new THREE.MeshBasicMaterial({ color: 0x83eeff })
);
root.add(particle);
particles.push({
mesh: particle,
a,
b,
rotation,
phase: i * 0.137
});
}
}
const axisGeometry = new THREE.BufferGeometry().setFromPoints([
new THREE.Vector3(-2.35, 0, 0),
new THREE.Vector3(2.35, 0, 0)
]);
const axis = new THREE.Line(
axisGeometry,
new THREE.LineDashedMaterial({
color: 0x72d9ff,
dashSize: 0.09,
gapSize: 0.075,
transparent: true,
opacity: 0.16,
depthWrite: false
})
);
axis.computeLineDistances();
root.add(axis);
root.userData.particles = particles;
return root;
}
class HelixCurve extends THREE.Curve {
constructor(width, radius, turns) {
super();
this.width = width;
this.radius = radius;
this.turns = turns;
}
getPoint(t, target = new THREE.Vector3()) {
const angle = t * this.turns * Math.PI * 2;
return target.set(
(t - 0.5) * this.width,
this.radius * Math.cos(angle),
this.radius * Math.sin(angle)
);
}
}
function createCoil() {
const root = new THREE.Group();
const copper = new THREE.MeshStandardMaterial({
color: 0xffa35d,
metalness: 0.78,
roughness: 0.24,
emissive: 0x4a1704,
emissiveIntensity: 0.42
});
const helix = new THREE.CatmullRomCurve3(
Array.from({ length: 240 }, (_, index) => {
const t = index / 239;
const angle = t * 8 * Math.PI * 2;
return new THREE.Vector3(
(t - 0.5) * 1.14,
COIL_RADIUS * Math.cos(angle),
COIL_RADIUS * Math.sin(angle)
);
})
);
const winding = new THREE.Mesh(
new THREE.TubeGeometry(helix, 240, 0.043, 7, false),
copper
);
root.add(winding);
for (const x of [-0.57, 0.57]) {
const rim = new THREE.Mesh(
new THREE.TorusGeometry(COIL_RADIUS, 0.025, 8, 64),
copper
);
rim.rotation.y = Math.PI / 2;
rim.position.x = x;
root.add(rim);
}
const disk = new THREE.Mesh(
new THREE.CircleGeometry(COIL_RADIUS * 0.9, 64),
new THREE.MeshBasicMaterial({
color: 0x52ddff,
transparent: true,
opacity: 0.12,
side: THREE.DoubleSide,
depthWrite: false
})
);
disk.rotation.y = Math.PI / 2;
root.add(disk);
const arrow = new THREE.ArrowHelper(
new THREE.Vector3(1, 0, 0),
new THREE.Vector3(-1.02, 0, 0),
2.04,
0x52ddff,
0.25,
0.13
);
arrow.line.material.transparent = true;
arrow.line.material.opacity = 0.82;
arrow.cone.material.transparent = true;
arrow.cone.material.opacity = 0.9;
root.add(arrow);
root.userData.material = copper;
root.userData.disk = disk;
root.userData.arrow = arrow;
return root;
}
function createAxisGuide() {
const group = new THREE.Group();
const positions = [];
positions.push(-3.4, -0.93, 0, 3.4, -0.93, 0);
for (let x = -3.4; x <= 3.4; x += 0.4) {
positions.push(x, -0.96, 0, x, -0.87, 0);
}
const geometry = new THREE.BufferGeometry();
geometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(positions, 3)
);
const guide = new THREE.LineSegments(
geometry,
new THREE.LineBasicMaterial({
color: 0x4d6d87,
transparent: true,
opacity: 0.42
})
);
group.add(guide);
return group;
}
function createSetup(index) {
const backgrounds = [0x08131f, 0x0d1020, 0x12131c];
const scene = new THREE.Scene();
scene.background = new THREE.Color(backgrounds[index]);
scene.fog = new THREE.Fog(backgrounds[index], 9, 17);
const camera = new THREE.PerspectiveCamera(42, 1, 0.1, 50);
const cameraState = {
theta: 0.57,
targetTheta: 0.57,
phi: 1.02,
targetPhi: 1.02,
radius: 8.6,
targetRadius: 8.6
};
scene.add(
new THREE.HemisphereLight(0xa9d9ff, 0x07101a, 1.6)
);
const key = new THREE.DirectionalLight(0xffffff, 2.2);
key.position.set(4, 7, 5);
scene.add(key);
const rimLight = new THREE.PointLight(0x4ec8ff, 9, 12);
rimLight.position.set(-3, 2, -3);
scene.add(rimLight);
const floor = new THREE.Mesh(
new THREE.PlaneGeometry(16, 16),
new THREE.MeshStandardMaterial({
color: 0x080e17,
metalness: 0.1,
roughness: 0.9
})
);
floor.rotation.x = -Math.PI / 2;
floor.position.y = -1.08;
scene.add(floor);
const grid = new THREE.GridHelper(14, 28, 0x24465f, 0x172c3d);
grid.position.y = -1.065;
grid.material.transparent = true;
grid.material.opacity = 0.22;
scene.add(grid, createAxisGuide());
const magnet = createMagnet();
const field = createField(index === 0);
magnet.add(field);
const coil = createCoil();
scene.add(magnet, coil);
return {
scene,
camera,
cameraState,
magnet,
field,
coil,
animateFieldParticles: index === 0
};
}
const setups = [createSetup(0), createSetup(1), createSetup(2)];
const reducedMotion = window.matchMedia(
"(prefers-reduced-motion: reduce)"
).matches;
let playing = !reducedMotion;
let speed = 1;
let phase = Math.asin(2.37 / AMPLITUDE);
let cycleTime = 0;
let simulationTime = 0;
let offset = AMPLITUDE * Math.sin(phase);
let velocity = 0;
let lastGraphUpdate = 0;
const histories = [[], [], []];
const paths = [$("graph0"), $("graph1"), $("graph2")];
const dots = [$("dot0"), $("dot1"), $("dot2")];
function signed(value, digits = 2) {
const sign = value >= 0 ? "+" : "−";
return sign + Math.abs(value).toFixed(digits);
}
function setPlaying(nextPlaying) {
playing = nextPlaying;
$("toggleBtn").textContent = playing ? "Pause" : "Play";
}
function updateGraph(index, flux) {
const history = histories[index];
const step = history.length > 1 ? 240 / (history.length - 1) : 240;
const pathData = history
.map((value, pointIndex) => {
const x = pointIndex * step;
const y = 27 - THREE.MathUtils.clamp(value, -1, 1) * 23;
return `${pointIndex === 0 ? "M" : "L"}${x.toFixed(2)},${y.toFixed(2)}`;
})
.join(" ");
paths[index].setAttribute("d", pathData);
const lastY = 27 - THREE.MathUtils.clamp(flux, -1, 1) * 23;
dots[index].setAttribute("cx", "240");
dots[index].setAttribute("cy", lastY.toFixed(2));
}
function initializeHistories() {
for (let index = 0; index < 3; index++) {
const distance = index === 2 ? STATIONARY_OFFSET : offset;
const flux = normalizedFlux(distance);
histories[index] = Array.from({ length: 110 }, () => flux);
updateGraph(index, flux);
}
}
function updateMetrics(index, flux, emf) {
const magnitude = Math.min(1, Math.abs(flux));
const fluxColor = flux >= 0 ? "#52ddff" : "#ff9b70";
const fill = $(`fill${index}`);
const width = magnitude * 50;
fill.style.width = `${width}%`;
fill.style.left = flux >= 0 ? "50%" : `${50 - width}%`;
fill.style.background = fluxColor;
$(`flux${index}`).textContent = signed(flux, 3);
$(`flux${index}`).style.color = fluxColor;
$(`emf${index}`).textContent = signed(emf, 2);
$(`emf${index}`).style.color =
Math.abs(emf) > 0.015 ? "#ffad7d" : "#9fb2c3";
const status = $(`status${index}`);
status.classList.remove("changing", "constant");
if (index === 2) {
status.textContent = "Constant flux · no EMF";
status.classList.add("constant");
} else if (!playing) {
status.textContent = "Held · dΦ/dt = 0";
status.classList.add("constant");
} else if (Math.abs(emf) < 0.015) {
status.textContent = "Zero flux slope";
status.classList.add("constant");
} else {
status.textContent = "Flux changing";
status.classList.add("changing");
}
if (index !== 2) {
$(`speedLabel${index}`).textContent = playing
? "RELATIVE SPEED"
: "MOTION PAUSED";
}
}
function updateSetups() {
const distances = [offset, offset, STATIONARY_OFFSET];
const velocities = [velocity, velocity, 0];
for (let index = 0; index < setups.length; index++) {
const setup = setups[index];
const distance = distances[index];
const flux = normalizedFlux(distance);
const emf =
index === 2
? 0
: -normalizedFluxDerivative(distance) * velocities[index];
if (index === 0) {
setup.magnet.position.x = -offset;
setup.coil.position.x = 0;
} else if (index === 1) {
setup.magnet.position.x = 0;
setup.coil.position.x = offset;
} else {
setup.magnet.position.x = 0;
setup.coil.position.x = STATIONARY_OFFSET;
}
const color = new THREE.Color(
flux >= 0 ? 0x52ddff : 0xff9b70
);
setup.coil.userData.disk.material.color.copy(color);
setup.coil.userData.disk.material.opacity =
0.07 + Math.min(1, Math.abs(flux)) * 0.15;
setup.coil.userData.material.emissiveIntensity =
0.28 + Math.min(1.8, Math.abs(emf) * 0.08);
const direction = flux >= 0 ? 1 : -1;
setup.coil.userData.arrow.setDirection(
new THREE.Vector3(direction, 0, 0)
);
setup.coil.userData.arrow.position.set(-direction * 1.02, 0, 0);
setup.coil.userData.arrow.setColor(color);
if (setup.animateFieldParticles) {
for (const particle of setup.field.userData.particles) {
const t = (simulationTime * 0.18 + particle.phase) % 1;
const angle = t * Math.PI * 2;
particle.mesh.position.set(
particle.a * Math.cos(angle),
particle.b * Math.sin(angle) * Math.cos(particle.rotation),
particle.b * Math.sin(angle) * Math.sin(particle.rotation)
);
}
}
updateMetrics(index, flux, emf);
}
}
function updateCamera(setup) {
const state = setup.cameraState;
const smoothing = 0.13;
state.theta += (state.targetTheta - state.theta) * smoothing;
state.phi += (state.targetPhi - state.phi) * smoothing;
state.radius += (state.targetRadius - state.radius) * smoothing;
setup.camera.position.set(
state.radius *
Math.sin(state.phi) *
Math.sin(state.theta),
state.radius * Math.cos(state.phi),
state.radius *
Math.sin(state.phi) *
Math.cos(state.theta)
);
setup.camera.lookAt(0, 0, 0);
}
function resizeRenderer() {
const rect = stage.getBoundingClientRect();
renderer.setSize(rect.width, rect.height, false);
}
const resizeObserver = new ResizeObserver(resizeRenderer);
resizeObserver.observe(stage);
window.addEventListener("resize", resizeRenderer);
// Direct camera controls, implemented without external controls modules.
viewElements.forEach((view, index) => {
const state = setups[index].cameraState;
let dragging = false;
let previousX = 0;
let previousY = 0;
view.addEventListener("pointerdown", (event) => {
if (event.button !== 0) return;
dragging = true;
previousX = event.clientX;
previousY = event.clientY;
view.setPointerCapture(event.pointerId);
event.preventDefault();
});
view.addEventListener("pointermove", (event) => {
if (!dragging) return;
const deltaX = event.clientX - previousX;
const deltaY = event.clientY - previousY;
previousX = event.clientX;
previousY = event.clientY;
state.targetTheta -= deltaX * 0.006;
state.targetPhi = THREE.MathUtils.clamp(
state.targetPhi + deltaY * 0.005,
0.48,
1.38
);
});
const endDrag = (event) => {
dragging = false;
if (view.hasPointerCapture(event.pointerId)) {
view.releasePointerCapture(event.pointerId);
}
};
view.addEventListener("pointerup", endDrag);
view.addEventListener("pointercancel", endDrag);
view.addEventListener(
"wheel",
(event) => {
event.preventDefault();
state.targetRadius = THREE.MathUtils.clamp(
state.targetRadius + event.deltaY * 0.006,
5.5,
12
);
},
{ passive: false }
);
});
$("toggleBtn").addEventListener("click", () => {
setPlaying(!playing);
});
$("resetBtn").addEventListener("click", () => {
phase = Math.asin(2.37 / AMPLITUDE);
cycleTime = 0;
simulationTime = 0;
offset = AMPLITUDE * Math.sin(phase);
velocity = 0;
speed = 1;
$("speed").value = "1";
$("speedValue").textContent = "1.00×";
setPlaying(!reducedMotion);
initializeHistories();
});
$("position").addEventListener("input", (event) => {
offset = Number(event.target.value);
phase = Math.asin(THREE.MathUtils.clamp(offset / AMPLITUDE, -1, 1));
cycleTime = 0;
velocity = 0;
setPlaying(false);
});
$("speed").addEventListener("input", (event) => {
speed = Number(event.target.value);
$("speedValue").textContent = `${speed.toFixed(2)}×`;
});
$("fieldToggle").addEventListener("change", (event) => {
for (const setup of setups) {
setup.field.visible = event.target.checked;
}
});
resizeRenderer();
initializeHistories();
setPlaying(playing);
const clock = new THREE.Clock();
function animate() {
requestAnimationFrame(animate);
const dt = Math.min(clock.getDelta(), 0.05);
if (playing) {
cycleTime += dt * speed;
simulationTime += dt * speed;
const currentPhase = phase + cycleTime * MOTION_FREQUENCY;
offset = AMPLITUDE * Math.sin(currentPhase);
velocity =
AMPLITUDE *
MOTION_FREQUENCY *
Math.cos(currentPhase) *
speed;
} else {
velocity = 0;
}
$("position").value = offset.toFixed(2);
$("positionValue").textContent = signed(offset, 2);
updateSetups();
if (simulationTime - lastGraphUpdate >= 0.075) {
lastGraphUpdate = simulationTime;
for (let index = 0; index < setups.length; index++) {
const distance =
index === 2 ? STATIONARY_OFFSET : offset;
const flux = normalizedFlux(distance);
histories[index].push(flux);
if (histories[index].length > 150) {
histories[index].shift();
}
updateGraph(index, flux);
}
}
renderer.setScissorTest(false);
renderer.clear();
renderer.setScissorTest(true);
const canvasRect = canvas.getBoundingClientRect();
for (let index = 0; index < setups.length; index++) {
const viewRect = viewElements[index].getBoundingClientRect();
const left = viewRect.left - canvasRect.left;
const bottom = canvasRect.bottom - viewRect.bottom;
renderer.setViewport(left, bottom, viewRect.width, viewRect.height);
renderer.setScissor(left, bottom, viewRect.width, viewRect.height);
const setup = setups[index];
setup.camera.aspect = viewRect.width / viewRect.height;
setup.camera.updateProjectionMatrix();
updateCamera(setup);
renderer.render(setup.scene, setup.camera);
}
renderer.setScissorTest(false);
}
animate();
</script>
</body>
</html>
```