Prompt specification
Build or customize the Three.js 3D scene "Suspension lines locating balance point" 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: Suspension lines locating balance point
Category: Science & Physics · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/suspension-lines-locating-balance-point-x1366
Scene Description Prompt:
Please create one self-contained HTML file with Three.js from cdnjs showing a stylized solid center-of-mass demonstration using one flat irregular plate. Make the plate from two connected regions with clearly stated, different mass contributions; present two side-by-side suspension views of that same plate, each hung from a different marked hole; let each plate settle so its calculated center of mass lies directly below the chosen suspension point. Show a thin vertical plumb line in each view, then transfer both lines onto an unrotated reference plate between the panels. Their intersection should coincide with the computed center-of-mass marker, with the original region masses still visible. Beside the reference plate, show the mass contribution of each connected region so the computed intersection can be checked against the chosen distribution.
Working Three.js Starter (suspension-lines-locating-balance-point-x1366):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Two-Region Plate — Center of Mass Demonstration</title>
<style>
:root {
color-scheme: dark;
--bg: #07101e;
--panel: rgba(15, 27, 47, 0.86);
--panel-border: rgba(148, 163, 184, 0.2);
--text: #eef4ff;
--muted: #9fb0c8;
--region-a: #22d3ee;
--region-b: #fb7185;
--com: #fde047;
--grid-line: rgba(148, 163, 184, 0.12);
}
* {
box-sizing: border-box;
}
body {
margin: 0;
min-width: 320px;
min-height: 100vh;
color: var(--text);
font-family:
Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
background:
radial-gradient(circle at 15% 0%, rgba(34, 211, 238, 0.09), transparent 34%),
radial-gradient(circle at 85% 0%, rgba(251, 113, 133, 0.08), transparent 34%),
var(--bg);
}
button,
input {
font: inherit;
}
.app {
width: min(1880px, 100%);
margin: 0 auto;
padding: 26px 22px 30px;
}
.page-header {
display: flex;
align-items: flex-end;
justify-content: space-between;
gap: 24px;
margin-bottom: 20px;
}
.eyebrow {
margin: 0 0 7px;
color: var(--region-a);
font-size: 0.75rem;
font-weight: 800;
letter-spacing: 0.15em;
text-transform: uppercase;
}
h1 {
margin: 0;
font-size: clamp(1.7rem, 3vw, 3rem);
line-height: 1.04;
letter-spacing: -0.04em;
}
.subtitle {
max-width: 830px;
margin: 10px 0 0;
color: var(--muted);
font-size: 0.96rem;
line-height: 1.55;
}
.toolbar {
display: flex;
flex-wrap: wrap;
align-items: center;
justify-content: flex-end;
gap: 10px;
min-width: 260px;
}
.tool-button {
border: 1px solid rgba(34, 211, 238, 0.38);
border-radius: 999px;
padding: 10px 15px;
color: #dffbff;
background: rgba(34, 211, 238, 0.09);
cursor: pointer;
transition:
background 150ms ease,
transform 150ms ease;
}
.tool-button:hover {
background: rgba(34, 211, 238, 0.18);
transform: translateY(-1px);
}
.toggle {
display: inline-flex;
align-items: center;
gap: 8px;
color: var(--muted);
font-size: 0.86rem;
cursor: pointer;
user-select: none;
}
.toggle input {
accent-color: var(--region-a);
}
.stage {
display: grid;
grid-template-columns:
minmax(280px, 0.9fr)
minmax(520px, 1.38fr)
minmax(280px, 0.9fr);
gap: 16px;
align-items: stretch;
}
.panel {
min-width: 0;
overflow: hidden;
border: 1px solid var(--panel-border);
border-radius: 18px;
background: var(--panel);
box-shadow:
0 22px 60px rgba(0, 0, 0, 0.24),
inset 0 1px rgba(255, 255, 255, 0.025);
}
.panel-header {
display: flex;
align-items: center;
justify-content: space-between;
min-height: 72px;
gap: 12px;
padding: 15px 17px;
border-bottom: 1px solid var(--panel-border);
}
.panel-kicker {
margin: 0 0 3px;
color: var(--muted);
font-size: 0.68rem;
font-weight: 800;
letter-spacing: 0.12em;
text-transform: uppercase;
}
.panel-title {
margin: 0;
font-size: 1rem;
line-height: 1.25;
}
.status {
flex: none;
border: 1px solid rgba(251, 113, 133, 0.35);
border-radius: 999px;
padding: 6px 9px;
color: #fecdd3;
background: rgba(251, 113, 133, 0.08);
font-size: 0.64rem;
font-weight: 850;
letter-spacing: 0.1em;
text-transform: uppercase;
}
.status.balanced {
border-color: rgba(74, 222, 128, 0.38);
color: #bbf7d0;
background: rgba(74, 222, 128, 0.08);
}
.canvas-wrap {
position: relative;
height: 525px;
overflow: hidden;
background:
radial-gradient(circle at 50% 43%, rgba(59, 76, 105, 0.24), transparent 60%),
linear-gradient(rgba(148, 163, 184, 0.035) 1px, transparent 1px),
linear-gradient(90deg, rgba(148, 163, 184, 0.035) 1px, transparent 1px);
background-size:
auto,
28px 28px,
28px 28px;
}
canvas {
display: block;
width: 100%;
height: 100%;
outline: none;
}
.gravity {
position: absolute;
top: 13px;
right: 14px;
display: flex;
align-items: center;
gap: 7px;
color: var(--muted);
font-size: 0.72rem;
letter-spacing: 0.08em;
text-transform: uppercase;
pointer-events: none;
}
.gravity-arrow {
color: var(--com);
font-size: 1.1rem;
line-height: 1;
}
.view-note {
min-height: 89px;
padding: 13px 17px 16px;
border-top: 1px solid var(--panel-border);
color: var(--muted);
font-size: 0.78rem;
line-height: 1.5;
}
.view-note strong {
color: var(--text);
}
.reference-body {
display: grid;
grid-template-columns: minmax(0, 1fr) 226px;
min-height: 525px;
}
.reference-body .canvas-wrap {
height: 100%;
min-height: 525px;
}
.mass-panel {
padding: 17px 16px;
border-left: 1px solid var(--panel-border);
background: rgba(5, 13, 26, 0.34);
}
.mass-panel h3 {
margin: 0 0 5px;
font-size: 0.96rem;
}
.mass-intro {
margin: 0 0 15px;
color: var(--muted);
font-size: 0.72rem;
line-height: 1.45;
}
.mass-row {
display: grid;
grid-template-columns: 13px 1fr auto;
align-items: center;
gap: 9px;
padding: 10px 0;
border-top: 1px solid var(--grid-line);
}
.swatch {
width: 11px;
height: 11px;
border-radius: 3px;
background: var(--region-a);
box-shadow: 0 0 13px color-mix(in srgb, var(--region-a) 45%, transparent);
}
.swatch.b {
background: var(--region-b);
box-shadow: 0 0 13px color-mix(in srgb, var(--region-b) 45%, transparent);
}
.mass-name {
font-size: 0.79rem;
font-weight: 750;
}
.mass-centroid {
display: block;
margin-top: 2px;
color: var(--muted);
font-size: 0.65rem;
font-variant-numeric: tabular-nums;
}
.mass-value {
font-size: 0.87rem;
font-weight: 850;
font-variant-numeric: tabular-nums;
}
.percent {
display: block;
color: var(--muted);
font-size: 0.62rem;
text-align: right;
}
.formula {
margin-top: 15px;
padding: 12px;
border: 1px solid var(--grid-line);
border-radius: 11px;
background: rgba(148, 163, 184, 0.045);
font-variant-numeric: tabular-nums;
}
.formula-title {
margin-bottom: 8px;
color: var(--com);
font-size: 0.68rem;
font-weight: 850;
letter-spacing: 0.1em;
text-transform: uppercase;
}
.formula-line {
margin: 4px 0;
color: #d7e3f4;
font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
font-size: 0.68rem;
line-height: 1.4;
}
.intersection {
margin-top: 13px;
padding: 10px 11px;
border-left: 3px solid var(--com);
color: var(--muted);
background: rgba(253, 224, 71, 0.055);
font-size: 0.7rem;
line-height: 1.45;
}
.intersection strong {
color: var(--com);
}
.line-key {
display: grid;
gap: 8px;
margin-top: 16px;
}
.line-key div {
display: flex;
align-items: center;
gap: 8px;
color: var(--muted);
font-size: 0.68rem;
}
.line-sample {
width: 27px;
height: 2px;
background: var(--region-a);
}
.line-sample.b {
background: var(--region-b);
}
.page-footer {
display: flex;
flex-wrap: wrap;
justify-content: space-between;
gap: 10px 24px;
margin-top: 15px;
color: var(--muted);
font-size: 0.72rem;
line-height: 1.5;
}
.legend {
display: flex;
flex-wrap: wrap;
align-items: center;
gap: 14px;
}
.legend-item {
display: inline-flex;
align-items: center;
gap: 7px;
}
.legend-dot {
width: 9px;
height: 9px;
border-radius: 50%;
background: var(--com);
box-shadow: 0 0 10px rgba(253, 224, 71, 0.45);
}
@media (max-width: 1120px) {
.page-header {
align-items: flex-start;
flex-direction: column;
}
.toolbar {
justify-content: flex-start;
}
.stage {
grid-template-columns: 1fr 1fr;
}
.panel.reference {
grid-column: 1 / -1;
grid-row: 1;
}
.panel.reference .reference-body {
grid-template-columns: minmax(0, 1fr) 260px;
}
}
@media (max-width: 720px) {
.app {
padding: 18px 10px 24px;
}
.stage {
grid-template-columns: 1fr;
}
.panel.reference {
grid-column: auto;
grid-row: auto;
}
.reference-body {
grid-template-columns: 1fr;
}
.reference-body .canvas-wrap {
min-height: 430px;
}
.mass-panel {
border-top: 1px solid var(--panel-border);
border-left: 0;
}
.canvas-wrap {
height: 430px;
}
}
</style>
</head>
<body>
<div class="app">
<header class="page-header">
<div>
<p class="eyebrow">Rigid-body center of mass</p>
<h1>Which way does a two-region plate hang?</h1>
<p class="subtitle">
The same irregular plate is released from two different marked holes.
Each view settles with the computed center of mass directly beneath
its support. The two plumb directions are then transferred to the
unrotated plate in the center.
</p>
</div>
<div class="toolbar">
<button class="tool-button" id="replay" type="button">
↻ Replay suspension
</button>
<label class="toggle">
<input id="show-lines" type="checkbox" checked>
Show plumb and transfer lines
</label>
</div>
</header>
<main class="stage">
<section class="panel">
<div class="panel-header">
<div>
<p class="panel-kicker">Suspension view A</p>
<h2 class="panel-title">Hung from the teal-region hole</h2>
</div>
<div class="status" id="status-a">Settling</div>
</div>
<div class="canvas-wrap">
<canvas id="view-a"></canvas>
<div class="gravity">
<span class="gravity-arrow">↓</span>
Gravity
</div>
</div>
<div class="view-note">
<strong>Support A:</strong> the marked hole in region A.
After settling, the gold center-of-mass marker lies on the
vertical teal plumb line.
</div>
</section>
<section class="panel reference">
<div class="panel-header">
<div>
<p class="panel-kicker">Unrotated transfer map</p>
<h2 class="panel-title">Both plumb lines intersect at the computed CM</h2>
</div>
</div>
<div class="reference-body">
<div class="canvas-wrap">
<canvas id="reference"></canvas>
</div>
<aside class="mass-panel">
<h3>Mass contribution</h3>
<p class="mass-intro">
Each assigned region total acts through that region’s
geometric area centroid.
</p>
<div class="mass-row">
<span class="swatch"></span>
<div>
<div class="mass-name">Region A · teal</div>
<span class="mass-centroid" id="centroid-a"></span>
</div>
<div>
<div class="mass-value">3.0 kg</div>
<span class="percent">75%</span>
</div>
</div>
<div class="mass-row">
<span class="swatch b"></span>
<div>
<div class="mass-name">Region B · rose</div>
<span class="mass-centroid" id="centroid-b"></span>
</div>
<div>
<div class="mass-value">1.0 kg</div>
<span class="percent">25%</span>
</div>
</div>
<div class="formula">
<div class="formula-title">Weighted calculation</div>
<div class="formula-line">M = 3.0 + 1.0 = 4.0 kg</div>
<div class="formula-line" id="formula-x"></div>
<div class="formula-line" id="formula-y"></div>
</div>
<div class="intersection">
<strong>Construction check:</strong><br>
Transferred line A and transferred line B both pass through
the same calculated point. Their intersection is the gold
center-of-mass marker.
</div>
<div class="line-key">
<div><span class="line-sample"></span> Plumb / transfer line A</div>
<div><span class="line-sample b"></span> Plumb / transfer line B</div>
</div>
</aside>
</div>
</section>
<section class="panel">
<div class="panel-header">
<div>
<p class="panel-kicker">Suspension view B</p>
<h2 class="panel-title">Hung from the rose-region hole</h2>
</div>
<div class="status" id="status-b">Settling</div>
</div>
<div class="canvas-wrap">
<canvas id="view-b"></canvas>
<div class="gravity">
<span class="gravity-arrow">↓</span>
Gravity
</div>
</div>
<div class="view-note">
<strong>Support B:</strong> the marked hole in region B.
The plate rotates differently, but the same center of mass must
again finish directly below the support.
</div>
</section>
</main>
<footer class="page-footer">
<div class="legend">
<span class="legend-item"><span class="legend-dot"></span> Computed center of mass</span>
<span>Mass model: assigned totals at each connected region’s area centroid.</span>
</div>
<div>The plate is rigid; only its orientation changes between the two suspension views.</div>
</footer>
</div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// -------------------------------------------------------------------------
// Plate geometry: two connected, non-overlapping regions sharing a boundary.
// Coordinates are in the plate's unrotated local XY plane.
// -------------------------------------------------------------------------
const regionDefinitions = [
{
id: "A",
name: "Teal region",
mass: 3.0,
fill: 0x0f766e,
accent: 0x22d3ee,
points: [
[-2.35, -1.15],
[-1.75, -1.75],
[-0.85, -1.65],
[-0.38, -1.48],
[-0.25, -0.52],
[-0.42, 0.45],
[-0.62, 1.25],
[-1.20, 1.62],
[-1.92, 1.18],
[-2.42, 0.32]
],
hole: [-1.58, -1.27]
},
{
id: "B",
name: "Rose region",
mass: 1.0,
fill: 0x9f3f55,
accent: 0xfb7185,
points: [
[-0.38, -1.48],
[-0.25, -0.52],
[-0.42, 0.45],
[-0.62, 1.25],
[-0.10, 1.78],
[ 0.72, 1.52],
[ 1.02, 0.72],
[ 1.78, 0.62],
[ 2.28, 0.02],
[ 2.12, -0.78],
[ 1.38, -1.40],
[ 0.45, -1.63]
],
hole: [1.55, 0.24]
}
];
function signedArea(points) {
let area = 0;
for (let i = 0; i < points.length; i++) {
const a = points[i];
const b = points[(i + 1) % points.length];
area += a[0] * b[1] - b[0] * a[1];
}
return area * 0.5;
}
function polygonCentroid(points) {
let twiceArea = 0;
let xAccumulator = 0;
let yAccumulator = 0;
for (let i = 0; i < points.length; i++) {
const a = points[i];
const b = points[(i + 1) % points.length];
const cross = a[0] * b[1] - b[0] * a[1];
twiceArea += cross;
xAccumulator += (a[0] + b[0]) * cross;
yAccumulator += (a[1] + b[1]) * cross;
}
const area = twiceArea * 0.5;
return new THREE.Vector2(
xAccumulator / (6 * area),
yAccumulator / (6 * area)
);
}
function counterClockwise(points) {
const copy = points.map(p => [...p]);
return signedArea(copy) < 0 ? copy.reverse() : copy;
}
for (const region of regionDefinitions) {
region.points = counterClockwise(region.points);
region.centroid = polygonCentroid(region.points);
region.holeVector = new THREE.Vector2(...region.hole);
}
const totalMass = regionDefinitions.reduce((sum, region) => sum + region.mass, 0);
const centerOfMass = new THREE.Vector2(
regionDefinitions.reduce(
(sum, region) => sum + region.mass * region.centroid.x,
0
) / totalMass,
regionDefinitions.reduce(
(sum, region) => sum + region.mass * region.centroid.y,
0
) / totalMass
);
// Populate the calculation panel.
document.getElementById("centroid-a").textContent =
`centroid (${regionDefinitions[0].centroid.x.toFixed(3)}, ` +
`${regionDefinitions[0].centroid.y.toFixed(3)})`;
document.getElementById("centroid-b").textContent =
`centroid (${regionDefinitions[1].centroid.x.toFixed(3)}, ` +
`${regionDefinitions[1].centroid.y.toFixed(3)})`;
document.getElementById("formula-x").textContent =
`xCM = (3xA + 1xB) / 4 = ${centerOfMass.x.toFixed(3)}`;
document.getElementById("formula-y").textContent =
`yCM = (3yA + 1yB) / 4 = ${centerOfMass.y.toFixed(3)}`;
// -------------------------------------------------------------------------
// General helpers.
// -------------------------------------------------------------------------
function createRenderer(canvas) {
const renderer = new THREE.WebGLRenderer({
canvas,
antialias: true,
alpha: true
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio || 1, 2));
renderer.setClearColor(0x000000, 0);
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.18;
return renderer;
}
function createScene() {
const scene = new THREE.Scene();
scene.add(new THREE.AmbientLight(0xd9e8ff, 1.75));
const key = new THREE.DirectionalLight(0xffffff, 2.3);
key.position.set(3.5, 5, 7);
scene.add(key);
const rim = new THREE.DirectionalLight(0x67e8f9, 0.8);
rim.position.set(-4, 1, 4);
scene.add(rim);
return scene;
}
function createCamera() {
const camera = new THREE.OrthographicCamera(-3, 3, 3, -3, 0.1, 100);
camera.position.set(0, 0, 10);
camera.lookAt(0, 0, 0);
return camera;
}
function resizeCamera(renderer, camera, canvas, minimumHalfWidth, minimumHalfHeight) {
const width = Math.max(1, canvas.clientWidth);
const height = Math.max(1, canvas.clientHeight);
const aspect = width / height;
renderer.setSize(width, height, false);
const halfHeight = Math.max(minimumHalfHeight, minimumHalfWidth / aspect);
camera.left = -halfHeight * aspect;
camera.right = halfHeight * aspect;
camera.top = halfHeight;
camera.bottom = -halfHeight;
camera.updateProjectionMatrix();
}
function roundedRect(ctx, x, y, width, height, radius) {
const r = Math.min(radius, width * 0.5, height * 0.5);
ctx.beginPath();
ctx.moveTo(x + r, y);
ctx.arcTo(x + width, y, x + width, y + height, r);
ctx.arcTo(x + width, y + height, x, y + height, r);
ctx.arcTo(x, y + height, x, y, r);
ctx.arcTo(x, y, x + width, y, r);
ctx.closePath();
}
function makeTextSprite(text, accent, width = 520, height = 112) {
const labelCanvas = document.createElement("canvas");
labelCanvas.width = width;
labelCanvas.height = height;
const ctx = labelCanvas.getContext("2d");
const cssAccent = `#${new THREE.Color(accent).getHexString()}`;
ctx.clearRect(0, 0, width, height);
roundedRect(ctx, 4, 4, width - 8, height - 8, 24);
ctx.fillStyle = "rgba(5, 13, 26, 0.88)";
ctx.fill();
ctx.lineWidth = 4;
ctx.strokeStyle = cssAccent;
ctx.stroke();
ctx.fillStyle = "#f8fafc";
ctx.font = "700 42px system-ui, sans-serif";
ctx.textAlign = "center";
ctx.textBaseline = "middle";
ctx.fillText(text, width * 0.5, height * 0.52);
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(1.42, 1.42 * height / width, 1);
sprite.renderOrder = 20;
return sprite;
}
function makeHoleBadge(text, accent) {
const badge = makeTextSprite(text, accent, 160, 112);
badge.scale.set(0.40, 0.40 * 112 / 160, 1);
return badge;
}
function shapeFromPolygon(points, hole) {
const shape = new THREE.Shape();
shape.moveTo(points[0][0], points[0][1]);
for (let i = 1; i < points.length; i++) {
shape.lineTo(points[i][0], points[i][1]);
}
shape.closePath();
if (hole) {
const path = new THREE.Path();
path.absarc(hole[0], hole[1], 0.135, 0, Math.PI * 2, true);
shape.holes.push(path);
}
return shape;
}
// -------------------------------------------------------------------------
// Build the same physical plate for each view.
// -------------------------------------------------------------------------
function createPlate() {
const group = new THREE.Group();
for (const region of regionDefinitions) {
const shape = shapeFromPolygon(region.points, region.hole);
const geometry = new THREE.ExtrudeGeometry(shape, {
depth: 0.13,
steps: 1,
curveSegments: 28,
bevelEnabled: true,
bevelThickness: 0.025,
bevelSize: 0.022,
bevelSegments: 2
});
geometry.computeVertexNormals();
const faceMaterial = new THREE.MeshStandardMaterial({
color: region.fill,
roughness: 0.63,
metalness: 0.12
});
const sideMaterial = new THREE.MeshStandardMaterial({
color: new THREE.Color(region.fill).multiplyScalar(0.55),
roughness: 0.78,
metalness: 0.08
});
const mesh = new THREE.Mesh(geometry, [faceMaterial, sideMaterial]);
mesh.castShadow = false;
group.add(mesh);
const outlinePoints = region.points.map(
point => new THREE.Vector3(point[0], point[1], 0.185)
);
const outline = new THREE.LineLoop(
new THREE.BufferGeometry().setFromPoints(outlinePoints),
new THREE.LineBasicMaterial({
color: region.accent,
transparent: true,
opacity: 0.78
})
);
outline.renderOrder = 5;
group.add(outline);
// Colored ring identifies the actual support hole.
const holeRing = new THREE.Mesh(
new THREE.TorusGeometry(0.17, 0.027, 8, 36),
new THREE.MeshBasicMaterial({ color: region.accent })
);
holeRing.position.set(region.hole[0], region.hole[1], 0.205);
holeRing.renderOrder = 12;
group.add(holeRing);
const holeBadge = makeHoleBadge(region.id, region.accent);
holeBadge.position.set(
region.hole[0] + (region.id === "A" ? -0.25 : 0.27),
region.hole[1] + (region.id === "A" ? -0.26 : 0.28),
0.40
);
group.add(holeBadge);
// A small marker at each region's geometric centroid.
const centroidDot = new THREE.Mesh(
new THREE.CircleGeometry(0.045, 24),
new THREE.MeshBasicMaterial({ color: 0xf8fafc })
);
centroidDot.position.set(
region.centroid.x,
region.centroid.y,
0.235
);
centroidDot.renderOrder = 14;
group.add(centroidDot);
// Mass remains visible on the original regions in every view.
const label = makeTextSprite(
`${region.id} · ${region.mass.toFixed(1)} kg`,
region.accent
);
const labelOffset = region.id === "A"
? new THREE.Vector2(-0.12, 0.36)
: new THREE.Vector2(0.20, -0.34);
label.position.set(
region.centroid.x + labelOffset.x,
region.centroid.y + labelOffset.y,
0.43
);
group.add(label);
}
// Computed center-of-mass marker.
const comMaterial = new THREE.MeshStandardMaterial({
color: 0xfde047,
emissive: 0x9a7800,
emissiveIntensity: 1.25,
roughness: 0.28,
metalness: 0.18
});
const comMarker = new THREE.Mesh(
new THREE.SphereGeometry(0.105, 24, 16),
comMaterial
);
comMarker.position.set(centerOfMass.x, centerOfMass.y, 0.30);
comMarker.renderOrder = 18;
group.add(comMarker);
const comHalo = new THREE.Mesh(
new THREE.TorusGeometry(0.205, 0.018, 8, 40),
new THREE.MeshBasicMaterial({
color: 0xfde047,
transparent: true,
opacity: 0.88,
depthTest: false
})
);
comHalo.position.set(centerOfMass.x, centerOfMass.y, 0.34);
comHalo.renderOrder = 19;
group.add(comHalo);
return { group, comHalo };
}
// -------------------------------------------------------------------------
// A physical suspension view.
//
// The plate is rotated about the selected local support point. The target
// angle maps the vector (CM - support) directly onto world -Y. The group's
// translation compensates for the support point so that it remains at the
// fixed world support location.
// -------------------------------------------------------------------------
function createHangingView(canvas, regionIndex, statusElement, delay, offset) {
const renderer = createRenderer(canvas);
const scene = createScene();
const camera = createCamera();
const region = regionDefinitions[regionIndex];
const supportLocal = region.holeVector;
const supportWorld = new THREE.Vector2(0, 1.62);
const plate = createPlate();
scene.add(plate.group);
const comVector = centerOfMass.clone().sub(supportLocal);
const targetAngle = -Math.PI / 2 - Math.atan2(comVector.y, comVector.x);
const initialAngle = targetAngle + offset;
// Fixed suspension pin.
const pin = new THREE.Mesh(
new THREE.TorusGeometry(0.18, 0.035, 8, 40),
new THREE.MeshBasicMaterial({ color: 0xf8fafc })
);
pin.position.set(supportWorld.x, supportWorld.y, 0.39);
pin.renderOrder = 30;
scene.add(pin);
const stem = new THREE.Line(
new THREE.BufferGeometry().setFromPoints([
new THREE.Vector3(supportWorld.x, supportWorld.y + 0.12, 0.39),
new THREE.Vector3(supportWorld.x, supportWorld.y + 0.46, 0.39)
]),
new THREE.LineBasicMaterial({ color: 0xcbd5e1 })
);
scene.add(stem);
// The world-space plumb line is always vertical.
const plumb = new THREE.Line(
new THREE.BufferGeometry().setFromPoints([
new THREE.Vector3(supportWorld.x, supportWorld.y - 0.03, 0.42),
new THREE.Vector3(supportWorld.x, supportWorld.y - 3.25, 0.42)
]),
new THREE.LineDashedMaterial({
color: region.accent,
dashSize: 0.085,
gapSize: 0.055,
transparent: true,
opacity: 0.92,
depthTest: false
})
);
plumb.computeLineDistances();
plumb.renderOrder = 25;
scene.add(plumb);
let animationStart = performance.now();
let hasSettled = false;
function reset() {
animationStart = performance.now();
hasSettled = false;
statusElement.textContent = "Settling";
statusElement.classList.remove("balanced");
}
function update(now) {
const elapsed = Math.max(0, now - animationStart - delay);
const progress = Math.min(1, elapsed / 1900);
let angle;
if (progress < 1) {
const angularError = Math.atan2(
Math.sin(initialAngle - targetAngle),
Math.cos(initialAngle - targetAngle)
);
// Damped oscillation gives the appearance of settling under gravity.
angle =
targetAngle +
angularError * Math.exp(-4.0 * progress) * Math.cos(9.0 * progress);
} else {
angle = targetAngle;
if (!hasSettled) {
hasSettled = true;
statusElement.textContent = "Balanced";
statusElement.classList.add("balanced");
}
}
plate.group.rotation.z = angle;
const cosine = Math.cos(angle);
const sine = Math.sin(angle);
const rotatedSupportX =
cosine * supportLocal.x - sine * supportLocal.y;
const rotatedSupportY =
sine * supportLocal.x + cosine * supportLocal.y;
plate.group.position.set(
supportWorld.x - rotatedSupportX,
supportWorld.y - rotatedSupportY,
0
);
const pulse = 1 + 0.07 * Math.sin(now * 0.004);
plate.comHalo.scale.setScalar(pulse);
}
function setLinesVisible(visible) {
plumb.visible = visible;
}
function resize() {
resizeCamera(
renderer,
camera,
canvas,
3.05, 2.48
);
}
resize();
return {
renderer,
scene,
camera,
update,
reset,
setLinesVisible,
resize
};
}
// -------------------------------------------------------------------------
// Unrotated reference plate with both transferred plumb lines.
// -------------------------------------------------------------------------
function makeTransferLine(start, intersection, accent) {
const direction = intersection.clone().sub(start).normalize();
const lineStart = start.clone().addScaledVector(direction, -0.22);
const lineEnd = intersection.clone().addScaledVector(direction, 0.92);
const points = [];
const subdivisions = 80;
for (let i = 0; i <= subdivisions; i++) {
const point = lineStart.clone().lerp(lineEnd, i / subdivisions);
points.push(new THREE.Vector3(point.x, point.y, 0.35));
}
const line = new THREE.Line(
new THREE.BufferGeometry().setFromPoints(points),
new THREE.LineBasicMaterial({
color: accent,
transparent: true,
opacity: 0.96,
depthTest: false
})
);
line.renderOrder = 16;
return line;
}
function createReferenceView(canvas) {
const renderer = createRenderer(canvas);
const scene = createScene();
const camera = createCamera();
const plate = createPlate();
// Translation only: the reference plate remains unrotated.
plate.group.position.set(-centerOfMass.x, -centerOfMass.y, 0);
scene.add(plate.group);
const transferLines = [];
for (const region of regionDefinitions) {
const line = makeTransferLine(
region.holeVector,
centerOfMass,
region.accent
);
transferLines.push(line);
plate.group.add(line);
}
function setLinesVisible(visible) {
for (const line of transferLines) {
line.visible = visible;
}
}
function resize() {
resizeCamera(
renderer,
camera,
canvas,
2.95, 2.25
);
}
function update(now) {
const pulse = 1 + 0.07 * Math.sin(now * 0.004);
plate.comHalo.scale.setScalar(pulse);
}
resize();
return {
renderer,
scene,
camera,
update,
setLinesVisible,
resize
};
}
// -------------------------------------------------------------------------
// Instantiate the three views.
// -------------------------------------------------------------------------
const viewA = createHangingView(
document.getElementById("view-a"),
0,
document.getElementById("status-a"),
120, 1.10
);
const reference = createReferenceView(
document.getElementById("reference")
);
const viewB = createHangingView(
document.getElementById("view-b"),
1,
document.getElementById("status-b"),
620, -0.86
);
const views = [viewA, reference, viewB];
document.getElementById("replay").addEventListener("click", () => {
viewA.reset();
viewB.reset();
});
document.getElementById("show-lines").addEventListener("change", event => {
const visible = event.target.checked;
viewA.setLinesVisible(visible);
viewB.setLinesVisible(visible);
reference.setLinesVisible(visible);
});
const resizeObserver = new ResizeObserver(() => {
for (const view of views) {
view.resize();
}
});
resizeObserver.observe(document.getElementById("view-a"));
resizeObserver.observe(document.getElementById("reference"));
resizeObserver.observe(document.getElementById("view-b"));
function animate(now) {
for (const view of views) {
view.update(now);
view.renderer.render(view.scene, view.camera);
}
requestAnimationFrame(animate);
}
requestAnimationFrame(animate);
</script>
</body>
</html>
```