Prompt specification
Build or customize the Three.js 3D scene "Projection choice for area or navigation" 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: Projection choice for area or navigation
Category: Space & Earth · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/projection-choice-for-area-or-navigation-x1320
Scene Description Prompt:
Please create one self-contained HTML file with Three.js from cdnjs showing map projections. Let users switch between an area-preserving and a cylindrical angle-preserving map. Keep a selected region highlighted and show which measured property each view preserves. Low-poly.
Working Three.js Starter (projection-choice-for-area-or-navigation-x1320):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Projection Lab — Area vs. Angle</title>
<style>
:root {
color-scheme: dark;
--page: #06131d;
--panel: rgba(9, 27, 38, 0.86);
--panel-border: rgba(170, 218, 220, 0.16);
--text: #e8f4f1;
--muted: #93aeb5;
--accent: #6be0c3;
--accent-soft: rgba(107, 224, 195, 0.14);
--land: #348d82;
--selected: #ffb454;
--shadow: 0 18px 50px rgba(0, 0, 0, 0.34);
}
body[data-projection="mercator"] {
--accent: #72b9ff;
--accent-soft: rgba(114, 185, 255, 0.14);
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: var(--page);
color: var(--text);
font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
body {
background:
radial-gradient(circle at 50% 42%, rgba(37, 91, 98, 0.18), transparent 42%),
linear-gradient(145deg, #071823, #040d15 70%);
}
button,
select {
font: inherit;
}
button {
color: inherit;
}
#stage {
position: fixed;
inset: 0;
}
#stage canvas {
display: block;
width: 100%;
height: 100%;
touch-action: none;
outline: none;
}
.topbar {
position: fixed;
z-index: 10;
top: 22px;
left: 24px;
right: 24px;
display: flex;
align-items: flex-start;
justify-content: space-between;
gap: 24px;
pointer-events: none;
}
.brand {
max-width: 430px;
text-shadow: 0 2px 18px rgba(0, 0, 0, 0.55);
}
.eyebrow {
display: flex;
align-items: center;
gap: 9px;
color: var(--accent);
font-size: 11px;
font-weight: 800;
letter-spacing: 0.17em;
text-transform: uppercase;
transition: color 250ms ease;
}
.eyebrow::before {
content: "";
width: 8px;
height: 8px;
border: 2px solid currentColor;
transform: rotate(45deg);
}
h1 {
margin: 8px 0 4px;
font-size: clamp(28px, 3.4vw, 48px);
line-height: 1;
letter-spacing: -0.045em;
font-weight: 760;
}
.subtitle {
margin: 0;
color: var(--muted);
font-size: 13px;
line-height: 1.5;
}
.projection-switch {
display: flex;
gap: 5px;
padding: 5px;
border: 1px solid var(--panel-border);
border-radius: 14px;
background: rgba(6, 20, 29, 0.82);
box-shadow: var(--shadow);
backdrop-filter: blur(16px);
pointer-events: auto;
}
.projection-switch button {
position: relative;
min-width: 150px;
padding: 11px 15px 10px;
border: 0;
border-radius: 10px;
background: transparent;
cursor: pointer;
text-align: left;
transition: background 180ms ease, color 180ms ease, transform 180ms ease;
}
.projection-switch button:hover {
background: rgba(255, 255, 255, 0.045);
}
.projection-switch button:focus-visible,
select:focus-visible,
.zoom-controls button:focus-visible {
outline: 2px solid var(--accent);
outline-offset: 3px;
}
.projection-switch button[aria-pressed="true"] {
background: var(--accent-soft);
color: var(--accent);
}
.projection-switch strong {
display: block;
font-size: 13px;
letter-spacing: -0.01em;
}
.projection-switch small {
display: block;
margin-top: 2px;
color: var(--muted);
font-size: 9px;
font-weight: 800;
letter-spacing: 0.12em;
text-transform: uppercase;
}
.projection-switch button[aria-pressed="true"] small {
color: color-mix(in srgb, var(--accent) 70%, white);
}
.panel {
position: fixed;
z-index: 9;
border: 1px solid var(--panel-border);
border-radius: 18px;
background: var(--panel);
box-shadow: var(--shadow);
backdrop-filter: blur(18px) saturate(120%);
}
.property-panel {
left: 24px;
bottom: 24px;
width: min(370px, calc(100vw - 48px));
padding: 20px;
}
.panel-kicker {
margin-bottom: 7px;
color: var(--accent);
font-size: 10px;
font-weight: 850;
letter-spacing: 0.16em;
text-transform: uppercase;
transition: color 250ms ease;
}
.property-panel h2 {
margin: 0;
font-size: 25px;
line-height: 1.1;
letter-spacing: -0.035em;
}
.description {
min-height: 48px;
margin: 9px 0 16px;
color: var(--muted);
font-size: 12px;
line-height: 1.55;
}
.measure {
display: grid;
grid-template-columns: auto 1fr;
align-items: center;
gap: 15px;
padding: 14px;
border: 1px solid color-mix(in srgb, var(--accent) 28%, transparent);
border-radius: 13px;
background: var(--accent-soft);
transition: border-color 250ms ease, background 250ms ease;
}
.measure-value {
color: var(--accent);
font-size: 30px;
font-weight: 780;
line-height: 1;
letter-spacing: -0.05em;
font-variant-numeric: tabular-nums;
transition: color 250ms ease;
}
.measure-label {
color: var(--text);
font-size: 11px;
font-weight: 750;
}
.measure-secondary {
margin-top: 4px;
color: var(--muted);
font-size: 10px;
line-height: 1.35;
}
.metric-grid {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 9px;
margin-top: 10px;
}
.metric {
padding: 10px 11px;
border: 1px solid rgba(255, 255, 255, 0.065);
border-radius: 11px;
background: rgba(255, 255, 255, 0.025);
}
.metric span {
display: block;
color: var(--muted);
font-size: 9px;
font-weight: 750;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.metric strong {
display: block;
margin-top: 5px;
font-size: 13px;
font-variant-numeric: tabular-nums;
}
.note {
margin: 11px 1px 0;
color: #6f8b93;
font-size: 9px;
line-height: 1.45;
}
.region-panel {
right: 24px;
bottom: 24px;
width: 290px;
padding: 16px;
}
.region-heading {
display: flex;
align-items: center;
justify-content: space-between;
gap: 10px;
margin-bottom: 9px;
}
.region-heading label {
color: var(--muted);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.13em;
text-transform: uppercase;
}
.selected-dot {
width: 9px;
height: 9px;
border-radius: 50%;
background: var(--selected);
box-shadow: 0 0 0 4px rgba(255, 180, 84, 0.12);
}
select {
width: 100%;
padding: 10px 36px 10px 11px;
border: 1px solid rgba(255, 255, 255, 0.11);
border-radius: 10px;
background: #102a37;
color: var(--text);
font-size: 13px;
cursor: pointer;
}
.region-facts {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 8px;
margin-top: 10px;
}
.region-fact {
padding: 9px 10px;
border-radius: 10px;
background: rgba(255, 255, 255, 0.035);
}
.region-fact span {
display: block;
color: var(--muted);
font-size: 8px;
font-weight: 800;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.region-fact strong {
display: block;
margin-top: 4px;
font-size: 11px;
}
.legend {
display: flex;
flex-wrap: wrap;
gap: 12px;
margin-top: 12px;
color: var(--muted);
font-size: 9px;
}
.legend span {
display: flex;
align-items: center;
gap: 5px;
}
.swatch {
width: 9px;
height: 9px;
border-radius: 2px;
}
.zoom-controls {
position: fixed;
z-index: 10;
right: 24px;
top: 112px;
display: flex;
flex-direction: column;
gap: 5px;
padding: 5px;
border: 1px solid var(--panel-border);
border-radius: 12px;
background: rgba(6, 20, 29, 0.78);
box-shadow: var(--shadow);
backdrop-filter: blur(12px);
}
.zoom-controls button {
width: 34px;
height: 34px;
padding: 0;
border: 0;
border-radius: 8px;
background: transparent;
color: var(--muted);
cursor: pointer;
font-size: 18px;
transition: background 150ms ease, color 150ms ease;
}
.zoom-controls button:hover {
background: rgba(255, 255, 255, 0.06);
color: var(--text);
}
.zoom-controls .reset {
border-top: 1px solid rgba(255, 255, 255, 0.07);
border-radius: 0 0 8px 8px;
font-size: 12px;
}
#regionPin {
position: fixed;
z-index: 8;
display: flex;
align-items: center;
gap: 8px;
min-width: 126px;
padding: 8px 10px;
border: 1px solid rgba(255, 180, 84, 0.45);
border-radius: 10px;
background: rgba(12, 28, 35, 0.9);
box-shadow: 0 10px 30px rgba(0, 0, 0, 0.35);
pointer-events: none;
transform: translate(-50%, calc(-100% - 13px));
transition: opacity 180ms ease;
}
#regionPin::after {
content: "";
position: absolute;
left: 50%;
bottom: -7px;
width: 11px;
height: 11px;
border-right: 1px solid rgba(255, 180, 84, 0.45);
border-bottom: 1px solid rgba(255, 180, 84, 0.45);
background: rgba(12, 28, 35, 0.9);
transform: translateX(-50%) rotate(45deg);
}
#regionPin .pin-dot {
width: 8px;
height: 8px;
flex: 0 0 auto;
border-radius: 50%;
background: var(--selected);
box-shadow: 0 0 0 4px rgba(255, 180, 84, 0.12);
}
#regionPin strong {
display: block;
font-size: 11px;
}
#regionPin small {
display: block;
margin-top: 2px;
color: var(--muted);
font-size: 8px;
font-weight: 700;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.hint {
position: fixed;
z-index: 7;
left: 50%;
bottom: 23px;
transform: translateX(-50%);
color: rgba(194, 218, 220, 0.55);
font-size: 9px;
font-weight: 650;
letter-spacing: 0.06em;
text-transform: uppercase;
pointer-events: none;
}
#hoverTip {
position: fixed;
z-index: 12;
display: none;
padding: 6px 9px;
border: 1px solid rgba(255, 255, 255, 0.12);
border-radius: 7px;
background: rgba(5, 17, 24, 0.92);
color: var(--text);
font-size: 10px;
pointer-events: none;
box-shadow: 0 8px 24px rgba(0, 0, 0, 0.3);
}
#loading {
position: fixed;
z-index: 30;
inset: 0;
display: grid;
place-items: center;
background: #06131d;
color: var(--muted);
font-size: 11px;
font-weight: 800;
letter-spacing: 0.14em;
text-transform: uppercase;
transition: opacity 300ms ease, visibility 300ms ease;
}
#loading.hidden {
opacity: 0;
visibility: hidden;
}
@media (max-width: 850px) {
.topbar {
top: 15px;
left: 15px;
right: 15px;
}
h1 {
font-size: 31px;
}
.subtitle {
display: none;
}
.projection-switch button {
min-width: 122px;
padding: 9px 11px;
}
.property-panel {
left: 15px;
bottom: 15px;
width: calc(100vw - 30px);
max-width: 370px;
}
.region-panel {
top: 102px;
right: 15px;
bottom: auto;
width: 250px;
}
.zoom-controls {
top: 102px;
right: 15px;
}
.region-panel + .zoom-controls {
top: 250px;
}
.hint {
display: none;
}
}
@media (max-width: 580px) {
.topbar {
display: block;
}
.brand {
max-width: none;
}
.projection-switch {
position: fixed;
top: 78px;
left: 15px;
right: 15px;
}
.projection-switch button {
flex: 1;
min-width: 0;
}
.region-panel {
top: 146px;
right: 15px;
width: 220px;
padding: 12px;
}
.region-facts,
.legend {
display: none;
}
.property-panel {
padding: 15px;
}
.description {
min-height: 0;
}
.metric-grid {
display: none;
}
.zoom-controls {
top: 146px;
right: 15px;
transform: translateX(0);
}
.region-panel ~ .zoom-controls {
top: 146px;
}
}
</style>
</head>
<body data-projection="equal-area">
<div id="stage" aria-label="Interactive low-poly world map"></div>
<header class="topbar">
<div class="brand">
<div class="eyebrow">Atlas / Projection Lab</div>
<h1>Map truth, measured.</h1>
<p class="subtitle">
Switch projections without losing the region you selected.
</p>
</div>
<nav class="projection-switch" role="group" aria-label="Map projection">
<button
type="button"
data-projection="equal-area"
aria-pressed="true"
title="Switch to the equal-area projection (1)"
>
<strong>Equal-area</strong>
<small>Area-preserving</small>
</button>
<button
type="button"
data-projection="mercator"
aria-pressed="false"
title="Switch to the Mercator projection (2)"
>
<strong>Mercator</strong>
<small>Angle-preserving</small>
</button>
</nav>
</header>
<section class="panel property-panel" aria-live="polite">
<div class="panel-kicker" id="projectionKicker">Projection 01 / Equal-area</div>
<h2 id="projectionTitle">Area stays true.</h2>
<p class="description" id="projectionDescription">
Distances stretch, but a small patch on the map has the same area as
the corresponding patch on Earth.
</p>
<div class="measure">
<div class="measure-value" id="measureValue">1.000×</div>
<div>
<div class="measure-label" id="measureLabel">Map area ÷ Earth area</div>
<div class="measure-secondary" id="measureSecondary">
Area preserved at the selected sample point.
</div>
</div>
</div>
<div class="metric-grid">
<div class="metric">
<span>Local area ratio</span>
<strong id="areaMetric">1.000×</strong>
</div>
<div class="metric">
<span>Bearing error</span>
<strong id="angleMetric">0.0°</strong>
</div>
</div>
<p class="note">
Measurements use local projection differentials at the selected marker.
Click a highlighted country on the map to change regions.
</p>
</section>
<section class="panel region-panel">
<div class="region-heading">
<label for="regionSelect">Highlighted region</label>
<span class="selected-dot" aria-hidden="true"></span>
</div>
<select id="regionSelect">
<option value="france">France</option>
<option value="brazil">Brazil</option>
<option value="india">India</option>
<option value="japan">Japan</option>
<option value="australia">Australia</option>
<option value="south-africa">South Africa</option>
</select>
<div class="region-facts">
<div class="region-fact">
<span>Region area</span>
<strong id="regionArea">551,700 km²</strong>
</div>
<div class="region-fact">
<span>Sample point</span>
<strong id="regionCoordinates">46.6°N, 2.2°E</strong>
</div>
</div>
<div class="legend" aria-label="Map legend">
<span><i class="swatch" style="background:#348d82"></i>Land</span>
<span><i class="swatch" style="background:#ffb454"></i>Selected</span>
<span><i class="swatch" style="background:#6ba2ad"></i>Graticule</span>
</div>
</section>
<div class="zoom-controls" aria-label="Map controls">
<button type="button" id="zoomIn" aria-label="Zoom in">+</button>
<button type="button" id="zoomOut" aria-label="Zoom out">−</button>
<button type="button" id="resetView" class="reset" aria-label="Reset view">⌂</button>
</div>
<div id="regionPin" aria-hidden="true">
<span class="pin-dot"></span>
<div>
<strong id="pinName">France</strong>
<small>Selected region</small>
</div>
</div>
<div class="hint">Drag to pan · Scroll to zoom · 1 / 2 to switch</div>
<div id="hoverTip"></div>
<div id="loading">Building low-poly atlas…</div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// ---------------------------------------------------------------------
// Simplified geographic data. Coordinates are [longitude, latitude].
// ---------------------------------------------------------------------
const WORLD = [
{
name: "North America",
points: [
[-168, 66], [-155, 71], [-140, 70], [-125, 70], [-110, 68],
[-95, 70], [-82, 67], [-66, 60], [-56, 52], [-66, 45],
[-76, 42], [-80, 35], [-84, 30], [-89, 29], [-97, 26],
[-106, 23], [-114, 29], [-121, 35], [-124, 43], [-125, 50],
[-133, 56], [-145, 59], [-158, 57], [-165, 60]
]
},
{
name: "South America",
points: [
[-81, 8], [-72, 12], [-64, 10], [-52, 5], [-48, 0],
[-35, -5], [-39, -14], [-48, -24], [-53, -34], [-59, -39],
[-67, -55], [-74, -50], [-73, -35], [-71, -20], [-78, -5]
]
},
{
name: "Europe",
points: [
[-10, 44], [-9, 48], [-4, 49], [0, 49], [4, 52], [8, 54],
[12, 55], [18, 55], [22, 60], [28, 61], [31, 66], [40, 68],
[60, 70], [68, 66], [60, 55], [50, 48], [40, 45], [30, 45],
[28, 41], [24, 40], [20, 40], [16, 38], [13, 45], [8, 44],
[3, 43], [-2, 43], [-2, 41], [-5, 36], [-10, 37]
]
},
{
name: "Africa",
points: [
[-17, 15], [-16, 22], [-10, 29], [-5, 35], [9, 37], [20, 33],
[32, 31], [35, 24], [43, 12], [51, 11], [45, 2], [41, -11],
[35, -20], [28, -33], [18, -35], [12, -24], [9, -5], [2, 5],
[-8, 4], [-13, 9]
]
},
{
name: "Asia",
points: [
[29, 41], [35, 45], [42, 46], [50, 48], [59, 53], [68, 60],
[78, 68], [95, 72], [113, 75], [135, 72], [160, 70], [179, 66],
[172, 60], [158, 55], [146, 49], [140, 42], [132, 36],
[122, 31], [122, 23], [112, 20], [108, 10], [101, 2], [96, 8],
[91, 20], [81, 8], [75, 17], [67, 23], [58, 25], [49, 30],
[40, 36], [32, 37]
]
},
{
name: "Australia",
points: [
[113, -22], [114, -27], [118, -35], [126, -32], [132, -32],
[138, -35], [145, -38], [150, -37], [153, -28], [146, -18],
[142, -11], [135, -12], [129, -15], [122, -18]
]
},
{
name: "Greenland",
points: [
[-73, 59], [-55, 59], [-43, 65], [-22, 70], [-20, 79],
[-42, 83], [-63, 80], [-69, 72]
]
},
{
name: "Antarctica",
points: [
[-180, -70], [-150, -78], [-100, -73], [-60, -64], [0, -70],
[60, -68], [100, -66], [140, -70], [180, -72], [180, -90],
[-180, -90]
]
},
{
name: "British Isles",
points: [
[-5, 50], [-6, 55], [-3, 58], [0, 54], [1, 52]
]
},
{
name: "Iceland",
points: [
[-24, 64], [-18, 66], [-14, 65], [-18, 63], [-22, 63]
]
},
{
name: "Japan",
points: [
[130, 32], [132, 34], [136, 35], [140, 38], [142, 42],
[145, 44], [141, 45], [138, 37], [135, 34], [132, 31]
]
},
{
name: "Madagascar",
points: [
[49, -12], [51, -16], [49, -25], [45, -25], [43, -19]
]
},
{
name: "Indonesia",
points: [
[95, 5], [104, 2], [106, -6], [116, -8], [126, -8],
[134, -5], [130, 0], [120, 3], [110, 2], [100, 7]
]
},
{
name: "New Zealand",
points: [
[166, -46], [172, -42], [175, -37], [178, -39],
[174, -45], [168, -47]
]
}
];
const REGIONS = {
france: {
name: "France",
area: "551,700 km²",
center: [2.2, 46.6],
points: [
[-4.8, 48.4], [-1.5, 49.7], [1.5, 50.9], [4.2, 49.8],
[7.6, 48.6], [7.4, 44.2], [3.0, 43.0], [0.0, 42.6],
[-1.8, 43.3], [-4.8, 46.0]
]
},
brazil: {
name: "Brazil",
area: "8.50 million km²",
center: [-52.0, -10.0],
points: [
[-74, 5], [-66, 6], [-58, 5], [-50, 2], [-35, -5],
[-39, -14], [-48, -25], [-53, -33], [-58, -30],
[-57, -20], [-68, -20], [-72, -10]
]
},
india: {
name: "India",
area: "3.29 million km²",
center: [79.0, 22.0],
points: [
[68, 24], [72, 28], [80, 29], [88, 27], [92, 28],
[96, 29], [92, 22], [87, 21], [80, 8], [77, 8],
[74, 13], [70, 20]
]
},
japan: {
name: "Japan",
area: "378,000 km²",
center: [138.0, 37.0],
points: [
[130, 32], [132, 34], [136, 35], [140, 38], [142, 42],
[145, 44], [141, 45], [138, 37], [135, 34], [132, 31]
]
},
australia: {
name: "Australia",
area: "7.69 million km²",
center: [134.0, -25.0],
points: [
[113, -22], [114, -27], [118, -35], [126, -32], [132, -32],
[138, -35], [145, -38], [150, -37], [153, -28], [146, -18],
[142, -11], [135, -12], [129, -15], [122, -18]
]
},
"south-africa": {
name: "South Africa",
area: "1.22 million km²",
center: [25.0, -29.0],
points: [
[16, -29], [24, -29], [30, -31], [33, -26], [30, -22],
[25, -18], [20, -25]
]
}
};
const COLORS = {
ocean: 0x0a2635,
oceanEdge: 0x174354,
land: 0x348d82,
region: 0x43aa99,
selected: 0xffb454,
grid: 0x6ba2ad,
coast: 0x78bbb4
};
const state = {
projection: "equal-area",
region: "france"
};
// ---------------------------------------------------------------------
// Renderer, scene, orthographic camera, and direct pan/zoom controls.
// ---------------------------------------------------------------------
const stage = document.getElementById("stage");
const scene = new THREE.Scene();
const renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.setClearColor(0x06131d, 1);
stage.appendChild(renderer.domElement);
const camera = new THREE.OrthographicCamera(-1, 1, 1, -1, 0.1, 100);
camera.position.set(0, 0, 10);
scene.add(new THREE.AmbientLight(0xffffff, 1.65));
const keyLight = new THREE.DirectionalLight(0xffffff, 1.55);
keyLight.position.set(-3, 5, 8);
scene.add(keyLight);
const rimLight = new THREE.DirectionalLight(0x69c8d1, 0.65);
rimLight.position.set(5, -3, 6);
scene.add(rimLight);
const cameraGoal = { x: 0, y: 0 };
let zoomGoal = 1;
function resize() {
const width = window.innerWidth;
const height = window.innerHeight;
const aspect = width / height;
const minimumWidth = 7.35;
const minimumHeight = 5.05;
let viewWidth;
let viewHeight;
if (aspect >= minimumWidth / minimumHeight) {
viewHeight = minimumHeight;
viewWidth = viewHeight * aspect;
} else {
viewWidth = minimumWidth;
viewHeight = viewWidth / aspect;
}
camera.left = -viewWidth / 2;
camera.right = viewWidth / 2;
camera.top = viewHeight / 2;
camera.bottom = -viewHeight / 2;
camera.updateProjectionMatrix();
renderer.setSize(width, height);
}
window.addEventListener("resize", resize);
resize();
// ---------------------------------------------------------------------
// Projection math.
//
// Equal-area cylindrical with a 45° standard parallel:
// x = lambda * cos(45°)
// y = sin(phi) / cos(45°)
// Its area Jacobian is exactly cos(phi), matching spherical area.
//
// Mercator:
// x = lambda
// y = ln(tan(pi/4 + phi/2))
// Its local scale is isotropic, so it preserves angles.
// ---------------------------------------------------------------------
const DEG = Math.PI / 180;
const STANDARD_PARALLEL = Math.PI / 4;
const MERCATOR_LIMIT = 82;
function projectEqualArea(lon, lat) {
return [
lon * DEG * Math.cos(STANDARD_PARALLEL),
Math.sin(lat * DEG) / Math.cos(STANDARD_PARALLEL)
];
}
function projectMercator(lon, lat) {
const limitedLat = Math.max(
-MERCATOR_LIMIT,
Math.min(MERCATOR_LIMIT, lat)
);
return [
lon * DEG,
Math.log(
Math.tan(
Math.PI / 4 + (limitedLat * DEG) / 2
)
)
];
}
const PROJECTIONS = {
"equal-area": {
project: projectEqualArea,
scale: 1.18,
latitudeLimit: 89,
bounds: [
-Math.PI * Math.cos(STANDARD_PARALLEL),
Math.PI * Math.cos(STANDARD_PARALLEL),
-1 / Math.cos(STANDARD_PARALLEL),
1 / Math.cos(STANDARD_PARALLEL)
]
},
mercator: {
project: projectMercator,
scale: 0.93,
latitudeLimit: MERCATOR_LIMIT,
bounds: [
-Math.PI,
Math.PI,
projectMercator(0, -MERCATOR_LIMIT)[1],
projectMercator(0, MERCATOR_LIMIT)[1]
]
}
};
// ---------------------------------------------------------------------
// Geometry helpers.
// ---------------------------------------------------------------------
function registerMaterial(group, material, baseOpacity = 1) {
material.transparent = true;
material.opacity = baseOpacity;
material.depthWrite = false;
material.userData.baseOpacity = baseOpacity;
group.userData.materials.push(material);
return material;
}
function shapeFromPoints(points) {
const shape = new THREE.Shape();
shape.moveTo(points[0][0], points[0][1]);
for (let i = 1; i < points.length; i++) {
shape.lineTo(points[i][0], points[i][1]);
}
shape.closePath();
return shape;
}
function makeFacetedGeometry(shape, baseColor) {
let geometry = new THREE.ShapeGeometry(shape);
if (geometry.index) {
const indexed = geometry;
geometry = indexed.toNonIndexed();
indexed.dispose();
}
const positionCount = geometry.getAttribute("position").count;
const colors = new Float32Array(positionCount * 3);
const base = new THREE.Color(baseColor);
for (let triangle = 0; triangle < positionCount / 3; triangle++) {
const random = Math.sin((triangle + 1) * 12.9898) * 43758.5453;
const variation = (random - Math.floor(random)) - 0.5;
const color = base.clone();
color.offsetHSL(
variation * 0.018,
variation * 0.045,
variation * 0.095
);
for (let vertex = 0; vertex < 3; vertex++) {
const offset = (triangle * 3 + vertex) * 3;
colors[offset] = color.r;
colors[offset + 1] = color.g;
colors[offset + 2] = color.b;
}
}
geometry.setAttribute(
"color",
new THREE.BufferAttribute(colors, 3)
);
return geometry;
}
function recolorGeometry(geometry, baseColor) {
const positionCount = geometry.getAttribute("position").count;
const colors = new Float32Array(positionCount * 3);
const base = new THREE.Color(baseColor);
for (let triangle = 0; triangle < positionCount / 3; triangle++) {
const random = Math.sin((triangle + 1) * 78.233) * 43758.5453;
const variation = (random - Math.floor(random)) - 0.5;
const color = base.clone();
color.offsetHSL(
variation * 0.012,
variation * 0.035,
variation * 0.065
);
for (let vertex = 0; vertex < 3; vertex++) {
const offset = (triangle * 3 + vertex) * 3;
colors[offset] = color.r;
colors[offset + 1] = color.g;
colors[offset + 2] = color.b;
}
}
geometry.getAttribute("color").needsUpdate = true;
}
function makePolygonMesh(group, points, projection, color, z = 0) {
const projected = points.map(([lon, lat]) =>
projection.project(lon, lat)
);
const geometry = makeFacetedGeometry(
shapeFromPoints(projected),
color
);
const material = registerMaterial(
group,
new THREE.MeshStandardMaterial({
vertexColors: true,
roughness: 1,
metalness: 0,
flatShading: true,
side: THREE.DoubleSide
}),
1
);
const mesh = new THREE.Mesh(geometry, material);
mesh.position.z = z;
mesh.renderOrder = z > 0 ? 4 : 2;
group.add(mesh);
return mesh;
}
function makeOutline(group, points, projection, color, z = 0.05, opacity = 0.7) {
const projected = points.map(([lon, lat]) => {
const [x, y] = projection.project(lon, lat);
return new THREE.Vector3(x, y, z);
});
const geometry = new THREE.BufferGeometry().setFromPoints(projected);
const material = registerMaterial(
group,
new THREE.LineBasicMaterial({ color }),
opacity
);
const outline = new THREE.LineLoop(geometry, material);
outline.renderOrder = 3;
group.add(outline);
return outline;
}
function makeFrame(group, projection) {
const [x0, x1, y0, y1] = projection.bounds;
const width = x1 - x0;
const height = y1 - y0;
const frameShape = new THREE.Shape();
frameShape.moveTo(x0, y0);
frameShape.lineTo(x1, y0);
frameShape.lineTo(x1, y1);
frameShape.lineTo(x0, y1);
frameShape.closePath();
const plate = new THREE.Mesh(
new THREE.ShapeGeometry(frameShape),
registerMaterial(
group,
new THREE.MeshStandardMaterial({
color: COLORS.ocean,
roughness: 1,
metalness: 0,
side: THREE.DoubleSide
}),
0.98
)
);
plate.position.z = -0.35;
plate.renderOrder = 0;
group.add(plate);
const borderPoints = [
new THREE.Vector3(x0, y0, -0.29),
new THREE.Vector3(x1, y0, -0.29),
new THREE.Vector3(x1, y1, -0.29),
new THREE.Vector3(x0, y1, -0.29)
];
const border = new THREE.LineLoop(
new THREE.BufferGeometry().setFromPoints(borderPoints),
registerMaterial(
group,
new THREE.LineBasicMaterial({ color: COLORS.oceanEdge }),
0.9
)
);
border.renderOrder = 1;
group.add(border);
const shadow = new THREE.Mesh(
new THREE.ShapeGeometry(frameShape),
registerMaterial(
group,
new THREE.MeshBasicMaterial({
color: 0x02080c,
transparent: true,
opacity: 0.22,
side: THREE.DoubleSide
}),
0.22
)
);
shadow.position.set(0.045, -0.055, -0.42);
shadow.renderOrder = -1;
group.add(shadow);
}
function makeGraticule(group, projection) {
const vertices = [];
const limit = projection.latitudeLimit;
for (let lon = -180; lon <= 180; lon += 30) {
for (let lat = -limit; lat < limit; lat += 2) {
const a = projection.project(lon, lat);
const b = projection.project(lon, lat + 2);
vertices.push(a[0], a[1], -0.06, b[0], b[1], -0.06);
}
}
for (let lat = -80; lat <= 80; lat += 20) {
for (let lon = -180; lon < 180; lon += 4) {
const a = projection.project(lon, lat);
const b = projection.project(lon + 4, lat);
vertices.push(a[0], a[1], -0.06, b[0], b[1], -0.06);
}
}
const geometry = new THREE.BufferGeometry();
geometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(vertices, 3)
);
const lines = new THREE.LineSegments(
geometry,
registerMaterial(
group,
new THREE.LineBasicMaterial({
color: COLORS.grid,
transparent: true
}),
0.16
)
);
lines.renderOrder = 1;
group.add(lines);
}
function makeMarker(group, projection) {
const marker = new THREE.Group();
marker.position.z = 0.22;
const ring = new THREE.Mesh(
new THREE.RingGeometry(0.065, 0.095, 32),
registerMaterial(
group,
new THREE.MeshBasicMaterial({
color: COLORS.selected,
side: THREE.DoubleSide
}),
0.95
)
);
const dot = new THREE.Mesh(
new THREE.CircleGeometry(0.028, 20),
registerMaterial(
group,
new THREE.MeshBasicMaterial({
color: 0xffe0a1,
side: THREE.DoubleSide
}),
1
)
);
dot.position.z = 0.01;
marker.add(ring, dot);
marker.renderOrder = 6;
group.add(marker);
return marker;
}
function createProjectionGroup(name) {
const projection = PROJECTIONS[name];
const group = new THREE.Group();
group.userData.materials = [];
group.userData.regionEntries = [];
group.userData.marker = null;
group.userData.baseScale = projection.scale;
makeFrame(group, projection);
makeGraticule(group, projection);
for (const land of WORLD) {
makePolygonMesh(
group,
land.points,
projection,
COLORS.land,
0
);
makeOutline(
group,
land.points,
projection,
COLORS.coast,
0.035,
0.58
);
}
for (const [id, region] of Object.entries(REGIONS)) {
const mesh = makePolygonMesh(
group,
region.points,
projection,
COLORS.region,
0.08
);
mesh.userData.regionId = id;
const outline = makeOutline(
group,
region.points,
projection,
0x8bd2c8,
0.11,
0.65
);
group.userData.regionEntries.push({
id,
mesh,
outline
});
}
group.userData.marker = makeMarker(group, projection);
group.scale.setScalar(projection.scale);
scene.add(group);
return group;
}
const groups = {
"equal-area": createProjectionGroup("equal-area"),
mercator: createProjectionGroup("mercator")
};
groups["equal-area"].visible = true;
groups.mercator.visible = false;
// ---------------------------------------------------------------------
// Mathematical measurements at the selected region's sample point.
// ---------------------------------------------------------------------
function measureAtRegion(region, projectionName) {
const latitude = region.center[1];
const latitudeRadians = latitude * DEG;
const testBearing = 45 * DEG;
const sine = Math.sin(testBearing);
const cosine = Math.cos(testBearing);
const cosLatitude = Math.cos(latitudeRadians);
let projectedBearing;
let areaRatio;
if (projectionName === "mercator") {
// Mercator's local scale is sec(phi) in every direction.
areaRatio = 1 / cosLatitude;
projectedBearing = testBearing;
} else {
// The equal-area cylindrical Jacobian is exactly cos(phi).
areaRatio = 1;
const projectedX =
Math.cos(STANDARD_PARALLEL) *
sine /
cosLatitude;
const projectedY =
cosLatitude *
cosine /
Math.cos(STANDARD_PARALLEL);
projectedBearing = Math.atan2(projectedX, projectedY);
}
const angleError = Math.abs(
projectedBearing / DEG - testBearing / DEG
);
return {
areaRatio,
angleError,
projectedBearing: projectedBearing / DEG
};
}
function formatCoordinates([lon, lat]) {
const latitude = `${Math.abs(lat).toFixed(1)}°${lat >= 0 ? "N" : "S"}`;
const longitude = `${Math.abs(lon).toFixed(1)}°${lon >= 0 ? "E" : "W"}`;
return `${latitude}, ${longitude}`;
}
function updateMeasurementUI() {
const region = REGIONS[state.region];
const measurement = measureAtRegion(region, state.projection);
document.getElementById("areaMetric").textContent =
`${measurement.areaRatio.toFixed(3)}×`;
document.getElementById("angleMetric").textContent =
`${measurement.angleError.toFixed(1)}°`;
if (state.projection === "equal-area") {
document.getElementById("projectionKicker").textContent =
"Projection 01 / Equal-area cylindrical";
document.getElementById("projectionTitle").textContent =
"Area stays true.";
document.getElementById("projectionDescription").textContent =
"Distances stretch, but a small patch on the map has the same area as the corresponding patch on Earth.";
document.getElementById("measureValue").textContent =
`${measurement.areaRatio.toFixed(3)}×`;
document.getElementById("measureLabel").textContent =
"Map area ÷ Earth area";
document.getElementById("measureSecondary").textContent =
`Area preserved; local bearing shifts by ${measurement.angleError.toFixed(1)}°.`;
} else {
document.getElementById("projectionKicker").textContent =
"Projection 02 / Mercator";
document.getElementById("projectionTitle").textContent =
"Angles stay true.";
document.getElementById("projectionDescription").textContent =
"Local shapes and angles stay true, but the map's area grows rapidly toward the poles.";
document.getElementById("measureValue").textContent =
`${measurement.angleError.toFixed(1)}°`;
document.getElementById("measureLabel").textContent =
"Bearing error at sample point";
document.getElementById("measureSecondary").textContent =
`Angles preserved; local area becomes ${measurement.areaRatio.toFixed(2)}× larger.`;
}
document.getElementById("regionArea").textContent = region.area;
document.getElementById("regionCoordinates").textContent =
formatCoordinates(region.center);
}
function updateSelectionVisuals() {
for (const group of Object.values(groups)) {
for (const entry of group.userData.regionEntries) {
const selected = entry.id === state.region;
recolorGeometry(
entry.mesh.geometry,
selected ? COLORS.selected : COLORS.region
);
entry.outline.material.color.set(
selected ? 0xffd38a : 0x8bd2c8
);
entry.outline.material.userData.baseOpacity =
selected ? 1 : 0.65;
}
const region = REGIONS[state.region];
const point = group.userData.projectionName === "mercator"
? projectMercator(...region.center)
: projectEqualArea(...region.center);
group.userData.marker.position.x = point[0];
group.userData.marker.position.y = point[1];
}
}
// Attach projection names after creation without rebuilding geometry.
groups["equal-area"].userData.projectionName = "equal-area";
groups.mercator.userData.projectionName = "mercator";
// ---------------------------------------------------------------------
// Projection switching and crossfade.
// ---------------------------------------------------------------------
let transition = null;
const reducedMotion = window.matchMedia(
"(prefers-reduced-motion: reduce)"
).matches;
function setGroupOpacity(group, multiplier) {
for (const material of group.userData.materials) {
material.opacity =
material.userData.baseOpacity * multiplier;
}
}
function switchProjection(nextProjection) {
if (nextProjection === state.projection) return;
const previousProjection = state.projection;
const from = groups[previousProjection];
const to = groups[nextProjection];
state.projection = nextProjection;
document.body.dataset.projection = nextProjection;
document.querySelectorAll(
".projection-switch button"
).forEach((button) => {
button.setAttribute(
"aria-pressed",
String(button.dataset.projection === nextProjection)
);
});
updateMeasurementUI();
from.visible = true;
to.visible = true;
if (reducedMotion) {
setGroupOpacity(from, 0);
setGroupOpacity(to, 1);
from.visible = false;
to.position.y = 0;
to.scale.setScalar(to.userData.baseScale);
transition = null;
return;
}
transition = {
from,
to,
start: performance.now(),
duration: 650
};
to.scale.setScalar(to.userData.baseScale * 0.985);
to.position.y = 0.075;
}
document.querySelectorAll(
".projection-switch button"
).forEach((button) => {
button.addEventListener("click", () => {
switchProjection(button.dataset.projection);
});
});
// ---------------------------------------------------------------------
// Region selection through the dropdown and map picking.
// ---------------------------------------------------------------------
const regionSelect = document.getElementById("regionSelect");
const pin = document.getElementById("regionPin");
const pinName = document.getElementById("pinName");
function selectRegion(id) {
if (!REGIONS[id]) return;
state.region = id;
regionSelect.value = id;
pinName.textContent = REGIONS[id].name;
updateSelectionVisuals();
updateMeasurementUI();
}
regionSelect.addEventListener("change", () => {
selectRegion(regionSelect.value);
});
const raycaster = new THREE.Raycaster();
const pointer = new THREE.Vector2();
const hoverTip = document.getElementById("hoverTip");
function pickRegion(clientX, clientY) {
const rect = renderer.domElement.getBoundingClientRect();
pointer.x =
((clientX - rect.left) / rect.width) * 2 - 1;
pointer.y =
-((clientY - rect.top) / rect.height) * 2 + 1;
const activeGroup = groups[state.projection];
activeGroup.updateMatrixWorld(true);
const regionMeshes = activeGroup.userData.regionEntries.map(
(entry) => entry.mesh
);
const intersections = raycaster.intersectObjects(
regionMeshes,
false
);
return intersections.length
? intersections[0].object.userData.regionId
: null;
}
// ---------------------------------------------------------------------
// Direct pan, zoom, reset, hover, and click behavior.
// ---------------------------------------------------------------------
const drag = {
active: false,
pointerId: null,
lastX: 0,
lastY: 0,
moved: false
};
function clamp(value, min, max) {
return Math.max(min, Math.min(max, value));
}
renderer.domElement.addEventListener("pointerdown", (event) => {
drag.active = true;
drag.pointerId = event.pointerId;
drag.lastX = event.clientX;
drag.lastY = event.clientY;
drag.moved = false;
renderer.domElement.setPointerCapture(event.pointerId);
renderer.domElement.style.cursor = "grabbing";
});
renderer.domElement.addEventListener("pointermove", (event) => {
if (drag.active) {
const dx = event.clientX - drag.lastX;
const dy = event.clientY - drag.lastY;
if (Math.abs(dx) + Math.abs(dy) > 2) {
drag.moved = true;
}
const worldWidth = camera.right - camera.left;
const worldHeight = camera.top - camera.bottom;
cameraGoal.x = clamp(
cameraGoal.x - (dx * worldWidth) / window.innerWidth / camera.zoom,
-5,
5
);
cameraGoal.y = clamp(
cameraGoal.y + (dy * worldHeight) / window.innerHeight / camera.zoom,
-4,
4
);
drag.lastX = event.clientX;
drag.lastY = event.clientY;
hoverTip.style.display = "none";
return;
}
const regionId = pickRegion(event.clientX, event.clientY);
if (regionId) {
renderer.domElement.style.cursor = "pointer";
hoverTip.textContent = `${REGIONS[regionId].name} — select`;
hoverTip.style.left = `${event.clientX + 13}px`;
hoverTip.style.top = `${event.clientY + 13}px`;
hoverTip.style.display = "block";
} else {
renderer.domElement.style.cursor = "grab";
hoverTip.style.display = "none";
}
});
function endDrag(event) {
if (!drag.active) return;
if (!drag.moved) {
const regionId = pickRegion(event.clientX, event.clientY);
if (regionId) selectRegion(regionId);
}
drag.active = false;
if (renderer.domElement.hasPointerCapture(event.pointerId)) {
renderer.domElement.releasePointerCapture(event.pointerId);
}
renderer.domElement.style.cursor = "grab";
}
renderer.domElement.addEventListener("pointerup", endDrag);
renderer.domElement.addEventListener("pointercancel", endDrag);
renderer.domElement.addEventListener(
"wheel",
(event) => {
event.preventDefault();
zoomGoal = clamp(
zoomGoal * Math.exp(-event.deltaY * 0.001),
0.75,
4
);
},
{ passive: false }
);
function resetView() {
cameraGoal.x = 0;
cameraGoal.y = 0;
zoomGoal = 1;
}
renderer.domElement.addEventListener("dblclick", resetView);
document.getElementById("zoomIn").addEventListener("click", () => {
zoomGoal = clamp(zoomGoal * 1.18, 0.75, 4);
});
document.getElementById("zoomOut").addEventListener("click", () => {
zoomGoal = clamp(zoomGoal / 1.18, 0.75, 4);
});
document.getElementById("resetView").addEventListener("click", resetView);
window.addEventListener("keydown", (event) => {
const tag = document.activeElement?.tagName;
if (tag === "SELECT" || tag === "INPUT") return;
if (event.key === "1") {
switchProjection("equal-area");
} else if (event.key === "2") {
switchProjection("mercator");
} else if (event.key.toLowerCase() === "r") {
resetView();
}
});
// ---------------------------------------------------------------------
// Render loop.
// ---------------------------------------------------------------------
const pinVector = new THREE.Vector3();
function updatePin() {
const group = groups[state.projection];
const region = REGIONS[state.region];
const projected = PROJECTIONS[state.projection].project(
...region.center
);
pinVector.set(projected[0], projected[1], 0.25);
group.updateWorldMatrix(true, false);
group.localToWorld(pinVector);
pinVector.project(camera);
const x = (pinVector.x * 0.5 + 0.5) * window.innerWidth;
const y = (-pinVector.y * 0.5 + 0.5) * window.innerHeight;
pin.style.left = `${x}px`;
pin.style.top = `${y}px`;
pin.style.opacity = pinVector.z > -1 && pinVector.z < 1 ? "1" : "0";
}
function animate(now) {
requestAnimationFrame(animate);
const cameraEase = reducedMotion ? 1 : 0.13;
camera.position.x +=
(cameraGoal.x - camera.position.x) * cameraEase;
camera.position.y +=
(cameraGoal.y - camera.position.y) * cameraEase;
camera.zoom +=
(zoomGoal - camera.zoom) * cameraEase;
camera.updateProjectionMatrix();
camera.lookAt(camera.position.x, camera.position.y, 0);
if (transition) {
const raw = clamp(
(now - transition.start) / transition.duration,
0,
1
);
const eased =
raw < 0.5
? 4 * raw * raw * raw
: 1 - Math.pow(-2 * raw + 2, 3) / 2;
setGroupOpacity(transition.from, 1 - eased);
setGroupOpacity(transition.to, eased);
transition.from.position.y = -0.075 * eased;
transition.to.position.y = 0.075 * (1 - eased);
transition.from.scale.setScalar(
transition.from.userData.baseScale *
(1 - 0.015 * eased)
);
transition.to.scale.setScalar(
transition.to.userData.baseScale *
(0.985 + 0.015 * eased)
);
if (raw >= 1) {
transition.from.visible = false;
transition.from.position.y = 0;
transition.from.scale.setScalar(
transition.from.userData.baseScale
);
transition.to.position.y = 0;
transition.to.scale.setScalar(
transition.to.userData.baseScale
);
transition = null;
}
}
const pulse = 1 + Math.sin(now * 0.003) * 0.07;
for (const group of Object.values(groups)) {
group.userData.marker.scale.setScalar(pulse);
}
updatePin();
renderer.render(scene, camera);
}
updateSelectionVisuals();
updateMeasurementUI();
requestAnimationFrame(animate);
requestAnimationFrame(() => {
document.getElementById("loading").classList.add("hidden");
});
</script>
</body>
</html>
```