Prompt specification
Build or customize the Three.js 3D scene "Balance beam distance markers" 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: Balance beam distance markers
Category: Games & Play · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/balance-beam-distance-markers-x0321
Scene Description Prompt:
Please create one self-contained HTML file with Three.js from cdnjs showing a balance scale. Build a toy balance with central pivot, evenly spaced hooks, and identical hanging blocks; keep beam distances readable beside each possible attachment point. Stylized solid forms.
Working Three.js Starter (balance-beam-distance-markers-x0321):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Balance Scale Toy</title>
<style>
:root {
color-scheme: light;
font-family: Inter, ui-rounded, "SF Pro Rounded", system-ui, -apple-system,
BlinkMacSystemFont, "Segoe UI", sans-serif;
--ink: #243f47;
--cream: #fff9ed;
--coral: #e66c5c;
--teal: #36a99f;
--gold: #f2b63f;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #f4eee3;
}
body.dragging {
cursor: grabbing;
}
#stage {
position: fixed;
inset: 0;
}
canvas {
display: block;
width: 100%;
height: 100%;
outline: none;
touch-action: none;
cursor: grab;
}
.overlay {
position: fixed;
inset: 0;
z-index: 2;
pointer-events: none;
}
.title-card {
position: absolute;
top: clamp(18px, 3vw, 34px);
left: clamp(18px, 3vw, 38px);
width: min(310px, calc(100vw - 36px));
padding: 18px 20px 17px;
border: 1px solid rgba(36, 63, 71, 0.12);
border-radius: 20px;
color: var(--ink);
background: rgba(255, 249, 237, 0.88);
box-shadow: 0 15px 45px rgba(61, 68, 62, 0.1);
backdrop-filter: blur(12px);
}
.eyebrow {
margin-bottom: 7px;
color: var(--coral);
font-size: 10px;
font-weight: 850;
letter-spacing: 0.18em;
text-transform: uppercase;
}
h1 {
margin: 0;
font-size: clamp(25px, 3vw, 35px);
line-height: 1;
letter-spacing: -0.045em;
}
.subtitle {
margin: 10px 0 0;
max-width: 250px;
color: rgba(36, 63, 71, 0.72);
font-size: 12px;
line-height: 1.55;
}
.scale-key {
display: flex;
align-items: center;
gap: 9px;
margin-top: 14px;
padding-top: 13px;
border-top: 1px solid rgba(36, 63, 71, 0.12);
color: rgba(36, 63, 71, 0.72);
font-size: 11px;
font-weight: 700;
}
.scale-key b {
display: grid;
width: 27px;
height: 27px;
place-items: center;
flex: 0 0 auto;
border-radius: 8px;
color: var(--cream);
background: var(--ink);
font-size: 13px;
}
.status {
position: absolute;
top: clamp(18px, 3vw, 34px);
right: clamp(18px, 3vw, 38px);
display: flex;
align-items: center;
gap: 9px;
padding: 11px 15px;
border: 1px solid rgba(36, 63, 71, 0.11);
border-radius: 999px;
color: var(--ink);
background: rgba(255, 249, 237, 0.86);
box-shadow: 0 10px 30px rgba(61, 68, 62, 0.08);
backdrop-filter: blur(10px);
font-size: 11px;
font-weight: 850;
letter-spacing: 0.12em;
}
.status-dot {
width: 9px;
height: 9px;
border-radius: 50%;
background: var(--teal);
box-shadow: 0 0 0 4px rgba(54, 169, 159, 0.14);
}
.help {
position: absolute;
right: clamp(18px, 3vw, 38px);
bottom: clamp(18px, 3vw, 30px);
padding: 9px 13px;
border-radius: 999px;
color: rgba(36, 63, 71, 0.68);
background: rgba(255, 249, 237, 0.75);
font-size: 10px;
font-weight: 750;
letter-spacing: 0.035em;
backdrop-filter: blur(8px);
}
#loading {
position: fixed;
inset: 0;
z-index: 5;
display: grid;
place-items: center;
color: var(--ink);
background: #f4eee3;
font-size: 12px;
font-weight: 800;
letter-spacing: 0.12em;
text-transform: uppercase;
transition: opacity 300ms ease;
}
#loading.hidden {
opacity: 0;
pointer-events: none;
}
@media (max-width: 650px) {
.title-card {
padding: 14px 16px;
border-radius: 16px;
}
.subtitle,
.scale-key {
display: none;
}
.status {
padding: 9px 12px;
font-size: 9px;
}
.help {
left: 18px;
right: auto;
}
}
</style>
</head>
<body>
<main id="stage"></main>
<div class="overlay">
<section class="title-card" aria-label="Scene information">
<div class="eyebrow">Mechanics / 01</div>
<h1>Balanced</h1>
<p class="subtitle">
Six identical hanging blocks placed at even intervals on either side
of the central pivot.
</p>
<div class="scale-key">
<b>u</b>
<span>Each beam mark is one unit from the pivot</span>
</div>
</section>
<div class="status" aria-label="Balance status">
<span class="status-dot"></span>
LEVEL
</div>
<div class="help">Drag to orbit · Scroll to zoom</div>
</div>
<div id="loading">Building the balance…</div>
<noscript>This scene requires JavaScript and WebGL.</noscript>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
const host = document.getElementById("stage");
const loading = document.getElementById("loading");
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xf4eee3);
scene.fog = new THREE.Fog(0xf4eee3, 24, 42);
const renderer = new THREE.WebGLRenderer({
antialias: true,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.08;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.domElement.setAttribute("role", "img");
renderer.domElement.setAttribute(
"aria-label",
"A colorful toy balance scale with a central pivot, six evenly spaced hooks, identical turquoise blocks, and readable distance labels."
);
renderer.domElement.tabIndex = 0;
host.appendChild(renderer.domElement);
const camera = new THREE.PerspectiveCamera(34, 1, 0.1, 100);
const cameraTarget = new THREE.Vector3(0, 2.25, 0);
let azimuth = 0.24;
let polar = 1.22;
let zoomFactor = 1;
let fitDistance = 15.5;
function calculateFitDistance() {
const verticalFov = THREE.MathUtils.degToRad(camera.fov);
const horizontalFov =
2 * Math.atan(Math.tan(verticalFov / 2) * camera.aspect);
return Math.max(14.5, 5.55 / Math.tan(horizontalFov / 2));
}
function updateCamera() {
const radius = fitDistance * zoomFactor;
const sinPolar = Math.sin(polar);
camera.position.set(
cameraTarget.x + radius * sinPolar * Math.sin(azimuth),
cameraTarget.y + radius * Math.cos(polar),
cameraTarget.z + radius * sinPolar * Math.cos(azimuth)
);
camera.lookAt(cameraTarget);
}
const hemisphere = new THREE.HemisphereLight(0xfff9e9, 0x77878a, 2.15);
scene.add(hemisphere);
const keyLight = new THREE.DirectionalLight(0xfff1d7, 3.1);
keyLight.position.set(-6, 11, 8);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(2048, 2048);
keyLight.shadow.camera.left = -9;
keyLight.shadow.camera.right = 9;
keyLight.shadow.camera.top = 9;
keyLight.shadow.camera.bottom = -4;
keyLight.shadow.camera.near = 1;
keyLight.shadow.camera.far = 30;
keyLight.shadow.normalBias = 0.025;
scene.add(keyLight);
const fillLight = new THREE.DirectionalLight(0xc9e8ee, 1.35);
fillLight.position.set(9, 6, -7);
scene.add(fillLight);
const materials = {
base: new THREE.MeshStandardMaterial({
color: 0xe66c5c,
roughness: 0.72,
metalness: 0
}),
cream: new THREE.MeshStandardMaterial({
color: 0xfff5df,
roughness: 0.78,
metalness: 0
}),
ink: new THREE.MeshStandardMaterial({
color: 0x243f47,
roughness: 0.66,
metalness: 0.04
}),
beam: new THREE.MeshStandardMaterial({
color: 0xf2b63f,
roughness: 0.6,
metalness: 0.02
}),
brass: new THREE.MeshStandardMaterial({
color: 0xc98732,
roughness: 0.38,
metalness: 0.42
}),
block: new THREE.MeshStandardMaterial({
color: 0x36a99f,
roughness: 0.52,
metalness: 0.02
}),
string: new THREE.MeshStandardMaterial({
color: 0x43565a,
roughness: 0.9,
metalness: 0
}),
ground: new THREE.MeshStandardMaterial({
color: 0xf4eee3,
roughness: 1,
metalness: 0
}),
platform: new THREE.MeshStandardMaterial({
color: 0xe8ddca,
roughness: 0.94,
metalness: 0
})
};
function roundedBoxGeometry(width, height, depth, radius = 0.12) {
const bevel = Math.min(
0.06,
depth * 0.12,
radius * 0.35,
width * 0.05,
height * 0.05
);
const shapeWidth = width - bevel * 2;
const shapeHeight = height - bevel * 2;
const shapeRadius = Math.max(0.025, radius - bevel);
const x = -shapeWidth / 2;
const y = -shapeHeight / 2;
const shape = new THREE.Shape();
shape.moveTo(x + shapeRadius, y);
shape.lineTo(x + shapeWidth - shapeRadius, y);
shape.quadraticCurveTo(
x + shapeWidth, y,
x + shapeWidth, y + shapeRadius
);
shape.lineTo(x + shapeWidth, y + shapeHeight - shapeRadius);
shape.quadraticCurveTo(
x + shapeWidth, y + shapeHeight,
x + shapeWidth - shapeRadius, y + shapeHeight
);
shape.lineTo(x + shapeRadius, y + shapeHeight);
shape.quadraticCurveTo(
x, y + shapeHeight,
x, y + shapeHeight - shapeRadius
);
shape.lineTo(x, y + shapeRadius);
shape.quadraticCurveTo(x, y, x + shapeRadius, y);
const geometry = new THREE.ExtrudeGeometry(shape, {
depth: Math.max(0.01, depth - bevel * 2),
steps: 1,
bevelEnabled: true,
bevelThickness: bevel,
bevelSize: bevel,
bevelSegments: 4,
curveSegments: 10
});
geometry.center();
geometry.computeVertexNormals();
return geometry;
}
function addMesh(geometry, material, position, castShadow = true) {
const mesh = new THREE.Mesh(geometry, material);
mesh.position.copy(position);
mesh.castShadow = castShadow;
mesh.receiveShadow = true;
scene.add(mesh);
return mesh;
}
function cylinderBetween(start, end, radius, material, radialSegments = 12) {
const direction = new THREE.Vector3().subVectors(end, start);
const length = direction.length();
const mesh = new THREE.Mesh(
new THREE.CylinderGeometry(
radius,
radius,
length,
radialSegments
),
material
);
mesh.position.copy(start).add(end).multiplyScalar(0.5);
mesh.quaternion.setFromUnitVectors(
new THREE.Vector3(0, 1, 0),
direction.normalize()
);
mesh.castShadow = true;
scene.add(mesh);
return mesh;
}
function roundedRectanglePath(context, x, y, width, height, radius) {
context.beginPath();
context.moveTo(x + radius, y);
context.lineTo(x + width - radius, y);
context.quadraticCurveTo(
x + width, y,
x + width, y + radius
);
context.lineTo(x + width, y + height - radius);
context.quadraticCurveTo(
x + width, y + height,
x + width - radius, y + height
);
context.lineTo(x + radius, y + height);
context.quadraticCurveTo(
x, y + height,
x, y + height - radius
);
context.lineTo(x, y + radius);
context.quadraticCurveTo(x, y, x + radius, y);
context.closePath();
}
function makeDistanceBadge(text, x, y) {
const canvas = document.createElement("canvas");
canvas.width = 256;
canvas.height = 128;
const context = canvas.getContext("2d");
context.clearRect(0, 0, canvas.width, canvas.height);
roundedRectanglePath(context, 8, 8, 240, 112, 28);
context.fillStyle = "#fff9ed";
context.fill();
context.lineWidth = 10;
context.strokeStyle = "#243f47";
context.stroke();
context.fillStyle = "#243f47";
context.font = "800 60px system-ui, sans-serif";
context.textAlign = "center";
context.textBaseline = "middle";
context.fillText(text, 128, 67);
const texture = new THREE.CanvasTexture(canvas);
texture.colorSpace = THREE.SRGBColorSpace;
texture.minFilter = THREE.LinearFilter;
texture.magFilter = THREE.LinearFilter;
texture.generateMipmaps = false;
const material = new THREE.SpriteMaterial({
map: texture,
transparent: true,
depthTest: false,
depthWrite: false
});
const sprite = new THREE.Sprite(material);
sprite.position.set(x, y, 0.48);
sprite.scale.set(0.76, 0.38, 1);
sprite.renderOrder = 30;
scene.add(sprite);
const leaderMaterial = new THREE.LineBasicMaterial({
color: 0x243f47,
transparent: true,
opacity: 0.62,
depthTest: false,
depthWrite: false
});
const leaderGeometry = new THREE.BufferGeometry().setFromPoints([
new THREE.Vector3(x, 3.91, 0.39),
new THREE.Vector3(x, 4.08, 0.39)
]);
const leader = new THREE.Line(leaderGeometry, leaderMaterial);
leader.renderOrder = 25;
scene.add(leader);
return sprite;
}
// Ground and circular display platform.
const ground = addMesh(
new THREE.PlaneGeometry(60, 60),
materials.ground,
new THREE.Vector3(0, -0.12, 0),
false
);
ground.rotation.x = -Math.PI / 2;
ground.receiveShadow = true;
addMesh(
new THREE.CylinderGeometry(6.3, 6.43, 0.18, 96),
materials.platform,
new THREE.Vector3(0, -0.02, 0),
false
);
const platformRim = addMesh(
new THREE.TorusGeometry(6.22, 0.032, 8, 96),
materials.cream,
new THREE.Vector3(0, 0.075, 0),
false
);
platformRim.rotation.x = Math.PI / 2;
// Toy base, feet, and top plinth.
for (const x of [-2.25, 2.25]) {
for (const z of [-0.82, 0.82]) {
addMesh(
new THREE.CylinderGeometry(0.27, 0.29, 0.2, 24),
materials.ink,
new THREE.Vector3(x, 0.16, z)
);
}
}
addMesh(
roundedBoxGeometry(5.8, 0.46, 2.45, 0.2),
materials.base,
new THREE.Vector3(0, 0.47, 0)
);
addMesh(
roundedBoxGeometry(3.6, 0.22, 1.65, 0.1),
materials.cream,
new THREE.Vector3(0, 0.78, 0)
);
// Tapered front and rear pivot supports.
const supportShape = new THREE.Shape();
supportShape.moveTo(-1.05, 0.84);
supportShape.lineTo(1.05, 0.84);
supportShape.quadraticCurveTo(0.76, 1.5, 0.25, 3.48);
supportShape.quadraticCurveTo(0.2, 3.67, 0, 3.72);
supportShape.quadraticCurveTo(-0.2, 3.67, -0.25, 3.48);
supportShape.quadraticCurveTo(-0.76, 1.5, -1.05, 0.84);
supportShape.closePath();
const supportGeometry = new THREE.ExtrudeGeometry(supportShape, {
depth: 0.24,
steps: 1,
bevelEnabled: true,
bevelThickness: 0.035,
bevelSize: 0.035,
bevelSegments: 3,
curveSegments: 14
});
supportGeometry.translate(0, 0, -0.12);
supportGeometry.computeVertexNormals();
addMesh(
supportGeometry,
materials.ink,
new THREE.Vector3(0, 0, -0.28)
);
addMesh(
supportGeometry,
materials.ink,
new THREE.Vector3(0, 0, 0.28)
);
// Horizontal beam.
const beamY = 3.75;
addMesh(
roundedBoxGeometry(10.2, 0.34, 0.62, 0.1),
materials.beam,
new THREE.Vector3(0, beamY, 0)
);
// Central pivot hub and axle caps.
const hub = addMesh(
new THREE.CylinderGeometry(0.47, 0.47, 0.82, 48),
materials.base,
new THREE.Vector3(0, beamY, 0)
);
hub.rotation.x = Math.PI / 2;
for (const z of [-0.47, 0.47]) {
const cap = addMesh(
new THREE.CylinderGeometry(0.22, 0.22, 0.085, 32),
materials.brass,
new THREE.Vector3(0, beamY, z)
);
cap.rotation.x = Math.PI / 2;
}
// Inset ruler strip.
addMesh(
roundedBoxGeometry(9.72, 0.21, 0.035, 0.025),
materials.ink,
new THREE.Vector3(0, beamY, 0.323),
false
);
// One conceptual unit equals one equal hook interval.
const unitSpacing = 1.35;
for (let index = -7; index <= 7; index++) {
if (index === 0) continue;
const x = index * unitSpacing * 0.5;
const isMajor = index % 2 === 0;
const height = isMajor ? 0.15 : 0.075;
addMesh(
new THREE.BoxGeometry(0.022, height, 0.025),
materials.cream,
new THREE.Vector3(x, beamY, 0.347),
false
);
}
// Six attachment points, symmetrically and evenly spaced.
const hookPositions = [-3, -2, -1, 1, 2, 3].map(
(distance) => distance * unitSpacing
);
const blockGeometry = roundedBoxGeometry(0.92, 0.92, 0.9, 0.14);
const medallionGeometry = new THREE.CylinderGeometry(0.13, 0.13, 0.035, 24);
const eyeletGeometry = new THREE.TorusGeometry(0.105, 0.033, 8, 20);
for (const x of hookPositions) {
const inward = x < 0 ? 1 : -1;
const blockX = x + inward * 0.08;
// Hook stem.
cylinderBetween(
new THREE.Vector3(x, 3.62, 0),
new THREE.Vector3(x, 3.45, 0),
0.052,
materials.brass
);
// Curved J hook.
const hookCurve = new THREE.CatmullRomCurve3([
new THREE.Vector3(x, 3.47, 0),
new THREE.Vector3(x, 3.23, 0),
new THREE.Vector3(x + inward * 0.08, 3.05, 0),
new THREE.Vector3(x + inward * 0.25, 3.11, 0),
new THREE.Vector3(x + inward * 0.29, 3.25, 0)
]);
const hook = addMesh(
new THREE.TubeGeometry(hookCurve, 28, 0.052, 8, false),
materials.brass,
new THREE.Vector3(0, 0, 0)
);
hook.castShadow = true;
// Every hanging block has the same dimensions and mass.
const blockCenterY = 1.86;
const block = addMesh(
blockGeometry,
materials.block,
new THREE.Vector3(blockX, blockCenterY, 0)
);
block.castShadow = true;
block.receiveShadow = true;
const medallion = addMesh(
medallionGeometry,
materials.cream,
new THREE.Vector3(blockX, blockCenterY, 0.466),
false
);
medallion.rotation.x = Math.PI / 2;
const eyelet = addMesh(
eyeletGeometry,
materials.brass,
new THREE.Vector3(blockX, 2.34, 0)
);
cylinderBetween(
new THREE.Vector3(blockX, 3.045, 0),
new THREE.Vector3(blockX, 2.34, 0),
0.027,
materials.string,
10
);
}
// Distance labels stay upright and readable beside each hook.
makeDistanceBadge("−3u", -3 * unitSpacing, 4.27);
makeDistanceBadge("−2u", -2 * unitSpacing, 4.27);
makeDistanceBadge("−1u", -1 * unitSpacing, 4.27);
makeDistanceBadge("0", 0, 4.27);
makeDistanceBadge("+1u", 1 * unitSpacing, 4.27);
makeDistanceBadge("+2u", 2 * unitSpacing, 4.27);
makeDistanceBadge("+3u", 3 * unitSpacing, 4.27);
// Direct camera controls implemented without external addons.
let dragging = false;
let lastPointerX = 0;
let lastPointerY = 0;
renderer.domElement.addEventListener("pointerdown", (event) => {
if (event.button !== 0) return;
dragging = true;
lastPointerX = event.clientX;
lastPointerY = event.clientY;
renderer.domElement.setPointerCapture(event.pointerId);
document.body.classList.add("dragging");
});
renderer.domElement.addEventListener("pointermove", (event) => {
if (!dragging) return;
const deltaX = event.clientX - lastPointerX;
const deltaY = event.clientY - lastPointerY;
azimuth -= deltaX * 0.006;
polar = THREE.MathUtils.clamp(polar + deltaY * 0.005, 0.84, 1.43);
lastPointerX = event.clientX;
lastPointerY = event.clientY;
updateCamera();
});
function endDrag(event) {
dragging = false;
document.body.classList.remove("dragging");
if (
event.pointerId !== undefined &&
renderer.domElement.hasPointerCapture(event.pointerId)
) {
renderer.domElement.releasePointerCapture(event.pointerId);
}
}
renderer.domElement.addEventListener("pointerup", endDrag);
renderer.domElement.addEventListener("pointercancel", endDrag);
renderer.domElement.addEventListener(
"wheel",
(event) => {
event.preventDefault();
zoomFactor = THREE.MathUtils.clamp(
zoomFactor * (1 + event.deltaY * 0.001),
0.65,
1.45
);
updateCamera();
},
{ passive: false }
);
renderer.domElement.addEventListener("keydown", (event) => {
let handled = true;
switch (event.key) {
case "ArrowLeft":
azimuth += 0.08;
break;
case "ArrowRight":
azimuth -= 0.08;
break;
case "ArrowUp":
polar = THREE.MathUtils.clamp(polar - 0.06, 0.84, 1.43);
break;
case "ArrowDown":
polar = THREE.MathUtils.clamp(polar + 0.06, 0.84, 1.43);
break;
case "+":
case "=":
zoomFactor = THREE.MathUtils.clamp(zoomFactor * 0.9, 0.65, 1.45);
break;
case "-":
case "_":
zoomFactor = THREE.MathUtils.clamp(zoomFactor * 1.1, 0.65, 1.45);
break;
default:
handled = false;
}
if (handled) {
event.preventDefault();
updateCamera();
}
});
function resize() {
const width = host.clientWidth;
const height = host.clientHeight;
camera.aspect = width / height;
camera.updateProjectionMatrix();
const nextFitDistance = calculateFitDistance();
zoomFactor *= nextFitDistance / fitDistance;
fitDistance = nextFitDistance;
zoomFactor = THREE.MathUtils.clamp(zoomFactor, 0.65, 1.45);
renderer.setSize(width, height, false);
updateCamera();
}
window.addEventListener("resize", resize);
camera.aspect = host.clientWidth / host.clientHeight;
camera.updateProjectionMatrix();
fitDistance = calculateFitDistance();
updateCamera();
resize();
renderer.render(scene, camera);
loading.classList.add("hidden");
function animate() {
requestAnimationFrame(animate);
renderer.render(scene, camera);
}
animate();
</script>
</body>
</html>
```