Prompt specification
Build or customize the Three.js 3D scene "Area rearrangement Pythagorean demonstration" 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: Area rearrangement Pythagorean demonstration
Category: Algorithms & Systems · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/area-rearrangement-pythagorean-demonstration-x1726
Scene Description Prompt:
I'd like to see this as a stylized Three.js scene: area rearrangement Pythagorean demonstration. Animate congruent right-triangle pieces between two square arrangements so the remaining areas expose the relation; preserve all piece shapes and avoid hidden overlaps. Use cdnjs and return one self-contained HTML file.
Working Three.js Starter (area-rearrangement-pythagorean-demonstration-x1726):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Area Rearrangement — Pythagorean Demonstration</title>
<style>
:root {
color-scheme: dark;
--bg: #071018;
--panel: rgba(12, 25, 36, 0.78);
--border: rgba(171, 220, 235, 0.16);
--text: #eaf8ff;
--muted: #91a9b6;
--cyan: #55e6d0;
--coral: #ff765f;
--violet: #a78bfa;
--amber: #fbbf24;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background:
radial-gradient(circle at 18% 12%, rgba(28, 103, 119, 0.22), transparent 34%),
radial-gradient(circle at 82% 18%, rgba(117, 70, 170, 0.18), transparent 32%),
linear-gradient(145deg, #071018 0%, #0a1520 55%, #071018 100%);
color: var(--text);
font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}
canvas {
display: block;
width: 100%;
height: 100%;
touch-action: none;
}
#hud {
position: fixed;
inset: 0;
pointer-events: none;
}
.brand {
position: absolute;
top: 26px;
left: 30px;
max-width: 390px;
text-shadow: 0 2px 24px rgba(0, 0, 0, 0.45);
}
.eyebrow {
display: flex;
align-items: center;
gap: 10px;
margin-bottom: 10px;
color: var(--cyan);
font-size: 11px;
font-weight: 750;
letter-spacing: 0.22em;
text-transform: uppercase;
}
.eyebrow::before {
width: 30px;
height: 1px;
content: "";
background: currentColor;
box-shadow: 0 0 12px currentColor;
}
h1 {
margin: 0;
font-size: clamp(30px, 4.2vw, 62px);
font-weight: 760;
line-height: 0.94;
letter-spacing: -0.055em;
}
h1 span {
display: block;
color: var(--muted);
font-size: 0.47em;
font-weight: 540;
letter-spacing: -0.02em;
}
.brand p {
max-width: 350px;
margin: 16px 0 0;
color: var(--muted);
font-size: 13px;
line-height: 1.55;
}
.proof {
position: absolute;
top: 25px;
right: 27px;
width: 310px;
padding: 18px 19px 17px;
border: 1px solid var(--border);
border-radius: 18px;
background: linear-gradient(150deg, rgba(17, 35, 48, 0.88), rgba(9, 19, 29, 0.74));
box-shadow: 0 24px 70px rgba(0, 0, 0, 0.28);
backdrop-filter: blur(18px);
}
.proof-title {
display: flex;
align-items: center;
justify-content: space-between;
margin-bottom: 14px;
color: var(--muted);
font-size: 10px;
font-weight: 750;
letter-spacing: 0.16em;
text-transform: uppercase;
}
.proof-title i {
width: 8px;
height: 8px;
border-radius: 50%;
background: var(--coral);
box-shadow: 0 0 16px rgba(255, 118, 95, 0.9);
}
.equation {
display: grid;
grid-template-columns: 1fr auto;
gap: 6px 14px;
align-items: baseline;
font-size: 15px;
}
.equation .lhs {
color: var(--muted);
}
.equation .triangle {
color: var(--coral);
}
.result {
margin-top: 14px;
padding-top: 13px;
border-top: 1px solid var(--border);
color: var(--cyan);
font-size: 31px;
font-weight: 760;
letter-spacing: -0.04em;
text-align: center;
text-shadow: 0 0 24px rgba(85, 230, 208, 0.18);
transition: color 250ms ease, text-shadow 250ms ease, transform 250ms ease;
}
.result.complete {
color: #fff;
text-shadow: 0 0 26px rgba(85, 230, 208, 0.7);
transform: scale(1.025);
}
.phase {
min-height: 32px;
margin-top: 10px;
color: var(--muted);
font-size: 11px;
line-height: 1.45;
text-align: center;
}
.controls {
position: absolute;
bottom: 24px;
left: 50%;
display: flex;
align-items: center;
gap: 12px;
width: min(720px, calc(100vw - 32px));
padding: 10px;
border: 1px solid var(--border);
border-radius: 16px;
background: var(--panel);
box-shadow: 0 18px 60px rgba(0, 0, 0, 0.35);
transform: translateX(-50%);
backdrop-filter: blur(18px);
pointer-events: auto;
}
button {
min-height: 42px;
border: 0;
border-radius: 11px;
padding: 0 17px;
background: var(--cyan);
color: #06131a;
font: inherit;
font-size: 12px;
font-weight: 780;
letter-spacing: 0.03em;
cursor: pointer;
transition: transform 160ms ease, filter 160ms ease;
}
button:hover {
filter: brightness(1.08);
transform: translateY(-1px);
}
button:active {
transform: translateY(0);
}
button.secondary {
min-width: 42px;
padding: 0 12px;
background: rgba(255, 255, 255, 0.07);
color: var(--text);
}
#timeline {
flex: 1;
min-width: 80px;
accent-color: var(--cyan);
cursor: pointer;
}
.time-readout {
min-width: 46px;
color: var(--muted);
font-size: 11px;
font-variant-numeric: tabular-nums;
text-align: right;
}
.hint {
position: absolute;
right: 28px;
bottom: 29px;
color: rgba(190, 215, 225, 0.56);
font-size: 10px;
letter-spacing: 0.05em;
pointer-events: none;
}
.legend {
position: absolute;
bottom: 28px;
left: 30px;
display: flex;
gap: 15px;
color: var(--muted);
font-size: 10px;
letter-spacing: 0.04em;
}
.legend span {
display: flex;
align-items: center;
gap: 6px;
}
.legend i {
width: 8px;
height: 8px;
border-radius: 2px;
background: currentColor;
}
@media (max-width: 900px) {
.brand {
top: 18px;
left: 18px;
max-width: 330px;
}
.brand p,
.legend,
.hint {
display: none;
}
.proof {
top: 18px;
right: 16px;
width: 245px;
padding: 14px;
}
.result {
font-size: 26px;
}
.controls {
bottom: 14px;
}
}
@media (max-width: 620px) {
.brand {
top: 15px;
left: 15px;
}
.brand h1 {
font-size: 28px;
}
.eyebrow {
font-size: 9px;
}
.proof {
top: 105px;
right: 12px;
width: 205px;
}
.equation {
font-size: 12px;
}
.phase {
display: none;
}
.controls {
gap: 8px;
width: calc(100vw - 20px);
padding: 8px;
}
button {
padding: 0 12px;
font-size: 11px;
}
}
</style>
</head>
<body>
<main id="app" aria-label="Interactive Pythagorean area rearrangement"></main>
<div id="hud">
<section class="brand">
<div class="eyebrow">Geometric dissection · 03—04—05</div>
<h1>
Area
<span>Rearrangement</span>
</h1>
<p>
Four congruent right triangles move between two equal squares.
Their total area stays fixed, exposing the identity behind the Pythagorean theorem.
</p>
</section>
<aside class="proof">
<div class="proof-title">
Area cancellation
<i></i>
</div>
<div class="equation">
<span class="lhs">(a + b)²</span>
<span>=</span>
<span>c²</span>
<span>+</span>
<span class="triangle">4 · ½ab</span>
</div>
<div class="equation" style="margin-top: 7px">
<span class="lhs">(a + b)²</span>
<span>=</span>
<span>a² + b²</span>
<span>+</span>
<span class="triangle">4 · ½ab</span>
</div>
<div id="result" class="result">c² = a² + b²</div>
<div id="phase" class="phase">The triangle area will cancel from both arrangements.</div>
</aside>
<div class="legend">
<span style="color: var(--coral)"><i></i> triangle pieces</span>
<span style="color: var(--cyan)"><i></i> c²</span>
<span style="color: var(--violet)"><i></i> a²</span>
<span style="color: var(--amber)"><i></i> b²</span>
</div>
<div class="controls">
<button id="play" type="button">Pause</button>
<button id="reset" class="secondary" type="button" title="Reset dissection">↺</button>
<input id="timeline" type="range" min="0" max="1000" value="0" aria-label="Rearrangement timeline">
<span id="time" class="time-readout">0%</span>
<button id="view" class="secondary" type="button" title="Reset camera view">⌖</button>
</div>
<div class="hint">Drag to orbit · Scroll to zoom</div>
</div>
<script type="module">
import * as THREE from "https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// -------------------------------------------------------------------------
// Scene and renderer
// -------------------------------------------------------------------------
const app = document.querySelector("#app");
const renderer = new THREE.WebGLRenderer({
antialias: true,
alpha: true,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.08;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.setClearColor(0x071018, 0);
app.appendChild(renderer.domElement);
const scene = new THREE.Scene();
scene.fog = new THREE.FogExp2(0x071018, 0.018);
const camera = new THREE.PerspectiveCamera(
36,
window.innerWidth / window.innerHeight,
0.1,
140
);
const cameraTarget = new THREE.Vector3(0, 0.2, -3.5);
let cameraTheta = 0;
let cameraPhi = 0.94;
let zoomFactor = 1;
function updateCamera() {
const fit = Math.max(1, 1.2 / camera.aspect);
const radius = 30 * fit * zoomFactor;
const spherical = new THREE.Spherical(radius, cameraPhi, cameraTheta);
camera.position.setFromSpherical(spherical).add(cameraTarget);
camera.lookAt(cameraTarget);
}
updateCamera();
// -------------------------------------------------------------------------
// Lighting
// -------------------------------------------------------------------------
scene.add(new THREE.HemisphereLight(0xb9ecff, 0x10151d, 1.45));
const key = new THREE.DirectionalLight(0xffffff, 3.2);
key.position.set(-9, 18, 12);
key.target.position.copy(cameraTarget);
key.castShadow = true;
key.shadow.mapSize.set(2048, 2048);
key.shadow.camera.left = -20;
key.shadow.camera.right = 20;
key.shadow.camera.top = 20;
key.shadow.camera.bottom = -20;
key.shadow.camera.near = 1;
key.shadow.camera.far = 60;
key.shadow.bias = -0.0005;
scene.add(key, key.target);
const rim = new THREE.DirectionalLight(0x66d9ff, 1.8);
rim.position.set(12, 8, -13);
scene.add(rim);
const warm = new THREE.PointLight(0xff876f, 26, 24, 2);
warm.position.set(-7, 4, 3);
scene.add(warm);
const cyan = new THREE.PointLight(0x55e6d0, 22, 22, 2);
cyan.position.set(8, 3, -4);
scene.add(cyan);
// -------------------------------------------------------------------------
// Geometric constants
//
// Board-local coordinates run from 0 to L:
// x = horizontal
// y = upward on the page
//
// World placement maps board y to world -z, making the board lie on the
// floor with its upper edge farther from the camera.
// -------------------------------------------------------------------------
const A = 4;
const B = 3;
const L = A + B;
const C = Math.hypot(A, B);
const LEFT_CENTER = -6.2;
const RIGHT_CENTER = 6.2;
const PIECE_Y = 0.22;
const SQUARE_Y = 0.20;
const v2 = (x, y) => new THREE.Vector2(x, y);
function boardToWorld(centerX, point, y = 0) {
return new THREE.Vector3(
centerX - L / 2 + point.x,
y,
-point.y
);
}
function boardDirection(x, y) {
return new THREE.Vector3(x, 0, -y).normalize();
}
// Geometry is authored in a canonical orientation:
// leg 1 has length A along +x, and leg 2 has length B along +y.
//
// Because the extruded shape is rotated onto the floor, canonical +y
// becomes local -z. The orientation function below accounts for that sign.
function orientationForLegs(leg1, leg2) {
const xAxis = boardDirection(leg1.x, leg1.y);
const zAxis = boardDirection(leg2.x, leg2.y).negate();
const yAxis = new THREE.Vector3().crossVectors(zAxis, xAxis).normalize();
const matrix = new THREE.Matrix4().makeBasis(xAxis, yAxis, zAxis);
return new THREE.Quaternion().setFromRotationMatrix(matrix);
}
function makeExtrudedShape(points, thickness = 0.16, bevel = 0.045) {
const shape = new THREE.Shape();
shape.moveTo(points[0].x, points[0].y);
for (let i = 1; i < points.length; i++) {
shape.lineTo(points[i].x, points[i].y);
}
shape.closePath();
const geometry = new THREE.ExtrudeGeometry(shape, {
depth: thickness,
steps: 1,
bevelEnabled: true,
bevelThickness: bevel,
bevelSize: bevel,
bevelSegments: 2,
curveSegments: 1
});
// Shape XY -> floor XZ; extrusion Z -> world vertical Y.
geometry.rotateX(-Math.PI / 2);
geometry.computeBoundingBox();
const box = geometry.boundingBox;
geometry.translate(0, -(box.min.y + box.max.y) / 2, 0);
geometry.computeVertexNormals();
return geometry;
}
function makeTriangleGeometry() {
return makeExtrudedShape([
v2(0, 0),
v2(A, 0),
v2(0, B)
], 0.17, 0.04);
}
function makeSquareGeometry(side) {
const h = side / 2;
return makeExtrudedShape([
v2(-h, -h),
v2(h, -h),
v2(h, h),
v2(-h, h)
], 0.17, 0.04);
}
function addEdges(mesh, color = 0x07131b, opacity = 0.82) {
const edges = new THREE.EdgesGeometry(mesh.geometry, 24);
const lines = new THREE.LineSegments(
edges,
new THREE.LineBasicMaterial({
color,
transparent: true,
opacity
})
);
lines.renderOrder = 2;
mesh.add(lines);
return lines;
}
// -------------------------------------------------------------------------
// Materials
// -------------------------------------------------------------------------
const triangleMaterial = new THREE.MeshStandardMaterial({
color: 0xff765f,
roughness: 0.34,
metalness: 0.08,
emissive: 0x4a1009,
emissiveIntensity: 0.22,
side: THREE.DoubleSide
});
const cSquareMaterial = new THREE.MeshStandardMaterial({
color: 0x43d9c6,
roughness: 0.3,
metalness: 0.12,
emissive: 0x0c5149,
emissiveIntensity: 0.3,
side: THREE.DoubleSide
});
const aSquareMaterial = new THREE.MeshStandardMaterial({
color: 0x9475ed,
roughness: 0.36,
metalness: 0.08,
emissive: 0x26195e,
emissiveIntensity: 0.24,
transparent: true,
opacity: 0.08,
side: THREE.DoubleSide
});
const bSquareMaterial = new THREE.MeshStandardMaterial({
color: 0xf6b72f,
roughness: 0.36,
metalness: 0.08,
emissive: 0x5c3903,
emissiveIntensity: 0.24,
transparent: true,
opacity: 0.08,
side: THREE.DoubleSide
});
const boardMaterial = new THREE.MeshStandardMaterial({
color: 0x0c1822,
roughness: 0.78,
metalness: 0.2
});
const frameMaterial = new THREE.MeshStandardMaterial({
color: 0x28404d,
roughness: 0.3,
metalness: 0.72,
emissive: 0x071820,
emissiveIntensity: 0.35
});
// -------------------------------------------------------------------------
// Floor, boards, and frames
// -------------------------------------------------------------------------
const floor = new THREE.Mesh(
new THREE.PlaneGeometry(80, 50),
new THREE.MeshStandardMaterial({
color: 0x071018,
roughness: 0.93,
metalness: 0.05
})
);
floor.rotation.x = -Math.PI / 2;
floor.position.set(0, -0.21, -3.5);
floor.receiveShadow = true;
scene.add(floor);
function createBoard(centerX, accent) {
const base = new THREE.Mesh(
new THREE.BoxGeometry(L + 0.46, 0.16, L + 0.46),
boardMaterial
);
base.position.set(centerX, -0.095, -L / 2);
base.receiveShadow = true;
scene.add(base);
const grid = new THREE.GridHelper(L, L, accent, 0x17313d);
grid.position.set(centerX, 0.006, -L / 2);
grid.material.transparent = true;
grid.material.opacity = 0.18;
grid.material.depthWrite = false;
scene.add(grid);
const railHeight = 0.17;
const railDepth = 0.12;
const horizontalGeometry = new THREE.BoxGeometry(L + 0.45, railHeight, railDepth);
const verticalGeometry = new THREE.BoxGeometry(railDepth, railHeight, L + 0.45);
for (const z of [0, -L]) {
const rail = new THREE.Mesh(horizontalGeometry, frameMaterial);
rail.position.set(centerX, 0.085, z);
rail.castShadow = true;
rail.receiveShadow = true;
scene.add(rail);
}
for (const x of [centerX - L / 2, centerX + L / 2]) {
const rail = new THREE.Mesh(verticalGeometry, frameMaterial);
rail.position.set(x, 0.085, -L / 2);
rail.castShadow = true;
rail.receiveShadow = true;
scene.add(rail);
}
}
createBoard(LEFT_CENTER, 0x55e6d0);
createBoard(RIGHT_CENTER, 0xa78bfa);
// -------------------------------------------------------------------------
// Label sprites
// -------------------------------------------------------------------------
function roundedRect(context, x, y, width, height, radius) {
const r = Math.min(radius, width / 2, height / 2);
context.beginPath();
context.moveTo(x + r, y);
context.arcTo(x + width, y, x + width, y + height, r);
context.arcTo(x + width, y + height, x, y + height, r);
context.arcTo(x, y + height, x, y, r);
context.arcTo(x, y, x + width, y, r);
context.closePath();
}
function makeBadge(text, color, width = 700) {
const canvas = document.createElement("canvas");
canvas.width = width;
canvas.height = 180;
const context = canvas.getContext("2d");
context.clearRect(0, 0, canvas.width, canvas.height);
roundedRect(context, 8, 8, canvas.width - 16, canvas.height - 16, 40);
context.fillStyle = "rgba(7, 18, 28, 0.88)";
context.fill();
context.strokeStyle = color;
context.globalAlpha = 0.55;
context.lineWidth = 3;
context.stroke();
context.globalAlpha = 1;
context.fillStyle = color;
context.font = "700 48px Inter, system-ui, sans-serif";
context.textAlign = "center";
context.textBaseline = "middle";
context.fillText(text, canvas.width / 2, canvas.height / 2 + 2);
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.renderOrder = 10;
return sprite;
}
const leftTitle = makeBadge("INITIAL CUT · (a + b)²", "#55e6d0");
leftTitle.position.set(LEFT_CENTER, 0.72, -L - 1.12);
leftTitle.scale.set(4.25, 1.09, 1);
scene.add(leftTitle);
const rightTitle = makeBadge("REARRANGED · (a + b)²", "#a78bfa");
rightTitle.position.set(RIGHT_CENTER, 0.72, -L - 1.12);
rightTitle.scale.set(4.25, 1.09, 1);
scene.add(rightTitle);
const cLabel = makeBadge("c²", "#55e6d0", 420);
cLabel.position.copy(boardToWorld(LEFT_CENTER, v2(L / 2, L / 2), 0.72));
cLabel.scale.set(1.16, 0.58, 1);
scene.add(cLabel);
const aLabel = makeBadge("a²", "#b9a6ff", 420);
aLabel.position.copy(boardToWorld(RIGHT_CENTER, v2(A / 2, A / 2), 0.68));
aLabel.scale.set(1.16, 0.58, 1);
scene.add(aLabel);
const bLabel = makeBadge("b²", "#ffd365", 420);
bLabel.position.copy(boardToWorld(RIGHT_CENTER, v2(A + B / 2, B / 2), 0.68));
bLabel.scale.set(1.16, 0.58, 1);
scene.add(bLabel);
// -------------------------------------------------------------------------
// The central c² square in the initial arrangement
// -------------------------------------------------------------------------
const cSquare = new THREE.Mesh(makeSquareGeometry(C), cSquareMaterial);
cSquare.position.copy(boardToWorld(LEFT_CENTER, v2(L / 2, L / 2), SQUARE_Y));
// The side of the central square has board direction (B, A).
const cAngle = Math.atan2(A, B);
cSquare.quaternion.setFromAxisAngle(new THREE.Vector3(0, 1, 0), cAngle);
cSquare.castShadow = true;
cSquare.receiveShadow = true;
addEdges(cSquare, 0x062b28, 0.72);
scene.add(cSquare);
// -------------------------------------------------------------------------
// The a² and b² squares in the rearranged arrangement
// -------------------------------------------------------------------------
function createResidualSquare(side, center, material) {
const mesh = new THREE.Mesh(makeSquareGeometry(side), material);
mesh.position.copy(boardToWorld(RIGHT_CENTER, center, SQUARE_Y));
mesh.castShadow = true;
mesh.receiveShadow = true;
addEdges(mesh, 0x12131f, 0.74);
scene.add(mesh);
return mesh;
}
const aSquare = createResidualSquare(A, v2(A / 2, A / 2), aSquareMaterial);
const bSquare = createResidualSquare(B, v2(A + B / 2, B / 2), bSquareMaterial);
// -------------------------------------------------------------------------
// Four congruent triangle pieces
//
// Each piece is moved with a quaternion and a rigid translation only.
// No vertex positions are morphed, so every triangle remains congruent.
// -------------------------------------------------------------------------
const initialTriangles = [
{
id: "T1",
right: v2(0, 0),
leg1: v2(A, 0),
leg2: v2(0, B),
target: "F1"
},
{
id: "T2",
right: v2(L, 0),
leg1: v2(0, A),
leg2: v2(-B, 0),
target: "F2"
},
{
id: "T3",
right: v2(L, L),
leg1: v2(-A, 0),
leg2: v2(0, -B),
target: "F3"
},
{
id: "T4",
right: v2(0, L),
leg1: v2(0, -A),
leg2: v2(B, 0),
target: "F0"
}
];
const finalTriangles = {
F0: {
right: v2(A, A),
leg1: v2(-A, 0),
leg2: v2(0, B)
},
F1: {
right: v2(0, L),
leg1: v2(A, 0),
leg2: v2(0, -B)
},
F2: {
right: v2(L, B),
leg1: v2(0, A),
leg2: v2(-B, 0)
},
F3: {
right: v2(A, L),
leg1: v2(0, -A),
leg2: v2(B, 0)
}
};
function makeGhostLoop() {
const geometry = new THREE.BufferGeometry().setFromPoints([
new THREE.Vector3(0, 0, 0),
new THREE.Vector3(A, 0, 0),
new THREE.Vector3(0, 0, -B),
new THREE.Vector3(0, 0, 0)
]);
return new THREE.Line(
geometry,
new THREE.LineBasicMaterial({
color: 0x8ab5c5,
transparent: true,
opacity: 0,
depthWrite: false
})
);
}
const triangleGeometry = makeTriangleGeometry();
const pieces = [];
initialTriangles.forEach((source, index) => {
const target = finalTriangles[source.target];
const mesh = new THREE.Mesh(triangleGeometry, triangleMaterial);
mesh.castShadow = true;
mesh.receiveShadow = true;
addEdges(mesh, 0x3a1712, 0.82);
const sourceQuaternion = orientationForLegs(source.leg1, source.leg2);
const targetQuaternion = orientationForLegs(target.leg1, target.leg2);
mesh.quaternion.copy(sourceQuaternion);
mesh.position.copy(boardToWorld(LEFT_CENTER, source.right, PIECE_Y));
scene.add(mesh);
const sourceGhost = makeGhostLoop();
sourceGhost.quaternion.copy(sourceQuaternion);
sourceGhost.position.copy(boardToWorld(LEFT_CENTER, source.right, 0.045));
scene.add(sourceGhost);
const targetGhost = makeGhostLoop();
targetGhost.quaternion.copy(targetQuaternion);
targetGhost.position.copy(boardToWorld(RIGHT_CENTER, target.right, 0.045));
scene.add(targetGhost);
const p0 = boardToWorld(LEFT_CENTER, source.right, PIECE_Y);
const p3 = boardToWorld(RIGHT_CENTER, target.right, PIECE_Y);
// Separate heights and lateral offsets keep the four moving pieces in
// distinct flight lanes while they cross between the two arrangements.
const lane = (index - 1.5) * 0.58;
const lift = 4.25 + index * 0.72;
const p1 = new THREE.Vector3(
p0.x + 3.0,
PIECE_Y + lift,
p0.z + lane
);
const p2 = new THREE.Vector3(
p3.x - 3.0,
PIECE_Y + lift,
p3.z + lane
);
const trailPoints = [];
for (let i = 0; i <= 80; i++) {
const t = i / 80;
const u = 1 - t;
trailPoints.push(
new THREE.Vector3(
u * u * u * p0.x + 3 * u * u * t * p1.x + 3 * u * t * t * p2.x + t * t * t * p3.x,
u * u * u * p0.y + 3 * u * u * t * p1.y + 3 * u * t * t * p2.y + t * t * t * p3.y,
u * u * u * p0.z + 3 * u * u * t * p1.z + 3 * u * t * t * p2.z + t * t * t * p3.z
)
);
}
const trail = new THREE.Line(
new THREE.BufferGeometry().setFromPoints(trailPoints),
new THREE.LineBasicMaterial({
color: 0xff8a75,
transparent: true,
opacity: 0,
depthWrite: false
})
);
trail.frustumCulled = false;
scene.add(trail);
pieces.push({
mesh,
sourceGhost,
targetGhost,
trail,
q0: sourceQuaternion,
q1: targetQuaternion,
p0,
p1,
p2,
p3,
start: 0.025 + index * 0.13,
duration: 0.53
});
});
// -------------------------------------------------------------------------
// Animation state and controls
// -------------------------------------------------------------------------
const playButton = document.querySelector("#play");
const resetButton = document.querySelector("#reset");
const viewButton = document.querySelector("#view");
const timeline = document.querySelector("#timeline");
const timeReadout = document.querySelector("#time");
const result = document.querySelector("#result");
const phase = document.querySelector("#phase");
let progress = 0;
let playing = false;
const reducedMotion = window.matchMedia("(prefers-reduced-motion: reduce)").matches;
function setPlaying(value) {
playing = value;
playButton.textContent = playing ? "Pause" : (progress >= 0.999 ? "Replay" : "Play");
}
function setProgress(value) {
progress = THREE.MathUtils.clamp(value, 0, 1);
timeline.value = Math.round(progress * 1000);
timeReadout.textContent = `${Math.round(progress * 100)}%`;
result.classList.toggle("complete", progress > 0.94);
if (progress < 0.18) {
phase.textContent = "The four congruent triangles are lifted from the initial square.";
} else if (progress < 0.78) {
phase.textContent = "The same pieces are redistributed without changing their shape.";
} else if (progress < 0.95) {
phase.textContent = "The triangle areas now occupy exactly the same total area.";
} else {
phase.textContent = "Cancel the identical triangle area from both equal squares.";
}
}
playButton.addEventListener("click", () => {
if (playing) {
setPlaying(false);
} else {
if (progress >= 0.999) setProgress(0);
setPlaying(true);
}
});
resetButton.addEventListener("click", () => {
setPlaying(false);
setProgress(0);
});
timeline.addEventListener("input", () => {
setPlaying(false);
setProgress(Number(timeline.value) / 1000);
});
viewButton.addEventListener("click", () => {
cameraTheta = 0;
cameraPhi = 0.94;
zoomFactor = 1;
updateCamera();
});
// -------------------------------------------------------------------------
// Direct camera controls — no addons or external examples are used
// -------------------------------------------------------------------------
let dragging = false;
let pointerX = 0;
let pointerY = 0;
renderer.domElement.addEventListener("pointerdown", (event) => {
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;
cameraTheta -= dx * 0.005;
cameraPhi = THREE.MathUtils.clamp(cameraPhi + dy * 0.004, 0.48, 1.28);
updateCamera();
});
function stopDragging(event) {
dragging = false;
if (event.pointerId !== undefined && renderer.domElement.hasPointerCapture(event.pointerId)) {
renderer.domElement.releasePointerCapture(event.pointerId);
}
}
renderer.domElement.addEventListener("pointerup", stopDragging);
renderer.domElement.addEventListener("pointercancel", stopDragging);
renderer.domElement.addEventListener("wheel", (event) => {
event.preventDefault();
zoomFactor = THREE.MathUtils.clamp(
zoomFactor * Math.exp(event.deltaY * 0.00065),
0.72,
1.75
);
updateCamera();
}, { passive: false });
window.addEventListener("resize", () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
updateCamera();
});
// -------------------------------------------------------------------------
// Timeline evaluation
// -------------------------------------------------------------------------
function cubicBezier(t, p0, p1, p2, p3, target) {
const u = 1 - t;
target.set(
u * u * u * p0.x + 3 * u * u * t * p1.x + 3 * u * t * t * p2.x + t * t * t * p3.x,
u * u * u * p0.y + 3 * u * u * t * p1.y + 3 * u * t * t * p2.y + t * t * t * p3.y,
u * u * u * p0.z + 3 * u * u * t * p1.z + 3 * u * t * t * p2.z + t * t * t * p3.z
);
return target;
}
const movingPosition = new THREE.Vector3();
function updateDissection() {
pieces.forEach((piece) => {
const raw = THREE.MathUtils.clamp(
(progress - piece.start) / piece.duration,
0,
1
);
const eased = raw * raw * (3 - 2 * raw);
cubicBezier(eased, piece.p0, piece.p1, piece.p2, piece.p3, movingPosition);
piece.mesh.position.copy(movingPosition);
// Rigid-body orientation: the triangle's vertices never change shape.
piece.mesh.quaternion.copy(piece.q0).slerp(piece.q1, eased);
piece.sourceGhost.material.opacity = 0.19 * raw;
piece.targetGhost.material.opacity = 0.19 * (1 - raw);
piece.trail.material.opacity = 0.25 * Math.sin(Math.PI * raw);
});
const reveal = THREE.MathUtils.smoothstep(progress, 0.2, 0.78);
aSquareMaterial.opacity = 0.08 + reveal * 0.92;
bSquareMaterial.opacity = 0.08 + reveal * 0.92;
const squareScale = 0.92 + reveal * 0.08;
aSquare.scale.set(squareScale, 1, squareScale);
bSquare.scale.set(squareScale, 1, squareScale);
aLabel.material.opacity = 0.25 + reveal * 0.75;
bLabel.material.opacity = 0.25 + reveal * 0.75;
}
// -------------------------------------------------------------------------
// Render loop
// -------------------------------------------------------------------------
const clock = new THREE.Clock();
renderer.setAnimationLoop(() => {
const delta = Math.min(clock.getDelta(), 0.05);
const elapsed = clock.elapsedTime;
if (playing) {
setProgress(progress + delta / 7.2);
if (progress >= 1) {
setPlaying(false);
playButton.textContent = "Replay";
}
}
updateDissection();
const pulse = 1 + Math.sin(elapsed * 2.2) * 0.035;
cSquareMaterial.emissiveIntensity = 0.28 + Math.sin(elapsed * 2.1) * 0.07;
aSquareMaterial.emissiveIntensity = 0.22 + Math.sin(elapsed * 2.1 + 1) * 0.05;
bSquareMaterial.emissiveIntensity = 0.22 + Math.sin(elapsed * 2.1 + 2) * 0.05;
triangleMaterial.emissiveIntensity = 0.2 + Math.sin(elapsed * 1.8) * 0.035;
cSquare.scale.set(pulse, 1, pulse);
renderer.render(scene, camera);
});
setProgress(0);
if (!reducedMotion) {
window.setTimeout(() => setPlaying(true), 650);
} else {
setProgress(1);
}
</script>
</body>
</html>
```