@johnmorrisdotca/chikyu-earth 0.8.0 → 0.9.1

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.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,36 @@
1
1
  # Changelog
2
2
 
3
+ ## 0.9.1
4
+
5
+ The ground now follows the pointer, in every look and when riding.
6
+
7
+ - **The Earth turned the wrong way up and down** (MODULES-200). A drag up made the ground go down in the line, thick, realistic and space looks, and the up arrow did the same: the controls' tilt was added to the latitude of the view, and it should be subtracted (the top of the globe tips away as the tilt grows, as the controls say). It is now one sign in `lookAt` and `projectionView` (`src/view.ts`) and one in the space view, so the flat globe, the realistic Earth and the space view agree. **A tilt in an address or a call now tips the other way.**
8
+ Left and right were right already. Auto-turn turns the real way: the Earth goes round west to east, so with north up the ground moves from left to right, the same way as a drag right and the right arrow.
9
+ - **A ride could not be dragged** (MODULES-199). Riding at a body, a drag turned the camera's head round the straight-down line, which from the ISS only spun the picture, and tilt could not go the other way past the straight-down look, so a drag seemed to do nothing. `ridePose` now looks about from the base look as a head on a pivot does: yaw turns the camera to its left and tilt tips it towards the bottom of its picture, so a drag right moves the ground right and a drag up moves it up, about one and a half pixels a pixel, the pace of the line globe and the Earth from space. The angle for a pixel follows the field of view, so zooming in does not make the ground race. Behind a satellite (`view: "follow"`) the drag is mirrored, because the Earth is behind what the camera looks at, and the Earth follows the pointer.
10
+ - **The demo keeps the address's yaw and tilt when it opens on a ride** (it set both to 0 as the ride began), and **an address with `ride` and no `look` opens the space look**, so the reported address rides the ISS.
11
+ - **Tests**: `e2e/drag.gl.ts` drags with a real pointer, presses the arrow keys and runs Auto-turn in the line, thick, realistic and space looks, riding the ISS (down and at the horizon) and the Moon, and behind the ISS and the Moon, and checks which way the ground moved by sliding the first picture over the second; and that a ride opens with the address's turn, a drag writes it back and the address brings the same picture back; and the reported address. All fail on 0.9.0. Unit tests for `ridePose` and the view's signs.
12
+
13
+ ## 0.9.0
14
+
15
+ Ride along: the camera on the ISS, a Starlink satellite, the Moon or a planet, looking at the Earth.
16
+
17
+ - **`element.ride(target, options)`** on `<chikyu-space>`: the camera sits at a moving body at its real place for the moment shown and looks at the Earth, with the way the body is going at the top of the picture; the Earth turns and slides beneath it as the body goes along its orbit
18
+ (an ISS view goes round in about 93 minutes). `target` is a body name (`"Moon"`, `"Mars"`, ...) or a satellite of a group (`{ group, index }`). Options: `view: "from"` (the default) or `"follow"` (a fixed distance from the body, looking at it; `distanceKm` for a satellite), `look: "down"` (the default) or
19
+ `"horizon"` (tipped forward until the horizon is a little above the middle), and `smooth`. The shared controls' tilt tips the camera further, their yaw turns it round the straight-down line, their zoom narrows the 60 degree field of view. Time is the page's clock, forwards, backwards, faster or slower.
20
+ `element.riding`, the `chikyu-ride` event, and `chikyu-ride-lost` (the satellite could not be placed on this date: its elements are too old; the ordinary camera comes back). A ridden satellite is placed exactly, one SGP4 call a frame, at any speed, even where its group is hidden.
21
+ - **Switching is smooth**: another `ride` call glides the camera's place, its turn (spherical interpolation) and its field of view from where the last draw left them to the new camera over 1.2 seconds, eased; reduced motion jumps. `ride(null)` stops.
22
+ - **`ridePose(input)`** (`@johnmorrisdotca/chikyu-earth/space`, pure, no browser): where the camera looks, for a place, a way of travel, a look and the controls' yaw, tilt and zoom. Unit-tested (looking down at the Earth's centre, the way it goes at the top, the horizon look, tilt, yaw, zoom, no way of travel).
23
+ - **Budget, each line tested** (`e2e/ride.gl.ts`): the camera is within 1 km of satellite.js's own place for the ISS and 420 km up; at 60 times real time it goes round the Earth at the ISS's rate (301 game seconds: 19.4 degrees, 19.4 expected); riding the Moon puts it 10 Earth radii away in the compressed scale and 55 to 66 in the true one;
24
+ it animates continuously but at most 30 frames a second (the ISS at real time 6.5 a second, at 60 times 28.5, Mars at a day a second 28.5); paused, 0 GL draws and 0 frame requests; scrolled away or in a hidden tab, 0 GL draws at 60 times; reduced motion 1.00 frames a second at 60 times, and a jump instead of a glide; the glide from the ISS to the Moon takes 1.4 s
25
+ and then rests; a satellite too old for the date gives `chikyu-ride-lost`; two more rounds of six switches add no buffers, textures or vertex arrays; a lost context comes back with the ride. The freeze test has a fourth look, the space view riding the ISS (and the look-changing rounds ride the ISS and the Moon). The ten-minute soak rides every
26
+ way (the ISS, Tiangong, a Starlink satellite, the Moon, Mars, from it and behind it, down and at the horizon) while it adds, removes and chooses satellites: 965 steps, JS heap 14.35 to 14.51 MB after garbage collection, GPU objects 123 to 123, one WebGL context, no error.
27
+ - **The Auto-turn of the controls no longer draws thirty times a second in the space view.** Its turn at the Earth's own rate moves nothing visible in seconds, but every frame of it asked the space view for a draw. `setView(view, final = true)` now keeps a turn that moves less than half a pixel for the next draw and does not draw for it unless `final` is true:
28
+ 8 seconds of Auto-turn now make 0 draws (the controls ask for 480 animation frames in that time). This holds for the Earth's views, for riding, and for satellites.
29
+ - **Demo**: a "Ride along" row in the space look (ISS, Tiangong, a Starlink satellite, Moon, Mars, Stop; View: From it or Behind it; Looking: Straight down or At the horizon), with a line saying what is ridden, in English and Japanese (the Japanese page is tested for English words left in a control). The choices are kept in the address (`ride`, `rview`, `rlook`).
30
+ The embed takes `ride=iss|css|starlink|Moon|Mars`, `rview=follow` and `rlook=horizon`; its test: one context, the ISS's height, nothing drawn at rest, a Moon ride at a day a second capped.
31
+ - **Docs**: a ride-along guide in English and Japanese, a page for it (docs/ride-along.md) and every new export with TSDoc and an example, usage examples run by `pnpm test:usage` (a Vite page that rides the ISS and is opened in a browser; `ridePose` in Node), README section, screenshots. The space view has no markers (those belong to the flat and the realistic globes), so the ticket's "markers stay on the ground" has nothing to do here.
32
+ - The names beside the bodies are not drawn for the body being ridden (the camera is inside it) and the Earth's is not drawn when riding at a body.
33
+
3
34
  ## 0.8.0
4
35
 
5
36
  Satellites on the space view, the space view in the embed, and the demo's space controls in Japanese.
package/README.md CHANGED
@@ -149,6 +149,14 @@ const elements: ElementSet[] = await (await fetch("/my-celestrak-copy.json")).js
149
149
  document.querySelector("chikyu-space")!.setSatellites("stations", createSatellites(elements), { orbits: true });
150
150
  ```
151
151
 
152
+ ## Riding along
153
+
154
+ <p align="center"><img alt="Riding along with the ISS, tipped towards the horizon: the Earth's curve and atmosphere, and the orbits of the other stations in the sky" src="docs/images/space-ride-horizon.png" width="420"></p>
155
+
156
+ `space.ride({ group: "stations", index: 0 }, { look: "horizon" })` puts the camera on the ISS (or a Starlink satellite, the Moon, a planet) at its real place for the moment shown and looks at the Earth, which goes by beneath
157
+ as the body moves along its orbit; switch to another body and the camera glides there. Straight down or toward the horizon, from the body or following it at a distance, time faster or slower. It animates continuously, so it keeps to the
158
+ same budget: at most 30 frames a second (one a second for reduced motion), nothing drawn while paused, out of sight or hidden. [Ride along guide](docs/guides/ride-along.md), [docs/ride-along.md](docs/ride-along.md).
159
+
152
160
  ## Markers
153
161
 
154
162
  Mark any place at `[lon, lat]` with a label, colour or icon, in groups you can show or hide, on either look. Chikyu ships no places; yours move with the globe, fade on the far side, make room
@@ -185,7 +193,7 @@ document.getElementById("controls")!.addEventListener("kazoku-viewchange", (even
185
193
  });
186
194
  ```
187
195
 
188
- The mapping is `rotate([lambda0 + yaw, phi0 - tilt])`, `scale(baseScale * zoom)` and
196
+ The mapping is `rotate([lambda0 + yaw, phi0 + tilt])`, `scale(baseScale * zoom)` and
189
197
  `translate([cx + panX, cy + panY])`, where `lambda0` and `phi0` are the home centre. Front returns to that home
190
198
  view. Auto-turn's `0.004178` degrees a second is 360° per sidereal day; kazoku-ui leaves it still for a reader who
191
199
  prefers reduced motion, and a drag or zoom never stops or resets it.
@@ -2,9 +2,10 @@ import type { GeoPoint } from "../types.ts";
2
2
  import { setThreeLoader } from "../realistic/shared.ts";
3
3
  import type { SatelliteInfo, SatelliteRef, Satellites } from "../satellites/types.ts";
4
4
  import { type SatelliteStyle } from "./satellite-layer.ts";
5
+ import type { RideLook } from "./ride.ts";
5
6
  import { type ScaleMode } from "./scale.ts";
6
- import { BODY_NAMES, type BodyName, type BodyPainter } from "./scene.ts";
7
- export { BODY_NAMES, type BodyName };
7
+ import { BODY_NAMES, type BodyName, type BodyPainter, type RideTarget } from "./scene.ts";
8
+ export { BODY_NAMES, type BodyName, type RideTarget };
8
9
  /**
9
10
  * Change how the Sun and the planets are drawn (before the first element connects): the space view keeps their places, sizes, light, names and orbits and asks
10
11
  * the painter only for what to draw at each place (see `BodyPainter`). Left alone, they are plain spheres in the right colours.
@@ -63,6 +64,42 @@ export type SpaceState = "loading" | "live" | "unavailable" | "lost" | "idle";
63
64
  * ```
64
65
  */
65
66
  export type SpacePreset = "earth" | "moon" | "sun" | "inner" | "solar-system";
67
+ /**
68
+ * How a ride along is done: `view` `from` (the default) puts the camera at the body looking at the Earth, `follow` keeps it a fixed distance from the body looking at it (`distanceKm`, 3000 by default,
69
+ * for a satellite; a planet or the Moon is kept at the distance the zoom says); `look` `down` (the default) or `horizon` is where a camera at the body points; `smooth` (the default) glides
70
+ * from the old camera to the new one in about a second and a bit, and jumps instead for a reader who asked for reduced motion.
71
+ *
72
+ * @example
73
+ * ```ts no-run
74
+ * import type { RideOptions } from "@johnmorrisdotca/chikyu-earth/space/element";
75
+ *
76
+ * const options: RideOptions = { view: "from", look: "horizon" };
77
+ * console.log(options.look);
78
+ * ```
79
+ */
80
+ export interface RideOptions {
81
+ view?: "from" | "follow";
82
+ look?: RideLook;
83
+ distanceKm?: number;
84
+ smooth?: boolean;
85
+ }
86
+ /**
87
+ * What the camera is riding along with now: the body or satellite, how (`from` or `follow`), where it looks, and the distance kept for a satellite.
88
+ *
89
+ * @example
90
+ * ```ts no-run
91
+ * import type { Riding } from "@johnmorrisdotca/chikyu-earth/space/element";
92
+ *
93
+ * const describe = (ride: Riding | null): string => (ride ? `${ride.view} ${typeof ride.target === "string" ? ride.target : ride.target.group}` : "not riding");
94
+ * console.log(typeof describe);
95
+ * ```
96
+ */
97
+ export interface Riding {
98
+ target: RideTarget;
99
+ view: "from" | "follow";
100
+ look: RideLook;
101
+ distanceKm: number;
102
+ }
66
103
  type SatelliteStats = {
67
104
  groups: number;
68
105
  placed: number;
@@ -108,15 +145,33 @@ export declare class ChikyuSpace extends HTMLElement {
108
145
  set labels(value: boolean);
109
146
  get orbits(): boolean;
110
147
  set orbits(value: boolean);
111
- /** The shared controls' camera: yaw and tilt turn the view, zoom brings the camera in (1 shows the Earth, less shows the sky round it). Pan is not used. */
148
+ /**
149
+ * The shared controls' camera: yaw and tilt turn the view (the Earth's surface moves right for a drag right and up for a drag up), zoom brings the camera in (1 shows the Earth, less shows the sky round it). Pan is not used. A turn that moves nothing on the screen by half a pixel is kept
150
+ * for the next draw but does not cause one unless `final` is true (the default), so an Auto-turn at the Earth's own rate costs a draw every half minute, not thirty a second.
151
+ *
152
+ * @param view - The controls' yaw and tilt in degrees and zoom.
153
+ * @param final - False for the frames of a turn that is still going on (Auto-turn, a drag); true when it has settled.
154
+ */
112
155
  setView(view: {
113
156
  yaw: number;
114
157
  tilt: number;
115
158
  zoom: number;
116
- }): void;
159
+ }, final?: boolean): void;
117
160
  /** The body the camera looks at: at zoom 1 it fills the view; the yaw and tilt turn the camera round it. */
118
161
  get body(): BodyName;
119
162
  set body(name: BodyName);
163
+ /**
164
+ * Ride along with a body or a satellite. By default the camera sits at it and looks at the Earth, with the way it is going at the top of the picture, at its real place for the moment shown, so the
165
+ * Earth turns and slides beneath as it moves along its orbit (an ISS view goes round about every 90 minutes). A drag looks about from there (a drag right turns the camera to its left and a drag up tips it towards the bottom of its picture, so the ground follows the pointer), the shared controls' zoom narrows the view.
166
+ * A satellite is `{ group, index }` of a group set with `setSatellites`; it is placed exactly, one SGP4 call a frame, at any speed. Another call switches smoothly; `null` stops. The draw rate follows how
167
+ * fast the picture moves, never more than 30 a second (one a second for a reader who asked for reduced motion), and nothing is drawn out of sight.
168
+ *
169
+ * @param target - A body name (`"Moon"`, `"Mars"`, ...) or a satellite `{ group, index }`, or `null` to stop.
170
+ * @param options - `view`, `look`, `distanceKm`, `smooth` (see `RideOptions`).
171
+ */
172
+ ride(target: RideTarget | null, options?: RideOptions): void;
173
+ /** What the camera is riding along with now, or null. */
174
+ get riding(): Riding | null;
120
175
  /**
121
176
  * Show a group of satellites (made by `createSatellites`) under a name, or take the group away with `null`. A group is one draw call however many satellites it holds. Giving the name of a group
122
177
  * that is already shown replaces it. `style`: `color` and `size` (CSS pixels) of the dots, and `orbits` to draw every orbit (only for a group of 40 or fewer, such as the stations).
@@ -170,6 +225,16 @@ export declare class ChikyuSpace extends HTMLElement {
170
225
  time: number;
171
226
  lookups: number;
172
227
  satellites: SatelliteStats;
228
+ ride: {
229
+ riding: boolean;
230
+ lost: boolean;
231
+ gliding: boolean;
232
+ };
233
+ /** The camera as the last draw left it: its place in Earth radii from the Earth's centre, and its field of view in degrees. */
234
+ camera: {
235
+ position: [number, number, number];
236
+ fov: number;
237
+ };
173
238
  };
174
239
  }
175
240
  declare global {
@@ -78,6 +78,8 @@ export class ChikyuSpace extends HTMLElement {
78
78
  #pool = new Map();
79
79
  #places;
80
80
  #chosenLabel;
81
+ #focusBeforeRide = "Earth";
82
+ #lostTold = false;
81
83
  #names = {};
82
84
  #hint = "choose to look at it";
83
85
  #tiers = {};
@@ -120,6 +122,8 @@ export class ChikyuSpace extends HTMLElement {
120
122
  },
121
123
  onLabels: (places) => this.#placeLabels(places),
122
124
  onSelected: (place) => this.#placeChosen(place),
125
+ onMoving: () => this.#gate?.request(),
126
+ onRideLost: () => this.#rideLost(),
123
127
  onSatellitesError: (error) => this.dispatchEvent(new CustomEvent("chikyu-satellites-unavailable", { detail: { reason: error instanceof Error ? error.message : String(error) } })),
124
128
  ...(painter ? { painter } : {}),
125
129
  });
@@ -219,12 +223,24 @@ export class ChikyuSpace extends HTMLElement {
219
223
  this.#model.orbits = value;
220
224
  this.#gate?.request();
221
225
  }
222
- /** The shared controls' camera: yaw and tilt turn the view, zoom brings the camera in (1 shows the Earth, less shows the sky round it). Pan is not used. */
223
- setView(view) {
226
+ /**
227
+ * The shared controls' camera: yaw and tilt turn the view (the Earth's surface moves right for a drag right and up for a drag up), zoom brings the camera in (1 shows the Earth, less shows the sky round it). Pan is not used. A turn that moves nothing on the screen by half a pixel is kept
228
+ * for the next draw but does not cause one unless `final` is true (the default), so an Auto-turn at the Earth's own rate costs a draw every half minute, not thirty a second.
229
+ *
230
+ * @param view - The controls' yaw and tilt in degrees and zoom.
231
+ * @param final - False for the frames of a turn that is still going on (Auto-turn, a drag); true when it has settled.
232
+ */
233
+ setView(view, final = true) {
224
234
  const now = this.#model.view;
225
235
  if (view.yaw === now.yaw && view.tilt === now.tilt && view.zoom === now.zoom)
226
236
  return;
227
237
  this.#model.view = { yaw: view.yaw, tilt: view.tilt, zoom: view.zoom };
238
+ const drawn = this.#model.drawnView;
239
+ if (drawn && !final && view.zoom === drawn.zoom) {
240
+ const turned = Math.max(Math.abs(view.yaw - drawn.yaw), Math.abs(view.tilt - drawn.tilt)) * RAD;
241
+ if (turned * (this.#model.heightPx / 2) < 0.5)
242
+ return;
243
+ }
228
244
  this.#gate?.request();
229
245
  }
230
246
  /** The body the camera looks at: at zoom 1 it fills the view; the yaw and tilt turn the camera round it. */
@@ -237,6 +253,57 @@ export class ChikyuSpace extends HTMLElement {
237
253
  this.#model.focus = name;
238
254
  this.#gate?.request();
239
255
  }
256
+ // --- riding along -------------------------------------------------------------------------------------------------------------------------------
257
+ /**
258
+ * Ride along with a body or a satellite. By default the camera sits at it and looks at the Earth, with the way it is going at the top of the picture, at its real place for the moment shown, so the
259
+ * Earth turns and slides beneath as it moves along its orbit (an ISS view goes round about every 90 minutes). A drag looks about from there (a drag right turns the camera to its left and a drag up tips it towards the bottom of its picture, so the ground follows the pointer), the shared controls' zoom narrows the view.
260
+ * A satellite is `{ group, index }` of a group set with `setSatellites`; it is placed exactly, one SGP4 call a frame, at any speed. Another call switches smoothly; `null` stops. The draw rate follows how
261
+ * fast the picture moves, never more than 30 a second (one a second for a reader who asked for reduced motion), and nothing is drawn out of sight.
262
+ *
263
+ * @param target - A body name (`"Moon"`, `"Mars"`, ...) or a satellite `{ group, index }`, or `null` to stop.
264
+ * @param options - `view`, `look`, `distanceKm`, `smooth` (see `RideOptions`).
265
+ */
266
+ ride(target, options = {}) {
267
+ const model = this.#model;
268
+ const reduced = typeof matchMedia === "function" && matchMedia("(prefers-reduced-motion: reduce)").matches;
269
+ const wasRiding = model.ride !== null;
270
+ if (target === null) {
271
+ model.ride = null;
272
+ model.focus = this.#focusBeforeRide;
273
+ }
274
+ else {
275
+ if (typeof target === "string" && !BODY_NAMES.includes(target))
276
+ return;
277
+ if (typeof target !== "string" && !model.satellites.groups.get(target.group))
278
+ return;
279
+ if (!wasRiding)
280
+ this.#focusBeforeRide = model.focus;
281
+ const view = typeof target === "string" && target === "Earth" ? "follow" : (options.view ?? "from");
282
+ model.ride = { target: typeof target === "string" ? target : { group: target.group, index: target.index }, view, look: options.look ?? "down", distanceKm: Math.max(100, options.distanceKm ?? 3000) };
283
+ if (typeof target === "string" && view === "follow")
284
+ model.focus = target;
285
+ else if (wasRiding)
286
+ model.focus = this.#focusBeforeRide;
287
+ }
288
+ this.#lostTold = false;
289
+ const pose = model.pose;
290
+ 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;
291
+ this.#last = performance.now();
292
+ this.#schedule();
293
+ this.#gate?.request();
294
+ this.dispatchEvent(new CustomEvent("chikyu-ride", { detail: this.riding }));
295
+ }
296
+ /** What the camera is riding along with now, or null. */
297
+ get riding() {
298
+ const ride = this.#model.ride;
299
+ return ride ? { ...ride, target: typeof ride.target === "string" ? ride.target : { ...ride.target } } : null;
300
+ }
301
+ #rideLost() {
302
+ if (this.#lostTold)
303
+ return;
304
+ this.#lostTold = true;
305
+ this.dispatchEvent(new CustomEvent("chikyu-ride-lost", { detail: this.riding }));
306
+ }
240
307
  // --- satellites ---------------------------------------------------------------------------------------------------------------------------------
241
308
  /**
242
309
  * Show a group of satellites (made by `createSatellites`) under a name, or take the group away with `null`. A group is one draw call however many satellites it holds. Giving the name of a group
@@ -351,6 +418,8 @@ export class ChikyuSpace extends HTMLElement {
351
418
  time: this.#model.time,
352
419
  lookups: this.#model.lookups,
353
420
  satellites: this.#satelliteStats(),
421
+ ride: { riding: this.#model.ride !== null, lost: this.#model.rideLost, gliding: this.#model.transition !== null },
422
+ camera: { position: [this.#model.pose.p[0], this.#model.pose.p[1], this.#model.pose.p[2]], fov: this.#model.pose.fov },
354
423
  };
355
424
  }
356
425
  #satelliteStats() {
@@ -380,19 +449,20 @@ export class ChikyuSpace extends HTMLElement {
380
449
  return true;
381
450
  return false;
382
451
  }
383
- /** While time runs, draw as often as it shows: real time once a minute; faster time as often as the Earth's turn moves a pixel (1 to 30 times a second). Satellites go round in 90 minutes, so with a group on show real time draws once a second, and 1 a second is all a reader who asked for reduced motion gets. */
452
+ /** While time runs, draw as often as it shows: real time once a minute; faster time as often as the Earth's turn moves a pixel (1 to 30 times a second). Satellites go round in 90 minutes and a ride along moves the whole picture, so with either on show real time draws about as often as a pixel moves (once a second at least), and a reader who asked for reduced motion gets one step a second. */
384
453
  #schedule() {
385
454
  clearTimeout(this.#timer);
386
455
  this.#timer = undefined;
387
456
  if (!this.isConnected || this.#speed === 0)
388
457
  return;
389
458
  const satellites = this.#satellitesMoving();
459
+ const riding = this.#model.ride !== null;
390
460
  const radius = this.#model.drawn?.radiusPx ?? 200;
391
- const pixelsPerSecond = (satellites ? SATELLITE_RATE : EARTH_RATE) * RAD * Math.abs(this.#speed) * radius;
461
+ const pixelsPerSecond = Math.max((satellites ? SATELLITE_RATE : EARTH_RATE) * RAD * Math.abs(this.#speed) * radius, this.#model.rideMotion * Math.abs(this.#speed));
392
462
  let perSecond = Math.min(30, Math.max(1, pixelsPerSecond));
393
- if (satellites && matchMedia("(prefers-reduced-motion: reduce)").matches)
463
+ if ((satellites || riding) && matchMedia("(prefers-reduced-motion: reduce)").matches)
394
464
  perSecond = 1;
395
- const wait = !satellites && this.#speed === 1 ? REAL_TIME_TICK_MS : 1000 / perSecond;
465
+ const wait = !satellites && !riding && this.#speed === 1 ? REAL_TIME_TICK_MS : 1000 / perSecond;
396
466
  this.#timer = setTimeout(this.#tick, wait);
397
467
  }
398
468
  #tick = () => {
@@ -112,7 +112,7 @@ export declare function blend(a: Frame, b: Frame, t: number): Frame;
112
112
  * `stepMs` ahead, so playing time at a steady rate costs one pair of lookups every `stepMs` and every draw in between is arithmetic.
113
113
  *
114
114
  * @param initialStepMs - How far ahead the second answer looks, in milliseconds.
115
- * @returns A track: `at(ms)` for a frame, `lookups` for how many times the ephemeris was asked, `reset()` and `setStep(ms)`.
115
+ * @returns A track: `at(ms)` for a frame, `lookups` for how many times the ephemeris was asked, `ends()` for the two answers it is between (undefined before the first `at`), `reset()` and `setStep(ms)`.
116
116
  *
117
117
  * @example
118
118
  * ```ts
@@ -126,6 +126,8 @@ export declare function blend(a: Frame, b: Frame, t: number): Frame;
126
126
  */
127
127
  export declare function createTrack(initialStepMs?: number): {
128
128
  at(ms: number): Frame;
129
+ /** The two answers the track is between now (undefined before the first), for working out which way something is going. */
130
+ ends(): [Frame, Frame] | undefined;
129
131
  /** How many times astronomy-engine has been asked for a frame. */
130
132
  readonly lookups: number;
131
133
  reset(): void;
@@ -95,7 +95,7 @@ export function blend(a, b, t) {
95
95
  * `stepMs` ahead, so playing time at a steady rate costs one pair of lookups every `stepMs` and every draw in between is arithmetic.
96
96
  *
97
97
  * @param initialStepMs - How far ahead the second answer looks, in milliseconds.
98
- * @returns A track: `at(ms)` for a frame, `lookups` for how many times the ephemeris was asked, `reset()` and `setStep(ms)`.
98
+ * @returns A track: `at(ms)` for a frame, `lookups` for how many times the ephemeris was asked, `ends()` for the two answers it is between (undefined before the first `at`), `reset()` and `setStep(ms)`.
99
99
  *
100
100
  * @example
101
101
  * ```ts
@@ -121,6 +121,10 @@ export function createTrack(initialStepMs = 1000) {
121
121
  }
122
122
  return blend(a, b, (ms - a.ms) / (b.ms - a.ms));
123
123
  },
124
+ /** The two answers the track is between now (undefined before the first), for working out which way something is going. */
125
+ ends() {
126
+ return a && b ? [a, b] : undefined;
127
+ },
124
128
  /** How many times astronomy-engine has been asked for a frame. */
125
129
  get lookups() {
126
130
  return lookups;
@@ -1,3 +1,4 @@
1
1
  export { AU_IN_EARTH_RADII, blend, createTrack, EARTH_RADIUS_KM, frameAt, orbitPath, PLANETS, type Frame, type PlanetName, type Vec3 } from "./ephemeris.ts";
2
2
  export { compress, MOON_COMPRESSED, placeAround, placeMoon, placeSun, RADIUS, SUN_COMPRESSED, truePosition, type ScaleMode } from "./scale.ts";
3
3
  export { plainPainter, type BodyDrawing, type BodyPainter } from "./scene.ts";
4
+ export { RIDE_FOV, ridePose, type RideInput, type RideLook, type RidePose } from "./ride.ts";
@@ -3,3 +3,4 @@
3
3
  export { AU_IN_EARTH_RADII, blend, createTrack, EARTH_RADIUS_KM, frameAt, orbitPath, PLANETS } from "./ephemeris.js";
4
4
  export { compress, MOON_COMPRESSED, placeAround, placeMoon, placeSun, RADIUS, SUN_COMPRESSED, truePosition } from "./scale.js";
5
5
  export { plainPainter } from "./scene.js";
6
+ export { RIDE_FOV, ridePose } from "./ride.js";
@@ -0,0 +1,84 @@
1
+ import type { Vec3 } from "./ephemeris.ts";
2
+ /**
3
+ * The field of view, in degrees, of a camera riding along at zoom 1 (the Earth's view from outside is 35).
4
+ *
5
+ * @example
6
+ * ```ts
7
+ * import { RIDE_FOV } from "@johnmorrisdotca/chikyu-earth/space";
8
+ *
9
+ * console.log(RIDE_FOV);
10
+ * ```
11
+ */
12
+ export declare const RIDE_FOV = 60;
13
+ /**
14
+ * Where a riding camera looks: `down` straight at the Earth's centre (the ground goes by under the camera), or `horizon` tipped forward along the way it is going until the horizon
15
+ * is a little above the middle of the view.
16
+ *
17
+ * @example
18
+ * ```ts
19
+ * import type { RideLook } from "@johnmorrisdotca/chikyu-earth/space";
20
+ *
21
+ * const looks: RideLook[] = ["down", "horizon"];
22
+ * console.log(looks.join(" or "));
23
+ * ```
24
+ */
25
+ export type RideLook = "down" | "horizon";
26
+ /**
27
+ * What `ridePose` is asked: the camera's place and its way of travel (any length), where it looks, and the shared controls' yaw, tilt and zoom.
28
+ *
29
+ * @example
30
+ * ```ts
31
+ * import type { RideInput } from "@johnmorrisdotca/chikyu-earth/space";
32
+ *
33
+ * const input: RideInput = { position: [1.066, 0, 0], velocity: [0, 0, -7.66], look: "down", yaw: 0, tilt: 0, zoom: 1 };
34
+ * console.log(input.look);
35
+ * ```
36
+ */
37
+ export interface RideInput {
38
+ /** The camera's place in Earth radii from the Earth's centre, in the scene's axes. */
39
+ position: Vec3;
40
+ /** Its direction of travel, in any unit: only the direction is used. */
41
+ velocity: Vec3;
42
+ look: RideLook;
43
+ /** The shared controls' yaw in degrees (a drag across the view is 180): turns the camera to look along its picture's left, so the ground moves right. 0 has the way it is going at the top of the picture. */
44
+ yaw: number;
45
+ /** The shared controls' tilt in degrees (a drag down the view is 60): tips the camera to look towards the bottom of its picture, so the ground moves up. */
46
+ tilt: number;
47
+ /** 1 is the natural view; more narrows the field of view. */
48
+ zoom: number;
49
+ }
50
+ /**
51
+ * The camera `ridePose` works out: where it is, which way it looks, which way is up in the picture, its field of view in degrees, and how big the Earth looks from there (its angular radius in degrees).
52
+ *
53
+ * @example
54
+ * ```ts
55
+ * import type { RidePose } from "@johnmorrisdotca/chikyu-earth/space";
56
+ *
57
+ * const describe = (pose: RidePose): string => `looking ${pose.forward.map((n) => n.toFixed(2)).join(",")} with a ${pose.fov} degree view`;
58
+ * console.log(typeof describe);
59
+ * ```
60
+ */
61
+ export interface RidePose {
62
+ position: Vec3;
63
+ forward: Vec3;
64
+ up: Vec3;
65
+ fov: number;
66
+ earthRadiusDegrees: number;
67
+ }
68
+ /**
69
+ * The camera of a body-borne view: at `position`, looking at the Earth, with the way the body is going at the top of the picture. `look: "horizon"` tips it forward until the
70
+ * horizon is a little above the middle. A drag looks about from there, as a head on a pivot does: `yaw` turns the camera to its left and `tilt` tips it towards the bottom of its picture, so the
71
+ * ground follows the pointer (right for a drag right, up for a drag up); the angle for each pixel of the drag is the field of view's, so the pace is the same at any zoom. `zoom` narrows the field of view.
72
+ *
73
+ * @param input - The camera's place, its way of travel, the look, and the shared controls' yaw, tilt and zoom.
74
+ * @returns The camera's place, forward and up directions (unit vectors), its field of view, and the Earth's angular radius from there.
75
+ *
76
+ * @example
77
+ * ```ts
78
+ * import { ridePose } from "@johnmorrisdotca/chikyu-earth/space";
79
+ *
80
+ * const pose = ridePose({ position: [1.066, 0, 0], velocity: [0, 0, -7.66], look: "down", yaw: 0, tilt: 0, zoom: 1 });
81
+ * console.log(pose.forward.map((n) => n.toFixed(2)).join(","), pose.earthRadiusDegrees.toFixed(0));
82
+ * ```
83
+ */
84
+ export declare function ridePose(input: RideInput): RidePose;
@@ -0,0 +1,63 @@
1
+ const RAD = Math.PI / 180;
2
+ /** How far the ground moves in the picture for each pixel of a drag at the middle of it: the same pace as the line globe and the Earth from space (about one and a half). */
3
+ const GRAB = 1.5;
4
+ /**
5
+ * The field of view, in degrees, of a camera riding along at zoom 1 (the Earth's view from outside is 35).
6
+ *
7
+ * @example
8
+ * ```ts
9
+ * import { RIDE_FOV } from "@johnmorrisdotca/chikyu-earth/space";
10
+ *
11
+ * console.log(RIDE_FOV);
12
+ * ```
13
+ */
14
+ export const RIDE_FOV = 60;
15
+ const dot = (a, b) => a[0] * b[0] + a[1] * b[1] + a[2] * b[2];
16
+ const cross = (a, b) => [a[1] * b[2] - a[2] * b[1], a[2] * b[0] - a[0] * b[2], a[0] * b[1] - a[1] * b[0]];
17
+ const scale = (a, k) => [a[0] * k, a[1] * k, a[2] * k];
18
+ const add = (a, b) => [a[0] + b[0], a[1] + b[1], a[2] + b[2]];
19
+ const norm = (a) => Math.hypot(a[0], a[1], a[2]);
20
+ const unit = (a) => {
21
+ const n = norm(a);
22
+ return n === 0 ? [0, 0, 0] : [a[0] / n, a[1] / n, a[2] / n];
23
+ };
24
+ /**
25
+ * The camera of a body-borne view: at `position`, looking at the Earth, with the way the body is going at the top of the picture. `look: "horizon"` tips it forward until the
26
+ * horizon is a little above the middle. A drag looks about from there, as a head on a pivot does: `yaw` turns the camera to its left and `tilt` tips it towards the bottom of its picture, so the
27
+ * ground follows the pointer (right for a drag right, up for a drag up); the angle for each pixel of the drag is the field of view's, so the pace is the same at any zoom. `zoom` narrows the field of view.
28
+ *
29
+ * @param input - The camera's place, its way of travel, the look, and the shared controls' yaw, tilt and zoom.
30
+ * @returns The camera's place, forward and up directions (unit vectors), its field of view, and the Earth's angular radius from there.
31
+ *
32
+ * @example
33
+ * ```ts
34
+ * import { ridePose } from "@johnmorrisdotca/chikyu-earth/space";
35
+ *
36
+ * const pose = ridePose({ position: [1.066, 0, 0], velocity: [0, 0, -7.66], look: "down", yaw: 0, tilt: 0, zoom: 1 });
37
+ * console.log(pose.forward.map((n) => n.toFixed(2)).join(","), pose.earthRadiusDegrees.toFixed(0));
38
+ * ```
39
+ */
40
+ export function ridePose(input) {
41
+ const distance = Math.max(1e-9, norm(input.position));
42
+ const nadir = scale(input.position, -1 / distance);
43
+ let ahead = add(input.velocity, scale(nadir, -dot(input.velocity, nadir)));
44
+ if (norm(ahead) < 1e-12 * Math.max(1, norm(input.velocity))) {
45
+ // no way of travel to go by (or it is straight at the Earth): north, or east where north is straight down
46
+ const north = [0, 1, 0];
47
+ ahead = add(north, scale(nadir, -dot(north, nadir)));
48
+ if (norm(ahead) < 1e-9)
49
+ ahead = cross([1, 0, 0], nadir);
50
+ }
51
+ ahead = unit(ahead);
52
+ const earthRadiusDegrees = distance <= 1 ? 90 : Math.asin(1 / distance) / RAD;
53
+ const base = input.look === "horizon" ? Math.max(0, earthRadiusDegrees - 12) * RAD : 0;
54
+ const forward0 = add(scale(nadir, Math.cos(base)), scale(ahead, Math.sin(base)));
55
+ const up0 = add(scale(ahead, Math.cos(base)), scale(nadir, -Math.sin(base)));
56
+ const fov = Math.min(100, Math.max(5, RIDE_FOV / Math.max(0.05, input.zoom)));
57
+ const turn = input.yaw * (GRAB * fov / 180) * RAD;
58
+ const lift = input.tilt * (GRAB * fov / 60) * RAD;
59
+ const turned = add(scale(forward0, Math.cos(turn)), scale(cross(up0, forward0), Math.sin(turn)));
60
+ const forward = add(scale(turned, Math.cos(lift)), scale(up0, -Math.sin(lift)));
61
+ const up = add(scale(up0, Math.cos(lift)), scale(turned, Math.sin(lift)));
62
+ return { position: [input.position[0], input.position[1], input.position[2]], forward, up, fov, earthRadiusDegrees };
63
+ }
@@ -5,6 +5,7 @@ import type { Layer } from "../realistic/tiers.ts";
5
5
  import { type Frame, type PlanetName, type Vec3 } from "./ephemeris.ts";
6
6
  import { type ScaleMode } from "./scale.ts";
7
7
  import type { SatelliteRef } from "../satellites/types.ts";
8
+ import { type RideLook } from "./ride.ts";
8
9
  import { type SatelliteState } from "./satellite-layer.ts";
9
10
  type Three = ThreeParts;
10
11
  /** The camera's distance, in Earth radii, at which the Earth fills the view at zoom 1. */
@@ -32,6 +33,33 @@ export declare const BODY_NAMES: readonly ["Earth", "Sun", "Moon", "Mercury", "V
32
33
  * ```
33
34
  */
34
35
  export type BodyName = (typeof BODY_NAMES)[number];
36
+ /**
37
+ * What a camera can ride along with: the name of one of the bodies, or a satellite of a group (`{ group, index }`).
38
+ *
39
+ * @example
40
+ * ```ts no-run
41
+ * import type { RideTarget } from "@johnmorrisdotca/chikyu-earth/space/element";
42
+ *
43
+ * const targets: RideTarget[] = ["Moon", { group: "stations", index: 0 }];
44
+ * console.log(targets.length);
45
+ * ```
46
+ */
47
+ export type RideTarget = BodyName | SatelliteRef;
48
+ export interface RideState {
49
+ target: RideTarget;
50
+ /** `from`: the camera is at the body, looking at the Earth. `follow`: the camera keeps a fixed distance from it, looking at it. */
51
+ view: "from" | "follow";
52
+ look: RideLook;
53
+ /** For `follow` a satellite: how far the camera keeps, in kilometres. */
54
+ distanceKm: number;
55
+ }
56
+ /** The camera as the last draw left it, for the next one to start from when the ride changes. */
57
+ export interface Pose {
58
+ p: Vec3;
59
+ q: [number, number, number, number];
60
+ fov: number;
61
+ valid: boolean;
62
+ }
35
63
  export interface SpaceModel {
36
64
  /** Moment shown, milliseconds since the epoch. */
37
65
  time: number;
@@ -67,6 +95,30 @@ export interface SpaceModel {
67
95
  speed: number;
68
96
  /** The satellites shown, the one chosen, and what the last draw did with them. */
69
97
  satellites: SatelliteState;
98
+ /** The body or satellite the camera rides along with, or null. */
99
+ ride: RideState | null;
100
+ /** 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. */
101
+ rideLost: boolean;
102
+ /** A camera move in progress from where the last draw left it: when it began (ms of the page's clock), how long it takes. */
103
+ transition: {
104
+ start: number;
105
+ duration: number;
106
+ from: {
107
+ p: Vec3;
108
+ q: [number, number, number, number];
109
+ fov: number;
110
+ };
111
+ } | null;
112
+ /** The shared controls' view as the last draw used it, and how tall the view was (CSS pixels): `setView` skips a draw for a turn that moves nothing by half a pixel. */
113
+ drawnView: {
114
+ yaw: number;
115
+ tilt: number;
116
+ zoom: number;
117
+ } | null;
118
+ heightPx: number;
119
+ pose: Pose;
120
+ /** Pixels a second that riding makes things move on the screen at 1x clock speed (0 when not riding): the draw rate follows it. */
121
+ rideMotion: number;
70
122
  /** The satellite at a point of the view (CSS pixels), within a radius, if a draw has been made and one is there. */
71
123
  pick: ((x: number, y: number, radius: number) => SatelliteRef | null) | null;
72
124
  ratio(): number;
@@ -126,6 +178,10 @@ export interface SpaceDeps {
126
178
  x: number;
127
179
  y: number;
128
180
  } | null) => void;
181
+ /** 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). */
182
+ onMoving?: () => void;
183
+ /** The target of the ride could not be placed at this draw. */
184
+ onRideLost?: () => void;
129
185
  /** The satellites could not be drawn (the three.js parts handed over lack `Points`). */
130
186
  onSatellitesError?: (error: unknown) => void;
131
187
  /** How the Sun and the planets are drawn; left out, plain spheres in the right colours (see `BodyPainter`). */
@@ -3,6 +3,7 @@ import { ATMOSPHERE_FRAG, ATMOSPHERE_VERT, BODY_FRAG, CLOUD_FRAG, GLOW_FRAG, GLO
3
3
  import { createTextureSet } from "../realistic/textures.js";
4
4
  import { createTrack, orbitPath, PLANETS } from "./ephemeris.js";
5
5
  import { placeAround, placeMoon, placeSun, RADIUS } from "./scale.js";
6
+ import { ridePose } from "./ride.js";
6
7
  import { createSatelliteLayer, newSatelliteState } from "./satellite-layer.js";
7
8
  const RAD = Math.PI / 180;
8
9
  /** The camera's distance, in Earth radii, at which the Earth fills the view at zoom 1. */
@@ -36,6 +37,13 @@ export const newSpaceModel = () => ({
36
37
  drawn: null,
37
38
  lookups: 0,
38
39
  speed: 1,
40
+ ride: null,
41
+ rideLost: false,
42
+ transition: null,
43
+ drawnView: null,
44
+ heightPx: 420,
45
+ pose: { p: [0, 0, 0], q: [0, 0, 0, 1], fov: FOV, valid: false },
46
+ rideMotion: 0,
39
47
  satellites: newSatelliteState(),
40
48
  pick: null,
41
49
  ratio: () => Math.min(globalThis.devicePixelRatio || 1, 2),
@@ -167,10 +175,20 @@ export function createSpaceItem(model, deps) {
167
175
  planets.set(name, { drawing, orbit, positions, samples: [], epoch: Number.NaN });
168
176
  }
169
177
  let layer;
178
+ const rideState = new Float64Array(6);
170
179
  let selectedShown = false;
171
180
  const camera = new three.PerspectiveCamera(FOV, width / Math.max(1, height), 0.05, 1000);
172
181
  const target = new three.Vector3();
182
+ const desiredQ = camera.quaternion.clone();
183
+ const fromQ = camera.quaternion.clone();
173
184
  const projected = new three.Vector3();
185
+ const placeAll = (at) => {
186
+ const sunAt = placeSun(model.mode, at.sun);
187
+ const all = { Earth: [0, 0, 0], Moon: placeMoon(model.mode, at.moon), Sun: sunAt };
188
+ for (const name of PLANETS)
189
+ all[name] = placeAround(model.mode, sunAt, [at.planets[name][0] - at.sun[0], at.planets[name][1] - at.sun[1], at.planets[name][2] - at.sun[2]]);
190
+ return all;
191
+ };
174
192
  const labelFor = (v) => {
175
193
  projected.set(v[0], v[1], v[2]).project(camera);
176
194
  if (projected.z > 1 || projected.z < -1)
@@ -246,26 +264,114 @@ export function createSpaceItem(model, deps) {
246
264
  }
247
265
  layer?.update(model.time, model.speed);
248
266
  model.pick = layer ? (x, y, radius) => layer.pick(x, y, camera, size, radius) : null;
249
- // the camera: looks at the focus body from the direction the sky was in when it was last moved, turned by yaw and tilt, as far as the zoom says
250
- const focus = model.focus;
251
- const focusRadius = radiusOf(focus);
252
- const spin = model.cameraSpin ?? frame.spin;
253
- const lon = model.centre.lon - model.view.yaw;
254
- const lat = Math.max(-90, Math.min(90, model.centre.lat + model.view.tilt));
255
- const aim = direction(spin + lon, lat);
256
- const distance = Math.max(1.05 * focusRadius, (FIT_DISTANCE * focusRadius) / Math.max(1e-9, model.view.zoom));
257
- target.set(pos[focus][0], pos[focus][1], pos[focus][2]);
258
- camera.position.set(target.x + aim[0] * distance, target.y + aim[1] * distance, target.z + aim[2] * distance);
259
- camera.up.set(0, 1, 0);
260
- camera.lookAt(target);
261
- camera.near = Math.max(0.0005, distance * 0.02 - focusRadius * 0.5, focusRadius * 0.01);
262
- camera.far = Math.max(2000, distance * 4 + (mode === "true" ? 40 * 23_481 : 400));
267
+ // the camera: looks at the focus body from the direction the sky was in when it was last moved, turned by yaw and tilt, as far as the zoom says; or, riding along, sits at the
268
+ // body and looks at the Earth
269
+ let focus = model.focus;
270
+ let focusPos = pos[focus];
271
+ let focusRadius = radiusOf(focus);
272
+ let rideFrom = null;
273
+ model.rideLost = false;
274
+ model.rideMotion = 0;
275
+ const ride = model.ride;
276
+ if (ride) {
277
+ if (typeof ride.target === "string") {
278
+ if (ride.view === "follow") {
279
+ focus = ride.target;
280
+ focusPos = pos[focus];
281
+ focusRadius = radiusOf(focus);
282
+ }
283
+ else {
284
+ const ends = track.ends();
285
+ const was = ends ? placeAll(ends[0])[ride.target] : pos[ride.target];
286
+ const will = ends ? placeAll(ends[1])[ride.target] : pos[ride.target];
287
+ const seconds = ends ? Math.max(1e-3, (ends[1].ms - ends[0].ms) / 1000) : 1;
288
+ rideFrom = { position: pos[ride.target], velocity: [will[0] - was[0], will[1] - was[1], will[2] - was[2]], hidden: ride.target, gameSeconds: seconds };
289
+ }
290
+ }
291
+ else if (layer?.exact(ride.target, model.time, rideState)) {
292
+ const place = [rideState[0], rideState[1], rideState[2]];
293
+ if (ride.view === "follow") {
294
+ focusPos = place;
295
+ focusRadius = Math.max(1e-4, ride.distanceKm / 6371 / FIT_DISTANCE);
296
+ }
297
+ else
298
+ rideFrom = { position: place, velocity: [rideState[3], rideState[4], rideState[5]], hidden: null, gameSeconds: 0 };
299
+ }
300
+ else
301
+ model.rideLost = true;
302
+ }
303
+ const hidden = rideFrom?.hidden ?? null;
304
+ moon.visible = hidden !== "Moon";
305
+ sunDrawing.object.visible = hidden !== "Sun";
306
+ for (const [name, p] of planets)
307
+ p.drawing.object.visible = hidden !== name;
308
+ let fovNow = FOV;
309
+ let distance = 0;
310
+ if (rideFrom) {
311
+ const pose = ridePose({ position: rideFrom.position, velocity: rideFrom.velocity, look: ride.look, yaw: model.view.yaw, tilt: model.view.tilt, zoom: model.view.zoom });
312
+ camera.position.set(pose.position[0], pose.position[1], pose.position[2]);
313
+ camera.up.set(pose.up[0], pose.up[1], pose.up[2]);
314
+ camera.lookAt(pose.position[0] + pose.forward[0], pose.position[1] + pose.forward[1], pose.position[2] + pose.forward[2]);
315
+ fovNow = pose.fov;
316
+ const away = Math.hypot(...rideFrom.position);
317
+ camera.near = Math.max(0.0008, Math.min(0.05, (away - 1) * 0.1));
318
+ camera.far = Math.max(2000, 400 + (mode === "true" ? 40 * 23_481 : 0));
319
+ // how fast the ground goes by under the camera, in pixels a second at 1x: the speed over the height for a satellite, the turn of the direction to the Earth for a body
320
+ const focal = (size.height / 2) / Math.tan((fovNow / 2) * RAD);
321
+ const speed = Math.hypot(...rideFrom.velocity);
322
+ model.rideMotion = typeof ride.target === "string" ? ((speed / Math.max(1e-9, away)) / rideFrom.gameSeconds) * focal : (speed / Math.max(50, (away - 1) * 6371)) * focal;
323
+ }
324
+ else {
325
+ const spin = model.cameraSpin ?? frame.spin;
326
+ // behind a satellite the Earth is the far side of what the camera looks at, so it moves with the camera's side-step; the view is mirrored for the ground to follow the pointer
327
+ const way = ride && ride.view === "follow" && typeof ride.target !== "string" ? -1 : 1;
328
+ const lon = model.centre.lon - way * model.view.yaw;
329
+ const lat = Math.max(-90, Math.min(90, model.centre.lat - way * model.view.tilt));
330
+ const aim = direction(spin + lon, lat);
331
+ distance = Math.max(1.05 * focusRadius, (FIT_DISTANCE * focusRadius) / Math.max(1e-9, model.view.zoom));
332
+ target.set(focusPos[0], focusPos[1], focusPos[2]);
333
+ camera.position.set(target.x + aim[0] * distance, target.y + aim[1] * distance, target.z + aim[2] * distance);
334
+ camera.up.set(0, 1, 0);
335
+ camera.lookAt(target);
336
+ camera.near = Math.max(0.0005, distance * 0.02 - focusRadius * 0.5, focusRadius * 0.01);
337
+ camera.far = Math.max(2000, distance * 4 + (mode === "true" ? 40 * 23_481 : 400));
338
+ if (ride && ride.view === "follow" && typeof ride.target !== "string") {
339
+ const focal = (size.height / 2) / Math.tan((fovNow / 2) * RAD);
340
+ model.rideMotion = (Math.hypot(rideState[3], rideState[4], rideState[5]) / Math.max(50, ride.distanceKm / Math.max(1e-9, model.view.zoom))) * focal;
341
+ }
342
+ }
343
+ // a ride that changed: glide from where the last draw left the camera (a jump for a reader who asked for reduced motion; the element does not start one then)
344
+ const glide = model.transition;
345
+ if (glide) {
346
+ const k = Math.min(1, (performance.now() - glide.start) / glide.duration);
347
+ const ease = k * k * (3 - 2 * k);
348
+ desiredQ.copy(camera.quaternion);
349
+ camera.position.set(glide.from.p[0] + (camera.position.x - glide.from.p[0]) * ease, glide.from.p[1] + (camera.position.y - glide.from.p[1]) * ease, glide.from.p[2] + (camera.position.z - glide.from.p[2]) * ease);
350
+ fromQ.set(glide.from.q[0], glide.from.q[1], glide.from.q[2], glide.from.q[3]);
351
+ camera.quaternion.copy(fromQ.slerp(desiredQ, ease));
352
+ fovNow = glide.from.fov + (fovNow - glide.from.fov) * ease;
353
+ if (k >= 1)
354
+ model.transition = null;
355
+ }
356
+ camera.fov = fovNow;
263
357
  camera.aspect = size.width / Math.max(1, size.height);
264
358
  camera.updateProjectionMatrix();
265
359
  camera.updateMatrixWorld();
266
360
  sunView.value.copy(sunEarth.value).transformDirection(camera.matrixWorldInverse);
361
+ const pose = model.pose;
362
+ pose.p[0] = camera.position.x;
363
+ pose.p[1] = camera.position.y;
364
+ pose.p[2] = camera.position.z;
365
+ pose.q[0] = camera.quaternion.x;
366
+ pose.q[1] = camera.quaternion.y;
367
+ pose.q[2] = camera.quaternion.z;
368
+ pose.q[3] = camera.quaternion.w;
369
+ pose.fov = fovNow;
370
+ pose.valid = true;
371
+ model.drawnView = { yaw: model.view.yaw, tilt: model.view.tilt, zoom: model.view.zoom };
372
+ model.heightPx = size.height;
267
373
  // how many pixels across each body is: for the pictures the Earth and the Moon need, and for what is chosen
268
- const half = Math.tan((FOV / 2) * RAD);
374
+ const half = Math.tan((fovNow / 2) * RAD);
269
375
  const radiusPxOf = (name) => {
270
376
  const d = Math.hypot(pos[name][0] - camera.position.x, pos[name][1] - camera.position.y, pos[name][2] - camera.position.z);
271
377
  const r = radiusOf(name);
@@ -281,7 +387,7 @@ export function createSpaceItem(model, deps) {
281
387
  if (deps.onLabels && model.labels) {
282
388
  const places = {};
283
389
  for (const name of BODY_NAMES) {
284
- const at = labelFor(pos[name]);
390
+ const at = name === hidden || (rideFrom && name === "Earth") ? null : labelFor(pos[name]);
285
391
  places[name] = at ? { x: at.x, y: at.y, r: px[name] } : null;
286
392
  }
287
393
  deps.onLabels(places);
@@ -292,6 +398,10 @@ export function createSpaceItem(model, deps) {
292
398
  deps.onSelected(at);
293
399
  selectedShown = at !== null;
294
400
  }
401
+ if (model.rideLost)
402
+ deps.onRideLost?.();
403
+ if (model.transition)
404
+ deps.onMoving?.();
295
405
  return false;
296
406
  },
297
407
  resize(w, h) {
package/dist/view.d.ts CHANGED
@@ -41,7 +41,7 @@ export interface ProjectionView {
41
41
  */
42
42
  export declare const globeBaseScale: (size: number) => number;
43
43
  /**
44
- * Where the view is centred on the globe: the home centre, turned by yaw (west) and tipped by tilt (north), held to the poles.
44
+ * Where the view is centred on the globe: the home centre, turned by yaw (west) and tipped by tilt (south), held to the poles; a drag right is yaw and a drag up is tilt, and the ground follows the pointer.
45
45
  *
46
46
  * @param centre - The home centre.
47
47
  * @param view - The turn, tip, zoom and pan from the view controls.
package/dist/view.js CHANGED
@@ -24,7 +24,7 @@ export const HOME_VIEW = Object.freeze({ yaw: 0, tilt: 0, zoom: 1, panX: 0, panY
24
24
  */
25
25
  export const globeBaseScale = (size) => size / 2 - 1;
26
26
  /**
27
- * Where the view is centred on the globe: the home centre, turned by yaw (west) and tipped by tilt (north), held to the poles.
27
+ * Where the view is centred on the globe: the home centre, turned by yaw (west) and tipped by tilt (south), held to the poles; a drag right is yaw and a drag up is tilt, and the ground follows the pointer.
28
28
  *
29
29
  * @param centre - The home centre.
30
30
  * @param view - The turn, tip, zoom and pan from the view controls.
@@ -39,7 +39,7 @@ export const globeBaseScale = (size) => size / 2 - 1;
39
39
  */
40
40
  export const lookAt = (centre, view) => ({
41
41
  lon: centre.lon - view.yaw,
42
- lat: Math.max(-90, Math.min(90, centre.lat + view.tilt)),
42
+ lat: Math.max(-90, Math.min(90, centre.lat - view.tilt)),
43
43
  });
44
44
  /**
45
45
  * What the projection is given for a view: yaw turns longitude, tilt tips latitude, zoom scales and pan moves the disc in CSS pixels.
@@ -58,7 +58,7 @@ export const lookAt = (centre, view) => ({
58
58
  */
59
59
  export function projectionView(centre, size, view = HOME_VIEW) {
60
60
  return {
61
- rotation: [-centre.lon + view.yaw, -centre.lat - view.tilt],
61
+ rotation: [-centre.lon + view.yaw, -centre.lat + view.tilt],
62
62
  scale: globeBaseScale(size) * view.zoom,
63
63
  translate: [size / 2 + view.panX, size / 2 + view.panY],
64
64
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@johnmorrisdotca/chikyu-earth",
3
- "version": "0.8.0",
3
+ "version": "0.9.1",
4
4
  "description": "Chikyu Earth 地球 — A line-drawn globe for the web: country outlines that turn at the Earth's own rate, centred on where you are, 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",