@zag-js/rect-utils 0.1.2 → 0.1.3
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 +361 -92
- package/dist/index.mjs +363 -92
- package/dist/intersection.d.ts +14 -0
- package/dist/operations.d.ts +0 -1
- package/dist/polygon.d.ts +5 -3
- package/dist/rect.d.ts +55 -59
- package/dist/types.d.ts +0 -1
- package/dist/union.d.ts +2 -0
- package/package.json +2 -1
- 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/index.mjs
CHANGED
|
@@ -1,93 +1,254 @@
|
|
|
1
|
-
|
|
2
|
-
var
|
|
3
|
-
var
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __defProps = Object.defineProperties;
|
|
3
|
+
var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
|
|
4
|
+
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __propIsEnum = Object.prototype.propertyIsEnumerable;
|
|
7
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
8
|
+
var __spreadValues = (a, b) => {
|
|
9
|
+
for (var prop in b || (b = {}))
|
|
10
|
+
if (__hasOwnProp.call(b, prop))
|
|
11
|
+
__defNormalProp(a, prop, b[prop]);
|
|
12
|
+
if (__getOwnPropSymbols)
|
|
13
|
+
for (var prop of __getOwnPropSymbols(b)) {
|
|
14
|
+
if (__propIsEnum.call(b, prop))
|
|
15
|
+
__defNormalProp(a, prop, b[prop]);
|
|
16
|
+
}
|
|
17
|
+
return a;
|
|
18
|
+
};
|
|
19
|
+
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
|
|
20
|
+
|
|
21
|
+
// src/align.ts
|
|
22
|
+
function hAlign(a, ref, h) {
|
|
23
|
+
let x = ref.minX;
|
|
24
|
+
if (h === "left-inside") {
|
|
25
|
+
x = ref.minX;
|
|
9
26
|
}
|
|
10
|
-
|
|
11
|
-
|
|
27
|
+
if (h === "left-outside") {
|
|
28
|
+
x = ref.minX - ref.width;
|
|
12
29
|
}
|
|
13
|
-
|
|
14
|
-
|
|
30
|
+
if (h === "right-inside") {
|
|
31
|
+
x = ref.maxX - ref.width;
|
|
15
32
|
}
|
|
16
|
-
|
|
17
|
-
|
|
33
|
+
if (h === "right-outside") {
|
|
34
|
+
x = ref.maxX;
|
|
18
35
|
}
|
|
19
|
-
|
|
20
|
-
|
|
36
|
+
if (h === "center") {
|
|
37
|
+
x = ref.midX - ref.width / 2;
|
|
21
38
|
}
|
|
22
|
-
|
|
23
|
-
|
|
39
|
+
return __spreadProps(__spreadValues({}, a), { x });
|
|
40
|
+
}
|
|
41
|
+
function vAlign(a, ref, v) {
|
|
42
|
+
let y = ref.minY;
|
|
43
|
+
if (v === "top-inside") {
|
|
44
|
+
y = ref.minY;
|
|
24
45
|
}
|
|
25
|
-
|
|
26
|
-
|
|
46
|
+
if (v === "top-outside") {
|
|
47
|
+
y = ref.minY - a.height;
|
|
27
48
|
}
|
|
28
|
-
|
|
29
|
-
|
|
49
|
+
if (v === "bottom-inside") {
|
|
50
|
+
y = ref.maxY - a.height;
|
|
30
51
|
}
|
|
31
|
-
|
|
32
|
-
|
|
52
|
+
if (v === "bottom-outside") {
|
|
53
|
+
y = ref.maxY;
|
|
33
54
|
}
|
|
34
|
-
|
|
35
|
-
|
|
55
|
+
if (v === "center") {
|
|
56
|
+
y = ref.midY - a.height / 2;
|
|
36
57
|
}
|
|
37
|
-
|
|
38
|
-
|
|
58
|
+
return __spreadProps(__spreadValues({}, a), { y });
|
|
59
|
+
}
|
|
60
|
+
function alignRect(a, ref, options) {
|
|
61
|
+
const { h, v } = options;
|
|
62
|
+
return vAlign(hAlign(a, ref, h), ref, v);
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
// ../core/dist/index.mjs
|
|
66
|
+
var hasProp = (obj, prop) => Object.prototype.hasOwnProperty.call(obj, prop);
|
|
67
|
+
|
|
68
|
+
// src/rect.ts
|
|
69
|
+
var point = (x, y) => ({ x, y });
|
|
70
|
+
function createRect(r) {
|
|
71
|
+
const { x, y, width, height } = r;
|
|
72
|
+
const midX = x + width / 2;
|
|
73
|
+
const midY = y + height / 2;
|
|
74
|
+
return {
|
|
75
|
+
x,
|
|
76
|
+
y,
|
|
77
|
+
width,
|
|
78
|
+
height,
|
|
79
|
+
minX: x,
|
|
80
|
+
minY: y,
|
|
81
|
+
maxX: x + width,
|
|
82
|
+
maxY: y + height,
|
|
83
|
+
midX,
|
|
84
|
+
midY,
|
|
85
|
+
center: point(midX, midY)
|
|
86
|
+
};
|
|
87
|
+
}
|
|
88
|
+
function isRect(v) {
|
|
89
|
+
return hasProp(v, "x") && hasProp(v, "y") && hasProp(v, "width") && hasProp(v, "height");
|
|
90
|
+
}
|
|
91
|
+
function getRectCenters(v) {
|
|
92
|
+
const top = point(v.midX, v.minY);
|
|
93
|
+
const right = point(v.maxX, v.midY);
|
|
94
|
+
const bottom = point(v.midX, v.maxY);
|
|
95
|
+
const left = point(v.minX, v.midY);
|
|
96
|
+
return { top, right, bottom, left };
|
|
97
|
+
}
|
|
98
|
+
function getRectCorners(v) {
|
|
99
|
+
const top = point(v.minX, v.minY);
|
|
100
|
+
const right = point(v.maxX, v.minY);
|
|
101
|
+
const bottom = point(v.maxX, v.maxY);
|
|
102
|
+
const left = point(v.minX, v.maxY);
|
|
103
|
+
return { top, right, bottom, left };
|
|
104
|
+
}
|
|
105
|
+
function getRectEdges(v) {
|
|
106
|
+
const c = getRectCorners(v);
|
|
107
|
+
const top = [c.top, c.right];
|
|
108
|
+
const right = [c.right, c.bottom];
|
|
109
|
+
const bottom = [c.left, c.bottom];
|
|
110
|
+
const left = [c.top, c.left];
|
|
111
|
+
return { top, right, bottom, left };
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
// src/intersection.ts
|
|
115
|
+
function intersects(a, b) {
|
|
116
|
+
return a.x < b.maxX && a.y < b.maxY && a.maxX > b.x && a.maxY > b.y;
|
|
117
|
+
}
|
|
118
|
+
function intersection(a, b) {
|
|
119
|
+
const x = Math.max(a.x, b.x);
|
|
120
|
+
const y = Math.max(a.y, b.y);
|
|
121
|
+
const x2 = Math.min(a.x + a.width, b.x + b.width);
|
|
122
|
+
const y2 = Math.min(a.y + a.height, b.y + b.height);
|
|
123
|
+
return createRect({ x, y, width: x2 - x, height: y2 - y });
|
|
124
|
+
}
|
|
125
|
+
function collisions(a, b) {
|
|
126
|
+
return {
|
|
127
|
+
top: a.minY <= b.minY,
|
|
128
|
+
right: a.maxX >= b.maxX,
|
|
129
|
+
bottom: a.maxY >= b.maxY,
|
|
130
|
+
left: a.minX <= b.minX
|
|
131
|
+
};
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
// src/distance.ts
|
|
135
|
+
function distance(a, b = { x: 0, y: 0 }) {
|
|
136
|
+
return Math.sqrt(Math.pow(a.x - b.x, 2) + Math.pow(a.y - b.y, 2));
|
|
137
|
+
}
|
|
138
|
+
function distanceFromPoint(r, p) {
|
|
139
|
+
let x = 0;
|
|
140
|
+
let y = 0;
|
|
141
|
+
if (p.x < r.x)
|
|
142
|
+
x = r.x - p.x;
|
|
143
|
+
else if (p.x > r.maxX)
|
|
144
|
+
x = p.x - r.maxX;
|
|
145
|
+
if (p.y < r.y)
|
|
146
|
+
y = r.y - p.y;
|
|
147
|
+
else if (p.y > r.maxY)
|
|
148
|
+
y = p.y - r.maxY;
|
|
149
|
+
return { x, y, value: distance({ x, y }) };
|
|
150
|
+
}
|
|
151
|
+
function distanceFromRect(a, b) {
|
|
152
|
+
if (intersects(a, b))
|
|
153
|
+
return { x: 0, y: 0, value: 0 };
|
|
154
|
+
const left = a.x < b.x ? a : b;
|
|
155
|
+
const right = b.x < a.x ? a : b;
|
|
156
|
+
const upper = a.y < b.y ? a : b;
|
|
157
|
+
const lower = b.y < a.y ? a : b;
|
|
158
|
+
let x = left.x === right.x ? 0 : right.x - left.maxX;
|
|
159
|
+
x = Math.max(0, x);
|
|
160
|
+
let y = upper.y === lower.y ? 0 : lower.y - upper.maxY;
|
|
161
|
+
y = Math.max(0, y);
|
|
162
|
+
return { x, y, value: distance({ x, y }) };
|
|
163
|
+
}
|
|
164
|
+
function distanceBtwEdges(a, b) {
|
|
165
|
+
return {
|
|
166
|
+
left: b.x - a.x,
|
|
167
|
+
top: b.y - a.y,
|
|
168
|
+
right: a.maxX - b.maxX,
|
|
169
|
+
bottom: a.maxY - b.maxY
|
|
170
|
+
};
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
// src/closest.ts
|
|
174
|
+
function closest(...pts) {
|
|
175
|
+
return (a) => {
|
|
176
|
+
const ds = pts.map((b) => distance(b, a));
|
|
177
|
+
const c = Math.min.apply(Math, ds);
|
|
178
|
+
return pts[ds.indexOf(c)];
|
|
179
|
+
};
|
|
180
|
+
}
|
|
181
|
+
function closestSideToRect(ref, r) {
|
|
182
|
+
if (r.maxX <= ref.minX) {
|
|
183
|
+
return "left";
|
|
39
184
|
}
|
|
40
|
-
|
|
41
|
-
return
|
|
185
|
+
if (r.minX >= ref.maxX) {
|
|
186
|
+
return "right";
|
|
42
187
|
}
|
|
43
|
-
|
|
44
|
-
return
|
|
188
|
+
if (r.maxY <= ref.minY) {
|
|
189
|
+
return "top";
|
|
45
190
|
}
|
|
46
|
-
|
|
47
|
-
return
|
|
191
|
+
if (r.minY >= ref.maxY) {
|
|
192
|
+
return "bottom";
|
|
48
193
|
}
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
194
|
+
return "left";
|
|
195
|
+
}
|
|
196
|
+
function closestSideToPoint(ref, p) {
|
|
197
|
+
const { x, y } = p;
|
|
198
|
+
const dl = x - ref.minX;
|
|
199
|
+
const dr = ref.maxX - x;
|
|
200
|
+
const dt = y - ref.minY;
|
|
201
|
+
const db = ref.maxY - y;
|
|
202
|
+
let closest2 = dl;
|
|
203
|
+
let side = "left";
|
|
204
|
+
if (dr < closest2) {
|
|
205
|
+
closest2 = dr;
|
|
206
|
+
side = "right";
|
|
55
207
|
}
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
const bottom = point(this.maxX, this.maxY);
|
|
60
|
-
const left = point(this.minX, this.maxY);
|
|
61
|
-
return { top, right, bottom, left };
|
|
208
|
+
if (dt < closest2) {
|
|
209
|
+
closest2 = dt;
|
|
210
|
+
side = "top";
|
|
62
211
|
}
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
const top = [c.top, c.right];
|
|
66
|
-
const right = [c.right, c.bottom];
|
|
67
|
-
const bottom = [c.left, c.bottom];
|
|
68
|
-
const left = [c.top, c.left];
|
|
69
|
-
return { top, right, bottom, left };
|
|
212
|
+
if (db < closest2) {
|
|
213
|
+
side = "bottom";
|
|
70
214
|
}
|
|
71
|
-
|
|
215
|
+
return side;
|
|
216
|
+
}
|
|
72
217
|
|
|
73
|
-
// src/
|
|
74
|
-
|
|
218
|
+
// src/contains.ts
|
|
219
|
+
function containsPoint(r, p) {
|
|
220
|
+
return r.minX <= p.x && p.x <= r.maxX && r.minY <= p.y && p.y <= r.maxY;
|
|
221
|
+
}
|
|
222
|
+
function containsRect(a, b) {
|
|
223
|
+
return Object.values(getRectCorners(b)).every((c) => containsPoint(a, c));
|
|
224
|
+
}
|
|
225
|
+
function contains(r, v) {
|
|
226
|
+
return isRect(v) ? containsRect(r, v) : containsPoint(r, v);
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
// ../dom/dist/index.mjs
|
|
230
|
+
function getCache() {
|
|
231
|
+
const g = globalThis;
|
|
232
|
+
g.__styleCache = g.__styleCache || /* @__PURE__ */ new WeakMap();
|
|
233
|
+
return g.__styleCache;
|
|
234
|
+
}
|
|
75
235
|
function getComputedStyle(el) {
|
|
76
236
|
var _a;
|
|
77
237
|
if (!el)
|
|
78
238
|
return {};
|
|
79
|
-
|
|
239
|
+
const cache = getCache();
|
|
240
|
+
let style = cache.get(el);
|
|
80
241
|
if (!style) {
|
|
81
242
|
const win = (_a = el == null ? void 0 : el.ownerDocument.defaultView) != null ? _a : window;
|
|
82
243
|
style = win.getComputedStyle(el);
|
|
83
|
-
|
|
244
|
+
cache.set(el, style);
|
|
84
245
|
}
|
|
85
246
|
return style;
|
|
86
247
|
}
|
|
87
248
|
|
|
88
249
|
// src/from-element.ts
|
|
89
250
|
function getElementRect(el, opts = {}) {
|
|
90
|
-
return
|
|
251
|
+
return createRect(getClientRect(el, opts));
|
|
91
252
|
}
|
|
92
253
|
function getClientRect(el, opts = {}) {
|
|
93
254
|
const { excludeScrollbar = false, excludeBorders = false } = opts;
|
|
@@ -114,45 +275,124 @@ function getClientRect(el, opts = {}) {
|
|
|
114
275
|
var px = (v) => parseFloat(v.replace("px", ""));
|
|
115
276
|
var sum = (...vals) => vals.reduce((sum2, v) => sum2 + (v ? px(v) : 0), 0);
|
|
116
277
|
|
|
117
|
-
//
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
function distance(a, b = { x: 0, y: 0 }) {
|
|
127
|
-
return Math.sqrt(Math.pow(a.x - b.x, 2) + Math.pow(a.y - b.y, 2));
|
|
278
|
+
// src/from-points.ts
|
|
279
|
+
function getRectFromPoints(...pts) {
|
|
280
|
+
const xs = pts.map((p) => p.x);
|
|
281
|
+
const ys = pts.map((p) => p.y);
|
|
282
|
+
const x = Math.min(...xs);
|
|
283
|
+
const y = Math.min(...ys);
|
|
284
|
+
const width = Math.max(...xs) - x;
|
|
285
|
+
const height = Math.max(...ys) - y;
|
|
286
|
+
return createRect({ x, y, width, height });
|
|
128
287
|
}
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
288
|
+
|
|
289
|
+
// src/union.ts
|
|
290
|
+
var { min, max } = Math;
|
|
291
|
+
function union(...rs) {
|
|
292
|
+
const pMin = {
|
|
293
|
+
x: min.apply(Math, rs.map((r) => r.minX)),
|
|
294
|
+
y: min.apply(Math, rs.map((r) => r.minY))
|
|
295
|
+
};
|
|
296
|
+
const pMax = {
|
|
297
|
+
x: max.apply(Math, rs.map((r) => r.maxX)),
|
|
298
|
+
y: max.apply(Math, rs.map((r) => r.maxY))
|
|
134
299
|
};
|
|
300
|
+
return getRectFromPoints(pMin, pMax);
|
|
135
301
|
}
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
302
|
+
|
|
303
|
+
// src/from-range.ts
|
|
304
|
+
function fromRange(range) {
|
|
305
|
+
let rs = [];
|
|
306
|
+
const rects = Array.from(range.getClientRects());
|
|
307
|
+
if (rects.length) {
|
|
308
|
+
rs = rs.concat(rects.map(createRect));
|
|
309
|
+
return union.apply(void 0, rs);
|
|
310
|
+
}
|
|
311
|
+
let start = range.startContainer;
|
|
312
|
+
if (start.nodeType === Node.TEXT_NODE) {
|
|
313
|
+
start = start.parentNode;
|
|
314
|
+
}
|
|
315
|
+
if (start instanceof HTMLElement) {
|
|
316
|
+
const r = getElementRect(start);
|
|
317
|
+
rs.push(__spreadProps(__spreadValues({}, r), { x: r.maxX, width: 0 }));
|
|
318
|
+
}
|
|
319
|
+
return union.apply(void 0, rs);
|
|
140
320
|
}
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
321
|
+
|
|
322
|
+
// src/from-rotation.ts
|
|
323
|
+
function toRad(d) {
|
|
324
|
+
return d % 360 * Math.PI / 180;
|
|
325
|
+
}
|
|
326
|
+
function rotate(a, d, c) {
|
|
327
|
+
const r = toRad(d);
|
|
328
|
+
const sin = Math.sin(r);
|
|
329
|
+
const cos = Math.cos(r);
|
|
330
|
+
const x = a.x - c.x;
|
|
331
|
+
const y = a.y - c.y;
|
|
144
332
|
return {
|
|
145
|
-
|
|
146
|
-
|
|
333
|
+
x: c.x + x * cos - y * sin,
|
|
334
|
+
y: c.y + x * sin + y * cos
|
|
147
335
|
};
|
|
148
336
|
}
|
|
337
|
+
function getRotationRect(r, deg) {
|
|
338
|
+
const rr = Object.values(getRectCorners(r)).map((p) => rotate(p, deg, r.center));
|
|
339
|
+
const xs = rr.map((p) => p.x);
|
|
340
|
+
const ys = rr.map((p) => p.y);
|
|
341
|
+
const minX = Math.min(...xs);
|
|
342
|
+
const minY = Math.min(...ys);
|
|
343
|
+
const maxX = Math.max(...xs);
|
|
344
|
+
const maxY = Math.max(...ys);
|
|
345
|
+
return createRect({
|
|
346
|
+
x: minX,
|
|
347
|
+
y: minY,
|
|
348
|
+
width: maxX - minX,
|
|
349
|
+
height: maxY - minY
|
|
350
|
+
});
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
// src/from-window.ts
|
|
354
|
+
function getWindowRect(win, opts = {}) {
|
|
355
|
+
return createRect(getViewportRect(win, opts));
|
|
356
|
+
}
|
|
357
|
+
function getViewportRect(win, opts) {
|
|
358
|
+
const { excludeScrollbar = false } = opts;
|
|
359
|
+
const { innerWidth: innerWidth2, innerHeight: innerHeight2, document: doc, visualViewport } = win;
|
|
360
|
+
const width = visualViewport.width || innerWidth2;
|
|
361
|
+
const height = visualViewport.height || innerHeight2;
|
|
362
|
+
const rect = { x: 0, y: 0, width, height };
|
|
363
|
+
if (excludeScrollbar) {
|
|
364
|
+
const scrollbarWidth = innerWidth2 - doc.documentElement.clientWidth;
|
|
365
|
+
const scrollbarHeight = innerHeight2 - doc.documentElement.clientHeight;
|
|
366
|
+
rect.width -= scrollbarWidth;
|
|
367
|
+
rect.height -= scrollbarHeight;
|
|
368
|
+
}
|
|
369
|
+
return rect;
|
|
370
|
+
}
|
|
371
|
+
|
|
372
|
+
// src/get-polygon.ts
|
|
373
|
+
function getElementPolygon(rectValue, placement) {
|
|
374
|
+
const rect = createRect(rectValue);
|
|
375
|
+
const { top, right, left, bottom } = getRectCorners(rect);
|
|
376
|
+
const [base] = placement.split("-");
|
|
377
|
+
return {
|
|
378
|
+
top: [left, top, right, bottom],
|
|
379
|
+
right: [top, right, bottom, left],
|
|
380
|
+
bottom: [top, left, bottom, right],
|
|
381
|
+
left: [right, top, left, bottom]
|
|
382
|
+
}[base];
|
|
383
|
+
}
|
|
149
384
|
|
|
150
385
|
// src/operations.ts
|
|
151
386
|
var isSymmetric = (v) => "dx" in v || "dy" in v;
|
|
152
387
|
function inset(r, i) {
|
|
153
388
|
const v = isSymmetric(i) ? { left: i.dx, right: i.dx, top: i.dy, bottom: i.dy } : i;
|
|
154
389
|
const { top = 0, right = 0, bottom = 0, left = 0 } = v;
|
|
155
|
-
return
|
|
390
|
+
return createRect({
|
|
391
|
+
x: r.x + left,
|
|
392
|
+
y: r.y + top,
|
|
393
|
+
width: r.width - left - right,
|
|
394
|
+
height: r.height - top - bottom
|
|
395
|
+
});
|
|
156
396
|
}
|
|
157
397
|
function expand(r, v) {
|
|
158
398
|
const value = typeof v === "number" ? { dx: -v, dy: -v } : v;
|
|
@@ -164,11 +404,16 @@ function shrink(r, v) {
|
|
|
164
404
|
}
|
|
165
405
|
function shift(r, o) {
|
|
166
406
|
const { x = 0, y = 0 } = o;
|
|
167
|
-
return
|
|
407
|
+
return createRect({
|
|
408
|
+
x: r.x + x,
|
|
409
|
+
y: r.y + y,
|
|
410
|
+
width: r.width,
|
|
411
|
+
height: r.height
|
|
412
|
+
});
|
|
168
413
|
}
|
|
169
414
|
|
|
170
415
|
// src/polygon.ts
|
|
171
|
-
function
|
|
416
|
+
function isPointInPolygon(polygon, point2) {
|
|
172
417
|
const { x, y } = point2;
|
|
173
418
|
let c = false;
|
|
174
419
|
for (let i = 0, j = polygon.length - 1; i < polygon.length; j = i++) {
|
|
@@ -210,19 +455,45 @@ function debugPolygon(polygon) {
|
|
|
210
455
|
const points = polygon.map((point2) => `${point2.x},${point2.y}`).join(" ");
|
|
211
456
|
el.setAttribute("points", points);
|
|
212
457
|
}
|
|
458
|
+
debugPolygon.remove = () => {
|
|
459
|
+
const el = document.getElementById("debug-polygon");
|
|
460
|
+
el == null ? void 0 : el.remove();
|
|
461
|
+
};
|
|
213
462
|
export {
|
|
214
|
-
|
|
463
|
+
alignRect,
|
|
215
464
|
closest,
|
|
465
|
+
closestSideToPoint,
|
|
466
|
+
closestSideToRect,
|
|
467
|
+
collisions,
|
|
468
|
+
contains,
|
|
469
|
+
containsPoint,
|
|
470
|
+
containsRect,
|
|
471
|
+
createRect,
|
|
216
472
|
debugPolygon,
|
|
217
473
|
distance,
|
|
474
|
+
distanceBtwEdges,
|
|
475
|
+
distanceFromPoint,
|
|
476
|
+
distanceFromRect,
|
|
218
477
|
expand,
|
|
478
|
+
fromRange,
|
|
479
|
+
getElementPolygon,
|
|
219
480
|
getElementRect,
|
|
220
|
-
|
|
481
|
+
getRectCenters,
|
|
482
|
+
getRectCorners,
|
|
483
|
+
getRectEdges,
|
|
484
|
+
getRectFromPoints,
|
|
485
|
+
getRotationRect,
|
|
486
|
+
getViewportRect,
|
|
487
|
+
getWindowRect,
|
|
221
488
|
inset,
|
|
489
|
+
intersection,
|
|
490
|
+
intersects,
|
|
491
|
+
isPointInPolygon,
|
|
492
|
+
isRect,
|
|
222
493
|
isSymmetric,
|
|
223
|
-
|
|
494
|
+
rotate,
|
|
224
495
|
shift,
|
|
225
496
|
shrink,
|
|
226
|
-
|
|
497
|
+
toRad,
|
|
498
|
+
union
|
|
227
499
|
};
|
|
228
|
-
//# sourceMappingURL=index.mjs.map
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { Rect } from "./rect";
|
|
2
|
+
import type { RectSide } from "./types";
|
|
3
|
+
/**
|
|
4
|
+
* Checks if a Rect intersects another Rect
|
|
5
|
+
*/
|
|
6
|
+
export declare function intersects(a: Rect, b: Rect): boolean;
|
|
7
|
+
/**
|
|
8
|
+
* Returns a new Rect that represents the intersection between two Rects
|
|
9
|
+
*/
|
|
10
|
+
export declare function intersection(a: Rect, b: Rect): Rect;
|
|
11
|
+
/**
|
|
12
|
+
* Returns whether two rects collide along each edge
|
|
13
|
+
*/
|
|
14
|
+
export declare function collisions(a: Rect, b: Rect): Record<RectSide, boolean>;
|
package/dist/operations.d.ts
CHANGED
|
@@ -5,4 +5,3 @@ export declare function inset(r: Rect, i: RectInset | SymmetricRectInset): Rect;
|
|
|
5
5
|
export declare function expand(r: Rect, v: number | SymmetricRectInset): Rect;
|
|
6
6
|
export declare function shrink(r: Rect, v: number | SymmetricRectInset): Rect;
|
|
7
7
|
export declare function shift(r: Rect, o: Partial<Point>): Rect;
|
|
8
|
-
//# sourceMappingURL=operations.d.ts.map
|
package/dist/polygon.d.ts
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
1
|
-
import { Point } from "./types";
|
|
2
|
-
export declare function
|
|
1
|
+
import type { Point } from "./types";
|
|
2
|
+
export declare function isPointInPolygon(polygon: Point[], point: Point): boolean;
|
|
3
3
|
export declare function debugPolygon(polygon: Point[]): void;
|
|
4
|
-
|
|
4
|
+
export declare namespace debugPolygon {
|
|
5
|
+
var remove: () => void;
|
|
6
|
+
}
|
package/dist/rect.d.ts
CHANGED
|
@@ -1,65 +1,61 @@
|
|
|
1
|
-
import { RectEdge, RectValue } from "./types";
|
|
2
|
-
export declare
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
get maxX(): number;
|
|
15
|
-
get minY(): number;
|
|
16
|
-
get midY(): number;
|
|
17
|
-
get maxY(): number;
|
|
18
|
-
get center(): {
|
|
1
|
+
import type { RectEdge, RectValue } from "./types";
|
|
2
|
+
export declare function createRect(r: RectValue): {
|
|
3
|
+
x: number;
|
|
4
|
+
y: number;
|
|
5
|
+
width: number;
|
|
6
|
+
height: number;
|
|
7
|
+
minX: number;
|
|
8
|
+
minY: number;
|
|
9
|
+
maxX: number;
|
|
10
|
+
maxY: number;
|
|
11
|
+
midX: number;
|
|
12
|
+
midY: number;
|
|
13
|
+
center: {
|
|
19
14
|
x: number;
|
|
20
15
|
y: number;
|
|
21
16
|
};
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
y: number;
|
|
30
|
-
};
|
|
31
|
-
bottom: {
|
|
32
|
-
x: number;
|
|
33
|
-
y: number;
|
|
34
|
-
};
|
|
35
|
-
left: {
|
|
36
|
-
x: number;
|
|
37
|
-
y: number;
|
|
38
|
-
};
|
|
17
|
+
};
|
|
18
|
+
export declare type Rect = ReturnType<typeof createRect>;
|
|
19
|
+
export declare function isRect(v: any): v is Rect;
|
|
20
|
+
export declare function getRectCenters(v: Rect): {
|
|
21
|
+
top: {
|
|
22
|
+
x: number;
|
|
23
|
+
y: number;
|
|
39
24
|
};
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
y: number;
|
|
44
|
-
};
|
|
45
|
-
right: {
|
|
46
|
-
x: number;
|
|
47
|
-
y: number;
|
|
48
|
-
};
|
|
49
|
-
bottom: {
|
|
50
|
-
x: number;
|
|
51
|
-
y: number;
|
|
52
|
-
};
|
|
53
|
-
left: {
|
|
54
|
-
x: number;
|
|
55
|
-
y: number;
|
|
56
|
-
};
|
|
25
|
+
right: {
|
|
26
|
+
x: number;
|
|
27
|
+
y: number;
|
|
57
28
|
};
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
29
|
+
bottom: {
|
|
30
|
+
x: number;
|
|
31
|
+
y: number;
|
|
32
|
+
};
|
|
33
|
+
left: {
|
|
34
|
+
x: number;
|
|
35
|
+
y: number;
|
|
36
|
+
};
|
|
37
|
+
};
|
|
38
|
+
export declare function getRectCorners(v: Rect): {
|
|
39
|
+
top: {
|
|
40
|
+
x: number;
|
|
41
|
+
y: number;
|
|
42
|
+
};
|
|
43
|
+
right: {
|
|
44
|
+
x: number;
|
|
45
|
+
y: number;
|
|
46
|
+
};
|
|
47
|
+
bottom: {
|
|
48
|
+
x: number;
|
|
49
|
+
y: number;
|
|
50
|
+
};
|
|
51
|
+
left: {
|
|
52
|
+
x: number;
|
|
53
|
+
y: number;
|
|
63
54
|
};
|
|
64
|
-
}
|
|
65
|
-
|
|
55
|
+
};
|
|
56
|
+
export declare function getRectEdges(v: Rect): {
|
|
57
|
+
top: RectEdge;
|
|
58
|
+
right: RectEdge;
|
|
59
|
+
bottom: RectEdge;
|
|
60
|
+
left: RectEdge;
|
|
61
|
+
};
|
package/dist/types.d.ts
CHANGED