Prompt specification
Build or customize the Three.js 3D scene "Encoding-convention explorer" 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: Encoding-convention explorer
Category: Algorithms & Systems · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/encoding-convention-explorer-x1848
Scene Description Prompt:
Reinterpret one fixed bit pattern by changing bit order and signedness in a low-poly encoding exhibit. Three.js from cdnjs, standalone HTML.
Working Three.js Starter (encoding-convention-explorer-x1848):
```html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Reinterpret — Binary Encoding Exhibit</title>
<style>
:root {
color-scheme: dark;
--bg: #071018;
--panel: rgba(9, 18, 27, 0.76);
--panel-strong: rgba(10, 21, 31, 0.92);
--line: rgba(151, 210, 220, 0.16);
--line-strong: rgba(151, 210, 220, 0.3);
--text: #eef8f7;
--muted: #8da7ae;
--cyan: #54e0d0;
--cyan-soft: rgba(84, 224, 208, 0.14);
--gold: #e7bd68;
--coral: #ff667a;
--mono: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas,
"Liberation Mono", monospace;
--sans: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: var(--bg);
color: var(--text);
font-family: var(--sans);
}
body {
background:
radial-gradient(circle at 50% 58%, #102735 0%, #09151e 42%, #050b11 100%);
}
button,
input {
font: inherit;
}
button {
color: inherit;
}
#app {
position: relative;
width: 100%;
height: 100%;
isolation: isolate;
}
#scene {
position: fixed;
inset: 0;
display: block;
width: 100%;
height: 100%;
touch-action: none;
cursor: grab;
outline: none;
}
#scene:active {
cursor: grabbing;
}
.atmosphere {
position: fixed;
inset: 0;
z-index: 1;
pointer-events: none;
background:
linear-gradient(180deg, rgba(2, 7, 11, 0.16), transparent 25%),
radial-gradient(circle at 50% 48%, transparent 36%, rgba(2, 6, 10, 0.52) 100%);
}
.topbar {
position: fixed;
z-index: 3;
top: 30px;
left: 34px;
right: 34px;
display: flex;
align-items: flex-start;
justify-content: space-between;
gap: 28px;
pointer-events: none;
}
.identity {
width: min(440px, 42vw);
pointer-events: none;
}
.eyebrow {
display: flex;
align-items: center;
gap: 9px;
color: var(--cyan);
font-family: var(--mono);
font-size: 10px;
font-weight: 700;
letter-spacing: 0.16em;
text-transform: uppercase;
}
.eyebrow::before {
width: 6px;
height: 6px;
border-radius: 50%;
background: var(--cyan);
box-shadow: 0 0 14px var(--cyan);
content: "";
}
h1 {
margin: 14px 0 12px;
font-size: clamp(36px, 4vw, 61px);
font-weight: 520;
letter-spacing: -0.055em;
line-height: 0.98;
}
h1 span {
color: var(--muted);
font-weight: 350;
}
.identity p {
max-width: 390px;
margin: 0;
color: var(--muted);
font-size: 13px;
line-height: 1.65;
}
.panel {
border: 1px solid var(--line);
border-radius: 16px;
background: var(--panel);
box-shadow:
0 24px 70px rgba(0, 0, 0, 0.24),
inset 0 1px 0 rgba(255, 255, 255, 0.035);
backdrop-filter: blur(18px) saturate(125%);
-webkit-backdrop-filter: blur(18px) saturate(125%);
}
.source {
width: 340px;
padding: 18px 20px 17px;
pointer-events: auto;
}
.panel-heading {
display: flex;
align-items: center;
justify-content: space-between;
gap: 12px;
margin-bottom: 13px;
color: var(--muted);
font-family: var(--mono);
font-size: 9px;
font-weight: 700;
letter-spacing: 0.14em;
text-transform: uppercase;
}
.immutable {
padding: 4px 7px;
border: 1px solid rgba(84, 224, 208, 0.24);
border-radius: 999px;
color: var(--cyan);
background: var(--cyan-soft);
font-size: 8px;
}
.source-binary {
margin: 0 0 8px;
color: var(--text);
font-family: var(--mono);
font-size: 22px;
font-weight: 650;
letter-spacing: 0.08em;
}
.source-meta {
display: flex;
align-items: center;
justify-content: space-between;
color: var(--muted);
font-family: var(--mono);
font-size: 10px;
}
.source-meta strong {
color: var(--gold);
font-size: 12px;
letter-spacing: 0.06em;
}
.readout {
position: fixed;
z-index: 3;
top: 226px;
left: 34px;
width: 304px;
padding: 20px;
pointer-events: auto;
}
.readout-top {
display: flex;
align-items: center;
justify-content: space-between;
gap: 12px;
margin-bottom: 11px;
}
.status {
padding: 5px 8px;
border: 1px solid var(--line-strong);
border-radius: 999px;
color: var(--cyan);
font-family: var(--mono);
font-size: 8px;
font-weight: 700;
letter-spacing: 0.09em;
text-transform: uppercase;
}
.status.negative {
border-color: rgba(255, 102, 122, 0.32);
color: var(--coral);
background: rgba(255, 102, 122, 0.08);
}
.decimal-label {
margin-bottom: 3px;
color: var(--muted);
font-family: var(--mono);
font-size: 9px;
letter-spacing: 0.12em;
text-transform: uppercase;
}
.decimal {
min-height: 58px;
margin: 0;
font-family: var(--mono);
font-size: 44px;
font-weight: 560;
letter-spacing: -0.07em;
line-height: 1.05;
font-variant-numeric: tabular-nums;
}
.decimal.flash {
animation: value-flash 320ms ease-out;
}
@keyframes value-flash {
0% {
color: var(--cyan);
transform: translateY(3px);
opacity: 0.45;
}
100% {
color: var(--text);
transform: translateY(0);
opacity: 1;
}
}
.readout-grid {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 10px;
margin-top: 15px;
padding-top: 14px;
border-top: 1px solid var(--line);
}
.datum span {
display: block;
margin-bottom: 5px;
color: var(--muted);
font-family: var(--mono);
font-size: 8px;
letter-spacing: 0.1em;
text-transform: uppercase;
}
.datum strong {
display: block;
font-family: var(--mono);
font-size: 12px;
font-weight: 550;
letter-spacing: 0.025em;
}
.datum.wide {
grid-column: 1 / -1;
}
.readout-note {
margin: 13px 0 0;
color: var(--muted);
font-size: 11px;
line-height: 1.5;
}
.controls {
position: fixed;
z-index: 3;
bottom: 72px;
left: 34px;
width: 446px;
padding: 19px 20px 18px;
pointer-events: auto;
}
.control-groups {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 18px;
}
.control-label {
display: block;
margin-bottom: 9px;
color: var(--muted);
font-family: var(--mono);
font-size: 8px;
font-weight: 700;
letter-spacing: 0.12em;
text-transform: uppercase;
}
.segmented {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 5px;
padding: 4px;
border: 1px solid var(--line);
border-radius: 10px;
background: rgba(0, 0, 0, 0.15);
}
.segmented button {
min-height: 39px;
padding: 7px 8px;
border: 0;
border-radius: 7px;
background: transparent;
color: var(--muted);
cursor: pointer;
font-family: var(--mono);
font-size: 9px;
font-weight: 650;
letter-spacing: 0.02em;
transition:
color 160ms ease,
background 160ms ease,
box-shadow 160ms ease;
}
.segmented button:hover,
.segmented button:focus-visible {
color: var(--text);
}
.segmented button[aria-pressed="true"] {
color: #04110f;
background: var(--cyan);
box-shadow: 0 5px 18px rgba(84, 224, 208, 0.16);
}
.segmented button:focus-visible,
.compare button:focus-visible,
.reset:focus-visible {
outline: 2px solid var(--gold);
outline-offset: 2px;
}
.control-help {
display: flex;
align-items: flex-start;
gap: 8px;
margin: 14px 0 0;
padding-top: 13px;
border-top: 1px solid var(--line);
color: var(--muted);
font-size: 10px;
line-height: 1.5;
}
.control-help::before {
flex: 0 0 auto;
width: 6px;
height: 6px;
margin-top: 4px;
border-radius: 50%;
background: var(--gold);
content: "";
}
.legend {
display: flex;
flex-wrap: wrap;
gap: 13px;
margin-top: 12px;
color: var(--muted);
font-family: var(--mono);
font-size: 8px;
letter-spacing: 0.03em;
}
.legend span {
display: flex;
align-items: center;
gap: 6px;
}
.legend i {
width: 7px;
height: 7px;
border-radius: 2px;
background: var(--cyan);
box-shadow: 0 0 9px rgba(84, 224, 208, 0.4);
}
.legend i.negative {
background: var(--coral);
box-shadow: 0 0 9px rgba(255, 102, 122, 0.4);
}
.legend i.dark {
background: #31414c;
box-shadow: none;
}
.compare {
position: fixed;
z-index: 3;
right: 34px;
bottom: 72px;
width: 312px;
padding: 17px;
pointer-events: auto;
}
.compare-list {
display: grid;
gap: 6px;
}
.compare button {
display: grid;
grid-template-columns: 1fr auto;
align-items: center;
gap: 12px;
width: 100%;
padding: 10px 11px;
border: 1px solid transparent;
border-radius: 9px;
background: rgba(255, 255, 255, 0.025);
cursor: pointer;
text-align: left;
transition:
border-color 160ms ease,
background 160ms ease,
transform 160ms ease;
}
.compare button:hover {
transform: translateX(-2px);
background: rgba(255, 255, 255, 0.045);
}
.compare button[aria-pressed="true"] {
border-color: rgba(84, 224, 208, 0.32);
background: var(--cyan-soft);
}
.compare-name {
display: block;
margin-bottom: 3px;
color: var(--muted);
font-family: var(--mono);
font-size: 8px;
letter-spacing: 0.05em;
text-transform: uppercase;
}
.compare strong {
display: block;
font-family: var(--mono);
font-size: 13px;
font-weight: 560;
font-variant-numeric: tabular-nums;
}
.compare small {
color: var(--muted);
font-family: var(--mono);
font-size: 9px;
}
.compare button[aria-pressed="true"] .compare-name {
color: var(--cyan);
}
.footer {
position: fixed;
z-index: 3;
right: 34px;
bottom: 25px;
left: 34px;
display: flex;
align-items: center;
justify-content: space-between;
gap: 20px;
color: rgba(141, 167, 174, 0.75);
font-family: var(--mono);
font-size: 9px;
letter-spacing: 0.04em;
pointer-events: none;
}
.reset {
padding: 7px 10px;
border: 1px solid var(--line);
border-radius: 8px;
background: rgba(9, 18, 27, 0.68);
color: var(--muted);
cursor: pointer;
font-family: var(--mono);
font-size: 9px;
pointer-events: auto;
}
.reset:hover {
color: var(--text);
border-color: var(--line-strong);
}
.tooltip {
position: fixed;
z-index: 10;
min-width: 195px;
padding: 12px 13px;
border: 1px solid rgba(84, 224, 208, 0.25);
border-radius: 10px;
background: var(--panel-strong);
box-shadow: 0 18px 40px rgba(0, 0, 0, 0.35);
opacity: 0;
pointer-events: none;
transform: translateY(5px);
transition:
opacity 120ms ease,
transform 120ms ease;
}
.tooltip.visible {
opacity: 1;
transform: translateY(0);
}
.tooltip strong {
display: block;
margin-bottom: 7px;
color: var(--cyan);
font-family: var(--mono);
font-size: 12px;
}
.tooltip-row {
display: flex;
justify-content: space-between;
gap: 18px;
margin-top: 4px;
color: var(--muted);
font-family: var(--mono);
font-size: 9px;
}
.tooltip-row b {
color: var(--text);
font-weight: 550;
}
.tooltip-row b.negative {
color: var(--coral);
}
@media (max-width: 1120px) {
.compare {
display: none;
}
.identity {
width: min(390px, 48vw);
}
}
@media (max-width: 760px) {
.topbar {
top: 14px;
right: 14px;
left: 14px;
}
.identity {
width: 100%;
}
.identity h1 {
margin: 8px 0 0;
font-size: 30px;
}
.identity p {
display: none;
}
.source {
position: fixed;
top: 91px;
right: 14px;
left: 14px;
width: auto;
padding: 12px 14px;
}
.source .panel-heading {
margin-bottom: 7px;
}
.source-binary {
margin-bottom: 4px;
font-size: 18px;
}
.readout {
top: 190px;
right: 14px;
left: 14px;
width: auto;
padding: 13px 15px;
}
.readout-top {
margin-bottom: 4px;
}
.decimal {
min-height: auto;
font-size: 35px;
}
.readout-grid {
grid-template-columns: 1fr 1fr 1fr;
gap: 8px;
margin-top: 9px;
padding-top: 9px;
}
.datum.wide {
grid-column: auto;
}
.datum:nth-child(3) {
display: none;
}
.readout-note {
display: none;
}
.controls {
right: 14px;
bottom: 48px;
left: 14px;
width: auto;
padding: 14px;
}
.control-groups {
gap: 10px;
}
.segmented button {
min-height: 36px;
padding: 6px 4px;
font-size: 8px;
}
.control-help {
margin-top: 10px;
padding-top: 9px;
}
.legend {
gap: 9px;
margin-top: 9px;
font-size: 7px;
}
.footer {
right: 14px;
bottom: 12px;
left: 14px;
font-size: 8px;
}
.footer > span {
display: none;
}
.footer::before {
content: "DRAG TO ORBIT · SCROLL TO ZOOM";
color: rgba(141, 167, 174, 0.65);
}
}
@media (max-height: 690px) and (min-width: 761px) {
.readout {
top: 190px;
}
.decimal {
font-size: 37px;
}
.controls,
.compare {
bottom: 54px;
}
.control-help {
display: none;
}
}
@media (prefers-reduced-motion: reduce) {
*,
*::before,
*::after {
scroll-behavior: auto !important;
animation-duration: 0.01ms !important;
transition-duration: 0.01ms !important;
}
}
</style>
</head>
<body>
<div id="app">
<canvas
id="scene"
aria-label="Interactive low-poly visualization of a fixed sixteen-bit pattern"
></canvas>
<div class="atmosphere"></div>
<header class="topbar">
<div class="identity">
<div class="eyebrow">Encoding archive / study 01</div>
<h1>Same bits.<br /><span>Different meaning.</span></h1>
<p>
A fixed sixteen-bit pattern, reinterpreted through bit order and
signedness. The input never changes. Its meaning does.
</p>
</div>
<section class="source panel" aria-label="Fixed source bit pattern">
<div class="panel-heading">
<span>Fixed source pattern · 16 bit</span>
<span class="immutable">Immutable</span>
</div>
<p class="source-binary">10110100 10101101</p>
<div class="source-meta">
<span>HEX</span>
<strong>0xB4AD</strong>
</div>
</section>
</header>
<section class="readout panel" aria-live="polite">
<div class="readout-top">
<div class="eyebrow">Active interpretation</div>
<span id="status" class="status">Unsigned</span>
</div>
<div class="decimal-label">Decimal value</div>
<p id="decimal" class="decimal">46,253</p>
<div class="readout-grid">
<div class="datum">
<span>Encoded hex</span>
<strong id="hex">0xB4AD</strong>
</div>
<div class="datum">
<span>Binary</span>
<strong id="binary">1011 0100 1010 1101</strong>
</div>
<div class="datum wide">
<span>Representable range</span>
<strong id="range">0 … 65,535</strong>
</div>
</div>
<p id="readout-note" class="readout-note"></p>
</section>
<section class="controls panel" aria-label="Reinterpretation controls">
<div class="panel-heading">
<span>Reinterpret the source</span>
<span>Live</span>
</div>
<div class="control-groups">
<div>
<span class="control-label">Bit order</span>
<div class="segmented" role="group" aria-label="Bit order">
<button class="order-button" data-set-order="msb" aria-pressed="true">
MSB → LSB
</button>
<button class="order-button" data-set-order="lsb" aria-pressed="false">
LSB → MSB
</button>
</div>
</div>
<div>
<span class="control-label">Signedness</span>
<div class="segmented" role="group" aria-label="Signedness">
<button class="signed-button" data-set-signed="false" aria-pressed="true">
Unsigned
</button>
<button class="signed-button" data-set-signed="true" aria-pressed="false">
Signed 2's comp.
</button>
</div>
</div>
</div>
<p id="control-help" class="control-help"></p>
<div class="legend" aria-label="Visualization legend">
<span><i></i>Positive contribution</span>
<span><i class="negative"></i>Negative contribution</span>
<span><i class="dark"></i>Zero bit</span>
</div>
</section>
<section class="compare panel" aria-label="All four interpretations">
<div class="panel-heading">
<span>Four possible readings</span>
<span>2 × 2</span>
</div>
<div class="compare-list">
<button data-config="msb:false" aria-pressed="true">
<span>
<span class="compare-name">MSB · Unsigned</span>
<strong>46,253</strong>
</span>
<small>0xB4AD</small>
</button>
<button data-config="msb:true" aria-pressed="false">
<span>
<span class="compare-name">MSB · Signed</span>
<strong>−19,283</strong>
</span>
<small>0xB4AD</small>
</button>
<button data-config="lsb:false" aria-pressed="false">
<span>
<span class="compare-name">LSB · Unsigned</span>
<strong>46,381</strong>
</span>
<small>0xB52D</small>
</button>
<button data-config="lsb:true" aria-pressed="false">
<span>
<span class="compare-name">LSB · Signed</span>
<strong>−19,155</strong>
</span>
<small>0xB52D</small>
</button>
</div>
</section>
<footer class="footer">
<span>Fixed input: 0xB4AD · Hover a column to inspect its contribution</span>
<button id="reset" class="reset">Reset view</button>
</footer>
<div id="tooltip" class="tooltip" role="tooltip" aria-hidden="true"></div>
</div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
const SOURCE_BINARY = "1011010010101101";
const SOURCE_HEX = "0xB4AD";
const BIT_COUNT = 16;
const TWO_POW_16 = 1 << 16;
const TWO_POW_15 = 1 << 15;
const state = {
order: "msb",
signed: false
};
const reducedMotion = window.matchMedia(
"(prefers-reduced-motion: reduce)"
).matches;
const canvas = document.querySelector("#scene");
const tooltip = document.querySelector("#tooltip");
const decimalEl = document.querySelector("#decimal");
const hexEl = document.querySelector("#hex");
const binaryEl = document.querySelector("#binary");
const rangeEl = document.querySelector("#range");
const statusEl = document.querySelector("#status");
const readoutNoteEl = document.querySelector("#readout-note");
const controlHelpEl = document.querySelector("#control-help");
const ui = {
orderButtons: [...document.querySelectorAll(".order-button")],
signedButtons: [...document.querySelectorAll(".signed-button")],
compareButtons: [...document.querySelectorAll("[data-config]")]
};
let mobileLayout = window.innerWidth <= 760;
function formatNumber(value) {
const absolute = Math.abs(value).toLocaleString("en-US");
return value < 0 ? `−${absolute}` : absolute;
}
function groupBinary(binary) {
return binary.replace(/(.{4})/g, "$1 ").trim();
}
function interpret(order, signed) {
const binary =
order === "msb"
? SOURCE_BINARY
: [...SOURCE_BINARY].reverse().join("");
const unsigned = parseInt(binary, 2);
const signedValue =
unsigned >= TWO_POW_15 ? unsigned - TWO_POW_16 : unsigned;
return {
binary,
unsigned,
value: signed ? signedValue : unsigned,
hex: `0x${unsigned.toString(16).toUpperCase().padStart(4, "0")}`
};
}
// -------------------------------------------------------------------------
// Renderer, camera, and scene
// -------------------------------------------------------------------------
const renderer = new THREE.WebGLRenderer({
canvas,
antialias: true,
alpha: true,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.setClearColor(0x071018, 0);
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.12;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
const scene = new THREE.Scene();
scene.fog = new THREE.FogExp2(0x071018, 0.031);
const camera = new THREE.PerspectiveCamera(
42,
window.innerWidth / window.innerHeight,
0.1,
100
);
const cameraTarget = new THREE.Vector3(0, 1.45, -0.1);
const cameraState = {
theta: -0.035,
phi: 1.22,
radius: 11.8
};
const hemisphere = new THREE.HemisphereLight(
0xa5e5ed,
0x071018,
1.55
);
scene.add(hemisphere);
const keyLight = new THREE.DirectionalLight(0xffffff, 2.5);
keyLight.position.set(5, 9, 7);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(2048, 2048);
keyLight.shadow.camera.left = -10;
keyLight.shadow.camera.right = 10;
keyLight.shadow.camera.top = 9;
keyLight.shadow.camera.bottom = -6;
keyLight.shadow.camera.near = 0.5;
keyLight.shadow.camera.far = 30;
keyLight.shadow.bias = -0.0004;
scene.add(keyLight);
const cyanLight = new THREE.PointLight(0x38dfcf, 22, 18, 2);
cyanLight.position.set(-4.5, 4.2, 3.5);
scene.add(cyanLight);
const warmLight = new THREE.PointLight(0xe7bd68, 13, 15, 2);
warmLight.position.set(4.5, 3.5, -2.5);
scene.add(warmLight);
// Faceted floor.
let floorGeometry = new THREE.PlaneGeometry(36, 25, 16, 12).toNonIndexed();
floorGeometry.rotateX(-Math.PI / 2);
const floorColors = [];
const floorPalette = [0x071018, 0x0a151d, 0x0b1921, 0x0d1d25];
for (let i = 0; i < floorGeometry.attributes.position.count; i += 3) {
const color = new THREE.Color(
floorPalette[Math.floor(Math.random() * floorPalette.length)]
);
for (let vertex = 0; vertex < 3; vertex++) {
floorColors.push(color.r, color.g, color.b);
}
}
floorGeometry.setAttribute(
"color",
new THREE.Float32BufferAttribute(floorColors, 3)
);
const floor = new THREE.Mesh(
floorGeometry,
new THREE.MeshStandardMaterial({
vertexColors: true,
roughness: 0.95,
metalness: 0.05,
flatShading: true
})
);
floor.position.y = -0.035;
floor.receiveShadow = true;
scene.add(floor);
const grid = new THREE.GridHelper(32, 64, 0x31515b, 0x19323b);
grid.position.y = 0.008;
grid.material.transparent = true;
grid.material.opacity = 0.16;
scene.add(grid);
// Low-poly plinth.
const plinth = new THREE.Mesh(
new THREE.CylinderGeometry(6.25, 6.55, 0.3, 12),
new THREE.MeshStandardMaterial({
color: 0x10212a,
roughness: 0.72,
metalness: 0.24,
flatShading: true
})
);
plinth.position.set(0, 0.15, 0.15);
plinth.scale.set(1, 1, 0.26);
plinth.receiveShadow = true;
plinth.castShadow = true;
scene.add(plinth);
const plinthTop = new THREE.Mesh(
new THREE.CylinderGeometry(6.12, 6.18, 0.035, 12),
new THREE.MeshStandardMaterial({
color: 0x17303a,
roughness: 0.62,
metalness: 0.32,
flatShading: true
})
);
plinthTop.position.set(0, 0.315, 0.15);
plinthTop.scale.set(1, 1, 0.26);
plinthTop.receiveShadow = true;
scene.add(plinthTop);
// Central low-poly "value core".
const coreGroup = new THREE.Group();
coreGroup.position.set(0, 1.78, -1.65);
scene.add(coreGroup);
const coreGeometry = new THREE.IcosahedronGeometry(0.78, 0);
const coreMaterial = new THREE.MeshStandardMaterial({
color: 0x54e0d0,
emissive: 0x0a8178,
emissiveIntensity: 0.42,
roughness: 0.34,
metalness: 0.36,
flatShading: true
});
const core = new THREE.Mesh(coreGeometry, coreMaterial);
core.castShadow = true;
coreGroup.add(core);
const coreEdges = new THREE.LineSegments(
new THREE.EdgesGeometry(coreGeometry),
new THREE.LineBasicMaterial({
color: 0xb8fff7,
transparent: true,
opacity: 0.38
})
);
coreGroup.add(coreEdges);
const coreRing = new THREE.Mesh(
new THREE.TorusGeometry(1.12, 0.018, 3, 36),
new THREE.MeshBasicMaterial({
color: 0xe7bd68,
transparent: true,
opacity: 0.62
})
);
coreRing.rotation.set(1.08, 0.25, 0.38);
coreGroup.add(coreRing);
const coreRingTwo = new THREE.Mesh(
new THREE.TorusGeometry(1.36, 0.012, 3, 32),
new THREE.MeshBasicMaterial({
color: 0x54e0d0,
transparent: true,
opacity: 0.3
})
);
coreRingTwo.rotation.set(-0.42, 0.7, 0.15);
coreGroup.add(coreRingTwo);
const orbiters = new THREE.Group();
coreGroup.add(orbiters);
for (let i = 0; i < 5; i++) {
const orbiter = new THREE.Mesh(
new THREE.TetrahedronGeometry(0.085 + i * 0.006),
new THREE.MeshStandardMaterial({
color: i % 2 ? 0xe7bd68 : 0x7de8dc,
emissive: i % 2 ? 0x5b4219 : 0x174f4b,
emissiveIntensity: 0.35,
roughness: 0.5,
flatShading: true
})
);
const angle = (i / 5) * Math.PI * 2;
orbiter.position.set(
Math.cos(angle) * 1.55,
Math.sin(angle) * 1.1,
Math.sin(angle * 2) * 0.45
);
orbiters.add(orbiter);
}
// Sparse background particles.
const particlePositions = [];
for (let i = 0; i < 150; i++) {
particlePositions.push(
(Math.random() - 0.5) * 25,
0.6 + Math.random() * 8,
-2 - Math.random() * 10
);
}
const particleGeometry = new THREE.BufferGeometry();
particleGeometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(particlePositions, 3)
);
const particles = new THREE.Points(
particleGeometry,
new THREE.PointsMaterial({
color: 0x86c5c9,
size: 0.025,
transparent: true,
opacity: 0.34,
depthWrite: false
})
);
scene.add(particles);
// -------------------------------------------------------------------------
// Sixteen bit columns
// -------------------------------------------------------------------------
const bars = [];
const barMeshes = [];
const bodyGeometry = new THREE.CylinderGeometry(0.125, 0.17, 1, 6);
const capGeometry = new THREE.OctahedronGeometry(0.17, 0);
const ringGeometry = new THREE.TorusGeometry(0.205, 0.018, 3, 12);
function roundedRect(ctx, x, y, width, height, radius) {
const r = Math.min(radius, width / 2, height / 2);
ctx.beginPath();
ctx.moveTo(x + r, y);
ctx.arcTo(x + width, y, x + width, y + height, r);
ctx.arcTo(x + width, y + height, x, y + height, r);
ctx.arcTo(x, y + height, x, y, r);
ctx.arcTo(x, y, x + width, y, r);
ctx.closePath();
}
function drawLabel(bar, activePosition) {
const ctx = bar.labelContext;
const width = bar.labelCanvas.width;
const height = bar.labelCanvas.height;
const negative = state.signed && activePosition === 15;
const accent = negative ? "#ff667a" : "#54e0d0";
ctx.clearRect(0, 0, width, height);
roundedRect(ctx, 4, 4, width - 8, height - 8, 14);
ctx.fillStyle = "rgba(5, 13, 20, 0.88)";
ctx.fill();
ctx.strokeStyle = negative
? "rgba(255, 102, 122, 0.35)"
: "rgba(84, 224, 208, 0.25)";
ctx.lineWidth = 2;
ctx.stroke();
ctx.fillStyle = accent;
ctx.fillRect(15, 20, 4, 52);
ctx.textBaseline = "middle";
ctx.font = "600 30px ui-monospace, monospace";
ctx.fillStyle = "#eef8f7";
ctx.fillText(`b${activePosition}`, 31, 45);
ctx.font = "700 28px ui-monospace, monospace";
ctx.textAlign = "right";
ctx.fillStyle = bar.bit === "1" ? "#e7bd68" : "#607681";
ctx.fillText(bar.bit, width - 20, 45);
bar.labelTexture.needsUpdate = true;
}
for (let sourcePosition = 0; sourcePosition < BIT_COUNT; sourcePosition++) {
const bit = SOURCE_BINARY[BIT_COUNT - 1 - sourcePosition];
const bodyMaterial = new THREE.MeshStandardMaterial({
color: 0x17333c,
emissive: 0x0b514c,
emissiveIntensity: 0.16,
roughness: 0.55,
metalness: 0.28,
flatShading: true
});
const body = new THREE.Mesh(bodyGeometry, bodyMaterial);
body.castShadow = true;
body.receiveShadow = true;
body.userData.sourcePosition = sourcePosition;
const capMaterial = new THREE.MeshStandardMaterial({
color: bit === "1" ? 0xe4fff8 : 0x33414c,
emissive: bit === "1" ? 0x5ac8ba : 0x071017,
emissiveIntensity: bit === "1" ? 0.58 : 0.08,
roughness: 0.35,
metalness: 0.42,
flatShading: true
});
const cap = new THREE.Mesh(capGeometry, capMaterial);
cap.castShadow = true;
const ringMaterial = new THREE.MeshBasicMaterial({
color: 0x54e0d0,
transparent: true,
opacity: bit === "1" ? 0.55 : 0.15
});
const ring = new THREE.Mesh(ringGeometry, ringMaterial);
ring.rotation.x = Math.PI / 2;
const labelCanvas = document.createElement("canvas");
labelCanvas.width = 320;
labelCanvas.height = 96;
const labelContext = labelCanvas.getContext("2d");
const labelTexture = new THREE.CanvasTexture(labelCanvas);
labelTexture.colorSpace = THREE.SRGBColorSpace;
labelTexture.minFilter = THREE.LinearFilter;
const label = new THREE.Sprite(
new THREE.SpriteMaterial({
map: labelTexture,
transparent: true,
depthTest: false,
depthWrite: false
})
);
label.scale.set(0.8, 0.24, 1);
label.renderOrder = 10;
scene.add(body, cap, ring, label);
const bar = {
sourcePosition,
bit,
body,
cap,
ring,
label,
labelCanvas,
labelContext,
labelTexture,
activePosition: sourcePosition,
currentX: 0,
targetX: 0,
currentZ: 0,
targetZ: 0,
currentHeight: 0.7,
targetHeight: 0.7,
targetBody: new THREE.Color(),
targetEmissive: new THREE.Color(),
targetCap: new THREE.Color(),
targetRing: new THREE.Color(),
hovered: false
};
bars.push(bar);
barMeshes.push(body);
}
function layoutForPosition(position) {
if (mobileLayout) {
const column = position % 8;
const row = Math.floor(position / 8);
return {
x: (column - 3.5) * 0.59,
z: row === 0 ? -0.43 : 0.57
};
}
return {
x: (position - 7.5) * 0.64,
z: 0.15
};
}
function updateBarTargets(snap = false) {
bars.forEach((bar) => {
const activePosition =
state.order === "msb"
? bar.sourcePosition
: BIT_COUNT - 1 - bar.sourcePosition;
const layout = layoutForPosition(activePosition);
const placeWeight = 2 ** activePosition;
const signedWeight =
state.signed && activePosition === 15
? -placeWeight
: placeWeight;
const negative = signedWeight < 0;
const bitOn = bar.bit === "1";
bar.activePosition = activePosition;
bar.targetX = layout.x;
bar.targetZ = layout.z;
bar.targetHeight =
0.68 + Math.log2(Math.abs(placeWeight) + 1) * 0.19;
if (negative) {
bar.targetBody.set(bitOn ? 0x4a202b : 0x261a22);
bar.targetEmissive.set(bitOn ? 0xff5368 : 0x3c1721);
bar.targetCap.set(bitOn ? 0xffa8b3 : 0x3b2930);
bar.targetRing.set(0xff667a);
} else {
bar.targetBody.set(bitOn ? 0x123e46 : 0x17262f);
bar.targetEmissive.set(bitOn ? 0x13a69d : 0x0b252d);
bar.targetCap.set(bitOn ? 0xe4fff8 : 0x33414c);
bar.targetRing.set(0x54e0d0);
}
drawLabel(bar, activePosition);
if (snap) {
bar.currentX = bar.targetX;
bar.currentZ = bar.targetZ;
bar.currentHeight = bar.targetHeight;
}
});
}
function updatePlinthLayout() {
plinth.scale.x = mobileLayout ? 0.57 : 1;
plinth.scale.z = mobileLayout ? 0.4 : 0.26;
plinthTop.scale.x = mobileLayout ? 0.57 : 1;
plinthTop.scale.z = mobileLayout ? 0.4 : 0.26;
coreGroup.position.z = mobileLayout ? -1.5 : -1.65;
}
// -------------------------------------------------------------------------
// UI
// -------------------------------------------------------------------------
function renderUI() {
const result = interpret(state.order, state.signed);
decimalEl.textContent = formatNumber(result.value);
hexEl.textContent = result.hex;
binaryEl.textContent = groupBinary(result.binary);
rangeEl.textContent = state.signed
? "−32,768 … 32,767"
: "0 … 65,535";
statusEl.textContent = state.signed ? "Signed 2's comp." : "Unsigned";
statusEl.classList.toggle("negative", state.signed && result.value < 0);
readoutNoteEl.textContent = state.signed
? "The sign bit b15 carries −32,768. The remaining one bits contribute positive place values."
: "Every one bit contributes its positive binary place value. The leading bit carries +32,768.";
controlHelpEl.textContent =
state.order === "msb"
? "The leftmost source bit is the most significant bit and carries 2¹⁵."
: "The source sequence is reversed before place values are assigned.";
ui.orderButtons.forEach((button) => {
button.setAttribute(
"aria-pressed",
String(button.dataset.setOrder === state.order)
);
});
ui.signedButtons.forEach((button) => {
button.setAttribute(
"aria-pressed",
String(
(button.dataset.setSigned === "true") === state.signed
)
);
});
ui.compareButtons.forEach((button) => {
const [order, signed] = button.dataset.config.split(":");
const comparison = interpret(order, signed === "true");
button.setAttribute(
"aria-pressed",
String(
order === state.order &&
(signed === "true") === state.signed
)
);
button.querySelector("strong").textContent = formatNumber(
comparison.value
);
button.querySelector("small").textContent = comparison.hex;
});
decimalEl.classList.remove("flash");
void decimalEl.offsetWidth;
decimalEl.classList.add("flash");
}
function setConfiguration(order, signed) {
state.order = order;
state.signed = signed;
updateBarTargets(false);
renderUI();
}
ui.orderButtons.forEach((button) => {
button.addEventListener("click", () => {
setConfiguration(button.dataset.setOrder, state.signed);
});
});
ui.signedButtons.forEach((button) => {
button.addEventListener("click", () => {
setConfiguration(
state.order,
button.dataset.setSigned === "true"
);
});
});
ui.compareButtons.forEach((button) => {
button.addEventListener("click", () => {
const [order, signed] = button.dataset.config.split(":");
setConfiguration(order, signed === "true");
});
});
// -------------------------------------------------------------------------
// Direct camera controls: orbit, zoom, and pinch
// -------------------------------------------------------------------------
const pointers = new Map();
let dragging = false;
let lastPointer = null;
let pinchDistance = 0;
function updateCamera() {
const sinPhi = Math.sin(cameraState.phi);
const cosPhi = Math.cos(cameraState.phi);
camera.position.set(
cameraTarget.x +
cameraState.radius * sinPhi * Math.sin(cameraState.theta),
cameraTarget.y + cameraState.radius * cosPhi,
cameraTarget.z +
cameraState.radius * sinPhi * Math.cos(cameraState.theta)
);
camera.lookAt(cameraTarget);
}
function resetCamera() {
cameraState.theta = -0.035;
cameraState.phi = 1.22;
cameraState.radius = mobileLayout ? 13.8 : 11.8;
cameraTarget.set(0, mobileLayout ? 1.25 : 1.45, -0.1);
updateCamera();
}
function pointerDistance() {
const values = [...pointers.values()];
if (values.length < 2) return 0;
const dx = values[0].x - values[1].x;
const dy = values[0].y - values[1].y;
return Math.hypot(dx, dy);
}
canvas.addEventListener("pointerdown", (event) => {
canvas.setPointerCapture(event.pointerId);
pointers.set(event.pointerId, { x: event.clientX, y: event.clientY });
if (pointers.size === 1) {
dragging = true;
lastPointer = { x: event.clientX, y: event.clientY };
} else if (pointers.size === 2) {
dragging = false;
pinchDistance = pointerDistance();
}
hideTooltip();
});
canvas.addEventListener("pointermove", (event) => {
if (pointers.has(event.pointerId)) {
pointers.set(event.pointerId, { x: event.clientX, y: event.clientY });
}
if (pointers.size >= 2) {
const distance = pointerDistance();
if (pinchDistance > 0 && distance > 0) {
cameraState.radius *= pinchDistance / distance;
cameraState.radius = THREE.MathUtils.clamp(
cameraState.radius,
mobileLayout ? 8 : 7,
mobileLayout ? 27 : 22
);
}
pinchDistance = distance;
hideTooltip();
return;
}
if (dragging && lastPointer) {
const dx = event.clientX - lastPointer.x;
const dy = event.clientY - lastPointer.y;
cameraState.theta -= dx * 0.0055;
cameraState.phi = THREE.MathUtils.clamp(
cameraState.phi - dy * 0.0045,
0.72,
1.48
);
lastPointer = { x: event.clientX, y: event.clientY };
hideTooltip();
return;
}
if (event.pointerType === "mouse") {
inspectPointer(event);
}
});
function endPointer(event) {
pointers.delete(event.pointerId);
if (pointers.size === 0) {
dragging = false;
lastPointer = null;
pinchDistance = 0;
}
}
canvas.addEventListener("pointerup", endPointer);
canvas.addEventListener("pointercancel", endPointer);
canvas.addEventListener("pointerleave", () => {
if (!dragging) hideTooltip();
});
canvas.addEventListener(
"wheel",
(event) => {
event.preventDefault();
cameraState.radius *= Math.exp(event.deltaY * 0.001);
cameraState.radius = THREE.MathUtils.clamp(
cameraState.radius,
mobileLayout ? 8 : 7,
mobileLayout ? 27 : 22
);
hideTooltip();
},
{ passive: false }
);
canvas.addEventListener("dblclick", resetCamera);
canvas.addEventListener("contextmenu", (event) => event.preventDefault());
document.querySelector("#reset").addEventListener("click", resetCamera);
// -------------------------------------------------------------------------
// Raycast inspection
// -------------------------------------------------------------------------
const raycaster = new THREE.Raycaster();
const pointerNdc = new THREE.Vector2();
function inspectPointer(event) {
const rect = canvas.getBoundingClientRect();
pointerNdc.x =
((event.clientX - rect.left) / rect.width) * 2 - 1;
pointerNdc.y =
-((event.clientY - rect.top) / rect.height) * 2 + 1;
raycaster.setFromCamera(pointerNdc, camera);
const hits = raycaster.intersectObjects(barMeshes, false);
const hit = hits[0];
bars.forEach((bar) => {
bar.hovered = false;
});
if (!hit) {
hideTooltip();
return;
}
const bar = bars.find(
(candidate) =>
candidate.body === hit.object
);
if (!bar) {
hideTooltip();
return;
}
bar.hovered = true;
const placeWeight = 2 ** bar.activePosition;
const negative = state.signed && bar.activePosition === 15;
const contribution =
bar.bit === "1"
? negative
? -placeWeight
: placeWeight
: 0;
tooltip.innerHTML = `
<strong>Active bit b${bar.activePosition}</strong>
<div class="tooltip-row">
<span>Bit value</span><b>${bar.bit}</b>
</div>
<div class="tooltip-row">
<span>Place weight</span><b>${formatNumber(placeWeight)}</b>
</div>
<div class="tooltip-row">
<span>Contribution</span>
<b class="${contribution < 0 ? "negative" : ""}">
${formatNumber(contribution)}
</b>
</div>
`;
const tooltipWidth = 210;
const tooltipHeight = 126;
const left = Math.min(
event.clientX + 17,
window.innerWidth - tooltipWidth - 10
);
const top = Math.min(
event.clientY + 16,
window.innerHeight - tooltipHeight - 10
);
tooltip.style.left = `${Math.max(8, left)}px`;
tooltip.style.top = `${Math.max(8, top)}px`;
tooltip.classList.add("visible");
tooltip.setAttribute("aria-hidden", "false");
}
function hideTooltip() {
bars.forEach((bar) => {
bar.hovered = false;
});
tooltip.classList.remove("visible");
tooltip.setAttribute("aria-hidden", "true");
}
// -------------------------------------------------------------------------
// Resize and animation
// -------------------------------------------------------------------------
function onResize() {
const width = window.innerWidth;
const height = window.innerHeight;
const wasMobile = mobileLayout;
mobileLayout = width <= 760;
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(width, height);
camera.aspect = width / height;
camera.fov = mobileLayout ? 48 : 42;
camera.updateProjectionMatrix();
updatePlinthLayout();
updateBarTargets(false);
if (wasMobile !== mobileLayout) {
resetCamera();
} else {
updateCamera();
}
}
window.addEventListener("resize", onResize);
const clock = new THREE.Clock();
const tempColor = new THREE.Color();
function animate() {
const dt = Math.min(clock.getDelta(), 0.05);
const smoothing = reducedMotion
? 1
: 1 - Math.exp(-dt * 7.5);
bars.forEach((bar) => {
bar.currentX = THREE.MathUtils.lerp(
bar.currentX,
bar.targetX,
smoothing
);
bar.currentZ = THREE.MathUtils.lerp(
bar.currentZ,
bar.targetZ,
smoothing
);
bar.currentHeight = THREE.MathUtils.lerp(
bar.currentHeight,
bar.targetHeight,
smoothing
);
bar.body.position.set(
bar.currentX,
0.35 + bar.currentHeight / 2,
bar.currentZ
);
bar.body.scale.y = bar.currentHeight;
bar.cap.position.set(
bar.currentX,
0.35 + bar.currentHeight + 0.15,
bar.currentZ
);
bar.cap.rotation.y += dt * 0.55;
bar.cap.rotation.x += dt * 0.22;
bar.ring.position.set(
bar.currentX,
0.35 + bar.currentHeight + 0.015,
bar.currentZ
);
bar.label.position.set(
bar.currentX,
0.35 + bar.currentHeight + 0.53,
bar.currentZ
);
const labelScale = bar.hovered ? 1.08 : 1;
bar.label.scale.set(
0.8 * labelScale,
0.24 * labelScale,
1
);
tempColor.copy(bar.targetBody);
bar.body.material.color.lerp(tempColor, smoothing);
tempColor.copy(bar.targetEmissive);
bar.body.material.emissive.lerp(tempColor, smoothing);
bar.body.material.emissiveIntensity = bar.hovered
? 0.72
: bar.bit === "1"
? 0.28
: 0.07;
tempColor.copy(bar.targetCap);
bar.cap.material.color.lerp(tempColor, smoothing);
bar.cap.material.emissiveIntensity = bar.hovered
? 0.9
: bar.bit === "1"
? 0.58
: 0.08;
tempColor.copy(bar.targetRing);
bar.ring.material.color.lerp(tempColor, smoothing);
bar.ring.material.opacity = bar.hovered
? 0.9
: bar.bit === "1"
? 0.55
: 0.15;
});
const interpretation = interpret(state.order, state.signed);
const coreColor =
state.signed && interpretation.value < 0
? new THREE.Color(0xff667a)
: new THREE.Color(0x54e0d0);
coreMaterial.color.lerp(coreColor, smoothing);
coreMaterial.emissive.lerp(coreColor, smoothing * 0.6);
core.rotation.y += dt * (state.order === "msb" ? 0.23 : -0.23);
core.rotation.x = Math.sin(clock.elapsedTime * 0.38) * 0.08;
coreRing.rotation.z += dt * 0.12;
coreRingTwo.rotation.y -= dt * 0.09;
orbiters.rotation.y += dt * 0.2;
orbiters.rotation.z = Math.sin(clock.elapsedTime * 0.22) * 0.16;
coreRing.material.color.lerp(
new THREE.Color(state.order === "lsb" ? 0xe7bd68 : 0x54e0d0),
smoothing
);
const corePulse = 1 + Math.sin(clock.elapsedTime * 1.5) * 0.025;
core.scale.setScalar(corePulse);
updateCamera();
renderer.render(scene, camera);
requestAnimationFrame(animate);
}
updatePlinthLayout();
updateBarTargets(true);
renderUI();
resetCamera();
animate();
</script>
</body>
</html>
```