Prompt specification
Build or customize the Three.js 3D scene "Carnival chance observation booth" 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: Carnival chance observation booth
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/carnival-chance-observation-booth-x1795
Scene Description Prompt:
A voxel carnival booth compares independent synthetic coin histories, showing different short-run outcomes under identical probabilities. One HTML file using Three.js from cdnjs.
Working Three.js Starter (carnival-chance-observation-booth-x1795):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Twin Coin Cabinet</title>
<style>
:root {
--ink: #10162d;
--panel: rgba(17, 24, 48, 0.9);
--cream: #fff2cf;
--mint: #59e2c2;
--coral: #ff6b73;
--gold: #ffd166;
--muted: #aab4d2;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background:
radial-gradient(circle at 50% 30%, #27365d 0%, #151c36 45%, #0b1022 100%);
color: var(--cream);
font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, monospace;
}
button,
input {
font: inherit;
}
#scene,
#scene canvas {
position: fixed;
inset: 0;
width: 100%;
height: 100%;
}
#scene canvas {
display: block;
touch-action: none;
cursor: grab;
}
#scene canvas:active {
cursor: grabbing;
}
#vignette {
position: fixed;
inset: 0;
pointer-events: none;
background:
radial-gradient(circle at 50% 45%, transparent 50%, rgba(5, 8, 20, 0.48) 100%);
z-index: 2;
}
.hud {
position: fixed;
inset: 0;
z-index: 3;
pointer-events: none;
}
.topbar {
position: absolute;
top: 18px;
left: 20px;
right: 20px;
display: flex;
justify-content: space-between;
align-items: flex-start;
gap: 20px;
}
.brand {
display: flex;
flex-direction: column;
gap: 3px;
text-shadow: 0 3px 0 rgba(0, 0, 0, 0.35);
}
.kicker {
color: var(--mint);
font-size: 10px;
letter-spacing: 0.22em;
text-transform: uppercase;
}
.brand strong {
font-size: clamp(18px, 2vw, 28px);
letter-spacing: 0.06em;
}
.shared-badges {
display: flex;
gap: 8px;
flex-wrap: wrap;
justify-content: flex-end;
}
.badge {
padding: 9px 12px;
border: 2px solid rgba(255, 242, 207, 0.18);
background: rgba(12, 18, 39, 0.82);
box-shadow: 4px 4px 0 rgba(0, 0, 0, 0.22);
font-size: 12px;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.badge b {
color: var(--gold);
}
.controls {
position: absolute;
top: 96px;
right: 20px;
width: min(320px, calc(100vw - 40px));
padding: 18px;
pointer-events: auto;
background: var(--panel);
border: 2px solid rgba(255, 242, 207, 0.2);
box-shadow:
7px 7px 0 rgba(0, 0, 0, 0.24),
inset 0 0 0 1px rgba(255, 255, 255, 0.035);
backdrop-filter: blur(12px);
}
.controls h2 {
margin: 0 0 4px;
color: var(--gold);
font-size: 16px;
letter-spacing: 0.07em;
text-transform: uppercase;
}
.controls > p {
margin: 0 0 18px;
color: var(--muted);
font-size: 11px;
line-height: 1.55;
}
.control-row {
margin: 0 0 18px;
}
.control-row label {
display: flex;
align-items: center;
justify-content: space-between;
gap: 12px;
margin-bottom: 9px;
color: var(--cream);
font-size: 11px;
letter-spacing: 0.06em;
text-transform: uppercase;
}
output {
min-width: 54px;
padding: 4px 6px;
text-align: center;
color: var(--ink);
background: var(--gold);
font-weight: 800;
}
input[type="range"] {
width: 100%;
height: 6px;
margin: 0;
accent-color: var(--mint);
cursor: pointer;
}
.button-grid {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 9px;
}
button {
min-height: 42px;
padding: 10px 12px;
border: 0;
border-bottom: 4px solid #087e6d;
background: var(--mint);
color: #102039;
font-weight: 900;
font-size: 11px;
letter-spacing: 0.04em;
text-transform: uppercase;
cursor: pointer;
transition:
transform 100ms ease,
filter 100ms ease;
}
button:hover {
filter: brightness(1.08);
transform: translateY(-1px);
}
button:active {
transform: translateY(2px);
border-bottom-width: 2px;
}
button.secondary {
border-bottom-color: #9f3540;
background: var(--coral);
color: #fff8e8;
}
button.wide {
grid-column: 1 / -1;
border-bottom-color: #9a721d;
background: var(--gold);
}
.legend {
display: flex;
align-items: center;
gap: 14px;
margin-top: 16px;
color: var(--muted);
font-size: 10px;
letter-spacing: 0.06em;
text-transform: uppercase;
}
.swatch {
display: inline-block;
width: 12px;
height: 12px;
margin-right: 5px;
vertical-align: -1px;
border: 2px solid #14203c;
}
.swatch.heads {
background: var(--mint);
}
.swatch.tails {
background: var(--coral);
}
.note {
margin-top: 15px;
padding-top: 13px;
border-top: 1px solid rgba(255, 255, 255, 0.11);
color: #8996b9;
font-size: 10px;
line-height: 1.55;
}
.verdict {
position: absolute;
left: 20px;
bottom: 54px;
width: min(470px, calc(100vw - 40px));
padding: 12px 14px;
border-left: 5px solid var(--mint);
background: rgba(12, 18, 39, 0.86);
box-shadow: 5px 5px 0 rgba(0, 0, 0, 0.2);
color: var(--cream);
font-size: 11px;
line-height: 1.5;
}
.verdict strong {
display: block;
margin-bottom: 3px;
color: var(--mint);
font-size: 10px;
letter-spacing: 0.15em;
text-transform: uppercase;
}
.hint {
position: absolute;
right: 20px;
bottom: 18px;
color: rgba(255, 242, 207, 0.7);
font-size: 10px;
letter-spacing: 0.08em;
text-transform: uppercase;
text-shadow: 0 2px 0 #070b18;
}
#loading {
position: fixed;
inset: 0;
z-index: 10;
display: grid;
place-items: center;
background: #10162d;
color: var(--mint);
font-size: 13px;
letter-spacing: 0.15em;
text-transform: uppercase;
transition: opacity 250ms ease;
}
#loading.hidden {
opacity: 0;
pointer-events: none;
}
@media (max-width: 760px) {
.topbar {
top: 12px;
left: 12px;
right: 12px;
}
.shared-badges {
max-width: 130px;
}
.badge {
padding: 7px 8px;
font-size: 9px;
}
.controls {
top: auto;
right: 12px;
bottom: 12px;
width: calc(100vw - 24px);
padding: 13px;
}
.controls h2,
.controls > p,
.note {
display: none;
}
.control-row {
margin-bottom: 12px;
}
.verdict {
top: 78px;
bottom: auto;
left: 12px;
width: calc(100vw - 24px);
}
.hint {
display: none;
}
}
</style>
</head>
<body>
<div id="scene"></div>
<div id="vignette"></div>
<div class="hud">
<header class="topbar">
<div class="brand">
<span class="kicker">Parallel Probability / Exhibit 04</span>
<strong>TWIN COIN CABINET</strong>
</div>
<div class="shared-badges">
<div class="badge">Shared odds <b id="oddsBadge">P = 50%</b></div>
<div class="badge">Flips <b id="flipBadge">16</b></div>
</div>
</header>
<aside class="controls">
<h2>Fair-game console</h2>
<p>
Both cabinets use the same probability. Every square is an independent
synthetic coin toss.
</p>
<div class="control-row">
<label for="probability">
Head probability
<output id="probabilityOutput" for="probability">50%</output>
</label>
<input
id="probability"
type="range"
min="10"
max="90"
step="1"
value="50"
>
</div>
<div class="control-row">
<label for="flips">
Tosses per run
<output id="flipsOutput" for="flips">16</output>
</label>
<input
id="flips"
type="range"
min="6"
max="40"
step="2"
value="16"
>
</div>
<div class="button-grid">
<button id="newButton" class="secondary">New histories</button>
<button id="replayButton">Replay pair</button>
<button id="pauseButton" class="wide">Pause motion</button>
<button id="resetButton" class="wide" style="background:#dbe3ff;border-bottom-color:#7b88ad">
Reset camera
</button>
</div>
<div class="legend">
<span><i class="swatch heads"></i>Heads</span>
<span><i class="swatch tails"></i>Tails</span>
</div>
<div class="note">
A short streak can look meaningful even when the underlying odds are
unchanged. This exhibit compares the sampled paths, not the odds.
</div>
</aside>
<div class="verdict" aria-live="polite">
<strong>Live comparison</strong>
<span id="verdictText">Generating two independent histories…</span>
</div>
<div class="hint">Drag to orbit · Scroll to zoom · Double-click to reset</div>
</div>
<div id="loading">Building the coin cabinet…</div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// -------------------------------------------------------------------------
// Renderer and scene
// -------------------------------------------------------------------------
const host = document.getElementById("scene");
const renderer = new THREE.WebGLRenderer({
antialias: true,
alpha: false,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.12;
host.appendChild(renderer.domElement);
renderer.domElement.setAttribute(
"aria-label",
"Interactive voxel carnival booth comparing two coin-toss histories"
);
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x10162d);
scene.fog = new THREE.Fog(0x10162d, 18, 38);
const camera = new THREE.PerspectiveCamera(
38,
window.innerWidth / window.innerHeight,
0.1,
100
);
// Manual orbit state — no camera-control addons are used.
const orbit = {
target: new THREE.Vector3(0, 2.65, 0),
yaw: 0.02,
pitch: 0.24,
radius: window.innerWidth < 760 ? 18.5 : 16.6
};
function updateCamera() {
const horizontal = Math.cos(orbit.pitch) * orbit.radius;
camera.position.set(
orbit.target.x + Math.sin(orbit.yaw) * horizontal,
orbit.target.y + Math.sin(orbit.pitch) * orbit.radius,
orbit.target.z + Math.cos(orbit.yaw) * horizontal
);
camera.lookAt(orbit.target);
}
function resetCamera() {
orbit.yaw = 0.02;
orbit.pitch = 0.24;
orbit.radius = window.innerWidth < 760 ? 18.5 : 16.6;
updateCamera();
}
updateCamera();
// -------------------------------------------------------------------------
// Lighting
// -------------------------------------------------------------------------
scene.add(new THREE.HemisphereLight(0xa9ddff, 0x302344, 2.15));
const keyLight = new THREE.DirectionalLight(0xfff1cf, 3.4);
keyLight.position.set(7, 12, 8);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(2048, 2048);
keyLight.shadow.camera.left = -11;
keyLight.shadow.camera.right = 11;
keyLight.shadow.camera.top = 11;
keyLight.shadow.camera.bottom = -7;
keyLight.shadow.camera.near = 1;
keyLight.shadow.camera.far = 35;
keyLight.shadow.bias = -0.0005;
scene.add(keyLight);
const rimLight = new THREE.DirectionalLight(0x6e8dff, 1.8);
rimLight.position.set(-8, 7, -6);
scene.add(rimLight);
const warmLightLeft = new THREE.PointLight(0xffbd66, 24, 8, 2);
warmLightLeft.position.set(-3.8, 4.8, 2.5);
scene.add(warmLightLeft);
const warmLightRight = new THREE.PointLight(0xff7c89, 24, 8, 2);
warmLightRight.position.set(3.8, 4.8, 2.5);
scene.add(warmLightRight);
// -------------------------------------------------------------------------
// Materials and voxel helpers
// -------------------------------------------------------------------------
const material = (color, options = {}) =>
new THREE.MeshStandardMaterial({
color,
roughness: 0.78,
metalness: 0.02,
...options
});
const M = {
navy: material(0x121a34),
dark: material(0x0b1228),
cream: material(0xffedc4),
coral: material(0xff6874),
mint: material(0x52dfbd),
gold: material(0xffca55, { metalness: 0.12 }),
blue: material(0x5c78d8),
violet: material(0x7667d9),
wood: material(0x9a5d49),
ground: material(0x111a31, { roughness: 0.94 })
};
function box(width, height, depth, x, y, z, mat, parent = scene) {
const mesh = new THREE.Mesh(
new THREE.BoxGeometry(width, height, depth),
mat
);
mesh.position.set(x, y, z);
mesh.castShadow = true;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
function createCanvasTexture(width, height, draw) {
const canvas = document.createElement("canvas");
canvas.width = width;
canvas.height = height;
const context = canvas.getContext("2d");
context.imageSmoothingEnabled = false;
const texture = new THREE.CanvasTexture(canvas);
texture.colorSpace = THREE.SRGBColorSpace;
texture.magFilter = THREE.NearestFilter;
texture.minFilter = THREE.LinearFilter;
texture.generateMipmaps = false;
draw(context, width, height);
texture.needsUpdate = true;
return { canvas, context, texture };
}
function textPlane(texture, width, height, x, y, z, parent = scene) {
const mesh = new THREE.Mesh(
new THREE.PlaneGeometry(width, height),
new THREE.MeshBasicMaterial({
map: texture,
transparent: true,
toneMapped: false
})
);
mesh.position.set(x, y, z);
parent.add(mesh);
return mesh;
}
function drawPixelBorder(context, width, height, color, thickness = 12) {
context.fillStyle = color;
context.fillRect(0, 0, width, thickness);
context.fillRect(0, height - thickness, width, thickness);
context.fillRect(0, 0, thickness, height);
context.fillRect(width - thickness, 0, thickness, height);
}
// Pixel-art faces for the two possible coin outcomes.
const headsFace = createCanvasTexture(128, 128, (ctx) => {
ctx.fillStyle = "#59e2c2";
ctx.fillRect(0, 0, 128, 128);
ctx.fillStyle = "#102039";
ctx.fillRect(0, 0, 128, 12);
ctx.fillRect(0, 116, 128, 12);
ctx.fillRect(0, 0, 12, 128);
ctx.fillRect(116, 0, 12, 128);
// Pixel H.
ctx.fillRect(31, 27, 18, 74);
ctx.fillRect(79, 27, 18, 74);
ctx.fillRect(49, 53, 30, 18);
});
const tailsFace = createCanvasTexture(128, 128, (ctx) => {
ctx.fillStyle = "#ff6b73";
ctx.fillRect(0, 0, 128, 128);
ctx.fillStyle = "#321629";
ctx.fillRect(0, 0, 128, 12);
ctx.fillRect(0, 116, 128, 12);
ctx.fillRect(0, 0, 12, 128);
ctx.fillRect(116, 0, 12, 128);
// Pixel T.
ctx.fillRect(25, 28, 78, 18);
ctx.fillRect(55, 28, 18, 73);
});
const headsBodyMaterial = material(0x59e2c2, {
roughness: 0.55,
emissive: 0x0d382e,
emissiveIntensity: 0.35
});
const tailsBodyMaterial = material(0xff6b73, {
roughness: 0.55,
emissive: 0x48151e,
emissiveIntensity: 0.35
});
const headsFaceMaterial = new THREE.MeshBasicMaterial({
map: headsFace.texture,
toneMapped: false
});
const tailsFaceMaterial = new THREE.MeshBasicMaterial({
map: tailsFace.texture,
toneMapped: false
});
const coinGeometry = new THREE.BoxGeometry(1, 1, 0.28);
const coinFaceGeometry = new THREE.PlaneGeometry(0.82, 0.82);
// -------------------------------------------------------------------------
// Carnival booth
// -------------------------------------------------------------------------
const ground = new THREE.Mesh(
new THREE.PlaneGeometry(80, 80),
M.ground
);
ground.rotation.x = -Math.PI / 2;
ground.position.y = -0.22;
ground.receiveShadow = true;
scene.add(ground);
// Stage and counter.
box(13.1, 0.55, 6.25, 0, 0.05, 0, M.navy);
box(12.4, 0.35, 5.75, 0, 0.48, 0, M.gold);
box(11.8, 1.16, 1.52, 0, 1.03, 1.42, M.wood);
box(12.05, 0.26, 1.82, 0, 1.72, 1.42, M.cream);
// Counter paneling.
for (let x = -5.2; x <= 5.2; x += 1.3) {
box(1.05, 0.64, 0.08, x, 1.02, 2.205, x < 0 ? M.coral : M.mint);
}
// Back wall and side towers.
box(11.9, 5.25, 0.45, 0, 3.24, -2.55, M.navy);
box(0.72, 5.45, 3.45, -6.13, 3.22, -0.35, M.coral);
box(0.72, 5.45, 3.45, 6.13, 3.22, -0.35, M.mint);
// Front posts.
box(0.34, 4.55, 0.34, -5.48, 3.08, 1.78, M.gold);
box(0.34, 4.55, 0.34, 5.48, 3.08, 1.78, M.gold);
// Roof.
box(12.8, 0.55, 5.95, 0, 6.13, -0.12, M.navy);
// Striped voxel canopy.
const canopyMaterials = [M.coral, M.cream];
for (let i = 0; i < 10; i++) {
box(
1.17,
0.3,
3.72,
-5.4 + i * 1.2,
5.78,
0.08,
canopyMaterials[i % 2]
);
box(
1.13,
0.5,
0.35,
-5.4 + i * 1.2,
5.42,
2.0,
canopyMaterials[i % 2]
);
}
// Marquee bulbs and wire.
const bulbMaterial = material(0xffdc79, {
emissive: 0xff9f32,
emissiveIntensity: 2
});
box(10.8, 0.05, 0.05, 0, 5.96, 2.24, M.dark);
for (let i = 0; i < 11; i++) {
box(
0.17,
0.17,
0.17,
-5.2 + i * 1.04,
5.8,
2.25,
bulbMaterial
);
}
// -------------------------------------------------------------------------
// Dynamic display textures
// -------------------------------------------------------------------------
const marquee = createCanvasTexture(1200, 280, () => {});
const counterText = createCanvasTexture(1200, 160, () => {});
const boardHeaders = [
createCanvasTexture(1200, 190, () => {}),
createCanvasTexture(1200, 190, () => {})
];
const boardFooters = [
createCanvasTexture(1200, 160, () => {}),
createCanvasTexture(1200, 160, () => {})
];
const oddsBadges = [
createCanvasTexture(256, 256, () => {}),
createCanvasTexture(256, 256, () => {})
];
textPlane(marquee.texture, 6.25, 1.46, 0, 5.16, 2.25);
textPlane(counterText.texture, 5.82, 0.78, 0, 1.05, 2.255);
textPlane(oddsBadges[0].texture, 0.94, 0.94, -6.13, 3.65, 1.4);
textPlane(oddsBadges[1].texture, 0.94, 0.94, 6.13, 3.65, 1.4);
// -------------------------------------------------------------------------
// Twin history boards
// -------------------------------------------------------------------------
const boardGroups = [];
function createBoard(side, centerX, accentMaterial) {
const group = new THREE.Group();
scene.add(group);
box(5.18, 4.18, 0.34, centerX, 2.55, -1.84, M.dark, group);
// Colored frames.
box(0.15, 4.34, 0.18, centerX - 2.64, 2.55, -1.6, accentMaterial, group);
box(0.15, 4.34, 0.18, centerX + 2.64, 2.55, -1.6, accentMaterial, group);
box(5.4, 0.15, 0.18, centerX, 4.7, -1.6, accentMaterial, group);
box(5.4, 0.15, 0.18, centerX, 0.4, -1.6, accentMaterial, group);
// Corner bolts.
for (const x of [-2.47, 2.47]) {
for (const y of [0.58, 4.52]) {
box(0.13, 0.13, 0.13, centerX + x, y, -1.47, M.gold, group);
}
}
textPlane(
boardHeaders[side].texture,
4.74,
0.75,
centerX,
4.17,
-1.54,
group
);
textPlane(
boardFooters[side].texture,
4.74,
0.63,
centerX,
1.03,
-1.54,
group
);
const tokenGroup = new THREE.Group();
scene.add(tokenGroup);
boardGroups.push({ centerX, tokenGroup, tokens: [] });
}
createBoard(0, -2.73, M.coral);
createBoard(1, 2.73, M.mint);
// -------------------------------------------------------------------------
// Synthetic histories and animation
// -------------------------------------------------------------------------
const ui = {
probability: document.getElementById("probability"),
flips: document.getElementById("flips"),
probabilityOutput: document.getElementById("probabilityOutput"),
flipsOutput: document.getElementById("flipsOutput"),
oddsBadge: document.getElementById("oddsBadge"),
flipBadge: document.getElementById("flipBadge"),
verdict: document.getElementById("verdictText"),
newButton: document.getElementById("newButton"),
replayButton: document.getElementById("replayButton"),
pauseButton: document.getElementById("pauseButton"),
resetButton: document.getElementById("resetButton")
};
let probability = 0.5;
let flipCount = 16;
let histories = [[], []];
let animationStart = 0;
let simulationTime = 0;
let paused = false;
function makeHistory(length, headProbability) {
return Array.from({ length }, () => {
return Math.random() < headProbability ? "H" : "T";
});
}
function removeExistingTokens(board) {
for (const token of board.tokens) {
board.tokenGroup.remove(token.group);
}
board.tokens.length = 0;
}
function buildTokenGrid(board, history) {
removeExistingTokens(board);
const columns = history.length <= 10 ? 5 : 8;
const rows = Math.ceil(history.length / columns);
const cell = columns === 5 ? 0.69 : 0.54;
const size = cell * 0.73;
const gridWidth = (columns - 1) * cell;
const gridCenterY = 2.61;
history.forEach((outcome, index) => {
const column = index % columns;
const row = Math.floor(index / columns);
const group = new THREE.Group();
const body = new THREE.Mesh(
coinGeometry,
outcome === "H" ? headsBodyMaterial : tailsBodyMaterial
);
body.scale.set(size, size, 1);
body.castShadow = true;
group.add(body);
const face = new THREE.Mesh(
coinFaceGeometry,
outcome === "H" ? headsFaceMaterial : tailsFaceMaterial
);
face.scale.set(size, size, 1);
face.position.z = 0.146;
group.add(face);
group.position.set(
board.centerX - gridWidth / 2 + column * cell,
gridCenterY + ((rows - 1) / 2 - row) * 0.53,
-1.31
);
group.userData.delay = index * 0.045;
group.scale.setScalar(0.001);
board.tokenGroup.add(group);
board.tokens.push({ group, body, face, outcome });
});
}
function updateMarquee() {
const ctx = marquee.context;
const w = marquee.canvas.width;
const h = marquee.canvas.height;
ctx.clearRect(0, 0, w, h);
ctx.fillStyle = "#111831";
ctx.fillRect(0, 0, w, h);
drawPixelBorder(ctx, w, h, "#ffd166", 16);
ctx.textAlign = "center";
ctx.textBaseline = "middle";
ctx.fillStyle = "#fff2cf";
ctx.font = "900 64px ui-monospace, Menlo, Consolas, monospace";
ctx.fillText("TWIN COIN CABINET", w / 2, 83);
ctx.fillStyle = "#59e2c2";
ctx.font = "800 31px ui-monospace, Menlo, Consolas, monospace";
ctx.fillText(
`SAME ODDS: ${Math.round(probability * 100)}% / ${flipCount} TOSSES`,
w / 2,
151
);
ctx.fillStyle = "#ff6b73";
ctx.font = "700 24px ui-monospace, Menlo, Consolas, monospace";
ctx.fillText("TWO INDEPENDENT SAMPLE HISTORIES", w / 2, 216);
marquee.texture.needsUpdate = true;
}
function updateCounterText() {
const ctx = counterText.context;
const w = counterText.canvas.width;
const h = counterText.canvas.height;
ctx.clearRect(0, 0, w, h);
ctx.fillStyle = "#17223d";
ctx.fillRect(0, 0, w, h);
ctx.textAlign = "center";
ctx.textBaseline = "middle";
ctx.fillStyle = "#ffd166";
ctx.font = "900 45px ui-monospace, Menlo, Consolas, monospace";
ctx.fillText(
`EACH CABINET: P(HEADS) = ${Math.round(probability * 100)}%`,
w / 2,
h / 2
);
counterText.texture.needsUpdate = true;
}
function updateOddsBadges() {
for (const badge of oddsBadges) {
const ctx = badge.context;
const size = badge.canvas.width;
ctx.clearRect(0, 0, size, size);
ctx.fillStyle = "#ffd166";
ctx.fillRect(0, 0, size, size);
drawPixelBorder(ctx, size, size, "#10162d", 14);
ctx.textAlign = "center";
ctx.textBaseline = "middle";
ctx.fillStyle = "#10162d";
ctx.font = "900 53px ui-monospace, Menlo, Consolas, monospace";
ctx.fillText("P", size / 2, 73);
ctx.font = "900 45px ui-monospace, Menlo, Consolas, monospace";
ctx.fillText(`${Math.round(probability * 100)}%`, size / 2, 137);
badge.texture.needsUpdate = true;
}
}
function updateBoardLabels(side, history) {
const heads = history.filter((value) => value === "H").length;
const tails = history.length - heads;
const expected = history.length * probability;
const difference = heads - expected;
const observed = Math.round((heads / history.length) * 100);
const expectedPercent = Math.round(probability * 100);
const accent = side === 0 ? "#ff6b73" : "#59e2c2";
const letter = side === 0 ? "A" : "B";
const header = boardHeaders[side];
const headerCtx = header.context;
headerCtx.clearRect(0, 0, header.canvas.width, header.canvas.height);
headerCtx.fillStyle = "#111831";
headerCtx.fillRect(0, 0, header.canvas.width, header.canvas.height);
headerCtx.fillStyle = accent;
headerCtx.fillRect(0, 0, 18, header.canvas.height);
headerCtx.textBaseline = "middle";
headerCtx.textAlign = "left";
headerCtx.fillStyle = accent;
headerCtx.font = "900 61px ui-monospace, Menlo, Consolas, monospace";
headerCtx.fillText(`RUN ${letter}`, 49, 61);
headerCtx.fillStyle = "#fff2cf";
headerCtx.font = "800 41px ui-monospace, Menlo, Consolas, monospace";
headerCtx.fillText(
`${heads} HEADS / ${tails} TAILS`,
430,
62
);
headerCtx.fillStyle = "#aab4d2";
headerCtx.font = "700 23px ui-monospace, Menlo, Consolas, monospace";
headerCtx.fillText(
`INDEPENDENT HISTORY · ${history.length} TRIALS`,
430,
128
);
header.texture.needsUpdate = true;
const footer = boardFooters[side];
const footerCtx = footer.context;
footerCtx.clearRect(0, 0, footer.canvas.width, footer.canvas.height);
footerCtx.fillStyle = "#111831";
footerCtx.fillRect(0, 0, footer.canvas.width, footer.canvas.height);
footerCtx.textBaseline = "middle";
footerCtx.textAlign = "left";
footerCtx.fillStyle = accent;
footerCtx.font = "900 38px ui-monospace, Menlo, Consolas, monospace";
footerCtx.fillText(
`OBSERVED ${observed}%`,
35,
49
);
footerCtx.fillStyle = "#fff2cf";
footerCtx.font = "700 31px ui-monospace, Menlo, Consolas, monospace";
footerCtx.fillText(
`EXPECTED ${expectedPercent}%`,
415,
49
);
const deltaText =
`${difference >= 0 ? "+" : ""}${difference.toFixed(1)} HEADS FROM EXPECTED`;
footerCtx.fillStyle =
Math.abs(difference) < 1 ? "#ffd166" : accent;
footerCtx.font = "700 25px ui-monospace, Menlo, Consolas, monospace";
footerCtx.fillText(deltaText, 35, 111);
footer.texture.needsUpdate = true;
}
function updateVerdict() {
const first = histories[0];
const second = histories[1];
let divergence = -1;
for (let i = 0; i < first.length; i++) {
if (first[i] !== second[i]) {
divergence = i;
break;
}
}
if (divergence === -1) {
ui.verdict.textContent =
"This pair happens to match exactly. That is possible under the same odds; replay for another independent pair.";
} else {
const a = histories[0][divergence] === "H" ? "HEADS" : "TAILS";
const b = histories[1][divergence] === "H" ? "HEADS" : "TAILS";
ui.verdict.textContent =
`The runs first diverge at toss ${divergence + 1}: A shows ${a}, B shows ${b}. The probability did not change.`;
}
}
function syncLabels() {
const probabilityPercent = Math.round(probability * 100);
ui.probabilityOutput.textContent = `${probabilityPercent}%`;
ui.flipsOutput.textContent = `${flipCount}`;
ui.oddsBadge.textContent = `P = ${probabilityPercent}%`;
ui.flipBadge.textContent = `${flipCount}`;
}
function replayAnimation() {
animationStart = simulationTime;
}
function generateHistories(generateNew = true) {
if (generateNew) {
histories = [
makeHistory(flipCount, probability),
makeHistory(flipCount, probability)
];
}
for (let side = 0; side < 2; side++) {
buildTokenGrid(boardGroups[side], histories[side]);
updateBoardLabels(side, histories[side]);
}
updateMarquee();
updateCounterText();
updateOddsBadges();
updateVerdict();
syncLabels();
replayAnimation();
}
function setPaused(value) {
paused = value;
ui.pauseButton.textContent = paused ? "Resume motion" : "Pause motion";
}
let regenerateTimer;
function scheduleRegeneration() {
clearTimeout(regenerateTimer);
regenerateTimer = setTimeout(() => {
setPaused(false);
generateHistories(true);
}, 160);
}
ui.probability.addEventListener("input", () => {
probability = Number(ui.probability.value) / 100;
syncLabels();
scheduleRegeneration();
});
ui.flips.addEventListener("input", () => {
flipCount = Number(ui.flips.value);
syncLabels();
scheduleRegeneration();
});
ui.newButton.addEventListener("click", () => {
setPaused(false);
generateHistories(true);
});
ui.replayButton.addEventListener("click", () => {
setPaused(false);
replayAnimation();
});
ui.pauseButton.addEventListener("click", () => {
setPaused(!paused);
});
ui.resetButton.addEventListener("click", resetCamera);
// -------------------------------------------------------------------------
// Small carnival details
// -------------------------------------------------------------------------
const confetti = [];
const confettiMaterials = [M.coral, M.mint, M.gold, M.blue];
for (let i = 0; i < 24; i++) {
const mesh = box(
0.12,
0.12,
0.12,
-8 + Math.random() * 16,
1.2 + Math.random() * 5.6,
-4 + Math.random() * 5,
confettiMaterials[i % confettiMaterials.length]
);
mesh.castShadow = false;
mesh.userData.baseY = mesh.position.y;
mesh.userData.phase = Math.random() * Math.PI * 2;
confetti.push(mesh);
}
// Decorative blocks on the stage front.
for (let i = 0; i < 6; i++) {
box(
0.22,
0.22,
0.22,
-5.25 + i * 2.1,
1.97,
1.93,
i % 2 ? M.gold : M.blue
);
}
// -------------------------------------------------------------------------
// Manual camera interaction
// -------------------------------------------------------------------------
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.addEventListener("pointermove", (event) => {
if (!dragging) return;
const deltaX = event.clientX - pointerX;
const deltaY = event.clientY - pointerY;
pointerX = event.clientX;
pointerY = event.clientY;
orbit.yaw = THREE.MathUtils.clamp(
orbit.yaw - deltaX * 0.006,
-1.1,
1.1
);
orbit.pitch = THREE.MathUtils.clamp(
orbit.pitch + deltaY * 0.005,
0.08,
0.68
);
updateCamera();
});
function endDrag(event) {
dragging = false;
if (event.pointerId !== undefined) {
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();
orbit.radius = THREE.MathUtils.clamp(
orbit.radius + event.deltaY * 0.012,
10.5,
23
);
updateCamera();
},
{ passive: false }
);
renderer.domElement.addEventListener("dblclick", resetCamera);
window.addEventListener("keydown", (event) => {
if (event.target instanceof HTMLInputElement) return;
let changed = true;
switch (event.key) {
case "ArrowLeft":
orbit.yaw = Math.max(-1.1, orbit.yaw - 0.08);
break;
case "ArrowRight":
orbit.yaw = Math.min(1.1, orbit.yaw + 0.08);
break;
case "ArrowUp":
orbit.pitch = Math.min(0.68, orbit.pitch + 0.06);
break;
case "ArrowDown":
orbit.pitch = Math.max(0.08, orbit.pitch - 0.06);
break;
case "+":
case "=":
orbit.radius = Math.max(10.5, orbit.radius - 0.7);
break;
case "-":
orbit.radius = Math.min(23, orbit.radius + 0.7);
break;
default:
changed = false;
}
if (changed) {
event.preventDefault();
updateCamera();
}
});
window.addEventListener("resize", () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
});
// -------------------------------------------------------------------------
// Animation loop
// -------------------------------------------------------------------------
let previousTime = performance.now();
function animate(now) {
const delta = Math.min((now - previousTime) / 1000, 0.05);
previousTime = now;
if (!paused) {
simulationTime += delta;
}
for (const board of boardGroups) {
for (const token of board.tokens) {
const delay = token.group.userData.delay;
const progress = THREE.MathUtils.clamp(
(simulationTime - animationStart - delay) / 0.62,
0,
1
);
// Overshooting scale creates a playful voxel "pop".
const c1 = 1.70158;
const c3 = c1 + 1;
const eased =
1 +
c3 * Math.pow(progress - 1, 3) +
c1 * Math.pow(progress - 1, 2);
token.group.scale.setScalar(Math.max(0.001, eased));
token.group.rotation.x = Math.sin(progress * Math.PI) * Math.PI * 2;
token.group.rotation.y = Math.sin(progress * Math.PI) * 0.22;
}
}
bulbMaterial.emissiveIntensity =
1.65 + Math.sin(simulationTime * 3.2) * 0.35;
warmLightLeft.intensity =
22 + Math.sin(simulationTime * 2.1) * 4;
warmLightRight.intensity =
22 + Math.sin(simulationTime * 2.1 + 2.2) * 4;
for (const piece of confetti) {
piece.rotation.x += delta * 0.55;
piece.rotation.y += delta * 0.8;
piece.position.y =
piece.userData.baseY + Math.sin(simulationTime * 0.9 + piece.userData.phase) * 0.16;
}
renderer.render(scene, camera);
}
// Initial exhibit state.
generateHistories(true);
renderer.setAnimationLoop(animate);
document.getElementById("loading").classList.add("hidden");
</script>
</body>
</html>
```