@johnmorrisdotca/chikyu-earth 0.23.0 → 0.25.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.
@@ -2,6 +2,7 @@ import { carryControls } from "@johnmorrisdotca/kazoku-ui/view-controls";
2
2
  import { adoptStyles } from "../adopt.js";
3
3
  import { createGate, frameCap } from "../realistic/gate.js";
4
4
  import { setThreeLoader, takeRenderer } from "../realistic/shared.js";
5
+ import { METEOR_NOTE_STYLE, prefersLessMotion, setMeteorNote } from "../meteors/note.js";
5
6
  import { MAX_TEXTURE } from "../realistic/tiers.js";
6
7
  import { MAX_SATELLITE_SPEED } from "../satellites/limits.js";
7
8
  import { AU_IN_EARTH_RADII, EARTH_RADIUS_KM, frameAt } from "./ephemeris.js";
@@ -61,7 +62,7 @@ const readTime = (value) => {
61
62
  const ms = Date.parse(value);
62
63
  return Number.isFinite(ms) ? ms : "now";
63
64
  };
64
- const STYLE = `:host{display:block;aspect-ratio:1/1;position:relative;overflow:hidden}#stage,#labels{position:absolute;inset:0}#labels{pointer-events:none;overflow:hidden}.sp-label{position:absolute;left:0;top:0;margin:0;padding:0;border:0;background:none;font:12px/16px system-ui,-apple-system,sans-serif;color:#fff;white-space:nowrap;text-shadow:0 0 3px #000,0 0 3px #000;will-change:transform;pointer-events:none}.sp-label:focus-visible{outline:2px solid #6cd0ff;outline-offset:2px;border-radius:4px}.sp-label::before{content:"";display:inline-block;width:5px;height:5px;margin:0 5px 1px -2px;border-radius:50%;background:#fff}.sp-label[hidden]{display:none}.sp-sat{position:absolute;left:0;top:0;font:12px/16px system-ui,-apple-system,sans-serif;color:#fff;white-space:nowrap;text-shadow:0 0 3px #000,0 0 3px #000;will-change:transform;pointer-events:none}.sp-sat[hidden]{display:none}`;
65
+ const STYLE = `:host{display:block;aspect-ratio:1/1;position:relative;overflow:hidden}#stage,#labels{position:absolute;inset:0}#labels{pointer-events:none;overflow:hidden}.sp-label{position:absolute;left:0;top:0;margin:0;padding:0;border:0;background:none;font:12px/16px system-ui,-apple-system,sans-serif;color:#fff;white-space:nowrap;text-shadow:0 0 3px #000,0 0 3px #000;will-change:transform;pointer-events:none}.sp-label:focus-visible{outline:2px solid #6cd0ff;outline-offset:2px;border-radius:4px}.sp-label::before{content:"";display:inline-block;width:5px;height:5px;margin:0 5px 1px -2px;border-radius:50%;background:#fff}.sp-label[hidden]{display:none}.sp-sat{position:absolute;left:0;top:0;font:12px/16px system-ui,-apple-system,sans-serif;color:#fff;white-space:nowrap;text-shadow:0 0 3px #000,0 0 3px #000;will-change:transform;pointer-events:none}.sp-sat[hidden]{display:none}${METEOR_NOTE_STYLE}`;
65
66
  /**
66
67
  * The `<chikyu-space>` custom element: the Earth, the Moon, the Sun and the planets where they are at a date and time, drawn with three.js through the shared renderer. It takes `center`, `time` (`now` or an ISO date), `speed` (1 is real time, 0 holds, 86400 is a day a second, negative runs backwards), `scale` (`compressed` or `true`), `labels` and `orbits` (`off` hides them); positions are computed in the browser by astronomy-engine, an optional peer dependency. It draws only when something changes, and nothing while it is out of sight.
67
68
  *
@@ -87,11 +88,14 @@ const STYLE = `:host{display:block;aspect-ratio:1/1;position:relative;overflow:h
87
88
  * ```
88
89
  */
89
90
  export class ChikyuSpace extends HTMLElement {
90
- static observedAttributes = ["center", "time", "speed", "scale", "labels", "orbits", "bodies", "light"];
91
+ static observedAttributes = ["center", "time", "speed", "scale", "labels", "orbits", "bodies", "light", "meteors", "meteors-label", "lang"];
91
92
  #model = newSpaceModel();
92
93
  #mode = "now";
93
94
  #speed = 1;
94
95
  #view;
96
+ /** The page has set a `speed` since the element came into the page: for a reader who prefers reduced motion, that is the Play that lets meteors start. */
97
+ #played = false;
98
+ #note;
95
99
  #gate;
96
100
  #taken;
97
101
  #stage;
@@ -131,10 +135,16 @@ export class ChikyuSpace extends HTMLElement {
131
135
  if (!this.hasAttribute("aria-label"))
132
136
  this.setAttribute("aria-label", "The Earth, the Moon, the Sun and the planets in their places for the date shown");
133
137
  const root = this.shadowRoot ?? this.attachShadow({ mode: "open" });
134
- root.innerHTML = `<div id="stage"></div><div id="labels" aria-hidden="true"></div>`;
138
+ root.innerHTML = `<div id="stage"></div><div id="labels" aria-hidden="true"></div><p id="meteor-note" hidden></p>`;
135
139
  adoptStyles(root, STYLE, this.nonce);
136
140
  this.#stage = root.getElementById("stage");
137
141
  this.#labelBox = root.getElementById("labels");
142
+ this.#note = root.getElementById("meteor-note");
143
+ this.#played = false;
144
+ this.#model.meteors.on = this.#meteorsAsked();
145
+ this.#model.meteors.running = () => this.#speed !== 0 && document.visibilityState !== "hidden" && (this.#played || !prefersLessMotion());
146
+ this.#model.meteors.onShower = (shower, zhr) => this.dispatchEvent(new CustomEvent("chikyu-meteors", { detail: { shower, zhr } }));
147
+ setMeteorNote(this.#note, this, this.#model.meteors.on);
138
148
  this.#pool.clear();
139
149
  const nav = navigator;
140
150
  this.#gate = createGate(1000 / frameCap(30, { deviceMemory: nav.deviceMemory, hardwareConcurrency: nav.hardwareConcurrency }), () => this.#view?.invalidate());
@@ -210,6 +220,34 @@ export class ChikyuSpace extends HTMLElement {
210
220
  this.bodies = value ?? "";
211
221
  else if (name === "light")
212
222
  this.light = readLight(value);
223
+ else if (name === "meteors")
224
+ this.meteors = this.#meteorsAsked();
225
+ else if (name === "meteors-label" || name === "lang")
226
+ setMeteorNote(this.#note, this, this.#model.meteors.on);
227
+ }
228
+ #meteorsAsked() {
229
+ return this.hasAttribute("meteors") && this.getAttribute("meteors") !== "off";
230
+ }
231
+ /**
232
+ * Meteors seen from space, on or off (the attribute `meteors`): short streaks where meteors burn, on the dark side of the Earth and in its twilight, more of them under an active shower, only while the
233
+ * clock runs, and for a reader who prefers reduced motion only once the page has set a `speed` (a Play). Each streak's place and moment are drawn at random, and a line under the picture says so
234
+ * (`meteors-label="off"` takes it away); how often they appear, where, how high and when follow real data (`@johnmorrisdotca/chikyu-earth/meteors`). None are drawn while the Earth is under 40 pixels in radius.
235
+ * `chikyu-meteors` is sent when the strongest active shower changes, with `detail: { shower, zhr }`. Off by default, and nothing of theirs is made until they are first switched on.
236
+ */
237
+ get meteors() {
238
+ return this.#model.meteors.on;
239
+ }
240
+ set meteors(value) {
241
+ if (value !== this.#meteorsAsked()) {
242
+ if (value)
243
+ this.setAttribute("meteors", "");
244
+ else
245
+ this.removeAttribute("meteors");
246
+ return;
247
+ }
248
+ this.#model.meteors.on = value;
249
+ setMeteorNote(this.#note, this, value);
250
+ this.#gate?.request();
213
251
  }
214
252
  get centre() {
215
253
  return { ...this.#model.centre };
@@ -239,6 +277,8 @@ export class ChikyuSpace extends HTMLElement {
239
277
  if (this.#speed !== 0 && this.isConnected)
240
278
  this.#advance(performance.now());
241
279
  const was = this.#speed;
280
+ if (this.isConnected && this.#view)
281
+ this.#played = true;
242
282
  this.#speed = Number.isFinite(value) ? value : 1;
243
283
  this.#model.speed = this.#speed;
244
284
  this.#model.stepMs = Math.max(1000, Math.abs(this.#speed) * 1000);
@@ -518,6 +558,7 @@ export class ChikyuSpace extends HTMLElement {
518
558
  ride: { riding: this.#model.ride !== null, lost: this.#model.rideLost, gliding: this.#model.transition !== null },
519
559
  camera: { position: [this.#model.pose.p[0], this.#model.pose.p[1], this.#model.pose.p[2]], fov: this.#model.pose.fov },
520
560
  bodies: { look: this.#model.bodies || painter?.looks?.[0] || "plain", painted: this.#model.painted.bodies, moons: this.#model.painted.moons },
561
+ meteors: { on: this.#model.meteors.on, ...this.#model.meteors.drawn },
521
562
  };
522
563
  }
523
564
  #satelliteStats() {
@@ -8,6 +8,7 @@ import { type ScaleMode } from "./scale.ts";
8
8
  import type { SatelliteRef } from "../satellites/types.ts";
9
9
  import { type RideLook } from "./ride.ts";
10
10
  import { type SatelliteState } from "./satellite-layer.ts";
11
+ import { type MeteorState } from "../meteors/layer.ts";
11
12
  type Three = ThreeParts;
12
13
  /** The camera's distance, in Earth radii, at which the Earth fills the view at zoom 1. */
13
14
  export declare const FIT_DISTANCE = 3.4;
@@ -105,6 +106,8 @@ export interface SpaceModel {
105
106
  speed: number;
106
107
  /** The satellites shown, the one chosen, and what the last draw did with them. */
107
108
  satellites: SatelliteState;
109
+ /** Meteors seen from space, on the Earth (src/meteors/layer.ts): off unless the element switches them on. */
110
+ meteors: MeteorState;
108
111
  /** The body or satellite the camera rides along with, or null. */
109
112
  ride: RideState | null;
110
113
  /** 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. */
@@ -5,6 +5,8 @@ import { createTrack, orbitPath, PLANETS } from "./ephemeris.js";
5
5
  import { placeAround, placeMoon, placeSun, RADIUS } from "./scale.js";
6
6
  import { ridePose } from "./ride.js";
7
7
  import { createSatelliteLayer, newSatelliteState } from "./satellite-layer.js";
8
+ import { createMeteorLayer, newMeteorState } from "../meteors/layer.js";
9
+ import { subsolarPoint, unitVector } from "../realistic/sun.js";
8
10
  const RAD = Math.PI / 180;
9
11
  /** The camera's distance, in Earth radii, at which the Earth fills the view at zoom 1. */
10
12
  export const FIT_DISTANCE = 3.4;
@@ -53,6 +55,7 @@ export const newSpaceModel = () => ({
53
55
  pose: { p: [0, 0, 0], q: [0, 0, 0, 1], fov: FOV, valid: false },
54
56
  rideMotion: 0,
55
57
  satellites: newSatelliteState(),
58
+ meteors: newMeteorState(),
56
59
  pick: null,
57
60
  ratio: () => Math.min(globalThis.devicePixelRatio || 1, 2),
58
61
  });
@@ -231,6 +234,8 @@ export function createSpaceItem(model, deps) {
231
234
  }
232
235
  const moonSlots = new Map();
233
236
  let releaseTimer;
237
+ let meteors = null;
238
+ let meteorTimer;
234
239
  let layer;
235
240
  const rideState = new Float64Array(6);
236
241
  let selectedShown = false;
@@ -535,6 +540,20 @@ export function createSpaceItem(model, deps) {
535
540
  deps.onSelected(at);
536
541
  selectedShown = at !== null;
537
542
  }
543
+ // the meteors, on the Earth: made the first time they are switched on, and asking for their own next draw while a streak is alive or one is due
544
+ if (model.meteors.on && !meteors)
545
+ meteors = createMeteorLayer(three, earth, model.meteors);
546
+ if (meteors) {
547
+ // the camera's place, turned into the Earth's own axes (the Earth is at the middle of the scene and turns about its pole)
548
+ const turn = earth.rotation.y;
549
+ const fx = camera.position.x * Math.cos(turn) - camera.position.z * Math.sin(turn);
550
+ const fz = camera.position.x * Math.sin(turn) + camera.position.z * Math.cos(turn);
551
+ const far = Math.hypot(fx, camera.position.y, fz) || 1;
552
+ const wait = meteors.step({ now: performance.now(), time: model.time, facing: [fx / far, camera.position.y / far, fz / far], sun: unitVector(subsolarPoint(model.time)), radius: Math.min(px.Earth, 1e5), width: size.width, height: size.height });
553
+ if (meteorTimer !== undefined)
554
+ clearTimeout(meteorTimer);
555
+ meteorTimer = Number.isFinite(wait) ? setTimeout(() => deps.invalidate(), wait) : undefined;
556
+ }
538
557
  if (model.rideLost)
539
558
  deps.onRideLost?.();
540
559
  if (model.transition)
@@ -547,6 +566,9 @@ export function createSpaceItem(model, deps) {
547
566
  dispose() {
548
567
  if (releaseTimer !== undefined)
549
568
  clearTimeout(releaseTimer);
569
+ if (meteorTimer !== undefined)
570
+ clearTimeout(meteorTimer);
571
+ meteors?.dispose();
550
572
  layer?.dispose();
551
573
  model.pick = null;
552
574
  textures.dispose();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@johnmorrisdotca/chikyu-earth",
3
- "version": "0.23.0",
3
+ "version": "0.25.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 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",
@@ -119,6 +119,11 @@
119
119
  "import": "./dist/space/element.js",
120
120
  "default": "./dist/space/element.js"
121
121
  },
122
+ "./meteors": {
123
+ "types": "./dist/meteors/index.d.ts",
124
+ "import": "./dist/meteors/index.js",
125
+ "default": "./dist/meteors/index.js"
126
+ },
122
127
  "./satellites": {
123
128
  "types": "./dist/satellites/index.d.ts",
124
129
  "import": "./dist/satellites/index.js",