Prompt specification
Build or customize the Three.js 3D scene "Tilting level bubble response" 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: Tilting level bubble response
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/tilting-level-bubble-response-x0314
Scene Description Prompt:
Build a self-contained HTML scene with Three.js from cdnjs featuring a voxel spirit level that tilts slowly in both directions. Model a rectangular tool body around a transparent curved vial, with two center marks and one clearly visible bubble. A side-view pointer shows the tool's tilt angle while the bubble moves toward the higher end of the vial. Pass through a level position where the bubble rests between the marks, then continue to the opposite tilt. Keep the tool and vial rigidly attached, and use a simple schematic bubble response rather than letting the bubble rotate with the body as a fixed decoration.
Working Three.js Starter (tilting-level-bubble-response-x0314):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Voxel Spirit Level</title>
<style>
:root {
color-scheme: dark;
--bg: #07131c;
--panel: rgba(9, 25, 35, 0.78);
--line: rgba(159, 206, 222, 0.22);
--text: #e7f6f7;
--muted: #91aeb8;
--cyan: #67e8f9;
--amber: #ffd166;
--coral: #ff785a;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: var(--bg);
color: var(--text);
font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas,
"Liberation Mono", monospace;
}
#scene {
position: fixed;
inset: 0;
}
canvas {
display: block;
width: 100%;
height: 100%;
}
.hud {
position: fixed;
inset: 0;
pointer-events: none;
}
.title {
position: absolute;
top: clamp(18px, 3vw, 34px);
left: clamp(18px, 3vw, 38px);
max-width: 390px;
}
.eyebrow {
margin-bottom: 8px;
color: var(--cyan);
font-size: 11px;
font-weight: 700;
letter-spacing: 0.23em;
text-transform: uppercase;
}
h1 {
margin: 0;
font-size: clamp(22px, 3vw, 36px);
line-height: 1.02;
letter-spacing: -0.055em;
}
.subtitle {
max-width: 360px;
margin: 12px 0 0;
color: var(--muted);
font-size: 12px;
line-height: 1.65;
}
.readout {
position: absolute;
top: clamp(18px, 3vw, 34px);
right: clamp(18px, 3vw, 38px);
width: min(250px, calc(100vw - 36px));
padding: 17px 18px 16px;
border: 1px solid var(--line);
border-radius: 12px;
background: var(--panel);
box-shadow: 0 18px 60px rgba(0, 0, 0, 0.25);
backdrop-filter: blur(12px);
}
.readout-label {
display: flex;
justify-content: space-between;
color: var(--muted);
font-size: 10px;
letter-spacing: 0.16em;
text-transform: uppercase;
}
#angle {
display: block;
margin: 7px 0 3px;
color: var(--amber);
font-size: clamp(30px, 4vw, 43px);
font-weight: 700;
line-height: 1;
letter-spacing: -0.06em;
font-variant-numeric: tabular-nums;
}
#state {
min-height: 32px;
color: var(--text);
font-size: 11px;
line-height: 1.45;
}
.bubble-track {
position: relative;
height: 5px;
margin-top: 14px;
overflow: hidden;
border-radius: 999px;
background:
linear-gradient(90deg,
rgba(255, 120, 90, 0.25),
rgba(103, 232, 249, 0.22) 45%,
rgba(255, 209, 102, 0.25));
}
.bubble-track::before {
position: absolute;
top: 0;
bottom: 0;
left: 50%;
width: 1px;
content: "";
background: rgba(231, 246, 247, 0.6);
}
#bubble-marker {
position: absolute;
top: 50%;
left: 50%;
width: 11px;
height: 11px;
border: 2px solid #fff6d8;
border-radius: 50%;
background: var(--amber);
box-shadow: 0 0 12px rgba(255, 209, 102, 0.65);
transform: translate(-50%, -50%);
}
.legend {
position: absolute;
left: clamp(18px, 3vw, 38px);
bottom: clamp(18px, 3vw, 32px);
display: flex;
gap: 18px;
color: var(--muted);
font-size: 10px;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.legend span {
display: flex;
align-items: center;
gap: 7px;
}
.dot {
width: 8px;
height: 8px;
border-radius: 50%;
background: var(--amber);
box-shadow: 0 0 8px rgba(255, 209, 102, 0.55);
}
.dot.mark {
width: 4px;
height: 13px;
border-radius: 1px;
background: var(--coral);
box-shadow: none;
}
.controls {
position: absolute;
right: clamp(18px, 3vw, 38px);
bottom: clamp(18px, 3vw, 32px);
display: flex;
gap: 8px;
pointer-events: auto;
}
button {
min-width: 92px;
padding: 10px 14px;
border: 1px solid var(--line);
border-radius: 8px;
color: var(--text);
background: rgba(9, 25, 35, 0.84);
font: inherit;
font-size: 10px;
font-weight: 700;
letter-spacing: 0.1em;
text-transform: uppercase;
cursor: pointer;
transition:
border-color 160ms ease,
background 160ms ease,
transform 160ms ease;
}
button:hover {
border-color: rgba(103, 232, 249, 0.7);
background: rgba(18, 48, 62, 0.94);
}
button:active {
transform: translateY(1px);
}
button:focus-visible {
outline: 2px solid var(--cyan);
outline-offset: 3px;
}
@media (max-width: 680px) {
.subtitle {
display: none;
}
.title {
max-width: 220px;
}
.readout {
width: 178px;
padding: 13px 14px;
}
#state {
font-size: 9px;
}
.legend {
display: none;
}
.controls {
left: 18px;
right: 18px;
justify-content: flex-start;
}
}
</style>
</head>
<body>
<div id="scene"></div>
<div class="hud">
<header class="title">
<div class="eyebrow">Schematic instrument / 01</div>
<h1>Voxel<br>Spirit Level</h1>
<p class="subtitle">
A slow ±12° test cycle. The glass, housing, and bubble travel with
the rigid tool, while the bubble responds to the raised end.
</p>
</header>
<section class="readout" aria-live="polite">
<div class="readout-label">
<span>Tilt angle</span>
<span>Side view</span>
</div>
<strong id="angle">−12.0°</strong>
<div id="state">Left end high · bubble traveling left</div>
<div class="bubble-track" aria-hidden="true">
<i id="bubble-marker"></i>
</div>
</section>
<div class="legend" aria-hidden="true">
<span><i class="dot"></i> Bubble</span>
<span><i class="dot mark"></i> Center marks</span>
</div>
<div class="controls">
<button id="pause" type="button" aria-pressed="false">Pause</button>
<button id="restart" type="button">Restart cycle</button>
</div>
</div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// -------------------------------------------------------------------------
// Renderer, camera, and lighting
// -------------------------------------------------------------------------
const sceneHost = document.getElementById("scene");
const renderer = new THREE.WebGLRenderer({
antialias: true,
powerPreference: "high-performance"
});
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;
sceneHost.appendChild(renderer.domElement);
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x07131c);
scene.fog = new THREE.Fog(0x07131c, 13, 28);
const camera = new THREE.PerspectiveCamera(
34,
window.innerWidth / window.innerHeight,
0.1,
100
);
const hemi = new THREE.HemisphereLight(0xbcefff, 0x10202a, 2.1);
scene.add(hemi);
const keyLight = new THREE.DirectionalLight(0xffffff, 3.2);
keyLight.position.set(-3.5, 7, 6);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(1024, 1024);
keyLight.shadow.camera.left = -8;
keyLight.shadow.camera.right = 8;
keyLight.shadow.camera.top = 6;
keyLight.shadow.camera.bottom = -6;
keyLight.shadow.camera.near = 0.5;
keyLight.shadow.camera.far = 25;
keyLight.shadow.bias = -0.0004;
scene.add(keyLight);
const cyanLight = new THREE.PointLight(0x49dfff, 18, 15, 2);
cyanLight.position.set(-4.5, 1.5, 4);
scene.add(cyanLight);
const amberLight = new THREE.PointLight(0xffb44d, 13, 12, 2);
amberLight.position.set(5, -0.5, 4);
scene.add(amberLight);
// -------------------------------------------------------------------------
// Materials and small helpers
// -------------------------------------------------------------------------
const housingMaterial = new THREE.MeshStandardMaterial({
color: 0x174b5b,
roughness: 0.62,
metalness: 0.16
});
const darkMaterial = new THREE.MeshStandardMaterial({
color: 0x0b2633,
roughness: 0.72,
metalness: 0.12
});
const amberMaterial = new THREE.MeshStandardMaterial({
color: 0xf2b84b,
roughness: 0.5,
metalness: 0.12
});
const cyanMaterial = new THREE.MeshStandardMaterial({
color: 0x4ac9d8,
roughness: 0.48,
metalness: 0.2
});
const boltMaterial = new THREE.MeshStandardMaterial({
color: 0xb9cbd0,
roughness: 0.3,
metalness: 0.78
});
const markMaterial = new THREE.MeshBasicMaterial({
color: 0xff785a,
toneMapped: false
});
const glassMaterial = new THREE.MeshPhysicalMaterial({
color: 0xa9f1ff,
transparent: true,
opacity: 0.3,
roughness: 0.08,
metalness: 0,
transmission: 0.18,
thickness: 0.22,
ior: 1.45,
clearcoat: 1,
clearcoatRoughness: 0.08,
side: THREE.DoubleSide,
depthWrite: false
});
const fluidMaterial = new THREE.MeshBasicMaterial({
color: 0x5edbe9,
transparent: true,
opacity: 0.075,
depthWrite: false,
side: THREE.DoubleSide
});
function addBox(parent, size, position, material, castShadow = true) {
const mesh = new THREE.Mesh(
new THREE.BoxGeometry(size.x, size.y, size.z),
material
);
mesh.position.copy(position);
mesh.castShadow = castShadow;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
function createTextPlane(
text,
width,
height,
color = "#dff7f8",
fontSize = 52
) {
const canvas = document.createElement("canvas");
canvas.width = 512;
canvas.height = 128;
const context = canvas.getContext("2d");
context.clearRect(0, 0, canvas.width, canvas.height);
context.fillStyle = color;
context.font = `700 ${fontSize}px monospace`;
context.textAlign = "center";
context.textBaseline = "middle";
context.fillText(text, canvas.width / 2, canvas.height / 2);
const texture = new THREE.CanvasTexture(canvas);
texture.colorSpace = THREE.SRGBColorSpace;
texture.minFilter = THREE.LinearFilter;
const material = new THREE.MeshBasicMaterial({
map: texture,
transparent: true,
depthWrite: false,
toneMapped: false
});
return new THREE.Mesh(
new THREE.PlaneGeometry(width, height),
material
);
}
// -------------------------------------------------------------------------
// Ground and subtle voxel scenery
// -------------------------------------------------------------------------
const floor = new THREE.Mesh(
new THREE.PlaneGeometry(32, 24),
new THREE.MeshStandardMaterial({
color: 0x081821,
roughness: 0.94,
metalness: 0.04
})
);
floor.rotation.x = -Math.PI / 2;
floor.position.y = -1.55;
floor.receiveShadow = true;
scene.add(floor);
const grid = new THREE.GridHelper(24, 48, 0x31515c, 0x18323d);
grid.position.y = -1.535;
grid.material.transparent = true;
grid.material.opacity = 0.24;
scene.add(grid);
const sceneryMaterial = new THREE.MeshStandardMaterial({
color: 0x102c38,
roughness: 0.9,
metalness: 0.03
});
const sceneryBlocks = [
[-4.8, -1.16, -2.1, 0.7, 0.7, 0.7],
[-4.2, -1.32, -2.35, 0.45, 0.45, 0.45],
[5.8, -1.25, -2.6, 0.6, 0.6, 0.6],
[6.3, -1.4, -2.4, 0.3, 0.3, 0.3]
];
for (const [x, y, z, sx, sy, sz] of sceneryBlocks) {
addBox(
scene,
new THREE.Vector3(sx, sy, sz),
new THREE.Vector3(x, y, z),
sceneryMaterial
);
}
// -------------------------------------------------------------------------
// Rigid spirit-level assembly
//
// The housing, vial, marks, and bubble all belong to levelRig. Rotating that
// group keeps the tool and glass rigidly attached.
// -------------------------------------------------------------------------
const levelRig = new THREE.Group();
levelRig.position.y = 0.02;
scene.add(levelRig);
// Open rectangular housing around the visible vial.
addBox(
levelRig,
new THREE.Vector3(6.55, 0.48, 1.02),
new THREE.Vector3(0, -0.59, 0),
housingMaterial
);
addBox(
levelRig,
new THREE.Vector3(5.45, 0.76, 0.14),
new THREE.Vector3(0, 0.04, -0.35),
darkMaterial
);
addBox(
levelRig,
new THREE.Vector3(5.58, 0.22, 0.68),
new THREE.Vector3(0, 0.60, -0.22),
housingMaterial
);
for (const x of [-2.95, 2.95]) {
addBox(
levelRig,
new THREE.Vector3(0.64, 1.55, 1.06),
new THREE.Vector3(x, -0.04, 0),
amberMaterial
);
for (const y of [-0.47, 0.38]) {
const bolt = new THREE.Mesh(
new THREE.CylinderGeometry(0.075, 0.075, 0.055, 12),
boltMaterial
);
bolt.rotation.x = Math.PI / 2;
bolt.position.set(x, y, 0.555);
bolt.castShadow = true;
levelRig.add(bolt);
}
}
// Raised, block-like details emphasize the voxel construction.
const topBlockXs = [-2.2, -1.65, -1.1, 1.1, 1.65, 2.2];
topBlockXs.forEach((x, index) => {
addBox(
levelRig,
new THREE.Vector3(0.38, 0.14, 0.72),
new THREE.Vector3(x, 0.78, -0.20),
index % 2 === 0 ? cyanMaterial : amberMaterial
);
});
for (const x of [-2.25, 2.25]) {
addBox(
levelRig,
new THREE.Vector3(0.28, 0.23, 0.78),
new THREE.Vector3(x, -0.27, -0.02),
cyanMaterial
);
}
const bodyLabel = createTextPlane(
"VOXEL / LEVEL 01",
1.95,
0.27,
"#d9f5f4",
44
);
bodyLabel.position.set(-1.42, -0.58, 0.516);
levelRig.add(bodyLabel);
// -------------------------------------------------------------------------
// Transparent curved vial
// -------------------------------------------------------------------------
const VIAL_HALF_LENGTH = 2.52;
const VIAL_Z = 0.12;
// A gently bowed centerline, built from a Catmull-Rom curve.
function vialY(x) {
const normalized = x / VIAL_HALF_LENGTH;
return 0.08 + 0.12 * normalized * normalized;
}
const vialPoints = [];
for (let i = 0; i <= 18; i++) {
const x = THREE.MathUtils.lerp(
-VIAL_HALF_LENGTH,
VIAL_HALF_LENGTH,
i / 18
);
vialPoints.push(new THREE.Vector3(x, vialY(x), VIAL_Z));
}
const vialCurve = new THREE.CatmullRomCurve3(
vialPoints,
false,
"catmullrom",
0.25
);
const glassTube = new THREE.Mesh(
new THREE.TubeGeometry(vialCurve, 128, 0.28, 18, false),
glassMaterial
);
glassTube.renderOrder = 3;
levelRig.add(glassTube);
// Rounded glass ends.
for (const x of [-VIAL_HALF_LENGTH, VIAL_HALF_LENGTH]) {
const cap = new THREE.Mesh(
new THREE.SphereGeometry(0.28, 24, 16),
glassMaterial
);
cap.position.set(x, vialY(x), VIAL_Z);
cap.renderOrder = 3;
levelRig.add(cap);
}
const fluidCore = new THREE.Mesh(
new THREE.TubeGeometry(vialCurve, 112, 0.19, 12, false),
fluidMaterial
);
fluidCore.renderOrder = 2;
levelRig.add(fluidCore);
// Thin specular stripe makes the curved glass easier to read.
const highlightPoints = [];
for (let i = 0; i <= 30; i++) {
const x = THREE.MathUtils.lerp(
-VIAL_HALF_LENGTH,
VIAL_HALF_LENGTH,
i / 30
);
highlightPoints.push(
new THREE.Vector3(x, vialY(x) + 0.165, VIAL_Z + 0.275)
);
}
const highlightCurve = new THREE.CatmullRomCurve3(highlightPoints);
const glassHighlight = new THREE.Mesh(
new THREE.TubeGeometry(highlightCurve, 100, 0.012, 6, false),
new THREE.MeshBasicMaterial({
color: 0xe7fdff,
transparent: true,
opacity: 0.58,
depthWrite: false,
toneMapped: false
})
);
glassHighlight.renderOrder = 5;
levelRig.add(glassHighlight);
// Two center marks. At zero tilt, the bubble sits directly between them.
for (const x of [-0.235, 0.235]) {
const mark = addBox(
levelRig,
new THREE.Vector3(0.038, 0.50, 0.026),
new THREE.Vector3(x, vialY(x), 0.407),
markMaterial,
false
);
mark.renderOrder = 6;
}
// -------------------------------------------------------------------------
// Bubble
//
// Its local X position is recomputed from the tool angle. This is a
// schematic response along the vial, not a decoration fixed at one point.
// Positive angle raises the vial's right end, so the bubble moves right.
// -------------------------------------------------------------------------
const bubbleGroup = new THREE.Group();
levelRig.add(bubbleGroup);
const bubble = new THREE.Mesh(
new THREE.SphereGeometry(0.165, 28, 20),
new THREE.MeshPhysicalMaterial({
color: 0xffd166,
emissive: 0x6e3500,
emissiveIntensity: 0.55,
roughness: 0.16,
metalness: 0,
clearcoat: 1,
clearcoatRoughness: 0.08
})
);
bubble.castShadow = false;
bubbleGroup.add(bubble);
// Small attached glint helps the single bubble remain clearly visible.
const bubbleGlint = new THREE.Mesh(
new THREE.SphereGeometry(0.028, 12, 8),
new THREE.MeshBasicMaterial({
color: 0xffffff,
toneMapped: false
})
);
bubbleGlint.position.set(-0.048, 0.058, 0.145);
bubbleGroup.add(bubbleGlint);
// -------------------------------------------------------------------------
// Side-view angle gauge
// -------------------------------------------------------------------------
const gauge = new THREE.Group();
gauge.position.set(4.22, -0.10, 0.02);
scene.add(gauge);
const gaugeArcPoints = [];
const gaugeRadius = 1.03;
const gaugeMinAngle = THREE.MathUtils.degToRad(-18);
const gaugeMaxAngle = THREE.MathUtils.degToRad(18);
for (let i = 0; i <= 72; i++) {
const angle = THREE.MathUtils.lerp(
gaugeMinAngle,
gaugeMaxAngle,
i / 72
);
gaugeArcPoints.push(
new THREE.Vector3(
Math.cos(angle) * gaugeRadius,
Math.sin(angle) * gaugeRadius,
0
)
);
}
const gaugeArc = new THREE.Line(
new THREE.BufferGeometry().setFromPoints(gaugeArcPoints),
new THREE.LineBasicMaterial({
color: 0x91b8c2,
transparent: true,
opacity: 0.72
})
);
gauge.add(gaugeArc);
const gaugePlate = new THREE.Mesh(
new THREE.RingGeometry(
0.90,
1.075,
72,
1,
gaugeMinAngle,
gaugeMaxAngle - gaugeMinAngle
),
new THREE.MeshBasicMaterial({
color: 0x4e8794,
transparent: true,
opacity: 0.075,
side: THREE.DoubleSide,
depthWrite: false
})
);
gauge.add(gaugePlate);
const tickVertices = [];
for (let degrees = -15; degrees <= 15; degrees += 3) {
const angle = THREE.MathUtils.degToRad(degrees);
const major = degrees % 6 === 0;
const innerRadius = major ? 0.87 : 0.93;
const outerRadius = 1.04;
tickVertices.push(
Math.cos(angle) * innerRadius,
Math.sin(angle) * innerRadius,
0.015,
Math.cos(angle) * outerRadius,
Math.sin(angle) * outerRadius,
0.015
);
}
const tickGeometry = new THREE.BufferGeometry();
tickGeometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(tickVertices, 3)
);
const ticks = new THREE.LineSegments(
tickGeometry,
new THREE.LineBasicMaterial({
color: 0xb5d3da,
transparent: true,
opacity: 0.88
})
);
gauge.add(ticks);
// Green zero-degree reference.
const zeroReference = new THREE.Line(
new THREE.BufferGeometry().setFromPoints([
new THREE.Vector3(0.72, 0, 0.025),
new THREE.Vector3(1.10, 0, 0.025)
]),
new THREE.LineBasicMaterial({
color: 0x67e8f9,
transparent: true,
opacity: 0.9
})
);
gauge.add(zeroReference);
for (const [label, degrees] of [
["−15", -15],
["0", 0],
["+15", 15]
]) {
const angle = THREE.MathUtils.degToRad(degrees);
const labelMesh = createTextPlane(
label,
0.34,
0.15,
degrees === 0 ? "#67e8f9" : "#b8d3d9",
46
);
labelMesh.position.set(
Math.cos(angle) * 1.27,
Math.sin(angle) * 1.27,
0.04
);
gauge.add(labelMesh);
}
const gaugeCaption = createTextPlane(
"ANGLE / SIDE VIEW",
1.65,
0.22,
"#83aab5",
38
);
gaugeCaption.position.set(0.35, -1.18, 0.02);
gauge.add(gaugeCaption);
const needle = new THREE.Group();
needle.position.z = 0.10;
gauge.add(needle);
const needleMaterial = new THREE.MeshStandardMaterial({
color: 0xff8a45,
emissive: 0x8c2600,
emissiveIntensity: 0.75,
roughness: 0.4,
metalness: 0.22
});
const needleShaft = new THREE.Mesh(
new THREE.BoxGeometry(0.82, 0.048, 0.065),
needleMaterial
);
needleShaft.position.x = 0.41;
needle.add(needleShaft);
const needleTip = new THREE.Mesh(
new THREE.ConeGeometry(0.095, 0.22, 10),
needleMaterial
);
needleTip.rotation.z = -Math.PI / 2;
needleTip.position.x = 0.92;
needle.add(needleTip);
const needleHub = new THREE.Mesh(
new THREE.CylinderGeometry(0.13, 0.13, 0.11, 24),
needleMaterial
);
needleHub.rotation.x = Math.PI / 2;
needle.add(needleHub);
// -------------------------------------------------------------------------
// Animation
// -------------------------------------------------------------------------
const MAX_TILT = THREE.MathUtils.degToRad(12);
const CYCLE_SECONDS = 12;
const INITIAL_PHASE = -Math.PI / 2;
let elapsed = 0;
let paused = false;
const clock = new THREE.Clock();
const angleElement = document.getElementById("angle");
const stateElement = document.getElementById("state");
const bubbleMarker = document.getElementById("bubble-marker");
const pauseButton = document.getElementById("pause");
const restartButton = document.getElementById("restart");
pauseButton.addEventListener("click", () => {
paused = !paused;
pauseButton.textContent = paused ? "Resume" : "Pause";
pauseButton.setAttribute("aria-pressed", String(paused));
});
restartButton.addEventListener("click", () => {
elapsed = 0;
paused = false;
pauseButton.textContent = "Pause";
pauseButton.setAttribute("aria-pressed", "false");
});
window.addEventListener("keydown", (event) => {
if (event.code === "Space") {
event.preventDefault();
pauseButton.click();
}
});
function updateReadout(angle, bubbleX) {
const degrees = THREE.MathUtils.radToDeg(angle);
const sign = degrees >= 0 ? "+" : "−";
const absoluteDegrees = Math.abs(degrees);
angleElement.textContent = `${sign}${absoluteDegrees.toFixed(1)}°`;
if (Math.abs(degrees) < 0.12) {
stateElement.textContent =
"Level · bubble centered between marks";
} else if (degrees > 0) {
stateElement.textContent =
"Right end high · bubble traveling right";
} else {
stateElement.textContent =
"Left end high · bubble traveling left";
}
const markerPercent = THREE.MathUtils.clamp(
50 + (bubbleX / 2.1) * 50,
2,
98
);
bubbleMarker.style.left = `${markerPercent}%`;
}
function animate() {
const delta = Math.min(clock.getDelta(), 0.05);
if (!paused) {
elapsed += delta;
}
const phase =
INITIAL_PHASE + (elapsed / CYCLE_SECONDS) * Math.PI * 2;
// Starts fully tilted left, passes through level, then continues right.
const angle = Math.sin(phase) * MAX_TILT;
levelRig.rotation.z = angle;
needle.rotation.z = angle;
// Schematic bubble response:
// tan(angle) changes sign at level and points toward the raised end.
// The gain is intentionally exaggerated so the motion is easy to read.
const bubbleX = THREE.MathUtils.clamp(
Math.tan(angle) * 3.0,
-2.1,
2.1
);
// Recompute the bubble's position inside the rigid vial every frame.
bubbleGroup.position.set(
bubbleX,
vialY(bubbleX),
0.20
);
updateReadout(angle, bubbleX);
renderer.render(scene, camera);
}
// -------------------------------------------------------------------------
// Responsive camera framing
// -------------------------------------------------------------------------
function resize() {
const width = window.innerWidth;
const height = window.innerHeight;
const aspect = width / height;
camera.aspect = aspect;
camera.position.set(0.72, 0.32, 11.6 * Math.max(1, 1.42 / aspect));
camera.lookAt(0.72, -0.03, 0);
camera.updateProjectionMatrix();
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(width, height);
}
window.addEventListener("resize", resize);
resize();
renderer.setAnimationLoop(animate);
</script>
</body>
</html>
```