@ac-kit/browser 0.2.0 → 0.3.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.
Files changed (64) hide show
  1. package/CHANGELOG.md +60 -0
  2. package/README.md +69 -2
  3. package/dist/animation-frame/frame-scheduler.d.ts +91 -0
  4. package/dist/animation-frame/frame-scheduler.d.ts.map +1 -0
  5. package/dist/animation-frame/frame-scheduler.js +181 -0
  6. package/dist/animation-frame/frame-scheduler.js.map +1 -0
  7. package/dist/canvas/canvas-backing-size.d.ts +58 -0
  8. package/dist/canvas/canvas-backing-size.d.ts.map +1 -0
  9. package/dist/canvas/canvas-backing-size.js +141 -0
  10. package/dist/canvas/canvas-backing-size.js.map +1 -0
  11. package/dist/canvas/image-data-pixels.d.ts +1 -1
  12. package/dist/canvas/image-data-pixels.d.ts.map +1 -1
  13. package/dist/canvas/image-data-pixels.js +2 -5
  14. package/dist/canvas/image-data-pixels.js.map +1 -1
  15. package/dist/canvas/read-image-data.d.ts.map +1 -1
  16. package/dist/canvas/read-image-data.js +2 -2
  17. package/dist/canvas/read-image-data.js.map +1 -1
  18. package/dist/dom/index.d.ts +2 -0
  19. package/dist/dom/index.d.ts.map +1 -1
  20. package/dist/dom/index.js +2 -0
  21. package/dist/dom/index.js.map +1 -1
  22. package/dist/dom/media-query/watch-media-query.d.ts +26 -0
  23. package/dist/dom/media-query/watch-media-query.d.ts.map +1 -0
  24. package/dist/dom/media-query/watch-media-query.js +42 -0
  25. package/dist/dom/media-query/watch-media-query.js.map +1 -0
  26. package/dist/dom/resize-observer/observe-canvas-size.d.ts +54 -0
  27. package/dist/dom/resize-observer/observe-canvas-size.d.ts.map +1 -0
  28. package/dist/dom/resize-observer/observe-canvas-size.js +189 -0
  29. package/dist/dom/resize-observer/observe-canvas-size.js.map +1 -0
  30. package/dist/dom/web-animations/wait-for-transitions.d.ts.map +1 -1
  31. package/dist/dom/web-animations/wait-for-transitions.js +2 -6
  32. package/dist/dom/web-animations/wait-for-transitions.js.map +1 -1
  33. package/dist/index.d.ts +2 -0
  34. package/dist/index.d.ts.map +1 -1
  35. package/dist/index.js +2 -0
  36. package/dist/index.js.map +1 -1
  37. package/dist/indexeddb/open-indexed-db.d.ts +1 -1
  38. package/dist/indexeddb/open-indexed-db.d.ts.map +1 -1
  39. package/dist/indexeddb/open-indexed-db.js +2 -1
  40. package/dist/indexeddb/open-indexed-db.js.map +1 -1
  41. package/dist/media-capture/microphone-access.d.ts.map +1 -1
  42. package/dist/media-capture/microphone-access.js +4 -6
  43. package/dist/media-capture/microphone-access.js.map +1 -1
  44. package/dist/testing/fake-animation-frames.d.ts +31 -0
  45. package/dist/testing/fake-animation-frames.d.ts.map +1 -0
  46. package/dist/testing/fake-animation-frames.js +75 -0
  47. package/dist/testing/fake-animation-frames.js.map +1 -0
  48. package/dist/testing/fake-match-media.d.ts +19 -0
  49. package/dist/testing/fake-match-media.d.ts.map +1 -0
  50. package/dist/testing/fake-match-media.js +135 -0
  51. package/dist/testing/fake-match-media.js.map +1 -0
  52. package/dist/testing/fake-resize-observer.d.ts +58 -0
  53. package/dist/testing/fake-resize-observer.d.ts.map +1 -0
  54. package/dist/testing/fake-resize-observer.js +118 -0
  55. package/dist/testing/fake-resize-observer.js.map +1 -0
  56. package/dist/testing/index.d.ts +3 -0
  57. package/dist/testing/index.d.ts.map +1 -1
  58. package/dist/testing/index.js +3 -0
  59. package/dist/testing/index.js.map +1 -1
  60. package/package.json +8 -6
  61. package/dist/_race-abort.d.ts +0 -2
  62. package/dist/_race-abort.d.ts.map +0 -1
  63. package/dist/_race-abort.js +0 -20
  64. package/dist/_race-abort.js.map +0 -1
package/CHANGELOG.md CHANGED
@@ -1,5 +1,65 @@
1
1
  # @ac-kit/browser
2
2
 
3
+ ## 0.3.0
4
+
5
+ ### Minor Changes
6
+
7
+ - 34e8cfa: `watchMediaQuery(window, query, { onChange?, signal? })` (from
8
+ `@ac-kit/browser/dom`) follows a media query such as `(prefers-reduced-motion:
9
+ reduce)`: its `matches` is the current value, `onChange` hears each change, and
10
+ disposing it, or aborting `signal`, stops the listening. It throws a
11
+ `TypeError` for a window without `matchMedia`. `./testing` gains
12
+ `FakeMatchMedia`, a `matchMedia` whose queries the test flips.
13
+ - 34e8cfa: Adds canvas sizing and frame scheduling. `canvasBackingSize(box, caps)` gives
14
+ the whole-pixel backing size of a canvas: the device-pixel size exactly when the
15
+ browser reports it and no cap applies, else the CSS size times the ratio,
16
+ rounded, and floored under `maxPixelRatio`, `maxPixels` or `maxSide` so the cap
17
+ holds; a worker takes the sizes the page sends. `observeCanvasSize` (from
18
+ `@ac-kit/browser/dom`) keeps a canvas at that size, follows ratio changes with
19
+ or without `devicePixelContentBoxSize`, assigns the canvas only when its size
20
+ differs, ignores a hidden canvas, and calls `onResize` after layout and before
21
+ paint. `FrameScheduler` renders on animation frames at most once each, on
22
+ `invalidate()` or every frame while `continuous`, with a capped frame delta,
23
+ `renderNow()` and `AbortSignal` or `Symbol.dispose` stop. `./testing` gains
24
+ `FakeAnimationFrames` and `FakeResizeObserver`.
25
+
26
+ ### Patch Changes
27
+
28
+ - 34e8cfa: An error message that quotes an argument of the wrong kind or value describes
29
+ it with `@ac-kit/core`'s `describeValue`, so building the message can no
30
+ longer throw on a symbol. The text changes: `-0` keeps its sign (`got -0`), a
31
+ string is JSON-quoted and an object is named by its kind. Affected:
32
+ `bigIntParse`'s radix, `decodeFloat64Base64`'s counts, `formatDuration`'s
33
+ value, `RateLimiter`'s `maxKeys`, cost and clock time, `TokenBucket`'s
34
+ capacity, refill rate and cost, `getBackoffDelay`'s attempt, delays and
35
+ factor, `retry`'s `maxAttempts`, `maxElapsedMs` and `delayMs`, `BitSet` and
36
+ `BitVector`'s size and members, the LRU caches', `TtlMap`'s and the bounded
37
+ collections' capacity, `TtlMap`'s time to live, `MultiSet`'s occurrence
38
+ count, `BloomFilter`, `CountMinSketch`, `CuckooFilter`, `HyperLogLog` and
39
+ `TopK`'s sizing options, `RingVector`, `SegmentedRingVector` and
40
+ `TypedVector`'s capacities and indexes, `openIndexedDb`'s version and
41
+ `readImageData`'s size.
42
+ - Updated dependencies [34e8cfa]
43
+ - Updated dependencies [34e8cfa]
44
+ - Updated dependencies [34e8cfa]
45
+ - Updated dependencies [34e8cfa]
46
+ - Updated dependencies [34e8cfa]
47
+ - Updated dependencies [34e8cfa]
48
+ - Updated dependencies [34e8cfa]
49
+ - Updated dependencies [34e8cfa]
50
+ - Updated dependencies [34e8cfa]
51
+ - Updated dependencies [34e8cfa]
52
+ - Updated dependencies [34e8cfa]
53
+ - Updated dependencies [34e8cfa]
54
+ - Updated dependencies [34e8cfa]
55
+ - Updated dependencies [34e8cfa]
56
+ - Updated dependencies [34e8cfa]
57
+ - Updated dependencies [34e8cfa]
58
+ - Updated dependencies [34e8cfa]
59
+ - Updated dependencies [34e8cfa]
60
+ - @ac-kit/async@0.8.0
61
+ - @ac-kit/core@0.12.0
62
+
3
63
  ## 0.2.0
4
64
 
5
65
  ### Minor Changes
package/README.md CHANGED
@@ -7,8 +7,9 @@ followed by its name across reloads and hot-plugs; the microphone opened for
7
7
  analysis, its permission and its devices; an audio clock mapped onto the
8
8
  page's; requests over
9
9
  `XMLHttpRequest` that report their upload progress, which `fetch` cannot; an
10
- image's pixels read through a canvas, scaled down; and the wait for an
11
- element's CSS transitions to end.
10
+ image's pixels read through a canvas, scaled down; a canvas's backing store
11
+ kept at its displayed size in device pixels; rendering on animation frames at
12
+ most once each; and the wait for an element's CSS transitions to end.
12
13
 
13
14
  Every function takes the browser object it works through — `indexedDB`,
14
15
  `navigator`, a `MIDIAccess`, `XMLHttpRequest`, a canvas factory, an element —
@@ -366,6 +367,68 @@ panel.classList.remove("is-hidden");
366
367
  `AbortSignal.timeout()`, or `@ac-kit/async`'s `withTimeout`, where that
367
368
  matters.
368
369
 
370
+ ## Canvas size
371
+
372
+ ```ts
373
+ import { FrameScheduler } from "@ac-kit/browser";
374
+ import { observeCanvasSize } from "@ac-kit/browser/dom";
375
+
376
+ const controller = new AbortController();
377
+ const scheduler = new FrameScheduler(window, () => draw(), {
378
+ signal: controller.signal,
379
+ });
380
+ observeCanvasSize(canvas, {
381
+ maxPixelRatio: 2,
382
+ // After layout, before paint: draw now rather than one stretched frame.
383
+ onResize: () => scheduler.renderNow(),
384
+ signal: controller.signal,
385
+ });
386
+ ```
387
+
388
+ - `canvasBackingSize(box, { maxPixelRatio?, maxPixels?, maxSide? })` — the
389
+ whole-pixel size `canvas.width` and `canvas.height` must hold for a content
390
+ box: the device-pixel size exactly when the browser gives it
391
+ (`devicePixelContentBoxSize`, not Safari) and no cap applies, else the CSS
392
+ size times the page's ratio, rounded; where a cap binds it is floored, so
393
+ rounding never passes a cap. Pure, so a worker drawing on an
394
+ `OffscreenCanvas` takes the sizes the page sends. It throws a `RangeError`
395
+ for a negative or non-finite size, or one too large for whole pixels.
396
+ - `observeCanvasSize(canvas, { onResize, resizeCanvas?, signal?, …caps })`
397
+ (from `/dom`) — keeps the backing store at the displayed size. It uses the
398
+ canvas's own window's `ResizeObserver`, on `device-pixel-content-box` where
399
+ supported (that also fires when the ratio changes, on another screen),
400
+ else on `content-box` plus a `resolution` media query. `onResize` is
401
+ called at the first layout and whenever the backing size changes, never
402
+ for a hidden (empty) canvas. The canvas is assigned only when its size
403
+ differs, since assigning resets it; `resizeCanvas: false` leaves it to a
404
+ worker. The canvas's CSS size must not depend on its `width` and `height`
405
+ attributes, or the backing size feeds back into the layout. Returns a
406
+ `Disposable`.
407
+
408
+ ## Frames
409
+
410
+ - `watchMediaQuery(window, query, { onChange?, signal? })` (from `/dom`) —
411
+ follows a media query such as `(prefers-reduced-motion: reduce)`: `matches`
412
+ is the current value, `onChange(matches)` hears each change, and disposing it
413
+ (or aborting `signal`) stops the listening. It throws a `TypeError` for a
414
+ window without `matchMedia`. `@ac-kit/browser/testing`'s `FakeMatchMedia`
415
+ stands in for `window.matchMedia` under Node: `set(query, matches)` flips a
416
+ query and fires `change`.
417
+
418
+ - `FrameScheduler(frames, render, { continuous?, maxDeltaSeconds?, clock?,
419
+ signal? })` — renders on animation frames, at most once each: after
420
+ `invalidate()` (calls before the frame coalesce), or every frame while
421
+ `continuous`. `FrameInfo` carries the frame's `time`, `deltaSeconds`
422
+ (0 on the first frame of a run, capped at `maxDeltaSeconds`, 0.1 by
423
+ default, so a tab back from the background does not jump) and `index`.
424
+ `renderNow()` renders at once and cancels a pending frame. Disposing, or
425
+ aborting `signal`, cancels the pending frame. `frames` is `window` or a
426
+ worker's global scope. A hidden page gets no frames, so a pending render
427
+ waits for it to be shown.
428
+
429
+ For a simulation stepped at a fixed rate under the frame rate, see
430
+ `FixedTimestep` in `@ac-kit/async`.
431
+
369
432
  ## Testing
370
433
 
371
434
  ```ts
@@ -414,6 +477,10 @@ helpers, and code built on them, take — for tests under Node, not for a page:
414
477
  and delivers what the test `emit`s.
415
478
  - `FakeMidiAccess` and `FakeMidiInput` — ports plugged in and out, messages
416
479
  sent with their time stamps.
480
+ - `FakeAnimationFrames` — `requestAnimationFrame` queueing, `runFrame(time)`
481
+ running what was queued before the call.
482
+ - `FakeResizeObserver` — records what is observed, delivers `resize(size)`
483
+ entries, and can refuse `device-pixel-content-box` as Safari does.
417
484
 
418
485
  They import no test runner: calls are recorded as data (`requests`,
419
486
  `posted`, `modules`, `connections`, `stopCount`, `closeCount`…), and a
@@ -0,0 +1,91 @@
1
+ import { type Callable, type Clock } from "@ac-kit/core";
2
+ /** `window` or a dedicated worker's global scope. */
3
+ export type AnimationFrames = Pick<AnimationFrameProvider, "requestAnimationFrame" | "cancelAnimationFrame">;
4
+ export type FrameInfo = {
5
+ /** The frame's time, ms on `performance.now()`'s time line. */
6
+ readonly time: number;
7
+ /**
8
+ * Seconds since the previous frame when it rendered too, else 0; at most
9
+ * `maxDeltaSeconds`. 0 for a non-finite `time`, which the next frame's delta
10
+ * does not count from.
11
+ */
12
+ readonly deltaSeconds: number;
13
+ /** Frames rendered before this one. */
14
+ readonly index: number;
15
+ };
16
+ export type FrameSchedulerOptions = {
17
+ /**
18
+ * Render every frame (playback, an animation) instead of on `invalidate()`.
19
+ * Default false.
20
+ */
21
+ readonly continuous?: boolean;
22
+ /**
23
+ * Cap on `deltaSeconds`, so a tab back from the background does not jump.
24
+ * Default 0.1.
25
+ */
26
+ readonly maxDeltaSeconds?: number;
27
+ /** `renderNow`'s time source. Default `SYSTEM_CLOCK`. */
28
+ readonly clock?: Clock;
29
+ /** Disposes the scheduler when it aborts. */
30
+ readonly signal?: AbortSignal;
31
+ };
32
+ /**
33
+ * Renders on animation frames, at most once each: on demand after
34
+ * `invalidate()`, or every frame while `continuous`. A throwing `render` is
35
+ * reported by the host like any frame callback; the next frame is already
36
+ * requested in continuous mode, and `invalidate()` keeps working.
37
+ *
38
+ * A frame with a delta of 0 starts a run: the first after idle, after
39
+ * `renderNow` or after a frame that rendered nothing. A hidden page gets no
40
+ * frames, so a pending render waits for it to be shown.
41
+ *
42
+ * @example
43
+ * const scheduler = new FrameScheduler(
44
+ * window,
45
+ * ({ deltaSeconds }) => {
46
+ * draw(deltaSeconds);
47
+ * },
48
+ * { signal },
49
+ * );
50
+ * scheduler.invalidate();
51
+ */
52
+ export declare class FrameScheduler implements Disposable {
53
+ private readonly frames_;
54
+ private readonly render_;
55
+ private readonly maxDeltaSeconds_;
56
+ private readonly clock_;
57
+ private readonly lifetime_;
58
+ private continuous_;
59
+ private handle_;
60
+ private invalidated_;
61
+ private rendering_;
62
+ private disposed_;
63
+ private index_;
64
+ /** The time of the last frame, `undefined` when the run is over. */
65
+ private lastTime_;
66
+ /** @throws {RangeError} When `maxDeltaSeconds` is not positive. */
67
+ constructor(frames: AnimationFrames, render: Callable<[frame: FrameInfo]>, options?: FrameSchedulerOptions);
68
+ /** Whether every frame renders. Changing it takes effect at the next frame. */
69
+ get continuous(): boolean;
70
+ set continuous(value: boolean);
71
+ /** Whether a frame is requested and not run yet. */
72
+ get isPending(): boolean;
73
+ get isDisposed(): boolean;
74
+ /** Requests one render at the next frame; calls before it coalesce. O(1). */
75
+ invalidate(): void;
76
+ /**
77
+ * Renders now, at `clock.now()`, and cancels a pending frame: for
78
+ * `observeCanvasSize`'s `onResize`. Called from within a render it is an
79
+ * `invalidate()` instead, as rendering inside a render would nest frames. In
80
+ * continuous mode the next frame is requested again.
81
+ */
82
+ renderNow(): void;
83
+ [Symbol.dispose](): void;
84
+ private release_;
85
+ private request_;
86
+ private cancel_;
87
+ private onFrame_;
88
+ private finishRun_;
89
+ private renderAt_;
90
+ }
91
+ //# sourceMappingURL=frame-scheduler.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"frame-scheduler.d.ts","sourceRoot":"","sources":["../../src/animation-frame/frame-scheduler.ts"],"names":[],"mappings":"AAAA,OAAO,EACN,KAAK,QAAQ,EACb,KAAK,KAAK,EAIV,MAAM,cAAc,CAAC;AAEtB,qDAAqD;AACrD,MAAM,MAAM,eAAe,GAAG,IAAI,CACjC,sBAAsB,EACtB,uBAAuB,GAAG,sBAAsB,CAChD,CAAC;AAEF,MAAM,MAAM,SAAS,GAAG;IACvB,+DAA+D;IAC/D,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IACtB;;;;OAIG;IACH,QAAQ,CAAC,YAAY,EAAE,MAAM,CAAC;IAC9B,uCAAuC;IACvC,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC;CACvB,CAAC;AAEF,MAAM,MAAM,qBAAqB,GAAG;IACnC;;;OAGG;IACH,QAAQ,CAAC,UAAU,CAAC,EAAE,OAAO,CAAC;IAC9B;;;OAGG;IACH,QAAQ,CAAC,eAAe,CAAC,EAAE,MAAM,CAAC;IAClC,yDAAyD;IACzD,QAAQ,CAAC,KAAK,CAAC,EAAE,KAAK,CAAC;IACvB,6CAA6C;IAC7C,QAAQ,CAAC,MAAM,CAAC,EAAE,WAAW,CAAC;CAC9B,CAAC;AAIF;;;;;;;;;;;;;;;;;;;GAmBG;AACH,qBAAa,cAAe,YAAW,UAAU;IAChD,OAAO,CAAC,QAAQ,CAAC,OAAO,CAAkB;IAC1C,OAAO,CAAC,QAAQ,CAAC,OAAO,CAA+B;IACvD,OAAO,CAAC,QAAQ,CAAC,gBAAgB,CAAS;IAC1C,OAAO,CAAC,QAAQ,CAAC,MAAM,CAAQ;IAC/B,OAAO,CAAC,QAAQ,CAAC,SAAS,CAAyB;IACnD,OAAO,CAAC,WAAW,CAAU;IAC7B,OAAO,CAAC,OAAO,CAAqB;IACpC,OAAO,CAAC,YAAY,CAAS;IAC7B,OAAO,CAAC,UAAU,CAAS;IAC3B,OAAO,CAAC,SAAS,CAAS;IAC1B,OAAO,CAAC,MAAM,CAAK;IACnB,oEAAoE;IACpE,OAAO,CAAC,SAAS,CAAqB;IAEtC,mEAAmE;IACnE,YACC,MAAM,EAAE,eAAe,EACvB,MAAM,EAAE,QAAQ,CAAC,CAAC,KAAK,EAAE,SAAS,CAAC,CAAC,EACpC,OAAO,GAAE,qBAA0B,EAiCnC;IAED,+EAA+E;IAC/E,IAAI,UAAU,IAAI,OAAO,CAExB;IAED,IAAI,UAAU,CAAC,KAAK,EAAE,OAAO,EAK5B;IAED,oDAAoD;IACpD,IAAI,SAAS,IAAI,OAAO,CAEvB;IAED,IAAI,UAAU,IAAI,OAAO,CAExB;IAED,6EAA6E;IAC7E,UAAU,IAAI,IAAI,CAGjB;IAED;;;;;OAKG;IACH,SAAS,IAAI,IAAI,CAmBhB;IAED,CAAC,MAAM,CAAC,OAAO,CAAC,IAAI,IAAI,CAEvB;IAED,OAAO,CAAC,QAAQ;IAQhB,OAAO,CAAC,QAAQ;IAShB,OAAO,CAAC,OAAO;IAOf,OAAO,CAAC,QAAQ;IAuBhB,OAAO,CAAC,UAAU;IAMlB,OAAO,CAAC,SAAS;CAuBjB"}
@@ -0,0 +1,181 @@
1
+ import { describeValue, MS_PER_SECOND, SYSTEM_CLOCK, } from "@ac-kit/core";
2
+ const DEFAULT_MAX_DELTA_SECONDS_ = 0.1;
3
+ /**
4
+ * Renders on animation frames, at most once each: on demand after
5
+ * `invalidate()`, or every frame while `continuous`. A throwing `render` is
6
+ * reported by the host like any frame callback; the next frame is already
7
+ * requested in continuous mode, and `invalidate()` keeps working.
8
+ *
9
+ * A frame with a delta of 0 starts a run: the first after idle, after
10
+ * `renderNow` or after a frame that rendered nothing. A hidden page gets no
11
+ * frames, so a pending render waits for it to be shown.
12
+ *
13
+ * @example
14
+ * const scheduler = new FrameScheduler(
15
+ * window,
16
+ * ({ deltaSeconds }) => {
17
+ * draw(deltaSeconds);
18
+ * },
19
+ * { signal },
20
+ * );
21
+ * scheduler.invalidate();
22
+ */
23
+ export class FrameScheduler {
24
+ frames_;
25
+ render_;
26
+ maxDeltaSeconds_;
27
+ clock_;
28
+ lifetime_ = new AbortController();
29
+ continuous_;
30
+ handle_;
31
+ invalidated_ = false;
32
+ rendering_ = false;
33
+ disposed_ = false;
34
+ index_ = 0;
35
+ /** The time of the last frame, `undefined` when the run is over. */
36
+ lastTime_;
37
+ /** @throws {RangeError} When `maxDeltaSeconds` is not positive. */
38
+ constructor(frames, render, options = {}) {
39
+ const { continuous = false, maxDeltaSeconds = DEFAULT_MAX_DELTA_SECONDS_, clock = SYSTEM_CLOCK, signal, } = options;
40
+ if (!(maxDeltaSeconds > 0)) {
41
+ throw new RangeError(`FrameScheduler: maxDeltaSeconds must be positive, got ${describeValue(maxDeltaSeconds)}`);
42
+ }
43
+ this.frames_ = frames;
44
+ this.render_ = render;
45
+ this.maxDeltaSeconds_ = maxDeltaSeconds;
46
+ this.clock_ = clock;
47
+ this.continuous_ = continuous;
48
+ if (signal?.aborted) {
49
+ this.disposed_ = true;
50
+ return;
51
+ }
52
+ // A derived signal aborts whatever a listener on `signal` does to the event.
53
+ const stop = signal === undefined
54
+ ? this.lifetime_.signal
55
+ : AbortSignal.any([signal, this.lifetime_.signal]);
56
+ stop.addEventListener("abort", () => this.release_(), { once: true });
57
+ if (continuous) {
58
+ this.request_();
59
+ }
60
+ }
61
+ /** Whether every frame renders. Changing it takes effect at the next frame. */
62
+ get continuous() {
63
+ return this.continuous_;
64
+ }
65
+ set continuous(value) {
66
+ this.continuous_ = value;
67
+ if (value) {
68
+ this.request_();
69
+ }
70
+ }
71
+ /** Whether a frame is requested and not run yet. */
72
+ get isPending() {
73
+ return this.handle_ !== undefined;
74
+ }
75
+ get isDisposed() {
76
+ return this.disposed_;
77
+ }
78
+ /** Requests one render at the next frame; calls before it coalesce. O(1). */
79
+ invalidate() {
80
+ this.invalidated_ = true;
81
+ this.request_();
82
+ }
83
+ /**
84
+ * Renders now, at `clock.now()`, and cancels a pending frame: for
85
+ * `observeCanvasSize`'s `onResize`. Called from within a render it is an
86
+ * `invalidate()` instead, as rendering inside a render would nest frames. In
87
+ * continuous mode the next frame is requested again.
88
+ */
89
+ renderNow() {
90
+ if (this.disposed_) {
91
+ return;
92
+ }
93
+ if (this.rendering_) {
94
+ this.invalidate();
95
+ return;
96
+ }
97
+ this.cancel_();
98
+ this.invalidated_ = false;
99
+ try {
100
+ this.renderAt_(this.clock_.now());
101
+ }
102
+ finally {
103
+ if (this.continuous_) {
104
+ this.request_();
105
+ }
106
+ this.finishRun_();
107
+ }
108
+ }
109
+ [Symbol.dispose]() {
110
+ this.lifetime_.abort();
111
+ }
112
+ release_() {
113
+ if (this.disposed_) {
114
+ return;
115
+ }
116
+ this.disposed_ = true;
117
+ this.cancel_();
118
+ }
119
+ request_() {
120
+ if (this.disposed_ || this.handle_ !== undefined) {
121
+ return;
122
+ }
123
+ this.handle_ = this.frames_.requestAnimationFrame((time) => this.onFrame_(time));
124
+ }
125
+ cancel_() {
126
+ if (this.handle_ !== undefined) {
127
+ this.frames_.cancelAnimationFrame(this.handle_);
128
+ this.handle_ = undefined;
129
+ }
130
+ }
131
+ onFrame_(time) {
132
+ this.handle_ = undefined;
133
+ if (this.disposed_) {
134
+ return;
135
+ }
136
+ const due = this.continuous_ || this.invalidated_;
137
+ this.invalidated_ = false;
138
+ if (!due) {
139
+ this.lastTime_ = undefined;
140
+ return;
141
+ }
142
+ if (this.continuous_) {
143
+ this.request_();
144
+ }
145
+ try {
146
+ this.renderAt_(time);
147
+ }
148
+ finally {
149
+ this.finishRun_();
150
+ }
151
+ }
152
+ // On demand, a run ends when nothing asks for the next frame.
153
+ finishRun_() {
154
+ if (this.handle_ === undefined) {
155
+ this.lastTime_ = undefined;
156
+ }
157
+ }
158
+ renderAt_(time) {
159
+ const elapsed = this.lastTime_ === undefined || !Number.isFinite(time)
160
+ ? 0
161
+ : (time - this.lastTime_) / MS_PER_SECOND;
162
+ // A clock that went back counts as no time passed.
163
+ const deltaSeconds = elapsed > 0 ? Math.min(elapsed, this.maxDeltaSeconds_) : 0;
164
+ // The baseline never moves back, so `renderNow`'s clock and the frames'
165
+ // time line, which may disagree, do not inflate the next delta; a
166
+ // non-finite time leaves it alone (and its frame's delta is 0).
167
+ if (Number.isFinite(time)) {
168
+ this.lastTime_ = Math.max(time, this.lastTime_ ?? time);
169
+ }
170
+ const frame = { time, deltaSeconds, index: this.index_ };
171
+ this.index_ += 1;
172
+ this.rendering_ = true;
173
+ try {
174
+ this.render_(frame);
175
+ }
176
+ finally {
177
+ this.rendering_ = false;
178
+ }
179
+ }
180
+ }
181
+ //# sourceMappingURL=frame-scheduler.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"frame-scheduler.js","sourceRoot":"","sources":["../../src/animation-frame/frame-scheduler.ts"],"names":[],"mappings":"AAAA,OAAO,EAGN,aAAa,EACb,aAAa,EACb,YAAY,GACZ,MAAM,cAAc,CAAC;AAsCtB,MAAM,0BAA0B,GAAG,GAAG,CAAC;AAEvC;;;;;;;;;;;;;;;;;;;GAmBG;AACH,MAAM,OAAO,cAAc;IACT,OAAO,CAAkB;IACzB,OAAO,CAA+B;IACtC,gBAAgB,CAAS;IACzB,MAAM,CAAQ;IACd,SAAS,GAAG,IAAI,eAAe,EAAE,CAAC;IAC3C,WAAW,CAAU;IACrB,OAAO,CAAqB;IAC5B,YAAY,GAAG,KAAK,CAAC;IACrB,UAAU,GAAG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AG,SAAS,CAAC;QAC5B,CAAC;IACF,CAAC;IAEO,SAAS,CAAC,IAAY;QAC7B,MAAM,OAAO,GACZ,IAAI,CAAC,SAAS,KAAK,SAAS,IAAI,CAAC,MAAM,CAAC,QAAQ,CAAC,IAAI,CAAC;YACrD,CAAC,CAAC,CAAC;YACH,CAAC,CAAC,CAAC,IAAI,GAAG,IAAI,CAAC,SAAS,CAAC,GAAG,aAAa,CAAC;QAC5C,mDAAmD;QACnD,MAAM,YAAY,GACjB,OAAO,GAAG,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,GAAG,CAAC,OAAO,EAAE,IAAI,CAAC,gBAAgB,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;QAC5D,wEAAwE;QACxE,kEAAkE;QAClE,gEAAgE;QAChE,IAAI,MAAM,CAAC,QAAQ,CAAC,IAAI,CAAC,EAAE,CAAC;YAC3B,IAAI,CAAC,SAAS,GAAG,IAAI,CAAC,GAAG,CAAC,IAAI,EAAE,IAAI,CAAC,SAAS,IAAI,IAAI,CAAC,CAAC;QACzD,CAAC;QACD,MAAM,KAAK,GAAc,EAAE,IAAI,EAAE,YAAY,EAAE,KAAK,EAAE,IAAI,CAAC,MAAM,EAAE,CAAC;QACpE,IAAI,CAAC,MAAM,IAAI,CAAC,CAAC;QACjB,IAAI,CAAC,UAAU,GAAG,IAAI,CAAC;QACvB,IAAI,CAAC;YACJ,IAAI,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC;QACrB,CAAC;gBAAS,CAAC;YACV,IAAI,CAAC,UAAU,GAAG,KAAK,CAAC;QACzB,CAAC;IACF,CAAC;CACD"}
@@ -0,0 +1,58 @@
1
+ /** A canvas's content box, as the page measures it. */
2
+ export type CanvasBoxSize = {
3
+ readonly cssWidth: number;
4
+ readonly cssHeight: number;
5
+ /** From `devicePixelContentBoxSize`, when the browser gives it (not Safari). */
6
+ readonly devicePixelWidth?: number;
7
+ readonly devicePixelHeight?: number;
8
+ /** The page's `devicePixelRatio`. */
9
+ readonly devicePixelRatio: number;
10
+ };
11
+ export type CanvasBackingSizeOptions = {
12
+ /** Most backing pixels per CSS pixel (heliotrope: 2). Default: no cap. */
13
+ readonly maxPixelRatio?: number;
14
+ /**
15
+ * Most backing pixels in all (e.g. 3840 × 2160); the ratio is lowered to fit.
16
+ * Default: no cap.
17
+ */
18
+ readonly maxPixels?: number;
19
+ /** Longest side (e.g. a device's `maxTextureDimension2D`). Default: no cap. */
20
+ readonly maxSide?: number;
21
+ };
22
+ export type CanvasBackingSize = {
23
+ /** Whole pixels, what `canvas.width` and `canvas.height` must hold. */
24
+ readonly width: number;
25
+ readonly height: number;
26
+ readonly cssWidth: number;
27
+ readonly cssHeight: number;
28
+ /**
29
+ * Backing pixels per CSS pixel actually used: `width / cssWidth`. For a
30
+ * canvas with no CSS width it is taken from the height, and for an empty one
31
+ * it is the ratio the caps leave.
32
+ */
33
+ readonly pixelRatio: number;
34
+ };
35
+ /**
36
+ * The backing-store size for a canvas: the device-pixel size exactly when the
37
+ * browser gives it and no cap applies (pixel-perfect, no moiré), else the CSS
38
+ * size times the capped ratio, rounded but floored wherever a cap binds, so
39
+ * rounding never passes `maxPixelRatio`, `maxPixels` or `maxSide`. A side with
40
+ * a CSS size is at least 1 pixel (which wins over a `maxPixelRatio` below one
41
+ * pixel per side); `maxPixels` and `maxSide` always hold, the larger side
42
+ * giving way. An empty canvas stays empty, whatever the device size says.
43
+ * O(1).
44
+ *
45
+ * @example
46
+ * const size = canvasBackingSize(
47
+ * { cssWidth: 300.4, cssHeight: 150, devicePixelRatio: 3 },
48
+ * { maxPixelRatio: 2 },
49
+ * );
50
+ * // { width: 600, height: 300, cssWidth: 300.4, cssHeight: 150, pixelRatio: … }
51
+ *
52
+ * @throws {RangeError} For a negative or non-finite size (a device dimension
53
+ * included) or ratio, a ratio of 0, a cap that is not positive, `maxPixels`
54
+ * or `maxSide` below 1, or a size, device dimensions included, too large for
55
+ * whole pixels without a cap.
56
+ */
57
+ export declare function canvasBackingSize(box: CanvasBoxSize, options?: CanvasBackingSizeOptions): CanvasBackingSize;
58
+ //# sourceMappingURL=canvas-backing-size.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"canvas-backing-size.d.ts","sourceRoot":"","sources":["../../src/canvas/canvas-backing-size.ts"],"names":[],"mappings":"AAEA,uDAAuD;AACvD,MAAM,MAAM,aAAa,GAAG;IAC3B,QAAQ,CAAC,QAAQ,EAAE,MAAM,CAAC;IAC1B,QAAQ,CAAC,SAAS,EAAE,MAAM,CAAC;IAC3B,gFAAgF;IAChF,QAAQ,CAAC,gBAAgB,CAAC,EAAE,MAAM,CAAC;IACnC,QAAQ,CAAC,iBAAiB,CAAC,EAAE,MAAM,CAAC;IACpC,qCAAqC;IACrC,QAAQ,CAAC,gBAAgB,EAAE,MAAM,CAAC;CAClC,CAAC;AAEF,MAAM,MAAM,wBAAwB,GAAG;IACtC,0EAA0E;IAC1E,QAAQ,CAAC,aAAa,CAAC,EAAE,MAAM,CAAC;IAChC;;;OAGG;IACH,QAAQ,CAAC,SAAS,CAAC,EAAE,MAAM,CAAC;IAC5B,+EAA+E;IAC/E,QAAQ,CAAC,OAAO,CAAC,EAAE,MAAM,CAAC;CAC1B,CAAC;AAEF,MAAM,MAAM,iBAAiB,GAAG;IAC/B,uEAAuE;IACvE,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC;IACvB,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;IACxB,QAAQ,CAAC,QAAQ,EAAE,MAAM,CAAC;IAC1B,QAAQ,CAAC,SAAS,EAAE,MAAM,CAAC;IAC3B;;;;OAIG;IACH,QAAQ,CAAC,UAAU,EAAE,MAAM,CAAC;CAC5B,CAAC;AA+EF;;;;;;;;;;;;;;;;;;;;;GAqBG;AACH,wBAAgB,iBAAiB,CAChC,GAAG,EAAE,aAAa,EAClB,OAAO,GAAE,wBAA6B,GACpC,iBAAiB,CAgFnB"}
@@ -0,0 +1,141 @@
1
+ import { assertNonNegative, assertPositive } from "@ac-kit/core";
2
+ // A cap of `Infinity` or `undefined` is no cap.
3
+ function capOf_(name, value, minimum) {
4
+ if (value === undefined || value === Infinity) {
5
+ return Infinity;
6
+ }
7
+ assertPositive(value, `canvasBackingSize: ${name}`);
8
+ if (value < minimum) {
9
+ throw new RangeError(`canvasBackingSize: ${name} must be at least ${minimum}, got ${value}`);
10
+ }
11
+ return value;
12
+ }
13
+ function fitsCaps_(width, height, caps) {
14
+ return width * height <= caps.pixels && Math.max(width, height) <= caps.side;
15
+ }
16
+ // The side at `ratio`, rounded, then floored to every per-side cap (the ratio
17
+ // cap and `maxSide`) so rounding never passes one; a visible side keeps 1.
18
+ function sideOf_(css, ratio, caps, round) {
19
+ if (!(css > 0)) {
20
+ return 0;
21
+ }
22
+ const bound = Math.floor(Math.min(css * caps.ratio, caps.side));
23
+ return Math.max(1, Math.min(round(css * ratio), bound));
24
+ }
25
+ function assertWholePixels_(width, height, what) {
26
+ if (!Number.isSafeInteger(width) || !Number.isSafeInteger(height)) {
27
+ throw new RangeError(`canvasBackingSize: ${what} are too large for whole pixels; give maxSide or maxPixels`);
28
+ }
29
+ }
30
+ // Lowers the larger side, then the other if one pixel was not enough: every
31
+ // non-empty side stays at least 1 and the product ends within `pixels`.
32
+ function fitPixels_(width, height, pixels) {
33
+ let fittedWidth = width;
34
+ let fittedHeight = height;
35
+ for (let pass = 0; pass < 2 && fittedWidth * fittedHeight > pixels; pass += 1) {
36
+ if (fittedWidth >= fittedHeight) {
37
+ fittedWidth = Math.max(1, Math.floor(pixels / fittedHeight));
38
+ }
39
+ else {
40
+ fittedHeight = Math.max(1, Math.floor(pixels / fittedWidth));
41
+ }
42
+ }
43
+ return [fittedWidth, fittedHeight];
44
+ }
45
+ /**
46
+ * The backing-store size for a canvas: the device-pixel size exactly when the
47
+ * browser gives it and no cap applies (pixel-perfect, no moiré), else the CSS
48
+ * size times the capped ratio, rounded but floored wherever a cap binds, so
49
+ * rounding never passes `maxPixelRatio`, `maxPixels` or `maxSide`. A side with
50
+ * a CSS size is at least 1 pixel (which wins over a `maxPixelRatio` below one
51
+ * pixel per side); `maxPixels` and `maxSide` always hold, the larger side
52
+ * giving way. An empty canvas stays empty, whatever the device size says.
53
+ * O(1).
54
+ *
55
+ * @example
56
+ * const size = canvasBackingSize(
57
+ * { cssWidth: 300.4, cssHeight: 150, devicePixelRatio: 3 },
58
+ * { maxPixelRatio: 2 },
59
+ * );
60
+ * // { width: 600, height: 300, cssWidth: 300.4, cssHeight: 150, pixelRatio: … }
61
+ *
62
+ * @throws {RangeError} For a negative or non-finite size (a device dimension
63
+ * included) or ratio, a ratio of 0, a cap that is not positive, `maxPixels`
64
+ * or `maxSide` below 1, or a size, device dimensions included, too large for
65
+ * whole pixels without a cap.
66
+ */
67
+ export function canvasBackingSize(box, options = {}) {
68
+ const { cssWidth, cssHeight, devicePixelRatio } = box;
69
+ assertNonNegative(cssWidth, "canvasBackingSize: cssWidth");
70
+ assertNonNegative(cssHeight, "canvasBackingSize: cssHeight");
71
+ assertPositive(devicePixelRatio, "canvasBackingSize: devicePixelRatio");
72
+ const caps = {
73
+ ratio: capOf_("maxPixelRatio", options.maxPixelRatio, 0),
74
+ pixels: capOf_("maxPixels", options.maxPixels, 1),
75
+ side: capOf_("maxSide", options.maxSide, 1),
76
+ };
77
+ const { devicePixelWidth, devicePixelHeight } = box;
78
+ if (devicePixelWidth !== undefined) {
79
+ assertNonNegative(devicePixelWidth, "canvasBackingSize: devicePixelWidth");
80
+ }
81
+ if (devicePixelHeight !== undefined) {
82
+ assertNonNegative(devicePixelHeight, "canvasBackingSize: devicePixelHeight");
83
+ }
84
+ const isEmpty = cssWidth === 0 || cssHeight === 0;
85
+ if (!isEmpty &&
86
+ devicePixelWidth !== undefined &&
87
+ devicePixelHeight !== undefined) {
88
+ const width = Math.round(devicePixelWidth);
89
+ const height = Math.round(devicePixelHeight);
90
+ if (width >= 1 &&
91
+ height >= 1 &&
92
+ fitsCaps_(width, height, caps) &&
93
+ width <= cssWidth * caps.ratio &&
94
+ height <= cssHeight * caps.ratio) {
95
+ assertWholePixels_(width, height, `${width} × ${height} device pixels`);
96
+ return {
97
+ width,
98
+ height,
99
+ cssWidth,
100
+ cssHeight,
101
+ pixelRatio: pixelRatioOf_(width, height, box, devicePixelRatio),
102
+ };
103
+ }
104
+ }
105
+ let ratio = Math.min(devicePixelRatio, caps.ratio);
106
+ const root = Math.sqrt(cssWidth) * Math.sqrt(cssHeight);
107
+ if (ratio * root > Math.sqrt(caps.pixels)) {
108
+ ratio = Math.sqrt(caps.pixels) / root;
109
+ }
110
+ const longest = Math.max(cssWidth, cssHeight);
111
+ if (ratio * longest > caps.side) {
112
+ ratio = caps.side / longest;
113
+ }
114
+ // Under a lowered ratio the cap must hold: floor, never round up.
115
+ const round = ratio < devicePixelRatio ? Math.floor : Math.round;
116
+ let width = sideOf_(cssWidth, ratio, caps, round);
117
+ let height = sideOf_(cssHeight, ratio, caps, round);
118
+ if (!fitsCaps_(width, height, caps)) {
119
+ width = sideOf_(cssWidth, ratio, caps, Math.floor);
120
+ height = sideOf_(cssHeight, ratio, caps, Math.floor);
121
+ }
122
+ [width, height] = fitPixels_(width, height, caps.pixels);
123
+ assertWholePixels_(width, height, `${cssWidth} × ${cssHeight} CSS pixels at ratio ${ratio}`);
124
+ return {
125
+ width,
126
+ height,
127
+ cssWidth,
128
+ cssHeight,
129
+ pixelRatio: pixelRatioOf_(width, height, box, ratio),
130
+ };
131
+ }
132
+ function pixelRatioOf_(width, height, box, fallback) {
133
+ if (box.cssWidth > 0) {
134
+ return width / box.cssWidth;
135
+ }
136
+ if (box.cssHeight > 0) {
137
+ return height / box.cssHeight;
138
+ }
139
+ return fallback;
140
+ }
141
+ //# sourceMappingURL=canvas-backing-size.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"canvas-backing-size.js","sourceRoot":"","sources":["../../src/canvas/canvas-backing-size.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,iBAAiB,EAAE,cAAc,EAAE,MA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ACD,CAAC,KAAK,EAAE,MAAM,CAAC,GAAG,UAAU,CAAC,KAAK,EAAE,MAAM,EAAE,IAAI,CAAC,MAAM,CAAC,CAAC;IACzD,kBAAkB,CACjB,KAAK,EACL,MAAM,EACN,GAAG,QAAQ,MAAM,SAAS,wBAAwB,KAAK,EAAE,CACzD,CAAC;IAEF,OAAO;QACN,KAAK;QACL,MAAM;QACN,QAAQ;QACR,SAAS;QACT,UAAU,EAAE,aAAa,CAAC,KAAK,EAAE,MAAM,EAAE,GAAG,EAAE,KAAK,CAAC;KACpD,CAAC;AACH,CAAC;AAED,SAAS,aAAa,CACrB,KAAa,EACb,MAAc,EACd,GAAkB,EAClB,QAAgB;IAEhB,IAAI,GAAG,CAAC,QAAQ,GAAG,CAAC,EAAE,CAAC;QACtB,OAAO,KAAK,GAAG,GAAG,CAAC,QAAQ,CAAC;IAC7B,CAAC;IACD,IAAI,GAAG,CAAC,SAAS,GAAG,CAAC,EAAE,CAAC;QACvB,OAAO,MAAM,GAAG,GAAG,CAAC,SAAS,CAAC;IAC/B,CAAC;IAED,OAAO,QAAQ,CAAC;AACjB,CAAC"}
@@ -1,4 +1,4 @@
1
- import type { Callable } from "@ac-kit/core";
1
+ import { type Callable } from "@ac-kit/core";
2
2
  /**
3
3
  * One pixel of 8-bit `ImageData`, each component in [0, 255]: the shape of
4
4
  * `@ac-kit/math-color`'s `Rgba8`, so it goes to its conversions as it is.