Prompt specification
Build or customize the Three.js 3D scene "One opportunity and many opportunities comparison" inside the active workspace.
Engineering Constraints:
1. Runtime & Orbit: Use Three.js r160 with OrbitControls (damping enabled) and ACESFilmicToneMapping.
2. Frame Timing: Drive all motion strictly with clock.getDelta() so animation speed is FPS-independent.
3. Viewport Hygiene: Handle window resize cleanly, cap DPR at Math.min(window.devicePixelRatio, 2), and dispose geometries/materials/renderer on unmount.
Scene Metadata:
Scene: One opportunity and many opportunities comparison
Category: Algorithms & Systems · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/one-opportunity-and-many-opportunities-comparison-x1822
Scene Description Prompt:
I'd like to see this as a stylized Three.js scene: one opportunity and many opportunities comparison. Compare independent trial batches of different sizes under the same rare-event probability; compute at-least-one probabilities and avoid the gambler's fallacy after long gaps. Use cdnjs and return one self-contained HTML file.
Working Three.js Starter (one-opportunity-and-many-opportunities-comparison-x1822):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Rare Event, Many Chances</title>
<style>
:root {
--bg: #07101d;
--panel: #0b1526;
--panel-2: #101d31;
--line: rgba(155, 190, 225, 0.13);
--text: #edf6ff;
--muted: #8fa8bf;
--cyan: #59ddff;
--blue: #3d9dff;
--coral: #ff627e;
--green: #68e0ae;
}
* {
box-sizing: border-box;
}
html, body {
margin: 0;
min-height: 100%;
background: var(--bg);
color: var(--text);
font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
body {
overflow: hidden;
}
button,
input {
font: inherit;
}
button {
color: inherit;
}
.topbar {
height: 72px;
display: flex;
align-items: center;
justify-content: space-between;
gap: 24px;
padding: 0 24px;
border-bottom: 1px solid var(--line);
background: rgba(7, 16, 29, 0.96);
position: relative;
z-index: 5;
}
.brand {
display: flex;
align-items: center;
gap: 13px;
min-width: 0;
}
.brand-mark {
width: 42px;
height: 42px;
flex: 0 0 auto;
display: grid;
place-items: center;
border-radius: 13px;
color: #07101d;
font-size: 23px;
font-weight: 900;
background: linear-gradient(135deg, var(--cyan), #7c8cff 60%, var(--coral));
box-shadow: 0 8px 26px rgba(89, 221, 255, 0.18);
}
.brand h1 {
margin: 0;
font-size: 18px;
line-height: 1.2;
letter-spacing: -0.02em;
}
.brand p {
margin: 4px 0 0;
color: var(--muted);
font-size: 12px;
}
.top-pills {
display: flex;
align-items: center;
gap: 8px;
flex-wrap: wrap;
justify-content: flex-end;
}
.pill {
display: inline-flex;
align-items: center;
gap: 7px;
min-height: 29px;
padding: 6px 10px;
border: 1px solid var(--line);
border-radius: 999px;
color: #b9cce0;
background: rgba(255, 255, 255, 0.025);
font-size: 11px;
white-space: nowrap;
}
.pill-dot {
width: 7px;
height: 7px;
border-radius: 50%;
background: var(--cyan);
box-shadow: 0 0 12px var(--cyan);
}
.pill.no-memory .pill-dot {
background: var(--coral);
box-shadow: 0 0 12px var(--coral);
}
.layout {
height: calc(100vh - 72px);
display: grid;
grid-template-columns: minmax(0, 1fr) 390px;
}
.scene-panel {
position: relative;
min-width: 0;
min-height: 0;
overflow: hidden;
background:
radial-gradient(circle at 35% 28%, rgba(55, 111, 180, 0.19), transparent 36%),
radial-gradient(circle at 78% 58%, rgba(255, 98, 126, 0.08), transparent 28%),
linear-gradient(160deg, #081322, #050b14 72%);
}
#canvas-host {
position: absolute;
inset: 0;
}
#canvas-host canvas {
width: 100%;
height: 100%;
display: block;
cursor: grab;
touch-action: none;
}
#canvas-host canvas.grabbing {
cursor: grabbing;
}
.scene-title {
position: absolute;
z-index: 2;
top: 24px;
left: 26px;
pointer-events: none;
}
.eyebrow {
margin: 0 0 7px;
color: var(--cyan);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.16em;
text-transform: uppercase;
}
.scene-title h2 {
margin: 0;
font-size: clamp(22px, 3vw, 37px);
line-height: 1.05;
letter-spacing: -0.04em;
}
.scene-title p {
margin: 9px 0 0;
color: var(--muted);
font-size: 13px;
}
.scene-tools {
position: absolute;
z-index: 3;
top: 22px;
right: 22px;
display: flex;
gap: 8px;
}
.icon-button {
width: 38px;
height: 38px;
display: grid;
place-items: center;
border: 1px solid var(--line);
border-radius: 11px;
color: #c9d9e8;
background: rgba(9, 20, 36, 0.76);
backdrop-filter: blur(12px);
cursor: pointer;
transition: border-color 160ms ease, background 160ms ease, transform 160ms ease;
}
.icon-button:hover {
border-color: rgba(89, 221, 255, 0.55);
background: rgba(20, 40, 63, 0.9);
transform: translateY(-1px);
}
.scene-bottom {
position: absolute;
z-index: 2;
left: 24px;
right: 24px;
bottom: 21px;
display: flex;
justify-content: space-between;
align-items: flex-end;
gap: 18px;
pointer-events: none;
}
.legend,
.camera-hint {
border: 1px solid var(--line);
background: rgba(7, 16, 29, 0.72);
backdrop-filter: blur(12px);
border-radius: 12px;
color: var(--muted);
font-size: 11px;
padding: 10px 12px;
}
.legend {
display: flex;
align-items: center;
gap: 15px;
flex-wrap: wrap;
}
.legend-item {
display: inline-flex;
align-items: center;
gap: 7px;
}
.legend-dot {
width: 9px;
height: 9px;
border-radius: 50%;
}
.legend-dot.miss {
background: var(--blue);
box-shadow: 0 0 10px rgba(61, 157, 255, 0.65);
}
.legend-dot.hit {
background: var(--coral);
box-shadow: 0 0 10px rgba(255, 98, 126, 0.7);
}
.legend-note {
color: #d9e7f4;
}
.sidebar {
min-height: 0;
overflow-y: auto;
border-left: 1px solid var(--line);
background:
linear-gradient(180deg, rgba(13, 25, 43, 0.98), rgba(8, 17, 31, 0.98));
scrollbar-width: thin;
scrollbar-color: #243c58 transparent;
}
.sidebar section {
padding: 21px 21px 22px;
border-bottom: 1px solid var(--line);
}
.sidebar h2,
.sidebar h3 {
margin: 0;
letter-spacing: -0.025em;
}
.sidebar h2 {
font-size: 23px;
line-height: 1.12;
}
.sidebar h3 {
font-size: 13px;
margin-bottom: 14px;
}
.copy {
color: var(--muted);
font-size: 12px;
line-height: 1.65;
margin: 10px 0 0;
}
.formula {
margin-top: 16px;
padding: 13px 14px;
border-radius: 12px;
border: 1px solid rgba(89, 221, 255, 0.16);
background: rgba(89, 221, 255, 0.055);
color: #dceeff;
font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
font-size: 12px;
line-height: 1.6;
}
.formula strong {
color: var(--cyan);
}
.control-head {
display: flex;
align-items: center;
justify-content: space-between;
gap: 12px;
margin-bottom: 10px;
}
.control-head label {
color: #c6d7e7;
font-size: 12px;
font-weight: 650;
}
.control-value {
color: var(--cyan);
font-size: 13px;
font-weight: 800;
font-variant-numeric: tabular-nums;
}
input[type="range"] {
width: 100%;
height: 5px;
margin: 8px 0 4px;
border-radius: 99px;
outline: none;
appearance: none;
background: linear-gradient(
90deg,
var(--cyan) 0%,
var(--cyan) var(--range-fill, 5.5%),
#20344a var(--range-fill, 5.5%),
#20344a 100%
);
cursor: pointer;
}
input[type="range"]::-webkit-slider-thumb {
appearance: none;
width: 18px;
height: 18px;
border-radius: 50%;
border: 3px solid #07101d;
background: var(--cyan);
box-shadow: 0 0 0 1px var(--cyan), 0 0 16px rgba(89, 221, 255, 0.5);
}
input[type="range"]::-moz-range-thumb {
width: 14px;
height: 14px;
border-radius: 50%;
border: 3px solid #07101d;
background: var(--cyan);
box-shadow: 0 0 0 1px var(--cyan), 0 0 16px rgba(89, 221, 255, 0.5);
}
.range-labels {
display: flex;
justify-content: space-between;
color: #6f879d;
font-size: 10px;
margin-top: 6px;
}
.control-block {
margin-top: 19px;
}
.size-grid {
display: grid;
grid-template-columns: repeat(6, 1fr);
gap: 6px;
}
.size-button {
min-height: 34px;
border: 1px solid var(--line);
border-radius: 9px;
background: rgba(255, 255, 255, 0.025);
color: #9fb5ca;
font-size: 11px;
font-weight: 750;
cursor: pointer;
transition: 160ms ease;
}
.size-button:hover {
color: white;
border-color: rgba(89, 221, 255, 0.36);
}
.size-button.active {
color: #07101d;
border-color: var(--cyan);
background: var(--cyan);
box-shadow: 0 7px 22px rgba(89, 221, 255, 0.14);
}
.run-button {
width: 100%;
min-height: 44px;
margin-top: 17px;
border: 0;
border-radius: 12px;
color: #06111c;
background: linear-gradient(100deg, var(--cyan), #7e9cff);
font-size: 12px;
font-weight: 850;
cursor: pointer;
box-shadow: 0 10px 30px rgba(89, 221, 255, 0.14);
transition: transform 160ms ease, filter 160ms ease;
}
.run-button:hover {
transform: translateY(-1px);
filter: brightness(1.08);
}
.run-button:active {
transform: translateY(0);
}
.comparison-grid {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 9px;
}
.metric-card {
min-width: 0;
padding: 15px 13px 13px;
border: 1px solid var(--line);
border-radius: 14px;
background: rgba(255, 255, 255, 0.025);
}
.metric-card.many {
border-color: rgba(255, 98, 126, 0.2);
background: linear-gradient(145deg, rgba(255, 98, 126, 0.07), rgba(255, 255, 255, 0.015));
}
.metric-label {
color: var(--muted);
font-size: 9px;
line-height: 1.35;
font-weight: 800;
letter-spacing: 0.08em;
text-transform: uppercase;
min-height: 24px;
}
.metric-value {
margin-top: 7px;
color: var(--cyan);
font-size: 26px;
line-height: 1;
letter-spacing: -0.04em;
font-weight: 850;
font-variant-numeric: tabular-nums;
}
.metric-card.many .metric-value {
color: var(--coral);
}
.metric-detail {
margin-top: 7px;
color: #7991a8;
font-size: 10px;
line-height: 1.35;
}
.comparison-ratio {
margin-top: 10px;
color: #b8cada;
font-size: 11px;
line-height: 1.5;
}
.comparison-ratio strong {
color: white;
}
.sample-grid {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 8px;
}
.sample-stat {
padding: 11px 12px;
border-radius: 11px;
border: 1px solid var(--line);
background: rgba(255, 255, 255, 0.02);
}
.sample-stat span {
display: block;
color: #748da4;
font-size: 9px;
font-weight: 750;
letter-spacing: 0.07em;
text-transform: uppercase;
}
.sample-stat strong {
display: block;
margin-top: 5px;
color: #e7f2fc;
font-size: 12px;
line-height: 1.35;
}
.trial-strip {
display: flex;
gap: 4px;
flex-wrap: wrap;
margin-top: 12px;
min-height: 12px;
}
.trial-cell {
width: 10px;
height: 10px;
border-radius: 3px;
background: var(--blue);
box-shadow: 0 0 8px rgba(61, 157, 255, 0.22);
}
.trial-cell.hit {
background: var(--coral);
box-shadow: 0 0 8px rgba(255, 98, 126, 0.42);
}
.strip-caption {
margin-top: 7px;
color: #687f95;
font-size: 9px;
}
.gap-box {
margin-top: 12px;
padding: 13px;
border-left: 3px solid var(--coral);
border-radius: 4px 11px 11px 4px;
background: linear-gradient(90deg, rgba(255, 98, 126, 0.08), rgba(255, 98, 126, 0.015));
}
.gap-box strong {
color: #ffd8df;
font-size: 12px;
}
.gap-box p {
margin: 6px 0 0;
color: #a88e99;
font-size: 10px;
line-height: 1.55;
}
.gap-facts {
display: flex;
justify-content: space-between;
gap: 12px;
margin-top: 10px;
color: #7890a6;
font-size: 10px;
}
.gap-facts strong {
color: #c8d8e7;
}
.table-head,
.table-row {
display: grid;
grid-template-columns: 38px minmax(0, 1fr) 65px;
align-items: center;
gap: 8px;
}
.table-head {
padding: 0 7px 8px;
color: #6f879d;
font-size: 9px;
font-weight: 750;
letter-spacing: 0.05em;
text-transform: uppercase;
}
.table-head span:last-child {
text-align: right;
}
.table-row {
min-height: 42px;
padding: 6px 7px;
border-radius: 9px;
color: #9db2c6;
font-size: 10px;
font-variant-numeric: tabular-nums;
}
.table-row.selected {
background: rgba(89, 221, 255, 0.075);
color: #e6f4ff;
}
.n-value {
color: #dceaf7;
font-weight: 750;
}
.probability-cell {
min-width: 0;
}
.probability-top {
display: flex;
justify-content: space-between;
gap: 8px;
margin-bottom: 5px;
}
.probability-top strong {
color: #e8f4ff;
}
.probability-bar {
height: 4px;
overflow: hidden;
border-radius: 99px;
background: #182a3e;
}
.probability-bar i {
display: block;
height: 100%;
min-width: 2px;
border-radius: inherit;
background: linear-gradient(90deg, var(--blue), var(--cyan));
}
.expected-value {
text-align: right;
color: #849cb2;
}
.method-note {
padding: 18px 21px 24px;
color: #6e879e;
font-size: 10px;
line-height: 1.65;
}
.method-note strong {
color: #a8bed1;
}
@media (max-width: 1050px) {
.layout {
grid-template-columns: minmax(0, 1fr) 350px;
}
.scene-title p {
max-width: 430px;
}
}
@media (max-width: 760px) {
body {
overflow: auto;
}
.topbar {
height: 66px;
padding: 0 15px;
}
.brand h1 {
font-size: 15px;
}
.brand p,
.top-pills .pill:not(.no-memory) {
display: none;
}
.layout {
height: auto;
display: block;
}
.scene-panel {
height: 61vh;
min-height: 480px;
}
.sidebar {
overflow: visible;
border-left: 0;
border-top: 1px solid var(--line);
}
.scene-title {
top: 18px;
left: 18px;
}
.scene-title h2 {
font-size: 25px;
}
.scene-title p {
font-size: 11px;
}
.scene-tools {
top: 17px;
right: 17px;
}
.scene-bottom {
left: 14px;
right: 14px;
bottom: 14px;
}
.camera-hint {
display: none;
}
.legend {
width: 100%;
justify-content: center;
}
}
</style>
</head>
<body>
<header class="topbar">
<div class="brand">
<div class="brand-mark" aria-hidden="true">P</div>
<div>
<h1>Rare Event, Many Chances</h1>
<p>Independent Bernoulli trials, visualized in exact probability space</p>
</div>
</div>
<div class="top-pills">
<span class="pill"><span class="pill-dot"></span>Same event probability</span>
<span class="pill no-memory"><span class="pill-dot"></span>No streak memory</span>
</div>
</header>
<main class="layout">
<section class="scene-panel" aria-label="Interactive three-dimensional probability comparison">
<div id="canvas-host"></div>
<div class="scene-title">
<p class="eyebrow">Independent trial laboratory</p>
<h2>One chance.<br />Or many chances.</h2>
<p>Every orb is a fresh trial with the same rare-event probability.</p>
</div>
<div class="scene-tools">
<button class="icon-button" id="reset-camera" type="button" title="Reset camera view" aria-label="Reset camera view">
↺
</button>
</div>
<div class="scene-bottom">
<div class="legend">
<span class="legend-item"><i class="legend-dot miss"></i>No event</span>
<span class="legend-item"><i class="legend-dot hit"></i>Event</span>
<span class="legend-note">Orb size is illustrative; probability is identical for every trial.</span>
</div>
<div class="camera-hint">Drag to orbit · Scroll to zoom</div>
</div>
</section>
<aside class="sidebar">
<section>
<p class="eyebrow">The exact rule</p>
<h2>More independent chances raise the chance of at least one event.</h2>
<p class="copy">
A miss does not make the next trial more “due.” Each opportunity is a fresh
independent draw from the same probability.
</p>
<div class="formula">
<strong>P(at least one in n trials)</strong><br />
= 1 − (1 − p)<sup>n</sup>
</div>
</section>
<section>
<div class="control-head">
<label for="probability">Event probability per trial</label>
<span class="control-value" id="probability-value">1.0%</span>
</div>
<input id="probability" type="range" min="0.5" max="10" step="0.1" value="1" />
<div class="range-labels">
<span>0.5%</span>
<span>10%</span>
</div>
<div class="control-block">
<div class="control-head">
<label>Many-opportunity batch</label>
<span class="control-value" id="batch-value">50 trials</span>
</div>
<div class="size-grid" role="group" aria-label="Number of opportunities">
<button class="size-button" type="button" data-n="5">5</button>
<button class="size-button" type="button" data-n="10">10</button>
<button class="size-button" type="button" data-n="25">25</button>
<button class="size-button active" type="button" data-n="50">50</button>
<button class="size-button" type="button" data-n="100">100</button>
<button class="size-button" type="button" data-n="250">250</button>
</div>
</div>
<button class="run-button" id="run-trials" type="button">Run new independent trials</button>
<p class="copy">The animated result is one random sample. The percentages below are exact.</p>
</section>
<section>
<h3>At least one event</h3>
<div class="comparison-grid">
<div class="metric-card">
<div class="metric-label">One opportunity</div>
<div class="metric-value" id="single-probability">1.0%</div>
<div class="metric-detail">P(event)</div>
</div>
<div class="metric-card many">
<div class="metric-label" id="many-label">50 opportunities</div>
<div class="metric-value" id="many-probability">39.5%</div>
<div class="metric-detail">P(at least one event)</div>
</div>
</div>
<div class="comparison-ratio" id="comparison-ratio"></div>
</section>
<section aria-live="polite">
<h3>Current independent sample</h3>
<div class="sample-grid">
<div class="sample-stat">
<span>Single draw</span>
<strong id="single-result">No event</strong>
</div>
<div class="sample-stat">
<span>Batch result</span>
<strong id="batch-result">0 events</strong>
</div>
</div>
<div class="trial-strip" id="trial-strip"></div>
<div class="strip-caption" id="strip-caption">Latest outcomes in the batch</div>
<div class="gap-box">
<strong id="gap-title">A gap does not change the next probability</strong>
<p id="gap-copy">Independent draws have no memory.</p>
<div class="gap-facts">
<span>Longest observed gap: <strong id="longest-gap">0 misses</strong></span>
<span>Average spacing: <strong id="average-gap">99 trials</strong></span>
</div>
</div>
</section>
<section>
<h3>Compare independent batch sizes</h3>
<div class="table-head">
<span>n</span>
<span>P(≥1 event)</span>
<span>Expected hits</span>
</div>
<div id="probability-table"></div>
</section>
<div class="method-note">
<strong>Why the probability behaves this way.</strong>
The probability of seeing no event in n trials is (1 − p)<sup>n</sup>.
Subtracting that from 1 gives the probability of seeing at least one event.
Expected hits, n × p, may exceed one; the table’s main probability is still
capped at 100%.
</div>
</aside>
</main>
<script type="module">
import * as THREE from "https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
const $ = (selector) => document.querySelector(selector);
const COLORS = {
cyan: 0x59ddff,
blue: 0x3d9dff,
coral: 0xff627e,
darkBlue: 0x17304b,
platform: 0x0e2035
};
const state = {
p: 0.01,
n: 50,
singleOutcome: false,
batchOutcomes: [],
startTime: 0,
longestGap: 0,
trailingGap: 0
};
const host = $("#canvas-host");
const clock = new THREE.Clock();
const renderer = new THREE.WebGLRenderer({
antialias: true,
alpha: true,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.15;
renderer.domElement.setAttribute("aria-label", "Interactive 3D comparison of one and many independent opportunities");
host.appendChild(renderer.domElement);
const scene = new THREE.Scene();
scene.fog = new THREE.FogExp2(0x07111e, 0.026);
const camera = new THREE.PerspectiveCamera(42, 1, 0.1, 100);
const cameraState = {
theta: 0,
phi: 1.18,
radius: 15.8,
targetTheta: 0,
targetPhi: 1.18,
targetRadius: 15.8,
target: new THREE.Vector3(0.25, 0.55, 0)
};
const sceneRoot = new THREE.Group();
scene.add(sceneRoot);
const singleGroup = new THREE.Group();
singleGroup.position.x = -3.25;
sceneRoot.add(singleGroup);
const manyGroup = new THREE.Group();
manyGroup.position.x = 2.35;
sceneRoot.add(manyGroup);
const manyPlatformGroup = new THREE.Group();
const manyTrialsGroup = new THREE.Group();
manyGroup.add(manyPlatformGroup, manyTrialsGroup);
const sphereGeometry = new THREE.SphereGeometry(0.15, 24, 18);
const singleHaloGeometry = new THREE.TorusGeometry(0.29, 0.018, 10, 64);
const manyHaloGeometry = new THREE.TorusGeometry(0.225, 0.013, 8, 48);
let singleTrial;
let manyTrials = [];
let singleLabel;
let manyLabel;
let sliderTimer;
function makeMaterial(color, options = {}) {
return new THREE.MeshStandardMaterial({
color,
roughness: 0.3,
metalness: 0.18,
emissive: color,
emissiveIntensity: 0.15,
...options
});
}
function roundedRect(ctx, x, y, w, h, r) {
ctx.beginPath();
ctx.moveTo(x + r, y);
ctx.arcTo(x + w, y, x + w, y + h, r);
ctx.arcTo(x + w, y + h, x, y + h, r);
ctx.arcTo(x, y + h, x, y, r);
ctx.arcTo(x, y, x + w, y, r);
ctx.closePath();
}
function makeLabel(accent = "#59ddff") {
const canvas = document.createElement("canvas");
canvas.width = 1024;
canvas.height = 300;
const texture = new THREE.CanvasTexture(canvas);
texture.colorSpace = THREE.SRGBColorSpace;
texture.minFilter = THREE.LinearFilter;
const material = new THREE.SpriteMaterial({
map: texture,
transparent: true,
depthTest: false,
depthWrite: false
});
const sprite = new THREE.Sprite(material);
sprite.scale.set(3.75, 1.1, 1);
sprite.renderOrder = 20;
sprite.userData.canvas = canvas;
sprite.userData.texture = texture;
sprite.userData.accent = accent;
return sprite;
}
function updateLabel(sprite, title, value) {
const canvas = sprite.userData.canvas;
const ctx = canvas.getContext("2d");
const accent = sprite.userData.accent;
ctx.clearRect(0, 0, canvas.width, canvas.height);
roundedRect(ctx, 8, 8, 1008, 284, 28);
ctx.fillStyle = "rgba(7, 17, 31, 0.90)";
ctx.fill();
ctx.strokeStyle = accent;
ctx.globalAlpha = 0.35;
ctx.lineWidth = 3;
ctx.stroke();
ctx.globalAlpha = 1;
ctx.fillStyle = accent;
ctx.fillRect(48, 42, 100, 5);
ctx.fillStyle = "#91a9bf";
ctx.font = "700 42px system-ui, sans-serif";
ctx.fillText(title.toUpperCase(), 48, 104);
ctx.fillStyle = "#f0f7ff";
ctx.font = "800 64px system-ui, sans-serif";
ctx.fillText(value, 48, 218);
sprite.userData.texture.needsUpdate = true;
}
function addPlatform(parent, width, depth) {
const group = new THREE.Group();
parent.add(group);
const geometry = new THREE.BoxGeometry(width, 0.18, depth);
const platform = new THREE.Mesh(
geometry,
new THREE.MeshStandardMaterial({
color: COLORS.platform,
roughness: 0.52,
metalness: 0.48,
transparent: true,
opacity: 0.96
})
);
platform.position.y = 0;
platform.userData.uniqueGeometry = true;
group.add(platform);
const edgeMaterial = new THREE.LineBasicMaterial({
color: 0x31516e,
transparent: true,
opacity: 0.7
});
const edges = new THREE.LineSegments(new THREE.EdgesGeometry(geometry), edgeMaterial);
edges.userData.uniqueGeometry = true;
group.add(edges);
const inset = new THREE.Mesh(
new THREE.BoxGeometry(width - 0.18, 0.012, depth - 0.18),
new THREE.MeshBasicMaterial({
color: 0x173957,
transparent: true,
opacity: 0.38
})
);
inset.position.y = 0.1;
inset.userData.uniqueGeometry = true;
group.add(inset);
return group;
}
function clearDynamicGroup(group) {
group.traverse((object) => {
if (object.material) {
const materials = Array.isArray(object.material) ? object.material : [object.material];
materials.forEach((material) => material.dispose());
}
if (object.userData.uniqueGeometry && object.geometry) {
object.geometry.dispose();
}
});
group.clear();
}
function seededRandom(seed) {
return function () {
seed |= 0;
seed = (seed + 0x6D2B79F5) | 0;
let t = Math.imul(seed ^ (seed >>> 15), 1 | seed);
t = (t + Math.imul(t ^ (t >>> 7), 61 | t)) ^ t;
return ((t ^ (t >>> 14)) >>> 0) / 4294967296;
};
}
function buildEnvironment() {
const hemisphere = new THREE.HemisphereLight(0xa9d9ff, 0x080c16, 2.0);
scene.add(hemisphere);
const key = new THREE.DirectionalLight(0xffffff, 3.1);
key.position.set(-2, 8, 6);
scene.add(key);
const cyanLight = new THREE.PointLight(COLORS.cyan, 22, 14, 2);
cyanLight.position.set(-5, 3.3, 3);
scene.add(cyanLight);
const coralLight = new THREE.PointLight(COLORS.coral, 18, 13, 2);
coralLight.position.set(5, 2.8, 1);
scene.add(coralLight);
const floor = new THREE.Mesh(
new THREE.PlaneGeometry(60, 60),
new THREE.MeshStandardMaterial({
color: 0x07101c,
roughness: 0.88,
metalness: 0.18
})
);
floor.rotation.x = -Math.PI / 2;
floor.position.y = -0.11;
sceneRoot.add(floor);
const grid = new THREE.GridHelper(40, 40, 0x23435e, 0x173047);
grid.position.y = -0.095;
grid.material.transparent = true;
grid.material.opacity = 0.28;
sceneRoot.add(grid);
[3.1, 5.6, 8.2].forEach((radius, index) => {
const ring = new THREE.Mesh(
new THREE.TorusGeometry(radius, 0.012, 8, 160),
new THREE.MeshBasicMaterial({
color: index === 1 ? 0x3b7196 : 0x244a68,
transparent: true,
opacity: index === 1 ? 0.2 : 0.12
})
);
ring.rotation.x = Math.PI / 2;
ring.position.y = -0.075;
sceneRoot.add(ring);
});
const random = seededRandom(1039);
const positions = new Float32Array(420 * 3);
for (let i = 0; i < 420; i++) {
const radius = 8 + random() * 17;
const angle = random() * Math.PI * 2;
positions[i * 3] = Math.cos(angle) * radius;
positions[i * 3 + 1] = 1 + random() * 10;
positions[i * 3 + 2] = Math.sin(angle) * radius;
}
const starGeometry = new THREE.BufferGeometry();
starGeometry.setAttribute("position", new THREE.BufferAttribute(positions, 3));
const stars = new THREE.Points(
starGeometry,
new THREE.PointsMaterial({
color: 0x8ccfff,
size: 0.035,
transparent: true,
opacity: 0.62,
depthWrite: false
})
);
sceneRoot.add(stars);
}
function buildSingle() {
addPlatform(singleGroup, 2.5, 2.25);
const beam = new THREE.Mesh(
new THREE.CylinderGeometry(0.43, 0.62, 2.1, 40, 1, true),
new THREE.MeshBasicMaterial({
color: COLORS.cyan,
transparent: true,
opacity: 0.035,
side: THREE.DoubleSide,
depthWrite: false
})
);
beam.position.y = 1.15;
singleGroup.add(beam);
const group = new THREE.Group();
group.position.y = 0.5;
singleGroup.add(group);
const material = makeMaterial(COLORS.darkBlue);
const mesh = new THREE.Mesh(sphereGeometry, material);
group.add(mesh);
const halo = new THREE.Mesh(singleHaloGeometry, new THREE.MeshBasicMaterial({
color: COLORS.coral,
transparent: true,
opacity: 0,
depthWrite: false
}));
group.add(halo);
singleTrial = {
group,
mesh,
halo,
baseY: 0.5,
delay: 0.22,
outcome: false
};
singleLabel = makeLabel("#59ddff");
singleLabel.position.set(0, 2.45, -0.35);
singleGroup.add(singleLabel);
}
function buildMany() {
clearDynamicGroup(manyPlatformGroup);
clearDynamicGroup(manyTrialsGroup);
manyTrials = [];
const n = state.n;
let columns;
if (n <= 10) columns = 5;
else if (n <= 25) columns = 5;
else if (n <= 50) columns = 8;
else if (n <= 100) columns = 10;
else columns = 16;
const rows = Math.ceil(n / columns);
const spacing = columns <= 10 ? 0.52 : 0.39;
const width = columns * spacing + 0.55;
const depth = rows * spacing + 0.55;
addPlatform(manyPlatformGroup, width, depth);
if (!manyLabel) {
manyLabel = makeLabel("#ff627e");
manyGroup.add(manyLabel);
}
manyLabel.position.set(0, 2.6, -depth / 2 + 0.5);
const interval = n > 100 ? 0.008 : n > 50 ? 0.014 : 0.021;
for (let i = 0; i < n; i++) {
const column = i % columns;
const row = Math.floor(i / columns);
const group = new THREE.Group();
group.position.set(
(column - (columns - 1) / 2) * spacing,
0.39,
(row - (rows - 1) / 2) * spacing
);
const material = makeMaterial(COLORS.darkBlue);
const mesh = new THREE.Mesh(sphereGeometry, material);
group.add(mesh);
const halo = new THREE.Mesh(manyHaloGeometry, new THREE.MeshBasicMaterial({
color: COLORS.coral,
transparent: true,
opacity: 0,
depthWrite: false
}));
group.add(halo);
manyTrialsGroup.add(group);
manyTrials.push({
group,
mesh,
halo,
baseY: 0.39,
delay: 0.35 + i * interval,
outcome: false
});
}
cameraState.targetRadius = state.n > 100 ? 17.1 : 15.8;
}
function setRevealedMaterial(material, hit) {
const color = hit ? COLORS.coral : COLORS.blue;
material.color.setHex(color);
material.emissive.setHex(color);
material.emissiveIntensity = hit ? 0.62 : 0.15;
material.roughness = hit ? 0.25 : 0.32;
}
function setHiddenMaterial(material) {
material.color.setHex(COLORS.darkBlue);
material.emissive.setHex(0x081729);
material.emissiveIntensity = 0.08;
}
function easeOutBack(value) {
const c1 = 1.70158;
const c3 = c1 + 1;
return 1 + c3 * Math.pow(value - 1, 3) + c1 * Math.pow(value - 1, 2);
}
function formatPercent(value) {
const percent = value * 100;
if (percent >= 10) return `${percent.toFixed(1)}%`;
if (percent >= 1) return `${percent.toFixed(1)}%`;
return `${percent.toFixed(2)}%`;
}
function gapStats(outcomes) {
let longest = 0;
let current = 0;
let trailing = 0;
outcomes.forEach((hit) => {
if (!hit) {
current++;
longest = Math.max(longest, current);
} else {
current = 0;
}
});
for (let i = outcomes.length - 1; i >= 0 && !outcomes[i]; i--) {
trailing++;
}
return { longest, trailing };
}
function runTrials() {
state.singleOutcome = Math.random() < state.p;
state.batchOutcomes = Array.from({ length: state.n }, () => Math.random() < state.p);
state.startTime = clock.getElapsedTime();
singleTrial.outcome = state.singleOutcome;
manyTrials.forEach((trial, index) => {
trial.outcome = state.batchOutcomes[index];
});
setHiddenMaterial(singleTrial.mesh.material);
manyTrials.forEach((trial) => setHiddenMaterial(trial.mesh.material));
const gaps = gapStats(state.batchOutcomes);
state.longestGap = gaps.longest;
state.trailingGap = gaps.trailing;
updateSamplePanel();
}
function updateSamplePanel() {
const hits = state.batchOutcomes.filter(Boolean).length;
$("#single-result").textContent = state.singleOutcome ? "Event observed" : "No event this run";
$("#single-result").style.color = state.singleOutcome ? "var(--coral)" : "#a7c7df";
$("#batch-result").textContent = `${hits} event${hits === 1 ? "" : "s"} / ${state.n} trials`;
const strip = $("#trial-strip");
strip.replaceChildren();
const visible = state.batchOutcomes.slice(-60);
visible.forEach((hit, index) => {
const cell = document.createElement("span");
cell.className = `trial-cell${hit ? " hit" : ""}`;
const originalIndex = state.batchOutcomes.length - visible.length + index;
cell.title = `Independent trial ${originalIndex + 1}: ${hit ? "event" : "no event"}`;
strip.appendChild(cell);
});
$("#strip-caption").textContent = state.n <= 60
? `All ${state.n} outcomes, in draw order`
: "Latest 60 outcomes, in draw order";
$("#gap-title").textContent = state.trailingGap > 0
? `${state.trailingGap} consecutive miss${state.trailingGap === 1 ? "" : "es"} so far`
: "A gap does not change the next probability";
$("#gap-copy").textContent =
`After 10 misses, the next trial is still ${formatPercent(state.p)}. ` +
`Independent draws have no memory.`;
$("#longest-gap").textContent = `${state.longestGap} miss${state.longestGap === 1 ? "" : "es"}`;
$("#average-gap").textContent = `${Math.round((1 - state.p) / state.p)} trials`;
}
function updateProbabilityTable() {
const table = $("#probability-table");
table.replaceChildren();
const sizes = [1, 5, 10, 25, 50, 100, 250];
sizes.forEach((n) => {
const probability = 1 - Math.pow(1 - state.p, n);
const expected = n * state.p;
const row = document.createElement("div");
row.className = `table-row${n === state.n ? " selected" : ""}`;
const nValue = document.createElement("div");
nValue.className = "n-value";
nValue.textContent = n;
const probabilityCell = document.createElement("div");
probabilityCell.className = "probability-cell";
const top = document.createElement("div");
top.className = "probability-top";
const value = document.createElement("strong");
value.textContent = formatPercent(probability);
const expectedLabel = document.createElement("span");
expectedLabel.textContent = `P${n === 1 ? "" : "≥1"}`;
expectedLabel.style.color = "#657e95";
top.append(value, expectedLabel);
const bar = document.createElement("div");
bar.className = "probability-bar";
const fill = document.createElement("i");
fill.style.width = `${Math.max(0.4, probability * 100)}%`;
bar.appendChild(fill);
probabilityCell.append(top, bar);
const expectedValue = document.createElement("div");
expectedValue.className = "expected-value";
expectedValue.textContent = expected.toFixed(expected < 1 ? 2 : 1);
row.append(nValue, probabilityCell, expectedValue);
table.appendChild(row);
});
}
function updateAll() {
const oneProbability = state.p;
const manyProbability = 1 - Math.pow(1 - state.p, state.n);
const ratio = manyProbability / oneProbability;
$("#probability-value").textContent = formatPercent(state.p);
$("#batch-value").textContent = `${state.n} trials`;
$("#single-probability").textContent = formatPercent(oneProbability);
$("#many-probability").textContent = formatPercent(manyProbability);
$("#many-label").textContent = `${state.n} opportunities`;
$("#comparison-ratio").innerHTML =
`The ${state.n}-trial batch has <strong>${ratio.toFixed(1)}×</strong> the ` +
`at-least-one-event probability of a single opportunity.`;
const slider = $("#probability");
const fill = ((state.p * 100 - 0.5) / 9.5) * 100;
slider.style.setProperty("--range-fill", `${fill}%`);
document.querySelectorAll(".size-button").forEach((button) => {
button.classList.toggle("active", Number(button.dataset.n) === state.n);
});
updateLabel(singleLabel, "One opportunity", `P(event) = ${formatPercent(oneProbability)}`);
updateLabel(
manyLabel,
`${state.n} independent trials`,
`P(≥1 event) = ${formatPercent(manyProbability)}`
);
updateProbabilityTable();
}
function bindControls() {
$("#probability").addEventListener("input", (event) => {
state.p = Number(event.target.value) / 100;
updateAll();
clearTimeout(sliderTimer);
sliderTimer = setTimeout(runTrials, 120);
});
document.querySelectorAll(".size-button").forEach((button) => {
button.addEventListener("click", () => {
state.n = Number(button.dataset.n);
buildMany();
updateAll();
runTrials();
});
});
$("#run-trials").addEventListener("click", runTrials);
$("#reset-camera").addEventListener("click", () => {
cameraState.targetTheta = 0;
cameraState.targetPhi = 1.18;
cameraState.targetRadius = state.n > 100 ? 17.1 : 15.8;
cameraState.target.set(0.25, 0.55, 0);
});
}
function bindCameraControls() {
const canvas = renderer.domElement;
let dragging = false;
let lastX = 0;
let lastY = 0;
canvas.addEventListener("pointerdown", (event) => {
dragging = true;
lastX = event.clientX;
lastY = event.clientY;
canvas.setPointerCapture(event.pointerId);
canvas.classList.add("grabbing");
});
canvas.addEventListener("pointermove", (event) => {
if (!dragging) return;
const deltaX = event.clientX - lastX;
const deltaY = event.clientY - lastY;
lastX = event.clientX;
lastY = event.clientY;
cameraState.targetTheta -= deltaX * 0.006;
cameraState.targetPhi = THREE.MathUtils.clamp(
cameraState.targetPhi - deltaY * 0.005,
0.48,
1.42
);
});
function endDrag(event) {
dragging = false;
if (canvas.hasPointerCapture(event.pointerId)) {
canvas.releasePointerCapture(event.pointerId);
}
canvas.classList.remove("grabbing");
}
canvas.addEventListener("pointerup", endDrag);
canvas.addEventListener("pointercancel", endDrag);
canvas.addEventListener("wheel", (event) => {
event.preventDefault();
cameraState.targetRadius = THREE.MathUtils.clamp(
cameraState.targetRadius + event.deltaY * 0.009,
8,
24
);
}, { passive: false });
}
function resize() {
const width = Math.max(1, host.clientWidth);
const height = Math.max(1, host.clientHeight);
renderer.setSize(width, height, false);
camera.aspect = width / height;
camera.updateProjectionMatrix();
}
function updateCamera() {
cameraState.theta += (cameraState.targetTheta - cameraState.theta) * 0.085;
cameraState.phi += (cameraState.targetPhi - cameraState.phi) * 0.085;
cameraState.radius += (cameraState.targetRadius - cameraState.radius) * 0.085;
const sinPhi = Math.sin(cameraState.phi);
camera.position.set(
cameraState.target.x + cameraState.radius * sinPhi * Math.sin(cameraState.theta),
cameraState.target.y + cameraState.radius * Math.cos(cameraState.phi),
cameraState.target.z + cameraState.radius * sinPhi * Math.cos(cameraState.theta)
);
camera.lookAt(cameraState.target);
}
function animate() {
requestAnimationFrame(animate);
const elapsed = clock.getElapsedTime();
const animationElapsed = elapsed - state.startTime;
const singleReveal = THREE.MathUtils.clamp((animationElapsed - singleTrial.delay) / 0.36, 0, 1);
singleTrial.group.scale.setScalar(Math.max(0.001, easeOutBack(singleReveal)));
singleTrial.group.position.y = singleTrial.baseY + Math.sin(elapsed * 1.5) * 0.025;
if (singleReveal > 0) {
setRevealedMaterial(singleTrial.mesh.material, singleTrial.outcome);
singleTrial.halo.material.opacity = singleTrial.outcome ? 0.72 : 0;
if (singleTrial.outcome) {
const pulse = 1 + Math.sin(elapsed * 4.2) * 0.09;
singleTrial.halo.scale.setScalar(pulse);
}
}
manyTrials.forEach((trial, index) => {
const reveal = THREE.MathUtils.clamp((animationElapsed - trial.delay) / 0.33, 0, 1);
trial.group.scale.setScalar(Math.max(0.001, easeOutBack(reveal)));
trial.group.position.y = trial.baseY + Math.sin(elapsed * 1.45 + index * 0.37) * 0.018;
if (reveal > 0) {
setRevealedMaterial(trial.mesh.material, trial.outcome);
trial.halo.material.opacity = trial.outcome ? 0.66 : 0;
if (trial.outcome) {
const pulse = 1 + Math.sin(elapsed * 4 + index * 0.21) * 0.12;
trial.halo.scale.setScalar(pulse);
}
}
});
updateCamera();
renderer.render(scene, camera);
}
buildEnvironment();
buildSingle();
buildMany();
bindControls();
bindCameraControls();
updateAll();
const resizeObserver = new ResizeObserver(resize);
resizeObserver.observe(host);
resize();
runTrials();
animate();
</script>
</body>
</html>
```