Prompt specification
Build or customize the Three.js 3D scene "Coin-toss count and proportion towers" 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: Coin-toss count and proportion towers
Category: Algorithms & Systems · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/coin-toss-count-and-proportion-towers-x1793
Scene Description Prompt:
Using Three.js from cdnjs in standalone HTML, stack low-poly synthetic coin-toss counts beside a proportion marker to compare absolute imbalance and relative-frequency deviation.
Working Three.js Starter (coin-toss-count-and-proportion-towers-x1793):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Coin Lab — Counts and Proportions</title>
<style>
:root {
--ink: #24343a;
--muted: #6f7c7c;
--paper: rgba(250, 247, 239, 0.88);
--line: rgba(36, 52, 58, 0.13);
--gold: #dda63a;
--slate: #648592;
--teal: #278873;
--coral: #e86f55;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #e9e7de;
color: var(--ink);
font-family:
Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
canvas {
position: fixed;
inset: 0;
display: block;
touch-action: none;
outline: none;
}
button,
input {
font: inherit;
}
.panel {
position: fixed;
z-index: 10;
border: 1px solid var(--line);
border-radius: 20px;
background: var(--paper);
box-shadow: 0 18px 55px rgba(50, 57, 53, 0.11);
backdrop-filter: blur(16px);
-webkit-backdrop-filter: blur(16px);
}
.summary {
top: 24px;
left: 24px;
width: 350px;
padding: 23px;
}
.eyebrow {
display: flex;
align-items: center;
gap: 9px;
color: var(--teal);
font-size: 11px;
font-weight: 800;
letter-spacing: 0.17em;
text-transform: uppercase;
}
.eyebrow::before {
width: 8px;
height: 8px;
border-radius: 50%;
background: var(--teal);
content: "";
box-shadow: 0 0 0 5px rgba(39, 136, 115, 0.11);
}
h1 {
margin: 17px 0 9px;
font-size: clamp(26px, 3vw, 36px);
font-weight: 720;
letter-spacing: -0.045em;
line-height: 1.04;
}
.intro {
margin: 0;
color: var(--muted);
font-size: 13px;
line-height: 1.55;
}
.metrics {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 10px;
margin-top: 20px;
}
.metric {
min-height: 104px;
padding: 14px;
border: 1px solid var(--line);
border-radius: 14px;
background: rgba(255, 255, 255, 0.48);
}
.metric-label {
display: block;
margin-bottom: 7px;
color: var(--muted);
font-size: 10px;
font-weight: 750;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.metric-value {
display: block;
font-size: 27px;
font-weight: 750;
letter-spacing: -0.04em;
font-variant-numeric: tabular-nums;
}
.metric-meta {
display: block;
margin-top: 5px;
color: var(--muted);
font-size: 10px;
line-height: 1.35;
}
.controls {
bottom: 24px;
left: 24px;
width: 350px;
padding: 18px;
}
.control-title {
display: flex;
align-items: center;
justify-content: space-between;
margin-bottom: 12px;
font-size: 11px;
font-weight: 800;
letter-spacing: 0.12em;
text-transform: uppercase;
}
.synthetic-badge {
color: var(--muted);
font-size: 9px;
letter-spacing: 0.08em;
}
.scenarios {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 7px;
}
.scenario {
min-height: 38px;
padding: 7px 8px;
border: 1px solid var(--line);
border-radius: 10px;
background: rgba(255, 255, 255, 0.62);
color: var(--ink);
cursor: pointer;
font-size: 11px;
font-weight: 700;
transition:
background 150ms ease,
border-color 150ms ease,
transform 150ms ease;
}
.scenario:hover {
transform: translateY(-1px);
border-color: rgba(36, 52, 58, 0.28);
}
.scenario.active {
border-color: var(--teal);
background: var(--teal);
color: white;
}
.slider-row {
display: grid;
grid-template-columns: 1fr 48px;
align-items: center;
gap: 10px;
margin-top: 14px;
color: var(--muted);
font-size: 11px;
font-weight: 650;
}
.slider-row output {
color: var(--ink);
text-align: right;
font-variant-numeric: tabular-nums;
}
input[type="range"] {
grid-column: 1 / -1;
width: 100%;
height: 4px;
margin: 2px 0 5px;
border-radius: 10px;
appearance: none;
background: #ccd2cd;
cursor: pointer;
}
input[type="range"]::-webkit-slider-thumb {
width: 16px;
height: 16px;
border: 3px solid #f8f5ed;
border-radius: 50%;
appearance: none;
background: var(--teal);
box-shadow: 0 1px 5px rgba(25, 50, 44, 0.28);
}
input[type="range"]::-moz-range-thumb {
width: 11px;
height: 11px;
border: 3px solid #f8f5ed;
border-radius: 50%;
background: var(--teal);
box-shadow: 0 1px 5px rgba(25, 50, 44, 0.28);
}
.generate {
width: 100%;
min-height: 42px;
margin-top: 16px;
border: 0;
border-radius: 11px;
background: var(--ink);
color: white;
cursor: pointer;
font-size: 12px;
font-weight: 750;
transition:
background 150ms ease,
transform 150ms ease;
}
.generate:hover {
background: #14262c;
transform: translateY(-1px);
}
.legend {
display: flex;
gap: 16px;
margin-top: 13px;
color: var(--muted);
font-size: 10px;
}
.legend span {
display: inline-flex;
align-items: center;
gap: 6px;
}
.swatch {
width: 9px;
height: 9px;
border-radius: 2px;
}
.hint {
position: fixed;
right: 24px;
bottom: 24px;
z-index: 5;
padding: 10px 13px;
border: 1px solid var(--line);
border-radius: 999px;
background: rgba(250, 247, 239, 0.72);
color: var(--muted);
font-size: 10px;
font-weight: 650;
letter-spacing: 0.03em;
pointer-events: none;
backdrop-filter: blur(12px);
}
.world-label {
position: fixed;
z-index: 4;
pointer-events: none;
transform: translate(-50%, -50%);
will-change: left, top;
}
.stack-label {
width: 104px;
padding: 9px 10px;
border: 1px solid rgba(36, 52, 58, 0.14);
border-radius: 11px;
background: rgba(250, 247, 239, 0.91);
box-shadow: 0 8px 22px rgba(44, 50, 45, 0.12);
text-align: center;
backdrop-filter: blur(8px);
}
.stack-label span {
display: block;
margin-bottom: 3px;
font-size: 9px;
font-weight: 850;
letter-spacing: 0.12em;
}
.stack-label strong {
display: block;
font-size: 23px;
line-height: 1;
font-variant-numeric: tabular-nums;
}
.stack-label em {
display: block;
margin-top: 4px;
color: var(--muted);
font-size: 9px;
font-style: normal;
}
.stack-label.heads {
border-top: 4px solid var(--gold);
}
.stack-label.heads span {
color: #a66f13;
}
.stack-label.tails {
border-top: 4px solid var(--slate);
}
.stack-label.tails span {
color: #496f7d;
}
.axis-label {
color: #687371;
font-size: 9px;
font-weight: 750;
font-variant-numeric: tabular-nums;
text-shadow: 0 1px 2px #eeeae1;
}
.marker-label {
min-width: 62px;
padding: 7px 10px;
border-radius: 9px;
background: var(--ink);
color: white;
box-shadow: 0 7px 18px rgba(32, 43, 43, 0.18);
text-align: center;
font-size: 13px;
font-weight: 800;
font-variant-numeric: tabular-nums;
}
.target-label {
padding: 4px 7px;
border-radius: 6px;
background: rgba(39, 136, 115, 0.12);
color: var(--teal);
font-size: 9px;
font-weight: 800;
white-space: nowrap;
}
.gauge-title {
color: var(--ink);
font-size: 9px;
font-weight: 850;
letter-spacing: 0.11em;
white-space: nowrap;
}
@media (max-width: 760px) {
.summary {
top: 12px;
left: 12px;
width: calc(100% - 24px);
padding: 15px;
}
h1 {
margin: 11px 0 5px;
font-size: 23px;
}
.intro,
.legend {
display: none;
}
.metrics {
margin-top: 12px;
}
.metric {
min-height: 77px;
padding: 10px;
}
.metric-value {
font-size: 21px;
}
.controls {
right: 12px;
bottom: 12px;
left: 12px;
width: auto;
padding: 13px;
}
.control-title {
margin-bottom: 8px;
}
.slider-row {
margin-top: 9px;
}
.generate {
min-height: 37px;
margin-top: 11px;
}
.hint {
display: none;
}
}
</style>
</head>
<body>
<section class="panel summary" aria-label="Experiment summary">
<div class="eyebrow">Coin Lab / Synthetic 01</div>
<h1>Balance in counts.<br>Signal in proportion.</h1>
<p class="intro">
A synthetic Bernoulli sequence shown as result stacks and as a frequency
marker relative to the even-split expectation.
</p>
<div class="metrics">
<div class="metric">
<span class="metric-label">Absolute imbalance</span>
<strong class="metric-value" id="imbalanceValue">0</strong>
<span class="metric-meta" id="imbalanceMeta">0 heads / 0 tails</span>
</div>
<div class="metric">
<span class="metric-label">Frequency deviation</span>
<strong class="metric-value" id="deviationValue">0.0 pp</strong>
<span class="metric-meta" id="deviationMeta">from the 50% target</span>
</div>
</div>
</section>
<section class="panel controls" aria-label="Synthetic toss controls">
<div class="control-title">
<span>Generate a sequence</span>
<span class="synthetic-badge">No real trials</span>
</div>
<div class="scenarios" role="group" aria-label="Preset scenarios">
<button class="scenario active" data-scenario="balanced">Balanced</button>
<button class="scenario" data-scenario="heads">Heads ↑</button>
<button class="scenario" data-scenario="tails">Tails ↑</button>
</div>
<label class="slider-row">
<span>Number of tosses</span>
<output id="trialsOutput">60</output>
<input
id="trialsInput"
type="range"
min="24"
max="100"
step="1"
value="60"
aria-label="Number of synthetic tosses"
>
</label>
<label class="slider-row">
<span>Model head probability</span>
<output id="probabilityOutput">50%</output>
<input
id="probabilityInput"
type="range"
min="30"
max="70"
step="1"
value="50"
aria-label="Synthetic head probability"
>
</label>
<button class="generate" id="generateButton">
↻ Generate new synthetic tosses
</button>
<div class="legend">
<span><i class="swatch" style="background: var(--gold)"></i>Heads</span>
<span><i class="swatch" style="background: var(--slate)"></i>Tails</span>
<span><i class="swatch" style="background: var(--teal)"></i>50% target</span>
</div>
</section>
<div class="hint">Drag to orbit · Scroll to zoom · Double-click to reset</div>
<div class="world-label stack-label heads" id="headsLabel"></div>
<div class="world-label stack-label tails" id="tailsLabel"></div>
<div class="world-label marker-label" id="observedLabel">50%</div>
<div class="world-label target-label" id="targetLabel">TARGET 50%</div>
<div class="world-label gauge-title" id="gaugeTitle">HEAD FREQUENCY</div>
<div class="world-label axis-label" id="axis0">0</div>
<div class="world-label axis-label" id="axis25">25</div>
<div class="world-label axis-label" id="axis50">50</div>
<div class="world-label axis-label" id="axis75">75</div>
<div class="world-label axis-label" id="axis100">100</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 renderer = new THREE.WebGLRenderer({
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.08;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.domElement.setAttribute(
"aria-label",
"Interactive three-dimensional synthetic coin-toss comparison"
);
document.body.prepend(renderer.domElement);
const scene = new THREE.Scene();
scene.background = new THREE.Color("#e9e7de");
scene.fog = new THREE.Fog("#e9e7de", 17, 31);
const camera = new THREE.PerspectiveCamera(
42,
window.innerWidth / window.innerHeight,
0.1,
100
);
const cameraTarget = new THREE.Vector3(0.35, 4.75, 0);
const cameraState = {
radius: 15.8,
phi: 1.13,
theta: 0.54,
adjusted: false
};
function updateCamera() {
const offset = new THREE.Vector3().setFromSphericalCoords(
cameraState.radius,
cameraState.phi,
cameraState.theta
);
camera.position.copy(cameraTarget).add(offset);
camera.lookAt(cameraTarget);
}
updateCamera();
const hemisphere = new THREE.HemisphereLight(
"#fffaf0",
"#81918c",
2.0
);
scene.add(hemisphere);
const keyLight = new THREE.DirectionalLight("#fff4dc", 3.2);
keyLight.position.set(5, 11, 7);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(2048, 2048);
keyLight.shadow.camera.left = -9;
keyLight.shadow.camera.right = 9;
keyLight.shadow.camera.top = 13;
keyLight.shadow.camera.bottom = -4;
keyLight.shadow.camera.near = 1;
keyLight.shadow.camera.far = 30;
keyLight.shadow.bias = -0.0003;
scene.add(keyLight);
const rimLight = new THREE.DirectionalLight("#b8d7df", 1.1);
rimLight.position.set(-7, 6, -5);
scene.add(rimLight);
// -------------------------------------------------------------------------
// Ground and low-poly display elements
// -------------------------------------------------------------------------
const groundMaterial = new THREE.MeshStandardMaterial({
color: "#dedfd7",
roughness: 0.92,
metalness: 0.0
});
const ground = new THREE.Mesh(
new THREE.CircleGeometry(9.5, 64),
groundMaterial
);
ground.rotation.x = -Math.PI / 2;
ground.position.set(0.25, -0.015, 0);
ground.receiveShadow = true;
scene.add(ground);
const grid = new THREE.GridHelper(16, 32, "#aeb8b1", "#c6ccc4");
grid.position.set(0.25, 0.002, 0);
grid.material.transparent = true;
grid.material.opacity = 0.18;
scene.add(grid);
const plinthMaterial = new THREE.MeshStandardMaterial({
color: "#53666a",
roughness: 0.78,
metalness: 0.06,
flatShading: true
});
const headsX = -2.35;
const tailsX = 0.0;
const coinRadius = 0.56;
const coinHeight = 0.13;
const coinBase = 0.255;
function makePlinth(x) {
const plinth = new THREE.Mesh(
new THREE.CylinderGeometry(0.82, 0.94, 0.24, 8),
plinthMaterial
);
plinth.position.set(x, 0.12, 0.05);
plinth.castShadow = true;
plinth.receiveShadow = true;
scene.add(plinth);
const inset = new THREE.Mesh(
new THREE.CylinderGeometry(0.67, 0.67, 0.035, 8),
new THREE.MeshStandardMaterial({
color: "#738487",
roughness: 0.7,
flatShading: true
})
);
inset.position.set(x, 0.255, 0.05);
inset.receiveShadow = true;
scene.add(inset);
}
makePlinth(headsX);
makePlinth(tailsX);
// Shared low-poly coin geometry and materials.
const coinGeometry = new THREE.CylinderGeometry(
coinRadius,
coinRadius * 1.035,
coinHeight,
8,
1,
false
);
const rimGeometry = new THREE.TorusGeometry(0.49, 0.023, 4, 8);
const headsMaterials = [
new THREE.MeshStandardMaterial({
color: "#e2a936",
roughness: 0.36,
metalness: 0.24,
flatShading: true
}),
new THREE.MeshStandardMaterial({
color: "#d99a2c",
roughness: 0.4,
metalness: 0.22,
flatShading: true
}),
new THREE.MeshStandardMaterial({
color: "#efb84d",
roughness: 0.34,
metalness: 0.26,
flatShading: true
})
];
const tailsMaterials = [
new THREE.MeshStandardMaterial({
color: "#648792",
roughness: 0.43,
metalness: 0.15,
flatShading: true
}),
new THREE.MeshStandardMaterial({
color: "#577985",
roughness: 0.46,
metalness: 0.13,
flatShading: true
}),
new THREE.MeshStandardMaterial({
color: "#7896a0",
roughness: 0.4,
metalness: 0.17,
flatShading: true
})
];
const glyphMaterial = new THREE.MeshStandardMaterial({
color: "#fff5d6",
roughness: 0.5,
metalness: 0.1,
flatShading: true
});
const stackGroup = new THREE.Group();
scene.add(stackGroup);
const animatedTokens = [];
function makeGlyph(letter) {
const group = new THREE.Group();
const barHeight = 0.035;
function addBar(width, depth, x, z) {
const bar = new THREE.Mesh(
new THREE.BoxGeometry(width, barHeight, depth),
glyphMaterial
);
bar.position.set(x, 0.096, z);
bar.castShadow = true;
group.add(bar);
}
if (letter === "H") {
addBar(0.045, 0.29, -0.13, 0);
addBar(0.045, 0.29, 0.13, 0);
addBar(0.30, 0.045, 0, 0);
} else {
addBar(0.32, 0.045, 0, -0.12);
addBar(0.045, 0.29, 0, 0.015);
}
return group;
}
function buildStack(count, x, isHeads, animationStart) {
const materials = isHeads ? headsMaterials : tailsMaterials;
for (let i = 0; i < count; i++) {
const token = new THREE.Group();
const coin = new THREE.Mesh(
coinGeometry,
materials[Math.floor(Math.random() * materials.length)]
);
coin.castShadow = true;
coin.receiveShadow = true;
token.add(coin);
const rim = new THREE.Mesh(rimGeometry, materials[1]);
rim.rotation.x = Math.PI / 2;
rim.position.y = coinHeight * 0.5 + 0.002;
rim.castShadow = true;
token.add(rim);
if (i === count - 1) {
token.add(makeGlyph(isHeads ? "H" : "T"));
}
const targetY = coinBase + coinHeight * 0.5 + i * coinHeight;
token.position.set(
x + (Math.random() - 0.5) * 0.025,
coinBase,
0.05 + (Math.random() - 0.5) * 0.025
);
token.rotation.y =
Math.floor(Math.random() * 8) * (Math.PI / 4) +
(Math.random() - 0.5) * 0.08;
token.userData = {
targetY,
delay: animationStart + i * 13 + (isHeads ? 0 : 45)
};
stackGroup.add(token);
animatedTokens.push(token);
}
return coinBase + count * coinHeight;
}
// -------------------------------------------------------------------------
// Proportion marker
// -------------------------------------------------------------------------
const gaugeX = 3.25;
const gaugeBase = 0.46;
const gaugeHeight = 8.65;
const gaugeBoard = new THREE.Mesh(
new THREE.BoxGeometry(1.34, gaugeHeight + 0.38, 0.18),
new THREE.MeshStandardMaterial({
color: "#f7f3e9",
roughness: 0.8,
metalness: 0.02
})
);
gaugeBoard.position.set(gaugeX, gaugeBase + gaugeHeight / 2, -0.18);
gaugeBoard.castShadow = true;
gaugeBoard.receiveShadow = true;
scene.add(gaugeBoard);
const gaugeBasePlinth = new THREE.Mesh(
new THREE.BoxGeometry(1.8, 0.22, 0.8),
plinthMaterial
);
gaugeBasePlinth.position.set(gaugeX, 0.11, -0.1);
gaugeBasePlinth.castShadow = true;
gaugeBasePlinth.receiveShadow = true;
scene.add(gaugeBasePlinth);
const tickMaterial = new THREE.MeshStandardMaterial({
color: "#9aa6a1",
roughness: 0.8,
flatShading: true
});
const majorTickMaterial = new THREE.MeshStandardMaterial({
color: "#667873",
roughness: 0.75,
flatShading: true
});
for (let i = 0; i <= 10; i++) {
const y = gaugeBase + (i / 10) * gaugeHeight;
const major = i % 2 === 0;
const tick = new THREE.Mesh(
new THREE.BoxGeometry(major ? 0.34 : 0.18, 0.028, 0.035),
major ? majorTickMaterial : tickMaterial
);
tick.position.set(gaugeX + 0.42, y, -0.065);
scene.add(tick);
}
const targetMarker = new THREE.Mesh(
new THREE.BoxGeometry(1.43, 0.045, 0.075),
new THREE.MeshStandardMaterial({
color: "#278873",
roughness: 0.6,
flatShading: true
})
);
targetMarker.position.set(gaugeX, gaugeBase + gaugeHeight / 2, 0.025);
scene.add(targetMarker);
const markerMaterial = new THREE.MeshStandardMaterial({
color: "#278873",
roughness: 0.5,
metalness: 0.08,
flatShading: true
});
const markerGroup = new THREE.Group();
const observedBar = new THREE.Mesh(
new THREE.BoxGeometry(1.43, 0.095, 0.1),
markerMaterial
);
observedBar.castShadow = true;
markerGroup.add(observedBar);
const observedPointer = new THREE.Mesh(
new THREE.ConeGeometry(0.15, 0.34, 3),
markerMaterial
);
observedPointer.rotation.z = -Math.PI / 2;
observedPointer.position.x = 0.86;
observedPointer.castShadow = true;
markerGroup.add(observedPointer);
markerGroup.position.set(gaugeX, gaugeBase + gaugeHeight * 0.5, 0.08);
scene.add(markerGroup);
const deviationConnector = new THREE.Mesh(
new THREE.CylinderGeometry(0.035, 0.035, 1, 6),
markerMaterial
);
deviationConnector.position.set(gaugeX - 0.75, gaugeBase + gaugeHeight / 2, 0.03);
scene.add(deviationConnector);
// -------------------------------------------------------------------------
// DOM labels and state
// -------------------------------------------------------------------------
const ui = {
imbalanceValue: document.getElementById("imbalanceValue"),
imbalanceMeta: document.getElementById("imbalanceMeta"),
deviationValue: document.getElementById("deviationValue"),
deviationMeta: document.getElementById("deviationMeta"),
trialsInput: document.getElementById("trialsInput"),
trialsOutput: document.getElementById("trialsOutput"),
probabilityInput: document.getElementById("probabilityInput"),
probabilityOutput: document.getElementById("probabilityOutput"),
generateButton: document.getElementById("generateButton"),
headsLabel: document.getElementById("headsLabel"),
tailsLabel: document.getElementById("tailsLabel"),
observedLabel: document.getElementById("observedLabel"),
targetLabel: document.getElementById("targetLabel"),
gaugeTitle: document.getElementById("gaugeTitle"),
axis0: document.getElementById("axis0"),
axis25: document.getElementById("axis25"),
axis50: document.getElementById("axis50"),
axis75: document.getElementById("axis75"),
axis100: document.getElementById("axis100")
};
const scenarioButtons = [
...document.querySelectorAll("[data-scenario]")
];
const scenarios = {
balanced: { probability: 0.5, trials: 60 },
heads: { probability: 0.67, trials: 80 },
tails: { probability: 0.33, trials: 80 }
};
const state = {
trials: 60,
probability: 0.5,
heads: 30,
tails: 30
};
const labelPositions = {
heads: new THREE.Vector3(),
tails: new THREE.Vector3(),
observed: new THREE.Vector3(),
target: new THREE.Vector3(gaugeX - 0.82, gaugeBase + gaugeHeight / 2 - 0.34, 0.1),
gaugeTitle: new THREE.Vector3(gaugeX, gaugeBase + gaugeHeight + 0.43, 0),
axis0: new THREE.Vector3(gaugeX + 1.02, gaugeBase, 0),
axis25: new THREE.Vector3(
gaugeX + 1.02,
gaugeBase + gaugeHeight * 0.25,
0
),
axis50: new THREE.Vector3(
gaugeX + 1.02,
gaugeBase + gaugeHeight * 0.5,
0
),
axis75: new THREE.Vector3(
gaugeX + 1.02,
gaugeBase + gaugeHeight * 0.75,
0
),
axis100: new THREE.Vector3(
gaugeX + 1.02,
gaugeBase + gaugeHeight,
0
)
};
let markerCurrent = 0.5;
let markerFrom = 0.5;
let markerTarget = 0.5;
let markerAnimationStart = 0;
function setActiveScenario(name) {
scenarioButtons.forEach((button) => {
button.classList.toggle(
"active",
button.dataset.scenario === name
);
});
}
function deviationColor(deviation) {
const magnitude = Math.abs(deviation);
const green = new THREE.Color("#278873");
const gold = new THREE.Color("#dda63a");
const coral = new THREE.Color("#e86f55");
if (magnitude <= 0.05) {
return green;
}
if (magnitude <= 0.12) {
return green.lerp(gold, (magnitude - 0.05) / 0.07);
}
return gold.lerp(
coral,
Math.min(1, (magnitude - 0.12) / 0.08)
);
}
function updateMarker(proportion) {
const y = gaugeBase + proportion * gaugeHeight;
markerGroup.position.y = y;
const deviation = proportion - 0.5;
const color = deviationColor(deviation);
markerMaterial.color.copy(color);
deviationConnector.material.color.copy(color);
const delta = deviation * gaugeHeight;
deviationConnector.visible = Math.abs(delta) > 0.025;
deviationConnector.scale.y = Math.max(0.001, Math.abs(delta));
deviationConnector.position.y =
gaugeBase + gaugeHeight / 2 + delta / 2;
labelPositions.observed.set(gaugeX, y + 0.42, 0.15);
ui.observedLabel.textContent = `${Math.round(proportion * 100)}%`;
ui.observedLabel.style.background = `#${color.getHexString()}`;
}
function generateSequence() {
state.trials = Number(ui.trialsInput.value);
state.probability = Number(ui.probabilityInput.value) / 100;
ui.trialsOutput.value = state.trials;
ui.probabilityOutput.value = `${Math.round(state.probability * 100)}%`;
let heads = 0;
for (let i = 0; i < state.trials; i++) {
if (Math.random() < state.probability) {
heads++;
}
}
const tails = state.trials - heads;
const headProportion = heads / state.trials;
const deviation = headProportion - 0.5;
state.heads = heads;
state.tails = tails;
ui.imbalanceValue.textContent = Math.abs(heads - tails);
ui.imbalanceMeta.textContent = `${heads} heads / ${tails} tails`;
const deviationPoints = deviation * 100;
ui.deviationValue.textContent =
`${deviationPoints > 0 ? "+" : ""}${deviationPoints.toFixed(1)} pp`;
ui.deviationValue.style.color = `#${deviationColor(deviation).getHexString()}`;
ui.deviationMeta.textContent =
`${Math.abs(deviationPoints).toFixed(1)} points from the 50% target`;
ui.headsLabel.innerHTML = `
<span>HEADS</span>
<strong>${heads}</strong>
<em>${(headProportion * 100).toFixed(1)}% of tosses</em>
`;
ui.tailsLabel.innerHTML = `
<span>TAILS</span>
<strong>${tails}</strong>
<em>${((1 - headProportion) * 100).toFixed(1)}% of tosses</em>
`;
stackGroup.clear();
animatedTokens.length = 0;
const now = performance.now();
const headsTop = buildStack(
heads,
headsX,
true,
now
);
const tailsTop = buildStack(
tails,
tailsX,
false,
now
);
labelPositions.heads.set(headsX, headsTop + 0.62, 0.05);
labelPositions.tails.set(tailsX, tailsTop + 0.62, 0.05);
markerFrom = markerCurrent;
markerTarget = headProportion;
markerAnimationStart = now;
}
// -------------------------------------------------------------------------
// Direct camera controls: drag to orbit, wheel to zoom
// -------------------------------------------------------------------------
let dragging = false;
let previousX = 0;
let previousY = 0;
renderer.domElement.addEventListener("pointerdown", (event) => {
dragging = true;
previousX = event.clientX;
previousY = event.clientY;
renderer.domElement.setPointerCapture(event.pointerId);
renderer.domElement.style.cursor = "grabbing";
});
renderer.domElement.addEventListener("pointermove", (event) => {
if (!dragging) return;
const dx = event.clientX - previousX;
const dy = event.clientY - previousY;
previousX = event.clientX;
previousY = event.clientY;
cameraState.theta -= dx * 0.005;
cameraState.phi = THREE.MathUtils.clamp(
cameraState.phi - dy * 0.0045,
0.58,
1.42
);
cameraState.adjusted = true;
updateCamera();
});
function endDrag(event) {
dragging = false;
renderer.domElement.style.cursor = "grab";
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.style.cursor = "grab";
renderer.domElement.addEventListener(
"wheel",
(event) => {
event.preventDefault();
cameraState.radius = THREE.MathUtils.clamp(
cameraState.radius + event.deltaY * 0.01,
10,
27
);
cameraState.adjusted = true;
updateCamera();
},
{ passive: false }
);
renderer.domElement.addEventListener("dblclick", () => {
cameraState.radius = window.innerWidth / window.innerHeight < 0.75
? 22
: 15.8;
cameraState.phi = 1.13;
cameraState.theta = 0.54;
cameraState.adjusted = false;
updateCamera();
});
// -------------------------------------------------------------------------
// UI events
// -------------------------------------------------------------------------
scenarioButtons.forEach((button) => {
button.addEventListener("click", () => {
const scenario = scenarios[button.dataset.scenario];
state.trials = scenario.trials;
state.probability = scenario.probability;
ui.trialsInput.value = scenario.trials;
ui.probabilityInput.value = Math.round(scenario.probability * 100);
setActiveScenario(button.dataset.scenario);
generateSequence();
});
});
ui.trialsInput.addEventListener("input", generateSequence);
ui.probabilityInput.addEventListener("input", () => {
setActiveScenario(null);
generateSequence();
});
ui.generateButton.addEventListener("click", generateSequence);
// -------------------------------------------------------------------------
// Label projection and animation
// -------------------------------------------------------------------------
const projected = new THREE.Vector3();
function positionLabel(element, worldPosition) {
projected.copy(worldPosition).project(camera);
const visible =
projected.z > -1 &&
projected.z < 1 &&
projected.x > -1.15 &&
projected.x < 1.15 &&
projected.y > -1.15 &&
projected.y < 1.15;
element.style.display = visible ? "block" : "none";
if (!visible) return;
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`;
}
function updateLabels() {
positionLabel(ui.headsLabel, labelPositions.heads);
positionLabel(ui.tailsLabel, labelPositions.tails);
positionLabel(ui.observedLabel, labelPositions.observed);
positionLabel(ui.targetLabel, labelPositions.target);
positionLabel(ui.gaugeTitle, labelPositions.gaugeTitle);
positionLabel(ui.axis0, labelPositions.axis0);
positionLabel(ui.axis25, labelPositions.axis25);
positionLabel(ui.axis50, labelPositions.axis50);
positionLabel(ui.axis75, labelPositions.axis75);
positionLabel(ui.axis100, labelPositions.axis100);
}
function animate(now) {
requestAnimationFrame(animate);
for (const token of animatedTokens) {
const elapsed = now - token.userData.delay;
if (elapsed <= 0) {
token.position.y = coinBase;
continue;
}
const t = Math.min(1, elapsed / 430);
const eased = 1 - Math.pow(1 - t, 3);
token.position.y = THREE.MathUtils.lerp(
coinBase,
token.userData.targetY,
eased
);
}
const markerElapsed = now - markerAnimationStart;
if (markerElapsed < 650) {
const t = Math.max(0, markerElapsed / 650);
const eased = 1 - Math.pow(1 - t, 3);
markerCurrent = THREE.MathUtils.lerp(
markerFrom,
markerTarget,
eased
);
updateMarker(markerCurrent);
}
updateLabels();
renderer.render(scene, camera);
}
function resize() {
const width = window.innerWidth;
const height = window.innerHeight;
const aspect = width / height;
renderer.setSize(width, height);
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
camera.aspect = aspect;
camera.updateProjectionMatrix();
if (!cameraState.adjusted) {
cameraState.radius = aspect < 0.75
? 22
: aspect < 1.2
? 18.5
: 15.8;
updateCamera();
}
}
window.addEventListener("resize", resize);
resize();
generateSequence();
requestAnimationFrame(animate);
</script>
</body>
</html>
```