Prompt specification
Build or customize the Three.js 3D scene "Solute particle concentration beakers" 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: Solute particle concentration beakers
Category: Science & Physics · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/solute-particle-concentration-beakers-x1993
Scene Description Prompt:
Build two transparent beakers with symbolic solute particles and volume markers; distinguish total solute amount from concentration and label particle counts as an illustration. Use Three.js from cdnjs in a single HTML file, with a 3D cutaway.
Working Three.js Starter (solute-particle-concentration-beakers-x1993):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Same Amount, Different Concentration</title>
<style>
:root {
color-scheme: dark;
--bg: #050d16;
--panel: rgba(10, 24, 38, 0.78);
--panel-border: rgba(134, 220, 255, 0.18);
--text: #edf8ff;
--muted: #91aabc;
--cyan: #60ddff;
--gold: #ffc65a;
--green: #75edbc;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
}
body {
font-family:
Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
color: var(--text);
background:
radial-gradient(circle at 50% 32%, #12324a 0%, #081722 42%, #040b12 100%);
}
canvas {
position: fixed;
inset: 0;
display: block;
width: 100%;
height: 100%;
touch-action: none;
outline: none;
}
.ui {
position: fixed;
inset: 0;
pointer-events: none;
z-index: 5;
}
header {
position: absolute;
top: 30px;
left: 34px;
width: min(455px, calc(100vw - 68px));
}
.eyebrow {
display: flex;
align-items: center;
gap: 10px;
margin-bottom: 12px;
color: var(--cyan);
font-size: 11px;
font-weight: 800;
letter-spacing: 0.17em;
text-transform: uppercase;
}
.eyebrow::before {
width: 28px;
height: 1px;
content: "";
background: var(--cyan);
box-shadow: 0 0 10px var(--cyan);
}
h1 {
margin: 0;
font-size: clamp(31px, 3.2vw, 52px);
font-weight: 760;
line-height: 1.02;
letter-spacing: -0.045em;
text-wrap: balance;
}
h1 span {
color: var(--cyan);
}
header p {
max-width: 430px;
margin: 16px 0 0;
color: var(--muted);
font-size: 14px;
line-height: 1.65;
}
.view-panel {
position: absolute;
top: 28px;
right: 30px;
width: 270px;
padding: 15px 16px;
border: 1px solid var(--panel-border);
border-radius: 15px;
background: var(--panel);
box-shadow: 0 16px 50px rgba(0, 0, 0, 0.24);
backdrop-filter: blur(14px);
pointer-events: auto;
}
.formula-label {
color: var(--muted);
font-size: 10px;
font-weight: 750;
letter-spacing: 0.14em;
text-transform: uppercase;
}
.formula {
margin: 3px 0 4px;
color: var(--text);
font-family: Georgia, "Times New Roman", serif;
font-size: 30px;
font-style: italic;
}
.formula span {
color: var(--cyan);
}
.view-panel p {
margin: 0 0 12px;
color: var(--muted);
font-size: 12px;
line-height: 1.45;
}
.control-row {
display: flex;
align-items: center;
justify-content: space-between;
gap: 12px;
padding-top: 11px;
border-top: 1px solid rgba(255, 255, 255, 0.08);
}
.control-hint {
color: #7893a6;
font-size: 10px;
line-height: 1.35;
}
button {
border: 1px solid rgba(96, 221, 255, 0.32);
border-radius: 9px;
padding: 7px 10px;
color: var(--text);
background: rgba(96, 221, 255, 0.08);
font: inherit;
font-size: 11px;
font-weight: 700;
cursor: pointer;
transition: background 160ms ease, border-color 160ms ease;
}
button:hover {
border-color: rgba(96, 221, 255, 0.7);
background: rgba(96, 221, 255, 0.16);
}
button:focus-visible {
outline: 2px solid var(--cyan);
outline-offset: 3px;
}
.projected-label {
position: absolute;
z-index: 3;
pointer-events: none;
will-change: transform;
}
.floating-label {
display: flex;
align-items: center;
gap: 9px;
padding: 8px 11px 8px 8px;
border: 1px solid rgba(255, 198, 90, 0.25);
border-radius: 11px;
background: rgba(8, 19, 30, 0.84);
box-shadow: 0 8px 25px rgba(0, 0, 0, 0.28);
transform: translate(-50%, -100%);
backdrop-filter: blur(8px);
white-space: nowrap;
}
.beaker-letter {
display: grid;
width: 27px;
height: 27px;
place-items: center;
border-radius: 8px;
color: #07111d;
background: var(--gold);
font-size: 13px;
font-weight: 850;
}
.floating-label strong,
.floating-label small {
display: block;
}
.floating-label strong {
font-size: 12px;
font-weight: 750;
}
.floating-label small {
margin-top: 2px;
color: var(--muted);
font-size: 9px;
}
.surface-label {
padding: 5px 9px;
border: 1px solid rgba(96, 221, 255, 0.3);
border-radius: 999px;
color: #dff8ff;
background: rgba(6, 25, 38, 0.82);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.05em;
transform: translate(-50%, -115%);
white-space: nowrap;
}
.volume-marker {
color: #a7c3d4;
font-size: 9px;
font-weight: 650;
text-shadow: 0 1px 5px #000;
white-space: nowrap;
}
.volume-marker.left {
transform: translate(-112%, -50%);
}
.volume-marker.right {
transform: translate(12%, -50%);
}
.metrics {
position: absolute;
right: 28px;
bottom: 24px;
left: 28px;
display: grid;
grid-template-columns: 1fr 1fr 0.95fr;
gap: 12px;
max-width: 1080px;
margin: 0 auto;
pointer-events: none;
}
.metric-card,
.takeaway {
min-width: 0;
padding: 15px 17px;
border: 1px solid var(--panel-border);
border-radius: 16px;
background: var(--panel);
box-shadow: 0 18px 55px rgba(0, 0, 0, 0.27);
backdrop-filter: blur(15px);
}
.metric-head {
display: flex;
align-items: center;
gap: 9px;
margin-bottom: 12px;
}
.metric-head .letter {
display: grid;
width: 24px;
height: 24px;
place-items: center;
border-radius: 7px;
color: #07111d;
background: var(--gold);
font-size: 12px;
font-weight: 900;
}
.metric-head strong {
font-size: 12px;
}
.metric-row {
display: grid;
grid-template-columns: 1fr auto;
gap: 12px;
align-items: baseline;
padding: 6px 0;
border-top: 1px solid rgba(255, 255, 255, 0.065);
}
.metric-row span {
color: var(--muted);
font-size: 10px;
line-height: 1.3;
}
.metric-row b {
font-size: 13px;
text-align: right;
}
.metric-row b small {
color: var(--muted);
font-size: 9px;
font-weight: 500;
}
.metric-row.concentration b {
color: var(--cyan);
font-size: 16px;
}
.takeaway {
display: flex;
flex-direction: column;
justify-content: center;
border-color: rgba(117, 237, 188, 0.23);
}
.takeaway .tag {
margin-bottom: 7px;
color: var(--green);
font-size: 9px;
font-weight: 800;
letter-spacing: 0.13em;
text-transform: uppercase;
}
.takeaway strong {
font-size: 15px;
line-height: 1.35;
}
.takeaway p {
margin: 7px 0 0;
color: var(--muted);
font-size: 11px;
line-height: 1.5;
}
.model-note {
position: absolute;
right: 30px;
bottom: 5px;
max-width: 510px;
color: #617b8d;
font-size: 9px;
line-height: 1.4;
text-align: right;
}
@media (max-width: 980px) {
header {
top: 22px;
left: 24px;
}
header p {
display: none;
}
.view-panel {
top: 22px;
right: 22px;
width: 225px;
}
.metrics {
right: 14px;
bottom: 18px;
left: 14px;
grid-template-columns: 1fr 1fr;
}
.takeaway {
grid-column: 1 / -1;
padding: 11px 16px;
}
.takeaway p {
margin-top: 4px;
}
.model-note {
display: none;
}
}
@media (max-width: 650px) {
header {
top: 16px;
left: 17px;
width: calc(100vw - 34px);
}
.eyebrow {
margin-bottom: 7px;
font-size: 9px;
}
h1 {
max-width: 320px;
font-size: 29px;
}
.view-panel {
top: 117px;
right: 14px;
width: 174px;
padding: 10px 12px;
}
.view-panel p,
.control-hint {
display: none;
}
.formula {
font-size: 23px;
}
.control-row {
justify-content: flex-end;
padding-top: 7px;
}
.metrics {
bottom: 10px;
gap: 7px;
}
.metric-card {
padding: 10px;
}
.metric-head {
margin-bottom: 7px;
}
.metric-row {
gap: 5px;
padding: 4px 0;
}
.metric-row span {
font-size: 8px;
}
.metric-row b {
font-size: 10px;
}
.metric-row.concentration b {
font-size: 13px;
}
.takeaway {
padding: 9px 12px;
}
.takeaway strong {
font-size: 12px;
}
.takeaway p {
font-size: 9px;
}
.floating-label {
gap: 5px;
padding: 5px 7px 5px 5px;
}
.beaker-letter {
width: 21px;
height: 21px;
border-radius: 6px;
font-size: 10px;
}
.floating-label strong {
font-size: 9px;
}
.floating-label small,
.surface-label,
.volume-marker {
font-size: 7px;
}
}
</style>
</head>
<body>
<div class="ui">
<header>
<div class="eyebrow">Interactive chemistry · 3D cutaway</div>
<h1>Same amount.<br><span>Different concentration.</span></h1>
<p>
Both beakers contain 12 symbolic solute particles. Beaker B has half
the solution volume, so the same amount of solute is more concentrated.
</p>
</header>
<aside class="view-panel">
<div class="formula-label">Concentration</div>
<div class="formula">c = <span>n / V</span></div>
<p>Total solute amount stays fixed; solution volume changes.</p>
<div class="control-row">
<div class="control-hint">Drag to orbit<br>Scroll or pinch to zoom</div>
<button id="reset-view" type="button">Reset view</button>
</div>
</aside>
<section class="metrics" aria-label="Comparison of amount, volume, and concentration">
<article class="metric-card">
<div class="metric-head">
<span class="letter">A</span>
<strong>Beaker A</strong>
</div>
<div class="metric-row">
<span>Illustrative total amount n</span>
<b>12 particles</b>
</div>
<div class="metric-row">
<span>Solution volume V</span>
<b>200 mL</b>
</div>
<div class="metric-row concentration">
<span>Concentration c = n / V</span>
<b>0.060 <small>model particles/mL</small></b>
</div>
</article>
<article class="metric-card">
<div class="metric-head">
<span class="letter">B</span>
<strong>Beaker B</strong>
</div>
<div class="metric-row">
<span>Illustrative total amount n</span>
<b>12 particles</b>
</div>
<div class="metric-row">
<span>Solution volume V</span>
<b>100 mL</b>
</div>
<div class="metric-row concentration">
<span>Concentration c = n / V</span>
<b>0.120 <small>model particles/mL</small></b>
</div>
</article>
<aside class="takeaway">
<div class="tag">Comparison</div>
<strong>Equal amount, but B is 2× as concentrated.</strong>
<p>
A has 6 model particles per 100 mL; B has 12 model particles per 100 mL.
No extra solute was added to B.
</p>
</aside>
</section>
<div class="model-note">
Teaching model only: every sphere represents one equal, symbolic particle.
These counts are illustrative, not molecular-scale; real amount n is usually
expressed in moles.
</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 scene = new THREE.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.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",
"Two transparent cutaway beakers containing symbolic solute particles"
);
document.body.prepend(renderer.domElement);
const camera = new THREE.PerspectiveCamera(
38,
window.innerWidth / window.innerHeight,
0.1,
100
);
const controls = {
target: new THREE.Vector3(0, 1.18, 0),
theta: 0.22,
phi: 1.38,
radius: 8.8
};
const defaultControls = { ...controls };
function cameraRadius() {
const aspect = window.innerWidth / window.innerHeight;
return controls.radius * Math.max(1, 0.88 / aspect);
}
function updateCamera() {
const radius = cameraRadius();
camera.position.set(
controls.target.x +
radius * Math.sin(controls.phi) * Math.sin(controls.theta),
controls.target.y + radius * Math.cos(controls.phi),
controls.target.z +
radius * Math.sin(controls.phi) * Math.cos(controls.theta)
);
camera.lookAt(controls.target);
camera.updateMatrixWorld();
}
updateCamera();
// Lighting
scene.add(new THREE.HemisphereLight(0xb8e8ff, 0x061019, 1.7));
const keyLight = new THREE.DirectionalLight(0xffffff, 2.6);
keyLight.position.set(4, 7, 5);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(2048, 2048);
keyLight.shadow.camera.left = -6;
keyLight.shadow.camera.right = 6;
keyLight.shadow.camera.top = 6;
keyLight.shadow.camera.bottom = -6;
keyLight.shadow.bias = -0.0005;
scene.add(keyLight);
const cyanLight = new THREE.PointLight(0x35cfff, 25, 10, 2);
cyanLight.position.set(-3.8, 2.6, 3.4);
scene.add(cyanLight);
const goldLight = new THREE.PointLight(0xffb54a, 18, 9, 2);
goldLight.position.set(3.6, 2.4, 2.2);
scene.add(goldLight);
// Ground and grid
const ground = new THREE.Mesh(
new THREE.PlaneGeometry(30, 30),
new THREE.MeshStandardMaterial({
color: 0x06121d,
roughness: 0.94,
metalness: 0.05
})
);
ground.rotation.x = -Math.PI / 2;
ground.position.y = -0.045;
ground.receiveShadow = true;
scene.add(ground);
const grid = new THREE.GridHelper(14, 28, 0x28627b, 0x153747);
grid.position.y = -0.025;
grid.material.transparent = true;
grid.material.opacity = 0.22;
grid.material.depthWrite = false;
scene.add(grid);
const labels = [];
function addProjectedLabel(element, worldPosition) {
document.body.appendChild(element);
labels.push({ element, worldPosition });
}
function projectLabels() {
const width = window.innerWidth;
const height = window.innerHeight;
for (const item of labels) {
const projected = item.worldPosition.clone().project(camera);
const visible = projected.z > -1 && projected.z < 1;
item.element.style.display = visible ? "flex" : "none";
if (!visible) continue;
const x = (projected.x * 0.5 + 0.5) * width;
const y = (-projected.y * 0.5 + 0.5) * height;
item.element.style.left = `${x}px`;
item.element.style.top = `${y}px`;
}
}
// The cutaway keeps the back half of each cylinder.
const HALF_START = Math.PI / 2;
const HALF_LENGTH = Math.PI;
const particleGeometry = new THREE.IcosahedronGeometry(0.075, 2);
const haloGeometry = new THREE.SphereGeometry(0.115, 14, 10);
const particleMaterial = new THREE.MeshStandardMaterial({
color: 0xffc65a,
emissive: 0xff8b24,
emissiveIntensity: 0.52,
roughness: 0.27,
metalness: 0.05
});
const haloMaterial = new THREE.MeshBasicMaterial({
color: 0xffbd4d,
transparent: true,
opacity: 0.13,
depthWrite: false
});
const animatedHalos = [];
function seededRandom(seed) {
let value = seed >>> 0;
return function () {
value += 0x6d2b79f5;
let t = value;
t = Math.imul(t ^ (t >>> 15), t | 1);
t ^= t + Math.imul(t ^ (t >>> 7), t | 61);
return ((t ^ (t >>> 14)) >>> 0) / 4294967296;
};
}
function createParticlePositions(count, radius, bottom, top, seed) {
const random = seededRandom(seed);
const positions = [];
let attempts = 0;
while (positions.length < count && attempts < 6000) {
attempts++;
const x = (random() * 2 - 1) * (radius - 0.14);
const z = -0.07 - random() * (radius - 0.17);
const y = bottom + random() * Math.max(0.05, top - bottom);
if (x * x + z * z > (radius - 0.1) ** 2) continue;
const tooClose = positions.some(
(p) => (p.x - x) ** 2 + (p.y - y) ** 2 + (p.z - z) ** 2 < 0.027
);
if (tooClose && positions.length < count - 2) continue;
positions.push(new THREE.Vector3(x, y, z));
}
return positions;
}
function createArcLine(radius, y, material, centerX = 0) {
const points = [];
for (let i = 0; i <= 80; i++) {
const theta = HALF_START + (i / 80) * HALF_LENGTH;
points.push(
new THREE.Vector3(
centerX + radius * Math.sin(theta),
y,
radius * Math.cos(theta)
)
);
}
const geometry = new THREE.BufferGeometry().setFromPoints(points);
return new THREE.Line(geometry, material);
}
function createBeaker({
x,
letter,
volume,
maxVolume,
markerSide,
seed
}) {
const group = new THREE.Group();
group.position.x = x;
scene.add(group);
const radius = 0.82;
const base = 0.19;
const wallHeight = 2.55;
const wallTop = base + wallHeight;
const liquidMaxHeight = 2.25;
const fillHeight = (volume / maxVolume) * liquidMaxHeight;
const surfaceY = base + fillHeight;
const particleCount = 12;
// Platform and glow
const platform = new THREE.Mesh(
new THREE.CylinderGeometry(1.05, 1.12, 0.075, 64),
new THREE.MeshStandardMaterial({
color: 0x0c2432,
roughness: 0.62,
metalness: 0.18
})
);
platform.position.y = 0.045;
platform.receiveShadow = true;
group.add(platform);
const glow = new THREE.Mesh(
new THREE.RingGeometry(0.97, 1.11, 64),
new THREE.MeshBasicMaterial({
color: 0x50d8ff,
transparent: true,
opacity: 0.15,
side: THREE.DoubleSide,
depthWrite: false
})
);
glow.rotation.x = -Math.PI / 2;
glow.position.y = 0.087;
group.add(glow);
// Transparent back half of the glass wall.
const glassMaterial = new THREE.MeshPhysicalMaterial({
color: 0xa8e9ff,
transparent: true,
opacity: 0.25,
roughness: 0.1,
metalness: 0.04,
side: THREE.DoubleSide,
depthWrite: false
});
const wall = new THREE.Mesh(
new THREE.CylinderGeometry(
radius,
radius,
wallHeight,
72,
1,
true,
HALF_START,
HALF_LENGTH
),
glassMaterial
);
wall.position.y = base + wallHeight / 2;
wall.renderOrder = 5;
group.add(wall);
// A second, slightly smaller surface suggests glass thickness.
const innerWall = new THREE.Mesh(
new THREE.CylinderGeometry(
radius - 0.025,
radius - 0.025,
wallHeight,
72,
1,
true,
HALF_START,
HALF_LENGTH
),
new THREE.MeshPhysicalMaterial({
color: 0xe0f8ff,
transparent: true,
opacity: 0.075,
roughness: 0.08,
side: THREE.DoubleSide,
depthWrite: false
})
);
innerWall.position.y = base + wallHeight / 2;
innerWall.renderOrder = 5;
group.add(innerWall);
const bottom = new THREE.Mesh(
new THREE.CylinderGeometry(
radius,
radius,
0.085,
72,
1,
false,
HALF_START,
HALF_LENGTH
),
new THREE.MeshPhysicalMaterial({
color: 0x9cdfff,
transparent: true,
opacity: 0.32,
roughness: 0.16,
side: THREE.DoubleSide,
depthWrite: false
})
);
bottom.position.y = base;
bottom.renderOrder = 5;
group.add(bottom);
// Cutaway edge lines.
const edgeMaterial = new THREE.LineBasicMaterial({
color: 0x91e9ff,
transparent: true,
opacity: 0.68
});
for (const side of [-1, 1]) {
const geometry = new THREE.BufferGeometry().setFromPoints([
new THREE.Vector3(side * radius, base, 0),
new THREE.Vector3(side * radius, wallTop, 0)
]);
const edge = new THREE.Line(geometry, edgeMaterial);
edge.renderOrder = 6;
group.add(edge);
}
const rim = createArcLine(radius, wallTop, edgeMaterial);
rim.renderOrder = 6;
group.add(rim);
const bottomRim = createArcLine(radius, base, edgeMaterial);
bottomRim.renderOrder = 6;
group.add(bottomRim);
// Half-cylinder liquid volume.
const liquidSide = new THREE.MeshPhysicalMaterial({
color: 0x20bfe9,
transparent: true,
opacity: 0.26,
roughness: 0.22,
metalness: 0,
side: THREE.DoubleSide,
depthWrite: false
});
const liquidTop = new THREE.MeshPhysicalMaterial({
color: 0x55ddff,
transparent: true,
opacity: 0.38,
roughness: 0.12,
metalness: 0,
side: THREE.DoubleSide,
depthWrite: false
});
const liquidBottom = new THREE.MeshBasicMaterial({
color: 0x168db6,
transparent: true,
opacity: 0.16,
side: THREE.DoubleSide,
depthWrite: false
});
const liquid = new THREE.Mesh(
new THREE.CylinderGeometry(
radius - 0.055,
radius - 0.055,
fillHeight,
72,
1,
false,
HALF_START,
HALF_LENGTH
),
[liquidSide, liquidTop, liquidBottom]
);
liquid.position.y = base + fillHeight / 2;
liquid.renderOrder = 1;
group.add(liquid);
// The exposed cut plane makes the half-volume visually explicit.
const cutFace = new THREE.Mesh(
new THREE.PlaneGeometry((radius - 0.055) * 2, fillHeight),
new THREE.MeshBasicMaterial({
color: 0x54d8ff,
transparent: true,
opacity: 0.095,
side: THREE.DoubleSide,
depthWrite: false
})
);
cutFace.position.set(0, base + fillHeight / 2, 0.004);
cutFace.renderOrder = 2;
group.add(cutFace);
// Liquid surface outline and cut diameter.
const surfaceMaterial = new THREE.LineBasicMaterial({
color: 0x9cecff,
transparent: true,
opacity: 0.7
});
const surfaceArc = createArcLine(
radius - 0.055,
surfaceY,
surfaceMaterial
);
surfaceArc.renderOrder = 4;
group.add(surfaceArc);
const diameterGeometry = new THREE.BufferGeometry().setFromPoints([
new THREE.Vector3(-(radius - 0.055), surfaceY, 0),
new THREE.Vector3(radius - 0.055, surfaceY, 0)
]);
const diameter = new THREE.Line(diameterGeometry, surfaceMaterial);
diameter.renderOrder = 4;
group.add(diameter);
// Exactly 12 equal symbolic solute particles in each beaker.
const positions = createParticlePositions(
particleCount,
radius - 0.09,
base + 0.14,
surfaceY - 0.12,
seed
);
positions.forEach((position, index) => {
const particle = new THREE.Mesh(particleGeometry, particleMaterial);
particle.position.copy(position);
particle.castShadow = true;
particle.rotation.set(index * 0.31, index * 0.57, index * 0.19);
group.add(particle);
const halo = new THREE.Mesh(haloGeometry, haloMaterial);
halo.position.copy(position);
halo.userData.phase = index * 0.72 + seed;
halo.renderOrder = 3;
group.add(halo);
animatedHalos.push(halo);
});
// Volume graduations.
const graduationMaterial = new THREE.LineBasicMaterial({
color: 0x8fc8dc,
transparent: true,
opacity: 0.58
});
for (let value = 50; value <= maxVolume; value += 50) {
const y = base + (value / maxVolume) * liquidMaxHeight;
const outerX = markerSide * (radius + 0.08);
const innerX = outerX - markerSide * 0.22;
const tickGeometry = new THREE.BufferGeometry().setFromPoints([
new THREE.Vector3(outerX, y, 0.015),
new THREE.Vector3(innerX, y, 0.015)
]);
const tick = new THREE.Line(tickGeometry, graduationMaterial);
tick.renderOrder = 4;
group.add(tick);
const label = document.createElement("div");
label.className = `projected-label volume-marker ${markerSide < 0 ? "left" : "right"}`;
label.textContent = `${value} mL`;
addProjectedLabel(
label,
new THREE.Vector3(
x + outerX + markerSide * 0.035,
y,
0.015
)
);
}
// Projected amount label.
const amountLabel = document.createElement("div");
amountLabel.className = "projected-label floating-label";
amountLabel.innerHTML = `
<span class="beaker-letter">${letter}</span>
<div>
<strong>n = 12 symbolic particles</strong>
<small>same illustrative total amount</small>
</div>
`;
addProjectedLabel(
amountLabel,
new THREE.Vector3(x, wallTop + 0.39, 0)
);
// Projected solution-volume label.
const surfaceLabel = document.createElement("div");
surfaceLabel.className = "projected-label surface-label";
surfaceLabel.textContent = `${volume} mL solution`;
addProjectedLabel(
surfaceLabel,
new THREE.Vector3(x, surfaceY + 0.075, 0.05)
);
}
createBeaker({
x: -1.48,
letter: "A",
volume: 200,
maxVolume: 250,
markerSide: -1,
seed: 41
});
createBeaker({
x: 1.48,
letter: "B",
volume: 100,
maxVolume: 250,
markerSide: 1,
seed: 97
});
// -------------------------------------------------------------------------
// Custom camera controls: no Three.js addons are used.
// -------------------------------------------------------------------------
const canvas = renderer.domElement;
const pointers = new Map();
let previousPinchDistance = 0;
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
);
}
canvas.addEventListener("pointerdown", (event) => {
canvas.setPointerCapture(event.pointerId);
pointers.set(event.pointerId, {
x: event.clientX,
y: event.clientY
});
if (pointers.size === 2) {
previousPinchDistance = pointerDistance();
}
});
canvas.addEventListener("pointermove", (event) => {
if (!pointers.has(event.pointerId)) return;
const previous = pointers.get(event.pointerId);
const current = {
x: event.clientX,
y: event.clientY
};
pointers.set(event.pointerId, current);
if (pointers.size === 1) {
const dx = current.x - previous.x;
const dy = current.y - previous.y;
controls.theta -= dx * 0.005;
controls.phi -= dy * 0.004;
controls.theta = THREE.MathUtils.clamp(
controls.theta,
-1.05,
1.05
);
controls.phi = THREE.MathUtils.clamp(
controls.phi,
0.83,
1.5
);
} else if (pointers.size === 2) {
const distance = pointerDistance();
if (previousPinchDistance > 0 && distance > 0) {
controls.radius *= previousPinchDistance / distance;
controls.radius = THREE.MathUtils.clamp(
controls.radius,
5.6,
13
);
}
previousPinchDistance = distance;
}
updateCamera();
});
function releasePointer(event) {
pointers.delete(event.pointerId);
previousPinchDistance = pointers.size === 2 ? pointerDistance() : 0;
}
canvas.addEventListener("pointerup", releasePointer);
canvas.addEventListener("pointercancel", releasePointer);
canvas.addEventListener(
"wheel",
(event) => {
event.preventDefault();
controls.radius += event.deltaY * 0.006;
controls.radius = THREE.MathUtils.clamp(
controls.radius,
5.6,
13
);
updateCamera();
},
{ passive: false }
);
function resetView() {
controls.theta = defaultControls.theta;
controls.phi = defaultControls.phi;
controls.radius = defaultControls.radius;
controls.target.copy(defaultControls.target);
updateCamera();
}
document.getElementById("reset-view").addEventListener("click", resetView);
canvas.addEventListener("dblclick", resetView);
window.addEventListener("keydown", (event) => {
const key = event.key.toLowerCase();
if (key === "arrowleft") controls.theta -= 0.08;
else if (key === "arrowright") controls.theta += 0.08;
else if (key === "arrowup") controls.phi -= 0.06;
else if (key === "arrowdown") controls.phi += 0.06;
else if (key === "+" || key === "=") controls.radius -= 0.4;
else if (key === "-") controls.radius += 0.4;
else if (key === "r") {
resetView();
return;
} else {
return;
}
controls.theta = THREE.MathUtils.clamp(controls.theta, -1.05, 1.05);
controls.phi = THREE.MathUtils.clamp(controls.phi, 0.83, 1.5);
controls.radius = THREE.MathUtils.clamp(controls.radius, 5.6, 13);
updateCamera();
});
window.addEventListener("resize", () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.fov = camera.aspect < 0.8 ? 47 : 38;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
updateCamera();
});
const clock = new THREE.Clock();
function animate() {
const elapsed = clock.getElapsedTime();
for (const halo of animatedHalos) {
const pulse = 1 + Math.sin(elapsed * 2.1 + halo.userData.phase) * 0.075;
halo.scale.setScalar(pulse);
}
updateCamera();
projectLabels();
renderer.render(scene, camera);
requestAnimationFrame(animate);
}
animate();
</script>
</body>
</html>
```