@johnmorrisdotca/wakusei-planets 0.28.0 → 0.30.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.
@@ -1,9 +1,10 @@
1
1
  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
+ import { createGameClock } from "./clock.js";
4
5
  import { createPainter } from "../scene/painter.js";
5
6
  import { setThreeLoader, takeRenderer } from "../scene/shared.js";
6
- import { texturePaths } from "../scene/textures.js";
7
+ import { texturePaths, tilesBase } from "../scene/textures.js";
7
8
  import { AU_IN_EARTH_RADII, EARTH_RADIUS_KM, frameAt, SPACE_PLANETS } from "./frame.js";
8
9
  import { moonDistance, placeBodies, radiusIn } from "./scale.js";
9
10
  import { createSpaceItem, FIT_DISTANCE, newSpaceModel, SPACE_BODIES } from "./scene.js";
@@ -33,14 +34,14 @@ const readBody = (value) => {
33
34
  };
34
35
  /**
35
36
  * 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
36
- * 1.05 radii from the surface (`FIT_DISTANCE` over the zoom), so past a zoom of about 3.2 nothing moves; 3 shows the surface as sharp as the painted maps (2048 pixels round) and the shader allow.
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.
37
38
  */
38
39
  const CARRIED_LIMITS = { zoomMin: 1e-6, zoomMax: 3, tiltMax: 90 };
39
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>`;
40
41
  /**
41
42
  * 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`
42
43
  * (`"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`),
43
- * `light` (`sun` or `lit`), `lang` (`en` or `ja`: the names), `labels` and `orbits` (`off` hides them), and `texture-base`, `texture-far-base` and `max-texture`. Positions come from this package's own models. It draws
44
+ * `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
44
45
  * only when something changes, and nothing while it is out of sight.
45
46
  *
46
47
  * 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
@@ -66,8 +67,7 @@ const STYLE = `<style>:host{display:block;aspect-ratio:1/1;position:relative;ove
66
67
  export class WakuseiSpace extends HTMLElement {
67
68
  static observedAttributes = ["center", "time", "speed", "scale", "labels", "orbits", "look", "light", "lang"];
68
69
  #model = newSpaceModel();
69
- #mode = "now";
70
- #speed = 1;
70
+ #clock = createGameClock("now", 1);
71
71
  #view;
72
72
  #gate;
73
73
  #taken;
@@ -75,7 +75,6 @@ export class WakuseiSpace extends HTMLElement {
75
75
  #labelBox;
76
76
  #state = "loading";
77
77
  #timer;
78
- #last = 0;
79
78
  #announced = -Infinity;
80
79
  #pool = new Map();
81
80
  #places;
@@ -91,15 +90,14 @@ export class WakuseiSpace extends HTMLElement {
91
90
  }
92
91
  connectedCallback() {
93
92
  this.#model.centre = readCentre(this.getAttribute("center")) ?? this.#model.centre;
94
- this.#mode = readTime(this.getAttribute("time"));
95
- this.#model.time = this.#mode === "now" ? Date.now() : this.#mode;
96
- this.#speed = this.#readSpeed();
93
+ this.#clock = createGameClock(readTime(this.getAttribute("time")), this.#readSpeed());
94
+ this.#model.time = this.#clock.time;
97
95
  this.#model.mode = this.getAttribute("scale") === "true" ? "true" : "compressed";
98
96
  this.#model.labels = this.getAttribute("labels") !== "off";
99
97
  this.#model.orbits = this.getAttribute("orbits") !== "off";
100
98
  this.#model.look = this.getAttribute("look") === "painted" ? "painted" : "realistic";
101
99
  this.#model.light = readLight(this.getAttribute("light"));
102
- this.#model.stepMs = Math.max(1000, Math.abs(this.#speed) * 1000);
100
+ this.#model.stepMs = Math.max(1000, Math.abs(this.#clock.speed) * 1000);
103
101
  if (!this.hasAttribute("role"))
104
102
  this.setAttribute("role", "img");
105
103
  if (!this.hasAttribute("tabindex"))
@@ -124,6 +122,7 @@ export class WakuseiSpace extends HTMLElement {
124
122
  painter: () => this.#painterNow(),
125
123
  cap: () => this.#textureCap(),
126
124
  invalidate: () => this.#gate?.request(),
125
+ advance: () => this.#advance(),
127
126
  onLabels: (places) => this.#placeLabels(places),
128
127
  onMoving: () => this.#gate?.request(),
129
128
  onRideLost: () => this.#rideLost(),
@@ -139,7 +138,6 @@ export class WakuseiSpace extends HTMLElement {
139
138
  else if (this.#state === "loading")
140
139
  this.#set("unavailable");
141
140
  });
142
- this.#last = performance.now();
143
141
  this.#schedule();
144
142
  this.addEventListener("pointerdown", this.#down);
145
143
  this.#hands = carryControls(this, { limits: CARRIED_LIMITS, onChange: (view, info) => this.setView(view, info.final) });
@@ -190,31 +188,30 @@ export class WakuseiSpace extends HTMLElement {
190
188
  }
191
189
  /** The moment shown: milliseconds since the epoch, or "now" to follow the clock. Setting it points the camera at the same place on the ground again. */
192
190
  get time() {
191
+ this.#advance();
193
192
  return this.#model.time;
194
193
  }
195
194
  set time(value) {
196
- this.#mode = value instanceof Date ? value.getTime() : value;
197
- this.#model.time = this.#mode === "now" ? Date.now() : this.#mode;
195
+ this.#clock.set(value instanceof Date ? value.getTime() : value);
196
+ this.#model.time = this.#clock.time;
198
197
  this.#model.anchor = null;
199
- this.#last = performance.now();
200
198
  this.#schedule();
201
199
  this.#gate?.request();
202
200
  }
203
201
  /** Seconds of game time per second: 1 is real time, 0 holds still, negative runs backwards. */
204
202
  get speed() {
205
- return this.#speed;
203
+ return this.#clock.speed;
206
204
  }
207
205
  set speed(value) {
208
- // time up to this moment is worked out at the old speed, so a hold keeps the moment the reader saw last
209
- if (this.#speed !== 0 && this.isConnected)
210
- this.#advance(performance.now());
211
- const was = this.#speed;
212
- this.#speed = Number.isFinite(value) ? value : 1;
213
- this.#model.stepMs = Math.max(1000, Math.abs(this.#speed) * 1000);
214
- this.#last = performance.now();
206
+ // time up to this moment is worked out at the old speed (the clock does that), so a hold keeps the moment the reader saw last
207
+ const was = this.#clock.speed;
208
+ this.#clock.setSpeed(value);
209
+ if (this.isConnected)
210
+ this.#model.time = this.#clock.time;
211
+ this.#model.stepMs = Math.max(1000, Math.abs(this.#clock.speed) * 1000);
215
212
  this.#schedule();
216
213
  this.#gate?.request();
217
- if (this.#speed === 0 && was !== 0)
214
+ if (this.#clock.speed === 0 && was !== 0)
218
215
  this.#announce(false);
219
216
  }
220
217
  /** `compressed` (the default: every direction real, the distances squeezed) or `true` (real distances and sizes). */
@@ -348,7 +345,6 @@ export class WakuseiSpace extends HTMLElement {
348
345
  this.#lostTold = false;
349
346
  const pose = model.pose;
350
347
  model.transition = (options.smooth ?? true) && !reduced && pose.valid ? { 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;
351
- this.#last = performance.now();
352
348
  this.#schedule();
353
349
  this.#gate?.request();
354
350
  this.dispatchEvent(new CustomEvent("wakusei-ride", { detail: this.riding }));
@@ -412,10 +408,11 @@ export class WakuseiSpace extends HTMLElement {
412
408
  canvas: stats?.canvas ?? { width: 0, height: 0, pixels: 0 },
413
409
  distance: this.#model.drawn?.distance ?? 0,
414
410
  assets: { entries: stats?.assets.entries ?? 0, refs: stats?.assets.refs ?? 0 },
415
- time: this.#model.time,
411
+ time: this.time,
416
412
  lookups: this.#model.lookups,
417
413
  camera: { position: [...this.#model.camera.position], fov: this.#model.camera.fov },
418
414
  bodies: { look: this.#model.look, painted: this.#model.painted.bodies, moons: this.#model.painted.moons, held: this.#model.painted.held },
415
+ tiles: { ...this.#model.tiles },
419
416
  ride: { riding: this.#model.ride !== null, lost: this.#model.rideLost, gliding: this.#model.transition !== null },
420
417
  };
421
418
  }
@@ -429,23 +426,20 @@ export class WakuseiSpace extends HTMLElement {
429
426
  #schedule() {
430
427
  clearTimeout(this.#timer);
431
428
  this.#timer = undefined;
432
- if (!this.isConnected || this.#speed === 0)
429
+ if (!this.isConnected || this.#clock.speed === 0)
433
430
  return;
434
431
  const riding = this.#model.ride !== null;
435
- const pixels = Math.max(this.#model.motion || 0.015, this.#model.rideMotion) * Math.abs(this.#speed);
432
+ const pixels = Math.max(this.#model.motion || 0.015, this.#model.rideMotion) * Math.abs(this.#clock.speed);
436
433
  let perSecond = Math.min(30, Math.max(1, pixels));
437
434
  if (riding && typeof matchMedia === "function" && matchMedia("(prefers-reduced-motion: reduce)").matches)
438
435
  perSecond = 1;
439
- const wait = !riding && this.#speed === 1 ? REAL_TIME_TICK_MS : 1000 / perSecond;
436
+ const wait = !riding && this.#clock.speed === 1 ? REAL_TIME_TICK_MS : 1000 / perSecond;
440
437
  this.#timer = setTimeout(this.#tick, wait);
441
438
  }
442
- /** Bring the moment shown up to `now` (the page's own clock, in milliseconds) at the speed it has run at. */
443
- #advance(now) {
444
- if (this.#speed === 1 && this.#mode === "now")
445
- this.#model.time = Date.now();
446
- else
447
- this.#model.time += (now - this.#last) * this.#speed;
448
- this.#last = now;
439
+ /** Bring the moment shown up to the page's clock, by the time that really passed at the speed it has run at, however few frames were drawn (`createGameClock`). */
440
+ #advance() {
441
+ if (this.isConnected)
442
+ this.#model.time = this.#clock.advance();
449
443
  }
450
444
  /** Tell the page the moment shown: at most four times a second while time runs (`running`), and once, at once, when it holds. */
451
445
  #announce(running) {
@@ -456,11 +450,11 @@ export class WakuseiSpace extends HTMLElement {
456
450
  this.dispatchEvent(new CustomEvent("wakusei-timechange", { detail: { time: this.#model.time, running } }));
457
451
  }
458
452
  #tick = () => {
459
- this.#advance(performance.now());
453
+ this.#advance();
460
454
  this.#gate?.request();
461
455
  this.#schedule();
462
456
  this.dispatchEvent(new CustomEvent("wakusei-time", { detail: { time: this.#model.time } }));
463
- this.#announce(this.#speed !== 0);
457
+ this.#announce(this.#clock.speed !== 0);
464
458
  };
465
459
  // --- labels --------------------------------------------------------------------------------------------------------------------------------------
466
460
  #nameOf(id) {
@@ -546,7 +540,7 @@ export class WakuseiSpace extends HTMLElement {
546
540
  };
547
541
  // --- the rest --------------------------------------------------------------------------------------------------------------------------------------
548
542
  #painterNow() {
549
- return (this.#painter ??= createPainter({ textureBase: texturePaths(this).textureBase(), farBase: texturePaths(this).farBase() }));
543
+ return (this.#painter ??= createPainter({ textureBase: texturePaths(this).textureBase(), farBase: texturePaths(this).farBase(), tiles: tilesBase(this) ?? "off" }));
550
544
  }
551
545
  #textureCap() {
552
546
  const given = Number(this.getAttribute("max-texture"));
@@ -135,6 +135,13 @@ export interface SpaceModel {
135
135
  moons: number;
136
136
  held: number;
137
137
  };
138
+ /** What the windows of finer map tiles on the bodies are doing, summed at the last draw: tile fetches made, tiles being fetched, tiles or windows that failed, and the finest level on a body now (`null` for none). */
139
+ tiles: {
140
+ requested: number;
141
+ loading: number;
142
+ failed: number;
143
+ shown: number | null;
144
+ };
138
145
  /** How many frames the ephemeris has worked out. */
139
146
  lookups: number;
140
147
  /** Pixels a game-second moves things on the screen (the fastest turning body in sight): the draw rate follows it. */
@@ -183,6 +190,8 @@ export interface SpaceDeps {
183
190
  painter: () => WakuseiPainter;
184
191
  cap: () => number;
185
192
  invalidate: () => void;
193
+ /** Bring the model's time up to the page's clock; called at the start of each draw, so a picture drawn late shows the moment it is drawn at and not the one of the last tick. */
194
+ advance?: () => void;
186
195
  /** Where each body is on the screen, in CSS pixels in the box, or null when it is behind the camera. Called at each draw. */
187
196
  onLabels?: (places: Record<SpaceBody, BodyPlace | null>) => void;
188
197
  /** Called at the end of a draw while the camera is moving from one ride to another, so the next frame is asked for (at the page's frame cap). */
@@ -42,6 +42,7 @@ export const newSpaceModel = () => ({
42
42
  look: "realistic",
43
43
  drawn: null,
44
44
  painted: { bodies: 0, moons: 0, held: 0 },
45
+ tiles: { requested: 0, loading: 0, failed: 0, shown: null },
45
46
  lookups: 0,
46
47
  motion: 0,
47
48
  drawnView: null,
@@ -129,6 +130,7 @@ export function createSpaceItem(model, deps) {
129
130
  scene,
130
131
  camera,
131
132
  update() {
133
+ deps.advance?.();
132
134
  const frame = track.at(model.time);
133
135
  model.lookups = track.lookups;
134
136
  if (model.stepMs !== trackStep) {
@@ -313,7 +315,7 @@ export function createSpaceItem(model, deps) {
313
315
  drawing.object.quaternion.copy(camera.quaternion);
314
316
  const lit = id !== "sun" && model.light === "lit";
315
317
  const toSun = lit ? unit([camera.position.x - pos[id][0], camera.position.y - pos[id][1], camera.position.z - pos[id][2]]) : toSunOf(id);
316
- drawing.update({ toSun, ms: model.time, radiusPx: px[id] });
318
+ drawing.update({ toSun, ms: model.time, radiusPx: px[id], view: { camera, width: size.width, height: size.height } });
317
319
  }
318
320
  model.painted.bodies = paintedBodies;
319
321
  // the orbits round the Sun, placed by the same mapping as the bodies
@@ -367,7 +369,7 @@ export function createSpaceItem(model, deps) {
367
369
  drawing.object.position.set(at[0], at[1], at[2]);
368
370
  drawing.object.scale.setScalar(drawn);
369
371
  const moonPx = away <= drawn * 1.001 ? 1e5 : ((size.height / 2) * (drawn / Math.sqrt(away * away - drawn * drawn))) / half;
370
- drawing.update({ toSun: model.light === "lit" ? unit([camera.position.x - at[0], camera.position.y - at[1], camera.position.z - at[2]]) : toSunOf(id), ms: model.time, radiusPx: moonPx });
372
+ drawing.update({ toSun: model.light === "lit" ? unit([camera.position.x - at[0], camera.position.y - at[1], camera.position.z - at[2]]) : toSunOf(id), ms: model.time, radiusPx: moonPx, view: { camera, width: size.width, height: size.height } });
371
373
  }
372
374
  }
373
375
  let soonest = Number.POSITIVE_INFINITY;
@@ -393,6 +395,18 @@ export function createSpaceItem(model, deps) {
393
395
  paintedMoons += 1;
394
396
  model.painted.moons = paintedMoons;
395
397
  model.painted.held = moonSlots.size;
398
+ const tiles = { requested: 0, loading: 0, failed: 0, shown: null };
399
+ for (const painting of [...[...slots.values()].map((slot) => slot.drawing), ...[...moonSlots.values()].map((held) => held.slot.drawing)]) {
400
+ const stats = painting?.tiles;
401
+ if (stats === undefined)
402
+ continue;
403
+ tiles.requested += stats.requested;
404
+ tiles.loading += stats.loading;
405
+ tiles.failed += stats.failed;
406
+ if (stats.shown !== null)
407
+ tiles.shown = Math.max(tiles.shown ?? 0, stats.shown);
408
+ }
409
+ model.tiles = tiles;
396
410
  if (deps.onLabels && model.labels) {
397
411
  const places = {};
398
412
  for (const id of SPACE_BODIES) {
package/dist/version.d.ts CHANGED
@@ -1 +1 @@
1
- export declare const VERSION = "0.28.0";
1
+ export declare const VERSION = "0.30.0";
package/dist/version.js CHANGED
@@ -1 +1 @@
1
- export const VERSION = "0.28.0";
1
+ export const VERSION = "0.30.0";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@johnmorrisdotca/wakusei-planets",
3
- "version": "0.28.0",
3
+ "version": "0.30.0",
4
4
  "description": "Wakusei Planets 惑星 — The Sun, planets and moons for the web: each as a clean line drawing, a shaded realistic drawing or a painted surface map, with real sizes, axial tilt and rotation as data, and positions of the moons for any date and time. SVG for the lines, three.js (optional) for the rest.",
5
5
  "keywords": [
6
6
  "planets",
@@ -89,12 +89,14 @@
89
89
  "sideEffects": [
90
90
  "./dist/element.js",
91
91
  "./dist/portrait/element.js",
92
+ "./dist/lineup/element.js",
92
93
  "./dist/space/element.js",
93
94
  "./dist/bodies/*.js",
94
95
  "./dist/tables/*.js",
95
96
  "./dist/craft/*.js",
96
97
  "./src/element.ts",
97
98
  "./src/portrait/element.ts",
99
+ "./src/lineup/element.ts",
98
100
  "./src/space/element.ts",
99
101
  "./src/bodies/*.ts",
100
102
  "./src/tables/*.ts",
@@ -119,6 +121,11 @@
119
121
  "import": "./dist/portrait/element.js",
120
122
  "default": "./dist/portrait/element.js"
121
123
  },
124
+ "./lineup": {
125
+ "types": "./dist/lineup/element.d.ts",
126
+ "import": "./dist/lineup/element.js",
127
+ "default": "./dist/lineup/element.js"
128
+ },
122
129
  "./space": {
123
130
  "types": "./dist/space/index.d.ts",
124
131
  "import": "./dist/space/index.js",