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