Prompt specification
Build or customize the Three.js 3D scene "Market Comparison Attributes" 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: Market Comparison Attributes
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/market-comparison-attributes-x3367
Scene Description Prompt:
Build a self-contained HTML scene with Three.js from cdnjs featuring comparison and ordering by object attributes. Create a fictional market table comparing fruit by count, displayed mass, and price. Keep each measurement separate and show how rankings change with the selected attribute. Voxel style.
Working Three.js Starter (market-comparison-attributes-x3367):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Voxel Market — Attribute Ranking</title>
<style>
:root {
--ink: #173331;
--ink-soft: #526560;
--paper: #f3eee3;
--card: #fffdf7;
--line: #d9d4c7;
--accent: #f3b83f;
--accent-soft: #fff1c8;
--green: #377463;
--shadow: 6px 6px 0 var(--ink);
--radius: 16px;
}
* {
box-sizing: border-box;
}
html {
scroll-behavior: smooth;
}
body {
margin: 0;
min-width: 320px;
color: var(--ink);
background:
linear-gradient(rgba(23, 51, 49, 0.035) 1px, transparent 1px),
linear-gradient(90deg, rgba(23, 51, 49, 0.035) 1px, transparent 1px),
var(--paper);
background-size: 24px 24px;
font-family:
Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
button {
color: inherit;
font: inherit;
}
button:focus-visible,
[tabindex]:focus-visible {
outline: 3px solid #1b8f83;
outline-offset: 3px;
}
.page {
width: min(1540px, 100%);
margin: 0 auto;
padding: 20px;
}
.topbar {
display: flex;
align-items: center;
justify-content: space-between;
gap: 24px;
padding: 8px 4px 22px;
}
.brand {
display: flex;
align-items: center;
gap: 14px;
}
.voxel-logo {
position: relative;
width: 48px;
height: 48px;
flex: 0 0 auto;
}
.voxel-logo span {
position: absolute;
display: block;
width: 25px;
height: 25px;
border: 3px solid var(--ink);
box-shadow: 3px 3px 0 rgba(23, 51, 49, 0.18);
}
.voxel-logo span:nth-child(1) {
top: 0;
left: 0;
background: var(--accent);
}
.voxel-logo span:nth-child(2) {
top: 0;
right: 0;
background: #e66d4f;
}
.voxel-logo span:nth-child(3) {
bottom: 0;
left: 11px;
background: #68a777;
}
.brand-copy h1 {
margin: 0;
font-size: clamp(20px, 2.1vw, 30px);
line-height: 1;
letter-spacing: -0.04em;
}
.brand-copy p {
margin: 7px 0 0;
color: var(--ink-soft);
font-size: 13px;
font-weight: 650;
}
.market-status {
display: flex;
align-items: center;
gap: 14px;
}
.open-pill {
display: flex;
align-items: center;
gap: 8px;
padding: 10px 13px;
border: 2px solid var(--ink);
background: var(--card);
font-size: 12px;
font-weight: 850;
letter-spacing: 0.06em;
text-transform: uppercase;
}
.open-pill::before {
width: 8px;
height: 8px;
content: "";
background: #4a9b70;
box-shadow: 0 0 0 3px #dcecdf;
}
.market-time {
color: var(--ink-soft);
font-size: 12px;
font-weight: 700;
text-align: right;
}
.workspace {
display: grid;
grid-template-columns: minmax(0, 1.6fr) minmax(440px, 0.9fr);
gap: 22px;
align-items: start;
}
.scene-card,
.data-card {
border: 2px solid var(--ink);
border-radius: var(--radius);
background: var(--card);
box-shadow: var(--shadow);
}
.scene-card {
position: sticky;
top: 18px;
display: flex;
height: calc(100vh - 130px);
min-height: 620px;
overflow: hidden;
}
.scene-toolbar {
z-index: 2;
display: flex;
align-items: center;
justify-content: space-between;
gap: 16px;
padding: 17px 18px;
border-bottom: 2px solid var(--ink);
background: var(--card);
}
.section-kicker {
margin: 0 0 4px;
color: var(--green);
font-size: 10px;
font-weight: 900;
letter-spacing: 0.14em;
text-transform: uppercase;
}
.scene-toolbar h2,
.data-title h2 {
margin: 0;
font-size: 19px;
letter-spacing: -0.03em;
}
.scene-actions {
display: flex;
align-items: center;
gap: 10px;
}
.scene-order {
padding: 8px 10px;
border: 1px solid var(--line);
background: #f5f1e8;
font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
font-size: 10px;
font-weight: 800;
letter-spacing: 0.05em;
}
.square-button {
display: grid;
width: 38px;
height: 38px;
padding: 0;
cursor: pointer;
place-items: center;
border: 2px solid var(--ink);
border-radius: 8px;
background: var(--accent);
font-size: 20px;
box-shadow: 3px 3px 0 var(--ink);
}
.square-button:hover {
transform: translate(1px, 1px);
box-shadow: 2px 2px 0 var(--ink);
}
#viewport {
position: relative;
min-height: 0;
flex: 1;
overflow: hidden;
background: #e8e2d5;
}
#viewport canvas {
position: absolute;
inset: 0;
display: block;
width: 100%;
height: 100%;
touch-action: none;
}
.scene-hud {
position: absolute;
z-index: 3;
pointer-events: none;
}
.hud-left {
top: 16px;
left: 16px;
}
.hud-right {
top: 16px;
right: 16px;
max-width: 240px;
}
.hud-chip {
border: 2px solid var(--ink);
border-radius: 8px;
background: rgba(255, 253, 247, 0.94);
box-shadow: 3px 3px 0 rgba(23, 51, 49, 0.9);
}
.hud-chip .label {
display: block;
padding: 6px 10px 3px;
color: var(--ink-soft);
font-size: 9px;
font-weight: 900;
letter-spacing: 0.12em;
text-transform: uppercase;
}
.hud-chip .value {
display: block;
padding: 0 10px 8px;
font-size: 13px;
font-weight: 850;
}
.scene-help {
bottom: 15px;
left: 15px;
padding: 9px 11px;
border: 1px solid rgba(23, 51, 49, 0.3);
border-radius: 7px;
background: rgba(255, 253, 247, 0.88);
color: var(--ink-soft);
font-size: 10px;
font-weight: 750;
letter-spacing: 0.02em;
}
.data-card {
overflow: hidden;
}
.data-title {
padding: 20px 20px 16px;
border-bottom: 2px solid var(--ink);
}
.data-title-row {
display: flex;
align-items: flex-start;
justify-content: space-between;
gap: 12px;
}
.data-title p:last-child {
margin: 7px 0 0;
color: var(--ink-soft);
font-size: 12px;
line-height: 1.45;
}
.tiny-badge {
flex: 0 0 auto;
padding: 6px 8px;
border: 1px solid var(--line);
border-radius: 6px;
background: #f6f1e7;
color: var(--ink-soft);
font-size: 9px;
font-weight: 900;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.controls {
padding: 16px 20px;
border-bottom: 1px solid var(--line);
background: #faf6ed;
}
.control-label {
display: flex;
align-items: center;
justify-content: space-between;
gap: 12px;
margin-bottom: 9px;
color: var(--ink-soft);
font-size: 10px;
font-weight: 900;
letter-spacing: 0.1em;
text-transform: uppercase;
}
.direction-button {
padding: 6px 8px;
cursor: pointer;
border: 1px solid var(--ink);
border-radius: 6px;
background: var(--card);
font-size: 10px;
font-weight: 850;
letter-spacing: 0.03em;
}
.direction-button:hover {
background: var(--accent-soft);
}
.metric-tabs {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 7px;
}
.metric-tab {
min-height: 48px;
padding: 8px 7px;
cursor: pointer;
border: 2px solid var(--line);
border-radius: 8px;
background: var(--card);
font-size: 12px;
font-weight: 850;
transition:
border-color 150ms ease,
background 150ms ease,
transform 150ms ease;
}
.metric-tab small {
display: block;
margin-top: 2px;
color: var(--ink-soft);
font-size: 9px;
font-weight: 700;
}
.metric-tab:hover {
transform: translateY(-1px);
border-color: #a6aaa0;
}
.metric-tab.active {
border-color: var(--ink);
background: var(--accent);
box-shadow: 3px 3px 0 var(--ink);
}
.metric-tab.active small {
color: var(--ink);
}
.leader-card {
display: grid;
grid-template-columns: minmax(0, 1fr) auto;
gap: 18px;
align-items: center;
margin: 16px 20px;
padding: 15px 16px;
border: 2px solid var(--ink);
border-radius: 10px;
background: var(--ink);
color: var(--card);
box-shadow: 4px 4px 0 var(--accent);
}
.leader-kicker {
margin-bottom: 4px;
color: var(--accent);
font-size: 9px;
font-weight: 900;
letter-spacing: 0.13em;
text-transform: uppercase;
}
.leader-name {
margin: 0;
font-size: 18px;
letter-spacing: -0.02em;
}
.leader-detail {
margin: 5px 0 0;
color: #bfcac5;
font-size: 10px;
line-height: 1.4;
}
.leader-value {
padding-left: 15px;
border-left: 1px solid rgba(255, 255, 255, 0.18);
text-align: right;
white-space: nowrap;
}
.leader-value strong {
display: block;
font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
font-size: 19px;
}
.leader-value span {
color: #bfcac5;
font-size: 9px;
font-weight: 700;
}
.table-wrap {
overflow-x: auto;
padding: 0 12px 10px;
}
table {
width: 100%;
min-width: 560px;
border-collapse: separate;
border-spacing: 0 5px;
font-size: 11px;
}
caption {
position: absolute;
width: 1px;
height: 1px;
overflow: hidden;
clip: rect(0 0 0 0);
}
thead th {
padding: 5px 6px 7px;
color: var(--ink-soft);
font-size: 9px;
font-weight: 900;
letter-spacing: 0.06em;
text-align: right;
text-transform: uppercase;
white-space: nowrap;
}
thead th:nth-child(1),
thead th:nth-child(2) {
text-align: left;
}
.column-button {
display: inline-flex;
align-items: center;
gap: 4px;
padding: 4px;
cursor: pointer;
border: 0;
border-radius: 4px;
background: transparent;
color: inherit;
font-size: inherit;
font-weight: inherit;
letter-spacing: inherit;
text-transform: inherit;
}
.column-button:hover,
.column-button.active {
color: var(--ink);
background: var(--accent-soft);
}
.sort-arrow {
color: #b56f22;
font-size: 13px;
line-height: 1;
}
tbody tr {
position: relative;
cursor: pointer;
outline: none;
transition: background 140ms ease;
}
tbody tr td {
padding: 10px 7px;
border-top: 1px solid var(--line);
border-bottom: 1px solid var(--line);
background: #faf8f1;
text-align: right;
white-space: nowrap;
}
tbody tr td:first-child {
border-left: 1px solid var(--line);
border-radius: 8px 0 0 8px;
}
tbody tr td:last-child {
border-right: 1px solid var(--line);
border-radius: 0 8px 8px 0;
}
tbody tr:hover td {
background: #f3efe5;
}
tbody tr.focused td {
background: var(--accent-soft);
border-color: #d9b75e;
}
tbody tr.focused td:first-child {
border-left: 2px solid var(--ink);
}
tbody tr.pinned td:last-child {
border-right: 2px solid var(--ink);
}
.rank-number {
display: inline-grid;
width: 25px;
height: 25px;
place-items: center;
border: 1px solid var(--ink);
border-radius: 5px;
background: var(--card);
font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
font-size: 10px;
font-weight: 900;
}
.lot-cell {
min-width: 132px;
text-align: left !important;
}
.lot-label {
display: flex;
align-items: center;
gap: 8px;
}
.lot-swatch {
width: 12px;
height: 12px;
flex: 0 0 auto;
border: 2px solid var(--ink);
background: var(--lot-color);
box-shadow: 2px 2px 0 rgba(23, 51, 49, 0.2);
}
.lot-name {
display: block;
font-size: 11px;
font-weight: 850;
}
.lot-kind {
display: block;
margin-top: 2px;
color: var(--ink-soft);
font-size: 8px;
font-weight: 750;
letter-spacing: 0.06em;
text-transform: uppercase;
}
.metric-cell {
font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
font-weight: 750;
}
.metric-cell.selected {
background: #fff0bc !important;
color: #68460a;
font-weight: 900;
}
.compare-toggle {
display: inline-grid;
width: 24px;
height: 24px;
margin-left: 5px;
padding: 0;
cursor: pointer;
place-items: center;
border: 1px solid #b8b6ad;
border-radius: 5px;
background: var(--card);
font-size: 14px;
font-weight: 900;
line-height: 1;
}
.compare-toggle:hover {
border-color: var(--ink);
}
.compare-toggle.active {
border-color: var(--ink);
background: var(--ink);
color: var(--accent);
}
.focus-strip {
display: grid;
grid-template-columns: minmax(125px, 1fr) repeat(3, minmax(72px, 0.7fr));
gap: 7px;
margin: 0 20px 18px;
}
.focus-name,
.focus-stat {
padding: 10px;
border: 1px solid var(--line);
border-radius: 7px;
background: #faf7ef;
}
.focus-name span,
.focus-stat span {
display: block;
color: var(--ink-soft);
font-size: 8px;
font-weight: 900;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.focus-name strong {
display: block;
margin-top: 4px;
font-size: 11px;
}
.focus-stat strong {
display: block;
margin-top: 4px;
font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
font-size: 11px;
}
.compare-section {
padding: 16px 20px 20px;
border-top: 2px solid var(--ink);
background: #f6f1e7;
}
.compare-heading {
display: flex;
align-items: center;
justify-content: space-between;
gap: 12px;
margin-bottom: 11px;
}
.compare-heading h3 {
margin: 0;
font-size: 13px;
}
.clear-button {
cursor: pointer;
border: 0;
background: transparent;
color: var(--ink-soft);
font-size: 9px;
font-weight: 850;
text-decoration: underline;
text-underline-offset: 3px;
}
.compare-grid {
display: grid;
grid-template-columns: repeat(2, minmax(0, 1fr));
gap: 9px;
}
.compare-card {
min-height: 142px;
padding: 11px;
border: 1px solid #c9c5ba;
border-radius: 8px;
background: var(--card);
}
.compare-card.empty {
display: grid;
place-items: center;
border-style: dashed;
color: #8c958f;
font-size: 10px;
font-weight: 750;
text-align: center;
}
.compare-card-title {
display: flex;
align-items: center;
gap: 7px;
padding-bottom: 8px;
border-bottom: 1px solid var(--line);
font-size: 11px;
font-weight: 850;
}
.compare-card-title i {
width: 9px;
height: 9px;
border: 1px solid var(--ink);
background: var(--lot-color);
}
.compare-metric {
display: flex;
align-items: baseline;
justify-content: space-between;
gap: 8px;
padding-top: 7px;
font-size: 9px;
}
.compare-metric span {
color: var(--ink-soft);
font-weight: 700;
}
.compare-metric strong {
font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
font-size: 10px;
}
.difference-box {
margin-top: 10px;
padding: 9px 10px;
border: 1px dashed #aaa89e;
border-radius: 7px;
background: rgba(255, 255, 255, 0.55);
}
.difference-title {
margin-bottom: 6px;
color: var(--ink-soft);
font-size: 8px;
font-weight: 900;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.difference-grid {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 7px;
}
.difference-grid div {
font-size: 8px;
line-height: 1.35;
}
.difference-grid strong {
display: block;
font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
font-size: 9px;
}
.page-footer {
display: flex;
justify-content: space-between;
gap: 15px;
padding: 18px 4px 4px;
color: var(--ink-soft);
font-size: 9px;
font-weight: 700;
letter-spacing: 0.04em;
}
.toast {
position: fixed;
z-index: 20;
bottom: 24px;
left: 50%;
padding: 11px 15px;
border: 2px solid var(--ink);
border-radius: 7px;
background: var(--accent);
box-shadow: 4px 4px 0 var(--ink);
font-size: 11px;
font-weight: 850;
opacity: 0;
pointer-events: none;
transform: translate(-50%, 15px);
transition:
opacity 180ms ease,
transform 180ms ease;
}
.toast.show {
opacity: 1;
transform: translate(-50%, 0);
}
@media (max-width: 1050px) {
.workspace {
grid-template-columns: 1fr;
}
.scene-card {
position: relative;
top: 0;
height: 650px;
}
}
@media (max-width: 620px) {
.page {
padding: 12px;
}
.topbar {
align-items: flex-start;
padding-bottom: 17px;
}
.market-time {
display: none;
}
.open-pill {
padding: 8px;
font-size: 9px;
}
.brand-copy p {
max-width: 170px;
font-size: 10px;
}
.scene-card {
min-height: 510px;
height: 58vh;
}
.scene-toolbar {
padding: 13px;
}
.scene-order {
display: none;
}
.hud-right {
display: none;
}
.data-title,
.controls,
.compare-section {
padding-left: 14px;
padding-right: 14px;
}
.leader-card {
margin-left: 14px;
margin-right: 14px;
}
.focus-strip {
grid-template-columns: repeat(3, 1fr);
margin-left: 14px;
margin-right: 14px;
}
.focus-name {
grid-column: 1 / -1;
}
.compare-grid {
grid-template-columns: 1fr;
}
.compare-card.empty {
min-height: 60px;
}
.page-footer {
flex-direction: column;
}
}
@media (prefers-reduced-motion: reduce) {
*,
*::before,
*::after {
scroll-behavior: auto !important;
transition-duration: 0.01ms !important;
animation-duration: 0.01ms !important;
animation-iteration-count: 1 !important;
}
}
</style>
</head>
<body>
<div class="page">
<header class="topbar">
<div class="brand">
<div class="voxel-logo" aria-hidden="true">
<span></span><span></span><span></span>
</div>
<div class="brand-copy">
<h1>Voxel Exchange</h1>
<p>Six lots. Three independent attributes.</p>
</div>
</div>
<div class="market-status">
<div class="market-time">
SATURDAY MARKET<br />
08:00—14:00 / PIER 03
</div>
<div class="open-pill">Market open</div>
</div>
</header>
<main class="workspace">
<section class="scene-card" aria-labelledby="scene-title">
<div class="scene-toolbar">
<div>
<p class="section-kicker">Live ranked stand</p>
<h2 id="scene-title">The attribute market</h2>
</div>
<div class="scene-actions">
<div class="scene-order" id="scene-order">COUNT · HIGH → LOW</div>
<button
class="square-button"
id="reset-camera"
type="button"
title="Reset camera"
aria-label="Reset camera"
>
↺
</button>
</div>
</div>
<div id="viewport">
<div class="scene-hud hud-left">
<div class="hud-chip">
<span class="label">Current order</span>
<span class="value" id="hud-order">By count</span>
</div>
</div>
<div class="scene-hud hud-right">
<div class="hud-chip">
<span class="label">Selected metric</span>
<span class="value" id="hud-metric">12 pieces maximum</span>
</div>
</div>
<div class="scene-hud scene-help">
DRAG TO ORBIT · SCROLL TO ZOOM · CLICK A STALL
</div>
</div>
</section>
<aside class="data-card" aria-labelledby="data-title">
<div class="data-title">
<div class="data-title-row">
<div>
<p class="section-kicker">Comparison board</p>
<h2 id="data-title">Rank the harvest</h2>
</div>
<div class="tiny-badge">6 lots</div>
</div>
<p>
Choose one attribute to reorder the stalls. Count, displayed mass,
and price are always measured separately.
</p>
</div>
<div class="controls">
<div class="control-label">
<span>Order by attribute</span>
<button class="direction-button" id="direction-button" type="button">
High → low
</button>
</div>
<div class="metric-tabs" role="group" aria-label="Ranking attribute">
<button
class="metric-tab active"
type="button"
data-attribute="count"
aria-pressed="true"
>
Count
<small>individual pieces</small>
</button>
<button
class="metric-tab"
type="button"
data-attribute="mass"
aria-pressed="false"
>
Displayed mass
<small>kilograms</small>
</button>
<button
class="metric-tab"
type="button"
data-attribute="price"
aria-pressed="false"
>
Price
<small>credits per kg</small>
</button>
</div>
</div>
<div class="leader-card" aria-live="polite">
<div>
<div class="leader-kicker" id="leader-kicker">Most pieces</div>
<h3 class="leader-name" id="leader-name">Pixel Plums</h3>
<p class="leader-detail" id="leader-detail"></p>
</div>
<div class="leader-value">
<strong id="leader-value">24 pieces</strong>
<span id="leader-unit">COUNT</span>
</div>
</div>
<div class="table-wrap">
<table>
<caption>Market lots ranked by the selected attribute</caption>
<thead>
<tr>
<th scope="col">Rank</th>
<th scope="col">Market lot</th>
<th scope="col">
<button class="column-button active" data-metric="count" type="button">
Count <span class="sort-arrow">↓</span>
</button>
</th>
<th scope="col">
<button class="column-button" data-metric="mass" type="button">
Displayed mass <span class="sort-arrow">↕</span>
</button>
</th>
<th scope="col">
<button class="column-button" data-metric="price" type="button">
Price <span class="sort-arrow">↕</span>
</button>
</th>
<th scope="col" title="Pin for comparison">Pin</th>
</tr>
</thead>
<tbody id="market-table-body"></tbody>
</table>
</div>
<div class="focus-strip" id="focus-strip" aria-label="Focused lot metrics"></div>
<section class="compare-section" aria-labelledby="compare-title">
<div class="compare-heading">
<h3 id="compare-title">Compare two lots</h3>
<button class="clear-button" id="clear-comparison" type="button">
Clear pins
</button>
</div>
<div class="compare-grid" id="compare-grid"></div>
<div class="difference-box" id="difference-box"></div>
</section>
</aside>
</main>
<footer class="page-footer">
<span>ALL PRICES ARE FICTIONAL MARKET CREDITS.</span>
<span>NO COMPOSITE SCORE — EACH ATTRIBUTE STANDS ALONE.</span>
</footer>
</div>
<div class="toast" id="toast" role="status" aria-live="polite"></div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
const lots = [
{
id: "apples",
name: "Ember Apples",
kind: "orchard lot",
count: 12,
mass: 8.4,
price: 3.2,
color: "#e35d4f",
shape: "apple"
},
{
id: "oranges",
name: "Sunset Oranges",
kind: "citrus lot",
count: 18,
mass: 10.8,
price: 2.8,
color: "#f39a35",
shape: "orange"
},
{
id: "pears",
name: "Moon Pears",
kind: "orchard lot",
count: 9,
mass: 7.2,
price: 4.5,
color: "#c8cf63",
shape: "pear"
},
{
id: "plums",
name: "Pixel Plums",
kind: "berry lot",
count: 24,
mass: 5.4,
price: 6.8,
color: "#8d527e",
shape: "plum"
},
{
id: "melons",
name: "Harbor Melons",
kind: "field lot",
count: 6,
mass: 12.0,
price: 2.1,
color: "#65a66d",
shape: "melon"
},
{
id: "figs",
name: "Honey Figs",
kind: "orchard lot",
count: 15,
mass: 6.0,
price: 5.4,
color: "#a8794e",
shape: "fig"
}
];
const metrics = {
count: {
label: "Count",
shortLabel: "Count",
unit: "pieces",
defaultDirection: "desc",
leaderLabel: "Most pieces",
sceneLabel: "piece count"
},
mass: {
label: "Displayed mass",
shortLabel: "Mass",
unit: "kg",
defaultDirection: "desc",
leaderLabel: "Heaviest lot",
sceneLabel: "displayed mass"
},
price: {
label: "Price",
shortLabel: "Price",
unit: "¢/kg",
defaultDirection: "asc",
leaderLabel: "Lowest price",
sceneLabel: "price"
}
};
const state = {
attribute: "count",
direction: "desc",
focus: "plums",
compared: ["plums", "melons"]
};
const viewport = document.querySelector("#viewport");
const tableBody = document.querySelector("#market-table-body");
const reducedMotion = window.matchMedia(
"(prefers-reduced-motion: reduce)"
).matches;
let toastTimer;
function showToast(message) {
const toast = document.querySelector("#toast");
toast.textContent = message;
toast.classList.add("show");
clearTimeout(toastTimer);
toastTimer = setTimeout(() => toast.classList.remove("show"), 2200);
}
function signed(value, digits = 0) {
const fixed = Number(value).toFixed(digits);
return value > 0 ? `+${fixed}` : fixed;
}
function formatMetric(lot, attribute) {
if (attribute === "count") {
return `${lot.count} pieces`;
}
if (attribute === "mass") {
return `${lot.mass.toFixed(1)} kg`;
}
return `${lot.price.toFixed(1)}¢/kg`;
}
function sortedLots() {
const direction = state.direction === "desc" ? -1 : 1;
return [...lots].sort((a, b) => {
const difference = (a[state.attribute] - b[state.attribute]) * direction;
return difference || a.name.localeCompare(b.name);
});
}
function directionLabel() {
return state.direction === "desc" ? "High → low" : "Low → high";
}
function setAttribute(attribute, toggle = false) {
if (toggle && state.attribute === attribute) {
state.direction = state.direction === "desc" ? "asc" : "desc";
} else {
state.attribute = attribute;
state.direction = metrics[attribute].defaultDirection;
}
updateAll();
}
function toggleComparison(id) {
const index = state.compared.indexOf(id);
if (index >= 0) {
state.compared.splice(index, 1);
} else {
if (state.compared.length >= 2) {
showToast("Pin two lots at a time. Unpin one to add another.");
return;
}
state.compared.push(id);
}
renderTable();
renderComparison();
}
function focusLot(id) {
state.focus = id;
renderTable();
renderFocusStrip();
updateSceneSelection();
}
function updateAll() {
const sorted = sortedLots();
document.querySelectorAll(".metric-tab").forEach((button) => {
const active = button.dataset.attribute === state.attribute;
button.classList.toggle("active", active);
button.setAttribute("aria-pressed", String(active));
});
document.querySelectorAll(".column-button").forEach((button) => {
const active = button.dataset.metric === state.attribute;
button.classList.toggle("active", active);
const arrow = button.querySelector(".sort-arrow");
arrow.textContent = active
? state.direction === "desc"
? "↓"
: "↑"
: "↕";
});
document.querySelector("#direction-button").textContent = directionLabel();
const metric = metrics[state.attribute];
document.querySelector("#scene-order").textContent =
`${metric.label.toUpperCase()} · ${directionLabel().toUpperCase()}`;
document.querySelector("#hud-order").textContent =
`By ${metric.label.toLowerCase()}`;
document.querySelector("#hud-metric").textContent =
`${metric.label}: ${metric.sceneLabel}`;
renderLeader(sorted);
renderTable();
renderFocusStrip();
renderComparison();
updateSceneOrder(sorted);
}
function renderLeader(sorted) {
const leader = sorted[0];
const runner = sorted[1];
const metric = metrics[state.attribute];
const difference = Math.abs(leader[state.attribute] - runner[state.attribute]);
document.querySelector("#leader-kicker").textContent = metric.leaderLabel;
document.querySelector("#leader-name").textContent = leader.name;
document.querySelector("#leader-value").textContent = formatMetric(
leader,
state.attribute
);
document.querySelector("#leader-unit").textContent =
state.attribute.toUpperCase();
let detail;
if (state.attribute === "count") {
detail = `${difference} more pieces than ${runner.name}`;
} else if (state.attribute === "mass") {
detail = `${difference.toFixed(1)} kg above ${runner.name}`;
} else {
detail = `${difference.toFixed(1)}¢/kg below ${runner.name}`;
}
document.querySelector("#leader-detail").textContent = detail;
}
function renderTable() {
const oldPositions = new Map();
tableBody.querySelectorAll("tr").forEach((row) => {
oldPositions.set(row.dataset.id, row.getBoundingClientRect().top);
});
const sorted = sortedLots();
tableBody.innerHTML = sorted
.map((lot, index) => {
const selected = lot[state.attribute] === state.attribute;
const pinned = state.compared.includes(lot.id);
const focused = state.focus === lot.id;
return `
<tr
data-id="${lot.id}"
class="${focused ? "focused" : ""} ${pinned ? "pinned" : ""}"
tabindex="0"
aria-label="${lot.name}, rank ${index + 1}"
aria-selected="${focused}"
>
<td>
<span class="rank-number">${String(index + 1).padStart(2, "0")}</span>
</td>
<td class="lot-cell">
<div class="lot-label">
<span
class="lot-swatch"
style="--lot-color: ${lot.color}"
aria-hidden="true"
></span>
<span>
<span class="lot-name">${lot.name}</span>
<span class="lot-kind">${lot.kind}</span>
</span>
</div>
</td>
<td class="metric-cell ${state.attribute === "count" ? "selected" : ""}">
${lot.count}
</td>
<td class="metric-cell ${state.attribute === "mass" ? "selected" : ""}">
${lot.mass.toFixed(1)}
</td>
<td class="metric-cell ${state.attribute === "price" ? "selected" : ""}">
${lot.price.toFixed(1)}¢
</td>
<td>
<button
class="compare-toggle ${pinned ? "active" : ""}"
type="button"
data-compare="${lot.id}"
aria-label="${pinned ? "Unpin" : "Pin"} ${lot.name} for comparison"
title="${pinned ? "Unpin" : "Pin"} for comparison"
>
${pinned ? "✓" : "+"}
</button>
</td>
</tr>
`;
})
.join("");
if (!reducedMotion && oldPositions.size) {
tableBody.querySelectorAll("tr").forEach((row) => {
const oldTop = oldPositions.get(row.dataset.id);
if (oldTop === undefined) return;
const newTop = row.getBoundingClientRect().top;
const shift = oldTop - newTop;
if (Math.abs(shift) > 1) {
row.animate(
[
{ transform: `translateY(${shift}px)` },
{ transform: "translateY(0)" }
],
{
duration: 360,
easing: "cubic-bezier(.2,.8,.2,1)"
}
);
}
});
}
}
function renderFocusStrip() {
const lot = lots.find((item) => item.id === state.focus);
const strip = document.querySelector("#focus-strip");
strip.innerHTML = `
<div class="focus-name">
<span>Focused lot</span>
<strong>${lot.name}</strong>
</div>
<div class="focus-stat">
<span>Count</span>
<strong>${lot.count} pcs</strong>
</div>
<div class="focus-stat">
<span>Mass</span>
<strong>${lot.mass.toFixed(1)} kg</strong>
</div>
<div class="focus-stat">
<span>Price</span>
<strong>${lot.price.toFixed(1)}¢/kg</strong>
</div>
`;
}
function renderComparison() {
const grid = document.querySelector("#compare-grid");
const differenceBox = document.querySelector("#difference-box");
const selected = state.compared
.map((id) => lots.find((lot) => lot.id === id))
.filter(Boolean);
const cards = [];
for (let index = 0; index < 2; index += 1) {
const lot = selected[index];
if (!lot) {
cards.push(`
<article class="compare-card empty">
Pin ${index === 0 ? "a first" : "a second"} lot<br />
from the table
</article>
`);
continue;
}
cards.push(`
<article class="compare-card">
<div
class="compare-card-title"
style="--lot-color: ${lot.color}"
>
<i aria-hidden="true"></i>
${index === 0 ? "A" : "B"} · ${lot.name}
</div>
<div class="compare-metric">
<span>Count</span>
<strong>${lot.count} pieces</strong>
</div>
<div class="compare-metric">
<span>Displayed mass</span>
<strong>${lot.mass.toFixed(1)} kg</strong>
</div>
<div class="compare-metric">
<span>Price</span>
<strong>${lot.price.toFixed(1)}¢/kg</strong>
</div>
</article>
`);
}
grid.innerHTML = cards.join("");
if (selected.length === 2) {
const [a, b] = selected;
differenceBox.innerHTML = `
<div class="difference-title">A − B, measured separately</div>
<div class="difference-grid">
<div>
Count
<strong>${signed(a.count - b.count)} pieces</strong>
</div>
<div>
Mass
<strong>${signed(a.mass - b.mass, 1)} kg</strong>
</div>
<div>
Price
<strong>${signed(a.price - b.price, 1)}¢/kg</strong>
</div>
</div>
`;
} else {
differenceBox.innerHTML = `
<div class="difference-title">Independent comparison</div>
<div class="difference-grid">
<div>Pin two lots<br /><strong>No combined score</strong></div>
<div>Each value stays<br /><strong>Separate</strong></div>
<div>Different attributes<br /><strong>Change order</strong></div>
</div>
`;
}
}
document.querySelectorAll(".metric-tab").forEach((button) => {
button.addEventListener("click", () => {
setAttribute(button.dataset.attribute);
});
});
document.querySelectorAll(".column-button").forEach((button) => {
button.addEventListener("click", () => {
setAttribute(button.dataset.metric, true);
});
});
document.querySelector("#direction-button").addEventListener("click", () => {
state.direction = state.direction === "desc" ? "asc" : "desc";
updateAll();
});
tableBody.addEventListener("click", (event) => {
const compareButton = event.target.closest("[data-compare]");
if (compareButton) {
event.stopPropagation();
toggleComparison(compareButton.dataset.compare);
return;
}
const row = event.target.closest("tr");
if (row) focusLot(row.dataset.id);
});
tableBody.addEventListener("keydown", (event) => {
if (event.key !== "Enter" && event.key !== " ") return;
const row = event.target.closest("tr");
if (!row) return;
event.preventDefault();
focusLot(row.dataset.id);
});
document.querySelector("#clear-comparison").addEventListener("click", () => {
state.compared = [];
renderTable();
renderComparison();
});
// ------------------------------------------------------------
// Three.js scene
// ------------------------------------------------------------
const scene = new THREE.Scene();
scene.background = new THREE.Color("#e8e2d5");
const camera = new THREE.OrthographicCamera(-8, 8, 7, -7, 0.1, 100);
const renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.12;
renderer.domElement.setAttribute(
"aria-label",
"Interactive voxel market showing fruit lots ranked by the selected attribute"
);
viewport.prepend(renderer.domElement);
const hemisphere = new THREE.HemisphereLight("#fff9df", "#7b8b78", 2.3);
scene.add(hemisphere);
const sun = new THREE.DirectionalLight("#fff0ca", 3.2);
sun.position.set(8, 15, 9);
sun.castShadow = true;
sun.shadow.mapSize.set(2048, 2048);
sun.shadow.camera.left = -14;
sun.shadow.camera.right = 14;
sun.shadow.camera.top = 12;
sun.shadow.camera.bottom = -12;
sun.shadow.camera.near = 1;
sun.shadow.camera.far = 40;
sun.shadow.normalBias = 0.035;
scene.add(sun);
const fill = new THREE.DirectionalLight("#b9d6d5", 0.75);
fill.position.set(-9, 7, -6);
scene.add(fill);
const materials = {
ground: new THREE.MeshStandardMaterial({
color: "#d9d4c7",
roughness: 1
}),
platform: new THREE.MeshStandardMaterial({
color: "#aa7448",
roughness: 0.92
}),
platformTop: new THREE.MeshStandardMaterial({
color: "#c99461",
roughness: 0.92
}),
plankSeam: new THREE.MeshStandardMaterial({
color: "#7f5537",
roughness: 1
}),
aisle: new THREE.MeshStandardMaterial({
color: "#e5cfa5",
roughness: 1
}),
crate: new THREE.MeshStandardMaterial({
color: "#a96f42",
roughness: 0.9
}),
crateLight: new THREE.MeshStandardMaterial({
color: "#c28a58",
roughness: 0.9
}),
crateDark: new THREE.MeshStandardMaterial({
color: "#69452f",
roughness: 0.95
}),
leaf: new THREE.MeshStandardMaterial({
color: "#497b4d",
roughness: 0.9
}),
stem: new THREE.MeshStandardMaterial({
color: "#6b4b32",
roughness: 0.95
}),
selection: new THREE.MeshBasicMaterial({
color: "#ffd166",
transparent: true,
opacity: 0.95
})
};
function addBox(parent, size, position, material, castShadow = true) {
const mesh = new THREE.Mesh(
new THREE.BoxGeometry(size[0], size[1], size[2]),
material
);
mesh.position.set(position[0], position[1], position[2]);
mesh.castShadow = castShadow;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
const ground = addBox(
scene,
[70, 0.3, 70],
[0, -0.55, 0],
materials.ground,
false
);
ground.receiveShadow = true;
const platform = addBox(
scene,
[13.4, 0.38, 7.4],
[0, -0.25, 0],
materials.platform,
false
);
platform.receiveShadow = true;
addBox(
scene,
[13.15, 0.12, 7.15],
[0, -0.02, 0],
materials.platformTop,
false
);
for (let index = -5; index <= 5; index += 1) {
addBox(
scene,
[0.025, 0.012, 7.02],
[index * 1.17, 0.048, 0],
materials.plankSeam,
false
);
}
addBox(
scene,
[12.1, 0.035, 0.72],
[0, 0.064, 0],
materials.aisle,
false
);
const crateEntries = new Map();
const crateRoots = [];
const rankPositions = [
[-3.8, 2.55],
[0, 2.55],
[3.8, 2.55],
[3.8, -2.55],
[0, -2.55],
[-3.8, -2.55]
];
function fruitMaterial(color) {
return new THREE.MeshStandardMaterial({
color,
roughness: 0.82,
metalness: 0
});
}
function createFruit(lot, size) {
const group = new THREE.Group();
const bodyMaterial = fruitMaterial(lot.color);
switch (lot.shape) {
case "apple": {
addBox(
group,
[size, size * 0.92, size],
[0, 0, 0],
bodyMaterial
);
addBox(
group,
[size * 0.23, size * 0.18, size * 0.23],
[0, size * 0.55, 0],
materials.stem
);
addBox(
group,
[size * 0.38, size * 0.12, size * 0.28],
[size * 0.2, size * 0.48, 0],
materials.leaf
);
break;
}
case "orange": {
addBox(
group,
[size * 0.96, size * 0.96, size * 0.96],
[0, 0, 0],
bodyMaterial
);
addBox(
group,
[size * 0.38, size * 0.12, size * 0.22],
[size * 0.14, size * 0.55, 0],
materials.leaf
);
addBox(
group,
[size * 0.14, size * 0.18, size * 0.14],
[0, size * 0.56, 0],
materials.stem
);
break;
}
case "pear": {
addBox(
group,
[size * 0.88, size * 0.68, size * 0.88],
[0, -size * 0.16, 0],
bodyMaterial
);
addBox(
group,
[size * 0.62, size * 0.62, size * 0.62],
[0, size * 0.42, 0],
bodyMaterial
);
addBox(
group,
[size * 0.14, size * 0.25, size * 0.14],
[0, size * 0.83, 0],
materials.stem
);
break;
}
case "plum": {
addBox(
group,
[size * 0.9, size * 0.9, size * 0.9],
[0, 0, 0],
bodyMaterial
);
addBox(
group,
[size * 0.16, size * 0.18, size * 0.16],
[0, size * 0.55, 0],
materials.stem
);
break;
}
case "melon": {
addBox(
group,
[size, size * 0.9, size],
[0, 0, 0],
bodyMaterial
);
const stripeMaterial = fruitMaterial("#3e7b50");
addBox(
group,
[size * 0.13, size * 0.76, size * 0.025],
[-size * 0.25, 0, size * 0.505],
stripeMaterial
);
addBox(
group,
[size * 0.13, size * 0.76, size * 0.025],
[size * 0.25, 0, size * 0.505],
stripeMaterial
);
break;
}
case "fig": {
addBox(
group,
[size * 0.86, size * 0.82, size * 0.86],
[0, 0, 0],
bodyMaterial
);
addBox(
group,
[size * 0.58, size * 0.36, size * 0.58],
[0, size * 0.47, 0],
bodyMaterial
);
addBox(
group,
[size * 0.12, size * 0.16, size * 0.12],
[0, size * 0.73, 0],
materials.stem
);
break;
}
}
return group;
}
function createCrate(lot) {
const group = new THREE.Group();
group.userData.lotId = lot.id;
// Pallet.
for (const z of [-0.66, 0, 0.66]) {
addBox(
group,
[2.88, 0.13, 0.34],
[0, 0.16, z],
materials.crateDark
);
}
// Corner posts.
for (const x of [-1.33, 1.33]) {
for (const z of [-0.8, 0.8]) {
addBox(
group,
[0.15, 1.03, 0.15],
[x, 0.64, z],
materials.crate
);
}
}
// Back and side slats.
for (const y of [0.43, 0.71, 0.99]) {
addBox(
group,
[2.7, 0.18, 0.12],
[0, y, -0.82],
materials.crateLight
);
addBox(
group,
[0.12, 0.18, 1.62],
[-1.36, y, 0],
materials.crate
);
addBox(
group,
[0.12, 0.18, 1.62],
[1.36, y, 0],
materials.crate
);
}
// Low front slats keep the fruit visible.
for (const y of [0.43, 0.7]) {
addBox(
group,
[2.7, 0.17, 0.12],
[0, y, 0.82],
materials.crateLight
);
}
const sizeByShape = {
apple: 0.34,
orange: 0.34,
pear: 0.34,
plum: 0.27,
melon: 0.47,
fig: 0.27
};
const size = sizeByShape[lot.shape];
const columns = lot.count <= 9 ? 3 : lot.count <= 12 ? 4 : lot.count <= 15 ? 5 : 6;
const rows = Math.ceil(lot.count / columns);
const spacing = size * 1.23;
for (let index = 0; index < lot.count; index += 1) {
const row = Math.floor(index / columns);
const column = index % columns;
const x = (column - (columns - 1) / 2) * spacing;
const z = (row - (rows - 1) / 2) * spacing;
const fruit = createFruit(lot, size);
fruit.position.set(x, 0.43 + size * 0.5, z);
group.add(fruit);
}
// Small price-token detail on the front of the crate.
addBox(
group,
[0.28, 0.2, 0.08],
[1.04, 0.59, 0.91],
fruitMaterial(lot.color)
);
const selection = new THREE.Group();
addBox(
selection,
[3.04, 0.055, 0.07],
[0, 0.1, 1.04],
materials.selection,
false
);
addBox(
selection,
[3.04, 0.055, 0.07],
[0, 0.1, -1.04],
materials.selection,
false
);
addBox(
selection,
[0.07, 0.055, 2.08],
[-1.49, 0.1, 0],
materials.selection,
false
);
addBox(
selection,
[0.07, 0.055, 2.08],
[1.49, 0.1, 0],
materials.selection,
false
);
selection.visible = false;
group.add(selection);
const sign = new THREE.Sprite(
new THREE.SpriteMaterial({
transparent: true,
depthTest: false,
depthWrite: false
})
);
sign.position.set(0, 2.05, 0);
sign.scale.set(3.12, 0.94, 1);
sign.renderOrder = 20;
group.add(sign);
group.traverse((object) => {
object.userData.lotId = lot.id;
});
scene.add(group);
crateRoots.push(group);
return {
lot,
group,
sign,
selection,
target: new THREE.Vector3()
};
}
lots.forEach((lot) => {
crateEntries.set(lot.id, createCrate(lot));
});
function signTexture(lot, rank, focused) {
const canvas = document.createElement("canvas");
canvas.width = 600;
canvas.height = 180;
const context = canvas.getContext("2d");
context.fillStyle = "#173331";
context.fillRect(0, 0, canvas.width, canvas.height);
context.fillStyle = focused ? "#f3b83f" : lot.color;
context.fillRect(0, 0, 13, canvas.height);
context.strokeStyle = focused ? "#f3b83f" : "rgba(255,255,255,.22)";
context.lineWidth = focused ? 8 : 3;
context.strokeRect(2, 2, canvas.width - 4, canvas.height - 4);
context.fillStyle = "#f3b83f";
context.font = "800 24px ui-monospace, SFMono-Regular, Menlo, monospace";
context.fillText(`RANK ${String(rank).padStart(2, "0")}`, 34, 38);
context.fillStyle = "#fffdf7";
context.font = "900 38px ui-sans-serif, system-ui, sans-serif";
context.fillText(lot.name.toUpperCase(), 34, 83);
const metricsData = [
{
label: "COUNT",
value: String(lot.count),
x: 30
},
{
label: "MASS",
value: `${lot.mass.toFixed(1)} kg`,
x: 210
},
{
label: "PRICE",
value: `${lot.price.toFixed(1)}¢/kg`,
x: 405
}
];
metricsData.forEach((metric) => {
const active =
(metric.label === "COUNT" && state.attribute === "count") ||
(metric.label === "MASS" && state.attribute === "mass") ||
(metric.label === "PRICE" && state.attribute === "price");
context.fillStyle = active ? "#f3b83f" : "rgba(255,255,255,.08)";
context.fillRect(metric.x, 102, 165, 58);
context.fillStyle = active ? "#173331" : "#b9c5c0";
context.font = "800 16px ui-sans-serif, system-ui, sans-serif";
context.fillText(metric.label, metric.x + 13, 124);
context.fillStyle = active ? "#173331" : "#fffdf7";
context.font = "900 27px ui-monospace, SFMono-Regular, Menlo, monospace";
context.fillText(metric.value, metric.x + 13, 151);
});
const texture = new THREE.CanvasTexture(canvas);
texture.colorSpace = THREE.SRGBColorSpace;
texture.minFilter = THREE.LinearFilter;
texture.magFilter = THREE.LinearFilter;
return texture;
}
function updateSceneOrder(sorted = sortedLots()) {
sorted.forEach((lot, index) => {
const entry = crateEntries.get(lot.id);
const [x, z] = rankPositions[index];
entry.target.set(x, 0.08, z);
entry.group.userData.rank = index + 1;
const oldTexture = entry.sign.material.map;
entry.sign.material.map = signTexture(
lot,
index + 1,
state.focus === lot.id
);
entry.sign.material.needsUpdate = true;
if (oldTexture) oldTexture.dispose();
});
updateSceneSelection();
}
function updateSceneSelection() {
crateEntries.forEach((entry) => {
const focused = entry.lot.id === state.focus;
entry.selection.visible = focused;
const oldTexture = entry.sign.material.map;
entry.sign.material.map = signTexture(
entry.lot,
entry.group.userData.rank || 1,
focused
);
entry.sign.material.needsUpdate = true;
if (oldTexture) oldTexture.dispose();
});
}
// Custom camera controls. No addons are used.
const cameraState = {
yaw: 0.36,
pitch: 0.73,
zoom: 1.04,
target: new THREE.Vector3(0, 0.65, 0)
};
function updateCamera() {
const radius = 29;
const horizontal = radius * Math.cos(cameraState.pitch);
camera.position.set(
cameraState.target.x + Math.sin(cameraState.yaw) * horizontal,
cameraState.target.y + Math.sin(cameraState.pitch) * radius,
cameraState.target.z + Math.cos(cameraState.yaw) * horizontal
);
camera.lookAt(cameraState.target);
camera.zoom = cameraState.zoom;
camera.updateProjectionMatrix();
}
function resetCamera() {
cameraState.yaw = 0.36;
cameraState.pitch = 0.73;
cameraState.zoom = 1.04;
updateCamera();
}
document.querySelector("#reset-camera").addEventListener("click", resetCamera);
const raycaster = new THREE.Raycaster();
const pointer = new THREE.Vector2();
const canvas = renderer.domElement;
let dragging = false;
let dragged = false;
let pointerStart = { x: 0, y: 0 };
let previousPointer = { x: 0, y: 0 };
function setPointerFromEvent(event) {
const rect = canvas.getBoundingClientRect();
pointer.x = ((event.clientX - rect.left) / rect.width) * 2 - 1;
pointer.y = -((event.clientY - rect.top) / rect.height) * 2 + 1;
}
function pickLot(event) {
setPointerFromEvent(event);
raycaster.setFromCamera(pointer, camera);
const hits = raycaster.intersectObjects(crateRoots, true);
if (!hits.length) return;
const hit = hits[0].object;
const lotId = hit.userData.lotId;
if (lotId) focusLot(lotId);
}
canvas.addEventListener("pointerdown", (event) => {
dragging = true;
dragged = false;
pointerStart = { x: event.clientX, y: event.clientY };
previousPointer = { ...pointerStart };
canvas.setPointerCapture(event.pointerId);
});
canvas.addEventListener("pointermove", (event) => {
if (dragging) {
const dx = event.clientX - previousPointer.x;
const dy = event.clientY - previousPointer.y;
if (
Math.abs(event.clientX - pointerStart.x) +
Math.abs(event.clientY - pointerStart.y) >
4
) {
dragged = true;
}
cameraState.yaw -= dx * 0.006;
cameraState.pitch = THREE.MathUtils.clamp(
cameraState.pitch + dy * 0.004,
0.38,
1.12
);
previousPointer = { x: event.clientX, y: event.clientY };
updateCamera();
canvas.style.cursor = "grabbing";
} else {
setPointerFromEvent(event);
raycaster.setFromCamera(pointer, camera);
canvas.style.cursor = raycaster.intersectObjects(crateRoots, true).length
? "pointer"
: "grab";
}
});
canvas.addEventListener("pointerup", (event) => {
dragging = false;
canvas.style.cursor = "grab";
if (!dragged) pickLot(event);
if (canvas.hasPointerCapture(event.pointerId)) {
canvas.releasePointerCapture(event.pointerId);
}
});
canvas.addEventListener("pointercancel", () => {
dragging = false;
canvas.style.cursor = "grab";
});
canvas.addEventListener(
"wheel",
(event) => {
event.preventDefault();
cameraState.zoom = THREE.MathUtils.clamp(
cameraState.zoom * Math.exp(-event.deltaY * 0.001),
0.72,
1.55
);
updateCamera();
},
{ passive: false }
);
canvas.addEventListener("dblclick", resetCamera);
function resize() {
const width = viewport.clientWidth;
const height = viewport.clientHeight;
if (!width || !height) return;
const aspect = width / height;
const viewHeight = Math.max(13.2, 15.2 / 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);
}
new ResizeObserver(resize).observe(viewport);
resize();
updateCamera();
const clock = new THREE.Clock();
function animate() {
const delta = Math.min(clock.getDelta(), 0.05);
const smoothing = reducedMotion ? 1 : 1 - Math.exp(-delta * 5.5);
crateEntries.forEach((entry) => {
entry.group.position.lerp(entry.target, smoothing);
});
renderer.render(scene, camera);
requestAnimationFrame(animate);
}
updateAll();
animate();
</script>
</body>
</html>
```