Prompt specification
Build or customize the Three.js 3D scene "Hash-load exploration tool" 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: Hash-load exploration tool
Category: Algorithms & Systems · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/hash-load-exploration-tool-x1892
Scene Description Prompt:
Let users add keys and inspect collision counts under a fixed toy hash function; explain that this classroom example does not measure cryptographic security or real library performance. Use Three.js from cdnjs in a single HTML file, with low-poly forms.
Acceptance criteria: Implement adding a key as a local button handler. Test an independently typed key and reflect it in exactly one visible bucket according to the displayed hash.
Technical checks: Add a typed key through a local button. Place it in exactly one bucket according to the displayed hash and recompute bucket occupancy and collision counts from the stored keys.
Return one complete HTML document. The file must run in a sandbox allowing inline scripts and the cdnjs Three.js r160 module. Construct procedural assets, use local event handlers and avoid remote assets, runtime network requests besides Three.js, pointer lock and native form submission. Keep the main requested geometry visible in the initial view.
Working Three.js Starter (hash-load-exploration-tool-x1892):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="color-scheme" content="dark">
<link rel="icon" href="data:,">
<title>Hash Lab — Toy Hash Classroom Model</title>
<style>
:root {
--bg: #07111b;
--panel: rgba(14, 29, 43, 0.9);
--panel-solid: #0e1d2b;
--panel-soft: #122638;
--line: #244154;
--text: #edf7f7;
--muted: #91a9b6;
--cyan: #77dfd0;
--cyan-deep: #287d80;
--gold: #ffc857;
--coral: #ff8066;
--danger: #ff9b8e;
--shadow: 0 22px 70px rgba(0, 0, 0, 0.35);
}
* {
box-sizing: border-box;
}
html {
min-height: 100%;
background: var(--bg);
}
body {
min-height: 100vh;
margin: 0;
color: var(--text);
background:
radial-gradient(circle at 12% 0%, rgba(39, 126, 127, 0.2), transparent 34rem),
radial-gradient(circle at 90% 12%, rgba(255, 128, 102, 0.1), transparent 28rem),
linear-gradient(180deg, #081521 0%, #06101a 100%);
font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}
button,
input {
font: inherit;
}
button {
color: inherit;
}
.shell {
width: min(1480px, calc(100% - 32px));
margin: 0 auto;
padding: 28px 0 40px;
}
.topbar {
display: flex;
align-items: center;
justify-content: space-between;
gap: 24px;
margin-bottom: 22px;
}
.brand {
display: flex;
align-items: center;
gap: 15px;
min-width: 0;
}
.brand-mark {
position: relative;
display: grid;
width: 52px;
height: 52px;
flex: 0 0 auto;
place-items: center;
border: 1px solid rgba(119, 223, 208, 0.45);
border-radius: 15px;
color: #08201f;
background: linear-gradient(145deg, #a2f4e5, #55bfb6);
box-shadow: 0 10px 34px rgba(50, 186, 174, 0.23);
font-size: 25px;
font-weight: 900;
transform: rotate(-3deg);
}
.brand-mark::after {
position: absolute;
right: -5px;
bottom: -5px;
width: 15px;
height: 15px;
border: 3px solid var(--bg);
border-radius: 50%;
background: var(--coral);
content: "";
}
.eyebrow {
margin: 0 0 5px;
color: var(--cyan);
font-size: 0.72rem;
font-weight: 800;
letter-spacing: 0.16em;
text-transform: uppercase;
}
h1 {
margin: 0;
font-size: clamp(1.65rem, 3vw, 2.45rem);
line-height: 1;
letter-spacing: -0.045em;
}
.subtitle {
margin: 8px 0 0;
color: var(--muted);
font-size: 0.95rem;
}
.badges {
display: flex;
flex-wrap: wrap;
justify-content: flex-end;
gap: 8px;
}
.badge {
padding: 8px 11px;
border: 1px solid var(--line);
border-radius: 999px;
color: #c8dbe1;
background: rgba(13, 30, 44, 0.74);
font-size: 0.74rem;
font-weight: 750;
letter-spacing: 0.04em;
white-space: nowrap;
}
.stats {
display: grid;
grid-template-columns: repeat(4, minmax(0, 1fr));
gap: 12px;
margin-bottom: 14px;
}
.stat {
min-width: 0;
padding: 16px 18px;
border: 1px solid var(--line);
border-radius: 16px;
background: linear-gradient(145deg, rgba(18, 38, 56, 0.92), rgba(11, 25, 38, 0.92));
box-shadow: 0 10px 35px rgba(0, 0, 0, 0.16);
}
.stat-label {
display: block;
margin-bottom: 5px;
color: var(--muted);
font-size: 0.72rem;
font-weight: 800;
letter-spacing: 0.09em;
text-transform: uppercase;
}
.stat-value {
display: block;
font-size: 1.72rem;
font-weight: 850;
line-height: 1.15;
font-variant-numeric: tabular-nums;
}
.stat-detail {
display: block;
min-height: 1.2em;
margin-top: 5px;
overflow: hidden;
color: #9db4bf;
font-size: 0.76rem;
text-overflow: ellipsis;
white-space: nowrap;
}
.stat.collision {
border-color: rgba(255, 128, 102, 0.42);
}
.stat.collision .stat-value {
color: var(--coral);
}
.workspace {
display: grid;
grid-template-columns: 355px minmax(0, 1fr);
gap: 14px;
align-items: stretch;
}
.panel,
.scene-panel {
border: 1px solid var(--line);
border-radius: 20px;
background: var(--panel);
box-shadow: var(--shadow);
}
.control-panel {
padding: 20px;
}
.section-heading {
display: flex;
align-items: baseline;
justify-content: space-between;
gap: 12px;
margin-bottom: 11px;
}
.section-heading h2,
.scene-heading h2 {
margin: 0;
font-size: 1rem;
letter-spacing: -0.01em;
}
.section-note {
color: var(--muted);
font-size: 0.75rem;
}
.input-label {
display: block;
margin-bottom: 8px;
color: #b8ccd5;
font-size: 0.76rem;
font-weight: 750;
}
.input-wrap {
position: relative;
}
#keyInput {
width: 100%;
height: 49px;
padding: 0 13px;
border: 1px solid #36536a;
border-radius: 11px;
outline: none;
color: var(--text);
background: #081722;
box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.03);
transition: border-color 160ms ease, box-shadow 160ms ease;
}
#keyInput::placeholder {
color: #6f8998;
}
#keyInput:focus {
border-color: var(--cyan);
box-shadow: 0 0 0 3px rgba(119, 223, 208, 0.13);
}
.add-button {
width: 100%;
height: 46px;
margin-top: 10px;
border: 0;
border-radius: 11px;
color: #09201e;
background: linear-gradient(135deg, #9af0df, #5bc8ba);
box-shadow: 0 10px 26px rgba(71, 194, 177, 0.2);
cursor: pointer;
font-weight: 850;
transition: transform 140ms ease, filter 140ms ease, box-shadow 140ms ease;
}
.add-button:hover {
filter: brightness(1.07);
transform: translateY(-1px);
box-shadow: 0 13px 30px rgba(71, 194, 177, 0.27);
}
.add-button:active {
transform: translateY(1px);
}
.add-button:focus-visible,
.reset-button:focus-visible,
.key-row:focus-visible {
outline: 3px solid rgba(255, 200, 87, 0.75);
outline-offset: 2px;
}
.preview {
min-height: 48px;
margin-top: 12px;
padding: 10px 12px;
border: 1px solid rgba(119, 223, 208, 0.2);
border-radius: 11px;
color: #bcece4;
background: rgba(38, 116, 116, 0.1);
font-size: 0.8rem;
font-variant-numeric: tabular-nums;
line-height: 1.45;
}
.preview strong {
color: var(--cyan);
}
.status {
min-height: 38px;
padding: 9px 2px 0;
color: var(--muted);
font-size: 0.78rem;
line-height: 1.4;
}
.status.success {
color: #a9e9d9;
}
.status.error {
color: var(--danger);
}
.divider {
height: 1px;
margin: 17px 0;
background: var(--line);
}
.formula {
margin: 0;
padding: 13px 14px;
border: 1px solid #2a4659;
border-radius: 12px;
color: #cfe9e8;
background: #081722;
font-family: "SFMono-Regular", Consolas, "Liberation Mono", monospace;
font-size: 0.78rem;
line-height: 1.65;
white-space: pre-line;
}
.method-copy {
margin: 11px 0 0;
color: var(--muted);
font-size: 0.78rem;
line-height: 1.55;
}
.method-copy strong {
color: #c9dce2;
}
.register-heading {
display: flex;
align-items: center;
justify-content: space-between;
margin-bottom: 8px;
}
.register-heading h2 {
margin: 0;
font-size: 0.91rem;
}
.register-hint {
color: var(--muted);
font-size: 0.69rem;
}
.register {
max-height: 274px;
overflow: auto;
padding-right: 3px;
scrollbar-color: #345469 transparent;
scrollbar-width: thin;
}
.register-columns,
.key-row {
display: grid;
grid-template-columns: minmax(0, 1fr) 58px 38px;
gap: 8px;
align-items: center;
}
.register-columns {
padding: 0 10px 7px;
color: #718e9d;
font-size: 0.65rem;
font-weight: 800;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.key-row {
width: 100%;
min-height: 38px;
margin: 0 0 5px;
padding: 8px 10px;
border: 1px solid transparent;
border-radius: 9px;
color: #d9e8ec;
background: rgba(22, 45, 62, 0.58);
cursor: pointer;
text-align: left;
font-size: 0.77rem;
font-variant-numeric: tabular-nums;
transition: background 140ms ease, border-color 140ms ease;
}
.key-row:hover {
border-color: #385b70;
background: rgba(30, 61, 80, 0.78);
}
.key-row.new {
animation: row-in 500ms ease both;
}
@keyframes row-in {
from {
opacity: 0;
transform: translateY(-7px);
background: rgba(119, 223, 208, 0.24);
}
to {
opacity: 1;
transform: translateY(0);
}
}
.key-name {
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}
.hash-value {
color: #90aeba;
}
.bucket-chip {
display: inline-grid;
min-width: 30px;
height: 22px;
place-items: center;
border: 1px solid rgba(119, 223, 208, 0.25);
border-radius: 7px;
color: var(--cyan);
background: rgba(42, 129, 127, 0.12);
font-weight: 850;
}
.key-row.shared .bucket-chip {
border-color: rgba(255, 128, 102, 0.38);
color: #ffac97;
background: rgba(255, 128, 102, 0.1);
}
.scene-panel {
position: relative;
min-height: 685px;
overflow: hidden;
isolation: isolate;
background: #081521;
}
#viewport {
position: absolute;
inset: 0;
z-index: 0;
}
#viewport canvas {
display: block;
width: 100%;
height: 100%;
cursor: grab;
outline: none;
touch-action: none;
}
#viewport canvas:active {
cursor: grabbing;
}
#viewport canvas:focus-visible {
outline: 3px solid rgba(255, 200, 87, 0.7);
outline-offset: -4px;
}
.scene-heading {
position: absolute;
top: 18px;
left: 19px;
z-index: 5;
display: flex;
align-items: flex-start;
justify-content: space-between;
width: calc(100% - 38px);
gap: 15px;
pointer-events: none;
}
.scene-heading p {
margin: 5px 0 0;
color: var(--muted);
font-size: 0.76rem;
}
.reset-button {
flex: 0 0 auto;
padding: 8px 11px;
border: 1px solid #365164;
border-radius: 9px;
color: #c1d6dd;
background: rgba(10, 26, 39, 0.85);
cursor: pointer;
pointer-events: auto;
font-size: 0.73rem;
font-weight: 750;
}
.reset-button:hover {
border-color: var(--cyan);
color: white;
}
.scene-legend {
position: absolute;
top: 83px;
left: 19px;
z-index: 5;
display: flex;
flex-wrap: wrap;
gap: 12px;
color: #a8bec7;
font-size: 0.68rem;
pointer-events: none;
}
.legend-item {
display: flex;
align-items: center;
gap: 6px;
}
.legend-swatch {
width: 9px;
height: 9px;
border-radius: 3px;
background: var(--cyan-deep);
box-shadow: inset 0 0 0 1px rgba(255, 255, 255, 0.15);
}
.legend-swatch.key {
background: var(--gold);
transform: rotate(45deg);
border-radius: 2px;
}
.legend-swatch.selected {
background: var(--coral);
border-radius: 50%;
}
.bucket-label {
position: absolute;
z-index: 3;
display: flex;
align-items: center;
gap: 6px;
padding: 5px 7px 5px 6px;
border: 1px solid rgba(100, 146, 162, 0.48);
border-radius: 9px;
color: #d9ecef;
background: rgba(7, 19, 30, 0.85);
box-shadow: 0 6px 18px rgba(0, 0, 0, 0.25);
pointer-events: none;
transform: translate(-50%, -50%);
white-space: nowrap;
backdrop-filter: blur(7px);
}
.bucket-label.hot {
border-color: rgba(255, 128, 102, 0.72);
color: #ffd1c5;
}
.bucket-id {
display: inline-grid;
min-width: 29px;
height: 22px;
place-items: center;
border-radius: 6px;
color: #09201f;
background: var(--cyan);
font-size: 0.7rem;
font-weight: 900;
}
.bucket-label.hot .bucket-id {
color: #32120d;
background: var(--coral);
}
.bucket-count {
color: #9db6c0;
font-size: 0.66rem;
font-weight: 750;
}
.key-tag {
position: absolute;
z-index: 6;
display: none;
align-items: center;
gap: 7px;
padding: 7px 9px;
border: 1px solid rgba(255, 200, 87, 0.68);
border-radius: 9px;
color: #fff3cd;
background: rgba(41, 29, 12, 0.92);
box-shadow: 0 9px 25px rgba(0, 0, 0, 0.34);
pointer-events: none;
transform: translate(-50%, -100%);
white-space: nowrap;
backdrop-filter: blur(7px);
font-size: 0.72rem;
font-weight: 800;
}
.key-tag::after {
position: absolute;
bottom: -5px;
left: 50%;
width: 8px;
height: 8px;
border-right: 1px solid rgba(255, 200, 87, 0.68);
border-bottom: 1px solid rgba(255, 200, 87, 0.68);
background: #291d0c;
content: "";
transform: translateX(-50%) rotate(45deg);
}
.key-tag-hash {
color: var(--gold);
font-variant-numeric: tabular-nums;
}
.bucket-inspector {
position: absolute;
left: 18px;
bottom: 18px;
z-index: 5;
width: min(355px, calc(100% - 36px));
padding: 11px 13px;
border: 1px solid rgba(61, 94, 111, 0.85);
border-radius: 11px;
background: rgba(7, 19, 30, 0.88);
pointer-events: none;
backdrop-filter: blur(9px);
}
.inspector-title {
margin-bottom: 3px;
color: var(--cyan);
font-size: 0.72rem;
font-weight: 850;
letter-spacing: 0.04em;
}
.inspector-detail {
color: #aec4cc;
font-size: 0.72rem;
line-height: 1.4;
}
.gesture-hint {
position: absolute;
right: 18px;
bottom: 18px;
z-index: 5;
padding: 8px 10px;
border: 1px solid rgba(61, 94, 111, 0.7);
border-radius: 9px;
color: #8fa9b4;
background: rgba(7, 19, 30, 0.82);
pointer-events: none;
font-size: 0.68rem;
}
.notice {
display: flex;
align-items: flex-start;
gap: 13px;
margin-top: 14px;
padding: 16px 18px;
border: 1px solid rgba(255, 200, 87, 0.28);
border-radius: 16px;
color: #b8c9cf;
background: rgba(50, 42, 22, 0.35);
font-size: 0.8rem;
line-height: 1.55;
}
.notice-icon {
display: grid;
width: 29px;
height: 29px;
flex: 0 0 auto;
place-items: center;
border: 1px solid rgba(255, 200, 87, 0.45);
border-radius: 50%;
color: var(--gold);
font-weight: 900;
}
.notice strong {
color: #ffe6a8;
}
@media (max-width: 1050px) {
.workspace {
grid-template-columns: 310px minmax(0, 1fr);
}
.scene-panel {
min-height: 640px;
}
.gesture-hint {
display: none;
}
}
@media (max-width: 820px) {
.shell {
width: min(100% - 20px, 680px);
padding-top: 18px;
}
.topbar {
align-items: flex-start;
}
.badges {
display: none;
}
.stats {
grid-template-columns: repeat(2, minmax(0, 1fr));
}
.workspace {
grid-template-columns: 1fr;
}
.scene-panel {
min-height: 570px;
}
.register {
max-height: 230px;
}
}
@media (max-width: 520px) {
.shell {
width: calc(100% - 14px);
}
.brand-mark {
width: 44px;
height: 44px;
border-radius: 12px;
font-size: 21px;
}
h1 {
font-size: 1.55rem;
}
.subtitle {
font-size: 0.78rem;
}
.stat {
padding: 13px;
}
.stat-value {
font-size: 1.45rem;
}
.control-panel {
padding: 16px;
}
.scene-panel {
min-height: 500px;
}
.scene-heading {
top: 14px;
left: 14px;
width: calc(100% - 28px);
}
.scene-legend {
top: 76px;
left: 14px;
}
.bucket-inspector {
left: 12px;
bottom: 12px;
width: calc(100% - 24px);
}
}
@media (prefers-reduced-motion: reduce) {
*,
*::before,
*::after {
scroll-behavior: auto !important;
animation-duration: 0.01ms !important;
animation-iteration-count: 1 !important;
transition-duration: 0.01ms !important;
}
}
</style>
</head>
<body>
<main class="shell">
<header class="topbar">
<div class="brand">
<div class="brand-mark" aria-hidden="true">H</div>
<div>
<p class="eyebrow">Classroom visualization</p>
<h1>Hash Lab</h1>
<p class="subtitle">Add keys, inspect their buckets, and count collisions.</p>
</div>
</div>
<div class="badges" aria-label="Model information">
<span class="badge">Fixed toy hash</span>
<span class="badge">7 buckets</span>
<span class="badge">Three.js r160</span>
</div>
</header>
<section class="stats" aria-label="Live hash table statistics">
<article class="stat">
<span class="stat-label">Keys inserted</span>
<strong class="stat-value" id="keyCount">0</strong>
<span class="stat-detail" id="keyCountDetail">Seven-key demonstration</span>
</article>
<article class="stat">
<span class="stat-label">Occupied buckets</span>
<strong class="stat-value" id="occupiedCount">0 / 7</strong>
<span class="stat-detail" id="occupiedDetail">Waiting for keys</span>
</article>
<article class="stat collision">
<span class="stat-label">Collision events</span>
<strong class="stat-value" id="collisionCount">0</strong>
<span class="stat-detail" id="collisionDetail">No shared buckets yet</span>
</article>
<article class="stat">
<span class="stat-label">Busiest bucket</span>
<strong class="stat-value" id="busiestValue">—</strong>
<span class="stat-detail" id="busiestDetail">Maximum load</span>
</article>
</section>
<div class="workspace">
<aside class="panel control-panel">
<div class="section-heading">
<h2>Add a key</h2>
<span class="section-note">Local interaction</span>
</div>
<label class="input-label" for="keyInput">Key string</label>
<div class="input-wrap">
<input
id="keyInput"
type="text"
placeholder="Type any key, e.g. lantern"
autocomplete="off"
autocapitalize="off"
spellcheck="false"
>
</div>
<button class="add-button" id="addKeyButton" type="button">+ Add key</button>
<div class="preview" id="hashPreview">
Type a key to preview its displayed hash and destination.
</div>
<div class="status" id="statusMessage" role="status" aria-live="polite">
Ready. Type a key and use the Add key button.
</div>
<div class="divider"></div>
<div class="section-heading">
<h2>Fixed mapping</h2>
<span class="section-note">Unicode code points</span>
</div>
<pre class="formula">H(s) = (Σ codePoint(sᵢ)) mod 997
B(s) = H(s) mod 7</pre>
<p class="method-copy">
The destination is always <strong>B = H mod 7</strong>. Different hash values can still
share a bucket because the final result is reduced to seven destinations.
</p>
<p class="method-copy">
An <strong>collision event</strong> is each extra key in an already occupied bucket.
The <strong>pair count</strong> is <strong>n(n−1)/2</strong> for a bucket holding n keys.
</p>
<div class="divider"></div>
<div class="register-heading">
<h2>Key register</h2>
<span class="register-hint">H → bucket</span>
</div>
<div class="register" id="keyRegister" aria-label="Inserted keys and their hashes">
<div class="register-columns">
<span>Key</span>
<span>Hash H</span>
<span>Bucket</span>
</div>
<div id="registerRows"></div>
</div>
</aside>
<section class="scene-panel" aria-label="Interactive 3D hash bucket visualization">
<div id="viewport"></div>
<div class="scene-heading">
<div>
<p class="eyebrow">Bucket field</p>
<h2>One fixed mapping, seven destinations</h2>
<p>Each gold token is one inserted key.</p>
</div>
<button class="reset-button" id="resetViewButton" type="button">Reset view</button>
</div>
<div class="scene-legend" aria-hidden="true">
<span class="legend-item"><span class="legend-swatch"></span>Bucket</span>
<span class="legend-item"><span class="legend-swatch key"></span>Key token</span>
<span class="legend-item"><span class="legend-swatch selected"></span>Selected bucket</span>
</div>
<div class="bucket-inspector">
<div class="inspector-title" id="inspectorTitle">Bucket B0</div>
<div class="inspector-detail" id="inspectorDetail">Inspect the bucket field.</div>
</div>
<div class="gesture-hint">Drag to orbit · wheel to zoom · click a bucket</div>
<div class="key-tag" id="selectedKeyTag">
<span id="selectedKeyName"></span>
<span class="key-tag-hash" id="selectedKeyHash"></span>
</div>
</section>
</div>
<section class="notice" aria-label="Model limitations">
<span class="notice-icon" aria-hidden="true">!</span>
<div>
<strong>Classroom model, not a security or performance test.</strong>
These collision counts are arithmetic consequences of the deliberately simple toy hash shown here.
The visualization does <strong>not</strong> measure cryptographic security, resistance to attacks, or
real hash-library performance. Real-world behavior also depends on capacity, resizing, probing,
encoding, workloads, and implementation choices.
</div>
</section>
</main>
<script type="module">
import * as THREE from "https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
const BUCKET_COUNT = 7;
const HASH_MODULUS = 997;
const INITIAL_KEYS = ["red", "oak", "ink", "raw", "owl", "zip", "jam"];
const viewport = document.getElementById("viewport");
const keyInput = document.getElementById("keyInput");
const addKeyButton = document.getElementById("addKeyButton");
const resetViewButton = document.getElementById("resetViewButton");
const hashPreview = document.getElementById("hashPreview");
const statusMessage = document.getElementById("statusMessage");
const registerRows = document.getElementById("registerRows");
const keyCount = document.getElementById("keyCount");
const keyCountDetail = document.getElementById("keyCountDetail");
const occupiedCount = document.getElementById("occupiedCount");
const occupiedDetail = document.getElementById("occupiedDetail");
const collisionCount = document.getElementById("collisionCount");
const collisionDetail = document.getElementById("collisionDetail");
const busiestValue = document.getElementById("busiestValue");
const busiestDetail = document.getElementById("busiestDetail");
const inspectorTitle = document.getElementById("inspectorTitle");
const inspectorDetail = document.getElementById("inspectorDetail");
const selectedKeyTag = document.getElementById("selectedKeyTag");
const selectedKeyName = document.getElementById("selectedKeyName");
const selectedKeyHash = document.getElementById("selectedKeyHash");
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x081521);
scene.fog = new THREE.Fog(0x081521, 15, 28);
const camera = new THREE.PerspectiveCamera(40, 1, 0.1, 100);
const renderer = new THREE.WebGLRenderer({ antialias: true, alpha: false });
renderer.setPixelRatio(Math.min(window.devicePixelRatio || 1, 1.75));
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFShadowMap;
renderer.domElement.tabIndex = 0;
renderer.domElement.setAttribute(
"aria-label",
"Interactive 3D hash bucket field. Drag to orbit, use the mouse wheel to zoom, or use arrow keys when focused."
);
viewport.appendChild(renderer.domElement);
const hemisphere = new THREE.HemisphereLight(0xb8f5ef, 0x172132, 2.25);
scene.add(hemisphere);
const keyLight = new THREE.DirectionalLight(0xffedc2, 3.2);
keyLight.position.set(-4, 9, 7);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(1024, 1024);
keyLight.shadow.camera.left = -9;
keyLight.shadow.camera.right = 9;
keyLight.shadow.camera.top = 9;
keyLight.shadow.camera.bottom = -9;
keyLight.shadow.camera.near = 1;
keyLight.shadow.camera.far = 25;
scene.add(keyLight);
const rimLight = new THREE.DirectionalLight(0x55d8df, 2.1);
rimLight.position.set(6, 5, -5);
scene.add(rimLight);
const floor = new THREE.Mesh(
new THREE.PlaneGeometry(24, 24),
new THREE.MeshStandardMaterial({
color: 0x0a1823,
roughness: 0.92,
metalness: 0.04
})
);
floor.rotation.x = -Math.PI / 2;
floor.position.y = -0.115;
floor.receiveShadow = true;
scene.add(floor);
const grid = new THREE.GridHelper(18, 18, 0x315367, 0x19303f);
grid.position.y = -0.105;
grid.material.transparent = true;
grid.material.opacity = 0.42;
scene.add(grid);
const bucketPositions = [
[-3.15, -1.08],
[-1.05, -1.08],
[1.05, -1.08],
[3.15, -1.08],
[-2.10, 1.16],
[0.00, 1.16],
[2.10, 1.16]
];
const bucketObjects = [];
const bucketContents = Array.from({ length: BUCKET_COUNT }, () => []);
const keyRecords = [];
const keyNames = new Set();
const interactiveMeshes = [];
const bucketBodyGeometry = new THREE.CylinderGeometry(0.69, 0.53, 0.72, 7, 1, true);
const bucketBottomGeometry = new THREE.CylinderGeometry(0.53, 0.53, 0.065, 7);
const pedestalGeometry = new THREE.CylinderGeometry(0.79, 0.83, 0.18, 8);
const rimGeometry = new THREE.TorusGeometry(0.69, 0.047, 4, 7);
const keyHeadGeometry = new THREE.TorusGeometry(0.115, 0.035, 4, 6);
const keyShaftGeometry = new THREE.BoxGeometry(0.075, 0.34, 0.07);
const keyToothGeometry = new THREE.BoxGeometry(0.14, 0.055, 0.07);
const bucketPalette = [
0x246e78,
0x296a7d,
0x267080,
0x2b6b73,
0x256b79,
0x2c657c,
0x286f77
];
let selectedBucket = 0;
let selectedKey = null;
let newestRecord = null;
function createBucket(index, position) {
const group = new THREE.Group();
group.position.set(position[0], 0, position[1]);
scene.add(group);
const bodyMaterial = new THREE.MeshStandardMaterial({
color: bucketPalette[index],
roughness: 0.58,
metalness: 0.12,
flatShading: true,
side: THREE.DoubleSide,
emissive: 0x000000,
emissiveIntensity: 0
});
const rimMaterial = new THREE.MeshStandardMaterial({
color: 0x81d9cf,
roughness: 0.48,
metalness: 0.18,
flatShading: true,
emissive: 0x000000,
emissiveIntensity: 0
});
const pedestal = new THREE.Mesh(
pedestalGeometry,
new THREE.MeshStandardMaterial({
color: 0x142c3c,
roughness: 0.82,
metalness: 0.08,
flatShading: true
})
);
pedestal.position.y = -0.015;
pedestal.castShadow = true;
pedestal.receiveShadow = true;
pedestal.userData.bucketIndex = index;
group.add(pedestal);
interactiveMeshes.push(pedestal);
const body = new THREE.Mesh(bucketBodyGeometry, bodyMaterial);
body.position.y = 0.46;
body.castShadow = true;
body.receiveShadow = true;
body.userData.bucketIndex = index;
group.add(body);
interactiveMeshes.push(body);
const bottom = new THREE.Mesh(
bucketBottomGeometry,
new THREE.MeshStandardMaterial({
color: 0x153d4b,
roughness: 0.72,
flatShading: true
})
);
bottom.position.y = 0.12;
bottom.receiveShadow = true;
group.add(bottom);
const rim = new THREE.Mesh(rimGeometry, rimMaterial);
rim.rotation.x = Math.PI / 2;
rim.position.y = 0.825;
rim.castShadow = true;
group.add(rim);
const label = document.createElement("div");
label.className = "bucket-label";
label.innerHTML = `
<span class="bucket-id">B${index}</span>
<span class="bucket-count">0 keys</span>
`;
viewport.parentElement.appendChild(label);
bucketObjects.push({
group,
bodyMaterial,
rimMaterial,
label,
countLabel: label.querySelector(".bucket-count")
});
}
bucketPositions.forEach((position, index) => createBucket(index, position));
const haloMaterial = new THREE.MeshBasicMaterial({
color: 0xff8066,
transparent: true,
opacity: 0.78,
depthWrite: false
});
const selectionHalo = new THREE.Mesh(
new THREE.TorusGeometry(0.91, 0.032, 4, 7),
haloMaterial
);
selectionHalo.rotation.x = Math.PI / 2;
selectionHalo.position.y = 0.012;
scene.add(selectionHalo);
function hashKey(value) {
let sum = 0;
for (const character of value) {
sum += character.codePointAt(0);
}
return sum % HASH_MODULUS;
}
function bucketForHash(hash) {
return hash % BUCKET_COUNT;
}
function keySlot(index) {
const slots = [
[0, 0],
[-0.27, 0.01],
[0.27, 0.01],
[-0.31, -0.04],
[0, 0.13],
[0.31, -0.04],
[-0.36, -0.13],
[0.36, -0.13],
[0, -0.17]
];
return slots[index] || [0, -0.17];
}
function createKeyVisual(record, isNew = false) {
const material = new THREE.MeshStandardMaterial({
color: isNew ? 0xff8a68 : 0xffc857,
roughness: 0.4,
metalness: 0.22,
flatShading: true,
emissive: 0x000000,
emissiveIntensity: 0
});
const group = new THREE.Group();
const head = new THREE.Mesh(keyHeadGeometry, material);
head.position.y = 0.15;
head.castShadow = true;
group.add(head);
const shaft = new THREE.Mesh(keyShaftGeometry, material);
shaft.position.y = -0.11;
shaft.castShadow = true;
group.add(shaft);
const tooth = new THREE.Mesh(keyToothGeometry, material);
tooth.position.set(0.045, -0.23, 0);
tooth.castShadow = true;
group.add(tooth);
const slot = keySlot(bucketContents[record.bucket].length - 1);
const baseY = 1.33 + (bucketContents[record.bucket].length % 2) * 0.07;
group.position.set(slot[0], baseY, slot[1]);
group.rotation.y = ((record.hash % 7) - 3) * 0.07;
bucketObjects[record.bucket].group.add(group);
record.visual = {
group,
material,
baseY,
phase: record.hash * 0.17 + bucketContents[record.bucket].length * 0.8
};
}
function insertKey(value, isNew = false) {
const hash = hashKey(value);
const bucket = bucketForHash(hash);
const record = { key: value, hash, bucket, visual: null };
keyRecords.push(record);
keyNames.add(value);
bucketContents[bucket].push(record);
createKeyVisual(record, isNew);
return record;
}
function crowdedSummary() {
const crowded = bucketContents
.map((contents, index) => ({ index, count: contents.length }))
.filter((entry) => entry.count > 1);
if (!crowded.length) {
return "No shared buckets yet";
}
const pairCount = crowded.reduce(
(total, entry) => total + (entry.count * (entry.count - 1)) / 2,
0
);
const locations = crowded
.map((entry) => `B${entry.index} has ${entry.count} keys`)
.join(" · ");
return `${pairCount} pair combinations · ${locations}`;
}
function updateStats() {
const counts = bucketContents.map((contents) => contents.length);
const occupied = counts.filter((count) => count > 0).length;
const events = counts.reduce((total, count) => total + Math.max(0, count - 1), 0);
const maxCount = Math.max(...counts);
const maxIndex = counts.indexOf(maxCount);
keyCount.textContent = String(keyRecords.length);
keyCountDetail.textContent = keyRecords.length === 1
? "One key in the table"
: `${keyRecords.length} distinct key strings`;
occupiedCount.textContent = `${occupied} / ${BUCKET_COUNT}`;
occupiedDetail.textContent = occupied === BUCKET_COUNT
? "Every bucket is occupied"
: `${BUCKET_COUNT - occupied} bucket${BUCKET_COUNT - occupied === 1 ? "" : "s"} still empty`;
collisionCount.textContent = String(events);
collisionDetail.textContent = crowdedSummary();
busiestValue.textContent = maxCount ? `${maxCount} / 7` : "—";
busiestDetail.textContent = maxCount
? `B${maxIndex} · maximum bucket load`
: "Maximum load";
bucketObjects.forEach((object, index) => {
const count = counts[index];
object.countLabel.textContent = `${count} ${count === 1 ? "key" : "keys"}`;
object.label.classList.toggle("hot", count > 1);
});
}
function updateRegister() {
registerRows.replaceChildren();
keyRecords.forEach((record) => {
const row = document.createElement("button");
row.type = "button";
row.className = "key-row";
if (record === newestRecord) {
row.classList.add("new");
}
if (bucketContents[record.bucket].length > 1) {
row.classList.add("shared");
}
const name = document.createElement("span");
name.className = "key-name";
name.textContent = record.key;
const hash = document.createElement("span");
hash.className = "hash-value";
hash.textContent = String(record.hash);
const bucket = document.createElement("span");
bucket.className = "bucket-chip";
bucket.textContent = `B${record.bucket}`;
row.append(name, hash, bucket);
row.setAttribute(
"aria-label",
`${record.key}, hash ${record.hash}, bucket ${record.bucket}. Select this bucket.`
);
row.addEventListener("click", () => {
focusBucket(record.bucket);
inspectBucket(record.bucket);
});
registerRows.appendChild(row);
});
}
function focusBucket(index) {
selectedBucket = index;
const object = bucketObjects[index];
bucketObjects.forEach((entry, entryIndex) => {
const active = entryIndex === index;
entry.bodyMaterial.emissive.setHex(active ? 0x0b4547 : 0x000000);
entry.bodyMaterial.emissiveIntensity = active ? 0.8 : 0;
entry.rimMaterial.emissive.setHex(active ? 0x6a261c : 0x000000);
entry.rimMaterial.emissiveIntensity = active ? 0.58 : 0;
});
selectionHalo.position.x = object.group.position.x;
selectionHalo.position.z = object.group.position.z;
selectionHalo.visible = true;
keyRecords.forEach((record) => {
if (!record.visual) return;
const isSelected = selectedKey && record === selectedKey;
record.visual.material.color.setHex(isSelected ? 0xff8a68 : 0xffc857);
record.visual.material.emissive.setHex(isSelected ? 0x5f1c11 : 0x000000);
record.visual.material.emissiveIntensity = isSelected ? 0.48 : 0;
});
const showKey = selectedKey && selectedKey.bucket === index;
selectedKeyTag.style.display = showKey ? "flex" : "none";
if (showKey) {
selectedKeyName.textContent = selectedKey.key;
selectedKeyHash.textContent = `H ${selectedKey.hash} → B ${selectedKey.bucket}`;
}
}
function inspectBucket(index) {
const contents = bucketContents[index];
inspectorTitle.textContent = `Bucket B${index}`;
inspectorDetail.textContent = contents.length
? `${contents.length} ${contents.length === 1 ? "key" : "keys"}: ${contents.map((record) => record.key).join(", ")}`
: "This bucket is empty. A new key can land here.";
}
function updatePreview() {
const value = keyInput.value;
if (!value.length) {
hashPreview.textContent = "Type a key to preview its displayed hash and destination.";
return;
}
const hash = hashKey(value);
const bucket = bucketForHash(hash);
hashPreview.innerHTML = `
Preview: <strong>H = ${hash}</strong> → <strong>B = ${bucket}</strong><br>
The key will be placed only in bucket ${bucket}.
`;
}
function addTypedKey() {
const value = keyInput.value;
if (!value.trim()) {
statusMessage.className = "status error";
statusMessage.textContent = "Enter a non-empty key before adding it.";
keyInput.focus();
return;
}
if (keyNames.has(value)) {
const existing = keyRecords.find((record) => record.key === value);
statusMessage.className = "status error";
statusMessage.textContent = existing
? `“${value}” is already in bucket B${existing.bucket}. The table was not changed.`
: "That key is already present. The table was not changed.";
keyInput.select();
return;
}
const record = insertKey(value, true);
newestRecord = record;
selectedKey = record;
focusBucket(record.bucket);
inspectBucket(record.bucket);
updateStats();
updateRegister();
inspectorTitle.textContent = `Bucket B${record.bucket}`;
inspectorDetail.textContent =
`Added “${record.key}” with H ${record.hash}. ` +
`The new gold token appears in this bucket only.`;
statusMessage.className = "status success";
statusMessage.textContent =
`Added “${record.key}” → H ${record.hash} → B ${record.bucket}. ` +
`The token was inserted exactly once.`;
keyInput.value = "";
updatePreview();
keyInput.focus();
}
addKeyButton.addEventListener("click", addTypedKey);
keyInput.addEventListener("input", updatePreview);
keyInput.addEventListener("keydown", (event) => {
if (event.key === "Enter") {
event.preventDefault();
addTypedKey();
}
});
INITIAL_KEYS.forEach((key) => insertKey(key, false));
updateStats();
updateRegister();
focusBucket(0);
inspectBucket(0);
let yaw = 0;
let pitch = 0.61;
let cameraDistance = 13.8;
const cameraTarget = new THREE.Vector3(0, 0.38, 0);
function updateCamera() {
const horizontal = Math.cos(pitch) * cameraDistance;
camera.position.set(
cameraTarget.x + Math.sin(yaw) * horizontal,
cameraTarget.y + Math.sin(pitch) * cameraDistance,
cameraTarget.z + Math.cos(yaw) * horizontal
);
camera.lookAt(cameraTarget);
camera.updateMatrixWorld();
}
function resetCamera() {
yaw = 0;
pitch = 0.61;
cameraDistance = 13.8;
updateCamera();
}
resetViewButton.addEventListener("click", resetCamera);
const raycaster = new THREE.Raycaster();
const pointer = new THREE.Vector2();
let dragging = false;
let pointerMoved = false;
let lastPointerX = 0;
let lastPointerY = 0;
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 bucketAtPointer(event) {
setPointerFromEvent(event);
raycaster.setFromCamera(pointer, camera);
const hits = raycaster.intersectObjects(interactiveMeshes, false);
return hits.length ? hits[0].object.userData.bucketIndex : null;
}
renderer.domElement.addEventListener("pointerdown", (event) => {
if (event.button !== 0) return;
dragging = true;
pointerMoved = false;
lastPointerX = event.clientX;
lastPointerY = event.clientY;
renderer.domElement.setPointerCapture(event.pointerId);
});
renderer.domElement.addEventListener("pointermove", (event) => {
if (dragging) {
const deltaX = event.clientX - lastPointerX;
const deltaY = event.clientY - lastPointerY;
if (Math.abs(event.clientX - lastPointerX) + Math.abs(event.clientY - lastPointerY) > 1) {
pointerMoved = true;
}
yaw -= deltaX * 0.006;
pitch = THREE.MathUtils.clamp(pitch + deltaY * 0.005, 0.28, 1.18);
lastPointerX = event.clientX;
lastPointerY = event.clientY;
updateCamera();
} else {
renderer.domElement.style.cursor = bucketAtPointer(event) === null ? "grab" : "pointer";
}
});
renderer.domElement.addEventListener("pointerup", (event) => {
if (dragging && !pointerMoved) {
const index = bucketAtPointer(event);
if (index !== null) {
focusBucket(index);
inspectBucket(index);
}
}
dragging = false;
if (renderer.domElement.hasPointerCapture(event.pointerId)) {
renderer.domElement.releasePointerCapture(event.pointerId);
}
});
renderer.domElement.addEventListener("pointercancel", () => {
dragging = false;
});
renderer.domElement.addEventListener("wheel", (event) => {
event.preventDefault();
cameraDistance = THREE.MathUtils.clamp(
cameraDistance + event.deltaY * 0.009,
8.5,
20
);
updateCamera();
}, { passive: false });
renderer.domElement.addEventListener("keydown", (event) => {
let handled = true;
switch (event.key) {
case "ArrowLeft":
yaw -= 0.1;
break;
case "ArrowRight":
yaw += 0.1;
break;
case "ArrowUp":
pitch = THREE.MathUtils.clamp(pitch + 0.07, 0.28, 1.18);
break;
case "ArrowDown":
pitch = THREE.MathUtils.clamp(pitch - 0.07, 0.28, 1.18);
break;
case "+":
case "=":
cameraDistance = Math.max(8.5, cameraDistance - 0.6);
break;
case "-":
case "_":
cameraDistance = Math.min(20, cameraDistance + 0.6);
break;
default:
handled = false;
}
if (handled) {
event.preventDefault();
updateCamera();
}
});
function resize() {
const width = Math.max(1, viewport.clientWidth);
const height = Math.max(1, viewport.clientHeight);
camera.aspect = width / height;
camera.updateProjectionMatrix();
renderer.setSize(width, height, false);
}
if ("ResizeObserver" in window) {
new ResizeObserver(resize).observe(viewport);
}
window.addEventListener("resize", resize);
resize();
updateCamera();
const projected = new THREE.Vector3();
const worldPosition = new THREE.Vector3();
const clock = new THREE.Clock();
function positionOverlay(element, world, offsetY = 0) {
projected.copy(world);
projected.y += offsetY;
projected.project(camera);
const width = viewport.clientWidth;
const height = viewport.clientHeight;
const x = (projected.x * 0.5 + 0.5) * width;
const y = (-projected.y * 0.5 + 0.5) * height;
const visible = projected.z > -1 && projected.z < 1 && x > -80 && x < width + 80 && y > -50 && y < height + 50;
element.style.left = `${x}px`;
element.style.top = `${y}px`;
element.style.opacity = visible ? "1" : "0";
}
function animate() {
const time = clock.getElapsedTime();
keyRecords.forEach((record) => {
if (!record.visual) return;
record.visual.group.position.y =
record.visual.baseY + Math.sin(time * 1.7 + record.visual.phase) * 0.035;
record.visual.group.rotation.z =
Math.sin(time * 1.15 + record.visual.phase) * 0.025;
});
const pulse = 1 + Math.sin(time * 3.4) * 0.045;
selectionHalo.scale.setScalar(pulse);
haloMaterial.opacity = 0.68 + Math.sin(time * 3.4) * 0.13;
selectionHalo.rotation.z = time * 0.16;
camera.updateMatrixWorld();
bucketObjects.forEach((object) => {
worldPosition.copy(object.group.position);
worldPosition.y = 2.12;
positionOverlay(object.label, worldPosition);
});
if (selectedKey && selectedKey.visual && selectedKey.bucket === selectedBucket) {
selectedKey.visual.group.getWorldPosition(worldPosition);
positionOverlay(selectedKeyTag, worldPosition, 0.42);
}
renderer.render(scene, camera);
requestAnimationFrame(animate);
}
animate();
</script>
</body>
</html>
```