Prompt specification
Build or customize the Three.js 3D scene "Two-way table area model" inside the active workspace.
Engineering Constraints:
1. Runtime & Orbit: Use Three.js r160 with OrbitControls (damping enabled) and ACESFilmicToneMapping.
2. Frame Timing: Drive all motion strictly with clock.getDelta() so animation speed is FPS-independent.
3. Viewport Hygiene: Handle window resize cleanly, cap DPR at Math.min(window.devicePixelRatio, 2), and dispose geometries/materials/renderer on unmount.
Scene Metadata:
Scene: Two-way table area model
Category: Algorithms & Systems · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/two-way-table-area-model-x1778
Scene Description Prompt:
Make a standalone HTML scene with Three.js from cdnjs that depicts conditional probabilities and their denominators. Build a partitioned rectangle whose cells match a synthetic contingency table; show row-conditional and column-conditional probabilities using explicitly different labeled denominators. Stylized solid forms.
Working Three.js Starter (two-way-table-area-model-x1778):
```html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Conditional Probabilities — Three.js</title>
<style>
:root {
color-scheme: light;
--ink: #18212b;
--muted: #627080;
--paper: #f2efe7;
--row: #0f8f80;
--column: #cf5846;
}
* { box-sizing: border-box; }
html, body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: var(--paper);
font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
color: var(--ink);
}
canvas {
position: fixed;
inset: 0;
display: block;
outline: none;
touch-action: none;
}
.intro {
position: fixed;
z-index: 5;
top: 26px;
left: 30px;
width: min(430px, calc(100vw - 60px));
pointer-events: none;
}
.eyebrow {
display: inline-flex;
align-items: center;
gap: 8px;
margin-bottom: 10px;
padding: 7px 10px;
border: 1px solid rgba(24, 33, 43, .16);
border-radius: 999px;
background: rgba(255, 255, 255, .58);
color: #52606c;
font-size: 10px;
font-weight: 800;
letter-spacing: .15em;
text-transform: uppercase;
backdrop-filter: blur(8px);
}
.eyebrow::before {
content: "";
width: 7px;
height: 7px;
border-radius: 50%;
background: #f0b33d;
box-shadow: 0 0 0 4px rgba(240, 179, 61, .18);
}
h1 {
margin: 0;
max-width: 390px;
font-size: clamp(27px, 3.5vw, 45px);
line-height: .98;
letter-spacing: -.045em;
}
.intro p {
max-width: 360px;
margin: 13px 0 0;
color: var(--muted);
font-size: 13px;
line-height: 1.5;
}
.legend {
position: fixed;
z-index: 5;
left: 30px;
bottom: 26px;
display: flex;
flex-wrap: wrap;
gap: 8px;
max-width: calc(100vw - 60px);
pointer-events: none;
}
.legend-item {
display: flex;
align-items: center;
gap: 9px;
padding: 9px 12px;
border: 1px solid rgba(24, 33, 43, .12);
border-radius: 10px;
background: rgba(255, 255, 255, .72);
box-shadow: 0 7px 24px rgba(40, 43, 45, .07);
color: #4e5b68;
font-size: 11px;
line-height: 1.25;
backdrop-filter: blur(8px);
}
.swatch {
width: 10px;
height: 25px;
flex: 0 0 auto;
border-radius: 4px;
}
.swatch.row { background: var(--row); }
.swatch.column { background: var(--column); }
.legend-item strong {
display: block;
color: var(--ink);
font-size: 12px;
}
.hint {
position: fixed;
z-index: 5;
right: 26px;
bottom: 28px;
padding: 8px 11px;
border: 1px solid rgba(24, 33, 43, .12);
border-radius: 999px;
background: rgba(255, 255, 255, .62);
color: #6a7480;
font-size: 10px;
font-weight: 700;
letter-spacing: .04em;
pointer-events: none;
backdrop-filter: blur(8px);
}
@media (max-width: 760px) {
.intro {
top: 18px;
left: 18px;
width: calc(100vw - 36px);
}
.intro p { display: none; }
.legend { left: 18px; bottom: 18px; }
.legend-item { padding: 7px 9px; }
.hint { display: none; }
}
</style>
</head>
<body>
<header class="intro">
<div class="eyebrow">Contingency table / conditional probability</div>
<h1>Same count.<br>Two denominators.</h1>
<p>
Select any solid cell. The two probability towers use the same numerator,
but condition on different margins of the table.
</p>
</header>
<div class="legend">
<div class="legend-item">
<span class="swatch row"></span>
<span>
<strong>Row-conditional</strong>
P(column | row), denominator = row total
</span>
</div>
<div class="legend-item">
<span class="swatch column"></span>
<span>
<strong>Column-conditional</strong>
P(row | column), denominator = column total
</span>
</div>
</div>
<div class="hint">DRAG TO ORBIT · WHEEL TO ZOOM · R TO RESET</div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// ------------------------------------------------------------
// Synthetic contingency table
// ------------------------------------------------------------
const table = [
[12, 8, 5],
[ 6, 9, 7],
[ 3, 7, 11]
];
const rowNames = ["R1", "R2", "R3"];
const colNames = ["C1", "C2", "C3"];
const rowTotals = table.map(row => row.reduce((a, b) => a + b, 0));
const colTotals = [0, 1, 2].map(c =>
table.reduce((sum, row) => sum + row[c], 0)
);
const grandTotal = rowTotals.reduce((a, b) => a + b, 0);
let selected = { r: 1, c: 1 };
// ------------------------------------------------------------
// Renderer, scene, camera
// ------------------------------------------------------------
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;
renderer.domElement.setAttribute(
"aria-label",
"Interactive three-dimensional contingency table"
);
document.body.prepend(renderer.domElement);
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xf2efe7);
scene.fog = new THREE.Fog(0xf2efe7, 28, 54);
const camera = new THREE.OrthographicCamera(-10, 10, 6, -6, 0.1, 100);
const cameraTarget = new THREE.Vector3(-0.25, 1.05, 0);
let azimuth = 0.32;
let polar = 0.76;
let cameraZoom = 1;
function updateCamera() {
const radius = 22;
camera.position.set(
cameraTarget.x + radius * Math.sin(polar) * Math.sin(azimuth),
cameraTarget.y + radius * Math.cos(polar),
cameraTarget.z + radius * Math.sin(polar) * Math.cos(azimuth)
);
camera.lookAt(cameraTarget);
camera.zoom = cameraZoom;
camera.updateProjectionMatrix();
}
function resize() {
const width = window.innerWidth;
const height = window.innerHeight;
const aspect = width / height;
// Preserve the full composition on narrow screens.
const viewHeight = Math.max(11.8, 21 / 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);
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
}
window.addEventListener("resize", resize);
resize();
updateCamera();
// ------------------------------------------------------------
// Lighting and floor
// ------------------------------------------------------------
scene.add(new THREE.HemisphereLight(0xffffff, 0x81786b, 2.1));
const keyLight = new THREE.DirectionalLight(0xfff5df, 3.1);
keyLight.position.set(-5, 13, 8);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(2048, 2048);
keyLight.shadow.camera.left = -14;
keyLight.shadow.camera.right = 14;
keyLight.shadow.camera.top = 12;
keyLight.shadow.camera.bottom = -12;
keyLight.shadow.camera.near = 1;
keyLight.shadow.camera.far = 35;
keyLight.shadow.bias = -0.00035;
scene.add(keyLight);
const fillLight = new THREE.DirectionalLight(0xa9d8ff, 0.65);
fillLight.position.set(8, 6, -8);
scene.add(fillLight);
const floor = new THREE.Mesh(
new THREE.PlaneGeometry(42, 30),
new THREE.MeshStandardMaterial({
color: 0xe9e5dc,
roughness: 0.96,
metalness: 0
})
);
floor.rotation.x = -Math.PI / 2;
floor.position.y = -0.03;
floor.receiveShadow = true;
scene.add(floor);
// ------------------------------------------------------------
// Canvas-based label helpers, using only Three.js core
// ------------------------------------------------------------
function roundedRectPath(ctx, x, y, width, height, radius) {
const r = Math.min(radius, width / 2, height / 2);
ctx.beginPath();
ctx.moveTo(x + r, y);
ctx.lineTo(x + width - r, y);
ctx.quadraticCurveTo(x + width, y, x + width, y + r);
ctx.lineTo(x + width, y + height - r);
ctx.quadraticCurveTo(
x + width, y + height, x + width - r, y + height
);
ctx.lineTo(x + r, y + height);
ctx.quadraticCurveTo(x, y + height, x, y + height - r);
ctx.lineTo(x, y + r);
ctx.quadraticCurveTo(x, y, x + r, y);
ctx.closePath();
}
function redrawTextSprite(sprite, text, options = {}) {
const canvas = sprite.userData.canvas;
const ctx = sprite.userData.ctx;
const width = canvas.width;
const height = canvas.height;
ctx.clearRect(0, 0, width, height);
const {
bg = null,
color = "#17212b",
fontSize = 58,
fontWeight = 700,
lineHeight = fontSize * 1.16,
align = "center",
padding = 22,
radius = 22,
border = null,
borderWidth = 3
} = options;
if (bg) {
roundedRectPath(ctx, 2, 2, width - 4, height - 4, radius);
ctx.fillStyle = bg;
ctx.fill();
}
if (border) {
roundedRectPath(
ctx,
borderWidth / 2,
borderWidth / 2,
width - borderWidth,
height - borderWidth,
radius
);
ctx.strokeStyle = border;
ctx.lineWidth = borderWidth;
ctx.stroke();
}
const lines = text.split("\n");
const blockHeight = lineHeight * (lines.length - 1) + fontSize;
let baseline = (height - blockHeight) / 2 + fontSize * 0.82;
ctx.fillStyle = color;
ctx.textBaseline = "middle";
ctx.textAlign = align;
ctx.font = `${fontWeight} ${fontSize}px Inter, system-ui, sans-serif`;
const x =
align === "left"
? padding
: align === "right"
? width - padding
: width / 2;
for (const line of lines) {
ctx.fillText(line, x, baseline, width - padding * 2);
baseline += lineHeight;
}
sprite.userData.texture.needsUpdate = true;
}
function makeTextSprite(text, options = {}) {
const canvas = document.createElement("canvas");
canvas.width = options.canvasWidth || 720;
canvas.height = options.canvasHeight || 180;
const ctx = canvas.getContext("2d");
const texture = new THREE.CanvasTexture(canvas);
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 = 12;
sprite.scale.set(
options.worldWidth || 3,
options.worldHeight || 0.75,
1
);
sprite.userData.canvas = canvas;
sprite.userData.ctx = ctx;
sprite.userData.texture = texture;
redrawTextSprite(sprite, text, options);
return sprite;
}
function makeCountBadge(count, accent) {
const canvas = document.createElement("canvas");
canvas.width = 256;
canvas.height = 256;
const ctx = canvas.getContext("2d");
ctx.clearRect(0, 0, 256, 256);
ctx.beginPath();
ctx.arc(128, 128, 92, 0, Math.PI * 2);
ctx.fillStyle = "rgba(255, 253, 246, .96)";
ctx.fill();
ctx.lineWidth = 12;
ctx.strokeStyle = `#${new THREE.Color(accent).getHexString()}`;
ctx.stroke();
ctx.fillStyle = "#17212b";
ctx.font = "800 104px Inter, system-ui, sans-serif";
ctx.textAlign = "center";
ctx.textBaseline = "middle";
ctx.fillText(String(count), 128, 133);
const texture = new THREE.CanvasTexture(canvas);
texture.colorSpace = THREE.SRGBColorSpace;
const sprite = new THREE.Sprite(
new THREE.SpriteMaterial({
map: texture,
transparent: true,
depthTest: false,
depthWrite: false
})
);
sprite.scale.set(0.48, 0.48, 1);
sprite.renderOrder = 18;
return sprite;
}
// ------------------------------------------------------------
// Contingency-table mosaic
//
// Column widths encode column totals.
// Row depths encode row totals.
// Cell height encodes the joint count.
// ------------------------------------------------------------
const tableGroup = new THREE.Group();
scene.add(tableGroup);
const boardWidth = 8.2;
const boardDepth = 5.4;
const boardX = -3.9;
const boardZ = 0;
const base = new THREE.Mesh(
new THREE.BoxGeometry(boardWidth + 0.25, 0.12, boardDepth + 0.25),
new THREE.MeshStandardMaterial({
color: 0x26313b,
roughness: 0.68,
metalness: 0.08
})
);
base.position.set(boardX, 0.045, boardZ);
base.castShadow = true;
base.receiveShadow = true;
tableGroup.add(base);
const rowPalette = [
[0x45d4bd, 0x2ab9a3, 0x15998a],
[0xffc36a, 0xf5a145, 0xdf8431],
[0xb49af8, 0x9477e8, 0x765acb]
];
const colEdges = [0];
for (let c = 0; c < 3; c++) {
colEdges.push(colEdges[c] + colTotals[c] / grandTotal);
}
const rowEdges = [0];
for (let r = 0; r < 3; r++) {
rowEdges.push(rowEdges[r] + rowTotals[r] / grandTotal);
}
const cells = [];
const cellMeshes = [];
for (let r = 0; r < 3; r++) {
for (let c = 0; c < 3; c++) {
const count = table[r][c];
const color = rowPalette[r][c];
const cellWidth = boardWidth * (colEdges[c + 1] - colEdges[c]);
const cellDepth = boardDepth * (rowEdges[r + 1] - rowEdges[r]);
const cellHeight = 0.14 + count * 0.105;
const x =
boardX - boardWidth / 2 +
(colEdges[c] + colEdges[c + 1]) * boardWidth / 2;
const z =
boardZ - boardDepth / 2 +
(rowEdges[r] + rowEdges[r + 1]) * boardDepth / 2;
const material = new THREE.MeshStandardMaterial({
color,
roughness: 0.48,
metalness: 0.04,
emissive: 0x000000,
emissiveIntensity: 0
});
const mesh = new THREE.Mesh(
new THREE.BoxGeometry(
cellWidth - 0.09,
cellHeight,
cellDepth - 0.09
),
material
);
mesh.position.set(x, 0.1 + cellHeight / 2, z);
mesh.castShadow = true;
mesh.receiveShadow = true;
mesh.userData = { r, c, count, color };
tableGroup.add(mesh);
const badge = makeCountBadge(count, color);
badge.position.set(x, 0.1 + cellHeight + 0.27, z);
tableGroup.add(badge);
cells.push({ mesh, badge, cellWidth, cellDepth, cellHeight, x, z });
cellMeshes.push(mesh);
}
}
// Table title and joint-count readout.
const tableTitle = makeTextSprite(
`SYNTHETIC 3×3 TABLE · CELL HEIGHT ∝ COUNT · N = ${grandTotal}`,
{
canvasWidth: 1100,
canvasHeight: 160,
worldWidth: 6.25,
worldHeight: 0.7,
fontSize: 46,
fontWeight: 800,
color: "#34414d",
bg: "rgba(255,255,255,.76)",
border: "rgba(24,33,43,.12)",
borderWidth: 3,
radius: 26
}
);
tableTitle.position.set(boardX, 3.45, -3.35);
tableGroup.add(tableTitle);
const jointReadout = makeTextSprite("", {
canvasWidth: 1150,
canvasHeight: 170,
worldWidth: 6.55,
worldHeight: 0.72,
fontSize: 43,
fontWeight: 750,
color: "#27333e",
bg: "rgba(255,255,255,.82)",
border: "rgba(24,33,43,.12)",
borderWidth: 3,
radius: 24
});
jointReadout.position.set(boardX, 0.38, 3.27);
tableGroup.add(jointReadout);
// Column headers.
for (let c = 0; c < 3; c++) {
const x =
boardX - boardWidth / 2 +
(colEdges[c] + colEdges[c + 1]) * boardWidth / 2;
const label = makeTextSprite(
`${colNames[c]}\nCOLUMN TOTAL ${colTotals[c]}`,
{
canvasWidth: 520,
canvasHeight: 190,
worldWidth: 2.15,
worldHeight: 0.78,
fontSize: 43,
lineHeight: 53,
fontWeight: 760,
color: "#43505c",
bg: "rgba(255,255,255,.66)",
radius: 24
}
);
label.position.set(x, 0.48, -3.05);
tableGroup.add(label);
}
// Row headers.
for (let r = 0; r < 3; r++) {
const z =
boardZ - boardDepth / 2 +
(rowEdges[r] + rowEdges[r + 1]) * boardDepth / 2;
const label = makeTextSprite(
`${rowNames[r]}\nROW TOTAL ${rowTotals[r]}`,
{
canvasWidth: 520,
canvasHeight: 190,
worldWidth: 2.15,
worldHeight: 0.78,
fontSize: 43,
lineHeight: 53,
fontWeight: 760,
color: "#43505c",
bg: "rgba(255,255,255,.66)",
radius: 24
}
);
label.position.set(-8.95, 0.58, z);
tableGroup.add(label);
}
const selectedOutline = new THREE.BoxHelper(cellMeshes[0], 0xffd166);
selectedOutline.material.depthTest = false;
selectedOutline.material.transparent = true;
selectedOutline.material.opacity = 1;
selectedOutline.renderOrder = 25;
scene.add(selectedOutline);
// ------------------------------------------------------------
// Conditional probability panels and solid towers
// ------------------------------------------------------------
function makeConditionalPanel(accent, x) {
const canvas = document.createElement("canvas");
canvas.width = 900;
canvas.height = 280;
const ctx = canvas.getContext("2d");
const texture = new THREE.CanvasTexture(canvas);
texture.colorSpace = THREE.SRGBColorSpace;
texture.minFilter = THREE.LinearFilter;
texture.magFilter = THREE.LinearFilter;
const sprite = new THREE.Sprite(
new THREE.SpriteMaterial({
map: texture,
transparent: true,
depthTest: false,
depthWrite: false
})
);
sprite.scale.set(3.35, 1.04, 1);
sprite.position.set(x, 4.45, 0.25);
sprite.renderOrder = 22;
scene.add(sprite);
return { canvas, ctx, texture, sprite };
}
function drawConditionalPanel(panel, accent, title, formula, denominator) {
const { ctx, texture } = panel;
ctx.clearRect(0, 0, 900, 280);
roundedRectPath(ctx, 4, 4, 892, 272, 28);
ctx.fillStyle = "#fffdf8";
ctx.fill();
ctx.strokeStyle = accent;
ctx.lineWidth = 5;
ctx.stroke();
ctx.textAlign = "left";
ctx.textBaseline = "middle";
ctx.fillStyle = accent;
ctx.font = "800 27px Inter, system-ui, sans-serif";
ctx.fillText(title, 31, 42);
ctx.fillStyle = "#17212b";
ctx.font = "800 42px Inter, system-ui, sans-serif";
ctx.fillText(formula, 31, 103);
roundedRectPath(ctx, 24, 151, 852, 100, 20);
ctx.fillStyle = accent;
ctx.fill();
ctx.fillStyle = "#ffffff";
ctx.font = "750 27px Inter, system-ui, sans-serif";
ctx.fillText(denominator, 43, 202);
texture.needsUpdate = true;
}
function makeTower(x, accent, lighterAccent) {
const group = new THREE.Group();
group.position.set(x, 0, 0.2);
scene.add(group);
const track = new THREE.Mesh(
new THREE.CylinderGeometry(0.84, 0.84, 3.8, 48, 1, true),
new THREE.MeshStandardMaterial({
color: 0xffffff,
roughness: 1,
transparent: true,
opacity: 0.17,
depthWrite: false
})
);
track.position.y = 2.03;
group.add(track);
const base = new THREE.Mesh(
new THREE.CylinderGeometry(0.98, 1.02, 0.14, 48),
new THREE.MeshStandardMaterial({
color: accent,
roughness: 0.58,
metalness: 0.04
})
);
base.position.y = 0.07;
base.castShadow = true;
group.add(base);
const bar = new THREE.Mesh(
new THREE.CylinderGeometry(0.68, 0.68, 1, 48),
new THREE.MeshStandardMaterial({
color: accent,
roughness: 0.42,
metalness: 0.06
})
);
bar.castShadow = true;
group.add(bar);
const cap = new THREE.Mesh(
new THREE.CylinderGeometry(0.72, 0.72, 0.08, 48),
new THREE.MeshStandardMaterial({
color: lighterAccent,
roughness: 0.4
})
);
cap.castShadow = true;
group.add(cap);
return { group, bar, cap };
}
const rowAccent = "#0f8f80";
const rowAccentLight = "#55d5c5";
const colAccent = "#cf5846";
const colAccentLight = "#f28b72";
const rowPanel = makeConditionalPanel(rowAccent, 2.8);
const colPanel = makeConditionalPanel(colAccent, 6.5);
const rowTower = makeTower(2.8, 0x0f8f80, 0x55d5c5);
const colTower = makeTower(6.5, 0xcf5846, 0xf28b72);
// Denominator pedestals and explicit labels.
const rowDenomPedestal = new THREE.Mesh(
new THREE.BoxGeometry(2.75, 0.14, 0.62),
new THREE.MeshStandardMaterial({
color: 0x0b6d62,
roughness: 0.62
})
);
rowDenomPedestal.position.set(2.8, 0.12, 1.48);
rowDenomPedestal.castShadow = true;
scene.add(rowDenomPedestal);
const colDenomPedestal = rowDenomPedestal.clone();
colDenomPedestal.material = new THREE.MeshStandardMaterial({
color: 0xa83f31,
roughness: 0.62
});
colDenomPedestal.position.x = 6.5;
scene.add(colDenomPedestal);
const rowDenominatorLabel = makeTextSprite("", {
canvasWidth: 760,
canvasHeight: 220,
worldWidth: 3.0,
worldHeight: 0.87,
fontSize: 39,
lineHeight: 53,
fontWeight: 780,
color: "#ffffff",
bg: rowAccent,
radius: 24
});
rowDenominatorLabel.position.set(2.8, 0.68, 1.48);
scene.add(rowDenominatorLabel);
const colDenominatorLabel = makeTextSprite("", {
canvasWidth: 760,
canvasHeight: 220,
worldWidth: 3.0,
worldHeight: 0.87,
fontSize: 39,
lineHeight: 53,
fontWeight: 780,
color: "#ffffff",
bg: colAccent,
radius: 24
});
colDenominatorLabel.position.set(6.5, 0.68, 1.48);
scene.add(colDenominatorLabel);
// ------------------------------------------------------------
// Selection and probability updates
// ------------------------------------------------------------
function formatPercent(value) {
return `${(value * 100).toFixed(1)}%`;
}
function updateTower(tower, probability) {
const height = Math.max(0.07, probability * 3.8);
tower.bar.scale.y = height;
tower.bar.position.y = 0.14 + height / 2;
tower.cap.position.y = 0.14 + height + 0.04;
}
function updateCellEmphasis(hoveredCell = null) {
for (const cell of cells) {
const isSelected =
cell.mesh.userData.r === selected.r &&
cell.mesh.userData.c === selected.c;
const isHovered = cell === hoveredCell;
cell.mesh.material.emissive.set(
isSelected ? 0x7a4b08 : isHovered ? 0x27333e : 0x000000
);
cell.mesh.material.emissiveIntensity =
isSelected ? 0.27 : isHovered ? 0.12 : 0;
}
}
function selectCell(r, c) {
selected = { r, c };
const count = table[r][c];
const rowTotal = rowTotals[r];
const colTotal = colTotals[c];
const rowProbability = count / rowTotal;
const colProbability = count / colTotal;
const jointProbability = count / grandTotal;
updateTower(rowTower, rowProbability);
updateTower(colTower, colProbability);
drawConditionalPanel(
rowPanel,
rowAccent,
`ROW-CONDITIONAL · P(${colNames[c]} | ${rowNames[r]})`,
`${count} / ${rowTotal} = ${formatPercent(rowProbability)}`,
`DENOMINATOR: ${rowNames[r]} ROW TOTAL = ${rowTotal}`
);
drawConditionalPanel(
colPanel,
colAccent,
`COLUMN-CONDITIONAL · P(${rowNames[r]} | ${colNames[c]})`,
`${count} / ${colTotal} = ${formatPercent(colProbability)}`,
`DENOMINATOR: ${colNames[c]} COLUMN TOTAL = ${colTotal}`
);
redrawTextSprite(
rowDenominatorLabel,
`DENOMINATOR\n${rowNames[r]} ROW TOTAL = ${rowTotal}`,
{
fontSize: 39,
lineHeight: 53,
fontWeight: 780,
color: "#ffffff",
bg: rowAccent,
radius: 24
}
);
redrawTextSprite(
colDenominatorLabel,
`DENOMINATOR\n${colNames[c]} COLUMN TOTAL = ${colTotal}`,
{
fontSize: 39,
lineHeight: 53,
fontWeight: 780,
color: "#ffffff",
bg: colAccent,
radius: 24
}
);
redrawTextSprite(
jointReadout,
`SELECTED ${rowNames[r]} × ${colNames[c]} · COUNT ${count} · JOINT ${count}/${grandTotal} = ${formatPercent(jointProbability)}`,
{
fontSize: 43,
fontWeight: 750,
color: "#27333e",
bg: "rgba(255,255,255,.82)",
border: "rgba(24,33,43,.12)",
borderWidth: 3,
radius: 24
}
);
const selectedCell = cells.find(
cell =>
cell.mesh.userData.r === r &&
cell.mesh.userData.c === c
);
selectedOutline.setFromObject(selectedCell.mesh);
selectedOutline.update();
updateCellEmphasis(hoveredCell);
}
// ------------------------------------------------------------
// Direct pointer controls and cell picking
// ------------------------------------------------------------
const raycaster = new THREE.Raycaster();
const pointer = new THREE.Vector2();
let dragging = false;
let lastX = 0;
let lastY = 0;
let travel = 0;
let hoveredCell = null;
function setPointerFromEvent(event) {
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;
}
function pickCell(event) {
setPointerFromEvent(event);
raycaster.setFromCamera(pointer, camera);
const hit = raycaster.intersectObjects(cellMeshes, false)[0];
if (!hit) return null;
const { r, c } = hit.object.userData;
return cells.find(
cell => cell.mesh.userData.r === r && cell.mesh.userData.c === c
);
}
renderer.domElement.addEventListener("pointerdown", event => {
if (event.button !== 0) return;
dragging = true;
travel = 0;
lastX = event.clientX;
lastY = event.clientY;
renderer.domElement.setPointerCapture(event.pointerId);
});
renderer.domElement.addEventListener("pointermove", event => {
if (dragging) {
const dx = event.clientX - lastX;
const dy = event.clientY - lastY;
travel += Math.hypot(dx, dy);
azimuth -= dx * 0.006;
polar = THREE.MathUtils.clamp(polar + dy * 0.005, 0.42, 1.18);
lastX = event.clientX;
lastY = event.clientY;
updateCamera();
} else {
const nextHovered = pickCell(event);
if (nextHovered !== hoveredCell) {
hoveredCell = nextHovered;
updateCellEmphasis(hoveredCell);
renderer.domElement.style.cursor = hoveredCell ? "pointer" : "grab";
}
}
});
renderer.domElement.addEventListener("pointerup", event => {
if (!dragging) return;
dragging = false;
renderer.domElement.releasePointerCapture(event.pointerId);
if (travel < 6) {
const cell = pickCell(event);
if (cell) {
selectCell(cell.mesh.userData.r, cell.mesh.userData.c);
}
}
renderer.domElement.style.cursor = hoveredCell ? "pointer" : "grab";
});
renderer.domElement.addEventListener("pointercancel", () => {
dragging = false;
});
renderer.domElement.addEventListener(
"wheel",
event => {
event.preventDefault();
cameraZoom = THREE.MathUtils.clamp(
cameraZoom * Math.exp(-event.deltaY * 0.001),
0.72,
2.1
);
updateCamera();
},
{ passive: false }
);
window.addEventListener("keydown", event => {
if (event.key.toLowerCase() === "r") {
azimuth = 0.32;
polar = 0.76;
cameraZoom = 1;
updateCamera();
}
if (event.key === "ArrowRight") {
selected.c = (selected.c + 1) % 3;
selectCell(selected.r, selected.c);
}
if (event.key === "ArrowLeft") {
selected.c = (selected.c + 2) % 3;
selectCell(selected.r, selected.c);
}
if (event.key === "ArrowDown") {
selected.r = (selected.r + 1) % 3;
selectCell(selected.r, selected.c);
}
if (event.key === "ArrowUp") {
selected.r = (selected.r + 2) % 3;
selectCell(selected.r, selected.c);
}
});
// Initial state.
selectCell(selected.r, selected.c);
renderer.domElement.style.cursor = "grab";
// Subtle selection pulse.
const clock = new THREE.Clock();
renderer.setAnimationLoop(() => {
const t = clock.getElapsedTime();
selectedOutline.material.opacity = 0.78 + Math.sin(t * 3.2) * 0.18;
const selectedCell = cells.find(
cell =>
cell.mesh.userData.r === selected.r &&
cell.mesh.userData.c === selected.c
);
if (selectedCell && !dragging) {
selectedCell.mesh.material.emissiveIntensity =
0.25 + Math.sin(t * 3.2) * 0.045;
}
renderer.render(scene, camera);
});
</script>
</body>
</html>
```