-
Notifications
You must be signed in to change notification settings - Fork 2
/
animation.ts
61 lines (58 loc) · 1.56 KB
/
animation.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
const raf =
globalThis.requestAnimationFrame ||
(globalThis as any).mozRequestAnimationFrame ||
(globalThis as any).webkitRequestAnimationFrame ||
(globalThis as any).msRequestAnimationFrame;
const caf =
globalThis.cancelAnimationFrame ||
(globalThis as any).mozCancelAnimationFrame ||
(globalThis as any).webkitCancelAnimationFrame ||
(globalThis as any).msCancelAnimationFrame;
/**
* 执行帧动画
* @function requestAnimationFrame
*
* @param {FrameRequestCallback} fn 动画函数
* @returns {number} 动画id
*
* @example
* var progress = 0;
* function render() {
* progress += 1; // 修改图像的位置
* if (progress < 100) { // 在动画没有结束前,递归渲染
* requestAnimationFrame(render);
* }
* }
* const handlerId = requestAnimationFrame(render);
* console.log('动画id', handlerId);
*/
export const requestAnimationFrame = (fn: FrameRequestCallback): number => raf(fn);
/**
* 取消帧动画
* @function cancelAnimationFrame
*
* @param {number | undefined | null} id 动画id
*
* @example
* const handlerId = requestAnimationFrame(render);
* cancelAnimationFrame(handlerId);
*/
export const cancelAnimationFrame = (id: number | undefined | null): void => {
if (id) caf(id);
};
/**
* 休眠
* @function sleep
*
* @param {number} ms 休眠时间(单位毫秒)
*
* @example
* sleep(1000);
*/
export const sleep = (ms: number): Promise<void> =>
new Promise((resolve) => {
const timer: NodeJS.Timeout = setTimeout(() => {
clearTimeout(timer);
resolve();
}, ms);
});