@johnmorrisdotca/chikyu-earth 0.40.0 → 0.42.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.
@@ -18,6 +18,7 @@ import { type BodyLabelState } from "../realistic/body-label.ts";
18
18
  import { type MeteorState } from "../meteors/layer.ts";
19
19
  import { type BorderState } from "../borders/layer.ts";
20
20
  import { type GridState } from "../grid/layer.ts";
21
+ import { type RouteState } from "../route/layer.ts";
21
22
  type Three = ThreeParts;
22
23
  /** How much of the view's shorter side the Earth's disc fills at zoom 1 when the page does not say: 0.9, so the atmosphere's glow (8% beyond the disc) is inside the view and never cut. */
23
24
  export declare const DEFAULT_EARTH_SIZE = 0.9;
@@ -53,6 +54,15 @@ export declare function fitDistance(size: number, aspect: number): number;
53
54
  export type CameraMode = "ground" | "space" | "sun";
54
55
  /** A moon that has been out of sight this long is let go (its pictures too). */
55
56
  export declare const MOON_KEEP_MS = 4000;
57
+ /**
58
+ * A body of the Sun, the Moon and the planets is made by the painter (and so gets its mapped picture) only when it is in the view and at least this many CSS pixels across its radius; a smaller one, or one out of the view, is a plain sphere of the right colour (the Moon, the view's own Moon).
59
+ * It is let go (its pictures with it) once it has been smaller than `BODY_LET_GO_PX`, or out of the view, for `BODY_KEEP_MS`.
60
+ */
61
+ export declare const BODY_MAKE_PX = 6;
62
+ export declare const BODY_LET_GO_PX = 3;
63
+ export declare const BODY_KEEP_MS = 4000;
64
+ /** For a reader who asked for reduced motion, the pictures that arrive within this time of one that was drawn ask for one draw between them (none at all while the clock steps). */
65
+ export declare const ARRIVAL_GATHER_MS = 1000;
56
66
  /**
57
67
  * The eleven bodies the space view places, names and lets a reader choose: the Earth, the Sun, the Moon, the seven other planets and Pluto.
58
68
  *
@@ -159,6 +169,8 @@ export interface SpaceModel {
159
169
  held: number;
160
170
  moon: boolean;
161
171
  tiles: number;
172
+ mapped: number;
173
+ pending: number;
162
174
  };
163
175
  /** How many times astronomy-engine has been asked for a frame. */
164
176
  lookups: number;
@@ -174,6 +186,8 @@ export interface SpaceModel {
174
186
  borders: BorderState;
175
187
  /** The meridians and parallels on the Earth (src/grid/layer.ts): off unless the element switches them on. */
176
188
  grid: GridState;
189
+ /** A route between two places, on the Earth (src/route/layer.ts): off unless the element sets it. */
190
+ route: RouteState;
177
191
  /** The body or satellite the camera rides along with, or null. */
178
192
  ride: RideState | null;
179
193
  /** Set at each draw: the target of the ride could not be placed (a satellite whose elements are too old, or that is gone), so the ordinary camera was used. */
@@ -413,6 +427,8 @@ export interface SpaceDeps {
413
427
  satellitePainter?: () => SatellitePainter | undefined;
414
428
  /** The painter could not make a body (the three.js parts handed over lack something it needs); the body is drawn plain instead. */
415
429
  onPainterError?: (error: unknown) => void;
430
+ /** A body the painter drew changed (a picture arrived, a window of its tiles changed, a body was made): the painter's credit, which names the pictures on the screen now, may have changed. Told before the redraw is asked for. */
431
+ onBodies?: () => void;
416
432
  /** The map source of each layer now (the folder of the `tiles` attribute), or none: the Earth then keeps the package's own pictures. Read at every draw. */
417
433
  sources?: () => TileSources;
418
434
  /** Told once when the tile set has been read: the width in pixels of the finest level of each of its layers. */
@@ -16,6 +16,7 @@ import { createBodyLabelLayer, newBodyLabelState } from "../realistic/body-label
16
16
  import { createMeteorLayer, newMeteorState } from "../meteors/layer.js";
17
17
  import { createBorderLayer, newBorderState } from "../borders/layer.js";
18
18
  import { createGridLayer, newGridState } from "../grid/layer.js";
19
+ import { createRouteLayer, newRouteState } from "../route/layer.js";
19
20
  import { subsolarPoint, unitVector } from "../realistic/sun.js";
20
21
  /** The most satellites ringed and named at once, over all the labelled groups. */
21
22
  const MAX_MARKED = 80;
@@ -48,6 +49,15 @@ const MOON_MIN_PLANET_PX = 4;
48
49
  const MOON_MIN_PX = 2.2;
49
50
  /** A moon that has been out of sight this long is let go (its pictures too). */
50
51
  export const MOON_KEEP_MS = 4000;
52
+ /**
53
+ * A body of the Sun, the Moon and the planets is made by the painter (and so gets its mapped picture) only when it is in the view and at least this many CSS pixels across its radius; a smaller one, or one out of the view, is a plain sphere of the right colour (the Moon, the view's own Moon).
54
+ * It is let go (its pictures with it) once it has been smaller than `BODY_LET_GO_PX`, or out of the view, for `BODY_KEEP_MS`.
55
+ */
56
+ export const BODY_MAKE_PX = 6;
57
+ export const BODY_LET_GO_PX = 3;
58
+ export const BODY_KEEP_MS = MOON_KEEP_MS;
59
+ /** For a reader who asked for reduced motion, the pictures that arrive within this time of one that was drawn ask for one draw between them (none at all while the clock steps). */
60
+ export const ARRIVAL_GATHER_MS = 1000;
51
61
  const PLAIN_COLOR = (name) => (name === "Sun" ? "#ffb347" : COLORS[name] ?? "#9a948b");
52
62
  /**
53
63
  * The eleven bodies the space view places, names and lets a reader choose: the Earth, the Sun, the Moon, the seven other planets and Pluto.
@@ -80,7 +90,7 @@ export const newSpaceModel = () => ({
80
90
  bodies: "",
81
91
  light: "sun",
82
92
  drawn: null,
83
- painted: { bodies: 0, moons: 0, held: 0, moon: false, tiles: 0 },
93
+ painted: { bodies: 0, moons: 0, held: 0, moon: false, tiles: 0, mapped: 0, pending: 0 },
84
94
  lookups: 0,
85
95
  speed: 1,
86
96
  ride: null,
@@ -98,6 +108,7 @@ export const newSpaceModel = () => ({
98
108
  bodyLabel: newBodyLabelState(),
99
109
  borders: newBorderState(),
100
110
  grid: newGridState(),
111
+ route: newRouteState(),
101
112
  pick: null,
102
113
  placeSatellite: null,
103
114
  followModel: null,
@@ -168,11 +179,42 @@ export function createSpaceItem(model, deps) {
168
179
  key: "chikyu-earth:space",
169
180
  async build({ three, width, height }) {
170
181
  let size = { width, height };
182
+ let ended = false;
183
+ // Pictures arrive one by one (the Earth's maps, its finer tiles, a body's), each asking for a draw. For a reader who asked for reduced motion they are gathered, to at most one draw a second: while the clock steps
184
+ // (at least once a second) the next step shows them, so they ask for nothing; at rest, one after a quiet second is drawn at once and those that follow within the second ask for one draw when it ends.
185
+ let arrivalAt = Number.NEGATIVE_INFINITY;
186
+ let arrivalTimer;
187
+ const arrivalDraw = () => {
188
+ if (!(typeof matchMedia === "function" && matchMedia("(prefers-reduced-motion: reduce)").matches)) {
189
+ deps.invalidate();
190
+ return;
191
+ }
192
+ // real time steps once a minute unless something rides, so it is gathered like rest
193
+ if (model.speed !== 0 && (model.speed !== 1 || model.ride !== null))
194
+ return;
195
+ if (arrivalTimer !== undefined)
196
+ return;
197
+ const now = performance.now();
198
+ if (now - arrivalAt >= ARRIVAL_GATHER_MS) {
199
+ arrivalAt = now;
200
+ deps.invalidate();
201
+ return;
202
+ }
203
+ model.painted.pending += 1;
204
+ arrivalTimer = setTimeout(() => {
205
+ arrivalTimer = undefined;
206
+ if (ended)
207
+ return;
208
+ model.painted.pending -= 1;
209
+ arrivalAt = performance.now();
210
+ deps.invalidate();
211
+ }, arrivalAt + ARRIVAL_GATHER_MS - now);
212
+ };
171
213
  const textures = await createTextureSet(three, LAYERS, {
172
214
  assets: deps.assets,
173
215
  textureBase: deps.textureBase,
174
216
  cap: deps.cap,
175
- invalidate: deps.invalidate,
217
+ invalidate: arrivalDraw,
176
218
  ...(deps.onError ? { onError: deps.onError } : {}),
177
219
  ...(deps.onTier ? { onTier: deps.onTier } : {}),
178
220
  });
@@ -210,7 +252,7 @@ export function createSpaceItem(model, deps) {
210
252
  });
211
253
  const earth = new three.Mesh(grid, earthMaterial);
212
254
  // the finer map tiles, laid over the Earth's own picture where the camera is near enough to need them (they turn with the Earth: they are its children)
213
- const tiles = createTileSet(three, earth, { sunDir: sunEarth, lights: earthMaterial.uniforms["lights"], boost: earthMaterial.uniforms["boost"], ortho: earthMaterial.uniforms["ortho"], water: uniform.water, night: uniform.night, extra: { cloudMap: uniform.clouds, sunLocal, cloudShift } }, { assets: deps.assets, sources: () => deps.sources?.() ?? {}, cap: deps.cap, invalidate: deps.invalidate, ...(deps.onTiles ? { onManifest: deps.onTiles } : {}) });
255
+ const tiles = createTileSet(three, earth, { sunDir: sunEarth, lights: earthMaterial.uniforms["lights"], boost: earthMaterial.uniforms["boost"], ortho: earthMaterial.uniforms["ortho"], water: uniform.water, night: uniform.night, extra: { cloudMap: uniform.clouds, sunLocal, cloudShift } }, { assets: deps.assets, sources: () => deps.sources?.() ?? {}, cap: deps.cap, invalidate: arrivalDraw, ...(deps.onTiles ? { onManifest: deps.onTiles } : {}) });
214
256
  model.tiles = tiles;
215
257
  const clouds = new three.Mesh(grid, cloudMaterial);
216
258
  clouds.scale.setScalar(1.006);
@@ -224,9 +266,20 @@ export function createSpaceItem(model, deps) {
224
266
  const painter = deps.painter ?? plainPainter;
225
267
  const scene = new three.Scene();
226
268
  scene.add(earth, clouds, glow, moon);
227
- // the Sun, the planets and the moons are made by the painter, one slot each: a slot is filled again when the look changes, and may be filled later (a painter may be slow)
228
- let ended = false;
229
- const newSlot = (name) => ({ name, drawing: undefined, look: null, token: 0, plain: false });
269
+ const newSlot = (name) => ({ name, drawing: undefined, look: null, token: 0, plain: false, small: true, bigAt: 0, mapped: false });
270
+ /** How many drawings the painter's pictures are on now: told to the element, which writes the painter's credit only while there are any. */
271
+ const tally = () => {
272
+ let mapped = sunSlot.mapped ? 1 : 0;
273
+ if (moonSlot.mapped)
274
+ mapped += 1;
275
+ for (const p of planets.values())
276
+ if (p.slot.mapped)
277
+ mapped += 1;
278
+ for (const held of moonSlots.values())
279
+ if (held.slot.mapped)
280
+ mapped += 1;
281
+ model.painted.mapped = mapped;
282
+ };
230
283
  const lookNow = () => model.bodies || painter.looks?.[0] || "";
231
284
  const empty = (slot) => {
232
285
  slot.token += 1;
@@ -235,22 +288,28 @@ export function createSpaceItem(model, deps) {
235
288
  scene.remove(slot.drawing.object);
236
289
  slot.drawing.dispose();
237
290
  slot.drawing = undefined;
291
+ slot.mapped = false;
238
292
  };
239
293
  const fill = (slot, isMoon = false) => {
240
294
  const look = lookNow();
241
- if (slot.look === look)
295
+ const key = slot.small && !isMoon ? `small|${look}` : look;
296
+ if (slot.look === key)
242
297
  return;
243
298
  empty(slot);
244
- slot.look = look;
299
+ slot.look = key;
245
300
  const token = slot.token;
301
+ const viaPainter = !(slot.plain || look === "plain" || (slot.small && !isMoon));
246
302
  const arrive = (made) => {
247
303
  if (ended || slot.token !== token) {
248
304
  made.dispose();
249
305
  return;
250
306
  }
251
307
  slot.drawing = made;
308
+ slot.mapped = viaPainter;
252
309
  scene.add(made.object);
253
- deps.invalidate();
310
+ tally();
311
+ deps.onBodies?.();
312
+ arrivalDraw();
254
313
  };
255
314
  const fail = (error) => {
256
315
  if (ended || slot.token !== token)
@@ -263,9 +322,20 @@ export function createSpaceItem(model, deps) {
263
322
  }
264
323
  };
265
324
  try {
266
- const made = (slot.plain || look === "plain" ? plainPainter : painter).make(three, slot.name, { look, invalidate: () => deps.invalidate(), ratio: model.ratio(), maxTexture: deps.cap(), mapped: true });
267
- if (typeof made.then === "function")
268
- made.then(arrive, fail);
325
+ const made = (viaPainter ? painter : plainPainter).make(three, slot.name, { look, invalidate: () => (deps.onBodies?.(), arrivalDraw()), ratio: model.ratio(), maxTexture: deps.cap(), mapped: true });
326
+ if (typeof made.then === "function") {
327
+ // a picture asked for and not yet here is counted, so a page (a test) can tell that more draws are still to come
328
+ model.painted.pending += 1;
329
+ made.then((drawing) => {
330
+ if (!ended)
331
+ model.painted.pending -= 1;
332
+ arrive(drawing);
333
+ }, (error) => {
334
+ if (!ended)
335
+ model.painted.pending -= 1;
336
+ fail(error);
337
+ });
338
+ }
269
339
  else
270
340
  arrive(made);
271
341
  }
@@ -288,12 +358,15 @@ export function createSpaceItem(model, deps) {
288
358
  }
289
359
  const moonSlots = new Map();
290
360
  let releaseTimer;
361
+ /** Asks for one more draw when a body that has shrunk or gone from the view is due to be let go (a view at rest draws nothing, so it would be kept for ever). */
362
+ let bodyTimer;
291
363
  let meteors = null;
292
364
  let meteorTimer;
293
365
  let nameOn = null;
294
366
  let borders = null;
295
367
  const placeRings = { sprites: null, tried: false };
296
368
  let gridLayer = null;
369
+ let routeLayer = null;
297
370
  let marksTold = false;
298
371
  // the followed satellite's model: which satellite it is for, the drawing once it is made, and a count so that a model made for a satellite no longer followed is let go
299
372
  const modelSlot = { key: "", drawing: null, turn: 0, parts: 0 };
@@ -378,11 +451,13 @@ export function createSpaceItem(model, deps) {
378
451
  for (const name of PLANETS)
379
452
  pos[name] = placeAround(mode, sunPos, helioOf(frame.planets[name]));
380
453
  const radiusOf = (name) => (name === "Earth" ? 1 : RADIUS[name][mode]);
381
- if (deps.painter && lookNow() !== "plain")
454
+ if (deps.painter && lookNow() !== "plain" && !moonSlot.small)
382
455
  fill(moonSlot, true);
383
456
  else if (moonSlot.look !== null) {
384
457
  empty(moonSlot);
385
458
  moonSlot.look = null;
459
+ tally();
460
+ deps.onBodies?.();
386
461
  }
387
462
  const paintedMoon = moonSlot.drawing;
388
463
  if (paintedMoon) {
@@ -599,6 +674,44 @@ export function createSpaceItem(model, deps) {
599
674
  px[name] = radiusPxOf(name);
600
675
  model.drawn = { frame, distance, radiusPx: px.Earth };
601
676
  layer?.resize(px.Earth);
677
+ // which bodies get their mapped pictures: those in the view and big enough to show a map. The others are plain spheres (the Moon, the view's own) and the painter is not asked for them, so it loads and holds nothing for a body too small to see.
678
+ // The choice is made here, from this draw's sizes, and acted on at the next (a body that changes asks for it); a body that has shrunk or gone is let go after `BODY_KEEP_MS`, so one that comes and goes is not made again and again.
679
+ {
680
+ const at = performance.now();
681
+ let changed = false;
682
+ let soonest = Number.POSITIVE_INFINITY;
683
+ const judge = (name, slot) => {
684
+ projected.set(pos[name][0], pos[name][1], pos[name][2]).project(camera);
685
+ const reach = Math.min(px[name], 1e5);
686
+ const inView = reach >= 1e5 || (projected.z > -1 && projected.z < 1 && Math.abs(projected.x) * (size.width / 2) <= size.width / 2 + reach && Math.abs(projected.y) * (size.height / 2) <= size.height / 2 + reach);
687
+ if (slot.small) {
688
+ if (inView && px[name] >= BODY_MAKE_PX) {
689
+ slot.small = false;
690
+ slot.bigAt = at;
691
+ changed = true;
692
+ }
693
+ }
694
+ else if (inView && px[name] >= BODY_LET_GO_PX)
695
+ slot.bigAt = at;
696
+ else if (at - slot.bigAt > BODY_KEEP_MS) {
697
+ slot.small = true;
698
+ changed = true;
699
+ }
700
+ else
701
+ soonest = Math.min(soonest, slot.bigAt + BODY_KEEP_MS - at);
702
+ };
703
+ if (deps.painter && lookNow() !== "plain") {
704
+ judge("Sun", sunSlot);
705
+ judge("Moon", moonSlot);
706
+ for (const name of PLANETS)
707
+ judge(name, planets.get(name).slot);
708
+ }
709
+ if (bodyTimer !== undefined)
710
+ clearTimeout(bodyTimer);
711
+ bodyTimer = soonest < Number.POSITIVE_INFINITY ? setTimeout(() => deps.invalidate(), soonest + 50) : undefined;
712
+ if (changed)
713
+ deps.invalidate();
714
+ }
602
715
  // the finer map tiles: the camera as the Earth's own axes have it, so a camera a few kilometres up asks for the tiles under it at the level its height needs
603
716
  {
604
717
  // the Earth is turned about the y axis and sits at the origin, so its own axes are the world's turned back by that angle
@@ -639,7 +752,7 @@ export function createSpaceItem(model, deps) {
639
752
  for (const drawing of [sunSlot.drawing, moonSlot.drawing, ...[...planets.values()].map((p) => p.slot.drawing), ...[...moonSlots.values()].map((held) => held.slot.drawing)])
640
753
  if (drawing?.tiles?.shown != null)
641
754
  tiled += 1;
642
- model.painted = { bodies: paintedBodies, moons: paintedMoons, held: 0, moon: paintedMoon !== undefined, tiles: tiled };
755
+ model.painted = { bodies: paintedBodies, moons: paintedMoons, held: 0, moon: paintedMoon !== undefined, tiles: tiled, mapped: model.painted.mapped, pending: model.painted.pending };
643
756
  // the followed satellite as a model, when the page has one for it: made once, placed where the satellite is, pointed the way it flies, sized to read and lit by the Sun
644
757
  const satellitePainter = deps.satellitePainter?.();
645
758
  const group = followed ? model.satellites.groups.get(followed.ref.group) : undefined;
@@ -765,7 +878,7 @@ export function createSpaceItem(model, deps) {
765
878
  // the ringed satellites first, so their names are laid out with the places' in this same draw
766
879
  model.placeSatellite = layer ? (ref) => layer.placeOf(ref, camera, size) : null;
767
880
  if (deps.onSatelliteMarks) {
768
- if (layer && model.satellites.marked.size > 0) {
881
+ if (layer && (model.satellites.marked.size > 0 || model.satellites.pinned.length > 0)) {
769
882
  const marks = [];
770
883
  for (const name of model.satellites.marked) {
771
884
  const group = model.satellites.groups.get(name);
@@ -778,6 +891,15 @@ export function createSpaceItem(model, deps) {
778
891
  marks.push({ ref, ...at });
779
892
  }
780
893
  }
894
+ for (const ref of model.satellites.pinned) {
895
+ if (marks.length >= MAX_MARKED)
896
+ break;
897
+ if (marks.some((mark) => mark.ref.group === ref.group && mark.ref.index === ref.index))
898
+ continue;
899
+ const at = layer.placeOf(ref, camera, size);
900
+ if (at)
901
+ marks.push({ ref, ...at });
902
+ }
781
903
  marksTold = true;
782
904
  deps.onSatelliteMarks(marks);
783
905
  }
@@ -949,6 +1071,16 @@ export function createSpaceItem(model, deps) {
949
1071
  }
950
1072
  }
951
1073
  gridLayer?.sync(Math.min(px.Earth, 1e5));
1074
+ if ((model.route.ends || routeLayer) && !routeLayer && !model.route.failed) {
1075
+ try {
1076
+ routeLayer = createRouteLayer(three, earth, model.route);
1077
+ }
1078
+ catch (error) {
1079
+ model.route.failed = true;
1080
+ console.warn(error instanceof Error ? error.message : error);
1081
+ }
1082
+ }
1083
+ routeLayer?.sync();
952
1084
  if (model.rideLost)
953
1085
  deps.onRideLost?.();
954
1086
  if (model.transition)
@@ -961,6 +1093,10 @@ export function createSpaceItem(model, deps) {
961
1093
  dispose() {
962
1094
  if (releaseTimer !== undefined)
963
1095
  clearTimeout(releaseTimer);
1096
+ if (bodyTimer !== undefined)
1097
+ clearTimeout(bodyTimer);
1098
+ if (arrivalTimer !== undefined)
1099
+ clearTimeout(arrivalTimer);
964
1100
  if (meteorTimer !== undefined)
965
1101
  clearTimeout(meteorTimer);
966
1102
  meteors?.dispose();
@@ -968,6 +1104,7 @@ export function createSpaceItem(model, deps) {
968
1104
  borders?.dispose();
969
1105
  placeRings.sprites?.dispose();
970
1106
  gridLayer?.dispose();
1107
+ routeLayer?.dispose();
971
1108
  modelSlot.turn += 1;
972
1109
  dropModel();
973
1110
  layer?.dispose();
@@ -977,6 +1114,7 @@ export function createSpaceItem(model, deps) {
977
1114
  tiles.dispose();
978
1115
  textures.dispose();
979
1116
  ended = true;
1117
+ model.painted.pending = 0;
980
1118
  empty(sunSlot);
981
1119
  empty(moonSlot);
982
1120
  for (const held of moonSlots.values())
package/dist/types.d.ts CHANGED
@@ -86,4 +86,9 @@ export interface GlobeOptions {
86
86
  style?: StyleName;
87
87
  /** Draw a light graticule under the countries. Default false. */
88
88
  graticule?: boolean;
89
+ /** A route between two places, drawn over the countries. Absent, none. */
90
+ route?: {
91
+ from: GeoPoint;
92
+ to: GeoPoint;
93
+ } | null;
89
94
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@johnmorrisdotca/chikyu-earth",
3
- "version": "0.40.0",
3
+ "version": "0.42.0",
4
4
  "description": "Chikyu Earth 地球 — A line-drawn globe for the web: country outlines that turn at the Earth's own rate, centred on the place you give it, with an optional realistic Earth from space (three.js), the Sun, Moon and planets, satellites on their orbits, and markers for any place. A web component and an SVG string.",
5
5
  "keywords": [
6
6
  "earth",
@@ -255,7 +255,7 @@
255
255
  "@johnmorrisdotca/hata": "^1.8.0",
256
256
  "@johnmorrisdotca/hoshi-stars": "^0.30.0",
257
257
  "@johnmorrisdotca/kuni": "^1.6.1",
258
- "@johnmorrisdotca/wakusei-planets": "^0.38.0",
258
+ "@johnmorrisdotca/wakusei-planets": "^0.39.0",
259
259
  "@playwright/test": "1.64.0",
260
260
  "@sveltejs/vite-plugin-svelte": "^7.3.1",
261
261
  "@types/d3-geo": "^3.1.0",