@hadoku/ui-shell 0.1.100 → 0.1.102
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/dist/motion/animate.d.ts +43 -0
- package/dist/motion/animate.js +65 -0
- package/dist/motion/frame.d.ts +9 -0
- package/dist/motion/frame.js +41 -0
- package/dist/motion/index.d.ts +5 -0
- package/dist/motion/index.js +5 -0
- package/dist/motion/particles.d.ts +20 -0
- package/dist/motion/particles.js +32 -0
- package/dist/motion/stage.d.ts +59 -0
- package/dist/motion/stage.js +206 -0
- package/dist/motion/stand-in.d.ts +34 -0
- package/dist/motion/stand-in.js +85 -0
- package/package.json +2 -2
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import type { everyFrame } from './frame.js';
|
|
2
|
+
import type { ParticlePool } from './particles.js';
|
|
3
|
+
import type { Stage } from './stage.js';
|
|
4
|
+
/** A motion under way: settles (never rejects) once it is over or cancelled, and is meant to last `endsInMs` from when it was made. */
|
|
5
|
+
export interface Running {
|
|
6
|
+
readonly finished: Promise<void>;
|
|
7
|
+
cancel(): void;
|
|
8
|
+
readonly endsInMs: number;
|
|
9
|
+
}
|
|
10
|
+
/** A motion not yet started: what a recipe is, given where and how to play. */
|
|
11
|
+
export type Clip = (ctx: MotionContext) => Running;
|
|
12
|
+
export interface MotionContext {
|
|
13
|
+
stage: Stage;
|
|
14
|
+
overlay: HTMLElement;
|
|
15
|
+
/** Every length is divided by it ({@link quicken}, {@link markTempo}). */
|
|
16
|
+
tempo: number;
|
|
17
|
+
reduced: boolean;
|
|
18
|
+
frame: typeof everyFrame;
|
|
19
|
+
particles: ParticlePool;
|
|
20
|
+
}
|
|
21
|
+
export type AnimateContext = Partial<Pick<MotionContext, 'tempo' | 'reduced' | 'stage'>>;
|
|
22
|
+
/** A motion that is already over: reduced motion's, or one with nothing to move. */
|
|
23
|
+
export declare const STILL: Running;
|
|
24
|
+
export declare const animationsOf: (r: Running) => readonly Animation[];
|
|
25
|
+
/** `anims` as one {@link Running}: over once every one is, cancelled together. */
|
|
26
|
+
export declare function runningOf(anims: readonly Animation[]): Running;
|
|
27
|
+
/** `el.animate(frames, options)` at `ctx.tempo`: already over without Web Animations (jsdom) or when `ctx.reduced`; with `ctx.stage`, one of its beats. */
|
|
28
|
+
export declare function animate(el: Element, frames: Keyframe[] | PropertyIndexedKeyframes, options: KeyframeAnimationOptions, ctx?: AnimateContext): Running;
|
|
29
|
+
/** The custom property every beat's sheet divides its lengths by. */
|
|
30
|
+
export declare const TEMPO_PROPERTY = "--tempo";
|
|
31
|
+
/** `el` animates at `tempo`: its sheet's lengths, and its children's, divide by it. */
|
|
32
|
+
export declare const markTempo: (el: Element, tempo: number) => void;
|
|
33
|
+
/** `anim` plays at `tempo`, its delay and duration both. Returns it. */
|
|
34
|
+
export declare function quicken<T extends Animation>(anim: T, tempo: number): T;
|
|
35
|
+
/**
|
|
36
|
+
* The pace of a move of `weight` (0 to 1), always the same length: at 0 nearly even (half speed to one and a
|
|
37
|
+
* half), at 1 a creep that ends at about nine times its average speed, so a heavy one lands harder.
|
|
38
|
+
*/
|
|
39
|
+
export declare function weightEase(weight: number): string;
|
|
40
|
+
/** The way home at `weight`: the same length, setting off slower the heavier, always settling gently into place. */
|
|
41
|
+
export declare function homeEase(weight: number): string;
|
|
42
|
+
/** Writes each `[name, ms]` on `el` as `<ms>ms` for a sheet's `var()`: a length is written once, by what times it. */
|
|
43
|
+
export declare function writeDurations(el: HTMLElement, durations: readonly (readonly [string, number])[]): void;
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* `animate()`, the one `.animate()` call a game needs, and `Running`, the one shape every motion hands back.
|
|
3
|
+
* Effects take a `MotionContext` instead of reading globals.
|
|
4
|
+
*/
|
|
5
|
+
/** A motion that is already over: reduced motion's, or one with nothing to move. */
|
|
6
|
+
export const STILL = { finished: Promise.resolve(), cancel: () => undefined, endsInMs: 0 };
|
|
7
|
+
/** The Web Animations a {@link runningOf} plays, for a stage to count as beats. */
|
|
8
|
+
const ANIMATIONS = new WeakMap();
|
|
9
|
+
export const animationsOf = (r) => ANIMATIONS.get(r) ?? [];
|
|
10
|
+
/** `anims` as one {@link Running}: over once every one is, cancelled together. */
|
|
11
|
+
export function runningOf(anims) {
|
|
12
|
+
if (anims.length === 0)
|
|
13
|
+
return STILL;
|
|
14
|
+
const ends = anims.map((a) => (Number(a.effect?.getComputedTiming().endTime ?? 0) - Number(a.currentTime ?? 0)) / (Math.abs(a.playbackRate) || 1));
|
|
15
|
+
const r = {
|
|
16
|
+
finished: Promise.allSettled(anims.map((a) => a.finished)).then(() => undefined),
|
|
17
|
+
cancel: () => {
|
|
18
|
+
for (const a of anims)
|
|
19
|
+
a.cancel();
|
|
20
|
+
},
|
|
21
|
+
endsInMs: Math.max(0, ...ends),
|
|
22
|
+
};
|
|
23
|
+
ANIMATIONS.set(r, anims);
|
|
24
|
+
return r;
|
|
25
|
+
}
|
|
26
|
+
/** `el.animate(frames, options)` at `ctx.tempo`: already over without Web Animations (jsdom) or when `ctx.reduced`; with `ctx.stage`, one of its beats. */
|
|
27
|
+
export function animate(el, frames, options, ctx = {}) {
|
|
28
|
+
if (ctx.reduced === true || typeof el.animate !== 'function')
|
|
29
|
+
return STILL;
|
|
30
|
+
const r = runningOf([quicken(el.animate(frames, options), ctx.tempo ?? 1)]);
|
|
31
|
+
ctx.stage?.beat(r);
|
|
32
|
+
return r;
|
|
33
|
+
}
|
|
34
|
+
/** The custom property every beat's sheet divides its lengths by. */
|
|
35
|
+
export const TEMPO_PROPERTY = '--tempo';
|
|
36
|
+
/** `el` animates at `tempo`: its sheet's lengths, and its children's, divide by it. */
|
|
37
|
+
export const markTempo = (el, tempo) => el.style.setProperty(TEMPO_PROPERTY, String(tempo));
|
|
38
|
+
/** `anim` plays at `tempo`, its delay and duration both. Returns it. */
|
|
39
|
+
export function quicken(anim, tempo) {
|
|
40
|
+
if (tempo !== 1)
|
|
41
|
+
anim.playbackRate = tempo;
|
|
42
|
+
return anim;
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* The pace of a move of `weight` (0 to 1), always the same length: at 0 nearly even (half speed to one and a
|
|
46
|
+
* half), at 1 a creep that ends at about nine times its average speed, so a heavy one lands harder.
|
|
47
|
+
*/
|
|
48
|
+
export function weightEase(weight) {
|
|
49
|
+
const p = Math.min(1, Math.max(0, weight));
|
|
50
|
+
const x1 = 0.4 + 0.52 * p;
|
|
51
|
+
const y1 = 0.2 * (1 - p);
|
|
52
|
+
const x2 = 0.75 + 0.2 * p;
|
|
53
|
+
const y2 = 0.6 - 0.05 * p;
|
|
54
|
+
return `cubic-bezier(${x1.toFixed(3)}, ${y1.toFixed(3)}, ${x2.toFixed(3)}, ${y2.toFixed(3)})`;
|
|
55
|
+
}
|
|
56
|
+
/** The way home at `weight`: the same length, setting off slower the heavier, always settling gently into place. */
|
|
57
|
+
export function homeEase(weight) {
|
|
58
|
+
const p = Math.min(1, Math.max(0, weight));
|
|
59
|
+
return `cubic-bezier(${(0.3 + 0.45 * p).toFixed(3)}, 0, 0.25, 1)`;
|
|
60
|
+
}
|
|
61
|
+
/** Writes each `[name, ms]` on `el` as `<ms>ms` for a sheet's `var()`: a length is written once, by what times it. */
|
|
62
|
+
export function writeDurations(el, durations) {
|
|
63
|
+
for (const [name, ms] of durations)
|
|
64
|
+
el.style.setProperty(name, `${ms}ms`);
|
|
65
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Every per-frame loop on one `requestAnimationFrame`: every loop's read, then every loop's write, so N loops
|
|
3
|
+
* cost one layout a frame, not N (a board wipe's eleven trails and stand-ins forced 30–60 ms a frame,
|
|
4
|
+
* 2026-11-01). `step` is the read and returns the write, or null once over. A loop added mid-frame runs next
|
|
5
|
+
* frame; one that throws ends alone, rethrown once the frame is through.
|
|
6
|
+
*/
|
|
7
|
+
export type FrameStep = () => (() => void) | null;
|
|
8
|
+
/** Run `step` every frame, its read with every other loop's reads and its write after them. Returns the stop. */
|
|
9
|
+
export declare function everyFrame(step: FrameStep): () => void;
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
const loops = new Set();
|
|
2
|
+
let frame = null;
|
|
3
|
+
/** Run `step` every frame, its read with every other loop's reads and its write after them. Returns the stop. */
|
|
4
|
+
export function everyFrame(step) {
|
|
5
|
+
loops.add(step);
|
|
6
|
+
frame ??= requestAnimationFrame(run);
|
|
7
|
+
return () => {
|
|
8
|
+
loops.delete(step);
|
|
9
|
+
};
|
|
10
|
+
}
|
|
11
|
+
function run() {
|
|
12
|
+
frame = null;
|
|
13
|
+
let failed = null;
|
|
14
|
+
const writes = [];
|
|
15
|
+
for (const step of [...loops]) {
|
|
16
|
+
try {
|
|
17
|
+
const write = step();
|
|
18
|
+
if (write === null)
|
|
19
|
+
loops.delete(step);
|
|
20
|
+
else
|
|
21
|
+
writes.push([step, write]);
|
|
22
|
+
}
|
|
23
|
+
catch (error) {
|
|
24
|
+
loops.delete(step);
|
|
25
|
+
failed ??= { error };
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
for (const [step, write] of writes) {
|
|
29
|
+
try {
|
|
30
|
+
write();
|
|
31
|
+
}
|
|
32
|
+
catch (error) {
|
|
33
|
+
loops.delete(step);
|
|
34
|
+
failed ??= { error };
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
if (loops.size > 0)
|
|
38
|
+
frame ??= requestAnimationFrame(run);
|
|
39
|
+
if (failed !== null)
|
|
40
|
+
throw failed.error;
|
|
41
|
+
}
|
package/dist/motion/index.d.ts
CHANGED
|
@@ -31,3 +31,8 @@ export declare function clearMotion(root: Element): void;
|
|
|
31
31
|
export declare function rememberedMotion(key: string): MotionChoices | null;
|
|
32
32
|
export declare function rememberMotion(key: string, choices: MotionChoices): void;
|
|
33
33
|
export { ambientPlayState, gateSelector, motionCss } from './css.js';
|
|
34
|
+
export { animate, animationsOf, homeEase, markTempo, quicken, runningOf, STILL, TEMPO_PROPERTY, weightEase, writeDurations, type AnimateContext, type Clip, type MotionContext, type Running } from './animate.js';
|
|
35
|
+
export { everyFrame, type FrameStep } from './frame.js';
|
|
36
|
+
export { claimParticles, PARTICLE_BUDGET, particlePool, particleShare, shared, type ParticlePool } from './particles.js';
|
|
37
|
+
export { animationName, Stage, type StageHold, type StageOptions, type TrackOptions } from './stage.js';
|
|
38
|
+
export { afterGlide, rel, standIn, type Box, type StandInOptions } from './stand-in.js';
|
package/dist/motion/index.js
CHANGED
|
@@ -86,3 +86,8 @@ export function rememberMotion(key, choices) {
|
|
|
86
86
|
}
|
|
87
87
|
}
|
|
88
88
|
export { ambientPlayState, gateSelector, motionCss } from "./css.js";
|
|
89
|
+
export { animate, animationsOf, homeEase, markTempo, quicken, runningOf, STILL, TEMPO_PROPERTY, weightEase, writeDurations } from "./animate.js";
|
|
90
|
+
export { everyFrame } from "./frame.js";
|
|
91
|
+
export { claimParticles, PARTICLE_BUDGET, particlePool, particleShare, shared } from "./particles.js";
|
|
92
|
+
export { animationName, Stage } from "./stage.js";
|
|
93
|
+
export { afterGlide, rel, standIn } from "./stand-in.js";
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* How many particles one beat's effects throw in all, shared out across whatever lands at once: four deaths
|
|
3
|
+
* together look about as busy as two (2026-10-23: four creatures dying ran 232 animations at 40 fps).
|
|
4
|
+
*/
|
|
5
|
+
/** The particles one beat may throw: about one shatter's or one big crash's worth, and a little more. */
|
|
6
|
+
export declare const PARTICLE_BUDGET = 60;
|
|
7
|
+
/** The share of its full count each effect of a beat throws, when together they would throw `wanted`. */
|
|
8
|
+
export declare function particleShare(wanted: number, budget?: number): number;
|
|
9
|
+
/** `full` particles at `share`, rounded down so a beat stays inside its budget, never fewer than `floor` (or `full`, if that is fewer): an effect never vanishes. */
|
|
10
|
+
export declare function shared(full: number, share: number, floor: number): number;
|
|
11
|
+
/**
|
|
12
|
+
* The particles one effect on `root` may throw between `fromMs` and `untilMs`: what is left of the budget once
|
|
13
|
+
* every other effect on screen then took its own, whatever its kind and whenever it asked (2026-10-25: a
|
|
14
|
+
* thirteen-creature wipe, each kind inside its own budget, ran at 34 fps). First come, first served; never fewer
|
|
15
|
+
* than `floor` (or `want`, if that is fewer).
|
|
16
|
+
*/
|
|
17
|
+
export declare function claimParticles(root: Element, want: number, floor: number, fromMs: number, untilMs: number, budget?: number): number;
|
|
18
|
+
/** One board's pool, as a context hands it to an effect. */
|
|
19
|
+
export type ParticlePool = (want: number, floor: number, fromMs: number, untilMs: number) => number;
|
|
20
|
+
export declare const particlePool: (root: Element, budget?: number) => ParticlePool;
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* How many particles one beat's effects throw in all, shared out across whatever lands at once: four deaths
|
|
3
|
+
* together look about as busy as two (2026-10-23: four creatures dying ran 232 animations at 40 fps).
|
|
4
|
+
*/
|
|
5
|
+
/** The particles one beat may throw: about one shatter's or one big crash's worth, and a little more. */
|
|
6
|
+
export const PARTICLE_BUDGET = 60;
|
|
7
|
+
/** The share of its full count each effect of a beat throws, when together they would throw `wanted`. */
|
|
8
|
+
export function particleShare(wanted, budget = PARTICLE_BUDGET) {
|
|
9
|
+
return wanted <= budget ? 1 : budget / wanted;
|
|
10
|
+
}
|
|
11
|
+
/** `full` particles at `share`, rounded down so a beat stays inside its budget, never fewer than `floor` (or `full`, if that is fewer): an effect never vanishes. */
|
|
12
|
+
export function shared(full, share, floor) {
|
|
13
|
+
return Math.max(Math.min(full, floor), Math.floor(full * share));
|
|
14
|
+
}
|
|
15
|
+
/** What each board's overlay has drawn or scheduled: how many particles, on screen from when until when (epoch ms). */
|
|
16
|
+
const pools = new WeakMap();
|
|
17
|
+
/**
|
|
18
|
+
* The particles one effect on `root` may throw between `fromMs` and `untilMs`: what is left of the budget once
|
|
19
|
+
* every other effect on screen then took its own, whatever its kind and whenever it asked (2026-10-25: a
|
|
20
|
+
* thirteen-creature wipe, each kind inside its own budget, ran at 34 fps). First come, first served; never fewer
|
|
21
|
+
* than `floor` (or `want`, if that is fewer).
|
|
22
|
+
*/
|
|
23
|
+
export function claimParticles(root, want, floor, fromMs, untilMs, budget = PARTICLE_BUDGET) {
|
|
24
|
+
const now = Date.now();
|
|
25
|
+
const pool = (pools.get(root) ?? []).filter((e) => e.until > now);
|
|
26
|
+
const busy = pool.reduce((n, e) => (e.from < untilMs && e.until > fromMs ? n + e.count : n), 0);
|
|
27
|
+
const granted = Math.max(Math.min(want, floor), Math.min(want, budget - busy));
|
|
28
|
+
pool.push({ from: fromMs, until: untilMs, count: granted });
|
|
29
|
+
pools.set(root, pool);
|
|
30
|
+
return granted;
|
|
31
|
+
}
|
|
32
|
+
export const particlePool = (root, budget = PARTICLE_BUDGET) => (want, floor, from, until) => claimParticles(root, want, floor, from, until, budget);
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import { type Running } from './animate.js';
|
|
2
|
+
/** `unlockAt`: how far through its drawings the stage unlocks (1: once over). `isBeat`: a beat unregistered. */
|
|
3
|
+
export interface StageOptions {
|
|
4
|
+
unlockAt?: number;
|
|
5
|
+
isBeat?: (a: Animation) => boolean;
|
|
6
|
+
}
|
|
7
|
+
/** `root`: where its beats run, each still running there at its time waited for to `unlockAt`. `after`: not unlocked before it settles. */
|
|
8
|
+
export interface TrackOptions {
|
|
9
|
+
root?: Element;
|
|
10
|
+
after?: Promise<unknown>;
|
|
11
|
+
}
|
|
12
|
+
/** What a held frame was waiting on: the drawings, the beats by name, when the last drawing began. */
|
|
13
|
+
export type StageHold = {
|
|
14
|
+
drawing: number;
|
|
15
|
+
waitingOn: string;
|
|
16
|
+
lastBeatAt: number | null;
|
|
17
|
+
};
|
|
18
|
+
/** An animation's name: its id, a stylesheet animation's keyframes, or the property a transition moves. */
|
|
19
|
+
export declare function animationName(a: Animation): string;
|
|
20
|
+
export declare class Stage {
|
|
21
|
+
readonly unlockAt: number;
|
|
22
|
+
private readonly recognised;
|
|
23
|
+
private readonly drawing;
|
|
24
|
+
private readonly locked;
|
|
25
|
+
private readonly live;
|
|
26
|
+
private readonly registered;
|
|
27
|
+
private readonly roots;
|
|
28
|
+
private readonly cues;
|
|
29
|
+
private lastBeatAt;
|
|
30
|
+
private waiting;
|
|
31
|
+
constructor(options?: StageOptions);
|
|
32
|
+
unlockAfter(ms: number): number;
|
|
33
|
+
/** `r`'s animations are this stage's beats. Returns `r`. */
|
|
34
|
+
beat(r: Running): Running;
|
|
35
|
+
/** The finite stylesheet animations on `el` and inside it now, as beats: registered, not recognised by name. */
|
|
36
|
+
trackCss(el: Element): Running;
|
|
37
|
+
/** The stage draws `r`: busy until it is over, locked until it is `unlockAt` through its `endsInMs` (an endless one: over). */
|
|
38
|
+
track(r: Running, o?: TrackOptions): Running;
|
|
39
|
+
/** Cancels every beat and drawing still running. */
|
|
40
|
+
cancelAll(): void;
|
|
41
|
+
get drawingCount(): number;
|
|
42
|
+
held(): StageHold;
|
|
43
|
+
/** Runs `done` once nothing tracked plays, at once when still. Returns a cancel. */
|
|
44
|
+
whenStill(done: () => void): () => void;
|
|
45
|
+
/** Runs `done` once everything tracked is `unlockAt` through, at once when it is. Returns a cancel. */
|
|
46
|
+
whenUnlocked(done: () => void): () => void;
|
|
47
|
+
/** Drawing `seq`'s moment `name` is `value` (an epoch ms, or what the cue carries). */
|
|
48
|
+
cue<T = number>(name: string, seq: number, value: T): void;
|
|
49
|
+
/** Drawing `seq`'s moment `name`, or null when it had none. */
|
|
50
|
+
cueAt<T = number>(name: string, seq: number): T | null;
|
|
51
|
+
isBeat(a: Animation): boolean;
|
|
52
|
+
/** The beats still waited on, by name (`beat×2 board-number-rise×1`). */
|
|
53
|
+
waitingOn(): string;
|
|
54
|
+
/** Settles once every finite beat on `root` and inside it is `through` of the way through, looking again as each comes due (one may start another). */
|
|
55
|
+
settled(root: Element, through?: number): Promise<void>;
|
|
56
|
+
/** A look in the next frame, before it is drawn (its style pass is then done once), unless one is coming. */
|
|
57
|
+
private lookSoon;
|
|
58
|
+
private look;
|
|
59
|
+
}
|
|
@@ -0,0 +1,206 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* A board's stage: what it is drawing, for what waits on that. It is STILL once every tracked drawing is over
|
|
3
|
+
* (`whenStill`: a game's end) and UNLOCKED once each is `unlockAt` through (`whenUnlocked`: a pacer's wait),
|
|
4
|
+
* the motion playing on to its end. A real-time game asks for neither.
|
|
5
|
+
*
|
|
6
|
+
* Its waits look at its beats: animations registered (`beat`, `trackCss`, `animate()` with a stage) or
|
|
7
|
+
* recognised by its `isBeat`; an endless pulse, a banner or a glide is no beat. Cues are moments a drawing
|
|
8
|
+
* tells the page about (its first hit, a sound's moment): one per name, keyed by the drawing's number.
|
|
9
|
+
*/
|
|
10
|
+
import { animationsOf, runningOf } from "./animate.js";
|
|
11
|
+
/** An animation's name: its id, a stylesheet animation's keyframes, or the property a transition moves. */
|
|
12
|
+
export function animationName(a) {
|
|
13
|
+
const named = a;
|
|
14
|
+
if (typeof named.animationName === 'string' && named.animationName !== '')
|
|
15
|
+
return named.animationName;
|
|
16
|
+
if (typeof named.transitionProperty === 'string' && named.transitionProperty !== '')
|
|
17
|
+
return `transition:${named.transitionProperty}`;
|
|
18
|
+
return a.id === '' ? 'unnamed' : a.id;
|
|
19
|
+
}
|
|
20
|
+
const untilThrough = (b, through) => Math.max(0, (through * b.end - b.at) / b.rate);
|
|
21
|
+
/** How long a look waits for a frame before running without one: a hidden tab draws none. */
|
|
22
|
+
const FRAME_FALLBACK_MS = 100;
|
|
23
|
+
/** A set of holds and who waits for it to empty. */
|
|
24
|
+
class Holds {
|
|
25
|
+
held = new Set();
|
|
26
|
+
waiters = new Set();
|
|
27
|
+
size = () => this.held.size;
|
|
28
|
+
add(until) {
|
|
29
|
+
this.held.add(until);
|
|
30
|
+
void until.then(() => {
|
|
31
|
+
this.held.delete(until);
|
|
32
|
+
if (this.held.size > 0)
|
|
33
|
+
return;
|
|
34
|
+
for (const wake of [...this.waiters]) {
|
|
35
|
+
this.waiters.delete(wake);
|
|
36
|
+
wake();
|
|
37
|
+
}
|
|
38
|
+
});
|
|
39
|
+
}
|
|
40
|
+
whenEmpty(done) {
|
|
41
|
+
if (this.held.size === 0) {
|
|
42
|
+
done();
|
|
43
|
+
return () => undefined;
|
|
44
|
+
}
|
|
45
|
+
this.waiters.add(done);
|
|
46
|
+
return () => void this.waiters.delete(done);
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
export class Stage {
|
|
50
|
+
unlockAt;
|
|
51
|
+
recognised;
|
|
52
|
+
drawing = new Holds();
|
|
53
|
+
locked = new Holds();
|
|
54
|
+
live = new Set();
|
|
55
|
+
registered = new WeakSet();
|
|
56
|
+
roots = new Map();
|
|
57
|
+
cues = new Map();
|
|
58
|
+
lastBeatAt = null;
|
|
59
|
+
waiting = [];
|
|
60
|
+
constructor(options = {}) {
|
|
61
|
+
this.unlockAt = options.unlockAt ?? 1;
|
|
62
|
+
this.recognised = options.isBeat ?? (() => false);
|
|
63
|
+
}
|
|
64
|
+
unlockAfter(ms) {
|
|
65
|
+
return Math.round(ms * this.unlockAt);
|
|
66
|
+
}
|
|
67
|
+
/** `r`'s animations are this stage's beats. Returns `r`. */
|
|
68
|
+
beat(r) {
|
|
69
|
+
for (const a of animationsOf(r))
|
|
70
|
+
this.registered.add(a);
|
|
71
|
+
if (!this.live.has(r)) {
|
|
72
|
+
this.live.add(r);
|
|
73
|
+
void r.finished.then(() => this.live.delete(r));
|
|
74
|
+
}
|
|
75
|
+
return r;
|
|
76
|
+
}
|
|
77
|
+
/** The finite stylesheet animations on `el` and inside it now, as beats: registered, not recognised by name. */
|
|
78
|
+
trackCss(el) {
|
|
79
|
+
const anims = typeof el.getAnimations === 'function' ? el.getAnimations({ subtree: true }) : [];
|
|
80
|
+
return this.beat(runningOf(anims.filter((a) => Number(a.effect?.getComputedTiming().endTime ?? 0) !== Infinity)));
|
|
81
|
+
}
|
|
82
|
+
/** The stage draws `r`: busy until it is over, locked until it is `unlockAt` through its `endsInMs` (an endless one: over). */
|
|
83
|
+
track(r, o = {}) {
|
|
84
|
+
this.beat(r);
|
|
85
|
+
this.lastBeatAt = Date.now();
|
|
86
|
+
let done = false;
|
|
87
|
+
const settled = r.finished.then(() => void (done = true), () => void (done = true));
|
|
88
|
+
this.drawing.add(settled);
|
|
89
|
+
const unlocked = new Promise((at) => {
|
|
90
|
+
if (!Number.isFinite(r.endsInMs))
|
|
91
|
+
return;
|
|
92
|
+
const look = () => (done || o.root === undefined ? at() : void this.settled(o.root, this.unlockAt).then(at));
|
|
93
|
+
setTimeout(() => void Promise.resolve(o.after).then(look, look), this.unlockAfter(r.endsInMs));
|
|
94
|
+
});
|
|
95
|
+
this.locked.add(Promise.race([settled, unlocked]));
|
|
96
|
+
return r;
|
|
97
|
+
}
|
|
98
|
+
/** Cancels every beat and drawing still running. */
|
|
99
|
+
cancelAll() {
|
|
100
|
+
for (const r of [...this.live])
|
|
101
|
+
r.cancel();
|
|
102
|
+
}
|
|
103
|
+
get drawingCount() {
|
|
104
|
+
return this.drawing.size();
|
|
105
|
+
}
|
|
106
|
+
held() {
|
|
107
|
+
return { drawing: this.drawing.size(), waitingOn: this.waitingOn(), lastBeatAt: this.lastBeatAt };
|
|
108
|
+
}
|
|
109
|
+
/** Runs `done` once nothing tracked plays, at once when still. Returns a cancel. */
|
|
110
|
+
whenStill(done) {
|
|
111
|
+
return this.drawing.whenEmpty(done);
|
|
112
|
+
}
|
|
113
|
+
/** Runs `done` once everything tracked is `unlockAt` through, at once when it is. Returns a cancel. */
|
|
114
|
+
whenUnlocked(done) {
|
|
115
|
+
return this.locked.whenEmpty(done);
|
|
116
|
+
}
|
|
117
|
+
/** Drawing `seq`'s moment `name` is `value` (an epoch ms, or what the cue carries). */
|
|
118
|
+
cue(name, seq, value) {
|
|
119
|
+
this.cues.set(name, { seq, value });
|
|
120
|
+
}
|
|
121
|
+
/** Drawing `seq`'s moment `name`, or null when it had none. */
|
|
122
|
+
cueAt(name, seq) {
|
|
123
|
+
const c = this.cues.get(name);
|
|
124
|
+
return c !== undefined && c.seq === seq ? c.value : null;
|
|
125
|
+
}
|
|
126
|
+
isBeat(a) {
|
|
127
|
+
return this.registered.has(a) || this.recognised(a);
|
|
128
|
+
}
|
|
129
|
+
/** The beats still waited on, by name (`beat×2 board-number-rise×1`). */
|
|
130
|
+
waitingOn() {
|
|
131
|
+
const counts = new Map();
|
|
132
|
+
for (const a of this.waiting)
|
|
133
|
+
if (a.playState !== 'finished' && a.playState !== 'idle')
|
|
134
|
+
counts.set(animationName(a), (counts.get(animationName(a)) ?? 0) + 1);
|
|
135
|
+
return [...counts].map(([name, n]) => `${name}×${n}`).join(' ');
|
|
136
|
+
}
|
|
137
|
+
/** Settles once every finite beat on `root` and inside it is `through` of the way through, looking again as each comes due (one may start another). */
|
|
138
|
+
settled(root, through = 1) {
|
|
139
|
+
return new Promise((resolve) => {
|
|
140
|
+
if (typeof root.getAnimations !== 'function')
|
|
141
|
+
return resolve();
|
|
142
|
+
let beats = this.roots.get(root);
|
|
143
|
+
if (beats === undefined)
|
|
144
|
+
this.roots.set(root, (beats = { waits: new Set(), queued: false, timer: null, watched: new WeakSet() }));
|
|
145
|
+
beats.waits.add({ through, settle: resolve });
|
|
146
|
+
this.lookSoon(root, beats);
|
|
147
|
+
});
|
|
148
|
+
}
|
|
149
|
+
/** A look in the next frame, before it is drawn (its style pass is then done once), unless one is coming. */
|
|
150
|
+
lookSoon(root, beats) {
|
|
151
|
+
if (beats.queued)
|
|
152
|
+
return;
|
|
153
|
+
beats.queued = true;
|
|
154
|
+
let frame = null;
|
|
155
|
+
const run = () => {
|
|
156
|
+
clearTimeout(fallback);
|
|
157
|
+
if (frame !== null && typeof cancelAnimationFrame === 'function')
|
|
158
|
+
cancelAnimationFrame(frame);
|
|
159
|
+
if (beats.queued)
|
|
160
|
+
this.look(root, beats);
|
|
161
|
+
};
|
|
162
|
+
const fallback = setTimeout(run, FRAME_FALLBACK_MS);
|
|
163
|
+
if (typeof requestAnimationFrame === 'function')
|
|
164
|
+
frame = requestAnimationFrame(run);
|
|
165
|
+
}
|
|
166
|
+
look(root, beats) {
|
|
167
|
+
beats.queued = false;
|
|
168
|
+
if (beats.timer !== null)
|
|
169
|
+
clearTimeout(beats.timer);
|
|
170
|
+
beats.timer = null;
|
|
171
|
+
const now = [];
|
|
172
|
+
for (const anim of root.getAnimations({ subtree: true })) {
|
|
173
|
+
// Whether it is a beat first: reading a timing is not free.
|
|
174
|
+
if (!this.isBeat(anim) || anim.playState === 'finished' || anim.playState === 'idle')
|
|
175
|
+
continue;
|
|
176
|
+
const end = Number(anim.effect?.getComputedTiming().endTime ?? 0);
|
|
177
|
+
if (end !== Infinity)
|
|
178
|
+
now.push({ anim, end, at: Number(anim.currentTime ?? 0), rate: Math.abs(anim.playbackRate) || 1 });
|
|
179
|
+
}
|
|
180
|
+
const holding = new Set();
|
|
181
|
+
let soonest = Infinity;
|
|
182
|
+
for (const wait of [...beats.waits]) {
|
|
183
|
+
const left = now.filter((b) => untilThrough(b, wait.through) > 0);
|
|
184
|
+
if (left.length === 0) {
|
|
185
|
+
beats.waits.delete(wait);
|
|
186
|
+
wait.settle();
|
|
187
|
+
}
|
|
188
|
+
for (const b of left) {
|
|
189
|
+
holding.add(b);
|
|
190
|
+
soonest = Math.min(soonest, untilThrough(b, wait.through));
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
this.waiting = [...holding].map((b) => b.anim);
|
|
194
|
+
if (beats.waits.size === 0)
|
|
195
|
+
return void this.roots.delete(root);
|
|
196
|
+
// Again as soon as one is far enough through, or one ends early (cancelled, its element gone).
|
|
197
|
+
beats.timer = setTimeout(() => this.lookSoon(root, beats), soonest);
|
|
198
|
+
for (const b of holding) {
|
|
199
|
+
if (beats.watched.has(b.anim))
|
|
200
|
+
continue;
|
|
201
|
+
beats.watched.add(b.anim);
|
|
202
|
+
const again = () => this.lookSoon(root, beats);
|
|
203
|
+
void b.anim.finished.then(again, again);
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
export interface Box {
|
|
2
|
+
left: number;
|
|
3
|
+
top: number;
|
|
4
|
+
width: number;
|
|
5
|
+
height: number;
|
|
6
|
+
}
|
|
7
|
+
/** `r` as a box relative to `to`. */
|
|
8
|
+
export declare const rel: (r: DOMRect, to: DOMRect) => Box;
|
|
9
|
+
export interface StandInOptions {
|
|
10
|
+
cls: string;
|
|
11
|
+
/** The piece inside it that carries the copy, and `data-stand-in`'s value. */
|
|
12
|
+
pieceCls: string;
|
|
13
|
+
key: string;
|
|
14
|
+
copy: (el: HTMLElement) => HTMLElement;
|
|
15
|
+
/** `el` as drawn now, once it is no longer connected; null when gone. */
|
|
16
|
+
find: () => HTMLElement | null;
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* A copy of `el` in `overlay`, kept on `el`'s box every frame until `stop`. Mounted, the copy's face is lined up
|
|
20
|
+
* on `el`'s exactly, so handing over either way never jumps. An `el` drawn again meanwhile is followed, and hidden.
|
|
21
|
+
*/
|
|
22
|
+
export declare function standIn(overlay: HTMLElement, el: HTMLElement, o: StandInOptions): {
|
|
23
|
+
wrap: HTMLElement;
|
|
24
|
+
box: () => Box;
|
|
25
|
+
card: () => HTMLElement;
|
|
26
|
+
mount: () => void;
|
|
27
|
+
stop: () => void;
|
|
28
|
+
};
|
|
29
|
+
/**
|
|
30
|
+
* Runs `done` once `el`'s running `left`/`top` transitions have finished (a clock started at the style change
|
|
31
|
+
* runs ahead of a transition that starts a frame later), a retargeted one waited for in turn; nothing running
|
|
32
|
+
* is done next microtask, and a page that cannot list animations (jsdom) waits `fallbackMs`. Returns a cancel.
|
|
33
|
+
*/
|
|
34
|
+
export declare function afterGlide(el: Element, done: () => void, fallbackMs: number): () => void;
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
/* Stand-ins: a copy of an element in an overlay, moving where the element must not (a transform in a laid-out row counts toward its overflow). */
|
|
2
|
+
import { everyFrame } from "./frame.js";
|
|
3
|
+
/** `r` as a box relative to `to`. */
|
|
4
|
+
export const rel = (r, to) => ({ left: r.left - to.left, top: r.top - to.top, width: r.width, height: r.height });
|
|
5
|
+
/**
|
|
6
|
+
* A copy of `el` in `overlay`, kept on `el`'s box every frame until `stop`. Mounted, the copy's face is lined up
|
|
7
|
+
* on `el`'s exactly, so handing over either way never jumps. An `el` drawn again meanwhile is followed, and hidden.
|
|
8
|
+
*/
|
|
9
|
+
export function standIn(overlay, el, o) {
|
|
10
|
+
const wrap = document.createElement('div');
|
|
11
|
+
wrap.className = o.cls;
|
|
12
|
+
wrap.dataset.standIn = o.key;
|
|
13
|
+
wrap.setAttribute('aria-hidden', 'true');
|
|
14
|
+
const piece = document.createElement('div');
|
|
15
|
+
piece.className = o.pieceCls;
|
|
16
|
+
const copy = o.copy(el);
|
|
17
|
+
piece.append(copy);
|
|
18
|
+
wrap.append(piece);
|
|
19
|
+
let at = rel(el.getBoundingClientRect(), overlay.getBoundingClientRect());
|
|
20
|
+
let fix = { x: 0, y: 0 };
|
|
21
|
+
let live = true;
|
|
22
|
+
/** The read: where `el` is drawn now. */
|
|
23
|
+
const measure = () => {
|
|
24
|
+
if (!el.isConnected) {
|
|
25
|
+
const now = o.find();
|
|
26
|
+
if (now !== null) {
|
|
27
|
+
el = now;
|
|
28
|
+
el.style.visibility = 'hidden';
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
if (el.isConnected)
|
|
32
|
+
at = rel(el.getBoundingClientRect(), overlay.getBoundingClientRect());
|
|
33
|
+
};
|
|
34
|
+
/** The write: the copy over it. */
|
|
35
|
+
const draw = () => {
|
|
36
|
+
Object.assign(wrap.style, { left: `${at.left + fix.x}px`, top: `${at.top + fix.y}px`, width: `${at.width}px` });
|
|
37
|
+
wrap.style.setProperty('--card-w', `${at.width}px`);
|
|
38
|
+
};
|
|
39
|
+
const mount = () => {
|
|
40
|
+
measure();
|
|
41
|
+
draw();
|
|
42
|
+
overlay.append(wrap);
|
|
43
|
+
// With no motion on it yet: where the copy is against `el`, in one layout that `el`'s box comes out of too.
|
|
44
|
+
const want = el.getBoundingClientRect();
|
|
45
|
+
const got = copy.getBoundingClientRect();
|
|
46
|
+
fix = { x: want.left - got.left, y: want.top - got.top };
|
|
47
|
+
at = rel(want, overlay.getBoundingClientRect());
|
|
48
|
+
draw();
|
|
49
|
+
everyFrame(() => {
|
|
50
|
+
if (!live)
|
|
51
|
+
return null;
|
|
52
|
+
measure();
|
|
53
|
+
return draw;
|
|
54
|
+
});
|
|
55
|
+
};
|
|
56
|
+
return { wrap, box: () => at, card: () => el, mount, stop: () => (live = false) };
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* Runs `done` once `el`'s running `left`/`top` transitions have finished (a clock started at the style change
|
|
60
|
+
* runs ahead of a transition that starts a frame later), a retargeted one waited for in turn; nothing running
|
|
61
|
+
* is done next microtask, and a page that cannot list animations (jsdom) waits `fallbackMs`. Returns a cancel.
|
|
62
|
+
*/
|
|
63
|
+
export function afterGlide(el, done, fallbackMs) {
|
|
64
|
+
let live = true;
|
|
65
|
+
if (typeof el.getAnimations !== 'function') {
|
|
66
|
+
const timer = setTimeout(done, fallbackMs);
|
|
67
|
+
return () => clearTimeout(timer);
|
|
68
|
+
}
|
|
69
|
+
const wait = () => {
|
|
70
|
+
if (!live)
|
|
71
|
+
return;
|
|
72
|
+
const running = el.getAnimations().filter((a) => 'transitionProperty' in a && (a.transitionProperty === 'left' || a.transitionProperty === 'top'));
|
|
73
|
+
if (running.length > 0) {
|
|
74
|
+
// Finished or cancelled (retargeted, or the element gone), it is looked at again: a cancel is not an arrival.
|
|
75
|
+
void Promise.allSettled(running.map((a) => a.finished)).then(wait);
|
|
76
|
+
return;
|
|
77
|
+
}
|
|
78
|
+
live = false;
|
|
79
|
+
done();
|
|
80
|
+
};
|
|
81
|
+
queueMicrotask(wait);
|
|
82
|
+
return () => {
|
|
83
|
+
live = false;
|
|
84
|
+
};
|
|
85
|
+
}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@hadoku/ui-shell",
|
|
3
|
-
"version": "0.1.
|
|
4
|
-
"gitHead": "
|
|
3
|
+
"version": "0.1.102",
|
|
4
|
+
"gitHead": "162621d4d3a7a5d4bef7d74046968836705d27e2",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "MIT",
|
|
7
7
|
"description": "The parts of a game's UI any game keeps: chrome, trim, room shell, embed mount, theme and settings frame, UI scale, usage counting, the sound mixer, motion and generic components. One appId parameterizes its storage keys, events and chat prefix. MIT; imports only react, react-dom and @wolffm/*.",
|