@johnmorrisdotca/wakusei-planets 0.63.0 → 0.64.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.
@@ -92,9 +92,9 @@ export interface BodyHover extends BodyInfo {
92
92
  * ```
93
93
  */
94
94
  export interface BodySight extends Sight {
95
- /** A body's id, a craft's own id (`voyager1`, `roadster`) when `kind` is `spacecraft`, or an orbiter's id (`mro`, the one `orbiter` takes) when `kind` is `orbiter`. */
95
+ /** A body's id, a craft's own id (`voyager1`, `roadster`) when `kind` is `spacecraft`, or an orbiter's id (`mro`, the one `orbiter` takes) when `kind` is `orbiter`, or an asteroid's id from `/asteroids` (`eros`, with the option `asteroids` on) when `kind` is `small-body`. */
96
96
  id: string;
97
- kind: SpaceKind | "spacecraft" | "orbiter";
97
+ kind: SpaceKind | "spacecraft" | "orbiter" | "small-body";
98
98
  host?: BodyId;
99
99
  /** Degrees between the camera's forward direction and the body, 0 (straight ahead) to 180 (straight behind). */
100
100
  offAxis: number;
@@ -360,6 +360,24 @@ export declare class WakuseiSpace extends HTMLElement {
360
360
  set pace(value: number);
361
361
  /** The stage out from the Sun the view is framed on (`inner-planets`, `asteroid-belt`, `outer-planets`, `kuiper-belt`, `whole-system`), or null for a view of a body. */
362
362
  get region(): SunRegion | null;
363
+ /**
364
+ * Place the asteroids of `/asteroids` in the view (the attribute `asteroids`; off by default): about a hundred bodies, each a point on its orbit on the date shown, drawn with the belts' points in the one instanced draw, and the
365
+ * nearest few within about an astronomical unit (the true scale) named, in the page's language. A name is pressable: `wakusei-body-click` (`{ id, name, kind: "small-body" }`) and `wakusei-asteroid-click` (`{ id, name }`)
366
+ * say which, and nothing else happens (a press does not go there: `goTo(id)` and `ride(id)` do). Each is an entry of `sight()` with `kind: "small-body"`. The records and their orbits are loaded when the option is first turned on and cost nothing
367
+ * before; with it off nothing is worked out or drawn for them. The positions are two-body: good near each body's epoch (`ASTEROID_ACCURACY` in `/asteroids`), less so far from it. `stats.asteroids` says how many were
368
+ * placed and named and what placing them cost.
369
+ *
370
+ * @example
371
+ * ```ts no-run
372
+ * import type { WakuseiSpace } from "@johnmorrisdotca/wakusei-planets/space/element";
373
+ *
374
+ * const space = document.querySelector<WakuseiSpace>("wakusei-space")!;
375
+ * space.addEventListener("wakusei-asteroid-click", (event) => console.log((event as CustomEvent<{ id: string; name: string }>).detail.name));
376
+ * space.asteroids = true;
377
+ * ```
378
+ */
379
+ get asteroids(): boolean;
380
+ set asteroids(value: boolean);
363
381
  /**
364
382
  * Show the spacecraft in orbit round the Moon and Mars (the attribute `orbiters`; off by default): each a small dot where `/orbiters` puts it on the date shown, scaled with its body, on its orbit line, with its
365
383
  * name beside it in the page's language, once the body is at least 24 CSS pixels in radius on the screen. A modelled path is drawn dim, and a predicted one whose measured error is more than 2 per cent of its body's radius (`orbiterDim`); a craft with no path is not drawn. The paths (a 9 kB module)
@@ -533,15 +551,33 @@ export declare class WakuseiSpace extends HTMLElement {
533
551
  * `outer-planets`, `kuiper-belt`, `whole-system`) is a target too: the Sun is looked at and the camera stands back until the stage's orbits are all in the picture, with the stage's name shown. `wakusei-hop`
534
552
  * tells the page where it went and where the next and previous hops lead. A name in any case, in English or Japanese, works.
535
553
  *
536
- * @param target - A body's name (`"saturn"`, `"Saturn"`, `"土星"`) or a stage's id.
537
- * @param options - `smooth` (true by default), `zoom` (1 by default), `transition` and `duration`.
554
+ *
555
+ * With the option `asteroids` on it takes an asteroid's id from `/asteroids` too (`"eros"`; `"asteroid:europa"` and `"asteroid:dione"` are the forms that mean the asteroid, for `europa` and `dione` are moons first): the view
556
+ * is set to the true scale (the squeezed scale cannot place a camera among the bodies, and going back to it ends the move), and the camera goes to a distance of a few times the asteroid's size (at least 200 km) looking at it
557
+ * and keeps it there as the clock runs: `riding` says which asteroid (`riding.target.id`), `wakusei-ride` is sent as for any ride and `wakusei-hop` is not (an asteroid is no stop of the hopper), and a `goTo` of a body or `ride(null)` ends it.
558
+ * Asked before the records are in, the move is made when they are, unless another move is asked for first.
559
+ *
560
+ * @param target - A body's name (`"saturn"`, `"Saturn"`, `"土星"`), a stage's id, or an asteroid's id.
561
+ * @param options - `smooth` (true by default), `zoom` (1 by default), `transition` and `duration`; for an asteroid also `distanceKm`.
538
562
  * @returns Whether the view can go there.
563
+ * @throws RangeError For an asteroid's id with the option `asteroids` off (the message says to turn it on), and, with it on, for a name that is no body of the view and no asteroid.
564
+ *
565
+ * @example
566
+ * ```ts no-run
567
+ * import type { WakuseiSpace } from "@johnmorrisdotca/wakusei-planets/space/element";
568
+ *
569
+ * const space = document.querySelector<WakuseiSpace>("wakusei-space")!;
570
+ * space.asteroids = true;
571
+ * space.goTo("eros");
572
+ * console.log(space.riding?.target);
573
+ * ```
539
574
  */
540
575
  goTo(target: BodyId | string, options?: {
541
576
  smooth?: boolean;
542
577
  zoom?: number;
543
578
  transition?: TravelKind;
544
579
  duration?: number;
580
+ distanceKm?: number;
545
581
  }): boolean;
546
582
  /**
547
583
  * Turn the camera where it stands until a body is in the picture, by the least turn: no move of the camera's place, only the way it looks. A body is in the picture when its middle is inside the picture less `margin` of
@@ -605,10 +641,25 @@ export declare class WakuseiSpace extends HTMLElement {
605
641
  * controls' zoom narrows the view. A body of the view (`"moon"`, `"mars"`) goes round the Earth; a moon (`"io"`) goes round its planet; a point of your own (`{ at(ms) }`, a satellite, a probe) goes round `around` (the Earth by default) and the
606
642
  * element knows nothing of what it is. Another call switches smoothly; `null` stops. The draw rate follows how fast the picture moves, never more than 30 a second (one a second for a reader who asked for reduced motion), and nothing is drawn out of sight.
607
643
  *
608
- * @param target - A body name, a moon name, a point, or `null` to stop.
644
+ *
645
+ * With the option `asteroids` on, an asteroid's id from `/asteroids` is a target too (`"apophis"`; `"asteroid:europa"` for the asteroid where the id is a moon's as well): the view is set to the true scale, the camera sits at the
646
+ * asteroid looking at the Sun with the way it goes at the top (`view: "follow"` and `distanceKm` keep a distance and look at it instead, as `goTo` does), and `riding.target.id` is the asteroid's id.
647
+ *
648
+ * @param target - A body name, a moon name, an asteroid's id, a point, or `null` to stop.
609
649
  * @param options - `view`, `look`, `distanceKm`, `around`, `smooth` (see `RideOptions`).
650
+ * @throws RangeError For an asteroid's id with the option `asteroids` off (the message says to turn it on), for the view `beside` with an asteroid, and, with the option on, for a name that is no body of the view and no asteroid.
651
+ *
652
+ * @example
653
+ * ```ts no-run
654
+ * import type { WakuseiSpace } from "@johnmorrisdotca/wakusei-planets/space/element";
655
+ *
656
+ * const space = document.querySelector<WakuseiSpace>("wakusei-space")!;
657
+ * space.asteroids = true;
658
+ * space.ride("apophis", { smooth: false });
659
+ * space.ride(null);
660
+ * ```
610
661
  */
611
- ride(target: RideTarget | null, options?: RideOptions): void;
662
+ ride(target: RideTarget | (string & Record<never, never>) | null, options?: RideOptions): void;
612
663
  /**
613
664
  * The belts drawn, by name (`main`, `hilda`, `trojan`, `centaur`, `classical`, `resonant`, `scattered`): all seven by default, `[]` for none. The attribute `belts` says the same (`belts="off"`, `belts="main trojan"`). Each belt is one instanced draw of its own
614
665
  * colour, from the package's own source of NASA/JPL's Small-Body Database (`@johnmorrisdotca/wakusei-planets/belts`), fetched when the view first draws it.
@@ -789,6 +840,12 @@ export declare class WakuseiSpace extends HTMLElement {
789
840
  lines: number;
790
841
  track: number;
791
842
  };
843
+ /** The asteroids of `/asteroids` at the last draw (the attribute `asteroids`): how many were placed (all of them, with the option on and the records loaded), how many named, and the milliseconds the places took; 0, 0 and 0 with it off. */
844
+ asteroids: {
845
+ placed: number;
846
+ named: number;
847
+ ms: number;
848
+ };
792
849
  /** Surface pictures still being fetched. A rest test waits for 0, then for the draw count to stay still: a sharper picture draws one more frame when it arrives. */
793
850
  pictures: {
794
851
  pending: number;
@@ -15,7 +15,8 @@ import { ownCredit } from "../door/own.js";
15
15
  import { BELT_IDS, BELTS, beltFlight, beltNote, beltsSource, beltSpacing, loadBeltBodies, OORT } from "../belts/index.js";
16
16
  import { setThreeLoader, takeRenderer } from "../scene/shared.js";
17
17
  import { texturePaths, tilesBase } from "../scene/textures.js";
18
- import { AU_IN_EARTH_RADII, EARTH_RADIUS_KM, frameAt, SPACE_DWARFS, SPACE_MINORS, SPACE_PLANETS } from "./frame.js";
18
+ import { ASTEROID_IDS, ASTEROID_PREFIX, asteroidIdOf } from "./asteroid-ids.js";
19
+ import { AU_IN_EARTH_RADII, EARTH_RADIUS_KM, eclipticToScene, frameAt, SPACE_DWARFS, SPACE_MINORS, SPACE_PLANETS } from "./frame.js";
19
20
  import { moonOffset, orientation } from "../scene/orient.js";
20
21
  import { moonDistance, placeBodies, radiusIn, readMoonSize } from "./scale.js";
21
22
  import { groundDirection } from "./aim.js";
@@ -143,7 +144,7 @@ const orbiterLines = (value) => (value === "chosen" || value === "none" ? value
143
144
  * ```
144
145
  */
145
146
  export class WakuseiSpace extends HTMLElement {
146
- static observedAttributes = ["center", "time", "speed", "scale", "labels", "labels-always", "covered", "roadster", "spacecraft", "orbiters", "orbiter", "orbiter-lines", "orbiter-track", "orbits", "help", "look", "light", "lang", "data", "arrows", "help-through", "help-place", "places", "surface-names", "site-flags", "earth-places", "earth-places-look", "all-moons", "far-opacity", "transition", "pace", "frame", "pair-with", "belts", "moon-size", ...NAME_LABEL_ATTRIBUTES];
147
+ static observedAttributes = ["center", "time", "speed", "scale", "labels", "labels-always", "covered", "roadster", "spacecraft", "orbiters", "asteroids", "orbiter", "orbiter-lines", "orbiter-track", "orbits", "help", "look", "light", "lang", "data", "arrows", "help-through", "help-place", "places", "surface-names", "site-flags", "earth-places", "earth-places-look", "all-moons", "far-opacity", "transition", "pace", "frame", "pair-with", "belts", "moon-size", ...NAME_LABEL_ATTRIBUTES];
147
148
  #model = newSpaceModel();
148
149
  #allMoons = false;
149
150
  #catalogue;
@@ -216,6 +217,7 @@ export class WakuseiSpace extends HTMLElement {
216
217
  if (craft !== null && craft !== "off")
217
218
  this.spacecraft = craft;
218
219
  this.orbiters = this.hasAttribute("orbiters") && this.getAttribute("orbiters") !== "off";
220
+ this.asteroids = this.hasAttribute("asteroids") && this.getAttribute("asteroids") !== "off";
219
221
  this.#model.orbiterChosen = this.getAttribute("orbiter") || null;
220
222
  this.#model.orbiterLines = orbiterLines(this.getAttribute("orbiter-lines"));
221
223
  this.#model.orbiterTrack = this.hasAttribute("orbiter-track") && this.getAttribute("orbiter-track") !== "off";
@@ -376,6 +378,8 @@ export class WakuseiSpace extends HTMLElement {
376
378
  this.spacecraft = value === null || value === "off" ? false : value;
377
379
  else if (name === "orbiters")
378
380
  this.orbiters = value !== null && value !== "off";
381
+ else if (name === "asteroids")
382
+ this.asteroids = value !== null && value !== "off";
379
383
  else if (name === "orbiter")
380
384
  this.orbiter = value || null;
381
385
  else if (name === "orbiter-lines")
@@ -869,6 +873,9 @@ export class WakuseiSpace extends HTMLElement {
869
873
  // a flight through a belt is at true distances: the squeezed scale does not map a camera that moves among the bodies
870
874
  if (value !== "true" && this.#flight !== null)
871
875
  this.leaveBelt();
876
+ // so is the ride with an asteroid
877
+ if (value !== "true" && this.#asteroidRide !== null)
878
+ this.ride(null);
872
879
  this.#nameScale();
873
880
  this.#gate?.request();
874
881
  }
@@ -927,6 +934,131 @@ export class WakuseiSpace extends HTMLElement {
927
934
  return this.#model.region;
928
935
  }
929
936
  #orbitersLoading = false;
937
+ #asteroidsLoading = false;
938
+ /** Resolved when the records of the asteroids are in: a `goTo` or `ride` to an asteroid asked before that waits for it. */
939
+ #asteroidsLoaded = (() => {
940
+ let resolve;
941
+ const promise = new Promise((done) => (resolve = done));
942
+ return { promise, resolve };
943
+ })();
944
+ /** The move to an asteroid that is waiting for the records, or null: any other move or call to stop takes it back. */
945
+ #asteroidMove = null;
946
+ /** The asteroid the camera is at (`goTo`) or riding with (`ride`), or null. */
947
+ #asteroidRide = null;
948
+ /**
949
+ * Place the asteroids of `/asteroids` in the view (the attribute `asteroids`; off by default): about a hundred bodies, each a point on its orbit on the date shown, drawn with the belts' points in the one instanced draw, and the
950
+ * nearest few within about an astronomical unit (the true scale) named, in the page's language. A name is pressable: `wakusei-body-click` (`{ id, name, kind: "small-body" }`) and `wakusei-asteroid-click` (`{ id, name }`)
951
+ * say which, and nothing else happens (a press does not go there: `goTo(id)` and `ride(id)` do). Each is an entry of `sight()` with `kind: "small-body"`. The records and their orbits are loaded when the option is first turned on and cost nothing
952
+ * before; with it off nothing is worked out or drawn for them. The positions are two-body: good near each body's epoch (`ASTEROID_ACCURACY` in `/asteroids`), less so far from it. `stats.asteroids` says how many were
953
+ * placed and named and what placing them cost.
954
+ *
955
+ * @example
956
+ * ```ts no-run
957
+ * import type { WakuseiSpace } from "@johnmorrisdotca/wakusei-planets/space/element";
958
+ *
959
+ * const space = document.querySelector<WakuseiSpace>("wakusei-space")!;
960
+ * space.addEventListener("wakusei-asteroid-click", (event) => console.log((event as CustomEvent<{ id: string; name: string }>).detail.name));
961
+ * space.asteroids = true;
962
+ * ```
963
+ */
964
+ get asteroids() {
965
+ return this.#model.asteroids;
966
+ }
967
+ set asteroids(value) {
968
+ this.#model.asteroids = value;
969
+ // an asteroid is no longer there to be at: the camera goes back where it was, and a move still waiting for the records is dropped
970
+ if (!value) {
971
+ this.#asteroidMove = null;
972
+ if (this.#asteroidRide !== null)
973
+ this.ride(null);
974
+ }
975
+ if (value && this.#model.asteroidSet === null && !this.#asteroidsLoading) {
976
+ this.#asteroidsLoading = true;
977
+ void import("../asteroids.js").then((mod) => {
978
+ const list = mod.asteroidList();
979
+ const source = mod.asteroidsSource();
980
+ if (this.#door.book.get(source.info.id) === undefined)
981
+ this.#door.book.addSource(source);
982
+ const of = (id) => list.find((one) => one.id === id);
983
+ this.#model.asteroidSet = {
984
+ ids: list.map((one) => one.id),
985
+ bodies: list.map((one) => mod.asteroidAsSmallBody(one)),
986
+ name: (id) => ({ ...(of(id)?.name ?? { en: id, ja: id }) }),
987
+ point: (id) => ({
988
+ id,
989
+ at: (ms) => {
990
+ const here = eclipticToScene(mod.asteroidPosition(id, ms));
991
+ const soon = eclipticToScene(mod.asteroidPosition(id, ms + 1000));
992
+ return { position: here, velocity: [soon[0] - here[0], soon[1] - here[1], soon[2] - here[2]] };
993
+ },
994
+ }),
995
+ distanceKm: (id) => Math.max(200, 8 * (of(id)?.diameterKm ?? 0)),
996
+ };
997
+ this.#door.book.enableSource(source.info.id, this.#model.asteroids);
998
+ this.#gate?.request();
999
+ this.#asteroidsLoaded.resolve();
1000
+ }, () => {
1001
+ this.#asteroidsLoading = false;
1002
+ });
1003
+ }
1004
+ else if (this.#model.asteroidSet !== null)
1005
+ this.#door.book.enableSource("wakusei-asteroids", value);
1006
+ this.#gate?.request();
1007
+ }
1008
+ /** Whether `key` is the view's key (`asteroidKey`) of one of the asteroids placed in the view. */
1009
+ #isAsteroid(key) {
1010
+ const id = asteroidIdOf(key);
1011
+ return id !== undefined && this.#model.asteroids && this.#model.asteroidSet !== null && this.#model.asteroidSet.ids.includes(id);
1012
+ }
1013
+ /**
1014
+ * The asteroid a `goTo` or `ride` names, as its plain id, or `undefined` where the name is none and the call goes on as it did: `eros`, or `asteroid:eros` (the form that always means the asteroid; `europa` and `dione`
1015
+ * are moons first). Throws a `RangeError` when it is one and the option `asteroids` is off, and, with the option on, for a name that is neither a body of the view nor an asteroid.
1016
+ */
1017
+ #asteroidNamed(target, call) {
1018
+ const lower = target.trim().toLowerCase();
1019
+ const prefixed = lower.startsWith(ASTEROID_PREFIX);
1020
+ const id = prefixed ? lower.slice(ASTEROID_PREFIX.length) : lower;
1021
+ if (!prefixed && findBody(target) !== undefined)
1022
+ return undefined;
1023
+ if (!ASTEROID_IDS.includes(id)) {
1024
+ if (prefixed || this.#model.asteroids)
1025
+ throw new RangeError(`${call}(${JSON.stringify(target)}): not a body of the view and not an asteroid of /asteroids (an asteroid's id is one of asteroidList(), such as "eros")`);
1026
+ return undefined;
1027
+ }
1028
+ if (!this.#model.asteroids)
1029
+ throw new RangeError(`${call}(${JSON.stringify(target)}): "${id}" is an asteroid of /asteroids, which the view places only with the option asteroids on (the attribute asteroids, or space.asteroids = true)`);
1030
+ return id;
1031
+ }
1032
+ /**
1033
+ * Go to or ride along with an asteroid: the camera moves to a point that goes round the Sun as the asteroid does, at the true scale (the squeezed scale does not map a camera that moves among the bodies, so it is
1034
+ * set to `true`, as `enterBelt` does). `goTo` keeps a distance from it looking at it (`follow`); `ride` sits at it looking at the Sun, with the way it goes at the top (`from`). With the records still being loaded, the move
1035
+ * is made when they are in, unless another move is asked for first.
1036
+ */
1037
+ #moveToAsteroid(id, call, options) {
1038
+ if (options.view === "beside")
1039
+ throw new RangeError(`${call}(${JSON.stringify(id)}): the view beside is for a moon`);
1040
+ const set = this.#model.asteroidSet;
1041
+ if (set === null) {
1042
+ const waiting = {};
1043
+ this.#asteroidMove = waiting;
1044
+ void this.#asteroidsLoaded.promise.then(() => {
1045
+ if (this.#asteroidMove !== waiting)
1046
+ return;
1047
+ this.#asteroidMove = null;
1048
+ if (this.#model.asteroids)
1049
+ this.#moveToAsteroid(id, call, options);
1050
+ });
1051
+ return true;
1052
+ }
1053
+ if (this.#model.mode !== "true")
1054
+ this.scale = "true";
1055
+ const view = call === "goTo" ? "follow" : (options.view ?? "from");
1056
+ this.ride(set.point(id), { ...options, around: "sun", view, distanceKm: options.distanceKm ?? set.distanceKm(id) });
1057
+ this.#asteroidRide = id;
1058
+ if (call === "goTo" && !this.hasAttribute("data-kazoku-bar"))
1059
+ this.#hands?.controller.set({ zoom: options.zoom ?? 1 }, true, "jump");
1060
+ return true;
1061
+ }
930
1062
  /**
931
1063
  * Show the spacecraft in orbit round the Moon and Mars (the attribute `orbiters`; off by default): each a small dot where `/orbiters` puts it on the date shown, scaled with its body, on its orbit line, with its
932
1064
  * name beside it in the page's language, once the body is at least 24 CSS pixels in radius on the screen. A modelled path is drawn dim, and a predicted one whose measured error is more than 2 per cent of its body's radius (`orbiterDim`); a craft with no path is not drawn. The paths (a 9 kB module)
@@ -1392,9 +1524,26 @@ export class WakuseiSpace extends HTMLElement {
1392
1524
  * `outer-planets`, `kuiper-belt`, `whole-system`) is a target too: the Sun is looked at and the camera stands back until the stage's orbits are all in the picture, with the stage's name shown. `wakusei-hop`
1393
1525
  * tells the page where it went and where the next and previous hops lead. A name in any case, in English or Japanese, works.
1394
1526
  *
1395
- * @param target - A body's name (`"saturn"`, `"Saturn"`, `"土星"`) or a stage's id.
1396
- * @param options - `smooth` (true by default), `zoom` (1 by default), `transition` and `duration`.
1527
+ *
1528
+ * With the option `asteroids` on it takes an asteroid's id from `/asteroids` too (`"eros"`; `"asteroid:europa"` and `"asteroid:dione"` are the forms that mean the asteroid, for `europa` and `dione` are moons first): the view
1529
+ * is set to the true scale (the squeezed scale cannot place a camera among the bodies, and going back to it ends the move), and the camera goes to a distance of a few times the asteroid's size (at least 200 km) looking at it
1530
+ * and keeps it there as the clock runs: `riding` says which asteroid (`riding.target.id`), `wakusei-ride` is sent as for any ride and `wakusei-hop` is not (an asteroid is no stop of the hopper), and a `goTo` of a body or `ride(null)` ends it.
1531
+ * Asked before the records are in, the move is made when they are, unless another move is asked for first.
1532
+ *
1533
+ * @param target - A body's name (`"saturn"`, `"Saturn"`, `"土星"`), a stage's id, or an asteroid's id.
1534
+ * @param options - `smooth` (true by default), `zoom` (1 by default), `transition` and `duration`; for an asteroid also `distanceKm`.
1397
1535
  * @returns Whether the view can go there.
1536
+ * @throws RangeError For an asteroid's id with the option `asteroids` off (the message says to turn it on), and, with it on, for a name that is no body of the view and no asteroid.
1537
+ *
1538
+ * @example
1539
+ * ```ts no-run
1540
+ * import type { WakuseiSpace } from "@johnmorrisdotca/wakusei-planets/space/element";
1541
+ *
1542
+ * const space = document.querySelector<WakuseiSpace>("wakusei-space")!;
1543
+ * space.asteroids = true;
1544
+ * space.goTo("eros");
1545
+ * console.log(space.riding?.target);
1546
+ * ```
1398
1547
  */
1399
1548
  goTo(target, options = {}) {
1400
1549
  const region = readRegion(target);
@@ -1402,6 +1551,10 @@ export class WakuseiSpace extends HTMLElement {
1402
1551
  this.#goToRegion(region, options);
1403
1552
  return true;
1404
1553
  }
1554
+ const asteroid = this.#asteroidNamed(target, "goTo");
1555
+ if (asteroid !== undefined)
1556
+ return this.#moveToAsteroid(asteroid, "goTo", options);
1557
+ this.#asteroidMove = null;
1405
1558
  const id = readBody(target);
1406
1559
  if (!id)
1407
1560
  return false;
@@ -1828,10 +1981,34 @@ export class WakuseiSpace extends HTMLElement {
1828
1981
  * controls' zoom narrows the view. A body of the view (`"moon"`, `"mars"`) goes round the Earth; a moon (`"io"`) goes round its planet; a point of your own (`{ at(ms) }`, a satellite, a probe) goes round `around` (the Earth by default) and the
1829
1982
  * element knows nothing of what it is. Another call switches smoothly; `null` stops. The draw rate follows how fast the picture moves, never more than 30 a second (one a second for a reader who asked for reduced motion), and nothing is drawn out of sight.
1830
1983
  *
1831
- * @param target - A body name, a moon name, a point, or `null` to stop.
1984
+ *
1985
+ * With the option `asteroids` on, an asteroid's id from `/asteroids` is a target too (`"apophis"`; `"asteroid:europa"` for the asteroid where the id is a moon's as well): the view is set to the true scale, the camera sits at the
1986
+ * asteroid looking at the Sun with the way it goes at the top (`view: "follow"` and `distanceKm` keep a distance and look at it instead, as `goTo` does), and `riding.target.id` is the asteroid's id.
1987
+ *
1988
+ * @param target - A body name, a moon name, an asteroid's id, a point, or `null` to stop.
1832
1989
  * @param options - `view`, `look`, `distanceKm`, `around`, `smooth` (see `RideOptions`).
1990
+ * @throws RangeError For an asteroid's id with the option `asteroids` off (the message says to turn it on), for the view `beside` with an asteroid, and, with the option on, for a name that is no body of the view and no asteroid.
1991
+ *
1992
+ * @example
1993
+ * ```ts no-run
1994
+ * import type { WakuseiSpace } from "@johnmorrisdotca/wakusei-planets/space/element";
1995
+ *
1996
+ * const space = document.querySelector<WakuseiSpace>("wakusei-space")!;
1997
+ * space.asteroids = true;
1998
+ * space.ride("apophis", { smooth: false });
1999
+ * space.ride(null);
2000
+ * ```
1833
2001
  */
1834
2002
  ride(target, options = {}) {
2003
+ if (typeof target === "string") {
2004
+ const asteroid = this.#asteroidNamed(target, "ride");
2005
+ if (asteroid !== undefined) {
2006
+ this.#moveToAsteroid(asteroid, "ride", options);
2007
+ return;
2008
+ }
2009
+ }
2010
+ this.#asteroidMove = null;
2011
+ this.#asteroidRide = null;
1835
2012
  const model = this.#model;
1836
2013
  const leaving = this.#leaving();
1837
2014
  const wasRiding = model.ride !== null;
@@ -2109,6 +2286,7 @@ export class WakuseiSpace extends HTMLElement {
2109
2286
  help: this.#model.helpDrawn ? { body: this.#model.helpDrawn.body, axis: [...this.#model.helpDrawn.axis], pole: [...this.#model.helpDrawn.pole], retrograde: this.#model.helpDrawn.retrograde, arcTurn: [...this.#model.helpDrawn.arcTurn] } : null,
2110
2287
  bodyLabels: { count: this.#model.labelsDrawn.count, focus: this.#model.labelsDrawn.focus === null ? null : { ...this.#model.labelsDrawn.focus } },
2111
2288
  orbiters: { count: this.#model.orbitersDrawn.count, status: { ...this.#model.orbitersDrawn.status }, dim: [...this.#model.orbitersDrawn.dim], lines: this.#model.orbitersDrawn.lines, track: this.#model.orbitersDrawn.track },
2289
+ asteroids: { ...this.#model.asteroidsDrawn },
2112
2290
  roadster: { shown: this.#model.roadsterDrawn.shown, position: this.#model.roadsterDrawn.position === null ? null : [...this.#model.roadsterDrawn.position], radiusPx: this.#model.roadsterDrawn.radiusPx },
2113
2291
  spacecraft: { count: this.#model.craftDrawn.count, status: { ...this.#model.craftDrawn.status }, drawn: [...this.#model.craftDrawn.drawn] },
2114
2292
  companion: this.#model.companion,
@@ -2166,6 +2344,8 @@ export class WakuseiSpace extends HTMLElement {
2166
2344
  return this.#names[id] ?? this.#model.craftNames?.(id, lang) ?? id;
2167
2345
  if (this.#model.orbiterSet !== null && this.#model.orbiterSet.ids.includes(id))
2168
2346
  return this.#names[id] ?? this.#model.orbiterSet.name(id)[lang];
2347
+ if (this.#isAsteroid(id))
2348
+ return this.#names[asteroidIdOf(id)] ?? this.#model.asteroidSet.name(asteroidIdOf(id))[lang];
2169
2349
  return this.#names[id] ?? BODIES[id].name[lang];
2170
2350
  }
2171
2351
  #placeLabels(places) {
@@ -2307,16 +2487,19 @@ export class WakuseiSpace extends HTMLElement {
2307
2487
  this.#hovered = id;
2308
2488
  this.#hoverBy = by;
2309
2489
  this.#markFar();
2310
- if (id !== null && (id === "roadster" || this.#model.craftIds.includes(id)))
2490
+ if (id !== null && this.#isAsteroid(id))
2491
+ this.dispatchEvent(new CustomEvent("wakusei-body-hover", { detail: { id: asteroidIdOf(id), name: this.#nameOf(id), kind: "small-body" }, bubbles: true, composed: true }));
2492
+ else if (id !== null && (id === "roadster" || this.#model.craftIds.includes(id)))
2311
2493
  this.dispatchEvent(new CustomEvent("wakusei-body-hover", { detail: { id, name: this.#nameOf(id), kind: "spacecraft" }, bubbles: true, composed: true }));
2312
2494
  else if (id !== null)
2313
2495
  this.dispatchEvent(new CustomEvent("wakusei-body-hover", { detail: { ...this.bodyInfo(id), system: systemOf(id) }, bubbles: true, composed: true }));
2314
2496
  else if (was !== null)
2315
2497
  this.dispatchEvent(new CustomEvent("wakusei-body-hover-end", { detail: { id: was }, bubbles: true, composed: true }));
2316
2498
  }
2317
- /** The body at a point of the element, in CSS pixels: on its disc (at least 14 pixels round its place) or on its name. */
2318
- #bodyAt(x, y) {
2319
- if (!this.#places || !this.#model.labels)
2499
+ /** The body at a point of the element, in CSS pixels: on its disc (at least 14 pixels round its place) or on its name. A press (`#up`) and the hover (`#pointAt`) both ask this, so what lights up under the pointer is what a press picks. */
2500
+ #bodyAt(x, y, pressing = false) {
2501
+ // the hover lights a system only with the names on; a press picks a body on its disc whether the names are shown or not
2502
+ if (!this.#places || (!pressing && !this.#model.labels))
2320
2503
  return null;
2321
2504
  let best = null;
2322
2505
  let bestD = Infinity;
@@ -2365,8 +2548,9 @@ export class WakuseiSpace extends HTMLElement {
2365
2548
  for (const [id, input] of Object.entries(bodies)) {
2366
2549
  const body = id;
2367
2550
  const orbiter = this.#model.orbiterSet !== null && this.#model.orbiterSet.ids.includes(id);
2368
- const craft = orbiter || this.#model.craftIds.includes(id) || id === "roadster";
2369
- const one = { id, kind: orbiter ? "orbiter" : craft ? "spacecraft" : bodyKindOf(body), ...sightOf(input, frame.width, frame.height, frame.axis), offAxis: input.offAxis ?? 0, distance: input.distance ?? 0, distanceKm: input.distanceKm ?? null };
2551
+ const asteroid = this.#isAsteroid(id);
2552
+ const craft = orbiter || asteroid || this.#model.craftIds.includes(id) || id === "roadster";
2553
+ const one = { id: asteroid ? asteroidIdOf(id) : id, kind: orbiter ? "orbiter" : asteroid ? "small-body" : craft ? "spacecraft" : bodyKindOf(body), ...sightOf(input, frame.width, frame.height, frame.axis), offAxis: input.offAxis ?? 0, distance: input.distance ?? 0, distanceKm: input.distanceKm ?? null };
2370
2554
  const host = craft ? undefined : hostOf(body);
2371
2555
  if (host !== undefined)
2372
2556
  one.host = host;
@@ -2438,6 +2622,8 @@ export class WakuseiSpace extends HTMLElement {
2438
2622
  return 3;
2439
2623
  if (this.#model.orbiterSet !== null && this.#model.orbiterSet.ids.includes(id))
2440
2624
  return 5;
2625
+ if (this.#isAsteroid(id))
2626
+ return 6;
2441
2627
  const kind = bodyKindOf(id);
2442
2628
  return kind === "moon" ? 4 : kind === "planet" || kind === "star" ? 2 : 3;
2443
2629
  }
@@ -2533,19 +2719,8 @@ export class WakuseiSpace extends HTMLElement {
2533
2719
  if (!this.#places)
2534
2720
  return;
2535
2721
  const rect = this.getBoundingClientRect();
2536
- const x = event.clientX - rect.left;
2537
- const y = event.clientY - rect.top;
2538
- let best;
2539
- let bestD = Infinity;
2540
- for (const [id, at] of Object.entries(this.#places)) {
2541
- if (!at)
2542
- continue;
2543
- const d = Math.hypot(x - at.x, y - at.y);
2544
- if (d <= Math.max(at.r, 14) && d < bestD) {
2545
- best = id;
2546
- bestD = d;
2547
- }
2548
- }
2722
+ // the one hit test the hover uses: a body's disc (14 pixels at least) or, failing that, a drawn name's box, so a press on a name picks its body
2723
+ const best = this.#bodyAt(event.clientX - rect.left, event.clientY - rect.top, true);
2549
2724
  if (best)
2550
2725
  this.#press(best);
2551
2726
  };
@@ -2570,6 +2745,12 @@ export class WakuseiSpace extends HTMLElement {
2570
2745
  */
2571
2746
  #press(id) {
2572
2747
  // the Roadster and the orbiters are no bodies of the package: a press on such a name says so and goes nowhere
2748
+ if (this.#isAsteroid(id)) {
2749
+ // an asteroid is no body of the package: the press says which one, in the body event and in an event of its own, and goes nowhere
2750
+ this.dispatchEvent(new CustomEvent("wakusei-body-click", { detail: { id: asteroidIdOf(id), name: this.#nameOf(id), kind: "small-body" }, bubbles: true, composed: true }));
2751
+ this.dispatchEvent(new CustomEvent("wakusei-asteroid-click", { detail: { id: asteroidIdOf(id), name: this.#nameOf(id) }, bubbles: true, composed: true }));
2752
+ return;
2753
+ }
2573
2754
  if (id === "roadster" || this.#model.craftIds.includes(id) || (this.#model.orbiterSet !== null && this.#model.orbiterSet.ids.includes(id))) {
2574
2755
  this.dispatchEvent(new CustomEvent("wakusei-body-click", { detail: { id, name: this.#nameOf(id), kind: "spacecraft" }, bubbles: true, composed: true }));
2575
2756
  // an orbiter is told in an event of its own as well (`{ id, name }`, the id `orbiter` takes), the one above unchanged
@@ -2591,8 +2772,9 @@ export class WakuseiSpace extends HTMLElement {
2591
2772
  if (!id)
2592
2773
  return;
2593
2774
  // a craft's name is no body: focus tells the page its id and name, as a press does
2594
- const craft = id === "roadster" || this.#model.craftIds.includes(id) || (this.#model.orbiterSet !== null && this.#model.orbiterSet.ids.includes(id));
2595
- this.dispatchEvent(new CustomEvent("wakusei-body-focus", { detail: craft ? { id, name: this.#nameOf(id), kind: "spacecraft" } : this.bodyInfo(id), bubbles: true, composed: true }));
2775
+ const asteroid = this.#isAsteroid(id);
2776
+ const craft = asteroid || id === "roadster" || this.#model.craftIds.includes(id) || (this.#model.orbiterSet !== null && this.#model.orbiterSet.ids.includes(id));
2777
+ this.dispatchEvent(new CustomEvent("wakusei-body-focus", { detail: craft ? { id: asteroid ? asteroidIdOf(id) : id, name: this.#nameOf(id), kind: asteroid ? "small-body" : "spacecraft" } : this.bodyInfo(id), bubbles: true, composed: true }));
2596
2778
  this.#hover(id, "focus");
2597
2779
  };
2598
2780
  // --- the rest --------------------------------------------------------------------------------------------------------------------------------------
@@ -3,6 +3,7 @@ import type { Item3D } from "@johnmorrisdotca/kazoku-ui/renderer";
3
3
  import { type BodyId, type MoonId } from "../facts.ts";
4
4
  import type { ThreeParts } from "../scene/body.ts";
5
5
  import type { SourceBook } from "../door/book.ts";
6
+ import type { SmallBody } from "../door/small-bodies.ts";
6
7
  import { type MoonDot } from "./all-moons.ts";
7
8
  import { type BeltId } from "../belts/index.ts";
8
9
  import { type PaintLabel, type WakuseiPainter } from "../scene/painter.ts";
@@ -72,6 +73,8 @@ export type SpaceLight = "sun" | "lit";
72
73
  * ```
73
74
  */
74
75
  export interface RidePoint {
76
+ /** What the point is, where it is something with an id: for the point `ride` and `goTo` make of an asteroid, the asteroid's id from `/asteroids`. A point of your own needs none. */
77
+ readonly id?: string;
75
78
  at(ms: number): {
76
79
  position: SpaceVec3;
77
80
  velocity: SpaceVec3;
@@ -139,6 +142,26 @@ export interface OrbiterSet {
139
142
  ja: string;
140
143
  };
141
144
  }
145
+ /** The asteroids of `/asteroids` as the view needs them: loaded only when they are shown. The bodies are in the data door's form, in the order of `ids`. */
146
+ export interface AsteroidSet {
147
+ ids: readonly string[];
148
+ bodies: readonly SmallBody[];
149
+ name(id: string): {
150
+ en: string;
151
+ ja: string;
152
+ };
153
+ /** The asteroid as a moving point to ride, round the Sun: kilometres and kilometres a second, ecliptic J2000 turned to the scene's axes. */
154
+ point(id: string): RidePoint;
155
+ /** A length to keep from it when going to it, in kilometres: a few times its measured size, 200 at the least. */
156
+ distanceKm(id: string): number;
157
+ }
158
+ /** The most asteroid names shown at once, the nearest to the camera within `ASTEROID_NAME_RANGE`. */
159
+ export declare const ASTEROID_NAMES = 12;
160
+ /** How near an asteroid must be to the camera to be named, in astronomical units in the true scale and in the view's units in the compressed one (where the whole system is some 250 across). */
161
+ export declare const ASTEROID_NAME_RANGE: {
162
+ readonly true: 1;
163
+ readonly compressed: 40;
164
+ };
142
165
  export interface SpaceModel {
143
166
  /** Moment shown, milliseconds since the epoch. */
144
167
  time: number;
@@ -256,6 +279,15 @@ export interface SpaceModel {
256
279
  lines: number;
257
280
  track: number;
258
281
  };
282
+ /** Place the asteroids of `/asteroids` in the view (the attribute `asteroids`): a point each, and a name for the few within `ASTEROID_NAME_RANGE`. Off by default; they are loaded when first shown. */
283
+ asteroids: boolean;
284
+ asteroidSet: AsteroidSet | null;
285
+ /** The asteroids at the last draw: how many were placed, how many were named, and the milliseconds the places took. */
286
+ asteroidsDrawn: {
287
+ placed: number;
288
+ named: number;
289
+ ms: number;
290
+ };
259
291
  /** The craft whose ground track is painted on its body, or null. */
260
292
  paint: PaintSpec | null;
261
293
  /** The trail buffer the element and the scene share. */
@@ -1,5 +1,6 @@
1
1
  import { BODIES, MOONS_OF } from "../facts.js";
2
- import { createSmallBodyPoints } from "../door/points.js";
2
+ import { createSmallBodyPoints, smallBodyPlace } from "../door/points.js";
3
+ import { asteroidKey } from "./asteroid-ids.js";
3
4
  import { createMoonDots } from "./all-moons.js";
4
5
  import { BELT_IDS, BELTS, OORT } from "../belts/index.js";
5
6
  import { toTerrestrialTime } from "../clock.js";
@@ -14,7 +15,7 @@ import { arrowSites, ARROWS_PER_ORBIT, chevron, helpSpin, MAX_ARROWS_PER_ORBIT }
14
15
  import { besideMoon } from "./beside.js";
15
16
  import { coveredBy, coverOf } from "./covered.js";
16
17
  import { spinAxis } from "./spin.js";
17
- import { createTrack, EARTH_RADIUS_KM, eclipticToScene, frameAt, moonOrbit, planetOrbit, SPACE_DWARFS, SPACE_MINORS, SPACE_PLANETS } from "./frame.js";
18
+ import { AU_IN_EARTH_RADII, createTrack, EARTH_RADIUS_KM, eclipticToScene, frameAt, moonOrbit, planetOrbit, SPACE_DWARFS, SPACE_MINORS, SPACE_PLANETS } from "./frame.js";
18
19
  import { createPaintOverlay } from "./paint-overlay.js";
19
20
  import { createPaintTrail, groundUnder, paintMinute } from "../paint/trail.js";
20
21
  import { BALL_FRAGMENT, VERTEX } from "../scene/glsl.js";
@@ -74,6 +75,10 @@ const ROADSTER_ORBIT_KEEP_MS = 30 * 86_400_000;
74
75
  * ```
75
76
  */
76
77
  export const SPACE_BODIES = ["earth", "sun", "moon", ...SPACE_PLANETS, ...SPACE_DWARFS, ...SPACE_MINORS];
78
+ /** The most asteroid names shown at once, the nearest to the camera within `ASTEROID_NAME_RANGE`. */
79
+ export const ASTEROID_NAMES = 12;
80
+ /** How near an asteroid must be to the camera to be named, in astronomical units in the true scale and in the view's units in the compressed one (where the whole system is some 250 across). */
81
+ export const ASTEROID_NAME_RANGE = { true: 1, compressed: 40 };
77
82
  /**
78
83
  * The direction the camera looks from, held in the frame the view is set to. `aim` is the direction from the body in focus to the camera as the stars hold it (fixed since the anchor moment); `body` turns it with the body's
79
84
  * ground and `pair` with the direction to a companion, from the anchor moment to now, and a pair keeps the companion from passing behind the body. Records the frame it held in `model.frameDrawn`. Worked out in the draw
@@ -153,6 +158,9 @@ export const newSpaceModel = () => ({
153
158
  orbiterLines: "all",
154
159
  orbiterTrack: false,
155
160
  orbitersDrawn: { count: 0, status: {}, dim: [], lines: 0, track: 0 },
161
+ asteroids: false,
162
+ asteroidSet: null,
163
+ asteroidsDrawn: { placed: 0, named: 0, ms: 0 },
156
164
  paint: null,
157
165
  paintTrail: createPaintTrail(),
158
166
  roadster: false,
@@ -1280,6 +1288,21 @@ export function createSpaceItem(model, deps) {
1280
1288
  for (const [id, read] of deps.book.recordReads())
1281
1289
  used[id] = id === "wakusei-belts" ? model.door.belts : read.records.length;
1282
1290
  model.door.used = used;
1291
+ // the asteroids of `/asteroids`, where the option is on: each placed as the data door's points are (the same mapping), the nearest few within range named; nothing is worked out with the option off
1292
+ const asteroidAts = [];
1293
+ const asteroidSet = model.asteroids ? model.asteroidSet : null;
1294
+ if (asteroidSet !== null) {
1295
+ const began = performance.now();
1296
+ const jd = toTerrestrialTime(model.time) / 86_400_000 + 2_440_587.5;
1297
+ for (let k = 0; k < asteroidSet.ids.length; k += 1) {
1298
+ const at = smallBodyPlace(mode, sunPos, asteroidSet.bodies[k], jd);
1299
+ asteroidAts.push({ id: asteroidKey(asteroidSet.ids[k]), at, away: Math.hypot(at[0] - camera.position.x, at[1] - camera.position.y, at[2] - camera.position.z) });
1300
+ }
1301
+ const range = mode === "true" ? ASTEROID_NAME_RANGE.true * AU_IN_EARTH_RADII : ASTEROID_NAME_RANGE.compressed;
1302
+ model.asteroidsDrawn = { placed: asteroidAts.length, named: Math.min(ASTEROID_NAMES, asteroidAts.filter((one) => one.away <= range).length), ms: performance.now() - began };
1303
+ }
1304
+ else if (model.asteroidsDrawn.placed !== 0)
1305
+ model.asteroidsDrawn = { placed: 0, named: 0, ms: 0 };
1283
1306
  if (deps.onLabels && model.labels) {
1284
1307
  const places = { ...moonPlaces };
1285
1308
  for (const id of SPACE_BODIES) {
@@ -1287,6 +1310,15 @@ export function createSpaceItem(model, deps) {
1287
1310
  const drawing = slots.get(id)?.drawing;
1288
1311
  places[id] = at ? { x: at.x, y: at.y, r: px[id], named: drawing !== undefined && drawing.object.visible && drawing.label?.shown === true } : null;
1289
1312
  }
1313
+ if (asteroidAts.length > 0) {
1314
+ const range = mode === "true" ? ASTEROID_NAME_RANGE.true * AU_IN_EARTH_RADII : ASTEROID_NAME_RANGE.compressed;
1315
+ for (const one of asteroidAts.filter((near) => near.away <= range).sort((x, y) => x.away - y.away).slice(0, ASTEROID_NAMES)) {
1316
+ const spot = labelFor(one.at);
1317
+ places[one.id] = spot ? { x: spot.x, y: spot.y, r: 1 } : null;
1318
+ if (spot)
1319
+ moonDistances[one.id] = one.away;
1320
+ }
1321
+ }
1290
1322
  for (const [id, { at, px: dotPx }] of Object.entries(orbiterPlaces)) {
1291
1323
  const spot = labelFor(at);
1292
1324
  places[id] = spot ? { x: spot.x, y: spot.y, r: dotPx } : null;
@@ -1371,6 +1403,8 @@ export function createSpaceItem(model, deps) {
1371
1403
  sight(craft.id, craft.at, craft.px);
1372
1404
  for (const [id, { at, px: dotPx }] of Object.entries(orbiterPlaces))
1373
1405
  sight(id, at, dotPx);
1406
+ for (const one of asteroidAts)
1407
+ sight(one.id, one.at, 1);
1374
1408
  projected.set(0, 0, -1).applyMatrix4(camera.projectionMatrix);
1375
1409
  deps.onSight(sights, { width: size.width, height: size.height, axis: { x: ((projected.x + 1) / 2) * size.width, y: ((1 - projected.y) / 2) * size.height } });
1376
1410
  }
@@ -52,7 +52,7 @@ export declare class WakuseiSurface extends HTMLElement {
52
52
  /** The eyes' look now. Setting it turns the view (azimuth and pitch in degrees, `fov` the view's height in degrees); it is drawn at once. */
53
53
  get look(): Look;
54
54
  set look(value: Look);
55
- /** Where the eyes are: metres east and north of the site, and metres over the ground under them. */
55
+ /** Where the eyes are: metres east and north of the site, and metres over the ground under them. Standing, that is a person's height; while the rover is driven or the landing watched, it is the height of the view being drawn (the seat, the chase camera). */
56
56
  get position(): {
57
57
  east: number;
58
58
  north: number;