Prompt specification
Build or customize the Three.js 3D scene "Lenz-direction reasoning 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: Lenz-direction reasoning explorer
Category: Science & Physics · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/lenz-direction-reasoning-explorer-x1644
Scene Description Prompt:
Create a single self-contained HTML file using Three.js from cdnjs that shows Lenz's law and induced magnetic opposition. Let users select field direction and whether flux increases or decreases; highlight the induced field and current using one consistent right-hand-rule convention. Low-poly.
Working Three.js Starter (lenz-direction-reasoning-explorer-x1644):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Lenz's Law — Induced Magnetic Opposition</title>
<style>
:root {
--bg: #07111f;
--panel: rgba(10, 20, 35, 0.88);
--panel-border: rgba(160, 194, 222, 0.18);
--text: #eaf4ff;
--muted: #96abc1;
--cyan: #38d6ff;
--pink: #ff58aa;
--amber: #ffc857;
--green: #62e6a7;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: var(--bg);
color: var(--text);
font-family:
Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
#scene {
position: fixed;
inset: 0;
width: 100%;
height: 100%;
display: block;
touch-action: none;
cursor: grab;
}
#scene:active {
cursor: grabbing;
}
.panel {
position: fixed;
z-index: 10;
border: 1px solid var(--panel-border);
background: var(--panel);
box-shadow: 0 18px 50px rgba(0, 0, 0, 0.3);
backdrop-filter: blur(14px);
-webkit-backdrop-filter: blur(14px);
}
#controls {
top: 18px;
left: 18px;
width: 338px;
max-height: calc(100vh - 36px);
overflow: auto;
padding: 20px;
border-radius: 18px;
scrollbar-width: thin;
scrollbar-color: #284159 transparent;
}
h1 {
margin: 0;
font-size: 25px;
line-height: 1.08;
letter-spacing: -0.035em;
}
.subtitle {
margin: 8px 0 19px;
color: var(--muted);
font-size: 13px;
line-height: 1.45;
}
.section-title {
margin: 17px 0 8px;
color: #c9d8e8;
font-size: 11px;
font-weight: 750;
letter-spacing: 0.12em;
text-transform: uppercase;
}
.segmented {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 8px;
}
button {
appearance: none;
border: 1px solid rgba(160, 194, 222, 0.2);
border-radius: 11px;
padding: 11px 10px;
background: rgba(22, 38, 57, 0.72);
color: #c7d6e6;
font: inherit;
font-size: 12px;
font-weight: 700;
cursor: pointer;
transition:
border-color 150ms ease,
background 150ms ease,
color 150ms ease,
transform 150ms ease;
}
button:hover {
border-color: rgba(160, 194, 222, 0.48);
transform: translateY(-1px);
}
button:focus-visible {
outline: 2px solid var(--cyan);
outline-offset: 2px;
}
button.selected {
color: #07111f;
background: var(--cyan);
border-color: var(--cyan);
box-shadow: 0 5px 18px rgba(56, 214, 255, 0.2);
}
#trendButtons button.selected {
background: var(--amber);
border-color: var(--amber);
box-shadow: 0 5px 18px rgba(255, 200, 87, 0.18);
}
.equation {
margin: 17px 0 13px;
padding: 11px 13px;
border: 1px solid rgba(255, 88, 170, 0.18);
border-radius: 11px;
background: rgba(255, 88, 170, 0.065);
color: #ffd0e5;
font-family: Georgia, "Times New Roman", serif;
font-size: 19px;
text-align: center;
}
.readout {
display: grid;
gap: 8px;
font-size: 12px;
}
.readout-row {
display: flex;
justify-content: space-between;
gap: 12px;
color: var(--muted);
}
.readout-row strong {
color: var(--text);
text-align: right;
}
.explanation {
margin: 13px 0 0;
padding-top: 12px;
border-top: 1px solid rgba(160, 194, 222, 0.13);
color: #b8c9da;
font-size: 12px;
line-height: 1.5;
}
.explanation strong {
color: var(--green);
}
.flux-meter {
height: 5px;
margin-top: 10px;
overflow: hidden;
border-radius: 99px;
background: rgba(56, 214, 255, 0.1);
}
.flux-meter i {
display: block;
width: 42%;
height: 100%;
border-radius: inherit;
background: linear-gradient(90deg, #248db3, var(--cyan));
transition: width 300ms ease;
}
.flux-meter.increasing i {
width: 88%;
animation: meter-pulse 1.5s ease-in-out infinite alternate;
}
.flux-meter.decreasing i {
width: 24%;
animation: meter-pulse-reverse 1.5s ease-in-out infinite alternate;
}
@keyframes meter-pulse {
from { width: 72%; }
to { width: 94%; }
}
@keyframes meter-pulse-reverse {
from { width: 34%; }
to { width: 15%; }
}
#rhr {
right: 18px;
bottom: 18px;
width: 214px;
padding: 14px 15px 13px;
border-radius: 17px;
text-align: center;
}
#rhr h2 {
margin: 0;
font-size: 12px;
letter-spacing: 0.08em;
text-transform: uppercase;
}
#rhr svg {
display: block;
width: 144px;
height: 144px;
margin: 4px auto 0;
overflow: visible;
}
.rhr-caption {
margin-top: -2px;
color: var(--muted);
font-size: 11px;
line-height: 1.4;
}
#legend {
top: 18px;
right: 18px;
padding: 12px 14px;
border-radius: 14px;
}
.legend-row {
display: flex;
align-items: center;
gap: 9px;
margin: 4px 0;
color: #c8d7e7;
font-size: 11px;
font-weight: 650;
}
.swatch {
width: 22px;
height: 4px;
border-radius: 99px;
box-shadow: 0 0 10px currentColor;
}
.swatch.cyan {
color: var(--cyan);
background: var(--cyan);
}
.swatch.pink {
color: var(--pink);
background: var(--pink);
}
.swatch.amber {
color: var(--amber);
background: var(--amber);
}
#help {
position: fixed;
z-index: 8;
left: 50%;
bottom: 17px;
padding: 8px 12px;
transform: translateX(-50%);
border: 1px solid rgba(160, 194, 222, 0.14);
border-radius: 99px;
background: rgba(7, 17, 31, 0.72);
color: var(--muted);
font-size: 10px;
pointer-events: none;
white-space: nowrap;
}
#reset {
width: 100%;
margin-top: 14px;
padding: 9px;
color: #b8c9da;
font-size: 11px;
}
@media (max-width: 760px) {
#controls {
top: 10px;
left: 10px;
width: min(292px, calc(100vw - 20px));
max-height: calc(100vh - 20px);
padding: 16px;
}
h1 {
font-size: 22px;
}
#legend {
display: none;
}
#rhr {
right: 10px;
bottom: 10px;
width: 166px;
padding: 10px;
}
#rhr svg {
width: 116px;
height: 116px;
}
#help {
display: none;
}
}
</style>
</head>
<body>
<canvas id="scene" aria-label="Interactive Lenz's law demonstration"></canvas>
<section id="controls" class="panel" aria-label="Lenz's law controls">
<h1>Lenz’s Law</h1>
<p class="subtitle">
Change the applied field and flux trend. The highlighted induced field and
current always obey the right-hand rule.
</p>
<div class="section-title">Applied field direction</div>
<div class="segmented" role="group" aria-label="Applied field direction">
<button id="fieldPlus" class="selected" aria-pressed="true">→ +X</button>
<button id="fieldMinus" aria-pressed="false">← −X</button>
</div>
<div class="section-title">External flux magnitude</div>
<div id="trendButtons" class="segmented" role="group" aria-label="Flux trend">
<button id="fluxIncreasing" class="selected" aria-pressed="true">
Increasing |Φ|
</button>
<button id="fluxDecreasing" aria-pressed="false">
Decreasing |Φ|
</button>
</div>
<div class="flux-meter increasing" id="fluxMeter" aria-hidden="true">
<i></i>
</div>
<div class="equation">B<sub>ind</sub> ∝ − dΦ<sub>ext</sub> / dt</div>
<div class="readout">
<div class="readout-row">
<span>Applied field</span>
<strong id="appliedReadout">+X</strong>
</div>
<div class="readout-row">
<span>Induced field</span>
<strong id="inducedReadout">−X</strong>
</div>
<div class="readout-row">
<span>Current from +X end</span>
<strong id="currentReadout">Clockwise</strong>
</div>
</div>
<p class="explanation" id="explanation">
The external flux is increasing, so the induced field points
<strong>opposite</strong> the applied field.
</p>
<button id="reset">Reset camera</button>
</section>
<aside id="legend" class="panel" aria-label="Color legend">
<div class="legend-row">
<span class="swatch cyan"></span>
Applied magnetic field
</div>
<div class="legend-row">
<span class="swatch pink"></span>
Induced magnetic field
</div>
<div class="legend-row">
<span class="swatch amber"></span>
Induced current
</div>
</aside>
<aside id="rhr" class="panel" aria-label="Right-hand rule reference">
<h2>Right-hand rule</h2>
<svg viewBox="0 0 140 140" role="img" aria-label="End-on current and field diagram">
<defs>
<marker
id="arrowHead"
markerWidth="7"
markerHeight="7"
refX="5.5"
refY="3.5"
orient="auto"
>
<path d="M0,0 L7,3.5 L0,7 Z" fill="#ffc857"></path>
</marker>
</defs>
<circle
cx="70"
cy="70"
r="50"
fill="rgba(255,200,87,0.025)"
stroke="rgba(160,194,222,0.22)"
stroke-width="1.5"
></circle>
<path
id="currentArc"
d="M 70 20 A 50 50 0 1 0 120 95"
fill="none"
stroke="#ffc857"
stroke-width="4"
stroke-linecap="round"
marker-end="url(#arrowHead)"
></path>
<g id="fieldSymbol"></g>
<text
x="70"
y="132"
fill="#8da2b8"
font-size="9"
text-anchor="middle"
>
VIEWED FROM +X END
</text>
</svg>
<div class="rhr-caption" id="rhrCaption">
Clockwise current gives an induced field away from you (−X).
</div>
</aside>
<div id="help">Drag to orbit · Scroll to zoom</div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
const canvas = document.querySelector("#scene");
const renderer = new THREE.WebGLRenderer({
canvas,
antialias: true,
alpha: false,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight, false);
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.15;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.setClearColor(0x07111f, 1);
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x07111f);
scene.fog = new THREE.Fog(0x07111f, 13, 27);
const camera = new THREE.PerspectiveCamera(
42,
window.innerWidth / window.innerHeight,
0.1,
100
);
// Camera controls are implemented directly, without external addons.
const orbit = {
theta: 1.16,
phi: 1.22,
radius: 11.4,
target: new THREE.Vector3(0, 0.25, 0),
dragging: false,
pointerId: null,
lastX: 0,
lastY: 0
};
function updateCamera() {
const sinPhi = Math.sin(orbit.phi);
camera.position.set(
orbit.target.x + orbit.radius * sinPhi * Math.cos(orbit.theta),
orbit.target.y + orbit.radius * Math.cos(orbit.phi),
orbit.target.z + orbit.radius * sinPhi * Math.sin(orbit.theta)
);
camera.lookAt(orbit.target);
}
updateCamera();
canvas.addEventListener("pointerdown", (event) => {
if (event.pointerType === "mouse" && event.button !== 0) return;
orbit.dragging = true;
orbit.pointerId = event.pointerId;
orbit.lastX = event.clientX;
orbit.lastY = event.clientY;
canvas.setPointerCapture(event.pointerId);
});
canvas.addEventListener("pointermove", (event) => {
if (!orbit.dragging || event.pointerId !== orbit.pointerId) return;
const dx = event.clientX - orbit.lastX;
const dy = event.clientY - orbit.lastY;
orbit.theta -= dx * 0.006;
orbit.phi = THREE.MathUtils.clamp(
orbit.phi + dy * 0.005,
0.48,
2.35
);
orbit.lastX = event.clientX;
orbit.lastY = event.clientY;
updateCamera();
});
function endDrag(event) {
if (event.pointerId !== orbit.pointerId) return;
orbit.dragging = false;
orbit.pointerId = null;
}
canvas.addEventListener("pointerup", endDrag);
canvas.addEventListener("pointercancel", endDrag);
canvas.addEventListener(
"wheel",
(event) => {
event.preventDefault();
orbit.radius = THREE.MathUtils.clamp(
orbit.radius + event.deltaY * 0.008,
7,
17
);
updateCamera();
},
{ passive: false }
);
document.querySelector("#reset").addEventListener("click", () => {
orbit.theta = 1.16;
orbit.phi = 1.22;
orbit.radius = 11.4;
orbit.target.set(0, 0.25, 0);
updateCamera();
});
// Lighting.
scene.add(new THREE.HemisphereLight(0x8fc9ff, 0x142033, 1.55));
const keyLight = new THREE.DirectionalLight(0xffffff, 2.2);
keyLight.position.set(5, 8, 6);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(1024, 1024);
keyLight.shadow.camera.left = -8;
keyLight.shadow.camera.right = 8;
keyLight.shadow.camera.top = 8;
keyLight.shadow.camera.bottom = -8;
scene.add(keyLight);
const rimLight = new THREE.DirectionalLight(0x38d6ff, 1.4);
rimLight.position.set(-6, 3, -5);
scene.add(rimLight);
const pinkLight = new THREE.PointLight(0xff58aa, 9, 10, 2);
pinkLight.position.set(1.5, 0.7, 2.4);
scene.add(pinkLight);
// Low-poly floor.
const floor = new THREE.Mesh(
new THREE.CircleGeometry(8.5, 14),
new THREE.MeshStandardMaterial({
color: 0x0b1827,
roughness: 0.92,
metalness: 0.08,
flatShading: true
})
);
floor.rotation.x = -Math.PI / 2;
floor.position.y = -1.42;
floor.receiveShadow = true;
scene.add(floor);
const grid = new THREE.GridHelper(14, 14, 0x31506a, 0x20384d);
grid.position.y = -1.405;
grid.material.transparent = true;
grid.material.opacity = 0.24;
scene.add(grid);
const AXIS_Y = 0.34;
const COIL_LENGTH = 5.2;
const COIL_RADIUS = 1.03;
const COIL_TURNS = 8;
// Fixed-helix curve: reversing current reverses its tangent.
class HelixCurve extends THREE.Curve {
getPoint(t, target = new THREE.Vector3()) {
const angle = t * COIL_TURNS * Math.PI * 2;
return target.set(
-COIL_LENGTH / 2 + t * COIL_LENGTH,
COIL_RADIUS * Math.cos(angle),
COIL_RADIUS * Math.sin(angle)
);
}
}
const helix = new HelixCurve();
// Coil assembly.
const coilAssembly = new THREE.Group();
coilAssembly.position.y = AXIS_Y;
scene.add(coilAssembly);
const core = new THREE.Mesh(
new THREE.CylinderGeometry(0.39, 0.39, 5.55, 10, 1, false),
new THREE.MeshStandardMaterial({
color: 0x71869b,
roughness: 0.52,
metalness: 0.32,
transparent: true,
opacity: 0.62,
flatShading: true
})
);
core.rotation.z = Math.PI / 2;
core.castShadow = true;
scene.add(core);
const coilMaterial = new THREE.MeshStandardMaterial({
color: 0xd98246,
emissive: 0xff9e35,
emissiveIntensity: 0.18,
roughness: 0.35,
metalness: 0.52,
flatShading: true
});
const coil = new THREE.Mesh(
new THREE.TubeGeometry(helix, 112, 0.09, 5, false),
coilMaterial
);
coil.castShadow = true;
coil.receiveShadow = true;
coilAssembly.add(coil);
const ringMaterial = new THREE.MeshStandardMaterial({
color: 0xf0a05c,
emissive: 0xff8d2b,
emissiveIntensity: 0.12,
roughness: 0.35,
metalness: 0.5,
flatShading: true
});
for (const x of [-COIL_LENGTH / 2, COIL_LENGTH / 2]) {
const ring = new THREE.Mesh(
new THREE.TorusGeometry(COIL_RADIUS, 0.105, 4, 20),
ringMaterial
);
ring.rotation.y = Math.PI / 2;
ring.position.x = x;
ring.castShadow = true;
coilAssembly.add(ring);
}
// Faceted supports.
const supportMaterial = new THREE.MeshStandardMaterial({
color: 0x263a4f,
roughness: 0.7,
metalness: 0.35,
flatShading: true
});
for (const x of [-2.85, 2.85]) {
const post = new THREE.Mesh(
new THREE.CylinderGeometry(0.13, 0.18, 1.16, 6),
supportMaterial
);
post.position.set(x, -0.78, 0);
post.castShadow = true;
scene.add(post);
const foot = new THREE.Mesh(
new THREE.BoxGeometry(0.82, 0.14, 0.72),
supportMaterial
);
foot.position.set(x, -1.31, 0);
foot.castShadow = true;
scene.add(foot);
}
// Arrow factory. Arrows are built along local +Y and then rotated.
const Y_AXIS = new THREE.Vector3(0, 1, 0);
function createArrow(length, color, opacity = 1) {
const group = new THREE.Group();
const headLength = 0.31;
const shaftLength = length - headLength;
const shaftMaterial = new THREE.MeshStandardMaterial({
color,
emissive: color,
emissiveIntensity: 0.42,
roughness: 0.3,
metalness: 0.12,
transparent: true,
opacity,
flatShading: true
});
const headMaterial = shaftMaterial.clone();
const shaft = new THREE.Mesh(
new THREE.CylinderGeometry(
0.052,
0.052,
shaftLength,
6,
1,
false
),
shaftMaterial
);
shaft.position.y = shaftLength / 2;
shaft.castShadow = true;
group.add(shaft);
const head = new THREE.Mesh(
new THREE.ConeGeometry(0.17, headLength, 6),
headMaterial
);
head.position.y = length - headLength / 2;
head.castShadow = true;
group.add(head);
group.userData.materials = [shaftMaterial, headMaterial];
return group;
}
function orientArrowX(arrow, sign) {
arrow.quaternion.setFromUnitVectors(
Y_AXIS,
new THREE.Vector3(sign, 0, 0)
);
}
// Applied field arrows.
const appliedArrows = [];
const appliedColor = 0x38d6ff;
for (let i = 0; i < 4; i++) {
const arrow = createArrow(1.35, appliedColor, 0.85);
arrow.position.y = 2.05;
arrow.position.z = -0.35;
scene.add(arrow);
appliedArrows.push(arrow);
}
// Induced field arrow inside the coil.
const inducedArrow = createArrow(4.38, 0xff58aa, 0.95);
inducedArrow.position.y = AXIS_Y;
inducedArrow.position.z = 0.66;
scene.add(inducedArrow);
// Induced-current arrows placed directly on the winding.
const currentArrowMaterial = new THREE.MeshStandardMaterial({
color: 0xffc857,
emissive: 0xffa928,
emissiveIntensity: 0.8,
roughness: 0.25,
metalness: 0.2,
flatShading: true
});
const currentArrowGeometry = new THREE.ConeGeometry(0.15, 0.35, 5);
const currentArrows = [];
const currentArrowParameters = [0.13, 0.36, 0.63, 0.86];
for (const t of currentArrowParameters) {
const arrow = new THREE.Mesh(currentArrowGeometry, currentArrowMaterial);
arrow.position.copy(helix.getPointAt(t));
arrow.castShadow = true;
arrow.userData.t = t;
coilAssembly.add(arrow);
currentArrows.push(arrow);
}
// Moving markers make the current direction easy to follow.
const movingMarkers = [];
const markerGeometry = new THREE.OctahedronGeometry(0.13, 0);
for (let i = 0; i < 4; i++) {
const marker = new THREE.Mesh(markerGeometry, currentArrowMaterial);
marker.userData.offset = i * 0.25;
coilAssembly.add(marker);
movingMarkers.push(marker);
}
// Billboard labels made from canvas textures.
function createLabel(text, color) {
const labelCanvas = document.createElement("canvas");
labelCanvas.width = 512;
labelCanvas.height = 96;
const context = labelCanvas.getContext("2d");
context.fillStyle = "rgba(7,17,31,0.82)";
context.fillRect(2, 8, 508, 80);
context.strokeStyle = color;
context.lineWidth = 3;
context.strokeRect(2, 8, 508, 80);
context.fillStyle = color;
context.font = "700 34px system-ui, sans-serif";
context.textAlign = "center";
context.textBaseline = "middle";
context.fillText(text, 256, 50);
const texture = new THREE.CanvasTexture(labelCanvas);
texture.colorSpace = THREE.SRGBColorSpace;
const sprite = new THREE.Sprite(
new THREE.SpriteMaterial({
map: texture,
transparent: true,
depthTest: false,
depthWrite: false
})
);
sprite.scale.set(2.35, 0.44, 1);
sprite.renderOrder = 20;
return sprite;
}
const appliedLabel = createLabel("APPLIED FIELD B_ext", "#38d6ff");
appliedLabel.position.set(0, 2.72, -0.35);
scene.add(appliedLabel);
const inducedLabel = createLabel("INDUCED FIELD B_ind", "#ff58aa");
inducedLabel.position.set(0.1, -0.86, 0.75);
scene.add(inducedLabel);
const currentLabel = createLabel("INDUCED CURRENT I", "#ffc857");
currentLabel.position.set(-2.25, 1.55, 0.9);
scene.add(currentLabel);
const xEndLabel = createLabel("+X END", "#d9e9f7");
xEndLabel.scale.set(1.15, 0.25, 1);
xEndLabel.position.set(3.28, -1.08, 0.05);
scene.add(xEndLabel);
const xEndMarker = new THREE.Mesh(
new THREE.OctahedronGeometry(0.13, 0),
new THREE.MeshStandardMaterial({
color: 0xd9e9f7,
emissive: 0x6aa9d1,
emissiveIntensity: 0.35,
flatShading: true
})
);
xEndMarker.position.set(3.3, -1.31, 0);
scene.add(xEndMarker);
// State.
let fieldSign = 1; // +1 means applied field along +X.
let fluxIncreasing = true;
let inducedSign = -1; // Derived from Lenz's law.
const fieldPlus = document.querySelector("#fieldPlus");
const fieldMinus = document.querySelector("#fieldMinus");
const fluxIncreasingButton = document.querySelector("#fluxIncreasing");
const fluxDecreasingButton = document.querySelector("#fluxDecreasing");
const appliedReadout = document.querySelector("#appliedReadout");
const inducedReadout = document.querySelector("#inducedReadout");
const currentReadout = document.querySelector("#currentReadout");
const explanation = document.querySelector("#explanation");
const fluxMeter = document.querySelector("#fluxMeter");
const currentArc = document.querySelector("#currentArc");
const fieldSymbol = document.querySelector("#fieldSymbol");
const rhrCaption = document.querySelector("#rhrCaption");
const CCW_PATH = "M 70 20 A 50 50 0 1 0 120 95";
const CW_PATH = "M 70 20 A 50 50 0 1 1 20 95";
function setSelected(button, selected) {
button.classList.toggle("selected", selected);
button.setAttribute("aria-pressed", String(selected));
}
function updateCurrentArrows() {
for (const arrow of currentArrows) {
const tangent = helix
.getTangentAt(arrow.userData.t)
.multiplyScalar(inducedSign)
.normalize();
arrow.quaternion.setFromUnitVectors(Y_AXIS, tangent);
}
}
function updateState() {
// Increasing flux: oppose the applied field.
// Decreasing flux: support the applied field to oppose the loss.
inducedSign = fluxIncreasing ? -fieldSign : fieldSign;
setSelected(fieldPlus, fieldSign > 0);
setSelected(fieldMinus, fieldSign < 0);
setSelected(fluxIncreasingButton, fluxIncreasing);
setSelected(fluxDecreasingButton, !fluxIncreasing);
appliedReadout.textContent = fieldSign > 0 ? "+X" : "−X";
inducedReadout.textContent = inducedSign > 0 ? "+X" : "−X";
currentReadout.textContent =
inducedSign > 0 ? "Counterclockwise" : "Clockwise";
if (fluxIncreasing) {
explanation.innerHTML =
"The external flux magnitude is increasing, so the induced field points " +
"<strong>opposite</strong> the applied field.";
} else {
explanation.innerHTML =
"The external flux magnitude is decreasing, so the induced field points " +
"<strong>along</strong> the applied field to oppose the loss of flux.";
}
fluxMeter.classList.toggle("increasing", fluxIncreasing);
fluxMeter.classList.toggle("decreasing", !fluxIncreasing);
// Applied arrows.
const basesRight = [-3.8, -2.2, -0.6, 1.0];
const basesLeft = [3.8, 2.2, 0.6, -1.0];
const bases = fieldSign > 0 ? basesRight : basesLeft;
appliedArrows.forEach((arrow, index) => {
arrow.position.x = bases[index];
orientArrowX(arrow, fieldSign);
});
// Induced field arrow.
orientArrowX(inducedArrow, inducedSign);
inducedArrow.position.x = inducedSign > 0 ? -2.19 : 2.19;
updateCurrentArrows();
// Right-hand-rule inset: from +X, counterclockwise current gives +X.
if (inducedSign > 0) {
currentArc.setAttribute("d", CCW_PATH);
fieldSymbol.innerHTML =
'<circle cx="70" cy="70" r="7" fill="#ff58aa"></circle>' +
'<text x="70" y="55" fill="#ff8cc2" font-size="9" text-anchor="middle">B OUT</text>';
rhrCaption.textContent =
"Counterclockwise current gives an induced field toward you (+X).";
} else {
currentArc.setAttribute("d", CW_PATH);
fieldSymbol.innerHTML =
'<path d="M62 62 L78 78 M78 62 L62 78" ' +
'stroke="#ff58aa" stroke-width="4" stroke-linecap="round"></path>' +
'<text x="70" y="55" fill="#ff8cc2" font-size="9" text-anchor="middle">B IN</text>';
rhrCaption.textContent =
"Clockwise current gives an induced field away from you (−X).";
}
}
fieldPlus.addEventListener("click", () => {
fieldSign = 1;
updateState();
});
fieldMinus.addEventListener("click", () => {
fieldSign = -1;
updateState();
});
fluxIncreasingButton.addEventListener("click", () => {
fluxIncreasing = true;
updateState();
});
fluxDecreasingButton.addEventListener("click", () => {
fluxIncreasing = false;
updateState();
});
updateState();
// Resize.
function resize() {
const width = window.innerWidth;
const height = window.innerHeight;
camera.aspect = width / height;
camera.updateProjectionMatrix();
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(width, height, false);
}
window.addEventListener("resize", resize);
// Animation.
const clock = new THREE.Clock();
function animate() {
const elapsed = clock.getElapsedTime();
// Applied field is brighter and pulses while its magnitude increases.
const appliedOpacity = fluxIncreasing
? 0.82 + 0.13 * Math.sin(elapsed * 2.3)
: 0.40 + 0.07 * Math.sin(elapsed * 1.5);
for (const arrow of appliedArrows) {
for (const material of arrow.userData.materials) {
material.opacity = appliedOpacity;
}
}
// Induced field remains strongly highlighted.
const inducedOpacity = 0.88 + 0.10 * Math.sin(elapsed * 3.0);
for (const material of inducedArrow.userData.materials) {
material.opacity = inducedOpacity;
}
// Move bright markers in the actual current direction.
for (const marker of movingMarkers) {
let u =
marker.userData.offset + elapsed * 0.105 * inducedSign;
u = ((u % 1) + 1) % 1;
marker.position.copy(helix.getPointAt(u));
marker.rotation.x = elapsed * 1.2;
marker.rotation.y = elapsed * 1.6;
}
// Subtle scene motion.
pinkLight.intensity = 8 + Math.sin(elapsed * 1.4) * 1.2;
xEndMarker.rotation.y = elapsed * 0.7;
renderer.render(scene, camera);
requestAnimationFrame(animate);
}
animate();
</script>
</body>
</html>
```