@zag-js/rect-utils 0.10.5 → 0.11.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 (53) hide show
  1. package/dist/index.d.mts +186 -0
  2. package/dist/index.d.ts +186 -16
  3. package/dist/index.js +540 -58
  4. package/dist/index.js.map +1 -0
  5. package/dist/index.mjs +478 -15
  6. package/dist/index.mjs.map +1 -0
  7. package/package.json +2 -2
  8. package/dist/align.d.ts +0 -8
  9. package/dist/align.js +0 -48
  10. package/dist/align.mjs +0 -44
  11. package/dist/closest.d.ts +0 -5
  12. package/dist/closest.js +0 -53
  13. package/dist/closest.mjs +0 -47
  14. package/dist/contains.d.ts +0 -5
  15. package/dist/contains.js +0 -19
  16. package/dist/contains.mjs +0 -13
  17. package/dist/distance.d.ts +0 -9
  18. package/dist/distance.js +0 -48
  19. package/dist/distance.mjs +0 -41
  20. package/dist/from-element.d.ts +0 -12
  21. package/dist/from-element.js +0 -43
  22. package/dist/from-element.mjs +0 -39
  23. package/dist/from-points.d.ts +0 -3
  24. package/dist/from-points.js +0 -17
  25. package/dist/from-points.mjs +0 -13
  26. package/dist/from-range.d.ts +0 -2
  27. package/dist/from-range.js +0 -27
  28. package/dist/from-range.mjs +0 -23
  29. package/dist/from-rotation.d.ts +0 -5
  30. package/dist/from-rotation.js +0 -39
  31. package/dist/from-rotation.mjs +0 -33
  32. package/dist/from-window.d.ts +0 -20
  33. package/dist/from-window.js +0 -26
  34. package/dist/from-window.mjs +0 -21
  35. package/dist/get-polygon.d.ts +0 -5
  36. package/dist/get-polygon.js +0 -19
  37. package/dist/get-polygon.mjs +0 -15
  38. package/dist/intersection.d.ts +0 -14
  39. package/dist/intersection.js +0 -28
  40. package/dist/intersection.mjs +0 -22
  41. package/dist/operations.d.ts +0 -7
  42. package/dist/operations.js +0 -40
  43. package/dist/operations.mjs +0 -32
  44. package/dist/polygon.d.ts +0 -3
  45. package/dist/polygon.js +0 -53
  46. package/dist/polygon.mjs +0 -48
  47. package/dist/rect.d.ts +0 -61
  48. package/dist/rect.js +0 -55
  49. package/dist/rect.mjs +0 -47
  50. package/dist/types.d.ts +0 -30
  51. package/dist/union.d.ts +0 -2
  52. package/dist/union.js +0 -32
  53. package/dist/union.mjs +0 -28
@@ -1,22 +0,0 @@
1
- import { createRect } from './rect.mjs';
2
-
3
- function intersects(a, b) {
4
- return a.x < b.maxX && a.y < b.maxY && a.maxX > b.x && a.maxY > b.y;
5
- }
6
- function intersection(a, b) {
7
- const x = Math.max(a.x, b.x);
8
- const y = Math.max(a.y, b.y);
9
- const x2 = Math.min(a.x + a.width, b.x + b.width);
10
- const y2 = Math.min(a.y + a.height, b.y + b.height);
11
- return createRect({ x, y, width: x2 - x, height: y2 - y });
12
- }
13
- function collisions(a, b) {
14
- return {
15
- top: a.minY <= b.minY,
16
- right: a.maxX >= b.maxX,
17
- bottom: a.maxY >= b.maxY,
18
- left: a.minX <= b.minX
19
- };
20
- }
21
-
22
- export { collisions, intersection, intersects };
@@ -1,7 +0,0 @@
1
- import { Rect } from "./rect";
2
- import type { Point, RectInset, SymmetricRectInset } from "./types";
3
- export declare const isSymmetric: (v: any) => v is SymmetricRectInset;
4
- export declare function inset(r: Rect, i: RectInset | SymmetricRectInset): Rect;
5
- export declare function expand(r: Rect, v: number | SymmetricRectInset): Rect;
6
- export declare function shrink(r: Rect, v: number | SymmetricRectInset): Rect;
7
- export declare function shift(r: Rect, o: Partial<Point>): Rect;
@@ -1,40 +0,0 @@
1
- 'use strict';
2
-
3
- Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
4
-
5
- const rect = require('./rect.js');
6
-
7
- const isSymmetric = (v) => "dx" in v || "dy" in v;
8
- function inset(r, i) {
9
- const v = isSymmetric(i) ? { left: i.dx, right: i.dx, top: i.dy, bottom: i.dy } : i;
10
- const { top = 0, right = 0, bottom = 0, left = 0 } = v;
11
- return rect.createRect({
12
- x: r.x + left,
13
- y: r.y + top,
14
- width: r.width - left - right,
15
- height: r.height - top - bottom
16
- });
17
- }
18
- function expand(r, v) {
19
- const value = typeof v === "number" ? { dx: -v, dy: -v } : v;
20
- return inset(r, value);
21
- }
22
- function shrink(r, v) {
23
- const value = typeof v === "number" ? { dx: -v, dy: -v } : v;
24
- return inset(r, value);
25
- }
26
- function shift(r, o) {
27
- const { x = 0, y = 0 } = o;
28
- return rect.createRect({
29
- x: r.x + x,
30
- y: r.y + y,
31
- width: r.width,
32
- height: r.height
33
- });
34
- }
35
-
36
- exports.expand = expand;
37
- exports.inset = inset;
38
- exports.isSymmetric = isSymmetric;
39
- exports.shift = shift;
40
- exports.shrink = shrink;
@@ -1,32 +0,0 @@
1
- import { createRect } from './rect.mjs';
2
-
3
- const isSymmetric = (v) => "dx" in v || "dy" in v;
4
- function inset(r, i) {
5
- const v = isSymmetric(i) ? { left: i.dx, right: i.dx, top: i.dy, bottom: i.dy } : i;
6
- const { top = 0, right = 0, bottom = 0, left = 0 } = v;
7
- return createRect({
8
- x: r.x + left,
9
- y: r.y + top,
10
- width: r.width - left - right,
11
- height: r.height - top - bottom
12
- });
13
- }
14
- function expand(r, v) {
15
- const value = typeof v === "number" ? { dx: -v, dy: -v } : v;
16
- return inset(r, value);
17
- }
18
- function shrink(r, v) {
19
- const value = typeof v === "number" ? { dx: -v, dy: -v } : v;
20
- return inset(r, value);
21
- }
22
- function shift(r, o) {
23
- const { x = 0, y = 0 } = o;
24
- return createRect({
25
- x: r.x + x,
26
- y: r.y + y,
27
- width: r.width,
28
- height: r.height
29
- });
30
- }
31
-
32
- export { expand, inset, isSymmetric, shift, shrink };
package/dist/polygon.d.ts DELETED
@@ -1,3 +0,0 @@
1
- import type { Point } from "./types";
2
- export declare function isPointInPolygon(polygon: Point[], point: Point): boolean;
3
- export declare function debugPolygon(polygon: Point[]): () => void;
package/dist/polygon.js DELETED
@@ -1,53 +0,0 @@
1
- 'use strict';
2
-
3
- Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
4
-
5
- function isPointInPolygon(polygon, point) {
6
- const { x, y } = point;
7
- let c = false;
8
- for (let i = 0, j = polygon.length - 1; i < polygon.length; j = i++) {
9
- const xi = polygon[i].x;
10
- const yi = polygon[i].y;
11
- const xj = polygon[j].x;
12
- const yj = polygon[j].y;
13
- if (yi > y !== yj > y && x < (xj - xi) * (y - yi) / (yj - yi) + xi) {
14
- c = !c;
15
- }
16
- }
17
- return c;
18
- }
19
- function createPolygonElement() {
20
- const id = "debug-polygon";
21
- const existingPolygon = document.getElementById(id);
22
- if (existingPolygon) {
23
- return existingPolygon;
24
- }
25
- const svg = document.createElementNS("http://www.w3.org/2000/svg", "svg");
26
- Object.assign(svg.style, {
27
- top: "0",
28
- left: "0",
29
- width: "100%",
30
- height: "100%",
31
- opacity: "0.15",
32
- position: "fixed",
33
- pointerEvents: "none",
34
- fill: "red"
35
- });
36
- const polygon = document.createElementNS("http://www.w3.org/2000/svg", "polygon");
37
- polygon.setAttribute("id", id);
38
- polygon.setAttribute("points", "0,0 0,0");
39
- svg.appendChild(polygon);
40
- document.body.appendChild(svg);
41
- return polygon;
42
- }
43
- function debugPolygon(polygon) {
44
- const el = createPolygonElement();
45
- const points = polygon.map((point) => `${point.x},${point.y}`).join(" ");
46
- el.setAttribute("points", points);
47
- return () => {
48
- el.remove();
49
- };
50
- }
51
-
52
- exports.debugPolygon = debugPolygon;
53
- exports.isPointInPolygon = isPointInPolygon;
package/dist/polygon.mjs DELETED
@@ -1,48 +0,0 @@
1
- function isPointInPolygon(polygon, point) {
2
- const { x, y } = point;
3
- let c = false;
4
- for (let i = 0, j = polygon.length - 1; i < polygon.length; j = i++) {
5
- const xi = polygon[i].x;
6
- const yi = polygon[i].y;
7
- const xj = polygon[j].x;
8
- const yj = polygon[j].y;
9
- if (yi > y !== yj > y && x < (xj - xi) * (y - yi) / (yj - yi) + xi) {
10
- c = !c;
11
- }
12
- }
13
- return c;
14
- }
15
- function createPolygonElement() {
16
- const id = "debug-polygon";
17
- const existingPolygon = document.getElementById(id);
18
- if (existingPolygon) {
19
- return existingPolygon;
20
- }
21
- const svg = document.createElementNS("http://www.w3.org/2000/svg", "svg");
22
- Object.assign(svg.style, {
23
- top: "0",
24
- left: "0",
25
- width: "100%",
26
- height: "100%",
27
- opacity: "0.15",
28
- position: "fixed",
29
- pointerEvents: "none",
30
- fill: "red"
31
- });
32
- const polygon = document.createElementNS("http://www.w3.org/2000/svg", "polygon");
33
- polygon.setAttribute("id", id);
34
- polygon.setAttribute("points", "0,0 0,0");
35
- svg.appendChild(polygon);
36
- document.body.appendChild(svg);
37
- return polygon;
38
- }
39
- function debugPolygon(polygon) {
40
- const el = createPolygonElement();
41
- const points = polygon.map((point) => `${point.x},${point.y}`).join(" ");
42
- el.setAttribute("points", points);
43
- return () => {
44
- el.remove();
45
- };
46
- }
47
-
48
- export { debugPolygon, isPointInPolygon };
package/dist/rect.d.ts DELETED
@@ -1,61 +0,0 @@
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: {
14
- x: number;
15
- y: number;
16
- };
17
- };
18
- export 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;
24
- };
25
- right: {
26
- x: number;
27
- y: number;
28
- };
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;
54
- };
55
- };
56
- export declare function getRectEdges(v: Rect): {
57
- top: RectEdge;
58
- right: RectEdge;
59
- bottom: RectEdge;
60
- left: RectEdge;
61
- };
package/dist/rect.js DELETED
@@ -1,55 +0,0 @@
1
- 'use strict';
2
-
3
- Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
4
-
5
- const 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
- const hasProp = (obj, prop) => Object.prototype.hasOwnProperty.call(obj, prop);
25
- function isRect(v) {
26
- return hasProp(v, "x") && hasProp(v, "y") && hasProp(v, "width") && hasProp(v, "height");
27
- }
28
- function getRectCenters(v) {
29
- const top = point(v.midX, v.minY);
30
- const right = point(v.maxX, v.midY);
31
- const bottom = point(v.midX, v.maxY);
32
- const left = point(v.minX, v.midY);
33
- return { top, right, bottom, left };
34
- }
35
- function getRectCorners(v) {
36
- const top = point(v.minX, v.minY);
37
- const right = point(v.maxX, v.minY);
38
- const bottom = point(v.maxX, v.maxY);
39
- const left = point(v.minX, v.maxY);
40
- return { top, right, bottom, left };
41
- }
42
- function getRectEdges(v) {
43
- const c = getRectCorners(v);
44
- const top = [c.top, c.right];
45
- const right = [c.right, c.bottom];
46
- const bottom = [c.left, c.bottom];
47
- const left = [c.top, c.left];
48
- return { top, right, bottom, left };
49
- }
50
-
51
- exports.createRect = createRect;
52
- exports.getRectCenters = getRectCenters;
53
- exports.getRectCorners = getRectCorners;
54
- exports.getRectEdges = getRectEdges;
55
- exports.isRect = isRect;
package/dist/rect.mjs DELETED
@@ -1,47 +0,0 @@
1
- const point = (x, y) => ({ x, y });
2
- function createRect(r) {
3
- const { x, y, width, height } = r;
4
- const midX = x + width / 2;
5
- const midY = y + height / 2;
6
- return {
7
- x,
8
- y,
9
- width,
10
- height,
11
- minX: x,
12
- minY: y,
13
- maxX: x + width,
14
- maxY: y + height,
15
- midX,
16
- midY,
17
- center: point(midX, midY)
18
- };
19
- }
20
- const hasProp = (obj, prop) => Object.prototype.hasOwnProperty.call(obj, prop);
21
- function isRect(v) {
22
- return hasProp(v, "x") && hasProp(v, "y") && hasProp(v, "width") && hasProp(v, "height");
23
- }
24
- function getRectCenters(v) {
25
- const top = point(v.midX, v.minY);
26
- const right = point(v.maxX, v.midY);
27
- const bottom = point(v.midX, v.maxY);
28
- const left = point(v.minX, v.midY);
29
- return { top, right, bottom, left };
30
- }
31
- function getRectCorners(v) {
32
- const top = point(v.minX, v.minY);
33
- const right = point(v.maxX, v.minY);
34
- const bottom = point(v.maxX, v.maxY);
35
- const left = point(v.minX, v.maxY);
36
- return { top, right, bottom, left };
37
- }
38
- function getRectEdges(v) {
39
- const c = getRectCorners(v);
40
- const top = [c.top, c.right];
41
- const right = [c.right, c.bottom];
42
- const bottom = [c.left, c.bottom];
43
- const left = [c.top, c.left];
44
- return { top, right, bottom, left };
45
- }
46
-
47
- export { createRect, getRectCenters, getRectCorners, getRectEdges, isRect };
package/dist/types.d.ts DELETED
@@ -1,30 +0,0 @@
1
- export type Point = {
2
- x: number;
3
- y: number;
4
- };
5
- export type RectValue = {
6
- x: number;
7
- y: number;
8
- width: number;
9
- height: number;
10
- };
11
- export type RectSide = "top" | "right" | "bottom" | "left";
12
- export type RectPoint = "top-left" | "top-center" | "top-right" | "right-center" | "left-center" | "bottom-left" | "bottom-right" | "bottom-center" | "center";
13
- export type RectEdge = [Point, Point];
14
- export type RectPoints = [Point, Point, Point, Point];
15
- export type RectEdges = Record<RectSide, RectEdge> & {
16
- value: RectEdge[];
17
- };
18
- export type RectCorner = "topLeft" | "topRight" | "bottomLeft" | "bottomRight";
19
- export type RectCorners = Record<RectCorner, Point> & {
20
- value: RectPoints;
21
- };
22
- export type RectCenter = "topCenter" | "rightCenter" | "leftCenter" | "bottomCenter";
23
- export type RectCenters = Record<RectCenter, Point> & {
24
- value: RectPoints;
25
- };
26
- export type RectInset = Partial<Record<RectSide, number>>;
27
- export type SymmetricRectInset = {
28
- dx?: number;
29
- dy?: number;
30
- };
package/dist/union.d.ts DELETED
@@ -1,2 +0,0 @@
1
- import type { Rect } from "./rect";
2
- export declare function union(...rs: Rect[]): Rect;
package/dist/union.js DELETED
@@ -1,32 +0,0 @@
1
- 'use strict';
2
-
3
- Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
4
-
5
- const fromPoints = require('./from-points.js');
6
-
7
- const { min, max } = Math;
8
- function union(...rs) {
9
- const pMin = {
10
- x: min.apply(
11
- Math,
12
- rs.map((r) => r.minX)
13
- ),
14
- y: min.apply(
15
- Math,
16
- rs.map((r) => r.minY)
17
- )
18
- };
19
- const pMax = {
20
- x: max.apply(
21
- Math,
22
- rs.map((r) => r.maxX)
23
- ),
24
- y: max.apply(
25
- Math,
26
- rs.map((r) => r.maxY)
27
- )
28
- };
29
- return fromPoints.getRectFromPoints(pMin, pMax);
30
- }
31
-
32
- exports.union = union;
package/dist/union.mjs DELETED
@@ -1,28 +0,0 @@
1
- import { getRectFromPoints } from './from-points.mjs';
2
-
3
- const { min, max } = Math;
4
- function union(...rs) {
5
- const pMin = {
6
- x: min.apply(
7
- Math,
8
- rs.map((r) => r.minX)
9
- ),
10
- y: min.apply(
11
- Math,
12
- rs.map((r) => r.minY)
13
- )
14
- };
15
- const pMax = {
16
- x: max.apply(
17
- Math,
18
- rs.map((r) => r.maxX)
19
- ),
20
- y: max.apply(
21
- Math,
22
- rs.map((r) => r.maxY)
23
- )
24
- };
25
- return getRectFromPoints(pMin, pMax);
26
- }
27
-
28
- export { union };