Prompt specification
Build or customize the Three.js 3D scene "Linkage probability simulator" 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: Linkage probability simulator
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/linkage-probability-simulator-x0928
Scene Description Prompt:
Please create one self-contained HTML file with Three.js from cdnjs showing genetic linkage. Allow a recombination-frequency slider within biologically meaningful bounds for a two-locus toy model; compare expected and sampled offspring categories with clearly labeled synthetic data. Voxel style.
Working Three.js Starter (linkage-probability-simulator-x0928):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Linkage Lab — Synthetic Two-Locus Model</title>
<style>
:root {
color-scheme: dark;
--bg: #07101d;
--panel: rgba(14, 27, 44, 0.88);
--panel-solid: #0e1b2c;
--panel-soft: #12243a;
--border: rgba(154, 190, 224, 0.16);
--text: #e8f1fb;
--muted: #91a8c1;
--cyan: #45e6ff;
--cyan-soft: rgba(69, 230, 255, 0.16);
--coral: #ff746c;
--coral-soft: rgba(255, 116, 108, 0.16);
--green: #7ee6a6;
--amber: #ffd166;
--shadow: 0 22px 60px rgba(0, 0, 0, 0.28);
}
* {
box-sizing: border-box;
}
html,
body {
margin: 0;
min-height: 100%;
}
body {
background:
radial-gradient(circle at 12% 0%, rgba(38, 120, 159, 0.19), transparent 32rem),
radial-gradient(circle at 95% 15%, rgba(108, 54, 82, 0.15), transparent 30rem),
var(--bg);
color: var(--text);
font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
line-height: 1.45;
}
button,
input,
select {
font: inherit;
}
button,
select {
color: var(--text);
}
.app {
width: min(1560px, 100%);
margin: 0 auto;
padding: 22px;
}
.topbar {
display: flex;
align-items: center;
justify-content: space-between;
gap: 20px;
margin-bottom: 18px;
}
.brand {
display: flex;
align-items: center;
gap: 14px;
}
.brand-mark {
position: relative;
width: 46px;
height: 46px;
border: 1px solid rgba(69, 230, 255, 0.45);
background: linear-gradient(145deg, rgba(69, 230, 255, 0.18), rgba(69, 230, 255, 0.02));
box-shadow: 0 0 30px rgba(69, 230, 255, 0.09);
}
.brand-mark::before,
.brand-mark::after {
content: "";
position: absolute;
width: 13px;
height: 13px;
background: var(--cyan);
box-shadow: 0 0 16px rgba(69, 230, 255, 0.55);
}
.brand-mark::before {
left: 9px;
top: 9px;
}
.brand-mark::after {
right: 9px;
bottom: 9px;
background: var(--coral);
box-shadow: 0 0 16px rgba(255, 116, 108, 0.5);
}
.eyebrow {
color: var(--cyan);
font-size: 0.72rem;
font-weight: 800;
letter-spacing: 0.15em;
text-transform: uppercase;
}
h1 {
margin: 2px 0 0;
font-size: clamp(1.3rem, 2.2vw, 2rem);
letter-spacing: -0.035em;
line-height: 1.1;
}
.synthetic-badge {
display: inline-flex;
align-items: center;
gap: 8px;
border: 1px solid rgba(255, 209, 102, 0.32);
border-radius: 999px;
padding: 8px 12px;
background: rgba(255, 209, 102, 0.08);
color: #ffe4a5;
font-size: 0.73rem;
font-weight: 800;
letter-spacing: 0.08em;
text-transform: uppercase;
white-space: nowrap;
}
.synthetic-badge::before {
content: "";
width: 7px;
height: 7px;
border-radius: 50%;
background: var(--amber);
box-shadow: 0 0 12px rgba(255, 209, 102, 0.8);
}
.layout {
display: grid;
grid-template-columns: 330px minmax(0, 1fr);
gap: 18px;
align-items: start;
}
.panel,
.visual-card,
.table-card {
border: 1px solid var(--border);
background: var(--panel);
box-shadow: var(--shadow);
backdrop-filter: blur(14px);
}
.panel {
position: sticky;
top: 18px;
border-radius: 18px;
padding: 20px;
}
.section-title {
display: flex;
align-items: center;
justify-content: space-between;
gap: 12px;
margin-bottom: 15px;
}
.section-title h2 {
margin: 0;
font-size: 0.98rem;
letter-spacing: -0.01em;
}
.section-number {
color: var(--muted);
font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
font-size: 0.72rem;
}
.control-block {
padding: 16px 0;
border-top: 1px solid var(--border);
}
.control-block:first-of-type {
border-top: 0;
padding-top: 0;
}
.control-label {
display: flex;
justify-content: space-between;
align-items: baseline;
gap: 10px;
margin-bottom: 10px;
color: var(--muted);
font-size: 0.78rem;
font-weight: 700;
}
.r-readout {
color: var(--cyan);
font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
font-size: 1.28rem;
font-weight: 800;
}
input[type="range"] {
width: 100%;
height: 24px;
margin: 0;
accent-color: var(--cyan);
cursor: pointer;
}
.range-labels {
display: flex;
justify-content: space-between;
gap: 12px;
margin-top: 6px;
color: var(--muted);
font-size: 0.68rem;
}
.metric-grid {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 9px;
margin-top: 14px;
}
.metric {
border: 1px solid var(--border);
border-radius: 10px;
padding: 10px;
background: rgba(255, 255, 255, 0.025);
}
.metric span {
display: block;
color: var(--muted);
font-size: 0.67rem;
text-transform: uppercase;
letter-spacing: 0.08em;
}
.metric strong {
display: block;
margin-top: 3px;
font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
font-size: 0.85rem;
}
.select-row {
display: grid;
grid-template-columns: 1fr auto;
gap: 9px;
align-items: end;
}
.field-label {
display: block;
margin-bottom: 6px;
color: var(--muted);
font-size: 0.7rem;
font-weight: 700;
text-transform: uppercase;
letter-spacing: 0.08em;
}
select {
width: 100%;
border: 1px solid var(--border);
border-radius: 9px;
padding: 9px 30px 9px 10px;
background: #102238;
outline: none;
}
button {
border: 1px solid rgba(69, 230, 255, 0.3);
border-radius: 9px;
background: var(--cyan-soft);
color: var(--cyan);
padding: 9px 12px;
font-size: 0.75rem;
font-weight: 800;
cursor: pointer;
transition: background 140ms ease, transform 140ms ease, border-color 140ms ease;
}
button:hover {
background: rgba(69, 230, 255, 0.24);
border-color: rgba(69, 230, 255, 0.6);
transform: translateY(-1px);
}
button:focus-visible,
select:focus-visible,
input:focus-visible {
outline: 2px solid var(--cyan);
outline-offset: 3px;
}
.secondary-button {
width: 100%;
margin-top: 10px;
border-color: var(--border);
background: rgba(255, 255, 255, 0.035);
color: var(--text);
}
.seed-line {
display: flex;
justify-content: space-between;
margin-top: 10px;
color: var(--muted);
font-size: 0.67rem;
}
.seed-line code {
color: #c8d8e9;
font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
}
.gamete-list {
display: grid;
gap: 10px;
}
.gamete-row {
display: grid;
grid-template-columns: 37px 1fr 48px;
align-items: center;
gap: 9px;
font-size: 0.74rem;
}
.gamete-name {
font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
font-weight: 800;
}
.gamete-track {
height: 7px;
overflow: hidden;
border-radius: 999px;
background: rgba(255, 255, 255, 0.065);
}
.gamete-fill {
width: 0;
height: 100%;
border-radius: inherit;
background: var(--cyan);
transition: width 180ms ease;
}
.gamete-fill.recombinant {
background: var(--coral);
}
.gamete-value {
color: var(--muted);
text-align: right;
font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
}
.model-note {
border-left: 3px solid var(--cyan);
padding: 9px 11px;
background: rgba(69, 230, 255, 0.06);
color: #b8cadc;
font-size: 0.72rem;
}
.model-note code {
color: #e8f7ff;
font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
}
.content {
min-width: 0;
display: grid;
gap: 18px;
}
.visual-card,
.table-card {
overflow: hidden;
border-radius: 18px;
}
.visual-header {
display: flex;
align-items: center;
justify-content: space-between;
gap: 16px;
padding: 15px 18px;
border-bottom: 1px solid var(--border);
}
.visual-title {
margin: 0;
font-size: 0.93rem;
}
.visual-subtitle {
margin: 3px 0 0;
color: var(--muted);
font-size: 0.72rem;
}
.legend {
display: flex;
flex-wrap: wrap;
justify-content: flex-end;
gap: 12px;
color: var(--muted);
font-size: 0.7rem;
}
.legend-item {
display: inline-flex;
align-items: center;
gap: 6px;
white-space: nowrap;
}
.legend-swatch {
width: 12px;
height: 12px;
border-radius: 3px;
background: var(--cyan);
box-shadow: 0 0 12px rgba(69, 230, 255, 0.3);
}
.legend-swatch.sampled {
background: var(--coral);
box-shadow: 0 0 12px rgba(255, 116, 108, 0.3);
}
#scene-wrap {
position: relative;
height: clamp(420px, 57vh, 660px);
min-height: 390px;
overflow: hidden;
background: #081321;
}
#three-canvas {
display: block;
width: 100%;
height: 100%;
cursor: grab;
touch-action: none;
}
#three-canvas:active {
cursor: grabbing;
}
.scene-readout {
position: absolute;
top: 16px;
left: 17px;
display: grid;
gap: 5px;
padding: 10px 12px;
border: 1px solid rgba(154, 190, 224, 0.15);
border-radius: 10px;
background: rgba(5, 13, 24, 0.7);
pointer-events: none;
backdrop-filter: blur(8px);
}
.scene-readout strong {
color: var(--cyan);
font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
font-size: 0.84rem;
}
.scene-readout span {
color: var(--muted);
font-size: 0.67rem;
}
.orbit-hint {
position: absolute;
right: 15px;
bottom: 14px;
border: 1px solid rgba(154, 190, 224, 0.13);
border-radius: 999px;
padding: 7px 10px;
background: rgba(5, 13, 24, 0.68);
color: var(--muted);
font-size: 0.66rem;
pointer-events: none;
}
.loading {
position: absolute;
inset: 0;
display: grid;
place-items: center;
background: #081321;
color: var(--muted);
font-size: 0.8rem;
z-index: 3;
transition: opacity 200ms ease;
}
.loading.hidden {
opacity: 0;
pointer-events: none;
}
.table-header {
display: flex;
align-items: center;
justify-content: space-between;
gap: 14px;
padding: 16px 18px 12px;
}
.table-header h2 {
margin: 0;
font-size: 0.95rem;
}
.table-header p {
margin: 3px 0 0;
color: var(--muted);
font-size: 0.71rem;
}
.table-scroll {
overflow-x: auto;
padding: 0 10px 12px;
}
table {
width: 100%;
min-width: 720px;
border-collapse: separate;
border-spacing: 0 5px;
font-size: 0.75rem;
}
th {
padding: 6px 12px;
color: var(--muted);
font-size: 0.65rem;
font-weight: 800;
letter-spacing: 0.07em;
text-align: right;
text-transform: uppercase;
}
th:first-child,
td:first-child {
text-align: left;
}
td {
padding: 9px 12px;
border-top: 1px solid rgba(154, 190, 224, 0.08);
border-bottom: 1px solid rgba(154, 190, 224, 0.08);
background: rgba(255, 255, 255, 0.018);
text-align: right;
font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
}
td:first-child {
border-left: 1px solid rgba(154, 190, 224, 0.08);
border-radius: 9px 0 0 9px;
}
td:last-child {
border-right: 1px solid rgba(154, 190, 224, 0.08);
border-radius: 0 9px 9px 0;
}
.class-cell {
display: flex;
align-items: center;
justify-content: space-between;
gap: 10px;
}
.class-name {
font-weight: 800;
color: #f3f8ff;
}
.class-tag {
display: inline-flex;
align-items: center;
border-radius: 999px;
padding: 3px 7px;
font-family: Inter, ui-sans-serif, system-ui, sans-serif;
font-size: 0.59rem;
font-weight: 800;
letter-spacing: 0.05em;
text-transform: uppercase;
white-space: nowrap;
}
.class-tag.parental {
color: #baf3ff;
background: rgba(69, 230, 255, 0.12);
}
.class-tag.mixed {
color: #ffe0a1;
background: rgba(255, 209, 102, 0.11);
}
.class-tag.recombinant {
color: #ffc1bd;
background: rgba(255, 116, 108, 0.12);
}
.expected-text {
color: #9eeeff;
}
.observed-text {
color: #ffaaa4;
font-weight: 800;
}
.table-bar {
width: 100%;
height: 3px;
margin-top: 5px;
overflow: hidden;
border-radius: 999px;
background: rgba(255, 255, 255, 0.055);
}
.table-bar i {
display: block;
width: 0;
height: 100%;
border-radius: inherit;
background: var(--coral);
transition: width 180ms ease;
}
.table-footnote {
display: flex;
justify-content: space-between;
gap: 16px;
padding: 0 18px 16px;
color: var(--muted);
font-size: 0.67rem;
}
.footer {
padding: 17px 2px 0;
color: #7188a0;
font-size: 0.68rem;
text-align: center;
}
.sr-only {
position: absolute;
width: 1px;
height: 1px;
padding: 0;
margin: -1px;
overflow: hidden;
clip: rect(0, 0, 0, 0);
white-space: nowrap;
border: 0;
}
@media (max-width: 1000px) {
.app {
padding: 14px;
}
.layout {
grid-template-columns: 1fr;
}
.panel {
position: static;
}
.control-block {
padding: 13px 0;
}
}
@media (max-width: 650px) {
.topbar {
align-items: flex-start;
}
.synthetic-badge {
padding: 7px 9px;
font-size: 0.61rem;
}
.brand-mark {
width: 39px;
height: 39px;
}
.brand-mark::before,
.brand-mark::after {
width: 11px;
height: 11px;
}
.brand-mark::before {
left: 7px;
top: 7px;
}
.brand-mark::after {
right: 7px;
bottom: 7px;
}
.visual-header {
align-items: flex-start;
flex-direction: column;
}
.legend {
justify-content: flex-start;
}
#scene-wrap {
height: 440px;
}
.table-footnote {
flex-direction: column;
gap: 4px;
}
}
</style>
</head>
<body>
<div class="app">
<header class="topbar">
<div class="brand">
<div class="brand-mark" aria-hidden="true"></div>
<div>
<div class="eyebrow">Linkage Lab</div>
<h1>Genetic Linkage Explorer</h1>
</div>
</div>
<div class="synthetic-badge">Synthetic data</div>
</header>
<main class="layout">
<aside class="panel" aria-label="Model controls">
<div class="section-title">
<h2>Two-locus toy model</h2>
<span class="section-number">01</span>
</div>
<div class="control-block">
<label class="control-label" for="recombination">
<span>Recombination fraction</span>
<output class="r-readout" id="rValue" for="recombination">0.220</output>
</label>
<input
id="recombination"
type="range"
min="0"
max="0.5"
step="0.001"
value="0.22"
aria-describedby="rangeExplanation"
/>
<div class="range-labels">
<span>0 · complete linkage</span>
<span>0.50 · independent</span>
</div>
<div class="metric-grid">
<div class="metric">
<span>Toy map distance</span>
<strong id="cmValue">22 cM*</strong>
</div>
<div class="metric">
<span>Linkage state</span>
<strong id="linkageState">Partial</strong>
</div>
</div>
<p id="rangeExplanation" class="model-note">
The biologically meaningful range for this model is
<code>0 ≤ r ≤ 0.50</code>. At 0.50, the two loci assort independently.
<br /><br />
*Map distance is approximated as <code>100r cM</code> for this simplified
toy model only.
</p>
</div>
<div class="control-block">
<div class="section-title">
<h2>Synthetic cohort</h2>
<span class="section-number">02</span>
</div>
<div class="select-row">
<label>
<span class="field-label">Offspring sample size</span>
<select id="sampleSize">
<option value="160">160 offspring</option>
<option value="320" selected>320 offspring</option>
<option value="640">640 offspring</option>
<option value="1200">1,200 offspring</option>
</select>
</label>
<button id="resampleBtn" type="button">Resample</button>
</div>
<div class="seed-line">
<span>Reproducible synthetic seed</span>
<code id="seedValue">0xC0FFEE</code>
</div>
<button id="resetViewBtn" class="secondary-button" type="button">
Reset 3D camera
</button>
</div>
<div class="control-block">
<div class="section-title">
<h2>Gamete frequencies</h2>
<span class="section-number">03</span>
</div>
<div class="gamete-list">
<div class="gamete-row">
<span class="gamete-name">AB</span>
<div class="gamete-track"><div class="gamete-fill" id="gAB"></div></div>
<span class="gamete-value" id="gABValue">39.0%</span>
</div>
<div class="gamete-row">
<span class="gamete-name">Ab</span>
<div class="gamete-track"><div class="gamete-fill recombinant" id="gAb"></div></div>
<span class="gamete-value" id="gAbValue">11.0%</span>
</div>
<div class="gamete-row">
<span class="gamete-name">aB</span>
<div class="gamete-track"><div class="gamete-fill recombinant" id="gABalt"></div></div>
<span class="gamete-value" id="gABaltValue">11.0%</span>
</div>
<div class="gamete-row">
<span class="gamete-name">ab</span>
<div class="gamete-track"><div class="gamete-fill" id="gab"></div></div>
<span class="gamete-value" id="gabValue">39.0%</span>
</div>
</div>
</div>
<div class="control-block">
<div class="section-title">
<h2>Assumptions</h2>
<span class="section-number">04</span>
</div>
<div class="model-note">
Both parents are <code>AB/ab × AB/ab</code> in coupling phase.
Each offspring receives one independent gamete from each parent.
<br /><br />
<code>P(AB) = P(ab) = (1−r)/2</code><br />
<code>P(Ab) = P(aB) = r/2</code>
<br /><br />
No mutation, selection, distortion, or biological sex difference is modeled.
</div>
</div>
</aside>
<section class="content">
<div class="visual-card">
<div class="visual-header">
<div>
<h2 class="visual-title">Expected vs. sampled offspring categories</h2>
<p class="visual-subtitle">
Each 5% probability level is represented by one voxel.
</p>
</div>
<div class="legend" aria-label="Chart legend">
<span class="legend-item">
<span class="legend-swatch"></span>
Theoretical expectation
</span>
<span class="legend-item">
<span class="legend-swatch sampled"></span>
Synthetic sample
</span>
</div>
</div>
<div id="scene-wrap">
<canvas
id="three-canvas"
aria-label="Interactive three-dimensional voxel chart comparing expected and synthetically sampled offspring genotype frequencies"
></canvas>
<div class="scene-readout">
<strong id="sceneR">r = 0.220</strong>
<span id="sceneN">Synthetic cohort: n = 320</span>
<span>Model: AB/ab × AB/ab</span>
</div>
<div class="orbit-hint">Drag to orbit · Wheel to zoom · Double-click to reset</div>
<div class="loading" id="loading">Building voxel model…</div>
</div>
</div>
<div class="table-card">
<div class="table-header">
<div>
<h2>Numerical comparison</h2>
<p>Expected values are model probabilities; sample values are browser-generated.</p>
</div>
<div class="synthetic-badge">Synthetic sample</div>
</div>
<div class="table-scroll">
<table>
<caption class="sr-only">
Expected and synthetic sampled frequencies for ten offspring genotype classes
</caption>
<thead>
<tr>
<th scope="col">Offspring class</th>
<th scope="col">Expected %</th>
<th scope="col">Expected n</th>
<th scope="col">Synthetic %</th>
<th scope="col">Sample n</th>
</tr>
</thead>
<tbody id="comparisonBody"></tbody>
</table>
</div>
<div class="table-footnote">
<span>“Parental” contains no recombinant haplotype; “mixed” contains one; “recombinant” contains two.</span>
<span>Expected n may be fractional because it is a theoretical expectation.</span>
</div>
</div>
</section>
</main>
<footer class="footer">
Illustrative educational model — not an empirical pedigree or population dataset.
All sampled offspring are synthetic.
</footer>
</div>
<script type="module">
import * as THREE from "https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// -------------------------------------------------------------------------
// Model state
// -------------------------------------------------------------------------
const $ = (id) => document.getElementById(id);
const GAMETE_INDEX = {
AB: 0,
Ab: 1,
aB: 2,
ab: 3
};
const categories = [
{ label: "AB / AB", a: 0, b: 0, type: "parental" },
{ label: "AB / Ab", a: 0, b: 1, type: "mixed" },
{ label: "AB / aB", a: 0, b: 2, type: "mixed" },
{ label: "AB / ab", a: 0, b: 3, type: "parental" },
{ label: "Ab / Ab", a: 1, b: 1, type: "recombinant" },
{ label: "Ab / aB", a: 1, b: 2, type: "recombinant" },
{ label: "Ab / ab", a: 1, b: 3, type: "mixed" },
{ label: "aB / aB", a: 2, b: 2, type: "recombinant" },
{ label: "aB / ab", a: 2, b: 3, type: "mixed" },
{ label: "ab / ab", a: 3, b: 3, type: "parental" }
];
let recombination = Number($("recombination").value);
let sampleSize = Number($("sampleSize").value);
let currentSeed = 0xc0ffee;
let sampledCounts = new Array(categories.length).fill(0);
function theoreticalProbabilities(r) {
const gameteProbabilities = [
(1 - r) / 2, // AB
r / 2, // Ab
r / 2, // aB
(1 - r) / 2 // ab
];
return categories.map((category) => {
const pa = gameteProbabilities[category.a];
const pb = gameteProbabilities[category.b];
return category.a === category.b
? pa * pb
: 2 * pa * pb;
});
}
function seededRandom(seed) {
let state = seed | 0;
return function () {
state = (state + 0x6d2b79f5) | 0;
let value = Math.imul(state ^ (state >>> 15), 1 | state);
value = (value + Math.imul(value ^ (value >>> 7), 61 | value)) ^ value;
return ((value ^ (value >>> 14)) >>> 0) / 4294967296;
};
}
function sampleSyntheticOffspring() {
const random = seededRandom(currentSeed);
const q = (1 - recombination) / 2;
const s = recombination / 2;
const pairToCategory = new Map();
categories.forEach((category, index) => {
const low = Math.min(category.a, category.b);
const high = Math.max(category.a, category.b);
pairToCategory.set(`${low},${high}`, index);
});
sampledCounts.fill(0);
for (let offspring = 0; offspring < sampleSize; offspring++) {
const first = drawGamete(random, q, s);
const second = drawGamete(random, q, s);
const low = Math.min(first, second);
const high = Math.max(first, second);
const categoryIndex = pairToCategory.get(`${low},${high}`);
sampledCounts[categoryIndex]++;
}
}
function drawGamete(random, q, s) {
const value = random();
if (value < q) return 0; // AB
if (value < q + s) return 1; // Ab
if (value < q + 2 * s) return 2; // aB
return 3; // ab
}
function formatPercent(value) {
return `${(value * 100).toFixed(2)}%`;
}
function formatCount(value) {
return Number.isInteger(value) ? String(value) : value.toFixed(1);
}
function updateReadouts() {
const q = (1 - recombination) / 2;
const s = recombination / 2;
$("rValue").textContent = recombination.toFixed(3);
$("cmValue").textContent = `${Math.round(recombination * 100)} cM*`;
$("sceneR").textContent = `r = ${recombination.toFixed(3)}`;
$("sceneN").textContent = `Synthetic cohort: n = ${sampleSize.toLocaleString()}`;
$("seedValue").textContent = `0x${currentSeed.toString(16).toUpperCase().padStart(6, "0")}`;
if (recombination <= 0.001) {
$("linkageState").textContent = "Complete";
} else if (recombination >= 0.499) {
$("linkageState").textContent = "Independent";
} else if (recombination >= 0.45) {
$("linkageState").textContent = "Near-independent";
} else {
$("linkageState").textContent = "Partial";
}
const gametes = [
{ id: "gAB", valueId: "gABValue", probability: q },
{ id: "gAb", valueId: "gAbValue", probability: s },
{ id: "gABalt", valueId: "gABaltValue", probability: s },
{ id: "gab", valueId: "gabValue", probability: q }
];
gametes.forEach((gamete) => {
$(gamete.id).style.width = `${(gamete.probability / 0.5) * 100}%`;
$(gamete.valueId).textContent = formatPercent(gamete.probability);
});
}
function buildTableRows() {
$("comparisonBody").innerHTML = categories.map((category, index) => `
<tr>
<td>
<div class="class-cell">
<span class="class-name">${category.label}</span>
<span class="class-tag ${category.type}">${category.type}</span>
</div>
</td>
<td class="expected-text" id="expectedPercent-${index}"></td>
<td id="expectedCount-${index}"></td>
<td>
<span class="observed-text" id="observedPercent-${index}"></span>
<div class="table-bar"><i id="observedBar-${index}"></i></div>
</td>
<td id="observedCount-${index}"></td>
</tr>
`).join("");
}
function updateTable(expectedProbabilities) {
categories.forEach((category, index) => {
const expectedProbability = expectedProbabilities[index];
const expectedNumber = expectedProbability * sampleSize;
const observedProbability = sampledCounts[index] / sampleSize;
$(`expectedPercent-${index}`).textContent = formatPercent(expectedProbability);
$(`expectedCount-${index}`).textContent = formatCount(expectedNumber);
$(`observedPercent-${index}`).textContent = formatPercent(observedProbability);
$(`observedCount-${index}`).textContent = sampledCounts[index];
$(`observedBar-${index}`).style.width =
`${Math.min(100, (observedProbability / 0.55) * 100)}%`;
});
}
// -------------------------------------------------------------------------
// Three.js scene
// -------------------------------------------------------------------------
const canvas = $("three-canvas");
const sceneWrap = $("scene-wrap");
let renderer;
try {
renderer = new THREE.WebGLRenderer({
canvas,
antialias: true,
alpha: false,
powerPreference: "high-performance"
});
} catch (error) {
$("loading").textContent = "WebGL could not be initialized in this browser.";
throw error;
}
renderer.setPixelRatio(Math.min(window.devicePixelRatio || 1, 2));
renderer.setClearColor(0x081321, 1);
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x081321);
scene.fog = new THREE.Fog(0x081321, 28, 50);
const camera = new THREE.OrthographicCamera(-10, 10, 6, -6, 0.1, 100);
const cameraTarget = new THREE.Vector3(0, 2.35, 0);
let cameraYaw = -0.34;
let cameraPitch = 0.57;
let cameraZoom = 1;
function updateCamera() {
const radius = 23;
const horizontal = radius * Math.sin(cameraPitch);
camera.position.set(
cameraTarget.x + horizontal * Math.sin(cameraYaw),
cameraTarget.y + radius * Math.cos(cameraPitch),
cameraTarget.z + horizontal * Math.cos(cameraYaw)
);
camera.lookAt(cameraTarget);
camera.zoom = cameraZoom;
camera.updateProjectionMatrix();
}
function resetCamera() {
cameraYaw = -0.34;
cameraPitch = 0.57;
cameraZoom = 1;
updateCamera();
}
// Lighting
scene.add(new THREE.HemisphereLight(0xa8e7ff, 0x111827, 1.6));
const keyLight = new THREE.DirectionalLight(0xffffff, 2.8);
keyLight.position.set(7, 13, 9);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(1024, 1024);
keyLight.shadow.camera.left = -13;
keyLight.shadow.camera.right = 13;
keyLight.shadow.camera.top = 11;
keyLight.shadow.camera.bottom = -5;
keyLight.shadow.camera.near = 1;
keyLight.shadow.camera.far = 35;
keyLight.shadow.normalBias = 0.03;
scene.add(keyLight);
const rimLight = new THREE.DirectionalLight(0x45e6ff, 1.25);
rimLight.position.set(-9, 5, -6);
scene.add(rimLight);
const warmLight = new THREE.DirectionalLight(0xff746c, 0.85);
warmLight.position.set(8, 3, -7);
scene.add(warmLight);
// Shared geometry and materials
const blockGeometry = new THREE.BoxGeometry(1, 1, 1);
const blockEdgesGeometry = new THREE.EdgesGeometry(blockGeometry);
const expectedMaterial = new THREE.MeshStandardMaterial({
color: 0x45e6ff,
emissive: 0x075666,
emissiveIntensity: 0.55,
roughness: 0.38,
metalness: 0.03,
transparent: true,
opacity: 0.47,
depthWrite: false
});
const sampledMaterial = new THREE.MeshStandardMaterial({
color: 0xff746c,
emissive: 0x6b1714,
emissiveIntensity: 0.48,
roughness: 0.38,
metalness: 0.03
});
const expectedEdgeMaterial = new THREE.LineBasicMaterial({
color: 0x9af2ff,
transparent: true,
opacity: 0.78
});
const sampledEdgeMaterial = new THREE.LineBasicMaterial({
color: 0xffc0bb,
transparent: true,
opacity: 0.72
});
const plinthMaterial = new THREE.MeshStandardMaterial({
color: 0x10243a,
roughness: 0.9,
metalness: 0.05
});
const floorMaterial = new THREE.MeshStandardMaterial({
color: 0x0a1726,
roughness: 0.95,
metalness: 0.02
});
const expectedBaseMaterial = new THREE.MeshStandardMaterial({
color: 0x45e6ff,
emissive: 0x0d5f72,
emissiveIntensity: 0.4
});
const sampledBaseMaterial = new THREE.MeshStandardMaterial({
color: 0xff746c,
emissive: 0x6b1a16,
emissiveIntensity: 0.4
});
// Floor and platform
const floor = new THREE.Mesh(
new THREE.BoxGeometry(21.6, 0.24, 6.4),
floorMaterial
);
floor.position.set(0, -0.18, 0);
floor.receiveShadow = true;
scene.add(floor);
const gridVertices = [];
for (let x = -10; x <= 10; x += 1) {
gridVertices.push(x, -0.052, -3, x, -0.052, 3);
}
for (let z = -3; z <= 3; z += 1) {
gridVertices.push(-10, -0.052, z, 10, -0.052, z);
}
const gridGeometry = new THREE.BufferGeometry();
gridGeometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(gridVertices, 3)
);
const gridMaterial = new THREE.LineBasicMaterial({
color: 0x31506a,
transparent: true,
opacity: 0.3
});
scene.add(new THREE.LineSegments(gridGeometry, gridMaterial));
// Axis
const axisVertices = [
-9.78, 0.08, 0,
-9.78, 6.95, 0
];
for (let percent = 0; percent <= 60; percent += 10) {
const y = 0.08 + (percent / 100) * 10.6;
axisVertices.push(-9.92, y, 0, -9.64, y, 0);
}
const axisGeometry = new THREE.BufferGeometry();
axisGeometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(axisVertices, 3)
);
scene.add(new THREE.LineSegments(
axisGeometry,
new THREE.LineBasicMaterial({
color: 0x6d8ba6,
transparent: true,
opacity: 0.58
})
));
function roundedRectangle(context, x, y, width, height, radius) {
const r = Math.min(radius, width / 2, height / 2);
context.beginPath();
context.moveTo(x + r, y);
context.lineTo(x + width - r, y);
context.quadraticCurveTo(x + width, y, x + width, y + r);
context.lineTo(x + width, y + height - r);
context.quadraticCurveTo(x + width, y + height, x + width - r, y + height);
context.lineTo(x + r, y + height);
context.quadraticCurveTo(x, y + height, x, y + height - r);
context.lineTo(x, y + r);
context.quadraticCurveTo(x, y, x + r, y);
context.closePath();
}
function createTextSprite(text, options = {}) {
const {
color = "#dcecff",
background = "rgba(6, 17, 30, 0.88)",
border = "rgba(117, 157, 190, 0.24)",
fontSize = 42
} = options;
const textCanvas = document.createElement("canvas");
textCanvas.width = 512;
textCanvas.height = 96;
const context = textCanvas.getContext("2d");
context.clearRect(0, 0, textCanvas.width, textCanvas.height);
if (background !== "transparent") {
roundedRectangle(context, 4, 4, 504, 88, 18);
context.fillStyle = background;
context.fill();
context.strokeStyle = border;
context.lineWidth = 2;
context.stroke();
}
context.fillStyle = color;
context.font = `700 ${fontSize}px system-ui, -apple-system, sans-serif`;
context.textAlign = "center";
context.textBaseline = "middle";
context.fillText(text, 256, 49);
const texture = new THREE.CanvasTexture(textCanvas);
texture.colorSpace = THREE.SRGBColorSpace;
texture.minFilter = THREE.LinearFilter;
texture.magFilter = THREE.LinearFilter;
const material = new THREE.SpriteMaterial({
map: texture,
transparent: true,
depthTest: false,
depthWrite: false
});
const sprite = new THREE.Sprite(material);
sprite.renderOrder = 20;
return sprite;
}
for (let percent = 0; percent <= 60; percent += 10) {
const label = createTextSprite(`${percent}%`, {
color: "#8fa9c0",
background: "transparent",
fontSize: 38
});
label.scale.set(0.72, 0.22, 1);
label.position.set(-10.22, 0.08 + (percent / 100) * 10.6, 0);
scene.add(label);
}
// One pair of voxel towers per offspring category.
const expectedTowers = [];
const sampledTowers = [];
categories.forEach((category, index) => {
const x = (index - (categories.length - 1) / 2) * 1.9;
const group = new THREE.Group();
group.position.x = x;
const plinth = new THREE.Mesh(
new THREE.BoxGeometry(1.48, 0.16, 1.38),
plinthMaterial
);
plinth.position.y = 0.02;
plinth.receiveShadow = true;
group.add(plinth);
const expectedBase = new THREE.Mesh(
new THREE.BoxGeometry(0.48, 0.035, 1.12),
expectedBaseMaterial
);
expectedBase.position.set(-0.32, 0.12, 0);
group.add(expectedBase);
const sampledBase = new THREE.Mesh(
new THREE.BoxGeometry(0.48, 0.035, 1.12),
sampledBaseMaterial
);
sampledBase.position.set(0.32, 0.12, 0);
group.add(sampledBase);
const expectedTower = new THREE.Group();
expectedTower.position.x = -0.32;
group.add(expectedTower);
expectedTowers.push(expectedTower);
const sampledTower = new THREE.Group();
sampledTower.position.x = 0.32;
group.add(sampledTower);
sampledTowers.push(sampledTower);
const label = createTextSprite(category.label, {
color: "#dce9f6",
background: "rgba(7, 18, 31, 0.9)",
fontSize: 38
});
label.scale.set(1.58, 0.37, 1);
label.position.set(0, 0.22, 1.18);
group.add(label);
scene.add(group);
});
function clearTower(tower) {
while (tower.children.length > 0) {
tower.remove(tower.children[tower.children.length - 1]);
}
}
function addVoxel(tower, heightScale, yPosition, material, edgeMaterial, castShadow) {
const mesh = new THREE.Mesh(blockGeometry, material);
mesh.scale.set(0.46, heightScale, 0.46);
mesh.position.y = yPosition;
mesh.castShadow = castShadow;
mesh.receiveShadow = castShadow;
tower.add(mesh);
const edges = new THREE.LineSegments(blockEdgesGeometry, edgeMaterial);
mesh.add(edges);
}
function fillTower(tower, probability, material, edgeMaterial, castShadow) {
clearTower(tower);
// Each full voxel represents a five-percentage-point probability step.
const units = probability / 0.05;
const fullBlocks = Math.floor(units + 1e-7);
const remainder = Math.max(0, units - fullBlocks);
const blockHeight = 0.46;
const blockStep = 0.53;
const baseY = 0.14;
for (let index = 0; index < fullBlocks; index++) {
addVoxel(
tower,
blockHeight,
baseY + index * blockStep + blockHeight / 2,
material,
edgeMaterial,
castShadow
);
}
if (remainder > 0.001) {
const partialHeight = blockHeight * remainder;
addVoxel(
tower,
partialHeight,
baseY + fullBlocks * blockStep + partialHeight / 2,
material,
edgeMaterial,
castShadow
);
}
}
function updateChart(expectedProbabilities) {
categories.forEach((category, index) => {
fillTower(
expectedTowers[index],
expectedProbabilities[index],
expectedMaterial,
expectedEdgeMaterial,
false
);
fillTower(
sampledTowers[index],
sampledCounts[index] / sampleSize,
sampledMaterial,
sampledEdgeMaterial,
true
);
});
}
// Responsive orthographic camera
function resizeRenderer() {
const width = Math.max(1, sceneWrap.clientWidth);
const height = Math.max(1, sceneWrap.clientHeight);
const aspect = width / height;
const viewHeight = Math.max(9.5, 21.5 / aspect);
camera.left = (-viewHeight * aspect) / 2;
camera.right = (viewHeight * aspect) / 2;
camera.top = viewHeight / 2;
camera.bottom = -viewHeight / 2;
camera.updateProjectionMatrix();
renderer.setSize(width, height, false);
}
const resizeObserver = new ResizeObserver(resizeRenderer);
resizeObserver.observe(sceneWrap);
// Direct camera controls: no addons.
let pointerDown = false;
let previousX = 0;
let previousY = 0;
canvas.addEventListener("pointerdown", (event) => {
if (event.button !== 0) return;
pointerDown = true;
previousX = event.clientX;
previousY = event.clientY;
canvas.setPointerCapture(event.pointerId);
});
canvas.addEventListener("pointermove", (event) => {
if (!pointerDown) return;
const deltaX = event.clientX - previousX;
const deltaY = event.clientY - previousY;
previousX = event.clientX;
previousY = event.clientY;
cameraYaw -= deltaX * 0.006;
cameraPitch = THREE.MathUtils.clamp(cameraPitch + deltaY * 0.005, 0.24, 1.08);
updateCamera();
});
function endPointer(event) {
pointerDown = false;
if (canvas.hasPointerCapture(event.pointerId)) {
canvas.releasePointerCapture(event.pointerId);
}
}
canvas.addEventListener("pointerup", endPointer);
canvas.addEventListener("pointercancel", endPointer);
canvas.addEventListener("wheel", (event) => {
event.preventDefault();
cameraZoom = THREE.MathUtils.clamp(
cameraZoom * Math.exp(-event.deltaY * 0.001),
0.65,
1.75
);
updateCamera();
}, { passive: false });
canvas.addEventListener("dblclick", resetCamera);
$("resetViewBtn").addEventListener("click", resetCamera);
// -------------------------------------------------------------------------
// Interaction and updates
// -------------------------------------------------------------------------
function updateEverything() {
updateReadouts();
const expectedProbabilities = theoreticalProbabilities(recombination);
sampleSyntheticOffspring();
updateTable(expectedProbabilities);
updateChart(expectedProbabilities);
}
$("recombination").addEventListener("input", (event) => {
recombination = Number(event.target.value);
updateEverything();
});
$("sampleSize").addEventListener("change", (event) => {
sampleSize = Number(event.target.value);
updateEverything();
});
$("resampleBtn").addEventListener("click", () => {
currentSeed = (currentSeed + 1) >>> 0;
updateEverything();
});
buildTableRows();
resizeRenderer();
resetCamera();
updateEverything();
renderer.setAnimationLoop(() => {
renderer.render(scene, camera);
});
requestAnimationFrame(() => {
$("loading").classList.add("hidden");
});
</script>
</body>
</html>
```