Prompt specification
Build or customize the Three.js 3D scene "Place-value rods with identical beads" 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: Place-value rods with identical beads
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/place-value-rods-with-identical-beads-x2481
Scene Description Prompt:
Please use Three.js from cdnjs for a voxel scene based on this idea: Place-value rods with identical beads. Build a small counting frame with three labeled rods and identical beads. Use an explicit base-ten teaching convention so bead position determines represented value. Return one self-contained HTML file.
Working Three.js Starter (place-value-rods-with-identical-beads-x2481):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Place-Value Rods — Base Ten</title>
<style>
:root {
--ink: #17332f;
--muted: #667b76;
--paper: #f4f0e7;
--card: rgba(255, 255, 255, 0.84);
--line: rgba(23, 51, 47, 0.12);
--teal: #147f78;
--teal-dark: #0d5f5a;
--teal-soft: #dcefeb;
--amber: #e49a3a;
--shadow: 0 24px 70px rgba(31, 55, 48, 0.13);
}
* {
box-sizing: border-box;
}
html,
body {
margin: 0;
min-height: 100%;
}
body {
color: var(--ink);
background:
radial-gradient(circle at 12% 8%, rgba(255, 255, 255, 0.95), transparent 27rem),
radial-gradient(circle at 88% 20%, rgba(210, 232, 224, 0.76), transparent 30rem),
var(--paper);
font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}
button {
font: inherit;
}
.page {
width: min(1240px, calc(100% - 32px));
margin: 0 auto;
padding: 30px 0 24px;
}
.site-header {
display: flex;
align-items: flex-end;
justify-content: space-between;
gap: 28px;
margin-bottom: 22px;
}
.brand-row {
display: flex;
align-items: center;
gap: 17px;
}
.brand-mark {
position: relative;
width: 58px;
height: 58px;
flex: 0 0 auto;
border-radius: 18px;
background: var(--teal);
box-shadow: 0 11px 24px rgba(20, 127, 120, 0.23);
}
.brand-mark::before,
.brand-mark::after {
content: "";
position: absolute;
left: 16px;
width: 26px;
height: 9px;
border-radius: 999px;
background: #f6d69f;
border: 2px solid rgba(255, 255, 255, 0.45);
}
.brand-mark::before {
top: 15px;
}
.brand-mark::after {
top: 31px;
}
.eyebrow {
display: block;
margin-bottom: 3px;
color: var(--teal-dark);
font-size: 0.72rem;
font-weight: 800;
letter-spacing: 0.16em;
text-transform: uppercase;
}
h1 {
margin: 0;
font-size: clamp(2rem, 4vw, 3.25rem);
line-height: 1;
letter-spacing: -0.055em;
}
.subtitle {
max-width: 670px;
margin: 9px 0 0;
color: var(--muted);
font-size: 1rem;
line-height: 1.55;
}
.header-tag {
flex: 0 0 auto;
padding: 9px 13px;
border: 1px solid var(--line);
border-radius: 999px;
color: var(--muted);
background: rgba(255, 255, 255, 0.46);
font-size: 0.78rem;
font-weight: 750;
letter-spacing: 0.05em;
text-transform: uppercase;
}
.layout {
display: grid;
grid-template-columns: minmax(0, 1fr) 340px;
gap: 20px;
align-items: start;
}
.scene-card {
position: relative;
overflow: hidden;
min-width: 0;
border: 1px solid rgba(23, 51, 47, 0.11);
border-radius: 28px;
background: #e8eeea;
box-shadow: var(--shadow);
}
#sceneWrap {
position: relative;
height: clamp(540px, 73vh, 720px);
min-height: 540px;
}
#scene {
display: block;
width: 100%;
height: 100%;
outline: none;
touch-action: none;
}
.scene-badge {
position: absolute;
top: 18px;
left: 18px;
max-width: 245px;
padding: 12px 14px;
border: 1px solid rgba(23, 51, 47, 0.11);
border-radius: 16px;
background: rgba(255, 255, 255, 0.82);
box-shadow: 0 9px 28px rgba(35, 65, 56, 0.09);
backdrop-filter: blur(10px);
pointer-events: none;
}
.scene-badge strong {
display: block;
margin-bottom: 3px;
color: var(--teal-dark);
font-size: 0.74rem;
letter-spacing: 0.12em;
text-transform: uppercase;
}
.scene-badge span {
display: block;
color: var(--muted);
font-size: 0.8rem;
line-height: 1.4;
}
.scene-actions {
position: absolute;
top: 18px;
right: 18px;
}
.soft-button {
border: 1px solid rgba(23, 51, 47, 0.12);
border-radius: 12px;
padding: 9px 12px;
color: var(--ink);
background: rgba(255, 255, 255, 0.82);
box-shadow: 0 7px 20px rgba(35, 65, 56, 0.08);
cursor: pointer;
backdrop-filter: blur(10px);
transition: transform 160ms ease, background 160ms ease;
}
.soft-button:hover {
transform: translateY(-1px);
background: #fff;
}
.scene-hint {
position: absolute;
left: 50%;
bottom: 16px;
width: max-content;
max-width: calc(100% - 32px);
transform: translateX(-50%);
padding: 9px 13px;
border: 1px solid rgba(23, 51, 47, 0.1);
border-radius: 999px;
color: var(--muted);
background: rgba(255, 255, 255, 0.8);
box-shadow: 0 8px 26px rgba(35, 65, 56, 0.08);
font-size: 0.76rem;
text-align: center;
pointer-events: none;
backdrop-filter: blur(10px);
}
.panel {
display: grid;
gap: 14px;
}
.panel-card {
padding: 20px;
border: 1px solid var(--line);
border-radius: 22px;
background: var(--card);
box-shadow: 0 14px 38px rgba(35, 65, 56, 0.07);
backdrop-filter: blur(12px);
}
.readout {
color: #f7fbf8;
border-color: transparent;
background:
radial-gradient(circle at 100% 0%, rgba(70, 180, 165, 0.32), transparent 55%),
linear-gradient(145deg, #174d48, #0e302d);
box-shadow: 0 18px 44px rgba(15, 54, 49, 0.21);
}
.readout-label {
color: #a7d4ca;
font-size: 0.72rem;
font-weight: 800;
letter-spacing: 0.14em;
text-transform: uppercase;
}
.total {
display: block;
margin: 4px 0 2px;
font-size: clamp(3.25rem, 7vw, 4.5rem);
line-height: 1;
letter-spacing: -0.055em;
font-variant-numeric: tabular-nums;
}
.readout-note {
color: #b9d4ce;
font-size: 0.82rem;
}
.equation {
display: flex;
flex-wrap: wrap;
align-items: center;
gap: 7px;
margin-top: 17px;
padding-top: 15px;
border-top: 1px solid rgba(255, 255, 255, 0.14);
color: #d7ebe6;
font-size: 0.86rem;
font-variant-numeric: tabular-nums;
}
.equation b {
color: #79b8ac;
font-weight: 500;
}
.card-title {
margin: 0 0 12px;
font-size: 1rem;
letter-spacing: -0.01em;
}
.convention {
color: var(--muted);
font-size: 0.86rem;
line-height: 1.55;
}
.convention strong {
color: var(--ink);
}
.rule {
display: grid;
grid-template-columns: 29px 1fr;
gap: 10px;
align-items: start;
margin-top: 12px;
}
.rule-number {
display: grid;
width: 29px;
height: 29px;
place-items: center;
border-radius: 9px;
color: var(--teal-dark);
background: var(--teal-soft);
font-size: 0.76rem;
font-weight: 850;
}
.rule-text {
padding-top: 4px;
color: var(--muted);
font-size: 0.82rem;
line-height: 1.45;
}
.rule-text strong {
color: var(--ink);
}
.places {
display: grid;
gap: 9px;
}
.place-row {
display: grid;
grid-template-columns: 34px minmax(0, 1fr) 34px;
gap: 10px;
align-items: center;
padding: 10px;
border: 1px solid var(--line);
border-radius: 15px;
background: rgba(255, 255, 255, 0.55);
}
.step-button {
width: 34px;
height: 34px;
padding: 0;
border: 0;
border-radius: 10px;
color: var(--teal-dark);
background: var(--teal-soft);
cursor: pointer;
font-size: 1.18rem;
font-weight: 800;
line-height: 1;
transition: transform 120ms ease, opacity 120ms ease;
}
.step-button:hover:not(:disabled) {
transform: translateY(-1px);
}
.step-button:disabled {
cursor: default;
opacity: 0.35;
}
.place-main {
min-width: 0;
}
.place-name {
display: block;
color: var(--muted);
font-size: 0.64rem;
font-weight: 850;
letter-spacing: 0.12em;
text-transform: uppercase;
}
.place-value {
display: flex;
align-items: baseline;
gap: 8px;
margin-top: 2px;
}
.place-digit {
font-size: 1.45rem;
line-height: 1;
font-variant-numeric: tabular-nums;
}
.place-contribution {
color: var(--muted);
font-size: 0.76rem;
font-variant-numeric: tabular-nums;
}
.actions {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 9px;
}
.action-button {
min-height: 42px;
border: 1px solid var(--line);
border-radius: 13px;
color: var(--ink);
background: #fff;
cursor: pointer;
font-size: 0.82rem;
font-weight: 750;
transition: transform 140ms ease, border-color 140ms ease;
}
.action-button:hover {
transform: translateY(-1px);
border-color: rgba(20, 127, 120, 0.35);
}
.action-button.primary {
color: #fff;
border-color: var(--teal);
background: var(--teal);
}
.action-button.wide {
grid-column: 1 / -1;
}
.microcopy {
margin: 9px 0 0;
color: var(--muted);
font-size: 0.72rem;
line-height: 1.45;
text-align: center;
}
footer {
margin-top: 18px;
color: var(--muted);
font-size: 0.76rem;
text-align: center;
}
@media (max-width: 940px) {
.layout {
grid-template-columns: 1fr;
}
.panel {
grid-template-columns: repeat(2, minmax(0, 1fr));
}
.readout {
grid-row: span 2;
}
#sceneWrap {
height: 610px;
}
}
@media (max-width: 650px) {
.page {
width: min(100% - 20px, 1240px);
padding-top: 20px;
}
.site-header {
align-items: flex-start;
}
.header-tag {
display: none;
}
.brand-mark {
width: 48px;
height: 48px;
border-radius: 15px;
}
.brand-mark::before,
.brand-mark::after {
left: 12px;
width: 24px;
}
.brand-mark::before {
top: 11px;
}
.brand-mark::after {
top: 27px;
}
.subtitle {
font-size: 0.9rem;
}
.scene-card {
border-radius: 22px;
}
#sceneWrap {
height: 540px;
min-height: 540px;
}
.scene-badge {
top: 12px;
left: 12px;
max-width: 205px;
padding: 10px 11px;
}
.scene-actions {
top: 12px;
right: 12px;
}
.scene-hint {
bottom: 11px;
font-size: 0.69rem;
}
.panel {
grid-template-columns: 1fr;
}
.readout {
grid-row: auto;
}
}
</style>
</head>
<body>
<div class="page">
<header class="site-header">
<div class="brand-row">
<div class="brand-mark" aria-hidden="true"></div>
<div>
<span class="eyebrow">Base Ten Studio</span>
<h1>Place-value rods</h1>
<p class="subtitle">
Move identical beads to decimal heights and watch hundreds, tens, and ones combine.
</p>
</div>
</div>
<div class="header-tag">Interactive decimal model</div>
</header>
<main class="layout">
<section class="scene-card" aria-label="Interactive place-value counting frame">
<div id="sceneWrap">
<canvas
id="scene"
tabindex="0"
aria-label="Three vertical rods labeled hundreds, tens, and ones. Drag a bead vertically or click a numbered height to change its digit."
></canvas>
<div class="scene-badge">
<strong>Height = digit</strong>
<span>Every rod uses the same bead. Its height selects 0–9.</span>
</div>
<div class="scene-actions">
<button id="resetView" class="soft-button" type="button">Reset view</button>
</div>
<div class="scene-hint">
Drag a bead vertically · Click a number · Drag the background to orbit
</div>
</div>
</section>
<aside class="panel">
<section class="panel-card readout">
<span class="readout-label">Decimal value</span>
<output id="total" class="total" aria-live="polite">427</output>
<span class="readout-note">Hundreds + tens + ones</span>
<div class="equation" aria-label="Place-value equation">
<span id="termHundreds">4 × 100</span>
<b>+</b>
<span id="termTens">2 × 10</span>
<b>+</b>
<span id="termOnes">7 × 1</span>
<b>=</b>
<strong id="equationTotal">427</strong>
</div>
</section>
<section class="panel-card">
<h2 class="card-title">The teaching convention</h2>
<div class="convention">
<strong>Height chooses the digit.</strong> The rod label chooses the place value.
All three beads are identical.
</div>
<div class="rule">
<span class="rule-number">1</span>
<div class="rule-text">
Choose a height from <strong>0 at the bottom</strong> to <strong>9 at the top</strong>.
</div>
</div>
<div class="rule">
<span class="rule-number">2</span>
<div class="rule-text">
Read the rod: its place is worth <strong>100, 10, or 1</strong>.
</div>
</div>
<div class="rule">
<span class="rule-number">3</span>
<div class="rule-text">
A bead at <strong>4 on the hundreds rod</strong> means
<strong>4 × 100 = 400</strong>.
</div>
</div>
</section>
<section class="panel-card">
<h2 class="card-title">Change each place</h2>
<div class="places">
<div class="place-row">
<button class="step-button" data-rod="0" data-delta="-1" aria-label="Decrease hundreds digit">−</button>
<div class="place-main">
<span class="place-name">Hundreds</span>
<div class="place-value">
<strong id="digitHundreds" class="place-digit">4</strong>
<span id="contributionHundreds" class="place-contribution">4 × 100 = 400</span>
</div>
</div>
<button class="step-button" data-rod="0" data-delta="1" aria-label="Increase hundreds digit">+</button>
</div>
<div class="place-row">
<button class="step-button" data-rod="1" data-delta="-1" aria-label="Decrease tens digit">−</button>
<div class="place-main">
<span class="place-name">Tens</span>
<div class="place-value">
<strong id="digitTens" class="place-digit">2</strong>
<span id="contributionTens" class="place-contribution">2 × 10 = 20</span>
</div>
</div>
<button class="step-button" data-rod="1" data-delta="1" aria-label="Increase tens digit">+</button>
</div>
<div class="place-row">
<button class="step-button" data-rod="2" data-delta="-1" aria-label="Decrease ones digit">−</button>
<div class="place-main">
<span class="place-name">Ones</span>
<div class="place-value">
<strong id="digitOnes" class="place-digit">7</strong>
<span id="contributionOnes" class="place-contribution">7 × 1 = 7</span>
</div>
</div>
<button class="step-button" data-rod="2" data-delta="1" aria-label="Increase ones digit">+</button>
</div>
</div>
</section>
<section class="panel-card">
<div class="actions">
<button id="randomNumber" class="action-button primary" type="button">New number</button>
<button id="clearNumber" class="action-button" type="button">Clear all</button>
<button id="addOne" class="action-button wide" type="button">Add 1 · practice carrying</button>
</div>
<p class="microcopy">Tip: “Add 1” carries from ones to tens whenever needed.</p>
</section>
</aside>
</main>
<footer>Built as a place-value model: identical beads, explicit positions, base ten.</footer>
</div>
<script type="module">
import * as THREE from "https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
const canvas = document.getElementById("scene");
const sceneWrap = document.getElementById("sceneWrap");
const renderer = new THREE.WebGLRenderer({
canvas,
antialias: true,
alpha: false,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio || 1, 2));
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.05;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xe8eeea);
scene.fog = new THREE.Fog(0xe8eeea, 13, 24);
const camera = new THREE.PerspectiveCamera(38, 1, 0.1, 100);
const cameraTarget = new THREE.Vector3(0, 2.28, 0);
const cameraState = {
theta: 0.12,
phi: 1.27,
zoom: 1
};
const hemisphere = new THREE.HemisphereLight(0xfff9ec, 0x6d8178, 2.15);
scene.add(hemisphere);
const keyLight = new THREE.DirectionalLight(0xfff2d7, 3.2);
keyLight.position.set(4.5, 8.5, 6.5);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(2048, 2048);
keyLight.shadow.camera.left = -6;
keyLight.shadow.camera.right = 6;
keyLight.shadow.camera.top = 7;
keyLight.shadow.camera.bottom = -2;
keyLight.shadow.bias = -0.0005;
scene.add(keyLight);
const fillLight = new THREE.DirectionalLight(0xbde8e0, 1.1);
fillLight.position.set(-5, 4, 4);
scene.add(fillLight);
function roundedRectPath(ctx, x, y, width, height, radius) {
const r = Math.min(radius, width / 2, height / 2);
ctx.beginPath();
ctx.moveTo(x + r, y);
ctx.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 makeWoodTexture() {
const c = document.createElement("canvas");
c.width = 512;
c.height = 128;
const ctx = c.getContext("2d");
const gradient = ctx.createLinearGradient(0, 0, 0, 128);
gradient.addColorStop(0, "#c98c55");
gradient.addColorStop(0.48, "#b87843");
gradient.addColorStop(1, "#9e6035");
ctx.fillStyle = gradient;
ctx.fillRect(0, 0, 512, 128);
for (let i = 0; i < 80; i += 1) {
const y = Math.random() * 128;
ctx.strokeStyle = `rgba(70, 34, 14, ${0.025 + Math.random() * 0.07})`;
ctx.lineWidth = 0.5 + Math.random() * 1.5;
ctx.beginPath();
ctx.moveTo(0, y);
ctx.bezierCurveTo(140, y + Math.random() * 8, 350, y - Math.random() * 8, 512, y);
ctx.stroke();
}
const texture = new THREE.CanvasTexture(c);
texture.colorSpace = THREE.SRGBColorSpace;
texture.wrapS = THREE.RepeatWrapping;
texture.wrapT = THREE.RepeatWrapping;
texture.repeat.set(2.2, 1);
return texture;
}
function makeDigitTexture(value) {
const c = document.createElement("canvas");
c.width = 128;
c.height = 96;
const ctx = c.getContext("2d");
ctx.clearRect(0, 0, 128, 96);
ctx.fillStyle = "#24443e";
ctx.font = "800 58px system-ui, sans-serif";
ctx.textAlign = "center";
ctx.textBaseline = "middle";
ctx.fillText(String(value), 64, 50);
const texture = new THREE.CanvasTexture(c);
texture.colorSpace = THREE.SRGBColorSpace;
texture.minFilter = THREE.LinearFilter;
texture.generateMipmaps = false;
return texture;
}
function makePlaceTexture(title, subtitle) {
const c = document.createElement("canvas");
c.width = 640;
c.height = 180;
const ctx = c.getContext("2d");
roundedRectPath(ctx, 5, 5, 630, 170, 30);
ctx.fillStyle = "rgba(255,255,255,0.94)";
ctx.fill();
ctx.lineWidth = 5;
ctx.strokeStyle = "rgba(19,91,84,0.30)";
ctx.stroke();
ctx.fillStyle = "#174d47";
ctx.font = "800 48px system-ui, sans-serif";
ctx.textAlign = "center";
ctx.textBaseline = "middle";
ctx.fillText(title, 320, 70);
ctx.fillStyle = "#6d817b";
ctx.font = "700 31px system-ui, sans-serif";
ctx.fillText(subtitle, 320, 125);
const texture = new THREE.CanvasTexture(c);
texture.colorSpace = THREE.SRGBColorSpace;
texture.minFilter = THREE.LinearFilter;
texture.generateMipmaps = false;
return texture;
}
const woodTexture = makeWoodTexture();
const woodMaterial = new THREE.MeshStandardMaterial({
map: woodTexture,
color: 0xffffff,
roughness: 0.66,
metalness: 0.02
});
const darkWoodMaterial = new THREE.MeshStandardMaterial({
color: 0x87502e,
roughness: 0.72,
metalness: 0.02
});
const brassMaterial = new THREE.MeshStandardMaterial({
color: 0xc39a52,
roughness: 0.28,
metalness: 0.72
});
const ivoryMaterial = new THREE.MeshStandardMaterial({
color: 0xf5ead2,
roughness: 0.48,
metalness: 0.06
});
const beadMaterial = new THREE.MeshPhysicalMaterial({
color: 0x16857c,
roughness: 0.23,
metalness: 0.02,
clearcoat: 0.8,
clearcoatRoughness: 0.18
});
function roundedBoxGeometry(width, height, depth, radius = 0.08) {
const shape = new THREE.Shape();
const x = -width / 2;
const y = -height / 2;
shape.moveTo(x + radius, y);
shape.lineTo(x + width - radius, y);
shape.quadraticCurveTo(x + width, y, x + width, y + radius);
shape.lineTo(x + width, y + height - radius);
shape.quadraticCurveTo(x + width, y + height, x + width - radius, y + height);
shape.lineTo(x + radius, y + height);
shape.quadraticCurveTo(x, y + height, x, y + height - radius);
shape.lineTo(x, y + radius);
shape.quadraticCurveTo(x, y, x + radius, y);
const geometry = new THREE.ExtrudeGeometry(shape, {
depth,
bevelEnabled: true,
bevelSegments: 2,
steps: 1,
bevelSize: 0.025,
bevelThickness: 0.025,
curveSegments: 6
});
geometry.center();
return geometry;
}
const floor = new THREE.Mesh(
new THREE.PlaneGeometry(30, 30),
new THREE.MeshStandardMaterial({
color: 0xe1e9e3,
roughness: 0.94,
metalness: 0
})
);
floor.rotation.x = -Math.PI / 2;
floor.position.y = -0.075;
floor.receiveShadow = true;
scene.add(floor);
const frame = new THREE.Group();
scene.add(frame);
const FRAME_Z = -0.18;
const ROD_Z = 0.22;
function addFrameBox(width, height, depth, x, y, material = woodMaterial, radius = 0.08) {
const mesh = new THREE.Mesh(
roundedBoxGeometry(width, height, depth, radius),
material
);
mesh.position.set(x, y, FRAME_Z);
mesh.castShadow = true;
mesh.receiveShadow = true;
frame.add(mesh);
return mesh;
}
addFrameBox(5.25, 0.34, 0.78, 0, 0.23, woodMaterial, 0.09);
addFrameBox(5.25, 0.34, 0.78, 0, 4.55, woodMaterial, 0.09);
addFrameBox(0.34, 4.35, 0.78, -2.46, 2.39, woodMaterial, 0.09);
addFrameBox(0.34, 4.35, 0.78, 2.46, 2.39, woodMaterial, 0.09);
addFrameBox(0.74, 0.14, 1.04, -2.08, -0.02, darkWoodMaterial, 0.05);
addFrameBox(0.74, 0.14, 1.04, 2.08, -0.02, darkWoodMaterial, 0.05);
const lowerInset = new THREE.Mesh(
new THREE.BoxGeometry(4.95, 0.035, 0.025),
darkWoodMaterial
);
lowerInset.position.set(0, 0.23, FRAME_Z + 0.405);
frame.add(lowerInset);
const upperInset = lowerInset.clone();
upperInset.position.y = 4.55;
frame.add(upperInset);
const rodX = [-1.45, 0, 1.45];
const placeNames = ["HUNDREDS", "TENS", "ONES"];
const placeSubtitles = ["100s place", "10s place", "1s place"];
const SLOT_START = 0.57;
const SLOT_STEP = 0.39;
const slotY = digit => SLOT_START + digit * SLOT_STEP;
function makeBeadGeometry() {
const profile = [
new THREE.Vector2(0.06, -0.13),
new THREE.Vector2(0.145, -0.115),
new THREE.Vector2(0.205, -0.06),
new THREE.Vector2(0.18, -0.025),
new THREE.Vector2(0.205, 0.025),
new THREE.Vector2(0.145, 0.115),
new THREE.Vector2(0.06, 0.13),
new THREE.Vector2(0.06, -0.13)
];
return new THREE.LatheGeometry(profile, 40);
}
const beadGeometry = makeBeadGeometry();
const interactive = [];
const rodStates = [];
for (let rodIndex = 0; rodIndex < 3; rodIndex += 1) {
const x = rodX[rodIndex];
const rod = new THREE.Mesh(
new THREE.CylinderGeometry(0.052, 0.052, 4.08, 20),
brassMaterial
);
rod.position.set(x, 2.42, ROD_Z);
rod.castShadow = true;
scene.add(rod);
const lowerSocket = new THREE.Mesh(
new THREE.CylinderGeometry(0.105, 0.12, 0.12, 24),
brassMaterial
);
lowerSocket.position.set(x, 0.40, ROD_Z);
lowerSocket.castShadow = true;
scene.add(lowerSocket);
const topCap = new THREE.Mesh(
new THREE.SphereGeometry(0.09, 20, 14),
brassMaterial
);
topCap.position.set(x, 4.45, ROD_Z);
topCap.castShadow = true;
scene.add(topCap);
const tickMaterial = new THREE.MeshStandardMaterial({
color: 0xb58c50,
roughness: 0.4,
metalness: 0.5
});
for (let digit = 0; digit <= 9; digit += 1) {
const y = slotY(digit);
const tick = new THREE.Mesh(
new THREE.BoxGeometry(0.19, 0.018, 0.025),
tickMaterial
);
tick.position.set(x + 0.145, y, ROD_Z + 0.015);
scene.add(tick);
const marker = new THREE.Mesh(
new THREE.CylinderGeometry(0.075, 0.075, 0.035, 24),
ivoryMaterial
);
marker.rotation.x = Math.PI / 2;
marker.position.set(x + 0.31, y, ROD_Z + 0.035);
scene.add(marker);
const hitTarget = new THREE.Mesh(
new THREE.CylinderGeometry(0.17, 0.17, 0.07, 16),
new THREE.MeshBasicMaterial({
transparent: true,
opacity: 0,
depthWrite: false,
colorWrite: false
})
);
hitTarget.rotation.x = Math.PI / 2;
hitTarget.position.set(x + 0.31, y, ROD_Z + 0.09);
hitTarget.userData = { type: "slot", rodIndex, digit };
scene.add(hitTarget);
interactive.push(hitTarget);
const numberMaterial = new THREE.SpriteMaterial({
map: makeDigitTexture(digit),
transparent: true,
depthTest: false,
depthWrite: false,
toneMapped: false
});
const numberSprite = new THREE.Sprite(numberMaterial);
numberSprite.scale.set(0.27, 0.20, 1);
numberSprite.position.set(x + 0.51, y, ROD_Z + 0.12);
numberSprite.renderOrder = 8;
scene.add(numberSprite);
}
const bead = new THREE.Mesh(beadGeometry, beadMaterial);
bead.castShadow = true;
bead.receiveShadow = true;
bead.position.set(x, slotY(0), ROD_Z);
bead.userData = { type: "bead", rodIndex };
scene.add(bead);
interactive.push(bead);
const selectedRing = new THREE.Mesh(
new THREE.TorusGeometry(0.245, 0.018, 10, 44),
new THREE.MeshBasicMaterial({
color: 0xe49a3a,
transparent: true,
opacity: 0.82,
depthWrite: false
})
);
selectedRing.rotation.x = Math.PI / 2;
selectedRing.position.set(x, slotY(0), ROD_Z);
selectedRing.renderOrder = 3;
scene.add(selectedRing);
const label = new THREE.Sprite(
new THREE.SpriteMaterial({
map: makePlaceTexture(placeNames[rodIndex], placeSubtitles[rodIndex]),
transparent: true,
depthTest: false,
depthWrite: false,
toneMapped: false
})
);
label.scale.set(1.43, 0.40, 1);
label.position.set(x, 0.22, 0.38);
label.renderOrder = 10;
scene.add(label);
rodStates.push({
rodIndex,
digit: 0,
bead,
selectedRing,
targetY: slotY(0),
dragging: false,
hovered: false
});
}
const digits = [4, 2, 7];
const placeValues = [100, 10, 1];
const digitOutputs = [
document.getElementById("digitHundreds"),
document.getElementById("digitTens"),
document.getElementById("digitOnes")
];
const contributionOutputs = [
document.getElementById("contributionHundreds"),
document.getElementById("contributionTens"),
document.getElementById("contributionOnes")
];
const termOutputs = [
document.getElementById("termHundreds"),
document.getElementById("termTens"),
document.getElementById("termOnes")
];
const totalOutput = document.getElementById("total");
const equationTotal = document.getElementById("equationTotal");
const stepButtons = [...document.querySelectorAll(".step-button")];
function syncUI() {
let total = 0;
digits.forEach((digit, index) => {
const contribution = digit * placeValues[index];
total += contribution;
digitOutputs[index].textContent = digit;
contributionOutputs[index].textContent =
`${digit} × ${placeValues[index]} = ${contribution}`;
termOutputs[index].textContent = `${digit} × ${placeValues[index]}`;
});
const padded = String(total).padStart(3, "0");
totalOutput.textContent = padded;
equationTotal.textContent = padded;
stepButtons.forEach(button => {
const rodIndex = Number(button.dataset.rod);
const delta = Number(button.dataset.delta);
button.disabled = delta < 0 ? digits[rodIndex] === 0 : digits[rodIndex] === 9;
});
}
function setDigits(nextDigits, immediate = false) {
nextDigits.forEach((value, index) => {
const digit = THREE.MathUtils.clamp(Math.round(value), 0, 9);
digits[index] = digit;
rodStates[index].digit = digit;
rodStates[index].targetY = slotY(digit);
if (immediate) {
rodStates[index].bead.position.y = slotY(digit);
rodStates[index].selectedRing.position.y = slotY(digit);
}
});
syncUI();
}
setDigits(digits, true);
stepButtons.forEach(button => {
button.addEventListener("click", () => {
const rodIndex = Number(button.dataset.rod);
const delta = Number(button.dataset.delta);
const next = digits.slice();
next[rodIndex] = THREE.MathUtils.clamp(next[rodIndex] + delta, 0, 9);
setDigits(next);
});
});
document.getElementById("randomNumber").addEventListener("click", () => {
setDigits([
1 + Math.floor(Math.random() * 9),
Math.floor(Math.random() * 10),
Math.floor(Math.random() * 10)
]);
});
document.getElementById("clearNumber").addEventListener("click", () => {
setDigits([0, 0, 0]);
});
document.getElementById("addOne").addEventListener("click", () => {
const next = digits.slice();
for (let index = 2; index >= 0; index -= 1) {
if (next[index] < 9) {
next[index] += 1;
break;
}
next[index] = 0;
}
setDigits(next);
});
function applyCamera() {
const aspect = camera.aspect || 1;
const responsiveDistance = 9.3 * Math.max(1, 0.9 / aspect);
const radius = responsiveDistance * cameraState.zoom;
const sinPhi = Math.sin(cameraState.phi);
const cosPhi = Math.cos(cameraState.phi);
camera.position.set(
cameraTarget.x + radius * sinPhi * Math.sin(cameraState.theta),
cameraTarget.y + radius * cosPhi,
cameraTarget.z + radius * sinPhi * Math.cos(cameraState.theta)
);
camera.lookAt(cameraTarget);
}
function resetCamera() {
cameraState.theta = 0.12;
cameraState.phi = 1.27;
cameraState.zoom = 1;
applyCamera();
}
document.getElementById("resetView").addEventListener("click", resetCamera);
function resize() {
const width = sceneWrap.clientWidth;
const height = sceneWrap.clientHeight;
renderer.setSize(width, height, false);
camera.aspect = width / height;
camera.updateProjectionMatrix();
applyCamera();
}
new ResizeObserver(resize).observe(sceneWrap);
resize();
const raycaster = new THREE.Raycaster();
const pointer = new THREE.Vector2();
const dragPlane = new THREE.Plane(new THREE.Vector3(0, 0, 1), -ROD_Z);
const dragPoint = new THREE.Vector3();
let pointerMode = null;
let activeRod = null;
let pendingSlot = null;
let pointerStart = { x: 0, y: 0 };
let lastPointer = { x: 0, y: 0 };
let movement = 0;
function setPointer(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 pickInteractive(event) {
setPointer(event);
raycaster.setFromCamera(pointer, camera);
return raycaster.intersectObjects(interactive, false)[0] || null;
}
canvas.addEventListener("pointerdown", event => {
canvas.setPointerCapture(event.pointerId);
pointerStart = { x: event.clientX, y: event.clientY };
lastPointer = { x: event.clientX, y: event.clientY };
movement = 0;
pendingSlot = null;
const hit = pickInteractive(event);
if (hit && hit.object.userData.type === "bead") {
pointerMode = "bead";
activeRod = rodStates[hit.object.userData.rodIndex];
activeRod.dragging = true;
canvas.style.cursor = "grabbing";
} else if (hit && hit.object.userData.type === "slot") {
pointerMode = "slot";
pendingSlot = hit.object.userData;
canvas.style.cursor = "pointer";
} else {
pointerMode = "orbit";
canvas.style.cursor = "grabbing";
}
});
canvas.addEventListener("pointermove", event => {
const dx = event.clientX - lastPointer.x;
const dy = event.clientY - lastPointer.y;
movement += Math.abs(dx) + Math.abs(dy);
lastPointer = { x: event.clientX, y: event.clientY };
if (pointerMode === "bead" && activeRod) {
setPointer(event);
raycaster.setFromCamera(pointer, camera);
if (raycaster.ray.intersectPlane(dragPlane, dragPoint)) {
const minY = slotY(0) - 0.11;
const maxY = slotY(9) + 0.11;
const y = THREE.MathUtils.clamp(dragPoint.y, minY, maxY);
activeRod.bead.position.y = y;
activeRod.selectedRing.position.y = y;
const digit = THREE.MathUtils.clamp(
Math.round((y - SLOT_START) / SLOT_STEP),
0,
9
);
if (digit !== activeRod.digit) {
activeRod.digit = digit;
digits[activeRod.rodIndex] = digit;
syncUI();
}
}
return;
}
if (pointerMode === "orbit") {
cameraState.theta = THREE.MathUtils.clamp(
cameraState.theta - dx * 0.005,
-0.68,
0.68
);
cameraState.phi = THREE.MathUtils.clamp(
cameraState.phi + dy * 0.004,
0.88,
1.48
);
applyCamera();
return;
}
if (!pointerMode) {
const hit = pickInteractive(event);
canvas.style.cursor = hit ? "pointer" : "grab";
}
});
function finishPointer(event) {
if (pointerMode === "bead" && activeRod) {
activeRod.targetY = slotY(activeRod.digit);
activeRod.dragging = false;
activeRod = null;
} else if (
pointerMode === "slot" &&
pendingSlot &&
movement < 8
) {
const next = digits.slice();
next[pendingSlot.rodIndex] = pendingSlot.digit;
setDigits(next);
}
pointerMode = null;
pendingSlot = null;
canvas.style.cursor = "grab";
if (canvas.hasPointerCapture(event.pointerId)) {
canvas.releasePointerCapture(event.pointerId);
}
}
canvas.addEventListener("pointerup", finishPointer);
canvas.addEventListener("pointercancel", finishPointer);
canvas.addEventListener("pointerleave", () => {
if (!pointerMode) canvas.style.cursor = "grab";
});
canvas.addEventListener(
"wheel",
event => {
event.preventDefault();
cameraState.zoom = THREE.MathUtils.clamp(
cameraState.zoom + event.deltaY * 0.0008,
0.74,
1.38
);
applyCamera();
},
{ passive: false }
);
canvas.style.cursor = "grab";
const clock = new THREE.Clock();
function animate() {
const delta = Math.min(clock.getDelta(), 0.05);
const elapsed = clock.elapsedTime;
rodStates.forEach(state => {
if (!state.dragging) {
const easing = 1 - Math.exp(-13 * delta);
state.bead.position.y += (state.targetY - state.bead.position.y) * easing;
state.selectedRing.position.y = state.bead.position.y;
}
const hoverScale = state.hovered || state.dragging ? 1.07 : 1;
const scaleEasing = 1 - Math.exp(-12 * delta);
const nextScale = state.bead.scale.x + (hoverScale - state.bead.scale.x) * scaleEasing;
state.bead.scale.setScalar(nextScale);
state.selectedRing.material.opacity = 0.7 + Math.sin(elapsed * 2.1 + state.rodIndex) * 0.12;
});
renderer.render(scene, camera);
requestAnimationFrame(animate);
}
animate();
</script>
</body>
</html>
```