@johnmorrisdotca/chikyu-earth 0.33.2 → 0.34.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +9 -0
- package/README.md +23 -5
- package/dist/cities/geometry.d.ts +233 -0
- package/dist/cities/geometry.js +174 -0
- package/dist/cities/host.d.ts +73 -0
- package/dist/cities/host.js +240 -0
- package/dist/element.d.ts +1 -1
- package/dist/element.js +2 -13
- package/dist/markers/index.d.ts +1 -0
- package/dist/markers/index.js +1 -0
- package/dist/markers/labels.d.ts +7 -1
- package/dist/markers/labels.js +12 -1
- package/dist/markers/place-layer.d.ts +43 -0
- package/dist/markers/place-layer.js +238 -0
- package/dist/markers/places.d.ts +279 -0
- package/dist/markers/places.js +213 -0
- package/dist/passes/element.js +2 -5
- package/dist/realistic/body-label.js +19 -11
- package/dist/realistic/element.d.ts +35 -0
- package/dist/realistic/element.js +94 -11
- package/dist/realistic/index.d.ts +2 -0
- package/dist/realistic/index.js +2 -0
- package/dist/satellites/index.d.ts +1 -0
- package/dist/satellites/index.js +1 -0
- package/dist/satellites/names.d.ts +14 -0
- package/dist/satellites/names.js +16 -0
- package/dist/space/element.d.ts +103 -1
- package/dist/space/element.js +219 -3
- package/dist/space/index.d.ts +3 -1
- package/dist/space/index.js +1 -0
- package/dist/space/satellite-layer.d.ts +26 -0
- package/dist/space/satellite-layer.js +37 -1
- package/dist/space/satellite-model.d.ts +72 -0
- package/dist/space/satellite-model.js +82 -0
- package/dist/space/scene.d.ts +42 -1
- package/dist/space/scene.js +152 -0
- package/package.json +7 -3
|
@@ -0,0 +1,240 @@
|
|
|
1
|
+
// The names of cities on the Earth, over the picture of an element (kuni's `cities`, loaded only when `city-names` is set, a tier at a time as the Earth grows on the screen). The element tells this where the Earth is at each draw (`frame`);
|
|
2
|
+
// it lays the names out, draws the dots on a canvas and the names as a few labels, and draws nothing at rest.
|
|
3
|
+
import { canvasRatio } from "../markers/project.js";
|
|
4
|
+
import { CITY_NAMES_MAX, cityRank, cityTiersFor, layoutCities, readCityNames } from "./geometry.js";
|
|
5
|
+
/** The styles of the layer, for an element to add to its own (`#cn-canvas`, `#cn-names` and the names in its shadow root). */
|
|
6
|
+
export const CITY_NAMES_STYLE = "#cn-canvas{position:absolute;inset:0;width:100%;height:100%;pointer-events:none}#cn-names{position:absolute;inset:0;overflow:hidden;pointer-events:none}" +
|
|
7
|
+
".cn-name{position:absolute;left:0;top:0;margin:0;white-space:nowrap;pointer-events:none;will-change:transform;font-family:var(--chikyu-city-font,system-ui,-apple-system,'Segoe UI',sans-serif);font-size:calc(var(--chikyu-city-size,12) * 1px);line-height:1.35;font-weight:var(--chikyu-city-weight,500);color:var(--chikyu-city-colour,#f3f7ff);text-shadow:0 0 calc(var(--chikyu-city-halo-width,3) * 1px) var(--chikyu-city-halo,#06101f),0 0 calc(var(--chikyu-city-halo-width,3) * 1px) var(--chikyu-city-halo,#06101f)}.cn-name[hidden]{display:none}";
|
|
8
|
+
const DEFAULTS = { size: 12, dot: 3, dotColour: "#ffd166", haloColour: "#06101f" };
|
|
9
|
+
/**
|
|
10
|
+
* The attributes the city names read: `city-names`.
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
* ```ts
|
|
14
|
+
* import { CITY_NAMES_ATTRIBUTES } from "@johnmorrisdotca/chikyu-earth/realistic";
|
|
15
|
+
*
|
|
16
|
+
* console.log(CITY_NAMES_ATTRIBUTES.join(" "));
|
|
17
|
+
* ```
|
|
18
|
+
*/
|
|
19
|
+
export const CITY_NAMES_ATTRIBUTES = ["city-names"];
|
|
20
|
+
const defaultLoad = async (tier) => {
|
|
21
|
+
const { loadCities } = await import("@johnmorrisdotca/kuni/cities");
|
|
22
|
+
return (await loadCities(tier)).cities;
|
|
23
|
+
};
|
|
24
|
+
/**
|
|
25
|
+
* Makes the layer of one element, inside its shadow root. Nothing of kuni is loaded, and nothing is added to the root, until `city-names` is there and the Earth is big enough for a name.
|
|
26
|
+
*
|
|
27
|
+
* @param options - The attribute reader, the language and the shadow root.
|
|
28
|
+
* @returns The host.
|
|
29
|
+
*/
|
|
30
|
+
export function createCityHost(options) {
|
|
31
|
+
const { root } = options;
|
|
32
|
+
const doc = root.ownerDocument;
|
|
33
|
+
const lists = new Map();
|
|
34
|
+
const asked = new Set();
|
|
35
|
+
let version = 0;
|
|
36
|
+
let cache;
|
|
37
|
+
let last = null;
|
|
38
|
+
let shown = [];
|
|
39
|
+
let renders = 0;
|
|
40
|
+
let style = {};
|
|
41
|
+
let canvas;
|
|
42
|
+
let box;
|
|
43
|
+
const pool = [];
|
|
44
|
+
let max = readCityNames(options.attribute("city-names"));
|
|
45
|
+
let drawnBefore = false;
|
|
46
|
+
/** The language the names were last laid out in: the page may change its language after the element was made, so it is read again at each draw. */
|
|
47
|
+
let usedLang;
|
|
48
|
+
const attach = () => {
|
|
49
|
+
if (canvas)
|
|
50
|
+
return;
|
|
51
|
+
canvas = doc.createElement("canvas");
|
|
52
|
+
canvas.id = "cn-canvas";
|
|
53
|
+
canvas.setAttribute("aria-hidden", "true");
|
|
54
|
+
box = doc.createElement("div");
|
|
55
|
+
box.id = "cn-names";
|
|
56
|
+
box.setAttribute("aria-hidden", "true");
|
|
57
|
+
root.append(canvas, box);
|
|
58
|
+
vars();
|
|
59
|
+
};
|
|
60
|
+
const clear = () => {
|
|
61
|
+
if (canvas && drawnBefore) {
|
|
62
|
+
canvas.getContext("2d")?.clearRect(0, 0, canvas.width, canvas.height);
|
|
63
|
+
drawnBefore = false;
|
|
64
|
+
}
|
|
65
|
+
for (const name of pool)
|
|
66
|
+
name.hidden = true;
|
|
67
|
+
shown = [];
|
|
68
|
+
};
|
|
69
|
+
function vars() {
|
|
70
|
+
if (!box)
|
|
71
|
+
return;
|
|
72
|
+
const set = (name, value) => {
|
|
73
|
+
if (value === undefined)
|
|
74
|
+
box.style.removeProperty(name);
|
|
75
|
+
else
|
|
76
|
+
box.style.setProperty(name, String(value));
|
|
77
|
+
};
|
|
78
|
+
set("--chikyu-city-font", style.font);
|
|
79
|
+
set("--chikyu-city-size", style.size);
|
|
80
|
+
set("--chikyu-city-weight", style.weight);
|
|
81
|
+
set("--chikyu-city-colour", style.colour);
|
|
82
|
+
set("--chikyu-city-halo", style.halo);
|
|
83
|
+
set("--chikyu-city-halo-width", style.haloWidth);
|
|
84
|
+
}
|
|
85
|
+
/** The width of a name in the font it is drawn in: a canvas set to the layer's own font, size and weight (the page's `--chikyu-city-*` properties, or the style set), so a fallback font with wider letters is measured as it is. */
|
|
86
|
+
let ruler;
|
|
87
|
+
const measurer = (size) => {
|
|
88
|
+
if (ruler === undefined)
|
|
89
|
+
ruler = doc.createElement("canvas").getContext?.("2d") ?? null;
|
|
90
|
+
const context = ruler;
|
|
91
|
+
if (!context || typeof context.measureText !== "function")
|
|
92
|
+
return undefined;
|
|
93
|
+
const host = root.host ? getComputedStyle(root.host) : undefined;
|
|
94
|
+
const family = style.font ?? (host?.getPropertyValue("--chikyu-city-font").trim() || "system-ui, -apple-system, 'Segoe UI', sans-serif");
|
|
95
|
+
const weight = style.weight ?? (host?.getPropertyValue("--chikyu-city-weight").trim() || 500);
|
|
96
|
+
context.font = `${weight} ${size}px ${family}`;
|
|
97
|
+
return (text) => context.measureText(text).width;
|
|
98
|
+
};
|
|
99
|
+
/** The cities of the tiers wanted that have arrived, the highest rank first; made again only when the tiers or the lists change. */
|
|
100
|
+
const citiesFor = (tiers) => {
|
|
101
|
+
const key = tiers.join();
|
|
102
|
+
if (cache?.key !== key || cache.version !== version) {
|
|
103
|
+
const cities = tiers.flatMap((tier) => lists.get(tier) ?? []).sort((a, b) => cityRank(b) - cityRank(a));
|
|
104
|
+
cache = { key, version, cities };
|
|
105
|
+
}
|
|
106
|
+
return cache.cities;
|
|
107
|
+
};
|
|
108
|
+
/** Asks for the tiers the Earth's size needs and are not here yet, one at a time. */
|
|
109
|
+
const ensure = (tiers) => {
|
|
110
|
+
const load = options.load ?? defaultLoad;
|
|
111
|
+
for (const tier of tiers) {
|
|
112
|
+
if (asked.has(tier))
|
|
113
|
+
continue;
|
|
114
|
+
asked.add(tier);
|
|
115
|
+
void load(tier)
|
|
116
|
+
.then((cities) => {
|
|
117
|
+
lists.set(tier, cities);
|
|
118
|
+
version += 1;
|
|
119
|
+
compute();
|
|
120
|
+
})
|
|
121
|
+
.catch((error) => options.onError(error));
|
|
122
|
+
}
|
|
123
|
+
};
|
|
124
|
+
function compute() {
|
|
125
|
+
const frame = last;
|
|
126
|
+
if (max === null || !frame)
|
|
127
|
+
return;
|
|
128
|
+
const tiers = cityTiersFor(frame.radius);
|
|
129
|
+
if (tiers.length === 0) {
|
|
130
|
+
clear();
|
|
131
|
+
return;
|
|
132
|
+
}
|
|
133
|
+
ensure(tiers);
|
|
134
|
+
attach();
|
|
135
|
+
const size = style.size ?? DEFAULTS.size;
|
|
136
|
+
const dot = style.dotSize ?? DEFAULTS.dot;
|
|
137
|
+
usedLang = options.language();
|
|
138
|
+
shown = layoutCities(citiesFor(tiers), frame, usedLang, { max, size, dot, measure: measurer(size) });
|
|
139
|
+
place(frame, dot);
|
|
140
|
+
}
|
|
141
|
+
function place(frame, dot) {
|
|
142
|
+
if (!canvas || !box)
|
|
143
|
+
return;
|
|
144
|
+
const ratio = canvasRatio(frame.width, frame.height, globalThis.devicePixelRatio || 1);
|
|
145
|
+
const w = Math.max(1, Math.round(frame.width * ratio));
|
|
146
|
+
const h = Math.max(1, Math.round(frame.height * ratio));
|
|
147
|
+
if (canvas.width !== w || canvas.height !== h) {
|
|
148
|
+
canvas.width = w;
|
|
149
|
+
canvas.height = h;
|
|
150
|
+
}
|
|
151
|
+
const context = canvas.getContext("2d");
|
|
152
|
+
if (!context)
|
|
153
|
+
return;
|
|
154
|
+
renders += 1;
|
|
155
|
+
drawnBefore = true;
|
|
156
|
+
context.setTransform(ratio, 0, 0, ratio, 0, 0);
|
|
157
|
+
context.clearRect(0, 0, frame.width, frame.height);
|
|
158
|
+
context.lineWidth = 1.5;
|
|
159
|
+
context.strokeStyle = style.halo ?? DEFAULTS.haloColour;
|
|
160
|
+
context.fillStyle = style.dot ?? DEFAULTS.dotColour;
|
|
161
|
+
for (const one of shown) {
|
|
162
|
+
context.beginPath();
|
|
163
|
+
context.arc(one.x, one.y, dot, 0, Math.PI * 2);
|
|
164
|
+
context.stroke();
|
|
165
|
+
context.fill();
|
|
166
|
+
}
|
|
167
|
+
while (pool.length < Math.min(CITY_NAMES_MAX, shown.length)) {
|
|
168
|
+
const name = doc.createElement("span");
|
|
169
|
+
name.className = "cn-name";
|
|
170
|
+
box.append(name);
|
|
171
|
+
pool.push(name);
|
|
172
|
+
}
|
|
173
|
+
for (let i = 0; i < pool.length; i += 1) {
|
|
174
|
+
const name = pool[i];
|
|
175
|
+
const one = shown[i];
|
|
176
|
+
if (!one) {
|
|
177
|
+
if (!name.hidden)
|
|
178
|
+
name.hidden = true;
|
|
179
|
+
continue;
|
|
180
|
+
}
|
|
181
|
+
if (name.textContent !== one.text)
|
|
182
|
+
name.textContent = one.text;
|
|
183
|
+
name.hidden = false;
|
|
184
|
+
name.style.transform = `translate(${Math.round(one.left)}px,${Math.round(one.top)}px)`;
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
// the family's chooser tells the document when the page's language changes: the names are written again, though nothing else is drawn
|
|
188
|
+
const onLanguage = () => queueMicrotask(() => {
|
|
189
|
+
if (max !== null && last && usedLang !== undefined && usedLang !== options.language())
|
|
190
|
+
compute();
|
|
191
|
+
});
|
|
192
|
+
doc.addEventListener("kazoku-languagechange", onLanguage);
|
|
193
|
+
// a font arrives: the names are measured in the font they are drawn in, so they are laid out again
|
|
194
|
+
const onFonts = () => {
|
|
195
|
+
if (max !== null && last)
|
|
196
|
+
compute();
|
|
197
|
+
};
|
|
198
|
+
doc.fonts?.addEventListener?.("loadingdone", onFonts);
|
|
199
|
+
return {
|
|
200
|
+
frame(next) {
|
|
201
|
+
last = next;
|
|
202
|
+
if (max === null)
|
|
203
|
+
return;
|
|
204
|
+
if (!next) {
|
|
205
|
+
clear();
|
|
206
|
+
return;
|
|
207
|
+
}
|
|
208
|
+
compute();
|
|
209
|
+
},
|
|
210
|
+
refresh() {
|
|
211
|
+
max = readCityNames(options.attribute("city-names"));
|
|
212
|
+
if (max === null) {
|
|
213
|
+
clear();
|
|
214
|
+
return;
|
|
215
|
+
}
|
|
216
|
+
compute();
|
|
217
|
+
},
|
|
218
|
+
setStyle(next) {
|
|
219
|
+
style = { ...next };
|
|
220
|
+
vars();
|
|
221
|
+
compute();
|
|
222
|
+
},
|
|
223
|
+
get stats() {
|
|
224
|
+
return { on: max !== null, max: max ?? 0, radius: last?.radius ?? 0, tiers: [...lists.keys()], cities: [...lists.values()].reduce((sum, list) => sum + list.length, 0), names: shown.length, renders };
|
|
225
|
+
},
|
|
226
|
+
dispose() {
|
|
227
|
+
doc.removeEventListener("kazoku-languagechange", onLanguage);
|
|
228
|
+
doc.fonts?.removeEventListener?.("loadingdone", onFonts);
|
|
229
|
+
canvas?.remove();
|
|
230
|
+
box?.remove();
|
|
231
|
+
canvas = undefined;
|
|
232
|
+
box = undefined;
|
|
233
|
+
pool.length = 0;
|
|
234
|
+
lists.clear();
|
|
235
|
+
asked.clear();
|
|
236
|
+
cache = undefined;
|
|
237
|
+
shown = [];
|
|
238
|
+
},
|
|
239
|
+
};
|
|
240
|
+
}
|
package/dist/element.d.ts
CHANGED
|
@@ -20,7 +20,7 @@ export declare class ChikyuEarth extends HTMLElement {
|
|
|
20
20
|
static observedAttributes: string[];
|
|
21
21
|
/** The controller behind the element's own hands (`play`, `set`, `fit`, `state`), while it is in a page. */
|
|
22
22
|
get controller(): ViewController | undefined;
|
|
23
|
-
/** Where the view is centred now: the `center` attribute, else
|
|
23
|
+
/** Where the view is centred now: the `center` attribute, else 0,0. The element never asks the browser where the reader is. */
|
|
24
24
|
get centre(): GeoPoint;
|
|
25
25
|
/** A box laid over the globe for markers (see `@johnmorrisdotca/chikyu-earth/markers`): where it is, the pose of the last drawing, and a way to hear the next. */
|
|
26
26
|
get overlay(): OverlayHost;
|
package/dist/element.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
// <chikyu-earth>: the globe as a web component. A shared view controller supplies its yaw, tilt, zoom and pan;
|
|
2
|
-
// it opens centred on
|
|
2
|
+
// it opens centred on the `center` attribute (0,0 without one) and never asks the browser where the reader is. Importing registers it once.
|
|
3
3
|
import { carryControls } from "@johnmorrisdotca/kazoku-ui/view-controls";
|
|
4
4
|
import { adoptStyles } from "./adopt.js";
|
|
5
5
|
import { globe } from "./globe.js";
|
|
@@ -51,7 +51,7 @@ export class ChikyuEarth extends HTMLElement {
|
|
|
51
51
|
get controller() {
|
|
52
52
|
return this.#hands?.controller;
|
|
53
53
|
}
|
|
54
|
-
/** Where the view is centred now: the `center` attribute, else
|
|
54
|
+
/** Where the view is centred now: the `center` attribute, else 0,0. The element never asks the browser where the reader is. */
|
|
55
55
|
get centre() {
|
|
56
56
|
return { ...this.#centre };
|
|
57
57
|
}
|
|
@@ -76,7 +76,6 @@ export class ChikyuEarth extends HTMLElement {
|
|
|
76
76
|
connectedCallback() {
|
|
77
77
|
this.#centre = readCentre(this.getAttribute("center")) ?? { lon: 0, lat: 0 };
|
|
78
78
|
this.#render();
|
|
79
|
-
this.#locate();
|
|
80
79
|
this.#hands = carryControls(this, {
|
|
81
80
|
initial: this.#view,
|
|
82
81
|
grabRadius: "fit",
|
|
@@ -118,16 +117,6 @@ export class ChikyuEarth extends HTMLElement {
|
|
|
118
117
|
}, wait);
|
|
119
118
|
}
|
|
120
119
|
}
|
|
121
|
-
#locate() {
|
|
122
|
-
if (readCentre(this.getAttribute("center")))
|
|
123
|
-
return;
|
|
124
|
-
if (typeof navigator === "undefined" || !navigator.geolocation)
|
|
125
|
-
return;
|
|
126
|
-
navigator.geolocation.getCurrentPosition((position) => {
|
|
127
|
-
this.#centre = { lon: position.coords.longitude, lat: position.coords.latitude };
|
|
128
|
-
this.#render();
|
|
129
|
-
}, () => { }, { timeout: 8000, maximumAge: 600000 });
|
|
130
|
-
}
|
|
131
120
|
#transform() {
|
|
132
121
|
const svg = this.shadowRoot?.querySelector("svg");
|
|
133
122
|
if (!svg)
|
package/dist/markers/index.d.ts
CHANGED
|
@@ -3,3 +3,4 @@ export { project, projectAll, canvasRatio, type Pose } from "./project.ts";
|
|
|
3
3
|
export { clusterPoints, type Cluster } from "./cluster.ts";
|
|
4
4
|
export { placeLabels, type LabelItem, type Placement } from "./labels.ts";
|
|
5
5
|
export type { GroupInfo, MarkerEventDetail, MarkerIcon, MarkerInput } from "./types.ts";
|
|
6
|
+
export { cleanPlaces, layoutPlaces, MAX_PLACES, nearby, orthoView, PLACE_LOOKS, readPlaces, type Place, type PlaceKind, type PlaceLabelSpot, type PlaceLayout, type PlaceLayoutOptions, type PlaceLook, type PlaceShape, type PlaceView, type SurfacePoint } from "./places.ts";
|
package/dist/markers/index.js
CHANGED
|
@@ -4,3 +4,4 @@ export { attachMarkers } from "./layer.js";
|
|
|
4
4
|
export { project, projectAll, canvasRatio } from "./project.js";
|
|
5
5
|
export { clusterPoints } from "./cluster.js";
|
|
6
6
|
export { placeLabels } from "./labels.js";
|
|
7
|
+
export { cleanPlaces, layoutPlaces, MAX_PLACES, nearby, orthoView, PLACE_LOOKS, readPlaces } from "./places.js";
|
package/dist/markers/labels.d.ts
CHANGED
|
@@ -48,6 +48,7 @@ export interface Placement {
|
|
|
48
48
|
* @param height - The height of that area, in pixels.
|
|
49
49
|
* @param max - The most labels to place.
|
|
50
50
|
* @param cell - The side of the grid cell used to find overlaps, in pixels.
|
|
51
|
+
* @param taken - Boxes already used by something else on the screen (another layer's names), as left, top, width and height: no label is put over one.
|
|
51
52
|
* @returns A placement for each label that fits; the others are left out.
|
|
52
53
|
*
|
|
53
54
|
* @example
|
|
@@ -61,4 +62,9 @@ export interface Placement {
|
|
|
61
62
|
* console.log(placeLabels(items, 300, 300, 10).map((place) => place.item));
|
|
62
63
|
* ```
|
|
63
64
|
*/
|
|
64
|
-
export declare function placeLabels(items: readonly LabelItem[], width: number, height: number, max: number, cell?: number
|
|
65
|
+
export declare function placeLabels(items: readonly LabelItem[], width: number, height: number, max: number, cell?: number, taken?: readonly {
|
|
66
|
+
left: number;
|
|
67
|
+
top: number;
|
|
68
|
+
width: number;
|
|
69
|
+
height: number;
|
|
70
|
+
}[]): Placement[];
|
package/dist/markers/labels.js
CHANGED
|
@@ -10,6 +10,7 @@ const GAP = 3;
|
|
|
10
10
|
* @param height - The height of that area, in pixels.
|
|
11
11
|
* @param max - The most labels to place.
|
|
12
12
|
* @param cell - The side of the grid cell used to find overlaps, in pixels.
|
|
13
|
+
* @param taken - Boxes already used by something else on the screen (another layer's names), as left, top, width and height: no label is put over one.
|
|
13
14
|
* @returns A placement for each label that fits; the others are left out.
|
|
14
15
|
*
|
|
15
16
|
* @example
|
|
@@ -23,7 +24,7 @@ const GAP = 3;
|
|
|
23
24
|
* console.log(placeLabels(items, 300, 300, 10).map((place) => place.item));
|
|
24
25
|
* ```
|
|
25
26
|
*/
|
|
26
|
-
export function placeLabels(items, width, height, max, cell = 64) {
|
|
27
|
+
export function placeLabels(items, width, height, max, cell = 64, taken = []) {
|
|
27
28
|
const order = items.map((_, i) => i).sort((a, b) => items[b].priority - items[a].priority || a - b);
|
|
28
29
|
const grid = new Map();
|
|
29
30
|
const placed = [];
|
|
@@ -41,6 +42,16 @@ export function placeLabels(items, width, height, max, cell = 64) {
|
|
|
41
42
|
return false;
|
|
42
43
|
return true;
|
|
43
44
|
};
|
|
45
|
+
for (const used of taken) {
|
|
46
|
+
const box = { l: used.left, t: used.top, r: used.left + used.width, b: used.top + used.height };
|
|
47
|
+
for (const key of keys(box)) {
|
|
48
|
+
const list = grid.get(key);
|
|
49
|
+
if (list)
|
|
50
|
+
list.push(box);
|
|
51
|
+
else
|
|
52
|
+
grid.set(key, [box]);
|
|
53
|
+
}
|
|
54
|
+
}
|
|
44
55
|
for (const index of order) {
|
|
45
56
|
if (placed.length >= max)
|
|
46
57
|
break;
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { type Place, type PlaceLook, type PlaceView } from "./places.ts";
|
|
2
|
+
/** A `places-look` attribute: one of `PLACE_LOOKS`, else `beam`. */
|
|
3
|
+
export declare const readPlaceLook: (value: string | null) => PlaceLook;
|
|
4
|
+
/** The places' look, for the element's own style sheet. Colours: `--chikyu-place-here` and `--chikyu-place-favourite` on the element or the page. */
|
|
5
|
+
export declare const PLACE_STYLE: string;
|
|
6
|
+
/** A satellite ringed and named in the picture just drawn: its name, where it is and whether it can be seen (in the box and not behind the Earth). */
|
|
7
|
+
export interface SatelliteMark {
|
|
8
|
+
id: string;
|
|
9
|
+
label: string;
|
|
10
|
+
x: number;
|
|
11
|
+
y: number;
|
|
12
|
+
visible: boolean;
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* The places' markers in one element: hand it the places and the look, and a view at each draw.
|
|
16
|
+
*/
|
|
17
|
+
export interface PlaceLayer {
|
|
18
|
+
set(places: readonly Place[]): void;
|
|
19
|
+
look: PlaceLook;
|
|
20
|
+
/** Lay the places on the picture just drawn; `null` hides them (the Earth is not in the picture). `taken` is boxes other names use; `marks` are satellites to ring and name, after the places' names and never over them. */
|
|
21
|
+
frame(view: PlaceView | null, taken?: readonly {
|
|
22
|
+
left: number;
|
|
23
|
+
top: number;
|
|
24
|
+
width: number;
|
|
25
|
+
height: number;
|
|
26
|
+
}[], marks?: readonly SatelliteMark[]): void;
|
|
27
|
+
readonly stats: {
|
|
28
|
+
places: number;
|
|
29
|
+
shown: number;
|
|
30
|
+
labels: number;
|
|
31
|
+
renders: number;
|
|
32
|
+
rings: number;
|
|
33
|
+
satelliteNames: number;
|
|
34
|
+
};
|
|
35
|
+
dispose(): void;
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Make the places' layer in a box of an element's shadow root (the box is `#places`, styled by {@link PLACE_STYLE}).
|
|
39
|
+
*
|
|
40
|
+
* @param box - The element's `#places` box.
|
|
41
|
+
* @returns The layer.
|
|
42
|
+
*/
|
|
43
|
+
export declare function createPlaceLayer(box: HTMLElement): PlaceLayer;
|
|
@@ -0,0 +1,238 @@
|
|
|
1
|
+
// The places' markers in an element: a small box for each place, moved by CSS transforms when the picture is drawn, and a name for each that has room. The pulse of a `here` place is a CSS
|
|
2
|
+
// animation, so the picture is never drawn again for it; it stops while the element is out of sight, and a reader who asked for less motion gets none. Names are measured as the page draws them.
|
|
3
|
+
import { placeLabels } from "./labels.js";
|
|
4
|
+
import { layoutPlaces, PLACE_LOOKS } from "./places.js";
|
|
5
|
+
/** A `places-look` attribute: one of `PLACE_LOOKS`, else `beam`. */
|
|
6
|
+
export const readPlaceLook = (value) => PLACE_LOOKS.includes(value ?? "") ? value : "beam";
|
|
7
|
+
/** The places' look, for the element's own style sheet. Colours: `--chikyu-place-here` and `--chikyu-place-favourite` on the element or the page. */
|
|
8
|
+
export const PLACE_STYLE = "#places{position:absolute;inset:0;overflow:hidden;pointer-events:none}" +
|
|
9
|
+
".pl-mark{position:absolute;left:0;top:0;width:0;height:0;will-change:transform,opacity;--c:var(--chikyu-place-favourite,#ffd27a)}" +
|
|
10
|
+
".pl-mark[data-kind=here]{--c:var(--chikyu-place-here,#7df9ff)}" +
|
|
11
|
+
".pl-mark[hidden],.pl-name[hidden]{display:none}" +
|
|
12
|
+
".pl-disc{position:absolute;left:calc(var(--r) * -1);top:calc(var(--r) * -1);width:calc(var(--r) * 2);height:calc(var(--r) * 2)}" +
|
|
13
|
+
".pl-ring,.pl-pulse{position:absolute;inset:0;box-sizing:border-box;border-radius:50%;border:1.5px solid var(--c)}" +
|
|
14
|
+
".pl-ring{box-shadow:0 0 6px var(--c),inset 0 0 4px var(--c);background:radial-gradient(circle,var(--c) 0 18%,transparent 22%)}" +
|
|
15
|
+
".pl-pulse{opacity:0}" +
|
|
16
|
+
".pl-mark[data-kind=here] .pl-pulse{animation:pl-pulse 2.6s ease-out infinite}" +
|
|
17
|
+
"#places.pl-still .pl-pulse{animation-play-state:paused}" +
|
|
18
|
+
"@keyframes pl-pulse{0%{transform:scale(1);opacity:.75}100%{transform:scale(2.8);opacity:0}}" +
|
|
19
|
+
"@media (prefers-reduced-motion:reduce){.pl-mark[data-kind=here] .pl-pulse{animation:none}}" +
|
|
20
|
+
".pl-stick{position:absolute;left:-1px;bottom:0;width:2px;height:100px;transform-origin:50% 100%;background:linear-gradient(to top,var(--c),color-mix(in srgb,var(--c) 40%,transparent) 70%,transparent);box-shadow:0 0 5px var(--c);border-radius:1px}" +
|
|
21
|
+
".pl-head{position:absolute;left:-2.5px;top:-2.5px;width:5px;height:5px;border-radius:50%;background:#fff;box-shadow:0 0 6px 1px var(--c)}" +
|
|
22
|
+
".pl-mark[data-kind=favourite] .pl-stick{width:1.5px;opacity:.8}" +
|
|
23
|
+
".pl-mark[data-kind=favourite] .pl-head{opacity:.85}" +
|
|
24
|
+
"#places[data-look=pin] .pl-stick{background:var(--c);box-shadow:none;width:1.5px}" +
|
|
25
|
+
"#places[data-look=pin] .pl-head{left:-4px;top:-4px;width:8px;height:8px;background:var(--c);box-shadow:0 0 0 1.5px rgba(4,10,20,.75)}" +
|
|
26
|
+
"#places[data-look=pin] .pl-ring{box-shadow:none;background:none;opacity:.7}" +
|
|
27
|
+
"#places[data-look=ring] .pl-stick,#places[data-look=ring] .pl-head{display:none}" +
|
|
28
|
+
".pl-name{position:absolute;left:0;top:0;box-sizing:border-box;padding:1px 6px;font:600 11px/16px system-ui,-apple-system,'Segoe UI',sans-serif;letter-spacing:.04em;white-space:nowrap;color:#eefcff;background:rgba(4,10,20,.72);border:1px solid color-mix(in srgb,var(--c) 60%,transparent);border-radius:3px;will-change:transform;--c:var(--chikyu-place-favourite,#ffd27a)}" +
|
|
29
|
+
".pl-name[data-kind=here]{--c:var(--chikyu-place-here,#7df9ff);color:#fff}" +
|
|
30
|
+
".pl-name[data-kind=satellite]{--c:#cfeaff;color:#cfeaff;font-weight:500;background:rgba(4,10,20,.5);border-color:transparent}" +
|
|
31
|
+
".pl-sat{position:absolute;left:-8px;top:-8px;width:16px;height:16px;box-sizing:border-box;border:1.2px solid #cfeaff;border-radius:50%;opacity:.85;will-change:transform}" +
|
|
32
|
+
".pl-sat[hidden]{display:none}" +
|
|
33
|
+
".pl-name.pl-measure{visibility:hidden}";
|
|
34
|
+
/** The ring round a named satellite, in CSS pixels. */
|
|
35
|
+
const SATELLITE_RING = 8;
|
|
36
|
+
/**
|
|
37
|
+
* Make the places' layer in a box of an element's shadow root (the box is `#places`, styled by {@link PLACE_STYLE}).
|
|
38
|
+
*
|
|
39
|
+
* @param box - The element's `#places` box.
|
|
40
|
+
* @returns The layer.
|
|
41
|
+
*/
|
|
42
|
+
export function createPlaceLayer(box) {
|
|
43
|
+
const doc = box.ownerDocument;
|
|
44
|
+
box.setAttribute("aria-hidden", "true");
|
|
45
|
+
box.dataset["look"] = "beam";
|
|
46
|
+
let places = [];
|
|
47
|
+
let look = "beam";
|
|
48
|
+
const marks = [];
|
|
49
|
+
const names = [];
|
|
50
|
+
const sizes = new Map();
|
|
51
|
+
const stats = { places: 0, shown: 0, labels: 0, renders: 0, rings: 0, satelliteNames: 0 };
|
|
52
|
+
const rings = [];
|
|
53
|
+
let measure = null;
|
|
54
|
+
const size = (place) => {
|
|
55
|
+
if (!place.label)
|
|
56
|
+
return null;
|
|
57
|
+
const kind = place.kind === "here" || place.kind === "satellite" ? place.kind : "favourite";
|
|
58
|
+
const key = `${kind}\n${place.label}`;
|
|
59
|
+
let found = sizes.get(key);
|
|
60
|
+
if (!found) {
|
|
61
|
+
if (!measure) {
|
|
62
|
+
measure = doc.createElement("span");
|
|
63
|
+
measure.className = "pl-name pl-measure";
|
|
64
|
+
box.append(measure);
|
|
65
|
+
}
|
|
66
|
+
measure.dataset["kind"] = kind;
|
|
67
|
+
measure.textContent = place.label;
|
|
68
|
+
const rect = measure.getBoundingClientRect();
|
|
69
|
+
found = { width: Math.ceil(rect.width), height: Math.ceil(rect.height) };
|
|
70
|
+
if (found.width === 0)
|
|
71
|
+
return null;
|
|
72
|
+
if (sizes.size > 500)
|
|
73
|
+
sizes.clear();
|
|
74
|
+
sizes.set(key, found);
|
|
75
|
+
}
|
|
76
|
+
return found;
|
|
77
|
+
};
|
|
78
|
+
// a font that arrives later draws the names wider or narrower: measure them again
|
|
79
|
+
const fonts = doc.fonts;
|
|
80
|
+
const refit = () => sizes.clear();
|
|
81
|
+
fonts?.addEventListener?.("loadingdone", refit);
|
|
82
|
+
const markAt = (i) => {
|
|
83
|
+
let mark = marks[i];
|
|
84
|
+
if (!mark) {
|
|
85
|
+
const root = doc.createElement("div");
|
|
86
|
+
root.className = "pl-mark";
|
|
87
|
+
const disc = doc.createElement("div");
|
|
88
|
+
disc.className = "pl-disc";
|
|
89
|
+
disc.innerHTML = '<i class="pl-ring"></i><i class="pl-pulse"></i>';
|
|
90
|
+
const stick = doc.createElement("i");
|
|
91
|
+
stick.className = "pl-stick";
|
|
92
|
+
const head = doc.createElement("i");
|
|
93
|
+
head.className = "pl-head";
|
|
94
|
+
root.append(disc, stick, head);
|
|
95
|
+
box.append(root);
|
|
96
|
+
mark = { root, disc, stick, head, kind: "", ring: 0 };
|
|
97
|
+
marks[i] = mark;
|
|
98
|
+
}
|
|
99
|
+
return mark;
|
|
100
|
+
};
|
|
101
|
+
const nameAt = (i) => {
|
|
102
|
+
let name = names[i];
|
|
103
|
+
if (!name) {
|
|
104
|
+
name = doc.createElement("span");
|
|
105
|
+
name.className = "pl-name";
|
|
106
|
+
box.append(name);
|
|
107
|
+
names[i] = name;
|
|
108
|
+
}
|
|
109
|
+
return name;
|
|
110
|
+
};
|
|
111
|
+
const hideFrom = (list, from) => {
|
|
112
|
+
for (let i = from; i < list.length; i += 1)
|
|
113
|
+
if (!list[i].hidden)
|
|
114
|
+
list[i].hidden = true;
|
|
115
|
+
};
|
|
116
|
+
// the pulse stops while the element is out of sight
|
|
117
|
+
const watcher = typeof IntersectionObserver === "function" ? new IntersectionObserver((entries) => box.classList.toggle("pl-still", !entries.some((entry) => entry.isIntersecting))) : null;
|
|
118
|
+
watcher?.observe(box);
|
|
119
|
+
return {
|
|
120
|
+
set(next) {
|
|
121
|
+
places = next;
|
|
122
|
+
stats.places = next.length;
|
|
123
|
+
},
|
|
124
|
+
get look() {
|
|
125
|
+
return look;
|
|
126
|
+
},
|
|
127
|
+
set look(value) {
|
|
128
|
+
look = value;
|
|
129
|
+
box.dataset["look"] = value;
|
|
130
|
+
},
|
|
131
|
+
frame(view, taken = [], satellites = []) {
|
|
132
|
+
if (!view || (places.length === 0 && satellites.length === 0)) {
|
|
133
|
+
hideFrom(marks.map((m) => m.root), 0);
|
|
134
|
+
hideFrom(names, 0);
|
|
135
|
+
hideFrom(rings, 0);
|
|
136
|
+
stats.shown = 0;
|
|
137
|
+
stats.labels = 0;
|
|
138
|
+
stats.rings = 0;
|
|
139
|
+
stats.satelliteNames = 0;
|
|
140
|
+
if (places.length > 0)
|
|
141
|
+
stats.renders += 1;
|
|
142
|
+
return;
|
|
143
|
+
}
|
|
144
|
+
stats.renders += 1;
|
|
145
|
+
const layout = layoutPlaces(places, view, look, { size, taken });
|
|
146
|
+
// favourites first, so a `here` marker is drawn over them
|
|
147
|
+
const order = [...layout.shapes].sort((a, b) => (places[a.index].kind === "here" ? 1 : 0) - (places[b.index].kind === "here" ? 1 : 0));
|
|
148
|
+
for (const [i, shape] of order.entries()) {
|
|
149
|
+
const mark = markAt(i);
|
|
150
|
+
const kind = places[shape.index].kind === "here" ? "here" : "favourite";
|
|
151
|
+
if (mark.kind !== kind) {
|
|
152
|
+
mark.kind = kind;
|
|
153
|
+
mark.root.dataset["kind"] = kind;
|
|
154
|
+
}
|
|
155
|
+
if (mark.ring !== shape.ring) {
|
|
156
|
+
mark.ring = shape.ring;
|
|
157
|
+
mark.root.style.setProperty("--r", `${shape.ring}px`);
|
|
158
|
+
}
|
|
159
|
+
if (mark.root.hidden)
|
|
160
|
+
mark.root.hidden = false;
|
|
161
|
+
mark.root.style.transform = `translate(${shape.x.toFixed(1)}px,${shape.y.toFixed(1)}px)`;
|
|
162
|
+
mark.root.style.opacity = shape.opacity >= 1 ? "" : shape.opacity.toFixed(2);
|
|
163
|
+
mark.disc.style.transform = `rotate(${shape.ringAngle.toFixed(1)}deg) scale(1,${shape.squash.toFixed(3)})`;
|
|
164
|
+
mark.stick.style.transform = `rotate(${shape.angle.toFixed(1)}deg) scaleY(${(shape.length / 100).toFixed(3)})`;
|
|
165
|
+
mark.head.style.transform = `translate(${(shape.tipX - shape.x).toFixed(1)}px,${(shape.tipY - shape.y).toFixed(1)}px)`;
|
|
166
|
+
}
|
|
167
|
+
hideFrom(marks.map((m) => m.root), order.length);
|
|
168
|
+
for (const [i, spot] of layout.labels.entries()) {
|
|
169
|
+
const place = places[spot.index];
|
|
170
|
+
const name = nameAt(i);
|
|
171
|
+
const kind = place.kind === "here" ? "here" : "favourite";
|
|
172
|
+
if (name.textContent !== place.label)
|
|
173
|
+
name.textContent = place.label ?? "";
|
|
174
|
+
if (name.dataset["kind"] !== kind)
|
|
175
|
+
name.dataset["kind"] = kind;
|
|
176
|
+
if (name.hidden)
|
|
177
|
+
name.hidden = false;
|
|
178
|
+
name.style.transform = `translate(${Math.round(spot.left)}px,${Math.round(spot.top)}px)`;
|
|
179
|
+
}
|
|
180
|
+
// the ringed satellites: a ring round each that can be seen, and its name where it is free of the places' names and the boxes taken
|
|
181
|
+
const used = [...taken];
|
|
182
|
+
for (const spot of layout.labels) {
|
|
183
|
+
const found = size(places[spot.index]);
|
|
184
|
+
if (found)
|
|
185
|
+
used.push({ left: spot.left, top: spot.top, ...found });
|
|
186
|
+
}
|
|
187
|
+
const seen = satellites.filter((mark) => mark.visible);
|
|
188
|
+
for (const [i, mark] of seen.entries()) {
|
|
189
|
+
let ring = rings[i];
|
|
190
|
+
if (!ring) {
|
|
191
|
+
ring = doc.createElement("i");
|
|
192
|
+
ring.className = "pl-sat";
|
|
193
|
+
box.append(ring);
|
|
194
|
+
rings[i] = ring;
|
|
195
|
+
}
|
|
196
|
+
if (ring.hidden)
|
|
197
|
+
ring.hidden = false;
|
|
198
|
+
ring.style.transform = `translate(${mark.x.toFixed(1)}px,${mark.y.toFixed(1)}px)`;
|
|
199
|
+
}
|
|
200
|
+
hideFrom(rings, seen.length);
|
|
201
|
+
const items = [];
|
|
202
|
+
const owners = [];
|
|
203
|
+
for (const [i, mark] of seen.entries()) {
|
|
204
|
+
const found = mark.label ? size({ label: mark.label, kind: "satellite" }) : null;
|
|
205
|
+
if (!found)
|
|
206
|
+
continue;
|
|
207
|
+
items.push({ x: mark.x, y: mark.y, r: SATELLITE_RING + 2, w: found.width, h: found.height, priority: -i });
|
|
208
|
+
owners.push(mark);
|
|
209
|
+
}
|
|
210
|
+
const placed = placeLabels(items, view.width, view.height, 24, 64, used);
|
|
211
|
+
for (const [k, spot] of placed.entries()) {
|
|
212
|
+
const mark = owners[spot.item];
|
|
213
|
+
const name = nameAt(layout.labels.length + k);
|
|
214
|
+
if (name.textContent !== mark.label)
|
|
215
|
+
name.textContent = mark.label;
|
|
216
|
+
if (name.dataset["kind"] !== "satellite")
|
|
217
|
+
name.dataset["kind"] = "satellite";
|
|
218
|
+
if (name.hidden)
|
|
219
|
+
name.hidden = false;
|
|
220
|
+
name.style.transform = `translate(${Math.round(spot.left)}px,${Math.round(spot.top)}px)`;
|
|
221
|
+
}
|
|
222
|
+
hideFrom(names, layout.labels.length + placed.length);
|
|
223
|
+
stats.shown = order.length;
|
|
224
|
+
stats.labels = layout.labels.length;
|
|
225
|
+
stats.rings = seen.length;
|
|
226
|
+
stats.satelliteNames = placed.length;
|
|
227
|
+
},
|
|
228
|
+
stats,
|
|
229
|
+
dispose() {
|
|
230
|
+
watcher?.disconnect();
|
|
231
|
+
fonts?.removeEventListener?.("loadingdone", refit);
|
|
232
|
+
box.replaceChildren();
|
|
233
|
+
marks.length = 0;
|
|
234
|
+
names.length = 0;
|
|
235
|
+
measure = null;
|
|
236
|
+
},
|
|
237
|
+
};
|
|
238
|
+
}
|