Prompt specification
Build or customize the Three.js 3D scene "Globe patch and flattened tile" 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: Globe patch and flattened tile
Category: Space & Earth · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/globe-patch-and-flattened-tile-x1317
Scene Description Prompt:
Create a single self-contained HTML file using Three.js from cdnjs that shows map projections. Display one gridded globe patch beside a flattened rectangular tile. Connect corresponding corners and make the mismatch in shape visible without presenting a universal correction. Stylized solid forms.
Working Three.js Starter (globe-patch-and-flattened-tile-x1317):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Projection, Unfolded</title>
<style>
:root {
--bg: #0b100f;
--panel: rgba(18, 25, 23, 0.78);
--line: rgba(226, 237, 229, 0.16);
--text: #edf2e9;
--muted: #9eaaa2;
--teal: #4ca99b;
--amber: #df9a55;
--cream: #f0d3a4;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background:
radial-gradient(circle at 50% 43%, #1b2925 0%, #101715 38%, var(--bg) 78%);
color: var(--text);
font-family: Inter, ui-sans-serif, system-ui, -apple-system,
BlinkMacSystemFont, "Segoe UI", sans-serif;
}
body::before {
content: "";
position: fixed;
inset: 0;
pointer-events: none;
opacity: 0.32;
background-image:
linear-gradient(rgba(210, 230, 218, 0.035) 1px, transparent 1px),
linear-gradient(90deg, rgba(210, 230, 218, 0.035) 1px, transparent 1px);
background-size: 52px 52px;
mask-image: radial-gradient(circle at center, black, transparent 78%);
}
#stage {
position: fixed;
inset: 0;
}
canvas {
display: block;
width: 100%;
height: 100%;
outline: none;
cursor: grab;
touch-action: none;
}
canvas:active {
cursor: grabbing;
}
.interface {
position: fixed;
inset: 0;
z-index: 2;
pointer-events: none;
}
header {
position: absolute;
top: clamp(22px, 4vw, 54px);
left: clamp(22px, 4vw, 58px);
width: min(390px, calc(100vw - 44px));
}
.kicker {
display: flex;
align-items: center;
gap: 10px;
margin-bottom: 22px;
color: var(--muted);
font-size: 10px;
font-weight: 700;
letter-spacing: 0.2em;
text-transform: uppercase;
}
.kicker::before {
content: "";
width: 9px;
height: 9px;
border: 2px solid var(--teal);
border-radius: 50%;
box-shadow: inset 0 0 0 2px var(--bg);
background: var(--teal);
}
h1 {
margin: 0;
font-family: Georgia, "Times New Roman", serif;
font-size: clamp(48px, 6.3vw, 88px);
font-weight: 400;
line-height: 0.88;
letter-spacing: -0.055em;
}
h1 em {
color: var(--amber);
font-weight: 400;
}
.intro {
max-width: 325px;
margin: 26px 0 0;
color: var(--muted);
font-size: 13px;
line-height: 1.65;
}
.legend {
position: absolute;
top: clamp(24px, 4vw, 54px);
right: clamp(22px, 4vw, 58px);
display: grid;
gap: 12px;
min-width: 190px;
padding: 16px 18px;
border: 1px solid var(--line);
border-radius: 3px;
background: rgba(13, 19, 17, 0.58);
backdrop-filter: blur(12px);
}
.legend-row {
display: flex;
align-items: center;
gap: 11px;
color: var(--muted);
font-size: 9px;
font-weight: 700;
letter-spacing: 0.16em;
text-transform: uppercase;
}
.swatch {
width: 22px;
height: 8px;
border-radius: 1px;
background: var(--teal);
}
.swatch.tile {
background: var(--amber);
}
.swatch.link {
height: 0;
border-top: 2px dashed var(--cream);
background: none;
}
.annotation {
position: absolute;
z-index: 3;
left: 0;
top: 0;
white-space: nowrap;
pointer-events: none;
will-change: transform;
}
.object-label {
padding: 8px 10px;
border-left: 2px solid var(--teal);
color: var(--text);
background: rgba(10, 15, 14, 0.72);
font-size: 9px;
font-weight: 800;
letter-spacing: 0.16em;
text-transform: uppercase;
backdrop-filter: blur(8px);
}
.object-label small {
display: block;
margin-top: 5px;
color: var(--muted);
font-size: 8px;
font-weight: 600;
letter-spacing: 0.09em;
}
#tile-label {
border-color: var(--amber);
}
.corner-label {
display: grid;
place-items: center;
width: 22px;
height: 22px;
border: 1px solid rgba(240, 211, 164, 0.55);
border-radius: 50%;
color: var(--cream);
background: rgba(10, 15, 14, 0.78);
font-family: Georgia, serif;
font-size: 11px;
font-style: italic;
box-shadow: 0 0 18px rgba(0, 0, 0, 0.25);
}
.note {
position: absolute;
right: clamp(22px, 4vw, 58px);
bottom: clamp(72px, 9vh, 112px);
width: min(330px, calc(100vw - 44px));
padding: 20px 22px;
border: 1px solid var(--line);
border-radius: 3px;
background: var(--panel);
backdrop-filter: blur(16px);
}
.note-label {
margin-bottom: 12px;
color: var(--amber);
font-size: 9px;
font-weight: 800;
letter-spacing: 0.18em;
text-transform: uppercase;
}
.note h2 {
margin: 0 0 11px;
font-family: Georgia, "Times New Roman", serif;
font-size: 25px;
font-weight: 400;
line-height: 1.1;
}
.note p {
margin: 0;
color: var(--muted);
font-size: 12px;
line-height: 1.65;
}
.controls {
position: absolute;
left: clamp(22px, 4vw, 58px);
bottom: clamp(24px, 4vw, 48px);
display: flex;
align-items: center;
gap: 14px;
pointer-events: auto;
}
.controls span {
color: var(--muted);
font-size: 9px;
font-weight: 700;
letter-spacing: 0.12em;
text-transform: uppercase;
}
button {
padding: 10px 13px;
border: 1px solid var(--line);
border-radius: 2px;
color: var(--text);
background: rgba(18, 25, 23, 0.72);
font: inherit;
font-size: 9px;
font-weight: 800;
letter-spacing: 0.13em;
text-transform: uppercase;
cursor: pointer;
transition:
border-color 160ms ease,
background 160ms ease,
color 160ms ease;
}
button:hover,
button:focus-visible {
border-color: var(--amber);
color: var(--amber);
background: rgba(33, 31, 24, 0.9);
outline: none;
}
.domain {
position: absolute;
left: 50%;
bottom: clamp(24px, 4vw, 48px);
transform: translateX(-50%);
color: rgba(220, 229, 221, 0.48);
font-size: 8px;
font-weight: 700;
letter-spacing: 0.17em;
text-transform: uppercase;
white-space: nowrap;
}
@media (max-width: 820px) {
header {
top: 22px;
}
h1 {
font-size: clamp(42px, 12vw, 66px);
}
.intro {
display: none;
}
.legend {
top: auto;
right: 18px;
bottom: 68px;
min-width: 150px;
padding: 12px;
}
.note {
display: none;
}
.controls {
left: 18px;
bottom: 18px;
}
.domain {
display: none;
}
}
@media (max-width: 520px) {
.kicker {
margin-bottom: 15px;
}
.legend {
display: none;
}
.object-label {
padding: 6px 7px;
font-size: 7px;
}
.object-label small {
font-size: 6px;
}
}
</style>
</head>
<body>
<main id="stage" aria-label="Interactive comparison of a spherical map patch and a flattened map tile"></main>
<div class="interface">
<header>
<div class="kicker">Cartographic form / study 01</div>
<h1>Projection,<br><em>unfolded.</em></h1>
<p class="intro">
One gridded globe patch becomes a rectangular tile.
The dashed paths connect corresponding corners—not equal distances.
</p>
</header>
<div class="legend" aria-label="Scene legend">
<div class="legend-row"><span class="swatch"></span>Spherical source</div>
<div class="legend-row"><span class="swatch tile"></span>Flattened tile</div>
<div class="legend-row"><span class="swatch link"></span>Corner correspondence</div>
</div>
<div id="source-label" class="annotation object-label">
Spherical source
<small>Curved graticule / A–D</small>
</div>
<div id="tile-label" class="annotation object-label">
Flattened tile
<small>Equirectangular sample</small>
</div>
<div id="corner-a" class="annotation corner-label">A</div>
<div id="corner-b" class="annotation corner-label">B</div>
<div id="corner-c" class="annotation corner-label">C</div>
<div id="corner-d" class="annotation corner-label">D</div>
<aside class="note">
<div class="note-label">The distortion between</div>
<h2>Rectangular does not mean faithful.</h2>
<p>
The tile and the globe patch share the same coordinates.
Their corner links reveal where area, angle, and distance cannot
all be kept at once. This is a translation, not a universal correction.
</p>
</aside>
<div class="controls">
<span>Drag to orbit · Scroll to zoom</span>
<button id="reset" type="button">Reset view</button>
</div>
<div class="domain">30° W — 30° E / 12° N — 56° N</div>
</div>
<script type="module">
import * as THREE from "https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
const stage = document.getElementById("stage");
const renderer = new THREE.WebGLRenderer({
antialias: true,
alpha: true,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.08;
renderer.setClearColor(0x000000, 0);
renderer.domElement.setAttribute(
"aria-label",
"A curved gridded globe patch connected by dashed lines to a flat rectangular map tile"
);
stage.appendChild(renderer.domElement);
const scene = new THREE.Scene();
const camera = new THREE.PerspectiveCamera(
42,
window.innerWidth / window.innerHeight,
0.1,
100
);
const cameraTarget = new THREE.Vector3(0, 0.28, 0.1);
const hemisphere = new THREE.HemisphereLight(0xc6e3d9, 0x302117, 2.1);
scene.add(hemisphere);
const keyLight = new THREE.DirectionalLight(0xffefd3, 3.2);
keyLight.position.set(4.5, 7, 7);
scene.add(keyLight);
const rimLight = new THREE.DirectionalLight(0x62b9ad, 2.1);
rimLight.position.set(-5, 1.5, 3);
scene.add(rimLight);
const lowerFill = new THREE.DirectionalLight(0xd87d3e, 0.8);
lowerFill.position.set(2, -4, 3);
scene.add(lowerFill);
const radius = 2;
const lonMin = THREE.MathUtils.degToRad(-30);
const lonMax = THREE.MathUtils.degToRad(30);
const latMin = THREE.MathUtils.degToRad(12);
const latMax = THREE.MathUtils.degToRad(56);
const lonMid = (lonMin + lonMax) * 0.5;
const latMid = (latMin + latMax) * 0.5;
const patchThickness = 0.16;
const tileWidth = radius * (lonMax - lonMin);
const tileHeight = radius * (latMax - latMin);
const globeGroup = new THREE.Group();
globeGroup.position.set(-2.75, -0.55, -0.15);
globeGroup.rotation.y = 0.035;
scene.add(globeGroup);
const tileGroup = new THREE.Group();
tileGroup.position.set(2.25, 0.35, 0.1);
tileGroup.rotation.y = -0.08;
tileGroup.rotation.x = 0.012;
scene.add(tileGroup);
function sphericalPoint(lon, lat, r = radius) {
return new THREE.Vector3(
r * Math.cos(lat) * Math.sin(lon),
r * Math.sin(lat),
r * Math.cos(lat) * Math.cos(lon)
);
}
function flatPoint(lon, lat) {
return new THREE.Vector3(
radius * (lon - lonMid),
radius * (lat - latMid),
0.093
);
}
function createPatchGeometry(r, columns = 24, rows = 16) {
const positions = [];
const uvs = [];
const indices = [];
for (let row = 0; row <= rows; row++) {
const v = row / rows;
const lat = THREE.MathUtils.lerp(latMax, latMin, v);
for (let column = 0; column <= columns; column++) {
const u = column / columns;
const lon = THREE.MathUtils.lerp(lonMin, lonMax, u);
const point = sphericalPoint(lon, lat, r);
positions.push(point.x, point.y, point.z);
uvs.push(u, 1 - v);
}
}
for (let row = 0; row < rows; row++) {
for (let column = 0; column < columns; column++) {
const a = row * (columns + 1) + column;
const b = a + 1;
const c = a + columns + 1;
const d = c + 1;
indices.push(a, c, b, b, c, d);
}
}
const geometry = new THREE.BufferGeometry();
geometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(positions, 3)
);
geometry.setAttribute(
"uv",
new THREE.Float32BufferAttribute(uvs, 2)
);
geometry.setIndex(indices);
geometry.computeVertexNormals();
return geometry;
}
const patchFrontMaterial = new THREE.MeshStandardMaterial({
color: 0x267b70,
roughness: 0.64,
metalness: 0.04,
side: THREE.DoubleSide
});
const patchBackMaterial = new THREE.MeshStandardMaterial({
color: 0x123d39,
roughness: 0.9,
metalness: 0,
side: THREE.DoubleSide
});
const patchEdgeMaterial = new THREE.MeshStandardMaterial({
color: 0x143c38,
roughness: 0.86,
metalness: 0.02,
side: THREE.DoubleSide
});
const patchFront = new THREE.Mesh(
createPatchGeometry(radius),
patchFrontMaterial
);
globeGroup.add(patchFront);
const patchBack = new THREE.Mesh(
createPatchGeometry(radius - patchThickness),
patchBackMaterial
);
globeGroup.add(patchBack);
function createPatchEdgeGeometry() {
const boundary = [];
for (let i = 0; i <= 24; i++) {
boundary.push([
THREE.MathUtils.lerp(lonMin, lonMax, i / 24),
latMax
]);
}
for (let j = 1; j <= 16; j++) {
boundary.push([
lonMax,
THREE.MathUtils.lerp(latMax, latMin, j / 16)
]);
}
for (let i = 23; i >= 0; i--) {
boundary.push([
THREE.MathUtils.lerp(lonMin, lonMax, i / 24),
latMin
]);
}
for (let j = 15; j >= 1; j--) {
boundary.push([
lonMin,
THREE.MathUtils.lerp(latMax, latMin, j / 16)
]);
}
const positions = [];
for (let i = 0; i < boundary.length; i++) {
const next = (i + 1) % boundary.length;
const [lon1, lat1] = boundary[i];
const [lon2, lat2] = boundary[next];
const f1 = sphericalPoint(lon1, lat1, radius);
const f2 = sphericalPoint(lon2, lat2, radius);
const b1 = sphericalPoint(lon1, lat1, radius - patchThickness);
const b2 = sphericalPoint(lon2, lat2, radius - patchThickness);
positions.push(
f1.x, f1.y, f1.z,
b1.x, b1.y, b1.z,
f2.x, f2.y, f2.z,
f2.x, f2.y, f2.z,
b1.x, b1.y, b1.z,
b2.x, b2.y, b2.z
);
}
const geometry = new THREE.BufferGeometry();
geometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(positions, 3)
);
geometry.computeVertexNormals();
return geometry;
}
const patchEdge = new THREE.Mesh(
createPatchEdgeGeometry(),
patchEdgeMaterial
);
globeGroup.add(patchEdge);
function createSphericalGrid() {
const positions = [];
const gridRadius = radius + 0.018;
const segments = 30;
for (let column = 0; column <= 8; column++) {
const lon = THREE.MathUtils.lerp(lonMin, lonMax, column / 8);
for (let segment = 0; segment < segments; segment++) {
const lat1 = THREE.MathUtils.lerp(
latMin,
latMax,
segment / segments
);
const lat2 = THREE.MathUtils.lerp(
latMin,
latMax,
(segment + 1) / segments
);
const p1 = sphericalPoint(lon, lat1, gridRadius);
const p2 = sphericalPoint(lon, lat2, gridRadius);
positions.push(
p1.x, p1.y, p1.z,
p2.x, p2.y, p2.z
);
}
}
for (let row = 0; row <= 10; row++) {
const lat = THREE.MathUtils.lerp(latMin, latMax, row / 10);
for (let segment = 0; segment < segments; segment++) {
const lon1 = THREE.MathUtils.lerp(
lonMin,
lonMax,
segment / segments
);
const lon2 = THREE.MathUtils.lerp(
lonMin,
lonMax,
(segment + 1) / segments
);
const p1 = sphericalPoint(lon1, lat, gridRadius);
const p2 = sphericalPoint(lon2, lat, gridRadius);
positions.push(
p1.x, p1.y, p1.z,
p2.x, p2.y, p2.z
);
}
}
const geometry = new THREE.BufferGeometry();
geometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(positions, 3)
);
const material = new THREE.LineBasicMaterial({
color: 0xc0eee1,
transparent: true,
opacity: 0.58
});
return new THREE.LineSegments(geometry, material);
}
globeGroup.add(createSphericalGrid());
const patchOutlinePoints = [
sphericalPoint(lonMin, latMax, radius + 0.028),
sphericalPoint(lonMax, latMax, radius + 0.028),
sphericalPoint(lonMax, latMin, radius + 0.028),
sphericalPoint(lonMin, latMin, radius + 0.028)
];
const patchOutline = new THREE.LineLoop(
new THREE.BufferGeometry().setFromPoints(patchOutlinePoints),
new THREE.LineBasicMaterial({
color: 0xe1fff3,
transparent: true,
opacity: 0.9
})
);
globeGroup.add(patchOutline);
const tileFrontMaterial = new THREE.MeshStandardMaterial({
color: 0xd88943,
roughness: 0.7,
metalness: 0.03
});
const tileSideMaterial = new THREE.MeshStandardMaterial({
color: 0x74401f,
roughness: 0.88
});
const tileTopMaterial = new THREE.MeshStandardMaterial({
color: 0xe0a15b,
roughness: 0.75
});
const tile = new THREE.Mesh(
new THREE.BoxGeometry(tileWidth, tileHeight, 0.18),
[
tileSideMaterial,
tileSideMaterial,
tileTopMaterial,
tileSideMaterial,
tileFrontMaterial,
tileSideMaterial
]
);
tileGroup.add(tile);
function createFlatGrid() {
const positions = [];
for (let column = 0; column <= 8; column++) {
const lon = THREE.MathUtils.lerp(lonMin, lonMax, column / 8);
const x = radius * (lon - lonMid);
positions.push(
x, -tileHeight * 0.5, 0.099,
x, tileHeight * 0.5, 0.099
);
}
for (let row = 0; row <= 10; row++) {
const lat = THREE.MathUtils.lerp(latMin, latMax, row / 10);
const y = radius * (lat - latMid);
positions.push(
-tileWidth * 0.5, y, 0.099,
tileWidth * 0.5, y, 0.099
);
}
const geometry = new THREE.BufferGeometry();
geometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(positions, 3)
);
const material = new THREE.LineBasicMaterial({
color: 0x693719,
transparent: true,
opacity: 0.66
});
return new THREE.LineSegments(geometry, material);
}
tileGroup.add(createFlatGrid());
const tileOutline = new THREE.LineLoop(
new THREE.BufferGeometry().setFromPoints([
new THREE.Vector3(-tileWidth * 0.5, tileHeight * 0.5, 0.106),
new THREE.Vector3(tileWidth * 0.5, tileHeight * 0.5, 0.106),
new THREE.Vector3(tileWidth * 0.5, -tileHeight * 0.5, 0.106),
new THREE.Vector3(-tileWidth * 0.5, -tileHeight * 0.5, 0.106)
]),
new THREE.LineBasicMaterial({
color: 0xffdca5,
transparent: true,
opacity: 0.9
})
);
tileGroup.add(tileOutline);
const corners = [
{
id: "a",
lon: lonMin,
lat: latMax
},
{
id: "b",
lon: lonMax,
lat: latMax
},
{
id: "c",
lon: lonMax,
lat: latMin
},
{
id: "d",
lon: lonMin,
lat: latMin
}
];
const sourceCornerWorld = [];
const tileCornerWorld = [];
globeGroup.updateMatrixWorld(true);
tileGroup.updateMatrixWorld(true);
const linkMaterial = new THREE.LineDashedMaterial({
color: 0xf0d3a4,
transparent: true,
opacity: 0.78,
dashSize: 0.11,
gapSize: 0.075
});
const linkGlowMaterial = new THREE.LineBasicMaterial({
color: 0xf0d3a4,
transparent: true,
opacity: 0.1
});
const sourcePointMaterial = new THREE.MeshBasicMaterial({
color: 0xf0d3a4
});
const tilePointMaterial = new THREE.MeshBasicMaterial({
color: 0xffbb70
});
const sourcePointGeometry = new THREE.SphereGeometry(0.055, 16, 12);
const tilePointGeometry = new THREE.SphereGeometry(0.046, 16, 12);
corners.forEach((corner) => {
const sourceLocal = sphericalPoint(corner.lon, corner.lat, radius + 0.035);
const tileLocal = flatPoint(corner.lon, corner.lat);
const sourceWorld = globeGroup.localToWorld(sourceLocal.clone());
const tileWorld = tileGroup.localToWorld(tileLocal.clone());
sourceCornerWorld.push(sourceWorld);
tileCornerWorld.push(tileWorld);
const linkGeometry = new THREE.BufferGeometry().setFromPoints([
sourceWorld,
tileWorld
]);
const glow = new THREE.Line(linkGeometry, linkGlowMaterial);
scene.add(glow);
const link = new THREE.Line(linkGeometry, linkMaterial);
link.computeLineDistances();
scene.add(link);
const sourcePoint = new THREE.Mesh(
sourcePointGeometry,
sourcePointMaterial
);
sourcePoint.position.copy(sourceWorld);
scene.add(sourcePoint);
const tilePoint = new THREE.Mesh(
tilePointGeometry,
tilePointMaterial
);
tilePoint.position.copy(tileWorld);
scene.add(tilePoint);
});
const sourceLabelAnchor = globeGroup.localToWorld(
sphericalPoint(0, latMax, radius).add(
new THREE.Vector3(0, 0.48, 0)
)
);
const tileLabelAnchor = tileGroup.localToWorld(
new THREE.Vector3(0, tileHeight * 0.5 + 0.43, 0.1)
);
let yaw = 0.035;
let pitch = 1.38;
let zoom = 1;
let currentYaw = yaw;
let currentPitch = pitch;
let currentZoom = zoom;
function fittedDistance() {
const aspect = window.innerWidth / window.innerHeight;
return Math.max(8.25, 9.7 / aspect);
}
function updateCamera() {
currentYaw += (yaw - currentYaw) * 0.12;
currentPitch += (pitch - currentPitch) * 0.12;
currentZoom += (zoom - currentZoom) * 0.12;
const distance = fittedDistance() * currentZoom;
const horizontal = Math.sin(currentPitch) * distance;
camera.position.set(
cameraTarget.x + Math.sin(currentYaw) * horizontal,
cameraTarget.y + Math.cos(currentPitch) * distance,
cameraTarget.z + Math.cos(currentYaw) * horizontal
);
camera.lookAt(cameraTarget);
}
function resetView() {
yaw = 0.035;
pitch = 1.38;
zoom = 1;
}
document.getElementById("reset").addEventListener("click", resetView);
let dragging = false;
let previousX = 0;
let previousY = 0;
renderer.domElement.addEventListener("pointerdown", (event) => {
dragging = true;
previousX = event.clientX;
previousY = event.clientY;
renderer.domElement.setPointerCapture(event.pointerId);
});
renderer.domElement.addEventListener("pointermove", (event) => {
if (!dragging) return;
const deltaX = event.clientX - previousX;
const deltaY = event.clientY - previousY;
yaw -= deltaX * 0.005;
pitch = THREE.MathUtils.clamp(
pitch + deltaY * 0.004,
0.78,
1.78
);
previousX = event.clientX;
previousY = event.clientY;
});
function endDrag(event) {
dragging = false;
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("dblclick", resetView);
renderer.domElement.addEventListener(
"wheel",
(event) => {
event.preventDefault();
zoom = THREE.MathUtils.clamp(
zoom * Math.exp(event.deltaY * 0.001),
0.66,
1.75
);
},
{ passive: false }
);
function resize() {
const width = window.innerWidth;
const height = window.innerHeight;
camera.aspect = width / height;
camera.updateProjectionMatrix();
renderer.setSize(width, height);
}
window.addEventListener("resize", resize);
resize();
const projectedLabels = [
{
element: document.getElementById("source-label"),
position: sourceLabelAnchor,
offsetX: 0,
offsetY: 0
},
{
element: document.getElementById("tile-label"),
position: tileLabelAnchor,
offsetX: 0,
offsetY: 0
},
{
element: document.getElementById("corner-a"),
position: sourceCornerWorld[0],
offsetX: -13,
offsetY: -12
},
{
element: document.getElementById("corner-b"),
position: sourceCornerWorld[1],
offsetX: 13,
offsetY: -12
},
{
element: document.getElementById("corner-c"),
position: sourceCornerWorld[2],
offsetX: 13,
offsetY: 12
},
{
element: document.getElementById("corner-d"),
position: sourceCornerWorld[3],
offsetX: -13,
offsetY: 12
}
];
function updateAnnotations() {
const width = window.innerWidth;
const height = window.innerHeight;
projectedLabels.forEach(({ element, position, offsetX, offsetY }) => {
const projected = position.clone().project(camera);
if (projected.z < -1 || projected.z > 1) {
element.style.display = "none";
return;
}
element.style.display = "block";
element.style.left = `${(projected.x * 0.5 + 0.5) * width}px`;
element.style.top = `${(-projected.y * 0.5 + 0.5) * height}px`;
element.style.transform =
`translate(-50%, -50%) translate(${offsetX}px, ${offsetY}px)`;
});
}
function animate() {
requestAnimationFrame(animate);
updateCamera();
camera.updateMatrixWorld();
updateAnnotations();
renderer.render(scene, camera);
}
animate();
</script>
</body>
</html>
```