Prompt specification
Build or customize the Three.js 3D scene "Market basket estimate teaching desk" 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: Market basket estimate teaching desk
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/market-basket-estimate-teaching-desk-x1775
Scene Description Prompt:
Show voxel bootstrap resampling of fictional market purchases, comparing repeated summaries with the original observed sample. One HTML file using Three.js from cdnjs.
Working Three.js Starter (market-basket-estimate-teaching-desk-x1775):
```html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Voxel Bootstrap — Fictional Market Purchases</title>
<style>
:root {
color-scheme: dark;
--bg: #070b14;
--panel: rgba(13, 20, 34, 0.88);
--panel-strong: rgba(15, 23, 39, 0.96);
--line: rgba(154, 185, 225, 0.15);
--text: #edf5ff;
--muted: #8fa3bd;
--cyan: #3cd8c7;
--violet: #9a83ff;
--gold: #ffc857;
--green: #67d777;
}
* { box-sizing: border-box; }
html, body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background:
radial-gradient(circle at 52% 38%, #101c30 0, #080d18 45%, #050810 100%);
color: var(--text);
font-family:
Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
button, input, select { font: inherit; }
#scene {
position: fixed;
inset: 0;
width: 100%;
height: 100%;
display: block;
touch-action: none;
outline: none;
}
.topbar {
position: fixed;
z-index: 5;
top: 22px;
left: 24px;
right: 24px;
display: flex;
align-items: center;
justify-content: space-between;
pointer-events: none;
}
.brand {
display: flex;
align-items: center;
gap: 13px;
}
.brand-mark {
width: 35px;
height: 35px;
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 3px;
transform: rotate(-7deg);
}
.brand-mark i {
display: block;
border-radius: 2px;
background: var(--cyan);
box-shadow: 0 0 14px rgba(60, 216, 199, 0.22);
}
.brand-mark i:nth-child(2),
.brand-mark i:nth-child(6) { background: var(--violet); }
.brand-mark i:nth-child(3),
.brand-mark i:nth-child(5) { background: var(--gold); }
.brand-mark i:nth-child(4) { opacity: 0.45; }
.eyebrow {
margin: 0 0 3px;
color: var(--cyan);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.19em;
text-transform: uppercase;
}
h1 {
margin: 0;
font-size: 19px;
line-height: 1.05;
letter-spacing: -0.02em;
}
.subtitle {
margin: 5px 0 0;
color: var(--muted);
font-size: 12px;
}
.top-actions {
display: flex;
align-items: center;
gap: 10px;
pointer-events: auto;
}
.status {
display: flex;
align-items: center;
gap: 8px;
padding: 8px 11px;
border: 1px solid var(--line);
border-radius: 999px;
background: rgba(10, 17, 29, 0.72);
color: #b8c8dc;
font-size: 10px;
font-weight: 800;
letter-spacing: 0.12em;
}
.status-dot {
width: 7px;
height: 7px;
border-radius: 50%;
background: var(--cyan);
box-shadow: 0 0 10px var(--cyan);
}
.status.running .status-dot {
background: var(--gold);
box-shadow: 0 0 10px var(--gold);
animation: blink 0.7s ease-in-out infinite alternate;
}
@keyframes blink { to { opacity: 0.25; } }
.ghost-button {
border: 1px solid var(--line);
border-radius: 9px;
padding: 8px 11px;
background: rgba(10, 17, 29, 0.76);
color: #c6d4e7;
cursor: pointer;
transition: 150ms ease;
}
.ghost-button:hover {
border-color: rgba(60, 216, 199, 0.42);
color: white;
background: rgba(18, 31, 48, 0.92);
}
.panel {
position: fixed;
z-index: 6;
border: 1px solid var(--line);
border-radius: 16px;
background:
linear-gradient(145deg, rgba(18, 28, 46, 0.94), rgba(9, 15, 27, 0.92));
box-shadow:
0 22px 60px rgba(0, 0, 0, 0.32),
inset 0 1px rgba(255, 255, 255, 0.035);
backdrop-filter: blur(18px);
}
.controls {
top: 88px;
left: 24px;
width: 306px;
max-height: calc(100vh - 112px);
padding: 18px;
overflow-y: auto;
scrollbar-width: thin;
scrollbar-color: #2a3c56 transparent;
}
.panel-heading {
display: flex;
align-items: flex-start;
justify-content: space-between;
gap: 12px;
margin-bottom: 6px;
}
.panel-heading h2 {
margin: 0;
font-size: 15px;
letter-spacing: -0.01em;
}
.mini-label {
color: var(--muted);
font-size: 9px;
font-weight: 800;
letter-spacing: 0.12em;
text-transform: uppercase;
}
.intro {
margin: 0 0 18px;
color: var(--muted);
font-size: 11px;
line-height: 1.55;
}
.field-label {
display: flex;
align-items: center;
justify-content: space-between;
margin: 15px 0 8px;
color: #b7c6da;
font-size: 11px;
font-weight: 650;
}
.field-label output {
color: var(--text);
font-variant-numeric: tabular-nums;
}
.stat-switch {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 4px;
padding: 4px;
border: 1px solid var(--line);
border-radius: 11px;
background: rgba(4, 8, 16, 0.42);
}
.stat-switch button {
min-height: 38px;
border: 0;
border-radius: 8px;
background: transparent;
color: var(--muted);
font-size: 10px;
font-weight: 700;
cursor: pointer;
}
.stat-switch button.active {
background: rgba(60, 216, 199, 0.13);
color: #dffffb;
box-shadow: inset 0 0 0 1px rgba(60, 216, 199, 0.28);
}
input[type="range"] {
width: 100%;
height: 4px;
margin: 7px 0;
border-radius: 999px;
appearance: none;
background: linear-gradient(90deg, var(--cyan) var(--fill, 50%), #243149 var(--fill, 50%));
cursor: pointer;
}
input[type="range"]::-webkit-slider-thumb {
width: 14px;
height: 14px;
appearance: none;
border: 2px solid #07121d;
border-radius: 50%;
background: #dffffb;
box-shadow: 0 0 0 2px rgba(60, 216, 199, 0.65);
}
input[type="range"]::-moz-range-thumb {
width: 12px;
height: 12px;
border: 2px solid #07121d;
border-radius: 50%;
background: #dffffb;
box-shadow: 0 0 0 2px rgba(60, 216, 199, 0.65);
}
.seed-row {
display: grid;
grid-template-columns: 1fr 38px;
gap: 7px;
}
.seed-row input,
.speed-select {
width: 100%;
height: 36px;
border: 1px solid var(--line);
border-radius: 8px;
outline: none;
background: rgba(4, 8, 16, 0.5);
color: var(--text);
padding: 0 10px;
font-size: 11px;
}
.seed-row input:focus,
.speed-select:focus {
border-color: rgba(60, 216, 199, 0.5);
}
.icon-button {
border: 1px solid var(--line);
border-radius: 8px;
background: rgba(4, 8, 16, 0.5);
color: var(--gold);
cursor: pointer;
font-size: 17px;
}
.run-button {
width: 100%;
min-height: 45px;
margin-top: 17px;
display: flex;
align-items: center;
justify-content: space-between;
border: 0;
border-radius: 10px;
padding: 0 14px;
background: linear-gradient(100deg, #36d6c4, #79e0d6);
color: #06131b;
font-size: 12px;
font-weight: 850;
box-shadow: 0 9px 28px rgba(60, 216, 199, 0.17);
cursor: pointer;
transition: transform 150ms ease, filter 150ms ease;
}
.run-button:hover {
transform: translateY(-1px);
filter: brightness(1.06);
}
.run-button.running {
background: linear-gradient(100deg, #eab34f, #ffd47a);
}
.run-button span {
font-size: 18px;
}
.section-rule {
height: 1px;
margin: 18px 0 14px;
background: var(--line);
}
.legend {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 9px 8px;
}
.legend-item {
display: flex;
align-items: center;
gap: 7px;
color: #9eb0c7;
font-size: 10px;
}
.swatch {
width: 8px;
height: 8px;
flex: 0 0 auto;
border-radius: 2px;
box-shadow: 0 0 9px currentColor;
}
.flow {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 5px;
margin-top: 15px;
}
.flow-step {
min-height: 59px;
padding: 9px 7px;
border: 1px solid var(--line);
border-radius: 9px;
background: rgba(4, 8, 16, 0.28);
}
.flow-step small {
display: block;
color: #647a98;
font-size: 8px;
font-weight: 800;
letter-spacing: 0.1em;
}
.flow-step b {
display: block;
margin-top: 7px;
color: #cbd8e8;
font-size: 10px;
line-height: 1.3;
}
.metrics {
top: 88px;
right: 24px;
width: 304px;
padding: 17px;
}
.metric-grid {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 8px;
margin-top: 13px;
}
.metric {
min-height: 83px;
padding: 12px;
border: 1px solid var(--line);
border-radius: 11px;
background: rgba(4, 9, 18, 0.32);
}
.metric.observed {
border-color: rgba(255, 200, 87, 0.25);
background: linear-gradient(145deg, rgba(255, 200, 87, 0.08), rgba(4, 9, 18, 0.3));
}
.metric-label {
color: var(--muted);
font-size: 9px;
font-weight: 750;
letter-spacing: 0.06em;
text-transform: uppercase;
}
.metric-value {
margin-top: 8px;
font-size: 19px;
font-weight: 780;
letter-spacing: -0.035em;
font-variant-numeric: tabular-nums;
}
.metric-sub {
margin-top: 3px;
color: #7187a3;
font-size: 9px;
}
.chart-card {
margin-top: 12px;
padding-top: 13px;
border-top: 1px solid var(--line);
}
.chart-heading {
display: flex;
justify-content: space-between;
align-items: center;
}
.chart-heading h3 {
margin: 0;
font-size: 11px;
}
#histogram {
display: block;
width: 100%;
height: 108px;
margin-top: 7px;
overflow: visible;
}
.chart-legend {
display: flex;
gap: 12px;
flex-wrap: wrap;
margin-top: 4px;
color: #8ca0b9;
font-size: 9px;
}
.chart-legend span {
display: flex;
align-items: center;
gap: 5px;
}
.insight {
margin-top: 12px;
padding: 11px;
border-left: 2px solid var(--cyan);
border-radius: 0 8px 8px 0;
background: rgba(60, 216, 199, 0.055);
color: #aebed1;
font-size: 10px;
line-height: 1.5;
}
.world-label {
position: fixed;
z-index: 3;
left: 0;
top: 0;
pointer-events: none;
padding: 7px 9px;
border: 1px solid rgba(138, 171, 211, 0.16);
border-radius: 7px;
background: rgba(5, 10, 19, 0.72);
color: #aebfd3;
font-size: 9px;
font-weight: 800;
letter-spacing: 0.1em;
text-transform: uppercase;
white-space: nowrap;
backdrop-filter: blur(8px);
transform: translate(-50%, -100%);
}
.world-label.gold {
border-color: rgba(255, 200, 87, 0.28);
color: var(--gold);
}
.tooltip {
position: fixed;
z-index: 20;
display: none;
min-width: 155px;
max-width: 220px;
padding: 10px 11px;
border: 1px solid rgba(139, 176, 217, 0.25);
border-radius: 9px;
background: rgba(8, 14, 25, 0.96);
box-shadow: 0 15px 35px rgba(0, 0, 0, 0.36);
pointer-events: none;
}
.tooltip-title {
margin-bottom: 5px;
color: #f2f7ff;
font-size: 11px;
font-weight: 800;
}
.tooltip-row {
display: flex;
justify-content: space-between;
gap: 18px;
margin-top: 4px;
color: #8da1ba;
font-size: 10px;
}
.tooltip-row b {
color: #dce8f6;
font-variant-numeric: tabular-nums;
}
.hint {
position: fixed;
z-index: 3;
right: 24px;
bottom: 18px;
display: flex;
align-items: center;
gap: 12px;
color: #61748e;
font-size: 9px;
letter-spacing: 0.03em;
pointer-events: none;
}
.hint kbd {
padding: 4px 6px;
border: 1px solid var(--line);
border-radius: 5px;
background: rgba(8, 14, 24, 0.64);
color: #9aadc4;
font: inherit;
}
@media (max-width: 1050px) {
.controls { width: 272px; left: 15px; }
.metrics { width: 272px; right: 15px; }
.topbar { left: 16px; right: 16px; }
.subtitle { display: none; }
}
@media (max-width: 760px) {
.topbar {
top: 13px;
left: 13px;
right: 13px;
}
h1 { font-size: 15px; }
.eyebrow { font-size: 8px; }
.brand-mark { width: 28px; height: 28px; }
.status { display: none; }
.metrics {
top: 62px;
left: 12px;
right: 12px;
width: auto;
padding: 11px;
}
.metrics > .panel-heading,
.chart-card,
.insight { display: none; }
.metric-grid {
grid-template-columns: repeat(4, 1fr);
gap: 5px;
margin: 0;
}
.metric {
min-height: 62px;
padding: 8px;
}
.metric-value { font-size: 14px; }
.metric-sub { display: none; }
.metric-label { font-size: 7px; }
.controls {
top: auto;
left: 12px;
right: 12px;
bottom: 12px;
width: auto;
max-height: 42vh;
padding: 14px;
}
.intro,
.legend,
.flow,
.section-rule { display: none; }
.field-label { margin-top: 10px; }
.stat-switch button { min-height: 33px; }
.run-button { min-height: 40px; margin-top: 12px; }
.world-label { display: none !important; }
.hint { display: none; }
}
</style>
</head>
<body>
<canvas id="scene" aria-label="Interactive 3D voxel bootstrap visualization"></canvas>
<header class="topbar">
<div class="brand">
<div class="brand-mark" aria-hidden="true">
<i></i><i></i><i></i><i></i><i></i><i></i><i></i><i></i><i></i>
</div>
<div>
<p class="eyebrow">Northstar Market · fictional data</p>
<h1>Voxel Bootstrap</h1>
<p class="subtitle">Resample purchases with replacement · compare repeated summaries</p>
</div>
</div>
<div class="top-actions">
<div class="status" id="status">
<span class="status-dot"></span>
<span id="statusText">READY</span>
</div>
<button class="ghost-button" id="resetView" type="button">Reset view</button>
</div>
</header>
<aside class="panel controls">
<div class="panel-heading">
<h2>Bootstrap controls</h2>
<span class="mini-label">Interactive</span>
</div>
<p class="intro">
Each bootstrap round draws purchases from the observed market sample
<strong>with replacement</strong>, then calculates one summary statistic.
</p>
<div class="stat-switch" role="group" aria-label="Summary statistic">
<button type="button" class="active" data-metric="mean">Mean spend</button>
<button type="button" data-metric="median">Median spend</button>
<button type="button" data-metric="high">Spend > $70</button>
</div>
<label class="field-label" for="iterations">
Replications
<output id="iterationsOut">80</output>
</label>
<input id="iterations" type="range" min="24" max="160" step="8" value="80" />
<label class="field-label" for="sampleSize">
Purchases per round
<output id="sampleSizeOut">48</output>
</label>
<input id="sampleSize" type="range" min="24" max="60" step="4" value="48" />
<label class="field-label" for="seed">Random seed</label>
<div class="seed-row">
<input id="seed" type="number" min="0" max="999999" value="7319" />
<button class="icon-button" id="randomSeed" type="button" aria-label="Choose a random seed">↻</button>
</div>
<label class="field-label" for="speed">
Reveal speed
<span class="mini-label">Animated</span>
</label>
<select id="speed" class="speed-select">
<option value="0.5">0.5×</option>
<option value="1" selected>1×</option>
<option value="2">2×</option>
<option value="4">4×</option>
</select>
<button class="run-button" id="run" type="button">
<span id="runText">Run bootstrap</span>
<span>→</span>
</button>
<div class="section-rule"></div>
<div class="mini-label" style="margin-bottom:10px">Observed basket categories</div>
<div class="legend" id="categoryLegend"></div>
<div class="flow">
<div class="flow-step">
<small>01 · SAMPLE</small>
<b>Draw with replacement</b>
</div>
<div class="flow-step">
<small>02 · REPEAT</small>
<b>80 independent rounds</b>
</div>
<div class="flow-step">
<small>03 · COMPARE</small>
<b>Summary vs. original</b>
</div>
</div>
</aside>
<aside class="panel metrics">
<div class="panel-heading">
<h2>Summary comparison</h2>
<span class="mini-label" id="metricName">Mean spend</span>
</div>
<div class="metric-grid" aria-live="polite">
<div class="metric observed">
<div class="metric-label">Original</div>
<div class="metric-value" id="originalValue">—</div>
<div class="metric-sub">Observed sample</div>
</div>
<div class="metric">
<div class="metric-label">Bootstrap avg.</div>
<div class="metric-value" id="averageValue">—</div>
<div class="metric-sub">Across rounds</div>
</div>
<div class="metric">
<div class="metric-label">95% interval</div>
<div class="metric-value" id="intervalValue">—</div>
<div class="metric-sub">Percentile range</div>
</div>
<div class="metric">
<div class="metric-label">Above original</div>
<div class="metric-value" id="aboveValue">—</div>
<div class="metric-sub">Of all rounds</div>
</div>
</div>
<div class="chart-card">
<div class="chart-heading">
<h3>Distribution of repeated summaries</h3>
<span class="mini-label" id="roundCount">80 rounds</span>
</div>
<svg id="histogram" viewBox="0 0 280 108" role="img"
aria-label="Histogram of bootstrap summaries"></svg>
<div class="chart-legend">
<span><i class="swatch" style="color:#3cd8c7;background:#3cd8c7"></i>Below original</span>
<span><i class="swatch" style="color:#9a83ff;background:#9a83ff"></i>Above original</span>
<span><i class="swatch" style="color:#ffc857;background:#ffc857"></i>Original</span>
</div>
</div>
<div class="insight" id="insight">
Run the bootstrap to compare the repeated summaries with the original observed sample.
</div>
</aside>
<div class="world-label" id="observedLabel">Observed sample · 48 purchases</div>
<div class="world-label" id="summaryLabel">Bootstrap summaries</div>
<div class="world-label gold" id="referenceLabel">Original summary</div>
<div class="tooltip" id="tooltip"></div>
<div class="hint">
<span><kbd>Drag</kbd> orbit</span>
<span><kbd>Wheel</kbd> zoom</span>
<span><kbd>Hover</kbd> inspect voxels</span>
</div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// ------------------------------------------------------------
// Data and statistics
// ------------------------------------------------------------
const categories = [
{ name: "Produce", color: 0x67d777 },
{ name: "Bakery", color: 0xf2b35b },
{ name: "Dairy", color: 0x73a7ff },
{ name: "Pantry", color: 0xbb86f2 },
{ name: "Snacks", color: 0xff738d },
{ name: "Drinks", color: 0x40d6d0 }
];
function mulberry32(seed) {
return function () {
let t = seed += 0x6D2B79F5;
t = Math.imul(t ^ t >>> 15, t | 1);
t ^= t + Math.imul(t ^ t >>> 7, t | 61);
return ((t ^ t >>> 14) >>> 0) / 4294967296;
};
}
const observedRandom = mulberry32(1847);
const basePrices = [48, 37, 58, 51, 34, 55];
const observed = Array.from({ length: 48 }, (_, i) => {
const categoryIndex = (i * 5 + Math.floor(i / 6)) % categories.length;
const amount = Math.round(
basePrices[categoryIndex] - 9 + observedRandom() * 61
);
return {
id: `P-${1042 + i}`,
category: categories[categoryIndex].name,
color: categories[categoryIndex].color,
amount
};
});
const metricDefinitions = {
mean: {
label: "Mean spend",
short: "Mean",
heightUnit: 6,
compute: rows => rows.reduce((sum, row) => sum + row.amount, 0) / rows.length,
format: value => `$${value.toFixed(1)}`
},
median: {
label: "Median spend",
short: "Median",
heightUnit: 6,
compute: rows => {
const sorted = rows.map(row => row.amount).sort((a, b) => a - b);
const middle = Math.floor(sorted.length / 2);
return sorted.length % 2
? sorted[middle]
: (sorted[middle - 1] + sorted[middle]) / 2;
},
format: value => `$${value.toFixed(1)}`
},
high: {
label: "Spend above $70",
short: "High-value share",
heightUnit: 5,
compute: rows =>
rows.filter(row => row.amount >= 70).length / rows.length * 100,
format: value => `${value.toFixed(0)}%`
}
};
function percentile(sorted, p) {
if (!sorted.length) return 0;
const index = (sorted.length - 1) * p;
const lower = Math.floor(index);
const upper = Math.ceil(index);
if (lower === upper) return sorted[lower];
return sorted[lower] + (sorted[upper] - sorted[lower]) * (index - lower);
}
function bootstrap(rows, sampleSize, repetitions, seed, metric) {
const random = mulberry32(seed >>> 0);
const result = [];
for (let repetition = 0; repetition < repetitions; repetition++) {
const sample = new Array(sampleSize);
for (let i = 0; i < sampleSize; i++) {
sample[i] = rows[Math.floor(random() * rows.length)];
}
result.push(metricDefinitions[metric].compute(sample));
}
return result;
}
// ------------------------------------------------------------
// Scene
// ------------------------------------------------------------
const canvas = document.querySelector("#scene");
const renderer = new THREE.WebGLRenderer({
canvas,
antialias: true,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.15;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x070b14);
scene.fog = new THREE.Fog(0x070b14, 40, 92);
const camera = new THREE.PerspectiveCamera(
42,
window.innerWidth / window.innerHeight,
0.1,
160
);
const cameraState = {
theta: 0.48,
phi: 1.02,
radius: 47,
target: new THREE.Vector3(-3, 4.2, 0)
};
function updateCamera() {
const horizontal = cameraState.radius * Math.sin(cameraState.phi);
camera.position.set(
cameraState.target.x + horizontal * Math.sin(cameraState.theta),
cameraState.target.y + cameraState.radius * Math.cos(cameraState.phi),
cameraState.target.z + horizontal * Math.cos(cameraState.theta)
);
camera.lookAt(cameraState.target);
}
updateCamera();
const hemisphere = new THREE.HemisphereLight(0xa9ccff, 0x080b14, 1.7);
scene.add(hemisphere);
const keyLight = new THREE.DirectionalLight(0xffffff, 2.5);
keyLight.position.set(7, 25, 17);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(1024, 1024);
keyLight.shadow.camera.left = -30;
keyLight.shadow.camera.right = 30;
keyLight.shadow.camera.top = 25;
keyLight.shadow.camera.bottom = -25;
keyLight.shadow.camera.near = 1;
keyLight.shadow.camera.far = 65;
scene.add(keyLight);
const cyanLight = new THREE.PointLight(0x35d8c9, 50, 42, 2);
cyanLight.position.set(-18, 10, -10);
scene.add(cyanLight);
const violetLight = new THREE.PointLight(0x8f79ff, 45, 38, 2);
violetLight.position.set(15, 9, 9);
scene.add(violetLight);
const platformY = 0.12;
const platformTop = 0.28;
const voxelHeight = 0.70;
const voxelBase = 0.34;
function createPlatform(centerX, width, depth, gridColor) {
const geometry = new THREE.BoxGeometry(width, 0.24, depth);
const material = new THREE.MeshStandardMaterial({
color: 0x0c1726,
roughness: 0.8,
metalness: 0.12
});
const mesh = new THREE.Mesh(geometry, material);
mesh.position.set(centerX, platformY, 0);
mesh.receiveShadow = true;
scene.add(mesh);
const edges = new THREE.LineSegments(
new THREE.EdgesGeometry(geometry),
new THREE.LineBasicMaterial({
color: 0x24425e,
transparent: true,
opacity: 0.52
})
);
edges.position.copy(mesh.position);
scene.add(edges);
const points = [];
for (let x = centerX - width / 2; x <= centerX + width / 2 + 0.01; x += 1.18) {
points.push(x, platformTop + 0.01, -depth / 2);
points.push(x, platformTop + 0.01, depth / 2);
}
for (let z = -depth / 2; z <= depth / 2 + 0.01; z += 1.18) {
points.push(centerX - width / 2, platformTop + 0.01, z);
points.push(centerX + width / 2, platformTop + 0.01, z);
}
const gridGeometry = new THREE.BufferGeometry();
gridGeometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(points, 3)
);
scene.add(new THREE.LineSegments(
gridGeometry,
new THREE.LineBasicMaterial({
color: gridColor,
transparent: true,
opacity: 0.32
})
));
}
createPlatform(-14.45, 11.3, 9.4, 0x31516d);
createPlatform(4.55, 19.7, 9.4, 0x20576b);
// Subtle voxel dust in the background.
const dustPositions = [];
const dustRandom = mulberry32(9090);
for (let i = 0; i < 260; i++) {
dustPositions.push(
(dustRandom() - 0.5) * 62,
dustRandom() * 26 + 0.5,
(dustRandom() - 0.5) * 44
);
}
const dustGeometry = new THREE.BufferGeometry();
dustGeometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(dustPositions, 3)
);
const dust = new THREE.Points(
dustGeometry,
new THREE.PointsMaterial({
color: 0x6c91b8,
size: 0.045,
transparent: true,
opacity: 0.28,
depthWrite: false
})
);
scene.add(dust);
// ------------------------------------------------------------
// Instanced voxel data
// ------------------------------------------------------------
const observedGeometry = new THREE.BoxGeometry(0.88, voxelHeight, 0.88);
const observedMaterial = new THREE.MeshStandardMaterial({
color: 0xffffff,
roughness: 0.56,
metalness: 0.08,
emissive: 0x061018,
emissiveIntensity: 0.28
});
const observedMesh = new THREE.InstancedMesh(
observedGeometry,
observedMaterial,
observed.length * 20
);
observedMesh.castShadow = true;
observedMesh.receiveShadow = true;
observedMesh.frustumCulled = false;
scene.add(observedMesh);
const summaryGeometry = new THREE.BoxGeometry(0.82, voxelHeight, 0.82);
const summaryMaterial = new THREE.MeshStandardMaterial({
color: 0xffffff,
roughness: 0.42,
metalness: 0.15,
emissive: 0x07191e,
emissiveIntensity: 0.45
});
const summaryMesh = new THREE.InstancedMesh(
summaryGeometry,
summaryMaterial,
160 * 23
);
summaryMesh.castShadow = true;
summaryMesh.receiveShadow = true;
summaryMesh.frustumCulled = false;
summaryMesh.count = 0;
scene.add(summaryMesh);
const matrix = new THREE.Matrix4();
const position = new THREE.Vector3();
const quaternion = new THREE.Quaternion();
const scale = new THREE.Vector3(1, 1, 1);
const color = new THREE.Color();
let observedMap = [];
let summaryMap = [];
let summaryCumulative = [];
function setVoxel(mesh, index, x, y, z, scaleY, voxelColor) {
position.set(x, y, z);
scale.set(1, scaleY, 1);
matrix.compose(position, quaternion, scale);
mesh.setMatrixAt(index, matrix);
mesh.setColorAt(index, voxelColor);
}
function buildObserved() {
observedMap = [];
let index = 0;
const columns = 8;
const rows = Math.ceil(observed.length / columns);
observed.forEach((purchase, purchaseIndex) => {
const column = purchaseIndex % columns;
const row = Math.floor(purchaseIndex / columns);
const x = -18.85 + column * 1.22;
const z = (row - (rows - 1) / 2) * 1.22;
const units = purchase.amount / 6;
const full = Math.floor(units);
const fraction = units - full;
for (let level = 0; level < full; level++) {
const shade = new THREE.Color(purchase.color).lerp(
new THREE.Color(0xffffff),
level * 0.018
);
setVoxel(
observedMesh,
index,
x,
voxelBase + voxelHeight * (level + 0.5),
z,
1,
shade
);
observedMap[index] = purchase;
index++;
}
if (fraction > 0.04) {
const shade = new THREE.Color(purchase.color).lerp(
new THREE.Color(0xffffff),
0.08
);
setVoxel(
observedMesh,
index,
x,
voxelBase + voxelHeight * (full + fraction / 2),
z,
fraction,
shade
);
observedMap[index] = purchase;
index++;
}
});
observedMesh.count = index;
observedMesh.instanceMatrix.needsUpdate = true;
if (observedMesh.instanceColor) observedMesh.instanceColor.needsUpdate = true;
}
const belowColor = new THREE.Color(0x3cd8c7);
const aboveColor = new THREE.Color(0x9a83ff);
const equalColor = new THREE.Color(0xffc857);
function buildSummary(results, observedMetric) {
summaryMap = [];
summaryCumulative = [];
let index = 0;
const columns = 14;
const rows = Math.ceil(results.length / columns);
const metric = metricDefinitions[state.metric];
results.forEach((value, repetition) => {
const column = repetition % columns;
const row = Math.floor(repetition / columns);
const x = -3.95 + column * 1.20;
const z = (row - (rows - 1) / 2) * 1.20;
const units = value / metric.heightUnit;
const full = Math.floor(units);
const fraction = units - full;
const difference = value - observedMetric;
const baseColor = Math.abs(difference) < 1e-8
? equalColor
: difference < 0 ? belowColor : aboveColor;
for (let level = 0; level < full; level++) {
const shade = baseColor.clone().lerp(
new THREE.Color(0xffffff),
Math.min(0.12, level * 0.01)
);
setVoxel(
summaryMesh,
index,
x,
voxelBase + voxelHeight * (level + 0.5),
z,
1,
shade
);
summaryMap[index] = { repetition, value };
index++;
}
if (fraction > 0.04) {
const shade = baseColor.clone().lerp(new THREE.Color(0xffffff), 0.1);
setVoxel(
summaryMesh,
index,
x,
voxelBase + voxelHeight * (full + fraction / 2),
z,
fraction,
shade
);
summaryMap[index] = { repetition, value };
index++;
}
summaryCumulative[repetition] = index;
});
summaryMesh.count = 0;
summaryMesh.instanceMatrix.needsUpdate = true;
if (summaryMesh.instanceColor) summaryMesh.instanceColor.needsUpdate = true;
}
// ------------------------------------------------------------
// Original observed summary marker
// ------------------------------------------------------------
const referenceMaterial = new THREE.MeshStandardMaterial({
color: 0xffc857,
emissive: 0xffa72b,
emissiveIntensity: 0.65,
roughness: 0.28,
metalness: 0.45
});
const referencePost = new THREE.Mesh(
new THREE.BoxGeometry(0.18, 1, 0.18),
referenceMaterial
);
referencePost.position.set(-5.18, 1, -4.38);
scene.add(referencePost);
const referenceRail = new THREE.Mesh(
new THREE.BoxGeometry(18.6, 0.055, 0.075),
new THREE.MeshBasicMaterial({
color: 0xffc857,
transparent: true,
opacity: 0.82
})
);
referenceRail.position.set(4.12, 5, -4.38);
scene.add(referenceRail);
function updateReference(observedMetric) {
const metric = metricDefinitions[state.metric];
const height = Math.max(
0.18,
observedMetric / metric.heightUnit * voxelHeight
);
referencePost.scale.y = height;
referencePost.position.y = voxelBase + height / 2;
referenceRail.position.y = voxelBase + height;
document.querySelector("#referenceLabel").textContent =
`Original ${metric.format(observedMetric)}`;
}
// ------------------------------------------------------------
// UI and statistics
// ------------------------------------------------------------
const state = {
metric: "mean",
iterations: 80,
sampleSize: 48,
seed: 7319,
speed: 1,
results: [],
revealStart: 0,
revealDuration: 1500,
revealing: false
};
const elements = {
iterations: document.querySelector("#iterations"),
iterationsOut: document.querySelector("#iterationsOut"),
sampleSize: document.querySelector("#sampleSize"),
sampleSizeOut: document.querySelector("#sampleSizeOut"),
seed: document.querySelector("#seed"),
speed: document.querySelector("#speed"),
run: document.querySelector("#run"),
runText: document.querySelector("#runText"),
status: document.querySelector("#status"),
statusText: document.querySelector("#statusText"),
originalValue: document.querySelector("#originalValue"),
averageValue: document.querySelector("#averageValue"),
intervalValue: document.querySelector("#intervalValue"),
aboveValue: document.querySelector("#aboveValue"),
metricName: document.querySelector("#metricName"),
roundCount: document.querySelector("#roundCount"),
insight: document.querySelector("#insight"),
histogram: document.querySelector("#histogram"),
tooltip: document.querySelector("#tooltip")
};
function updateRangeFill(input) {
const percentage =
(input.value - input.min) / (input.max - input.min) * 100;
input.style.setProperty("--fill", `${percentage}%`);
}
function updateHistogram(results, observedMetric) {
const width = 280;
const height = 108;
const left = 8;
const right = 8;
const top = 10;
const bottom = 22;
const plotWidth = width - left - right;
const plotHeight = height - top - bottom;
const sorted = [...results].sort((a, b) => a - b);
const low = percentile(sorted, 0.025);
const high = percentile(sorted, 0.975);
const domainMin = Math.min(...results, observedMetric);
const domainMax = Math.max(...results, observedMetric);
const padding = Math.max((domainMax - domainMin) * 0.08, 0.01);
const min = domainMin - padding;
const max = domainMax + padding;
const x = value => left + (value - min) / (max - min) * plotWidth;
const bins = 12;
const counts = new Array(bins).fill(0);
for (const value of results) {
const bin = Math.min(
bins - 1,
Math.max(0, Math.floor((value - min) / (max - min) * bins))
);
counts[bin]++;
}
const maxCount = Math.max(...counts, 1);
const barWidth = plotWidth / bins;
let svg = `
<defs>
<linearGradient id="barGradient" x1="0" y1="1" x2="0" y2="0">
<stop offset="0" stop-color="#245c70" stop-opacity="0.7"/>
<stop offset="1" stop-color="#42dac9" stop-opacity="0.95"/>
</linearGradient>
</defs>
<rect x="${x(low)}" y="${top}" width="${Math.max(1, x(high) - x(low))}"
height="${plotHeight}" fill="#3cd8c7" opacity="0.055"/>
<line x1="${left}" y1="${top + plotHeight}" x2="${width - right}"
y2="${top + plotHeight}" stroke="#344760" stroke-width="1"/>
`;
counts.forEach((count, index) => {
const barHeight = count / maxCount * plotHeight;
const barX = left + index * barWidth + 2;
const barY = top + plotHeight - barHeight;
svg += `
<rect x="${barX}" y="${barY}" width="${Math.max(2, barWidth - 4)}"
height="${barHeight}" rx="2" fill="url(#barGradient)"/>
`;
});
svg += `
<line x1="${x(observedMetric)}" y1="${top - 3}"
x2="${x(observedMetric)}" y2="${top + plotHeight + 3}"
stroke="#ffc857" stroke-width="2"/>
<circle cx="${x(observedMetric)}" cy="${top - 4}" r="3" fill="#ffc857"/>
<text x="${width / 2}" y="${height - 3}" text-anchor="middle"
fill="#657b96" font-size="8">REPEATED SUMMARY VALUES</text>
`;
elements.histogram.innerHTML = svg;
}
function updateStatistics() {
const metric = metricDefinitions[state.metric];
const observedMetric = metric.compute(observed);
const results = state.results;
const sorted = [...results].sort((a, b) => a - b);
const average = results.reduce((sum, value) => sum + value, 0) / results.length;
const low = percentile(sorted, 0.025);
const high = percentile(sorted, 0.975);
const above = results.filter(value => value > observedMetric).length / results.length;
elements.originalValue.textContent = metric.format(observedMetric);
elements.averageValue.textContent = metric.format(average);
elements.intervalValue.textContent = `${metric.format(low)}–${metric.format(high)}`;
elements.aboveValue.textContent = `${Math.round(above * 100)}%`;
elements.metricName.textContent = metric.label;
elements.roundCount.textContent = `${results.length} rounds`;
const difference = average - observedMetric;
const direction = difference >= 0 ? "above" : "below";
elements.insight.innerHTML =
`The bootstrap center is <strong>${metric.format(Math.abs(difference))} ${direction}</strong> ` +
`the original. The gold marker shows the observed value; repeated columns show ` +
`each independently resampled estimate.`;
updateHistogram(results, observedMetric);
updateReference(observedMetric);
return observedMetric;
}
function runBootstrap() {
state.iterations = Number(elements.iterations.value);
state.sampleSize = Number(elements.sampleSize.value);
state.seed = Math.max(0, Math.min(999999, Number(elements.seed.value) || 0));
elements.seed.value = state.seed;
state.results = bootstrap(
observed,
state.sampleSize,
state.iterations,
state.seed,
state.metric
);
const observedMetric = metricDefinitions[state.metric].compute(observed);
buildSummary(state.results, observedMetric);
updateStatistics();
state.revealStart = performance.now();
state.revealDuration = Math.max(900, state.iterations * 17) / state.speed;
state.revealing = true;
elements.run.classList.add("running");
elements.runText.textContent = "Resampling…";
elements.status.classList.add("running");
elements.statusText.textContent = "RUNNING";
}
elements.run.addEventListener("click", runBootstrap);
document.querySelectorAll("[data-metric]").forEach(button => {
button.addEventListener("click", () => {
document.querySelectorAll("[data-metric]").forEach(item => {
item.classList.toggle("active", item === button);
});
state.metric = button.dataset.metric;
runBootstrap();
});
});
elements.iterations.addEventListener("input", () => {
elements.iterationsOut.textContent = elements.iterations.value;
updateRangeFill(elements.iterations);
});
elements.iterations.addEventListener("change", runBootstrap);
elements.sampleSize.addEventListener("input", () => {
elements.sampleSizeOut.textContent = elements.sampleSize.value;
updateRangeFill(elements.sampleSize);
});
elements.sampleSize.addEventListener("change", runBootstrap);
elements.seed.addEventListener("change", runBootstrap);
elements.speed.addEventListener("change", () => {
state.speed = Number(elements.speed.value);
});
document.querySelector("#randomSeed").addEventListener("click", () => {
elements.seed.value = Math.floor(Math.random() * 1000000);
runBootstrap();
});
// Category legend.
const legend = document.querySelector("#categoryLegend");
categories.forEach(category => {
const item = document.createElement("div");
item.className = "legend-item";
item.innerHTML = `
<i class="swatch"
style="color:#${category.color.toString(16).padStart(6, "0")};
background:#${category.color.toString(16).padStart(6, "0")}"></i>
${category.name}
`;
legend.appendChild(item);
});
// ------------------------------------------------------------
// Camera controls implemented directly with Three.js core
// ------------------------------------------------------------
let dragging = false;
let dragMoved = false;
let lastX = 0;
let lastY = 0;
canvas.addEventListener("pointerdown", event => {
dragging = true;
dragMoved = false;
lastX = event.clientX;
lastY = event.clientY;
canvas.setPointerCapture(event.pointerId);
canvas.style.cursor = "grabbing";
});
canvas.addEventListener("pointermove", event => {
const rect = canvas.getBoundingClientRect();
if (dragging) {
const dx = event.clientX - lastX;
const dy = event.clientY - lastY;
if (Math.abs(dx) + Math.abs(dy) > 2) dragMoved = true;
cameraState.theta -= dx * 0.005;
cameraState.phi = THREE.MathUtils.clamp(
cameraState.phi - dy * 0.0045,
0.35,
1.42
);
lastX = event.clientX;
lastY = event.clientY;
elements.tooltip.style.display = "none";
return;
}
pointer.x = ((event.clientX - rect.left) / rect.width) * 2 - 1;
pointer.y = -((event.clientY - rect.top) / rect.height) * 2 + 1;
inspectVoxels(event.clientX, event.clientY);
});
function endDrag(event) {
dragging = false;
canvas.style.cursor = "default";
if (event && canvas.hasPointerCapture(event.pointerId)) {
canvas.releasePointerCapture(event.pointerId);
}
}
canvas.addEventListener("pointerup", endDrag);
canvas.addEventListener("pointercancel", endDrag);
canvas.addEventListener("pointerleave", () => {
if (!dragging) elements.tooltip.style.display = "none";
});
canvas.addEventListener("wheel", event => {
event.preventDefault();
cameraState.radius = THREE.MathUtils.clamp(
cameraState.radius * Math.exp(event.deltaY * 0.001),
28,
76
);
}, { passive: false });
canvas.addEventListener("dblclick", resetCamera);
window.addEventListener("keydown", event => {
if (event.key === "ArrowLeft") cameraState.theta += 0.08;
if (event.key === "ArrowRight") cameraState.theta -= 0.08;
if (event.key === "ArrowUp") {
cameraState.phi = THREE.MathUtils.clamp(cameraState.phi - 0.06, 0.35, 1.42);
}
if (event.key === "ArrowDown") {
cameraState.phi = THREE.MathUtils.clamp(cameraState.phi + 0.06, 0.35, 1.42);
}
if (event.key === "+" || event.key === "=") {
cameraState.radius = Math.max(28, cameraState.radius - 2);
}
if (event.key === "-") {
cameraState.radius = Math.min(76, cameraState.radius + 2);
}
});
function resetCamera() {
cameraState.theta = 0.48;
cameraState.phi = 1.02;
cameraState.radius = window.innerWidth < 760 ? 58 : 47;
cameraState.target.set(-3, 4.2, 0);
}
document.querySelector("#resetView").addEventListener("click", resetCamera);
// ------------------------------------------------------------
// Hover inspection
// ------------------------------------------------------------
const raycaster = new THREE.Raycaster();
const pointer = new THREE.Vector2(2, 2);
function inspectVoxels(clientX, clientY) {
raycaster.setFromCamera(pointer, camera);
const hits = raycaster.intersectObjects([observedMesh, summaryMesh], false);
if (!hits.length) {
elements.tooltip.style.display = "none";
canvas.style.cursor = "default";
return;
}
const hit = hits[0];
const index = hit.instanceId;
if (hit.object === observedMesh && observedMap[index]) {
const purchase = observedMap[index];
const category = categories.find(item => item.name === purchase.category);
const hex = category.color.toString(16).padStart(6, "0");
elements.tooltip.innerHTML = `
<div class="tooltip-title">${purchase.id} · ${purchase.category}</div>
<div class="tooltip-row"><span>Basket total</span><b>$${purchase.amount.toFixed(2)}</b></div>
<div class="tooltip-row"><span>Category color</span><b style="color:#${hex}">${purchase.category}</b></div>
<div class="tooltip-row"><span>Source</span><b>Observed sample</b></div>
`;
} else if (hit.object === summaryMesh && summaryMap[index]) {
const info = summaryMap[index];
const metric = metricDefinitions[state.metric];
elements.tooltip.innerHTML = `
<div class="tooltip-title">Bootstrap round ${info.repetition + 1}</div>
<div class="tooltip-row"><span>${metric.label}</span><b>${metric.format(info.value)}</b></div>
<div class="tooltip-row"><span>Sample size</span><b>${state.sampleSize}</b></div>
<div class="tooltip-row"><span>Seed</span><b>${state.seed}</b></div>
`;
} else {
elements.tooltip.style.display = "none";
return;
}
elements.tooltip.style.display = "block";
const tooltipWidth = 220;
const left = Math.min(
clientX + 14,
window.innerWidth - tooltipWidth - 12
);
const top = Math.min(clientY + 14, window.innerHeight - 110);
elements.tooltip.style.left = `${Math.max(8, left)}px`;
elements.tooltip.style.top = `${Math.max(8, top)}px`;
canvas.style.cursor = "help";
}
// ------------------------------------------------------------
// Projected labels
// ------------------------------------------------------------
const observedAnchor = new THREE.Vector3(-14.3, 13.0, -4.2);
const summaryAnchor = new THREE.Vector3(4.8, 12.2, -4.2);
const referenceAnchor = new THREE.Vector3(-5.18, 2, -4.38);
function placeLabel(element, worldPosition) {
const projected = worldPosition.clone().project(camera);
const x = (projected.x * 0.5 + 0.5) * window.innerWidth;
const y = (-projected.y * 0.5 + 0.5) * window.innerHeight;
element.style.left = `${x}px`;
element.style.top = `${y}px`;
element.style.visibility =
projected.z > -1 && projected.z < 1 ? "visible" : "hidden";
}
// ------------------------------------------------------------
// Resize and animation
// ------------------------------------------------------------
function resize() {
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
}
window.addEventListener("resize", resize);
function updateReveal(now) {
if (!state.revealing) return;
const progress = Math.min(1, (now - state.revealStart) / state.revealDuration);
const repFloat = progress * state.results.length;
const completed = Math.floor(repFloat);
const fraction = repFloat - completed;
let visible = completed > 0 ? summaryCumulative[completed - 1] : 0;
if (completed < state.results.length) {
const previous = completed > 0 ? summaryCumulative[completed - 1] : 0;
const current = summaryCumulative[completed];
visible = previous + Math.floor((current - previous) * fraction);
}
summaryMesh.count = visible;
if (progress >= 1) {
state.revealing = false;
summaryMesh.count = summaryCumulative[state.results.length - 1] || 0;
elements.run.classList.remove("running");
elements.runText.textContent = "Run bootstrap";
elements.status.classList.remove("running");
elements.statusText.textContent = "READY";
}
}
function animate(now) {
updateCamera();
updateReveal(now);
referenceMaterial.emissiveIntensity =
0.53 + Math.sin(now * 0.0026) * 0.16;
dust.rotation.y = now * 0.000018;
camera.updateMatrixWorld();
placeLabel(document.querySelector("#observedLabel"), observedAnchor);
placeLabel(document.querySelector("#summaryLabel"), summaryAnchor);
const referenceHeight =
metricDefinitions[state.metric].compute(observed) /
metricDefinitions[state.metric].heightUnit * voxelHeight;
referenceAnchor.y = voxelBase + referenceHeight + 0.65;
placeLabel(document.querySelector("#referenceLabel"), referenceAnchor);
renderer.render(scene, camera);
}
// Initial state.
buildObserved();
updateRangeFill(elements.iterations);
updateRangeFill(elements.sampleSize);
resize();
runBootstrap();
renderer.setAnimationLoop(animate);
</script>
</body>
</html>
```