Prompt specification
Build or customize the Three.js 3D scene "Workshop lift force-distance exhibit" 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: Workshop lift force-distance exhibit
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/workshop-lift-force-distance-exhibit-x1483
Scene Description Prompt:
A voxel hydraulic lift: a narrow input piston travels farther to lift a heavier crate. One HTML file, Three.js from cdnjs.
Working Three.js Starter (workshop-lift-force-distance-exhibit-x1483):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Voxel Hydraulic Lift</title>
<style>
:root {
--ink: #132b35;
--muted: #60747a;
--paper: rgba(247, 250, 247, 0.88);
--line: rgba(19, 43, 53, 0.13);
--orange: #f4a524;
--cyan: #09aeb8;
--green: #37a76f;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #e8efea;
color: var(--ink);
font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
#scene {
position: fixed;
inset: 0;
display: block;
width: 100%;
height: 100%;
outline: none;
touch-action: none;
}
.ui {
position: fixed;
inset: 0;
pointer-events: none;
z-index: 2;
}
.masthead {
position: absolute;
top: 30px;
left: 32px;
width: min(360px, calc(100vw - 64px));
}
.eyebrow {
display: flex;
align-items: center;
gap: 9px;
margin-bottom: 14px;
color: #3d575e;
font-size: 11px;
font-weight: 800;
letter-spacing: 0.17em;
text-transform: uppercase;
}
.eyebrow::before {
width: 9px;
height: 9px;
border-radius: 2px;
background: var(--orange);
box-shadow: 0 0 0 4px rgba(244, 165, 36, 0.16);
content: "";
}
h1 {
margin: 0;
font-size: clamp(42px, 5.2vw, 74px);
font-weight: 850;
letter-spacing: -0.065em;
line-height: 0.86;
}
h1 em {
color: var(--cyan);
font-style: normal;
}
.masthead p {
max-width: 330px;
margin: 19px 0 0;
color: var(--muted);
font-size: 14px;
line-height: 1.65;
}
.force-card {
position: absolute;
left: 32px;
bottom: 32px;
width: 330px;
padding: 18px;
border: 1px solid var(--line);
border-radius: 18px;
background: var(--paper);
box-shadow: 0 16px 50px rgba(30, 59, 66, 0.11);
backdrop-filter: blur(16px);
}
.formula {
display: flex;
align-items: baseline;
gap: 8px;
font-size: 24px;
font-weight: 800;
letter-spacing: -0.04em;
}
.formula sub {
font-size: 11px;
letter-spacing: 0;
}
.formula .cyan {
color: var(--cyan);
}
.formula .orange {
color: #db8d0e;
}
.force-card > p {
margin: 8px 0 15px;
color: var(--muted);
font-size: 12px;
}
.stats {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 8px;
}
.stat {
min-width: 0;
padding: 10px 9px;
border-radius: 10px;
background: rgba(19, 43, 53, 0.055);
}
.stat span {
display: block;
margin-bottom: 5px;
color: var(--muted);
font-size: 8px;
font-weight: 800;
letter-spacing: 0.11em;
text-transform: uppercase;
}
.stat strong {
display: block;
overflow: hidden;
font-size: 13px;
font-variant-numeric: tabular-nums;
text-overflow: ellipsis;
white-space: nowrap;
}
.controls {
position: absolute;
top: 30px;
right: 30px;
display: flex;
gap: 8px;
pointer-events: auto;
}
button {
height: 42px;
padding: 0 15px;
border: 1px solid var(--line);
border-radius: 12px;
background: var(--paper);
color: var(--ink);
cursor: pointer;
font: inherit;
font-size: 12px;
font-weight: 750;
box-shadow: 0 8px 25px rgba(30, 59, 66, 0.08);
backdrop-filter: blur(14px);
transition: transform 150ms ease, background 150ms ease;
}
button:hover {
transform: translateY(-2px);
background: #fff;
}
button:active {
transform: translateY(0);
}
.timeline {
position: absolute;
right: 30px;
bottom: 32px;
width: min(500px, calc(100vw - 430px));
padding: 14px 16px 12px;
border: 1px solid var(--line);
border-radius: 16px;
background: var(--paper);
box-shadow: 0 16px 50px rgba(30, 59, 66, 0.11);
backdrop-filter: blur(16px);
pointer-events: auto;
}
.timeline-head {
display: flex;
align-items: center;
justify-content: space-between;
margin-bottom: 8px;
}
.status {
display: flex;
align-items: center;
gap: 8px;
font-size: 10px;
font-weight: 850;
letter-spacing: 0.13em;
text-transform: uppercase;
}
.status::before {
width: 8px;
height: 8px;
border-radius: 50%;
background: var(--green);
box-shadow: 0 0 0 4px rgba(55, 167, 111, 0.13);
content: "";
}
.time {
color: var(--muted);
font-size: 10px;
font-variant-numeric: tabular-nums;
}
input[type="range"] {
width: 100%;
height: 5px;
margin: 4px 0 8px;
border-radius: 10px;
appearance: none;
background: linear-gradient(
to right,
var(--cyan) 0%,
var(--cyan) var(--progress, 0%),
rgba(19, 43, 53, 0.12) var(--progress, 0%),
rgba(19, 43, 53, 0.12) 100%
);
cursor: pointer;
}
input[type="range"]::-webkit-slider-thumb {
width: 17px;
height: 17px;
border: 3px solid #fff;
border-radius: 50%;
appearance: none;
background: var(--cyan);
box-shadow: 0 2px 8px rgba(19, 43, 53, 0.25);
}
input[type="range"]::-moz-range-thumb {
width: 12px;
height: 12px;
border: 3px solid #fff;
border-radius: 50%;
background: var(--cyan);
box-shadow: 0 2px 8px rgba(19, 43, 53, 0.25);
}
.timeline-labels {
display: flex;
justify-content: space-between;
color: var(--muted);
font-size: 8px;
font-weight: 750;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.hint {
position: absolute;
right: 32px;
bottom: 136px;
color: rgba(19, 43, 53, 0.5);
font-size: 9px;
font-weight: 800;
letter-spacing: 0.13em;
text-transform: uppercase;
}
@media (max-width: 900px) {
.masthead {
top: 22px;
left: 22px;
}
.masthead p {
display: none;
}
.force-card {
left: 22px;
bottom: 22px;
width: 290px;
}
.timeline {
right: 22px;
bottom: 22px;
width: calc(100vw - 360px);
}
.hint {
display: none;
}
}
@media (max-width: 650px) {
.masthead {
top: 17px;
left: 18px;
}
h1 {
font-size: 39px;
}
.eyebrow {
margin-bottom: 10px;
font-size: 9px;
}
.controls {
top: 17px;
right: 17px;
flex-direction: column;
}
button {
height: 37px;
padding: 0 12px;
font-size: 11px;
}
.force-card {
display: none;
}
.timeline {
right: 16px;
bottom: 16px;
left: 16px;
width: auto;
}
}
</style>
</head>
<body>
<canvas id="scene" aria-label="Interactive voxel hydraulic lift demonstration"></canvas>
<div class="ui">
<section class="masthead">
<div class="eyebrow">Voxel Lab / Pascal’s Law</div>
<h1>Hydraulic<br /><em>lift.</em></h1>
<p>
A narrow piston needs less force. Pressure travels through the fluid,
letting a much larger piston raise the heavy crate.
</p>
</section>
<section class="force-card">
<div class="formula">
<span class="cyan">F<sub>out</sub></span>
<span>=</span>
<span>4 ×</span>
<span class="orange">F<sub>in</sub></span>
</div>
<p>Equal pressure acts on four times the piston area.</p>
<div class="stats">
<div class="stat">
<span>Input push</span>
<strong id="pushValue">0.00 m</strong>
</div>
<div class="stat">
<span>Crate lift</span>
<strong id="liftValue">0.00 m</strong>
</div>
<div class="stat">
<span>Output force</span>
<strong id="forceValue">0 N</strong>
</div>
</div>
</section>
<div class="controls">
<button id="pauseButton" type="button" aria-label="Pause animation">Pause</button>
<button id="resetButton" type="button">Reset</button>
</div>
<div class="timeline">
<div class="timeline-head">
<div class="status" id="status">Ready</div>
<div class="time" id="timeValue">0.0 / 10.0 s</div>
</div>
<input
id="scrubber"
type="range"
min="0"
max="1000"
value="0"
aria-label="Animation timeline"
/>
<div class="timeline-labels">
<span>Ready</span>
<span>Long input stroke</span>
<span>Short output lift</span>
</div>
</div>
<div class="hint">Drag to orbit · Scroll to zoom</div>
</div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// ------------------------------------------------------------
// Renderer and scene
// ------------------------------------------------------------
const canvas = document.querySelector("#scene");
const renderer = new THREE.WebGLRenderer({
canvas,
antialias: true,
alpha: false,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.05;
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xe8efea);
scene.fog = new THREE.Fog(0xe8efea, 17, 34);
const camera = new THREE.PerspectiveCamera(
37,
window.innerWidth / window.innerHeight,
0.1,
100
);
const hemisphere = new THREE.HemisphereLight(0xf8fff9, 0x63777a, 2.0);
scene.add(hemisphere);
const sun = new THREE.DirectionalLight(0xfff5dc, 3.2);
sun.position.set(-5, 11, 7);
sun.castShadow = true;
sun.shadow.mapSize.set(2048, 2048);
sun.shadow.camera.left = -10;
sun.shadow.camera.right = 10;
sun.shadow.camera.top = 10;
sun.shadow.camera.bottom = -10;
sun.shadow.camera.near = 1;
sun.shadow.camera.far = 28;
sun.shadow.bias = -0.00035;
scene.add(sun);
const rim = new THREE.DirectionalLight(0x9ee6ef, 1.0);
rim.position.set(7, 5, -8);
scene.add(rim);
// ------------------------------------------------------------
// Palette and reusable geometry
// ------------------------------------------------------------
const palette = {
ink: 0x17323d,
inkLight: 0x31515d,
orange: 0xf4a524,
orangeDark: 0xc97910,
cyan: 0x0aaeb8,
cyanDark: 0x087d89,
fluid: 0x19c4d1,
yellow: 0xf4c542,
wood: 0xa86732,
woodDark: 0x744321,
woodLight: 0xc27d40,
metal: 0x9aabb0,
cream: 0xf2eee2
};
const materials = {
ink: new THREE.MeshStandardMaterial({
color: palette.ink,
roughness: 0.72,
metalness: 0.15
}),
inkLight: new THREE.MeshStandardMaterial({
color: palette.inkLight,
roughness: 0.7,
metalness: 0.12
}),
orange: new THREE.MeshStandardMaterial({
color: palette.orange,
roughness: 0.55,
metalness: 0.08
}),
yellow: new THREE.MeshStandardMaterial({
color: palette.yellow,
roughness: 0.58,
metalness: 0.08
}),
cyan: new THREE.MeshStandardMaterial({
color: palette.cyan,
roughness: 0.42,
metalness: 0.12
}),
fluid: new THREE.MeshStandardMaterial({
color: palette.fluid,
emissive: 0x064a52,
emissiveIntensity: 0.25,
transparent: true,
opacity: 0.84,
roughness: 0.25,
metalness: 0.04
}),
glass: new THREE.MeshPhysicalMaterial({
color: 0xbfe6e6,
transparent: true,
opacity: 0.16,
roughness: 0.12,
metalness: 0,
depthWrite: false
}),
wood: new THREE.MeshStandardMaterial({
color: palette.wood,
roughness: 0.88,
metalness: 0
}),
woodDark: new THREE.MeshStandardMaterial({
color: palette.woodDark,
roughness: 0.92,
metalness: 0
}),
woodLight: new THREE.MeshStandardMaterial({
color: palette.woodLight,
roughness: 0.86,
metalness: 0
}),
metal: new THREE.MeshStandardMaterial({
color: palette.metal,
roughness: 0.4,
metalness: 0.72
})
};
function box(width, height, depth, material, x, y, z, parent = scene) {
const mesh = new THREE.Mesh(
new THREE.BoxGeometry(width, height, depth),
material
);
mesh.position.set(x, y, z);
mesh.castShadow = true;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
function makeLabel(text, accent = "#09aeb8", worldWidth = 2.5) {
const labelCanvas = document.createElement("canvas");
labelCanvas.width = 512;
labelCanvas.height = 128;
const ctx = labelCanvas.getContext("2d");
ctx.clearRect(0, 0, 512, 128);
const x = 12;
const y = 20;
const w = 488;
const h = 88;
const r = 18;
ctx.beginPath();
ctx.moveTo(x + r, y);
ctx.lineTo(x + w - r, y);
ctx.quadraticCurveTo(x + w, y, x + w, y + r);
ctx.lineTo(x + w, y + h - r);
ctx.quadraticCurveTo(x + w, y + h, x + w - r, y + h);
ctx.lineTo(x + r, y + h);
ctx.quadraticCurveTo(x, y + h, x, y + h - r);
ctx.lineTo(x, y + r);
ctx.quadraticCurveTo(x, y, x + r, y);
ctx.closePath();
ctx.fillStyle = "rgba(18, 43, 52, 0.93)";
ctx.fill();
ctx.strokeStyle = "rgba(255,255,255,0.13)";
ctx.lineWidth = 3;
ctx.stroke();
ctx.fillStyle = accent;
ctx.fillRect(x + 20, y + 24, 8, 40);
ctx.fillStyle = "#ffffff";
ctx.font = "800 34px system-ui, sans-serif";
ctx.textAlign = "left";
ctx.textBaseline = "middle";
ctx.fillText(text, x + 45, y + h / 2 + 1);
const texture = new THREE.CanvasTexture(labelCanvas);
texture.colorSpace = THREE.SRGBColorSpace;
texture.minFilter = THREE.LinearFilter;
const material = new THREE.SpriteMaterial({
map: texture,
transparent: true,
depthTest: false,
depthWrite: false
});
const sprite = new THREE.Sprite(material);
sprite.scale.set(worldWidth, worldWidth / 4, 1);
sprite.renderOrder = 20;
return sprite;
}
// ------------------------------------------------------------
// Ground and machine base
// ------------------------------------------------------------
const floorMaterial = new THREE.MeshStandardMaterial({
color: 0xdbe5de,
roughness: 0.95,
metalness: 0
});
box(30, 0.3, 24, floorMaterial, 0, -0.23, 0);
const grid = new THREE.GridHelper(26, 26, 0x9aada4, 0xb9c7c0);
grid.position.y = -0.065;
grid.material.transparent = true;
grid.material.opacity = 0.55;
scene.add(grid);
// Main skid
box(7.25, 0.36, 2.05, materials.ink, -0.78, 0.16, 0);
box(6.7, 0.12, 1.75, materials.inkLight, -0.78, 0.4, 0);
// Yellow bumper blocks
box(0.22, 0.48, 2.15, materials.yellow, -4.43, 0.14, 0);
box(0.22, 0.48, 2.15, materials.yellow, 2.87, 0.14, 0);
// Feet
for (const x of [-3.9, -1.4, 1.2, 2.35]) {
for (const z of [-0.76, 0.76]) {
box(0.42, 0.18, 0.42, materials.ink, x, -0.03, z);
}
}
// ------------------------------------------------------------
// Small input cylinder
// ------------------------------------------------------------
const smallX = -2.8;
const largeX = 1.25;
const smallFluid = box(
0.62,
1,
0.62,
materials.fluid,
smallX,
0,
0
);
smallFluid.castShadow = false;
const smallGlass = box(
0.86,
1.9,
0.86,
materials.glass,
smallX,
1.46,
0
);
smallGlass.castShadow = false;
smallGlass.renderOrder = 2;
// Corner rails
for (const dx of [-0.42, 0.42]) {
for (const dz of [-0.42, 0.42]) {
box(
0.08,
1.95,
0.08,
materials.ink,
smallX + dx,
1.45,
dz
);
}
}
// Bottom and top collars
box(0.94, 0.13, 0.94, materials.ink, smallX, 0.52, 0);
box(0.94, 0.11, 0.94, materials.ink, smallX, 2.39, 0);
// ------------------------------------------------------------
// Large output cylinder
// ------------------------------------------------------------
const largeFluid = box(
1.4,
1,
1.4,
materials.fluid,
largeX,
0,
0
);
largeFluid.castShadow = false;
const largeGlass = box(
1.64,
2.05,
1.64,
materials.glass,
largeX,
1.53,
0
);
largeGlass.castShadow = false;
largeGlass.renderOrder = 2;
for (const dx of [-0.8, 0.8]) {
for (const dz of [-0.8, 0.8]) {
box(
0.09,
2.1,
0.09,
materials.ink,
largeX + dx,
1.52,
dz
);
}
}
box(1.75, 0.14, 1.75, materials.ink, largeX, 0.52, 0);
box(1.75, 0.12, 1.75, materials.ink, largeX, 2.56, 0);
// Yellow safety strip on output housing
box(0.12, 1.92, 1.72, materials.yellow, largeX - 0.83, 1.52, 0);
box(0.12, 1.92, 1.72, materials.yellow, largeX + 0.83, 1.52, 0);
// ------------------------------------------------------------
// Connecting pipe
// ------------------------------------------------------------
const pipeMaterial = new THREE.MeshStandardMaterial({
color: palette.cyanDark,
emissive: 0x063d45,
emissiveIntensity: 0.18,
roughness: 0.38,
metalness: 0.25
});
const pipeLength = largeX - smallX;
const pipeCenter = (smallX + largeX) / 2;
box(pipeLength + 0.28, 0.17, 0.17, pipeMaterial, pipeCenter, 0.51, 0.78);
box(0.17, 0.38, 0.17, pipeMaterial, smallX, 0.63, 0.78);
box(0.17, 0.38, 0.17, pipeMaterial, largeX, 0.63, 0.78);
// Pipe collars
for (const x of [smallX, largeX]) {
box(0.28, 0.28, 0.28, materials.ink, x, 0.51, 0.78);
box(0.16, 0.16, 0.16, materials.cyan, x, 0.51, 0.94);
}
// Moving fluid markers
const flowMaterial = new THREE.MeshStandardMaterial({
color: 0x75edf0,
emissive: 0x0aaeb8,
emissiveIntensity: 0.8,
roughness: 0.2,
transparent: true,
opacity: 0.9
});
const flowMarkers = [];
for (let i = 0; i < 15; i++) {
const marker = new THREE.Mesh(
new THREE.BoxGeometry(0.08, 0.08, 0.08),
flowMaterial
);
scene.add(marker);
flowMarkers.push(marker);
}
// ------------------------------------------------------------
// Moving input piston
// ------------------------------------------------------------
const inputMoving = new THREE.Group();
scene.add(inputMoving);
box(0.7, 0.13, 0.7, materials.orange, smallX, 2.16, 0, inputMoving);
box(0.14, 1.72, 0.14, materials.metal, smallX, 3.04, 0, inputMoving);
box(0.98, 0.16, 0.78, materials.orange, smallX, 3.96, 0, inputMoving);
// Dark grip pads on the input plate
box(0.2, 0.07, 0.82, materials.ink, smallX - 0.35, 4.07, 0, inputMoving);
box(0.2, 0.07, 0.82, materials.ink, smallX + 0.35, 4.07, 0, inputMoving);
const inputArrow = new THREE.ArrowHelper(
new THREE.Vector3(0, -1, 0),
new THREE.Vector3(smallX - 0.78, 4.0, 0),
0.8,
palette.orange,
0.22,
0.16
);
inputMoving.add(inputArrow);
const inputLabel = makeLabel("NARROW INPUT", "#f4a524", 2.55);
inputLabel.position.set(smallX, 4.52, 0);
scene.add(inputLabel);
// ------------------------------------------------------------
// Moving output piston, platform and crate
// ------------------------------------------------------------
const outputMoving = new THREE.Group();
scene.add(outputMoving);
box(1.4, 0.15, 1.4, materials.cyan, largeX, 2.36, 0, outputMoving);
box(1.18, 0.72, 1.18, materials.metal, largeX, 2.77, 0, outputMoving);
box(2.3, 0.18, 2.0, materials.ink, largeX, 3.24, 0, outputMoving);
// Platform yellow edge
box(2.34, 0.08, 0.11, materials.yellow, largeX, 3.25, 1.01, outputMoving);
const outputArrow = new THREE.ArrowHelper(
new THREE.Vector3(0, 1, 0),
new THREE.Vector3(largeX + 1.62, 2.66, 0),
1.25,
palette.cyan,
0.26,
0.18
);
outputMoving.add(outputArrow);
// Voxel crate
const crate = new THREE.Group();
crate.position.set(largeX, 3.93, 0);
outputMoving.add(crate);
box(2.0, 1.2, 1.7, materials.wood, 0, 0, 0, crate);
// Front vertical boards and dark seams
for (const x of [-0.66, 0, 0.66]) {
box(0.58, 1.13, 0.06, materials.woodLight, x, 0, 0.87, crate);
box(0.025, 1.12, 0.075, materials.woodDark, x + 0.3, 0, 0.88, crate);
}
// Horizontal braces
box(2.12, 0.13, 0.08, materials.woodDark, 0, 0.43, 0.91, crate);
box(2.12, 0.13, 0.08, materials.woodDark, 0, -0.43, 0.91, crate);
// Top boards
for (const z of [-0.5, 0, 0.5]) {
box(2.08, 0.08, 0.42, materials.woodLight, 0, 0.63, z, crate);
}
// Metal corner guards
for (const x of [-0.94, 0.94]) {
for (const z of [-0.8, 0.8]) {
box(0.12, 1.23, 0.12, materials.metal, x, 0, z, crate);
}
}
const weightLabel = makeLabel("180 kg", "#f4c542", 1.22);
weightLabel.position.set(0, 0.08, 0.97);
crate.add(weightLabel);
const outputLabel = makeLabel("WIDE OUTPUT", "#09aeb8", 2.55);
outputLabel.position.set(largeX, 5.0, 0);
scene.add(outputLabel);
const pressureLabel = makeLabel("EQUAL PRESSURE", "#19c4d1", 2.25);
pressureLabel.position.set(-0.75, 1.04, 1.05);
scene.add(pressureLabel);
// Small floor annotation blocks
const markerMaterial = new THREE.MeshStandardMaterial({
color: 0xf3a72a,
roughness: 0.85
});
box(0.28, 0.08, 0.28, markerMaterial, -4.05, -0.02, 2.25);
box(0.18, 0.08, 0.18, markerMaterial, -3.55, -0.02, 2.55);
box(0.12, 0.08, 0.12, markerMaterial, -3.17, -0.02, 2.72);
// ------------------------------------------------------------
// Custom orbit camera controls
// ------------------------------------------------------------
const orbit = {
theta: 0.7,
phi: 1.08,
radius: window.innerWidth < 650 ? 16.8 : 13.3,
target: new THREE.Vector3(-0.45, 2.05, 0)
};
function updateCamera() {
const sinPhi = Math.sin(orbit.phi);
camera.position.set(
orbit.target.x + orbit.radius * sinPhi * Math.sin(orbit.theta),
orbit.target.y + orbit.radius * Math.cos(orbit.phi),
orbit.target.z + orbit.radius * sinPhi * Math.cos(orbit.theta)
);
camera.lookAt(orbit.target);
}
let dragging = false;
let lastPointerX = 0;
let lastPointerY = 0;
canvas.addEventListener("pointerdown", (event) => {
dragging = true;
lastPointerX = event.clientX;
lastPointerY = event.clientY;
canvas.setPointerCapture(event.pointerId);
});
canvas.addEventListener("pointermove", (event) => {
if (!dragging) return;
const dx = event.clientX - lastPointerX;
const dy = event.clientY - lastPointerY;
lastPointerX = event.clientX;
lastPointerY = event.clientY;
orbit.theta -= dx * 0.007;
orbit.phi = THREE.MathUtils.clamp(orbit.phi + dy * 0.006, 0.38, 1.43);
updateCamera();
});
function endDrag(event) {
dragging = false;
if (canvas.hasPointerCapture(event.pointerId)) {
canvas.releasePointerCapture(event.pointerId);
}
}
canvas.addEventListener("pointerup", endDrag);
canvas.addEventListener("pointercancel", endDrag);
canvas.addEventListener(
"wheel",
(event) => {
event.preventDefault();
orbit.radius = THREE.MathUtils.clamp(
orbit.radius * Math.exp(event.deltaY * 0.001),
9,
22
);
updateCamera();
},
{ passive: false }
);
canvas.addEventListener("dblclick", () => {
orbit.theta = 0.7;
orbit.phi = 1.08;
orbit.radius = window.innerWidth < 650 ? 16.8 : 13.3;
updateCamera();
});
updateCamera();
// ------------------------------------------------------------
// Animation and hydraulic relationship
// ------------------------------------------------------------
const CYCLE_SECONDS = 10;
const INPUT_STROKE = 0.95;
const OUTPUT_STROKE = INPUT_STROKE / 4;
const REQUIRED_INPUT_FORCE = 180 * 9.81 / 4;
let phase = 0;
let playing = true;
const pauseButton = document.querySelector("#pauseButton");
const resetButton = document.querySelector("#resetButton");
const scrubber = document.querySelector("#scrubber");
const pushValue = document.querySelector("#pushValue");
const liftValue = document.querySelector("#liftValue");
const forceValue = document.querySelector("#forceValue");
const status = document.querySelector("#status");
const timeValue = document.querySelector("#timeValue");
function smoothStep(value) {
const t = THREE.MathUtils.clamp(value, 0, 1);
return t * t * (3 - 2 * t);
}
function sampleCycle(time) {
if (time < 0.12) {
return { amount: 0, label: "Ready", flowing: 0 };
}
if (time < 0.55) {
return {
amount: smoothStep((time - 0.12) / 0.43),
label: "Pushing",
flowing: 1
};
}
if (time < 0.78) {
return { amount: 1, label: "Holding", flowing: 0 };
}
if (time < 0.96) {
return {
amount: 1 - smoothStep((time - 0.78) / 0.18),
label: "Returning",
flowing: -1
};
}
return { amount: 0, label: "Ready", flowing: 0 };
}
pauseButton.addEventListener("click", () => {
playing = !playing;
pauseButton.textContent = playing ? "Pause" : "Play";
pauseButton.setAttribute(
"aria-label",
playing ? "Pause animation" : "Play animation"
);
});
resetButton.addEventListener("click", () => {
phase = 0;
playing = true;
pauseButton.textContent = "Pause";
pauseButton.setAttribute("aria-label", "Pause animation");
});
scrubber.addEventListener("input", () => {
phase = Number(scrubber.value) / 1000;
playing = false;
pauseButton.textContent = "Play";
pauseButton.setAttribute("aria-label", "Play animation");
});
const clock = new THREE.Clock();
function animate() {
requestAnimationFrame(animate);
const dt = Math.min(clock.getDelta(), 0.05);
if (playing) {
phase = (phase + dt / CYCLE_SECONDS) % 1;
}
const sample = sampleCycle(phase);
const amount = sample.amount;
const inputTravel = INPUT_STROKE * amount;
const outputTravel = OUTPUT_STROKE * amount;
inputMoving.position.y = -inputTravel;
outputMoving.position.y = outputTravel;
// Visible fluid level changes.
const smallFluidHeight = 1.5 - inputTravel;
smallFluid.scale.y = smallFluidHeight;
smallFluid.position.y = 0.59 + smallFluidHeight / 2;
const largeFluidHeight = 1.7 + outputTravel;
largeFluid.scale.y = largeFluidHeight;
largeFluid.position.y = 0.59 + largeFluidHeight / 2;
// Moving markers visualize fluid transfer through the pipe.
flowMaterial.opacity = sample.flowing === 0 ? 0.32 : 0.95;
let flowProgress = 0;
if (phase >= 0.12 && phase < 0.55) {
flowProgress = (phase - 0.12) / 0.43;
} else if (phase >= 0.78 && phase < 0.96) {
flowProgress = (0.96 - phase) / 0.18;
}
flowMarkers.forEach((marker, index) => {
const t = (flowProgress * 1.8 + index / flowMarkers.length) % 1;
if (t < 0.84) {
marker.position.set(
THREE.MathUtils.lerp(smallX, largeX, t / 0.84),
0.51,
0.79
);
} else {
const q = (t - 0.84) / 0.16;
marker.position.set(largeX, 0.51 + q * 0.3, 0.79);
}
});
pushValue.textContent = `${inputTravel.toFixed(2)} m`;
liftValue.textContent = `${outputTravel.toFixed(3)} m`;
forceValue.textContent = `${Math.round(
REQUIRED_INPUT_FORCE * amount
)} N`;
status.textContent = sample.label;
timeValue.textContent = `${(phase * CYCLE_SECONDS).toFixed(
1
)} / ${CYCLE_SECONDS.toFixed(1)} s`;
scrubber.value = String(Math.round(phase * 1000));
scrubber.style.setProperty("--progress", `${phase * 100}%`);
renderer.render(scene, camera);
}
animate();
function resize() {
const width = window.innerWidth;
const height = window.innerHeight;
renderer.setSize(width, height, false);
camera.aspect = width / height;
camera.updateProjectionMatrix();
}
window.addEventListener("resize", resize);
resize();
</script>
</body>
</html>
```