Prompt specification
Build or customize the Three.js 3D scene "Solenoid coil and axial field model" 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: Solenoid coil and axial field model
Category: Science & Physics · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/solenoid-coil-and-axial-field-model-x1669
Scene Description Prompt:
Model a low-poly solenoid with current arrows, axial magnetic field, and external return paths under the long-solenoid approximation. Three.js from cdnjs, standalone HTML.
Working Three.js Starter (solenoid-coil-and-axial-field-model-x1669):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Low-Poly Solenoid</title>
<style>
:root {
color-scheme: dark;
--bg: #080b12;
--panel: rgba(13, 18, 28, 0.82);
--border: rgba(161, 188, 222, 0.18);
--text: #e8eef8;
--muted: #91a0b5;
--cyan: #68e4ff;
--orange: #ffad43;
--green: #74e0ae;
}
* {
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;
}
#app {
position: relative;
width: 100%;
height: 100%;
}
#scene {
position: absolute;
inset: 0;
width: 100%;
height: 100%;
display: block;
touch-action: none;
cursor: grab;
}
#scene.grabbing {
cursor: grabbing;
}
.panel {
position: absolute;
z-index: 2;
border: 1px solid var(--border);
border-radius: 14px;
background: var(--panel);
box-shadow: 0 14px 45px rgba(0, 0, 0, 0.28);
backdrop-filter: blur(14px);
-webkit-backdrop-filter: blur(14px);
}
.intro {
top: 18px;
left: 18px;
width: min(430px, calc(100vw - 36px));
padding: 18px 20px;
pointer-events: none;
}
.eyebrow {
color: var(--cyan);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.18em;
text-transform: uppercase;
}
h1 {
margin: 6px 0 7px;
font-size: clamp(24px, 3vw, 36px);
line-height: 1.05;
letter-spacing: -0.035em;
}
.intro p {
margin: 0;
max-width: 380px;
color: var(--muted);
font-size: 13px;
line-height: 1.55;
}
.controls {
top: 18px;
right: 18px;
width: 230px;
padding: 14px;
}
.controls h2 {
margin: 0 0 10px;
color: var(--muted);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.15em;
text-transform: uppercase;
}
.toggle {
display: flex;
align-items: center;
gap: 9px;
min-height: 31px;
color: #d9e2ef;
font-size: 13px;
cursor: pointer;
user-select: none;
}
.toggle input {
width: 15px;
height: 15px;
margin: 0;
accent-color: var(--cyan);
cursor: pointer;
}
.toggle.current input {
accent-color: var(--orange);
}
.toggle.return input {
accent-color: #59bddd;
}
#reset-view {
width: 100%;
margin-top: 10px;
padding: 9px 12px;
border: 1px solid var(--border);
border-radius: 8px;
background: rgba(255, 255, 255, 0.055);
color: var(--text);
font: inherit;
font-size: 12px;
cursor: pointer;
transition: background 150ms ease, border-color 150ms ease;
}
#reset-view:hover {
background: rgba(104, 228, 255, 0.1);
border-color: rgba(104, 228, 255, 0.42);
}
.science {
bottom: 18px;
left: 18px;
width: min(390px, calc(100vw - 36px));
padding: 15px 16px;
pointer-events: none;
}
.equations {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 9px;
margin-bottom: 10px;
}
.equation {
padding: 10px;
border: 1px solid rgba(161, 188, 222, 0.12);
border-radius: 9px;
background: rgba(255, 255, 255, 0.025);
}
.equation strong {
display: block;
margin-bottom: 4px;
color: var(--muted);
font-size: 10px;
letter-spacing: 0.1em;
text-transform: uppercase;
}
.equation span {
display: block;
color: var(--cyan);
font-family: Georgia, "Times New Roman", serif;
font-size: 20px;
}
.equation small {
color: var(--muted);
font-size: 10px;
}
.science p {
margin: 0;
color: var(--muted);
font-size: 11px;
line-height: 1.5;
}
.legend {
display: flex;
flex-wrap: wrap;
gap: 8px 14px;
margin-top: 11px;
padding-top: 10px;
border-top: 1px solid rgba(161, 188, 222, 0.12);
font-size: 10px;
color: #bdc8d8;
}
.legend span {
display: inline-flex;
align-items: center;
gap: 6px;
}
.swatch {
width: 16px;
height: 4px;
border-radius: 3px;
background: var(--cyan);
}
.swatch.current {
background: var(--orange);
}
.swatch.return {
height: 0;
border-top: 2px dashed #59bddd;
border-radius: 0;
background: transparent;
}
.swatch.core {
background: #8898aa;
}
.hint {
position: absolute;
right: 18px;
bottom: 18px;
z-index: 2;
padding: 9px 12px;
border: 1px solid var(--border);
border-radius: 999px;
background: rgba(13, 18, 28, 0.7);
color: var(--muted);
font-size: 11px;
pointer-events: none;
}
#loading {
position: absolute;
inset: 0;
z-index: 10;
display: grid;
place-items: center;
background: var(--bg);
color: var(--muted);
font-size: 13px;
letter-spacing: 0.08em;
}
@media (max-width: 760px) {
.intro {
top: 10px;
left: 10px;
right: 10px;
width: auto;
padding: 13px 15px;
}
.intro p {
display: none;
}
.controls {
top: auto;
right: 10px;
bottom: 10px;
width: 185px;
padding: 11px;
}
.science {
left: 10px;
bottom: 10px;
width: calc(100vw - 215px);
padding: 11px;
}
.legend,
.science p {
display: none;
}
.equations {
gap: 5px;
}
.equation {
padding: 7px;
}
.equation span {
font-size: 16px;
}
.hint {
display: none;
}
}
@media (max-width: 480px) {
.science {
display: none;
}
.controls {
left: 10px;
width: auto;
}
}
</style>
</head>
<body>
<div id="app">
<canvas id="scene"></canvas>
<header class="panel intro">
<div class="eyebrow">Electromagnetism · Long-Solenoid Limit</div>
<h1>Low-poly solenoid</h1>
<p>
Conventional current follows the copper winding. By the right-hand rule,
the interior magnetic field points along the solenoid axis toward N.
</p>
</header>
<aside class="panel controls">
<h2>Display Layers</h2>
<label class="toggle current">
<input id="toggle-current" type="checkbox" checked />
<span>Current arrows</span>
</label>
<label class="toggle">
<input id="toggle-field" type="checkbox" checked />
<span>Internal field B</span>
</label>
<label class="toggle return">
<input id="toggle-return" type="checkbox" checked />
<span>External return paths</span>
</label>
<label class="toggle">
<input id="toggle-orbit" type="checkbox" />
<span>Automatic orbit</span>
</label>
<button id="reset-view" type="button">Reset camera</button>
</aside>
<section class="panel science">
<div class="equations">
<div class="equation">
<strong>Inside</strong>
<span>B ≈ μ₀ nI</span>
<small>uniform and axial</small>
</div>
<div class="equation">
<strong>Outside</strong>
<span>B ≈ 0</span>
<small>away from the ends</small>
</div>
</div>
<p>
For L ≫ R and n = N/L, the field is confined to the interior.
The dashed arcs are qualitative fringing/return paths near the ends,
not a finite exterior field in the ideal long-solenoid limit.
</p>
<div class="legend">
<span><i class="swatch current"></i>Current I</span>
<span><i class="swatch"></i>Axial field B</span>
<span><i class="swatch return"></i>Weak return/fringing</span>
<span><i class="swatch core"></i>Solenoid core</span>
</div>
</section>
<div class="hint">Drag to orbit · Scroll to zoom · Double-click to reset</div>
<div id="loading">Loading Three.js…</div>
</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 loading = document.getElementById("loading");
const renderer = new THREE.WebGLRenderer({
canvas,
antialias: true,
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.12;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x080b12);
scene.fog = new THREE.Fog(0x080b12, 13, 29);
const camera = new THREE.PerspectiveCamera(
43,
window.innerWidth / window.innerHeight,
0.1,
100
);
// ------------------------------------------------------------------
// Lighting and environment
// ------------------------------------------------------------------
scene.add(new THREE.HemisphereLight(0xc8e7ff, 0x15121a, 1.8));
const keyLight = new THREE.DirectionalLight(0xffffff, 3.2);
keyLight.position.set(4, 8, 7);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(1024, 1024);
keyLight.shadow.camera.left = -10;
keyLight.shadow.camera.right = 10;
keyLight.shadow.camera.top = 8;
keyLight.shadow.camera.bottom = -8;
keyLight.shadow.camera.near = 1;
keyLight.shadow.camera.far = 30;
scene.add(keyLight);
const cyanLight = new THREE.PointLight(0x42dfff, 14, 13, 2);
cyanLight.position.set(0, 1.5, 3.5);
scene.add(cyanLight);
const copperLight = new THREE.PointLight(0xff8b32, 10, 10, 2);
copperLight.position.set(-3, -0.5, 2);
scene.add(copperLight);
const floor = new THREE.Mesh(
new THREE.PlaneGeometry(45, 45),
new THREE.MeshStandardMaterial({
color: 0x0d121b,
roughness: 0.96,
metalness: 0.02
})
);
floor.rotation.x = -Math.PI / 2;
floor.position.y = -2.4;
floor.receiveShadow = true;
scene.add(floor);
const grid = new THREE.GridHelper(30, 30, 0x304258, 0x192332);
grid.position.y = -2.38;
grid.material.transparent = true;
grid.material.opacity = 0.42;
scene.add(grid);
// ------------------------------------------------------------------
// Solenoid geometry
// ------------------------------------------------------------------
const coreGroup = new THREE.Group();
const windingGroup = new THREE.Group();
const currentGroup = new THREE.Group();
const fieldGroup = new THREE.Group();
const returnGroup = new THREE.Group();
scene.add(
coreGroup,
windingGroup,
currentGroup,
fieldGroup,
returnGroup
);
const coilLength = 6.8;
const coilRadius = 1.3;
const coilTurns = 13;
const coreMaterial = new THREE.MeshStandardMaterial({
color: 0x718197,
roughness: 0.72,
metalness: 0.18,
transparent: true,
opacity: 0.18,
depthWrite: false,
side: THREE.DoubleSide,
flatShading: true
});
const formerGeometry = new THREE.CylinderGeometry(
1.03,
1.03,
coilLength,
16,
1,
false
);
const former = new THREE.Mesh(formerGeometry, coreMaterial);
former.rotation.z = Math.PI / 2;
former.castShadow = true;
coreGroup.add(former);
const formerEdges = new THREE.LineSegments(
new THREE.EdgesGeometry(formerGeometry, 18),
new THREE.LineBasicMaterial({
color: 0x9aacc2,
transparent: true,
opacity: 0.27
})
);
former.add(formerEdges);
const capMaterial = new THREE.MeshStandardMaterial({
color: 0x8798ac,
roughness: 0.54,
metalness: 0.38,
flatShading: true
});
for (const side of [-1, 1]) {
const cap = new THREE.Mesh(
new THREE.CylinderGeometry(1.065, 1.065, 0.13, 16),
capMaterial
);
cap.rotation.z = Math.PI / 2;
cap.position.x = side * 3.42;
cap.castShadow = true;
coreGroup.add(cap);
}
/**
* Creates a low-poly tube around a polyline without using addons.
* normalAt supplies a preferred normal for each cross-section.
*/
function buildTubeGeometry(
points,
radius,
radialSegments,
normalAt
) {
const positions = [];
const indices = [];
const tangents = [];
for (let i = 0; i < points.length; i++) {
let tangent;
if (i === 0) {
tangent = points[1].clone().sub(points[0]);
} else if (i === points.length - 1) {
tangent = points[i].clone().sub(points[i - 1]);
} else {
tangent = points[i + 1].clone().sub(points[i - 1]);
}
tangents.push(tangent.normalize());
}
for (let i = 0; i < points.length; i++) {
const point = points[i];
const tangent = tangents[i];
const normal = normalAt(point, tangent, i).clone();
normal.addScaledVector(tangent, -normal.dot(tangent));
if (normal.lengthSq() < 1e-8) {
normal.set(0, 1, 0);
normal.addScaledVector(tangent, -normal.dot(tangent));
}
if (normal.lengthSq() < 1e-8) {
normal.set(0, 0, 1);
normal.addScaledVector(tangent, -normal.dot(tangent));
}
normal.normalize();
const binormal = new THREE.Vector3()
.crossVectors(tangent, normal)
.normalize();
for (let j = 0; j < radialSegments; j++) {
const angle = (j / radialSegments) * Math.PI * 2;
const offset = normal
.clone()
.multiplyScalar(Math.cos(angle) * radius)
.addScaledVector(binormal, Math.sin(angle) * radius);
positions.push(
point.x + offset.x,
point.y + offset.y,
point.z + offset.z
);
}
}
for (let i = 0; i < points.length - 1; i++) {
for (let j = 0; j < radialSegments; j++) {
const jNext = (j + 1) % radialSegments;
const a = i * radialSegments + j;
const b = i * radialSegments + jNext;
const c = (i + 1) * radialSegments + j;
const d = (i + 1) * radialSegments + jNext;
indices.push(a, b, c, b, d, c);
}
}
const geometry = new THREE.BufferGeometry();
geometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(positions, 3)
);
geometry.setIndex(indices);
geometry.computeVertexNormals();
return geometry;
}
function helixPoint(t) {
const angle = Math.PI * 2 * coilTurns * t;
return new THREE.Vector3(
-coilLength / 2 + coilLength * t,
coilRadius * Math.cos(angle),
coilRadius * Math.sin(angle)
);
}
function helixTangent(t) {
const angle = Math.PI * 2 * coilTurns * t;
return new THREE.Vector3(
coilLength,
-coilRadius * Math.sin(angle) * Math.PI * 2 * coilTurns,
coilRadius * Math.cos(angle) * Math.PI * 2 * coilTurns
).normalize();
}
const helixPoints = [];
for (let i = 0; i <= coilTurns * 24; i++) {
helixPoints.push(helixPoint(i / (coilTurns * 24)));
}
const windingGeometry = buildTubeGeometry(
helixPoints,
0.075,
6,
(point) => new THREE.Vector3(0, point.y, point.z).normalize()
);
const windingMaterial = new THREE.MeshStandardMaterial({
color: 0xe58a32,
roughness: 0.36,
metalness: 0.72,
emissive: 0x321000,
emissiveIntensity: 0.28,
flatShading: true
});
const winding = new THREE.Mesh(windingGeometry, windingMaterial);
winding.castShadow = true;
windingGroup.add(winding);
const leadMaterial = windingMaterial.clone();
for (const side of [-1, 1]) {
const innerX = side * (coilLength / 2);
const outerX = side * 4.58;
const leadPoints = [
new THREE.Vector3(innerX, coilRadius, 0),
new THREE.Vector3(outerX, coilRadius, 0)
];
const lead = new THREE.Mesh(
buildTubeGeometry(
leadPoints,
0.075,
6,
() => new THREE.Vector3(0, 1, 0)
),
leadMaterial
);
lead.castShadow = true;
windingGroup.add(lead);
const terminal = new THREE.Mesh(
new THREE.CylinderGeometry(0.16, 0.16, 0.34, 8),
new THREE.MeshStandardMaterial({
color: 0x4c5b70,
roughness: 0.5,
metalness: 0.6,
flatShading: true
})
);
terminal.rotation.z = Math.PI / 2;
terminal.position.x = outerX;
terminal.castShadow = true;
coreGroup.add(terminal);
}
// ------------------------------------------------------------------
// Arrow construction
// ------------------------------------------------------------------
const localUp = new THREE.Vector3(0, 1, 0);
function createArrowMesh(
color,
length = 0.8,
radius = 0.12,
opacity = 1
) {
const group = new THREE.Group();
const material = new THREE.MeshBasicMaterial({
color,
transparent: opacity < 1,
opacity,
depthWrite: opacity >= 1,
toneMapped: false
});
const shaft = new THREE.Mesh(
new THREE.CylinderGeometry(
radius * 0.38,
radius * 0.38,
length * 0.58,
6
),
material
);
shaft.position.y = length * 0.29;
const head = new THREE.Mesh(
new THREE.ConeGeometry(radius, length * 0.42, 6),
material
);
head.position.y = length * 0.79;
group.add(shaft, head);
group.userData.material = material;
return group;
}
function orientArrow(group, direction) {
group.quaternion.setFromUnitVectors(
localUp,
direction.clone().normalize()
);
}
// Current arrows follow increasing helix parameter t.
for (let turn = 0; turn < coilTurns; turn += 1.25) {
const t = (turn + 0.22) / coilTurns;
const point = helixPoint(t);
const radial = new THREE.Vector3(0, point.y, point.z).normalize();
point.addScaledVector(radial, 0.045);
const arrow = createArrowMesh(0xffb13d, 0.46, 0.13);
arrow.position.copy(point);
orientArrow(arrow, helixTangent(t));
currentGroup.add(arrow);
}
// A small moving marker reinforces the current direction.
const currentMarker = new THREE.Mesh(
new THREE.SphereGeometry(0.115, 8, 6),
new THREE.MeshBasicMaterial({
color: 0xffd37a,
toneMapped: false
})
);
currentGroup.add(currentMarker);
// ------------------------------------------------------------------
// Internal axial magnetic field
// ------------------------------------------------------------------
const fieldVolume = new THREE.Mesh(
new THREE.CylinderGeometry(0.93, 0.93, 6.35, 16, 1, true),
new THREE.MeshBasicMaterial({
color: 0x36cfff,
transparent: true,
opacity: 0.045,
side: THREE.DoubleSide,
depthWrite: false,
toneMapped: false
})
);
fieldVolume.rotation.z = Math.PI / 2;
fieldGroup.add(fieldVolume);
const axialLine = new THREE.Mesh(
new THREE.CylinderGeometry(0.018, 0.018, 6.3, 6),
new THREE.MeshBasicMaterial({
color: 0x73e8ff,
transparent: true,
opacity: 0.44,
toneMapped: false
})
);
axialLine.rotation.z = Math.PI / 2;
fieldGroup.add(axialLine);
const fieldArrows = [];
for (const x of [-2.7, -1.35, 0, 1.35, 2.7]) {
const arrow = createArrowMesh(0x69e4ff, 1.0, 0.145, 0.88);
arrow.position.set(x, 0, 0);
orientArrow(arrow, new THREE.Vector3(1, 0, 0));
fieldGroup.add(arrow);
fieldArrows.push(arrow);
}
// ------------------------------------------------------------------
// Qualitative external return / fringing paths
// ------------------------------------------------------------------
function createReturnCurve(zOffset, sign) {
return new THREE.CatmullRomCurve3(
[
new THREE.Vector3(3.48, sign * 1.28, zOffset),
new THREE.Vector3(4.25, sign * 1.72, zOffset),
new THREE.Vector3(3.0, sign * 2.62, zOffset),
new THREE.Vector3(1.2, sign * 2.94, zOffset),
new THREE.Vector3(-1.2, sign * 2.94, zOffset),
new THREE.Vector3(-3.0, sign * 2.62, zOffset),
new THREE.Vector3(-4.25, sign * 1.72, zOffset),
new THREE.Vector3(-3.48, sign * 1.28, zOffset)
],
false,
"catmullrom",
0.45
);
}
function addReturnPath(curve, zOffset) {
const points = curve.getPoints(120);
const geometry = new THREE.BufferGeometry().setFromPoints(points);
const material = new THREE.LineDashedMaterial({
color: 0x5bc9e8,
transparent: true,
opacity: 0.34,
dashSize: 0.22,
gapSize: 0.16,
toneMapped: false
});
const line = new THREE.Line(geometry, material);
line.computeLineDistances();
returnGroup.add(line);
for (const t of [0.27, 0.69]) {
const arrow = createArrowMesh(0x6ad6ef, 0.62, 0.12, 0.58);
arrow.position.copy(curve.getPoint(t));
orientArrow(arrow, curve.getTangent(t));
returnGroup.add(arrow);
}
}
addReturnPath(createReturnCurve(0.42, 1), 0.42);
addReturnPath(createReturnCurve(-0.42, -1), -0.42);
// ------------------------------------------------------------------
// N/S badges
// ------------------------------------------------------------------
function createPoleBadge(letter, color) {
const badgeCanvas = document.createElement("canvas");
badgeCanvas.width = 128;
badgeCanvas.height = 128;
const context = badgeCanvas.getContext("2d");
context.beginPath();
context.arc(64, 64, 49, 0, Math.PI * 2);
context.fillStyle = "rgba(8, 11, 18, 0.9)";
context.fill();
context.lineWidth = 6;
context.strokeStyle = color;
context.stroke();
context.fillStyle = color;
context.font = "bold 58px system-ui";
context.textAlign = "center";
context.textBaseline = "middle";
context.fillText(letter, 64, 67);
const texture = new THREE.CanvasTexture(badgeCanvas);
texture.colorSpace = THREE.SRGBColorSpace;
const sprite = new THREE.Sprite(
new THREE.SpriteMaterial({
map: texture,
transparent: true,
depthTest: false,
depthWrite: false
})
);
sprite.scale.set(0.68, 0.68, 1);
return sprite;
}
const southBadge = createPoleBadge("S", "#ff7b8d");
southBadge.position.set(-3.85, 1.82, 0.3);
coreGroup.add(southBadge);
const northBadge = createPoleBadge("N", "#74e0ae");
northBadge.position.set(3.85, 1.82, 0.3);
coreGroup.add(northBadge);
// ------------------------------------------------------------------
// Custom camera controls — no OrbitControls or addons
// ------------------------------------------------------------------
const target = new THREE.Vector3(0, 0.05, 0);
const initialCamera = {
theta: 0.82,
phi: 1.13,
radius: 11.6
};
let cameraTheta = initialCamera.theta;
let cameraPhi = initialCamera.phi;
let cameraRadius = initialCamera.radius;
let dragging = false;
let previousX = 0;
let previousY = 0;
function updateCamera() {
const sinPhi = Math.sin(cameraPhi);
camera.position.set(
target.x + cameraRadius * sinPhi * Math.cos(cameraTheta),
target.y + cameraRadius * Math.cos(cameraPhi),
target.z + cameraRadius * sinPhi * Math.sin(cameraTheta)
);
camera.lookAt(target);
}
function resetCamera() {
cameraTheta = initialCamera.theta;
cameraPhi = initialCamera.phi;
cameraRadius = initialCamera.radius;
updateCamera();
}
canvas.addEventListener("pointerdown", (event) => {
dragging = true;
previousX = event.clientX;
previousY = event.clientY;
canvas.setPointerCapture(event.pointerId);
canvas.classList.add("grabbing");
});
canvas.addEventListener("pointermove", (event) => {
if (!dragging) return;
const deltaX = event.clientX - previousX;
const deltaY = event.clientY - previousY;
previousX = event.clientX;
previousY = event.clientY;
cameraTheta -= deltaX * 0.006;
cameraPhi = THREE.MathUtils.clamp(
cameraPhi - deltaY * 0.006,
0.22,
Math.PI - 0.22
);
updateCamera();
});
function endDrag(event) {
dragging = false;
canvas.classList.remove("grabbing");
if (
event.pointerId !== undefined &&
canvas.hasPointerCapture(event.pointerId)
) {
canvas.releasePointerCapture(event.pointerId);
}
}
canvas.addEventListener("pointerup", endDrag);
canvas.addEventListener("pointercancel", endDrag);
canvas.addEventListener(
"wheel",
(event) => {
event.preventDefault();
cameraRadius = THREE.MathUtils.clamp(
cameraRadius * Math.exp(event.deltaY * 0.001),
6.2,
21
);
updateCamera();
},
{ passive: false }
);
canvas.addEventListener("dblclick", resetCamera);
canvas.addEventListener("contextmenu", (event) => event.preventDefault());
window.addEventListener("keydown", (event) => {
if (event.key.toLowerCase() === "r") resetCamera();
});
document.getElementById("reset-view").addEventListener("click", resetCamera);
// ------------------------------------------------------------------
// Layer controls
// ------------------------------------------------------------------
document.getElementById("toggle-current").addEventListener("change", (event) => {
currentGroup.visible = event.target.checked;
});
document.getElementById("toggle-field").addEventListener("change", (event) => {
fieldGroup.visible = event.target.checked;
});
document.getElementById("toggle-return").addEventListener("change", (event) => {
returnGroup.visible = event.target.checked;
});
let automaticOrbit = false;
document.getElementById("toggle-orbit").addEventListener("change", (event) => {
automaticOrbit = event.target.checked;
});
// ------------------------------------------------------------------
// Animation and 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);
const clock = new THREE.Clock();
function animate() {
requestAnimationFrame(animate);
const delta = Math.min(clock.getDelta(), 0.05);
const elapsed = clock.elapsedTime;
if (automaticOrbit && !dragging) {
cameraTheta += delta * 0.12;
updateCamera();
}
if (currentGroup.visible) {
const t = (elapsed * 0.105) % 1;
const point = helixPoint(t);
const radial = new THREE.Vector3(0, point.y, point.z).normalize();
point.addScaledVector(radial, 0.12);
currentMarker.position.copy(point);
}
fieldArrows.forEach((arrow, index) => {
arrow.userData.material.opacity =
0.76 + Math.sin(elapsed * 2.2 - index * 0.58) * 0.16;
});
renderer.render(scene, camera);
}
updateCamera();
resize();
loading.remove();
animate();
</script>
</body>
</html>
```