Prompt specification
Build or customize the Three.js 3D scene "Outlier movement comparison" 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: Outlier movement comparison
Category: Algorithms & Systems · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/outlier-movement-comparison-x1810
Scene Description Prompt:
Outlier movement comparison. Move one extreme observation gradually farther from a cluster while tracking mean and median; keep all other points fixed and show that their responses differ. Please build this with Three.js from cdnjs in one self-contained HTML file, using stylized solid shapes.
Working Three.js Starter (outlier-movement-comparison-x1810):
```html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Outlier Movement Comparison</title>
<style>
:root {
color-scheme: dark;
--bg: #07111f;
--panel: rgba(12, 25, 43, 0.82);
--panel-border: rgba(164, 198, 231, 0.16);
--text: #eef6ff;
--muted: #90a8bf;
--blue: #58a6ff;
--amber: #ffb24a;
--green: #54e6a5;
--pink: #f26db6;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
font-family:
Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
color: var(--text);
background:
radial-gradient(circle at 22% 18%, rgba(40, 102, 161, 0.22), transparent 34%),
radial-gradient(circle at 82% 76%, rgba(120, 60, 150, 0.15), transparent 32%),
linear-gradient(145deg, #07111f 0%, #0a1728 48%, #060d18 100%);
}
button,
input {
font: inherit;
}
#scene {
position: fixed;
inset: 0;
}
#scene canvas {
display: block;
width: 100%;
height: 100%;
touch-action: none;
cursor: grab;
}
#scene canvas:active {
cursor: grabbing;
}
#annotations {
position: fixed;
inset: 0;
pointer-events: none;
overflow: hidden;
}
.annotation {
position: absolute;
left: 0;
top: 0;
transform: translate(-50%, -100%);
white-space: nowrap;
font-size: 11px;
font-weight: 750;
letter-spacing: 0.055em;
text-shadow: 0 2px 8px rgba(0, 0, 0, 0.9);
transition: opacity 120ms ease;
}
.annotation.outlier {
color: var(--amber);
}
.annotation.mean {
color: var(--green);
}
.annotation.median {
color: var(--pink);
}
.annotation.axis {
color: #829bb2;
font-size: 10px;
font-weight: 650;
}
.annotation.lane {
color: #738ba2;
font-size: 9px;
letter-spacing: 0.13em;
}
.annotation.gap {
color: #ffc978;
font-size: 10px;
}
.intro {
position: fixed;
top: 28px;
left: 30px;
max-width: 430px;
pointer-events: none;
}
.eyebrow {
display: inline-flex;
align-items: center;
gap: 8px;
margin-bottom: 12px;
padding: 6px 10px;
border: 1px solid rgba(84, 230, 165, 0.25);
border-radius: 999px;
color: #8ff0c3;
background: rgba(19, 53, 47, 0.48);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.13em;
text-transform: uppercase;
}
.eyebrow::before {
width: 6px;
height: 6px;
border-radius: 50%;
background: var(--green);
box-shadow: 0 0 12px var(--green);
content: "";
}
h1 {
margin: 0;
font-size: clamp(28px, 3.1vw, 48px);
line-height: 0.98;
letter-spacing: -0.045em;
}
.intro p {
max-width: 400px;
margin: 13px 0 0;
color: var(--muted);
font-size: 13px;
line-height: 1.55;
}
.intro strong {
color: #dceaff;
font-weight: 700;
}
.metrics {
position: fixed;
top: 28px;
right: 30px;
display: grid;
grid-template-columns: repeat(3, minmax(112px, 1fr));
gap: 9px;
width: min(390px, calc(100vw - 60px));
pointer-events: none;
}
.metric {
min-height: 98px;
padding: 15px 16px;
border: 1px solid var(--panel-border);
border-radius: 15px;
background: var(--panel);
box-shadow: 0 16px 45px rgba(0, 0, 0, 0.22);
backdrop-filter: blur(16px);
}
.metric-label {
color: var(--muted);
font-size: 9px;
font-weight: 800;
letter-spacing: 0.13em;
text-transform: uppercase;
}
.metric-value {
margin-top: 8px;
font-size: clamp(21px, 2vw, 28px);
font-weight: 760;
letter-spacing: -0.035em;
font-variant-numeric: tabular-nums;
}
.metric.mean .metric-value {
color: var(--green);
}
.metric.median .metric-value {
color: var(--pink);
}
.metric.gap .metric-value {
color: var(--amber);
}
.metric-sub {
margin-top: 3px;
color: #718aa1;
font-size: 10px;
}
.dock {
position: fixed;
right: 30px;
bottom: 28px;
left: 30px;
display: grid;
grid-template-columns: minmax(320px, 1.35fr) minmax(280px, 1fr);
gap: 12px;
pointer-events: none;
}
.panel {
border: 1px solid var(--panel-border);
border-radius: 18px;
background: var(--panel);
box-shadow: 0 20px 60px rgba(0, 0, 0, 0.3);
backdrop-filter: blur(18px);
pointer-events: auto;
}
.controls {
padding: 17px 19px 15px;
}
.control-heading {
display: flex;
align-items: center;
justify-content: space-between;
gap: 20px;
margin-bottom: 12px;
}
.control-title {
font-size: 11px;
font-weight: 800;
letter-spacing: 0.1em;
text-transform: uppercase;
}
.control-value {
color: var(--amber);
font-size: 14px;
font-weight: 750;
font-variant-numeric: tabular-nums;
}
.slider-row {
display: grid;
grid-template-columns: 1fr auto;
align-items: center;
gap: 12px;
}
input[type="range"] {
width: 100%;
height: 5px;
margin: 8px 0;
border-radius: 99px;
outline: none;
appearance: none;
background: linear-gradient(
to right,
var(--amber) 0%,
var(--amber) var(--progress, 0%),
rgba(141, 170, 199, 0.2) var(--progress, 0%),
rgba(141, 170, 199, 0.2) 100%
);
cursor: pointer;
}
input[type="range"]::-webkit-slider-thumb {
width: 18px;
height: 18px;
border: 3px solid #102035;
border-radius: 50%;
appearance: none;
background: var(--amber);
box-shadow: 0 0 0 1px rgba(255, 178, 74, 0.6), 0 0 18px rgba(255, 178, 74, 0.35);
}
input[type="range"]::-moz-range-thumb {
width: 13px;
height: 13px;
border: 3px solid #102035;
border-radius: 50%;
background: var(--amber);
box-shadow: 0 0 0 1px rgba(255, 178, 74, 0.6), 0 0 18px rgba(255, 178, 74, 0.35);
}
.buttons {
display: flex;
gap: 7px;
}
button {
min-height: 36px;
padding: 0 13px;
border: 1px solid rgba(160, 193, 222, 0.18);
border-radius: 10px;
color: #dcecff;
background: rgba(105, 141, 174, 0.12);
font-size: 11px;
font-weight: 750;
cursor: pointer;
transition:
transform 140ms ease,
background 140ms ease,
border-color 140ms ease;
}
button:hover {
border-color: rgba(160, 193, 222, 0.34);
background: rgba(105, 141, 174, 0.22);
transform: translateY(-1px);
}
button.primary {
min-width: 86px;
border-color: rgba(255, 178, 74, 0.35);
color: #ffd394;
background: rgba(255, 178, 74, 0.12);
}
.legend {
display: flex;
flex-wrap: wrap;
gap: 15px;
margin-top: 13px;
color: #8299af;
font-size: 10px;
}
.legend-item {
display: inline-flex;
align-items: center;
gap: 6px;
}
.swatch {
width: 8px;
height: 8px;
border-radius: 3px;
background: var(--blue);
box-shadow: 0 0 9px rgba(88, 166, 255, 0.35);
}
.swatch.outlier {
border-radius: 50%;
background: var(--amber);
}
.swatch.mean {
background: var(--green);
}
.swatch.median {
border-radius: 50%;
background: var(--pink);
}
.responses {
display: grid;
gap: 14px;
padding: 17px 19px;
}
.response-top {
display: flex;
align-items: baseline;
justify-content: space-between;
gap: 12px;
}
.response-title {
font-size: 11px;
font-weight: 800;
letter-spacing: 0.1em;
text-transform: uppercase;
}
.response-note {
color: #748ca2;
font-size: 10px;
}
.response-row {
display: grid;
grid-template-columns: 74px 1fr 60px;
align-items: center;
gap: 10px;
}
.response-name {
color: #c7d8e9;
font-size: 11px;
font-weight: 700;
}
.track {
height: 7px;
overflow: hidden;
border-radius: 99px;
background: rgba(133, 162, 188, 0.15);
}
.track i {
display: block;
width: 0;
height: 100%;
border-radius: inherit;
transition: width 80ms linear;
}
#meanBar {
background: linear-gradient(90deg, #2cba7e, var(--green));
box-shadow: 0 0 14px rgba(84, 230, 165, 0.38);
}
#medianBar {
background: var(--pink);
box-shadow: 0 0 14px rgba(242, 109, 182, 0.3);
}
.response-number {
color: #d8e7f5;
text-align: right;
font-size: 11px;
font-weight: 750;
font-variant-numeric: tabular-nums;
}
.explanation {
padding-top: 2px;
color: #8098ae;
font-size: 10px;
line-height: 1.45;
}
.explanation strong {
color: #bcd1e5;
}
.view-hint {
position: fixed;
right: 32px;
bottom: 178px;
color: rgba(151, 177, 201, 0.58);
font-size: 9px;
letter-spacing: 0.08em;
text-transform: uppercase;
pointer-events: none;
}
@media (max-width: 900px) {
.intro {
top: 18px;
left: 18px;
max-width: 360px;
}
.intro p {
display: none;
}
h1 {
font-size: 31px;
}
.metrics {
top: 104px;
right: 18px;
left: 18px;
width: auto;
}
.metric {
min-height: 76px;
padding: 11px 12px;
}
.metric-sub {
display: none;
}
.dock {
right: 14px;
bottom: 14px;
left: 14px;
grid-template-columns: 1fr;
gap: 8px;
}
.controls,
.responses {
padding: 12px 14px;
}
.responses {
grid-template-columns: 1fr 1fr;
column-gap: 16px;
}
.response-top,
.explanation {
grid-column: 1 / -1;
}
.response-top {
display: none;
}
.view-hint {
display: none;
}
}
@media (max-width: 520px) {
.eyebrow {
margin-bottom: 8px;
}
h1 {
font-size: 26px;
}
.metrics {
top: 89px;
gap: 5px;
}
.metric {
min-height: 67px;
border-radius: 11px;
}
.metric-value {
font-size: 19px;
}
.control-heading {
margin-bottom: 7px;
}
.slider-row {
grid-template-columns: 1fr;
gap: 7px;
}
.buttons {
justify-content: flex-end;
}
.legend {
gap: 9px;
margin-top: 9px;
}
.response-row {
grid-template-columns: 58px 1fr 50px;
gap: 6px;
}
.explanation {
display: none;
}
}
</style>
</head>
<body>
<div id="scene" aria-label="Interactive three-dimensional outlier movement comparison"></div>
<div id="annotations" aria-hidden="true"></div>
<header class="intro">
<div class="eyebrow">Live statistics experiment</div>
<h1>Watch the tail<br />drag the average.</h1>
<p>
Move the amber observation farther from the fixed blue cluster.
The <strong>mean</strong> follows every step, while the
<strong>median</strong> stays locked in the center.
</p>
</header>
<section class="metrics" aria-label="Live statistics">
<div class="metric mean">
<div class="metric-label">Mean μ</div>
<div class="metric-value" id="meanValue">8.11</div>
<div class="metric-sub" id="meanSub">shift +0.00</div>
</div>
<div class="metric median">
<div class="metric-label">Median</div>
<div class="metric-value" id="medianValue">8.00</div>
<div class="metric-sub" id="medianSub">locked at 8.00</div>
</div>
<div class="metric gap">
<div class="metric-label">Mean − median</div>
<div class="metric-value" id="gapValue">+0.11</div>
<div class="metric-sub" id="gapSub">tail gap +2.0</div>
</div>
</section>
<div class="view-hint">Drag the scene to orbit · scroll to zoom</div>
<main class="dock">
<section class="panel controls">
<div class="control-heading">
<label class="control-title" for="outlierRange">
Moving observation
</label>
<output class="control-value" id="outlierOutput" for="outlierRange">
x = 13.0
</output>
</div>
<div class="slider-row">
<input
id="outlierRange"
type="range"
min="13"
max="42"
step="0.1"
value="13"
aria-label="Position of the moving outlier"
/>
<div class="buttons">
<button id="playButton" class="primary" type="button">Play</button>
<button id="resetButton" type="button">Reset</button>
<button id="viewButton" type="button">View</button>
</div>
</div>
<div class="legend" aria-label="Legend">
<span class="legend-item">
<i class="swatch"></i> 8 fixed observations
</span>
<span class="legend-item">
<i class="swatch outlier"></i> moving outlier
</span>
<span class="legend-item">
<i class="swatch mean"></i> mean
</span>
<span class="legend-item">
<i class="swatch median"></i> median
</span>
</div>
</section>
<section class="panel responses" aria-label="Statistic response comparison">
<div class="response-top">
<span class="response-title">Response comparison</span>
<span class="response-note">change from starting position</span>
</div>
<div class="response-row">
<span class="response-name">Mean</span>
<div class="track"><i id="meanBar"></i></div>
<span class="response-number" id="meanShift">+0.00</span>
</div>
<div class="response-row">
<span class="response-name">Median</span>
<div class="track"><i id="medianBar"></i></div>
<span class="response-number" id="medianShift">+0.00</span>
</div>
<div class="explanation">
The outlier never crosses the middle rank, so the median is unaffected.
The mean absorbs <strong>one ninth</strong> of every unit of movement.
</div>
</section>
</main>
<script type="module">
import * as THREE from "https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// ------------------------------------------------------------
// Experiment data
// ------------------------------------------------------------
const fixedValues = [4, 5, 6, 7, 8, 9, 10, 11];
const fixedSum = fixedValues.reduce((sum, value) => sum + value, 0);
const observationCount = fixedValues.length + 1;
const minimumValue = 13;
const maximumValue = 42;
const fixedMaximum = Math.max(...fixedValues);
const initialMean = (fixedSum + minimumValue) / observationCount;
const initialMedian = 8;
const maximumMeanShift =
(fixedSum + maximumValue) / observationCount - initialMean;
const state = {
value: minimumValue,
playing: false
};
// ------------------------------------------------------------
// Renderer and scene
// ------------------------------------------------------------
const host = document.getElementById("scene");
const annotationHost = document.getElementById("annotations");
const renderer = new THREE.WebGLRenderer({
antialias: true,
alpha: true
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.15;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
host.appendChild(renderer.domElement);
const scene = new THREE.Scene();
scene.fog = new THREE.FogExp2(0x07111f, 0.012);
const camera = new THREE.OrthographicCamera(-24, 24, 14, -14, 0.1, 150);
const cameraTarget = new THREE.Vector3(0, 0, 0);
let azimuth = 0;
let elevation = 0.58;
const cameraRadius = 35;
function updateCamera() {
const horizontal = Math.cos(elevation) * cameraRadius;
camera.position.set(
cameraTarget.x + horizontal * Math.sin(azimuth),
cameraTarget.y + Math.sin(elevation) * cameraRadius,
cameraTarget.z + horizontal * Math.cos(azimuth)
);
camera.lookAt(cameraTarget);
}
updateCamera();
const hemisphere = new THREE.HemisphereLight(0xc9e6ff, 0x142033, 2.15);
scene.add(hemisphere);
const keyLight = new THREE.DirectionalLight(0xffffff, 3.2);
keyLight.position.set(-8, 19, 13);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(2048, 2048);
keyLight.shadow.camera.left = -28;
keyLight.shadow.camera.right = 28;
keyLight.shadow.camera.top = 18;
keyLight.shadow.camera.bottom = -18;
keyLight.shadow.camera.near = 1;
keyLight.shadow.camera.far = 60;
keyLight.shadow.bias = -0.0004;
scene.add(keyLight);
const rimLight = new THREE.DirectionalLight(0x659dff, 1.8);
rimLight.position.set(17, 8, -15);
scene.add(rimLight);
const amberLight = new THREE.PointLight(0xffa63d, 2.2, 12, 2);
amberLight.position.set(0, 3, 2);
scene.add(amberLight);
// ------------------------------------------------------------
// Coordinate helpers
// ------------------------------------------------------------
const domainStart = 0;
const domainEnd = 44;
const domainCenter = (domainStart + domainEnd) / 2;
const meanLaneZ = 2.25;
const medianLaneZ = -2.25;
function worldX(value) {
return value - domainCenter;
}
function valueToWorld(value, y, z) {
return new THREE.Vector3(worldX(value), y, z);
}
// ------------------------------------------------------------
// Materials
// ------------------------------------------------------------
const platformMaterial = new THREE.MeshStandardMaterial({
color: 0x0c1b2d,
roughness: 0.72,
metalness: 0.18
});
const gridMaterial = new THREE.MeshBasicMaterial({
color: 0x44627d,
transparent: true,
opacity: 0.18,
depthWrite: false
});
const railMaterial = new THREE.MeshBasicMaterial({
color: 0x46637f,
transparent: true,
opacity: 0.3
});
const bluePointMaterial = new THREE.MeshStandardMaterial({
color: 0x58a6ff,
roughness: 0.34,
metalness: 0.12,
emissive: 0x0b3767,
emissiveIntensity: 0.45
});
const amberMaterial = new THREE.MeshStandardMaterial({
color: 0xffb24a,
roughness: 0.28,
metalness: 0.18,
emissive: 0x7c3500,
emissiveIntensity: 0.5
});
const greenMaterial = new THREE.MeshStandardMaterial({
color: 0x54e6a5,
roughness: 0.3,
metalness: 0.12,
emissive: 0x075c38,
emissiveIntensity: 0.48
});
const pinkMaterial = new THREE.MeshStandardMaterial({
color: 0xf26db6,
roughness: 0.3,
metalness: 0.12,
emissive: 0x68143f,
emissiveIntensity: 0.45
});
// ------------------------------------------------------------
// Base platform, grid, and lane rails
// ------------------------------------------------------------
const platform = new THREE.Mesh(
new THREE.BoxGeometry(44, 0.35, 8.6),
platformMaterial
);
platform.position.y = -0.35;
platform.receiveShadow = true;
scene.add(platform);
const platformBorder = new THREE.LineSegments(
new THREE.EdgesGeometry(platform.geometry),
new THREE.LineBasicMaterial({
color: 0x5f7c97,
transparent: true,
opacity: 0.24
})
);
platformBorder.position.copy(platform.position);
scene.add(platformBorder);
for (let tick = 0; tick <= 40; tick += 5) {
const gridLine = new THREE.Mesh(
new THREE.BoxGeometry(0.022, 0.025, 7.65),
gridMaterial
);
gridLine.position.set(worldX(tick), -0.155, 0);
scene.add(gridLine);
}
function addLane(z, color, opacity) {
const material = new THREE.MeshBasicMaterial({
color,
transparent: true,
opacity,
depthWrite: false
});
const rail = new THREE.Mesh(
new THREE.BoxGeometry(43, 0.035, 0.075),
material
);
rail.position.set(0, -0.12, z);
scene.add(rail);
}
addLane(0, 0x7792aa, 0.24);
addLane(meanLaneZ, 0x54e6a5, 0.42);
addLane(medianLaneZ, 0xf26db6, 0.42);
const axis = new THREE.Mesh(
new THREE.BoxGeometry(43, 0.055, 0.08),
railMaterial
);
axis.position.set(0, -0.105, 4.05);
scene.add(axis);
for (let tick = 0; tick <= 40; tick += 5) {
const tickMesh = new THREE.Mesh(
new THREE.BoxGeometry(0.045, 0.045, 0.42),
railMaterial
);
tickMesh.position.set(worldX(tick), -0.085, 4.02);
scene.add(tickMesh);
}
// ------------------------------------------------------------
// Fixed observations
// ------------------------------------------------------------
const clusterOffsets = [-0.48, 0.38, -0.18, 0.53, -0.43, 0.18, -0.3, 0.43];
fixedValues.forEach((value, index) => {
const point = new THREE.Mesh(
new THREE.SphereGeometry(0.29, 22, 16),
bluePointMaterial
);
point.position.set(
worldX(value),
0.11,
clusterOffsets[index]
);
point.castShadow = true;
point.receiveShadow = true;
scene.add(point);
const shadow = new THREE.Mesh(
new THREE.CircleGeometry(0.31, 24),
new THREE.MeshBasicMaterial({
color: 0x020711,
transparent: true,
opacity: 0.42,
depthWrite: false
})
);
shadow.rotation.x = -Math.PI / 2;
shadow.position.set(worldX(value), -0.14, clusterOffsets[index]);
scene.add(shadow);
});
// ------------------------------------------------------------
// Moving outlier
// ------------------------------------------------------------
const outlier = new THREE.Mesh(
new THREE.IcosahedronGeometry(0.55, 1),
amberMaterial
);
outlier.position.set(worldX(state.value), 0.38, 0);
outlier.castShadow = true;
scene.add(outlier);
const outlierGlow = new THREE.Mesh(
new THREE.SphereGeometry(0.78, 24, 16),
new THREE.MeshBasicMaterial({
color: 0xffa73c,
transparent: true,
opacity: 0.09,
depthWrite: false
})
);
outlierGlow.position.copy(outlier.position);
scene.add(outlierGlow);
const outlierRing = new THREE.Mesh(
new THREE.TorusGeometry(0.72, 0.045, 10, 42),
new THREE.MeshBasicMaterial({
color: 0xffb24a,
transparent: true,
opacity: 0.68
})
);
outlierRing.rotation.x = Math.PI / 2;
outlierRing.position.set(worldX(state.value), -0.09, 0);
scene.add(outlierRing);
// ------------------------------------------------------------
// Mean and median markers
// ------------------------------------------------------------
const meanMarker = new THREE.Mesh(
new THREE.BoxGeometry(0.62, 0.62, 0.62),
greenMaterial
);
meanMarker.position.set(worldX(initialMean), 0.17, meanLaneZ);
meanMarker.castShadow = true;
scene.add(meanMarker);
const medianMarker = new THREE.Mesh(
new THREE.CylinderGeometry(0.34, 0.38, 0.58, 24),
pinkMaterial
);
medianMarker.position.set(worldX(initialMedian), 0.13, medianLaneZ);
medianMarker.castShadow = true;
scene.add(medianMarker);
function addMarkerRing(color, z) {
const ring = new THREE.Mesh(
new THREE.TorusGeometry(0.49, 0.035, 8, 36),
new THREE.MeshBasicMaterial({
color,
transparent: true,
opacity: 0.7
})
);
ring.rotation.x = Math.PI / 2;
ring.position.set(0, -0.1, z);
scene.add(ring);
return ring;
}
const meanRing = addMarkerRing(0x54e6a5, meanLaneZ);
const medianRing = addMarkerRing(0xf26db6, medianLaneZ);
// Mean movement trail
const meanTrail = new THREE.Mesh(
new THREE.BoxGeometry(1, 0.07, 0.14),
new THREE.MeshBasicMaterial({
color: 0x54e6a5,
transparent: true,
opacity: 0.38
})
);
meanTrail.position.set(worldX(initialMean), -0.095, meanLaneZ);
scene.add(meanTrail);
// ------------------------------------------------------------
// Dynamic lines and trail dots
// ------------------------------------------------------------
function createDynamicLine(color, opacity) {
const geometry = new THREE.BufferGeometry();
geometry.setAttribute(
"position",
new THREE.BufferAttribute(new Float32Array(6), 3)
);
const material = new THREE.LineBasicMaterial({
color,
transparent: true,
opacity
});
const line = new THREE.Line(geometry, material);
scene.add(line);
return line;
}
const outlierPath = createDynamicLine(0xffb24a, 0.3);
const gapLine = createDynamicLine(0xffc16f, 0.48);
function updateLine(line, x1, x2, y, z) {
const positions = line.geometry.attributes.position.array;
positions[0] = x1;
positions[1] = y;
positions[2] = z;
positions[3] = x2;
positions[4] = y;
positions[5] = z;
line.geometry.attributes.position.needsUpdate = true;
line.geometry.computeBoundingSphere();
}
const trailDots = [];
const trailDotGeometry = new THREE.SphereGeometry(0.075, 10, 8);
const trailDotMaterial = new THREE.MeshBasicMaterial({
color: 0xffb24a,
transparent: true,
opacity: 0.26,
depthWrite: false
});
for (let i = 0; i < 20; i++) {
const dot = new THREE.Mesh(trailDotGeometry, trailDotMaterial);
dot.visible = false;
scene.add(dot);
trailDots.push(dot);
}
// ------------------------------------------------------------
// HTML annotations projected from the 3D scene
// ------------------------------------------------------------
const annotations = [];
function createAnnotation(className, text = "") {
const element = document.createElement("div");
element.className = `annotation ${className}`;
element.textContent = text;
annotationHost.appendChild(element);
return element;
}
const outlierLabel = createAnnotation("outlier");
const meanLabel = createAnnotation("mean");
const medianLabel = createAnnotation("median");
const gapLabel = createAnnotation("gap");
annotations.push(
{
element: outlierLabel,
position: () => valueToWorld(state.value, 1.35, 0)
},
{
element: meanLabel,
position: () =>
valueToWorld(
(fixedSum + state.value) / observationCount,
1.25,
meanLaneZ
)
},
{
element: medianLabel,
position: () => valueToWorld(initialMedian, 1.3, medianLaneZ)
},
{
element: gapLabel,
position: () =>
valueToWorld(
(fixedMaximum + state.value) / 2,
0.12,
0.82
)
}
);
for (let tick = 0; tick <= 40; tick += 10) {
const label = createAnnotation("axis", String(tick));
annotations.push({
element: label,
position: () => valueToWorld(tick, -0.02, 4.28)
});
}
const laneData = [
["OBSERVATIONS", 0],
["MEAN TRACK", meanLaneZ],
["MEDIAN TRACK", medianLaneZ]
];
laneData.forEach(([text, z]) => {
const label = createAnnotation("lane", text);
annotations.push({
element: label,
position: () => new THREE.Vector3(-21.1, 0.05, z)
});
});
const projected = new THREE.Vector3();
function updateAnnotations() {
const width = window.innerWidth;
const height = window.innerHeight;
annotations.forEach((annotation) => {
projected.copy(annotation.position()).project(camera);
const visible = projected.z >= -1 && projected.z <= 1;
annotation.element.style.opacity = visible ? "1" : "0";
annotation.element.style.left =
`${(projected.x * 0.5 + 0.5) * width}px`;
annotation.element.style.top =
`${(-projected.y * 0.5 + 0.5) * height}px`;
});
}
// ------------------------------------------------------------
// UI references
// ------------------------------------------------------------
const range = document.getElementById("outlierRange");
const output = document.getElementById("outlierOutput");
const playButton = document.getElementById("playButton");
const resetButton = document.getElementById("resetButton");
const viewButton = document.getElementById("viewButton");
const meanValue = document.getElementById("meanValue");
const medianValue = document.getElementById("medianValue");
const gapValue = document.getElementById("gapValue");
const meanSub = document.getElementById("meanSub");
const medianSub = document.getElementById("medianSub");
const gapSub = document.getElementById("gapSub");
const meanShift = document.getElementById("meanShift");
const medianShift = document.getElementById("medianShift");
const meanBar = document.getElementById("meanBar");
const medianBar = document.getElementById("medianBar");
function medianOfCurrentData() {
const values = [...fixedValues, state.value].sort((a, b) => a - b);
const middle = Math.floor(values.length / 2);
return values.length % 2 === 0
? (values[middle - 1] + values[middle]) / 2
: values[middle];
}
function formatSigned(value) {
const sign = value >= 0 ? "+" : "−";
return `${sign}${Math.abs(value).toFixed(2)}`;
}
function updateExperiment() {
const mean = (fixedSum + state.value) / observationCount;
const median = medianOfCurrentData();
const meanChange = mean - initialMean;
const medianChange = median - initialMedian;
const gap = mean - median;
const tailGap = state.value - fixedMaximum;
// Three.js object positions
outlier.position.x = worldX(state.value);
outlierGlow.position.x = worldX(state.value);
outlierRing.position.x = worldX(state.value);
meanMarker.position.x = worldX(mean);
meanRing.position.x = worldX(mean);
medianMarker.position.x = worldX(median);
medianRing.position.x = worldX(median);
const meanTravel = mean - initialMean;
meanTrail.scale.x = Math.max(0.001, Math.abs(meanTravel));
meanTrail.position.x = worldX((mean + initialMean) / 2);
meanTrail.visible = Math.abs(meanTravel) > 0.005;
updateLine(
outlierPath,
worldX(minimumValue),
worldX(state.value),
-0.105,
0
);
updateLine(
gapLine,
worldX(fixedMaximum),
worldX(state.value),
-0.095,
0.82
);
const progress = (state.value - minimumValue) / (maximumValue - minimumValue);
trailDots.forEach((dot, index) => {
const t = index / (trailDots.length - 1);
dot.visible = progress > 0.005;
dot.position.set(
worldX(THREE.MathUtils.lerp(minimumValue, state.value, t)),
-0.09,
0
);
dot.scale.setScalar(0.7 + t * 0.45);
});
// UI values
range.value = state.value.toFixed(1);
range.style.setProperty(
"--progress",
`${progress * 100}%`
);
output.textContent = `x = ${state.value.toFixed(1)}`;
meanValue.textContent = mean.toFixed(2);
medianValue.textContent = median.toFixed(2);
gapValue.textContent = formatSigned(gap);
meanSub.textContent = `shift ${formatSigned(meanChange)}`;
medianSub.textContent = `locked at ${median.toFixed(2)}`;
gapSub.textContent = `tail gap +${tailGap.toFixed(1)}`;
meanShift.textContent = formatSigned(meanChange);
medianShift.textContent = formatSigned(medianChange);
meanBar.style.width = `${
maximumMeanShift > 0
? (Math.max(0, meanChange) / maximumMeanShift) * 100
: 0
}%`;
medianBar.style.width = `${
maximumMeanShift > 0
? (Math.max(0, medianChange) / maximumMeanShift) * 100
: 0
}%`;
// Projected labels
outlierLabel.textContent = `OUTLIER ${state.value.toFixed(1)}`;
meanLabel.textContent = `MEAN ${mean.toFixed(2)} ${formatSigned(meanChange)}`;
medianLabel.textContent = `MEDIAN ${median.toFixed(2)} LOCKED`;
gapLabel.textContent = `TAIL GAP +${tailGap.toFixed(1)}`;
}
// ------------------------------------------------------------
// Playback and direct controls
// ------------------------------------------------------------
function setPlaying(playing) {
state.playing = playing;
playButton.textContent = playing
? "Pause"
: state.value >= maximumValue - 0.01
? "Replay"
: "Play";
}
range.addEventListener("input", () => {
state.value = Number(range.value);
setPlaying(false);
updateExperiment();
});
playButton.addEventListener("click", () => {
if (state.playing) {
setPlaying(false);
return;
}
if (state.value >= maximumValue - 0.01) {
state.value = minimumValue;
}
setPlaying(true);
});
resetButton.addEventListener("click", () => {
state.value = minimumValue;
setPlaying(false);
updateExperiment();
});
viewButton.addEventListener("click", () => {
azimuth = 0;
elevation = 0.58;
camera.zoom = 1;
camera.updateProjectionMatrix();
updateCamera();
});
// Direct camera controls; no external control addon is used.
let dragging = false;
let previousX = 0;
let previousY = 0;
renderer.domElement.addEventListener("pointerdown", (event) => {
dragging = true;
previousX = event.clientX;
previousY = event.clientY;
renderer.domElement.setPointerCapture(event.pointerId);
});
renderer.domElement.addEventListener("pointermove", (event) => {
if (!dragging) return;
const deltaX = event.clientX - previousX;
const deltaY = event.clientY - previousY;
previousX = event.clientX;
previousY = event.clientY;
azimuth = THREE.MathUtils.clamp(
azimuth - deltaX * 0.005,
-0.72,
0.72
);
elevation = THREE.MathUtils.clamp(
elevation + deltaY * 0.004,
0.3,
1.12
);
updateCamera();
});
function endDrag(event) {
dragging = false;
if (renderer.domElement.hasPointerCapture(event.pointerId)) {
renderer.domElement.releasePointerCapture(event.pointerId);
}
}
renderer.domElement.addEventListener("pointerup", endDrag);
renderer.domElement.addEventListener("pointercancel", endDrag);
renderer.domElement.addEventListener(
"wheel",
(event) => {
event.preventDefault();
camera.zoom = THREE.MathUtils.clamp(
camera.zoom * Math.exp(-event.deltaY * 0.001),
0.72,
1.7
);
camera.updateProjectionMatrix();
},
{ passive: false }
);
renderer.domElement.addEventListener("dblclick", () => {
azimuth = 0;
elevation = 0.58;
camera.zoom = 1;
camera.updateProjectionMatrix();
updateCamera();
});
// ------------------------------------------------------------
// Resize and animation
// ------------------------------------------------------------
function resize() {
const width = window.innerWidth;
const height = window.innerHeight;
const aspect = width / height;
// Preserve the full horizontal data scale on narrow displays.
const viewHeight = Math.max(25, 47 / aspect);
camera.left = (-viewHeight * aspect) / 2;
camera.right = (viewHeight * aspect) / 2;
camera.top = viewHeight / 2;
camera.bottom = -viewHeight / 2;
camera.updateProjectionMatrix();
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(width, height);
}
window.addEventListener("resize", resize);
resize();
let previousTime = performance.now();
function animate(now) {
requestAnimationFrame(animate);
const delta = Math.min(0.05, (now - previousTime) / 1000);
previousTime = now;
if (state.playing) {
state.value = Math.min(
maximumValue,
state.value + delta * 2.9
);
if (state.value >= maximumValue) {
setPlaying(false);
}
}
const time = now * 0.001;
outlier.rotation.x += delta * 0.42;
outlier.rotation.y += delta * 0.65;
meanMarker.rotation.x += delta * 0.38;
meanMarker.rotation.y += delta * 0.52;
const pulse = 1 + Math.sin(time * 2.6) * 0.08;
outlierGlow.scale.setScalar(pulse);
outlierRing.scale.setScalar(1 + Math.sin(time * 2.6) * 0.04);
amberLight.position.x = outlier.position.x;
updateExperiment();
updateAnnotations();
renderer.render(scene, camera);
}
updateExperiment();
requestAnimationFrame(animate);
</script>
</body>
</html>
```