@thienvu18/designable-shared 2.0.1 → 2.0.2

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 (73) hide show
  1. package/esm/animation.d.ts +8 -2
  2. package/esm/animation.js +29 -30
  3. package/esm/array.d.ts +163 -37
  4. package/esm/array.js +104 -87
  5. package/esm/clone.d.ts +4 -4
  6. package/esm/clone.js +79 -86
  7. package/esm/compose.d.ts +3 -1
  8. package/esm/compose.js +6 -6
  9. package/esm/coordinate.d.ts +120 -76
  10. package/esm/coordinate.js +480 -411
  11. package/esm/element.d.ts +11 -6
  12. package/esm/element.js +123 -125
  13. package/esm/event.d.ts +162 -75
  14. package/esm/event.js +197 -197
  15. package/esm/globalThisPolyfill.d.ts +1 -1
  16. package/esm/globalThisPolyfill.js +14 -17
  17. package/esm/index.d.ts +17 -17
  18. package/esm/index.js +17 -17
  19. package/esm/instanceof.d.ts +1 -1
  20. package/esm/instanceof.js +9 -10
  21. package/esm/keycode.d.ts +145 -145
  22. package/esm/keycode.js +149 -149
  23. package/esm/lru.d.ts +50 -47
  24. package/esm/lru.js +236 -245
  25. package/esm/observer.d.ts +7 -7
  26. package/esm/observer.js +37 -40
  27. package/esm/request-idle.d.ts +9 -6
  28. package/esm/request-idle.js +6 -6
  29. package/esm/scroller.d.ts +24 -8
  30. package/esm/scroller.js +78 -75
  31. package/esm/subscribable.d.ts +10 -10
  32. package/esm/subscribable.js +39 -40
  33. package/esm/types.d.ts +13 -13
  34. package/esm/types.js +19 -20
  35. package/esm/uid.d.ts +1 -1
  36. package/esm/uid.js +7 -7
  37. package/lib/animation.d.ts +8 -2
  38. package/lib/animation.js +34 -35
  39. package/lib/array.d.ts +163 -37
  40. package/lib/array.js +129 -101
  41. package/lib/clone.d.ts +4 -4
  42. package/lib/clone.js +84 -91
  43. package/lib/compose.d.ts +3 -1
  44. package/lib/compose.js +10 -10
  45. package/lib/coordinate.d.ts +120 -76
  46. package/lib/coordinate.js +538 -440
  47. package/lib/element.d.ts +11 -6
  48. package/lib/element.js +139 -133
  49. package/lib/event.d.ts +162 -75
  50. package/lib/event.js +206 -202
  51. package/lib/globalThisPolyfill.d.ts +1 -1
  52. package/lib/globalThisPolyfill.js +17 -20
  53. package/lib/index.d.ts +17 -17
  54. package/lib/index.js +49 -33
  55. package/lib/instanceof.d.ts +1 -1
  56. package/lib/instanceof.js +13 -14
  57. package/lib/keycode.d.ts +145 -145
  58. package/lib/keycode.js +153 -153
  59. package/lib/lru.d.ts +50 -47
  60. package/lib/lru.js +240 -249
  61. package/lib/observer.d.ts +7 -7
  62. package/lib/observer.js +41 -44
  63. package/lib/request-idle.d.ts +9 -6
  64. package/lib/request-idle.js +14 -11
  65. package/lib/scroller.d.ts +24 -8
  66. package/lib/scroller.js +89 -81
  67. package/lib/subscribable.d.ts +10 -10
  68. package/lib/subscribable.js +43 -44
  69. package/lib/types.d.ts +13 -13
  70. package/lib/types.js +40 -28
  71. package/lib/uid.d.ts +1 -1
  72. package/lib/uid.js +11 -11
  73. package/package.json +1 -1
package/esm/observer.js CHANGED
@@ -1,43 +1,40 @@
1
1
  export class LayoutObserver {
2
- constructor(observer = () => { }) {
3
- this.connected = false;
4
- this.observe = (target) => {
5
- if (!target)
6
- return;
7
- this.resizeObserver?.observe(target);
8
- try {
9
- this.performanceObserver?.observe({
10
- entryTypes: ['paint', 'element', 'layout-shift', 'event'],
11
- });
12
- }
13
- catch (e) { }
14
- this.mutationObserver?.observe(target, {
15
- attributeFilter: ['style'],
16
- attributes: true,
17
- });
18
- this.connected = true;
19
- };
20
- this.disconnect = () => {
21
- if (this.connected) {
22
- this.resizeObserver?.disconnect();
23
- this.performanceObserver?.disconnect();
24
- this.mutationObserver?.disconnect();
25
- }
26
- this.connected = false;
27
- };
28
- if (typeof ResizeObserver !== 'undefined') {
29
- this.resizeObserver = new ResizeObserver(() => observer());
30
- }
31
- if (typeof PerformanceObserver !== 'undefined') {
32
- try {
33
- this.performanceObserver = new PerformanceObserver(() => {
34
- observer();
35
- });
36
- }
37
- catch (e) { }
38
- }
39
- if (typeof MutationObserver !== 'undefined') {
40
- this.mutationObserver = new MutationObserver(() => observer());
41
- }
2
+ constructor(observer = () => {}) {
3
+ this.connected = false
4
+ this.observe = (target) => {
5
+ if (!target) return
6
+ this.resizeObserver?.observe(target)
7
+ try {
8
+ this.performanceObserver?.observe({
9
+ entryTypes: ['paint', 'element', 'layout-shift', 'event'],
10
+ })
11
+ } catch (e) {}
12
+ this.mutationObserver?.observe(target, {
13
+ attributeFilter: ['style'],
14
+ attributes: true,
15
+ })
16
+ this.connected = true
42
17
  }
18
+ this.disconnect = () => {
19
+ if (this.connected) {
20
+ this.resizeObserver?.disconnect()
21
+ this.performanceObserver?.disconnect()
22
+ this.mutationObserver?.disconnect()
23
+ }
24
+ this.connected = false
25
+ }
26
+ if (typeof ResizeObserver !== 'undefined') {
27
+ this.resizeObserver = new ResizeObserver(() => observer())
28
+ }
29
+ if (typeof PerformanceObserver !== 'undefined') {
30
+ try {
31
+ this.performanceObserver = new PerformanceObserver(() => {
32
+ observer()
33
+ })
34
+ } catch (e) {}
35
+ }
36
+ if (typeof MutationObserver !== 'undefined') {
37
+ this.mutationObserver = new MutationObserver(() => observer())
38
+ }
39
+ }
43
40
  }
@@ -1,10 +1,13 @@
1
- import 'requestidlecallback';
1
+ import 'requestidlecallback'
2
2
  export interface IIdleDeadline {
3
- didTimeout: boolean;
4
- timeRemaining: () => DOMHighResTimeStamp;
3
+ didTimeout: boolean
4
+ timeRemaining: () => DOMHighResTimeStamp
5
5
  }
6
6
  export interface IdleCallbackOptions {
7
- timeout?: number;
7
+ timeout?: number
8
8
  }
9
- export declare const requestIdle: (callback: (params: IIdleDeadline) => void, options?: IdleCallbackOptions) => number;
10
- export declare const cancelIdle: (id: number) => void;
9
+ export declare const requestIdle: (
10
+ callback: (params: IIdleDeadline) => void,
11
+ options?: IdleCallbackOptions
12
+ ) => number
13
+ export declare const cancelIdle: (id: number) => void
@@ -1,8 +1,8 @@
1
- import 'requestidlecallback';
2
- import { globalThisPolyfill } from './globalThisPolyfill';
1
+ import 'requestidlecallback'
2
+ import { globalThisPolyfill } from './globalThisPolyfill'
3
3
  export const requestIdle = (callback, options) => {
4
- return globalThisPolyfill['requestIdleCallback'](callback, options);
5
- };
4
+ return globalThisPolyfill['requestIdleCallback'](callback, options)
5
+ }
6
6
  export const cancelIdle = (id) => {
7
- globalThisPolyfill['cancelIdleCallback'](id);
8
- };
7
+ globalThisPolyfill['cancelIdleCallback'](id)
8
+ }
package/esm/scroller.d.ts CHANGED
@@ -1,10 +1,26 @@
1
- import { IPoint } from './coordinate';
2
- export type ScrollDirection = 'begin' | 'end';
1
+ import { IPoint } from './coordinate'
2
+ export type ScrollDirection = 'begin' | 'end'
3
3
  export interface IAutoScrollBasicInfo {
4
- direction: ScrollDirection;
5
- speedFactor: number;
6
- speed: number;
4
+ direction: ScrollDirection
5
+ speedFactor: number
6
+ speed: number
7
7
  }
8
- export declare const calcAutoScrollBasicInfo: (point: IPoint, axis: 'x' | 'y', viewport: DOMRect, maxSpeed?: number) => IAutoScrollBasicInfo | null;
9
- export declare const updateScrollValue: (element: HTMLElement | Window, axis: 'x' | 'y', value: number, callback?: (scrollValue: number) => void) => void;
10
- export declare const scrollAnimate: (element: HTMLElement | Window, axis: 'x' | 'y', direction: 'begin' | 'end', speed: number, callback?: (scrollValue: number) => void) => () => void;
8
+ export declare const calcAutoScrollBasicInfo: (
9
+ point: IPoint,
10
+ axis: 'x' | 'y',
11
+ viewport: DOMRect,
12
+ maxSpeed?: number
13
+ ) => IAutoScrollBasicInfo | null
14
+ export declare const updateScrollValue: (
15
+ element: HTMLElement | Window,
16
+ axis: 'x' | 'y',
17
+ value: number,
18
+ callback?: (scrollValue: number) => void
19
+ ) => void
20
+ export declare const scrollAnimate: (
21
+ element: HTMLElement | Window,
22
+ axis: 'x' | 'y',
23
+ direction: 'begin' | 'end',
24
+ speed: number,
25
+ callback?: (scrollValue: number) => void
26
+ ) => () => void
package/esm/scroller.js CHANGED
@@ -1,82 +1,85 @@
1
- import { createUniformSpeedAnimation, calcSpeedFactor } from './animation';
2
- import { isFn, isWindow } from './types';
3
- const MAX_SPEED = 80; // px/s
4
- export const calcAutoScrollBasicInfo = (point, axis, viewport, maxSpeed = MAX_SPEED) => {
5
- const { left, right, top, bottom } = viewport;
6
- const { x, y } = point;
7
- let begin;
8
- let end;
9
- let pos;
10
- let speedFactor;
11
- if (axis === 'x') {
12
- begin = left;
13
- end = right;
14
- pos = x;
1
+ import { calcSpeedFactor, createUniformSpeedAnimation } from './animation'
2
+ import { isFn, isWindow } from './types'
3
+ const MAX_SPEED = 80 // px/s
4
+ export const calcAutoScrollBasicInfo = (
5
+ point,
6
+ axis,
7
+ viewport,
8
+ maxSpeed = MAX_SPEED
9
+ ) => {
10
+ const { left, right, top, bottom } = viewport
11
+ const { x, y } = point
12
+ let begin
13
+ let end
14
+ let pos
15
+ let speedFactor
16
+ if (axis === 'x') {
17
+ begin = left
18
+ end = right
19
+ pos = x
20
+ } else {
21
+ begin = top
22
+ end = bottom
23
+ pos = y
24
+ }
25
+ const scrollerSize = end - begin
26
+ const moveDistance = scrollerSize > 400 ? 100 : scrollerSize / 3
27
+ if (end - pos < moveDistance) {
28
+ return {
29
+ direction: 'end',
30
+ speedFactor,
31
+ speed: maxSpeed * calcSpeedFactor(end - pos, moveDistance),
15
32
  }
16
- else {
17
- begin = top;
18
- end = bottom;
19
- pos = y;
33
+ } else if (pos - begin < moveDistance) {
34
+ return {
35
+ direction: 'begin',
36
+ speedFactor,
37
+ speed: maxSpeed * calcSpeedFactor(pos - begin, moveDistance),
20
38
  }
21
- const scrollerSize = end - begin;
22
- const moveDistance = scrollerSize > 400 ? 100 : scrollerSize / 3;
23
- if (end - pos < moveDistance) {
24
- return {
25
- direction: 'end',
26
- speedFactor,
27
- speed: maxSpeed * calcSpeedFactor(end - pos, moveDistance),
28
- };
29
- }
30
- else if (pos - begin < moveDistance) {
31
- return {
32
- direction: 'begin',
33
- speedFactor,
34
- speed: maxSpeed * calcSpeedFactor(pos - begin, moveDistance),
35
- };
36
- }
37
- return null;
38
- };
39
+ }
40
+ return null
41
+ }
39
42
  export const updateScrollValue = (element, axis, value, callback) => {
40
- if (element) {
41
- if (!isWindow(element)) {
42
- if (axis === 'x') {
43
- if (element.scrollLeft + value > element.scrollWidth)
44
- return;
45
- element.scrollLeft += value;
46
- if (isFn(callback)) {
47
- callback(element.scrollLeft);
48
- }
49
- }
50
- else {
51
- if (element.scrollTop + value > element.scrollHeight)
52
- return;
53
- element.scrollTop += value;
54
- if (isFn(callback)) {
55
- callback(element.scrollTop);
56
- }
57
- }
43
+ if (element) {
44
+ if (!isWindow(element)) {
45
+ if (axis === 'x') {
46
+ if (element.scrollLeft + value > element.scrollWidth) return
47
+ element.scrollLeft += value
48
+ if (isFn(callback)) {
49
+ callback(element.scrollLeft)
58
50
  }
59
- else {
60
- if (axis === 'x') {
61
- element.scrollBy({
62
- left: value,
63
- behavior: 'smooth',
64
- });
65
- }
66
- else {
67
- element.scrollBy({
68
- top: value,
69
- behavior: 'smooth',
70
- });
71
- }
72
- if (isFn(callback)) {
73
- callback(value);
74
- }
51
+ } else {
52
+ if (element.scrollTop + value > element.scrollHeight) return
53
+ element.scrollTop += value
54
+ if (isFn(callback)) {
55
+ callback(element.scrollTop)
75
56
  }
57
+ }
58
+ } else {
59
+ if (axis === 'x') {
60
+ element.scrollBy({
61
+ left: value,
62
+ behavior: 'smooth',
63
+ })
64
+ } else {
65
+ element.scrollBy({
66
+ top: value,
67
+ behavior: 'smooth',
68
+ })
69
+ }
70
+ if (isFn(callback)) {
71
+ callback(value)
72
+ }
76
73
  }
77
- };
74
+ }
75
+ }
78
76
  export const scrollAnimate = (element, axis, direction, speed, callback) => {
79
- return createUniformSpeedAnimation(speed, (delta) => {
80
- updateScrollValue(element, axis, direction === 'begin' ? 0 - delta : delta, callback);
81
- });
82
- };
77
+ return createUniformSpeedAnimation(speed, (delta) => {
78
+ updateScrollValue(
79
+ element,
80
+ axis,
81
+ direction === 'begin' ? 0 - delta : delta,
82
+ callback
83
+ )
84
+ })
85
+ }
@@ -1,14 +1,14 @@
1
- declare const UNSUBSCRIBE_ID_SYMBOL: unique symbol;
1
+ declare const UNSUBSCRIBE_ID_SYMBOL: unique symbol
2
2
  export interface ISubscriber<Payload = any> {
3
- (payload: Payload): void | boolean;
3
+ (payload: Payload): void | boolean
4
4
  }
5
5
  export declare class Subscribable<ExtendsType = any> {
6
- private subscribers;
7
- dispatch<T extends ExtendsType = any>(event: T, context?: any): boolean;
8
- subscribe(subscriber: ISubscriber): {
9
- (): void;
10
- [UNSUBSCRIBE_ID_SYMBOL]: number;
11
- };
12
- unsubscribe: (id?: number | string | (() => void)) => void;
6
+ private subscribers
7
+ dispatch<T extends ExtendsType = any>(event: T, context?: any): boolean
8
+ subscribe(subscriber: ISubscriber): {
9
+ (): void
10
+ [UNSUBSCRIBE_ID_SYMBOL]: number
11
+ }
12
+ unsubscribe: (id?: number | string | (() => void)) => void
13
13
  }
14
- export {};
14
+ export {}
@@ -1,48 +1,47 @@
1
- import { isFn } from './types';
2
- const UNSUBSCRIBE_ID_SYMBOL = Symbol('UNSUBSCRIBE_ID_SYMBOL');
1
+ import { isFn } from './types'
2
+ const UNSUBSCRIBE_ID_SYMBOL = Symbol('UNSUBSCRIBE_ID_SYMBOL')
3
3
  export class Subscribable {
4
- constructor() {
5
- this.subscribers = {
6
- index: 0,
7
- };
8
- this.unsubscribe = (id) => {
9
- if (id === undefined || id === null) {
10
- for (const key in this.subscribers) {
11
- this.unsubscribe(key);
12
- }
13
- return;
14
- }
15
- if (!isFn(id)) {
16
- delete this.subscribers[id];
17
- }
18
- else {
19
- delete this.subscribers[id[UNSUBSCRIBE_ID_SYMBOL]];
20
- }
21
- };
4
+ constructor() {
5
+ this.subscribers = {
6
+ index: 0,
22
7
  }
23
- dispatch(event, context) {
24
- let interrupted = false;
8
+ this.unsubscribe = (id) => {
9
+ if (id === undefined || id === null) {
25
10
  for (const key in this.subscribers) {
26
- if (isFn(this.subscribers[key])) {
27
- event['context'] = context;
28
- if (this.subscribers[key](event) === false) {
29
- interrupted = true;
30
- }
31
- }
11
+ this.unsubscribe(key)
32
12
  }
33
- return interrupted ? false : true;
13
+ return
14
+ }
15
+ if (!isFn(id)) {
16
+ delete this.subscribers[id]
17
+ } else {
18
+ delete this.subscribers[id[UNSUBSCRIBE_ID_SYMBOL]]
19
+ }
34
20
  }
35
- subscribe(subscriber) {
36
- let id;
37
- if (isFn(subscriber)) {
38
- id = this.subscribers.index + 1;
39
- this.subscribers[id] = subscriber;
40
- this.subscribers.index++;
21
+ }
22
+ dispatch(event, context) {
23
+ let interrupted = false
24
+ for (const key in this.subscribers) {
25
+ if (isFn(this.subscribers[key])) {
26
+ event['context'] = context
27
+ if (this.subscribers[key](event) === false) {
28
+ interrupted = true
41
29
  }
42
- const unsubscribe = () => {
43
- this.unsubscribe(id);
44
- };
45
- unsubscribe[UNSUBSCRIBE_ID_SYMBOL] = id;
46
- return unsubscribe;
30
+ }
47
31
  }
32
+ return interrupted ? false : true
33
+ }
34
+ subscribe(subscriber) {
35
+ let id
36
+ if (isFn(subscriber)) {
37
+ id = this.subscribers.index + 1
38
+ this.subscribers[id] = subscriber
39
+ this.subscribers.index++
40
+ }
41
+ const unsubscribe = () => {
42
+ this.unsubscribe(id)
43
+ }
44
+ unsubscribe[UNSUBSCRIBE_ID_SYMBOL] = id
45
+ return unsubscribe
46
+ }
48
47
  }
package/esm/types.d.ts CHANGED
@@ -1,13 +1,13 @@
1
- export declare const getType: (obj: any) => any;
2
- export declare const isFn: (obj: unknown) => obj is (...args: any[]) => any;
3
- export declare const isWindow: (obj: unknown) => obj is Window;
4
- export declare const isHTMLElement: (obj: any) => obj is HTMLElement;
5
- export declare const isArr: (arg: any) => arg is any[];
6
- export declare const isPlainObj: (obj: unknown) => obj is object;
7
- export declare const isStr: (obj: unknown) => obj is string;
8
- export declare const isBool: (obj: unknown) => obj is boolean;
9
- export declare const isNum: (obj: unknown) => obj is number;
10
- export declare const isObj: (val: unknown) => val is object;
11
- export declare const isRegExp: (obj: unknown) => obj is RegExp;
12
- export declare const isValid: (val: any) => boolean;
13
- export declare const isValidNumber: (val: any) => val is number;
1
+ export declare const getType: (obj: any) => any
2
+ export declare const isFn: (obj: unknown) => obj is (...args: any[]) => any
3
+ export declare const isWindow: (obj: unknown) => obj is Window
4
+ export declare const isHTMLElement: (obj: any) => obj is HTMLElement
5
+ export declare const isArr: (arg: any) => arg is any[]
6
+ export declare const isPlainObj: (obj: unknown) => obj is object
7
+ export declare const isStr: (obj: unknown) => obj is string
8
+ export declare const isBool: (obj: unknown) => obj is boolean
9
+ export declare const isNum: (obj: unknown) => obj is number
10
+ export declare const isObj: (val: unknown) => val is object
11
+ export declare const isRegExp: (obj: unknown) => obj is RegExp
12
+ export declare const isValid: (val: any) => boolean
13
+ export declare const isValidNumber: (val: any) => val is number
package/esm/types.js CHANGED
@@ -1,21 +1,20 @@
1
- const isType = (type) => (obj) => obj != null &&
2
- (Array.isArray(type) ? type : [type]).some((t) => getType(obj) === `[object ${t}]`);
3
- export const getType = (obj) => Object.prototype.toString.call(obj);
4
- export const isFn = isType([
5
- 'Function',
6
- 'AsyncFunction',
7
- 'GeneratorFunction',
8
- ]);
9
- export const isWindow = isType('Window');
1
+ const isType = (type) => (obj) =>
2
+ obj != null &&
3
+ (Array.isArray(type) ? type : [type]).some(
4
+ (t) => getType(obj) === `[object ${t}]`
5
+ )
6
+ export const getType = (obj) => Object.prototype.toString.call(obj)
7
+ export const isFn = isType(['Function', 'AsyncFunction', 'GeneratorFunction'])
8
+ export const isWindow = isType('Window')
10
9
  export const isHTMLElement = (obj) => {
11
- return obj?.['nodeName'] || obj?.['tagName'];
12
- };
13
- export const isArr = Array.isArray;
14
- export const isPlainObj = isType('Object');
15
- export const isStr = isType('String');
16
- export const isBool = isType('Boolean');
17
- export const isNum = isType('Number');
18
- export const isObj = (val) => typeof val === 'object';
19
- export const isRegExp = isType('RegExp');
20
- export const isValid = (val) => val !== null && val !== undefined;
21
- export const isValidNumber = (val) => !isNaN(val) && isNum(val);
10
+ return obj?.['nodeName'] || obj?.['tagName']
11
+ }
12
+ export const isArr = Array.isArray
13
+ export const isPlainObj = isType('Object')
14
+ export const isStr = isType('String')
15
+ export const isBool = isType('Boolean')
16
+ export const isNum = isType('Number')
17
+ export const isObj = (val) => typeof val === 'object'
18
+ export const isRegExp = isType('RegExp')
19
+ export const isValid = (val) => val !== null && val !== undefined
20
+ export const isValidNumber = (val) => !isNaN(val) && isNum(val)
package/esm/uid.d.ts CHANGED
@@ -1 +1 @@
1
- export declare function uid(len?: number): string;
1
+ export declare function uid(len?: number): string
package/esm/uid.js CHANGED
@@ -1,9 +1,9 @@
1
- let IDX = 36, HEX = '';
2
- while (IDX--)
3
- HEX += IDX.toString(36);
1
+ let IDX = 36,
2
+ HEX = ''
3
+ while (IDX--) HEX += IDX.toString(36)
4
4
  export function uid(len) {
5
- let str = '', num = len || 11;
6
- while (num--)
7
- str += HEX[(Math.random() * 36) | 0];
8
- return str;
5
+ let str = '',
6
+ num = len || 11
7
+ while (num--) str += HEX[(Math.random() * 36) | 0]
8
+ return str
9
9
  }
@@ -1,2 +1,8 @@
1
- export declare const createUniformSpeedAnimation: (speed: number, callback: (delta: number) => void) => () => void;
2
- export declare const calcSpeedFactor: (delta?: number, threshold?: number) => number;
1
+ export declare const createUniformSpeedAnimation: (
2
+ speed: number,
3
+ callback: (delta: number) => void
4
+ ) => () => void
5
+ export declare const calcSpeedFactor: (
6
+ delta?: number,
7
+ threshold?: number
8
+ ) => number
package/lib/animation.js CHANGED
@@ -1,38 +1,37 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.calcSpeedFactor = exports.createUniformSpeedAnimation = void 0;
1
+ 'use strict'
2
+ Object.defineProperty(exports, '__esModule', { value: true })
3
+ exports.calcSpeedFactor = exports.createUniformSpeedAnimation = void 0
4
4
  const createUniformSpeedAnimation = (speed = 10, callback) => {
5
- let request = null;
6
- let startTime = null;
7
- const start = () => {
8
- if (request)
9
- return;
10
- request = requestAnimationFrame((timestamp) => {
11
- if (startTime === null) {
12
- startTime = timestamp;
13
- }
14
- const deltaTime = timestamp - startTime;
15
- const delta = (deltaTime / 1000) * speed;
16
- callback(delta);
17
- request = null;
18
- start();
19
- });
20
- };
21
- start();
22
- return () => {
23
- if (request) {
24
- cancelAnimationFrame(request);
25
- request = null;
26
- }
27
- startTime = null;
28
- };
29
- };
30
- exports.createUniformSpeedAnimation = createUniformSpeedAnimation;
5
+ let request = null
6
+ let startTime = null
7
+ const start = () => {
8
+ if (request) return
9
+ request = requestAnimationFrame((timestamp) => {
10
+ if (startTime === null) {
11
+ startTime = timestamp
12
+ }
13
+ const deltaTime = timestamp - startTime
14
+ const delta = (deltaTime / 1000) * speed
15
+ callback(delta)
16
+ request = null
17
+ start()
18
+ })
19
+ }
20
+ start()
21
+ return () => {
22
+ if (request) {
23
+ cancelAnimationFrame(request)
24
+ request = null
25
+ }
26
+ startTime = null
27
+ }
28
+ }
29
+ exports.createUniformSpeedAnimation = createUniformSpeedAnimation
31
30
  //越接近阈值,速度越小,越远离阈值,速度越大
32
31
  const calcSpeedFactor = (delta = 0, threshold = Infinity) => {
33
- if (threshold >= delta) {
34
- return (threshold - delta) / threshold;
35
- }
36
- return 0;
37
- };
38
- exports.calcSpeedFactor = calcSpeedFactor;
32
+ if (threshold >= delta) {
33
+ return (threshold - delta) / threshold
34
+ }
35
+ return 0
36
+ }
37
+ exports.calcSpeedFactor = calcSpeedFactor