Prompt specification
Build or customize the Three.js 3D scene "Different slice counts same area" 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: Different slice counts same area
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/different-slice-counts-same-area-x0134
Scene Description Prompt:
Make a standalone HTML scene with Three.js from cdnjs that depicts pizza portions and fractions. Place identical pizzas cut into four and eight pieces; move matching total-area portions onto plates so users can compare equivalent fractions through visible geometry. Voxel style.
Working Three.js Starter (different-slice-counts-same-area-x0134):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Pixel Pizza Fractions</title>
<style>
:root {
color-scheme: dark;
--bg: #101827;
--panel: rgba(18, 28, 45, 0.88);
--border: rgba(255, 255, 255, 0.13);
--text: #f6f8fc;
--muted: #aebbd0;
--yellow: #ffd45b;
--cyan: #66e5d2;
--red: #ff786b;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: var(--bg);
font-family: Inter, ui-sans-serif, system-ui, -apple-system,
BlinkMacSystemFont, "Segoe UI", sans-serif;
color: var(--text);
}
#scene {
position: fixed;
inset: 0;
}
canvas {
display: block;
touch-action: none;
}
.hud {
position: fixed;
inset: 0;
pointer-events: none;
}
.panel {
position: absolute;
top: 14px;
left: 50%;
width: min(810px, calc(100vw - 24px));
transform: translateX(-50%);
padding: 14px;
border: 1px solid var(--border);
border-radius: 18px;
background: var(--panel);
box-shadow: 0 18px 60px rgba(0, 0, 0, 0.35);
backdrop-filter: blur(14px);
pointer-events: auto;
}
.panel-head {
display: flex;
align-items: center;
justify-content: space-between;
gap: 14px;
margin-bottom: 10px;
}
h1 {
margin: 0;
font-size: clamp(18px, 2vw, 25px);
line-height: 1;
letter-spacing: 0.07em;
}
.subtitle {
margin-top: 5px;
color: var(--muted);
font-size: 12px;
}
.legend {
display: flex;
gap: 13px;
flex: 0 0 auto;
font-size: 11px;
color: var(--muted);
}
.legend span::before {
content: "";
display: inline-block;
width: 9px;
height: 9px;
margin-right: 5px;
border-radius: 2px;
vertical-align: -1px;
}
.legend .a::before {
background: var(--yellow);
}
.legend .b::before {
background: var(--cyan);
}
#fractionGrid {
display: grid;
grid-template-columns: repeat(4, 1fr);
gap: 8px;
}
.fraction-card {
min-height: 64px;
padding: 8px 6px;
border: 1px solid var(--border);
border-radius: 12px;
background: rgba(255, 255, 255, 0.045);
color: var(--text);
cursor: pointer;
transition:
transform 150ms ease,
border-color 150ms ease,
background 150ms ease;
}
.fraction-card:hover {
transform: translateY(-2px);
border-color: rgba(255, 255, 255, 0.35);
background: rgba(255, 255, 255, 0.075);
}
.fraction-card.active {
border-color: var(--yellow);
background: rgba(255, 212, 91, 0.12);
box-shadow: inset 0 0 0 1px rgba(255, 212, 91, 0.18);
}
.fraction-pair {
display: flex;
align-items: center;
justify-content: center;
gap: 9px;
font-size: 20px;
font-weight: 800;
font-variant-numeric: tabular-nums;
}
.fraction-pair .equals {
color: var(--muted);
font-size: 16px;
}
.fraction-card small {
display: block;
margin-top: 4px;
color: var(--muted);
font-size: 10px;
}
.actions {
display: flex;
align-items: center;
gap: 8px;
margin-top: 10px;
}
button {
border: 0;
border-radius: 10px;
padding: 9px 13px;
font: inherit;
font-size: 12px;
font-weight: 750;
cursor: pointer;
}
.primary {
background: var(--yellow);
color: #2b2107;
}
.secondary {
background: rgba(255, 255, 255, 0.09);
color: var(--text);
border: 1px solid var(--border);
}
button:hover {
filter: brightness(1.08);
}
#status {
flex: 1;
min-width: 0;
color: var(--muted);
font-size: 11px;
line-height: 1.35;
}
.help {
position: absolute;
right: 14px;
bottom: 13px;
padding: 8px 11px;
border: 1px solid var(--border);
border-radius: 10px;
background: rgba(15, 24, 39, 0.72);
color: var(--muted);
font-size: 11px;
backdrop-filter: blur(8px);
}
#loading {
position: fixed;
inset: 0;
display: grid;
place-items: center;
z-index: 10;
background: #101827;
color: var(--yellow);
font-weight: 800;
letter-spacing: 0.12em;
transition: opacity 250ms ease;
}
#loading.hidden {
opacity: 0;
pointer-events: none;
}
@media (max-width: 700px) {
.panel {
top: 8px;
padding: 10px;
border-radius: 14px;
}
.legend {
display: none;
}
#fractionGrid {
grid-template-columns: repeat(2, 1fr);
}
.fraction-card {
min-height: 54px;
padding: 5px;
}
.fraction-pair {
font-size: 17px;
}
.actions {
flex-wrap: wrap;
}
#status {
order: 3;
flex-basis: 100%;
}
.help {
left: 10px;
right: auto;
bottom: 10px;
font-size: 10px;
}
}
</style>
</head>
<body>
<div id="scene"></div>
<div class="hud">
<section class="panel" aria-label="Fraction comparison controls">
<div class="panel-head">
<div>
<h1>PIXEL PIZZA LAB</h1>
<div class="subtitle">
Equal pizza area, different ways of cutting it
</div>
</div>
<div class="legend">
<span class="a">4-cut pizza</span>
<span class="b">8-cut pizza</span>
</div>
</div>
<div id="fractionGrid">
<button
class="fraction-card active"
data-key="quarter"
data-left="1"
data-right="2"
>
<span class="fraction-pair">
<span>1/4</span>
<span class="equals">=</span>
<span>2/8</span>
</span>
<small>one quarter = two eighths</small>
</button>
<button
class="fraction-card"
data-key="half"
data-left="2"
data-right="4"
>
<span class="fraction-pair">
<span>2/4</span>
<span class="equals">=</span>
<span>4/8</span>
</span>
<small>two quarters = four eighths</small>
</button>
<button
class="fraction-card"
data-key="threeQuarters"
data-left="3"
data-right="6"
>
<span class="fraction-pair">
<span>3/4</span>
<span class="equals">=</span>
<span>6/8</span>
</span>
<small>three quarters = six eighths</small>
</button>
<button
class="fraction-card"
data-key="whole"
data-left="4"
data-right="8"
>
<span class="fraction-pair">
<span>4/4</span>
<span class="equals">=</span>
<span>8/8</span>
</span>
<small>the complete pizza = every piece</small>
</button>
</div>
<div class="actions">
<button id="moveButton" class="primary">
Move matching portions
</button>
<button id="resetButton" class="secondary">
Reset pizzas
</button>
<button id="viewButton" class="secondary">
Reset view
</button>
<div id="status" aria-live="polite">
Choose a fraction pair to send equal-area portions to the plates.
</div>
</div>
</section>
<div class="help">
Drag to orbit · Scroll or pinch to zoom
</div>
</div>
<div id="loading">BUILDING PIXEL PIZZAS…</div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
const host = document.getElementById("scene");
const status = document.getElementById("status");
const moveButton = document.getElementById("moveButton");
const resetButton = document.getElementById("resetButton");
const viewButton = document.getElementById("viewButton");
const fractionCards = [
...document.querySelectorAll(".fraction-card")
];
// ------------------------------------------------------------
// Renderer and scene
// ------------------------------------------------------------
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x101827);
scene.fog = new THREE.Fog(0x101827, 24, 42);
const camera = new THREE.PerspectiveCamera(
42,
window.innerWidth / window.innerHeight,
0.1,
100
);
let renderer;
try {
renderer = new THREE.WebGLRenderer({
antialias: true,
powerPreference: "high-performance"
});
} catch (error) {
document.getElementById("loading").textContent =
"WEBGL IS NOT AVAILABLE IN THIS BROWSER";
throw error;
}
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",
"Interactive voxel pizza fraction comparison"
);
host.appendChild(renderer.domElement);
const hemisphere = new THREE.HemisphereLight(
0xb9d8ff,
0x342514,
1.9
);
scene.add(hemisphere);
const sun = new THREE.DirectionalLight(0xfff1cf, 3.4);
sun.position.set(6, 13, 9);
sun.castShadow = true;
sun.shadow.mapSize.set(2048, 2048);
sun.shadow.camera.left = -14;
sun.shadow.camera.right = 14;
sun.shadow.camera.top = 14;
sun.shadow.camera.bottom = -14;
sun.shadow.camera.near = 1;
sun.shadow.camera.far = 35;
sun.shadow.bias = -0.0003;
scene.add(sun);
const rimLight = new THREE.DirectionalLight(0x66dfff, 1.3);
rimLight.position.set(-9, 7, -7);
scene.add(rimLight);
const groundMaterial = new THREE.MeshStandardMaterial({
color: 0x172438,
roughness: 0.96,
metalness: 0.02
});
const ground = new THREE.Mesh(
new THREE.PlaneGeometry(46, 46),
groundMaterial
);
ground.rotation.x = -Math.PI / 2;
ground.receiveShadow = true;
scene.add(ground);
const grid = new THREE.GridHelper(
36,
36,
0x3a526d,
0x24374d
);
grid.position.y = 0.012;
grid.material.transparent = true;
grid.material.opacity = 0.38;
scene.add(grid);
// ------------------------------------------------------------
// Direct camera controls: no external controls module
// ------------------------------------------------------------
const view = {
azimuth: 0.22,
polar: 0.57,
distance: 20,
targetAzimuth: 0.22,
targetPolar: 0.57,
targetDistance: 20,
target: new THREE.Vector3(0, 1.45, 1.05)
};
function portraitLayout() {
return window.innerWidth / window.innerHeight < 0.8;
}
function resetView() {
view.targetAzimuth = 0.22;
view.targetPolar = 0.57;
view.targetDistance = portraitLayout() ? 25 : 20;
camera.fov = portraitLayout() ? 57 : 42;
camera.updateProjectionMatrix();
}
resetView();
let dragging = false;
let pointerX = 0;
let pointerY = 0;
renderer.domElement.addEventListener("pointerdown", (event) => {
if (event.button !== 0) return;
dragging = true;
pointerX = event.clientX;
pointerY = event.clientY;
renderer.domElement.setPointerCapture(event.pointerId);
});
renderer.domElement.addEventListener("pointermove", (event) => {
if (!dragging) return;
const dx = event.clientX - pointerX;
const dy = event.clientY - pointerY;
pointerX = event.clientX;
pointerY = event.clientY;
view.targetAzimuth -= dx * 0.006;
view.targetPolar = THREE.MathUtils.clamp(
view.targetPolar + dy * 0.005,
0.27,
1.15
);
});
function endDrag(event) {
dragging = false;
if (
event.pointerId !== undefined &&
renderer.domElement.hasPointerCapture(event.pointerId)
) {
renderer.domElement.releasePointerCapture(event.pointerId);
}
}
renderer.domElement.addEventListener("pointerup", endDrag);
renderer.domElement.addEventListener("pointercancel", endDrag);
renderer.domElement.addEventListener(
"wheel",
(event) => {
event.preventDefault();
view.targetDistance = THREE.MathUtils.clamp(
view.targetDistance + event.deltaY * 0.012,
11,
31
);
},
{ passive: false }
);
// ------------------------------------------------------------
// Text labels
// ------------------------------------------------------------
function roundedRect(ctx, x, y, w, h, r) {
ctx.beginPath();
ctx.moveTo(x + r, y);
ctx.arcTo(x + w, y, x + w, y + h, r);
ctx.arcTo(x + w, y + h, x, y + h, r);
ctx.arcTo(x, y + h, x, y, r);
ctx.arcTo(x, y, x + w, y, r);
ctx.closePath();
}
function makeLabel(text, accent = "#ffd45b", scale = [3.4, 0.85]) {
const canvas = document.createElement("canvas");
canvas.width = 768;
canvas.height = 192;
const ctx = canvas.getContext("2d");
const texture = new THREE.CanvasTexture(canvas);
texture.colorSpace = THREE.SRGBColorSpace;
const material = new THREE.SpriteMaterial({
map: texture,
transparent: true,
depthTest: false,
depthWrite: false
});
const sprite = new THREE.Sprite(material);
sprite.scale.set(scale[0], scale[1], 1);
sprite.renderOrder = 20;
sprite.userData.canvas = canvas;
sprite.userData.ctx = ctx;
sprite.userData.texture = texture;
sprite.userData.accent = accent;
paintLabel(sprite, text, accent);
return sprite;
}
function paintLabel(sprite, text, accent = sprite.userData.accent) {
const canvas = sprite.userData.canvas;
const ctx = sprite.userData.ctx;
ctx.clearRect(0, 0, canvas.width, canvas.height);
roundedRect(ctx, 12, 12, canvas.width - 24, canvas.height - 24, 32);
ctx.fillStyle = "rgba(12, 20, 34, 0.88)";
ctx.fill();
ctx.lineWidth = 7;
ctx.strokeStyle = accent;
ctx.stroke();
ctx.fillStyle = "#ffffff";
ctx.font = "800 64px system-ui, sans-serif";
ctx.textAlign = "center";
ctx.textBaseline = "middle";
ctx.fillText(text, canvas.width / 2, canvas.height / 2 + 2);
sprite.userData.texture.needsUpdate = true;
}
const labelA = makeLabel("PIZZA A · 4 CUTS", "#ffd45b", [3.6, 0.9]);
labelA.position.set(-4.1, 4.05, -1.25);
scene.add(labelA);
const labelB = makeLabel("PIZZA B · 8 CUTS", "#66e5d2", [3.6, 0.9]);
labelB.position.set(4.1, 4.05, -1.25);
scene.add(labelB);
const plateLabelA = makeLabel(
"PORTION PLATE",
"#ffd45b",
[2.9, 0.73]
);
plateLabelA.position.set(-4.1, 0.63, 5.45);
scene.add(plateLabelA);
const plateLabelB = makeLabel(
"PORTION PLATE",
"#66e5d2",
[2.9, 0.73]
);
plateLabelB.position.set(4.1, 0.63, 5.45);
scene.add(plateLabelB);
const equalityLabel = makeLabel(
"VS",
"#ffffff",
[2.5, 0.72]
);
equalityLabel.position.set(0, 1.25, 4.15);
scene.add(equalityLabel);
// ------------------------------------------------------------
// Pizza construction
//
// Both pizzas use the same 32-segment polygon. A quarter uses
// eight polygon edges and an eighth uses four, so the pieces
// partition exactly the same total area.
// ------------------------------------------------------------
const PIZZA_RADIUS = 1.78;
const RING_SEGMENTS = 32;
const RING_STEP = (Math.PI * 2) / RING_SEGMENTS;
const QUARTER_STEP = Math.PI / 2;
const EIGHTH_STEP = Math.PI / 4;
const pizzaCenterA = new THREE.Vector3(-4.1, 1.57, -1.25);
const pizzaCenterB = new THREE.Vector3(4.1, 1.57, -1.25);
const plateCenterA = new THREE.Vector3(-4.1, 0.22, 3.45);
const plateCenterB = new THREE.Vector3(4.1, 0.22, 3.45);
const sauceMaterial = new THREE.MeshStandardMaterial({
color: 0xd94b35,
roughness: 0.9,
flatShading: true
});
const cheeseMaterial = new THREE.MeshStandardMaterial({
color: 0xffd45b,
roughness: 0.82,
flatShading: true
});
const crustMaterial = new THREE.MeshStandardMaterial({
color: 0xb96c32,
roughness: 1,
flatShading: true
});
const seamMaterial = new THREE.MeshStandardMaterial({
color: 0x74351f,
roughness: 1,
flatShading: true
});
const pepperoniMaterial = new THREE.MeshStandardMaterial({
color: 0xc9342f,
roughness: 0.85,
flatShading: true
});
const basilMaterial = new THREE.MeshStandardMaterial({
color: 0x4e9e54,
roughness: 0.9,
flatShading: true
});
const cheeseToppingMaterial = new THREE.MeshStandardMaterial({
color: 0xffe98b,
roughness: 0.86,
flatShading: true
});
const crustGeometry = new THREE.BoxGeometry(0.31, 0.22, 0.27);
const pepperoniGeometry = new THREE.BoxGeometry(0.26, 0.09, 0.26);
const basilGeometry = new THREE.BoxGeometry(0.19, 0.065, 0.17);
const cheeseToppingGeometry = new THREE.BoxGeometry(
0.18,
0.06,
0.18
);
const seamGeometry = new THREE.BoxGeometry(0.035, 0.025, 1.58);
function sectorGeometry(
radius,
startIndex,
endIndex,
centerAngle,
depth
) {
const shape = new THREE.Shape();
shape.moveTo(0, 0);
for (let k = startIndex; k <= endIndex; k++) {
const globalAngle = k * RING_STEP;
const localAngle = globalAngle - centerAngle;
// The shape is created in XY and rotated into the XZ plane.
shape.lineTo(
Math.cos(localAngle) * radius,
-Math.sin(localAngle) * radius
);
}
shape.lineTo(0, 0);
const geometry = new THREE.ExtrudeGeometry(shape, {
depth,
bevelEnabled: false,
steps: 1,
curveSegments: 1
});
geometry.rotateX(-Math.PI / 2);
geometry.computeVertexNormals();
return geometry;
}
function seededRandom(seed) {
let value = seed >>> 0;
return function () {
value += 0x6d2b79f5;
let t = value;
t = Math.imul(t ^ (t >>> 15), t | 1);
t ^= t + Math.imul(t ^ (t >>> 7), t | 61);
return ((t ^ (t >>> 14)) >>> 0) / 4294967296;
};
}
const slices4 = [];
const slices8 = [];
function createSlice({
pizza,
index,
startIndex,
endIndex,
edgeStart,
edgeCount,
centerAngle,
halfAngle,
toppingCount,
seed,
sourcePosition
}) {
const group = new THREE.Group();
group.name = `Pizza ${pizza}, piece ${index + 1}`;
const sauce = new THREE.Mesh(
sectorGeometry(
PIZZA_RADIUS,
startIndex,
endIndex,
centerAngle,
0.07
),
sauceMaterial
);
sauce.castShadow = true;
sauce.receiveShadow = true;
group.add(sauce);
const cheese = new THREE.Mesh(
sectorGeometry(
PIZZA_RADIUS - 0.11,
startIndex,
endIndex,
centerAngle,
0.045
),
cheeseMaterial
);
cheese.position.y = 0.07;
cheese.castShadow = true;
cheese.receiveShadow = true;
group.add(cheese);
// One crust block for every global polygon edge. Ownership is
// divided differently between the 4-cut and 8-cut pizzas.
for (let edge = 0; edge < edgeCount; edge++) {
const globalEdge = edgeStart + edge;
const globalMidAngle = (globalEdge + 0.5) * RING_STEP;
const localAngle = globalMidAngle - centerAngle;
const crust = new THREE.Mesh(
crustGeometry,
crustMaterial
);
crust.position.set(
Math.cos(localAngle) * (PIZZA_RADIUS + 0.015),
0.035,
Math.sin(localAngle) * (PIZZA_RADIUS + 0.015)
);
crust.rotation.y = -localAngle;
crust.castShadow = true;
crust.receiveShadow = true;
group.add(crust);
}
// A dark radial cut marker travels with the slice.
const seamAngle = -halfAngle;
const seam = new THREE.Mesh(seamGeometry, seamMaterial);
seam.position.set(
Math.cos(seamAngle) * 0.79,
0.132,
Math.sin(seamAngle) * 0.79
);
seam.rotation.y = Math.PI / 2 - seamAngle;
seam.receiveShadow = true;
group.add(seam);
// Voxel toppings.
const random = seededRandom(seed);
for (let n = 0; n < toppingCount; n++) {
const angle =
(random() - 0.5) *
Math.max(0.08, halfAngle * 2 - 0.16);
const radius =
0.43 +
random() * (PIZZA_RADIUS - 0.78);
const x = Math.cos(angle) * radius;
const z = Math.sin(angle) * radius;
const choice = random();
let topping;
if (choice < 0.5) {
topping = new THREE.Mesh(
pepperoniGeometry,
pepperoniMaterial
);
} else if (choice < 0.78) {
topping = new THREE.Mesh(
basilGeometry,
basilMaterial
);
} else {
topping = new THREE.Mesh(
cheeseToppingGeometry,
cheeseToppingMaterial
);
}
topping.position.set(x, 0.155, z);
topping.rotation.y = random() * Math.PI;
topping.castShadow = true;
topping.receiveShadow = true;
group.add(topping);
}
const sourceQuaternion = new THREE.Quaternion().setFromEuler(
new THREE.Euler(0, -centerAngle, 0)
);
group.position.copy(sourcePosition);
group.quaternion.copy(sourceQuaternion);
scene.add(group);
return {
group,
pizza,
index,
centerAngle,
sourcePosition: sourcePosition.clone(),
sourceQuaternion: sourceQuaternion.clone(),
animation: null
};
}
for (let i = 0; i < 4; i++) {
const start = i * 8;
const end = (i + 1) * 8;
const center = (i + 0.5) * 8 * RING_STEP;
slices4.push(
createSlice({
pizza: "A",
index: i,
startIndex: start,
endIndex: end,
edgeStart: start,
edgeCount: 8,
centerAngle: center,
halfAngle: QUARTER_STEP / 2,
toppingCount: 7,
seed: 100 + i * 37,
sourcePosition: pizzaCenterA
})
);
}
for (let i = 0; i < 8; i++) {
const start = i * 4;
const end = (i + 1) * 4;
const center = (i + 0.5) * 4 * RING_STEP;
slices8.push(
createSlice({
pizza: "B",
index: i,
startIndex: start,
endIndex: end,
edgeStart: start,
edgeCount: 4,
centerAngle: center,
halfAngle: EIGHTH_STEP / 2,
toppingCount: 4,
seed: 500 + i * 41,
sourcePosition: pizzaCenterB
})
);
}
// ------------------------------------------------------------
// Stands and plates
// ------------------------------------------------------------
const standTopMaterial = new THREE.MeshStandardMaterial({
color: 0x26394f,
roughness: 0.72,
metalness: 0.12,
flatShading: true
});
const standStemMaterial = new THREE.MeshStandardMaterial({
color: 0x17283b,
roughness: 0.8,
metalness: 0.18,
flatShading: true
});
const plateMaterial = new THREE.MeshStandardMaterial({
color: 0x70889f,
roughness: 0.55,
metalness: 0.18,
flatShading: true
});
const plateRimMaterial = new THREE.MeshStandardMaterial({
color: 0xa9c1d2,
roughness: 0.45,
metalness: 0.22,
flatShading: true
});
function createStand(x, z) {
const stand = new THREE.Group();
const top = new THREE.Mesh(
new THREE.CylinderGeometry(2.03, 2.03, 0.16, 16),
standTopMaterial
);
top.position.y = 1.46;
top.castShadow = true;
top.receiveShadow = true;
stand.add(top);
const stem = new THREE.Mesh(
new THREE.CylinderGeometry(0.42, 0.56, 1.32, 8),
standStemMaterial
);
stem.position.y = 0.72;
stem.castShadow = true;
stem.receiveShadow = true;
stand.add(stem);
const base = new THREE.Mesh(
new THREE.CylinderGeometry(1.04, 1.18, 0.18, 8),
standStemMaterial
);
base.position.y = 0.1;
base.castShadow = true;
base.receiveShadow = true;
stand.add(base);
stand.position.set(x, 0, z);
scene.add(stand);
}
function createPlate(x, z, accent) {
const plate = new THREE.Group();
const base = new THREE.Mesh(
new THREE.CylinderGeometry(2.32, 2.2, 0.16, 24),
plateMaterial
);
base.position.y = 0.1;
base.castShadow = true;
base.receiveShadow = true;
plate.add(base);
const rim = new THREE.Mesh(
new THREE.TorusGeometry(2.18, 0.095, 8, 32),
plateRimMaterial
);
rim.rotation.x = Math.PI / 2;
rim.position.y = 0.19;
rim.castShadow = true;
rim.receiveShadow = true;
plate.add(rim);
const accentMaterial = new THREE.MeshStandardMaterial({
color: accent,
emissive: accent,
emissiveIntensity: 0.12,
roughness: 0.5,
flatShading: true
});
const marker = new THREE.Mesh(
new THREE.BoxGeometry(0.65, 0.035, 0.13),
accentMaterial
);
marker.position.set(0, 0.205, 2.05);
marker.receiveShadow = true;
plate.add(marker);
plate.position.set(x, 0, z);
scene.add(plate);
}
createStand(-4.1, -1.25);
createStand(4.1, -1.25);
createPlate(-4.1, 3.45, 0xffd45b);
createPlate(4.1, 3.45, 0x66e5d2);
// ------------------------------------------------------------
// Fraction state and animation
// ------------------------------------------------------------
const options = {
quarter: {
leftCount: 1,
rightCount: 2,
leftText: "1/4",
rightText: "2/8",
description: "One quarter and two eighths cover the same pizza area."
},
half: {
leftCount: 2,
rightCount: 4,
leftText: "2/4",
rightText: "4/8",
description: "Two quarters and four eighths cover the same pizza area."
},
threeQuarters: {
leftCount: 3,
rightCount: 6,
leftText: "3/4",
rightText: "6/8",
description: "Three quarters and six eighths cover the same pizza area."
},
whole: {
leftCount: 4,
rightCount: 8,
leftText: "4/4",
rightText: "8/8",
description: "All four quarter pieces and all eight eighth pieces are the whole pizza."
}
};
let activeOption = "quarter";
let actionToken = 0;
function targetQuaternion(angle) {
return new THREE.Quaternion().setFromEuler(
new THREE.Euler(0, -angle, 0)
);
}
function animateSlice(
slice,
targetPosition,
targetAngle,
delay = 0,
arcHeight = 2.2
) {
slice.animation = {
startTime: performance.now() + delay,
duration: 760,
startPosition: slice.group.position.clone(),
startQuaternion: slice.group.quaternion.clone(),
targetPosition: targetPosition.clone(),
targetQuaternion: targetQuaternion(targetAngle),
arcHeight
};
}
function snapToSource(slice) {
slice.animation = null;
slice.group.position.copy(slice.sourcePosition);
slice.group.quaternion.copy(slice.sourceQuaternion);
}
function updateAnimations(now) {
const allSlices = [...slices4, ...slices8];
for (const slice of allSlices) {
const animation = slice.animation;
if (!animation || now < animation.startTime) continue;
const raw =
(now - animation.startTime) / animation.duration;
const t = THREE.MathUtils.clamp(raw, 0, 1);
const eased =
t < 0.5
? 4 * t * t * t
: 1 - Math.pow(-2 * t + 2, 3) / 2;
slice.group.position.lerpVectors(
animation.startPosition,
animation.targetPosition,
eased
);
slice.group.position.y +=
Math.sin(Math.PI * eased) * animation.arcHeight;
slice.group.quaternion.slerpQuaternions(
animation.startQuaternion,
animation.targetQuaternion,
eased
);
if (t >= 1) {
slice.group.position.copy(animation.targetPosition);
slice.group.quaternion.copy(animation.targetQuaternion);
slice.animation = null;
}
}
}
function updateLabels(option, moved = true) {
if (!moved) {
paintLabel(plateLabelA, "PORTION PLATE", "#ffd45b");
paintLabel(plateLabelB, "PORTION PLATE", "#66e5d2");
paintLabel(equalityLabel, "VS", "#ffffff");
return;
}
paintLabel(plateLabelA, option.leftText, "#ffd45b");
paintLabel(plateLabelB, option.rightText, "#66e5d2");
paintLabel(equalityLabel, "= SAME AREA", "#ffffff");
}
function moveMatchingPortions() {
const option = options[activeOption];
const token = ++actionToken;
// Clear any previous arrangement before starting a new comparison.
for (const slice of [...slices4, ...slices8]) {
snapToSource(slice);
}
updateLabels(option, true);
status.textContent = "Flying the matching portions to the plates…";
const frontAngle = -Math.PI / 2;
const selected4 = slices4.slice(0, option.leftCount);
const selected8 = slices8.slice(0, option.rightCount);
selected4.forEach((slice, index) => {
const targetAngle =
frontAngle +
(index - (option.leftCount - 1) / 2) * QUARTER_STEP;
animateSlice(
slice,
plateCenterA,
targetAngle,
index * 70,
2.35
);
});
selected8.forEach((slice, index) => {
const targetAngle =
frontAngle +
(index - (option.rightCount - 1) / 2) * EIGHTH_STEP;
animateSlice(
slice,
plateCenterB,
targetAngle,
index * 45,
2.35
);
});
const completionDelay =
760 +
Math.max(option.leftCount, option.rightCount) * 70 +
100;
window.setTimeout(() => {
if (token !== actionToken) return;
status.textContent = option.description;
}, completionDelay);
}
function resetPizzas() {
const token = ++actionToken;
updateLabels(options[activeOption], false);
status.textContent = "Returning every piece to its original pizza.";
[...slices4, ...slices8].forEach((slice, index) => {
animateSlice(
slice,
slice.sourcePosition,
slice.centerAngle,
index * 18,
1.05
);
});
window.setTimeout(() => {
if (token !== actionToken) return;
status.textContent =
"Choose a fraction pair to send equal-area portions to the plates.";
}, 1150);
}
fractionCards.forEach((card) => {
card.addEventListener("click", () => {
fractionCards.forEach((item) =>
item.classList.toggle("active", item === card)
);
activeOption = card.dataset.key;
moveMatchingPortions();
});
});
moveButton.addEventListener("click", moveMatchingPortions);
resetButton.addEventListener("click", resetPizzas);
viewButton.addEventListener("click", resetView);
// ------------------------------------------------------------
// Resize and render loop
// ------------------------------------------------------------
function resize() {
const width = window.innerWidth;
const height = window.innerHeight;
camera.aspect = width / height;
camera.updateProjectionMatrix();
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(width, height);
}
window.addEventListener("resize", resize);
function animate(now) {
requestAnimationFrame(animate);
updateAnimations(now);
const damping = 0.09;
view.azimuth +=
(view.targetAzimuth - view.azimuth) * damping;
view.polar +=
(view.targetPolar - view.polar) * damping;
view.distance +=
(view.targetDistance - view.distance) * damping;
const horizontal = Math.cos(view.polar) * view.distance;
camera.position.set(
view.target.x + Math.sin(view.azimuth) * horizontal,
view.target.y + Math.sin(view.polar) * view.distance,
view.target.z + Math.cos(view.azimuth) * horizontal
);
camera.lookAt(view.target);
renderer.render(scene, camera);
}
resize();
requestAnimationFrame(animate);
requestAnimationFrame(() => {
document.getElementById("loading").classList.add("hidden");
});
</script>
</body>
</html>
```