Prompt specification
Build or customize the Three.js 3D scene "One-to-one counting tray" 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: One-to-one counting tray
Category: Algorithms & Systems · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/one-to-one-counting-tray-x1705
Scene Description Prompt:
Please create one self-contained HTML file with Three.js from cdnjs showing one-to-one correspondence in counting. Build a tray of distinct toy fruit pieces beside numbered slots; pair each item with exactly one slot and keep the final count separate from arrangement. Low-poly.
Working Three.js Starter (one-to-one-counting-tray-x1705):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>One-to-One Counting — Fruit Pairing</title>
<style>
:root {
--paper: #f4f0e7;
--card: #fffdf8;
--ink: #183b3d;
--muted: #6d7f7e;
--line: #d8dfd7;
--coral: #ef654c;
--lime: #cbe66c;
--teal: #7fb8ae;
--shadow: 0 18px 50px rgba(31, 58, 57, 0.11);
}
* {
box-sizing: border-box;
}
html,
body {
margin: 0;
min-height: 100%;
}
body {
color: var(--ink);
background:
radial-gradient(circle at 9% 8%, rgba(203, 230, 108, 0.22), transparent 28rem),
radial-gradient(circle at 92% 92%, rgba(127, 184, 174, 0.18), transparent 32rem),
var(--paper);
font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
body::before {
content: "";
position: fixed;
inset: 0;
z-index: -1;
pointer-events: none;
opacity: 0.23;
background-image: radial-gradient(#8ca39d 0.7px, transparent 0.7px);
background-size: 18px 18px;
}
.app {
width: min(1500px, 100%);
margin: 0 auto;
padding: 28px;
}
.topbar {
display: flex;
align-items: center;
justify-content: space-between;
gap: 28px;
margin-bottom: 22px;
}
.brand {
display: flex;
align-items: center;
gap: 16px;
min-width: 0;
}
.brand-mark {
width: 58px;
height: 58px;
flex: 0 0 auto;
display: grid;
place-items: center;
border-radius: 18px;
color: var(--ink);
background: var(--lime);
box-shadow: 0 8px 18px rgba(90, 119, 50, 0.14);
font-size: 29px;
font-weight: 900;
transform: rotate(-4deg);
}
.eyebrow {
margin: 0 0 3px;
color: var(--coral);
font-size: 11px;
font-weight: 850;
letter-spacing: 0.17em;
text-transform: uppercase;
}
h1 {
margin: 0;
font-size: clamp(25px, 3vw, 42px);
line-height: 1.05;
letter-spacing: -0.045em;
}
.subtitle {
margin: 7px 0 0;
color: var(--muted);
font-size: 14px;
line-height: 1.45;
}
.equation {
flex: 0 0 auto;
padding: 12px 16px;
border: 1px solid rgba(24, 59, 61, 0.13);
border-radius: 15px;
background: rgba(255, 253, 248, 0.66);
color: var(--ink);
font-size: 14px;
font-weight: 800;
white-space: nowrap;
}
.equation strong {
color: var(--coral);
}
.layout {
display: grid;
grid-template-columns: minmax(0, 1fr) 310px;
gap: 20px;
align-items: start;
}
.stage-card,
.count-card,
.help-card {
border: 1px solid rgba(24, 59, 61, 0.1);
box-shadow: var(--shadow);
}
.stage-card {
min-width: 0;
overflow: hidden;
border-radius: 26px;
background:
linear-gradient(145deg, rgba(255, 255, 255, 0.8), rgba(239, 242, 231, 0.75)),
var(--card);
}
.stage-top {
min-height: 62px;
display: flex;
align-items: center;
justify-content: space-between;
gap: 16px;
padding: 13px 20px;
border-bottom: 1px solid rgba(24, 59, 61, 0.08);
}
.stage-badge {
display: inline-flex;
align-items: center;
gap: 8px;
color: var(--ink);
font-size: 11px;
font-weight: 900;
letter-spacing: 0.14em;
text-transform: uppercase;
}
.stage-badge::before {
content: "";
width: 9px;
height: 9px;
border-radius: 50%;
background: var(--coral);
box-shadow: 0 0 0 5px rgba(239, 101, 76, 0.12);
}
.stage-instruction {
color: var(--muted);
font-size: 13px;
font-weight: 650;
text-align: right;
}
.scene {
position: relative;
height: clamp(520px, 67vh, 720px);
min-height: 500px;
overflow: hidden;
}
.scene canvas {
display: block;
width: 100%;
height: 100%;
outline: none;
touch-action: none;
}
.tooltip {
position: absolute;
z-index: 5;
pointer-events: none;
padding: 7px 10px;
border-radius: 9px;
color: white;
background: rgba(24, 59, 61, 0.92);
box-shadow: 0 8px 20px rgba(24, 59, 61, 0.18);
font-size: 12px;
font-weight: 800;
opacity: 0;
transform: translate(-50%, calc(-100% - 14px));
transition: opacity 120ms ease;
white-space: nowrap;
}
.tooltip.visible {
opacity: 1;
}
.scene-footer {
display: flex;
align-items: center;
justify-content: space-between;
gap: 16px;
padding: 13px 20px 16px;
border-top: 1px solid rgba(24, 59, 61, 0.08);
}
.fruit-legend {
display: flex;
flex-wrap: wrap;
gap: 8px 15px;
}
.fruit-key {
display: inline-flex;
align-items: center;
gap: 6px;
color: var(--muted);
font-size: 11px;
font-weight: 750;
}
.fruit-dot {
width: 10px;
height: 10px;
border-radius: 50%;
box-shadow: inset 0 -2px 0 rgba(0, 0, 0, 0.08);
}
.status {
max-width: 42%;
color: var(--ink);
font-size: 12px;
font-weight: 700;
text-align: right;
}
aside {
display: grid;
gap: 20px;
}
.count-card {
position: relative;
overflow: hidden;
padding: 24px;
border-radius: 26px;
color: white;
background: var(--ink);
}
.count-card::after {
content: "";
position: absolute;
right: -42px;
top: -46px;
width: 145px;
height: 145px;
border: 24px solid rgba(203, 230, 108, 0.13);
border-radius: 50%;
pointer-events: none;
}
.count-kicker {
position: relative;
z-index: 1;
margin: 0;
color: var(--lime);
font-size: 11px;
font-weight: 900;
letter-spacing: 0.16em;
text-transform: uppercase;
}
.count-main {
position: relative;
z-index: 1;
display: flex;
align-items: baseline;
gap: 8px;
margin: 12px 0 2px;
}
.count-value {
color: var(--lime);
font-size: 88px;
font-weight: 900;
line-height: 0.95;
letter-spacing: -0.08em;
}
.count-total {
color: rgba(255, 255, 255, 0.46);
font-size: 27px;
font-weight: 800;
}
.count-caption {
position: relative;
z-index: 1;
margin: 8px 0 18px;
color: rgba(255, 255, 255, 0.7);
font-size: 13px;
line-height: 1.45;
}
.pair-dots {
position: relative;
z-index: 1;
display: grid;
grid-template-columns: repeat(5, 1fr);
gap: 8px;
}
.pair-dot {
height: 8px;
border-radius: 99px;
background: rgba(255, 255, 255, 0.13);
transition: background 200ms ease, transform 200ms ease;
}
.pair-dot.complete {
background: var(--lime);
transform: scaleY(1.2);
}
.count-message {
position: relative;
z-index: 1;
min-height: 42px;
margin: 18px 0 0;
padding-top: 16px;
border-top: 1px solid rgba(255, 255, 255, 0.13);
color: rgba(255, 255, 255, 0.68);
font-size: 12px;
line-height: 1.55;
}
.count-card.complete {
background: #244b45;
}
.help-card {
padding: 21px;
border-radius: 22px;
background: rgba(255, 253, 248, 0.88);
}
.help-card h2 {
margin: 0 0 12px;
font-size: 16px;
letter-spacing: -0.02em;
}
.rule {
display: flex;
align-items: flex-start;
gap: 10px;
margin: 10px 0;
color: var(--muted);
font-size: 13px;
line-height: 1.45;
}
.rule-number {
width: 22px;
height: 22px;
flex: 0 0 auto;
display: grid;
place-items: center;
border-radius: 7px;
color: var(--ink);
background: rgba(203, 230, 108, 0.62);
font-size: 11px;
font-weight: 900;
}
.reset-button {
width: 100%;
margin-top: 14px;
padding: 12px 16px;
border: 0;
border-radius: 13px;
color: white;
background: var(--coral);
box-shadow: 0 8px 16px rgba(239, 101, 76, 0.2);
font: inherit;
font-size: 13px;
font-weight: 850;
cursor: pointer;
transition: transform 150ms ease, box-shadow 150ms ease;
}
.reset-button:hover {
transform: translateY(-2px);
box-shadow: 0 11px 22px rgba(239, 101, 76, 0.26);
}
.reset-button:active {
transform: translateY(0);
}
.reset-button:focus-visible {
outline: 3px solid rgba(239, 101, 76, 0.3);
outline-offset: 3px;
}
.visually-hidden {
position: absolute;
width: 1px;
height: 1px;
padding: 0;
margin: -1px;
overflow: hidden;
clip: rect(0, 0, 0, 0);
white-space: nowrap;
border: 0;
}
@media (max-width: 1000px) {
.layout {
grid-template-columns: 1fr;
}
aside {
grid-template-columns: minmax(0, 1fr) minmax(0, 1fr);
}
}
@media (max-width: 700px) {
.app {
padding: 14px;
}
.topbar {
align-items: flex-start;
flex-direction: column;
gap: 14px;
}
.equation {
font-size: 12px;
}
.scene {
height: 490px;
min-height: 490px;
}
.stage-instruction {
max-width: 190px;
font-size: 11px;
}
.scene-footer {
align-items: flex-start;
flex-direction: column;
}
.status {
max-width: 100%;
text-align: left;
}
aside {
grid-template-columns: 1fr;
}
}
</style>
</head>
<body>
<div class="app">
<header class="topbar">
<div class="brand">
<div class="brand-mark" aria-hidden="true">5</div>
<div>
<p class="eyebrow">Counting Studio</p>
<h1>One fruit, one slot.</h1>
<p class="subtitle">
Make one-to-one matches by moving every fruit into exactly one numbered slot.
</p>
</div>
</div>
<div class="equation" aria-label="One fruit plus one slot equals one pair">
1 fruit + 1 slot = <strong>1 pair</strong>
</div>
</header>
<main class="layout">
<section class="stage-card" aria-label="Fruit pairing activity">
<div class="stage-top">
<div class="stage-badge">Pairing table</div>
<div class="stage-instruction">
Drag a fruit to a slot · or tap a fruit, then tap a slot
</div>
</div>
<div class="scene" id="scene">
<div class="tooltip" id="tooltip" aria-hidden="true"></div>
</div>
<div class="scene-footer">
<div class="fruit-legend" aria-label="Fruit types">
<span class="fruit-key"><i class="fruit-dot" style="background:#e84d45"></i>Apple</span>
<span class="fruit-key"><i class="fruit-dot" style="background:#f39a32"></i>Orange</span>
<span class="fruit-key"><i class="fruit-dot" style="background:#88ad45"></i>Pear</span>
<span class="fruit-key"><i class="fruit-dot" style="background:#d63d55"></i>Strawberry</span>
<span class="fruit-key"><i class="fruit-dot" style="background:#f2c447"></i>Banana</span>
</div>
<div class="status" id="status" aria-live="polite">
Start with any fruit in the tray.
</div>
</div>
</section>
<aside aria-label="Count and instructions">
<section class="count-card" id="countCard">
<p class="count-kicker" id="countKicker">Count so far</p>
<div class="count-main">
<span class="count-value" id="countValue">0</span>
<span class="count-total">/ 5</span>
</div>
<p class="count-caption" id="countCaption">fruit–slot pairs</p>
<div class="pair-dots" id="pairDots" aria-hidden="true">
<span class="pair-dot"></span>
<span class="pair-dot"></span>
<span class="pair-dot"></span>
<span class="pair-dot"></span>
<span class="pair-dot"></span>
</div>
<p class="count-message" id="countMessage">
The count is separate from the arrangement. It tells how many complete pairs
exist, no matter which slot numbers are used.
</p>
</section>
<section class="help-card">
<h2>How to play</h2>
<div class="rule">
<span class="rule-number">1</span>
<span>Choose one fruit from the tray or an occupied slot.</span>
</div>
<div class="rule">
<span class="rule-number">2</span>
<span>Move it to one numbered slot. A slot can hold only one fruit.</span>
</div>
<div class="rule">
<span class="rule-number">3</span>
<span>Pair all five fruits to make a final count of five.</span>
</div>
<button class="reset-button" id="resetButton" type="button">
Reset the pairs
</button>
</section>
</aside>
</main>
<div class="visually-hidden" id="liveCount" aria-live="polite"></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 sceneElement = document.getElementById("scene");
const tooltip = document.getElementById("tooltip");
const statusElement = document.getElementById("status");
const countCard = document.getElementById("countCard");
const countKicker = document.getElementById("countKicker");
const countValue = document.getElementById("countValue");
const countCaption = document.getElementById("countCaption");
const countMessage = document.getElementById("countMessage");
const pairDots = [...document.querySelectorAll(".pair-dot")];
const liveCount = document.getElementById("liveCount");
const resetButton = document.getElementById("resetButton");
const scene = new THREE.Scene();
const renderer = new THREE.WebGLRenderer({
antialias: true,
alpha: true,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setClearColor(0x000000, 0);
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.05;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.domElement.setAttribute(
"aria-label",
"Low-poly fruit tray beside five numbered matching slots"
);
renderer.domElement.tabIndex = 0;
sceneElement.appendChild(renderer.domElement);
const camera = new THREE.OrthographicCamera(-9, 9, 5, -5, 0.1, 100);
camera.position.set(0.3, 10.5, 12.5);
camera.lookAt(0.3, 0, 0.4);
const hemisphere = new THREE.HemisphereLight(0xffffff, 0x879b82, 2.15);
scene.add(hemisphere);
const keyLight = new THREE.DirectionalLight(0xfff5df, 3.1);
keyLight.position.set(-4.5, 10, 7);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(2048, 2048);
keyLight.shadow.camera.left = -11;
keyLight.shadow.camera.right = 11;
keyLight.shadow.camera.top = 8;
keyLight.shadow.camera.bottom = -8;
keyLight.shadow.camera.near = 1;
keyLight.shadow.camera.far = 30;
keyLight.shadow.normalBias = 0.025;
scene.add(keyLight);
const fillLight = new THREE.DirectionalLight(0xcce7ed, 1.15);
fillLight.position.set(7, 5, -6);
scene.add(fillLight);
const world = new THREE.Group();
scene.add(world);
function standardMaterial(color, options = {}) {
return new THREE.MeshStandardMaterial({
color,
roughness: 0.72,
metalness: 0,
flatShading: true,
...options
});
}
function roundedRectPath(context, x, y, width, height, radius) {
const r = Math.min(radius, width / 2, height / 2);
context.beginPath();
context.moveTo(x + r, y);
context.lineTo(x + width - r, y);
context.quadraticCurveTo(x + width, y, x + width, y + r);
context.lineTo(x + width, y + height - r);
context.quadraticCurveTo(
x + width,
y + height,
x + width - r,
y + height
);
context.lineTo(x + r, y + height);
context.quadraticCurveTo(x, y + height, x, y + height - r);
context.lineTo(x, y + r);
context.quadraticCurveTo(x, y, x + r, y);
context.closePath();
}
function makeRoundedPrism(width, depth, height, radius, material) {
const shape = new THREE.Shape();
const x = -width / 2;
const y = -depth / 2;
const r = radius;
shape.moveTo(x + r, y);
shape.lineTo(x + width - r, y);
shape.quadraticCurveTo(x + width, y, x + width, y + r);
shape.lineTo(x + width, y + depth - r);
shape.quadraticCurveTo(
x + width,
y + depth,
x + width - r,
y + depth
);
shape.lineTo(x + r, y + depth);
shape.quadraticCurveTo(x, y + depth, x, y + depth - r);
shape.lineTo(x, y + r);
shape.quadraticCurveTo(x, y, x + r, y);
const geometry = new THREE.ExtrudeGeometry(shape, {
depth: height,
steps: 1,
bevelEnabled: true,
bevelSegments: 1,
bevelSize: 0.035,
bevelThickness: 0.035,
curveSegments: 3
});
geometry.rotateX(Math.PI / 2);
geometry.translate(0, height / 2, 0);
geometry.computeVertexNormals();
const mesh = new THREE.Mesh(geometry, material);
mesh.castShadow = true;
mesh.receiveShadow = true;
return mesh;
}
function makeTextSprite(text, foreground = "#183b3d", background = "#fffdf8") {
const canvas = document.createElement("canvas");
canvas.width = 512;
canvas.height = 128;
const context = canvas.getContext("2d");
context.clearRect(0, 0, canvas.width, canvas.height);
roundedRectPath(context, 8, 16, 496, 96, 30);
context.fillStyle = background;
context.fill();
context.lineWidth = 7;
context.strokeStyle = "rgba(24,59,61,0.20)";
context.stroke();
context.fillStyle = foreground;
context.font = "900 42px system-ui, sans-serif";
context.textAlign = "center";
context.textBaseline = "middle";
context.fillText(text, 256, 65);
const texture = new THREE.CanvasTexture(canvas);
texture.colorSpace = THREE.SRGBColorSpace;
texture.minFilter = THREE.LinearFilter;
const sprite = new THREE.Sprite(
new THREE.SpriteMaterial({
map: texture,
transparent: true,
depthWrite: false
})
);
sprite.scale.set(2.05, 0.51, 1);
return sprite;
}
function makeNumberSprite(number) {
const canvas = document.createElement("canvas");
canvas.width = 256;
canvas.height = 256;
const context = canvas.getContext("2d");
context.clearRect(0, 0, 256, 256);
roundedRectPath(context, 18, 18, 220, 220, 56);
context.fillStyle = "#183b3d";
context.fill();
context.lineWidth = 12;
context.strokeStyle = "#cbe66c";
context.stroke();
context.fillStyle = "#ffffff";
context.font = "900 132px system-ui, sans-serif";
context.textAlign = "center";
context.textBaseline = "middle";
context.fillText(String(number), 128, 133);
const texture = new THREE.CanvasTexture(canvas);
texture.colorSpace = THREE.SRGBColorSpace;
texture.minFilter = THREE.LinearFilter;
const sprite = new THREE.Sprite(
new THREE.SpriteMaterial({
map: texture,
transparent: true,
depthWrite: false
})
);
sprite.scale.set(0.64, 0.64, 1);
return sprite;
}
// Board and tray.
const boardUnderlay = makeRoundedPrism(
16.45,
6.35,
0.2,
0.42,
standardMaterial("#9eb8b0")
);
boardUnderlay.position.set(0.3, -0.22, 0.4);
world.add(boardUnderlay);
const board = makeRoundedPrism(
16.2,
6.1,
0.24,
0.4,
standardMaterial("#e4ece2")
);
board.position.set(0.3, -0.04, 0.4);
world.add(board);
const tray = makeRoundedPrism(
5.0,
3.55,
0.24,
0.35,
standardMaterial("#cf9259")
);
tray.position.set(-4.55, 0.17, 0.25);
world.add(tray);
const trayInset = makeRoundedPrism(
4.65,
3.2,
0.045,
0.28,
standardMaterial("#f0cf98")
);
trayInset.position.set(-4.55, 0.305, 0.25);
world.add(trayInset);
const trayLabel = makeTextSprite("FRUIT TRAY");
trayLabel.position.set(-4.55, 0.42, -1.72);
world.add(trayLabel);
const slotLabel = makeTextSprite("NUMBER SLOTS");
slotLabel.position.set(4.3, 0.42, -1.72);
world.add(slotLabel);
const arrowMaterial = standardMaterial("#6aa59e");
const arrowShaft = new THREE.Mesh(
new THREE.BoxGeometry(0.62, 0.075, 0.075),
arrowMaterial
);
arrowShaft.position.set(-1.35, 0.27, 0.45);
arrowShaft.castShadow = true;
world.add(arrowShaft);
const arrowHead = new THREE.Mesh(
new THREE.ConeGeometry(0.18, 0.38, 3),
arrowMaterial
);
arrowHead.position.set(-0.86, 0.27, 0.45);
arrowHead.rotation.z = -Math.PI / 2;
arrowHead.castShadow = true;
world.add(arrowHead);
const fruits = [];
const fruitPickMeshes = [];
const slots = [];
const slotHitMeshes = [];
const appleHome = new THREE.Vector3(-5.85, 0.34, -0.58);
const orangeHome = new THREE.Vector3(-4.55, 0.34, -0.58);
const pearHome = new THREE.Vector3(-3.25, 0.34, -0.58);
const strawberryHome = new THREE.Vector3(-5.2, 0.34, 0.88);
const bananaHome = new THREE.Vector3(-3.62, 0.34, 0.88);
const recessMaterial = standardMaterial("#e5b97d");
for (const position of [
appleHome,
orangeHome,
pearHome,
strawberryHome,
bananaHome
]) {
const recess = new THREE.Mesh(
new THREE.CylinderGeometry(0.59, 0.59, 0.035, 20),
recessMaterial
);
recess.position.set(position.x, 0.335, position.z);
recess.receiveShadow = true;
world.add(recess);
}
function makeFruit(id, name, group, home) {
group.position.copy(home);
world.add(group);
group.traverse((object) => {
if (object.isMesh) {
object.userData.fruitId = id;
object.castShadow = true;
object.receiveShadow = true;
fruitPickMeshes.push(object);
}
});
fruits.push({
id,
name,
group,
home: home.clone(),
slotIndex: null,
tween: null
});
}
// Apple.
{
const group = new THREE.Group();
const appleMaterial = standardMaterial("#e84d45");
const body = new THREE.Mesh(
new THREE.IcosahedronGeometry(0.44, 1),
appleMaterial
);
body.position.y = 0.42;
body.scale.set(1, 0.96, 0.96);
group.add(body);
const dimple = new THREE.Mesh(
new THREE.IcosahedronGeometry(0.17, 1),
standardMaterial("#cb3936")
);
dimple.position.set(0, 0.78, 0);
group.add(dimple);
const stem = new THREE.Mesh(
new THREE.CylinderGeometry(0.035, 0.055, 0.22, 5),
standardMaterial("#70422d")
);
stem.position.y = 0.88;
stem.rotation.z = -0.13;
group.add(stem);
const leaf = new THREE.Mesh(
new THREE.SphereGeometry(0.13, 5, 3),
standardMaterial("#68a84c")
);
leaf.position.set(0.12, 0.91, 0);
leaf.scale.set(1.6, 0.22, 0.75);
leaf.rotation.z = 0.28;
group.add(leaf);
makeFruit("apple", "Apple", group, appleHome);
}
// Orange.
{
const group = new THREE.Group();
const body = new THREE.Mesh(
new THREE.IcosahedronGeometry(0.43, 1),
standardMaterial("#f39a32")
);
body.position.y = 0.42;
body.scale.y = 0.98;
group.add(body);
const stem = new THREE.Mesh(
new THREE.CylinderGeometry(0.035, 0.045, 0.12, 5),
standardMaterial("#79502c")
);
stem.position.y = 0.83;
group.add(stem);
const leaf = new THREE.Mesh(
new THREE.SphereGeometry(0.11, 5, 3),
standardMaterial("#5f9e43")
);
leaf.position.set(0.11, 0.83, 0);
leaf.scale.set(1.5, 0.23, 0.75);
leaf.rotation.z = 0.35;
group.add(leaf);
makeFruit("orange", "Orange", group, orangeHome);
}
// Pear.
{
const group = new THREE.Group();
const pearPoints = [
new THREE.Vector2(0, 0),
new THREE.Vector2(0.31, 0.08),
new THREE.Vector2(0.44, 0.29),
new THREE.Vector2(0.38, 0.55),
new THREE.Vector2(0.27, 0.81),
new THREE.Vector2(0.17, 1.02),
new THREE.Vector2(0, 1.08)
];
const body = new THREE.Mesh(
new THREE.LatheGeometry(pearPoints, 8),
standardMaterial("#88ad45")
);
group.add(body);
const stem = new THREE.Mesh(
new THREE.CylinderGeometry(0.032, 0.05, 0.24, 5),
standardMaterial("#70502e")
);
stem.position.y = 1.16;
stem.rotation.z = -0.14;
group.add(stem);
const leaf = new THREE.Mesh(
new THREE.SphereGeometry(0.12, 5, 3),
standardMaterial("#5e9142")
);
leaf.position.set(0.11, 1.15, 0);
leaf.scale.set(1.65, 0.22, 0.75);
leaf.rotation.z = 0.3;
group.add(leaf);
makeFruit("pear", "Pear", group, pearHome);
}
// Strawberry.
{
const group = new THREE.Group();
const body = new THREE.Mesh(
new THREE.CylinderGeometry(0.34, 0.045, 0.68, 7, 1),
standardMaterial("#d63d55")
);
body.position.y = 0.35;
body.scale.z = 0.86;
group.add(body);
const crown = new THREE.Group();
crown.position.y = 0.7;
for (let index = 0; index < 5; index += 1) {
const leaf = new THREE.Mesh(
new THREE.ConeGeometry(0.11, 0.27, 3),
standardMaterial("#5b9a43")
);
const angle = (index / 5) * Math.PI * 2;
leaf.position.set(Math.cos(angle) * 0.13, 0.02, Math.sin(angle) * 0.13);
leaf.rotation.z = Math.cos(angle) * 0.75;
leaf.rotation.x = Math.sin(angle) * 0.75;
crown.add(leaf);
}
group.add(crown);
const seedMaterial = standardMaterial("#ffd66b");
const seedGeometry = new THREE.IcosahedronGeometry(0.026, 0);
const seedLocations = [
[-0.19, 0.53, 0.16],
[0.02, 0.59, 0.18],
[0.2, 0.49, 0.13],
[-0.24, 0.36, 0.12],
[-0.04, 0.39, 0.21],
[0.16, 0.32, 0.16],
[-0.1, 0.2, 0.13],
[0.05, 0.15, 0.08]
];
for (const [x, y, z] of seedLocations) {
const seed = new THREE.Mesh(seedGeometry, seedMaterial);
seed.position.set(x, y, z);
group.add(seed);
}
makeFruit("strawberry", "Strawberry", group, strawberryHome);
}
// Banana.
{
const group = new THREE.Group();
const curve = new THREE.CatmullRomCurve3([
new THREE.Vector3(-0.55, 0.13, 0),
new THREE.Vector3(-0.32, 0.47, 0.03),
new THREE.Vector3(0.13, 0.58, -0.02),
new THREE.Vector3(0.53, 0.24, 0)
]);
const body = new THREE.Mesh(
new THREE.TubeGeometry(curve, 11, 0.135, 5, false),
standardMaterial("#f2c447")
);
group.add(body);
const end = new THREE.Mesh(
new THREE.IcosahedronGeometry(0.09, 0),
standardMaterial("#72502d")
);
end.position.set(-0.55, 0.13, 0);
end.scale.set(0.8, 1.1, 0.9);
group.add(end);
group.rotation.y = -0.12;
makeFruit("banana", "Banana", group, bananaHome);
}
// Numbered slots.
const slotXPositions = [0.8, 2.55, 4.3, 6.05, 7.8];
const slotMaterial = standardMaterial("#79aea8");
const wellMaterial = standardMaterial("#d7ece5");
slotXPositions.forEach((x, index) => {
const z = 0.45;
const base = makeRoundedPrism(
1.04,
1.16,
0.16,
0.22,
slotMaterial
);
base.position.set(x, 0.19, z);
base.userData.slotIndex = index;
world.add(base);
slotHitMeshes.push(base);
const well = new THREE.Mesh(
new THREE.CylinderGeometry(0.45, 0.45, 0.055, 20),
wellMaterial
);
well.position.set(x, 0.295, z + 0.08);
well.userData.slotIndex = index;
world.add(well);
slotHitMeshes.push(well);
const haloMaterial = new THREE.MeshBasicMaterial({
color: "#ef654c",
transparent: true,
opacity: 0.9,
depthWrite: false,
side: THREE.DoubleSide
});
const halo = new THREE.Mesh(
new THREE.TorusGeometry(0.51, 0.035, 4, 24),
haloMaterial
);
halo.position.set(x, 0.37, z + 0.08);
halo.rotation.x = Math.PI / 2;
halo.visible = false;
world.add(halo);
const pole = new THREE.Mesh(
new THREE.CylinderGeometry(0.035, 0.045, 0.48, 6),
standardMaterial("#426f6b")
);
pole.position.set(x, 0.49, z - 0.48);
pole.castShadow = true;
world.add(pole);
const number = makeNumberSprite(index + 1);
number.position.set(x, 0.96, z - 0.48);
world.add(number);
slots.push({
index,
x,
z,
base,
well,
halo,
position: new THREE.Vector3(x, 0.34, z + 0.08)
});
});
// Selection ring.
const selectionMaterial = new THREE.MeshBasicMaterial({
color: "#ef654c",
transparent: true,
opacity: 0.86,
depthWrite: false,
side: THREE.DoubleSide
});
const selectionRing = new THREE.Mesh(
new THREE.TorusGeometry(0.61, 0.035, 4, 28),
selectionMaterial
);
selectionRing.rotation.x = Math.PI / 2;
selectionRing.visible = false;
world.add(selectionRing);
const raycaster = new THREE.Raycaster();
const pointer = new THREE.Vector2();
const dragPlane = new THREE.Plane(new THREE.Vector3(0, 1, 0), -0.34);
let selectedFruit = null;
let dragState = null;
let hoveredSlot = -1;
function setStatus(message) {
statusElement.textContent = message;
}
function updatePointer(event) {
const rect = renderer.domElement.getBoundingClientRect();
pointer.x = ((event.clientX - rect.left) / rect.width) * 2 - 1;
pointer.y = -((event.clientY - rect.top) / rect.height) * 2 + 1;
raycaster.setFromCamera(pointer, camera);
}
function pickFruit(event) {
updatePointer(event);
const hits = raycaster.intersectObjects(fruitPickMeshes, false);
return hits.length ? hits[0] : null;
}
function pickSlot(event) {
updatePointer(event);
const hits = raycaster.intersectObjects(slotHitMeshes, false);
return hits.length ? hits[0] : null;
}
function findFruit(id) {
return fruits.find((fruit) => fruit.id === id);
}
function moveFruitTo(fruit, target, duration = 480, delay = 0) {
fruit.tween = null;
const now = performance.now();
fruit.tween = {
start: now + delay,
duration,
from: fruit.group.position.clone(),
to: target.clone()
};
}
function setHoveredSlot(index) {
if (hoveredSlot === index) return;
if (hoveredSlot >= 0) {
slots[hoveredSlot].halo.visible = false;
}
hoveredSlot = index;
if (hoveredSlot >= 0) {
slots[hoveredSlot].halo.visible = true;
}
}
function updateCount(announce = false) {
// This count is based only on complete fruit–slot pairs.
const count = fruits.filter((fruit) => fruit.slotIndex !== null).length;
const complete = count === fruits.length;
countValue.textContent = String(count);
countCaption.textContent = count === 1
? "fruit–slot pair"
: "fruit–slot pairs";
pairDots.forEach((dot, index) => {
dot.classList.toggle("complete", index < count);
});
countCard.classList.toggle("complete", complete);
countKicker.textContent = complete ? "Final count" : "Count so far";
if (complete) {
countMessage.textContent =
"All five fruits have exactly one slot. The final count is 5, regardless of the order of the numbered slots.";
} else {
countMessage.textContent =
"The count is separate from the arrangement. It tells how many complete pairs exist, no matter which slot numbers are used.";
}
if (announce) {
liveCount.textContent = `${count} fruit–slot pair${count === 1 ? "" : "s"}.`;
}
}
function assignFruit(fruit, slotIndex) {
const targetSlot = slots[slotIndex];
const previousSlot = fruit.slotIndex;
const previousOccupant = fruits.find(
(other) => other.id !== fruit.id && other.slotIndex === slotIndex
);
fruit.slotIndex = slotIndex;
selectedFruit = null;
if (previousOccupant) {
previousOccupant.slotIndex = null;
moveFruitTo(previousOccupant, previousOccupant.home, 430);
}
moveFruitTo(fruit, targetSlot.position, 470);
if (previousOccupant) {
setStatus(
`${fruit.name} moved to slot ${slotIndex + 1}; ${previousOccupant.name} returned to the tray.`
);
} else if (previousSlot === null) {
setStatus(`${fruit.name} is now paired with slot ${slotIndex + 1}.`);
} else {
setStatus(`${fruit.name} moved from slot ${previousSlot + 1} to slot ${slotIndex + 1}.`);
}
updateCount(true);
}
renderer.domElement.addEventListener("pointerdown", (event) => {
const fruitHit = pickFruit(event);
if (fruitHit) {
const fruit = findFruit(fruitHit.object.userData.fruitId);
selectedFruit = fruit;
dragState = {
pointerId: event.pointerId,
fruit,
startX: event.clientX,
startY: event.clientY,
moved: false,
offsetX: 0,
offsetZ: 0
};
const planePoint = new THREE.Vector3();
if (raycaster.ray.intersectPlane(dragPlane, planePoint)) {
dragState.offsetX = fruit.group.position.x - planePoint.x;
dragState.offsetZ = fruit.group.position.z - planePoint.z;
}
renderer.domElement.setPointerCapture(event.pointerId);
event.preventDefault();
return;
}
const slotHit = pickSlot(event);
if (slotHit) {
const slotIndex = slotHit.object.userData.slotIndex;
if (selectedFruit) {
assignFruit(selectedFruit, slotIndex);
} else {
const occupant = fruits.find((fruit) => fruit.slotIndex === slotIndex);
if (occupant) {
selectedFruit = occupant;
setStatus(`${occupant.name} selected. Choose another slot to move it.`);
} else {
setStatus("Choose a fruit first, then choose a slot.");
}
}
event.preventDefault();
return;
}
selectedFruit = null;
});
renderer.domElement.addEventListener("pointermove", (event) => {
if (dragState && dragState.pointerId === event.pointerId) {
const distance = Math.hypot(
event.clientX - dragState.startX,
event.clientY - dragState.startY
);
if (!dragState.moved && distance > 5) {
dragState.moved = true;
dragState.fruit.tween = null;
}
if (dragState.moved) {
updatePointer(event);
const planePoint = new THREE.Vector3();
if (raycaster.ray.intersectPlane(dragPlane, planePoint)) {
const fruit = dragState.fruit;
const x = THREE.MathUtils.clamp(
planePoint.x + dragState.offsetX,
-7.1,
8.2
);
const z = THREE.MathUtils.clamp(
planePoint.z + dragState.offsetZ,
-2.0,
2.65
);
fruit.group.position.set(x, 1.02, z);
let closestSlot = -1;
let closestDistance = Infinity;
slots.forEach((slot) => {
const slotDistance = Math.hypot(x - slot.x, z - slot.position.z);
if (slotDistance < 0.72 && slotDistance < closestDistance) {
closestDistance = slotDistance;
closestSlot = slot.index;
}
});
setHoveredSlot(closestSlot);
}
}
tooltip.classList.remove("visible");
event.preventDefault();
return;
}
const hit = pickFruit(event);
const stageRect = sceneElement.getBoundingClientRect();
if (hit) {
const fruit = findFruit(hit.object.userData.fruitId);
tooltip.textContent = `${fruit.name} · drag to a slot`;
tooltip.style.left = `${event.clientX - stageRect.left}px`;
tooltip.style.top = `${event.clientY - stageRect.top}px`;
tooltip.classList.add("visible");
renderer.domElement.style.cursor = "grab";
} else {
tooltip.classList.remove("visible");
renderer.domElement.style.cursor = "default";
}
});
renderer.domElement.addEventListener("pointerup", (event) => {
if (!dragState || dragState.pointerId !== event.pointerId) return;
const fruit = dragState.fruit;
if (dragState.moved) {
let targetSlot = -1;
let targetDistance = Infinity;
slots.forEach((slot) => {
const distance = Math.hypot(
fruit.group.position.x - slot.x,
fruit.group.position.z - slot.position.z
);
if (distance < 0.72 && distance < targetDistance) {
targetDistance = distance;
targetSlot = slot.index;
}
});
if (targetSlot >= 0) {
assignFruit(fruit, targetSlot);
} else {
const returnPosition = fruit.slotIndex !== null
? slots[fruit.slotIndex].position
: fruit.home;
moveFruitTo(fruit, returnPosition, 360);
selectedFruit = fruit;
setStatus(`${fruit.name} stayed in place. Drop it closer to a slot to make a pair.`);
}
} else {
selectedFruit = fruit;
setStatus(`${fruit.name} selected. Now choose a numbered slot.`);
}
dragState = null;
setHoveredSlot(-1);
if (renderer.domElement.hasPointerCapture(event.pointerId)) {
renderer.domElement.releasePointerCapture(event.pointerId);
}
});
renderer.domElement.addEventListener("pointercancel", () => {
if (dragState) {
const fruit = dragState.fruit;
const returnPosition = fruit.slotIndex !== null
? slots[fruit.slotIndex].position
: fruit.home;
moveFruitTo(fruit, returnPosition, 300);
}
dragState = null;
setHoveredSlot(-1);
});
renderer.domElement.addEventListener("pointerleave", () => {
if (!dragState) {
tooltip.classList.remove("visible");
renderer.domElement.style.cursor = "default";
}
});
resetButton.addEventListener("click", () => {
selectedFruit = null;
setHoveredSlot(-1);
fruits.forEach((fruit, index) => {
fruit.slotIndex = null;
moveFruitTo(fruit, fruit.home, 500, index * 55);
});
setStatus("All fruits returned to the tray. The count is back to zero.");
updateCount(true);
});
function resize() {
const width = sceneElement.clientWidth;
const height = sceneElement.clientHeight;
const aspect = width / Math.max(height, 1);
const viewHeight = Math.max(9.4, 17.8 / aspect);
camera.left = (-viewHeight * aspect) / 2;
camera.right = (viewHeight * aspect) / 2;
camera.top = viewHeight / 2;
camera.bottom = -viewHeight / 2;
camera.updateProjectionMatrix();
renderer.setSize(width, height, false);
}
const resizeObserver = new ResizeObserver(resize);
resizeObserver.observe(sceneElement);
resize();
const clock = new THREE.Clock();
renderer.setAnimationLoop(() => {
const now = performance.now();
const elapsed = clock.getElapsedTime();
for (const fruit of fruits) {
if (!fruit.tween) continue;
const tween = fruit.tween;
if (now < tween.start) continue;
const progress = THREE.MathUtils.clamp(
(now - tween.start) / tween.duration,
0,
1
);
const eased = 1 - Math.pow(1 - progress, 3);
fruit.group.position.lerpVectors(tween.from, tween.to, eased);
if (progress >= 1) {
fruit.group.position.copy(tween.to);
fruit.tween = null;
}
}
if (selectedFruit) {
selectionRing.visible = true;
selectionRing.position.set(
selectedFruit.group.position.x,
0.385,
selectedFruit.group.position.z
);
const pulse = 1 + Math.sin(elapsed * 4.5) * 0.055;
selectionRing.scale.set(pulse, pulse, pulse);
selectionMaterial.opacity = 0.7 + Math.sin(elapsed * 4.5) * 0.12;
} else {
selectionRing.visible = false;
}
for (const slot of slots) {
if (slot.halo.visible) {
const pulse = 1 + Math.sin(elapsed * 6) * 0.05;
slot.halo.scale.set(pulse, pulse, pulse);
}
}
renderer.render(scene, camera);
});
updateCount();
</script>
</body>
</html>
```