Prompt specification
Build or customize the Three.js 3D scene "Independent spinner pair model" 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: Independent spinner pair model
Category: Algorithms & Systems · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/independent-spinner-pair-model-x1801
Scene Description Prompt:
Two independent low-poly spinners feed a joint-outcome grid; multiply their labeled probabilities. Standalone HTML with Three.js from cdnjs.
Working Three.js Starter (independent-spinner-pair-model-x1801):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Independent Spinners — Joint Probability Lab</title>
<style>
:root {
color-scheme: dark;
--bg: #090d16;
--panel: rgba(17, 24, 39, 0.78);
--panel-solid: #111827;
--line: rgba(255, 255, 255, 0.10);
--text: #f4f7fb;
--muted: #93a1b7;
--cyan: #58e2dc;
--blue: #6997ff;
--orange: #ffb45e;
--pink: #ff708f;
--green: #9cf29c;
}
* { box-sizing: border-box; }
html, body {
margin: 0;
min-height: 100%;
background:
radial-gradient(circle at 15% 0%, rgba(71, 112, 255, 0.16), transparent 34rem),
radial-gradient(circle at 88% 12%, rgba(255, 123, 89, 0.12), transparent 30rem),
var(--bg);
color: var(--text);
font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}
button { font: inherit; }
.app {
width: min(1440px, 100%);
margin: 0 auto;
padding: 18px;
}
.topbar {
display: flex;
align-items: center;
justify-content: space-between;
gap: 20px;
padding: 7px 3px 17px;
}
.brand {
display: flex;
align-items: center;
gap: 13px;
min-width: 0;
}
.brand-mark {
position: relative;
width: 43px;
height: 43px;
flex: 0 0 auto;
border: 1px solid rgba(88, 226, 220, 0.34);
border-radius: 13px;
background: linear-gradient(145deg, rgba(88, 226, 220, 0.18), rgba(105, 151, 255, 0.08));
box-shadow: inset 0 0 24px rgba(88, 226, 220, 0.08);
}
.brand-mark::before,
.brand-mark::after {
content: "";
position: absolute;
border-radius: 50%;
border: 2px solid var(--cyan);
}
.brand-mark::before {
width: 19px;
height: 19px;
left: 7px;
top: 7px;
}
.brand-mark::after {
width: 8px;
height: 8px;
right: 7px;
bottom: 7px;
background: var(--orange);
border-color: var(--orange);
}
.eyebrow {
margin: 0 0 3px;
color: var(--cyan);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.18em;
text-transform: uppercase;
}
h1 {
margin: 0;
font-size: clamp(18px, 2vw, 25px);
line-height: 1.1;
letter-spacing: -0.035em;
}
.controls {
display: flex;
flex-wrap: wrap;
justify-content: flex-end;
gap: 8px;
}
.button {
appearance: none;
border: 1px solid var(--line);
border-radius: 10px;
padding: 10px 13px;
color: var(--text);
background: rgba(255, 255, 255, 0.055);
cursor: pointer;
font-size: 12px;
font-weight: 750;
transition: transform 140ms ease, background 140ms ease, border-color 140ms ease, opacity 140ms ease;
}
.button:hover:not(:disabled) {
transform: translateY(-1px);
border-color: rgba(255, 255, 255, 0.23);
background: rgba(255, 255, 255, 0.095);
}
.button:active:not(:disabled) {
transform: translateY(0);
}
.button:focus-visible {
outline: 2px solid var(--cyan);
outline-offset: 3px;
}
.button.primary {
color: #07110f;
border-color: transparent;
background: linear-gradient(135deg, var(--cyan), #8ff0c7);
box-shadow: 0 8px 24px rgba(88, 226, 220, 0.16);
}
.button:disabled {
opacity: 0.42;
cursor: not-allowed;
}
.stage {
position: relative;
height: clamp(420px, 57vh, 590px);
min-height: 420px;
overflow: hidden;
border: 1px solid var(--line);
border-radius: 22px;
background:
linear-gradient(rgba(255,255,255,0.018) 1px, transparent 1px),
linear-gradient(90deg, rgba(255,255,255,0.018) 1px, transparent 1px),
radial-gradient(ellipse at 50% 48%, rgba(46, 69, 104, 0.28), transparent 57%),
rgba(12, 18, 31, 0.78);
background-size: 42px 42px, 42px 42px, auto, auto;
box-shadow: 0 22px 70px rgba(0, 0, 0, 0.25);
isolation: isolate;
}
.stage::after {
content: "";
position: absolute;
inset: 0;
pointer-events: none;
background: linear-gradient(180deg, rgba(8, 13, 23, 0.03), rgba(8, 13, 23, 0.24));
}
#scene {
position: absolute;
inset: 0;
width: 100%;
height: 100%;
display: block;
touch-action: none;
cursor: grab;
}
#scene:active { cursor: grabbing; }
.stage-heading {
position: absolute;
z-index: 3;
top: 20px;
left: 22px;
pointer-events: none;
}
.stage-heading strong {
display: block;
font-size: 11px;
letter-spacing: 0.16em;
text-transform: uppercase;
}
.stage-heading span {
display: block;
margin-top: 5px;
color: var(--muted);
font-size: 11px;
}
.camera-hint {
position: absolute;
z-index: 3;
right: 19px;
top: 20px;
color: rgba(255,255,255,0.42);
font-size: 10px;
letter-spacing: 0.04em;
pointer-events: none;
}
.wheel-caption {
position: absolute;
z-index: 4;
bottom: 20px;
width: min(180px, 35%);
padding: 10px 13px;
border: 1px solid var(--line);
border-radius: 12px;
background: rgba(10, 16, 28, 0.78);
backdrop-filter: blur(12px);
pointer-events: none;
}
#caption-a { left: 7%; }
#caption-b { right: 7%; }
.caption-top {
display: flex;
align-items: center;
justify-content: space-between;
gap: 8px;
margin-bottom: 5px;
}
.caption-name {
font-size: 12px;
font-weight: 850;
letter-spacing: 0.05em;
}
.caption-name.a { color: var(--cyan); }
.caption-name.b { color: var(--orange); }
.caption-prob {
color: var(--muted);
font-size: 11px;
}
.caption-value {
font-size: 20px;
font-weight: 850;
letter-spacing: -0.035em;
}
.joint-readout {
position: absolute;
z-index: 5;
top: 43%;
left: 50%;
width: 190px;
transform: translate(-50%, -50%);
padding: 13px 15px;
border: 1px solid rgba(255,255,255,0.13);
border-radius: 15px;
background: rgba(11, 17, 29, 0.90);
box-shadow: 0 18px 45px rgba(0,0,0,0.34);
backdrop-filter: blur(16px);
text-align: center;
pointer-events: none;
}
.joint-label {
color: var(--muted);
font-size: 9px;
font-weight: 800;
letter-spacing: 0.16em;
text-transform: uppercase;
}
.joint-expression {
margin-top: 7px;
font-size: 15px;
font-weight: 800;
white-space: nowrap;
}
.joint-result {
margin-top: 5px;
color: var(--green);
font-size: 25px;
font-weight: 900;
letter-spacing: -0.05em;
}
.joint-percent {
color: var(--muted);
font-size: 11px;
}
.workbench {
margin-top: 13px;
overflow: hidden;
border: 1px solid var(--line);
border-radius: 18px;
background: var(--panel);
backdrop-filter: blur(18px);
box-shadow: 0 18px 60px rgba(0,0,0,0.20);
}
.workbench-header {
display: flex;
align-items: center;
justify-content: space-between;
gap: 20px;
padding: 16px 18px;
border-bottom: 1px solid var(--line);
}
.workbench-title h2 {
margin: 0;
font-size: 14px;
letter-spacing: -0.015em;
}
.workbench-title p {
margin: 4px 0 0;
color: var(--muted);
font-size: 11px;
}
.formula {
flex: 0 0 auto;
padding: 8px 12px;
border: 1px solid rgba(88, 226, 220, 0.16);
border-radius: 9px;
color: var(--cyan);
background: rgba(88, 226, 220, 0.055);
font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
font-size: 12px;
font-weight: 700;
}
.grid-wrap {
padding: 15px 18px 19px;
overflow-x: auto;
}
.joint-grid {
min-width: 690px;
display: grid;
grid-template-columns: 115px repeat(4, minmax(125px, 1fr));
gap: 6px;
}
.corner {
display: flex;
align-items: flex-end;
padding: 5px 7px 7px;
color: var(--muted);
font-size: 9px;
font-weight: 750;
letter-spacing: 0.10em;
text-transform: uppercase;
}
.column-head,
.row-head {
display: flex;
align-items: center;
border: 1px solid rgba(255,255,255,0.065);
border-radius: 9px;
background: rgba(255,255,255,0.028);
}
.column-head {
min-height: 48px;
justify-content: space-between;
gap: 9px;
padding: 8px 11px;
}
.column-head b,
.row-head b {
font-size: 11px;
}
.column-head span,
.row-head span {
color: var(--muted);
font-size: 10px;
white-space: nowrap;
}
.column-head.b b { color: var(--orange); }
.row-head.a b { color: var(--cyan); }
.row-head {
min-height: 68px;
justify-content: space-between;
gap: 9px;
padding: 10px 11px;
}
.cell {
position: relative;
min-height: 68px;
padding: 9px 10px;
overflow: hidden;
border: 1px solid rgba(255,255,255,0.065);
border-radius: 9px;
color: var(--text);
background: rgba(255,255,255,0.025);
cursor: pointer;
text-align: left;
transition: border-color 140ms ease, background 140ms ease, transform 140ms ease;
}
.cell:hover {
transform: translateY(-1px);
border-color: rgba(255,255,255,0.20);
background: rgba(255,255,255,0.055);
}
.cell:focus-visible {
outline: 2px solid var(--cyan);
outline-offset: 1px;
}
.cell.active {
border-color: rgba(156, 242, 156, 0.62);
background: linear-gradient(145deg, rgba(156,242,156,0.13), rgba(88,226,220,0.045));
box-shadow: inset 0 0 0 1px rgba(156,242,156,0.08);
}
.cell.sampled::after {
content: "";
position: absolute;
top: 8px;
right: 8px;
width: 5px;
height: 5px;
border-radius: 50%;
background: var(--green);
box-shadow: 0 0 10px rgba(156,242,156,0.8);
}
.cell-factors {
color: var(--muted);
font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
font-size: 9px;
white-space: nowrap;
}
.cell-product {
margin-top: 6px;
font-size: 15px;
font-weight: 850;
letter-spacing: -0.025em;
}
.cell-percent {
position: absolute;
right: 9px;
bottom: 9px;
color: var(--muted);
font-size: 9px;
}
.legend {
display: flex;
flex-wrap: wrap;
gap: 14px;
align-items: center;
margin-top: 13px;
color: var(--muted);
font-size: 10px;
}
.legend-item {
display: inline-flex;
align-items: center;
gap: 6px;
}
.dot {
width: 7px;
height: 7px;
border-radius: 50%;
background: var(--green);
box-shadow: 0 0 9px rgba(156,242,156,0.55);
}
.dot.outline {
background: transparent;
border: 1px solid rgba(255,255,255,0.30);
box-shadow: none;
}
.fallback {
position: absolute;
inset: 0;
display: grid;
place-items: center;
padding: 30px;
color: var(--muted);
text-align: center;
}
@media (max-width: 760px) {
.app { padding: 10px; }
.topbar { align-items: flex-start; flex-direction: column; }
.controls { width: 100%; justify-content: flex-start; }
.button { flex: 1; white-space: nowrap; }
.stage { height: 510px; }
.camera-hint { display: none; }
.stage-heading { top: 15px; left: 15px; }
.wheel-caption { bottom: 13px; width: 39%; padding: 9px; }
#caption-a { left: 4%; }
#caption-b { right: 4%; }
.caption-value { font-size: 17px; }
.joint-readout { top: 17%; width: 165px; }
.workbench-header { align-items: flex-start; flex-direction: column; gap: 10px; }
.grid-wrap { padding: 12px; }
}
@media (prefers-reduced-motion: reduce) {
*, *::before, *::after {
scroll-behavior: auto !important;
transition-duration: 0.01ms !important;
}
}
</style>
</head>
<body>
<div class="app">
<header class="topbar">
<div class="brand">
<div class="brand-mark" aria-hidden="true"></div>
<div>
<p class="eyebrow">Probability laboratory</p>
<h1>Independent Spinners</h1>
</div>
</div>
<div class="controls">
<button class="button" id="spin-a" type="button">Spin A</button>
<button class="button" id="spin-b" type="button">Spin B</button>
<button class="button primary" id="spin-both" type="button">Spin both ↗</button>
</div>
</header>
<main>
<section class="stage" aria-label="Two interactive probability spinners">
<canvas id="scene" aria-label="Three-dimensional view of two spinners"></canvas>
<div class="stage-heading">
<strong>Joint-outcome experiment</strong>
<span>Wedge size represents labeled probability.</span>
</div>
<div class="camera-hint">Drag to orbit · Scroll to zoom</div>
<div class="wheel-caption" id="caption-a">
<div class="caption-top">
<span class="caption-name a">SPINNER A</span>
<span class="caption-prob">P(A)</span>
</div>
<div class="caption-value" id="value-a">A1 · 1/2</div>
</div>
<div class="wheel-caption" id="caption-b">
<div class="caption-top">
<span class="caption-name b">SPINNER B</span>
<span class="caption-prob">P(B)</span>
</div>
<div class="caption-value" id="value-b">B1 · 1/3</div>
</div>
<div class="joint-readout" id="joint-readout" aria-live="polite">
<div class="joint-label">Joint event</div>
<div class="joint-expression" id="joint-expression">A1 × B1</div>
<div class="joint-result" id="joint-result">1/6</div>
<div class="joint-percent" id="joint-percent">16.67% probability</div>
</div>
</section>
<section class="workbench">
<div class="workbench-header">
<div class="workbench-title">
<h2>Joint-outcome grid</h2>
<p>Rows are outcomes of A; columns are outcomes of B. Select any cell to inspect its product.</p>
</div>
<div class="formula">P(A ∩ B) = P(A) × P(B)</div>
</div>
<div class="grid-wrap">
<div class="joint-grid" id="joint-grid"></div>
<div class="legend">
<span class="legend-item"><i class="dot"></i> Selected outcome</span>
<span class="legend-item"><i class="dot outline"></i> Latest spin intersection</span>
<span>All values shown as exact fractions and percentages.</span>
</div>
</div>
</section>
</main>
</div>
<script type="module">
import * as THREE from "https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
const TAU = Math.PI * 2;
const reducedMotion = matchMedia("(prefers-reduced-motion: reduce)").matches;
const outcomesA = [
{ label: "A1", probability: 1 / 2, numerator: 6, denominator: 12, color: 0x58e2dc },
{ label: "A2", probability: 1 / 4, numerator: 3, denominator: 12, color: 0x6997ff },
{ label: "A3", probability: 1 / 6, numerator: 2, denominator: 12, color: 0x9c83ff },
{ label: "A4", probability: 1 / 12, numerator: 1, denominator: 12, color: 0x3f6fb6 }
];
const outcomesB = [
{ label: "B1", probability: 1 / 3, numerator: 4, denominator: 12, color: 0xffb45e },
{ label: "B2", probability: 1 / 4, numerator: 3, denominator: 12, color: 0xff708f },
{ label: "B3", probability: 1 / 6, numerator: 2, denominator: 12, color: 0xff8f62 },
{ label: "B4", probability: 1 / 4, numerator: 3, denominator: 12, color: 0xd95e9f }
];
const canvas = document.querySelector("#scene");
const stage = document.querySelector(".stage");
const valueA = document.querySelector("#value-a");
const valueB = document.querySelector("#value-b");
const jointExpression = document.querySelector("#joint-expression");
const jointResult = document.querySelector("#joint-result");
const jointPercent = document.querySelector("#joint-percent");
const gridElement = document.querySelector("#joint-grid");
const spinAButton = document.querySelector("#spin-a");
const spinBButton = document.querySelector("#spin-b");
const spinBothButton = document.querySelector("#spin-both");
let renderer;
try {
renderer = new THREE.WebGLRenderer({
canvas,
alpha: true,
antialias: true,
powerPreference: "high-performance"
});
} catch (error) {
const fallback = document.createElement("div");
fallback.className = "fallback";
fallback.textContent = "WebGL is unavailable in this browser. The probability grid remains fully usable.";
stage.appendChild(fallback);
}
const scene = new THREE.Scene();
const camera = new THREE.PerspectiveCamera(38, 1, 0.1, 100);
if (renderer) {
renderer.setPixelRatio(Math.min(window.devicePixelRatio || 1, 2));
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.08;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
}
scene.add(new THREE.HemisphereLight(0xcfe6ff, 0x111827, 2.3));
const keyLight = new THREE.DirectionalLight(0xffffff, 3.2);
keyLight.position.set(-3.5, 5, 7);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(1024, 1024);
keyLight.shadow.camera.left = -6;
keyLight.shadow.camera.right = 6;
keyLight.shadow.camera.top = 6;
keyLight.shadow.camera.bottom = -6;
scene.add(keyLight);
const rimLight = new THREE.DirectionalLight(0x58e2dc, 2.4);
rimLight.position.set(5, -1, 4);
scene.add(rimLight);
const warmLight = new THREE.DirectionalLight(0xff8e67, 1.8);
warmLight.position.set(-4, -3, 3);
scene.add(warmLight);
const shadowPlane = new THREE.Mesh(
new THREE.PlaneGeometry(12, 7),
new THREE.ShadowMaterial({ color: 0x000000, opacity: 0.25 })
);
shadowPlane.position.z = -0.32;
shadowPlane.receiveShadow = true;
scene.add(shadowPlane);
function createTextSprite(title, subtitle, accent) {
const labelCanvas = document.createElement("canvas");
labelCanvas.width = 512;
labelCanvas.height = 160;
const ctx = labelCanvas.getContext("2d");
ctx.clearRect(0, 0, 512, 160);
const r = 28;
ctx.beginPath();
ctx.moveTo(r, 4);
ctx.lineTo(508 - r, 4);
ctx.quadraticCurveTo(508, 4, 508, 4 + r);
ctx.lineTo(508, 156 - r);
ctx.quadraticCurveTo(508, 156, 508 - r, 156);
ctx.lineTo(r, 156);
ctx.quadraticCurveTo(4, 156, 4, 156 - r);
ctx.lineTo(4, 4 + r);
ctx.quadraticCurveTo(4, 4, r, 4);
ctx.closePath();
ctx.fillStyle = "rgba(7, 12, 22, 0.86)";
ctx.fill();
ctx.strokeStyle = "rgba(255,255,255,0.20)";
ctx.lineWidth = 3;
ctx.stroke();
ctx.textAlign = "center";
ctx.textBaseline = "middle";
ctx.font = "800 58px system-ui, sans-serif";
ctx.fillStyle = "#ffffff";
ctx.fillText(title, 256, 62);
ctx.font = "700 31px system-ui, sans-serif";
ctx.fillStyle = accent;
ctx.fillText(subtitle, 256, 116);
const texture = new THREE.CanvasTexture(labelCanvas);
texture.colorSpace = THREE.SRGBColorSpace;
texture.minFilter = THREE.LinearFilter;
const material = new THREE.SpriteMaterial({
map: texture,
transparent: true,
depthWrite: false,
depthTest: false
});
const sprite = new THREE.Sprite(material);
sprite.scale.set(0.72, 0.225, 1);
sprite.renderOrder = 10;
return sprite;
}
function createWheel(outcomes, isA) {
const group = new THREE.Group();
const radius = 1.08;
const thickness = 0.24;
const base = new THREE.Mesh(
new THREE.CylinderGeometry(radius + 0.08, radius + 0.08, thickness, 32),
new THREE.MeshStandardMaterial({
color: 0x121b2b,
roughness: 0.65,
metalness: 0.28,
flatShading: true
})
);
base.rotation.x = Math.PI / 2;
base.castShadow = true;
base.receiveShadow = true;
group.add(base);
let cumulative = 0;
outcomes.forEach((outcome, index) => {
const start = -Math.PI / 2 + cumulative * TAU;
const length = outcome.probability * TAU;
const center = start + length / 2;
const wedge = new THREE.Mesh(
new THREE.CircleGeometry(radius, Math.max(5, Math.ceil(length * 11)), start, length),
new THREE.MeshStandardMaterial({
color: outcome.color,
roughness: 0.72,
metalness: 0.08,
flatShading: true,
side: THREE.DoubleSide
})
);
wedge.position.z = thickness / 2 + 0.008;
wedge.castShadow = true;
group.add(wedge);
const borderPoints = [];
const borderSegments = Math.max(6, Math.ceil(length * 16));
for (let i = 0; i <= borderSegments; i++) {
const angle = start + length * (i / borderSegments);
borderPoints.push(new THREE.Vector3(
Math.cos(angle) * (radius + 0.006),
Math.sin(angle) * (radius + 0.006),
thickness / 2 + 0.016
));
}
const border = new THREE.Line(
new THREE.BufferGeometry().setFromPoints(borderPoints),
new THREE.LineBasicMaterial({
color: 0xeaf4ff,
transparent: true,
opacity: 0.30
})
);
group.add(border);
const separator = new THREE.Line(
new THREE.BufferGeometry().setFromPoints([
new THREE.Vector3(0, 0, thickness / 2 + 0.018),
new THREE.Vector3(
Math.cos(start) * radius,
Math.sin(start) * radius,
thickness / 2 + 0.018
)
]),
new THREE.LineBasicMaterial({
color: 0xffffff,
transparent: true,
opacity: 0.30
})
);
group.add(separator);
const accent = isA ? "#58e2dc" : "#ffb45e";
const label = createTextSprite(
outcome.label,
`${formatFraction(outcome.numerator, outcome.denominator)} probability`,
accent
);
label.position.set(
Math.cos(center) * radius * 0.59,
Math.sin(center) * radius * 0.59,
thickness / 2 + 0.07
);
group.add(label);
cumulative += outcome.probability;
});
const outerRim = new THREE.Mesh(
new THREE.TorusGeometry(radius + 0.055, 0.055, 6, 32),
new THREE.MeshStandardMaterial({
color: 0xd5e4f2,
roughness: 0.32,
metalness: 0.62,
flatShading: true
})
);
outerRim.position.z = thickness / 2 + 0.018;
outerRim.castShadow = true;
group.add(outerRim);
const hub = new THREE.Mesh(
new THREE.CylinderGeometry(0.19, 0.22, 0.12, 12),
new THREE.MeshStandardMaterial({
color: 0x111927,
roughness: 0.3,
metalness: 0.65,
flatShading: true
})
);
hub.rotation.x = Math.PI / 2;
hub.position.z = thickness / 2 + 0.09;
hub.castShadow = true;
group.add(hub);
const hubCap = new THREE.Mesh(
new THREE.CircleGeometry(0.075, 12),
new THREE.MeshBasicMaterial({ color: isA ? 0x58e2dc : 0xffb45e })
);
hubCap.position.z = thickness / 2 + 0.153;
group.add(hubCap);
const pointerShape = new THREE.Shape();
pointerShape.moveTo(0, 1.01);
pointerShape.lineTo(-0.13, 1.34);
pointerShape.lineTo(0.13, 1.34);
pointerShape.closePath();
const pointer = new THREE.Mesh(
new THREE.ShapeGeometry(pointerShape),
new THREE.MeshStandardMaterial({
color: 0xffffff,
roughness: 0.32,
metalness: 0.24,
side: THREE.DoubleSide
})
);
pointer.position.z = thickness / 2 + 0.18;
pointer.castShadow = true;
group.add(pointer);
const pointerInset = new THREE.Mesh(
new THREE.CircleGeometry(0.038, 12),
new THREE.MeshBasicMaterial({ color: isA ? 0x58e2dc : 0xffb45e })
);
pointerInset.position.set(0, 1.21, thickness / 2 + 0.185);
group.add(pointerInset);
return group;
}
const wheelA = createWheel(outcomesA, true);
const wheelB = createWheel(outcomesB, false);
scene.add(wheelA, wheelB);
const stateA = {
group: wheelA,
outcomes: outcomesA,
outcome: 0,
spinning: false,
animation: null
};
const stateB = {
group: wheelB,
outcomes: outcomesB,
outcome: 0,
spinning: false,
animation: null
};
function sectorCenter(outcome, outcomes) {
let cumulative = 0;
for (let i = 0; i < outcome; i++) cumulative += outcomes[i].probability;
return -Math.PI / 2 + (cumulative + outcomes[outcome].probability / 2) * TAU;
}
function rotationForOutcome(outcome, outcomes) {
return Math.PI / 2 - sectorCenter(outcome, outcomes);
}
wheelA.rotation.z = rotationForOutcome(0, outcomesA);
wheelB.rotation.z = rotationForOutcome(0, outcomesB);
function randomUnit() {
if (window.crypto && window.crypto.getRandomValues) {
const data = new Uint32Array(1);
window.crypto.getRandomValues(data);
return data[0] / 4294967296;
}
return Math.random();
}
function chooseOutcome(outcomes) {
const value = randomUnit();
let cumulative = 0;
for (let i = 0; i < outcomes.length; i++) {
cumulative += outcomes[i].probability;
if (value < cumulative) return i;
}
return outcomes.length - 1;
}
function easeOutQuint(t) {
return 1 - Math.pow(1 - t, 5);
}
function spinWheel(state) {
if (state.spinning) return;
state.spinning = true;
const selected = chooseOutcome(state.outcomes);
const desired = rotationForOutcome(selected, state.outcomes);
const current = state.group.rotation.z;
const normalizedCurrent = ((current % TAU) + TAU) % TAU;
const delta = ((desired - normalizedCurrent) % TAU + TAU) % TAU;
const turns = reducedMotion ? 0 : 5 + Math.floor(randomUnit() * 3);
const target = current + turns * TAU + delta;
state.animation = {
start: performance.now(),
from: current,
to: target,
duration: reducedMotion ? 180 : 2850 + randomUnit() * 600,
selected
};
updateButtons();
}
function finishSpin(state) {
const selected = state.animation.selected;
state.outcome = selected;
state.spinning = false;
state.animation = null;
if (state === stateA) {
valueA.textContent = `${outcomesA[selected].label} · ${formatProbability(outcomesA[selected])}`;
} else {
valueB.textContent = `${outcomesB[selected].label} · ${formatProbability(outcomesB[selected])}`;
}
activeCell = { row: stateA.outcome, column: stateB.outcome };
updateReadout();
updateGridClasses();
updateButtons();
}
function updateButtons() {
spinAButton.disabled = stateA.spinning;
spinBButton.disabled = stateB.spinning;
spinBothButton.disabled = stateA.spinning || stateB.spinning;
}
function gcd(a, b) {
while (b) {
const temp = b;
b = a % b;
a = temp;
}
return a;
}
function formatFraction(numerator, denominator) {
const divisor = gcd(numerator, denominator);
const n = numerator / divisor;
const d = denominator / divisor;
return `${n}/${d}`;
}
function formatProbability(outcome) {
return formatFraction(outcome.numerator, outcome.denominator);
}
let activeCell = { row: 0, column: 0 };
const cellElements = [];
function buildGrid() {
gridElement.innerHTML = "";
const corner = document.createElement("div");
corner.className = "corner";
corner.textContent = "P(A) ↓ / P(B) →";
gridElement.appendChild(corner);
outcomesB.forEach((outcomeB) => {
const head = document.createElement("div");
head.className = "column-head b";
head.innerHTML = `<b>${outcomeB.label}</b><span>P = ${formatProbability(outcomeB)}</span>`;
gridElement.appendChild(head);
});
outcomesA.forEach((outcomeA, row) => {
const head = document.createElement("div");
head.className = "row-head a";
head.innerHTML = `<b>${outcomeA.label}</b><span>P = ${formatProbability(outcomeA)}</span>`;
gridElement.appendChild(head);
outcomesB.forEach((outcomeB, column) => {
const cell = document.createElement("button");
cell.type = "button";
cell.className = "cell";
const productNumerator = outcomeA.numerator * outcomeB.numerator;
const productDenominator = outcomeA.denominator * outcomeB.denominator;
const fraction = formatFraction(productNumerator, productDenominator);
const percentage = productNumerator / productDenominator * 100;
cell.innerHTML = `
<div class="cell-factors">${formatProbability(outcomeA)} × ${formatProbability(outcomeB)}</div>
<div class="cell-product">${fraction}</div>
<div class="cell-percent">${percentage.toFixed(2)}%</div>
`;
cell.setAttribute(
"aria-label",
`${outcomeA.label} and ${outcomeB.label}: ${formatProbability(outcomeA)} times ${formatProbability(outcomeB)} equals ${fraction}, or ${percentage.toFixed(2)} percent`
);
cell.addEventListener("click", () => {
activeCell = { row, column };
updateReadout();
updateGridClasses();
});
gridElement.appendChild(cell);
cellElements.push({ row, column, element: cell });
});
});
}
function updateGridClasses() {
cellElements.forEach(({ row, column, element }) => {
element.classList.toggle("active", row === activeCell.row && column === activeCell.column);
element.classList.toggle(
"sampled",
row === stateA.outcome && column === stateB.outcome
);
});
}
function updateReadout() {
const a = outcomesA[activeCell.row];
const b = outcomesB[activeCell.column];
const numerator = a.numerator * b.numerator;
const denominator = a.denominator * b.denominator;
const fraction = formatFraction(numerator, denominator);
const percentage = numerator / denominator * 100;
jointExpression.textContent = `${a.label} × ${b.label}`;
jointResult.textContent = fraction;
jointPercent.textContent = `${percentage.toFixed(2)}% probability`;
}
buildGrid();
updateReadout();
updateGridClasses();
spinAButton.addEventListener("click", () => spinWheel(stateA));
spinBButton.addEventListener("click", () => spinWheel(stateB));
spinBothButton.addEventListener("click", () => {
spinWheel(stateA);
spinWheel(stateB);
});
let targetYaw = 0;
let targetPitch = 0;
let currentYaw = 0;
let currentPitch = 0;
let cameraRadius = 8.2;
let dragging = false;
let pointerX = 0;
let pointerY = 0;
canvas.addEventListener("pointerdown", (event) => {
dragging = true;
pointerX = event.clientX;
pointerY = event.clientY;
canvas.setPointerCapture(event.pointerId);
});
canvas.addEventListener("pointermove", (event) => {
if (!dragging) return;
const deltaX = event.clientX - pointerX;
const deltaY = event.clientY - pointerY;
pointerX = event.clientX;
pointerY = event.clientY;
targetYaw = THREE.MathUtils.clamp(targetYaw + deltaX * 0.004, -0.42, 0.42);
targetPitch = THREE.MathUtils.clamp(targetPitch + deltaY * 0.003, -0.18, 0.18);
});
function endDrag(event) {
dragging = false;
if (event.pointerId !== undefined && canvas.hasPointerCapture(event.pointerId)) {
canvas.releasePointerCapture(event.pointerId);
}
}
canvas.addEventListener("pointerup", endDrag);
canvas.addEventListener("pointercancel", endDrag);
canvas.addEventListener("wheel", (event) => {
event.preventDefault();
cameraRadius = THREE.MathUtils.clamp(cameraRadius + event.deltaY * 0.006, 6.6, 10.2);
}, { passive: false });
function resize() {
if (!renderer) return;
const width = stage.clientWidth;
const height = stage.clientHeight;
const aspect = width / height;
renderer.setSize(width, height, false);
camera.aspect = aspect;
camera.updateProjectionMatrix();
const compact = aspect < 1.15;
const wheelX = compact ? 1.42 : 2.32;
const wheelY = compact ? -0.45 : -0.10;
wheelA.position.set(-wheelX, wheelY, 0);
wheelB.position.set(wheelX, wheelY, 0);
if (compact) {
cameraRadius = 8.9;
} else {
cameraRadius = 8.2;
}
}
const resizeObserver = new ResizeObserver(resize);
resizeObserver.observe(stage);
resize();
function updateCamera() {
currentYaw += (targetYaw - currentYaw) * 0.07;
currentPitch += (targetPitch - currentPitch) * 0.07;
const horizontal = Math.cos(currentPitch) * cameraRadius;
camera.position.set(
Math.sin(currentYaw) * horizontal,
Math.sin(currentPitch) * cameraRadius - 0.1,
Math.cos(currentYaw) * horizontal
);
camera.lookAt(0, -0.05, 0);
}
function animate(now) {
requestAnimationFrame(animate);
[stateA, stateB].forEach((state) => {
if (!state.animation) return;
const animation = state.animation;
const elapsed = now - animation.start;
const progress = THREE.MathUtils.clamp(elapsed / animation.duration, 0, 1);
const eased = easeOutQuint(progress);
state.group.rotation.z = THREE.MathUtils.lerp(
animation.from,
animation.to,
eased
);
if (progress >= 1) {
finishSpin(state);
}
});
updateCamera();
if (renderer) renderer.render(scene, camera);
}
requestAnimationFrame(animate);
</script>
</body>
</html>
```