@beeson-group/tycho 0.1.0 → 0.2.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/AUTHORS +7 -0
- package/LICENSE +202 -0
- package/NOTICE +14 -0
- package/README.md +38 -0
- package/THIRD_PARTY_NOTICES.md +68 -0
- package/dist/tycho.esm.js +515 -261
- package/dist/tycho.js +515 -261
- package/dist/types/container.d.ts +64 -0
- package/dist/types/errors.d.ts +10 -0
- package/dist/types/evaluate/clock.d.ts +30 -0
- package/dist/types/evaluate/frames.d.ts +63 -0
- package/dist/types/evaluate/index.d.ts +14 -0
- package/dist/types/evaluate/layers.d.ts +47 -0
- package/dist/types/evaluate/resolve.d.ts +39 -0
- package/dist/types/evaluate/sets.d.ts +49 -0
- package/dist/types/evaluate/types.d.ts +36 -0
- package/dist/types/index.d.ts +22 -0
- package/dist/types/runtime/colormaps.d.ts +10 -0
- package/dist/types/runtime/colors.d.ts +94 -0
- package/dist/types/runtime/element.d.ts +224 -0
- package/dist/types/runtime/export.d.ts +119 -0
- package/dist/types/runtime/geometry.d.ts +214 -0
- package/dist/types/runtime/icons.d.ts +56 -0
- package/dist/types/runtime/layout.d.ts +34 -0
- package/dist/types/runtime/overlay.d.ts +33 -0
- package/dist/types/runtime/readouts.d.ts +129 -0
- package/dist/types/runtime/renderer.d.ts +303 -0
- package/dist/types/runtime/state.d.ts +106 -0
- package/dist/types/runtime/tiles.d.ts +33 -0
- package/dist/types/scene.d.ts +37 -0
- package/dist/types/scene.gen.d.ts +440 -0
- package/dist/types/validate.d.ts +9 -0
- package/package.json +63 -35
- package/src/colormaps.ts +0 -42
- package/src/container.ts +0 -126
- package/src/element.ts +0 -1034
- package/src/frames.ts +0 -204
- package/src/index.ts +0 -14
- package/src/layout.ts +0 -82
- package/src/viewer.ts +0 -864
package/src/element.ts
DELETED
|
@@ -1,1034 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* <tycho-viewer>: the embeddable web component.
|
|
3
|
-
*
|
|
4
|
-
* Attributes:
|
|
5
|
-
* data "#id" of an embedded script tag, or a URL to a sidecar container
|
|
6
|
-
* title page title shown in the header; also keys the remembered layout
|
|
7
|
-
* layout JSON of a saved layout (tree, panels with frame and branch, family)
|
|
8
|
-
* frames space-separated frame ids to open at start when no layout is given
|
|
9
|
-
* kiosk hide all controls and autoplay (for embedding in a slide)
|
|
10
|
-
*
|
|
11
|
-
* Panels tile the stage as a binary split tree (see layout.ts). Drag a frame
|
|
12
|
-
* chip onto a panel's edge to split it there, onto its centre to replace it;
|
|
13
|
-
* drag a panel by its header to move it; drag the gutters to resize. Each
|
|
14
|
-
* panel owns its frame and its family branch, so periapsis and apoapsis can
|
|
15
|
-
* sit side by side. The eccentricity slider, the family tabs, the clock and
|
|
16
|
-
* playback are shared; every panel shows its branch's member nearest the
|
|
17
|
-
* slider's eccentricity. The layout is remembered in the browser.
|
|
18
|
-
*/
|
|
19
|
-
|
|
20
|
-
import { Container, type DatasetRecord, loadContainer } from './container';
|
|
21
|
-
import { FRAMES, type ER3BPSystem, type FrameId, elapsedTime, trueAnomalyFromTime } from './frames';
|
|
22
|
-
import {
|
|
23
|
-
type DropZone,
|
|
24
|
-
type LayoutNode,
|
|
25
|
-
hasPanel,
|
|
26
|
-
isLayoutNode,
|
|
27
|
-
panelAreas,
|
|
28
|
-
panelIds,
|
|
29
|
-
removePanel,
|
|
30
|
-
setRatio,
|
|
31
|
-
splitAt,
|
|
32
|
-
zoneFor,
|
|
33
|
-
} from './layout';
|
|
34
|
-
import { type FamilyMode, type FamilyTracks, OrbitViewer } from './viewer';
|
|
35
|
-
|
|
36
|
-
interface FamilyDataset extends DatasetRecord {
|
|
37
|
-
kind: 'periodic_orbit_family';
|
|
38
|
-
group: string;
|
|
39
|
-
branch: string;
|
|
40
|
-
system: { model: string; mu: number; f0: number; primary: string; secondary: string };
|
|
41
|
-
independent_variable: { symbol: string; view: string; per_member?: boolean };
|
|
42
|
-
parameter: { name: string; symbol: string; view: string };
|
|
43
|
-
positions: { view: string };
|
|
44
|
-
closure_error?: { view: string };
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
type Spacing = 'anomaly' | 'time' | 'arclength';
|
|
48
|
-
|
|
49
|
-
interface Panel {
|
|
50
|
-
id: string;
|
|
51
|
-
frame: FrameId;
|
|
52
|
-
branch: string;
|
|
53
|
-
host: HTMLDivElement;
|
|
54
|
-
body: HTMLDivElement;
|
|
55
|
-
title: HTMLSpanElement;
|
|
56
|
-
meta: HTMLSpanElement;
|
|
57
|
-
branchSeg: Seg<string>;
|
|
58
|
-
drop: HTMLDivElement;
|
|
59
|
-
viewer: OrbitViewer;
|
|
60
|
-
tracks: FamilyTracks | null;
|
|
61
|
-
member: number;
|
|
62
|
-
sys: ER3BPSystem;
|
|
63
|
-
fRel: Float32Array;
|
|
64
|
-
period: number;
|
|
65
|
-
tauPeriod: number;
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
interface SavedLayout {
|
|
69
|
-
version: 1;
|
|
70
|
-
group: string;
|
|
71
|
-
e: number;
|
|
72
|
-
tree: LayoutNode | null;
|
|
73
|
-
panels: Record<string, { frame: FrameId; branch: string }>;
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
type DragPayload = { kind: 'frame'; frame: FrameId } | { kind: 'panel'; id: string };
|
|
77
|
-
|
|
78
|
-
/** Frames per revolution for the readout and keyboard stepping; matches the reference tool's 400 frames. */
|
|
79
|
-
const STEPS_PER_REV = 400;
|
|
80
|
-
/** At speed 1 a revolution takes this many display frames (four times slower than one frame per step). */
|
|
81
|
-
const SLOWDOWN = 4;
|
|
82
|
-
const MAX_PANELS = 8;
|
|
83
|
-
const BRANCHES = ['periapsis', 'apoapsis'];
|
|
84
|
-
|
|
85
|
-
const STYLE = `
|
|
86
|
-
:host { display: block; position: relative; background: #000; color: #d8d8d8;
|
|
87
|
-
font: 13px/1.4 "Helvetica Neue", Arial, sans-serif; overflow: hidden; }
|
|
88
|
-
* { box-sizing: border-box; }
|
|
89
|
-
[hidden] { display: none !important; }
|
|
90
|
-
.root { position: absolute; inset: 0; display: grid; grid-template-rows: auto auto 1fr auto; }
|
|
91
|
-
.root.kiosk { grid-template-rows: 1fr; }
|
|
92
|
-
.root.kiosk header, .root.kiosk .controls, .root.kiosk footer, .root.kiosk .phead { display: none; }
|
|
93
|
-
header { display: flex; flex-wrap: wrap; align-items: baseline; gap: 6px 18px; padding: 10px 14px 4px; }
|
|
94
|
-
header h1 { font-size: 17px; font-weight: 600; margin: 0; letter-spacing: 0.01em; }
|
|
95
|
-
header .caption { color: #9a9a9a; font-variant-numeric: tabular-nums; }
|
|
96
|
-
.controls { display: flex; flex-wrap: wrap; align-items: center; gap: 8px 16px; padding: 6px 14px 8px; }
|
|
97
|
-
.seg { display: inline-flex; border: 1px solid #333; border-radius: 6px; overflow: hidden; }
|
|
98
|
-
.seg button { background: #0d0d0d; color: #bbb; border: 0; padding: 5px 11px; cursor: pointer; font: inherit; }
|
|
99
|
-
.seg button + button { border-left: 1px solid #333; }
|
|
100
|
-
.seg button[aria-pressed="true"] { background: #2b2b2b; color: #fff; }
|
|
101
|
-
.seg.mini button { padding: 1px 7px; font-size: 11px; }
|
|
102
|
-
.seg button:hover { color: #fff; }
|
|
103
|
-
label.field { display: inline-flex; align-items: center; gap: 8px; }
|
|
104
|
-
label.field span.k { color: #9a9a9a; }
|
|
105
|
-
input[type=range] { accent-color: #E69F00; width: 220px; }
|
|
106
|
-
input[type=range].scrub { flex: 1; min-width: 160px; width: auto; }
|
|
107
|
-
.chips { display: inline-flex; flex-wrap: wrap; gap: 6px; align-items: center; }
|
|
108
|
-
.chips .k { color: #9a9a9a; margin-right: 2px; }
|
|
109
|
-
.chip { display: inline-flex; align-items: center; gap: 6px; background: #0d0d0d; color: #ccc; border: 1px solid #333;
|
|
110
|
-
border-radius: 6px; padding: 4px 10px 4px 8px; cursor: grab; user-select: none; touch-action: none; }
|
|
111
|
-
.chip:hover { color: #fff; border-color: #666; }
|
|
112
|
-
.chip .grip { color: #555; font-size: 11px; letter-spacing: -2px; }
|
|
113
|
-
.chip .n { min-width: 16px; text-align: center; font-size: 11px; color: #ffd9a8; background: #3a2a14; border-radius: 8px; padding: 0 5px; }
|
|
114
|
-
.chip .n:empty { display: none; }
|
|
115
|
-
.stage { position: relative; min-height: 120px; background: #141414; display: flex; }
|
|
116
|
-
.stage > .split, .stage > .panel { flex: 1 1 0; }
|
|
117
|
-
.split { display: flex; min-width: 0; min-height: 0; }
|
|
118
|
-
.split.row { flex-direction: row; }
|
|
119
|
-
.split.col { flex-direction: column; }
|
|
120
|
-
.gutter { flex: 0 0 6px; background: #141414; touch-action: none; }
|
|
121
|
-
.split.row > .gutter { cursor: col-resize; }
|
|
122
|
-
.split.col > .gutter { cursor: row-resize; }
|
|
123
|
-
.gutter:hover, .gutter.active { background: #3a2a14; }
|
|
124
|
-
.panel { position: relative; display: flex; flex-direction: column; min-width: 0; min-height: 0; background: #000; }
|
|
125
|
-
.phead { display: flex; align-items: center; gap: 8px; padding: 3px 8px; background: #0a0a0a; border-bottom: 1px solid #1a1a1a;
|
|
126
|
-
cursor: grab; user-select: none; touch-action: none; font-size: 12px; white-space: nowrap; min-width: 0; }
|
|
127
|
-
.phead .t { font-weight: 600; color: #ddd; flex: 0 1 auto; min-width: 3em; overflow: hidden; text-overflow: ellipsis; }
|
|
128
|
-
.phead .m { color: #8a8a8a; font-variant-numeric: tabular-nums; flex: 0 10 auto; min-width: 0; overflow: hidden; text-overflow: ellipsis; }
|
|
129
|
-
.phead .sp { flex: 1 0 0; }
|
|
130
|
-
.phead .seg, .phead .x { flex: 0 0 auto; }
|
|
131
|
-
.phead .x { background: none; border: 0; color: #777; cursor: pointer; font: inherit; font-size: 14px; padding: 0 4px; }
|
|
132
|
-
.phead .x:hover { color: #fff; }
|
|
133
|
-
.phead .home { display: inline-flex; align-items: center; }
|
|
134
|
-
.pbody { position: relative; flex: 1 1 0; min-height: 0; }
|
|
135
|
-
.drop { position: absolute; inset: 0; background: rgba(230,159,0,0.18); border: 2px solid rgba(230,159,0,0.7); pointer-events: none; z-index: 2; }
|
|
136
|
-
.drop.centre { background: rgba(86,180,233,0.16); border-color: rgba(86,180,233,0.7); }
|
|
137
|
-
.ghostchip { position: fixed; z-index: 10; pointer-events: none; background: #2b2b2b; color: #fff; border: 1px solid #666;
|
|
138
|
-
border-radius: 6px; padding: 4px 10px; font-size: 12px; transform: translate(12px, 12px); }
|
|
139
|
-
.hint { flex: 1; display: grid; place-items: center; color: #777; background: #000; }
|
|
140
|
-
.hint.over { color: #ffd9a8; background: #120d05; }
|
|
141
|
-
footer { display: flex; flex-wrap: wrap; align-items: center; gap: 10px 12px; padding: 8px 14px 10px; border-top: 1px solid #1a1a1a; }
|
|
142
|
-
footer button { background: #0d0d0d; color: #ddd; border: 1px solid #333; border-radius: 6px; padding: 5px 12px; cursor: pointer; font: inherit; min-width: 68px; }
|
|
143
|
-
footer button:hover { border-color: #666; color: #fff; }
|
|
144
|
-
.readout { color: #bbb; font-variant-numeric: tabular-nums; white-space: nowrap; }
|
|
145
|
-
.fps { color: #8a8a8a; font-variant-numeric: tabular-nums; min-width: 5.5em; text-align: right; }
|
|
146
|
-
.legend { display: inline-flex; gap: 10px; color: #9a9a9a; }
|
|
147
|
-
.legend i { display: inline-block; width: 8px; height: 8px; border-radius: 50%; margin-right: 4px; }
|
|
148
|
-
select { background: #0d0d0d; color: #ddd; border: 1px solid #333; border-radius: 6px; padding: 4px 6px; font: inherit; }
|
|
149
|
-
.check { display: inline-flex; align-items: center; gap: 5px; color: #bbb; cursor: pointer; }
|
|
150
|
-
.check input { accent-color: #E69F00; }
|
|
151
|
-
.status { position: absolute; inset: 0; display: grid; place-items: center; color: #888; }
|
|
152
|
-
.warn { color: #e0a24a; }
|
|
153
|
-
`;
|
|
154
|
-
|
|
155
|
-
interface Seg<T extends string> {
|
|
156
|
-
el: HTMLDivElement;
|
|
157
|
-
set(id: T): void;
|
|
158
|
-
}
|
|
159
|
-
|
|
160
|
-
function seg<T extends string>(items: { id: T; label: string }[], onPick: (id: T) => void, cls = 'seg'): Seg<T> {
|
|
161
|
-
const el = document.createElement('div');
|
|
162
|
-
el.className = cls;
|
|
163
|
-
const buttons = new Map<T, HTMLButtonElement>();
|
|
164
|
-
for (const it of items) {
|
|
165
|
-
const b = document.createElement('button');
|
|
166
|
-
b.type = 'button';
|
|
167
|
-
b.textContent = it.label;
|
|
168
|
-
b.setAttribute('aria-pressed', 'false');
|
|
169
|
-
b.addEventListener('pointerdown', (ev) => ev.stopPropagation());
|
|
170
|
-
b.addEventListener('click', () => onPick(it.id));
|
|
171
|
-
buttons.set(it.id, b);
|
|
172
|
-
el.appendChild(b);
|
|
173
|
-
}
|
|
174
|
-
return {
|
|
175
|
-
el,
|
|
176
|
-
set(id: T) {
|
|
177
|
-
for (const [k, b] of buttons) b.setAttribute('aria-pressed', String(k === id));
|
|
178
|
-
},
|
|
179
|
-
};
|
|
180
|
-
}
|
|
181
|
-
|
|
182
|
-
function select<T extends string>(options: { id: T; label: string }[], initial: T, onChange: (v: T) => void): HTMLSelectElement {
|
|
183
|
-
const s = document.createElement('select');
|
|
184
|
-
for (const o of options) {
|
|
185
|
-
const opt = document.createElement('option');
|
|
186
|
-
opt.value = o.id;
|
|
187
|
-
opt.textContent = o.label;
|
|
188
|
-
if (o.id === initial) opt.selected = true;
|
|
189
|
-
s.appendChild(opt);
|
|
190
|
-
}
|
|
191
|
-
s.addEventListener('change', () => onChange(s.value as T));
|
|
192
|
-
return s;
|
|
193
|
-
}
|
|
194
|
-
|
|
195
|
-
function frameLabel(f: FrameId): string {
|
|
196
|
-
return FRAMES.find((s) => s.id === f)?.label ?? f;
|
|
197
|
-
}
|
|
198
|
-
|
|
199
|
-
export class TychoViewerElement extends HTMLElement {
|
|
200
|
-
private container: Container | null = null;
|
|
201
|
-
private families: FamilyDataset[] = [];
|
|
202
|
-
private groups: string[] = [];
|
|
203
|
-
private group = '';
|
|
204
|
-
private e = 0; // shared target eccentricity; each panel snaps to its branch's nearest member
|
|
205
|
-
private eStep = 0.005;
|
|
206
|
-
private eMax = 1;
|
|
207
|
-
private playing = false;
|
|
208
|
-
private speed = 1;
|
|
209
|
-
private spacing: Spacing = 'anomaly';
|
|
210
|
-
private readonly ghostMode: FamilyMode = 'family';
|
|
211
|
-
private progress = 0;
|
|
212
|
-
private raf = 0;
|
|
213
|
-
private showGhost = true;
|
|
214
|
-
private threeD = false;
|
|
215
|
-
private fpsEma = 0;
|
|
216
|
-
private lastTick = 0;
|
|
217
|
-
private kiosk = false;
|
|
218
|
-
|
|
219
|
-
private tree: LayoutNode | null = null;
|
|
220
|
-
private panels = new Map<string, Panel>();
|
|
221
|
-
private nextId = 1;
|
|
222
|
-
private tracksCache = new Map<string, FamilyTracks>();
|
|
223
|
-
|
|
224
|
-
private root!: HTMLDivElement;
|
|
225
|
-
private titleEl!: HTMLHeadingElement;
|
|
226
|
-
private captionEl!: HTMLSpanElement;
|
|
227
|
-
private groupSeg!: Seg<string>;
|
|
228
|
-
private chips = new Map<FrameId, HTMLSpanElement>();
|
|
229
|
-
private slider!: HTMLInputElement;
|
|
230
|
-
private sliderOut!: HTMLSpanElement;
|
|
231
|
-
private scrub!: HTMLInputElement;
|
|
232
|
-
private playBtn!: HTMLButtonElement;
|
|
233
|
-
private readout!: HTMLSpanElement;
|
|
234
|
-
private fpsEl!: HTMLSpanElement;
|
|
235
|
-
private statusEl!: HTMLDivElement;
|
|
236
|
-
private stage!: HTMLDivElement;
|
|
237
|
-
private hint!: HTMLDivElement;
|
|
238
|
-
|
|
239
|
-
connectedCallback(): void {
|
|
240
|
-
this.kiosk = this.hasAttribute('kiosk');
|
|
241
|
-
const root = this.attachShadow({ mode: 'open' });
|
|
242
|
-
const style = document.createElement('style');
|
|
243
|
-
style.textContent = STYLE;
|
|
244
|
-
root.appendChild(style);
|
|
245
|
-
this.root = document.createElement('div');
|
|
246
|
-
this.root.className = this.kiosk ? 'root kiosk' : 'root';
|
|
247
|
-
root.appendChild(this.root);
|
|
248
|
-
|
|
249
|
-
const header = document.createElement('header');
|
|
250
|
-
this.titleEl = document.createElement('h1');
|
|
251
|
-
this.titleEl.textContent = this.getAttribute('title') ?? '';
|
|
252
|
-
this.captionEl = document.createElement('span');
|
|
253
|
-
this.captionEl.className = 'caption';
|
|
254
|
-
header.append(this.titleEl, this.captionEl);
|
|
255
|
-
|
|
256
|
-
const controls = document.createElement('div');
|
|
257
|
-
controls.className = 'controls';
|
|
258
|
-
this.groupSeg = seg<string>([], () => undefined);
|
|
259
|
-
const sliderField = document.createElement('label');
|
|
260
|
-
sliderField.className = 'field';
|
|
261
|
-
const k = document.createElement('span');
|
|
262
|
-
k.className = 'k';
|
|
263
|
-
k.textContent = 'e';
|
|
264
|
-
this.slider = document.createElement('input');
|
|
265
|
-
this.slider.type = 'range';
|
|
266
|
-
this.slider.min = '0';
|
|
267
|
-
this.slider.addEventListener('input', () => this.setE(Number(this.slider.value)));
|
|
268
|
-
this.sliderOut = document.createElement('span');
|
|
269
|
-
sliderField.append(k, this.slider, this.sliderOut);
|
|
270
|
-
const chips = document.createElement('div');
|
|
271
|
-
chips.className = 'chips';
|
|
272
|
-
const ck = document.createElement('span');
|
|
273
|
-
ck.className = 'k';
|
|
274
|
-
ck.textContent = 'frames';
|
|
275
|
-
chips.appendChild(ck);
|
|
276
|
-
for (const f of FRAMES) {
|
|
277
|
-
const chip = document.createElement('span');
|
|
278
|
-
chip.className = 'chip';
|
|
279
|
-
chip.title = 'drag onto a panel to split it there or replace it; click to add';
|
|
280
|
-
const grip = document.createElement('span');
|
|
281
|
-
grip.className = 'grip';
|
|
282
|
-
grip.textContent = '⠇';
|
|
283
|
-
const n = document.createElement('span');
|
|
284
|
-
n.className = 'n';
|
|
285
|
-
chip.append(grip, document.createTextNode(f.label), n);
|
|
286
|
-
chip.addEventListener('pointerdown', (ev) => this.startDrag(ev, { kind: 'frame', frame: f.id }, chip));
|
|
287
|
-
this.chips.set(f.id, chip);
|
|
288
|
-
chips.appendChild(chip);
|
|
289
|
-
}
|
|
290
|
-
controls.append(this.groupSeg.el, sliderField, chips);
|
|
291
|
-
|
|
292
|
-
this.stage = document.createElement('div');
|
|
293
|
-
this.stage.className = 'stage';
|
|
294
|
-
this.statusEl = document.createElement('div');
|
|
295
|
-
this.statusEl.className = 'status';
|
|
296
|
-
this.statusEl.textContent = 'loading…';
|
|
297
|
-
this.hint = document.createElement('div');
|
|
298
|
-
this.hint.className = 'hint';
|
|
299
|
-
this.hint.textContent = 'drag a frame here';
|
|
300
|
-
this.hint.hidden = true;
|
|
301
|
-
this.stage.append(this.statusEl, this.hint);
|
|
302
|
-
|
|
303
|
-
const footer = document.createElement('footer');
|
|
304
|
-
this.playBtn = document.createElement('button');
|
|
305
|
-
this.playBtn.type = 'button';
|
|
306
|
-
this.playBtn.textContent = 'play';
|
|
307
|
-
this.playBtn.addEventListener('click', () => this.togglePlay());
|
|
308
|
-
this.scrub = document.createElement('input');
|
|
309
|
-
this.scrub.type = 'range';
|
|
310
|
-
this.scrub.className = 'scrub';
|
|
311
|
-
this.scrub.min = '0';
|
|
312
|
-
this.scrub.max = '1000';
|
|
313
|
-
this.scrub.step = '1';
|
|
314
|
-
this.scrub.addEventListener('input', () => {
|
|
315
|
-
this.pause();
|
|
316
|
-
this.progress = Number(this.scrub.value) / 1000;
|
|
317
|
-
this.drawAll();
|
|
318
|
-
});
|
|
319
|
-
const speed = select(
|
|
320
|
-
[0.25, 0.5, 1, 2, 4].map((s) => ({ id: String(s), label: `${s}×` })),
|
|
321
|
-
'1',
|
|
322
|
-
(v) => (this.speed = Number(v)),
|
|
323
|
-
);
|
|
324
|
-
const spacing = select<Spacing>(
|
|
325
|
-
[
|
|
326
|
-
{ id: 'anomaly', label: 'uniform in f' },
|
|
327
|
-
{ id: 'time', label: 'uniform in τ' },
|
|
328
|
-
{ id: 'arclength', label: 'uniform in arclength' },
|
|
329
|
-
],
|
|
330
|
-
this.spacing,
|
|
331
|
-
(v) => {
|
|
332
|
-
this.spacing = v;
|
|
333
|
-
this.drawAll();
|
|
334
|
-
},
|
|
335
|
-
);
|
|
336
|
-
const ghost = this.checkbox('full orbit', true, (on) => {
|
|
337
|
-
this.showGhost = on;
|
|
338
|
-
for (const p of this.panels.values()) p.viewer.showGhost = on;
|
|
339
|
-
this.drawAll();
|
|
340
|
-
});
|
|
341
|
-
const threeD = this.checkbox('3D orbit camera', false, (on) => {
|
|
342
|
-
this.threeD = on;
|
|
343
|
-
for (const p of this.panels.values()) p.viewer.setThreeD(on);
|
|
344
|
-
});
|
|
345
|
-
const reset = document.createElement('button');
|
|
346
|
-
reset.type = 'button';
|
|
347
|
-
reset.textContent = 'reset layout';
|
|
348
|
-
reset.addEventListener('click', () => {
|
|
349
|
-
this.forget();
|
|
350
|
-
this.applyDefaultLayout();
|
|
351
|
-
});
|
|
352
|
-
this.readout = document.createElement('span');
|
|
353
|
-
this.readout.className = 'readout';
|
|
354
|
-
this.fpsEl = document.createElement('span');
|
|
355
|
-
this.fpsEl.className = 'fps';
|
|
356
|
-
this.fpsEl.textContent = '— fps';
|
|
357
|
-
const legend = document.createElement('span');
|
|
358
|
-
legend.className = 'legend';
|
|
359
|
-
for (const [css, label] of [
|
|
360
|
-
['#F0E442', 'spacecraft'],
|
|
361
|
-
['#56B4E9', 'm₁'],
|
|
362
|
-
['#A8A8A8', 'm₂'],
|
|
363
|
-
]) {
|
|
364
|
-
const item = document.createElement('span');
|
|
365
|
-
const dot = document.createElement('i');
|
|
366
|
-
dot.style.background = css;
|
|
367
|
-
item.append(dot, document.createTextNode(label));
|
|
368
|
-
legend.appendChild(item);
|
|
369
|
-
}
|
|
370
|
-
footer.append(this.playBtn, this.scrub, speed, spacing, ghost, threeD, reset, legend, this.readout, this.fpsEl);
|
|
371
|
-
|
|
372
|
-
this.root.append(header, controls, this.stage, footer);
|
|
373
|
-
|
|
374
|
-
this.tabIndex = 0;
|
|
375
|
-
this.addEventListener('keydown', (ev) => this.onKey(ev));
|
|
376
|
-
|
|
377
|
-
const source = this.getAttribute('data');
|
|
378
|
-
if (!source) {
|
|
379
|
-
this.statusEl.textContent = 'no data attribute';
|
|
380
|
-
return;
|
|
381
|
-
}
|
|
382
|
-
loadContainer(source)
|
|
383
|
-
.then((c) => {
|
|
384
|
-
this.container = c;
|
|
385
|
-
this.statusEl.remove();
|
|
386
|
-
this.populate();
|
|
387
|
-
})
|
|
388
|
-
.catch((err: unknown) => {
|
|
389
|
-
this.statusEl.textContent = `failed to load data: ${(err as Error).message}`;
|
|
390
|
-
});
|
|
391
|
-
}
|
|
392
|
-
|
|
393
|
-
disconnectedCallback(): void {
|
|
394
|
-
this.pause();
|
|
395
|
-
for (const p of this.panels.values()) p.viewer.dispose();
|
|
396
|
-
this.panels.clear();
|
|
397
|
-
}
|
|
398
|
-
|
|
399
|
-
private checkbox(label: string, checked: boolean, onChange: (on: boolean) => void): HTMLLabelElement {
|
|
400
|
-
const l = document.createElement('label');
|
|
401
|
-
l.className = 'check';
|
|
402
|
-
const c = document.createElement('input');
|
|
403
|
-
c.type = 'checkbox';
|
|
404
|
-
c.checked = checked;
|
|
405
|
-
c.addEventListener('change', () => onChange(c.checked));
|
|
406
|
-
l.append(c, document.createTextNode(label));
|
|
407
|
-
return l;
|
|
408
|
-
}
|
|
409
|
-
|
|
410
|
-
// ---- data ---------------------------------------------------------------
|
|
411
|
-
|
|
412
|
-
private populate(): void {
|
|
413
|
-
const c = this.container!;
|
|
414
|
-
this.families = c.datasets.filter((d): d is FamilyDataset => d.kind === 'periodic_orbit_family');
|
|
415
|
-
this.groups = [...new Set(this.families.map((d) => d.group))];
|
|
416
|
-
const gs = seg(
|
|
417
|
-
this.groups.map((g) => ({ id: g, label: g })),
|
|
418
|
-
(g) => this.setGroup(g),
|
|
419
|
-
);
|
|
420
|
-
this.groupSeg.el.replaceWith(gs.el);
|
|
421
|
-
this.groupSeg = gs;
|
|
422
|
-
const saved = this.kiosk ? null : this.recall();
|
|
423
|
-
const fromAttr = this.parseLayout(this.getAttribute('layout'));
|
|
424
|
-
const layout = saved ?? fromAttr;
|
|
425
|
-
if (layout && this.groups.includes(layout.group)) {
|
|
426
|
-
this.group = layout.group;
|
|
427
|
-
this.groupSeg.set(this.group);
|
|
428
|
-
this.rangeForGroup();
|
|
429
|
-
this.e = Math.min(layout.e, this.eMax);
|
|
430
|
-
this.restore(layout);
|
|
431
|
-
} else {
|
|
432
|
-
if (this.groups.length) {
|
|
433
|
-
this.group = this.groups[0];
|
|
434
|
-
this.groupSeg.set(this.group);
|
|
435
|
-
this.rangeForGroup();
|
|
436
|
-
}
|
|
437
|
-
this.applyDefaultLayout();
|
|
438
|
-
}
|
|
439
|
-
if (this.kiosk) this.play();
|
|
440
|
-
}
|
|
441
|
-
|
|
442
|
-
private branchDataset(branch: string): FamilyDataset | undefined {
|
|
443
|
-
return this.families.find((d) => d.group === this.group && d.branch === branch);
|
|
444
|
-
}
|
|
445
|
-
|
|
446
|
-
private tracksFor(d: FamilyDataset): FamilyTracks {
|
|
447
|
-
let t = this.tracksCache.get(d.id);
|
|
448
|
-
if (t) return t;
|
|
449
|
-
const c = this.container!;
|
|
450
|
-
const info = c.viewInfo(d.positions.view);
|
|
451
|
-
t = {
|
|
452
|
-
count: info.shape[0],
|
|
453
|
-
samples: info.shape[1],
|
|
454
|
-
positions: c.f32(d.positions.view),
|
|
455
|
-
anomalies: c.f32(d.independent_variable.view),
|
|
456
|
-
params: c.f64(d.parameter.view),
|
|
457
|
-
mu: d.system.mu,
|
|
458
|
-
f0: d.system.f0,
|
|
459
|
-
};
|
|
460
|
-
this.tracksCache.set(d.id, t);
|
|
461
|
-
return t;
|
|
462
|
-
}
|
|
463
|
-
|
|
464
|
-
/** Slider range and step from the union of the group's branches. */
|
|
465
|
-
private rangeForGroup(): void {
|
|
466
|
-
let max = 0;
|
|
467
|
-
let step = Infinity;
|
|
468
|
-
for (const d of this.families.filter((d) => d.group === this.group)) {
|
|
469
|
-
const p = this.tracksFor(d).params;
|
|
470
|
-
max = Math.max(max, p[p.length - 1]);
|
|
471
|
-
for (let i = 1; i < p.length; i++) step = Math.min(step, Math.abs(p[i] - p[i - 1]));
|
|
472
|
-
}
|
|
473
|
-
this.eMax = max;
|
|
474
|
-
this.eStep = Number.isFinite(step) && step > 0 ? step : 0.005;
|
|
475
|
-
this.slider.max = String(this.eMax);
|
|
476
|
-
this.slider.step = String(this.eStep);
|
|
477
|
-
this.e = Math.min(this.e, this.eMax);
|
|
478
|
-
}
|
|
479
|
-
|
|
480
|
-
private setGroup(g: string): void {
|
|
481
|
-
this.group = g;
|
|
482
|
-
this.groupSeg.set(g);
|
|
483
|
-
this.rangeForGroup();
|
|
484
|
-
for (const p of this.panels.values()) this.loadPanel(p, true);
|
|
485
|
-
this.drawAll();
|
|
486
|
-
this.updateCaption();
|
|
487
|
-
this.remember();
|
|
488
|
-
}
|
|
489
|
-
|
|
490
|
-
private setE(e: number): void {
|
|
491
|
-
this.e = Math.max(0, Math.min(this.eMax, e));
|
|
492
|
-
for (const p of this.panels.values()) this.loadPanel(p, false);
|
|
493
|
-
this.drawAll();
|
|
494
|
-
this.updateCaption();
|
|
495
|
-
this.remember();
|
|
496
|
-
}
|
|
497
|
-
|
|
498
|
-
/** Points a panel at its branch's member nearest the shared eccentricity. */
|
|
499
|
-
private loadPanel(p: Panel, refit: boolean): void {
|
|
500
|
-
const d = this.branchDataset(p.branch);
|
|
501
|
-
if (!d) {
|
|
502
|
-
p.tracks = null;
|
|
503
|
-
p.fRel = new Float32Array(0);
|
|
504
|
-
p.meta.textContent = 'no data';
|
|
505
|
-
return;
|
|
506
|
-
}
|
|
507
|
-
const t = this.tracksFor(d);
|
|
508
|
-
let idx = 0;
|
|
509
|
-
let best = Infinity;
|
|
510
|
-
for (let i = 0; i < t.count; i++) {
|
|
511
|
-
const dd = Math.abs(t.params[i] - this.e);
|
|
512
|
-
if (dd < best) {
|
|
513
|
-
best = dd;
|
|
514
|
-
idx = i;
|
|
515
|
-
}
|
|
516
|
-
}
|
|
517
|
-
const m = t.samples;
|
|
518
|
-
const changedTracks = p.tracks !== t;
|
|
519
|
-
p.tracks = t;
|
|
520
|
-
p.member = idx;
|
|
521
|
-
const xy = t.positions.subarray(idx * m * 2, (idx + 1) * m * 2);
|
|
522
|
-
p.fRel = d.independent_variable.per_member ? t.anomalies.subarray(idx * m, (idx + 1) * m) : t.anomalies.subarray(0, m);
|
|
523
|
-
p.sys = { mu: t.mu, e: t.params[idx], f0: t.f0 };
|
|
524
|
-
p.period = p.fRel[m - 1];
|
|
525
|
-
p.tauPeriod = elapsedTime(p.sys.e, p.sys.f0, p.sys.f0 + p.period);
|
|
526
|
-
if (changedTracks) p.viewer.setFamily(t, this.ghostMode);
|
|
527
|
-
p.viewer.setTrajectory(xy, p.fRel, p.sys, refit);
|
|
528
|
-
let warn = '';
|
|
529
|
-
if (d.closure_error) {
|
|
530
|
-
const ce = this.container!.f32(d.closure_error.view)[idx];
|
|
531
|
-
if (ce > 1e-4) warn = ` · closure ${ce.toExponential(1)}`;
|
|
532
|
-
}
|
|
533
|
-
p.meta.textContent = `e = ${p.sys.e.toFixed(4)}${warn}`;
|
|
534
|
-
p.meta.classList.toggle('warn', warn !== '');
|
|
535
|
-
}
|
|
536
|
-
|
|
537
|
-
// ---- panels and layout --------------------------------------------------
|
|
538
|
-
|
|
539
|
-
private createPanel(frame: FrameId, branch: string, id?: string): Panel {
|
|
540
|
-
const pid = id ?? `p${this.nextId++}`;
|
|
541
|
-
const num = Number(pid.slice(1));
|
|
542
|
-
if (Number.isFinite(num) && num >= this.nextId) this.nextId = num + 1;
|
|
543
|
-
const host = document.createElement('div');
|
|
544
|
-
host.className = 'panel';
|
|
545
|
-
host.dataset.id = pid;
|
|
546
|
-
const head = document.createElement('div');
|
|
547
|
-
head.className = 'phead';
|
|
548
|
-
const title = document.createElement('span');
|
|
549
|
-
title.className = 't';
|
|
550
|
-
title.textContent = frameLabel(frame);
|
|
551
|
-
const meta = document.createElement('span');
|
|
552
|
-
meta.className = 'm';
|
|
553
|
-
const sp = document.createElement('span');
|
|
554
|
-
sp.className = 'sp';
|
|
555
|
-
const branchSeg = seg<string>(
|
|
556
|
-
[
|
|
557
|
-
{ id: 'periapsis', label: 'peri' },
|
|
558
|
-
{ id: 'apoapsis', label: 'apo' },
|
|
559
|
-
],
|
|
560
|
-
(b) => {
|
|
561
|
-
const p = this.panels.get(pid);
|
|
562
|
-
if (!p) return;
|
|
563
|
-
p.branch = b;
|
|
564
|
-
p.branchSeg.set(b);
|
|
565
|
-
this.loadPanel(p, false);
|
|
566
|
-
this.drawAll();
|
|
567
|
-
this.updateCaption();
|
|
568
|
-
this.remember();
|
|
569
|
-
},
|
|
570
|
-
'seg mini',
|
|
571
|
-
);
|
|
572
|
-
branchSeg.set(branch);
|
|
573
|
-
const home = document.createElement('button');
|
|
574
|
-
home.className = 'x home';
|
|
575
|
-
home.type = 'button';
|
|
576
|
-
home.title = 'reset the view';
|
|
577
|
-
home.innerHTML =
|
|
578
|
-
'<svg viewBox="0 0 16 16" width="14" height="14" aria-hidden="true"><path d="M8 2.2 1.8 7.6h1.7V13.5h3.3V9.7h2.4v3.8h3.3V7.6h1.7z" fill="currentColor"/></svg>';
|
|
579
|
-
home.addEventListener('pointerdown', (ev) => ev.stopPropagation());
|
|
580
|
-
home.addEventListener('click', () => this.panels.get(pid)?.viewer.fit());
|
|
581
|
-
const close = document.createElement('button');
|
|
582
|
-
close.className = 'x';
|
|
583
|
-
close.type = 'button';
|
|
584
|
-
close.title = 'close panel';
|
|
585
|
-
close.textContent = '×';
|
|
586
|
-
close.addEventListener('pointerdown', (ev) => ev.stopPropagation());
|
|
587
|
-
close.addEventListener('click', () => this.closePanel(pid));
|
|
588
|
-
head.append(title, meta, sp, branchSeg.el, home, close);
|
|
589
|
-
head.addEventListener('pointerdown', (ev) => this.startDrag(ev, { kind: 'panel', id: pid }, head));
|
|
590
|
-
const body = document.createElement('div');
|
|
591
|
-
body.className = 'pbody';
|
|
592
|
-
const drop = document.createElement('div');
|
|
593
|
-
drop.className = 'drop';
|
|
594
|
-
drop.hidden = true;
|
|
595
|
-
host.append(head, body);
|
|
596
|
-
const viewer = new OrbitViewer(body, frame);
|
|
597
|
-
body.appendChild(drop); // above the canvases
|
|
598
|
-
viewer.showGhost = this.showGhost;
|
|
599
|
-
viewer.setThreeD(this.threeD);
|
|
600
|
-
const p: Panel = {
|
|
601
|
-
id: pid,
|
|
602
|
-
frame,
|
|
603
|
-
branch,
|
|
604
|
-
host,
|
|
605
|
-
body,
|
|
606
|
-
title,
|
|
607
|
-
meta,
|
|
608
|
-
branchSeg,
|
|
609
|
-
drop,
|
|
610
|
-
viewer,
|
|
611
|
-
tracks: null,
|
|
612
|
-
member: 0,
|
|
613
|
-
sys: { mu: 0.5, e: 0, f0: 0 },
|
|
614
|
-
fRel: new Float32Array(0),
|
|
615
|
-
period: 0,
|
|
616
|
-
tauPeriod: 0,
|
|
617
|
-
};
|
|
618
|
-
this.panels.set(pid, p);
|
|
619
|
-
this.loadPanel(p, true);
|
|
620
|
-
return p;
|
|
621
|
-
}
|
|
622
|
-
|
|
623
|
-
private destroyPanel(id: string): void {
|
|
624
|
-
const p = this.panels.get(id);
|
|
625
|
-
if (!p) return;
|
|
626
|
-
p.viewer.dispose();
|
|
627
|
-
p.host.remove();
|
|
628
|
-
this.panels.delete(id);
|
|
629
|
-
}
|
|
630
|
-
|
|
631
|
-
private closePanel(id: string): void {
|
|
632
|
-
if (!this.tree) return;
|
|
633
|
-
this.tree = removePanel(this.tree, id);
|
|
634
|
-
this.destroyPanel(id);
|
|
635
|
-
this.renderLayout();
|
|
636
|
-
this.remember();
|
|
637
|
-
}
|
|
638
|
-
|
|
639
|
-
/** Adds a panel by splitting the largest panel along its longer side (or filling an empty stage). */
|
|
640
|
-
private addPanel(frame: FrameId, branch?: string): void {
|
|
641
|
-
if (this.panels.size >= MAX_PANELS) return;
|
|
642
|
-
const b = branch ?? this.defaultBranch();
|
|
643
|
-
if (!this.tree) {
|
|
644
|
-
const p = this.createPanel(frame, b);
|
|
645
|
-
this.tree = { kind: 'panel', id: p.id };
|
|
646
|
-
} else {
|
|
647
|
-
const target = panelAreas(this.tree).sort((x, y) => y.area - x.area)[0];
|
|
648
|
-
const tp = this.panels.get(target.id)!;
|
|
649
|
-
const r = tp.host.getBoundingClientRect();
|
|
650
|
-
const p = this.createPanel(frame, b);
|
|
651
|
-
this.tree = splitAt(this.tree, target.id, p.id, r.width >= r.height ? 'right' : 'bottom');
|
|
652
|
-
}
|
|
653
|
-
this.renderLayout();
|
|
654
|
-
this.drawAll();
|
|
655
|
-
this.remember();
|
|
656
|
-
}
|
|
657
|
-
|
|
658
|
-
private defaultBranch(): string {
|
|
659
|
-
const last = [...this.panels.values()].pop();
|
|
660
|
-
return last?.branch ?? BRANCHES[0];
|
|
661
|
-
}
|
|
662
|
-
|
|
663
|
-
/** Places a new or existing panel relative to `targetId` in `zone`. */
|
|
664
|
-
private dropOn(targetId: string, zone: DropZone, payload: DragPayload): void {
|
|
665
|
-
if (!this.tree) return;
|
|
666
|
-
if (payload.kind === 'panel') {
|
|
667
|
-
if (payload.id === targetId) return;
|
|
668
|
-
if (zone === 'centre') {
|
|
669
|
-
// swap frame and branch between the two panels
|
|
670
|
-
const a = this.panels.get(payload.id)!;
|
|
671
|
-
const b = this.panels.get(targetId)!;
|
|
672
|
-
[a.frame, b.frame] = [b.frame, a.frame];
|
|
673
|
-
[a.branch, b.branch] = [b.branch, a.branch];
|
|
674
|
-
for (const p of [a, b]) {
|
|
675
|
-
p.viewer.setFrame(p.frame);
|
|
676
|
-
p.title.textContent = frameLabel(p.frame);
|
|
677
|
-
p.branchSeg.set(p.branch);
|
|
678
|
-
this.loadPanel(p, true);
|
|
679
|
-
}
|
|
680
|
-
} else {
|
|
681
|
-
this.tree = removePanel(this.tree, payload.id) ?? this.tree;
|
|
682
|
-
this.tree = splitAt(this.tree, targetId, payload.id, zone);
|
|
683
|
-
}
|
|
684
|
-
} else if (zone === 'centre') {
|
|
685
|
-
const p = this.panels.get(targetId)!;
|
|
686
|
-
p.frame = payload.frame;
|
|
687
|
-
p.viewer.setFrame(p.frame);
|
|
688
|
-
p.title.textContent = frameLabel(p.frame);
|
|
689
|
-
this.loadPanel(p, true);
|
|
690
|
-
} else {
|
|
691
|
-
if (this.panels.size >= MAX_PANELS) return;
|
|
692
|
-
const p = this.createPanel(payload.frame, this.panels.get(targetId)!.branch);
|
|
693
|
-
this.tree = splitAt(this.tree, targetId, p.id, zone);
|
|
694
|
-
}
|
|
695
|
-
this.renderLayout();
|
|
696
|
-
this.drawAll();
|
|
697
|
-
this.remember();
|
|
698
|
-
}
|
|
699
|
-
|
|
700
|
-
/** Rebuilds the stage DOM from the tree; panel hosts are re-parented, not recreated. */
|
|
701
|
-
private renderLayout(): void {
|
|
702
|
-
for (const el of [...this.stage.children]) if (el !== this.hint) el.remove();
|
|
703
|
-
this.hint.hidden = !!this.tree;
|
|
704
|
-
if (this.tree) this.stage.appendChild(this.build(this.tree));
|
|
705
|
-
for (const p of this.panels.values()) p.viewer.resize();
|
|
706
|
-
const counts = new Map<FrameId, number>();
|
|
707
|
-
for (const p of this.panels.values()) counts.set(p.frame, (counts.get(p.frame) ?? 0) + 1);
|
|
708
|
-
for (const [f, chip] of this.chips) chip.querySelector('.n')!.textContent = counts.get(f) ? String(counts.get(f)) : '';
|
|
709
|
-
this.updateCaption();
|
|
710
|
-
}
|
|
711
|
-
|
|
712
|
-
private build(node: LayoutNode): HTMLElement {
|
|
713
|
-
if (node.kind === 'panel') {
|
|
714
|
-
const p = this.panels.get(node.id);
|
|
715
|
-
if (!p) throw new Error(`layout references unknown panel ${node.id}`);
|
|
716
|
-
p.host.style.flex = '';
|
|
717
|
-
return p.host;
|
|
718
|
-
}
|
|
719
|
-
const el = document.createElement('div');
|
|
720
|
-
el.className = `split ${node.dir}`;
|
|
721
|
-
const a = this.build(node.a);
|
|
722
|
-
const b = this.build(node.b);
|
|
723
|
-
a.style.flex = `${node.ratio} 1 0`;
|
|
724
|
-
b.style.flex = `${1 - node.ratio} 1 0`;
|
|
725
|
-
const g = document.createElement('div');
|
|
726
|
-
g.className = 'gutter';
|
|
727
|
-
g.addEventListener('pointerdown', (ev) => this.startResize(ev, node, el, a, b, g));
|
|
728
|
-
el.append(a, g, b);
|
|
729
|
-
return el;
|
|
730
|
-
}
|
|
731
|
-
|
|
732
|
-
private startResize(ev: PointerEvent, node: LayoutNode, el: HTMLElement, a: HTMLElement, b: HTMLElement, g: HTMLElement): void {
|
|
733
|
-
if (node.kind !== 'split') return;
|
|
734
|
-
ev.preventDefault();
|
|
735
|
-
g.classList.add('active');
|
|
736
|
-
g.setPointerCapture(ev.pointerId);
|
|
737
|
-
let ratio = node.ratio;
|
|
738
|
-
const move = (e: PointerEvent) => {
|
|
739
|
-
const r = el.getBoundingClientRect();
|
|
740
|
-
ratio = node.dir === 'row' ? (e.clientX - r.left) / Math.max(1, r.width) : (e.clientY - r.top) / Math.max(1, r.height);
|
|
741
|
-
ratio = Math.max(0.1, Math.min(0.9, ratio));
|
|
742
|
-
a.style.flex = `${ratio} 1 0`;
|
|
743
|
-
b.style.flex = `${1 - ratio} 1 0`;
|
|
744
|
-
};
|
|
745
|
-
const up = () => {
|
|
746
|
-
g.classList.remove('active');
|
|
747
|
-
g.removeEventListener('pointermove', move);
|
|
748
|
-
g.removeEventListener('pointerup', up);
|
|
749
|
-
g.removeEventListener('pointercancel', up);
|
|
750
|
-
if (this.tree) this.tree = setRatio(this.tree, node, ratio);
|
|
751
|
-
for (const p of this.panels.values()) p.viewer.resize();
|
|
752
|
-
this.remember();
|
|
753
|
-
};
|
|
754
|
-
g.addEventListener('pointermove', move);
|
|
755
|
-
g.addEventListener('pointerup', up);
|
|
756
|
-
g.addEventListener('pointercancel', up);
|
|
757
|
-
}
|
|
758
|
-
|
|
759
|
-
// ---- drag and drop (pointer events, so it also works on touch) ----------
|
|
760
|
-
|
|
761
|
-
private startDrag(ev: PointerEvent, payload: DragPayload, source: HTMLElement): void {
|
|
762
|
-
if (ev.button !== 0) return;
|
|
763
|
-
ev.preventDefault();
|
|
764
|
-
const sx = ev.clientX;
|
|
765
|
-
const sy = ev.clientY;
|
|
766
|
-
let ghost: HTMLDivElement | null = null;
|
|
767
|
-
let target: { id: string; zone: DropZone } | null = null;
|
|
768
|
-
let overHint = false;
|
|
769
|
-
const root = this.shadowRoot!;
|
|
770
|
-
source.setPointerCapture(ev.pointerId);
|
|
771
|
-
const clearDrop = () => {
|
|
772
|
-
for (const p of this.panels.values()) p.drop.hidden = true;
|
|
773
|
-
this.hint.classList.remove('over');
|
|
774
|
-
};
|
|
775
|
-
const move = (e: PointerEvent) => {
|
|
776
|
-
if (!ghost) {
|
|
777
|
-
if (Math.hypot(e.clientX - sx, e.clientY - sy) < 4) return;
|
|
778
|
-
ghost = document.createElement('div');
|
|
779
|
-
ghost.className = 'ghostchip';
|
|
780
|
-
ghost.textContent = payload.kind === 'frame' ? frameLabel(payload.frame) : frameLabel(this.panels.get(payload.id)!.frame);
|
|
781
|
-
root.appendChild(ghost);
|
|
782
|
-
}
|
|
783
|
-
ghost.style.left = `${e.clientX}px`;
|
|
784
|
-
ghost.style.top = `${e.clientY}px`;
|
|
785
|
-
clearDrop();
|
|
786
|
-
target = null;
|
|
787
|
-
overHint = false;
|
|
788
|
-
const under = root.elementFromPoint(e.clientX, e.clientY) as HTMLElement | null;
|
|
789
|
-
if (under && (under === this.hint || this.hint.contains(under))) {
|
|
790
|
-
overHint = true;
|
|
791
|
-
this.hint.classList.add('over');
|
|
792
|
-
return;
|
|
793
|
-
}
|
|
794
|
-
const host = under?.closest('.panel') as HTMLElement | null;
|
|
795
|
-
if (!host) return;
|
|
796
|
-
const id = host.dataset.id!;
|
|
797
|
-
if (payload.kind === 'panel' && payload.id === id) return;
|
|
798
|
-
const p = this.panels.get(id)!;
|
|
799
|
-
const r = p.body.getBoundingClientRect();
|
|
800
|
-
const zone = zoneFor(e.clientX - r.left, e.clientY - r.top, r.width, r.height);
|
|
801
|
-
target = { id, zone };
|
|
802
|
-
const d = p.drop;
|
|
803
|
-
d.hidden = false;
|
|
804
|
-
d.classList.toggle('centre', zone === 'centre');
|
|
805
|
-
d.style.inset =
|
|
806
|
-
zone === 'left' ? '0 50% 0 0' : zone === 'right' ? '0 0 0 50%' : zone === 'top' ? '0 0 50% 0' : zone === 'bottom' ? '50% 0 0 0' : '0';
|
|
807
|
-
};
|
|
808
|
-
const up = () => {
|
|
809
|
-
source.removeEventListener('pointermove', move);
|
|
810
|
-
source.removeEventListener('pointerup', up);
|
|
811
|
-
source.removeEventListener('pointercancel', up);
|
|
812
|
-
clearDrop();
|
|
813
|
-
const dragged = !!ghost;
|
|
814
|
-
ghost?.remove();
|
|
815
|
-
if (!dragged) {
|
|
816
|
-
if (payload.kind === 'frame') this.addPanel(payload.frame);
|
|
817
|
-
return;
|
|
818
|
-
}
|
|
819
|
-
if (overHint && payload.kind === 'frame' && !this.tree) this.addPanel(payload.frame);
|
|
820
|
-
else if (target) this.dropOn(target.id, target.zone, payload);
|
|
821
|
-
};
|
|
822
|
-
source.addEventListener('pointermove', move);
|
|
823
|
-
source.addEventListener('pointerup', up);
|
|
824
|
-
source.addEventListener('pointercancel', up);
|
|
825
|
-
}
|
|
826
|
-
|
|
827
|
-
// ---- layouts: default, saved, attribute ---------------------------------
|
|
828
|
-
|
|
829
|
-
private applyDefaultLayout(): void {
|
|
830
|
-
for (const id of [...this.panels.keys()]) this.destroyPanel(id);
|
|
831
|
-
this.tree = null;
|
|
832
|
-
const initial = (this.getAttribute('frames') ?? 'rotating_pulsating').split(/[\s,]+/) as FrameId[];
|
|
833
|
-
for (const f of initial) if (FRAMES.some((s) => s.id === f)) this.addPanel(f);
|
|
834
|
-
this.renderLayout();
|
|
835
|
-
this.drawAll();
|
|
836
|
-
}
|
|
837
|
-
|
|
838
|
-
private restore(layout: SavedLayout): void {
|
|
839
|
-
for (const id of [...this.panels.keys()]) this.destroyPanel(id);
|
|
840
|
-
this.tree = null;
|
|
841
|
-
let tree = layout.tree;
|
|
842
|
-
for (const [id, spec] of Object.entries(layout.panels)) {
|
|
843
|
-
if (!tree || !hasPanel(tree, id)) continue;
|
|
844
|
-
if (!FRAMES.some((s) => s.id === spec.frame)) {
|
|
845
|
-
tree = removePanel(tree, id);
|
|
846
|
-
continue;
|
|
847
|
-
}
|
|
848
|
-
this.createPanel(spec.frame, BRANCHES.includes(spec.branch) ? spec.branch : BRANCHES[0], id);
|
|
849
|
-
}
|
|
850
|
-
for (const id of panelIds(tree)) if (tree && !this.panels.has(id)) tree = removePanel(tree, id);
|
|
851
|
-
this.tree = tree;
|
|
852
|
-
if (!this.tree) {
|
|
853
|
-
this.applyDefaultLayout();
|
|
854
|
-
return;
|
|
855
|
-
}
|
|
856
|
-
this.renderLayout();
|
|
857
|
-
this.drawAll();
|
|
858
|
-
}
|
|
859
|
-
|
|
860
|
-
private snapshot(): SavedLayout {
|
|
861
|
-
const panels: SavedLayout['panels'] = {};
|
|
862
|
-
for (const p of this.panels.values()) panels[p.id] = { frame: p.frame, branch: p.branch };
|
|
863
|
-
return { version: 1, group: this.group, e: this.e, tree: this.tree, panels };
|
|
864
|
-
}
|
|
865
|
-
|
|
866
|
-
/** The current layout as JSON, usable as the `layout` attribute of an export. */
|
|
867
|
-
get layoutJSON(): string {
|
|
868
|
-
return JSON.stringify(this.snapshot());
|
|
869
|
-
}
|
|
870
|
-
|
|
871
|
-
private storageKey(): string {
|
|
872
|
-
return `tycho:layout:${this.getAttribute('title') ?? ''}`;
|
|
873
|
-
}
|
|
874
|
-
|
|
875
|
-
private remember(): void {
|
|
876
|
-
if (this.kiosk) return;
|
|
877
|
-
try {
|
|
878
|
-
localStorage.setItem(this.storageKey(), this.layoutJSON);
|
|
879
|
-
} catch {
|
|
880
|
-
/* storage unavailable: the layout just is not remembered */
|
|
881
|
-
}
|
|
882
|
-
}
|
|
883
|
-
|
|
884
|
-
private forget(): void {
|
|
885
|
-
try {
|
|
886
|
-
localStorage.removeItem(this.storageKey());
|
|
887
|
-
} catch {
|
|
888
|
-
/* ignore */
|
|
889
|
-
}
|
|
890
|
-
}
|
|
891
|
-
|
|
892
|
-
private recall(): SavedLayout | null {
|
|
893
|
-
try {
|
|
894
|
-
return this.parseLayout(localStorage.getItem(this.storageKey()));
|
|
895
|
-
} catch {
|
|
896
|
-
return null;
|
|
897
|
-
}
|
|
898
|
-
}
|
|
899
|
-
|
|
900
|
-
private parseLayout(text: string | null): SavedLayout | null {
|
|
901
|
-
if (!text) return null;
|
|
902
|
-
try {
|
|
903
|
-
const v = JSON.parse(text) as Partial<SavedLayout>;
|
|
904
|
-
if (v.version !== 1 || typeof v.group !== 'string' || typeof v.e !== 'number') return null;
|
|
905
|
-
if (v.tree !== null && !isLayoutNode(v.tree)) return null;
|
|
906
|
-
if (!v.panels || typeof v.panels !== 'object') return null;
|
|
907
|
-
return v as SavedLayout;
|
|
908
|
-
} catch {
|
|
909
|
-
return null;
|
|
910
|
-
}
|
|
911
|
-
}
|
|
912
|
-
|
|
913
|
-
// ---- drawing and readouts -----------------------------------------------
|
|
914
|
-
|
|
915
|
-
private currentF(p: Panel): number {
|
|
916
|
-
const pr = Math.max(0, Math.min(1, this.progress));
|
|
917
|
-
switch (this.spacing) {
|
|
918
|
-
case 'anomaly':
|
|
919
|
-
return pr * p.period;
|
|
920
|
-
case 'time':
|
|
921
|
-
return trueAnomalyFromTime(p.sys.e, p.sys.f0, pr * p.tauPeriod) - p.sys.f0;
|
|
922
|
-
case 'arclength': {
|
|
923
|
-
const m = p.fRel.length;
|
|
924
|
-
if (m < 2) return 0;
|
|
925
|
-
const x = pr * (m - 1);
|
|
926
|
-
const k = Math.min(m - 2, Math.floor(x));
|
|
927
|
-
return p.fRel[k] + (p.fRel[k + 1] - p.fRel[k]) * (x - k);
|
|
928
|
-
}
|
|
929
|
-
}
|
|
930
|
-
}
|
|
931
|
-
|
|
932
|
-
private firstPanel(): Panel | undefined {
|
|
933
|
-
const ids = panelIds(this.tree);
|
|
934
|
-
return ids.length ? this.panels.get(ids[0]) : undefined;
|
|
935
|
-
}
|
|
936
|
-
|
|
937
|
-
private drawAll(): void {
|
|
938
|
-
for (const p of this.panels.values()) if (p.fRel.length) p.viewer.showAt(this.currentF(p));
|
|
939
|
-
this.updateReadout();
|
|
940
|
-
}
|
|
941
|
-
|
|
942
|
-
private updateCaption(): void {
|
|
943
|
-
const branches = [...new Set([...this.panels.values()].map((p) => p.branch))].join(' and ');
|
|
944
|
-
const mu = this.branchDataset(BRANCHES[0])?.system.mu ?? this.branchDataset(BRANCHES[1])?.system.mu;
|
|
945
|
-
this.captionEl.textContent = this.group
|
|
946
|
-
? `${this.group}${branches ? `, ${branches}` : ''}, e ≈ ${this.e.toFixed(3)}${mu !== undefined ? `, μ = ${mu}` : ''}`
|
|
947
|
-
: '';
|
|
948
|
-
this.slider.value = String(this.e);
|
|
949
|
-
this.sliderOut.textContent = this.e.toFixed(4);
|
|
950
|
-
}
|
|
951
|
-
|
|
952
|
-
private updateReadout(): void {
|
|
953
|
-
const p = this.firstPanel();
|
|
954
|
-
if (!p || !p.fRel.length) {
|
|
955
|
-
this.readout.textContent = '';
|
|
956
|
-
return;
|
|
957
|
-
}
|
|
958
|
-
const f = this.currentF(p);
|
|
959
|
-
const tau = elapsedTime(p.sys.e, p.sys.f0, p.sys.f0 + f);
|
|
960
|
-
const frame = Math.round(this.progress * STEPS_PER_REV);
|
|
961
|
-
this.readout.textContent = `f − f₀ = ${f.toFixed(2)} rad τ = ${tau.toFixed(2)} TU frame ${frame}/${STEPS_PER_REV}`;
|
|
962
|
-
this.scrub.value = String(Math.round(this.progress * 1000));
|
|
963
|
-
}
|
|
964
|
-
|
|
965
|
-
// ---- transport ----------------------------------------------------------
|
|
966
|
-
|
|
967
|
-
private togglePlay(): void {
|
|
968
|
-
if (this.playing) this.pause();
|
|
969
|
-
else this.play();
|
|
970
|
-
}
|
|
971
|
-
|
|
972
|
-
play(): void {
|
|
973
|
-
if (this.playing) return;
|
|
974
|
-
this.playing = true;
|
|
975
|
-
this.playBtn.textContent = 'pause';
|
|
976
|
-
this.lastTick = 0;
|
|
977
|
-
this.fpsEma = 0;
|
|
978
|
-
const tick = (now: number) => {
|
|
979
|
-
if (!this.playing) return;
|
|
980
|
-
if (this.lastTick) {
|
|
981
|
-
const inst = 1000 / Math.max(1, now - this.lastTick);
|
|
982
|
-
this.fpsEma = this.fpsEma ? this.fpsEma * 0.9 + inst * 0.1 : inst;
|
|
983
|
-
this.fpsEl.textContent = `${this.fpsEma.toFixed(0)} fps`;
|
|
984
|
-
}
|
|
985
|
-
this.lastTick = now;
|
|
986
|
-
this.progress += this.speed / (STEPS_PER_REV * SLOWDOWN);
|
|
987
|
-
if (this.progress >= 1) this.progress -= 1;
|
|
988
|
-
this.drawAll();
|
|
989
|
-
this.raf = requestAnimationFrame(tick);
|
|
990
|
-
};
|
|
991
|
-
this.raf = requestAnimationFrame(tick);
|
|
992
|
-
}
|
|
993
|
-
|
|
994
|
-
pause(): void {
|
|
995
|
-
this.playing = false;
|
|
996
|
-
this.playBtn.textContent = 'play';
|
|
997
|
-
this.fpsEl.textContent = '— fps';
|
|
998
|
-
cancelAnimationFrame(this.raf);
|
|
999
|
-
}
|
|
1000
|
-
|
|
1001
|
-
private onKey(ev: KeyboardEvent): void {
|
|
1002
|
-
const step = (ev.shiftKey ? 10 : 1) / STEPS_PER_REV;
|
|
1003
|
-
switch (ev.key) {
|
|
1004
|
-
case ' ':
|
|
1005
|
-
ev.preventDefault();
|
|
1006
|
-
this.togglePlay();
|
|
1007
|
-
break;
|
|
1008
|
-
case 'ArrowRight':
|
|
1009
|
-
ev.preventDefault();
|
|
1010
|
-
this.pause();
|
|
1011
|
-
this.progress = (this.progress + step) % 1;
|
|
1012
|
-
this.drawAll();
|
|
1013
|
-
break;
|
|
1014
|
-
case 'ArrowLeft':
|
|
1015
|
-
ev.preventDefault();
|
|
1016
|
-
this.pause();
|
|
1017
|
-
this.progress = (this.progress - step + 1) % 1;
|
|
1018
|
-
this.drawAll();
|
|
1019
|
-
break;
|
|
1020
|
-
case 'ArrowUp':
|
|
1021
|
-
ev.preventDefault();
|
|
1022
|
-
this.setE(this.e + this.eStep * (ev.shiftKey ? 10 : 1));
|
|
1023
|
-
break;
|
|
1024
|
-
case 'ArrowDown':
|
|
1025
|
-
ev.preventDefault();
|
|
1026
|
-
this.setE(this.e - this.eStep * (ev.shiftKey ? 10 : 1));
|
|
1027
|
-
break;
|
|
1028
|
-
}
|
|
1029
|
-
}
|
|
1030
|
-
}
|
|
1031
|
-
|
|
1032
|
-
export function define(tag = 'tycho-viewer'): void {
|
|
1033
|
-
if (!customElements.get(tag)) customElements.define(tag, TychoViewerElement);
|
|
1034
|
-
}
|