Prompt specification
Build or customize the Three.js 3D scene "Balanced and unbalanced equation comparison" 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: Balanced and unbalanced equation comparison
Category: Science & Physics · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/balanced-and-unbalanced-equation-comparison-x1986
Scene Description Prompt:
Build a self-contained HTML scene with Three.js from cdnjs featuring atom conservation in balanced chemical reactions. Place two symbolic reaction boards with element counters on both sides; show why changing coefficients can balance atoms while changing molecular subscripts changes substances. A 3D cutaway view.
Working Three.js Starter (balanced-and-unbalanced-equation-comparison-x1986):
```html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Atom Conservation Lab</title>
<style>
:root {
color-scheme: dark;
--bg: #07131d;
--panel: rgba(11, 27, 40, 0.88);
--panel-border: rgba(164, 222, 236, 0.16);
--text: #edf9ff;
--muted: #91adba;
--cyan: #61e6d0;
--blue: #74b9ff;
--oxygen: #ff786f;
--amber: #ffc46b;
--green: #71efb1;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background:
radial-gradient(circle at 18% 12%, rgba(38, 111, 133, 0.25), transparent 34%),
radial-gradient(circle at 84% 18%, rgba(70, 84, 150, 0.20), transparent 32%),
linear-gradient(160deg, #091b28 0%, #061019 58%, #081722 100%);
color: var(--text);
font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
button {
font: inherit;
}
#stage {
position: fixed;
inset: 0 0 205px;
overflow: hidden;
}
#stage canvas {
display: block;
width: 100%;
height: 100%;
touch-action: none;
cursor: grab;
}
#stage canvas:active {
cursor: grabbing;
}
.hud {
position: fixed;
z-index: 5;
top: 24px;
left: 28px;
max-width: 650px;
pointer-events: none;
}
.eyebrow {
display: inline-flex;
align-items: center;
gap: 9px;
padding: 7px 11px;
border: 1px solid rgba(97, 230, 208, 0.28);
border-radius: 999px;
background: rgba(7, 25, 35, 0.68);
color: var(--cyan);
font-size: 11px;
font-weight: 800;
letter-spacing: 0.15em;
text-transform: uppercase;
backdrop-filter: blur(12px);
}
.eyebrow::before {
width: 7px;
height: 7px;
border-radius: 50%;
background: var(--cyan);
box-shadow: 0 0 14px var(--cyan);
content: "";
}
h1 {
max-width: 620px;
margin: 12px 0 7px;
font-size: clamp(27px, 3.1vw, 48px);
font-weight: 760;
letter-spacing: -0.045em;
line-height: 1.02;
}
.subtitle {
max-width: 580px;
margin: 0;
color: var(--muted);
font-size: clamp(12px, 1.2vw, 15px);
line-height: 1.5;
}
.legend {
position: fixed;
z-index: 5;
top: 27px;
right: 28px;
display: flex;
gap: 16px;
align-items: center;
padding: 10px 13px;
border: 1px solid var(--panel-border);
border-radius: 12px;
background: rgba(7, 23, 33, 0.66);
color: var(--muted);
font-size: 12px;
backdrop-filter: blur(12px);
}
.legend span {
display: inline-flex;
align-items: center;
gap: 7px;
}
.atom-dot {
width: 10px;
height: 10px;
border-radius: 50%;
box-shadow: 0 0 12px currentColor;
}
.atom-dot.h {
color: var(--cyan);
background: var(--cyan);
}
.atom-dot.o {
color: var(--oxygen);
background: var(--oxygen);
}
.view-tools {
position: fixed;
z-index: 6;
right: 28px;
bottom: 217px;
display: flex;
gap: 8px;
}
.tool-button {
padding: 9px 12px;
border: 1px solid var(--panel-border);
border-radius: 10px;
background: rgba(9, 28, 40, 0.82);
color: var(--text);
cursor: pointer;
transition: border-color 160ms ease, background 160ms ease;
backdrop-filter: blur(12px);
}
.tool-button:hover {
border-color: rgba(97, 230, 208, 0.45);
background: rgba(16, 45, 56, 0.92);
}
.controls {
position: fixed;
z-index: 10;
right: 18px;
bottom: 18px;
left: 18px;
width: min(1040px, calc(100% - 36px));
margin: auto;
}
.control-grid {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 12px;
}
.control-card {
min-width: 0;
padding: 15px 17px 14px;
border: 1px solid var(--panel-border);
border-radius: 16px;
background: var(--panel);
box-shadow: 0 20px 60px rgba(0, 0, 0, 0.30);
backdrop-filter: blur(18px);
}
.card-heading {
display: flex;
align-items: flex-start;
justify-content: space-between;
gap: 12px;
margin-bottom: 11px;
}
.card-kicker {
color: var(--cyan);
font-size: 10px;
font-weight: 850;
letter-spacing: 0.16em;
text-transform: uppercase;
}
.control-card:nth-child(2) .card-kicker {
color: var(--oxygen);
}
.card-heading h2 {
margin: 2px 0 0;
font-size: 16px;
font-weight: 750;
letter-spacing: -0.02em;
}
.card-copy {
margin: 3px 0 0;
color: var(--muted);
font-size: 11px;
line-height: 1.4;
}
.mini-button {
flex: 0 0 auto;
padding: 6px 9px;
border: 1px solid rgba(145, 173, 186, 0.25);
border-radius: 8px;
background: transparent;
color: var(--muted);
font-size: 10px;
cursor: pointer;
}
.mini-button:hover {
color: var(--text);
border-color: rgba(97, 230, 208, 0.45);
}
.bench-row {
display: flex;
flex-wrap: wrap;
gap: 8px;
align-items: center;
}
.stepper {
display: grid;
grid-template-columns: auto 28px 28px 28px;
align-items: center;
overflow: hidden;
border: 1px solid rgba(145, 173, 186, 0.18);
border-radius: 10px;
background: rgba(3, 14, 22, 0.48);
transition: border-color 150ms ease, background 150ms ease;
}
.stepper.selected {
border-color: rgba(97, 230, 208, 0.58);
background: rgba(26, 74, 75, 0.34);
}
.formula {
min-width: 53px;
padding: 8px 9px;
font-size: 17px;
font-weight: 760;
text-align: center;
white-space: nowrap;
}
.formula sub {
font-size: 0.72em;
}
.stepper button {
width: 28px;
height: 34px;
padding: 0;
border: 0;
border-left: 1px solid rgba(145, 173, 186, 0.14);
background: transparent;
color: var(--muted);
cursor: pointer;
}
.stepper button:hover:not(:disabled) {
color: var(--cyan);
background: rgba(97, 230, 208, 0.08);
}
.stepper button:disabled {
opacity: 0.25;
cursor: not-allowed;
}
.stepper output {
display: grid;
place-items: center;
height: 34px;
border-left: 1px solid rgba(145, 173, 186, 0.14);
color: var(--text);
font-size: 14px;
font-weight: 800;
}
.formula-toggle {
display: flex;
gap: 7px;
margin-bottom: 10px;
}
.formula-toggle button {
padding: 8px 11px;
border: 1px solid rgba(145, 173, 186, 0.18);
border-radius: 9px;
background: rgba(3, 14, 22, 0.44);
color: var(--muted);
cursor: pointer;
font-size: 12px;
font-weight: 700;
}
.formula-toggle button.active {
border-color: rgba(255, 120, 111, 0.55);
background: rgba(255, 120, 111, 0.11);
color: #ffd2ce;
}
.status {
min-height: 39px;
margin-top: 10px;
padding: 8px 10px;
border-left: 3px solid var(--amber);
border-radius: 6px;
background: rgba(255, 196, 107, 0.08);
color: #f6d9a7;
font-size: 11px;
line-height: 1.42;
}
.status.balanced {
border-left-color: var(--green);
background: rgba(113, 239, 177, 0.08);
color: #baf6d6;
}
.instructions {
margin: 8px 3px 0;
color: rgba(145, 173, 186, 0.75);
font-size: 10px;
text-align: center;
letter-spacing: 0.03em;
}
@media (max-width: 760px) {
#stage {
bottom: 330px;
}
.hud {
top: 13px;
left: 15px;
right: 15px;
}
.eyebrow {
padding: 5px 8px;
font-size: 9px;
}
h1 {
max-width: 330px;
margin-top: 8px;
font-size: 25px;
}
.subtitle {
display: none;
}
.legend {
top: 145px;
right: 12px;
padding: 7px 9px;
font-size: 10px;
}
.control-grid {
grid-template-columns: 1fr;
gap: 8px;
}
.control-card {
padding: 11px 12px;
}
.card-heading {
margin-bottom: 7px;
}
.card-copy {
display: none;
}
.stepper {
flex: 1 1 125px;
}
.status {
min-height: 32px;
margin-top: 7px;
padding: 6px 8px;
font-size: 10px;
}
.view-tools {
right: 13px;
bottom: 340px;
}
.controls {
right: 9px;
bottom: 9px;
left: 9px;
width: auto;
}
.instructions {
display: none;
}
}
</style>
</head>
<body>
<div id="stage" aria-label="Interactive three-dimensional chemistry cutaway"></div>
<header class="hud">
<div class="eyebrow">Atom Conservation Lab</div>
<h1>Balance the reaction.<br />Keep the substance.</h1>
<p class="subtitle">
Coefficients change how many molecules react. Subscripts change what each molecule is.
Orbit the cutaway, edit the equations, and watch every atom accounted for.
</p>
</header>
<div class="legend" aria-label="Element color legend">
<span><i class="atom-dot h"></i>Hydrogen</span>
<span><i class="atom-dot o"></i>Oxygen</span>
</div>
<div class="view-tools">
<button class="tool-button" id="resetView" type="button">Reset view</button>
</div>
<main class="controls">
<div class="control-grid">
<section class="control-card" aria-labelledby="coefficientTitle">
<div class="card-heading">
<div>
<div class="card-kicker">Board 01 · Coefficient bench</div>
<h2 id="coefficientTitle">Change the number in front</h2>
<p class="card-copy">The formula stays H₂O; only the molecule count changes.</p>
</div>
<button class="mini-button" id="resetCoefficients" type="button">Reset</button>
</div>
<div class="bench-row">
<div class="stepper selected" data-stepper="h2">
<span class="formula">H<sub>2</sub></span>
<button type="button" data-key="h2" data-delta="-1" aria-label="Decrease H₂ coefficient">−</button>
<output data-output="h2">1</output>
<button type="button" data-key="h2" data-delta="1" aria-label="Increase H₂ coefficient">+</button>
</div>
<div class="stepper" data-stepper="o2">
<span class="formula">O<sub>2</sub></span>
<button type="button" data-key="o2" data-delta="-1" aria-label="Decrease O₂ coefficient">−</button>
<output data-output="o2">1</output>
<button type="button" data-key="o2" data-delta="1" aria-label="Increase O₂ coefficient">+</button>
</div>
<div class="stepper" data-stepper="h2o">
<span class="formula">H<sub>2</sub>O</span>
<button type="button" data-key="h2o" data-delta="-1" aria-label="Decrease H₂O coefficient">−</button>
<output data-output="h2o">1</output>
<button type="button" data-key="h2o" data-delta="1" aria-label="Increase H₂O coefficient">+</button>
</div>
<button class="mini-button" id="showBalance" type="button">Balanced example</button>
</div>
<div class="status" id="balanceStatus" role="status" aria-live="polite"></div>
</section>
<section class="control-card" aria-labelledby="subscriptTitle">
<div class="card-heading">
<div>
<div class="card-kicker">Board 02 · Subscript bench</div>
<h2 id="subscriptTitle">Change the product formula</h2>
<p class="card-copy">H₂O and H₂O₂ use the same elements, but they are different substances.</p>
</div>
</div>
<div class="formula-toggle" role="group" aria-label="Choose the product substance">
<button type="button" data-formula="water">Water · H₂O</button>
<button type="button" data-formula="peroxide" class="active">Peroxide · H₂O₂</button>
</div>
<div class="status balanced" id="substanceStatus" role="status" aria-live="polite"></div>
</section>
</div>
<div class="instructions">Drag to orbit · Scroll or pinch to zoom · Click a molecule to select it</div>
</main>
<script type="module">
import * as THREE from "https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
const stage = document.querySelector("#stage");
const scene = new THREE.Scene();
scene.fog = new THREE.FogExp2(0x07131d, 0.026);
const camera = new THREE.PerspectiveCamera(42, 1, 0.1, 80);
camera.position.set(0, 0.2, 13);
const renderer = new THREE.WebGLRenderer({
antialias: true,
alpha: true,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setClearColor(0x07131d, 0);
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.15;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.domElement.setAttribute("aria-label", "Interactive atom conservation cutaway");
stage.appendChild(renderer.domElement);
const hemisphere = new THREE.HemisphereLight(0xb9efff, 0x12202c, 1.75);
scene.add(hemisphere);
const keyLight = new THREE.DirectionalLight(0xffffff, 3.2);
keyLight.position.set(-5, 8, 9);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(1536, 1536);
keyLight.shadow.camera.left = -12;
keyLight.shadow.camera.right = 12;
keyLight.shadow.camera.top = 10;
keyLight.shadow.camera.bottom = -10;
scene.add(keyLight);
const cyanLight = new THREE.PointLight(0x52e8db, 25, 18, 2);
cyanLight.position.set(-7, 2, 6);
scene.add(cyanLight);
const coralLight = new THREE.PointLight(0xff776e, 22, 18, 2);
coralLight.position.set(7, 1, 5);
scene.add(coralLight);
const materials = {
board: new THREE.MeshStandardMaterial({
color: 0x102837,
roughness: 0.62,
metalness: 0.18
}),
frame: new THREE.MeshStandardMaterial({
color: 0x254553,
roughness: 0.42,
metalness: 0.42
}),
cyan: new THREE.MeshStandardMaterial({
color: 0x61e6d0,
roughness: 0.34,
metalness: 0.12,
emissive: 0x123d39,
emissiveIntensity: 0.7
}),
blue: new THREE.MeshStandardMaterial({
color: 0x74b9ff,
roughness: 0.38,
metalness: 0.16,
emissive: 0x173451,
emissiveIntensity: 0.55
}),
coral: new THREE.MeshStandardMaterial({
color: 0xff786f,
roughness: 0.38,
metalness: 0.12,
emissive: 0x4a1d1d,
emissiveIntensity: 0.55
}),
hydrogen: new THREE.MeshStandardMaterial({
color: 0xc7f8ff,
roughness: 0.27,
metalness: 0.05,
emissive: 0x397a87,
emissiveIntensity: 0.65
}),
oxygen: new THREE.MeshStandardMaterial({
color: 0xff6e67,
roughness: 0.3,
metalness: 0.04,
emissive: 0x6c2422,
emissiveIntensity: 0.65
}),
bond: new THREE.MeshStandardMaterial({
color: 0xb7cad2,
roughness: 0.35,
metalness: 0.2
})
};
const hydrogenGeometry = new THREE.SphereGeometry(1, 24, 16);
const oxygenGeometry = new THREE.SphereGeometry(1, 24, 16);
function roundedRect(ctx, x, y, w, h, r) {
r = Math.min(r, 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 createCanvasPanel(width, height, draw) {
const canvas = document.createElement("canvas");
canvas.width = 1024;
canvas.height = Math.max(128, Math.round(1024 * height / width));
const ctx = canvas.getContext("2d");
const texture = new THREE.CanvasTexture(canvas);
texture.colorSpace = THREE.SRGBColorSpace;
texture.minFilter = THREE.LinearFilter;
texture.magFilter = THREE.LinearFilter;
const panel = {
canvas,
ctx,
texture,
redraw(nextDraw = draw) {
ctx.clearRect(0, 0, canvas.width, canvas.height);
nextDraw(ctx, canvas.width, canvas.height);
texture.needsUpdate = true;
}
};
panel.redraw();
return panel;
}
function addTextPanel(parent, width, height, draw, x, y, z = 0.22) {
const panel = createCanvasPanel(width, height, draw);
const mesh = new THREE.Mesh(
new THREE.PlaneGeometry(width, height),
new THREE.MeshBasicMaterial({
map: panel.texture,
transparent: true,
depthWrite: false,
side: THREE.DoubleSide
})
);
mesh.position.set(x, y, z);
mesh.renderOrder = 3;
parent.add(mesh);
return { mesh, panel };
}
function drawHeader(ctx, w, h, number, title, subtitle, accent) {
ctx.clearRect(0, 0, w, h);
roundedRect(ctx, 6, 6, w - 12, h - 12, h * 0.16);
ctx.fillStyle = "rgba(7, 24, 34, 0.82)";
ctx.fill();
ctx.fillStyle = accent;
roundedRect(ctx, 24, h * 0.24, 8, h * 0.52, 4);
ctx.fill();
ctx.textBaseline = "middle";
ctx.textAlign = "left";
ctx.fillStyle = accent;
ctx.font = `800 ${Math.round(h * 0.19)}px ui-sans-serif, system-ui, sans-serif`;
ctx.fillText(number, 54, h * 0.34);
ctx.fillStyle = "#f0fbff";
ctx.font = `800 ${Math.round(h * 0.24)}px ui-sans-serif, system-ui, sans-serif`;
ctx.fillText(title, 54, h * 0.64);
ctx.fillStyle = "#91adba";
ctx.font = `500 ${Math.round(h * 0.145)}px ui-sans-serif, system-ui, sans-serif`;
ctx.fillText(subtitle, 258, h * 0.64);
}
function drawFormula(ctx, w, h, text, accent = "#edf9ff") {
ctx.clearRect(0, 0, w, h);
ctx.textAlign = "center";
ctx.textBaseline = "middle";
ctx.fillStyle = accent;
ctx.font = `800 ${Math.round(h * 0.48)}px ui-sans-serif, system-ui, sans-serif`;
ctx.fillText(text, w / 2, h * 0.43);
ctx.fillStyle = "rgba(145, 173, 186, 0.65)";
ctx.font = `700 ${Math.round(h * 0.105)}px ui-sans-serif, system-ui, sans-serif`;
ctx.fillText("COEFFICIENT", w / 2, h * 0.78);
}
function drawSign(ctx, w, h, text, color) {
ctx.clearRect(0, 0, w, h);
ctx.textAlign = "center";
ctx.textBaseline = "middle";
ctx.fillStyle = color;
ctx.font = `700 ${Math.round(h * 0.55)}px ui-sans-serif, system-ui, sans-serif`;
ctx.fillText(text, w / 2, h / 2);
}
function drawCounter(ctx, w, h, title, hydrogen, oxygen, balanced) {
ctx.clearRect(0, 0, w, h);
roundedRect(ctx, 8, 8, w - 16, h - 16, 18);
ctx.fillStyle = "rgba(4, 18, 28, 0.94)";
ctx.fill();
ctx.strokeStyle = balanced
? "rgba(113, 239, 177, 0.48)"
: "rgba(255, 196, 107, 0.44)";
ctx.lineWidth = 3;
ctx.stroke();
ctx.textAlign = "left";
ctx.textBaseline = "middle";
ctx.fillStyle = balanced ? "#71efb1" : "#ffc46b";
ctx.font = `800 ${Math.round(h * 0.105)}px ui-sans-serif, system-ui, sans-serif`;
ctx.fillText(title.toUpperCase(), 30, h * 0.19);
const rows = [
{ label: "H", count: hydrogen, color: "#61e6d0", y: 0.47 },
{ label: "O", count: oxygen, color: "#ff786f", y: 0.76 }
];
for (const row of rows) {
ctx.beginPath();
ctx.arc(49, h * row.y, 18, 0, Math.PI * 2);
ctx.fillStyle = row.color;
ctx.fill();
ctx.fillStyle = "#eafaff";
ctx.font = `800 ${Math.round(h * 0.145)}px ui-sans-serif, system-ui, sans-serif`;
ctx.fillText(row.label, 82, h * row.y);
ctx.textAlign = "right";
ctx.font = `800 ${Math.round(h * 0.21)}px ui-monospace, SFMono-Regular, Consolas, monospace`;
ctx.fillText(String(row.count), w - 35, h * row.y);
ctx.textAlign = "left";
}
ctx.textAlign = "center";
ctx.fillStyle = balanced ? "#a8f4ce" : "#e8c990";
ctx.font = `700 ${Math.round(h * 0.09)}px ui-sans-serif, system-ui, sans-serif`;
ctx.fillText(
balanced ? "INVENTORY MATCHES" : "COUNT THE DIFFERENCE",
w / 2,
h * 0.94
);
}
function drawIdentity(ctx, w, h, text, color) {
ctx.clearRect(0, 0, w, h);
roundedRect(ctx, 5, 5, w - 10, h - 10, h * 0.42);
ctx.fillStyle = "rgba(3, 14, 22, 0.75)";
ctx.fill();
ctx.strokeStyle = color;
ctx.lineWidth = 2;
ctx.stroke();
ctx.textAlign = "center";
ctx.textBaseline = "middle";
ctx.fillStyle = color;
ctx.font = `800 ${Math.round(h * 0.38)}px ui-sans-serif, system-ui, sans-serif`;
ctx.fillText(text, w / 2, h / 2);
}
function bondBetween(a, b, radius = 0.035) {
const start = new THREE.Vector3(...a);
const end = new THREE.Vector3(...b);
const direction = end.clone().sub(start);
const length = direction.length();
const mesh = new THREE.Mesh(
new THREE.CylinderGeometry(radius, radius, length, 12),
materials.bond
);
mesh.position.copy(start).add(end).multiplyScalar(0.5);
mesh.quaternion.setFromUnitVectors(
new THREE.Vector3(0, 1, 0),
direction.normalize()
);
mesh.castShadow = true;
return mesh;
}
function makeAtom(material, radius, position) {
const mesh = new THREE.Mesh(
material === materials.hydrogen ? hydrogenGeometry : oxygenGeometry,
material
);
mesh.scale.setScalar(radius);
mesh.position.set(...position);
mesh.castShadow = true;
mesh.receiveShadow = true;
return mesh;
}
function makeMolecule(type) {
const group = new THREE.Group();
if (type === "h2") {
group.add(makeAtom(materials.hydrogen, 0.115, [-0.19, 0, 0]));
group.add(makeAtom(materials.hydrogen, 0.115, [0.19, 0, 0]));
group.add(bondBetween([-0.13, 0, 0], [0.13, 0, 0]));
}
if (type === "o2") {
group.add(makeAtom(materials.oxygen, 0.17, [-0.21, 0, 0]));
group.add(makeAtom(materials.oxygen, 0.17, [0.21, 0, 0]));
group.add(bondBetween([-0.15, 0, 0], [0.15, 0, 0], 0.045));
}
if (type === "h2o") {
group.add(makeAtom(materials.oxygen, 0.18, [0, 0.04, 0]));
group.add(makeAtom(materials.hydrogen, 0.11, [-0.3, -0.18, 0]));
group.add(makeAtom(materials.hydrogen, 0.11, [0.3, -0.18, 0]));
group.add(bondBetween([0, 0.02, 0], [-0.24, -0.15, 0]));
group.add(bondBetween([0, 0.02, 0], [0.24, -0.15, 0]));
}
if (type === "h2o2") {
group.add(makeAtom(materials.oxygen, 0.175, [-0.19, 0.04, 0]));
group.add(makeAtom(materials.oxygen, 0.175, [0.19, 0.04, 0]));
group.add(makeAtom(materials.hydrogen, 0.105, [-0.45, -0.17, 0]));
group.add(makeAtom(materials.hydrogen, 0.105, [0.45, -0.17, 0]));
group.add(bondBetween([-0.12, 0.04, 0], [0.12, 0.04, 0], 0.045));
group.add(bondBetween([-0.27, -0.02, 0], [-0.39, -0.14, 0]));
group.add(bondBetween([0.27, -0.02, 0], [0.39, -0.14, 0]));
}
return group;
}
const pickableMolecules = [];
function makeMoleculeHolder(type, actionKey = null, scale = 0.72) {
const holder = new THREE.Group();
const model = makeMolecule(type);
model.scale.setScalar(scale);
holder.add(model);
holder.userData.actionKey = actionKey;
holder.userData.baseScale = scale;
holder.userData.model = model;
if (actionKey) {
pickableMolecules.push(holder);
}
return holder;
}
function buildShell(accentMaterial, title, subtitle, number) {
const group = new THREE.Group();
const back = new THREE.Mesh(
new THREE.BoxGeometry(5.55, 4.5, 0.24),
materials.board
);
back.position.z = -0.18;
back.castShadow = true;
back.receiveShadow = true;
group.add(back);
const top = new THREE.Mesh(
new THREE.BoxGeometry(5.75, 0.17, 0.48),
accentMaterial
);
top.position.set(0, 2.22, 0.01);
top.castShadow = true;
group.add(top);
const bottom = new THREE.Mesh(
new THREE.BoxGeometry(5.75, 0.17, 0.48),
materials.frame
);
bottom.position.set(0, -2.22, 0.01);
bottom.castShadow = true;
group.add(bottom);
for (const x of [-2.77, 2.77]) {
const side = new THREE.Mesh(
new THREE.BoxGeometry(0.16, 4.3, 0.46),
materials.frame
);
side.position.set(x, 0, 0.01);
side.castShadow = true;
group.add(side);
}
const shelf = new THREE.Mesh(
new THREE.BoxGeometry(5.22, 0.09, 0.42),
materials.frame
);
shelf.position.set(0, -0.62, 0.04);
shelf.castShadow = true;
group.add(shelf);
const divider = new THREE.Mesh(
new THREE.BoxGeometry(0.035, 1.82, 0.2),
accentMaterial
);
divider.position.set(0.18, 0.33, 0.16);
group.add(divider);
// Exposed section layers at the cut edge.
const cutLayers = [
{ z: -0.04, material: materials.blue },
{ z: 0.10, material: materials.cyan },
{ z: 0.24, material: materials.coral }
];
cutLayers.forEach((layer, i) => {
const slab = new THREE.Mesh(
new THREE.BoxGeometry(0.15, 0.72 + i * 0.13, 0.07),
layer.material
);
slab.position.set(2.87, 0.35 + i * 0.06, layer.z);
slab.castShadow = true;
group.add(slab);
});
addTextPanel(
group,
5.2,
0.7,
(ctx, w, h) => drawHeader(
ctx,
w,
h,
number,
title,
subtitle,
accentMaterial === materials.coral ? "#ff9189" : "#61e6d0"
),
0,
1.76,
0.27
);
const led = new THREE.Mesh(
new THREE.SphereGeometry(0.07, 16, 12),
new THREE.MeshStandardMaterial({
color: 0xffc46b,
emissive: 0xff7c35,
emissiveIntensity: 1.2
})
);
led.position.set(2.47, 1.95, 0.4);
group.add(led);
group.userData.led = led;
return group;
}
function addCounterBay(parent, x) {
const box = new THREE.Mesh(
new THREE.BoxGeometry(2.28, 1.25, 0.18),
new THREE.MeshStandardMaterial({
color: 0x071923,
roughness: 0.72,
metalness: 0.08
})
);
box.position.set(x, -1.34, 0.15);
box.castShadow = true;
box.receiveShadow = true;
parent.add(box);
return addTextPanel(
parent,
2.2,
1.17,
() => {},
x,
-1.34,
0.26
);
}
function createPool(parent, x, type, actionKey = null, count = 1, scale = 0.65) {
const slot = new THREE.Group();
slot.position.set(x, 0.1, 0.58);
parent.add(slot);
const pool = [];
for (let i = 0; i < count; i++) {
const holder = makeMoleculeHolder(type, actionKey, scale);
holder.visible = i < count;
slot.add(holder);
pool.push(holder);
}
return { slot, pool };
}
const boardA = {
root: buildShell(
materials.cyan,
"COEFFICIENT BENCH",
"Change the small number in front",
"01"
),
coefficients: { h2: 1, o2: 1, h2o: 1 },
selected: "h2"
};
boardA.root.position.x = -3.05;
addTextPanel(
boardA.root, 0.55, 0.52,
(ctx, w, h) => drawSign(ctx, w, h, "+", "#61e6d0"),
-1.28, 0.93, 0.32
);
addTextPanel(
boardA.root, 0.75, 0.52,
(ctx, w, h) => drawSign(ctx, w, h, "→", "#edf9ff"),
0.35, 0.93, 0.32
);
boardA.formulas = {
h2: addTextPanel(
boardA.root, 1.03, 0.55,
() => {},
-1.95, 0.94, 0.34
),
o2: addTextPanel(
boardA.root, 1.03, 0.55,
() => {},
-0.68, 0.94, 0.34
),
h2o: addTextPanel(
boardA.root, 1.2, 0.55,
() => {},
1.57, 0.94, 0.34
)
};
boardA.counters = {
left: addCounterBay(boardA.root, -1.32),
right: addCounterBay(boardA.root, 1.32)
};
boardA.models = {
h2: createPool(boardA.root, -1.95, "h2", "h2", 3),
o2: createPool(boardA.root, -0.68, "o2", "o2", 3),
h2o: createPool(boardA.root, 1.57, "h2o", "h2o", 3)
};
const boardB = {
root: buildShell(
materials.coral,
"SUBSCRIPT BENCH",
"Change what the molecule is",
"02"
),
formula: "peroxide"
};
boardB.root.position.x = 3.05;
addTextPanel(
boardB.root, 0.55, 0.52,
(ctx, w, h) => drawSign(ctx, w, h, "+", "#ff9189"),
-1.28, 0.93, 0.32
);
addTextPanel(
boardB.root, 0.75, 0.52,
(ctx, w, h) => drawSign(ctx, w, h, "→", "#edf9ff"),
0.35, 0.93, 0.32
);
boardB.formulaPanel = addTextPanel(
boardB.root, 1.25, 0.55,
() => {},
1.57, 0.94, 0.34
);
boardB.identity = addTextPanel(
boardB.root, 2.2, 0.25,
() => {},
1.57, -0.42, 0.32
);
boardB.counters = {
left: addCounterBay(boardB.root, -1.32),
right: addCounterBay(boardB.root, 1.32)
};
boardB.leftModels = {
h2: createPool(boardB.root, -1.95, "h2", null, 1, 0.68),
o2: createPool(boardB.root, -0.68, "o2", null, 1, 0.68)
};
boardB.productModels = {
water: createPool(boardB.root, 1.57, "h2o", null, 1, 0.72),
peroxide: createPool(boardB.root, 1.57, "h2o2", null, 1, 0.68)
};
const balanceStatus = document.querySelector("#balanceStatus");
const substanceStatus = document.querySelector("#substanceStatus");
function coefficientCounts() {
const c = boardA.coefficients;
return {
left: { H: 2 * c.h2, O: 2 * c.o2 },
right: { H: 2 * c.h2o, O: c.h2o }
};
}
function setPoolVisibility(pool, count) {
const positions = [
[0, 0],
[-0.27, 0],
[-0.3, 0.015],
[-0.3, 0.015]
];
pool.pool.forEach((holder, index) => {
holder.visible = index < count;
holder.position.x = positions[Math.min(index, positions.length - 1)][0];
holder.position.y = positions[Math.min(index, positions.length - 1)][1];
});
}
function updateBoardA() {
const c = boardA.coefficients;
const counts = coefficientCounts();
const balanced =
counts.left.H === counts.right.H &&
counts.left.O === counts.right.O;
const formulaText = {
h2: `${c.h2} H₂`,
o2: `${c.o2} O₂`,
h2o: `${c.h2o} H₂O`
};
for (const key of ["h2", "o2", "h2o"]) {
boardA.formulas[key].panel.redraw((ctx, w, h) =>
drawFormula(ctx, w, h, formulaText[key])
);
setPoolVisibility(boardA.models[key], c[key]);
boardA.models[key].pool.forEach((holder) => {
const selected = boardA.selected === key;
holder.userData.model.scale.setScalar(
holder.userData.baseScale * (selected ? 1.13 : 1)
);
});
}
boardA.counters.left.panel.redraw((ctx, w, h) =>
drawCounter(ctx, w, h, "Reactants", counts.left.H, counts.left.O, balanced)
);
boardA.counters.right.panel.redraw((ctx, w, h) =>
drawCounter(ctx, w, h, "Products", counts.right.H, counts.right.O, balanced)
);
boardA.root.userData.led.material.color.set(
balanced ? 0x71efb1 : 0xffc46b
);
boardA.root.userData.led.material.emissive.set(
balanced ? 0x2a8b5c : 0xff7c35
);
document.querySelectorAll("[data-output]").forEach((output) => {
output.value = c[output.dataset.output];
output.textContent = c[output.dataset.output];
});
document.querySelectorAll("[data-stepper]").forEach((stepper) => {
stepper.classList.toggle("selected", stepper.dataset.stepper === boardA.selected);
});
document.querySelectorAll(".stepper button").forEach((button) => {
const key = button.dataset.key;
const delta = Number(button.dataset.delta);
const value = c[key];
button.disabled = delta < 0 ? value <= 1 : value >= 4;
});
if (balanced) {
balanceStatus.className = "status balanced";
balanceStatus.textContent =
`Balanced: ${counts.left.H} H and ${counts.left.O} O on each side. ` +
`The coefficients changed the number of H₂O molecules, not the substance itself.`;
} else {
const differences = [];
if (counts.right.H < counts.left.H) {
differences.push(
`${counts.left.H - counts.right.H} H atom${counts.left.H - counts.right.H === 1 ? "" : "s"} missing on the right`
);
} else if (counts.right.H > counts.left.H) {
differences.push(
`${counts.right.H - counts.left.H} extra H atom${counts.right.H - counts.left.H === 1 ? "" : "s"} on the right`
);
}
if (counts.right.O < counts.left.O) {
differences.push(
`${counts.left.O - counts.right.O} O atom${counts.left.O - counts.right.O === 1 ? "" : "s"} missing on the right`
);
} else if (counts.right.O > counts.left.O) {
differences.push(
`${counts.right.O - counts.left.O} extra O atom${counts.right.O - counts.left.O === 1 ? "" : "s"} on the right`
);
}
balanceStatus.className = "status";
balanceStatus.textContent =
`Left: ${counts.left.H} H / ${counts.left.O} O. ` +
`Right: ${counts.right.H} H / ${counts.right.O} O. ` +
differences.join("; ") +
". Edit the leading coefficients only.";
}
}
function updateBoardB() {
const peroxide = boardB.formula === "peroxide";
const left = { H: 2, O: 2 };
const right = peroxide ? { H: 2, O: 2 } : { H: 2, O: 1 };
const balanced = peroxide;
boardB.formulaPanel.panel.redraw((ctx, w, h) =>
drawFormula(
ctx,
w,
h,
peroxide ? "H₂O₂" : "H₂O",
peroxide ? "#ff9189" : "#edf9ff"
)
);
boardB.identity.panel.redraw((ctx, w, h) =>
drawIdentity(
ctx,
w,
h,
peroxide ? "HYDROGEN PEROXIDE · DIFFERENT SUBSTANCE" : "WATER · H₂O",
peroxide ? "#ff9189" : "#74b9ff"
)
);
boardB.productModels.water.pool[0].visible = !peroxide;
boardB.productModels.peroxide.pool[0].visible = peroxide;
boardB.counters.left.panel.redraw((ctx, w, h) =>
drawCounter(ctx, w, h, "Reactants", left.H, left.O, balanced)
);
boardB.counters.right.panel.redraw((ctx, w, h) =>
drawCounter(ctx, w, h, "Products", right.H, right.O, balanced)
);
boardB.root.userData.led.material.color.set(
peroxide ? 0x71efb1 : 0xffc46b
);
boardB.root.userData.led.material.emissive.set(
peroxide ? 0x2a8b5c : 0xff7c35
);
document.querySelectorAll("[data-formula]").forEach((button) => {
button.classList.toggle("active", button.dataset.formula === boardB.formula);
});
if (peroxide) {
substanceStatus.className = "status balanced";
substanceStatus.textContent =
"Atom counts match: 2 H and 2 O on each side. But H₂O₂ is hydrogen peroxide, not water. " +
"Balanced atoms do not guarantee the same product.";
} else {
substanceStatus.className = "status";
substanceStatus.textContent =
"Water is H₂O, with one oxygen per molecule. With these coefficients the right side is " +
"short one O atom. The balanced coefficient equation is 2H₂ + O₂ → 2H₂O.";
}
}
document.querySelectorAll(".stepper button").forEach((button) => {
button.addEventListener("click", () => {
const key = button.dataset.key;
const delta = Number(button.dataset.delta);
const next = THREE.MathUtils.clamp(
boardA.coefficients[key] + delta,
1,
4
);
boardA.coefficients[key] = next;
boardA.selected = key;
updateBoardA();
});
});
document.querySelectorAll("[data-stepper]").forEach((stepper) => {
stepper.addEventListener("click", (event) => {
if (event.target.closest("button")) return;
boardA.selected = stepper.dataset.stepper;
updateBoardA();
});
});
document.querySelectorAll("[data-formula]").forEach((button) => {
button.addEventListener("click", () => {
boardB.formula = button.dataset.formula;
updateBoardB();
});
});
document.querySelector("#resetCoefficients").addEventListener("click", () => {
boardA.coefficients = { h2: 1, o2: 1, h2o: 1 };
boardA.selected = "h2";
updateBoardA();
});
document.querySelector("#showBalance").addEventListener("click", () => {
boardA.coefficients = { h2: 2, o2: 1, h2o: 2 };
boardA.selected = "h2o";
updateBoardA();
});
scene.add(boardA.root, boardB.root);
updateBoardA();
updateBoardB();
// Decorative depth cues behind the floating cutaway boards.
const grid = new THREE.GridHelper(30, 30, 0x4b9aa2, 0x173a48);
grid.position.y = -2.38;
grid.position.z = -0.8;
grid.material.transparent = true;
grid.material.opacity = 0.16;
scene.add(grid);
const floor = new THREE.Mesh(
new THREE.PlaneGeometry(30, 30),
new THREE.ShadowMaterial({ opacity: 0.2 })
);
floor.rotation.x = -Math.PI / 2;
floor.position.y = -2.37;
floor.position.z = -0.8;
floor.receiveShadow = true;
scene.add(floor);
const particleCount = 100;
const particlePositions = new Float32Array(particleCount * 3);
for (let i = 0; i < particleCount; i++) {
particlePositions[i * 3] = (Math.random() - 0.5) * 23;
particlePositions[i * 3 + 1] = (Math.random() - 0.5) * 12;
particlePositions[i * 3 + 2] = -1.5 - Math.random() * 5;
}
const particleGeometry = new THREE.BufferGeometry();
particleGeometry.setAttribute(
"position",
new THREE.BufferAttribute(particlePositions, 3)
);
const particles = new THREE.Points(
particleGeometry,
new THREE.PointsMaterial({
color: 0x78c8d4,
size: 0.035,
transparent: true,
opacity: 0.34,
depthWrite: false
})
);
scene.add(particles);
// Direct camera controls: no external controls addon.
const cameraState = {
yaw: 0,
pitch: 0,
distance: 13,
targetDistance: 13,
target: new THREE.Vector3(0, 0, 0)
};
function updateCamera() {
const horizontal = Math.cos(cameraState.pitch) * cameraState.distance;
camera.position.set(
cameraState.target.x + Math.sin(cameraState.yaw) * horizontal,
cameraState.target.y + Math.sin(cameraState.pitch) * cameraState.distance,
cameraState.target.z + Math.cos(cameraState.yaw) * horizontal
);
camera.lookAt(cameraState.target);
}
function resetView() {
cameraState.yaw = 0;
cameraState.pitch = 0;
cameraState.distance = cameraState.targetDistance;
updateCamera();
}
document.querySelector("#resetView").addEventListener("click", resetView);
let pointerDown = false;
let pointerMoved = false;
let previousX = 0;
let previousY = 0;
const raycaster = new THREE.Raycaster();
const pointer = new THREE.Vector2();
renderer.domElement.addEventListener("pointerdown", (event) => {
pointerDown = true;
pointerMoved = false;
previousX = event.clientX;
previousY = event.clientY;
renderer.domElement.setPointerCapture(event.pointerId);
});
renderer.domElement.addEventListener("pointermove", (event) => {
if (!pointerDown) return;
const dx = event.clientX - previousX;
const dy = event.clientY - previousY;
if (Math.abs(dx) + Math.abs(dy) > 1.5) {
pointerMoved = true;
}
cameraState.yaw -= dx * 0.005;
cameraState.pitch = THREE.MathUtils.clamp(
cameraState.pitch + dy * 0.0035,
-0.34,
0.34
);
previousX = event.clientX;
previousY = event.clientY;
updateCamera();
});
renderer.domElement.addEventListener("pointerup", (event) => {
pointerDown = false;
if (!pointerMoved) {
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);
const visiblePickables = pickableMolecules.filter((item) => item.visible);
const hits = raycaster.intersectObjects(visiblePickables, true);
if (hits.length) {
let object = hits[0].object;
while (object && !object.userData.actionKey) {
object = object.parent;
}
if (object?.userData.actionKey) {
boardA.selected = object.userData.actionKey;
updateBoardA();
}
}
}
});
renderer.domElement.addEventListener("pointercancel", () => {
pointerDown = false;
});
renderer.domElement.addEventListener("wheel", (event) => {
event.preventDefault();
cameraState.targetDistance = THREE.MathUtils.clamp(
cameraState.targetDistance + event.deltaY * 0.008,
8.5,
19
);
}, { passive: false });
let narrowLayout = false;
function resize() {
const width = Math.max(1, stage.clientWidth);
const height = Math.max(1, stage.clientHeight);
const aspect = width / height;
renderer.setSize(width, height, false);
camera.aspect = aspect;
camera.updateProjectionMatrix();
narrowLayout = aspect < 1.05;
if (narrowLayout) {
boardA.root.position.set(0, 2.65, 0);
boardB.root.position.set(0, -2.65, 0);
boardA.root.scale.setScalar(0.76);
boardB.root.scale.setScalar(0.76);
cameraState.target.set(0, 0, 0);
cameraState.targetDistance = 15.8;
grid.position.y = -4.32;
floor.position.y = -4.31;
} else {
boardA.root.position.set(-3.05, 0, 0);
boardB.root.position.set(3.05, 0, 0);
boardA.root.scale.setScalar(1);
boardB.root.scale.setScalar(1);
cameraState.target.set(0, 0, 0);
cameraState.targetDistance = Math.max(11.8, 14.2 / aspect);
grid.position.y = -2.38;
floor.position.y = -2.37;
}
resetView();
}
const resizeObserver = new ResizeObserver(resize);
resizeObserver.observe(stage);
window.addEventListener("resize", resize);
resize();
const clock = new THREE.Clock();
function animate() {
const elapsed = clock.getElapsedTime();
particles.rotation.y = elapsed * 0.008;
particles.position.y = Math.sin(elapsed * 0.12) * 0.08;
renderer.render(scene, camera);
requestAnimationFrame(animate);
}
animate();
</script>
</body>
</html>
```