Prompt specification
Build or customize the Three.js 3D scene "Satellite Velocity and Acceleration Arrows" 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: Satellite Velocity and Acceleration Arrows
Category: Space & Earth · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/satellite-velocity-and-acceleration-arrows-x3797
Scene Description Prompt:
A low-poly satellite in ideal circular orbit shows tangential velocity and inward acceleration as distinct arrows. Standalone HTML with Three.js from cdnjs.
Working Three.js Starter (satellite-velocity-and-acceleration-arrows-x3797):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Satellite in Circular Orbit</title>
<style>
:root {
color-scheme: dark;
--cyan: #35e7ff;
--orange: #ff8a3d;
--text: #edf5ff;
--muted: #9eb0ca;
--panel: rgba(7, 13, 29, 0.78);
--border: rgba(160, 195, 235, 0.18);
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #050916;
color: var(--text);
font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
#scene {
position: fixed;
inset: 0;
}
canvas {
display: block;
width: 100%;
height: 100%;
touch-action: none;
cursor: grab;
}
canvas:active {
cursor: grabbing;
}
.panel {
position: fixed;
z-index: 5;
top: 20px;
left: 20px;
width: min(350px, calc(100vw - 40px));
padding: 18px;
border: 1px solid var(--border);
border-radius: 16px;
background: var(--panel);
box-shadow: 0 18px 60px rgba(0, 0, 0, 0.32);
backdrop-filter: blur(14px);
-webkit-backdrop-filter: blur(14px);
pointer-events: none;
}
.eyebrow {
margin-bottom: 6px;
color: var(--cyan);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.18em;
text-transform: uppercase;
}
h1 {
margin: 0 0 16px;
font-size: clamp(22px, 4vw, 30px);
line-height: 1.08;
letter-spacing: -0.035em;
}
.legend {
display: grid;
gap: 9px;
padding: 12px;
border: 1px solid rgba(255, 255, 255, 0.07);
border-radius: 11px;
background: rgba(255, 255, 255, 0.025);
}
.legend-row {
display: flex;
align-items: center;
gap: 10px;
color: #c9d6e8;
font-size: 13px;
}
.swatch {
position: relative;
width: 29px;
height: 4px;
flex: 0 0 auto;
border-radius: 999px;
background: var(--cyan);
box-shadow: 0 0 12px var(--cyan);
}
.swatch::after {
position: absolute;
top: 50%;
right: -2px;
width: 0;
height: 0;
border-top: 5px solid transparent;
border-bottom: 5px solid transparent;
border-left: 8px solid var(--cyan);
content: "";
transform: translateY(-50%);
}
.swatch.orange {
background: var(--orange);
box-shadow: 0 0 12px var(--orange);
}
.swatch.orange::after {
border-left-color: var(--orange);
}
.legend-row code {
display: inline-grid;
width: 22px;
height: 22px;
place-items: center;
margin-left: auto;
border-radius: 6px;
background: rgba(255, 255, 255, 0.07);
color: #fff;
font-family: Georgia, serif;
font-style: italic;
}
.stats {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 8px;
margin-top: 12px;
}
.stat {
padding: 10px;
border: 1px solid rgba(255, 255, 255, 0.06);
border-radius: 9px;
background: rgba(255, 255, 255, 0.025);
}
.stat span {
display: block;
margin-bottom: 4px;
color: var(--muted);
font-size: 9px;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.stat strong {
font-size: 12px;
font-weight: 700;
white-space: nowrap;
}
.note {
margin: 12px 0 0;
color: var(--muted);
font-size: 11px;
line-height: 1.45;
}
.actions {
position: fixed;
z-index: 6;
top: 20px;
right: 20px;
display: flex;
gap: 8px;
}
button {
padding: 9px 13px;
border: 1px solid var(--border);
border-radius: 9px;
background: rgba(7, 13, 29, 0.76);
color: #dceaff;
font: inherit;
font-size: 12px;
cursor: pointer;
backdrop-filter: blur(12px);
-webkit-backdrop-filter: blur(12px);
}
button:hover {
border-color: rgba(53, 231, 255, 0.5);
background: rgba(16, 31, 53, 0.9);
}
.vector-label {
position: fixed;
z-index: 4;
display: none;
padding: 5px 8px;
border: 1px solid currentColor;
border-radius: 7px;
background: rgba(4, 9, 20, 0.84);
box-shadow: 0 0 18px rgba(0, 0, 0, 0.3);
color: var(--cyan);
font-size: 11px;
font-weight: 700;
letter-spacing: 0.02em;
white-space: nowrap;
pointer-events: none;
transform: translate(-50%, -50%);
}
.vector-label b {
margin-right: 4px;
font-family: Georgia, serif;
font-size: 14px;
font-style: italic;
}
.vector-label.acceleration {
color: var(--orange);
}
.help {
position: fixed;
z-index: 5;
right: 20px;
bottom: 18px;
padding: 8px 11px;
border: 1px solid var(--border);
border-radius: 9px;
background: rgba(7, 13, 29, 0.65);
color: var(--muted);
font-size: 11px;
pointer-events: none;
}
@media (max-width: 760px) {
.panel {
top: 12px;
left: 12px;
width: min(330px, calc(100vw - 24px));
padding: 14px;
}
.actions {
top: auto;
right: 12px;
bottom: 12px;
}
.help {
display: none;
}
.stats {
gap: 5px;
}
.stat {
padding: 8px;
}
.stat strong {
font-size: 10px;
}
}
</style>
</head>
<body>
<div id="scene"></div>
<section class="panel" aria-label="Orbit information">
<div class="eyebrow">Circular Orbit / Vector View</div>
<h1>Satellite in Motion</h1>
<div class="legend">
<div class="legend-row">
<span class="swatch"></span>
<span>Tangential velocity</span>
<code>v</code>
</div>
<div class="legend-row">
<span class="swatch orange"></span>
<span>Inward acceleration</span>
<code>a</code>
</div>
</div>
<div class="stats">
<div class="stat">
<span>Radius</span>
<strong>6,800 km</strong>
</div>
<div class="stat">
<span>Speed</span>
<strong>7.66 km/s</strong>
</div>
<div class="stat">
<span>Acceleration</span>
<strong>8.62 m/s²</strong>
</div>
</div>
<p class="note">
Velocity stays tangent to the orbit. Acceleration points radially toward
Earth, continually bending the satellite's path.
</p>
</section>
<div class="actions">
<button id="pauseButton" type="button">Pause</button>
<button id="resetButton" type="button">Reset view</button>
</div>
<div id="velocityLabel" class="vector-label">
<b>v</b> tangential velocity
</div>
<div id="accelerationLabel" class="vector-label acceleration">
<b>a</b> inward acceleration
</div>
<div class="help">Drag to orbit · Wheel to zoom · Double-click to reset</div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// -------------------------------------------------------------------------
// Renderer and scene
// -------------------------------------------------------------------------
const sceneContainer = document.getElementById("scene");
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x050916);
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.15;
sceneContainer.appendChild(renderer.domElement);
renderer.domElement.tabIndex = 0;
renderer.domElement.setAttribute(
"aria-label",
"Interactive 3D view of a satellite in circular orbit"
);
const camera = new THREE.PerspectiveCamera(
45,
window.innerWidth / window.innerHeight,
0.1,
100
);
// -------------------------------------------------------------------------
// Lighting
// -------------------------------------------------------------------------
scene.add(new THREE.HemisphereLight(0x9ccaff, 0x121522, 1.65));
const sunlight = new THREE.DirectionalLight(0xffffff, 3.1);
sunlight.position.set(5, 8, 4);
scene.add(sunlight);
const rimLight = new THREE.DirectionalLight(0x377dff, 1.25);
rimLight.position.set(-5, 1, -6);
scene.add(rimLight);
// -------------------------------------------------------------------------
// Deterministic star field
// -------------------------------------------------------------------------
let randomSeed = 72431;
function random() {
randomSeed =
(randomSeed * 1664525 + 1013904223) >>> 0;
return randomSeed / 4294967296;
}
const starCount = 420;
const starPositions = new Float32Array(starCount * 3);
for (let i = 0; i < starCount; i++) {
const y = random() * 2 - 1;
const angle = random() * Math.PI * 2;
const radius = 18 + random() * 22;
const horizontal = Math.sqrt(1 - y * y);
starPositions[i * 3] =
Math.cos(angle) * horizontal * radius;
starPositions[i * 3 + 1] = y * radius;
starPositions[i * 3 + 2] =
Math.sin(angle) * horizontal * radius;
}
const starGeometry = new THREE.BufferGeometry();
starGeometry.setAttribute(
"position",
new THREE.BufferAttribute(starPositions, 3)
);
const stars = new THREE.Points(
starGeometry,
new THREE.PointsMaterial({
color: 0xd9e9ff,
size: 0.045,
transparent: true,
opacity: 0.8,
sizeAttenuation: true
})
);
scene.add(stars);
// -------------------------------------------------------------------------
// Faceted Earth
// -------------------------------------------------------------------------
const earthGroup = new THREE.Group();
scene.add(earthGroup);
const baseEarthGeometry = new THREE.IcosahedronGeometry(1.08, 3);
const earthGeometry = baseEarthGeometry.index
? baseEarthGeometry.toNonIndexed()
: baseEarthGeometry;
const earthPositions = earthGeometry.attributes.position;
const earthColors = new Float32Array(earthPositions.count * 3);
const oceanDeep = new THREE.Color(0x124b7c);
const oceanLight = new THREE.Color(0x237ca5);
const land = new THREE.Color(0x4f8c58);
const landLight = new THREE.Color(0x82a95e);
const ice = new THREE.Color(0xc9e7ed);
for (let i = 0; i < earthPositions.count; i += 3) {
const x =
(earthPositions.getX(i) +
earthPositions.getX(i + 1) +
earthPositions.getX(i + 2)) /
3;
const y =
(earthPositions.getY(i) +
earthPositions.getY(i + 1) +
earthPositions.getY(i + 2)) /
3;
const z =
(earthPositions.getZ(i) +
earthPositions.getZ(i + 1) +
earthPositions.getZ(i + 2)) /
3;
const pattern =
Math.sin(x * 8.3 + z * 5.1) +
Math.cos(y * 10.2 - x * 4.7) +
Math.sin((x + z) * 12.0) * 0.45;
let color;
if (y > 0.73) {
color = ice;
} else if (pattern > 1.1) {
color = land;
} else if (pattern > 0.65) {
color = landLight;
} else if (pattern > -0.25) {
color = oceanLight;
} else {
color = oceanDeep;
}
for (let vertex = 0; vertex < 3; vertex++) {
const offset = (i + vertex) * 3;
earthColors[offset] = color.r;
earthColors[offset + 1] = color.g;
earthColors[offset + 2] = color.b;
}
}
earthGeometry.setAttribute(
"color",
new THREE.BufferAttribute(earthColors, 3)
);
earthGeometry.computeVertexNormals();
const earth = new THREE.Mesh(
earthGeometry,
new THREE.MeshStandardMaterial({
vertexColors: true,
flatShading: true,
roughness: 0.88,
metalness: 0.02
})
);
earthGroup.add(earth);
const atmosphere = new THREE.Mesh(
new THREE.IcosahedronGeometry(1.13, 2),
new THREE.MeshBasicMaterial({
color: 0x4fbfff,
transparent: true,
opacity: 0.1,
side: THREE.BackSide,
depthWrite: false
})
);
earthGroup.add(atmosphere);
// -------------------------------------------------------------------------
// Orbit path
// -------------------------------------------------------------------------
const orbitRadius = 3.15;
const orbitPoints = [];
for (let i = 0; i < 256; i++) {
const angle = (i / 256) * Math.PI * 2;
orbitPoints.push(
new THREE.Vector3(
Math.cos(angle) * orbitRadius,
0,
Math.sin(angle) * orbitRadius
)
);
}
const orbit = new THREE.Line(
new THREE.BufferGeometry().setFromPoints(orbitPoints),
new THREE.LineDashedMaterial({
color: 0x7693bd,
dashSize: 0.085,
gapSize: 0.055,
transparent: true,
opacity: 0.5
})
);
orbit.computeLineDistances();
scene.add(orbit);
// Faint line showing the radial direction toward Earth.
const radialGeometry = new THREE.BufferGeometry();
radialGeometry.setAttribute(
"position",
new THREE.BufferAttribute(new Float32Array(6), 3)
);
const radialLine = new THREE.Line(
radialGeometry,
new THREE.LineBasicMaterial({
color: 0x8eb1dc,
transparent: true,
opacity: 0.2
})
);
scene.add(radialLine);
// -------------------------------------------------------------------------
// Low-poly satellite
// -------------------------------------------------------------------------
const satellite = new THREE.Group();
scene.add(satellite);
const bodyMaterial = new THREE.MeshStandardMaterial({
color: 0xcfaa55,
roughness: 0.42,
metalness: 0.58,
flatShading: true
});
const darkMaterial = new THREE.MeshStandardMaterial({
color: 0x172336,
roughness: 0.58,
metalness: 0.5,
flatShading: true
});
const panelMaterial = new THREE.MeshStandardMaterial({
color: 0x174b86,
roughness: 0.3,
metalness: 0.42,
flatShading: true
});
const panelGridMaterial = new THREE.MeshBasicMaterial({
color: 0x6ac8ef,
transparent: true,
opacity: 0.5
});
const antennaMaterial = new THREE.MeshStandardMaterial({
color: 0xe8edf3,
roughness: 0.5,
metalness: 0.25,
flatShading: true,
side: THREE.DoubleSide
});
const body = new THREE.Mesh(
new THREE.BoxGeometry(0.48, 0.38, 0.44),
bodyMaterial
);
satellite.add(body);
const bodyEdges = new THREE.LineSegments(
new THREE.EdgesGeometry(body.geometry),
new THREE.LineBasicMaterial({
color: 0xffdfa0,
transparent: true,
opacity: 0.72
})
);
satellite.add(bodyEdges);
// Solar-panel wings extend along the satellite's local X axis.
for (const side of [-1, 1]) {
const centerX = side * 0.67;
const connector = new THREE.Mesh(
new THREE.BoxGeometry(0.2, 0.07, 0.13),
darkMaterial
);
connector.position.x = side * 0.36;
satellite.add(connector);
const frame = new THREE.Mesh(
new THREE.BoxGeometry(0.84, 0.045, 0.54),
darkMaterial
);
frame.position.x = centerX;
satellite.add(frame);
const panel = new THREE.Mesh(
new THREE.BoxGeometry(0.77, 0.052, 0.47),
panelMaterial
);
panel.position.set(centerX, 0.006, 0);
satellite.add(panel);
// Cell divisions.
for (const offset of [-0.25, 0, 0.25]) {
const gridLine = new THREE.Mesh(
new THREE.BoxGeometry(0.008, 0.006, 0.46),
panelGridMaterial
);
gridLine.position.set(centerX + offset, 0.036, 0);
satellite.add(gridLine);
}
for (const offset of [-0.16, 0, 0.16]) {
const gridLine = new THREE.Mesh(
new THREE.BoxGeometry(0.76, 0.006, 0.008),
panelGridMaterial
);
gridLine.position.set(centerX, 0.037, offset);
satellite.add(gridLine);
}
}
const antennaStem = new THREE.Mesh(
new THREE.CylinderGeometry(0.018, 0.018, 0.22, 8),
darkMaterial
);
antennaStem.position.y = 0.28;
satellite.add(antennaStem);
const antennaDish = new THREE.Mesh(
new THREE.ConeGeometry(0.17, 0.14, 10, 1, true),
antennaMaterial
);
antennaDish.position.y = 0.41;
satellite.add(antennaDish);
const antennaTip = new THREE.Mesh(
new THREE.SphereGeometry(0.035, 8, 6),
new THREE.MeshBasicMaterial({ color: 0xff654f })
);
antennaTip.position.y = 0.31;
satellite.add(antennaTip);
// -------------------------------------------------------------------------
// Vector arrows built from Three.js core geometry
// -------------------------------------------------------------------------
const Y_AXIS = new THREE.Vector3(0, 1, 0);
function createArrow(color) {
const group = new THREE.Group();
const shaft = new THREE.Mesh(
new THREE.CylinderGeometry(0.025, 0.025, 1, 8),
new THREE.MeshBasicMaterial({ color })
);
const head = new THREE.Mesh(
new THREE.ConeGeometry(0.11, 0.3, 8),
new THREE.MeshBasicMaterial({ color })
);
group.add(shaft, head);
group.userData.shaft = shaft;
group.userData.head = head;
return group;
}
function setArrow(arrow, origin, direction, length) {
const headLength = 0.3;
const shaftLength = length - headLength;
arrow.position.copy(origin);
arrow.quaternion.setFromUnitVectors(Y_AXIS, direction);
arrow.userData.shaft.scale.set(1, shaftLength, 1);
arrow.userData.shaft.position.y = shaftLength * 0.5;
arrow.userData.head.position.y = shaftLength + headLength * 0.5;
}
const velocityArrow = createArrow(0x35e7ff);
const accelerationArrow = createArrow(0xff8a3d);
scene.add(velocityArrow, accelerationArrow);
function createVectorStem(color) {
const geometry = new THREE.BufferGeometry();
geometry.setAttribute(
"position",
new THREE.BufferAttribute(new Float32Array(6), 3)
);
const line = new THREE.Line(
geometry,
new THREE.LineBasicMaterial({
color,
transparent: true,
opacity: 0.3
})
);
scene.add(line);
return line;
}
const velocityStem = createVectorStem(0x35e7ff);
const accelerationStem = createVectorStem(0xff8a3d);
function updateStem(line, start, end) {
const position = line.geometry.attributes.position;
position.setXYZ(0, start.x, start.y, start.z);
position.setXYZ(1, end.x, end.y, end.z);
position.needsUpdate = true;
}
// -------------------------------------------------------------------------
// Orbit animation and vector updates
// -------------------------------------------------------------------------
const satellitePosition = new THREE.Vector3();
const radialDirection = new THREE.Vector3();
const tangentDirection = new THREE.Vector3();
const inwardDirection = new THREE.Vector3();
const velocityOrigin = new THREE.Vector3();
const accelerationOrigin = new THREE.Vector3();
const velocityLabelPosition = new THREE.Vector3();
const accelerationLabelPosition = new THREE.Vector3();
const orientationMatrix = new THREE.Matrix4();
const orbitUp = new THREE.Vector3(0, 1, 0);
const angularSpeed = 0.28;
const initialAngle = 0.35;
function updateOrbit(elapsed) {
const angle = initialAngle + elapsed * angularSpeed;
satellitePosition.set(
Math.cos(angle) * orbitRadius,
0,
Math.sin(angle) * orbitRadius
);
satellite.position.copy(satellitePosition);
radialDirection.set(
Math.cos(angle),
0,
Math.sin(angle)
);
tangentDirection.set(
-Math.sin(angle),
0,
Math.cos(angle)
);
inwardDirection.copy(radialDirection).negate();
// Local X follows velocity, local Y points away from Earth,
// and local Z remains aligned with the orbit's vertical axis.
orientationMatrix.makeBasis(
tangentDirection,
radialDirection,
orbitUp
);
satellite.quaternion.setFromRotationMatrix(orientationMatrix);
// Radial guide from Earth to satellite.
const radialPosition = radialLine.geometry.attributes.position;
radialPosition.setXYZ(0, 0, 0, 0);
radialPosition.setXYZ(
1,
satellitePosition.x,
satellitePosition.y,
satellitePosition.z
);
radialPosition.needsUpdate = true;
// Slight offsets keep the two arrows visually separate.
velocityOrigin
.copy(satellitePosition)
.addScaledVector(tangentDirection, 0.3)
.addScaledVector(orbitUp, 0.16);
accelerationOrigin
.copy(satellitePosition)
.addScaledVector(radialDirection, -0.3)
.addScaledVector(orbitUp, -0.16);
setArrow(
velocityArrow,
velocityOrigin,
tangentDirection,
1.3
);
setArrow(
accelerationArrow,
accelerationOrigin,
inwardDirection,
1.15
);
updateStem(velocityStem, satellitePosition, velocityOrigin);
updateStem(accelerationStem, satellitePosition, accelerationOrigin);
velocityLabelPosition
.copy(velocityOrigin)
.addScaledVector(tangentDirection, 0.74)
.addScaledVector(orbitUp, 0.1);
accelerationLabelPosition
.copy(accelerationOrigin)
.addScaledVector(inwardDirection, 0.7)
.addScaledVector(orbitUp, 0.03);
}
// -------------------------------------------------------------------------
// Project HTML labels into screen space
// -------------------------------------------------------------------------
const velocityLabel = document.getElementById("velocityLabel");
const accelerationLabel = document.getElementById("accelerationLabel");
function placeLabel(element, worldPosition) {
const projected = worldPosition.clone().project(camera);
const visible = projected.z > -1 && projected.z < 1;
element.style.display = visible ? "block" : "none";
if (!visible) return;
element.style.left =
`${(projected.x * 0.5 + 0.5) * window.innerWidth}px`;
element.style.top =
`${(-projected.y * 0.5 + 0.5) * window.innerHeight}px`;
}
// -------------------------------------------------------------------------
// Custom camera controls: drag, wheel, and double-click reset
// -------------------------------------------------------------------------
const cameraState = {
theta: 0.68,
phi: 1.08,
radius: 10.2,
target: new THREE.Vector3(0, 0, 0),
dragging: false,
pointerId: null,
lastX: 0,
lastY: 0
};
function applyCamera() {
const sinPhi = Math.sin(cameraState.phi);
camera.position.set(
cameraState.target.x +
cameraState.radius * sinPhi * Math.sin(cameraState.theta),
cameraState.target.y +
cameraState.radius * Math.cos(cameraState.phi),
cameraState.target.z +
cameraState.radius * sinPhi * Math.cos(cameraState.theta)
);
camera.lookAt(cameraState.target);
}
function resetCamera() {
cameraState.theta = 0.68;
cameraState.phi = 1.08;
cameraState.radius = 10.2;
applyCamera();
}
renderer.domElement.addEventListener("pointerdown", (event) => {
cameraState.dragging = true;
cameraState.pointerId = event.pointerId;
cameraState.lastX = event.clientX;
cameraState.lastY = event.clientY;
renderer.domElement.setPointerCapture(event.pointerId);
});
renderer.domElement.addEventListener("pointermove", (event) => {
if (!cameraState.dragging) return;
if (event.pointerId !== cameraState.pointerId) return;
const deltaX = event.clientX - cameraState.lastX;
const deltaY = event.clientY - cameraState.lastY;
cameraState.lastX = event.clientX;
cameraState.lastY = event.clientY;
cameraState.theta -= deltaX * 0.006;
cameraState.phi -= deltaY * 0.006;
cameraState.phi = THREE.MathUtils.clamp(
cameraState.phi,
0.22,
Math.PI - 0.22
);
});
function stopDragging(event) {
if (event.pointerId !== cameraState.pointerId) return;
cameraState.dragging = false;
cameraState.pointerId = null;
}
renderer.domElement.addEventListener("pointerup", stopDragging);
renderer.domElement.addEventListener("pointercancel", stopDragging);
renderer.domElement.addEventListener(
"wheel",
(event) => {
event.preventDefault();
cameraState.radius = THREE.MathUtils.clamp(
cameraState.radius * Math.exp(event.deltaY * 0.001),
5.2,
18
);
},
{ passive: false }
);
renderer.domElement.addEventListener("dblclick", resetCamera);
// -------------------------------------------------------------------------
// Buttons and keyboard controls
// -------------------------------------------------------------------------
let paused = false;
const pauseButton = document.getElementById("pauseButton");
const resetButton = document.getElementById("resetButton");
function togglePause() {
paused = !paused;
pauseButton.textContent = paused ? "Resume" : "Pause";
}
pauseButton.addEventListener("click", togglePause);
resetButton.addEventListener("click", resetCamera);
window.addEventListener("keydown", (event) => {
if (event.target instanceof HTMLButtonElement) return;
if (event.code === "Space") {
event.preventDefault();
togglePause();
}
if (event.key.toLowerCase() === "r") {
resetCamera();
}
});
// -------------------------------------------------------------------------
// Resize and render loop
// -------------------------------------------------------------------------
window.addEventListener("resize", () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
});
const clock = new THREE.Clock();
let simulationTime = 0;
let visualTime = 0;
function animate() {
requestAnimationFrame(animate);
const delta = Math.min(clock.getDelta(), 0.05);
visualTime += delta;
if (!paused) {
simulationTime += delta;
}
applyCamera();
updateOrbit(simulationTime);
earth.rotation.y = visualTime * 0.075;
stars.rotation.y = visualTime * 0.003;
camera.updateMatrixWorld();
placeLabel(velocityLabel, velocityLabelPosition);
placeLabel(accelerationLabel, accelerationLabelPosition);
renderer.render(scene, camera);
}
applyCamera();
updateOrbit(0);
animate();
</script>
</body>
</html>
```