Prompt specification
Build or customize the Three.js 3D scene "Rate-law parameter exploration tool" 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: Rate-law parameter exploration tool
Category: Science & Physics · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/rate-law-parameter-exploration-tool-x1992
Scene Description Prompt:
Explore predefined reaction-rate laws with synthetic constants and concentration curves, separating rate order from stoichiometry. Low-poly Three.js from cdnjs, one HTML file.
Working Three.js Starter (rate-law-parameter-exploration-tool-x1992):
```html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Reaction Rate Lab</title>
<style>
:root {
color-scheme: dark;
--bg: #070b12;
--panel: rgba(15, 21, 32, 0.88);
--panel-strong: #111a27;
--line: rgba(155, 190, 215, 0.15);
--text: #eaf3fa;
--muted: #8da0b3;
--cyan: #64e6d3;
--cyan-soft: rgba(100, 230, 211, 0.14);
--orange: #ffad5c;
--orange-soft: rgba(255, 173, 92, 0.14);
--purple: #ae8cff;
--purple-soft: rgba(174, 140, 255, 0.14);
--red: #ff7d78;
}
* {
box-sizing: border-box;
}
html,
body {
margin: 0;
min-height: 100%;
background:
radial-gradient(circle at 48% 18%, #17283a 0, transparent 36%),
radial-gradient(circle at 90% 90%, #171329 0, transparent 30%),
var(--bg);
color: var(--text);
font-family:
Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
button,
input {
font: inherit;
}
button {
color: inherit;
}
.app {
min-height: 100vh;
display: grid;
grid-template-rows: auto minmax(0, 1fr);
}
.topbar {
min-height: 72px;
display: flex;
align-items: center;
justify-content: space-between;
gap: 20px;
padding: 12px 20px;
border-bottom: 1px solid var(--line);
background: rgba(7, 11, 18, 0.76);
backdrop-filter: blur(16px);
z-index: 5;
}
.brand {
display: flex;
align-items: center;
gap: 12px;
}
.brand-mark {
width: 42px;
height: 42px;
display: grid;
place-items: center;
border: 1px solid rgba(100, 230, 211, 0.35);
border-radius: 13px;
background:
linear-gradient(145deg, rgba(100, 230, 211, 0.18), transparent),
#101b29;
box-shadow: 0 0 26px rgba(100, 230, 211, 0.1);
}
.brand-mark svg {
width: 26px;
height: 26px;
}
.eyebrow {
display: block;
margin-bottom: 2px;
color: var(--cyan);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.18em;
}
h1 {
margin: 0;
font-size: clamp(18px, 2vw, 24px);
line-height: 1.1;
letter-spacing: -0.03em;
}
.header-note {
padding: 8px 12px;
border: 1px solid rgba(174, 140, 255, 0.22);
border-radius: 999px;
background: var(--purple-soft);
color: #d8c9ff;
font-size: 12px;
font-weight: 700;
}
.header-status {
display: flex;
align-items: center;
gap: 7px;
color: var(--muted);
font-size: 11px;
font-weight: 700;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.header-status span {
width: 7px;
height: 7px;
border-radius: 50%;
background: var(--cyan);
box-shadow: 0 0 12px var(--cyan);
}
.workspace {
min-height: 0;
display: grid;
grid-template-columns: 310px minmax(430px, 1fr) 370px;
gap: 12px;
padding: 12px;
}
.panel {
border: 1px solid var(--line);
border-radius: 18px;
background: var(--panel);
box-shadow:
0 18px 50px rgba(0, 0, 0, 0.2),
inset 0 1px rgba(255, 255, 255, 0.025);
backdrop-filter: blur(14px);
}
.controls,
.charts {
min-height: 0;
overflow: auto;
scrollbar-width: thin;
scrollbar-color: #32445a transparent;
}
.controls {
padding: 18px;
}
.section {
padding-bottom: 18px;
margin-bottom: 18px;
border-bottom: 1px solid var(--line);
}
.section:last-child {
margin-bottom: 0;
border-bottom: 0;
}
.section-title {
display: flex;
align-items: center;
justify-content: space-between;
gap: 10px;
margin: 0 0 12px;
color: #cbd8e4;
font-size: 11px;
font-weight: 800;
letter-spacing: 0.12em;
text-transform: uppercase;
}
.section-title small {
color: var(--muted);
font-size: 9px;
letter-spacing: 0.08em;
}
.preset-grid {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 7px;
}
.law-btn {
min-height: 54px;
padding: 8px 4px;
border: 1px solid var(--line);
border-radius: 11px;
background: #101824;
cursor: pointer;
transition: 160ms ease;
}
.law-btn:hover {
border-color: rgba(100, 230, 211, 0.35);
transform: translateY(-1px);
}
.law-btn.active {
border-color: rgba(100, 230, 211, 0.6);
background: var(--cyan-soft);
box-shadow: inset 0 0 18px rgba(100, 230, 211, 0.05);
}
.law-btn strong,
.law-btn span {
display: block;
}
.law-btn strong {
color: var(--text);
font-size: 12px;
}
.law-btn span {
margin-top: 3px;
color: var(--muted);
font-size: 10px;
}
.law-btn.active strong {
color: var(--cyan);
}
.range-row {
margin-top: 15px;
}
.range-label {
display: flex;
align-items: baseline;
justify-content: space-between;
gap: 10px;
margin-bottom: 8px;
color: #cbd7e2;
font-size: 12px;
}
.range-label b {
color: var(--text);
font-size: 12px;
text-align: right;
}
input[type="range"] {
width: 100%;
height: 5px;
margin: 4px 0;
accent-color: var(--cyan);
cursor: pointer;
}
.range-scale {
display: flex;
justify-content: space-between;
margin-top: 4px;
color: #617386;
font-size: 9px;
}
.equation-card {
margin-top: 15px;
padding: 13px;
border: 1px solid rgba(100, 230, 211, 0.18);
border-radius: 13px;
background:
linear-gradient(145deg, rgba(100, 230, 211, 0.08), transparent),
#0d1621;
}
.equation-card span {
display: block;
margin-bottom: 5px;
color: var(--muted);
font-size: 9px;
font-weight: 800;
letter-spacing: 0.1em;
text-transform: uppercase;
}
.equation-card code {
display: block;
margin-bottom: 12px;
color: #effcfa;
font-family: "SFMono-Regular", Consolas, monospace;
font-size: 13px;
}
.equation-card code:last-child {
margin-bottom: 0;
color: #d9ceff;
}
.stoich-card {
padding: 13px;
border: 1px solid rgba(174, 140, 255, 0.22);
border-radius: 13px;
background: var(--purple-soft);
}
.reaction {
display: flex;
align-items: center;
gap: 10px;
margin-bottom: 5px;
color: #e8deff;
font-family: Georgia, serif;
font-size: 18px;
font-style: italic;
}
.reaction .coefficient {
min-width: 28px;
height: 28px;
display: grid;
place-items: center;
border-radius: 8px;
background: rgba(174, 140, 255, 0.16);
color: var(--purple);
font-family: inherit;
font-size: 18px;
font-style: normal;
font-weight: 800;
}
.hint {
margin: 9px 0 0;
color: var(--muted);
font-size: 11px;
line-height: 1.55;
}
.hint strong {
color: #cbbcff;
}
.stage {
position: relative;
min-height: 540px;
overflow: hidden;
background:
radial-gradient(circle at 50% 43%, rgba(50, 100, 120, 0.13), transparent 43%),
linear-gradient(180deg, rgba(14, 23, 35, 0.65), rgba(7, 12, 19, 0.9));
}
#viewport {
position: absolute;
inset: 0;
}
#viewport canvas {
display: block;
width: 100%;
height: 100%;
touch-action: none;
cursor: grab;
}
#viewport canvas:active {
cursor: grabbing;
}
.stage-head {
position: absolute;
z-index: 2;
top: 15px;
left: 16px;
right: 16px;
display: flex;
align-items: flex-start;
justify-content: space-between;
gap: 12px;
pointer-events: none;
}
.stage-title {
color: #d9e7f2;
font-size: 12px;
font-weight: 800;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.stage-subtitle {
margin-top: 4px;
color: var(--muted);
font-size: 10px;
}
.badge-row {
display: flex;
flex-wrap: wrap;
justify-content: flex-end;
gap: 6px;
}
.badge {
padding: 6px 9px;
border: 1px solid var(--line);
border-radius: 999px;
background: rgba(9, 15, 23, 0.7);
color: #c3d1de;
font-size: 10px;
font-weight: 800;
backdrop-filter: blur(8px);
}
.badge.cyan {
border-color: rgba(100, 230, 211, 0.3);
color: var(--cyan);
}
.badge.purple {
border-color: rgba(174, 140, 255, 0.3);
color: #cfbdff;
}
.hud {
position: absolute;
z-index: 2;
left: 16px;
right: 16px;
bottom: 49px;
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 8px;
pointer-events: none;
}
.hud-card {
min-width: 0;
padding: 11px 12px;
border: 1px solid var(--line);
border-radius: 12px;
background: rgba(8, 14, 22, 0.76);
backdrop-filter: blur(10px);
}
.hud-card span {
display: block;
overflow: hidden;
color: var(--muted);
font-size: 9px;
font-weight: 800;
letter-spacing: 0.07em;
text-overflow: ellipsis;
text-transform: uppercase;
white-space: nowrap;
}
.hud-card strong {
display: block;
margin-top: 4px;
font-family: "SFMono-Regular", Consolas, monospace;
font-size: clamp(15px, 2vw, 21px);
letter-spacing: -0.04em;
}
.hud-card:nth-child(1) strong {
color: var(--cyan);
}
.hud-card:nth-child(2) strong {
color: var(--orange);
}
.hud-card:nth-child(3) strong {
color: #cfbdff;
}
.stage-foot {
position: absolute;
z-index: 2;
left: 17px;
right: 17px;
bottom: 15px;
display: flex;
align-items: center;
justify-content: space-between;
gap: 10px;
color: #6f8295;
font-size: 10px;
pointer-events: none;
}
.particle-key {
display: flex;
align-items: center;
gap: 7px;
}
.particle-key i {
width: 8px;
height: 8px;
display: block;
border-radius: 2px;
background: var(--orange);
box-shadow: 0 0 9px rgba(255, 173, 92, 0.65);
transform: rotate(15deg);
}
.charts {
padding: 18px;
}
.chart-section {
margin-bottom: 18px;
}
.chart-heading {
display: flex;
align-items: baseline;
justify-content: space-between;
gap: 10px;
margin-bottom: 8px;
}
.chart-heading h2 {
margin: 0;
color: #dbe7f1;
font-size: 12px;
letter-spacing: 0.02em;
}
.chart-heading span {
color: var(--muted);
font-family: "SFMono-Regular", Consolas, monospace;
font-size: 10px;
}
.chart-legend {
display: flex;
flex-wrap: wrap;
gap: 12px;
margin-bottom: 5px;
color: var(--muted);
font-size: 10px;
}
.legend-item {
display: flex;
align-items: center;
gap: 6px;
}
.legend-line {
width: 18px;
height: 3px;
display: inline-block;
border-radius: 4px;
background: var(--cyan);
}
.legend-line.orange {
background: var(--orange);
}
.legend-line.purple {
background: var(--purple);
}
.legend-line.dashed {
height: 0;
border-top: 2px dashed var(--purple);
border-radius: 0;
background: transparent;
}
.chart-wrap {
overflow: hidden;
border: 1px solid var(--line);
border-radius: 13px;
background: rgba(7, 12, 19, 0.68);
}
.chart-wrap canvas {
width: 100%;
height: 174px;
display: block;
}
.timeline {
padding-top: 16px;
border-top: 1px solid var(--line);
}
.timeline-head {
display: flex;
align-items: center;
justify-content: space-between;
margin-bottom: 10px;
}
.time-readout {
color: #dce8f2;
font-family: "SFMono-Regular", Consolas, monospace;
font-size: 12px;
}
.transport {
display: flex;
gap: 7px;
}
.transport button {
height: 32px;
padding: 0 11px;
border: 1px solid var(--line);
border-radius: 9px;
background: #121d2a;
cursor: pointer;
font-size: 11px;
font-weight: 800;
transition: 140ms ease;
}
.transport button:hover {
border-color: rgba(100, 230, 211, 0.4);
background: #172534;
}
.transport .play {
border-color: rgba(100, 230, 211, 0.35);
background: var(--cyan-soft);
color: var(--cyan);
}
.timeline input {
accent-color: var(--cyan);
}
.assumptions {
margin-top: 15px;
padding: 12px;
border: 1px solid var(--line);
border-radius: 12px;
background: rgba(255, 255, 255, 0.025);
color: var(--muted);
font-size: 10px;
line-height: 1.65;
}
.assumptions strong {
color: #cdd9e4;
}
@media (max-width: 1180px) {
.workspace {
grid-template-columns: 290px minmax(400px, 1fr);
overflow: auto;
}
.charts {
grid-column: 1 / -1;
display: grid;
grid-template-columns: 1fr 1fr;
gap: 14px;
}
.charts .timeline {
grid-column: 1 / -1;
}
}
@media (max-width: 760px) {
.app {
display: block;
}
.topbar {
position: sticky;
top: 0;
}
.header-note,
.header-status {
display: none;
}
.workspace {
display: flex;
flex-direction: column;
overflow: visible;
}
.stage {
order: 1;
min-height: 620px;
}
.controls {
order: 2;
}
.charts {
order: 3;
display: block;
}
.hud {
bottom: 55px;
}
.hud-card {
padding: 9px 8px;
}
.stage-foot {
align-items: flex-end;
}
}
</style>
</head>
<body>
<div class="app">
<header class="topbar">
<div class="brand">
<div class="brand-mark" aria-hidden="true">
<svg viewBox="0 0 32 32" fill="none">
<path d="M7 5h18M11 5v8l-5 9a3 3 0 0 0 2.6 4.5h14.8A3 3 0 0 0 26 22l-5-9V5"
stroke="#64e6d3" stroke-width="2" stroke-linecap="round"/>
<circle cx="12" cy="21" r="2" fill="#ffad5c"/>
<circle cx="19" cy="18" r="2" fill="#ffad5c"/>
</svg>
</div>
<div>
<span class="eyebrow">Interactive Kinetics</span>
<h1>Reaction Rate Lab</h1>
</div>
</div>
<div class="header-note">Rate order ≠ stoichiometry</div>
<div class="header-status"><span></span>Analytic model</div>
</header>
<main class="workspace">
<aside class="panel controls">
<section class="section">
<div class="section-title">
Rate order
<small id="lawTag">First order</small>
</div>
<div class="preset-grid">
<button class="law-btn" data-order="0" type="button">
<strong>Zero</strong>
<span>r = k</span>
</button>
<button class="law-btn active" data-order="1" type="button">
<strong>First</strong>
<span>r = k[A]</span>
</button>
<button class="law-btn" data-order="2" type="button">
<strong>Second</strong>
<span>r = k[A]²</span>
</button>
</div>
<div class="range-row">
<label class="range-label" for="order">
<span>Reaction order, n</span>
<b id="orderValue">1</b>
</label>
<input id="order" type="range" min="0" max="3" step="0.1" value="1">
<div class="range-scale">
<span>0</span>
<span>custom</span>
<span>3</span>
</div>
</div>
<div class="equation-card">
<span>Extent-based reaction rate</span>
<code id="rateEquation">r = k[A]¹</code>
<span>Reactant species balance</span>
<code id="balanceEquation">−d[A]/dt = 1 × r</code>
</div>
</section>
<section class="section">
<div class="section-title">
Synthetic constants
<small>constant volume</small>
</div>
<div class="range-row">
<label class="range-label" for="k">
<span>Rate constant, k</span>
<b id="kValue">0.50 s⁻¹</b>
</label>
<input id="k" type="range" min="0.05" max="1.5" step="0.05" value="0.5">
<div class="range-scale">
<span>0.05</span>
<span>1.50</span>
</div>
</div>
<div class="range-row">
<label class="range-label" for="c0">
<span>Initial concentration, [A]₀</span>
<b id="c0Value">1.00 mol·L⁻¹</b>
</label>
<input id="c0" type="range" min="0.25" max="2" step="0.05" value="1">
<div class="range-scale">
<span>0.25</span>
<span>2.00</span>
</div>
</div>
<div class="range-row">
<label class="range-label" for="speed">
<span>Playback speed</span>
<b id="speedValue">1.00×</b>
</label>
<input id="speed" type="range" min="0.25" max="3" step="0.25" value="1">
<div class="range-scale">
<span>0.25×</span>
<span>3.00×</span>
</div>
</div>
</section>
<section class="section">
<div class="section-title">
Stoichiometry
<small>independent control</small>
</div>
<div class="stoich-card">
<div class="reaction">
<span class="coefficient" id="reactionCoefficient">1</span>
<span>A → products</span>
</div>
<div class="range-row">
<label class="range-label" for="stoich">
<span>Reactant coefficient, ν<sub>A</sub></span>
<b id="stoichValue">1</b>
</label>
<input id="stoich" type="range" min="1" max="4" step="1" value="1">
<div class="range-scale">
<span>1 A</span>
<span>4 A</span>
</div>
</div>
<p class="hint">
<strong>Order</strong> determines how the reaction rate responds to
concentration. <strong>Stoichiometry</strong> determines how much
A disappears per unit of reaction rate.
</p>
</div>
</section>
</aside>
<section class="panel stage">
<div id="viewport" aria-label="Interactive low-poly reaction vessel"></div>
<div class="stage-head">
<div>
<div class="stage-title">Low-poly batch reactor</div>
<div class="stage-subtitle">Orange icosahedra represent reactant A</div>
</div>
<div class="badge-row">
<span class="badge cyan" id="orderBadge">n = 1</span>
<span class="badge purple" id="stoichBadge">νA = 1</span>
</div>
</div>
<div class="hud">
<div class="hud-card">
<span>Concentration [A]</span>
<strong id="concentrationValue">1.000</strong>
</div>
<div class="hud-card">
<span>Reaction rate r</span>
<strong id="rateValue">0.500</strong>
</div>
<div class="hud-card">
<span>Species loss |d[A]/dt|</span>
<strong id="lossValue">0.500</strong>
</div>
</div>
<div class="stage-foot">
<div class="particle-key"><i></i> reactant particles</div>
<div>Drag to orbit · wheel to zoom · double-click to reset</div>
</div>
</section>
<aside class="panel charts">
<section class="chart-section">
<div class="chart-heading">
<h2>Concentration response</h2>
<span>[A](t)</span>
</div>
<div class="chart-legend">
<span class="legend-item"><i class="legend-line"></i> analytic [A]</span>
</div>
<div class="chart-wrap">
<canvas id="concentrationChart" aria-label="Concentration versus time"></canvas>
</div>
</section>
<section class="chart-section">
<div class="chart-heading">
<h2>Rate separation</h2>
<span>reaction rate vs. species loss</span>
</div>
<div class="chart-legend">
<span class="legend-item"><i class="legend-line orange"></i> r</span>
<span class="legend-item"><i class="legend-line dashed"></i> νA · r</span>
</div>
<div class="chart-wrap">
<canvas id="rateChart" aria-label="Reaction rate and species disappearance versus time"></canvas>
</div>
</section>
<section class="timeline">
<div class="timeline-head">
<div>
<div class="time-readout" id="timeReadout">t = 0.00 s</div>
</div>
<div class="transport">
<button id="playButton" class="play" type="button">Ⅱ Pause</button>
<button id="resetButton" type="button">↺ Reset</button>
</div>
</div>
<input id="timeline" type="range" min="0" max="10" step="0.01" value="0"
aria-label="Simulation time">
<div class="assumptions">
<strong>Model assumptions</strong><br>
Irreversible reaction · constant volume · well mixed ·
no temperature effects · reverse and parallel reactions omitted.
The purple curve equals the orange curve multiplied only by ν<sub>A</sub>.
</div>
</section>
</aside>
</main>
</div>
<script type="module">
import * as THREE from "https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
const $ = (selector) => document.querySelector(selector);
const state = {
order: 1,
k: 0.5,
c0: 1,
stoich: 1,
speed: 1,
time: 0,
timeMax: 10,
playing: true
};
let curves = {
time: [],
concentration: [],
rate: [],
loss: []
};
const clamp = THREE.MathUtils.clamp;
function formatOrder(n) {
return Number.isInteger(n) ? String(n) : n.toFixed(1);
}
function lawName(n) {
if (Math.abs(n) < 1e-8) return "Zero order";
if (Math.abs(n - 1) < 1e-8) return "First order";
if (Math.abs(n - 2) < 1e-8) return "Second order";
return "Custom order";
}
function kUnit(n) {
if (Math.abs(n) < 1e-8) return "mol·L⁻¹·s⁻¹";
if (Math.abs(n - 1) < 1e-8) return "s⁻¹";
if (Math.abs(n - 2) < 1e-8) return "L·mol⁻¹·s⁻¹";
return "(mol·L⁻¹)¹⁻ⁿ·s⁻¹";
}
function concentrationAt(t, parameters = state) {
const n = parameters.order;
const a = parameters.stoich;
const k = parameters.k;
const c = parameters.c0;
if (t <= 0) return c;
if (Math.abs(n) < 1e-8) {
return Math.max(0, c - a * k * t);
}
if (Math.abs(n - 1) < 1e-8) {
return c * Math.exp(-a * k * t);
}
if (Math.abs(n - 2) < 1e-8) {
return c / (1 + a * k * c * t);
}
const denominator =
Math.pow(c, 1 - n) + (n - 1) * a * k * t;
return Math.pow(denominator, -1 / (n - 1));
}
function rateAt(t, parameters = state) {
const c = concentrationAt(t, parameters);
return parameters.k * Math.pow(c, parameters.order);
}
function calculateTimeMax() {
const { order: n, stoich: a, k, c0: c } = state;
let t;
if (Math.abs(n) < 1e-8) {
t = c / (a * k);
} else if (Math.abs(n - 1) < 1e-8) {
t = Math.log(100) / (a * k);
} else if (Math.abs(n - 2) < 1e-8) {
t = 99 / (a * k * c);
} else {
const numerator = Math.pow(100, n - 1) - 1;
const denominator = (n - 1) * a * k * Math.pow(c, n - 1);
t = numerator / denominator;
}
return clamp(Number.isFinite(t) ? t : 10, 2, 30);
}
function rebuildCurves() {
const previousMax = state.timeMax || 10;
const progress = clamp(state.time / previousMax, 0, 1);
state.timeMax = calculateTimeMax();
state.time = progress * state.timeMax;
const count = 280;
curves = {
time: [],
concentration: [],
rate: [],
loss: []
};
for (let i = 0; i < count; i++) {
const t = (i / (count - 1)) * state.timeMax;
const c = concentrationAt(t);
const r = state.k * Math.pow(c, state.order);
curves.time.push(t);
curves.concentration.push(c);
curves.rate.push(r);
curves.loss.push(state.stoich * r);
}
$("#timeline").max = state.timeMax.toFixed(3);
updateLabels();
drawCharts();
}
function updateLabels() {
const nText = formatOrder(state.order);
$("#orderValue").textContent = nText;
$("#kValue").textContent = `${state.k.toFixed(2)} ${kUnit(state.order)}`;
$("#c0Value").textContent = `${state.c0.toFixed(2)} mol·L⁻¹`;
$("#speedValue").textContent = `${state.speed.toFixed(2)}×`;
$("#stoichValue").textContent = String(state.stoich);
$("#reactionCoefficient").textContent = String(state.stoich);
$("#lawTag").textContent = lawName(state.order);
$("#orderBadge").textContent = `n = ${nText}`;
$("#stoichBadge").textContent = `νA = ${state.stoich}`;
$("#rateEquation").innerHTML = `r = k[A]<sup>${nText}</sup>`;
$("#balanceEquation").textContent = `−d[A]/dt = ${state.stoich} × r`;
document.querySelectorAll(".law-btn").forEach((button) => {
const buttonOrder = Number(button.dataset.order);
button.classList.toggle(
"active",
Math.abs(buttonOrder - state.order) < 1e-8
);
});
}
function setOrder(order) {
state.order = order;
$("#order").value = order;
rebuildCurves();
}
document.querySelectorAll(".law-btn").forEach((button) => {
button.addEventListener("click", () => {
setOrder(Number(button.dataset.order));
});
});
$("#order").addEventListener("input", (event) => {
setOrder(Number(event.target.value));
});
$("#k").addEventListener("input", (event) => {
state.k = Number(event.target.value);
rebuildCurves();
});
$("#c0").addEventListener("input", (event) => {
state.c0 = Number(event.target.value);
rebuildCurves();
});
$("#stoich").addEventListener("input", (event) => {
state.stoich = Number(event.target.value);
rebuildCurves();
});
$("#speed").addEventListener("input", (event) => {
state.speed = Number(event.target.value);
updateLabels();
});
$("#timeline").addEventListener("input", (event) => {
state.time = Number(event.target.value);
state.playing = false;
updatePlayButton();
});
function updatePlayButton() {
$("#playButton").textContent = state.playing ? "Ⅱ Pause" : "▶ Play";
}
$("#playButton").addEventListener("click", () => {
if (!state.playing && state.time >= state.timeMax - 0.001) {
state.time = 0;
}
state.playing = !state.playing;
updatePlayButton();
});
$("#resetButton").addEventListener("click", () => {
state.time = 0;
state.playing = true;
updatePlayButton();
});
// ---------------------------------------------------------------------
// Canvas charts
// ---------------------------------------------------------------------
function prepareCanvas(canvas) {
const rect = canvas.getBoundingClientRect();
const dpr = Math.min(window.devicePixelRatio || 1, 2);
const width = Math.max(1, Math.floor(rect.width * dpr));
const height = Math.max(1, Math.floor(rect.height * dpr));
if (canvas.width !== width || canvas.height !== height) {
canvas.width = width;
canvas.height = height;
}
const ctx = canvas.getContext("2d");
ctx.setTransform(dpr, 0, 0, dpr, 0, 0);
ctx.clearRect(0, 0, rect.width, rect.height);
return { ctx, width: rect.width, height: rect.height };
}
function drawGrid(ctx, plot, yMax, timeMax, yLabel) {
const { left, top, width, height } = plot;
ctx.font = "9px system-ui, sans-serif";
ctx.textBaseline = "middle";
for (let i = 0; i <= 4; i++) {
const y = top + (height * i) / 4;
const value = yMax * (1 - i / 4);
ctx.strokeStyle = "rgba(139, 166, 190, 0.11)";
ctx.lineWidth = 1;
ctx.beginPath();
ctx.moveTo(left, y);
ctx.lineTo(left + width, y);
ctx.stroke();
ctx.fillStyle = "#6d8092";
ctx.textAlign = "right";
ctx.fillText(value.toFixed(value < 1 ? 2 : 1), left - 7, y);
}
ctx.textBaseline = "top";
ctx.textAlign = "center";
for (let i = 0; i <= 4; i++) {
const x = left + (width * i) / 4;
const value = (timeMax * i) / 4;
ctx.strokeStyle = "rgba(139, 166, 190, 0.055)";
ctx.beginPath();
ctx.moveTo(x, top);
ctx.lineTo(x, top + height);
ctx.stroke();
ctx.fillStyle = "#6d8092";
ctx.fillText(`${value.toFixed(1)}s`, x, top + height + 8);
}
ctx.save();
ctx.translate(12, top + height / 2);
ctx.rotate(-Math.PI / 2);
ctx.fillStyle = "#7e92a4";
ctx.textAlign = "center";
ctx.textBaseline = "middle";
ctx.fillText(yLabel, 0, 0);
ctx.restore();
}
function drawConcentrationChart() {
const canvas = $("#concentrationChart");
const { ctx, width, height } = prepareCanvas(canvas);
const plot = { left: 43, top: 13, width: width - 58, height: height - 43 };
const yMax = state.c0 * 1.15;
drawGrid(ctx, plot, yMax, state.timeMax, "mol·L⁻¹");
const xAt = (t) => plot.left + (t / state.timeMax) * plot.width;
const yAt = (c) => plot.top + (1 - c / yMax) * plot.height;
const gradient = ctx.createLinearGradient(0, plot.top, 0, plot.top + plot.height);
gradient.addColorStop(0, "rgba(100, 230, 211, 0.25)");
gradient.addColorStop(1, "rgba(100, 230, 211, 0.015)");
ctx.beginPath();
curves.concentration.forEach((c, i) => {
const x = xAt(curves.time[i]);
const y = yAt(c);
if (i === 0) ctx.moveTo(x, y);
else ctx.lineTo(x, y);
});
ctx.lineTo(xAt(state.timeMax), plot.top + plot.height);
ctx.lineTo(plot.left, plot.top + plot.height);
ctx.closePath();
ctx.fillStyle = gradient;
ctx.fill();
ctx.beginPath();
curves.concentration.forEach((c, i) => {
const x = xAt(curves.time[i]);
const y = yAt(c);
if (i === 0) ctx.moveTo(x, y);
else ctx.lineTo(x, y);
});
ctx.strokeStyle = "#64e6d3";
ctx.lineWidth = 2.2;
ctx.stroke();
const currentC = concentrationAt(state.time);
const cursorX = xAt(state.time);
const currentY = yAt(currentC);
ctx.setLineDash([3, 4]);
ctx.strokeStyle = "rgba(234, 243, 250, 0.35)";
ctx.lineWidth = 1;
ctx.beginPath();
ctx.moveTo(cursorX, plot.top);
ctx.lineTo(cursorX, plot.top + plot.height);
ctx.stroke();
ctx.setLineDash([]);
ctx.fillStyle = "#071219";
ctx.strokeStyle = "#64e6d3";
ctx.lineWidth = 2;
ctx.beginPath();
ctx.arc(cursorX, currentY, 4.5, 0, Math.PI * 2);
ctx.fill();
ctx.stroke();
}
function drawRateChart() {
const canvas = $("#rateChart");
const { ctx, width, height } = prepareCanvas(canvas);
const plot = { left: 43, top: 13, width: width - 58, height: height - 43 };
const yMax = Math.max(...curves.loss, 0.001) * 1.15;
drawGrid(ctx, plot, yMax, state.timeMax, "rate");
const xAt = (t) => plot.left + (t / state.timeMax) * plot.width;
const yAt = (v) => plot.top + (1 - v / yMax) * plot.height;
function plotSeries(values, color, dashed = false) {
ctx.beginPath();
values.forEach((value, i) => {
const x = xAt(curves.time[i]);
const y = yAt(value);
if (i === 0) ctx.moveTo(x, y);
else ctx.lineTo(x, y);
});
ctx.strokeStyle = color;
ctx.lineWidth = 2;
ctx.setLineDash(dashed ? [5, 4] : []);
ctx.stroke();
ctx.setLineDash([]);
}
plotSeries(curves.rate, "#ffad5c");
plotSeries(curves.loss, "#ae8cff", true);
const currentRate = rateAt(state.time);
const currentLoss = state.stoich * currentRate;
const cursorX = xAt(state.time);
ctx.setLineDash([3, 4]);
ctx.strokeStyle = "rgba(234, 243, 250, 0.35)";
ctx.lineWidth = 1;
ctx.beginPath();
ctx.moveTo(cursorX, plot.top);
ctx.lineTo(cursorX, plot.top + plot.height);
ctx.stroke();
ctx.setLineDash([]);
[
[currentRate, "#ffad5c"],
[currentLoss, "#ae8cff"]
].forEach(([value, color]) => {
ctx.fillStyle = "#071219";
ctx.strokeStyle = color;
ctx.lineWidth = 2;
ctx.beginPath();
ctx.arc(cursorX, yAt(value), 4.2, 0, Math.PI * 2);
ctx.fill();
ctx.stroke();
});
}
function drawCharts() {
drawConcentrationChart();
drawRateChart();
}
// ---------------------------------------------------------------------
// Three.js scene
// ---------------------------------------------------------------------
const viewport = $("#viewport");
const scene = new THREE.Scene();
scene.fog = new THREE.FogExp2(0x0a1019, 0.028);
const camera = new THREE.PerspectiveCamera(38, 1, 0.1, 100);
const renderer = new THREE.WebGLRenderer({
antialias: true,
alpha: true
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio || 1, 2));
renderer.setClearColor(0x000000, 0);
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
viewport.appendChild(renderer.domElement);
const hemisphere = new THREE.HemisphereLight(0xc9efff, 0x172036, 2.0);
scene.add(hemisphere);
const keyLight = new THREE.DirectionalLight(0xffffff, 3.2);
keyLight.position.set(5, 8, 4);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(1024, 1024);
keyLight.shadow.camera.left = -6;
keyLight.shadow.camera.right = 6;
keyLight.shadow.camera.top = 7;
keyLight.shadow.camera.bottom = -5;
scene.add(keyLight);
const rimLight = new THREE.PointLight(0x64e6d3, 18, 12, 2);
rimLight.position.set(-4, 2, -3);
scene.add(rimLight);
const warmLight = new THREE.PointLight(0xffad5c, 13, 10, 2);
warmLight.position.set(4, 0, 3);
scene.add(warmLight);
const platformMaterial = new THREE.MeshStandardMaterial({
color: 0x172333,
roughness: 0.72,
metalness: 0.28,
flatShading: true
});
const platform = new THREE.Mesh(
new THREE.CylinderGeometry(4.4, 4.8, 0.34, 12),
platformMaterial
);
platform.position.y = -2.55;
platform.receiveShadow = true;
scene.add(platform);
const platformInset = new THREE.Mesh(
new THREE.CylinderGeometry(3.25, 3.65, 0.12, 12),
new THREE.MeshStandardMaterial({
color: 0x0e1926,
roughness: 0.58,
metalness: 0.35,
flatShading: true
})
);
platformInset.position.y = -2.34;
platformInset.receiveShadow = true;
scene.add(platformInset);
const grid = new THREE.GridHelper(12, 24, 0x31516a, 0x203348);
grid.position.y = -2.34;
grid.material.transparent = true;
grid.material.opacity = 0.18;
scene.add(grid);
const glassMaterial = new THREE.MeshPhongMaterial({
color: 0x9deee8,
transparent: true,
opacity: 0.12,
shininess: 85,
side: THREE.DoubleSide,
depthWrite: false
});
const vessel = new THREE.Mesh(
new THREE.CylinderGeometry(1.9, 1.25, 3.8, 12, 1, true),
glassMaterial
);
vessel.position.y = -0.05;
scene.add(vessel);
const bowl = new THREE.Mesh(
new THREE.CylinderGeometry(1.25, 1.65, 0.8, 12),
glassMaterial.clone()
);
bowl.position.y = -2.08;
scene.add(bowl);
const liquidMaterial = new THREE.MeshPhongMaterial({
color: 0x4ad9c7,
transparent: true,
opacity: 0.18,
shininess: 70,
side: THREE.DoubleSide,
depthWrite: false
});
const liquid = new THREE.Mesh(
new THREE.CylinderGeometry(1.7, 1.12, 0.12, 12),
liquidMaterial
);
liquid.position.y = -1.59;
scene.add(liquid);
const frameMaterial = new THREE.MeshStandardMaterial({
color: 0x49677b,
roughness: 0.4,
metalness: 0.65,
flatShading: true
});
function makeRing(radius, tube, y, color = 0x49677b) {
const material = frameMaterial.clone();
material.color.setHex(color);
const ring = new THREE.Mesh(
new THREE.TorusGeometry(radius, tube, 6, 32),
material
);
ring.rotation.x = Math.PI / 2;
ring.position.y = y;
ring.castShadow = true;
scene.add(ring);
return ring;
}
makeRing(1.9, 0.075, 1.86, 0x5c8297);
makeRing(1.46, 0.065, -1.67, 0x49677b);
makeRing(1.65, 0.055, -2.43, 0x3e596c);
for (let i = 0; i < 4; i++) {
const angle = (i / 4) * Math.PI * 2 + Math.PI / 4;
const support = new THREE.Mesh(
new THREE.BoxGeometry(0.09, 3.8, 0.09),
frameMaterial
);
support.position.set(
Math.cos(angle) * 1.94,
0.08,
Math.sin(angle) * 1.94
);
support.castShadow = true;
scene.add(support);
const node = new THREE.Mesh(
new THREE.OctahedronGeometry(0.15, 0),
new THREE.MeshStandardMaterial({
color: 0x64e6d3,
emissive: 0x173c3b,
emissiveIntensity: 0.7,
roughness: 0.45,
flatShading: true
})
);
node.position.set(
Math.cos(angle) * 1.94,
1.86,
Math.sin(angle) * 1.94
);
scene.add(node);
}
const rateHalo = new THREE.Mesh(
new THREE.TorusGeometry(1.96, 0.025, 5, 40),
new THREE.MeshBasicMaterial({
color: 0xffad5c,
transparent: true,
opacity: 0.35,
depthWrite: false
})
);
rateHalo.rotation.x = Math.PI / 2;
rateHalo.position.y = 1.92;
scene.add(rateHalo);
const particleCount = 180;
const particleGeometry = new THREE.IcosahedronGeometry(0.105, 0);
const particleMaterial = new THREE.MeshStandardMaterial({
color: 0xffad5c,
emissive: 0x4b2107,
emissiveIntensity: 0.55,
roughness: 0.5,
metalness: 0.05,
flatShading: true
});
const particles = new THREE.InstancedMesh(
particleGeometry,
particleMaterial,
particleCount
);
particles.instanceMatrix.setUsage(THREE.DynamicDrawUsage);
particles.castShadow = true;
particles.receiveShadow = true;
scene.add(particles);
let randomSeed = 73421;
function seededRandom() {
randomSeed = (randomSeed * 1664525 + 1013904223) >>> 0;
return randomSeed / 4294967296;
}
const particleBases = [];
for (let i = 0; i < particleCount; i++) {
const angle = seededRandom() * Math.PI * 2;
const radius = Math.sqrt(seededRandom()) * 1.48;
particleBases.push({
x: Math.cos(angle) * radius,
y: -1.38 + seededRandom() * 2.95,
z: Math.sin(angle) * radius,
phase: seededRandom() * Math.PI * 2
});
}
const dummy = new THREE.Object3D();
// ---------------------------------------------------------------------
// Direct camera controls: no addons or OrbitControls
// ---------------------------------------------------------------------
const cameraState = {
azimuth: 0.62,
elevation: 0.19,
distance: 9.1
};
let dragging = false;
let pointerX = 0;
let pointerY = 0;
function updateCamera() {
const { azimuth, elevation, distance } = cameraState;
camera.position.set(
distance * Math.cos(elevation) * Math.sin(azimuth),
distance * Math.sin(elevation) + 0.05,
distance * Math.cos(elevation) * Math.cos(azimuth)
);
camera.lookAt(0, -0.15, 0);
}
renderer.domElement.addEventListener("pointerdown", (event) => {
dragging = true;
pointerX = event.clientX;
pointerY = event.clientY;
renderer.domElement.setPointerCapture(event.pointerId);
});
renderer.domElement.addEventListener("pointermove", (event) => {
if (!dragging) return;
const dx = event.clientX - pointerX;
const dy = event.clientY - pointerY;
pointerX = event.clientX;
pointerY = event.clientY;
cameraState.azimuth -= dx * 0.007;
cameraState.elevation = clamp(
cameraState.elevation + dy * 0.005,
-0.08,
0.85
);
});
function endDrag(event) {
dragging = false;
if (event.pointerId !== undefined && renderer.domElement.hasPointerCapture(event.pointerId)) {
renderer.domElement.releasePointerCapture(event.pointerId);
}
}
renderer.domElement.addEventListener("pointerup", endDrag);
renderer.domElement.addEventListener("pointercancel", endDrag);
renderer.domElement.addEventListener("wheel", (event) => {
event.preventDefault();
cameraState.distance = clamp(
cameraState.distance + event.deltaY * 0.008,
6.2,
13.5
);
}, { passive: false });
renderer.domElement.addEventListener("dblclick", () => {
cameraState.azimuth = 0.62;
cameraState.elevation = 0.19;
cameraState.distance = 9.1;
});
function resizeRenderer() {
const width = Math.max(1, viewport.clientWidth);
const height = Math.max(1, viewport.clientHeight);
renderer.setSize(width, height, false);
camera.aspect = width / height;
camera.updateProjectionMatrix();
}
const resizeObserver = new ResizeObserver(() => {
resizeRenderer();
drawCharts();
});
resizeObserver.observe(viewport);
resizeObserver.observe($(".charts"));
// ---------------------------------------------------------------------
// Animation and readouts
// ---------------------------------------------------------------------
const clock = new THREE.Clock();
let visualTime = 0;
let frameCounter = 0;
function updateReadouts() {
const concentration = concentrationAt(state.time);
const rate = rateAt(state.time);
const loss = state.stoich * rate;
$("#concentrationValue").textContent = concentration.toFixed(3);
$("#rateValue").textContent = rate.toFixed(3);
$("#lossValue").textContent = loss.toFixed(3);
$("#timeReadout").textContent = `t = ${state.time.toFixed(2)} s`;
$("#timeline").value = state.time.toFixed(3);
}
function animate() {
requestAnimationFrame(animate);
const dt = Math.min(clock.getDelta(), 0.05);
visualTime += dt;
if (state.playing) {
state.time += dt * state.speed;
if (state.time >= state.timeMax) {
state.time = state.timeMax;
state.playing = false;
updatePlayButton();
}
}
const concentration = concentrationAt(state.time);
const rate = rateAt(state.time);
const concentrationFraction = clamp(concentration / state.c0, 0, 1);
const visibleCount = Math.round(particleCount * concentrationFraction);
particles.count = visibleCount;
for (let i = 0; i < visibleCount; i++) {
const base = particleBases[i];
dummy.position.set(
base.x + Math.sin(visualTime * 0.7 + base.phase) * 0.075,
base.y + Math.sin(visualTime * 0.9 + base.phase * 1.7) * 0.065,
base.z + Math.cos(visualTime * 0.6 + base.phase) * 0.075
);
const pulse = 1 + Math.sin(visualTime * 2 + base.phase) * 0.035;
dummy.scale.setScalar(pulse);
dummy.rotation.set(
base.phase + visualTime * 0.18,
base.phase * 0.7 + visualTime * 0.22,
0
);
dummy.updateMatrix();
particles.setMatrixAt(i, dummy.matrix);
}
particles.instanceMatrix.needsUpdate = true;
const maxInitialRate = state.k * Math.pow(state.c0, state.order);
const normalizedRate = maxInitialRate > 0 ? rate / maxInitialRate : 0;
rateHalo.material.opacity = 0.1 + normalizedRate * 0.48;
const haloScale = 1 + normalizedRate * 0.08 + Math.sin(visualTime * 2.4) * 0.008;
rateHalo.scale.setScalar(haloScale);
liquidMaterial.opacity = 0.06 + concentrationFraction * 0.16;
if (!dragging) {
cameraState.azimuth += dt * 0.035;
}
updateCamera();
updateReadouts();
frameCounter++;
if (frameCounter % 2 === 0) {
drawCharts();
}
renderer.render(scene, camera);
}
rebuildCurves();
resizeRenderer();
updatePlayButton();
updateReadouts();
animate();
</script>
</body>
</html>
```