Prompt specification
Build or customize the Three.js 3D scene "Linear Scale Factor and Different Separation Rates" 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: Linear Scale Factor and Different Separation Rates
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/linear-scale-factor-and-different-separation-rates-x3772
Scene Description Prompt:
Create a standalone voxel scene in one HTML file using Three.js from cdnjs. Create a toy expansion model with a declared scale factor. Compare distance-change rates for several initial separations while keeping cosmological interpretation separate from the mathematical demonstration.
Working Three.js Starter (linear-scale-factor-and-different-separation-rates-x3772):
```html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Voxel Expansion — Mathematical Toy Model</title>
<style>
:root {
color-scheme: dark;
--bg: #07111f;
--panel: rgba(10, 22, 38, 0.88);
--panel-border: rgba(155, 203, 255, 0.18);
--text: #e7f2ff;
--muted: #9eb3ca;
--cyan: #54e6ff;
--violet: #b38cff;
--orange: #ffb45e;
--green: #6cf0b4;
--amber: #ffc66d;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: var(--bg);
color: var(--text);
font-family:
Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
#viewport {
position: fixed;
inset: 0;
}
#viewport canvas {
display: block;
width: 100%;
height: 100%;
outline: none;
touch-action: none;
}
header {
position: fixed;
top: 16px;
left: 18px;
z-index: 5;
pointer-events: none;
text-shadow: 0 2px 18px rgba(0, 0, 0, 0.65);
}
h1 {
margin: 0;
font-size: clamp(20px, 2.4vw, 32px);
letter-spacing: -0.035em;
}
header p {
margin: 5px 0 0;
color: var(--muted);
font-size: 13px;
}
.badge {
display: inline-block;
margin-top: 8px;
padding: 5px 9px;
border: 1px solid rgba(108, 240, 180, 0.35);
border-radius: 999px;
color: var(--green);
background: rgba(7, 35, 31, 0.58);
font-size: 11px;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.panel {
position: fixed;
z-index: 6;
border: 1px solid var(--panel-border);
border-radius: 16px;
background: var(--panel);
box-shadow:
0 18px 60px rgba(0, 0, 0, 0.35),
inset 0 1px 0 rgba(255, 255, 255, 0.035);
backdrop-filter: blur(16px);
-webkit-backdrop-filter: blur(16px);
}
#controls {
top: 104px;
left: 18px;
width: 300px;
max-height: calc(100vh - 126px);
padding: 16px;
overflow: auto;
}
#math-panel {
top: 104px;
right: 18px;
width: 370px;
max-height: calc(100vh - 126px);
padding: 16px;
overflow: auto;
}
.section-title {
margin: 0 0 12px;
font-size: 13px;
letter-spacing: 0.09em;
text-transform: uppercase;
color: #c9dcf0;
}
.law-card {
margin-bottom: 14px;
padding: 12px;
border: 1px solid rgba(84, 230, 255, 0.24);
border-radius: 12px;
background:
linear-gradient(135deg, rgba(84, 230, 255, 0.08), transparent),
rgba(0, 0, 0, 0.12);
}
.equation {
font-family: "SFMono-Regular", Consolas, "Liberation Mono", monospace;
font-size: 17px;
color: #f5fbff;
}
.equation + .equation {
margin-top: 6px;
color: var(--cyan);
}
.small {
color: var(--muted);
font-size: 12px;
line-height: 1.55;
}
.readout-grid {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 8px;
margin-top: 10px;
}
.readout {
padding: 9px;
border-radius: 10px;
background: rgba(255, 255, 255, 0.045);
}
.readout span {
display: block;
color: var(--muted);
font-size: 10px;
text-transform: uppercase;
letter-spacing: 0.07em;
}
.readout strong {
display: block;
margin-top: 3px;
font-size: 18px;
font-variant-numeric: tabular-nums;
}
.control {
margin: 14px 0;
}
.control label {
display: flex;
justify-content: space-between;
gap: 10px;
margin-bottom: 7px;
color: #d7e6f4;
font-size: 12px;
}
.control output {
color: var(--cyan);
font-variant-numeric: tabular-nums;
}
input[type="range"] {
width: 100%;
accent-color: var(--cyan);
cursor: pointer;
}
.button-row {
display: flex;
flex-wrap: wrap;
gap: 7px;
margin-top: 10px;
}
button {
appearance: none;
border: 1px solid rgba(155, 203, 255, 0.2);
border-radius: 9px;
padding: 8px 11px;
color: var(--text);
background: rgba(255, 255, 255, 0.055);
font: inherit;
font-size: 12px;
cursor: pointer;
transition:
background 120ms ease,
border-color 120ms ease,
transform 120ms ease;
}
button:hover {
border-color: rgba(84, 230, 255, 0.55);
background: rgba(84, 230, 255, 0.1);
}
button:active {
transform: translateY(1px);
}
button.primary {
border-color: rgba(84, 230, 255, 0.4);
color: #07111f;
background: var(--cyan);
font-weight: 700;
}
.hint {
margin-top: 12px;
padding-top: 11px;
border-top: 1px solid rgba(255, 255, 255, 0.08);
color: var(--muted);
font-size: 11px;
line-height: 1.5;
}
table {
width: 100%;
margin-top: 10px;
border-collapse: collapse;
font-size: 11px;
font-variant-numeric: tabular-nums;
}
th {
padding: 7px 5px;
color: var(--muted);
font-weight: 600;
text-align: right;
border-bottom: 1px solid rgba(255, 255, 255, 0.1);
}
th:first-child,
td:first-child {
text-align: left;
}
td {
padding: 8px 5px;
text-align: right;
border-bottom: 1px solid rgba(255, 255, 255, 0.055);
}
.dot {
display: inline-block;
width: 7px;
height: 7px;
margin-right: 6px;
border-radius: 50%;
vertical-align: 1px;
}
.rate-bar {
position: relative;
height: 3px;
margin-top: 4px;
overflow: hidden;
border-radius: 3px;
background: rgba(255, 255, 255, 0.08);
}
.rate-bar > span {
display: block;
height: 100%;
border-radius: inherit;
}
#rate-plot {
display: block;
width: 100%;
height: 185px;
margin-top: 8px;
border: 1px solid rgba(255, 255, 255, 0.075);
border-radius: 10px;
background: rgba(0, 0, 0, 0.12);
}
.interpretation {
margin-top: 14px;
padding: 12px;
border: 1px solid rgba(255, 198, 109, 0.28);
border-radius: 12px;
background: rgba(70, 48, 13, 0.15);
}
.interpretation summary {
color: var(--amber);
cursor: pointer;
font-size: 12px;
font-weight: 700;
}
.interpretation p {
margin: 10px 0 0;
color: #ddcfb5;
font-size: 11px;
line-height: 1.6;
}
#status {
position: fixed;
left: 50%;
bottom: 18px;
z-index: 5;
transform: translateX(-50%);
padding: 8px 12px;
border: 1px solid rgba(155, 203, 255, 0.14);
border-radius: 999px;
color: var(--muted);
background: rgba(7, 17, 31, 0.74);
font-size: 11px;
pointer-events: none;
white-space: nowrap;
}
@media (max-width: 900px) {
body {
overflow: auto;
}
#viewport {
height: 58vh;
bottom: auto;
}
header {
top: 12px;
left: 12px;
}
#controls,
#math-panel {
position: relative;
top: auto;
right: auto;
left: auto;
width: auto;
max-height: none;
margin: 12px;
}
#status {
display: none;
}
}
</style>
</head>
<body>
<div id="viewport"></div>
<header>
<h1>Voxel Expansion Lab</h1>
<p>A Euclidean toy model for comparing expansion rates</p>
<span class="badge">Mathematics first · interpretation separated</span>
</header>
<section id="controls" class="panel" aria-label="Model controls">
<h2 class="section-title">Toy Model Controls</h2>
<div class="law-card">
<div class="equation">a(t) = exp(Ht)</div>
<div class="equation">D(t) = a(t)D₀</div>
<div class="small" style="margin-top: 8px">
Declared law: a constant toy rate H multiplies every initial
separation by the same scale factor.
</div>
<div class="readout-grid">
<div class="readout">
<span>Scale factor a</span>
<strong id="factor-readout">1.00</strong>
</div>
<div class="readout">
<span>Toy time</span>
<strong id="time-readout">0.0 min</strong>
</div>
</div>
</div>
<div class="control">
<label for="rate-slider">
Toy rate H
<output id="rate-output">0.120 min⁻¹</output>
</label>
<input
id="rate-slider"
type="range"
min="0.02"
max="0.12"
step="0.001"
value="0.12"
>
</div>
<div class="control">
<label for="time-slider">
Toy time t
<output id="time-output">0.0 min</output>
</label>
<input
id="time-slider"
type="range"
min="0"
max="12"
step="0.01"
value="0"
>
</div>
<div class="button-row">
<button id="play-button" class="primary" type="button">Pause</button>
<button id="reset-button" type="button">Reset</button>
<button id="home-button" type="button">Camera home</button>
<button id="top-button" type="button">Top view</button>
<button id="labels-button" type="button">Hide labels</button>
</div>
<div class="hint">
Drag: orbit · Shift-drag or right-drag: pan · Wheel: zoom<br>
Space: play/pause · R: reset · H: camera home
</div>
</section>
<section id="math-panel" class="panel" aria-label="Mathematical comparison">
<h2 class="section-title">Distance-Change Comparison</h2>
<div class="small">
At the same toy epoch, every pair is multiplied by the same factor
<strong>a</strong>. Its absolute distance-change rate is
<strong>dD/dt = HD</strong>, while its percentage rate is H for every pair.
</div>
<table>
<thead>
<tr>
<th>Initial D₀</th>
<th>Current D</th>
<th>dD/dt</th>
<th>Rate / min</th>
</tr>
</thead>
<tbody id="rate-table"></tbody>
</table>
<canvas id="rate-plot" aria-label="Graph of distance-change rate"></canvas>
<div class="small" style="margin-top: 8px">
The plotted points lie on dD/dt = HD. Larger initial separations
acquire larger absolute rates, but all pairs have the same fractional
rate H at a given epoch.
</div>
<details class="interpretation" open>
<summary>Cosmological interpretation — separate from the model</summary>
<p>
This scene is only a Euclidean visualization. In relativistic
cosmology, the scale factor is normally connected to an FLRW
spacetime and its dynamics through cosmological field equations.
The real function H(t) is not an arbitrary visual control, and
“distance” can require distinctions among proper distance,
luminosity distance, and angular-diameter distance.
</p>
<p>
Nothing in the toy equations establishes a cosmic expansion history,
redshift law, cosmological horizon, dark-energy model, or age of the
universe. Those are separate physical questions.
</p>
</details>
</section>
<div id="status">Three.js core only · custom camera controls</div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// ---------------------------------------------------------------------
// DOM and model state
// ---------------------------------------------------------------------
const viewport = document.querySelector("#viewport");
const factorReadout = document.querySelector("#factor-readout");
const timeReadout = document.querySelector("#time-readout");
const rateOutput = document.querySelector("#rate-output");
const timeOutput = document.querySelector("#time-output");
const rateSlider = document.querySelector("#rate-slider");
const timeSlider = document.querySelector("#time-slider");
const playButton = document.querySelector("#play-button");
const resetButton = document.querySelector("#reset-button");
const homeButton = document.querySelector("#home-button");
const topButton = document.querySelector("#top-button");
const labelsButton = document.querySelector("#labels-button");
const rateTable = document.querySelector("#rate-table");
const plot = document.querySelector("#rate-plot");
const initialSeparations = [2, 4, 6, 8];
const pairColors = [0x54e6ff, 0xb38cff, 0xffb45e, 0x6cf0b4];
const pairCSSColors = ["#54e6ff", "#b38cff", "#ffb45e", "#6cf0b4"];
const state = {
H: 0.12,
time: 0,
playing: true,
labelsVisible: true
};
function scaleFactor() {
return Math.exp(state.H * state.time);
}
// ---------------------------------------------------------------------
// Three.js renderer, scene, and camera
// ---------------------------------------------------------------------
const renderer = new THREE.WebGLRenderer({
antialias: true,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.domElement.tabIndex = 0;
renderer.domElement.setAttribute(
"aria-label",
"Interactive 3D voxel expansion scene"
);
viewport.appendChild(renderer.domElement);
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x07111f);
scene.fog = new THREE.Fog(0x07111f, 28, 78);
const camera = new THREE.PerspectiveCamera(
48,
window.innerWidth / window.innerHeight,
0.1,
160
);
const cameraState = {
target: new THREE.Vector3(0, 1.8, 1.2),
radius: 22,
phi: 1.06,
theta: 0.62
};
function updateCamera() {
camera.position.setFromSphericalCoords(
cameraState.radius,
cameraState.phi,
cameraState.theta
);
camera.position.add(cameraState.target);
camera.lookAt(cameraState.target);
camera.updateMatrixWorld();
}
updateCamera();
const hemisphere = new THREE.HemisphereLight(0xbde9ff, 0x182031, 2.2);
scene.add(hemisphere);
const keyLight = new THREE.DirectionalLight(0xffffff, 3.1);
keyLight.position.set(8, 17, 10);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(2048, 2048);
keyLight.shadow.camera.left = -24;
keyLight.shadow.camera.right = 24;
keyLight.shadow.camera.top = 24;
keyLight.shadow.camera.bottom = -24;
keyLight.shadow.camera.near = 1;
keyLight.shadow.camera.far = 60;
scene.add(keyLight);
const rimLight = new THREE.DirectionalLight(0x4ba8ff, 1.5);
rimLight.position.set(-12, 7, -13);
scene.add(rimLight);
const floor = new THREE.Mesh(
new THREE.PlaneGeometry(120, 120),
new THREE.MeshStandardMaterial({
color: 0x0b1727,
roughness: 0.95,
metalness: 0.02
})
);
floor.rotation.x = -Math.PI / 2;
floor.position.y = -0.02;
floor.receiveShadow = true;
scene.add(floor);
const grid = new THREE.GridHelper(100, 50, 0x31506b, 0x182d42);
grid.position.y = 0.01;
grid.material.transparent = true;
grid.material.opacity = 0.46;
scene.add(grid);
const axisX = new THREE.ArrowHelper(
new THREE.Vector3(1, 0, 0),
new THREE.Vector3(-6, 0.03, -5),
2,
0xff6f91,
0.35,
0.2
);
const axisY = new THREE.ArrowHelper(
new THREE.Vector3(0, 1, 0),
new THREE.Vector3(-6, 0.03, -5),
2,
0x6cf0b4,
0.35,
0.2
);
const axisZ = new THREE.ArrowHelper(
new THREE.Vector3(0, 0, 1),
new THREE.Vector3(-6, 0.03, -5),
2,
0x54e6ff,
0.35,
0.2
);
scene.add(axisX, axisY, axisZ);
// ---------------------------------------------------------------------
// Voxel lattice
// ---------------------------------------------------------------------
const voxelGeometry = new THREE.BoxGeometry(1, 1, 1);
const voxelMaterial = new THREE.MeshStandardMaterial({
color: 0xffffff,
roughness: 0.58,
metalness: 0.06,
vertexColors: true
});
const baseVoxels = [];
for (let x = -4; x <= 4; x++) {
for (let y = 0; y < 3; y++) {
for (let z = -3; z <= 3; z++) {
baseVoxels.push({
x,
y: y + 0.25,
z,
layer: y
});
}
}
}
const voxelMesh = new THREE.InstancedMesh(
voxelGeometry,
voxelMaterial,
baseVoxels.length
);
voxelMesh.instanceMatrix.setUsage(THREE.DynamicDrawUsage);
voxelMesh.castShadow = true;
voxelMesh.receiveShadow = true;
voxelMesh.frustumCulled = false;
const dummy = new THREE.Object3D();
const teal = new THREE.Color(0x32b7c9);
const blue = new THREE.Color(0x397ac7);
const violet = new THREE.Color(0x855fd6);
const warm = new THREE.Color(0xe58c65);
baseVoxels.forEach((voxel, index) => {
const checker = (voxel.x + voxel.z + voxel.layer) % 2;
const color = new THREE.Color();
if (voxel.layer === 0) {
color.copy(checker ? teal : blue);
} else if (voxel.layer === 1) {
color.copy(checker ? violet : teal);
} else {
color.copy(checker ? warm : violet);
}
color.offsetHSL(0, 0, (voxel.x % 3) * 0.012);
voxelMesh.setColorAt(index, color);
});
voxelMesh.instanceColor.needsUpdate = true;
scene.add(voxelMesh);
const cageGeometry = new THREE.EdgesGeometry(
new THREE.BoxGeometry(9.5, 3.5, 7.5)
);
const currentCage = new THREE.LineSegments(
cageGeometry,
new THREE.LineBasicMaterial({
color: 0x54e6ff,
transparent: true,
opacity: 0.34
})
);
currentCage.position.y = 1.75;
scene.add(currentCage);
const initialFootprintPoints = [
new THREE.Vector3(-4.75, 0.035, -3.75),
new THREE.Vector3(4.75, 0.035, -3.75),
new THREE.Vector3(4.75, 0.035, 3.75),
new THREE.Vector3(-4.75, 0.035, 3.75)
];
const initialFootprint = new THREE.LineLoop(
new THREE.BufferGeometry().setFromPoints(initialFootprintPoints),
new THREE.LineBasicMaterial({
color: 0x7891aa,
transparent: true,
opacity: 0.48
})
);
scene.add(initialFootprint);
// ---------------------------------------------------------------------
// Measurement pairs
// ---------------------------------------------------------------------
const measurementBay = new THREE.Group();
measurementBay.position.set(0, 0, 5.7);
scene.add(measurementBay);
const labelGroup = new THREE.Group();
measurementBay.add(labelGroup);
function makeLabelSprite(text, colorHex) {
const canvas = document.createElement("canvas");
canvas.width = 320;
canvas.height = 80;
const context = canvas.getContext("2d");
context.clearRect(0, 0, canvas.width, canvas.height);
context.fillStyle = "rgba(5, 15, 27, 0.84)";
context.strokeStyle = `#${colorHex.toString(16).padStart(6, "0")}`;
context.lineWidth = 3;
context.beginPath();
context.roundRect(4, 8, 312, 64, 14);
context.fill();
context.stroke();
context.fillStyle = "#eef8ff";
context.font = "600 27px system-ui, sans-serif";
context.textAlign = "center";
context.textBaseline = "middle";
context.fillText(text, 160, 41);
const texture = new THREE.CanvasTexture(canvas);
texture.colorSpace = THREE.SRGBColorSpace;
const material = new THREE.SpriteMaterial({
map: texture,
transparent: true,
depthTest: false,
depthWrite: false
});
const sprite = new THREE.Sprite(material);
sprite.scale.set(2.1, 0.525, 1);
sprite.renderOrder = 10;
return sprite;
}
const measurementPairs = [];
initialSeparations.forEach((separation, index) => {
const color = pairColors[index];
const z = index * 1.12;
const material = new THREE.MeshStandardMaterial({
color,
emissive: color,
emissiveIntensity: 0.18,
roughness: 0.42,
metalness: 0.08
});
const left = new THREE.Mesh(voxelGeometry, material);
const right = new THREE.Mesh(voxelGeometry, material);
left.castShadow = true;
right.castShadow = true;
measurementBay.add(left, right);
const lineGeometry = new THREE.BufferGeometry();
lineGeometry.setAttribute(
"position",
new THREE.BufferAttribute(new Float32Array(6), 3)
);
const line = new THREE.Line(
lineGeometry,
new THREE.LineBasicMaterial({
color,
transparent: true,
opacity: 0.9
})
);
measurementBay.add(line);
const label = makeLabelSprite(`D₀ = ${separation}`, color);
labelGroup.add(label);
measurementPairs.push({
separation,
color,
left,
right,
line,
label,
z
});
});
function updateScene(a) {
const voxelSize = 0.5 * a;
baseVoxels.forEach((voxel, index) => {
dummy.position.set(
voxel.x * a,
voxel.y * a,
voxel.z * a
);
dummy.scale.setScalar(voxelSize);
dummy.updateMatrix();
voxelMesh.setMatrixAt(index, dummy.matrix);
});
voxelMesh.instanceMatrix.needsUpdate = true;
currentCage.scale.set(a, a, a);
currentCage.position.y = 1.75 * a;
measurementPairs.forEach((pair) => {
const half = 0.5 * pair.separation * a;
const y = 0.45;
const markerSize = 0.34 * a;
pair.left.position.set(-half, y, pair.z);
pair.right.position.set(half, y, pair.z);
pair.left.scale.setScalar(markerSize);
pair.right.scale.setScalar(markerSize);
const positions = pair.line.geometry.attributes.position;
positions.setXYZ(0, -half, y, pair.z);
positions.setXYZ(1, half, y, pair.z);
positions.needsUpdate = true;
pair.label.position.set(
-half - 0.72,
y + 0.58,
pair.z
);
});
}
// ---------------------------------------------------------------------
// Mathematical table and graph
// ---------------------------------------------------------------------
function updateReadouts() {
const a = scaleFactor();
factorReadout.textContent = a.toFixed(3);
timeReadout.textContent = `${state.time.toFixed(1)} min`;
rateOutput.textContent = `${state.H.toFixed(3)} min⁻¹`;
timeOutput.textContent = `${state.time.toFixed(1)} min`;
const maximumRate = Math.max(
...initialSeparations.map((d0) => state.H * a * d0)
);
rateTable.innerHTML = initialSeparations
.map((d0, index) => {
const distance = a * d0;
const rate = state.H * distance;
const percentage = state.H * 100;
const width = maximumRate > 0 ? (rate / maximumRate) * 100 : 0;
return `
<tr>
<td>
<span class="dot" style="background:${pairCSSColors[index]}"></span>
${d0.toFixed(1)}
</td>
<td>${distance.toFixed(2)}</td>
<td>${rate.toFixed(3)}</td>
<td>
${percentage.toFixed(1)}%
<div class="rate-bar">
<span
style="width:${width}%;background:${pairCSSColors[index]}"
></span>
</div>
</td>
</tr>
`;
})
.join("");
drawPlot(a);
}
function drawPlot(a) {
const width = Math.max(1, plot.clientWidth);
const height = Math.max(1, plot.clientHeight);
const dpr = Math.min(window.devicePixelRatio, 2);
const pixelWidth = Math.round(width * dpr);
const pixelHeight = Math.round(height * dpr);
if (plot.width !== pixelWidth || plot.height !== pixelHeight) {
plot.width = pixelWidth;
plot.height = pixelHeight;
}
const context = plot.getContext("2d");
context.setTransform(dpr, 0, 0, dpr, 0, 0);
context.clearRect(0, 0, width, height);
const margin = {
left: 48,
right: 18,
top: 24,
bottom: 42
};
const innerWidth = width - margin.left - margin.right;
const innerHeight = height - margin.top - margin.bottom;
const maximumDistance = Math.max(...initialSeparations.map((d0) => a * d0));
const maximumRate = state.H * maximumDistance;
const xMax = Math.max(1, maximumDistance * 1.14);
const yMax = Math.max(0.001, maximumRate * 1.18);
const xScale = value => margin.left + (value / xMax) * innerWidth;
const yScale = value =>
margin.top + innerHeight - (value / yMax) * innerHeight;
context.font = "10px system-ui, sans-serif";
context.textBaseline = "middle";
// Grid and ticks
for (let i = 0; i <= 4; i++) {
const x = margin.left + (innerWidth * i) / 4;
const y = margin.top + (innerHeight * i) / 4;
context.strokeStyle = "rgba(170, 200, 230, 0.09)";
context.lineWidth = 1;
context.beginPath();
context.moveTo(x, margin.top);
context.lineTo(x, margin.top + innerHeight);
context.stroke();
context.beginPath();
context.moveTo(margin.left, y);
context.lineTo(margin.left + innerWidth, y);
context.stroke();
const xValue = (xMax * i) / 4;
const yValue = (yMax * i) / 4;
context.fillStyle = "#8299b1";
context.textAlign = "center";
context.fillText(xValue.toFixed(1), x, height - 23);
context.textAlign = "right";
context.fillText(yValue.toFixed(2), margin.left - 7, y);
}
// Axes
context.strokeStyle = "rgba(190, 220, 245, 0.38)";
context.lineWidth = 1.2;
context.beginPath();
context.moveTo(margin.left, margin.top);
context.lineTo(margin.left, margin.top + innerHeight);
context.lineTo(margin.left + innerWidth, margin.top + innerHeight);
context.stroke();
// dD/dt = H D
context.strokeStyle = "rgba(108, 240, 180, 0.72)";
context.lineWidth = 2;
context.setLineDash([5, 4]);
context.beginPath();
context.moveTo(xScale(0), yScale(0));
context.lineTo(xScale(xMax), yScale(state.H * xMax));
context.stroke();
context.setLineDash([]);
// Points
initialSeparations.forEach((d0, index) => {
const distance = a * d0;
const rate = state.H * distance;
const x = xScale(distance);
const y = yScale(rate);
context.fillStyle = pairCSSColors[index];
context.beginPath();
context.arc(x, y, 4.2, 0, Math.PI * 2);
context.fill();
context.strokeStyle = "rgba(255, 255, 255, 0.7)";
context.lineWidth = 1;
context.stroke();
context.fillStyle = pairCSSColors[index];
context.font = "600 10px system-ui, sans-serif";
context.textAlign = "center";
context.fillText(`D₀=${d0}`, x, y - 10);
});
// Labels
context.fillStyle = "#9eb3ca";
context.font = "10px system-ui, sans-serif";
context.textAlign = "center";
context.fillText(
"Current distance D",
margin.left + innerWidth / 2,
height - 7
);
context.save();
context.translate(11, margin.top + innerHeight / 2);
context.rotate(-Math.PI / 2);
context.fillText("dD/dt", 0, 0);
context.restore();
context.fillStyle = "#6cf0b4";
context.textAlign = "right";
context.fillText(
"slope = H",
margin.left + innerWidth,
margin.top + 10
);
}
// ---------------------------------------------------------------------
// Custom camera controls: orbit, pan, and zoom
// ---------------------------------------------------------------------
let dragging = null;
let previousX = 0;
let previousY = 0;
renderer.domElement.addEventListener("contextmenu", event => {
event.preventDefault();
});
renderer.domElement.addEventListener("pointerdown", event => {
renderer.domElement.setPointerCapture(event.pointerId);
previousX = event.clientX;
previousY = event.clientY;
dragging =
event.button === 2 || event.shiftKey ? "pan" : "orbit";
});
renderer.domElement.addEventListener("pointermove", event => {
if (!dragging) return;
const dx = event.clientX - previousX;
const dy = event.clientY - previousY;
previousX = event.clientX;
previousY = event.clientY;
if (dragging === "orbit") {
cameraState.theta -= dx * 0.006;
cameraState.phi = THREE.MathUtils.clamp(
cameraState.phi - dy * 0.005,
0.12,
Math.PI - 0.12
);
} else {
const right = new THREE.Vector3()
.setFromMatrixColumn(camera.matrixWorld, 0)
.normalize();
const up = new THREE.Vector3()
.setFromMatrixColumn(camera.matrixWorld, 1)
.normalize();
const panScale =
(2 *
cameraState.radius *
Math.tan(THREE.MathUtils.degToRad(camera.fov / 2))) /
renderer.domElement.clientHeight;
cameraState.target
.addScaledVector(right, -dx * panScale)
.addScaledVector(up, dy * panScale);
}
updateCamera();
});
function endDrag(event) {
dragging = null;
if (event.pointerId !== undefined &&
renderer.domElement.hasPointerCapture(event.pointerId)) {
renderer.domElement.releasePointerCapture(event.pointerId);
}
}
renderer.domElement.addEventListener("pointerup", endDrag);
renderer.domElement.addEventListener("pointercancel", endDrag);
renderer.domElement.addEventListener(
"wheel",
event => {
event.preventDefault();
cameraState.radius = THREE.MathUtils.clamp(
cameraState.radius * Math.exp(event.deltaY * 0.001),
7,
55
);
updateCamera();
},
{ passive: false }
);
function cameraHome() {
cameraState.target.set(0, 1.8, 1.2);
cameraState.radius = 22;
cameraState.phi = 1.06;
cameraState.theta = 0.62;
updateCamera();
}
function cameraTop() {
cameraState.target.set(0, 1.2, 1.5);
cameraState.radius = 25;
cameraState.phi = 0.18;
cameraState.theta = 0;
updateCamera();
}
// ---------------------------------------------------------------------
// UI events
// ---------------------------------------------------------------------
function updatePlayButton() {
playButton.textContent = state.playing ? "Pause" : "Play";
}
function togglePlayback() {
if (!state.playing && state.time >= 12) {
state.time = 0;
}
state.playing = !state.playing;
updatePlayButton();
}
playButton.addEventListener("click", togglePlayback);
resetButton.addEventListener("click", () => {
state.H = 0.12;
state.time = 0;
state.playing = true;
rateSlider.value = state.H;
timeSlider.value = state.time;
updatePlayButton();
updateReadouts();
});
homeButton.addEventListener("click", cameraHome);
topButton.addEventListener("click", cameraTop);
labelsButton.addEventListener("click", () => {
state.labelsVisible = !state.labelsVisible;
labelGroup.visible = state.labelsVisible;
labelsButton.textContent = state.labelsVisible
? "Hide labels"
: "Show labels";
});
rateSlider.addEventListener("input", () => {
state.H = Number(rateSlider.value);
updateReadouts();
});
timeSlider.addEventListener("input", () => {
state.time = Number(timeSlider.value);
state.playing = false;
updatePlayButton();
updateReadouts();
});
window.addEventListener("keydown", event => {
if (
event.target instanceof HTMLInputElement ||
event.target instanceof HTMLButtonElement
) {
return;
}
if (event.code === "Space") {
event.preventDefault();
togglePlayback();
} else if (event.key.toLowerCase() === "r") {
resetButton.click();
} else if (event.key.toLowerCase() === "h") {
cameraHome();
}
});
// ---------------------------------------------------------------------
// Resize and animation loop
// ---------------------------------------------------------------------
function resize() {
const width = window.innerWidth;
const height = window.innerHeight;
camera.aspect = width / height;
camera.updateProjectionMatrix();
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(width, height);
drawPlot(scaleFactor());
}
window.addEventListener("resize", resize);
const clock = new THREE.Clock();
let lastReadoutUpdate = 0;
renderer.setAnimationLoop(() => {
const delta = Math.min(clock.getDelta(), 0.05);
if (state.playing) {
// One real second advances half of one toy minute.
state.time = Math.min(12, state.time + delta * 0.5);
timeSlider.value = state.time;
if (state.time >= 12) {
state.playing = false;
updatePlayButton();
}
}
const a = scaleFactor();
updateScene(a);
const now = performance.now();
if (now - lastReadoutUpdate > 90) {
updateReadouts();
lastReadoutUpdate = now;
}
renderer.render(scene, camera);
});
updatePlayButton();
updateReadouts();
resize();
</script>
</body>
</html>
```