@zag-js/rect-utils 0.1.1 → 0.1.4
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/align.d.ts +8 -0
- package/dist/closest.d.ts +5 -0
- package/dist/contains.d.ts +5 -0
- package/dist/distance.d.ts +9 -0
- package/dist/from-element.d.ts +0 -1
- package/dist/from-points.d.ts +3 -0
- package/dist/from-range.d.ts +2 -0
- package/dist/from-rotation.d.ts +5 -0
- package/dist/from-window.d.ts +20 -0
- package/dist/get-polygon.d.ts +5 -0
- package/dist/index.d.ts +13 -4
- package/dist/index.js +362 -90
- package/dist/index.mjs +364 -90
- package/dist/intersection.d.ts +14 -0
- package/dist/operations.d.ts +0 -1
- package/dist/polygon.d.ts +3 -4
- package/dist/rect.d.ts +55 -59
- package/dist/types.d.ts +0 -1
- package/dist/union.d.ts +2 -0
- package/package.json +3 -2
- package/dist/computed-style.d.ts +0 -6
- package/dist/computed-style.d.ts.map +0 -1
- package/dist/from-element.d.ts.map +0 -1
- package/dist/index.d.ts.map +0 -1
- package/dist/index.js.map +0 -7
- package/dist/index.mjs.map +0 -7
- package/dist/operations.d.ts.map +0 -1
- package/dist/point.d.ts +0 -16
- package/dist/point.d.ts.map +0 -1
- package/dist/polygon.d.ts.map +0 -1
- package/dist/rect.d.ts.map +0 -1
- package/dist/types.d.ts.map +0 -1
package/dist/align.d.ts
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { Rect } from "./rect";
|
|
2
|
+
export declare function alignRect(a: Rect, ref: Rect, options: AlignOptions): Rect;
|
|
3
|
+
export declare type AlignOptions = {
|
|
4
|
+
h: HAlign;
|
|
5
|
+
v: VAlign;
|
|
6
|
+
};
|
|
7
|
+
export declare type HAlign = "left-inside" | "left-outside" | "center" | "right-inside" | "right-outside";
|
|
8
|
+
export declare type VAlign = "top-inside" | "top-outside" | "center" | "bottom-inside" | "bottom-outside";
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import type { Rect } from "./rect";
|
|
2
|
+
import type { Point, RectSide } from "./types";
|
|
3
|
+
export declare function closest(...pts: Point[]): (a: Point) => Point;
|
|
4
|
+
export declare function closestSideToRect(ref: Rect, r: Rect): RectSide;
|
|
5
|
+
export declare function closestSideToPoint(ref: Rect, p: Point): RectSide;
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import { Rect } from "./rect";
|
|
2
|
+
import type { Point } from "./types";
|
|
3
|
+
export declare function containsPoint(r: Rect, p: Point): boolean;
|
|
4
|
+
export declare function containsRect(a: Rect, b: Rect): boolean;
|
|
5
|
+
export declare function contains(r: Rect, v: Rect | Point): boolean;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { Rect } from "./rect";
|
|
2
|
+
import type { Point, RectSide } from "./types";
|
|
3
|
+
export declare type DistanceValue = Point & {
|
|
4
|
+
value: number;
|
|
5
|
+
};
|
|
6
|
+
export declare function distance(a: Point, b?: Point): number;
|
|
7
|
+
export declare function distanceFromPoint(r: Rect, p: Point): DistanceValue;
|
|
8
|
+
export declare function distanceFromRect(a: Rect, b: Rect): DistanceValue;
|
|
9
|
+
export declare function distanceBtwEdges(a: Rect, b: Rect): Record<RectSide, number>;
|
package/dist/from-element.d.ts
CHANGED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { Rect } from "./rect";
|
|
2
|
+
export declare type WindowRectOptions = {
|
|
3
|
+
/**
|
|
4
|
+
* Whether to exclude the element's scrollbar size from the calculation.
|
|
5
|
+
*/
|
|
6
|
+
excludeScrollbar?: boolean;
|
|
7
|
+
};
|
|
8
|
+
/**
|
|
9
|
+
* Creates a rectange from window object
|
|
10
|
+
*/
|
|
11
|
+
export declare function getWindowRect(win: Window, opts?: WindowRectOptions): Rect;
|
|
12
|
+
/**
|
|
13
|
+
* Get the rect of the window with the option to exclude the scrollbar
|
|
14
|
+
*/
|
|
15
|
+
export declare function getViewportRect(win: Window, opts: WindowRectOptions): {
|
|
16
|
+
x: number;
|
|
17
|
+
y: number;
|
|
18
|
+
width: number;
|
|
19
|
+
height: number;
|
|
20
|
+
};
|
package/dist/index.d.ts
CHANGED
|
@@ -1,7 +1,16 @@
|
|
|
1
|
-
export * from "./
|
|
2
|
-
export * from "./
|
|
1
|
+
export * from "./align";
|
|
2
|
+
export * from "./closest";
|
|
3
|
+
export * from "./contains";
|
|
4
|
+
export * from "./distance";
|
|
3
5
|
export * from "./from-element";
|
|
4
|
-
export * from "./
|
|
6
|
+
export * from "./from-points";
|
|
7
|
+
export * from "./from-range";
|
|
8
|
+
export * from "./from-rotation";
|
|
9
|
+
export * from "./from-window";
|
|
10
|
+
export * from "./get-polygon";
|
|
11
|
+
export * from "./intersection";
|
|
5
12
|
export * from "./operations";
|
|
6
13
|
export * from "./polygon";
|
|
7
|
-
|
|
14
|
+
export * from "./rect";
|
|
15
|
+
export * from "./types";
|
|
16
|
+
export * from "./union";
|
package/dist/index.js
CHANGED
|
@@ -1,7 +1,25 @@
|
|
|
1
|
+
"use strict";
|
|
1
2
|
var __defProp = Object.defineProperty;
|
|
3
|
+
var __defProps = Object.defineProperties;
|
|
2
4
|
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
|
|
3
6
|
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
7
|
+
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
|
|
4
8
|
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
9
|
+
var __propIsEnum = Object.prototype.propertyIsEnumerable;
|
|
10
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
11
|
+
var __spreadValues = (a, b) => {
|
|
12
|
+
for (var prop in b || (b = {}))
|
|
13
|
+
if (__hasOwnProp.call(b, prop))
|
|
14
|
+
__defNormalProp(a, prop, b[prop]);
|
|
15
|
+
if (__getOwnPropSymbols)
|
|
16
|
+
for (var prop of __getOwnPropSymbols(b)) {
|
|
17
|
+
if (__propIsEnum.call(b, prop))
|
|
18
|
+
__defNormalProp(a, prop, b[prop]);
|
|
19
|
+
}
|
|
20
|
+
return a;
|
|
21
|
+
};
|
|
22
|
+
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
|
|
5
23
|
var __export = (target, all) => {
|
|
6
24
|
for (var name in all)
|
|
7
25
|
__defProp(target, name, { get: all[name], enumerable: true });
|
|
@@ -19,112 +37,276 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
|
|
|
19
37
|
// src/index.ts
|
|
20
38
|
var src_exports = {};
|
|
21
39
|
__export(src_exports, {
|
|
22
|
-
|
|
40
|
+
alignRect: () => alignRect,
|
|
23
41
|
closest: () => closest,
|
|
42
|
+
closestSideToPoint: () => closestSideToPoint,
|
|
43
|
+
closestSideToRect: () => closestSideToRect,
|
|
44
|
+
collisions: () => collisions,
|
|
45
|
+
contains: () => contains,
|
|
46
|
+
containsPoint: () => containsPoint,
|
|
47
|
+
containsRect: () => containsRect,
|
|
48
|
+
createRect: () => createRect,
|
|
24
49
|
debugPolygon: () => debugPolygon,
|
|
25
50
|
distance: () => distance,
|
|
51
|
+
distanceBtwEdges: () => distanceBtwEdges,
|
|
52
|
+
distanceFromPoint: () => distanceFromPoint,
|
|
53
|
+
distanceFromRect: () => distanceFromRect,
|
|
26
54
|
expand: () => expand,
|
|
55
|
+
fromRange: () => fromRange,
|
|
56
|
+
getElementPolygon: () => getElementPolygon,
|
|
27
57
|
getElementRect: () => getElementRect,
|
|
28
|
-
|
|
58
|
+
getRectCenters: () => getRectCenters,
|
|
59
|
+
getRectCorners: () => getRectCorners,
|
|
60
|
+
getRectEdges: () => getRectEdges,
|
|
61
|
+
getRectFromPoints: () => getRectFromPoints,
|
|
62
|
+
getRotationRect: () => getRotationRect,
|
|
63
|
+
getViewportRect: () => getViewportRect,
|
|
64
|
+
getWindowRect: () => getWindowRect,
|
|
29
65
|
inset: () => inset,
|
|
66
|
+
intersection: () => intersection,
|
|
67
|
+
intersects: () => intersects,
|
|
68
|
+
isPointInPolygon: () => isPointInPolygon,
|
|
69
|
+
isRect: () => isRect,
|
|
30
70
|
isSymmetric: () => isSymmetric,
|
|
31
|
-
|
|
71
|
+
rotate: () => rotate,
|
|
32
72
|
shift: () => shift,
|
|
33
73
|
shrink: () => shrink,
|
|
34
|
-
|
|
74
|
+
toRad: () => toRad,
|
|
75
|
+
union: () => union
|
|
35
76
|
});
|
|
36
77
|
module.exports = __toCommonJS(src_exports);
|
|
37
78
|
|
|
38
|
-
// src/
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
}
|
|
44
|
-
static create(v) {
|
|
45
|
-
return new Rect(v);
|
|
79
|
+
// src/align.ts
|
|
80
|
+
function hAlign(a, ref, h) {
|
|
81
|
+
let x = ref.minX;
|
|
82
|
+
if (h === "left-inside") {
|
|
83
|
+
x = ref.minX;
|
|
46
84
|
}
|
|
47
|
-
|
|
48
|
-
|
|
85
|
+
if (h === "left-outside") {
|
|
86
|
+
x = ref.minX - ref.width;
|
|
49
87
|
}
|
|
50
|
-
|
|
51
|
-
|
|
88
|
+
if (h === "right-inside") {
|
|
89
|
+
x = ref.maxX - ref.width;
|
|
52
90
|
}
|
|
53
|
-
|
|
54
|
-
|
|
91
|
+
if (h === "right-outside") {
|
|
92
|
+
x = ref.maxX;
|
|
55
93
|
}
|
|
56
|
-
|
|
57
|
-
|
|
94
|
+
if (h === "center") {
|
|
95
|
+
x = ref.midX - ref.width / 2;
|
|
58
96
|
}
|
|
59
|
-
|
|
60
|
-
|
|
97
|
+
return __spreadProps(__spreadValues({}, a), { x });
|
|
98
|
+
}
|
|
99
|
+
function vAlign(a, ref, v) {
|
|
100
|
+
let y = ref.minY;
|
|
101
|
+
if (v === "top-inside") {
|
|
102
|
+
y = ref.minY;
|
|
61
103
|
}
|
|
62
|
-
|
|
63
|
-
|
|
104
|
+
if (v === "top-outside") {
|
|
105
|
+
y = ref.minY - a.height;
|
|
64
106
|
}
|
|
65
|
-
|
|
66
|
-
|
|
107
|
+
if (v === "bottom-inside") {
|
|
108
|
+
y = ref.maxY - a.height;
|
|
67
109
|
}
|
|
68
|
-
|
|
69
|
-
|
|
110
|
+
if (v === "bottom-outside") {
|
|
111
|
+
y = ref.maxY;
|
|
70
112
|
}
|
|
71
|
-
|
|
72
|
-
|
|
113
|
+
if (v === "center") {
|
|
114
|
+
y = ref.midY - a.height / 2;
|
|
73
115
|
}
|
|
74
|
-
|
|
75
|
-
|
|
116
|
+
return __spreadProps(__spreadValues({}, a), { y });
|
|
117
|
+
}
|
|
118
|
+
function alignRect(a, ref, options) {
|
|
119
|
+
const { h, v } = options;
|
|
120
|
+
return vAlign(hAlign(a, ref, h), ref, v);
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
// ../core/dist/index.mjs
|
|
124
|
+
var hasProp = (obj, prop) => Object.prototype.hasOwnProperty.call(obj, prop);
|
|
125
|
+
|
|
126
|
+
// src/rect.ts
|
|
127
|
+
var point = (x, y) => ({ x, y });
|
|
128
|
+
function createRect(r) {
|
|
129
|
+
const { x, y, width, height } = r;
|
|
130
|
+
const midX = x + width / 2;
|
|
131
|
+
const midY = y + height / 2;
|
|
132
|
+
return {
|
|
133
|
+
x,
|
|
134
|
+
y,
|
|
135
|
+
width,
|
|
136
|
+
height,
|
|
137
|
+
minX: x,
|
|
138
|
+
minY: y,
|
|
139
|
+
maxX: x + width,
|
|
140
|
+
maxY: y + height,
|
|
141
|
+
midX,
|
|
142
|
+
midY,
|
|
143
|
+
center: point(midX, midY)
|
|
144
|
+
};
|
|
145
|
+
}
|
|
146
|
+
function isRect(v) {
|
|
147
|
+
return hasProp(v, "x") && hasProp(v, "y") && hasProp(v, "width") && hasProp(v, "height");
|
|
148
|
+
}
|
|
149
|
+
function getRectCenters(v) {
|
|
150
|
+
const top = point(v.midX, v.minY);
|
|
151
|
+
const right = point(v.maxX, v.midY);
|
|
152
|
+
const bottom = point(v.midX, v.maxY);
|
|
153
|
+
const left = point(v.minX, v.midY);
|
|
154
|
+
return { top, right, bottom, left };
|
|
155
|
+
}
|
|
156
|
+
function getRectCorners(v) {
|
|
157
|
+
const top = point(v.minX, v.minY);
|
|
158
|
+
const right = point(v.maxX, v.minY);
|
|
159
|
+
const bottom = point(v.maxX, v.maxY);
|
|
160
|
+
const left = point(v.minX, v.maxY);
|
|
161
|
+
return { top, right, bottom, left };
|
|
162
|
+
}
|
|
163
|
+
function getRectEdges(v) {
|
|
164
|
+
const c = getRectCorners(v);
|
|
165
|
+
const top = [c.top, c.right];
|
|
166
|
+
const right = [c.right, c.bottom];
|
|
167
|
+
const bottom = [c.left, c.bottom];
|
|
168
|
+
const left = [c.top, c.left];
|
|
169
|
+
return { top, right, bottom, left };
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
// src/intersection.ts
|
|
173
|
+
function intersects(a, b) {
|
|
174
|
+
return a.x < b.maxX && a.y < b.maxY && a.maxX > b.x && a.maxY > b.y;
|
|
175
|
+
}
|
|
176
|
+
function intersection(a, b) {
|
|
177
|
+
const x = Math.max(a.x, b.x);
|
|
178
|
+
const y = Math.max(a.y, b.y);
|
|
179
|
+
const x2 = Math.min(a.x + a.width, b.x + b.width);
|
|
180
|
+
const y2 = Math.min(a.y + a.height, b.y + b.height);
|
|
181
|
+
return createRect({ x, y, width: x2 - x, height: y2 - y });
|
|
182
|
+
}
|
|
183
|
+
function collisions(a, b) {
|
|
184
|
+
return {
|
|
185
|
+
top: a.minY <= b.minY,
|
|
186
|
+
right: a.maxX >= b.maxX,
|
|
187
|
+
bottom: a.maxY >= b.maxY,
|
|
188
|
+
left: a.minX <= b.minX
|
|
189
|
+
};
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
// src/distance.ts
|
|
193
|
+
function distance(a, b = { x: 0, y: 0 }) {
|
|
194
|
+
return Math.sqrt(Math.pow(a.x - b.x, 2) + Math.pow(a.y - b.y, 2));
|
|
195
|
+
}
|
|
196
|
+
function distanceFromPoint(r, p) {
|
|
197
|
+
let x = 0;
|
|
198
|
+
let y = 0;
|
|
199
|
+
if (p.x < r.x)
|
|
200
|
+
x = r.x - p.x;
|
|
201
|
+
else if (p.x > r.maxX)
|
|
202
|
+
x = p.x - r.maxX;
|
|
203
|
+
if (p.y < r.y)
|
|
204
|
+
y = r.y - p.y;
|
|
205
|
+
else if (p.y > r.maxY)
|
|
206
|
+
y = p.y - r.maxY;
|
|
207
|
+
return { x, y, value: distance({ x, y }) };
|
|
208
|
+
}
|
|
209
|
+
function distanceFromRect(a, b) {
|
|
210
|
+
if (intersects(a, b))
|
|
211
|
+
return { x: 0, y: 0, value: 0 };
|
|
212
|
+
const left = a.x < b.x ? a : b;
|
|
213
|
+
const right = b.x < a.x ? a : b;
|
|
214
|
+
const upper = a.y < b.y ? a : b;
|
|
215
|
+
const lower = b.y < a.y ? a : b;
|
|
216
|
+
let x = left.x === right.x ? 0 : right.x - left.maxX;
|
|
217
|
+
x = Math.max(0, x);
|
|
218
|
+
let y = upper.y === lower.y ? 0 : lower.y - upper.maxY;
|
|
219
|
+
y = Math.max(0, y);
|
|
220
|
+
return { x, y, value: distance({ x, y }) };
|
|
221
|
+
}
|
|
222
|
+
function distanceBtwEdges(a, b) {
|
|
223
|
+
return {
|
|
224
|
+
left: b.x - a.x,
|
|
225
|
+
top: b.y - a.y,
|
|
226
|
+
right: a.maxX - b.maxX,
|
|
227
|
+
bottom: a.maxY - b.maxY
|
|
228
|
+
};
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
// src/closest.ts
|
|
232
|
+
function closest(...pts) {
|
|
233
|
+
return (a) => {
|
|
234
|
+
const ds = pts.map((b) => distance(b, a));
|
|
235
|
+
const c = Math.min.apply(Math, ds);
|
|
236
|
+
return pts[ds.indexOf(c)];
|
|
237
|
+
};
|
|
238
|
+
}
|
|
239
|
+
function closestSideToRect(ref, r) {
|
|
240
|
+
if (r.maxX <= ref.minX) {
|
|
241
|
+
return "left";
|
|
76
242
|
}
|
|
77
|
-
|
|
78
|
-
return
|
|
243
|
+
if (r.minX >= ref.maxX) {
|
|
244
|
+
return "right";
|
|
79
245
|
}
|
|
80
|
-
|
|
81
|
-
return
|
|
246
|
+
if (r.maxY <= ref.minY) {
|
|
247
|
+
return "top";
|
|
82
248
|
}
|
|
83
|
-
|
|
84
|
-
return
|
|
249
|
+
if (r.minY >= ref.maxY) {
|
|
250
|
+
return "bottom";
|
|
85
251
|
}
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
252
|
+
return "left";
|
|
253
|
+
}
|
|
254
|
+
function closestSideToPoint(ref, p) {
|
|
255
|
+
const { x, y } = p;
|
|
256
|
+
const dl = x - ref.minX;
|
|
257
|
+
const dr = ref.maxX - x;
|
|
258
|
+
const dt = y - ref.minY;
|
|
259
|
+
const db = ref.maxY - y;
|
|
260
|
+
let closest2 = dl;
|
|
261
|
+
let side = "left";
|
|
262
|
+
if (dr < closest2) {
|
|
263
|
+
closest2 = dr;
|
|
264
|
+
side = "right";
|
|
92
265
|
}
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
const bottom = point(this.maxX, this.maxY);
|
|
97
|
-
const left = point(this.minX, this.maxY);
|
|
98
|
-
return { top, right, bottom, left };
|
|
266
|
+
if (dt < closest2) {
|
|
267
|
+
closest2 = dt;
|
|
268
|
+
side = "top";
|
|
99
269
|
}
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
const top = [c.top, c.right];
|
|
103
|
-
const right = [c.right, c.bottom];
|
|
104
|
-
const bottom = [c.left, c.bottom];
|
|
105
|
-
const left = [c.top, c.left];
|
|
106
|
-
return { top, right, bottom, left };
|
|
270
|
+
if (db < closest2) {
|
|
271
|
+
side = "bottom";
|
|
107
272
|
}
|
|
108
|
-
|
|
273
|
+
return side;
|
|
274
|
+
}
|
|
109
275
|
|
|
110
|
-
// src/
|
|
111
|
-
|
|
276
|
+
// src/contains.ts
|
|
277
|
+
function containsPoint(r, p) {
|
|
278
|
+
return r.minX <= p.x && p.x <= r.maxX && r.minY <= p.y && p.y <= r.maxY;
|
|
279
|
+
}
|
|
280
|
+
function containsRect(a, b) {
|
|
281
|
+
return Object.values(getRectCorners(b)).every((c) => containsPoint(a, c));
|
|
282
|
+
}
|
|
283
|
+
function contains(r, v) {
|
|
284
|
+
return isRect(v) ? containsRect(r, v) : containsPoint(r, v);
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
// ../dom/dist/index.mjs
|
|
288
|
+
function getCache() {
|
|
289
|
+
const g = globalThis;
|
|
290
|
+
g.__styleCache = g.__styleCache || /* @__PURE__ */ new WeakMap();
|
|
291
|
+
return g.__styleCache;
|
|
292
|
+
}
|
|
112
293
|
function getComputedStyle(el) {
|
|
113
294
|
var _a;
|
|
114
295
|
if (!el)
|
|
115
296
|
return {};
|
|
116
|
-
|
|
297
|
+
const cache = getCache();
|
|
298
|
+
let style = cache.get(el);
|
|
117
299
|
if (!style) {
|
|
118
300
|
const win = (_a = el == null ? void 0 : el.ownerDocument.defaultView) != null ? _a : window;
|
|
119
301
|
style = win.getComputedStyle(el);
|
|
120
|
-
|
|
302
|
+
cache.set(el, style);
|
|
121
303
|
}
|
|
122
304
|
return style;
|
|
123
305
|
}
|
|
124
306
|
|
|
125
307
|
// src/from-element.ts
|
|
126
308
|
function getElementRect(el, opts = {}) {
|
|
127
|
-
return
|
|
309
|
+
return createRect(getClientRect(el, opts));
|
|
128
310
|
}
|
|
129
311
|
function getClientRect(el, opts = {}) {
|
|
130
312
|
const { excludeScrollbar = false, excludeBorders = false } = opts;
|
|
@@ -151,42 +333,124 @@ function getClientRect(el, opts = {}) {
|
|
|
151
333
|
var px = (v) => parseFloat(v.replace("px", ""));
|
|
152
334
|
var sum = (...vals) => vals.reduce((sum2, v) => sum2 + (v ? px(v) : 0), 0);
|
|
153
335
|
|
|
154
|
-
//
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
336
|
+
// src/from-points.ts
|
|
337
|
+
function getRectFromPoints(...pts) {
|
|
338
|
+
const xs = pts.map((p) => p.x);
|
|
339
|
+
const ys = pts.map((p) => p.y);
|
|
340
|
+
const x = Math.min(...xs);
|
|
341
|
+
const y = Math.min(...ys);
|
|
342
|
+
const width = Math.max(...xs) - x;
|
|
343
|
+
const height = Math.max(...ys) - y;
|
|
344
|
+
return createRect({ x, y, width, height });
|
|
162
345
|
}
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
346
|
+
|
|
347
|
+
// src/union.ts
|
|
348
|
+
var { min, max } = Math;
|
|
349
|
+
function union(...rs) {
|
|
350
|
+
const pMin = {
|
|
351
|
+
x: min.apply(Math, rs.map((r) => r.minX)),
|
|
352
|
+
y: min.apply(Math, rs.map((r) => r.minY))
|
|
168
353
|
};
|
|
354
|
+
const pMax = {
|
|
355
|
+
x: max.apply(Math, rs.map((r) => r.maxX)),
|
|
356
|
+
y: max.apply(Math, rs.map((r) => r.maxY))
|
|
357
|
+
};
|
|
358
|
+
return getRectFromPoints(pMin, pMax);
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
// src/from-range.ts
|
|
362
|
+
function fromRange(range) {
|
|
363
|
+
let rs = [];
|
|
364
|
+
const rects = Array.from(range.getClientRects());
|
|
365
|
+
if (rects.length) {
|
|
366
|
+
rs = rs.concat(rects.map(createRect));
|
|
367
|
+
return union.apply(void 0, rs);
|
|
368
|
+
}
|
|
369
|
+
let start = range.startContainer;
|
|
370
|
+
if (start.nodeType === Node.TEXT_NODE) {
|
|
371
|
+
start = start.parentNode;
|
|
372
|
+
}
|
|
373
|
+
if (start instanceof HTMLElement) {
|
|
374
|
+
const r = getElementRect(start);
|
|
375
|
+
rs.push(__spreadProps(__spreadValues({}, r), { x: r.maxX, width: 0 }));
|
|
376
|
+
}
|
|
377
|
+
return union.apply(void 0, rs);
|
|
169
378
|
}
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
return
|
|
379
|
+
|
|
380
|
+
// src/from-rotation.ts
|
|
381
|
+
function toRad(d) {
|
|
382
|
+
return d % 360 * Math.PI / 180;
|
|
174
383
|
}
|
|
175
|
-
function
|
|
176
|
-
const
|
|
177
|
-
const
|
|
384
|
+
function rotate(a, d, c) {
|
|
385
|
+
const r = toRad(d);
|
|
386
|
+
const sin = Math.sin(r);
|
|
387
|
+
const cos = Math.cos(r);
|
|
388
|
+
const x = a.x - c.x;
|
|
389
|
+
const y = a.y - c.y;
|
|
178
390
|
return {
|
|
179
|
-
|
|
180
|
-
|
|
391
|
+
x: c.x + x * cos - y * sin,
|
|
392
|
+
y: c.y + x * sin + y * cos
|
|
181
393
|
};
|
|
182
394
|
}
|
|
395
|
+
function getRotationRect(r, deg) {
|
|
396
|
+
const rr = Object.values(getRectCorners(r)).map((p) => rotate(p, deg, r.center));
|
|
397
|
+
const xs = rr.map((p) => p.x);
|
|
398
|
+
const ys = rr.map((p) => p.y);
|
|
399
|
+
const minX = Math.min(...xs);
|
|
400
|
+
const minY = Math.min(...ys);
|
|
401
|
+
const maxX = Math.max(...xs);
|
|
402
|
+
const maxY = Math.max(...ys);
|
|
403
|
+
return createRect({
|
|
404
|
+
x: minX,
|
|
405
|
+
y: minY,
|
|
406
|
+
width: maxX - minX,
|
|
407
|
+
height: maxY - minY
|
|
408
|
+
});
|
|
409
|
+
}
|
|
410
|
+
|
|
411
|
+
// src/from-window.ts
|
|
412
|
+
function getWindowRect(win, opts = {}) {
|
|
413
|
+
return createRect(getViewportRect(win, opts));
|
|
414
|
+
}
|
|
415
|
+
function getViewportRect(win, opts) {
|
|
416
|
+
const { excludeScrollbar = false } = opts;
|
|
417
|
+
const { innerWidth: innerWidth2, innerHeight: innerHeight2, document: doc, visualViewport } = win;
|
|
418
|
+
const width = visualViewport.width || innerWidth2;
|
|
419
|
+
const height = visualViewport.height || innerHeight2;
|
|
420
|
+
const rect = { x: 0, y: 0, width, height };
|
|
421
|
+
if (excludeScrollbar) {
|
|
422
|
+
const scrollbarWidth = innerWidth2 - doc.documentElement.clientWidth;
|
|
423
|
+
const scrollbarHeight = innerHeight2 - doc.documentElement.clientHeight;
|
|
424
|
+
rect.width -= scrollbarWidth;
|
|
425
|
+
rect.height -= scrollbarHeight;
|
|
426
|
+
}
|
|
427
|
+
return rect;
|
|
428
|
+
}
|
|
429
|
+
|
|
430
|
+
// src/get-polygon.ts
|
|
431
|
+
function getElementPolygon(rectValue, placement) {
|
|
432
|
+
const rect = createRect(rectValue);
|
|
433
|
+
const { top, right, left, bottom } = getRectCorners(rect);
|
|
434
|
+
const [base] = placement.split("-");
|
|
435
|
+
return {
|
|
436
|
+
top: [left, top, right, bottom],
|
|
437
|
+
right: [top, right, bottom, left],
|
|
438
|
+
bottom: [top, left, bottom, right],
|
|
439
|
+
left: [right, top, left, bottom]
|
|
440
|
+
}[base];
|
|
441
|
+
}
|
|
183
442
|
|
|
184
443
|
// src/operations.ts
|
|
185
444
|
var isSymmetric = (v) => "dx" in v || "dy" in v;
|
|
186
445
|
function inset(r, i) {
|
|
187
446
|
const v = isSymmetric(i) ? { left: i.dx, right: i.dx, top: i.dy, bottom: i.dy } : i;
|
|
188
447
|
const { top = 0, right = 0, bottom = 0, left = 0 } = v;
|
|
189
|
-
return
|
|
448
|
+
return createRect({
|
|
449
|
+
x: r.x + left,
|
|
450
|
+
y: r.y + top,
|
|
451
|
+
width: r.width - left - right,
|
|
452
|
+
height: r.height - top - bottom
|
|
453
|
+
});
|
|
190
454
|
}
|
|
191
455
|
function expand(r, v) {
|
|
192
456
|
const value = typeof v === "number" ? { dx: -v, dy: -v } : v;
|
|
@@ -198,11 +462,16 @@ function shrink(r, v) {
|
|
|
198
462
|
}
|
|
199
463
|
function shift(r, o) {
|
|
200
464
|
const { x = 0, y = 0 } = o;
|
|
201
|
-
return
|
|
465
|
+
return createRect({
|
|
466
|
+
x: r.x + x,
|
|
467
|
+
y: r.y + y,
|
|
468
|
+
width: r.width,
|
|
469
|
+
height: r.height
|
|
470
|
+
});
|
|
202
471
|
}
|
|
203
472
|
|
|
204
473
|
// src/polygon.ts
|
|
205
|
-
function
|
|
474
|
+
function isPointInPolygon(polygon, point2) {
|
|
206
475
|
const { x, y } = point2;
|
|
207
476
|
let c = false;
|
|
208
477
|
for (let i = 0, j = polygon.length - 1; i < polygon.length; j = i++) {
|
|
@@ -230,7 +499,8 @@ function createPolygonElement() {
|
|
|
230
499
|
height: "100%",
|
|
231
500
|
opacity: "0.15",
|
|
232
501
|
position: "fixed",
|
|
233
|
-
pointerEvents: "none"
|
|
502
|
+
pointerEvents: "none",
|
|
503
|
+
fill: "red"
|
|
234
504
|
});
|
|
235
505
|
const polygon = document.createElementNS("http://www.w3.org/2000/svg", "polygon");
|
|
236
506
|
polygon.setAttribute("id", id);
|
|
@@ -243,5 +513,7 @@ function debugPolygon(polygon) {
|
|
|
243
513
|
const el = createPolygonElement();
|
|
244
514
|
const points = polygon.map((point2) => `${point2.x},${point2.y}`).join(" ");
|
|
245
515
|
el.setAttribute("points", points);
|
|
516
|
+
return () => {
|
|
517
|
+
el.remove();
|
|
518
|
+
};
|
|
246
519
|
}
|
|
247
|
-
//# sourceMappingURL=index.js.map
|