@zag-js/rect-utils 0.0.0-dev-20220706194607 → 0.0.0-dev-20220709094240

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.
@@ -0,0 +1,5 @@
1
+ import type { RectValue } from "./types";
2
+ export declare function getElementPolygon(rectValue: RectValue, placement: string): {
3
+ x: number;
4
+ y: number;
5
+ }[];
package/dist/index.d.ts CHANGED
@@ -7,6 +7,7 @@ export * from "./from-points";
7
7
  export * from "./from-range";
8
8
  export * from "./from-rotation";
9
9
  export * from "./from-window";
10
+ export * from "./get-polygon";
10
11
  export * from "./intersection";
11
12
  export * from "./operations";
12
13
  export * from "./polygon";
package/dist/index.js CHANGED
@@ -53,6 +53,7 @@ __export(src_exports, {
53
53
  distanceFromRect: () => distanceFromRect,
54
54
  expand: () => expand,
55
55
  fromRange: () => fromRange,
56
+ getElementPolygon: () => getElementPolygon,
56
57
  getElementRect: () => getElementRect,
57
58
  getRectCenters: () => getRectCenters,
58
59
  getRectCorners: () => getRectCorners,
@@ -64,14 +65,14 @@ __export(src_exports, {
64
65
  inset: () => inset,
65
66
  intersection: () => intersection,
66
67
  intersects: () => intersects,
68
+ isPointInPolygon: () => isPointInPolygon,
67
69
  isRect: () => isRect,
68
70
  isSymmetric: () => isSymmetric,
69
71
  rotate: () => rotate,
70
72
  shift: () => shift,
71
73
  shrink: () => shrink,
72
74
  toRad: () => toRad,
73
- union: () => union,
74
- withinPolygon: () => withinPolygon
75
+ union: () => union
75
76
  });
76
77
  module.exports = __toCommonJS(src_exports);
77
78
 
@@ -124,18 +125,23 @@ var hasProp = (obj, prop) => Object.prototype.hasOwnProperty.call(obj, prop);
124
125
 
125
126
  // src/rect.ts
126
127
  var point = (x, y) => ({ x, y });
127
- function createRect(v) {
128
- const midX = v.x + v.width / 2;
129
- const midY = v.y + v.height / 2;
130
- return __spreadProps(__spreadValues({}, v), {
131
- minX: v.x,
132
- minY: v.y,
133
- maxX: v.x + v.width,
134
- maxY: v.y + v.height,
128
+ function createRect(r) {
129
+ const { x, y, width, height } = r;
130
+ const midX = x + width / 2;
131
+ const midY = y + height / 2;
132
+ return {
133
+ x,
134
+ y,
135
+ width,
136
+ height,
137
+ minX: x,
138
+ minY: y,
139
+ maxX: x + width,
140
+ maxY: y + height,
135
141
  midX,
136
142
  midY,
137
143
  center: point(midX, midY)
138
- });
144
+ };
139
145
  }
140
146
  function isRect(v) {
141
147
  return hasProp(v, "x") && hasProp(v, "y") && hasProp(v, "width") && hasProp(v, "height");
@@ -421,6 +427,19 @@ function getViewportRect(win, opts) {
421
427
  return rect;
422
428
  }
423
429
 
430
+ // src/get-polygon.ts
431
+ function getElementPolygon(rectValue, placement) {
432
+ const rect = createRect(rectValue);
433
+ const { top, right, left, bottom } = getRectCorners(rect);
434
+ const [base] = placement.split("-");
435
+ return {
436
+ top: [left, top, right, bottom],
437
+ right: [top, right, bottom, left],
438
+ bottom: [top, left, bottom, right],
439
+ left: [right, top, left, bottom]
440
+ }[base];
441
+ }
442
+
424
443
  // src/operations.ts
425
444
  var isSymmetric = (v) => "dx" in v || "dy" in v;
426
445
  function inset(r, i) {
@@ -452,7 +471,7 @@ function shift(r, o) {
452
471
  }
453
472
 
454
473
  // src/polygon.ts
455
- function withinPolygon(polygon, point2) {
474
+ function isPointInPolygon(polygon, point2) {
456
475
  const { x, y } = point2;
457
476
  let c = false;
458
477
  for (let i = 0, j = polygon.length - 1; i < polygon.length; j = i++) {
@@ -494,3 +513,7 @@ function debugPolygon(polygon) {
494
513
  const points = polygon.map((point2) => `${point2.x},${point2.y}`).join(" ");
495
514
  el.setAttribute("points", points);
496
515
  }
516
+ debugPolygon.remove = () => {
517
+ const el = document.getElementById("debug-polygon");
518
+ el == null ? void 0 : el.remove();
519
+ };
package/dist/index.mjs CHANGED
@@ -67,18 +67,23 @@ var hasProp = (obj, prop) => Object.prototype.hasOwnProperty.call(obj, prop);
67
67
 
68
68
  // src/rect.ts
69
69
  var point = (x, y) => ({ x, y });
70
- function createRect(v) {
71
- const midX = v.x + v.width / 2;
72
- const midY = v.y + v.height / 2;
73
- return __spreadProps(__spreadValues({}, v), {
74
- minX: v.x,
75
- minY: v.y,
76
- maxX: v.x + v.width,
77
- maxY: v.y + v.height,
70
+ function createRect(r) {
71
+ const { x, y, width, height } = r;
72
+ const midX = x + width / 2;
73
+ const midY = y + height / 2;
74
+ return {
75
+ x,
76
+ y,
77
+ width,
78
+ height,
79
+ minX: x,
80
+ minY: y,
81
+ maxX: x + width,
82
+ maxY: y + height,
78
83
  midX,
79
84
  midY,
80
85
  center: point(midX, midY)
81
- });
86
+ };
82
87
  }
83
88
  function isRect(v) {
84
89
  return hasProp(v, "x") && hasProp(v, "y") && hasProp(v, "width") && hasProp(v, "height");
@@ -364,6 +369,19 @@ function getViewportRect(win, opts) {
364
369
  return rect;
365
370
  }
366
371
 
372
+ // src/get-polygon.ts
373
+ function getElementPolygon(rectValue, placement) {
374
+ const rect = createRect(rectValue);
375
+ const { top, right, left, bottom } = getRectCorners(rect);
376
+ const [base] = placement.split("-");
377
+ return {
378
+ top: [left, top, right, bottom],
379
+ right: [top, right, bottom, left],
380
+ bottom: [top, left, bottom, right],
381
+ left: [right, top, left, bottom]
382
+ }[base];
383
+ }
384
+
367
385
  // src/operations.ts
368
386
  var isSymmetric = (v) => "dx" in v || "dy" in v;
369
387
  function inset(r, i) {
@@ -395,7 +413,7 @@ function shift(r, o) {
395
413
  }
396
414
 
397
415
  // src/polygon.ts
398
- function withinPolygon(polygon, point2) {
416
+ function isPointInPolygon(polygon, point2) {
399
417
  const { x, y } = point2;
400
418
  let c = false;
401
419
  for (let i = 0, j = polygon.length - 1; i < polygon.length; j = i++) {
@@ -437,6 +455,10 @@ function debugPolygon(polygon) {
437
455
  const points = polygon.map((point2) => `${point2.x},${point2.y}`).join(" ");
438
456
  el.setAttribute("points", points);
439
457
  }
458
+ debugPolygon.remove = () => {
459
+ const el = document.getElementById("debug-polygon");
460
+ el == null ? void 0 : el.remove();
461
+ };
440
462
  export {
441
463
  alignRect,
442
464
  closest,
@@ -454,6 +476,7 @@ export {
454
476
  distanceFromRect,
455
477
  expand,
456
478
  fromRange,
479
+ getElementPolygon,
457
480
  getElementRect,
458
481
  getRectCenters,
459
482
  getRectCorners,
@@ -465,12 +488,12 @@ export {
465
488
  inset,
466
489
  intersection,
467
490
  intersects,
491
+ isPointInPolygon,
468
492
  isRect,
469
493
  isSymmetric,
470
494
  rotate,
471
495
  shift,
472
496
  shrink,
473
497
  toRad,
474
- union,
475
- withinPolygon
498
+ union
476
499
  };
package/dist/polygon.d.ts CHANGED
@@ -1,3 +1,6 @@
1
1
  import type { Point } from "./types";
2
- export declare function withinPolygon(polygon: Point[], point: Point): boolean;
2
+ export declare function isPointInPolygon(polygon: Point[], point: Point): boolean;
3
3
  export declare function debugPolygon(polygon: Point[]): void;
4
+ export declare namespace debugPolygon {
5
+ var remove: () => void;
6
+ }
package/dist/rect.d.ts CHANGED
@@ -1,5 +1,9 @@
1
1
  import type { RectEdge, RectValue } from "./types";
2
- export declare function createRect(v: RectValue): {
2
+ export declare function createRect(r: RectValue): {
3
+ x: number;
4
+ y: number;
5
+ width: number;
6
+ height: number;
3
7
  minX: number;
4
8
  minY: number;
5
9
  maxX: number;
@@ -10,10 +14,6 @@ export declare function createRect(v: RectValue): {
10
14
  x: number;
11
15
  y: number;
12
16
  };
13
- x: number;
14
- y: number;
15
- width: number;
16
- height: number;
17
17
  };
18
18
  export declare type Rect = ReturnType<typeof createRect>;
19
19
  export declare function isRect(v: any): v is Rect;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zag-js/rect-utils",
3
- "version": "0.0.0-dev-20220706194607",
3
+ "version": "0.0.0-dev-20220709094240",
4
4
  "description": "",
5
5
  "keywords": [
6
6
  "js",
@@ -25,7 +25,7 @@
25
25
  "url": "https://github.com/chakra-ui/zag/issues"
26
26
  },
27
27
  "dependencies": {
28
- "@zag-js/dom-utils": "0.0.0-dev-20220706194607",
28
+ "@zag-js/dom-utils": "0.0.0-dev-20220709094240",
29
29
  "@zag-js/utils": "0.1.2"
30
30
  },
31
31
  "scripts": {