Prompt specification
Build or customize the Three.js 3D scene "Traveling constriction transport demonstration" 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: Traveling constriction transport demonstration
Category: Biology & Nature · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/traveling-constriction-transport-demonstration-x1010
Scene Description Prompt:
A peristalsis cutaway: a prescribed contraction wave moves along a tube carrying a symbolic bolus. Three.js from cdnjs, one HTML file.
Working Three.js Starter (traveling-constriction-transport-demonstration-x1010):
```html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Peristalsis — Cutaway Study</title>
<style>
:root {
color-scheme: dark;
--bg: #071018;
--panel: rgba(11, 24, 34, 0.74);
--line: rgba(167, 214, 226, 0.16);
--text: #eef8fa;
--muted: #8da8b1;
--cyan: #64e0d4;
--pink: #ef8396;
--amber: #ffc45c;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background:
radial-gradient(circle at 50% 18%, #112b38 0%, transparent 42%),
radial-gradient(circle at 15% 90%, #24131d 0%, transparent 38%),
var(--bg);
color: var(--text);
font-family: Inter, ui-sans-serif, system-ui, -apple-system,
BlinkMacSystemFont, "Segoe UI", sans-serif;
}
button,
input {
font: inherit;
}
#viewport {
position: fixed;
inset: 0;
}
#viewport canvas {
display: block;
width: 100%;
height: 100%;
cursor: grab;
touch-action: none;
}
body.grabbing #viewport canvas {
cursor: grabbing;
}
.hud {
position: fixed;
inset: 0;
pointer-events: none;
}
.hero {
position: absolute;
top: 30px;
left: 34px;
max-width: 420px;
}
.eyebrow {
display: flex;
align-items: center;
gap: 10px;
margin-bottom: 10px;
color: var(--cyan);
font-size: 10px;
font-weight: 750;
letter-spacing: 0.22em;
text-transform: uppercase;
}
.eyebrow::before {
width: 25px;
height: 1px;
background: currentColor;
content: "";
}
h1 {
margin: 0;
font-size: clamp(38px, 5vw, 68px);
font-weight: 620;
letter-spacing: -0.055em;
line-height: 0.94;
}
.hero p {
margin: 15px 0 0;
color: var(--muted);
font-size: 13px;
letter-spacing: 0.02em;
line-height: 1.6;
}
.telemetry {
position: absolute;
top: 30px;
right: 30px;
width: 210px;
padding: 15px 17px;
border: 1px solid var(--line);
border-radius: 14px;
background: rgba(8, 20, 29, 0.58);
backdrop-filter: blur(14px);
box-shadow: 0 18px 60px rgba(0, 0, 0, 0.18);
}
.telemetry-head {
display: flex;
align-items: center;
justify-content: space-between;
padding-bottom: 11px;
border-bottom: 1px solid var(--line);
color: var(--muted);
font-size: 9px;
font-weight: 700;
letter-spacing: 0.16em;
text-transform: uppercase;
}
.status {
display: inline-flex;
align-items: center;
gap: 6px;
color: var(--cyan);
}
.status::before {
width: 6px;
height: 6px;
border-radius: 50%;
background: currentColor;
box-shadow: 0 0 12px currentColor;
content: "";
}
.status.paused {
color: var(--amber);
}
.metric {
display: grid;
grid-template-columns: 1fr auto;
gap: 10px;
align-items: baseline;
padding-top: 12px;
}
.metric span {
color: var(--muted);
font-size: 10px;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.metric strong {
font-size: 13px;
font-variant-numeric: tabular-nums;
font-weight: 650;
}
.controls {
position: absolute;
right: 30px;
bottom: 28px;
left: 30px;
display: grid;
grid-template-columns: auto minmax(150px, 1fr) minmax(150px, 1fr) auto auto;
gap: 22px;
align-items: center;
max-width: 1040px;
margin: 0 auto;
padding: 14px 16px;
border: 1px solid var(--line);
border-radius: 16px;
background: var(--panel);
backdrop-filter: blur(18px);
box-shadow: 0 22px 70px rgba(0, 0, 0, 0.28);
pointer-events: auto;
}
.control {
min-width: 0;
}
.control label {
display: flex;
justify-content: space-between;
margin-bottom: 9px;
color: var(--muted);
font-size: 9px;
font-weight: 700;
letter-spacing: 0.14em;
text-transform: uppercase;
}
.control output {
color: var(--text);
font-variant-numeric: tabular-nums;
}
input[type="range"] {
width: 100%;
height: 3px;
margin: 0;
border-radius: 4px;
outline: none;
appearance: none;
background: linear-gradient(
90deg,
var(--cyan) 0%,
var(--cyan) var(--fill, 50%),
rgba(160, 205, 215, 0.16) var(--fill, 50%),
rgba(160, 205, 215, 0.16) 100%
);
cursor: pointer;
}
input[type="range"]::-webkit-slider-thumb {
width: 13px;
height: 13px;
border: 3px solid #0b1b24;
border-radius: 50%;
appearance: none;
background: var(--cyan);
box-shadow: 0 0 0 1px rgba(100, 224, 212, 0.5);
}
input[type="range"]::-moz-range-thumb {
width: 9px;
height: 9px;
border: 3px solid #0b1b24;
border-radius: 50%;
background: var(--cyan);
box-shadow: 0 0 0 1px rgba(100, 224, 212, 0.5);
}
button {
height: 37px;
padding: 0 14px;
border: 1px solid var(--line);
border-radius: 9px;
outline: none;
color: var(--text);
background: rgba(255, 255, 255, 0.035);
font-size: 10px;
font-weight: 700;
letter-spacing: 0.08em;
text-transform: uppercase;
cursor: pointer;
transition:
border-color 160ms ease,
background 160ms ease,
transform 160ms ease;
}
button:hover {
border-color: rgba(100, 224, 212, 0.48);
background: rgba(100, 224, 212, 0.08);
}
button:active {
transform: translateY(1px);
}
button.primary {
display: inline-flex;
gap: 9px;
align-items: center;
justify-content: center;
min-width: 100px;
border-color: rgba(100, 224, 212, 0.35);
color: #071a1d;
background: var(--cyan);
}
button.primary:hover {
background: #83eadf;
}
#playGlyph {
font-size: 13px;
line-height: 1;
}
.timeline {
position: absolute;
right: 30px;
bottom: 139px;
left: 30px;
max-width: 1040px;
height: 2px;
margin: auto;
overflow: visible;
background: rgba(139, 180, 190, 0.12);
}
.timeline-fill {
position: absolute;
inset: 0 auto 0 0;
width: 0;
background: linear-gradient(90deg, var(--pink), var(--cyan));
}
.timeline-marker {
position: absolute;
top: 50%;
left: 0;
width: 8px;
height: 8px;
border: 2px solid #0a1820;
border-radius: 50%;
background: var(--cyan);
box-shadow: 0 0 14px rgba(100, 224, 212, 0.7);
transform: translate(-50%, -50%);
}
.tag {
position: absolute;
z-index: 3;
min-width: max-content;
padding: 7px 10px;
border: 1px solid rgba(140, 207, 215, 0.18);
border-radius: 7px;
color: #dcebed;
background: rgba(7, 18, 26, 0.76);
backdrop-filter: blur(10px);
font-size: 9px;
font-weight: 700;
letter-spacing: 0.13em;
text-transform: uppercase;
transform: translate(-50%, -100%);
transition: opacity 140ms ease;
}
.tag::after {
position: absolute;
top: 100%;
left: 50%;
width: 1px;
height: 24px;
background: linear-gradient(rgba(100, 224, 212, 0.65), transparent);
content: "";
}
#bolusTag {
border-color: rgba(255, 196, 92, 0.25);
color: #ffe1a8;
}
#bolusTag::after {
background: linear-gradient(rgba(255, 196, 92, 0.65), transparent);
}
.hint {
position: absolute;
bottom: 126px;
left: 34px;
color: rgba(157, 185, 193, 0.6);
font-size: 9px;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.axis-note {
position: absolute;
top: 47%;
right: 31px;
display: flex;
gap: 8px;
align-items: center;
color: rgba(100, 224, 212, 0.66);
font-size: 9px;
font-weight: 700;
letter-spacing: 0.16em;
text-transform: uppercase;
writing-mode: vertical-rl;
}
.axis-note::before {
width: 1px;
height: 45px;
background: currentColor;
content: "";
}
@media (max-width: 820px) {
.hero {
top: 22px;
left: 22px;
}
.hero p {
max-width: 280px;
}
.telemetry {
top: 22px;
right: 18px;
width: 168px;
}
.controls {
right: 16px;
bottom: 16px;
left: 16px;
grid-template-columns: 1fr 1fr;
gap: 15px;
}
.controls button.primary {
grid-column: 1 / -1;
}
.timeline {
right: 16px;
bottom: 215px;
left: 16px;
}
.hint {
display: none;
}
}
@media (max-width: 560px) {
.telemetry {
display: none;
}
.hero p {
font-size: 11px;
}
.controls {
grid-template-columns: 1fr;
}
.controls button.primary {
grid-column: auto;
}
.timeline {
bottom: 295px;
}
.tag {
font-size: 8px;
}
}
</style>
</head>
<body>
<div id="viewport"></div>
<div class="hud">
<header class="hero">
<div class="eyebrow">Biomechanics / 01</div>
<h1>Peristalsis</h1>
<p>
A prescribed traveling contraction wave carries a symbolic bolus
through a longitudinal cutaway of the lumen.
</p>
</header>
<aside class="telemetry" aria-label="Simulation telemetry">
<div class="telemetry-head">
<span>Model telemetry</span>
<span id="status" class="status">Live</span>
</div>
<div class="metric">
<span>Wave position</span>
<strong id="wavePosition">−6.0 u</strong>
</div>
<div class="metric">
<span>Lumen diameter</span>
<strong id="lumenDiameter">1.98 u</strong>
</div>
<div class="metric">
<span>Direction</span>
<strong>+X</strong>
</div>
<div class="metric">
<span>Transit</span>
<strong id="transit">0%</strong>
</div>
</aside>
<div id="waveTag" class="tag">Contraction wave</div>
<div id="bolusTag" class="tag">Symbolic bolus</div>
<div class="axis-note">Transport +X</div>
<div class="hint">Drag to orbit · Scroll to zoom · Space to pause</div>
<div class="timeline" aria-hidden="true">
<div id="timelineFill" class="timeline-fill"></div>
<div id="timelineMarker" class="timeline-marker"></div>
</div>
<section class="controls" aria-label="Simulation controls">
<button id="playButton" class="primary" type="button">
<span id="playGlyph">Ⅱ</span>
<span id="playText">Pause</span>
</button>
<div class="control">
<label for="speed">
Wave speed
<output id="speedValue">1.00×</output>
</label>
<input id="speed" type="range" min="0.25" max="1.75" step="0.05" value="1" />
</div>
<div class="control">
<label for="opening">
Cutaway opening
<output id="openingValue">150°</output>
</label>
<input id="opening" type="range" min="110" max="205" step="1" value="150" />
</div>
<button id="restartButton" type="button">Restart wave</button>
<button id="cameraButton" type="button">Reset view</button>
</section>
</div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
const viewport = document.querySelector("#viewport");
const scene = new THREE.Scene();
scene.fog = new THREE.FogExp2(0x071018, 0.028);
const camera = new THREE.PerspectiveCamera(
42,
innerWidth / innerHeight,
0.1,
100
);
const renderer = new THREE.WebGLRenderer({
antialias: true,
alpha: true,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(devicePixelRatio, 2));
renderer.setSize(innerWidth, innerHeight);
renderer.setClearColor(0x071018, 0);
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.08;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
viewport.appendChild(renderer.domElement);
// ─────────────────────────────────────────────────────────────────────
// Scene lighting and environment
// ─────────────────────────────────────────────────────────────────────
scene.add(new THREE.HemisphereLight(0xa8e5ff, 0x32151d, 2.1));
const keyLight = new THREE.DirectionalLight(0xffeee4, 3.2);
keyLight.position.set(4, 8, 7);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(1024, 1024);
keyLight.shadow.camera.left = -10;
keyLight.shadow.camera.right = 10;
keyLight.shadow.camera.top = 8;
keyLight.shadow.camera.bottom = -8;
keyLight.shadow.camera.near = 0.5;
keyLight.shadow.camera.far = 30;
scene.add(keyLight);
const rimLight = new THREE.DirectionalLight(0x54d9d1, 1.25);
rimLight.position.set(-7, 3, -6);
scene.add(rimLight);
const floor = new THREE.Mesh(
new THREE.PlaneGeometry(36, 22),
new THREE.MeshStandardMaterial({
color: 0x08151d,
roughness: 0.92,
metalness: 0.02,
transparent: true,
opacity: 0.84
})
);
floor.rotation.x = -Math.PI / 2;
floor.position.y = -1.36;
floor.receiveShadow = true;
scene.add(floor);
const grid = new THREE.GridHelper(30, 30, 0x31515d, 0x18313c);
grid.position.y = -1.345;
grid.material.transparent = true;
grid.material.opacity = 0.22;
scene.add(grid);
const dustGeometry = new THREE.BufferGeometry();
const dustPositions = new Float32Array(260 * 3);
let seed = 4137;
function random() {
seed = (seed * 16807) % 2147483647;
return (seed - 1) / 2147483646;
}
for (let i = 0; i < 260; i++) {
dustPositions[i * 3] = (random() - 0.5) * 24;
dustPositions[i * 3 + 1] = random() * 8 - 2;
dustPositions[i * 3 + 2] = (random() - 0.5) * 17;
}
dustGeometry.setAttribute(
"position",
new THREE.BufferAttribute(dustPositions, 3)
);
const dust = new THREE.Points(
dustGeometry,
new THREE.PointsMaterial({
color: 0x7bb8c2,
size: 0.025,
transparent: true,
opacity: 0.3,
depthWrite: false
})
);
scene.add(dust);
// ─────────────────────────────────────────────────────────────────────
// Dynamic longitudinal cutaway tube
// ─────────────────────────────────────────────────────────────────────
const LENGTH = 12;
const BASE_RADIUS = 0.96;
const WALL_THICKNESS = 0.12;
const WAVELENGTH = 4;
const BASE_SPEED = 1.15;
const NX = 112;
const NA = 44;
const innerRadii = new Float32Array(NX + 1);
const outerRadii = new Float32Array(NX + 1);
let openingDegrees = 150;
let startTheta = 0;
let endTheta = 0;
const openCenter = THREE.MathUtils.degToRad(45);
function updateAngles() {
const opening = THREE.MathUtils.degToRad(openingDegrees);
startTheta = openCenter + opening / 2;
endTheta = startTheta + (Math.PI * 2 - opening);
}
updateAngles();
const outerMaterial = new THREE.MeshStandardMaterial({
color: 0xe36e84,
roughness: 0.42,
metalness: 0.02,
side: THREE.FrontSide
});
const innerMaterial = new THREE.MeshStandardMaterial({
color: 0xffd7d1,
roughness: 0.64,
metalness: 0,
emissive: 0x4a111a,
emissiveIntensity: 0.07,
side: THREE.FrontSide
});
const cutMaterial = new THREE.MeshStandardMaterial({
color: 0xff9cac,
roughness: 0.4,
metalness: 0.01,
emissive: 0x5b1020,
emissiveIntensity: 0.12,
side: THREE.DoubleSide
});
function createLayer(isInner) {
const vertexCount = (NX + 1) * (NA + 1);
const geometry = new THREE.BufferGeometry();
geometry.setAttribute(
"position",
new THREE.BufferAttribute(new Float32Array(vertexCount * 3), 3)
.setUsage(THREE.DynamicDrawUsage)
);
const indices = [];
for (let i = 0; i < NX; i++) {
for (let j = 0; j < NA; j++) {
const a = i * (NA + 1) + j;
const b = (i + 1) * (NA + 1) + j;
const c = b + 1;
const d = a + 1;
if (isInner) {
indices.push(a, b, d, b, c, d);
} else {
indices.push(a, d, b, b, d, c);
}
}
}
geometry.setIndex(indices);
const mesh = new THREE.Mesh(
geometry,
isInner ? innerMaterial : outerMaterial
);
mesh.castShadow = true;
mesh.receiveShadow = true;
mesh.frustumCulled = false;
scene.add(mesh);
return mesh;
}
const outerLayer = createLayer(false);
const innerLayer = createLayer(true);
function createEdgeGeometry() {
const geometry = new THREE.BufferGeometry();
const count = 2 * (NX + 1) * 2;
geometry.setAttribute(
"position",
new THREE.BufferAttribute(new Float32Array(count * 3), 3)
.setUsage(THREE.DynamicDrawUsage)
);
const indices = [];
const sideSize = (NX + 1) * 2;
for (let side = 0; side < 2; side++) {
const offset = side * sideSize;
for (let i = 0; i < NX; i++) {
const a = offset + i * 2;
const b = a + 1;
const c = a + 2;
const d = a + 3;
indices.push(a, b, c, b, d, c);
}
}
geometry.setIndex(indices);
const mesh = new THREE.Mesh(geometry, cutMaterial);
mesh.castShadow = true;
mesh.frustumCulled = false;
scene.add(mesh);
return mesh;
}
function createEndGeometry() {
const geometry = new THREE.BufferGeometry();
const count = 2 * (NA + 1) * 2;
geometry.setAttribute(
"position",
new THREE.BufferAttribute(new Float32Array(count * 3), 3)
.setUsage(THREE.DynamicDrawUsage)
);
const indices = [];
for (let end = 0; end < 2; end++) {
const offset = end * (NA + 1) * 2;
for (let j = 0; j < NA; j++) {
const a = offset + j * 2;
const b = a + 1;
const c = a + 2;
const d = a + 3;
indices.push(a, b, c, b, d, c);
}
}
geometry.setIndex(indices);
const mesh = new THREE.Mesh(geometry, cutMaterial);
mesh.castShadow = true;
mesh.frustumCulled = false;
scene.add(mesh);
return mesh;
}
const edgeMesh = createEdgeGeometry();
const endMesh = createEndGeometry();
function wrapOffset(value, center, period) {
let s = (value - center + period / 2) % period;
if (s < 0) s += period;
return s - period / 2;
}
function tubeMetrics(x, center) {
const s = wrapOffset(x, center, WAVELENGTH);
const contraction = Math.exp(
-Math.pow(Math.abs(s) / 0.46, 4)
);
const anteriorBulge =
s > 0
? Math.exp(-Math.pow((s - 0.5) / 0.55, 2))
: 0;
const inner =
BASE_RADIUS *
(1 - 0.22 * contraction + 0.035 * anteriorBulge);
return {
inner,
outer: inner + WALL_THICKNESS,
contraction,
offset: s
};
}
function updateLayer(mesh, isInner) {
const attribute = mesh.geometry.attributes.position;
for (let i = 0; i <= NX; i++) {
const x = -LENGTH / 2 + (LENGTH * i) / NX;
for (let j = 0; j <= NA; j++) {
const theta =
startTheta + ((endTheta - startTheta) * j) / NA;
const radius = isInner ? innerRadii[i] : outerRadii[i];
const index = i * (NA + 1) + j;
attribute.setXYZ(
index,
x,
Math.cos(theta) * radius,
Math.sin(theta) * radius
);
}
}
attribute.needsUpdate = true;
mesh.geometry.computeVertexNormals();
}
function updateEdgeGeometry() {
const attribute = edgeMesh.geometry.attributes.position;
const sideSize = (NX + 1) * 2;
for (let side = 0; side < 2; side++) {
const theta = side === 0 ? startTheta : endTheta;
for (let i = 0; i <= NX; i++) {
const x = -LENGTH / 2 + (LENGTH * i) / NX;
const base = side * sideSize + i * 2;
attribute.setXYZ(
base,
x,
Math.cos(theta) * innerRadii[i],
Math.sin(theta) * innerRadii[i]
);
attribute.setXYZ(
base + 1,
x,
Math.cos(theta) * outerRadii[i],
Math.sin(theta) * outerRadii[i]
);
}
}
attribute.needsUpdate = true;
edgeMesh.geometry.computeVertexNormals();
}
function updateEndGeometry() {
const attribute = endMesh.geometry.attributes.position;
for (let end = 0; end < 2; end++) {
const x = end === 0 ? -LENGTH / 2 : LENGTH / 2;
const radiusIndex = end === 0 ? 0 : NX;
for (let j = 0; j <= NA; j++) {
const theta =
startTheta + ((endTheta - startTheta) * j) / NA;
const base = (end * (NA + 1) + j) * 2;
attribute.setXYZ(
base,
x,
Math.cos(theta) * innerRadii[radiusIndex],
Math.sin(theta) * innerRadii[radiusIndex]
);
attribute.setXYZ(
base + 1,
x,
Math.cos(theta) * outerRadii[radiusIndex],
Math.sin(theta) * outerRadii[radiusIndex]
);
}
}
attribute.needsUpdate = true;
endMesh.geometry.computeVertexNormals();
}
// ─────────────────────────────────────────────────────────────────────
// Traveling contraction scan rings
// ─────────────────────────────────────────────────────────────────────
const ringMaterial = new THREE.MeshStandardMaterial({
color: 0x8af4e7,
emissive: 0x38d7cc,
emissiveIntensity: 1.8,
roughness: 0.3,
metalness: 0.08,
transparent: true,
opacity: 0.65
});
const glowMaterial = new THREE.MeshBasicMaterial({
color: 0x64e0d4,
transparent: true,
opacity: 0.08,
depthWrite: false
});
const waveRings = [];
for (let i = 0; i < 5; i++) {
const group = new THREE.Group();
const ring = new THREE.Mesh(
new THREE.TorusGeometry(1, 0.018, 8, 72),
ringMaterial
);
ring.rotation.y = Math.PI / 2;
group.add(ring);
const glow = new THREE.Mesh(
new THREE.TorusGeometry(1, 0.065, 8, 72),
glowMaterial
);
glow.rotation.y = Math.PI / 2;
group.add(glow);
group.visible = false;
scene.add(group);
waveRings.push(group);
}
// ─────────────────────────────────────────────────────────────────────
// Symbolic bolus
// ─────────────────────────────────────────────────────────────────────
const bolusGroup = new THREE.Group();
const bolusMaterial = new THREE.MeshStandardMaterial({
color: 0xffbd4c,
emissive: 0x8f4300,
emissiveIntensity: 0.52,
roughness: 0.26,
metalness: 0.14,
flatShading: true,
transparent: true
});
const bolusCore = new THREE.Mesh(
new THREE.IcosahedronGeometry(0.43, 2),
bolusMaterial
);
bolusCore.castShadow = true;
bolusGroup.add(bolusCore);
const bolusWire = new THREE.Mesh(
new THREE.IcosahedronGeometry(0.455, 1),
new THREE.MeshBasicMaterial({
color: 0xffe9ad,
wireframe: true,
transparent: true,
opacity: 0.34
})
);
bolusGroup.add(bolusWire);
const symbolCanvas = document.createElement("canvas");
symbolCanvas.width = 128;
symbolCanvas.height = 128;
const context = symbolCanvas.getContext("2d");
context.strokeStyle = "#ffffff";
context.fillStyle = "#ffffff";
context.lineWidth = 12;
context.lineCap = "round";
context.lineJoin = "round";
context.beginPath();
context.moveTo(-37, 0);
context.lineTo(31, 0);
context.stroke();
context.beginPath();
context.moveTo(14, -22);
context.lineTo(39, 0);
context.lineTo(14, 22);
context.closePath();
context.fill();
const symbolTexture = new THREE.CanvasTexture(symbolCanvas);
symbolTexture.colorSpace = THREE.SRGBColorSpace;
const symbol = new THREE.Sprite(
new THREE.SpriteMaterial({
map: symbolTexture,
transparent: true,
depthTest: false,
depthWrite: false
})
);
symbol.scale.set(0.5, 0.5, 0.5);
symbol.renderOrder = 10;
bolusGroup.add(symbol);
const bolusLight = new THREE.PointLight(0xffaa35, 2.2, 3.2, 2);
scene.add(bolusLight);
scene.add(bolusGroup);
// ─────────────────────────────────────────────────────────────────────
// Direction arrows
// ─────────────────────────────────────────────────────────────────────
const arrowMaterial = new THREE.MeshStandardMaterial({
color: 0x64e0d4,
emissive: 0x1e8f87,
emissiveIntensity: 0.7,
transparent: true,
opacity: 0.45,
depthWrite: false
});
const flowArrows = [];
for (let i = 0; i < 7; i++) {
const arrow = new THREE.Group();
const shaft = new THREE.Mesh(
new THREE.BoxGeometry(0.42, 0.022, 0.022),
arrowMaterial
);
arrow.add(shaft);
const head = new THREE.Mesh(
new THREE.ConeGeometry(0.085, 0.18, 12),
arrowMaterial
);
head.rotation.z = -Math.PI / 2;
head.position.x = 0.3;
arrow.add(head);
arrow.position.set(-5.1 + i * 1.7, 1.58, 0);
scene.add(arrow);
flowArrows.push(arrow);
}
// ─────────────────────────────────────────────────────────────────────
// Custom camera controls: orbit, pan-free, zoom
// ─────────────────────────────────────────────────────────────────────
const cameraTarget = new THREE.Vector3(0, -0.05, 0);
const cameraState = {
theta: 0.12,
phi: 1.19,
radius: 13.4
};
function updateCamera() {
const sinPhi = Math.sin(cameraState.phi);
camera.position.set(
cameraTarget.x +
cameraState.radius * sinPhi * Math.sin(cameraState.theta),
cameraTarget.y +
cameraState.radius * Math.cos(cameraState.phi),
cameraTarget.z +
cameraState.radius * sinPhi * Math.cos(cameraState.theta)
);
camera.lookAt(cameraTarget);
}
function resetCamera() {
cameraState.theta = 0.12;
cameraState.phi = 1.19;
cameraState.radius = 13.4;
updateCamera();
}
updateCamera();
let dragging = false;
let pointerX = 0;
let pointerY = 0;
renderer.domElement.addEventListener("pointerdown", (event) => {
dragging = true;
pointerX = event.clientX;
pointerY = event.clientY;
renderer.domElement.setPointerCapture(event.pointerId);
document.body.classList.add("grabbing");
});
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.theta -= dx * 0.006;
cameraState.phi = THREE.MathUtils.clamp(
cameraState.phi - dy * 0.005,
0.48,
2.35
);
updateCamera();
});
function endDrag(event) {
dragging = false;
document.body.classList.remove("grabbing");
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.radius = THREE.MathUtils.clamp(
cameraState.radius * Math.exp(event.deltaY * 0.001),
8,
20
);
updateCamera();
},
{ passive: false }
);
renderer.domElement.addEventListener("dblclick", resetCamera);
// ─────────────────────────────────────────────────────────────────────
// UI and simulation state
// ─────────────────────────────────────────────────────────────────────
const playButton = document.querySelector("#playButton");
const playGlyph = document.querySelector("#playGlyph");
const playText = document.querySelector("#playText");
const status = document.querySelector("#status");
const speedInput = document.querySelector("#speed");
const speedValue = document.querySelector("#speedValue");
const openingInput = document.querySelector("#opening");
const openingValue = document.querySelector("#openingValue");
const restartButton = document.querySelector("#restartButton");
const cameraButton = document.querySelector("#cameraButton");
const wavePositionElement = document.querySelector("#wavePosition");
const lumenDiameterElement = document.querySelector("#lumenDiameter");
const transitElement = document.querySelector("#transit");
const waveTag = document.querySelector("#waveTag");
const bolusTag = document.querySelector("#bolusTag");
const timelineFill = document.querySelector("#timelineFill");
const timelineMarker = document.querySelector("#timelineMarker");
let playing = true;
let speed = Number(speedInput.value);
let travel = -LENGTH / 2;
let elapsed = 0;
function updateRangeFill(input) {
const percentage =
((Number(input.value) - Number(input.min)) /
(Number(input.max) - Number(input.min))) *
100;
input.style.setProperty("--fill", `${percentage}%`);
}
updateRangeFill(speedInput);
updateRangeFill(openingInput);
function setPlaying(value) {
playing = value;
playGlyph.textContent = playing ? "Ⅱ" : "▶";
playText.textContent = playing ? "Pause" : "Play";
status.textContent = playing ? "Live" : "Paused";
status.classList.toggle("paused", !playing);
}
playButton.addEventListener("click", () => setPlaying(!playing));
speedInput.addEventListener("input", () => {
speed = Number(speedInput.value);
speedValue.textContent = `${speed.toFixed(2)}×`;
updateRangeFill(speedInput);
});
openingInput.addEventListener("input", () => {
openingDegrees = Number(openingInput.value);
openingValue.textContent = `${openingDegrees.toFixed(0)}°`;
updateAngles();
updateRangeFill(openingInput);
});
restartButton.addEventListener("click", () => {
travel = -LENGTH / 2;
elapsed = 0;
});
cameraButton.addEventListener("click", resetCamera);
addEventListener("keydown", (event) => {
const tag = document.activeElement?.tagName;
if (
event.code === "Space" &&
tag !== "INPUT" &&
tag !== "BUTTON"
) {
event.preventDefault();
setPlaying(!playing);
}
});
function smoothstep(min, max, value) {
const x = THREE.MathUtils.clamp(
(value - min) / (max - min),
0,
1
);
return x * x * (3 - 2 * x);
}
function wrapBolusPosition(value) {
return ((((value + LENGTH / 2) % LENGTH) + LENGTH) % LENGTH) -
LENGTH / 2;
}
function placeTag(element, worldPosition, visible = true) {
const projected = worldPosition.clone().project(camera);
const onScreen =
projected.z > -1 &&
projected.z < 1 &&
Math.abs(projected.x) < 1.2 &&
Math.abs(projected.y) < 1.2;
const x = (projected.x * 0.5 + 0.5) * innerWidth;
const y = (-projected.y * 0.5 + 0.5) * innerHeight;
element.style.left = `${x}px`;
element.style.top = `${y}px`;
element.style.opacity =
visible && onScreen ? "1" : "0";
}
// ─────────────────────────────────────────────────────────────────────
// Animation
// ─────────────────────────────────────────────────────────────────────
const clock = new THREE.Clock();
const waveAnchor = new THREE.Vector3();
const bolusAnchor = new THREE.Vector3();
renderer.setAnimationLoop(() => {
const dt = Math.min(clock.getDelta(), 0.05);
if (playing) {
elapsed += dt;
travel += dt * BASE_SPEED * speed;
if (travel > LENGTH / 2) travel -= LENGTH;
if (travel < -LENGTH / 2) travel += LENGTH;
}
// The wave pattern and the tube shell deform together.
for (let i = 0; i <= NX; i++) {
const x = -LENGTH / 2 + (LENGTH * i) / NX;
const metrics = tubeMetrics(x, travel);
innerRadii[i] = metrics.inner;
outerRadii[i] = metrics.outer;
}
updateLayer(outerLayer, false);
updateLayer(innerLayer, true);
updateEdgeGeometry();
updateEndGeometry();
// Position visible contraction scan rings.
let ringIndex = 0;
for (let k = -2; k <= 2; k++) {
const x = travel + k * WAVELENGTH;
const ring = waveRings[ringIndex++];
if (!ring) break;
const visible = x > -LENGTH / 2 - 0.05 &&
x < LENGTH / 2 + 0.05;
ring.visible = visible;
if (visible) {
const metrics = tubeMetrics(x, travel);
ring.position.set(x, 0, 0);
ring.scale.setScalar(metrics.outer * 1.015);
}
}
// The bolus stays ahead of the leading contraction.
const bolusX = wrapBolusPosition(travel + 0.78);
const edgeDistance = LENGTH / 2 - Math.abs(bolusX);
const bolusFade = smoothstep(0, 0.75, edgeDistance);
const bolusMetrics = tubeMetrics(bolusX, travel);
bolusGroup.position.set(bolusX, 0.02, 0.02);
bolusGroup.visible = bolusFade > 0.01;
const bolusPulse = 1 + Math.sin(elapsed * 4.2) * 0.018;
const lumenScale = bolusMetrics.inner / BASE_RADIUS;
bolusCore.scale.set(
lumenScale * bolusPulse,
bolusPulse / Math.sqrt(lumenScale),
bolusPulse / Math.sqrt(lumenScale)
);
bolusCore.rotation.x += dt * 0.42;
bolusCore.rotation.y += dt * 0.58;
bolusMaterial.opacity = bolusFade;
bolusWire.material.opacity = bolusFade * 0.34;
bolusLight.position.copy(bolusGroup.position);
bolusLight.intensity = bolusFade * 2.2;
// Subtle motion in the transport arrows.
flowArrows.forEach((arrow, index) => {
arrow.position.x =
-5.1 +
index * 1.7 +
Math.sin(elapsed * 1.2 + index * 0.75) * 0.055;
});
dust.rotation.y += dt * 0.008;
// HUD telemetry.
const normalizedTravel = (travel + LENGTH / 2) / LENGTH;
const progress = normalizedTravel * 100;
wavePositionElement.textContent =
`${travel >= 0 ? "+" : "−"}${Math.abs(travel).toFixed(1)} u`;
lumenDiameterElement.textContent =
`${(bolusMetrics.inner * 2).toFixed(2)} u`;
transitElement.textContent = `${Math.round(progress)}%`;
timelineFill.style.width = `${progress}%`;
timelineMarker.style.left = `${progress}%`;
waveAnchor.set(
THREE.MathUtils.clamp(travel, -5.65, 5.65),
1.34,
0.1
);
bolusAnchor.copy(bolusGroup.position);
bolusAnchor.y += 0.8;
placeTag(waveTag, waveAnchor);
placeTag(bolusTag, bolusAnchor, bolusFade > 0.08);
renderer.render(scene, camera);
});
addEventListener("resize", () => {
camera.aspect = innerWidth / innerHeight;
camera.updateProjectionMatrix();
renderer.setPixelRatio(Math.min(devicePixelRatio, 2));
renderer.setSize(innerWidth, innerHeight);
});
</script>
</body>
</html>
```