@johnmorrisdotca/wakusei-planets 0.59.0 → 0.61.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 +18 -0
- package/NOTICE.md +9 -0
- package/README.md +31 -3
- package/dist/bodies/earth.js +22 -1
- package/dist/data/places-moon.d.ts +12 -9
- package/dist/data/places-moon.js +3 -3
- package/dist/element.d.ts +1 -1
- 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/system-define.d.ts +3 -0
- package/dist/flat/system-define.js +6 -0
- package/dist/flat/system-element.d.ts +133 -0
- package/dist/flat/system-element.js +1435 -0
- package/dist/flat/system-words.d.ts +28 -0
- package/dist/flat/system-words.js +102 -0
- package/dist/flat/system.d.ts +440 -0
- package/dist/flat/system.js +521 -0
- package/dist/flat/words.d.ts +55 -0
- package/dist/flat/words.js +133 -0
- package/dist/space/travel.js +36 -5
- package/dist/surface/drive.d.ts +5 -2
- package/dist/surface/drive.js +13 -15
- package/dist/surface/element.d.ts +38 -1
- package/dist/surface/element.js +144 -14
- package/dist/surface/index.d.ts +1 -0
- package/dist/surface/index.js +1 -0
- package/dist/surface/model.d.ts +54 -0
- package/dist/surface/model.js +147 -0
- package/dist/surface/rig.d.ts +125 -0
- package/dist/surface/rig.js +107 -0
- package/dist/surface/scene.d.ts +3 -0
- package/dist/surface/scene.js +22 -1
- package/package.json +38 -2
|
@@ -0,0 +1,1435 @@
|
|
|
1
|
+
var _a;
|
|
2
|
+
// <wakusei-flat-system>: the solar system as a flat, top-down map. A canvas for the disc (the rings, the orbit lines) and a canvas for what moves: the planets, the dwarf planets, the belts as clouds of points, the comets and our spacecraft (the Tesla Roadster among them).
|
|
3
|
+
// The same family as <wakusei-flat> (the Moon and Mars): the same time, speed, paused, selected, controls, lang, dot-size and names attributes, the same `wakusei-flat-pick`, `-view`, `-filter` and `wakusei-timechange` events, the same pan, pinch and keyboard.
|
|
4
|
+
// Nothing runs on a timer when the clock is held, and nothing is drawn at rest, out of sight or in a hidden tab. This file registers nothing: `@johnmorrisdotca/wakusei-planets/flat/system/define` does.
|
|
5
|
+
import { BELT_IDS, BELTS, loadBeltBodies } from "../belts/index.js";
|
|
6
|
+
import { BELT_SAMPLE } from "../belts/totals.js";
|
|
7
|
+
import { listComets } from "../comets.js";
|
|
8
|
+
import { readSurfaceNames } from "../place-marks.js";
|
|
9
|
+
import { planRadius } from "../plan/layout.js";
|
|
10
|
+
import { createGameClock } from "../space/clock.js";
|
|
11
|
+
import { loadSpacecraft, SPACECRAFT, SPACECRAFT_IDS } from "../spacecraft.js";
|
|
12
|
+
import { MAX_ZOOM } from "./geometry.js";
|
|
13
|
+
import { SYSTEM_DWARFS, SYSTEM_LAYERS, SYSTEM_RINGS, clampSystemView, craftPath, projectSystem, radiusAu, squeezeAu, systemBelts, systemColour, systemCometOrbit, systemMarks, systemName, systemOrbit, systemReach, systemScale, unprojectSystem, zoomSystemAbout, } from "./system.js";
|
|
14
|
+
import { saySystem } from "./system-words.js";
|
|
15
|
+
const TIME_EVENT_MS = 250;
|
|
16
|
+
/** The furthest the pointer may move between going down and coming up and still be a tap, pixels. */
|
|
17
|
+
const TAP_SLOP = 5;
|
|
18
|
+
/** From this clock speed (ten days a second) a body is drawn with a short streak behind it. */
|
|
19
|
+
const STREAK_FROM = 864_000;
|
|
20
|
+
const STREAK_SECONDS = 0.25;
|
|
21
|
+
const STREAK_PX = 28;
|
|
22
|
+
/** Mercury's turn in radians a second of the map's time, which sets how often a running clock must draw. */
|
|
23
|
+
const MERCURY_RATE = (2 * Math.PI) / (87.969 * 86_400);
|
|
24
|
+
const escape = (text) => text.replace(/[&<>"]/g, (c) => ({ "&": "&", "<": "<", ">": ">", '"': """ })[c]);
|
|
25
|
+
const flag = (value) => value !== null && !/^(off|false|no|0)$/i.test(value.trim());
|
|
26
|
+
const finite = (value, fallback) => (value === null || value.trim() === "" || !Number.isFinite(Number(value)) ? fallback : Number(value));
|
|
27
|
+
const readTime = (value) => {
|
|
28
|
+
if (value === null || value.trim() === "" || value.trim().toLowerCase() === "now")
|
|
29
|
+
return "now";
|
|
30
|
+
const text = value.trim();
|
|
31
|
+
if (/^-?\d+(\.\d+)?$/.test(text))
|
|
32
|
+
return Number(text);
|
|
33
|
+
const ms = Date.parse(/[zZ]|[+-]\d\d:?\d\d$/.test(text) || !/T/.test(text) ? text : `${text}Z`);
|
|
34
|
+
return Number.isFinite(ms) ? ms : "now";
|
|
35
|
+
};
|
|
36
|
+
/** A list attribute: `undefined` for all (left out, empty or `all`), an empty list for `off`, else the words in it. */
|
|
37
|
+
const readList = (value) => {
|
|
38
|
+
if (value === null || value.trim() === "" || /^(all|on|true|1)$/i.test(value.trim()))
|
|
39
|
+
return undefined;
|
|
40
|
+
if (/^(off|none|false|0)$/i.test(value.trim()))
|
|
41
|
+
return [];
|
|
42
|
+
return value.toLowerCase().split(/[\s,]+/).filter((one) => one !== "");
|
|
43
|
+
};
|
|
44
|
+
const prefersLessMotion = () => typeof matchMedia === "function" && matchMedia("(prefers-reduced-motion: reduce)").matches;
|
|
45
|
+
const hidden = () => typeof document !== "undefined" && document.visibilityState === "hidden";
|
|
46
|
+
const CSS = `
|
|
47
|
+
:host{display:flex;flex-direction:column;gap:8px;color:inherit;font:inherit;container-type:inline-size;min-width:0}
|
|
48
|
+
:host([hidden]){display:none}
|
|
49
|
+
*{box-sizing:border-box}
|
|
50
|
+
.bar{display:grid;gap:6px}
|
|
51
|
+
.bar:empty{display:none}
|
|
52
|
+
.row{display:flex;flex-wrap:wrap;gap:6px;align-items:center}
|
|
53
|
+
.chips{flex-wrap:nowrap;overflow-x:auto;scrollbar-width:thin;padding-bottom:2px}
|
|
54
|
+
.chips>*{flex:none}
|
|
55
|
+
@container (min-width:600px){.chips{flex-wrap:wrap;overflow:visible}}
|
|
56
|
+
.gap{flex:1 1 0}
|
|
57
|
+
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}
|
|
58
|
+
button[aria-pressed="true"]{background:color-mix(in srgb,currentColor 14%,transparent);border-color:currentColor}
|
|
59
|
+
button:disabled{opacity:.45;cursor:default}
|
|
60
|
+
button:focus-visible{outline:3px solid #6cd0ff;outline-offset:2px}
|
|
61
|
+
@media (pointer:coarse){button{min-height:44px;padding:6px 14px}}
|
|
62
|
+
.chip .swatch{width:10px;height:10px;border-radius:50%;background:var(--c,#9aa5b1);flex:none}
|
|
63
|
+
.chip .n{opacity:.7;font-variant-numeric:tabular-nums;font-size:.85em}
|
|
64
|
+
.stage{position:relative;flex:1 1 auto;min-height:0;aspect-ratio:var(--wakusei-flat-aspect,3/2);overflow:hidden;border-radius:var(--wakusei-flat-radius,10px);background:var(--wakusei-flat-space,#05070f)}
|
|
65
|
+
@container (max-width:599px){.stage{aspect-ratio:var(--wakusei-flat-aspect-narrow,1/1)}}
|
|
66
|
+
canvas{position:absolute;inset:0;width:100%;height:100%;display:block}
|
|
67
|
+
.dots{outline:none;cursor:grab;touch-action:pan-y}
|
|
68
|
+
.dots[data-zoomed]{touch-action:none}
|
|
69
|
+
.dots:focus-visible{outline:3px solid #6cd0ff;outline-offset:-3px}
|
|
70
|
+
.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}
|
|
71
|
+
.ring.hov{width:14px;height:14px;margin:-7px 0 0 -7px;border-width:1px}
|
|
72
|
+
.ring[hidden],.tip[hidden]{display:none}
|
|
73
|
+
.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)}
|
|
74
|
+
.sr{position:absolute;width:1px;height:1px;overflow:hidden;clip:rect(0 0 0 0);white-space:nowrap}
|
|
75
|
+
`;
|
|
76
|
+
/** 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`. */
|
|
77
|
+
const sheets = new Map();
|
|
78
|
+
function adoptStyles(root, css, nonce) {
|
|
79
|
+
if (typeof CSSStyleSheet === "function" && "replaceSync" in CSSStyleSheet.prototype && "adoptedStyleSheets" in root) {
|
|
80
|
+
try {
|
|
81
|
+
let sheet = sheets.get(css);
|
|
82
|
+
if (!sheet) {
|
|
83
|
+
sheet = new CSSStyleSheet();
|
|
84
|
+
sheet.replaceSync(css);
|
|
85
|
+
sheets.set(css, sheet);
|
|
86
|
+
}
|
|
87
|
+
if (!root.adoptedStyleSheets.includes(sheet))
|
|
88
|
+
root.adoptedStyleSheets = [...root.adoptedStyleSheets, sheet];
|
|
89
|
+
return;
|
|
90
|
+
}
|
|
91
|
+
catch {
|
|
92
|
+
// a browser that has the API and refuses it: the <style> below
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
const style = root.ownerDocument.createElement("style");
|
|
96
|
+
if (nonce)
|
|
97
|
+
style.setAttribute("nonce", nonce);
|
|
98
|
+
style.textContent = css;
|
|
99
|
+
root.prepend(style);
|
|
100
|
+
}
|
|
101
|
+
const BELT_TOTAL_SAMPLE = BELT_IDS.reduce((sum, id) => sum + BELT_SAMPLE[id], 0);
|
|
102
|
+
const COMET_COUNT = listComets().length;
|
|
103
|
+
/**
|
|
104
|
+
* The `<wakusei-flat-system>` custom element: the solar system as a flat, top-down map in the plane of the ecliptic, the planets at their places for the time shown, with their orbits, the dwarf planets and a few small bodies, the asteroid belts and the Kuiper belt as clouds of points,
|
|
105
|
+
* 34 comets (with a tail turned from the Sun when near it), and our spacecraft, the Tesla Roadster among them, each with its whole path to draw. Import `@johnmorrisdotca/wakusei-planets/flat/system/define` to register the tag. It needs no WebGL and no peer.
|
|
106
|
+
* Distances are squeezed by the plan's logarithm (`ln(1 + d / 0.1 au)`), so Mercury and Neptune both show, with rings at 1, 5, 10, 30 and 100 au, or `spacing="true"` for the real proportion. A body further out than the edge is left off; the edge is the smallest of 50, 100, 200, 500, 1,000 and 2,000 au that holds the planets, dwarf planets and craft shown.
|
|
107
|
+
*
|
|
108
|
+
* Attributes (the ones marked "same" are the names and meanings `<wakusei-flat>` has): `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), `paused` (same: hold the clock and keep the speed),
|
|
109
|
+
* `selected` (same: the chosen body's id such as `earth`, `halley` or `roadster`), `center` (a body to centre the map on and follow: `sun` by default; a drag lets go of it), `dot-size` (same: pixels), `controls` (same: `none`, or any of `centre zoom orbits track scale chips`; all by default), `lang` (same: `en` or `ja`; the page's language by default),
|
|
110
|
+
* `layers` (`planets dwarfs belts comets craft`: what is drawn; all by default, `off` for none), `craft`, `comets` and `belts` (`all`, `off` or ids with commas: which craft, comets or belts), `orbits` (`off` hides the orbit lines of the planets and dwarf planets), `track` (same: the whole path of the chosen craft and the orbit of the chosen comet, or of every one shown when none is chosen),
|
|
111
|
+
* `spacing` (`squeezed`, the default, or `true`), `extent` (the distance at the edge in au; `auto`, the default, is the smallest step that holds what is shown), `names` (same: `off` for no names beside the bodies, or the most names to show). `footprint`, `shade`, `home-lon` and `home-lat` belong to a map of a surface and are not read.
|
|
112
|
+
*
|
|
113
|
+
* Events (the names `<wakusei-flat>` sends): `wakusei-flat-pick` (a body or a belt body under the pointer, tapped, or reached by the keyboard: a `SystemPick`; `detail.id` is null when a tap lands on nothing and clears the choice), `wakusei-flat-filter` (a chip changed what is shown: `{ layers }`),
|
|
114
|
+
* `wakusei-flat-view` (the reader panned or zoomed: `{ center, x, y, zoom }`) and `wakusei-timechange` (`{ time, running, paused }`, at most four a second while time runs).
|
|
115
|
+
* Style it with the custom properties `--wakusei-flat-space`, `--wakusei-flat-aspect`, `--wakusei-flat-aspect-narrow` and `--wakusei-flat-radius`.
|
|
116
|
+
*
|
|
117
|
+
* @example
|
|
118
|
+
* ```ts no-run
|
|
119
|
+
* import "@johnmorrisdotca/wakusei-planets/flat/system/define";
|
|
120
|
+
* import type { WakuseiFlatSystem } from "@johnmorrisdotca/wakusei-planets/flat/system";
|
|
121
|
+
*
|
|
122
|
+
* const system = document.createElement("wakusei-flat-system") as WakuseiFlatSystem;
|
|
123
|
+
* system.setAttribute("selected", "roadster");
|
|
124
|
+
* system.setAttribute("track", "");
|
|
125
|
+
* document.body.append(system);
|
|
126
|
+
* system.addEventListener("wakusei-flat-pick", (event) => console.log((event as CustomEvent).detail.name));
|
|
127
|
+
* ```
|
|
128
|
+
*/
|
|
129
|
+
export class WakuseiFlatSystem extends HTMLElement {
|
|
130
|
+
static observedAttributes = ["time", "speed", "paused", "orbits", "track", "selected", "center", "dot-size", "controls", "lang", "layers", "craft", "comets", "belts", "spacing", "extent", "names"];
|
|
131
|
+
#clock = createGameClock("now", 1);
|
|
132
|
+
#speed = 1;
|
|
133
|
+
#paused = false;
|
|
134
|
+
#view = { width: 0, height: 0, centerX: 0, centerY: 0, zoom: 1 };
|
|
135
|
+
#follow = null;
|
|
136
|
+
#dpr = 1;
|
|
137
|
+
#layers = new Set(SYSTEM_LAYERS);
|
|
138
|
+
#craftIds;
|
|
139
|
+
#cometIds;
|
|
140
|
+
#beltIds = [...BELT_IDS];
|
|
141
|
+
#orbits = true;
|
|
142
|
+
#track = false;
|
|
143
|
+
#spacing = "squeezed";
|
|
144
|
+
#fixedExtent;
|
|
145
|
+
#selected = null;
|
|
146
|
+
#pendingSelected = null;
|
|
147
|
+
#cursor = null;
|
|
148
|
+
#hover = null;
|
|
149
|
+
#mks = [];
|
|
150
|
+
#belts = [];
|
|
151
|
+
#beltXY = new Float32Array(0);
|
|
152
|
+
#beltOn = 0;
|
|
153
|
+
#extent = 50;
|
|
154
|
+
#mercuryPx = 40;
|
|
155
|
+
#craftLoaded = new Set();
|
|
156
|
+
#beltsAsked = false;
|
|
157
|
+
#lastDraw = 0;
|
|
158
|
+
#totalMs = 0;
|
|
159
|
+
#timer;
|
|
160
|
+
#frame = 0;
|
|
161
|
+
#inView = true;
|
|
162
|
+
#connected = false;
|
|
163
|
+
#viewSent = "";
|
|
164
|
+
#announced = -Infinity;
|
|
165
|
+
#mapKey = "";
|
|
166
|
+
#loads = 0;
|
|
167
|
+
#stage;
|
|
168
|
+
#bar;
|
|
169
|
+
#mapCanvas;
|
|
170
|
+
#dotCanvas;
|
|
171
|
+
#selRing;
|
|
172
|
+
#hovRing;
|
|
173
|
+
#tip;
|
|
174
|
+
#live;
|
|
175
|
+
#sizer;
|
|
176
|
+
#watcher;
|
|
177
|
+
#langWatch;
|
|
178
|
+
#pointers = new Map();
|
|
179
|
+
#drag = null;
|
|
180
|
+
#counts = { draws: 0, mapDraws: 0, dots: 0, lastMs: 0, maxMs: 0 };
|
|
181
|
+
#measureCtx;
|
|
182
|
+
// --- the page's controls ---------------------------------------------------------------------------------------------------------------------
|
|
183
|
+
/** The id of the chosen body (or the name of a chosen belt body), or null. Set it to choose one from code (no `wakusei-flat-pick` is sent), `null` to clear. */
|
|
184
|
+
get selected() {
|
|
185
|
+
return this.#selected?.id ?? null;
|
|
186
|
+
}
|
|
187
|
+
set selected(id) {
|
|
188
|
+
this.#selected = this.#find(id);
|
|
189
|
+
this.#sync();
|
|
190
|
+
this.#request();
|
|
191
|
+
}
|
|
192
|
+
/** The id of the body the map is centred on and follows, or null while it is centred on the Sun or the reader has dragged it away. Set it to a body's id to centre there, or to `sun` or null for the Sun. */
|
|
193
|
+
get center() {
|
|
194
|
+
return this.#follow;
|
|
195
|
+
}
|
|
196
|
+
set center(id) {
|
|
197
|
+
this.#follow = id === null || id === "" || id === "sun" ? null : id;
|
|
198
|
+
if (this.#follow === null)
|
|
199
|
+
this.#view = clampSystemView({ ...this.#view, centerX: 0, centerY: 0 });
|
|
200
|
+
this.#mapKey = "";
|
|
201
|
+
this.#sync();
|
|
202
|
+
this.#request();
|
|
203
|
+
}
|
|
204
|
+
/** The zoom: 1 shows the whole disc, up to 8. */
|
|
205
|
+
get zoom() {
|
|
206
|
+
return this.#view.zoom;
|
|
207
|
+
}
|
|
208
|
+
set zoom(value) {
|
|
209
|
+
this.#setView({ ...this.#view, zoom: value });
|
|
210
|
+
}
|
|
211
|
+
/** 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. */
|
|
212
|
+
get time() {
|
|
213
|
+
return this.#clock.advance(performance.now());
|
|
214
|
+
}
|
|
215
|
+
set time(value) {
|
|
216
|
+
this.#clock.set(value === "now" ? "now" : typeof value === "number" ? value : value.getTime());
|
|
217
|
+
this.#request();
|
|
218
|
+
this.#schedule();
|
|
219
|
+
}
|
|
220
|
+
/** How fast the clock runs: 1 is real time, 0 holds, 86400 is a day a second, negative runs backwards. */
|
|
221
|
+
get speed() {
|
|
222
|
+
return this.#speed;
|
|
223
|
+
}
|
|
224
|
+
set speed(value) {
|
|
225
|
+
this.#speed = Number.isFinite(value) ? value : 1;
|
|
226
|
+
this.#clock.setSpeed(this.#paused ? 0 : this.#speed);
|
|
227
|
+
this.#schedule();
|
|
228
|
+
this.#request();
|
|
229
|
+
}
|
|
230
|
+
/** True while the clock is held with its speed kept: nothing is drawn but what the reader changes. Setting it false carries on from the moment shown at the same speed. */
|
|
231
|
+
get paused() {
|
|
232
|
+
return this.#paused;
|
|
233
|
+
}
|
|
234
|
+
set paused(value) {
|
|
235
|
+
if (value === this.#paused)
|
|
236
|
+
return;
|
|
237
|
+
const now = performance.now();
|
|
238
|
+
const shown = this.#clock.advance(now);
|
|
239
|
+
this.#paused = value;
|
|
240
|
+
this.#clock.set(shown, now);
|
|
241
|
+
this.#clock.setSpeed(value ? 0 : this.#speed, now);
|
|
242
|
+
this.#schedule();
|
|
243
|
+
this.#request();
|
|
244
|
+
this.dispatchEvent(new CustomEvent("wakusei-timechange", { detail: { time: shown, running: this.#running(), paused: value }, bubbles: true, composed: true }));
|
|
245
|
+
}
|
|
246
|
+
/** The language of the words: the `lang` attribute, else the page's. */
|
|
247
|
+
get language() {
|
|
248
|
+
return (this.getAttribute("lang") ?? this.closest("[lang]")?.getAttribute("lang") ?? this.ownerDocument.documentElement.lang ?? "en").toLowerCase().startsWith("ja") ? "ja" : "en";
|
|
249
|
+
}
|
|
250
|
+
/** The layers drawn now: `planets`, `dwarfs`, `belts`, `comets` and `craft`. */
|
|
251
|
+
get layers() {
|
|
252
|
+
return [...this.#layers];
|
|
253
|
+
}
|
|
254
|
+
/** The distance at the edge of the map in au, as of the last draw. */
|
|
255
|
+
get extent() {
|
|
256
|
+
return this.#extent;
|
|
257
|
+
}
|
|
258
|
+
/** The bodies drawn at the last draw, with their place on the screen: their ids, names, kinds and layers, whether each is inside the box, and its distance from the Sun in au. */
|
|
259
|
+
get bodies() {
|
|
260
|
+
const lang = this.language;
|
|
261
|
+
return this.#mks.map((m) => ({ id: m.mark.id, name: systemName(m.mark.id, lang), kind: m.mark.kind, layer: m.mark.layer, shown: m.on, distanceAu: m.mark.distanceAu, predicted: m.dim }));
|
|
262
|
+
}
|
|
263
|
+
/** Counters for a test or a page: draws of the bodies, draws of the disc, the bodies and belt points drawn at the last draw, the milliseconds the last draw and the slowest took, and the canvases' size in pixels. */
|
|
264
|
+
get stats() {
|
|
265
|
+
const canvases = [this.#mapCanvas, this.#dotCanvas].filter((c) => c !== undefined).map((c) => ({ width: c.width, height: c.height }));
|
|
266
|
+
return { draws: this.#counts.draws, mapDraws: this.#counts.mapDraws, dots: this.#counts.dots, beltPoints: this.#beltOn, 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, extent: this.#extent, canvases };
|
|
267
|
+
}
|
|
268
|
+
/** Where a body 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 (a layer off, outside the edge, or a craft with no path at the moment shown). */
|
|
269
|
+
placeOf(id) {
|
|
270
|
+
const one = this.#mks.find((m) => m.mark.id === id && m.on);
|
|
271
|
+
return one ? { x: one.px, y: one.py, au: one.mark.au, distanceAu: one.mark.distanceAu } : null;
|
|
272
|
+
}
|
|
273
|
+
/** Choose a body 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. */
|
|
274
|
+
pick(id) {
|
|
275
|
+
this.selected = id;
|
|
276
|
+
}
|
|
277
|
+
// --- the element's life ----------------------------------------------------------------------------------------------------------------------
|
|
278
|
+
connectedCallback() {
|
|
279
|
+
const root = this.shadowRoot ?? this.attachShadow({ mode: "open" });
|
|
280
|
+
adoptStyles(root, CSS, this.getAttribute("nonce") ?? "");
|
|
281
|
+
if (!this.#stage)
|
|
282
|
+
this.#build(root);
|
|
283
|
+
this.#connected = true;
|
|
284
|
+
this.#readAll();
|
|
285
|
+
this.#sizer = new ResizeObserver(() => {
|
|
286
|
+
this.#measure();
|
|
287
|
+
this.#request();
|
|
288
|
+
});
|
|
289
|
+
this.#sizer.observe(this.#stage);
|
|
290
|
+
if (typeof IntersectionObserver === "function") {
|
|
291
|
+
this.#watcher = new IntersectionObserver((entries) => {
|
|
292
|
+
const was = this.#visibleNow();
|
|
293
|
+
this.#inView = entries[entries.length - 1]?.isIntersecting ?? true;
|
|
294
|
+
if (!was && this.#visibleNow())
|
|
295
|
+
this.#wake();
|
|
296
|
+
});
|
|
297
|
+
this.#watcher.observe(this);
|
|
298
|
+
}
|
|
299
|
+
this.#langWatch = new MutationObserver(() => {
|
|
300
|
+
if (!this.hasAttribute("lang"))
|
|
301
|
+
this.#words();
|
|
302
|
+
});
|
|
303
|
+
this.#langWatch.observe(this.ownerDocument.documentElement, { attributes: true, attributeFilter: ["lang"] });
|
|
304
|
+
document.addEventListener("visibilitychange", this.#onVisibility);
|
|
305
|
+
this.#measure();
|
|
306
|
+
this.#words();
|
|
307
|
+
this.setAttribute("data-state", "loading");
|
|
308
|
+
this.#fetch();
|
|
309
|
+
this.#request();
|
|
310
|
+
this.#schedule();
|
|
311
|
+
}
|
|
312
|
+
disconnectedCallback() {
|
|
313
|
+
this.#connected = false;
|
|
314
|
+
clearTimeout(this.#timer);
|
|
315
|
+
this.#timer = undefined;
|
|
316
|
+
if (this.#frame)
|
|
317
|
+
cancelAnimationFrame(this.#frame);
|
|
318
|
+
this.#frame = 0;
|
|
319
|
+
this.#sizer?.disconnect();
|
|
320
|
+
this.#watcher?.disconnect();
|
|
321
|
+
this.#langWatch?.disconnect();
|
|
322
|
+
this.#sizer = this.#watcher = this.#langWatch = undefined;
|
|
323
|
+
document.removeEventListener("visibilitychange", this.#onVisibility);
|
|
324
|
+
}
|
|
325
|
+
attributeChangedCallback(name, old, value) {
|
|
326
|
+
if (!this.#connected || old === value)
|
|
327
|
+
return;
|
|
328
|
+
this.#read(name);
|
|
329
|
+
if (name === "lang" || name === "controls" || name === "layers" || name === "spacing")
|
|
330
|
+
this.#words();
|
|
331
|
+
if (name === "layers" || name === "craft" || name === "belts")
|
|
332
|
+
this.#fetch();
|
|
333
|
+
this.#sync();
|
|
334
|
+
this.#request();
|
|
335
|
+
this.#schedule();
|
|
336
|
+
}
|
|
337
|
+
#onVisibility = () => {
|
|
338
|
+
if (this.#visibleNow())
|
|
339
|
+
this.#wake();
|
|
340
|
+
else {
|
|
341
|
+
clearTimeout(this.#timer);
|
|
342
|
+
this.#timer = undefined;
|
|
343
|
+
}
|
|
344
|
+
};
|
|
345
|
+
#visibleNow() {
|
|
346
|
+
return this.#connected && this.#inView && !hidden();
|
|
347
|
+
}
|
|
348
|
+
/** Back in sight: bring the clock up to date, draw once, and start the timer again. */
|
|
349
|
+
#wake() {
|
|
350
|
+
this.#clock.advance(performance.now());
|
|
351
|
+
this.#request();
|
|
352
|
+
this.#schedule();
|
|
353
|
+
}
|
|
354
|
+
#readAll() {
|
|
355
|
+
for (const name of _a.observedAttributes)
|
|
356
|
+
this.#read(name);
|
|
357
|
+
}
|
|
358
|
+
/** Take in one attribute. A change to one never undoes what the reader did with another (a chosen body, the pan). */
|
|
359
|
+
#read(name) {
|
|
360
|
+
const value = this.getAttribute(name);
|
|
361
|
+
if (name === "layers") {
|
|
362
|
+
const list = readList(value);
|
|
363
|
+
this.#layers = new Set(list === undefined ? SYSTEM_LAYERS : SYSTEM_LAYERS.filter((one) => list.includes(one)));
|
|
364
|
+
this.#mapKey = "";
|
|
365
|
+
}
|
|
366
|
+
else if (name === "craft")
|
|
367
|
+
this.#craftIds = readList(value);
|
|
368
|
+
else if (name === "comets")
|
|
369
|
+
this.#cometIds = readList(value);
|
|
370
|
+
else if (name === "belts") {
|
|
371
|
+
const list = readList(value);
|
|
372
|
+
this.#beltIds = list === undefined ? [...BELT_IDS] : BELT_IDS.filter((one) => list.includes(one));
|
|
373
|
+
}
|
|
374
|
+
else if (name === "orbits") {
|
|
375
|
+
this.#orbits = value === null ? true : flag(value);
|
|
376
|
+
this.#mapKey = "";
|
|
377
|
+
}
|
|
378
|
+
else if (name === "track")
|
|
379
|
+
this.#track = flag(value);
|
|
380
|
+
else if (name === "spacing") {
|
|
381
|
+
this.#spacing = value !== null && value.trim().toLowerCase() === "true" ? "true" : "squeezed";
|
|
382
|
+
this.#mapKey = "";
|
|
383
|
+
}
|
|
384
|
+
else if (name === "extent") {
|
|
385
|
+
const au = finite(value, Number.NaN);
|
|
386
|
+
this.#fixedExtent = au > 0 ? au : undefined;
|
|
387
|
+
this.#mapKey = "";
|
|
388
|
+
}
|
|
389
|
+
else if (name === "center") {
|
|
390
|
+
const id = value === null ? "" : value.trim().toLowerCase();
|
|
391
|
+
this.#follow = id === "" || id === "sun" ? null : id;
|
|
392
|
+
if (this.#follow === null)
|
|
393
|
+
this.#view = clampSystemView({ ...this.#view, centerX: 0, centerY: 0 });
|
|
394
|
+
this.#mapKey = "";
|
|
395
|
+
}
|
|
396
|
+
else if (name === "selected") {
|
|
397
|
+
this.#pendingSelected = null;
|
|
398
|
+
this.#selected = value === null || value.trim() === "" ? null : this.#find(value.trim());
|
|
399
|
+
if (this.#selected === null && value !== null && value.trim() !== "")
|
|
400
|
+
this.#pendingSelected = value.trim();
|
|
401
|
+
}
|
|
402
|
+
else if (name === "paused") {
|
|
403
|
+
if (flag(value) !== this.#paused)
|
|
404
|
+
this.paused = flag(value);
|
|
405
|
+
}
|
|
406
|
+
else if (name === "time" || name === "speed")
|
|
407
|
+
this.#readClock();
|
|
408
|
+
else if (name === "dot-size" || name === "names")
|
|
409
|
+
this.#mapKey = "";
|
|
410
|
+
}
|
|
411
|
+
#readClock() {
|
|
412
|
+
const now = performance.now();
|
|
413
|
+
this.#clock.advance(now);
|
|
414
|
+
const mode = readTime(this.getAttribute("time"));
|
|
415
|
+
const raw = this.getAttribute("speed");
|
|
416
|
+
const speed = raw === null || raw.trim() === "" ? 1 : Number(raw);
|
|
417
|
+
this.#speed = Number.isFinite(speed) ? speed : 1;
|
|
418
|
+
this.#clock.set(mode, now);
|
|
419
|
+
this.#clock.setSpeed(this.#paused ? 0 : this.#speed, now);
|
|
420
|
+
}
|
|
421
|
+
/** The body with this id, or the belt body with this name, from what the map holds. */
|
|
422
|
+
#find(id) {
|
|
423
|
+
if (id === null)
|
|
424
|
+
return null;
|
|
425
|
+
const key = id.trim().toLowerCase();
|
|
426
|
+
if (key === "sun" || key === "")
|
|
427
|
+
return key === "sun" ? { kind: "mark", id: "sun" } : null;
|
|
428
|
+
if (this.#mks.some((m) => m.mark.id === key) || this.#knownId(key))
|
|
429
|
+
return { kind: "mark", id: key };
|
|
430
|
+
const index = this.#belts.findIndex((b) => b.name.toLowerCase() === key);
|
|
431
|
+
return index >= 0 ? { kind: "belt", id: this.#belts[index].name, index } : null;
|
|
432
|
+
}
|
|
433
|
+
#knownId(id) {
|
|
434
|
+
return (["mercury", "venus", "earth", "mars", "jupiter", "saturn", "uranus", "neptune", "pluto"].includes(id) || SYSTEM_DWARFS.includes(id) || SPACECRAFT_IDS.includes(id) || listComets().some((c) => c.id === id));
|
|
435
|
+
}
|
|
436
|
+
/** The things the map has to wait for: the craft's paths and the belts' elements, each fetched once, when a layer first asks for them. */
|
|
437
|
+
#fetch() {
|
|
438
|
+
if (this.#layers.has("craft")) {
|
|
439
|
+
const wanted = (this.#craftIds ?? [...SPACECRAFT_IDS]).filter((id) => SPACECRAFT_IDS.includes(id) && !this.#craftLoaded.has(id));
|
|
440
|
+
if (wanted.length > 0) {
|
|
441
|
+
for (const id of wanted)
|
|
442
|
+
this.#craftLoaded.add(id);
|
|
443
|
+
this.#loads += 1;
|
|
444
|
+
void loadSpacecraft(...wanted).then(() => this.#loaded(), () => {
|
|
445
|
+
for (const id of wanted)
|
|
446
|
+
this.#craftLoaded.delete(id);
|
|
447
|
+
this.#loaded();
|
|
448
|
+
});
|
|
449
|
+
}
|
|
450
|
+
}
|
|
451
|
+
if (this.#layers.has("belts") && !this.#beltsAsked) {
|
|
452
|
+
this.#beltsAsked = true;
|
|
453
|
+
this.#loads += 1;
|
|
454
|
+
void loadBeltBodies().then((bodies) => {
|
|
455
|
+
this.#belts = bodies;
|
|
456
|
+
this.#beltXY = new Float32Array(bodies.length * 2);
|
|
457
|
+
if (this.#pendingSelected !== null) {
|
|
458
|
+
this.#selected = this.#find(this.#pendingSelected) ?? this.#selected;
|
|
459
|
+
this.#pendingSelected = null;
|
|
460
|
+
}
|
|
461
|
+
this.#loaded();
|
|
462
|
+
}, () => {
|
|
463
|
+
this.#beltsAsked = false;
|
|
464
|
+
this.#loaded();
|
|
465
|
+
});
|
|
466
|
+
}
|
|
467
|
+
}
|
|
468
|
+
#loaded() {
|
|
469
|
+
this.#loads = Math.max(0, this.#loads - 1);
|
|
470
|
+
if (!this.#connected)
|
|
471
|
+
return;
|
|
472
|
+
if (this.#pendingSelected !== null) {
|
|
473
|
+
const found = this.#find(this.#pendingSelected);
|
|
474
|
+
if (found) {
|
|
475
|
+
this.#selected = found;
|
|
476
|
+
this.#pendingSelected = null;
|
|
477
|
+
}
|
|
478
|
+
}
|
|
479
|
+
this.#mapKey = "";
|
|
480
|
+
this.#request();
|
|
481
|
+
}
|
|
482
|
+
// --- the clock -------------------------------------------------------------------------------------------------------------------------------
|
|
483
|
+
/** 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. */
|
|
484
|
+
#running() {
|
|
485
|
+
return this.#visibleNow() && this.#clock.speed !== 0 && !this.#paused;
|
|
486
|
+
}
|
|
487
|
+
#fast() {
|
|
488
|
+
return !this.#paused && Math.abs(this.#speed) >= STREAK_FROM;
|
|
489
|
+
}
|
|
490
|
+
/**
|
|
491
|
+
* How often a running clock draws: as often as the innermost planet moves half a pixel, up to every frame, and at least once in 30 seconds (the belts and the craft are slower than Mercury); once a second at most for a reader who asked for reduced motion.
|
|
492
|
+
*/
|
|
493
|
+
#rate() {
|
|
494
|
+
let perSecond = (MERCURY_RATE * Math.abs(this.#speed) * this.#mercuryPx) / 0.5;
|
|
495
|
+
perSecond = Math.min(60, Math.max(1 / 30, perSecond));
|
|
496
|
+
if (prefersLessMotion())
|
|
497
|
+
perSecond = Math.min(perSecond, 1);
|
|
498
|
+
return perSecond;
|
|
499
|
+
}
|
|
500
|
+
/** After a draw, or when something about the clock changed: ask for the next frame in time. Nothing at all when the clock is held or out of sight. */
|
|
501
|
+
#schedule() {
|
|
502
|
+
clearTimeout(this.#timer);
|
|
503
|
+
this.#timer = undefined;
|
|
504
|
+
if (!this.#running())
|
|
505
|
+
return;
|
|
506
|
+
const wait = 1000 / this.#rate() - (performance.now() - this.#lastDraw);
|
|
507
|
+
if (wait > 14) {
|
|
508
|
+
this.#timer = setTimeout(() => {
|
|
509
|
+
this.#timer = undefined;
|
|
510
|
+
this.#request();
|
|
511
|
+
}, wait);
|
|
512
|
+
}
|
|
513
|
+
else
|
|
514
|
+
this.#request();
|
|
515
|
+
}
|
|
516
|
+
/** `wakusei-timechange`: the moment shown, at most four times a second while time runs. */
|
|
517
|
+
#announceTime(ms) {
|
|
518
|
+
const now = performance.now();
|
|
519
|
+
if (!this.#running() || now - this.#announced < TIME_EVENT_MS)
|
|
520
|
+
return;
|
|
521
|
+
this.#announced = now;
|
|
522
|
+
this.dispatchEvent(new CustomEvent("wakusei-timechange", { detail: { time: ms, running: true, paused: false }, bubbles: true, composed: true }));
|
|
523
|
+
}
|
|
524
|
+
// --- the map ---------------------------------------------------------------------------------------------------------------------------------
|
|
525
|
+
#measure() {
|
|
526
|
+
const stage = this.#stage;
|
|
527
|
+
if (!stage)
|
|
528
|
+
return;
|
|
529
|
+
const width = stage.clientWidth;
|
|
530
|
+
const height = stage.clientHeight;
|
|
531
|
+
// the canvases stay inside 6 million pixels and 4096 a side, whatever the screen
|
|
532
|
+
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));
|
|
533
|
+
if (width === this.#view.width && height === this.#view.height && dpr === this.#dpr)
|
|
534
|
+
return;
|
|
535
|
+
this.#dpr = dpr;
|
|
536
|
+
this.#view = clampSystemView({ ...this.#view, width, height });
|
|
537
|
+
for (const canvas of [this.#mapCanvas, this.#dotCanvas]) {
|
|
538
|
+
if (!canvas)
|
|
539
|
+
continue;
|
|
540
|
+
canvas.width = Math.max(1, Math.round(width * dpr));
|
|
541
|
+
canvas.height = Math.max(1, Math.round(height * dpr));
|
|
542
|
+
}
|
|
543
|
+
this.#mapKey = "";
|
|
544
|
+
}
|
|
545
|
+
#measureText(text) {
|
|
546
|
+
this.#measureCtx ??= this.ownerDocument.createElement("canvas").getContext("2d");
|
|
547
|
+
const ctx = this.#measureCtx;
|
|
548
|
+
if (ctx === null)
|
|
549
|
+
return text.length * 6.5;
|
|
550
|
+
ctx.font = "600 11px system-ui, sans-serif";
|
|
551
|
+
return ctx.measureText(text).width;
|
|
552
|
+
}
|
|
553
|
+
/** The disc and what stands still on it: its edge, the rings with their distances, the orbit lines of the planets and dwarf planets (cut once a year of map time). Drawn again only when the view, the reach, the spacing or a layer changed. */
|
|
554
|
+
#drawMap(ms) {
|
|
555
|
+
const canvas = this.#mapCanvas;
|
|
556
|
+
const v = this.#view;
|
|
557
|
+
if (!canvas || v.width === 0)
|
|
558
|
+
return;
|
|
559
|
+
const lang = this.language;
|
|
560
|
+
const year = Math.floor(ms / (365.25 * 86_400_000));
|
|
561
|
+
const key = [v.width, v.height, this.#dpr, v.centerX.toFixed(4), v.centerY.toFixed(4), v.zoom.toFixed(3), this.#extent, this.#spacing, this.#orbits, this.#layers.has("planets"), this.#layers.has("dwarfs"), year, lang].join("|");
|
|
562
|
+
if (key === this.#mapKey)
|
|
563
|
+
return;
|
|
564
|
+
this.#mapKey = key;
|
|
565
|
+
this.#counts.mapDraws += 1;
|
|
566
|
+
const ctx = canvas.getContext("2d");
|
|
567
|
+
if (!ctx)
|
|
568
|
+
return;
|
|
569
|
+
ctx.setTransform(this.#dpr, 0, 0, this.#dpr, 0, 0);
|
|
570
|
+
ctx.clearRect(0, 0, v.width, v.height);
|
|
571
|
+
const k = systemScale(v);
|
|
572
|
+
const sun = projectSystem(v, 0, 0);
|
|
573
|
+
ctx.beginPath();
|
|
574
|
+
ctx.arc(sun.x, sun.y, k, 0, Math.PI * 2);
|
|
575
|
+
ctx.fillStyle = "#0a1224";
|
|
576
|
+
ctx.fill();
|
|
577
|
+
ctx.save();
|
|
578
|
+
ctx.clip();
|
|
579
|
+
// the rings: the Earth's distance, Jupiter's, Saturn's, Neptune's and the rest, left out where two would crowd
|
|
580
|
+
ctx.font = "11px system-ui, sans-serif";
|
|
581
|
+
ctx.textBaseline = "middle";
|
|
582
|
+
let last = -Infinity;
|
|
583
|
+
const rings = [];
|
|
584
|
+
for (const au of SYSTEM_RINGS) {
|
|
585
|
+
if (au > this.#extent)
|
|
586
|
+
continue;
|
|
587
|
+
const r = planRadius(au, this.#extent, this.#spacing, 0.1) * k;
|
|
588
|
+
if (r - last < 14)
|
|
589
|
+
continue;
|
|
590
|
+
last = r;
|
|
591
|
+
rings.push([au, r]);
|
|
592
|
+
}
|
|
593
|
+
ctx.lineWidth = 1;
|
|
594
|
+
ctx.strokeStyle = "rgba(150,175,215,.22)";
|
|
595
|
+
for (const [, r] of rings) {
|
|
596
|
+
ctx.beginPath();
|
|
597
|
+
ctx.arc(sun.x, sun.y, r, 0, Math.PI * 2);
|
|
598
|
+
ctx.stroke();
|
|
599
|
+
}
|
|
600
|
+
if (this.#orbits) {
|
|
601
|
+
ctx.lineWidth = 1;
|
|
602
|
+
const ids = [...(this.#layers.has("planets") ? ["mercury", "venus", "earth", "mars", "jupiter", "saturn", "uranus", "neptune", "pluto"] : []), ...(this.#layers.has("dwarfs") ? SYSTEM_DWARFS : [])];
|
|
603
|
+
for (const id of ids) {
|
|
604
|
+
const line = systemOrbit(id, ms);
|
|
605
|
+
ctx.beginPath();
|
|
606
|
+
line.forEach((p, i) => {
|
|
607
|
+
const q = squeezeAu(p[0], p[1], this.#extent, this.#spacing);
|
|
608
|
+
const at = projectSystem(v, q.x, q.y);
|
|
609
|
+
if (i === 0)
|
|
610
|
+
ctx.moveTo(at.x, at.y);
|
|
611
|
+
else
|
|
612
|
+
ctx.lineTo(at.x, at.y);
|
|
613
|
+
});
|
|
614
|
+
ctx.strokeStyle = this.#layers.has("planets") && !SYSTEM_DWARFS.includes(id) ? `${systemColour(id, "planets")}77` : "rgba(207,214,224,.32)";
|
|
615
|
+
ctx.stroke();
|
|
616
|
+
}
|
|
617
|
+
}
|
|
618
|
+
ctx.restore();
|
|
619
|
+
ctx.beginPath();
|
|
620
|
+
ctx.arc(sun.x, sun.y, k, 0, Math.PI * 2);
|
|
621
|
+
ctx.lineWidth = 1.5;
|
|
622
|
+
ctx.strokeStyle = "rgba(150,175,215,.5)";
|
|
623
|
+
ctx.stroke();
|
|
624
|
+
// the distances, along a line up and to the right
|
|
625
|
+
const angle = (-40 * Math.PI) / 180;
|
|
626
|
+
ctx.fillStyle = "rgba(190,208,235,.85)";
|
|
627
|
+
ctx.textAlign = "left";
|
|
628
|
+
for (const [au, r] of rings) {
|
|
629
|
+
const x = sun.x + Math.cos(angle) * r + 3;
|
|
630
|
+
const y = sun.y + Math.sin(angle) * r;
|
|
631
|
+
if (x < 0 || x > v.width - 30 || y < 8 || y > v.height - 8)
|
|
632
|
+
continue;
|
|
633
|
+
ctx.fillText(saySystem(lang, "ring", { n: au }), x, y - 7);
|
|
634
|
+
}
|
|
635
|
+
// the scale, said at the foot
|
|
636
|
+
ctx.textAlign = "left";
|
|
637
|
+
ctx.fillStyle = "rgba(190,208,235,.9)";
|
|
638
|
+
ctx.fillText(saySystem(lang, this.#spacing === "squeezed" ? "scaleSqueezed" : "scaleTrue"), 8, v.height - 10);
|
|
639
|
+
}
|
|
640
|
+
// --- the bodies ------------------------------------------------------------------------------------------------------------------------------
|
|
641
|
+
/** Whether a craft's place is a prediction: the car's always (its path is Horizons' prediction from 2018), and any craft's after its mission ended. */
|
|
642
|
+
#predicted(id, ms) {
|
|
643
|
+
if (!SPACECRAFT_IDS.includes(id))
|
|
644
|
+
return false;
|
|
645
|
+
const end = SPACECRAFT[id].end;
|
|
646
|
+
return id === "roadster" || (end !== undefined && ms > end);
|
|
647
|
+
}
|
|
648
|
+
#draw() {
|
|
649
|
+
const started = performance.now();
|
|
650
|
+
const canvas = this.#dotCanvas;
|
|
651
|
+
const v0 = this.#view;
|
|
652
|
+
if (!canvas || v0.width === 0 || v0.height === 0)
|
|
653
|
+
return;
|
|
654
|
+
const ms = this.#clock.advance(started);
|
|
655
|
+
const showLayers = [...this.#layers].filter((one) => one !== "belts");
|
|
656
|
+
const marks = systemMarks(ms, { layers: showLayers, ...(this.#craftIds ? { craft: this.#craftIds } : {}), ...(this.#cometIds ? { comets: this.#cometIds } : {}), ...(this.#fixedExtent ? { extent: this.#fixedExtent } : {}), spacing: this.#spacing });
|
|
657
|
+
this.#extent = systemReach(marks, this.#fixedExtent);
|
|
658
|
+
// the marks are placed again on the reach: `systemMarks` already used it, so `x` and `y` are on this disc
|
|
659
|
+
if (this.#follow !== null) {
|
|
660
|
+
const at = marks.find((m) => m.id === this.#follow && !m.beyond);
|
|
661
|
+
if (at)
|
|
662
|
+
this.#view = clampSystemView({ ...this.#view, centerX: at.x, centerY: at.y });
|
|
663
|
+
}
|
|
664
|
+
const v = this.#view;
|
|
665
|
+
this.#drawMap(ms);
|
|
666
|
+
const ctx = canvas.getContext("2d");
|
|
667
|
+
if (!ctx)
|
|
668
|
+
return;
|
|
669
|
+
ctx.setTransform(this.#dpr, 0, 0, this.#dpr, 0, 0);
|
|
670
|
+
ctx.clearRect(0, 0, v.width, v.height);
|
|
671
|
+
const k = systemScale(v);
|
|
672
|
+
const size = finite(this.getAttribute("dot-size"), 0);
|
|
673
|
+
const radius = (size > 0 ? size / 2 : 3.6) * Math.min(1.8, 0.85 + v.zoom * 0.15);
|
|
674
|
+
const pad = radius + 4;
|
|
675
|
+
const lang = this.language;
|
|
676
|
+
const sun = projectSystem(v, 0, 0);
|
|
677
|
+
const mks = marks.map((mark) => {
|
|
678
|
+
const at = projectSystem(v, mark.x, mark.y);
|
|
679
|
+
const on = !mark.beyond && at.x >= -pad && at.x <= v.width + pad && at.y >= -pad && at.y <= v.height + pad;
|
|
680
|
+
return { mark, px: at.x, py: at.y, on, colour: systemColour(mark.id, mark.layer), dim: mark.layer === "craft" && this.#predicted(mark.id, ms) };
|
|
681
|
+
});
|
|
682
|
+
this.#mks = mks;
|
|
683
|
+
const mercury = mks.find((m) => m.mark.id === "mercury");
|
|
684
|
+
this.#mercuryPx = mercury ? Math.max(6, Math.hypot(mercury.px - sun.x, mercury.py - sun.y)) : 40;
|
|
685
|
+
// the belts, as points, clipped to the disc
|
|
686
|
+
this.#beltOn = 0;
|
|
687
|
+
if (this.#layers.has("belts") && this.#belts.length > 0) {
|
|
688
|
+
systemBelts(this.#belts, ms, this.#beltXY);
|
|
689
|
+
const wanted = new Set(this.#beltIds);
|
|
690
|
+
ctx.save();
|
|
691
|
+
ctx.beginPath();
|
|
692
|
+
ctx.arc(sun.x, sun.y, k, 0, Math.PI * 2);
|
|
693
|
+
ctx.clip();
|
|
694
|
+
const side = Math.max(1.3, Math.min(2.6, 1.1 + v.zoom * 0.2));
|
|
695
|
+
for (const belt of BELT_IDS) {
|
|
696
|
+
if (!wanted.has(belt))
|
|
697
|
+
continue;
|
|
698
|
+
ctx.fillStyle = BELTS[belt].colour;
|
|
699
|
+
ctx.globalAlpha = 0.85;
|
|
700
|
+
ctx.beginPath();
|
|
701
|
+
for (let i = 0; i < this.#belts.length; i += 1) {
|
|
702
|
+
if (this.#belts[i].belt !== belt)
|
|
703
|
+
continue;
|
|
704
|
+
const q = squeezeAu(this.#beltXY[i * 2], this.#beltXY[i * 2 + 1], this.#extent, this.#spacing);
|
|
705
|
+
if (q.r > 1)
|
|
706
|
+
continue;
|
|
707
|
+
const px = v.width / 2 + (q.x - v.centerX) * k;
|
|
708
|
+
const py = v.height / 2 - (q.y - v.centerY) * k;
|
|
709
|
+
if (px < -2 || px > v.width + 2 || py < -2 || py > v.height + 2)
|
|
710
|
+
continue;
|
|
711
|
+
ctx.rect(px - side / 2, py - side / 2, side, side);
|
|
712
|
+
this.#beltOn += 1;
|
|
713
|
+
}
|
|
714
|
+
ctx.fill();
|
|
715
|
+
}
|
|
716
|
+
ctx.globalAlpha = 1;
|
|
717
|
+
ctx.restore();
|
|
718
|
+
}
|
|
719
|
+
const chosenMark = this.#selected?.kind === "mark" ? this.#selected.id : null;
|
|
720
|
+
this.#drawLines(ctx, mks, chosenMark, ms);
|
|
721
|
+
// a short streak behind each body at a fast clock
|
|
722
|
+
const streak = this.#fast();
|
|
723
|
+
if (streak) {
|
|
724
|
+
const lag = this.#speed * 1000 * STREAK_SECONDS;
|
|
725
|
+
const before = systemMarks(ms - lag, { layers: showLayers.filter((one) => one !== "comets"), ...(this.#craftIds ? { craft: this.#craftIds } : {}), extent: this.#extent, spacing: this.#spacing });
|
|
726
|
+
const was = new Map(before.map((m) => [m.id, m]));
|
|
727
|
+
ctx.lineCap = "butt";
|
|
728
|
+
ctx.lineWidth = Math.max(1.5, radius * 1.1);
|
|
729
|
+
ctx.globalAlpha = 0.7;
|
|
730
|
+
for (const m of mks) {
|
|
731
|
+
const b = was.get(m.mark.id);
|
|
732
|
+
if (!m.on || !b || m.mark.layer === "sun" || b.beyond)
|
|
733
|
+
continue;
|
|
734
|
+
const at = projectSystem(v, b.x, b.y);
|
|
735
|
+
const d = Math.hypot(at.x - m.px, at.y - m.py);
|
|
736
|
+
if (d < 3)
|
|
737
|
+
continue;
|
|
738
|
+
const f = d > STREAK_PX ? STREAK_PX / d : 1;
|
|
739
|
+
ctx.strokeStyle = m.colour;
|
|
740
|
+
ctx.beginPath();
|
|
741
|
+
ctx.moveTo(m.px + (at.x - m.px) * f, m.py + (at.y - m.py) * f);
|
|
742
|
+
ctx.lineTo(m.px, m.py);
|
|
743
|
+
ctx.stroke();
|
|
744
|
+
}
|
|
745
|
+
ctx.globalAlpha = 1;
|
|
746
|
+
}
|
|
747
|
+
let dots = 0;
|
|
748
|
+
for (const m of mks) {
|
|
749
|
+
if (!m.on)
|
|
750
|
+
continue;
|
|
751
|
+
dots += 1;
|
|
752
|
+
this.#drawMark(ctx, m, radius, sun);
|
|
753
|
+
}
|
|
754
|
+
this.#drawBeltChoice(ctx, radius);
|
|
755
|
+
this.#drawNames(ctx, mks, lang, chosenMark);
|
|
756
|
+
this.#counts.draws += 1;
|
|
757
|
+
this.#counts.dots = dots;
|
|
758
|
+
this.#placeMarks();
|
|
759
|
+
const took = performance.now() - started;
|
|
760
|
+
this.#lastDraw = started;
|
|
761
|
+
this.#totalMs += took;
|
|
762
|
+
this.#counts.lastMs = took;
|
|
763
|
+
if (took > this.#counts.maxMs)
|
|
764
|
+
this.#counts.maxMs = took;
|
|
765
|
+
if (this.#loads === 0 && this.getAttribute("data-state") !== "ready")
|
|
766
|
+
this.setAttribute("data-state", "ready");
|
|
767
|
+
this.#announceView();
|
|
768
|
+
this.#announceTime(ms);
|
|
769
|
+
this.#schedule();
|
|
770
|
+
}
|
|
771
|
+
/** One body: a circle for a planet, a smaller one for a dwarf planet or small body, a dot with a tail for a comet near the Sun, a diamond for a craft (a ring where its place is a prediction) and the Sun as a bright disc. */
|
|
772
|
+
#drawMark(ctx, m, radius, sun) {
|
|
773
|
+
const { mark, px, py, colour } = m;
|
|
774
|
+
ctx.save();
|
|
775
|
+
ctx.translate(px, py);
|
|
776
|
+
ctx.lineWidth = 1.5;
|
|
777
|
+
ctx.strokeStyle = "rgba(0,0,0,.8)";
|
|
778
|
+
if (mark.layer === "sun") {
|
|
779
|
+
ctx.beginPath();
|
|
780
|
+
ctx.arc(0, 0, radius * 2, 0, Math.PI * 2);
|
|
781
|
+
ctx.fillStyle = colour;
|
|
782
|
+
ctx.fill();
|
|
783
|
+
ctx.stroke();
|
|
784
|
+
}
|
|
785
|
+
else if (mark.layer === "craft") {
|
|
786
|
+
ctx.rotate(Math.PI / 4);
|
|
787
|
+
const s = mark.id === "roadster" ? radius * 0.95 : radius * 0.75;
|
|
788
|
+
if (m.dim) {
|
|
789
|
+
ctx.strokeRect(-s, -s, s * 2, s * 2);
|
|
790
|
+
ctx.fillStyle = `${colour}59`;
|
|
791
|
+
ctx.fillRect(-s, -s, s * 2, s * 2);
|
|
792
|
+
ctx.strokeStyle = colour;
|
|
793
|
+
ctx.lineWidth = 1.6;
|
|
794
|
+
ctx.strokeRect(-s, -s, s * 2, s * 2);
|
|
795
|
+
}
|
|
796
|
+
else {
|
|
797
|
+
ctx.fillStyle = colour;
|
|
798
|
+
ctx.strokeRect(-s, -s, s * 2, s * 2);
|
|
799
|
+
ctx.fillRect(-s, -s, s * 2, s * 2);
|
|
800
|
+
}
|
|
801
|
+
if (mark.id === "roadster") {
|
|
802
|
+
ctx.strokeStyle = "#fff";
|
|
803
|
+
ctx.lineWidth = 1.2;
|
|
804
|
+
ctx.strokeRect(-s - 1.5, -s - 1.5, s * 2 + 3, s * 2 + 3);
|
|
805
|
+
}
|
|
806
|
+
}
|
|
807
|
+
else if (mark.layer === "comets") {
|
|
808
|
+
// a tail turned from the Sun, longer the nearer the Sun
|
|
809
|
+
const len = 22 * (1 - Math.min(1, mark.planeAu / 8));
|
|
810
|
+
const away = Math.hypot(px - sun.x, py - sun.y);
|
|
811
|
+
if (len > 4 && away > 0) {
|
|
812
|
+
const grad = ctx.createLinearGradient(0, 0, ((px - sun.x) / away) * len, ((py - sun.y) / away) * len);
|
|
813
|
+
grad.addColorStop(0, "rgba(158,230,255,.9)");
|
|
814
|
+
grad.addColorStop(1, "rgba(158,230,255,0)");
|
|
815
|
+
ctx.strokeStyle = grad;
|
|
816
|
+
ctx.lineWidth = Math.max(2, radius * 0.9);
|
|
817
|
+
ctx.beginPath();
|
|
818
|
+
ctx.moveTo(0, 0);
|
|
819
|
+
ctx.lineTo(((px - sun.x) / away) * len, ((py - sun.y) / away) * len);
|
|
820
|
+
ctx.stroke();
|
|
821
|
+
}
|
|
822
|
+
ctx.beginPath();
|
|
823
|
+
ctx.arc(0, 0, radius * 0.7, 0, Math.PI * 2);
|
|
824
|
+
ctx.fillStyle = colour;
|
|
825
|
+
ctx.lineWidth = 1.2;
|
|
826
|
+
ctx.strokeStyle = "rgba(0,0,0,.8)";
|
|
827
|
+
ctx.fill();
|
|
828
|
+
ctx.stroke();
|
|
829
|
+
}
|
|
830
|
+
else {
|
|
831
|
+
ctx.beginPath();
|
|
832
|
+
ctx.arc(0, 0, mark.layer === "dwarfs" ? radius * 0.7 : radius, 0, Math.PI * 2);
|
|
833
|
+
ctx.fillStyle = colour;
|
|
834
|
+
ctx.fill();
|
|
835
|
+
ctx.stroke();
|
|
836
|
+
}
|
|
837
|
+
ctx.restore();
|
|
838
|
+
}
|
|
839
|
+
/** The chosen belt body, ringed on the canvas where it is (a belt body is one point of thousands, so it needs more than the choice ring). */
|
|
840
|
+
#drawBeltChoice(ctx, radius) {
|
|
841
|
+
const item = this.#selected;
|
|
842
|
+
if (item?.kind !== "belt")
|
|
843
|
+
return;
|
|
844
|
+
const at = this.#at(item);
|
|
845
|
+
if (!at)
|
|
846
|
+
return;
|
|
847
|
+
ctx.beginPath();
|
|
848
|
+
ctx.arc(at.x, at.y, radius * 0.9, 0, Math.PI * 2);
|
|
849
|
+
ctx.fillStyle = BELTS[this.#belts[item.index].belt].colour;
|
|
850
|
+
ctx.fill();
|
|
851
|
+
ctx.strokeStyle = "rgba(0,0,0,.8)";
|
|
852
|
+
ctx.lineWidth = 1.5;
|
|
853
|
+
ctx.stroke();
|
|
854
|
+
}
|
|
855
|
+
/** The paths: a craft's whole path (the part behind the moment dashed), a comet's orbit, and the orbit of a chosen planet or small body in white; for the chosen one, or for every one shown when the `track` attribute is on and none is chosen. */
|
|
856
|
+
#drawLines(ctx, mks, chosen, ms) {
|
|
857
|
+
const v = this.#view;
|
|
858
|
+
const k = systemScale(v);
|
|
859
|
+
const sun = projectSystem(v, 0, 0);
|
|
860
|
+
ctx.save();
|
|
861
|
+
ctx.beginPath();
|
|
862
|
+
ctx.arc(sun.x, sun.y, k, 0, Math.PI * 2);
|
|
863
|
+
ctx.clip();
|
|
864
|
+
const at = (x, y) => {
|
|
865
|
+
const q = squeezeAu(x, y, this.#extent, this.#spacing);
|
|
866
|
+
return projectSystem(v, q.x, q.y);
|
|
867
|
+
};
|
|
868
|
+
const stroke = (points, colour, strong, dash) => {
|
|
869
|
+
if (points.length < 2)
|
|
870
|
+
return;
|
|
871
|
+
for (const pass of strong ? [0, 1] : [1]) {
|
|
872
|
+
ctx.beginPath();
|
|
873
|
+
points.forEach((p, i) => (i === 0 ? ctx.moveTo(p.x, p.y) : ctx.lineTo(p.x, p.y)));
|
|
874
|
+
ctx.setLineDash(dash ? [4, 4] : []);
|
|
875
|
+
ctx.lineWidth = pass === 0 ? 4 : strong ? 1.8 : 1.2;
|
|
876
|
+
ctx.strokeStyle = pass === 0 ? "rgba(0,0,0,.55)" : strong ? "#ffffff" : colour;
|
|
877
|
+
ctx.globalAlpha = strong || pass === 0 ? 1 : dash ? 0.5 : 0.85;
|
|
878
|
+
ctx.stroke();
|
|
879
|
+
}
|
|
880
|
+
};
|
|
881
|
+
for (const m of mks) {
|
|
882
|
+
const { id, layer } = m.mark;
|
|
883
|
+
const strong = chosen === id;
|
|
884
|
+
if (!strong && !(this.#track && chosen === null))
|
|
885
|
+
continue;
|
|
886
|
+
if (layer === "craft") {
|
|
887
|
+
const path = craftPath(id);
|
|
888
|
+
const now = path.filter((p) => p.ms <= ms).map((p) => at(p.x, p.y));
|
|
889
|
+
const next = path.filter((p) => p.ms >= ms).map((p) => at(p.x, p.y));
|
|
890
|
+
const here = { x: m.px, y: m.py };
|
|
891
|
+
stroke([...now, here], m.colour, strong, true);
|
|
892
|
+
stroke([here, ...next], m.colour, strong, false);
|
|
893
|
+
}
|
|
894
|
+
else if (layer === "comets")
|
|
895
|
+
stroke(systemCometOrbit(id, this.#extent).map((p) => at(p[0], p[1])), m.colour, strong, false);
|
|
896
|
+
else if ((layer === "planets" || layer === "dwarfs") && strong)
|
|
897
|
+
stroke(systemOrbit(id, ms).map((p) => at(p[0], p[1])), m.colour, true, false);
|
|
898
|
+
}
|
|
899
|
+
ctx.setLineDash([]);
|
|
900
|
+
ctx.globalAlpha = 1;
|
|
901
|
+
ctx.restore();
|
|
902
|
+
}
|
|
903
|
+
/** Names beside the bodies, as many as there is room for: the chosen first, then the Sun, planets, craft, dwarfs and comets; a name that would cover a surer one is left out. */
|
|
904
|
+
#drawNames(ctx, mks, lang, chosen) {
|
|
905
|
+
const names = readSurfaceNames(this.getAttribute("names"));
|
|
906
|
+
if (!names.on)
|
|
907
|
+
return;
|
|
908
|
+
const v = this.#view;
|
|
909
|
+
const limit = names.limit ?? Math.round(14 * v.zoom ** 1.4);
|
|
910
|
+
const order = { sun: 1, planets: 2, craft: 3, dwarfs: 4, comets: 5 };
|
|
911
|
+
const list = mks.filter((m) => m.on).sort((a, b) => (a.mark.id === chosen ? -1 : b.mark.id === chosen ? 1 : 0) || (order[a.mark.layer] ?? 9) - (order[b.mark.layer] ?? 9) || a.mark.planeAu - b.mark.planeAu);
|
|
912
|
+
const boxes = list.map((m) => ({ x0: m.px - 6, y0: m.py - 6, x1: m.px + 6, y1: m.py + 6 }));
|
|
913
|
+
const hit = (a) => boxes.some((b) => a.x0 < b.x1 && a.x1 > b.x0 && a.y0 < b.y1 && a.y1 > b.y0);
|
|
914
|
+
ctx.font = "600 11px system-ui, sans-serif";
|
|
915
|
+
ctx.textBaseline = "middle";
|
|
916
|
+
ctx.lineJoin = "round";
|
|
917
|
+
ctx.lineWidth = 3;
|
|
918
|
+
let named = 0;
|
|
919
|
+
for (const m of list) {
|
|
920
|
+
if (named >= limit && m.mark.id !== chosen)
|
|
921
|
+
continue;
|
|
922
|
+
const text = systemName(m.mark.id, lang);
|
|
923
|
+
const width = this.#measureText(text);
|
|
924
|
+
const right = { x0: m.px + 8, y0: m.py - 7, x1: m.px + 8 + width, y1: m.py + 7 };
|
|
925
|
+
const left = { x0: m.px - 8 - width, y0: m.py - 7, x1: m.px - 8, y1: m.py + 7 };
|
|
926
|
+
const fits = (b) => b.x0 >= 0 && b.x1 <= v.width && !hit(b);
|
|
927
|
+
const side = fits(right) ? right : fits(left) ? left : null;
|
|
928
|
+
if (side === null)
|
|
929
|
+
continue;
|
|
930
|
+
boxes.push(side);
|
|
931
|
+
named += 1;
|
|
932
|
+
ctx.textAlign = side === right ? "left" : "right";
|
|
933
|
+
const x = side === right ? side.x0 : side.x1;
|
|
934
|
+
ctx.strokeStyle = "rgba(0,0,0,.75)";
|
|
935
|
+
ctx.strokeText(text, x, m.py);
|
|
936
|
+
ctx.fillStyle = "#fff";
|
|
937
|
+
ctx.fillText(text, x, m.py);
|
|
938
|
+
}
|
|
939
|
+
}
|
|
940
|
+
#announceView() {
|
|
941
|
+
const v = this.#view;
|
|
942
|
+
const key = `${v.centerX.toFixed(3)},${v.centerY.toFixed(3)},${v.zoom.toFixed(2)},${this.#follow ?? ""}`;
|
|
943
|
+
if (key === this.#viewSent)
|
|
944
|
+
return;
|
|
945
|
+
const first = this.#viewSent === "";
|
|
946
|
+
this.#viewSent = key;
|
|
947
|
+
if (!first)
|
|
948
|
+
this.dispatchEvent(new CustomEvent("wakusei-flat-view", { detail: { center: this.#follow, x: v.centerX, y: v.centerY, zoom: v.zoom }, bubbles: true, composed: true }));
|
|
949
|
+
}
|
|
950
|
+
// --- the marks, the words and the controls ---------------------------------------------------------------------------------------------------
|
|
951
|
+
#at(item) {
|
|
952
|
+
if (item === null)
|
|
953
|
+
return null;
|
|
954
|
+
if (item.kind === "belt") {
|
|
955
|
+
const i = item.index;
|
|
956
|
+
if (i * 2 + 1 >= this.#beltXY.length)
|
|
957
|
+
return null;
|
|
958
|
+
const q = squeezeAu(this.#beltXY[i * 2], this.#beltXY[i * 2 + 1], this.#extent, this.#spacing);
|
|
959
|
+
if (q.r > 1)
|
|
960
|
+
return null;
|
|
961
|
+
return projectSystem(this.#view, q.x, q.y);
|
|
962
|
+
}
|
|
963
|
+
const m = this.#mks.find((one) => one.mark.id === item.id);
|
|
964
|
+
return m?.on ? { x: m.px, y: m.py } : null;
|
|
965
|
+
}
|
|
966
|
+
/** The rings round the chosen and the hovered body, and the name beside the one pointed at: a few DOM nodes moved, so they never ask for a draw. */
|
|
967
|
+
#placeMarks() {
|
|
968
|
+
const mark = (ring, item) => {
|
|
969
|
+
if (!ring)
|
|
970
|
+
return null;
|
|
971
|
+
const at = this.#at(item);
|
|
972
|
+
if (!at) {
|
|
973
|
+
ring.hidden = true;
|
|
974
|
+
return null;
|
|
975
|
+
}
|
|
976
|
+
ring.hidden = false;
|
|
977
|
+
ring.style.transform = `translate(${at.x.toFixed(1)}px,${at.y.toFixed(1)}px)`;
|
|
978
|
+
return at;
|
|
979
|
+
};
|
|
980
|
+
const same = (a, b) => a === b || (a !== null && b !== null && a.kind === b.kind && a.id === b.id);
|
|
981
|
+
const picked = this.#cursor ?? this.#selected;
|
|
982
|
+
const at = mark(this.#selRing, picked);
|
|
983
|
+
const over = mark(this.#hovRing, this.#hover && !same(this.#hover, picked) ? this.#hover : null);
|
|
984
|
+
const tip = this.#tip;
|
|
985
|
+
if (!tip)
|
|
986
|
+
return;
|
|
987
|
+
const item = this.#hover ?? picked;
|
|
988
|
+
const where = this.#hover ? (over ?? at) : at;
|
|
989
|
+
if (!item || !where) {
|
|
990
|
+
tip.hidden = true;
|
|
991
|
+
return;
|
|
992
|
+
}
|
|
993
|
+
tip.hidden = false;
|
|
994
|
+
const words = this.#describe(item);
|
|
995
|
+
if (tip.textContent !== words)
|
|
996
|
+
tip.textContent = words;
|
|
997
|
+
const wide = tip.offsetWidth;
|
|
998
|
+
const left = where.x + 14 + wide > this.#view.width ? Math.max(0, where.x - 14 - wide) : where.x + 14;
|
|
999
|
+
const top = Math.max(0, Math.min(this.#view.height - tip.offsetHeight, where.y - 10));
|
|
1000
|
+
tip.style.transform = `translate(${left.toFixed(1)}px,${top.toFixed(1)}px)`;
|
|
1001
|
+
}
|
|
1002
|
+
#describe(item) {
|
|
1003
|
+
const lang = this.language;
|
|
1004
|
+
if (item.kind === "belt") {
|
|
1005
|
+
const body = this.#belts[item.index];
|
|
1006
|
+
if (!body)
|
|
1007
|
+
return item.id;
|
|
1008
|
+
const i = item.index;
|
|
1009
|
+
const d = Math.hypot(this.#beltXY[i * 2] ?? 0, this.#beltXY[i * 2 + 1] ?? 0);
|
|
1010
|
+
return saySystem(lang, "announceBody", { name: body.name, kind: BELTS[body.belt].name[lang], distance: d.toFixed(2), note: "" });
|
|
1011
|
+
}
|
|
1012
|
+
const m = this.#mks.find((one) => one.mark.id === item.id);
|
|
1013
|
+
if (item.id === "sun")
|
|
1014
|
+
return saySystem(lang, "announceSun", { name: systemName("sun", lang) });
|
|
1015
|
+
if (!m)
|
|
1016
|
+
return systemName(item.id, lang);
|
|
1017
|
+
const d = m.mark.distanceAu;
|
|
1018
|
+
return saySystem(lang, "announceBody", { name: systemName(item.id, lang), kind: saySystem(lang, `kind_${m.mark.kind}`), distance: d < 10 ? d.toFixed(2) : d.toFixed(1), note: m.dim ? saySystem(lang, "predicted") : "" });
|
|
1019
|
+
}
|
|
1020
|
+
#build(root) {
|
|
1021
|
+
const d = root.ownerDocument;
|
|
1022
|
+
const bar = Object.assign(d.createElement("div"), { className: "bar" });
|
|
1023
|
+
bar.addEventListener("click", this.#onBar);
|
|
1024
|
+
const stage = Object.assign(d.createElement("div"), { className: "stage" });
|
|
1025
|
+
const map = Object.assign(d.createElement("canvas"), { className: "map" });
|
|
1026
|
+
map.setAttribute("aria-hidden", "true");
|
|
1027
|
+
const dots = Object.assign(d.createElement("canvas"), { className: "dots", tabIndex: 0 });
|
|
1028
|
+
dots.setAttribute("role", "application");
|
|
1029
|
+
const selRing = Object.assign(d.createElement("div"), { className: "ring sel", hidden: true });
|
|
1030
|
+
const hovRing = Object.assign(d.createElement("div"), { className: "ring hov", hidden: true });
|
|
1031
|
+
const tip = Object.assign(d.createElement("div"), { className: "tip", hidden: true });
|
|
1032
|
+
tip.setAttribute("aria-hidden", "true");
|
|
1033
|
+
const live = Object.assign(d.createElement("p"), { className: "sr", id: "live" });
|
|
1034
|
+
live.setAttribute("role", "status");
|
|
1035
|
+
live.setAttribute("aria-live", "polite");
|
|
1036
|
+
const help = Object.assign(d.createElement("p"), { className: "sr", id: "help" });
|
|
1037
|
+
dots.setAttribute("aria-describedby", "help live");
|
|
1038
|
+
stage.append(map, dots, selRing, hovRing, tip);
|
|
1039
|
+
root.append(bar, stage, live, help);
|
|
1040
|
+
this.#bar = bar;
|
|
1041
|
+
this.#stage = stage;
|
|
1042
|
+
this.#mapCanvas = map;
|
|
1043
|
+
this.#dotCanvas = dots;
|
|
1044
|
+
this.#selRing = selRing;
|
|
1045
|
+
this.#hovRing = hovRing;
|
|
1046
|
+
this.#tip = tip;
|
|
1047
|
+
this.#live = live;
|
|
1048
|
+
dots.addEventListener("pointerdown", this.#onDown);
|
|
1049
|
+
dots.addEventListener("pointermove", this.#onMove);
|
|
1050
|
+
dots.addEventListener("pointerup", this.#onUp);
|
|
1051
|
+
dots.addEventListener("pointercancel", this.#onCancel);
|
|
1052
|
+
dots.addEventListener("pointerleave", this.#onLeave);
|
|
1053
|
+
dots.addEventListener("wheel", this.#onWheel, { passive: false });
|
|
1054
|
+
dots.addEventListener("keydown", this.#onKey);
|
|
1055
|
+
dots.addEventListener("blur", () => {
|
|
1056
|
+
if (this.#cursor) {
|
|
1057
|
+
this.#cursor = null;
|
|
1058
|
+
this.#placeMarks();
|
|
1059
|
+
}
|
|
1060
|
+
});
|
|
1061
|
+
}
|
|
1062
|
+
/** Which controls the `controls` attribute asks for. */
|
|
1063
|
+
#wants() {
|
|
1064
|
+
const raw = (this.getAttribute("controls") ?? "").trim().toLowerCase();
|
|
1065
|
+
if (raw === "none" || raw === "off")
|
|
1066
|
+
return new Set();
|
|
1067
|
+
const all = ["centre", "zoom", "orbits", "track", "scale", "chips"];
|
|
1068
|
+
if (raw === "" || raw === "all")
|
|
1069
|
+
return new Set(all);
|
|
1070
|
+
return new Set(raw.split(/[\s,]+/).map((one) => (one === "center" ? "centre" : one)));
|
|
1071
|
+
}
|
|
1072
|
+
/** The words the map and the buttons use, made again when the language, the controls or the layers change. */
|
|
1073
|
+
#words() {
|
|
1074
|
+
const bar = this.#bar;
|
|
1075
|
+
const dots = this.#dotCanvas;
|
|
1076
|
+
if (!bar || !dots)
|
|
1077
|
+
return;
|
|
1078
|
+
const lang = this.language;
|
|
1079
|
+
const wants = this.#wants();
|
|
1080
|
+
dots.setAttribute("aria-label", saySystem(lang, "map"));
|
|
1081
|
+
this.shadowRoot.querySelector("#help").textContent = `${saySystem(lang, "help")} ${saySystem(lang, this.#spacing === "squeezed" ? "scaleNoteSqueezed" : "scaleNoteTrue", { rings: SYSTEM_RINGS.filter((au) => au <= 100).join(", ") })}`;
|
|
1082
|
+
bar.setAttribute("role", "toolbar");
|
|
1083
|
+
bar.setAttribute("aria-label", saySystem(lang, "controls"));
|
|
1084
|
+
const button = (act, label, extra = "") => `<button type="button" data-act="${act}"${extra}>${escape(label)}</button>`;
|
|
1085
|
+
const rows = [];
|
|
1086
|
+
const first = [];
|
|
1087
|
+
if (wants.has("centre"))
|
|
1088
|
+
for (const id of ["sun", "earth"])
|
|
1089
|
+
first.push(button("centre", saySystem(lang, id), ` data-id="${id}" aria-label="${escape(saySystem(lang, "centreOn", { where: saySystem(lang, id) }))}" aria-pressed="false"`));
|
|
1090
|
+
first.push(`<span class="gap"></span>`);
|
|
1091
|
+
if (wants.has("zoom"))
|
|
1092
|
+
first.push(button("zoomout", "−", ` aria-label="${escape(saySystem(lang, "zoomOut"))}"`), button("zoomin", "+", ` aria-label="${escape(saySystem(lang, "zoomIn"))}"`));
|
|
1093
|
+
if (wants.has("orbits"))
|
|
1094
|
+
first.push(button("orbits", saySystem(lang, "orbits"), ` aria-pressed="false"`));
|
|
1095
|
+
if (wants.has("track"))
|
|
1096
|
+
first.push(button("track", saySystem(lang, "track"), ` aria-pressed="false"`));
|
|
1097
|
+
if (wants.has("scale"))
|
|
1098
|
+
first.push(button("scale", saySystem(lang, "scaleSqueezed"), ` aria-pressed="false"`));
|
|
1099
|
+
if (first.length > 1)
|
|
1100
|
+
rows.push(`<div class="row">${first.join("")}</div>`);
|
|
1101
|
+
if (wants.has("chips")) {
|
|
1102
|
+
const counts = { planets: 9, dwarfs: SYSTEM_DWARFS.length, belts: BELT_TOTAL_SAMPLE, comets: COMET_COUNT, craft: SPACECRAFT_IDS.length };
|
|
1103
|
+
const chip = (layer) => `<button type="button" class="chip" data-act="layer" data-layer="${layer}" aria-pressed="false"><span class="swatch" data-layer="${layer}"></span>${escape(saySystem(lang, layer))} <span class="n">${counts[layer].toLocaleString(lang)}</span></button>`;
|
|
1104
|
+
rows.push(`<div class="row chips" role="group" aria-label="${escape(saySystem(lang, "layersFilter"))}">${SYSTEM_LAYERS.map(chip).join("")}</div>`);
|
|
1105
|
+
}
|
|
1106
|
+
bar.innerHTML = rows.join("");
|
|
1107
|
+
// colours go on through the CSS object model: an inline style attribute in the markup would be refused by a strict Content-Security-Policy
|
|
1108
|
+
bar.querySelectorAll(".swatch").forEach((swatch) => swatch.style.setProperty("--c", systemColour("voyager1", swatch.dataset["layer"])));
|
|
1109
|
+
this.#sync();
|
|
1110
|
+
}
|
|
1111
|
+
/** Bring the pressed states of the buttons and chips up to date without making them again (a reader may be using one). */
|
|
1112
|
+
#sync() {
|
|
1113
|
+
const bar = this.#bar;
|
|
1114
|
+
if (!bar)
|
|
1115
|
+
return;
|
|
1116
|
+
bar.querySelectorAll('[data-act="centre"]').forEach((b) => b.setAttribute("aria-pressed", String((this.#follow ?? "sun") === b.dataset["id"])));
|
|
1117
|
+
bar.querySelectorAll('[data-act="orbits"]').forEach((b) => b.setAttribute("aria-pressed", String(this.#orbits)));
|
|
1118
|
+
bar.querySelectorAll('[data-act="track"]').forEach((b) => b.setAttribute("aria-pressed", String(this.#track)));
|
|
1119
|
+
bar.querySelectorAll('[data-act="scale"]').forEach((b) => b.setAttribute("aria-pressed", String(this.#spacing === "squeezed")));
|
|
1120
|
+
bar.querySelectorAll('[data-act="layer"]').forEach((b) => b.setAttribute("aria-pressed", String(this.#layers.has(b.dataset["layer"]))));
|
|
1121
|
+
bar.querySelector('[data-act="zoomin"]')?.toggleAttribute("disabled", this.#view.zoom >= MAX_ZOOM);
|
|
1122
|
+
bar.querySelector('[data-act="zoomout"]')?.toggleAttribute("disabled", this.#view.zoom <= 1);
|
|
1123
|
+
if (this.#dotCanvas)
|
|
1124
|
+
this.#dotCanvas.toggleAttribute("data-zoomed", this.#view.zoom > 1);
|
|
1125
|
+
}
|
|
1126
|
+
#onBar = (event) => {
|
|
1127
|
+
const target = event.target?.closest("button[data-act]");
|
|
1128
|
+
if (!target || target.hasAttribute("disabled"))
|
|
1129
|
+
return;
|
|
1130
|
+
const act = target.dataset["act"];
|
|
1131
|
+
const v = this.#view;
|
|
1132
|
+
if (act === "centre")
|
|
1133
|
+
this.center = target.dataset["id"] ?? null;
|
|
1134
|
+
else if (act === "zoomin")
|
|
1135
|
+
this.#setView(zoomSystemAbout(v, 1.6));
|
|
1136
|
+
else if (act === "zoomout")
|
|
1137
|
+
this.#setView(zoomSystemAbout(v, 1 / 1.6));
|
|
1138
|
+
else if (act === "orbits") {
|
|
1139
|
+
this.#orbits = !this.#orbits;
|
|
1140
|
+
this.#mapKey = "";
|
|
1141
|
+
this.#sync();
|
|
1142
|
+
this.#request();
|
|
1143
|
+
}
|
|
1144
|
+
else if (act === "track") {
|
|
1145
|
+
this.#track = !this.#track;
|
|
1146
|
+
this.#sync();
|
|
1147
|
+
this.#request();
|
|
1148
|
+
}
|
|
1149
|
+
else if (act === "scale") {
|
|
1150
|
+
this.#spacing = this.#spacing === "squeezed" ? "true" : "squeezed";
|
|
1151
|
+
this.#mapKey = "";
|
|
1152
|
+
this.#words();
|
|
1153
|
+
this.#request();
|
|
1154
|
+
}
|
|
1155
|
+
else if (act === "layer") {
|
|
1156
|
+
const layer = target.dataset["layer"];
|
|
1157
|
+
if (this.#layers.has(layer))
|
|
1158
|
+
this.#layers.delete(layer);
|
|
1159
|
+
else
|
|
1160
|
+
this.#layers.add(layer);
|
|
1161
|
+
// a choice whose mark has just gone is let go
|
|
1162
|
+
if (this.#selected?.kind === "belt" && !this.#layers.has("belts"))
|
|
1163
|
+
this.#selected = null;
|
|
1164
|
+
const chosen = this.#selected;
|
|
1165
|
+
if (chosen?.kind === "mark" && this.#layerOf(chosen.id) === layer && !this.#layers.has(layer))
|
|
1166
|
+
this.#selected = null;
|
|
1167
|
+
this.#mapKey = "";
|
|
1168
|
+
this.#fetch();
|
|
1169
|
+
this.#sync();
|
|
1170
|
+
this.#request();
|
|
1171
|
+
this.#schedule();
|
|
1172
|
+
this.dispatchEvent(new CustomEvent("wakusei-flat-filter", { detail: { layers: [...this.#layers] }, bubbles: true, composed: true }));
|
|
1173
|
+
}
|
|
1174
|
+
};
|
|
1175
|
+
/** The layer an id is in, or undefined. */
|
|
1176
|
+
#layerOf(id) {
|
|
1177
|
+
const layer = this.#mks.find((m) => m.mark.id === id)?.mark.layer;
|
|
1178
|
+
return layer === "sun" ? undefined : layer;
|
|
1179
|
+
}
|
|
1180
|
+
#setView(view) {
|
|
1181
|
+
this.#view = clampSystemView(view);
|
|
1182
|
+
this.#mapKey = "";
|
|
1183
|
+
this.#sync();
|
|
1184
|
+
this.#request();
|
|
1185
|
+
}
|
|
1186
|
+
// --- drawing, asked for ----------------------------------------------------------------------------------------------------------------------
|
|
1187
|
+
/** 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. */
|
|
1188
|
+
#request() {
|
|
1189
|
+
if (!this.#connected)
|
|
1190
|
+
return;
|
|
1191
|
+
if (!this.#visibleNow())
|
|
1192
|
+
return;
|
|
1193
|
+
if (this.#frame)
|
|
1194
|
+
return;
|
|
1195
|
+
this.#frame = requestAnimationFrame(() => {
|
|
1196
|
+
this.#frame = 0;
|
|
1197
|
+
this.#draw();
|
|
1198
|
+
});
|
|
1199
|
+
}
|
|
1200
|
+
// --- the pointer and the keyboard ------------------------------------------------------------------------------------------------------------
|
|
1201
|
+
#local(event) {
|
|
1202
|
+
const box = this.#dotCanvas.getBoundingClientRect();
|
|
1203
|
+
return { x: event.clientX - box.left, y: event.clientY - box.top };
|
|
1204
|
+
}
|
|
1205
|
+
/** The body drawn nearest to a point, within `reach` pixels; a belt body only where no body is, and within a shorter reach. */
|
|
1206
|
+
#hit(x, y, reach) {
|
|
1207
|
+
let best = null;
|
|
1208
|
+
let gap = reach * reach;
|
|
1209
|
+
for (const m of this.#mks) {
|
|
1210
|
+
if (!m.on)
|
|
1211
|
+
continue;
|
|
1212
|
+
const d = (m.px - x) ** 2 + (m.py - y) ** 2;
|
|
1213
|
+
if (d <= gap) {
|
|
1214
|
+
gap = d;
|
|
1215
|
+
best = { kind: "mark", id: m.mark.id };
|
|
1216
|
+
}
|
|
1217
|
+
}
|
|
1218
|
+
if (best !== null || !this.#layers.has("belts") || this.#belts.length === 0)
|
|
1219
|
+
return best;
|
|
1220
|
+
const v = this.#view;
|
|
1221
|
+
const k = systemScale(v);
|
|
1222
|
+
const near = (reach * 0.7) ** 2;
|
|
1223
|
+
let bestBelt = near;
|
|
1224
|
+
let index = -1;
|
|
1225
|
+
const wanted = new Set(this.#beltIds);
|
|
1226
|
+
for (let i = 0; i < this.#belts.length; i += 1) {
|
|
1227
|
+
if (!wanted.has(this.#belts[i].belt))
|
|
1228
|
+
continue;
|
|
1229
|
+
const q = squeezeAu(this.#beltXY[i * 2], this.#beltXY[i * 2 + 1], this.#extent, this.#spacing);
|
|
1230
|
+
if (q.r > 1)
|
|
1231
|
+
continue;
|
|
1232
|
+
const d = (v.width / 2 + (q.x - v.centerX) * k - x) ** 2 + (v.height / 2 - (q.y - v.centerY) * k - y) ** 2;
|
|
1233
|
+
if (d <= bestBelt) {
|
|
1234
|
+
bestBelt = d;
|
|
1235
|
+
index = i;
|
|
1236
|
+
}
|
|
1237
|
+
}
|
|
1238
|
+
return index >= 0 ? { kind: "belt", id: this.#belts[index].name, index } : null;
|
|
1239
|
+
}
|
|
1240
|
+
#pick(item, via) {
|
|
1241
|
+
let detail;
|
|
1242
|
+
if (item === null)
|
|
1243
|
+
detail = { kind: null, id: null, name: null, layer: null, distanceAu: null, x: null, y: null, predicted: null, via };
|
|
1244
|
+
else if (item.kind === "belt") {
|
|
1245
|
+
const body = this.#belts[item.index];
|
|
1246
|
+
const x = this.#beltXY[item.index * 2] ?? 0;
|
|
1247
|
+
const y = this.#beltXY[item.index * 2 + 1] ?? 0;
|
|
1248
|
+
detail = { kind: "belt", id: item.id, name: item.id, layer: "belts", ...(body ? { belt: body.belt } : {}), distanceAu: Math.hypot(x, y), x, y, predicted: false, via };
|
|
1249
|
+
}
|
|
1250
|
+
else {
|
|
1251
|
+
const m = this.#mks.find((one) => one.mark.id === item.id);
|
|
1252
|
+
detail = { kind: m?.mark.kind ?? null, id: item.id, name: systemName(item.id, this.language), layer: m?.mark.layer ?? null, distanceAu: m?.mark.distanceAu ?? null, x: m?.mark.au[0] ?? null, y: m?.mark.au[1] ?? null, predicted: m ? m.dim : null, via };
|
|
1253
|
+
}
|
|
1254
|
+
this.dispatchEvent(new CustomEvent("wakusei-flat-pick", { detail, bubbles: true, composed: true }));
|
|
1255
|
+
}
|
|
1256
|
+
#choose(item, via) {
|
|
1257
|
+
const same = item !== null && this.#selected !== null && item.kind === this.#selected.kind && item.id === this.#selected.id;
|
|
1258
|
+
if (item === null && this.#selected === null)
|
|
1259
|
+
return;
|
|
1260
|
+
if (!same) {
|
|
1261
|
+
this.#selected = item;
|
|
1262
|
+
this.#request();
|
|
1263
|
+
}
|
|
1264
|
+
this.#sync();
|
|
1265
|
+
this.#placeMarks();
|
|
1266
|
+
if (item) {
|
|
1267
|
+
if (this.#live)
|
|
1268
|
+
this.#live.textContent = this.#describe(item);
|
|
1269
|
+
this.#pick(item, via);
|
|
1270
|
+
}
|
|
1271
|
+
else if (!same)
|
|
1272
|
+
this.#pick(null, via);
|
|
1273
|
+
}
|
|
1274
|
+
#onDown = (event) => {
|
|
1275
|
+
if (event.pointerType === "mouse" && event.button !== 0)
|
|
1276
|
+
return;
|
|
1277
|
+
const at = this.#local(event);
|
|
1278
|
+
this.#pointers.set(event.pointerId, at);
|
|
1279
|
+
try {
|
|
1280
|
+
this.#dotCanvas.setPointerCapture(event.pointerId);
|
|
1281
|
+
}
|
|
1282
|
+
catch {
|
|
1283
|
+
// a pointer that is already gone
|
|
1284
|
+
}
|
|
1285
|
+
this.#drag = { x: at.x, y: at.y, moved: this.#pointers.size === 2, pinch: this.#pointers.size === 2 ? this.#spread() : 0 };
|
|
1286
|
+
this.#dotCanvas.style.cursor = "grabbing";
|
|
1287
|
+
};
|
|
1288
|
+
#spread() {
|
|
1289
|
+
const [a, b] = [...this.#pointers.values()];
|
|
1290
|
+
return a && b ? Math.hypot(a.x - b.x, a.y - b.y) : 0;
|
|
1291
|
+
}
|
|
1292
|
+
#onMove = (event) => {
|
|
1293
|
+
const at = this.#local(event);
|
|
1294
|
+
if (!this.#pointers.has(event.pointerId)) {
|
|
1295
|
+
if (event.pointerType === "mouse")
|
|
1296
|
+
this.#hoverAt(at.x, at.y);
|
|
1297
|
+
return;
|
|
1298
|
+
}
|
|
1299
|
+
const before = this.#pointers.get(event.pointerId);
|
|
1300
|
+
this.#pointers.set(event.pointerId, at);
|
|
1301
|
+
const drag = this.#drag;
|
|
1302
|
+
if (!drag)
|
|
1303
|
+
return;
|
|
1304
|
+
if (this.#pointers.size >= 2) {
|
|
1305
|
+
const spread = this.#spread();
|
|
1306
|
+
const [a, b] = [...this.#pointers.values()];
|
|
1307
|
+
if (drag.pinch > 0 && spread > 0 && a && b) {
|
|
1308
|
+
this.#view = zoomSystemAbout(this.#view, spread / drag.pinch, (a.x + b.x) / 2, (a.y + b.y) / 2);
|
|
1309
|
+
drag.pinch = spread;
|
|
1310
|
+
drag.moved = true;
|
|
1311
|
+
this.#mapKey = "";
|
|
1312
|
+
this.#sync();
|
|
1313
|
+
this.#request();
|
|
1314
|
+
}
|
|
1315
|
+
return;
|
|
1316
|
+
}
|
|
1317
|
+
if (!drag.moved && Math.hypot(at.x - drag.x, at.y - drag.y) < TAP_SLOP)
|
|
1318
|
+
return;
|
|
1319
|
+
drag.moved = true;
|
|
1320
|
+
const k = systemScale(this.#view);
|
|
1321
|
+
// a drag lets go of a followed body
|
|
1322
|
+
this.#follow = null;
|
|
1323
|
+
this.#view = clampSystemView({ ...this.#view, centerX: this.#view.centerX - (at.x - before.x) / k, centerY: this.#view.centerY + (at.y - before.y) / k });
|
|
1324
|
+
this.#mapKey = "";
|
|
1325
|
+
this.#hover = null;
|
|
1326
|
+
this.#sync();
|
|
1327
|
+
this.#request();
|
|
1328
|
+
};
|
|
1329
|
+
#onUp = (event) => {
|
|
1330
|
+
const at = this.#local(event);
|
|
1331
|
+
const drag = this.#drag;
|
|
1332
|
+
this.#pointers.delete(event.pointerId);
|
|
1333
|
+
try {
|
|
1334
|
+
this.#dotCanvas.releasePointerCapture(event.pointerId);
|
|
1335
|
+
}
|
|
1336
|
+
catch {
|
|
1337
|
+
// already released
|
|
1338
|
+
}
|
|
1339
|
+
if (this.#pointers.size === 0) {
|
|
1340
|
+
this.#drag = null;
|
|
1341
|
+
this.#dotCanvas.style.cursor = "";
|
|
1342
|
+
if (drag && !drag.moved) {
|
|
1343
|
+
// a tap: a finger needs more room than a mouse
|
|
1344
|
+
const hit = this.#hit(at.x, at.y, event.pointerType === "touch" ? 22 : 9);
|
|
1345
|
+
this.#hover = null;
|
|
1346
|
+
this.#choose(hit, "tap");
|
|
1347
|
+
}
|
|
1348
|
+
}
|
|
1349
|
+
else if (drag)
|
|
1350
|
+
drag.pinch = this.#spread();
|
|
1351
|
+
};
|
|
1352
|
+
#onCancel = (event) => {
|
|
1353
|
+
this.#pointers.delete(event.pointerId);
|
|
1354
|
+
if (this.#pointers.size === 0) {
|
|
1355
|
+
this.#drag = null;
|
|
1356
|
+
this.#dotCanvas.style.cursor = "";
|
|
1357
|
+
}
|
|
1358
|
+
};
|
|
1359
|
+
#onLeave = (event) => {
|
|
1360
|
+
if (event.pointerType !== "mouse" || this.#hover === null)
|
|
1361
|
+
return;
|
|
1362
|
+
this.#hover = null;
|
|
1363
|
+
this.#placeMarks();
|
|
1364
|
+
};
|
|
1365
|
+
#hoverAt(x, y) {
|
|
1366
|
+
const hit = this.#hit(x, y, 8);
|
|
1367
|
+
if ((hit === null && this.#hover === null) || (hit !== null && this.#hover !== null && hit.kind === this.#hover.kind && hit.id === this.#hover.id))
|
|
1368
|
+
return;
|
|
1369
|
+
this.#hover = hit;
|
|
1370
|
+
this.#placeMarks();
|
|
1371
|
+
if (hit)
|
|
1372
|
+
this.#pick(hit, "hover");
|
|
1373
|
+
}
|
|
1374
|
+
#onWheel = (event) => {
|
|
1375
|
+
// a plain wheel scrolls the page; Control or Command with the wheel (and a trackpad's pinch, which arrives as one) zooms
|
|
1376
|
+
if (!event.ctrlKey && !event.metaKey)
|
|
1377
|
+
return;
|
|
1378
|
+
event.preventDefault();
|
|
1379
|
+
const at = this.#local(event);
|
|
1380
|
+
this.#setView(zoomSystemAbout(this.#view, Math.exp(-event.deltaY * 0.01), at.x, at.y));
|
|
1381
|
+
};
|
|
1382
|
+
#onKey = (event) => {
|
|
1383
|
+
const key = event.key;
|
|
1384
|
+
if (key === "+" || key === "=") {
|
|
1385
|
+
event.preventDefault();
|
|
1386
|
+
this.#setView(zoomSystemAbout(this.#view, 1.6));
|
|
1387
|
+
return;
|
|
1388
|
+
}
|
|
1389
|
+
if (key === "-" || key === "_") {
|
|
1390
|
+
event.preventDefault();
|
|
1391
|
+
this.#setView(zoomSystemAbout(this.#view, 1 / 1.6));
|
|
1392
|
+
return;
|
|
1393
|
+
}
|
|
1394
|
+
if (key === "Escape") {
|
|
1395
|
+
if (this.#cursor || this.#selected) {
|
|
1396
|
+
this.#cursor = null;
|
|
1397
|
+
this.#choose(null, "key");
|
|
1398
|
+
}
|
|
1399
|
+
return;
|
|
1400
|
+
}
|
|
1401
|
+
if (key === "Enter" || key === " ") {
|
|
1402
|
+
event.preventDefault();
|
|
1403
|
+
const item = this.#cursor ?? this.#selected;
|
|
1404
|
+
if (item)
|
|
1405
|
+
this.#choose(item, "key");
|
|
1406
|
+
return;
|
|
1407
|
+
}
|
|
1408
|
+
const step = key === "ArrowRight" || key === "ArrowDown" ? 1 : key === "ArrowLeft" || key === "ArrowUp" ? -1 : key === "PageDown" ? 10 : key === "PageUp" ? -10 : 0;
|
|
1409
|
+
if (step === 0 && key !== "Home" && key !== "End")
|
|
1410
|
+
return;
|
|
1411
|
+
event.preventDefault();
|
|
1412
|
+
// the bodies on show, from the left of the map to the right (a belt body is reached by the pointer: the belts are a cloud, not a list)
|
|
1413
|
+
const list = [];
|
|
1414
|
+
for (const m of this.#mks)
|
|
1415
|
+
if (m.on)
|
|
1416
|
+
list.push({ item: { kind: "mark", id: m.mark.id }, x: m.px, y: m.py });
|
|
1417
|
+
if (list.length === 0)
|
|
1418
|
+
return;
|
|
1419
|
+
list.sort((a, b) => a.x - b.x || a.y - b.y);
|
|
1420
|
+
const current = this.#cursor ?? this.#selected;
|
|
1421
|
+
const at = current ? list.findIndex((one) => one.item.kind === current.kind && one.item.id === current.id) : -1;
|
|
1422
|
+
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));
|
|
1423
|
+
this.#cursor = list[next].item;
|
|
1424
|
+
this.#placeMarks();
|
|
1425
|
+
if (this.#live)
|
|
1426
|
+
this.#live.textContent = this.#describe(this.#cursor);
|
|
1427
|
+
this.#pick(this.#cursor, "key");
|
|
1428
|
+
};
|
|
1429
|
+
/** The distance in au under a point of the box, in the map's present scale: for a page that reads where a pointer is. */
|
|
1430
|
+
auAt(x, y) {
|
|
1431
|
+
const p = unprojectSystem(this.#view, x, y);
|
|
1432
|
+
return radiusAu(Math.hypot(p.x, p.y), this.#extent, this.#spacing);
|
|
1433
|
+
}
|
|
1434
|
+
}
|
|
1435
|
+
_a = WakuseiFlatSystem;
|