@johnmorrisdotca/wakusei-planets 0.59.0 → 0.60.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.
- package/CHANGELOG.md +4 -0
- package/README.md +16 -3
- package/dist/flat/bodies.d.ts +122 -0
- package/dist/flat/bodies.js +115 -0
- package/dist/flat/define.d.ts +3 -0
- package/dist/flat/define.js +6 -0
- package/dist/flat/element.d.ts +145 -0
- package/dist/flat/element.js +1614 -0
- package/dist/flat/geometry.d.ts +127 -0
- package/dist/flat/geometry.js +121 -0
- package/dist/flat/index.d.ts +5 -0
- package/dist/flat/index.js +6 -0
- package/dist/flat/scene.d.ts +152 -0
- package/dist/flat/scene.js +195 -0
- package/dist/flat/words.d.ts +55 -0
- package/dist/flat/words.js +133 -0
- package/package.json +13 -1
|
@@ -0,0 +1,1614 @@
|
|
|
1
|
+
var _a;
|
|
2
|
+
// <wakusei-flat>: the Moon or Mars on a flat map. A canvas for the map (the body's own picture, day and night, the Sun) with the landing, rover and crash sites on it, and a canvas for the orbiters, which move over it with their coverage circles and ground tracks.
|
|
3
|
+
// The same attributes, events and look as chikyu-earth's <chikyu-flat>, so a page can swap one for the other behind one control. Nothing runs on a timer when the clock is held, and nothing is drawn at rest, out of sight or in a hidden tab.
|
|
4
|
+
// This file registers nothing: `@johnmorrisdotca/wakusei-planets/flat/define` does.
|
|
5
|
+
import { coverageAngleRad } from "../ground-track.js";
|
|
6
|
+
import { orbiterElements } from "../orbiters.js";
|
|
7
|
+
import { flagCodeOf, LANDING_SITES } from "../landing-sites.js";
|
|
8
|
+
import { layerOf, placeRank, readPlaceLayers, readSurfaceNames } from "../place-marks.js";
|
|
9
|
+
import { loadPlaces } from "../places.js";
|
|
10
|
+
import { texturePaths } from "../scene/textures.js";
|
|
11
|
+
import { BODIES } from "../facts.js";
|
|
12
|
+
import { createGameClock } from "../space/clock.js";
|
|
13
|
+
import { flatBody, isFlatBody, orbiterColour, orbiterName } from "./bodies.js";
|
|
14
|
+
import { MAX_ZOOM, clampView, pixelsPerDegree, projectFlat, wrapLongitude, zoomAbout } from "./geometry.js";
|
|
15
|
+
import { circleOutline, flatOrbiter, flatSun, flatTrack, shadeNight, trackPieces } from "./scene.js";
|
|
16
|
+
import { coordinateText, say } from "./words.js";
|
|
17
|
+
const RAD = Math.PI / 180;
|
|
18
|
+
const TIME_EVENT_MS = 250;
|
|
19
|
+
/** How long, in real seconds, a fast clock's streak covers, and the longest it is drawn, in pixels. */
|
|
20
|
+
const STREAK_SECONDS = 0.25;
|
|
21
|
+
const STREAK_PX = 28;
|
|
22
|
+
/** The furthest the pointer may move between going down and coming up and still be a tap, pixels. */
|
|
23
|
+
const TAP_SLOP = 5;
|
|
24
|
+
/** The clock speed from which the map is shaded in coarser cells and the tracks are worked out less often. */
|
|
25
|
+
const FAST_SPEED = 300;
|
|
26
|
+
/** The widths of the body's pictures: the package's own two, and the sharpest, which the repository holds. */
|
|
27
|
+
const WIDTHS = [1024, 2048];
|
|
28
|
+
const escape = (text) => text.replace(/[&<>"]/g, (c) => ({ "&": "&", "<": "<", ">": ">", '"': """ })[c]);
|
|
29
|
+
const flag = (value) => value !== null && !/^(off|false|no|0)$/i.test(value.trim());
|
|
30
|
+
const finite = (value, fallback) => (value === null || value.trim() === "" || !Number.isFinite(Number(value)) ? fallback : Number(value));
|
|
31
|
+
const readTime = (value) => {
|
|
32
|
+
if (value === null || value.trim() === "" || value.trim().toLowerCase() === "now")
|
|
33
|
+
return "now";
|
|
34
|
+
const text = value.trim();
|
|
35
|
+
if (/^-?\d+(\.\d+)?$/.test(text))
|
|
36
|
+
return Number(text);
|
|
37
|
+
const ms = Date.parse(/[zZ]|[+-]\d\d:?\d\d$/.test(text) || !/T/.test(text) ? text : `${text}Z`);
|
|
38
|
+
return Number.isFinite(ms) ? ms : "now";
|
|
39
|
+
};
|
|
40
|
+
const prefersLessMotion = () => typeof matchMedia === "function" && matchMedia("(prefers-reduced-motion: reduce)").matches;
|
|
41
|
+
const hidden = () => typeof document !== "undefined" && document.visibilityState === "hidden";
|
|
42
|
+
/** The landing site of each place of the tables that is on one: the lander's place, and the rover's and the descent stage's on the same spot. */
|
|
43
|
+
let siteOfPlace;
|
|
44
|
+
const siteOf = (id) => {
|
|
45
|
+
if (!siteOfPlace) {
|
|
46
|
+
siteOfPlace = new Map();
|
|
47
|
+
for (const site of LANDING_SITES)
|
|
48
|
+
for (const place of [site.place, ...(site.alsoAt ?? [])])
|
|
49
|
+
siteOfPlace.set(place, site);
|
|
50
|
+
}
|
|
51
|
+
return siteOfPlace.get(id);
|
|
52
|
+
};
|
|
53
|
+
const FLAG_W = 21;
|
|
54
|
+
const LAYER_WORD = { landing: "landing", crash: "crash", planned: "planned", feature: "feature" };
|
|
55
|
+
const CSS = `
|
|
56
|
+
:host{display:flex;flex-direction:column;gap:8px;color:inherit;font:inherit;container-type:inline-size;min-width:0}
|
|
57
|
+
:host([hidden]){display:none}
|
|
58
|
+
*{box-sizing:border-box}
|
|
59
|
+
.bar{display:grid;gap:6px}
|
|
60
|
+
.bar:empty{display:none}
|
|
61
|
+
.row{display:flex;flex-wrap:wrap;gap:6px;align-items:center}
|
|
62
|
+
.chips{flex-wrap:nowrap;overflow-x:auto;scrollbar-width:thin;padding-bottom:2px}
|
|
63
|
+
.chips>*{flex:none}
|
|
64
|
+
@container (min-width:600px){.chips{flex-wrap:wrap;overflow:visible}}
|
|
65
|
+
.gap{flex:1 1 0}
|
|
66
|
+
button{font:inherit;color:inherit;background:transparent;border:1px solid color-mix(in srgb,currentColor 35%,transparent);border-radius:999px;padding:3px 12px;min-height:32px;cursor:pointer;display:inline-flex;align-items:center;gap:6px}
|
|
67
|
+
button[aria-pressed="true"]{background:color-mix(in srgb,currentColor 14%,transparent);border-color:currentColor}
|
|
68
|
+
button:disabled{opacity:.45;cursor:default}
|
|
69
|
+
button:focus-visible{outline:3px solid #6cd0ff;outline-offset:2px}
|
|
70
|
+
@media (pointer:coarse){button{min-height:44px;padding:6px 14px}}
|
|
71
|
+
.chip .swatch{width:10px;height:10px;border-radius:50%;background:var(--c,#9aa5b1);flex:none}
|
|
72
|
+
.chip .n{opacity:.7;font-variant-numeric:tabular-nums;font-size:.85em}
|
|
73
|
+
.stage{position:relative;flex:1 1 auto;min-height:0;aspect-ratio:var(--wakusei-flat-aspect,2/1);overflow:hidden;border-radius:var(--wakusei-flat-radius,10px);background:var(--wakusei-flat-sea,#0b1830)}
|
|
74
|
+
@container (max-width:599px){.stage{aspect-ratio:var(--wakusei-flat-aspect-narrow,4/3)}}
|
|
75
|
+
canvas{position:absolute;inset:0;width:100%;height:100%;display:block}
|
|
76
|
+
.dots{outline:none;cursor:grab;touch-action:pan-y}
|
|
77
|
+
.dots[data-zoomed]{touch-action:none}
|
|
78
|
+
.dots:focus-visible{outline:3px solid #6cd0ff;outline-offset:-3px}
|
|
79
|
+
.ring{position:absolute;left:0;top:0;width:20px;height:20px;margin:-10px 0 0 -10px;border:2px solid #fff;border-radius:50%;pointer-events:none;box-shadow:0 0 0 1px #000,inset 0 0 0 1px #000}
|
|
80
|
+
.ring.hov{width:14px;height:14px;margin:-7px 0 0 -7px;border-width:1px}
|
|
81
|
+
.ring[hidden],.tip[hidden]{display:none}
|
|
82
|
+
.tip{position:absolute;left:0;top:0;pointer-events:none;background:rgba(8,14,28,.94);color:#fff;padding:4px 8px;border-radius:6px;font-size:.85em;line-height:1.3;max-width:calc(100% - 12px);width:max-content;border:1px solid rgba(255,255,255,.3)}
|
|
83
|
+
.msg{position:absolute;inset:0;display:grid;place-items:end center;margin:0;padding:8px;text-align:center;color:#fff;font-size:.85em;text-shadow:0 0 3px #000,0 0 3px #000;pointer-events:none}
|
|
84
|
+
.msg:empty{display:none}
|
|
85
|
+
.flags{position:absolute;inset:0;overflow:hidden;pointer-events:none}
|
|
86
|
+
.flags[hidden]{display:none}
|
|
87
|
+
.flag{position:absolute;left:0;top:0;height:11px;width:16px;pointer-events:none}
|
|
88
|
+
.flag:not(:defined){display:none}
|
|
89
|
+
.tip .flag{position:static;display:inline-block;vertical-align:-1px;margin-right:6px}
|
|
90
|
+
.sr{position:absolute;width:1px;height:1px;overflow:hidden;clip:rect(0 0 0 0);white-space:nowrap}
|
|
91
|
+
`;
|
|
92
|
+
/** Puts the styles in the shadow root so a strict Content-Security-Policy allows them: a constructable stylesheet is not inline markup. A browser without that gets a <style> carrying the element's `nonce`. */
|
|
93
|
+
const sheets = new Map();
|
|
94
|
+
function adoptStyles(root, css, nonce) {
|
|
95
|
+
if (typeof CSSStyleSheet === "function" && "replaceSync" in CSSStyleSheet.prototype && "adoptedStyleSheets" in root) {
|
|
96
|
+
try {
|
|
97
|
+
let sheet = sheets.get(css);
|
|
98
|
+
if (!sheet) {
|
|
99
|
+
sheet = new CSSStyleSheet();
|
|
100
|
+
sheet.replaceSync(css);
|
|
101
|
+
sheets.set(css, sheet);
|
|
102
|
+
}
|
|
103
|
+
if (!root.adoptedStyleSheets.includes(sheet))
|
|
104
|
+
root.adoptedStyleSheets = [...root.adoptedStyleSheets, sheet];
|
|
105
|
+
return;
|
|
106
|
+
}
|
|
107
|
+
catch {
|
|
108
|
+
// a browser that has the API and refuses it: the <style> below
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
const style = root.ownerDocument.createElement("style");
|
|
112
|
+
if (nonce)
|
|
113
|
+
style.setAttribute("nonce", nonce);
|
|
114
|
+
style.textContent = css;
|
|
115
|
+
root.prepend(style);
|
|
116
|
+
}
|
|
117
|
+
/** Pictures already asked for, by address: a page with two maps of the same body fetches one. */
|
|
118
|
+
const pictures = new Map();
|
|
119
|
+
function picture(url) {
|
|
120
|
+
let held = pictures.get(url);
|
|
121
|
+
if (held === undefined) {
|
|
122
|
+
held = new Promise((resolve, reject) => {
|
|
123
|
+
const image = new Image();
|
|
124
|
+
image.crossOrigin = "anonymous";
|
|
125
|
+
image.onload = () => resolve(image);
|
|
126
|
+
image.onerror = () => {
|
|
127
|
+
pictures.delete(url);
|
|
128
|
+
reject(new Error(`the map ${url} could not be loaded`));
|
|
129
|
+
};
|
|
130
|
+
image.src = url;
|
|
131
|
+
});
|
|
132
|
+
pictures.set(url, held);
|
|
133
|
+
}
|
|
134
|
+
return held;
|
|
135
|
+
}
|
|
136
|
+
/**
|
|
137
|
+
* The `<wakusei-flat>` custom element: the Moon or Mars on a flat map, with day and night from the Sun's direction for the time shown, the landing, rover and crash sites marked, and the spacecraft in orbit as dots that move over it. Import
|
|
138
|
+
* `@johnmorrisdotca/wakusei-planets/flat/define` to register the tag. It needs no WebGL and no peer: the map is the package's own picture of the body (the same one the 3D views are painted from), fetched from the package's CDN folder unless `texture-base` says where yours are.
|
|
139
|
+
*
|
|
140
|
+
* Attributes (the ones marked "same" are the names and meanings `<chikyu-flat>` has): `body` (`moon`, the default, or `mars`), `center` (same: the longitude in the middle, degrees; 0, the near side or the prime meridian, by default; the map wraps round the body),
|
|
141
|
+
* `home-lon` and `home-lat` (same: the reader's place, a chosen site: a marker, and the "My place" button, which appear only when given),
|
|
142
|
+
* `footprint` and `track` (same: the circle on the ground the orbiter can see, and its ground track for one trip round; the footprint and track of the chosen orbiter, or of every orbiter shown when none is chosen), `footprint-elevation` (same: degrees above the horizon the circle is drawn for; 10 by default),
|
|
143
|
+
* `paused` (same: hold the clock and keep the speed; nothing is drawn while it is held), `time` (same: `now`, an ISO date or milliseconds since 1970; live when absent), `speed` (same: 1 is real time, 0 holds, 86400 is a day a second, negative runs backwards),
|
|
144
|
+
* `selected` (same: the chosen orbiter's id such as `lro`, or a place's id such as `moon/apollo-11-lunar-module`), `shade` (same: `off` for no night), `dot-size` (same: pixels), `controls` (same: `none`, or any of `centre zoom footprint track chips`; all by default), `lang` (same: `en` or `ja`; the page's language by default),
|
|
145
|
+
* `orbiters` (`all`, the default, `off`, or some ids with commas: the orbiters drawn), `places` (the kinds of place marked, from `landing` (crewed, landing and rover sites), `crash` (crashes and impacts), `planned` and `feature` (named craters, seas and mountains); `landing` by default, `all` for every kind, `off` for none),
|
|
146
|
+
* `names` (`off` for no names beside the marks, or the most names to show), `site-flags` (the landing sites carry their country's flag: `<hata-flag>` beside the name on the map and in the tip; the page imports `@johnmorrisdotca/hata`, and without it the name stands alone), `texture-base` and `texture-far-base` (where the body's pictures are: the package's folder on jsDelivr by default).
|
|
147
|
+
*
|
|
148
|
+
* Events (the same names as chikyu's with `wakusei-` for `chikyu-`): `wakusei-flat-pick` (an orbiter, a place of the tables (a landing, a rover, a crash, a crater, a mountain) or a landing site under the pointer, tapped, or reached by the keyboard: a `FlatPick` with `kind`, `id`, `name`, `via` and, for a place, `place` (its record) and `site` (the landing site, where it is one); `detail.id` is null when a tap lands on nothing and clears the choice), `wakusei-flat-filter` (a chip changed what is shown: `{ orbiters, layers }`),
|
|
149
|
+
* `wakusei-flat-view` (the reader panned or zoomed: `{ center, zoom }`) and `wakusei-timechange` (`{ time, running, paused }`, at most four a second while time runs; the name the space view uses).
|
|
150
|
+
* Style it with the custom properties `--wakusei-flat-sea`, `--wakusei-flat-aspect`, `--wakusei-flat-aspect-narrow` and `--wakusei-flat-radius`.
|
|
151
|
+
*
|
|
152
|
+
* @example
|
|
153
|
+
* ```ts no-run
|
|
154
|
+
* import "@johnmorrisdotca/wakusei-planets/flat/define";
|
|
155
|
+
* import type { WakuseiFlat } from "@johnmorrisdotca/wakusei-planets/flat/define";
|
|
156
|
+
*
|
|
157
|
+
* const flat = document.createElement("wakusei-flat") as WakuseiFlat;
|
|
158
|
+
* flat.setAttribute("body", "moon");
|
|
159
|
+
* flat.setAttribute("track", "");
|
|
160
|
+
* document.body.append(flat);
|
|
161
|
+
* flat.addEventListener("wakusei-flat-pick", (event) => console.log((event as CustomEvent).detail.name));
|
|
162
|
+
* ```
|
|
163
|
+
*/
|
|
164
|
+
export class WakuseiFlat extends HTMLElement {
|
|
165
|
+
static observedAttributes = ["body", "center", "home-lon", "home-lat", "time", "speed", "paused", "footprint", "track", "footprint-elevation", "selected", "shade", "dot-size", "controls", "lang", "orbiters", "places", "names", "site-flags", "texture-base", "texture-far-base"];
|
|
166
|
+
#body = "moon";
|
|
167
|
+
#info = flatBody("moon");
|
|
168
|
+
#clock = createGameClock("now", 1);
|
|
169
|
+
#speed = 1;
|
|
170
|
+
#paused = false;
|
|
171
|
+
#view = { width: 0, height: 0, centerLon: 0, centerLat: 0, zoom: 1 };
|
|
172
|
+
#dpr = 1;
|
|
173
|
+
#places = [];
|
|
174
|
+
#placeIds = new Map();
|
|
175
|
+
#placesFor = "";
|
|
176
|
+
#layers = new Set(["landing"]);
|
|
177
|
+
#showOrbiters = true;
|
|
178
|
+
#orbiterIds = [];
|
|
179
|
+
#footprint = false;
|
|
180
|
+
#track = false;
|
|
181
|
+
#selected = null;
|
|
182
|
+
#cursor = null;
|
|
183
|
+
#hover = null;
|
|
184
|
+
#orbs = [];
|
|
185
|
+
#marks = [];
|
|
186
|
+
#flagSpots = [];
|
|
187
|
+
#flagLayer;
|
|
188
|
+
#lastDraw = 0;
|
|
189
|
+
#totalMs = 0;
|
|
190
|
+
#timer;
|
|
191
|
+
#frame = 0;
|
|
192
|
+
#inView = true;
|
|
193
|
+
#connected = false;
|
|
194
|
+
#viewSent = "";
|
|
195
|
+
#announced = -Infinity;
|
|
196
|
+
#mapKey = "";
|
|
197
|
+
#image;
|
|
198
|
+
#imageWidth = 0;
|
|
199
|
+
#imageAsked = "";
|
|
200
|
+
#imageFailed = false;
|
|
201
|
+
#imageTurn = 0;
|
|
202
|
+
#placesTurn = 0;
|
|
203
|
+
#shade;
|
|
204
|
+
#tracks = new Map();
|
|
205
|
+
#stage;
|
|
206
|
+
#bar;
|
|
207
|
+
#mapCanvas;
|
|
208
|
+
#dotCanvas;
|
|
209
|
+
#selRing;
|
|
210
|
+
#hovRing;
|
|
211
|
+
#tip;
|
|
212
|
+
#live;
|
|
213
|
+
#msg;
|
|
214
|
+
#sizer;
|
|
215
|
+
#watcher;
|
|
216
|
+
#langWatch;
|
|
217
|
+
#pointers = new Map();
|
|
218
|
+
#drag = null;
|
|
219
|
+
#counts = { draws: 0, mapDraws: 0, dots: 0, lastMs: 0, maxMs: 0 };
|
|
220
|
+
#measureCtx;
|
|
221
|
+
// --- the page's controls ---------------------------------------------------------------------------------------------------------------------
|
|
222
|
+
/** The body on the map: `moon` or `mars`. */
|
|
223
|
+
get body() {
|
|
224
|
+
return this.#body;
|
|
225
|
+
}
|
|
226
|
+
set body(value) {
|
|
227
|
+
this.setAttribute("body", value);
|
|
228
|
+
}
|
|
229
|
+
/** Whether the landing sites carry their country's flag (the attribute `site-flags`): beside the name on the map, and in the tip. The flag is `<hata-flag>` from `@johnmorrisdotca/hata`, which the page imports; where it is not defined the name stands alone. */
|
|
230
|
+
get siteFlags() {
|
|
231
|
+
return this.hasAttribute("site-flags");
|
|
232
|
+
}
|
|
233
|
+
set siteFlags(on) {
|
|
234
|
+
this.toggleAttribute("site-flags", on);
|
|
235
|
+
}
|
|
236
|
+
/** The id of the chosen orbiter or place, or null. Set it to choose one from code (no `wakusei-flat-pick` is sent), `null` to clear. */
|
|
237
|
+
get selected() {
|
|
238
|
+
return this.#selected?.id ?? null;
|
|
239
|
+
}
|
|
240
|
+
set selected(id) {
|
|
241
|
+
this.#selected = this.#find(id);
|
|
242
|
+
this.#sync();
|
|
243
|
+
this.#request();
|
|
244
|
+
}
|
|
245
|
+
/** The longitude in the middle, degrees. Set it to centre the map there; a reader's pan changes it, and `wakusei-flat-view` tells the page. */
|
|
246
|
+
get center() {
|
|
247
|
+
return this.#view.centerLon;
|
|
248
|
+
}
|
|
249
|
+
set center(lon) {
|
|
250
|
+
this.#setView({ ...this.#view, centerLon: lon });
|
|
251
|
+
}
|
|
252
|
+
/** The zoom: 1 shows the whole body, up to 8. */
|
|
253
|
+
get zoom() {
|
|
254
|
+
return this.#view.zoom;
|
|
255
|
+
}
|
|
256
|
+
set zoom(value) {
|
|
257
|
+
this.#setView({ ...this.#view, zoom: value });
|
|
258
|
+
}
|
|
259
|
+
/** The moment shown, in milliseconds since the Unix epoch. Set it to a number (or a `Date`) to move the clock, or "now" to go live. */
|
|
260
|
+
get time() {
|
|
261
|
+
return this.#clock.advance(performance.now());
|
|
262
|
+
}
|
|
263
|
+
set time(value) {
|
|
264
|
+
this.#clock.set(value === "now" ? "now" : typeof value === "number" ? value : value.getTime());
|
|
265
|
+
this.#request();
|
|
266
|
+
this.#schedule();
|
|
267
|
+
}
|
|
268
|
+
/** How fast the clock runs: 1 is real time, 0 holds, 86400 is a day a second, negative runs backwards. */
|
|
269
|
+
get speed() {
|
|
270
|
+
return this.#speed;
|
|
271
|
+
}
|
|
272
|
+
set speed(value) {
|
|
273
|
+
this.#speed = Number.isFinite(value) ? value : 1;
|
|
274
|
+
this.#clock.setSpeed(this.#paused ? 0 : this.#speed);
|
|
275
|
+
this.#schedule();
|
|
276
|
+
this.#request();
|
|
277
|
+
}
|
|
278
|
+
/** True while the clock is held with its speed kept (the page's Pause): nothing is drawn but what the reader changes. Setting it false carries on from the moment shown at the same speed. */
|
|
279
|
+
get paused() {
|
|
280
|
+
return this.#paused;
|
|
281
|
+
}
|
|
282
|
+
set paused(value) {
|
|
283
|
+
if (value === this.#paused)
|
|
284
|
+
return;
|
|
285
|
+
const now = performance.now();
|
|
286
|
+
const shown = this.#clock.advance(now);
|
|
287
|
+
this.#paused = value;
|
|
288
|
+
// held, the moment stays where it is even in real time: it does not jump to the wall clock when carried on
|
|
289
|
+
this.#clock.set(shown, now);
|
|
290
|
+
this.#clock.setSpeed(value ? 0 : this.#speed, now);
|
|
291
|
+
this.#schedule();
|
|
292
|
+
this.#request();
|
|
293
|
+
this.dispatchEvent(new CustomEvent("wakusei-timechange", { detail: { time: shown, running: this.#running(), paused: value }, bubbles: true, composed: true }));
|
|
294
|
+
}
|
|
295
|
+
/** The language of the words: the `lang` attribute, else the page's. */
|
|
296
|
+
get language() {
|
|
297
|
+
return (this.getAttribute("lang") ?? this.closest("[lang]")?.getAttribute("lang") ?? this.ownerDocument.documentElement.lang ?? "en").toLowerCase().startsWith("ja") ? "ja" : "en";
|
|
298
|
+
}
|
|
299
|
+
/** The orbiters the map can draw for this body now: their ids, names and colours, and whether each has a path at the moment shown. A craft with no public path (Tianwen-1, CAPSTONE) is not in the list. */
|
|
300
|
+
get orbiters() {
|
|
301
|
+
const lang = this.language;
|
|
302
|
+
return this.#info.orbiters.filter((id) => orbiterElements(id) !== undefined).map((id) => ({ id, name: orbiterName(id, lang), colour: orbiterColour(id), shown: this.#orbiterIds.includes(id) }));
|
|
303
|
+
}
|
|
304
|
+
/** The places loaded for this body (every kind; the `places` attribute and the chips choose which are marked), or an empty list while they load. */
|
|
305
|
+
get places() {
|
|
306
|
+
return this.#places;
|
|
307
|
+
}
|
|
308
|
+
/** The kinds of place marked now: `landing`, `crash`, `planned` and `feature`. */
|
|
309
|
+
get layers() {
|
|
310
|
+
return [...this.#layers];
|
|
311
|
+
}
|
|
312
|
+
/** Counters for a test or a page: draws of the dots, draws of the map, the orbiters drawn at the last draw, the milliseconds the last draw and the slowest took, the marks drawn, and the canvases' size in pixels. */
|
|
313
|
+
get stats() {
|
|
314
|
+
const canvases = [this.#mapCanvas, this.#dotCanvas].filter((c) => c !== undefined).map((c) => ({ width: c.width, height: c.height }));
|
|
315
|
+
return { draws: this.#counts.draws, mapDraws: this.#counts.mapDraws, dots: this.#counts.dots, marks: this.#marks.length, lastDrawMs: this.#counts.lastMs, maxDrawMs: this.#counts.maxMs, averageDrawMs: this.#counts.draws > 0 ? this.#totalMs / this.#counts.draws : 0, running: this.#running(), paused: this.#paused, fast: this.#fast(), mapLoaded: this.#image !== undefined, canvases };
|
|
316
|
+
}
|
|
317
|
+
/** Where an orbiter or a place is on the screen as of the last draw, in CSS pixels from the map's top left, or null when it is not drawn (not shown, off the map, or an orbiter with no path). */
|
|
318
|
+
placeOf(id) {
|
|
319
|
+
const orb = this.#orbs.find((one) => one.id === id && one.on);
|
|
320
|
+
if (orb)
|
|
321
|
+
return { x: orb.x, y: orb.y, lon: orb.lon, lat: orb.lat, altitudeKm: orb.altitudeKm };
|
|
322
|
+
const mark = this.#marks.find((one) => one.place.id === id);
|
|
323
|
+
return mark ? { x: mark.x, y: mark.y, lon: mark.place.lon, lat: mark.place.lat, altitudeKm: null } : null;
|
|
324
|
+
}
|
|
325
|
+
/** Choose an orbiter or a place from code, as a reader's tap would, without sending `wakusei-flat-pick`. An id the map does not hold, or `null`, clears the choice. */
|
|
326
|
+
pick(id) {
|
|
327
|
+
this.selected = id;
|
|
328
|
+
}
|
|
329
|
+
// --- the element's life ----------------------------------------------------------------------------------------------------------------------
|
|
330
|
+
connectedCallback() {
|
|
331
|
+
const root = this.shadowRoot ?? this.attachShadow({ mode: "open" });
|
|
332
|
+
adoptStyles(root, CSS, this.getAttribute("nonce") ?? "");
|
|
333
|
+
if (!this.#stage)
|
|
334
|
+
this.#build(root);
|
|
335
|
+
this.#connected = true;
|
|
336
|
+
this.#readAll();
|
|
337
|
+
this.#sizer = new ResizeObserver(() => {
|
|
338
|
+
this.#measure();
|
|
339
|
+
this.#request();
|
|
340
|
+
});
|
|
341
|
+
this.#sizer.observe(this.#stage);
|
|
342
|
+
if (typeof IntersectionObserver === "function") {
|
|
343
|
+
this.#watcher = new IntersectionObserver((entries) => {
|
|
344
|
+
const was = this.#visibleNow();
|
|
345
|
+
this.#inView = entries[entries.length - 1]?.isIntersecting ?? true;
|
|
346
|
+
if (!was && this.#visibleNow())
|
|
347
|
+
this.#wake();
|
|
348
|
+
});
|
|
349
|
+
this.#watcher.observe(this);
|
|
350
|
+
}
|
|
351
|
+
// the page's language, when this element has none of its own
|
|
352
|
+
this.#langWatch = new MutationObserver(() => {
|
|
353
|
+
if (!this.hasAttribute("lang"))
|
|
354
|
+
this.#words();
|
|
355
|
+
});
|
|
356
|
+
this.#langWatch.observe(this.ownerDocument.documentElement, { attributes: true, attributeFilter: ["lang"] });
|
|
357
|
+
document.addEventListener("visibilitychange", this.#onVisibility);
|
|
358
|
+
this.#measure();
|
|
359
|
+
this.#words();
|
|
360
|
+
this.setAttribute("data-state", "loading");
|
|
361
|
+
this.#request();
|
|
362
|
+
this.#schedule();
|
|
363
|
+
}
|
|
364
|
+
disconnectedCallback() {
|
|
365
|
+
this.#connected = false;
|
|
366
|
+
clearTimeout(this.#timer);
|
|
367
|
+
this.#timer = undefined;
|
|
368
|
+
if (this.#frame)
|
|
369
|
+
cancelAnimationFrame(this.#frame);
|
|
370
|
+
this.#frame = 0;
|
|
371
|
+
this.#sizer?.disconnect();
|
|
372
|
+
this.#watcher?.disconnect();
|
|
373
|
+
this.#langWatch?.disconnect();
|
|
374
|
+
this.#sizer = this.#watcher = this.#langWatch = undefined;
|
|
375
|
+
document.removeEventListener("visibilitychange", this.#onVisibility);
|
|
376
|
+
}
|
|
377
|
+
attributeChangedCallback(name, old, value) {
|
|
378
|
+
if (!this.#connected || old === value)
|
|
379
|
+
return;
|
|
380
|
+
this.#read(name);
|
|
381
|
+
if (name === "lang" || name === "controls" || name === "home-lon" || name === "home-lat" || name === "body" || name === "orbiters" || name === "places")
|
|
382
|
+
this.#words();
|
|
383
|
+
this.#sync();
|
|
384
|
+
this.#request();
|
|
385
|
+
this.#schedule();
|
|
386
|
+
}
|
|
387
|
+
#onVisibility = () => {
|
|
388
|
+
if (this.#visibleNow())
|
|
389
|
+
this.#wake();
|
|
390
|
+
else {
|
|
391
|
+
clearTimeout(this.#timer);
|
|
392
|
+
this.#timer = undefined;
|
|
393
|
+
}
|
|
394
|
+
};
|
|
395
|
+
#visibleNow() {
|
|
396
|
+
return this.#connected && this.#inView && !hidden();
|
|
397
|
+
}
|
|
398
|
+
/** Back in sight: bring the clock up to date, draw once, and start the timer again. */
|
|
399
|
+
#wake() {
|
|
400
|
+
this.#clock.advance(performance.now());
|
|
401
|
+
this.#request();
|
|
402
|
+
this.#schedule();
|
|
403
|
+
}
|
|
404
|
+
/** Take in every attribute the element has: when it comes into the page. `body` first, since the others are read for it. */
|
|
405
|
+
#readAll() {
|
|
406
|
+
this.#read("body");
|
|
407
|
+
for (const name of _a.observedAttributes)
|
|
408
|
+
if (name !== "body")
|
|
409
|
+
this.#read(name);
|
|
410
|
+
}
|
|
411
|
+
/** Take in one attribute. A change to one never undoes what the reader did with another (a chosen orbiter, the pan). */
|
|
412
|
+
#read(name) {
|
|
413
|
+
const value = this.getAttribute(name);
|
|
414
|
+
if (name === "body") {
|
|
415
|
+
const wanted = value === null ? "moon" : value.trim().toLowerCase();
|
|
416
|
+
const next = isFlatBody(wanted) ? wanted : "moon";
|
|
417
|
+
if (next !== this.#body || this.#placesFor === "") {
|
|
418
|
+
this.#body = next;
|
|
419
|
+
this.#info = flatBody(next);
|
|
420
|
+
this.#selected = this.#cursor = this.#hover = null;
|
|
421
|
+
this.#tracks.clear();
|
|
422
|
+
this.#orbs = [];
|
|
423
|
+
this.#marks = [];
|
|
424
|
+
this.#places = [];
|
|
425
|
+
this.#placeIds = new Map();
|
|
426
|
+
this.#placesFor = "";
|
|
427
|
+
this.#image = undefined;
|
|
428
|
+
this.#imageWidth = 0;
|
|
429
|
+
this.#imageAsked = "";
|
|
430
|
+
this.#imageFailed = false;
|
|
431
|
+
this.#mapKey = "";
|
|
432
|
+
this.#view = clampView({ ...this.#view, centerLon: finite(this.getAttribute("center"), 0), centerLat: 0, zoom: 1 });
|
|
433
|
+
this.#readOrbiters();
|
|
434
|
+
this.#loadPlaces();
|
|
435
|
+
}
|
|
436
|
+
}
|
|
437
|
+
else if (name === "center") {
|
|
438
|
+
const lon = finite(value, Number.NaN);
|
|
439
|
+
if (Number.isFinite(lon))
|
|
440
|
+
this.#view = clampView({ ...this.#view, centerLon: lon });
|
|
441
|
+
this.#mapKey = "";
|
|
442
|
+
}
|
|
443
|
+
else if (name === "orbiters")
|
|
444
|
+
this.#readOrbiters();
|
|
445
|
+
else if (name === "places") {
|
|
446
|
+
this.#layers = new Set(value === null ? ["landing"] : /^(off|none|false|0)$/i.test(value.trim()) ? [] : readPlaceLayers(value));
|
|
447
|
+
this.#mapKey = "";
|
|
448
|
+
}
|
|
449
|
+
else if (name === "footprint")
|
|
450
|
+
this.#footprint = flag(value);
|
|
451
|
+
else if (name === "track") {
|
|
452
|
+
this.#track = flag(value);
|
|
453
|
+
this.#tracks.clear();
|
|
454
|
+
}
|
|
455
|
+
else if (name === "selected")
|
|
456
|
+
this.#selected = value === null || value.trim() === "" ? null : this.#find(value.trim());
|
|
457
|
+
else if (name === "paused") {
|
|
458
|
+
if (flag(value) !== this.#paused)
|
|
459
|
+
this.paused = flag(value);
|
|
460
|
+
}
|
|
461
|
+
else if (name === "time" || name === "speed")
|
|
462
|
+
this.#readClock();
|
|
463
|
+
else if (name === "shade" || name === "dot-size" || name === "names" || name === "site-flags" || name === "home-lon" || name === "home-lat")
|
|
464
|
+
this.#mapKey = "";
|
|
465
|
+
else if (name === "texture-base" || name === "texture-far-base") {
|
|
466
|
+
this.#imageAsked = "";
|
|
467
|
+
this.#imageFailed = false;
|
|
468
|
+
}
|
|
469
|
+
}
|
|
470
|
+
#readOrbiters() {
|
|
471
|
+
const value = this.getAttribute("orbiters");
|
|
472
|
+
const all = this.#info.orbiters.filter((id) => orbiterElements(id) !== undefined);
|
|
473
|
+
if (value === null || value.trim() === "" || /^(all|on|true|1)$/i.test(value.trim())) {
|
|
474
|
+
this.#showOrbiters = true;
|
|
475
|
+
this.#orbiterIds = all;
|
|
476
|
+
}
|
|
477
|
+
else if (/^(off|none|false|0)$/i.test(value.trim())) {
|
|
478
|
+
this.#showOrbiters = false;
|
|
479
|
+
this.#orbiterIds = all;
|
|
480
|
+
}
|
|
481
|
+
else {
|
|
482
|
+
const wanted = value.toLowerCase().split(/[\s,]+/).filter((one) => one !== "");
|
|
483
|
+
this.#showOrbiters = true;
|
|
484
|
+
this.#orbiterIds = all.filter((id) => wanted.includes(id));
|
|
485
|
+
}
|
|
486
|
+
this.#tracks.clear();
|
|
487
|
+
}
|
|
488
|
+
#readClock() {
|
|
489
|
+
const now = performance.now();
|
|
490
|
+
this.#clock.advance(now);
|
|
491
|
+
const mode = readTime(this.getAttribute("time"));
|
|
492
|
+
const raw = this.getAttribute("speed");
|
|
493
|
+
const speed = raw === null || raw.trim() === "" ? 1 : Number(raw);
|
|
494
|
+
this.#speed = Number.isFinite(speed) ? speed : 1;
|
|
495
|
+
this.#clock.set(mode, now);
|
|
496
|
+
this.#clock.setSpeed(this.#paused ? 0 : this.#speed, now);
|
|
497
|
+
}
|
|
498
|
+
/** The orbiter or place with this id, from what the map holds. */
|
|
499
|
+
#find(id) {
|
|
500
|
+
if (id === null)
|
|
501
|
+
return null;
|
|
502
|
+
if (this.#info.orbiters.includes(id))
|
|
503
|
+
return { kind: "orbiter", id: id };
|
|
504
|
+
// a landing site's own id (`moon/apollo-11`) is its lander's place
|
|
505
|
+
const place = LANDING_SITES.find((site) => site.id === id)?.place ?? id;
|
|
506
|
+
return this.#placeIds.has(place) || /^(moon|mars)\//.test(place) ? { kind: "place", id: place } : null;
|
|
507
|
+
}
|
|
508
|
+
#loadPlaces() {
|
|
509
|
+
const body = this.#body;
|
|
510
|
+
this.#placesFor = body;
|
|
511
|
+
const turn = (this.#placesTurn += 1);
|
|
512
|
+
void loadPlaces(body).then((list) => {
|
|
513
|
+
if (turn !== this.#placesTurn || !this.#connected)
|
|
514
|
+
return;
|
|
515
|
+
this.#places = list;
|
|
516
|
+
this.#placeIds = new Map(list.map((place) => [place.id, place]));
|
|
517
|
+
const wanted = this.getAttribute("selected");
|
|
518
|
+
if (this.#selected === null && wanted !== null && wanted.trim() !== "")
|
|
519
|
+
this.#selected = this.#find(wanted.trim());
|
|
520
|
+
this.#mapKey = "";
|
|
521
|
+
this.#words();
|
|
522
|
+
this.#request();
|
|
523
|
+
}, () => undefined);
|
|
524
|
+
}
|
|
525
|
+
// --- the clock -------------------------------------------------------------------------------------------------------------------------------
|
|
526
|
+
/** The clock is running and the element is in sight: it asks for frames. A held clock, a hidden tab and a view out of sight ask for none. */
|
|
527
|
+
#running() {
|
|
528
|
+
return this.#visibleNow() && this.#clock.speed !== 0 && !this.#paused;
|
|
529
|
+
}
|
|
530
|
+
/** Is the clock fast enough that the orbiters are drawn as streaks and the shading is coarser? */
|
|
531
|
+
#fast() {
|
|
532
|
+
return !this.#paused && Math.abs(this.#speed) >= FAST_SPEED;
|
|
533
|
+
}
|
|
534
|
+
/**
|
|
535
|
+
* How often a running clock draws: as often as an orbiter (about 0.08 degree of the map a second) moves half a pixel, up to every frame, and at least once a second; once a second at most for a reader who asked for reduced motion.
|
|
536
|
+
* With no orbiter to show, the pace of the night's edge (Mars turns 0.004 degree a second, the Moon's sunrise creeps along at 0.0001).
|
|
537
|
+
*/
|
|
538
|
+
#rate() {
|
|
539
|
+
const k = pixelsPerDegree(this.#view.width > 0 ? this.#view : { ...this.#view, width: 600, height: 300 });
|
|
540
|
+
const dots = this.#showOrbiters && this.#orbiterIds.length > 0;
|
|
541
|
+
let perSecond = ((dots ? 0.08 : this.#body === "mars" ? 0.0042 : 0.00015) * Math.abs(this.#speed) * k) / 0.5;
|
|
542
|
+
perSecond = Math.min(60, Math.max(dots ? 1 : 1 / 30, perSecond));
|
|
543
|
+
if (prefersLessMotion())
|
|
544
|
+
perSecond = Math.min(perSecond, 1);
|
|
545
|
+
return perSecond;
|
|
546
|
+
}
|
|
547
|
+
/** After a draw, or when something about the clock changed: ask for the next frame in time, by a timer when frames are far apart. Nothing at all when the clock is held or out of sight. */
|
|
548
|
+
#schedule() {
|
|
549
|
+
clearTimeout(this.#timer);
|
|
550
|
+
this.#timer = undefined;
|
|
551
|
+
if (!this.#running())
|
|
552
|
+
return;
|
|
553
|
+
const wait = 1000 / this.#rate() - (performance.now() - this.#lastDraw);
|
|
554
|
+
if (wait > 14) {
|
|
555
|
+
this.#timer = setTimeout(() => {
|
|
556
|
+
this.#timer = undefined;
|
|
557
|
+
this.#request();
|
|
558
|
+
}, wait);
|
|
559
|
+
}
|
|
560
|
+
else
|
|
561
|
+
this.#request();
|
|
562
|
+
}
|
|
563
|
+
/** `wakusei-timechange`: the moment shown, at most four times a second while time runs. */
|
|
564
|
+
#announceTime(ms) {
|
|
565
|
+
const now = performance.now();
|
|
566
|
+
if (!this.#running() || now - this.#announced < TIME_EVENT_MS)
|
|
567
|
+
return;
|
|
568
|
+
this.#announced = now;
|
|
569
|
+
this.dispatchEvent(new CustomEvent("wakusei-timechange", { detail: { time: ms, running: true, paused: false }, bubbles: true, composed: true }));
|
|
570
|
+
}
|
|
571
|
+
// --- the map ---------------------------------------------------------------------------------------------------------------------------------
|
|
572
|
+
/** The width of the picture the view needs, and its address; asks for it when it is not the one held. */
|
|
573
|
+
#wantPicture() {
|
|
574
|
+
if (this.#imageFailed || this.#view.width === 0)
|
|
575
|
+
return;
|
|
576
|
+
const need = 360 * pixelsPerDegree(this.#view) * this.#dpr;
|
|
577
|
+
const width = WIDTHS.find((one) => one >= need) ?? WIDTHS[WIDTHS.length - 1];
|
|
578
|
+
const paths = texturePaths(this);
|
|
579
|
+
const url = `${width <= 1024 ? paths.textureBase() : paths.farBase()}${this.#body}-${width}.webp`;
|
|
580
|
+
// a sharper picture already held is kept; the first is asked for at once, a sharper one when the view needs it
|
|
581
|
+
if (url === this.#imageAsked || (this.#image !== undefined && this.#imageWidth >= width))
|
|
582
|
+
return;
|
|
583
|
+
this.#imageAsked = url;
|
|
584
|
+
const turn = (this.#imageTurn += 1);
|
|
585
|
+
picture(url).then((image) => {
|
|
586
|
+
if (turn !== this.#imageTurn || !this.#connected)
|
|
587
|
+
return;
|
|
588
|
+
this.#image = image;
|
|
589
|
+
this.#imageWidth = width;
|
|
590
|
+
this.#mapKey = "";
|
|
591
|
+
this.setAttribute("data-state", "ready");
|
|
592
|
+
this.#request();
|
|
593
|
+
}, () => {
|
|
594
|
+
if (turn !== this.#imageTurn)
|
|
595
|
+
return;
|
|
596
|
+
// a sharper picture that cannot be had leaves the one held; with none, the plain ground colour
|
|
597
|
+
if (this.#image === undefined) {
|
|
598
|
+
this.#imageFailed = true;
|
|
599
|
+
this.setAttribute("data-state", "ready");
|
|
600
|
+
this.#mapKey = "";
|
|
601
|
+
this.#showMessage();
|
|
602
|
+
this.#request();
|
|
603
|
+
}
|
|
604
|
+
});
|
|
605
|
+
}
|
|
606
|
+
#measure() {
|
|
607
|
+
const stage = this.#stage;
|
|
608
|
+
if (!stage)
|
|
609
|
+
return;
|
|
610
|
+
const width = stage.clientWidth;
|
|
611
|
+
const height = stage.clientHeight;
|
|
612
|
+
// the canvases stay inside 6 million pixels and 4096 a side, whatever the screen
|
|
613
|
+
const dpr = Math.max(0.5, Math.min(window.devicePixelRatio || 1, 3, width > 0 && height > 0 ? Math.sqrt(6_000_000 / (width * height)) : 3, width > 0 || height > 0 ? 4096 / Math.max(width, height) : 3));
|
|
614
|
+
if (width === this.#view.width && height === this.#view.height && dpr === this.#dpr)
|
|
615
|
+
return;
|
|
616
|
+
this.#dpr = dpr;
|
|
617
|
+
this.#view = clampView({ ...this.#view, width, height });
|
|
618
|
+
for (const canvas of [this.#mapCanvas, this.#dotCanvas]) {
|
|
619
|
+
if (!canvas)
|
|
620
|
+
continue;
|
|
621
|
+
canvas.width = Math.max(1, Math.round(width * dpr));
|
|
622
|
+
canvas.height = Math.max(1, Math.round(height * dpr));
|
|
623
|
+
}
|
|
624
|
+
this.#mapKey = "";
|
|
625
|
+
}
|
|
626
|
+
/** The place on the screen of a longitude and latitude, taking the copy of the world that falls in the box; null when none does. */
|
|
627
|
+
#screen(lon, lat, pad = 0) {
|
|
628
|
+
const v = this.#view;
|
|
629
|
+
const k = pixelsPerDegree(v);
|
|
630
|
+
const wrap = 360 * k;
|
|
631
|
+
const y = v.height / 2 - (lat - v.centerLat) * k;
|
|
632
|
+
if (y < -pad || y > v.height + pad)
|
|
633
|
+
return null;
|
|
634
|
+
let x = v.width / 2 + wrapLongitude(lon - v.centerLon) * k;
|
|
635
|
+
if (x < -pad && x + wrap <= v.width + pad)
|
|
636
|
+
x += wrap;
|
|
637
|
+
else if (x > v.width + pad && x - wrap >= -pad)
|
|
638
|
+
x -= wrap;
|
|
639
|
+
return x < -pad || x > v.width + pad ? null : { x, y };
|
|
640
|
+
}
|
|
641
|
+
#home() {
|
|
642
|
+
const lon = finite(this.getAttribute("home-lon"), Number.NaN);
|
|
643
|
+
const lat = finite(this.getAttribute("home-lat"), Number.NaN);
|
|
644
|
+
return Number.isFinite(lon) && Number.isFinite(lat) ? { lon: wrapLongitude(lon), lat } : null;
|
|
645
|
+
}
|
|
646
|
+
#measureText(text) {
|
|
647
|
+
this.#measureCtx ??= this.ownerDocument.createElement("canvas").getContext("2d");
|
|
648
|
+
const ctx = this.#measureCtx;
|
|
649
|
+
if (ctx === null)
|
|
650
|
+
return text.length * 6.5;
|
|
651
|
+
ctx.font = "600 11px system-ui, sans-serif";
|
|
652
|
+
return ctx.measureText(text).width;
|
|
653
|
+
}
|
|
654
|
+
/** The marks of the kinds on show that fall in the view, the most important first: every site, and of the named features as many as the zoom has room for. */
|
|
655
|
+
#drawMap(ms) {
|
|
656
|
+
const canvas = this.#mapCanvas;
|
|
657
|
+
const v = this.#view;
|
|
658
|
+
if (!canvas || v.width === 0)
|
|
659
|
+
return;
|
|
660
|
+
this.#wantPicture();
|
|
661
|
+
const sun = flatSun(this.#body, ms);
|
|
662
|
+
const shade = this.getAttribute("shade") !== "off";
|
|
663
|
+
const home = this.#home();
|
|
664
|
+
const lang = this.language;
|
|
665
|
+
const names = readSurfaceNames(this.getAttribute("names"));
|
|
666
|
+
// the map is drawn again only when the view, the home, the marks, the picture or the Sun's place (about a quarter of a degree) changed
|
|
667
|
+
const key = [v.width, v.height, this.#dpr, v.centerLon.toFixed(3), v.centerLat.toFixed(3), v.zoom.toFixed(3), shade ? Math.round(sun.lon * 4) : "x", shade ? Math.round(sun.lat * 4) : "x", this.#fast() ? "f" : "s", home ? `${home.lon},${home.lat}` : "", [...this.#layers].join("+"), this.#places.length, this.hasAttribute("site-flags"), names.on ? (names.limit ?? "on") : "off", lang, this.#imageWidth, this.#imageFailed].join("|");
|
|
668
|
+
if (key === this.#mapKey)
|
|
669
|
+
return;
|
|
670
|
+
this.#mapKey = key;
|
|
671
|
+
this.#counts.mapDraws += 1;
|
|
672
|
+
const ctx = canvas.getContext("2d");
|
|
673
|
+
if (!ctx)
|
|
674
|
+
return;
|
|
675
|
+
const k = pixelsPerDegree(v);
|
|
676
|
+
const wrap = 360 * k;
|
|
677
|
+
ctx.setTransform(this.#dpr, 0, 0, this.#dpr, 0, 0);
|
|
678
|
+
ctx.fillStyle = this.#info.ground;
|
|
679
|
+
ctx.fillRect(0, 0, v.width, v.height);
|
|
680
|
+
// the body's picture: 360 degrees wide, drawn again a world to the left and right wherever the box is wider than the world
|
|
681
|
+
if (this.#image) {
|
|
682
|
+
ctx.imageSmoothingEnabled = true;
|
|
683
|
+
ctx.imageSmoothingQuality = "high";
|
|
684
|
+
const left = v.width / 2 + (-180 - v.centerLon) * k;
|
|
685
|
+
const top = v.height / 2 - (90 - v.centerLat) * k;
|
|
686
|
+
for (let x = left - Math.ceil(left / wrap) * wrap; x < v.width; x += wrap)
|
|
687
|
+
ctx.drawImage(this.#image, x, top, wrap, 180 * k);
|
|
688
|
+
}
|
|
689
|
+
if (shade)
|
|
690
|
+
this.#drawNight(ctx, sun);
|
|
691
|
+
this.#drawGraticule(ctx);
|
|
692
|
+
// the Sun overhead
|
|
693
|
+
const at = projectFlat(v, sun.lon, sun.lat);
|
|
694
|
+
for (const x of [at.x, at.x - wrap, at.x + wrap]) {
|
|
695
|
+
if (x < -8 || x > v.width + 8)
|
|
696
|
+
continue;
|
|
697
|
+
ctx.beginPath();
|
|
698
|
+
ctx.arc(x, at.y, 5, 0, Math.PI * 2);
|
|
699
|
+
ctx.fillStyle = "#ffd54f";
|
|
700
|
+
ctx.fill();
|
|
701
|
+
ctx.lineWidth = 1;
|
|
702
|
+
ctx.strokeStyle = "rgba(0,0,0,.6)";
|
|
703
|
+
ctx.stroke();
|
|
704
|
+
}
|
|
705
|
+
this.#drawMarks(ctx, names, lang);
|
|
706
|
+
if (home) {
|
|
707
|
+
const place = projectFlat(v, home.lon, home.lat);
|
|
708
|
+
for (const x of [place.x, place.x - wrap, place.x + wrap]) {
|
|
709
|
+
if (x < -10 || x > v.width + 10)
|
|
710
|
+
continue;
|
|
711
|
+
ctx.save();
|
|
712
|
+
ctx.translate(x, place.y);
|
|
713
|
+
ctx.rotate(Math.PI / 4);
|
|
714
|
+
ctx.fillStyle = "#ffffff";
|
|
715
|
+
ctx.strokeStyle = "#0b1830";
|
|
716
|
+
ctx.lineWidth = 2;
|
|
717
|
+
ctx.strokeRect(-4.5, -4.5, 9, 9);
|
|
718
|
+
ctx.fillRect(-4.5, -4.5, 9, 9);
|
|
719
|
+
ctx.restore();
|
|
720
|
+
}
|
|
721
|
+
}
|
|
722
|
+
this.setAttribute("data-state", this.#image || this.#imageFailed ? "ready" : "loading");
|
|
723
|
+
}
|
|
724
|
+
/** The night side: the Sun's height at each cell of a grid laid over the view, turned into the night's colour and drawn over the picture, smoothed. Coarser cells at a fast clock. */
|
|
725
|
+
#drawNight(ctx, sun) {
|
|
726
|
+
const v = this.#view;
|
|
727
|
+
const cell = this.#fast() ? 4 : 2;
|
|
728
|
+
const columns = Math.max(1, Math.ceil(v.width / cell));
|
|
729
|
+
const rows = Math.max(1, Math.ceil(v.height / cell));
|
|
730
|
+
let shade = this.#shade;
|
|
731
|
+
if (!shade) {
|
|
732
|
+
const canvas = this.ownerDocument.createElement("canvas");
|
|
733
|
+
const own = canvas.getContext("2d");
|
|
734
|
+
if (!own)
|
|
735
|
+
return;
|
|
736
|
+
shade = this.#shade = { canvas, ctx: own, data: null };
|
|
737
|
+
}
|
|
738
|
+
if (shade.canvas.width !== columns || shade.canvas.height !== rows || shade.data === null) {
|
|
739
|
+
shade.canvas.width = columns;
|
|
740
|
+
shade.canvas.height = rows;
|
|
741
|
+
shade.data = shade.ctx.createImageData(columns, rows);
|
|
742
|
+
}
|
|
743
|
+
shadeNight(shade.data.data, columns, rows, cell, v, sun, this.#body);
|
|
744
|
+
shade.ctx.putImageData(shade.data, 0, 0);
|
|
745
|
+
ctx.imageSmoothingEnabled = true;
|
|
746
|
+
ctx.drawImage(shade.canvas, 0, 0, columns * cell, rows * cell);
|
|
747
|
+
}
|
|
748
|
+
#drawGraticule(ctx) {
|
|
749
|
+
const v = this.#view;
|
|
750
|
+
const k = pixelsPerDegree(v);
|
|
751
|
+
const wrap = 360 * k;
|
|
752
|
+
ctx.lineWidth = 1;
|
|
753
|
+
ctx.beginPath();
|
|
754
|
+
for (let lon = -180; lon < 180; lon += 30) {
|
|
755
|
+
const x0 = v.width / 2 + wrapLongitude(lon - v.centerLon) * k;
|
|
756
|
+
for (let x = x0 - Math.ceil((x0 + 1) / wrap) * wrap; x <= v.width; x += wrap) {
|
|
757
|
+
if (x < -1)
|
|
758
|
+
continue;
|
|
759
|
+
ctx.moveTo(Math.round(x) + 0.5, 0);
|
|
760
|
+
ctx.lineTo(Math.round(x) + 0.5, v.height);
|
|
761
|
+
}
|
|
762
|
+
}
|
|
763
|
+
for (let lat = -60; lat <= 60; lat += 30) {
|
|
764
|
+
const y = Math.round(v.height / 2 - (lat - v.centerLat) * k) + 0.5;
|
|
765
|
+
if (y < 0 || y > v.height)
|
|
766
|
+
continue;
|
|
767
|
+
ctx.moveTo(0, y);
|
|
768
|
+
ctx.lineTo(v.width, y);
|
|
769
|
+
}
|
|
770
|
+
ctx.strokeStyle = "rgba(170,195,230,.32)";
|
|
771
|
+
ctx.stroke();
|
|
772
|
+
}
|
|
773
|
+
/** The marks of the kinds on show: sites as shapes by kind, the named features as a small dot, and a name beside as many as there is room for. */
|
|
774
|
+
#drawMarks(ctx, names, lang) {
|
|
775
|
+
const v = this.#view;
|
|
776
|
+
this.#marks = [];
|
|
777
|
+
this.#flagSpots = [];
|
|
778
|
+
if (this.#layers.size === 0 || this.#places.length === 0) {
|
|
779
|
+
this.#syncFlags();
|
|
780
|
+
return;
|
|
781
|
+
}
|
|
782
|
+
const pad = 8;
|
|
783
|
+
const flags = this.hasAttribute("site-flags");
|
|
784
|
+
const shown = [];
|
|
785
|
+
for (const place of this.#places) {
|
|
786
|
+
const layer = layerOf(place.kind);
|
|
787
|
+
if (!this.#layers.has(layer))
|
|
788
|
+
continue;
|
|
789
|
+
const at = this.#screen(place.lon, place.lat, pad);
|
|
790
|
+
if (at === null)
|
|
791
|
+
continue;
|
|
792
|
+
shown.push({ place, layer, x: at.x, y: at.y });
|
|
793
|
+
}
|
|
794
|
+
const rank = (place) => (place.kind === "crewed" ? -3_000_000 : placeRank(place));
|
|
795
|
+
shown.sort((a, b) => rank(a.place) - rank(b.place));
|
|
796
|
+
// a feature with no room for its name is marked by a small dot once the map is zoomed in far enough, so every feature can be pressed
|
|
797
|
+
const limit = names.on ? (names.limit ?? Math.round(7 * v.zoom ** 1.7)) : 0;
|
|
798
|
+
const dotsFrom = v.zoom >= 2.5;
|
|
799
|
+
const boxes = [];
|
|
800
|
+
const hit = (a) => boxes.some((b) => a.x0 < b.x1 && a.x1 > b.x0 && a.y0 < b.y1 && a.y1 > b.y0);
|
|
801
|
+
let named = 0;
|
|
802
|
+
ctx.font = "600 11px system-ui, sans-serif";
|
|
803
|
+
ctx.textBaseline = "middle";
|
|
804
|
+
ctx.lineJoin = "round";
|
|
805
|
+
const labels = [];
|
|
806
|
+
const chosenId = this.#selected?.kind === "place" ? this.#selected.id : null;
|
|
807
|
+
for (const mark of shown) {
|
|
808
|
+
const feature = mark.layer === "feature";
|
|
809
|
+
const site = siteOf(mark.place.id);
|
|
810
|
+
// a landing site is named by its own name (Apollo 11), once, on the lander
|
|
811
|
+
const text = site ? site.name[lang] : mark.place.name[lang];
|
|
812
|
+
const flag = flags && site !== undefined ? flagCodeOf(site.country) : undefined;
|
|
813
|
+
const room = flag === undefined ? 0 : FLAG_W;
|
|
814
|
+
let label = null;
|
|
815
|
+
if (names.on && (named < limit || mark.place.id === chosenId) && (site === undefined || site.place === mark.place.id)) {
|
|
816
|
+
const width = this.#measureText(text) + room;
|
|
817
|
+
const right = { x0: mark.x + 8, y0: mark.y - 7, x1: mark.x + 8 + width, y1: mark.y + 7, x: mark.x + 8 + room, align: "left", flagX: mark.x + 8 };
|
|
818
|
+
const left = { x0: mark.x - 8 - width, y0: mark.y - 7, x1: mark.x - 8, y1: mark.y + 7, x: mark.x - 8 - room, align: "right", flagX: mark.x - 8 - FLAG_W + 3 };
|
|
819
|
+
const fits = (b) => b.x0 >= 0 && b.x1 <= v.width && !hit(b);
|
|
820
|
+
label = fits(right) ? right : fits(left) ? left : null;
|
|
821
|
+
}
|
|
822
|
+
// a feature is marked by its name, or by a dot when the map is zoomed in
|
|
823
|
+
if (feature && label === null && !dotsFrom && mark.place.id !== chosenId)
|
|
824
|
+
continue;
|
|
825
|
+
if (label !== null) {
|
|
826
|
+
boxes.push(label);
|
|
827
|
+
labels.push({ text, x: label.x, y: mark.y, align: label.align });
|
|
828
|
+
if (flag !== undefined)
|
|
829
|
+
this.#flagSpots.push({ x: label.flagX, y: mark.y, code: flag });
|
|
830
|
+
named += 1;
|
|
831
|
+
}
|
|
832
|
+
boxes.push({ x0: mark.x - 5, y0: mark.y - 5, x1: mark.x + 5, y1: mark.y + 5 });
|
|
833
|
+
this.#marks.push(mark);
|
|
834
|
+
this.#mark(ctx, mark);
|
|
835
|
+
}
|
|
836
|
+
ctx.lineWidth = 3;
|
|
837
|
+
ctx.strokeStyle = "rgba(0,0,0,.72)";
|
|
838
|
+
ctx.fillStyle = "#fff";
|
|
839
|
+
for (const label of labels) {
|
|
840
|
+
ctx.textAlign = label.align;
|
|
841
|
+
ctx.strokeText(label.text, label.x, label.y);
|
|
842
|
+
ctx.fillText(label.text, label.x, label.y);
|
|
843
|
+
}
|
|
844
|
+
this.#syncFlags();
|
|
845
|
+
}
|
|
846
|
+
/** The flags beside the names of the landing sites: `<hata-flag>` elements (from `@johnmorrisdotca/hata`, which the page imports; where it is not defined they draw nothing and the name stands alone), moved to the spots the map drew, each made once and kept. */
|
|
847
|
+
#syncFlags() {
|
|
848
|
+
const layer = this.#flagLayer;
|
|
849
|
+
if (!layer)
|
|
850
|
+
return;
|
|
851
|
+
const spots = this.#flagSpots;
|
|
852
|
+
layer.hidden = spots.length === 0;
|
|
853
|
+
while (layer.children.length > spots.length)
|
|
854
|
+
layer.lastElementChild.remove();
|
|
855
|
+
spots.forEach((spot, i) => {
|
|
856
|
+
let flag = layer.children[i];
|
|
857
|
+
if (!flag) {
|
|
858
|
+
flag = this.ownerDocument.createElement("hata-flag");
|
|
859
|
+
flag.className = "flag";
|
|
860
|
+
flag.setAttribute("border", "");
|
|
861
|
+
flag.setAttribute("aria-hidden", "true");
|
|
862
|
+
layer.append(flag);
|
|
863
|
+
}
|
|
864
|
+
if (flag.getAttribute("code") !== spot.code)
|
|
865
|
+
flag.setAttribute("code", spot.code);
|
|
866
|
+
flag.style.transform = `translate(${spot.x.toFixed(1)}px,${(spot.y - 5.5).toFixed(1)}px)`;
|
|
867
|
+
});
|
|
868
|
+
}
|
|
869
|
+
/** One mark: a diamond for a site (gold for a crewed one, white for a lander, outlined for a rover), a cross for a crash or an impact, a dashed ring for a planned landing and a small dot for a named feature. */
|
|
870
|
+
#mark(ctx, mark) {
|
|
871
|
+
const { x, y, place } = mark;
|
|
872
|
+
ctx.save();
|
|
873
|
+
ctx.translate(x, y);
|
|
874
|
+
ctx.lineWidth = 1.5;
|
|
875
|
+
ctx.strokeStyle = "rgba(0,0,0,.85)";
|
|
876
|
+
const kind = place.kind;
|
|
877
|
+
if (kind === "crewed" || kind === "landing" || kind === "rover") {
|
|
878
|
+
ctx.rotate(Math.PI / 4);
|
|
879
|
+
const s = kind === "crewed" ? 4.2 : kind === "landing" ? 3.6 : 3;
|
|
880
|
+
ctx.fillStyle = kind === "crewed" ? "#ffd23f" : kind === "landing" ? "#ffffff" : "#6ee7a0";
|
|
881
|
+
ctx.strokeRect(-s, -s, s * 2, s * 2);
|
|
882
|
+
ctx.fillRect(-s, -s, s * 2, s * 2);
|
|
883
|
+
}
|
|
884
|
+
else if (kind === "crash" || kind === "impact" || kind === "entry") {
|
|
885
|
+
ctx.strokeStyle = "rgba(0,0,0,.85)";
|
|
886
|
+
ctx.lineWidth = 3.4;
|
|
887
|
+
ctx.beginPath();
|
|
888
|
+
ctx.moveTo(-3.5, -3.5);
|
|
889
|
+
ctx.lineTo(3.5, 3.5);
|
|
890
|
+
ctx.moveTo(3.5, -3.5);
|
|
891
|
+
ctx.lineTo(-3.5, 3.5);
|
|
892
|
+
ctx.stroke();
|
|
893
|
+
ctx.strokeStyle = "#ff8a65";
|
|
894
|
+
ctx.lineWidth = 1.7;
|
|
895
|
+
ctx.stroke();
|
|
896
|
+
}
|
|
897
|
+
else if (kind === "planned") {
|
|
898
|
+
ctx.setLineDash([2, 2]);
|
|
899
|
+
ctx.strokeStyle = "rgba(0,0,0,.85)";
|
|
900
|
+
ctx.lineWidth = 3.4;
|
|
901
|
+
ctx.beginPath();
|
|
902
|
+
ctx.arc(0, 0, 4.5, 0, Math.PI * 2);
|
|
903
|
+
ctx.stroke();
|
|
904
|
+
ctx.strokeStyle = "#b9a7ff";
|
|
905
|
+
ctx.lineWidth = 1.7;
|
|
906
|
+
ctx.stroke();
|
|
907
|
+
}
|
|
908
|
+
else {
|
|
909
|
+
ctx.beginPath();
|
|
910
|
+
ctx.arc(0, 0, 2.2, 0, Math.PI * 2);
|
|
911
|
+
ctx.fillStyle = "#fff";
|
|
912
|
+
ctx.fill();
|
|
913
|
+
ctx.lineWidth = 1;
|
|
914
|
+
ctx.stroke();
|
|
915
|
+
}
|
|
916
|
+
ctx.restore();
|
|
917
|
+
}
|
|
918
|
+
// --- the orbiters ----------------------------------------------------------------------------------------------------------------------------
|
|
919
|
+
#draw() {
|
|
920
|
+
const started = performance.now();
|
|
921
|
+
const canvas = this.#dotCanvas;
|
|
922
|
+
const v = this.#view;
|
|
923
|
+
if (!canvas || v.width === 0 || v.height === 0)
|
|
924
|
+
return;
|
|
925
|
+
const ms = this.#clock.advance(started);
|
|
926
|
+
this.#drawMap(ms);
|
|
927
|
+
const ctx = canvas.getContext("2d");
|
|
928
|
+
if (!ctx)
|
|
929
|
+
return;
|
|
930
|
+
ctx.setTransform(this.#dpr, 0, 0, this.#dpr, 0, 0);
|
|
931
|
+
ctx.clearRect(0, 0, v.width, v.height);
|
|
932
|
+
const k = pixelsPerDegree(v);
|
|
933
|
+
const size = finite(this.getAttribute("dot-size"), 0);
|
|
934
|
+
const radius = (size > 0 ? size / 2 : 4) * Math.min(1.8, 0.85 + v.zoom * 0.15);
|
|
935
|
+
const streak = this.#fast();
|
|
936
|
+
const lag = streak ? this.#speed * 1000 * STREAK_SECONDS : 0;
|
|
937
|
+
// where each orbiter is over the ground now: exact for any time, so a fast clock never makes a dot lag or jump out of its orbit
|
|
938
|
+
const orbs = [];
|
|
939
|
+
const behind = [];
|
|
940
|
+
if (this.#showOrbiters) {
|
|
941
|
+
for (const id of this.#orbiterIds) {
|
|
942
|
+
const state = flatOrbiter(id, ms);
|
|
943
|
+
if (state === undefined)
|
|
944
|
+
continue;
|
|
945
|
+
const at = this.#screen(state.lon, state.lat, radius + 1);
|
|
946
|
+
orbs.push({ ...state, x: at?.x ?? 0, y: at?.y ?? 0, on: at !== null, colour: orbiterColour(id) });
|
|
947
|
+
let tail = null;
|
|
948
|
+
if (streak && at !== null) {
|
|
949
|
+
// where it was a moment ago on the same turn of the map, shortened to the streak's longest
|
|
950
|
+
const before = flatOrbiter(id, ms - lag);
|
|
951
|
+
if (before) {
|
|
952
|
+
const x0 = at.x + wrapLongitude(before.lon - state.lon) * k;
|
|
953
|
+
const y0 = v.height / 2 - (before.lat - v.centerLat) * k;
|
|
954
|
+
const d = Math.hypot(x0 - at.x, y0 - at.y);
|
|
955
|
+
const f = d > STREAK_PX ? STREAK_PX / d : 1;
|
|
956
|
+
tail = d > 3 ? { x: at.x + (x0 - at.x) * f, y: at.y + (y0 - at.y) * f } : null;
|
|
957
|
+
}
|
|
958
|
+
}
|
|
959
|
+
behind.push(tail);
|
|
960
|
+
}
|
|
961
|
+
}
|
|
962
|
+
this.#orbs = orbs;
|
|
963
|
+
const chosen = this.#selected?.kind === "orbiter" ? orbs.find((o) => o.id === this.#selected.id && o.on) : undefined;
|
|
964
|
+
this.#drawOver(ctx, orbs, chosen, ms);
|
|
965
|
+
if (streak) {
|
|
966
|
+
ctx.lineCap = "butt";
|
|
967
|
+
ctx.lineWidth = Math.max(1.5, radius * 1.2);
|
|
968
|
+
ctx.globalAlpha = 0.7;
|
|
969
|
+
orbs.forEach((o, i) => {
|
|
970
|
+
const tail = behind[i];
|
|
971
|
+
if (!o.on || !tail)
|
|
972
|
+
return;
|
|
973
|
+
ctx.strokeStyle = o.colour;
|
|
974
|
+
ctx.beginPath();
|
|
975
|
+
ctx.moveTo(tail.x, tail.y);
|
|
976
|
+
ctx.lineTo(o.x, o.y);
|
|
977
|
+
ctx.stroke();
|
|
978
|
+
});
|
|
979
|
+
ctx.globalAlpha = 1;
|
|
980
|
+
}
|
|
981
|
+
let dots = 0;
|
|
982
|
+
for (const o of orbs) {
|
|
983
|
+
if (!o.on)
|
|
984
|
+
continue;
|
|
985
|
+
dots += 1;
|
|
986
|
+
ctx.beginPath();
|
|
987
|
+
ctx.arc(o.x, o.y, radius, 0, Math.PI * 2);
|
|
988
|
+
ctx.lineWidth = 1.6;
|
|
989
|
+
ctx.strokeStyle = "rgba(0,0,0,.8)";
|
|
990
|
+
if (o.dim) {
|
|
991
|
+
// a position that is only approximate: a ring, not a solid dot
|
|
992
|
+
ctx.stroke();
|
|
993
|
+
ctx.beginPath();
|
|
994
|
+
ctx.arc(o.x, o.y, radius, 0, Math.PI * 2);
|
|
995
|
+
ctx.strokeStyle = o.colour;
|
|
996
|
+
ctx.lineWidth = 1.6;
|
|
997
|
+
ctx.stroke();
|
|
998
|
+
ctx.globalAlpha = 0.35;
|
|
999
|
+
ctx.fillStyle = o.colour;
|
|
1000
|
+
ctx.fill();
|
|
1001
|
+
ctx.globalAlpha = 1;
|
|
1002
|
+
}
|
|
1003
|
+
else {
|
|
1004
|
+
ctx.fillStyle = o.colour;
|
|
1005
|
+
ctx.fill();
|
|
1006
|
+
ctx.stroke();
|
|
1007
|
+
}
|
|
1008
|
+
}
|
|
1009
|
+
this.#counts.draws += 1;
|
|
1010
|
+
this.#counts.dots = dots;
|
|
1011
|
+
this.#placeMarks();
|
|
1012
|
+
this.#showMessage();
|
|
1013
|
+
const took = performance.now() - started;
|
|
1014
|
+
this.#lastDraw = started;
|
|
1015
|
+
this.#totalMs += took;
|
|
1016
|
+
this.#counts.lastMs = took;
|
|
1017
|
+
if (took > this.#counts.maxMs)
|
|
1018
|
+
this.#counts.maxMs = took;
|
|
1019
|
+
this.#announceView();
|
|
1020
|
+
this.#announceTime(ms);
|
|
1021
|
+
this.#schedule();
|
|
1022
|
+
}
|
|
1023
|
+
/** The coverage circles and the ground tracks, drawn before the dots so the dots lie on them. */
|
|
1024
|
+
#drawOver(ctx, orbs, chosen, ms) {
|
|
1025
|
+
const v = this.#view;
|
|
1026
|
+
const k = pixelsPerDegree(v);
|
|
1027
|
+
const wrap = 360 * k;
|
|
1028
|
+
if (this.#footprint) {
|
|
1029
|
+
const elevation = finite(this.getAttribute("footprint-elevation"), 10);
|
|
1030
|
+
for (const o of chosen ? [chosen] : orbs) {
|
|
1031
|
+
const angle = coverageAngleRad(o.altitudeKm, this.#info.radiusKm, elevation) / RAD;
|
|
1032
|
+
if (!(angle > 0))
|
|
1033
|
+
continue;
|
|
1034
|
+
const ring = circleOutline(o.lat, o.lon, angle);
|
|
1035
|
+
const base = wrapLongitude(o.lon - v.centerLon);
|
|
1036
|
+
const xs = ring.points.map(([lon]) => v.width / 2 + (base + (lon - o.lon)) * k);
|
|
1037
|
+
const ys = ring.points.map(([, lat]) => v.height / 2 - (lat - v.centerLat) * k);
|
|
1038
|
+
const last = ring.points.length - 1;
|
|
1039
|
+
for (const shift of [-wrap, 0, wrap]) {
|
|
1040
|
+
const lo = Math.min(...xs) + shift;
|
|
1041
|
+
const hi = Math.max(...xs) + shift;
|
|
1042
|
+
if (hi < 0 || lo > v.width)
|
|
1043
|
+
continue;
|
|
1044
|
+
const outline = () => xs.forEach((x, i) => (i === 0 ? ctx.moveTo(x + shift, ys[i]) : ctx.lineTo(x + shift, ys[i])));
|
|
1045
|
+
ctx.beginPath();
|
|
1046
|
+
outline();
|
|
1047
|
+
if (ring.pole !== 0) {
|
|
1048
|
+
// a circle that holds a pole is filled down to the map's edge; only its outline is stroked
|
|
1049
|
+
const edge = v.height / 2 - (ring.pole * 90 - v.centerLat) * k;
|
|
1050
|
+
ctx.lineTo(xs[last] + shift, edge);
|
|
1051
|
+
ctx.lineTo(xs[0] + shift, edge);
|
|
1052
|
+
}
|
|
1053
|
+
ctx.closePath();
|
|
1054
|
+
ctx.fillStyle = "rgba(255,255,255,.10)";
|
|
1055
|
+
ctx.fill();
|
|
1056
|
+
if (ring.pole !== 0) {
|
|
1057
|
+
ctx.beginPath();
|
|
1058
|
+
outline();
|
|
1059
|
+
}
|
|
1060
|
+
ctx.lineWidth = 3;
|
|
1061
|
+
ctx.strokeStyle = "rgba(0,0,0,.35)";
|
|
1062
|
+
ctx.stroke();
|
|
1063
|
+
ctx.lineWidth = 1.2;
|
|
1064
|
+
ctx.strokeStyle = "rgba(255,255,255,.75)";
|
|
1065
|
+
ctx.stroke();
|
|
1066
|
+
}
|
|
1067
|
+
}
|
|
1068
|
+
}
|
|
1069
|
+
if (this.#track || chosen) {
|
|
1070
|
+
for (const o of this.#track ? orbs : chosen ? [chosen] : []) {
|
|
1071
|
+
const lines = this.#trackOf(o.id, ms);
|
|
1072
|
+
const strong = chosen !== undefined && o.id === chosen.id;
|
|
1073
|
+
const draw = (pieces, dash) => {
|
|
1074
|
+
if (pieces.length === 0)
|
|
1075
|
+
return;
|
|
1076
|
+
for (const pass of strong ? [0, 1] : [1]) {
|
|
1077
|
+
ctx.beginPath();
|
|
1078
|
+
for (const [a, b] of pieces) {
|
|
1079
|
+
const xa = v.width / 2 + wrapLongitude(a.lon - v.centerLon) * k;
|
|
1080
|
+
const xb = xa + (b.lon - a.lon) * k;
|
|
1081
|
+
const ya = v.height / 2 - (a.lat - v.centerLat) * k;
|
|
1082
|
+
const yb = v.height / 2 - (b.lat - v.centerLat) * k;
|
|
1083
|
+
for (const shift of [-wrap, 0, wrap]) {
|
|
1084
|
+
if (Math.max(xa, xb) + shift < -2 || Math.min(xa, xb) + shift > v.width + 2)
|
|
1085
|
+
continue;
|
|
1086
|
+
ctx.moveTo(xa + shift, ya);
|
|
1087
|
+
ctx.lineTo(xb + shift, yb);
|
|
1088
|
+
}
|
|
1089
|
+
}
|
|
1090
|
+
ctx.setLineDash(dash ? [4, 4] : []);
|
|
1091
|
+
ctx.lineWidth = pass === 0 ? 4 : strong ? 1.8 : 1.2;
|
|
1092
|
+
ctx.strokeStyle = pass === 0 ? "rgba(0,0,0,.55)" : strong ? "#ffffff" : o.colour;
|
|
1093
|
+
ctx.globalAlpha = strong || pass === 0 ? 1 : dash ? 0.5 : 0.85;
|
|
1094
|
+
ctx.stroke();
|
|
1095
|
+
}
|
|
1096
|
+
};
|
|
1097
|
+
draw(lines.past, true);
|
|
1098
|
+
draw(lines.future, false);
|
|
1099
|
+
ctx.setLineDash([]);
|
|
1100
|
+
ctx.globalAlpha = 1;
|
|
1101
|
+
}
|
|
1102
|
+
}
|
|
1103
|
+
}
|
|
1104
|
+
/** An orbiter's ground track for one trip round, half behind it and half ahead, worked out again when the clock has moved a fortieth of a trip (and, at a fast clock, a real second). */
|
|
1105
|
+
#trackOf(id, ms) {
|
|
1106
|
+
const days = orbiterElements(id)?.periodDays ?? 0;
|
|
1107
|
+
const every = Math.max((days * 86_400_000) / 40, this.#fast() ? Math.abs(this.#speed) * 1000 : 0);
|
|
1108
|
+
const key = `${Math.round(ms / every)}`;
|
|
1109
|
+
const held = this.#tracks.get(id);
|
|
1110
|
+
if (held && held.key === key)
|
|
1111
|
+
return held;
|
|
1112
|
+
const points = flatTrack(id, ms);
|
|
1113
|
+
const next = { key, past: trackPieces(points.filter((p) => p.ms <= ms)), future: trackPieces(points.filter((p) => p.ms >= ms)) };
|
|
1114
|
+
this.#tracks.set(id, next);
|
|
1115
|
+
return next;
|
|
1116
|
+
}
|
|
1117
|
+
#announceView() {
|
|
1118
|
+
const v = this.#view;
|
|
1119
|
+
const key = `${v.centerLon.toFixed(2)},${v.centerLat.toFixed(2)},${v.zoom.toFixed(2)}`;
|
|
1120
|
+
if (key === this.#viewSent)
|
|
1121
|
+
return;
|
|
1122
|
+
const first = this.#viewSent === "";
|
|
1123
|
+
this.#viewSent = key;
|
|
1124
|
+
if (!first)
|
|
1125
|
+
this.dispatchEvent(new CustomEvent("wakusei-flat-view", { detail: { center: v.centerLon, zoom: v.zoom }, bubbles: true, composed: true }));
|
|
1126
|
+
}
|
|
1127
|
+
// --- the marks, the words and the controls ---------------------------------------------------------------------------------------------------
|
|
1128
|
+
#at(item) {
|
|
1129
|
+
if (item === null)
|
|
1130
|
+
return null;
|
|
1131
|
+
if (item.kind === "orbiter") {
|
|
1132
|
+
const o = this.#orbs.find((one) => one.id === item.id);
|
|
1133
|
+
return o?.on ? { x: o.x, y: o.y } : null;
|
|
1134
|
+
}
|
|
1135
|
+
const mark = this.#marks.find((one) => one.place.id === item.id);
|
|
1136
|
+
return mark ? { x: mark.x, y: mark.y } : null;
|
|
1137
|
+
}
|
|
1138
|
+
/** The rings round the chosen and the hovered orbiter or place, and the name beside the one pointed at: a few DOM nodes moved, so they never ask for a draw of the dots. */
|
|
1139
|
+
#placeMarks() {
|
|
1140
|
+
const mark = (ring, item) => {
|
|
1141
|
+
if (!ring)
|
|
1142
|
+
return null;
|
|
1143
|
+
const at = this.#at(item);
|
|
1144
|
+
if (!at) {
|
|
1145
|
+
ring.hidden = true;
|
|
1146
|
+
return null;
|
|
1147
|
+
}
|
|
1148
|
+
ring.hidden = false;
|
|
1149
|
+
ring.style.transform = `translate(${at.x.toFixed(1)}px,${at.y.toFixed(1)}px)`;
|
|
1150
|
+
return at;
|
|
1151
|
+
};
|
|
1152
|
+
const same = (a, b) => a === b || (a !== null && b !== null && a.kind === b.kind && a.id === b.id);
|
|
1153
|
+
const picked = this.#cursor ?? this.#selected;
|
|
1154
|
+
const at = mark(this.#selRing, picked);
|
|
1155
|
+
const over = mark(this.#hovRing, this.#hover && !same(this.#hover, picked) ? this.#hover : null);
|
|
1156
|
+
const tip = this.#tip;
|
|
1157
|
+
if (!tip)
|
|
1158
|
+
return;
|
|
1159
|
+
const item = this.#hover ?? picked;
|
|
1160
|
+
const where = this.#hover ? (over ?? at) : at;
|
|
1161
|
+
if (!item || !where) {
|
|
1162
|
+
tip.hidden = true;
|
|
1163
|
+
return;
|
|
1164
|
+
}
|
|
1165
|
+
tip.hidden = false;
|
|
1166
|
+
// the words, and the flag of a landing site's country when the page asks for flags; made again only when they change
|
|
1167
|
+
const words = this.#describe(item);
|
|
1168
|
+
const code = item.kind === "place" && this.hasAttribute("site-flags") ? flagCodeOf(siteOf(item.id)?.country) : undefined;
|
|
1169
|
+
const key = `${code ?? ""}|${words}`;
|
|
1170
|
+
if (tip.dataset["key"] !== key) {
|
|
1171
|
+
tip.dataset["key"] = key;
|
|
1172
|
+
if (code === undefined)
|
|
1173
|
+
tip.textContent = words;
|
|
1174
|
+
else {
|
|
1175
|
+
const flag = this.ownerDocument.createElement("hata-flag");
|
|
1176
|
+
flag.className = "flag";
|
|
1177
|
+
flag.setAttribute("code", code);
|
|
1178
|
+
flag.setAttribute("border", "");
|
|
1179
|
+
flag.setAttribute("aria-hidden", "true");
|
|
1180
|
+
tip.replaceChildren(flag, words);
|
|
1181
|
+
}
|
|
1182
|
+
}
|
|
1183
|
+
const wide = tip.offsetWidth;
|
|
1184
|
+
const left = where.x + 14 + wide > this.#view.width ? Math.max(0, where.x - 14 - wide) : where.x + 14;
|
|
1185
|
+
const top = Math.max(0, Math.min(this.#view.height - tip.offsetHeight, where.y - 10));
|
|
1186
|
+
tip.style.transform = `translate(${left.toFixed(1)}px,${top.toFixed(1)}px)`;
|
|
1187
|
+
}
|
|
1188
|
+
#nameOfBody(lang) {
|
|
1189
|
+
return BODIES[this.#body].name[lang];
|
|
1190
|
+
}
|
|
1191
|
+
#describe(item) {
|
|
1192
|
+
const lang = this.language;
|
|
1193
|
+
if (item.kind === "orbiter") {
|
|
1194
|
+
const o = this.#orbs.find((one) => one.id === item.id);
|
|
1195
|
+
return say(lang, "announceOrbiter", { name: orbiterName(item.id, lang), body: this.#nameOfBody(lang), altitude: o ? Math.round(o.altitudeKm).toLocaleString(lang) : "", note: o?.dim ? say(lang, "approximate") : "" });
|
|
1196
|
+
}
|
|
1197
|
+
const place = this.#placeIds.get(item.id);
|
|
1198
|
+
if (!place)
|
|
1199
|
+
return item.id;
|
|
1200
|
+
return say(lang, "announcePlace", { name: place.name[lang], kind: say(lang, `kind_${place.kind}`), body: this.#nameOfBody(lang), lat: coordinateText(lang, "lat", place.lat), lon: coordinateText(lang, "lon", place.lon) });
|
|
1201
|
+
}
|
|
1202
|
+
#showMessage() {
|
|
1203
|
+
const msg = this.#msg;
|
|
1204
|
+
if (!msg)
|
|
1205
|
+
return;
|
|
1206
|
+
msg.textContent = this.#imageFailed ? say(this.language, "mapMissing") : "";
|
|
1207
|
+
}
|
|
1208
|
+
#build(root) {
|
|
1209
|
+
const d = root.ownerDocument;
|
|
1210
|
+
const bar = Object.assign(d.createElement("div"), { className: "bar" });
|
|
1211
|
+
bar.addEventListener("click", this.#onBar);
|
|
1212
|
+
const stage = Object.assign(d.createElement("div"), { className: "stage" });
|
|
1213
|
+
const map = Object.assign(d.createElement("canvas"), { className: "map" });
|
|
1214
|
+
map.setAttribute("aria-hidden", "true");
|
|
1215
|
+
const dots = Object.assign(d.createElement("canvas"), { className: "dots", tabIndex: 0 });
|
|
1216
|
+
dots.setAttribute("role", "application");
|
|
1217
|
+
const selRing = Object.assign(d.createElement("div"), { className: "ring sel", hidden: true });
|
|
1218
|
+
const hovRing = Object.assign(d.createElement("div"), { className: "ring hov", hidden: true });
|
|
1219
|
+
const tip = Object.assign(d.createElement("div"), { className: "tip", hidden: true });
|
|
1220
|
+
tip.setAttribute("aria-hidden", "true");
|
|
1221
|
+
const msg = Object.assign(d.createElement("p"), { className: "msg" });
|
|
1222
|
+
msg.setAttribute("role", "status");
|
|
1223
|
+
const live = Object.assign(d.createElement("p"), { className: "sr", id: "live" });
|
|
1224
|
+
live.setAttribute("role", "status");
|
|
1225
|
+
live.setAttribute("aria-live", "polite");
|
|
1226
|
+
const help = Object.assign(d.createElement("p"), { className: "sr", id: "help" });
|
|
1227
|
+
dots.setAttribute("aria-describedby", "help live");
|
|
1228
|
+
const flagLayer = Object.assign(d.createElement("div"), { className: "flags", hidden: true });
|
|
1229
|
+
stage.append(map, flagLayer, dots, selRing, hovRing, tip, msg);
|
|
1230
|
+
this.#flagLayer = flagLayer;
|
|
1231
|
+
root.append(bar, stage, live, help);
|
|
1232
|
+
this.#bar = bar;
|
|
1233
|
+
this.#stage = stage;
|
|
1234
|
+
this.#mapCanvas = map;
|
|
1235
|
+
this.#dotCanvas = dots;
|
|
1236
|
+
this.#selRing = selRing;
|
|
1237
|
+
this.#hovRing = hovRing;
|
|
1238
|
+
this.#tip = tip;
|
|
1239
|
+
this.#msg = msg;
|
|
1240
|
+
this.#live = live;
|
|
1241
|
+
dots.addEventListener("pointerdown", this.#onDown);
|
|
1242
|
+
dots.addEventListener("pointermove", this.#onMove);
|
|
1243
|
+
dots.addEventListener("pointerup", this.#onUp);
|
|
1244
|
+
dots.addEventListener("pointercancel", this.#onCancel);
|
|
1245
|
+
dots.addEventListener("pointerleave", this.#onLeave);
|
|
1246
|
+
dots.addEventListener("wheel", this.#onWheel, { passive: false });
|
|
1247
|
+
dots.addEventListener("keydown", this.#onKey);
|
|
1248
|
+
dots.addEventListener("blur", () => {
|
|
1249
|
+
if (this.#cursor) {
|
|
1250
|
+
this.#cursor = null;
|
|
1251
|
+
this.#placeMarks();
|
|
1252
|
+
}
|
|
1253
|
+
});
|
|
1254
|
+
}
|
|
1255
|
+
/** Which controls the `controls` attribute asks for. */
|
|
1256
|
+
#wants() {
|
|
1257
|
+
const raw = (this.getAttribute("controls") ?? "").trim().toLowerCase();
|
|
1258
|
+
if (raw === "none" || raw === "off")
|
|
1259
|
+
return new Set();
|
|
1260
|
+
const all = ["centre", "zoom", "footprint", "track", "chips"];
|
|
1261
|
+
if (raw === "" || raw === "all")
|
|
1262
|
+
return new Set(all);
|
|
1263
|
+
return new Set(raw.split(/[\s,]+/).map((one) => (one === "center" ? "centre" : one)));
|
|
1264
|
+
}
|
|
1265
|
+
/** The words the map and the buttons use, made again when the language, the body, the places or the home change. */
|
|
1266
|
+
#words() {
|
|
1267
|
+
const bar = this.#bar;
|
|
1268
|
+
const dots = this.#dotCanvas;
|
|
1269
|
+
if (!bar || !dots)
|
|
1270
|
+
return;
|
|
1271
|
+
const lang = this.language;
|
|
1272
|
+
const wants = this.#wants();
|
|
1273
|
+
dots.setAttribute("aria-label", say(lang, "map", { body: this.#nameOfBody(lang) }));
|
|
1274
|
+
this.shadowRoot.querySelector("#help").textContent = say(lang, "help");
|
|
1275
|
+
bar.setAttribute("role", "toolbar");
|
|
1276
|
+
bar.setAttribute("aria-label", say(lang, "controls"));
|
|
1277
|
+
const button = (act, label, extra = "") => `<button type="button" data-act="${act}"${extra}>${escape(label)}</button>`;
|
|
1278
|
+
const rows = [];
|
|
1279
|
+
const first = [];
|
|
1280
|
+
if (wants.has("centre")) {
|
|
1281
|
+
this.#info.centres.forEach((c, i) => first.push(button("centre", say(lang, c.say), ` data-lon="${c.lon}" data-i="${i}" aria-label="${escape(say(lang, "centreOn", { where: say(lang, c.say) }))}" aria-pressed="false"`)));
|
|
1282
|
+
if (Number.isFinite(finite(this.getAttribute("home-lon"), Number.NaN)))
|
|
1283
|
+
first.push(button("home", say(lang, "myPlace"), ` aria-label="${escape(say(lang, "centreHome"))}" aria-pressed="false"`));
|
|
1284
|
+
}
|
|
1285
|
+
first.push(`<span class="gap"></span>`);
|
|
1286
|
+
if (wants.has("zoom"))
|
|
1287
|
+
first.push(button("zoomout", "−", ` aria-label="${escape(say(lang, "zoomOut"))}"`), button("zoomin", "+", ` aria-label="${escape(say(lang, "zoomIn"))}"`));
|
|
1288
|
+
if (wants.has("footprint"))
|
|
1289
|
+
first.push(button("footprint", say(lang, "footprint"), ` aria-pressed="false"`));
|
|
1290
|
+
if (wants.has("track"))
|
|
1291
|
+
first.push(button("track", say(lang, "groundTrack"), ` aria-pressed="false"`));
|
|
1292
|
+
if (first.length > 1)
|
|
1293
|
+
rows.push(`<div class="row">${first.join("")}</div>`);
|
|
1294
|
+
if (wants.has("chips")) {
|
|
1295
|
+
const counts = new Map();
|
|
1296
|
+
for (const place of this.#places)
|
|
1297
|
+
counts.set(layerOf(place.kind), (counts.get(layerOf(place.kind)) ?? 0) + 1);
|
|
1298
|
+
const chip = (act, key, label, n) => `<button type="button" class="chip" data-act="layer" data-layer="${act}" aria-pressed="false"><span class="swatch" data-key="${key}"></span>${escape(label)} <span class="n">${n.toLocaleString(lang)}</span></button>`;
|
|
1299
|
+
const chips = [chip("orbiters", "orbiters", say(lang, "orbiters"), this.#info.orbiters.filter((id) => orbiterElements(id) !== undefined).length)];
|
|
1300
|
+
for (const layer of ["landing", "crash", "planned", "feature"])
|
|
1301
|
+
if ((counts.get(layer) ?? 0) > 0 || this.#places.length === 0)
|
|
1302
|
+
chips.push(chip(layer, LAYER_WORD[layer], say(lang, layer), counts.get(layer) ?? 0));
|
|
1303
|
+
rows.push(`<div class="row chips" role="group" aria-label="${escape(say(lang, "layersFilter"))}">${chips.join("")}</div>`);
|
|
1304
|
+
}
|
|
1305
|
+
bar.innerHTML = rows.join("");
|
|
1306
|
+
// colours go on through the CSS object model: an inline style attribute in the markup would be refused by a strict Content-Security-Policy
|
|
1307
|
+
const swatches = { orbiters: "#ffd23f", landing: "#ffffff", crash: "#ff8a65", planned: "#b9a7ff", feature: "#9aa5b1" };
|
|
1308
|
+
bar.querySelectorAll(".swatch").forEach((swatch) => swatch.style.setProperty("--c", swatches[swatch.dataset["key"] ?? ""] ?? "#9aa5b1"));
|
|
1309
|
+
this.#sync();
|
|
1310
|
+
}
|
|
1311
|
+
/** Bring the pressed states of the buttons and chips up to date without making them again (a reader may be using one). */
|
|
1312
|
+
#sync() {
|
|
1313
|
+
const bar = this.#bar;
|
|
1314
|
+
if (!bar)
|
|
1315
|
+
return;
|
|
1316
|
+
const home = finite(this.getAttribute("home-lon"), Number.NaN);
|
|
1317
|
+
bar.querySelectorAll('[data-act="centre"]').forEach((b) => b.setAttribute("aria-pressed", String(Math.abs(wrapLongitude(this.#view.centerLon - Number(b.dataset["lon"]))) < 0.5)));
|
|
1318
|
+
bar.querySelectorAll('[data-act="home"]').forEach((b) => b.setAttribute("aria-pressed", String(Number.isFinite(home) && Math.abs(wrapLongitude(this.#view.centerLon - home)) < 0.5)));
|
|
1319
|
+
bar.querySelectorAll('[data-act="footprint"]').forEach((b) => b.setAttribute("aria-pressed", String(this.#footprint)));
|
|
1320
|
+
bar.querySelectorAll('[data-act="track"]').forEach((b) => b.setAttribute("aria-pressed", String(this.#track)));
|
|
1321
|
+
bar.querySelectorAll('[data-act="layer"]').forEach((b) => {
|
|
1322
|
+
const layer = b.dataset["layer"];
|
|
1323
|
+
b.setAttribute("aria-pressed", String(layer === "orbiters" ? this.#showOrbiters : this.#layers.has(layer)));
|
|
1324
|
+
});
|
|
1325
|
+
bar.querySelector('[data-act="zoomin"]')?.toggleAttribute("disabled", this.#view.zoom >= MAX_ZOOM);
|
|
1326
|
+
bar.querySelector('[data-act="zoomout"]')?.toggleAttribute("disabled", this.#view.zoom <= 1);
|
|
1327
|
+
if (this.#dotCanvas)
|
|
1328
|
+
this.#dotCanvas.toggleAttribute("data-zoomed", this.#view.zoom > 1);
|
|
1329
|
+
}
|
|
1330
|
+
#onBar = (event) => {
|
|
1331
|
+
const target = event.target?.closest("button[data-act]");
|
|
1332
|
+
if (!target || target.hasAttribute("disabled"))
|
|
1333
|
+
return;
|
|
1334
|
+
const act = target.dataset["act"];
|
|
1335
|
+
const v = this.#view;
|
|
1336
|
+
if (act === "centre")
|
|
1337
|
+
this.#setView({ ...v, centerLon: Number(target.dataset["lon"]) });
|
|
1338
|
+
else if (act === "home") {
|
|
1339
|
+
const home = this.#home();
|
|
1340
|
+
this.#setView({ ...v, centerLon: finite(this.getAttribute("home-lon"), 0), centerLat: v.zoom > 1 && home ? home.lat : v.centerLat });
|
|
1341
|
+
}
|
|
1342
|
+
else if (act === "zoomin")
|
|
1343
|
+
this.#setView(zoomAbout(v, 1.6));
|
|
1344
|
+
else if (act === "zoomout")
|
|
1345
|
+
this.#setView(zoomAbout(v, 1 / 1.6));
|
|
1346
|
+
else if (act === "footprint") {
|
|
1347
|
+
this.#footprint = !this.#footprint;
|
|
1348
|
+
this.#sync();
|
|
1349
|
+
this.#request();
|
|
1350
|
+
}
|
|
1351
|
+
else if (act === "track") {
|
|
1352
|
+
this.#track = !this.#track;
|
|
1353
|
+
this.#tracks.clear();
|
|
1354
|
+
this.#sync();
|
|
1355
|
+
this.#request();
|
|
1356
|
+
}
|
|
1357
|
+
else if (act === "layer") {
|
|
1358
|
+
const layer = target.dataset["layer"];
|
|
1359
|
+
if (layer === "orbiters")
|
|
1360
|
+
this.#showOrbiters = !this.#showOrbiters;
|
|
1361
|
+
else if (this.#layers.has(layer))
|
|
1362
|
+
this.#layers.delete(layer);
|
|
1363
|
+
else
|
|
1364
|
+
this.#layers.add(layer);
|
|
1365
|
+
// a choice whose mark has just gone is let go
|
|
1366
|
+
if (this.#selected?.kind === "place" && !this.#layers.has(layerOf(this.#placeIds.get(this.#selected.id)?.kind ?? "feature")))
|
|
1367
|
+
this.#selected = null;
|
|
1368
|
+
if (this.#selected?.kind === "orbiter" && !this.#showOrbiters)
|
|
1369
|
+
this.#selected = null;
|
|
1370
|
+
this.#mapKey = "";
|
|
1371
|
+
this.#sync();
|
|
1372
|
+
this.#request();
|
|
1373
|
+
this.#schedule();
|
|
1374
|
+
this.dispatchEvent(new CustomEvent("wakusei-flat-filter", { detail: { orbiters: this.#showOrbiters, layers: [...this.#layers] }, bubbles: true, composed: true }));
|
|
1375
|
+
}
|
|
1376
|
+
};
|
|
1377
|
+
#setView(view) {
|
|
1378
|
+
this.#view = clampView(view);
|
|
1379
|
+
this.#mapKey = "";
|
|
1380
|
+
this.#sync();
|
|
1381
|
+
this.#request();
|
|
1382
|
+
}
|
|
1383
|
+
// --- drawing, asked for ----------------------------------------------------------------------------------------------------------------------
|
|
1384
|
+
/** Ask for one draw at the next frame, however many reasons there are. Nothing is asked for out of sight or in a hidden tab: the draw is made when the element comes back. */
|
|
1385
|
+
#request() {
|
|
1386
|
+
if (!this.#connected)
|
|
1387
|
+
return;
|
|
1388
|
+
if (!this.#visibleNow())
|
|
1389
|
+
return;
|
|
1390
|
+
if (this.#frame)
|
|
1391
|
+
return;
|
|
1392
|
+
this.#frame = requestAnimationFrame(() => {
|
|
1393
|
+
this.#frame = 0;
|
|
1394
|
+
this.#draw();
|
|
1395
|
+
});
|
|
1396
|
+
}
|
|
1397
|
+
// --- the pointer and the keyboard ------------------------------------------------------------------------------------------------------------
|
|
1398
|
+
#local(event) {
|
|
1399
|
+
const box = this.#dotCanvas.getBoundingClientRect();
|
|
1400
|
+
return { x: event.clientX - box.left, y: event.clientY - box.top };
|
|
1401
|
+
}
|
|
1402
|
+
/** The orbiter or mark drawn nearest to a point, within `reach` pixels (an orbiter is favoured: it is the one that moves). */
|
|
1403
|
+
#hit(x, y, reach) {
|
|
1404
|
+
let best = null;
|
|
1405
|
+
let gap = reach * reach;
|
|
1406
|
+
for (const o of this.#orbs) {
|
|
1407
|
+
if (!o.on)
|
|
1408
|
+
continue;
|
|
1409
|
+
const d = (o.x - x) ** 2 + (o.y - y) ** 2 - 12;
|
|
1410
|
+
if (d <= gap) {
|
|
1411
|
+
gap = d;
|
|
1412
|
+
best = { kind: "orbiter", id: o.id };
|
|
1413
|
+
}
|
|
1414
|
+
}
|
|
1415
|
+
for (const mark of this.#marks) {
|
|
1416
|
+
const d = (mark.x - x) ** 2 + (mark.y - y) ** 2;
|
|
1417
|
+
if (d <= gap) {
|
|
1418
|
+
gap = d;
|
|
1419
|
+
best = { kind: "place", id: mark.place.id };
|
|
1420
|
+
}
|
|
1421
|
+
}
|
|
1422
|
+
return best;
|
|
1423
|
+
}
|
|
1424
|
+
#pick(item, via) {
|
|
1425
|
+
let detail;
|
|
1426
|
+
if (item === null)
|
|
1427
|
+
detail = { kind: null, id: null, name: null, body: this.#body, lat: null, lon: null, altitudeKm: null, status: null, placeKind: null, via };
|
|
1428
|
+
else if (item.kind === "orbiter") {
|
|
1429
|
+
const o = this.#orbs.find((one) => one.id === item.id);
|
|
1430
|
+
detail = { kind: "orbiter", id: item.id, name: orbiterName(item.id, this.language), body: this.#body, lat: o?.lat ?? null, lon: o?.lon ?? null, altitudeKm: o?.altitudeKm ?? null, status: o?.status ?? null, placeKind: null, via };
|
|
1431
|
+
}
|
|
1432
|
+
else {
|
|
1433
|
+
const place = this.#placeIds.get(item.id);
|
|
1434
|
+
const site = siteOf(item.id);
|
|
1435
|
+
detail = { kind: "place", id: item.id, name: place?.name[this.language] ?? item.id, body: this.#body, lat: place?.lat ?? null, lon: place?.lon ?? null, altitudeKm: null, status: null, placeKind: place?.kind ?? null, via, ...(place ? { place } : {}), ...(site ? { site } : {}) };
|
|
1436
|
+
}
|
|
1437
|
+
this.dispatchEvent(new CustomEvent("wakusei-flat-pick", { detail, bubbles: true, composed: true }));
|
|
1438
|
+
}
|
|
1439
|
+
#choose(item, via) {
|
|
1440
|
+
const same = item !== null && this.#selected !== null && item.kind === this.#selected.kind && item.id === this.#selected.id;
|
|
1441
|
+
if (item === null && this.#selected === null)
|
|
1442
|
+
return;
|
|
1443
|
+
if (!same) {
|
|
1444
|
+
this.#selected = item;
|
|
1445
|
+
this.#request();
|
|
1446
|
+
}
|
|
1447
|
+
this.#sync();
|
|
1448
|
+
this.#placeMarks();
|
|
1449
|
+
if (item) {
|
|
1450
|
+
if (this.#live)
|
|
1451
|
+
this.#live.textContent = this.#describe(item);
|
|
1452
|
+
this.#pick(item, via);
|
|
1453
|
+
}
|
|
1454
|
+
else if (!same)
|
|
1455
|
+
this.#pick(null, via);
|
|
1456
|
+
}
|
|
1457
|
+
#onDown = (event) => {
|
|
1458
|
+
if (event.pointerType === "mouse" && event.button !== 0)
|
|
1459
|
+
return;
|
|
1460
|
+
const at = this.#local(event);
|
|
1461
|
+
this.#pointers.set(event.pointerId, at);
|
|
1462
|
+
try {
|
|
1463
|
+
this.#dotCanvas.setPointerCapture(event.pointerId);
|
|
1464
|
+
}
|
|
1465
|
+
catch {
|
|
1466
|
+
// a pointer that is already gone
|
|
1467
|
+
}
|
|
1468
|
+
this.#drag = { x: at.x, y: at.y, moved: this.#pointers.size === 2, pinch: this.#pointers.size === 2 ? this.#spread() : 0 };
|
|
1469
|
+
this.#dotCanvas.style.cursor = "grabbing";
|
|
1470
|
+
};
|
|
1471
|
+
#spread() {
|
|
1472
|
+
const [a, b] = [...this.#pointers.values()];
|
|
1473
|
+
return a && b ? Math.hypot(a.x - b.x, a.y - b.y) : 0;
|
|
1474
|
+
}
|
|
1475
|
+
#onMove = (event) => {
|
|
1476
|
+
const at = this.#local(event);
|
|
1477
|
+
if (!this.#pointers.has(event.pointerId)) {
|
|
1478
|
+
// a pointer going over the map with no button down: the one under it is named
|
|
1479
|
+
if (event.pointerType === "mouse")
|
|
1480
|
+
this.#hoverAt(at.x, at.y);
|
|
1481
|
+
return;
|
|
1482
|
+
}
|
|
1483
|
+
const before = this.#pointers.get(event.pointerId);
|
|
1484
|
+
this.#pointers.set(event.pointerId, at);
|
|
1485
|
+
const drag = this.#drag;
|
|
1486
|
+
if (!drag)
|
|
1487
|
+
return;
|
|
1488
|
+
if (this.#pointers.size >= 2) {
|
|
1489
|
+
const spread = this.#spread();
|
|
1490
|
+
const [a, b] = [...this.#pointers.values()];
|
|
1491
|
+
if (drag.pinch > 0 && spread > 0 && a && b) {
|
|
1492
|
+
this.#view = zoomAbout(this.#view, spread / drag.pinch, (a.x + b.x) / 2, (a.y + b.y) / 2);
|
|
1493
|
+
drag.pinch = spread;
|
|
1494
|
+
drag.moved = true;
|
|
1495
|
+
this.#mapKey = "";
|
|
1496
|
+
this.#sync();
|
|
1497
|
+
this.#request();
|
|
1498
|
+
}
|
|
1499
|
+
return;
|
|
1500
|
+
}
|
|
1501
|
+
if (!drag.moved && Math.hypot(at.x - drag.x, at.y - drag.y) < TAP_SLOP)
|
|
1502
|
+
return;
|
|
1503
|
+
drag.moved = true;
|
|
1504
|
+
const k = pixelsPerDegree(this.#view);
|
|
1505
|
+
this.#view = clampView({ ...this.#view, centerLon: this.#view.centerLon - (at.x - before.x) / k, centerLat: this.#view.centerLat + (at.y - before.y) / k });
|
|
1506
|
+
this.#mapKey = "";
|
|
1507
|
+
this.#hover = null;
|
|
1508
|
+
this.#sync();
|
|
1509
|
+
this.#request();
|
|
1510
|
+
};
|
|
1511
|
+
#onUp = (event) => {
|
|
1512
|
+
const at = this.#local(event);
|
|
1513
|
+
const drag = this.#drag;
|
|
1514
|
+
this.#pointers.delete(event.pointerId);
|
|
1515
|
+
try {
|
|
1516
|
+
this.#dotCanvas.releasePointerCapture(event.pointerId);
|
|
1517
|
+
}
|
|
1518
|
+
catch {
|
|
1519
|
+
// already released
|
|
1520
|
+
}
|
|
1521
|
+
if (this.#pointers.size === 0) {
|
|
1522
|
+
this.#drag = null;
|
|
1523
|
+
this.#dotCanvas.style.cursor = "";
|
|
1524
|
+
if (drag && !drag.moved) {
|
|
1525
|
+
// a tap: a finger needs more room than a mouse
|
|
1526
|
+
const hit = this.#hit(at.x, at.y, event.pointerType === "touch" ? 22 : 9);
|
|
1527
|
+
this.#hover = null;
|
|
1528
|
+
this.#choose(hit, "tap");
|
|
1529
|
+
}
|
|
1530
|
+
}
|
|
1531
|
+
else if (drag)
|
|
1532
|
+
drag.pinch = this.#spread();
|
|
1533
|
+
};
|
|
1534
|
+
#onCancel = (event) => {
|
|
1535
|
+
this.#pointers.delete(event.pointerId);
|
|
1536
|
+
if (this.#pointers.size === 0) {
|
|
1537
|
+
this.#drag = null;
|
|
1538
|
+
this.#dotCanvas.style.cursor = "";
|
|
1539
|
+
}
|
|
1540
|
+
};
|
|
1541
|
+
#onLeave = (event) => {
|
|
1542
|
+
if (event.pointerType !== "mouse" || this.#hover === null)
|
|
1543
|
+
return;
|
|
1544
|
+
this.#hover = null;
|
|
1545
|
+
this.#placeMarks();
|
|
1546
|
+
};
|
|
1547
|
+
#hoverAt(x, y) {
|
|
1548
|
+
const hit = this.#hit(x, y, 8);
|
|
1549
|
+
if ((hit === null && this.#hover === null) || (hit !== null && this.#hover !== null && hit.kind === this.#hover.kind && hit.id === this.#hover.id))
|
|
1550
|
+
return;
|
|
1551
|
+
this.#hover = hit;
|
|
1552
|
+
this.#placeMarks();
|
|
1553
|
+
if (hit)
|
|
1554
|
+
this.#pick(hit, "hover");
|
|
1555
|
+
}
|
|
1556
|
+
#onWheel = (event) => {
|
|
1557
|
+
// a plain wheel scrolls the page; Control or Command with the wheel (and a trackpad's pinch, which arrives as one) zooms
|
|
1558
|
+
if (!event.ctrlKey && !event.metaKey)
|
|
1559
|
+
return;
|
|
1560
|
+
event.preventDefault();
|
|
1561
|
+
const at = this.#local(event);
|
|
1562
|
+
this.#setView(zoomAbout(this.#view, Math.exp(-event.deltaY * 0.01), at.x, at.y));
|
|
1563
|
+
};
|
|
1564
|
+
#onKey = (event) => {
|
|
1565
|
+
const key = event.key;
|
|
1566
|
+
if (key === "+" || key === "=") {
|
|
1567
|
+
event.preventDefault();
|
|
1568
|
+
this.#setView(zoomAbout(this.#view, 1.6));
|
|
1569
|
+
return;
|
|
1570
|
+
}
|
|
1571
|
+
if (key === "-" || key === "_") {
|
|
1572
|
+
event.preventDefault();
|
|
1573
|
+
this.#setView(zoomAbout(this.#view, 1 / 1.6));
|
|
1574
|
+
return;
|
|
1575
|
+
}
|
|
1576
|
+
if (key === "Escape") {
|
|
1577
|
+
if (this.#cursor || this.#selected) {
|
|
1578
|
+
this.#cursor = null;
|
|
1579
|
+
this.#choose(null, "key");
|
|
1580
|
+
}
|
|
1581
|
+
return;
|
|
1582
|
+
}
|
|
1583
|
+
if (key === "Enter" || key === " ") {
|
|
1584
|
+
event.preventDefault();
|
|
1585
|
+
const item = this.#cursor ?? this.#selected;
|
|
1586
|
+
if (item)
|
|
1587
|
+
this.#choose(item, "key");
|
|
1588
|
+
return;
|
|
1589
|
+
}
|
|
1590
|
+
const step = key === "ArrowRight" || key === "ArrowDown" ? 1 : key === "ArrowLeft" || key === "ArrowUp" ? -1 : key === "PageDown" ? 10 : key === "PageUp" ? -10 : 0;
|
|
1591
|
+
if (step === 0 && key !== "Home" && key !== "End")
|
|
1592
|
+
return;
|
|
1593
|
+
event.preventDefault();
|
|
1594
|
+
// the orbiters and marks on show, from the left of the map to the right
|
|
1595
|
+
const list = [];
|
|
1596
|
+
for (const o of this.#orbs)
|
|
1597
|
+
if (o.on)
|
|
1598
|
+
list.push({ item: { kind: "orbiter", id: o.id }, x: o.x, y: o.y });
|
|
1599
|
+
for (const m of this.#marks)
|
|
1600
|
+
list.push({ item: { kind: "place", id: m.place.id }, x: m.x, y: m.y });
|
|
1601
|
+
if (list.length === 0)
|
|
1602
|
+
return;
|
|
1603
|
+
list.sort((a, b) => a.x - b.x || a.y - b.y);
|
|
1604
|
+
const current = this.#cursor ?? this.#selected;
|
|
1605
|
+
const at = current ? list.findIndex((one) => one.item.kind === current.kind && one.item.id === current.id) : -1;
|
|
1606
|
+
const next = key === "Home" ? 0 : key === "End" ? list.length - 1 : at < 0 ? (step > 0 ? 0 : list.length - 1) : Math.max(0, Math.min(list.length - 1, at + step));
|
|
1607
|
+
this.#cursor = list[next].item;
|
|
1608
|
+
this.#placeMarks();
|
|
1609
|
+
if (this.#live)
|
|
1610
|
+
this.#live.textContent = this.#describe(this.#cursor);
|
|
1611
|
+
this.#pick(this.#cursor, "key");
|
|
1612
|
+
};
|
|
1613
|
+
}
|
|
1614
|
+
_a = WakuseiFlat;
|