@krate/motion 0.0.0-bootstrap.0 → 0.5.0-rc.17
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/LICENSE +201 -0
- package/NOTICE +36 -0
- package/README.md +56 -1
- package/package.json +64 -6
- package/src/components.tsx +59 -0
- package/src/context.ts +9 -0
- package/src/engine/animate.ts +340 -0
- package/src/engine/animation.ts +852 -0
- package/src/engine/drag.ts +584 -0
- package/src/engine/easing.ts +108 -0
- package/src/engine/factories.ts +192 -0
- package/src/engine/fallback.ts +44 -0
- package/src/engine/frameloop.ts +179 -0
- package/src/engine/generators.ts +342 -0
- package/src/engine/gesture-features.ts +136 -0
- package/src/engine/gestures.ts +229 -0
- package/src/engine/index.ts +22 -0
- package/src/engine/krate.ts +402 -0
- package/src/engine/layout.ts +365 -0
- package/src/engine/motion-value.ts +234 -0
- package/src/engine/node.ts +988 -0
- package/src/engine/render.ts +295 -0
- package/src/engine/reorder.ts +65 -0
- package/src/engine/runtime.ts +74 -0
- package/src/engine/scope.ts +83 -0
- package/src/engine/scroll.ts +272 -0
- package/src/engine/signals.ts +108 -0
- package/src/engine/utils.ts +122 -0
- package/src/engine/values.ts +508 -0
- package/src/hooks.ts +191 -0
- package/src/index.ts +143 -0
- package/src/island/index.ts +696 -0
- package/src/motion-types.ts +25 -0
- package/src/reorder.tsx +112 -0
- package/src/types.ts +179 -0
|
@@ -0,0 +1,192 @@
|
|
|
1
|
+
// Motion values derived from other motion values, shared by the Krate hooks
|
|
2
|
+
// and the Preact hooks: transform, spring, velocity, template, time, scroll.
|
|
3
|
+
// Ported from framer-motion and motion-dom (MIT, Copyright (c) 2018 Framer
|
|
4
|
+
// B.V.), see NOTICE.
|
|
5
|
+
|
|
6
|
+
import { acceleratedValues, JSAnimation } from './animation.js';
|
|
7
|
+
import { cancelFrame, frame } from './frameloop.js';
|
|
8
|
+
import { collectMotionValues, isMotionValue, motionValue, MotionValue, subscribeValue } from './motion-value.js';
|
|
9
|
+
import { transformProps } from './render.js';
|
|
10
|
+
import { scrollInfo, type ScrollInfoOptions } from './scroll.js';
|
|
11
|
+
import { transform, type InterpolateOptions } from './values.js';
|
|
12
|
+
|
|
13
|
+
/** A motion value combining `inputs` through `combine`, updated before each render after an input changes. */
|
|
14
|
+
export function combineMotionValues<O>(inputs: MotionValue<any>[], combine: () => O): MotionValue<O> {
|
|
15
|
+
const value = motionValue(combine());
|
|
16
|
+
subscribeValue(inputs, value, combine);
|
|
17
|
+
return value;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/** A motion value computed by `fn` from the motion values it reads with `.get()`. */
|
|
21
|
+
export function computedMotionValue<O>(fn: () => O): MotionValue<O> {
|
|
22
|
+
const collected: MotionValue<any>[] = [];
|
|
23
|
+
collectMotionValues.current = collected;
|
|
24
|
+
try {
|
|
25
|
+
fn();
|
|
26
|
+
} finally {
|
|
27
|
+
collectMotionValues.current = undefined;
|
|
28
|
+
}
|
|
29
|
+
return combineMotionValues(collected, fn);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* useTransform: `(value, inputRange, outputRange, options)`, `(value, fn)`,
|
|
34
|
+
* `([a, b], ([a, b]) => ...)` or `(() => a.get() * 2)`.
|
|
35
|
+
*/
|
|
36
|
+
export function transformMotionValue(input: any, rangeOrFn?: any, outputRange?: any[], options?: InterpolateOptions<any>): MotionValue<any> {
|
|
37
|
+
if (typeof input === 'function') return computedMotionValue(input);
|
|
38
|
+
if (isMotionValue(rangeOrFn)) throw new Error('@krate/motion: useTransform needs an input range array or a function');
|
|
39
|
+
const fn = typeof rangeOrFn === 'function' ? rangeOrFn : transform(rangeOrFn, outputRange!, options);
|
|
40
|
+
const values: MotionValue<any>[] = Array.isArray(input) ? input : [input];
|
|
41
|
+
const latest: any[] = [];
|
|
42
|
+
const combine = (): any => {
|
|
43
|
+
latest.length = 0;
|
|
44
|
+
for (let i = 0; i < values.length; i++) latest[i] = values[i].get();
|
|
45
|
+
return Array.isArray(input) ? fn(latest) : fn(latest[0]);
|
|
46
|
+
};
|
|
47
|
+
return combineMotionValues(values, combine);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export interface SpringOptions {
|
|
51
|
+
stiffness?: number;
|
|
52
|
+
damping?: number;
|
|
53
|
+
mass?: number;
|
|
54
|
+
velocity?: number;
|
|
55
|
+
restDelta?: number;
|
|
56
|
+
restSpeed?: number;
|
|
57
|
+
duration?: number;
|
|
58
|
+
bounce?: number;
|
|
59
|
+
visualDuration?: number;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
const asNumber = (v: unknown): number => (typeof v === 'number' ? v : parseFloat(v as string));
|
|
63
|
+
|
|
64
|
+
/** Makes `value` spring to every value set on it, and follow `source` when it is a motion value. */
|
|
65
|
+
export function attachSpring(value: MotionValue<any>, source: any, options: SpringOptions = {}): () => void {
|
|
66
|
+
const initial = value.get();
|
|
67
|
+
let active: JSAnimation | null = null;
|
|
68
|
+
let latest = initial;
|
|
69
|
+
let setter: (v: any) => void = () => {};
|
|
70
|
+
const unit = typeof initial === 'string' ? initial.replace(/[\d.-]/g, '') : undefined;
|
|
71
|
+
const parse = (v: number): any => (unit ? v + unit : v);
|
|
72
|
+
const stop = (): void => {
|
|
73
|
+
if (active) {
|
|
74
|
+
active.stop();
|
|
75
|
+
active = null;
|
|
76
|
+
}
|
|
77
|
+
};
|
|
78
|
+
const start = (): void => {
|
|
79
|
+
stop();
|
|
80
|
+
active = new JSAnimation({
|
|
81
|
+
keyframes: [asNumber(value.get()), asNumber(latest)],
|
|
82
|
+
velocity: value.getVelocity(),
|
|
83
|
+
type: 'spring',
|
|
84
|
+
restDelta: 0.001,
|
|
85
|
+
restSpeed: 0.01,
|
|
86
|
+
...options,
|
|
87
|
+
onUpdate: setter,
|
|
88
|
+
} as any);
|
|
89
|
+
};
|
|
90
|
+
value.attach((v, set) => {
|
|
91
|
+
latest = v;
|
|
92
|
+
setter = (l) => set(parse(l));
|
|
93
|
+
frame.postRender(start);
|
|
94
|
+
}, stop);
|
|
95
|
+
if (isMotionValue(source)) {
|
|
96
|
+
const off = source.on('change', (v: any) => value.set(parse(asNumber(v))));
|
|
97
|
+
const offDestroy = value.on('destroy', off);
|
|
98
|
+
return () => {
|
|
99
|
+
off();
|
|
100
|
+
offDestroy();
|
|
101
|
+
};
|
|
102
|
+
}
|
|
103
|
+
return stop;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/** A motion value that springs to its target (useSpring). */
|
|
107
|
+
export function springMotionValue(source: any, options?: SpringOptions): MotionValue<any> {
|
|
108
|
+
const value = motionValue(isMotionValue(source) ? source.get() : source);
|
|
109
|
+
attachSpring(value, source, options);
|
|
110
|
+
return value;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/** A motion value of another motion value's velocity (useVelocity). */
|
|
114
|
+
export function velocityMotionValue(value: MotionValue<any>): MotionValue<number> {
|
|
115
|
+
const velocity = motionValue(value.getVelocity());
|
|
116
|
+
const update = (): void => {
|
|
117
|
+
const latest = value.getVelocity();
|
|
118
|
+
velocity.set(latest);
|
|
119
|
+
if (latest) frame.update(update);
|
|
120
|
+
};
|
|
121
|
+
const off = value.on('change', () => {
|
|
122
|
+
frame.update(update, false, true);
|
|
123
|
+
});
|
|
124
|
+
velocity.on('destroy', off);
|
|
125
|
+
return velocity;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
/** A string motion value from a template literal of motion values (useMotionTemplate). */
|
|
129
|
+
export function motionTemplate(fragments: TemplateStringsArray, ...values: Array<MotionValue<any> | string | number>): MotionValue<string> {
|
|
130
|
+
const build = (): string => {
|
|
131
|
+
let out = '';
|
|
132
|
+
for (let i = 0; i < fragments.length; i++) {
|
|
133
|
+
out += fragments[i];
|
|
134
|
+
const v = values[i];
|
|
135
|
+
if (v !== undefined && v !== null && v !== '') out += isMotionValue(v) ? v.get() : v;
|
|
136
|
+
}
|
|
137
|
+
return out;
|
|
138
|
+
};
|
|
139
|
+
return combineMotionValues(values.filter(isMotionValue) as MotionValue<any>[], build);
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
/** Calls `cb(timeSinceStart, delta)` every frame until the returned stop is called. */
|
|
143
|
+
export function animationFrame(cb: (time: number, delta: number) => void): () => void {
|
|
144
|
+
let initial = 0;
|
|
145
|
+
const tick = ({ timestamp, delta }: { timestamp: number; delta: number }): void => {
|
|
146
|
+
if (!initial) initial = timestamp;
|
|
147
|
+
cb(timestamp - initial, delta);
|
|
148
|
+
};
|
|
149
|
+
frame.update(tick, true);
|
|
150
|
+
return () => cancelFrame(tick);
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
/** A motion value of the time (ms) since it was created, updated every frame (useTime). */
|
|
154
|
+
export function timeMotionValue(): [MotionValue<number>, () => void] {
|
|
155
|
+
const t = motionValue(0);
|
|
156
|
+
return [t, animationFrame((time) => t.set(time))];
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
export interface ScrollMotionValues {
|
|
160
|
+
scrollX: MotionValue<number>;
|
|
161
|
+
scrollY: MotionValue<number>;
|
|
162
|
+
scrollXProgress: MotionValue<number>;
|
|
163
|
+
scrollYProgress: MotionValue<number>;
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
export const createScrollMotionValues = (): ScrollMotionValues => ({
|
|
167
|
+
scrollX: motionValue(0),
|
|
168
|
+
scrollY: motionValue(0),
|
|
169
|
+
scrollXProgress: motionValue(0),
|
|
170
|
+
scrollYProgress: motionValue(0),
|
|
171
|
+
});
|
|
172
|
+
|
|
173
|
+
/** Feeds `values` from scroll of `container` (the page when unset); returns the stop. */
|
|
174
|
+
export function trackScroll(values: ScrollMotionValues, options: ScrollInfoOptions = {}): () => void {
|
|
175
|
+
return scrollInfo((info) => {
|
|
176
|
+
values.scrollX.set(info.x.current);
|
|
177
|
+
values.scrollXProgress.set(info.x.progress);
|
|
178
|
+
values.scrollY.set(info.y.current);
|
|
179
|
+
values.scrollYProgress.set(info.y.progress);
|
|
180
|
+
}, options);
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
/** will-change as a motion value: "transform" once a transform or compositor value animates. */
|
|
184
|
+
export class WillChangeMotionValue extends MotionValue<string> {
|
|
185
|
+
isEnabled = false;
|
|
186
|
+
add(name: string): void {
|
|
187
|
+
if (transformProps.has(name) || acceleratedValues.has(name)) {
|
|
188
|
+
this.isEnabled = true;
|
|
189
|
+
this.set(this.isEnabled ? 'transform' : 'auto');
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
// The runtime values of `motion` and `m`. The compiler turns `<motion.div>`
|
|
2
|
+
// in JSX into a plain element (see internal/motionjsx), so these only run
|
|
3
|
+
// for code it cannot see statically, such as `const Tag = motion.div`: they
|
|
4
|
+
// render the element in the browser with the runtime's h().
|
|
5
|
+
|
|
6
|
+
import { __kmRef } from './krate.js';
|
|
7
|
+
import { isValidMotionProp } from './node.js';
|
|
8
|
+
import { gestureFeatures } from './gesture-features.js';
|
|
9
|
+
|
|
10
|
+
type AnyProps = Record<string, any>;
|
|
11
|
+
|
|
12
|
+
function domProps(props: AnyProps): AnyProps {
|
|
13
|
+
const out: AnyProps = {};
|
|
14
|
+
for (const key in props) if (key !== 'children' && key !== 'ref' && !isValidMotionProp(key)) out[key] = props[key];
|
|
15
|
+
return out;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
function component(tag: any, strict: boolean): (props: AnyProps) => unknown {
|
|
19
|
+
return (props: AnyProps) => {
|
|
20
|
+
const h = (globalThis as unknown as { h?: (tag: any, props: AnyProps | null, ...children: unknown[]) => unknown }).h;
|
|
21
|
+
if (typeof h !== 'function') return null;
|
|
22
|
+
const ref = __kmRef(() => props, props.ref, undefined, typeof tag === 'string' ? tag : 'div', [gestureFeatures], strict);
|
|
23
|
+
return h(tag, { ...domProps(props), ref }, props.children);
|
|
24
|
+
};
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
const cache = new Map<string, unknown>();
|
|
28
|
+
|
|
29
|
+
function namespace(strict: boolean): any {
|
|
30
|
+
const create = (c: any) => component(c, strict);
|
|
31
|
+
const target = Object.assign((c: any) => create(c), { create });
|
|
32
|
+
return new Proxy(target, {
|
|
33
|
+
get(t, key: string | symbol) {
|
|
34
|
+
if (typeof key !== 'string' || key in t) return (t as any)[key];
|
|
35
|
+
const id = (strict ? 'motion:' : 'm:') + key;
|
|
36
|
+
let c = cache.get(id);
|
|
37
|
+
if (!c) cache.set(id, (c = component(key, strict)));
|
|
38
|
+
return c;
|
|
39
|
+
},
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export const motionNamespace = /*@__PURE__*/ namespace(true);
|
|
44
|
+
export const mNamespace = /*@__PURE__*/ namespace(false);
|
|
@@ -0,0 +1,179 @@
|
|
|
1
|
+
// The shared, batched frame loop: every motion value, animation and gesture
|
|
2
|
+
// schedules work into ordered steps of one requestAnimationFrame callback, so
|
|
3
|
+
// DOM reads and writes of a frame are grouped (read before render). Ported
|
|
4
|
+
// from motion-dom (MIT, Copyright (c) 2018 Framer B.V.), see NOTICE.
|
|
5
|
+
|
|
6
|
+
import { MotionGlobalConfig, noop } from './utils.js';
|
|
7
|
+
|
|
8
|
+
export interface FrameData {
|
|
9
|
+
delta: number;
|
|
10
|
+
timestamp: number;
|
|
11
|
+
isProcessing: boolean;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export type Process = (data: FrameData) => void;
|
|
15
|
+
export type StepId = 'setup' | 'read' | 'resolveKeyframes' | 'preUpdate' | 'update' | 'preRender' | 'render' | 'postRender';
|
|
16
|
+
|
|
17
|
+
export const stepsOrder: StepId[] = ['setup', 'read', 'resolveKeyframes', 'preUpdate', 'update', 'preRender', 'render', 'postRender'];
|
|
18
|
+
|
|
19
|
+
export interface Step {
|
|
20
|
+
schedule(process: Process, keepAlive?: boolean, immediate?: boolean): Process;
|
|
21
|
+
cancel(process: Process): void;
|
|
22
|
+
process(data: FrameData): void;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export type Batcher = Record<StepId, (process: Process, keepAlive?: boolean, immediate?: boolean) => Process>;
|
|
26
|
+
|
|
27
|
+
function createRenderStep(runNextFrame: () => void): Step {
|
|
28
|
+
let thisFrame = new Set<Process>();
|
|
29
|
+
let nextFrame = new Set<Process>();
|
|
30
|
+
let isProcessing = false;
|
|
31
|
+
let flushNextFrame = false;
|
|
32
|
+
const toKeepAlive = new WeakSet<Process>();
|
|
33
|
+
let latest: FrameData = { delta: 0, timestamp: 0, isProcessing: false };
|
|
34
|
+
function trigger(cb: Process): void {
|
|
35
|
+
if (toKeepAlive.has(cb)) {
|
|
36
|
+
step.schedule(cb);
|
|
37
|
+
runNextFrame();
|
|
38
|
+
}
|
|
39
|
+
cb(latest);
|
|
40
|
+
}
|
|
41
|
+
const step: Step = {
|
|
42
|
+
schedule(cb, keepAlive = false, immediate = false) {
|
|
43
|
+
const queue = immediate && isProcessing ? thisFrame : nextFrame;
|
|
44
|
+
if (keepAlive) toKeepAlive.add(cb);
|
|
45
|
+
queue.add(cb);
|
|
46
|
+
return cb;
|
|
47
|
+
},
|
|
48
|
+
cancel(cb) {
|
|
49
|
+
nextFrame.delete(cb);
|
|
50
|
+
toKeepAlive.delete(cb);
|
|
51
|
+
},
|
|
52
|
+
process(data) {
|
|
53
|
+
latest = data;
|
|
54
|
+
if (isProcessing) {
|
|
55
|
+
flushNextFrame = true;
|
|
56
|
+
return;
|
|
57
|
+
}
|
|
58
|
+
isProcessing = true;
|
|
59
|
+
[thisFrame, nextFrame] = [nextFrame, thisFrame];
|
|
60
|
+
thisFrame.forEach(trigger);
|
|
61
|
+
thisFrame.clear();
|
|
62
|
+
isProcessing = false;
|
|
63
|
+
if (flushNextFrame) {
|
|
64
|
+
flushNextFrame = false;
|
|
65
|
+
step.process(data);
|
|
66
|
+
}
|
|
67
|
+
},
|
|
68
|
+
};
|
|
69
|
+
return step;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
const maxElapsed = 40;
|
|
73
|
+
|
|
74
|
+
export function createRenderBatcher(scheduleNextBatch: (cb: () => void) => void, allowKeepAlive: boolean) {
|
|
75
|
+
let runNextFrame = false;
|
|
76
|
+
let useDefaultElapsed = true;
|
|
77
|
+
const state: FrameData = { delta: 0, timestamp: 0, isProcessing: false };
|
|
78
|
+
const flag = (): void => {
|
|
79
|
+
runNextFrame = true;
|
|
80
|
+
};
|
|
81
|
+
const steps = {} as Record<StepId, Step>;
|
|
82
|
+
for (const key of stepsOrder) steps[key] = createRenderStep(flag);
|
|
83
|
+
const processBatch = (): void => {
|
|
84
|
+
const timestamp = MotionGlobalConfig.useManualTiming ? state.timestamp : performance.now();
|
|
85
|
+
runNextFrame = false;
|
|
86
|
+
if (!MotionGlobalConfig.useManualTiming) {
|
|
87
|
+
state.delta = useDefaultElapsed ? 1000 / 60 : Math.max(Math.min(timestamp - state.timestamp, maxElapsed), 1);
|
|
88
|
+
}
|
|
89
|
+
state.timestamp = timestamp;
|
|
90
|
+
state.isProcessing = true;
|
|
91
|
+
for (const key of stepsOrder) steps[key].process(state);
|
|
92
|
+
state.isProcessing = false;
|
|
93
|
+
if (runNextFrame && allowKeepAlive) {
|
|
94
|
+
useDefaultElapsed = false;
|
|
95
|
+
scheduleNextBatch(processBatch);
|
|
96
|
+
}
|
|
97
|
+
};
|
|
98
|
+
const wake = (): void => {
|
|
99
|
+
runNextFrame = true;
|
|
100
|
+
useDefaultElapsed = true;
|
|
101
|
+
if (!state.isProcessing) scheduleNextBatch(processBatch);
|
|
102
|
+
};
|
|
103
|
+
const schedule = {} as Batcher;
|
|
104
|
+
for (const key of stepsOrder) {
|
|
105
|
+
schedule[key] = (process, keepAlive = false, immediate = false) => {
|
|
106
|
+
if (!runNextFrame) wake();
|
|
107
|
+
return steps[key].schedule(process, keepAlive, immediate);
|
|
108
|
+
};
|
|
109
|
+
}
|
|
110
|
+
const cancel = (process: Process): void => {
|
|
111
|
+
for (const key of stepsOrder) steps[key].cancel(process);
|
|
112
|
+
};
|
|
113
|
+
return { schedule, cancel, state, steps, processBatch };
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
const raf = typeof requestAnimationFrame !== 'undefined' ? (cb: () => void) => requestAnimationFrame(cb) : noop;
|
|
117
|
+
const loop = /*@__PURE__*/ createRenderBatcher(raf as (cb: () => void) => void, true);
|
|
118
|
+
|
|
119
|
+
/** Schedules work in a step of the next frame: `frame.update(cb)`, `frame.render(cb, true)` (keep alive). */
|
|
120
|
+
export const frame: Batcher = loop.schedule;
|
|
121
|
+
/** Cancels a callback scheduled with `frame`. */
|
|
122
|
+
export const cancelFrame: (process: Process) => void = loop.cancel;
|
|
123
|
+
/** Timing of the frame being processed (`delta`, `timestamp` in ms). */
|
|
124
|
+
export const frameData: FrameData = loop.state;
|
|
125
|
+
export const frameSteps: Record<StepId, Step> = loop.steps;
|
|
126
|
+
|
|
127
|
+
const micro = /*@__PURE__*/ createRenderBatcher(
|
|
128
|
+
typeof queueMicrotask !== 'undefined' ? queueMicrotask : (cb: () => void) => Promise.resolve().then(cb),
|
|
129
|
+
false,
|
|
130
|
+
);
|
|
131
|
+
export const microtask: Batcher = micro.schedule;
|
|
132
|
+
export const cancelMicrotask: (process: Process) => void = micro.cancel;
|
|
133
|
+
|
|
134
|
+
let now: number | undefined;
|
|
135
|
+
function clearTime(): void {
|
|
136
|
+
now = undefined;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
/** A clock that stays the same within one synchronous block (the frame time while a frame runs). */
|
|
140
|
+
export const time = {
|
|
141
|
+
now(): number {
|
|
142
|
+
if (now === undefined) {
|
|
143
|
+
time.set(frameData.isProcessing || MotionGlobalConfig.useManualTiming ? frameData.timestamp : performance.now());
|
|
144
|
+
}
|
|
145
|
+
return now as number;
|
|
146
|
+
},
|
|
147
|
+
set(t: number): void {
|
|
148
|
+
now = t;
|
|
149
|
+
if (typeof queueMicrotask !== 'undefined') queueMicrotask(clearTime);
|
|
150
|
+
else void Promise.resolve().then(clearTime);
|
|
151
|
+
},
|
|
152
|
+
};
|
|
153
|
+
|
|
154
|
+
/** Runs every step of a frame now (tests drive time with useManualTiming). */
|
|
155
|
+
export function flushFrame(timestamp?: number): void {
|
|
156
|
+
if (timestamp !== undefined) {
|
|
157
|
+
loop.state.delta = timestamp - loop.state.timestamp;
|
|
158
|
+
loop.state.timestamp = timestamp;
|
|
159
|
+
time.set(timestamp);
|
|
160
|
+
}
|
|
161
|
+
loop.processBatch();
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
/**
|
|
165
|
+
* Calls `callback` on the frame loop once `timeout` milliseconds have passed,
|
|
166
|
+
* with the overshoot in milliseconds. Returns a function that cancels it.
|
|
167
|
+
*/
|
|
168
|
+
export function delay(callback: (overshoot: number) => void, timeout: number): () => void {
|
|
169
|
+
const start = time.now();
|
|
170
|
+
const check: Process = ({ timestamp }) => {
|
|
171
|
+
const elapsed = timestamp - start;
|
|
172
|
+
if (elapsed >= timeout) {
|
|
173
|
+
cancelFrame(check);
|
|
174
|
+
callback(elapsed - timeout);
|
|
175
|
+
}
|
|
176
|
+
};
|
|
177
|
+
frame.setup(check, true);
|
|
178
|
+
return () => cancelFrame(check);
|
|
179
|
+
}
|