Prompt specification
Build or customize the Three.js 3D scene "Squared-deviation assembly display" 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: Squared-deviation assembly display
Category: Algorithms & Systems · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/squared-deviation-assembly-display-x1838
Scene Description Prompt:
Please create one self-contained HTML file with Three.js from cdnjs showing a stylized solid variance demonstration with two synthetic datasets having the same mean. Place each set of observation markers along its own horizontal number line and mark the shared mean. From every observation, draw a deviation segment and build a vertical square whose area represents that deviation squared; group those square areas into a visible total for each dataset, then divide by the explicitly selected population denominator to display the variance. Include a tightly clustered set and a more spread-out set. Preserve equal scale across both panels so the difference in dispersion can be inspected directly. Set each square side length from the absolute deviation, making a doubled deviation produce four times the displayed square area.
Working Three.js Starter (squared-deviation-assembly-display-x1838):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Solid Variance Demonstration</title>
<style>
:root {
color-scheme: dark;
--bg: #07111f;
--panel: #0c192a;
--panel-2: #102238;
--border: rgba(151, 182, 211, 0.18);
--text: #eaf4ff;
--muted: #91a7bc;
--tight: #35d0ba;
--spread: #ff8a5b;
--gold: #f7c948;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: var(--bg);
font-family:
Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
color: var(--text);
}
button,
select {
font: inherit;
}
.app {
display: flex;
flex-direction: column;
width: 100%;
height: 100%;
}
.topbar {
z-index: 2;
display: grid;
grid-template-columns: minmax(280px, 1fr) auto auto;
align-items: center;
gap: 22px;
padding: 14px 22px;
background:
linear-gradient(90deg, rgba(12, 28, 45, 0.98), rgba(7, 17, 31, 0.94));
border-bottom: 1px solid var(--border);
box-shadow: 0 8px 30px rgba(0, 0, 0, 0.18);
}
.brand {
min-width: 0;
}
.eyebrow {
margin-bottom: 4px;
color: var(--gold);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.16em;
text-transform: uppercase;
}
h1 {
margin: 0;
font-size: clamp(19px, 2vw, 28px);
line-height: 1.1;
letter-spacing: -0.025em;
}
.subtitle {
margin: 5px 0 0;
color: var(--muted);
font-size: 12px;
line-height: 1.35;
}
.stats {
display: flex;
gap: 9px;
}
.stat {
min-width: 112px;
padding: 8px 11px;
background: rgba(255, 255, 255, 0.035);
border: 1px solid var(--border);
border-radius: 10px;
}
.stat span {
display: block;
margin-bottom: 2px;
color: var(--muted);
font-size: 10px;
font-weight: 700;
letter-spacing: 0.04em;
text-transform: uppercase;
}
.stat strong {
font-size: 20px;
font-variant-numeric: tabular-nums;
}
.stat.tight {
border-color: rgba(53, 208, 186, 0.32);
}
.stat.tight strong {
color: var(--tight);
}
.stat.spread {
border-color: rgba(255, 138, 91, 0.32);
}
.stat.spread strong {
color: var(--spread);
}
.denominator {
min-width: 225px;
}
.denominator label {
display: block;
margin-bottom: 5px;
color: var(--muted);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.07em;
text-transform: uppercase;
}
select {
width: 100%;
padding: 9px 34px 9px 11px;
color: var(--text);
background: #11243a;
border: 1px solid rgba(247, 201, 72, 0.35);
border-radius: 9px;
outline: none;
cursor: pointer;
}
select:focus {
border-color: var(--gold);
box-shadow: 0 0 0 3px rgba(247, 201, 72, 0.12);
}
.denominator-note {
min-height: 14px;
margin-top: 4px;
color: var(--muted);
font-size: 10px;
}
#stage {
position: relative;
flex: 1;
min-height: 0;
overflow: hidden;
background:
radial-gradient(circle at 50% 45%, #10243a 0%, #07111f 68%);
}
canvas {
display: block;
width: 100%;
height: 100%;
cursor: grab;
touch-action: none;
}
canvas.dragging {
cursor: grabbing;
}
.legend,
.view-controls,
.hint {
position: absolute;
z-index: 3;
background: rgba(7, 17, 31, 0.86);
border: 1px solid var(--border);
border-radius: 11px;
backdrop-filter: blur(9px);
}
.legend {
left: 16px;
bottom: 16px;
display: flex;
flex-wrap: wrap;
gap: 8px 15px;
max-width: min(690px, calc(100% - 220px));
padding: 10px 13px;
color: var(--muted);
font-size: 10px;
}
.legend-item {
display: flex;
align-items: center;
gap: 7px;
white-space: nowrap;
}
.swatch {
width: 11px;
height: 11px;
border-radius: 3px;
background: var(--tight);
box-shadow: 0 0 10px rgba(53, 208, 186, 0.3);
}
.swatch.spread {
background: var(--spread);
box-shadow: 0 0 10px rgba(255, 138, 91, 0.3);
}
.line-swatch {
width: 3px;
height: 15px;
border-radius: 2px;
background: #f4fbff;
}
.square-swatch {
width: 13px;
height: 13px;
border: 1px solid #bcd3e6;
background: rgba(188, 211, 230, 0.12);
}
.view-controls {
right: 16px;
bottom: 16px;
display: flex;
align-items: center;
gap: 6px;
padding: 7px;
}
.view-controls button {
width: 30px;
height: 30px;
padding: 0;
color: var(--text);
background: #14283e;
border: 1px solid var(--border);
border-radius: 7px;
cursor: pointer;
}
.view-controls button:hover {
color: var(--gold);
border-color: rgba(247, 201, 72, 0.45);
}
.view-controls .reset {
width: auto;
padding: 0 10px;
font-size: 11px;
}
.hint {
left: 16px;
top: 14px;
padding: 7px 10px;
color: var(--muted);
font-size: 10px;
}
@media (max-width: 900px) {
.topbar {
grid-template-columns: 1fr auto;
gap: 10px 16px;
padding: 10px 14px;
}
.brand {
grid-column: 1 / -1;
}
.stats {
grid-column: 1;
}
.denominator {
min-width: 210px;
}
.subtitle {
font-size: 11px;
}
.legend {
max-width: calc(100% - 160px);
}
}
@media (max-width: 620px) {
.topbar {
grid-template-columns: 1fr;
}
.stats,
.denominator {
grid-column: 1;
}
.stats {
width: 100%;
}
.stat {
flex: 1;
min-width: 0;
}
.denominator {
min-width: 0;
}
.subtitle {
display: none;
}
.legend {
display: none;
}
.hint {
top: 8px;
left: 8px;
}
}
</style>
</head>
<body>
<div class="app">
<header class="topbar">
<div class="brand">
<div class="eyebrow">Solid Variance Lab</div>
<h1>Same mean, different variance</h1>
<p class="subtitle">
Each deviation becomes a square. Doubling a deviation doubles its side
and quadruples its area.
</p>
</div>
<div class="stats" aria-live="polite">
<div class="stat tight">
<span id="stat-label-a">Tight · σ²</span>
<strong id="stat-a">1.80</strong>
</div>
<div class="stat spread">
<span id="stat-label-b">Spread · σ²</span>
<strong id="stat-b">7.20</strong>
</div>
</div>
<div class="denominator">
<label for="denominator">Selected denominator</label>
<select id="denominator">
<option value="population" selected>
Population variance · N = 4
</option>
<option value="sample">
Sample variance · N − 1 = 3
</option>
</select>
<div class="denominator-note" id="denominator-note">
σ² = Σ(xᵢ − μ)² / N
</div>
</div>
</header>
<main id="stage">
<div class="hint">Drag to pan · Scroll to zoom · Double-click to reset</div>
<div class="legend" aria-label="Visualization legend">
<div class="legend-item">
<span class="swatch"></span>
Tight-cluster deviation square
</div>
<div class="legend-item">
<span class="swatch spread"></span>
Spread deviation square
</div>
<div class="legend-item">
<span class="line-swatch"></span>
Deviation |xᵢ − μ|
</div>
<div class="legend-item">
<span class="square-swatch"></span>
Area = deviation²
</div>
</div>
<div class="view-controls" aria-label="Camera controls">
<button id="zoom-out" type="button" aria-label="Zoom out">−</button>
<button id="zoom-in" type="button" aria-label="Zoom in">+</button>
<button id="reset-view" class="reset" type="button">Reset</button>
</div>
</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 stage = document.getElementById("stage");
const denominatorSelect = document.getElementById("denominator");
const denominatorNote = document.getElementById("denominator-note");
const statA = document.getElementById("stat-a");
const statB = document.getElementById("stat-b");
const statLabelA = document.getElementById("stat-label-a");
const statLabelB = document.getElementById("stat-label-b");
const reducedMotion = window.matchMedia(
"(prefers-reduced-motion: reduce)"
).matches;
// The two synthetic datasets have exactly the same mean: 0.
// The spread observations are exactly 2× the tight observations.
const datasets = [
{
id: "A",
name: "TIGHT CLUSTER",
values: [-1.8, -0.6, 0.6, 1.8],
baseline: 3.4,
color: 0x35d0ba,
bright: 0xb8fff3,
css: "#35d0ba"
},
{
id: "B",
name: "MORE SPREAD",
values: [-3.6, -1.2, 1.2, 3.6],
baseline: -3.8,
color: 0xff8a5b,
bright: 0xffe0d1,
css: "#ff8a5b"
}
];
for (const dataset of datasets) {
dataset.mean =
dataset.values.reduce((sum, value) => sum + value, 0) /
dataset.values.length;
dataset.deviations = dataset.values.map(
(value) => value - dataset.mean
);
dataset.squaredDeviations = dataset.deviations.map(
(deviation) => deviation * deviation
);
dataset.sumSquares = dataset.squaredDeviations.reduce(
(sum, area) => sum + area,
0
);
// A square with this side length has exactly Σd² as its area.
dataset.totalSide = Math.sqrt(dataset.sumSquares);
}
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x07111f);
scene.fog = new THREE.Fog(0x07111f, 31, 58);
const renderer = new THREE.WebGLRenderer({
antialias: true,
alpha: false,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio || 1, 2));
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.12;
stage.prepend(renderer.domElement);
renderer.domElement.setAttribute(
"aria-label",
"Interactive 3D visualization of population variance using deviation squares"
);
const camera = new THREE.OrthographicCamera(-10, 10, 7, -7, 0.1, 100);
const view = {
target: new THREE.Vector3(2.9, 0.65, 0),
zoom: 1,
initialTarget: new THREE.Vector3(2.9, 0.65, 0),
initialZoom: 1
};
const ambient = new THREE.AmbientLight(0xdceeff, 1.45);
scene.add(ambient);
const keyLight = new THREE.DirectionalLight(0xffffff, 3.1);
keyLight.position.set(-4, 7, 14);
scene.add(keyLight);
const fillLight = new THREE.DirectionalLight(0x7abfff, 1.0);
fillLight.position.set(8, -5, 10);
scene.add(fillLight);
const animatedGroups = [];
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.lineTo(x + width - r, y);
ctx.quadraticCurveTo(x + width, y, x + width, y + r);
ctx.lineTo(x + width, y + height - r);
ctx.quadraticCurveTo(
x + width,
y + height,
x + width - r,
y + height
);
ctx.lineTo(x + r, y + height);
ctx.quadraticCurveTo(x, y + height, x, y + height - r);
ctx.lineTo(x, y + r);
ctx.quadraticCurveTo(x, y, x + r, y);
ctx.closePath();
}
function redrawText(sprite, text, options = {}) {
const {
color = "#eaf4ff",
background = null,
border = null,
fontSize = 56,
worldHeight = 0.36,
weight = 700
} = options;
const canvas = sprite.userData.canvas;
const ctx = canvas.getContext("2d");
ctx.font = `${weight} ${fontSize}px Inter, system-ui, sans-serif`;
const measured = ctx.measureText(text).width;
const width = Math.ceil(
Math.min(1800, Math.max(150, measured + 54))
);
const height = 128;
if (canvas.width !== width) canvas.width = width;
if (canvas.height !== height) canvas.height = height;
ctx.clearRect(0, 0, width, height);
if (background || border) {
roundedRect(ctx, 3, 3, width - 6, height - 6, 18);
ctx.fillStyle = background || "rgba(0,0,0,0)";
ctx.fill();
if (border) {
ctx.lineWidth = 3;
ctx.strokeStyle = border;
ctx.stroke();
}
}
ctx.font = `${weight} ${fontSize}px Inter, system-ui, sans-serif`;
ctx.textAlign = "center";
ctx.textBaseline = "middle";
ctx.fillStyle = color;
ctx.fillText(text, width / 2, height / 2 + 1, width - 28);
const oldTexture = sprite.material.map;
const texture = new THREE.CanvasTexture(canvas);
texture.colorSpace = THREE.SRGBColorSpace;
texture.minFilter = THREE.LinearFilter;
texture.magFilter = THREE.LinearFilter;
sprite.material.map = texture;
sprite.material.needsUpdate = true;
sprite.scale.set(
worldHeight * (canvas.width / canvas.height),
worldHeight,
1
);
if (oldTexture) oldTexture.dispose();
}
function makeText(text, options = {}) {
const material = new THREE.SpriteMaterial({
transparent: true,
depthTest: false,
depthWrite: false,
toneMapped: false
});
const sprite = new THREE.Sprite(material);
sprite.renderOrder = 50;
sprite.userData.canvas = document.createElement("canvas");
sprite.userData.options = options;
redrawText(sprite, text, options);
return sprite;
}
function updateText(sprite, text, options = {}) {
const merged = { ...sprite.userData.options, ...options };
sprite.userData.options = merged;
redrawText(sprite, text, merged);
}
function addOutline(mesh, color = 0xd9efff, opacity = 0.48) {
const edges = new THREE.EdgesGeometry(mesh.geometry);
const lines = new THREE.LineSegments(
edges,
new THREE.LineBasicMaterial({
color,
transparent: true,
opacity
})
);
lines.position.copy(mesh.position);
lines.renderOrder = 4;
mesh.parent.add(lines);
return lines;
}
function makePanelBackground(dataset) {
const isTop = dataset.baseline > 0;
const height = isTop ? 3.9 : 7.0;
const centerY = isTop ? 4.95 : -1.55;
const background = new THREE.Mesh(
new THREE.BoxGeometry(18.7, height, 0.12),
new THREE.MeshStandardMaterial({
color: isTop ? 0x0d2231 : 0x0e1d30,
roughness: 0.96,
metalness: 0.02
})
);
background.position.set(2.85, centerY, -0.65);
scene.add(background);
const border = new THREE.LineSegments(
new THREE.EdgesGeometry(background.geometry),
new THREE.LineBasicMaterial({
color: dataset.color,
transparent: true,
opacity: 0.18
})
);
border.position.copy(background.position);
scene.add(border);
}
function makeAxis(dataset) {
const y = dataset.baseline;
const axis = new THREE.Mesh(
new THREE.BoxGeometry(12.05, 0.055, 0.10),
new THREE.MeshStandardMaterial({
color: 0x9eb5c9,
emissive: 0x24394d,
emissiveIntensity: 0.2,
roughness: 0.45
})
);
axis.position.set(0, y, 0.02);
scene.add(axis);
const arrow = new THREE.Mesh(
new THREE.ConeGeometry(0.12, 0.32, 16),
new THREE.MeshStandardMaterial({
color: 0x9eb5c9,
roughness: 0.45
})
);
arrow.rotation.z = -Math.PI / 2;
arrow.position.set(6.17, y, 0.02);
scene.add(arrow);
for (let x = -6; x <= 6; x++) {
const major = x % 2 === 0;
const tick = new THREE.Mesh(
new THREE.BoxGeometry(0.025, major ? 0.24 : 0.13, 0.055),
new THREE.MeshBasicMaterial({
color: major ? 0x8099b0 : 0x425b73
})
);
tick.position.set(x, y, 0.04);
scene.add(tick);
if (major && x !== 0) {
const tickLabel = makeText(String(x), {
color: "#68829b",
fontSize: 52,
worldHeight: 0.23,
weight: 600
});
tickLabel.position.set(x, y - 0.36, 0.36);
scene.add(tickLabel);
}
}
}
function makeMeanMarker(dataset) {
const y = dataset.baseline;
const lineGeometry = new THREE.BufferGeometry().setFromPoints([
new THREE.Vector3(0, y, 0.20),
new THREE.Vector3(0, y + 0.68, 0.20)
]);
const meanLine = new THREE.Line(
lineGeometry,
new THREE.LineDashedMaterial({
color: 0xf7c948,
dashSize: 0.09,
gapSize: 0.065,
transparent: true,
opacity: 0.85
})
);
meanLine.computeLineDistances();
scene.add(meanLine);
const diamond = new THREE.Mesh(
new THREE.OctahedronGeometry(0.135, 0),
new THREE.MeshStandardMaterial({
color: 0xf7c948,
emissive: 0x7a5b00,
emissiveIntensity: 0.35,
roughness: 0.35,
metalness: 0.15
})
);
diamond.position.set(0, y, 0.27);
diamond.rotation.z = Math.PI / 4;
scene.add(diamond);
const meanLabel = makeText("μ = 0", {
color: "#07111f",
background: "#f7c948",
fontSize: 58,
worldHeight: 0.34
});
meanLabel.position.set(0, y + 0.43, 0.58);
scene.add(meanLabel);
}
function makeDeviationSquare(dataset, value, index) {
const deviation = Math.abs(value - dataset.mean);
const x = value;
const y = dataset.baseline;
const group = new THREE.Group();
group.position.set(x, y, 0);
scene.add(group);
// Offset shadow gives the square a solid, raised appearance.
const shadow = new THREE.Mesh(
new THREE.BoxGeometry(deviation, deviation, 0.10),
new THREE.MeshBasicMaterial({
color: 0x000000,
transparent: true,
opacity: 0.32,
depthWrite: false
})
);
shadow.position.set(0.075, -0.075, -0.08);
group.add(shadow);
const square = new THREE.Mesh(
new THREE.BoxGeometry(deviation, deviation, 0.20),
new THREE.MeshStandardMaterial({
color: dataset.color,
emissive: dataset.color,
emissiveIntensity: 0.10,
roughness: 0.34,
metalness: 0.10
})
);
square.position.set(0, deviation / 2, 0.04);
group.add(square);
addOutline(square, dataset.bright, 0.72);
// This vertical segment starts at the observation and reaches d.
const segment = new THREE.Mesh(
new THREE.BoxGeometry(0.038, deviation, 0.045),
new THREE.MeshBasicMaterial({ color: 0xf7fbff })
);
segment.position.set(0, deviation / 2, 0.20);
group.add(segment);
const observation = new THREE.Mesh(
new THREE.SphereGeometry(0.095, 18, 12),
new THREE.MeshStandardMaterial({
color: 0xffffff,
emissive: dataset.color,
emissiveIntensity: 0.28,
roughness: 0.3
})
);
observation.position.set(x, y, 0.30);
scene.add(observation);
const formatted = value.toFixed(1).replace("-", "−");
const valueLabel = makeText(formatted, {
color: dataset.css,
background: "rgba(5, 13, 24, 0.88)",
border: dataset.css,
fontSize: 58,
worldHeight: 0.31
});
valueLabel.position.set(x, y - 0.80, 0.50);
scene.add(valueLabel);
animatedGroups.push({
group,
delay: index * 75,
duration: 650
});
}
function makeGroupedTotal(dataset) {
const side = dataset.totalSide;
const x = 9.05;
const y = dataset.baseline;
const group = new THREE.Group();
group.position.set(x, y, 0);
scene.add(group);
const shadow = new THREE.Mesh(
new THREE.BoxGeometry(side, side, 0.13),
new THREE.MeshBasicMaterial({
color: 0x000000,
transparent: true,
opacity: 0.38,
depthWrite: false
})
);
shadow.position.set(0.10, -0.10, -0.12);
group.add(shadow);
const base = new THREE.Mesh(
new THREE.BoxGeometry(side, side, 0.22),
new THREE.MeshStandardMaterial({
color: 0x102a3d,
emissive: dataset.color,
emissiveIntensity: 0.025,
roughness: 0.48,
metalness: 0.08
})
);
base.position.set(0, side / 2, 0.05);
group.add(base);
addOutline(base, dataset.bright, 0.84);
/*
* Partition the large square into strips whose areas equal the
* original deviation-square areas:
*
* strip area = strip width * side = d_i²
* strip width = d_i² / side
*
* Therefore the entire large square has area Σd_i².
*/
let cursor = -side / 2;
const baseColor = new THREE.Color(dataset.color);
dataset.squaredDeviations.forEach((area, index) => {
const stripWidth = area / side;
const stripColor = baseColor.clone();
if (index % 2 === 1) {
stripColor.lerp(new THREE.Color(0xffffff), 0.18);
}
const strip = new THREE.Mesh(
new THREE.BoxGeometry(stripWidth, side, 0.055),
new THREE.MeshStandardMaterial({
color: stripColor,
emissive: stripColor,
emissiveIntensity: 0.055,
roughness: 0.42,
metalness: 0.04,
transparent: true,
opacity: 0.94
})
);
strip.position.set(
cursor + stripWidth / 2,
side / 2,
0.19
);
group.add(strip);
cursor += stripWidth;
});
// A small collector shelf under the grouped area.
const shelf = new THREE.Mesh(
new THREE.BoxGeometry(side + 0.35, 0.12, 0.52),
new THREE.MeshStandardMaterial({
color: dataset.color,
emissive: dataset.color,
emissiveIntensity: 0.06,
roughness: 0.55
})
);
shelf.position.set(0, -0.10, 0.02);
group.add(shelf);
const totalCaption = makeText("GROUPED TOTAL AREA", {
color: dataset.css,
fontSize: 53,
worldHeight: 0.34
});
totalCaption.position.set(x, y + side + 0.62, 0.55);
scene.add(totalCaption);
const denominatorLabel = makeText("", {
color: "#dceeff",
background: "rgba(5, 13, 24, 0.90)",
border: dataset.css,
fontSize: 51,
worldHeight: 0.31
});
denominatorLabel.position.set(x, y + side + 0.24, 0.60);
scene.add(denominatorLabel);
const varianceLabel = makeText("", {
color: "#ffffff",
background: "rgba(5, 13, 24, 0.86)",
border: dataset.css,
fontSize: 62,
worldHeight: 0.42
});
varianceLabel.position.set(x, y + side * 0.62, 0.65);
scene.add(varianceLabel);
const sumLabel = makeText(
`Σd² = ${dataset.sumSquares.toFixed(2)}`,
{
color: "#eef8ff",
background: "rgba(5, 13, 24, 0.80)",
fontSize: 55,
worldHeight: 0.35
}
);
sumLabel.position.set(x, y + side * 0.34, 0.65);
scene.add(sumLabel);
const sideLabel = makeText(
`side = √${dataset.sumSquares.toFixed(2)} = ${side.toFixed(2)}`,
{
color: "#9fb5c8",
fontSize: 48,
worldHeight: 0.27,
weight: 600
}
);
sideLabel.position.set(x, y - 0.42, 0.55);
scene.add(sideLabel);
dataset.totalLabels = {
denominatorLabel,
varianceLabel
};
animatedGroups.push({
group,
delay: 500,
duration: 760
});
}
function makePanel(dataset) {
makePanelBackground(dataset);
makeAxis(dataset);
makeMeanMarker(dataset);
const title = makeText(
`${dataset.id} · ${dataset.name}`,
{
color: dataset.css,
fontSize: 61,
worldHeight: 0.48
}
);
title.position.set(-5.55, dataset.baseline + 3.08, 0.55);
scene.add(title);
const subtitle = makeText(
"square side = |xᵢ − μ| • area = (xᵢ − μ)²",
{
color: "#8fa7bc",
fontSize: 48,
worldHeight: 0.28,
weight: 600
}
);
subtitle.position.set(-5.48, dataset.baseline + 2.63, 0.55);
scene.add(subtitle);
dataset.values.forEach((value, index) => {
makeDeviationSquare(dataset, value, index);
});
makeGroupedTotal(dataset);
}
// Shared x-coordinate guides make the identical panel scales explicit.
for (let x = -6; x <= 6; x += 2) {
const guideGeometry = new THREE.BufferGeometry().setFromPoints([
new THREE.Vector3(x, -5.05, -0.49),
new THREE.Vector3(x, 6.88, -0.49)
]);
const guide = new THREE.Line(
guideGeometry,
new THREE.LineDashedMaterial({
color: x === 0 ? 0xf7c948 : 0x52718b,
dashSize: 0.07,
gapSize: 0.13,
transparent: true,
opacity: x === 0 ? 0.18 : 0.12
})
);
guide.computeLineDistances();
scene.add(guide);
}
datasets.forEach(makePanel);
const sharedMeanBadge = makeText(
"SAME MEAN • μ = 0 • 2× DEVIATION → 4× AREA",
{
color: "#f7d66d",
background: "rgba(10, 23, 38, 0.95)",
border: "rgba(247, 201, 72, 0.48)",
fontSize: 50,
worldHeight: 0.36
}
);
sharedMeanBadge.position.set(0, 2.48, 0.75);
scene.add(sharedMeanBadge);
function currentDenominator() {
return denominatorSelect.value === "population"
? datasets[0].values.length
: datasets[0].values.length - 1;
}
function updateVarianceDisplay() {
const population = denominatorSelect.value === "population";
const denominator = currentDenominator();
const symbol = population ? "σ²" : "s²";
const denominatorText = population ? "N = 4" : "N − 1 = 3";
denominatorNote.textContent = population
? "σ² = Σ(xᵢ − μ)² / N"
: "s² = Σ(xᵢ − μ)² / (N − 1)";
statLabelA.textContent = `Tight · ${symbol}`;
statLabelB.textContent = `Spread · ${symbol}`;
for (const [index, dataset] of datasets.entries()) {
const variance = dataset.sumSquares / denominator;
[statA, statB][index].textContent = variance.toFixed(2);
updateText(
dataset.totalLabels.denominatorLabel,
`${population ? "POPULATION" : "SAMPLE"} · ${denominatorText}`
);
updateText(
dataset.totalLabels.varianceLabel,
`${symbol} = ${variance.toFixed(2)}`
);
}
}
denominatorSelect.addEventListener("change", updateVarianceDisplay);
updateVarianceDisplay();
function updateCamera() {
camera.zoom = view.zoom;
camera.position.set(
view.target.x,
view.target.y - 3.2,
32
);
camera.lookAt(view.target.x, view.target.y, 0);
camera.updateProjectionMatrix();
}
function resize() {
const width = Math.max(stage.clientWidth, 1);
const height = Math.max(stage.clientHeight, 1);
const aspect = width / height;
renderer.setSize(width, height, false);
// Preserve at least 20.5 world units of horizontal visibility.
const viewHeight = Math.max(13.4, 20.5 / aspect);
camera.left = (-viewHeight * aspect) / 2;
camera.right = (viewHeight * aspect) / 2;
camera.top = viewHeight / 2;
camera.bottom = -viewHeight / 2;
updateCamera();
}
const resizeObserver = new ResizeObserver(resize);
resizeObserver.observe(stage);
resize();
function resetView() {
view.target.copy(view.initialTarget);
view.zoom = view.initialZoom;
updateCamera();
}
function zoomBy(factor) {
view.zoom = THREE.MathUtils.clamp(
view.zoom * factor,
0.72,
2.25
);
updateCamera();
}
document.getElementById("zoom-in").addEventListener("click", () => {
zoomBy(1.16);
});
document.getElementById("zoom-out").addEventListener("click", () => {
zoomBy(1 / 1.16);
});
document.getElementById("reset-view").addEventListener("click", resetView);
renderer.domElement.addEventListener("dblclick", resetView);
// Camera controls implemented directly, without Three.js add-ons.
let dragging = false;
let pointerX = 0;
let pointerY = 0;
renderer.domElement.addEventListener("pointerdown", (event) => {
dragging = true;
pointerX = event.clientX;
pointerY = event.clientY;
renderer.domElement.setPointerCapture(event.pointerId);
renderer.domElement.classList.add("dragging");
});
renderer.domElement.addEventListener("pointermove", (event) => {
if (!dragging) return;
const dx = event.clientX - pointerX;
const dy = event.clientY - pointerY;
pointerX = event.clientX;
pointerY = event.clientY;
const worldPerPixel =
(camera.top - camera.bottom) /
(camera.zoom * Math.max(stage.clientHeight, 1));
view.target.x -= dx * worldPerPixel;
view.target.y += dy * worldPerPixel;
updateCamera();
});
function endDrag(event) {
dragging = false;
renderer.domElement.classList.remove("dragging");
if (
event.pointerId !== undefined &&
renderer.domElement.hasPointerCapture(event.pointerId)
) {
renderer.domElement.releasePointerCapture(event.pointerId);
}
}
renderer.domElement.addEventListener("pointerup", endDrag);
renderer.domElement.addEventListener("pointercancel", endDrag);
renderer.domElement.addEventListener(
"wheel",
(event) => {
event.preventDefault();
zoomBy(event.deltaY < 0 ? 1.09 : 1 / 1.09);
},
{ passive: false }
);
window.addEventListener("keydown", (event) => {
if (event.key === "+" || event.key === "=") {
zoomBy(1.12);
} else if (event.key === "-" || event.key === "_") {
zoomBy(1 / 1.12);
} else if (event.key === "0") {
resetView();
}
});
const animationStart = performance.now();
function easeOutCubic(value) {
return 1 - Math.pow(1 - value, 3);
}
renderer.setAnimationLoop((now) => {
for (const item of animatedGroups) {
if (reducedMotion) {
item.group.scale.y = 1;
continue;
}
const progress = THREE.MathUtils.clamp(
(now - animationStart - item.delay) / item.duration,
0,
1
);
item.group.scale.y = Math.max(0.001, easeOutCubic(progress));
}
renderer.render(scene, camera);
});
</script>
</body>
</html>
```