Prompt specification
Build or customize the Three.js 3D scene "Loop orientation torque 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: Loop orientation torque comparison
Category: Science & Physics · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/loop-orientation-torque-comparison-x1654
Scene Description Prompt:
Compare stylized current-loop torques at different angles in one uniform magnetic field, including zero torque. Three.js from cdnjs, one HTML file.
Working Three.js Starter (loop-orientation-torque-comparison-x1654):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Current Loop Torque in a Uniform Field</title>
<style>
:root {
color-scheme: dark;
--bg: #050813;
--panel: rgba(11, 17, 31, 0.82);
--panel-border: rgba(160, 190, 255, 0.15);
--text: #edf4ff;
--muted: #93a4be;
--blue: #54a8ff;
--purple: #c084fc;
--orange: #ff9f43;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
font-family:
Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
color: var(--text);
background:
radial-gradient(circle at 48% 42%, #14213c 0%, #080d1c 42%, #04060d 100%);
}
#scene {
position: fixed;
inset: 0;
width: 100%;
height: 100%;
display: block;
touch-action: none;
cursor: grab;
}
#scene:active {
cursor: grabbing;
}
#labels {
position: fixed;
inset: 0;
pointer-events: none;
overflow: hidden;
z-index: 2;
}
.world-label {
position: absolute;
transform: translate(-50%, -50%);
white-space: nowrap;
pointer-events: none;
transition: opacity 120ms ease;
}
.angle-label {
display: flex;
align-items: center;
gap: 7px;
padding: 6px 10px;
border: 1px solid rgba(255, 255, 255, 0.13);
border-radius: 999px;
color: #f7fbff;
background: rgba(5, 9, 19, 0.76);
box-shadow: 0 8px 22px rgba(0, 0, 0, 0.22);
font-size: 12px;
backdrop-filter: blur(10px);
}
.angle-label .dot {
width: 8px;
height: 8px;
border-radius: 50%;
background: var(--loop-color);
box-shadow: 0 0 12px var(--loop-color);
}
.torque-label {
padding: 5px 9px;
border-radius: 7px;
border: 1px solid rgba(255, 159, 67, 0.25);
background: rgba(37, 21, 9, 0.72);
color: #ffc27f;
font-size: 11px;
font-weight: 700;
letter-spacing: 0.02em;
backdrop-filter: blur(8px);
}
.torque-label.zero {
color: #ffd9b4;
border-color: rgba(255, 159, 67, 0.5);
}
.field-label {
padding: 7px 11px;
border-radius: 8px;
border: 1px solid rgba(84, 168, 255, 0.3);
background: rgba(7, 26, 51, 0.8);
color: #91caff;
font-size: 12px;
font-weight: 800;
letter-spacing: 0.08em;
}
.glass {
position: fixed;
z-index: 5;
border: 1px solid var(--panel-border);
border-radius: 16px;
background: var(--panel);
box-shadow:
0 22px 60px rgba(0, 0, 0, 0.32),
inset 0 1px 0 rgba(255, 255, 255, 0.035);
backdrop-filter: blur(18px) saturate(125%);
}
.brand {
position: fixed;
z-index: 4;
top: 26px;
left: 28px;
pointer-events: none;
max-width: 390px;
text-shadow: 0 2px 20px rgba(0, 0, 0, 0.5);
}
.eyebrow {
display: inline-flex;
align-items: center;
gap: 8px;
color: #7fc4ff;
font-size: 11px;
font-weight: 800;
letter-spacing: 0.15em;
text-transform: uppercase;
}
.eyebrow::before {
content: "";
width: 22px;
height: 1px;
background: #4ca5ff;
box-shadow: 0 0 8px #4ca5ff;
}
h1 {
margin: 9px 0 7px;
font-size: clamp(25px, 3vw, 42px);
line-height: 1.05;
letter-spacing: -0.04em;
}
.brand p {
margin: 0;
max-width: 360px;
color: var(--muted);
font-size: 13px;
line-height: 1.55;
}
.controls {
left: 24px;
bottom: 24px;
width: 292px;
padding: 17px;
}
.panel-title {
display: flex;
align-items: center;
justify-content: space-between;
gap: 12px;
margin-bottom: 14px;
color: #dce9ff;
font-size: 12px;
font-weight: 800;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.uniform-badge {
padding: 4px 7px;
border-radius: 999px;
color: #8ccaff;
background: rgba(63, 154, 255, 0.1);
border: 1px solid rgba(84, 168, 255, 0.2);
font-size: 9px;
letter-spacing: 0.06em;
}
.control {
margin: 13px 0;
}
.control-label {
display: flex;
justify-content: space-between;
align-items: baseline;
gap: 12px;
margin-bottom: 8px;
color: #c6d2e6;
font-size: 12px;
}
.control-label output {
color: #f4f8ff;
font-variant-numeric: tabular-nums;
font-weight: 750;
}
input[type="range"] {
width: 100%;
height: 5px;
margin: 0;
border-radius: 999px;
appearance: none;
background: rgba(255, 255, 255, 0.1);
cursor: pointer;
}
input[type="range"]::-webkit-slider-thumb {
width: 15px;
height: 15px;
appearance: none;
border: 2px solid #d8ecff;
border-radius: 50%;
background: #318dde;
box-shadow: 0 0 0 4px rgba(49, 141, 222, 0.15);
}
input[type="range"]::-moz-range-thumb {
width: 13px;
height: 13px;
border: 2px solid #d8ecff;
border-radius: 50%;
background: #318dde;
box-shadow: 0 0 0 4px rgba(49, 141, 222, 0.15);
}
.button-row {
display: flex;
gap: 8px;
margin-top: 14px;
}
button {
appearance: none;
border: 1px solid rgba(255, 255, 255, 0.12);
border-radius: 9px;
padding: 9px 10px;
color: #dfeaff;
background: rgba(255, 255, 255, 0.055);
font: inherit;
font-size: 11px;
font-weight: 700;
cursor: pointer;
transition:
background 140ms ease,
border-color 140ms ease,
transform 140ms ease;
}
button:hover {
background: rgba(255, 255, 255, 0.095);
border-color: rgba(255, 255, 255, 0.23);
}
button:active {
transform: translateY(1px);
}
button.primary {
color: #07111e;
border-color: #70bcff;
background: linear-gradient(135deg, #8ed0ff, #4a9ff5);
}
.wide {
flex: 1;
}
.toggle-row {
display: flex;
align-items: center;
gap: 8px;
margin-top: 12px;
color: #9fb0c9;
font-size: 11px;
cursor: pointer;
}
.toggle-row input {
accent-color: #4a9ff5;
}
.formula {
margin-top: 15px;
padding: 11px 12px;
border: 1px solid rgba(192, 132, 252, 0.18);
border-radius: 10px;
background: rgba(192, 132, 252, 0.055);
color: #cbd7eb;
font-size: 11px;
line-height: 1.55;
}
.formula strong {
color: #e3c9ff;
font-size: 13px;
}
.compare {
top: 24px;
right: 24px;
width: 294px;
padding: 17px;
}
.cards {
display: grid;
gap: 9px;
}
.loop-card {
--loop-color: #54a8ff;
padding: 11px 12px 10px;
border: 1px solid rgba(255, 255, 255, 0.09);
border-radius: 11px;
background: rgba(255, 255, 255, 0.035);
}
.card-top,
.torque-line,
.card-foot {
display: flex;
align-items: center;
justify-content: space-between;
gap: 8px;
}
.card-top {
margin-bottom: 8px;
color: #dbe7f8;
font-size: 11px;
font-weight: 800;
}
.swatch {
width: 9px;
height: 9px;
margin-right: 7px;
display: inline-block;
border-radius: 50%;
background: var(--loop-color);
box-shadow: 0 0 10px var(--loop-color);
}
.loop-name {
margin-right: auto;
}
.angle-output {
color: var(--loop-color);
font-variant-numeric: tabular-nums;
}
.loop-card input[type="range"] {
accent-color: var(--loop-color);
}
.torque-line {
margin-top: 9px;
color: #8fa1ba;
font-size: 10px;
}
.torque-line strong {
color: #ffbd78;
font-size: 12px;
font-variant-numeric: tabular-nums;
}
.track {
height: 4px;
margin-top: 7px;
overflow: hidden;
border-radius: 999px;
background: rgba(255, 255, 255, 0.07);
}
.track i {
display: block;
width: 0;
height: 100%;
border-radius: inherit;
background: linear-gradient(90deg, #ff7c36, #ffc26e);
box-shadow: 0 0 9px rgba(255, 139, 55, 0.45);
transition: width 100ms ease;
}
.card-foot {
margin-top: 6px;
color: #64758d;
font-size: 9px;
}
.zero-note {
margin: 12px 2px 0;
color: #7f91aa;
font-size: 10px;
line-height: 1.45;
}
.legend {
position: fixed;
z-index: 4;
left: 50%;
bottom: 25px;
display: flex;
gap: 16px;
align-items: center;
padding: 9px 14px;
transform: translateX(-50%);
border: 1px solid rgba(255, 255, 255, 0.09);
border-radius: 999px;
color: #93a5bd;
background: rgba(6, 10, 20, 0.64);
backdrop-filter: blur(12px);
pointer-events: none;
font-size: 10px;
}
.legend span {
display: flex;
align-items: center;
gap: 6px;
white-space: nowrap;
}
.legend i {
width: 14px;
height: 2px;
display: inline-block;
background: currentColor;
box-shadow: 0 0 8px currentColor;
}
.legend .field {
color: #54a8ff;
}
.legend .moment {
color: #c084fc;
}
.legend .torque {
color: #ff9f43;
}
.legend .current {
color: #eef7ff;
}
.hint {
position: fixed;
z-index: 3;
right: 26px;
bottom: 26px;
color: rgba(167, 184, 211, 0.65);
font-size: 10px;
pointer-events: none;
}
@media (max-width: 900px) {
.brand {
top: 16px;
left: 16px;
}
.brand p {
display: none;
}
.controls {
left: 12px;
bottom: 12px;
width: 248px;
padding: 13px;
}
.compare {
top: 12px;
right: 12px;
width: 250px;
padding: 13px;
}
.legend,
.hint {
display: none;
}
}
@media (max-width: 620px) {
.brand {
top: 10px;
left: 12px;
}
.brand h1 {
max-width: 260px;
font-size: 24px;
}
.eyebrow {
font-size: 9px;
}
.compare {
top: auto;
right: 10px;
bottom: 10px;
left: 10px;
width: auto;
padding: 11px;
}
.compare .panel-title {
margin-bottom: 8px;
}
.cards {
grid-template-columns: 1fr 1fr;
gap: 7px;
}
.loop-card {
padding: 8px;
}
.card-foot,
.zero-note {
display: none;
}
.controls {
top: 88px;
bottom: auto;
width: 218px;
padding: 11px;
}
.formula,
.toggle-row {
display: none;
}
.control {
margin: 9px 0;
}
.button-row {
flex-wrap: wrap;
}
.button-row button {
padding: 7px 8px;
}
}
</style>
</head>
<body>
<canvas id="scene"></canvas>
<div id="labels"></div>
<header class="brand">
<div class="eyebrow">Electromagnetism lab</div>
<h1>Current-loop torque</h1>
<p>
Compare four magnetic moments in the same uniform field. The angle
θ is measured between the magnetic moment μ and the field B.
</p>
</header>
<section class="glass controls" aria-label="Simulation controls">
<div class="panel-title">
Shared field
<span class="uniform-badge">ONE UNIFORM B</span>
</div>
<div class="control">
<label class="control-label" for="fieldStrength">
<span>Field strength B</span>
<output id="fieldOutput">500 mT</output>
</label>
<input
id="fieldStrength"
type="range"
min="100"
max="1000"
step="10"
value="500"
/>
</div>
<div class="control">
<label class="control-label" for="currentStrength">
<span>Loop current I</span>
<output id="currentOutput">2.0 A</output>
</label>
<input
id="currentStrength"
type="range"
min="0.5"
max="5"
step="0.1"
value="2"
/>
</div>
<div class="button-row">
<button id="pauseButton" class="primary wide">Pause current</button>
<button id="resetButton">Reset view</button>
</div>
<div class="button-row">
<button id="presetButton" class="wide">
Apply 0° / 30° / 60° / 90°
</button>
</div>
<label class="toggle-row">
<input id="fieldToggle" type="checkbox" checked />
Show uniform field arrows
</label>
<div class="formula">
<strong>τ = μ × B</strong><br />
|τ| = IAB sin θ<br />
Model loop radius: 10 cm
</div>
</section>
<section class="glass compare" aria-label="Torque comparison">
<div class="panel-title">
Angle comparison
<span class="uniform-badge">LIVE VALUES</span>
</div>
<div id="cards" class="cards"></div>
<p class="zero-note">
Torque is zero when θ = 0° or 180°, because μ is parallel or
antiparallel to B. Maximum torque occurs at θ = 90°.
</p>
</section>
<div class="legend" aria-hidden="true">
<span class="field"><i></i>Uniform field B</span>
<span class="moment"><i></i>Magnetic moment μ</span>
<span class="torque"><i></i>Torque τ</span>
<span class="current"><i></i>Current direction</span>
</div>
<div class="hint">Drag to orbit · Wheel to zoom · Double-click to reset</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.getElementById("scene");
const labelsElement = document.getElementById("labels");
const state = {
current: 2,
fieldMillitesla: 500,
paused: false,
showField: true,
angles: [0, 30, 60, 90]
};
const MODEL_RADIUS = 0.10;
const MODEL_AREA = Math.PI * MODEL_RADIUS * MODEL_RADIUS;
const VISUAL_RADIUS = 1.05;
const FIELD_DIRECTION = new THREE.Vector3(0, 1, 0);
const TORQUE_DIRECTION = new THREE.Vector3(0, 0, 1);
const LOCAL_NORMAL = new THREE.Vector3(0, 0, 1);
const colors = [0x54d6ff, 0xc084fc, 0xffb15c, 0x69e6a5];
const cssColors = ["#54d6ff", "#c084fc", "#ffb15c", "#69e6a5"];
const names = ["Loop A", "Loop B", "Loop C", "Loop D"];
const positions = [-5.7, -1.9, 1.9, 5.7];
const renderer = new THREE.WebGLRenderer({
canvas,
antialias: true,
alpha: true
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight, false);
renderer.setClearColor(0x050813, 0);
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.12;
const scene = new THREE.Scene();
scene.fog = new THREE.FogExp2(0x050813, 0.026);
const camera = new THREE.PerspectiveCamera(
42,
window.innerWidth / window.innerHeight,
0.1,
120
);
const cameraState = {
yaw: 0.38,
pitch: 0.30,
radius: 21,
target: new THREE.Vector3(0, -0.05, 0)
};
scene.add(new THREE.HemisphereLight(0x9ac5ff, 0x101421, 1.8));
const keyLight = new THREE.DirectionalLight(0xffffff, 3.2);
keyLight.position.set(5, 10, 8);
scene.add(keyLight);
const rimLight = new THREE.DirectionalLight(0x4e8dff, 2.0);
rimLight.position.set(-8, 4, -7);
scene.add(rimLight);
const floor = new THREE.Mesh(
new THREE.PlaneGeometry(55, 35),
new THREE.MeshStandardMaterial({
color: 0x070b15,
roughness: 0.88,
metalness: 0.12,
transparent: true,
opacity: 0.82
})
);
floor.rotation.x = -Math.PI / 2;
floor.position.y = -2.18;
scene.add(floor);
const grid = new THREE.GridHelper(38, 38, 0x41628c, 0x23324a);
grid.position.y = -2.16;
grid.material.transparent = true;
grid.material.opacity = 0.19;
grid.material.depthWrite = false;
scene.add(grid);
const fieldObjects = [];
function createFieldArrow(position, length, opacity = 0.35) {
const arrow = new THREE.ArrowHelper(
FIELD_DIRECTION,
position,
length,
0x4a9fff,
Math.min(0.28, length * 0.22),
Math.min(0.15, length * 0.12)
);
for (const part of [arrow.line, arrow.cone]) {
part.material.transparent = true;
part.material.opacity = opacity;
part.material.depthWrite = false;
}
scene.add(arrow);
fieldObjects.push(arrow);
return arrow;
}
// Repeated arrows communicate that the field is spatially uniform.
for (const x of [-8, -4, 0, 4, 8]) {
createFieldArrow(
new THREE.Vector3(x, -1.9, -3.65),
2.35,
0.26
);
createFieldArrow(
new THREE.Vector3(x, -1.9, 3.65),
2.35,
0.18
);
}
const mainFieldArrow = createFieldArrow(
new THREE.Vector3(-8.45, -1.9, 0),
3.25,
0.95
);
function createWorldLabel(className) {
const element = document.createElement("div");
element.className = `world-label ${className}`;
labelsElement.appendChild(element);
return element;
}
const fieldLabel = createWorldLabel("field-label");
fieldLabel.textContent = "B";
const beadGeometry = new THREE.SphereGeometry(0.045, 10, 8);
const loops = [];
function createLoop(index) {
const color = colors[index];
const root = new THREE.Group();
root.position.set(positions[index], 0, 0);
scene.add(root);
const torus = new THREE.Mesh(
new THREE.TorusGeometry(VISUAL_RADIUS, 0.072, 14, 112),
new THREE.MeshStandardMaterial({
color,
emissive: color,
emissiveIntensity: 0.14,
roughness: 0.25,
metalness: 0.52
})
);
root.add(torus);
const light = new THREE.PointLight(color, 13, 4.2, 2);
light.position.set(0, 0.1, 0);
root.add(light);
const beadMaterial = new THREE.MeshBasicMaterial({
color: 0xf4fbff
});
const beads = [];
const beadCount = 12;
for (let i = 0; i < beadCount; i++) {
const bead = new THREE.Mesh(beadGeometry, beadMaterial);
root.add(bead);
beads.push(bead);
}
const currentArrow = new THREE.ArrowHelper(
new THREE.Vector3(0, 1, 0),
new THREE.Vector3(VISUAL_RADIUS, 0, 0),
0.52,
0xffffff,
0.18,
0.09
);
root.add(currentArrow);
const momentArrow = new THREE.ArrowHelper(
LOCAL_NORMAL,
new THREE.Vector3(),
1.55,
0xc084fc,
0.27,
0.13
);
root.add(momentArrow);
const torqueArrow = new THREE.ArrowHelper(
TORQUE_DIRECTION,
root.position.clone(),
1,
0xff9f43,
0.28,
0.14
);
scene.add(torqueArrow);
const zeroMarker = new THREE.Mesh(
new THREE.RingGeometry(0.11, 0.17, 32),
new THREE.MeshBasicMaterial({
color: 0xff9f43,
transparent: true,
opacity: 0.9,
side: THREE.DoubleSide,
depthWrite: false
})
);
zeroMarker.position.copy(root.position);
scene.add(zeroMarker);
const angleLine = new THREE.Line(
new THREE.BufferGeometry(),
new THREE.LineBasicMaterial({
color: 0xe6ddff,
transparent: true,
opacity: 0.9
})
);
angleLine.position.copy(root.position);
scene.add(angleLine);
const angleWedge = new THREE.Mesh(
new THREE.BufferGeometry(),
new THREE.MeshBasicMaterial({
color: 0xc084fc,
transparent: true,
opacity: 0.085,
side: THREE.DoubleSide,
depthWrite: false
})
);
angleWedge.position.copy(root.position);
scene.add(angleWedge);
const angleLabel = createWorldLabel("angle-label");
angleLabel.style.setProperty("--loop-color", cssColors[index]);
angleLabel.innerHTML = `
<span class="dot"></span>
<b class="angle-label-text"></b>
`;
const torqueLabel = createWorldLabel("torque-label");
return {
root,
torus,
beads,
beadCount,
currentArrow,
momentArrow,
torqueArrow,
zeroMarker,
angleLine,
angleWedge,
angleLabel,
torqueLabel,
phase: index * 0.43,
card: null
};
}
for (let i = 0; i < 4; i++) {
loops.push(createLoop(i));
}
const cardsElement = document.getElementById("cards");
loops.forEach((loop, index) => {
const card = document.createElement("article");
card.className = "loop-card";
card.style.setProperty("--loop-color", cssColors[index]);
card.innerHTML = `
<div class="card-top">
<span class="swatch"></span>
<span class="loop-name">${names[index]}</span>
<output class="angle-output">0°</output>
</div>
<input
type="range"
min="0"
max="180"
step="1"
value="${state.angles[index]}"
aria-label="${names[index]} angle"
/>
<div class="torque-line">
<span>Torque magnitude</span>
<strong class="torque-value">0.0 mN·m</strong>
</div>
<div class="track"><i></i></div>
<div class="card-foot">
<span>sin θ</span>
<span class="fraction">0.00</span>
</div>
`;
cardsElement.appendChild(card);
loop.card = {
slider: card.querySelector("input"),
angleOutput: card.querySelector(".angle-output"),
torqueValue: card.querySelector(".torque-value"),
bar: card.querySelector(".track i"),
fraction: card.querySelector(".fraction")
};
loop.card.slider.addEventListener("input", (event) => {
state.angles[index] = Number(event.target.value);
updateLoop(index);
});
});
function torqueInNewtonMeters(angleDegrees) {
const theta = THREE.MathUtils.degToRad(angleDegrees);
const fieldTesla = state.fieldMillitesla / 1000;
return (
state.current *
MODEL_AREA *
fieldTesla *
Math.sin(theta)
);
}
function formatTorque(angleDegrees) {
const millinewtonMeters = torqueInNewtonMeters(angleDegrees) * 1000;
if (Math.abs(millinewtonMeters) < 0.05) {
return "0.0 mN·m";
}
return `${millinewtonMeters.toFixed(1)} mN·m`;
}
function updateArc(loop, angleDegrees) {
const theta = THREE.MathUtils.degToRad(angleDegrees);
const segments = 56;
const arcRadius = 0.62;
const arcPoints = [];
for (let i = 0; i <= segments; i++) {
const alpha = theta * (i / segments);
arcPoints.push(
new THREE.Vector3(
Math.sin(alpha) * arcRadius,
Math.cos(alpha) * arcRadius,
0
)
);
}
const lineGeometry = new THREE.BufferGeometry().setFromPoints(
arcPoints
);
loop.angleLine.geometry.dispose();
loop.angleLine.geometry = lineGeometry;
const trianglePositions = [];
for (let i = 0; i < segments; i++) {
trianglePositions.push(
0, 0, 0,
arcPoints[i].x, arcPoints[i].y, arcPoints[i].z,
arcPoints[i + 1].x, arcPoints[i + 1].y, arcPoints[i + 1].z
);
}
const wedgeGeometry = new THREE.BufferGeometry();
wedgeGeometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(trianglePositions, 3)
);
loop.angleWedge.geometry.dispose();
loop.angleWedge.geometry = wedgeGeometry;
const visible = angleDegrees > 0.5;
loop.angleLine.visible = visible;
loop.angleWedge.visible = visible;
}
function updateLoop(index) {
const loop = loops[index];
const angle = state.angles[index];
const theta = THREE.MathUtils.degToRad(angle);
/*
B points along +Y.
μ = (sin θ, cos θ, 0), so the angle between μ and B is θ.
τ = μ × B = (0, 0, sin θ).
*/
const magneticMomentDirection = new THREE.Vector3(
Math.sin(theta),
Math.cos(theta),
0
);
loop.root.quaternion.setFromUnitVectors(
LOCAL_NORMAL,
magneticMomentDirection
);
// The current arrow follows the right-hand rule.
const currentPhase = 0.72;
loop.currentArrow.position.set(
VISUAL_RADIUS * Math.cos(currentPhase),
VISUAL_RADIUS * Math.sin(currentPhase),
0
);
loop.currentArrow.setDirection(
new THREE.Vector3(
-Math.sin(currentPhase),
Math.cos(currentPhase),
0
)
);
updateArc(loop, angle);
const torque = torqueInNewtonMeters(angle);
const sinTheta = Math.sin(theta);
// Visual scaling is relative to a 0.08 N·m reference torque.
const visualLength = Math.min(2.65, 2.65 * torque / 0.08);
const isZero = Math.abs(torque) < 1e-8;
loop.torqueArrow.visible = !isZero;
if (!isZero) {
loop.torqueArrow.setLength(
Math.max(visualLength, 0.035),
Math.min(0.30, visualLength * 0.32),
Math.min(0.15, visualLength * 0.16)
);
}
loop.zeroMarker.visible = isZero;
loop.torqueLabel.classList.toggle("zero", isZero);
loop.angleLabel.querySelector(".angle-label-text").textContent =
`θ = ${angle}°`;
loop.torqueLabel.textContent = `τ = ${formatTorque(angle)}`;
loop.card.angleOutput.textContent = `${angle}°`;
loop.card.torqueValue.textContent = formatTorque(angle);
loop.card.bar.style.width = `${Math.abs(sinTheta) * 100}%`;
loop.card.fraction.textContent = sinTheta.toFixed(2);
}
function updateAll() {
document.getElementById("fieldOutput").textContent =
`${state.fieldMillitesla} mT`;
document.getElementById("currentOutput").textContent =
`${state.current.toFixed(1)} A`;
fieldLabel.textContent = `B = ${state.fieldMillitesla} mT`;
for (let i = 0; i < loops.length; i++) {
updateLoop(i);
}
}
function updateCamera() {
const aspect = window.innerWidth / window.innerHeight;
const portraitCompensation = Math.max(1, 0.92 / aspect);
const radius = cameraState.radius * portraitCompensation;
const horizontalRadius =
radius * Math.cos(cameraState.pitch);
camera.position.set(
cameraState.target.x +
horizontalRadius * Math.sin(cameraState.yaw),
cameraState.target.y +
radius * Math.sin(cameraState.pitch),
cameraState.target.z +
horizontalRadius * Math.cos(cameraState.yaw)
);
camera.lookAt(cameraState.target);
}
function resetCamera() {
cameraState.yaw = 0.38;
cameraState.pitch = 0.30;
cameraState.radius = 21;
updateCamera();
}
// Direct orbit and zoom controls; no external controls addon is used.
let pointerDown = false;
let pointerX = 0;
let pointerY = 0;
canvas.addEventListener("pointerdown", (event) => {
pointerDown = true;
pointerX = event.clientX;
pointerY = event.clientY;
canvas.setPointerCapture(event.pointerId);
});
canvas.addEventListener("pointermove", (event) => {
if (!pointerDown) return;
const deltaX = event.clientX - pointerX;
const deltaY = event.clientY - pointerY;
cameraState.yaw -= deltaX * 0.006;
cameraState.pitch = THREE.MathUtils.clamp(
cameraState.pitch + deltaY * 0.005,
-0.05,
1.30
);
pointerX = event.clientX;
pointerY = event.clientY;
});
function endPointer(event) {
pointerDown = false;
if (canvas.hasPointerCapture(event.pointerId)) {
canvas.releasePointerCapture(event.pointerId);
}
}
canvas.addEventListener("pointerup", endPointer);
canvas.addEventListener("pointercancel", endPointer);
canvas.addEventListener(
"wheel",
(event) => {
event.preventDefault();
cameraState.radius = THREE.MathUtils.clamp(
cameraState.radius * Math.exp(event.deltaY * 0.001),
12,
34
);
},
{ passive: false }
);
canvas.addEventListener("dblclick", resetCamera);
document.getElementById("fieldStrength").addEventListener(
"input",
(event) => {
state.fieldMillitesla = Number(event.target.value);
updateAll();
}
);
document.getElementById("currentStrength").addEventListener(
"input",
(event) => {
state.current = Number(event.target.value);
updateAll();
}
);
document.getElementById("pauseButton").addEventListener("click", () => {
state.paused = !state.paused;
document.getElementById("pauseButton").textContent =
state.paused ? "Resume current" : "Pause current";
});
document.getElementById("resetButton").addEventListener(
"click",
resetCamera
);
document.getElementById("presetButton").addEventListener("click", () => {
state.angles = [0, 30, 60, 90];
loops.forEach((loop, index) => {
loop.card.slider.value = state.angles[index];
});
updateAll();
});
document.getElementById("fieldToggle").addEventListener(
"change",
(event) => {
state.showField = event.target.checked;
for (const object of fieldObjects) {
object.visible = state.showField;
}
}
);
function placeLabel(element, worldPosition) {
const projected = worldPosition.clone().project(camera);
const visible =
projected.z > -1 &&
projected.z < 1 &&
projected.x > -1.2 &&
projected.x < 1.2 &&
projected.y > -1.2 &&
projected.y < 1.2;
element.style.opacity = visible ? "1" : "0";
if (!visible) return;
const x = (projected.x * 0.5 + 0.5) * window.innerWidth;
const y = (-projected.y * 0.5 + 0.5) * window.innerHeight;
element.style.left = `${x}px`;
element.style.top = `${y}px`;
}
function updateLabels() {
for (const loop of loops) {
const anglePosition = loop.root.position.clone();
anglePosition.y += 1.65;
const torquePosition = loop.root.position.clone();
torquePosition.x -= 0.72;
torquePosition.y -= 1.45;
torquePosition.z += 0.2;
placeLabel(loop.angleLabel, anglePosition);
placeLabel(loop.torqueLabel, torquePosition);
}
const fieldLabelPosition = mainFieldArrow.position.clone();
fieldLabelPosition.y += 3.55;
placeLabel(fieldLabel, fieldLabelPosition);
}
function onResize() {
renderer.setPixelRatio(
Math.min(window.devicePixelRatio, 2)
);
renderer.setSize(
window.innerWidth,
window.innerHeight,
false
);
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
updateCamera();
}
window.addEventListener("resize", onResize);
updateAll();
updateCamera();
let previousTime = performance.now();
function animate(now) {
const deltaTime = Math.min((now - previousTime) / 1000, 0.05);
previousTime = now;
if (!state.paused) {
for (const loop of loops) {
loop.phase +=
deltaTime *
(0.42 + state.current * 0.24);
for (let i = 0; i < loop.beadCount; i++) {
const phi =
loop.phase +
(i / loop.beadCount) * Math.PI * 2;
loop.beads[i].position.set(
VISUAL_RADIUS * Math.cos(phi),
VISUAL_RADIUS * Math.sin(phi),
0
);
}
loop.torus.material.emissiveIntensity =
0.13 + Math.sin(now * 0.0018 + loop.phase) * 0.025;
}
}
updateCamera();
renderer.render(scene, camera);
updateLabels();
requestAnimationFrame(animate);
}
requestAnimationFrame(animate);
</script>
</body>
</html>
```