@johnmorrisdotca/chikyu-earth 0.55.0 → 0.57.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.
@@ -20,7 +20,8 @@ import { COUNTRY_NAMES_ATTRIBUTES } from "../country-names/geometry.js";
20
20
  import { COUNTRY_NAMES_STYLE, createCountryHost } from "../country-names/host.js";
21
21
  import { SEA_NAMES_ATTRIBUTES, readSeaNames } from "../sea-names/geometry.js";
22
22
  import { SEA_NAMES_STYLE, createSeaHost } from "../sea-names/host.js";
23
- import { createGate, frameCap } from "../realistic/gate.js";
23
+ import { createGate, frameCap, smallDevice } from "../realistic/gate.js";
24
+ import { framesPerSecond, readSmoothFps, smoothCap, SMOOTH_UP_TO, waitFor, watchLowBattery } from "../realistic/pace.js";
24
25
  import { createFinerHolder, GIBS_ID } from "../door/gibs.js";
25
26
  import { folderSource } from "../door/map.js";
26
27
  import { setThreeLoader, takeRenderer } from "../realistic/shared.js";
@@ -38,7 +39,7 @@ import { styleTable } from "./paths.js";
38
39
  import { bindSatelliteHover, hoverPathOn, hoverStyleOf, namesOnHover as namesHoverOn } from "./satellite-hover.js";
39
40
  import { clampFollow, retargetFollow } from "./follow.js";
40
41
  import { COMPASS_HTML, COMPASS_STYLE, createCompass, readCompass } from "./compass.js";
41
- import { RIDE_FACES, rideTiltLimits, rideTurnDegrees } from "./ride.js";
42
+ import { OUTSIDE_KM, RIDE_FACES, rideTiltLimits, rideTurnDegrees } from "./ride.js";
42
43
  import { placeAround, placeMoon, placeSun, RADIUS } from "./scale.js";
43
44
  import { BODY_NAMES, createSpaceItem, DEFAULT_EARTH_SIZE, newSpaceModel } from "./scene.js";
44
45
  import { createPaintState, paintedGround, paintedPeriod } from "../paint/buffer.js";
@@ -114,6 +115,22 @@ function readFrame(value, min, max) {
114
115
  const number = Number(value);
115
116
  return Number.isFinite(number) ? Math.min(max, Math.max(min, number)) : null;
116
117
  }
118
+ /** The distance an outside ride keeps, in kilometres, from an option or an attribute: a positive number, else `null`. */
119
+ function readOutsideKm(value) {
120
+ const number = typeof value === "string" ? (value.trim() === "" ? NaN : Number(value)) : value;
121
+ return typeof number === "number" && Number.isFinite(number) && number > 0 ? number : null;
122
+ }
123
+ /** The length of the glide to a ride: a number of milliseconds as given (0 to 20,000), or a length that follows the distance (the option's `minMs` and `maxMs`), with the length the glide has until the first draw knows the distance. */
124
+ function readGlide(value) {
125
+ if (typeof value === "number" && Number.isFinite(value))
126
+ return { duration: Math.min(20_000, Math.max(0, value)) };
127
+ if (value && typeof value === "object") {
128
+ const minMs = typeof value.minMs === "number" && Number.isFinite(value.minMs) ? Math.min(20_000, Math.max(0, value.minMs)) : 800;
129
+ const maxMs = typeof value.maxMs === "number" && Number.isFinite(value.maxMs) ? Math.min(20_000, Math.max(minMs, value.maxMs)) : Math.max(minMs, 4000);
130
+ return { duration: minMs, scaled: { minMs, maxMs } };
131
+ }
132
+ return { duration: 1200 };
133
+ }
117
134
  /** An option's number held between `min` and `max`, or `undefined` when it is not given. */
118
135
  function clampFrame(value, min, max) {
119
136
  return typeof value === "number" && Number.isFinite(value) ? Math.min(max, Math.max(min, value)) : undefined;
@@ -178,7 +195,7 @@ const STYLE = `:host{display:block;aspect-ratio:1/1;position:relative;overflow:h
178
195
  * ```
179
196
  */
180
197
  export class ChikyuSpace extends HTMLElement {
181
- static observedAttributes = ["cloud-drift", "camera", "earth-size", "earth-x", "earth-y", "center", "time", "speed", "scale", "labels", "orbits", "bodies", "light", "meteors", "meteors-label", "meteors-strength", ...BODY_LABEL_ATTRIBUTES, ...SKY_ATTRIBUTES, ...CITY_NAMES_ATTRIBUTES, ...BORDER_ATTRIBUTES, ...GRID_ATTRIBUTES, ...BEAM_ATTRIBUTES, ...COUNTRY_NAMES_ATTRIBUTES, ...SEA_NAMES_ATTRIBUTES, "air-thickness", "air-label", "lang", "places", "places-look", "place-groups", "here-city", "route", "bodies-credit", "satellite-tint", "satellite-streak-max", "tiles", "tiles-finer", "tiles-finer-layer", "tiles-finer-date", "tiles-finer-timeout", "ride-viewpoint", "ride-model-size", "ride-drag", "hover-path", "names-on-hover", "selected-path", "path-light", "path-hit", "path-priority", "estimate-far", "follow", "follow-ease", "dim-occluded", "ride-model-share", "ride-model-seat", "ride-zoom", "ride-up", "compass"];
198
+ static observedAttributes = ["cloud-drift", "camera", "earth-size", "earth-x", "earth-y", "center", "time", "speed", "scale", "labels", "orbits", "bodies", "light", "meteors", "meteors-label", "meteors-strength", ...BODY_LABEL_ATTRIBUTES, ...SKY_ATTRIBUTES, ...CITY_NAMES_ATTRIBUTES, ...BORDER_ATTRIBUTES, ...GRID_ATTRIBUTES, ...BEAM_ATTRIBUTES, ...COUNTRY_NAMES_ATTRIBUTES, ...SEA_NAMES_ATTRIBUTES, "air-thickness", "air-label", "lang", "places", "places-look", "place-groups", "here-city", "route", "bodies-credit", "satellite-tint", "satellite-streak-max", "tiles", "tiles-finer", "tiles-finer-layer", "tiles-finer-date", "tiles-finer-timeout", "ride-viewpoint", "ride-outside-km", "ride-model-size", "ride-drag", "hover-path", "names-on-hover", "selected-path", "path-light", "path-hit", "path-priority", "estimate-far", "follow", "follow-ease", "dim-occluded", "ride-model-share", "ride-model-seat", "ride-zoom", "ride-up", "compass", "smooth-fps"];
182
199
  #model = newSpaceModel();
183
200
  /** The finer imagery (NASA's tile service), one source while the attributes stay the same. Off unless `tiles-finer="gibs"`. */
184
201
  #finerHold = createFinerHolder(() => new Date(this.#model.time), () => this.#gate?.request());
@@ -215,6 +232,10 @@ export class ChikyuSpace extends HTMLElement {
215
232
  #toldTrack = "";
216
233
  #bodiesNote;
217
234
  #gate;
235
+ /** Whether the battery is low (at 20% or less and not charging): the smooth frame rate is halved. */
236
+ #lowBattery = false;
237
+ /** What `#schedule` last worked out, for `stats`. */
238
+ #pace = { pixelsPerSecond: 0, perSecond: 0, cap: 0, waitMs: 0 };
218
239
  #taken;
219
240
  #stage;
220
241
  #labelBox;
@@ -344,6 +365,11 @@ export class ChikyuSpace extends HTMLElement {
344
365
  this.#pool.clear();
345
366
  const nav = navigator;
346
367
  this.#gate = createGate(1000 / frameCap(30, { deviceMemory: nav.deviceMemory, hardwareConcurrency: nav.hardwareConcurrency }), () => this.#view?.invalidate());
368
+ this.#lowBattery = false;
369
+ watchLowBattery(nav, () => {
370
+ this.#lowBattery = true;
371
+ this.#schedule();
372
+ });
347
373
  this.#stage.addEventListener("webglcontextlost", this.#lost, true);
348
374
  this.#stage.addEventListener("webglcontextrestored", this.#restored, true);
349
375
  const taken = takeRenderer((state) => {
@@ -478,6 +504,8 @@ export class ChikyuSpace extends HTMLElement {
478
504
  this.#applyViewpoint();
479
505
  else if (name === "ride-model-size")
480
506
  this.#applyModelSize();
507
+ else if (name === "ride-outside-km")
508
+ this.#applyOutsideKm();
481
509
  else if (name === "ride-drag")
482
510
  this.#applyDrag();
483
511
  else if (name === "ride-up")
@@ -603,6 +631,8 @@ export class ChikyuSpace extends HTMLElement {
603
631
  this.#model.satellites.streakMax = readStreakMax(value);
604
632
  this.#gate?.request();
605
633
  }
634
+ else if (name === "smooth-fps")
635
+ this.#schedule();
606
636
  else if (name === "tiles" || name === "tiles-finer" || name === "tiles-finer-layer" || name === "tiles-finer-date" || name === "tiles-finer-timeout")
607
637
  this.#gate?.request();
608
638
  if (name === "air-thickness" || name === "air-label" || name === "lang")
@@ -1459,7 +1489,7 @@ export class ChikyuSpace extends HTMLElement {
1459
1489
  model.rideTurn += rideTurnDegrees(wrapYaw(view.yaw - now.yaw), view.zoom);
1460
1490
  // and the tilt is held to what does something in the look: past it a drag would have to come back before anything moved (the controls driving the view are handed the held tilt, the bar's own when one is bound)
1461
1491
  let tilt = view.tilt;
1462
- const limits = model.ride && model.ride.view === "from" && model.rideDistance !== null ? rideTiltLimits(model.rideDistance, model.ride.look, view.zoom) : null;
1492
+ const limits = model.ride && model.ride.view === "from" && model.rideDistance !== null ? rideTiltLimits(model.rideDistance, model.ride.look, view.zoom, model.ride.face) : null;
1463
1493
  if (limits)
1464
1494
  tilt = Math.max(limits.min, Math.min(limits.max, tilt));
1465
1495
  this.#model.view = { yaw: view.yaw, tilt, zoom: view.zoom };
@@ -1536,7 +1566,7 @@ export class ChikyuSpace extends HTMLElement {
1536
1566
  // a body followed (a planet, the Moon, the Earth) is looked at from round it as the view turns: it has no ways to face
1537
1567
  const faceable = !(view === "follow" && typeof target === "string");
1538
1568
  const face = faceable && options.face && RIDE_FACES.includes(options.face) ? options.face : null;
1539
- model.ride = { target: typeof target === "string" ? target : { group: target.group, index: target.index }, view, look: face && view === "from" ? "horizon" : (options.look ?? "down"), face, distanceKm: Math.max(100, options.distanceKm ?? 3000), viewpoint: options.viewpoint === "inside" || options.viewpoint === "outside" ? options.viewpoint : this.rideViewpoint, modelSize: options.modelSize === "fixed" || options.modelSize === "distance" ? options.modelSize : this.rideModelSize, drag: options.drag === "head" || options.drag === "orbit" ? options.drag : this.rideDrag, up: options.up === "north" || options.up === "travel" ? options.up : this.rideUp };
1569
+ model.ride = { target: typeof target === "string" ? target : { group: target.group, index: target.index }, view, look: face && view === "from" ? "horizon" : (options.look ?? "down"), face, distanceKm: Math.max(100, options.distanceKm ?? 3000), viewpoint: options.viewpoint === "inside" || options.viewpoint === "outside" ? options.viewpoint : this.rideViewpoint, outsideKm: readOutsideKm(options.outsideKm) ?? this.rideOutsideKm ?? OUTSIDE_KM, modelSize: options.modelSize === "fixed" || options.modelSize === "distance" ? options.modelSize : this.rideModelSize, drag: options.drag === "head" || options.drag === "orbit" ? options.drag : this.rideDrag, up: options.up === "north" || options.up === "travel" ? options.up : this.rideUp };
1540
1570
  if (face) {
1541
1571
  model.view = { yaw: 0, tilt: 0, zoom: model.view.zoom };
1542
1572
  model.rideTurn = 0;
@@ -1550,7 +1580,8 @@ export class ChikyuSpace extends HTMLElement {
1550
1580
  this.#zoomRange(model.ride?.view === "from");
1551
1581
  this.#lostTold = false;
1552
1582
  const pose = model.pose;
1553
- 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;
1583
+ const glide = readGlide(options.glide);
1584
+ model.transition = (options.smooth ?? true) && !reduced && pose.valid ? { start: performance.now(), duration: glide.duration, ...(glide.scaled ? { scaled: glide.scaled } : {}), 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;
1554
1585
  this.#last = performance.now();
1555
1586
  this.#schedule();
1556
1587
  this.#gate?.request();
@@ -1669,6 +1700,45 @@ export class ChikyuSpace extends HTMLElement {
1669
1700
  this.ride(next.ref, { view: "follow" });
1670
1701
  return next;
1671
1702
  }
1703
+ /**
1704
+ * How far back an outside ride keeps the camera from the satellite, in kilometres (the attribute `ride-outside-km`), or `null` for the package's own, `OUTSIDE_KM` (25). It is the default for the next `ride` (`RideOptions.outsideKm`) and, set while riding,
1705
+ * moves the camera there in a glide of 0.8 s (a jump for a reader who asked for less motion). The camera is never more than half the satellite's height back.
1706
+ *
1707
+ * @example
1708
+ * ```ts
1709
+ * import type { ChikyuSpace } from "@johnmorrisdotca/chikyu-earth/space/element";
1710
+ *
1711
+ * const space = document.createElement("chikyu-space") as ChikyuSpace;
1712
+ * space.rideOutsideKm = 100;
1713
+ * console.log(space.rideOutsideKm);
1714
+ * ```
1715
+ */
1716
+ get rideOutsideKm() {
1717
+ return readOutsideKm(this.getAttribute("ride-outside-km"));
1718
+ }
1719
+ set rideOutsideKm(value) {
1720
+ if (readOutsideKm(value) === null)
1721
+ this.removeAttribute("ride-outside-km");
1722
+ else
1723
+ this.setAttribute("ride-outside-km", String(value));
1724
+ }
1725
+ #applyOutsideKm() {
1726
+ const model = this.#model;
1727
+ const ride = model.ride;
1728
+ const next = this.rideOutsideKm ?? OUTSIDE_KM;
1729
+ if (!ride || ride.outsideKm === next)
1730
+ return;
1731
+ const reduced = typeof matchMedia === "function" && matchMedia("(prefers-reduced-motion: reduce)").matches;
1732
+ const pose = model.pose;
1733
+ model.ride = { ...ride, outsideKm: next };
1734
+ // only an outside ride from the satellite has a camera that far back: the others have nothing to glide
1735
+ const moves = ride.view === "from" && ride.viewpoint === "outside" && typeof ride.target !== "string";
1736
+ model.transition = moves && !reduced && pose.valid ? { start: performance.now(), duration: 800, 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;
1737
+ this.#last = performance.now();
1738
+ this.#schedule();
1739
+ this.#gate?.request();
1740
+ this.dispatchEvent(new CustomEvent("chikyu-ride", { detail: this.riding }));
1741
+ }
1672
1742
  /**
1673
1743
  * Where a ride puts the camera: `outside` (the default; the attribute `ride-viewpoint`) shows the satellite, `inside` puts the camera at it. It is the default for the next `ride` and, set while riding, moves the ride there in one eased glide (a jump for a reader who asked for reduced motion), keeping the ride and the clock.
1674
1744
  * See `RideOptions.viewpoint`.
@@ -1963,6 +2033,27 @@ export class ChikyuSpace extends HTMLElement {
1963
2033
  else
1964
2034
  this.removeAttribute("satellite-streak-max");
1965
2035
  }
2036
+ /**
2037
+ * The most frames a second drawn for a clock at real time, or up to 60 times it (the attribute `smooth-fps`; 15 when the page says nothing; 1 to 30). The picture is drawn each time the quickest thing in it (the Earth's turn at this zoom, a satellite dot,
2038
+ * the ridden craft and the camera in a ride) has moved half a pixel, at least once a second while anything but the Earth is moving, and at most this many. Half as many on a small device or a battery at 20% or lower; one a second for a reader who asked for reduced motion.
2039
+ *
2040
+ * @example
2041
+ * ```ts
2042
+ * import "@johnmorrisdotca/chikyu-earth/space/element";
2043
+ *
2044
+ * const space = document.querySelector("chikyu-space")!;
2045
+ * space.smoothFps = 24;
2046
+ * ```
2047
+ */
2048
+ get smoothFps() {
2049
+ return readSmoothFps(this.getAttribute("smooth-fps"));
2050
+ }
2051
+ set smoothFps(value) {
2052
+ if (Number.isFinite(value) && value > 0)
2053
+ this.setAttribute("smooth-fps", String(value));
2054
+ else
2055
+ this.removeAttribute("smooth-fps");
2056
+ }
1966
2057
  /**
1967
2058
  * 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
1968
2059
  * 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).
@@ -2453,6 +2544,7 @@ export class ChikyuSpace extends HTMLElement {
2453
2544
  time: this.#model.time,
2454
2545
  lookups: this.#model.lookups,
2455
2546
  satellites: this.#satelliteStats(),
2547
+ pace: { ...this.#pace },
2456
2548
  lightFade: { mix: this.#model.lightFade.mix, fading: this.#model.lightFade.fading },
2457
2549
  paths: this.#pathControls?.stats() ?? emptyPathStats(),
2458
2550
  ride: { track: this.#model.rideTrack ? { ...this.#model.rideTrack } : null, compass: this.#compass?.shown ? { ...this.#compass.shown } : null, riding: this.#model.ride !== null, lost: this.#model.rideLost, gliding: this.#model.transition !== null || this.#model.rideFrame.moving, facing: this.facing, groundKm: this.#model.rideFrame.groundKm },
@@ -2502,7 +2594,19 @@ export class ChikyuSpace extends HTMLElement {
2502
2594
  return true;
2503
2595
  return false;
2504
2596
  }
2505
- /** 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. */
2597
+ /** The quickest a satellite on show goes, in Earth radii for each second of game time: the quickest the groups report between their two SGP4 moments, or, before they have any (and above the interpolation limit, where they do not), a low orbit's. */
2598
+ #satelliteRate() {
2599
+ let most = 0;
2600
+ for (const [name, group] of this.#model.satellites.groups)
2601
+ if (satelliteEmphasis(name, group.visible, this.#model.satellites.focus).drawn)
2602
+ most = Math.max(most, group.satellites.fastest);
2603
+ return most > 0 && Math.abs(this.#speed) <= SMOOTH_UP_TO ? most : SATELLITE_RATE * RAD;
2604
+ }
2605
+ /**
2606
+ * While time runs, draw each time the quickest thing in the picture has moved half a pixel: the Earth's turn at this zoom, a satellite dot (the quickest of the groups on show, from the places they hold for the interpolation), the ridden craft and the camera in a ride.
2607
+ * At most `smooth-fps` a second (15), half as many on a small device or a low battery, and at least once a second while a satellite or a ride moves; real time with only the Earth turning waits up to a minute. A reader who asked for reduced motion gets one a second at most.
2608
+ * Faster than 60 times real time it is a frame for each pixel, up to 30 a second.
2609
+ */
2506
2610
  #schedule() {
2507
2611
  clearTimeout(this.#timer);
2508
2612
  this.#timer = undefined;
@@ -2510,12 +2614,16 @@ export class ChikyuSpace extends HTMLElement {
2510
2614
  return;
2511
2615
  const satellites = this.#satellitesMoving();
2512
2616
  const riding = this.#model.ride !== null;
2617
+ const speed = Math.abs(this.#speed);
2513
2618
  const radius = this.#model.drawn?.radiusPx ?? 200;
2514
- const pixelsPerSecond = Math.max((satellites ? SATELLITE_RATE : EARTH_RATE) * RAD * Math.abs(this.#speed) * radius, this.#model.rideMotion * Math.abs(this.#speed));
2515
- let perSecond = Math.min(30, Math.max(1, pixelsPerSecond));
2516
- if ((satellites || riding) && matchMedia("(prefers-reduced-motion: reduce)").matches)
2517
- perSecond = 1;
2518
- const wait = !satellites && !riding && this.#speed === 1 ? REAL_TIME_TICK_MS : 1000 / perSecond;
2619
+ const pixelsPerSecond = Math.max(EARTH_RATE * RAD * speed * radius, satellites ? this.#satelliteRate() * speed * radius : 0, this.#model.rideMotion * speed);
2620
+ const nav = navigator;
2621
+ const cap = smoothCap(this.smoothFps, this.#lowBattery || smallDevice({ deviceMemory: nav.deviceMemory, hardwareConcurrency: nav.hardwareConcurrency }));
2622
+ // a reader who asked for reduced motion gets one a second while satellites or a ride move or the clock is at real time; a clock the reader set running faster over a bare Earth is theirs to ask for (as before)
2623
+ const perSecond = framesPerSecond({ pixelsPerSecond, speed: this.#speed, cap, reduced: prefersLessMotion() && (satellites || riding || speed <= 1), always: satellites || riding });
2624
+ // a hidden tab is not drawn: it is looked at once a second, as the browser would have it anyway
2625
+ const wait = Math.max(waitFor(perSecond, REAL_TIME_TICK_MS), document.visibilityState === "hidden" ? 1000 : 0);
2626
+ this.#pace = { pixelsPerSecond, perSecond, cap, waitMs: wait };
2519
2627
  this.#timer = setTimeout(this.#tick, wait);
2520
2628
  }
2521
2629
  /** Bring the moment shown up to `now` (the page's own clock, in milliseconds) at the speed it has run at. */
@@ -1,7 +1,7 @@
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 { fitDistance, plainPainter, type BodyDrawing, type RideTrack, type BodyMoon, type BodyPainter, type BodyView, type PaintContext, type SatellitePainter } from "./scene.ts";
4
- export { faceOffset, OUTSIDE_KM, outsidePose, RIDE_FACES, RIDE_FOV, rideFov, ridePose, rideTiltLimits, rideTurnDegrees, type RideDrag, type RideFace, type RideInput, type RideLook, type RidePose, type RideModelSize, type RideViewpoint } from "./ride.ts";
4
+ export { faceOffset, OUTSIDE_KM, outsidePose, RIDE_FACES, RIDE_FOV, rideFov, rideGlideMs, ridePose, rideTiltLimits, rideTurnDegrees, type RideDrag, type RideFace, type RideInput, type RideLook, type RidePose, type RideModelSize, type RideViewpoint } from "./ride.ts";
5
5
  export { earthHides, type SatelliteScreen } from "./satellite-layer.ts";
6
6
  export { ABOVE_MODEL_KM, ABOVE_MODEL_SHARE, aboveAxes, aboveModelShare, MODEL_FADE_FROM_KM, MODEL_FADE_TO_KM, MODEL_HANDOVER_PX, MODEL_RADIUS_SHARE, modelMatrix, modelRadius, satelliteAxes } from "./satellite-model.ts";
7
7
  export { localNorth, NORTH_UP_BLEND_DEG, NORTH_UP_POLE_DEG, northUpYaw, screenAngle, type RideUp } from "./north-up.ts";
@@ -3,7 +3,7 @@
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 { fitDistance, plainPainter } from "./scene.js";
6
- export { faceOffset, OUTSIDE_KM, outsidePose, RIDE_FACES, RIDE_FOV, rideFov, ridePose, rideTiltLimits, rideTurnDegrees } from "./ride.js";
6
+ export { faceOffset, OUTSIDE_KM, outsidePose, RIDE_FACES, RIDE_FOV, rideFov, rideGlideMs, ridePose, rideTiltLimits, rideTurnDegrees } from "./ride.js";
7
7
  export { earthHides } from "./satellite-layer.js";
8
8
  export { ABOVE_MODEL_KM, ABOVE_MODEL_SHARE, aboveAxes, aboveModelShare, MODEL_FADE_FROM_KM, MODEL_FADE_TO_KM, MODEL_HANDOVER_PX, MODEL_RADIUS_SHARE, modelMatrix, modelRadius, satelliteAxes } from "./satellite-model.js";
9
9
  export { localNorth, NORTH_UP_BLEND_DEG, NORTH_UP_POLE_DEG, northUpYaw, screenAngle } from "./north-up.js";
@@ -95,11 +95,13 @@ export declare function rideFov(zoom: number): number;
95
95
  export declare function rideTurnDegrees(change: number, zoom: number): number;
96
96
  /**
97
97
  * The range of the controls' tilt that does something in the `horizon` look: from straight down to a little above the horizon. A tilt beyond it moves nothing, so a page holds the tilt to it, and a drag back starts moving at once instead of after the
98
- * excess is dragged back. It depends on the height (the horizon is further from straight down the higher the camera is) and the zoom. The `down` look has no such range.
98
+ * excess is dragged back. It depends on the height (the horizon is further from straight down the higher the camera is), the zoom and the facing: the range is the one the facing opens in (`ridePose` rests the camera there), so looking `up` the camera can tip from straight away
99
+ * from the Earth to straight down, and looking `down` from straight down to a little above the horizon. Leave the facing out for a ride with none (the horizon a little above the middle). The `down` look has no such range.
99
100
  *
100
101
  * @param distance - The camera's distance from the Earth's centre, in Earth radii.
101
102
  * @param look - The look.
102
103
  * @param zoom - The shared controls' zoom.
104
+ * @param face - The facing the ride opened with (`RideFace`), or leave it out or `null` for none.
103
105
  * @returns The lowest and highest tilt, or `null` for the `down` look.
104
106
  *
105
107
  * @example
@@ -107,10 +109,11 @@ export declare function rideTurnDegrees(change: number, zoom: number): number;
107
109
  * import { rideTiltLimits } from "@johnmorrisdotca/chikyu-earth/space";
108
110
  *
109
111
  * const range = rideTiltLimits(1.066, "horizon", 1);
110
- * console.log(range && `${range.min.toFixed(0)} to ${range.max.toFixed(0)}`, rideTiltLimits(1.066, "down", 1));
112
+ * const below = rideTiltLimits(1.066, "horizon", 1, "up");
113
+ * console.log(range && `${range.min.toFixed(0)} to ${range.max.toFixed(0)}`, below && `${below.min.toFixed(0)} to ${below.max.toFixed(0)}`, rideTiltLimits(1.066, "down", 1));
111
114
  * ```
112
115
  */
113
- export declare function rideTiltLimits(distance: number, look: RideLook, zoom: number): {
116
+ export declare function rideTiltLimits(distance: number, look: RideLook, zoom: number, face?: RideFace | null): {
114
117
  min: number;
115
118
  max: number;
116
119
  } | null;
@@ -200,7 +203,7 @@ export type RideDrag = "head" | "orbit";
200
203
  */
201
204
  export type RideModelSize = "distance" | "fixed";
202
205
  /**
203
- * How far an outside ride keeps the camera from the satellite, in kilometres. The satellite itself is a few metres across, so it is drawn far larger than life (a model about a fifth of the picture's height across): not to scale, and the page says so in its caption.
206
+ * How far an outside ride keeps the camera from the satellite by default, in kilometres (a ride's `outsideKm` and the element's `ride-outside-km` change it). The satellite itself is a few metres across, so it is drawn far larger than life (a model about a fifth of the picture's height across): not to scale, and the page says so in its caption.
204
207
  *
205
208
  * @example
206
209
  * ```ts
@@ -210,6 +213,23 @@ export type RideModelSize = "distance" | "fixed";
210
213
  * ```
211
214
  */
212
215
  export declare const OUTSIDE_KM = 25;
216
+ /**
217
+ * How long a glide between two cameras lasts, in milliseconds, when it follows the distance the camera has to move: the shortest at a hop of 10 km or less, the longest at 30,000 km or more (a camera that goes to the Moon takes the longest),
218
+ * and in between the length grows with each tenfold of the distance (3,000 km is about 70 per cent of the way). A fixed 1.2 seconds is quick for a hop of thousands of kilometres and slow for one of tens. Pure.
219
+ *
220
+ * @param km - How far the camera moves, in kilometres.
221
+ * @param minMs - The shortest glide, in milliseconds; 800 by default.
222
+ * @param maxMs - The longest glide, in milliseconds; 4,000 by default (never below `minMs`).
223
+ * @returns The length of the glide in milliseconds, between `minMs` and `maxMs`.
224
+ *
225
+ * @example
226
+ * ```ts
227
+ * import { rideGlideMs } from "@johnmorrisdotca/chikyu-earth/space";
228
+ *
229
+ * console.log(rideGlideMs(25), rideGlideMs(3000), rideGlideMs(400_000));
230
+ * ```
231
+ */
232
+ export declare function rideGlideMs(km: number, minMs?: number, maxMs?: number): number;
213
233
  /**
214
234
  * The camera of an outside ride: the pose `ridePose` works out for a camera at the satellite, moved to `OUTSIDE_KM` back along its own line of sight so the satellite is in front of it. Looking along the way it goes (`look: "horizon"`) the satellite sits a little below the middle of the picture, so the camera is behind and above it; looking
215
235
  * down (`look: "down"`) it is at the middle, with the camera straight above it. Where the satellite sits in the picture does not change with the zoom. Which way the camera looks and which way is up are the pose's own. With no `rest`, the camera goes round the satellite as it looks about (`orbit`):
@@ -220,6 +240,7 @@ export declare const OUTSIDE_KM = 25;
220
240
  * @param look - The look the pose was made with.
221
241
  * @param low - How far below the middle of the picture the satellite is, as a share of half its height (0 the middle, 1 the bottom edge); by default 0.8 looking along and 0 looking down.
222
242
  * @param rest - The pose from `ridePose` for the same camera with no drag (yaw, tilt and turn of 0): where the camera is kept. Leave it out for the camera to go round the satellite as it looks about.
243
+ * @param km - How far back the camera is kept, in kilometres: `OUTSIDE_KM` (25) by default. Never more than half the satellite's height.
223
244
  * @returns The pose moved back from the satellite, with how far the camera is from it in Earth radii and how many degrees below the middle of the picture the satellite is.
224
245
  *
225
246
  * @example
@@ -233,7 +254,7 @@ export declare const OUTSIDE_KM = 25;
233
254
  * console.log(out.distance.toFixed(4), out.below.toFixed(1), out.pose.position.map((n) => n.toFixed(4)).join(","), head.pose.position.map((n) => n.toFixed(4)).join(","));
234
255
  * ```
235
256
  */
236
- export declare function outsidePose(pose: RidePose, look: RideLook, low?: number, rest?: RidePose): {
257
+ export declare function outsidePose(pose: RidePose, look: RideLook, low?: number, rest?: RidePose, km?: number): {
237
258
  pose: RidePose;
238
259
  distance: number;
239
260
  below: number;
@@ -27,6 +27,14 @@ export const RIDE_FOV = 60;
27
27
  export const RIDE_FACES = ["ahead", "behind", "left", "right", "down", "up"];
28
28
  /** How far each facing turns from the way it goes, in degrees, to the left, in the view from the body. */
29
29
  const FACE_TURN = { ahead: 0, behind: 180, left: 90, right: -90, down: 0, up: 0 };
30
+ /** Where the horizon look rests, as an elevation in radians (0 the horizon, a quarter turn up or down at the ends): straight at the Earth for `down`, straight away from it for `up`, and for every other facing the horizon a little above the middle. */
31
+ function restElevation(earthRadiusDegrees, face) {
32
+ if (face === "down")
33
+ return -Math.PI / 2;
34
+ if (face === "up")
35
+ return Math.PI / 2;
36
+ return Math.max(0, earthRadiusDegrees - 12) * RAD - Math.PI / 2;
37
+ }
30
38
  /**
31
39
  * Yaw and tilt, in radians, that put a camera looking at a satellite on a facing. `down` is straight above it, looking down at it and the Earth. `ahead` looks along the way it goes.
32
40
  *
@@ -89,11 +97,13 @@ export function rideTurnDegrees(change, zoom) {
89
97
  }
90
98
  /**
91
99
  * The range of the controls' tilt that does something in the `horizon` look: from straight down to a little above the horizon. A tilt beyond it moves nothing, so a page holds the tilt to it, and a drag back starts moving at once instead of after the
92
- * excess is dragged back. It depends on the height (the horizon is further from straight down the higher the camera is) and the zoom. The `down` look has no such range.
100
+ * excess is dragged back. It depends on the height (the horizon is further from straight down the higher the camera is), the zoom and the facing: the range is the one the facing opens in (`ridePose` rests the camera there), so looking `up` the camera can tip from straight away
101
+ * from the Earth to straight down, and looking `down` from straight down to a little above the horizon. Leave the facing out for a ride with none (the horizon a little above the middle). The `down` look has no such range.
93
102
  *
94
103
  * @param distance - The camera's distance from the Earth's centre, in Earth radii.
95
104
  * @param look - The look.
96
105
  * @param zoom - The shared controls' zoom.
106
+ * @param face - The facing the ride opened with (`RideFace`), or leave it out or `null` for none.
97
107
  * @returns The lowest and highest tilt, or `null` for the `down` look.
98
108
  *
99
109
  * @example
@@ -101,16 +111,18 @@ export function rideTurnDegrees(change, zoom) {
101
111
  * import { rideTiltLimits } from "@johnmorrisdotca/chikyu-earth/space";
102
112
  *
103
113
  * const range = rideTiltLimits(1.066, "horizon", 1);
104
- * console.log(range && `${range.min.toFixed(0)} to ${range.max.toFixed(0)}`, rideTiltLimits(1.066, "down", 1));
114
+ * const below = rideTiltLimits(1.066, "horizon", 1, "up");
115
+ * console.log(range && `${range.min.toFixed(0)} to ${range.max.toFixed(0)}`, below && `${below.min.toFixed(0)} to ${below.max.toFixed(0)}`, rideTiltLimits(1.066, "down", 1));
105
116
  * ```
106
117
  */
107
- export function rideTiltLimits(distance, look, zoom) {
118
+ export function rideTiltLimits(distance, look, zoom, face) {
108
119
  if (look !== "horizon")
109
120
  return null;
110
121
  const radius = distance <= 1 ? 90 : Math.asin(1 / distance) / RAD;
111
- const start = Math.max(0, radius - 12) * RAD - Math.PI / 2;
122
+ const rest = restElevation(radius, face);
123
+ const cap = Math.max(MAX_ABOVE_HORIZON * RAD, rest);
112
124
  const perDegree = (GRAB * rideFov(zoom)) / 60;
113
- return { min: (start - (MAX_ABOVE_HORIZON * RAD)) / RAD / perDegree, max: (start + Math.PI / 2) / RAD / perDegree };
125
+ return { min: (rest - cap) / RAD / perDegree, max: (rest + Math.PI / 2) / RAD / perDegree };
114
126
  }
115
127
  const dot = (a, b) => a[0] * b[0] + a[1] * b[1] + a[2] * b[2];
116
128
  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]];
@@ -122,7 +134,7 @@ const unit = (a) => {
122
134
  return n === 0 ? [0, 0, 0] : [a[0] / n, a[1] / n, a[2] / n];
123
135
  };
124
136
  /**
125
- * How far an outside ride keeps the camera from the satellite, in kilometres. The satellite itself is a few metres across, so it is drawn far larger than life (a model about a fifth of the picture's height across): not to scale, and the page says so in its caption.
137
+ * How far an outside ride keeps the camera from the satellite by default, in kilometres (a ride's `outsideKm` and the element's `ride-outside-km` change it). The satellite itself is a few metres across, so it is drawn far larger than life (a model about a fifth of the picture's height across): not to scale, and the page says so in its caption.
126
138
  *
127
139
  * @example
128
140
  * ```ts
@@ -132,6 +144,28 @@ const unit = (a) => {
132
144
  * ```
133
145
  */
134
146
  export const OUTSIDE_KM = 25;
147
+ /**
148
+ * How long a glide between two cameras lasts, in milliseconds, when it follows the distance the camera has to move: the shortest at a hop of 10 km or less, the longest at 30,000 km or more (a camera that goes to the Moon takes the longest),
149
+ * and in between the length grows with each tenfold of the distance (3,000 km is about 70 per cent of the way). A fixed 1.2 seconds is quick for a hop of thousands of kilometres and slow for one of tens. Pure.
150
+ *
151
+ * @param km - How far the camera moves, in kilometres.
152
+ * @param minMs - The shortest glide, in milliseconds; 800 by default.
153
+ * @param maxMs - The longest glide, in milliseconds; 4,000 by default (never below `minMs`).
154
+ * @returns The length of the glide in milliseconds, between `minMs` and `maxMs`.
155
+ *
156
+ * @example
157
+ * ```ts
158
+ * import { rideGlideMs } from "@johnmorrisdotca/chikyu-earth/space";
159
+ *
160
+ * console.log(rideGlideMs(25), rideGlideMs(3000), rideGlideMs(400_000));
161
+ * ```
162
+ */
163
+ export function rideGlideMs(km, minMs = 800, maxMs = 4000) {
164
+ const low = Math.max(0, Number.isFinite(minMs) ? minMs : 800);
165
+ const high = Math.max(low, Number.isFinite(maxMs) ? maxMs : 4000);
166
+ const share = Number.isFinite(km) && km > 10 ? Math.min(1, Math.log10(km / 10) / 3.5) : 0;
167
+ return Math.round(low + (high - low) * share);
168
+ }
135
169
  /** How far below the middle of the picture the satellite's middle is drawn in an outside ride that looks along the way it goes, as a share of half the picture's height (0.8: at 90 per cent of the height, so with the model drawn 17 per cent of the height in radius it crosses the bottom edge). */
136
170
  const OUTSIDE_LOW = 0.8;
137
171
  /**
@@ -144,6 +178,7 @@ const OUTSIDE_LOW = 0.8;
144
178
  * @param look - The look the pose was made with.
145
179
  * @param low - How far below the middle of the picture the satellite is, as a share of half its height (0 the middle, 1 the bottom edge); by default 0.8 looking along and 0 looking down.
146
180
  * @param rest - The pose from `ridePose` for the same camera with no drag (yaw, tilt and turn of 0): where the camera is kept. Leave it out for the camera to go round the satellite as it looks about.
181
+ * @param km - How far back the camera is kept, in kilometres: `OUTSIDE_KM` (25) by default. Never more than half the satellite's height.
147
182
  * @returns The pose moved back from the satellite, with how far the camera is from it in Earth radii and how many degrees below the middle of the picture the satellite is.
148
183
  *
149
184
  * @example
@@ -157,10 +192,10 @@ const OUTSIDE_LOW = 0.8;
157
192
  * console.log(out.distance.toFixed(4), out.below.toFixed(1), out.pose.position.map((n) => n.toFixed(4)).join(","), head.pose.position.map((n) => n.toFixed(4)).join(","));
158
193
  * ```
159
194
  */
160
- export function outsidePose(pose, look, low, rest) {
195
+ export function outsidePose(pose, look, low, rest, km = OUTSIDE_KM) {
161
196
  const base = rest ?? pose;
162
197
  const height = Math.max(0, norm(pose.position) - 1);
163
- const distance = Math.max(1e-5, Math.min(OUTSIDE_KM / 6371, height * 0.5));
198
+ const distance = Math.max(1e-5, Math.min((Number.isFinite(km) && km > 0 ? km : OUTSIDE_KM) / 6371, height * 0.5));
164
199
  const lowNow = low ?? (look === "horizon" ? OUTSIDE_LOW : 0);
165
200
  const below = Math.atan(Math.max(0, lowNow) * Math.tan((base.fov / 2) * RAD));
166
201
  // the satellite is along the line of sight turned `below` down (towards the bottom of the picture): the camera is back along that line. The line is the pose's own, so the camera goes round the satellite as it looks about, or the line of the pose with no drag (`rest`), so the camera keeps its place and only turns
@@ -202,7 +237,7 @@ export function ridePose(input) {
202
237
  const fov = rideFov(input.zoom);
203
238
  const turn = ((input.turn ?? input.yaw * (GRAB * fov / 180)) + faced) * RAD;
204
239
  const lift = input.tilt * (GRAB * fov / 60) * RAD;
205
- const rest = input.face === "down" ? -Math.PI / 2 : input.face === "up" ? Math.PI / 2 : base - Math.PI / 2;
240
+ const rest = restElevation(earthRadiusDegrees, input.face);
206
241
  const cap = Math.max(MAX_ABOVE_HORIZON * RAD, rest);
207
242
  if (input.look === "horizon") {
208
243
  // looking round on the spot: a drag sideways turns the camera about the local vertical, all the way round, and a drag up or down tips it up or down in the vertical plane it faces, from straight down to a little above the horizon,
@@ -122,6 +122,8 @@ export interface RideState {
122
122
  distanceKm: number;
123
123
  /** `outside`: a ridden satellite is in the picture (the camera a little behind and above it in `from`, a drawing of it where the page has no model); `inside`: as a ride was before, the camera at the satellite and no drawing of it where the page has no model. */
124
124
  viewpoint: RideViewpoint;
125
+ /** How far back an outside ride keeps the camera from the satellite, in kilometres. */
126
+ outsideKm: number;
125
127
  /** How the model of a satellite seen from above is sized as the camera rises: `distance` shrinks it and hands over to the mark, `fixed` keeps one share of the picture. */
126
128
  modelSize: RideModelSize;
127
129
  /** What a drag does in an outside ride from the satellite: `head` turns the view where the camera is, `orbit` takes the camera round the satellite. */
@@ -240,6 +242,10 @@ export interface SpaceModel {
240
242
  transition: {
241
243
  start: number;
242
244
  duration: number;
245
+ scaled?: {
246
+ minMs: number;
247
+ maxMs: number;
248
+ };
243
249
  from: {
244
250
  p: Vec3;
245
251
  q: [number, number, number, number];
@@ -5,7 +5,7 @@ import { createTextureSet } from "../realistic/textures.js";
5
5
  import { createTileSet } from "../realistic/tile-set.js";
6
6
  import { createTrack, orbitPath, PLANETS } from "./ephemeris.js";
7
7
  import { placeAround, placeMoon, placeSun, RADIUS } from "./scale.js";
8
- import { faceOffset, outsidePose, ridePose } from "./ride.js";
8
+ import { faceOffset, outsidePose, rideGlideMs, ridePose } from "./ride.js";
9
9
  import { stripLights } from "./model-lights.js";
10
10
  import { newLightFade, stepLightFade } from "../realistic/light-fade.js";
11
11
  import { prefersLessMotion } from "../meteors/note.js";
@@ -661,7 +661,7 @@ export function createSpaceItem(model, deps) {
661
661
  if (ridden && ride.viewpoint === "outside") {
662
662
  // a drag turns the view where the camera is (`head`: the camera's place is the one it has with no drag), or takes the camera round the satellite (`orbit`)
663
663
  const rest = ride.drag === "orbit" ? undefined : ridePose({ position: rideFrom.position, velocity: rideFrom.velocity, look: ride.look, yaw: 0, tilt: 0, zoom: model.view.zoom, turn: 0, ...(ride.face ? { face: ride.face } : {}) });
664
- const out = outsidePose(pose, ride.look, framing ? (framing.seat - 0.5) / 0.5 : undefined, rest);
664
+ const out = outsidePose(pose, ride.look, framing ? (framing.seat - 0.5) / 0.5 : undefined, rest, ride.outsideKm);
665
665
  pose = out.pose;
666
666
  outsideAway = out.distance;
667
667
  }
@@ -730,6 +730,12 @@ export function createSpaceItem(model, deps) {
730
730
  // 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)
731
731
  const glide = model.transition;
732
732
  if (glide) {
733
+ // a glide that follows the distance takes its length from how far the camera has to go, at its first frame
734
+ if (glide.scaled) {
735
+ const km = Math.hypot(camera.position.x - glide.from.p[0], camera.position.y - glide.from.p[1], camera.position.z - glide.from.p[2]) * 6371;
736
+ glide.duration = rideGlideMs(km, glide.scaled.minMs, glide.scaled.maxMs);
737
+ delete glide.scaled;
738
+ }
733
739
  const k = Math.min(1, (performance.now() - glide.start) / glide.duration);
734
740
  const ease = k * k * (3 - 2 * k);
735
741
  desiredQ.copy(camera.quaternion);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@johnmorrisdotca/chikyu-earth",
3
- "version": "0.55.0",
3
+ "version": "0.57.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",