Prompt specification
Build or customize the Three.js 3D scene "Three Proposal Boxes and Ballot Tokens" 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: Three Proposal Boxes and Ballot Tokens
Category: Art & Culture · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/three-proposal-boxes-and-ballot-tokens-x3205
Scene Description Prompt:
Create a standalone low-poly scene in one HTML file using Three.js from cdnjs. Model three project boxes with unmarked ballot tokens on a tabletop. Distinguish a voter's preference ranking from the number of tokens physically placed in a box.
Working Three.js Starter (three-proposal-boxes-and-ballot-tokens-x3205):
```html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>The Ballot Table — Order ≠ Quantity</title>
<style>
:root {
--ink: #252522;
--muted: #6f6c64;
--paper: rgba(255, 253, 247, 0.94);
--line: rgba(37, 37, 34, 0.12);
--project-a: #3b82f6;
--project-b: #f97352;
--project-c: #22a06b;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #e8e1d6;
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%;
cursor: grab;
touch-action: none;
outline: none;
}
#scene canvas.dragging {
cursor: grabbing;
}
#labels {
position: fixed;
inset: 0;
pointer-events: none;
overflow: hidden;
z-index: 2;
}
.box-label {
position: absolute;
min-width: 116px;
padding: 8px 11px;
border: 1px solid rgba(37, 37, 34, 0.12);
border-radius: 9px;
background: rgba(255, 253, 247, 0.91);
box-shadow: 0 7px 24px rgba(44, 39, 31, 0.1);
text-align: center;
transform: translate(-50%, -100%);
transition:
border-color 160ms ease,
box-shadow 160ms ease,
opacity 160ms ease;
}
.box-label::after {
content: "";
position: absolute;
left: 50%;
bottom: -6px;
width: 10px;
height: 10px;
background: rgba(255, 253, 247, 0.95);
border-right: 1px solid rgba(37, 37, 34, 0.12);
border-bottom: 1px solid rgba(37, 37, 34, 0.12);
transform: translateX(-50%) rotate(45deg);
}
.box-label strong {
display: block;
font-size: 12px;
letter-spacing: 0.02em;
}
.box-label span {
display: block;
margin-top: 2px;
color: var(--muted);
font-size: 10px;
font-weight: 700;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.box-label.active {
border-color: rgba(37, 37, 34, 0.36);
box-shadow:
0 9px 28px rgba(44, 39, 31, 0.15),
0 0 0 2px rgba(255, 253, 247, 0.7);
}
.brand {
position: fixed;
top: 32px;
left: 36px;
z-index: 4;
width: min(450px, calc(100vw - 420px));
pointer-events: none;
}
.eyebrow {
display: flex;
align-items: center;
gap: 9px;
margin-bottom: 13px;
color: var(--muted);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.18em;
text-transform: uppercase;
}
.eyebrow::before {
content: "";
width: 28px;
height: 2px;
background: var(--ink);
}
h1 {
margin: 0;
font-family: Georgia, "Times New Roman", serif;
font-size: clamp(38px, 5vw, 68px);
font-weight: 500;
line-height: 0.95;
letter-spacing: -0.055em;
}
h1 em {
color: #777168;
font-weight: 400;
}
.brand p {
max-width: 390px;
margin: 17px 0 0;
color: #625f58;
font-size: 13px;
line-height: 1.6;
}
.panel {
position: fixed;
top: 24px;
right: 24px;
z-index: 5;
width: 340px;
max-height: calc(100vh - 48px);
overflow: auto;
padding: 20px;
border: 1px solid rgba(37, 37, 34, 0.12);
border-radius: 17px;
background: var(--paper);
box-shadow:
0 18px 55px rgba(53, 47, 37, 0.14),
inset 0 1px rgba(255, 255, 255, 0.85);
backdrop-filter: blur(14px);
}
.panel-intro {
margin: 0 0 17px;
color: var(--muted);
font-size: 12px;
line-height: 1.55;
}
.measure {
padding: 17px 0;
border-top: 1px solid var(--line);
}
.measure:last-of-type {
padding-bottom: 12px;
}
.measure-heading {
display: flex;
align-items: center;
justify-content: space-between;
gap: 12px;
margin-bottom: 12px;
}
.measure-heading h2 {
margin: 0;
font-size: 12px;
font-weight: 800;
letter-spacing: 0.06em;
text-transform: uppercase;
}
.measure-type {
flex: 0 0 auto;
padding: 4px 7px;
border-radius: 999px;
background: #eee9df;
color: #777168;
font-size: 8px;
font-weight: 900;
letter-spacing: 0.12em;
text-transform: uppercase;
}
.ranking {
display: grid;
gap: 7px;
margin: 0;
padding: 0;
list-style: none;
}
.ranking li {
display: grid;
grid-template-columns: 28px 10px 1fr auto;
align-items: center;
gap: 9px;
min-height: 34px;
padding: 7px 9px;
border: 1px solid rgba(37, 37, 34, 0.08);
border-radius: 8px;
background: rgba(255, 255, 255, 0.48);
}
.rank-number {
color: #8b877e;
font-family: Georgia, "Times New Roman", serif;
font-size: 18px;
}
.dot {
display: block;
width: 9px;
height: 9px;
border-radius: 50%;
background: var(--dot);
box-shadow: 0 0 0 3px color-mix(in srgb, var(--dot) 15%, transparent);
}
.project-name {
font-size: 12px;
font-weight: 750;
}
.rank-label {
color: #8b877e;
font-size: 9px;
font-weight: 800;
letter-spacing: 0.1em;
text-transform: uppercase;
}
.token-list {
display: grid;
gap: 6px;
}
.token-row {
display: grid;
grid-template-columns: 10px 1fr auto auto;
align-items: center;
gap: 9px;
width: 100%;
padding: 9px 10px;
border: 1px solid rgba(37, 37, 34, 0.08);
border-radius: 8px;
background: rgba(255, 255, 255, 0.48);
color: var(--ink);
font: inherit;
text-align: left;
cursor: pointer;
transition:
background 140ms ease,
border-color 140ms ease,
transform 140ms ease;
}
.token-row:hover,
.token-row.active {
border-color: rgba(37, 37, 34, 0.28);
background: rgba(255, 255, 255, 0.9);
transform: translateX(-2px);
}
.token-row strong {
font-family: Georgia, "Times New Roman", serif;
font-size: 21px;
font-weight: 500;
line-height: 1;
}
.token-unit {
color: #8b877e;
font-size: 9px;
font-weight: 800;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.note {
display: flex;
align-items: flex-start;
gap: 9px;
margin: 5px 0 0;
padding-top: 14px;
border-top: 1px solid var(--line);
color: var(--muted);
font-size: 10px;
line-height: 1.55;
}
.note-mark {
flex: 0 0 auto;
display: grid;
place-items: center;
width: 18px;
height: 18px;
border: 1px solid #aaa49a;
border-radius: 50%;
color: #777168;
font-size: 11px;
font-weight: 800;
}
.panel-footer {
display: flex;
align-items: center;
justify-content: space-between;
gap: 10px;
margin-top: 16px;
}
#status {
color: var(--muted);
font-size: 9px;
line-height: 1.4;
}
button {
appearance: none;
border: 1px solid rgba(37, 37, 34, 0.16);
border-radius: 999px;
padding: 7px 10px;
background: transparent;
color: var(--ink);
font: inherit;
font-size: 9px;
font-weight: 800;
letter-spacing: 0.06em;
text-transform: uppercase;
cursor: pointer;
}
button:hover {
background: #eee9df;
}
.controls-hint {
position: fixed;
bottom: 28px;
left: 36px;
z-index: 4;
display: flex;
align-items: center;
gap: 10px;
color: #68645c;
font-size: 10px;
font-weight: 650;
letter-spacing: 0.03em;
pointer-events: none;
}
.controls-hint span {
display: inline-block;
width: 5px;
height: 5px;
border-radius: 50%;
background: #777168;
}
@media (max-width: 800px) {
.brand {
top: 18px;
left: 18px;
width: calc(100vw - 36px);
}
.brand p {
display: none;
}
.brand h1 {
font-size: 38px;
}
.panel {
top: auto;
right: 12px;
bottom: 12px;
left: 12px;
width: auto;
max-height: 245px;
display: grid;
grid-template-columns: 1fr 1fr;
gap: 18px;
padding: 15px;
}
.panel-intro {
display: none;
}
.measure {
padding: 12px 0 0;
border-top: 1px solid var(--line);
}
.measure-heading {
margin-bottom: 8px;
}
.measure-heading h2 {
font-size: 10px;
}
.ranking li {
min-height: 29px;
padding: 5px 7px;
}
.token-row {
padding: 7px 8px;
}
.note {
grid-column: 1 / -1;
padding-top: 10px;
}
.panel-footer {
grid-column: 1 / -1;
margin-top: 10px;
}
.controls-hint {
display: none;
}
.box-label {
min-width: 96px;
padding: 6px 8px;
}
}
@media (max-width: 480px) {
.panel {
grid-template-columns: 1fr;
max-height: 255px;
gap: 10px;
}
.measure {
padding-top: 8px;
}
.note {
display: none;
}
.ranking {
grid-template-columns: repeat(3, 1fr);
}
.ranking li {
grid-template-columns: 22px 8px 1fr;
}
.rank-label {
display: none;
}
.token-list {
grid-template-columns: repeat(3, 1fr);
}
.token-row {
grid-template-columns: 8px 1fr;
}
.token-row strong,
.token-unit {
display: none;
}
}
</style>
</head>
<body>
<main id="scene" aria-label="Interactive low-poly ballot scene"></main>
<div id="labels" aria-hidden="true"></div>
<header class="brand">
<div class="eyebrow">The Ballot Table · Three Projects</div>
<h1>Order <em>≠</em><br>quantity.</h1>
<p>
A sample ballot records one voter’s preference order.
The three boxes contain pooled, physically deposited tokens.
</p>
</header>
<aside class="panel" aria-label="Ballot measures">
<p class="panel-intro">
These two panels describe different things: an ordinal ranking
and the cardinal quantity of tokens in each box.
</p>
<section class="measure">
<div class="measure-heading">
<h2>Preference ranking</h2>
<span class="measure-type">Ordinal · one voter</span>
</div>
<ol class="ranking">
<li>
<span class="rank-number">1</span>
<i class="dot" style="--dot: var(--project-a)"></i>
<span class="project-name">Project A</span>
<span class="rank-label">First</span>
</li>
<li>
<span class="rank-number">2</span>
<i class="dot" style="--dot: var(--project-c)"></i>
<span class="project-name">Project C</span>
<span class="rank-label">Second</span>
</li>
<li>
<span class="rank-number">3</span>
<i class="dot" style="--dot: var(--project-b)"></i>
<span class="project-name">Project B</span>
<span class="rank-label">Third</span>
</li>
</ol>
</section>
<section class="measure">
<div class="measure-heading">
<h2>Tokens physically placed</h2>
<span class="measure-type">Cardinal · all voters</span>
</div>
<div class="token-list">
<button class="token-row" data-project="A" type="button">
<i class="dot" style="--dot: var(--project-a)"></i>
<span class="project-name">Project A</span>
<strong>6</strong>
<span class="token-unit">tokens</span>
</button>
<button class="token-row" data-project="B" type="button">
<i class="dot" style="--dot: var(--project-b)"></i>
<span class="project-name">Project B</span>
<strong>4</strong>
<span class="token-unit">tokens</span>
</button>
<button class="token-row" data-project="C" type="button">
<i class="dot" style="--dot: var(--project-c)"></i>
<span class="project-name">Project C</span>
<strong>2</strong>
<span class="token-unit">tokens</span>
</button>
</div>
</section>
<p class="note">
<span class="note-mark">i</span>
<span>
Changing a preference order would not move a token.
Here, C ranks above B for the sample voter, while B currently
contains more pooled tokens.
</span>
</p>
<div class="panel-footer">
<div id="status" aria-live="polite">Project A selected · 6 tokens</div>
<button id="reset-view" type="button">Reset view</button>
</div>
</aside>
<div class="controls-hint">
<span></span>
Drag to orbit · Scroll to zoom · Select a box
</div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
const sceneHost = document.querySelector("#scene");
const labelHost = document.querySelector("#labels");
const status = document.querySelector("#status");
const resetButton = document.querySelector("#reset-view");
const projects = [
{
id: "A",
name: "Project A",
color: 0x3b82f6,
css: "#3b82f6",
count: 6,
x: -2.15
},
{
id: "B",
name: "Project B",
color: 0xf97352,
css: "#f97352",
count: 4,
x: 0
},
{
id: "C",
name: "Project C",
color: 0x22a06b,
css: "#22a06b",
count: 2,
x: 2.15
}
];
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xe8e1d6);
scene.fog = new THREE.Fog(0xe8e1d6, 15, 29);
const camera = new THREE.PerspectiveCamera(
39,
window.innerWidth / window.innerHeight,
0.1,
60
);
const renderer = new THREE.WebGLRenderer({
antialias: true,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.08;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
sceneHost.appendChild(renderer.domElement);
renderer.domElement.tabIndex = 0;
renderer.domElement.setAttribute(
"aria-label",
"Three-dimensional scene. Drag to orbit, scroll to zoom, and select a project box."
);
// ------------------------------------------------------------------
// Lighting
// ------------------------------------------------------------------
const hemisphere = new THREE.HemisphereLight(0xfff8e9, 0x77756f, 1.8);
scene.add(hemisphere);
const keyLight = new THREE.DirectionalLight(0xfff3d9, 3.2);
keyLight.position.set(-4.5, 8.5, 6.5);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(2048, 2048);
keyLight.shadow.camera.left = -8;
keyLight.shadow.camera.right = 8;
keyLight.shadow.camera.top = 8;
keyLight.shadow.camera.bottom = -8;
keyLight.shadow.camera.near = 1;
keyLight.shadow.camera.far = 24;
keyLight.shadow.normalBias = 0.035;
scene.add(keyLight);
const fillLight = new THREE.DirectionalLight(0xbdd8ff, 0.9);
fillLight.position.set(6, 4, -5);
scene.add(fillLight);
// ------------------------------------------------------------------
// Materials
// ------------------------------------------------------------------
const floorMaterial = new THREE.MeshStandardMaterial({
color: 0xd8d0c4,
roughness: 1,
flatShading: true
});
const tableMaterial = new THREE.MeshStandardMaterial({
color: 0xb98f67,
roughness: 0.92,
flatShading: true
});
const tableEdgeMaterial = new THREE.MeshStandardMaterial({
color: 0x866043,
roughness: 1,
flatShading: true
});
const seamMaterial = new THREE.MeshBasicMaterial({
color: 0x8f694b,
transparent: true,
opacity: 0.28
});
// Plain tokens: intentionally unmarked, with no symbols or lettering.
const tokenMaterial = new THREE.MeshStandardMaterial({
color: 0xf2d49c,
roughness: 0.78,
metalness: 0,
flatShading: true
});
const tokenGeometry = new THREE.CylinderGeometry(
0.18,
0.18,
0.055,
10,
1,
false
);
// ------------------------------------------------------------------
// Floor and tabletop
// ------------------------------------------------------------------
const floor = new THREE.Mesh(
new THREE.PlaneGeometry(40, 40),
floorMaterial
);
floor.rotation.x = -Math.PI / 2;
floor.position.y = -0.48;
floor.receiveShadow = true;
scene.add(floor);
const table = new THREE.Mesh(
new THREE.BoxGeometry(9.2, 0.45, 5.2),
tableMaterial
);
table.position.y = -0.25;
table.castShadow = true;
table.receiveShadow = true;
scene.add(table);
const tableEdge = new THREE.Mesh(
new THREE.BoxGeometry(9.26, 0.12, 5.26),
tableEdgeMaterial
);
tableEdge.position.y = -0.43;
tableEdge.castShadow = true;
tableEdge.receiveShadow = true;
scene.add(tableEdge);
for (const z of [-1.55, 0, 1.55]) {
const seam = new THREE.Mesh(
new THREE.BoxGeometry(9.1, 0.008, 0.018),
seamMaterial
);
seam.position.set(0, -0.019, z);
scene.add(seam);
}
// ------------------------------------------------------------------
// Project box construction
// ------------------------------------------------------------------
function createPlaqueTexture(letter, color) {
const canvas = document.createElement("canvas");
canvas.width = 256;
canvas.height = 256;
const context = canvas.getContext("2d");
context.fillStyle = color;
context.fillRect(0, 0, 256, 256);
context.fillStyle = "rgba(255,255,255,0.18)";
context.fillRect(0, 218, 256, 38);
context.fillStyle = "#ffffff";
context.textAlign = "center";
context.textBaseline = "middle";
context.font = "700 138px Georgia, serif";
context.fillText(letter, 128, 117);
const texture = new THREE.CanvasTexture(canvas);
texture.colorSpace = THREE.SRGBColorSpace;
texture.anisotropy = renderer.capabilities.getMaxAnisotropy();
return texture;
}
function createProjectBox(project) {
const group = new THREE.Group();
group.position.set(project.x, 0, -0.32);
group.userData.project = project.id;
const accent = new THREE.Color(project.color);
const paleBody = accent.clone().lerp(new THREE.Color(0xf5f0e7), 0.76);
const bodyMaterial = new THREE.MeshStandardMaterial({
color: paleBody,
roughness: 0.9,
flatShading: true
});
const trimMaterial = new THREE.MeshStandardMaterial({
color: project.color,
roughness: 0.72,
flatShading: true
});
function addPart(geometry, material, x, y, z) {
const mesh = new THREE.Mesh(geometry, material);
mesh.position.set(x, y, z);
mesh.castShadow = true;
mesh.receiveShadow = true;
group.add(mesh);
return mesh;
}
// Bottom.
addPart(
new THREE.BoxGeometry(1.55, 0.1, 1.3),
bodyMaterial,
0,
0.05,
0
);
// Side walls.
addPart(
new THREE.BoxGeometry(0.1, 0.66, 1.3),
bodyMaterial,
-0.725,
0.43,
0
);
addPart(
new THREE.BoxGeometry(0.1, 0.66, 1.3),
bodyMaterial,
0.725,
0.43,
0
);
// Taller rear wall.
addPart(
new THREE.BoxGeometry(1.55, 0.66, 0.1),
bodyMaterial,
0,
0.43,
-0.6
);
// Lower front wall keeps the contents visible.
addPart(
new THREE.BoxGeometry(1.55, 0.3, 0.1),
bodyMaterial,
0,
0.25,
0.6
);
// Colored trim makes each project immediately identifiable.
addPart(
new THREE.BoxGeometry(0.105, 0.045, 1.31),
trimMaterial,
-0.725,
0.765,
0
);
addPart(
new THREE.BoxGeometry(0.105, 0.045, 1.31),
trimMaterial,
0.725,
0.765,
0
);
addPart(
new THREE.BoxGeometry(1.55, 0.045, 0.105),
trimMaterial,
0,
0.765,
-0.6
);
addPart(
new THREE.BoxGeometry(1.55, 0.045, 0.105),
trimMaterial,
0,
0.505,
0.6
);
// Front plaque.
const plaqueMaterial = new THREE.MeshBasicMaterial({
map: createPlaqueTexture(project.id, project.css),
toneMapped: false
});
const plaque = new THREE.Mesh(
new THREE.PlaneGeometry(0.4, 0.32),
plaqueMaterial
);
plaque.position.set(0, 0.24, 0.656);
group.add(plaque);
// Every token is a separate physical mesh.
// Counts: A = 6, B = 4, C = 2.
for (let i = 0; i < project.count; i++) {
const layer = Math.floor(i / 2);
const slot = i % 2;
const token = new THREE.Mesh(tokenGeometry, tokenMaterial);
token.position.set(
slot === 0 ? -0.255 : 0.245,
0.145 + layer * 0.059,
layer % 2 === 0 ? -0.075 : 0.105 + slot * 0.018
);
token.rotation.y = (i % 3) * 0.21;
token.castShadow = true;
token.receiveShadow = true;
group.add(token);
}
scene.add(group);
return group;
}
const boxGroups = new Map();
const boxLabels = new Map();
for (const project of projects) {
const group = createProjectBox(project);
boxGroups.set(project.id, group);
const label = document.createElement("div");
label.className = "box-label";
label.innerHTML = `
<strong>${project.name}</strong>
<span>${project.count} plain tokens</span>
`;
labelHost.appendChild(label);
boxLabels.set(project.id, label);
}
// ------------------------------------------------------------------
// Sample ranked ballot lying on the tabletop
// ------------------------------------------------------------------
function createBallotTexture() {
const canvas = document.createElement("canvas");
canvas.width = 1024;
canvas.height = 640;
const context = canvas.getContext("2d");
context.fillStyle = "#fbf7ed";
context.fillRect(0, 0, 1024, 640);
context.strokeStyle = "#d9d0bf";
context.lineWidth = 8;
context.strokeRect(18, 18, 988, 604);
context.fillStyle = "#777168";
context.font = "700 27px Arial, sans-serif";
context.letterSpacing = "3px";
context.fillText("SAMPLE BALLOT · ONE VOTER", 65, 82);
context.fillStyle = "#252522";
context.font = "500 50px Georgia, serif";
context.fillText("Preference order", 62, 151);
const rows = [
{ rank: "1", id: "A", color: "#3b82f6" },
{ rank: "2", id: "C", color: "#22a06b" },
{ rank: "3", id: "B", color: "#f97352" }
];
rows.forEach((row, index) => {
const y = 225 + index * 119;
context.fillStyle = row.color;
context.beginPath();
context.arc(94, y - 10, 22, 0, Math.PI * 2);
context.fill();
context.fillStyle = "#252522";
context.font = "700 35px Georgia, serif";
context.fillText(row.rank, 141, y);
context.fillStyle = "#4d4942";
context.font = "700 32px Arial, sans-serif";
context.fillText(`Project ${row.id}`, 200, y);
});
context.fillStyle = "#8b877e";
context.font = "24px Arial, sans-serif";
context.fillText("Order only — this ballot contains no tokens.", 62, 580);
const texture = new THREE.CanvasTexture(canvas);
texture.colorSpace = THREE.SRGBColorSpace;
texture.anisotropy = renderer.capabilities.getMaxAnisotropy();
return texture;
}
const ballotShadow = new THREE.Mesh(
new THREE.PlaneGeometry(3.25, 2.02),
new THREE.MeshBasicMaterial({
color: 0x6c5038,
transparent: true,
opacity: 0.13
})
);
ballotShadow.rotation.x = -Math.PI / 2;
ballotShadow.position.set(0.06, -0.01, 1.68);
scene.add(ballotShadow);
const ballot = new THREE.Mesh(
new THREE.PlaneGeometry(3.1, 1.9),
new THREE.MeshStandardMaterial({
map: createBallotTexture(),
roughness: 0.95,
flatShading: true
})
);
ballot.rotation.x = -Math.PI / 2;
ballot.rotation.z = -0.035;
ballot.position.set(0, 0.005, 1.62);
ballot.receiveShadow = true;
scene.add(ballot);
// ------------------------------------------------------------------
// Selection outline
// ------------------------------------------------------------------
let selectedProject = "A";
let selectionHelper = null;
function selectProject(id) {
selectedProject = id;
const project = projects.find(item => item.id === id);
if (!selectionHelper) {
selectionHelper = new THREE.BoxHelper(
boxGroups.get(id),
project.color
);
selectionHelper.visible = true;
selectionHelper.material.transparent = true;
selectionHelper.material.opacity = 0.75;
selectionHelper.material.depthTest = false;
selectionHelper.renderOrder = 10;
scene.add(selectionHelper);
} else {
selectionHelper.setFromObject(boxGroups.get(id));
selectionHelper.material.color.set(project.color);
}
document.querySelectorAll(".token-row").forEach(row => {
row.classList.toggle("active", row.dataset.project === id);
});
for (const item of projects) {
boxLabels
.get(item.id)
.classList.toggle("active", item.id === id);
}
status.textContent =
`${project.name} selected · ${project.count} tokens`;
}
document.querySelectorAll(".token-row").forEach(row => {
row.addEventListener("click", () => {
selectProject(row.dataset.project);
});
});
selectProject("A");
// ------------------------------------------------------------------
// Custom camera controls: orbit, zoom, keyboard, and picking
// ------------------------------------------------------------------
const target = new THREE.Vector3(0, 0.22, 0.25);
const defaultView = {
theta: 0.38,
phi: 0.79,
radius: 10.5
};
const spherical = {
theta: defaultView.theta,
phi: defaultView.phi,
radius: 12.2
};
const desired = {
theta: defaultView.theta,
phi: defaultView.phi,
radius: defaultView.radius
};
let dragging = false;
let pointerId = null;
let lastX = 0;
let lastY = 0;
let moved = 0;
const raycaster = new THREE.Raycaster();
const pointer = new THREE.Vector2();
function updatePointer(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 pickProject(event) {
updatePointer(event);
raycaster.setFromCamera(pointer, camera);
const intersections = raycaster.intersectObjects(
Array.from(boxGroups.values()),
true
);
if (!intersections.length) return;
let object = intersections[0].object;
while (object && !object.userData.project) {
object = object.parent;
}
if (object?.userData.project) {
selectProject(object.userData.project);
}
}
renderer.domElement.addEventListener("pointerdown", event => {
dragging = true;
pointerId = event.pointerId;
lastX = event.clientX;
lastY = event.clientY;
moved = 0;
renderer.domElement.setPointerCapture(pointerId);
renderer.domElement.classList.add("dragging");
renderer.domElement.focus({ preventScroll: true });
});
renderer.domElement.addEventListener("pointermove", event => {
if (!dragging || event.pointerId !== pointerId) return;
const deltaX = event.clientX - lastX;
const deltaY = event.clientY - lastY;
moved += Math.abs(deltaX) + Math.abs(deltaY);
lastX = event.clientX;
lastY = event.clientY;
desired.theta -= deltaX * 0.006;
desired.phi = THREE.MathUtils.clamp(
desired.phi + deltaY * 0.005,
0.34,
1.28
);
});
function endPointer(event) {
if (!dragging || event.pointerId !== pointerId) return;
dragging = false;
renderer.domElement.classList.remove("dragging");
if (renderer.domElement.hasPointerCapture(pointerId)) {
renderer.domElement.releasePointerCapture(pointerId);
}
if (moved < 7) {
pickProject(event);
}
}
renderer.domElement.addEventListener("pointerup", endPointer);
renderer.domElement.addEventListener("pointercancel", endPointer);
renderer.domElement.addEventListener(
"wheel",
event => {
event.preventDefault();
desired.radius = THREE.MathUtils.clamp(
desired.radius * Math.exp(event.deltaY * 0.001),
7.2,
14.5
);
},
{ passive: false }
);
renderer.domElement.addEventListener("keydown", event => {
const key = event.key;
if (key === "ArrowLeft") desired.theta += 0.12;
else if (key === "ArrowRight") desired.theta -= 0.12;
else if (key === "ArrowUp") desired.phi -= 0.08;
else if (key === "ArrowDown") desired.phi += 0.08;
else if (key === "+" || key === "=") desired.radius -= 0.5;
else if (key === "-" || key === "_") desired.radius += 0.5;
else return;
event.preventDefault();
});
resetButton.addEventListener("click", () => {
desired.theta = defaultView.theta;
desired.phi = defaultView.phi;
desired.radius = defaultView.radius;
});
// ------------------------------------------------------------------
// Resize and projected HTML labels
// ------------------------------------------------------------------
function resize() {
const width = window.innerWidth;
const height = window.innerHeight;
camera.aspect = width / height;
camera.updateProjectionMatrix();
renderer.setSize(width, height);
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
}
window.addEventListener("resize", resize);
const projected = new THREE.Vector3();
function updateLabels() {
for (const project of projects) {
const group = boxGroups.get(project.id);
const label = boxLabels.get(project.id);
projected.set(
group.position.x,
group.position.y + 1.02,
group.position.z + 0.02
);
projected.project(camera);
const visible = projected.z > -1 && projected.z < 1;
label.style.opacity = visible ? "1" : "0";
label.style.left =
`${(projected.x * 0.5 + 0.5) * window.innerWidth}px`;
label.style.top =
`${(-projected.y * 0.5 + 0.5) * window.innerHeight}px`;
}
}
// ------------------------------------------------------------------
// Animation
// ------------------------------------------------------------------
const clock = new THREE.Clock();
const reduceMotion = window.matchMedia(
"(prefers-reduced-motion: reduce)"
).matches;
if (reduceMotion) {
spherical.theta = desired.theta;
spherical.phi = desired.phi;
spherical.radius = desired.radius;
}
function animate() {
const delta = Math.min(clock.getDelta(), 0.05);
const smoothing = 1 - Math.pow(0.001, delta);
spherical.theta = THREE.MathUtils.lerp(
spherical.theta,
desired.theta,
smoothing
);
spherical.phi = THREE.MathUtils.lerp(
spherical.phi,
desired.phi,
smoothing
);
spherical.radius = THREE.MathUtils.lerp(
spherical.radius,
desired.radius,
smoothing
);
const sinPhi = Math.sin(spherical.phi);
camera.position.set(
target.x + spherical.radius * sinPhi * Math.sin(spherical.theta),
target.y + spherical.radius * Math.cos(spherical.phi),
target.z + spherical.radius * sinPhi * Math.cos(spherical.theta)
);
camera.lookAt(target);
if (selectionHelper) {
selectionHelper.setFromObject(boxGroups.get(selectedProject));
}
updateLabels();
renderer.render(scene, camera);
}
renderer.setAnimationLoop(animate);
</script>
</body>
</html>
```