Prompt specification
Build or customize the Three.js 3D scene "Leaf epidermis stomatal window" 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: Leaf epidermis stomatal window
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/leaf-epidermis-stomatal-window-x0789
Scene Description Prompt:
Please create one self-contained HTML file with Three.js from cdnjs showing leaf stomata. Model an enlarged leaf-surface patch with one stoma and paired guard cells; distinguish the pore from surrounding cells and include a whole-leaf scale reference. Voxel style.
Working Three.js Starter (leaf-epidermis-stomatal-window-x0789):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="color-scheme" content="light">
<title>Leaf Stomata — Voxel Anatomy</title>
<style>
:root {
--ink: #18352a;
--muted: #5e7066;
--paper: #f4f4e9;
--accent: #d5a735;
--line: rgba(24, 53, 42, 0.48);
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #e8ebdf;
color: var(--ink);
font-family:
Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
#app {
position: relative;
width: 100%;
height: 100%;
isolation: isolate;
}
canvas {
display: block;
width: 100%;
height: 100%;
touch-action: none;
outline: none;
}
.header {
position: fixed;
z-index: 5;
top: 28px;
left: 30px;
max-width: 430px;
pointer-events: none;
}
.eyebrow {
display: flex;
align-items: center;
gap: 9px;
margin-bottom: 8px;
color: #597064;
font-size: 11px;
font-weight: 800;
letter-spacing: 0.16em;
text-transform: uppercase;
}
.eyebrow::before {
width: 22px;
height: 3px;
background: var(--accent);
content: "";
}
h1 {
margin: 0;
font-size: clamp(29px, 4vw, 49px);
font-weight: 760;
letter-spacing: -0.045em;
line-height: 0.98;
}
.header p {
max-width: 390px;
margin: 13px 0 0;
color: var(--muted);
font-size: 13px;
line-height: 1.55;
}
.controls {
position: fixed;
z-index: 6;
top: 26px;
right: 28px;
display: flex;
gap: 8px;
}
button {
appearance: none;
border: 1px solid rgba(24, 53, 42, 0.16);
border-radius: 9px;
padding: 10px 13px;
background: rgba(248, 248, 238, 0.88);
box-shadow: 0 5px 20px rgba(32, 53, 39, 0.08);
color: var(--ink);
cursor: pointer;
font: inherit;
font-size: 12px;
font-weight: 700;
backdrop-filter: blur(10px);
transition:
transform 150ms ease,
background 150ms ease;
}
button:hover {
background: #fffef4;
transform: translateY(-1px);
}
button:active {
transform: translateY(0);
}
button:focus-visible {
outline: 3px solid rgba(213, 167, 53, 0.55);
outline-offset: 2px;
}
#leaders {
position: fixed;
z-index: 2;
inset: 0;
width: 100%;
height: 100%;
overflow: visible;
pointer-events: none;
}
.scene-label {
position: fixed;
z-index: 3;
min-width: 145px;
max-width: 190px;
padding: 10px 12px 10px 14px;
border-left: 4px solid var(--label-accent, var(--accent));
border-radius: 4px 9px 9px 4px;
background: rgba(250, 250, 240, 0.92);
box-shadow: 0 8px 28px rgba(24, 53, 42, 0.12);
pointer-events: none;
transform: translate(-50%, -50%);
backdrop-filter: blur(8px);
}
.scene-label strong {
display: block;
font-size: 11px;
font-weight: 850;
letter-spacing: 0.095em;
line-height: 1.2;
text-transform: uppercase;
}
.scene-label span {
display: block;
margin-top: 4px;
color: var(--muted);
font-size: 10px;
line-height: 1.35;
}
.legend {
position: fixed;
z-index: 5;
bottom: 26px;
left: 30px;
width: 270px;
padding: 15px 17px 14px;
border: 1px solid rgba(24, 53, 42, 0.13);
border-radius: 12px;
background: rgba(248, 248, 238, 0.9);
box-shadow: 0 8px 30px rgba(24, 53, 42, 0.09);
backdrop-filter: blur(10px);
}
.legend-title {
margin-bottom: 10px;
color: #66776d;
font-size: 10px;
font-weight: 800;
letter-spacing: 0.14em;
text-transform: uppercase;
}
.legend-row {
display: flex;
align-items: center;
gap: 9px;
margin-top: 7px;
font-size: 11px;
font-weight: 650;
}
.swatch {
width: 14px;
height: 14px;
flex: 0 0 auto;
border: 1px solid rgba(0, 0, 0, 0.12);
border-radius: 3px;
box-shadow:
inset 2px 2px 0 rgba(255, 255, 255, 0.12),
0 1px 2px rgba(0, 0, 0, 0.08);
}
.swatch.pore {
background: #071813;
}
.swatch.guard {
background: #91c950;
}
.swatch.pavement {
background: #668b50;
}
.swatch.leaf {
background: #315d39;
}
.legend-note {
margin-top: 11px;
padding-top: 10px;
border-top: 1px solid rgba(24, 53, 42, 0.12);
color: #6d7b73;
font-size: 9px;
line-height: 1.45;
}
.hint {
position: fixed;
z-index: 5;
right: 28px;
bottom: 28px;
padding: 9px 12px;
border: 1px solid rgba(24, 53, 42, 0.12);
border-radius: 8px;
background: rgba(248, 248, 238, 0.78);
color: #64756b;
font-size: 10px;
font-weight: 650;
pointer-events: none;
backdrop-filter: blur(8px);
}
.labels-off .scene-label,
.labels-off #leaders {
display: none;
}
#loading {
position: fixed;
z-index: 20;
inset: 0;
display: grid;
place-content: center;
gap: 8px;
background: #e8ebdf;
color: var(--ink);
text-align: center;
transition: opacity 350ms ease;
}
#loading strong {
font-size: 15px;
}
#loading span {
color: var(--muted);
font-size: 11px;
}
#loading.done {
opacity: 0;
pointer-events: none;
}
@media (max-width: 760px) {
.header {
top: 18px;
left: 18px;
max-width: 58%;
}
.eyebrow {
font-size: 8px;
letter-spacing: 0.1em;
}
h1 {
font-size: 29px;
}
.header p,
.legend,
.hint {
display: none;
}
.controls {
top: 16px;
right: 16px;
flex-direction: column;
}
button {
padding: 9px 10px;
font-size: 10px;
}
.scene-label {
min-width: 118px;
max-width: 145px;
padding: 8px 9px;
}
.scene-label strong {
font-size: 9px;
}
.scene-label span {
font-size: 8px;
}
.scene-label.optional {
display: none;
}
}
</style>
</head>
<body>
<div id="app">
<canvas id="scene" aria-label="Interactive voxel model of a leaf stoma"></canvas>
<header class="header">
<div class="eyebrow">Plant anatomy · voxel study</div>
<h1>Inside a leaf stoma</h1>
<p>
An enlarged leaf-surface patch showing one pore, its paired guard cells,
and the surrounding pavement cells. A miniature whole leaf provides
the scale reference.
</p>
</header>
<div class="controls">
<button id="reset" type="button" title="Reset the camera">↺ Reset view</button>
<button id="toggle-labels" type="button" aria-pressed="true">
Hide labels
</button>
</div>
<svg id="leaders" aria-hidden="true"></svg>
<div id="label-patch" class="scene-label optional" style="--label-accent:#d5a735">
<strong>Enlarged patch</strong>
<span>2 mm field · schematic</span>
</div>
<div id="label-pore" class="scene-label" style="--label-accent:#071813">
<strong>Central pore</strong>
<span>Open air space</span>
</div>
<div id="label-guard" class="scene-label optional" style="--label-accent:#8fc84d">
<strong>Guard cell</strong>
<span>One of a paired pair</span>
</div>
<div id="label-cell" class="scene-label optional" style="--label-accent:#688e50">
<strong>Pavement cell</strong>
<span>Typical epidermal cell</span>
</div>
<div id="label-leaf" class="scene-label" style="--label-accent:#315d39">
<strong>Whole-leaf reference</strong>
<span>8 cm long · not proportional</span>
</div>
<aside class="legend">
<div class="legend-title">Reading the model</div>
<div class="legend-row">
<i class="swatch pore"></i>
Stomatal pore
</div>
<div class="legend-row">
<i class="swatch guard"></i>
Paired guard cells
</div>
<div class="legend-row">
<i class="swatch pavement"></i>
Pavement cells
</div>
<div class="legend-row">
<i class="swatch leaf"></i>
Whole-leaf reference
</div>
<div class="legend-note">
The 8 cm leaf and 2 mm patch are conceptual scale references.
Their displayed sizes are intentionally not proportional.
</div>
</aside>
<div class="hint">Drag to orbit · Scroll or pinch to zoom · Double-click to reset</div>
<div id="loading">
<strong>Building the voxel leaf…</strong>
<span>Loading Three.js from cdnjs</span>
</div>
</div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// -------------------------------------------------------------------------
// Scene setup
// -------------------------------------------------------------------------
const canvas = document.querySelector("#scene");
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xe8ebdf);
scene.fog = new THREE.Fog(0xe8ebdf, 38, 68);
const camera = new THREE.PerspectiveCamera(
42,
window.innerWidth / window.innerHeight,
0.1,
120
);
const renderer = new THREE.WebGLRenderer({
canvas,
antialias: true,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight, false);
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.12;
const hemisphere = new THREE.HemisphereLight(0xfbf8e9, 0x56715c, 2.35);
scene.add(hemisphere);
const keyLight = new THREE.DirectionalLight(0xfff8df, 3.4);
keyLight.position.set(-7, 15, 10);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(2048, 2048);
keyLight.shadow.camera.left = -16;
keyLight.shadow.camera.right = 16;
keyLight.shadow.camera.top = 15;
keyLight.shadow.camera.bottom = -15;
keyLight.shadow.camera.near = 1;
keyLight.shadow.camera.far = 45;
keyLight.shadow.bias = -0.0007;
keyLight.target.position.set(1, 0, 0);
scene.add(keyLight, keyLight.target);
const fillLight = new THREE.DirectionalLight(0xbad7c8, 1.25);
fillLight.position.set(12, 7, -10);
scene.add(fillLight);
const unitBox = new THREE.BoxGeometry(1, 1, 1);
const dummy = new THREE.Object3D();
const materials = {
base: new THREE.MeshStandardMaterial({
color: 0x38533d,
roughness: 0.95
}),
baseDark: new THREE.MeshStandardMaterial({
color: 0x263e31,
roughness: 1
}),
edge: new THREE.MeshStandardMaterial({
color: 0x5d7d4f,
roughness: 0.9
}),
pore: new THREE.MeshStandardMaterial({
color: 0x071813,
roughness: 1,
metalness: 0
}),
poreWall: new THREE.MeshStandardMaterial({
color: 0x112b22,
roughness: 0.95
}),
bracket: new THREE.MeshStandardMaterial({
color: 0x7d7047,
roughness: 0.85
}),
selection: new THREE.MeshStandardMaterial({
color: 0xe3bd4d,
roughness: 0.65
}),
stem: new THREE.MeshStandardMaterial({
color: 0x6f6840,
roughness: 1
}),
platform: new THREE.MeshStandardMaterial({
color: 0xd6dac6,
roughness: 0.96
}),
platformSide: new THREE.MeshStandardMaterial({
color: 0xb8c0aa,
roughness: 0.98
}),
instance: new THREE.MeshStandardMaterial({
color: 0xffffff,
roughness: 0.84,
metalness: 0
}),
guard: new THREE.MeshStandardMaterial({
color: 0xffffff,
roughness: 0.72,
metalness: 0,
emissive: 0x172b08,
emissiveIntensity: 0.13
})
};
function addBox(parent, size, position, material, castShadow = true) {
const mesh = new THREE.Mesh(unitBox, material);
mesh.scale.set(size[0], size[1], size[2]);
mesh.position.set(position[0], position[1], position[2]);
mesh.castShadow = castShadow;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
function addInstances(parent, items, material, name) {
const mesh = new THREE.InstancedMesh(unitBox, material, items.length);
mesh.name = name;
mesh.castShadow = true;
mesh.receiveShadow = true;
items.forEach((item, index) => {
dummy.position.set(item.x, item.y, item.z);
dummy.rotation.set(0, item.rotation || 0, 0);
dummy.scale.set(item.sx, item.sy, item.sz);
dummy.updateMatrix();
mesh.setMatrixAt(index, dummy.matrix);
mesh.setColorAt(index, item.color);
});
mesh.instanceMatrix.needsUpdate = true;
if (mesh.instanceColor) mesh.instanceColor.needsUpdate = true;
mesh.computeBoundingSphere();
parent.add(mesh);
return mesh;
}
let randomSeed = 82741;
function random() {
randomSeed =
(randomSeed * 1664525 + 1013904223) >>> 0;
return randomSeed / 4294967296;
}
// -------------------------------------------------------------------------
// Ground
// -------------------------------------------------------------------------
const ground = new THREE.Mesh(
new THREE.PlaneGeometry(90, 90),
new THREE.MeshStandardMaterial({
color: 0xe8ebdf,
roughness: 1
})
);
ground.rotation.x = -Math.PI / 2;
ground.position.y = -0.8;
ground.receiveShadow = true;
scene.add(ground);
// -------------------------------------------------------------------------
// Enlarged leaf-surface patch
// -------------------------------------------------------------------------
const patchGroup = new THREE.Group();
patchGroup.name = "Enlarged leaf epidermis";
scene.add(patchGroup);
// Layered slab makes the enlarged patch read as a cut section.
addBox(
patchGroup,
[12.78, 0.48, 9.86],
[0, -0.42, 0],
materials.base
);
addBox(
patchGroup,
[12.9, 0.12, 9.98],
[0, -0.72, 0],
materials.baseDark
);
// Exposed voxel edge on the near and right sides.
const edgeItems = [];
for (let i = 0; i < 17; i++) {
const x = (i - 8) * 0.72;
edgeItems.push({
x,
y: -0.34 + (random() - 0.5) * 0.025,
z: 4.89,
sx: 0.63,
sy: 0.25,
sz: 0.17,
color: new THREE.Color().setHSL(
0.25 + random() * 0.025,
0.28 + random() * 0.08,
0.32 + random() * 0.09
)
});
}
for (let j = 0; j < 13; j++) {
const z = (j - 6) * 0.72;
edgeItems.push({
x: 6.28,
y: -0.34 + (random() - 0.5) * 0.025,
z,
sx: 0.17,
sy: 0.25,
sz: 0.63,
color: new THREE.Color().setHSL(
0.25 + random() * 0.025,
0.28 + random() * 0.08,
0.32 + random() * 0.09
)
});
}
addInstances(patchGroup, edgeItems, materials.instance, "Exposed edge voxels");
// Pavement cells: each cell is a 2 × 2 cluster of voxels.
// Gaps between clusters clearly reveal individual epidermal cells.
const pavementItems = [];
const chloroplastItems = [];
const cellTops = [];
const nx = 17;
const nz = 13;
const pitch = 0.72;
for (let i = 0; i < nx; i++) {
for (let j = 0; j < nz; j++) {
const cellX = (i - (nx - 1) / 2) * pitch;
const cellZ = (j - (nz - 1) / 2) * pitch;
// Remove pavement cells beneath and immediately around the stoma.
if (Math.abs(cellX) < 1.01 && Math.abs(cellZ) < 2.05) {
continue;
}
let cellTop = 0;
for (const sx of [-1, 1]) {
for (const sz of [-1, 1]) {
const height = 0.235 + random() * 0.09;
cellTop = Math.max(cellTop, height);
pavementItems.push({
x: cellX + sx * 0.153 + (random() - 0.5) * 0.01,
y: (height - 0.15) / 2,
z: cellZ + sz * 0.153 + (random() - 0.5) * 0.01,
sx: 0.292,
sy: height,
sz: 0.292,
color: new THREE.Color().setHSL(
0.245 + random() * 0.028,
0.30 + random() * 0.09,
0.39 + random() * 0.13
)
});
}
}
cellTops.push({ x: cellX, z: cellZ, top: cellTop });
// Tiny square chloroplast details on selected pavement cells.
if (random() > 0.58) {
chloroplastItems.push({
x: cellX + (random() - 0.5) * 0.34,
y: cellTop + 0.012,
z: cellZ + (random() - 0.5) * 0.34,
sx: 0.045 + random() * 0.025,
sy: 0.022,
sz: 0.045 + random() * 0.025,
color: new THREE.Color(0x315e35)
});
}
}
}
addInstances(
patchGroup,
pavementItems,
materials.instance,
"Pavement epidermal cells"
);
addInstances(
patchGroup,
chloroplastItems,
materials.instance,
"Pavement chloroplast voxels"
);
// -------------------------------------------------------------------------
// One stoma: recessed pore and two separate voxel-built guard cells
// -------------------------------------------------------------------------
// Recessed dark floor of the pore.
addBox(
patchGroup,
[0.39, 0.055, 3.16],
[0, -0.145, 0],
materials.pore,
false
);
// Dark inner walls emphasize the depth of the opening.
addBox(
patchGroup,
[0.045, 0.31, 3.14],
[-0.212, 0.005, 0],
materials.poreWall,
false
);
addBox(
patchGroup,
[0.045, 0.31, 3.14],
[0.212, 0.005, 0],
materials.poreWall,
false
);
const guardItems = [];
const guardChloroplasts = [];
const guardRows = 13;
for (const side of [-1, 1]) {
for (let row = 0; row < guardRows; row++) {
const z = (row - (guardRows - 1) / 2) * 0.25;
const t = (row - (guardRows - 1) / 2) / ((guardRows - 1) / 2);
const t2 = t * t;
// Concave inner edge and thicker middle produce a kidney-like profile.
const innerEdge = 0.19 + 0.14 * t2;
const outerEdge = 0.70 + 0.15 * (1 - t2);
for (
let x = innerEdge + 0.105;
x < outerEdge;
x += 0.22
) {
guardItems.push({
x: side * x,
y: 0.135,
z,
sx: 0.205,
sy: 0.57,
sz: 0.225,
color: new THREE.Color().setHSL(
side < 0 ? 0.235 : 0.215,
0.60 + random() * 0.08,
0.47 + random() * 0.09
)
});
if (random() > 0.76) {
guardChloroplasts.push({
x: side * x,
y: 0.432,
z,
sx: 0.065,
sy: 0.026,
sz: 0.065,
color: new THREE.Color(0x396c2d)
});
}
}
}
}
addInstances(
patchGroup,
guardItems,
materials.guard,
"Paired kidney-shaped guard cells"
);
addInstances(
patchGroup,
guardChloroplasts,
materials.instance,
"Guard-cell chloroplast voxels"
);
// Highlight one pavement cell for the annotation.
const selectedCellX = 2.88;
const selectedCellZ = 2.16;
const selectedY = 0.35;
addBox(
patchGroup,
[0.72, 0.03, 0.026],
[selectedCellX, selectedY, selectedCellZ - 0.36],
materials.selection
);
addBox(
patchGroup,
[0.72, 0.03, 0.026],
[selectedCellX, selectedY, selectedCellZ + 0.36],
materials.selection
);
addBox(
patchGroup,
[0.026, 0.03, 0.72],
[selectedCellX - 0.36, selectedY, selectedCellZ],
materials.selection
);
addBox(
patchGroup,
[0.026, 0.03, 0.72],
[selectedCellX + 0.36, selectedY, selectedCellZ],
materials.selection
);
// Conceptual 2 mm bracket along the front edge of the patch.
addBox(
patchGroup,
[12.12, 0.026, 0.026],
[0, -0.15, 5.14],
materials.bracket
);
addBox(
patchGroup,
[0.026, 0.026, 0.24],
[-6.06, -0.15, 5.14],
materials.bracket
);
addBox(
patchGroup,
[0.026, 0.026, 0.24],
[6.06, -0.15, 5.14],
materials.bracket
);
// -------------------------------------------------------------------------
// Whole-leaf scale reference
// -------------------------------------------------------------------------
const referenceX = 9.25;
const referenceZ = -1.35;
addBox(
scene,
[5.35, 0.68, 3.24],
[referenceX, -0.43, referenceZ],
materials.platformSide
);
addBox(
scene,
[5.48, 0.08, 3.36],
[referenceX, -0.05, referenceZ],
materials.platform
);
const leafGroup = new THREE.Group();
leafGroup.position.set(referenceX, 0, referenceZ);
leafGroup.rotation.y = -0.10;
leafGroup.name = "Whole leaf scale reference";
scene.add(leafGroup);
const leafItems = [];
const leafNX = 34;
const leafNZ = 15;
const leafStep = 0.14;
for (let i = 0; i < leafNX; i++) {
const t = i / (leafNX - 1);
const x = (i - (leafNX - 1) / 2) * leafStep;
const halfWidth =
0.96 * Math.pow(Math.max(0, Math.sin(Math.PI * t)), 0.72);
for (let j = 0; j < leafNZ; j++) {
const z = (j - (leafNZ - 1) / 2) * leafStep;
if (Math.abs(z) > halfWidth + 0.025) continue;
const isMidrib = j === (leafNZ - 1) / 2;
const isSideVein =
j !== (leafNZ - 1) / 2 &&
Math.abs(j - (leafNZ - 1) / 2) <= 4 &&
(i + Math.abs(j - (leafNZ - 1) / 2) * 3) % 12 < 2;
let color;
if (isMidrib) {
color = new THREE.Color(0xa9bd65);
} else if (isSideVein) {
color = new THREE.Color().setHSL(
0.22,
0.42,
0.39 + random() * 0.04
);
} else {
color = new THREE.Color().setHSL(
0.285 + random() * 0.025,
0.38 + random() * 0.09,
0.29 + random() * 0.14
);
}
leafItems.push({
x,
y: 0.10,
z,
sx: 0.135,
sy: 0.16,
sz: 0.135,
color
});
}
}
// Block-built petiole.
for (let i = 0; i < 4; i++) {
leafItems.push({
x: -2.64 + i * 0.14,
y: 0.09,
z: 0,
sx: 0.155,
sy: 0.145,
sz: 0.155,
color: new THREE.Color(i === 0 ? 0x6c6844 : 0x617344)
});
}
addInstances(
leafGroup,
leafItems,
materials.instance,
"Voxel whole leaf"
);
// Conceptual 8 cm dimension bracket.
addBox(
leafGroup,
[4.64, 0.026, 0.026],
[0, 0.005, 1.27],
materials.bracket
);
addBox(
leafGroup,
[0.026, 0.026, 0.22],
[-2.32, 0.005, 1.27],
materials.bracket
);
addBox(
leafGroup,
[0.026, 0.026, 0.22],
[2.32, 0.005, 1.27],
materials.bracket
);
// -------------------------------------------------------------------------
// Annotation leaders, projected directly from 3D positions
// -------------------------------------------------------------------------
const leaders = document.querySelector("#leaders");
const svgNS = "http://www.w3.org/2000/svg";
const labelSpecs = [
{
element: document.querySelector("#label-patch"),
anchor: () => new THREE.Vector3(-4.5, 0.42, 4.35),
offset: [-70, 112],
accent: "#d5a735"
},
{
element: document.querySelector("#label-pore"),
anchor: () => new THREE.Vector3(0, 0.10, 0),
offset: [-112, 110],
accent: "#071813"
},
{
element: document.querySelector("#label-guard"),
anchor: () => new THREE.Vector3(0.55, 0.46, 0.75),
offset: [135, -72],
accent: "#8fc84d"
},
{
element: document.querySelector("#label-cell"),
anchor: () =>
new THREE.Vector3(
selectedCellX,
selectedY + 0.08,
selectedCellZ
),
offset: [135, 62],
accent: "#688e50"
},
{
element: document.querySelector("#label-leaf"),
anchor: () =>
leafGroup.localToWorld(new THREE.Vector3(0, 0.30, 0)),
offset: [92, -92],
accent: "#315d39"
}
];
for (const spec of labelSpecs) {
const line = document.createElementNS(svgNS, "line");
line.setAttribute("stroke", spec.accent);
line.setAttribute("stroke-width", "1.5");
line.setAttribute("stroke-dasharray", "4 5");
line.setAttribute("opacity", "0.62");
const dot = document.createElementNS(svgNS, "circle");
dot.setAttribute("r", "4");
dot.setAttribute("fill", spec.accent);
dot.setAttribute("stroke", "#f7f7eb");
dot.setAttribute("stroke-width", "2");
leaders.append(line, dot);
spec.line = line;
spec.dot = dot;
}
function updateLabels() {
const width = window.innerWidth;
const height = window.innerHeight;
for (const spec of labelSpecs) {
const world = spec.anchor();
const projected = world.clone().project(camera);
const visible =
projected.z > -1 &&
projected.z < 1 &&
getComputedStyle(spec.element).display !== "none";
spec.line.style.display = visible ? "" : "none";
spec.dot.style.display = visible ? "" : "none";
if (!visible) continue;
const sx = (projected.x * 0.5 + 0.5) * width;
const sy = (-projected.y * 0.5 + 0.5) * height;
const labelX = THREE.MathUtils.clamp(
sx + spec.offset[0],
80,
width - 80
);
const labelY = THREE.MathUtils.clamp(
sy + spec.offset[1],
95,
height - 75
);
spec.element.style.left = `${labelX}px`;
spec.element.style.top = `${labelY}px`;
spec.line.setAttribute("x1", sx);
spec.line.setAttribute("y1", sy);
spec.line.setAttribute("x2", labelX);
spec.line.setAttribute("y2", labelY);
spec.dot.setAttribute("cx", sx);
spec.dot.setAttribute("cy", sy);
}
}
// -------------------------------------------------------------------------
// Custom camera controls: orbit, wheel zoom, and pinch zoom
// -------------------------------------------------------------------------
const defaultTarget = new THREE.Vector3(1.35, -0.05, 0);
const view = {
theta: 0.67,
phi: 0.94,
radius: 26,
target: defaultTarget.clone()
};
const goal = {
theta: view.theta,
phi: view.phi,
radius: view.radius,
target: defaultTarget.clone()
};
function initialRadius() {
const aspect = window.innerWidth / window.innerHeight;
const fitDistance =
9.5 / (Math.tan(THREE.MathUtils.degToRad(camera.fov / 2)) * aspect);
return Math.max(26, fitDistance);
}
goal.radius = initialRadius();
view.radius = goal.radius;
function updateCamera() {
const spherical = new THREE.Spherical(
view.radius,
view.phi,
view.theta
);
camera.position.setFromSpherical(spherical).add(view.target);
camera.lookAt(view.target);
}
function resetView() {
goal.theta = 0.67;
goal.phi = 0.94;
goal.radius = initialRadius();
goal.target.copy(defaultTarget);
}
const pointers = new Map();
let pinchDistance = 0;
let hasInteracted = false;
function pointerPairDistance() {
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
});
hasInteracted = true;
if (pointers.size === 2) {
pinchDistance = pointerPairDistance();
}
});
canvas.addEventListener("pointermove", (event) => {
const previous = pointers.get(event.pointerId);
if (!previous) return;
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;
goal.theta -= dx * 0.0055;
goal.phi = THREE.MathUtils.clamp(
goal.phi - dy * 0.0048,
0.34,
1.34
);
} else if (pointers.size === 2) {
const nextDistance = pointerPairDistance();
if (pinchDistance > 0 && nextDistance > 0) {
goal.radius = THREE.MathUtils.clamp(
goal.radius * (pinchDistance / nextDistance),
14,
52
);
}
pinchDistance = nextDistance;
}
});
function releasePointer(event) {
pointers.delete(event.pointerId);
pinchDistance = pointers.size === 2 ? pointerPairDistance() : 0;
}
canvas.addEventListener("pointerup", releasePointer);
canvas.addEventListener("pointercancel", releasePointer);
canvas.addEventListener("lostpointercapture", releasePointer);
canvas.addEventListener(
"wheel",
(event) => {
event.preventDefault();
hasInteracted = true;
goal.radius = THREE.MathUtils.clamp(
goal.radius * Math.exp(event.deltaY * 0.001),
14,
52
);
},
{ passive: false }
);
canvas.addEventListener("dblclick", resetView);
document.querySelector("#reset").addEventListener("click", resetView);
const labelButton = document.querySelector("#toggle-labels");
labelButton.addEventListener("click", () => {
const labelsVisible = !document.body.classList.contains("labels-off");
document.body.classList.toggle("labels-off", labelsVisible);
labelButton.setAttribute("aria-pressed", String(!labelsVisible));
labelButton.textContent = labelsVisible ? "Show labels" : "Hide labels";
});
// -------------------------------------------------------------------------
// Resize and render loop
// -------------------------------------------------------------------------
function resize() {
const width = window.innerWidth;
const height = window.innerHeight;
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(width, height, false);
camera.aspect = width / height;
camera.updateProjectionMatrix();
leaders.setAttribute("viewBox", `0 0 ${width} ${height}`);
if (!hasInteracted) {
goal.radius = initialRadius();
view.radius = goal.radius;
}
}
window.addEventListener("resize", resize);
resize();
let lastTime = performance.now();
function animate(time) {
const delta = Math.min((time - lastTime) / 1000, 0.05);
lastTime = time;
const smoothing = 1 - Math.pow(0.001, delta);
view.theta += (goal.theta - view.theta) * smoothing;
view.phi += (goal.phi - view.phi) * smoothing;
view.radius += (goal.radius - view.radius) * smoothing;
view.target.lerp(goal.target, smoothing);
updateCamera();
renderer.render(scene, camera);
updateLabels();
requestAnimationFrame(animate);
}
updateCamera();
renderer.render(scene, camera);
updateLabels();
document.querySelector("#loading").classList.add("done");
requestAnimationFrame(animate);
</script>
</body>
</html>
```