Prompt specification
Build or customize the Three.js 3D scene "Quantized ruler and continuous marker" 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: Quantized ruler and continuous marker
Category: Algorithms & Systems · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/quantized-ruler-and-continuous-marker-x1873
Scene Description Prompt:
Please use Three.js from cdnjs for a low-poly scene based on this idea: quantized ruler and continuous marker. Build a continuous position slider beside discrete representable values on a ruler; show rounding error as a separate segment and state the chosen precision. Return one self-contained HTML file.
Working Three.js Starter (quantized-ruler-and-continuous-marker-x1873):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Quantized Ruler — Continuous Marker</title>
<style>
:root {
color-scheme: dark;
--bg: #0b1018;
--panel: rgba(14, 21, 31, 0.88);
--border: rgba(151, 177, 204, 0.18);
--text: #edf5ff;
--muted: #92a5bb;
--cyan: #51e0d5;
--gold: #f6c85f;
--pink: #ff5c8a;
}
* { box-sizing: border-box; }
html, body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: var(--bg);
color: var(--text);
font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
#three-canvas {
position: fixed;
inset: 0;
display: block;
width: 100%;
height: 100%;
touch-action: none;
outline: none;
}
.vignette {
position: fixed;
inset: 0;
pointer-events: none;
background:
radial-gradient(circle at 48% 45%, transparent 38%, rgba(4, 8, 13, 0.34) 100%),
linear-gradient(180deg, rgba(4, 8, 13, 0.18), transparent 25%);
}
header {
position: fixed;
top: 28px;
left: 32px;
width: min(390px, calc(100vw - 410px));
pointer-events: none;
z-index: 2;
}
.eyebrow {
margin-bottom: 9px;
color: var(--cyan);
font: 700 11px/1.2 ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
letter-spacing: 0.2em;
text-transform: uppercase;
}
h1 {
margin: 0;
font-size: clamp(29px, 3.1vw, 46px);
line-height: 0.98;
letter-spacing: -0.045em;
}
header p {
max-width: 350px;
margin: 14px 0 0;
color: var(--muted);
font-size: 14px;
line-height: 1.55;
}
.panel {
position: fixed;
top: 24px;
right: 24px;
z-index: 3;
width: 342px;
padding: 18px;
border: 1px solid var(--border);
border-radius: 18px;
background: var(--panel);
box-shadow: 0 22px 60px rgba(0, 0, 0, 0.32);
backdrop-filter: blur(16px);
-webkit-backdrop-filter: blur(16px);
}
.panel-heading {
display: flex;
align-items: center;
justify-content: space-between;
gap: 12px;
margin-bottom: 17px;
}
.panel-heading h2 {
margin: 0;
font-size: 13px;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.precision-state {
padding: 6px 9px;
border: 1px solid rgba(246, 200, 95, 0.3);
border-radius: 9px;
color: var(--gold);
background: rgba(246, 200, 95, 0.08);
font: 700 12px/1 ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
white-space: nowrap;
}
.field-label {
display: flex;
justify-content: space-between;
margin: 0 0 8px;
color: var(--muted);
font: 700 10px/1.2 ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
letter-spacing: 0.13em;
text-transform: uppercase;
}
.precision-options {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 7px;
margin-bottom: 18px;
}
.precision-options button {
padding: 9px 4px;
border: 1px solid var(--border);
border-radius: 9px;
color: var(--muted);
background: rgba(255, 255, 255, 0.035);
font: 700 12px/1 ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
cursor: pointer;
transition: 150ms ease;
}
.precision-options button:hover {
color: var(--text);
border-color: rgba(246, 200, 95, 0.45);
}
.precision-options button[aria-pressed="true"] {
color: #161108;
border-color: var(--gold);
background: var(--gold);
}
input[type="range"] {
width: 100%;
height: 5px;
margin: 4px 0 20px;
border-radius: 999px;
appearance: none;
-webkit-appearance: none;
background: linear-gradient(90deg, #36536c, #51e0d5);
cursor: pointer;
}
input[type="range"]::-webkit-slider-thumb {
width: 18px;
height: 18px;
border: 3px solid #d9fffa;
border-radius: 50%;
appearance: none;
-webkit-appearance: none;
background: var(--cyan);
box-shadow: 0 0 0 5px rgba(81, 224, 213, 0.13);
}
input[type="range"]::-moz-range-thumb {
width: 14px;
height: 14px;
border: 3px solid #d9fffa;
border-radius: 50%;
background: var(--cyan);
box-shadow: 0 0 0 5px rgba(81, 224, 213, 0.13);
}
.metrics {
display: grid;
grid-template-columns: 1fr 1fr;
overflow: hidden;
border: 1px solid var(--border);
border-radius: 12px;
}
.metric {
min-height: 68px;
padding: 12px;
background: rgba(255, 255, 255, 0.025);
}
.metric:nth-child(odd) { border-right: 1px solid var(--border); }
.metric:nth-child(-n + 2) { border-bottom: 1px solid var(--border); }
.metric span {
display: block;
margin-bottom: 7px;
color: var(--muted);
font: 700 9px/1.2 ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
letter-spacing: 0.11em;
text-transform: uppercase;
}
.metric strong {
font: 700 18px/1 ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
}
.continuous strong { color: var(--cyan); }
.quantized strong { color: var(--gold); }
.error strong { color: var(--pink); }
.legend {
display: grid;
gap: 8px;
margin: 16px 0;
color: var(--muted);
font-size: 12px;
}
.legend div {
display: flex;
align-items: center;
gap: 9px;
}
.swatch {
display: inline-block;
flex: 0 0 auto;
width: 15px;
height: 7px;
border-radius: 3px;
}
.swatch.continuous { background: var(--cyan); }
.swatch.quantized { background: var(--gold); }
.swatch.error { background: var(--pink); }
.panel-footer {
display: flex;
align-items: center;
justify-content: space-between;
gap: 12px;
padding-top: 14px;
border-top: 1px solid var(--border);
color: var(--muted);
font-size: 11px;
line-height: 1.45;
}
button.reset {
flex: 0 0 auto;
padding: 8px 10px;
border: 1px solid var(--border);
border-radius: 8px;
color: var(--text);
background: rgba(255, 255, 255, 0.04);
font: 700 10px/1 ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
letter-spacing: 0.08em;
text-transform: uppercase;
cursor: pointer;
}
button.reset:hover { border-color: rgba(81, 224, 213, 0.5); }
.hint {
position: fixed;
bottom: 22px;
left: 32px;
z-index: 2;
padding: 9px 12px;
border: 1px solid var(--border);
border-radius: 9px;
color: var(--muted);
background: rgba(11, 16, 24, 0.72);
font: 600 10px/1.4 ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
letter-spacing: 0.06em;
pointer-events: none;
}
@media (max-width: 760px) {
header {
top: 16px;
left: 16px;
width: calc(100vw - 32px);
}
header p { display: none; }
h1 { font-size: 30px; }
.panel {
top: auto;
right: 12px;
bottom: 12px;
left: 12px;
width: auto;
padding: 14px;
}
.panel-heading { margin-bottom: 12px; }
.precision-options { margin-bottom: 13px; }
.metric { min-height: 58px; padding: 9px; }
.metric strong { font-size: 15px; }
.legend { display: none; }
.panel-footer { padding-top: 10px; }
.hint { display: none; }
}
</style>
</head>
<body>
<canvas id="three-canvas" aria-label="Interactive three-dimensional quantized ruler"></canvas>
<div class="vignette"></div>
<header>
<div class="eyebrow">Continuous signal / discrete lattice</div>
<h1>Quantized Ruler</h1>
<p>
Drag the cyan marker along the continuous rail. Gold marks are the
representable values; the pink segment isolates the rounding error.
</p>
</header>
<section class="panel" aria-label="Quantization controls and readout">
<div class="panel-heading">
<h2>Quantization</h2>
<div class="precision-state">
p = <span id="precisionText">0.25</span>
</div>
</div>
<div class="field-label">
<span>Chosen precision</span>
<span>units</span>
</div>
<div class="precision-options" role="group" aria-label="Choose precision">
<button type="button" data-precision="0.5" aria-pressed="false">0.50</button>
<button type="button" data-precision="0.25" aria-pressed="true">0.25</button>
<button type="button" data-precision="0.1" aria-pressed="false">0.10</button>
</div>
<label class="field-label" for="position">
<span>Continuous position</span>
<span>x</span>
</label>
<input id="position" type="range" min="0" max="4" step="0.001" value="2.37" />
<div class="metrics" aria-live="polite">
<div class="metric continuous">
<span>Continuous x</span>
<strong id="continuousValue">2.370</strong>
</div>
<div class="metric quantized">
<span>Quantized Q(x)</span>
<strong id="quantizedValue">2.250</strong>
</div>
<div class="metric error">
<span>Absolute error</span>
<strong id="absoluteError">0.120</strong>
</div>
<div class="metric error">
<span>Residual x − Q(x)</span>
<strong id="residualValue">+0.120</strong>
</div>
</div>
<div class="legend">
<div><span class="swatch continuous"></span>Continuous marker and projection</div>
<div><span class="swatch quantized"></span>Nearest representable value</div>
<div><span class="swatch error"></span>Separate rounding-error segment</div>
</div>
<div class="panel-footer">
<span>Drag empty space to orbit.<br />Wheel to zoom. The error lane is offset in front.</span>
<button class="reset" id="resetCamera" type="button">Reset view</button>
</div>
</section>
<div class="hint">DRAG CYAN MARKER · ORBIT EMPTY SPACE · WHEEL TO ZOOM</div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
const canvas = document.getElementById("three-canvas");
const positionInput = document.getElementById("position");
const precisionText = document.getElementById("precisionText");
const continuousValueEl = document.getElementById("continuousValue");
const quantizedValueEl = document.getElementById("quantizedValue");
const absoluteErrorEl = document.getElementById("absoluteError");
const residualValueEl = document.getElementById("residualValue");
const precisionButtons = [...document.querySelectorAll("[data-precision]")];
const VALUE_MIN = 0;
const VALUE_MAX = 4;
const X_MIN = -5.65;
const X_MAX = 5.65;
const LANE_Z = 2.58;
const LANE_Y = -0.10;
let precision = 0.25;
let continuousValue = 2.37;
const valueToX = (value) =>
X_MIN + ((value - VALUE_MIN) / (VALUE_MAX - VALUE_MIN)) * (X_MAX - X_MIN);
const xToValue = (x) =>
VALUE_MIN + ((x - X_MIN) / (X_MAX - X_MIN)) * (VALUE_MAX - VALUE_MIN);
const quantize = (value, p) =>
Math.round((value + Number.EPSILON) / p) * p;
const precisionDigits = (p) => (p === 0.25 ? 2 : 1);
const renderer = new THREE.WebGLRenderer({
canvas,
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;
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x0b1018);
scene.fog = new THREE.Fog(0x0b1018, 18, 38);
const camera = new THREE.PerspectiveCamera(
43,
window.innerWidth / window.innerHeight,
0.1,
80
);
const cameraTarget = new THREE.Vector3(0, 0.05, 0.35);
let cameraYaw = 0;
let cameraPitch = 0.63;
let cameraRadius = 15.5;
function updateCamera() {
const sinPitch = Math.sin(cameraPitch);
camera.position.set(
cameraTarget.x + cameraRadius * sinPitch * Math.sin(cameraYaw),
cameraTarget.y + cameraRadius * Math.cos(cameraPitch),
cameraTarget.z + cameraRadius * sinPitch * Math.cos(cameraYaw)
);
camera.lookAt(cameraTarget);
}
updateCamera();
const hemisphere = new THREE.HemisphereLight(0xb8dcff, 0x20202a, 2.0);
scene.add(hemisphere);
const keyLight = new THREE.DirectionalLight(0xfff0cf, 3.1);
keyLight.position.set(-4, 11, 8);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(2048, 2048);
keyLight.shadow.camera.left = -11;
keyLight.shadow.camera.right = 11;
keyLight.shadow.camera.top = 9;
keyLight.shadow.camera.bottom = -9;
keyLight.shadow.camera.near = 1;
keyLight.shadow.camera.far = 30;
keyLight.shadow.bias = -0.00035;
scene.add(keyLight);
const rimLight = new THREE.DirectionalLight(0x42d7e8, 1.25);
rimLight.position.set(7, 4, -7);
scene.add(rimLight);
const pinkLight = new THREE.PointLight(0xff477d, 7.0, 10, 2);
pinkLight.position.set(0, 2.2, 6);
scene.add(pinkLight);
const flatMaterial = (color, options = {}) =>
new THREE.MeshStandardMaterial({
color,
roughness: 0.72,
metalness: 0.06,
flatShading: true,
...options
});
function box(width, height, depth, material, x, y, z) {
const mesh = new THREE.Mesh(
new THREE.BoxGeometry(width, height, depth),
material
);
mesh.position.set(x, y, z);
mesh.castShadow = true;
mesh.receiveShadow = true;
scene.add(mesh);
return mesh;
}
// Ground and restrained laboratory grid.
const ground = new THREE.Mesh(
new THREE.PlaneGeometry(50, 50),
flatMaterial(0x0e151e)
);
ground.rotation.x = -Math.PI / 2;
ground.position.y = -0.47;
ground.receiveShadow = true;
scene.add(ground);
const grid = new THREE.GridHelper(38, 38, 0x2b4052, 0x1b2835);
grid.position.y = -0.458;
grid.material.transparent = true;
grid.material.opacity = 0.23;
grid.material.depthWrite = false;
scene.add(grid);
// Main ruler body.
const rulerBodyMaterial = flatMaterial(0x182432);
const rulerEdgeMaterial = flatMaterial(0x355064);
const panelA = flatMaterial(0x26394a);
const panelB = flatMaterial(0x223546);
box(12.15, 0.34, 2.35, rulerBodyMaterial, 0, 0, 0);
const panelWidth = (X_MAX - X_MIN) / 5;
for (let i = 0; i < 5; i++) {
const centerValue = i + 0.5;
box(
panelWidth - 0.055,
0.07,
2.13,
i % 2 === 0 ? panelA : panelB,
valueToX(centerValue),
0.20,
0
);
}
box(12.15, 0.09, 0.09, rulerEdgeMaterial, 0, 0.20, 1.13);
box(12.15, 0.09, 0.09, rulerEdgeMaterial, 0, 0.20, -1.13);
const supportMaterial = flatMaterial(0x202c38);
box(0.62, 0.40, 1.65, supportMaterial, -4.2, -0.26, -0.08);
box(0.62, 0.40, 1.65, supportMaterial, 4.2, -0.26, -0.08);
// Text sprite helpers.
function roundedRect(ctx, x, y, width, height, radius) {
ctx.beginPath();
ctx.moveTo(x + radius, y);
ctx.lineTo(x + width - radius, y);
ctx.quadraticCurveTo(x + width, y, x + width, y + radius);
ctx.lineTo(x + width, y + height - radius);
ctx.quadraticCurveTo(
x + width, y + height, x + width - radius, y + height
);
ctx.lineTo(x + radius, y + height);
ctx.quadraticCurveTo(x, y + height, x, y + height - radius);
ctx.lineTo(x, y + radius);
ctx.quadraticCurveTo(x, y, x + radius, y);
ctx.closePath();
}
function drawLabel(ctx, text, color, background, width, height) {
ctx.clearRect(0, 0, width, height);
if (background) {
roundedRect(ctx, 4, 4, width - 8, height - 8, 20);
ctx.fillStyle = background;
ctx.fill();
ctx.strokeStyle = color;
ctx.globalAlpha = 0.45;
ctx.lineWidth = 3;
ctx.stroke();
ctx.globalAlpha = 1;
}
ctx.font =
'700 44px ui-monospace, SFMono-Regular, Menlo, Consolas, monospace';
ctx.textAlign = "center";
ctx.textBaseline = "middle";
ctx.shadowColor = "rgba(0,0,0,0.8)";
ctx.shadowBlur = 7;
ctx.fillStyle = color;
ctx.fillText(text, width / 2, height / 2 + 2);
ctx.shadowBlur = 0;
}
function createLabel(text, color, options = {}) {
const labelCanvas = document.createElement("canvas");
labelCanvas.width = options.width ?? 512;
labelCanvas.height = options.height ?? 128;
const ctx = labelCanvas.getContext("2d");
const background = options.background ?? "rgba(9,15,23,0.82)";
drawLabel(ctx, text, color, background, labelCanvas.width, labelCanvas.height);
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,
toneMapped: false
});
const sprite = new THREE.Sprite(material);
sprite.renderOrder = 20;
sprite.userData.canvas = labelCanvas;
sprite.userData.context = ctx;
sprite.userData.texture = texture;
sprite.userData.color = color;
sprite.userData.background = background;
return sprite;
}
function updateLabel(sprite, text, color = sprite.userData.color) {
drawLabel(
sprite.userData.context,
text,
color,
sprite.userData.background,
sprite.userData.canvas.width,
sprite.userData.canvas.height
);
sprite.userData.texture.needsUpdate = true;
}
// Major ruler ticks and numeric labels.
const tickMaterial = flatMaterial(0xdce8ee, {
emissive: 0x26333b,
emissiveIntensity: 0.2
});
for (let i = 0; i <= 4; i++) {
const x = valueToX(i);
box(0.065, 0.055, 0.77, tickMaterial, x, 0.265, -0.47);
const label = createLabel(String(i), "#dce8ee", {
width: 256,
height: 128,
background: null
});
label.scale.set(0.48, 0.24, 1);
label.position.set(x, 0.60, -1.02);
scene.add(label);
}
const halfTickMaterial = flatMaterial(0x8ca4b5);
for (let i = 0; i <= 8; i++) {
if (i % 2 === 0) continue;
box(0.045, 0.05, 0.43, halfTickMaterial, valueToX(i / 2), 0.26, -0.48);
}
// All representable values are rendered as a discrete gold lattice.
const discreteGeometry = new THREE.BoxGeometry(0.035, 0.038, 0.20);
const discreteMaterial = flatMaterial(0xf6c85f, {
emissive: 0x5d4210,
emissiveIntensity: 0.34
});
let discreteMarks = null;
function rebuildDiscreteMarks() {
if (discreteMarks) {
scene.remove(discreteMarks);
if (typeof discreteMarks.dispose === "function") discreteMarks.dispose();
}
const count = Math.round((VALUE_MAX - VALUE_MIN) / precision) + 1;
discreteMarks = new THREE.InstancedMesh(
discreteGeometry,
discreteMaterial,
count
);
const matrix = new THREE.Matrix4();
for (let i = 0; i < count; i++) {
const value = VALUE_MIN + i * precision;
matrix.makeTranslation(valueToX(value), 0.267, 0.68);
discreteMarks.setMatrixAt(i, matrix);
}
discreteMarks.instanceMatrix.needsUpdate = true;
scene.add(discreteMarks);
}
rebuildDiscreteMarks();
// Continuous rail beside the discrete ruler.
const railMaterial = flatMaterial(0x142832, {
emissive: 0x0d3538,
emissiveIntensity: 0.25
});
box(12.1, 0.12, 0.38, railMaterial, 0, 0.22, 1.65);
box(12.1, 0.045, 0.035, flatMaterial(0x51e0d5), 0, 0.295, 1.84);
// Separate error lane in front of the continuous rail.
box(
12.05,
0.055,
0.22,
flatMaterial(0x321c28, {
emissive: 0x2a0c1b,
emissiveIntensity: 0.18
}),
0,
-0.225,
LANE_Z
);
const cyanMaterial = flatMaterial(0x51e0d5, {
emissive: 0x1b817c,
emissiveIntensity: 1.0
});
const goldMaterial = flatMaterial(0xf6c85f, {
emissive: 0x8a5c0b,
emissiveIntensity: 0.8
});
const pinkMaterial = flatMaterial(0xff5c8a, {
emissive: 0x9d173f,
emissiveIntensity: 0.9
});
// Cyan continuous marker.
const continuousMarker = new THREE.Group();
scene.add(continuousMarker);
const markerStem = new THREE.Mesh(
new THREE.CylinderGeometry(0.045, 0.045, 0.47, 6),
cyanMaterial
);
markerStem.position.y = 0.60;
markerStem.castShadow = true;
continuousMarker.add(markerStem);
const markerPointer = new THREE.Mesh(
new THREE.ConeGeometry(0.20, 0.36, 3),
cyanMaterial
);
markerPointer.rotation.z = Math.PI;
markerPointer.position.y = 0.43;
markerPointer.castShadow = true;
continuousMarker.add(markerPointer);
const markerRing = new THREE.Mesh(
new THREE.TorusGeometry(0.205, 0.035, 5, 12),
cyanMaterial
);
markerRing.rotation.x = Math.PI / 2;
markerRing.position.y = 0.80;
continuousMarker.add(markerRing);
// Invisible, enlarged hit target for dragging.
const markerHitTarget = new THREE.Mesh(
new THREE.SphereGeometry(0.42, 10, 8),
new THREE.MeshBasicMaterial({
transparent: true,
opacity: 0,
depthWrite: false
})
);
markerHitTarget.position.y = 0.55;
continuousMarker.add(markerHitTarget);
// Cyan projection from the continuous marker onto the ruler.
const projection = new THREE.Mesh(
new THREE.BoxGeometry(0.027, 0.032, 1.93),
new THREE.MeshBasicMaterial({
color: 0x51e0d5,
transparent: true,
opacity: 0.62,
depthWrite: false
})
);
projection.position.set(0, 0.275, -0.08);
scene.add(projection);
// Gold marker identifying the nearest representable value.
const quantizedMarker = new THREE.Group();
scene.add(quantizedMarker);
const quantizedGem = new THREE.Mesh(
new THREE.OctahedronGeometry(0.145, 0),
goldMaterial
);
quantizedGem.position.y = 0.43;
quantizedGem.castShadow = true;
quantizedMarker.add(quantizedGem);
const quantizedRing = new THREE.Mesh(
new THREE.TorusGeometry(0.145, 0.025, 5, 12),
goldMaterial
);
quantizedRing.rotation.x = Math.PI / 2;
quantizedRing.position.y = 0.29;
quantizedMarker.add(quantizedRing);
// Pink error segment and endpoints.
const errorGeometry = new THREE.CylinderGeometry(0.043, 0.043, 1, 8);
const errorLine = new THREE.Mesh(errorGeometry, pinkMaterial);
scene.add(errorLine);
const errorCapGeometry = new THREE.OctahedronGeometry(0.085, 0);
const errorCapA = new THREE.Mesh(errorCapGeometry, pinkMaterial);
const errorCapB = new THREE.Mesh(errorCapGeometry, pinkMaterial);
scene.add(errorCapA, errorCapB);
const errorLeader = new THREE.Mesh(
new THREE.CylinderGeometry(0.011, 0.011, 1, 6),
pinkMaterial
);
scene.add(errorLeader);
const continuousLabel = createLabel("CONTINUOUS", "#51e0d5");
continuousLabel.scale.set(1.65, 0.34, 1);
continuousLabel.position.set(-4.12, 0.86, 1.65);
scene.add(continuousLabel);
const errorLaneLabel = createLabel("ROUNDING ERROR", "#ff5c8a");
errorLaneLabel.scale.set(1.95, 0.34, 1);
errorLaneLabel.position.set(-4.12, 0.03, LANE_Z);
scene.add(errorLaneLabel);
const precisionLabel = createLabel("PRECISION 0.25", "#f6c85f");
precisionLabel.scale.set(1.95, 0.34, 1);
precisionLabel.position.set(-4.12, 0.88, -0.92);
scene.add(precisionLabel);
const errorLabel = createLabel("|Δ| 0.120", "#ff5c8a");
errorLabel.scale.set(1.48, 0.36, 1);
scene.add(errorLabel);
const up = new THREE.Vector3(0, 1, 0);
const horizontal = new THREE.Vector3(1, 0, 0);
function updateScene() {
const quantizedValue = quantize(continuousValue, precision);
const residual = continuousValue - quantizedValue;
const absoluteError = Math.abs(residual);
const continuousX = valueToX(continuousValue);
const quantizedX = valueToX(quantizedValue);
const errorDistance = Math.abs(continuousX - quantizedX);
const errorMidpoint = (continuousX + quantizedX) / 2;
continuousMarker.position.x = continuousX;
projection.position.x = continuousX;
quantizedMarker.position.x = quantizedX;
errorLine.position.set(errorMidpoint, LANE_Y, LANE_Z);
errorLine.scale.set(1, Math.max(errorDistance, 0.001), 1);
errorLine.quaternion.setFromUnitVectors(up, horizontal);
errorCapA.position.set(continuousX, LANE_Y, LANE_Z);
errorCapB.position.set(quantizedX, LANE_Y, LANE_Z);
errorLeader.position.set(errorMidpoint, 0.025, LANE_Z);
errorLeader.scale.y = 0.25;
errorLabel.position.set(errorMidpoint, 0.38, LANE_Z);
const digits = precisionDigits(precision);
const errorDigits = digits;
precisionText.textContent = precision.toFixed(digits);
continuousValueEl.textContent = continuousValue.toFixed(3);
quantizedValueEl.textContent = quantizedValue.toFixed(digits);
absoluteErrorEl.textContent = absoluteError.toFixed(errorDigits);
residualValueEl.textContent =
(residual >= 0 ? "+" : "−") + Math.abs(residual).toFixed(errorDigits);
updateLabel(precisionLabel, `PRECISION ${precision.toFixed(digits)}`);
updateLabel(errorLabel, `|Δ| ${absoluteError.toFixed(errorDigits)}`);
positionInput.value = continuousValue.toFixed(3);
}
precisionButtons.forEach((button) => {
button.addEventListener("click", () => {
precision = Number(button.dataset.precision);
precisionButtons.forEach((other) => {
other.setAttribute(
"aria-pressed",
String(other === button)
);
});
rebuildDiscreteMarks();
updateScene();
});
});
positionInput.addEventListener("input", () => {
continuousValue = THREE.MathUtils.clamp(
Number(positionInput.value),
VALUE_MIN,
VALUE_MAX
);
updateScene();
});
// Direct pointer controls: drag the marker, drag empty space to orbit,
// and use the wheel to zoom. No camera-control addons are used.
const raycaster = new THREE.Raycaster();
const pointer = new THREE.Vector2();
const dragPlane = new THREE.Plane(new THREE.Vector3(0, 1, 0), -0.55);
const planeIntersection = new THREE.Vector3();
let draggingMarker = false;
let orbiting = false;
let dragValueOffset = 0;
let previousPointerX = 0;
let previousPointerY = 0;
function setPointer(event) {
const rect = canvas.getBoundingClientRect();
pointer.x = ((event.clientX - rect.left) / rect.width) * 2 - 1;
pointer.y = -((event.clientY - rect.top) / rect.height) * 2 + 1;
raycaster.setFromCamera(pointer, camera);
}
canvas.addEventListener("contextmenu", (event) => event.preventDefault());
canvas.addEventListener("pointerdown", (event) => {
setPointer(event);
previousPointerX = event.clientX;
previousPointerY = event.clientY;
const hit = raycaster.intersectObject(markerHitTarget, false);
if (event.button === 0 && hit.length > 0) {
draggingMarker = true;
if (raycaster.ray.intersectPlane(dragPlane, planeIntersection)) {
dragValueOffset = continuousValue - xToValue(planeIntersection.x);
} else {
dragValueOffset = 0;
}
} else {
orbiting = true;
}
canvas.setPointerCapture(event.pointerId);
});
canvas.addEventListener("pointermove", (event) => {
if (draggingMarker) {
setPointer(event);
if (raycaster.ray.intersectPlane(dragPlane, planeIntersection)) {
continuousValue = THREE.MathUtils.clamp(
xToValue(planeIntersection.x) + dragValueOffset,
VALUE_MIN,
VALUE_MAX
);
updateScene();
}
return;
}
if (orbiting) {
const deltaX = event.clientX - previousPointerX;
const deltaY = event.clientY - previousPointerY;
cameraYaw -= deltaX * 0.006;
cameraPitch = THREE.MathUtils.clamp(
cameraPitch + deltaY * 0.0045,
0.30,
1.18
);
previousPointerX = event.clientX;
previousPointerY = event.clientY;
updateCamera();
}
});
function stopPointerInteraction(event) {
draggingMarker = false;
orbiting = false;
if (canvas.hasPointerCapture(event.pointerId)) {
canvas.releasePointerCapture(event.pointerId);
}
}
canvas.addEventListener("pointerup", stopPointerInteraction);
canvas.addEventListener("pointercancel", stopPointerInteraction);
canvas.addEventListener(
"wheel",
(event) => {
event.preventDefault();
cameraRadius = THREE.MathUtils.clamp(
cameraRadius + event.deltaY * 0.01,
9,
23
);
updateCamera();
},
{ passive: false }
);
function resetCamera() {
cameraYaw = 0;
cameraPitch = 0.63;
cameraRadius = 15.5;
updateCamera();
}
document.getElementById("resetCamera").addEventListener("click", resetCamera);
canvas.addEventListener("dblclick", resetCamera);
function resize() {
const width = window.innerWidth;
const height = window.innerHeight;
renderer.setSize(width, height, false);
camera.aspect = width / height;
camera.updateProjectionMatrix();
if (width < 760 && cameraRadius < 18) {
cameraRadius = 18;
updateCamera();
}
}
window.addEventListener("resize", resize);
resize();
updateScene();
renderer.setAnimationLoop(() => {
renderer.render(scene, camera);
});
</script>
</body>
</html>
```