@pencil-lang/figure 0.0.2 → 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 +37 -6
- package/astro/Figure.astro +9 -1
- 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 +342 -38
- package/src/host.js +1 -0
- package/src/scheme.d.ts +7 -0
package/src/element.js
CHANGED
|
@@ -38,10 +38,19 @@
|
|
|
38
38
|
// is defined: changes made before that script finishes are fetched once.
|
|
39
39
|
// Its labels are an em of the text where it sits, and follow when a reader changes their text size.
|
|
40
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.
|
|
41
49
|
// Events: `load` when a figure is compiled, `draw` after each frame drawn, `resize` when its drawn
|
|
42
50
|
// size or its em changes (its `detail.em`, in CSS pixels), `input` when a reader
|
|
43
|
-
// drags a param, `play` and `pause`,
|
|
44
|
-
|
|
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';
|
|
45
54
|
import { LABEL_CSS, LabelLayer, fonts } from './labels.js';
|
|
46
55
|
import { match } from './fit.js';
|
|
47
56
|
import { follow, settle, unfollow } from './scheme.js';
|
|
@@ -53,6 +62,12 @@ const REACH = 24;
|
|
|
53
62
|
// How far from the viewport the GPU starts, so a figure is drawn by the time it's scrolled to.
|
|
54
63
|
const NEAR = '200px';
|
|
55
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
|
+
|
|
56
71
|
const fetchText = async (url) => {
|
|
57
72
|
const response = await fetch(new URL(url, document.baseURI));
|
|
58
73
|
if (!response.ok) throw new Error(`${url}: ${response.status}`);
|
|
@@ -60,7 +75,10 @@ const fetchText = async (url) => {
|
|
|
60
75
|
};
|
|
61
76
|
|
|
62
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. */
|
|
63
80
|
:host { display: block; }
|
|
81
|
+
:host, .stage *, ::slotted(*) { -webkit-user-select: none; user-select: none; -webkit-touch-callout: none; -webkit-tap-highlight-color: transparent; }
|
|
64
82
|
:host([hidden]) { display: none; }
|
|
65
83
|
.stage { position: relative; overflow: hidden; width: 100%; }
|
|
66
84
|
canvas { display: block; width: 100%; aspect-ratio: var(--pencil-aspect, 16 / 9); outline: none; }
|
|
@@ -72,11 +90,17 @@ slot { display: none; }
|
|
|
72
90
|
::slotted(source) { display: none; }
|
|
73
91
|
::slotted(img), ::slotted(picture) { display: block; width: 100%; height: auto; }
|
|
74
92
|
.em { position: absolute; left: 0; top: 0; width: 1em; height: 0; visibility: hidden; pointer-events: none; }
|
|
75
|
-
/* Something to focus over each handle
|
|
76
|
-
|
|
77
|
-
|
|
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. */
|
|
78
98
|
.handles { position: absolute; inset: 0; pointer-events: none; }
|
|
79
|
-
.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; }
|
|
80
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); }
|
|
81
105
|
.handle[hidden] { display: none; }
|
|
82
106
|
.said { position: absolute; width: 1px; height: 1px; overflow: hidden; clip-path: inset(50%); white-space: nowrap; }
|
|
@@ -132,6 +156,24 @@ export class PencilFigure extends HTMLElement {
|
|
|
132
156
|
#fresh = false;
|
|
133
157
|
#seen = null;
|
|
134
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
|
|
135
177
|
|
|
136
178
|
constructor() {
|
|
137
179
|
super();
|
|
@@ -156,6 +198,19 @@ export class PencilFigure extends HTMLElement {
|
|
|
156
198
|
this.#said.className = 'said';
|
|
157
199
|
this.#said.setAttribute('aria-live', 'polite');
|
|
158
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);
|
|
159
214
|
this.addEventListener('keydown', this.#key);
|
|
160
215
|
try {
|
|
161
216
|
this.#internals = this.attachInternals();
|
|
@@ -239,6 +294,8 @@ export class PencilFigure extends HTMLElement {
|
|
|
239
294
|
});
|
|
240
295
|
this.#children.observe(this, { childList: true, attributes: true, subtree: true, attributeFilter: ['media', 'src', 'sheets', 'compiled'] });
|
|
241
296
|
document.fonts?.addEventListener('loadingdone', this.#fontsChanged);
|
|
297
|
+
document.addEventListener('visibilitychange', this.#shown);
|
|
298
|
+
window.addEventListener('pageshow', this.#shown);
|
|
242
299
|
follow(this);
|
|
243
300
|
this.#wait();
|
|
244
301
|
this.#listen();
|
|
@@ -255,6 +312,8 @@ export class PencilFigure extends HTMLElement {
|
|
|
255
312
|
|
|
256
313
|
disconnectedCallback() {
|
|
257
314
|
document.fonts?.removeEventListener('loadingdone', this.#fontsChanged);
|
|
315
|
+
document.removeEventListener('visibilitychange', this.#shown);
|
|
316
|
+
window.removeEventListener('pageshow', this.#shown);
|
|
258
317
|
this.#seen?.disconnect();
|
|
259
318
|
this.#resized?.disconnect();
|
|
260
319
|
this.#children?.disconnect();
|
|
@@ -270,7 +329,7 @@ export class PencilFigure extends HTMLElement {
|
|
|
270
329
|
|
|
271
330
|
attributeChangedCallback(name) {
|
|
272
331
|
if (name === 'params') {
|
|
273
|
-
|
|
332
|
+
this.#hold(parseParams(this.getAttribute('params')));
|
|
274
333
|
} else {
|
|
275
334
|
this.#refetch();
|
|
276
335
|
}
|
|
@@ -280,13 +339,20 @@ export class PencilFigure extends HTMLElement {
|
|
|
280
339
|
const canvas = document.createElement('canvas');
|
|
281
340
|
canvas.setAttribute('part', 'canvas');
|
|
282
341
|
if (this.#size) canvas.style.setProperty('--pencil-aspect', `${this.#size[0]} / ${this.#size[1]}`);
|
|
283
|
-
|
|
284
|
-
canvas
|
|
285
|
-
canvas
|
|
286
|
-
canvas.addEventListener('
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
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();
|
|
290
356
|
});
|
|
291
357
|
return canvas;
|
|
292
358
|
}
|
|
@@ -294,6 +360,11 @@ export class PencilFigure extends HTMLElement {
|
|
|
294
360
|
/** Starts the GPU, once, when the element is near the viewport. */
|
|
295
361
|
async #start() {
|
|
296
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
|
+
}
|
|
297
368
|
this.#starting = true;
|
|
298
369
|
const generation = this.#generation;
|
|
299
370
|
try {
|
|
@@ -307,6 +378,14 @@ export class PencilFigure extends HTMLElement {
|
|
|
307
378
|
return;
|
|
308
379
|
}
|
|
309
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
|
+
});
|
|
310
389
|
player.set_measure(this.#layer.measure);
|
|
311
390
|
this.#setEm();
|
|
312
391
|
if (this.#sources) {
|
|
@@ -316,13 +395,169 @@ export class PencilFigure extends HTMLElement {
|
|
|
316
395
|
this.#fail(error);
|
|
317
396
|
}
|
|
318
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;
|
|
319
403
|
} catch (error) {
|
|
320
|
-
|
|
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);
|
|
321
409
|
} finally {
|
|
322
410
|
if (generation === this.#generation) this.#starting = false;
|
|
323
411
|
}
|
|
324
412
|
}
|
|
325
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
|
+
|
|
326
561
|
/** Lets go of the clock and the GPU. The figure's sources, time and held params are kept, so
|
|
327
562
|
* it starts again where it was if it's put back. */
|
|
328
563
|
#teardown() {
|
|
@@ -332,15 +567,27 @@ export class PencilFigure extends HTMLElement {
|
|
|
332
567
|
cancelAnimationFrame(this.#raf);
|
|
333
568
|
this.#raf = 0;
|
|
334
569
|
this.#idle = true;
|
|
335
|
-
const started = this.#player || this.#starting;
|
|
570
|
+
const started = this.#player || this.#starting || this.#restoring;
|
|
336
571
|
this.#generation += 1;
|
|
337
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');
|
|
338
584
|
if (this.#player) {
|
|
339
|
-
|
|
340
|
-
this.#player.destroy();
|
|
585
|
+
this.#letGo(this.#player);
|
|
341
586
|
this.#player = null;
|
|
342
|
-
|
|
587
|
+
// A browser has only so many contexts live, and loses the oldest past that.
|
|
588
|
+
if (this.#backend === 'Gl') this.#lose?.loseContext();
|
|
343
589
|
else this.#canvas.getContext('webgpu')?.unconfigure();
|
|
590
|
+
this.#lose = null;
|
|
344
591
|
}
|
|
345
592
|
// A canvas keeps its context for life, so a figure put back gets a new one.
|
|
346
593
|
if (started) {
|
|
@@ -474,7 +721,9 @@ export class PencilFigure extends HTMLElement {
|
|
|
474
721
|
this.#fresh = true;
|
|
475
722
|
this.#invalidate();
|
|
476
723
|
this.#sync();
|
|
477
|
-
|
|
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'));
|
|
478
727
|
}
|
|
479
728
|
|
|
480
729
|
/** Shapes the figure to its design size, so nothing moves as it loads. */
|
|
@@ -500,15 +749,15 @@ export class PencilFigure extends HTMLElement {
|
|
|
500
749
|
return this.#info?.duration ?? LOOP;
|
|
501
750
|
}
|
|
502
751
|
|
|
503
|
-
/** The time last drawn, in seconds. Setting it seeks, and
|
|
504
|
-
* 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. */
|
|
505
754
|
get time() {
|
|
506
755
|
return this.#time;
|
|
507
756
|
}
|
|
508
757
|
|
|
509
758
|
set time(seconds) {
|
|
510
759
|
this.#seek(seconds);
|
|
511
|
-
if (this.#driven.has(
|
|
760
|
+
for (const name of [...this.#held.keys()]) if (this.#driven.has(name)) this.#release(name);
|
|
512
761
|
this.#invalidate();
|
|
513
762
|
}
|
|
514
763
|
|
|
@@ -659,14 +908,40 @@ export class PencilFigure extends HTMLElement {
|
|
|
659
908
|
}
|
|
660
909
|
}
|
|
661
910
|
|
|
662
|
-
/** 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. */
|
|
663
913
|
hold(name, value) {
|
|
664
|
-
this.#
|
|
665
|
-
|
|
666
|
-
|
|
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
|
+
}
|
|
667
925
|
this.#invalidate();
|
|
668
926
|
}
|
|
669
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
|
+
|
|
670
945
|
/** Hands a param back to its binding, or every param with no name. */
|
|
671
946
|
release(name) {
|
|
672
947
|
for (const each of name === undefined ? [...this.#held.keys()] : [name]) this.#release(each);
|
|
@@ -683,13 +958,13 @@ export class PencilFigure extends HTMLElement {
|
|
|
683
958
|
return new Map(this.#held);
|
|
684
959
|
}
|
|
685
960
|
|
|
686
|
-
/** 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. */
|
|
687
962
|
get params() {
|
|
688
963
|
return Object.fromEntries((this.#info?.params ?? []).map((p) => [p.id, this.value(p.id)]));
|
|
689
964
|
}
|
|
690
965
|
|
|
691
966
|
set params(values) {
|
|
692
|
-
|
|
967
|
+
this.#hold(Object.entries(values));
|
|
693
968
|
}
|
|
694
969
|
|
|
695
970
|
/** The figure's design size, `[width, height]` in design pixels, from its sheets. */
|
|
@@ -726,13 +1001,14 @@ export class PencilFigure extends HTMLElement {
|
|
|
726
1001
|
}
|
|
727
1002
|
|
|
728
1003
|
#pointerDown = (e) => {
|
|
729
|
-
|
|
1004
|
+
// One pointer drags at a time, and only with the main button.
|
|
1005
|
+
if (!this.#player || !e.isPrimary || e.button !== 0) return;
|
|
730
1006
|
const [x, y] = this.#device(e);
|
|
731
1007
|
try {
|
|
732
1008
|
if (!this.#player.grab(this.#time, x, y, REACH * (window.devicePixelRatio || 1))) return;
|
|
733
1009
|
this.pause();
|
|
734
|
-
this.#
|
|
735
|
-
this.#
|
|
1010
|
+
this.#stage.setPointerCapture(e.pointerId);
|
|
1011
|
+
this.#stage.style.cursor = 'grabbing';
|
|
736
1012
|
// The keys carry on from where the pointer leaves it.
|
|
737
1013
|
this.#handles[this.#player.grabbed()]?.focus({ preventScroll: true });
|
|
738
1014
|
this.#drag(x, y);
|
|
@@ -743,8 +1019,8 @@ export class PencilFigure extends HTMLElement {
|
|
|
743
1019
|
if (!this.#player) return;
|
|
744
1020
|
const [x, y] = this.#device(e);
|
|
745
1021
|
try {
|
|
746
|
-
if (!this.#
|
|
747
|
-
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' : '';
|
|
748
1024
|
this.#player.release();
|
|
749
1025
|
return;
|
|
750
1026
|
}
|
|
@@ -753,9 +1029,10 @@ export class PencilFigure extends HTMLElement {
|
|
|
753
1029
|
};
|
|
754
1030
|
|
|
755
1031
|
#pointerEnd = (e) => {
|
|
756
|
-
if (this.#
|
|
1032
|
+
if (!this.#stage.hasPointerCapture(e.pointerId)) return;
|
|
1033
|
+
this.#stage.releasePointerCapture(e.pointerId);
|
|
757
1034
|
this.#player?.release();
|
|
758
|
-
this.#
|
|
1035
|
+
this.#stage.style.cursor = '';
|
|
759
1036
|
};
|
|
760
1037
|
|
|
761
1038
|
#drag(x, y) {
|
|
@@ -766,6 +1043,7 @@ export class PencilFigure extends HTMLElement {
|
|
|
766
1043
|
#moved(name) {
|
|
767
1044
|
if (!name) return;
|
|
768
1045
|
const value = this.#player.value(name, this.#time);
|
|
1046
|
+
this.#reclaim([name]);
|
|
769
1047
|
this.#held.delete(name);
|
|
770
1048
|
this.#held.set(name, value);
|
|
771
1049
|
this.#invalidate();
|
|
@@ -828,6 +1106,8 @@ export class PencilFigure extends HTMLElement {
|
|
|
828
1106
|
return;
|
|
829
1107
|
}
|
|
830
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 ?? [];
|
|
831
1111
|
this.#handles.forEach((el, i) => {
|
|
832
1112
|
const [x, y] = [places[2 * i] * scale, places[2 * i + 1] * scale];
|
|
833
1113
|
const there = Number.isFinite(x) && Number.isFinite(y);
|
|
@@ -835,6 +1115,14 @@ export class PencilFigure extends HTMLElement {
|
|
|
835
1115
|
if (!there) return;
|
|
836
1116
|
const moved = `translate(${x.toFixed(2)}px, ${y.toFixed(2)}px)`;
|
|
837
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
|
+
}
|
|
838
1126
|
const param = this.#info.handles[i].param;
|
|
839
1127
|
const value = this.#valueAt(param, this.#time);
|
|
840
1128
|
const [min, max] = this.range(param);
|
|
@@ -950,6 +1238,10 @@ export class PencilFigure extends HTMLElement {
|
|
|
950
1238
|
try {
|
|
951
1239
|
this.#layer.update(JSON.parse(this.#player.draw(this.#time, dpr)));
|
|
952
1240
|
} catch (error) {
|
|
1241
|
+
if (this.#recoverable(error)) {
|
|
1242
|
+
this.#recover(error);
|
|
1243
|
+
return;
|
|
1244
|
+
}
|
|
953
1245
|
this.pause();
|
|
954
1246
|
this.dispatchEvent(new CustomEvent('error', { detail: { error, time: this.#time, fresh } }));
|
|
955
1247
|
return;
|
|
@@ -958,7 +1250,12 @@ export class PencilFigure extends HTMLElement {
|
|
|
958
1250
|
this.#drawn = true;
|
|
959
1251
|
this.#wait();
|
|
960
1252
|
}
|
|
1253
|
+
this.#healthy = true;
|
|
961
1254
|
this.#placeHandles();
|
|
1255
|
+
if (this.#reported) {
|
|
1256
|
+
this.#reported = false;
|
|
1257
|
+
this.dispatchEvent(new Event('recover'));
|
|
1258
|
+
}
|
|
962
1259
|
this.dispatchEvent(new CustomEvent('draw', { detail: { time: this.#time, fresh } }));
|
|
963
1260
|
}
|
|
964
1261
|
|
|
@@ -980,8 +1277,15 @@ export class PencilFigure extends HTMLElement {
|
|
|
980
1277
|
return;
|
|
981
1278
|
}
|
|
982
1279
|
const [width, height] = [this.#canvas.width, this.#canvas.height];
|
|
983
|
-
|
|
984
|
-
|
|
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
|
+
}
|
|
985
1289
|
if (em || this.#canvas.width !== width || this.#canvas.height !== height) {
|
|
986
1290
|
this.#paint();
|
|
987
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;
|