Prompt specification
Build or customize the Three.js 3D scene "Turn-density comparison coils" 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: Turn-density comparison coils
Category: Science & Physics · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/turn-density-comparison-coils-x1670
Scene Description Prompt:
Please create one self-contained HTML file with Three.js from cdnjs showing a solenoid and its magnetic field. Place equal-length coils with different turn counts carrying the same current; compare ideal internal field strengths while stating that finite-end effects are omitted. Stylized solid forms.
Working Three.js Starter (turn-density-comparison-coils-x1670):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Ideal Solenoid Field Comparison</title>
<style>
:root {
color-scheme: dark;
--bg: #07111f;
--panel: rgba(10, 23, 39, 0.82);
--panel-border: rgba(155, 201, 233, 0.16);
--text: #eef7ff;
--muted: #9db1c6;
--copper: #ee9252;
--field: #52dcff;
--field-bright: #b6f8ff;
--current: #ffd166;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: var(--bg);
font-family:
Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
color: var(--text);
}
body {
background:
radial-gradient(circle at 50% 28%, rgba(31, 83, 118, 0.22), transparent 42%),
linear-gradient(160deg, #07111f 0%, #091728 52%, #050c16 100%);
}
#scene {
position: fixed;
inset: 0;
}
#scene canvas {
display: block;
width: 100%;
height: 100%;
touch-action: none;
cursor: grab;
}
#scene canvas:active {
cursor: grabbing;
}
#labels {
position: fixed;
inset: 0;
z-index: 2;
overflow: hidden;
pointer-events: none;
}
.scene-label {
position: absolute;
left: 0;
top: 0;
padding: 8px 13px;
border: 1px solid rgba(116, 221, 255, 0.25);
border-radius: 999px;
background: rgba(5, 16, 29, 0.78);
color: #dff8ff;
font-size: 11px;
font-weight: 800;
letter-spacing: 0.13em;
white-space: nowrap;
box-shadow: 0 10px 30px rgba(0, 0, 0, 0.24);
transform: translate(-50%, -50%);
backdrop-filter: blur(8px);
}
.scene-label.dimension {
padding: 5px 9px;
border-color: rgba(164, 187, 211, 0.18);
color: #aebfd1;
font-size: 10px;
letter-spacing: 0.06em;
}
#ui {
position: fixed;
inset: 0;
z-index: 3;
pointer-events: none;
}
.panel {
border: 1px solid var(--panel-border);
background: var(--panel);
box-shadow: 0 18px 55px rgba(0, 0, 0, 0.24);
backdrop-filter: blur(14px);
}
.intro {
position: absolute;
top: 24px;
left: 24px;
width: min(420px, calc(100vw - 48px));
padding: 22px 24px 20px;
border-radius: 22px;
}
.eyebrow {
margin-bottom: 10px;
color: #73dfff;
font-size: 10px;
font-weight: 850;
letter-spacing: 0.2em;
text-transform: uppercase;
}
h1 {
margin: 0;
font-size: clamp(30px, 4vw, 52px);
font-weight: 750;
line-height: 0.98;
letter-spacing: -0.045em;
}
h1 em {
color: var(--field);
font-style: normal;
}
.intro p {
max-width: 355px;
margin: 15px 0 0;
color: var(--muted);
font-size: 13px;
line-height: 1.55;
}
.formula {
display: inline-block;
margin-top: 14px;
padding: 8px 11px;
border: 1px solid rgba(82, 220, 255, 0.2);
border-radius: 10px;
background: rgba(82, 220, 255, 0.07);
color: #d9f8ff;
font-family: Georgia, "Times New Roman", serif;
font-size: 17px;
}
.assumption {
display: flex;
align-items: center;
gap: 8px;
margin-top: 14px;
color: #ffd69a;
font-size: 10px;
font-weight: 800;
letter-spacing: 0.1em;
text-transform: uppercase;
}
.assumption::before {
width: 7px;
height: 7px;
border-radius: 50%;
background: #ffbd62;
box-shadow: 0 0 13px rgba(255, 189, 98, 0.8);
content: "";
}
.controls-panel {
position: absolute;
top: 24px;
right: 24px;
width: 246px;
padding: 16px;
border-radius: 18px;
pointer-events: auto;
}
.inputs {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 8px;
margin-bottom: 13px;
}
.input {
padding: 10px;
border: 1px solid rgba(154, 193, 220, 0.12);
border-radius: 11px;
background: rgba(255, 255, 255, 0.025);
}
.input span {
display: block;
margin-bottom: 4px;
color: #8299af;
font-size: 9px;
font-weight: 800;
letter-spacing: 0.12em;
text-transform: uppercase;
}
.input b {
font-size: 15px;
font-variant-numeric: tabular-nums;
}
.legend {
display: grid;
gap: 8px;
margin-bottom: 14px;
color: #a9bacb;
font-size: 11px;
}
.legend-row {
display: flex;
align-items: center;
gap: 9px;
}
.swatch {
width: 21px;
height: 5px;
border-radius: 999px;
background: var(--copper);
box-shadow: 0 0 10px rgba(238, 146, 82, 0.45);
}
.swatch.field {
background: var(--field);
box-shadow: 0 0 11px rgba(82, 220, 255, 0.75);
}
.swatch.current {
width: 9px;
height: 9px;
border-radius: 50%;
background: var(--current);
box-shadow: 0 0 11px rgba(255, 209, 102, 0.7);
}
.button-row {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 8px;
}
button {
min-height: 36px;
border: 1px solid rgba(137, 190, 225, 0.2);
border-radius: 10px;
background: rgba(83, 135, 174, 0.1);
color: #d9edf8;
font: inherit;
font-size: 10px;
font-weight: 800;
letter-spacing: 0.04em;
cursor: pointer;
transition:
background 160ms ease,
border-color 160ms ease,
transform 160ms ease;
}
button:hover {
border-color: rgba(111, 220, 255, 0.48);
background: rgba(83, 135, 174, 0.2);
}
button:active {
transform: translateY(1px);
}
button[aria-pressed="true"] {
border-color: rgba(82, 220, 255, 0.55);
background: rgba(82, 220, 255, 0.15);
color: #d8fbff;
}
.comparison {
position: absolute;
left: 50%;
bottom: 20px;
display: grid;
grid-template-columns: minmax(0, 1fr) 52px minmax(0, 1fr);
align-items: stretch;
width: min(760px, calc(100vw - 32px));
transform: translateX(-50%);
}
.coil-card {
min-width: 0;
padding: 15px 17px 13px;
border-radius: 18px;
}
.card-head {
display: flex;
align-items: center;
justify-content: space-between;
gap: 12px;
color: #a8bbcd;
font-size: 11px;
font-weight: 800;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.letter {
display: grid;
width: 25px;
height: 25px;
place-items: center;
border: 1px solid rgba(82, 220, 255, 0.24);
border-radius: 8px;
background: rgba(82, 220, 255, 0.09);
color: var(--field);
}
.reading {
display: flex;
align-items: baseline;
gap: 7px;
margin-top: 3px;
}
.reading strong {
font-size: clamp(24px, 4vw, 34px);
font-weight: 760;
letter-spacing: -0.04em;
font-variant-numeric: tabular-nums;
}
.reading span {
color: #7fdfff;
font-size: 13px;
font-weight: 750;
}
.meter {
height: 5px;
margin: 9px 0 10px;
overflow: hidden;
border-radius: 999px;
background: rgba(255, 255, 255, 0.07);
}
.meter i {
display: block;
width: 50%;
height: 100%;
border-radius: inherit;
background: linear-gradient(90deg, #269fc7, #69eaff);
box-shadow: 0 0 13px rgba(82, 220, 255, 0.65);
}
.card-detail {
display: flex;
justify-content: space-between;
gap: 12px;
color: #8299af;
font-size: 10px;
}
.card-detail b {
color: #c6d8e8;
font-variant-numeric: tabular-nums;
}
.ratio {
display: grid;
place-items: center;
color: #e8fbff;
font-size: 19px;
font-weight: 850;
text-shadow: 0 0 18px rgba(82, 220, 255, 0.5);
}
.hint {
position: absolute;
left: 50%;
bottom: 184px;
padding: 7px 11px;
border-radius: 999px;
background: rgba(4, 12, 22, 0.54);
color: #8fa6bb;
font-size: 10px;
letter-spacing: 0.04em;
transform: translateX(-50%);
backdrop-filter: blur(7px);
}
.note {
position: absolute;
right: 24px;
bottom: 24px;
max-width: 270px;
color: #70889e;
font-size: 10px;
line-height: 1.45;
text-align: right;
}
@media (max-width: 880px) {
.intro {
width: min(365px, calc(100vw - 32px));
top: 16px;
left: 16px;
padding: 18px;
}
.controls-panel {
top: auto;
right: 16px;
bottom: 190px;
width: 220px;
}
.note {
display: none;
}
.hint {
left: 16px;
bottom: 196px;
transform: none;
}
}
@media (max-width: 620px) {
.intro {
width: calc(100vw - 32px);
}
.intro p,
.formula {
display: none;
}
.assumption {
margin-top: 11px;
font-size: 9px;
}
.controls-panel {
right: 16px;
bottom: 170px;
width: 192px;
padding: 12px;
}
.legend {
margin-bottom: 10px;
}
.comparison {
bottom: 12px;
grid-template-columns: 1fr 36px 1fr;
width: calc(100vw - 24px);
}
.coil-card {
padding: 12px 11px;
}
.card-head {
font-size: 9px;
}
.letter {
width: 21px;
height: 21px;
}
.reading strong {
font-size: 23px;
}
.card-detail {
display: block;
}
.card-detail b {
display: block;
margin-top: 3px;
}
.hint {
display: none;
}
.ratio {
font-size: 15px;
}
}
</style>
</head>
<body>
<div id="scene" aria-label="Interactive 3D comparison of two ideal solenoids"></div>
<div id="labels" aria-hidden="true"></div>
<main id="ui">
<section class="intro panel">
<div class="eyebrow">Magnetic field comparison</div>
<h1>Same current.<br><em>Twice the turns.</em></h1>
<p>
Two equal-length coils carry the same current. For an ideal long
solenoid, the internal field grows with the number of turns.
</p>
<div class="formula">B = μ₀ (N / L) I</div>
<div class="assumption">Finite-end effects omitted</div>
</section>
<aside class="controls-panel panel interactive">
<div class="inputs">
<div class="input">
<span>Current</span>
<b>1.20 A</b>
</div>
<div class="input">
<span>Coil length</span>
<b>0.24 m</b>
</div>
</div>
<div class="legend">
<div class="legend-row">
<i class="swatch"></i>
Solid copper winding
</div>
<div class="legend-row">
<i class="swatch field"></i>
Internal magnetic field
</div>
<div class="legend-row">
<i class="swatch current"></i>
Current direction / right-hand rule
</div>
</div>
<div class="button-row">
<button id="orbitButton" type="button" aria-pressed="false">
Auto orbit
</button>
<button id="resetButton" type="button">
Reset view
</button>
</div>
</aside>
<div class="hint">Drag to orbit · Scroll or pinch to zoom</div>
<section class="comparison" aria-label="Ideal internal field strength comparison">
<article class="coil-card panel">
<div class="card-head">
<span class="letter">A</span>
<span>12 turns</span>
</div>
<div class="reading">
<strong id="fieldA">75.4</strong>
<span>μT internal</span>
</div>
<div class="meter"><i id="meterA"></i></div>
<div class="card-detail">
<span>Turn density N/L</span>
<b>50 m⁻¹</b>
</div>
</article>
<div class="ratio">×2</div>
<article class="coil-card panel">
<div class="card-head">
<span class="letter">B</span>
<span>24 turns</span>
</div>
<div class="reading">
<strong id="fieldB">150.8</strong>
<span>μT internal</span>
</div>
<div class="meter"><i id="meterB"></i></div>
<div class="card-detail">
<span>Turn density N/L</span>
<b>100 m⁻¹</b>
</div>
</article>
</section>
<div class="note">
Ideal long-solenoid approximation. Only the internal field is visualized;
fringing and exterior finite-end fields are omitted. Display scale and
field-line thickness are schematic.
</div>
</main>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// -------------------------------------------------------------------------
// Physical values and scene scale
// -------------------------------------------------------------------------
const MU_0 = 4 * Math.PI * 1e-7;
const CURRENT = 1.20; // amperes
const LENGTH_METERS = 0.24; // meters
const COIL_LENGTH = 4.60; // schematic scene units
const COIL_RADIUS = 0.88;
const WIRE_RADIUS = 0.069;
const BOTTOM_Y = 0.72;
const specifications = [
{
id: "A",
turns: 12,
x: -2.75,
strengthRatio: 0.5
},
{
id: "B",
turns: 24,
x: 2.75,
strengthRatio: 1.0
}
];
for (const spec of specifications) {
spec.turnDensity = spec.turns / LENGTH_METERS;
spec.fieldTesla =
MU_0 * spec.turnDensity * CURRENT;
spec.fieldMicroTesla =
spec.fieldTesla * 1e6;
}
document.getElementById("fieldA").textContent =
specifications[0].fieldMicroTesla.toFixed(1);
document.getElementById("fieldB").textContent =
specifications[1].fieldMicroTesla.toFixed(1);
document.getElementById("meterA").style.width = "50%";
document.getElementById("meterB").style.width = "100%";
// -------------------------------------------------------------------------
// Renderer, scene, lights, and camera
// -------------------------------------------------------------------------
const container = document.getElementById("scene");
const labelLayer = document.getElementById("labels");
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x07111f);
scene.fog = new THREE.Fog(0x07111f, 16, 34);
const renderer = new THREE.WebGLRenderer({
antialias: true,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.14;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
container.appendChild(renderer.domElement);
const camera = new THREE.PerspectiveCamera(
42,
window.innerWidth / window.innerHeight,
0.1,
80
);
const cameraTarget = new THREE.Vector3(0, 2.85, 0);
let cameraRadius = window.innerWidth < 620 ? 22.5 : 15.5;
let cameraTheta = 0.22;
let cameraPhi = 1.35;
let autoOrbit = false;
function updateCamera() {
const sinPhi = Math.sin(cameraPhi);
camera.position.set(
cameraTarget.x + cameraRadius * sinPhi * Math.sin(cameraTheta),
cameraTarget.y + cameraRadius * Math.cos(cameraPhi),
cameraTarget.z + cameraRadius * sinPhi * Math.cos(cameraTheta)
);
camera.lookAt(cameraTarget);
}
updateCamera();
const hemisphere = new THREE.HemisphereLight(
0xc8eaff,
0x07101c,
1.8
);
scene.add(hemisphere);
const keyLight = new THREE.DirectionalLight(0xfff0dc, 3.2);
keyLight.position.set(4.5, 10, 7);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(2048, 2048);
keyLight.shadow.camera.left = -11;
keyLight.shadow.camera.right = 11;
keyLight.shadow.camera.top = 11;
keyLight.shadow.camera.bottom = -6;
keyLight.shadow.camera.near = 0.5;
keyLight.shadow.camera.far = 30;
keyLight.shadow.bias = -0.0002;
scene.add(keyLight);
const blueRim = new THREE.DirectionalLight(0x4bc8ff, 1.35);
blueRim.position.set(-7, 5, -6);
scene.add(blueRim);
const warmRim = new THREE.DirectionalLight(0xff9a55, 0.8);
warmRim.position.set(7, 3, -4);
scene.add(warmRim);
// -------------------------------------------------------------------------
// Ground and shared materials
// -------------------------------------------------------------------------
const floor = new THREE.Mesh(
new THREE.PlaneGeometry(42, 32),
new THREE.MeshStandardMaterial({
color: 0x081421,
roughness: 0.92,
metalness: 0.08
})
);
floor.rotation.x = -Math.PI / 2;
floor.position.y = -0.015;
floor.receiveShadow = true;
scene.add(floor);
const grid = new THREE.GridHelper(34, 34, 0x1b4862, 0x102c40);
grid.position.y = 0.004;
grid.material.transparent = true;
grid.material.opacity = 0.24;
grid.material.depthWrite = false;
scene.add(grid);
const copperMaterial = new THREE.MeshStandardMaterial({
color: 0xe98b48,
metalness: 0.72,
roughness: 0.27,
emissive: 0x3b1607,
emissiveIntensity: 0.18
});
const baseMaterial = new THREE.MeshStandardMaterial({
color: 0x162638,
metalness: 0.72,
roughness: 0.36
});
const baseTopMaterial = new THREE.MeshStandardMaterial({
color: 0x203a4e,
metalness: 0.66,
roughness: 0.32
});
const currentMaterial = new THREE.MeshBasicMaterial({
color: 0xffd166
});
const fieldColor = 0x52dcff;
// -------------------------------------------------------------------------
// Scene labels projected from 3D anchors
// -------------------------------------------------------------------------
const projectedLabels = [];
function createSceneLabel(text, anchor, className = "") {
const element = document.createElement("div");
element.className = `scene-label ${className}`.trim();
element.textContent = text;
labelLayer.appendChild(element);
projectedLabels.push({
element,
anchor: anchor.clone()
});
return element;
}
function updateProjectedLabels() {
const width = window.innerWidth;
const height = window.innerHeight;
for (const item of projectedLabels) {
const projected = item.anchor.clone().project(camera);
const visible =
projected.z > -1 &&
projected.z < 1 &&
projected.x > -1.25 &&
projected.x < 1.25 &&
projected.y > -1.25 &&
projected.y < 1.25;
item.element.style.display = visible ? "block" : "none";
if (!visible) continue;
const x = (projected.x * 0.5 + 0.5) * width;
const y = (-projected.y * 0.5 + 0.5) * height;
item.element.style.left = `${x}px`;
item.element.style.top = `${y}px`;
}
}
// -------------------------------------------------------------------------
// Geometry helpers
// -------------------------------------------------------------------------
function makeHelixCurve(turns) {
const points = [];
const pointCount = turns * 40;
for (let i = 0; i <= pointCount; i++) {
const t = i / pointCount;
const angle = -Math.PI * 2 * turns * t;
points.push(
new THREE.Vector3(
COIL_RADIUS * Math.cos(angle),
BOTTOM_Y + COIL_LENGTH * t,
-COIL_RADIUS * Math.sin(angle)
)
);
}
return new THREE.CatmullRomCurve3(
points,
false,
"catmullrom",
0.12
);
}
function makeArrow(material, shaftRadius, shaftLength, coneRadius, coneLength) {
const arrow = new THREE.Group();
const shaft = new THREE.Mesh(
new THREE.CylinderGeometry(
shaftRadius,
shaftRadius,
shaftLength,
10
),
material
);
const cone = new THREE.Mesh(
new THREE.ConeGeometry(
coneRadius,
coneLength,
12
),
material
);
cone.position.y = shaftLength * 0.5 + coneLength * 0.5;
arrow.add(shaft, cone);
return arrow;
}
function makePedestal(x, strengthRatio) {
const group = new THREE.Group();
group.position.x = x;
const base = new THREE.Mesh(
new THREE.CylinderGeometry(1.24, 1.34, 0.28, 48),
baseMaterial
);
base.position.y = 0.14;
base.castShadow = true;
base.receiveShadow = true;
group.add(base);
const top = new THREE.Mesh(
new THREE.CylinderGeometry(1.08, 1.18, 0.09, 48),
baseTopMaterial
);
top.position.y = 0.325;
top.castShadow = true;
top.receiveShadow = true;
group.add(top);
// Strength gauge mounted on the front of the pedestal.
const gaugeBack = new THREE.Mesh(
new THREE.BoxGeometry(1.72, 0.13, 0.09),
new THREE.MeshStandardMaterial({
color: 0x07121e,
roughness: 0.7,
metalness: 0.3
})
);
gaugeBack.position.set(0, 0.43, 1.19);
group.add(gaugeBack);
const gaugeWidth = 1.55 * strengthRatio;
const gauge = new THREE.Mesh(
new THREE.BoxGeometry(gaugeWidth, 0.085, 0.105),
new THREE.MeshStandardMaterial({
color: fieldColor,
emissive: fieldColor,
emissiveIntensity: 0.75 + strengthRatio * 0.85,
roughness: 0.28,
metalness: 0.12
})
);
gauge.position.set(
-1.55 * 0.5 + gaugeWidth * 0.5,
0.43,
1.245
);
group.add(gauge);
return group;
}
function makeDimensionBracket(x) {
const bracketX = x + 1.12;
const z = 0.73;
const tick = 0.13;
const positions = [
bracketX, BOTTOM_Y, z,
bracketX, BOTTOM_Y + COIL_LENGTH, z,
bracketX - tick, BOTTOM_Y, z,
bracketX + tick, BOTTOM_Y, z,
bracketX - tick, BOTTOM_Y + COIL_LENGTH, z,
bracketX + tick, BOTTOM_Y + COIL_LENGTH, z,
bracketX, BOTTOM_Y, z,
x + COIL_RADIUS, BOTTOM_Y, z,
bracketX, BOTTOM_Y + COIL_LENGTH, z,
x + COIL_RADIUS, BOTTOM_Y + COIL_LENGTH, z
];
const geometry = new THREE.BufferGeometry();
geometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(positions, 3)
);
const lines = new THREE.LineSegments(
geometry,
new THREE.LineBasicMaterial({
color: 0x8ea8bd,
transparent: true,
opacity: 0.62
})
);
scene.add(lines);
createSceneLabel(
"L = 0.24 m",
new THREE.Vector3(bracketX + 0.18, BOTTOM_Y + COIL_LENGTH * 0.5, z),
"dimension"
);
}
// -------------------------------------------------------------------------
// Solenoids
// -------------------------------------------------------------------------
const fieldPulses = [];
for (const spec of specifications) {
const group = new THREE.Group();
group.position.x = spec.x;
scene.add(group);
scene.add(makePedestal(spec.x, spec.strengthRatio));
makeDimensionBracket(spec.x);
const curve = makeHelixCurve(spec.turns);
const winding = new THREE.Mesh(
new THREE.TubeGeometry(
curve,
spec.turns * 32,
WIRE_RADIUS,
10,
false
),
copperMaterial
);
winding.castShadow = true;
winding.receiveShadow = true;
group.add(winding);
// A short lead connects the winding to the base.
const lead = new THREE.Mesh(
new THREE.CylinderGeometry(0.058, 0.058, 0.37, 12),
copperMaterial
);
lead.position.set(COIL_RADIUS, 0.505, 0);
lead.castShadow = true;
group.add(lead);
const terminal = new THREE.Mesh(
new THREE.SphereGeometry(0.082, 16, 12),
copperMaterial
);
terminal.position.set(COIL_RADIUS, 0.34, 0);
group.add(terminal);
// Gold arrows show current circulation around the windings.
for (const t of [0.17, 0.58]) {
const point = curve.getPointAt(t);
const tangent = curve.getTangentAt(t).normalize();
const radial = new THREE.Vector3(point.x, 0, point.z).normalize();
point.addScaledVector(radial, 0.10);
const currentArrow = makeArrow(
currentMaterial,
0.032,
0.24,
0.105,
0.21
);
currentArrow.position.copy(point);
currentArrow.quaternion.setFromUnitVectors(
new THREE.Vector3(0, 1, 0),
tangent
);
group.add(currentArrow);
}
// Translucent volume indicates the ideal internal-field region.
const envelopeMaterial = new THREE.MeshBasicMaterial({
color: fieldColor,
transparent: true,
opacity: spec.strengthRatio === 1 ? 0.085 : 0.04,
depthWrite: false,
side: THREE.DoubleSide,
blending: THREE.AdditiveBlending
});
const envelope = new THREE.Mesh(
new THREE.CylinderGeometry(
0.50,
0.50,
COIL_LENGTH * 0.92,
32,
1,
true
),
envelopeMaterial
);
envelope.position.y = BOTTOM_Y + COIL_LENGTH * 0.5;
group.add(envelope);
// Internal field is shown as upward solid arrows.
const arrowMaterial = new THREE.MeshStandardMaterial({
color: fieldColor,
emissive: fieldColor,
emissiveIntensity: spec.strengthRatio === 1 ? 2.0 : 0.9,
metalness: 0.05,
roughness: 0.22
});
const fieldRows = [1.05, 1.98, 2.91, 3.84, 4.77];
const fieldColumns = [-0.28, 0, 0.28];
const shaftRadius =
spec.strengthRatio === 1 ? 0.066 : 0.043;
const coneRadius =
spec.strengthRatio === 1 ? 0.15 : 0.105;
const coneLength =
spec.strengthRatio === 1 ? 0.28 : 0.23;
for (const y of fieldRows) {
for (const offsetX of fieldColumns) {
const fieldArrow = makeArrow(
arrowMaterial,
shaftRadius,
0.34,
coneRadius,
coneLength
);
fieldArrow.position.set(offsetX, y, 0);
group.add(fieldArrow);
}
}
// Moving highlights reinforce the upward field direction.
for (let i = 0; i < 2; i++) {
const pulse = new THREE.Mesh(
new THREE.SphereGeometry(
spec.strengthRatio === 1 ? 0.072 : 0.055,
14,
10
),
new THREE.MeshBasicMaterial({
color: 0xd8fbff,
transparent: true,
opacity: 0.9
})
);
pulse.position.set(0, BOTTOM_Y, 0);
pulse.userData.offset = i * 0.48;
group.add(pulse);
fieldPulses.push(pulse);
}
const fieldLight = new THREE.PointLight(
fieldColor,
spec.strengthRatio === 1 ? 0.85 : 0.34,
3.3,
2
);
fieldLight.position.set(0, 2.9, 0.25);
group.add(fieldLight);
createSceneLabel(
`${spec.id} · ${spec.turns} TURNS`,
new THREE.Vector3(spec.x, BOTTOM_Y + COIL_LENGTH + 0.43, 0)
);
}
// -------------------------------------------------------------------------
// Direct camera controls: orbit, drag, wheel, and touch pinch
// -------------------------------------------------------------------------
const canvas = renderer.domElement;
const pointers = new Map();
let dragging = false;
let previousX = 0;
let previousY = 0;
let pinchDistance = 0;
canvas.addEventListener("pointerdown", (event) => {
pointers.set(event.pointerId, {
x: event.clientX,
y: event.clientY
});
if (pointers.size === 1) {
dragging = true;
previousX = event.clientX;
previousY = event.clientY;
canvas.setPointerCapture(event.pointerId);
} else if (pointers.size === 2) {
const [a, b] = [...pointers.values()];
pinchDistance = Math.hypot(a.x - b.x, a.y - b.y);
}
});
canvas.addEventListener("pointermove", (event) => {
if (!pointers.has(event.pointerId)) return;
pointers.set(event.pointerId, {
x: event.clientX,
y: event.clientY
});
if (pointers.size === 2) {
const [a, b] = [...pointers.values()];
const distance = Math.hypot(a.x - b.x, a.y - b.y);
if (pinchDistance > 0) {
cameraRadius = THREE.MathUtils.clamp(
cameraRadius * (pinchDistance / distance),
8.5,
28
);
}
pinchDistance = distance;
return;
}
if (!dragging) return;
const deltaX = event.clientX - previousX;
const deltaY = event.clientY - previousY;
cameraTheta -= deltaX * 0.005;
cameraPhi = THREE.MathUtils.clamp(
cameraPhi - deltaY * 0.005,
0.62,
1.78
);
previousX = event.clientX;
previousY = event.clientY;
});
function releasePointer(event) {
pointers.delete(event.pointerId);
if (pointers.size === 0) {
dragging = false;
pinchDistance = 0;
}
}
canvas.addEventListener("pointerup", releasePointer);
canvas.addEventListener("pointercancel", releasePointer);
canvas.addEventListener("lostpointercapture", releasePointer);
canvas.addEventListener(
"wheel",
(event) => {
event.preventDefault();
cameraRadius = THREE.MathUtils.clamp(
cameraRadius + event.deltaY * 0.012,
8.5,
28
);
},
{ passive: false }
);
function resetView() {
cameraTheta = 0.22;
cameraPhi = 1.35;
cameraRadius = window.innerWidth < 620 ? 22.5 : 15.5;
cameraTarget.set(0, 2.85, 0);
updateCamera();
}
document.getElementById("resetButton").addEventListener("click", resetView);
const orbitButton = document.getElementById("orbitButton");
orbitButton.addEventListener("click", () => {
autoOrbit = !autoOrbit;
orbitButton.setAttribute("aria-pressed", String(autoOrbit));
orbitButton.textContent = autoOrbit ? "Stop orbit" : "Auto orbit";
});
// -------------------------------------------------------------------------
// Resize and animation
// -------------------------------------------------------------------------
window.addEventListener("resize", () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
});
const clock = new THREE.Clock();
function animate() {
const delta = Math.min(clock.getDelta(), 0.05);
const elapsed = clock.elapsedTime;
if (autoOrbit && !dragging) {
cameraTheta += delta * 0.13;
}
updateCamera();
for (const pulse of fieldPulses) {
const travel = COIL_LENGTH - 0.52;
const progress = (elapsed * 0.58 + pulse.userData.offset) % 1;
pulse.position.y = BOTTOM_Y + 0.26 + progress * travel;
pulse.material.opacity = 0.55 + 0.35 * Math.sin(progress * Math.PI);
}
updateProjectedLabels();
renderer.render(scene, camera);
}
renderer.setAnimationLoop(animate);
</script>
</body>
</html>
```