Prompt specification
Build or customize the Three.js 3D scene "Replacement Rule Counterfactual" 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: Replacement Rule Counterfactual
Category: Games & Play · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/replacement-rule-counterfactual-x3532
Scene Description Prompt:
Create a standalone low-poly scene in one HTML file using Three.js from cdnjs. Run the same toy expedition under drawing with and without replacement. Use seeded sequences and explain the rule difference without treating simulated frequencies as exact probabilities.
Working Three.js Starter (replacement-rule-counterfactual-x3532):
```html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Seeded Trail Expedition</title>
<style>
:root {
--ink: #24333a;
--muted: #627078;
--paper: rgba(255, 252, 244, 0.94);
--line: rgba(36, 51, 58, 0.14);
--teal: #248f83;
--coral: #df7053;
--gold: #e8bd59;
}
* { box-sizing: border-box; }
html, body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #c9e1df;
color: var(--ink);
font-family: Inter, ui-sans-serif, system-ui, -apple-system,
BlinkMacSystemFont, "Segoe UI", sans-serif;
}
#scene {
position: fixed;
inset: 0;
width: 100%;
height: 100%;
display: block;
touch-action: none;
cursor: grab;
}
#scene:active { cursor: grabbing; }
.hud {
position: fixed;
inset: 0;
pointer-events: none;
}
.panel {
pointer-events: auto;
background: var(--paper);
border: 1px solid rgba(255, 255, 255, 0.75);
border-radius: 18px;
box-shadow:
0 16px 45px rgba(39, 62, 63, 0.16),
0 2px 8px rgba(39, 62, 63, 0.08);
backdrop-filter: blur(12px);
}
.setup {
position: absolute;
top: 18px;
left: 18px;
width: min(390px, calc(100vw - 36px));
padding: 19px;
}
.eyebrow {
display: flex;
align-items: center;
gap: 8px;
margin-bottom: 7px;
color: var(--teal);
font-size: 11px;
font-weight: 850;
letter-spacing: 0.16em;
text-transform: uppercase;
}
.eyebrow::before {
content: "";
width: 8px;
height: 8px;
border-radius: 50%;
background: var(--coral);
box-shadow: 0 0 0 4px rgba(223, 112, 83, 0.14);
}
h1 {
margin: 0;
font-size: clamp(25px, 3vw, 34px);
line-height: 1.03;
letter-spacing: -0.045em;
}
.intro {
margin: 10px 0 16px;
color: var(--muted);
font-size: 13px;
line-height: 1.5;
}
.controls {
display: grid;
grid-template-columns: 1fr 112px;
gap: 10px;
margin-bottom: 12px;
}
label {
display: grid;
gap: 5px;
color: var(--muted);
font-size: 11px;
font-weight: 750;
letter-spacing: 0.04em;
text-transform: uppercase;
}
input, select, button {
width: 100%;
min-height: 40px;
border: 1px solid var(--line);
border-radius: 10px;
font: inherit;
}
input, select {
padding: 0 11px;
background: rgba(255, 255, 255, 0.78);
color: var(--ink);
outline: none;
}
input:focus, select:focus {
border-color: var(--teal);
box-shadow: 0 0 0 3px rgba(36, 143, 131, 0.12);
}
.buttons {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 8px;
}
button {
padding: 9px 11px;
cursor: pointer;
font-size: 12px;
font-weight: 800;
transition: transform 120ms ease, opacity 120ms ease,
background 120ms ease;
}
button:hover:not(:disabled) { transform: translateY(-1px); }
button:active:not(:disabled) { transform: translateY(0); }
button:disabled { opacity: 0.48; cursor: wait; }
button.primary {
border-color: var(--teal);
background: var(--teal);
color: white;
}
button.secondary {
background: rgba(36, 143, 131, 0.08);
color: var(--teal);
}
button.ghost {
background: transparent;
color: var(--muted);
}
#reset { margin-top: 8px; }
.status {
min-height: 34px;
margin-top: 11px;
padding: 9px 11px;
border-radius: 10px;
background: rgba(36, 143, 131, 0.08);
color: var(--muted);
font-size: 12px;
line-height: 1.35;
}
.tickets {
margin-top: 11px;
padding-top: 10px;
border-top: 1px solid var(--line);
}
.tickets-label {
display: block;
margin-bottom: 7px;
color: var(--muted);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.08em;
text-transform: uppercase;
}
#ticketList {
display: flex;
flex-wrap: wrap;
gap: 4px;
min-height: 24px;
}
.ticket {
padding: 4px 6px;
border: 1px solid var(--line);
border-radius: 6px;
background: rgba(255,255,255,.7);
color: var(--muted);
font: 10px ui-monospace, SFMono-Regular, Menlo, monospace;
}
.results {
position: absolute;
top: 18px;
right: 18px;
width: min(365px, calc(100vw - 36px));
padding: 17px;
}
.result + .result {
margin-top: 12px;
padding-top: 14px;
border-top: 1px solid var(--line);
}
.result-heading {
display: flex;
align-items: center;
justify-content: space-between;
gap: 12px;
}
.result h2 {
margin: 0;
font-size: 16px;
letter-spacing: -0.025em;
}
.rule-badge {
flex: none;
padding: 4px 7px;
border-radius: 999px;
font-size: 9px;
font-weight: 850;
letter-spacing: 0.07em;
text-transform: uppercase;
}
.with .rule-badge {
background: rgba(36, 143, 131, 0.12);
color: var(--teal);
}
.without .rule-badge {
background: rgba(223, 112, 83, 0.12);
color: var(--coral);
}
.rule {
margin: 7px 0 10px;
color: var(--muted);
font-size: 12px;
line-height: 1.45;
}
.sequence {
display: flex;
flex-wrap: wrap;
gap: 5px;
min-height: 30px;
}
.chip {
display: inline-grid;
place-items: center;
width: 28px;
height: 28px;
border: 2px solid rgba(255,255,255,.82);
border-radius: 9px;
color: #173138;
font: 800 11px ui-monospace, SFMono-Regular, Menlo, monospace;
box-shadow: 0 2px 5px rgba(36,51,58,.13);
animation: pop 180ms ease-out;
}
.c0 { background: #55b8aa; }
.c1 { background: #edc86e; }
.c2 { background: #e9856c; }
@keyframes pop {
from { transform: scale(.65); opacity: 0; }
to { transform: scale(1); opacity: 1; }
}
.tally {
margin-top: 9px;
color: var(--ink);
font-size: 11px;
line-height: 1.4;
}
.tally strong { font-weight: 850; }
.note {
margin-top: 14px;
padding: 11px 12px;
border-radius: 11px;
background: rgba(232, 189, 89, 0.15);
color: #5d5542;
font-size: 11px;
line-height: 1.45;
}
.note strong { color: #453f31; }
.help {
position: absolute;
bottom: 15px;
left: 50%;
transform: translateX(-50%);
padding: 8px 12px;
border-radius: 999px;
background: rgba(255,255,255,.72);
color: rgba(36,51,58,.7);
font-size: 11px;
pointer-events: none;
white-space: nowrap;
}
@media (max-width: 760px) {
.setup {
top: 10px;
left: 10px;
width: calc(100vw - 20px);
padding: 14px;
}
.intro { margin-bottom: 10px; }
.tickets { display: none; }
.results {
top: auto;
right: 10px;
bottom: 10px;
width: calc(100vw - 20px);
max-height: 42vh;
overflow: auto;
padding: 13px;
}
.help { display: none; }
h1 { font-size: 25px; }
}
</style>
</head>
<body>
<canvas id="scene" aria-label="Low-poly trailhead token-drawing scene"></canvas>
<main class="hud">
<section class="panel setup">
<div class="eyebrow">Paired field test</div>
<h1>Seeded Trail Expedition</h1>
<p class="intro">
Collect trail tokens from the same 12-token cache. Run the paired
expedition once with replacement and once without replacement.
</p>
<div class="controls">
<label>
Seed
<input id="seed" value="CAMP-42" maxlength="40"
spellcheck="false" aria-label="Random seed">
</label>
<label>
Draws
<select id="drawCount" aria-label="Number of draws">
<option value="4">4</option>
<option value="6">6</option>
<option value="8" selected>8</option>
<option value="10">10</option>
<option value="12">12</option>
</select>
</label>
</div>
<div class="buttons">
<button class="primary" id="runBoth">Run paired expedition</button>
<button class="secondary" id="runWith">With replacement</button>
<button class="secondary" id="runWithout">Without replacement</button>
</div>
<button class="ghost" id="reset">Reset the trailhead</button>
<div class="status" id="status">
Ready. The seed creates a repeatable pseudo-random ticket stream.
</div>
<div class="tickets">
<span class="tickets-label">Shared seeded draw tickets</span>
<div id="ticketList"><span class="ticket">Run to reveal</span></div>
</div>
</section>
<section class="panel results">
<article class="result with" id="result-with">
<div class="result-heading">
<h2>With replacement</h2>
<span class="rule-badge">Return token</span>
</div>
<p class="rule">
Each recorded token returns to the cache, so every draw begins with
all 12 tokens available.
</p>
<div class="sequence"></div>
<div class="tally">Observed tally: <strong>—</strong></div>
</article>
<article class="result without" id="result-without">
<div class="result-heading">
<h2>Without replacement</h2>
<span class="rule-badge">Keep token</span>
</div>
<p class="rule">
Each recorded token stays in the expedition basket, so the cache
shrinks and depleted colors cannot be drawn again.
</p>
<div class="sequence"></div>
<div class="tally">Observed tally: <strong>—</strong></div>
</article>
<div class="note">
<strong>Observed counts are not probability estimates.</strong>
They are tallies from this finite seeded simulation. Changing the
seed, draw count, or drawing rule can change the tally.
</div>
</section>
<div class="help">Drag the scene to orbit · Scroll to zoom</div>
</main>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// ---------------------------------------------------------------------
// Scene
// ---------------------------------------------------------------------
const canvas = document.querySelector("#scene");
const renderer = new THREE.WebGLRenderer({
canvas,
antialias: true,
alpha: false
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight, false);
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.08;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xc9e1df);
scene.fog = new THREE.Fog(0xc9e1df, 18, 34);
const camera = new THREE.PerspectiveCamera(
42,
window.innerWidth / window.innerHeight,
0.1,
70
);
const cameraState = {
target: new THREE.Vector3(0, 1.1, 0),
radius: 22,
theta: 0.18,
phi: 1.03
};
function updateCamera() {
const { target, radius, theta, phi } = cameraState;
camera.position.set(
target.x + radius * Math.sin(phi) * Math.sin(theta),
target.y + radius * Math.cos(phi),
target.z + radius * Math.sin(phi) * Math.cos(theta)
);
camera.lookAt(target);
}
updateCamera();
scene.add(new THREE.HemisphereLight(0xe9fbff, 0x7e745c, 2.0));
const sun = new THREE.DirectionalLight(0xfff0cc, 3.0);
sun.position.set(-9, 15, 8);
sun.castShadow = true;
sun.shadow.mapSize.set(2048, 2048);
sun.shadow.camera.left = -16;
sun.shadow.camera.right = 16;
sun.shadow.camera.top = 16;
sun.shadow.camera.bottom = -16;
sun.shadow.normalBias = 0.035;
scene.add(sun);
const colors = {
ground: 0x93aa78,
groundSide: 0x667653,
path: 0xc7ad82,
mountain: 0x6d8179,
mountainDark: 0x536963,
snow: 0xe9f0e6,
pine: 0x355e4d,
pineLight: 0x47765e,
wood: 0x8c6848,
teal: 0x55b8aa,
gold: 0xedc86e,
coral: 0xe9856c
};
const tokenColors = [colors.teal, colors.gold, colors.coral];
const tokenNames = ["teal", "gold", "coral"];
function mat(color, extra = {}) {
return new THREE.MeshStandardMaterial({
color,
roughness: 0.86,
metalness: 0,
flatShading: true,
...extra
});
}
function mesh(geometry, material, position, parent = scene) {
const object = new THREE.Mesh(geometry, material);
if (position) object.position.copy(position);
object.castShadow = true;
object.receiveShadow = true;
parent.add(object);
return object;
}
// Low-poly island.
mesh(
new THREE.CylinderGeometry(14.5, 15.4, 1.1, 12),
mat(colors.groundSide),
new THREE.Vector3(0, -0.65, 0)
);
mesh(
new THREE.CylinderGeometry(14.45, 14.45, 0.22, 12),
mat(colors.ground),
new THREE.Vector3(0, -0.04, 0)
);
// Winding path made from flat stepping stones.
for (let i = 0; i < 7; i++) {
const stone = mesh(
new THREE.CylinderGeometry(0.72, 0.82, 0.12, 6),
mat(colors.path),
new THREE.Vector3(
Math.sin(i * 0.8) * 0.42,
0.12,
5.6 - i * 1.55
)
);
stone.rotation.y = i * 0.73;
stone.scale.z = 0.75;
}
// Faceted mountains.
function mountain(x, z, radius, height, color) {
mesh(
new THREE.ConeGeometry(radius, height, 5),
mat(color),
new THREE.Vector3(x, height / 2 - 0.05, z)
);
mesh(
new THREE.ConeGeometry(radius * 0.39, height * 0.38, 5),
mat(colors.snow),
new THREE.Vector3(x, height * 0.81, z)
);
}
mountain(-10.5, -8.5, 4.0, 7.0, colors.mountainDark);
mountain(-5.4, -10.8, 4.8, 8.4, colors.mountain);
mountain(3.5, -11.2, 4.1, 6.8, colors.mountainDark);
mountain(9.6, -8.8, 4.4, 7.7, colors.mountain);
function tree(x, z, scale = 1) {
const group = new THREE.Group();
group.position.set(x, 0.1, z);
group.scale.setScalar(scale);
scene.add(group);
mesh(
new THREE.CylinderGeometry(0.13, 0.18, 1.4, 5),
mat(colors.wood),
new THREE.Vector3(0, 0.7, 0),
group
);
mesh(
new THREE.ConeGeometry(0.75, 1.7, 5),
mat(colors.pine),
new THREE.Vector3(0, 1.65, 0),
group
);
mesh(
new THREE.ConeGeometry(0.58, 1.4, 5),
mat(colors.pineLight),
new THREE.Vector3(0, 2.35, 0),
group
);
}
[
[-11, 0, 1.05], [-9, -3, 0.8], [-8.5, 3.3, 0.9],
[8.8, 2.8, 0.95], [10.8, -1.4, 1.15], [6.8, -4.8, 0.72],
[-5.7, 5.9, 0.7], [5.8, 5.5, 0.78]
].forEach(args => tree(...args));
// Tent.
const tent = new THREE.Group();
tent.position.set(0.2, 0.1, -5.2);
tent.rotation.y = Math.PI / 4;
scene.add(tent);
mesh(
new THREE.ConeGeometry(2.05, 2.7, 4),
mat(colors.coral),
new THREE.Vector3(0, 1.35, 0),
tent
);
const doorGeometry = new THREE.BufferGeometry();
doorGeometry.setAttribute(
"position",
new THREE.Float32BufferAttribute([
0, 0.1, 1.36,
-0.68, 0.1, 1.36,
0, 1.25, 1.36
], 3)
);
doorGeometry.computeVertexNormals();
const door = new THREE.Mesh(
doorGeometry,
new THREE.MeshBasicMaterial({
color: 0x344b48,
side: THREE.DoubleSide
})
);
tent.add(door);
// Flag.
mesh(
new THREE.CylinderGeometry(0.055, 0.07, 2.8, 6),
mat(0x725039),
new THREE.Vector3(2.0, 1.4, -4.5)
);
const flagShape = new THREE.BufferGeometry();
flagShape.setAttribute(
"position",
new THREE.Float32BufferAttribute([
2.04, 2.65, -4.5,
3.2, 2.35, -4.5,
2.04, 2.05, -4.5
], 3)
);
flagShape.computeVertexNormals();
mesh(flagShape, mat(colors.gold));
// ---------------------------------------------------------------------
// Labels and expedition stations
// ---------------------------------------------------------------------
function labelSprite(text, accent) {
const labelCanvas = document.createElement("canvas");
labelCanvas.width = 512;
labelCanvas.height = 128;
const ctx = labelCanvas.getContext("2d");
ctx.fillStyle = "rgba(255,252,244,0.96)";
ctx.fillRect(5, 5, 502, 118);
ctx.fillStyle = accent;
ctx.fillRect(5, 5, 18, 118);
ctx.strokeStyle = "rgba(36,51,58,0.18)";
ctx.strokeRect(5, 5, 502, 118);
ctx.fillStyle = "#24333a";
ctx.font = "800 37px system-ui, sans-serif";
ctx.textAlign = "center";
ctx.textBaseline = "middle";
ctx.fillText(text, 265, 66);
const texture = new THREE.CanvasTexture(labelCanvas);
texture.colorSpace = THREE.SRGBColorSpace;
const sprite = new THREE.Sprite(
new THREE.SpriteMaterial({
map: texture,
transparent: true,
depthTest: false
})
);
sprite.scale.set(3.6, 0.9, 1);
return sprite;
}
const tokenGeometry = new THREE.IcosahedronGeometry(0.29, 0);
const tokenMaterials = tokenColors.map(color => mat(color));
// Four of each token, deliberately interleaved.
const tokenItems = Array.from({ length: 12 }, (_, i) => ({
id: i,
colorIndex: i % 3,
color: tokenColors[i % 3]
}));
const stations = {};
function createStation(key, x, label, accent) {
const group = new THREE.Group();
group.position.set(x, 0, 0.7);
scene.add(group);
mesh(
new THREE.CylinderGeometry(3.25, 3.45, 0.16, 12),
mat(accent, { transparent: true, opacity: 0.22 }),
new THREE.Vector3(0, 0.13, 0),
group
);
// Source tray.
const trayY = 0.38;
const trayZ = -0.72;
mesh(
new THREE.BoxGeometry(4.65, 0.14, 2.18),
mat(0x6e5844),
new THREE.Vector3(0, trayY, trayZ),
group
);
mesh(
new THREE.BoxGeometry(4.65, 0.52, 0.12),
mat(0x8b6a4d),
new THREE.Vector3(0, trayY + 0.25, trayZ - 1.03),
group
);
mesh(
new THREE.BoxGeometry(0.12, 0.52, 2.18),
mat(0x8b6a4d),
new THREE.Vector3(-2.27, trayY + 0.25, trayZ),
group
);
mesh(
new THREE.BoxGeometry(0.12, 0.52, 2.18),
mat(0x8b6a4d),
new THREE.Vector3(2.27, trayY + 0.25, trayZ),
group
);
// Collection table.
mesh(
new THREE.BoxGeometry(5.35, 0.18, 2.18),
mat(0xa47a52),
new THREE.Vector3(0, 0.5, 1.65),
group
);
for (const lx of [-2.25, 2.25]) {
mesh(
new THREE.BoxGeometry(0.18, 0.62, 0.18),
mat(colors.wood),
new THREE.Vector3(lx, 0.16, 1.65),
group
);
}
const sourcePositions = [];
const meshes = [];
tokenItems.forEach((item, index) => {
const col = index % 4;
const row = Math.floor(index / 4);
const position = new THREE.Vector3(
-1.62 + col * 1.08,
0.78,
-1.22 + row * 0.62
);
const token = mesh(
tokenGeometry,
tokenMaterials[item.colorIndex],
position,
group
);
token.rotation.set(
index * 0.31,
index * 0.47,
index * 0.19
);
token.userData.baseRotation = token.rotation.clone();
sourcePositions.push(position.clone());
meshes.push(token);
});
const destinations = [];
for (let i = 0; i < 12; i++) {
const col = i % 6;
const row = Math.floor(i / 6);
destinations.push(new THREE.Vector3(
-2.12 + col * 0.845,
0.78,
1.20 + row * 0.82
));
}
const laneLabel = labelSprite(label, accent);
laneLabel.position.set(0, 2.6, -1.55);
group.add(laneLabel);
return {
key,
group,
meshes,
sourcePositions,
destinations
};
}
stations.with = createStation(
"with", -4.35, "RETURN EACH TOKEN", "#248f83"
);
stations.without = createStation(
"without", 4.35, "KEEP EACH TOKEN", "#df7053"
);
// ---------------------------------------------------------------------
// Seeded sequence generation
// ---------------------------------------------------------------------
function hashSeed(text) {
let hash = 2166136261;
for (let i = 0; i < text.length; i++) {
hash ^= text.charCodeAt(i);
hash = Math.imul(hash, 16777619);
}
return hash >>> 0;
}
function mulberry32(seed) {
return function () {
let value = seed += 0x6D2B79F5;
value = Math.imul(value ^ value >>> 15, value | 1);
value ^= value + Math.imul(value ^ value >>> 7, value | 61);
return ((value ^ value >>> 14) >>> 0) / 4294967296;
};
}
// One shared stream of pseudo-random draw tickets is applied to both
// rules. The remaining-token state differs between the rules.
function makeTickets(seed, count) {
const random = mulberry32(hashSeed(seed));
return Array.from({ length: count }, () => random());
}
function makePlans(tickets) {
const withPlan = [];
const withoutPlan = [];
let remaining = tokenItems.map((_, index) => index);
for (const ticket of tickets) {
// Replacement: the full fixed cache is available every time.
const fixedIndex = Math.min(
tokenItems.length - 1,
Math.floor(ticket * tokenItems.length)
);
withPlan.push(fixedIndex);
// No replacement: choose from the current remaining list and
// permanently remove that token from the cache.
const remainingIndex = Math.min(
remaining.length - 1,
Math.floor(ticket * remaining.length)
);
withoutPlan.push(remaining.splice(remainingIndex, 1)[0]);
}
return {
with: withPlan,
without: withoutPlan
};
}
// ---------------------------------------------------------------------
// Lightweight tween system
// ---------------------------------------------------------------------
const tweens = new Set();
function tween(duration, update) {
return new Promise(resolve => {
tweens.add({
start: performance.now(),
duration,
update,
resolve
});
});
}
function delay(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
// ---------------------------------------------------------------------
// UI state and expedition animation
// ---------------------------------------------------------------------
const seedInput = document.querySelector("#seed");
const countInput = document.querySelector("#drawCount");
const status = document.querySelector("#status");
const ticketList = document.querySelector("#ticketList");
const runButtons = [
document.querySelector("#runBoth"),
document.querySelector("#runWith"),
document.querySelector("#runWithout")
];
const resetButton = document.querySelector("#reset");
const resultUI = {
with: document.querySelector("#result-with"),
without: document.querySelector("#result-without")
};
const results = {
with: { counts: [0, 0, 0] },
without: { counts: [0, 0, 0] }
};
let running = false;
function clearResult(mode) {
results[mode].counts = [0, 0, 0];
resultUI[mode].querySelector(".sequence").replaceChildren();
resultUI[mode].querySelector(".tally").innerHTML =
"Observed tally: <strong>—</strong>";
}
function resetStation(mode) {
const station = stations[mode];
station.meshes.forEach((token, index) => {
token.position.copy(station.sourcePositions[index]);
token.rotation.copy(token.userData.baseRotation);
token.scale.setScalar(1);
});
}
function resetAll() {
clearResult("with");
clearResult("without");
resetStation("with");
resetStation("without");
ticketList.innerHTML = '<span class="ticket">Run to reveal</span>';
status.textContent =
"Ready. The seed creates a repeatable pseudo-random ticket stream.";
}
function recordDraw(mode, item, drawNumber) {
const result = results[mode];
result.counts[item.colorIndex]++;
const chip = document.createElement("span");
chip.className = `chip c${item.colorIndex}`;
chip.textContent = String(drawNumber);
chip.title = `Draw ${drawNumber}: ${tokenNames[item.colorIndex]} token`;
resultUI[mode].querySelector(".sequence").appendChild(chip);
const tally = result.counts
.map((count, index) => `${count} ${tokenNames[index]}`)
.join(" · ");
resultUI[mode].querySelector(".tally").innerHTML =
`Observed tally: <strong>${tally}</strong>`;
}
async function animateMode(mode, plan) {
const station = stations[mode];
for (let i = 0; i < plan.length; i++) {
const itemIndex = plan[i];
const item = tokenItems[itemIndex];
const token = station.meshes[itemIndex];
const start = token.position.clone();
const end = station.destinations[i].clone();
const startRotation = token.rotation.clone();
token.scale.setScalar(1.12);
await tween(610, t => {
const eased = t * t * (3 - 2 * t);
token.position.lerpVectors(start, end, eased);
token.position.y += Math.sin(Math.PI * t) * 1.75;
token.rotation.x = startRotation.x + t * 2.3;
token.rotation.y = startRotation.y + t * 1.7;
});
token.scale.setScalar(1);
recordDraw(mode, item, i + 1);
if (mode === "with") {
// Show the token briefly, then return it to the source cache.
await delay(260);
const returnStart = token.position.clone();
const returnRotation = token.rotation.clone();
await tween(500, t => {
const eased = t * t * (3 - 2 * t);
token.position.lerpVectors(
returnStart,
station.sourcePositions[itemIndex],
eased
);
token.position.y += Math.sin(Math.PI * t) * 1.25;
token.rotation.x = returnRotation.x + t * 1.5;
token.rotation.y = returnRotation.y + t * 1.1;
});
}
// Without replacement: leave the token on the collection table.
}
}
async function runExperiment(modes) {
if (running) return;
running = true;
runButtons.forEach(button => button.disabled = true);
resetButton.disabled = true;
clearResult("with");
clearResult("without");
resetStation("with");
resetStation("without");
const seed = seedInput.value.trim() || "CAMP-42";
const count = Number(countInput.value);
const tickets = makeTickets(seed, count);
const plans = makePlans(tickets);
ticketList.replaceChildren();
tickets.forEach(ticket => {
const element = document.createElement("span");
element.className = "ticket";
element.textContent = ticket.toFixed(3);
ticketList.appendChild(element);
});
const modeNames = modes
.map(mode => mode === "with" ? "with replacement" : "without replacement")
.join(" and ");
status.textContent = `Drawing ${count} tokens ${modeNames}…`;
await Promise.all(
modes.map(mode => animateMode(mode, plans[mode]))
);
status.textContent =
`${modes.length === 2 ? "Paired expedition" : "Expedition"} complete. ` +
`Same seed, ${count} draws, ${modeNames}.`;
running = false;
runButtons.forEach(button => button.disabled = false);
resetButton.disabled = false;
}
document.querySelector("#runBoth").addEventListener("click", () => {
runExperiment(["with", "without"]);
});
document.querySelector("#runWith").addEventListener("click", () => {
runExperiment(["with"]);
});
document.querySelector("#runWithout").addEventListener("click", () => {
runExperiment(["without"]);
});
resetButton.addEventListener("click", resetAll);
// ---------------------------------------------------------------------
// Direct camera controls: no addons
// ---------------------------------------------------------------------
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.addEventListener("pointermove", event => {
if (!dragging) return;
const dx = event.clientX - lastX;
const dy = event.clientY - lastY;
cameraState.theta -= dx * 0.006;
cameraState.phi = THREE.MathUtils.clamp(
cameraState.phi + dy * 0.005,
0.43,
1.32
);
lastX = event.clientX;
lastY = event.clientY;
updateCamera();
});
function stopDragging(event) {
dragging = false;
if (event && canvas.hasPointerCapture(event.pointerId)) {
canvas.releasePointerCapture(event.pointerId);
}
}
canvas.addEventListener("pointerup", stopDragging);
canvas.addEventListener("pointercancel", stopDragging);
canvas.addEventListener("wheel", event => {
event.preventDefault();
cameraState.radius = THREE.MathUtils.clamp(
cameraState.radius + event.deltaY * 0.012,
14,
31
);
updateCamera();
}, { 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, false);
});
// ---------------------------------------------------------------------
// Render loop
// ---------------------------------------------------------------------
function animate(now) {
requestAnimationFrame(animate);
for (const tween of Array.from(tweens)) {
const progress = Math.min(1, (now - tween.start) / tween.duration);
tween.update(progress);
if (progress >= 1) {
tweens.delete(tween);
tween.resolve();
}
}
renderer.render(scene, camera);
}
resetAll();
requestAnimationFrame(animate);
</script>
</body>
</html>
```