Prompt specification
Build or customize the Three.js 3D scene "Corroding metal surface sectional 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: Corroding metal surface sectional model
Category: Science & Physics · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/corroding-metal-surface-sectional-model-x1941
Scene Description Prompt:
Use Three.js from cdnjs to build a metal slab with localized oxidized regions and separate symbolic electron-transfer arrows; distinguish corrosion product volume from the original intact metal geometry. Deliver one self-contained HTML file with a 3D cutaway.
Working Three.js Starter (corroding-metal-surface-sectional-model-x1941):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Localized Corrosion — 3D Metal Slab Cutaway</title>
<style>
:root {
color-scheme: dark;
--panel: rgba(9, 16, 25, 0.82);
--panel-border: rgba(154, 196, 224, 0.18);
--text: #eaf4ff;
--muted: #91a8ba;
--cyan: #62dcff;
--oxide: #d87335;
--electron: #f06cff;
}
* { box-sizing: border-box; }
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background:
radial-gradient(circle at 50% 42%, #132536 0%, #09131d 48%, #050a10 100%);
color: var(--text);
font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
#scene {
position: fixed;
inset: 0;
}
canvas {
display: block;
width: 100%;
height: 100%;
touch-action: none;
outline: none;
}
.panel {
position: fixed;
z-index: 10;
border: 1px solid var(--panel-border);
background: var(--panel);
box-shadow:
0 20px 60px rgba(0, 0, 0, 0.28),
inset 0 1px 0 rgba(255, 255, 255, 0.035);
backdrop-filter: blur(16px) saturate(125%);
-webkit-backdrop-filter: blur(16px) saturate(125%);
}
.title-panel {
top: 22px;
left: 22px;
width: min(390px, calc(100vw - 44px));
padding: 19px 21px 18px;
border-radius: 16px;
pointer-events: none;
}
.eyebrow {
display: flex;
align-items: center;
gap: 9px;
margin-bottom: 9px;
color: var(--cyan);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.18em;
text-transform: uppercase;
}
.eyebrow::before {
width: 7px;
height: 7px;
border-radius: 50%;
background: var(--cyan);
box-shadow: 0 0 14px var(--cyan);
content: "";
}
h1 {
margin: 0;
font-size: clamp(22px, 2.4vw, 31px);
line-height: 1.08;
letter-spacing: -0.035em;
font-weight: 720;
}
.subtitle {
margin: 10px 0 0;
max-width: 350px;
color: var(--muted);
font-size: 12.5px;
line-height: 1.55;
}
.legend-panel {
top: 22px;
right: 22px;
width: 285px;
padding: 16px;
border-radius: 16px;
}
.panel-heading {
margin: 0 0 12px;
color: #dceaf5;
font-size: 10px;
font-weight: 800;
letter-spacing: 0.16em;
text-transform: uppercase;
}
.legend-row {
display: grid;
grid-template-columns: 34px 1fr;
align-items: center;
gap: 10px;
margin: 9px 0;
}
.swatch {
position: relative;
width: 32px;
height: 16px;
border-radius: 5px;
}
.swatch.metal {
border: 1px solid #9cb0c2;
background: linear-gradient(145deg, #8da2b4, #44596b);
box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.25);
}
.swatch.oxide {
border: 1px solid #ed9b55;
background:
radial-gradient(circle at 35% 30%, #e9a24a 0 12%, transparent 13%),
radial-gradient(circle at 68% 65%, #7f351c 0 17%, transparent 18%),
linear-gradient(135deg, #d16b2d, #69301e);
}
.swatch.electron {
height: 0;
border-top: 3px dashed var(--electron);
border-radius: 0;
filter: drop-shadow(0 0 4px rgba(240, 108, 255, 0.7));
}
.swatch.envelope {
border: 1px dashed var(--cyan);
background: rgba(98, 220, 255, 0.06);
}
.legend-copy strong {
display: block;
margin-bottom: 2px;
color: #e9f4fc;
font-size: 11.5px;
font-weight: 680;
}
.legend-copy span {
display: block;
color: #829aab;
font-size: 10px;
line-height: 1.35;
}
.divider {
height: 1px;
margin: 13px 0;
background: rgba(255, 255, 255, 0.08);
}
.toggle {
display: flex;
align-items: center;
justify-content: space-between;
gap: 12px;
padding: 5px 1px;
color: #b8cbd9;
font-size: 11px;
cursor: pointer;
user-select: none;
}
.toggle input {
width: 15px;
height: 15px;
accent-color: var(--cyan);
cursor: pointer;
}
.reset-button {
width: 100%;
margin-top: 11px;
padding: 9px 12px;
border: 1px solid rgba(98, 220, 255, 0.3);
border-radius: 9px;
background: rgba(98, 220, 255, 0.08);
color: #dff7ff;
font: inherit;
font-size: 11px;
font-weight: 700;
cursor: pointer;
transition: background 150ms ease, border-color 150ms ease;
}
.reset-button:hover {
border-color: rgba(98, 220, 255, 0.65);
background: rgba(98, 220, 255, 0.15);
}
.note-panel {
right: 22px;
bottom: 22px;
width: 285px;
padding: 15px 16px;
border-radius: 14px;
color: #a9bdcc;
font-size: 10.5px;
line-height: 1.55;
pointer-events: none;
}
.note-panel strong {
color: #e7f3fb;
font-weight: 700;
}
.equation {
margin-top: 10px;
padding-top: 10px;
border-top: 1px solid rgba(255, 255, 255, 0.08);
color: #cfdaea;
font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
font-size: 10px;
line-height: 1.7;
}
.equation .anode { color: #efa15f; }
.equation .cathode { color: #70dcff; }
.hint {
position: fixed;
z-index: 8;
left: 22px;
bottom: 22px;
padding: 9px 12px;
border: 1px solid rgba(255, 255, 255, 0.08);
border-radius: 999px;
background: rgba(7, 13, 20, 0.58);
color: #8097a9;
font-size: 10px;
letter-spacing: 0.02em;
pointer-events: none;
backdrop-filter: blur(10px);
}
#loading {
position: fixed;
z-index: 30;
inset: 0;
display: grid;
place-items: center;
background: #07101a;
color: #bcd3e2;
font-size: 12px;
letter-spacing: 0.08em;
transition: opacity 350ms ease;
}
#loading.hidden {
opacity: 0;
pointer-events: none;
}
.loader {
width: 38px;
height: 38px;
margin: 0 auto 12px;
border: 2px solid rgba(98, 220, 255, 0.15);
border-top-color: var(--cyan);
border-radius: 50%;
animation: spin 900ms linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }
@media (max-width: 760px) {
.title-panel {
top: 12px;
left: 12px;
width: calc(100vw - 24px);
padding: 14px 16px;
}
.subtitle { display: none; }
.legend-panel {
top: auto;
right: 12px;
bottom: 12px;
width: 238px;
padding: 12px;
}
.legend-row { margin: 7px 0; }
.note-panel { display: none; }
.hint { display: none; }
}
</style>
</head>
<body>
<div id="scene"></div>
<section class="panel title-panel">
<div class="eyebrow">Electrochemistry · 3D cutaway</div>
<h1>Localized corrosion on a metal slab</h1>
<p class="subtitle">
The blue-gray solid is the original metal substrate. Raised orange volumes
are corrosion products, while dashed magenta paths separately symbolize
electron transfer through the metal.
</p>
</section>
<aside class="panel legend-panel">
<h2 class="panel-heading">Visual encoding</h2>
<div class="legend-row">
<div class="swatch metal"></div>
<div class="legend-copy">
<strong>Original metal geometry</strong>
<span>Blue-gray solid; front-right volume removed</span>
</div>
</div>
<div class="legend-row">
<div class="swatch oxide"></div>
<div class="legend-copy">
<strong>Corrosion-product volume</strong>
<span>Raised, rough oxide solids added beyond the surface</span>
</div>
</div>
<div class="legend-row">
<div class="swatch electron"></div>
<div class="legend-copy">
<strong>Symbolic electron transfer</strong>
<span>Animated dashed arrows; not to scale</span>
</div>
</div>
<div class="legend-row">
<div class="swatch envelope"></div>
<div class="legend-copy">
<strong>Uncut slab envelope</strong>
<span>Cyan reference for the original full bounding volume</span>
</div>
</div>
<div class="divider"></div>
<label class="toggle">
<span>Corrosion products</span>
<input id="toggleOxide" type="checkbox" checked />
</label>
<label class="toggle">
<span>Electron-transfer arrows</span>
<input id="toggleArrows" type="checkbox" checked />
</label>
<label class="toggle">
<span>Original envelope</span>
<input id="toggleEnvelope" type="checkbox" checked />
</label>
<button id="resetView" class="reset-button">Reset camera</button>
</aside>
<aside class="panel note-panel">
<strong>Reading the cutaway:</strong> the orange regions are discrete
product volumes rather than a recolored metal surface. Their raised profiles
make the material added by oxidation visible. Cyan cut planes mark where
the slab was sectioned.
<div class="equation">
<div class="anode">Fe → Fe²⁺ + 2e⁻</div>
<div class="cathode">O₂ + 2H₂O + 4e⁻ → 4OH⁻</div>
</div>
</aside>
<div class="hint">Drag: orbit · Shift/right-drag: pan · Wheel/pinch: zoom · R: reset</div>
<div id="loading">
<div>
<div class="loader"></div>
INITIALIZING CUTAWAY
</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 sceneHost = document.getElementById("scene");
const loading = document.getElementById("loading");
// ---------------------------------------------------------------------
// Renderer and scene
// ---------------------------------------------------------------------
const renderer = new THREE.WebGLRenderer({
antialias: true,
alpha: true,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.setClearColor(0x000000, 0);
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.18;
renderer.domElement.setAttribute(
"aria-label",
"Interactive three-dimensional cutaway of a corroded metal slab"
);
sceneHost.appendChild(renderer.domElement);
const scene = new THREE.Scene();
scene.fog = new THREE.FogExp2(0x08131e, 0.018);
const camera = new THREE.PerspectiveCamera(
42,
window.innerWidth / window.innerHeight,
0.1,
100
);
const modelGroup = new THREE.Group();
const oxideGroup = new THREE.Group();
const arrowGroup = new THREE.Group();
const envelopeGroup = new THREE.Group();
const labelGroup = new THREE.Group();
scene.add(modelGroup, oxideGroup, arrowGroup, envelopeGroup, labelGroup);
// ---------------------------------------------------------------------
// Lighting and ground
// ---------------------------------------------------------------------
const hemi = new THREE.HemisphereLight(0xbadfff, 0x17212b, 1.55);
scene.add(hemi);
const keyLight = new THREE.DirectionalLight(0xe8f5ff, 4.0);
keyLight.position.set(7, 11, 8);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(2048, 2048);
keyLight.shadow.camera.left = -12;
keyLight.shadow.camera.right = 12;
keyLight.shadow.camera.top = 12;
keyLight.shadow.camera.bottom = -12;
keyLight.shadow.camera.near = 1;
keyLight.shadow.camera.far = 32;
keyLight.shadow.bias = -0.00025;
scene.add(keyLight);
const warmFill = new THREE.DirectionalLight(0xffa763, 1.25);
warmFill.position.set(-7, 4, 5);
scene.add(warmFill);
const coolFill = new THREE.DirectionalLight(0x58cfff, 1.8);
coolFill.position.set(4, 5, -9);
scene.add(coolFill);
const ground = new THREE.Mesh(
new THREE.PlaneGeometry(50, 50),
new THREE.MeshStandardMaterial({
color: 0x0a121b,
roughness: 0.94,
metalness: 0.05
})
);
ground.rotation.x = -Math.PI / 2;
ground.position.y = -0.055;
ground.receiveShadow = true;
scene.add(ground);
const grid = new THREE.GridHelper(40, 40, 0x31556d, 0x183142);
grid.position.y = -0.045;
grid.material.transparent = true;
grid.material.opacity = 0.26;
grid.material.depthWrite = false;
scene.add(grid);
// ---------------------------------------------------------------------
// Original slab: an L-shaped extrusion with a front-right cutaway
// Local shape Y becomes world -Z after rotation.
// ---------------------------------------------------------------------
const slabShape = new THREE.Shape();
slabShape.moveTo(-5, 3);
slabShape.lineTo(5, 3);
slabShape.lineTo(5, 0.8);
slabShape.lineTo(2.2, 0.8);
slabShape.lineTo(2.2, -3);
slabShape.lineTo(-5, -3);
slabShape.closePath();
const slabGeometry = new THREE.ExtrudeGeometry(slabShape, {
depth: 1.12,
bevelEnabled: true,
bevelThickness: 0.04,
bevelSize: 0.045,
bevelOffset: 0,
bevelSegments: 3,
curveSegments: 1
});
slabGeometry.rotateX(-Math.PI / 2);
const metalMaterial = new THREE.MeshStandardMaterial({
color: 0x738b9e,
metalness: 0.58,
roughness: 0.34,
envMapIntensity: 0.9
});
const slab = new THREE.Mesh(slabGeometry, metalMaterial);
slab.position.y = 0.04;
slab.castShadow = true;
slab.receiveShadow = true;
modelGroup.add(slab);
const slabEdges = new THREE.LineSegments(
new THREE.EdgesGeometry(slabGeometry, 24),
new THREE.LineBasicMaterial({
color: 0xb4c8d7,
transparent: true,
opacity: 0.62
})
);
slab.add(slabEdges);
// Transparent cyan section faces emphasize the geometry removed by the cut.
const cutFaceMaterial = new THREE.MeshBasicMaterial({
color: 0x54d7ff,
transparent: true,
opacity: 0.105,
side: THREE.DoubleSide,
depthWrite: false,
polygonOffset: true,
polygonOffsetFactor: -1
});
const cutEdgeMaterial = new THREE.LineBasicMaterial({
color: 0x72e4ff,
transparent: true,
opacity: 0.9
});
function addCutFace(width, height, position, rotationY = 0) {
const face = new THREE.Mesh(
new THREE.PlaneGeometry(width, height),
cutFaceMaterial
);
face.position.copy(position);
face.rotation.y = rotationY;
face.renderOrder = 2;
modelGroup.add(face);
const half = width / 2;
const points = [
new THREE.Vector3(-half, -height / 2, 0),
new THREE.Vector3(half, -height / 2, 0),
new THREE.Vector3(half, height / 2, 0),
new THREE.Vector3(-half, height / 2, 0)
];
const outline = new THREE.LineLoop(
new THREE.BufferGeometry().setFromPoints(points),
cutEdgeMaterial
);
outline.position.copy(position);
outline.rotation.y = rotationY;
outline.renderOrder = 4;
modelGroup.add(outline);
}
// X-normal cut face at x = 2.2.
addCutFace(
3.8,
1.2,
new THREE.Vector3(2.205, 0.6, 1.1),
Math.PI / 2
);
// Z-normal cut face at z = -0.8.
addCutFace(
2.8,
1.2,
new THREE.Vector3(3.6, 0.6, -0.795),
0
);
// ---------------------------------------------------------------------
// Uncut reference envelope
// ---------------------------------------------------------------------
const envelopeGeometry = new THREE.EdgesGeometry(
new THREE.BoxGeometry(10, 1.2, 6)
);
const envelope = new THREE.LineSegments(
envelopeGeometry,
new THREE.LineDashedMaterial({
color: 0x6cddff,
transparent: true,
opacity: 0.32,
dashSize: 0.24,
gapSize: 0.15,
depthTest: false,
depthWrite: false
})
);
envelope.position.y = 0.6;
envelope.computeLineDistances();
envelope.renderOrder = 3;
envelopeGroup.add(envelope);
// ---------------------------------------------------------------------
// Irregular corrosion-product volumes
// ---------------------------------------------------------------------
const rustPalette = [
new THREE.Color(0x542619),
new THREE.Color(0x85351d),
new THREE.Color(0xb84d23),
new THREE.Color(0xd87a34),
new THREE.Color(0xe1a04d)
];
const rustMaterial = new THREE.MeshStandardMaterial({
vertexColors: true,
roughness: 0.93,
metalness: 0.035,
side: THREE.DoubleSide
});
const granuleGeometry = new THREE.IcosahedronGeometry(1, 1);
const granuleMaterial = new THREE.MeshStandardMaterial({
color: 0x9c4825,
roughness: 0.98,
metalness: 0.01
});
function organicFactor(theta, seed) {
return (
1 +
0.13 * Math.sin(theta * 3 + seed) +
0.075 * Math.sin(theta * 7 - seed * 1.7) +
0.035 * Math.sin(theta * 11 + seed * 0.9)
);
}
function createMoundGeometry(rx, rz, height, seed) {
const segments = 72;
const rings = 8;
const positions = [];
const colors = [];
const indices = [];
function pushColor(index, color) {
colors[index * 3] = color.r;
colors[index * 3 + 1] = color.g;
colors[index * 3 + 2] = color.b;
}
// Center vertex.
positions.push(0, height, 0);
pushColor(0, rustPalette[3]);
for (let ring = 1; ring <= rings; ring++) {
const t = ring / rings;
for (let i = 0; i < segments; i++) {
const theta = (i / segments) * Math.PI * 2;
const edge = organicFactor(theta, seed);
const radius = t * edge;
const x = rx * radius * Math.cos(theta);
const z = rz * radius * Math.sin(theta);
const micro =
0.78 +
0.22 *
(0.5 +
0.5 *
Math.sin(x * 5.1 + seed) *
Math.cos(z * 6.3 - seed * 0.7));
const profile = Math.pow(Math.max(0, 1 - t), 1.48);
const y = height * profile * micro;
const tone =
0.5 +
0.5 *
Math.sin(theta * 4 + seed * 0.6 + Math.sin(radius * 5.0));
let paletteIndex;
if (ring === rings) paletteIndex = 0;
else if (tone > 0.77) paletteIndex = 4;
else if (tone > 0.45) paletteIndex = 3;
else paletteIndex = tone > 0.22 ? 2 : 1;
positions.push(x, y, z);
pushColor(positions.length / 3 - 1, rustPalette[paletteIndex]);
}
}
// Top center fan.
for (let i = 0; i < segments; i++) {
const current = 1 + i;
const next = 1 + ((i + 1) % segments);
indices.push(0, next, current);
}
// Top surface rings.
for (let ring = 1; ring < rings; ring++) {
const innerStart = 1 + (ring - 1) * segments;
const outerStart = 1 + ring * segments;
for (let i = 0; i < segments; i++) {
const next = (i + 1) % segments;
const a = innerStart + i;
const b = outerStart + i;
const c = outerStart + next;
const d = innerStart + next;
indices.push(a, c, b, a, d, c);
}
}
// Side wall and bottom skirt.
const topBoundary = 1 + (rings - 1) * segments;
const bottomBoundary = positions.length / 3;
for (let i = 0; i < segments; i++) {
const theta = (i / segments) * Math.PI * 2;
const edge = organicFactor(theta, seed);
positions.push(
rx * edge * Math.cos(theta),
0,
rz * edge * Math.sin(theta)
);
pushColor(positions.length / 3 - 1, rustPalette[0]);
}
for (let i = 0; i < segments; i++) {
const next = (i + 1) % segments;
const ti = topBoundary + i;
const tn = topBoundary + next;
const bi = bottomBoundary + i;
const bn = bottomBoundary + next;
indices.push(ti, bn, bi, ti, tn, bn);
}
const geometry = new THREE.BufferGeometry();
geometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(positions, 3)
);
geometry.setAttribute(
"color",
new THREE.Float32BufferAttribute(colors, 3)
);
geometry.setIndex(indices);
geometry.computeVertexNormals();
geometry.computeBoundingSphere();
return geometry;
}
function seededRandom(seed) {
let state = seed >>> 0;
return () => {
state = (1664525 * state + 1013904223) >>> 0;
return state / 4294967296;
};
}
function makeOxidePatch({
position,
rx,
rz,
height,
seed,
rotation = null
}) {
const group = new THREE.Group();
group.position.copy(position);
if (rotation) group.rotation.copy(rotation);
const mound = new THREE.Mesh(
createMoundGeometry(rx, rz, height, seed),
rustMaterial
);
mound.castShadow = true;
mound.receiveShadow = true;
group.add(mound);
const random = seededRandom(seed * 977 + 31);
const count = 22;
const granules = new THREE.InstancedMesh(
granuleGeometry,
granuleMaterial,
count
);
granules.castShadow = true;
granules.receiveShadow = true;
const dummy = new THREE.Object3D();
for (let i = 0; i < count; i++) {
const angle = random() * Math.PI * 2;
const radial = Math.sqrt(random()) * 0.83;
const x = rx * radial * Math.cos(angle);
const z = rz * radial * Math.sin(angle);
const y =
height * Math.pow(Math.max(0, 1 - radial), 1.45) * 0.76 + 0.025;
const size = 0.035 + random() * 0.075;
dummy.position.set(x, y, z);
dummy.scale.set(size, size * (0.65 + random() * 0.55), size);
dummy.rotation.set(random() * 3, random() * 3, random() * 3);
dummy.updateMatrix();
granules.setMatrixAt(i, dummy.matrix);
}
granules.instanceMatrix.needsUpdate = true;
group.add(granules);
oxideGroup.add(group);
return group;
}
const anchorA1 = new THREE.Vector3(-2.35, 1.2, 1.25);
const anchorA2 = new THREE.Vector3(-3.65, 1.2, -1.35);
const anchorC = new THREE.Vector3(1.1, 1.2, -1.75);
makeOxidePatch({
position: new THREE.Vector3(-2.35, 1.205, 1.25),
rx: 1.28,
rz: 0.93,
height: 0.52,
seed: 1.7
});
makeOxidePatch({
position: new THREE.Vector3(-3.65, 1.205, -1.35),
rx: 0.88,
rz: 0.72,
height: 0.32,
seed: 4.3
});
makeOxidePatch({
position: new THREE.Vector3(1.1, 1.205, -1.75),
rx: 1.05,
rz: 0.78,
height: 0.40,
seed: 8.1
});
// A visible oxide deposit on the front face.
makeOxidePatch({
position: new THREE.Vector3(-2.55, 0.64, 3.005),
rx: 1.05,
rz: 0.48,
height: 0.30,
seed: 11.4,
rotation: new THREE.Euler(Math.PI / 2, 0, 0)
});
// ---------------------------------------------------------------------
// Canvas-generated labels, using only core Three.js
// ---------------------------------------------------------------------
function roundedRect(ctx, x, y, w, h, radius) {
const r = Math.min(radius, w / 2, h / 2);
ctx.beginPath();
ctx.moveTo(x + r, y);
ctx.arcTo(x + w, y, x + w, y + h, r);
ctx.arcTo(x + w, y + h, x, y + h, r);
ctx.arcTo(x, y + h, x, y, r);
ctx.arcTo(x, y, x + w, y, r);
ctx.closePath();
}
function createLabelSprite({
kicker,
main,
accent = "#62dcff",
width = 640,
height = 190
}) {
const canvas = document.createElement("canvas");
canvas.width = width;
canvas.height = height;
const ctx = canvas.getContext("2d");
const gradient = ctx.createLinearGradient(0, 0, width, height);
gradient.addColorStop(0, "rgba(10,20,31,0.94)");
gradient.addColorStop(1, "rgba(5,11,18,0.91)");
roundedRect(ctx, 5, 5, width - 10, height - 10, 24);
ctx.fillStyle = gradient;
ctx.fill();
ctx.lineWidth = 3;
ctx.strokeStyle = accent;
ctx.globalAlpha = 0.72;
ctx.stroke();
ctx.globalAlpha = 1;
ctx.fillStyle = accent;
ctx.font = "700 24px system-ui, sans-serif";
ctx.fillText(kicker, 27, 45);
ctx.fillStyle = "#f1f8fc";
ctx.font = "700 38px ui-monospace, SFMono-Regular, Menlo, monospace";
ctx.fillText(main, 27, 101);
ctx.fillStyle = "rgba(202,220,232,0.72)";
ctx.font = "500 20px system-ui, sans-serif";
ctx.fillText("symbolic annotation", 27, 151);
const texture = new THREE.CanvasTexture(canvas);
texture.colorSpace = THREE.SRGBColorSpace;
texture.minFilter = THREE.LinearFilter;
const sprite = new THREE.Sprite(
new THREE.SpriteMaterial({
map: texture,
transparent: true,
depthTest: false,
depthWrite: false
})
);
sprite.scale.set(3.35, 0.995, 1);
sprite.renderOrder = 20;
return sprite;
}
const labelA1 = createLabelSprite({
kicker: "ANODE A1 · OXIDATION",
main: "Fe → Fe²⁺ + 2e⁻",
accent: "#efa15f"
});
labelA1.position.set(-2.4, 2.67, 1.28);
labelGroup.add(labelA1);
const labelA2 = createLabelSprite({
kicker: "ANODE A2 · OXIDATION",
main: "Fe → Fe²⁺ + 2e⁻",
accent: "#efa15f",
width: 560
});
labelA2.scale.set(3.05, 0.91, 1);
labelA2.position.set(-3.72, 2.08, -1.35);
labelGroup.add(labelA2);
const labelC = createLabelSprite({
kicker: "CATHODE · REDUCTION",
main: "O₂ + 2H₂O + 4e⁻ → 4OH⁻",
accent: "#62dcff",
width: 700
});
labelC.scale.set(3.65, 0.99, 1);
labelC.position.set(1.3, 2.48, -1.78);
labelGroup.add(labelC);
const cutLabel = createLabelSprite({
kicker: "SECTIONED VOLUME",
main: "CUTAWAY",
accent: "#62dcff",
width: 430
});
cutLabel.scale.set(2.15, 0.76, 1);
cutLabel.position.set(3.75, 1.78, -0.75);
labelGroup.add(cutLabel);
// ---------------------------------------------------------------------
// Separate symbolic electron-transfer arrows
// ---------------------------------------------------------------------
const animatedPulses = [];
function createElectronArrow(curve, delay, speed) {
const lineGeometry = new THREE.BufferGeometry().setFromPoints(
curve.getPoints(110)
);
const line = new THREE.Line(
lineGeometry,
new THREE.LineDashedMaterial({
color: 0xf06cff,
dashSize: 0.23,
gapSize: 0.13,
transparent: true,
opacity: 0.96,
depthTest: false,
depthWrite: false
})
);
line.computeLineDistances();
line.renderOrder = 12;
arrowGroup.add(line);
const tangent = curve.getTangentAt(1).normalize();
const headHeight = 0.56;
const head = new THREE.Mesh(
new THREE.ConeGeometry(0.19, headHeight, 22),
new THREE.MeshBasicMaterial({
color: 0xf06cff,
depthTest: false,
depthWrite: false
})
);
head.position.copy(curve.getPointAt(1)).addScaledVector(tangent, -headHeight / 2);
head.quaternion.setFromUnitVectors(
new THREE.Vector3(0, 1, 0),
tangent
);
head.renderOrder = 13;
arrowGroup.add(head);
const startRing = new THREE.Mesh(
new THREE.TorusGeometry(0.19, 0.025, 10, 48),
new THREE.MeshBasicMaterial({
color: 0xf06cff,
transparent: true,
opacity: 0.8,
depthTest: false,
depthWrite: false
})
);
startRing.position.copy(curve.getPointAt(0));
startRing.rotation.x = Math.PI / 2;
startRing.renderOrder = 13;
arrowGroup.add(startRing);
for (let i = 0; i < 2; i++) {
const pulse = new THREE.Mesh(
new THREE.SphereGeometry(0.085, 16, 12),
new THREE.MeshBasicMaterial({
color: i === 0 ? 0xffffff : 0xff9cff,
depthTest: false,
depthWrite: false
})
);
pulse.renderOrder = 15;
arrowGroup.add(pulse);
animatedPulses.push({
mesh: pulse,
curve,
delay: delay + i * 0.47,
speed
});
}
}
const curveA1 = new THREE.CubicBezierCurve3(
new THREE.Vector3(-2.35, 1.92, 1.25),
new THREE.Vector3(-1.3, 4.25, 1.8),
new THREE.Vector3(0.3, 4.55, -1.15),
new THREE.Vector3(1.1, 1.82, -1.75)
);
const curveA2 = new THREE.CubicBezierCurve3(
new THREE.Vector3(-3.65, 1.62, -1.35),
new THREE.Vector3(-3.2, 3.25, -1.95),
new THREE.Vector3(-0.65, 3.8, -2.1),
new THREE.Vector3(1.1, 1.82, -1.75)
);
createElectronArrow(curveA1, 0.0, 0.17);
createElectronArrow(curveA2, 0.34, 0.15);
const electronLabel = createLabelSprite({
kicker: "THROUGH THE METAL",
main: "e⁻ transfer",
accent: "#f06cff",
width: 450
});
electronLabel.scale.set(2.15, 0.72, 1);
electronLabel.position.set(-0.75, 3.68, 0.1);
labelGroup.add(electronLabel);
// ---------------------------------------------------------------------
// Direct camera controls: orbit, pan, zoom, keyboard reset
// ---------------------------------------------------------------------
const target = new THREE.Vector3(0, 0.72, 0);
const spherical = new THREE.Spherical(15.2, 1.02, 0.72);
const initialCamera = {
target: target.clone(),
spherical: spherical.clone()
};
const pointers = new Map();
let interactionMode = "rotate";
let lastX = 0;
let lastY = 0;
let pinchDistance = 0;
function updateCamera() {
spherical.phi = THREE.MathUtils.clamp(spherical.phi, 0.32, 1.48);
spherical.radius = THREE.MathUtils.clamp(spherical.radius, 8, 26);
spherical.makeSafe();
const offset = new THREE.Vector3().setFromSpherical(spherical);
camera.position.copy(target).add(offset);
camera.lookAt(target);
camera.updateMatrixWorld();
}
function resetCamera() {
target.copy(initialCamera.target);
spherical.copy(initialCamera.spherical);
updateCamera();
}
function pointerDistance() {
const values = [...pointers.values()];
if (values.length < 2) return 0;
return Math.hypot(
values[0].x - values[1].x,
values[0].y - values[1].y
);
}
renderer.domElement.addEventListener("pointerdown", (event) => {
renderer.domElement.setPointerCapture(event.pointerId);
pointers.set(event.pointerId, { x: event.clientX, y: event.clientY });
if (pointers.size === 1) {
interactionMode =
event.button === 2 || event.shiftKey ? "pan" : "rotate";
lastX = event.clientX;
lastY = event.clientY;
} else if (pointers.size === 2) {
pinchDistance = pointerDistance();
}
});
renderer.domElement.addEventListener("pointermove", (event) => {
if (!pointers.has(event.pointerId)) return;
pointers.set(event.pointerId, { x: event.clientX, y: event.clientY });
if (pointers.size === 2) {
const distance = pointerDistance();
if (pinchDistance > 0 && distance > 0) {
spherical.radius *= pinchDistance / distance;
pinchDistance = distance;
updateCamera();
}
return;
}
const dx = event.clientX - lastX;
const dy = event.clientY - lastY;
lastX = event.clientX;
lastY = event.clientY;
if (interactionMode === "rotate") {
spherical.theta -= dx * 0.006;
spherical.phi -= dy * 0.005;
} else {
const direction = new THREE.Vector3();
camera.getWorldDirection(direction);
const right = new THREE.Vector3()
.crossVectors(direction, camera.up)
.normalize();
const up = new THREE.Vector3()
.crossVectors(right, direction)
.normalize();
const scale = spherical.radius * 0.00145;
target.addScaledVector(right, -dx * scale);
target.addScaledVector(up, dy * scale);
}
updateCamera();
});
function endPointer(event) {
pointers.delete(event.pointerId);
if (pointers.size < 2) pinchDistance = 0;
if (pointers.size === 1) {
const only = [...pointers.values()][0];
lastX = only.x;
lastY = only.y;
}
}
renderer.domElement.addEventListener("pointerup", endPointer);
renderer.domElement.addEventListener("pointercancel", endPointer);
renderer.domElement.addEventListener("contextmenu", (event) => event.preventDefault());
renderer.domElement.addEventListener(
"wheel",
(event) => {
event.preventDefault();
spherical.radius *= Math.exp(event.deltaY * 0.001);
updateCamera();
},
{ passive: false }
);
window.addEventListener("keydown", (event) => {
if (event.key.toLowerCase() === "r") resetCamera();
});
document.getElementById("resetView").addEventListener("click", resetCamera);
document.getElementById("toggleOxide").addEventListener("change", (event) => {
oxideGroup.visible = event.target.checked;
});
document.getElementById("toggleArrows").addEventListener("change", (event) => {
arrowGroup.visible = event.target.checked;
});
document.getElementById("toggleEnvelope").addEventListener("change", (event) => {
envelopeGroup.visible = event.target.checked;
});
// ---------------------------------------------------------------------
// Resize and animation
// ---------------------------------------------------------------------
function onResize() {
const width = window.innerWidth;
const height = window.innerHeight;
camera.aspect = width / height;
camera.updateProjectionMatrix();
renderer.setSize(width, height);
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
}
window.addEventListener("resize", onResize);
const clock = new THREE.Clock();
function animate() {
requestAnimationFrame(animate);
const elapsed = clock.getElapsedTime();
for (const pulse of animatedPulses) {
const t = (elapsed * pulse.speed + pulse.delay) % 1;
pulse.mesh.position.copy(pulse.curve.getPointAt(t));
const tangent = pulse.curve.getTangentAt(t);
pulse.mesh.quaternion.setFromUnitVectors(
new THREE.Vector3(0, 1, 0),
tangent
);
}
// Subtle label breathing keeps the annotations visually secondary.
labelA1.material.opacity = 0.96;
labelA2.material.opacity = 0.96;
labelC.material.opacity = 0.96;
renderer.render(scene, camera);
}
updateCamera();
onResize();
animate();
requestAnimationFrame(() => {
loading.classList.add("hidden");
setTimeout(() => loading.remove(), 400);
});
</script>
</body>
</html>
```