Prompt specification
Build or customize the Three.js 3D scene "Heated fluid circulation cell" 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: Heated fluid circulation cell
Category: Science & Physics · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/heated-fluid-circulation-cell-x1457
Scene Description Prompt:
Animate qualitative convection in a low-poly transparent chamber, warming below and cooling above. Label the prescribed circulation loop. Three.js from cdnjs, one HTML file.
Working Three.js Starter (heated-fluid-circulation-cell-x1457):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Qualitative Convection</title>
<style>
:root {
color-scheme: dark;
--bg: #07111f;
--panel: rgba(10, 21, 37, 0.78);
--line: rgba(174, 222, 255, 0.22);
--text: #edf7ff;
--muted: #91a9bd;
--warm: #ff7048;
--cool: #4ebeff;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: var(--bg);
font-family:
Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
color: var(--text);
}
#scene {
position: fixed;
inset: 0;
width: 100%;
height: 100%;
display: block;
outline: none;
touch-action: none;
cursor: grab;
}
#scene.dragging {
cursor: grabbing;
}
#ui {
position: fixed;
inset: 0;
pointer-events: none;
}
header {
position: absolute;
top: clamp(20px, 4vw, 38px);
left: clamp(20px, 4vw, 42px);
max-width: 390px;
text-shadow: 0 2px 20px rgba(0, 0, 0, 0.55);
}
.eyebrow {
margin-bottom: 9px;
color: #89cfff;
font-size: 11px;
font-weight: 750;
letter-spacing: 0.2em;
text-transform: uppercase;
}
h1 {
margin: 0;
font-size: clamp(29px, 4vw, 52px);
font-weight: 680;
line-height: 0.98;
letter-spacing: -0.045em;
}
header p {
max-width: 340px;
margin: 14px 0 0;
color: var(--muted);
font-size: 13px;
line-height: 1.55;
}
.flow-card {
position: absolute;
top: clamp(20px, 4vw, 38px);
right: clamp(20px, 4vw, 42px);
width: min(330px, calc(100vw - 40px));
padding: 16px 18px;
border: 1px solid var(--line);
border-radius: 16px;
background: var(--panel);
box-shadow: 0 18px 50px rgba(0, 0, 0, 0.25);
backdrop-filter: blur(14px);
}
.live-row {
display: flex;
align-items: center;
gap: 8px;
margin-bottom: 10px;
color: #9fc4dc;
font-size: 10px;
font-weight: 750;
letter-spacing: 0.16em;
text-transform: uppercase;
}
#status-dot {
width: 7px;
height: 7px;
border-radius: 50%;
background: #55e6a7;
box-shadow: 0 0 14px #55e6a7;
transition: 180ms ease;
}
#status-dot.paused {
background: #8997a7;
box-shadow: none;
}
.flow-card strong {
display: block;
margin-bottom: 7px;
font-size: 17px;
font-weight: 650;
letter-spacing: -0.015em;
}
.flow-card p {
margin: 0;
color: var(--muted);
font-size: 12px;
line-height: 1.55;
}
.legend {
position: absolute;
left: clamp(20px, 4vw, 42px);
bottom: clamp(20px, 4vw, 38px);
display: grid;
gap: 9px;
padding: 14px 16px;
border: 1px solid var(--line);
border-radius: 14px;
background: rgba(8, 18, 32, 0.72);
backdrop-filter: blur(12px);
}
.legend-row {
display: flex;
align-items: center;
gap: 10px;
color: #b9ccdc;
font-size: 12px;
}
.swatch {
width: 10px;
height: 10px;
border-radius: 3px;
}
.swatch.warm {
background: var(--warm);
box-shadow: 0 0 12px rgba(255, 112, 72, 0.7);
}
.swatch.cool {
background: var(--cool);
box-shadow: 0 0 12px rgba(78, 190, 255, 0.7);
}
.controls {
position: absolute;
right: clamp(20px, 4vw, 42px);
bottom: clamp(20px, 4vw, 38px);
display: flex;
align-items: center;
gap: 8px;
pointer-events: auto;
}
button {
min-height: 38px;
padding: 0 13px;
border: 1px solid var(--line);
border-radius: 10px;
background: rgba(12, 26, 43, 0.88);
color: #dcefff;
font: inherit;
font-size: 12px;
cursor: pointer;
transition:
border-color 160ms ease,
background 160ms ease,
transform 160ms ease;
}
button:hover {
border-color: rgba(137, 207, 255, 0.55);
background: rgba(23, 47, 72, 0.95);
}
button:active {
transform: translateY(1px);
}
#pause {
min-width: 96px;
color: #fff;
}
.hint {
position: absolute;
right: clamp(20px, 4vw, 42px);
bottom: calc(clamp(20px, 4vw, 38px) + 49px);
color: rgba(170, 194, 215, 0.68);
font-size: 10px;
letter-spacing: 0.03em;
}
@media (max-width: 720px) {
header p {
display: none;
}
.flow-card {
top: auto;
right: 16px;
bottom: 74px;
width: 235px;
padding: 12px 14px;
}
.flow-card strong {
font-size: 14px;
}
.flow-card p {
font-size: 10px;
}
.legend {
left: 16px;
bottom: 16px;
padding: 11px 13px;
}
.controls {
right: 16px;
bottom: 16px;
}
.hint {
display: none;
}
button {
padding: 0 10px;
}
}
</style>
</head>
<body>
<canvas id="scene" tabindex="0" aria-label="Interactive convection chamber"></canvas>
<div id="ui">
<header>
<div class="eyebrow">Thermal transport / qualitative model</div>
<h1>Convection<br />in a chamber</h1>
<p>
Warm fluid rises from the heated floor, cools beneath the upper
boundary, descends along the cool side, and returns along the base.
</p>
</header>
<section class="flow-card" aria-label="Prescribed circulation loop">
<div class="live-row">
<i id="status-dot"></i>
<span id="status-text">Live circulation</span>
</div>
<div class="eyebrow">Prescribed circulation loop</div>
<strong>Clockwise convection cell</strong>
<p>
Warm rise → upper cooling → cool descent → heated return
</p>
</section>
<div class="legend" aria-label="Temperature legend">
<div class="legend-row">
<span class="swatch warm"></span>
<span>Warming below · buoyant rise</span>
</div>
<div class="legend-row">
<span class="swatch cool"></span>
<span>Cooling above · denser descent</span>
</div>
</div>
<div class="hint">Drag to orbit · Wheel to zoom · R to reset</div>
<div class="controls">
<button id="zoom-out" title="Zoom out" aria-label="Zoom out">−</button>
<button id="zoom-in" title="Zoom in" aria-label="Zoom in">+</button>
<button id="reset" title="Reset camera">Reset view</button>
<button id="pause" aria-pressed="false">Pause flow</button>
</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 canvas = document.querySelector("#scene");
const pauseButton = document.querySelector("#pause");
const statusDot = document.querySelector("#status-dot");
const statusText = document.querySelector("#status-text");
// ---------------------------------------------------------------------
// Renderer, scene, and camera
// ---------------------------------------------------------------------
const renderer = new THREE.WebGLRenderer({
canvas,
antialias: true,
alpha: false
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.08;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x07111f);
scene.fog = new THREE.Fog(0x07111f, 19, 38);
const camera = new THREE.PerspectiveCamera(
42,
1,
0.1,
100
);
const target = new THREE.Vector3(0, 4.65, 0);
const desiredCamera = {
theta: 0.38,
phi: 1.34,
radius: 16.5
};
const currentCamera = {
theta: desiredCamera.theta,
phi: desiredCamera.phi,
radius: desiredCamera.radius
};
function resetCamera() {
desiredCamera.theta = 0.38;
desiredCamera.phi = 1.34;
desiredCamera.radius = window.innerWidth < 720 ? 21 : 16.5;
}
function updateCamera(dt) {
const smoothing = 1 - Math.exp(-dt * 9);
currentCamera.theta = THREE.MathUtils.lerp(
currentCamera.theta,
desiredCamera.theta,
smoothing
);
currentCamera.phi = THREE.MathUtils.lerp(
currentCamera.phi,
desiredCamera.phi,
smoothing
);
currentCamera.radius = THREE.MathUtils.lerp(
currentCamera.radius,
desiredCamera.radius,
smoothing
);
const { theta, phi, radius } = currentCamera;
const sinPhi = Math.sin(phi);
camera.position.set(
target.x + radius * sinPhi * Math.sin(theta),
target.y + radius * Math.cos(phi),
target.z + radius * sinPhi * Math.cos(theta)
);
camera.lookAt(target);
}
// ---------------------------------------------------------------------
// Lighting and environment
// ---------------------------------------------------------------------
scene.add(new THREE.HemisphereLight(0xb9dcff, 0x3a160d, 1.45));
const keyLight = new THREE.DirectionalLight(0xeaf7ff, 2.3);
keyLight.position.set(6, 13, 9);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(1024, 1024);
keyLight.shadow.camera.left = -10;
keyLight.shadow.camera.right = 10;
keyLight.shadow.camera.top = 13;
keyLight.shadow.camera.bottom = -5;
scene.add(keyLight);
const warmLight = new THREE.PointLight(0xff5a2f, 42, 11, 1.7);
warmLight.position.set(0, 0.65, 0.5);
scene.add(warmLight);
const coolLight = new THREE.PointLight(0x45baff, 35, 11, 1.7);
coolLight.position.set(0, 9.0, 0.4);
scene.add(coolLight);
const floor = new THREE.Mesh(
new THREE.PlaneGeometry(42, 32),
new THREE.MeshStandardMaterial({
color: 0x08111d,
roughness: 0.88,
metalness: 0.12
})
);
floor.rotation.x = -Math.PI / 2;
floor.position.y = -0.48;
floor.receiveShadow = true;
scene.add(floor);
const grid = new THREE.GridHelper(
32,
32,
0x31516b,
0x183044
);
grid.position.y = -0.465;
grid.material.transparent = true;
grid.material.opacity = 0.23;
scene.add(grid);
// ---------------------------------------------------------------------
// Low-poly transparent chamber
// ---------------------------------------------------------------------
const chamber = new THREE.Group();
scene.add(chamber);
const chamberWidth = 7.2;
const chamberHeight = 8.8;
const chamberDepth = 4.4;
const chamberCenterY = 4.85;
const glassMaterial = new THREE.MeshPhysicalMaterial({
color: 0xb5e4ff,
transparent: true,
opacity: 0.09,
roughness: 0.2,
metalness: 0,
transmission: 0.18,
thickness: 0.15,
side: THREE.DoubleSide,
depthWrite: false
});
const glass = new THREE.Mesh(
new THREE.BoxGeometry(
chamberWidth,
chamberHeight,
chamberDepth
),
glassMaterial
);
glass.position.y = chamberCenterY;
glass.renderOrder = 1;
chamber.add(glass);
const chamberEdges = new THREE.LineSegments(
new THREE.EdgesGeometry(glass.geometry),
new THREE.LineBasicMaterial({
color: 0xbfe8ff,
transparent: true,
opacity: 0.58
})
);
chamberEdges.position.copy(glass.position);
chamber.add(chamberEdges);
const frameMaterial = new THREE.MeshStandardMaterial({
color: 0x7192aa,
transparent: true,
opacity: 0.4,
roughness: 0.36,
metalness: 0.45
});
const cornerOffsets = [
[-chamberWidth / 2, -chamberDepth / 2],
[-chamberWidth / 2, chamberDepth / 2],
[chamberWidth / 2, -chamberDepth / 2],
[chamberWidth / 2, chamberDepth / 2]
];
for (const [x, z] of cornerOffsets) {
const post = new THREE.Mesh(
new THREE.BoxGeometry(0.07, chamberHeight, 0.07),
frameMaterial
);
post.position.set(x, chamberCenterY, z);
chamber.add(post);
}
const base = new THREE.Mesh(
new THREE.BoxGeometry(7.8, 0.38, 5.0),
new THREE.MeshStandardMaterial({
color: 0x142235,
roughness: 0.34,
metalness: 0.58
})
);
base.position.y = 0.12;
base.castShadow = true;
base.receiveShadow = true;
chamber.add(base);
const heatedFloor = new THREE.Mesh(
new THREE.BoxGeometry(6.9, 0.09, 4.1),
new THREE.MeshStandardMaterial({
color: 0x6f291b,
emissive: 0xff4c24,
emissiveIntensity: 0.8,
roughness: 0.52,
metalness: 0.12
})
);
heatedFloor.position.y = 0.38;
chamber.add(heatedFloor);
const heaterMaterial = new THREE.MeshStandardMaterial({
color: 0xff7147,
emissive: 0xff3d17,
emissiveIntensity: 1.5,
roughness: 0.35,
metalness: 0.25
});
for (let i = 0; i < 9; i++) {
const heater = new THREE.Mesh(
new THREE.BoxGeometry(0.48, 0.09, 0.7),
heaterMaterial
);
heater.position.set(-2.75 + i * 0.69, 0.47, 1.55);
chamber.add(heater);
}
const coolingMaterial = new THREE.MeshStandardMaterial({
color: 0x60caff,
emissive: 0x238dff,
emissiveIntensity: 1.1,
roughness: 0.32,
metalness: 0.36
});
for (let i = 0; i < 9; i++) {
const fin = new THREE.Mesh(
new THREE.BoxGeometry(0.48, 0.12, 3.7),
coolingMaterial
);
fin.position.set(-2.75 + i * 0.69, 9.28, 0);
chamber.add(fin);
}
const topFrame = new THREE.Mesh(
new THREE.BoxGeometry(7.65, 0.16, 4.85),
new THREE.MeshStandardMaterial({
color: 0x17283b,
roughness: 0.35,
metalness: 0.55
})
);
topFrame.position.y = 9.43;
chamber.add(topFrame);
for (const x of [-2.6, 2.6]) {
const foot = new THREE.Mesh(
new THREE.CylinderGeometry(0.25, 0.34, 0.42, 6),
new THREE.MeshStandardMaterial({
color: 0x17283b,
roughness: 0.42,
metalness: 0.52
})
);
foot.position.set(x, -0.27, 0);
foot.castShadow = true;
chamber.add(foot);
}
// ---------------------------------------------------------------------
// Prescribed clockwise circulation path
//
// Hot fluid rises through the center, moves across the upper boundary,
// cools and descends at the right, then returns along the heated base.
// ---------------------------------------------------------------------
const loopPoints = [
new THREE.Vector3(0.00, 0.62, 0.00),
new THREE.Vector3(-0.16, 2.00, 0.06),
new THREE.Vector3(0.10, 4.25, -0.05),
new THREE.Vector3(0.02, 6.65, 0.05),
new THREE.Vector3(0.18, 8.22, 0.00),
new THREE.Vector3(1.15, 8.57, 0.04),
new THREE.Vector3(2.35, 8.23, 0.00),
new THREE.Vector3(3.03, 7.18, -0.05),
new THREE.Vector3(3.16, 5.05, 0.00),
new THREE.Vector3(3.00, 2.60, 0.05),
new THREE.Vector3(2.55, 0.67, 0.00),
new THREE.Vector3(1.65, 0.49, 0.00),
new THREE.Vector3(0.64, 0.49, 0.00)
];
const loopCurve = new THREE.CatmullRomCurve3(
loopPoints,
true,
"centripetal",
0.5
);
function setThermalColor(y, out) {
const t = THREE.MathUtils.smoothstep(y, 0.48, 9.0);
// Orange → magenta → blue keeps warm and cool visually distinct.
const hue = (0.055 - 0.47 * t + 1) % 1;
const saturation = 0.82 - 0.08 * t;
const lightness = 0.57 + 0.04 * Math.sin(t * Math.PI);
return out.setHSL(hue, saturation, lightness);
}
const tubeSegments = 180;
const radialSegments = 5;
const tubeGeometry = new THREE.TubeGeometry(
loopCurve,
tubeSegments,
0.047,
radialSegments,
true
);
const tubeColors = [];
const tubePosition = tubeGeometry.attributes.position;
const thermalColor = new THREE.Color();
for (let i = 0; i < tubePosition.count; i++) {
setThermalColor(tubePosition.getY(i), thermalColor);
tubeColors.push(
thermalColor.r,
thermalColor.g,
thermalColor.b
);
}
tubeGeometry.setAttribute(
"color",
new THREE.Float32BufferAttribute(tubeColors, 3)
);
const flowTube = new THREE.Mesh(
tubeGeometry,
new THREE.MeshBasicMaterial({
vertexColors: true,
transparent: true,
opacity: 0.38,
depthWrite: false
})
);
flowTube.renderOrder = 2;
scene.add(flowTube);
// Directional arrowheads mark the prescribed circulation.
const arrowGeometry = new THREE.ConeGeometry(0.13, 0.38, 3);
const up = new THREE.Vector3(0, 1, 0);
const arrowPoint = new THREE.Vector3();
const arrowTangent = new THREE.Vector3();
const flowArrows = [];
for (let i = 0; i < 16; i++) {
const t = i / 16;
loopCurve.getPointAt(t, arrowPoint);
loopCurve.getTangentAt(t, arrowTangent);
setThermalColor(arrowPoint.y, thermalColor);
const material = new THREE.MeshStandardMaterial({
color: thermalColor,
emissive: thermalColor,
emissiveIntensity: 0.38,
roughness: 0.42,
metalness: 0.08
});
const arrow = new THREE.Mesh(arrowGeometry, material);
arrow.position.copy(arrowPoint);
arrow.position.z += 0.08;
arrow.quaternion.setFromUnitVectors(up, arrowTangent);
arrow.userData.phase = t * Math.PI * 2;
arrow.userData.baseScale = 0.88 + (i % 3) * 0.06;
scene.add(arrow);
flowArrows.push(arrow);
}
// ---------------------------------------------------------------------
// Low-poly fluid parcels
// ---------------------------------------------------------------------
const parcelCount = 76;
const parcelGeometry = new THREE.ConeGeometry(0.115, 0.34, 3);
const parcelMaterial = new THREE.MeshStandardMaterial({
vertexColors: true,
roughness: 0.46,
metalness: 0.08
});
const parcels = new THREE.InstancedMesh(
parcelGeometry,
parcelMaterial,
parcelCount
);
parcels.instanceMatrix.setUsage(THREE.DynamicDrawUsage);
parcels.frustumCulled = false;
scene.add(parcels);
const parcelData = [];
for (let i = 0; i < parcelCount; i++) {
parcelData.push({
phase: Math.random(),
offsetX: (Math.random() - 0.5) * 0.48,
offsetY: (Math.random() - 0.5) * 0.18,
offsetZ: (Math.random() - 0.5) * 0.54,
scale: 0.64 + Math.random() * 0.58,
wobble: Math.random() * Math.PI * 2
});
}
const dummy = new THREE.Object3D();
const parcelPoint = new THREE.Vector3();
const parcelTangent = new THREE.Vector3();
const parcelColor = new THREE.Color();
function updateParcels(time) {
for (let i = 0; i < parcelCount; i++) {
const data = parcelData[i];
const progress = (data.phase + time * 0.034) % 1;
loopCurve.getPointAt(progress, parcelPoint);
loopCurve.getTangentAt(progress, parcelTangent);
const wobble = Math.sin(time * 1.7 + data.wobble);
dummy.position.set(
parcelPoint.x + data.offsetX,
parcelPoint.y + data.offsetY + wobble * 0.025,
parcelPoint.z +
data.offsetZ +
Math.cos(time * 1.25 + data.wobble) * 0.08
);
dummy.quaternion.setFromUnitVectors(up, parcelTangent);
dummy.scale.setScalar(data.scale);
dummy.updateMatrix();
parcels.setMatrixAt(i, dummy.matrix);
setThermalColor(
parcelPoint.y + data.offsetY,
parcelColor
);
parcels.setColorAt(i, parcelColor);
}
parcels.instanceMatrix.needsUpdate = true;
if (parcels.instanceColor) {
parcels.instanceColor.needsUpdate = true;
}
}
// ---------------------------------------------------------------------
// World-space label for the prescribed loop
// ---------------------------------------------------------------------
function makeLabelSprite() {
const labelCanvas = document.createElement("canvas");
labelCanvas.width = 1024;
labelCanvas.height = 280;
const ctx = labelCanvas.getContext("2d");
const width = labelCanvas.width;
const height = labelCanvas.height;
function roundedRect(x, y, w, h, radius) {
ctx.beginPath();
ctx.moveTo(x + radius, y);
ctx.lineTo(x + w - radius, y);
ctx.quadraticCurveTo(x + w, y, x + w, y + radius);
ctx.lineTo(x + w, y + h - radius);
ctx.quadraticCurveTo(
x + w,
y + h,
x + w - radius,
y + h
);
ctx.lineTo(x + radius, y + h);
ctx.quadraticCurveTo(x, y + h, x, y + h - radius);
ctx.lineTo(x, y + radius);
ctx.quadraticCurveTo(x, y, x + radius, y);
ctx.closePath();
}
roundedRect(8, 8, width - 16, height - 16, 42);
ctx.fillStyle = "rgba(7, 18, 32, 0.88)";
ctx.fill();
ctx.strokeStyle = "rgba(140, 211, 255, 0.62)";
ctx.lineWidth = 4;
ctx.stroke();
ctx.textAlign = "center";
ctx.fillStyle = "#8dd2ff";
ctx.font = "700 35px Arial";
ctx.fillText("PRESCRIBED LOOP", width / 2, 94);
ctx.fillStyle = "#f0f8ff";
ctx.font = "700 65px Arial";
ctx.fillText("CLOCKWISE", width / 2, 177);
ctx.fillStyle = "#9db7ca";
ctx.font = "28px Arial";
ctx.fillText("RISE → COOL → DESCEND", width / 2, 226);
const texture = new THREE.CanvasTexture(labelCanvas);
texture.colorSpace = THREE.SRGBColorSpace;
texture.minFilter = THREE.LinearFilter;
const sprite = new THREE.Sprite(
new THREE.SpriteMaterial({
map: texture,
transparent: true,
depthTest: false,
depthWrite: false,
toneMapped: false
})
);
sprite.position.set(4.75, 7.65, 0.1);
sprite.scale.set(3.45, 0.94, 1);
sprite.renderOrder = 10;
return sprite;
}
scene.add(makeLabelSprite());
const leaderGeometry = new THREE.BufferGeometry().setFromPoints([
new THREE.Vector3(2.75, 8.15, 0),
new THREE.Vector3(4.65, 7.95, 0)
]);
const leader = new THREE.Line(
leaderGeometry,
new THREE.LineBasicMaterial({
color: 0x8bd4ff,
transparent: true,
opacity: 0.6,
depthTest: false
})
);
leader.renderOrder = 9;
scene.add(leader);
// ---------------------------------------------------------------------
// Direct camera controls: orbit, zoom, keyboard, and reset
// ---------------------------------------------------------------------
let dragging = false;
let pointerX = 0;
let pointerY = 0;
canvas.addEventListener("pointerdown", (event) => {
dragging = true;
pointerX = event.clientX;
pointerY = event.clientY;
canvas.setPointerCapture(event.pointerId);
canvas.classList.add("dragging");
canvas.focus({ preventScroll: true });
});
canvas.addEventListener("pointermove", (event) => {
if (!dragging) return;
const dx = event.clientX - pointerX;
const dy = event.clientY - pointerY;
pointerX = event.clientX;
pointerY = event.clientY;
desiredCamera.theta -= dx * 0.006;
desiredCamera.phi = THREE.MathUtils.clamp(
desiredCamera.phi - dy * 0.005,
0.72,
1.62
);
});
function endDrag(event) {
dragging = false;
canvas.classList.remove("dragging");
if (
event.pointerId !== undefined &&
canvas.hasPointerCapture(event.pointerId)
) {
canvas.releasePointerCapture(event.pointerId);
}
}
canvas.addEventListener("pointerup", endDrag);
canvas.addEventListener("pointercancel", endDrag);
canvas.addEventListener(
"wheel",
(event) => {
event.preventDefault();
desiredCamera.radius = THREE.MathUtils.clamp(
desiredCamera.radius * Math.exp(event.deltaY * 0.001),
10,
25
);
},
{ passive: false }
);
canvas.addEventListener("dblclick", resetCamera);
canvas.addEventListener("keydown", (event) => {
if (event.code === "ArrowLeft") {
desiredCamera.theta -= 0.08;
} else if (event.code === "ArrowRight") {
desiredCamera.theta += 0.08;
} else if (event.code === "ArrowUp") {
desiredCamera.phi = THREE.MathUtils.clamp(
desiredCamera.phi - 0.06,
0.72,
1.62
);
} else if (event.code === "ArrowDown") {
desiredCamera.phi = THREE.MathUtils.clamp(
desiredCamera.phi + 0.06,
0.72,
1.62
);
} else if (event.code === "Equal" || event.code === "NumpadAdd") {
desiredCamera.radius = Math.max(10, desiredCamera.radius - 1);
} else if (
event.code === "Minus" ||
event.code === "NumpadSubtract"
) {
desiredCamera.radius = Math.min(25, desiredCamera.radius + 1);
} else if (event.code === "KeyR") {
resetCamera();
}
});
document.querySelector("#reset").addEventListener("click", resetCamera);
document.querySelector("#zoom-in").addEventListener("click", () => {
desiredCamera.radius = Math.max(10, desiredCamera.radius - 1.2);
});
document.querySelector("#zoom-out").addEventListener("click", () => {
desiredCamera.radius = Math.min(25, desiredCamera.radius + 1.2);
});
// ---------------------------------------------------------------------
// Playback controls
// ---------------------------------------------------------------------
let paused = false;
let flowTime = 0;
function setPaused(value) {
paused = value;
pauseButton.textContent = paused ? "Resume flow" : "Pause flow";
pauseButton.setAttribute("aria-pressed", String(paused));
statusText.textContent = paused ? "Circulation paused" : "Live circulation";
statusDot.classList.toggle("paused", paused);
}
pauseButton.addEventListener("click", () => {
setPaused(!paused);
});
// ---------------------------------------------------------------------
// Resize and render loop
// ---------------------------------------------------------------------
function resize() {
const width = window.innerWidth;
const height = window.innerHeight;
renderer.setSize(width, height, false);
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
camera.aspect = width / height;
camera.updateProjectionMatrix();
}
window.addEventListener("resize", resize);
resize();
resetCamera();
Object.assign(currentCamera, desiredCamera);
let previousTime = performance.now();
function animate(now) {
requestAnimationFrame(animate);
const dt = Math.min((now - previousTime) / 1000, 0.05);
previousTime = now;
if (!paused) {
flowTime += dt;
}
updateCamera(dt);
updateParcels(flowTime);
for (const arrow of flowArrows) {
const pulse =
0.5 +
0.5 *
Math.sin(
flowTime * 3.0 -
arrow.userData.phase
);
const scale =
arrow.userData.baseScale *
(0.92 + 0.16 * pulse);
arrow.scale.setScalar(scale);
}
renderer.render(scene, camera);
}
requestAnimationFrame(animate);
</script>
</body>
</html>
```