@johnmorrisdotca/chikyu-earth 0.33.1 → 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.
Files changed (48) hide show
  1. package/CHANGELOG.md +19 -0
  2. package/README.md +23 -5
  3. package/dist/cities/geometry.d.ts +233 -0
  4. package/dist/cities/geometry.js +174 -0
  5. package/dist/cities/host.d.ts +73 -0
  6. package/dist/cities/host.js +240 -0
  7. package/dist/element.d.ts +1 -1
  8. package/dist/element.js +2 -13
  9. package/dist/markers/index.d.ts +1 -0
  10. package/dist/markers/index.js +1 -0
  11. package/dist/markers/labels.d.ts +7 -1
  12. package/dist/markers/labels.js +12 -1
  13. package/dist/markers/place-layer.d.ts +43 -0
  14. package/dist/markers/place-layer.js +238 -0
  15. package/dist/markers/places.d.ts +279 -0
  16. package/dist/markers/places.js +213 -0
  17. package/dist/passes/element.js +2 -5
  18. package/dist/realistic/body-label.js +19 -11
  19. package/dist/realistic/earth.d.ts +1 -1
  20. package/dist/realistic/element.d.ts +45 -5
  21. package/dist/realistic/element.js +96 -13
  22. package/dist/realistic/index.d.ts +3 -0
  23. package/dist/realistic/index.js +2 -0
  24. package/dist/satellites/index.d.ts +1 -0
  25. package/dist/satellites/index.js +1 -0
  26. package/dist/satellites/names.d.ts +14 -0
  27. package/dist/satellites/names.js +16 -0
  28. package/dist/sky/cover.d.ts +27 -0
  29. package/dist/sky/cover.js +32 -0
  30. package/dist/sky/frame.d.ts +21 -0
  31. package/dist/sky/frame.js +2 -0
  32. package/dist/sky/host.d.ts +10 -15
  33. package/dist/sky/host.js +37 -3
  34. package/dist/sky/style.d.ts +50 -0
  35. package/dist/sky/style.js +2 -0
  36. package/dist/space/element.d.ts +116 -8
  37. package/dist/space/element.js +253 -8
  38. package/dist/space/index.d.ts +4 -2
  39. package/dist/space/index.js +2 -1
  40. package/dist/space/ride.d.ts +60 -4
  41. package/dist/space/ride.js +78 -6
  42. package/dist/space/satellite-layer.d.ts +26 -0
  43. package/dist/space/satellite-layer.js +37 -1
  44. package/dist/space/satellite-model.d.ts +72 -0
  45. package/dist/space/satellite-model.js +82 -0
  46. package/dist/space/scene.d.ts +47 -2
  47. package/dist/space/scene.js +169 -1
  48. package/package.json +8 -4
@@ -15,11 +15,15 @@ import { folderSource } from "../door/map.js";
15
15
  import { ownCredit } from "../door/own.js";
16
16
  import { BODY_LABEL_ATTRIBUTES, bodyLabelText, loadMichroma, readBodyLabel } from "./body-label.js";
17
17
  import { createSkyHost, SKY_ATTRIBUTES, SKY_STYLE } from "../sky/host.js";
18
+ import { cleanPlaces, orthoView, readPlaces } from "../markers/places.js";
19
+ import { createPlaceLayer, PLACE_STYLE, readPlaceLook } from "../markers/place-layer.js";
20
+ import { CITY_NAMES_ATTRIBUTES, CITY_NAMES_STYLE, createCityHost } from "../cities/host.js";
21
+ import { project } from "../markers/project.js";
18
22
  import { createGate, frameCap } from "./gate.js";
19
23
  import { setSharedRenderer, setThreeLoader, takeRenderer } from "./shared.js";
20
24
  export { setSharedRenderer, setThreeLoader };
21
25
  import { MAX_TEXTURE, TIERS } from "./tiers.js";
22
- const STYLE = ":host{display:block;aspect-ratio:1/1;position:relative}:host([immersive]){aspect-ratio:auto;width:100%;height:100%;touch-action:none!important;user-select:none;-webkit-user-select:none;-webkit-tap-highlight-color:transparent}#stage,#overlay{position:absolute;inset:0}#overlay{pointer-events:none}" + SKY_STYLE + METEOR_NOTE_STYLE + NOTES_STYLE;
26
+ const STYLE = ":host{display:block;aspect-ratio:1/1;position:relative}:host([immersive]){aspect-ratio:auto;width:100%;height:100%;touch-action:none!important;user-select:none;-webkit-user-select:none;-webkit-tap-highlight-color:transparent}#stage,#overlay{position:absolute;inset:0}#overlay{pointer-events:none}" + PLACE_STYLE + SKY_STYLE + CITY_NAMES_STYLE + METEOR_NOTE_STYLE + NOTES_STYLE;
23
27
  const RAD = Math.PI / 180;
24
28
  /** The Sun and the clouds are re-placed this often at real time: 15 degrees an hour is 0.25 degree a minute. */
25
29
  const LIVE_TICK_MS = 60_000;
@@ -130,7 +134,7 @@ export const readTime = (value) => {
130
134
  * ```
131
135
  */
132
136
  export class ChikyuEarthRealistic extends HTMLElement {
133
- static observedAttributes = ["cloud-drift", "center", "time", "speed", "clouds", "lights", "light", "live-seconds", "tiles", "data", "camera", "immersive", "meteors", "meteors-label", "meteors-strength", ...BODY_LABEL_ATTRIBUTES, ...SKY_ATTRIBUTES, "air-thickness", "air-label", "lang"];
137
+ static observedAttributes = ["cloud-drift", "center", "time", "speed", "clouds", "lights", "light", "live-seconds", "tiles", "data", "camera", "immersive", "meteors", "meteors-label", "meteors-strength", ...BODY_LABEL_ATTRIBUTES, ...SKY_ATTRIBUTES, ...CITY_NAMES_ATTRIBUTES, "air-thickness", "air-label", "lang", "places", "places-look"];
134
138
  #model = newModel();
135
139
  #mode = "now";
136
140
  #view;
@@ -154,7 +158,12 @@ export class ChikyuEarthRealistic extends HTMLElement {
154
158
  #stage;
155
159
  #overlay;
156
160
  #pose = null;
161
+ /** The names of cities on the Earth (`city-names`), and the look of the names. */
162
+ #cities;
163
+ #citiesStyle = {};
157
164
  #listeners = new Set();
165
+ #placeLayer;
166
+ #placeList = [];
158
167
  #hands;
159
168
  #reach = null;
160
169
  /** The page has set a `speed` since the element came into the page: for a reader who prefers reduced motion, that is the Play that lets meteors start. */
@@ -191,7 +200,7 @@ export class ChikyuEarthRealistic extends HTMLElement {
191
200
  if (!this.hasAttribute("aria-label"))
192
201
  this.setAttribute("aria-label", "The Earth from space, with its sunlit and dark sides");
193
202
  const root = this.shadowRoot ?? this.attachShadow({ mode: "open" });
194
- root.innerHTML = `<div id="stage"></div><div id="overlay"></div><div id="notes"><p id="air-note" hidden></p><p id="meteor-note" hidden></p></div>`;
203
+ root.innerHTML = `<div id="stage"></div><div id="places"></div><div id="overlay"></div><div id="notes"><p id="air-note" hidden></p><p id="meteor-note" hidden></p></div>`;
195
204
  adoptStyles(root, STYLE, this.nonce);
196
205
  this.#stage = root.getElementById("stage");
197
206
  this.#overlay = root.getElementById("overlay");
@@ -205,6 +214,17 @@ export class ChikyuEarthRealistic extends HTMLElement {
205
214
  loadFont: loadMichroma,
206
215
  });
207
216
  this.#sky.setStyle(this.#skyStyle);
217
+ this.#placeLayer = createPlaceLayer(root.getElementById("places"));
218
+ this.#placeList = readPlaces(this.getAttribute("places"));
219
+ this.#placeLayer.set(this.#placeList);
220
+ this.#placeLayer.look = readPlaceLook(this.getAttribute("places-look"));
221
+ this.#cities = createCityHost({
222
+ root,
223
+ attribute: (name) => this.getAttribute(name),
224
+ language: () => ((this.closest("[lang]")?.getAttribute("lang") ?? "").toLowerCase().startsWith("ja") ? "ja" : "en"),
225
+ onError: (error) => this.dispatchEvent(new CustomEvent("chikyu-city-names-unavailable", { detail: { reason: error instanceof Error ? error.message : String(error) } })),
226
+ });
227
+ this.#cities.setStyle(this.#citiesStyle);
208
228
  this.#played = false;
209
229
  this.#model.meteors.on = this.#meteorsAsked();
210
230
  this.#model.meteors.strength = readMeteorStrength(this.getAttribute("meteors-strength"));
@@ -252,6 +272,9 @@ export class ChikyuEarthRealistic extends HTMLElement {
252
272
  this.#pose = { ...pose, transform: "" };
253
273
  for (const listener of this.#listeners)
254
274
  listener(this.#pose);
275
+ if (this.#placeLayer && (this.#placeList.length > 0 || this.#placeLayer.stats.shown > 0))
276
+ this.#placeLayer.frame(orthoView(pose));
277
+ this.#cityFrame();
255
278
  },
256
279
  onSatellitesError: (error) => this.dispatchEvent(new CustomEvent("chikyu-satellites-unavailable", { detail: { reason: error instanceof Error ? error.message : String(error) } })),
257
280
  onError: (error) => this.#fail(error instanceof Error ? error.message : String(error)),
@@ -279,7 +302,6 @@ export class ChikyuEarthRealistic extends HTMLElement {
279
302
  this.#hands = carryControls(this, { initial: this.#model.view, limits: LIMITS, onChange: (state, info) => this.setView(state, info.final) });
280
303
  this.#startTick();
281
304
  document.addEventListener("visibilitychange", this.#onVisible);
282
- this.#locate();
283
305
  }
284
306
  disconnectedCallback() {
285
307
  this.removeEventListener("pointerdown", this.#take);
@@ -303,6 +325,10 @@ export class ChikyuEarthRealistic extends HTMLElement {
303
325
  this.#taken = undefined;
304
326
  this.#sky?.dispose();
305
327
  this.#sky = undefined;
328
+ this.#placeLayer?.dispose();
329
+ this.#placeLayer = undefined;
330
+ this.#cities?.dispose();
331
+ this.#cities = undefined;
306
332
  }
307
333
  attributeChangedCallback(name, _old, value) {
308
334
  if (name === "center")
@@ -344,12 +370,24 @@ export class ChikyuEarthRealistic extends HTMLElement {
344
370
  if (name === "lang") {
345
371
  this.#applyBodyLabel();
346
372
  this.#sky?.refresh();
373
+ this.#cities?.refresh();
347
374
  }
348
375
  }
349
376
  else if (BODY_LABEL_ATTRIBUTES.includes(name))
350
377
  this.#applyBodyLabel();
351
378
  else if (SKY_ATTRIBUTES.includes(name))
352
379
  this.#sky?.refresh();
380
+ else if (name === "places") {
381
+ this.#placeList = readPlaces(value);
382
+ this.#placeLayer?.set(this.#placeList);
383
+ this.#gate?.soon();
384
+ }
385
+ else if (name === "places-look" && this.#placeLayer) {
386
+ this.#placeLayer.look = readPlaceLook(value);
387
+ this.#gate?.soon();
388
+ }
389
+ else if (CITY_NAMES_ATTRIBUTES.includes(name))
390
+ this.#cities?.refresh();
353
391
  if (name === "air-thickness" || name === "air-label" || name === "lang")
354
392
  setAirNote(this.#airNote, this, this.#model.airThickness);
355
393
  }
@@ -431,7 +469,7 @@ export class ChikyuEarthRealistic extends HTMLElement {
431
469
  /**
432
470
  * The names of the brightest stars and the constellations as faint lines, laid over the sky the page draws (hoshi-stars' `sky-labels`, an optional peer loaded only when one of these is set). The plain attributes are hoshi's: `star-names` (a count of the brightest stars in view and not covered by the
433
471
  * Earth, the Moon or the Sun; bare is 10), `star-names-magnitude` (only stars at least that bright) and `constellation-lines` (the 88 figures as lines at this strength, 0 to 1; bare is 0.16): none of them, and nothing is loaded. The figure under the pointer or touched is at full strength and shows its name. Names are in the
434
- * page's language and never overlap. Nothing is redrawn at rest. `chikyu-sky-unavailable` is sent when hoshi-stars is not there. The property is the look of the names, a partial hoshi-stars `LabelStyle` (font, size, case, ring, leader and the rest).
472
+ * page's language and never overlap. Nothing is redrawn at rest. `chikyu-sky-unavailable` is sent when hoshi-stars is not there. The property is the look of the names, a partial `SkyLabelStyle` (font, size, case, ring, leader and the rest).
435
473
  */
436
474
  get skyStyle() {
437
475
  return { ...this.#skyStyle };
@@ -440,6 +478,56 @@ export class ChikyuEarthRealistic extends HTMLElement {
440
478
  this.#skyStyle = { ...value };
441
479
  this.#sky?.setStyle(this.#skyStyle);
442
480
  }
481
+ /**
482
+ * Places marked on the Earth (the attribute `places`, a JSON array): each `{ id, lon, lat, label, kind }`, `kind` being `here` (where the reader is: drawn bright and taller, with a quiet pulse) or `favourite`
483
+ * (the default: quieter). Each stands up out of the ground, turns with the Earth, is hidden on its far side and keeps its size in pixels at any zoom. Names go at the top, `here` first, and never overlap
484
+ * one another; one with no room is left out. The element stores nothing and sends nothing: the page hands the places over and keeps them where it likes. Setting the property writes the attribute; at most
485
+ * `MAX_PLACES` are drawn. Nothing is drawn again at rest: the pulse is the page's own animation, stopped out of sight and for a reader who asked for less motion.
486
+ */
487
+ get places() {
488
+ return this.#placeList.map((place) => ({ ...place }));
489
+ }
490
+ set places(value) {
491
+ const clean = cleanPlaces(value);
492
+ if (clean.length === 0)
493
+ this.removeAttribute("places");
494
+ else
495
+ this.setAttribute("places", JSON.stringify(clean));
496
+ }
497
+ /** How the places are drawn (the attribute `places-look`): `beam` (a light rising out of the ground with a ring at its foot, the default), `pin` (a thin stick with a round head) or `ring` (only the ring on the ground). */
498
+ get placesLook() {
499
+ return this.#placeLayer?.look ?? readPlaceLook(this.getAttribute("places-look"));
500
+ }
501
+ set placesLook(value) {
502
+ this.setAttribute("places-look", value);
503
+ }
504
+ /**
505
+ * The look of the names of cities (`city-names`): font, size, weight, colour, the outline round the text, and the dot's colour and size. Any of them may be left out; the Earth's size, the language and the data are not set here.
506
+ */
507
+ get cityNamesStyle() {
508
+ return { ...this.#citiesStyle };
509
+ }
510
+ set cityNamesStyle(value) {
511
+ this.#citiesStyle = { ...value };
512
+ this.#cities?.setStyle(this.#citiesStyle);
513
+ }
514
+ /** Tells the names of cities where the Earth is in the picture just drawn: the orthographic view the markers share. */
515
+ #cityFrame() {
516
+ const pose = this.#pose;
517
+ if (!this.#cities || !pose)
518
+ return;
519
+ this.#cities.frame({
520
+ width: pose.width,
521
+ height: pose.height,
522
+ radius: pose.radius,
523
+ project: (lon, lat, out) => {
524
+ const at = project(lon, lat, pose);
525
+ out.x = at.x;
526
+ out.y = at.y;
527
+ return at.z;
528
+ },
529
+ });
530
+ }
443
531
  /** Puts the name's settings and text right from the attributes (`body-label` and its family, `lang`). */
444
532
  #applyBodyLabel() {
445
533
  this.#model.bodyLabel.settings = readBodyLabel((name) => this.getAttribute(name));
@@ -625,7 +713,9 @@ export class ChikyuEarthRealistic extends HTMLElement {
625
713
  assets: { entries: stats?.assets.entries ?? 0, refs: stats?.assets.refs ?? 0 },
626
714
  meteors: { on: this.#model.meteors.on, strength: this.#model.meteors.strength, ...this.#model.meteors.drawn },
627
715
  bodyLabel: { on: this.#model.bodyLabel.settings !== null, text: this.#model.bodyLabel.text, ...this.#model.bodyLabel.drawn },
628
- sky: this.#sky?.stats ?? { on: false, loaded: false, names: 0, figures: 0, picked: null, renders: 0, lang: "" },
716
+ sky: this.#sky?.stats ?? { on: false, loaded: false, names: 0, figures: 0, picked: null, renders: 0, lang: "", stars: [] },
717
+ places: { ...(this.#placeLayer?.stats ?? { places: 0, shown: 0, labels: 0, renders: 0 }) },
718
+ cityNames: this.#cities?.stats ?? { on: false, max: 0, radius: 0, tiers: [], cities: 0, names: 0, renders: 0 },
629
719
  };
630
720
  }
631
721
  /**
@@ -842,13 +932,6 @@ export class ChikyuEarthRealistic extends HTMLElement {
842
932
  this.#set("unavailable");
843
933
  this.dispatchEvent(new CustomEvent("chikyu-unavailable", { detail: { reason } }));
844
934
  }
845
- #locate() {
846
- if (readCentre(this.getAttribute("center")) || typeof navigator === "undefined" || !navigator.geolocation)
847
- return;
848
- navigator.geolocation.getCurrentPosition((position) => {
849
- this.centre = { lon: position.coords.longitude, lat: position.coords.latitude };
850
- }, () => undefined, { timeout: 8000, maximumAge: 600000 });
851
- }
852
935
  }
853
936
  if (typeof customElements !== "undefined" && !customElements.get("chikyu-earth-realistic")) {
854
937
  customElements.define("chikyu-earth-realistic", ChikyuEarthRealistic);
@@ -8,3 +8,6 @@ export { TILE_KEEP_MS, TILE_LIMIT } from "./tile-set.ts";
8
8
  export { BODY_LABEL_ATTRIBUTES, bodyLabelText, readBodyLabel, loadMichroma, setBodyLabelFontUrl, type BodyLabelSettings } from "./body-label.ts";
9
9
  export { AIR_QUALITY, AIR_THICKNESS_DEFAULT, AIR_THICKNESS_MAX, AIR_TOP, AIR_VERT, airFor, airFrag, airTop, readAirThickness, type AirSamples } from "./air.ts";
10
10
  export { AIR_NOTE } from "./air-note.ts";
11
+ export type { SkyLabelStyle } from "../sky/style.ts";
12
+ export { CITY_NAMES_DEFAULT, CITY_NAMES_MAX, TIER_RADIUS, cityNameText, cityRank, cityTiersFor, layoutCities, nameWidth, readCityNames, type CityFrame, type CityLayoutOptions, type CityName, type CityNameTier, type NamedCity } from "../cities/geometry.ts";
13
+ export { CITY_NAMES_ATTRIBUTES, type CityNamesStyle } from "../cities/host.ts";
@@ -9,3 +9,5 @@ export { TILE_KEEP_MS, TILE_LIMIT } from "./tile-set.js";
9
9
  export { BODY_LABEL_ATTRIBUTES, bodyLabelText, readBodyLabel, loadMichroma, setBodyLabelFontUrl } from "./body-label.js";
10
10
  export { AIR_QUALITY, AIR_THICKNESS_DEFAULT, AIR_THICKNESS_MAX, AIR_TOP, AIR_VERT, airFor, airFrag, airTop, readAirThickness } from "./air.js";
11
11
  export { AIR_NOTE } from "./air-note.js";
12
+ export { CITY_NAMES_DEFAULT, CITY_NAMES_MAX, TIER_RADIUS, cityNameText, cityRank, cityTiersFor, layoutCities, nameWidth, readCityNames } from "../cities/geometry.js";
13
+ export { CITY_NAMES_ATTRIBUTES } from "../cities/host.js";
@@ -1,6 +1,7 @@
1
1
  export { createSatellites, type SatellitesOptions } from "./track.ts";
2
2
  export { DEFAULT_DIM_AFTER_DAYS, DEFAULT_MAX_AGE_DAYS, MAX_SATELLITE_SPEED } from "./limits.ts";
3
3
  export type { ElementSet, RefusedElementSet, SatelliteInfo, SatelliteRef, Satellites } from "./types.ts";
4
+ export { SHORT_NAMES } from "./names.ts";
4
5
  export { CATALOGUE, SATELLITE_GROUPS, catalogueEntry, findInCatalogue, type CatalogueEntry, type OrbitKind, type SatelliteGroupInfo } from "./catalogue.ts";
5
6
  export { ElementSourceError, fetchElements, forgetElements, parseElements, type ElementFormat, type ElementSource, type FetchElementsOptions, type ParsedElements } from "./source.ts";
6
7
  export { missionElements, missionOf, missionPosition, missions, type HubbleVisit, type Mission, type MissionFile, type MissionPosition, type Orbiter } from "./missions.ts";
@@ -1,6 +1,7 @@
1
1
  // The satellites' pure parts: orbital elements in, positions out (SGP4 by satellite.js, an optional peer). They import on a server. The space view takes a group with `setSatellites`.
2
2
  export { createSatellites } from "./track.js";
3
3
  export { DEFAULT_DIM_AFTER_DAYS, DEFAULT_MAX_AGE_DAYS, MAX_SATELLITE_SPEED } from "./limits.js";
4
+ export { SHORT_NAMES } from "./names.js";
4
5
  export { CATALOGUE, SATELLITE_GROUPS, catalogueEntry, findInCatalogue } from "./catalogue.js";
5
6
  export { ElementSourceError, fetchElements, forgetElements, parseElements } from "./source.js";
6
7
  export { missionElements, missionOf, missionPosition, missions } from "./missions.js";
@@ -0,0 +1,14 @@
1
+ /**
2
+ * The short names of the satellites the package knows by name, by NORAD number, in English and in Japanese: the ISS, Tiangong and Hubble.
3
+ *
4
+ * @example
5
+ * ```ts
6
+ * import { SHORT_NAMES } from "@johnmorrisdotca/chikyu-earth/satellites";
7
+ *
8
+ * console.log(SHORT_NAMES[48274]?.ja);
9
+ * ```
10
+ */
11
+ export declare const SHORT_NAMES: Readonly<Record<number, {
12
+ en: string;
13
+ ja: string;
14
+ }>>;
@@ -0,0 +1,16 @@
1
+ // The short names of the large satellites the package knows, in each language, for a label or a list: any other satellite is shown under the name in its elements. Pure.
2
+ /**
3
+ * The short names of the satellites the package knows by name, by NORAD number, in English and in Japanese: the ISS, Tiangong and Hubble.
4
+ *
5
+ * @example
6
+ * ```ts
7
+ * import { SHORT_NAMES } from "@johnmorrisdotca/chikyu-earth/satellites";
8
+ *
9
+ * console.log(SHORT_NAMES[48274]?.ja);
10
+ * ```
11
+ */
12
+ export const SHORT_NAMES = {
13
+ 25544: { en: "ISS", ja: "ISS" },
14
+ 48274: { en: "Tiangong", ja: "天宮" },
15
+ 20580: { en: "Hubble", ja: "ハッブル" },
16
+ };
@@ -0,0 +1,27 @@
1
+ /** A body as the camera sees it: the unit direction to its centre, whether the camera is inside it, and the cosine of its angular radius. */
2
+ export interface CoverBody {
3
+ dx: number;
4
+ dy: number;
5
+ dz: number;
6
+ inside: boolean;
7
+ cos: number;
8
+ }
9
+ /**
10
+ * The cover of a body for a camera.
11
+ *
12
+ * @param position - The body's centre, x y z.
13
+ * @param camera - The camera's place, x y z, in the same units.
14
+ * @param radius - The body's radius, in the same units.
15
+ * @returns The body as the camera sees it.
16
+ */
17
+ export declare function coverBody(position: readonly number[], camera: readonly number[], radius: number): CoverBody;
18
+ /**
19
+ * Whether any of the bodies covers a ray from the camera.
20
+ *
21
+ * @param bodies - The bodies, from `coverBody`.
22
+ * @param x - The ray's direction, x (a unit vector's).
23
+ * @param y - The ray's direction, y.
24
+ * @param z - The ray's direction, z.
25
+ * @returns True where the ray meets a body.
26
+ */
27
+ export declare function covers(bodies: readonly CoverBody[], x: number, y: number, z: number): boolean;
@@ -0,0 +1,32 @@
1
+ // Which bodies cover the sky at a ray from the camera: the Earth, the Moon and the Sun. Pure: a body covers a ray when the angle between the ray and the line to the body's centre is less than the body's angular radius, which is exact for any camera,
2
+ // where a circle on the screen is right only for a body on the camera's axis (a wide view draws the Earth as an ellipse towards its edges).
3
+ /**
4
+ * The cover of a body for a camera.
5
+ *
6
+ * @param position - The body's centre, x y z.
7
+ * @param camera - The camera's place, x y z, in the same units.
8
+ * @param radius - The body's radius, in the same units.
9
+ * @returns The body as the camera sees it.
10
+ */
11
+ export function coverBody(position, camera, radius) {
12
+ const dx = position[0] - camera[0];
13
+ const dy = position[1] - camera[1];
14
+ const dz = position[2] - camera[2];
15
+ const away = Math.hypot(dx, dy, dz) || 1;
16
+ return { dx: dx / away, dy: dy / away, dz: dz / away, inside: away <= radius, cos: away > radius ? Math.sqrt(1 - (radius / away) ** 2) : -1 };
17
+ }
18
+ /**
19
+ * Whether any of the bodies covers a ray from the camera.
20
+ *
21
+ * @param bodies - The bodies, from `coverBody`.
22
+ * @param x - The ray's direction, x (a unit vector's).
23
+ * @param y - The ray's direction, y.
24
+ * @param z - The ray's direction, z.
25
+ * @returns True where the ray meets a body.
26
+ */
27
+ export function covers(bodies, x, y, z) {
28
+ for (const body of bodies)
29
+ if (body.inside || x * body.dx + y * body.dy + z * body.dz > body.cos)
30
+ return true;
31
+ return false;
32
+ }
@@ -0,0 +1,21 @@
1
+ /** What an element tells the layer at each draw: where a direction in the sky falls on the screen, the size of the box, and the bodies that cover the sky (round, in pixels). */
2
+ export interface SkyFrame {
3
+ /** Writes where the direction (right ascension and declination, degrees) falls on the screen in CSS pixels and answers whether it is in front of the camera. */
4
+ project(rightAscension: number, declination: number, out: {
5
+ x: number;
6
+ y: number;
7
+ }): boolean;
8
+ width: number;
9
+ height: number;
10
+ /** Bodies in front of the sky, each a disc on the screen: the Earth, the Moon, the Sun. Used where `covered` is not given (a view that looks straight at the Earth, where a body is a circle). */
11
+ discs: readonly {
12
+ x: number;
13
+ y: number;
14
+ radius: number;
15
+ }[];
16
+ /**
17
+ * Whether a body covers the sky at a point of the screen, worked out from the ray through the point: a body covers it when the ray's angle to the body's centre is less than the body's angular radius. A camera with a wide field of view (a ride along a satellite sees the
18
+ * Earth 70 degrees across) draws a body as an ellipse towards the edges, which a circle on the screen cannot say.
19
+ */
20
+ covered?: (x: number, y: number) => boolean;
21
+ }
@@ -0,0 +1,2 @@
1
+ // What an element tells the star names' layer at each draw. Types only, and no import of hoshi-stars, so the package's own declarations can name it.
2
+ export {};
@@ -1,26 +1,14 @@
1
- import type { LabelStyle, ScreenPoint } from "@johnmorrisdotca/hoshi-stars/sky-labels";
1
+ import type { SkyFrame } from "./frame.ts";
2
+ import type { SkyLabelStyle } from "./style.ts";
2
3
  /** The styles of the layer, for an element to add to its own (`#sk-canvas`, `#sk-names` and the labels in its shadow root). */
3
4
  export declare const SKY_STYLE: string;
4
- /** What an element tells the layer at each draw: where a direction in the sky falls on the screen, the size of the box, and the bodies that cover the sky (round, in pixels). */
5
- export interface SkyFrame {
6
- /** Writes where the direction (right ascension and declination, degrees) falls on the screen in CSS pixels and answers whether it is in front of the camera. */
7
- project(rightAscension: number, declination: number, out: ScreenPoint): boolean;
8
- width: number;
9
- height: number;
10
- /** Bodies in front of the sky, each a disc on the screen: the Earth, the Moon, the Sun. */
11
- discs: readonly {
12
- x: number;
13
- y: number;
14
- radius: number;
15
- }[];
16
- }
17
5
  export interface SkyHost {
18
6
  /** Tells where the sky falls in the picture just drawn, or `null` when there is no picture. */
19
7
  frame(frame: SkyFrame | null): void;
20
8
  /** What the settings are now (`star-names`, `star-names-magnitude`, `constellation-lines`, the language): read again after any of them changed. */
21
9
  refresh(): void;
22
10
  /** The look of the names, as a partial hoshi-stars `LabelStyle` (the property `skyStyle`). */
23
- setStyle(style: Partial<LabelStyle>): void;
11
+ setStyle(style: Partial<SkyLabelStyle>): void;
24
12
  readonly stats: {
25
13
  on: boolean;
26
14
  loaded: boolean;
@@ -29,6 +17,11 @@ export interface SkyHost {
29
17
  picked: string | null;
30
18
  renders: number;
31
19
  lang: string;
20
+ stars: {
21
+ text: string;
22
+ x: number;
23
+ y: number;
24
+ }[];
32
25
  };
33
26
  dispose(): void;
34
27
  }
@@ -43,6 +36,8 @@ export interface SkyHostOptions {
43
36
  onError(error: unknown): void;
44
37
  /** Loads Michroma for the names, when the page has named no font of its own: kazoku-ui's `loadBodyLabelFont`. */
45
38
  loadFont(): Promise<unknown>;
39
+ /** Brings hoshi-stars' `sky-labels` in: a dynamic import of the package by default, a page's own (or a test's) where it has one. */
40
+ importer?: () => Promise<typeof import("@johnmorrisdotca/hoshi-stars/sky-labels")>;
46
41
  }
47
42
  /** The attributes this layer reads. */
48
43
  export declare const SKY_ATTRIBUTES: readonly ["star-names", "star-names-magnitude", "constellation-lines"];
package/dist/sky/host.js CHANGED
@@ -217,6 +217,19 @@ export function createSkyHost(options) {
217
217
  continue;
218
218
  }
219
219
  const rect = name.getBoundingClientRect();
220
+ // a name whose whole top edge is over a body (the Earth, the Moon, the Sun) lies on it: hoshi-stars tests the star's point only, so a name set below a star at the Earth's edge can be left entirely on the Earth, and is left out
221
+ const own = covered ? root.host.getBoundingClientRect() : undefined;
222
+ if (covered && own) {
223
+ const top = rect.top - own.top;
224
+ const left = rect.left - own.left;
225
+ const right = rect.right - own.left;
226
+ if (covered(left, top) && covered((left + right) / 2, top) && covered(right, top)) {
227
+ name.hidden = true;
228
+ designations[i].hidden = true;
229
+ culled.push(true);
230
+ continue;
231
+ }
232
+ }
220
233
  const over = kept.some((one) => rect.left < one.r && rect.right > one.l && rect.top < one.b && rect.bottom > one.t);
221
234
  if (over) {
222
235
  name.hidden = true;
@@ -271,7 +284,7 @@ export function createSkyHost(options) {
271
284
  return;
272
285
  loading = true;
273
286
  attach();
274
- void import("@johnmorrisdotca/hoshi-stars/sky-labels")
287
+ void (options.importer ?? (() => import("@johnmorrisdotca/hoshi-stars/sky-labels")))()
275
288
  .then(async (loaded) => {
276
289
  module = loaded;
277
290
  if (!style.font)
@@ -283,6 +296,14 @@ export function createSkyHost(options) {
283
296
  .catch((error) => {
284
297
  loading = false;
285
298
  failed = true;
299
+ // nothing of the layer stays in the page when hoshi-stars is not there
300
+ canvas?.remove();
301
+ box?.remove();
302
+ canvas = undefined;
303
+ box = undefined;
304
+ attached = false;
305
+ pool.length = 0;
306
+ designations.length = 0;
286
307
  options.onError(error);
287
308
  });
288
309
  };
@@ -294,7 +315,7 @@ export function createSkyHost(options) {
294
315
  if (usedLang !== undefined && options.language() !== usedLang)
295
316
  configure();
296
317
  const discs = frame.discs;
297
- covered = discs.length === 0 ? undefined : (x, y) => discs.some((d) => (x - d.x) * (x - d.x) + (y - d.y) * (y - d.y) <= d.radius * d.radius);
318
+ covered = frame.covered ?? (discs.length === 0 ? undefined : (x, y) => discs.some((d) => (x - d.x) * (x - d.x) + (y - d.y) * (y - d.y) <= d.radius * d.radius));
298
319
  const view = { width: frame.width, height: frame.height };
299
320
  if (names)
300
321
  names.update(frame.project, view, covered);
@@ -303,6 +324,16 @@ export function createSkyHost(options) {
303
324
  placeNames();
304
325
  drawLines();
305
326
  }
327
+ /** The stars that are named at the last frame: each one's own point on the screen (the label's anchor) and its name, from the picture's top left, without the names the browser's measure left out. */
328
+ const starPoints = () => {
329
+ const out = [];
330
+ if (!names)
331
+ return out;
332
+ for (const [index, label] of names.labels.entries())
333
+ if (!culled[index])
334
+ out.push({ text: label.text, x: label.x, y: label.y });
335
+ return out;
336
+ };
306
337
  // the family's chooser tells the document when the page's language changes: the names are written again, though nothing else is drawn
307
338
  const onLanguage = () => queueMicrotask(() => {
308
339
  if (module && wanted() && usedLang !== options.language()) {
@@ -368,7 +399,7 @@ export function createSkyHost(options) {
368
399
  }
369
400
  },
370
401
  get stats() {
371
- return { on: wanted(), loaded: module !== undefined, names: names ? drawnNames : 0, figures: lines?.count ?? 0, picked: lines?.picked?.abbreviation ?? null, renders, lang: usedLang ?? "" };
402
+ return { on: wanted(), loaded: module !== undefined, names: names ? drawnNames : 0, figures: lines?.count ?? 0, picked: lines?.picked?.abbreviation ?? null, renders, lang: usedLang ?? "", stars: starPoints() };
372
403
  },
373
404
  dispose() {
374
405
  doc.removeEventListener("kazoku-languagechange", onLanguage);
@@ -380,3 +411,6 @@ export function createSkyHost(options) {
380
411
  },
381
412
  };
382
413
  }
414
+ // The look chikyu-earth names (`SkyLabelStyle`) and hoshi-stars' `LabelStyle` are the same type: either one given where the other is asked for must compile, or this file does not.
415
+ const sameStyle = [{}, {}];
416
+ void sameStyle;
@@ -0,0 +1,50 @@
1
+ /**
2
+ * The look of a name drawn on the sky (the property `skyStyle` of `<chikyu-space>` and `<chikyu-earth-realistic>` takes any part of it): font, size, weight, colour, outline, case, letter spacing, the leader line, the ring round a star and the catalogue letter under the name. These are
3
+ * hoshi-stars' `LabelStyle`, with the same defaults and ranges, written out so a page without hoshi-stars installed still type-checks.
4
+ *
5
+ * @example
6
+ * ```ts
7
+ * import type { SkyLabelStyle } from "@johnmorrisdotca/chikyu-earth/realistic";
8
+ *
9
+ * const look: Partial<SkyLabelStyle> = { size: 12, colour: "#ffe9b0", case: "as-written", ring: 0 };
10
+ * console.log(Object.keys(look).join(" "));
11
+ * ```
12
+ */
13
+ export interface SkyLabelStyle {
14
+ /** The font family, as CSS writes it. `Michroma, system-ui, sans-serif` by default: Michroma is the family's font for a name on a body, and the page loads it (kazoku-ui's `loadBodyLabelFont`); until it is in, the next family is used. */
15
+ font: string;
16
+ /** The text size in pixels, 6 to 48. 11 by default. */
17
+ size: number;
18
+ /** The font weight, 100 to 900. 400 by default. */
19
+ weight: number;
20
+ /** The text colour, any CSS colour. A quiet white by default. */
21
+ colour: string;
22
+ /** The colour of the outline round the letters that keeps them readable over stars, any CSS colour, or `none` for no outline. A dark blue by default. */
23
+ halo: string;
24
+ /** The width of that outline in pixels, 0 to 12. 3 by default. */
25
+ haloWidth: number;
26
+ /** `small-caps` writes the name in small capitals (the page draws it with CSS `font-variant: small-caps`), `as-written` keeps it as it is, `upper` and `lower` change the case (Japanese is not changed by any). `small-caps` by default. */
27
+ case: "small-caps" | "as-written" | "upper" | "lower";
28
+ /** The space added between letters, in em, -0.1 to 1. 0.14 by default. */
29
+ letterSpacing: number;
30
+ /** The length in pixels of a short line from the star to its name, 0 to 80, or 0 for no line and the name set beside the star. 0 by default. */
31
+ leader: number;
32
+ /** The colour of that line, any CSS colour. A quiet blue-white by default. */
33
+ leaderColour: string;
34
+ /** The gap in pixels between the ring round a star (or the star) and its name (or its line), 0 to 40. 4 by default. */
35
+ offset: number;
36
+ /** The radius in pixels of a thin ring drawn round each named star, 0 to 30, or 0 for no ring. 6 by default. */
37
+ ring: number;
38
+ /** The colour of the ring, any CSS colour. A quiet blue-white by default. */
39
+ ringColour: string;
40
+ /** The line width of the ring in pixels, 0.25 to 4. 0.75 by default. */
41
+ ringWidth: number;
42
+ /** `below` writes the star's catalogue letter (`α Ori`) in small type under its name; `none` leaves it out. A star with no Bayer letter or Flamsteed number has none to write. `below` by default. */
43
+ designation: "below" | "none";
44
+ /** The font family of that line. A small monospace by default. */
45
+ designationFont: string;
46
+ /** Its size in pixels, 6 to 24. 9 by default. */
47
+ designationSize: number;
48
+ /** Its colour, any CSS colour. A quieter white by default. */
49
+ designationColour: string;
50
+ }
@@ -0,0 +1,2 @@
1
+ // The look of the names on the sky, as chikyu-earth's own type: the same fields as hoshi-stars' `LabelStyle` (an optional peer), written out here so the package's types never import a package a page may not have installed. A drift check in `host.ts` keeps the two the same.
2
+ export {};