@zag-js/rect-utils 0.70.0 → 0.72.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.
- package/dist/index.js +51 -123
- package/dist/index.mjs +2 -51
- package/package.json +2 -3
- package/dist/index.js.map +0 -1
- package/dist/index.mjs.map +0 -1
- package/src/affine-transform.ts +0 -177
- package/src/align.ts +0 -26
- package/src/clamp.ts +0 -26
- package/src/closest.ts +0 -44
- package/src/compass.ts +0 -25
- package/src/constrain.ts +0 -15
- package/src/contains.ts +0 -14
- package/src/distance.ts +0 -44
- package/src/equality.ts +0 -13
- package/src/from-element.ts +0 -61
- package/src/from-points.ts +0 -15
- package/src/from-range.ts +0 -27
- package/src/from-rotation.ts +0 -41
- package/src/from-window.ts +0 -34
- package/src/index.ts +0 -20
- package/src/intersection.ts +0 -32
- package/src/operations.ts +0 -35
- package/src/polygon.ts +0 -67
- package/src/rect.ts +0 -66
- package/src/resize.ts +0 -106
- package/src/types.ts +0 -92
- package/src/union.ts +0 -16
package/src/polygon.ts
DELETED
|
@@ -1,67 +0,0 @@
|
|
|
1
|
-
import { createRect, getRectCorners } from "./rect"
|
|
2
|
-
import type { Point, RectInit } from "./types"
|
|
3
|
-
|
|
4
|
-
export function getElementPolygon(rectValue: RectInit, placement: string) {
|
|
5
|
-
const rect = createRect(rectValue)
|
|
6
|
-
const { top, right, left, bottom } = getRectCorners(rect)
|
|
7
|
-
const [base] = placement.split("-")
|
|
8
|
-
|
|
9
|
-
return {
|
|
10
|
-
top: [left, top, right, bottom],
|
|
11
|
-
right: [top, right, bottom, left],
|
|
12
|
-
bottom: [top, left, bottom, right],
|
|
13
|
-
left: [right, top, left, bottom],
|
|
14
|
-
}[base]
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
export function isPointInPolygon(polygon: Point[], point: Point) {
|
|
18
|
-
const { x, y } = point
|
|
19
|
-
let c = false
|
|
20
|
-
|
|
21
|
-
for (let i = 0, j = polygon.length - 1; i < polygon.length; j = i++) {
|
|
22
|
-
const xi = polygon[i].x
|
|
23
|
-
const yi = polygon[i].y
|
|
24
|
-
const xj = polygon[j].x
|
|
25
|
-
const yj = polygon[j].y
|
|
26
|
-
|
|
27
|
-
if (yi > y !== yj > y && x < ((xj - xi) * (y - yi)) / (yj - yi) + xi) {
|
|
28
|
-
c = !c
|
|
29
|
-
}
|
|
30
|
-
}
|
|
31
|
-
return c
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
function createPolygonElement() {
|
|
35
|
-
const id = "debug-polygon"
|
|
36
|
-
const existingPolygon = document.getElementById(id)
|
|
37
|
-
if (existingPolygon) {
|
|
38
|
-
return existingPolygon
|
|
39
|
-
}
|
|
40
|
-
const svg = document.createElementNS("http://www.w3.org/2000/svg", "svg")
|
|
41
|
-
Object.assign(svg.style, {
|
|
42
|
-
top: "0",
|
|
43
|
-
left: "0",
|
|
44
|
-
width: "100%",
|
|
45
|
-
height: "100%",
|
|
46
|
-
opacity: "0.15",
|
|
47
|
-
position: "fixed",
|
|
48
|
-
pointerEvents: "none",
|
|
49
|
-
fill: "red",
|
|
50
|
-
})
|
|
51
|
-
|
|
52
|
-
const polygon = document.createElementNS("http://www.w3.org/2000/svg", "polygon")
|
|
53
|
-
polygon.setAttribute("id", id)
|
|
54
|
-
polygon.setAttribute("points", "0,0 0,0")
|
|
55
|
-
svg.appendChild(polygon)
|
|
56
|
-
document.body.appendChild(svg)
|
|
57
|
-
return polygon
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
export function debugPolygon(polygon: Point[]) {
|
|
61
|
-
const el = createPolygonElement()
|
|
62
|
-
const points = polygon.map((point) => `${point.x},${point.y}`).join(" ")
|
|
63
|
-
el.setAttribute("points", points)
|
|
64
|
-
return () => {
|
|
65
|
-
el.remove()
|
|
66
|
-
}
|
|
67
|
-
}
|
package/src/rect.ts
DELETED
|
@@ -1,66 +0,0 @@
|
|
|
1
|
-
import type { Point, Rect, RectEdge, RectInit } from "./types"
|
|
2
|
-
|
|
3
|
-
/* -----------------------------------------------------------------------------
|
|
4
|
-
* Point
|
|
5
|
-
* -----------------------------------------------------------------------------*/
|
|
6
|
-
|
|
7
|
-
export const createPoint = (x: number, y: number) => ({ x, y })
|
|
8
|
-
|
|
9
|
-
export const subtractPoints = (a: Point, b: Point) => createPoint(a.x - b.x, a.y - b.y)
|
|
10
|
-
export const addPoints = (a: Point, b: Point) => createPoint(a.x + b.x, a.y + b.y)
|
|
11
|
-
|
|
12
|
-
export function isPoint(v: any): v is Point {
|
|
13
|
-
return Reflect.has(v, "x") && Reflect.has(v, "y")
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
/* -----------------------------------------------------------------------------
|
|
17
|
-
* Rect
|
|
18
|
-
* -----------------------------------------------------------------------------*/
|
|
19
|
-
|
|
20
|
-
export function createRect(r: RectInit): Rect {
|
|
21
|
-
const { x, y, width, height } = r
|
|
22
|
-
const midX = x + width / 2
|
|
23
|
-
const midY = y + height / 2
|
|
24
|
-
return {
|
|
25
|
-
x,
|
|
26
|
-
y,
|
|
27
|
-
width,
|
|
28
|
-
height,
|
|
29
|
-
minX: x,
|
|
30
|
-
minY: y,
|
|
31
|
-
maxX: x + width,
|
|
32
|
-
maxY: y + height,
|
|
33
|
-
midX,
|
|
34
|
-
midY,
|
|
35
|
-
center: createPoint(midX, midY),
|
|
36
|
-
}
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
export function isRect(v: any): v is Rect {
|
|
40
|
-
return Reflect.has(v, "x") && Reflect.has(v, "y") && Reflect.has(v, "width") && Reflect.has(v, "height")
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
export function getRectCenters(v: Rect) {
|
|
44
|
-
const top = createPoint(v.midX, v.minY)
|
|
45
|
-
const right = createPoint(v.maxX, v.midY)
|
|
46
|
-
const bottom = createPoint(v.midX, v.maxY)
|
|
47
|
-
const left = createPoint(v.minX, v.midY)
|
|
48
|
-
return { top, right, bottom, left }
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
export function getRectCorners(v: Rect) {
|
|
52
|
-
const top = createPoint(v.minX, v.minY)
|
|
53
|
-
const right = createPoint(v.maxX, v.minY)
|
|
54
|
-
const bottom = createPoint(v.maxX, v.maxY)
|
|
55
|
-
const left = createPoint(v.minX, v.maxY)
|
|
56
|
-
return { top, right, bottom, left }
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
export function getRectEdges(v: Rect) {
|
|
60
|
-
const c = getRectCorners(v)
|
|
61
|
-
const top: RectEdge = [c.top, c.right]
|
|
62
|
-
const right: RectEdge = [c.right, c.bottom]
|
|
63
|
-
const bottom: RectEdge = [c.left, c.bottom]
|
|
64
|
-
const left: RectEdge = [c.top, c.left]
|
|
65
|
-
return { top, right, bottom, left }
|
|
66
|
-
}
|
package/src/resize.ts
DELETED
|
@@ -1,106 +0,0 @@
|
|
|
1
|
-
import { AffineTransform } from "./affine-transform"
|
|
2
|
-
import { compassDirectionMap, oppositeDirectionMap, type CompassDirection } from "./compass"
|
|
3
|
-
import type { Point, Rect, RectInit, ScalingOptions } from "./types"
|
|
4
|
-
|
|
5
|
-
const { sign, abs, min } = Math
|
|
6
|
-
|
|
7
|
-
function getRectExtentPoint(rect: Rect, direction: CompassDirection) {
|
|
8
|
-
const { minX, minY, maxX, maxY, midX, midY } = rect
|
|
9
|
-
const x = direction.includes("w") ? minX : direction.includes("e") ? maxX : midX
|
|
10
|
-
const y = direction.includes("n") ? minY : direction.includes("s") ? maxY : midY
|
|
11
|
-
return { x, y }
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
function getOppositeDirection(direction: CompassDirection) {
|
|
15
|
-
return oppositeDirectionMap[direction]
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
export function resizeRect(rect: Rect, offset: Point, direction: CompassDirection, opts: ScalingOptions) {
|
|
19
|
-
const { scalingOriginMode, lockAspectRatio } = opts
|
|
20
|
-
|
|
21
|
-
const extent = getRectExtentPoint(rect, direction)
|
|
22
|
-
|
|
23
|
-
const oppositeDirection = getOppositeDirection(direction)
|
|
24
|
-
const oppositeExtent = getRectExtentPoint(rect, oppositeDirection)
|
|
25
|
-
|
|
26
|
-
if (scalingOriginMode === "center") {
|
|
27
|
-
offset = { x: offset.x * 2, y: offset.y * 2 }
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
const newExtent = {
|
|
31
|
-
x: extent.x + offset.x,
|
|
32
|
-
y: extent.y + offset.y,
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
const multiplier = {
|
|
36
|
-
x: compassDirectionMap[direction].x * 2 - 1,
|
|
37
|
-
y: compassDirectionMap[direction].y * 2 - 1,
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
const newSize = {
|
|
41
|
-
width: newExtent.x - oppositeExtent.x,
|
|
42
|
-
height: newExtent.y - oppositeExtent.y,
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
const scaleX = (multiplier.x * newSize.width) / rect.width
|
|
46
|
-
const scaleY = (multiplier.y * newSize.height) / rect.height
|
|
47
|
-
|
|
48
|
-
const largestMagnitude = abs(scaleX) > abs(scaleY) ? scaleX : scaleY
|
|
49
|
-
|
|
50
|
-
const scale = lockAspectRatio
|
|
51
|
-
? { x: largestMagnitude, y: largestMagnitude }
|
|
52
|
-
: {
|
|
53
|
-
x: extent.x === oppositeExtent.x ? 1 : scaleX,
|
|
54
|
-
y: extent.y === oppositeExtent.y ? 1 : scaleY,
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
if (extent.y === oppositeExtent.y) {
|
|
58
|
-
scale.y = abs(scale.y)
|
|
59
|
-
} else if (sign(scale.y) !== sign(scaleY)) {
|
|
60
|
-
scale.y *= -1
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
if (extent.x === oppositeExtent.x) {
|
|
64
|
-
scale.x = abs(scale.x)
|
|
65
|
-
} else if (sign(scale.x) !== sign(scaleX)) {
|
|
66
|
-
scale.x *= -1
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
switch (scalingOriginMode) {
|
|
70
|
-
case "extent":
|
|
71
|
-
return transformRect(rect, AffineTransform.scale(scale.x, scale.y, oppositeExtent), false)
|
|
72
|
-
case "center":
|
|
73
|
-
return transformRect(
|
|
74
|
-
rect,
|
|
75
|
-
AffineTransform.scale(scale.x, scale.y, {
|
|
76
|
-
x: rect.midX,
|
|
77
|
-
y: rect.midY,
|
|
78
|
-
}),
|
|
79
|
-
false,
|
|
80
|
-
)
|
|
81
|
-
}
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
function createRectFromPoints(initialPoint: Point, finalPoint: Point, normalized: boolean = true): RectInit {
|
|
85
|
-
if (normalized) {
|
|
86
|
-
return {
|
|
87
|
-
x: min(finalPoint.x, initialPoint.x),
|
|
88
|
-
y: min(finalPoint.y, initialPoint.y),
|
|
89
|
-
width: abs(finalPoint.x - initialPoint.x),
|
|
90
|
-
height: abs(finalPoint.y - initialPoint.y),
|
|
91
|
-
}
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
return {
|
|
95
|
-
x: initialPoint.x,
|
|
96
|
-
y: initialPoint.y,
|
|
97
|
-
width: finalPoint.x - initialPoint.x,
|
|
98
|
-
height: finalPoint.y - initialPoint.y,
|
|
99
|
-
}
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
function transformRect(rect: Rect, transform: AffineTransform, normalized = true): RectInit {
|
|
103
|
-
const p1 = transform.applyTo({ x: rect.minX, y: rect.minY })
|
|
104
|
-
const p2 = transform.applyTo({ x: rect.maxX, y: rect.maxY })
|
|
105
|
-
return createRectFromPoints(p1, p2, normalized)
|
|
106
|
-
}
|
package/src/types.ts
DELETED
|
@@ -1,92 +0,0 @@
|
|
|
1
|
-
/* -----------------------------------------------------------------------------
|
|
2
|
-
* Basic
|
|
3
|
-
* -----------------------------------------------------------------------------*/
|
|
4
|
-
|
|
5
|
-
export interface Point {
|
|
6
|
-
x: number
|
|
7
|
-
y: number
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
export interface Size {
|
|
11
|
-
width: number
|
|
12
|
-
height: number
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
export interface Bounds {
|
|
16
|
-
minX: number
|
|
17
|
-
midX: number
|
|
18
|
-
maxX: number
|
|
19
|
-
minY: number
|
|
20
|
-
midY: number
|
|
21
|
-
maxY: number
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
export interface CenterPoint {
|
|
25
|
-
center: Point
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
export interface RectInit extends Point, Size {}
|
|
29
|
-
|
|
30
|
-
export interface Rect extends Point, Size, Bounds, CenterPoint {}
|
|
31
|
-
|
|
32
|
-
/* -----------------------------------------------------------------------------
|
|
33
|
-
* Edge and Side
|
|
34
|
-
* -----------------------------------------------------------------------------*/
|
|
35
|
-
|
|
36
|
-
export type RectSide = "top" | "right" | "bottom" | "left"
|
|
37
|
-
|
|
38
|
-
export type RectPoint =
|
|
39
|
-
| "top-left"
|
|
40
|
-
| "top-center"
|
|
41
|
-
| "top-right"
|
|
42
|
-
| "right-center"
|
|
43
|
-
| "left-center"
|
|
44
|
-
| "bottom-left"
|
|
45
|
-
| "bottom-right"
|
|
46
|
-
| "bottom-center"
|
|
47
|
-
| "center"
|
|
48
|
-
|
|
49
|
-
export type RectEdge = [Point, Point]
|
|
50
|
-
|
|
51
|
-
export type RectPoints = [Point, Point, Point, Point]
|
|
52
|
-
|
|
53
|
-
export type RectEdges = Record<RectSide, RectEdge> & {
|
|
54
|
-
value: RectEdge[]
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
export type RectCorner = "topLeft" | "topRight" | "bottomLeft" | "bottomRight"
|
|
58
|
-
|
|
59
|
-
export type RectCorners = Record<RectCorner, Point> & {
|
|
60
|
-
value: RectPoints
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
export type RectCenter = "topCenter" | "rightCenter" | "leftCenter" | "bottomCenter"
|
|
64
|
-
|
|
65
|
-
export type RectCenters = Record<RectCenter, Point> & {
|
|
66
|
-
value: RectPoints
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
export type RectInset = Partial<Record<RectSide, number>>
|
|
70
|
-
|
|
71
|
-
export interface SymmetricRectInset {
|
|
72
|
-
dx?: number
|
|
73
|
-
dy?: number
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
export interface ScalingOptions {
|
|
77
|
-
scalingOriginMode: "center" | "extent"
|
|
78
|
-
lockAspectRatio: boolean
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
/* -----------------------------------------------------------------------------
|
|
82
|
-
* Alignment
|
|
83
|
-
* -----------------------------------------------------------------------------*/
|
|
84
|
-
|
|
85
|
-
export interface AlignOptions {
|
|
86
|
-
h: HAlign
|
|
87
|
-
v: VAlign
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
export type HAlign = "left-inside" | "left-outside" | "center" | "right-inside" | "right-outside"
|
|
91
|
-
|
|
92
|
-
export type VAlign = "top-inside" | "top-outside" | "center" | "bottom-inside" | "bottom-outside"
|
package/src/union.ts
DELETED
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
import { getRectFromPoints } from "./from-points"
|
|
2
|
-
import type { Rect } from "./types"
|
|
3
|
-
|
|
4
|
-
const { min, max } = Math
|
|
5
|
-
|
|
6
|
-
export function union(...rs: Rect[]): Rect {
|
|
7
|
-
const pMin = {
|
|
8
|
-
x: min(...rs.map((r) => r.minX)),
|
|
9
|
-
y: min(...rs.map((r) => r.minY)),
|
|
10
|
-
}
|
|
11
|
-
const pMax = {
|
|
12
|
-
x: max(...rs.map((r) => r.maxX)),
|
|
13
|
-
y: max(...rs.map((r) => r.maxY)),
|
|
14
|
-
}
|
|
15
|
-
return getRectFromPoints(pMin, pMax)
|
|
16
|
-
}
|