Prompt specification
Build or customize the Three.js 3D scene "Spreading cell on an adhesion surface" 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: Spreading cell on an adhesion surface
Category: Biology & Nature · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/spreading-cell-on-an-adhesion-surface-x0817
Scene Description Prompt:
Build a self-contained HTML scene with Three.js from cdnjs featuring cell migration. Model a flattened cell with a leading edge, trailing region, and discrete attachment markers; label its geometry schematic and maintain contact with the substrate. Low-poly.
Working Three.js Starter (spreading-cell-on-an-adhesion-surface-x0817):
```html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Cell Migration — Low-Poly Schematic</title>
<style>
:root {
--ink: #183047;
--muted: #68798d;
--cyan: #45cdb9;
--violet: #695bc8;
--coral: #ff735e;
--panel: rgba(255, 255, 255, 0.86);
--border: rgba(43, 71, 96, 0.13);
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #edf2f6;
color: var(--ink);
font-family: Inter, ui-sans-serif, system-ui, -apple-system,
BlinkMacSystemFont, "Segoe UI", sans-serif;
}
#scene,
#scene canvas {
position: absolute;
inset: 0;
width: 100%;
height: 100%;
}
#scene canvas {
display: block;
cursor: grab;
touch-action: none;
}
#scene canvas:active {
cursor: grabbing;
}
#leaders {
position: absolute;
inset: 0;
width: 100%;
height: 100%;
pointer-events: none;
z-index: 2;
}
#leaders path {
fill: none;
stroke: rgba(45, 67, 87, 0.48);
stroke-width: 1.2;
stroke-dasharray: 3 4;
}
#leaders circle {
fill: #ffffff;
stroke: rgba(45, 67, 87, 0.65);
stroke-width: 1.4;
}
#labels {
position: absolute;
inset: 0;
pointer-events: none;
z-index: 3;
}
.annotation {
position: absolute;
display: flex;
align-items: flex-start;
gap: 9px;
min-width: 164px;
max-width: 220px;
padding: 10px 12px;
border: 1px solid var(--border);
border-radius: 11px;
background: var(--panel);
box-shadow: 0 8px 24px rgba(35, 58, 79, 0.09);
backdrop-filter: blur(10px);
transform: translate(-50%, -50%);
user-select: none;
}
.annotation .index {
display: grid;
flex: 0 0 22px;
width: 22px;
height: 22px;
place-items: center;
border-radius: 7px;
background: color-mix(in srgb, var(--accent) 16%, white);
color: var(--accent);
font-size: 10px;
font-weight: 800;
}
.annotation strong {
display: block;
margin-top: 1px;
color: var(--ink);
font-size: 11px;
letter-spacing: 0.02em;
}
.annotation small {
display: block;
margin-top: 3px;
color: var(--muted);
font-size: 10px;
line-height: 1.35;
}
.hud {
position: absolute;
z-index: 5;
pointer-events: none;
}
#heading {
top: 24px;
left: 24px;
width: min(360px, calc(100vw - 48px));
}
.eyebrow {
color: #249a91;
font-size: 9px;
font-weight: 800;
letter-spacing: 0.18em;
text-transform: uppercase;
}
#heading h1 {
margin: 6px 0 4px;
font-size: clamp(24px, 3vw, 36px);
font-weight: 750;
letter-spacing: -0.045em;
}
#heading p {
margin: 0;
color: var(--muted);
font-size: 12px;
line-height: 1.55;
}
.badges {
display: flex;
flex-wrap: wrap;
gap: 6px;
margin-top: 12px;
}
.badge {
padding: 5px 8px;
border: 1px solid var(--border);
border-radius: 999px;
background: rgba(255, 255, 255, 0.66);
color: #53677a;
font-size: 8px;
font-weight: 750;
letter-spacing: 0.1em;
text-transform: uppercase;
}
#legend {
bottom: 22px;
left: 22px;
display: grid;
grid-template-columns: repeat(2, auto);
gap: 8px 18px;
padding: 12px 14px;
border: 1px solid var(--border);
border-radius: 12px;
background: var(--panel);
box-shadow: 0 8px 24px rgba(35, 58, 79, 0.07);
backdrop-filter: blur(10px);
}
.legend-item {
display: flex;
align-items: center;
gap: 8px;
color: #52667a;
font-size: 10px;
white-space: nowrap;
}
.swatch {
width: 9px;
height: 9px;
border-radius: 3px;
background: var(--swatch);
box-shadow: inset 0 0 0 1px rgba(0, 0, 0, 0.08);
}
#controls {
right: 22px;
bottom: 22px;
display: flex;
align-items: center;
gap: 8px;
padding: 8px;
border: 1px solid var(--border);
border-radius: 12px;
background: var(--panel);
box-shadow: 0 8px 24px rgba(35, 58, 79, 0.07);
backdrop-filter: blur(10px);
pointer-events: auto;
}
button {
padding: 8px 10px;
border: 1px solid rgba(43, 71, 96, 0.13);
border-radius: 8px;
background: #ffffff;
color: #3f566b;
font: inherit;
font-size: 10px;
font-weight: 700;
cursor: pointer;
}
button:hover {
border-color: rgba(69, 205, 185, 0.55);
color: #168d82;
}
.toggle {
display: flex;
align-items: center;
gap: 6px;
padding: 0 5px;
color: #52667a;
font-size: 10px;
white-space: nowrap;
}
.toggle input {
accent-color: var(--cyan);
}
#hint {
bottom: 86px;
right: 24px;
color: rgba(54, 75, 93, 0.58);
font-size: 9px;
letter-spacing: 0.04em;
}
#loading {
position: absolute;
inset: 0;
z-index: 10;
display: grid;
place-items: center;
background: #edf2f6;
color: #5c7184;
font-size: 11px;
letter-spacing: 0.12em;
text-transform: uppercase;
}
@media (max-width: 680px) {
#heading {
top: 16px;
left: 16px;
}
#heading p {
display: none;
}
.badges {
margin-top: 8px;
}
#legend {
right: 16px;
bottom: 72px;
left: 16px;
grid-template-columns: repeat(2, 1fr);
}
#controls {
right: 16px;
bottom: 16px;
}
#hint {
display: none;
}
.annotation {
min-width: 135px;
max-width: 165px;
padding: 8px;
}
.annotation small {
display: none;
}
}
</style>
</head>
<body>
<div id="scene"></div>
<svg id="leaders" aria-hidden="true"></svg>
<div id="labels"></div>
<section id="heading" class="hud">
<div class="eyebrow">Interactive biology schematic</div>
<h1>Cell migration</h1>
<p>
A polarized, low-poly cell crawling across a substrate while maintaining
discrete focal contacts.
</p>
<div class="badges">
<span class="badge">Low-poly mesh</span>
<span class="badge">Substrate contact</span>
<span class="badge">Directional motion</span>
</div>
</section>
<section id="legend" class="hud" aria-label="Scene legend">
<div class="legend-item">
<span class="swatch" style="--swatch:#695bc8"></span>
Trailing region
</div>
<div class="legend-item">
<span class="swatch" style="--swatch:#45cdb9"></span>
Leading edge
</div>
<div class="legend-item">
<span class="swatch" style="--swatch:#ff735e"></span>
Attachment marker
</div>
<div class="legend-item">
<span class="swatch" style="--swatch:#dbe3e9"></span>
Substrate
</div>
</section>
<div id="hint" class="hud">Drag to orbit · Scroll to zoom · Double-click to reset</div>
<section id="controls" class="hud">
<button id="pause" type="button">Pause motion</button>
<button id="reset" type="button">Reset view</button>
<label class="toggle">
<input id="show-labels" type="checkbox" checked>
Labels
</label>
</section>
<div id="loading">Loading Three.js…</div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
const container = document.getElementById("scene");
const labelsLayer = document.getElementById("labels");
const leaders = document.getElementById("leaders");
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xedf2f6);
scene.fog = new THREE.Fog(0xedf2f6, 15, 29);
const camera = new THREE.PerspectiveCamera(
40,
innerWidth / innerHeight,
0.1,
100
);
const renderer = new THREE.WebGLRenderer({
antialias: true,
alpha: false
});
renderer.setPixelRatio(Math.min(devicePixelRatio, 2));
renderer.setSize(innerWidth, innerHeight);
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.12;
renderer.domElement.setAttribute(
"aria-label",
"Interactive low-poly cell migration scene"
);
container.appendChild(renderer.domElement);
// ---------------------------------------------------------------------
// Lighting
// ---------------------------------------------------------------------
const hemisphere = new THREE.HemisphereLight(0xffffff, 0x8c9aaa, 2.15);
scene.add(hemisphere);
const keyLight = new THREE.DirectionalLight(0xffffff, 3.1);
keyLight.position.set(4.5, 8, 5.5);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(2048, 2048);
keyLight.shadow.camera.left = -7;
keyLight.shadow.camera.right = 7;
keyLight.shadow.camera.top = 7;
keyLight.shadow.camera.bottom = -7;
keyLight.shadow.camera.near = 0.5;
keyLight.shadow.camera.far = 25;
keyLight.shadow.bias = -0.0007;
scene.add(keyLight);
const rimLight = new THREE.DirectionalLight(0x92e7ff, 1.25);
rimLight.position.set(-5, 3.5, -4);
scene.add(rimLight);
// ---------------------------------------------------------------------
// Substrate and migration guide
// ---------------------------------------------------------------------
const substrate = new THREE.Mesh(
new THREE.PlaneGeometry(24, 16),
new THREE.MeshStandardMaterial({
color: 0xe5ebf0,
roughness: 0.94,
metalness: 0
})
);
substrate.rotation.x = -Math.PI / 2;
substrate.receiveShadow = true;
scene.add(substrate);
const grid = new THREE.GridHelper(24, 24, 0xaebbc7, 0xd2dbe3);
grid.position.y = 0.004;
grid.scale.z = 0.665;
grid.material.transparent = true;
grid.material.opacity = 0.3;
grid.material.depthWrite = false;
scene.add(grid);
const pathBand = new THREE.Mesh(
new THREE.PlaneGeometry(13, 2.25),
new THREE.MeshBasicMaterial({
color: 0x69cfc2,
transparent: true,
opacity: 0.055,
depthWrite: false
})
);
pathBand.rotation.x = -Math.PI / 2;
pathBand.position.y = 0.007;
scene.add(pathBand);
const arrowShape = new THREE.Shape();
arrowShape.moveTo(0, -0.12);
arrowShape.lineTo(0.82, -0.12);
arrowShape.lineTo(0.82, -0.34);
arrowShape.lineTo(1.25, 0);
arrowShape.lineTo(0.82, 0.34);
arrowShape.lineTo(0.82, 0.12);
arrowShape.lineTo(0, 0.12);
arrowShape.closePath();
const directionArrow = new THREE.Mesh(
new THREE.ShapeGeometry(arrowShape),
new THREE.MeshBasicMaterial({
color: 0x29aa9d,
transparent: true,
opacity: 0.36,
side: THREE.DoubleSide,
depthWrite: false
})
);
directionArrow.rotation.x = -Math.PI / 2;
directionArrow.position.set(3.0, 0.018, 0);
scene.add(directionArrow);
// ---------------------------------------------------------------------
// Low-poly cell geometry
//
// The top is triangulated as a coarse fan, the underside is held at a
// constant height immediately above the substrate, and the perimeter is
// closed with triangular side walls.
// ---------------------------------------------------------------------
const CONTOUR_COUNT = 32;
const BOTTOM_Y = 0.018;
const contour = Array.from(
{ length: CONTOUR_COUNT },
() => new THREE.Vector3()
);
const topHeights = new Float32Array(CONTOUR_COUNT);
const triangleCount = CONTOUR_COUNT * 4;
const cellPositions = new Float32Array(triangleCount * 9);
const cellColors = new Float32Array(triangleCount * 9);
const cellGeometry = new THREE.BufferGeometry();
cellGeometry.setAttribute(
"position",
new THREE.BufferAttribute(cellPositions, 3).setUsage(
THREE.DynamicDrawUsage
)
);
cellGeometry.setAttribute(
"color",
new THREE.BufferAttribute(cellColors, 3).setUsage(
THREE.DynamicDrawUsage
)
);
const cellMaterial = new THREE.MeshStandardMaterial({
vertexColors: true,
roughness: 0.5,
metalness: 0.025,
flatShading: true
});
const cellGroup = new THREE.Group();
scene.add(cellGroup);
const cellMesh = new THREE.Mesh(cellGeometry, cellMaterial);
cellMesh.castShadow = true;
cellMesh.receiveShadow = true;
cellGroup.add(cellMesh);
// A soft contact shadow remains directly on the substrate.
const shadowPositions = new Float32Array(CONTOUR_COUNT * 9);
const shadowGeometry = new THREE.BufferGeometry();
shadowGeometry.setAttribute(
"position",
new THREE.BufferAttribute(shadowPositions, 3).setUsage(
THREE.DynamicDrawUsage
)
);
const contactShadow = new THREE.Mesh(
shadowGeometry,
new THREE.MeshBasicMaterial({
color: 0x173249,
transparent: true,
opacity: 0.105,
depthWrite: false,
side: THREE.DoubleSide
})
);
cellGroup.add(contactShadow);
// Bright membrane perimeter.
const rimPositions = new Float32Array(CONTOUR_COUNT * 3);
const rimGeometry = new THREE.BufferGeometry();
rimGeometry.setAttribute(
"position",
new THREE.BufferAttribute(rimPositions, 3).setUsage(
THREE.DynamicDrawUsage
)
);
const membraneRim = new THREE.LineLoop(
rimGeometry,
new THREE.LineBasicMaterial({
color: 0xc0fff5,
transparent: true,
opacity: 0.78
})
);
cellGroup.add(membraneRim);
// Faceted nucleus embedded in the trailing half.
const nucleus = new THREE.Mesh(
new THREE.IcosahedronGeometry(0.42, 1),
new THREE.MeshStandardMaterial({
color: 0x3e397d,
roughness: 0.62,
metalness: 0.02,
flatShading: true
})
);
nucleus.position.set(-0.38, 0.245, 0.03);
nucleus.scale.set(1.28, 0.38, 0.9);
nucleus.rotation.set(0.08, -0.24, 0.06);
nucleus.castShadow = true;
cellGroup.add(nucleus);
// ---------------------------------------------------------------------
// Discrete attachment markers
// ---------------------------------------------------------------------
const markerGroups = [];
const markerIndices = [2, 5, 8, 11, 14, 17, 20, 23, 26, 29];
for (const contourIndex of markerIndices) {
const marker = new THREE.Group();
const disc = new THREE.Mesh(
new THREE.CylinderGeometry(0.072, 0.084, 0.024, 8),
new THREE.MeshStandardMaterial({
color: 0xff735e,
emissive: 0x54140d,
emissiveIntensity: 0.18,
roughness: 0.58,
flatShading: true
})
);
disc.castShadow = true;
marker.add(disc);
const halo = new THREE.Mesh(
new THREE.RingGeometry(0.09, 0.125, 12),
new THREE.MeshBasicMaterial({
color: 0xff735e,
transparent: true,
opacity: 0.38,
side: THREE.DoubleSide,
depthWrite: false
})
);
halo.rotation.x = -Math.PI / 2;
halo.position.y = -0.01;
marker.add(halo);
marker.userData.contourIndex = contourIndex;
markerGroups.push(marker);
cellGroup.add(marker);
}
const rearColor = new THREE.Color(0x695bc8);
const middleColor = new THREE.Color(0x328fa9);
const frontColor = new THREE.Color(0x49d1bb);
const undersideColor = new THREE.Color(0x355e72);
function faceColor(cosine, index) {
const color = new THREE.Color();
if (cosine < 0) {
color.lerpColors(rearColor, middleColor, THREE.MathUtils.clamp(
(cosine + 0.68) / 0.68,
0,
1
));
} else {
color.lerpColors(middleColor, frontColor, THREE.MathUtils.clamp(
cosine / 0.68,
0,
1
));
}
const variation = 0.92 + ((index * 37) % 11) * 0.006;
color.multiplyScalar(variation);
return color;
}
function updateCellGeometry(time) {
// A broad +X lamellipodium and a narrower, tapered -X rear.
for (let i = 0; i < CONTOUR_COUNT; i++) {
const theta = (i / CONTOUR_COUNT) * Math.PI * 2;
const c = Math.cos(theta);
const s = Math.sin(theta);
const front = Math.max(c, 0);
const rear = Math.max(-c, 0);
let x = c >= 0
? 1.72 * Math.pow(c, 0.72)
: -1.48 * Math.pow(-c, 0.9);
const widthFactor = 0.68 + 0.48 * ((c + 1) * 0.5);
let z =
1.18 *
widthFactor *
Math.sign(s) *
Math.pow(Math.abs(s), 1.08);
// Small leading-edge protrusions.
const leadingLobe = Math.exp(-(theta * theta) / 0.05);
x += leadingLobe * (
0.04 +
0.05 * (0.5 + 0.5 * Math.sin(time * 1.5 + i * 0.45))
);
z += leadingLobe * 0.025 * Math.sin(time * 1.8 + i * 0.5);
// Gentle contraction at the trailing edge.
x -= rear * rear * (
0.045 +
0.025 * (0.5 + 0.5 * Math.sin(time * 0.9))
);
// Low-amplitude side undulation.
z += 0.018 * Math.sin(theta * 3 + time * 0.35);
contour[i].set(x, 0, z);
topHeights[i] =
0.11 +
0.055 * ((c + 1) * 0.5) +
0.026 * front +
0.008 * Math.sin(theta * 2 + time * 0.7);
}
let positionOffset = 0;
let colorOffset = 0;
function putPosition(x, y, z) {
cellPositions[positionOffset++] = x;
cellPositions[positionOffset++] = y;
cellPositions[positionOffset++] = z;
}
function putColor(color) {
cellColors[colorOffset++] = color.r;
cellColors[colorOffset++] = color.g;
cellColors[colorOffset++] = color.b;
}
for (let i = 0; i < CONTOUR_COUNT; i++) {
const next = (i + 1) % CONTOUR_COUNT;
const a = contour[i];
const b = contour[next];
const ay = topHeights[i];
const by = topHeights[next];
const thetaMid =
((i + 0.5) / CONTOUR_COUNT) * Math.PI * 2;
const topTint = faceColor(Math.cos(thetaMid), i);
const sideTint = topTint.clone().multiplyScalar(0.68);
// Top triangle, wound upward.
putPosition(0, 0.19, 0);
putPosition(b.x, by, b.z);
putPosition(a.x, ay, a.z);
putColor(topTint);
putColor(topTint);
putColor(topTint);
// Underside triangle, held at the contact height.
putPosition(0, BOTTOM_Y, 0);
putPosition(a.x, BOTTOM_Y, a.z);
putPosition(b.x, BOTTOM_Y, b.z);
putColor(undersideColor);
putColor(undersideColor);
putColor(undersideColor);
// Two side-wall triangles.
putPosition(a.x, ay, a.z);
putPosition(b.x, by, b.z);
putPosition(a.x, BOTTOM_Y, a.z);
putColor(sideTint);
putColor(sideTint);
putColor(sideTint);
putPosition(b.x, by, b.z);
putPosition(b.x, BOTTOM_Y, b.z);
putPosition(a.x, BOTTOM_Y, a.z);
putColor(sideTint);
putColor(sideTint);
putColor(sideTint);
}
cellGeometry.attributes.position.needsUpdate = true;
cellGeometry.attributes.color.needsUpdate = true;
cellGeometry.computeVertexNormals();
cellGeometry.computeBoundingSphere();
// Contact shadow.
let shadowOffset = 0;
function putShadow(x, z) {
shadowPositions[shadowOffset++] = x;
shadowPositions[shadowOffset++] = 0.011;
shadowPositions[shadowOffset++] = z;
}
for (let i = 0; i < CONTOUR_COUNT; i++) {
const a = contour[i];
const b = contour[(i + 1) % CONTOUR_COUNT];
putShadow(0, 0);
putShadow(a.x * 1.025, a.z * 1.025);
putShadow(b.x * 1.025, b.z * 1.025);
}
shadowGeometry.attributes.position.needsUpdate = true;
shadowGeometry.computeBoundingSphere();
// Membrane rim.
for (let i = 0; i < CONTOUR_COUNT; i++) {
const p = contour[i];
rimPositions[i * 3] = p.x;
rimPositions[i * 3 + 1] = topHeights[i] + 0.007;
rimPositions[i * 3 + 2] = p.z;
}
rimGeometry.attributes.position.needsUpdate = true;
rimGeometry.computeBoundingSphere();
// Keep the attachment markers at substrate level while following the
// changing cell perimeter.
for (const marker of markerGroups) {
const index = marker.userData.contourIndex;
const point = contour[index];
const outward = new THREE.Vector3(point.x, 0, point.z).normalize();
marker.position.set(
point.x + outward.x * 0.065,
0.012,
point.z + outward.z * 0.065
);
}
}
// ---------------------------------------------------------------------
// Projected HTML annotations
// ---------------------------------------------------------------------
const svgNS = "http://www.w3.org/2000/svg";
function makeAnnotation({
id,
index,
title,
detail,
accent,
offset,
anchor
}) {
const element = document.createElement("div");
element.className = "annotation";
element.id = id;
element.style.setProperty("--accent", accent);
element.innerHTML = `
<span class="index">${index}</span>
<div>
<strong>${title}</strong>
<small>${detail}</small>
</div>
`;
labelsLayer.appendChild(element);
const path = document.createElementNS(svgNS, "path");
const dot = document.createElementNS(svgNS, "circle");
dot.setAttribute("r", "3.2");
leaders.append(path, dot);
return { element, path, dot, offset, anchor };
}
const annotations = [
makeAnnotation({
id: "label-leading",
index: "01",
title: "Leading edge",
detail: "Broad protrusive lamellipodium",
accent: "#249f90",
offset: [92, -48],
anchor: () =>
cellGroup.localToWorld(new THREE.Vector3(1.62, 0.19, 0))
}),
makeAnnotation({
id: "label-trailing",
index: "02",
title: "Trailing region",
detail: "Tapered, contractile rear",
accent: "#695bc8",
offset: [-105, -58],
anchor: () =>
cellGroup.localToWorld(new THREE.Vector3(-1.32, 0.15, 0))
}),
makeAnnotation({
id: "label-attachment",
index: "03",
title: "Attachment marker",
detail: "Discrete focal substrate contact",
accent: "#e85f4b",
offset: [-118, 62],
anchor: () => {
const p = new THREE.Vector3();
markerGroups[2].getWorldPosition(p);
return p;
}
}),
makeAnnotation({
id: "label-substrate",
index: "04",
title: "Substrate",
detail: "Stable contact plane · y = 0",
accent: "#71869a",
offset: [-18, 36],
anchor: () => new THREE.Vector3(-3.3, 0.02, 1.55)
}),
makeAnnotation({
id: "label-direction",
index: "05",
title: "Migration direction",
detail: "Polarized forward motion",
accent: "#249f90",
offset: [16, 38],
anchor: () => new THREE.Vector3(3.6, 0.03, 0)
})
];
function updateAnnotations() {
const width = innerWidth;
const height = innerHeight;
const show = document.getElementById("show-labels").checked;
labelsLayer.style.display = show ? "block" : "none";
leaders.style.display = show ? "block" : "none";
if (!show) return;
camera.updateMatrixWorld();
cellGroup.updateMatrixWorld(true);
for (const annotation of annotations) {
const worldPoint = annotation.anchor();
const projected = worldPoint.clone().project(camera);
const screenX = (projected.x * 0.5 + 0.5) * width;
const screenY = (-projected.y * 0.5 + 0.5) * height;
const halfLabelWidth = annotation.element.offsetWidth * 0.5;
const halfLabelHeight = annotation.element.offsetHeight * 0.5;
const labelX = THREE.MathUtils.clamp(
screenX + annotation.offset[0],
halfLabelWidth + 12,
width - halfLabelWidth - 12
);
const labelY = THREE.MathUtils.clamp(
screenY + annotation.offset[1],
halfLabelHeight + 12,
height - halfLabelHeight - 12
);
annotation.element.style.left = `${labelX}px`;
annotation.element.style.top = `${labelY}px`;
const elbowX = screenX + (labelX - screenX) * 0.55;
annotation.path.setAttribute(
"d",
`M ${screenX} ${screenY} L ${elbowX} ${screenY} L ${labelX} ${labelY}`
);
annotation.dot.setAttribute("cx", screenX);
annotation.dot.setAttribute("cy", screenY);
}
}
// ---------------------------------------------------------------------
// Custom orbit controls — no Three.js addons
// ---------------------------------------------------------------------
const orbit = {
target: new THREE.Vector3(0, 0.2, 0),
yaw: 0.63,
pitch: 0.63,
distance: 8.2,
desiredYaw: 0.63,
desiredPitch: 0.63,
desiredDistance: 8.2,
dragging: false,
pointerX: 0,
pointerY: 0
};
function resetView() {
orbit.desiredYaw = 0.63;
orbit.desiredPitch = 0.63;
orbit.desiredDistance = 8.2;
orbit.target.set(0, 0.2, 0);
}
renderer.domElement.addEventListener("pointerdown", (event) => {
if (event.button !== 0) return;
orbit.dragging = true;
orbit.pointerX = event.clientX;
orbit.pointerY = event.clientY;
renderer.domElement.setPointerCapture(event.pointerId);
});
renderer.domElement.addEventListener("pointermove", (event) => {
if (!orbit.dragging) return;
const dx = event.clientX - orbit.pointerX;
const dy = event.clientY - orbit.pointerY;
orbit.pointerX = event.clientX;
orbit.pointerY = event.clientY;
orbit.desiredYaw -= dx * 0.006;
orbit.desiredPitch = THREE.MathUtils.clamp(
orbit.desiredPitch + dy * 0.005,
0.22,
1.35
);
});
function stopDragging(event) {
orbit.dragging = false;
if (renderer.domElement.hasPointerCapture(event.pointerId)) {
renderer.domElement.releasePointerCapture(event.pointerId);
}
}
renderer.domElement.addEventListener("pointerup", stopDragging);
renderer.domElement.addEventListener("pointercancel", stopDragging);
renderer.domElement.addEventListener(
"wheel",
(event) => {
event.preventDefault();
orbit.desiredDistance = THREE.MathUtils.clamp(
orbit.desiredDistance + event.deltaY * 0.008,
4.4,
14
);
},
{ passive: false }
);
renderer.domElement.addEventListener("dblclick", resetView);
document.getElementById("reset").addEventListener("click", resetView);
let paused = false;
const pauseButton = document.getElementById("pause");
pauseButton.addEventListener("click", () => {
paused = !paused;
pauseButton.textContent = paused ? "Resume motion" : "Pause motion";
});
// ---------------------------------------------------------------------
// Resize and animation
// ---------------------------------------------------------------------
function resize() {
const width = innerWidth;
const height = innerHeight;
camera.aspect = width / height;
camera.updateProjectionMatrix();
renderer.setPixelRatio(Math.min(devicePixelRatio, 2));
renderer.setSize(width, height);
leaders.setAttribute("viewBox", `0 0 ${width} ${height}`);
}
addEventListener("resize", resize);
resize();
const clock = new THREE.Clock();
let elapsed = 0;
function animate() {
requestAnimationFrame(animate);
const delta = Math.min(clock.getDelta(), 0.05);
if (!paused) elapsed += delta;
const smoothing = 1 - Math.exp(-delta * 8);
orbit.yaw = THREE.MathUtils.lerp(
orbit.yaw,
orbit.desiredYaw,
smoothing
);
orbit.pitch = THREE.MathUtils.lerp(
orbit.pitch,
orbit.desiredPitch,
smoothing
);
orbit.distance = THREE.MathUtils.lerp(
orbit.distance,
orbit.desiredDistance,
smoothing
);
const horizontalDistance =
orbit.distance * Math.cos(orbit.pitch);
camera.position.set(
orbit.target.x + horizontalDistance * Math.sin(orbit.yaw),
orbit.target.y + orbit.distance * Math.sin(orbit.pitch),
orbit.target.z + horizontalDistance * Math.cos(orbit.yaw)
);
camera.lookAt(orbit.target);
// Subtle forward-and-back creep with a persistent +X polarity.
cellGroup.position.x = 0.72 * Math.sin(elapsed * 0.24);
cellGroup.position.z = 0.09 * Math.sin(elapsed * 0.17 + 0.7);
cellGroup.rotation.y = 0.028 * Math.sin(elapsed * 0.29);
updateCellGeometry(elapsed);
directionArrow.material.opacity =
0.29 + Math.sin(elapsed * 1.2) * 0.08;
renderer.render(scene, camera);
updateAnnotations();
}
document.getElementById("loading").remove();
animate();
</script>
</body>
</html>
```