@johnmorrisdotca/wakusei-planets 0.31.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.
@@ -5,6 +5,8 @@ import { createGameClock } from "./clock.js";
5
5
  import { bodyKindOf, findBody, hopFrom, hostOf, HOP_ORDER } from "./bodies.js";
6
6
  import { chooseLabels } from "./labels.js";
7
7
  import { createPainter } from "../scene/painter.js";
8
+ import { createDoorHost } from "../door/host.js";
9
+ import { ownCredit } from "../door/own.js";
8
10
  import { setThreeLoader, takeRenderer } from "../scene/shared.js";
9
11
  import { texturePaths, tilesBase } from "../scene/textures.js";
10
12
  import { AU_IN_EARTH_RADII, EARTH_RADIUS_KM, frameAt, SPACE_DWARFS, SPACE_PLANETS } from "./frame.js";
@@ -81,7 +83,7 @@ const STYLE = `<style>:host{display:block;aspect-ratio:1/1;position:relative;ove
81
83
  * ```
82
84
  */
83
85
  export class WakuseiSpace extends HTMLElement {
84
- static observedAttributes = ["center", "time", "speed", "scale", "labels", "orbits", "help", "look", "light", "lang"];
86
+ static observedAttributes = ["center", "time", "speed", "scale", "labels", "orbits", "help", "look", "light", "lang", "data"];
85
87
  #model = newSpaceModel();
86
88
  #clock = createGameClock("now", 1);
87
89
  #view;
@@ -101,6 +103,20 @@ export class WakuseiSpace extends HTMLElement {
101
103
  #scaleLine;
102
104
  #hint = "choose to look at it";
103
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());
104
120
  #focusBeforeRide = "earth";
105
121
  #lostTold = false;
106
122
  #hands;
@@ -118,6 +134,7 @@ export class WakuseiSpace extends HTMLElement {
118
134
  this.#model.help = this.hasAttribute("help") && this.getAttribute("help") !== "off";
119
135
  this.#model.look = this.getAttribute("look") === "painted" ? "painted" : "realistic";
120
136
  this.#model.light = readLight(this.getAttribute("light"));
137
+ this.#door.book.ownOnly(this.getAttribute("data") === "own");
121
138
  this.#model.stepMs = Math.max(1000, Math.abs(this.#clock.speed) * 1000);
122
139
  if (!this.hasAttribute("role"))
123
140
  this.setAttribute("role", "img");
@@ -144,10 +161,14 @@ export class WakuseiSpace extends HTMLElement {
144
161
  this.#taken = taken;
145
162
  const item = createSpaceItem(this.#model, {
146
163
  painter: () => this.#painterNow(),
164
+ book: this.#door.book,
147
165
  cap: () => this.#textureCap(),
148
166
  invalidate: () => this.#gate?.request(),
149
167
  advance: () => this.#advance(),
150
- onLabels: (places) => this.#placeLabels(places),
168
+ onLabels: (places) => {
169
+ this.#placeLabels(places);
170
+ this.#door.announce();
171
+ },
151
172
  onMoving: () => this.#gate?.request(),
152
173
  onRideLost: () => this.#rideLost(),
153
174
  onPainterError: (error) => this.dispatchEvent(new CustomEvent("wakusei-bodies-unavailable", { detail: { reason: error instanceof Error ? error.message : String(error) } })),
@@ -203,6 +224,8 @@ export class WakuseiSpace extends HTMLElement {
203
224
  this.look = value === "painted" ? "painted" : "realistic";
204
225
  else if (name === "light")
205
226
  this.light = readLight(value);
227
+ else if (name === "data")
228
+ this.#door.book.ownOnly(value === "own");
206
229
  else if (name === "lang") {
207
230
  for (const [id, button] of this.#pool)
208
231
  this.#nameLabel(button, id);
@@ -555,6 +578,35 @@ export class WakuseiSpace extends HTMLElement {
555
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
556
579
  return preset === "moon" && mode === "compressed" ? zoom * (radiusIn("moon", mode) / 0.9) : zoom;
557
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();
609
+ }
558
610
  /** What the element is doing, for a page or a test. */
559
611
  get stats() {
560
612
  const stats = this.#taken?.renderer.stats;
@@ -570,6 +622,7 @@ export class WakuseiSpace extends HTMLElement {
570
622
  camera: { position: [...this.#model.camera.position], fov: this.#model.camera.fov },
571
623
  bodies: { look: this.#model.look, painted: this.#model.painted.bodies, moons: this.#model.painted.moons, held: this.#model.painted.held },
572
624
  tiles: { ...this.#model.tiles },
625
+ door: { points: this.#model.door.points, sources: this.#door.book.stats() },
573
626
  ride: { riding: this.#model.ride !== null, lost: this.#model.rideLost, gliding: this.#model.transition !== null },
574
627
  orbits: { drawn: [...this.#model.rings.drawn], miss: { ...this.#model.rings.miss }, hidden: [...this.#model.rings.hidden] },
575
628
  arrows: Object.fromEntries(Object.entries(this.#model.arrows).map(([id, list]) => [id, list.map((a) => ({ position: [...a.position], direction: [...a.direction] }))])),
@@ -803,7 +856,7 @@ export class WakuseiSpace extends HTMLElement {
803
856
  };
804
857
  // --- the rest --------------------------------------------------------------------------------------------------------------------------------------
805
858
  #painterNow() {
806
- 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 }));
807
860
  }
808
861
  #textureCap() {
809
862
  const given = Number(this.getAttribute("max-texture"));
@@ -2,6 +2,7 @@ import type * as THREE from "three";
2
2
  import type { Item3D } from "@johnmorrisdotca/kazoku-ui/renderer";
3
3
  import { type BodyId, type MoonId } from "../facts.ts";
4
4
  import type { ThreeParts } from "../scene/body.ts";
5
+ import type { SourceBook } from "../door/book.ts";
5
6
  import { type WakuseiPainter } from "../scene/painter.ts";
6
7
  import "../bodies/all.ts";
7
8
  import type { Vec3 } from "../vec.ts";
@@ -164,6 +165,11 @@ export interface SpaceModel {
164
165
  retrograde: boolean;
165
166
  arcTurn: Vec3;
166
167
  } | null;
168
+ /** The data door at the last draw: how many small bodies are in the one instanced draw, and how many tiles or marks each source gave (by id): the credits. */
169
+ door: {
170
+ points: number;
171
+ used: Record<string, number>;
172
+ };
167
173
  /** How many frames the ephemeris has worked out. */
168
174
  lookups: number;
169
175
  /** Pixels a game-second moves things on the screen (the fastest turning body in sight): the draw rate follows it. */
@@ -220,6 +226,8 @@ export interface BodyPlace {
220
226
  }
221
227
  export interface SpaceDeps {
222
228
  painter: () => WakuseiPainter;
229
+ /** The view's sources (the data door): small bodies are drawn as points from it. Left out, none are. */
230
+ book?: SourceBook;
223
231
  cap: () => number;
224
232
  invalidate: () => void;
225
233
  /** 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. */
@@ -1,4 +1,6 @@
1
1
  import { BODIES, MOONS_OF } from "../facts.js";
2
+ import { createSmallBodyPoints } from "../door/points.js";
3
+ import { toTerrestrialTime } from "../clock.js";
2
4
  import { moonOffset } from "../scene/orient.js";
3
5
  import {} from "../scene/painter.js";
4
6
  import "../bodies/all.js";
@@ -66,6 +68,7 @@ export const newSpaceModel = () => ({
66
68
  help: false,
67
69
  arrowPx: 0,
68
70
  helpDrawn: null,
71
+ door: { points: 0, used: {} },
69
72
  lookups: 0,
70
73
  motion: 0,
71
74
  drawnView: null,
@@ -107,6 +110,11 @@ export function createSpaceItem(model, deps) {
107
110
  const track = createTrack(model.stepMs);
108
111
  let trackStep = model.stepMs;
109
112
  const scene = new three.Scene();
113
+ // the small bodies of the record sources, in one instanced draw (a page's own subset of three needs the two instanced classes for it)
114
+ const points = deps.book !== undefined && typeof three.InstancedBufferGeometry === "function" ? createSmallBodyPoints(three) : undefined;
115
+ if (points !== undefined)
116
+ scene.add(points.object);
117
+ let pointsVersion = -1;
110
118
  const camera = new three.PerspectiveCamera(FOV, width / Math.max(1, height), 0.05, 1000);
111
119
  const target = new three.Vector3();
112
120
  const projected = new three.Vector3();
@@ -466,6 +474,15 @@ export function createSpaceItem(model, deps) {
466
474
  drawing.update({ toSun, ms: model.time, radiusPx: px[id], view: { camera, width: size.width, height: size.height } });
467
475
  }
468
476
  model.painted.bodies = paintedBodies;
477
+ if (points !== undefined && deps.book !== undefined) {
478
+ if (pointsVersion !== deps.book.version) {
479
+ pointsVersion = deps.book.version;
480
+ points.set(deps.book.smallBodies());
481
+ }
482
+ const ratio = model.ratio();
483
+ points.update({ jd: toTerrestrialTime(model.time) / 86_400_000 + 2_440_587.5, mode, sun: sunPos, width: size.width * ratio, height: size.height * ratio, ratio });
484
+ model.door.points = points.count;
485
+ }
469
486
  // the orbits round the Sun, placed by the same mapping as the bodies
470
487
  const drawnRings = [];
471
488
  const hiddenRings = [];
@@ -666,6 +683,16 @@ export function createSpaceItem(model, deps) {
666
683
  tiles.shown = Math.max(tiles.shown ?? 0, stats.shown);
667
684
  }
668
685
  model.tiles = tiles;
686
+ const used = {};
687
+ for (const painting of [...[...slots.values()].map((slot) => slot.drawing), ...[...moonSlots.values()].map((held) => held.slot.drawing)]) {
688
+ const shown = painting?.tiles;
689
+ if (shown?.source !== undefined)
690
+ used[shown.source.id] = (used[shown.source.id] ?? 0) + shown.used;
691
+ }
692
+ if (points !== undefined && deps.book !== undefined && points.count > 0)
693
+ for (const [id, read] of deps.book.recordReads())
694
+ used[id] = read.records.length;
695
+ model.door.used = used;
669
696
  if (deps.onLabels && model.labels) {
670
697
  const places = { ...moonPlaces };
671
698
  for (const id of SPACE_BODIES) {
@@ -684,6 +711,7 @@ export function createSpaceItem(model, deps) {
684
711
  size = { width: w, height: h };
685
712
  },
686
713
  dispose() {
714
+ points?.dispose();
687
715
  if (releaseTimer !== undefined)
688
716
  clearTimeout(releaseTimer);
689
717
  ended = true;
package/dist/version.d.ts CHANGED
@@ -1 +1 @@
1
- export declare const VERSION = "0.31.0";
1
+ export declare const VERSION = "0.32.0";
package/dist/version.js CHANGED
@@ -1 +1 @@
1
- export const VERSION = "0.31.0";
1
+ export const VERSION = "0.32.0";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@johnmorrisdotca/wakusei-planets",
3
- "version": "0.31.0",
3
+ "version": "0.32.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",
@@ -141,6 +141,11 @@
141
141
  "import": "./dist/time.js",
142
142
  "default": "./dist/time.js"
143
143
  },
144
+ "./door": {
145
+ "types": "./dist/door/index.d.ts",
146
+ "import": "./dist/door/index.js",
147
+ "default": "./dist/door/index.js"
148
+ },
144
149
  "./stars": {
145
150
  "types": "./dist/stars.d.ts",
146
151
  "import": "./dist/stars.js",