@johnmorrisdotca/chikyu-earth 0.41.0 → 0.43.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.
@@ -4,6 +4,8 @@ import { airFor, readAirThickness } from "../realistic/air.js";
4
4
  import { BODY_LABEL_ATTRIBUTES, bodyLabelText, loadMichroma, readBodyLabel } from "../realistic/body-label.js";
5
5
  import { createSkyHost, SKY_ATTRIBUTES, SKY_STYLE } from "../sky/host.js";
6
6
  import { cleanPlaces, readPlaceGroups, readPlaces } from "../markers/places.js";
7
+ import { readHereCity } from "../markers/here-city.js";
8
+ import { createHereCity } from "../markers/here-city-host.js";
7
9
  import { createPlaceLayer, PLACE_STYLE, placesLostNotice, readPlaceLook } from "../markers/place-layer.js";
8
10
  import { aimAt, findPlace, glideView } from "../markers/go-to-place.js";
9
11
  import { subsolarPoint } from "../realistic/sun.js";
@@ -11,6 +13,8 @@ import { SHORT_NAMES } from "../satellites/names.js";
11
13
  import { CITY_NAMES_ATTRIBUTES, CITY_NAMES_STYLE, createCityHost } from "../cities/host.js";
12
14
  import { BORDER_ATTRIBUTES, readBorders } from "../borders/lines.js";
13
15
  import { GRID_ATTRIBUTES, readGrid } from "../grid/lines.js";
16
+ import { readRoute, routeLeg, routeText } from "../route.js";
17
+ import { applyRoute } from "../route/layer.js";
14
18
  import { COUNTRY_NAMES_ATTRIBUTES } from "../country-names/geometry.js";
15
19
  import { COUNTRY_NAMES_STYLE, createCountryHost } from "../country-names/host.js";
16
20
  import { createGate, frameCap } from "../realistic/gate.js";
@@ -23,6 +27,7 @@ import { METEOR_NOTE_STYLE, prefersLessMotion, setMeteorNote } from "../meteors/
23
27
  import { MAX_TEXTURE } from "../realistic/tiers.js";
24
28
  import { AU_IN_EARTH_RADII, EARTH_RADIUS_KM, frameAt } from "./ephemeris.js";
25
29
  import { MAX_ORBIT_GROUP } from "./satellite-layer.js";
30
+ import { applySatelliteFocus, satelliteEmphasis, satelliteGroupRows, shellRadiusOf, zoomForPerspectiveShell } from "./satellite-focus.js";
26
31
  import { RIDE_FACES, rideTiltLimits, rideTurnDegrees } from "./ride.js";
27
32
  import { placeAround, placeMoon, placeSun, RADIUS } from "./scale.js";
28
33
  import { BODY_NAMES, createSpaceItem, DEFAULT_EARTH_SIZE, newSpaceModel } from "./scene.js";
@@ -149,7 +154,7 @@ const STYLE = `:host{display:block;aspect-ratio:1/1;position:relative;overflow:h
149
154
  * ```
150
155
  */
151
156
  export class ChikyuSpace extends HTMLElement {
152
- static observedAttributes = ["cloud-drift", "camera", "earth-size", "earth-x", "earth-y", "center", "time", "speed", "scale", "labels", "orbits", "bodies", "light", "meteors", "meteors-label", "meteors-strength", ...BODY_LABEL_ATTRIBUTES, ...SKY_ATTRIBUTES, ...CITY_NAMES_ATTRIBUTES, ...BORDER_ATTRIBUTES, ...GRID_ATTRIBUTES, ...COUNTRY_NAMES_ATTRIBUTES, "air-thickness", "air-label", "lang", "places", "places-look", "place-groups", "bodies-credit", "satellite-tint", "tiles"];
157
+ static observedAttributes = ["cloud-drift", "camera", "earth-size", "earth-x", "earth-y", "center", "time", "speed", "scale", "labels", "orbits", "bodies", "light", "meteors", "meteors-label", "meteors-strength", ...BODY_LABEL_ATTRIBUTES, ...SKY_ATTRIBUTES, ...CITY_NAMES_ATTRIBUTES, ...BORDER_ATTRIBUTES, ...GRID_ATTRIBUTES, ...COUNTRY_NAMES_ATTRIBUTES, "air-thickness", "air-label", "lang", "places", "places-look", "place-groups", "here-city", "route", "bodies-credit", "satellite-tint", "tiles"];
153
158
  #model = newSpaceModel();
154
159
  #paint = createPaintState();
155
160
  #mode = "now";
@@ -189,6 +194,8 @@ export class ChikyuSpace extends HTMLElement {
189
194
  #places;
190
195
  #chosenLabel;
191
196
  #focusBeforeRide = "Earth";
197
+ /** The yaw, tilt and zoom from before a satellite group was framed, put back when the focus is cleared. */
198
+ #focusView = null;
192
199
  #lostTold = false;
193
200
  #names = {};
194
201
  #hint = "choose to look at it";
@@ -197,6 +204,8 @@ export class ChikyuSpace extends HTMLElement {
197
204
  #ridden = {};
198
205
  #toldFacing = null;
199
206
  #placeLayer;
207
+ /** The nearest city's name on a `here` marker (`here-city`), loaded only when that is on. */
208
+ #here;
200
209
  /** Whether the page has been told that places in sight were left undrawn. */
201
210
  #placesLost = { told: false };
202
211
  #placeList = [];
@@ -250,8 +259,9 @@ export class ChikyuSpace extends HTMLElement {
250
259
  this.#sky.setStyle(this.#skyStyle);
251
260
  this.#sky.setKeepClear(this.#keepClear);
252
261
  this.#placeLayer = createPlaceLayer(root.getElementById("places"));
262
+ this.#here = this.#openHereCity();
253
263
  this.#placeList = readPlaces(this.getAttribute("places"));
254
- this.#placeLayer.set(this.#placeList);
264
+ this.#placeLayer.set(this.#here.named(this.#placeList));
255
265
  this.#placeLayer.look = readPlaceLook(this.getAttribute("places-look"));
256
266
  this.#placeLayer.grouping = readPlaceGroups(this.getAttribute("place-groups"));
257
267
  this.#cities = createCityHost({
@@ -373,6 +383,8 @@ export class ChikyuSpace extends HTMLElement {
373
383
  this.#taken = undefined;
374
384
  this.#sky?.dispose();
375
385
  this.#sky = undefined;
386
+ this.#here?.dispose();
387
+ this.#here = undefined;
376
388
  this.#placeLayer?.dispose();
377
389
  this.#placeLayer = undefined;
378
390
  this.#cities?.dispose();
@@ -426,6 +438,7 @@ export class ChikyuSpace extends HTMLElement {
426
438
  this.#sky?.refresh();
427
439
  this.#cities?.refresh();
428
440
  this.#countries?.refresh();
441
+ this.#here?.refresh();
429
442
  }
430
443
  }
431
444
  else if (BODY_LABEL_ATTRIBUTES.includes(name))
@@ -434,8 +447,11 @@ export class ChikyuSpace extends HTMLElement {
434
447
  this.#sky?.refresh();
435
448
  else if (name === "places") {
436
449
  this.#placeList = readPlaces(value);
437
- this.#placeLayer?.set(this.#placeList);
438
- this.#gate?.request();
450
+ this.#applyPlaces();
451
+ }
452
+ else if (name === "here-city") {
453
+ this.#here?.setLimit(readHereCity(value));
454
+ this.#applyPlaces();
439
455
  }
440
456
  else if (name === "places-look" && this.#placeLayer) {
441
457
  this.#placeLayer.look = readPlaceLook(value);
@@ -445,6 +461,10 @@ export class ChikyuSpace extends HTMLElement {
445
461
  this.#placeLayer.grouping = readPlaceGroups(value);
446
462
  this.#gate?.request();
447
463
  }
464
+ else if (name === "route") {
465
+ if (applyRoute(this.#model.route, readRoute(value)))
466
+ this.#gate?.request();
467
+ }
448
468
  else if (CITY_NAMES_ATTRIBUTES.includes(name))
449
469
  this.#cities?.refresh();
450
470
  else if (name === "borders") {
@@ -508,7 +528,8 @@ export class ChikyuSpace extends HTMLElement {
508
528
  note.textContent = credit;
509
529
  if (note.lang !== lang)
510
530
  note.lang = lang;
511
- const hidden = credit === "" || this.bodies === "plain" || this.getAttribute("bodies-credit") === "off";
531
+ // the painter holds no picture while no body is big enough in the view for its map: a painter that has made nothing names all its pictures, so no line then
532
+ const hidden = credit === "" || this.#model.painted.mapped === 0 || this.bodies === "plain" || this.getAttribute("bodies-credit") === "off";
512
533
  if (note.hidden !== hidden)
513
534
  note.hidden = hidden;
514
535
  }
@@ -640,6 +661,77 @@ export class ChikyuSpace extends HTMLElement {
640
661
  else
641
662
  this.setAttribute("places", JSON.stringify(clean));
642
663
  }
664
+ /**
665
+ * Name each `here` place with the nearest city (the attribute `here-city`). `false` (the default) leaves the page's label and loads nothing.
666
+ * `true` looks within 100 km (`HERE_CITY_KM`). A number is that limit in kilometres, at most 20,000 (`HERE_CITY_MAX_KM`); a city further away is not used, and the page's label stays.
667
+ * The cities are kuni's capitals, the other cities of a million and the others of 100,000, an optional peer loaded only when this is on and a `here` place is set. The name follows the page's language
668
+ * (the short Japanese name, then the Japanese name, else English). `places` is not changed: only the drawn name is. Nothing is drawn again at rest.
669
+ *
670
+ * @example
671
+ * ```ts no-run
672
+ * import type { ChikyuSpace } from "@johnmorrisdotca/chikyu-earth/space/element";
673
+ *
674
+ * const space = document.querySelector("chikyu-space") as ChikyuSpace;
675
+ * space.hereCity = true;
676
+ * console.log(space.hereCity); // 100
677
+ * space.hereCity = false;
678
+ * ```
679
+ */
680
+ get hereCity() {
681
+ return readHereCity(this.getAttribute("here-city"));
682
+ }
683
+ set hereCity(value) {
684
+ if (value === true)
685
+ this.setAttribute("here-city", "");
686
+ else if (value === false)
687
+ this.removeAttribute("here-city");
688
+ else {
689
+ const km = readHereCity(String(value));
690
+ if (km === false)
691
+ this.removeAttribute("here-city");
692
+ else
693
+ this.setAttribute("here-city", String(km));
694
+ }
695
+ }
696
+ /** The cities for `here-city`, asked for only when the attribute is on. */
697
+ #openHereCity() {
698
+ const host = createHereCity({
699
+ language: () => ((this.closest("[lang]")?.getAttribute("lang") ?? "").toLowerCase().startsWith("ja") ? "ja" : "en"),
700
+ onChange: () => {
701
+ if (this.isConnected)
702
+ this.#applyPlaces();
703
+ },
704
+ onError: (error) => this.dispatchEvent(new CustomEvent("chikyu-here-city-unavailable", { detail: { reason: error instanceof Error ? error.message : String(error) } })),
705
+ });
706
+ host.setLimit(readHereCity(this.getAttribute("here-city")));
707
+ return host;
708
+ }
709
+ /** Hand the places to the layer, with a here marker's city name when one is known, and draw once. */
710
+ #applyPlaces() {
711
+ const layer = this.#placeLayer;
712
+ if (!layer)
713
+ return;
714
+ layer.set(this.#here?.named(this.#placeList) ?? this.#placeList);
715
+ this.#gate?.request();
716
+ }
717
+ /**
718
+ * A route between two places (the attribute `route`, `lon,lat,lon,lat`). Null, the default, draws none. The line is the WGS84 geodesic, split at the date line, and it turns with the Earth.
719
+ * Setting the property writes the attribute. Nothing is drawn again at rest: the line is drawn with the picture, and only when the route or the view changes.
720
+ */
721
+ get route() {
722
+ return readRoute(this.getAttribute("route"));
723
+ }
724
+ set route(value) {
725
+ if (!value)
726
+ this.removeAttribute("route");
727
+ else
728
+ this.setAttribute("route", routeText(value));
729
+ }
730
+ /** The distance, bearings and flight-time estimate for `route`, or null when none is set. The flight time assumes 900 km/h. */
731
+ get routeInfo() {
732
+ const route = this.route;
733
+ return route ? routeLeg(route.from, route.to) : null;
734
+ }
643
735
  /**
644
736
  * Turn the view so a place of `places` is in the middle, at a pace a reader can follow (at once with `smooth: false`, for a reader who asked for less motion, or before the picture is live); the zoom stays.
645
737
  * The place is named by its `id`, or by its label in any case. The camera must be looking at the Earth: while it rides along or looks at another body nothing moves. The tilt is held to 90 degrees either
@@ -1263,6 +1355,11 @@ export class ChikyuSpace extends HTMLElement {
1263
1355
  groups.delete(group);
1264
1356
  if (this.#model.satellites.selected?.group === group)
1265
1357
  this.#model.satellites.selected = null;
1358
+ if (this.#model.satellites.focus?.name === group) {
1359
+ this.#model.satellites.focus = null;
1360
+ this.#restoreFocusView();
1361
+ this.dispatchEvent(new CustomEvent("chikyu-satellite-focus", { detail: { group: null, count: 0 }, bubbles: true, composed: true }));
1362
+ }
1266
1363
  }
1267
1364
  else {
1268
1365
  const was = groups.get(group);
@@ -1289,9 +1386,67 @@ export class ChikyuSpace extends HTMLElement {
1289
1386
  this.#schedule();
1290
1387
  this.#gate?.request();
1291
1388
  }
1292
- /** The groups now set, with how many satellites each holds and whether it is shown. */
1389
+ /**
1390
+ * The groups now set. `count` is how many satellites the group holds, `visible` is the shown state from `showSatellites` (a focus does not change it), `placed` is how many the last draw placed (0 when the group is not drawn) and `emphasis` is how a focus treats it: `focus`, `dim`, `hidden` or `plain` (no focus).
1391
+ */
1293
1392
  get satelliteGroups() {
1294
- return [...this.#model.satellites.groups].map(([name, group]) => ({ name, count: group.satellites.count, visible: group.visible }));
1393
+ return satelliteGroupRows(this.#model.satellites);
1394
+ }
1395
+ /** The group `focusSatelliteGroup` is showing on its own, or `null`. */
1396
+ get focusedSatelliteGroup() {
1397
+ return this.#model.satellites.focus?.name ?? null;
1398
+ }
1399
+ /**
1400
+ * Show one group on its own. The others are drawn dimmer (`others: "dim"`, the default, through the same dimming an old element set uses) or not drawn (`"hide"`). `frame` (true by default) pulls the camera back, by this view's own zoom, until the group's orbits fit: the distance is the greatest apogee of the group, from each satellite's mean motion and eccentricity, and it stops at 8 Earth radii. `null` puts every group back to the shown state it had, and puts the camera back when it was moved. A name that `setSatellites` has not set returns `{ ok: false, reason: "not-set" }` and changes nothing. It does not throw. When the choice changes, `chikyu-satellite-focus` is sent with `{ group, count }`.
1401
+ *
1402
+ * @param name - The group's name, or `null` to clear the focus.
1403
+ * @param options - `others` and `frame`.
1404
+ * @returns `{ ok: true, group, count }` when the group is set (`group` is `null` and `count` is 0 when cleared). `{ ok: false, reason: "not-set" }` when it is not.
1405
+ */
1406
+ focusSatelliteGroup(name, options = {}) {
1407
+ const answer = applySatelliteFocus(this.#model.satellites, name, options);
1408
+ if (!answer.ok || answer.group === undefined || answer.count === undefined)
1409
+ return { ok: false, reason: "not-set" };
1410
+ if (answer.group === null)
1411
+ this.#restoreFocusView();
1412
+ else if (options.frame !== false)
1413
+ this.#frameSatelliteShell(answer.group);
1414
+ if (answer.changed)
1415
+ this.dispatchEvent(new CustomEvent("chikyu-satellite-focus", { detail: { group: answer.group, count: answer.count }, bubbles: true, composed: true }));
1416
+ // which groups are drawn decides how often time is drawn, so the next step is worked out again, as for `showSatellites`
1417
+ this.#schedule();
1418
+ this.#gate?.request();
1419
+ return { ok: true, group: answer.group, count: answer.count };
1420
+ }
1421
+ /** The view's own zoom, far enough that the group's shell fills the picture. A ride is left, because the camera has to look at the Earth to see the shell. */
1422
+ #frameSatelliteShell(name) {
1423
+ const group = this.#model.satellites.groups.get(name);
1424
+ if (!group)
1425
+ return;
1426
+ const box = this.getBoundingClientRect();
1427
+ if (box.width < 2 || box.height < 2)
1428
+ return;
1429
+ if (!this.#focusView)
1430
+ this.#focusView = { yaw: this.#model.view.yaw, tilt: this.#model.view.tilt, zoom: this.#model.view.zoom };
1431
+ if (this.#model.ride)
1432
+ this.ride(null);
1433
+ const zoom = zoomForPerspectiveShell(shellRadiusOf(group.satellites.sets), this.earthSize, box.width / box.height, LIMITS.zoomMin, LIMITS.zoomMax);
1434
+ this.#moveView({ yaw: this.#model.view.yaw, tilt: this.#model.view.tilt, zoom });
1435
+ }
1436
+ /** Put back the yaw, tilt and zoom from before the shell was framed. */
1437
+ #restoreFocusView() {
1438
+ const saved = this.#focusView;
1439
+ this.#focusView = null;
1440
+ if (saved)
1441
+ this.#moveView(saved);
1442
+ }
1443
+ /** Hand a camera to the controls that are driving the view, or to the view itself when those are not there yet. */
1444
+ #moveView(view) {
1445
+ const controls = this.#controls();
1446
+ if (controls)
1447
+ controls.set(view, true, "satellites");
1448
+ else
1449
+ this.setView(view, true);
1295
1450
  }
1296
1451
  /** Mark one satellite: a larger ring round it, its orbit drawn, and its name beside it. `null` lets it go. */
1297
1452
  selectSatellite(ref) {
@@ -1321,6 +1476,16 @@ export class ChikyuSpace extends HTMLElement {
1321
1476
  this.#model.satellites.marked.delete(group);
1322
1477
  this.#gate?.request();
1323
1478
  }
1479
+ /**
1480
+ * Ring and name these satellites, besides any group `labelSatellites` names. Pass an empty list to take them off. One draw, then nothing at rest.
1481
+ *
1482
+ * @param refs - The satellites, by group and index, as `setSatellites` knows them.
1483
+ */
1484
+ ringSatellites(refs) {
1485
+ this.#model.satellites.pinned = refs.map((ref) => ({ group: ref.group, index: ref.index }));
1486
+ this.#gate?.request();
1487
+ this.#gate?.soon();
1488
+ }
1324
1489
  /** The groups whose satellites are ringed and named now (`labelSatellites`). */
1325
1490
  get labelledSatellites() {
1326
1491
  return [...this.#model.satellites.marked];
@@ -1337,8 +1502,7 @@ export class ChikyuSpace extends HTMLElement {
1337
1502
  this.#gate?.request();
1338
1503
  }
1339
1504
  /**
1340
- * Where a satellite is on the screen as of the last draw: `{ x, y, visible, heightKm }` in CSS pixels from the view's top left, `visible` meaning it is inside the box and not behind the Earth. `null` when its group is not set or not shown, it cannot be
1341
- * placed (its elements are too old), or it is behind the camera. It is the picture's own camera maths and its own test for the Earth in the way, so a page that draws its own rings need not repeat either.
1505
+ * Where a satellite is on the screen as of the last draw: `{ x, y, visible, heightKm, edge, behind }` in CSS pixels from the view's top left. `visible` means it is inside the box, in front of the camera and not behind the Earth. `edge` is `top`, `right`, `bottom` or `left` when the place lies beyond that edge, and `null` when it is inside the box. `behind` is true when it is behind the camera (the picture would mirror it, so `edge` is read from the camera's own axes). `x` and `y` may lie outside the box. `null` when its group is not set or not shown, or it cannot be placed (its elements are too old). It is the picture's own camera maths and its own test for the Earth in the way, so a page that draws its own rings, or a mark on the edge, need not repeat either.
1342
1506
  */
1343
1507
  placeSatellite(ref) {
1344
1508
  return this.#model.placeSatellite?.(ref) ?? null;
@@ -1456,16 +1620,18 @@ export class ChikyuSpace extends HTMLElement {
1456
1620
  satellites: this.#satelliteStats(),
1457
1621
  ride: { riding: this.#model.ride !== null, lost: this.#model.rideLost, gliding: this.#model.transition !== null, facing: this.facing },
1458
1622
  camera: { position: [this.#model.pose.p[0], this.#model.pose.p[1], this.#model.pose.p[2]], quaternion: [this.#model.pose.q[0], this.#model.pose.q[1], this.#model.pose.q[2], this.#model.pose.q[3]], fov: this.#model.pose.fov },
1459
- bodies: { look: this.#model.bodies || painter?.looks?.[0] || "plain", painted: this.#model.painted.bodies, moons: this.#model.painted.moons, held: this.#model.painted.held, moon: this.#model.painted.moon, tiles: this.#model.painted.tiles },
1623
+ bodies: { look: this.#model.bodies || painter?.looks?.[0] || "plain", painted: this.#model.painted.bodies, moons: this.#model.painted.moons, held: this.#model.painted.held, moon: this.#model.painted.moon, tiles: this.#model.painted.tiles, mapped: this.#model.painted.mapped, pending: this.#model.painted.pending },
1460
1624
  meteors: { on: this.#model.meteors.on, strength: this.#model.meteors.strength, ...this.#model.meteors.drawn },
1461
1625
  bodyLabel: { on: this.#model.bodyLabel.settings !== null, text: this.#model.bodyLabel.text, ...this.#model.bodyLabel.drawn },
1462
1626
  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: [] },
1463
1627
  followModel: this.#model.followModel,
1464
1628
  tiles: this.#model.tiles?.stats ?? { ready: false, level: null, held: 0, shown: 0, loading: 0, failed: 0, requested: 0, heldBytes: 0, refused: 0, used: {} },
1465
1629
  places: { ...(this.#placeLayer?.stats ?? { places: 0, shown: 0, labels: 0, renders: 0, rings: 0, satelliteNames: 0, marks: 0, groups: 0, sprites: 0, dots: 0, omitted: 0 }) },
1630
+ hereCity: this.#here?.stats ?? { on: false, maxKm: 0, loaded: false, pending: false },
1466
1631
  cityNames: this.#cities?.stats ?? { on: false, max: 0, radius: 0, tiers: [], cities: 0, names: 0, renders: 0 },
1467
1632
  borders: { ...this.#model.borders.stats, shown: { ...this.#model.borders.stats.shown }, loaded: [...this.#model.borders.stats.loaded] },
1468
1633
  grid: { ...this.#model.grid.stats },
1634
+ route: { ...this.#model.route.stats },
1469
1635
  countryNames: this.#countries?.stats ?? { on: false, max: 0, countries: 0, names: 0, renders: 0, failed: false },
1470
1636
  };
1471
1637
  }
@@ -1492,8 +1658,8 @@ export class ChikyuSpace extends HTMLElement {
1492
1658
  }
1493
1659
  /** Is anything on show that moves faster than the Earth turns: a group of satellites, including above the interpolation limit where they are drawn as streaks? */
1494
1660
  #satellitesMoving() {
1495
- for (const group of this.#model.satellites.groups.values())
1496
- if (group.visible && group.satellites.count > 0)
1661
+ for (const [name, group] of this.#model.satellites.groups)
1662
+ if (satelliteEmphasis(name, group.visible, this.#model.satellites.focus).drawn && group.satellites.count > 0)
1497
1663
  return true;
1498
1664
  return false;
1499
1665
  }
@@ -0,0 +1,90 @@
1
+ import type { ElementSet } from "../satellites/types.ts";
2
+ import type { SatelliteState } from "./satellite-layer.ts";
3
+ /** What happens to the groups that are not the one in focus. `dim` draws them dimmer. `hide` does not draw them. */
4
+ export type SatelliteOthers = "dim" | "hide";
5
+ /** The group a view is showing on its own, and what it does with the others. `null` when every group is as the page set it. */
6
+ export interface SatelliteFocus {
7
+ name: string;
8
+ others: SatelliteOthers;
9
+ }
10
+ /** How one group is drawn while a focus is set. `focus` is the chosen group, `dim` a group drawn dimmer, `hidden` a group not drawn, `plain` a group with no focus in force. */
11
+ export type SatelliteEmphasis = "focus" | "dim" | "hidden" | "plain";
12
+ /** What `focusSatelliteGroup` returns. `reason: "not-set"` is a group `setSatellites` has not set; the call changes nothing and does not throw. */
13
+ export interface SatelliteFocusAnswer {
14
+ ok: boolean;
15
+ group?: string | null;
16
+ count?: number;
17
+ changed?: boolean;
18
+ reason?: "not-set";
19
+ }
20
+ /**
21
+ * Whether a group is drawn, and whether its dots take the dim value, for a focus. A group the page hid stays hidden unless it is the one in focus. The page's own shown state is not an input that this changes.
22
+ *
23
+ * @param name - The group's name.
24
+ * @param ownVisible - Whether the page is showing the group (`showSatellites`).
25
+ * @param focus - The focus in force, or `null`.
26
+ * @returns `drawn` (the group is placed), `dim` (1 to draw it dimmer, 0 to leave it) and `emphasis`.
27
+ */
28
+ export declare function satelliteEmphasis(name: string, ownVisible: boolean, focus: SatelliteFocus | null): {
29
+ drawn: boolean;
30
+ dim: number;
31
+ emphasis: SatelliteEmphasis;
32
+ };
33
+ /**
34
+ * Put the focus on one group, or clear it with `null`. A name that is not a group already set is refused.
35
+ *
36
+ * @param state - The view's satellite state.
37
+ * @param name - The group's name, or `null` to clear.
38
+ * @param options - `others` is `dim` when left out. `frame` is read by the element, not here.
39
+ * @returns The answer. `changed` is false when the focus was already that.
40
+ */
41
+ export declare function applySatelliteFocus(state: SatelliteState, name: string | null, options?: {
42
+ others?: SatelliteOthers;
43
+ }): SatelliteFocusAnswer;
44
+ /** One group as `satelliteGroups` reports it: the page's own shown state, how the focus treats it, and how many the last draw placed. */
45
+ export interface SatelliteGroupRow {
46
+ name: string;
47
+ count: number;
48
+ visible: boolean;
49
+ placed: number;
50
+ emphasis: SatelliteEmphasis;
51
+ }
52
+ /**
53
+ * The groups now set, in the order they were set, with the focus applied for reading. `placed` is 0 when the group is not drawn.
54
+ *
55
+ * @param state - The view's satellite state.
56
+ * @returns One row a group.
57
+ */
58
+ export declare function satelliteGroupRows(state: SatelliteState): SatelliteGroupRow[];
59
+ /** The greatest apogee a shell is pulled out to, in Earth radii. Past this, one far satellite would shrink the Earth instead of showing the group. */
60
+ export declare const SHELL_CAP = 8;
61
+ /**
62
+ * The shell of a group, in Earth radii from the Earth's centre: the greatest apogee of its element sets, from the mean motion (revolutions a day) and the eccentricity.
63
+ * One revolution a day is 6.62 Earth radii (Kepler's third law, an Earth of 6371 km). A group with no usable set is the Earth's own radius. The result is never above {@link SHELL_CAP}.
64
+ *
65
+ * @param sets - The group's element sets.
66
+ * @returns The shell, from 1 to 8.
67
+ */
68
+ export declare function shellRadiusOf(sets: readonly Pick<ElementSet, "MEAN_MOTION" | "ECCENTRICITY">[]): number;
69
+ /**
70
+ * The zoom that puts a shell of `shell` Earth radii inside the space view. At zoom 1 the Earth fills `earthSize` of the shorter side. The shell fills about 0.92 of that side.
71
+ *
72
+ * @param shell - The shell, in Earth radii.
73
+ * @param earthSize - The share of the shorter side the Earth fills at zoom 1.
74
+ * @param aspect - The view's width over its height.
75
+ * @param min - The least zoom the view allows.
76
+ * @param max - The greatest zoom the view allows.
77
+ * @returns The zoom.
78
+ */
79
+ export declare function zoomForPerspectiveShell(shell: number, earthSize: number, aspect: number, min: number, max: number): number;
80
+ /**
81
+ * The zoom that puts a shell of `shell` Earth radii inside the realistic Earth's picture. At zoom 1 the Earth and its glow fill the shorter side (the glow is 1.08 radii). The shell fills about 0.92 of that side.
82
+ *
83
+ * @param shell - The shell, in Earth radii.
84
+ * @param width - The view's width in CSS pixels.
85
+ * @param height - The view's height in CSS pixels.
86
+ * @param min - The least zoom the view allows.
87
+ * @param max - The greatest zoom the view allows.
88
+ * @returns The zoom.
89
+ */
90
+ export declare function zoomForOrthographicShell(shell: number, width: number, height: number, min: number, max: number): number;
@@ -0,0 +1,109 @@
1
+ /**
2
+ * Whether a group is drawn, and whether its dots take the dim value, for a focus. A group the page hid stays hidden unless it is the one in focus. The page's own shown state is not an input that this changes.
3
+ *
4
+ * @param name - The group's name.
5
+ * @param ownVisible - Whether the page is showing the group (`showSatellites`).
6
+ * @param focus - The focus in force, or `null`.
7
+ * @returns `drawn` (the group is placed), `dim` (1 to draw it dimmer, 0 to leave it) and `emphasis`.
8
+ */
9
+ export function satelliteEmphasis(name, ownVisible, focus) {
10
+ if (!focus)
11
+ return { drawn: ownVisible, dim: 0, emphasis: "plain" };
12
+ if (focus.name === name)
13
+ return { drawn: true, dim: 0, emphasis: "focus" };
14
+ if (focus.others === "hide" || !ownVisible)
15
+ return { drawn: false, dim: 0, emphasis: "hidden" };
16
+ return { drawn: true, dim: 1, emphasis: "dim" };
17
+ }
18
+ /**
19
+ * Put the focus on one group, or clear it with `null`. A name that is not a group already set is refused.
20
+ *
21
+ * @param state - The view's satellite state.
22
+ * @param name - The group's name, or `null` to clear.
23
+ * @param options - `others` is `dim` when left out. `frame` is read by the element, not here.
24
+ * @returns The answer. `changed` is false when the focus was already that.
25
+ */
26
+ export function applySatelliteFocus(state, name, options = {}) {
27
+ if (name === null) {
28
+ const changed = state.focus !== null;
29
+ state.focus = null;
30
+ return { ok: true, group: null, count: 0, changed };
31
+ }
32
+ const found = state.groups.get(name);
33
+ if (!found)
34
+ return { ok: false, reason: "not-set" };
35
+ const others = options.others === "hide" ? "hide" : "dim";
36
+ const changed = state.focus?.name !== name || state.focus.others !== others;
37
+ state.focus = { name, others };
38
+ return { ok: true, group: name, count: found.satellites.count, changed };
39
+ }
40
+ /**
41
+ * The groups now set, in the order they were set, with the focus applied for reading. `placed` is 0 when the group is not drawn.
42
+ *
43
+ * @param state - The view's satellite state.
44
+ * @returns One row a group.
45
+ */
46
+ export function satelliteGroupRows(state) {
47
+ return [...state.groups].map(([name, group]) => {
48
+ const choice = satelliteEmphasis(name, group.visible, state.focus);
49
+ return { name, count: group.satellites.count, visible: group.visible, placed: choice.drawn ? (group.placed ?? 0) : 0, emphasis: choice.emphasis };
50
+ });
51
+ }
52
+ /** The greatest apogee a shell is pulled out to, in Earth radii. Past this, one far satellite would shrink the Earth instead of showing the group. */
53
+ export const SHELL_CAP = 8;
54
+ /**
55
+ * The shell of a group, in Earth radii from the Earth's centre: the greatest apogee of its element sets, from the mean motion (revolutions a day) and the eccentricity.
56
+ * One revolution a day is 6.62 Earth radii (Kepler's third law, an Earth of 6371 km). A group with no usable set is the Earth's own radius. The result is never above {@link SHELL_CAP}.
57
+ *
58
+ * @param sets - The group's element sets.
59
+ * @returns The shell, from 1 to 8.
60
+ */
61
+ export function shellRadiusOf(sets) {
62
+ let far = 1;
63
+ for (const set of sets) {
64
+ if (!(set.MEAN_MOTION > 0) || !Number.isFinite(set.ECCENTRICITY))
65
+ continue;
66
+ const apogee = 6.62 * (1 / set.MEAN_MOTION) ** (2 / 3) * (1 + Math.max(0, set.ECCENTRICITY));
67
+ if (Number.isFinite(apogee))
68
+ far = Math.max(far, apogee);
69
+ }
70
+ return Math.min(SHELL_CAP, far);
71
+ }
72
+ /** The space view's field of view, in degrees. The same number `fitDistance` uses. */
73
+ const FOV = 35;
74
+ const RAD = Math.PI / 180;
75
+ /** The camera's distance, in radii, at which a body of radius 1 fills `size` of the view's shorter side. The same sum as `fitDistance`. */
76
+ const fitDistance = (size, aspect) => {
77
+ const across = Math.max(0.02, Math.min(50, size)) * Math.min(1, Math.max(0.05, aspect));
78
+ return Math.sqrt(1 + 1 / (across * Math.tan((FOV / 2) * RAD)) ** 2);
79
+ };
80
+ /**
81
+ * The zoom that puts a shell of `shell` Earth radii inside the space view. At zoom 1 the Earth fills `earthSize` of the shorter side. The shell fills about 0.92 of that side.
82
+ *
83
+ * @param shell - The shell, in Earth radii.
84
+ * @param earthSize - The share of the shorter side the Earth fills at zoom 1.
85
+ * @param aspect - The view's width over its height.
86
+ * @param min - The least zoom the view allows.
87
+ * @param max - The greatest zoom the view allows.
88
+ * @returns The zoom.
89
+ */
90
+ export function zoomForPerspectiveShell(shell, earthSize, aspect, min, max) {
91
+ const wanted = Math.max(1.02, shell) * fitDistance(0.92, aspect);
92
+ return Math.min(max, Math.max(min, fitDistance(earthSize, aspect) / wanted));
93
+ }
94
+ /**
95
+ * The zoom that puts a shell of `shell` Earth radii inside the realistic Earth's picture. At zoom 1 the Earth and its glow fill the shorter side (the glow is 1.08 radii). The shell fills about 0.92 of that side.
96
+ *
97
+ * @param shell - The shell, in Earth radii.
98
+ * @param width - The view's width in CSS pixels.
99
+ * @param height - The view's height in CSS pixels.
100
+ * @param min - The least zoom the view allows.
101
+ * @param max - The greatest zoom the view allows.
102
+ * @returns The zoom.
103
+ */
104
+ export function zoomForOrthographicShell(shell, width, height, min, max) {
105
+ const shorter = Math.max(1, Math.min(width, height));
106
+ const discAt1 = Math.max(1, shorter / 2 / 1.08 - 1);
107
+ const halfAt1 = shorter / 2 / discAt1;
108
+ return Math.min(max, Math.max(min, (halfAt1 * 0.92) / Math.max(1.02, shell)));
109
+ }
@@ -1,6 +1,7 @@
1
1
  import type * as THREE from "three";
2
2
  import type { SatelliteRef, Satellites } from "../satellites/types.ts";
3
3
  import type { ThreeParts } from "../realistic/textures.ts";
4
+ import { type SatelliteFocus } from "./satellite-focus.ts";
4
5
  type Three = ThreeParts;
5
6
  /** How a group looks: the colour of its dots and their size in CSS pixels, and whether every orbit is drawn (only for a small group). */
6
7
  export interface SatelliteStyle {
@@ -11,6 +12,8 @@ export interface SatelliteStyle {
11
12
  export interface SatelliteGroupState extends SatelliteStyle {
12
13
  satellites: Satellites;
13
14
  visible: boolean;
15
+ /** How many of this group the last draw placed. 0 before the first draw and when the group is not drawn. */
16
+ placed?: number;
14
17
  }
15
18
  /** The part of the space view's model the satellites read and write. */
16
19
  export interface SatelliteState {
@@ -18,9 +21,13 @@ export interface SatelliteState {
18
21
  selected: SatelliteRef | null;
19
22
  /** The groups whose members are ringed and named (`labelSatellites`): the scene tells the page where each is, at every draw. */
20
23
  marked: Set<string>;
24
+ /** Satellites ringed besides a whole group: the nearest ones a page asked to mark. Empty unless set. */
25
+ pinned: SatelliteRef[];
21
26
  /** Written at each draw: satellites placed, whether the clock is too fast for the groups, and the milliseconds the last update took. */
22
27
  /** Colour each dot by its orbit class. Off by default: a group keeps the one colour it was given. */
23
28
  orbitTint: boolean;
29
+ /** One group shown on its own. `null` leaves every group as the page set it. */
30
+ focus: SatelliteFocus | null;
24
31
  drawn: {
25
32
  placed: number;
26
33
  segments: number;
@@ -59,8 +66,28 @@ export declare function pinPlan(count: number, next: number, elapsedMs: number):
59
66
  runs: [number, number][];
60
67
  next: number;
61
68
  };
69
+ /** An edge of the picture a satellite lies beyond. `null` when its place is inside the box. */
70
+ export type ScreenEdge = "top" | "right" | "bottom" | "left";
62
71
  /**
63
- * Where a satellite is on the screen now: in CSS pixels in the view's box, whether it can be seen (inside the box and not behind the Earth) and how high it is.
72
+ * Which edge of a box a point lies beyond. A corner goes to the edge it passes by more. Inside the box, `null`.
73
+ *
74
+ * @param x - CSS pixels from the left.
75
+ * @param y - CSS pixels from the top.
76
+ * @param width - The box width.
77
+ * @param height - The box height.
78
+ * @returns The edge, or `null` when the point is inside.
79
+ */
80
+ export declare function screenEdge(x: number, y: number, width: number, height: number): ScreenEdge | null;
81
+ /**
82
+ * Which edge a point lies beyond, from the camera's own axes: `x` to the right, `y` up. Used when the point is behind the camera, whose picture would otherwise mirror it.
83
+ *
84
+ * @param x - To the camera's right.
85
+ * @param y - Up, in the camera's axes.
86
+ * @returns The edge.
87
+ */
88
+ export declare function edgeBeyondCamera(x: number, y: number): ScreenEdge;
89
+ /**
90
+ * Where a satellite is on the screen now: in CSS pixels from the view's top left (which may lie outside the box), whether it can be seen, which edge it lies beyond, whether it is behind the camera, and how high it is.
64
91
  *
65
92
  * @example
66
93
  * ```ts
@@ -73,13 +100,17 @@ export declare function pinPlan(count: number, next: number, elapsedMs: number):
73
100
  export interface SatelliteScreen {
74
101
  x: number;
75
102
  y: number;
76
- /** Inside the box and in front of the Earth (not hidden by it). */
103
+ /** Inside the box, in front of the camera and not hidden by the Earth. */
77
104
  visible: boolean;
78
105
  /** Height above the mean Earth radius, in kilometres, at the moment drawn. */
79
106
  heightKm: number;
107
+ /** The edge the satellite lies beyond, or `null` when its place is inside the box. Behind the camera, the edge is the way it lies in the camera's own axes, not the mirrored picture. */
108
+ edge: ScreenEdge | null;
109
+ /** The satellite is behind the camera. The picture would put it on the opposite side, so `edge` is taken from the camera's own axes. */
110
+ behind: boolean;
80
111
  }
81
112
  export interface SatelliteLayer {
82
- /** Where one satellite of a shown group is on the screen as of the last update, or `null` when its group is not shown, it is not placed (its elements are too old) or it is behind the camera. */
113
+ /** Where one satellite of a shown group is on the screen as of the last update, or `null` when its group is not shown or it is not placed (its elements are too old). A satellite outside the picture or behind the camera is still returned, with `visible` false. */
83
114
  placeOf(ref: SatelliteRef, camera: THREE.Camera, size: {
84
115
  width: number;
85
116
  height: number;