Prompt specification
Build or customize the Three.js 3D scene "Ordered data line with mean and median" 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: Ordered data line with mean and median
Category: Algorithms & Systems · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/ordered-data-line-with-mean-and-median-x1809
Scene Description Prompt:
Please use Three.js from cdnjs for a low-poly scene based on this idea: ordered data line with mean and median. Build a row of synthetic numerical tokens on one axis with separate mean and median markers; compute both from the displayed values. Return one self-contained HTML file.
Working Three.js Starter (ordered-data-line-with-mean-and-median-x1809):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Ordered Data — Mean & Median</title>
<style>
:root {
color-scheme: dark;
--ink: #edf6ff;
--muted: #91a7bd;
--panel: rgba(7, 17, 31, 0.76);
--border: rgba(151, 195, 226, 0.18);
--mean: #52e0d1;
--median: #ffba63;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background:
radial-gradient(circle at 20% 15%, rgba(37, 96, 138, 0.18), transparent 36%),
radial-gradient(circle at 80% 80%, rgba(91, 56, 135, 0.15), transparent 38%),
#07111f;
color: var(--ink);
font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
#scene-container {
position: fixed;
inset: 0;
}
#scene-container canvas {
display: block;
width: 100%;
height: 100%;
outline: none;
touch-action: none;
}
.hero,
.stats,
.legend,
.instructions,
#tooltip {
position: fixed;
z-index: 2;
pointer-events: none;
}
.hero {
top: 30px;
left: 34px;
width: min(520px, calc(100vw - 390px));
}
.eyebrow {
display: flex;
align-items: center;
gap: 10px;
margin-bottom: 13px;
color: #8cb2d1;
font-size: 11px;
font-weight: 750;
letter-spacing: 0.19em;
text-transform: uppercase;
}
.eyebrow::before {
width: 29px;
height: 1px;
content: "";
background: #72b6df;
box-shadow: 0 0 12px rgba(114, 182, 223, 0.8);
}
h1 {
margin: 0;
font-size: clamp(36px, 4.6vw, 64px);
font-weight: 720;
line-height: 0.98;
letter-spacing: -0.055em;
}
.hero p {
max-width: 480px;
margin: 18px 0 0;
color: var(--muted);
font-size: 14px;
line-height: 1.65;
}
.stats {
top: 30px;
right: 30px;
width: 286px;
padding: 18px;
border: 1px solid var(--border);
border-radius: 20px;
background: var(--panel);
box-shadow:
0 20px 60px rgba(0, 0, 0, 0.28),
inset 0 1px 0 rgba(255, 255, 255, 0.035);
backdrop-filter: blur(16px);
}
.stats-header {
display: flex;
align-items: center;
justify-content: space-between;
margin-bottom: 13px;
color: #9bb1c5;
font-size: 10px;
font-weight: 700;
letter-spacing: 0.16em;
text-transform: uppercase;
}
.live-dot {
display: inline-block;
width: 6px;
height: 6px;
margin-right: 6px;
border-radius: 50%;
background: #65e6b2;
box-shadow: 0 0 10px #65e6b2;
}
.stat {
display: grid;
grid-template-columns: 1fr auto;
align-items: end;
padding: 13px 0;
border-top: 1px solid rgba(151, 195, 226, 0.11);
}
.stat-name {
display: flex;
align-items: center;
gap: 8px;
color: #b5c7d8;
font-size: 12px;
font-weight: 650;
}
.stat-value {
color: var(--ink);
font-family: "SFMono-Regular", Consolas, "Liberation Mono", monospace;
font-size: 25px;
font-variant-numeric: tabular-nums;
letter-spacing: -0.05em;
}
.swatch {
width: 8px;
height: 8px;
border-radius: 2px;
transform: rotate(45deg);
}
.swatch.mean {
background: var(--mean);
box-shadow: 0 0 12px rgba(82, 224, 209, 0.65);
}
.swatch.median {
background: var(--median);
box-shadow: 0 0 12px rgba(255, 186, 99, 0.65);
}
.stat-note {
grid-column: 1 / -1;
margin-top: 6px;
color: #71899f;
font-size: 10px;
line-height: 1.45;
}
.center-note {
margin-top: 9px;
padding: 11px 12px;
border: 1px solid rgba(151, 195, 226, 0.11);
border-radius: 11px;
background: rgba(255, 255, 255, 0.025);
color: #91a7bd;
font-size: 11px;
line-height: 1.5;
}
.legend {
bottom: 28px;
left: 32px;
display: flex;
align-items: center;
gap: 19px;
color: #9bb0c4;
font-size: 11px;
}
.legend-item {
display: flex;
align-items: center;
gap: 8px;
}
.legend-symbol {
position: relative;
width: 16px;
height: 16px;
}
.legend-symbol.token::before {
position: absolute;
top: 3px;
left: 4px;
width: 9px;
height: 11px;
content: "";
clip-path: polygon(50% 0, 100% 25%, 100% 75%, 50% 100%, 0 75%, 0 25%);
background: #789df0;
}
.legend-symbol.mean::before,
.legend-symbol.median::before {
position: absolute;
top: 1px;
left: 7px;
width: 3px;
height: 15px;
content: "";
border-radius: 2px;
background: currentColor;
box-shadow: 0 0 9px currentColor;
}
.legend-symbol.mean {
color: var(--mean);
}
.legend-symbol.median {
color: var(--median);
}
.instructions {
right: 32px;
bottom: 30px;
color: #667e95;
font-size: 10px;
letter-spacing: 0.04em;
}
#tooltip {
display: none;
min-width: 82px;
padding: 9px 11px;
border: 1px solid rgba(159, 202, 233, 0.2);
border-radius: 9px;
background: rgba(7, 17, 31, 0.92);
box-shadow: 0 10px 30px rgba(0, 0, 0, 0.28);
transform: translate(13px, 13px);
backdrop-filter: blur(10px);
}
#tooltip span {
display: block;
margin-bottom: 2px;
color: #7992a9;
font-size: 9px;
font-weight: 700;
letter-spacing: 0.12em;
text-transform: uppercase;
}
#tooltip strong {
color: #eef7ff;
font-family: "SFMono-Regular", Consolas, monospace;
font-size: 16px;
}
@media (max-width: 760px) {
.hero {
top: 19px;
left: 19px;
width: calc(100vw - 38px);
}
.hero p {
display: none;
}
h1 {
max-width: 320px;
font-size: 37px;
}
.stats {
top: auto;
right: 14px;
bottom: 61px;
left: 14px;
display: grid;
width: auto;
grid-template-columns: repeat(3, 1fr);
gap: 0 12px;
padding: 11px 13px;
border-radius: 15px;
}
.stats-header,
.center-note {
display: none;
}
.stat {
display: block;
padding: 8px 0 3px;
}
.stat-value {
display: block;
margin-top: 6px;
font-size: 21px;
}
.stat-note {
display: none;
}
.legend {
bottom: 18px;
left: 17px;
gap: 12px;
font-size: 9px;
}
.instructions {
display: none;
}
}
</style>
</head>
<body>
<main id="scene-container"></main>
<header class="hero">
<div class="eyebrow">Ordered observations · 12 tokens</div>
<h1>The shape<br />between.</h1>
<p>
A synthetic data line arranged from low to high. Its summary markers show
where the mean and median fall—and how the upper values influence the center.
</p>
</header>
<aside class="stats" aria-live="polite" aria-label="Calculated summary statistics">
<div class="stats-header">
<span>Calculated statistics</span>
<span><i class="live-dot"></i>Live</span>
</div>
<div class="stat">
<div class="stat-name">
<i class="swatch mean"></i>
Arithmetic mean
</div>
<div class="stat-value" id="mean-value">—</div>
<div class="stat-note">Sum of the 12 labeled values divided by 12</div>
</div>
<div class="stat">
<div class="stat-name">
<i class="swatch median"></i>
Median
</div>
<div class="stat-value" id="median-value">—</div>
<div class="stat-note">Average of the 6th and 7th ordered values</div>
</div>
<div class="stat">
<div class="stat-name">Separation</div>
<div class="stat-value" id="delta-value">—</div>
<div class="stat-note">Absolute distance between the two markers</div>
</div>
<div class="center-note" id="center-note"></div>
</aside>
<footer class="legend" aria-hidden="true">
<div class="legend-item">
<span class="legend-symbol token"></span>
Ordered value
</div>
<div class="legend-item">
<span class="legend-symbol mean"></span>
Mean
</div>
<div class="legend-item">
<span class="legend-symbol median"></span>
Median
</div>
</footer>
<div class="instructions">Drag to orbit · Scroll to zoom · Double-click to reset</div>
<div id="tooltip"></div>
<script type="module">
import * as THREE from "https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// These are the exact values rendered and labeled on the tokens.
const sourceValues = [7, 15, 23, 30, 36, 41, 45, 49, 60, 76, 91, 98];
const displayedValues = [...sourceValues].sort((a, b) => a - b);
// Both statistics are calculated from the displayed, labeled values.
const mean =
displayedValues.reduce((sum, value) => sum + value, 0) / displayedValues.length;
const middleIndex = Math.floor(displayedValues.length / 2);
const median =
displayedValues.length % 2 === 0
? (displayedValues[middleIndex - 1] + displayedValues[middleIndex]) / 2
: displayedValues[middleIndex];
const separation = Math.abs(mean - median);
const formatNumber = (number) =>
Number.isInteger(number) ? number.toFixed(0) : number.toFixed(2);
document.getElementById("mean-value").textContent = formatNumber(mean);
document.getElementById("median-value").textContent = formatNumber(median);
document.getElementById("delta-value").textContent = formatNumber(separation);
document.getElementById("center-note").textContent =
mean > median
? "The larger values pull the mean to the right of the median."
: mean < median
? "The smaller values pull the mean to the left of the median."
: "The mean and median coincide for this distribution.";
const container = document.getElementById("scene-container");
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x07111f);
scene.fog = new THREE.Fog(0x07111f, 55, 95);
const camera = new THREE.PerspectiveCamera(
40,
window.innerWidth / window.innerHeight,
0.1,
120
);
const renderer = new THREE.WebGLRenderer({
antialias: 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.12;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.domElement.tabIndex = 0;
renderer.domElement.setAttribute(
"aria-label",
"Interactive three-dimensional ordered data line with mean and median markers"
);
container.appendChild(renderer.domElement);
const hemisphere = new THREE.HemisphereLight(0xa9dcff, 0x111827, 1.7);
scene.add(hemisphere);
const ambient = new THREE.AmbientLight(0xffffff, 0.28);
scene.add(ambient);
const keyLight = new THREE.DirectionalLight(0xffffff, 3.1);
keyLight.position.set(5, 11, 8);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(2048, 2048);
keyLight.shadow.camera.left = -12;
keyLight.shadow.camera.right = 12;
keyLight.shadow.camera.top = 9;
keyLight.shadow.camera.bottom = -7;
keyLight.shadow.camera.near = 1;
keyLight.shadow.camera.far = 35;
keyLight.shadow.bias = -0.0004;
scene.add(keyLight);
const cyanLight = new THREE.PointLight(0x54e4d2, 16, 13, 2);
cyanLight.position.set(-6, 3.5, 3);
scene.add(cyanLight);
const violetLight = new THREE.PointLight(0x9b68ff, 14, 13, 2);
violetLight.position.set(6, 3, -2);
scene.add(violetLight);
const floor = new THREE.Mesh(
new THREE.PlaneGeometry(38, 24),
new THREE.MeshStandardMaterial({
color: 0x081421,
roughness: 0.98,
metalness: 0.02
})
);
floor.rotation.x = -Math.PI / 2;
floor.position.y = -0.18;
floor.receiveShadow = true;
scene.add(floor);
const grid = new THREE.GridHelper(32, 32, 0x1a405b, 0x10293c);
grid.position.y = -0.165;
grid.material.transparent = true;
grid.material.opacity = 0.28;
scene.add(grid);
const platform = new THREE.Mesh(
new THREE.CylinderGeometry(8.25, 8.6, 0.18, 12, 1, false),
new THREE.MeshStandardMaterial({
color: 0x0d2234,
roughness: 0.84,
metalness: 0.12,
flatShading: true
})
);
platform.scale.z = 0.43;
platform.position.y = -0.06;
platform.receiveShadow = true;
scene.add(platform);
const track = new THREE.Mesh(
new THREE.BoxGeometry(15.55, 0.14, 0.58),
new THREE.MeshStandardMaterial({
color: 0x19334a,
roughness: 0.48,
metalness: 0.42,
emissive: 0x071522,
emissiveIntensity: 0.45
})
);
track.position.y = 0.14;
track.castShadow = true;
track.receiveShadow = true;
scene.add(track);
const valueToX = (value) => (value - 50) * 0.15;
function roundedRect(context, x, y, width, height, radius) {
context.beginPath();
context.moveTo(x + radius, y);
context.lineTo(x + width - radius, y);
context.quadraticCurveTo(x + width, y, x + width, y + radius);
context.lineTo(x + width, y + height - radius);
context.quadraticCurveTo(
x + width,
y + height,
x + width - radius,
y + height
);
context.lineTo(x + radius, y + height);
context.quadraticCurveTo(x, y + height, x, y + height - radius);
context.lineTo(x, y + radius);
context.quadraticCurveTo(x, y, x + radius, y);
context.closePath();
}
function makeLabel(text, options = {}) {
const {
color = "#e8f4ff",
background = "rgba(5, 14, 27, 0.88)",
border = "rgba(154, 198, 229, 0.19)",
fontSize = 50,
bold = false,
scale = [0.7, 0.22]
} = options;
const canvas = document.createElement("canvas");
canvas.width = 512;
canvas.height = 160;
const context = canvas.getContext("2d");
roundedRect(context, 4, 4, 504, 152, 24);
context.fillStyle = background;
context.fill();
context.lineWidth = 4;
context.strokeStyle = border;
context.stroke();
context.fillStyle = color;
context.font = `${bold ? 750 : 600} ${fontSize}px "SFMono-Regular", Consolas, monospace`;
context.textAlign = "center";
context.textBaseline = "middle";
context.fillText(text, 256, 82);
const texture = new THREE.CanvasTexture(canvas);
texture.colorSpace = THREE.SRGBColorSpace;
texture.anisotropy = renderer.capabilities.getMaxAnisotropy();
const material = new THREE.SpriteMaterial({
map: texture,
transparent: true,
depthWrite: false,
depthTest: false
});
const sprite = new THREE.Sprite(material);
sprite.scale.set(scale[0], scale[1], 1);
return sprite;
}
// Axis ticks and numeric scale.
for (let tick = 0; tick <= 100; tick += 10) {
const major = tick % 20 === 0;
const tickMesh = new THREE.Mesh(
new THREE.BoxGeometry(major ? 0.045 : 0.028, 0.04, major ? 0.82 : 0.56),
new THREE.MeshStandardMaterial({
color: major ? 0x6c9bb9 : 0x36566e,
emissive: major ? 0x1c4059 : 0x102536,
emissiveIntensity: 0.45,
roughness: 0.55,
metalness: 0.35
})
);
tickMesh.position.set(valueToX(tick), 0.235, 0);
scene.add(tickMesh);
if (major) {
const label = makeLabel(String(tick), {
color: "#8fa9bf",
background: "rgba(5, 14, 27, 0.58)",
border: "rgba(154, 198, 229, 0.10)",
fontSize: 45,
scale: [0.54, 0.17]
});
label.position.set(valueToX(tick), 0.03, 1.05);
label.renderOrder = 2;
scene.add(label);
}
}
const tokenGeometry = new THREE.CylinderGeometry(0.2, 0.26, 0.7, 6, 1, false);
const ringGeometry = new THREE.TorusGeometry(0.29, 0.025, 4, 12);
const tokenMeshes = [];
displayedValues.forEach((value, index) => {
const x = valueToX(value);
const color = new THREE.Color().setHSL(
0.59 + (index / (displayedValues.length - 1)) * 0.13,
0.7,
0.64
);
const material = new THREE.MeshStandardMaterial({
color,
emissive: color,
emissiveIntensity: 0.08,
roughness: 0.38,
metalness: 0.22,
flatShading: true
});
const token = new THREE.Mesh(tokenGeometry, material);
token.position.set(x, 0.58, 0);
token.castShadow = true;
token.receiveShadow = true;
token.userData.value = value;
token.userData.rank = index + 1;
tokenMeshes.push(token);
scene.add(token);
const ringMaterial = new THREE.MeshStandardMaterial({
color,
emissive: color,
emissiveIntensity: 0.2,
roughness: 0.35,
metalness: 0.35
});
const ring = new THREE.Mesh(ringGeometry, ringMaterial);
ring.rotation.x = Math.PI / 2;
ring.position.set(x, 0.235, 0);
scene.add(ring);
const label = makeLabel(String(value), {
color: "#e6f2ff",
background: "rgba(6, 16, 30, 0.82)",
border: "rgba(130, 181, 221, 0.16)",
fontSize: 52,
bold: true,
scale: [0.5, 0.156]
});
// Alternating heights keep adjacent numeric labels readable.
label.position.set(x, 1.16 + (index % 2) * 0.27, index % 2 ? -0.08 : 0.08);
label.renderOrder = 4;
scene.add(label);
});
function createSummaryMarker(value, title, color, height, z, labelHeight) {
const group = new THREE.Group();
group.position.set(valueToX(value), 0, z);
const markerMaterial = new THREE.MeshStandardMaterial({
color,
emissive: color,
emissiveIntensity: 0.75,
roughness: 0.3,
metalness: 0.28,
flatShading: true
});
const glowMaterial = new THREE.MeshBasicMaterial({
color,
transparent: true,
opacity: 0.12,
depthWrite: false
});
const glow = new THREE.Mesh(
new THREE.BoxGeometry(0.17, height, 0.17),
glowMaterial
);
glow.position.y = 0.23 + height / 2;
group.add(glow);
const beam = new THREE.Mesh(
new THREE.BoxGeometry(0.065, height, 0.065),
markerMaterial
);
beam.position.y = 0.23 + height / 2;
beam.castShadow = true;
group.add(beam);
const cap = new THREE.Mesh(
new THREE.OctahedronGeometry(0.19, 0),
markerMaterial
);
cap.position.y = 0.23 + height;
cap.castShadow = true;
group.add(cap);
const base = new THREE.Mesh(
new THREE.TorusGeometry(0.31, 0.035, 4, 16),
markerMaterial
);
base.rotation.x = Math.PI / 2;
base.position.y = 0.26;
group.add(base);
const label = makeLabel(`${title} ${formatNumber(value)}`, {
color: `#${new THREE.Color(color).getHexString()}`,
background: "rgba(5, 14, 27, 0.94)",
border: `#${new THREE.Color(color).getHexString()}`,
fontSize: 45,
bold: true,
scale: [1.55, 0.48]
});
label.position.set(0, labelHeight, 0);
label.renderOrder = 8;
group.add(label);
scene.add(group);
}
createSummaryMarker(median, "MEDIAN", 0xffba63, 2.62, -0.42, 3.3);
createSummaryMarker(mean, "MEAN", 0x52e0d1, 3.25, 0.42, 4.02);
// Camera controls are implemented directly without Three.js addons.
const cameraTarget = new THREE.Vector3(0, 1.05, 0);
const controls = {
theta: 0,
phi: 1.22,
radius: 20,
targetTheta: 0,
targetPhi: 1.22,
targetRadius: 20
};
function fittedRadius() {
const halfWidth = 8.25;
const halfFov = THREE.MathUtils.degToRad(camera.fov * 0.5);
return THREE.MathUtils.clamp(
halfWidth / (Math.tan(halfFov) * camera.aspect),
18,
55
);
}
function resetCamera() {
controls.targetTheta = 0;
controls.targetPhi = 1.22;
controls.targetRadius = fittedRadius();
}
controls.radius = fittedRadius();
controls.targetRadius = controls.radius;
let hasInteracted = false;
let dragging = false;
let movedWhileDragging = false;
let previousPointerX = 0;
let previousPointerY = 0;
const raycaster = new THREE.Raycaster();
const pointer = new THREE.Vector2();
const tooltip = document.getElementById("tooltip");
let hoveredToken = null;
function setHoveredToken(nextToken, event) {
if (hoveredToken !== nextToken) {
if (hoveredToken) {
hoveredToken.material.emissiveIntensity = 0.08;
}
hoveredToken = nextToken;
if (hoveredToken) {
hoveredToken.material.emissiveIntensity = 0.48;
tooltip.innerHTML = `
<span>Token ${String(hoveredToken.userData.rank).padStart(2, "0")}</span>
<strong>${hoveredToken.userData.value}</strong>
`;
tooltip.style.display = "block";
tooltip.style.left = `${event.clientX}px`;
tooltip.style.top = `${event.clientY}px`;
} else {
tooltip.style.display = "none";
}
} else if (hoveredToken) {
tooltip.style.left = `${event.clientX}px`;
tooltip.style.top = `${event.clientY}px`;
}
}
renderer.domElement.addEventListener("pointerdown", (event) => {
if (event.button !== 0) return;
dragging = true;
movedWhileDragging = false;
previousPointerX = event.clientX;
previousPointerY = event.clientY;
renderer.domElement.setPointerCapture(event.pointerId);
tooltip.style.display = "none";
});
renderer.domElement.addEventListener("pointermove", (event) => {
if (dragging) {
const deltaX = event.clientX - previousPointerX;
const deltaY = event.clientY - previousPointerY;
if (Math.abs(deltaX) + Math.abs(deltaY) > 2) {
movedWhileDragging = true;
hasInteracted = true;
}
controls.targetTheta -= deltaX * 0.006;
controls.targetPhi = THREE.MathUtils.clamp(
controls.targetPhi + deltaY * 0.005,
0.65,
1.43
);
previousPointerX = event.clientX;
previousPointerY = event.clientY;
return;
}
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);
const intersections = raycaster.intersectObjects(tokenMeshes, false);
const nextToken = intersections.length ? intersections[0].object : null;
setHoveredToken(nextToken, event);
});
function endDrag(event) {
if (!dragging) return;
dragging = false;
if (renderer.domElement.hasPointerCapture(event.pointerId)) {
renderer.domElement.releasePointerCapture(event.pointerId);
}
}
renderer.domElement.addEventListener("pointerup", endDrag);
renderer.domElement.addEventListener("pointercancel", endDrag);
renderer.domElement.addEventListener("pointerleave", () => {
if (!dragging) setHoveredToken(null, { clientX: 0, clientY: 0 });
});
renderer.domElement.addEventListener(
"wheel",
(event) => {
event.preventDefault();
hasInteracted = true;
controls.targetRadius = THREE.MathUtils.clamp(
controls.targetRadius + event.deltaY * 0.012,
11,
62
);
},
{ passive: false }
);
renderer.domElement.addEventListener("dblclick", () => {
hasInteracted = false;
resetCamera();
});
renderer.domElement.addEventListener("keydown", (event) => {
let handled = true;
switch (event.key) {
case "ArrowLeft":
controls.targetTheta += 0.12;
break;
case "ArrowRight":
controls.targetTheta -= 0.12;
break;
case "ArrowUp":
controls.targetPhi = Math.max(0.65, controls.targetPhi - 0.08);
break;
case "ArrowDown":
controls.targetPhi = Math.min(1.43, controls.targetPhi + 0.08);
break;
case "+":
case "=":
controls.targetRadius = Math.max(11, controls.targetRadius - 1.5);
break;
case "-":
controls.targetRadius = Math.min(62, controls.targetRadius + 1.5);
break;
default:
handled = false;
}
if (handled) {
event.preventDefault();
hasInteracted = true;
}
});
function resize() {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
if (!hasInteracted) {
controls.targetRadius = fittedRadius();
} else {
controls.targetRadius = Math.max(controls.targetRadius, fittedRadius());
}
}
window.addEventListener("resize", resize);
function animate() {
requestAnimationFrame(animate);
const damping = 0.085;
controls.theta += (controls.targetTheta - controls.theta) * damping;
controls.phi += (controls.targetPhi - controls.phi) * damping;
controls.radius += (controls.targetRadius - controls.radius) * damping;
const sinPhi = Math.sin(controls.phi);
camera.position.set(
cameraTarget.x + controls.radius * sinPhi * Math.sin(controls.theta),
cameraTarget.y + controls.radius * Math.cos(controls.phi),
cameraTarget.z + controls.radius * sinPhi * Math.cos(controls.theta)
);
camera.lookAt(cameraTarget);
renderer.render(scene, camera);
}
animate();
</script>
</body>
</html>
```