@mun-ui/animation 0.0.0-stage → 0.2.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.
- package/ARCHITECTURE.md +470 -0
- package/CHANGELOG.md +88 -0
- package/LICENSE +21 -0
- package/PERFORMANCE.md +151 -0
- package/README.md +635 -2
- package/dist/index.d.ts +474 -0
- package/dist/src/canvas/index.d.ts +15 -0
- package/dist/src/canvas/index.js +93 -0
- package/dist/src/constraints/index.d.ts +33 -0
- package/dist/src/constraints/index.js +346 -0
- package/dist/src/core/bezier.js +51 -0
- package/dist/src/core/composition.js +17 -0
- package/dist/src/core/controls.js +22 -0
- package/dist/src/core/default-engine.js +20 -0
- package/dist/src/core/easing.js +58 -0
- package/dist/src/core/engine.js +1031 -0
- package/dist/src/core/frame-budget.js +30 -0
- package/dist/src/core/index.d.ts +43 -0
- package/dist/src/core/index.js +17 -0
- package/dist/src/core/js-spring-batch.js +57 -0
- package/dist/src/core/kinetics.js +140 -0
- package/dist/src/core/math.js +20 -0
- package/dist/src/core/motion-value.js +53 -0
- package/dist/src/core/planner.js +72 -0
- package/dist/src/core/specs.js +70 -0
- package/dist/src/dom/index.d.ts +41 -0
- package/dist/src/dom/index.js +364 -0
- package/dist/src/gesture/index.d.ts +66 -0
- package/dist/src/gesture/index.js +376 -0
- package/dist/src/index.js +53 -0
- package/dist/src/interpolate/color.js +223 -0
- package/dist/src/interpolate/css.d.ts +13 -0
- package/dist/src/interpolate/css.js +34 -0
- package/dist/src/interpolate/index.d.ts +13 -0
- package/dist/src/interpolate/index.js +55 -0
- package/dist/src/interpolate/transform.js +247 -0
- package/dist/src/layout/index.d.ts +56 -0
- package/dist/src/layout/index.js +485 -0
- package/dist/src/material/index.d.ts +9 -0
- package/dist/src/material/index.js +70 -0
- package/dist/src/path/index.d.ts +37 -0
- package/dist/src/path/index.js +527 -0
- package/dist/src/render/frame-batcher.js +52 -0
- package/dist/src/rendering-quality/index.d.ts +28 -0
- package/dist/src/rendering-quality/index.js +284 -0
- package/dist/src/rendering-quality/probe.js +132 -0
- package/dist/src/scroll/index.d.ts +55 -0
- package/dist/src/scroll/index.js +233 -0
- package/dist/src/timeline/index.d.ts +147 -0
- package/dist/src/timeline/index.js +849 -0
- package/dist/src/transition/index.d.ts +88 -0
- package/dist/src/transition/index.js +369 -0
- package/dist/src/wasm/index.d.ts +29 -0
- package/dist/src/wasm/index.js +8 -0
- package/dist/src/wasm/loader.js +55 -0
- package/dist/src/wasm/shared-wasm-spring-batch.js +52 -0
- package/dist/src/wasm/wasm-spring-batch.js +52 -0
- package/dist/src/webgl/index.d.ts +22 -0
- package/dist/src/webgl/index.js +94 -0
- package/dist/src/webgpu/index.d.ts +35 -0
- package/dist/src/webgpu/index.js +73 -0
- package/dist/src/webgpu/spring-batch.js +218 -0
- package/dist/src/worker/index.d.ts +17 -0
- package/dist/src/worker/index.js +1 -0
- package/dist/src/worker/shared-spring-worker.js +218 -0
- package/dist/src/worker/shared-worker.js +75 -0
- package/dist/wasm/kernel-scalar.wasm +0 -0
- package/dist/wasm/kernel-shared-scalar.wasm +0 -0
- package/dist/wasm/kernel-shared-simd.wasm +0 -0
- package/dist/wasm/kernel-simd.wasm +0 -0
- package/package.json +114 -3
|
@@ -0,0 +1,284 @@
|
|
|
1
|
+
/* SPDX-License-Identifier: MIT */
|
|
2
|
+
|
|
3
|
+
import { cacheRenderingQuality, probeRenderingQuality, screenKey } from './probe.js';
|
|
4
|
+
export { qualityFromFrames } from './probe.js';
|
|
5
|
+
|
|
6
|
+
const controllers = new WeakMap();
|
|
7
|
+
const levels = ['full', 'reduced', 'none'];
|
|
8
|
+
const intervalMs = 30_000;
|
|
9
|
+
const defaultOwner = () => typeof window === 'undefined' ? null : window;
|
|
10
|
+
|
|
11
|
+
/** One owner per Window; subscriptions own all timers and observers. */
|
|
12
|
+
class RenderingQualityController {
|
|
13
|
+
constructor(owner) {
|
|
14
|
+
this.owner = owner;
|
|
15
|
+
this.listeners = new Set();
|
|
16
|
+
this.queues = new WeakMap();
|
|
17
|
+
this.pendingQueues = new Set();
|
|
18
|
+
this.snapshot = Object.freeze({ quality: 'full', source: 'default', baselineMs: null, frameDelayMs: null, gpuQueueDelayMs: null, longFrameMs: null, measuredAt: null });
|
|
19
|
+
this.timer = 0;
|
|
20
|
+
this.running = null;
|
|
21
|
+
this.ready = null;
|
|
22
|
+
this.resolveReady = null;
|
|
23
|
+
this.started = false;
|
|
24
|
+
this.debug = false;
|
|
25
|
+
this.initialized = false;
|
|
26
|
+
this.healthy = 0;
|
|
27
|
+
this.pressureWindows = new Map();
|
|
28
|
+
this.pressureTimes = new Map();
|
|
29
|
+
this.lastPressure = -Infinity;
|
|
30
|
+
this.lastChange = -Infinity;
|
|
31
|
+
this.lastCheck = owner.performance.now();
|
|
32
|
+
this.frameWindow = 0;
|
|
33
|
+
this.frameSamples = 0;
|
|
34
|
+
this.delayedFrames = 0;
|
|
35
|
+
this.epoch = 0;
|
|
36
|
+
this.key = screenKey(owner);
|
|
37
|
+
this.onVisibility = () => {
|
|
38
|
+
this.pause();
|
|
39
|
+
this.resolveReady?.(this.quality);
|
|
40
|
+
this.resolveReady = null;
|
|
41
|
+
this.initialized = true;
|
|
42
|
+
if (!this.listeners.size) this.stop();
|
|
43
|
+
else if (owner.document.visibilityState === 'visible') this.schedule();
|
|
44
|
+
};
|
|
45
|
+
this.onResize = () => {
|
|
46
|
+
if (screenKey(owner) === this.key) return;
|
|
47
|
+
this.pause();
|
|
48
|
+
this.resolveReady?.('full');
|
|
49
|
+
this.resolveReady = null;
|
|
50
|
+
this.ready = null;
|
|
51
|
+
this.key = screenKey(owner);
|
|
52
|
+
this.initialized = false;
|
|
53
|
+
this.healthy = 0;
|
|
54
|
+
this.lastPressure = this.lastChange = -Infinity;
|
|
55
|
+
this.update({ quality: 'full', source: 'default', baselineMs: null, frameDelayMs: null, gpuQueueDelayMs: null, longFrameMs: null, measuredAt: null });
|
|
56
|
+
if (this.started) this.prepare();
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
get quality() { return this.snapshot.quality; }
|
|
61
|
+
|
|
62
|
+
setDebug(enabled) {
|
|
63
|
+
this.debug = enabled === true;
|
|
64
|
+
if (this.debug) this.log('debug:enabled');
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
log(event, details = this.snapshot) {
|
|
68
|
+
if (!this.debug) return;
|
|
69
|
+
try { this.owner.console?.debug?.('[Mun rendering-quality]', event, details); } catch { /* diagnostics must not affect rendering */ }
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
subscribe(listener) {
|
|
73
|
+
if (screenKey(this.owner) !== this.key) this.onResize();
|
|
74
|
+
const subscription = snapshot => listener(snapshot);
|
|
75
|
+
this.listeners.add(subscription);
|
|
76
|
+
this.emit(subscription);
|
|
77
|
+
this.start();
|
|
78
|
+
if (!this.ready) this.prepare();
|
|
79
|
+
return () => {
|
|
80
|
+
this.listeners.delete(subscription);
|
|
81
|
+
if (!this.listeners.size) {
|
|
82
|
+
this.resolveReady?.(this.quality);
|
|
83
|
+
this.resolveReady = null;
|
|
84
|
+
this.stop();
|
|
85
|
+
}
|
|
86
|
+
};
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
prepare() {
|
|
90
|
+
if (screenKey(this.owner) !== this.key) this.onResize();
|
|
91
|
+
if (this.ready) return this.ready;
|
|
92
|
+
this.ready = new Promise(resolve => { this.resolveReady = resolve; });
|
|
93
|
+
this.start();
|
|
94
|
+
void this.check(true);
|
|
95
|
+
return this.ready;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
start() {
|
|
99
|
+
if (this.started) return;
|
|
100
|
+
this.started = true;
|
|
101
|
+
this.log('monitor:start');
|
|
102
|
+
this.owner.document.addEventListener('visibilitychange', this.onVisibility);
|
|
103
|
+
this.owner.addEventListener('resize', this.onResize);
|
|
104
|
+
const Observer = this.owner.PerformanceObserver;
|
|
105
|
+
try {
|
|
106
|
+
const type = Observer?.supportedEntryTypes?.includes('long-animation-frame') ? 'long-animation-frame'
|
|
107
|
+
: Observer?.supportedEntryTypes?.includes('longtask') ? 'longtask' : null;
|
|
108
|
+
if (type) {
|
|
109
|
+
this.observer = new Observer(list => {
|
|
110
|
+
if (this.owner.document.visibilityState !== 'visible') return;
|
|
111
|
+
const duration = Math.max(0, ...list.getEntries().map(entry => entry.duration));
|
|
112
|
+
this.update({ longFrameMs: duration });
|
|
113
|
+
if (duration >= 50) this.pressure('long-frame');
|
|
114
|
+
});
|
|
115
|
+
this.observer.observe({ type });
|
|
116
|
+
}
|
|
117
|
+
} catch { /* observers are optional */ }
|
|
118
|
+
if (this.ready && !this.resolveReady) this.schedule();
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
pause() {
|
|
122
|
+
this.log('monitor:pause');
|
|
123
|
+
this.epoch++;
|
|
124
|
+
this.owner.clearTimeout(this.timer);
|
|
125
|
+
this.timer = 0;
|
|
126
|
+
this.running?.abort();
|
|
127
|
+
this.running = null;
|
|
128
|
+
this.frameSamples = this.delayedFrames = this.healthy = 0;
|
|
129
|
+
this.pressureWindows.clear();
|
|
130
|
+
this.pressureTimes.clear();
|
|
131
|
+
this.pendingQueues.clear();
|
|
132
|
+
this.frameWindow = this.owner.performance.now();
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
stop() {
|
|
136
|
+
this.log('monitor:stop');
|
|
137
|
+
this.pause();
|
|
138
|
+
this.started = false;
|
|
139
|
+
this.observer?.disconnect();
|
|
140
|
+
this.observer = null;
|
|
141
|
+
this.owner.document.removeEventListener('visibilitychange', this.onVisibility);
|
|
142
|
+
this.owner.removeEventListener('resize', this.onResize);
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
schedule() {
|
|
146
|
+
this.owner.clearTimeout(this.timer);
|
|
147
|
+
if (!this.started || !this.listeners.size || this.owner.document.visibilityState !== 'visible') return;
|
|
148
|
+
const delay = Math.max(0, intervalMs - (this.owner.performance.now() - this.lastCheck));
|
|
149
|
+
this.timer = this.owner.setTimeout(() => { this.timer = 0; void this.check(false); }, delay);
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
async check(initial) {
|
|
153
|
+
if (this.running || !this.started) return;
|
|
154
|
+
const abort = new AbortController();
|
|
155
|
+
this.running = abort;
|
|
156
|
+
if (this.debug) this.log('probe:start', { initial, cached: initial });
|
|
157
|
+
const result = await probeRenderingQuality(this.owner, { cached: initial, signal: abort.signal });
|
|
158
|
+
this.log('probe:result', result);
|
|
159
|
+
if (this.running !== abort) return;
|
|
160
|
+
this.running = null;
|
|
161
|
+
const now = this.owner.performance.now();
|
|
162
|
+
this.lastCheck = now;
|
|
163
|
+
if (result.quality) {
|
|
164
|
+
const current = levels.indexOf(this.quality), target = levels.indexOf(result.quality);
|
|
165
|
+
let quality = this.quality;
|
|
166
|
+
if (!this.initialized || target > current) {
|
|
167
|
+
quality = result.quality;
|
|
168
|
+
this.healthy = 0;
|
|
169
|
+
} else if (target < current && !this.pendingQueues.size && now - this.lastPressure >= intervalMs) {
|
|
170
|
+
if (++this.healthy >= 2) { quality = levels[current - 1]; this.healthy = 0; }
|
|
171
|
+
} else this.healthy = 0;
|
|
172
|
+
if (quality !== this.quality) this.lastChange = now;
|
|
173
|
+
this.update({ quality, source: result.source, measuredAt: now, ...(result.baselineMs ? { baselineMs: result.baselineMs } : {}) });
|
|
174
|
+
if (result.source !== 'cache') cacheRenderingQuality(this.owner, this.key, quality);
|
|
175
|
+
} else this.healthy = 0; // Unknown samples never restore quality during runtime.
|
|
176
|
+
this.initialized = true;
|
|
177
|
+
this.resolveReady?.(this.quality);
|
|
178
|
+
this.resolveReady = null;
|
|
179
|
+
if (!this.listeners.size) this.stop();
|
|
180
|
+
else this.schedule();
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
update(values) {
|
|
184
|
+
const previous = this.quality;
|
|
185
|
+
this.snapshot = Object.freeze({ ...this.snapshot, ...values });
|
|
186
|
+
if (previous !== this.quality && this.debug) this.log('quality:change', { from: previous, ...this.snapshot });
|
|
187
|
+
for (const listener of this.listeners) this.emit(listener);
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
emit(listener) {
|
|
191
|
+
try { listener(this.snapshot); } catch { /* isolate renderer callbacks */ }
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
pressure(source) {
|
|
195
|
+
if (!this.initialized || !this.started || this.running || this.owner.document.visibilityState !== 'visible') return;
|
|
196
|
+
const now = this.owner.performance.now();
|
|
197
|
+
const previous = this.pressureTimes.get(source) ?? -Infinity;
|
|
198
|
+
if (now - previous < 1000) return;
|
|
199
|
+
this.pressureTimes.set(source, now);
|
|
200
|
+
this.lastPressure = now;
|
|
201
|
+
this.healthy = 0;
|
|
202
|
+
const count = now - previous > 2500 ? 1 : (this.pressureWindows.get(source) ?? 0) + 1;
|
|
203
|
+
this.pressureWindows.set(source, count);
|
|
204
|
+
if (this.debug) this.log('pressure', { signal: source, streak: count, ...this.snapshot });
|
|
205
|
+
if (count < 3 || now - this.lastChange < 10_000) return;
|
|
206
|
+
this.pressureWindows.clear();
|
|
207
|
+
const quality = levels[Math.min(2, levels.indexOf(this.quality) + 1)];
|
|
208
|
+
if (quality === this.quality) return;
|
|
209
|
+
this.lastChange = now;
|
|
210
|
+
this.update({ quality, source: 'pressure' });
|
|
211
|
+
cacheRenderingQuality(this.owner, this.key, quality);
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
/** Piggyback on renderer-owned animation callbacks; never create a monitoring RAF. */
|
|
215
|
+
recordFrame(durationMs) {
|
|
216
|
+
if (!this.started || this.running || this.owner.document.visibilityState !== 'visible' || durationMs < 4 || durationMs > 250 || !Number.isFinite(durationMs)) return;
|
|
217
|
+
const now = this.owner.performance.now();
|
|
218
|
+
this.frameSamples++;
|
|
219
|
+
if (durationMs >= Math.max(24, (this.snapshot.baselineMs ?? 16.67) * 1.5)) this.delayedFrames++;
|
|
220
|
+
if (now - this.frameWindow < 1000) return;
|
|
221
|
+
this.update({ frameDelayMs: durationMs });
|
|
222
|
+
if (this.frameSamples >= 4 && this.delayedFrames / this.frameSamples >= 0.25) this.pressure('frame');
|
|
223
|
+
else this.pressureWindows.delete('frame');
|
|
224
|
+
this.frameWindow = now;
|
|
225
|
+
this.frameSamples = this.delayedFrames = 0;
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
/** Queue completion latency includes scheduling delay; it is not GPU utilization. */
|
|
229
|
+
sampleGPUQueue(queue) {
|
|
230
|
+
if (!this.started || this.running || this.owner.document.visibilityState !== 'visible' || typeof queue?.onSubmittedWorkDone !== 'function') return;
|
|
231
|
+
const now = this.owner.performance.now();
|
|
232
|
+
const previous = this.queues.get(queue);
|
|
233
|
+
if (previous?.pending) {
|
|
234
|
+
this.pendingQueues.add(previous);
|
|
235
|
+
if (now - (previous.reported ?? previous.at) >= 1000) {
|
|
236
|
+
previous.reported = now;
|
|
237
|
+
this.update({ gpuQueueDelayMs: now - previous.at });
|
|
238
|
+
this.pressure('queue');
|
|
239
|
+
}
|
|
240
|
+
return;
|
|
241
|
+
}
|
|
242
|
+
if (previous && now - previous.at < 1000) return;
|
|
243
|
+
const entry = { pending: true, at: now };
|
|
244
|
+
this.queues.set(queue, entry);
|
|
245
|
+
this.pendingQueues.add(entry);
|
|
246
|
+
const epoch = this.epoch;
|
|
247
|
+
try {
|
|
248
|
+
Promise.resolve(queue.onSubmittedWorkDone()).then(() => {
|
|
249
|
+
entry.pending = false;
|
|
250
|
+
this.pendingQueues.delete(entry);
|
|
251
|
+
if (!this.started || epoch !== this.epoch || this.running || this.owner.document.visibilityState !== 'visible') return;
|
|
252
|
+
const duration = this.owner.performance.now() - now;
|
|
253
|
+
this.update({ gpuQueueDelayMs: duration });
|
|
254
|
+
if (duration >= Math.max(32, (this.snapshot.baselineMs ?? 16.67) * 2)) this.pressure('queue');
|
|
255
|
+
else this.pressureWindows.delete('queue');
|
|
256
|
+
}, () => { entry.pending = false; this.pendingQueues.delete(entry); });
|
|
257
|
+
} catch { entry.pending = false; this.pendingQueues.delete(entry); }
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
export function getRenderingQuality(owner = defaultOwner()) {
|
|
262
|
+
if (!owner) return null;
|
|
263
|
+
try {
|
|
264
|
+
let controller = controllers.get(owner);
|
|
265
|
+
if (!controller) { controller = new RenderingQualityController(owner); controllers.set(owner, controller); }
|
|
266
|
+
return controller;
|
|
267
|
+
} catch { return null; }
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
export function prepareRenderingQuality(owner = defaultOwner()) {
|
|
271
|
+
try { return getRenderingQuality(owner)?.prepare() ?? Promise.resolve('full'); } catch { return Promise.resolve('full'); }
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
export function observeRenderingQuality(listener, owner = defaultOwner()) {
|
|
275
|
+
const controller = getRenderingQuality(owner);
|
|
276
|
+
if (controller) return controller.subscribe(listener);
|
|
277
|
+
try { listener(Object.freeze({ quality: 'full', source: 'default', baselineMs: null, frameDelayMs: null, gpuQueueDelayMs: null, longFrameMs: null, measuredAt: null })); } catch { /* isolate renderer callbacks */ }
|
|
278
|
+
return () => {};
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
/** Opt-in development diagnostics; enabling logging starts no monitoring work. */
|
|
282
|
+
export function setRenderingQualityDebug(enabled, owner = defaultOwner()) {
|
|
283
|
+
getRenderingQuality(owner)?.setDebug(enabled);
|
|
284
|
+
}
|
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
/* SPDX-License-Identifier: MIT */
|
|
2
|
+
|
|
3
|
+
export const inputEvents = ['pointerdown', 'pointermove', 'keydown', 'wheel', 'touchstart', 'touchmove', 'scroll'];
|
|
4
|
+
// Retain the startup probe's existing cache across this API migration.
|
|
5
|
+
const cachePrefix = 'mun:backdrop-quality:v1:';
|
|
6
|
+
export const screenKey = owner => JSON.stringify([owner.navigator.userAgent, owner.screen.width, owner.screen.height, owner.screen.colorDepth, owner.devicePixelRatio]);
|
|
7
|
+
const isQuality = value => value === 'full' || value === 'reduced' || value === 'none';
|
|
8
|
+
const requiredAPIs = ['requestIdleCallback', 'cancelIdleCallback', 'requestAnimationFrame', 'cancelAnimationFrame'];
|
|
9
|
+
|
|
10
|
+
function classifyFrames(baseline, filtered) {
|
|
11
|
+
if (baseline.length !== 2 || filtered.length !== 2) return undefined;
|
|
12
|
+
if ([...baseline, ...filtered].some(ms => !Number.isFinite(ms) || ms < 4 || ms > 100)) return undefined;
|
|
13
|
+
const base = (baseline[0] + baseline[1]) / 2;
|
|
14
|
+
if (base > 40 || Math.abs(baseline[0] - baseline[1]) > Math.max(2, base * 0.2)) return undefined;
|
|
15
|
+
const slow = Math.min(...filtered);
|
|
16
|
+
if (Math.max(...filtered) > slow * 1.25) return undefined;
|
|
17
|
+
if (slow >= Math.max(48, base * 2.5)) return 'none';
|
|
18
|
+
if (slow >= Math.max(24, base * 1.5)) return 'reduced';
|
|
19
|
+
return 'full';
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/** Compatibility classifier: uncertainty defaults to full only for initial selection. */
|
|
23
|
+
export function qualityFromFrames(baseline, filtered) {
|
|
24
|
+
return classifyFrames(baseline, filtered) ?? 'full';
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/** A single cancellable, bounded CSS backdrop sample. Repeat calls bypass the cache. */
|
|
28
|
+
export function probeRenderingQuality(owner, { cached = false, signal } = {}) {
|
|
29
|
+
return new Promise(resolve => {
|
|
30
|
+
if (!owner || signal?.aborted) return resolve({ source: 'unavailable', reason: 'aborted' });
|
|
31
|
+
const document = owner.document;
|
|
32
|
+
const missing = requiredAPIs.filter(name => typeof owner[name] !== 'function');
|
|
33
|
+
if (missing.length) return resolve({ source: 'unavailable', reason: 'unsupported-api', missing });
|
|
34
|
+
const key = screenKey(owner);
|
|
35
|
+
let done = false, frame = 0, idle = 0, quiet = 0, watchdog = 0;
|
|
36
|
+
let sample;
|
|
37
|
+
const finish = (result = { source: 'unavailable' }) => {
|
|
38
|
+
if (done) return;
|
|
39
|
+
done = true;
|
|
40
|
+
owner.cancelAnimationFrame(frame);
|
|
41
|
+
owner.cancelIdleCallback(idle);
|
|
42
|
+
owner.clearTimeout(quiet);
|
|
43
|
+
owner.clearTimeout(watchdog);
|
|
44
|
+
owner.removeEventListener('load', loaded);
|
|
45
|
+
owner.removeEventListener('pagehide', cancel);
|
|
46
|
+
document.removeEventListener('visibilitychange', cancel);
|
|
47
|
+
owner.removeEventListener('resize', cancel);
|
|
48
|
+
signal?.removeEventListener('abort', cancel);
|
|
49
|
+
for (const event of inputEvents) owner.removeEventListener(event, cancel, true);
|
|
50
|
+
sample?.remove();
|
|
51
|
+
resolve(result);
|
|
52
|
+
};
|
|
53
|
+
const cancel = event => finish({ source: 'unavailable', reason: event?.type ?? 'timeout' });
|
|
54
|
+
const safe = work => { try { work(); } catch { finish({ source: 'unavailable', reason: 'exception' }); } };
|
|
55
|
+
const available = () => !done && document.visibilityState === 'visible' && screenKey(owner) === key;
|
|
56
|
+
const whenIdle = work => {
|
|
57
|
+
idle = owner.requestIdleCallback(deadline => safe(() => {
|
|
58
|
+
if (!available()) return finish({ source: 'unavailable', reason: 'not-visible-or-screen-changed' });
|
|
59
|
+
if (deadline.didTimeout || deadline.timeRemaining() < 8) return finish({ source: 'unavailable', reason: 'insufficient-idle-budget' });
|
|
60
|
+
work();
|
|
61
|
+
}));
|
|
62
|
+
};
|
|
63
|
+
const measure = next => {
|
|
64
|
+
const times = [];
|
|
65
|
+
const tick = time => safe(() => {
|
|
66
|
+
if (!available()) return finish({ source: 'unavailable', reason: 'not-visible-or-screen-changed' });
|
|
67
|
+
times.push(time);
|
|
68
|
+
if (times.length === 3) return next([times[1] - times[0], times[2] - times[1]]);
|
|
69
|
+
sample.style.backgroundPosition = `${times.length % 2}px 0px`;
|
|
70
|
+
frame = owner.requestAnimationFrame(tick);
|
|
71
|
+
});
|
|
72
|
+
frame = owner.requestAnimationFrame(tick);
|
|
73
|
+
};
|
|
74
|
+
const probe = () => {
|
|
75
|
+
if (cached) {
|
|
76
|
+
try {
|
|
77
|
+
const quality = owner.localStorage.getItem(cachePrefix + key);
|
|
78
|
+
if (isQuality(quality)) return finish({ quality, source: 'cache' });
|
|
79
|
+
} catch { /* storage is optional */ }
|
|
80
|
+
}
|
|
81
|
+
if (!owner.CSS?.supports('backdrop-filter', 'blur(1px)') && !owner.CSS?.supports('-webkit-backdrop-filter', 'blur(1px)')) return finish({ source: 'unavailable', reason: 'unsupported-backdrop-filter' });
|
|
82
|
+
sample = document.createElement('div');
|
|
83
|
+
sample.setAttribute('aria-hidden', 'true');
|
|
84
|
+
sample.dataset.pbProbe = '';
|
|
85
|
+
const dpr = Math.max(1, owner.devicePixelRatio || 1);
|
|
86
|
+
sample.style.cssText = `position:fixed;right:0;bottom:0;width:${32 / dpr}px;height:${32 / dpr}px;z-index:2147483647;pointer-events:none;overflow:hidden;background:repeating-linear-gradient(90deg,#888 0px,#888 1px,#999 1px,#999 2px)`;
|
|
87
|
+
document.body.append(sample);
|
|
88
|
+
owner.clearTimeout(watchdog);
|
|
89
|
+
watchdog = owner.setTimeout(cancel, 250);
|
|
90
|
+
measure(baseline => whenIdle(() => {
|
|
91
|
+
// The same three equal-variance layers and 17-stop linear masks as the
|
|
92
|
+
// original radius-6 edge probe, without depending on a particular renderer.
|
|
93
|
+
for (let index = 0; index < 3; index++) {
|
|
94
|
+
const stops = Array.from({ length: 17 }, (_, sample) => {
|
|
95
|
+
const t = sample / 16;
|
|
96
|
+
const alpha = Math.round(Math.min(1, Math.max(0, 3 * t * t - index)) * 1000) / 1000;
|
|
97
|
+
return `rgba(0,0,0,${alpha}) calc(100% - ${(Math.round((1 - t) * 32 / dpr * 100) / 100)}px)`;
|
|
98
|
+
}).join(',');
|
|
99
|
+
const element = document.createElement('div');
|
|
100
|
+
const mask = `linear-gradient(to bottom,${stops})`;
|
|
101
|
+
element.style.cssText = `position:absolute;inset:0;backdrop-filter:blur(${3.46 / dpr}px);-webkit-backdrop-filter:blur(${3.46 / dpr}px);mask-image:${mask};-webkit-mask-image:${mask}`;
|
|
102
|
+
sample.append(element);
|
|
103
|
+
}
|
|
104
|
+
measure(filtered => finish({ quality: classifyFrames(baseline, filtered), source: 'probe', baselineMs: (baseline[0] + baseline[1]) / 2, baseline, filtered }));
|
|
105
|
+
}));
|
|
106
|
+
};
|
|
107
|
+
const loaded = () => safe(() => {
|
|
108
|
+
frame = owner.requestAnimationFrame(() => {
|
|
109
|
+
frame = owner.requestAnimationFrame(() => {
|
|
110
|
+
quiet = owner.setTimeout(() => safe(() => whenIdle(probe)), 1000);
|
|
111
|
+
});
|
|
112
|
+
});
|
|
113
|
+
});
|
|
114
|
+
for (const event of inputEvents) owner.addEventListener(event, cancel, { capture: true, passive: true });
|
|
115
|
+
document.addEventListener('visibilitychange', cancel);
|
|
116
|
+
owner.addEventListener('pagehide', cancel);
|
|
117
|
+
owner.addEventListener('resize', cancel);
|
|
118
|
+
signal?.addEventListener('abort', cancel, { once: true });
|
|
119
|
+
watchdog = owner.setTimeout(cancel, 5000);
|
|
120
|
+
if (document.readyState === 'complete') loaded();
|
|
121
|
+
else owner.addEventListener('load', loaded, { once: true });
|
|
122
|
+
}).catch(() => ({ source: 'unavailable' }));
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
/** Persist the selected policy, rather than a noisy raw sample, outside input work. */
|
|
126
|
+
export function cacheRenderingQuality(owner, key, quality) {
|
|
127
|
+
if (typeof owner.requestIdleCallback !== 'function') return;
|
|
128
|
+
owner.requestIdleCallback(deadline => {
|
|
129
|
+
if (deadline.timeRemaining() < 2) return;
|
|
130
|
+
try { owner.localStorage.setItem(cachePrefix + key, quality); } catch { /* blocked or full storage */ }
|
|
131
|
+
});
|
|
132
|
+
}
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import type { MotionValue } from '../../index.js';
|
|
2
|
+
import type { TimelinePlayer } from '../timeline/index.js';
|
|
3
|
+
|
|
4
|
+
export type ScrollMetrics = { offset: number; viewport: number; extent: number; max: number };
|
|
5
|
+
export type ScrollRangeContext = { offset?: number; metrics?: ScrollMetrics; target?: unknown; axis?: 'x' | 'y' };
|
|
6
|
+
export type ScrollBound = number | ((context: ScrollRangeContext | ScrollMetrics) => number);
|
|
7
|
+
|
|
8
|
+
export class ScrollTracker {
|
|
9
|
+
constructor(options?: {
|
|
10
|
+
start?: ScrollBound;
|
|
11
|
+
end?: ScrollBound;
|
|
12
|
+
clamp?: boolean;
|
|
13
|
+
velocity?: { windowMs?: number; maxSamples?: number; maxVelocity?: number };
|
|
14
|
+
initialOffset?: number;
|
|
15
|
+
});
|
|
16
|
+
readonly offset: MotionValue;
|
|
17
|
+
readonly progress: MotionValue;
|
|
18
|
+
readonly lastRange: { start: number; end: number; span: number };
|
|
19
|
+
setRange(start: ScrollBound, end: ScrollBound): this;
|
|
20
|
+
resolveRange(context?: ScrollRangeContext): { start: number; end: number; span: number };
|
|
21
|
+
sample(offset: number, time?: number, options?: { context?: ScrollRangeContext; resetVelocity?: boolean }): number;
|
|
22
|
+
reset(offset?: number, time?: number, context?: ScrollRangeContext): number;
|
|
23
|
+
getState(): { offset: number; velocity: number; progress: number; progressVelocity: number; start: number; end: number };
|
|
24
|
+
}
|
|
25
|
+
export function createScrollTracker(options?: ConstructorParameters<typeof ScrollTracker>[0]): ScrollTracker;
|
|
26
|
+
export function readScrollMetrics(target: Window | Element | object, axis?: 'x' | 'y'): ScrollMetrics;
|
|
27
|
+
|
|
28
|
+
export class ScrollObserver {
|
|
29
|
+
constructor(target: EventTarget & object, options?: {
|
|
30
|
+
tracker?: ScrollTracker;
|
|
31
|
+
axis?: 'x' | 'y';
|
|
32
|
+
start?: ScrollBound;
|
|
33
|
+
end?: ScrollBound;
|
|
34
|
+
clamp?: boolean;
|
|
35
|
+
requestFrame?: (callback: FrameRequestCallback) => number;
|
|
36
|
+
cancelFrame?: (id: number) => void;
|
|
37
|
+
passive?: boolean;
|
|
38
|
+
velocity?: { windowMs?: number; maxSamples?: number; maxVelocity?: number };
|
|
39
|
+
autoStart?: boolean;
|
|
40
|
+
});
|
|
41
|
+
readonly tracker: ScrollTracker;
|
|
42
|
+
readonly offset: MotionValue;
|
|
43
|
+
readonly progress: MotionValue;
|
|
44
|
+
metrics: ScrollMetrics;
|
|
45
|
+
schedule(): void;
|
|
46
|
+
update(time?: number, options?: { resetVelocity?: boolean }): ReturnType<ScrollTracker['getState']>;
|
|
47
|
+
dispose(): void;
|
|
48
|
+
}
|
|
49
|
+
export function observeScroll(target: EventTarget & object, options?: ConstructorParameters<typeof ScrollObserver>[1]): ScrollObserver;
|
|
50
|
+
|
|
51
|
+
export class ScrollTimelineLink {
|
|
52
|
+
constructor(player: TimelinePlayer, source: ScrollTracker | ScrollObserver | MotionValue, options?: { pause?: boolean });
|
|
53
|
+
dispose(): void;
|
|
54
|
+
}
|
|
55
|
+
export function bindScrollTimeline(player: TimelinePlayer, source: ScrollTracker | ScrollObserver | MotionValue, options?: { pause?: boolean }): ScrollTimelineLink;
|