Prompt specification
Build or customize the Three.js 3D scene "Held-object distance lever comparison" 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: Held-object distance lever comparison
Category: Biology & Nature · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/held-object-distance-lever-comparison-x1026
Scene Description Prompt:
Make one HTML file using Three.js from cdnjs for this cutaway scene: Show a stylized torso holding equal masses close to and farther from the body; illustrate external moment changes without presenting internal spinal-force estimates.
Working Three.js Starter (held-object-distance-lever-comparison-x1026):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>External Moment Cutaway</title>
<style>
:root {
color-scheme: dark;
--bg: #07111e;
--panel: rgba(10, 22, 37, 0.82);
--line: rgba(176, 211, 240, 0.18);
--text: #eef7ff;
--muted: #9fb4c8;
--near: #4bd7ff;
--far: #ff74ad;
--weight: #f5c85b;
}
* {
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;
}
body {
color: var(--text);
}
#stage,
#labels {
position: fixed;
inset: 0;
}
#stage canvas {
display: block;
width: 100%;
height: 100%;
cursor: grab;
touch-action: none;
}
#stage canvas:active {
cursor: grabbing;
}
#labels {
z-index: 2;
pointer-events: none;
overflow: hidden;
}
.scene-label {
position: absolute;
left: 0;
top: 0;
max-width: 190px;
padding: 7px 10px;
border: 1px solid rgba(255, 255, 255, 0.17);
border-radius: 8px;
background: rgba(5, 13, 24, 0.84);
box-shadow: 0 6px 22px rgba(0, 0, 0, 0.24);
color: #eef7ff;
font-size: 11px;
font-weight: 650;
line-height: 1.25;
letter-spacing: 0.025em;
white-space: nowrap;
transform: translate(-50%, -50%);
backdrop-filter: blur(8px);
}
.scene-label.near {
border-color: rgba(75, 215, 255, 0.46);
color: #bcefff;
}
.scene-label.far {
border-color: rgba(255, 116, 173, 0.48);
color: #ffd0e3;
}
.scene-label.weight {
border-color: rgba(245, 200, 91, 0.48);
color: #ffe7a3;
}
.scene-label.reference {
border-color: rgba(255, 255, 255, 0.2);
color: #c9d6e2;
}
.hud {
position: fixed;
z-index: 4;
pointer-events: none;
}
.hud > * {
pointer-events: auto;
}
.intro {
top: 24px;
left: 24px;
width: min(390px, calc(100vw - 48px));
}
.eyebrow {
margin-bottom: 10px;
color: #7edfff;
font-size: 10px;
font-weight: 800;
letter-spacing: 0.18em;
text-transform: uppercase;
}
h1 {
margin: 0;
font-size: clamp(29px, 4vw, 49px);
font-weight: 760;
line-height: 0.98;
letter-spacing: -0.045em;
text-shadow: 0 3px 22px rgba(0, 0, 0, 0.45);
}
h1 em {
color: #89ddff;
font-style: normal;
}
.intro p {
max-width: 370px;
margin: 14px 0 0;
color: #b9cada;
font-size: 13px;
line-height: 1.55;
}
.formula {
display: inline-flex;
align-items: center;
gap: 10px;
margin-top: 13px;
padding: 8px 11px;
border: 1px solid rgba(126, 223, 255, 0.24);
border-radius: 9px;
background: rgba(7, 17, 30, 0.74);
color: #d9f7ff;
font-size: 13px;
font-weight: 750;
}
.formula small {
color: #91a9bc;
font-size: 10px;
font-weight: 650;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.control-panel {
top: 24px;
right: 24px;
width: 290px;
padding: 17px;
border: 1px solid var(--line);
border-radius: 15px;
background: var(--panel);
box-shadow: 0 18px 60px rgba(0, 0, 0, 0.25);
backdrop-filter: blur(15px);
}
.panel-title {
margin: 0 0 5px;
font-size: 13px;
font-weight: 760;
}
.panel-copy {
margin: 0 0 15px;
color: var(--muted);
font-size: 11px;
line-height: 1.45;
}
.slider-heading {
display: flex;
align-items: center;
justify-content: space-between;
gap: 10px;
margin-bottom: 8px;
color: #dce9f4;
font-size: 11px;
font-weight: 700;
}
#reachValue {
color: var(--far);
font-variant-numeric: tabular-nums;
}
input[type="range"] {
width: 100%;
height: 5px;
margin: 3px 0 7px;
border-radius: 999px;
accent-color: var(--far);
cursor: pointer;
}
.range-ends {
display: flex;
justify-content: space-between;
margin-bottom: 14px;
color: #748ba0;
font-size: 9px;
font-weight: 700;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.check-row {
display: flex;
align-items: center;
gap: 9px;
margin-top: 13px;
color: #c5d5e3;
font-size: 11px;
cursor: pointer;
user-select: none;
}
.check-row input {
accent-color: #69d9ff;
}
.status {
min-height: 54px;
margin-top: 14px;
padding: 10px 11px;
border-left: 2px solid var(--near);
border-radius: 0 8px 8px 0;
background: rgba(75, 215, 255, 0.07);
color: #b9cad8;
font-size: 10px;
line-height: 1.5;
}
.disclaimer {
margin-top: 12px;
color: #7f95a8;
font-size: 9px;
line-height: 1.45;
}
.bottom-bar {
bottom: 22px;
left: 50%;
display: flex;
align-items: center;
gap: 10px;
transform: translateX(-50%);
}
.segmented {
display: flex;
gap: 4px;
padding: 5px;
border: 1px solid var(--line);
border-radius: 12px;
background: rgba(8, 19, 33, 0.88);
box-shadow: 0 12px 38px rgba(0, 0, 0, 0.28);
backdrop-filter: blur(12px);
}
button {
border: 0;
border-radius: 8px;
padding: 9px 13px;
background: transparent;
color: #9fb5c8;
font: inherit;
font-size: 11px;
font-weight: 730;
cursor: pointer;
transition:
color 150ms ease,
background 150ms ease,
transform 150ms ease;
}
button:hover {
color: #fff;
background: rgba(255, 255, 255, 0.07);
}
button:active {
transform: translateY(1px);
}
button.active {
color: #06111e;
background: #8de5ff;
}
.reset-button {
border: 1px solid var(--line);
background: rgba(8, 19, 33, 0.88);
box-shadow: 0 12px 38px rgba(0, 0, 0, 0.28);
backdrop-filter: blur(12px);
}
.legend {
bottom: 25px;
left: 24px;
display: flex;
flex-direction: column;
gap: 6px;
color: #9eb2c4;
font-size: 10px;
}
.legend-item {
display: flex;
align-items: center;
gap: 8px;
}
.legend-swatch {
width: 25px;
height: 2px;
border-radius: 2px;
background: #cceeff;
}
.legend-swatch.dashed {
height: 0;
border-top: 2px dashed #b8ccdc;
background: none;
}
.legend-swatch.arc {
width: 16px;
height: 16px;
border: 2px solid var(--far);
border-right-color: transparent;
border-radius: 50%;
background: none;
}
.help {
right: 24px;
bottom: 25px;
color: #70879b;
font-size: 9px;
text-align: right;
line-height: 1.5;
}
@media (max-width: 900px) {
.intro {
width: 315px;
}
.intro p {
max-width: 300px;
font-size: 12px;
}
.control-panel {
width: 255px;
}
.legend,
.help {
display: none;
}
}
@media (max-width: 650px) {
.intro {
top: 16px;
left: 16px;
width: calc(100vw - 32px);
}
.intro p,
.formula {
display: none;
}
h1 {
font-size: 31px;
}
.control-panel {
top: auto;
right: 14px;
bottom: 78px;
width: 225px;
padding: 12px;
}
.panel-copy,
.range-ends,
.disclaimer {
display: none;
}
.status {
min-height: 0;
margin-top: 10px;
font-size: 9px;
}
.bottom-bar {
bottom: 14px;
}
button {
padding: 8px 10px;
font-size: 10px;
}
.scene-label {
padding: 5px 7px;
font-size: 9px;
}
}
</style>
</head>
<body>
<div id="stage"></div>
<div id="labels"></div>
<header class="hud intro">
<div class="eyebrow">Interactive cutaway · external moments</div>
<h1>Same mass.<br /><em>Different lever arm.</em></h1>
<p>
Two identical masses are held at different distances from the body’s
midline. Gravity acts equally, but the farther mass has a longer
perpendicular lever arm and therefore a larger external moment.
</p>
<div class="formula">
<span>M<sub>external</sub> = r × F</span>
<small>Equal loads · unequal lever arms</small>
</div>
</header>
<aside class="hud control-panel">
<h2 class="panel-title">Change the far-hand reach</h2>
<p class="panel-copy">
Move the farther mass while keeping both mass blocks identical.
</p>
<div class="slider-heading">
<span>Far load</span>
<span id="reachValue">2.5× near reach</span>
</div>
<input
id="reach"
type="range"
min="1.55"
max="3.15"
step="0.01"
value="2.65"
aria-label="Far mass lever arm"
/>
<div class="range-ends">
<span>Closer</span>
<span>Farther</span>
</div>
<label class="check-row">
<input id="vectors" type="checkbox" checked />
Show force, lever-arm, and moment vectors
</label>
<div id="status" class="status" aria-live="polite"></div>
<div class="disclaimer">
This is a qualitative external-load illustration, not a biomechanical
model. It does not estimate internal spinal forces or spinal loading.
</div>
</aside>
<nav class="hud bottom-bar" aria-label="Comparison view">
<div class="segmented">
<button class="active" data-mode="compare">Compare</button>
<button data-mode="near">Near only</button>
<button data-mode="far">Far only</button>
</div>
<button id="reset" class="reset-button">Reset view</button>
</nav>
<div class="hud legend">
<div class="legend-item">
<span class="legend-swatch"></span>
<span>Downward weight vector</span>
</div>
<div class="legend-item">
<span class="legend-swatch dashed"></span>
<span>Perpendicular lever arm r</span>
</div>
<div class="legend-item">
<span class="legend-swatch arc"></span>
<span>External-moment direction and relative size</span>
</div>
</div>
<div class="hud help">
Drag to orbit · Scroll to zoom<br />
Double-click to reset the camera
</div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
const stage = document.getElementById("stage");
const labelLayer = document.getElementById("labels");
const state = {
mode: "compare",
vectors: true,
nearX: 1.05,
farX: 2.65,
nearSweep: 0.95,
farSweep: 2.5
};
const V = (x = 0, y = 0, z = 0) => new THREE.Vector3(x, y, z);
const colors = {
near: 0x4bd7ff,
far: 0xff74ad,
weight: 0xf5c85b,
reference: 0xc9d9e7
};
// ------------------------------------------------------------------
// Renderer, scene, camera, and lighting
// ------------------------------------------------------------------
const renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.12;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
stage.appendChild(renderer.domElement);
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x07111e);
scene.fog = new THREE.Fog(0x07111e, 11, 25);
const camera = new THREE.PerspectiveCamera(
39,
window.innerWidth / window.innerHeight,
0.1,
60
);
const cameraTarget = V(0.95, 2.45, 0);
const cameraState = {
yaw: 0.2,
pitch: 0.13,
radius: 8.4,
desiredYaw: 0.2,
desiredPitch: 0.13,
desiredRadius: 8.4
};
scene.add(new THREE.HemisphereLight(0xa9dcff, 0x07101b, 2.1));
const keyLight = new THREE.DirectionalLight(0xffffff, 3.2);
keyLight.position.set(-3.5, 7.5, 6);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(2048, 2048);
keyLight.shadow.camera.left = -6;
keyLight.shadow.camera.right = 6;
keyLight.shadow.camera.top = 7;
keyLight.shadow.camera.bottom = -3;
scene.add(keyLight);
const rimLight = new THREE.DirectionalLight(0x56d7ff, 2.1);
rimLight.position.set(5, 3, -5);
scene.add(rimLight);
const warmLight = new THREE.PointLight(0xffa8c8, 24, 11, 2);
warmLight.position.set(3.5, 2.4, 2.5);
scene.add(warmLight);
const floor = new THREE.Mesh(
new THREE.PlaneGeometry(24, 24),
new THREE.MeshStandardMaterial({
color: 0x091522,
roughness: 0.95,
metalness: 0.05
})
);
floor.rotation.x = -Math.PI / 2;
floor.position.y = -0.04;
floor.receiveShadow = true;
scene.add(floor);
const grid = new THREE.GridHelper(20, 40, 0x35526b, 0x183047);
grid.position.y = -0.025;
grid.material.transparent = true;
grid.material.opacity = 0.2;
scene.add(grid);
// ------------------------------------------------------------------
// Materials and reusable geometry helpers
// ------------------------------------------------------------------
const shellMaterial = new THREE.MeshStandardMaterial({
color: 0xd99079,
roughness: 0.56,
metalness: 0.02,
transparent: true,
opacity: 0.72,
side: THREE.DoubleSide,
depthWrite: false
});
const cutEdgeMaterial = new THREE.MeshStandardMaterial({
color: 0xffc3aa,
roughness: 0.45,
emissive: 0x32100b,
emissiveIntensity: 0.25
});
const boneMaterial = new THREE.MeshStandardMaterial({
color: 0xf2e3c4,
roughness: 0.62,
metalness: 0.02
});
const diskMaterial = new THREE.MeshStandardMaterial({
color: 0x9ccbd2,
roughness: 0.65
});
const armMaterial = new THREE.MeshStandardMaterial({
color: 0xd89980,
roughness: 0.52,
transparent: true,
opacity: 0.9
});
const jointMaterial = new THREE.MeshStandardMaterial({
color: 0xc5d1dc,
roughness: 0.5
});
const massMaterial = new THREE.MeshStandardMaterial({
color: colors.weight,
roughness: 0.32,
metalness: 0.22
});
const up = V(0, 1, 0);
function setLimb(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(up, direction.normalize());
mesh.scale.set(1, length, 1);
}
function makeLimb(a, b, radius, material) {
const mesh = new THREE.Mesh(
new THREE.CylinderGeometry(radius, radius, 1, 14),
material
);
setLimb(mesh, a, b);
mesh.castShadow = true;
return mesh;
}
function makeTube(points, radius, material) {
const curve = new THREE.CatmullRomCurve3(points);
const mesh = new THREE.Mesh(
new THREE.TubeGeometry(curve, 56, radius, 7, false),
material
);
mesh.castShadow = true;
return mesh;
}
function makeDashedLine(points, color, opacity = 1) {
const geometry = new THREE.BufferGeometry().setFromPoints(points);
const material = new THREE.LineDashedMaterial({
color,
dashSize: 0.09,
gapSize: 0.065,
transparent: true,
opacity,
depthTest: false
});
const line = new THREE.Line(geometry, material);
line.computeLineDistances();
line.renderOrder = 8;
return line;
}
function makeArrow(origin, direction, length, color) {
const arrow = new THREE.ArrowHelper(
direction,
origin,
length,
color,
0.15,
0.075
);
arrow.line.material.depthTest = false;
arrow.cone.material.depthTest = false;
arrow.line.renderOrder = 12;
arrow.cone.renderOrder = 12;
return arrow;
}
// ------------------------------------------------------------------
// Stylized front-cutaway torso
// ------------------------------------------------------------------
const torso = new THREE.Group();
scene.add(torso);
const profile = [
{ y: 1.23, rx: 0.48, rz: 0.30 },
{ y: 1.43, rx: 0.58, rz: 0.36 },
{ y: 1.76, rx: 0.46, rz: 0.30 },
{ y: 2.12, rx: 0.45, rz: 0.31 },
{ y: 2.56, rx: 0.59, rz: 0.38 },
{ y: 3.00, rx: 0.71, rz: 0.42 },
{ y: 3.37, rx: 0.66, rz: 0.37 },
{ y: 3.55, rx: 0.29, rz: 0.24 }
];
function buildCutawayShell() {
const thetaStart = Math.PI / 2 + 0.55;
const thetaEnd = Math.PI / 2 + Math.PI * 2 - 0.55;
const radialSegments = 64;
const vertices = [];
const uvs = [];
const indices = [];
for (let i = 0; i < profile.length; i++) {
const ring = profile[i];
for (let j = 0; j <= radialSegments; j++) {
const t = j / radialSegments;
const theta = THREE.MathUtils.lerp(thetaStart, thetaEnd, t);
vertices.push(
ring.rx * Math.sin(theta),
ring.y,
ring.rz * Math.cos(theta)
);
uvs.push(t, i / (profile.length - 1));
}
}
for (let i = 0; i < profile.length - 1; i++) {
for (let j = 0; j < radialSegments; j++) {
const a = i * (radialSegments + 1) + j;
const b = a + radialSegments + 1;
const c = b + 1;
const d = a + 1;
indices.push(a, b, d, b, c, d);
}
}
const geometry = new THREE.BufferGeometry();
geometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(vertices, 3)
);
geometry.setAttribute("uv", new THREE.Float32BufferAttribute(uvs, 2));
geometry.setIndex(indices);
geometry.computeVertexNormals();
const shell = new THREE.Mesh(geometry, shellMaterial);
shell.castShadow = true;
shell.receiveShadow = true;
shell.renderOrder = 3;
torso.add(shell);
for (const theta of [thetaStart, thetaEnd]) {
const edgePoints = profile.map(
(ring) =>
V(
ring.rx * Math.sin(theta),
ring.y,
ring.rz * Math.cos(theta)
)
);
torso.add(makeTube(edgePoints, 0.025, cutEdgeMaterial));
}
}
buildCutawayShell();
// Simplified visible anatomy: bones only, with no force annotations.
const skeleton = new THREE.Group();
torso.add(skeleton);
for (let i = 0; i < 12; i++) {
const y = 1.46 + i * 0.175;
const vertebra = new THREE.Mesh(
new THREE.SphereGeometry(0.135, 16, 10),
boneMaterial
);
vertebra.scale.set(0.85, 0.52, 0.9);
vertebra.position.set(-0.16, y, 0);
vertebra.castShadow = true;
skeleton.add(vertebra);
if (i < 11) {
const disk = new THREE.Mesh(
new THREE.CylinderGeometry(0.075, 0.075, 0.032, 14),
diskMaterial
);
disk.position.set(-0.16, y + 0.085, 0);
skeleton.add(disk);
}
}
for (let i = 0; i < 5; i++) {
const y = 2.14 + i * 0.185;
for (const side of [-1, 1]) {
const rib = makeTube(
[
V(-0.16, y, 0),
V(-0.36, y + 0.015, side * 0.17),
V(-0.29, y + 0.07, side * 0.35),
V(0.08, y + 0.08, side * 0.40),
V(0.42, y + 0.02, side * 0.29),
V(0.58, y - 0.07, side * 0.07)
],
0.026,
boneMaterial
);
skeleton.add(rib);
}
}
for (const side of [-1, 1]) {
skeleton.add(
makeTube(
[
V(-0.15, 1.43, 0),
V(-0.36, 1.36, side * 0.18),
V(-0.28, 1.20, side * 0.32),
V(0.02, 1.15, side * 0.31),
V(0.29, 1.28, side * 0.17),
V(0.25, 1.44, 0)
],
0.043,
boneMaterial
)
);
skeleton.add(
makeTube(
[
V(-0.09, 3.40, 0),
V(0.02, 3.49, side * 0.20),
V(0.08, 3.44, side * 0.42),
V(-0.06, 3.37, side * 0.48)
],
0.035,
boneMaterial
)
);
}
const neck = new THREE.Mesh(
new THREE.CylinderGeometry(0.15, 0.18, 0.42, 20),
armMaterial
);
neck.position.set(-0.02, 3.70, 0);
neck.castShadow = true;
torso.add(neck);
const head = new THREE.Mesh(
new THREE.SphereGeometry(0.36, 28, 20),
armMaterial
);
head.scale.set(0.9, 1.15, 0.9);
head.position.set(-0.02, 4.10, 0);
head.castShadow = true;
torso.add(head);
// ------------------------------------------------------------------
// Near and far arms and equal-mass blocks
// ------------------------------------------------------------------
const nearScene = new THREE.Group();
const farScene = new THREE.Group();
const nearArm = new THREE.Group();
const farArm = new THREE.Group();
const nearVectors = new THREE.Group();
const farVectors = new THREE.Group();
nearScene.add(nearArm, nearVectors);
farScene.add(farArm, farVectors);
scene.add(nearScene, farScene);
const nearShoulder = V(0.02, 3.34, 0.22);
const nearElbow = V(0.52, 2.65, 0.22);
const nearHand = V(state.nearX, 1.89, 0.22);
const nearUpperArm = makeLimb(
nearShoulder,
nearElbow,
0.115,
armMaterial
);
const nearLowerArm = makeLimb(
nearElbow,
nearHand,
0.09,
armMaterial
);
nearArm.add(nearUpperArm, nearLowerArm);
const nearShoulderJoint = new THREE.Mesh(
new THREE.SphereGeometry(0.135, 18, 12),
jointMaterial
);
nearShoulderJoint.position.copy(nearShoulder);
nearArm.add(nearShoulderJoint);
const nearElbowJoint = new THREE.Mesh(
new THREE.SphereGeometry(0.105, 18, 12),
jointMaterial
);
nearElbowJoint.position.copy(nearElbow);
nearArm.add(nearElbowJoint);
const nearHandJoint = new THREE.Mesh(
new THREE.SphereGeometry(0.09, 18, 12),
jointMaterial
);
nearHandJoint.position.copy(nearHand);
nearArm.add(nearHandJoint);
const farShoulder = V(0.02, 3.34, -0.19);
const farElbow = V(1.35, 2.79, -0.19);
const farHand = V(state.farX, 1.89, -0.19);
const farUpperArm = makeLimb(farShoulder, farElbow, 0.115, armMaterial);
const farLowerArm = makeLimb(farElbow, farHand, 0.09, armMaterial);
farArm.add(farUpperArm, farLowerArm);
const farShoulderJoint = new THREE.Mesh(
new THREE.SphereGeometry(0.135, 18, 12),
jointMaterial
);
farShoulderJoint.position.copy(farShoulder);
farArm.add(farShoulderJoint);
const farElbowJoint = new THREE.Mesh(
new THREE.SphereGeometry(0.105, 18, 12),
jointMaterial
);
farElbowJoint.position.copy(farElbow);
farArm.add(farElbowJoint);
const farHandJoint = new THREE.Mesh(
new THREE.SphereGeometry(0.09, 18, 12),
jointMaterial
);
farHandJoint.position.copy(farHand);
farArm.add(farHandJoint);
function makeMass(z) {
const group = new THREE.Group();
group.position.set(state.nearX, 1.40, z);
const cube = new THREE.Mesh(
new THREE.BoxGeometry(0.58, 0.58, 0.58),
massMaterial
);
cube.castShadow = true;
cube.receiveShadow = true;
group.add(cube);
const edges = new THREE.LineSegments(
new THREE.EdgesGeometry(cube.geometry),
new THREE.LineBasicMaterial({
color: 0xfff4cf,
transparent: true,
opacity: 0.8
})
);
group.add(edges);
const handleMaterial = new THREE.MeshStandardMaterial({
color: 0xe9eef2,
roughness: 0.35,
metalness: 0.55
});
for (const side of [-1, 1]) {
const post = new THREE.Mesh(
new THREE.CylinderGeometry(0.025, 0.025, 0.20, 10),
handleMaterial
);
post.position.set(side * 0.11, 0.38, 0);
group.add(post);
}
const bar = new THREE.Mesh(
new THREE.BoxGeometry(0.27, 0.045, 0.065),
handleMaterial
);
bar.position.y = 0.47;
group.add(bar);
return group;
}
const nearMass = makeMass(0.22);
const farMass = makeMass(-0.19);
nearScene.add(nearMass);
farScene.add(farMass);
const nearForce = makeArrow(
V(state.nearX, 1.40, 0.22),
V(0, -1, 0),
0.76,
0xe8f8ff
);
const farForce = makeArrow(
V(state.farX, 1.40, -0.19),
V(0, -1, 0),
0.76,
0xe8f8ff
);
nearVectors.add(nearForce);
farVectors.add(farForce);
const nearLever = makeDashedLine(
[V(0, 1.40, 0.22), V(state.nearX, 1.40, 0.22)],
colors.near
);
const farLever = makeDashedLine(
[V(0, 1.40, -0.19), V(state.farX, 1.40, -0.19)],
colors.far
);
nearVectors.add(nearLever);
farVectors.add(farLever);
const midline = makeDashedLine(
[V(0, 1.10, 0.70), V(0, 3.48, 0.70)],
colors.reference,
0.48
);
scene.add(midline);
// A small ring identifies the chosen transverse reference axis.
const axisRing = new THREE.Mesh(
new THREE.TorusGeometry(0.105, 0.014, 8, 32),
new THREE.MeshBasicMaterial({
color: 0xd8e7f2,
transparent: true,
opacity: 0.78,
depthTest: false
})
);
axisRing.position.set(0, 1.40, 0.69);
axisRing.renderOrder = 10;
scene.add(axisRing);
// ------------------------------------------------------------------
// External-moment arrows
// The arcs turn clockwise because both loads are on the same side.
// Their lengths indicate the relative external moment magnitude.
// ------------------------------------------------------------------
const momentStart = THREE.MathUtils.degToRad(220);
function buildMomentArc(group, sweep, color, z) {
while (group.children.length) {
const child = group.children.pop();
child.geometry?.dispose();
if (Array.isArray(child.material)) {
child.material.forEach((material) => material.dispose());
} else {
child.material?.dispose();
}
}
group.position.set(0, 1.40, z);
const radius = 0.63;
const points = [];
for (let i = 0; i <= 64; i++) {
const angle = momentStart - (sweep * i) / 64;
points.push(
V(
radius * Math.cos(angle),
radius * Math.sin(angle),
0
)
);
}
const curve = new THREE.CatmullRomCurve3(points);
const material = new THREE.MeshBasicMaterial({
color,
transparent: true,
opacity: 0.9,
depthTest: false
});
const tube = new THREE.Mesh(
new THREE.TubeGeometry(curve, 64, 0.026, 7, false),
material
);
tube.renderOrder = 11;
group.add(tube);
const end = points[points.length - 1];
const tangent = V(
Math.sin(momentStart - sweep),
-Math.cos(momentStart - sweep),
0
).normalize();
const cone = new THREE.Mesh(
new THREE.ConeGeometry(0.085, 0.21, 14),
material
);
cone.position.copy(end);
cone.quaternion.setFromUnitVectors(up, tangent);
cone.renderOrder = 12;
group.add(cone);
}
const nearMoment = new THREE.Group();
const farMoment = new THREE.Group();
nearVectors.add(nearMoment);
farVectors.add(farMoment);
function updateMomentArcs() {
state.farSweep = Math.min(
2.85,
state.nearSweep * (state.farX / state.nearX)
);
buildMomentArc(nearMoment, state.nearSweep, colors.near, 0.56);
buildMomentArc(farMoment, state.farSweep, colors.far, 0.65);
}
updateMomentArcs();
// ------------------------------------------------------------------
// DOM labels projected from 3D positions
// ------------------------------------------------------------------
const labels = [];
function addLabel(html, className, position, offset = [0, 0], visible) {
const element = document.createElement("div");
element.className = `scene-label ${className}`;
element.innerHTML = html;
labelLayer.appendChild(element);
labels.push({
element,
position,
offset,
visible: visible ?? (() => true)
});
}
const showNear = () => state.mode !== "far";
const showFar = () => state.mode !== "near";
const showNearVector = () => state.mode !== "far" && state.vectors;
const showFarVector = () => state.mode !== "near" && state.vectors;
addLabel(
"Equal mass m · near",
"weight",
() => nearMass.position.clone().add(V(0, 0.73, 0)),
[0, -5],
showNear
);
addLabel(
"Equal mass m · far",
"weight",
() => farMass.position.clone().add(V(0, 0.73, 0)),
[0, -5],
showFar
);
addLabel(
"Weight F<sub>g</sub>",
"reference",
() => V(state.nearX, 0.68, 0.22),
[38, 0],
showNearVector
);
addLabel(
"Weight F<sub>g</sub>",
"reference",
() => V(state.farX, 0.68, -0.19),
[38, 0],
showFarVector
);
addLabel(
"short lever arm r",
"near",
() => V(state.nearX * 0.5, 1.52, 0.28),
[0, -8],
showNearVector
);
addLabel(
"long lever arm r",
"far",
() => V(state.farX * 0.5, 1.52, -0.10),
[0, -8],
showFarVector
);
addLabel(
"smaller external moment",
"near",
() => {
const angle = momentStart - state.nearSweep;
return V(
0.72 * Math.cos(angle),
1.40 + 0.72 * Math.sin(angle),
0.58
);
},
[-16, -12],
showNearVector
);
addLabel(
"larger external moment",
"far",
() => {
const angle = momentStart - state.farSweep;
return V(
0.72 * Math.cos(angle),
1.40 + 0.72 * Math.sin(angle),
0.67
);
},
[12, -10],
showFarVector
);
addLabel(
"body midline · reference",
"reference",
() => V(0, 1.63, 0.72),
[-78, 8]
);
addLabel(
"reference axis",
"reference",
() => V(0, 1.40, 0.70),
[-65, 28]
);
addLabel(
"front cutaway",
"reference",
() => V(0.23, 3.19, 0.43),
[55, -4]
);
function updateLabels() {
const width = renderer.domElement.clientWidth;
const height = renderer.domElement.clientHeight;
for (const label of labels) {
if (!label.visible()) {
label.element.style.display = "none";
continue;
}
const projected = label.position().project(camera);
if (
projected.z < -1 ||
projected.z > 1 ||
projected.x < -1.2 ||
projected.x > 1.2 ||
projected.y < -1.2 ||
projected.y > 1.2
) {
label.element.style.display = "none";
continue;
}
const x = (projected.x * 0.5 + 0.5) * width;
const y = (-projected.y * 0.5 + 0.5) * height;
label.element.style.display = "block";
label.element.style.left = `${x + label.offset[0]}px`;
label.element.style.top = `${y + label.offset[1]}px`;
}
}
// ------------------------------------------------------------------
// UI and scene updates
// ------------------------------------------------------------------
const reachInput = document.getElementById("reach");
const reachValue = document.getElementById("reachValue");
const vectorsInput = document.getElementById("vectors");
const status = document.getElementById("status");
function updateStatus() {
const ratio = state.farX / state.nearX;
if (state.mode === "near") {
status.textContent =
"The near mass has a short perpendicular lever arm, so gravity produces the smaller external moment.";
} else if (state.mode === "far") {
status.textContent =
"The far mass has a long perpendicular lever arm, so gravity produces the larger external moment.";
} else {
status.textContent =
`Equal weight blocks are present. The far lever arm is ${ratio.toFixed(1)}× the near lever arm, giving the far load the larger external moment.`;
}
}
function updateFarReach() {
state.farX = Number(reachInput.value);
const t =
(state.farX - Number(reachInput.min)) /
(Number(reachInput.max) - Number(reachInput.min));
farElbow.set(
THREE.MathUtils.lerp(0.85, 1.80, t),
THREE.MathUtils.lerp(2.78, 2.88, t),
-0.19
);
farHand.set(state.farX, 1.89, -0.19);
setLimb(farUpperArm, farShoulder, farElbow);
setLimb(farLowerArm, farElbow, farHand);
farElbowJoint.position.copy(farElbow);
farHandJoint.position.copy(farHand);
farMass.position.x = state.farX;
farForce.position.set(state.farX, 1.40, -0.19);
const farLeverPositions = farLever.geometry.attributes.position;
farLeverPositions.setXYZ(1, state.farX, 1.40, -0.19);
farLeverPositions.needsUpdate = true;
farLever.computeLineDistances();
const ratio = state.farX / state.nearX;
reachValue.textContent = `${ratio.toFixed(1)}× near reach`;
updateMomentArcs();
updateStatus();
}
function applyMode() {
nearScene.visible = state.mode !== "far";
farScene.visible = state.mode !== "near";
nearVectors.visible = state.vectors;
farVectors.visible = state.vectors;
document.querySelectorAll("[data-mode]").forEach((button) => {
button.classList.toggle("active", button.dataset.mode === state.mode);
});
updateStatus();
}
reachInput.addEventListener("input", updateFarReach);
vectorsInput.addEventListener("change", () => {
state.vectors = vectorsInput.checked;
nearVectors.visible = state.vectors;
farVectors.visible = state.vectors;
});
document.querySelectorAll("[data-mode]").forEach((button) => {
button.addEventListener("click", () => {
state.mode = button.dataset.mode;
applyMode();
});
});
// ------------------------------------------------------------------
// Direct camera controls: pointer drag, wheel zoom, and reset
// ------------------------------------------------------------------
function resetCamera() {
cameraState.desiredYaw = 0.2;
cameraState.desiredPitch = 0.13;
cameraState.desiredRadius = 8.4;
}
document.getElementById("reset").addEventListener("click", resetCamera);
renderer.domElement.addEventListener("dblclick", resetCamera);
let dragging = false;
let pointerX = 0;
let pointerY = 0;
renderer.domElement.addEventListener("pointerdown", (event) => {
dragging = true;
pointerX = event.clientX;
pointerY = event.clientY;
renderer.domElement.setPointerCapture(event.pointerId);
});
renderer.domElement.addEventListener("pointermove", (event) => {
if (!dragging) return;
const dx = event.clientX - pointerX;
const dy = event.clientY - pointerY;
pointerX = event.clientX;
pointerY = event.clientY;
cameraState.desiredYaw -= dx * 0.006;
cameraState.desiredPitch = THREE.MathUtils.clamp(
cameraState.desiredPitch + dy * 0.0045,
-0.12,
0.58
);
});
function stopDragging(event) {
dragging = false;
if (
event.pointerId !== undefined &&
renderer.domElement.hasPointerCapture(event.pointerId)
) {
renderer.domElement.releasePointerCapture(event.pointerId);
}
}
renderer.domElement.addEventListener("pointerup", stopDragging);
renderer.domElement.addEventListener("pointercancel", stopDragging);
renderer.domElement.addEventListener(
"wheel",
(event) => {
event.preventDefault();
cameraState.desiredRadius = THREE.MathUtils.clamp(
cameraState.desiredRadius + event.deltaY * 0.006,
5.2,
13.5
);
},
{ passive: false }
);
// ------------------------------------------------------------------
// Resize and render 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));
});
function updateCamera() {
cameraState.yaw +=
(cameraState.desiredYaw - cameraState.yaw) * 0.085;
cameraState.pitch +=
(cameraState.desiredPitch - cameraState.pitch) * 0.085;
cameraState.radius +=
(cameraState.desiredRadius - cameraState.radius) * 0.085;
const horizontal = Math.cos(cameraState.pitch) * cameraState.radius;
camera.position.set(
cameraTarget.x + Math.sin(cameraState.yaw) * horizontal,
cameraTarget.y + Math.sin(cameraState.pitch) * cameraState.radius,
cameraTarget.z + Math.cos(cameraState.yaw) * horizontal
);
camera.lookAt(cameraTarget);
}
updateFarReach();
applyMode();
function animate() {
requestAnimationFrame(animate);
updateCamera();
updateLabels();
renderer.render(scene, camera);
}
animate();
</script>
</body>
</html>
```