@bouzu/shared 0.0.1 → 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/LICENSE +21 -0
- package/README.md +57 -0
- package/dist/index.cjs +185 -11
- package/dist/index.d.cts +123 -0
- package/dist/index.d.mts +123 -0
- package/dist/index.d.ts +72 -9
- package/dist/index.mjs +164 -12
- package/package.json +20 -4
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) zhong666 <hi@zhong666.me> and bouzu contributors
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
# @bouzu/shared
|
|
2
|
+
|
|
3
|
+
[![npm version][npm-version-src]][npm-version-href]
|
|
4
|
+
[![npm downloads][npm-downloads-src]][npm-downloads-href]
|
|
5
|
+
[![bundle][bundle-src]][bundle-href]
|
|
6
|
+
[![JSDocs][jsdocs-src]][jsdocs-href]
|
|
7
|
+
|
|
8
|
+
## Usage
|
|
9
|
+
|
|
10
|
+
Install package:
|
|
11
|
+
|
|
12
|
+
```sh
|
|
13
|
+
# npm
|
|
14
|
+
npm install @bouzu/shared
|
|
15
|
+
|
|
16
|
+
# yarn
|
|
17
|
+
yarn add @bouzu/shared
|
|
18
|
+
|
|
19
|
+
# pnpm
|
|
20
|
+
pnpm install @bouzu/shared
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
Import:
|
|
24
|
+
|
|
25
|
+
```js
|
|
26
|
+
// ESM
|
|
27
|
+
import * as shared from '@bouzu/shared'
|
|
28
|
+
|
|
29
|
+
// CommonJS
|
|
30
|
+
const shared = require('@bouzu/shared')
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
## Development
|
|
34
|
+
|
|
35
|
+
- Clone this repository
|
|
36
|
+
- Install latest LTS version of [Node.js](https://nodejs.org/en/)
|
|
37
|
+
- Enable [Corepack](https://github.com/nodejs/corepack) using `corepack enable`
|
|
38
|
+
- Install dependencies using `pnpm install`
|
|
39
|
+
- Run interactive tests using `pnpm dev`
|
|
40
|
+
|
|
41
|
+
## License
|
|
42
|
+
|
|
43
|
+
Made with 💛
|
|
44
|
+
|
|
45
|
+
Published under [MIT License](./LICENSE).
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
<!-- Badges -->
|
|
49
|
+
|
|
50
|
+
[npm-version-src]: https://img.shields.io/npm/v/@bouzu/shared?style=flat&colorA=18181B&colorB=F0DB4F
|
|
51
|
+
[npm-version-href]: https://npmjs.com/package/@bouzu/shared
|
|
52
|
+
[npm-downloads-src]: https://img.shields.io/npm/dm/@bouzu/shared?style=flat&colorA=18181B&colorB=F0DB4F
|
|
53
|
+
[npm-downloads-href]: https://npmjs.com/package/@bouzu/shared
|
|
54
|
+
[bundle-src]: https://img.shields.io/bundlephobia/minzip/@bouzu/shared?style=flat&colorA=18181B&colorB=F0DB4F
|
|
55
|
+
[bundle-href]: https://bundlephobia.com/result?p=@bouzu/shared
|
|
56
|
+
[jsdocs-src]: https://img.shields.io/badge/jsDocs.io-reference-18181B?style=flat&colorA=18181B&colorB=F0DB4F
|
|
57
|
+
[jsdocs-href]: https://www.jsdocs.io/package/@bouzu/shared
|
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 };
|
|
@@ -54,13 +71,12 @@ function checkSizeEqual(a, b) {
|
|
|
54
71
|
return a.width === b.width && a.height === b.height;
|
|
55
72
|
}
|
|
56
73
|
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
})(RectCorner || {});
|
|
74
|
+
const RectCorner = {
|
|
75
|
+
TopLeft: "TopLeft",
|
|
76
|
+
TopRight: "TopRight",
|
|
77
|
+
BottomLeft: "BottomLeft",
|
|
78
|
+
BottomRight: "BottomRight"
|
|
79
|
+
};
|
|
64
80
|
function createRect(x = 0, y = 0, width = 0, height = 0) {
|
|
65
81
|
return {
|
|
66
82
|
...createPoint(x, y),
|
|
@@ -93,13 +109,13 @@ function getRectBottomRight(rect) {
|
|
|
93
109
|
}
|
|
94
110
|
function getRectPointByRectCorner(rect, corner) {
|
|
95
111
|
switch (corner) {
|
|
96
|
-
case
|
|
112
|
+
case RectCorner.TopLeft:
|
|
97
113
|
return getRectTopLeft(rect);
|
|
98
|
-
case
|
|
114
|
+
case RectCorner.TopRight:
|
|
99
115
|
return getRectTopRight(rect);
|
|
100
|
-
case
|
|
116
|
+
case RectCorner.BottomLeft:
|
|
101
117
|
return getRectBottomLeft(rect);
|
|
102
|
-
case
|
|
118
|
+
case RectCorner.BottomRight:
|
|
103
119
|
return getRectBottomRight(rect);
|
|
104
120
|
default:
|
|
105
121
|
throw new TypeError(`Not match corner: ${corner}`);
|
|
@@ -147,9 +163,148 @@ function toPoint(rect) {
|
|
|
147
163
|
return createPoint(rect.x, rect.y);
|
|
148
164
|
}
|
|
149
165
|
|
|
166
|
+
function isObject(val) {
|
|
167
|
+
return Object.prototype.toString.call(val) === "[object Object]";
|
|
168
|
+
}
|
|
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;
|
|
150
303
|
exports.RectCorner = RectCorner;
|
|
304
|
+
exports.checkAxis = checkAxis;
|
|
151
305
|
exports.checkLayoutInvalidate = checkLayoutInvalidate;
|
|
152
306
|
exports.checkPointEqual = checkPointEqual;
|
|
307
|
+
exports.checkPointEqualWithTolerance = checkPointEqualWithTolerance;
|
|
153
308
|
exports.checkPointOrigin = checkPointOrigin;
|
|
154
309
|
exports.checkRectContains = checkRectContains;
|
|
155
310
|
exports.checkRectContainsPoint = checkRectContainsPoint;
|
|
@@ -157,24 +312,43 @@ exports.checkRectEqual = checkRectEqual;
|
|
|
157
312
|
exports.checkRectEqualPoint = checkRectEqualPoint;
|
|
158
313
|
exports.checkRectEqualSize = checkRectEqualSize;
|
|
159
314
|
exports.checkRectIntersects = checkRectIntersects;
|
|
315
|
+
exports.checkRectIntersectsByAxis = checkRectIntersectsByAxis;
|
|
160
316
|
exports.checkRectIntersectsX = checkRectIntersectsX;
|
|
161
317
|
exports.checkRectIntersectsY = checkRectIntersectsY;
|
|
162
318
|
exports.checkSizeEqual = checkSizeEqual;
|
|
319
|
+
exports.clamp = clamp;
|
|
320
|
+
exports.clonePoint = clonePoint;
|
|
163
321
|
exports.cloneRect = cloneRect;
|
|
164
322
|
exports.createPoint = createPoint;
|
|
165
323
|
exports.createRect = createRect;
|
|
166
324
|
exports.createSize = createSize;
|
|
325
|
+
exports.easeLinear = easeLinear;
|
|
326
|
+
exports.easeOut = easeOut;
|
|
327
|
+
exports.easeOutCubic = easeOutCubic;
|
|
167
328
|
exports.execLastTick = execLastTick;
|
|
329
|
+
exports.getPointByAxis = getPointByAxis;
|
|
330
|
+
exports.getPointCenter = getPointCenter;
|
|
331
|
+
exports.getPointDistance = getPointDistance;
|
|
168
332
|
exports.getRectArea = getRectArea;
|
|
169
333
|
exports.getRectBottomLeft = getRectBottomLeft;
|
|
170
334
|
exports.getRectBottomRight = getRectBottomRight;
|
|
171
335
|
exports.getRectCornerInOther = getRectCornerInOther;
|
|
336
|
+
exports.getRectMaxByAxis = getRectMaxByAxis;
|
|
172
337
|
exports.getRectMaxX = getRectMaxX;
|
|
173
338
|
exports.getRectMaxY = getRectMaxY;
|
|
174
339
|
exports.getRectPointByRectCorner = getRectPointByRectCorner;
|
|
175
340
|
exports.getRectTopLeft = getRectTopLeft;
|
|
176
341
|
exports.getRectTopRight = getRectTopRight;
|
|
342
|
+
exports.getSizeByAxis = getSizeByAxis;
|
|
343
|
+
exports.getTime = getTime;
|
|
344
|
+
exports.isObject = isObject;
|
|
177
345
|
exports.noop = noop;
|
|
346
|
+
exports.registerRaf = registerRaf;
|
|
347
|
+
exports.reverseAxis = reverseAxis;
|
|
348
|
+
exports.runNoopTransition = runNoopTransition;
|
|
349
|
+
exports.runTransition = runTransition;
|
|
178
350
|
exports.toPoint = toPoint;
|
|
179
351
|
exports.toSize = toSize;
|
|
352
|
+
exports.updatePointByAxis = updatePointByAxis;
|
|
353
|
+
exports.updateSizeByAxis = updateSizeByAxis;
|
|
180
354
|
exports.usePrevious = usePrevious;
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
import { ValueOf } from 'type-fest';
|
|
2
|
+
|
|
3
|
+
declare function noop(): void;
|
|
4
|
+
|
|
5
|
+
type Previous<T> = [
|
|
6
|
+
{
|
|
7
|
+
readonly curr: T | undefined;
|
|
8
|
+
readonly prev: T | undefined;
|
|
9
|
+
},
|
|
10
|
+
(val: T) => void
|
|
11
|
+
];
|
|
12
|
+
declare function usePrevious<T>(val?: T): Previous<T>;
|
|
13
|
+
|
|
14
|
+
type WithoutFirstParameter<T> = T extends (arg1: any, ...args: infer U) => any ? U : any;
|
|
15
|
+
declare function execLastTick<TTickFn extends typeof setTimeout | typeof queueMicrotask | typeof requestAnimationFrame, TFn extends (...args: any) => any>(tickFn: TTickFn, fn: TFn, ...tickFnArgs: WithoutFirstParameter<TTickFn>): (...args: Parameters<TFn>) => void;
|
|
16
|
+
|
|
17
|
+
interface Point {
|
|
18
|
+
x: number;
|
|
19
|
+
y: number;
|
|
20
|
+
}
|
|
21
|
+
declare function createPoint(x?: number, y?: number): Point;
|
|
22
|
+
declare function checkPointOrigin(p: Point): boolean;
|
|
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;
|
|
28
|
+
|
|
29
|
+
interface Size {
|
|
30
|
+
width: number;
|
|
31
|
+
height: number;
|
|
32
|
+
}
|
|
33
|
+
declare function createSize(width?: number, height?: number): Size;
|
|
34
|
+
declare function checkSizeEqual(a: Size, b: Size): boolean;
|
|
35
|
+
|
|
36
|
+
type Rect = Size & Point;
|
|
37
|
+
declare const RectCorner: {
|
|
38
|
+
readonly TopLeft: "TopLeft";
|
|
39
|
+
readonly TopRight: "TopRight";
|
|
40
|
+
readonly BottomLeft: "BottomLeft";
|
|
41
|
+
readonly BottomRight: "BottomRight";
|
|
42
|
+
};
|
|
43
|
+
type RectCornerValue = ValueOf<typeof RectCorner>;
|
|
44
|
+
declare function createRect(x?: number, y?: number, width?: number, height?: number): Rect;
|
|
45
|
+
declare function cloneRect(rect: Rect): Rect;
|
|
46
|
+
declare function getRectMaxX(rect: Rect): number;
|
|
47
|
+
declare function getRectMaxY(rect: Rect): number;
|
|
48
|
+
declare function getRectArea(rect: Rect): number;
|
|
49
|
+
declare function getRectTopLeft(rect: Rect): Point;
|
|
50
|
+
declare function getRectTopRight(rect: Rect): Point;
|
|
51
|
+
declare function getRectBottomLeft(rect: Rect): Point;
|
|
52
|
+
declare function getRectBottomRight(rect: Rect): Point;
|
|
53
|
+
declare function getRectPointByRectCorner(rect: Rect, corner: RectCornerValue): Point;
|
|
54
|
+
declare function checkRectIntersectsX(a: Rect, b: Rect): boolean;
|
|
55
|
+
declare function checkRectIntersectsY(a: Rect, b: Rect): boolean;
|
|
56
|
+
declare function checkRectIntersects(a: Rect, b: Rect): boolean;
|
|
57
|
+
declare function checkRectContains(a: Rect, b: Rect): boolean;
|
|
58
|
+
declare function checkRectContainsPoint(a: Rect, p: Point): boolean;
|
|
59
|
+
declare function getRectCornerInOther(a: Rect, b: Rect): RectCornerValue | null;
|
|
60
|
+
declare function checkRectEqual(a: Rect, b: Rect): boolean;
|
|
61
|
+
declare function checkRectEqualPoint(r: Rect, p: Point | Rect): boolean;
|
|
62
|
+
declare function checkRectEqualSize(r: Rect, s: Size | Rect): boolean;
|
|
63
|
+
declare function checkLayoutInvalidate(newRect: Rect, oldRect: Rect): boolean;
|
|
64
|
+
declare function toSize(rect: Rect): Size;
|
|
65
|
+
declare function toPoint(rect: Rect): Point;
|
|
66
|
+
|
|
67
|
+
declare function isObject(val: unknown): val is Record<PropertyKey, any>;
|
|
68
|
+
|
|
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
ADDED
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
import { ValueOf } from 'type-fest';
|
|
2
|
+
|
|
3
|
+
declare function noop(): void;
|
|
4
|
+
|
|
5
|
+
type Previous<T> = [
|
|
6
|
+
{
|
|
7
|
+
readonly curr: T | undefined;
|
|
8
|
+
readonly prev: T | undefined;
|
|
9
|
+
},
|
|
10
|
+
(val: T) => void
|
|
11
|
+
];
|
|
12
|
+
declare function usePrevious<T>(val?: T): Previous<T>;
|
|
13
|
+
|
|
14
|
+
type WithoutFirstParameter<T> = T extends (arg1: any, ...args: infer U) => any ? U : any;
|
|
15
|
+
declare function execLastTick<TTickFn extends typeof setTimeout | typeof queueMicrotask | typeof requestAnimationFrame, TFn extends (...args: any) => any>(tickFn: TTickFn, fn: TFn, ...tickFnArgs: WithoutFirstParameter<TTickFn>): (...args: Parameters<TFn>) => void;
|
|
16
|
+
|
|
17
|
+
interface Point {
|
|
18
|
+
x: number;
|
|
19
|
+
y: number;
|
|
20
|
+
}
|
|
21
|
+
declare function createPoint(x?: number, y?: number): Point;
|
|
22
|
+
declare function checkPointOrigin(p: Point): boolean;
|
|
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;
|
|
28
|
+
|
|
29
|
+
interface Size {
|
|
30
|
+
width: number;
|
|
31
|
+
height: number;
|
|
32
|
+
}
|
|
33
|
+
declare function createSize(width?: number, height?: number): Size;
|
|
34
|
+
declare function checkSizeEqual(a: Size, b: Size): boolean;
|
|
35
|
+
|
|
36
|
+
type Rect = Size & Point;
|
|
37
|
+
declare const RectCorner: {
|
|
38
|
+
readonly TopLeft: "TopLeft";
|
|
39
|
+
readonly TopRight: "TopRight";
|
|
40
|
+
readonly BottomLeft: "BottomLeft";
|
|
41
|
+
readonly BottomRight: "BottomRight";
|
|
42
|
+
};
|
|
43
|
+
type RectCornerValue = ValueOf<typeof RectCorner>;
|
|
44
|
+
declare function createRect(x?: number, y?: number, width?: number, height?: number): Rect;
|
|
45
|
+
declare function cloneRect(rect: Rect): Rect;
|
|
46
|
+
declare function getRectMaxX(rect: Rect): number;
|
|
47
|
+
declare function getRectMaxY(rect: Rect): number;
|
|
48
|
+
declare function getRectArea(rect: Rect): number;
|
|
49
|
+
declare function getRectTopLeft(rect: Rect): Point;
|
|
50
|
+
declare function getRectTopRight(rect: Rect): Point;
|
|
51
|
+
declare function getRectBottomLeft(rect: Rect): Point;
|
|
52
|
+
declare function getRectBottomRight(rect: Rect): Point;
|
|
53
|
+
declare function getRectPointByRectCorner(rect: Rect, corner: RectCornerValue): Point;
|
|
54
|
+
declare function checkRectIntersectsX(a: Rect, b: Rect): boolean;
|
|
55
|
+
declare function checkRectIntersectsY(a: Rect, b: Rect): boolean;
|
|
56
|
+
declare function checkRectIntersects(a: Rect, b: Rect): boolean;
|
|
57
|
+
declare function checkRectContains(a: Rect, b: Rect): boolean;
|
|
58
|
+
declare function checkRectContainsPoint(a: Rect, p: Point): boolean;
|
|
59
|
+
declare function getRectCornerInOther(a: Rect, b: Rect): RectCornerValue | null;
|
|
60
|
+
declare function checkRectEqual(a: Rect, b: Rect): boolean;
|
|
61
|
+
declare function checkRectEqualPoint(r: Rect, p: Point | Rect): boolean;
|
|
62
|
+
declare function checkRectEqualSize(r: Rect, s: Size | Rect): boolean;
|
|
63
|
+
declare function checkLayoutInvalidate(newRect: Rect, oldRect: Rect): boolean;
|
|
64
|
+
declare function toSize(rect: Rect): Size;
|
|
65
|
+
declare function toPoint(rect: Rect): Point;
|
|
66
|
+
|
|
67
|
+
declare function isObject(val: unknown): val is Record<PropertyKey, any>;
|
|
68
|
+
|
|
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
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import { ValueOf } from 'type-fest';
|
|
2
|
+
|
|
1
3
|
declare function noop(): void;
|
|
2
4
|
|
|
3
5
|
type Previous<T> = [
|
|
@@ -19,6 +21,10 @@ interface Point {
|
|
|
19
21
|
declare function createPoint(x?: number, y?: number): Point;
|
|
20
22
|
declare function checkPointOrigin(p: Point): boolean;
|
|
21
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;
|
|
22
28
|
|
|
23
29
|
interface Size {
|
|
24
30
|
width: number;
|
|
@@ -28,12 +34,13 @@ declare function createSize(width?: number, height?: number): Size;
|
|
|
28
34
|
declare function checkSizeEqual(a: Size, b: Size): boolean;
|
|
29
35
|
|
|
30
36
|
type Rect = Size & Point;
|
|
31
|
-
declare
|
|
32
|
-
TopLeft
|
|
33
|
-
TopRight
|
|
34
|
-
BottomLeft
|
|
35
|
-
BottomRight
|
|
36
|
-
}
|
|
37
|
+
declare const RectCorner: {
|
|
38
|
+
readonly TopLeft: "TopLeft";
|
|
39
|
+
readonly TopRight: "TopRight";
|
|
40
|
+
readonly BottomLeft: "BottomLeft";
|
|
41
|
+
readonly BottomRight: "BottomRight";
|
|
42
|
+
};
|
|
43
|
+
type RectCornerValue = ValueOf<typeof RectCorner>;
|
|
37
44
|
declare function createRect(x?: number, y?: number, width?: number, height?: number): Rect;
|
|
38
45
|
declare function cloneRect(rect: Rect): Rect;
|
|
39
46
|
declare function getRectMaxX(rect: Rect): number;
|
|
@@ -43,13 +50,13 @@ declare function getRectTopLeft(rect: Rect): Point;
|
|
|
43
50
|
declare function getRectTopRight(rect: Rect): Point;
|
|
44
51
|
declare function getRectBottomLeft(rect: Rect): Point;
|
|
45
52
|
declare function getRectBottomRight(rect: Rect): Point;
|
|
46
|
-
declare function getRectPointByRectCorner(rect: Rect, corner:
|
|
53
|
+
declare function getRectPointByRectCorner(rect: Rect, corner: RectCornerValue): Point;
|
|
47
54
|
declare function checkRectIntersectsX(a: Rect, b: Rect): boolean;
|
|
48
55
|
declare function checkRectIntersectsY(a: Rect, b: Rect): boolean;
|
|
49
56
|
declare function checkRectIntersects(a: Rect, b: Rect): boolean;
|
|
50
57
|
declare function checkRectContains(a: Rect, b: Rect): boolean;
|
|
51
58
|
declare function checkRectContainsPoint(a: Rect, p: Point): boolean;
|
|
52
|
-
declare function getRectCornerInOther(a: Rect, b: Rect):
|
|
59
|
+
declare function getRectCornerInOther(a: Rect, b: Rect): RectCornerValue | null;
|
|
53
60
|
declare function checkRectEqual(a: Rect, b: Rect): boolean;
|
|
54
61
|
declare function checkRectEqualPoint(r: Rect, p: Point | Rect): boolean;
|
|
55
62
|
declare function checkRectEqualSize(r: Rect, s: Size | Rect): boolean;
|
|
@@ -57,4 +64,60 @@ declare function checkLayoutInvalidate(newRect: Rect, oldRect: Rect): boolean;
|
|
|
57
64
|
declare function toSize(rect: Rect): Size;
|
|
58
65
|
declare function toPoint(rect: Rect): Point;
|
|
59
66
|
|
|
60
|
-
|
|
67
|
+
declare function isObject(val: unknown): val is Record<PropertyKey, any>;
|
|
68
|
+
|
|
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 };
|
|
@@ -52,13 +69,12 @@ function checkSizeEqual(a, b) {
|
|
|
52
69
|
return a.width === b.width && a.height === b.height;
|
|
53
70
|
}
|
|
54
71
|
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
})(RectCorner || {});
|
|
72
|
+
const RectCorner = {
|
|
73
|
+
TopLeft: "TopLeft",
|
|
74
|
+
TopRight: "TopRight",
|
|
75
|
+
BottomLeft: "BottomLeft",
|
|
76
|
+
BottomRight: "BottomRight"
|
|
77
|
+
};
|
|
62
78
|
function createRect(x = 0, y = 0, width = 0, height = 0) {
|
|
63
79
|
return {
|
|
64
80
|
...createPoint(x, y),
|
|
@@ -91,13 +107,13 @@ function getRectBottomRight(rect) {
|
|
|
91
107
|
}
|
|
92
108
|
function getRectPointByRectCorner(rect, corner) {
|
|
93
109
|
switch (corner) {
|
|
94
|
-
case
|
|
110
|
+
case RectCorner.TopLeft:
|
|
95
111
|
return getRectTopLeft(rect);
|
|
96
|
-
case
|
|
112
|
+
case RectCorner.TopRight:
|
|
97
113
|
return getRectTopRight(rect);
|
|
98
|
-
case
|
|
114
|
+
case RectCorner.BottomLeft:
|
|
99
115
|
return getRectBottomLeft(rect);
|
|
100
|
-
case
|
|
116
|
+
case RectCorner.BottomRight:
|
|
101
117
|
return getRectBottomRight(rect);
|
|
102
118
|
default:
|
|
103
119
|
throw new TypeError(`Not match corner: ${corner}`);
|
|
@@ -145,4 +161,140 @@ function toPoint(rect) {
|
|
|
145
161
|
return createPoint(rect.x, rect.y);
|
|
146
162
|
}
|
|
147
163
|
|
|
148
|
-
|
|
164
|
+
function isObject(val) {
|
|
165
|
+
return Object.prototype.toString.call(val) === "[object Object]";
|
|
166
|
+
}
|
|
167
|
+
|
|
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,15 +1,23 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@bouzu/shared",
|
|
3
|
-
"version": "0.0
|
|
3
|
+
"version": "0.1.0",
|
|
4
4
|
"private": false,
|
|
5
5
|
"author": "zhong666 <hi@zhong666.me>",
|
|
6
6
|
"license": "MIT",
|
|
7
|
+
"homepage": "https://bouzu.zhong666.me",
|
|
8
|
+
"repository": {
|
|
9
|
+
"type": "git",
|
|
10
|
+
"url": "https://github.com/aa900031/bouzu"
|
|
11
|
+
},
|
|
12
|
+
"bugs": {
|
|
13
|
+
"url": "https://github.com/aa900031/bouzu/issues"
|
|
14
|
+
},
|
|
7
15
|
"sideEffects": false,
|
|
8
16
|
"exports": {
|
|
9
17
|
".": {
|
|
10
18
|
"types": "./dist/index.d.ts",
|
|
11
|
-
"
|
|
12
|
-
"
|
|
19
|
+
"import": "./dist/index.mjs",
|
|
20
|
+
"require": "./dist/index.cjs"
|
|
13
21
|
}
|
|
14
22
|
},
|
|
15
23
|
"main": "./dist/index.cjs",
|
|
@@ -21,7 +29,15 @@
|
|
|
21
29
|
"publishConfig": {
|
|
22
30
|
"access": "public"
|
|
23
31
|
},
|
|
32
|
+
"dependencies": {
|
|
33
|
+
"type-fest": "^3.13.1"
|
|
34
|
+
},
|
|
35
|
+
"devDependencies": {
|
|
36
|
+
"@bouzu/release-it-config": "0.1.0",
|
|
37
|
+
"@bouzu/tsconfig": "0.1.0"
|
|
38
|
+
},
|
|
24
39
|
"scripts": {
|
|
25
|
-
"build": "unbuild"
|
|
40
|
+
"build": "unbuild",
|
|
41
|
+
"release": "release-it --ci"
|
|
26
42
|
}
|
|
27
43
|
}
|