@pencil-lang/figure 0.0.4 → 0.0.6
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/README.md +105 -4
- package/astro/Figure.astro +27 -2
- package/astro/build.d.ts +29 -1
- package/astro/build.js +9 -3
- package/package.json +2 -1
- package/pkg/pencil-figure.min.js +75 -9
- package/pkg/pencil_web.js +60 -9
- package/pkg/pencil_web_bg.wasm +0 -0
- package/src/controls.js +417 -0
- package/src/element.js +174 -4
- package/src/labels.js +37 -26
- package/src/wiring.js +313 -0
package/src/element.js
CHANGED
|
@@ -46,6 +46,16 @@
|
|
|
46
46
|
// figure then starts its GPU again, keeping its sources, time, held params and whether it's
|
|
47
47
|
// paused, and shows its poster meanwhile. It tries at once, after a second and after five, then
|
|
48
48
|
// says `error` (with `detail.gpu`), and tries once more each time the page is shown.
|
|
49
|
+
// `controls` asks for the default controls, as `<video controls>` does: play and time when anything
|
|
50
|
+
// follows time, beat stepping with its caption, a control for each param a handle doesn't set, and
|
|
51
|
+
// the readouts. They're `<pencil-controls slot="controls">`, a child whose shadow root holds them,
|
|
52
|
+
// styled through `::part` and custom properties (`controls.js`). A build writes it into the page,
|
|
53
|
+
// with every size reserved, and disabled until the figure loads (the Astro component does); a page
|
|
54
|
+
// that wrote none gets one when the figure loads. Without the attribute they're hidden, and what a
|
|
55
|
+
// page put in `<pencil-controls>` stays, as `<video controls>` does. A page with its own fills the `controls` slot
|
|
56
|
+
// with inputs and outputs marked `data-param="q"` or `data-readout="ratio"`, and the element sets
|
|
57
|
+
// their ranges, steps and values and follows them each frame (`wiring.js`), with or without the
|
|
58
|
+
// attribute. `readouts` is what the readouts say this frame.
|
|
49
59
|
// Events: `load` when a figure is compiled, `draw` after each frame drawn, `resize` when its drawn
|
|
50
60
|
// size or its em changes (its `detail.em`, in CSS pixels), `input` when a reader
|
|
51
61
|
// drags a param (its `detail.name` and `detail.value`, and `detail.values`, every param that one
|
|
@@ -56,6 +66,8 @@ import { Player, let_go, figure_size, figure_size_compiled, ready, isReady, gath
|
|
|
56
66
|
import { LABEL_CSS, LabelLayer, fonts } from './labels.js';
|
|
57
67
|
import { match } from './fit.js';
|
|
58
68
|
import { follow, settle, unfollow } from './scheme.js';
|
|
69
|
+
import { CONTROLS_CSS, hasControls, markup, shape } from './controls.js';
|
|
70
|
+
import { Wiring, identity } from './wiring.js';
|
|
59
71
|
|
|
60
72
|
// How long time runs before it loops, for a figure that declares no `duration`.
|
|
61
73
|
const LOOP = 10;
|
|
@@ -106,8 +118,19 @@ slot { display: none; }
|
|
|
106
118
|
.handle:focus-visible { outline: 2px solid var(--pencil-focus, currentColor); outline-offset: 1px; box-shadow: 0 0 0 2px var(--pencil-focus-halo, Canvas); }
|
|
107
119
|
.handle[hidden] { display: none; }
|
|
108
120
|
.said { position: absolute; width: 1px; height: 1px; overflow: hidden; clip-path: inset(50%); white-space: nowrap; }
|
|
121
|
+
slot[name='controls'] { display: block; }
|
|
109
122
|
${LABEL_CSS}`;
|
|
110
123
|
|
|
124
|
+
/** The defaults' styles, made once and shared by every figure's controls the element writes. */
|
|
125
|
+
let controlsSheet = null;
|
|
126
|
+
const controlsStyles = () => {
|
|
127
|
+
if (!controlsSheet && typeof CSSStyleSheet !== 'undefined' && 'replaceSync' in CSSStyleSheet.prototype) {
|
|
128
|
+
controlsSheet = new CSSStyleSheet();
|
|
129
|
+
controlsSheet.replaceSync(CONTROLS_CSS);
|
|
130
|
+
}
|
|
131
|
+
return controlsSheet;
|
|
132
|
+
};
|
|
133
|
+
|
|
111
134
|
/** `a: 0.5; beat: 2` as pairs. */
|
|
112
135
|
const parseParams = (text) =>
|
|
113
136
|
(text ?? '')
|
|
@@ -117,7 +140,7 @@ const parseParams = (text) =>
|
|
|
117
140
|
.map(([name, value]) => [name, Number(value)]);
|
|
118
141
|
|
|
119
142
|
export class PencilFigure extends HTMLElement {
|
|
120
|
-
static observedAttributes = ['src', 'sheets', 'compiled', 'params'];
|
|
143
|
+
static observedAttributes = ['src', 'sheets', 'compiled', 'params', 'controls'];
|
|
121
144
|
|
|
122
145
|
/** Resolves once the WASM core is ready, after which `load` checks sources at once. */
|
|
123
146
|
static get ready() {
|
|
@@ -176,6 +199,9 @@ export class PencilFigure extends HTMLElement {
|
|
|
176
199
|
#restoreTimer = 0;
|
|
177
200
|
#startPending = false;
|
|
178
201
|
#lose = null; // the canvas's `WEBGL_lose_context`, which a lost context doesn't give
|
|
202
|
+
#controlsSlot; // where the controls go, below the figure
|
|
203
|
+
#wiring = new Wiring(this);
|
|
204
|
+
#readouts = null; // what the readouts say, and at which time, worked out once a frame
|
|
179
205
|
|
|
180
206
|
constructor() {
|
|
181
207
|
super();
|
|
@@ -200,6 +226,11 @@ export class PencilFigure extends HTMLElement {
|
|
|
200
226
|
this.#said.className = 'said';
|
|
201
227
|
this.#said.setAttribute('aria-live', 'polite');
|
|
202
228
|
this.#stage.append(this.#handleLayer, this.#said);
|
|
229
|
+
// The controls, the defaults or a page's own, below the figure.
|
|
230
|
+
this.#controlsSlot = document.createElement('slot');
|
|
231
|
+
this.#controlsSlot.name = 'controls';
|
|
232
|
+
this.#controlsSlot.addEventListener('slotchange', () => this.#wire());
|
|
233
|
+
root.append(this.#controlsSlot);
|
|
203
234
|
// A press on the canvas or on a handle's reach comes here, and a grab captures the pointer.
|
|
204
235
|
const stage = this.#stage;
|
|
205
236
|
stage.addEventListener('pointerdown', this.#pointerDown);
|
|
@@ -221,9 +252,20 @@ export class PencilFigure extends HTMLElement {
|
|
|
221
252
|
|
|
222
253
|
// ---- The poster and the sources ----
|
|
223
254
|
|
|
224
|
-
/** Whether the element has a poster: any child that isn't a `<source>`, or a
|
|
255
|
+
/** Whether the element has a poster: any child that isn't a `<source>`, a script, or in a slot,
|
|
256
|
+
* as its controls are. */
|
|
225
257
|
#hasPoster() {
|
|
226
|
-
return [...this.children].some(
|
|
258
|
+
return [...this.children].some(
|
|
259
|
+
(child) => child.localName !== 'source' && child.localName !== 'script' && !child.hasAttribute('slot'),
|
|
260
|
+
);
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
/** Whether a node is in the controls, which change each frame and say nothing of the figure. */
|
|
264
|
+
#inSlot(node) {
|
|
265
|
+
for (let at = node; at && at !== this; at = at.parentNode) {
|
|
266
|
+
if (at.parentNode === this && at.nodeType === 1 && at.hasAttribute('slot')) return true;
|
|
267
|
+
}
|
|
268
|
+
return false;
|
|
227
269
|
}
|
|
228
270
|
|
|
229
271
|
/** Shows the poster until a frame is drawn. */
|
|
@@ -286,7 +328,8 @@ export class PencilFigure extends HTMLElement {
|
|
|
286
328
|
this.#resized.observe(this.#canvas);
|
|
287
329
|
this.#resized.observe(this.#em);
|
|
288
330
|
// Children can arrive after the element does, while the page is still being read.
|
|
289
|
-
this.#children = new MutationObserver(() => {
|
|
331
|
+
this.#children = new MutationObserver((records) => {
|
|
332
|
+
if (records.every((record) => this.#inSlot(record.target))) return;
|
|
290
333
|
// A `<source>` that has just arrived is answered too. What that changes comes back here
|
|
291
334
|
// once, and then changes nothing.
|
|
292
335
|
settle(this);
|
|
@@ -332,6 +375,9 @@ export class PencilFigure extends HTMLElement {
|
|
|
332
375
|
attributeChangedCallback(name) {
|
|
333
376
|
if (name === 'params') {
|
|
334
377
|
this.#hold(parseParams(this.getAttribute('params')));
|
|
378
|
+
} else if (name === 'controls') {
|
|
379
|
+
// As `<video controls>`: without it the defaults are hidden, and what the page wrote stays.
|
|
380
|
+
if (this.#info) this.#controlsReady();
|
|
335
381
|
} else {
|
|
336
382
|
this.#refetch();
|
|
337
383
|
}
|
|
@@ -720,6 +766,8 @@ export class PencilFigure extends HTMLElement {
|
|
|
720
766
|
}
|
|
721
767
|
this.#retime();
|
|
722
768
|
this.#operable();
|
|
769
|
+
this.#readouts = null;
|
|
770
|
+
this.#controlsReady();
|
|
723
771
|
this.#fresh = true;
|
|
724
772
|
this.#invalidate();
|
|
725
773
|
this.#sync();
|
|
@@ -728,6 +776,118 @@ export class PencilFigure extends HTMLElement {
|
|
|
728
776
|
else this.dispatchEvent(new Event('load'));
|
|
729
777
|
}
|
|
730
778
|
|
|
779
|
+
// ---- Controls ----
|
|
780
|
+
|
|
781
|
+
/** What's in the controls slot: a `<pencil-controls>` by its shadow root, and a page's own
|
|
782
|
+
* element as it is. */
|
|
783
|
+
#controlsRoots() {
|
|
784
|
+
return this.#controlsSlot
|
|
785
|
+
.assignedElements()
|
|
786
|
+
.map((el) => (el.localName === 'pencil-controls' && el.shadowRoot ? el.shadowRoot : el));
|
|
787
|
+
}
|
|
788
|
+
|
|
789
|
+
/** Listens to what's in the controls slot, and shows the figure in it, once there's a figure. */
|
|
790
|
+
#wire() {
|
|
791
|
+
this.#wiring.attach(this.#controlsRoots());
|
|
792
|
+
if (this.#info) this.#wiring.enable();
|
|
793
|
+
}
|
|
794
|
+
|
|
795
|
+
/**
|
|
796
|
+
* The figure has loaded: writes the default controls if the page asked for them and has none,
|
|
797
|
+
* or writes them again if they were made for another kind of figure, as after a reader picks
|
|
798
|
+
* another look, and turns them on. Controls a build wrote for this kind of figure are kept as
|
|
799
|
+
* they are, so nothing moves.
|
|
800
|
+
*/
|
|
801
|
+
#controlsReady() {
|
|
802
|
+
const info = this.#info;
|
|
803
|
+
if (!info) return;
|
|
804
|
+
const asked = this.hasAttribute('controls');
|
|
805
|
+
const slotted = this.querySelectorAll(':scope > [slot="controls"]');
|
|
806
|
+
if (asked && slotted.length === 0 && hasControls(info)) {
|
|
807
|
+
const made = document.createElement('pencil-controls');
|
|
808
|
+
made.setAttribute('slot', 'controls');
|
|
809
|
+
made.attachShadow({ mode: 'open' });
|
|
810
|
+
this.#writeControls(made, info);
|
|
811
|
+
this.append(made);
|
|
812
|
+
}
|
|
813
|
+
for (const host of this.querySelectorAll(':scope > pencil-controls[slot="controls"]')) {
|
|
814
|
+
if (!asked) {
|
|
815
|
+
// What a page put in shows, and the defaults don't. One the page wrote without a shadow
|
|
816
|
+
// root of defaults, as the Astro component does without `controls`, is the page's alone.
|
|
817
|
+
if (host.shadowRoot && host.dataset.shape !== 'page') this.#writeControls(host, null);
|
|
818
|
+
host.hidden = host.children.length === 0;
|
|
819
|
+
continue;
|
|
820
|
+
}
|
|
821
|
+
// A declarative shadow root that wasn't read, as in HTML a script put in the page, isn't
|
|
822
|
+
// there: the element writes the defaults into one of its own.
|
|
823
|
+
const unread = !host.shadowRoot || host.shadowRoot.childElementCount === 0;
|
|
824
|
+
// Its template is left as a child, which isn't the page's own content.
|
|
825
|
+
if (unread) for (const left of host.querySelectorAll(':scope > template[shadowrootmode]')) left.remove();
|
|
826
|
+
if (!host.shadowRoot) host.attachShadow({ mode: 'open' });
|
|
827
|
+
if (unread || host.dataset.shape !== shape(info)) this.#writeControls(host, info);
|
|
828
|
+
// A figure with nothing to control, as another look can be, shows only what the page put in.
|
|
829
|
+
host.hidden = !hasControls(info) && host.children.length === 0;
|
|
830
|
+
}
|
|
831
|
+
this.#wiring.attach(this.#controlsRoots());
|
|
832
|
+
this.#wiring.enable();
|
|
833
|
+
}
|
|
834
|
+
|
|
835
|
+
/** Writes the defaults into a `<pencil-controls>`, as a build would for this figure as it stands,
|
|
836
|
+
* or with no `info`, only what the page put in it. */
|
|
837
|
+
#writeControls(host, info) {
|
|
838
|
+
const root = host.shadowRoot;
|
|
839
|
+
// A reader who was on a control is still on it afterwards, as one who picked a look with the
|
|
840
|
+
// keyboard: the same control, found by what it's for, and a menu that was open is open still.
|
|
841
|
+
const focused = root.activeElement ? identity(root.activeElement) : null;
|
|
842
|
+
const open = !!root.querySelector('[popover]:popover-open');
|
|
843
|
+
let html = '<div class="controls" part="controls"><div class="rest" part="rest"><slot></slot></div></div>';
|
|
844
|
+
if (info) {
|
|
845
|
+
let room = [];
|
|
846
|
+
try {
|
|
847
|
+
room = JSON.parse(this.#player.readout_room(this.#time));
|
|
848
|
+
} catch {}
|
|
849
|
+
const values = Object.fromEntries(info.params.map((p) => [p.id, this.value(p.id)]));
|
|
850
|
+
const ranges = Object.fromEntries(info.params.map((p) => [p.id, this.range(p.id)]));
|
|
851
|
+
const sized = { ...info, readouts: (info.readouts ?? []).map((r, i) => ({ ...r, room: room[i] })) };
|
|
852
|
+
html = markup({
|
|
853
|
+
info: sized,
|
|
854
|
+
moment: { time: this.#time, values, ranges, readouts: this.readouts },
|
|
855
|
+
paused: this.paused,
|
|
856
|
+
extra: host.children.length > 0,
|
|
857
|
+
});
|
|
858
|
+
}
|
|
859
|
+
const sheet = controlsStyles();
|
|
860
|
+
if (sheet) {
|
|
861
|
+
root.adoptedStyleSheets = [sheet];
|
|
862
|
+
root.innerHTML = html;
|
|
863
|
+
} else {
|
|
864
|
+
root.innerHTML = `<style>${CONTROLS_CSS}</style>${html}`;
|
|
865
|
+
}
|
|
866
|
+
host.dataset.shape = info ? shape(info) : 'page';
|
|
867
|
+
this.#wiring.forget();
|
|
868
|
+
if (open) {
|
|
869
|
+
try {
|
|
870
|
+
root.querySelector('[popover]')?.showPopover();
|
|
871
|
+
} catch {}
|
|
872
|
+
}
|
|
873
|
+
if (focused) root.querySelector(focused)?.focus();
|
|
874
|
+
}
|
|
875
|
+
|
|
876
|
+
/** What each readout says this frame, `{ id, text, words, math, width, height, laid }`, in the
|
|
877
|
+
* order they're written: a readout is a label with no place, said beside the figure. Its layout
|
|
878
|
+
* is in ems, from the left and middle of its line (`readoutHtml` in `controls.js` sets one). */
|
|
879
|
+
get readouts() {
|
|
880
|
+
if (!this.#player || !this.#info?.readouts?.length) return [];
|
|
881
|
+
if (this.#readouts?.time !== this.#time) {
|
|
882
|
+
let said = [];
|
|
883
|
+
try {
|
|
884
|
+
said = JSON.parse(this.#player.readouts(this.#time));
|
|
885
|
+
} catch {}
|
|
886
|
+
this.#readouts = { time: this.#time, said };
|
|
887
|
+
}
|
|
888
|
+
return this.#readouts.said;
|
|
889
|
+
}
|
|
890
|
+
|
|
731
891
|
/** Shapes the figure to its design size, so nothing moves as it loads. */
|
|
732
892
|
#shape(size) {
|
|
733
893
|
this.#size = size;
|
|
@@ -772,6 +932,7 @@ export class PencilFigure extends HTMLElement {
|
|
|
772
932
|
/** Plays from where it is. Each param time drives is handed back to time, carrying on from
|
|
773
933
|
* where the reader left the last one they moved. */
|
|
774
934
|
play() {
|
|
935
|
+
this.#readouts = null;
|
|
775
936
|
const moved = [...this.#held.keys()].filter((name) => this.#driven.has(name)).pop();
|
|
776
937
|
if (moved && this.#player) {
|
|
777
938
|
try {
|
|
@@ -783,6 +944,7 @@ export class PencilFigure extends HTMLElement {
|
|
|
783
944
|
this.#paused = false;
|
|
784
945
|
this.#sync();
|
|
785
946
|
this.#invalidate();
|
|
947
|
+
this.#wiring.paintPlay();
|
|
786
948
|
if (was !== false) this.dispatchEvent(new Event('play'));
|
|
787
949
|
}
|
|
788
950
|
|
|
@@ -790,6 +952,7 @@ export class PencilFigure extends HTMLElement {
|
|
|
790
952
|
const was = this.#paused;
|
|
791
953
|
this.#paused = true;
|
|
792
954
|
this.#sync();
|
|
955
|
+
this.#wiring.paintPlay();
|
|
793
956
|
if (was !== true) this.dispatchEvent(new Event('pause'));
|
|
794
957
|
}
|
|
795
958
|
|
|
@@ -918,6 +1081,7 @@ export class PencilFigure extends HTMLElement {
|
|
|
918
1081
|
|
|
919
1082
|
/** Holds params together: none of them hands another back. */
|
|
920
1083
|
#hold(pairs) {
|
|
1084
|
+
this.#readouts = null;
|
|
921
1085
|
this.#reclaim(pairs.map(([name]) => name));
|
|
922
1086
|
for (const [name, value] of pairs) {
|
|
923
1087
|
this.#held.delete(name);
|
|
@@ -1197,6 +1361,8 @@ export class PencilFigure extends HTMLElement {
|
|
|
1197
1361
|
/** A key on the figure itself: ← and → step its beats, and Home and End go to the first and
|
|
1198
1362
|
* last. What it came to is said for a screen reader, with the beat's caption. */
|
|
1199
1363
|
#key = (e) => {
|
|
1364
|
+
// Keys in the controls are theirs: an arrow on a slider moves the slider.
|
|
1365
|
+
if (e.target !== this) return;
|
|
1200
1366
|
if (e.defaultPrevented || e.altKey || e.ctrlKey || e.metaKey || !this.#player || !this.#steps()) return;
|
|
1201
1367
|
const range = this.#info.params.find((p) => p.id === 'beat');
|
|
1202
1368
|
if (e.key === 'ArrowRight') this.step(1);
|
|
@@ -1212,6 +1378,8 @@ export class PencilFigure extends HTMLElement {
|
|
|
1212
1378
|
const timed = (this.#info.beatLengths?.length ?? 0) > 0;
|
|
1213
1379
|
const n = Math.round(timed ? beat : beat + 1);
|
|
1214
1380
|
const caption = beats[n - 1] ?? '';
|
|
1381
|
+
// Controls that say each new beat say this one, and a beat is said once.
|
|
1382
|
+
if (this.#wiring.speaks) return;
|
|
1215
1383
|
this.#said.textContent = n < 1 ? 'Start' : `Step ${n} of ${beats.length}${caption ? `: ${caption}` : ''}`;
|
|
1216
1384
|
};
|
|
1217
1385
|
|
|
@@ -1269,6 +1437,8 @@ export class PencilFigure extends HTMLElement {
|
|
|
1269
1437
|
}
|
|
1270
1438
|
this.#healthy = true;
|
|
1271
1439
|
this.#placeHandles();
|
|
1440
|
+
this.#readouts = null;
|
|
1441
|
+
this.#wiring.read();
|
|
1272
1442
|
if (this.#reported) {
|
|
1273
1443
|
this.#reported = false;
|
|
1274
1444
|
this.dispatchEvent(new Event('recover'));
|
package/src/labels.js
CHANGED
|
@@ -66,10 +66,9 @@ export class LabelLayer {
|
|
|
66
66
|
this.stage = stage;
|
|
67
67
|
this.dpr = dpr;
|
|
68
68
|
// Leaders under everything, then every halo under every glyph, so one term's can't eat
|
|
69
|
-
// into its neighbor.
|
|
69
|
+
// into its neighbor. The leaders' layer is in the page only while a label draws one.
|
|
70
70
|
this.leaders = document.createElementNS(SVG, 'svg');
|
|
71
71
|
this.leaders.style.cssText = 'position:absolute;inset:0;width:100%;height:100%;pointer-events:none;overflow:visible';
|
|
72
|
-
this.stage.append(this.leaders);
|
|
73
72
|
this.halos = this.makeLayer();
|
|
74
73
|
this.glyphs = this.makeLayer();
|
|
75
74
|
this.ruler = document.createElement('canvas').getContext('2d');
|
|
@@ -125,9 +124,7 @@ export class LabelLayer {
|
|
|
125
124
|
span.dataset.label = label.id;
|
|
126
125
|
this.halos.append(halo);
|
|
127
126
|
this.glyphs.append(span);
|
|
128
|
-
|
|
129
|
-
this.leaders.append(line);
|
|
130
|
-
item = { span, halo, line, offset: target.slice(), velocity: [0, 0], shown: false };
|
|
127
|
+
item = { span, halo, line: null, offset: target.slice(), velocity: [0, 0], shown: false };
|
|
131
128
|
this.items.set(label.id, item);
|
|
132
129
|
}
|
|
133
130
|
const { span, halo } = item;
|
|
@@ -144,8 +141,8 @@ export class LabelLayer {
|
|
|
144
141
|
item.text = label.text;
|
|
145
142
|
item.size = label.size;
|
|
146
143
|
item.math = label.math;
|
|
147
|
-
item.
|
|
148
|
-
item.
|
|
144
|
+
item.color = label.color;
|
|
145
|
+
item.opacity = label.opacity;
|
|
149
146
|
// What's laid out is rebuilt only when it changes, which a label that only moves doesn't.
|
|
150
147
|
const laid = label.laid ? JSON.stringify(label.laid) : '';
|
|
151
148
|
item.half = label.laid ? [label.laid.halfWidth, label.laid.halfHeight] : null;
|
|
@@ -175,7 +172,7 @@ export class LabelLayer {
|
|
|
175
172
|
if (!present.has(id)) {
|
|
176
173
|
item.span.hidden = true;
|
|
177
174
|
item.halo.hidden = true;
|
|
178
|
-
item
|
|
175
|
+
this.dropLeader(item);
|
|
179
176
|
item.shown = false;
|
|
180
177
|
}
|
|
181
178
|
}
|
|
@@ -214,28 +211,42 @@ export class LabelLayer {
|
|
|
214
211
|
}
|
|
215
212
|
}
|
|
216
213
|
|
|
217
|
-
/** As `frame::leader_ends`: from its gap off the point to a little short of the text's box.
|
|
214
|
+
/** As `frame::leader_ends`: from its gap off the point to a little short of the text's box.
|
|
215
|
+
* A label has a line only while it draws one: most labels have no leader, and one that has can
|
|
216
|
+
* sit too near its point to need it. */
|
|
218
217
|
placeLeader(item, x, y) {
|
|
219
|
-
const {
|
|
218
|
+
const { leader, anchor } = item;
|
|
220
219
|
const [dx, dy] = [x - anchor[0], y - anchor[1]];
|
|
221
220
|
const apart = Math.hypot(dx, dy);
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
line.setAttribute('y1', a[1]);
|
|
233
|
-
line.setAttribute('x2', b[0]);
|
|
234
|
-
line.setAttribute('y2', b[1]);
|
|
235
|
-
line.style.strokeWidth = `${leader.width / this.dpr}px`;
|
|
236
|
-
}
|
|
221
|
+
if (!leader || !(apart > 0)) return this.dropLeader(item);
|
|
222
|
+
const [ux, uy] = [dx / apart, dy / apart];
|
|
223
|
+
const half = item.half ?? [this.measure(item.text, item.size, item.math) / 2, item.size * TEXT_HALF_HEIGHT];
|
|
224
|
+
const toEdge = Math.min(half[0] / Math.max(Math.abs(ux), 1e-9), half[1] / Math.max(Math.abs(uy), 1e-9));
|
|
225
|
+
const end = apart - toEdge - LEADER_MARGIN * item.size;
|
|
226
|
+
if (!(end > leader.gap)) return this.dropLeader(item);
|
|
227
|
+
if (!item.line) {
|
|
228
|
+
item.line = document.createElementNS(SVG, 'line');
|
|
229
|
+
if (this.leaders.parentNode !== this.stage) this.stage.insertBefore(this.leaders, this.halos);
|
|
230
|
+
this.leaders.append(item.line);
|
|
237
231
|
}
|
|
238
|
-
|
|
232
|
+
const { line } = item;
|
|
233
|
+
const at = (d) => [(anchor[0] + ux * d) / this.dpr, (anchor[1] + uy * d) / this.dpr];
|
|
234
|
+
const [a, b] = [at(leader.gap), at(end)];
|
|
235
|
+
line.setAttribute('x1', a[0]);
|
|
236
|
+
line.setAttribute('y1', a[1]);
|
|
237
|
+
line.setAttribute('x2', b[0]);
|
|
238
|
+
line.setAttribute('y2', b[1]);
|
|
239
|
+
line.style.strokeWidth = `${leader.width / this.dpr}px`;
|
|
240
|
+
line.style.stroke = item.color;
|
|
241
|
+
line.style.opacity = item.opacity;
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
/** Takes a label's line out, and the layer with the last one. */
|
|
245
|
+
dropLeader(item) {
|
|
246
|
+
if (!item.line) return;
|
|
247
|
+
item.line.remove();
|
|
248
|
+
item.line = null;
|
|
249
|
+
if (!this.leaders.firstChild) this.leaders.remove();
|
|
239
250
|
}
|
|
240
251
|
|
|
241
252
|
/** Moves labels along by the clock. Needs no redraw of the figure. */
|