Prompt specification
Build or customize the Three.js 3D scene "Carrying one group to the next rod" 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: Carrying one group to the next rod
Category: Art & Culture · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/carrying-one-group-to-the-next-rod-x2482
Scene Description Prompt:
Build a self-contained HTML scene with Three.js from cdnjs featuring place-value arithmetic on an abacus. Animate ten unit counters being exchanged for one counter on the next place-value rod. Keep the displayed total unchanged throughout the regrouping operation. A 3D cutaway view.
Working Three.js Starter (carrying-one-group-to-the-next-rod-x2482):
```html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Place Value Abacus — Ten-to-One Exchange</title>
<style>
:root {
--ink: #26313a;
--muted: #66727a;
--paper: rgba(250, 248, 242, 0.88);
--line: rgba(38, 49, 58, 0.14);
--orange: #df744c;
--teal: #159b91;
--blue: #586bc8;
--green: #368a61;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #e8e4db;
color: var(--ink);
font-family:
Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
#stage {
position: fixed;
inset: 0;
}
canvas {
display: block;
width: 100%;
height: 100%;
touch-action: none;
outline: none;
}
.hud {
position: fixed;
inset: 0;
pointer-events: none;
}
.title-card {
position: absolute;
top: 24px;
left: 26px;
max-width: 390px;
}
.eyebrow {
margin-bottom: 7px;
color: var(--teal);
font-size: 11px;
font-weight: 800;
letter-spacing: 0.18em;
text-transform: uppercase;
}
h1 {
margin: 0;
font-family: Georgia, "Times New Roman", serif;
font-size: clamp(30px, 3vw, 48px);
font-weight: 500;
line-height: 0.98;
letter-spacing: -0.035em;
}
.subtitle {
margin: 12px 0 0;
max-width: 350px;
color: var(--muted);
font-size: 13px;
line-height: 1.5;
}
.cutaway-badge {
display: inline-flex;
align-items: center;
gap: 7px;
margin-top: 14px;
padding: 7px 10px;
border: 1px solid var(--line);
border-radius: 999px;
background: rgba(255, 255, 255, 0.58);
color: #59656c;
font-size: 10px;
font-weight: 800;
letter-spacing: 0.1em;
text-transform: uppercase;
backdrop-filter: blur(8px);
}
.cutaway-badge::before {
width: 7px;
height: 7px;
border-radius: 50%;
background: var(--orange);
content: "";
}
.readout {
position: absolute;
top: 24px;
right: 26px;
width: 302px;
padding: 18px 20px 17px;
border: 1px solid rgba(255, 255, 255, 0.72);
border-radius: 20px;
background: var(--paper);
box-shadow:
0 16px 42px rgba(42, 49, 52, 0.12),
inset 0 1px 0 rgba(255, 255, 255, 0.72);
backdrop-filter: blur(16px);
}
.readout-label {
color: var(--muted);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.15em;
text-transform: uppercase;
}
.total-row {
display: flex;
align-items: flex-end;
gap: 11px;
margin-top: 3px;
}
#totalValue {
font-family: Georgia, "Times New Roman", serif;
font-size: 68px;
font-weight: 500;
line-height: 1;
letter-spacing: -0.06em;
}
.total-unit {
margin-bottom: 8px;
color: var(--muted);
font-size: 13px;
font-weight: 650;
}
.constant {
display: inline-flex;
align-items: center;
gap: 6px;
margin: 8px 0 13px;
color: var(--green);
font-size: 10px;
font-weight: 850;
letter-spacing: 0.12em;
text-transform: uppercase;
}
.constant::before {
width: 7px;
height: 7px;
border-radius: 50%;
background: var(--green);
box-shadow: 0 0 0 4px rgba(54, 138, 97, 0.12);
content: "";
}
.equation {
display: flex;
align-items: center;
justify-content: space-between;
gap: 8px;
padding-top: 13px;
border-top: 1px solid var(--line);
font-family: Georgia, "Times New Roman", serif;
font-size: 20px;
font-weight: 600;
}
.unit-term {
color: var(--orange);
}
.ten-term {
color: var(--teal);
}
.status {
display: flex;
align-items: flex-start;
gap: 8px;
min-height: 34px;
margin-top: 12px;
color: var(--muted);
font-size: 11px;
line-height: 1.45;
}
.status-dot {
flex: 0 0 auto;
width: 6px;
height: 6px;
margin-top: 4px;
border-radius: 50%;
background: var(--orange);
box-shadow: 0 0 0 4px rgba(223, 116, 76, 0.1);
}
.progress {
height: 3px;
margin-top: 12px;
overflow: hidden;
border-radius: 999px;
background: rgba(38, 49, 58, 0.09);
}
#progressFill {
width: 0;
height: 100%;
border-radius: inherit;
background: linear-gradient(90deg, var(--orange), var(--teal));
transition: width 80ms linear;
}
.equation-card {
position: absolute;
left: 50%;
bottom: 24px;
display: flex;
align-items: center;
gap: 15px;
padding: 12px 17px;
border: 1px solid rgba(255, 255, 255, 0.7);
border-radius: 15px;
background: rgba(250, 248, 242, 0.78);
box-shadow: 0 10px 30px rgba(42, 49, 52, 0.08);
color: var(--muted);
font-size: 12px;
transform: translateX(-50%);
backdrop-filter: blur(12px);
}
.equation-card strong {
color: var(--ink);
font-family: Georgia, "Times New Roman", serif;
font-size: 19px;
font-weight: 600;
}
.equation-card .orange {
color: var(--orange);
}
.equation-card .teal {
color: var(--teal);
}
.controls {
position: absolute;
right: 26px;
bottom: 24px;
display: flex;
flex-direction: column;
align-items: stretch;
gap: 8px;
width: 238px;
pointer-events: auto;
}
button {
border: 0;
border-radius: 12px;
cursor: pointer;
font: inherit;
transition:
transform 150ms ease,
box-shadow 150ms ease,
opacity 150ms ease;
}
button:hover:not(:disabled) {
transform: translateY(-1px);
}
button:active:not(:disabled) {
transform: translateY(1px);
}
button:focus-visible {
outline: 3px solid rgba(21, 155, 145, 0.35);
outline-offset: 3px;
}
#exchangeButton {
min-height: 50px;
padding: 0 16px;
background: var(--ink);
box-shadow: 0 9px 20px rgba(38, 49, 58, 0.19);
color: white;
font-size: 12px;
font-weight: 750;
letter-spacing: 0.01em;
}
#exchangeButton:disabled {
cursor: wait;
opacity: 0.62;
}
#resetButton {
min-height: 40px;
border: 1px solid var(--line);
background: rgba(250, 248, 242, 0.82);
color: var(--ink);
font-size: 11px;
font-weight: 700;
backdrop-filter: blur(10px);
}
.legend {
position: absolute;
left: 26px;
bottom: 25px;
display: flex;
flex-direction: column;
gap: 8px;
color: var(--muted);
font-size: 10px;
line-height: 1.4;
}
.legend-row {
display: flex;
align-items: center;
gap: 8px;
}
.swatch {
width: 9px;
height: 9px;
border-radius: 50%;
box-shadow: inset 0 0 0 1px rgba(0, 0, 0, 0.08);
}
.swatch.orange {
background: var(--orange);
}
.swatch.teal {
background: var(--teal);
}
.controls-hint {
margin-top: 4px;
color: #7a8388;
}
@media (max-width: 820px) {
.title-card {
top: 16px;
left: 16px;
max-width: 280px;
}
h1 {
font-size: 30px;
}
.subtitle,
.cutaway-badge {
display: none;
}
.readout {
top: 105px;
right: 16px;
width: 225px;
padding: 13px 15px;
}
#totalValue {
font-size: 48px;
}
.equation {
font-size: 16px;
}
.controls {
right: 16px;
bottom: 16px;
width: 210px;
}
.equation-card {
left: 16px;
bottom: 16px;
transform: none;
}
.legend {
display: none;
}
}
@media (max-width: 560px) {
.readout {
top: 80px;
width: 205px;
}
.readout .status {
display: none;
}
.equation-card {
display: none;
}
.controls {
left: 16px;
right: 16px;
width: auto;
flex-direction: row;
}
#exchangeButton {
flex: 1;
}
#resetButton {
width: 95px;
}
}
</style>
</head>
<body>
<main id="stage" aria-label="Interactive three-dimensional place-value abacus"></main>
<div class="hud">
<section class="title-card">
<div class="eyebrow">Place Value Lab</div>
<h1>The Ten-to-One Exchange</h1>
<p class="subtitle">
Watch ten unit counters travel to the tens rod and become one counter
worth ten. The abacus value stays exactly the same.
</p>
<div class="cutaway-badge">Open-front 3D cutaway</div>
</section>
<section class="readout" aria-label="Abacus value readout">
<div class="readout-label">Displayed total</div>
<div class="total-row">
<div id="totalValue">10</div>
<div class="total-unit">units</div>
</div>
<div class="constant">Value held constant</div>
<div class="equation" aria-label="Ten unit counters equal one ten counter">
<span class="unit-term">10 × 1</span>
<span>=</span>
<span class="ten-term">1 × 10</span>
</div>
<div class="status" aria-live="polite">
<span class="status-dot"></span>
<span id="statusText">Ready: ten unit counters are on the ones rod.</span>
</div>
<div class="progress" aria-hidden="true">
<div id="progressFill"></div>
</div>
</section>
<section class="equation-card" aria-hidden="true">
<span class="orange">10</span>
<strong>×</strong>
<span class="orange">1</span>
<strong>=</strong>
<span class="teal">1</span>
<strong>×</strong>
<span class="teal">10</span>
<strong>=</strong>
<span>10</span>
</section>
<section class="controls">
<button id="exchangeButton" type="button">
Exchange ten unit counters
</button>
<button id="resetButton" type="button">Reset abacus</button>
<div class="legend">
<div class="legend-row">
<span class="swatch orange"></span>
<span>Unit counter · value 1</span>
</div>
<div class="legend-row">
<span class="swatch teal"></span>
<span>Ten counter · value 10</span>
</div>
<div class="controls-hint">Drag to orbit · Scroll to zoom · Double-click to reset view</div>
</div>
</section>
</div>
<script type="module">
import * as THREE from "https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// -------------------------------------------------------------------------
// Renderer and scene
// -------------------------------------------------------------------------
const stage = document.getElementById("stage");
const totalValue = document.getElementById("totalValue");
const statusText = document.getElementById("statusText");
const progressFill = document.getElementById("progressFill");
const exchangeButton = document.getElementById("exchangeButton");
const resetButton = document.getElementById("resetButton");
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xe8e4db);
scene.fog = new THREE.Fog(0xe8e4db, 22, 42);
const renderer = new THREE.WebGLRenderer({
antialias: true,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.12;
renderer.domElement.setAttribute("aria-label", "3D cutaway abacus");
renderer.domElement.tabIndex = 0;
stage.appendChild(renderer.domElement);
const camera = new THREE.PerspectiveCamera(
38,
window.innerWidth / window.innerHeight,
0.1,
100
);
const cameraTarget = new THREE.Vector3(0, 1.25, 0);
const cameraState = {
theta: 0.56,
phi: 1.06,
radius: window.innerWidth / window.innerHeight < 0.8 ? 26 : 13.8,
targetTheta: 0.56,
targetPhi: 1.06,
targetRadius: window.innerWidth / window.innerHeight < 0.8 ? 26 : 13.8
};
function updateCamera() {
const sinPhi = Math.sin(cameraState.phi);
camera.position.set(
cameraTarget.x + cameraState.radius * sinPhi * Math.sin(cameraState.theta),
cameraTarget.y + cameraState.radius * Math.cos(cameraState.phi),
cameraTarget.z + cameraState.radius * sinPhi * Math.cos(cameraState.theta)
);
camera.lookAt(cameraTarget);
}
updateCamera();
// -------------------------------------------------------------------------
// Lights and ground
// -------------------------------------------------------------------------
const hemisphere = new THREE.HemisphereLight(0xffffff, 0x8c735d, 2.15);
scene.add(hemisphere);
const keyLight = new THREE.DirectionalLight(0xfff4dc, 3.4);
keyLight.position.set(6, 10, 8);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(2048, 2048);
keyLight.shadow.camera.left = -11;
keyLight.shadow.camera.right = 11;
keyLight.shadow.camera.top = 11;
keyLight.shadow.camera.bottom = -11;
keyLight.shadow.camera.near = 1;
keyLight.shadow.camera.far = 30;
keyLight.shadow.bias = -0.0004;
scene.add(keyLight);
const fillLight = new THREE.DirectionalLight(0xb7d7ff, 1.25);
fillLight.position.set(-7, 5, 3);
scene.add(fillLight);
const rimLight = new THREE.PointLight(0xffb077, 18, 18, 2);
rimLight.position.set(1, 4, -4);
scene.add(rimLight);
const ground = new THREE.Mesh(
new THREE.PlaneGeometry(80, 80),
new THREE.MeshStandardMaterial({
color: 0xe8e4db,
roughness: 0.96,
metalness: 0
})
);
ground.rotation.x = -Math.PI / 2;
ground.position.y = -0.39;
ground.receiveShadow = true;
scene.add(ground);
const grid = new THREE.GridHelper(34, 34, 0xb8b1a5, 0xcec8bd);
grid.position.y = -0.375;
grid.material.transparent = true;
grid.material.opacity = 0.22;
scene.add(grid);
// -------------------------------------------------------------------------
// Procedural textures
// -------------------------------------------------------------------------
function makeWoodTexture() {
const canvas = document.createElement("canvas");
canvas.width = 512;
canvas.height = 512;
const ctx = canvas.getContext("2d");
ctx.fillStyle = "#a96f43";
ctx.fillRect(0, 0, 512, 512);
let seed = 31;
const random = () => {
seed = (seed * 16807) % 2147483647;
return (seed - 1) / 2147483646;
};
for (let i = 0; i < 105; i++) {
const y = random() * 512;
const alpha = 0.025 + random() * 0.075;
const wave = 4 + random() * 18;
ctx.beginPath();
ctx.moveTo(-20, y);
for (let x = 0; x <= 540; x += 32) {
const yy =
y +
Math.sin(x * 0.023 + i) * wave * 0.25 +
Math.sin(x * 0.071 + i * 0.7) * wave * 0.12;
ctx.lineTo(x, yy);
}
ctx.strokeStyle = `rgba(68, 34, 13, ${alpha})`;
ctx.lineWidth = 1 + random() * 3;
ctx.stroke();
}
for (let i = 0; i < 18; i++) {
const x = random() * 512;
const y = random() * 512;
ctx.beginPath();
ctx.ellipse(x, y, 8 + random() * 22, 2 + random() * 5, 0, 0, Math.PI * 2);
ctx.strokeStyle = "rgba(70, 37, 16, 0.13)";
ctx.lineWidth = 2;
ctx.stroke();
}
const texture = new THREE.CanvasTexture(canvas);
texture.colorSpace = THREE.SRGBColorSpace;
texture.wrapS = THREE.RepeatWrapping;
texture.wrapT = THREE.RepeatWrapping;
texture.repeat.set(2.3, 1.8);
return texture;
}
function makeRodLabel(title, subtitle, accent) {
const canvas = document.createElement("canvas");
canvas.width = 512;
canvas.height = 180;
const ctx = canvas.getContext("2d");
ctx.clearRect(0, 0, 512, 180);
ctx.fillStyle = "rgba(250, 248, 242, 0.94)";
ctx.fillRect(0, 0, 512, 180);
ctx.fillStyle = accent;
ctx.fillRect(0, 0, 12, 180);
ctx.fillStyle = "#26313a";
ctx.font = "800 48px Arial, sans-serif";
ctx.fillText(title, 38, 72);
ctx.fillStyle = "#6d777c";
ctx.font = "500 27px Arial, sans-serif";
ctx.fillText(subtitle, 40, 124);
const texture = new THREE.CanvasTexture(canvas);
texture.colorSpace = THREE.SRGBColorSpace;
texture.anisotropy = renderer.capabilities.getMaxAnisotropy();
return texture;
}
function makeCounterNumberTexture(text, color) {
const canvas = document.createElement("canvas");
canvas.width = 256;
canvas.height = 256;
const ctx = canvas.getContext("2d");
ctx.clearRect(0, 0, 256, 256);
ctx.textAlign = "center";
ctx.textBaseline = "middle";
ctx.fillStyle = color;
ctx.font = "800 150px Georgia, serif";
ctx.fillText(text, 128, 128);
const texture = new THREE.CanvasTexture(canvas);
texture.colorSpace = THREE.SRGBColorSpace;
texture.anisotropy = renderer.capabilities.getMaxAnisotropy();
return texture;
}
// -------------------------------------------------------------------------
// Cutaway abacus structure
// -------------------------------------------------------------------------
const woodTexture = makeWoodTexture();
const woodMaterial = new THREE.MeshStandardMaterial({
map: woodTexture,
color: 0xffffff,
roughness: 0.72,
metalness: 0
});
const darkWoodMaterial = new THREE.MeshStandardMaterial({
map: woodTexture,
color: 0x9b633c,
roughness: 0.76,
metalness: 0
});
const topWoodMaterial = new THREE.MeshStandardMaterial({
map: woodTexture,
color: 0xc08a5d,
roughness: 0.68,
metalness: 0
});
const brassMaterial = new THREE.MeshStandardMaterial({
color: 0xc89a4b,
roughness: 0.24,
metalness: 0.78
});
const darkBrassMaterial = new THREE.MeshStandardMaterial({
color: 0x72502c,
roughness: 0.36,
metalness: 0.7
});
function addBox(width, height, depth, material, x, y, z) {
const mesh = new THREE.Mesh(
new THREE.BoxGeometry(width, height, depth),
material
);
mesh.position.set(x, y, z);
mesh.castShadow = true;
mesh.receiveShadow = true;
scene.add(mesh);
return mesh;
}
// Layered base.
addBox(11.9, 0.7, 10.6, woodMaterial, 0, 0, 0);
addBox(11.55, 0.12, 10.25, topWoodMaterial, 0, 0.41, 0);
// Rear frame.
addBox(11.9, 3.4, 0.3, darkWoodMaterial, 0, 2.08, -4.86);
addBox(11.9, 0.32, 0.46, woodMaterial, 0, 3.72, -4.67);
// Full left cheek and deliberately cut-down right cheek reveal the interior.
addBox(0.3, 3.15, 9.52, woodMaterial, -5.78, 1.92, -0.08);
addBox(0.3, 1.03, 9.52, woodMaterial, 5.78, 0.86, -0.08);
// Warm edge marking the cutaway section.
addBox(
0.33,
0.075,
9.52,
new THREE.MeshStandardMaterial({
color: 0xdf744c,
roughness: 0.55
}),
5.78,
1.405,
-0.08
);
// Low front fascia; the front remains open.
addBox(11.9, 0.44, 0.34, woodMaterial, 0, 0.55, 4.83);
// Three place-value rods.
const rodX = [-3, 0, 3];
const rodNames = ["ONES", "TENS", "HUNDREDS"];
const rodSubtitles = [
"1 counter = 1",
"1 counter = 10",
"1 counter = 100"
];
const rodAccents = ["#df744c", "#159b91", "#586bc8"];
for (let i = 0; i < rodX.length; i++) {
const x = rodX[i];
const rod = new THREE.Mesh(
new THREE.CylinderGeometry(0.055, 0.055, 9.1, 24),
brassMaterial
);
rod.rotation.x = Math.PI / 2;
rod.position.set(x, 0.98, 0);
rod.castShadow = true;
scene.add(rod);
for (const z of [-4.43, 4.35]) {
const post = new THREE.Mesh(
new THREE.CylinderGeometry(0.16, 0.19, 0.66, 24),
darkBrassMaterial
);
post.position.set(x, 0.72, z);
post.castShadow = true;
scene.add(post);
const collar = new THREE.Mesh(
new THREE.TorusGeometry(0.17, 0.035, 10, 30),
brassMaterial
);
collar.rotation.x = Math.PI / 2;
collar.position.set(x, 1.01, z);
scene.add(collar);
}
const backLabel = new THREE.Mesh(
new THREE.PlaneGeometry(2.75, 0.97),
new THREE.MeshBasicMaterial({
map: makeRodLabel(
rodNames[i],
rodSubtitles[i],
rodAccents[i]
),
transparent: true,
toneMapped: false
})
);
backLabel.position.set(x, 3.03, -4.69);
scene.add(backLabel);
const frontLabel = new THREE.Mesh(
new THREE.PlaneGeometry(2.35, 0.52),
new THREE.MeshBasicMaterial({
map: makeRodLabel(
rodNames[i],
`${[1, 10, 100][i]} each`,
rodAccents[i]
),
transparent: true,
toneMapped: false
})
);
frontLabel.position.set(x, 0.1, 5.31);
scene.add(frontLabel);
}
// -------------------------------------------------------------------------
// Counter construction
// -------------------------------------------------------------------------
function makeCounterGeometry(radius, depth) {
const shape = new THREE.Shape();
shape.absarc(0, 0, radius, 0, Math.PI * 2, false);
const hole = new THREE.Path();
hole.absarc(0, 0, 0.105, 0, Math.PI * 2, true);
shape.holes.push(hole);
const geometry = new THREE.ExtrudeGeometry(shape, {
depth,
bevelEnabled: false,
curveSegments: 48
});
geometry.translate(0, 0, -depth / 2);
return geometry;
}
function makeCounter(radius, depth, color, number, numberColor) {
const group = new THREE.Group();
const bodyMaterial = new THREE.MeshStandardMaterial({
color,
roughness: 0.32,
metalness: 0.08,
transparent: true,
opacity: 1
});
const body = new THREE.Mesh(
makeCounterGeometry(radius, depth),
bodyMaterial
);
body.castShadow = true;
body.receiveShadow = true;
group.add(body);
const rimMaterial = new THREE.MeshStandardMaterial({
color: 0xffe1b2,
roughness: 0.3,
metalness: 0.18,
transparent: true,
opacity: 0.82
});
const rim = new THREE.Mesh(
new THREE.TorusGeometry(radius - 0.045, 0.025, 10, 52),
rimMaterial
);
rim.position.z = depth / 2 + 0.014;
group.add(rim);
const numberMaterial = new THREE.MeshBasicMaterial({
map: makeCounterNumberTexture(number, numberColor),
transparent: true,
opacity: 0.96,
depthWrite: false,
toneMapped: false
});
const numberPlane = new THREE.Mesh(
new THREE.PlaneGeometry(radius * 0.72, radius * 0.72),
numberMaterial
);
numberPlane.position.set(0, radius * 0.17, depth / 2 + 0.02);
group.add(numberPlane);
return group;
}
function setGroupOpacity(group, opacity) {
group.traverse((child) => {
if (child.material) {
const materials = Array.isArray(child.material)
? child.material
: [child.material];
for (const material of materials) {
material.transparent = true;
material.opacity = opacity;
}
}
});
}
const unitCounters = [];
const unitStartZ = [];
for (let i = 0; i < 10; i++) {
const z = -3.24 + i * 0.72;
unitStartZ.push(z);
const counter = makeCounter(
0.355,
0.17,
i % 2 === 0 ? 0xe47a4f : 0xdc6b42,
"1",
"#fff8e9"
);
counter.position.set(-3, 0.98, z);
scene.add(counter);
unitCounters.push(counter);
}
const tenCounter = makeCounter(
0.62,
0.27,
0x159b91,
"10",
"#f0fffb"
);
tenCounter.position.set(0, 0.98, 0);
tenCounter.visible = false;
scene.add(tenCounter);
const targetHalo = new THREE.Mesh(
new THREE.TorusGeometry(0.84, 0.028, 12, 64),
new THREE.MeshBasicMaterial({
color: 0x75e3d6,
transparent: true,
opacity: 0,
depthWrite: false,
toneMapped: false
})
);
targetHalo.position.set(0, 0.98, 0.22);
targetHalo.visible = false;
scene.add(targetHalo);
// Small convergence particles.
const particles = [];
const particleGeometry = new THREE.SphereGeometry(0.045, 8, 8);
const particleMaterial = new THREE.MeshBasicMaterial({
color: 0xffd991,
transparent: true,
opacity: 0,
depthWrite: false,
toneMapped: false
});
for (let i = 0; i < 24; i++) {
const particle = new THREE.Mesh(particleGeometry, particleMaterial);
particle.visible = false;
scene.add(particle);
particles.push(particle);
}
// -------------------------------------------------------------------------
// Animation state
// -------------------------------------------------------------------------
const exchangeDuration = 2.8;
const growthDuration = 0.72;
let phase = "ready";
let animationStart = 0;
const clamp01 = (value) => Math.max(0, Math.min(1, value));
const lerp = (a, b, t) => a + (b - a) * t;
function smoothstep(edge0, edge1, value) {
const t = clamp01((value - edge0) / (edge1 - edge0));
return t * t * (3 - 2 * t);
}
function easeInOutCubic(value) {
return value < 0.5
? 4 * value * value * value
: 1 - Math.pow(-2 * value + 2, 3) / 2;
}
function easeOutBack(value) {
const c1 = 1.70158;
const c3 = c1 + 1;
return 1 + c3 * Math.pow(value - 1, 3) + c1 * Math.pow(value - 1, 2);
}
function resetScene(updateUI = true) {
phase = "ready";
animationStart = 0;
tenCounter.visible = false;
tenCounter.scale.setScalar(1);
targetHalo.visible = false;
for (let i = 0; i < unitCounters.length; i++) {
const counter = unitCounters[i];
counter.visible = true;
counter.position.set(-3, 0.98, unitStartZ[i]);
counter.rotation.set(0, 0, 0);
counter.scale.setScalar(1);
setGroupOpacity(counter, 1);
}
for (const particle of particles) {
particle.visible = false;
}
particleMaterial.opacity = 0;
progressFill.style.width = "0%";
if (updateUI) {
exchangeButton.disabled = false;
exchangeButton.textContent = "Exchange ten unit counters";
statusText.textContent =
"Ready: ten unit counters are on the ones rod.";
}
}
function startExchange() {
if (phase === "animating") return;
if (phase === "done") resetScene(false);
phase = "animating";
animationStart = performance.now();
tenCounter.visible = false;
tenCounter.scale.setScalar(0.01);
exchangeButton.disabled = true;
exchangeButton.textContent = "Regrouping…";
statusText.textContent =
"The ten unit counters are moving to the tens rod. Total remains 10.";
}
function updateExchange(now) {
if (phase !== "animating") return;
const elapsed = (now - animationStart) / 1000;
const p = clamp01(elapsed / exchangeDuration);
// All ten counters move together, making the exchange atomic: no
// intermediate place-value count is implied on the rods.
for (let i = 0; i < unitCounters.length; i++) {
const counter = unitCounters[i];
const u = easeInOutCubic(p);
const startZ = unitStartZ[i];
const arcHeight = 1.22 + Math.abs(startZ) * 0.075;
const sideDrift = Math.sin(u * Math.PI) * (i % 2 ? 0.13 : -0.13);
counter.position.set(
lerp(-3, 0, u) + sideDrift,
0.98 + Math.sin(Math.PI * u) * arcHeight,
lerp(startZ, 0, u)
);
counter.rotation.set(
u * 0.18,
Math.sin(Math.PI * u) * (i % 2 ? 0.42 : -0.42),
u * Math.PI * 1.25
);
counter.scale.setScalar(Math.max(0.01, 1 - u * 0.95));
setGroupOpacity(counter, 1 - smoothstep(0.72, 1, u));
if (u >= 0.999) {
counter.visible = false;
}
}
if (p > 0.69) {
targetHalo.visible = true;
const haloProgress = clamp01((p - 0.69) / 0.31);
const pulse = 1 + Math.sin(haloProgress * Math.PI * 5) * 0.1;
targetHalo.scale.setScalar(pulse);
targetHalo.material.opacity = 0.22 + haloProgress * 0.62;
}
// The ten counter materializes only after all unit counters converge.
if (p >= 1) {
const growth = clamp01((elapsed - exchangeDuration) / growthDuration);
const scale = Math.max(0.01, easeOutBack(growth));
tenCounter.visible = true;
tenCounter.scale.setScalar(scale);
targetHalo.material.opacity = 0.7 * (1 - growth);
const particleProgress = clamp01((elapsed - exchangeDuration) / 0.9);
particleMaterial.opacity = 1 - particleProgress;
for (let i = 0; i < particles.length; i++) {
const particle = particles[i];
const angle = (i / particles.length) * Math.PI * 2 + i * 0.17;
const age = clamp01(
(particleProgress - (i % 6) * 0.025) / 0.72
);
particle.visible = age > 0 && age < 1;
particle.position.set(
Math.cos(angle) * age * 1.35,
0.98 + Math.sin(angle) * age * 1.02,
0.25 + Math.sin(angle * 2) * 0.08
);
particle.scale.setScalar(0.75 + age * 0.8);
}
}
progressFill.style.width = `${Math.min(100, (elapsed / 3.65) * 100)}%`;
if (elapsed >= exchangeDuration + growthDuration + 0.08) {
phase = "done";
tenCounter.visible = true;
tenCounter.scale.setScalar(1);
targetHalo.visible = true;
targetHalo.material.opacity = 0.18;
for (const particle of particles) {
particle.visible = false;
}
for (const counter of unitCounters) {
counter.visible = false;
}
progressFill.style.width = "100%";
exchangeButton.disabled = false;
exchangeButton.textContent = "Replay exchange";
statusText.textContent =
"Exchange complete: one ten counter now represents the same total of 10.";
}
}
exchangeButton.addEventListener("click", startExchange);
resetButton.addEventListener("click", () => resetScene(true));
// -------------------------------------------------------------------------
// Custom camera controls: orbit, wheel zoom, pinch zoom, keyboard
// -------------------------------------------------------------------------
const pointers = new Map();
let lastPointer = null;
let previousPinchDistance = 0;
function pinchDistance() {
const values = [...pointers.values()];
if (values.length < 2) return 0;
return Math.hypot(
values[0].x - values[1].x,
values[0].y - values[1].y
);
}
renderer.domElement.addEventListener("pointerdown", (event) => {
renderer.domElement.setPointerCapture(event.pointerId);
pointers.set(event.pointerId, { x: event.clientX, y: event.clientY });
if (pointers.size === 1) {
lastPointer = { x: event.clientX, y: event.clientY };
} else if (pointers.size === 2) {
previousPinchDistance = pinchDistance();
}
});
renderer.domElement.addEventListener("pointermove", (event) => {
if (!pointers.has(event.pointerId)) return;
pointers.set(event.pointerId, { x: event.clientX, y: event.clientY });
if (pointers.size >= 2) {
const distance = pinchDistance();
if (previousPinchDistance > 0 && distance > 0) {
cameraState.targetRadius = THREE.MathUtils.clamp(
cameraState.targetRadius *
(previousPinchDistance / distance),
8,
29
);
}
previousPinchDistance = distance;
return;
}
if (!lastPointer) return;
const dx = event.clientX - lastPointer.x;
const dy = event.clientY - lastPointer.y;
cameraState.targetTheta -= dx * 0.006;
cameraState.targetPhi = THREE.MathUtils.clamp(
cameraState.targetPhi + dy * 0.005,
0.5,
1.46
);
lastPointer = { x: event.clientX, y: event.clientY };
});
function releasePointer(event) {
pointers.delete(event.pointerId);
previousPinchDistance = pinchDistance();
if (pointers.size === 0) lastPointer = null;
if (pointers.size === 1) {
const remaining = [...pointers.values()][0];
lastPointer = { x: remaining.x, y: remaining.y };
}
}
renderer.domElement.addEventListener("pointerup", releasePointer);
renderer.domElement.addEventListener("pointercancel", releasePointer);
renderer.domElement.addEventListener(
"wheel",
(event) => {
event.preventDefault();
cameraState.targetRadius = THREE.MathUtils.clamp(
cameraState.targetRadius * Math.exp(event.deltaY * 0.001),
8,
29
);
},
{ passive: false }
);
function resetCamera() {
cameraState.targetTheta = 0.56;
cameraState.targetPhi = 1.06;
cameraState.targetRadius =
window.innerWidth / window.innerHeight < 0.8 ? 26 : 13.8;
}
renderer.domElement.addEventListener("dblclick", resetCamera);
renderer.domElement.addEventListener("keydown", (event) => {
const step = 0.09;
if (event.key === "ArrowLeft") {
cameraState.targetTheta += step;
} else if (event.key === "ArrowRight") {
cameraState.targetTheta -= step;
} else if (event.key === "ArrowUp") {
cameraState.targetPhi = THREE.MathUtils.clamp(
cameraState.targetPhi - step,
0.5,
1.46
);
} else if (event.key === "ArrowDown") {
cameraState.targetPhi = THREE.MathUtils.clamp(
cameraState.targetPhi + step,
0.5,
1.46
);
} else if (event.key === "+" || event.key === "=") {
cameraState.targetRadius = THREE.MathUtils.clamp(
cameraState.targetRadius - 0.8,
8,
29
);
} else if (event.key === "-") {
cameraState.targetRadius = THREE.MathUtils.clamp(
cameraState.targetRadius + 0.8,
8,
29
);
} else {
return;
}
event.preventDefault();
});
renderer.domElement.addEventListener("contextmenu", (event) => {
event.preventDefault();
});
// -------------------------------------------------------------------------
// Resize and render loop
// -------------------------------------------------------------------------
window.addEventListener("resize", () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
});
function animate(now) {
requestAnimationFrame(animate);
cameraState.theta +=
(cameraState.targetTheta - cameraState.theta) * 0.1;
cameraState.phi +=
(cameraState.targetPhi - cameraState.phi) * 0.1;
cameraState.radius +=
(cameraState.targetRadius - cameraState.radius) * 0.1;
updateCamera();
updateExchange(now);
renderer.render(scene, camera);
}
resetScene(false);
requestAnimationFrame(animate);
</script>
</body>
</html>
```