@pencil-lang/figure 0.0.1 → 0.0.3
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 +41 -5
- package/astro/Figure.astro +23 -9
- package/astro/build.d.ts +95 -0
- package/astro/build.js +50 -12
- package/astro/index.d.ts +20 -0
- package/astro/index.js +16 -1
- package/package.json +5 -2
- package/pkg/pencil-figure.min.js +21 -12
- package/pkg/pencil_web.js +146 -29
- package/pkg/pencil_web_bg.wasm +0 -0
- package/src/element.js +352 -40
- package/src/host.js +1 -0
- package/src/scheme.d.ts +7 -0
- package/src/scheme.js +62 -0
package/src/element.js
CHANGED
|
@@ -29,6 +29,8 @@
|
|
|
29
29
|
// already has its shape. A `<source>` child picks other sources while its `media` matches, as in
|
|
30
30
|
// `<picture>`: the first that matches wins, and the element's own attributes are the rest. It can
|
|
31
31
|
// give `compiled`, or `sheets` (and `src`, which is the element's if it gives none).
|
|
32
|
+
// Where a page pins a theme over the reader's system, with `color-scheme` in its CSS, the element
|
|
33
|
+
// answers `prefers-color-scheme` in those `media` with the page's scheme (`scheme.js`).
|
|
32
34
|
//
|
|
33
35
|
// Attributes: `src` and `sheets` (space-separated URLs, in order), or `compiled`, `params`
|
|
34
36
|
// (`a: 0.5; beat: 2`, held as if a reader set them), `paused` to start paused, and `gl` to skip
|
|
@@ -36,12 +38,22 @@
|
|
|
36
38
|
// is defined: changes made before that script finishes are fetched once.
|
|
37
39
|
// Its labels are an em of the text where it sits, and follow when a reader changes their text size.
|
|
38
40
|
// `reload()` fetches the sources again, for a page that watches them change.
|
|
41
|
+
// A handle is dragged with a mouse, a pen or a finger, or tabbed to and moved with the keys. A touch
|
|
42
|
+
// within a handle's reach drags it and doesn't scroll the page; anywhere else on the figure the
|
|
43
|
+
// page scrolls and zooms as it would over an image. Nothing in the figure is text to select, and a
|
|
44
|
+
// drag never selects the page's. The element does all this itself: a page needs no CSS for it.
|
|
45
|
+
// A browser can lose the GPU under a figure, after sleep or when its GPU process restarts. The
|
|
46
|
+
// figure then starts its GPU again, keeping its sources, time, held params and whether it's
|
|
47
|
+
// paused, and shows its poster meanwhile. It tries at once, after a second and after five, then
|
|
48
|
+
// says `error` (with `detail.gpu`), and tries once more each time the page is shown.
|
|
39
49
|
// Events: `load` when a figure is compiled, `draw` after each frame drawn, `resize` when its drawn
|
|
40
50
|
// size or its em changes (its `detail.em`, in CSS pixels), `input` when a reader
|
|
41
|
-
// drags a param, `play` and `pause`,
|
|
42
|
-
|
|
51
|
+
// drags a param, `play` and `pause`, `error` (its `detail.error`) when one can't load or draw, and
|
|
52
|
+
// `recover` when one that said `error` because it lost its GPU draws again.
|
|
53
|
+
import { Player, let_go, figure_size, figure_size_compiled, ready, isReady, gather, ahead } from './host.js';
|
|
43
54
|
import { LABEL_CSS, LabelLayer, fonts } from './labels.js';
|
|
44
55
|
import { match } from './fit.js';
|
|
56
|
+
import { follow, settle, unfollow } from './scheme.js';
|
|
45
57
|
|
|
46
58
|
// How long time runs before it loops, for a figure that declares no `duration`.
|
|
47
59
|
const LOOP = 10;
|
|
@@ -50,6 +62,12 @@ const REACH = 24;
|
|
|
50
62
|
// How far from the viewport the GPU starts, so a figure is drawn by the time it's scrolled to.
|
|
51
63
|
const NEAR = '200px';
|
|
52
64
|
|
|
65
|
+
// How long a figure has to draw after starting again before a loss counts as a new one.
|
|
66
|
+
const SETTLED = 30_000;
|
|
67
|
+
// When a figure that has lost its GPU tries again: at once, then after a moment for a GPU process
|
|
68
|
+
// that's restarting, then a little longer. After those it says `error`.
|
|
69
|
+
const RETRIES = [0, 1000, 5000];
|
|
70
|
+
|
|
53
71
|
const fetchText = async (url) => {
|
|
54
72
|
const response = await fetch(new URL(url, document.baseURI));
|
|
55
73
|
if (!response.ok) throw new Error(`${url}: ${response.status}`);
|
|
@@ -57,7 +75,10 @@ const fetchText = async (url) => {
|
|
|
57
75
|
};
|
|
58
76
|
|
|
59
77
|
const CSS = `
|
|
78
|
+
/* Nothing in a figure is text to select, and a long press on it shows no callout: a handle is
|
|
79
|
+
pressed and held to be dragged. WebKit doesn't carry the host's down, so each part says it. */
|
|
60
80
|
:host { display: block; }
|
|
81
|
+
:host, .stage *, ::slotted(*) { -webkit-user-select: none; user-select: none; -webkit-touch-callout: none; -webkit-tap-highlight-color: transparent; }
|
|
61
82
|
:host([hidden]) { display: none; }
|
|
62
83
|
.stage { position: relative; overflow: hidden; width: 100%; }
|
|
63
84
|
canvas { display: block; width: 100%; aspect-ratio: var(--pencil-aspect, 16 / 9); outline: none; }
|
|
@@ -69,11 +90,17 @@ slot { display: none; }
|
|
|
69
90
|
::slotted(source) { display: none; }
|
|
70
91
|
::slotted(img), ::slotted(picture) { display: block; width: 100%; height: auto; }
|
|
71
92
|
.em { position: absolute; left: 0; top: 0; width: 1em; height: 0; visibility: hidden; pointer-events: none; }
|
|
72
|
-
/* Something to focus over each handle
|
|
73
|
-
|
|
74
|
-
|
|
93
|
+
/* Something to focus over each handle: a reader on a keyboard tabs to it and moves it with the
|
|
94
|
+
arrow keys. Its ring is two tones, so it shows on any paper. A pointer takes it within the
|
|
95
|
+
handle's reach, the circle its ::before fills, and a touch there drags the handle and doesn't
|
|
96
|
+
scroll the page. Anywhere else on the figure the touch goes to the canvas, which leaves the
|
|
97
|
+
page to scroll and zoom as it would over an image. */
|
|
75
98
|
.handles { position: absolute; inset: 0; pointer-events: none; }
|
|
76
|
-
.handle { position: absolute; left: -14px; top: -14px; width: 28px; height: 28px; border-radius: 50%; outline: none; will-change: transform; }
|
|
99
|
+
.handle { position: absolute; left: -14px; top: -14px; width: 28px; height: 28px; border-radius: 50%; outline: none; will-change: transform; pointer-events: auto; touch-action: none; }
|
|
100
|
+
.handle::before { content: ''; position: absolute; inset: ${14 - REACH}px; border-radius: 50%; clip-path: var(--reach, none); }
|
|
101
|
+
/* With no GPU, as while the poster shows, a handle can't be dragged, so it takes no pointer, and
|
|
102
|
+
a touch on it scrolls the page. The keys still find it. */
|
|
103
|
+
.stage:not(.live) .handle { pointer-events: none; touch-action: auto; }
|
|
77
104
|
.handle:focus-visible { outline: 2px solid var(--pencil-focus, currentColor); outline-offset: 1px; box-shadow: 0 0 0 2px var(--pencil-focus-halo, Canvas); }
|
|
78
105
|
.handle[hidden] { display: none; }
|
|
79
106
|
.said { position: absolute; width: 1px; height: 1px; overflow: hidden; clip-path: inset(50%); white-space: nowrap; }
|
|
@@ -129,6 +156,24 @@ export class PencilFigure extends HTMLElement {
|
|
|
129
156
|
#fresh = false;
|
|
130
157
|
#seen = null;
|
|
131
158
|
#resized = null;
|
|
159
|
+
// Starting again after the GPU is lost: how many times since the figure last settled, when it
|
|
160
|
+
// last did, whether it has drawn since, whether it's waiting for the page to be seen again,
|
|
161
|
+
// and that the `load` a start sends isn't news.
|
|
162
|
+
#tries = 0;
|
|
163
|
+
#restarted = 0;
|
|
164
|
+
#healthy = false;
|
|
165
|
+
#awaiting = false;
|
|
166
|
+
#quiet = false;
|
|
167
|
+
#retry = 0; // the timer for the next try
|
|
168
|
+
#reported = false; // whether it said `error` since it last drew
|
|
169
|
+
#playerId = -1; // the player's number and backend, which a trapped player can't say
|
|
170
|
+
#backend = null;
|
|
171
|
+
// A WebGL2 canvas whose context is being brought back, for the next player, and a start that
|
|
172
|
+
// waits for it.
|
|
173
|
+
#restoring = false;
|
|
174
|
+
#restoreTimer = 0;
|
|
175
|
+
#startPending = false;
|
|
176
|
+
#lose = null; // the canvas's `WEBGL_lose_context`, which a lost context doesn't give
|
|
132
177
|
|
|
133
178
|
constructor() {
|
|
134
179
|
super();
|
|
@@ -153,6 +198,19 @@ export class PencilFigure extends HTMLElement {
|
|
|
153
198
|
this.#said.className = 'said';
|
|
154
199
|
this.#said.setAttribute('aria-live', 'polite');
|
|
155
200
|
this.#stage.append(this.#handleLayer, this.#said);
|
|
201
|
+
// A press on the canvas or on a handle's reach comes here, and a grab captures the pointer.
|
|
202
|
+
const stage = this.#stage;
|
|
203
|
+
stage.addEventListener('pointerdown', this.#pointerDown);
|
|
204
|
+
stage.addEventListener('pointermove', this.#pointerMove);
|
|
205
|
+
stage.addEventListener('pointerup', this.#pointerEnd);
|
|
206
|
+
stage.addEventListener('pointercancel', this.#pointerEnd);
|
|
207
|
+
// A grab focuses its handle, and the press mustn't take the focus away again, or start a
|
|
208
|
+
// selection that follows the pointer out over the page. Nor does a long press open a menu.
|
|
209
|
+
const grabbing = (e) => {
|
|
210
|
+
if (this.#player?.grabbed() >= 0) e.preventDefault();
|
|
211
|
+
};
|
|
212
|
+
stage.addEventListener('mousedown', grabbing);
|
|
213
|
+
stage.addEventListener('contextmenu', grabbing);
|
|
156
214
|
this.addEventListener('keydown', this.#key);
|
|
157
215
|
try {
|
|
158
216
|
this.#internals = this.attachInternals();
|
|
@@ -161,9 +219,9 @@ export class PencilFigure extends HTMLElement {
|
|
|
161
219
|
|
|
162
220
|
// ---- The poster and the sources ----
|
|
163
221
|
|
|
164
|
-
/** Whether the element has a poster: any child that isn't a `<source
|
|
222
|
+
/** Whether the element has a poster: any child that isn't a `<source>`, or a script. */
|
|
165
223
|
#hasPoster() {
|
|
166
|
-
return [...this.children].some((child) => child.localName !== 'source');
|
|
224
|
+
return [...this.children].some((child) => child.localName !== 'source' && child.localName !== 'script');
|
|
167
225
|
}
|
|
168
226
|
|
|
169
227
|
/** Shows the poster until a frame is drawn. */
|
|
@@ -227,12 +285,18 @@ export class PencilFigure extends HTMLElement {
|
|
|
227
285
|
this.#resized.observe(this.#em);
|
|
228
286
|
// Children can arrive after the element does, while the page is still being read.
|
|
229
287
|
this.#children = new MutationObserver(() => {
|
|
288
|
+
// A `<source>` that has just arrived is answered too. What that changes comes back here
|
|
289
|
+
// once, and then changes nothing.
|
|
290
|
+
settle(this);
|
|
230
291
|
this.#wait();
|
|
231
292
|
this.#listen();
|
|
232
293
|
this.#refetch();
|
|
233
294
|
});
|
|
234
295
|
this.#children.observe(this, { childList: true, attributes: true, subtree: true, attributeFilter: ['media', 'src', 'sheets', 'compiled'] });
|
|
235
296
|
document.fonts?.addEventListener('loadingdone', this.#fontsChanged);
|
|
297
|
+
document.addEventListener('visibilitychange', this.#shown);
|
|
298
|
+
window.addEventListener('pageshow', this.#shown);
|
|
299
|
+
follow(this);
|
|
236
300
|
this.#wait();
|
|
237
301
|
this.#listen();
|
|
238
302
|
this.#refetch();
|
|
@@ -248,9 +312,12 @@ export class PencilFigure extends HTMLElement {
|
|
|
248
312
|
|
|
249
313
|
disconnectedCallback() {
|
|
250
314
|
document.fonts?.removeEventListener('loadingdone', this.#fontsChanged);
|
|
315
|
+
document.removeEventListener('visibilitychange', this.#shown);
|
|
316
|
+
window.removeEventListener('pageshow', this.#shown);
|
|
251
317
|
this.#seen?.disconnect();
|
|
252
318
|
this.#resized?.disconnect();
|
|
253
319
|
this.#children?.disconnect();
|
|
320
|
+
unfollow(this);
|
|
254
321
|
this.#listen();
|
|
255
322
|
this.#visible = false;
|
|
256
323
|
this.#sync();
|
|
@@ -262,7 +329,7 @@ export class PencilFigure extends HTMLElement {
|
|
|
262
329
|
|
|
263
330
|
attributeChangedCallback(name) {
|
|
264
331
|
if (name === 'params') {
|
|
265
|
-
|
|
332
|
+
this.#hold(parseParams(this.getAttribute('params')));
|
|
266
333
|
} else {
|
|
267
334
|
this.#refetch();
|
|
268
335
|
}
|
|
@@ -272,13 +339,20 @@ export class PencilFigure extends HTMLElement {
|
|
|
272
339
|
const canvas = document.createElement('canvas');
|
|
273
340
|
canvas.setAttribute('part', 'canvas');
|
|
274
341
|
if (this.#size) canvas.style.setProperty('--pencil-aspect', `${this.#size[0]} / ${this.#size[1]}`);
|
|
275
|
-
|
|
276
|
-
canvas
|
|
277
|
-
canvas
|
|
278
|
-
canvas.addEventListener('
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
if (this.#player
|
|
342
|
+
// WebGL says so when it loses its context, and drawing on carries on drawing nothing. The
|
|
343
|
+
// context is kept, to be brought back, since a browser counts a canvas's against its cap
|
|
344
|
+
// until the canvas is collected, which WebKit can leave for a long while.
|
|
345
|
+
canvas.addEventListener('webglcontextlost', (e) => {
|
|
346
|
+
e.preventDefault();
|
|
347
|
+
if (canvas !== this.#canvas) return;
|
|
348
|
+
if (this.#player) this.#recover(new Error('GPU context lost'));
|
|
349
|
+
else if (this.#restoring) this.#restoreLater(canvas);
|
|
350
|
+
});
|
|
351
|
+
canvas.addEventListener('webglcontextrestored', () => {
|
|
352
|
+
if (canvas !== this.#canvas || !this.#restoring) return;
|
|
353
|
+
this.#restoring = false;
|
|
354
|
+
clearTimeout(this.#restoreTimer);
|
|
355
|
+
this.#startIfPending();
|
|
282
356
|
});
|
|
283
357
|
return canvas;
|
|
284
358
|
}
|
|
@@ -286,6 +360,11 @@ export class PencilFigure extends HTMLElement {
|
|
|
286
360
|
/** Starts the GPU, once, when the element is near the viewport. */
|
|
287
361
|
async #start() {
|
|
288
362
|
if (this.#player || this.#starting) return;
|
|
363
|
+
// Its WebGL context is on its way back.
|
|
364
|
+
if (this.#restoring) {
|
|
365
|
+
this.#startPending = true;
|
|
366
|
+
return;
|
|
367
|
+
}
|
|
289
368
|
this.#starting = true;
|
|
290
369
|
const generation = this.#generation;
|
|
291
370
|
try {
|
|
@@ -299,6 +378,14 @@ export class PencilFigure extends HTMLElement {
|
|
|
299
378
|
return;
|
|
300
379
|
}
|
|
301
380
|
this.#player = player;
|
|
381
|
+
this.#playerId = player.id;
|
|
382
|
+
this.#backend = player.backend;
|
|
383
|
+
this.#lose = player.backend === 'Gl' ? (this.#canvas.getContext('webgl2')?.getExtension('WEBGL_lose_context') ?? null) : null;
|
|
384
|
+
this.#stage.classList.add('live');
|
|
385
|
+
// A browser loses a GPU device after sleep or when its GPU process restarts, and says so.
|
|
386
|
+
player.on_lost(() => {
|
|
387
|
+
if (this.#player === player) this.#recover(new Error('GPU device lost'));
|
|
388
|
+
});
|
|
302
389
|
player.set_measure(this.#layer.measure);
|
|
303
390
|
this.#setEm();
|
|
304
391
|
if (this.#sources) {
|
|
@@ -308,13 +395,169 @@ export class PencilFigure extends HTMLElement {
|
|
|
308
395
|
this.#fail(error);
|
|
309
396
|
}
|
|
310
397
|
}
|
|
398
|
+
// Started, so nothing is waiting to try again, and the next `load` is news.
|
|
399
|
+
this.#awaiting = false;
|
|
400
|
+
this.#quiet = false;
|
|
401
|
+
clearTimeout(this.#retry);
|
|
402
|
+
this.#retry = 0;
|
|
311
403
|
} catch (error) {
|
|
312
|
-
|
|
404
|
+
// Starting again can fail while the browser's GPU is still coming back.
|
|
405
|
+
if (this.#tries && generation === this.#generation) {
|
|
406
|
+
this.#starting = false;
|
|
407
|
+
this.#recover(error);
|
|
408
|
+
} else this.#fail(error);
|
|
313
409
|
} finally {
|
|
314
410
|
if (generation === this.#generation) this.#starting = false;
|
|
315
411
|
}
|
|
316
412
|
}
|
|
317
413
|
|
|
414
|
+
// ---- A lost GPU ----
|
|
415
|
+
|
|
416
|
+
/** Whether a draw failed on the GPU's side, which a new player gets past: a trap inside the
|
|
417
|
+
* WASM (`unreachable`), after which that player can't be used again ("recursive use of an
|
|
418
|
+
* object"), a lost device or context, or no texture to draw into, as when a draw comes before
|
|
419
|
+
* the word that the device is lost. A GPU error on a device that isn't lost is the figure's,
|
|
420
|
+
* and is said at once, as any other. A trap can be the core's too: that one fails each time it's
|
|
421
|
+
* tried, and is said once the tries are done. */
|
|
422
|
+
#recoverable(error) {
|
|
423
|
+
if (error instanceof WebAssembly.RuntimeError) return true;
|
|
424
|
+
const message = String(error?.message ?? '');
|
|
425
|
+
if (/recursive use of an object|^GPU (device lost|context lost|surface)/.test(message)) return true;
|
|
426
|
+
if (!message.startsWith('GPU error:')) return false;
|
|
427
|
+
try {
|
|
428
|
+
return this.#player?.lost ?? false;
|
|
429
|
+
} catch {
|
|
430
|
+
return true;
|
|
431
|
+
}
|
|
432
|
+
}
|
|
433
|
+
|
|
434
|
+
/**
|
|
435
|
+
* The GPU went away under the figure, or a draw failed on its side: start the GPU again, keeping
|
|
436
|
+
* the figure's sources, time, held params and whether it's paused, with its poster meanwhile.
|
|
437
|
+
* It's tried at once, then after a second and after five, for a GPU process that's coming back,
|
|
438
|
+
* and then it's an `error` (with `detail.gpu`), tried once more each time the page is shown. A
|
|
439
|
+
* figure that draws again after that says `recover`. One that has drawn for a while since, or
|
|
440
|
+
* that the reader has come back to, has its tries back.
|
|
441
|
+
*/
|
|
442
|
+
#recover(error) {
|
|
443
|
+
clearTimeout(this.#retry);
|
|
444
|
+
this.#retry = 0;
|
|
445
|
+
if (this.#healthy && performance.now() - this.#restarted > SETTLED) this.#tries = 0;
|
|
446
|
+
this.#tries += 1;
|
|
447
|
+
this.#dropGpu();
|
|
448
|
+
const wait = RETRIES[this.#tries - 1];
|
|
449
|
+
if (wait === undefined) {
|
|
450
|
+
this.#awaiting = true;
|
|
451
|
+
this.#reported = true;
|
|
452
|
+
this.dispatchEvent(new CustomEvent('error', { detail: { error, time: this.#time, fresh: false, gpu: true } }));
|
|
453
|
+
} else if (wait === 0) {
|
|
454
|
+
this.#restart();
|
|
455
|
+
} else {
|
|
456
|
+
this.#retry = setTimeout(() => {
|
|
457
|
+
this.#retry = 0;
|
|
458
|
+
this.#restart();
|
|
459
|
+
}, wait);
|
|
460
|
+
}
|
|
461
|
+
}
|
|
462
|
+
|
|
463
|
+
#restart() {
|
|
464
|
+
this.#restarted = performance.now();
|
|
465
|
+
this.#quiet = true;
|
|
466
|
+
if (this.#visible) this.#start();
|
|
467
|
+
}
|
|
468
|
+
|
|
469
|
+
/** The page was seen again, or brought back from the back/forward cache. */
|
|
470
|
+
#shown = () => {
|
|
471
|
+
if (document.visibilityState !== 'visible') return;
|
|
472
|
+
if (this.#awaiting && !this.#player) {
|
|
473
|
+
// One more try, which says `error` again if it fails.
|
|
474
|
+
this.#awaiting = false;
|
|
475
|
+
this.#tries = RETRIES.length;
|
|
476
|
+
this.#restart();
|
|
477
|
+
} else if (this.#healthy) {
|
|
478
|
+
this.#tries = 0;
|
|
479
|
+
}
|
|
480
|
+
};
|
|
481
|
+
|
|
482
|
+
/** Lets go of a player, even one that trapped, which can't be borrowed again: its device is
|
|
483
|
+
* let go of by its number. */
|
|
484
|
+
#letGo(player) {
|
|
485
|
+
try {
|
|
486
|
+
player.destroy();
|
|
487
|
+
} catch {
|
|
488
|
+
let_go(this.#playerId);
|
|
489
|
+
}
|
|
490
|
+
}
|
|
491
|
+
|
|
492
|
+
/** Lets go of the player, and nothing else, as `#teardown` does with the rest. The poster shows
|
|
493
|
+
* again until the figure draws. A WebGL2 canvas keeps its context, lost and brought back for
|
|
494
|
+
* the next player, and a WebGPU one gets a new canvas. */
|
|
495
|
+
#dropGpu() {
|
|
496
|
+
cancelAnimationFrame(this.#raf);
|
|
497
|
+
this.#raf = 0;
|
|
498
|
+
this.#idle = true;
|
|
499
|
+
this.#generation += 1;
|
|
500
|
+
this.#starting = false;
|
|
501
|
+
this.#healthy = false;
|
|
502
|
+
this.#stage.classList.remove('live');
|
|
503
|
+
const player = this.#player;
|
|
504
|
+
this.#player = null;
|
|
505
|
+
// The clock stops where it is, and runs again once there's a figure to draw.
|
|
506
|
+
this.#sync();
|
|
507
|
+
if (player) {
|
|
508
|
+
this.#letGo(player);
|
|
509
|
+
if (this.#backend === 'Gl') this.#restore(this.#canvas);
|
|
510
|
+
else this.#freshCanvas();
|
|
511
|
+
}
|
|
512
|
+
this.#drawn = false;
|
|
513
|
+
this.#wait();
|
|
514
|
+
}
|
|
515
|
+
|
|
516
|
+
/** Loses a WebGL2 context, if it isn't already, and brings it back, for the next player. One the
|
|
517
|
+
* browser lost and doesn't bring back within two seconds is given up on, for a new canvas. */
|
|
518
|
+
#restore(canvas) {
|
|
519
|
+
const gl = canvas.getContext('webgl2');
|
|
520
|
+
if (!gl || !this.#lose) {
|
|
521
|
+
this.#freshCanvas();
|
|
522
|
+
return;
|
|
523
|
+
}
|
|
524
|
+
this.#restoring = true;
|
|
525
|
+
// Its `webglcontextlost` brings it back; one already lost is brought back now.
|
|
526
|
+
if (gl.isContextLost()) this.#restoreLater(canvas);
|
|
527
|
+
else this.#lose.loseContext();
|
|
528
|
+
clearTimeout(this.#restoreTimer);
|
|
529
|
+
this.#restoreTimer = setTimeout(() => {
|
|
530
|
+
if (!this.#restoring || canvas !== this.#canvas) return;
|
|
531
|
+
this.#restoring = false;
|
|
532
|
+
this.#freshCanvas();
|
|
533
|
+
this.#startIfPending();
|
|
534
|
+
}, 2000);
|
|
535
|
+
}
|
|
536
|
+
|
|
537
|
+
/** Asks for a lost context back, once the event that says it's lost is done: only a loss whose
|
|
538
|
+
* event was cancelled can be restored. One the browser lost comes back by itself, if it can. */
|
|
539
|
+
#restoreLater(canvas) {
|
|
540
|
+
setTimeout(() => {
|
|
541
|
+
if (this.#restoring && canvas === this.#canvas) this.#lose?.restoreContext();
|
|
542
|
+
}, 0);
|
|
543
|
+
}
|
|
544
|
+
|
|
545
|
+
#startIfPending() {
|
|
546
|
+
if (!this.#startPending) return;
|
|
547
|
+
this.#startPending = false;
|
|
548
|
+
if (this.#visible) this.#start();
|
|
549
|
+
}
|
|
550
|
+
|
|
551
|
+
/** A new canvas in place of the old one, which keeps its context for life. */
|
|
552
|
+
#freshCanvas() {
|
|
553
|
+
this.#lose = null;
|
|
554
|
+
const canvas = this.#makeCanvas();
|
|
555
|
+
this.#resized?.unobserve(this.#canvas);
|
|
556
|
+
this.#canvas.replaceWith(canvas);
|
|
557
|
+
this.#canvas = canvas;
|
|
558
|
+
this.#resized?.observe(canvas);
|
|
559
|
+
}
|
|
560
|
+
|
|
318
561
|
/** Lets go of the clock and the GPU. The figure's sources, time and held params are kept, so
|
|
319
562
|
* it starts again where it was if it's put back. */
|
|
320
563
|
#teardown() {
|
|
@@ -324,15 +567,27 @@ export class PencilFigure extends HTMLElement {
|
|
|
324
567
|
cancelAnimationFrame(this.#raf);
|
|
325
568
|
this.#raf = 0;
|
|
326
569
|
this.#idle = true;
|
|
327
|
-
const started = this.#player || this.#starting;
|
|
570
|
+
const started = this.#player || this.#starting || this.#restoring;
|
|
328
571
|
this.#generation += 1;
|
|
329
572
|
this.#starting = false;
|
|
573
|
+
// Nothing waits to try again: a figure put back starts afresh.
|
|
574
|
+
clearTimeout(this.#retry);
|
|
575
|
+
this.#retry = 0;
|
|
576
|
+
this.#awaiting = false;
|
|
577
|
+
this.#quiet = false;
|
|
578
|
+
this.#tries = 0;
|
|
579
|
+
this.#reported = false;
|
|
580
|
+
this.#restoring = false;
|
|
581
|
+
this.#startPending = false;
|
|
582
|
+
clearTimeout(this.#restoreTimer);
|
|
583
|
+
this.#stage.classList.remove('live');
|
|
330
584
|
if (this.#player) {
|
|
331
|
-
|
|
332
|
-
this.#player.destroy();
|
|
585
|
+
this.#letGo(this.#player);
|
|
333
586
|
this.#player = null;
|
|
334
|
-
|
|
587
|
+
// A browser has only so many contexts live, and loses the oldest past that.
|
|
588
|
+
if (this.#backend === 'Gl') this.#lose?.loseContext();
|
|
335
589
|
else this.#canvas.getContext('webgpu')?.unconfigure();
|
|
590
|
+
this.#lose = null;
|
|
336
591
|
}
|
|
337
592
|
// A canvas keeps its context for life, so a figure put back gets a new one.
|
|
338
593
|
if (started) {
|
|
@@ -466,7 +721,9 @@ export class PencilFigure extends HTMLElement {
|
|
|
466
721
|
this.#fresh = true;
|
|
467
722
|
this.#invalidate();
|
|
468
723
|
this.#sync();
|
|
469
|
-
|
|
724
|
+
// A figure started again after losing its GPU is the same figure.
|
|
725
|
+
if (this.#quiet) this.#quiet = false;
|
|
726
|
+
else this.dispatchEvent(new Event('load'));
|
|
470
727
|
}
|
|
471
728
|
|
|
472
729
|
/** Shapes the figure to its design size, so nothing moves as it loads. */
|
|
@@ -492,15 +749,15 @@ export class PencilFigure extends HTMLElement {
|
|
|
492
749
|
return this.#info?.duration ?? LOOP;
|
|
493
750
|
}
|
|
494
751
|
|
|
495
|
-
/** The time last drawn, in seconds. Setting it seeks, and
|
|
496
|
-
* it
|
|
752
|
+
/** The time last drawn, in seconds. Setting it seeks, and hands every param time drives back
|
|
753
|
+
* to time, as a reader setting any param hands back the params whose bindings read it. */
|
|
497
754
|
get time() {
|
|
498
755
|
return this.#time;
|
|
499
756
|
}
|
|
500
757
|
|
|
501
758
|
set time(seconds) {
|
|
502
759
|
this.#seek(seconds);
|
|
503
|
-
if (this.#driven.has(
|
|
760
|
+
for (const name of [...this.#held.keys()]) if (this.#driven.has(name)) this.#release(name);
|
|
504
761
|
this.#invalidate();
|
|
505
762
|
}
|
|
506
763
|
|
|
@@ -651,14 +908,40 @@ export class PencilFigure extends HTMLElement {
|
|
|
651
908
|
}
|
|
652
909
|
}
|
|
653
910
|
|
|
654
|
-
/** Takes a param over from
|
|
911
|
+
/** Takes a param over from its binding, at this value, as a reader setting it does, until it's
|
|
912
|
+
* released, the figure plays, or a param its binding reads is set. */
|
|
655
913
|
hold(name, value) {
|
|
656
|
-
this.#
|
|
657
|
-
|
|
658
|
-
|
|
914
|
+
this.#hold([[name, value]]);
|
|
915
|
+
}
|
|
916
|
+
|
|
917
|
+
/** Holds params together: none of them hands another back. */
|
|
918
|
+
#hold(pairs) {
|
|
919
|
+
this.#reclaim(pairs.map(([name]) => name));
|
|
920
|
+
for (const [name, value] of pairs) {
|
|
921
|
+
this.#held.delete(name);
|
|
922
|
+
this.#held.set(name, value);
|
|
923
|
+
this.#player?.set_param(name, value);
|
|
924
|
+
}
|
|
659
925
|
this.#invalidate();
|
|
660
926
|
}
|
|
661
927
|
|
|
928
|
+
/**
|
|
929
|
+
* A param with a binding has two drivers, its binding and the reader, and the reader's last
|
|
930
|
+
* action picks. So when a reader sets params, every other held param whose binding reads one
|
|
931
|
+
* of them (`reads`, in `info.params`, which the core works out) goes back to its binding, as
|
|
932
|
+
* θ in Bounds goes back to closing the flower when q or the case is set. Before a figure is
|
|
933
|
+
* loaded nothing is known of its bindings, so holds made then are all kept.
|
|
934
|
+
*/
|
|
935
|
+
#reclaim(names) {
|
|
936
|
+
const params = this.#info?.params;
|
|
937
|
+
if (!params) return;
|
|
938
|
+
for (const name of [...this.#held.keys()]) {
|
|
939
|
+
if (names.includes(name)) continue;
|
|
940
|
+
const reads = params.find((p) => p.id === name)?.reads ?? [];
|
|
941
|
+
if (reads.some((read) => names.includes(read))) this.#release(name);
|
|
942
|
+
}
|
|
943
|
+
}
|
|
944
|
+
|
|
662
945
|
/** Hands a param back to its binding, or every param with no name. */
|
|
663
946
|
release(name) {
|
|
664
947
|
for (const each of name === undefined ? [...this.#held.keys()] : [name]) this.#release(each);
|
|
@@ -675,13 +958,13 @@ export class PencilFigure extends HTMLElement {
|
|
|
675
958
|
return new Map(this.#held);
|
|
676
959
|
}
|
|
677
960
|
|
|
678
|
-
/** Every param's value this frame, by name. Setting holds each one given. */
|
|
961
|
+
/** Every param's value this frame, by name. Setting holds each one given, together. */
|
|
679
962
|
get params() {
|
|
680
963
|
return Object.fromEntries((this.#info?.params ?? []).map((p) => [p.id, this.value(p.id)]));
|
|
681
964
|
}
|
|
682
965
|
|
|
683
966
|
set params(values) {
|
|
684
|
-
|
|
967
|
+
this.#hold(Object.entries(values));
|
|
685
968
|
}
|
|
686
969
|
|
|
687
970
|
/** The figure's design size, `[width, height]` in design pixels, from its sheets. */
|
|
@@ -718,13 +1001,14 @@ export class PencilFigure extends HTMLElement {
|
|
|
718
1001
|
}
|
|
719
1002
|
|
|
720
1003
|
#pointerDown = (e) => {
|
|
721
|
-
|
|
1004
|
+
// One pointer drags at a time, and only with the main button.
|
|
1005
|
+
if (!this.#player || !e.isPrimary || e.button !== 0) return;
|
|
722
1006
|
const [x, y] = this.#device(e);
|
|
723
1007
|
try {
|
|
724
1008
|
if (!this.#player.grab(this.#time, x, y, REACH * (window.devicePixelRatio || 1))) return;
|
|
725
1009
|
this.pause();
|
|
726
|
-
this.#
|
|
727
|
-
this.#
|
|
1010
|
+
this.#stage.setPointerCapture(e.pointerId);
|
|
1011
|
+
this.#stage.style.cursor = 'grabbing';
|
|
728
1012
|
// The keys carry on from where the pointer leaves it.
|
|
729
1013
|
this.#handles[this.#player.grabbed()]?.focus({ preventScroll: true });
|
|
730
1014
|
this.#drag(x, y);
|
|
@@ -735,8 +1019,8 @@ export class PencilFigure extends HTMLElement {
|
|
|
735
1019
|
if (!this.#player) return;
|
|
736
1020
|
const [x, y] = this.#device(e);
|
|
737
1021
|
try {
|
|
738
|
-
if (!this.#
|
|
739
|
-
this.#
|
|
1022
|
+
if (!this.#stage.hasPointerCapture(e.pointerId)) {
|
|
1023
|
+
this.#stage.style.cursor = this.#player.grab(this.#time, x, y, REACH * (window.devicePixelRatio || 1)) ? 'grab' : '';
|
|
740
1024
|
this.#player.release();
|
|
741
1025
|
return;
|
|
742
1026
|
}
|
|
@@ -745,9 +1029,10 @@ export class PencilFigure extends HTMLElement {
|
|
|
745
1029
|
};
|
|
746
1030
|
|
|
747
1031
|
#pointerEnd = (e) => {
|
|
748
|
-
if (this.#
|
|
1032
|
+
if (!this.#stage.hasPointerCapture(e.pointerId)) return;
|
|
1033
|
+
this.#stage.releasePointerCapture(e.pointerId);
|
|
749
1034
|
this.#player?.release();
|
|
750
|
-
this.#
|
|
1035
|
+
this.#stage.style.cursor = '';
|
|
751
1036
|
};
|
|
752
1037
|
|
|
753
1038
|
#drag(x, y) {
|
|
@@ -758,6 +1043,7 @@ export class PencilFigure extends HTMLElement {
|
|
|
758
1043
|
#moved(name) {
|
|
759
1044
|
if (!name) return;
|
|
760
1045
|
const value = this.#player.value(name, this.#time);
|
|
1046
|
+
this.#reclaim([name]);
|
|
761
1047
|
this.#held.delete(name);
|
|
762
1048
|
this.#held.set(name, value);
|
|
763
1049
|
this.#invalidate();
|
|
@@ -820,6 +1106,8 @@ export class PencilFigure extends HTMLElement {
|
|
|
820
1106
|
return;
|
|
821
1107
|
}
|
|
822
1108
|
const scale = this.#canvas.clientWidth / this.#canvas.width;
|
|
1109
|
+
const [width, height] = [this.#canvas.clientWidth, this.#canvas.clientHeight];
|
|
1110
|
+
const panels = this.#info.panels ?? [];
|
|
823
1111
|
this.#handles.forEach((el, i) => {
|
|
824
1112
|
const [x, y] = [places[2 * i] * scale, places[2 * i + 1] * scale];
|
|
825
1113
|
const there = Number.isFinite(x) && Number.isFinite(y);
|
|
@@ -827,6 +1115,14 @@ export class PencilFigure extends HTMLElement {
|
|
|
827
1115
|
if (!there) return;
|
|
828
1116
|
const moved = `translate(${x.toFixed(2)}px, ${y.toFixed(2)}px)`;
|
|
829
1117
|
if (el.style.transform !== moved) el.style.transform = moved;
|
|
1118
|
+
// A handle can't be grabbed outside its panel, so its reach stops there, and a touch past
|
|
1119
|
+
// it scrolls the page. The figure's own edge already stops it.
|
|
1120
|
+
if (panels.length > 1) {
|
|
1121
|
+
const [px, py, pw, ph] = panels[this.#info.handles[i].panel]?.rect ?? [0, 0, 1, 1];
|
|
1122
|
+
const cut = [py * height - (y - REACH), x + REACH - (px + pw) * width, y + REACH - (py + ph) * height, px * width - (x - REACH)];
|
|
1123
|
+
const reach = cut.some((c) => c > 0) ? `inset(${cut.map((c) => `${Math.max(0, c).toFixed(1)}px`).join(' ')})` : 'none';
|
|
1124
|
+
if (el.style.getPropertyValue('--reach') !== reach) el.style.setProperty('--reach', reach);
|
|
1125
|
+
}
|
|
830
1126
|
const param = this.#info.handles[i].param;
|
|
831
1127
|
const value = this.#valueAt(param, this.#time);
|
|
832
1128
|
const [min, max] = this.range(param);
|
|
@@ -942,6 +1238,10 @@ export class PencilFigure extends HTMLElement {
|
|
|
942
1238
|
try {
|
|
943
1239
|
this.#layer.update(JSON.parse(this.#player.draw(this.#time, dpr)));
|
|
944
1240
|
} catch (error) {
|
|
1241
|
+
if (this.#recoverable(error)) {
|
|
1242
|
+
this.#recover(error);
|
|
1243
|
+
return;
|
|
1244
|
+
}
|
|
945
1245
|
this.pause();
|
|
946
1246
|
this.dispatchEvent(new CustomEvent('error', { detail: { error, time: this.#time, fresh } }));
|
|
947
1247
|
return;
|
|
@@ -950,7 +1250,12 @@ export class PencilFigure extends HTMLElement {
|
|
|
950
1250
|
this.#drawn = true;
|
|
951
1251
|
this.#wait();
|
|
952
1252
|
}
|
|
1253
|
+
this.#healthy = true;
|
|
953
1254
|
this.#placeHandles();
|
|
1255
|
+
if (this.#reported) {
|
|
1256
|
+
this.#reported = false;
|
|
1257
|
+
this.dispatchEvent(new Event('recover'));
|
|
1258
|
+
}
|
|
954
1259
|
this.dispatchEvent(new CustomEvent('draw', { detail: { time: this.#time, fresh } }));
|
|
955
1260
|
}
|
|
956
1261
|
|
|
@@ -972,8 +1277,15 @@ export class PencilFigure extends HTMLElement {
|
|
|
972
1277
|
return;
|
|
973
1278
|
}
|
|
974
1279
|
const [width, height] = [this.#canvas.width, this.#canvas.height];
|
|
975
|
-
|
|
976
|
-
|
|
1280
|
+
let em;
|
|
1281
|
+
try {
|
|
1282
|
+
this.#player.resize(...match(this.#canvas));
|
|
1283
|
+
em = this.#setEm();
|
|
1284
|
+
} catch (error) {
|
|
1285
|
+
if (!this.#recoverable(error)) throw error;
|
|
1286
|
+
this.#recover(error);
|
|
1287
|
+
return;
|
|
1288
|
+
}
|
|
977
1289
|
if (em || this.#canvas.width !== width || this.#canvas.height !== height) {
|
|
978
1290
|
this.#paint();
|
|
979
1291
|
this.dispatchEvent(new CustomEvent('resize', { detail: { em: this.#emPx } }));
|
package/src/host.js
CHANGED
package/src/scheme.d.ts
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
// Types for `scheme.js`, which `Figure.astro` imports.
|
|
2
|
+
|
|
3
|
+
/** Answers `prefers-color-scheme` in the `media` of every `<source>` in a figure with the scheme
|
|
4
|
+
* the page pins there, and gives that scheme, or `null` with none pinned. */
|
|
5
|
+
export declare function settle(figure: Element): 'light' | 'dark' | null;
|
|
6
|
+
export declare function follow(figure: Element): void;
|
|
7
|
+
export declare function unfollow(figure: Element): void;
|