mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-12-01 00:43:15 +00:00
31859651d7
Every help screen checked against the v5 redesign. Tours is documented for the first time, as a screen of its own with twelve screens under it, and notifications get help of their own. All pictures are taken again on a realistic demo trip, with short loops where a still cannot show the motion. The English is plain and every language is written naturally rather than word for word. The switcher shows two levels at most, a screen's overview lists its sub-screens as cards above its guides, each wiki page is linked once, and the help sidebar is wider. Fixes found along the way: Save to Collection keeps a tour's route, a track dropped on the places column opens the Tours import, tour Share on desktop, the unsaved tour draft lives on the account, a route click no longer adds an end point, Trip places leaves the trip's tours out, Escape closes a stage's day menu, confirms before destructive deletes, DWD warnings sharing one id kept apart, a booking's date moves with it, and smaller fixes across notifications, bookings, the day plan, stays, Atlas, Collections, Vacay and the admin screens.
118 lines
4.0 KiB
TypeScript
118 lines
4.0 KiB
TypeScript
/**
|
|
* Warms the browser cache with every tile a flyover along a route will ask for, so the
|
|
* 3D replay draws from cache instead of loading the landscape in front of the camera.
|
|
*/
|
|
|
|
export interface TileTemplate {
|
|
/** A `{z}/{x}/{y}` URL template; `{s}` subdomains are resolved before this. */
|
|
url: string;
|
|
minzoom: number;
|
|
maxzoom: number;
|
|
}
|
|
|
|
interface LatLng {
|
|
lat: number;
|
|
lng: number;
|
|
}
|
|
|
|
/** How far around the route each zoom reaches, in tiles: wide when coarse, tight when fine. */
|
|
const RING_BY_ZOOM: Record<number, number> = { 9: 1, 10: 1, 11: 2, 12: 2, 13: 1, 14: 1, 15: 1 };
|
|
const ZOOMS = [9, 10, 11, 12, 13, 14, 15];
|
|
const CONCURRENCY = 8;
|
|
|
|
function tileOf(point: LatLng, z: number): [number, number] {
|
|
const n = 2 ** z;
|
|
const x = Math.floor(((point.lng + 180) / 360) * n);
|
|
const rad = (point.lat * Math.PI) / 180;
|
|
const y = Math.floor(((1 - Math.log(Math.tan(rad) + 1 / Math.cos(rad)) / Math.PI) / 2) * n);
|
|
return [Math.min(n - 1, Math.max(0, x)), Math.min(n - 1, Math.max(0, y))];
|
|
}
|
|
|
|
/** The tiles within `ring` of a centre tile; x wraps round the antimeridian, y stops at the poles. */
|
|
function ringAround([cx, cy]: [number, number], ring: number, z: number): Array<[number, number, number]> {
|
|
const n = 2 ** z;
|
|
const tiles: Array<[number, number, number]> = [];
|
|
for (let dx = -ring; dx <= ring; dx++) {
|
|
for (let dy = -ring; dy <= ring; dy++) {
|
|
const y = cy + dy;
|
|
if (y >= 0 && y < n) tiles.push([z, (cx + dx + n) % n, y]);
|
|
}
|
|
}
|
|
return tiles;
|
|
}
|
|
|
|
/** Every tile within the ring of a point on the route, per zoom, without duplicates. */
|
|
export function corridorTiles(
|
|
points: readonly LatLng[],
|
|
zooms: readonly number[] = ZOOMS
|
|
): Array<[number, number, number]> {
|
|
const seen = new Set<string>();
|
|
const tiles: Array<[number, number, number]> = [];
|
|
for (const z of zooms) {
|
|
const ring = RING_BY_ZOOM[z] ?? 1;
|
|
for (const point of points) {
|
|
for (const tile of ringAround(tileOf(point, z), ring, z)) {
|
|
const key = tile.join('/');
|
|
if (seen.has(key)) continue;
|
|
seen.add(key);
|
|
tiles.push(tile);
|
|
}
|
|
}
|
|
}
|
|
return tiles;
|
|
}
|
|
|
|
export function tileUrls(templates: readonly TileTemplate[], tiles: ReadonlyArray<[number, number, number]>): string[] {
|
|
const urls: string[] = [];
|
|
for (const template of templates) {
|
|
for (const [z, x, y] of tiles) {
|
|
if (z < template.minzoom) continue;
|
|
// Past a source's last zoom the map overzooms the deepest tile, so fetch that one.
|
|
const shift = Math.max(0, z - template.maxzoom);
|
|
const zz = z - shift;
|
|
urls.push(
|
|
template.url
|
|
.replace('{z}', String(zz))
|
|
.replace('{x}', String(x >> shift))
|
|
.replace('{y}', String(y >> shift))
|
|
);
|
|
}
|
|
}
|
|
return [...new Set(urls)];
|
|
}
|
|
|
|
/** How long one tile may take: a tile server that never answers must not hold the whole share below 1. */
|
|
export const TILE_TIMEOUT_MS = 15_000;
|
|
|
|
/** Fetches the URLs a few at a time and reports progress from 0 to 1. A failed or stalled tile is skipped, not retried. */
|
|
export async function prefetchTiles(
|
|
urls: readonly string[],
|
|
signal: AbortSignal,
|
|
onProgress: (share: number) => void
|
|
): Promise<void> {
|
|
let done = 0;
|
|
let next = 0;
|
|
const total = urls.length || 1;
|
|
onProgress(urls.length ? 0 : 1);
|
|
const worker = async () => {
|
|
while (next < urls.length && !signal.aborted) {
|
|
const url = urls[next++];
|
|
const attempt = new AbortController();
|
|
const stop = () => attempt.abort();
|
|
signal.addEventListener('abort', stop);
|
|
const timer = setTimeout(stop, TILE_TIMEOUT_MS);
|
|
try {
|
|
await fetch(url, { signal: attempt.signal, mode: 'cors', cache: 'force-cache' });
|
|
} catch {
|
|
// A missing or stalled tile only means the map loads that one itself later.
|
|
} finally {
|
|
clearTimeout(timer);
|
|
signal.removeEventListener('abort', stop);
|
|
}
|
|
done++;
|
|
onProgress(done / total);
|
|
}
|
|
};
|
|
await Promise.all(Array.from({ length: CONCURRENCY }, worker));
|
|
}
|