Prompt specification
Build or customize the Three.js 3D scene "Summary-statistic selection explorer" 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: Summary-statistic selection explorer
Category: Algorithms & Systems · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/summary-statistic-selection-explorer-x1812
Scene Description Prompt:
Let users add or move observations and inspect mean, median, and range; explain each quantity directly and avoid automatically labeling every distant observation an error. Build this with Three.js from cdnjs as a low-poly scene in one self-contained HTML file.
Working Three.js Starter (summary-statistic-selection-explorer-x1812):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Observation Studio — Mean, Median & Range</title>
<style>
:root {
--navy: #101a2b;
--navy-2: #17253a;
--paper: #f4f2eb;
--panel: #fffdf8;
--ink: #172235;
--muted: #657084;
--line: #dfe2e5;
--blue: #63aef7;
--teal: #38c7b4;
--coral: #ff806d;
--gold: #f5c451;
--shadow: 0 18px 50px rgba(20, 31, 48, 0.13);
}
* {
box-sizing: border-box;
}
html,
body {
margin: 0;
min-height: 100%;
}
body {
color: var(--ink);
background: var(--paper);
font-family:
Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
button,
input {
font: inherit;
}
button {
cursor: pointer;
}
[hidden] {
display: none !important;
}
.app {
min-height: 100vh;
display: flex;
flex-direction: column;
}
.topbar {
min-height: 86px;
padding: 16px 24px;
display: flex;
align-items: center;
justify-content: space-between;
gap: 20px;
color: #f8fbff;
background:
radial-gradient(circle at 85% -50%, #355575 0, transparent 42%),
var(--navy);
border-bottom: 1px solid rgba(255, 255, 255, 0.08);
}
.brand {
display: flex;
align-items: center;
gap: 14px;
min-width: 0;
}
.brand-mark {
width: 36px;
height: 36px;
flex: 0 0 auto;
position: relative;
transform: rotate(45deg);
border-radius: 8px;
background: linear-gradient(135deg, var(--blue), var(--teal));
box-shadow: 0 0 30px rgba(99, 174, 247, 0.28);
}
.brand-mark::after {
content: "";
position: absolute;
inset: 9px;
border-radius: 3px;
background: var(--navy);
}
.brand h1 {
margin: 0;
font-size: clamp(1.1rem, 2vw, 1.45rem);
letter-spacing: -0.03em;
}
.brand p {
margin: 4px 0 0;
color: #aebed3;
font-size: 0.88rem;
}
.top-actions {
display: flex;
align-items: center;
gap: 10px;
flex-wrap: wrap;
justify-content: flex-end;
}
.button {
min-height: 42px;
padding: 9px 15px;
border: 1px solid transparent;
border-radius: 12px;
color: var(--ink);
background: #fff;
font-weight: 700;
transition:
transform 150ms ease,
box-shadow 150ms ease,
background 150ms ease;
}
.button:hover {
transform: translateY(-1px);
box-shadow: 0 7px 18px rgba(0, 0, 0, 0.14);
}
.button:active {
transform: translateY(0);
}
.button.primary {
color: #102033;
background: linear-gradient(135deg, #9dd4ff, #61e1cd);
box-shadow: 0 8px 22px rgba(55, 201, 185, 0.2);
}
.button.ghost {
color: #dce7f6;
background: rgba(255, 255, 255, 0.07);
border-color: rgba(255, 255, 255, 0.15);
}
.workspace {
flex: 1;
min-height: 0;
display: grid;
grid-template-columns: minmax(0, 1fr) 380px;
gap: 14px;
padding: 14px;
}
.scene-card {
position: relative;
min-height: 560px;
overflow: hidden;
border-radius: 24px;
background: var(--navy);
box-shadow: var(--shadow);
}
#viewport {
position: absolute;
inset: 0;
}
#viewport canvas {
width: 100%;
height: 100%;
display: block;
outline: none;
touch-action: none;
}
#viewport canvas:focus-visible {
outline: 3px solid rgba(154, 213, 255, 0.8);
outline-offset: -4px;
}
.scene-heading {
position: absolute;
top: 20px;
left: 22px;
z-index: 3;
color: #f5f9ff;
pointer-events: none;
}
.eyebrow {
margin: 0 0 5px;
color: #86a3c4;
font-size: 0.72rem;
font-weight: 800;
letter-spacing: 0.14em;
text-transform: uppercase;
}
.scene-heading h2 {
margin: 0;
font-size: clamp(1.25rem, 2.6vw, 2rem);
letter-spacing: -0.045em;
}
.scene-heading p {
margin: 6px 0 0;
color: #aebed0;
font-size: 0.86rem;
}
.zoom-controls {
position: absolute;
top: 18px;
right: 18px;
z-index: 4;
display: flex;
gap: 6px;
padding: 6px;
border: 1px solid rgba(255, 255, 255, 0.13);
border-radius: 13px;
background: rgba(10, 19, 32, 0.75);
backdrop-filter: blur(10px);
}
.zoom-controls button {
width: 34px;
height: 34px;
border: 0;
border-radius: 9px;
color: #eaf3ff;
background: rgba(255, 255, 255, 0.08);
font-size: 1.1rem;
font-weight: 800;
}
.zoom-controls button:hover {
background: rgba(255, 255, 255, 0.16);
}
.zoom-controls .reset-view {
width: auto;
padding: 0 10px;
font-size: 0.75rem;
}
.legend {
position: absolute;
left: 18px;
bottom: 58px;
z-index: 3;
display: flex;
flex-wrap: wrap;
gap: 8px 13px;
max-width: min(600px, calc(100% - 36px));
padding: 10px 12px;
border: 1px solid rgba(255, 255, 255, 0.1);
border-radius: 13px;
color: #c8d5e5;
background: rgba(8, 16, 28, 0.73);
backdrop-filter: blur(10px);
font-size: 0.75rem;
pointer-events: none;
}
.legend-item {
display: inline-flex;
align-items: center;
gap: 6px;
}
.swatch {
width: 9px;
height: 9px;
border-radius: 3px;
transform: rotate(45deg);
}
.swatch.observation {
background: var(--blue);
}
.swatch.separated {
background: var(--gold);
}
.swatch.mean {
background: var(--coral);
}
.swatch.median {
background: var(--teal);
}
.swatch.range {
height: 4px;
border-radius: 4px;
transform: none;
background: var(--gold);
}
.scene-hint {
position: absolute;
left: 18px;
right: 18px;
bottom: 16px;
z-index: 3;
color: #91a8c4;
font-size: 0.76rem;
text-align: center;
pointer-events: none;
}
.toast {
position: absolute;
left: 50%;
top: 88px;
z-index: 8;
max-width: min(420px, calc(100% - 30px));
padding: 11px 15px;
border: 1px solid rgba(255, 255, 255, 0.15);
border-radius: 12px;
color: #f7fbff;
background: rgba(8, 17, 30, 0.9);
box-shadow: 0 12px 35px rgba(0, 0, 0, 0.25);
font-size: 0.83rem;
opacity: 0;
transform: translate(-50%, -8px);
transition:
opacity 180ms ease,
transform 180ms ease;
pointer-events: none;
}
.toast.visible {
opacity: 1;
transform: translate(-50%, 0);
}
.sidebar {
min-height: 0;
overflow-y: auto;
padding: 18px;
border: 1px solid rgba(35, 48, 66, 0.08);
border-radius: 24px;
background: var(--panel);
box-shadow: var(--shadow);
}
.section-heading {
display: flex;
align-items: baseline;
justify-content: space-between;
gap: 12px;
margin-bottom: 12px;
}
.section-heading h2,
.section-heading h3 {
margin: 0;
letter-spacing: -0.025em;
}
.section-heading h2 {
font-size: 1.12rem;
}
.section-heading h3 {
font-size: 0.96rem;
}
.count-pill {
padding: 4px 9px;
border-radius: 999px;
color: #496179;
background: #edf3f8;
font-size: 0.74rem;
font-weight: 800;
}
.stats {
display: grid;
gap: 10px;
}
.stat-card {
--accent: var(--blue);
position: relative;
overflow: hidden;
padding: 13px 14px 13px 17px;
border: 1px solid #e2e5e6;
border-radius: 16px;
background: #fff;
outline: none;
cursor: pointer;
transition:
transform 150ms ease,
border-color 150ms ease,
box-shadow 150ms ease;
}
.stat-card::before {
content: "";
position: absolute;
inset: 0 auto 0 0;
width: 4px;
background: var(--accent);
}
.stat-card:hover,
.stat-card:focus-visible,
.stat-card.active {
border-color: color-mix(in srgb, var(--accent) 55%, #e2e5e6);
box-shadow: 0 8px 24px rgba(27, 40, 58, 0.08);
transform: translateY(-1px);
}
.stat-card[data-focus="mean"] {
--accent: var(--coral);
}
.stat-card[data-focus="median"] {
--accent: var(--teal);
}
.stat-card[data-focus="range"] {
--accent: var(--gold);
}
.stat-top {
display: flex;
align-items: center;
justify-content: space-between;
gap: 12px;
}
.stat-name {
color: var(--muted);
font-size: 0.76rem;
font-weight: 800;
letter-spacing: 0.1em;
text-transform: uppercase;
}
.stat-value {
color: var(--accent);
font-size: 1.55rem;
font-weight: 850;
letter-spacing: -0.055em;
font-variant-numeric: tabular-nums;
}
.formula {
margin-top: 2px;
color: #536076;
font-size: 0.78rem;
font-variant-numeric: tabular-nums;
}
.explanation {
margin: 8px 0 0;
color: #515d70;
font-size: 0.79rem;
line-height: 1.45;
}
.divider {
height: 1px;
margin: 17px 0;
background: #e6e7e5;
}
.input-row {
display: grid;
grid-template-columns: 1fr auto;
gap: 8px;
}
.number-input {
min-width: 0;
height: 42px;
padding: 0 11px;
border: 1px solid #d9dee3;
border-radius: 11px;
color: var(--ink);
background: #fff;
}
.number-input:focus {
border-color: var(--blue);
outline: 3px solid rgba(99, 174, 247, 0.15);
}
.compact-note {
margin: 7px 0 0;
color: #7a8493;
font-size: 0.72rem;
}
.selected-box {
margin-top: 13px;
padding: 12px;
border: 1px solid #dbe7f1;
border-radius: 14px;
background: #f3f8fc;
}
.selected-head {
display: flex;
align-items: center;
justify-content: space-between;
gap: 10px;
font-size: 0.78rem;
color: #617085;
}
.selected-head strong {
color: #236ca8;
font-size: 1.05rem;
font-variant-numeric: tabular-nums;
}
.slider {
width: 100%;
margin: 11px 0 4px;
accent-color: #438fcf;
}
.remove-button {
padding: 3px 7px;
border: 0;
border-radius: 7px;
color: #98604f;
background: #f9e9e2;
font-size: 0.69rem;
font-weight: 800;
}
.data-list {
display: grid;
gap: 6px;
max-height: 175px;
overflow-y: auto;
padding: 2px;
}
.observation-row {
width: 100%;
padding: 9px 10px;
display: flex;
align-items: center;
justify-content: space-between;
gap: 10px;
border: 1px solid #e2e5e6;
border-radius: 11px;
color: var(--ink);
background: #fff;
text-align: left;
}
.observation-row:hover {
border-color: #b8cee1;
}
.observation-row.selected {
border-color: #65a9dc;
background: #eef7fd;
}
.observation-name {
display: flex;
align-items: center;
gap: 8px;
font-size: 0.79rem;
font-weight: 700;
}
.observation-dot {
width: 8px;
height: 8px;
border-radius: 3px;
transform: rotate(45deg);
background: var(--blue);
}
.observation-dot.separated {
background: var(--gold);
}
.observation-value {
font-size: 0.88rem;
font-weight: 850;
font-variant-numeric: tabular-nums;
}
.separated-badge {
padding: 2px 6px;
border-radius: 999px;
color: #806319;
background: #fff3c8;
font-size: 0.62rem;
font-weight: 800;
}
.empty-list {
padding: 16px 10px;
border: 1px dashed #d6dadd;
border-radius: 12px;
color: #7c8593;
font-size: 0.78rem;
text-align: center;
}
.context-box {
margin-top: 14px;
padding: 12px;
border: 1px solid #f0dfaa;
border-radius: 14px;
background: #fff9e9;
}
.context-box h3 {
margin: 0 0 5px;
color: #715a1d;
font-size: 0.83rem;
}
.context-box p {
margin: 0;
color: #746947;
font-size: 0.75rem;
line-height: 1.48;
}
@media (max-width: 920px) {
.app {
height: auto;
}
.topbar {
align-items: flex-start;
flex-direction: column;
}
.top-actions {
width: 100%;
justify-content: flex-start;
}
.workspace {
grid-template-columns: 1fr;
}
.scene-card {
min-height: 500px;
}
.sidebar {
overflow: visible;
}
}
@media (max-width: 520px) {
.topbar {
padding: 15px;
}
.workspace {
padding: 8px;
gap: 8px;
}
.scene-card {
min-height: 460px;
border-radius: 18px;
}
.sidebar {
padding: 15px;
border-radius: 18px;
}
.scene-heading {
top: 15px;
left: 16px;
}
.scene-heading p {
max-width: 210px;
}
.zoom-controls {
top: 13px;
right: 13px;
}
.zoom-controls .reset-view {
display: none;
}
.legend {
left: 10px;
bottom: 53px;
max-width: calc(100% - 20px);
gap: 6px 10px;
padding: 8px 10px;
}
.scene-hint {
left: 10px;
right: 10px;
bottom: 13px;
font-size: 0.68rem;
}
}
</style>
</head>
<body>
<div class="app">
<header class="topbar">
<div class="brand">
<div class="brand-mark" aria-hidden="true"></div>
<div>
<h1>Observation Studio</h1>
<p>Build a dataset, move its values, and investigate its center and spread.</p>
</div>
</div>
<div class="top-actions">
<button class="button primary" id="addObservation" type="button">
+ Add observation
</button>
<button class="button ghost" id="resetDataset" type="button">
Reset example
</button>
</div>
</header>
<main class="workspace">
<section class="scene-card" aria-label="Interactive three-dimensional number line">
<div id="viewport"></div>
<div class="scene-heading">
<p class="eyebrow">Interactive number line</p>
<h2>Explore the dataset</h2>
<p>Each faceted marker represents one observation.</p>
</div>
<div class="zoom-controls" aria-label="Camera controls">
<button id="zoomIn" type="button" aria-label="Zoom in">+</button>
<button id="zoomOut" type="button" aria-label="Zoom out">−</button>
<button
class="reset-view"
id="resetView"
type="button"
aria-label="Reset camera view"
>
Reset view
</button>
</div>
<div class="legend" aria-label="Scene legend">
<span class="legend-item">
<span class="swatch observation"></span>
Observation
</span>
<span class="legend-item">
<span class="swatch separated"></span>
Separated value
</span>
<span class="legend-item">
<span class="swatch mean"></span>
Mean
</span>
<span class="legend-item">
<span class="swatch median"></span>
Median
</span>
<span class="legend-item">
<span class="swatch range"></span>
Range
</span>
</div>
<div class="scene-hint" id="sceneHint">
Drag a marker to move it · Drag the background to orbit · Scroll to zoom · Double-click the line to add
</div>
<div class="toast" id="toast" role="status" aria-live="polite"></div>
</section>
<aside class="sidebar">
<div class="section-heading">
<h2>What the numbers say</h2>
<span class="count-pill" id="observationCount">5 observations</span>
</div>
<div class="stats" aria-live="polite">
<article
class="stat-card"
data-focus="mean"
tabindex="0"
role="button"
aria-label="Inspect the mean"
>
<div class="stat-top">
<span class="stat-name">Mean</span>
<span class="stat-value" id="meanValue">4.8</span>
</div>
<div class="formula" id="meanFormula"></div>
<p class="explanation">
Add every observation, then divide by the number of observations.
It uses every value, so moving one observation also changes it.
</p>
</article>
<article
class="stat-card"
data-focus="median"
tabindex="0"
role="button"
aria-label="Inspect the median"
>
<div class="stat-top">
<span class="stat-name">Median</span>
<span class="stat-value" id="medianValue">4</span>
</div>
<div class="formula" id="medianFormula"></div>
<p class="explanation">
Sort the values and find the middle one. With an even number of
values, average the two middle values. It describes the center
without being pulled around by a distant value.
</p>
</article>
<article
class="stat-card"
data-focus="range"
tabindex="0"
role="button"
aria-label="Inspect the range"
>
<div class="stat-top">
<span class="stat-name">Range</span>
<span class="stat-value" id="rangeValue">6</span>
</div>
<div class="formula" id="rangeFormula"></div>
<p class="explanation">
Subtract the smallest observation from the largest. The range
measures the total span, but it does not describe how the values
are distributed within that span.
</p>
</article>
</div>
<div class="divider"></div>
<div class="section-heading">
<h3>Add an exact value</h3>
</div>
<div class="input-row">
<input
class="number-input"
id="addValue"
type="number"
min="-10"
max="10"
step="0.1"
value="0"
aria-label="Observation value to add"
>
<button class="button" id="addExact" type="button">Add value</button>
</div>
<p class="compact-note">Values may range from −10 to 10.</p>
<div class="selected-box" id="selectedBox" hidden>
<div class="selected-head">
<span>
Selected: <strong id="selectedName">Observation</strong>
</span>
<strong id="selectedValue">0</strong>
</div>
<input
class="slider"
id="valueSlider"
type="range"
min="-10"
max="10"
step="0.1"
value="0"
aria-label="Move the selected observation"
>
<div class="selected-head">
<span>Drag the marker or use this slider.</span>
<button class="remove-button" id="removeSelected" type="button">
Remove
</button>
</div>
</div>
<div class="divider"></div>
<div class="section-heading">
<h3>Observations</h3>
<span class="count-pill" id="listCount">5</span>
</div>
<div class="data-list" id="dataList"></div>
<div class="context-box">
<h3 id="contextTitle">Distance is not a verdict</h3>
<p id="contextCopy">
Amber is reserved for values unusually separated from the middle of
the dataset. It is a prompt to investigate context, not an automatic
label that a value is wrong.
</p>
</div>
</aside>
</main>
</div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
const $ = (id) => document.getElementById(id);
const viewport = $("viewport");
const DOMAIN = 10;
const SCALE = 1.04;
const MAX_OBSERVATIONS = 24;
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x101a2b);
const renderer = new THREE.WebGLRenderer({
antialias: true,
alpha: false,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio || 1, 2));
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.12;
viewport.appendChild(renderer.domElement);
renderer.domElement.tabIndex = 0;
renderer.domElement.setAttribute(
"aria-label",
"Three-dimensional number line. Use the controls beside the scene to add and move observations."
);
const camera = new THREE.OrthographicCamera(-14, 14, 9, -9, 0.1, 100);
const cameraTarget = new THREE.Vector3(0, 0.45, 0.45);
const orbit = {
theta: 0.52,
phi: 0.96,
zoom: 1
};
function updateCamera() {
const radius = 25;
const sinPhi = Math.sin(orbit.phi);
camera.position.set(
cameraTarget.x + radius * sinPhi * Math.sin(orbit.theta),
cameraTarget.y + radius * Math.cos(orbit.phi),
cameraTarget.z + radius * sinPhi * Math.cos(orbit.theta)
);
camera.lookAt(cameraTarget);
camera.zoom = orbit.zoom;
camera.updateProjectionMatrix();
}
updateCamera();
const hemisphere = new THREE.HemisphereLight(0xc7e8ff, 0x152039, 2.3);
scene.add(hemisphere);
const keyLight = new THREE.DirectionalLight(0xffffff, 3.1);
keyLight.position.set(-7, 13, 9);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(2048, 2048);
keyLight.shadow.camera.left = -15;
keyLight.shadow.camera.right = 15;
keyLight.shadow.camera.top = 15;
keyLight.shadow.camera.bottom = -15;
keyLight.shadow.camera.near = 1;
keyLight.shadow.camera.far = 45;
scene.add(keyLight);
const rimLight = new THREE.DirectionalLight(0xff8e7b, 1.15);
rimLight.position.set(9, 5, -8);
scene.add(rimLight);
const platform = new THREE.Mesh(
new THREE.CylinderGeometry(12.8, 12.8, 0.45, 12),
new THREE.MeshStandardMaterial({
color: 0x18283d,
roughness: 0.88,
metalness: 0.05,
flatShading: true
})
);
platform.scale.z = 0.38;
platform.position.y = -0.42;
platform.receiveShadow = true;
scene.add(platform);
const axisMaterial = new THREE.MeshStandardMaterial({
color: 0x9ed8ff,
emissive: 0x173a58,
emissiveIntensity: 0.55,
roughness: 0.45
});
const axisMesh = new THREE.Mesh(
new THREE.BoxGeometry(21.5, 0.12, 0.22),
axisMaterial
);
axisMesh.position.y = 0.02;
axisMesh.receiveShadow = true;
scene.add(axisMesh);
const tickGeometry = new THREE.BoxGeometry(0.035, 0.2, 0.42);
const majorTickGeometry = new THREE.BoxGeometry(0.06, 0.3, 0.58);
function roundedRect(ctx, x, y, width, height, radius) {
const r = Math.min(radius, width / 2, height / 2);
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 createTextSprite(text, color = "#ffffff", background = null, fontSize = 54) {
const canvas = document.createElement("canvas");
canvas.width = 512;
canvas.height = 160;
const texture = new THREE.CanvasTexture(canvas);
texture.colorSpace = THREE.SRGBColorSpace;
texture.minFilter = THREE.LinearFilter;
texture.magFilter = THREE.LinearFilter;
const material = new THREE.SpriteMaterial({
map: texture,
transparent: true,
depthTest: false,
depthWrite: false
});
const sprite = new THREE.Sprite(material);
sprite.renderOrder = 20;
sprite.userData.canvas = canvas;
sprite.userData.ctx = canvas.getContext("2d");
sprite.userData.texture = texture;
updateTextSprite(sprite, text, color, background, fontSize);
return sprite;
}
function updateTextSprite(
sprite,
text,
color = "#ffffff",
background = null,
fontSize = 54
) {
const { canvas, ctx, texture } = sprite.userData;
ctx.clearRect(0, 0, canvas.width, canvas.height);
if (background) {
roundedRect(ctx, 12, 18, 488, 124, 30);
ctx.fillStyle = background;
ctx.fill();
ctx.strokeStyle = "rgba(255,255,255,0.14)";
ctx.lineWidth = 4;
ctx.stroke();
}
ctx.fillStyle = color;
ctx.font = `700 ${fontSize}px ui-sans-serif, system-ui, sans-serif`;
ctx.textAlign = "center";
ctx.textBaseline = "middle";
ctx.fillText(text, canvas.width / 2, canvas.height / 2 + 2);
texture.needsUpdate = true;
}
for (let value = -DOMAIN; value <= DOMAIN; value++) {
const major = value % 5 === 0;
const tick = new THREE.Mesh(
major ? majorTickGeometry : tickGeometry,
new THREE.MeshStandardMaterial({
color: major ? 0xd7f0ff : 0x6e9bbd,
roughness: 0.55
})
);
tick.position.set(value * SCALE, 0.07, 0);
scene.add(tick);
const label = createTextSprite(
String(value),
major ? "#cfe5f7" : "#819bb5",
null,
48
);
label.position.set(value * SCALE, -0.26, 0.92);
label.scale.set(0.58, 0.25, 1);
scene.add(label);
}
// Mean marker.
const meanMarker = new THREE.Group();
const meanDiamond = new THREE.Mesh(
new THREE.OctahedronGeometry(0.23, 0),
new THREE.MeshStandardMaterial({
color: 0xff806d,
emissive: 0x5f1b16,
emissiveIntensity: 0.7,
roughness: 0.38,
flatShading: true
})
);
meanDiamond.position.y = 0.31;
meanDiamond.castShadow = true;
meanMarker.add(meanDiamond);
const meanLabel = createTextSprite(
"Mean",
"#ffd7cf",
"rgba(79,28,25,0.9)",
46
);
meanLabel.position.set(0, 0.82, 1.08);
meanLabel.scale.set(1.65, 0.48, 1);
meanMarker.add(meanLabel);
scene.add(meanMarker);
// Median marker.
const medianMarker = new THREE.Group();
const medianShape = new THREE.Mesh(
new THREE.CylinderGeometry(0.2, 0.2, 0.2, 6),
new THREE.MeshStandardMaterial({
color: 0x38c7b4,
emissive: 0x0d4e48,
emissiveIntensity: 0.75,
roughness: 0.4,
flatShading: true
})
);
medianShape.position.y = 0.3;
medianShape.castShadow = true;
medianMarker.add(medianShape);
const medianLabel = createTextSprite(
"Median",
"#c8fff7",
"rgba(10,67,62,0.9)",
46
);
medianLabel.position.set(0, 0.82, 1.58);
medianLabel.scale.set(1.65, 0.48, 1);
medianMarker.add(medianLabel);
scene.add(medianMarker);
// Range rail.
const rangeGroup = new THREE.Group();
const rangeMaterial = new THREE.MeshStandardMaterial({
color: 0xf5c451,
emissive: 0x5b430b,
emissiveIntensity: 0.55,
roughness: 0.48
});
const rangeBar = new THREE.Mesh(
new THREE.BoxGeometry(1, 0.075, 0.075),
rangeMaterial
);
rangeBar.position.y = 0.28;
rangeGroup.add(rangeBar);
const rangeCapLeft = new THREE.Mesh(
new THREE.BoxGeometry(0.075, 0.5, 0.075),
rangeMaterial
);
const rangeCapRight = rangeCapLeft.clone();
rangeCapLeft.position.y = 0.28;
rangeCapRight.position.y = 0.28;
rangeGroup.add(rangeCapLeft, rangeCapRight);
const rangeLabel = createTextSprite(
"Range",
"#fff0b9",
"rgba(79,58,9,0.9)",
46
);
rangeLabel.position.set(0, 0.8, 2.08);
rangeLabel.scale.set(1.75, 0.48, 1);
rangeGroup.add(rangeLabel);
scene.add(rangeGroup);
let observations = [];
let nextObservationNumber = 1;
let selectedId = null;
let focusKey = null;
let toastTimer = null;
const observationGroups = new Map();
const raycaster = new THREE.Raycaster();
const pointer = new THREE.Vector2();
const dragPlane = new THREE.Plane(new THREE.Vector3(0, 1, 0), -1.08);
const planeHit = new THREE.Vector3();
let dragState = null;
let orbitDrag = null;
function formatNumber(value) {
const rounded = Math.abs(value) < 0.00001 ? 0 : value;
return Number.isInteger(rounded)
? String(rounded)
: rounded.toFixed(1);
}
function roundValue(value) {
return Math.round(value * 10) / 10;
}
function showToast(message) {
const toast = $("toast");
toast.textContent = message;
toast.classList.add("visible");
clearTimeout(toastTimer);
toastTimer = setTimeout(() => {
toast.classList.remove("visible");
}, 2200);
}
function quantile(sorted, probability) {
if (!sorted.length) return 0;
const position = (sorted.length - 1) * probability;
const lower = Math.floor(position);
const upper = Math.ceil(position);
if (lower === upper) return sorted[lower];
const fraction = position - lower;
return sorted[lower] + (sorted[upper] - sorted[lower]) * fraction;
}
function calculateStatistics() {
const values = observations.map((observation) => observation.value);
const sorted = [...values].sort((a, b) => a - b);
const count = values.length;
if (!count) {
return {
count: 0,
sorted,
sum: 0,
mean: 0,
median: 0,
min: 0,
max: 0,
range: 0
};
}
const sum = values.reduce((total, value) => total + value, 0);
const mean = sum / count;
const middle = Math.floor(count / 2);
const median =
count % 2 === 0
? (sorted[middle - 1] + sorted[middle]) / 2
: sorted[middle];
const min = sorted[0];
const max = sorted[sorted.length - 1];
return {
count,
sorted,
sum,
mean,
median,
min,
max,
range: max - min
};
}
function findSeparatedIds() {
const separated = new Set();
const stats = calculateStatistics();
if (stats.count < 4) return separated;
const q1 = quantile(stats.sorted, 0.25);
const q3 = quantile(stats.sorted, 0.75);
const iqr = q3 - q1;
for (const observation of observations) {
if (iqr > 0) {
const lowerFence = q1 - 1.5 * iqr;
const upperFence = q3 + 1.5 * iqr;
if (
observation.value < lowerFence ||
observation.value > upperFence
) {
separated.add(observation.id);
}
} else {
// With no quartile spread, only highlight values that are
// substantially separated from both the center and total range.
const threshold = Math.max(2, stats.range * 0.35);
if (
Math.abs(observation.value - stats.median) >= threshold
) {
separated.add(observation.id);
}
}
}
return separated;
}
function createObservationGroup(observation) {
const group = new THREE.Group();
group.userData.obsId = observation.id;
const stemMaterial = new THREE.MeshStandardMaterial({
color: 0x63aef7,
roughness: 0.65,
metalness: 0.05
});
const stem = new THREE.Mesh(
new THREE.CylinderGeometry(0.045, 0.065, 0.78, 6),
stemMaterial
);
stem.position.y = 0.57;
stem.castShadow = true;
group.add(stem);
const marker = new THREE.Mesh(
new THREE.IcosahedronGeometry(0.3, 0),
new THREE.MeshStandardMaterial({
color: 0x63aef7,
emissive: 0x173a58,
emissiveIntensity: 0.65,
roughness: 0.4,
metalness: 0.03,
flatShading: true
})
);
marker.position.y = 1.08;
marker.castShadow = true;
group.add(marker);
const selectionRing = new THREE.Mesh(
new THREE.TorusGeometry(0.38, 0.035, 6, 20),
new THREE.MeshBasicMaterial({
color: 0xe8f8ff,
transparent: true,
opacity: 0.95,
depthTest: false
})
);
selectionRing.rotation.x = Math.PI / 2;
selectionRing.position.y = 1.08;
selectionRing.renderOrder = 15;
group.add(selectionRing);
const label = createTextSprite(
formatNumber(observation.value),
"#eef8ff",
"rgba(12,28,48,0.9)",
50
);
label.position.set(0, 1.56, 0);
label.scale.set(1.02, 0.34, 1);
group.add(label);
group.userData.marker = marker;
group.userData.stem = stem;
group.userData.selectionRing = selectionRing;
group.userData.label = label;
scene.add(group);
observationGroups.set(observation.id, group);
}
function syncObservationGroups(separatedIds) {
const currentIds = new Set(observations.map((observation) => observation.id));
for (const [id, group] of observationGroups) {
if (!currentIds.has(id)) {
scene.remove(group);
observationGroups.delete(id);
}
}
const duplicateOffsets = new Map();
for (const observation of observations) {
if (!observationGroups.has(observation.id)) {
createObservationGroup(observation);
}
const group = observationGroups.get(observation.id);
const isSelected = observation.id === selectedId;
const isSeparated = separatedIds.has(observation.id);
const color = isSeparated ? 0xf5c451 : 0x63aef7;
group.position.x = observation.value * SCALE;
group.userData.marker.material.color.setHex(color);
group.userData.marker.material.emissive.setHex(
isSeparated ? 0x5b430b : 0x173a58
);
group.userData.stem.material.color.setHex(color);
group.userData.selectionRing.visible = isSelected;
updateTextSprite(
group.userData.label,
formatNumber(observation.value),
isSeparated ? "#fff0b9" : "#eef8ff",
isSeparated ? "rgba(79,58,9,0.92)" : "rgba(12,28,48,0.9)",
50
);
const duplicateKey = formatNumber(observation.value);
const duplicateIndex = duplicateOffsets.get(duplicateKey) || 0;
duplicateOffsets.set(duplicateKey, duplicateIndex + 1);
group.userData.label.position.y = 1.56 + duplicateIndex * 0.32;
}
}
function updateGizmos(stats) {
meanMarker.visible = stats.count > 0;
medianMarker.visible = stats.count > 0;
rangeGroup.visible = stats.count > 0;
if (!stats.count) return;
meanMarker.position.x = stats.mean * SCALE;
medianMarker.position.x = stats.median * SCALE;
updateTextSprite(
meanLabel,
`Mean ${formatNumber(stats.mean)}`,
"#ffd7cf",
"rgba(79,28,25,0.92)",
44
);
updateTextSprite(
medianLabel,
`Median ${formatNumber(stats.median)}`,
"#c8fff7",
"rgba(10,67,62,0.92)",
44
);
const minimumX = stats.min * SCALE;
const maximumX = stats.max * SCALE;
const middleX = (minimumX + maximumX) / 2;
rangeBar.position.x = middleX;
rangeBar.scale.x = Math.max(0.001, maximumX - minimumX);
rangeCapLeft.position.x = minimumX;
rangeCapRight.position.x = maximumX;
rangeLabel.position.x = middleX;
updateTextSprite(
rangeLabel,
`Range ${formatNumber(stats.range)}`,
"#fff0b9",
"rgba(79,58,9,0.92)",
44
);
}
function renderDataList(separatedIds) {
const list = $("dataList");
list.replaceChildren();
$("listCount").textContent = String(observations.length);
if (!observations.length) {
const empty = document.createElement("div");
empty.className = "empty-list";
empty.textContent = "Add an observation to begin.";
list.appendChild(empty);
return;
}
for (const observation of observations) {
const button = document.createElement("button");
button.type = "button";
button.className = "observation-row";
if (observation.id === selectedId) button.classList.add("selected");
const name = document.createElement("span");
name.className = "observation-name";
const dot = document.createElement("span");
dot.className = "observation-dot";
if (separatedIds.has(observation.id)) {
dot.classList.add("separated");
}
const label = document.createElement("span");
label.textContent = observation.label;
name.append(dot, label);
const right = document.createElement("span");
right.style.display = "inline-flex";
right.style.alignItems = "center";
right.style.gap = "7px";
if (separatedIds.has(observation.id)) {
const badge = document.createElement("span");
badge.className = "separated-badge";
badge.textContent = "separated";
right.appendChild(badge);
}
const value = document.createElement("span");
value.className = "observation-value";
value.textContent = formatNumber(observation.value);
right.appendChild(value);
button.append(name, right);
button.addEventListener("click", () => {
selectedId = observation.id;
updateAll();
});
list.appendChild(button);
}
}
function updateSelectedPanel() {
const selected = observations.find(
(observation) => observation.id === selectedId
);
$("selectedBox").hidden = !selected;
if (!selected) return;
$("selectedName").textContent = selected.label;
$("selectedValue").textContent = formatNumber(selected.value);
$("valueSlider").value = String(selected.value);
}
function updateStatisticsPanel(stats, separatedIds) {
$("observationCount").textContent =
`${stats.count} observation${stats.count === 1 ? "" : "s"}`;
$("meanValue").textContent = formatNumber(stats.mean);
$("medianValue").textContent = formatNumber(stats.median);
$("rangeValue").textContent = formatNumber(stats.range);
$("meanFormula").textContent = stats.count
? `${formatNumber(stats.sum)} ÷ ${stats.count} = ${formatNumber(stats.mean)}`
: "No observations yet";
if (!stats.count) {
$("medianFormula").textContent = "No observations yet";
$("rangeFormula").textContent = "No observations yet";
} else if (stats.count % 2 === 0) {
const middle = Math.floor(stats.count / 2);
$("medianFormula").textContent =
`(${formatNumber(stats.sorted[middle - 1])} + ` +
`${formatNumber(stats.sorted[middle])}) ÷ 2 = ` +
`${formatNumber(stats.median)}`;
} else {
const middle = Math.floor(stats.count / 2);
$("medianFormula").textContent =
`Middle sorted value = ${formatNumber(stats.sorted[middle])}`;
}
$("rangeFormula").textContent = stats.count
? `${formatNumber(stats.max)} − ${formatNumber(stats.min)} = ` +
`${formatNumber(stats.range)}`
: "No observations yet";
const separatedCount = separatedIds.size;
if (separatedCount) {
$("contextTitle").textContent =
`${separatedCount} separated value${separatedCount === 1 ? " is" : "s are"} highlighted`;
$("contextCopy").textContent =
"Amber marks a value unusually separated from the middle of this dataset. " +
"That is a prompt to inspect the measurement and its context—not an automatic " +
"judgment that it is wrong. A distant value may be correct and important.";
} else {
$("contextTitle").textContent = "Distance is not a verdict";
$("contextCopy").textContent =
"Amber is reserved for values unusually separated from the middle of the " +
"dataset. It is a prompt to investigate context, not an automatic label " +
"that a value is wrong.";
}
}
function updateAll() {
const stats = calculateStatistics();
const separatedIds = findSeparatedIds();
syncObservationGroups(separatedIds);
updateGizmos(stats);
updateStatisticsPanel(stats, separatedIds);
renderDataList(separatedIds);
updateSelectedPanel();
}
function addObservation(value) {
if (observations.length >= MAX_OBSERVATIONS) {
showToast(`This scene supports up to ${MAX_OBSERVATIONS} observations.`);
return;
}
const clipped = roundValue(
THREE.MathUtils.clamp(Number(value), -DOMAIN, DOMAIN)
);
const observation = {
id: `obs-${Date.now().toString(36)}-${Math.random().toString(36).slice(2)}`,
label: `Obs ${nextObservationNumber++}`,
value: clipped
};
observations.push(observation);
selectedId = observation.id;
focusKey = null;
updateAll();
showToast(`Added ${observation.label} at ${formatNumber(clipped)}.`);
}
function removeSelected() {
if (!selectedId) return;
const index = observations.findIndex(
(observation) => observation.id === selectedId
);
if (index === -1) return;
const [removed] = observations.splice(index, 1);
selectedId = observations.length
? observations[Math.min(index, observations.length - 1)].id
: null;
updateAll();
showToast(`Removed ${removed.label}.`);
}
function resetDataset() {
observations = [
{ id: "sample-1", label: "Obs 1", value: 2 },
{ id: "sample-2", label: "Obs 2", value: 4 },
{ id: "sample-3", label: "Obs 3", value: 4 },
{ id: "sample-4", label: "Obs 4", value: 6 },
{ id: "sample-5", label: "Obs 5", value: 8 }
];
nextObservationNumber = 6;
selectedId = null;
focusKey = null;
document.querySelectorAll(".stat-card").forEach((card) => {
card.classList.remove("active");
});
$("sceneHint").textContent =
"Drag a marker to move it · Drag the background to orbit · Scroll to zoom · Double-click the line to add";
updateAll();
showToast("Example dataset restored.");
}
function setPointer(event) {
const rect = renderer.domElement.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);
}
function getObservationHit(event) {
setPointer(event);
const intersections = raycaster.intersectObjects(
[...observationGroups.values()],
true
);
for (const intersection of intersections) {
let object = intersection.object;
while (object && !object.userData.obsId) {
object = object.parent;
}
if (object?.userData.obsId) {
return {
id: object.userData.obsId,
point: intersection.point
};
}
}
return null;
}
renderer.domElement.addEventListener("pointerdown", (event) => {
if (event.button !== 0) return;
renderer.domElement.focus({ preventScroll: true });
const hit = getObservationHit(event);
if (hit) {
selectedId = hit.id;
const observation = observations.find(
(item) => item.id === selectedId
);
if (raycaster.ray.intersectPlane(dragPlane, planeHit)) {
dragState = {
id: selectedId,
pointerId: event.pointerId,
offset: observation.value * SCALE - planeHit.x
};
}
renderer.domElement.setPointerCapture(event.pointerId);
renderer.domElement.style.cursor = "grabbing";
updateAll();
event.preventDefault();
return;
}
orbitDrag = {
pointerId: event.pointerId,
x: event.clientX,
y: event.clientY,
theta: orbit.theta,
phi: orbit.phi
};
renderer.domElement.setPointerCapture(event.pointerId);
renderer.domElement.style.cursor = "grabbing";
});
renderer.domElement.addEventListener("pointermove", (event) => {
if (dragState && event.pointerId === dragState.pointerId) {
setPointer(event);
if (raycaster.ray.intersectPlane(dragPlane, planeHit)) {
const observation = observations.find(
(item) => item.id === dragState.id
);
if (observation) {
const value = roundValue(
THREE.MathUtils.clamp(
(planeHit.x + dragState.offset) / SCALE,
-DOMAIN,
DOMAIN
)
);
observation.value = value;
updateAll();
}
}
event.preventDefault();
return;
}
if (orbitDrag && event.pointerId === orbitDrag.pointerId) {
const deltaX = event.clientX - orbitDrag.x;
const deltaY = event.clientY - orbitDrag.y;
orbit.theta = orbitDrag.theta - deltaX * 0.006;
orbit.phi = THREE.MathUtils.clamp(
orbitDrag.phi + deltaY * 0.005,
0.55,
1.35
);
updateCamera();
event.preventDefault();
return;
}
const hit = getObservationHit(event);
renderer.domElement.style.cursor = hit ? "pointer" : "grab";
});
function endPointer(event) {
if (dragState && event.pointerId === dragState.pointerId) {
dragState = null;
}
if (orbitDrag && event.pointerId === orbitDrag.pointerId) {
orbitDrag = null;
}
if (renderer.domElement.hasPointerCapture(event.pointerId)) {
renderer.domElement.releasePointerCapture(event.pointerId);
}
renderer.domElement.style.cursor = "grab";
}
renderer.domElement.addEventListener("pointerup", endPointer);
renderer.domElement.addEventListener("pointercancel", endPointer);
renderer.domElement.addEventListener("dblclick", (event) => {
if (getObservationHit(event)) return;
setPointer(event);
const axisHit = raycaster.intersectObject(axisMesh, false)[0];
if (axisHit) {
const value = roundValue(
THREE.MathUtils.clamp(axisHit.point.x / SCALE, -DOMAIN, DOMAIN)
);
addObservation(value);
}
});
renderer.domElement.addEventListener(
"wheel",
(event) => {
event.preventDefault();
orbit.zoom = THREE.MathUtils.clamp(
orbit.zoom * (event.deltaY > 0 ? 0.92 : 1.08),
0.7,
1.75
);
updateCamera();
},
{ passive: false }
);
renderer.domElement.addEventListener("contextmenu", (event) => {
event.preventDefault();
});
renderer.domElement.addEventListener("keydown", (event) => {
const selected = observations.find(
(observation) => observation.id === selectedId
);
if (!selected) return;
if (event.key === "ArrowLeft" || event.key === "ArrowRight") {
event.preventDefault();
const direction = event.key === "ArrowLeft" ? -1 : 1;
selected.value = roundValue(
THREE.MathUtils.clamp(
selected.value + direction * 0.1,
-DOMAIN,
DOMAIN
)
);
updateAll();
}
if (event.key === "Delete" || event.key === "Backspace") {
event.preventDefault();
removeSelected();
}
});
$("addObservation").addEventListener("click", () => {
addObservation(0);
});
$("addExact").addEventListener("click", () => {
const input = $("addValue");
const value = Number(input.value);
if (!Number.isFinite(value) || value < -DOMAIN || value > DOMAIN) {
showToast(`Enter a value from ${-DOMAIN} to ${DOMAIN}.`);
return;
}
addObservation(value);
input.value = "0";
});
$("addValue").addEventListener("keydown", (event) => {
if (event.key === "Enter") $("addExact").click();
});
$("valueSlider").addEventListener("input", (event) => {
const selected = observations.find(
(observation) => observation.id === selectedId
);
if (!selected) return;
selected.value = roundValue(Number(event.target.value));
updateAll();
});
$("removeSelected").addEventListener("click", removeSelected);
$("resetDataset").addEventListener("click", resetDataset);
function zoomBy(factor) {
orbit.zoom = THREE.MathUtils.clamp(
orbit.zoom * factor,
0.7,
1.75
);
updateCamera();
}
$("zoomIn").addEventListener("click", () => zoomBy(1.12));
$("zoomOut").addEventListener("click", () => zoomBy(0.89));
$("resetView").addEventListener("click", () => {
orbit.theta = 0.52;
orbit.phi = 0.96;
orbit.zoom = 1;
updateCamera();
});
const focusMessages = {
mean:
"Mean = sum of all observations ÷ number of observations. Its coral diamond shows the arithmetic center.",
median:
"Median = middle sorted value. Its teal shape does not depend on how far the other values are from it.",
range:
"Range = largest value − smallest value. The gold rail shows the full span of the dataset."
};
function setFocus(key) {
focusKey = key;
document.querySelectorAll(".stat-card").forEach((card) => {
card.classList.toggle("active", card.dataset.focus === key);
});
$("sceneHint").textContent = focusMessages[key];
}
document.querySelectorAll(".stat-card").forEach((card) => {
card.addEventListener("click", () => {
setFocus(card.dataset.focus);
});
card.addEventListener("keydown", (event) => {
if (event.key === "Enter" || event.key === " ") {
event.preventDefault();
setFocus(card.dataset.focus);
}
});
});
function resize() {
const width = Math.max(1, viewport.clientWidth);
const height = Math.max(1, viewport.clientHeight);
const aspect = width / height;
const viewWidth = 26.5;
const viewHeight = viewWidth / aspect;
camera.left = -viewWidth / 2;
camera.right = viewWidth / 2;
camera.top = viewHeight / 2;
camera.bottom = -viewHeight / 2;
camera.updateProjectionMatrix();
renderer.setSize(width, height, false);
}
const resizeObserver = new ResizeObserver(resize);
resizeObserver.observe(viewport);
resize();
resetDataset();
const reducedMotion = window.matchMedia(
"(prefers-reduced-motion: reduce)"
).matches;
const clock = new THREE.Clock();
renderer.setAnimationLoop(() => {
const time = clock.getElapsedTime();
let index = 0;
for (const observation of observations) {
const group = observationGroups.get(observation.id);
if (!group) continue;
const marker = group.userData.marker;
const selected = observation.id === selectedId;
if (!reducedMotion) {
marker.rotation.y = time * 0.42 + index * 0.31;
marker.rotation.x = Math.sin(time * 0.7 + index) * 0.08;
}
const pulse = selected && !reducedMotion
? 1 + Math.sin(time * 4.2) * 0.055
: 1;
marker.scale.setScalar(pulse);
const ring = group.userData.selectionRing;
ring.material.opacity = selected && !reducedMotion
? 0.7 + Math.sin(time * 4.2) * 0.22
: 0.95;
index++;
}
const meanPulse =
focusKey === "mean" && !reducedMotion
? 1.16 + Math.sin(time * 4) * 0.06
: 1;
const medianPulse =
focusKey === "median" && !reducedMotion
? 1.16 + Math.sin(time * 4 + 1.2) * 0.06
: 1;
meanMarker.scale.setScalar(meanPulse);
medianMarker.scale.setScalar(medianPulse);
if (focusKey === "range") {
rangeMaterial.emissiveIntensity = 0.95;
} else {
rangeMaterial.emissiveIntensity = 0.55;
}
renderer.render(scene, camera);
});
</script>
</body>
</html>
```