@johnmorrisdotca/chikyu-earth 0.41.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.
@@ -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,23 +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);
310
+ tally();
253
311
  deps.onBodies?.();
254
- deps.invalidate();
312
+ arrivalDraw();
255
313
  };
256
314
  const fail = (error) => {
257
315
  if (ended || slot.token !== token)
@@ -264,9 +322,20 @@ export function createSpaceItem(model, deps) {
264
322
  }
265
323
  };
266
324
  try {
267
- const made = (slot.plain || look === "plain" ? plainPainter : painter).make(three, slot.name, { look, invalidate: () => (deps.onBodies?.(), deps.invalidate()), ratio: model.ratio(), maxTexture: deps.cap(), mapped: true });
268
- if (typeof made.then === "function")
269
- 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
+ }
270
339
  else
271
340
  arrive(made);
272
341
  }
@@ -289,12 +358,15 @@ export function createSpaceItem(model, deps) {
289
358
  }
290
359
  const moonSlots = new Map();
291
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;
292
363
  let meteors = null;
293
364
  let meteorTimer;
294
365
  let nameOn = null;
295
366
  let borders = null;
296
367
  const placeRings = { sprites: null, tried: false };
297
368
  let gridLayer = null;
369
+ let routeLayer = null;
298
370
  let marksTold = false;
299
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
300
372
  const modelSlot = { key: "", drawing: null, turn: 0, parts: 0 };
@@ -379,11 +451,13 @@ export function createSpaceItem(model, deps) {
379
451
  for (const name of PLANETS)
380
452
  pos[name] = placeAround(mode, sunPos, helioOf(frame.planets[name]));
381
453
  const radiusOf = (name) => (name === "Earth" ? 1 : RADIUS[name][mode]);
382
- if (deps.painter && lookNow() !== "plain")
454
+ if (deps.painter && lookNow() !== "plain" && !moonSlot.small)
383
455
  fill(moonSlot, true);
384
456
  else if (moonSlot.look !== null) {
385
457
  empty(moonSlot);
386
458
  moonSlot.look = null;
459
+ tally();
460
+ deps.onBodies?.();
387
461
  }
388
462
  const paintedMoon = moonSlot.drawing;
389
463
  if (paintedMoon) {
@@ -600,6 +674,44 @@ export function createSpaceItem(model, deps) {
600
674
  px[name] = radiusPxOf(name);
601
675
  model.drawn = { frame, distance, radiusPx: px.Earth };
602
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
+ }
603
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
604
716
  {
605
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
@@ -640,7 +752,7 @@ export function createSpaceItem(model, deps) {
640
752
  for (const drawing of [sunSlot.drawing, moonSlot.drawing, ...[...planets.values()].map((p) => p.slot.drawing), ...[...moonSlots.values()].map((held) => held.slot.drawing)])
641
753
  if (drawing?.tiles?.shown != null)
642
754
  tiled += 1;
643
- 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 };
644
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
645
757
  const satellitePainter = deps.satellitePainter?.();
646
758
  const group = followed ? model.satellites.groups.get(followed.ref.group) : undefined;
@@ -766,7 +878,7 @@ export function createSpaceItem(model, deps) {
766
878
  // the ringed satellites first, so their names are laid out with the places' in this same draw
767
879
  model.placeSatellite = layer ? (ref) => layer.placeOf(ref, camera, size) : null;
768
880
  if (deps.onSatelliteMarks) {
769
- if (layer && model.satellites.marked.size > 0) {
881
+ if (layer && (model.satellites.marked.size > 0 || model.satellites.pinned.length > 0)) {
770
882
  const marks = [];
771
883
  for (const name of model.satellites.marked) {
772
884
  const group = model.satellites.groups.get(name);
@@ -779,6 +891,15 @@ export function createSpaceItem(model, deps) {
779
891
  marks.push({ ref, ...at });
780
892
  }
781
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
+ }
782
903
  marksTold = true;
783
904
  deps.onSatelliteMarks(marks);
784
905
  }
@@ -950,6 +1071,16 @@ export function createSpaceItem(model, deps) {
950
1071
  }
951
1072
  }
952
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();
953
1084
  if (model.rideLost)
954
1085
  deps.onRideLost?.();
955
1086
  if (model.transition)
@@ -962,6 +1093,10 @@ export function createSpaceItem(model, deps) {
962
1093
  dispose() {
963
1094
  if (releaseTimer !== undefined)
964
1095
  clearTimeout(releaseTimer);
1096
+ if (bodyTimer !== undefined)
1097
+ clearTimeout(bodyTimer);
1098
+ if (arrivalTimer !== undefined)
1099
+ clearTimeout(arrivalTimer);
965
1100
  if (meteorTimer !== undefined)
966
1101
  clearTimeout(meteorTimer);
967
1102
  meteors?.dispose();
@@ -969,6 +1104,7 @@ export function createSpaceItem(model, deps) {
969
1104
  borders?.dispose();
970
1105
  placeRings.sprites?.dispose();
971
1106
  gridLayer?.dispose();
1107
+ routeLayer?.dispose();
972
1108
  modelSlot.turn += 1;
973
1109
  dropModel();
974
1110
  layer?.dispose();
@@ -978,6 +1114,7 @@ export function createSpaceItem(model, deps) {
978
1114
  tiles.dispose();
979
1115
  textures.dispose();
980
1116
  ended = true;
1117
+ model.painted.pending = 0;
981
1118
  empty(sunSlot);
982
1119
  empty(moonSlot);
983
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.41.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",