Prompt specification
Build or customize the Three.js 3D scene "Transfer station with two line colors" 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: Transfer station with two line colors
Category: Space & Earth · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/transfer-station-with-two-line-colors-x1354
Scene Description Prompt:
Create a single self-contained HTML file using Three.js from cdnjs that shows two crossing transit lines with one shared station. Show a geographic view and simplified diagram, preserving transfers while changing track angles and spacing. Low-poly.
Working Three.js Starter (transfer-station-with-two-line-colors-x1354):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Crossline — Transit Interchange Atlas</title>
<style>
:root {
--ink: #17272d;
--muted: #68757a;
--paper: #f4f0e7;
--ground: #d9d5ca;
--blue: #2563eb;
--orange: #ed6847;
--green: #237b57;
--panel-border: rgba(28, 45, 50, 0.12);
--shadow: 0 16px 44px rgba(38, 48, 48, 0.13);
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
min-height: 100%;
margin: 0;
}
body {
min-height: 100vh;
overflow: hidden;
color: var(--ink);
background:
radial-gradient(circle at 10% 0%, rgba(255,255,255,.72), transparent 34%),
var(--ground);
font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
button,
input {
font: inherit;
}
button {
color: inherit;
}
.app {
display: flex;
flex-direction: column;
min-height: 100vh;
height: 100vh;
}
.topbar {
position: relative;
z-index: 10;
display: grid;
grid-template-columns: auto auto minmax(260px, 1fr) auto;
align-items: center;
gap: 22px;
padding: 16px 20px;
}
.brand {
display: flex;
align-items: center;
gap: 12px;
min-width: 230px;
}
.brand-mark {
display: grid;
place-items: center;
width: 44px;
height: 44px;
border-radius: 14px;
color: white;
background: var(--ink);
box-shadow: 0 8px 18px rgba(23, 39, 45, .18);
}
.brand-mark svg {
width: 27px;
height: 27px;
}
.brand h1 {
margin: 0;
font-size: 17px;
line-height: 1.1;
letter-spacing: -.02em;
}
.brand p {
margin: 4px 0 0;
color: var(--muted);
font-size: 11px;
letter-spacing: .08em;
text-transform: uppercase;
}
.top-stats {
display: flex;
gap: 8px;
}
.stat-pill,
.lock-pill {
display: inline-flex;
align-items: center;
gap: 7px;
min-height: 31px;
padding: 0 11px;
border: 1px solid rgba(25, 45, 50, .1);
border-radius: 999px;
background: rgba(255,255,255,.48);
color: #526066;
font-size: 11px;
font-weight: 650;
white-space: nowrap;
}
.lock-pill {
color: var(--green);
background: rgba(238, 247, 237, .72);
border-color: rgba(35, 123, 87, .16);
}
.lock-dot {
width: 7px;
height: 7px;
border-radius: 50%;
background: #3aa574;
box-shadow: 0 0 0 4px rgba(58,165,116,.12);
}
.geometry-controls {
display: flex;
justify-content: center;
gap: 18px;
min-width: 0;
padding: 8px 18px;
border: 1px solid rgba(25, 45, 50, .1);
border-radius: 16px;
background: rgba(255,255,255,.44);
backdrop-filter: blur(12px);
}
.range-control {
display: grid;
grid-template-columns: auto 92px auto;
align-items: center;
gap: 9px;
min-width: 0;
}
.range-control label {
color: #526066;
font-size: 11px;
font-weight: 700;
white-space: nowrap;
}
.range-control output {
min-width: 36px;
color: var(--ink);
font-size: 11px;
font-weight: 750;
text-align: right;
}
input[type="range"] {
width: 92px;
height: 4px;
margin: 0;
appearance: none;
border-radius: 999px;
outline: none;
background: linear-gradient(90deg, var(--ink), #6d7a7e);
cursor: pointer;
}
input[type="range"]::-webkit-slider-thumb {
width: 15px;
height: 15px;
appearance: none;
border: 3px solid white;
border-radius: 50%;
background: var(--ink);
box-shadow: 0 2px 7px rgba(23,39,45,.28);
}
input[type="range"]::-moz-range-thumb {
width: 10px;
height: 10px;
border: 3px solid white;
border-radius: 50%;
background: var(--ink);
box-shadow: 0 2px 7px rgba(23,39,45,.28);
}
.reset-button {
display: grid;
place-items: center;
width: 38px;
height: 38px;
border: 1px solid rgba(25, 45, 50, .12);
border-radius: 12px;
background: rgba(255,255,255,.62);
cursor: pointer;
transition: transform .18s ease, background .18s ease;
}
.reset-button:hover {
transform: rotate(-8deg);
background: white;
}
.reset-button svg {
width: 17px;
height: 17px;
}
.workspace {
display: grid;
grid-template-columns: minmax(0, 1.35fr) minmax(360px, .9fr);
gap: 14px;
flex: 1;
min-height: 0;
padding: 0 18px 18px;
}
.panel {
position: relative;
min-width: 0;
min-height: 0;
overflow: hidden;
border: 1px solid var(--panel-border);
border-radius: 22px;
background: var(--paper);
box-shadow: var(--shadow);
isolation: isolate;
}
.scene-wrap {
position: absolute;
inset: 0;
}
canvas {
display: block;
width: 100%;
height: 100%;
outline: none;
touch-action: none;
}
.panel-head {
position: absolute;
z-index: 5;
top: 18px;
left: 18px;
right: 18px;
display: flex;
align-items: flex-start;
justify-content: space-between;
gap: 14px;
pointer-events: none;
}
.view-title {
display: flex;
align-items: center;
gap: 10px;
}
.view-number {
display: grid;
place-items: center;
width: 32px;
height: 32px;
border: 1px solid rgba(23,39,45,.13);
border-radius: 10px;
color: var(--ink);
background: rgba(255,255,255,.72);
font-size: 10px;
font-weight: 800;
box-shadow: 0 5px 14px rgba(30,45,48,.06);
backdrop-filter: blur(8px);
}
.view-title h2 {
margin: 0;
font-size: 15px;
letter-spacing: -.01em;
}
.view-title p {
margin: 3px 0 0;
color: var(--muted);
font-size: 10px;
letter-spacing: .07em;
text-transform: uppercase;
}
.view-chips {
display: flex;
flex-wrap: wrap;
justify-content: flex-end;
gap: 6px;
}
.view-chip {
padding: 7px 9px;
border: 1px solid rgba(23,39,45,.1);
border-radius: 9px;
color: #56646a;
background: rgba(255,255,255,.72);
font-size: 10px;
font-weight: 700;
box-shadow: 0 4px 12px rgba(30,45,48,.05);
backdrop-filter: blur(8px);
}
.view-chip strong {
color: var(--ink);
}
.panel-foot {
position: absolute;
z-index: 5;
left: 18px;
bottom: 18px;
display: flex;
align-items: center;
gap: 8px;
pointer-events: none;
}
.line-chip {
display: inline-flex;
align-items: center;
gap: 7px;
min-height: 29px;
padding: 0 10px;
border: 1px solid rgba(23,39,45,.1);
border-radius: 999px;
color: #4d5b61;
background: rgba(255,255,255,.78);
font-size: 10px;
font-weight: 750;
box-shadow: 0 5px 15px rgba(30,45,48,.06);
backdrop-filter: blur(8px);
}
.line-swatch {
width: 15px;
height: 4px;
border-radius: 999px;
background: var(--blue);
}
.line-swatch.orange {
background: var(--orange);
}
.interaction-hint {
position: absolute;
z-index: 5;
right: 18px;
bottom: 19px;
color: rgba(45,58,63,.58);
font-size: 9px;
font-weight: 650;
letter-spacing: .04em;
pointer-events: none;
}
.station-card {
position: absolute;
z-index: 6;
right: 18px;
bottom: 48px;
width: min(245px, calc(100% - 36px));
padding: 13px 14px 14px;
border: 1px solid rgba(23,39,45,.12);
border-radius: 14px;
background: rgba(255,255,255,.88);
box-shadow: 0 12px 28px rgba(29,42,45,.11);
backdrop-filter: blur(14px);
pointer-events: none;
}
.station-card::before {
content: "";
position: absolute;
top: 14px;
bottom: 14px;
left: 0;
width: 3px;
border-radius: 0 4px 4px 0;
background: var(--blue);
}
.station-card.transfer::before {
background: linear-gradient(var(--blue) 0 50%, var(--orange) 50% 100%);
}
.card-kicker {
margin-bottom: 4px;
color: var(--muted);
font-size: 9px;
font-weight: 800;
letter-spacing: .09em;
text-transform: uppercase;
}
.station-card h3 {
margin: 0;
font-size: 15px;
letter-spacing: -.015em;
}
.station-card p {
margin: 5px 0 0;
color: var(--muted);
font-size: 10px;
line-height: 1.45;
}
.transfer-badge {
display: inline-flex;
align-items: center;
gap: 5px;
margin-top: 9px;
padding: 5px 7px;
border-radius: 7px;
color: var(--green);
background: #e8f4ec;
font-size: 9px;
font-weight: 800;
letter-spacing: .05em;
text-transform: uppercase;
}
.transfer-badge::before {
content: "";
width: 5px;
height: 5px;
border-radius: 50%;
background: #3ba675;
}
.labels {
position: absolute;
z-index: 4;
inset: 0;
overflow: hidden;
pointer-events: none;
}
.station-label {
position: absolute;
display: flex;
align-items: center;
gap: 5px;
min-height: 24px;
padding: 4px 7px;
transform: translate(-50%, -50%);
border: 1px solid rgba(23,39,45,.13);
border-radius: 8px;
color: #34454b;
background: rgba(255,255,255,.88);
box-shadow: 0 4px 13px rgba(25,40,44,.09);
font-size: 9px;
font-weight: 750;
white-space: nowrap;
opacity: 0;
pointer-events: auto;
cursor: pointer;
backdrop-filter: blur(7px);
transition: opacity .15s ease, color .15s ease, border-color .15s ease,
background .15s ease, transform .15s ease;
}
.station-label:hover,
.station-label.selected {
color: var(--ink);
border-color: rgba(23,39,45,.28);
background: white;
}
.station-label.transfer {
min-height: 29px;
padding: 5px 9px;
color: white;
border-color: transparent;
background: var(--ink);
box-shadow: 0 7px 18px rgba(23,39,45,.23);
}
.station-label.transfer.selected,
.station-label.transfer:hover {
color: white;
background: #263b43;
}
.label-dot {
width: 6px;
height: 6px;
flex: 0 0 auto;
border-radius: 50%;
background: var(--blue);
}
.label-dot.orange {
background: var(--orange);
}
.transfer-label-mark {
display: grid;
grid-template-columns: 6px 6px;
gap: 2px;
}
.transfer-label-mark i {
width: 6px;
height: 6px;
border-radius: 50%;
background: var(--blue);
}
.transfer-label-mark i:last-child {
background: var(--orange);
}
.route-label {
position: absolute;
padding: 4px 7px;
transform: translate(-50%, -50%);
border-radius: 6px;
color: var(--blue);
background: rgba(255,255,255,.72);
font-size: 8px;
font-weight: 900;
letter-spacing: .12em;
opacity: 0;
pointer-events: none;
}
.route-label.orange {
color: var(--orange);
}
.map-panel {
background: #cbd7d3;
}
.diagram-panel {
background: #f5f1e8;
}
.map-panel .station-label {
background: rgba(251,250,245,.91);
}
@media (max-width: 1180px) {
.topbar {
grid-template-columns: auto 1fr auto;
}
.top-stats {
display: none;
}
}
@media (max-width: 850px) {
body {
overflow: auto;
}
.app {
height: auto;
}
.topbar {
grid-template-columns: 1fr auto;
gap: 12px;
}
.geometry-controls {
grid-column: 1 / -1;
grid-row: 2;
justify-content: stretch;
}
.range-control {
flex: 1;
grid-template-columns: auto 1fr auto;
}
input[type="range"] {
width: 100%;
}
.workspace {
grid-template-columns: 1fr;
min-height: auto;
}
.panel {
height: 620px;
}
}
@media (max-width: 560px) {
.topbar {
padding: 12px;
}
.geometry-controls {
flex-direction: column;
gap: 12px;
padding: 12px;
}
.workspace {
padding: 0 10px 10px;
}
.panel {
height: 570px;
border-radius: 18px;
}
.panel-head {
top: 14px;
left: 14px;
right: 14px;
}
.view-chips {
display: none;
}
.station-card {
right: 12px;
bottom: 49px;
}
.panel-foot {
left: 12px;
bottom: 12px;
}
.interaction-hint {
display: none;
}
}
</style>
</head>
<body>
<div class="app">
<header class="topbar">
<div class="brand">
<div class="brand-mark" aria-hidden="true">
<svg viewBox="0 0 32 32" fill="none">
<path d="M3 16H29" stroke="white" stroke-width="4" stroke-linecap="round"/>
<path d="M16 3V29" stroke="white" stroke-width="4" stroke-linecap="round"/>
<circle cx="16" cy="16" r="5.5" fill="#17272d" stroke="white" stroke-width="2.5"/>
<circle cx="16" cy="16" r="2" fill="#ed6847"/>
</svg>
</div>
<div>
<h1>Crossline</h1>
<p>Transit interchange atlas</p>
</div>
</div>
<div class="top-stats">
<div class="stat-pill">9 stations</div>
<div class="stat-pill">2 lines</div>
<div class="lock-pill"><span class="lock-dot"></span>Topology locked</div>
</div>
<div class="geometry-controls" aria-label="Geographic geometry controls">
<div class="range-control">
<label for="angleRange">Crossing angle</label>
<input id="angleRange" type="range" min="35" max="75" step="1" value="58">
<output id="angleValue" for="angleRange">58°</output>
</div>
<div class="range-control">
<label for="spacingRange">Station spread</label>
<input id="spacingRange" type="range" min="80" max="120" step="5" value="100">
<output id="spacingValue" for="spacingRange">100%</output>
</div>
</div>
<button class="reset-button" id="resetButton" type="button" aria-label="Reset geometry">
<svg viewBox="0 0 24 24" fill="none" aria-hidden="true">
<path d="M4 4v6h6M20 20v-6h-6" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>
<path d="M5.2 15.5A8 8 0 0 0 19 13M18.8 8.5A8 8 0 0 0 5 11" stroke="currentColor" stroke-width="2" stroke-linecap="round"/>
</svg>
</button>
</header>
<main class="workspace">
<section class="panel map-panel" aria-label="Geographic transit map">
<div class="scene-wrap" id="mapWrap">
<canvas id="mapCanvas" aria-label="Interactive low-poly geographic transit map"></canvas>
<div class="labels" id="mapLabels"></div>
</div>
<div class="panel-head">
<div class="view-title">
<div class="view-number">01</div>
<div>
<h2>Geographic view</h2>
<p>Live spatial geometry</p>
</div>
</div>
<div class="view-chips">
<div class="view-chip">Angle <strong id="mapAngleChip">58°</strong></div>
<div class="view-chip">Spread <strong id="mapSpreadChip">100%</strong></div>
</div>
</div>
<div class="panel-foot">
<div class="line-chip"><span class="line-swatch"></span>A · Harbor–Eastgate</div>
<div class="line-chip"><span class="line-swatch orange"></span>B · North–South</div>
</div>
<div class="station-card transfer" id="mapCard">
<div class="card-kicker">Selected station</div>
<h3>Central Interchange</h3>
<p>Shared by lines A and B. The transfer remains anchored while map geometry changes.</p>
<span class="transfer-badge">Transfer preserved</span>
</div>
<div class="interaction-hint">Drag to orbit · scroll to zoom · select a station</div>
</section>
<section class="panel diagram-panel" aria-label="Simplified transit diagram">
<div class="scene-wrap" id="diagramWrap">
<canvas id="diagramCanvas" aria-label="Simplified transit network diagram"></canvas>
<div class="labels" id="diagramLabels"></div>
</div>
<div class="panel-head">
<div class="view-title">
<div class="view-number">02</div>
<div>
<h2>Simplified diagram</h2>
<p>Normalized network topology</p>
</div>
</div>
<div class="view-chips">
<div class="view-chip">Angle <strong>90°</strong></div>
<div class="view-chip">Spacing <strong>Regular</strong></div>
</div>
</div>
<div class="panel-foot">
<div class="line-chip"><span class="line-swatch"></span>Line A</div>
<div class="line-chip"><span class="line-swatch orange"></span>Line B</div>
</div>
<div class="station-card transfer" id="diagramCard">
<div class="card-kicker">Selected station</div>
<h3>Central Interchange</h3>
<p>Same transfer node, redrawn with straight track angles and normalized station spacing.</p>
<span class="transfer-badge">Transfer preserved</span>
</div>
<div class="interaction-hint">Drag to pan · scroll to zoom</div>
</section>
</main>
</div>
<script type="module">
import * as THREE from "https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
const COLORS = {
blue: 0x2563eb,
orange: 0xed6847,
ink: 0x17272d,
white: 0xf8f5ec,
outline: 0x27383d
};
const stationMeta = {
A0: { name: "Harbor", line: "A", color: COLORS.blue, detail: "Western terminus on line A." },
A1: { name: "Market", line: "A", color: COLORS.blue, detail: "Two stops east of Harbor on line A." },
X: { name: "Central Interchange", line: "A + B", color: COLORS.ink, detail: "Shared by lines A and B. The transfer remains anchored while map geometry changes." },
A2: { name: "Museum", line: "A", color: COLORS.blue, detail: "Two stops west of Eastgate on line A." },
A3: { name: "Eastgate", line: "A", color: COLORS.blue, detail: "Eastern terminus on line A." },
B0: { name: "North Quay", line: "B", color: COLORS.orange, detail: "Northern terminus on line B." },
B1: { name: "Foundry", line: "B", color: COLORS.orange, detail: "Two stops south of North Quay on line B." },
B2: { name: "Civic", line: "B", color: COLORS.orange, detail: "Two stops north of Southbank on line B." },
B3: { name: "Southbank", line: "B", color: COLORS.orange, detail: "Southern terminus on line B." }
};
const selected = { key: "X" };
const angleRange = document.getElementById("angleRange");
const spacingRange = document.getElementById("spacingRange");
const angleValue = document.getElementById("angleValue");
const spacingValue = document.getElementById("spacingValue");
const mapAngleChip = document.getElementById("mapAngleChip");
const mapSpreadChip = document.getElementById("mapSpreadChip");
function updateCards() {
const meta = stationMeta[selected.key];
const cards = [document.getElementById("mapCard"), document.getElementById("diagramCard")];
for (const card of cards) {
card.classList.toggle("transfer", selected.key === "X");
card.querySelector(".card-kicker").textContent =
selected.key === "X" ? "Shared transfer" : `Line ${meta.line} station`;
card.querySelector("h3").textContent = meta.name;
card.querySelector("p").textContent = meta.detail;
const badge = card.querySelector(".transfer-badge");
badge.textContent = selected.key === "X" ? "Transfer preserved" : `Line ${meta.line}`;
}
document.querySelectorAll(".station-label").forEach(label => {
label.classList.toggle("selected", label.dataset.key === selected.key);
});
}
function selectStation(key) {
selected.key = key;
updateCards();
}
function projectPoint(point, camera, width, height) {
const p = point.clone().project(camera);
return {
x: (p.x * 0.5 + 0.5) * width,
y: (-p.y * 0.5 + 0.5) * height,
visible: p.z > -1 && p.z < 1
};
}
function positionLabel(element, world, camera, width, height, offset = [0, 0]) {
const p = projectPoint(world, camera, width, height);
const visible = p.visible && p.x > -80 && p.x < width + 80 && p.y > -50 && p.y < height + 50;
element.style.opacity = visible ? "1" : "0";
element.style.pointerEvents = visible ? "auto" : "none";
element.style.left = `${p.x + offset[0]}px`;
element.style.top = `${p.y + offset[1]}px`;
}
function makeStationLabel(container, key, name, color, isTransfer = false) {
const label = document.createElement("button");
label.type = "button";
label.className = `station-label${isTransfer ? " transfer" : ""}${key === selected.key ? " selected" : ""}`;
label.dataset.key = key;
label.setAttribute("aria-label", `Select ${name}`);
if (isTransfer) {
label.innerHTML = `<span class="transfer-label-mark"><i></i><i></i></span>${name} · Transfer`;
} else {
const dot = document.createElement("span");
dot.className = `label-dot${color === COLORS.orange ? " orange" : ""}`;
label.appendChild(dot);
label.appendChild(document.createTextNode(name));
}
label.addEventListener("pointerdown", event => event.stopPropagation());
label.addEventListener("click", event => {
event.stopPropagation();
selectStation(key);
});
container.appendChild(label);
return label;
}
function makeRouteLabel(container, text, color) {
const label = document.createElement("div");
label.className = `route-label${color === COLORS.orange ? " orange" : ""}`;
label.textContent = text;
container.appendChild(label);
return label;
}
function disposeGroup(group) {
group.traverse(object => {
if (object.geometry) object.geometry.dispose();
if (object.material) {
const materials = Array.isArray(object.material) ? object.material : [object.material];
materials.forEach(material => material.dispose());
}
});
group.clear();
}
function seededRandom(seed) {
return function() {
seed |= 0;
seed = seed + 0x6D2B79F5 | 0;
let t = Math.imul(seed ^ seed >>> 15, 1 | seed);
t = t + Math.imul(t ^ t >>> 7, 61 | t) ^ t;
return ((t ^ t >>> 14) >>> 0) / 4294967296;
};
}
// -------------------------------------------------------------------------
// Geographic view
// -------------------------------------------------------------------------
const mapCanvas = document.getElementById("mapCanvas");
const mapWrap = document.getElementById("mapWrap");
const mapLabels = document.getElementById("mapLabels");
const mapRenderer = new THREE.WebGLRenderer({
canvas: mapCanvas,
antialias: true,
alpha: false
});
mapRenderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
mapRenderer.outputColorSpace = THREE.SRGBColorSpace;
mapRenderer.toneMapping = THREE.ACESFilmicToneMapping;
mapRenderer.toneMappingExposure = 1.12;
mapRenderer.shadowMap.enabled = true;
mapRenderer.shadowMap.type = THREE.PCFSoftShadowMap;
const mapScene = new THREE.Scene();
mapScene.background = new THREE.Color(0xcbd7d3);
mapScene.fog = new THREE.Fog(0xcbd7d3, 28, 48);
const mapCamera = new THREE.PerspectiveCamera(39, 1, 0.1, 100);
const hemisphere = new THREE.HemisphereLight(0xf6f1df, 0x6c7776, 2.25);
mapScene.add(hemisphere);
const sun = new THREE.DirectionalLight(0xfff1d1, 3.2);
sun.position.set(-10, 18, 11);
sun.castShadow = true;
sun.shadow.mapSize.set(2048, 2048);
sun.shadow.camera.left = -22;
sun.shadow.camera.right = 22;
sun.shadow.camera.top = 18;
sun.shadow.camera.bottom = -18;
sun.shadow.camera.near = 1;
sun.shadow.camera.far = 55;
sun.shadow.bias = -0.0006;
mapScene.add(sun);
const fill = new THREE.DirectionalLight(0xa8cce0, 0.75);
fill.position.set(12, 8, -14);
mapScene.add(fill);
const ground = new THREE.Mesh(
new THREE.BoxGeometry(30, 0.5, 22),
new THREE.MeshStandardMaterial({ color: 0xd9d1bf, roughness: 0.96, flatShading: true })
);
ground.position.y = -0.3;
ground.receiveShadow = true;
mapScene.add(ground);
const roadMaterial = new THREE.MeshStandardMaterial({
color: 0xb8b6ad,
roughness: 1,
flatShading: true
});
for (const z of [-6.4, 3.8]) {
const road = new THREE.Mesh(new THREE.BoxGeometry(29, 0.035, 0.85), roadMaterial);
road.position.set(0, 0.01, z);
road.receiveShadow = true;
mapScene.add(road);
}
for (const x of [-9.4, 8.7]) {
const road = new THREE.Mesh(new THREE.BoxGeometry(0.85, 0.035, 21), roadMaterial);
road.position.set(x, 0.012, 0);
road.receiveShadow = true;
mapScene.add(road);
}
const park = new THREE.Mesh(
new THREE.BoxGeometry(4.5, 0.045, 3.1),
new THREE.MeshStandardMaterial({ color: 0x91aa82, roughness: 1, flatShading: true })
);
park.position.set(-10.7, 0.025, 6.8);
park.receiveShadow = true;
mapScene.add(park);
function addTree(x, z, scale = 1) {
const trunk = new THREE.Mesh(
new THREE.CylinderGeometry(0.09, 0.12, 0.55, 6),
new THREE.MeshStandardMaterial({ color: 0x76624e, roughness: 1, flatShading: true })
);
trunk.position.set(x, 0.28, z);
trunk.castShadow = true;
mapScene.add(trunk);
const crown = new THREE.Mesh(
new THREE.ConeGeometry(0.48, 1.25, 7),
new THREE.MeshStandardMaterial({ color: 0x648365, roughness: 1, flatShading: true })
);
crown.position.set(x, 1.05 * scale, z);
crown.scale.setScalar(scale);
crown.castShadow = true;
mapScene.add(crown);
}
[[-12.3,5.8], [-11.2,6.1], [-9.8,6], [-10.5,7.8], [-9.2,7.8]].forEach(
([x, z], i) => addTree(x, z, .82 + (i % 3) * .08)
);
function addBuildings(angle) {
const random = seededRandom(73921);
const palette = [0xd9c6b3, 0xc5d0ca, 0xe3d5bd, 0xb8c1bd, 0xd5b8a7, 0xc9c2aa];
const angleRad = THREE.MathUtils.degToRad(angle);
const sin = Math.sin(angleRad);
const cos = Math.cos(angleRad);
for (let x = -12; x <= 12; x += 3.35) {
for (let z = -8; z <= 8; z += 3.15) {
const distanceA = Math.abs(z);
const distanceB = Math.abs(-sin * x + cos * z);
if (distanceA < 1.45 || distanceB < 1.45 || random() < 0.27) continue;
if (x > -13 && x < -8 && z > 5 && z < 8.5) continue;
const width = 1.35 + random() * 1.15;
const depth = 1.2 + random() * 1.05;
const height = .42 + Math.pow(random(), 1.7) * 2.7;
const material = new THREE.MeshStandardMaterial({
color: palette[Math.floor(random() * palette.length)],
roughness: .9,
flatShading: true
});
const building = new THREE.Mesh(
new THREE.BoxGeometry(width, height, depth),
material
);
building.position.set(x, height / 2 + 0.03, z);
building.castShadow = true;
building.receiveShadow = true;
mapScene.add(building);
if (height > 1.55 && random() > .4) {
const roof = new THREE.Mesh(
new THREE.BoxGeometry(width * .45, .22, depth * .45),
new THREE.MeshStandardMaterial({
color: 0xa9aaa3,
roughness: .9,
flatShading: true
})
);
roof.position.set(x, height + .14, z);
roof.castShadow = true;
mapScene.add(roof);
}
}
}
}
const mapRouteGroup = new THREE.Group();
mapScene.add(mapRouteGroup);
let mapCurves = {};
let mapTrains = [];
const mapLabelEntries = [];
let mapTransferPulse;
function createMapStation(position, key, lineColor) {
const group = new THREE.Group();
group.position.copy(position);
group.userData.stationKey = key;
const base = new THREE.Mesh(
new THREE.CylinderGeometry(.36, .39, .13, 12),
new THREE.MeshStandardMaterial({ color: COLORS.outline, roughness: .75, flatShading: true })
);
base.position.y = .66;
base.castShadow = true;
base.userData.stationKey = key;
group.add(base);
const top = new THREE.Mesh(
new THREE.CylinderGeometry(.28, .31, .12, 12),
new THREE.MeshStandardMaterial({ color: COLORS.white, roughness: .7, flatShading: true })
);
top.position.y = .75;
top.castShadow = true;
top.userData.stationKey = key;
group.add(top);
const dot = new THREE.Mesh(
new THREE.CylinderGeometry(.13, .13, .13, 10),
new THREE.MeshStandardMaterial({ color: lineColor, roughness: .65, flatShading: true })
);
dot.position.y = .84;
dot.userData.stationKey = key;
group.add(dot);
mapRouteGroup.add(group);
}
function createMapTransfer(position) {
const group = new THREE.Group();
group.position.copy(position);
const darkRing = new THREE.Mesh(
new THREE.RingGeometry(.72, 1.08, 32),
new THREE.MeshBasicMaterial({ color: COLORS.outline, side: THREE.DoubleSide })
);
darkRing.rotation.x = -Math.PI / 2;
darkRing.position.y = .74;
group.add(darkRing);
const blueHalf = new THREE.Mesh(
new THREE.RingGeometry(.72, .99, 24, 1, 0, Math.PI),
new THREE.MeshBasicMaterial({ color: COLORS.blue, side: THREE.DoubleSide })
);
blueHalf.rotation.x = -Math.PI / 2;
blueHalf.position.y = .76;
group.add(blueHalf);
const orangeHalf = new THREE.Mesh(
new THREE.RingGeometry(.72, .99, 24, 1, Math.PI, Math.PI),
new THREE.MeshBasicMaterial({ color: COLORS.orange, side: THREE.DoubleSide })
);
orangeHalf.rotation.x = -Math.PI / 2;
orangeHalf.position.y = .76;
group.add(orangeHalf);
const center = new THREE.Mesh(
new THREE.CylinderGeometry(.68, .68, .23, 12),
new THREE.MeshStandardMaterial({ color: COLORS.white, roughness: .72, flatShading: true })
);
center.position.y = .79;
center.castShadow = true;
group.add(center);
const centerDot = new THREE.Mesh(
new THREE.CylinderGeometry(.15, .15, .25, 10),
new THREE.MeshStandardMaterial({ color: COLORS.ink, roughness: .6, flatShading: true })
);
centerDot.position.y = .94;
group.add(centerDot);
mapTransferPulse = new THREE.Mesh(
new THREE.TorusGeometry(1.18, .025, 5, 36),
new THREE.MeshBasicMaterial({ color: COLORS.white, transparent: true, opacity: .8 })
);
mapTransferPulse.rotation.x = Math.PI / 2;
mapTransferPulse.position.y = .77;
group.add(mapTransferPulse);
mapRouteGroup.add(group);
}
function createTrain(color) {
const train = new THREE.Group();
const bodyMaterial = new THREE.MeshStandardMaterial({
color: 0xf5f2e9,
roughness: .65,
flatShading: true
});
const roofMaterial = new THREE.MeshStandardMaterial({
color,
roughness: .6,
flatShading: true
});
const windowMaterial = new THREE.MeshStandardMaterial({
color: 0x294047,
roughness: .4,
flatShading: true
});
for (let i = -1; i <= 1; i++) {
const car = new THREE.Mesh(new THREE.BoxGeometry(.52, .28, .42), bodyMaterial);
car.position.x = i * .58;
car.castShadow = true;
train.add(car);
const roof = new THREE.Mesh(new THREE.BoxGeometry(.42, .08, .34), roofMaterial);
roof.position.set(i * .58, .17, 0);
train.add(roof);
for (const side of [-1, 1]) {
const window = new THREE.Mesh(new THREE.BoxGeometry(.33, .10, .018), windowMaterial);
window.position.set(i * .58, .02, side * .218);
train.add(window);
}
}
mapRouteGroup.add(train);
return train;
}
function buildMapGeometry(angle = Number(angleRange.value), spacingPercent = Number(spacingRange.value)) {
disposeGroup(mapRouteGroup);
mapLabels.replaceChildren();
mapTrains = [];
mapCurves = {};
const angleRad = THREE.MathUtils.degToRad(angle);
const spacing = spacingPercent / 100;
const directionA = new THREE.Vector3(1, 0, 0);
const directionB = new THREE.Vector3(Math.cos(angleRad), 0, Math.sin(angleRad));
const perpendicularA = new THREE.Vector3(0, 0, 1);
const perpendicularB = new THREE.Vector3(-Math.sin(angleRad), 0, Math.cos(angleRad));
const controlsA = [-10, -7.5, -5, -2.5, 0, 2.5, 5, 7.5, 10];
const bendsA = [0, .28, -.18, .08, 0, -.12, .19, -.22, 0];
const controlsB = [-8, -6, -4, -2, 0, 2, 4, 6, 8];
const bendsB = [0, -.22, .17, -.08, 0, .16, -.18, .24, 0];
const pointsA = controlsA.map((s, index) =>
directionA.clone().multiplyScalar(s * spacing).addScaledVector(perpendicularA, bendsA[index])
);
const pointsB = controlsB.map((s, index) =>
directionB.clone().multiplyScalar(s * spacing).addScaledVector(perpendicularB, bendsB[index])
);
pointsA.forEach(p => p.y = .5);
pointsB.forEach(p => p.y = .5);
mapCurves.A = new THREE.CatmullRomCurve3(pointsA, false, "catmullrom", .12);
mapCurves.B = new THREE.CatmullRomCurve3(pointsB, false, "catmullrom", .12);
function addTrack(curve, color) {
const outline = new THREE.Mesh(
new THREE.TubeGeometry(curve, 100, .24, 6, false),
new THREE.MeshStandardMaterial({
color: COLORS.outline,
roughness: .78,
flatShading: true
})
);
outline.position.y = -.08;
outline.castShadow = true;
mapRouteGroup.add(outline);
const line = new THREE.Mesh(
new THREE.TubeGeometry(curve, 100, .145, 6, false),
new THREE.MeshStandardMaterial({
color,
roughness: .62,
flatShading: true
})
);
line.castShadow = true;
mapRouteGroup.add(line);
}
addTrack(mapCurves.A, COLORS.blue);
addTrack(mapCurves.B, COLORS.orange);
mapLabelEntries.length = 0;
const aStations = [
{ key: "A0", s: -10, name: "Harbor", offset: [0, -15] },
{ key: "A1", s: -5, name: "Market", offset: [0, 17] },
{ key: "A2", s: 5, name: "Museum", offset: [0, -16] },
{ key: "A3", s: 10, name: "Eastgate", offset: [0, 17] }
];
const bStations = [
{ key: "B0", s: -8, name: "North Quay", offset: [0, -16] },
{ key: "B1", s: -4, name: "Foundry", offset: [-18, 16] },
{ key: "B2", s: 4, name: "Civic", offset: [18, -16] },
{ key: "B3", s: 8, name: "Southbank", offset: [0, 18] }
];
for (const station of aStations) {
const position = directionA.clone().multiplyScalar(station.s * spacing)
.addScaledVector(perpendicularA, bendsA[controlsA.indexOf(station.s)]);
position.y = .5;
createMapStation(position, station.key, COLORS.blue);
const label = makeStationLabel(mapLabels, station.key, station.name, COLORS.blue);
mapLabelEntries.push({ label, position: position.clone().setY(.95), offset: station.offset });
}
for (const station of bStations) {
const position = directionB.clone().multiplyScalar(station.s * spacing)
.addScaledVector(perpendicularB, bendsB[controlsB.indexOf(station.s)]);
position.y = .5;
createMapStation(position, station.key, COLORS.orange);
const label = makeStationLabel(mapLabels, station.key, station.name, COLORS.orange);
mapLabelEntries.push({ label, position: position.clone().setY(.95), offset: station.offset });
}
const transferPosition = new THREE.Vector3(0, .5, 0);
createMapTransfer(transferPosition);
const transferLabel = makeStationLabel(
mapLabels,
"X",
"Central",
COLORS.ink,
true
);
mapLabelEntries.push({
label: transferLabel,
position: transferPosition.clone().setY(1.08),
offset: [60, -38]
});
const routeLabelA = makeRouteLabel(mapLabels, "LINE A", COLORS.blue);
const routeLabelB = makeRouteLabel(mapLabels, "LINE B", COLORS.orange);
mapLabelEntries.push({
label: routeLabelA,
position: new THREE.Vector3(-7.8, .8, .85),
offset: [0, 0]
});
mapLabelEntries.push({
label: routeLabelB,
position: directionB.clone().multiplyScalar(-5.6 * spacing).add(new THREE.Vector3(0, .8, 0)),
offset: [0, 0]
});
mapTrains.push({ object: createTrain(COLORS.blue), curve: mapCurves.A, speed: .045, offset: .1 });
mapTrains.push({ object: createTrain(COLORS.orange), curve: mapCurves.B, speed: .037, offset: .48 });
mapLabelEntries.forEach(entry => {
entry.update = () => {
positionLabel(
entry.label,
entry.position,
mapCamera,
mapWrap.clientWidth,
mapWrap.clientHeight,
entry.offset
);
};
});
mapLabelEntries.forEach(entry => entry.update());
updateCards();
}
const mapControls = {
target: new THREE.Vector3(0, .15, 0),
radius: 25,
desiredRadius: 25,
theta: .72,
desiredTheta: .72,
phi: .87,
desiredPhi: .87,
dragging: false,
lastX: 0,
lastY: 0,
moved: 0,
attach(canvas) {
canvas.addEventListener("pointerdown", event => {
this.dragging = true;
this.moved = 0;
this.lastX = event.clientX;
this.lastY = event.clientY;
canvas.setPointerCapture(event.pointerId);
canvas.style.cursor = "grabbing";
});
canvas.addEventListener("pointermove", event => {
if (!this.dragging) return;
const dx = event.clientX - this.lastX;
const dy = event.clientY - this.lastY;
this.moved += Math.abs(dx) + Math.abs(dy);
this.lastX = event.clientX;
this.lastY = event.clientY;
this.desiredTheta -= dx * .006;
this.desiredPhi = THREE.MathUtils.clamp(this.desiredPhi - dy * .005, .36, 1.25);
});
const stop = event => {
this.dragging = false;
canvas.style.cursor = "grab";
if (canvas.hasPointerCapture(event.pointerId)) canvas.releasePointerCapture(event.pointerId);
};
canvas.addEventListener("pointerup", stop);
canvas.addEventListener("pointercancel", stop);
canvas.addEventListener("wheel", event => {
event.preventDefault();
this.desiredRadius = THREE.MathUtils.clamp(
this.desiredRadius * Math.exp(event.deltaY * .001),
15,
39
);
}, { passive: false });
canvas.style.cursor = "grab";
},
update() {
const smoothing = .1;
this.theta += (this.desiredTheta - this.theta) * smoothing;
this.phi += (this.desiredPhi - this.phi) * smoothing;
this.radius += (this.desiredRadius - this.radius) * smoothing;
const sinPhi = Math.sin(this.phi);
mapCamera.position.set(
this.target.x + this.radius * sinPhi * Math.cos(this.theta),
this.target.y + this.radius * Math.cos(this.phi),
this.target.z + this.radius * sinPhi * Math.sin(this.theta)
);
mapCamera.lookAt(this.target);
}
};
mapControls.attach(mapCanvas);
const mapRaycaster = new THREE.Raycaster();
const mapPointer = new THREE.Vector2();
mapCanvas.addEventListener("pointerup", event => {
if (mapControls.moved > 5) return;
const rect = mapCanvas.getBoundingClientRect();
mapPointer.x = ((event.clientX - rect.left) / rect.width) * 2 - 1;
mapPointer.y = -((event.clientY - rect.top) / rect.height) * 2 + 1;
mapRaycaster.setFromCamera(mapPointer, mapCamera);
const hits = mapRaycaster.intersectObjects(mapRouteGroup.children, true);
for (const hit of hits) {
let object = hit.object;
while (object && !object.userData.stationKey) object = object.parent;
if (object?.userData.stationKey) {
selectStation(object.userData.stationKey);
break;
}
}
});
// -------------------------------------------------------------------------
// Simplified diagram
// -------------------------------------------------------------------------
const diagramCanvas = document.getElementById("diagramCanvas");
const diagramWrap = document.getElementById("diagramWrap");
const diagramLabels = document.getElementById("diagramLabels");
const diagramRenderer = new THREE.WebGLRenderer({
canvas: diagramCanvas,
antialias: true,
alpha: false
});
diagramRenderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
diagramRenderer.outputColorSpace = THREE.SRGBColorSpace;
diagramRenderer.toneMapping = THREE.ACESFilmicToneMapping;
diagramRenderer.toneMappingExposure = 1.05;
const diagramScene = new THREE.Scene();
diagramScene.background = new THREE.Color(0xf5f1e8);
const diagramCamera = new THREE.OrthographicCamera(-10, 10, 8, -8, .1, 100);
diagramCamera.position.set(0, 0, 25);
diagramCamera.lookAt(0, 0, 0);
const diagramAmbient = new THREE.HemisphereLight(0xffffff, 0xd7d0c2, 2.3);
diagramScene.add(diagramAmbient);
const diagramLight = new THREE.DirectionalLight(0xffffff, 1.4);
diagramLight.position.set(4, 5, 12);
diagramScene.add(diagramLight);
const gridVertices = [];
for (let x = -12; x <= 12; x++) {
gridVertices.push(x, -8, -.2, x, 8, -.2);
}
for (let y = -8; y <= 8; y++) {
gridVertices.push(-12, y, -.2, 12, y, -.2);
}
const gridGeometry = new THREE.BufferGeometry();
gridGeometry.setAttribute("position", new THREE.Float32BufferAttribute(gridVertices, 3));
const grid = new THREE.LineSegments(
gridGeometry,
new THREE.LineBasicMaterial({ color: 0xd9d4c8, transparent: true, opacity: .58 })
);
diagramScene.add(grid);
const diagramRouteGroup = new THREE.Group();
diagramScene.add(diagramRouteGroup);
let diagramTransferPulse;
const diagramLabelEntries = [];
function createDiagramStation(position, key, lineColor) {
const group = new THREE.Group();
group.position.copy(position);
const outer = new THREE.Mesh(
new THREE.CylinderGeometry(.35, .35, .14, 16),
new THREE.MeshStandardMaterial({ color: COLORS.outline, roughness: .7, flatShading: true })
);
outer.rotation.x = Math.PI / 2;
outer.position.z = .27;
outer.userData.stationKey = key;
group.add(outer);
const inner = new THREE.Mesh(
new THREE.CylinderGeometry(.25, .25, .16, 16),
new THREE.MeshStandardMaterial({ color: COLORS.white, roughness: .7, flatShading: true })
);
inner.rotation.x = Math.PI / 2;
inner.position.z = .37;
inner.userData.stationKey = key;
group.add(inner);
const dot = new THREE.Mesh(
new THREE.CylinderGeometry(.11, .11, .17, 12),
new THREE.MeshStandardMaterial({ color: lineColor, roughness: .6, flatShading: true })
);
dot.rotation.x = Math.PI / 2;
dot.position.z = .47;
dot.userData.stationKey = key;
group.add(dot);
diagramRouteGroup.add(group);
}
function createDiagramTransfer(position) {
const group = new THREE.Group();
group.position.copy(position);
const dark = new THREE.Mesh(
new THREE.RingGeometry(.73, 1.08, 32),
new THREE.MeshBasicMaterial({ color: COLORS.outline, side: THREE.DoubleSide })
);
dark.position.z = .38;
group.add(dark);
const blueHalf = new THREE.Mesh(
new THREE.RingGeometry(.73, .99, 24, 1, 0, Math.PI),
new THREE.MeshBasicMaterial({ color: COLORS.blue, side: THREE.DoubleSide })
);
blueHalf.position.z = .4;
group.add(blueHalf);
const orangeHalf = new THREE.Mesh(
new THREE.RingGeometry(.73, .99, 24, 1, Math.PI, Math.PI),
new THREE.MeshBasicMaterial({ color: COLORS.orange, side: THREE.DoubleSide })
);
orangeHalf.position.z = .4;
group.add(orangeHalf);
const center = new THREE.Mesh(
new THREE.CylinderGeometry(.68, .68, .22, 16),
new THREE.MeshStandardMaterial({ color: COLORS.white, roughness: .65, flatShading: true })
);
center.rotation.x = Math.PI / 2;
center.position.z = .43;
group.add(center);
const dot = new THREE.Mesh(
new THREE.CylinderGeometry(.14, .14, .24, 12),
new THREE.MeshStandardMaterial({ color: COLORS.ink, roughness: .6, flatShading: true })
);
dot.rotation.x = Math.PI / 2;
dot.position.z = .57;
group.add(dot);
diagramTransferPulse = new THREE.Mesh(
new THREE.TorusGeometry(1.18, .022, 5, 36),
new THREE.MeshBasicMaterial({ color: COLORS.ink, transparent: true, opacity: .34 })
);
diagramTransferPulse.position.z = .62;
group.add(diagramTransferPulse);
diagramRouteGroup.add(group);
}
function buildDiagram() {
disposeGroup(diagramRouteGroup);
diagramLabels.replaceChildren();
diagramLabelEntries.length = 0;
const pointsA = [-8, -4, 0, 4, 8].map(x => new THREE.Vector3(x, 0, .04));
const pointsB = [-6.5, -3.25, 0, 3.25, 6.5].map(y => new THREE.Vector3(0, y, .04));
const curveA = new THREE.CatmullRomCurve3(pointsA, false, "catmullrom", 0);
const curveB = new THREE.CatmullRomCurve3(pointsB, false, "catmullrom", 0);
function addTrack(curve, color) {
const outline = new THREE.Mesh(
new THREE.TubeGeometry(curve, 72, .23, 6, false),
new THREE.MeshStandardMaterial({ color: COLORS.outline, roughness: .78, flatShading: true })
);
outline.position.z = -.08;
diagramRouteGroup.add(outline);
const line = new THREE.Mesh(
new THREE.TubeGeometry(curve, 72, .14, 6, false),
new THREE.MeshStandardMaterial({ color, roughness: .62, flatShading: true })
);
line.position.z = .02;
diagramRouteGroup.add(line);
}
addTrack(curveA, COLORS.blue);
addTrack(curveB, COLORS.orange);
const diagramStations = [
{ key: "A0", name: "Harbor", position: new THREE.Vector3(-8, 0, .2), color: COLORS.blue, offset: [0, 23] },
{ key: "A1", name: "Market", position: new THREE.Vector3(-4, 0, .2), color: COLORS.blue, offset: [0, -24] },
{ key: "A2", name: "Museum", position: new THREE.Vector3(4, 0, .2), color: COLORS.blue, offset: [0, -24] },
{ key: "A3", name: "Eastgate", position: new THREE.Vector3(8, 0, .2), color: COLORS.blue, offset: [0, 23] },
{ key: "B0", name: "North Quay", position: new THREE.Vector3(0, -6.5, .2), color: COLORS.orange, offset: [48, 0] },
{ key: "B1", name: "Foundry", position: new THREE.Vector3(0, -3.25, .2), color: COLORS.orange, offset: [47, 0] },
{ key: "B2", name: "Civic", position: new THREE.Vector3(0, 3.25, .2), color: COLORS.orange, offset: [38, 0] },
{ key: "B3", name: "Southbank", position: new THREE.Vector3(0, 6.5, .2), color: COLORS.orange, offset: [53, 0] }
];
for (const station of diagramStations) {
createDiagramStation(station.position, station.key, station.color);
const label = makeStationLabel(diagramLabels, station.key, station.name, station.color);
diagramLabelEntries.push({
label,
position: station.position.clone().setZ(.75),
offset: station.offset
});
}
const transferPosition = new THREE.Vector3(0, 0, .2);
createDiagramTransfer(transferPosition);
const transferLabel = makeStationLabel(
diagramLabels,
"X",
"Central",
COLORS.ink,
true
);
diagramLabelEntries.push({
label: transferLabel,
position: transferPosition.clone().setZ(.85),
offset: [55, -35]
});
const routeA = makeRouteLabel(diagramLabels, "LINE A", COLORS.blue);
const routeB = makeRouteLabel(diagramLabels, "LINE B", COLORS.orange);
diagramLabelEntries.push({
label: routeA,
position: new THREE.Vector3(-7.8, .58, .4),
offset: [0, 0]
});
diagramLabelEntries.push({
label: routeB,
position: new THREE.Vector3(.58, 6.8, .4),
offset: [0, 0]
});
diagramLabelEntries.forEach(entry => {
entry.update = () => {
positionLabel(
entry.label,
entry.position,
diagramCamera,
diagramWrap.clientWidth,
diagramWrap.clientHeight,
entry.offset
);
};
});
diagramLabelEntries.forEach(entry => entry.update());
updateCards();
}
const diagramControls = {
target: new THREE.Vector2(0, 0),
zoom: 1,
desiredZoom: 1,
dragging: false,
lastX: 0,
lastY: 0,
moved: 0,
attach(canvas) {
canvas.addEventListener("pointerdown", event => {
this.dragging = true;
this.moved = 0;
this.lastX = event.clientX;
this.lastY = event.clientY;
canvas.setPointerCapture(event.pointerId);
canvas.style.cursor = "grabbing";
});
canvas.addEventListener("pointermove", event => {
if (!this.dragging) return;
const dx = event.clientX - this.lastX;
const dy = event.clientY - this.lastY;
this.moved += Math.abs(dx) + Math.abs(dy);
this.lastX = event.clientX;
this.lastY = event.clientY;
const viewWidth = diagramCamera.right - diagramCamera.left;
const viewHeight = diagramCamera.top - diagramCamera.bottom;
this.target.x -= dx * viewWidth / canvas.clientWidth / this.zoom;
this.target.y += dy * viewHeight / canvas.clientHeight / this.zoom;
});
const stop = event => {
this.dragging = false;
canvas.style.cursor = "grab";
if (canvas.hasPointerCapture(event.pointerId)) canvas.releasePointerCapture(event.pointerId);
};
canvas.addEventListener("pointerup", stop);
canvas.addEventListener("pointercancel", stop);
canvas.addEventListener("wheel", event => {
event.preventDefault();
this.desiredZoom = THREE.MathUtils.clamp(
this.desiredZoom * Math.exp(-event.deltaY * .001),
.72,
1.8
);
}, { passive: false });
canvas.style.cursor = "grab";
},
update() {
this.zoom += (this.desiredZoom - this.zoom) * .12;
diagramCamera.position.x = this.target.x;
diagramCamera.position.y = this.target.y;
diagramCamera.lookAt(this.target.x, this.target.y, 0);
}
};
diagramControls.attach(diagramCanvas);
const diagramRaycaster = new THREE.Raycaster();
const diagramPointer = new THREE.Vector2();
diagramCanvas.addEventListener("pointerup", event => {
if (diagramControls.moved > 5) return;
const rect = diagramCanvas.getBoundingClientRect();
diagramPointer.x = ((event.clientX - rect.left) / rect.width) * 2 - 1;
diagramPointer.y = -((event.clientY - rect.top) / rect.height) * 2 + 1;
diagramRaycaster.setFromCamera(diagramPointer, diagramCamera);
const hits = diagramRaycaster.intersectObjects(diagramRouteGroup.children, true);
for (const hit of hits) {
let object = hit.object;
while (object && !object.userData.stationKey) object = object.parent;
if (object?.userData.stationKey) {
selectStation(object.userData.stationKey);
break;
}
}
});
// -------------------------------------------------------------------------
// Resizing and controls
// -------------------------------------------------------------------------
function resizeMap() {
const width = Math.max(1, mapWrap.clientWidth);
const height = Math.max(1, mapWrap.clientHeight);
mapRenderer.setSize(width, height, false);
mapCamera.aspect = width / height;
mapCamera.updateProjectionMatrix();
}
function resizeDiagram() {
const width = Math.max(1, diagramWrap.clientWidth);
const height = Math.max(1, diagramWrap.clientHeight);
diagramRenderer.setSize(width, height, false);
const aspect = width / height;
const viewHeight = 15.6;
diagramCamera.left = -viewHeight * aspect / 2;
diagramCamera.right = viewHeight * aspect / 2;
diagramCamera.top = viewHeight / 2;
diagramCamera.bottom = -viewHeight / 2;
diagramCamera.updateProjectionMatrix();
}
const mapResizeObserver = new ResizeObserver(resizeMap);
const diagramResizeObserver = new ResizeObserver(resizeDiagram);
mapResizeObserver.observe(mapWrap);
diagramResizeObserver.observe(diagramWrap);
function updateGeometryUI() {
const angle = Number(angleRange.value);
const spacing = Number(spacingRange.value);
angleValue.textContent = `${angle}°`;
spacingValue.textContent = `${spacing}%`;
mapAngleChip.textContent = `${angle}°`;
mapSpreadChip.textContent = `${spacing}%`;
buildMapGeometry(angle, spacing);
}
angleRange.addEventListener("input", updateGeometryUI);
spacingRange.addEventListener("input", updateGeometryUI);
document.getElementById("resetButton").addEventListener("click", () => {
angleRange.value = 58;
spacingRange.value = 100;
mapControls.desiredTheta = .72;
mapControls.desiredPhi = .87;
mapControls.desiredRadius = 25;
diagramControls.target.set(0, 0);
diagramControls.desiredZoom = 1;
updateGeometryUI();
});
// Build both representations. The diagram always keeps one common transfer
// node even though its angles and spacing are normalized independently.
resizeMap();
resizeDiagram();
buildMapGeometry(Number(angleRange.value), Number(spacingRange.value));
buildDiagram();
updateCards();
const clock = new THREE.Clock();
function animate() {
requestAnimationFrame(animate);
const elapsed = clock.getElapsedTime();
mapControls.update();
diagramControls.update();
for (const train of mapTrains) {
const phase = (elapsed * train.speed + train.offset) % 1;
const t = phase < .5 ? phase * 2 : 2 - phase * 2;
const point = train.curve.getPointAt(t);
const tangent = train.curve.getTangentAt(t).normalize();
train.object.position.copy(point);
train.object.position.y += .28;
train.object.rotation.y = -Math.atan2(tangent.z, tangent.x);
}
if (mapTransferPulse) {
const pulse = 1 + (Math.sin(elapsed * 2.2) * .5 + .5) * .13;
mapTransferPulse.scale.setScalar(pulse);
mapTransferPulse.material.opacity = .72 - (pulse - 1) * 2.2;
}
if (diagramTransferPulse) {
const pulse = 1 + (Math.sin(elapsed * 2.2) * .5 + .5) * .13;
diagramTransferPulse.scale.setScalar(pulse);
diagramTransferPulse.material.opacity = .34 - (pulse - 1) * .9;
}
mapLabelEntries.forEach(entry => entry.update());
diagramLabelEntries.forEach(entry => entry.update());
mapRenderer.render(mapScene, mapCamera);
diagramRenderer.render(diagramScene, diagramCamera);
}
animate();
</script>
</body>
</html>
```