Prompt specification
Build or customize the Three.js 3D scene "Movable pulley rope-length model" 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: Movable pulley rope-length model
Category: Science & Physics · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/movable-pulley-rope-length-model-x1421
Scene Description Prompt:
Show a low-poly movable pulley, continuous rope, and two segments supporting its load. One HTML file using Three.js from cdnjs.
Working Three.js Starter (movable-pulley-rope-length-model-x1421):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Low-Poly Movable Pulley</title>
<style>
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #eef2f5;
font-family: Inter, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}
canvas {
display: block;
touch-action: none;
}
#title {
position: fixed;
top: 22px;
left: 24px;
z-index: 10;
max-width: 390px;
pointer-events: none;
}
#title h1 {
margin: 0 0 7px;
color: #172432;
font-size: clamp(24px, 3vw, 38px);
line-height: 1;
letter-spacing: -0.04em;
}
#title p {
margin: 0;
color: #526273;
font-size: 14px;
line-height: 1.5;
}
#controls {
position: fixed;
top: 22px;
right: 22px;
z-index: 20;
display: grid;
gap: 8px;
width: 174px;
padding: 12px;
border: 1px solid rgba(255, 255, 255, 0.85);
border-radius: 16px;
background: rgba(255, 255, 255, 0.78);
box-shadow: 0 12px 35px rgba(31, 48, 66, 0.13);
backdrop-filter: blur(12px);
}
button {
width: 100%;
padding: 10px 12px;
border: 0;
border-radius: 10px;
color: white;
background: #ed7443;
font: inherit;
font-size: 13px;
font-weight: 750;
cursor: pointer;
transition:
transform 120ms ease,
filter 120ms ease;
}
button:hover {
filter: brightness(1.06);
transform: translateY(-1px);
}
button:active {
transform: translateY(1px);
}
button.secondary {
color: #273849;
background: #e6edf2;
}
#readout {
position: fixed;
right: 22px;
bottom: 22px;
z-index: 10;
min-width: 214px;
padding: 15px 17px;
border: 1px solid rgba(255, 255, 255, 0.88);
border-radius: 15px;
color: #344556;
background: rgba(255, 255, 255, 0.82);
box-shadow: 0 12px 35px rgba(31, 48, 66, 0.12);
backdrop-filter: blur(12px);
pointer-events: none;
}
#readout .row {
display: flex;
justify-content: space-between;
gap: 18px;
margin: 5px 0;
font-size: 13px;
}
#readout strong {
color: #172432;
}
#readout .advantage {
margin-top: 10px;
padding-top: 9px;
border-top: 1px solid #dce4e9;
color: #178d8a;
font-size: 12px;
font-weight: 750;
}
#legend {
position: fixed;
left: 24px;
bottom: 22px;
z-index: 10;
display: flex;
align-items: center;
gap: 9px;
padding: 10px 13px;
border-radius: 12px;
color: #465667;
background: rgba(255, 255, 255, 0.78);
box-shadow: 0 10px 25px rgba(31, 48, 66, 0.09);
backdrop-filter: blur(10px);
font-size: 12px;
pointer-events: none;
}
.dot {
width: 10px;
height: 10px;
border-radius: 50%;
background: #29c7c6;
box-shadow: 0 0 0 4px rgba(41, 199, 198, 0.14);
}
#labels {
position: fixed;
inset: 0;
z-index: 5;
overflow: hidden;
pointer-events: none;
}
.tag {
position: absolute;
padding: 5px 8px;
border: 1px solid rgba(255, 255, 255, 0.9);
border-radius: 7px;
color: #334354;
background: rgba(255, 255, 255, 0.88);
box-shadow: 0 5px 14px rgba(30, 45, 62, 0.1);
font-size: 11px;
font-weight: 700;
white-space: nowrap;
transform: translate(-50%, -50%);
}
.tag.cyan {
color: #147f7e;
background: rgba(231, 255, 254, 0.92);
}
.tag.orange {
color: #a74a24;
background: rgba(255, 241, 232, 0.94);
}
@media (max-width: 700px) {
#title {
top: 16px;
left: 16px;
max-width: calc(100vw - 190px);
}
#title p {
font-size: 12px;
}
#controls {
top: 14px;
right: 14px;
width: 150px;
padding: 9px;
}
button {
padding: 9px 8px;
font-size: 12px;
}
#readout {
right: 14px;
bottom: 14px;
min-width: 190px;
}
#legend {
left: 14px;
bottom: 14px;
max-width: calc(100vw - 225px);
}
}
</style>
</head>
<body>
<div id="title">
<h1>Movable Pulley</h1>
<p>Drag the orange handle downward to lift the load. Drag the background to rotate; scroll to zoom.</p>
</div>
<div id="controls">
<button id="pull">Pull rope ↑</button>
<button id="lower" class="secondary">Lower load ↓</button>
<button id="reset" class="secondary">Reset demo</button>
</div>
<div id="readout">
<div class="row">
<span>Rope pulled</span>
<strong id="ropeValue">0.00 m</strong>
</div>
<div class="row">
<span>Load rise</span>
<strong id="loadValue">0.00 m</strong>
</div>
<div class="advantage">T₁ = T₂ = ½ load · 2× advantage</div>
</div>
<div id="legend">
<span class="dot"></span>
<span>Two rope segments support the movable pulley</span>
</div>
<div id="labels">
<div id="tagAnchor" class="tag">Fixed end</div>
<div id="tagFixed" class="tag">Fixed pulley</div>
<div id="tagMoving" class="tag orange">Movable pulley</div>
<div id="tagLoad" class="tag orange">Load</div>
<div id="tagFree" class="tag orange">Drag to pull</div>
<div id="tagSupportA" class="tag cyan">Supporting segment 1</div>
<div id="tagSupportB" class="tag cyan">Supporting segment 2</div>
</div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// ------------------------------------------------------------
// Constants
// ------------------------------------------------------------
const MOVING_RADIUS = 0.56;
const FIXED_RADIUS = 0.42;
const ANCHOR_Y = 5.6;
const FIXED_Y = 5.6;
const FIXED_X = MOVING_RADIUS + FIXED_RADIUS;
const FREE_X = FIXED_X + FIXED_RADIUS;
const START_MOVING_Y = 1.65;
const MAX_MOVING_Y = 3.35;
const START_FREE_Y = 2.6;
const UP = new THREE.Vector3(0, 1, 0);
// ------------------------------------------------------------
// Renderer, scene and camera
// ------------------------------------------------------------
const renderer = new THREE.WebGLRenderer({ antialias: true });
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.08;
document.body.appendChild(renderer.domElement);
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xeef2f5);
scene.fog = new THREE.Fog(0xeef2f5, 12, 27);
const camera = new THREE.PerspectiveCamera(
40,
window.innerWidth / window.innerHeight,
0.1,
60
);
const orbit = {
radius: 11.2,
theta: 0.47,
phi: 1.19,
target: new THREE.Vector3(0.15, 2.35, 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);
}
function resetCamera() {
orbit.radius = 11.2;
orbit.theta = 0.47;
orbit.phi = 1.19;
orbit.target.set(0.15, 2.35, 0);
updateCamera();
}
updateCamera();
// ------------------------------------------------------------
// Lighting
// ------------------------------------------------------------
const hemisphere = new THREE.HemisphereLight(0xffffff, 0x768594, 2.15);
scene.add(hemisphere);
const keyLight = new THREE.DirectionalLight(0xffffff, 3.2);
keyLight.position.set(5.5, 10, 7);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(2048, 2048);
keyLight.shadow.camera.left = -7;
keyLight.shadow.camera.right = 7;
keyLight.shadow.camera.top = 9;
keyLight.shadow.camera.bottom = -4;
keyLight.shadow.bias = -0.0004;
scene.add(keyLight);
const fillLight = new THREE.DirectionalLight(0x91c8ff, 1.0);
fillLight.position.set(-6, 4, 3);
scene.add(fillLight);
// ------------------------------------------------------------
// Materials
// ------------------------------------------------------------
const frameMaterial = new THREE.MeshStandardMaterial({
color: 0x31485c,
roughness: 0.72,
metalness: 0.28
});
const steelMaterial = new THREE.MeshStandardMaterial({
color: 0x526575,
roughness: 0.42,
metalness: 0.7
});
const wheelMaterial = new THREE.MeshStandardMaterial({
color: 0xed7443,
roughness: 0.58,
metalness: 0.18,
flatShading: true
});
const hubMaterial = new THREE.MeshStandardMaterial({
color: 0x263b4d,
roughness: 0.4,
metalness: 0.7,
flatShading: true
});
const woodMaterial = new THREE.MeshStandardMaterial({
color: 0xa96d3d,
roughness: 0.9,
flatShading: true
});
const woodLightMaterial = new THREE.MeshStandardMaterial({
color: 0xc88a53,
roughness: 0.92,
flatShading: true
});
const ropeMaterial = new THREE.MeshStandardMaterial({
color: 0xf2b85f,
roughness: 0.88,
metalness: 0,
flatShading: true
});
// ------------------------------------------------------------
// Helpers
// ------------------------------------------------------------
function shadowed(mesh, cast = true, receive = true) {
mesh.castShadow = cast;
mesh.receiveShadow = receive;
return mesh;
}
function alignY(mesh, a, b) {
const delta = new THREE.Vector3().subVectors(b, a);
const length = delta.length();
mesh.position.copy(a).add(b).multiplyScalar(0.5);
mesh.scale.set(1, length, 1);
mesh.quaternion.setFromUnitVectors(UP, delta.normalize());
return mesh;
}
function beam(parent, a, b, width, depth, material) {
const mesh = new THREE.Mesh(
new THREE.BoxGeometry(width, 1, depth),
material
);
alignY(mesh, a, b);
parent.add(mesh);
return mesh;
}
function cylinderAlongZ(radius, length, material, segments = 12) {
const geometry = new THREE.CylinderGeometry(
radius,
radius,
length,
segments
);
geometry.rotateX(Math.PI / 2);
return shadowed(new THREE.Mesh(geometry, material));
}
// ------------------------------------------------------------
// Ground and support frame
// ------------------------------------------------------------
const ground = new THREE.Mesh(
new THREE.PlaneGeometry(30, 30),
new THREE.MeshStandardMaterial({
color: 0xdfe6eb,
roughness: 1
})
);
ground.rotation.x = -Math.PI / 2;
ground.position.y = -1.55;
ground.receiveShadow = true;
scene.add(ground);
const grid = new THREE.GridHelper(20, 40, 0x9eacb7, 0xbcc8d0);
grid.position.y = -1.545;
grid.material.transparent = true;
grid.material.opacity = 0.24;
scene.add(grid);
const frame = new THREE.Group();
scene.add(frame);
const leftPost = shadowed(
new THREE.Mesh(
new THREE.BoxGeometry(0.28, 7.95, 0.38),
frameMaterial
)
);
leftPost.position.set(-2.15, 2.425, -0.55);
frame.add(leftPost);
const rightPost = shadowed(
new THREE.Mesh(
new THREE.BoxGeometry(0.28, 7.95, 0.38),
frameMaterial
)
);
rightPost.position.set(2.55, 2.425, -0.55);
frame.add(rightPost);
const topBeam = shadowed(
new THREE.Mesh(
new THREE.BoxGeometry(5.05, 0.28, 0.42),
frameMaterial
)
);
topBeam.position.set(0.2, 6.4, -0.55);
frame.add(topBeam);
const footGeometry = new THREE.BoxGeometry(0.9, 0.16, 1.1);
const leftFoot = shadowed(new THREE.Mesh(footGeometry, frameMaterial));
const rightFoot = shadowed(new THREE.Mesh(footGeometry, frameMaterial));
leftFoot.position.set(-2.15, -1.47, -0.55);
rightFoot.position.set(2.55, -1.47, -0.55);
frame.add(leftFoot, rightFoot);
// Fixed rope-end anchor.
beam(
frame,
new THREE.Vector3(-MOVING_RADIUS, 6.27, -0.24),
new THREE.Vector3(-MOVING_RADIUS, 5.72, -0.24),
0.12,
0.14,
steelMaterial
);
const anchorRing = shadowed(
new THREE.Mesh(
new THREE.TorusGeometry(0.14, 0.035, 4, 10),
steelMaterial
)
);
anchorRing.position.set(-MOVING_RADIUS, ANCHOR_Y, -0.16);
frame.add(anchorRing);
// Bracket behind the fixed pulley.
beam(
frame,
new THREE.Vector3(FIXED_X, 6.27, -0.38),
new THREE.Vector3(FIXED_X, FIXED_Y, -0.38),
0.16,
0.17,
steelMaterial
);
// ------------------------------------------------------------
// Low-poly pulley construction
// ------------------------------------------------------------
function createPulley(radius) {
const pulley = new THREE.Group();
// Two faceted rims form the rope groove.
for (const z of [-0.13, 0.13]) {
const rim = shadowed(
new THREE.Mesh(
new THREE.TorusGeometry(radius - 0.075, 0.075, 4, 12),
wheelMaterial
)
);
rim.position.z = z;
pulley.add(rim);
}
// Six low-poly spokes.
for (let i = 0; i < 6; i++) {
const angle = (i / 6) * Math.PI * 2;
const spoke = shadowed(
new THREE.Mesh(
new THREE.BoxGeometry(radius * 0.66, 0.075, 0.14),
hubMaterial
)
);
spoke.position.set(
Math.cos(angle) * radius * 0.45,
Math.sin(angle) * radius * 0.45,
0
);
spoke.rotation.z = angle;
pulley.add(spoke);
}
const hub = cylinderAlongZ(0.17, 0.4, hubMaterial);
pulley.add(hub);
const axle = cylinderAlongZ(0.075, 0.66, steelMaterial);
pulley.add(axle);
return pulley;
}
const fixedPulley = createPulley(FIXED_RADIUS);
fixedPulley.position.set(FIXED_X, FIXED_Y, 0);
scene.add(fixedPulley);
// ------------------------------------------------------------
// Movable pulley, yoke and load
// ------------------------------------------------------------
const movingAssembly = new THREE.Group();
scene.add(movingAssembly);
const movingPulley = createPulley(MOVING_RADIUS);
movingAssembly.add(movingPulley);
// Two yoke plates connect the pulley axle to the load.
for (const z of [-0.3, 0.3]) {
beam(
movingAssembly,
new THREE.Vector3(-0.3, -0.06, z),
new THREE.Vector3(0, -1.04, z),
0.09,
0.09,
steelMaterial
);
beam(
movingAssembly,
new THREE.Vector3(0.3, -0.06, z),
new THREE.Vector3(0, -1.04, z),
0.09,
0.09,
steelMaterial
);
}
const lowerConnector = shadowed(
new THREE.Mesh(
new THREE.CylinderGeometry(0.09, 0.09, 0.23, 8),
steelMaterial
)
);
lowerConnector.position.set(0, -1.08, 0);
movingAssembly.add(lowerConnector);
const crate = new THREE.Group();
crate.position.y = -1.58;
movingAssembly.add(crate);
const crateBody = shadowed(
new THREE.Mesh(
new THREE.BoxGeometry(1.15, 0.9, 0.9),
woodMaterial
)
);
crate.add(crateBody);
// Raised wooden slats on the visible front face.
for (const x of [-0.39, -0.13, 0.13, 0.39]) {
const slat = shadowed(
new THREE.Mesh(
new THREE.BoxGeometry(0.12, 0.79, 0.055),
woodLightMaterial
)
);
slat.position.set(x, 0, 0.477);
crate.add(slat);
}
// Metal bands around the crate.
for (const y of [-0.24, 0.24]) {
const band = shadowed(
new THREE.Mesh(
new THREE.BoxGeometry(1.19, 0.07, 0.94),
steelMaterial
)
);
band.position.y = y;
crate.add(band);
}
// ------------------------------------------------------------
// Free-end handle
// ------------------------------------------------------------
const handle = new THREE.Group();
scene.add(handle);
const handleGrip = shadowed(
new THREE.Mesh(
new THREE.CylinderGeometry(0.13, 0.13, 0.55, 8),
wheelMaterial
)
);
handleGrip.position.y = -0.16;
handle.add(handleGrip);
const handleCap = shadowed(
new THREE.Mesh(
new THREE.SphereGeometry(0.145, 8, 5),
wheelMaterial
)
);
handleCap.position.y = -0.44;
handle.add(handleCap);
const handlePick = new THREE.Mesh(
new THREE.SphereGeometry(0.34, 8, 6),
new THREE.MeshBasicMaterial({
transparent: true,
opacity: 0,
depthWrite: false,
colorWrite: false
})
);
handle.add(handlePick);
// ------------------------------------------------------------
// Highlighting for the two load-supporting rope segments
// ------------------------------------------------------------
const supportMaterial = new THREE.MeshBasicMaterial({
color: 0x29c7c6,
transparent: true,
opacity: 0.24,
depthWrite: false
});
const supportShellA = new THREE.Mesh(
new THREE.CylinderGeometry(0.075, 0.075, 1, 8),
supportMaterial
);
const supportShellB = supportShellA.clone();
scene.add(supportShellA, supportShellB);
const tensionArrowA = new THREE.ArrowHelper(
new THREE.Vector3(0, 1, 0),
new THREE.Vector3(),
1.08,
0x29c7c6,
0.2,
0.12
);
const tensionArrowB = new THREE.ArrowHelper(
new THREE.Vector3(0, 1, 0),
new THREE.Vector3(),
1.08,
0x29c7c6,
0.2,
0.12
);
scene.add(tensionArrowA, tensionArrowB);
// ------------------------------------------------------------
// Exact continuous rope curve
//
// Fixed end
// ↓
// moving pulley lower half
// ↑
// fixed pulley upper half
// ↓
// free handle
// ------------------------------------------------------------
class ContinuousRopeCurve extends THREE.Curve {
constructor() {
super();
this.segments = [];
this.totalLength = 0;
}
configure(movingY, freeY) {
this.segments = [
{
type: "line",
a: new THREE.Vector3(-MOVING_RADIUS, ANCHOR_Y, 0),
b: new THREE.Vector3(-MOVING_RADIUS, movingY, 0),
length: ANCHOR_Y - movingY
},
{
type: "arc",
center: new THREE.Vector3(0, movingY, 0),
radius: MOVING_RADIUS,
startAngle: Math.PI,
endAngle: Math.PI * 2,
length: Math.PI * MOVING_RADIUS
},
{
type: "line",
a: new THREE.Vector3(MOVING_RADIUS, movingY, 0),
b: new THREE.Vector3(MOVING_RADIUS, FIXED_Y, 0),
length: FIXED_Y - movingY
},
{
type: "arc",
center: new THREE.Vector3(FIXED_X, FIXED_Y, 0),
radius: FIXED_RADIUS,
startAngle: Math.PI,
endAngle: 0,
length: Math.PI * FIXED_RADIUS
},
{
type: "line",
a: new THREE.Vector3(FREE_X, FIXED_Y, 0),
b: new THREE.Vector3(FREE_X, freeY, 0),
length: FIXED_Y - freeY
}
];
this.totalLength = this.segments.reduce(
(sum, segment) => sum + segment.length,
0
);
}
getPoint(t, target = new THREE.Vector3()) {
let distance = THREE.MathUtils.clamp(t, 0, 1) * this.totalLength;
for (const segment of this.segments) {
if (distance <= segment.length || segment === this.segments.at(-1)) {
const u = THREE.MathUtils.clamp(
distance / segment.length,
0,
1
);
if (segment.type === "line") {
return target.lerpVectors(segment.a, segment.b, u);
}
const angle = THREE.MathUtils.lerp(
segment.startAngle,
segment.endAngle,
u
);
return target.set(
segment.center.x + Math.cos(angle) * segment.radius,
segment.center.y + Math.sin(angle) * segment.radius,
0
);
}
distance -= segment.length;
}
return target.copy(this.segments.at(-1).b);
}
}
const ropeCurve = new ContinuousRopeCurve();
const ropeMesh = shadowed(
new THREE.Mesh(
new THREE.BufferGeometry(),
ropeMaterial
)
);
scene.add(ropeMesh);
function rebuildRope(movingY, freeY) {
ropeCurve.configure(movingY, freeY);
const geometry = new THREE.TubeGeometry(
ropeCurve,
120,
0.055,
5,
false
);
const oldGeometry = ropeMesh.geometry;
ropeMesh.geometry = geometry;
oldGeometry.dispose();
}
// ------------------------------------------------------------
// Mechanism state
// ------------------------------------------------------------
let pullAmount = 0;
let targetPullAmount = 0;
let movingY = START_MOVING_Y;
let freeY = START_FREE_Y;
const ropeValue = document.getElementById("ropeValue");
const loadValue = document.getElementById("loadValue");
function updateMechanism(nextMovingY) {
movingY = THREE.MathUtils.clamp(
nextMovingY,
START_MOVING_Y,
MAX_MOVING_Y
);
// Conservation of rope length:
// shortening both supporting segments extends the free side twice as far.
freeY = START_FREE_Y - 2 * (movingY - START_MOVING_Y);
movingAssembly.position.y = movingY;
handle.position.set(FREE_X, freeY, 0);
alignY(
supportShellA,
new THREE.Vector3(-MOVING_RADIUS, ANCHOR_Y, 0),
new THREE.Vector3(-MOVING_RADIUS, movingY, 0)
);
alignY(
supportShellB,
new THREE.Vector3(MOVING_RADIUS, movingY, 0),
new THREE.Vector3(MOVING_RADIUS, FIXED_Y, 0)
);
tensionArrowA.position.set(
-MOVING_RADIUS,
movingY + 0.18,
0.33
);
tensionArrowB.position.set(
MOVING_RADIUS,
movingY + 0.18,
0.33
);
fixedPulley.rotation.z = -pullAmount * 5.2;
movingPulley.rotation.z = pullAmount * 2.5;
rebuildRope(movingY, freeY);
const loadRise = movingY - START_MOVING_Y;
ropeValue.textContent = `${(loadRise * 2).toFixed(2)} m`;
loadValue.textContent = `${loadRise.toFixed(2)} m`;
}
// ------------------------------------------------------------
// HTML labels projected from 3D positions
// ------------------------------------------------------------
const labels = {
anchor: document.getElementById("tagAnchor"),
fixed: document.getElementById("tagFixed"),
moving: document.getElementById("tagMoving"),
load: document.getElementById("tagLoad"),
free: document.getElementById("tagFree"),
supportA: document.getElementById("tagSupportA"),
supportB: document.getElementById("tagSupportB")
};
function placeLabel(element, position) {
const projected = position.clone().project(camera);
const visible =
projected.z > -1 &&
projected.z < 1 &&
Math.abs(projected.x) < 1.15 &&
Math.abs(projected.y) < 1.15;
element.style.display = visible ? "block" : "none";
if (!visible) return;
const x = (projected.x * 0.5 + 0.5) * window.innerWidth;
const y = (-projected.y * 0.5 + 0.5) * window.innerHeight;
element.style.left = `${x}px`;
element.style.top = `${y}px`;
}
function updateLabels() {
placeLabel(
labels.anchor,
new THREE.Vector3(-MOVING_RADIUS, ANCHOR_Y + 0.28, 0.1)
);
placeLabel(
labels.fixed,
new THREE.Vector3(FIXED_X, FIXED_Y + 0.48, 0.1)
);
placeLabel(
labels.moving,
new THREE.Vector3(0, movingY + 0.86, 0.2)
);
placeLabel(
labels.load,
new THREE.Vector3(0, movingY - 1.58, 0.7)
);
placeLabel(
labels.free,
new THREE.Vector3(FREE_X, freeY - 0.58, 0.2)
);
placeLabel(
labels.supportA,
new THREE.Vector3(
-MOVING_RADIUS,
(ANCHOR_Y + movingY) * 0.5,
0.2
)
);
placeLabel(
labels.supportB,
new THREE.Vector3(
MOVING_RADIUS,
(movingY + FIXED_Y) * 0.5,
0.2
)
);
}
// ------------------------------------------------------------
// Direct camera and handle interaction
// ------------------------------------------------------------
const raycaster = new THREE.Raycaster();
const pointer = new THREE.Vector2();
const dragPlane = new THREE.Plane(new THREE.Vector3(0, 0, 1), 0);
const dragPoint = new THREE.Vector3();
let orbiting = false;
let draggingHandle = false;
let previousPointerX = 0;
let previousPointerY = 0;
function updatePointer(event) {
const rect = renderer.domElement.getBoundingClientRect();
pointer.x = (
(event.clientX - rect.left) / rect.width
) * 2 - 1;
pointer.y = -(
(event.clientY - rect.top) / rect.height
) * 2 + 1;
raycaster.setFromCamera(pointer, camera);
}
renderer.domElement.addEventListener("pointerdown", (event) => {
updatePointer(event);
const hits = raycaster.intersectObject(handlePick, false);
previousPointerX = event.clientX;
previousPointerY = event.clientY;
renderer.domElement.setPointerCapture(event.pointerId);
if (hits.length > 0) {
draggingHandle = true;
renderer.domElement.style.cursor = "grabbing";
} else {
orbiting = true;
renderer.domElement.style.cursor = "grabbing";
}
event.preventDefault();
});
renderer.domElement.addEventListener("pointermove", (event) => {
updatePointer(event);
if (draggingHandle) {
if (raycaster.ray.intersectPlane(dragPlane, dragPoint)) {
const clampedY = THREE.MathUtils.clamp(
dragPoint.y,
START_FREE_Y - 2 * (MAX_MOVING_Y - START_MOVING_Y),
START_FREE_Y
);
const nextMovingY =
START_MOVING_Y - (clampedY - START_FREE_Y) * 0.5;
pullAmount = THREE.MathUtils.clamp(
(nextMovingY - START_MOVING_Y) /
(MAX_MOVING_Y - START_MOVING_Y),
0,
1
);
targetPullAmount = pullAmount;
updateMechanism(nextMovingY);
}
return;
}
if (orbiting) {
const dx = event.clientX - previousPointerX;
const dy = event.clientY - previousPointerY;
orbit.theta -= dx * 0.006;
orbit.phi = THREE.MathUtils.clamp(
orbit.phi + dy * 0.005,
0.55,
1.46
);
previousPointerX = event.clientX;
previousPointerY = event.clientY;
updateCamera();
return;
}
const hoveringHandle =
raycaster.intersectObject(handlePick, false).length > 0;
renderer.domElement.style.cursor = hoveringHandle
? "ns-resize"
: "grab";
});
function endPointerInteraction(event) {
orbiting = false;
draggingHandle = false;
renderer.domElement.style.cursor = "grab";
if (renderer.domElement.hasPointerCapture(event.pointerId)) {
renderer.domElement.releasePointerCapture(event.pointerId);
}
}
renderer.domElement.addEventListener(
"pointerup",
endPointerInteraction
);
renderer.domElement.addEventListener(
"pointercancel",
endPointerInteraction
);
renderer.domElement.addEventListener("wheel", (event) => {
event.preventDefault();
orbit.radius = THREE.MathUtils.clamp(
orbit.radius + event.deltaY * 0.008,
6.8,
17
);
updateCamera();
}, { passive: false });
renderer.domElement.addEventListener("dblclick", resetCamera);
document.getElementById("pull").addEventListener("click", () => {
targetPullAmount = 1;
});
document.getElementById("lower").addEventListener("click", () => {
targetPullAmount = 0;
});
document.getElementById("reset").addEventListener("click", () => {
targetPullAmount = 0;
resetCamera();
});
// ------------------------------------------------------------
// Resize and animation loop
// ------------------------------------------------------------
window.addEventListener("resize", () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
});
const clock = new THREE.Clock();
function animate() {
requestAnimationFrame(animate);
const delta = Math.min(clock.getDelta(), 0.05);
if (!draggingHandle) {
pullAmount +=
(targetPullAmount - pullAmount) *
(1 - Math.exp(-delta * 3.5));
if (Math.abs(targetPullAmount - pullAmount) < 0.0001) {
pullAmount = targetPullAmount;
}
const nextMovingY =
START_MOVING_Y +
pullAmount * (MAX_MOVING_Y - START_MOVING_Y);
updateMechanism(nextMovingY);
}
updateLabels();
renderer.render(scene, camera);
}
updateMechanism(START_MOVING_Y);
animate();
</script>
</body>
</html>
```