Prompt specification
Build or customize the Three.js 3D scene "Approach and withdrawal sign 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: Approach and withdrawal sign comparison
Category: Science & Physics · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/approach-and-withdrawal-sign-comparison-x1642
Scene Description Prompt:
Make a standalone HTML scene with Three.js from cdnjs that depicts Lenz's law and induced magnetic opposition. Place matched coils beside approaching and receding magnet proxies; reverse induced-current direction consistently and explain opposition to change rather than universal magnetic repulsion. Stylized solid forms.
Working Three.js Starter (approach-and-withdrawal-sign-comparison-x1642):
```html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Lenz's Law — Induced Magnetic Opposition</title>
<style>
:root {
color-scheme: dark;
--bg: #07111f;
--panel: #0d1b2c;
--text: #eef6ff;
--muted: #91a8bf;
--line: rgba(163, 201, 232, 0.16);
--cyan: #67d8ff;
--amber: #ffb24a;
--violet: #bb8cff;
--red: #ff5c72;
--blue: #4b91ff;
}
* {
box-sizing: border-box;
}
html,
body {
margin: 0;
min-height: 100%;
background:
radial-gradient(circle at 20% 0%, #10243a 0, transparent 38%),
radial-gradient(circle at 85% 15%, #1d1838 0, transparent 35%),
var(--bg);
color: var(--text);
font-family:
Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
body {
overflow-x: hidden;
}
button,
input {
font: inherit;
}
.app {
width: min(1440px, 100%);
margin: 0 auto;
padding: 26px 28px 36px;
}
header {
display: grid;
grid-template-columns: minmax(0, 1fr) minmax(260px, 460px);
gap: 32px;
align-items: end;
padding: 2px 4px 22px;
}
.eyebrow {
display: inline-flex;
align-items: center;
gap: 9px;
margin-bottom: 10px;
color: var(--cyan);
font-size: 0.72rem;
font-weight: 800;
letter-spacing: 0.17em;
text-transform: uppercase;
}
.eyebrow::before {
width: 26px;
height: 2px;
content: "";
background: var(--cyan);
box-shadow: 0 0 14px var(--cyan);
}
h1 {
margin: 0;
font-size: clamp(2rem, 4vw, 4.15rem);
font-weight: 760;
letter-spacing: -0.045em;
line-height: 0.98;
}
.lede {
margin: 0;
color: var(--muted);
font-size: clamp(0.95rem, 1.3vw, 1.08rem);
line-height: 1.65;
}
.lede strong {
color: var(--text);
font-weight: 650;
}
#stage {
position: relative;
height: clamp(540px, 64vh, 700px);
min-height: 540px;
overflow: hidden;
border: 1px solid var(--line);
border-radius: 24px;
background:
linear-gradient(rgba(113, 173, 220, 0.035) 1px, transparent 1px),
linear-gradient(90deg, rgba(113, 173, 220, 0.035) 1px, transparent 1px),
radial-gradient(circle at 50% 45%, rgba(33, 65, 91, 0.2), transparent 58%);
background-size: 42px 42px, 42px 42px, auto;
box-shadow:
0 28px 80px rgba(0, 0, 0, 0.34),
inset 0 1px 0 rgba(255, 255, 255, 0.035);
isolation: isolate;
}
#stage canvas {
position: absolute;
inset: 0;
z-index: 0;
display: block;
width: 100%;
height: 100%;
}
.world-label {
position: absolute;
z-index: 3;
pointer-events: none;
transform: translate(-50%, -50%);
white-space: nowrap;
}
.scene-title {
width: min(330px, 42vw);
text-align: center;
text-shadow: 0 3px 18px #06101d;
}
.scene-title .kicker {
display: block;
margin-bottom: 5px;
color: var(--cyan);
font-size: 0.68rem;
font-weight: 850;
letter-spacing: 0.17em;
text-transform: uppercase;
}
.scene-title strong {
display: block;
font-size: clamp(1rem, 1.65vw, 1.35rem);
letter-spacing: -0.02em;
}
.scene-title small {
display: block;
margin-top: 5px;
color: var(--muted);
font-size: 0.76rem;
}
.pole {
display: inline-flex;
align-items: center;
gap: 7px;
padding: 5px 9px 5px 5px;
border: 1px solid rgba(255, 255, 255, 0.13);
border-radius: 999px;
background: rgba(7, 17, 31, 0.82);
box-shadow: 0 8px 24px rgba(0, 0, 0, 0.25);
backdrop-filter: blur(9px);
color: #c9d8e7;
font-size: 0.68rem;
font-weight: 700;
letter-spacing: 0.02em;
}
.pole i {
display: grid;
width: 23px;
height: 23px;
place-items: center;
border-radius: 50%;
background: var(--red);
color: white;
font-size: 0.75rem;
font-style: normal;
font-weight: 900;
box-shadow: 0 0 14px rgba(255, 92, 114, 0.4);
}
.pole.blue i {
background: var(--blue);
box-shadow: 0 0 14px rgba(75, 145, 255, 0.4);
}
.pole.coil {
border-color: rgba(255, 92, 114, 0.32);
}
.pole.coil.blue {
border-color: rgba(75, 145, 255, 0.32);
}
.micro-label {
padding: 5px 9px;
border: 1px solid rgba(255, 178, 74, 0.24);
border-radius: 8px;
background: rgba(7, 17, 31, 0.78);
color: var(--amber);
font-size: 0.67rem;
font-weight: 800;
letter-spacing: 0.08em;
text-transform: uppercase;
backdrop-filter: blur(8px);
}
.logic-label {
width: min(340px, 44vw);
padding: 10px 13px;
border: 1px solid rgba(255, 255, 255, 0.11);
border-radius: 11px;
background: rgba(7, 17, 31, 0.84);
color: var(--muted);
text-align: center;
font-size: 0.72rem;
line-height: 1.45;
backdrop-filter: blur(10px);
}
.logic-label b {
color: var(--text);
font-weight: 700;
}
.legend {
position: absolute;
right: 16px;
bottom: 14px;
z-index: 4;
display: flex;
flex-wrap: wrap;
justify-content: flex-end;
gap: 8px 14px;
max-width: 430px;
padding: 9px 12px;
border: 1px solid var(--line);
border-radius: 12px;
background: rgba(7, 17, 31, 0.76);
color: var(--muted);
font-size: 0.68rem;
backdrop-filter: blur(12px);
pointer-events: none;
}
.legend span {
display: inline-flex;
align-items: center;
gap: 6px;
}
.legend i {
width: 17px;
height: 3px;
border-radius: 3px;
background: currentColor;
box-shadow: 0 0 10px currentColor;
}
.legend .flux {
color: var(--cyan);
}
.legend .induced {
color: var(--violet);
}
.legend .current {
color: var(--amber);
}
.view-note {
position: absolute;
top: 15px;
left: 16px;
z-index: 4;
padding: 7px 10px;
border: 1px solid var(--line);
border-radius: 999px;
background: rgba(7, 17, 31, 0.72);
color: var(--muted);
font-size: 0.65rem;
font-weight: 750;
letter-spacing: 0.08em;
text-transform: uppercase;
backdrop-filter: blur(10px);
}
.control-bar {
display: grid;
grid-template-columns: auto minmax(180px, 1fr) auto auto;
gap: 16px;
align-items: center;
margin: 14px 0 0;
padding: 13px 15px;
border: 1px solid var(--line);
border-radius: 16px;
background: rgba(12, 26, 42, 0.72);
backdrop-filter: blur(14px);
}
.control-bar button {
min-height: 38px;
padding: 0 14px;
border: 1px solid rgba(103, 216, 255, 0.28);
border-radius: 10px;
background: rgba(103, 216, 255, 0.1);
color: var(--text);
cursor: pointer;
font-size: 0.78rem;
font-weight: 750;
transition: background 150ms ease, transform 150ms ease;
}
.control-bar button:hover {
background: rgba(103, 216, 255, 0.18);
transform: translateY(-1px);
}
.control-bar button:focus-visible,
input:focus-visible {
outline: 2px solid var(--cyan);
outline-offset: 3px;
}
.timeline {
display: grid;
grid-template-columns: auto minmax(100px, 1fr) auto;
gap: 10px;
align-items: center;
color: var(--muted);
font-size: 0.72rem;
font-weight: 700;
}
input[type="range"] {
width: 100%;
accent-color: var(--cyan);
cursor: pointer;
}
#progressText {
min-width: 36px;
color: var(--text);
text-align: right;
font-variant-numeric: tabular-nums;
}
.toggle {
display: inline-flex;
align-items: center;
gap: 8px;
color: var(--muted);
font-size: 0.74rem;
font-weight: 700;
cursor: pointer;
user-select: none;
}
.toggle input {
width: 16px;
height: 16px;
accent-color: var(--violet);
}
.insight-grid {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 14px;
margin-top: 14px;
}
.insight {
position: relative;
overflow: hidden;
padding: 19px 20px;
border: 1px solid var(--line);
border-radius: 17px;
background: rgba(13, 27, 44, 0.66);
}
.insight::before {
position: absolute;
inset: 0 auto 0 0;
width: 3px;
content: "";
background: var(--red);
}
.insight.recede::before {
background: var(--blue);
}
.insight .tag {
color: var(--red);
font-size: 0.68rem;
font-weight: 850;
letter-spacing: 0.13em;
text-transform: uppercase;
}
.insight.recede .tag {
color: var(--blue);
}
.insight h2 {
margin: 7px 0 7px;
font-size: 1rem;
letter-spacing: -0.01em;
}
.insight p {
margin: 0;
color: var(--muted);
font-size: 0.84rem;
line-height: 1.6;
}
.principle {
margin: 14px 0 0;
padding: 16px 18px;
border: 1px solid rgba(187, 140, 255, 0.2);
border-radius: 15px;
background: linear-gradient(90deg, rgba(187, 140, 255, 0.09), rgba(187, 140, 255, 0.025));
color: #b8c8d8;
font-size: 0.88rem;
line-height: 1.65;
}
.principle strong {
color: var(--text);
}
.principle em {
color: var(--violet);
font-style: normal;
font-weight: 800;
}
#fallback {
position: absolute;
inset: 0;
z-index: 10;
display: none;
place-items: center;
padding: 30px;
background: var(--bg);
color: var(--text);
text-align: center;
}
@media (max-width: 820px) {
.app {
padding: 20px 14px 28px;
}
header {
grid-template-columns: 1fr;
gap: 12px;
}
.lede {
max-width: 680px;
}
#stage {
height: 570px;
min-height: 570px;
}
.control-bar {
grid-template-columns: auto 1fr;
}
.timeline {
grid-column: 1 / -1;
grid-row: 2;
}
.toggle {
justify-self: end;
}
.insight-grid {
grid-template-columns: 1fr;
}
.scene-title {
width: 46vw;
}
.scene-title small,
.minor-label,
.logic-label {
display: none;
}
.legend {
left: 12px;
right: 12px;
justify-content: center;
max-width: none;
}
}
@media (max-width: 520px) {
#stage {
height: 500px;
min-height: 500px;
border-radius: 18px;
}
.world-label {
transform: translate(-50%, -50%) scale(0.86);
}
.scene-title {
width: 49vw;
}
.scene-title .kicker {
font-size: 0.56rem;
}
.scene-title strong {
font-size: 0.88rem;
}
.micro-label {
font-size: 0.55rem;
}
.view-note {
display: none;
}
}
</style>
</head>
<body>
<main class="app">
<header>
<div>
<div class="eyebrow">Interactive electromagnetism</div>
<h1>Lenz’s law</h1>
</div>
<p class="lede">
Induced current creates a magnetic effect that opposes the
<strong>change in magnetic flux</strong>—not motion itself.
The same law can produce repulsion or attraction.
</p>
</header>
<section
id="stage"
role="img"
aria-label="Two matched coils beside approaching and receding bar magnets. The induced current and magnetic field reverse between the two cases."
>
<div class="view-note">Current directions viewed from magnet side</div>
<div id="titleL" class="world-label scene-title"></div>
<div id="titleR" class="world-label scene-title"></div>
<div id="magnetNL" class="world-label pole"></div>
<div id="magnetSL" class="world-label pole blue minor-label"></div>
<div id="coilL" class="world-label pole coil"></div>
<div id="coilR" class="world-label pole coil blue"></div>
<div id="currentL" class="world-label micro-label"></div>
<div id="currentR" class="world-label micro-label"></div>
<div id="logicL" class="world-label logic-label"></div>
<div id="logicR" class="world-label logic-label"></div>
<div class="legend" aria-hidden="true">
<span class="flux"><i></i>Magnet flux</span>
<span class="induced"><i></i>Induced field</span>
<span class="current"><i></i>Induced current</span>
</div>
<div id="fallback">
<p>This scene needs a browser with WebGL enabled.</p>
</div>
</section>
<section class="control-bar" aria-label="Scene controls">
<button id="playToggle" type="button">Pause</button>
<label class="timeline" for="timeline">
Experiment
<input id="timeline" type="range" min="0" max="1000" value="0">
<span id="progressText">0%</span>
</label>
<button id="resetButton" type="button">Reset</button>
<label class="toggle">
<input id="fieldToggle" type="checkbox" checked>
Field vectors
</label>
</section>
<section class="insight-grid" aria-label="Explanation">
<article class="insight">
<div class="tag">Approaching north pole</div>
<h2>Flux increases → induced field points backward</h2>
<p>
The coil’s near face becomes <strong>north</strong>, repelling the
approaching north pole. This repulsion opposes the increase in flux.
</p>
</article>
<article class="insight recede">
<div class="tag">Receding north pole</div>
<h2>Flux decreases → induced field points forward</h2>
<p>
The coil’s near face becomes <strong>south</strong>, attracting the
receding north pole. This attraction opposes the decrease in flux.
</p>
</article>
</section>
<p class="principle">
<strong>Key distinction:</strong> Lenz’s law says that an induced current
opposes the <em>change in magnetic flux</em> that caused it.
Therefore the resulting mechanical force may be repulsive or attractive;
it is <em>not universal magnetic repulsion</em>.
</p>
</main>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
const stage = document.querySelector("#stage");
const fallback = document.querySelector("#fallback");
let renderer;
try {
renderer = new THREE.WebGLRenderer({
antialias: true,
alpha: true,
powerPreference: "high-performance"
});
} catch (error) {
fallback.style.display = "grid";
throw error;
}
renderer.setPixelRatio(Math.min(window.devicePixelRatio || 1, 2));
renderer.setClearColor(0x07111f, 0);
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.08;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.domElement.setAttribute("aria-hidden", "true");
stage.prepend(renderer.domElement);
const scene = new THREE.Scene();
scene.fog = new THREE.Fog(0x07111f, 18, 34);
const camera = new THREE.OrthographicCamera(-6, 6, 3.5, -3.5, 0.1, 100);
camera.position.set(0, 1.35, 12);
camera.lookAt(0, 0.05, 0);
scene.add(new THREE.HemisphereLight(0xb8dcff, 0x08101b, 1.65));
const keyLight = new THREE.DirectionalLight(0xffffff, 2.8);
keyLight.position.set(1.5, 7, 10);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(1024, 1024);
keyLight.shadow.camera.left = -8;
keyLight.shadow.camera.right = 8;
keyLight.shadow.camera.top = 6;
keyLight.shadow.camera.bottom = -6;
keyLight.shadow.camera.near = 1;
keyLight.shadow.camera.far = 30;
keyLight.shadow.bias = -0.0005;
scene.add(keyLight);
const cyanLight = new THREE.PointLight(0x48cfff, 18, 12, 2);
cyanLight.position.set(-5, 2, 5);
scene.add(cyanLight);
const violetLight = new THREE.PointLight(0xa878ff, 16, 12, 2);
violetLight.position.set(5, 1, 4);
scene.add(violetLight);
const COLORS = {
copper: 0xd68a43,
copperDark: 0x7d3f21,
cyan: 0x67d8ff,
amber: 0xffb24a,
violet: 0xbb8cff,
red: 0xff5c72,
blue: 0x4b91ff,
white: 0xeaf6ff
};
const X_AXIS = new THREE.Vector3(1, 0, 0);
const Y_AXIS = new THREE.Vector3(0, 1, 0);
function standard(color, options = {}) {
return new THREE.MeshStandardMaterial({
color,
roughness: 0.38,
metalness: 0.42,
...options
});
}
function basic(color, opacity = 0.94) {
return new THREE.MeshBasicMaterial({
color,
transparent: opacity < 1,
opacity,
depthTest: false,
depthWrite: false
});
}
class HelixCurve extends THREE.Curve {
constructor(centerX, centerY, length, radius, turns) {
super();
this.centerX = centerX;
this.centerY = centerY;
this.length = length;
this.radius = radius;
this.turns = turns;
}
getPoint(t, target = new THREE.Vector3()) {
const angle = Math.PI * 2 * this.turns * t;
return target.set(
this.centerX - this.length / 2 + this.length * t,
this.centerY + this.radius * Math.cos(angle),
this.radius * Math.sin(angle)
);
}
}
function makeGlowTexture(hexColor) {
const canvas = document.createElement("canvas");
canvas.width = canvas.height = 128;
const context = canvas.getContext("2d");
const r = (hexColor >> 16) & 255;
const g = (hexColor >> 8) & 255;
const b = hexColor & 255;
const gradient = context.createRadialGradient(
64, 64, 0,
64, 64, 64
);
gradient.addColorStop(0, `rgba(${r}, ${g}, ${b}, 0.72)`);
gradient.addColorStop(0.25, `rgba(${r}, ${g}, ${b}, 0.34)`);
gradient.addColorStop(0.65, `rgba(${r}, ${g}, ${b}, 0.08)`);
gradient.addColorStop(1, `rgba(${r}, ${g}, ${b}, 0)`);
context.fillStyle = gradient;
context.fillRect(0, 0, 128, 128);
const texture = new THREE.CanvasTexture(canvas);
texture.colorSpace = THREE.SRGBColorSpace;
return texture;
}
const redGlow = makeGlowTexture(COLORS.red);
const blueGlow = makeGlowTexture(COLORS.blue);
function makeSolidArrow(color, length, category = "") {
const group = new THREE.Group();
const material = basic(color);
const headLength = Math.min(0.15, length * 0.34);
const shaftLength = Math.max(0.001, length - headLength);
const shaft = new THREE.Mesh(
new THREE.CylinderGeometry(
0.018,
0.018,
shaftLength,
10
),
material
);
shaft.rotation.z = -Math.PI / 2;
shaft.position.x = shaftLength / 2;
const head = new THREE.Mesh(
new THREE.ConeGeometry(0.075, headLength, 14),
material
);
head.rotation.z = -Math.PI / 2;
head.position.x = shaftLength + headLength / 2;
group.add(shaft, head);
group.userData.category = category;
group.renderOrder = 5;
return group;
}
function setArrowDirection(group, x, y = 0, z = 0) {
const direction = new THREE.Vector3(x, y, z).normalize();
group.quaternion.setFromUnitVectors(X_AXIS, direction);
}
function createPanel(worldX, scenario) {
const group = new THREE.Group();
group.position.x = worldX;
scene.add(group);
const isApproach = scenario === "approach";
const card = new THREE.Mesh(
new THREE.BoxGeometry(5.7, 4.35, 0.14),
standard(0x0d1b2c, {
roughness: 0.86,
metalness: 0.08,
transparent: true,
opacity: 0.94
})
);
card.position.set(0, 0, -1.13);
card.receiveShadow = true;
group.add(card);
const border = new THREE.LineSegments(
new THREE.EdgesGeometry(card.geometry),
new THREE.LineBasicMaterial({
color: 0x31506d,
transparent: true,
opacity: 0.55
})
);
border.position.copy(card.position);
group.add(border);
const accent = new THREE.Mesh(
new THREE.BoxGeometry(1.2, 0.035, 0.025),
basic(isApproach ? COLORS.red : COLORS.blue, 0.8)
);
accent.position.set(-1.65, 1.82, -1.02);
group.add(accent);
// Solid support and base.
const baseMaterial = standard(0x182b40, {
roughness: 0.68,
metalness: 0.26
});
const base = new THREE.Mesh(
new THREE.BoxGeometry(1.82, 0.17, 1.02),
baseMaterial
);
base.position.set(0.85, -1.13, -0.02);
base.castShadow = true;
base.receiveShadow = true;
group.add(base);
for (const supportX of [0.35, 1.35]) {
const support = new THREE.Mesh(
new THREE.CylinderGeometry(0.09, 0.11, 0.47, 18),
baseMaterial
);
support.position.set(supportX, -0.84, 0);
support.castShadow = true;
group.add(support);
}
// Matched air-core solenoid.
const curve = new HelixCurve(0.85, 0, 1.2, 0.62, 7);
const wireMaterial = standard(COLORS.copper, {
roughness: 0.27,
metalness: 0.68,
emissive: 0x36170b,
emissiveIntensity: 0.25
});
const coil = new THREE.Mesh(
new THREE.TubeGeometry(curve, 300, 0.055, 9, false),
wireMaterial
);
coil.castShadow = true;
group.add(coil);
const core = new THREE.Mesh(
new THREE.CylinderGeometry(0.18, 0.18, 1.25, 32),
standard(0x10263b, {
roughness: 0.55,
metalness: 0.16,
transparent: true,
opacity: 0.78
})
);
core.rotation.z = -Math.PI / 2;
core.position.set(0.85, 0, 0);
group.add(core);
const endRingMaterial = standard(COLORS.copper, {
roughness: 0.24,
metalness: 0.72,
emissive: 0x321309,
emissiveIntensity: 0.32
});
for (const x of [0.25, 1.45]) {
const ring = new THREE.Mesh(
new THREE.TorusGeometry(0.62, 0.036, 10, 72),
endRingMaterial
);
ring.rotation.y = Math.PI / 2;
ring.position.x = x;
group.add(ring);
}
// Near-face color indicates the induced pole.
const poleRingMaterial = standard(
isApproach ? COLORS.red : COLORS.blue,
{
roughness: 0.22,
metalness: 0.48,
emissive: isApproach ? COLORS.red : COLORS.blue,
emissiveIntensity: 1.15
}
);
const poleRing = new THREE.Mesh(
new THREE.TorusGeometry(0.665, 0.055, 12, 72),
poleRingMaterial
);
poleRing.rotation.y = Math.PI / 2;
poleRing.position.x = 0.235;
group.add(poleRing);
const poleGlow = new THREE.Sprite(
new THREE.SpriteMaterial({
map: isApproach ? redGlow : blueGlow,
transparent: true,
opacity: 0.48,
depthWrite: false,
blending: THREE.AdditiveBlending
})
);
poleGlow.position.set(0.22, 0, 0);
poleGlow.scale.set(1.65, 1.65, 1);
group.add(poleGlow);
// Bar magnet proxy: blue south half and red north half.
const magnet = new THREE.Group();
group.add(magnet);
const halfLength = 0.56;
const southHalf = new THREE.Mesh(
new THREE.CylinderGeometry(0.3, 0.3, halfLength, 48),
standard(COLORS.blue, {
roughness: 0.3,
metalness: 0.38,
emissive: 0x0c245c,
emissiveIntensity: 0.28
})
);
southHalf.rotation.z = -Math.PI / 2;
southHalf.position.x = -halfLength / 2;
southHalf.castShadow = true;
magnet.add(southHalf);
const northHalf = new THREE.Mesh(
new THREE.CylinderGeometry(0.3, 0.3, halfLength, 48),
standard(COLORS.red, {
roughness: 0.3,
metalness: 0.38,
emissive: 0x5c101b,
emissiveIntensity: 0.3
})
);
northHalf.rotation.z = -Math.PI / 2;
northHalf.position.x = halfLength / 2;
northHalf.castShadow = true;
magnet.add(northHalf);
const seam = new THREE.Mesh(
new THREE.TorusGeometry(0.302, 0.024, 10, 64),
standard(0xd6e6f1, {
roughness: 0.42,
metalness: 0.55
})
);
seam.rotation.y = Math.PI / 2;
magnet.add(seam);
// Current direction:
// Approach requires counterclockwise current as viewed from the magnet.
// Recede requires clockwise current as viewed from the magnet.
const currentSign = isApproach ? -1 : 1;
const currentHeadGeometry = new THREE.ConeGeometry(
0.085, 0.18, 14
);
const currentHeadMaterial = standard(COLORS.amber, {
roughness: 0.22,
metalness: 0.28,
emissive: COLORS.amber,
emissiveIntensity: 1.2
});
const currentHeads = [];
for (let i = 0; i < 11; i++) {
const u = (i + 0.35) / 11;
const arrowHead = new THREE.Mesh(
currentHeadGeometry,
currentHeadMaterial
);
const point = curve.getPointAt(u);
const tangent = curve.getTangentAt(u)
.multiplyScalar(currentSign)
.normalize();
arrowHead.position.copy(point);
arrowHead.quaternion.setFromUnitVectors(Y_AXIS, tangent);
arrowHead.userData.u = u;
group.add(arrowHead);
currentHeads.push(arrowHead);
}
const currentBeads = [];
for (let i = 0; i < 4; i++) {
const bead = new THREE.Mesh(
new THREE.SphereGeometry(0.074, 14, 10),
basic(COLORS.amber)
);
bead.renderOrder = 6;
group.add(bead);
currentBeads.push(bead);
}
// Cyan vector: original magnetic flux from the magnet's north pole.
const originalFlux = makeSolidArrow(
COLORS.cyan, 0.86, "flux"
);
originalFlux.position.set(0.39, 0.30, 0.79);
setArrowDirection(originalFlux, 1);
group.add(originalFlux);
const gapFlux = makeSolidArrow(
COLORS.cyan, 0.29, "flux"
);
gapFlux.position.set(-0.16, 0.12, 0.68);
setArrowDirection(gapFlux, 1);
group.add(gapFlux);
// Violet vector: induced field inside the coil.
const inducedField = makeSolidArrow(
COLORS.violet, 0.96, "induced"
);
inducedField.position.set(0.37, -0.29, 0.81);
setArrowDirection(inducedField, isApproach ? -1 : 1);
group.add(inducedField);
// Motion and force vectors.
const motion = makeSolidArrow(
COLORS.white, 0.55, "motion"
);
motion.position.set(-0.95, -0.69, 0.82);
setArrowDirection(motion, isApproach ? 1 : -1);
group.add(motion);
const force = makeSolidArrow(
isApproach ? COLORS.red : COLORS.blue,
0.56,
"force"
);
force.position.set(-0.72, 0.65, 0.83);
setArrowDirection(force, isApproach ? -1 : 1);
group.add(force);
const rail = new THREE.Mesh(
new THREE.BoxGeometry(2.15, 0.018, 0.018),
basic(0x7890a5, 0.48)
);
rail.position.set(-0.85, -0.69, 0.78);
group.add(rail);
return {
group,
curve,
magnet,
currentSign,
currentHeads,
currentBeads,
poleRing,
poleRingMaterial,
poleGlow,
originalFlux,
gapFlux,
inducedField,
motion,
force,
scenario
};
}
const panels = {
left: createPanel(-3.0, "approach"),
right: createPanel(3.0, "recede")
};
// HTML labels are projected from Three.js positions.
document.querySelector("#titleL").innerHTML = `
<span class="kicker">01 / Approaching</span>
<strong>Magnet flux through coil is increasing</strong>
<small>North pole moves right →</small>
`;
document.querySelector("#titleR").innerHTML = `
<span class="kicker">02 / Receding</span>
<strong>Magnet flux through coil is decreasing</strong>
<small>North pole moves left ←</small>
`;
document.querySelector("#magnetNL").innerHTML =
"<i>N</i> approaching magnet";
document.querySelector("#magnetNL").classList.add("minor-label");
document.querySelector("#magnetSL").innerHTML =
"<i>S</i> magnet south pole";
document.querySelector("#magnetSL").classList.add("minor-label");
document.querySelector("#coilL").innerHTML =
"<i>N</i> induced at near coil face";
document.querySelector("#coilL").classList.add("minor-label");
document.querySelector("#coilR").innerHTML =
"<i>S</i> induced at near coil face";
document.querySelector("#coilR").classList.add("minor-label");
document.querySelector("#currentL").innerHTML =
"↺ current · CCW from magnet";
document.querySelector("#currentR").innerHTML =
"↻ current · CW from magnet";
document.querySelector("#logicL").innerHTML = `
Near face becomes <b>north</b><br>
repels the approaching north pole
`;
document.querySelector("#logicR").innerHTML = `
Near face becomes <b>south</b><br>
attracts the receding north pole
`;
const timeline = document.querySelector("#timeline");
const progressText = document.querySelector("#progressText");
const playToggle = document.querySelector("#playToggle");
const resetButton = document.querySelector("#resetButton");
const fieldToggle = document.querySelector("#fieldToggle");
const reducedMotion = window.matchMedia(
"(prefers-reduced-motion: reduce)"
).matches;
let phase = reducedMotion ? 0.52 : 0;
let simulationTime = reducedMotion ? 0 : 0;
let playing = !reducedMotion;
function updatePlayButton() {
if (phase >= 0.999) {
playToggle.textContent = "Replay";
} else {
playToggle.textContent = playing ? "Pause" : "Play";
}
}
function setPhase(value) {
phase = THREE.MathUtils.clamp(value, 0, 1);
timeline.value = Math.round(phase * 1000);
progressText.textContent = `${Math.round(phase * 100)}%`;
}
function smoothStep(value) {
return value * value * (3 - 2 * value);
}
playToggle.addEventListener("click", () => {
if (phase >= 0.999) {
setPhase(0);
simulationTime = 0;
playing = true;
} else {
playing = !playing;
}
updatePlayButton();
});
resetButton.addEventListener("click", () => {
setPhase(0);
simulationTime = 0;
playing = false;
updatePlayButton();
});
timeline.addEventListener("input", () => {
setPhase(Number(timeline.value) / 1000);
playing = false;
updatePlayButton();
});
fieldToggle.addEventListener("change", () => {
for (const panel of Object.values(panels)) {
for (const object of [
panel.originalFlux,
panel.gapFlux,
panel.inducedField
]) {
object.visible = fieldToggle.checked;
}
}
});
updatePlayButton();
setPhase(phase);
function updatePanel(panel, progress) {
const eased = smoothStep(progress);
const isApproach = panel.scenario === "approach";
// Left: magnet approaches the coil. Right: equal proxy recedes.
const magnetX = isApproach
? THREE.MathUtils.lerp(-1.48, -0.72, eased)
: THREE.MathUtils.lerp(-0.72, -1.48, eased);
panel.magnet.position.x = magnetX;
panel.gapFlux.position.x = magnetX + 0.58;
panel.motion.position.x = magnetX - 0.19;
panel.force.position.x = magnetX;
const pulse = 1.05 + 0.28 * Math.sin(simulationTime * 3.1);
panel.poleRingMaterial.emissiveIntensity = pulse;
panel.poleGlow.material.opacity = 0.38 + 0.09 * Math.sin(simulationTime * 3.1);
for (let i = 0; i < panel.currentBeads.length; i++) {
const u = (
i / panel.currentBeads.length +
simulationTime * 0.075 * panel.currentSign +
10
) % 1;
panel.currentBeads[i].position.copy(
panel.curve.getPointAt(u)
);
}
}
const projected = new THREE.Vector3();
function placeLabel(id, panel, x, y, z) {
const element = document.querySelector(id);
if (!element) return;
projected.set(x, y, z);
panel.group.localToWorld(projected);
projected.project(camera);
const left = (projected.x * 0.5 + 0.5) * stage.clientWidth;
const top = (-projected.y * 0.5 + 0.5) * stage.clientHeight;
element.style.left = `${left}px`;
element.style.top = `${top}px`;
}
function updateLabels() {
for (const panel of Object.values(panels)) {
const isApproach = panel.scenario === "approach";
const magnetX = panel.magnet.position.x;
const prefix = isApproach ? "L" : "R";
placeLabel(`#title${prefix}`, panel, -0.45, 1.48, 0.9);
placeLabel(`#magnetN${prefix}`, panel, magnetX + 0.51, 0.12, 0.95);
placeLabel(`#magnetS${prefix}`, panel, magnetX - 0.51, 0.12, 0.95);
placeLabel(`#coil${prefix}`, panel, 0.22, 0.80, 0.96);
placeLabel(`#current${prefix}`, panel, 1.16, -0.96, 0.96);
placeLabel(`#logic${prefix}`, panel, -0.20, -1.65, 0.96);
}
}
function resize() {
const width = stage.clientWidth;
const height = stage.clientHeight;
const aspect = width / height;
// Keep both panels visible at wide and narrow aspect ratios.
const viewHeight = Math.max(6.65, 12.35 / aspect);
camera.left = -viewHeight * aspect / 2;
camera.right = viewHeight * aspect / 2;
camera.top = viewHeight / 2;
camera.bottom = -viewHeight / 2;
camera.updateProjectionMatrix();
camera.updateMatrixWorld();
renderer.setSize(width, height, false);
}
window.addEventListener("resize", resize);
resize();
let previousTime = performance.now();
function animate(now) {
requestAnimationFrame(animate);
const delta = Math.min((now - previousTime) / 1000, 0.05);
previousTime = now;
if (playing) {
simulationTime += delta;
phase = Math.min(1, phase + delta / 18);
timeline.value = Math.round(phase * 1000);
progressText.textContent = `${Math.round(phase * 100)}%`;
if (phase >= 1) {
playing = false;
updatePlayButton();
}
}
updatePanel(panels.left, phase);
updatePanel(panels.right, phase);
renderer.render(scene, camera);
updateLabels();
}
requestAnimationFrame(animate);
</script>
</body>
</html>
```