@zag-js/rect-utils 0.2.0 → 0.2.2

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 (68) hide show
  1. package/dist/align.d.ts +12 -0
  2. package/dist/align.js +71 -0
  3. package/dist/align.mjs +6 -0
  4. package/dist/chunk-2QYJISFU.mjs +21 -0
  5. package/dist/chunk-AYR2YRSU.mjs +49 -0
  6. package/dist/chunk-AYWORFWQ.mjs +32 -0
  7. package/dist/chunk-BGAHRJ6S.mjs +41 -0
  8. package/dist/chunk-DBR73IMM.mjs +54 -0
  9. package/dist/chunk-GVBSNO2Z.mjs +55 -0
  10. package/dist/chunk-IFHTGH3H.mjs +29 -0
  11. package/dist/chunk-LBA674LT.mjs +27 -0
  12. package/dist/chunk-NGMCS5TG.mjs +33 -0
  13. package/dist/chunk-QXXKPYP2.mjs +47 -0
  14. package/dist/chunk-R24UTBJ3.mjs +52 -0
  15. package/dist/chunk-S3HXSPS7.mjs +21 -0
  16. package/dist/chunk-T4IEPEZY.mjs +41 -0
  17. package/dist/chunk-TPU7B3ZS.mjs +18 -0
  18. package/dist/chunk-WBQAMGXK.mjs +0 -0
  19. package/dist/chunk-YDYJCJQZ.mjs +56 -0
  20. package/dist/closest.d.ts +8 -0
  21. package/dist/closest.js +83 -0
  22. package/dist/closest.mjs +13 -0
  23. package/dist/contains.d.ts +8 -0
  24. package/dist/contains.js +60 -0
  25. package/dist/contains.mjs +11 -0
  26. package/dist/distance.d.ts +12 -0
  27. package/dist/distance.js +79 -0
  28. package/dist/distance.mjs +14 -0
  29. package/dist/from-element.d.ts +16 -0
  30. package/dist/from-element.js +98 -0
  31. package/dist/from-element.mjs +7 -0
  32. package/dist/from-points.d.ts +6 -0
  33. package/dist/from-points.js +61 -0
  34. package/dist/from-points.mjs +7 -0
  35. package/dist/from-range.d.ts +6 -0
  36. package/dist/from-range.js +154 -0
  37. package/dist/from-range.mjs +10 -0
  38. package/dist/from-rotation.d.ts +8 -0
  39. package/dist/from-rotation.js +92 -0
  40. package/dist/from-rotation.mjs +11 -0
  41. package/dist/from-window.d.ts +24 -0
  42. package/dist/from-window.js +71 -0
  43. package/dist/from-window.mjs +9 -0
  44. package/dist/get-polygon.d.ts +8 -0
  45. package/dist/get-polygon.js +70 -0
  46. package/dist/get-polygon.mjs +7 -0
  47. package/dist/index.d.ts +16 -186
  48. package/dist/index.js +8 -9
  49. package/dist/index.mjs +67 -453
  50. package/dist/intersection.d.ts +17 -0
  51. package/dist/intersection.js +74 -0
  52. package/dist/intersection.mjs +11 -0
  53. package/dist/operations.d.ts +10 -0
  54. package/dist/operations.js +88 -0
  55. package/dist/operations.mjs +15 -0
  56. package/dist/polygon.d.ts +6 -0
  57. package/dist/polygon.js +77 -0
  58. package/dist/polygon.mjs +8 -0
  59. package/dist/rect.d.ts +64 -0
  60. package/dist/rect.js +86 -0
  61. package/dist/rect.mjs +14 -0
  62. package/dist/types.d.ts +32 -0
  63. package/dist/types.js +18 -0
  64. package/dist/types.mjs +1 -0
  65. package/dist/union.d.ts +6 -0
  66. package/dist/union.js +87 -0
  67. package/dist/union.mjs +8 -0
  68. package/package.json +18 -8
@@ -0,0 +1,12 @@
1
+ import { Rect } from './rect.js';
2
+ import './types.js';
3
+
4
+ declare function alignRect(a: Rect, ref: Rect, options: AlignOptions): Rect;
5
+ type AlignOptions = {
6
+ h: HAlign;
7
+ v: VAlign;
8
+ };
9
+ type HAlign = "left-inside" | "left-outside" | "center" | "right-inside" | "right-outside";
10
+ type VAlign = "top-inside" | "top-outside" | "center" | "bottom-inside" | "bottom-outside";
11
+
12
+ export { AlignOptions, HAlign, VAlign, alignRect };
package/dist/align.js ADDED
@@ -0,0 +1,71 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/align.ts
21
+ var align_exports = {};
22
+ __export(align_exports, {
23
+ alignRect: () => alignRect
24
+ });
25
+ module.exports = __toCommonJS(align_exports);
26
+ function hAlign(a, ref, h) {
27
+ let x = ref.minX;
28
+ if (h === "left-inside") {
29
+ x = ref.minX;
30
+ }
31
+ if (h === "left-outside") {
32
+ x = ref.minX - ref.width;
33
+ }
34
+ if (h === "right-inside") {
35
+ x = ref.maxX - ref.width;
36
+ }
37
+ if (h === "right-outside") {
38
+ x = ref.maxX;
39
+ }
40
+ if (h === "center") {
41
+ x = ref.midX - ref.width / 2;
42
+ }
43
+ return { ...a, x };
44
+ }
45
+ function vAlign(a, ref, v) {
46
+ let y = ref.minY;
47
+ if (v === "top-inside") {
48
+ y = ref.minY;
49
+ }
50
+ if (v === "top-outside") {
51
+ y = ref.minY - a.height;
52
+ }
53
+ if (v === "bottom-inside") {
54
+ y = ref.maxY - a.height;
55
+ }
56
+ if (v === "bottom-outside") {
57
+ y = ref.maxY;
58
+ }
59
+ if (v === "center") {
60
+ y = ref.midY - a.height / 2;
61
+ }
62
+ return { ...a, y };
63
+ }
64
+ function alignRect(a, ref, options) {
65
+ const { h, v } = options;
66
+ return vAlign(hAlign(a, ref, h), ref, v);
67
+ }
68
+ // Annotate the CommonJS export names for ESM import in node:
69
+ 0 && (module.exports = {
70
+ alignRect
71
+ });
package/dist/align.mjs ADDED
@@ -0,0 +1,6 @@
1
+ import {
2
+ alignRect
3
+ } from "./chunk-QXXKPYP2.mjs";
4
+ export {
5
+ alignRect
6
+ };
@@ -0,0 +1,21 @@
1
+ import {
2
+ getRectCorners,
3
+ isRect
4
+ } from "./chunk-YDYJCJQZ.mjs";
5
+
6
+ // src/contains.ts
7
+ function containsPoint(r, p) {
8
+ return r.minX <= p.x && p.x <= r.maxX && r.minY <= p.y && p.y <= r.maxY;
9
+ }
10
+ function containsRect(a, b) {
11
+ return Object.values(getRectCorners(b)).every((c) => containsPoint(a, c));
12
+ }
13
+ function contains(r, v) {
14
+ return isRect(v) ? containsRect(r, v) : containsPoint(r, v);
15
+ }
16
+
17
+ export {
18
+ containsPoint,
19
+ containsRect,
20
+ contains
21
+ };
@@ -0,0 +1,49 @@
1
+ import {
2
+ intersects
3
+ } from "./chunk-IFHTGH3H.mjs";
4
+
5
+ // src/distance.ts
6
+ function distance(a, b = { x: 0, y: 0 }) {
7
+ return Math.sqrt(Math.pow(a.x - b.x, 2) + Math.pow(a.y - b.y, 2));
8
+ }
9
+ function distanceFromPoint(r, p) {
10
+ let x = 0;
11
+ let y = 0;
12
+ if (p.x < r.x)
13
+ x = r.x - p.x;
14
+ else if (p.x > r.maxX)
15
+ x = p.x - r.maxX;
16
+ if (p.y < r.y)
17
+ y = r.y - p.y;
18
+ else if (p.y > r.maxY)
19
+ y = p.y - r.maxY;
20
+ return { x, y, value: distance({ x, y }) };
21
+ }
22
+ function distanceFromRect(a, b) {
23
+ if (intersects(a, b))
24
+ return { x: 0, y: 0, value: 0 };
25
+ const left = a.x < b.x ? a : b;
26
+ const right = b.x < a.x ? a : b;
27
+ const upper = a.y < b.y ? a : b;
28
+ const lower = b.y < a.y ? a : b;
29
+ let x = left.x === right.x ? 0 : right.x - left.maxX;
30
+ x = Math.max(0, x);
31
+ let y = upper.y === lower.y ? 0 : lower.y - upper.maxY;
32
+ y = Math.max(0, y);
33
+ return { x, y, value: distance({ x, y }) };
34
+ }
35
+ function distanceBtwEdges(a, b) {
36
+ return {
37
+ left: b.x - a.x,
38
+ top: b.y - a.y,
39
+ right: a.maxX - b.maxX,
40
+ bottom: a.maxY - b.maxY
41
+ };
42
+ }
43
+
44
+ export {
45
+ distance,
46
+ distanceFromPoint,
47
+ distanceFromRect,
48
+ distanceBtwEdges
49
+ };
@@ -0,0 +1,32 @@
1
+ import {
2
+ union
3
+ } from "./chunk-NGMCS5TG.mjs";
4
+ import {
5
+ getElementRect
6
+ } from "./chunk-GVBSNO2Z.mjs";
7
+ import {
8
+ createRect
9
+ } from "./chunk-YDYJCJQZ.mjs";
10
+
11
+ // src/from-range.ts
12
+ function fromRange(range) {
13
+ let rs = [];
14
+ const rects = Array.from(range.getClientRects());
15
+ if (rects.length) {
16
+ rs = rs.concat(rects.map(createRect));
17
+ return union.apply(void 0, rs);
18
+ }
19
+ let start = range.startContainer;
20
+ if (start.nodeType === Node.TEXT_NODE) {
21
+ start = start.parentNode;
22
+ }
23
+ if (start instanceof HTMLElement) {
24
+ const r = getElementRect(start);
25
+ rs.push({ ...r, x: r.maxX, width: 0 });
26
+ }
27
+ return union.apply(void 0, rs);
28
+ }
29
+
30
+ export {
31
+ fromRange
32
+ };
@@ -0,0 +1,41 @@
1
+ import {
2
+ createRect,
3
+ getRectCorners
4
+ } from "./chunk-YDYJCJQZ.mjs";
5
+
6
+ // src/from-rotation.ts
7
+ function toRad(d) {
8
+ return d % 360 * Math.PI / 180;
9
+ }
10
+ function rotate(a, d, c) {
11
+ const r = toRad(d);
12
+ const sin = Math.sin(r);
13
+ const cos = Math.cos(r);
14
+ const x = a.x - c.x;
15
+ const y = a.y - c.y;
16
+ return {
17
+ x: c.x + x * cos - y * sin,
18
+ y: c.y + x * sin + y * cos
19
+ };
20
+ }
21
+ function getRotationRect(r, deg) {
22
+ const rr = Object.values(getRectCorners(r)).map((p) => rotate(p, deg, r.center));
23
+ const xs = rr.map((p) => p.x);
24
+ const ys = rr.map((p) => p.y);
25
+ const minX = Math.min(...xs);
26
+ const minY = Math.min(...ys);
27
+ const maxX = Math.max(...xs);
28
+ const maxY = Math.max(...ys);
29
+ return createRect({
30
+ x: minX,
31
+ y: minY,
32
+ width: maxX - minX,
33
+ height: maxY - minY
34
+ });
35
+ }
36
+
37
+ export {
38
+ toRad,
39
+ rotate,
40
+ getRotationRect
41
+ };
@@ -0,0 +1,54 @@
1
+ import {
2
+ distance
3
+ } from "./chunk-AYR2YRSU.mjs";
4
+
5
+ // src/closest.ts
6
+ function closest(...pts) {
7
+ return (a) => {
8
+ const ds = pts.map((b) => distance(b, a));
9
+ const c = Math.min.apply(Math, ds);
10
+ return pts[ds.indexOf(c)];
11
+ };
12
+ }
13
+ function closestSideToRect(ref, r) {
14
+ if (r.maxX <= ref.minX) {
15
+ return "left";
16
+ }
17
+ if (r.minX >= ref.maxX) {
18
+ return "right";
19
+ }
20
+ if (r.maxY <= ref.minY) {
21
+ return "top";
22
+ }
23
+ if (r.minY >= ref.maxY) {
24
+ return "bottom";
25
+ }
26
+ return "left";
27
+ }
28
+ function closestSideToPoint(ref, p) {
29
+ const { x, y } = p;
30
+ const dl = x - ref.minX;
31
+ const dr = ref.maxX - x;
32
+ const dt = y - ref.minY;
33
+ const db = ref.maxY - y;
34
+ let closest2 = dl;
35
+ let side = "left";
36
+ if (dr < closest2) {
37
+ closest2 = dr;
38
+ side = "right";
39
+ }
40
+ if (dt < closest2) {
41
+ closest2 = dt;
42
+ side = "top";
43
+ }
44
+ if (db < closest2) {
45
+ side = "bottom";
46
+ }
47
+ return side;
48
+ }
49
+
50
+ export {
51
+ closest,
52
+ closestSideToRect,
53
+ closestSideToPoint
54
+ };
@@ -0,0 +1,55 @@
1
+ import {
2
+ createRect
3
+ } from "./chunk-YDYJCJQZ.mjs";
4
+
5
+ // ../dom/src/get-computed-style.ts
6
+ function getCache() {
7
+ const g = globalThis;
8
+ g.__styleCache = g.__styleCache || /* @__PURE__ */ new WeakMap();
9
+ return g.__styleCache;
10
+ }
11
+ function getComputedStyle(el) {
12
+ if (!el)
13
+ return {};
14
+ const cache = getCache();
15
+ let style = cache.get(el);
16
+ if (!style) {
17
+ const win = el?.ownerDocument.defaultView ?? window;
18
+ style = win.getComputedStyle(el);
19
+ cache.set(el, style);
20
+ }
21
+ return style;
22
+ }
23
+
24
+ // src/from-element.ts
25
+ function getElementRect(el, opts = {}) {
26
+ return createRect(getClientRect(el, opts));
27
+ }
28
+ function getClientRect(el, opts = {}) {
29
+ const { excludeScrollbar = false, excludeBorders = false } = opts;
30
+ const { x, y, width, height } = el.getBoundingClientRect();
31
+ const r = { x, y, width, height };
32
+ const style = getComputedStyle(el);
33
+ const { borderLeftWidth, borderTopWidth, borderRightWidth, borderBottomWidth } = style;
34
+ const borderXWidth = sum(borderLeftWidth, borderRightWidth);
35
+ const borderYWidth = sum(borderTopWidth, borderBottomWidth);
36
+ if (excludeBorders) {
37
+ r.width -= borderXWidth;
38
+ r.height -= borderYWidth;
39
+ r.x += px(borderLeftWidth);
40
+ r.y += px(borderTopWidth);
41
+ }
42
+ if (excludeScrollbar) {
43
+ const scrollbarWidth = el.offsetWidth - el.clientWidth - borderXWidth;
44
+ const scrollbarHeight = el.offsetHeight - el.clientHeight - borderYWidth;
45
+ r.width -= scrollbarWidth;
46
+ r.height -= scrollbarHeight;
47
+ }
48
+ return r;
49
+ }
50
+ var px = (v) => parseFloat(v.replace("px", ""));
51
+ var sum = (...vals) => vals.reduce((sum2, v) => sum2 + (v ? px(v) : 0), 0);
52
+
53
+ export {
54
+ getElementRect
55
+ };
@@ -0,0 +1,29 @@
1
+ import {
2
+ createRect
3
+ } from "./chunk-YDYJCJQZ.mjs";
4
+
5
+ // src/intersection.ts
6
+ function intersects(a, b) {
7
+ return a.x < b.maxX && a.y < b.maxY && a.maxX > b.x && a.maxY > b.y;
8
+ }
9
+ function intersection(a, b) {
10
+ const x = Math.max(a.x, b.x);
11
+ const y = Math.max(a.y, b.y);
12
+ const x2 = Math.min(a.x + a.width, b.x + b.width);
13
+ const y2 = Math.min(a.y + a.height, b.y + b.height);
14
+ return createRect({ x, y, width: x2 - x, height: y2 - y });
15
+ }
16
+ function collisions(a, b) {
17
+ return {
18
+ top: a.minY <= b.minY,
19
+ right: a.maxX >= b.maxX,
20
+ bottom: a.maxY >= b.maxY,
21
+ left: a.minX <= b.minX
22
+ };
23
+ }
24
+
25
+ export {
26
+ intersects,
27
+ intersection,
28
+ collisions
29
+ };
@@ -0,0 +1,27 @@
1
+ import {
2
+ createRect
3
+ } from "./chunk-YDYJCJQZ.mjs";
4
+
5
+ // src/from-window.ts
6
+ function getWindowRect(win, opts = {}) {
7
+ return createRect(getViewportRect(win, opts));
8
+ }
9
+ function getViewportRect(win, opts) {
10
+ const { excludeScrollbar = false } = opts;
11
+ const { innerWidth, innerHeight, document: doc, visualViewport } = win;
12
+ const width = visualViewport?.width || innerWidth;
13
+ const height = visualViewport?.height || innerHeight;
14
+ const rect = { x: 0, y: 0, width, height };
15
+ if (excludeScrollbar) {
16
+ const scrollbarWidth = innerWidth - doc.documentElement.clientWidth;
17
+ const scrollbarHeight = innerHeight - doc.documentElement.clientHeight;
18
+ rect.width -= scrollbarWidth;
19
+ rect.height -= scrollbarHeight;
20
+ }
21
+ return rect;
22
+ }
23
+
24
+ export {
25
+ getWindowRect,
26
+ getViewportRect
27
+ };
@@ -0,0 +1,33 @@
1
+ import {
2
+ getRectFromPoints
3
+ } from "./chunk-TPU7B3ZS.mjs";
4
+
5
+ // src/union.ts
6
+ var { min, max } = Math;
7
+ function union(...rs) {
8
+ const pMin = {
9
+ x: min.apply(
10
+ Math,
11
+ rs.map((r) => r.minX)
12
+ ),
13
+ y: min.apply(
14
+ Math,
15
+ rs.map((r) => r.minY)
16
+ )
17
+ };
18
+ const pMax = {
19
+ x: max.apply(
20
+ Math,
21
+ rs.map((r) => r.maxX)
22
+ ),
23
+ y: max.apply(
24
+ Math,
25
+ rs.map((r) => r.maxY)
26
+ )
27
+ };
28
+ return getRectFromPoints(pMin, pMax);
29
+ }
30
+
31
+ export {
32
+ union
33
+ };
@@ -0,0 +1,47 @@
1
+ // src/align.ts
2
+ function hAlign(a, ref, h) {
3
+ let x = ref.minX;
4
+ if (h === "left-inside") {
5
+ x = ref.minX;
6
+ }
7
+ if (h === "left-outside") {
8
+ x = ref.minX - ref.width;
9
+ }
10
+ if (h === "right-inside") {
11
+ x = ref.maxX - ref.width;
12
+ }
13
+ if (h === "right-outside") {
14
+ x = ref.maxX;
15
+ }
16
+ if (h === "center") {
17
+ x = ref.midX - ref.width / 2;
18
+ }
19
+ return { ...a, x };
20
+ }
21
+ function vAlign(a, ref, v) {
22
+ let y = ref.minY;
23
+ if (v === "top-inside") {
24
+ y = ref.minY;
25
+ }
26
+ if (v === "top-outside") {
27
+ y = ref.minY - a.height;
28
+ }
29
+ if (v === "bottom-inside") {
30
+ y = ref.maxY - a.height;
31
+ }
32
+ if (v === "bottom-outside") {
33
+ y = ref.maxY;
34
+ }
35
+ if (v === "center") {
36
+ y = ref.midY - a.height / 2;
37
+ }
38
+ return { ...a, y };
39
+ }
40
+ function alignRect(a, ref, options) {
41
+ const { h, v } = options;
42
+ return vAlign(hAlign(a, ref, h), ref, v);
43
+ }
44
+
45
+ export {
46
+ alignRect
47
+ };
@@ -0,0 +1,52 @@
1
+ // src/polygon.ts
2
+ function isPointInPolygon(polygon, point) {
3
+ const { x, y } = point;
4
+ let c = false;
5
+ for (let i = 0, j = polygon.length - 1; i < polygon.length; j = i++) {
6
+ const xi = polygon[i].x;
7
+ const yi = polygon[i].y;
8
+ const xj = polygon[j].x;
9
+ const yj = polygon[j].y;
10
+ if (yi > y !== yj > y && x < (xj - xi) * (y - yi) / (yj - yi) + xi) {
11
+ c = !c;
12
+ }
13
+ }
14
+ return c;
15
+ }
16
+ function createPolygonElement() {
17
+ const id = "debug-polygon";
18
+ const existingPolygon = document.getElementById(id);
19
+ if (existingPolygon) {
20
+ return existingPolygon;
21
+ }
22
+ const svg = document.createElementNS("http://www.w3.org/2000/svg", "svg");
23
+ Object.assign(svg.style, {
24
+ top: "0",
25
+ left: "0",
26
+ width: "100%",
27
+ height: "100%",
28
+ opacity: "0.15",
29
+ position: "fixed",
30
+ pointerEvents: "none",
31
+ fill: "red"
32
+ });
33
+ const polygon = document.createElementNS("http://www.w3.org/2000/svg", "polygon");
34
+ polygon.setAttribute("id", id);
35
+ polygon.setAttribute("points", "0,0 0,0");
36
+ svg.appendChild(polygon);
37
+ document.body.appendChild(svg);
38
+ return polygon;
39
+ }
40
+ function debugPolygon(polygon) {
41
+ const el = createPolygonElement();
42
+ const points = polygon.map((point) => `${point.x},${point.y}`).join(" ");
43
+ el.setAttribute("points", points);
44
+ return () => {
45
+ el.remove();
46
+ };
47
+ }
48
+
49
+ export {
50
+ isPointInPolygon,
51
+ debugPolygon
52
+ };
@@ -0,0 +1,21 @@
1
+ import {
2
+ createRect,
3
+ getRectCorners
4
+ } from "./chunk-YDYJCJQZ.mjs";
5
+
6
+ // src/get-polygon.ts
7
+ function getElementPolygon(rectValue, placement) {
8
+ const rect = createRect(rectValue);
9
+ const { top, right, left, bottom } = getRectCorners(rect);
10
+ const [base] = placement.split("-");
11
+ return {
12
+ top: [left, top, right, bottom],
13
+ right: [top, right, bottom, left],
14
+ bottom: [top, left, bottom, right],
15
+ left: [right, top, left, bottom]
16
+ }[base];
17
+ }
18
+
19
+ export {
20
+ getElementPolygon
21
+ };
@@ -0,0 +1,41 @@
1
+ import {
2
+ createRect
3
+ } from "./chunk-YDYJCJQZ.mjs";
4
+
5
+ // src/operations.ts
6
+ var isSymmetric = (v) => "dx" in v || "dy" in v;
7
+ function inset(r, i) {
8
+ const v = isSymmetric(i) ? { left: i.dx, right: i.dx, top: i.dy, bottom: i.dy } : i;
9
+ const { top = 0, right = 0, bottom = 0, left = 0 } = v;
10
+ return createRect({
11
+ x: r.x + left,
12
+ y: r.y + top,
13
+ width: r.width - left - right,
14
+ height: r.height - top - bottom
15
+ });
16
+ }
17
+ function expand(r, v) {
18
+ const value = typeof v === "number" ? { dx: -v, dy: -v } : v;
19
+ return inset(r, value);
20
+ }
21
+ function shrink(r, v) {
22
+ const value = typeof v === "number" ? { dx: -v, dy: -v } : v;
23
+ return inset(r, value);
24
+ }
25
+ function shift(r, o) {
26
+ const { x = 0, y = 0 } = o;
27
+ return createRect({
28
+ x: r.x + x,
29
+ y: r.y + y,
30
+ width: r.width,
31
+ height: r.height
32
+ });
33
+ }
34
+
35
+ export {
36
+ isSymmetric,
37
+ inset,
38
+ expand,
39
+ shrink,
40
+ shift
41
+ };
@@ -0,0 +1,18 @@
1
+ import {
2
+ createRect
3
+ } from "./chunk-YDYJCJQZ.mjs";
4
+
5
+ // src/from-points.ts
6
+ function getRectFromPoints(...pts) {
7
+ const xs = pts.map((p) => p.x);
8
+ const ys = pts.map((p) => p.y);
9
+ const x = Math.min(...xs);
10
+ const y = Math.min(...ys);
11
+ const width = Math.max(...xs) - x;
12
+ const height = Math.max(...ys) - y;
13
+ return createRect({ x, y, width, height });
14
+ }
15
+
16
+ export {
17
+ getRectFromPoints
18
+ };
File without changes
@@ -0,0 +1,56 @@
1
+ // ../core/src/guard.ts
2
+ var hasProp = (obj, prop) => Object.prototype.hasOwnProperty.call(obj, prop);
3
+
4
+ // src/rect.ts
5
+ var point = (x, y) => ({ x, y });
6
+ function createRect(r) {
7
+ const { x, y, width, height } = r;
8
+ const midX = x + width / 2;
9
+ const midY = y + height / 2;
10
+ return {
11
+ x,
12
+ y,
13
+ width,
14
+ height,
15
+ minX: x,
16
+ minY: y,
17
+ maxX: x + width,
18
+ maxY: y + height,
19
+ midX,
20
+ midY,
21
+ center: point(midX, midY)
22
+ };
23
+ }
24
+ function isRect(v) {
25
+ return hasProp(v, "x") && hasProp(v, "y") && hasProp(v, "width") && hasProp(v, "height");
26
+ }
27
+ function getRectCenters(v) {
28
+ const top = point(v.midX, v.minY);
29
+ const right = point(v.maxX, v.midY);
30
+ const bottom = point(v.midX, v.maxY);
31
+ const left = point(v.minX, v.midY);
32
+ return { top, right, bottom, left };
33
+ }
34
+ function getRectCorners(v) {
35
+ const top = point(v.minX, v.minY);
36
+ const right = point(v.maxX, v.minY);
37
+ const bottom = point(v.maxX, v.maxY);
38
+ const left = point(v.minX, v.maxY);
39
+ return { top, right, bottom, left };
40
+ }
41
+ function getRectEdges(v) {
42
+ const c = getRectCorners(v);
43
+ const top = [c.top, c.right];
44
+ const right = [c.right, c.bottom];
45
+ const bottom = [c.left, c.bottom];
46
+ const left = [c.top, c.left];
47
+ return { top, right, bottom, left };
48
+ }
49
+
50
+ export {
51
+ createRect,
52
+ isRect,
53
+ getRectCenters,
54
+ getRectCorners,
55
+ getRectEdges
56
+ };
@@ -0,0 +1,8 @@
1
+ import { Rect } from './rect.js';
2
+ import { Point, RectSide } from './types.js';
3
+
4
+ declare function closest(...pts: Point[]): (a: Point) => Point;
5
+ declare function closestSideToRect(ref: Rect, r: Rect): RectSide;
6
+ declare function closestSideToPoint(ref: Rect, p: Point): RectSide;
7
+
8
+ export { closest, closestSideToPoint, closestSideToRect };