Prompt specification
Build or customize the Three.js 3D scene "Current-loop torque demonstrator" 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: Current-loop torque demonstrator
Category: Science & Physics · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/current-loop-torque-demonstrator-x1653
Scene Description Prompt:
A low-poly current loop between magnetic poles, with opposing force arrows revealing torque. Build one HTML file with Three.js from cdnjs.
Working Three.js Starter (current-loop-torque-demonstrator-x1653):
```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 Lab</title>
<style>
:root {
--cyan: #55e7ff;
--amber: #ffb14a;
--purple: #c494ff;
--white: #edf7ff;
--muted: #92a8bd;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #07111f;
font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
color: var(--white);
}
#app {
position: fixed;
inset: 0;
}
canvas {
display: block;
width: 100%;
height: 100%;
touch-action: none;
cursor: grab;
}
canvas:active {
cursor: grabbing;
}
.title-panel {
position: fixed;
top: 24px;
left: 26px;
z-index: 5;
pointer-events: none;
text-shadow: 0 2px 18px rgba(0, 0, 0, 0.75);
}
.eyebrow {
color: var(--cyan);
font-size: 11px;
font-weight: 800;
letter-spacing: 0.24em;
text-transform: uppercase;
}
h1 {
margin: 7px 0 4px;
font-size: clamp(28px, 4vw, 52px);
line-height: 0.95;
letter-spacing: -0.055em;
font-weight: 850;
}
.subtitle {
margin: 10px 0 0;
color: var(--muted);
font-size: 13px;
letter-spacing: 0.035em;
}
.equation-card {
position: fixed;
top: 24px;
right: 26px;
z-index: 5;
min-width: 218px;
padding: 15px 17px;
border: 1px solid rgba(130, 190, 225, 0.18);
border-radius: 14px;
background: linear-gradient(
145deg,
rgba(12, 27, 45, 0.86),
rgba(7, 17, 31, 0.68)
);
box-shadow: 0 18px 50px rgba(0, 0, 0, 0.25);
backdrop-filter: blur(14px);
pointer-events: none;
}
.equation-row {
display: flex;
align-items: baseline;
justify-content: space-between;
gap: 22px;
padding: 5px 0;
color: #c6d4e1;
font-family: Georgia, "Times New Roman", serif;
font-size: 18px;
}
.equation-row small {
color: var(--muted);
font-family: Inter, ui-sans-serif, system-ui, sans-serif;
font-size: 10px;
letter-spacing: 0.05em;
text-transform: uppercase;
}
.cyan {
color: var(--cyan);
}
.amber {
color: var(--amber);
}
.purple {
color: var(--purple);
}
.legend {
position: fixed;
left: 26px;
bottom: 25px;
z-index: 5;
display: flex;
flex-wrap: wrap;
gap: 8px;
max-width: min(650px, calc(100vw - 270px));
pointer-events: none;
}
.chip {
display: flex;
align-items: center;
gap: 8px;
padding: 8px 11px;
border: 1px solid rgba(137, 187, 220, 0.14);
border-radius: 999px;
background: rgba(8, 19, 34, 0.72);
color: #b9c8d5;
font-size: 11px;
font-weight: 650;
letter-spacing: 0.035em;
backdrop-filter: blur(10px);
}
.dot {
width: 8px;
height: 8px;
border-radius: 50%;
box-shadow: 0 0 12px currentColor;
}
.dot.cyan {
background: var(--cyan);
color: var(--cyan);
}
.dot.amber {
background: var(--amber);
color: var(--amber);
}
.dot.purple {
background: var(--purple);
color: var(--purple);
}
.dot.copper {
background: #ee8b42;
color: #ee8b42;
}
.controls {
position: fixed;
right: 26px;
bottom: 25px;
z-index: 6;
display: flex;
gap: 9px;
}
button {
appearance: none;
border: 1px solid rgba(128, 190, 225, 0.2);
border-radius: 10px;
padding: 10px 13px;
background: rgba(11, 25, 42, 0.84);
color: var(--white);
font: inherit;
font-size: 11px;
font-weight: 750;
letter-spacing: 0.045em;
cursor: pointer;
backdrop-filter: blur(12px);
transition:
border-color 160ms ease,
background 160ms ease,
transform 160ms ease;
}
button:hover {
border-color: rgba(85, 231, 255, 0.55);
background: rgba(17, 40, 61, 0.94);
transform: translateY(-1px);
}
.hint {
position: fixed;
left: 50%;
bottom: 28px;
z-index: 4;
transform: translateX(-50%);
color: rgba(177, 198, 215, 0.58);
font-size: 10px;
letter-spacing: 0.09em;
text-transform: uppercase;
pointer-events: none;
}
@media (max-width: 800px) {
.title-panel {
top: 18px;
left: 18px;
}
.subtitle,
.hint {
display: none;
}
.equation-card {
top: auto;
right: 16px;
bottom: 78px;
min-width: 178px;
padding: 10px 12px;
}
.equation-row {
font-size: 15px;
}
.legend {
left: 16px;
bottom: 18px;
max-width: calc(100vw - 185px);
}
.chip {
padding: 7px 9px;
font-size: 9px;
}
.controls {
right: 16px;
bottom: 18px;
flex-direction: column;
}
button {
padding: 9px 11px;
}
}
</style>
</head>
<body>
<div id="app"></div>
<header class="title-panel">
<div class="eyebrow">Electromagnetism / Interactive Diagram</div>
<h1>CURRENT LOOP</h1>
<p class="subtitle">Opposing magnetic forces reveal the resulting torque.</p>
</header>
<aside class="equation-card">
<div class="equation-row">
<span class="cyan">B →</span>
<small>N to S field</small>
</div>
<div class="equation-row">
<span class="amber">F = I L × B</span>
<small>Force pair</small>
</div>
<div class="equation-row">
<span class="purple">τ = r × F</span>
<small>Torque</small>
</div>
</aside>
<div class="legend">
<div class="chip"><span class="dot copper"></span>Current loop</div>
<div class="chip"><span class="dot cyan"></span>Magnetic field</div>
<div class="chip"><span class="dot amber"></span>Opposing forces</div>
<div class="chip"><span class="dot purple"></span>Rotation / torque</div>
</div>
<div class="controls">
<button id="pauseButton" type="button">Pause Current</button>
<button id="resetButton" type="button">Reset View</button>
</div>
<div class="hint">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 app = document.getElementById("app");
const pauseButton = document.getElementById("pauseButton");
const resetButton = document.getElementById("resetButton");
// ------------------------------------------------------------
// Renderer, scene and camera
// ------------------------------------------------------------
const renderer = new THREE.WebGLRenderer({
antialias: true,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.15;
app.appendChild(renderer.domElement);
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x07111f);
scene.fog = new THREE.Fog(0x07111f, 11, 24);
const camera = new THREE.PerspectiveCamera(
43,
window.innerWidth / window.innerHeight,
0.1,
100
);
const cameraTarget = new THREE.Vector3(0, 0, 0);
let cameraRadius = 9.4;
let cameraAzimuth = 0.58;
let cameraPolar = 1.13;
function updateCamera() {
const sinPolar = Math.sin(cameraPolar);
camera.position.set(
cameraTarget.x + cameraRadius * sinPolar * Math.sin(cameraAzimuth),
cameraTarget.y + cameraRadius * Math.cos(cameraPolar),
cameraTarget.z + cameraRadius * sinPolar * Math.cos(cameraAzimuth)
);
camera.lookAt(cameraTarget);
}
updateCamera();
// ------------------------------------------------------------
// Lighting
// ------------------------------------------------------------
scene.add(new THREE.HemisphereLight(0x9ddcff, 0x152033, 2.0));
const keyLight = new THREE.DirectionalLight(0xffffff, 3.2);
keyLight.position.set(5, 8, 6);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(2048, 2048);
keyLight.shadow.camera.left = -7;
keyLight.shadow.camera.right = 7;
keyLight.shadow.camera.top = 7;
keyLight.shadow.camera.bottom = -7;
keyLight.shadow.camera.near = 0.5;
keyLight.shadow.camera.far = 25;
keyLight.shadow.bias = -0.0004;
scene.add(keyLight);
const rimLight = new THREE.DirectionalLight(0x45bfff, 2.0);
rimLight.position.set(-6, 3, -5);
scene.add(rimLight);
const warmLight = new THREE.PointLight(0xff8a42, 11, 8, 2);
warmLight.position.set(-3.2, 0.5, 2.5);
scene.add(warmLight);
const coolLight = new THREE.PointLight(0x3f8cff, 10, 8, 2);
coolLight.position.set(3.2, -0.2, 2.5);
scene.add(coolLight);
// ------------------------------------------------------------
// Ground and grid
// ------------------------------------------------------------
const floor = new THREE.Mesh(
new THREE.PlaneGeometry(30, 30),
new THREE.MeshStandardMaterial({
color: 0x091727,
roughness: 0.95,
metalness: 0.05
})
);
floor.rotation.x = -Math.PI / 2;
floor.position.y = -1.84;
floor.receiveShadow = true;
scene.add(floor);
const grid = new THREE.GridHelper(22, 22, 0x24445e, 0x132b40);
grid.position.y = -1.825;
grid.material.transparent = true;
grid.material.opacity = 0.32;
scene.add(grid);
// ------------------------------------------------------------
// Utility functions
// ------------------------------------------------------------
function createTextSprite(
text,
color = "#ffffff",
background = "rgba(5, 16, 29, 0.78)",
scaleX = 1.15,
scaleY = 0.29
) {
const canvas = document.createElement("canvas");
canvas.width = 512;
canvas.height = 128;
const context = canvas.getContext("2d");
context.clearRect(0, 0, canvas.width, canvas.height);
const radius = 24;
const x = 8;
const y = 8;
const width = canvas.width - 16;
const height = canvas.height - 16;
context.beginPath();
context.moveTo(x + radius, y);
context.lineTo(x + width - radius, y);
context.quadraticCurveTo(x + width, y, x + width, y + radius);
context.lineTo(x + width, y + height - radius);
context.quadraticCurveTo(
x + width,
y + height,
x + width - radius,
y + height
);
context.lineTo(x + radius, y + height);
context.quadraticCurveTo(x, y + height, x, y + height - radius);
context.lineTo(x, y + radius);
context.quadraticCurveTo(x, y, x + radius, y);
context.closePath();
context.fillStyle = background;
context.fill();
context.strokeStyle = color;
context.globalAlpha = 0.25;
context.lineWidth = 3;
context.stroke();
context.globalAlpha = 1;
context.fillStyle = color;
context.font = "800 58px Arial, sans-serif";
context.textAlign = "center";
context.textBaseline = "middle";
context.fillText(text, canvas.width / 2, canvas.height / 2 + 2);
const texture = new THREE.CanvasTexture(canvas);
texture.colorSpace = THREE.SRGBColorSpace;
const material = new THREE.SpriteMaterial({
map: texture,
transparent: true,
depthTest: false,
depthWrite: false
});
const sprite = new THREE.Sprite(material);
sprite.scale.set(scaleX, scaleY, 1);
sprite.renderOrder = 20;
return sprite;
}
function cylinderBetween(start, end, radius, material, radialSegments = 6) {
const direction = new THREE.Vector3().subVectors(end, start);
const length = direction.length();
const mesh = new THREE.Mesh(
new THREE.CylinderGeometry(
radius,
radius,
length,
radialSegments,
1,
false
),
material
);
mesh.position.copy(start).add(end).multiplyScalar(0.5);
mesh.quaternion.setFromUnitVectors(
new THREE.Vector3(0, 1, 0),
direction.normalize()
);
mesh.castShadow = true;
mesh.receiveShadow = true;
return mesh;
}
function createArrow(
origin,
direction,
length,
color,
shaftRadius = 0.035,
headRadius = 0.12,
headLength = 0.28
) {
const group = new THREE.Group();
group.position.copy(origin);
const normalizedDirection = direction.clone().normalize();
const actualHeadLength = Math.min(headLength, length * 0.4);
const shaftLength = Math.max(0.001, length - actualHeadLength);
const material = new THREE.MeshStandardMaterial({
color,
emissive: color,
emissiveIntensity: 0.34,
roughness: 0.42,
metalness: 0.2
});
const shaft = new THREE.Mesh(
new THREE.CylinderGeometry(
shaftRadius,
shaftRadius,
shaftLength,
6,
1,
false
),
material
);
shaft.position.y = shaftLength / 2;
shaft.castShadow = true;
group.add(shaft);
const head = new THREE.Mesh(
new THREE.ConeGeometry(headRadius, actualHeadLength, 6),
material
);
head.position.y = shaftLength + actualHeadLength / 2;
head.castShadow = true;
group.add(head);
group.quaternion.setFromUnitVectors(
new THREE.Vector3(0, 1, 0),
normalizedDirection
);
return group;
}
// ------------------------------------------------------------
// Low-poly magnetic poles
// ------------------------------------------------------------
function createMagnet(x, innerDirection, letter, bodyColor, capColor) {
const group = new THREE.Group();
group.position.x = x;
const bodyMaterial = new THREE.MeshStandardMaterial({
color: bodyColor,
emissive: bodyColor,
emissiveIntensity: 0.08,
roughness: 0.48,
metalness: 0.48,
flatShading: true
});
const capMaterial = new THREE.MeshStandardMaterial({
color: capColor,
emissive: capColor,
emissiveIntensity: 0.14,
roughness: 0.32,
metalness: 0.55,
flatShading: true
});
const body = new THREE.Mesh(
new THREE.CylinderGeometry(1.25, 1.25, 1.3, 8, 1, false),
bodyMaterial
);
body.rotation.z = Math.PI / 2;
body.castShadow = true;
body.receiveShadow = true;
group.add(body);
const poleFace = new THREE.Mesh(
new THREE.CylinderGeometry(1.08, 1.08, 0.18, 8, 1, false),
capMaterial
);
poleFace.rotation.z = Math.PI / 2;
poleFace.position.x = innerDirection * 0.69;
poleFace.castShadow = true;
poleFace.receiveShadow = true;
group.add(poleFace);
const outerRing = new THREE.Mesh(
new THREE.CylinderGeometry(1.31, 1.31, 0.11, 8, 1, false),
new THREE.MeshStandardMaterial({
color: 0x111b28,
roughness: 0.7,
metalness: 0.5,
flatShading: true
})
);
outerRing.rotation.z = Math.PI / 2;
outerRing.position.x = -innerDirection * 0.66;
outerRing.castShadow = true;
group.add(outerRing);
const label = createTextSprite(
letter,
letter === "N" ? "#ffb1b7" : "#a9d6ff",
"rgba(4, 11, 20, 0.58)",
0.9,
0.28
);
label.position.set(0, 0.05, 1.28);
group.add(label);
scene.add(group);
return group;
}
const northPole = createMagnet(
-3.1,
1,
"N",
0x9c2734,
0xff5367
);
const southPole = createMagnet(
3.1,
-1,
"S",
0x174b8f,
0x368cff
);
// ------------------------------------------------------------
// Magnetic field arrows: N to S along +X
// ------------------------------------------------------------
const fieldArrowMaterialColor = 0x45ddff;
const fieldPositions = [
new THREE.Vector3(-1.95, 1.43, 0),
new THREE.Vector3(-1.95, -1.43, 0)
];
fieldPositions.forEach((position) => {
const arrow = createArrow(
position,
new THREE.Vector3(1, 0, 0),
3.9,
fieldArrowMaterialColor,
0.022,
0.095,
0.24
);
arrow.traverse((child) => {
if (child.material) {
child.material.transparent = true;
child.material.opacity = 0.72;
child.material.emissiveIntensity = 0.65;
}
});
scene.add(arrow);
});
const fieldLabel = createTextSprite(
"B",
"#66e9ff",
"rgba(4, 18, 31, 0.70)",
0.7,
0.25
);
fieldLabel.position.set(-2.02, 1.72, 0.05);
scene.add(fieldLabel);
// ------------------------------------------------------------
// Current loop in the XY plane
// Clockwise as viewed from +Z
// ------------------------------------------------------------
const loopGroup = new THREE.Group();
scene.add(loopGroup);
const loopWidth = 1.55;
const loopHeight = 2.15;
const conductorRadius = 0.075;
const topLeft = new THREE.Vector3(
-loopWidth / 2,
loopHeight / 2,
0
);
const topRight = new THREE.Vector3(
loopWidth / 2,
loopHeight / 2,
0
);
const bottomRight = new THREE.Vector3(
loopWidth / 2,
-loopHeight / 2,
0
);
const bottomLeft = new THREE.Vector3(
-loopWidth / 2,
-loopHeight / 2,
0
);
const copperMaterial = new THREE.MeshStandardMaterial({
color: 0xf08a3f,
emissive: 0x8d2d08,
emissiveIntensity: 0.14,
roughness: 0.3,
metalness: 0.72,
flatShading: true
});
const conductorSegments = [
[topLeft, topRight],
[topRight, bottomRight],
[bottomRight, bottomLeft],
[bottomLeft, topLeft]
];
conductorSegments.forEach(([start, end]) => {
loopGroup.add(
cylinderBetween(start, end, conductorRadius, copperMaterial, 6)
);
});
const jointMaterial = new THREE.MeshStandardMaterial({
color: 0xffa152,
emissive: 0x8d2d08,
emissiveIntensity: 0.12,
roughness: 0.28,
metalness: 0.72,
flatShading: true
});
[topLeft, topRight, bottomRight, bottomLeft].forEach((point) => {
const joint = new THREE.Mesh(
new THREE.IcosahedronGeometry(conductorRadius * 1.25, 1),
jointMaterial
);
joint.position.copy(point);
joint.castShadow = true;
loopGroup.add(joint);
});
// Current-direction arrows.
const currentArrow = createArrow(
new THREE.Vector3(-0.5, loopHeight / 2 + 0.15, 0),
new THREE.Vector3(1, 0, 0),
1.0,
0xffd85a,
0.026,
0.085,
0.19
);
loopGroup.add(currentArrow);
const bottomCurrentArrow = createArrow(
new THREE.Vector3(0.5, -loopHeight / 2 - 0.15, 0),
new THREE.Vector3(-1, 0, 0),
1.0,
0xffd85a,
0.026,
0.085,
0.19
);
loopGroup.add(bottomCurrentArrow);
const currentLabel = createTextSprite(
"I",
"#ffe36f",
"rgba(13, 22, 24, 0.65)",
0.58,
0.25
);
currentLabel.position.set(0.12, loopHeight / 2 + 0.34, 0);
loopGroup.add(currentLabel);
// ------------------------------------------------------------
// Opposing force arrows
// Right side: current downward, F points +Z
// Left side: current upward, F points -Z
// ------------------------------------------------------------
const forceColor = 0xffa23d;
const forceLength = 1.5;
const rightForce = createArrow(
new THREE.Vector3(loopWidth / 2, -0.03, 0.08),
new THREE.Vector3(0, 0, 1),
forceLength,
forceColor,
0.052,
0.16,
0.34
);
const leftForce = createArrow(
new THREE.Vector3(-loopWidth / 2, -0.03, -0.08),
new THREE.Vector3(0, 0, -1),
forceLength,
forceColor,
0.052,
0.16,
0.34
);
loopGroup.add(rightForce, leftForce);
const rightForceLabel = createTextSprite(
"F",
"#ffc06a",
"rgba(28, 17, 9, 0.72)",
0.58,
0.25
);
rightForceLabel.position.set(
loopWidth / 2,
-0.03,
0.08 + forceLength + 0.15
);
const leftForceLabel = createTextSprite(
"F",
"#ffc06a",
"rgba(28, 17, 9, 0.72)",
0.58,
0.25
);
leftForceLabel.position.set(
-loopWidth / 2,
-0.03,
-0.08 - forceLength - 0.15
);
loopGroup.add(rightForceLabel, leftForceLabel);
// ------------------------------------------------------------
// Animated current particles
// ------------------------------------------------------------
const beadGeometry = new THREE.IcosahedronGeometry(0.092, 1);
const beadMaterial = new THREE.MeshBasicMaterial({
color: 0xffe761
});
const beadCount = 13;
const currentParticles = [];
for (let i = 0; i < beadCount; i++) {
const bead = new THREE.Mesh(beadGeometry, beadMaterial);
loopGroup.add(bead);
currentParticles.push(bead);
}
const totalPathLength = 2 * (loopWidth + loopHeight);
function pointOnLoop(distance) {
let d = distance % totalPathLength;
if (d < 0) {
d += totalPathLength;
}
if (d < loopWidth) {
return topLeft.clone().lerp(topRight, d / loopWidth);
}
d -= loopWidth;
if (d < loopHeight) {
return topRight.clone().lerp(bottomRight, d / loopHeight);
}
d -= loopHeight;
if (d < loopWidth) {
return bottomRight.clone().lerp(bottomLeft, d / loopWidth);
}
d -= loopWidth;
return bottomLeft.clone().lerp(topLeft, d / loopHeight);
}
let currentPhase = 0;
let currentRunning = true;
pauseButton.addEventListener("click", () => {
currentRunning = !currentRunning;
pauseButton.textContent = currentRunning ? "Pause Current" : "Resume Current";
});
// ------------------------------------------------------------
// Torque arc and rotation axis
// Torque is about the vertical Y axis
// ------------------------------------------------------------
const torqueColor = 0xc494ff;
const torqueArcRadius = 1.28;
const torqueArcY = 0.92;
const arcStart = 0.3;
const arcEnd = 2.25;
const arcPoints = [];
for (let i = 0; i <= 56; i++) {
const t = i / 56;
const angle = THREE.MathUtils.lerp(arcStart, arcEnd, t);
arcPoints.push(
new THREE.Vector3(
Math.cos(angle) * torqueArcRadius,
torqueArcY,
Math.sin(angle) * torqueArcRadius
)
);
}
const torqueArcGeometry = new THREE.BufferGeometry().setFromPoints(
arcPoints
);
const torqueArc = new THREE.Line(
torqueArcGeometry,
new THREE.LineDashedMaterial({
color: torqueColor,
dashSize: 0.13,
gapSize: 0.075,
transparent: true,
opacity: 0.9,
depthTest: false
})
);
torqueArc.computeLineDistances();
torqueArc.renderOrder = 15;
scene.add(torqueArc);
const arcEndPoint = arcPoints[arcPoints.length - 1];
const arcTangent = new THREE.Vector3()
.subVectors(arcPoints[arcPoints.length - 1], arcPoints[arcPoints.length - 2])
.normalize();
const torqueHead = new THREE.Mesh(
new THREE.ConeGeometry(0.14, 0.34, 6),
new THREE.MeshStandardMaterial({
color: torqueColor,
emissive: torqueColor,
emissiveIntensity: 0.5,
roughness: 0.4
})
);
torqueHead.position.copy(arcEndPoint);
torqueHead.quaternion.setFromUnitVectors(
new THREE.Vector3(0, 1, 0),
arcTangent
);
torqueHead.renderOrder = 15;
scene.add(torqueHead);
const torqueLabel = createTextSprite(
"τ",
"#d6b4ff",
"rgba(21, 11, 32, 0.72)",
0.72,
0.27
);
torqueLabel.position.set(
arcEndPoint.x - 0.2,
arcEndPoint.y + 0.27,
arcEndPoint.z
);
scene.add(torqueLabel);
const axisGeometry = new THREE.BufferGeometry().setFromPoints([
new THREE.Vector3(0, -1.65, 0),
new THREE.Vector3(0, 1.7, 0)
]);
const axisLine = new THREE.Line(
axisGeometry,
new THREE.LineDashedMaterial({
color: 0x8294a8,
dashSize: 0.08,
gapSize: 0.07,
transparent: true,
opacity: 0.52,
depthTest: false
})
);
axisLine.computeLineDistances();
axisLine.renderOrder = 14;
scene.add(axisLine);
const axisLabel = createTextSprite(
"rotation axis",
"#a8b8c8",
"rgba(5, 14, 25, 0.62)",
1.25,
0.24
);
axisLabel.position.set(0, 1.82, 0);
scene.add(axisLabel);
// ------------------------------------------------------------
// Custom orbit controls: no addons
// ------------------------------------------------------------
let dragging = false;
let previousX = 0;
let previousY = 0;
renderer.domElement.addEventListener("pointerdown", (event) => {
dragging = true;
previousX = event.clientX;
previousY = event.clientY;
renderer.domElement.setPointerCapture(event.pointerId);
});
renderer.domElement.addEventListener("pointermove", (event) => {
if (!dragging) {
return;
}
const deltaX = event.clientX - previousX;
const deltaY = event.clientY - previousY;
previousX = event.clientX;
previousY = event.clientY;
cameraAzimuth -= deltaX * 0.006;
cameraPolar = THREE.MathUtils.clamp(
cameraPolar - deltaY * 0.005,
0.48,
1.48
);
updateCamera();
});
function endDrag(event) {
dragging = false;
if (renderer.domElement.hasPointerCapture(event.pointerId)) {
renderer.domElement.releasePointerCapture(event.pointerId);
}
}
renderer.domElement.addEventListener("pointerup", endDrag);
renderer.domElement.addEventListener("pointercancel", endDrag);
renderer.domElement.addEventListener(
"wheel",
(event) => {
event.preventDefault();
cameraRadius = THREE.MathUtils.clamp(
cameraRadius * Math.exp(event.deltaY * 0.001),
5.2,
15
);
updateCamera();
},
{ passive: false }
);
resetButton.addEventListener("click", () => {
cameraRadius = 9.4;
cameraAzimuth = 0.58;
cameraPolar = 1.13;
updateCamera();
});
// ------------------------------------------------------------
// Animation
// ------------------------------------------------------------
const clock = new THREE.Clock();
function animate() {
requestAnimationFrame(animate);
const delta = Math.min(clock.getDelta(), 0.05);
const elapsed = clock.elapsedTime;
if (currentRunning) {
currentPhase = (currentPhase + delta * 0.13) % 1;
}
currentParticles.forEach((particle, index) => {
const distance =
(currentPhase + index / currentParticles.length) * totalPathLength;
particle.position.copy(pointOnLoop(distance));
});
// A restrained rocking motion communicates the torque without
// obscuring the relationship between current and magnetic field.
loopGroup.rotation.y = Math.sin(elapsed * 0.9) * 0.045;
// Subtle emissive breathing on the force arrows.
const forcePulse = 0.42 + Math.sin(elapsed * 3.2) * 0.1;
rightForce.traverse((child) => {
if (child.material) {
child.material.emissiveIntensity = forcePulse;
}
});
leftForce.traverse((child) => {
if (child.material) {
child.material.emissiveIntensity = forcePulse;
}
});
northPole.children.forEach((child) => {
if (child.material?.emissive) {
child.material.emissiveIntensity =
0.08 + Math.sin(elapsed * 1.7) * 0.025;
}
});
southPole.children.forEach((child) => {
if (child.material?.emissive) {
child.material.emissiveIntensity =
0.08 + Math.sin(elapsed * 1.7 + 1.2) * 0.025;
}
});
renderer.render(scene, camera);
}
animate();
window.addEventListener("resize", () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
});
</script>
</body>
</html>
```