@zag-js/rect-utils 1.34.1 → 1.35.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.
Files changed (94) hide show
  1. package/dist/affine-transform.d.mts +39 -0
  2. package/dist/affine-transform.d.ts +39 -0
  3. package/dist/affine-transform.js +189 -0
  4. package/dist/affine-transform.mjs +166 -0
  5. package/dist/align.d.mts +5 -0
  6. package/dist/align.d.ts +5 -0
  7. package/dist/align.js +51 -0
  8. package/dist/align.mjs +28 -0
  9. package/dist/angle.d.mts +5 -0
  10. package/dist/angle.d.ts +5 -0
  11. package/dist/angle.js +35 -0
  12. package/dist/angle.mjs +12 -0
  13. package/dist/chunk-QZ7TP4HQ.mjs +7 -0
  14. package/dist/clamp.d.mts +12 -0
  15. package/dist/clamp.d.ts +12 -0
  16. package/dist/clamp.js +51 -0
  17. package/dist/clamp.mjs +27 -0
  18. package/dist/closest.d.mts +7 -0
  19. package/dist/closest.d.ts +7 -0
  20. package/dist/closest.js +69 -0
  21. package/dist/closest.mjs +44 -0
  22. package/dist/compass.d.mts +7 -0
  23. package/dist/compass.d.ts +7 -0
  24. package/dist/compass.js +51 -0
  25. package/dist/compass.mjs +27 -0
  26. package/dist/constrain.d.mts +5 -0
  27. package/dist/constrain.d.ts +5 -0
  28. package/dist/constrain.js +39 -0
  29. package/dist/constrain.mjs +16 -0
  30. package/dist/contains.d.mts +7 -0
  31. package/dist/contains.d.ts +7 -0
  32. package/dist/contains.js +43 -0
  33. package/dist/contains.mjs +18 -0
  34. package/dist/distance.d.mts +11 -0
  35. package/dist/distance.d.ts +11 -0
  36. package/dist/distance.js +68 -0
  37. package/dist/distance.mjs +42 -0
  38. package/dist/equality.d.mts +7 -0
  39. package/dist/equality.d.ts +7 -0
  40. package/dist/equality.js +42 -0
  41. package/dist/equality.mjs +17 -0
  42. package/dist/from-element.d.mts +15 -0
  43. package/dist/from-element.d.ts +15 -0
  44. package/dist/from-element.js +65 -0
  45. package/dist/from-element.mjs +42 -0
  46. package/dist/from-points.d.mts +5 -0
  47. package/dist/from-points.d.ts +5 -0
  48. package/dist/from-points.js +39 -0
  49. package/dist/from-points.mjs +16 -0
  50. package/dist/from-range.d.mts +5 -0
  51. package/dist/from-range.d.ts +5 -0
  52. package/dist/from-range.js +49 -0
  53. package/dist/from-range.mjs +26 -0
  54. package/dist/from-rotation.d.mts +7 -0
  55. package/dist/from-rotation.d.ts +7 -0
  56. package/dist/from-rotation.js +63 -0
  57. package/dist/from-rotation.mjs +38 -0
  58. package/dist/from-window.d.mts +23 -0
  59. package/dist/from-window.d.ts +23 -0
  60. package/dist/from-window.js +49 -0
  61. package/dist/from-window.mjs +25 -0
  62. package/dist/index.d.mts +22 -253
  63. package/dist/index.d.ts +22 -253
  64. package/dist/index.js +62 -772
  65. package/dist/index.mjs +22 -724
  66. package/dist/intersection.d.mts +16 -0
  67. package/dist/intersection.d.ts +16 -0
  68. package/dist/intersection.js +52 -0
  69. package/dist/intersection.mjs +27 -0
  70. package/dist/operations.d.mts +9 -0
  71. package/dist/operations.d.ts +9 -0
  72. package/dist/operations.js +66 -0
  73. package/dist/operations.mjs +39 -0
  74. package/dist/polygon.d.mts +10 -0
  75. package/dist/polygon.d.ts +10 -0
  76. package/dist/polygon.js +91 -0
  77. package/dist/polygon.mjs +66 -0
  78. package/dist/rect.d.mts +58 -0
  79. package/dist/rect.d.ts +58 -0
  80. package/dist/rect.js +97 -0
  81. package/dist/rect.mjs +66 -0
  82. package/dist/resize.d.mts +6 -0
  83. package/dist/resize.d.ts +6 -0
  84. package/dist/resize.js +113 -0
  85. package/dist/resize.mjs +90 -0
  86. package/dist/types.d.mts +55 -0
  87. package/dist/types.d.ts +55 -0
  88. package/dist/types.js +18 -0
  89. package/dist/types.mjs +0 -0
  90. package/dist/union.d.mts +5 -0
  91. package/dist/union.d.ts +5 -0
  92. package/dist/union.js +42 -0
  93. package/dist/union.mjs +19 -0
  94. package/package.json +1 -1
package/dist/index.js CHANGED
@@ -1,774 +1,64 @@
1
- 'use strict';
2
-
1
+ "use strict";
3
2
  var __defProp = Object.defineProperty;
4
- var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
5
- var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
6
-
7
- // src/affine-transform.ts
8
- var AffineTransform = class _AffineTransform {
9
- constructor([m00, m01, m02, m10, m11, m12] = [0, 0, 0, 0, 0, 0]) {
10
- __publicField(this, "m00");
11
- __publicField(this, "m01");
12
- __publicField(this, "m02");
13
- __publicField(this, "m10");
14
- __publicField(this, "m11");
15
- __publicField(this, "m12");
16
- __publicField(this, "rotate", (...args) => {
17
- return this.prepend(_AffineTransform.rotate(...args));
18
- });
19
- __publicField(this, "scale", (...args) => {
20
- return this.prepend(_AffineTransform.scale(...args));
21
- });
22
- __publicField(this, "translate", (...args) => {
23
- return this.prepend(_AffineTransform.translate(...args));
24
- });
25
- this.m00 = m00;
26
- this.m01 = m01;
27
- this.m02 = m02;
28
- this.m10 = m10;
29
- this.m11 = m11;
30
- this.m12 = m12;
31
- }
32
- applyTo(point) {
33
- const { x, y } = point;
34
- const { m00, m01, m02, m10, m11, m12 } = this;
35
- return {
36
- x: m00 * x + m01 * y + m02,
37
- y: m10 * x + m11 * y + m12
38
- };
39
- }
40
- prepend(other) {
41
- return new _AffineTransform([
42
- this.m00 * other.m00 + this.m01 * other.m10,
43
- // m00
44
- this.m00 * other.m01 + this.m01 * other.m11,
45
- // m01
46
- this.m00 * other.m02 + this.m01 * other.m12 + this.m02,
47
- // m02
48
- this.m10 * other.m00 + this.m11 * other.m10,
49
- // m10
50
- this.m10 * other.m01 + this.m11 * other.m11,
51
- // m11
52
- this.m10 * other.m02 + this.m11 * other.m12 + this.m12
53
- // m12
54
- ]);
55
- }
56
- append(other) {
57
- return new _AffineTransform([
58
- other.m00 * this.m00 + other.m01 * this.m10,
59
- // m00
60
- other.m00 * this.m01 + other.m01 * this.m11,
61
- // m01
62
- other.m00 * this.m02 + other.m01 * this.m12 + other.m02,
63
- // m02
64
- other.m10 * this.m00 + other.m11 * this.m10,
65
- // m10
66
- other.m10 * this.m01 + other.m11 * this.m11,
67
- // m11
68
- other.m10 * this.m02 + other.m11 * this.m12 + other.m12
69
- // m12
70
- ]);
71
- }
72
- get determinant() {
73
- return this.m00 * this.m11 - this.m01 * this.m10;
74
- }
75
- get isInvertible() {
76
- const det = this.determinant;
77
- return isFinite(det) && isFinite(this.m02) && isFinite(this.m12) && det !== 0;
78
- }
79
- invert() {
80
- const det = this.determinant;
81
- return new _AffineTransform([
82
- this.m11 / det,
83
- // m00
84
- -this.m01 / det,
85
- // m01
86
- (this.m01 * this.m12 - this.m11 * this.m02) / det,
87
- // m02
88
- -this.m10 / det,
89
- // m10
90
- this.m00 / det,
91
- // m11
92
- (this.m10 * this.m02 - this.m00 * this.m12) / det
93
- // m12
94
- ]);
95
- }
96
- get array() {
97
- return [this.m00, this.m01, this.m02, this.m10, this.m11, this.m12, 0, 0, 1];
98
- }
99
- get float32Array() {
100
- return new Float32Array(this.array);
101
- }
102
- // Static
103
- static get identity() {
104
- return new _AffineTransform([1, 0, 0, 0, 1, 0]);
105
- }
106
- static rotate(theta, origin) {
107
- const rotation = new _AffineTransform([Math.cos(theta), -Math.sin(theta), 0, Math.sin(theta), Math.cos(theta), 0]);
108
- if (origin && (origin.x !== 0 || origin.y !== 0)) {
109
- return _AffineTransform.multiply(
110
- _AffineTransform.translate(origin.x, origin.y),
111
- rotation,
112
- _AffineTransform.translate(-origin.x, -origin.y)
113
- );
114
- }
115
- return rotation;
116
- }
117
- static scale(sx, sy = sx, origin = { x: 0, y: 0 }) {
118
- const scale = new _AffineTransform([sx, 0, 0, 0, sy, 0]);
119
- if (origin.x !== 0 || origin.y !== 0) {
120
- return _AffineTransform.multiply(
121
- _AffineTransform.translate(origin.x, origin.y),
122
- scale,
123
- _AffineTransform.translate(-origin.x, -origin.y)
124
- );
125
- }
126
- return scale;
127
- }
128
- static translate(tx, ty) {
129
- return new _AffineTransform([1, 0, tx, 0, 1, ty]);
130
- }
131
- static multiply(...[first, ...rest]) {
132
- if (!first) return _AffineTransform.identity;
133
- return rest.reduce((result, item) => result.prepend(item), first);
134
- }
135
- get a() {
136
- return this.m00;
137
- }
138
- get b() {
139
- return this.m10;
140
- }
141
- get c() {
142
- return this.m01;
143
- }
144
- get d() {
145
- return this.m11;
146
- }
147
- get tx() {
148
- return this.m02;
149
- }
150
- get ty() {
151
- return this.m12;
152
- }
153
- get scaleComponents() {
154
- return { x: this.a, y: this.d };
155
- }
156
- get translationComponents() {
157
- return { x: this.tx, y: this.ty };
158
- }
159
- get skewComponents() {
160
- return { x: this.c, y: this.b };
161
- }
162
- toString() {
163
- return `matrix(${this.a}, ${this.b}, ${this.c}, ${this.d}, ${this.tx}, ${this.ty})`;
164
- }
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __copyProps = (to, from, except, desc) => {
7
+ if (from && typeof from === "object" || typeof from === "function") {
8
+ for (let key of __getOwnPropNames(from))
9
+ if (!__hasOwnProp.call(to, key) && key !== except)
10
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
11
+ }
12
+ return to;
165
13
  };
166
-
167
- // src/align.ts
168
- function hAlign(a, ref, h) {
169
- let x = ref.minX;
170
- if (h === "left-inside") x = ref.minX;
171
- if (h === "left-outside") x = ref.minX - ref.width;
172
- if (h === "right-inside") x = ref.maxX - ref.width;
173
- if (h === "right-outside") x = ref.maxX;
174
- if (h === "center") x = ref.midX - ref.width / 2;
175
- return { ...a, x };
176
- }
177
- function vAlign(a, ref, v) {
178
- let y = ref.minY;
179
- if (v === "top-inside") y = ref.minY;
180
- if (v === "top-outside") y = ref.minY - a.height;
181
- if (v === "bottom-inside") y = ref.maxY - a.height;
182
- if (v === "bottom-outside") y = ref.maxY;
183
- if (v === "center") y = ref.midY - a.height / 2;
184
- return { ...a, y };
185
- }
186
- function alignRect(a, ref, options) {
187
- const { h, v } = options;
188
- return vAlign(hAlign(a, ref, h), ref, v);
189
- }
190
-
191
- // src/angle.ts
192
- function getPointAngle(rect, point, reference = rect.center) {
193
- const x = point.x - reference.x;
194
- const y = point.y - reference.y;
195
- const deg = Math.atan2(x, y) * (180 / Math.PI) + 180;
196
- return 360 - deg;
197
- }
198
-
199
- // src/clamp.ts
200
- var clamp = (value, min3, max2) => Math.min(Math.max(value, min3), max2);
201
- var clampPoint = (position, size, boundaryRect) => {
202
- const x = clamp(position.x, boundaryRect.x, boundaryRect.x + boundaryRect.width - size.width);
203
- const y = clamp(position.y, boundaryRect.y, boundaryRect.y + boundaryRect.height - size.height);
204
- return { x, y };
205
- };
206
- var defaultMinSize = {
207
- width: 0,
208
- height: 0
209
- };
210
- var defaultMaxSize = {
211
- width: Infinity,
212
- height: Infinity
213
- };
214
- var clampSize = (size, minSize = defaultMinSize, maxSize = defaultMaxSize) => {
215
- return {
216
- width: Math.min(Math.max(size.width, minSize.width), maxSize.width),
217
- height: Math.min(Math.max(size.height, minSize.height), maxSize.height)
218
- };
219
- };
220
-
221
- // src/rect.ts
222
- var createPoint = (x, y) => ({ x, y });
223
- var subtractPoints = (a, b) => {
224
- if (!b) return a;
225
- return createPoint(a.x - b.x, a.y - b.y);
226
- };
227
- var addPoints = (a, b) => createPoint(a.x + b.x, a.y + b.y);
228
- function isPoint(v) {
229
- return Reflect.has(v, "x") && Reflect.has(v, "y");
230
- }
231
- function createRect(r) {
232
- const { x, y, width, height } = r;
233
- const midX = x + width / 2;
234
- const midY = y + height / 2;
235
- return {
236
- x,
237
- y,
238
- width,
239
- height,
240
- minX: x,
241
- minY: y,
242
- maxX: x + width,
243
- maxY: y + height,
244
- midX,
245
- midY,
246
- center: createPoint(midX, midY)
247
- };
248
- }
249
- function isRect(v) {
250
- return Reflect.has(v, "x") && Reflect.has(v, "y") && Reflect.has(v, "width") && Reflect.has(v, "height");
251
- }
252
- function getRectCenters(v) {
253
- const top = createPoint(v.midX, v.minY);
254
- const right = createPoint(v.maxX, v.midY);
255
- const bottom = createPoint(v.midX, v.maxY);
256
- const left = createPoint(v.minX, v.midY);
257
- return { top, right, bottom, left };
258
- }
259
- function getRectCorners(v) {
260
- const top = createPoint(v.minX, v.minY);
261
- const right = createPoint(v.maxX, v.minY);
262
- const bottom = createPoint(v.maxX, v.maxY);
263
- const left = createPoint(v.minX, v.maxY);
264
- return { top, right, bottom, left };
265
- }
266
- function getRectEdges(v) {
267
- const c = getRectCorners(v);
268
- const top = [c.top, c.right];
269
- const right = [c.right, c.bottom];
270
- const bottom = [c.left, c.bottom];
271
- const left = [c.top, c.left];
272
- return { top, right, bottom, left };
273
- }
274
-
275
- // src/intersection.ts
276
- function intersects(a, b) {
277
- return a.x < b.maxX && a.y < b.maxY && a.maxX > b.x && a.maxY > b.y;
278
- }
279
- function intersection(a, b) {
280
- const x = Math.max(a.x, b.x);
281
- const y = Math.max(a.y, b.y);
282
- const x2 = Math.min(a.x + a.width, b.x + b.width);
283
- const y2 = Math.min(a.y + a.height, b.y + b.height);
284
- return createRect({ x, y, width: x2 - x, height: y2 - y });
285
- }
286
- function collisions(a, b) {
287
- return {
288
- top: a.minY <= b.minY,
289
- right: a.maxX >= b.maxX,
290
- bottom: a.maxY >= b.maxY,
291
- left: a.minX <= b.minX
292
- };
293
- }
294
-
295
- // src/distance.ts
296
- function distance(a, b = { x: 0, y: 0 }) {
297
- return Math.sqrt(Math.pow(a.x - b.x, 2) + Math.pow(a.y - b.y, 2));
298
- }
299
- function distanceFromPoint(r, p) {
300
- let x = 0;
301
- let y = 0;
302
- if (p.x < r.x) x = r.x - p.x;
303
- else if (p.x > r.maxX) x = p.x - r.maxX;
304
- if (p.y < r.y) y = r.y - p.y;
305
- else if (p.y > r.maxY) y = p.y - r.maxY;
306
- return { x, y, value: distance({ x, y }) };
307
- }
308
- function distanceFromRect(a, b) {
309
- if (intersects(a, b)) return { x: 0, y: 0, value: 0 };
310
- const left = a.x < b.x ? a : b;
311
- const right = b.x < a.x ? a : b;
312
- const upper = a.y < b.y ? a : b;
313
- const lower = b.y < a.y ? a : b;
314
- let x = left.x === right.x ? 0 : right.x - left.maxX;
315
- x = Math.max(0, x);
316
- let y = upper.y === lower.y ? 0 : lower.y - upper.maxY;
317
- y = Math.max(0, y);
318
- return { x, y, value: distance({ x, y }) };
319
- }
320
- function distanceBtwEdges(a, b) {
321
- return {
322
- left: b.x - a.x,
323
- top: b.y - a.y,
324
- right: a.maxX - b.maxX,
325
- bottom: a.maxY - b.maxY
326
- };
327
- }
328
-
329
- // src/closest.ts
330
- function closest(...pts) {
331
- return (a) => {
332
- const ds = pts.map((b) => distance(b, a));
333
- const c = Math.min.apply(Math, ds);
334
- return pts[ds.indexOf(c)];
335
- };
336
- }
337
- function closestSideToRect(ref, r) {
338
- if (r.maxX <= ref.minX) return "left";
339
- if (r.minX >= ref.maxX) return "right";
340
- if (r.maxY <= ref.minY) return "top";
341
- if (r.minY >= ref.maxY) return "bottom";
342
- return "left";
343
- }
344
- function closestSideToPoint(ref, p) {
345
- const { x, y } = p;
346
- const dl = x - ref.minX;
347
- const dr = ref.maxX - x;
348
- const dt = y - ref.minY;
349
- const db = ref.maxY - y;
350
- let closest2 = dl;
351
- let side = "left";
352
- if (dr < closest2) {
353
- closest2 = dr;
354
- side = "right";
355
- }
356
- if (dt < closest2) {
357
- closest2 = dt;
358
- side = "top";
359
- }
360
- if (db < closest2) {
361
- side = "bottom";
362
- }
363
- return side;
364
- }
365
-
366
- // src/constrain.ts
367
- var constrainRect = (rect, boundary) => {
368
- const left = Math.max(boundary.x, Math.min(rect.x, boundary.x + boundary.width - rect.width));
369
- const top = Math.max(boundary.y, Math.min(rect.y, boundary.y + boundary.height - rect.height));
370
- return {
371
- x: left,
372
- y: top,
373
- width: Math.min(rect.width, boundary.width),
374
- height: Math.min(rect.height, boundary.height)
375
- };
376
- };
377
-
378
- // src/contains.ts
379
- function containsPoint(r, p) {
380
- return r.minX <= p.x && p.x <= r.maxX && r.minY <= p.y && p.y <= r.maxY;
381
- }
382
- function containsRect(a, b) {
383
- return Object.values(getRectCorners(b)).every((c) => containsPoint(a, c));
384
- }
385
- function contains(r, v) {
386
- return isRect(v) ? containsRect(r, v) : containsPoint(r, v);
387
- }
388
-
389
- // src/equality.ts
390
- var isSizeEqual = (a, b) => {
391
- return a.width === b?.width && a.height === b?.height;
392
- };
393
- var isPointEqual = (a, b) => {
394
- return a.x === b?.x && a.y === b?.y;
395
- };
396
- var isRectEqual = (a, b) => {
397
- return isPointEqual(a, b) && isSizeEqual(a, b);
398
- };
399
-
400
- // src/from-element.ts
401
- var styleCache = /* @__PURE__ */ new WeakMap();
402
- function getCacheComputedStyle(el) {
403
- if (!styleCache.has(el)) {
404
- const win = el.ownerDocument.defaultView || window;
405
- styleCache.set(el, win.getComputedStyle(el));
406
- }
407
- return styleCache.get(el);
408
- }
409
- function getElementRect(el, opts = {}) {
410
- return createRect(getClientRect(el, opts));
411
- }
412
- function getClientRect(el, opts = {}) {
413
- const { excludeScrollbar = false, excludeBorders = false } = opts;
414
- const { x, y, width, height } = el.getBoundingClientRect();
415
- const r = { x, y, width, height };
416
- const style = getCacheComputedStyle(el);
417
- const { borderLeftWidth, borderTopWidth, borderRightWidth, borderBottomWidth } = style;
418
- const borderXWidth = sum(borderLeftWidth, borderRightWidth);
419
- const borderYWidth = sum(borderTopWidth, borderBottomWidth);
420
- if (excludeBorders) {
421
- r.width -= borderXWidth;
422
- r.height -= borderYWidth;
423
- r.x += px(borderLeftWidth);
424
- r.y += px(borderTopWidth);
425
- }
426
- if (excludeScrollbar) {
427
- const scrollbarWidth = el.offsetWidth - el.clientWidth - borderXWidth;
428
- const scrollbarHeight = el.offsetHeight - el.clientHeight - borderYWidth;
429
- r.width -= scrollbarWidth;
430
- r.height -= scrollbarHeight;
431
- }
432
- return r;
433
- }
434
- var px = (v) => parseFloat(v.replace("px", ""));
435
- var sum = (...vals) => vals.reduce((sum2, v) => sum2 + (v ? px(v) : 0), 0);
436
-
437
- // src/from-points.ts
438
- function getRectFromPoints(...pts) {
439
- const xs = pts.map((p) => p.x);
440
- const ys = pts.map((p) => p.y);
441
- const x = Math.min(...xs);
442
- const y = Math.min(...ys);
443
- const width = Math.max(...xs) - x;
444
- const height = Math.max(...ys) - y;
445
- return createRect({ x, y, width, height });
446
- }
447
-
448
- // src/union.ts
449
- var { min, max } = Math;
450
- function union(...rs) {
451
- const pMin = {
452
- x: min(...rs.map((r) => r.minX)),
453
- y: min(...rs.map((r) => r.minY))
454
- };
455
- const pMax = {
456
- x: max(...rs.map((r) => r.maxX)),
457
- y: max(...rs.map((r) => r.maxY))
458
- };
459
- return getRectFromPoints(pMin, pMax);
460
- }
461
-
462
- // src/from-range.ts
463
- function fromRange(range) {
464
- let rs = [];
465
- const rects = Array.from(range.getClientRects());
466
- if (rects.length) {
467
- rs = rs.concat(rects.map(createRect));
468
- return union.apply(void 0, rs);
469
- }
470
- let start = range.startContainer;
471
- if (start.nodeType === Node.TEXT_NODE) {
472
- start = start.parentNode;
473
- }
474
- if (start instanceof HTMLElement) {
475
- const r = getElementRect(start);
476
- rs.push({ ...r, x: r.maxX, width: 0 });
477
- }
478
- return union.apply(void 0, rs);
479
- }
480
-
481
- // src/from-rotation.ts
482
- function toRad(d) {
483
- return d % 360 * Math.PI / 180;
484
- }
485
- function rotate(a, d, c) {
486
- const r = toRad(d);
487
- const sin = Math.sin(r);
488
- const cos = Math.cos(r);
489
- const x = a.x - c.x;
490
- const y = a.y - c.y;
491
- return {
492
- x: c.x + x * cos - y * sin,
493
- y: c.y + x * sin + y * cos
494
- };
495
- }
496
- function getRotationRect(r, deg) {
497
- const rr = Object.values(getRectCorners(r)).map((p) => rotate(p, deg, r.center));
498
- const xs = rr.map((p) => p.x);
499
- const ys = rr.map((p) => p.y);
500
- const minX = Math.min(...xs);
501
- const minY = Math.min(...ys);
502
- const maxX = Math.max(...xs);
503
- const maxY = Math.max(...ys);
504
- return createRect({
505
- x: minX,
506
- y: minY,
507
- width: maxX - minX,
508
- height: maxY - minY
509
- });
510
- }
511
-
512
- // src/from-window.ts
513
- function getWindowRect(win, opts = {}) {
514
- return createRect(getViewportRect(win, opts));
515
- }
516
- function getViewportRect(win, opts) {
517
- const { excludeScrollbar = false } = opts;
518
- const { innerWidth, innerHeight, document: doc, visualViewport } = win;
519
- const width = visualViewport?.width || innerWidth;
520
- const height = visualViewport?.height || innerHeight;
521
- const rect = { x: 0, y: 0, width, height };
522
- if (excludeScrollbar) {
523
- const scrollbarWidth = innerWidth - doc.documentElement.clientWidth;
524
- const scrollbarHeight = innerHeight - doc.documentElement.clientHeight;
525
- rect.width -= scrollbarWidth;
526
- rect.height -= scrollbarHeight;
527
- }
528
- return rect;
529
- }
530
-
531
- // src/operations.ts
532
- var isSymmetric = (v) => "dx" in v || "dy" in v;
533
- function inset(r, i) {
534
- const v = isSymmetric(i) ? { left: i.dx, right: i.dx, top: i.dy, bottom: i.dy } : i;
535
- const { top = 0, right = 0, bottom = 0, left = 0 } = v;
536
- return createRect({
537
- x: r.x + left,
538
- y: r.y + top,
539
- width: r.width - left - right,
540
- height: r.height - top - bottom
541
- });
542
- }
543
- function expand(r, v) {
544
- const value = typeof v === "number" ? { dx: -v, dy: -v } : v;
545
- return inset(r, value);
546
- }
547
- function shrink(r, v) {
548
- const value = typeof v === "number" ? { dx: -v, dy: -v } : v;
549
- return inset(r, value);
550
- }
551
- function shift(r, o) {
552
- const { x = 0, y = 0 } = o;
553
- return createRect({
554
- x: r.x + x,
555
- y: r.y + y,
556
- width: r.width,
557
- height: r.height
558
- });
559
- }
560
-
561
- // src/polygon.ts
562
- function getElementPolygon(rectValue, placement) {
563
- const rect = createRect(rectValue);
564
- const { top, right, left, bottom } = getRectCorners(rect);
565
- const [base] = placement.split("-");
566
- return {
567
- top: [left, top, right, bottom],
568
- right: [top, right, bottom, left],
569
- bottom: [top, left, bottom, right],
570
- left: [right, top, left, bottom]
571
- }[base];
572
- }
573
- function isPointInPolygon(polygon, point) {
574
- const { x, y } = point;
575
- let c = false;
576
- for (let i = 0, j = polygon.length - 1; i < polygon.length; j = i++) {
577
- const xi = polygon[i].x;
578
- const yi = polygon[i].y;
579
- const xj = polygon[j].x;
580
- const yj = polygon[j].y;
581
- if (yi > y !== yj > y && x < (xj - xi) * (y - yi) / (yj - yi) + xi) {
582
- c = !c;
583
- }
584
- }
585
- return c;
586
- }
587
- function createPolygonElement() {
588
- const id = "debug-polygon";
589
- const existingPolygon = document.getElementById(id);
590
- if (existingPolygon) {
591
- return existingPolygon;
592
- }
593
- const svg = document.createElementNS("http://www.w3.org/2000/svg", "svg");
594
- Object.assign(svg.style, {
595
- top: "0",
596
- left: "0",
597
- width: "100%",
598
- height: "100%",
599
- opacity: "0.15",
600
- position: "fixed",
601
- pointerEvents: "none",
602
- fill: "red"
603
- });
604
- const polygon = document.createElementNS("http://www.w3.org/2000/svg", "polygon");
605
- polygon.setAttribute("id", id);
606
- polygon.setAttribute("points", "0,0 0,0");
607
- svg.appendChild(polygon);
608
- document.body.appendChild(svg);
609
- return polygon;
610
- }
611
- function debugPolygon(polygon) {
612
- const el = createPolygonElement();
613
- const points = polygon.map((point) => `${point.x},${point.y}`).join(" ");
614
- el.setAttribute("points", points);
615
- return () => {
616
- el.remove();
617
- };
618
- }
619
-
620
- // src/compass.ts
621
- var compassDirectionMap = {
622
- n: { x: 0.5, y: 0 },
623
- ne: { x: 1, y: 0 },
624
- e: { x: 1, y: 0.5 },
625
- se: { x: 1, y: 1 },
626
- s: { x: 0.5, y: 1 },
627
- sw: { x: 0, y: 1 },
628
- w: { x: 0, y: 0.5 },
629
- nw: { x: 0, y: 0 }
630
- };
631
- var oppositeDirectionMap = {
632
- n: "s",
633
- ne: "sw",
634
- e: "w",
635
- se: "nw",
636
- s: "n",
637
- sw: "ne",
638
- w: "e",
639
- nw: "se"
640
- };
641
-
642
- // src/resize.ts
643
- var { sign, abs, min: min2 } = Math;
644
- function getRectExtentPoint(rect, direction) {
645
- const { minX, minY, maxX, maxY, midX, midY } = rect;
646
- const x = direction.includes("w") ? minX : direction.includes("e") ? maxX : midX;
647
- const y = direction.includes("n") ? minY : direction.includes("s") ? maxY : midY;
648
- return { x, y };
649
- }
650
- function getOppositeDirection(direction) {
651
- return oppositeDirectionMap[direction];
652
- }
653
- function resizeRect(rect, offset, direction, opts) {
654
- const { scalingOriginMode, lockAspectRatio } = opts;
655
- const extent = getRectExtentPoint(rect, direction);
656
- const oppositeDirection = getOppositeDirection(direction);
657
- const oppositeExtent = getRectExtentPoint(rect, oppositeDirection);
658
- if (scalingOriginMode === "center") {
659
- offset = { x: offset.x * 2, y: offset.y * 2 };
660
- }
661
- const newExtent = {
662
- x: extent.x + offset.x,
663
- y: extent.y + offset.y
664
- };
665
- const multiplier = {
666
- x: compassDirectionMap[direction].x * 2 - 1,
667
- y: compassDirectionMap[direction].y * 2 - 1
668
- };
669
- const newSize = {
670
- width: newExtent.x - oppositeExtent.x,
671
- height: newExtent.y - oppositeExtent.y
672
- };
673
- const scaleX = multiplier.x * newSize.width / rect.width;
674
- const scaleY = multiplier.y * newSize.height / rect.height;
675
- const largestMagnitude = abs(scaleX) > abs(scaleY) ? scaleX : scaleY;
676
- const scale = lockAspectRatio ? { x: largestMagnitude, y: largestMagnitude } : {
677
- x: extent.x === oppositeExtent.x ? 1 : scaleX,
678
- y: extent.y === oppositeExtent.y ? 1 : scaleY
679
- };
680
- if (extent.y === oppositeExtent.y) {
681
- scale.y = abs(scale.y);
682
- } else if (sign(scale.y) !== sign(scaleY)) {
683
- scale.y *= -1;
684
- }
685
- if (extent.x === oppositeExtent.x) {
686
- scale.x = abs(scale.x);
687
- } else if (sign(scale.x) !== sign(scaleX)) {
688
- scale.x *= -1;
689
- }
690
- switch (scalingOriginMode) {
691
- case "extent":
692
- return transformRect(rect, AffineTransform.scale(scale.x, scale.y, oppositeExtent), false);
693
- case "center":
694
- return transformRect(
695
- rect,
696
- AffineTransform.scale(scale.x, scale.y, {
697
- x: rect.midX,
698
- y: rect.midY
699
- }),
700
- false
701
- );
702
- }
703
- }
704
- function createRectFromPoints(initialPoint, finalPoint, normalized = true) {
705
- if (normalized) {
706
- return {
707
- x: min2(finalPoint.x, initialPoint.x),
708
- y: min2(finalPoint.y, initialPoint.y),
709
- width: abs(finalPoint.x - initialPoint.x),
710
- height: abs(finalPoint.y - initialPoint.y)
711
- };
712
- }
713
- return {
714
- x: initialPoint.x,
715
- y: initialPoint.y,
716
- width: finalPoint.x - initialPoint.x,
717
- height: finalPoint.y - initialPoint.y
718
- };
719
- }
720
- function transformRect(rect, transform, normalized = true) {
721
- const p1 = transform.applyTo({ x: rect.minX, y: rect.minY });
722
- const p2 = transform.applyTo({ x: rect.maxX, y: rect.maxY });
723
- return createRectFromPoints(p1, p2, normalized);
724
- }
725
-
726
- exports.AffineTransform = AffineTransform;
727
- exports.addPoints = addPoints;
728
- exports.alignRect = alignRect;
729
- exports.clampPoint = clampPoint;
730
- exports.clampSize = clampSize;
731
- exports.closest = closest;
732
- exports.closestSideToPoint = closestSideToPoint;
733
- exports.closestSideToRect = closestSideToRect;
734
- exports.collisions = collisions;
735
- exports.constrainRect = constrainRect;
736
- exports.contains = contains;
737
- exports.containsPoint = containsPoint;
738
- exports.containsRect = containsRect;
739
- exports.createPoint = createPoint;
740
- exports.createRect = createRect;
741
- exports.debugPolygon = debugPolygon;
742
- exports.distance = distance;
743
- exports.distanceBtwEdges = distanceBtwEdges;
744
- exports.distanceFromPoint = distanceFromPoint;
745
- exports.distanceFromRect = distanceFromRect;
746
- exports.expand = expand;
747
- exports.fromRange = fromRange;
748
- exports.getElementPolygon = getElementPolygon;
749
- exports.getElementRect = getElementRect;
750
- exports.getPointAngle = getPointAngle;
751
- exports.getRectCenters = getRectCenters;
752
- exports.getRectCorners = getRectCorners;
753
- exports.getRectEdges = getRectEdges;
754
- exports.getRectFromPoints = getRectFromPoints;
755
- exports.getRotationRect = getRotationRect;
756
- exports.getViewportRect = getViewportRect;
757
- exports.getWindowRect = getWindowRect;
758
- exports.inset = inset;
759
- exports.intersection = intersection;
760
- exports.intersects = intersects;
761
- exports.isPoint = isPoint;
762
- exports.isPointEqual = isPointEqual;
763
- exports.isPointInPolygon = isPointInPolygon;
764
- exports.isRect = isRect;
765
- exports.isRectEqual = isRectEqual;
766
- exports.isSizeEqual = isSizeEqual;
767
- exports.isSymmetric = isSymmetric;
768
- exports.resizeRect = resizeRect;
769
- exports.rotate = rotate;
770
- exports.shift = shift;
771
- exports.shrink = shrink;
772
- exports.subtractPoints = subtractPoints;
773
- exports.toRad = toRad;
774
- exports.union = union;
14
+ var __reExport = (target, mod, secondTarget) => (__copyProps(target, mod, "default"), secondTarget && __copyProps(secondTarget, mod, "default"));
15
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
16
+
17
+ // src/index.ts
18
+ var index_exports = {};
19
+ module.exports = __toCommonJS(index_exports);
20
+ __reExport(index_exports, require("./affine-transform.cjs"), module.exports);
21
+ __reExport(index_exports, require("./align.cjs"), module.exports);
22
+ __reExport(index_exports, require("./angle.cjs"), module.exports);
23
+ __reExport(index_exports, require("./clamp.cjs"), module.exports);
24
+ __reExport(index_exports, require("./closest.cjs"), module.exports);
25
+ __reExport(index_exports, require("./constrain.cjs"), module.exports);
26
+ __reExport(index_exports, require("./contains.cjs"), module.exports);
27
+ __reExport(index_exports, require("./distance.cjs"), module.exports);
28
+ __reExport(index_exports, require("./equality.cjs"), module.exports);
29
+ __reExport(index_exports, require("./from-element.cjs"), module.exports);
30
+ __reExport(index_exports, require("./from-points.cjs"), module.exports);
31
+ __reExport(index_exports, require("./from-range.cjs"), module.exports);
32
+ __reExport(index_exports, require("./from-rotation.cjs"), module.exports);
33
+ __reExport(index_exports, require("./from-window.cjs"), module.exports);
34
+ __reExport(index_exports, require("./intersection.cjs"), module.exports);
35
+ __reExport(index_exports, require("./operations.cjs"), module.exports);
36
+ __reExport(index_exports, require("./polygon.cjs"), module.exports);
37
+ __reExport(index_exports, require("./rect.cjs"), module.exports);
38
+ __reExport(index_exports, require("./resize.cjs"), module.exports);
39
+ __reExport(index_exports, require("./types.cjs"), module.exports);
40
+ __reExport(index_exports, require("./union.cjs"), module.exports);
41
+ // Annotate the CommonJS export names for ESM import in node:
42
+ 0 && (module.exports = {
43
+ ...require("./affine-transform.cjs"),
44
+ ...require("./align.cjs"),
45
+ ...require("./angle.cjs"),
46
+ ...require("./clamp.cjs"),
47
+ ...require("./closest.cjs"),
48
+ ...require("./constrain.cjs"),
49
+ ...require("./contains.cjs"),
50
+ ...require("./distance.cjs"),
51
+ ...require("./equality.cjs"),
52
+ ...require("./from-element.cjs"),
53
+ ...require("./from-points.cjs"),
54
+ ...require("./from-range.cjs"),
55
+ ...require("./from-rotation.cjs"),
56
+ ...require("./from-window.cjs"),
57
+ ...require("./intersection.cjs"),
58
+ ...require("./operations.cjs"),
59
+ ...require("./polygon.cjs"),
60
+ ...require("./rect.cjs"),
61
+ ...require("./resize.cjs"),
62
+ ...require("./types.cjs"),
63
+ ...require("./union.cjs")
64
+ });