@johnmorrisdotca/chikyu-earth 0.42.0 → 0.45.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.
Files changed (43) hide show
  1. package/CHANGELOG.md +17 -0
  2. package/NOTICE.md +15 -5
  3. package/README.md +4 -4
  4. package/dist/door/map.js +5 -1
  5. package/dist/element.d.ts +30 -0
  6. package/dist/element.js +82 -2
  7. package/dist/markers/here-city-host.d.ts +3 -1
  8. package/dist/markers/here-city-host.js +67 -5
  9. package/dist/markers/here-city.d.ts +94 -1
  10. package/dist/markers/here-city.js +69 -1
  11. package/dist/markers/index.d.ts +1 -1
  12. package/dist/markers/index.js +1 -1
  13. package/dist/realistic/element-base.d.ts +44 -1
  14. package/dist/realistic/element-base.js +128 -16
  15. package/dist/realistic/index.d.ts +1 -0
  16. package/dist/realistic/index.js +1 -0
  17. package/dist/realistic/zoom-reach.d.ts +29 -0
  18. package/dist/realistic/zoom-reach.js +30 -0
  19. package/dist/satellites/catalogue.d.ts +52 -1
  20. package/dist/satellites/catalogue.js +96 -5
  21. package/dist/satellites/index.d.ts +1 -1
  22. package/dist/satellites/index.js +1 -1
  23. package/dist/sea-names/features.d.ts +12 -0
  24. package/dist/sea-names/features.js +46 -0
  25. package/dist/sea-names/geometry.d.ts +121 -0
  26. package/dist/sea-names/geometry.js +135 -0
  27. package/dist/sea-names/host.d.ts +46 -0
  28. package/dist/sea-names/host.js +111 -0
  29. package/dist/space/element-base.d.ts +58 -11
  30. package/dist/space/element-base.js +153 -13
  31. package/dist/space/generic-satellite.d.ts +46 -0
  32. package/dist/space/generic-satellite.js +96 -0
  33. package/dist/space/index.d.ts +2 -1
  34. package/dist/space/index.js +2 -1
  35. package/dist/space/ride.d.ts +45 -0
  36. package/dist/space/ride.js +38 -0
  37. package/dist/space/satellite-focus.d.ts +90 -0
  38. package/dist/space/satellite-focus.js +109 -0
  39. package/dist/space/satellite-layer.d.ts +32 -3
  40. package/dist/space/satellite-layer.js +95 -29
  41. package/dist/space/scene.d.ts +3 -1
  42. package/dist/space/scene.js +54 -25
  43. package/package.json +3 -3
@@ -1,5 +1,6 @@
1
1
  // A small curated catalogue of satellites worth following: the groups the demo offers as buttons and the famous ones by name. Facts (names, NORAD numbers, orbit types) and our own words;
2
2
  // no orbital elements are here, and nothing is fetched. The elements come from the page (see docs/satellite-sources.md for the sources and their terms).
3
+ import { SHORT_NAMES } from "./names.js";
3
4
  /**
4
5
  * The groups, in the order the demo shows them. Each one is a CelesTrak list or a few single numbers; the elements are fetched by the page (or the demo's scheduled build), never by the package.
5
6
  *
@@ -205,7 +206,87 @@ export const CATALOGUE = [
205
206
  /** A name made easy to compare: upper case, letters and digits only. */
206
207
  const squash = (text) => text.toUpperCase().replace(/[^A-Z0-9]+/g, "");
207
208
  /**
208
- * Find catalogue entries by name, group name or NORAD number: "hubble", "hst", "25544" or "weather". Matching ignores case, spaces and punctuation, and a name that starts with the text comes before one that only has it inside.
209
+ * Hiragana readings of the Japanese names already in this catalogue, for a page that indexes by sound.
210
+ * A name that is already kana or Latin has none. These are this package's own readings (NOTICE.md).
211
+ */
212
+ const READINGS = {
213
+ 宇宙ステーション: "うちゅうステーション",
214
+ 有名な衛星: "ゆうめいなえいせい",
215
+ 科学衛星: "かがくえいせい",
216
+ 地球観測: "ちきゅうかんそく",
217
+ 気象衛星: "きしょうえいせい",
218
+ 静止衛星: "せいしえいせい",
219
+ 北斗: "ほくと",
220
+ アマチュア無線: "あまちゅあむせん",
221
+ 天宮: "てんぐう",
222
+ };
223
+ const GROUPS_BY_ID = new Map(SATELLITE_GROUPS.map((group) => [group.id, group]));
224
+ /** English and Japanese names. The reading is present only when it adds kana for a name that is not already kana. */
225
+ function rowNames(en, ja) {
226
+ const reading = READINGS[ja];
227
+ return reading !== undefined && reading !== ja ? { en, ja, reading } : { en, ja };
228
+ }
229
+ /**
230
+ * Other names already written on the catalogue entry: the catalogue's own name when a short name is shown instead, and a designation written in parentheses.
231
+ * Nothing is added that is not in that name. See NOTICE.md.
232
+ */
233
+ function akaOf(entry) {
234
+ const short = SHORT_NAMES[entry.noradId];
235
+ const en = short?.en ?? entry.name;
236
+ const ja = short?.ja ?? en;
237
+ const extras = [];
238
+ const add = (name) => {
239
+ const trimmed = name.trim();
240
+ if (trimmed === "" || trimmed === en || trimmed === ja || extras.includes(trimmed))
241
+ return;
242
+ extras.push(trimmed);
243
+ };
244
+ if (short)
245
+ add(entry.name);
246
+ const paren = /\(([^)]+)\)/.exec(entry.name);
247
+ if (paren?.[1])
248
+ add(paren[1]);
249
+ return extras.length > 0 ? extras : undefined;
250
+ }
251
+ /**
252
+ * Plain rows for every group and every catalogue satellite, for a page to index. Both languages are on every row: `lang` says which language the page is showing and does not drop the other, so one index serves either.
253
+ * A satellite's Japanese name comes from {@link SHORT_NAMES} where that has one, and is otherwise the English name.
254
+ *
255
+ * @param lang - `en` or `ja`, the language the page is showing. Omit it for the same rows.
256
+ * @returns The groups in their own order, then the satellites in catalogue order.
257
+ * @example
258
+ * ```ts
259
+ * import { catalogueRows } from "@johnmorrisdotca/chikyu-earth/satellites";
260
+ *
261
+ * const hubble = catalogueRows("ja").find((row) => row.noradId === 20580);
262
+ * console.log(hubble?.names.ja, hubble?.aka?.join(", "));
263
+ * ```
264
+ */
265
+ export function catalogueRows(lang) {
266
+ if (lang !== undefined && lang !== "en" && lang !== "ja")
267
+ throw new TypeError("catalogueRows lang is en or ja");
268
+ const groups = SATELLITE_GROUPS.map((group) => ({
269
+ kind: "group",
270
+ id: group.id,
271
+ names: rowNames(group.label, group.labelJa),
272
+ orbit: group.orbit,
273
+ }));
274
+ const satellites = CATALOGUE.map((entry) => {
275
+ const short = SHORT_NAMES[entry.noradId];
276
+ const en = short?.en ?? entry.name;
277
+ const ja = short?.ja ?? en;
278
+ const aka = akaOf(entry);
279
+ const row = { kind: "satellite", id: String(entry.noradId), noradId: entry.noradId, names: rowNames(en, ja), group: entry.group, orbit: entry.orbit };
280
+ if (aka)
281
+ row.aka = aka;
282
+ return row;
283
+ });
284
+ return [...groups, ...satellites];
285
+ }
286
+ /**
287
+ * Find catalogue entries by name, group name, NORAD number, Japanese name or a group's Japanese label: "hubble", "hst", "25544", "weather" or "ハッブル".
288
+ * An English or numeric query ignores case, spaces and punctuation, as before. Kana and kanji are matched as typed: only letter case and character width are folded (NFKC), and that same width fold is applied before the English comparison.
289
+ * A name that starts with the text comes before one that only has it inside.
209
290
  *
210
291
  * @param text - What the reader typed.
211
292
  * @param limit - The most entries to give back (8 by default).
@@ -215,19 +296,29 @@ const squash = (text) => text.toUpperCase().replace(/[^A-Z0-9]+/g, "");
215
296
  * import { findInCatalogue } from "@johnmorrisdotca/chikyu-earth/satellites";
216
297
  *
217
298
  * console.log(findInCatalogue("sentinel 2").map((entry) => entry.name));
299
+ * console.log(findInCatalogue("ハッブル").map((entry) => entry.name));
218
300
  * ```
219
301
  */
220
302
  export function findInCatalogue(text, limit = 8) {
221
- const wanted = squash(text);
222
- if (!wanted)
303
+ const source = text.normalize("NFKC");
304
+ const wanted = squash(source);
305
+ const folded = source.toLowerCase().trim();
306
+ const japanese = /[぀-ヿ一-鿿]/.test(source);
307
+ if (!wanted && !japanese)
223
308
  return [];
224
309
  const starts = [];
225
310
  const inside = [];
226
311
  for (const entry of CATALOGUE) {
227
312
  const name = squash(entry.name);
228
- if (String(entry.noradId) === wanted || name.startsWith(wanted))
313
+ const englishStart = wanted !== "" && (String(entry.noradId) === wanted || name.startsWith(wanted));
314
+ const englishInside = wanted !== "" && (name.includes(wanted) || squash(entry.group).includes(wanted) || squash(entry.about).includes(wanted));
315
+ const jaName = japanese ? (SHORT_NAMES[entry.noradId]?.ja ?? entry.name).normalize("NFKC").toLowerCase() : "";
316
+ const labelJa = japanese ? (GROUPS_BY_ID.get(entry.group)?.labelJa ?? "").normalize("NFKC").toLowerCase() : "";
317
+ const jaStart = japanese && folded !== "" && jaName.startsWith(folded);
318
+ const jaInside = japanese && folded !== "" && (jaName.includes(folded) || labelJa.includes(folded));
319
+ if (englishStart || jaStart)
229
320
  starts.push(entry);
230
- else if (name.includes(wanted) || squash(entry.group).includes(wanted) || squash(entry.about).includes(wanted))
321
+ else if (englishInside || jaInside)
231
322
  inside.push(entry);
232
323
  }
233
324
  return [...starts, ...inside].slice(0, limit);
@@ -4,6 +4,6 @@ export { DEFAULT_DIM_AFTER_DAYS, DEFAULT_MAX_AGE_DAYS, MAX_SATELLITE_SPEED } fro
4
4
  export type { ElementSet, RefusedElementSet, SatelliteInfo, SatelliteRef, Satellites } from "./types.ts";
5
5
  export { SHORT_NAMES } from "./names.ts";
6
6
  export { OWNER_COUNTRIES, ownerCountry } from "./owners.ts";
7
- export { CATALOGUE, SATELLITE_GROUPS, catalogueEntry, findInCatalogue, type CatalogueEntry, type OrbitKind, type SatelliteGroupInfo } from "./catalogue.ts";
7
+ export { CATALOGUE, SATELLITE_GROUPS, catalogueEntry, catalogueRows, findInCatalogue, type CatalogueEntry, type CatalogueRow, type OrbitKind, type SatelliteGroupInfo } from "./catalogue.ts";
8
8
  export { ElementSourceError, fetchElements, forgetElements, parseElements, type ElementFormat, type ElementSource, type FetchElementsOptions, type ParsedElements } from "./source.ts";
9
9
  export { missionElements, missionOf, missionPosition, missions, type HubbleVisit, type Mission, type MissionFile, type MissionPosition, type Orbiter } from "./missions.ts";
@@ -4,6 +4,6 @@ export { NEAREST_COUNT, closeApproach, nearest, overheadKm, within } from "./nea
4
4
  export { DEFAULT_DIM_AFTER_DAYS, DEFAULT_MAX_AGE_DAYS, MAX_SATELLITE_SPEED } from "./limits.js";
5
5
  export { SHORT_NAMES } from "./names.js";
6
6
  export { OWNER_COUNTRIES, ownerCountry } from "./owners.js";
7
- export { CATALOGUE, SATELLITE_GROUPS, catalogueEntry, findInCatalogue } from "./catalogue.js";
7
+ export { CATALOGUE, SATELLITE_GROUPS, catalogueEntry, catalogueRows, findInCatalogue } from "./catalogue.js";
8
8
  export { ElementSourceError, fetchElements, forgetElements, parseElements } from "./source.js";
9
9
  export { missionElements, missionOf, missionPosition, missions } from "./missions.js";
@@ -0,0 +1,12 @@
1
+ import type { SeaFeature } from "./geometry.ts";
2
+ /**
3
+ * The oceans, seas, gulfs, bays and continents that can be named, each with the point its name is set at.
4
+ *
5
+ * @example
6
+ * ```ts
7
+ * import { SEA_FEATURES } from "@johnmorrisdotca/chikyu-earth/realistic";
8
+ *
9
+ * console.log(SEA_FEATURES.find((one) => one.id === "africa")?.name.en);
10
+ * ```
11
+ */
12
+ export declare const SEA_FEATURES: readonly SeaFeature[];
@@ -0,0 +1,46 @@
1
+ /**
2
+ * The oceans, seas, gulfs, bays and continents that can be named, each with the point its name is set at.
3
+ *
4
+ * @example
5
+ * ```ts
6
+ * import { SEA_FEATURES } from "@johnmorrisdotca/chikyu-earth/realistic";
7
+ *
8
+ * console.log(SEA_FEATURES.find((one) => one.id === "africa")?.name.en);
9
+ * ```
10
+ */
11
+ export const SEA_FEATURES = [
12
+ { id: "arctic-ocean", kind: "ocean", lon: -178.50, lat: 87.71, name: { en: "Arctic Ocean", ja: "北極海" } },
13
+ { id: "southern-ocean", kind: "ocean", lon: -88.50, lat: -70.76, name: { en: "Southern Ocean", ja: "南極海" } },
14
+ { id: "atlantic-ocean", kind: "ocean", lon: -28.59, lat: -5.03, name: { en: "Atlantic Ocean", ja: "大西洋" } },
15
+ { id: "pacific-ocean", kind: "ocean", lon: -145.50, lat: -8.03, name: { en: "Pacific Ocean", ja: "太平洋" } },
16
+ { id: "indian-ocean", kind: "ocean", lon: 81.12, lat: -38.03, name: { en: "Indian Ocean", ja: "インド洋" } },
17
+ { id: "south-america", kind: "continent", lon: -59.34, lat: -19.88, name: { en: "South America", ja: "南アメリカ" } },
18
+ { id: "australia", kind: "continent", lon: 139.18, lat: -25.15, name: { en: "Australia", ja: "オーストラリア大陸" } },
19
+ { id: "africa", kind: "continent", lon: 16.50, lat: 7.21, name: { en: "Africa", ja: "アフリカ" } },
20
+ { id: "antarctica", kind: "continent", lon: 34.00, lat: -88.00, name: { en: "Antarctica", ja: "南極大陸" } },
21
+ { id: "asia", kind: "continent", lon: 96.10, lat: 47.33, name: { en: "Asia", ja: "アジア" } },
22
+ { id: "europe", kind: "continent", lon: 20.52, lat: 54.03, name: { en: "Europe", ja: "ヨーロッパ" } },
23
+ { id: "north-america", kind: "continent", lon: -102.09, lat: 61.65, name: { en: "North America", ja: "北アメリカ" } },
24
+ { id: "black-sea", kind: "sea", lon: 34.98, lat: 42.42, name: { en: "Black Sea", ja: "黒海" } },
25
+ { id: "philippine-sea", kind: "sea", lon: 134.32, lat: 19.12, name: { en: "Philippine Sea", ja: "フィリピン海" } },
26
+ { id: "tasman-sea", kind: "sea", lon: 159.86, lat: -40.29, name: { en: "Tasman Sea", ja: "タスマン海" } },
27
+ { id: "bay-of-bengal", kind: "bay", lon: 87.19, lat: 13.02, name: { en: "Bay of Bengal", ja: "ベンガル湾" } },
28
+ { id: "south-china-sea", kind: "sea", lon: 112.85, lat: 10.28, name: { en: "South China Sea", ja: "南シナ海" } },
29
+ { id: "arabian-sea", kind: "sea", lon: 64.53, lat: 12.82, name: { en: "Arabian Sea", ja: "アラビア海" } },
30
+ { id: "beaufort-sea", kind: "sea", lon: -137.07, lat: 70.71, name: { en: "Beaufort Sea", ja: "ボーフォート海" } },
31
+ { id: "caribbean-sea", kind: "sea", lon: -74.85, lat: 15.99, name: { en: "Caribbean Sea", ja: "カリブ海" } },
32
+ { id: "gulf-of-mexico", kind: "gulf", lon: -90.36, lat: 25.30, name: { en: "Gulf of Mexico", ja: "メキシコ湾" } },
33
+ { id: "labrador-sea", kind: "sea", lon: -54.27, lat: 54.96, name: { en: "Labrador Sea", ja: "ラブラドル海" } },
34
+ { id: "hudson-bay", kind: "bay", lon: -84.46, lat: 59.20, name: { en: "Hudson Bay", ja: "ハドソン湾" } },
35
+ { id: "caspian-sea", kind: "sea", lon: 51.21, lat: 41.11, name: { en: "Caspian Sea", ja: "カスピ海" } },
36
+ { id: "baffin-bay", kind: "bay", lon: -65.57, lat: 74.42, name: { en: "Baffin Bay", ja: "バフィン湾" } },
37
+ { id: "gulf-of-alaska", kind: "gulf", lon: -149.88, lat: 58.55, name: { en: "Gulf of Alaska", ja: "アラスカ湾" } },
38
+ { id: "red-sea", kind: "sea", lon: 38.33, lat: 22.96, name: { en: "Red Sea", ja: "紅海" } },
39
+ { id: "sea-of-okhotsk", kind: "sea", lon: 147.72, lat: 50.78, name: { en: "Sea of Okhotsk", ja: "オホーツク海" } },
40
+ { id: "weddell-sea", kind: "sea", lon: -46.28, lat: -75.96, name: { en: "Weddell Sea", ja: "ウェッデル海" } },
41
+ { id: "persian-gulf", kind: "gulf", lon: 52.22, lat: 25.47, name: { en: "Persian Gulf", ja: "ペルシア湾" } },
42
+ { id: "mediterranean-sea", kind: "sea", lon: 20.41, lat: 35.33, name: { en: "Mediterranean Sea", ja: "地中海" } },
43
+ { id: "ross-sea", kind: "sea", lon: -169.50, lat: -77.69, name: { en: "Ross Sea", ja: "ロス海" } },
44
+ { id: "coral-sea", kind: "sea", lon: 156.28, lat: -19.38, name: { en: "Coral Sea", ja: "珊瑚海" } },
45
+ { id: "sea-of-japan", kind: "sea", lon: 135.81, lat: 41.78, name: { en: "Sea of Japan", ja: "日本海" } },
46
+ ];
@@ -0,0 +1,121 @@
1
+ import type { CityFrame } from "../cities/geometry.ts";
2
+ import { SEA_FEATURES } from "./features.ts";
3
+ export { SEA_FEATURES };
4
+ /**
5
+ * One named place: an ocean, a sea, a gulf, a bay or a continent, with the point its name is set at.
6
+ *
7
+ * @example
8
+ * ```ts
9
+ * import type { SeaFeature } from "@johnmorrisdotca/chikyu-earth/realistic";
10
+ *
11
+ * const pacific: SeaFeature = { id: "pacific-ocean", kind: "ocean", lon: -145.5, lat: -8, name: { en: "Pacific Ocean", ja: "太平洋" } };
12
+ * console.log(pacific.name.en);
13
+ * ```
14
+ */
15
+ export interface SeaFeature {
16
+ id: string;
17
+ kind: "ocean" | "continent" | "sea" | "gulf" | "bay";
18
+ lon: number;
19
+ lat: number;
20
+ name: {
21
+ en: string;
22
+ ja: string;
23
+ };
24
+ }
25
+ /**
26
+ * A name placed for one draw: its id, the text, and the box it takes.
27
+ *
28
+ * @example
29
+ * ```ts
30
+ * import type { SeaName } from "@johnmorrisdotca/chikyu-earth/realistic";
31
+ *
32
+ * const say = (one: SeaName): string => one.text;
33
+ * console.log(typeof say);
34
+ * ```
35
+ */
36
+ export interface SeaName {
37
+ id: string;
38
+ kind: SeaFeature["kind"];
39
+ text: string;
40
+ left: number;
41
+ top: number;
42
+ width: number;
43
+ height: number;
44
+ size: number;
45
+ }
46
+ /**
47
+ * The Earth's radius, in CSS pixels, at which seas, gulfs and bays join the oceans and continents.
48
+ * Below this the view is the wide Earth: only oceans and continents are named.
49
+ *
50
+ * @example
51
+ * ```ts
52
+ * import { SEA_RADIUS } from "@johnmorrisdotca/chikyu-earth/realistic";
53
+ *
54
+ * console.log(SEA_RADIUS > 200);
55
+ * ```
56
+ */
57
+ export declare const SEA_RADIUS = 280;
58
+ /** Space between the letters of a water name, as a share of the text size. Continents use a little less. */
59
+ export declare const SEA_LETTER_SPACING = 0.22;
60
+ export declare const LAND_LETTER_SPACING = 0.06;
61
+ /**
62
+ * The names a view of this size may show: oceans and continents while the Earth is small, and seas, gulfs and bays once it is at least {@link SEA_RADIUS} pixels in radius.
63
+ *
64
+ * @param radius - The Earth's radius on the screen, in CSS pixels.
65
+ * @returns The features, oceans and continents first.
66
+ *
67
+ * @example
68
+ * ```ts
69
+ * import { seaNamesFor } from "@johnmorrisdotca/chikyu-earth/realistic";
70
+ *
71
+ * console.log(seaNamesFor(120).some((one) => one.id === "pacific-ocean"));
72
+ * console.log(seaNamesFor(120).some((one) => one.id === "sea-of-japan"));
73
+ * console.log(seaNamesFor(400).some((one) => one.id === "mediterranean-sea"));
74
+ * ```
75
+ */
76
+ export declare function seaNamesFor(radius: number): readonly SeaFeature[];
77
+ /**
78
+ * Reads the `sea-names` attribute. Absent or `off` is off. A bare attribute, or any other value, is on.
79
+ *
80
+ * @param value - The attribute's value, or null when it is absent.
81
+ * @returns Whether the names are on.
82
+ *
83
+ * @example
84
+ * ```ts
85
+ * import { readSeaNames } from "@johnmorrisdotca/chikyu-earth/realistic";
86
+ *
87
+ * console.log(readSeaNames(null), readSeaNames(""), readSeaNames("off"));
88
+ * ```
89
+ */
90
+ export declare function readSeaNames(value: string | null): boolean;
91
+ /** The attributes the elements watch for these names. */
92
+ export declare const SEA_NAMES_ATTRIBUTES: readonly ["sea-names"];
93
+ /**
94
+ * Lays the names out for one draw. A name on the far side, or off the Earth's disc, is left out. A box in `taken` (a city name, a place, a country) keeps its space: the name moves aside, and if the only room is taken it is left out, so a city that was placed first wins.
95
+ *
96
+ * @param radius - The Earth's radius on the screen. It chooses oceans and continents, or those plus the seas.
97
+ * @param frame - Where the Earth is, and how to project a place. `project` returns how squarely the place faces the viewer.
98
+ * @param lang - `en` or `ja`.
99
+ * @param taken - Boxes already used. A name is not put over one.
100
+ * @returns The names to show.
101
+ *
102
+ * @example
103
+ * ```ts
104
+ * import { layoutSeaNames } from "@johnmorrisdotca/chikyu-earth/realistic";
105
+ *
106
+ * const frame = { width: 400, height: 400, radius: 160, project: (lon: number, lat: number, out: { x: number; y: number }) => { out.x = 200 + lon; out.y = 200 - lat; return 1; } };
107
+ * console.log(layoutSeaNames(160, frame, "en", []).some((one) => one.id === "africa"));
108
+ * ```
109
+ */
110
+ export declare function layoutSeaNames(radius: number, frame: CityFrame & {
111
+ disc?: {
112
+ x: number;
113
+ y: number;
114
+ radius: number;
115
+ };
116
+ }, lang: "en" | "ja", taken: readonly {
117
+ left: number;
118
+ top: number;
119
+ width: number;
120
+ height: number;
121
+ }[]): SeaName[];
@@ -0,0 +1,135 @@
1
+ import { nameWidth } from "../cities/geometry.js";
2
+ import { SEA_FEATURES } from "./features.js";
3
+ export { SEA_FEATURES };
4
+ /**
5
+ * The Earth's radius, in CSS pixels, at which seas, gulfs and bays join the oceans and continents.
6
+ * Below this the view is the wide Earth: only oceans and continents are named.
7
+ *
8
+ * @example
9
+ * ```ts
10
+ * import { SEA_RADIUS } from "@johnmorrisdotca/chikyu-earth/realistic";
11
+ *
12
+ * console.log(SEA_RADIUS > 200);
13
+ * ```
14
+ */
15
+ export const SEA_RADIUS = 280;
16
+ /** Space between the letters of a water name, as a share of the text size. Continents use a little less. */
17
+ export const SEA_LETTER_SPACING = 0.22;
18
+ export const LAND_LETTER_SPACING = 0.06;
19
+ const WATER = new Set(["ocean", "sea", "gulf", "bay"]);
20
+ const FACING = 0.25;
21
+ const MAX = 18;
22
+ /**
23
+ * The names a view of this size may show: oceans and continents while the Earth is small, and seas, gulfs and bays once it is at least {@link SEA_RADIUS} pixels in radius.
24
+ *
25
+ * @param radius - The Earth's radius on the screen, in CSS pixels.
26
+ * @returns The features, oceans and continents first.
27
+ *
28
+ * @example
29
+ * ```ts
30
+ * import { seaNamesFor } from "@johnmorrisdotca/chikyu-earth/realistic";
31
+ *
32
+ * console.log(seaNamesFor(120).some((one) => one.id === "pacific-ocean"));
33
+ * console.log(seaNamesFor(120).some((one) => one.id === "sea-of-japan"));
34
+ * console.log(seaNamesFor(400).some((one) => one.id === "mediterranean-sea"));
35
+ * ```
36
+ */
37
+ export function seaNamesFor(radius) {
38
+ const wide = SEA_FEATURES.filter((one) => one.kind === "ocean" || one.kind === "continent");
39
+ if (radius < SEA_RADIUS)
40
+ return wide;
41
+ return SEA_FEATURES;
42
+ }
43
+ /**
44
+ * Reads the `sea-names` attribute. Absent or `off` is off. A bare attribute, or any other value, is on.
45
+ *
46
+ * @param value - The attribute's value, or null when it is absent.
47
+ * @returns Whether the names are on.
48
+ *
49
+ * @example
50
+ * ```ts
51
+ * import { readSeaNames } from "@johnmorrisdotca/chikyu-earth/realistic";
52
+ *
53
+ * console.log(readSeaNames(null), readSeaNames(""), readSeaNames("off"));
54
+ * ```
55
+ */
56
+ export function readSeaNames(value) {
57
+ return value !== null && value !== "off";
58
+ }
59
+ /** The attributes the elements watch for these names. */
60
+ export const SEA_NAMES_ATTRIBUTES = ["sea-names"];
61
+ const spacingOf = (kind) => (WATER.has(kind) ? SEA_LETTER_SPACING : LAND_LETTER_SPACING);
62
+ const boxOf = (text, size, spacing) => {
63
+ let width = 0;
64
+ for (const char of text)
65
+ width += ((char.codePointAt(0) ?? 0) >= 0x2e80 ? size : size * 0.58) + size * spacing;
66
+ return { width: Math.ceil(Math.max(width, nameWidth(text, size))) + 4, height: Math.ceil(size * 1.35) };
67
+ };
68
+ /**
69
+ * Lays the names out for one draw. A name on the far side, or off the Earth's disc, is left out. A box in `taken` (a city name, a place, a country) keeps its space: the name moves aside, and if the only room is taken it is left out, so a city that was placed first wins.
70
+ *
71
+ * @param radius - The Earth's radius on the screen. It chooses oceans and continents, or those plus the seas.
72
+ * @param frame - Where the Earth is, and how to project a place. `project` returns how squarely the place faces the viewer.
73
+ * @param lang - `en` or `ja`.
74
+ * @param taken - Boxes already used. A name is not put over one.
75
+ * @returns The names to show.
76
+ *
77
+ * @example
78
+ * ```ts
79
+ * import { layoutSeaNames } from "@johnmorrisdotca/chikyu-earth/realistic";
80
+ *
81
+ * const frame = { width: 400, height: 400, radius: 160, project: (lon: number, lat: number, out: { x: number; y: number }) => { out.x = 200 + lon; out.y = 200 - lat; return 1; } };
82
+ * console.log(layoutSeaNames(160, frame, "en", []).some((one) => one.id === "africa"));
83
+ * ```
84
+ */
85
+ export function layoutSeaNames(radius, frame, lang, taken) {
86
+ const out = { x: 0, y: 0 };
87
+ const disc = frame.disc;
88
+ const blocked = taken.map((box) => ({ l: box.left, t: box.top, r: box.left + box.width, b: box.top + box.height }));
89
+ const placed = [];
90
+ const hits = (box, others) => {
91
+ for (const other of others)
92
+ if (box.l < other.r && box.r > other.l && box.t < other.b && box.b > other.t)
93
+ return true;
94
+ return false;
95
+ };
96
+ const inDisc = (box) => {
97
+ if (!disc)
98
+ return true;
99
+ const reach = disc.radius - 2;
100
+ return Math.hypot(box.l - disc.x, box.t - disc.y) <= reach && Math.hypot(box.r - disc.x, box.t - disc.y) <= reach && Math.hypot(box.l - disc.x, box.b - disc.y) <= reach && Math.hypot(box.r - disc.x, box.b - disc.y) <= reach;
101
+ };
102
+ const names = [];
103
+ const features = [...seaNamesFor(radius)].sort((a, b) => rank(b) - rank(a));
104
+ for (const feature of features) {
105
+ if (names.length >= MAX)
106
+ break;
107
+ const facing = frame.project(feature.lon, feature.lat, out);
108
+ if (facing < FACING)
109
+ continue;
110
+ const text = lang === "ja" ? feature.name.ja : feature.name.en;
111
+ const size = feature.kind === "ocean" || feature.kind === "continent" ? 14 : 12;
112
+ const { width, height } = boxOf(text, size, spacingOf(feature.kind));
113
+ const shifts = [
114
+ [-width / 2, -height / 2],
115
+ [8, -height / 2],
116
+ [-width - 8, -height / 2],
117
+ [-width / 2, -height - 6],
118
+ [-width / 2, 8],
119
+ ];
120
+ for (const [dx, dy] of shifts) {
121
+ const left = out.x + dx;
122
+ const top = out.y + dy;
123
+ const box = { l: left, t: top, r: left + width, b: top + height };
124
+ if (box.l < 0 || box.t < 0 || box.r > frame.width || box.b > frame.height)
125
+ continue;
126
+ if (!inDisc(box) || hits(box, blocked) || hits(box, placed))
127
+ continue;
128
+ placed.push(box);
129
+ names.push({ id: feature.id, kind: feature.kind, text, left, top, width, height, size });
130
+ break;
131
+ }
132
+ }
133
+ return names;
134
+ }
135
+ const rank = (feature) => (feature.kind === "sea" || feature.kind === "gulf" || feature.kind === "bay" ? 3 : feature.kind === "continent" ? 2 : 1);
@@ -0,0 +1,46 @@
1
+ import { type SeaName } from "./geometry.ts";
2
+ import type { CityFrame } from "../cities/geometry.ts";
3
+ /** The styles of the layer (`#sea-names` in the element's shadow root). Water is spaced out; continents a little less. */
4
+ export declare const SEA_NAMES_STYLE: string;
5
+ export interface SeaNamesStats {
6
+ on: boolean;
7
+ radius: number;
8
+ names: number;
9
+ renders: number;
10
+ }
11
+ export interface SeaHost {
12
+ frame(frame: (CityFrame & {
13
+ disc?: {
14
+ x: number;
15
+ y: number;
16
+ radius: number;
17
+ };
18
+ }) | null, taken?: readonly {
19
+ left: number;
20
+ top: number;
21
+ width: number;
22
+ height: number;
23
+ }[]): void;
24
+ refresh(): void;
25
+ readonly shown: readonly SeaName[];
26
+ readonly boxes: readonly {
27
+ left: number;
28
+ top: number;
29
+ width: number;
30
+ height: number;
31
+ }[];
32
+ readonly stats: SeaNamesStats;
33
+ dispose(): void;
34
+ }
35
+ export interface SeaHostOptions {
36
+ attribute(name: string): string | null;
37
+ language(): "en" | "ja";
38
+ root: ShadowRoot;
39
+ }
40
+ /**
41
+ * Makes the layer of one element. Nothing is added to the root until `sea-names` is on.
42
+ *
43
+ * @param options - The attribute reader, the language and the shadow root.
44
+ * @returns The host.
45
+ */
46
+ export declare function createSeaHost(options: SeaHostOptions): SeaHost;
@@ -0,0 +1,111 @@
1
+ // Names of oceans, seas and continents over an element's picture. The element tells this where the Earth is at each draw. Nothing is added until `sea-names` is on, and nothing is drawn at rest.
2
+ import { LAND_LETTER_SPACING, SEA_LETTER_SPACING, layoutSeaNames, readSeaNames } from "./geometry.js";
3
+ /** The styles of the layer (`#sea-names` in the element's shadow root). Water is spaced out; continents a little less. */
4
+ export const SEA_NAMES_STYLE = "#sea-names{position:absolute;inset:0;overflow:hidden;pointer-events:none}" +
5
+ ".sea-name{position:absolute;left:0;top:0;margin:0;white-space:nowrap;text-align:center;pointer-events:none;will-change:transform;" +
6
+ "font-family:var(--chikyu-sea-font,system-ui,-apple-system,'Segoe UI',sans-serif);font-weight:500;color:var(--chikyu-sea-colour,rgba(186,214,228,.9));" +
7
+ `letter-spacing:${SEA_LETTER_SPACING}em;` +
8
+ "text-shadow:0 0 3px #061018,0 0 3px #061018}" +
9
+ `.sea-name[data-kind=continent]{letter-spacing:${LAND_LETTER_SPACING}em;color:var(--chikyu-land-colour,rgba(232,224,200,.92))}` +
10
+ ".sea-name[hidden]{display:none}";
11
+ /**
12
+ * Makes the layer of one element. Nothing is added to the root until `sea-names` is on.
13
+ *
14
+ * @param options - The attribute reader, the language and the shadow root.
15
+ * @returns The host.
16
+ */
17
+ export function createSeaHost(options) {
18
+ const { root } = options;
19
+ const doc = root.ownerDocument;
20
+ let on = readSeaNames(options.attribute("sea-names"));
21
+ let last = null;
22
+ let taken = [];
23
+ let shown = [];
24
+ let renders = 0;
25
+ let box;
26
+ const pool = [];
27
+ const attach = () => {
28
+ if (box)
29
+ return;
30
+ box = doc.createElement("div");
31
+ box.id = "sea-names";
32
+ box.setAttribute("aria-hidden", "true");
33
+ const cities = root.getElementById("cn-canvas");
34
+ if (cities)
35
+ root.insertBefore(box, cities);
36
+ else
37
+ root.append(box);
38
+ };
39
+ const clear = () => {
40
+ for (const name of pool)
41
+ name.hidden = true;
42
+ shown = [];
43
+ };
44
+ const place = () => {
45
+ if (!box)
46
+ return;
47
+ renders += 1;
48
+ while (pool.length < shown.length) {
49
+ const name = doc.createElement("span");
50
+ name.className = "sea-name";
51
+ box.append(name);
52
+ pool.push(name);
53
+ }
54
+ pool.forEach((name, index) => {
55
+ const one = shown[index];
56
+ if (!one) {
57
+ name.hidden = true;
58
+ return;
59
+ }
60
+ name.hidden = false;
61
+ name.dataset["kind"] = one.kind;
62
+ name.textContent = one.text;
63
+ name.style.fontSize = `${one.size}px`;
64
+ name.style.transform = `translate(${one.left}px, ${one.top}px)`;
65
+ });
66
+ };
67
+ const compute = () => {
68
+ if (!on || !last) {
69
+ clear();
70
+ if (box)
71
+ place();
72
+ return;
73
+ }
74
+ attach();
75
+ shown = layoutSeaNames(last.radius, last, options.language(), taken);
76
+ place();
77
+ };
78
+ return {
79
+ frame(frame, next = []) {
80
+ last = frame;
81
+ taken = next;
82
+ compute();
83
+ },
84
+ refresh() {
85
+ on = readSeaNames(options.attribute("sea-names"));
86
+ if (!on) {
87
+ clear();
88
+ box?.remove();
89
+ box = undefined;
90
+ pool.length = 0;
91
+ return;
92
+ }
93
+ compute();
94
+ },
95
+ get shown() {
96
+ return shown;
97
+ },
98
+ get boxes() {
99
+ return shown.map((one) => ({ left: one.left, top: one.top, width: one.width, height: one.height }));
100
+ },
101
+ get stats() {
102
+ return { on, radius: last?.radius ?? 0, names: on ? shown.length : 0, renders };
103
+ },
104
+ dispose() {
105
+ clear();
106
+ box?.remove();
107
+ box = undefined;
108
+ on = false;
109
+ },
110
+ };
111
+ }