@bouzu/shared 0.0.2 → 0.1.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/dist/index.cjs CHANGED
@@ -46,6 +46,23 @@ function checkPointOrigin(p) {
46
46
  function checkPointEqual(a, b) {
47
47
  return a.x === b.x && a.y === b.y;
48
48
  }
49
+ function checkPointEqualWithTolerance(a, b, tolerance = 0.01) {
50
+ return Math.abs(a.x - b.x) < tolerance && Math.abs(a.y - b.y) < tolerance;
51
+ }
52
+ function getPointCenter(p1, p2) {
53
+ return createPoint(
54
+ (p1.x + p2.x) / 2,
55
+ (p1.y + p2.y) / 2
56
+ );
57
+ }
58
+ function getPointDistance(p1, p2) {
59
+ const dx = p1.x - p2.x;
60
+ const dy = p1.y - p2.y;
61
+ return Math.sqrt(dx * dx + dy * dy);
62
+ }
63
+ function clonePoint(a) {
64
+ return createPoint(a.x, a.y);
65
+ }
49
66
 
50
67
  function createSize(width = 0, height = 0) {
51
68
  return { width, height };
@@ -150,9 +167,144 @@ function isObject(val) {
150
167
  return Object.prototype.toString.call(val) === "[object Object]";
151
168
  }
152
169
 
170
+ const Axis = {
171
+ X: "x",
172
+ Y: "y"
173
+ };
174
+ function checkAxis(currVisibleRect, prevVisibleRect) {
175
+ if (Math.abs(currVisibleRect.x - prevVisibleRect.x) > 0)
176
+ return Axis.X;
177
+ if (Math.abs(currVisibleRect.y - prevVisibleRect.y) > 0)
178
+ return Axis.Y;
179
+ }
180
+ function reverseAxis(axis) {
181
+ switch (axis) {
182
+ case Axis.X:
183
+ return Axis.Y;
184
+ case Axis.Y:
185
+ return Axis.X;
186
+ }
187
+ }
188
+ function getSizeByAxis(size, axis, reverse = false) {
189
+ switch (reverse ? reverseAxis(axis) : axis) {
190
+ case Axis.X:
191
+ return size.width;
192
+ case Axis.Y:
193
+ return size.height;
194
+ }
195
+ }
196
+ function getPointByAxis(point, axis, reverse = false) {
197
+ return point[reverse ? reverseAxis(axis) : axis];
198
+ }
199
+ function updateSizeByAxis(size, axis, value, reverse = false) {
200
+ switch (reverse ? reverseAxis(axis) : axis) {
201
+ case Axis.X:
202
+ size.width = value;
203
+ break;
204
+ case Axis.Y:
205
+ size.height = value;
206
+ break;
207
+ }
208
+ }
209
+ function updatePointByAxis(point, axis, value, reverse = false) {
210
+ point[reverse ? reverseAxis(axis) : axis] = value;
211
+ }
212
+ function getRectMaxByAxis(rect, axis, reverse = false) {
213
+ switch (reverse ? reverseAxis(axis) : axis) {
214
+ case Axis.X:
215
+ return getRectMaxX(rect);
216
+ case Axis.Y:
217
+ return getRectMaxY(rect);
218
+ }
219
+ }
220
+ function checkRectIntersectsByAxis(a, b, axis, reverse = false) {
221
+ switch (reverse ? reverseAxis(axis) : axis) {
222
+ case Axis.X:
223
+ return checkRectIntersectsX(a, b);
224
+ case Axis.Y:
225
+ return checkRectIntersectsY(a, b);
226
+ }
227
+ }
228
+
229
+ function registerRaf(handler, methods) {
230
+ const raf = methods?.raf ?? window.requestAnimationFrame;
231
+ const caf = methods?.caf ?? window.cancelAnimationFrame;
232
+ let handleId = raf(handler);
233
+ return () => {
234
+ if (handleId == null)
235
+ return;
236
+ caf(handleId);
237
+ handleId = null;
238
+ };
239
+ }
240
+
241
+ const perf = window?.performance ?? null;
242
+ const perfNowFn = perf?.now ?? perf?.webkitNow ?? perf?.msNow ?? perf?.mozNow;
243
+ const getTime = perfNowFn ? perfNowFn.bind(perf) : Date.now ?? (() => (/* @__PURE__ */ new Date()).getTime());
244
+
245
+ function clamp(value, min, max) {
246
+ return Math.min(Math.max(value, min), max);
247
+ }
248
+
249
+ function runTransition(options) {
250
+ const {
251
+ start,
252
+ end,
253
+ onUpdate,
254
+ onStarted,
255
+ onFinished,
256
+ raf = window.requestAnimationFrame,
257
+ caf = window.cancelAnimationFrame,
258
+ duration = 1e3,
259
+ // eslint-disable-next-line ts/no-use-before-define
260
+ easing = easeLinear,
261
+ getTime: getTime$1 = getTime
262
+ } = options;
263
+ const rafMethos = { raf, caf };
264
+ let _cancelRaf = noop;
265
+ let _canceled = false;
266
+ const promise = new Promise((resolve) => {
267
+ const startAt = getTime$1();
268
+ onStarted?.();
269
+ const tick = (currentTime) => {
270
+ if (_canceled) {
271
+ resolve();
272
+ return;
273
+ }
274
+ const elapsed = currentTime - startAt;
275
+ const progress = Math.min(elapsed / duration, 1);
276
+ const value = start + (end - start) * easing(progress);
277
+ onUpdate(value);
278
+ if (progress < 1) {
279
+ _cancelRaf = registerRaf(tick, rafMethos);
280
+ } else {
281
+ onFinished?.();
282
+ resolve();
283
+ }
284
+ };
285
+ _cancelRaf = registerRaf(tick, rafMethos);
286
+ });
287
+ promise.cancel = () => {
288
+ _canceled = true;
289
+ _cancelRaf();
290
+ };
291
+ return promise;
292
+ }
293
+ function runNoopTransition() {
294
+ const promise = new Promise((resolve) => resolve());
295
+ promise.cancel = noop;
296
+ return promise;
297
+ }
298
+ const easeLinear = (t) => t;
299
+ const easeOut = (t) => Math.sin(t * Math.PI / 2);
300
+ const easeOutCubic = (t) => 1 - (1 - t) ** 3;
301
+
302
+ exports.Axis = Axis;
153
303
  exports.RectCorner = RectCorner;
304
+ exports.checkAxis = checkAxis;
154
305
  exports.checkLayoutInvalidate = checkLayoutInvalidate;
155
306
  exports.checkPointEqual = checkPointEqual;
307
+ exports.checkPointEqualWithTolerance = checkPointEqualWithTolerance;
156
308
  exports.checkPointOrigin = checkPointOrigin;
157
309
  exports.checkRectContains = checkRectContains;
158
310
  exports.checkRectContainsPoint = checkRectContainsPoint;
@@ -160,25 +312,43 @@ exports.checkRectEqual = checkRectEqual;
160
312
  exports.checkRectEqualPoint = checkRectEqualPoint;
161
313
  exports.checkRectEqualSize = checkRectEqualSize;
162
314
  exports.checkRectIntersects = checkRectIntersects;
315
+ exports.checkRectIntersectsByAxis = checkRectIntersectsByAxis;
163
316
  exports.checkRectIntersectsX = checkRectIntersectsX;
164
317
  exports.checkRectIntersectsY = checkRectIntersectsY;
165
318
  exports.checkSizeEqual = checkSizeEqual;
319
+ exports.clamp = clamp;
320
+ exports.clonePoint = clonePoint;
166
321
  exports.cloneRect = cloneRect;
167
322
  exports.createPoint = createPoint;
168
323
  exports.createRect = createRect;
169
324
  exports.createSize = createSize;
325
+ exports.easeLinear = easeLinear;
326
+ exports.easeOut = easeOut;
327
+ exports.easeOutCubic = easeOutCubic;
170
328
  exports.execLastTick = execLastTick;
329
+ exports.getPointByAxis = getPointByAxis;
330
+ exports.getPointCenter = getPointCenter;
331
+ exports.getPointDistance = getPointDistance;
171
332
  exports.getRectArea = getRectArea;
172
333
  exports.getRectBottomLeft = getRectBottomLeft;
173
334
  exports.getRectBottomRight = getRectBottomRight;
174
335
  exports.getRectCornerInOther = getRectCornerInOther;
336
+ exports.getRectMaxByAxis = getRectMaxByAxis;
175
337
  exports.getRectMaxX = getRectMaxX;
176
338
  exports.getRectMaxY = getRectMaxY;
177
339
  exports.getRectPointByRectCorner = getRectPointByRectCorner;
178
340
  exports.getRectTopLeft = getRectTopLeft;
179
341
  exports.getRectTopRight = getRectTopRight;
342
+ exports.getSizeByAxis = getSizeByAxis;
343
+ exports.getTime = getTime;
180
344
  exports.isObject = isObject;
181
345
  exports.noop = noop;
346
+ exports.registerRaf = registerRaf;
347
+ exports.reverseAxis = reverseAxis;
348
+ exports.runNoopTransition = runNoopTransition;
349
+ exports.runTransition = runTransition;
182
350
  exports.toPoint = toPoint;
183
351
  exports.toSize = toSize;
352
+ exports.updatePointByAxis = updatePointByAxis;
353
+ exports.updateSizeByAxis = updateSizeByAxis;
184
354
  exports.usePrevious = usePrevious;
package/dist/index.d.cts CHANGED
@@ -21,6 +21,10 @@ interface Point {
21
21
  declare function createPoint(x?: number, y?: number): Point;
22
22
  declare function checkPointOrigin(p: Point): boolean;
23
23
  declare function checkPointEqual(a: Point, b: Point): boolean;
24
+ declare function checkPointEqualWithTolerance(a: Point, b: Point, tolerance?: number): boolean;
25
+ declare function getPointCenter(p1: Point, p2: Point): Point;
26
+ declare function getPointDistance(p1: Point, p2: Point): number;
27
+ declare function clonePoint(a: Point): Point;
24
28
 
25
29
  interface Size {
26
30
  width: number;
@@ -62,4 +66,58 @@ declare function toPoint(rect: Rect): Point;
62
66
 
63
67
  declare function isObject(val: unknown): val is Record<PropertyKey, any>;
64
68
 
65
- export { type Point, type Previous, type Rect, RectCorner, type RectCornerValue, type Size, checkLayoutInvalidate, checkPointEqual, checkPointOrigin, checkRectContains, checkRectContainsPoint, checkRectEqual, checkRectEqualPoint, checkRectEqualSize, checkRectIntersects, checkRectIntersectsX, checkRectIntersectsY, checkSizeEqual, cloneRect, createPoint, createRect, createSize, execLastTick, getRectArea, getRectBottomLeft, getRectBottomRight, getRectCornerInOther, getRectMaxX, getRectMaxY, getRectPointByRectCorner, getRectTopLeft, getRectTopRight, isObject, noop, toPoint, toSize, usePrevious };
69
+ declare const Axis: {
70
+ readonly X: "x";
71
+ readonly Y: "y";
72
+ };
73
+ type AxisValue = ValueOf<typeof Axis>;
74
+ declare function checkAxis(currVisibleRect: Rect, prevVisibleRect: Rect): AxisValue | undefined;
75
+ declare function reverseAxis(axis: AxisValue): AxisValue;
76
+ declare function getSizeByAxis(size: Size, axis: AxisValue, reverse?: boolean): number;
77
+ declare function getPointByAxis(point: Point, axis: AxisValue, reverse?: boolean): number;
78
+ declare function updateSizeByAxis(size: Size, axis: AxisValue, value: number, reverse?: boolean): void;
79
+ declare function updatePointByAxis(point: Point, axis: AxisValue, value: number, reverse?: boolean): void;
80
+ declare function getRectMaxByAxis(rect: Rect, axis: AxisValue, reverse?: boolean): number;
81
+ declare function checkRectIntersectsByAxis(a: Rect, b: Rect, axis: AxisValue, reverse?: boolean): boolean;
82
+
83
+ interface RegisterRafMethods {
84
+ /**
85
+ * requestAnimationFrame
86
+ */
87
+ raf: AnimationFrameProvider['requestAnimationFrame'];
88
+ /**
89
+ * cancelAnimationFrame
90
+ */
91
+ caf: AnimationFrameProvider['cancelAnimationFrame'];
92
+ }
93
+ type CancelRaf = () => void;
94
+ declare function registerRaf(handler: (time: number) => void, methods?: RegisterRafMethods): CancelRaf;
95
+
96
+ type GetTimeFn = () => number;
97
+ declare const getTime: GetTimeFn;
98
+
99
+ declare function clamp(value: number, min: number, max: number): number;
100
+
101
+ interface RunTransitionOptions {
102
+ start: number;
103
+ end: number;
104
+ onUpdate: (value: number) => void | boolean;
105
+ easing?: EaseFn;
106
+ duration?: number;
107
+ onStarted?: () => void;
108
+ onFinished?: () => void;
109
+ getTime?: GetTimeFn;
110
+ raf?: RegisterRafMethods['raf'];
111
+ caf?: RegisterRafMethods['caf'];
112
+ }
113
+ type TransitionRunner = Promise<void> & {
114
+ cancel: () => void;
115
+ };
116
+ declare function runTransition(options: RunTransitionOptions): TransitionRunner;
117
+ declare function runNoopTransition(): TransitionRunner;
118
+ type EaseFn = (t: number) => number;
119
+ declare const easeLinear: EaseFn;
120
+ declare const easeOut: EaseFn;
121
+ declare const easeOutCubic: EaseFn;
122
+
123
+ export { Axis, type AxisValue, type CancelRaf, type EaseFn, type GetTimeFn, type Point, type Previous, type Rect, RectCorner, type RectCornerValue, type RegisterRafMethods, type RunTransitionOptions, type Size, type TransitionRunner, checkAxis, checkLayoutInvalidate, checkPointEqual, checkPointEqualWithTolerance, checkPointOrigin, checkRectContains, checkRectContainsPoint, checkRectEqual, checkRectEqualPoint, checkRectEqualSize, checkRectIntersects, checkRectIntersectsByAxis, checkRectIntersectsX, checkRectIntersectsY, checkSizeEqual, clamp, clonePoint, cloneRect, createPoint, createRect, createSize, easeLinear, easeOut, easeOutCubic, execLastTick, getPointByAxis, getPointCenter, getPointDistance, getRectArea, getRectBottomLeft, getRectBottomRight, getRectCornerInOther, getRectMaxByAxis, getRectMaxX, getRectMaxY, getRectPointByRectCorner, getRectTopLeft, getRectTopRight, getSizeByAxis, getTime, isObject, noop, registerRaf, reverseAxis, runNoopTransition, runTransition, toPoint, toSize, updatePointByAxis, updateSizeByAxis, usePrevious };
package/dist/index.d.mts CHANGED
@@ -21,6 +21,10 @@ interface Point {
21
21
  declare function createPoint(x?: number, y?: number): Point;
22
22
  declare function checkPointOrigin(p: Point): boolean;
23
23
  declare function checkPointEqual(a: Point, b: Point): boolean;
24
+ declare function checkPointEqualWithTolerance(a: Point, b: Point, tolerance?: number): boolean;
25
+ declare function getPointCenter(p1: Point, p2: Point): Point;
26
+ declare function getPointDistance(p1: Point, p2: Point): number;
27
+ declare function clonePoint(a: Point): Point;
24
28
 
25
29
  interface Size {
26
30
  width: number;
@@ -62,4 +66,58 @@ declare function toPoint(rect: Rect): Point;
62
66
 
63
67
  declare function isObject(val: unknown): val is Record<PropertyKey, any>;
64
68
 
65
- export { type Point, type Previous, type Rect, RectCorner, type RectCornerValue, type Size, checkLayoutInvalidate, checkPointEqual, checkPointOrigin, checkRectContains, checkRectContainsPoint, checkRectEqual, checkRectEqualPoint, checkRectEqualSize, checkRectIntersects, checkRectIntersectsX, checkRectIntersectsY, checkSizeEqual, cloneRect, createPoint, createRect, createSize, execLastTick, getRectArea, getRectBottomLeft, getRectBottomRight, getRectCornerInOther, getRectMaxX, getRectMaxY, getRectPointByRectCorner, getRectTopLeft, getRectTopRight, isObject, noop, toPoint, toSize, usePrevious };
69
+ declare const Axis: {
70
+ readonly X: "x";
71
+ readonly Y: "y";
72
+ };
73
+ type AxisValue = ValueOf<typeof Axis>;
74
+ declare function checkAxis(currVisibleRect: Rect, prevVisibleRect: Rect): AxisValue | undefined;
75
+ declare function reverseAxis(axis: AxisValue): AxisValue;
76
+ declare function getSizeByAxis(size: Size, axis: AxisValue, reverse?: boolean): number;
77
+ declare function getPointByAxis(point: Point, axis: AxisValue, reverse?: boolean): number;
78
+ declare function updateSizeByAxis(size: Size, axis: AxisValue, value: number, reverse?: boolean): void;
79
+ declare function updatePointByAxis(point: Point, axis: AxisValue, value: number, reverse?: boolean): void;
80
+ declare function getRectMaxByAxis(rect: Rect, axis: AxisValue, reverse?: boolean): number;
81
+ declare function checkRectIntersectsByAxis(a: Rect, b: Rect, axis: AxisValue, reverse?: boolean): boolean;
82
+
83
+ interface RegisterRafMethods {
84
+ /**
85
+ * requestAnimationFrame
86
+ */
87
+ raf: AnimationFrameProvider['requestAnimationFrame'];
88
+ /**
89
+ * cancelAnimationFrame
90
+ */
91
+ caf: AnimationFrameProvider['cancelAnimationFrame'];
92
+ }
93
+ type CancelRaf = () => void;
94
+ declare function registerRaf(handler: (time: number) => void, methods?: RegisterRafMethods): CancelRaf;
95
+
96
+ type GetTimeFn = () => number;
97
+ declare const getTime: GetTimeFn;
98
+
99
+ declare function clamp(value: number, min: number, max: number): number;
100
+
101
+ interface RunTransitionOptions {
102
+ start: number;
103
+ end: number;
104
+ onUpdate: (value: number) => void | boolean;
105
+ easing?: EaseFn;
106
+ duration?: number;
107
+ onStarted?: () => void;
108
+ onFinished?: () => void;
109
+ getTime?: GetTimeFn;
110
+ raf?: RegisterRafMethods['raf'];
111
+ caf?: RegisterRafMethods['caf'];
112
+ }
113
+ type TransitionRunner = Promise<void> & {
114
+ cancel: () => void;
115
+ };
116
+ declare function runTransition(options: RunTransitionOptions): TransitionRunner;
117
+ declare function runNoopTransition(): TransitionRunner;
118
+ type EaseFn = (t: number) => number;
119
+ declare const easeLinear: EaseFn;
120
+ declare const easeOut: EaseFn;
121
+ declare const easeOutCubic: EaseFn;
122
+
123
+ export { Axis, type AxisValue, type CancelRaf, type EaseFn, type GetTimeFn, type Point, type Previous, type Rect, RectCorner, type RectCornerValue, type RegisterRafMethods, type RunTransitionOptions, type Size, type TransitionRunner, checkAxis, checkLayoutInvalidate, checkPointEqual, checkPointEqualWithTolerance, checkPointOrigin, checkRectContains, checkRectContainsPoint, checkRectEqual, checkRectEqualPoint, checkRectEqualSize, checkRectIntersects, checkRectIntersectsByAxis, checkRectIntersectsX, checkRectIntersectsY, checkSizeEqual, clamp, clonePoint, cloneRect, createPoint, createRect, createSize, easeLinear, easeOut, easeOutCubic, execLastTick, getPointByAxis, getPointCenter, getPointDistance, getRectArea, getRectBottomLeft, getRectBottomRight, getRectCornerInOther, getRectMaxByAxis, getRectMaxX, getRectMaxY, getRectPointByRectCorner, getRectTopLeft, getRectTopRight, getSizeByAxis, getTime, isObject, noop, registerRaf, reverseAxis, runNoopTransition, runTransition, toPoint, toSize, updatePointByAxis, updateSizeByAxis, usePrevious };
package/dist/index.d.ts CHANGED
@@ -21,6 +21,10 @@ interface Point {
21
21
  declare function createPoint(x?: number, y?: number): Point;
22
22
  declare function checkPointOrigin(p: Point): boolean;
23
23
  declare function checkPointEqual(a: Point, b: Point): boolean;
24
+ declare function checkPointEqualWithTolerance(a: Point, b: Point, tolerance?: number): boolean;
25
+ declare function getPointCenter(p1: Point, p2: Point): Point;
26
+ declare function getPointDistance(p1: Point, p2: Point): number;
27
+ declare function clonePoint(a: Point): Point;
24
28
 
25
29
  interface Size {
26
30
  width: number;
@@ -62,4 +66,58 @@ declare function toPoint(rect: Rect): Point;
62
66
 
63
67
  declare function isObject(val: unknown): val is Record<PropertyKey, any>;
64
68
 
65
- export { type Point, type Previous, type Rect, RectCorner, type RectCornerValue, type Size, checkLayoutInvalidate, checkPointEqual, checkPointOrigin, checkRectContains, checkRectContainsPoint, checkRectEqual, checkRectEqualPoint, checkRectEqualSize, checkRectIntersects, checkRectIntersectsX, checkRectIntersectsY, checkSizeEqual, cloneRect, createPoint, createRect, createSize, execLastTick, getRectArea, getRectBottomLeft, getRectBottomRight, getRectCornerInOther, getRectMaxX, getRectMaxY, getRectPointByRectCorner, getRectTopLeft, getRectTopRight, isObject, noop, toPoint, toSize, usePrevious };
69
+ declare const Axis: {
70
+ readonly X: "x";
71
+ readonly Y: "y";
72
+ };
73
+ type AxisValue = ValueOf<typeof Axis>;
74
+ declare function checkAxis(currVisibleRect: Rect, prevVisibleRect: Rect): AxisValue | undefined;
75
+ declare function reverseAxis(axis: AxisValue): AxisValue;
76
+ declare function getSizeByAxis(size: Size, axis: AxisValue, reverse?: boolean): number;
77
+ declare function getPointByAxis(point: Point, axis: AxisValue, reverse?: boolean): number;
78
+ declare function updateSizeByAxis(size: Size, axis: AxisValue, value: number, reverse?: boolean): void;
79
+ declare function updatePointByAxis(point: Point, axis: AxisValue, value: number, reverse?: boolean): void;
80
+ declare function getRectMaxByAxis(rect: Rect, axis: AxisValue, reverse?: boolean): number;
81
+ declare function checkRectIntersectsByAxis(a: Rect, b: Rect, axis: AxisValue, reverse?: boolean): boolean;
82
+
83
+ interface RegisterRafMethods {
84
+ /**
85
+ * requestAnimationFrame
86
+ */
87
+ raf: AnimationFrameProvider['requestAnimationFrame'];
88
+ /**
89
+ * cancelAnimationFrame
90
+ */
91
+ caf: AnimationFrameProvider['cancelAnimationFrame'];
92
+ }
93
+ type CancelRaf = () => void;
94
+ declare function registerRaf(handler: (time: number) => void, methods?: RegisterRafMethods): CancelRaf;
95
+
96
+ type GetTimeFn = () => number;
97
+ declare const getTime: GetTimeFn;
98
+
99
+ declare function clamp(value: number, min: number, max: number): number;
100
+
101
+ interface RunTransitionOptions {
102
+ start: number;
103
+ end: number;
104
+ onUpdate: (value: number) => void | boolean;
105
+ easing?: EaseFn;
106
+ duration?: number;
107
+ onStarted?: () => void;
108
+ onFinished?: () => void;
109
+ getTime?: GetTimeFn;
110
+ raf?: RegisterRafMethods['raf'];
111
+ caf?: RegisterRafMethods['caf'];
112
+ }
113
+ type TransitionRunner = Promise<void> & {
114
+ cancel: () => void;
115
+ };
116
+ declare function runTransition(options: RunTransitionOptions): TransitionRunner;
117
+ declare function runNoopTransition(): TransitionRunner;
118
+ type EaseFn = (t: number) => number;
119
+ declare const easeLinear: EaseFn;
120
+ declare const easeOut: EaseFn;
121
+ declare const easeOutCubic: EaseFn;
122
+
123
+ export { Axis, type AxisValue, type CancelRaf, type EaseFn, type GetTimeFn, type Point, type Previous, type Rect, RectCorner, type RectCornerValue, type RegisterRafMethods, type RunTransitionOptions, type Size, type TransitionRunner, checkAxis, checkLayoutInvalidate, checkPointEqual, checkPointEqualWithTolerance, checkPointOrigin, checkRectContains, checkRectContainsPoint, checkRectEqual, checkRectEqualPoint, checkRectEqualSize, checkRectIntersects, checkRectIntersectsByAxis, checkRectIntersectsX, checkRectIntersectsY, checkSizeEqual, clamp, clonePoint, cloneRect, createPoint, createRect, createSize, easeLinear, easeOut, easeOutCubic, execLastTick, getPointByAxis, getPointCenter, getPointDistance, getRectArea, getRectBottomLeft, getRectBottomRight, getRectCornerInOther, getRectMaxByAxis, getRectMaxX, getRectMaxY, getRectPointByRectCorner, getRectTopLeft, getRectTopRight, getSizeByAxis, getTime, isObject, noop, registerRaf, reverseAxis, runNoopTransition, runTransition, toPoint, toSize, updatePointByAxis, updateSizeByAxis, usePrevious };
package/dist/index.mjs CHANGED
@@ -44,6 +44,23 @@ function checkPointOrigin(p) {
44
44
  function checkPointEqual(a, b) {
45
45
  return a.x === b.x && a.y === b.y;
46
46
  }
47
+ function checkPointEqualWithTolerance(a, b, tolerance = 0.01) {
48
+ return Math.abs(a.x - b.x) < tolerance && Math.abs(a.y - b.y) < tolerance;
49
+ }
50
+ function getPointCenter(p1, p2) {
51
+ return createPoint(
52
+ (p1.x + p2.x) / 2,
53
+ (p1.y + p2.y) / 2
54
+ );
55
+ }
56
+ function getPointDistance(p1, p2) {
57
+ const dx = p1.x - p2.x;
58
+ const dy = p1.y - p2.y;
59
+ return Math.sqrt(dx * dx + dy * dy);
60
+ }
61
+ function clonePoint(a) {
62
+ return createPoint(a.x, a.y);
63
+ }
47
64
 
48
65
  function createSize(width = 0, height = 0) {
49
66
  return { width, height };
@@ -148,4 +165,136 @@ function isObject(val) {
148
165
  return Object.prototype.toString.call(val) === "[object Object]";
149
166
  }
150
167
 
151
- export { RectCorner, checkLayoutInvalidate, checkPointEqual, checkPointOrigin, checkRectContains, checkRectContainsPoint, checkRectEqual, checkRectEqualPoint, checkRectEqualSize, checkRectIntersects, checkRectIntersectsX, checkRectIntersectsY, checkSizeEqual, cloneRect, createPoint, createRect, createSize, execLastTick, getRectArea, getRectBottomLeft, getRectBottomRight, getRectCornerInOther, getRectMaxX, getRectMaxY, getRectPointByRectCorner, getRectTopLeft, getRectTopRight, isObject, noop, toPoint, toSize, usePrevious };
168
+ const Axis = {
169
+ X: "x",
170
+ Y: "y"
171
+ };
172
+ function checkAxis(currVisibleRect, prevVisibleRect) {
173
+ if (Math.abs(currVisibleRect.x - prevVisibleRect.x) > 0)
174
+ return Axis.X;
175
+ if (Math.abs(currVisibleRect.y - prevVisibleRect.y) > 0)
176
+ return Axis.Y;
177
+ }
178
+ function reverseAxis(axis) {
179
+ switch (axis) {
180
+ case Axis.X:
181
+ return Axis.Y;
182
+ case Axis.Y:
183
+ return Axis.X;
184
+ }
185
+ }
186
+ function getSizeByAxis(size, axis, reverse = false) {
187
+ switch (reverse ? reverseAxis(axis) : axis) {
188
+ case Axis.X:
189
+ return size.width;
190
+ case Axis.Y:
191
+ return size.height;
192
+ }
193
+ }
194
+ function getPointByAxis(point, axis, reverse = false) {
195
+ return point[reverse ? reverseAxis(axis) : axis];
196
+ }
197
+ function updateSizeByAxis(size, axis, value, reverse = false) {
198
+ switch (reverse ? reverseAxis(axis) : axis) {
199
+ case Axis.X:
200
+ size.width = value;
201
+ break;
202
+ case Axis.Y:
203
+ size.height = value;
204
+ break;
205
+ }
206
+ }
207
+ function updatePointByAxis(point, axis, value, reverse = false) {
208
+ point[reverse ? reverseAxis(axis) : axis] = value;
209
+ }
210
+ function getRectMaxByAxis(rect, axis, reverse = false) {
211
+ switch (reverse ? reverseAxis(axis) : axis) {
212
+ case Axis.X:
213
+ return getRectMaxX(rect);
214
+ case Axis.Y:
215
+ return getRectMaxY(rect);
216
+ }
217
+ }
218
+ function checkRectIntersectsByAxis(a, b, axis, reverse = false) {
219
+ switch (reverse ? reverseAxis(axis) : axis) {
220
+ case Axis.X:
221
+ return checkRectIntersectsX(a, b);
222
+ case Axis.Y:
223
+ return checkRectIntersectsY(a, b);
224
+ }
225
+ }
226
+
227
+ function registerRaf(handler, methods) {
228
+ const raf = methods?.raf ?? window.requestAnimationFrame;
229
+ const caf = methods?.caf ?? window.cancelAnimationFrame;
230
+ let handleId = raf(handler);
231
+ return () => {
232
+ if (handleId == null)
233
+ return;
234
+ caf(handleId);
235
+ handleId = null;
236
+ };
237
+ }
238
+
239
+ const perf = window?.performance ?? null;
240
+ const perfNowFn = perf?.now ?? perf?.webkitNow ?? perf?.msNow ?? perf?.mozNow;
241
+ const getTime = perfNowFn ? perfNowFn.bind(perf) : Date.now ?? (() => (/* @__PURE__ */ new Date()).getTime());
242
+
243
+ function clamp(value, min, max) {
244
+ return Math.min(Math.max(value, min), max);
245
+ }
246
+
247
+ function runTransition(options) {
248
+ const {
249
+ start,
250
+ end,
251
+ onUpdate,
252
+ onStarted,
253
+ onFinished,
254
+ raf = window.requestAnimationFrame,
255
+ caf = window.cancelAnimationFrame,
256
+ duration = 1e3,
257
+ // eslint-disable-next-line ts/no-use-before-define
258
+ easing = easeLinear,
259
+ getTime: getTime$1 = getTime
260
+ } = options;
261
+ const rafMethos = { raf, caf };
262
+ let _cancelRaf = noop;
263
+ let _canceled = false;
264
+ const promise = new Promise((resolve) => {
265
+ const startAt = getTime$1();
266
+ onStarted?.();
267
+ const tick = (currentTime) => {
268
+ if (_canceled) {
269
+ resolve();
270
+ return;
271
+ }
272
+ const elapsed = currentTime - startAt;
273
+ const progress = Math.min(elapsed / duration, 1);
274
+ const value = start + (end - start) * easing(progress);
275
+ onUpdate(value);
276
+ if (progress < 1) {
277
+ _cancelRaf = registerRaf(tick, rafMethos);
278
+ } else {
279
+ onFinished?.();
280
+ resolve();
281
+ }
282
+ };
283
+ _cancelRaf = registerRaf(tick, rafMethos);
284
+ });
285
+ promise.cancel = () => {
286
+ _canceled = true;
287
+ _cancelRaf();
288
+ };
289
+ return promise;
290
+ }
291
+ function runNoopTransition() {
292
+ const promise = new Promise((resolve) => resolve());
293
+ promise.cancel = noop;
294
+ return promise;
295
+ }
296
+ const easeLinear = (t) => t;
297
+ const easeOut = (t) => Math.sin(t * Math.PI / 2);
298
+ const easeOutCubic = (t) => 1 - (1 - t) ** 3;
299
+
300
+ export { Axis, RectCorner, checkAxis, checkLayoutInvalidate, checkPointEqual, checkPointEqualWithTolerance, checkPointOrigin, checkRectContains, checkRectContainsPoint, checkRectEqual, checkRectEqualPoint, checkRectEqualSize, checkRectIntersects, checkRectIntersectsByAxis, checkRectIntersectsX, checkRectIntersectsY, checkSizeEqual, clamp, clonePoint, cloneRect, createPoint, createRect, createSize, easeLinear, easeOut, easeOutCubic, execLastTick, getPointByAxis, getPointCenter, getPointDistance, getRectArea, getRectBottomLeft, getRectBottomRight, getRectCornerInOther, getRectMaxByAxis, getRectMaxX, getRectMaxY, getRectPointByRectCorner, getRectTopLeft, getRectTopRight, getSizeByAxis, getTime, isObject, noop, registerRaf, reverseAxis, runNoopTransition, runTransition, toPoint, toSize, updatePointByAxis, updateSizeByAxis, usePrevious };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bouzu/shared",
3
- "version": "0.0.2",
3
+ "version": "0.1.0",
4
4
  "private": false,
5
5
  "author": "zhong666 <hi@zhong666.me>",
6
6
  "license": "MIT",