@johnmorrisdotca/chikyu-earth 0.38.0 → 0.40.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.
Files changed (39) hide show
  1. package/CHANGELOG.md +13 -0
  2. package/README.md +2 -1
  3. package/dist/cities/geometry.d.ts +7 -0
  4. package/dist/cities/geometry.js +1 -1
  5. package/dist/cities/host.d.ts +10 -2
  6. package/dist/cities/host.js +4 -2
  7. package/dist/markers/layer.d.ts +16 -1
  8. package/dist/markers/layer.js +3 -2
  9. package/dist/markers/place-layer.js +3 -0
  10. package/dist/paint/buffer.d.ts +6 -1
  11. package/dist/paint/buffer.js +151 -68
  12. package/dist/paint/texture.d.ts +16 -6
  13. package/dist/paint/texture.js +46 -27
  14. package/dist/realistic/earth.d.ts +1 -1
  15. package/dist/realistic/earth.js +5 -4
  16. package/dist/realistic/element-base.d.ts +437 -0
  17. package/dist/realistic/element-base.js +1249 -0
  18. package/dist/realistic/element.d.ts +2 -431
  19. package/dist/realistic/element.js +3 -1203
  20. package/dist/realistic/index.d.ts +1 -1
  21. package/dist/realistic/no-cultures.d.ts +1 -0
  22. package/dist/realistic/no-cultures.js +2 -0
  23. package/dist/realistic/tiles.d.ts +24 -0
  24. package/dist/realistic/tiles.js +30 -12
  25. package/dist/sky/culture-source.d.ts +15 -0
  26. package/dist/sky/culture-source.js +15 -0
  27. package/dist/sky/cultures.d.ts +1 -0
  28. package/dist/sky/cultures.js +8 -0
  29. package/dist/sky/host.d.ts +12 -0
  30. package/dist/sky/host.js +174 -22
  31. package/dist/space/element-base.d.ts +624 -0
  32. package/dist/space/element-base.js +1656 -0
  33. package/dist/space/element.d.ts +2 -612
  34. package/dist/space/element.js +3 -1597
  35. package/dist/space/no-cultures.d.ts +1 -0
  36. package/dist/space/no-cultures.js +2 -0
  37. package/dist/space/scene.d.ts +8 -1
  38. package/dist/space/scene.js +73 -34
  39. package/package.json +23 -3
package/CHANGELOG.md CHANGED
@@ -2,6 +2,19 @@
2
2
 
3
3
  ## Unreleased
4
4
 
5
+ ## 0.40.0 - 2026-11-12
6
+
7
+ - **Names of different layers never overlap** (MODULES-1072). On `<chikyu-space>` and `<chikyu-earth-realistic>` each layer placed its names alone: city names ignored the places, the markers' labels ignored the places' names, and star names ignored every layer's names and rings, so a name could sit over another layer's. The layers now lay out in a fixed order, each keeping clear of the boxes of those before it: the places' names, rings and marks with the ringed satellites, then the markers' labels, then the cities' names, then the stars' and constellations' names (the countries' names, which already kept clear of the others, come after the cities'). A star whose name box or ring touches an earlier box is left out and the next star is named. The order is the draw's: the realistic Earth's places are laid out before its markers and the sky after the cities, as the space view's sky is after its cities. `OverlayHost` has an optional `taken()` (the places' boxes, which `attachMarkers` reads), `CityLayoutOptions` has `taken`, and a satellite's ring is among the boxes the places take. Boxes are handed over when names are laid out, which is when the view changes; nothing is laid out or drawn at rest. Tests: `test/label-overlap.test.ts` and `e2e/label-overlap.gl.ts`.
8
+ - **A glide is counted on the page, not polled** (MODULES-1190). `e2e/ride-above.gl.ts` watched for the camera's glide into the above view by polling `stats.ride.gliding`, and a short glide could be missed (seen in CI on 2026-11-12). The page now counts each glide at every animation frame and the test reads the count; it fails when a view change never glides. A test change only; the package is unchanged.
9
+
10
+ ## 0.39.0 - 2026-11-12
11
+
12
+ - **The painted trail follows the orbit at any clock, and costs nothing until it is used** (MODULES-1188). A month laid down at once, or a fast clock, took samples too far apart and drew chords across the map: samples are now at most a third of a trip apart. A clock run backwards laid down nothing for the older end, so the trail shrank: it now fills it in. A coverage swath over the date line was cut at the edge of the map: it now wraps. Switching to a satellite that paints nothing left the old picture on the globe: the cleared picture is sent. The trail's two megabytes and its picture on the graphics card were made in every view, painting or not: they are made when the first satellite is painted, and `clearPaint()` gives them back.
13
+ - **Sharp ground when riding above a satellite, and the model there** (MODULES-1184). `<chikyu-space>` takes `tiles` (attribute and property, as `<chikyu-earth-realistic>` does) and lays the finer map tiles over its Earth: the level is chosen from the pixels a unit of ground takes under the camera, so from 150 km above the ISS the ground is the finest level (4) and from 3,000 km it is level 3, where it was one blurred 4096 picture; only the tiles under the camera are loaded (at most 48, freed after four seconds), a view of the whole Earth asks for none, and nothing is drawn at rest. `stats.tiles` has the tile set's state and `chikyu-tiles` is sent once when a set has been read. `TileCamera` has an optional `perspective` (`TilePerspective`, new in `/realistic`) for a camera with a real place, which `tilesInView` and `pointOnEarth` use in place of the disc; a disc is read as before. The satellite's model (`setSatellitePainter`) was already drawn in the `above` view as in `follow`; the tests now say so at 150 km (`e2e/ride-above-model.gl.ts`, `e2e/ride-above-tiles.gl.ts`). The demo's space view uses its tiles.
14
+ - **A press on a named star or a constellation figure tells the page** (MODULES-1179). `chikyu-sky-pick` is sent on `<chikyu-space>` and `<chikyu-earth-realistic>` when a reader presses or taps one. A star's detail is `{ kind: "star", hr, name, designation, x, y, pointerType }` (`name` as drawn, `x` and `y` CSS pixels inside the element, `pointerType` `mouse`, `pen`, `touch` or `keyboard`). A figure's detail is `{ kind: "constellation", abbreviation, pointerType }`. Each named star and each figure in view is a button, so a keyboard reaches it and Enter picks it, and the pick is announced; the keys `C` and `Shift`+`C` (the next and the previous figure) send it too (`pointerType` `keyboard`). Nothing is drawn at rest.
15
+ - **A build without the sky cultures** (MODULES-1185). `@johnmorrisdotca/chikyu-earth/space/no-cultures` and `@johnmorrisdotca/chikyu-earth/realistic/no-cultures` are `<chikyu-space>` and `<chikyu-earth-realistic>` with no sky culture but the IAU's: nothing under `hoshi-stars/cultures` is imported, so a site's bundler no longer needs a stub for it, and `constellation-culture` is `iau` whatever a page sets (one warning). The default entries are unchanged: they bring in `western`, `japanese` and `chinese`, a dynamic import each. How: the elements' code moved to `element-base.ts` in each folder; `space/element` and `realistic/element` are now that code plus `sky/cultures.ts`, which sets the table the sky layer reads (`setCultureLoaders`). A test reads each entry's imports and finds no `hoshi-stars/cultures` in the two new ones and all three in the default ones, and the usage test bundles the new entries with every such import refused.
16
+ - **The demo takes every planet picture at the widest width again** (MODULES-1175). The demo's `wakusei-planets` is `^0.38.0`, which stops at the widest picture its package ships when a page gives a `textureBase` and no `farBase`, so the demo's cap of 1024 on the painter (and its 404s for 2048) is gone.
17
+
5
18
  ## 0.38.0 - 2026-11-12
6
19
 
7
20
  - **Paint a satellite's path on the Earth** (MODULES-1167). `paint` on `<chikyu-space>` and `<chikyu-earth-realistic>` is `{ id, length, swath }`. `id` is a NORAD catalogue number or `group:index`. `length` is `1d` (the default), `1w` or `1m` of simulated time. `swath` is `track` (the point under the satellite, the default) or `coverage` (the ground where the satellite is at least 10° above the horizon, from its height). `clearPaint()` takes the trail off. Changing the satellite clears it. The trail is one equirectangular texture on the globe. It is stamped only while simulated time moves, one upload a frame, and nothing at rest or while painting is off. A part older than `length` fades out. The date line and the poles are split so the line does not cross the map. The demo's satellite card has Paint, Clear, and 1 day, 1 week, 1 month.
package/README.md CHANGED
@@ -547,6 +547,7 @@ The reference is a site, made from the source's own doc comments: every export h
547
547
  | `.../realistic`, `.../realistic/element` | The Earth from space; `three` for the element. |
548
548
  | `.../markers` | `attachMarkers` and the pure projection, clusters and labels. |
549
549
  | `.../space`, `.../space/element` | The Sun, the Moon and the planets; `astronomy-engine` and `three`. |
550
+ | `.../space/no-cultures`, `.../realistic/no-cultures` | The same two elements with no sky culture but the IAU's: nothing under `hoshi-stars/cultures` is in the build ([guide](docs/guides/sky-names.md)). |
550
551
  | `.../satellites` | SGP4 for a group of satellites; `satellite.js`. |
551
552
  | `.../meteors` | The year's meteor showers, and when and where their meteors fall. No DOM, no dependency. |
552
553
  | `.../door` | The data door: sources of your own maps and orbit data with their licence and credit, `creditLine`, the package's own sources. No DOM, no dependency. |
@@ -608,7 +609,7 @@ console.log(countryList().filter((country) => country.alpha2 === null).map((coun
608
609
 
609
610
  ## How to cite
610
611
 
611
- > Morris, J. (2026). *Chikyu Earth* (version 0.38.0) [Computer software]. https://github.com/johnmorrisdotca/chikyu-earth
612
+ > Morris, J. (2026). *Chikyu Earth* (version 0.40.0) [Computer software]. https://github.com/johnmorrisdotca/chikyu-earth
612
613
 
613
614
  GitHub's "Cite this repository" button reads [CITATION.cff](CITATION.cff) and gives BibTeX and APA. Cite the version you used (`npm ls @johnmorrisdotca/chikyu-earth`), and credit the sources behind the numbers and pictures: [NOTICE.md](NOTICE.md) says who made each one and under what terms.
614
615
 
@@ -77,6 +77,13 @@ export interface CityLayoutOptions {
77
77
  * (a fallback font for Japanese, say) would otherwise lay names over one another. Left out, the estimate alone is used, as in Node.
78
78
  */
79
79
  measure?: (text: string) => number;
80
+ /** Boxes another layer's names, rings and marks already take (left, top, width and height from the picture's top left): no name is put over one. */
81
+ taken?: readonly {
82
+ left: number;
83
+ top: number;
84
+ width: number;
85
+ height: number;
86
+ }[];
80
87
  }
81
88
  /**
82
89
  * A city to name: where its dot is, the text to write and where the text's box goes (its top left corner and size).
@@ -124,7 +124,7 @@ export function layoutCities(cities, frame, lang, options) {
124
124
  const height = Math.ceil(options.size * 1.35);
125
125
  const texts = seen.map((one) => cityNameText(one.city, lang));
126
126
  const items = seen.map((one, index) => ({ x: one.x, y: one.y, r: options.dot, w: Math.max(nameWidth(texts[index], options.size), options.measure ? Math.ceil(options.measure(texts[index])) + 4 : 0), h: height, priority: seen.length - index }));
127
- return placeLabels(items, frame.width, frame.height, options.max).map((place) => {
127
+ return placeLabels(items, frame.width, frame.height, options.max, 64, options.taken).map((place) => {
128
128
  const one = seen[place.item];
129
129
  return { city: one.city, text: texts[place.item], x: one.x, y: one.y, left: place.left, top: place.top, width: items[place.item].w, height };
130
130
  });
@@ -38,8 +38,16 @@ export interface CityHostOptions {
38
38
  onLayout?(): void;
39
39
  }
40
40
  export interface CityHost {
41
- /** Tells where the Earth is in the picture just drawn, or `null` when there is no Earth to name. */
42
- frame(frame: CityFrame | null): void;
41
+ /**
42
+ * Tells where the Earth is in the picture just drawn, or `null` when there is no Earth to name. `taken` is the boxes the layers placed before this one (the places', the markers', the bodies' names) take: no name goes over one.
43
+ * The list is read again whenever the names are laid out between draws, so the element refills the same array.
44
+ */
45
+ frame(frame: CityFrame | null, taken?: readonly {
46
+ left: number;
47
+ top: number;
48
+ width: number;
49
+ height: number;
50
+ }[]): void;
43
51
  /** Reads the attribute and the language again after either changed. */
44
52
  refresh(): void;
45
53
  /** The look of the names (the property `cityNamesStyle`). */
@@ -35,6 +35,7 @@ export function createCityHost(options) {
35
35
  let version = 0;
36
36
  let cache;
37
37
  let last = null;
38
+ let lastTaken;
38
39
  let shown = [];
39
40
  let renders = 0;
40
41
  let style = {};
@@ -154,7 +155,7 @@ export function createCityHost(options) {
154
155
  const size = style.size ?? DEFAULTS.size;
155
156
  const dot = style.dotSize ?? DEFAULTS.dot;
156
157
  usedLang = options.language();
157
- shown = layoutCities(citiesFor(tiers), frame, usedLang, { max, size, dot, measure: measurer(size) });
158
+ shown = layoutCities(citiesFor(tiers), frame, usedLang, { max, size, dot, measure: measurer(size), ...(lastTaken ? { taken: lastTaken } : {}) });
158
159
  place(frame, dot);
159
160
  }
160
161
  function place(frame, dot) {
@@ -223,10 +224,11 @@ export function createCityHost(options) {
223
224
  };
224
225
  doc.fonts?.addEventListener?.("loadingdone", onFonts);
225
226
  return {
226
- frame(next) {
227
+ frame(next, taken) {
227
228
  if (gone)
228
229
  return;
229
230
  last = next;
231
+ lastTaken = taken;
230
232
  if (max === null)
231
233
  return;
232
234
  if (!next) {
@@ -33,13 +33,28 @@ export interface OverlayHost {
33
33
  readonly overlayRoot: HTMLElement;
34
34
  readonly pose: OverlayPose | null;
35
35
  subscribe(listener: (pose: OverlayPose) => void): () => void;
36
- /** Tells the element the boxes an overlay's marks and labels take now (an empty list when it is taken away), so the element's own names keep clear of them. */
36
+ /**
37
+ * Tells the element the boxes an overlay's marks and labels take now (an empty list when it is taken away), so the element's own names keep clear of them. `names` is the part of `boxes` that is the labels' (the marks' own boxes are the rest): the cities' names, which are
38
+ * set at a mark's own place, keep clear of the labels and not of the marks.
39
+ */
37
40
  claim?(owner: object, boxes: readonly {
38
41
  left: number;
39
42
  top: number;
40
43
  width: number;
41
44
  height: number;
45
+ }[], names?: readonly {
46
+ left: number;
47
+ top: number;
48
+ width: number;
49
+ height: number;
42
50
  }[]): void;
51
+ /** The boxes the element's own places take now (names, rings, marks): the markers' labels are put clear of them, as the places come first. */
52
+ taken?(): readonly {
53
+ left: number;
54
+ top: number;
55
+ width: number;
56
+ height: number;
57
+ }[];
43
58
  }
44
59
  /**
45
60
  * What {@link attachMarkers} may be asked: the most labels, how close markers gather, and the default colour.
@@ -232,7 +232,7 @@ export function attachMarkers(element, options = {}) {
232
232
  hitMembers.push(g.members);
233
233
  }
234
234
  }
235
- const placed = placeLabels(items, pose.width, pose.height, maxLabels);
235
+ const placed = placeLabels(items, pose.width, pose.height, maxLabels, 64, host.taken?.());
236
236
  for (let k = 0; k < placed.length; k += 1) {
237
237
  const p = placed[k];
238
238
  const m = data.items[labelled[p.item]];
@@ -264,9 +264,10 @@ export function attachMarkers(element, options = {}) {
264
264
  const boxes = [];
265
265
  for (const p of placed)
266
266
  boxes.push({ left: p.left, top: p.top, width: items[p.item].w, height: items[p.item].h });
267
+ const names = boxes.slice();
267
268
  for (let k = 0; k < hitX.length; k += 1)
268
269
  boxes.push({ left: hitX[k] - hitR[k], top: hitY[k] - hitR[k], width: hitR[k] * 2, height: hitR[k] * 2 });
269
- host.claim?.(api, boxes);
270
+ host.claim?.(api, boxes, names);
270
271
  stats.markers = visible.length - groups.reduce((sum, g) => sum + (g.members.length > 1 ? g.members.length : 0), 0);
271
272
  stats.clusters = clusters;
272
273
  stats.labels = placed.length;
@@ -544,6 +544,9 @@ export function createPlaceLayer(box) {
544
544
  boxes.push({ left: spot.left, top: spot.top, width: items[spot.item].w, height: items[spot.item].h });
545
545
  for (const shape of layout.shapes)
546
546
  boxes.push({ left: shape.tipX - 5, top: shape.tipY - 5, width: 10, height: 10 });
547
+ // the ring round each satellite that can be seen: other layers' names keep off it as they keep off its name
548
+ for (const mark of seen)
549
+ boxes.push({ left: mark.x - SATELLITE_RING, top: mark.y - SATELLITE_RING, width: SATELLITE_RING * 2, height: SATELLITE_RING * 2 });
547
550
  stats.shown = shown;
548
551
  stats.marks = order.length;
549
552
  stats.labels = layout.labels.length;
@@ -32,10 +32,11 @@ export interface PaintState {
32
32
  /**
33
33
  * Stamp the ground crossed since the last call. `sample` is the satellite at a moment, or null when it cannot be placed.
34
34
  * Returns true when `data` changed and the picture should upload it. At rest, or with painting off, it returns false.
35
+ * `periodMs` is one trip of the satellite round the Earth: samples are taken often enough that the line between two follows the orbit, however fast the clock. A clock run backwards lays down the older end of the length that it uncovers.
35
36
  */
36
37
  step(ms: number, sample: (ms: number) => (LonLat & {
37
38
  heightKm: number;
38
- }) | null): boolean;
39
+ }) | null, periodMs?: number): boolean;
39
40
  }
40
41
  export declare function createPaintState(): PaintState;
41
42
  /** The satellite `id` names, in the groups now set: a NORAD number, or `group:index`. */
@@ -51,3 +52,7 @@ export declare function paintedGround(groups: Iterable<[string, {
51
52
  }]>, id: string, ms: number): (LonLat & {
52
53
  heightKm: number;
53
54
  }) | null;
55
+ /** One trip of the painted satellite round the Earth, in milliseconds, from its mean motion; undefined when it is not found. */
56
+ export declare function paintedPeriod(groups: Iterable<[string, {
57
+ satellites: Satellites;
58
+ }]>, id: string): number | undefined;
@@ -62,67 +62,128 @@ const stampLine = (data, a, b, minute) => {
62
62
  }
63
63
  return added;
64
64
  };
65
- const angular = (a, b) => {
66
- const lat1 = a.lat * Math.PI / 180;
67
- const lat2 = b.lat * Math.PI / 180;
68
- const dLat = lat2 - lat1;
69
- const dLon = (b.lon - a.lon) * Math.PI / 180;
70
- const h = Math.sin(dLat / 2) ** 2 + Math.cos(lat1) * Math.cos(lat2) * Math.sin(dLon / 2) ** 2;
71
- return 2 * Math.atan2(Math.sqrt(h), Math.sqrt(Math.max(0, 1 - h)));
72
- };
65
+ const RAD = Math.PI / 180;
66
+ /** Every pixel within `theta` radians of `point`, row by row: each row's half-width in longitude is worked out, and a cap over the date line wraps to the other edge. */
73
67
  const stampCap = (data, point, theta, minute) => {
74
68
  if (!(theta > 0))
75
69
  return stampDot(data, point, minute, 1);
76
- const deg = theta * 180 / Math.PI;
77
- const cosLat = Math.max(0.15, Math.cos(point.lat * Math.PI / 180));
78
- const dLon = Math.min(180, deg / cosLat);
79
- const pole = Math.abs(point.lat) + deg >= 90;
80
- const west = pole ? -180 : point.lon - dLon;
81
- const east = pole ? 180 : point.lon + dLon;
82
- const south = Math.max(-90, point.lat - deg);
83
- const north = Math.min(90, point.lat + deg);
84
- const a = paintPixel(west, north);
85
- const b = paintPixel(east, south);
70
+ const deg = theta / RAD;
71
+ const sin1 = Math.sin(point.lat * RAD);
72
+ const cos1 = Math.cos(point.lat * RAD);
73
+ const cosTheta = Math.cos(theta);
74
+ const y0 = paintPixel(0, Math.min(90, point.lat + deg)).y;
75
+ const y1 = paintPixel(0, Math.max(-90, point.lat - deg)).y;
86
76
  let added = 0;
87
- const x0 = pole ? 0 : Math.max(0, Math.min(a.x, b.x));
88
- const x1 = pole ? PAINT_W - 1 : Math.min(PAINT_W - 1, Math.max(a.x, b.x));
89
- const y0 = Math.max(0, Math.min(a.y, b.y));
90
- const y1 = Math.min(PAINT_H - 1, Math.max(a.y, b.y));
91
77
  for (let y = y0; y <= y1; y += 1) {
92
- const lat = 90 - (y + 0.5) / PAINT_H * 180;
93
- for (let x = x0; x <= x1; x += 1) {
94
- let lon = (x + 0.5) / PAINT_W * 360 - 180;
95
- if (!pole && west < -180)
96
- lon = lon > 0 ? lon - 360 : lon;
97
- if (angular(point, { lon, lat }) <= theta && write(data, x, y, minute))
98
- added += 1;
78
+ const lat = (90 - ((y + 0.5) / PAINT_H) * 180) * RAD;
79
+ const denominator = cos1 * Math.cos(lat);
80
+ const need = cosTheta - sin1 * Math.sin(lat);
81
+ let half;
82
+ if (denominator < 1e-9) {
83
+ if (need > 0)
84
+ continue;
85
+ half = 180;
86
+ }
87
+ else {
88
+ const ratio = need / denominator;
89
+ if (ratio > 1)
90
+ continue;
91
+ half = ratio <= -1 ? 180 : Math.acos(ratio) / RAD;
92
+ }
93
+ const first = Math.floor(((point.lon - half + 180) / 360) * PAINT_W);
94
+ const last = Math.floor(((point.lon + half + 180) / 360) * PAINT_W);
95
+ if (last - first >= PAINT_W - 1) {
96
+ for (let x = 0; x < PAINT_W; x += 1)
97
+ if (write(data, x, y, minute))
98
+ added += 1;
99
+ }
100
+ else {
101
+ for (let x = first; x <= last; x += 1)
102
+ if (write(data, ((x % PAINT_W) + PAINT_W) % PAINT_W, y, minute))
103
+ added += 1;
99
104
  }
100
105
  }
101
106
  return added;
102
107
  };
103
108
  const stampGround = (data, swath, ground, previous, minute) => {
104
- if (swath === "coverage")
105
- return { added: stampCap(data, ground, coverageAngleRad(ground.heightKm), minute), at: ground };
106
- if (!previous)
109
+ const theta = swath === "coverage" ? coverageAngleRad(ground.heightKm) : 0;
110
+ if (swath === "coverage" && previous === null)
111
+ return { added: stampCap(data, ground, theta, minute), at: ground };
112
+ if (swath === "track" && previous === null)
107
113
  return { added: stampDot(data, ground, minute, 1), at: ground };
114
+ // a line is made of steps 0.4 of a degree long; a swath of caps that overlap, a step of about seven tenths of the cap's radius
115
+ const points = trackSteps(previous, ground, swath === "coverage" ? Math.max(1, (theta / RAD) * 0.7) : 0.4);
108
116
  let added = 0;
109
117
  let cursor = previous;
110
- for (const point of trackSteps(previous, ground, 0.4)) {
111
- for (const [from, to] of splitDateLine(cursor, point))
112
- added += stampLine(data, from, to, minute);
118
+ for (const point of points) {
119
+ if (swath === "coverage")
120
+ added += stampCap(data, point, theta, minute);
121
+ else
122
+ for (const [from, to] of splitDateLine(cursor, point))
123
+ added += stampLine(data, from, to, minute);
113
124
  cursor = point;
114
125
  }
115
126
  return { added, at: cursor };
116
127
  };
128
+ /** A stamp's age in minutes, from the 16 bits it holds. */
129
+ const ageOf = (then, now) => {
130
+ let age = now - then;
131
+ if (age < -32768)
132
+ age += 65536;
133
+ if (age > 32767)
134
+ age -= 65536;
135
+ return age;
136
+ };
137
+ /** The longest gap between two samples that a line may leap: a twentieth of a trip, and between 30 seconds and four minutes. The samples are never more than a third of a trip apart, however many that takes. */
138
+ const MAX_GAP = 240_000;
139
+ const MIN_GAP = 30_000;
140
+ const FRAME_SAMPLES = 600;
141
+ const REBUILD_SAMPLES = { track: 4000, coverage: 1500 };
142
+ const gapOf = (periodMs) => (periodMs !== undefined && periodMs > 0 ? Math.max(MIN_GAP, Math.min(MAX_GAP, periodMs / 20)) : 20_000);
117
143
  export function createPaintState() {
118
- const data = new Uint8Array(PAINT_W * PAINT_H * 4);
144
+ // the two megabytes are taken when the first satellite is painted and given back by `clear`, not made with the view
145
+ let buffer = null;
146
+ const grab = () => (buffer ??= new Uint8Array(PAINT_W * PAINT_H * 4));
119
147
  let spec = null;
120
148
  let pixels = 0;
121
149
  let lastMs = Number.NaN;
150
+ /** The last point stamped and the way the clock was going then, so a line carries on from it. */
122
151
  let previous = null;
123
- let uploadClear = false;
152
+ let heading = 0;
153
+ /** The buffer was emptied since the last step: the picture must be sent even if nothing new was stamped. */
154
+ let stale = false;
155
+ const reset = () => {
156
+ buffer?.fill(0);
157
+ pixels = 0;
158
+ lastMs = Number.NaN;
159
+ previous = null;
160
+ heading = 0;
161
+ stale = true;
162
+ };
163
+ /** Take out what has fallen out of the length (or is later than the clock now says). */
164
+ const dropOld = (data, now, span) => {
165
+ let dropped = false;
166
+ for (let i = 0; i < data.length; i += 4) {
167
+ if (data[i + 3] === 0)
168
+ continue;
169
+ const then = ((data[i] ?? 0) << 8) | (data[i + 1] ?? 0);
170
+ const age = ageOf(then, now);
171
+ // a minute of slack at each end, for the rounding of the stamp's minute
172
+ if (age > span + 1 || age < -1) {
173
+ data[i] = 0;
174
+ data[i + 1] = 0;
175
+ data[i + 2] = 0;
176
+ data[i + 3] = 0;
177
+ pixels -= 1;
178
+ dropped = true;
179
+ }
180
+ }
181
+ return dropped;
182
+ };
124
183
  const self = {
125
- data,
184
+ get data() {
185
+ return grab();
186
+ },
126
187
  width: PAINT_W,
127
188
  height: PAINT_H,
128
189
  get pixels() {
@@ -153,55 +214,71 @@ export function createPaintState() {
153
214
  const swath = paintSwathOf(next.swath);
154
215
  if (spec && spec.id === id && spec.length === length && spec.swath === swath)
155
216
  return;
156
- data.fill(0);
157
- pixels = 0;
158
- lastMs = Number.NaN;
159
- previous = null;
217
+ reset();
160
218
  spec = { id, length, swath };
161
219
  },
162
220
  clear() {
163
- data.fill(0);
164
- pixels = 0;
165
- lastMs = Number.NaN;
166
- previous = null;
221
+ reset();
167
222
  spec = null;
168
- uploadClear = true;
223
+ buffer = null;
169
224
  },
170
- step(ms, sample) {
225
+ step(ms, sample, periodMs) {
171
226
  if (!spec) {
172
- const upload = uploadClear;
173
- uploadClear = false;
227
+ const upload = stale;
228
+ stale = false;
174
229
  return upload;
175
230
  }
176
231
  if (!Number.isFinite(ms))
177
232
  return false;
178
- if (Number.isFinite(lastMs) && ms === lastMs)
179
- return false;
233
+ if (Number.isFinite(lastMs) && ms === lastMs) {
234
+ const upload = stale;
235
+ stale = false;
236
+ return upload;
237
+ }
238
+ const data = grab();
180
239
  const length = LENGTH_MS[spec.length];
181
- let dirty = false;
182
- const put = (at) => {
183
- const ground = sample(at);
184
- if (!ground)
185
- return;
186
- const stamped = stampGround(data, spec.swath, ground, previous, paintMinute(at));
187
- pixels += stamped.added;
188
- previous = stamped.at;
189
- dirty = true;
240
+ const gap = gapOf(periodMs);
241
+ let dirty = stale;
242
+ stale = false;
243
+ /** Stamp the satellite's place at each of `times`, one line from the one before. */
244
+ const lay = (times, way) => {
245
+ if (way !== heading)
246
+ previous = null;
247
+ heading = way;
248
+ for (const at of times) {
249
+ const ground = sample(at);
250
+ if (!ground) {
251
+ previous = null;
252
+ continue;
253
+ }
254
+ const stamped = stampGround(data, spec.swath, ground, previous, paintMinute(at));
255
+ pixels += stamped.added;
256
+ previous = stamped.at;
257
+ dirty = true;
258
+ }
259
+ };
260
+ const between = (from, to, cap) => {
261
+ const span = Math.abs(to - from);
262
+ const count = Math.max(Math.min(cap, Math.max(1, Math.ceil(span / gap))), periodMs === undefined ? 1 : Math.ceil(span / (periodMs / 3)));
263
+ return Array.from({ length: count + 1 }, (_, i) => from + ((to - from) * i) / count);
190
264
  };
191
265
  if (!Number.isFinite(lastMs) || Math.abs(ms - lastMs) > length) {
266
+ // the first moment, or a jump: the whole length is laid down again, oldest first
192
267
  data.fill(0);
193
268
  pixels = 0;
194
269
  previous = null;
195
- const steps = Math.min(240, Math.max(1, Math.ceil(length / 60_000)));
196
- for (let i = steps; i >= 0; i -= 1)
197
- put(ms - (length * i) / steps);
270
+ heading = 1;
271
+ lay(between(ms - length, ms, REBUILD_SAMPLES[spec.swath]), 1);
272
+ }
273
+ else if (ms > lastMs) {
274
+ lay(between(lastMs, ms, FRAME_SAMPLES), 1);
198
275
  }
199
276
  else {
200
- const span = ms - lastMs;
201
- const steps = Math.min(96, Math.max(1, Math.ceil(Math.abs(span) / 20_000)));
202
- for (let i = 1; i <= steps; i += 1)
203
- put(lastMs + (span * i) / steps);
277
+ // backwards: the newer end falls away below, and the older end, which the clock has just uncovered, is laid down
278
+ lay(between(lastMs - length, ms - length, FRAME_SAMPLES), -1);
204
279
  }
280
+ if (dropOld(data, paintMinute(ms), length / MINUTE))
281
+ dirty = true;
205
282
  lastMs = ms;
206
283
  return dirty;
207
284
  },
@@ -236,3 +313,9 @@ export function paintedGround(groups, id, ms) {
236
313
  return null;
237
314
  return groundUnder(scratch[0], scratch[1], scratch[2], ms);
238
315
  }
316
+ /** One trip of the painted satellite round the Earth, in milliseconds, from its mean motion; undefined when it is not found. */
317
+ export function paintedPeriod(groups, id) {
318
+ const found = findPainted(groups, id);
319
+ const motion = found?.satellites.sets[found.index]?.MEAN_MOTION;
320
+ return motion !== undefined && motion > 0 ? DAY / motion : undefined;
321
+ }
@@ -1,10 +1,15 @@
1
1
  import type * as THREE from "three";
2
2
  import type { ThreeParts } from "../realistic/textures.ts";
3
+ /** Where the trail's picture comes from: its pixels are asked for only while it is on, so a page that paints nothing never makes them. */
4
+ export interface PaintSource {
5
+ data(): Uint8Array;
6
+ width: number;
7
+ height: number;
8
+ }
3
9
  export interface PaintTexture {
4
- texture: THREE.Texture;
5
10
  uniforms: {
6
11
  paintMap: {
7
- value: THREE.Texture;
12
+ value: THREE.Texture | null;
8
13
  };
9
14
  paintNow: {
10
15
  value: number;
@@ -16,9 +21,14 @@ export interface PaintTexture {
16
21
  value: number;
17
22
  };
18
23
  };
19
- /** Upload `data` on the next draw. */
20
- upload(): void;
24
+ /** Whether the picture is on the graphics card now. */
25
+ readonly held: boolean;
26
+ /**
27
+ * Make the picture when painting goes on (and send it), send it again when `changed`, and give it back when painting is off.
28
+ * With painting off nothing is made and nothing is held.
29
+ */
30
+ sync(on: boolean, changed: boolean): void;
21
31
  dispose(): void;
22
32
  }
23
- /** A texture over `data` (equirectangular, north in the first row). The same uniforms go on the sphere and on any tile laid over it. */
24
- export declare function createPaintTexture(three: ThreeParts, data: Uint8Array, width?: number, height?: number): PaintTexture;
33
+ /** A texture over the trail (equirectangular, north in the first row), made on the first paint and freed on clear. The same uniforms go on the sphere and on any tile laid over it. */
34
+ export declare function createPaintTexture(three: ThreeParts, source: PaintSource): PaintTexture;
@@ -1,33 +1,52 @@
1
- import { PAINT_H, PAINT_W } from "./math.js";
2
- /** A texture over `data` (equirectangular, north in the first row). The same uniforms go on the sphere and on any tile laid over it. */
3
- export function createPaintTexture(three, data, width = PAINT_W, height = PAINT_H) {
1
+ /** A texture over the trail (equirectangular, north in the first row), made on the first paint and freed on clear. The same uniforms go on the sphere and on any tile laid over it. */
2
+ export function createPaintTexture(three, source) {
4
3
  const DataTexture = three.DataTexture;
5
- const texture = DataTexture ? new DataTexture(data, width, height) : new three.Texture();
6
- if (!DataTexture)
7
- texture.image = { data, width, height };
8
- texture.generateMipmaps = false;
9
- texture.flipY = false;
10
- texture.magFilter = three.LinearFilter;
11
- texture.minFilter = three.LinearFilter;
12
- texture.wrapS = three.ClampToEdgeWrapping;
13
- texture.wrapT = three.ClampToEdgeWrapping;
14
- const space = three.NoColorSpace;
15
- if (space)
16
- texture.colorSpace = space;
17
- texture.needsUpdate = true;
4
+ const uniforms = {
5
+ paintMap: { value: null },
6
+ paintNow: { value: 0 },
7
+ paintSpan: { value: 1440 },
8
+ paintOn: { value: 0 },
9
+ };
10
+ let texture = null;
11
+ const make = () => {
12
+ const data = source.data();
13
+ const made = DataTexture ? new DataTexture(data, source.width, source.height) : new three.Texture();
14
+ if (!DataTexture)
15
+ made.image = { data, width: source.width, height: source.height };
16
+ made.generateMipmaps = false;
17
+ made.flipY = false;
18
+ made.magFilter = three.LinearFilter;
19
+ made.minFilter = three.LinearFilter;
20
+ made.wrapS = three.ClampToEdgeWrapping;
21
+ made.wrapT = three.ClampToEdgeWrapping;
22
+ const space = three.NoColorSpace;
23
+ if (space)
24
+ made.colorSpace = space;
25
+ made.needsUpdate = true;
26
+ return made;
27
+ };
28
+ const free = () => {
29
+ texture?.dispose();
30
+ texture = null;
31
+ uniforms.paintMap.value = null;
32
+ };
18
33
  return {
19
- texture,
20
- uniforms: {
21
- paintMap: { value: texture },
22
- paintNow: { value: 0 },
23
- paintSpan: { value: 1440 },
24
- paintOn: { value: 0 },
25
- },
26
- upload() {
27
- texture.needsUpdate = true;
34
+ uniforms,
35
+ get held() {
36
+ return texture !== null;
28
37
  },
29
- dispose() {
30
- texture.dispose();
38
+ sync(on, changed) {
39
+ if (!on) {
40
+ free();
41
+ return;
42
+ }
43
+ if (texture === null) {
44
+ texture = make();
45
+ uniforms.paintMap.value = texture;
46
+ }
47
+ else if (changed)
48
+ texture.needsUpdate = true;
31
49
  },
50
+ dispose: free,
32
51
  };
33
52
  }
@@ -138,7 +138,7 @@ export interface EarthDeps {
138
138
  onPlaceRings?: (drawn: boolean) => void;
139
139
  /** The painted satellite trail, when the element has one. Read at every draw; nothing is uploaded while `step` returns false. */
140
140
  paint?: {
141
- data: Uint8Array;
141
+ data(): Uint8Array;
142
142
  width: number;
143
143
  height: number;
144
144
  step(ms: number): boolean;