Prompt specification
Build or customize the Three.js 3D scene "Pulley tradeoff exploration rig" 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: Pulley tradeoff exploration rig
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/pulley-tradeoff-exploration-rig-x1424
Scene Description Prompt:
Compare one and two supporting rope segments on voxel pulleys; calculate force and pull distance, neglecting friction and pulley mass. Three.js from cdnjs, standalone HTML.
Working Three.js Starter (pulley-tradeoff-exploration-rig-x1424):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Voxel Pulley Comparison</title>
<style>
:root {
color-scheme: dark;
--bg: #09111f;
--panel: rgba(15, 25, 42, 0.9);
--border: rgba(148, 163, 184, 0.25);
--text: #e5edf8;
--muted: #9eb0c8;
--cyan: #52d6ff;
--gold: #ffc857;
--green: #66e3a4;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: var(--bg);
color: var(--text);
font-family:
Inter, ui-sans-serif, system-ui, -apple-system,
BlinkMacSystemFont, "Segoe UI", sans-serif;
}
canvas {
display: block;
width: 100%;
height: 100%;
touch-action: none;
}
header {
position: absolute;
top: 18px;
left: 50%;
transform: translateX(-50%);
width: min(760px, calc(100% - 32px));
text-align: center;
pointer-events: none;
text-shadow: 0 2px 18px #000;
}
header h1 {
margin: 0;
font-size: clamp(22px, 3vw, 36px);
letter-spacing: -0.03em;
}
header p {
margin: 5px 0 0;
color: var(--muted);
font-size: 14px;
}
.readout {
position: absolute;
top: 104px;
width: min(285px, calc(50% - 34px));
padding: 15px 17px;
border: 1px solid var(--border);
border-radius: 16px;
background: var(--panel);
box-shadow: 0 14px 38px rgba(0, 0, 0, 0.28);
backdrop-filter: blur(12px);
pointer-events: none;
}
#one-card {
left: 22px;
border-top: 3px solid var(--cyan);
}
#two-card {
right: 22px;
border-top: 3px solid var(--gold);
}
.eyebrow {
color: var(--muted);
font-size: 11px;
font-weight: 800;
letter-spacing: 0.12em;
text-transform: uppercase;
}
.readout h2 {
margin: 4px 0 13px;
font-size: 18px;
}
.metric {
display: flex;
align-items: baseline;
justify-content: space-between;
gap: 12px;
margin: 8px 0;
}
.metric span {
color: var(--muted);
font-size: 13px;
}
.metric strong {
font-size: 19px;
font-variant-numeric: tabular-nums;
}
.formula {
margin-top: 12px;
padding-top: 10px;
border-top: 1px solid var(--border);
color: var(--muted);
font-size: 12px;
line-height: 1.55;
}
.formula b {
color: var(--text);
}
#controls {
position: absolute;
left: 50%;
bottom: 22px;
transform: translateX(-50%);
display: flex;
align-items: center;
gap: 22px;
width: max-content;
max-width: calc(100% - 28px);
padding: 14px 17px;
border: 1px solid var(--border);
border-radius: 17px;
background: var(--panel);
box-shadow: 0 14px 38px rgba(0, 0, 0, 0.3);
backdrop-filter: blur(12px);
}
.control {
min-width: 150px;
}
.control label {
display: flex;
justify-content: space-between;
gap: 18px;
margin-bottom: 7px;
color: var(--muted);
font-size: 12px;
}
.control output {
color: var(--text);
font-weight: 700;
font-variant-numeric: tabular-nums;
}
input[type="range"] {
width: 100%;
accent-color: var(--cyan);
}
button {
min-width: 100px;
padding: 10px 15px;
border: 1px solid rgba(82, 214, 255, 0.45);
border-radius: 10px;
background: rgba(82, 214, 255, 0.13);
color: var(--cyan);
font: inherit;
font-weight: 750;
cursor: pointer;
}
button:hover {
background: rgba(82, 214, 255, 0.22);
}
button.secondary {
border-color: var(--border);
background: rgba(148, 163, 184, 0.09);
color: var(--text);
}
button.secondary:hover {
background: rgba(148, 163, 184, 0.17);
}
#hint {
position: absolute;
left: 16px;
bottom: 15px;
color: rgba(220, 231, 246, 0.65);
font-size: 12px;
pointer-events: none;
}
#status {
position: absolute;
top: 92px;
left: 50%;
transform: translateX(-50%);
padding: 5px 10px;
border: 1px solid var(--border);
border-radius: 999px;
background: rgba(9, 17, 31, 0.78);
color: var(--green);
font-size: 11px;
font-weight: 750;
letter-spacing: 0.06em;
text-transform: uppercase;
pointer-events: none;
}
@media (max-width: 760px) {
header {
top: 12px;
}
header p {
display: none;
}
#status {
top: 56px;
}
.readout {
top: 88px;
width: calc(50% - 16px);
padding: 10px;
}
#one-card {
left: 9px;
}
#two-card {
right: 9px;
}
.readout h2 {
margin-bottom: 7px;
font-size: 14px;
}
.metric {
display: block;
margin: 5px 0;
}
.metric span,
.metric strong {
display: block;
}
.metric strong {
font-size: 16px;
}
.formula {
display: none;
}
#controls {
bottom: 12px;
gap: 10px;
padding: 10px;
flex-wrap: wrap;
justify-content: center;
}
.control {
min-width: 120px;
flex: 1;
}
#hint {
display: none;
}
}
</style>
</head>
<body>
<canvas id="scene"></canvas>
<header>
<h1>Voxel Pulley Comparison</h1>
<p>Ideal pulleys · negligible friction and pulley mass</p>
</header>
<div id="status">Ready</div>
<section id="one-card" class="readout">
<div class="eyebrow">One supporting segment</div>
<h2>Fixed Pulley</h2>
<div class="metric">
<span>Pull force</span>
<strong id="force-one">—</strong>
</div>
<div class="metric">
<span>Free-end pull</span>
<strong id="distance-one">—</strong>
</div>
<div class="formula">
Load weight: <b id="weight-one">—</b><br>
<b>F = W</b><br>
<b>s = h</b>
</div>
</section>
<section id="two-card" class="readout">
<div class="eyebrow">Two supporting segments</div>
<h2>Moving + Fixed Pulley</h2>
<div class="metric">
<span>Pull force</span>
<strong id="force-two">—</strong>
</div>
<div class="metric">
<span>Free-end pull</span>
<strong id="distance-two">—</strong>
</div>
<div class="formula">
Load weight: <b id="weight-two">—</b><br>
<b>F = W / 2</b><br>
<b>s = 2h</b>
</div>
</section>
<div id="controls">
<div class="control">
<label for="mass">
Load mass
<output id="mass-output">120 kg</output>
</label>
<input id="mass" type="range" min="10" max="300" step="5" value="120">
</div>
<div class="control">
<label for="height">
Load rise
<output id="height-output">1.20 m</output>
</label>
<input id="height" type="range" min="0.5" max="2" step="0.05" value="1.2">
</div>
<button id="play" type="button">Pause</button>
<button id="reset" class="secondary" type="button">Reset</button>
</div>
<div id="hint">Drag to orbit · Scroll to zoom · Double-click to restore the view</div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// ------------------------------------------------------------
// DOM and simulation state
// ------------------------------------------------------------
const canvas = document.querySelector("#scene");
const massInput = document.querySelector("#mass");
const heightInput = document.querySelector("#height");
const massOutput = document.querySelector("#mass-output");
const heightOutput = document.querySelector("#height-output");
const playButton = document.querySelector("#play");
const resetButton = document.querySelector("#reset");
const statusElement = document.querySelector("#status");
const G = 9.81;
const ROPE_RADIUS = 0.55;
const FIXED_HEIGHT = 5.35;
const FREE_START_HEIGHT = 4.4;
const state = {
mass: 120,
height: 1.2,
elapsed: 0,
duration: 4.8,
playing: true
};
function formatForce(value) {
return new Intl.NumberFormat("en-US", {
maximumFractionDigits: 1
}).format(value);
}
function updateReadouts() {
const weight = state.mass * G;
const forceOne = weight;
const forceTwo = weight / 2;
const distanceOne = state.height;
const distanceTwo = 2 * state.height;
document.querySelector("#weight-one").textContent =
`${formatForce(weight)} N`;
document.querySelector("#weight-two").textContent =
`${formatForce(weight)} N`;
document.querySelector("#force-one").textContent =
`${formatForce(forceOne)} N`;
document.querySelector("#force-two").textContent =
`${formatForce(forceTwo)} N`;
document.querySelector("#distance-one").textContent =
`${distanceOne.toFixed(2)} m`;
document.querySelector("#distance-two").textContent =
`${distanceTwo.toFixed(2)} m`;
massOutput.textContent = `${state.mass.toFixed(0)} kg`;
heightOutput.textContent = `${state.height.toFixed(2)} m`;
}
// ------------------------------------------------------------
// Renderer, scene, lights and camera
// ------------------------------------------------------------
const renderer = new THREE.WebGLRenderer({
canvas,
antialias: true,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
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(0x09111f);
scene.fog = new THREE.Fog(0x09111f, 23, 48);
const camera = new THREE.PerspectiveCamera(
42,
window.innerWidth / window.innerHeight,
0.1,
100
);
const hemisphere = new THREE.HemisphereLight(0xb9dcff, 0x182033, 2.1);
scene.add(hemisphere);
const keyLight = new THREE.DirectionalLight(0xffffff, 3.2);
keyLight.position.set(5, 12, 8);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(2048, 2048);
keyLight.shadow.camera.left = -14;
keyLight.shadow.camera.right = 14;
keyLight.shadow.camera.top = 12;
keyLight.shadow.camera.bottom = -8;
keyLight.shadow.camera.near = 0.5;
keyLight.shadow.camera.far = 35;
scene.add(keyLight);
const rimLight = new THREE.DirectionalLight(0x4ac8ff, 1.7);
rimLight.position.set(-9, 7, -8);
scene.add(rimLight);
const warmLight = new THREE.DirectionalLight(0xffbb66, 1.1);
warmLight.position.set(8, 4, -4);
scene.add(warmLight);
const floor = new THREE.Mesh(
new THREE.PlaneGeometry(44, 26),
new THREE.MeshStandardMaterial({
color: 0x0e192a,
roughness: 0.92,
metalness: 0.04
})
);
floor.rotation.x = -Math.PI / 2;
floor.position.y = -0.08;
floor.receiveShadow = true;
scene.add(floor);
const grid = new THREE.GridHelper(36, 36, 0x28405b, 0x18283e);
grid.position.y = -0.065;
scene.add(grid);
// ------------------------------------------------------------
// Materials
// ------------------------------------------------------------
const material = {
frame: new THREE.MeshStandardMaterial({
color: 0x33445c,
roughness: 0.64,
metalness: 0.55
}),
frameDark: new THREE.MeshStandardMaterial({
color: 0x1c2b40,
roughness: 0.72,
metalness: 0.48
}),
pulleyOne: new THREE.MeshStandardMaterial({
color: 0x39bde9,
roughness: 0.38,
metalness: 0.48
}),
pulleyTwo: new THREE.MeshStandardMaterial({
color: 0xf4ad32,
roughness: 0.4,
metalness: 0.48
}),
rope: new THREE.MeshStandardMaterial({
color: 0xf3f6fb,
roughness: 0.75,
metalness: 0.02
}),
cargoA: new THREE.MeshStandardMaterial({
color: 0xff7b54,
roughness: 0.72,
metalness: 0.12
}),
cargoB: new THREE.MeshStandardMaterial({
color: 0xf6c453,
roughness: 0.7,
metalness: 0.12
}),
handle: new THREE.MeshStandardMaterial({
color: 0x66e3a4,
emissive: 0x174b36,
emissiveIntensity: 0.8,
roughness: 0.42,
metalness: 0.35
}),
marker: new THREE.MeshStandardMaterial({
color: 0xffffff,
emissive: 0xffffff,
emissiveIntensity: 0.25,
roughness: 0.45
})
};
// ------------------------------------------------------------
// Geometry helpers
// ------------------------------------------------------------
const boxGeometry = new THREE.BoxGeometry(1, 1, 1);
const ropeGeometry = new THREE.CylinderGeometry(
0.035,
0.035,
1,
8
);
const Y_AXIS = new THREE.Vector3(0, 1, 0);
function addBox(parent, position, size, boxMaterial) {
const mesh = new THREE.Mesh(boxGeometry, boxMaterial);
mesh.position.set(...position);
mesh.scale.set(...size);
mesh.castShadow = true;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
function setCylinderBetween(mesh, a, b) {
const direction = new THREE.Vector3().subVectors(b, a);
const length = direction.length();
mesh.position.copy(a).add(b).multiplyScalar(0.5);
mesh.quaternion.setFromUnitVectors(Y_AXIS, direction.normalize());
mesh.scale.set(1, length, 1);
}
function makeRopeSegment() {
const mesh = new THREE.Mesh(ropeGeometry, material.rope);
mesh.castShadow = true;
return mesh;
}
function makeVoxelCrate(crateMaterial) {
const group = new THREE.Group();
const cubeSize = 0.31;
const spacing = 0.34;
for (let row = 0; row < 3; row++) {
for (let column = 0; column < 3; column++) {
addBox(
group,
[
(column - 1) * spacing,
(row - 1) * spacing,
0
],
[cubeSize, cubeSize, 0.68],
crateMaterial
);
}
}
// Corner bands give the crate a more structural voxel look.
addBox(
group,
[-0.48, 0, 0.37],
[0.09, 1.08, 0.09],
material.frameDark
);
addBox(
group,
[0.48, 0, 0.37],
[0.09, 1.08, 0.09],
material.frameDark
);
return group;
}
function makeVoxelPulley(pulleyMaterial) {
const group = new THREE.Group();
const blockCount = 16;
// Block-built outer rim.
for (let i = 0; i < blockCount; i++) {
const angle = (i / blockCount) * Math.PI * 2;
const rimBlock = addBox(
group,
[
Math.cos(angle) * 0.46,
Math.sin(angle) * 0.46,
0
],
[0.27, 0.27, 0.46],
pulleyMaterial
);
rimBlock.rotation.z = angle;
}
// Four voxel-like spokes.
addBox(group, [0, 0, 0], [0.94, 0.13, 0.29], pulleyMaterial);
addBox(group, [0, 0, 0], [0.13, 0.94, 0.29], pulleyMaterial);
// Central hub and axle.
addBox(group, [0, 0, 0], [0.38, 0.38, 0.58], material.frameDark);
addBox(group, [0, 0, 0.05], [0.16, 0.16, 0.74], material.marker);
// A contrasting timing mark makes pulley rotation visible.
const timingMark = addBox(
group,
[0.44, 0, 0.25],
[0.18, 0.18, 0.12],
material.marker
);
group.userData.timingMark = timingMark;
return group;
}
function makeArcRope(startAngle, endAngle, segmentCount = 16) {
const group = new THREE.Group();
for (let i = 0; i < segmentCount; i++) {
const a1 = THREE.MathUtils.lerp(
startAngle,
endAngle,
i / segmentCount
);
const a2 = THREE.MathUtils.lerp(
startAngle,
endAngle,
(i + 1) / segmentCount
);
const p1 = new THREE.Vector3(
Math.cos(a1) * ROPE_RADIUS,
Math.sin(a1) * ROPE_RADIUS,
0
);
const p2 = new THREE.Vector3(
Math.cos(a2) * ROPE_RADIUS,
Math.sin(a2) * ROPE_RADIUS,
0
);
const segment = makeRopeSegment();
setCylinderBetween(segment, p1, p2);
group.add(segment);
}
return group;
}
function makeFrame(parent) {
const frame = new THREE.Group();
addBox(
frame,
[-1.35, 2.95, 0],
[0.22, 6.0, 0.3],
material.frame
);
addBox(
frame,
[2.55, 2.95, 0],
[0.22, 6.0, 0.3],
material.frame
);
addBox(
frame,
[0.6, 6.03, 0],
[4.12, 0.25, 0.38],
material.frame
);
addBox(
frame,
[-1.35, 0.12, 0],
[0.62, 0.24, 0.62],
material.frameDark
);
addBox(
frame,
[2.55, 0.12, 0],
[0.62, 0.24, 0.62],
material.frameDark
);
parent.add(frame);
return frame;
}
function makeLabel(text, color) {
const labelCanvas = document.createElement("canvas");
labelCanvas.width = 768;
labelCanvas.height = 160;
const context = labelCanvas.getContext("2d");
context.clearRect(0, 0, labelCanvas.width, labelCanvas.height);
context.fillStyle = "rgba(7, 14, 26, 0.78)";
context.fillRect(0, 0, labelCanvas.width, labelCanvas.height);
context.strokeStyle = color;
context.lineWidth = 8;
context.strokeRect(4, 4, labelCanvas.width - 8, labelCanvas.height - 8);
context.fillStyle = color;
context.font = "700 48px system-ui, sans-serif";
context.textAlign = "center";
context.textBaseline = "middle";
context.fillText(text, labelCanvas.width / 2, labelCanvas.height / 2 + 2);
const texture = new THREE.CanvasTexture(labelCanvas);
texture.colorSpace = THREE.SRGBColorSpace;
const sprite = new THREE.Sprite(
new THREE.SpriteMaterial({
map: texture,
transparent: true,
depthWrite: false
})
);
sprite.scale.set(4.1, 0.85, 1);
return sprite;
}
// ------------------------------------------------------------
// One-segment system
// ------------------------------------------------------------
const oneSystem = new THREE.Group();
oneSystem.position.x = -5.2;
scene.add(oneSystem);
makeFrame(oneSystem);
const oneLabel = makeLabel("1 SUPPORTING SEGMENT", "#52d6ff");
oneLabel.position.set(0.55, 6.62, 0);
oneSystem.add(oneLabel);
const pulleyOne = makeVoxelPulley(material.pulleyOne);
pulleyOne.position.set(0, FIXED_HEIGHT, 0);
oneSystem.add(pulleyOne);
const crateOne = makeVoxelCrate(material.cargoA);
crateOne.position.set(-0.55, 1.1, 0);
oneSystem.add(crateOne);
const supportOne = makeRopeSegment();
const freeOne = makeRopeSegment();
oneSystem.add(supportOne, freeOne);
const fixedArcOne = makeArcRope(Math.PI, 0);
fixedArcOne.position.set(0, FIXED_HEIGHT, 0);
oneSystem.add(fixedArcOne);
const handleOne = addBox(
oneSystem,
[0.55, FREE_START_HEIGHT, 0],
[0.38, 0.16, 0.38],
material.handle
);
// ------------------------------------------------------------
// Two-segment system
// ------------------------------------------------------------
const twoSystem = new THREE.Group();
twoSystem.position.x = 3.0;
scene.add(twoSystem);
makeFrame(twoSystem);
const twoLabel = makeLabel("2 SUPPORTING SEGMENTS", "#ffc857");
twoLabel.position.set(0.55, 6.62, 0);
twoSystem.add(twoLabel);
const fixedPulleyTwo = makeVoxelPulley(material.pulleyTwo);
fixedPulleyTwo.position.set(1.1, FIXED_HEIGHT, 0);
twoSystem.add(fixedPulleyTwo);
const movingPulleyTwo = makeVoxelPulley(material.pulleyTwo);
movingPulleyTwo.position.set(0, 2.0, 0);
twoSystem.add(movingPulleyTwo);
const crateTwo = makeVoxelCrate(material.cargoB);
crateTwo.position.set(0, 0.82, 0);
twoSystem.add(crateTwo);
const connectorTwo = addBox(
twoSystem,
[0, 1.34, 0],
[0.16, 0.64, 0.16],
material.frameDark
);
const anchorTwo = addBox(
twoSystem,
[-0.55, 5.92, 0],
[0.32, 0.24, 0.52],
material.frameDark
);
const leftSupportTwo = makeRopeSegment();
const rightSupportTwo = makeRopeSegment();
const freeTwo = makeRopeSegment();
twoSystem.add(leftSupportTwo, rightSupportTwo, freeTwo);
const movingArcTwo = makeArcRope(Math.PI, Math.PI * 2);
movingArcTwo.position.set(0, 2.0, 0);
twoSystem.add(movingArcTwo);
const fixedArcTwo = makeArcRope(Math.PI, 0);
fixedArcTwo.position.set(1.1, FIXED_HEIGHT, 0);
twoSystem.add(fixedArcTwo);
const handleTwo = addBox(
twoSystem,
[1.65, FREE_START_HEIGHT, 0],
[0.38, 0.16, 0.38],
material.handle
);
// ------------------------------------------------------------
// Update the mechanisms
// ------------------------------------------------------------
function updateMechanisms(progress) {
const h = state.height;
const rise = h * progress;
// System 1:
// The fixed pulley only changes force direction.
const crateOneY = 1.1 + rise;
crateOne.position.y = crateOneY;
const attachOneY = crateOneY + 0.56;
const freeOneY = FREE_START_HEIGHT - h * progress;
setCylinderBetween(
supportOne,
new THREE.Vector3(-0.55, attachOneY, 0),
new THREE.Vector3(-0.55, FIXED_HEIGHT, 0)
);
setCylinderBetween(
freeOne,
new THREE.Vector3(0.55, FIXED_HEIGHT, 0),
new THREE.Vector3(0.55, freeOneY, 0)
);
handleOne.position.y = freeOneY;
pulleyOne.rotation.z = -rise / ROPE_RADIUS;
// System 2:
// The load rises by h, while the free end is pulled by 2h.
const movingY = 2.0 + rise;
const freeTwoY = FREE_START_HEIGHT - 2 * h * progress;
movingPulleyTwo.position.y = movingY;
movingArcTwo.position.y = movingY;
crateTwo.position.y = movingY - 1.18;
connectorTwo.position.y = movingY - 0.66;
setCylinderBetween(
leftSupportTwo,
new THREE.Vector3(-0.55, 5.9, 0),
new THREE.Vector3(-0.55, movingY, 0)
);
setCylinderBetween(
rightSupportTwo,
new THREE.Vector3(0.55, movingY, 0),
new THREE.Vector3(0.55, FIXED_HEIGHT, 0)
);
setCylinderBetween(
freeTwo,
new THREE.Vector3(1.65, FIXED_HEIGHT, 0),
new THREE.Vector3(1.65, freeTwoY, 0)
);
handleTwo.position.y = freeTwoY;
// Ideal no-slip angular displacement, used only for visual rotation.
movingPulleyTwo.rotation.z = -rise / ROPE_RADIUS;
fixedPulleyTwo.rotation.z = -(2 * rise) / ROPE_RADIUS;
}
// ------------------------------------------------------------
// Custom orbit controls — no Three.js addons
// ------------------------------------------------------------
const cameraTarget = new THREE.Vector3(-1.05, 2.9, 0);
const orbit = {
theta: 0.10,
phi: 1.22,
radius: 14.5
};
function updateCamera() {
const sinPhi = Math.sin(orbit.phi);
camera.position.set(
cameraTarget.x +
orbit.radius * sinPhi * Math.sin(orbit.theta),
cameraTarget.y +
orbit.radius * Math.cos(orbit.phi),
cameraTarget.z +
orbit.radius * sinPhi * Math.cos(orbit.theta)
);
camera.lookAt(cameraTarget);
}
function resetCamera() {
orbit.theta = 0.10;
orbit.phi = 1.22;
orbit.radius = window.innerWidth / window.innerHeight < 1
? Math.max(25, 19.5 / (window.innerWidth / window.innerHeight))
: 14.5;
updateCamera();
}
let dragging = false;
let previousX = 0;
let previousY = 0;
canvas.addEventListener("pointerdown", (event) => {
dragging = true;
previousX = event.clientX;
previousY = event.clientY;
canvas.setPointerCapture(event.pointerId);
});
canvas.addEventListener("pointermove", (event) => {
if (!dragging) return;
const deltaX = event.clientX - previousX;
const deltaY = event.clientY - previousY;
previousX = event.clientX;
previousY = event.clientY;
orbit.theta -= deltaX * 0.006;
orbit.phi = THREE.MathUtils.clamp(
orbit.phi + deltaY * 0.005,
0.42,
1.48
);
updateCamera();
});
function endDrag(event) {
dragging = false;
if (event.pointerId !== undefined) {
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),
7,
55
);
updateCamera();
},
{ passive: false }
);
canvas.addEventListener("dblclick", resetCamera);
// ------------------------------------------------------------
// Controls
// ------------------------------------------------------------
function updatePlayButton() {
if (state.elapsed >= state.duration) {
playButton.textContent = "Replay";
statusElement.textContent = "Complete";
statusElement.style.color = "#ffc857";
} else if (state.playing) {
playButton.textContent = "Pause";
statusElement.textContent = "Pulling";
statusElement.style.color = "#66e3a4";
} else {
playButton.textContent = "Continue";
statusElement.textContent = "Paused";
statusElement.style.color = "#9eb0c8";
}
}
function resetSimulation() {
state.elapsed = 0;
state.playing = false;
updateMechanisms(0);
updatePlayButton();
}
playButton.addEventListener("click", () => {
if (state.elapsed >= state.duration) {
state.elapsed = 0;
}
state.playing = !state.playing;
updatePlayButton();
});
resetButton.addEventListener("click", resetSimulation);
massInput.addEventListener("input", () => {
state.mass = Number(massInput.value);
updateReadouts();
resetSimulation();
});
heightInput.addEventListener("input", () => {
state.height = Number(heightInput.value);
updateReadouts();
resetSimulation();
});
window.addEventListener("resize", () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setPixelRatio(
Math.min(window.devicePixelRatio, 2)
);
renderer.setSize(
window.innerWidth,
window.innerHeight
);
});
// ------------------------------------------------------------
// Animation loop
// ------------------------------------------------------------
const clock = new THREE.Clock();
function animate() {
requestAnimationFrame(animate);
const delta = Math.min(clock.getDelta(), 0.05);
if (state.playing) {
state.elapsed = Math.min(
state.elapsed + delta,
state.duration
);
if (state.elapsed >= state.duration) {
state.playing = false;
updatePlayButton();
}
}
const progress = state.elapsed / state.duration;
updateMechanisms(progress);
renderer.render(scene, camera);
}
updateReadouts();
updateMechanisms(0);
resetCamera();
updatePlayButton();
animate();
</script>
</body>
</html>
```