Prompt specification
Build or customize the Three.js 3D scene "Island colonist sample tray" 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: Island colonist sample tray
Category: Biology & Nature · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/island-colonist-sample-tray-x0901
Scene Description Prompt:
Make a standalone HTML scene with Three.js from cdnjs that depicts the founder effect. Build a source-population tray with multiple variant tokens beside a small boat carrying a random subset; show the subset before any reproduction occurs. Low-poly.
Working Three.js Starter (island-colonist-sample-tray-x0901):
```html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Founder Effect — Random Founder Group</title>
<style>
:root {
--ink: #263238;
--muted: #647278;
--paper: rgba(255, 255, 255, 0.9);
--line: rgba(38, 50, 56, 0.13);
--amber: #e3a13b;
--teal: #2a9d8f;
--violet: #7b61ff;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #e9ede5;
color: var(--ink);
font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
#scene {
position: fixed;
inset: 0;
}
#scene canvas {
display: block;
width: 100%;
height: 100%;
touch-action: none;
cursor: grab;
}
#scene canvas.dragging {
cursor: grabbing;
}
header {
position: fixed;
z-index: 5;
top: 24px;
left: 24px;
max-width: 490px;
pointer-events: none;
}
.eyebrow {
margin-bottom: 7px;
color: #718078;
font-size: 11px;
font-weight: 800;
letter-spacing: 0.16em;
text-transform: uppercase;
}
h1 {
margin: 0;
font-size: clamp(30px, 4vw, 52px);
font-weight: 760;
letter-spacing: -0.045em;
line-height: 0.98;
}
header p {
max-width: 430px;
margin: 12px 0 0;
color: var(--muted);
font-size: 14px;
line-height: 1.5;
}
#info-panel {
position: fixed;
z-index: 10;
top: 24px;
right: 24px;
width: 330px;
padding: 20px;
border: 1px solid var(--line);
border-radius: 20px;
background: var(--paper);
box-shadow: 0 18px 50px rgba(36, 48, 42, 0.12);
backdrop-filter: blur(14px);
}
.panel-heading {
display: flex;
align-items: center;
justify-content: space-between;
gap: 12px;
}
.panel-heading h2 {
margin: 0;
font-size: 17px;
letter-spacing: -0.02em;
}
.stage-badge {
display: inline-flex;
align-items: center;
gap: 6px;
padding: 7px 9px;
border-radius: 999px;
background: #fff4d9;
color: #8b5a00;
font-size: 9px;
font-weight: 850;
letter-spacing: 0.08em;
line-height: 1;
text-transform: uppercase;
white-space: nowrap;
}
.stage-badge::before {
width: 6px;
height: 6px;
border-radius: 50%;
background: #e3a13b;
content: "";
}
.sample-summary {
display: flex;
align-items: baseline;
gap: 7px;
margin: 18px 0 15px;
}
#sample-count {
font-size: 34px;
font-weight: 780;
letter-spacing: -0.05em;
}
.sample-summary span {
color: var(--muted);
font-size: 13px;
}
.section-label {
margin: 16px 0 9px;
color: #738078;
font-size: 10px;
font-weight: 800;
letter-spacing: 0.12em;
text-transform: uppercase;
}
.slider-row {
display: flex;
align-items: center;
gap: 12px;
}
input[type="range"] {
width: 100%;
accent-color: #384d45;
cursor: pointer;
}
#sample-size-value {
min-width: 38px;
color: #46564f;
font-size: 12px;
font-weight: 800;
text-align: right;
}
.variant {
display: grid;
grid-template-columns: 11px 1fr auto;
align-items: center;
gap: 9px;
margin-top: 12px;
font-size: 12px;
}
.swatch {
width: 10px;
height: 10px;
border-radius: 3px;
box-shadow: inset 0 0 0 1px rgba(0, 0, 0, 0.08);
}
.variant-name {
font-weight: 700;
}
.variant-values {
color: var(--muted);
font-variant-numeric: tabular-nums;
white-space: nowrap;
}
.variant-values strong {
color: var(--ink);
}
.meter {
position: relative;
grid-column: 2 / 4;
height: 6px;
margin-top: -3px;
overflow: hidden;
border-radius: 999px;
background: #e6ebe6;
}
.meter span {
position: absolute;
inset: 0 auto 0 0;
border-radius: inherit;
transition: width 400ms ease;
}
.meter .source-bar {
opacity: 0.2;
}
.meter .sample-bar {
height: 3px;
top: auto;
bottom: 0;
}
.key {
display: flex;
gap: 13px;
margin-top: 10px;
color: #7b8580;
font-size: 10px;
}
.key span {
display: inline-flex;
align-items: center;
gap: 5px;
}
.key i {
display: inline-block;
width: 14px;
height: 4px;
border-radius: 4px;
background: #c7cfca;
}
.key i.solid {
height: 3px;
background: #46564f;
}
#insight {
min-height: 55px;
margin: 16px 0;
padding: 12px 13px;
border-left: 3px solid #e3a13b;
border-radius: 0 9px 9px 0;
background: rgba(227, 161, 59, 0.08);
color: #5e665f;
font-size: 12px;
line-height: 1.48;
}
.buttons {
display: grid;
grid-template-columns: 1fr auto;
gap: 8px;
}
button {
min-height: 42px;
border: 0;
border-radius: 11px;
background: #344a42;
color: white;
font: inherit;
font-size: 12px;
font-weight: 780;
cursor: pointer;
transition: transform 120ms ease, background 120ms ease;
}
button:hover {
background: #263d35;
}
button:active {
transform: translateY(1px);
}
#reset-view {
width: 44px;
padding: 0;
background: #e8ece8;
color: #53625b;
font-size: 19px;
}
#reset-view:hover {
background: #dce3de;
}
.scene-label {
position: fixed;
z-index: 4;
padding: 8px 11px;
border: 1px solid rgba(38, 50, 56, 0.12);
border-radius: 10px;
background: rgba(255, 255, 255, 0.9);
box-shadow: 0 7px 22px rgba(42, 52, 46, 0.1);
color: #4f5e57;
font-size: 9px;
font-weight: 850;
letter-spacing: 0.08em;
line-height: 1.3;
pointer-events: none;
text-align: center;
text-transform: uppercase;
transform: translate(-50%, -100%);
white-space: nowrap;
}
.scene-label b {
display: block;
margin-top: 3px;
color: #25332d;
font-size: 11px;
letter-spacing: 0;
text-transform: none;
}
.scene-label::after {
position: absolute;
left: 50%;
bottom: -11px;
width: 1px;
height: 10px;
background: rgba(38, 50, 56, 0.28);
content: "";
}
#stage-note {
position: fixed;
z-index: 4;
left: 50%;
bottom: 22px;
padding: 10px 15px;
border: 1px solid var(--line);
border-radius: 999px;
background: rgba(255, 255, 255, 0.82);
color: #64716a;
font-size: 11px;
pointer-events: none;
transform: translateX(-50%);
white-space: nowrap;
}
#stage-note strong {
color: #34473f;
}
@media (max-width: 820px) {
header {
top: 16px;
left: 16px;
max-width: calc(100vw - 32px);
}
header p {
display: none;
}
#info-panel {
top: auto;
right: 12px;
bottom: 12px;
left: 12px;
width: auto;
padding: 15px;
border-radius: 16px;
}
.sample-summary {
margin: 12px 0;
}
#sample-count {
font-size: 26px;
}
.variant {
margin-top: 8px;
}
#insight {
min-height: 0;
margin: 12px 0;
padding: 9px 11px;
}
#stage-note {
display: none;
}
.scene-label {
font-size: 8px;
}
}
</style>
</head>
<body>
<div id="scene" aria-label="Interactive three-dimensional founder effect diagram"></div>
<header>
<div class="eyebrow">Population genetics · interactive model</div>
<h1>Founder effect</h1>
<p>
A chance sample can produce a founder group with a very different mix
of variants from the larger source population.
</p>
</header>
<aside id="info-panel">
<div class="panel-heading">
<h2>Founder sample</h2>
<div class="stage-badge">Before reproduction</div>
</div>
<div class="sample-summary">
<strong id="sample-count">5</strong>
<span>of 30 source individuals board the boat</span>
</div>
<div class="section-label">Founder group size</div>
<div class="slider-row">
<input
id="sample-size"
type="range"
min="3"
max="10"
value="5"
aria-label="Number of individuals in the founder sample"
>
<span id="sample-size-value">5</span>
</div>
<div class="section-label">Variant frequencies</div>
<div class="variant">
<span class="swatch" style="background: var(--amber)"></span>
<span class="variant-name">Amber</span>
<span class="variant-values">
<strong id="sample-amber">—</strong> / <span id="source-amber">—</span>
</span>
<div class="meter">
<span class="source-bar" style="background: var(--amber)"></span>
<span class="sample-bar" style="background: var(--amber)"></span>
</div>
</div>
<div class="variant">
<span class="swatch" style="background: var(--teal)"></span>
<span class="variant-name">Teal</span>
<span class="variant-values">
<strong id="sample-teal">—</strong> / <span id="source-teal">—</span>
</span>
<div class="meter">
<span class="source-bar" style="background: var(--teal)"></span>
<span class="sample-bar" style="background: var(--teal)"></span>
</div>
</div>
<div class="variant">
<span class="swatch" style="background: var(--violet)"></span>
<span class="variant-name">Violet</span>
<span class="variant-values">
<strong id="sample-violet">—</strong> / <span id="source-violet">—</span>
</span>
<div class="meter">
<span class="source-bar" style="background: var(--violet)"></span>
<span class="sample-bar" style="background: var(--violet)"></span>
</div>
</div>
<div class="key">
<span><i></i> source</span>
<span><i class="solid"></i> founder group</span>
</div>
<div id="insight"></div>
<div class="buttons">
<button id="draw-sample" type="button">Draw random founder group</button>
<button id="reset-view" type="button" title="Reset camera" aria-label="Reset camera">↺</button>
</div>
</aside>
<div id="source-label" class="scene-label">
Source population
<b>30 variant tokens</b>
</div>
<div id="boat-label" class="scene-label">
Random founder subset
<b>Pre-reproduction</b>
</div>
<div id="stage-note">
<strong>Only the sampled individuals move on.</strong> No offspring are shown.
</div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
const container = document.querySelector("#scene");
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xe9ede5);
scene.fog = new THREE.Fog(0xe9ede5, 15, 30);
const renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.08;
container.appendChild(renderer.domElement);
const camera = new THREE.PerspectiveCamera(
39,
window.innerWidth / window.innerHeight,
0.1,
60
);
// Camera controls are implemented directly, without Three.js addons.
const cameraState = {
target: new THREE.Vector3(-0.15, 0.18, 0),
radius: 10.5,
theta: 0.58,
phi: 0.88
};
function updateCamera() {
const { target, radius, theta, phi } = cameraState;
const sinPhi = Math.sin(phi);
camera.position.set(
target.x + radius * sinPhi * Math.sin(theta),
target.y + radius * Math.cos(phi),
target.z + radius * sinPhi * Math.cos(theta)
);
camera.lookAt(target);
}
function resetCamera() {
cameraState.radius = 10.5;
cameraState.theta = 0.58;
cameraState.phi = 0.88;
cameraState.target.set(-0.15, 0.18, 0);
updateCamera();
}
updateCamera();
let dragging = false;
let previousX = 0;
let previousY = 0;
renderer.domElement.addEventListener("pointerdown", (event) => {
dragging = true;
previousX = event.clientX;
previousY = event.clientY;
renderer.domElement.setPointerCapture(event.pointerId);
renderer.domElement.classList.add("dragging");
});
renderer.domElement.addEventListener("pointermove", (event) => {
if (!dragging) return;
const deltaX = event.clientX - previousX;
const deltaY = event.clientY - previousY;
previousX = event.clientX;
previousY = event.clientY;
cameraState.theta -= deltaX * 0.006;
cameraState.phi = THREE.MathUtils.clamp(
cameraState.phi - deltaY * 0.005,
0.42,
1.3
);
updateCamera();
});
function endDrag(event) {
dragging = false;
renderer.domElement.classList.remove("dragging");
if (event.pointerId !== undefined && renderer.domElement.hasPointerCapture(event.pointerId)) {
renderer.domElement.releasePointerCapture(event.pointerId);
}
}
renderer.domElement.addEventListener("pointerup", endDrag);
renderer.domElement.addEventListener("pointercancel", endDrag);
renderer.domElement.addEventListener(
"wheel",
(event) => {
event.preventDefault();
cameraState.radius = THREE.MathUtils.clamp(
cameraState.radius * Math.exp(event.deltaY * 0.001),
6.8,
15
);
updateCamera();
},
{ passive: false }
);
renderer.domElement.addEventListener("dblclick", resetCamera);
// Lighting
const hemisphere = new THREE.HemisphereLight(0xf7fbff, 0x9da98f, 2.15);
scene.add(hemisphere);
const sun = new THREE.DirectionalLight(0xfff4df, 3.15);
sun.position.set(5.5, 9, 5);
sun.castShadow = true;
sun.shadow.mapSize.set(2048, 2048);
sun.shadow.camera.left = -9;
sun.shadow.camera.right = 9;
sun.shadow.camera.top = 9;
sun.shadow.camera.bottom = -9;
sun.shadow.camera.near = 0.5;
sun.shadow.camera.far = 25;
sun.shadow.bias = -0.0004;
scene.add(sun);
const fill = new THREE.DirectionalLight(0xb8d7ff, 0.75);
fill.position.set(-6, 4, -5);
scene.add(fill);
// Ground and a restrained grid give the diagram a tabletop setting.
const ground = new THREE.Mesh(
new THREE.CircleGeometry(11.5, 28),
new THREE.MeshStandardMaterial({
color: 0xe9ede5,
roughness: 1,
metalness: 0,
flatShading: true
})
);
ground.rotation.x = -Math.PI / 2;
ground.position.y = -0.015;
ground.receiveShadow = true;
scene.add(ground);
const grid = new THREE.GridHelper(20, 32, 0xbac5ba, 0xcbd3ca);
grid.position.y = 0.002;
grid.material.transparent = true;
grid.material.opacity = 0.18;
scene.add(grid);
// Palette and source population
const variants = [
{ name: "Amber", color: 0xe3a13b },
{ name: "Teal", color: 0x2a9d8f },
{ name: "Violet", color: 0x7b61ff }
];
const sourceCounts = [17, 9, 4];
const sourcePopulation = 30;
const tokenGeometry = new THREE.CylinderGeometry(0.13, 0.15, 0.11, 8);
const tokenBaseGeometry = new THREE.CylinderGeometry(0.151, 0.151, 0.025, 8);
const tokenCapGeometry = new THREE.CylinderGeometry(0.095, 0.095, 0.014, 8);
const tokenMaterials = variants.map((variant) => {
const bodyColor = new THREE.Color(variant.color);
const capColor = bodyColor.clone().offsetHSL(0, 0, 0.1);
return {
body: new THREE.MeshStandardMaterial({
color: bodyColor,
roughness: 0.66,
metalness: 0.02,
flatShading: true
}),
base: new THREE.MeshStandardMaterial({
color: bodyColor.clone().multiplyScalar(0.7),
roughness: 0.8,
flatShading: true
}),
cap: new THREE.MeshStandardMaterial({
color: capColor,
roughness: 0.58,
flatShading: true
})
};
});
const highlightGeometry = new THREE.TorusGeometry(0.19, 0.021, 4, 18);
const highlightMaterial = new THREE.MeshBasicMaterial({
color: 0xffcf5c,
transparent: true,
opacity: 0.88,
depthWrite: false
});
function makeToken(variantIndex) {
const token = new THREE.Group();
const materials = tokenMaterials[variantIndex];
const base = new THREE.Mesh(tokenBaseGeometry, materials.base);
base.position.y = -0.062;
base.castShadow = true;
base.receiveShadow = true;
token.add(base);
const body = new THREE.Mesh(tokenGeometry, materials.body);
body.castShadow = true;
body.receiveShadow = true;
token.add(body);
const cap = new THREE.Mesh(tokenCapGeometry, materials.cap);
cap.position.y = 0.06;
cap.castShadow = true;
token.add(cap);
token.userData.variantIndex = variantIndex;
return token;
}
// Source tray
const tray = new THREE.Group();
tray.position.x = -2.4;
scene.add(tray);
const trayMaterial = new THREE.MeshStandardMaterial({
color: 0xcab995,
roughness: 0.92,
flatShading: true
});
const trayEdgeMaterial = new THREE.MeshStandardMaterial({
color: 0xb5a27d,
roughness: 0.95,
flatShading: true
});
const trayBase = new THREE.Mesh(
new THREE.BoxGeometry(4.25, 0.18, 2.45),
trayMaterial
);
trayBase.position.y = 0.09;
trayBase.castShadow = true;
trayBase.receiveShadow = true;
tray.add(trayBase);
const wallHeight = 0.32;
const longWallGeometry = new THREE.BoxGeometry(4.4, wallHeight, 0.13);
const shortWallGeometry = new THREE.BoxGeometry(0.13, wallHeight, 2.5);
for (const z of [-1.25, 1.25]) {
const wall = new THREE.Mesh(longWallGeometry, trayEdgeMaterial);
wall.position.set(0, 0.23, z);
wall.castShadow = true;
wall.receiveShadow = true;
tray.add(wall);
}
for (const x of [-2.125, 2.125]) {
const wall = new THREE.Mesh(shortWallGeometry, trayEdgeMaterial);
wall.position.set(x, 0.23, 0);
wall.castShadow = true;
wall.receiveShadow = true;
tray.add(wall);
}
// A shuffled sequence of variants ensures that spatial order does not
// affect the random sampling.
const variantSequence = [];
variants.forEach((variant, index) => {
for (let i = 0; i < sourceCounts[index]; i++) {
variantSequence.push(index);
}
});
for (let i = variantSequence.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[variantSequence[i], variantSequence[j]] = [variantSequence[j], variantSequence[i]];
}
const sourceTokens = [];
for (let row = 0; row < 5; row++) {
for (let column = 0; column < 6; column++) {
const index = row * 6 + column;
const token = makeToken(variantSequence[index]);
token.position.set(
(column - 2.5) * 0.58 + (Math.random() - 0.5) * 0.055,
0.245,
(row - 2) * 0.43 + (Math.random() - 0.5) * 0.045
);
token.rotation.y = Math.random() * Math.PI;
const highlight = new THREE.Mesh(highlightGeometry, highlightMaterial);
highlight.rotation.x = Math.PI / 2;
highlight.position.y = -0.065;
highlight.visible = false;
token.add(highlight);
token.userData.highlight = highlight;
token.userData.variantIndex = variantSequence[index];
tray.add(token);
sourceTokens.push(token);
}
}
// Low-poly rowboat
const boat = new THREE.Group();
boat.position.set(2.05, 0.24, 0);
scene.add(boat);
function createHullGeometry() {
const top = [
[-1.2, 0],
[-0.72, 0.58],
[0.58, 0.64],
[1.28, 0],
[0.58, -0.64],
[-0.72, -0.58]
];
const bottom = top.map(([x, z]) => [x * 0.7, z * 0.58]);
const positions = [];
function triangle(a, b, c) {
positions.push(...a, ...b, ...c);
}
for (let i = 0; i < top.length; i++) {
const j = (i + 1) % top.length;
const topA = [top[i][0], 0.29, top[i][1]];
const topB = [top[j][0], 0.29, top[j][1]];
const bottomA = [bottom[i][0], -0.22, bottom[i][1]];
const bottomB = [bottom[j][0], -0.22, bottom[j][1]];
triangle(topA, bottomA, topB);
triangle(topB, bottomA, bottomB);
triangle([0, -0.27, 0], bottomB, bottomA);
}
const geometry = new THREE.BufferGeometry();
geometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(positions, 3)
);
geometry.computeVertexNormals();
return geometry;
}
const hullMaterial = new THREE.MeshStandardMaterial({
color: 0xb97842,
roughness: 0.84,
metalness: 0,
flatShading: true,
side: THREE.DoubleSide
});
const hull = new THREE.Mesh(createHullGeometry(), hullMaterial);
hull.castShadow = true;
hull.receiveShadow = true;
boat.add(hull);
const rimMaterial = new THREE.MeshStandardMaterial({
color: 0x80502d,
roughness: 0.9,
flatShading: true
});
const rimPoints = [
new THREE.Vector3(-1.2, 0.3, 0),
new THREE.Vector3(-0.72, 0.3, 0.58),
new THREE.Vector3(0.58, 0.3, 0.64),
new THREE.Vector3(1.28, 0.3, 0),
new THREE.Vector3(0.58, 0.3, -0.64),
new THREE.Vector3(-0.72, 0.3, -0.58)
];
function cylinderBetween(start, end, radius, material, parent, segments = 6) {
const direction = new THREE.Vector3().subVectors(end, start);
const length = direction.length();
const mesh = new THREE.Mesh(
new THREE.CylinderGeometry(radius, radius, length, segments),
material
);
mesh.position.copy(start).add(end).multiplyScalar(0.5);
mesh.quaternion.setFromUnitVectors(
new THREE.Vector3(0, 1, 0),
direction.normalize()
);
mesh.castShadow = true;
parent.add(mesh);
return mesh;
}
for (let i = 0; i < rimPoints.length; i++) {
cylinderBetween(
rimPoints[i],
rimPoints[(i + 1) % rimPoints.length],
0.065,
rimMaterial,
boat
);
}
const floor = new THREE.Mesh(
new THREE.BoxGeometry(1.72, 0.06, 0.73),
new THREE.MeshStandardMaterial({
color: 0x8d5b37,
roughness: 0.95,
flatShading: true
})
);
floor.position.set(0, 0.2, 0);
floor.receiveShadow = true;
boat.add(floor);
// Two oars make the vessel immediately readable as a small rowboat.
const oarMaterial = new THREE.MeshStandardMaterial({
color: 0xd0a66d,
roughness: 0.85,
flatShading: true
});
cylinderBetween(
new THREE.Vector3(-0.35, 0.34, 0.49),
new THREE.Vector3(-0.78, 0.24, 1.18),
0.026,
oarMaterial,
boat
);
cylinderBetween(
new THREE.Vector3(-0.35, 0.34, -0.49),
new THREE.Vector3(-0.78, 0.24, -1.18),
0.026,
oarMaterial,
boat
);
const cargoGroup = new THREE.Group();
boat.add(cargoGroup);
const cargoPositions = [
[-0.68, -0.23],
[-0.68, 0.23],
[-0.23, -0.23],
[-0.23, 0.23],
[0.22, -0.23],
[0.22, 0.23],
[0.67, -0.23],
[0.67, 0.23],
[0.02, 0]
];
// Sampling and statistics
const sampleInput = document.querySelector("#sample-size");
const sampleSizeValue = document.querySelector("#sample-size-value");
const sampleCount = document.querySelector("#sample-count");
const insight = document.querySelector("#insight");
const sampleElements = variants.map((variant) => ({
sample: document.querySelector(`#sample-${variant.name.toLowerCase()}`),
source: document.querySelector(`#source-${variant.name.toLowerCase()}`),
sourceBar: document.querySelectorAll(
`#info-panel .variant .source-bar`
)[variants.indexOf(variant)],
sampleBar: document.querySelectorAll(
`#info-panel .variant .sample-bar`
)[variants.indexOf(variant)]
}));
function shuffledIndices(length) {
const indices = Array.from({ length }, (_, index) => index);
for (let i = indices.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[indices[i], indices[j]] = [indices[j], indices[i]];
}
return indices;
}
function drawSample() {
const size = Number(sampleInput.value);
sampleSizeValue.textContent = size;
sampleCount.textContent = size;
const selected = shuffledIndices(sourcePopulation).slice(0, size);
sourceTokens.forEach((token) => {
token.userData.highlight.visible = false;
});
while (cargoGroup.children.length) {
cargoGroup.remove(cargoGroup.children[0]);
}
const sampleCounts = [0, 0, 0];
const now = performance.now();
selected.forEach((sourceIndex, cargoIndex) => {
const sourceToken = sourceTokens[sourceIndex];
const variantIndex = sourceToken.userData.variantIndex;
sourceToken.userData.highlight.visible = true;
sampleCounts[variantIndex]++;
const cargoToken = makeToken(variantIndex);
const [x, z] = cargoPositions[cargoIndex];
cargoToken.position.set(x, 0.31, z);
cargoToken.rotation.y = Math.random() * Math.PI;
cargoToken.scale.setScalar(0.001);
cargoToken.userData.birthTime = now + cargoIndex * 35;
cargoGroup.add(cargoToken);
});
variants.forEach((variant, index) => {
const samplePercent = (sampleCounts[index] / size) * 100;
const sourcePercent = (sourceCounts[index] / sourcePopulation) * 100;
const elements = sampleElements[index];
elements.sample.textContent = `${Math.round(samplePercent)}%`;
elements.source.textContent = `${sourcePercent.toFixed(1).replace(".0", "")}%`;
elements.sourceBar.style.width = `${sourcePercent}%`;
elements.sampleBar.style.width = `${samplePercent}%`;
});
const differences = variants.map((variant, index) => ({
index,
difference:
(sampleCounts[index] / size) * 100 -
(sourceCounts[index] / sourcePopulation) * 100
}));
differences.sort((a, b) => Math.abs(b.difference) - Math.abs(a.difference));
const strongest = differences[0];
if (Math.abs(strongest.difference) < 0.5) {
insight.textContent =
"This chance sample happens to have nearly the same variant mix as the source population.";
} else {
const direction = strongest.difference > 0 ? "more common" : "less common";
const samplePct = ((sampleCounts[strongest.index] / size) * 100).toFixed(0);
const sourcePct = (
(sourceCounts[strongest.index] / sourcePopulation) *
100
).toFixed(0);
insight.textContent =
`By chance, ${variants[strongest.index].name} is ${samplePct}% of the founder group, ` +
`compared with ${sourcePct}% in the source population.`;
}
insight.textContent +=
" These sampled individuals—not offspring—are the founders.";
}
sampleInput.addEventListener("input", drawSample);
document.querySelector("#draw-sample").addEventListener("click", drawSample);
document.querySelector("#reset-view").addEventListener("click", resetCamera);
drawSample();
// Project HTML scene labels from world-space anchors.
const sourceLabel = document.querySelector("#source-label");
const boatLabel = document.querySelector("#boat-label");
const sourceAnchor = new THREE.Vector3(-2.4, 0.85, 1.28);
const boatAnchor = new THREE.Vector3(2.05, 0.95, 0.78);
function placeLabel(element, anchor) {
const projected = anchor.clone().project(camera);
const visible = projected.z > -1 && projected.z < 1;
element.style.display = visible ? "block" : "none";
element.style.left = `${(projected.x * 0.5 + 0.5) * window.innerWidth}px`;
element.style.top = `${(-projected.y * 0.5 + 0.5) * window.innerHeight}px`;
}
function onResize() {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
}
window.addEventListener("resize", onResize);
const clock = new THREE.Clock();
function animate() {
requestAnimationFrame(animate);
const time = clock.getElapsedTime();
const now = performance.now();
// The boat gently rocks, but no reproduction animation or offspring appear.
boat.position.y = 0.24 + Math.sin(time * 1.55) * 0.018;
boat.rotation.z = Math.sin(time * 1.05) * 0.012;
boat.rotation.x = Math.cos(time * 0.85) * 0.006;
sourceTokens.forEach((token, index) => {
const ring = token.userData.highlight;
if (ring.visible) {
const pulse = 1 + Math.sin(time * 2.2 + index * 0.31) * 0.045;
ring.scale.setScalar(pulse);
}
});
cargoGroup.children.forEach((token) => {
const age = now - token.userData.birthTime;
if (age < 430) {
const progress = Math.max(0, Math.min(1, age / 430));
const eased = 1 - Math.pow(1 - progress, 3);
token.scale.setScalar(Math.max(0.001, eased));
} else {
token.scale.setScalar(1);
}
});
placeLabel(sourceLabel, sourceAnchor);
placeLabel(boatLabel, boatAnchor);
renderer.render(scene, camera);
}
animate();
</script>
</body>
</html>
```