@bouzu/shared 0.0.2 → 0.1.1

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