@johnmorrisdotca/wakusei-planets 0.30.0 → 0.32.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (52) hide show
  1. package/CHANGELOG.md +16 -0
  2. package/NOTICE.md +5 -0
  3. package/README.md +98 -12
  4. package/dist/data-door.d.ts +357 -0
  5. package/dist/data-door.js +393 -0
  6. package/dist/door/book.d.ts +133 -0
  7. package/dist/door/book.js +256 -0
  8. package/dist/door/host.d.ts +30 -0
  9. package/dist/door/host.js +44 -0
  10. package/dist/door/index.d.ts +6 -0
  11. package/dist/door/index.js +10 -0
  12. package/dist/door/map.d.ts +152 -0
  13. package/dist/door/map.js +162 -0
  14. package/dist/door/own.d.ts +36 -0
  15. package/dist/door/own.js +86 -0
  16. package/dist/door/points.d.ts +61 -0
  17. package/dist/door/points.js +175 -0
  18. package/dist/door/small-bodies.d.ts +157 -0
  19. package/dist/door/small-bodies.js +148 -0
  20. package/dist/element.d.ts +21 -0
  21. package/dist/element.js +74 -4
  22. package/dist/scene/body.d.ts +5 -2
  23. package/dist/scene/body.js +2 -1
  24. package/dist/scene/detail.d.ts +29 -12
  25. package/dist/scene/detail.js +217 -135
  26. package/dist/scene/painter.d.ts +5 -0
  27. package/dist/scene/painter.js +8 -5
  28. package/dist/scene/textures.d.ts +9 -2
  29. package/dist/scene/textures.js +17 -0
  30. package/dist/space/arrows.d.ts +87 -0
  31. package/dist/space/arrows.js +117 -0
  32. package/dist/space/beside.d.ts +74 -0
  33. package/dist/space/beside.js +76 -0
  34. package/dist/space/bodies.d.ts +83 -0
  35. package/dist/space/bodies.js +93 -0
  36. package/dist/space/element.d.ts +114 -14
  37. package/dist/space/element.js +344 -28
  38. package/dist/space/frame.d.ts +45 -2
  39. package/dist/space/frame.js +56 -4
  40. package/dist/space/index.d.ts +6 -1
  41. package/dist/space/index.js +6 -1
  42. package/dist/space/labels.d.ts +52 -0
  43. package/dist/space/labels.js +40 -0
  44. package/dist/space/scale.d.ts +5 -5
  45. package/dist/space/scale.js +10 -4
  46. package/dist/space/scene.d.ts +49 -9
  47. package/dist/space/scene.js +325 -34
  48. package/dist/space/spin.d.ts +37 -0
  49. package/dist/space/spin.js +29 -0
  50. package/dist/version.d.ts +1 -1
  51. package/dist/version.js +1 -1
  52. package/package.json +6 -1
@@ -2,16 +2,30 @@ import { carryControls } from "@johnmorrisdotca/kazoku-ui/view-controls";
2
2
  import { BODIES, MOONS_OF } from "../facts.js";
3
3
  import { createGate, frameCap } from "../gate.js";
4
4
  import { createGameClock } from "./clock.js";
5
+ import { bodyKindOf, findBody, hopFrom, hostOf, HOP_ORDER } from "./bodies.js";
6
+ import { chooseLabels } from "./labels.js";
5
7
  import { createPainter } from "../scene/painter.js";
8
+ import { createDoorHost } from "../door/host.js";
9
+ import { ownCredit } from "../door/own.js";
6
10
  import { setThreeLoader, takeRenderer } from "../scene/shared.js";
7
11
  import { texturePaths, tilesBase } from "../scene/textures.js";
8
- import { AU_IN_EARTH_RADII, EARTH_RADIUS_KM, frameAt, SPACE_PLANETS } from "./frame.js";
12
+ import { AU_IN_EARTH_RADII, EARTH_RADIUS_KM, frameAt, SPACE_DWARFS, SPACE_PLANETS } from "./frame.js";
13
+ import { moonOffset } from "../scene/orient.js";
9
14
  import { moonDistance, placeBodies, radiusIn } from "./scale.js";
10
15
  import { createSpaceItem, FIT_DISTANCE, newSpaceModel, SPACE_BODIES } from "./scene.js";
11
16
  export { setThreeLoader, SPACE_BODIES };
17
+ /** The words the view says on itself for the scale in use. A page in another language gives its own with `scaleNames`. */
18
+ const SCALE_WORDS = {
19
+ en: { true: "True distances", compressed: "Distances squeezed to fit" },
20
+ ja: { true: "本当の距離", compressed: "距離を縮めて表示" },
21
+ };
22
+ /** A body whose disc is smaller than this many CSS pixels across its radius is marked with a ring as well as its name. */
23
+ const RING_BELOW_PX = 4;
12
24
  const readLight = (value) => (value === "lit" ? "lit" : "sun");
13
25
  const RAD = Math.PI / 180;
14
26
  const REAL_TIME_TICK_MS = 60_000;
27
+ /** The plain wheel in an immersive view is passed on as a pinch of this share, so a notch (100) zooms about 1.2 times, as the family's mouse wheel does with Alt. */
28
+ const WHEEL_AS_PINCH = 0.2;
15
29
  /** The least time between two `wakusei-timechange` events while time runs: a few a second at most, however fast the picture is drawn. */
16
30
  const TIME_EVENT_MS = 250;
17
31
  const readCentre = (value) => {
@@ -28,22 +42,26 @@ const readTime = (value) => {
28
42
  const ms = Date.parse(value);
29
43
  return Number.isFinite(ms) ? ms : "now";
30
44
  };
45
+ /** A body the view can put at its middle: the eleven of `SPACE_BODIES` and the moons of the planets in them. */
46
+ const canCentre = (id) => SPACE_BODIES.includes(id) || (BODIES[id].kind === "moon" && SPACE_BODIES.includes(hostOf(id) ?? ""));
31
47
  const readBody = (value) => {
32
- const id = value.toLowerCase();
33
- return SPACE_BODIES.includes(id) ? id : undefined;
48
+ const id = findBody(value);
49
+ return id !== undefined && canCentre(id) ? id : undefined;
34
50
  };
35
51
  /**
36
52
  * The zoom range of the space view when it carries its own controls. The floor, a millionth, takes the camera out past Neptune in the true scale. The ceiling is where the camera reaches the body: it is held at
37
- * 1.05 radii from the surface (`FIT_DISTANCE` over the zoom), so past a zoom of about 3.2 nothing moves; 3 is where the finest map tiles of the Earth, Mars and the Moon (16384 pixels round) run out of detail too, so it is the same ceiling for both.
53
+ * 1.05 radii from the surface (`FIT_DISTANCE` over the zoom), so past a zoom of about 3.9 nothing moves; 3.6 is where the finest map tiles of the Earth, Mars and the Moon (16384 pixels round) run out of detail too, so it is the same ceiling for both.
38
54
  */
39
- const CARRIED_LIMITS = { zoomMin: 1e-6, zoomMax: 3, tiltMax: 90 };
40
- const STYLE = `<style>:host{display:block;aspect-ratio:1/1;position:relative;overflow:hidden}#stage,#labels{position:absolute;inset:0}#labels{pointer-events:none;overflow:hidden}.sp-label{position:absolute;left:0;top:0;margin:0;padding:0;border:0;background:none;font:12px/16px system-ui,-apple-system,sans-serif;color:#fff;white-space:nowrap;text-shadow:0 0 3px #000,0 0 3px #000;will-change:transform;pointer-events:none}.sp-label:focus-visible{outline:2px solid #6cd0ff;outline-offset:2px;border-radius:4px}.sp-label::before{content:"";display:inline-block;width:5px;height:5px;margin:0 5px 1px -2px;border-radius:50%;background:#fff}.sp-label[hidden]{display:none}</style><div id="stage"></div><div id="labels"></div>`;
55
+ const CARRIED_LIMITS = { zoomMin: 1e-6, zoomMax: 3.6, tiltMax: 90 };
56
+ const STYLE = `<style>:host{display:block;aspect-ratio:1/1;position:relative;overflow:hidden}:host([immersive]){aspect-ratio:auto;width:100%;height:100%;touch-action:none!important;user-select:none;-webkit-user-select:none;-webkit-tap-highlight-color:transparent}#stage,#labels{position:absolute;inset:0}#labels{pointer-events:none;overflow:hidden}.sp-label{position:absolute;left:0;top:0;margin:0;padding:0;border:0;background:none;font:12px/16px system-ui,-apple-system,sans-serif;color:#fff;white-space:nowrap;text-shadow:0 0 3px #000,0 0 3px #000;will-change:transform;pointer-events:none}.sp-label:focus-visible{outline:2px solid #6cd0ff;outline-offset:2px;border-radius:4px}.sp-label::before{content:"";display:inline-block;width:5px;height:5px;margin:0 5px 1px -2px;border-radius:50%;background:#fff}.sp-label[hidden]{display:none}.sp-ring{position:absolute;left:0;top:0;width:16px;height:16px;margin:-8px 0 0 -8px;box-sizing:border-box;border:1.5px solid rgba(255,255,255,.85);border-radius:50%;box-shadow:0 0 3px #000;pointer-events:none;will-change:transform}.sp-ring[hidden]{display:none}.sp-scale{position:absolute;left:10px;top:8px;margin:0;padding:0;font:12px/16px system-ui,-apple-system,sans-serif;color:#fff;opacity:.9;text-shadow:0 0 3px #000,0 0 3px #000;pointer-events:none}</style><div id="stage"></div><div id="labels"></div><p class="sp-scale" part="scale" role="status"></p>`;
41
57
  /**
42
58
  * The `<wakusei-space>` custom element: the Earth, the Moon, the Sun and the planets where they are at a date and time, drawn with three.js through the shared renderer. It takes `center`
43
59
  * (`"lon,lat"`), `time` (`now` or an ISO date), `speed` (1 is real time, 0 holds, 86400 is a day a second, negative runs backwards), `scale` (`compressed` or `true`), `look` (`realistic` or `painted`),
44
60
  * `light` (`sun` or `lit`), `lang` (`en` or `ja`: the names), `labels` and `orbits` (`off` hides them), and `texture-base`, `texture-far-base`, `max-texture` and `tiles` (where the finer map tiles are, for a close zoom; `off` for none). Positions come from this package's own models. It draws
45
61
  * only when something changes, and nothing while it is out of sight.
46
62
  *
63
+ * `immersive` makes it a full-screen view: it fills the box it is in, whatever the shape (the body looked at is framed by the shorter side), the plain wheel zooms and a drag anywhere on it turns it. Without it Ctrl, Command or Alt with the wheel zooms and the plain wheel scrolls the page.
64
+ *
47
65
  * It carries the shared view controls itself: a drag turns the camera round the body, two fingers pinch, Ctrl, Command or Alt with the wheel zooms (the plain wheel scrolls the page), and the arrow keys, + and - and 0 work once it is
48
66
  * focused. `controls="none"` turns them off; `zoom-min` and `zoom-max` set the range (a millionth to 3 by default); a `<kazoku-view-controls for="...">` takes over and reads it.
49
67
  *
@@ -65,7 +83,7 @@ const STYLE = `<style>:host{display:block;aspect-ratio:1/1;position:relative;ove
65
83
  * ```
66
84
  */
67
85
  export class WakuseiSpace extends HTMLElement {
68
- static observedAttributes = ["center", "time", "speed", "scale", "labels", "orbits", "look", "light", "lang"];
86
+ static observedAttributes = ["center", "time", "speed", "scale", "labels", "orbits", "help", "look", "light", "lang", "data"];
69
87
  #model = newSpaceModel();
70
88
  #clock = createGameClock("now", 1);
71
89
  #view;
@@ -79,8 +97,26 @@ export class WakuseiSpace extends HTMLElement {
79
97
  #pool = new Map();
80
98
  #places;
81
99
  #names = {};
100
+ #selected = null;
101
+ #scaleNames = {};
102
+ #rings = new Map();
103
+ #scaleLine;
82
104
  #hint = "choose to look at it";
83
105
  #painter;
106
+ #door = createDoorHost(this, {
107
+ // the package's own: the pictures and the positions of the bodies drawn, and the finer tiles of a window on a body (the folder's: a source of a page's is counted by its id)
108
+ own: () => {
109
+ const own = [];
110
+ const drawn = this.#model.painted.bodies;
111
+ if (drawn > 0)
112
+ own.push(ownCredit("wakusei-pictures", drawn), ownCredit("wakusei-orbits", drawn));
113
+ for (const [id, used] of Object.entries(this.#model.door.used))
114
+ if (this.#door.book.get(id) === undefined)
115
+ own.push(ownCredit(id, used));
116
+ return own;
117
+ },
118
+ used: () => new Map(Object.entries(this.#model.door.used)),
119
+ }, { kinds: ["tiles", "records"] }, () => this.#gate?.request());
84
120
  #focusBeforeRide = "earth";
85
121
  #lostTold = false;
86
122
  #hands;
@@ -92,11 +128,13 @@ export class WakuseiSpace extends HTMLElement {
92
128
  this.#model.centre = readCentre(this.getAttribute("center")) ?? this.#model.centre;
93
129
  this.#clock = createGameClock(readTime(this.getAttribute("time")), this.#readSpeed());
94
130
  this.#model.time = this.#clock.time;
95
- this.#model.mode = this.getAttribute("scale") === "true" ? "true" : "compressed";
131
+ this.#model.mode = this.getAttribute("scale") === "compressed" ? "compressed" : "true";
96
132
  this.#model.labels = this.getAttribute("labels") !== "off";
97
133
  this.#model.orbits = this.getAttribute("orbits") !== "off";
134
+ this.#model.help = this.hasAttribute("help") && this.getAttribute("help") !== "off";
98
135
  this.#model.look = this.getAttribute("look") === "painted" ? "painted" : "realistic";
99
136
  this.#model.light = readLight(this.getAttribute("light"));
137
+ this.#door.book.ownOnly(this.getAttribute("data") === "own");
100
138
  this.#model.stepMs = Math.max(1000, Math.abs(this.#clock.speed) * 1000);
101
139
  if (!this.hasAttribute("role"))
102
140
  this.setAttribute("role", "img");
@@ -108,7 +146,10 @@ export class WakuseiSpace extends HTMLElement {
108
146
  root.innerHTML = STYLE;
109
147
  this.#stage = root.getElementById("stage");
110
148
  this.#labelBox = root.getElementById("labels");
149
+ this.#scaleLine = root.querySelector(".sp-scale");
150
+ this.#nameScale();
111
151
  this.#pool.clear();
152
+ this.#rings.clear();
112
153
  const nav = navigator;
113
154
  this.#gate = createGate(1000 / frameCap(30, { deviceMemory: nav.deviceMemory, hardwareConcurrency: nav.hardwareConcurrency }), () => this.#view?.invalidate());
114
155
  this.#stage.addEventListener("webglcontextlost", this.#lost, true);
@@ -120,10 +161,14 @@ export class WakuseiSpace extends HTMLElement {
120
161
  this.#taken = taken;
121
162
  const item = createSpaceItem(this.#model, {
122
163
  painter: () => this.#painterNow(),
164
+ book: this.#door.book,
123
165
  cap: () => this.#textureCap(),
124
166
  invalidate: () => this.#gate?.request(),
125
167
  advance: () => this.#advance(),
126
- onLabels: (places) => this.#placeLabels(places),
168
+ onLabels: (places) => {
169
+ this.#placeLabels(places);
170
+ this.#door.announce();
171
+ },
127
172
  onMoving: () => this.#gate?.request(),
128
173
  onRideLost: () => this.#rideLost(),
129
174
  onPainterError: (error) => this.dispatchEvent(new CustomEvent("wakusei-bodies-unavailable", { detail: { reason: error instanceof Error ? error.message : String(error) } })),
@@ -140,6 +185,8 @@ export class WakuseiSpace extends HTMLElement {
140
185
  });
141
186
  this.#schedule();
142
187
  this.addEventListener("pointerdown", this.#down);
188
+ // before the shared hands (which hear it after this): a plain wheel in an immersive view zooms
189
+ this.addEventListener("wheel", this.#wheel, { capture: true, passive: false });
143
190
  this.#hands = carryControls(this, { limits: CARRIED_LIMITS, onChange: (view, info) => this.setView(view, info.final) });
144
191
  this.#labelBox.addEventListener("click", this.#labelClick);
145
192
  this.#labelBox.addEventListener("focusin", this.#labelFocus);
@@ -148,6 +195,7 @@ export class WakuseiSpace extends HTMLElement {
148
195
  this.#hands?.unbind();
149
196
  this.#hands = undefined;
150
197
  this.removeEventListener("pointerdown", this.#down);
198
+ this.removeEventListener("wheel", this.#wheel, { capture: true });
151
199
  window.removeEventListener("pointerup", this.#up, true);
152
200
  clearTimeout(this.#timer);
153
201
  this.#timer = undefined;
@@ -165,18 +213,24 @@ export class WakuseiSpace extends HTMLElement {
165
213
  else if (name === "speed")
166
214
  this.speed = this.#readSpeed();
167
215
  else if (name === "scale")
168
- this.scale = value === "true" ? "true" : "compressed";
216
+ this.scale = value === "compressed" ? "compressed" : "true";
169
217
  else if (name === "labels")
170
218
  this.labels = value !== "off";
171
219
  else if (name === "orbits")
172
220
  this.orbits = value !== "off";
221
+ else if (name === "help")
222
+ this.help = value !== null && value !== "off";
173
223
  else if (name === "look")
174
224
  this.look = value === "painted" ? "painted" : "realistic";
175
225
  else if (name === "light")
176
226
  this.light = readLight(value);
177
- else if (name === "lang")
227
+ else if (name === "data")
228
+ this.#door.book.ownOnly(value === "own");
229
+ else if (name === "lang") {
178
230
  for (const [id, button] of this.#pool)
179
231
  this.#nameLabel(button, id);
232
+ this.#nameScale();
233
+ }
180
234
  }
181
235
  /** The home place the camera starts above, as longitude and latitude in degrees. */
182
236
  get centre() {
@@ -214,21 +268,30 @@ export class WakuseiSpace extends HTMLElement {
214
268
  if (this.#clock.speed === 0 && was !== 0)
215
269
  this.#announce(false);
216
270
  }
217
- /** `compressed` (the default: every direction real, the distances squeezed) or `true` (real distances and sizes). */
271
+ /** `true` (the default: real distances and sizes; a body too small to see is marked with a ring and its name) or `compressed` (every direction real, the distances squeezed). The scale in use is always named on the view. */
218
272
  get scale() {
219
273
  return this.#model.mode;
220
274
  }
221
275
  set scale(value) {
222
276
  this.#model.mode = value;
277
+ this.#nameScale();
223
278
  this.#gate?.request();
224
279
  }
280
+ /** The words naming the scale on the view, for a page in another language (`{ true: "距離はそのまま" }`). A scale left out keeps its words in the element's `lang`. */
281
+ get scaleNames() {
282
+ return { ...this.#scaleNames };
283
+ }
284
+ set scaleNames(value) {
285
+ this.#scaleNames = { ...value };
286
+ this.#nameScale();
287
+ }
225
288
  get labels() {
226
289
  return this.#model.labels;
227
290
  }
228
291
  set labels(value) {
229
292
  this.#model.labels = value;
230
293
  if (!value)
231
- for (const label of this.#pool.values())
294
+ for (const label of [...this.#pool.values(), ...this.#rings.values()])
232
295
  label.hidden = true;
233
296
  this.#gate?.request();
234
297
  }
@@ -239,6 +302,17 @@ export class WakuseiSpace extends HTMLElement {
239
302
  this.#model.orbits = value;
240
303
  this.#gate?.request();
241
304
  }
305
+ /**
306
+ * Help mode (the `help` attribute; off by default): on the body looked at, a line along its axis and a curved arrow round it showing which way it spins, by the right-hand rule about the axis it really turns about, so it is
307
+ * right for Venus and Uranus, which turn backwards, and for Uranus on its side. It is about learning, so the arrow is drawn over the body.
308
+ */
309
+ get help() {
310
+ return this.#model.help;
311
+ }
312
+ set help(value) {
313
+ this.#model.help = value;
314
+ this.#gate?.request();
315
+ }
242
316
  /** How the bodies are drawn: `realistic` (shaded from the body's real colours and features) or `painted` (public-domain surface maps). */
243
317
  get look() {
244
318
  return this.#model.look === "painted" ? "painted" : "realistic";
@@ -277,7 +351,7 @@ export class WakuseiSpace extends HTMLElement {
277
351
  }
278
352
  this.#gate?.request();
279
353
  }
280
- /** The body the camera looks at (`earth`, `sun`, `moon`, `mercury` to `neptune`, `pluto`): at zoom 1 it fills the view; the yaw and tilt turn the camera round it. */
354
+ /** The body the camera looks at: any body the view places, by name in any case (`earth`, `sun`, `moon`, `mercury` to `neptune`, `pluto`, and the moons of the planets such as `titan`). At zoom 1 it fills the view; the yaw and tilt turn the camera round it. Setting it moves the camera there at once and keeps the zoom; `goTo` glides and brings the body into view. */
281
355
  get body() {
282
356
  return this.#model.focus;
283
357
  }
@@ -285,8 +359,75 @@ export class WakuseiSpace extends HTMLElement {
285
359
  const id = readBody(name);
286
360
  if (!id)
287
361
  return;
362
+ this.#centre(id);
363
+ }
364
+ /** Move the camera to `id` (not yet drawn) and tell the page. */
365
+ #centre(id) {
366
+ const was = this.#model.focus;
288
367
  this.#model.focus = id;
289
368
  this.#gate?.request();
369
+ if (was !== id)
370
+ this.#announceHop();
371
+ }
372
+ /** What the hopper can do from where it is: the body at the middle, and the bodies the previous and next hop lead to. */
373
+ get hopper() {
374
+ const here = this.#model.focus;
375
+ return { current: this.bodyInfo(here), previous: this.bodyInfo(hopFrom(here, -1, canCentre)), next: this.bodyInfo(hopFrom(here, 1, canCentre)) };
376
+ }
377
+ /**
378
+ * Go to a body: it becomes the middle of the view and the zoom brings it whole into view (`zoom`, 1 by default; a planet's `-moons` preset for its system), gliding there from where the camera is unless `smooth` is
379
+ * false or the reader asked for reduced motion. `wakusei-hop` tells the page where it went and where the next and previous hops lead. A name in any case, in English or Japanese, works.
380
+ *
381
+ * @param target - A body's name (`"saturn"`, `"Saturn"`, `"土星"`).
382
+ * @param options - `smooth` (true by default) and `zoom` (1 by default).
383
+ * @returns Whether the view can go there.
384
+ */
385
+ goTo(target, options = {}) {
386
+ const id = readBody(target);
387
+ if (!id)
388
+ return false;
389
+ const model = this.#model;
390
+ const reduced = typeof matchMedia === "function" && matchMedia("(prefers-reduced-motion: reduce)").matches;
391
+ const pose = model.pose;
392
+ const glide = (options.smooth ?? true) && !reduced && pose.valid && model.ride === null;
393
+ if (model.ride !== null)
394
+ this.ride(null, { smooth: false });
395
+ model.transition = glide ? { start: performance.now(), duration: 1200, from: { p: [pose.p[0], pose.p[1], pose.p[2]], q: [pose.q[0], pose.q[1], pose.q[2], pose.q[3]], fov: pose.fov } } : null;
396
+ this.#selected = id;
397
+ this.#model.focus = id;
398
+ const zoom = options.zoom ?? 1;
399
+ // a bar bound to the element holds the view's state: it applies the zoom the page hears in the event; without one the element's own hands do
400
+ if (!this.hasAttribute("data-kazoku-bar"))
401
+ this.#hands?.controller.set({ zoom }, true, "jump");
402
+ this.#schedule();
403
+ this.#gate?.request();
404
+ this.#announceHop(zoom);
405
+ return true;
406
+ }
407
+ /** Go to the next body in the order of `bodyList()` (the Sun, Mercury, Venus, the Earth, the Moon, Mars, Phobos, Deimos, Ceres, Jupiter, its moons, and on), round to the Sun after the last. */
408
+ next(options = {}) {
409
+ this.goTo(hopFrom(this.#model.focus, 1, canCentre), options);
410
+ return this.bodyInfo(this.#model.focus);
411
+ }
412
+ /** Go to the previous body in the same order. */
413
+ previous(options = {}) {
414
+ this.goTo(hopFrom(this.#model.focus, -1, canCentre), options);
415
+ return this.bodyInfo(this.#model.focus);
416
+ }
417
+ /** Every body the view can go to, in the hopper's order, with its kind and, for a moon, its planet: a page's jump list. */
418
+ bodyList() {
419
+ return HOP_ORDER.filter(canCentre).map((id) => this.bodyInfo(id));
420
+ }
421
+ /** The body a press has chosen and the page may be showing a card for, or null. A second press on it, or `goTo`, makes it the middle of the view. */
422
+ get selected() {
423
+ return this.#selected;
424
+ }
425
+ set selected(id) {
426
+ this.#selected = id;
427
+ }
428
+ /** `zoom` is the zoom the page should bring the view to: given by `goTo`, left out when the body was only set (the zoom stays). */
429
+ #announceHop(zoom) {
430
+ this.dispatchEvent(new CustomEvent("wakusei-hop", { detail: { ...this.hopper, zoom }, bubbles: true, composed: true }));
290
431
  }
291
432
  /** The words shown beside each body, for a page in another language (`{ mars: "火星" }`). A body left out keeps its name in the element's `lang`. */
292
433
  get bodyNames() {
@@ -309,8 +450,30 @@ export class WakuseiSpace extends HTMLElement {
309
450
  /** The name, radius and distance from the Earth of a body now, for a page's own caption. */
310
451
  bodyInfo(id) {
311
452
  const frame = this.#model.drawn?.frame ?? frameAt(this.#model.time);
312
- const geo = id === "earth" ? [0, 0, 0] : id === "sun" ? frame.sun : id === "moon" ? frame.moon : frame.planets[id];
313
- return { id, name: this.#nameOf(id), radiusKm: BODIES[id].radiusKm, distanceKm: Math.hypot(geo[0], geo[1], geo[2]) * AU_IN_EARTH_RADII * EARTH_RADIUS_KM };
453
+ const km = AU_IN_EARTH_RADII * EARTH_RADIUS_KM;
454
+ let geo = [Number.NaN, Number.NaN, Number.NaN];
455
+ const host = hostOf(id);
456
+ if (id === "earth")
457
+ geo = [0, 0, 0];
458
+ else if (id === "sun")
459
+ geo = frame.sun;
460
+ else if (id === "moon")
461
+ geo = frame.moon;
462
+ else if (SPACE_PLANETS.includes(id))
463
+ geo = frame.planets[id];
464
+ else if (SPACE_DWARFS.includes(id))
465
+ geo = frame.dwarfs[id];
466
+ else if (host !== undefined && (SPACE_PLANETS.includes(host) || SPACE_DWARFS.includes(host))) {
467
+ // a moon: its planet's place and its offset in the planet's radii
468
+ const at = SPACE_DWARFS.includes(host) ? frame.dwarfs[host] : frame.planets[host];
469
+ const offset = moonOffset(id, this.#model.time);
470
+ const radius = BODIES[host].radiusKm / km;
471
+ geo = [at[0] + offset[0] * radius, at[1] + offset[1] * radius, at[2] + offset[2] * radius];
472
+ }
473
+ const info = { id, name: this.#nameOf(id), kind: bodyKindOf(id), radiusKm: BODIES[id].radiusKm, distanceKm: Math.hypot(geo[0], geo[1], geo[2]) * km };
474
+ if (host !== undefined)
475
+ info.host = host;
476
+ return info;
314
477
  }
315
478
  // --- riding along -------------------------------------------------------------------------------------------------------------------------------
316
479
  /**
@@ -354,11 +517,17 @@ export class WakuseiSpace extends HTMLElement {
354
517
  const distanceKm = Math.max(100, options.distanceKm ?? 3000);
355
518
  if (typeof target !== "string") {
356
519
  const around = options.around && SPACE_BODIES.includes(options.around) ? options.around : "earth";
520
+ if (options.view === "beside")
521
+ return undefined;
357
522
  return { target, around, view: options.view ?? "from", look, distanceKm };
358
523
  }
359
524
  const id = target.toLowerCase();
360
- if (SPACE_BODIES.includes(id))
525
+ if (SPACE_BODIES.includes(id)) {
526
+ // the view from beside a moon is for a moon: of the bodies of the view only the Moon is one
527
+ if (options.view === "beside" && id !== "moon")
528
+ return undefined;
361
529
  return { target: id, around: "earth", view: id === "earth" ? "follow" : (options.view ?? "from"), look, distanceKm };
530
+ }
362
531
  for (const [planet, moons] of Object.entries(MOONS_OF)) {
363
532
  if (!moons.includes(id) || !SPACE_BODIES.includes(planet) || planet === "earth")
364
533
  continue;
@@ -377,12 +546,19 @@ export class WakuseiSpace extends HTMLElement {
377
546
  this.dispatchEvent(new CustomEvent("wakusei-ride-lost", { detail: this.riding }));
378
547
  }
379
548
  /** The zoom that shows a part of the sky: the Earth alone, the Moon's orbit, the Sun, the planets out to Mars, all of them, or a planet's nearer moons, at this scale and moment. */
380
- zoomFor(preset) {
549
+ zoomFor(named) {
381
550
  const mode = this.#model.mode;
551
+ // a name in any case; one that is no preset gives 1, the zoom that fills the view with the body looked at, not NaN
552
+ const preset = String(named).toLowerCase();
553
+ if (!["earth", "moon", "sun", "inner", "solar-system"].includes(preset) && !(preset.endsWith("-moons") && findBody(preset.slice(0, -6)) !== undefined))
554
+ return 1;
382
555
  if (preset.endsWith("-moons")) {
383
556
  const id = preset.slice(0, -6);
384
557
  const sites = this.#painterNow().moons(id, this.#model.time);
385
- const reach = Math.max(1, ...sites.map((site) => moonDistance(mode, id, Math.hypot(...site.offset))).filter((r) => r <= 22));
558
+ const distances = sites.map((site) => moonDistance(mode, id, Math.hypot(...site.offset)));
559
+ // the moons within 22 radii; a system whose moons are all farther (a dwarf planet's) is framed by them all
560
+ const near = distances.filter((r) => r <= 22);
561
+ const reach = Math.max(1, ...(near.length > 0 ? near : distances));
386
562
  const radius = radiusIn(id, mode);
387
563
  return Math.min(1, (FIT_DISTANCE * radius) / ((reach * radius * 1.15) / Math.tan(17.5 * RAD)));
388
564
  }
@@ -396,7 +572,40 @@ export class WakuseiSpace extends HTMLElement {
396
572
  inner: Math.max(away("mercury"), away("venus"), away("mars"), away("sun")),
397
573
  "solar-system": Math.max(...SPACE_PLANETS.map(away), away("sun")),
398
574
  }[preset];
399
- return preset === "earth" ? 1 : FIT_DISTANCE / Math.max(FIT_DISTANCE, reach * 3.4);
575
+ if (preset === "earth")
576
+ return 1;
577
+ const zoom = FIT_DISTANCE / Math.max(FIT_DISTANCE, reach * 3.4);
578
+ // the Moon's own picture is a quarter of the Earth's in the compressed scale (it was drawn as 0.9 of it): the view of its orbit is still the one that holds the Earth too, three Moon-distances out
579
+ return preset === "moon" && mode === "compressed" ? zoom * (radiusIn("moon", mode) / 0.9) : zoom;
580
+ }
581
+ /**
582
+ * The outside sources of this view (the data door): maps of a body, which take the place of the folder of tiles for it, and records of small bodies, which are drawn as points in one instanced draw. Each carries its own
583
+ * licence and credit. Assign a list to replace them; a source `validateSource` refuses throws a `TypeError` and changes nothing. A property, not an attribute: data is not text.
584
+ */
585
+ get sources() {
586
+ return this.#door.sources();
587
+ }
588
+ set sources(list) {
589
+ this.#door.setSources(list);
590
+ }
591
+ /** Adds one source (checked at once: a `TypeError` for an empty credit, no licence, a clashing id or a map source with no body). Returns the function that removes it. */
592
+ addSource(source) {
593
+ return this.#door.book.addSource(source);
594
+ }
595
+ /** Removes a source by id: its loads are aborted, its tiles and records freed, its points and window taken off the picture, and its credit goes. Returns whether there was one. */
596
+ removeSource(id) {
597
+ return this.#door.book.removeSource(id);
598
+ }
599
+ /** Switches a source off (`false`) or on, for a layers panel. */
600
+ enableSource(id, on) {
601
+ this.#door.book.enableSource(id, on);
602
+ }
603
+ /**
604
+ * The sources whose data was in the last drawn frame, the package's own first: the surface pictures and the positions of the bodies, the finer map tiles where a window of them is on a body, and each source of yours that
605
+ * gave points or tiles. `creditLine(credits, "en" | "ja")` from `@johnmorrisdotca/wakusei-planets/door` writes them with the licences. `wakusei-creditschange` fires when this set changes (not at each frame), with `detail: { credits }`.
606
+ */
607
+ credits() {
608
+ return this.#door.credits();
400
609
  }
401
610
  /** What the element is doing, for a page or a test. */
402
611
  get stats() {
@@ -413,7 +622,12 @@ export class WakuseiSpace extends HTMLElement {
413
622
  camera: { position: [...this.#model.camera.position], fov: this.#model.camera.fov },
414
623
  bodies: { look: this.#model.look, painted: this.#model.painted.bodies, moons: this.#model.painted.moons, held: this.#model.painted.held },
415
624
  tiles: { ...this.#model.tiles },
625
+ door: { points: this.#model.door.points, sources: this.#door.book.stats() },
416
626
  ride: { riding: this.#model.ride !== null, lost: this.#model.rideLost, gliding: this.#model.transition !== null },
627
+ orbits: { drawn: [...this.#model.rings.drawn], miss: { ...this.#model.rings.miss }, hidden: [...this.#model.rings.hidden] },
628
+ arrows: Object.fromEntries(Object.entries(this.#model.arrows).map(([id, list]) => [id, list.map((a) => ({ position: [...a.position], direction: [...a.direction] }))])),
629
+ arrowPx: this.#model.arrowPx,
630
+ 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,
417
631
  };
418
632
  }
419
633
  // --- the game clock ----------------------------------------------------------------------------------------------------------------------------
@@ -465,7 +679,10 @@ export class WakuseiSpace extends HTMLElement {
465
679
  const box = this.#labelBox;
466
680
  if (!box)
467
681
  return;
468
- for (const id of SPACE_BODIES) {
682
+ const wanted = [];
683
+ const spots = new Map();
684
+ const ids = [...new Set([...SPACE_BODIES, ...this.#pool.keys(), ...Object.keys(places)])];
685
+ for (const id of ids) {
469
686
  let label = this.#pool.get(id);
470
687
  const at = places[id];
471
688
  if (!label) {
@@ -480,21 +697,107 @@ export class WakuseiSpace extends HTMLElement {
480
697
  this.#pool.set(id, button);
481
698
  label = button;
482
699
  }
483
- if (!at) {
700
+ let ring = this.#rings.get(id);
701
+ if (!ring) {
702
+ ring = box.ownerDocument.createElement("div");
703
+ ring.className = "sp-ring";
704
+ ring.dataset["ring"] = id;
705
+ ring.hidden = true;
706
+ box.append(ring);
707
+ this.#rings.set(id, ring);
708
+ }
709
+ if (!at)
710
+ continue;
711
+ const x = Math.round(at.x + Math.max(4, Math.min(at.r, 40)));
712
+ const y = Math.round(at.y - 8);
713
+ spots.set(id, { x, y, small: at.r < RING_BELOW_PX });
714
+ wanted.push({ id, x, y, width: this.#widthOf(label), height: 16, rank: this.#rank(id), size: at.r });
715
+ }
716
+ // no two names overlap: a moon gives way to its planet and a planet to the Sun
717
+ const shown = chooseLabels(wanted);
718
+ for (const id of ids) {
719
+ const label = this.#pool.get(id);
720
+ const ring = this.#rings.get(id);
721
+ if (!label || !ring)
722
+ continue;
723
+ const spot = spots.get(id);
724
+ if (!spot || !shown.has(id)) {
484
725
  if (!label.hidden)
485
726
  label.hidden = true;
727
+ if (!ring.hidden)
728
+ ring.hidden = true;
486
729
  continue;
487
730
  }
488
731
  if (label.hidden)
489
732
  label.hidden = false;
490
- label.style.transform = `translate(${Math.round(at.x + Math.max(4, Math.min(at.r, 40)))}px,${Math.round(at.y - 8)}px)`;
733
+ label.style.transform = `translate(${spot.x}px,${spot.y}px)`;
734
+ // a body too small to see is marked: a ring round the place it is, as well as its name
735
+ if (ring.hidden === spot.small)
736
+ ring.hidden = !spot.small;
737
+ if (spot.small)
738
+ ring.style.transform = `translate(${Math.round(places[id].x)}px,${Math.round(places[id].y)}px)`;
491
739
  }
492
740
  }
741
+ /** How sure a body's name is of its place: the body looked at first, then the Sun, the planets, the dwarf planets and the moons. */
742
+ #rank(id) {
743
+ if (id === this.#model.focus)
744
+ return 0;
745
+ if (id === "sun")
746
+ return 1;
747
+ const kind = bodyKindOf(id);
748
+ return kind === "moon" ? 4 : kind === "dwarf-planet" ? 3 : 2;
749
+ }
750
+ #widths = new WeakMap();
751
+ /** The width of a name's box on the screen, measured when its words change and not at every frame. */
752
+ #widthOf(label) {
753
+ let width = this.#widths.get(label);
754
+ if (width === undefined) {
755
+ const hidden = label.hidden;
756
+ if (hidden)
757
+ label.hidden = false;
758
+ width = label.offsetWidth;
759
+ if (hidden)
760
+ label.hidden = true;
761
+ if (width > 0)
762
+ this.#widths.set(label, width);
763
+ }
764
+ return width;
765
+ }
766
+ /** Say which scale is on, on the view itself. */
767
+ #nameScale() {
768
+ if (!this.#scaleLine)
769
+ return;
770
+ const words = SCALE_WORDS[this.getAttribute("lang") === "ja" ? "ja" : "en"];
771
+ this.#scaleLine.textContent = this.#scaleNames[this.#model.mode] ?? words[this.#model.mode];
772
+ }
773
+ /** Where a body is on the screen now: its centre and radius in CSS pixels in the element, or `null` when it is behind the camera or not drawn. */
774
+ placeOf(id) {
775
+ const at = this.#places?.[id];
776
+ return at ? { ...at } : null;
777
+ }
493
778
  #nameLabel(button, id) {
494
779
  const text = this.#nameOf(id);
495
780
  button.textContent = text;
781
+ this.#widths.delete(button);
496
782
  button.setAttribute("aria-label", `${text}: ${this.#hint}`);
497
783
  }
784
+ /** True for the full-screen mode (the `immersive` attribute): the element fills its box in any shape, a plain wheel zooms instead of scrolling the page, and a touch drag turns the view instead of scrolling it. */
785
+ get immersive() {
786
+ return this.hasAttribute("immersive");
787
+ }
788
+ set immersive(value) {
789
+ this.toggleAttribute("immersive", value);
790
+ }
791
+ // In an immersive view a plain wheel zooms. It is passed on to the shared hands as the wheel's own pinch (Ctrl and a small step), so whichever hands hold the view (the element's, or a bar bound to it) apply it with their limits;
792
+ // Ctrl, Command, Alt and Shift with the wheel are the family's own and are left alone.
793
+ #wheel = (event) => {
794
+ if (!this.hasAttribute("immersive") || event.ctrlKey || event.metaKey || event.altKey || event.shiftKey)
795
+ return;
796
+ event.preventDefault();
797
+ event.stopImmediatePropagation();
798
+ const pixels = event.deltaMode === 1 ? event.deltaY * 33 : event.deltaMode === 2 ? event.deltaY * 800 : event.deltaY;
799
+ this.dispatchEvent(new WheelEvent("wheel", { deltaY: Math.max(-250, Math.min(250, pixels)) * WHEEL_AS_PINCH, deltaMode: 0, clientX: event.clientX, clientY: event.clientY, ctrlKey: true, bubbles: true, cancelable: true, composed: true }));
800
+ };
498
801
  // A body is chosen by pressing on it or on its name (and by Tab and Enter on the name); a drag that turns the view is not a choice.
499
802
  #downX = 0;
500
803
  #downY = 0;
@@ -515,8 +818,7 @@ export class WakuseiSpace extends HTMLElement {
515
818
  const y = event.clientY - rect.top;
516
819
  let best;
517
820
  let bestD = Infinity;
518
- for (const id of SPACE_BODIES) {
519
- const at = this.#places[id];
821
+ for (const [id, at] of Object.entries(this.#places)) {
520
822
  if (!at)
521
823
  continue;
522
824
  const d = Math.hypot(x - at.x, y - at.y);
@@ -526,13 +828,27 @@ export class WakuseiSpace extends HTMLElement {
526
828
  }
527
829
  }
528
830
  if (best)
529
- this.dispatchEvent(new CustomEvent("wakusei-body-click", { detail: this.bodyInfo(best), bubbles: true, composed: true }));
831
+ this.#press(best);
530
832
  };
531
833
  #labelClick = (event) => {
532
834
  const id = event.target?.dataset?.["body"];
533
835
  if (id)
534
- this.dispatchEvent(new CustomEvent("wakusei-body-click", { detail: this.bodyInfo(id), bubbles: true, composed: true }));
836
+ this.#press(id);
535
837
  };
838
+ /**
839
+ * A press on a body or its name. `wakusei-body-click` tells the page every time. The first press on a body selects it (`wakusei-body-select`, for the page's card); a second press on the body that is selected makes it
840
+ * the middle of the view (`goTo`), unless it already is, so a press never sends the view anywhere else.
841
+ */
842
+ #press(id) {
843
+ this.dispatchEvent(new CustomEvent("wakusei-body-click", { detail: this.bodyInfo(id), bubbles: true, composed: true }));
844
+ if (this.#selected === id) {
845
+ if (canCentre(id) && this.#model.focus !== id)
846
+ this.goTo(id);
847
+ return;
848
+ }
849
+ this.#selected = id;
850
+ this.dispatchEvent(new CustomEvent("wakusei-body-select", { detail: this.bodyInfo(id), bubbles: true, composed: true }));
851
+ }
536
852
  #labelFocus = (event) => {
537
853
  const id = event.target?.dataset?.["body"];
538
854
  if (id)
@@ -540,7 +856,7 @@ export class WakuseiSpace extends HTMLElement {
540
856
  };
541
857
  // --- the rest --------------------------------------------------------------------------------------------------------------------------------------
542
858
  #painterNow() {
543
- return (this.#painter ??= createPainter({ textureBase: texturePaths(this).textureBase(), farBase: texturePaths(this).farBase(), tiles: tilesBase(this) ?? "off" }));
859
+ return (this.#painter ??= createPainter({ textureBase: texturePaths(this).textureBase(), farBase: texturePaths(this).farBase(), tiles: tilesBase(this) ?? "off", book: this.#door.book }));
544
860
  }
545
861
  #textureCap() {
546
862
  const given = Number(this.getAttribute("max-texture"));