@johnmorrisdotca/chikyu-earth 0.34.1 → 0.35.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 CHANGED
@@ -2,6 +2,13 @@
2
2
 
3
3
  ## Unreleased
4
4
 
5
+ ## 0.35.0 - 2026-11-12
6
+
7
+ - **The figures follow a sky culture** (MODULES-1125). `constellation-culture` is `iau` (the 88, the default), `western`, `japanese` (the 88 by their Japanese names, then the traditional figures such as Subaru, the Pleiades) or `chinese`; the lines, the hovered and picked figure and its name follow it, and the events carry `id`, `kind` and `culture`. The value is exactly what the page sets (a Japanese page keeps the IAU's figures unless it says `japanese`); an unknown value is `iau` with one warning. Each culture's data is a dynamic import of its own, fetched only when asked for. hoshi-stars' `createFigureLines` takes no culture yet, so the lines of a culture are laid out here from `figures(culture)`.
8
+ - **Say which constellation was picked, reveal its name on hover, and an option to name every constellation** (MODULES-1108). A mouse resting on a figure (its lines, or inside it) lights it and fades its name in at the middle of the part of it that is on the picture, inside the frame, off the Earth and clear of the star names; a click or tap picks it and the name stays until another pick or a click on empty sky, `Escape` or the property set to `null` (`C` and `Shift`+`C` pick with the keyboard). `chikyu-constellation-hover` and `chikyu-constellation-pick` are sent with `{ abbreviation, name, english, japanese, culture }` (`null` when let go), and `constellation` reads or sets the pick. The name sat above the figure's whole box, which includes the part off the picture, so a figure that ran off the top had its name left near its bottom. `constellation-names` (off by default) writes every figure's name in view, quieter than the star names, in the page's language, with no overlap. The hit test runs once a frame at most, only after the pointer has moved, and the layer is drawn again only when the hovered figure changes. A pick is read out by a visually hidden polite live region (a hover is not). A pick set before a sky culture has arrived is kept until it has.
9
+ - **A page can mark places where the sky's names must not go** (MODULES-1124). `<chikyu-space>` and `<chikyu-earth-realistic>` take `keepClear` (a list of rectangles in the element's pixels, or of page elements) and the attribute `keep-clear` (a CSS selector). A star name that touches one is left out, a star whose point is in one is not named, and the picked constellation's name moves clear of it; the lines are still drawn. The boxes are read when the list changes or the page is resized or scrolled (the names are laid out again at the next frame, once however many events), never at each frame.
10
+ - **Ride above a satellite** (MODULES-1138). `ride(ref, { view: "above" })` puts the camera on the line from the Earth's centre through the satellite, `distanceKm` over the zoom above it, looking straight down at it with the way it is going at the top of the picture. The satellite stays in the middle while the clock moves it and the Earth turns beneath; a drag turns the picture about it and tips the camera back or ahead of it; the zoom changes the height; a satellite model is drawn there as in the follow view. It glides from and to the other views (a jump for reduced motion) and draws as the follow view does. `aboveAxes` gives the camera's place and top. The demo has an "Above it" button and the address `rview=above`.
11
+
5
12
  ## 0.34.1 - 2026-11-11
6
13
 
7
14
  - **The sky is behind the Earth: constellation lines no longer cross it** (MODULES-1103). The lines were drawn over the Earth's disc (in a ride, across the whole globe). Each line is now cut exactly at the Earth's edge, the air's rim included, in the picture's own camera, in the space view and in a ride at any height, on the day side and the night side. A star whose point, ring or name box touches the Earth, the Moon or the Sun has its name and ring left out (a name only partly on the Earth was shown before), and the figure under the pointer is not lit over the Earth.
package/README.md CHANGED
@@ -590,7 +590,7 @@ console.log(countryList().filter((country) => country.alpha2 === null).map((coun
590
590
 
591
591
  ## How to cite
592
592
 
593
- > Morris, J. (2026). *Chikyu Earth* (version 0.34.1) [Computer software]. https://github.com/johnmorrisdotca/chikyu-earth
593
+ > Morris, J. (2026). *Chikyu Earth* (version 0.35.0) [Computer software]. https://github.com/johnmorrisdotca/chikyu-earth
594
594
 
595
595
  GitHub's "Cite this repository" button reads [CITATION.cff](CITATION.cff) and gives BibTeX and APA. Cite the version you used (`npm ls @johnmorrisdotca/chikyu-earth`), and credit the sources behind the numbers and pictures: [NOTICE.md](NOTICE.md) says who made each one and under what terms.
596
596
 
@@ -5,6 +5,7 @@ import type { SatelliteInfo, SatelliteRef, Satellites } from "../satellites/type
5
5
  import { type SatelliteStyle } from "../space/satellite-layer.ts";
6
6
  import type { Credit, DataSource } from "../data-door.ts";
7
7
  import { type BodyLabelSettings } from "./body-label.ts";
8
+ import type { SkyKeepClear } from "../sky/keep-clear.ts";
8
9
  import type { SkyLabelStyle } from "../sky/style.ts";
9
10
  import { type Place, type PlaceLook } from "../markers/places.ts";
10
11
  import { type CityNamesStyle } from "../cities/host.ts";
@@ -143,6 +144,20 @@ export declare class ChikyuEarthRealistic extends HTMLElement {
143
144
  */
144
145
  get skyStyle(): Partial<SkyLabelStyle>;
145
146
  set skyStyle(value: Partial<SkyLabelStyle>);
147
+ /**
148
+ * The constellation that is picked, by its IAU abbreviation (`Ori`), or `null`. A reader picks one by a click or a tap on its lines or inside its figure (and with the key `C`, or `Shift`+`C` for the previous, while the element has the focus); a click on empty sky, or `Escape`, lets it go. The picked figure's lines are lit and its name is
149
+ * shown at the middle of the part of it in view, until another is picked or it is let go. A page sets the property to pick one itself, which sends no event. `chikyu-constellation-pick` is sent at each pick of the reader's, with `detail` `{ abbreviation, id, name, english, japanese, kind, culture }` (`culture` is `iau` unless `constellation-culture` names another), and `null` when it is let go; `chikyu-constellation-hover` is sent the same way as a mouse rests on a figure and leaves it.
150
+ * Needs `constellation-lines` set. `constellation-culture` (`iau` by default, `western`, `japanese` or `chinese`; each is fetched only when asked for, and the value is exactly what the page sets) makes the figures, the pick and the names those of that sky culture. The attribute `constellation-names` (off by default) writes the name of every figure in view at the middle of the part of it that is on the picture, quieter than the star names and clear of them, of the Earth, the Moon, the Sun, the places and satellites named, and of the places kept clear (`keepClear`).
151
+ */
152
+ get constellation(): string | null;
153
+ set constellation(value: string | null);
154
+ /**
155
+ * Places on the picture where the sky's names must not go: a page's own text laid over the element, say. Each is a rectangle in the element's own CSS pixels from its top left (`{ x, y, width, height }`), or an element of the page whose box is read. A star name whose box touches
156
+ * one is left out, and so is a star whose point is in one (the next star is named instead); the picked constellation's name is moved clear of it. The lines are still drawn. The boxes are read when the list changes, when the page or an element is resized or scrolled, and not at each frame: a
157
+ * page that moves its text without resizing it sets the property again. The attribute `keep-clear` takes a CSS selector for the same (`keep-clear=".hero h1, .hero .lead"`). Nothing is redrawn at rest.
158
+ */
159
+ get keepClear(): SkyKeepClear[];
160
+ set keepClear(value: readonly SkyKeepClear[] | null);
146
161
  /**
147
162
  * 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`
148
163
  * (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
@@ -253,6 +268,10 @@ export declare class ChikyuEarthRealistic extends HTMLElement {
253
268
  names: number;
254
269
  figures: number;
255
270
  picked: string | null;
271
+ pick: string | null;
272
+ hover: string | null;
273
+ constellations: number;
274
+ culture: string;
256
275
  renders: number;
257
276
  lang: string;
258
277
  stars: {
@@ -172,6 +172,7 @@ export class ChikyuEarthRealistic extends HTMLElement {
172
172
  /** The names of the stars and the constellation lines (`star-names`, `constellation-lines`), and the look of the names. */
173
173
  #sky;
174
174
  #skyStyle = {};
175
+ #keepClear = [];
175
176
  #airNote;
176
177
  #onContext = (state) => {
177
178
  if (state === "unavailable")
@@ -214,6 +215,7 @@ export class ChikyuEarthRealistic extends HTMLElement {
214
215
  loadFont: loadMichroma,
215
216
  });
216
217
  this.#sky.setStyle(this.#skyStyle);
218
+ this.#sky.setKeepClear(this.#keepClear);
217
219
  this.#placeLayer = createPlaceLayer(root.getElementById("places"));
218
220
  this.#placeList = readPlaces(this.getAttribute("places"));
219
221
  this.#placeLayer.set(this.#placeList);
@@ -478,6 +480,29 @@ export class ChikyuEarthRealistic extends HTMLElement {
478
480
  this.#skyStyle = { ...value };
479
481
  this.#sky?.setStyle(this.#skyStyle);
480
482
  }
483
+ /**
484
+ * The constellation that is picked, by its IAU abbreviation (`Ori`), or `null`. A reader picks one by a click or a tap on its lines or inside its figure (and with the key `C`, or `Shift`+`C` for the previous, while the element has the focus); a click on empty sky, or `Escape`, lets it go. The picked figure's lines are lit and its name is
485
+ * shown at the middle of the part of it in view, until another is picked or it is let go. A page sets the property to pick one itself, which sends no event. `chikyu-constellation-pick` is sent at each pick of the reader's, with `detail` `{ abbreviation, id, name, english, japanese, kind, culture }` (`culture` is `iau` unless `constellation-culture` names another), and `null` when it is let go; `chikyu-constellation-hover` is sent the same way as a mouse rests on a figure and leaves it.
486
+ * Needs `constellation-lines` set. `constellation-culture` (`iau` by default, `western`, `japanese` or `chinese`; each is fetched only when asked for, and the value is exactly what the page sets) makes the figures, the pick and the names those of that sky culture. The attribute `constellation-names` (off by default) writes the name of every figure in view at the middle of the part of it that is on the picture, quieter than the star names and clear of them, of the Earth, the Moon, the Sun, the places and satellites named, and of the places kept clear (`keepClear`).
487
+ */
488
+ get constellation() {
489
+ return this.#sky?.picked ?? null;
490
+ }
491
+ set constellation(value) {
492
+ this.#sky?.pick(value);
493
+ }
494
+ /**
495
+ * Places on the picture where the sky's names must not go: a page's own text laid over the element, say. Each is a rectangle in the element's own CSS pixels from its top left (`{ x, y, width, height }`), or an element of the page whose box is read. A star name whose box touches
496
+ * one is left out, and so is a star whose point is in one (the next star is named instead); the picked constellation's name is moved clear of it. The lines are still drawn. The boxes are read when the list changes, when the page or an element is resized or scrolled, and not at each frame: a
497
+ * page that moves its text without resizing it sets the property again. The attribute `keep-clear` takes a CSS selector for the same (`keep-clear=".hero h1, .hero .lead"`). Nothing is redrawn at rest.
498
+ */
499
+ get keepClear() {
500
+ return [...this.#keepClear];
501
+ }
502
+ set keepClear(value) {
503
+ this.#keepClear = value ? [...value] : [];
504
+ this.#sky?.setKeepClear(this.#keepClear);
505
+ }
481
506
  /**
482
507
  * 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
508
  * (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
@@ -713,7 +738,7 @@ export class ChikyuEarthRealistic extends HTMLElement {
713
738
  assets: { entries: stats?.assets.entries ?? 0, refs: stats?.assets.refs ?? 0 },
714
739
  meteors: { on: this.#model.meteors.on, strength: this.#model.meteors.strength, ...this.#model.meteors.drawn },
715
740
  bodyLabel: { on: this.#model.bodyLabel.settings !== null, text: this.#model.bodyLabel.text, ...this.#model.bodyLabel.drawn },
716
- sky: this.#sky?.stats ?? { on: false, loaded: false, names: 0, figures: 0, picked: null, renders: 0, lang: "", stars: [] },
741
+ sky: this.#sky?.stats ?? { on: false, loaded: false, names: 0, figures: 0, picked: null, pick: null, hover: null, constellations: 0, culture: "iau", renders: 0, lang: "", stars: [] },
717
742
  places: { ...(this.#placeLayer?.stats ?? { places: 0, shown: 0, labels: 0, renders: 0 }) },
718
743
  cityNames: this.#cities?.stats ?? { on: false, max: 0, radius: 0, tiers: [], cities: 0, names: 0, renders: 0 },
719
744
  };
@@ -9,5 +9,6 @@ export { BODY_LABEL_ATTRIBUTES, bodyLabelText, readBodyLabel, loadMichroma, setB
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
11
  export type { SkyLabelStyle } from "../sky/style.ts";
12
+ export type { SkyKeepClear } from "../sky/keep-clear.ts";
12
13
  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
14
  export { CITY_NAMES_ATTRIBUTES, type CityNamesStyle } from "../cities/host.ts";
@@ -0,0 +1,56 @@
1
+ import type { LabelStyle } from "@johnmorrisdotca/hoshi-stars/sky-labels";
2
+ /** A star's place in the sky, in degrees. */
3
+ export interface SkyPoint {
4
+ rightAscension: number;
5
+ declination: number;
6
+ }
7
+ /** A figure of a culture as lines: its key and the pairs of stars its lines join (hoshi-stars' `CultureFigure`, to the part used here). */
8
+ export interface CultureFigureLines {
9
+ id: string;
10
+ segments: ReadonlyArray<readonly [SkyPoint, SkyPoint]>;
11
+ }
12
+ /** One figure on the screen: where its lines are in `segments`, and its box. */
13
+ export interface OnScreen {
14
+ abbreviation: string;
15
+ start: number;
16
+ end: number;
17
+ left: number;
18
+ top: number;
19
+ right: number;
20
+ bottom: number;
21
+ x: number;
22
+ }
23
+ /** What the sky layer needs of a set of figure lines: those of hoshi-stars' `FigureLines` and of `createCultureLines`. */
24
+ export interface Lines {
25
+ readonly settings: Readonly<{
26
+ strength?: number;
27
+ reach?: number;
28
+ }>;
29
+ readonly style: LabelStyle;
30
+ readonly segments: ArrayLike<number>;
31
+ readonly count: number;
32
+ readonly figures: readonly OnScreen[];
33
+ opacityOf(figure: OnScreen): number;
34
+ update(project: (rightAscension: number, declination: number, out: {
35
+ x: number;
36
+ y: number;
37
+ }) => boolean, view: {
38
+ width: number;
39
+ height: number;
40
+ }, avoid?: unknown): number;
41
+ set(next: {
42
+ strength?: number;
43
+ lang?: unknown;
44
+ style?: Partial<LabelStyle>;
45
+ }): void;
46
+ }
47
+ /**
48
+ * The lines of the figures of a culture.
49
+ *
50
+ * @param figures - The culture's figures, from hoshi-stars' `figures(culture)`.
51
+ * @param strength - How strong the lines are when not lit, 0 to 1.
52
+ * @param styleOf - Makes a label style (hoshi-stars' `labelStyle`) from a partial one.
53
+ * @param style - The look of the names, a partial `LabelStyle`.
54
+ * @returns The lines.
55
+ */
56
+ export declare function createCultureLines(figures: readonly CultureFigureLines[], strength: number, styleOf: (style?: Partial<LabelStyle>) => LabelStyle, style?: Partial<LabelStyle>): Lines;
@@ -0,0 +1,82 @@
1
+ /**
2
+ * The lines of the figures of a culture.
3
+ *
4
+ * @param figures - The culture's figures, from hoshi-stars' `figures(culture)`.
5
+ * @param strength - How strong the lines are when not lit, 0 to 1.
6
+ * @param styleOf - Makes a label style (hoshi-stars' `labelStyle`) from a partial one.
7
+ * @param style - The look of the names, a partial `LabelStyle`.
8
+ * @returns The lines.
9
+ */
10
+ export function createCultureLines(figures, strength, styleOf, style) {
11
+ let total = 0;
12
+ for (const figure of figures)
13
+ total += figure.segments.length;
14
+ const segments = new Float32Array(total * 4);
15
+ const onScreen = figures.map((figure) => ({ abbreviation: figure.id, start: 0, end: 0, left: 0, top: 0, right: 0, bottom: 0, x: 0 }));
16
+ const a = { x: 0, y: 0 };
17
+ const b = { x: 0, y: 0 };
18
+ let current = Object.freeze({ strength });
19
+ let look = styleOf(style);
20
+ let count = 0;
21
+ return {
22
+ get settings() {
23
+ return current;
24
+ },
25
+ get style() {
26
+ return look;
27
+ },
28
+ segments,
29
+ get count() {
30
+ return count;
31
+ },
32
+ figures: onScreen,
33
+ opacityOf() {
34
+ return current.strength ?? 0;
35
+ },
36
+ set(next) {
37
+ current = Object.freeze({ ...current, strength: Math.max(0, Math.min(1, next.strength ?? current.strength ?? 0)) });
38
+ look = styleOf(next.style);
39
+ },
40
+ update(project, view) {
41
+ count = 0;
42
+ for (let i = 0; i < figures.length; i += 1) {
43
+ const out = onScreen[i];
44
+ out.start = count;
45
+ let left = Number.POSITIVE_INFINITY;
46
+ let top = Number.POSITIVE_INFINITY;
47
+ let right = Number.NEGATIVE_INFINITY;
48
+ let bottom = Number.NEGATIVE_INFINITY;
49
+ if ((current.strength ?? 0) > 0) {
50
+ const pairs = figures[i].segments;
51
+ for (let k = 0; k < pairs.length; k += 1) {
52
+ const from = pairs[k][0];
53
+ const to = pairs[k][1];
54
+ if (!project(from.rightAscension, from.declination, a) || !project(to.rightAscension, to.declination, b))
55
+ continue;
56
+ if ((a.x < 0 && b.x < 0) || (a.x > view.width && b.x > view.width) || (a.y < 0 && b.y < 0) || (a.y > view.height && b.y > view.height))
57
+ continue;
58
+ const at = count * 4;
59
+ segments[at] = a.x;
60
+ segments[at + 1] = a.y;
61
+ segments[at + 2] = b.x;
62
+ segments[at + 3] = b.y;
63
+ count += 1;
64
+ left = Math.min(left, a.x, b.x);
65
+ right = Math.max(right, a.x, b.x);
66
+ top = Math.min(top, a.y, b.y);
67
+ bottom = Math.max(bottom, a.y, b.y);
68
+ }
69
+ }
70
+ out.end = count;
71
+ if (out.end > out.start) {
72
+ out.left = left;
73
+ out.right = right;
74
+ out.top = top;
75
+ out.bottom = bottom;
76
+ out.x = Math.max(0, Math.min(view.width, (left + right) / 2));
77
+ }
78
+ }
79
+ return count;
80
+ },
81
+ };
82
+ }
@@ -0,0 +1,75 @@
1
+ /** A list of segments, four numbers each: where one starts and where it ends. */
2
+ export type SegmentList = ArrayLike<number>;
3
+ /**
4
+ * The squared distance from a point to the nearest segment of a figure.
5
+ *
6
+ * @param segments - The segments, four numbers each.
7
+ * @param start - The figure's first segment.
8
+ * @param end - One past its last.
9
+ * @param x - The point, x.
10
+ * @param y - The point, y.
11
+ * @returns The squared distance to the nearest, or infinity where the figure has none.
12
+ */
13
+ export declare function nearestSegment(segments: SegmentList, start: number, end: number, x: number, y: number): number;
14
+ /**
15
+ * The convex hull of a figure's segment ends (Andrew's monotone chain), as x y pairs: made once for a figure and kept while the picture holds, then asked of with `areaInHull` at each pointer.
16
+ *
17
+ * @param segments - The segments, four numbers each.
18
+ * @param start - The figure's first segment.
19
+ * @param end - One past its last.
20
+ * @returns The hull's corners, x then y for each, round the figure.
21
+ */
22
+ export declare function hullOf(segments: SegmentList, start: number, end: number): number[];
23
+ /**
24
+ * The area of the convex hull of a figure when a point is inside it, or -1 when it is not. A pointer inside a figure (not only on its lines) picks it; where figures overlap, the smaller hull is the one meant.
25
+ *
26
+ * @param segments - The segments, four numbers each.
27
+ * @param start - The figure's first segment.
28
+ * @param end - One past its last.
29
+ * @param x - The point, x.
30
+ * @param y - The point, y.
31
+ * @returns The hull's area where the point is inside it, else -1.
32
+ */
33
+ export declare function hullAreaAround(segments: SegmentList, start: number, end: number, x: number, y: number): number;
34
+ /**
35
+ * The area of a hull when a point is inside it, or -1 when it is not (and for a hull with fewer than three corners).
36
+ *
37
+ * @param h - The hull, from `hullOf`.
38
+ * @param x - The point, x.
39
+ * @param y - The point, y.
40
+ * @returns The area where the point is inside, else -1.
41
+ */
42
+ export declare function areaInHull(h: readonly number[], x: number, y: number): number;
43
+ /**
44
+ * The visual middle of the part of a figure that is on the picture: the middle of its lines, each counted by its length, taken only where they are inside the picture and not under a body or a place kept clear. Pieces outside the picture, and the parts of a figure that run off it, count for nothing.
45
+ *
46
+ * @param segments - The segments, four numbers each.
47
+ * @param start - The figure's first segment.
48
+ * @param end - One past its last.
49
+ * @param width - The picture's width.
50
+ * @param height - The picture's height.
51
+ * @param blocked - Whether a point of the picture is on a body or in a place kept clear, or `undefined` where none is.
52
+ * @param out - Where to write the middle.
53
+ * @returns True where any of the figure is in view, with `out` written.
54
+ */
55
+ export declare function visibleCentre(segments: SegmentList, start: number, end: number, width: number, height: number, blocked: ((x: number, y: number) => boolean) | undefined, out: {
56
+ x: number;
57
+ y: number;
58
+ }): boolean;
59
+ /**
60
+ * Where a box goes: its middle at a point, or the nearest place round the point where it is inside the picture and `free` says it can stand.
61
+ *
62
+ * @param x - The wanted middle, x.
63
+ * @param y - The wanted middle, y.
64
+ * @param w - The box's width.
65
+ * @param h - The box's height.
66
+ * @param width - The picture's width.
67
+ * @param height - The picture's height.
68
+ * @param free - Whether a box (left, top, right, bottom) may stand there.
69
+ * @param out - Where to write the middle of the place found.
70
+ * @returns True where a place was found.
71
+ */
72
+ export declare function placeBox(x: number, y: number, w: number, h: number, width: number, height: number, free: (l: number, t: number, r: number, b: number) => boolean, out: {
73
+ x: number;
74
+ y: number;
75
+ }): boolean;
@@ -0,0 +1,200 @@
1
+ // Where a reader's pointer is on the constellation figures, and where a figure's name goes. Pure: the lines are a list of segments (x0, y0, x1, y1 in the picture's pixels), a figure being a run of them.
2
+ /**
3
+ * The squared distance from a point to the nearest segment of a figure.
4
+ *
5
+ * @param segments - The segments, four numbers each.
6
+ * @param start - The figure's first segment.
7
+ * @param end - One past its last.
8
+ * @param x - The point, x.
9
+ * @param y - The point, y.
10
+ * @returns The squared distance to the nearest, or infinity where the figure has none.
11
+ */
12
+ export function nearestSegment(segments, start, end, x, y) {
13
+ let best = Number.POSITIVE_INFINITY;
14
+ for (let s = start; s < end; s += 1) {
15
+ const at = s * 4;
16
+ const x0 = segments[at];
17
+ const y0 = segments[at + 1];
18
+ const dx = segments[at + 2] - x0;
19
+ const dy = segments[at + 3] - y0;
20
+ const length = dx * dx + dy * dy;
21
+ const t = length === 0 ? 0 : Math.max(0, Math.min(1, ((x - x0) * dx + (y - y0) * dy) / length));
22
+ const d = (x0 + t * dx - x) ** 2 + (y0 + t * dy - y) ** 2;
23
+ if (d < best)
24
+ best = d;
25
+ }
26
+ return best;
27
+ }
28
+ /**
29
+ * The convex hull of a figure's segment ends (Andrew's monotone chain), as x y pairs: made once for a figure and kept while the picture holds, then asked of with `areaInHull` at each pointer.
30
+ *
31
+ * @param segments - The segments, four numbers each.
32
+ * @param start - The figure's first segment.
33
+ * @param end - One past its last.
34
+ * @returns The hull's corners, x then y for each, round the figure.
35
+ */
36
+ export function hullOf(segments, start, end) {
37
+ const points = [];
38
+ for (let s = start; s < end; s += 1) {
39
+ points.push([segments[s * 4], segments[s * 4 + 1]], [segments[s * 4 + 2], segments[s * 4 + 3]]);
40
+ }
41
+ points.sort((a, b) => a[0] - b[0] || a[1] - b[1]);
42
+ const cross = (o, a, b) => (a[0] - o[0]) * (b[1] - o[1]) - (a[1] - o[1]) * (b[0] - o[0]);
43
+ const chain = [];
44
+ for (const point of points) {
45
+ while (chain.length >= 2 && cross(chain[chain.length - 2], chain[chain.length - 1], point) <= 0)
46
+ chain.pop();
47
+ chain.push(point);
48
+ }
49
+ const floor = chain.length + 1;
50
+ for (let i = points.length - 2; i >= 0; i -= 1) {
51
+ const point = points[i];
52
+ while (chain.length >= floor && cross(chain[chain.length - 2], chain[chain.length - 1], point) <= 0)
53
+ chain.pop();
54
+ chain.push(point);
55
+ }
56
+ chain.pop();
57
+ return chain.flat();
58
+ }
59
+ /**
60
+ * The area of the convex hull of a figure when a point is inside it, or -1 when it is not. A pointer inside a figure (not only on its lines) picks it; where figures overlap, the smaller hull is the one meant.
61
+ *
62
+ * @param segments - The segments, four numbers each.
63
+ * @param start - The figure's first segment.
64
+ * @param end - One past its last.
65
+ * @param x - The point, x.
66
+ * @param y - The point, y.
67
+ * @returns The hull's area where the point is inside it, else -1.
68
+ */
69
+ export function hullAreaAround(segments, start, end, x, y) {
70
+ return areaInHull(hullOf(segments, start, end), x, y);
71
+ }
72
+ /**
73
+ * The area of a hull when a point is inside it, or -1 when it is not (and for a hull with fewer than three corners).
74
+ *
75
+ * @param h - The hull, from `hullOf`.
76
+ * @param x - The point, x.
77
+ * @param y - The point, y.
78
+ * @returns The area where the point is inside, else -1.
79
+ */
80
+ export function areaInHull(h, x, y) {
81
+ const n = h.length / 2;
82
+ if (n < 3)
83
+ return -1;
84
+ let area = 0;
85
+ let sign = 0;
86
+ for (let i = 0; i < n; i += 1) {
87
+ const j = (i + 1) % n;
88
+ const ax = h[i * 2];
89
+ const ay = h[i * 2 + 1];
90
+ const bx = h[j * 2];
91
+ const by = h[j * 2 + 1];
92
+ area += ax * by - bx * ay;
93
+ const side = (bx - ax) * (y - ay) - (by - ay) * (x - ax);
94
+ if (side !== 0) {
95
+ if (sign === 0)
96
+ sign = Math.sign(side);
97
+ else if (Math.sign(side) !== sign)
98
+ return -1;
99
+ }
100
+ }
101
+ return Math.abs(area) / 2;
102
+ }
103
+ /**
104
+ * The visual middle of the part of a figure that is on the picture: the middle of its lines, each counted by its length, taken only where they are inside the picture and not under a body or a place kept clear. Pieces outside the picture, and the parts of a figure that run off it, count for nothing.
105
+ *
106
+ * @param segments - The segments, four numbers each.
107
+ * @param start - The figure's first segment.
108
+ * @param end - One past its last.
109
+ * @param width - The picture's width.
110
+ * @param height - The picture's height.
111
+ * @param blocked - Whether a point of the picture is on a body or in a place kept clear, or `undefined` where none is.
112
+ * @param out - Where to write the middle.
113
+ * @returns True where any of the figure is in view, with `out` written.
114
+ */
115
+ export function visibleCentre(segments, start, end, width, height, blocked, out) {
116
+ let sx = 0;
117
+ let sy = 0;
118
+ let sum = 0;
119
+ for (let s = start; s < end; s += 1) {
120
+ const at = s * 4;
121
+ const x0 = segments[at];
122
+ const y0 = segments[at + 1];
123
+ const dx = segments[at + 2] - x0;
124
+ const dy = segments[at + 3] - y0;
125
+ // Liang and Barsky: the part of the segment inside the picture, as parameters t0 to t1
126
+ let t0 = 0;
127
+ let t1 = 1;
128
+ let inside = true;
129
+ for (let side = 0; side < 4 && inside; side += 1) {
130
+ const p = side === 0 ? -dx : side === 1 ? dx : side === 2 ? -dy : dy;
131
+ const q = side === 0 ? x0 : side === 1 ? width - x0 : side === 2 ? y0 : height - y0;
132
+ if (p === 0) {
133
+ if (q < 0)
134
+ inside = false;
135
+ }
136
+ else {
137
+ const r = q / p;
138
+ if (p < 0) {
139
+ if (r > t1)
140
+ inside = false;
141
+ else if (r > t0)
142
+ t0 = r;
143
+ }
144
+ else if (r < t0)
145
+ inside = false;
146
+ else if (r < t1)
147
+ t1 = r;
148
+ }
149
+ }
150
+ if (!inside || t1 <= t0)
151
+ continue;
152
+ const mid = (t0 + t1) / 2;
153
+ const mx = x0 + dx * mid;
154
+ const my = y0 + dy * mid;
155
+ if (blocked?.(mx, my))
156
+ continue;
157
+ const length = (t1 - t0) * Math.hypot(dx, dy) || 0.01;
158
+ sx += mx * length;
159
+ sy += my * length;
160
+ sum += length;
161
+ }
162
+ if (sum <= 0)
163
+ return false;
164
+ out.x = sx / sum;
165
+ out.y = sy / sum;
166
+ return true;
167
+ }
168
+ /** Offsets tried, in steps of the box's own width (across) and height (down), nearest first. */
169
+ const ACROSS = [0, -0.4, 0.4, -0.8, 0.8, -1.2, 1.2, -1.6, 1.6, -2.2, 2.2];
170
+ const DOWN = [0, -1.2, 1.2, -2.4, 2.4, -3.6, 3.6, -4.8, 4.8];
171
+ /**
172
+ * Where a box goes: its middle at a point, or the nearest place round the point where it is inside the picture and `free` says it can stand.
173
+ *
174
+ * @param x - The wanted middle, x.
175
+ * @param y - The wanted middle, y.
176
+ * @param w - The box's width.
177
+ * @param h - The box's height.
178
+ * @param width - The picture's width.
179
+ * @param height - The picture's height.
180
+ * @param free - Whether a box (left, top, right, bottom) may stand there.
181
+ * @param out - Where to write the middle of the place found.
182
+ * @returns True where a place was found.
183
+ */
184
+ export function placeBox(x, y, w, h, width, height, free, out) {
185
+ const margin = 4;
186
+ let best = Number.POSITIVE_INFINITY;
187
+ for (const down of DOWN) {
188
+ for (const across of ACROSS) {
189
+ const cx = Math.max(margin + w / 2, Math.min(width - margin - w / 2, x + across * w));
190
+ const cy = Math.max(margin + h / 2, Math.min(height - margin - h / 2, y + down * h));
191
+ const away = Math.abs(cx - x) + Math.abs(cy - y);
192
+ if (away < best && free(cx - w / 2, cy - h / 2, cx + w / 2, cy + h / 2)) {
193
+ best = away;
194
+ out.x = cx;
195
+ out.y = cy;
196
+ }
197
+ }
198
+ }
199
+ return best !== Number.POSITIVE_INFINITY;
200
+ }
@@ -1,3 +1,5 @@
1
+ import type { Culture } from "@johnmorrisdotca/hoshi-stars/culture";
2
+ import { type SkyKeepClear } from "./keep-clear.ts";
1
3
  import type { SkyFrame } from "./frame.ts";
2
4
  import type { SkyLabelStyle } from "./style.ts";
3
5
  /** The styles of the layer, for an element to add to its own (`#sk-canvas`, `#sk-names` and the labels in its shadow root). */
@@ -7,14 +9,24 @@ export interface SkyHost {
7
9
  frame(frame: SkyFrame | null): void;
8
10
  /** What the settings are now (`star-names`, `star-names-magnitude`, `constellation-lines`, the language): read again after any of them changed. */
9
11
  refresh(): void;
12
+ /** The places where the sky's names must not go (the property `keepClear`): rectangles in the element's pixels, or elements of the page. `null` for none. */
13
+ setKeepClear(areas: readonly SkyKeepClear[] | null): void;
10
14
  /** The look of the names, as a partial hoshi-stars `LabelStyle` (the property `skyStyle`). */
11
15
  setStyle(style: Partial<SkyLabelStyle>): void;
16
+ /** Picks a constellation by its IAU abbreviation (`Ori`), or lets it go (`null`): the property `constellation`. Sends no event. */
17
+ pick(abbreviation: string | null): void;
18
+ /** The picked constellation's IAU abbreviation, or `null`. */
19
+ readonly picked: string | null;
12
20
  readonly stats: {
13
21
  on: boolean;
14
22
  loaded: boolean;
15
23
  names: number;
16
24
  figures: number;
17
25
  picked: string | null;
26
+ pick: string | null;
27
+ hover: string | null;
28
+ constellations: number;
29
+ culture: string;
18
30
  renders: number;
19
31
  lang: string;
20
32
  stars: {
@@ -38,9 +50,13 @@ export interface SkyHostOptions {
38
50
  loadFont(): Promise<unknown>;
39
51
  /** 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
52
  importer?: () => Promise<typeof import("@johnmorrisdotca/hoshi-stars/sky-labels")>;
53
+ /** Brings hoshi-stars' `constellations` in, for the names of the figures: a dynamic import by default. */
54
+ constellations?: () => Promise<typeof import("@johnmorrisdotca/hoshi-stars/constellations")>;
55
+ /** Brings a sky culture of hoshi-stars in (`western`, `japanese` or `chinese`): a dynamic import of the culture by default, a page's own (or a test's) where it has one. */
56
+ cultures?: (id: string) => Promise<Culture>;
41
57
  }
42
58
  /** The attributes this layer reads. */
43
- export declare const SKY_ATTRIBUTES: readonly ["star-names", "star-names-magnitude", "constellation-lines"];
59
+ export declare const SKY_ATTRIBUTES: readonly ["star-names", "star-names-magnitude", "constellation-lines", "constellation-names", "constellation-culture", "keep-clear"];
44
60
  /**
45
61
  * Makes the layer of one element, inside its shadow root. Nothing of hoshi-stars is loaded, and nothing is added to the root, until one of the three attributes is there.
46
62
  *