Prompt specification
Build or customize the Three.js 3D scene "With and without replacement urns" 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: With and without replacement urns
Category: Algorithms & Systems · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/with-and-without-replacement-urns-x1802
Scene Description Prompt:
Two stylized urns draw tokens with and without replacement, revealing different conditional second-draw probabilities. Three.js from cdnjs, one HTML file.
Working Three.js Starter (with-and-without-replacement-urns-x1802):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Urns & Conditional Probability</title>
<style>
:root {
color-scheme: dark;
--bg: #07111f;
--panel: rgba(12, 26, 44, 0.82);
--line: rgba(255, 255, 255, 0.12);
--muted: #92a4bb;
--text: #f5f8ff;
--red: #ff6078;
--blue: #50c9f4;
--without: #ffad66;
--with: #58e0c2;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
min-height: 100%;
margin: 0;
overflow: hidden;
background:
radial-gradient(circle at 50% 30%, #142944 0%, transparent 48%),
linear-gradient(145deg, #07111f, #091727 55%, #06101c);
color: var(--text);
font-family:
Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
button {
font: inherit;
}
#scene {
position: fixed;
inset: 0;
}
#scene canvas {
display: block;
width: 100%;
height: 100%;
cursor: grab;
touch-action: none;
}
#scene canvas.dragging {
cursor: grabbing;
}
.hero {
position: fixed;
z-index: 5;
top: 26px;
left: 30px;
max-width: 660px;
pointer-events: none;
}
.eyebrow {
display: flex;
align-items: center;
gap: 9px;
margin-bottom: 9px;
color: #9eb3cc;
font-size: 11px;
font-weight: 800;
letter-spacing: 0.18em;
text-transform: uppercase;
}
.eyebrow::before {
width: 9px;
height: 9px;
border-radius: 50%;
background: var(--with);
box-shadow: 0 0 18px rgba(88, 224, 194, 0.85);
content: "";
}
h1 {
margin: 0;
font-size: clamp(30px, 4vw, 52px);
font-weight: 750;
letter-spacing: -0.045em;
line-height: 1.02;
}
.hero p {
max-width: 590px;
margin: 12px 0 0;
color: var(--muted);
font-size: 14px;
line-height: 1.55;
}
.compare-note {
position: fixed;
z-index: 5;
top: 30px;
right: 30px;
display: grid;
grid-template-columns: auto auto;
gap: 2px 18px;
padding: 13px 16px;
border: 1px solid var(--line);
border-radius: 16px;
background: rgba(10, 23, 40, 0.68);
box-shadow: 0 15px 40px rgba(0, 0, 0, 0.16);
backdrop-filter: blur(14px);
pointer-events: none;
}
.compare-note span,
.compare-note small {
color: var(--muted);
font-size: 10px;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.compare-note strong {
grid-row: span 2;
align-self: center;
font-size: 15px;
}
.interaction-hint {
position: fixed;
z-index: 4;
top: 120px;
left: 50%;
transform: translateX(-50%);
color: rgba(192, 210, 231, 0.62);
font-size: 11px;
letter-spacing: 0.04em;
pointer-events: none;
}
.panel {
position: fixed;
z-index: 10;
bottom: 18px;
left: 50%;
display: grid;
width: min(980px, calc(100% - 32px));
grid-template-columns: repeat(2, minmax(0, 1fr));
gap: 14px;
transform: translateX(-50%);
}
.card {
--accent: var(--without);
position: relative;
overflow: hidden;
padding: 18px;
border: 1px solid var(--line);
border-radius: 22px;
background:
linear-gradient(145deg, rgba(255, 255, 255, 0.055), transparent 45%),
var(--panel);
box-shadow:
0 24px 70px rgba(0, 0, 0, 0.34),
inset 0 1px 0 rgba(255, 255, 255, 0.04);
backdrop-filter: blur(20px);
}
.card.with {
--accent: var(--with);
}
.card::before {
position: absolute;
top: 0;
right: 0;
left: 0;
height: 2px;
background: linear-gradient(90deg, transparent, var(--accent), transparent);
content: "";
opacity: 0.85;
}
.card.reveal {
animation: reveal 420ms ease-out;
}
@keyframes reveal {
0% {
box-shadow:
0 24px 70px rgba(0, 0, 0, 0.34),
0 0 0 0 color-mix(in srgb, var(--accent) 38%, transparent);
}
55% {
box-shadow:
0 24px 70px rgba(0, 0, 0, 0.34),
0 0 0 7px color-mix(in srgb, var(--accent) 8%, transparent);
}
100% {
box-shadow:
0 24px 70px rgba(0, 0, 0, 0.34),
0 0 0 0 transparent;
}
}
.card-top {
display: flex;
align-items: flex-start;
justify-content: space-between;
gap: 16px;
}
.kicker {
margin-bottom: 3px;
color: var(--accent);
font-size: 10px;
font-weight: 850;
letter-spacing: 0.14em;
text-transform: uppercase;
}
h2 {
margin: 0;
font-size: 20px;
letter-spacing: -0.025em;
}
.mix {
flex: 0 0 auto;
padding: 7px 10px;
border: 1px solid var(--line);
border-radius: 11px;
color: #c8d5e4;
font-size: 11px;
line-height: 1.35;
text-align: right;
}
.description {
min-height: 35px;
margin: 8px 0 12px;
color: var(--muted);
font-size: 12px;
line-height: 1.45;
}
.outcome {
display: flex;
min-height: 29px;
align-items: center;
gap: 8px;
margin-bottom: 12px;
color: #aebdd0;
font-size: 12px;
}
.outcome strong {
color: var(--text);
}
.outcome .second-result {
padding-left: 8px;
border-left: 1px solid var(--line);
}
.token-dot {
display: inline-block;
width: 10px;
height: 10px;
border-radius: 50%;
}
.token-dot.red {
background: var(--red);
box-shadow: 0 0 12px rgba(255, 96, 120, 0.55);
}
.token-dot.blue {
background: var(--blue);
box-shadow: 0 0 12px rgba(80, 201, 244, 0.55);
}
.probability-box {
padding: 12px;
border: 1px solid rgba(255, 255, 255, 0.08);
border-radius: 15px;
background: rgba(2, 10, 20, 0.34);
}
.probability-head {
display: flex;
align-items: center;
justify-content: space-between;
gap: 10px;
margin-bottom: 11px;
font-size: 11px;
}
.condition {
max-width: 58%;
overflow: hidden;
padding: 4px 7px;
border-radius: 999px;
background: color-mix(in srgb, var(--accent) 12%, transparent);
color: var(--accent);
font-size: 9px;
font-weight: 750;
letter-spacing: 0.05em;
text-overflow: ellipsis;
text-transform: uppercase;
white-space: nowrap;
}
.prob-row {
display: grid;
grid-template-columns: 48px 1fr 72px;
align-items: center;
gap: 9px;
margin-top: 8px;
}
.prob-name {
color: #c4d0df;
font-size: 11px;
}
.bar {
overflow: hidden;
height: 6px;
border-radius: 999px;
background: rgba(255, 255, 255, 0.07);
}
.bar i {
display: block;
width: 50%;
height: 100%;
border-radius: inherit;
transition: width 600ms cubic-bezier(0.22, 1, 0.36, 1);
}
.prob-row.red .bar i {
background: linear-gradient(90deg, #ff8a9b, var(--red));
}
.prob-row.blue .bar i {
background: linear-gradient(90deg, #8be0ff, var(--blue));
}
.prob-value {
display: flex;
align-items: baseline;
justify-content: flex-end;
gap: 5px;
font-size: 12px;
}
.prob-value b {
font-weight: 780;
}
.prob-value small {
color: var(--muted);
font-size: 9px;
}
.explanation {
min-height: 44px;
margin: 11px 2px 0;
color: #99abc0;
font-size: 11px;
line-height: 1.45;
}
.actions {
display: flex;
gap: 8px;
margin-top: 12px;
}
button {
border: 0;
color: var(--text);
cursor: pointer;
transition:
transform 160ms ease,
opacity 160ms ease,
border-color 160ms ease,
background 160ms ease;
}
button:hover:not(:disabled) {
transform: translateY(-1px);
}
button:active:not(:disabled) {
transform: translateY(0);
}
button:disabled {
cursor: not-allowed;
opacity: 0.38;
}
.primary {
flex: 1;
padding: 10px 13px;
border-radius: 11px;
background: var(--accent);
color: #081421;
font-size: 12px;
font-weight: 850;
box-shadow: 0 8px 25px color-mix(in srgb, var(--accent) 16%, transparent);
}
.reset {
padding: 9px 13px;
border: 1px solid var(--line);
border-radius: 11px;
background: rgba(255, 255, 255, 0.04);
color: #c4d1df;
font-size: 11px;
}
.forced {
display: flex;
align-items: center;
gap: 6px;
margin-top: 9px;
color: #71849b;
font-size: 10px;
}
.forced span {
margin-right: 2px;
}
.forced button {
padding: 4px 8px;
border: 1px solid var(--line);
border-radius: 999px;
background: rgba(255, 255, 255, 0.035);
color: #c7d2df;
font-size: 10px;
}
.forced button[data-force="red"]:hover:not(:disabled) {
border-color: rgba(255, 96, 120, 0.55);
color: #ff9aaa;
}
.forced button[data-force="blue"]:hover:not(:disabled) {
border-color: rgba(80, 201, 244, 0.55);
color: #9bdef7;
}
@media (max-width: 720px) {
body {
overflow: auto;
}
.hero {
top: 20px;
left: 20px;
right: 20px;
}
.hero p {
max-width: 90%;
font-size: 12px;
}
.compare-note,
.interaction-hint {
display: none;
}
.panel {
position: relative;
bottom: auto;
left: auto;
width: calc(100% - 24px);
grid-template-columns: 1fr;
margin: 72vh auto 24px;
transform: none;
}
}
@media (prefers-reduced-motion: reduce) {
*,
*::before,
*::after {
scroll-behavior: auto !important;
transition-duration: 0.01ms !important;
animation-duration: 0.01ms !important;
}
}
</style>
</head>
<body>
<div id="scene" aria-label="Interactive 3D urn visualization"></div>
<header class="hero">
<div class="eyebrow">Interactive probability laboratory</div>
<h1>The urn remembers.<br />Sometimes it doesn’t.</h1>
<p>
Both urns begin with 5 red and 5 blue tokens. Draw once and compare how
replacement changes the conditional probabilities of the second draw.
</p>
</header>
<aside class="compare-note" aria-label="Starting composition">
<span>Same starting mix</span>
<strong>5 red + 5 blue</strong>
<small>Only the rule changes</small>
</aside>
<div class="interaction-hint">
Drag to orbit · Scroll to zoom · Click an urn to draw
</div>
<main class="panel">
<section class="card" id="card-without" aria-labelledby="title-without">
<div class="card-top">
<div>
<div class="kicker">Urn A</div>
<h2 id="title-without">Without replacement</h2>
</div>
<div class="mix">5 red<br />5 blue</div>
</div>
<p class="description">
The first token stays out, so the information from draw one changes
what remains.
</p>
<div class="outcome" aria-live="polite">
<span data-result="first">No token drawn yet</span>
<span class="second-result" data-result="second"></span>
</div>
<div class="probability-box">
<div class="probability-head">
<strong>Second-draw probabilities</strong>
<span class="condition" data-condition>No condition yet</span>
</div>
<div class="prob-row red">
<span class="prob-name">Red</span>
<div class="bar"><i data-bar="red"></i></div>
<strong class="prob-value" data-prob="red"></strong>
</div>
<div class="prob-row blue">
<span class="prob-name">Blue</span>
<div class="bar"><i data-bar="blue"></i></div>
<strong class="prob-value" data-prob="blue"></strong>
</div>
</div>
<p class="explanation" data-explanation></p>
<div class="actions">
<button class="primary" data-action>Draw first token</button>
<button class="reset" data-reset>Reset</button>
</div>
<div class="forced">
<span>Force first outcome:</span>
<button data-force="red">Red</button>
<button data-force="blue">Blue</button>
</div>
</section>
<section class="card with" id="card-with" aria-labelledby="title-with">
<div class="card-top">
<div>
<div class="kicker">Urn B</div>
<h2 id="title-with">With replacement</h2>
</div>
<div class="mix">5 red<br />5 blue</div>
</div>
<p class="description">
The first token goes back, so the urn returns to exactly 5 red and
5 blue.
</p>
<div class="outcome" aria-live="polite">
<span data-result="first">No token drawn yet</span>
<span class="second-result" data-result="second"></span>
</div>
<div class="probability-box">
<div class="probability-head">
<strong>Second-draw probabilities</strong>
<span class="condition" data-condition>No condition yet</span>
</div>
<div class="prob-row red">
<span class="prob-name">Red</span>
<div class="bar"><i data-bar="red"></i></div>
<strong class="prob-value" data-prob="red"></strong>
</div>
<div class="prob-row blue">
<span class="prob-name">Blue</span>
<div class="bar"><i data-bar="blue"></i></div>
<strong class="prob-value" data-prob="blue"></strong>
</div>
</div>
<p class="explanation" data-explanation></p>
<div class="actions">
<button class="primary" data-action>Draw first token</button>
<button class="reset" data-reset>Reset</button>
</div>
<div class="forced">
<span>Force first outcome:</span>
<button data-force="red">Red</button>
<button data-force="blue">Blue</button>
</div>
</section>
</main>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
const RED = 0xff6078;
const BLUE = 0x50c9f4;
const RED_CSS = "#ff6078";
const BLUE_CSS = "#50c9f4";
const sceneHost = document.querySelector("#scene");
const renderer = new THREE.WebGLRenderer({
antialias: true,
alpha: 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",
"Two interactive glass urns containing red and blue tokens"
);
sceneHost.appendChild(renderer.domElement);
const scene = new THREE.Scene();
scene.fog = new THREE.FogExp2(0x07111f, 0.035);
const camera = new THREE.PerspectiveCamera(
38,
window.innerWidth / window.innerHeight,
0.1,
100
);
const orbit = {
yaw: 0,
pitch: 0.14,
distance: 10.6,
target: new THREE.Vector3(0, 1.35, 0)
};
function updateCamera() {
const horizontal = Math.cos(orbit.pitch) * orbit.distance;
camera.position.set(
orbit.target.x + Math.sin(orbit.yaw) * horizontal,
orbit.target.y + Math.sin(orbit.pitch) * orbit.distance,
orbit.target.z + Math.cos(orbit.yaw) * horizontal
);
camera.lookAt(orbit.target);
}
updateCamera();
const ambient = new THREE.HemisphereLight(0xccecff, 0x102039, 2.25);
scene.add(ambient);
const keyLight = new THREE.DirectionalLight(0xffffff, 3.1);
keyLight.position.set(-4, 8, 6);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(1024, 1024);
keyLight.shadow.camera.left = -7;
keyLight.shadow.camera.right = 7;
keyLight.shadow.camera.top = 7;
keyLight.shadow.camera.bottom = -7;
scene.add(keyLight);
const blueRim = new THREE.DirectionalLight(0x4cc9f0, 2.4);
blueRim.position.set(6, 3, -4);
scene.add(blueRim);
const warmRim = new THREE.DirectionalLight(0xff8d78, 1.8);
warmRim.position.set(-6, 2, -3);
scene.add(warmRim);
const floor = new THREE.Mesh(
new THREE.PlaneGeometry(30, 30),
new THREE.MeshStandardMaterial({
color: 0x0a1728,
roughness: 0.83,
metalness: 0.12
})
);
floor.rotation.x = -Math.PI / 2;
floor.position.y = -0.29;
floor.receiveShadow = true;
scene.add(floor);
const grid = new THREE.GridHelper(22, 44, 0x35516d, 0x20364d);
grid.position.y = -0.275;
grid.material.transparent = true;
grid.material.opacity = 0.18;
scene.add(grid);
const starGeometry = new THREE.BufferGeometry();
const starPositions = [];
for (let i = 0; i < 110; i++) {
starPositions.push(
(Math.random() - 0.5) * 22,
Math.random() * 8 - 0.1,
(Math.random() - 0.5) * 14 - 2
);
}
starGeometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(starPositions, 3)
);
const stars = new THREE.Points(
starGeometry,
new THREE.PointsMaterial({
color: 0x8fc8ee,
size: 0.025,
transparent: true,
opacity: 0.42,
depthWrite: false
})
);
scene.add(stars);
const interactiveMeshes = [];
const urns = [];
const tweens = [];
function tween(duration, update, easing = easeInOutCubic) {
return new Promise((resolve) => {
tweens.push({
start: performance.now(),
duration,
update,
easing,
resolve
});
});
}
function easeInOutCubic(t) {
return t < 0.5
? 4 * t * t * t
: 1 - Math.pow(-2 * t + 2, 3) / 2;
}
function wait(milliseconds) {
return new Promise((resolve) => setTimeout(resolve, milliseconds));
}
function makeLabel(text, accent) {
const canvas = document.createElement("canvas");
canvas.width = 768;
canvas.height = 128;
const context = canvas.getContext("2d");
context.clearRect(0, 0, canvas.width, canvas.height);
context.fillStyle = "rgba(6, 17, 31, 0.86)";
context.strokeStyle = accent;
context.lineWidth = 3;
context.beginPath();
context.roundRect(8, 8, 752, 112, 28);
context.fill();
context.stroke();
context.fillStyle = accent;
context.font = "800 42px system-ui, sans-serif";
context.textAlign = "center";
context.textBaseline = "middle";
context.fillText(text, 384, 65);
const texture = new THREE.CanvasTexture(canvas);
texture.colorSpace = THREE.SRGBColorSpace;
const sprite = new THREE.Sprite(
new THREE.SpriteMaterial({
map: texture,
transparent: true,
depthWrite: false
})
);
sprite.scale.set(2.75, 0.46, 1);
return sprite;
}
function makeToken(color) {
const material = new THREE.MeshStandardMaterial({
color,
roughness: 0.28,
metalness: 0.14,
emissive: color,
emissiveIntensity: 0.13
});
const mesh = new THREE.Mesh(
new THREE.SphereGeometry(0.135, 24, 16),
material
);
mesh.castShadow = true;
mesh.receiveShadow = true;
return mesh;
}
function makeMarker(color) {
const material = new THREE.MeshStandardMaterial({
color,
roughness: 0.18,
metalness: 0.12,
emissive: color,
emissiveIntensity: 0.65
});
const mesh = new THREE.Mesh(
new THREE.SphereGeometry(0.17, 28, 20),
material
);
mesh.visible = false;
return mesh;
}
function createUrn(mode, x, accentColor) {
const group = new THREE.Group();
group.position.x = x;
group.userData.urnId = mode;
scene.add(group);
const profile = [
new THREE.Vector2(0.0, 0.0),
new THREE.Vector2(0.52, 0.0),
new THREE.Vector2(0.82, 0.08),
new THREE.Vector2(1.0, 0.36),
new THREE.Vector2(1.03, 0.63),
new THREE.Vector2(0.9, 0.96),
new THREE.Vector2(0.7, 1.3),
new THREE.Vector2(0.6, 1.59),
new THREE.Vector2(0.61, 1.72),
new THREE.Vector2(0.73, 1.82)
];
const glassMaterial = new THREE.MeshPhysicalMaterial({
color: 0xb8ddff,
transparent: true,
opacity: 0.19,
roughness: 0.12,
metalness: 0.02,
side: THREE.DoubleSide,
depthWrite: false,
clearcoat: 0.7,
clearcoatRoughness: 0.2
});
const shell = new THREE.Mesh(
new THREE.LatheGeometry(profile, 72),
glassMaterial
);
shell.castShadow = true;
shell.renderOrder = 4;
group.add(shell);
interactiveMeshes.push(shell);
const accentMaterial = new THREE.MeshStandardMaterial({
color: accentColor,
roughness: 0.27,
metalness: 0.58,
emissive: accentColor,
emissiveIntensity: 0.08
});
const base = new THREE.Mesh(
new THREE.CylinderGeometry(0.69, 0.76, 0.13, 48),
new THREE.MeshStandardMaterial({
color: 0x172a40,
roughness: 0.4,
metalness: 0.45
})
);
base.position.y = -0.015;
base.castShadow = true;
base.receiveShadow = true;
group.add(base);
interactiveMeshes.push(base);
const rings = [
{ radius: 0.735, tube: 0.065, y: 1.82 },
{ radius: 0.605, tube: 0.043, y: 1.68 },
{ radius: 0.91, tube: 0.025, y: 0.72 }
];
for (const ringData of rings) {
const ring = new THREE.Mesh(
new THREE.TorusGeometry(
ringData.radius,
ringData.tube,
14,
64
),
accentMaterial
);
ring.rotation.x = Math.PI / 2;
ring.position.y = ringData.y;
ring.castShadow = true;
group.add(ring);
interactiveMeshes.push(ring);
}
for (const side of [-1, 1]) {
const handle = new THREE.Mesh(
new THREE.TorusGeometry(0.31, 0.055, 12, 48),
accentMaterial
);
handle.position.set(side * 0.94, 1.02, 0.08);
handle.rotation.y = side * 0.28;
handle.scale.y = 1.16;
handle.castShadow = true;
group.add(handle);
interactiveMeshes.push(handle);
}
const tokenPositions = [
[-0.43, 0.34, -0.2],
[-0.02, 0.34, -0.25],
[0.4, 0.34, -0.17],
[-0.23, 0.35, 0.16],
[0.22, 0.35, 0.17],
[-0.36, 0.67, -0.08],
[0.02, 0.67, -0.11],
[0.4, 0.67, -0.03],
[-0.12, 0.68, 0.19],
[0.29, 0.68, 0.2]
];
const colors = [
RED, RED, RED, RED, RED,
BLUE, BLUE, BLUE, BLUE, BLUE
];
for (let i = colors.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[colors[i], colors[j]] = [colors[j], colors[i]];
}
const tokens = tokenPositions.map((position, index) => {
const mesh = makeToken(colors[index]);
mesh.position.set(...position);
mesh.rotation.set(
Math.random() * Math.PI,
Math.random() * Math.PI,
Math.random() * Math.PI
);
group.add(mesh);
return {
mesh,
color: colors[index],
colorName: colors[index] === RED ? "red" : "blue",
home: mesh.position.clone(),
removed: false
};
});
const firstReveal = new THREE.Vector3(-0.66, 2.25, 0.34);
const secondReveal = new THREE.Vector3(0.66, 2.25, 0.34);
const firstMarker = makeMarker(RED);
firstMarker.position.copy(firstReveal);
group.add(firstMarker);
const secondMarker = makeMarker(RED);
secondMarker.position.copy(secondReveal);
group.add(secondMarker);
const label = makeLabel(
mode === "without" ? "NO REPLACEMENT" : "REPLACEMENT",
mode === "without" ? "#ffad66" : "#58e0c2"
);
label.position.set(0, 2.78, -0.12);
group.add(label);
const stageRing = new THREE.Mesh(
new THREE.RingGeometry(1.23, 1.29, 80),
new THREE.MeshBasicMaterial({
color: accentColor,
transparent: true,
opacity: 0.24,
side: THREE.DoubleSide,
depthWrite: false
})
);
stageRing.rotation.x = -Math.PI / 2;
stageRing.position.y = -0.265;
group.add(stageRing);
const glow = new THREE.PointLight(accentColor, 1.2, 4, 2);
glow.position.set(0, 1.45, 1.2);
group.add(glow);
const urn = {
mode,
group,
tokens,
firstReveal,
secondReveal,
firstMarker,
secondMarker,
first: null,
second: null,
firstMesh: null,
secondMesh: null,
busy: false
};
urns.push(urn);
return urn;
}
createUrn("without", -2.18, 0xffad66);
createUrn("with", 2.18, 0x58e0c2);
const cardElements = new Map();
for (const urn of urns) {
const root = document.querySelector(`#card-${urn.mode}`);
const refs = {
root,
first: root.querySelector('[data-result="first"]'),
second: root.querySelector('[data-result="second"]'),
condition: root.querySelector("[data-condition]"),
explanation: root.querySelector("[data-explanation]"),
action: root.querySelector("[data-action]"),
reset: root.querySelector("[data-reset]"),
red: root.querySelector('[data-prob="red"]'),
blue: root.querySelector('[data-prob="blue"]'),
redBar: root.querySelector('[data-bar="red"]'),
blueBar: root.querySelector('[data-bar="blue"]'),
forces: [...root.querySelectorAll("[data-force]")]
};
cardElements.set(urn.mode, refs);
refs.action.addEventListener("click", () => {
if (!urn.first) {
drawFirst(urn);
} else if (!urn.second) {
drawSecond(urn);
}
});
refs.reset.addEventListener("click", () => resetUrn(urn));
for (const forceButton of refs.forces) {
forceButton.addEventListener("click", () => {
if (!urn.first && !urn.busy) {
drawFirst(urn, forceButton.dataset.force);
}
});
}
}
function formatColor(colorName) {
return colorName === "red" ? "Red" : "Blue";
}
function colorCSS(colorName) {
return colorName === "red" ? RED_CSS : BLUE_CSS;
}
function updateCard(urn) {
const refs = cardElements.get(urn.mode);
if (!urn.first) {
refs.first.textContent = "No token drawn yet";
refs.second.innerHTML = "";
refs.condition.textContent = "No condition yet";
refs.red.innerHTML = "<b>1/2</b><small>50%</small>";
refs.blue.innerHTML = "<b>1/2</b><small>50%</small>";
refs.redBar.style.width = "50%";
refs.blueBar.style.width = "50%";
refs.explanation.textContent =
"Draw once to reveal the conditional odds for draw two.";
refs.action.textContent = "Draw first token";
} else {
const firstName = formatColor(urn.first);
refs.first.innerHTML =
`<span class="token-dot ${urn.first}"></span>` +
`First token: <strong>${firstName}</strong>`;
refs.condition.textContent = `Given first: ${firstName}`;
if (urn.mode === "with") {
refs.red.innerHTML = "<b>1/2</b><small>50%</small>";
refs.blue.innerHTML = "<b>1/2</b><small>50%</small>";
refs.redBar.style.width = "50%";
refs.blueBar.style.width = "50%";
refs.explanation.textContent =
"The token was returned, so the urn is still 5 red + 5 blue. " +
"The first color gives no information about the second draw.";
} else if (urn.first === "red") {
refs.red.innerHTML = "<b>4/9</b><small>44.4%</small>";
refs.blue.innerHTML = "<b>5/9</b><small>55.6%</small>";
refs.redBar.style.width = "44.4%";
refs.blueBar.style.width = "55.6%";
refs.explanation.textContent =
"One red token stayed out. The remaining urn has 4 red and " +
"5 blue, so blue is now more likely on draw two.";
} else {
refs.red.innerHTML = "<b>5/9</b><small>55.6%</small>";
refs.blue.innerHTML = "<b>4/9</b><small>44.4%</small>";
refs.redBar.style.width = "55.6%";
refs.blueBar.style.width = "44.4%";
refs.explanation.textContent =
"One blue token stayed out. The remaining urn has 5 red and " +
"4 blue, so red is now more likely on draw two.";
}
refs.action.textContent = urn.second
? "Both tokens drawn"
: "Draw second token";
}
if (urn.second) {
refs.second.innerHTML =
`<span class="token-dot ${urn.second}"></span>` +
`Second: <strong>${formatColor(urn.second)}</strong>`;
}
refs.action.disabled =
urn.busy || Boolean(urn.first && urn.second);
refs.reset.disabled = urn.busy;
for (const button of refs.forces) {
button.disabled = urn.busy || Boolean(urn.first);
}
refs.root.classList.remove("reveal");
void refs.root.offsetWidth;
refs.root.classList.add("reveal");
}
function availableTokens(urn) {
if (urn.mode === "without") {
return urn.tokens.filter((token) => !token.removed);
}
return urn.tokens;
}
function chooseToken(urn, forcedColor = null) {
let candidates = availableTokens(urn);
if (forcedColor) {
candidates = candidates.filter(
(token) => token.colorName === forcedColor
);
}
return candidates[
Math.floor(Math.random() * candidates.length)
];
}
async function moveAlongArc(mesh, destination, duration, lift = 0.8) {
const origin = mesh.position.clone();
await tween(duration, (progress) => {
const eased = easeInOutCubic(progress);
mesh.position.lerpVectors(origin, destination, eased);
mesh.position.y += Math.sin(Math.PI * eased) * lift;
mesh.rotation.x += 0.035;
mesh.rotation.y += 0.055;
});
}
async function drawFirst(urn, forcedColor = null) {
if (urn.busy || urn.first) return;
urn.busy = true;
const refs = cardElements.get(urn.mode);
refs.action.textContent = "Drawing…";
refs.action.disabled = true;
refs.reset.disabled = true;
for (const button of refs.forces) {
button.disabled = true;
}
const token = chooseToken(urn, forcedColor);
const destination = urn.firstReveal.clone();
await moveAlongArc(token.mesh, destination, 820, 0.82);
urn.first = token.colorName;
urn.firstMesh = token.mesh;
if (urn.mode === "without") {
token.removed = true;
} else {
urn.firstMarker.material.color.set(token.color);
urn.firstMarker.material.emissive.set(token.color);
urn.firstMarker.visible = true;
}
updateCard(urn);
if (urn.mode === "with") {
await wait(520);
await moveAlongArc(
token.mesh,
token.home.clone(),
680,
0.55
);
}
urn.busy = false;
updateCard(urn);
}
async function drawSecond(urn) {
if (urn.busy || !urn.first || urn.second) return;
urn.busy = true;
const refs = cardElements.get(urn.mode);
refs.action.textContent = "Drawing…";
refs.action.disabled = true;
refs.reset.disabled = true;
const token = chooseToken(urn);
const destination = urn.secondReveal.clone();
await moveAlongArc(token.mesh, destination, 820, 0.82);
urn.second = token.colorName;
urn.secondMesh = token.mesh;
if (urn.mode === "without") {
token.removed = true;
} else {
urn.secondMarker.material.color.set(token.color);
urn.secondMarker.material.emissive.set(token.color);
urn.secondMarker.visible = true;
}
updateCard(urn);
if (urn.mode === "with") {
await wait(520);
await moveAlongArc(
token.mesh,
token.home.clone(),
680,
0.55
);
}
urn.busy = false;
updateCard(urn);
}
function resetUrn(urn) {
if (urn.busy) return;
urn.first = null;
urn.second = null;
urn.firstMesh = null;
urn.secondMesh = null;
for (const token of urn.tokens) {
token.removed = false;
token.mesh.visible = true;
token.mesh.position.copy(token.home);
token.mesh.scale.setScalar(1);
}
urn.firstMarker.visible = false;
urn.secondMarker.visible = false;
updateCard(urn);
}
for (const urn of urns) {
updateCard(urn);
}
// Direct camera controls: no examples/addons are used.
const pointer = new THREE.Vector2();
const raycaster = new THREE.Raycaster();
let pointerDown = false;
let pointerMoved = false;
let previousX = 0;
let previousY = 0;
renderer.domElement.addEventListener("pointerdown", (event) => {
pointerDown = true;
pointerMoved = false;
previousX = event.clientX;
previousY = event.clientY;
renderer.domElement.setPointerCapture(event.pointerId);
renderer.domElement.classList.add("dragging");
});
renderer.domElement.addEventListener("pointermove", (event) => {
if (!pointerDown) return;
const deltaX = event.clientX - previousX;
const deltaY = event.clientY - previousY;
if (Math.abs(deltaX) + Math.abs(deltaY) > 5) {
pointerMoved = true;
}
orbit.yaw -= deltaX * 0.0055;
orbit.pitch = THREE.MathUtils.clamp(
orbit.pitch + deltaY * 0.004,
-0.02,
0.46
);
previousX = event.clientX;
previousY = event.clientY;
});
renderer.domElement.addEventListener("pointerup", (event) => {
pointerDown = false;
renderer.domElement.classList.remove("dragging");
if (pointerMoved) return;
const rect = renderer.domElement.getBoundingClientRect();
pointer.x =
((event.clientX - rect.left) / rect.width) * 2 - 1;
pointer.y =
-((event.clientY - rect.top) / rect.height) * 2 + 1;
raycaster.setFromCamera(pointer, camera);
const hits = raycaster.intersectObjects(interactiveMeshes, false);
if (hits.length === 0) return;
let object = hits[0].object;
while (object && !object.userData.urnId) {
object = object.parent;
}
if (!object) return;
const urn = urns.find((item) => item.mode === object.userData.urnId);
if (!urn) return;
if (!urn.first) {
drawFirst(urn);
} else if (!urn.second) {
drawSecond(urn);
}
});
renderer.domElement.addEventListener("pointercancel", () => {
pointerDown = false;
renderer.domElement.classList.remove("dragging");
});
renderer.domElement.addEventListener(
"wheel",
(event) => {
event.preventDefault();
orbit.distance = THREE.MathUtils.clamp(
orbit.distance + event.deltaY * 0.008,
7.6,
13.5
);
},
{ passive: false }
);
window.addEventListener("resize", () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
});
function animate(now) {
requestAnimationFrame(animate);
for (let i = tweens.length - 1; i >= 0; i--) {
const current = tweens[i];
const rawProgress = Math.min(
1,
(now - current.start) / current.duration
);
current.update(current.easing(rawProgress));
if (rawProgress >= 1) {
current.resolve();
tweens.splice(i, 1);
}
}
for (const urn of urns) {
for (const token of urn.tokens) {
if (!token.removed && token.mesh.position.y < 1.1) {
token.mesh.rotation.y += 0.0025;
}
}
}
stars.rotation.y += 0.00008;
updateCamera();
renderer.render(scene, camera);
}
requestAnimationFrame(animate);
</script>
</body>
</html>
```