Prompt specification
Build or customize the Three.js 3D scene "Counting unchanged after rearrangement" 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: Counting unchanged after rearrangement
Category: Algorithms & Systems · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/counting-unchanged-after-rearrangement-x1706
Scene Description Prompt:
Spread and cluster the same stylized marked objects, showing that rearrangement leaves their count unchanged. Three.js from cdnjs, one HTML file.
Working Three.js Starter (counting-unchanged-after-rearrangement-x1706):
```html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Same Objects, Different Arrangement</title>
<style>
:root {
--ink: #191b1f;
--muted: #6d6a64;
--paper: #f3efe7;
--panel: rgba(255, 253, 248, 0.84);
--line: rgba(25, 27, 31, 0.13);
--accent: #ff654f;
--green: #218a68;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: var(--paper);
color: var(--ink);
font-family:
Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
button {
font: inherit;
}
#stage {
position: fixed;
inset: 0;
touch-action: none;
cursor: grab;
}
#stage.dragging {
cursor: grabbing;
}
#stage canvas {
display: block;
outline: none;
}
.wash {
position: fixed;
inset: 0;
pointer-events: none;
background:
radial-gradient(circle at 18% 12%, rgba(255, 255, 255, 0.7), transparent 31%),
radial-gradient(circle at 83% 85%, rgba(213, 201, 182, 0.22), transparent 34%);
mix-blend-mode: soft-light;
}
.interface {
position: fixed;
inset: 0;
pointer-events: none;
}
.brand {
position: absolute;
top: clamp(22px, 4vw, 48px);
left: clamp(22px, 4vw, 52px);
max-width: 440px;
}
.eyebrow {
display: flex;
align-items: center;
gap: 9px;
margin-bottom: 11px;
color: var(--muted);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.19em;
text-transform: uppercase;
}
.eyebrow::before {
width: 20px;
height: 3px;
border-radius: 99px;
background: var(--accent);
content: "";
}
h1 {
margin: 0;
max-width: 430px;
font-size: clamp(30px, 4.2vw, 58px);
font-weight: 760;
letter-spacing: -0.055em;
line-height: 0.98;
}
.brand p {
max-width: 390px;
margin: 17px 0 0;
color: var(--muted);
font-size: 13px;
line-height: 1.55;
}
.count-card {
position: absolute;
top: clamp(20px, 3vw, 40px);
right: clamp(20px, 3vw, 44px);
width: 220px;
padding: 17px 18px 15px;
overflow: hidden;
border: 1px solid var(--line);
border-radius: 18px;
background: var(--panel);
box-shadow: 0 18px 50px rgba(56, 48, 38, 0.08);
backdrop-filter: blur(16px);
}
.count-card::after {
position: absolute;
inset: 0;
border: 1px solid transparent;
border-radius: inherit;
content: "";
pointer-events: none;
}
.count-card.verified {
animation: verify 600ms ease;
}
@keyframes verify {
0% { box-shadow: 0 18px 50px rgba(56, 48, 38, 0.08); }
35% { box-shadow: 0 0 0 5px rgba(33, 138, 104, 0.14); }
100% { box-shadow: 0 18px 50px rgba(56, 48, 38, 0.08); }
}
.count-top {
display: flex;
align-items: center;
justify-content: space-between;
color: var(--muted);
font-size: 9px;
font-weight: 800;
letter-spacing: 0.15em;
text-transform: uppercase;
}
.stable-dot {
display: inline-block;
width: 7px;
height: 7px;
margin-right: 6px;
border-radius: 50%;
background: var(--green);
box-shadow: 0 0 0 4px rgba(33, 138, 104, 0.1);
}
.count-row {
display: flex;
align-items: baseline;
gap: 9px;
margin: 8px 0 4px;
}
.count-row strong {
font-size: 48px;
font-weight: 760;
letter-spacing: -0.07em;
line-height: 1;
}
.count-row span {
color: var(--muted);
font-size: 18px;
font-weight: 500;
}
#proof {
color: var(--muted);
font-size: 10px;
font-weight: 650;
letter-spacing: 0.02em;
}
.inventory {
display: grid;
grid-template-columns: repeat(12, 1fr);
gap: 4px;
margin-top: 13px;
padding-top: 12px;
border-top: 1px solid var(--line);
}
.inventory i {
aspect-ratio: 1;
border-radius: 3px;
opacity: 0.86;
transition: transform 300ms ease, opacity 300ms ease;
}
.inventory.moving i {
animation: inventoryPulse 700ms ease infinite alternate;
}
.inventory.moving i:nth-child(3n) { animation-delay: -180ms; }
.inventory.moving i:nth-child(4n) { animation-delay: -360ms; }
@keyframes inventoryPulse {
to { transform: translateY(-2px); opacity: 1; }
}
.layout-tag {
position: absolute;
top: clamp(185px, 29vh, 270px);
left: 50%;
display: flex;
align-items: center;
gap: 9px;
padding: 8px 12px;
border: 1px solid var(--line);
border-radius: 999px;
background: rgba(255, 253, 248, 0.65);
color: var(--muted);
font-size: 9px;
font-weight: 800;
letter-spacing: 0.17em;
text-transform: uppercase;
transform: translateX(-50%);
backdrop-filter: blur(12px);
white-space: nowrap;
}
.layout-tag b {
width: 6px;
height: 6px;
border-radius: 50%;
background: var(--accent);
}
.legend {
position: absolute;
bottom: clamp(112px, 15vh, 150px);
left: clamp(22px, 4vw, 52px);
display: grid;
gap: 10px;
}
.legend div {
display: flex;
align-items: center;
gap: 10px;
color: var(--muted);
font-size: 10px;
font-weight: 700;
letter-spacing: 0.04em;
}
.legend span {
display: grid;
width: 22px;
height: 22px;
place-items: center;
border: 1px solid var(--line);
border-radius: 50%;
background: rgba(255, 255, 255, 0.4);
color: var(--ink);
font-size: 9px;
}
.controls {
position: absolute;
bottom: clamp(18px, 3vw, 36px);
left: 50%;
display: flex;
align-items: center;
gap: 6px;
padding: 7px;
border: 1px solid var(--line);
border-radius: 17px;
background: var(--panel);
box-shadow: 0 16px 55px rgba(56, 48, 38, 0.12);
pointer-events: auto;
transform: translateX(-50%);
backdrop-filter: blur(18px);
}
.controls button {
display: flex;
align-items: center;
justify-content: center;
gap: 8px;
min-height: 42px;
padding: 0 14px;
border: 0;
border-radius: 11px;
background: transparent;
color: var(--muted);
cursor: pointer;
font-size: 11px;
font-weight: 750;
white-space: nowrap;
transition:
color 180ms ease,
background 180ms ease,
transform 180ms ease;
}
.controls button:hover {
color: var(--ink);
background: rgba(25, 27, 31, 0.055);
}
.controls button:active {
transform: scale(0.97);
}
.controls button.active {
background: var(--ink);
color: #fffdf8;
}
.controls svg {
width: 15px;
height: 15px;
fill: none;
stroke: currentColor;
stroke-linecap: round;
stroke-linejoin: round;
stroke-width: 1.8;
}
.divider {
width: 1px;
height: 24px;
margin: 0 2px;
background: var(--line);
}
.help {
position: absolute;
right: clamp(22px, 4vw, 52px);
bottom: clamp(26px, 4vw, 46px);
color: var(--muted);
font-size: 9px;
font-weight: 700;
letter-spacing: 0.08em;
text-align: right;
line-height: 1.6;
text-transform: uppercase;
}
@media (max-width: 760px) {
.brand p {
max-width: 280px;
}
.count-card {
width: 155px;
padding: 13px;
}
.count-row strong {
font-size: 38px;
}
.inventory {
gap: 3px;
}
.legend,
.help {
display: none;
}
.controls {
width: calc(100% - 24px);
justify-content: center;
}
.controls button {
flex: 1;
padding: 0 8px;
}
.controls button span {
display: none;
}
.layout-tag {
top: 172px;
}
}
@media (max-width: 480px) {
.brand {
top: 20px;
left: 18px;
}
.brand p {
display: none;
}
h1 {
max-width: 210px;
font-size: 30px;
}
.count-card {
top: 16px;
right: 14px;
width: 118px;
}
.count-top {
font-size: 8px;
}
.count-top .stable-word {
display: none;
}
.count-row strong {
font-size: 34px;
}
#proof {
font-size: 8px;
}
.inventory {
grid-template-columns: repeat(8, 1fr);
}
.layout-tag {
top: 125px;
}
}
</style>
</head>
<body>
<main id="stage" aria-label="Interactive 3D arrangement of 24 numbered objects"></main>
<div class="wash"></div>
<div class="interface">
<section class="brand">
<div class="eyebrow">Conservation of count</div>
<h1>Same objects.<br />Different arrangement.</h1>
<p>
Every numbered object keeps its identity while the group moves.
Nothing is created, removed, or replaced.
</p>
</section>
<section class="count-card" id="countCard" aria-live="polite">
<div class="count-top">
<span>Live inventory</span>
<span><i class="stable-dot"></i><span class="stable-word">stable</span></span>
</div>
<div class="count-row">
<strong id="count">24</strong>
<span>objects</span>
</div>
<div id="proof">24 tracked · +0 · −0</div>
<div class="inventory" id="inventory" aria-label="Inventory of 24 objects"></div>
</section>
<div class="layout-tag">
<b></b>
<span id="layoutLabel">Spread arrangement</span>
</div>
<aside class="legend" aria-label="Explanation">
<div><span>01</span> Keep the same marked identities</div>
<div><span>02</span> Change only their positions</div>
<div><span>03</span> Preserve the total count</div>
</aside>
<nav class="controls" aria-label="Arrangement controls">
<button id="spreadButton" class="active" aria-pressed="true">
<svg viewBox="0 0 24 24" aria-hidden="true">
<rect x="3" y="4" width="4" height="4" rx="1"></rect>
<rect x="10" y="4" width="4" height="4" rx="1"></rect>
<rect x="17" y="4" width="4" height="4" rx="1"></rect>
<rect x="3" y="14" width="4" height="4" rx="1"></rect>
<rect x="10" y="14" width="4" height="4" rx="1"></rect>
<rect x="17" y="14" width="4" height="4" rx="1"></rect>
</svg>
<span>Spread</span>
</button>
<button id="clusterButton" aria-pressed="false">
<svg viewBox="0 0 24 24" aria-hidden="true">
<circle cx="12" cy="12" r="2"></circle>
<circle cx="7" cy="8" r="2"></circle>
<circle cx="17" cy="8" r="2"></circle>
<circle cx="7" cy="16" r="2"></circle>
<circle cx="17" cy="16" r="2"></circle>
<circle cx="12" cy="5" r="2"></circle>
<circle cx="12" cy="19" r="2"></circle>
</svg>
<span>Cluster</span>
</button>
<div class="divider"></div>
<button id="autoButton" class="active" aria-pressed="true">
<svg viewBox="0 0 24 24" aria-hidden="true">
<path d="M4 12a8 8 0 0 1 13.7-5.7L20 8"></path>
<path d="M20 4v4h-4"></path>
<path d="M20 12a8 8 0 0 1-13.7 5.7L4 16"></path>
<path d="M4 20v-4h4"></path>
</svg>
<span id="autoText">Auto: on</span>
</button>
</nav>
<div class="help">
Drag to orbit · Scroll / pinch to zoom<br />
Double-click to reset camera
</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 COUNT = 24;
const reducedMotion = matchMedia("(prefers-reduced-motion: reduce)").matches;
const palette = [
0xff654f,
0x3f7cff,
0xa9d43b,
0x8b63e8,
0xf3b63f,
0x27aa9a
];
const stage = document.querySelector("#stage");
const countCard = document.querySelector("#countCard");
const inventory = document.querySelector("#inventory");
const layoutLabel = document.querySelector("#layoutLabel");
const proof = document.querySelector("#proof");
const spreadButton = document.querySelector("#spreadButton");
const clusterButton = document.querySelector("#clusterButton");
const autoButton = document.querySelector("#autoButton");
const autoText = document.querySelector("#autoText");
document.querySelector("#count").textContent = COUNT;
for (let i = 0; i < COUNT; i++) {
const chip = document.createElement("i");
const color = new THREE.Color(palette[i % palette.length]);
chip.style.background = `#${color.getHexString()}`;
chip.title = `Object ${String(i + 1).padStart(2, "0")}`;
inventory.appendChild(chip);
}
// Scene and renderer.
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xf3efe7);
scene.fog = new THREE.Fog(0xf3efe7, 16, 34);
const camera = new THREE.PerspectiveCamera(
42,
innerWidth / innerHeight,
0.1,
100
);
const renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setPixelRatio(Math.min(devicePixelRatio, 2));
renderer.setSize(innerWidth, innerHeight);
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.08;
stage.appendChild(renderer.domElement);
const hemisphere = new THREE.HemisphereLight(0xfffbf2, 0x9b958b, 2.25);
scene.add(hemisphere);
const keyLight = new THREE.DirectionalLight(0xffffff, 3.2);
keyLight.position.set(5.5, 10, 6);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(2048, 2048);
keyLight.shadow.camera.left = -9;
keyLight.shadow.camera.right = 9;
keyLight.shadow.camera.top = 9;
keyLight.shadow.camera.bottom = -9;
keyLight.shadow.camera.near = 0.5;
keyLight.shadow.camera.far = 30;
keyLight.shadow.bias = -0.0005;
keyLight.shadow.normalBias = 0.025;
scene.add(keyLight);
const rimLight = new THREE.DirectionalLight(0xb7cfff, 1.15);
rimLight.position.set(-7, 5, -7);
scene.add(rimLight);
const ground = new THREE.Mesh(
new THREE.PlaneGeometry(50, 50),
new THREE.MeshStandardMaterial({
color: 0xe9e4da,
roughness: 0.94,
metalness: 0
})
);
ground.rotation.x = -Math.PI / 2;
ground.receiveShadow = true;
scene.add(ground);
const grid = new THREE.GridHelper(30, 60, 0xc4bdae, 0xd4cec2);
grid.position.y = 0.006;
grid.material.transparent = true;
grid.material.opacity = 0.22;
grid.material.depthWrite = false;
scene.add(grid);
// The two destination outlines.
function createSpreadOutline() {
const points = [
new THREE.Vector3(-4.28, 0.016, -1.72),
new THREE.Vector3(4.28, 0.016, -1.72),
new THREE.Vector3(4.28, 0.016, 1.72),
new THREE.Vector3(-4.28, 0.016, 1.72)
];
const geometry = new THREE.BufferGeometry().setFromPoints(points);
const material = new THREE.LineBasicMaterial({
color: 0x6e6961,
transparent: true,
opacity: 0.34,
depthWrite: false
});
return new THREE.LineLoop(geometry, material);
}
function createClusterOutline() {
const points = [];
const radius = 2.28;
for (let i = 0; i < 96; i++) {
const angle = (i / 96) * Math.PI * 2;
points.push(
new THREE.Vector3(
Math.cos(angle) * radius,
0.017,
Math.sin(angle) * radius
)
);
}
const geometry = new THREE.BufferGeometry().setFromPoints(points);
const material = new THREE.LineBasicMaterial({
color: 0xff654f,
transparent: true,
opacity: 0.08,
depthWrite: false
});
return new THREE.LineLoop(geometry, material);
}
const spreadOutline = createSpreadOutline();
const clusterOutline = createClusterOutline();
scene.add(spreadOutline, clusterOutline);
// Precompute both arrangements.
const spreadPositions = [];
const clusterPositions = [];
const goldenAngle = Math.PI * (3 - Math.sqrt(5));
for (let i = 0; i < COUNT; i++) {
const row = Math.floor(i / 8);
const column = i % 8;
spreadPositions.push(
new THREE.Vector3(
(column - 3.5) * 1.08,
0,
(row - 1) * 1.08
)
);
const radius = i === 0 ? 0 : 0.42 * Math.sqrt(i);
const angle = i * goldenAngle;
clusterPositions.push(
new THREE.Vector3(
Math.cos(angle) * radius,
0,
Math.sin(angle) * radius
)
);
}
// Reusable geometry.
const baseGeometry = new THREE.CylinderGeometry(0.205, 0.215, 0.12, 24);
const bodyGeometry = new THREE.CylinderGeometry(0.165, 0.185, 0.48, 24);
const capGeometry = new THREE.SphereGeometry(0.18, 24, 16);
const bandGeometry = new THREE.TorusGeometry(0.178, 0.027, 10, 32);
const baseMaterial = new THREE.MeshStandardMaterial({
color: 0x26282d,
roughness: 0.7
});
const bandMaterial = new THREE.MeshStandardMaterial({
color: 0xfff9ed,
roughness: 0.56
});
function createNumberTexture(number) {
const canvas = document.createElement("canvas");
canvas.width = 128;
canvas.height = 128;
const context = canvas.getContext("2d");
context.clearRect(0, 0, 128, 128);
context.beginPath();
context.arc(64, 64, 52, 0, Math.PI * 2);
context.fillStyle = "#fffdf7";
context.fill();
context.lineWidth = 7;
context.strokeStyle = "#202226";
context.stroke();
context.fillStyle = "#202226";
context.font = "750 58px system-ui, sans-serif";
context.textAlign = "center";
context.textBaseline = "middle";
context.fillText(
String(number).padStart(2, "0"),
64,
67
);
const texture = new THREE.CanvasTexture(canvas);
texture.colorSpace = THREE.SRGBColorSpace;
texture.anisotropy = Math.min(
4,
renderer.capabilities.getMaxAnisotropy()
);
return texture;
}
const objects = [];
for (let i = 0; i < COUNT; i++) {
const group = new THREE.Group();
const color = new THREE.Color(palette[i % palette.length]);
const bodyMaterial = new THREE.MeshStandardMaterial({
color,
roughness: 0.38,
metalness: 0.04
});
const base = new THREE.Mesh(baseGeometry, baseMaterial);
base.position.y = 0.06;
base.castShadow = true;
base.receiveShadow = true;
group.add(base);
const body = new THREE.Mesh(bodyGeometry, bodyMaterial);
body.position.y = 0.36;
body.castShadow = true;
body.receiveShadow = true;
group.add(body);
const cap = new THREE.Mesh(capGeometry, bodyMaterial);
cap.position.y = 0.6;
cap.castShadow = true;
group.add(cap);
const band = new THREE.Mesh(bandGeometry, bandMaterial);
band.rotation.x = Math.PI / 2;
band.position.y = 0.26;
band.castShadow = true;
group.add(band);
const label = new THREE.Sprite(
new THREE.SpriteMaterial({
map: createNumberTexture(i + 1),
transparent: true,
depthTest: false,
depthWrite: false,
toneMapped: false
})
);
label.position.set(0, 0.43, 0.205);
label.scale.set(0.34, 0.34, 0.34);
label.renderOrder = 10;
group.add(label);
group.position.copy(spreadPositions[i]);
scene.add(group);
objects.push({
group,
from: spreadPositions[i].clone(),
to: spreadPositions[i].clone(),
rotationFrom: 0,
rotationTo: 0
});
}
// Motion state.
let currentLayout = "spread";
let desiredLayout = "spread";
let motionActive = false;
let motionStart = 0;
let motionDuration = reducedMotion ? 1 : 1350;
let autoEnabled = !reducedMotion;
let nextAutoAt = performance.now() + (reducedMotion ? Infinity : 3000);
function setAuto(enabled) {
autoEnabled = enabled;
autoButton.classList.toggle("active", enabled);
autoButton.setAttribute("aria-pressed", String(enabled));
autoText.textContent = `Auto: ${enabled ? "on" : "off"}`;
if (enabled) {
nextAutoAt = performance.now() + 3400;
}
}
function updateLayoutUI() {
layoutLabel.textContent =
desiredLayout === "spread"
? "Spread arrangement"
: "Clustered arrangement";
spreadButton.classList.toggle("active", desiredLayout === "spread");
clusterButton.classList.toggle("active", desiredLayout === "cluster");
spreadButton.setAttribute("aria-pressed", String(desiredLayout === "spread"));
clusterButton.setAttribute("aria-pressed", String(desiredLayout === "cluster"));
if (motionActive) {
inventory.classList.add("moving");
proof.textContent = "24 tracked · +0 · −0";
} else {
inventory.classList.remove("moving");
proof.textContent = "24 tracked · +0 · −0";
}
}
function setLayout(layout, fromAuto = false) {
if (!fromAuto) setAuto(false);
desiredLayout = layout;
motionStart = performance.now();
motionDuration = reducedMotion ? 1 : 1350;
motionActive = true;
const targets = layout === "spread" ? spreadPositions : clusterPositions;
objects.forEach((object, index) => {
object.from.copy(object.group.position);
object.to.copy(targets[index]);
object.rotationFrom = object.group.rotation.y;
object.rotationTo = 0;
});
if (autoEnabled) {
nextAutoAt = performance.now() + motionDuration + 3600;
}
updateLayoutUI();
}
spreadButton.addEventListener("click", () => setLayout("spread"));
clusterButton.addEventListener("click", () => setLayout("cluster"));
autoButton.addEventListener("click", () => {
const next = !autoEnabled;
setAuto(next);
if (next && !motionActive) {
setLayout(currentLayout === "spread" ? "cluster" : "spread", true);
}
});
function easeInOutCubic(value) {
return value < 0.5
? 4 * value * value * value
: 1 - Math.pow(-2 * value + 2, 3) / 2;
}
function updateObjects(now) {
if (!motionActive) return;
const elapsed = now - motionStart;
objects.forEach((object, index) => {
const stagger = (index % 8) * 15;
const local = THREE.MathUtils.clamp(
(elapsed - stagger) / Math.max(1, motionDuration - 110),
0,
1
);
const eased = easeInOutCubic(local);
object.group.position.lerpVectors(object.from, object.to, eased);
const arcHeight = 0.42 + (index % 4) * 0.08;
object.group.position.y = Math.sin(Math.PI * eased) * arcHeight;
const spin = Math.sin(Math.PI * eased) * 0.28;
object.group.rotation.y =
object.rotationFrom * (1 - eased) + object.rotationTo * eased + spin;
const scale = 1 + Math.sin(Math.PI * eased) * 0.035;
object.group.scale.setScalar(scale);
});
const progress = THREE.MathUtils.clamp(elapsed / motionDuration, 0, 1);
spreadOutline.material.opacity = 0.34 * (1 - progress) + 0.08 * progress;
clusterOutline.material.opacity = 0.08 * (1 - progress) + 0.34 * progress;
if (elapsed >= motionDuration) {
motionActive = false;
currentLayout = desiredLayout;
objects.forEach((object, index) => {
object.group.position.copy(
desiredLayout === "spread"
? spreadPositions[index]
: clusterPositions[index]
);
object.group.position.y = 0;
object.group.rotation.y = 0;
object.group.scale.setScalar(1);
});
spreadOutline.material.opacity =
desiredLayout === "spread" ? 0.34 : 0.08;
clusterOutline.material.opacity =
desiredLayout === "cluster" ? 0.34 : 0.08;
countCard.classList.remove("verified");
void countCard.offsetWidth;
countCard.classList.add("verified");
updateLayoutUI();
}
}
// Camera controls implemented directly without external addons.
const orbit = {
theta: 0.56,
phi: 0.88,
radius: 12,
targetTheta: 0.56,
targetPhi: 0.88,
targetRadius: 12
};
const pointers = new Map();
let pinchDistance = 0;
function resetCamera() {
const aspect = innerWidth / innerHeight;
orbit.targetTheta = 0.56;
orbit.targetPhi = aspect < 0.75 ? 1.04 : 0.88;
orbit.targetRadius = aspect < 0.75 ? 19 : aspect < 1.15 ? 15 : 12;
}
function pointerDistance() {
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 });
stage.classList.add("dragging");
if (pointers.size === 2) {
pinchDistance = pointerDistance();
}
});
renderer.domElement.addEventListener("pointermove", (event) => {
if (!pointers.has(event.pointerId)) return;
const previous = pointers.get(event.pointerId);
pointers.set(event.pointerId, { x: event.clientX, y: event.clientY });
if (pointers.size === 1) {
const dx = event.clientX - previous.x;
const dy = event.clientY - previous.y;
orbit.targetTheta -= dx * 0.006;
orbit.targetPhi = THREE.MathUtils.clamp(
orbit.targetPhi - dy * 0.005,
0.36,
1.32
);
} else if (pointers.size === 2) {
const distance = pointerDistance();
if (pinchDistance > 0 && distance > 0) {
orbit.targetRadius = THREE.MathUtils.clamp(
orbit.targetRadius * (pinchDistance / distance),
7,
25
);
}
pinchDistance = distance;
}
});
function releasePointer(event) {
pointers.delete(event.pointerId);
pinchDistance = pointers.size === 2 ? pointerDistance() : 0;
if (!pointers.size) stage.classList.remove("dragging");
}
renderer.domElement.addEventListener("pointerup", releasePointer);
renderer.domElement.addEventListener("pointercancel", releasePointer);
renderer.domElement.addEventListener("lostpointercapture", releasePointer);
renderer.domElement.addEventListener(
"wheel",
(event) => {
event.preventDefault();
orbit.targetRadius = THREE.MathUtils.clamp(
orbit.targetRadius * Math.exp(event.deltaY * 0.001),
7,
25
);
},
{ passive: false }
);
renderer.domElement.addEventListener("dblclick", resetCamera);
window.addEventListener("keydown", (event) => {
if (event.target instanceof HTMLButtonElement) return;
if (event.key === "1") setLayout("spread");
if (event.key === "2") setLayout("cluster");
if (event.key.toLowerCase() === "r") resetCamera();
if (event.code === "Space") {
event.preventDefault();
autoButton.click();
}
if (event.key === "ArrowLeft") orbit.targetTheta -= 0.12;
if (event.key === "ArrowRight") orbit.targetTheta += 0.12;
if (event.key === "ArrowUp") orbit.targetPhi -= 0.08;
if (event.key === "ArrowDown") orbit.targetPhi += 0.08;
});
window.addEventListener("resize", () => {
camera.aspect = innerWidth / innerHeight;
camera.updateProjectionMatrix();
renderer.setPixelRatio(Math.min(devicePixelRatio, 2));
renderer.setSize(innerWidth, innerHeight);
});
resetCamera();
function animate(now) {
requestAnimationFrame(animate);
updateObjects(now);
if (autoEnabled && !motionActive && now >= nextAutoAt) {
setLayout(currentLayout === "spread" ? "cluster" : "spread", true);
}
const damping = 0.085;
orbit.theta += (orbit.targetTheta - orbit.theta) * damping;
orbit.phi += (orbit.targetPhi - orbit.phi) * damping;
orbit.radius += (orbit.targetRadius - orbit.radius) * damping;
const sinPhi = Math.sin(orbit.phi);
camera.position.set(
orbit.radius * sinPhi * Math.sin(orbit.theta),
orbit.radius * Math.cos(orbit.phi),
orbit.radius * sinPhi * Math.cos(orbit.theta)
);
camera.lookAt(0, 0.25, 0);
renderer.render(scene, camera);
}
requestAnimationFrame(animate);
</script>
</body>
</html>
```