Prompt specification
Build or customize the Three.js 3D scene "Induction discovery classroom scene" 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: Induction discovery classroom scene
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/induction-discovery-classroom-scene-x1635
Scene Description Prompt:
I'd like to see this as a voxel Three.js scene: induction discovery classroom scene. Compose a hand-cranked magnet slider, coil, and waveform display; connect direction reversals to voltage sign changes while labeling the field model approximate. Use cdnjs and return one self-contained HTML file.
Working Three.js Starter (induction-discovery-classroom-scene-x1635):
```html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Voxel Induction Discovery Lab</title>
<style>
:root {
--ink: #e9fff7;
--muted: #9fc6bd;
--panel: rgba(8, 25, 29, 0.88);
--line: rgba(126, 255, 220, 0.22);
--mint: #72f6c5;
--amber: #ffc857;
--red: #ff6b6b;
}
* { box-sizing: border-box; }
html, body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #0b171d;
font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
color: var(--ink);
}
#scene {
position: fixed;
inset: 0;
}
#scene canvas {
display: block;
width: 100%;
height: 100%;
touch-action: none;
cursor: grab;
}
#scene canvas:active { cursor: grabbing; }
.overlay {
position: fixed;
inset: 0;
pointer-events: none;
}
.title-card {
position: absolute;
top: 20px;
left: 22px;
max-width: min(440px, calc(100vw - 44px));
padding: 15px 17px 14px;
border: 1px solid var(--line);
border-radius: 13px;
background: var(--panel);
box-shadow: 0 16px 45px rgba(0, 0, 0, 0.25);
backdrop-filter: blur(12px);
}
.eyebrow {
margin-bottom: 5px;
color: var(--mint);
font: 700 11px/1.2 ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
letter-spacing: 0.16em;
text-transform: uppercase;
}
h1 {
margin: 0;
font-size: clamp(20px, 3vw, 30px);
line-height: 1.05;
letter-spacing: -0.03em;
}
.subtitle {
margin-top: 7px;
color: var(--muted);
font-size: 13px;
line-height: 1.45;
}
.control-panel {
position: absolute;
left: 22px;
bottom: 22px;
width: min(390px, calc(100vw - 44px));
padding: 15px;
border: 1px solid var(--line);
border-radius: 14px;
background: var(--panel);
box-shadow: 0 16px 45px rgba(0, 0, 0, 0.3);
backdrop-filter: blur(12px);
pointer-events: auto;
}
.control-panel h2 {
margin: 0 0 11px;
font-size: 13px;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.readouts {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 8px;
margin-bottom: 11px;
}
.readout {
min-width: 0;
padding: 9px 8px;
border: 1px solid rgba(145, 219, 203, 0.16);
border-radius: 9px;
background: rgba(255, 255, 255, 0.035);
}
.readout span {
display: block;
margin-bottom: 4px;
color: var(--muted);
font: 700 9px/1.2 ui-monospace, SFMono-Regular, Menlo, monospace;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.readout strong {
display: block;
overflow: hidden;
color: var(--amber);
font: 800 15px/1.1 ui-monospace, SFMono-Regular, Menlo, monospace;
text-overflow: ellipsis;
white-space: nowrap;
}
.readout strong.positive { color: var(--mint); }
.readout strong.negative { color: var(--red); }
.buttons {
display: flex;
flex-wrap: wrap;
gap: 8px;
margin-bottom: 12px;
}
button {
appearance: none;
border: 1px solid rgba(114, 246, 197, 0.28);
border-radius: 8px;
padding: 9px 11px;
background: rgba(114, 246, 197, 0.09);
color: var(--ink);
font: 800 10px/1 ui-monospace, SFMono-Regular, Menlo, monospace;
letter-spacing: 0.04em;
cursor: pointer;
transition: transform 0.12s ease, background 0.12s ease;
}
button:hover {
transform: translateY(-1px);
background: rgba(114, 246, 197, 0.17);
}
button:active { transform: translateY(0); }
button.primary {
border-color: rgba(255, 200, 87, 0.48);
background: rgba(255, 200, 87, 0.16);
color: #fff0bd;
}
.slider-label {
display: flex;
justify-content: space-between;
margin-bottom: 7px;
color: var(--muted);
font: 700 10px/1 ui-monospace, SFMono-Regular, Menlo, monospace;
}
input[type="range"] {
width: 100%;
margin: 0 0 11px;
accent-color: var(--mint);
cursor: ew-resize;
}
.hint {
margin: 0;
color: var(--muted);
font-size: 11px;
line-height: 1.45;
}
.model-card {
position: absolute;
top: 20px;
right: 22px;
width: min(330px, calc(100vw - 44px));
padding: 14px 15px;
border: 1px solid rgba(255, 200, 87, 0.25);
border-radius: 13px;
background: var(--panel);
box-shadow: 0 16px 45px rgba(0, 0, 0, 0.25);
backdrop-filter: blur(12px);
}
.model-card h3 {
margin: 0 0 8px;
color: var(--amber);
font: 800 11px/1.3 ui-monospace, SFMono-Regular, Menlo, monospace;
letter-spacing: 0.1em;
text-transform: uppercase;
}
.formula {
margin: 5px 0;
font: 700 14px/1.35 ui-monospace, SFMono-Regular, Menlo, monospace;
}
.model-card p {
margin: 8px 0 0;
color: var(--muted);
font-size: 11px;
line-height: 1.45;
}
.badge {
display: inline-block;
margin-top: 10px;
padding: 5px 7px;
border: 1px solid rgba(255, 200, 87, 0.3);
border-radius: 6px;
color: #ffe39d;
font: 800 9px/1 ui-monospace, SFMono-Regular, Menlo, monospace;
letter-spacing: 0.08em;
}
@media (max-width: 760px) {
.title-card {
top: 12px;
left: 12px;
padding: 12px 13px;
}
.subtitle { display: none; }
.model-card {
top: auto;
right: 12px;
bottom: 12px;
width: 260px;
}
.model-card p { display: none; }
.control-panel {
left: 12px;
bottom: 12px;
width: calc(100vw - 292px);
min-width: 260px;
}
.readouts { grid-template-columns: 1fr 1fr; }
.readout:nth-child(3) { display: none; }
.hint { display: none; }
}
@media (max-width: 560px) {
.model-card { display: none; }
.control-panel {
right: 12px;
width: auto;
min-width: 0;
}
}
</style>
</head>
<body>
<div id="scene"></div>
<div class="overlay">
<section class="title-card">
<div class="eyebrow">Voxel Science Laboratory</div>
<h1>Induction Discovery</h1>
<div class="subtitle">
Turn the crank, move the bar magnet through the coil, and watch induced
voltage change sign when the motion is reversed.
</div>
</section>
<section class="model-card">
<h3>Approximate Field Model</h3>
<div class="formula">B(d) ≈ B₀ / [1 + (d/s)²]</div>
<div class="formula">V ≈ −K · dΦ/dt</div>
<p>
This is a simplified one-dimensional teaching model, not a solved
magnetostatic field. Voltage polarity assumes the winding direction shown.
</p>
<span class="badge">MODEL IS APPROXIMATE</span>
</section>
<section class="control-panel">
<h2>Experiment Controls</h2>
<div class="readouts" aria-live="polite">
<div class="readout">
<span>Induced voltage</span>
<strong id="voltageValue">+0.00 V</strong>
</div>
<div class="readout">
<span>Magnet motion</span>
<strong id="directionValue">STILL</strong>
</div>
<div class="readout">
<span>Relative field</span>
<strong id="fieldValue">0.00</strong>
</div>
</div>
<div class="buttons">
<button id="driveBtn" class="primary">START CRANK</button>
<button id="reverseBtn">REVERSE: OUTWARD</button>
<button id="resetBtn">RESET</button>
</div>
<div class="slider-label">
<span>MANUAL MAGNET POSITION</span>
<span>COIL ← → CRANK</span>
</div>
<input id="positionSlider" type="range" min="-5" max="-1.15" step="0.01" value="-3.45" />
<p class="hint">
Drag the yellow crank handle or red/blue magnet in 3D. Drag the background
to orbit, shift-drag to pan, and use the wheel to zoom.
</p>
</section>
</div>
<script type="module">
import * as THREE from "https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
const sceneRoot = document.getElementById("scene");
const renderer = new THREE.WebGLRenderer({ antialias: true, alpha: false });
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.08;
sceneRoot.appendChild(renderer.domElement);
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x101d24);
scene.fog = new THREE.Fog(0x101d24, 20, 39);
const camera = new THREE.PerspectiveCamera(
45,
window.innerWidth / window.innerHeight,
0.1,
100
);
const state = {
slider: -3.45,
target: -3.45,
velocity: 0,
angle: 0,
running: false,
direction: 1,
field: 0,
slope: 0,
voltage: 0
};
const clamp = THREE.MathUtils.clamp;
const materialCache = new Map();
function standardMaterial(color) {
if (!materialCache.has(color)) {
materialCache.set(
color,
new THREE.MeshStandardMaterial({
color,
roughness: 0.82,
metalness: 0.02,
flatShading: true
})
);
}
return materialCache.get(color);
}
function box(parent, w, h, d, material, x = 0, y = 0, z = 0) {
const mesh = new THREE.Mesh(
new THREE.BoxGeometry(w, h, d),
typeof material === "string" ? standardMaterial(material) : material
);
mesh.position.set(x, y, z);
mesh.castShadow = true;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
function makeLabel(text, x, y, z, width = 2.2, color = "#effff9") {
const canvas = document.createElement("canvas");
canvas.width = 768;
canvas.height = 128;
const ctx = canvas.getContext("2d");
ctx.fillStyle = "rgba(6, 20, 25, 0.86)";
ctx.fillRect(0, 8, canvas.width, 112);
ctx.strokeStyle = "rgba(120, 245, 200, 0.45)";
ctx.lineWidth = 4;
ctx.strokeRect(2, 10, canvas.width - 4, 108);
ctx.fillStyle = color;
ctx.font = "800 37px ui-monospace, Menlo, Consolas, monospace";
ctx.textAlign = "center";
ctx.textBaseline = "middle";
ctx.fillText(text, canvas.width / 2, canvas.height / 2 + 2);
const texture = new THREE.CanvasTexture(canvas);
texture.colorSpace = THREE.SRGBColorSpace;
texture.minFilter = THREE.LinearFilter;
const material = new THREE.SpriteMaterial({
map: texture,
transparent: true,
depthWrite: false
});
const sprite = new THREE.Sprite(material);
sprite.position.set(x, y, z);
sprite.scale.set(width, width * 128 / 768, 1);
scene.add(sprite);
return sprite;
}
function wireBetween(x1, y1, z1, x2, y2, z2, color = 0x17262b) {
const dx = Math.abs(x2 - x1);
const dy = Math.abs(y2 - y1);
const dz = Math.abs(z2 - z1);
if (dx >= dy && dx >= dz) {
return box(scene, dx + 0.05, 0.055, 0.055, color, (x1 + x2) / 2, y1, z1);
}
if (dy >= dx && dy >= dz) {
return box(scene, 0.055, dy + 0.05, 0.055, color, x1, (y1 + y2) / 2, z1);
}
return box(scene, 0.055, 0.055, dz + 0.05, color, x1, y1, (z1 + z2) / 2);
}
function fieldModel(x) {
const d = Math.max(0.25, -x);
const scale = 1.6;
const B0 = 1.6;
const q = d / scale;
const field = B0 / (1 + q * q);
const slope = (2 * B0 * d) /
(scale * scale * Math.pow(1 + q * q, 2));
return { field, slope };
}
// Lighting.
scene.add(new THREE.HemisphereLight(0xccefff, 0x26352f, 2.25));
const keyLight = new THREE.DirectionalLight(0xfff3d6, 3.1);
keyLight.position.set(-6, 12, 8);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(2048, 2048);
keyLight.shadow.camera.left = -13;
keyLight.shadow.camera.right = 13;
keyLight.shadow.camera.top = 12;
keyLight.shadow.camera.bottom = -8;
keyLight.shadow.camera.near = 1;
keyLight.shadow.camera.far = 35;
scene.add(keyLight);
const fillLight = new THREE.DirectionalLight(0x8de8ff, 1.1);
fillLight.position.set(8, 7, 1);
scene.add(fillLight);
// Classroom shell.
box(scene, 18, 0.18, 12, 0x34484a, 0, -0.12, -0.5);
box(scene, 18, 7.5, 0.22, 0x385054, 0, 3.65, -4.35);
box(scene, 0.22, 7.5, 9.5, 0x30474c, -9, 3.65, 0.1);
box(scene, 18, 0.16, 0.32, 0x24383d, 0, 0.18, -4.15);
const gridMaterial = new THREE.MeshBasicMaterial({ color: 0x557174 });
for (let x = -8; x <= 8; x += 1) {
box(scene, 0.018, 0.012, 11.4, gridMaterial, x, -0.018, -0.45);
}
for (let z = -5; z <= 5; z += 1) {
box(scene, 17.5, 0.012, 0.018, gridMaterial, 0, -0.018, z - 0.45);
}
// Window.
box(scene, 2.8, 2.7, 0.16, 0x172c34, -5.7, 4.25, -4.18);
const glass = new THREE.MeshStandardMaterial({
color: 0x77c8df,
emissive: 0x285b6c,
emissiveIntensity: 0.55,
roughness: 0.25,
transparent: true,
opacity: 0.78
});
box(scene, 2.45, 2.35, 0.08, glass, -5.7, 4.25, -4.07);
box(scene, 0.11, 2.45, 0.12, 0x20363c, -5.7, 4.25, -4.0);
box(scene, 2.55, 0.11, 0.12, 0x20363c, -5.7, 4.25, -4.0);
box(scene, 3.1, 0.17, 0.45, 0x725946, -5.7, 2.82, -4.0);
// Chalkboard.
box(scene, 7.8, 3.25, 0.22, 0x745a40, 1.35, 3.75, -4.13);
const boardCanvas = document.createElement("canvas");
boardCanvas.width = 1200;
boardCanvas.height = 500;
const boardCtx = boardCanvas.getContext("2d");
boardCtx.fillStyle = "#173f3b";
boardCtx.fillRect(0, 0, 1200, 500);
boardCtx.strokeStyle = "rgba(225,255,244,0.12)";
boardCtx.lineWidth = 2;
for (let x = 0; x < 1200; x += 60) {
boardCtx.beginPath();
boardCtx.moveTo(x, 0);
boardCtx.lineTo(x, 500);
boardCtx.stroke();
}
for (let y = 0; y < 500; y += 60) {
boardCtx.beginPath();
boardCtx.moveTo(0, y);
boardCtx.lineTo(1200, y);
boardCtx.stroke();
}
boardCtx.fillStyle = "#e6fff5";
boardCtx.font = "800 63px Trebuchet MS, sans-serif";
boardCtx.fillText("INDUCTION DISCOVERY", 55, 88);
boardCtx.fillStyle = "#7dffca";
boardCtx.font = "700 39px ui-monospace, Consolas, monospace";
boardCtx.fillText("B(d) ≈ B₀ / [1 + (d/s)²]", 55, 190);
boardCtx.fillStyle = "#ffe19a";
boardCtx.fillText("V ≈ −K · dΦ/dt", 55, 260);
boardCtx.fillStyle = "#e6fff5";
boardCtx.font = "600 31px Trebuchet MS, sans-serif";
boardCtx.fillText("Motion reversed → voltage sign reverses", 55, 342);
boardCtx.fillStyle = "#ffd78b";
boardCtx.font = "800 27px ui-monospace, Consolas, monospace";
boardCtx.fillText("FIELD MODEL IS APPROXIMATE", 55, 430);
const boardTexture = new THREE.CanvasTexture(boardCanvas);
boardTexture.colorSpace = THREE.SRGBColorSpace;
boardTexture.anisotropy = renderer.capabilities.getMaxAnisotropy();
const board = new THREE.Mesh(
new THREE.PlaneGeometry(7.45, 3.0),
new THREE.MeshBasicMaterial({ map: boardTexture, toneMapped: false })
);
board.position.set(1.35, 3.75, -3.995);
scene.add(board);
// Teacher and student classroom details.
function voxelPerson(x, z, shirt, seated = false) {
const person = new THREE.Group();
person.position.set(x, 0, z);
scene.add(person);
const skin = 0xe9b98f;
box(person, 0.58, 0.72, 0.34, shirt, 0, seated ? 1.15 : 1.25, 0);
box(person, 0.45, 0.43, 0.42, skin, 0, seated ? 1.72 : 2.05, 0);
box(person, 0.49, 0.12, 0.46, 0x3a302c, 0, seated ? 1.92 : 2.28, 0);
if (seated) {
box(person, 0.24, 0.24, 0.8, 0x263a54, -0.16, 0.58, 0.28);
box(person, 0.24, 0.24, 0.8, 0x263a54, 0.16, 0.58, 0.28);
box(person, 0.18, 0.66, 0.18, 0x263a54, -0.36, 0.92, -0.18);
box(person, 0.18, 0.66, 0.18, 0x263a54, 0.36, 0.92, -0.18);
} else {
box(person, 0.24, 0.7, 0.24, 0x263a54, -0.16, 0.45, 0);
box(person, 0.24, 0.7, 0.24, 0x263a54, 0.16, 0.45, 0);
}
box(person, 0.18, 0.7, 0.18, shirt, -0.4, 1.12, 0.05);
box(person, 0.18, 0.7, 0.18, shirt, 0.4, 1.12, 0.05);
return person;
}
box(scene, 2.0, 0.15, 0.9, 0xa27b50, -4.5, 1.0, -2.55);
box(scene, 0.16, 1.0, 0.65, 0x43555a, -5.2, 0.5, -2.55);
box(scene, 0.16, 1.0, 0.65, 0x43555a, -3.8, 0.5, -2.55);
voxelPerson(-4.6, -2.75, 0x477d86, true);
for (const sx of [-7.2, -2.4]) {
box(scene, 1.7, 0.14, 0.8, 0x9b714b, sx, 1.0, -2.7);
box(scene, 0.15, 1.0, 0.6, 0x43555a, sx - 0.6, 0.5, -2.7);
box(scene, 0.15, 1.0, 0.6, 0x43555a, sx + 0.6, 0.5, -2.7);
voxelPerson(sx, -2.9, sx < -5 ? 0xb35f5f : 0x8c6caf, true);
}
// Main experiment bench.
box(scene, 12.4, 0.24, 3.15, 0xa47b50, -0.85, 1.43, 0.2);
box(scene, 12.0, 0.16, 2.82, 0xc59a68, -0.85, 1.59, 0.2);
for (const x of [-6.35, 4.6]) {
for (const z of [-0.9, 1.25]) {
box(scene, 0.28, 1.42, 0.28, 0x4d6263, x, 0.7, z);
}
}
box(scene, 10.8, 0.14, 0.15, 0x4d6263, -0.85, 0.43, 0.2);
// Rail and rack.
for (const z of [-0.28, 0.28]) {
box(scene, 5.05, 0.12, 0.12, 0x34484c, -3.12, 1.75, z);
for (let x = -5.4; x < -0.8; x += 0.62) {
box(scene, 0.18, 0.16, 0.32, 0x596d70, x, 1.66, z);
}
}
box(scene, 4.7, 0.12, 0.14, 0x8f693e, -3.15, 1.73, 0.62);
for (let x = -5.35; x < -0.8; x += 0.23) {
box(scene, 0.10, 0.20, 0.17, 0x725431, x, 1.83, 0.62);
}
// Coil support and voxel copper winding.
box(scene, 2.25, 0.18, 1.35, 0x5e4937, 0, 1.72, 0);
for (const x of [-0.86, 0.86]) {
box(scene, 0.15, 1.18, 0.16, 0x604b38, x, 2.0, -0.53);
box(scene, 0.15, 1.18, 0.16, 0x604b38, x, 2.0, 0.53);
}
const coil = new THREE.Group();
scene.add(coil);
const copper = 0xd7793f;
const turns = 2.65;
const segments = 48;
for (let i = 0; i < segments; i++) {
const t = i / (segments - 1);
const angle = t * Math.PI * 2 * turns;
const x = -0.68 + t * 1.36;
const segment = box(
coil,
0.17,
0.24,
0.12,
copper,
x,
Math.cos(angle) * 0.48,
Math.sin(angle) * 0.48
);
segment.rotation.x = angle + Math.PI / 2;
}
box(coil, 0.18, 0.18, 1.25, 0x57483b, -0.73, 0, 0);
box(coil, 0.18, 0.18, 1.25, 0x57483b, 0.73, 0, 0);
// Magnet slider.
const magnet = new THREE.Group();
magnet.position.set(state.slider, 2.03, 0);
scene.add(magnet);
box(magnet, 0.53, 0.62, 0.62, 0x2779b8, -0.275, 0, 0);
box(magnet, 0.53, 0.62, 0.62, 0xe55252, 0.275, 0, 0);
box(magnet, 0.055, 0.66, 0.66, 0x182b34, 0, 0, 0);
box(magnet, 0.27, 0.12, 0.28, 0xdce9e8, -0.25, 0.38, 0);
box(magnet, 0.27, 0.12, 0.28, 0xdce9e8, 0.25, 0.38, 0);
const nLabel = makeLabel("N", 0, 0, 0, 0.44, "#ff9a9a");
nLabel.position.set(0.27, 0.48, 0.1);
magnet.add(nLabel);
const sLabel = makeLabel("S", 0, 0, 0, 0.44, "#8dd7ff");
sLabel.position.set(-0.27, 0.48, 0.1);
magnet.add(sLabel);
// Small rack pinion attached to the slider.
const sliderGear = new THREE.Group();
sliderGear.position.set(state.slider, 1.83, 0.62);
scene.add(sliderGear);
for (let i = 0; i < 8; i++) {
const a = i * Math.PI / 4;
const tooth = box(
sliderGear,
0.16,
0.18,
0.18,
0xe2ad48,
0,
Math.cos(a) * 0.21,
Math.sin(a) * 0.21
);
tooth.rotation.x = a;
}
box(sliderGear, 0.34, 0.34, 0.34, 0x775a35, 0, 0, 0);
// Hand crank.
const crank = new THREE.Group();
crank.position.set(-5.75, 2.03, 0.47);
scene.add(crank);
box(crank, 0.78, 0.13, 0.13, 0x73878a, 0, 0, 0);
for (let i = 0; i < 12; i++) {
const a = i * Math.PI / 6;
const tooth = box(
crank,
0.20,
0.20,
0.20,
0xb98a3c,
-0.08,
Math.cos(a) * 0.54,
Math.sin(a) * 0.54
);
tooth.rotation.x = a;
}
box(crank, 0.20, 0.65, 0.16, 0xe2ad48, 0.31, 0.30, 0);
const crankHandle = box(
crank,
0.34,
0.24,
0.24,
new THREE.MeshStandardMaterial({
color: 0xffc857,
emissive: 0x6a3d00,
emissiveIntensity: 0.42,
roughness: 0.55
}),
0.48,
0.68,
0
);
const interactive = [];
function markInteractive(root, action) {
root.traverse((child) => {
if (child.isMesh) {
child.userData.action = action;
interactive.push(child);
}
});
}
markInteractive(magnet, "magnet");
markInteractive(crankHandle, "crank");
// Approximate field arrows.
function createVoxelArrow() {
const group = new THREE.Group();
const material = new THREE.MeshBasicMaterial({
color: 0xffd35c,
transparent: true,
opacity: 0.6,
depthWrite: false
});
box(group, 0.36, 0.075, 0.075, material, 0, 0, 0);
box(group, 0.10, 0.19, 0.075, material, 0.21, 0, 0);
box(group, 0.10, 0.10, 0.19, material, 0.25, 0, 0);
scene.add(group);
return { group, material };
}
const axialArrows = [];
const returnArrows = [];
for (let i = 0; i < 9; i++) {
axialArrows.push(createVoxelArrow());
returnArrows.push(createVoxelArrow());
returnArrows[i].group.rotation.y = Math.PI;
}
makeLabel("HAND-CRANKED MAGNET SLIDER", -4.7, 3.03, 0.15, 2.65);
makeLabel("COIL", 0, 3.08, 0, 1.25, "#ffd898");
makeLabel("APPROX. FIELD", -2.05, 3.33, -0.35, 1.85, "#ffe295");
// Voltage display.
const display = new THREE.Group();
display.position.set(3.45, 3.03, -0.48);
scene.add(display);
box(display, 3.35, 2.18, 0.30, 0x172b31, 0, 0, 0);
box(display, 3.08, 1.92, 0.08, 0x071216, 0, 0, 0.18);
const screenCanvas = document.createElement("canvas");
screenCanvas.width = 640;
screenCanvas.height = 400;
const screenCtx = screenCanvas.getContext("2d");
const screenTexture = new THREE.CanvasTexture(screenCanvas);
screenTexture.colorSpace = THREE.SRGBColorSpace;
screenTexture.minFilter = THREE.LinearFilter;
const screen = new THREE.Mesh(
new THREE.PlaneGeometry(3.0, 1.86),
new THREE.MeshBasicMaterial({ map: screenTexture, toneMapped: false })
);
screen.position.set(0, 0, 0.225);
display.add(screen);
box(scene, 0.16, 0.66, 0.16, 0x52666a, 3.45, 1.72, -0.48);
box(scene, 1.7, 0.12, 0.8, 0x4d6265, 3.45, 1.66, -0.48);
makeLabel("VOLTAGE DISPLAY", 3.45, 4.28, -0.46, 2.25, "#8dffdb");
// Circuit wires from coil to display.
wireBetween(-0.73, 2.03, 0, -0.73, 1.67, 0, 0x16272c);
wireBetween(-0.73, 1.67, 0, 2.15, 1.67, 0, 0x16272c);
wireBetween(2.15, 1.67, 0, 2.15, 2.15, -0.48, 0x16272c);
wireBetween(2.15, 2.15, -0.48, 2.15, 2.15, -0.38, 0xd85f4f);
wireBetween(0.73, 2.03, 0, 1.02, 2.03, 0, 0x16272c);
wireBetween(1.02, 2.03, 0, 1.02, 1.76, -0.32, 0x16272c);
wireBetween(1.02, 1.76, -0.32, 4.7, 1.76, -0.32, 0x16272c);
wireBetween(4.7, 1.76, -0.32, 4.7, 2.15, -0.38, 0x16272c);
wireBetween(4.7, 2.15, -0.38, 4.7, 2.15, -0.48, 0x72e5c0);
// Waveform history.
const waveSamples = new Array(280).fill(0);
let sampleAccumulator = 0;
function drawScreen() {
const w = screenCanvas.width;
const h = screenCanvas.height;
const mid = h * 0.51;
const graphTop = 52;
const graphBottom = h - 42;
const range = 6;
screenCtx.fillStyle = "#06171c";
screenCtx.fillRect(0, 0, w, h);
screenCtx.fillStyle = "#7dffd0";
screenCtx.font = "800 23px ui-monospace, Consolas, monospace";
screenCtx.fillText("INDUCED VOLTAGE", 22, 31);
screenCtx.textAlign = "right";
screenCtx.fillStyle = state.voltage >= 0 ? "#7dffd0" : "#ff8585";
screenCtx.fillText(`${state.voltage >= 0 ? "+" : ""}${state.voltage.toFixed(2)} V`, w - 22, 31);
screenCtx.textAlign = "left";
screenCtx.strokeStyle = "rgba(101, 183, 174, 0.17)";
screenCtx.lineWidth = 1;
for (let x = 0; x <= w; x += 40) {
screenCtx.beginPath();
screenCtx.moveTo(x, graphTop);
screenCtx.lineTo(x, graphBottom);
screenCtx.stroke();
}
for (let y = graphTop; y <= graphBottom; y += 36) {
screenCtx.beginPath();
screenCtx.moveTo(0, y);
screenCtx.lineTo(w, y);
screenCtx.stroke();
}
screenCtx.strokeStyle = "rgba(190, 255, 236, 0.62)";
screenCtx.lineWidth = 2;
screenCtx.beginPath();
screenCtx.moveTo(0, mid);
screenCtx.lineTo(w, mid);
screenCtx.stroke();
screenCtx.fillStyle = "#7dffd0";
screenCtx.font = "800 17px ui-monospace, Consolas, monospace";
screenCtx.fillText("+", 8, graphTop + 14);
screenCtx.fillStyle = "#ff8585";
screenCtx.fillText("−", 9, graphBottom - 7);
screenCtx.strokeStyle = "#ffc857";
screenCtx.lineWidth = 3;
screenCtx.beginPath();
for (let i = 0; i < waveSamples.length; i++) {
const x = (i / (waveSamples.length - 1)) * w;
const value = clamp(waveSamples[i] / range, -1, 1);
const y = mid - value * (mid - graphTop - 10);
if (i === 0) screenCtx.moveTo(x, y);
else screenCtx.lineTo(x, y);
}
screenCtx.stroke();
screenCtx.fillStyle = "#95bdb7";
screenCtx.font = "700 15px ui-monospace, Consolas, monospace";
screenCtx.fillText("REVERSING MOTION REVERSES SIGN", 20, h - 13);
screenTexture.needsUpdate = true;
}
// Camera controls implemented directly without addons.
let yaw = -0.57;
let pitch = 0.43;
let cameraRadius = 16.8;
const cameraTarget = new THREE.Vector3(-0.8, 1.85, -0.2);
function updateCamera() {
const horizontal = cameraRadius * Math.cos(pitch);
camera.position.set(
cameraTarget.x + Math.sin(yaw) * horizontal,
cameraTarget.y + Math.sin(pitch) * cameraRadius,
cameraTarget.z + Math.cos(yaw) * horizontal
);
camera.lookAt(cameraTarget);
}
updateCamera();
const raycaster = new THREE.Raycaster();
const pointer = new THREE.Vector2();
const dragPlane = new THREE.Plane(new THREE.Vector3(0, 0, 1), 0);
const planeHit = new THREE.Vector3();
let pointerMode = null;
let lastPointerX = 0;
let lastPointerY = 0;
let shiftPressed = false;
function setPointer(event) {
const rect = renderer.domElement.getBoundingClientRect();
pointer.x = ((event.clientX - rect.left) / rect.width) * 2 - 1;
pointer.y = -((event.clientY - rect.top) / rect.height) * 2 + 1;
raycaster.setFromCamera(pointer, camera);
}
renderer.domElement.addEventListener("pointerdown", (event) => {
if (event.button !== 0) return;
setPointer(event);
lastPointerX = event.clientX;
lastPointerY = event.clientY;
shiftPressed = event.shiftKey;
const hits = raycaster.intersectObjects(interactive, false);
if (hits.length) {
pointerMode = hits[0].object.userData.action;
state.running = false;
renderer.domElement.setPointerCapture(event.pointerId);
} else {
pointerMode = shiftPressed ? "pan" : "orbit";
renderer.domElement.setPointerCapture(event.pointerId);
}
});
renderer.domElement.addEventListener("pointermove", (event) => {
if (!pointerMode) return;
const dx = event.clientX - lastPointerX;
const dy = event.clientY - lastPointerY;
lastPointerX = event.clientX;
lastPointerY = event.clientY;
if (pointerMode === "orbit") {
yaw -= dx * 0.006;
pitch = clamp(pitch + dy * 0.005, 0.12, 1.16);
updateCamera();
} else if (pointerMode === "pan") {
const right = new THREE.Vector3(Math.cos(yaw), 0, -Math.sin(yaw));
const forward = new THREE.Vector3(-Math.sin(yaw), 0, -Math.cos(yaw));
cameraTarget.addScaledVector(right, -dx * 0.014);
cameraTarget.addScaledVector(forward, -dy * 0.014);
cameraTarget.x = clamp(cameraTarget.x, -8, 8);
cameraTarget.z = clamp(cameraTarget.z, -6, 6);
updateCamera();
} else if (pointerMode === "crank") {
state.angle += dx * 0.018;
state.target = clamp(state.target + dx * 0.006, -5, -1.15);
} else if (pointerMode === "magnet") {
setPointer(event);
if (raycaster.ray.intersectPlane(dragPlane, planeHit)) {
const next = clamp(planeHit.x, -5, -1.15);
const delta = next - state.target;
state.target = next;
state.angle += delta * 0.13;
}
}
});
function endPointer(event) {
pointerMode = null;
if (renderer.domElement.hasPointerCapture(event.pointerId)) {
renderer.domElement.releasePointerCapture(event.pointerId);
}
}
renderer.domElement.addEventListener("pointerup", endPointer);
renderer.domElement.addEventListener("pointercancel", endPointer);
renderer.domElement.addEventListener("wheel", (event) => {
event.preventDefault();
cameraRadius = clamp(cameraRadius + event.deltaY * 0.012, 9, 25);
updateCamera();
}, { passive: false });
renderer.domElement.addEventListener("dblclick", () => {
yaw = -0.57;
pitch = 0.43;
cameraRadius = 16.8;
cameraTarget.set(-0.8, 1.85, -0.2);
updateCamera();
});
// UI controls.
const driveBtn = document.getElementById("driveBtn");
const reverseBtn = document.getElementById("reverseBtn");
const resetBtn = document.getElementById("resetBtn");
const positionSlider = document.getElementById("positionSlider");
const voltageValue = document.getElementById("voltageValue");
const directionValue = document.getElementById("directionValue");
const fieldValue = document.getElementById("fieldValue");
function updateButtons() {
driveBtn.textContent = state.running ? "PAUSE CRANK" : "START CRANK";
reverseBtn.textContent =
state.direction > 0 ? "REVERSE: OUTWARD" : "REVERSE: INWARD";
}
driveBtn.addEventListener("click", () => {
if (state.running) {
state.running = false;
} else {
if (state.target >= -1.16) state.direction = -1;
if (state.target <= -4.99) state.direction = 1;
state.running = true;
}
updateButtons();
});
reverseBtn.addEventListener("click", () => {
state.direction *= -1;
state.running = true;
updateButtons();
});
resetBtn.addEventListener("click", () => {
state.slider = -3.45;
state.target = -3.45;
state.velocity = 0;
state.angle = 0;
state.running = false;
state.direction = 1;
state.voltage = 0;
waveSamples.fill(0);
positionSlider.value = String(state.target);
updateButtons();
});
positionSlider.addEventListener("input", () => {
state.target = Number(positionSlider.value);
state.running = false;
updateButtons();
});
window.addEventListener("resize", () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
});
// Animation and approximate induction model.
const clock = new THREE.Clock();
function animate() {
requestAnimationFrame(animate);
const dt = Math.min(clock.getDelta(), 0.05);
if (state.running) {
state.angle += state.direction * dt * 2.2;
state.target = clamp(
state.target + state.direction * dt * 0.65,
-5,
-1.15
);
if (
(state.direction > 0 && state.target >= -1.149) ||
(state.direction < 0 && state.target <= -4.999)
) {
state.running = false;
updateButtons();
}
}
const previousSlider = state.slider;
state.slider += (state.target - state.slider) * (1 - Math.exp(-dt * 14));
const rawVelocity = dt > 0 ? (state.slider - previousSlider) / dt : 0;
state.velocity += (rawVelocity - state.velocity) * (1 - Math.exp(-dt * 12));
state.velocity = clamp(state.velocity, -4, 4);
const model = fieldModel(state.slider);
state.field = model.field;
state.slope = model.slope;
// For the displayed winding polarity, reversing velocity reverses sign.
state.voltage = -12 * model.slope * state.velocity;
magnet.position.x = state.slider;
sliderGear.position.x = state.slider;
sliderGear.rotation.x = -state.slider * 0.7;
crank.rotation.x = state.angle;
positionSlider.value = String(state.target);
// Update schematic field arrows.
const start = state.slider + 0.62;
const end = 0.04;
for (let i = 0; i < axialArrows.length; i++) {
const t = i / (axialArrows.length - 1);
const arrow = axialArrows[i];
arrow.group.position.set(start + (end - start) * t, 2.03, 0.03);
arrow.material.opacity = clamp(0.16 + state.field * 0.62, 0.12, 0.88);
const returnArrow = returnArrows[i];
returnArrow.group.position.set(
end + (start - end) * t,
2.79,
-0.12
);
returnArrow.material.opacity = clamp(state.field * 0.45, 0.06, 0.52);
}
// Sample and redraw the waveform.
sampleAccumulator += dt;
if (sampleAccumulator >= 0.035) {
sampleAccumulator = 0;
waveSamples.push(state.voltage);
waveSamples.shift();
drawScreen();
}
const moving = Math.abs(state.velocity) > 0.035;
const inward = state.velocity > 0;
voltageValue.textContent =
`${state.voltage >= 0 ? "+" : ""}${state.voltage.toFixed(2)} V`;
voltageValue.className = state.voltage >= 0 ? "positive" : "negative";
directionValue.textContent = moving
? (inward ? "INWARD → COIL" : "OUTWARD ← COIL")
: "STILL";
directionValue.className = moving
? (inward ? "negative" : "positive")
: "";
fieldValue.textContent = state.field.toFixed(2);
renderer.render(scene, camera);
}
updateButtons();
drawScreen();
animate();
</script>
</body>
</html>
```