@zag-js/rect-utils 0.39.0 → 0.40.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.d.mts +147 -79
- package/dist/index.d.ts +147 -79
- package/dist/index.js +391 -72
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +381 -72
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
- package/src/affine-transform.ts +177 -0
- package/src/align.ts +11 -44
- package/src/clamp.ts +26 -0
- package/src/closest.ts +5 -14
- package/src/compass.ts +25 -0
- package/src/constrain.ts +18 -0
- package/src/contains.ts +2 -2
- package/src/distance.ts +4 -3
- package/src/equality.ts +13 -0
- package/src/from-element.ts +3 -1
- package/src/from-points.ts +2 -2
- package/src/from-range.ts +2 -1
- package/src/from-rotation.ts +2 -2
- package/src/from-window.ts +2 -1
- package/src/index.ts +5 -1
- package/src/intersection.ts +2 -2
- package/src/operations.ts +2 -2
- package/src/polygon.ts +15 -1
- package/src/rect.ts +28 -18
- package/src/resize.ts +106 -0
- package/src/types.ts +50 -3
- package/src/union.ts +5 -17
- package/src/get-polygon.ts +0 -15
package/dist/index.js
CHANGED
|
@@ -3,6 +3,7 @@ var __defProp = Object.defineProperty;
|
|
|
3
3
|
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
4
|
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
5
|
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
6
7
|
var __export = (target, all) => {
|
|
7
8
|
for (var name in all)
|
|
8
9
|
__defProp(target, name, { get: all[name], enumerable: true });
|
|
@@ -16,18 +17,28 @@ var __copyProps = (to, from, except, desc) => {
|
|
|
16
17
|
return to;
|
|
17
18
|
};
|
|
18
19
|
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
20
|
+
var __publicField = (obj, key, value) => {
|
|
21
|
+
__defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
22
|
+
return value;
|
|
23
|
+
};
|
|
19
24
|
|
|
20
25
|
// src/index.ts
|
|
21
26
|
var src_exports = {};
|
|
22
27
|
__export(src_exports, {
|
|
28
|
+
AffineTransform: () => AffineTransform,
|
|
29
|
+
addPoints: () => addPoints,
|
|
23
30
|
alignRect: () => alignRect,
|
|
31
|
+
clampPoint: () => clampPoint,
|
|
32
|
+
clampSize: () => clampSize,
|
|
24
33
|
closest: () => closest,
|
|
25
34
|
closestSideToPoint: () => closestSideToPoint,
|
|
26
35
|
closestSideToRect: () => closestSideToRect,
|
|
27
36
|
collisions: () => collisions,
|
|
37
|
+
constrainRect: () => constrainRect,
|
|
28
38
|
contains: () => contains,
|
|
29
39
|
containsPoint: () => containsPoint,
|
|
30
40
|
containsRect: () => containsRect,
|
|
41
|
+
createPoint: () => createPoint,
|
|
31
42
|
createRect: () => createRect,
|
|
32
43
|
debugPolygon: () => debugPolygon,
|
|
33
44
|
distance: () => distance,
|
|
@@ -48,54 +59,211 @@ __export(src_exports, {
|
|
|
48
59
|
inset: () => inset,
|
|
49
60
|
intersection: () => intersection,
|
|
50
61
|
intersects: () => intersects,
|
|
62
|
+
isPoint: () => isPoint,
|
|
63
|
+
isPointEqual: () => isPointEqual,
|
|
51
64
|
isPointInPolygon: () => isPointInPolygon,
|
|
52
65
|
isRect: () => isRect,
|
|
66
|
+
isRectEqual: () => isRectEqual,
|
|
67
|
+
isSizeEqual: () => isSizeEqual,
|
|
53
68
|
isSymmetric: () => isSymmetric,
|
|
69
|
+
resizeRect: () => resizeRect,
|
|
54
70
|
rotate: () => rotate,
|
|
55
71
|
shift: () => shift,
|
|
56
72
|
shrink: () => shrink,
|
|
73
|
+
subtractPoints: () => subtractPoints,
|
|
57
74
|
toRad: () => toRad,
|
|
58
75
|
union: () => union
|
|
59
76
|
});
|
|
60
77
|
module.exports = __toCommonJS(src_exports);
|
|
61
78
|
|
|
79
|
+
// src/affine-transform.ts
|
|
80
|
+
var AffineTransform = class _AffineTransform {
|
|
81
|
+
constructor([m00, m01, m02, m10, m11, m12] = [0, 0, 0, 0, 0, 0]) {
|
|
82
|
+
__publicField(this, "m00");
|
|
83
|
+
__publicField(this, "m01");
|
|
84
|
+
__publicField(this, "m02");
|
|
85
|
+
__publicField(this, "m10");
|
|
86
|
+
__publicField(this, "m11");
|
|
87
|
+
__publicField(this, "m12");
|
|
88
|
+
__publicField(this, "rotate", (...args) => {
|
|
89
|
+
return this.prepend(_AffineTransform.rotate(...args));
|
|
90
|
+
});
|
|
91
|
+
__publicField(this, "scale", (...args) => {
|
|
92
|
+
return this.prepend(_AffineTransform.scale(...args));
|
|
93
|
+
});
|
|
94
|
+
__publicField(this, "translate", (...args) => {
|
|
95
|
+
return this.prepend(_AffineTransform.translate(...args));
|
|
96
|
+
});
|
|
97
|
+
this.m00 = m00;
|
|
98
|
+
this.m01 = m01;
|
|
99
|
+
this.m02 = m02;
|
|
100
|
+
this.m10 = m10;
|
|
101
|
+
this.m11 = m11;
|
|
102
|
+
this.m12 = m12;
|
|
103
|
+
}
|
|
104
|
+
applyTo(point) {
|
|
105
|
+
const { x, y } = point;
|
|
106
|
+
const { m00, m01, m02, m10, m11, m12 } = this;
|
|
107
|
+
return {
|
|
108
|
+
x: m00 * x + m01 * y + m02,
|
|
109
|
+
y: m10 * x + m11 * y + m12
|
|
110
|
+
};
|
|
111
|
+
}
|
|
112
|
+
prepend(other) {
|
|
113
|
+
return new _AffineTransform([
|
|
114
|
+
this.m00 * other.m00 + this.m01 * other.m10,
|
|
115
|
+
// m00
|
|
116
|
+
this.m00 * other.m01 + this.m01 * other.m11,
|
|
117
|
+
// m01
|
|
118
|
+
this.m00 * other.m02 + this.m01 * other.m12 + this.m02,
|
|
119
|
+
// m02
|
|
120
|
+
this.m10 * other.m00 + this.m11 * other.m10,
|
|
121
|
+
// m10
|
|
122
|
+
this.m10 * other.m01 + this.m11 * other.m11,
|
|
123
|
+
// m11
|
|
124
|
+
this.m10 * other.m02 + this.m11 * other.m12 + this.m12
|
|
125
|
+
// m12
|
|
126
|
+
]);
|
|
127
|
+
}
|
|
128
|
+
append(other) {
|
|
129
|
+
return new _AffineTransform([
|
|
130
|
+
other.m00 * this.m00 + other.m01 * this.m10,
|
|
131
|
+
// m00
|
|
132
|
+
other.m00 * this.m01 + other.m01 * this.m11,
|
|
133
|
+
// m01
|
|
134
|
+
other.m00 * this.m02 + other.m01 * this.m12 + other.m02,
|
|
135
|
+
// m02
|
|
136
|
+
other.m10 * this.m00 + other.m11 * this.m10,
|
|
137
|
+
// m10
|
|
138
|
+
other.m10 * this.m01 + other.m11 * this.m11,
|
|
139
|
+
// m11
|
|
140
|
+
other.m10 * this.m02 + other.m11 * this.m12 + other.m12
|
|
141
|
+
// m12
|
|
142
|
+
]);
|
|
143
|
+
}
|
|
144
|
+
get determinant() {
|
|
145
|
+
return this.m00 * this.m11 - this.m01 * this.m10;
|
|
146
|
+
}
|
|
147
|
+
get isInvertible() {
|
|
148
|
+
const det = this.determinant;
|
|
149
|
+
return isFinite(det) && isFinite(this.m02) && isFinite(this.m12) && det !== 0;
|
|
150
|
+
}
|
|
151
|
+
invert() {
|
|
152
|
+
const det = this.determinant;
|
|
153
|
+
return new _AffineTransform([
|
|
154
|
+
this.m11 / det,
|
|
155
|
+
// m00
|
|
156
|
+
-this.m01 / det,
|
|
157
|
+
// m01
|
|
158
|
+
(this.m01 * this.m12 - this.m11 * this.m02) / det,
|
|
159
|
+
// m02
|
|
160
|
+
-this.m10 / det,
|
|
161
|
+
// m10
|
|
162
|
+
this.m00 / det,
|
|
163
|
+
// m11
|
|
164
|
+
(this.m10 * this.m02 - this.m00 * this.m12) / det
|
|
165
|
+
// m12
|
|
166
|
+
]);
|
|
167
|
+
}
|
|
168
|
+
get array() {
|
|
169
|
+
return [this.m00, this.m01, this.m02, this.m10, this.m11, this.m12, 0, 0, 1];
|
|
170
|
+
}
|
|
171
|
+
get float32Array() {
|
|
172
|
+
return new Float32Array(this.array);
|
|
173
|
+
}
|
|
174
|
+
// Static
|
|
175
|
+
static get identity() {
|
|
176
|
+
return new _AffineTransform([1, 0, 0, 0, 1, 0]);
|
|
177
|
+
}
|
|
178
|
+
static rotate(theta, origin) {
|
|
179
|
+
const rotation = new _AffineTransform([Math.cos(theta), -Math.sin(theta), 0, Math.sin(theta), Math.cos(theta), 0]);
|
|
180
|
+
if (origin && (origin.x !== 0 || origin.y !== 0)) {
|
|
181
|
+
return _AffineTransform.multiply(
|
|
182
|
+
_AffineTransform.translate(origin.x, origin.y),
|
|
183
|
+
rotation,
|
|
184
|
+
_AffineTransform.translate(-origin.x, -origin.y)
|
|
185
|
+
);
|
|
186
|
+
}
|
|
187
|
+
return rotation;
|
|
188
|
+
}
|
|
189
|
+
static scale(sx, sy = sx, origin = { x: 0, y: 0 }) {
|
|
190
|
+
const scale = new _AffineTransform([sx, 0, 0, 0, sy, 0]);
|
|
191
|
+
if (origin.x !== 0 || origin.y !== 0) {
|
|
192
|
+
return _AffineTransform.multiply(
|
|
193
|
+
_AffineTransform.translate(origin.x, origin.y),
|
|
194
|
+
scale,
|
|
195
|
+
_AffineTransform.translate(-origin.x, -origin.y)
|
|
196
|
+
);
|
|
197
|
+
}
|
|
198
|
+
return scale;
|
|
199
|
+
}
|
|
200
|
+
static translate(tx, ty) {
|
|
201
|
+
return new _AffineTransform([1, 0, tx, 0, 1, ty]);
|
|
202
|
+
}
|
|
203
|
+
static multiply(...[first, ...rest]) {
|
|
204
|
+
if (!first)
|
|
205
|
+
return _AffineTransform.identity;
|
|
206
|
+
return rest.reduce((result, item) => result.prepend(item), first);
|
|
207
|
+
}
|
|
208
|
+
get a() {
|
|
209
|
+
return this.m00;
|
|
210
|
+
}
|
|
211
|
+
get b() {
|
|
212
|
+
return this.m10;
|
|
213
|
+
}
|
|
214
|
+
get c() {
|
|
215
|
+
return this.m01;
|
|
216
|
+
}
|
|
217
|
+
get d() {
|
|
218
|
+
return this.m11;
|
|
219
|
+
}
|
|
220
|
+
get tx() {
|
|
221
|
+
return this.m02;
|
|
222
|
+
}
|
|
223
|
+
get ty() {
|
|
224
|
+
return this.m12;
|
|
225
|
+
}
|
|
226
|
+
get scaleComponents() {
|
|
227
|
+
return { x: this.a, y: this.d };
|
|
228
|
+
}
|
|
229
|
+
get translationComponents() {
|
|
230
|
+
return { x: this.tx, y: this.ty };
|
|
231
|
+
}
|
|
232
|
+
get skewComponents() {
|
|
233
|
+
return { x: this.c, y: this.b };
|
|
234
|
+
}
|
|
235
|
+
toString() {
|
|
236
|
+
return `matrix(${this.a}, ${this.b}, ${this.c}, ${this.d}, ${this.tx}, ${this.ty})`;
|
|
237
|
+
}
|
|
238
|
+
};
|
|
239
|
+
|
|
62
240
|
// src/align.ts
|
|
63
241
|
function hAlign(a, ref, h) {
|
|
64
242
|
let x = ref.minX;
|
|
65
|
-
if (h === "left-inside")
|
|
243
|
+
if (h === "left-inside")
|
|
66
244
|
x = ref.minX;
|
|
67
|
-
|
|
68
|
-
if (h === "left-outside") {
|
|
245
|
+
if (h === "left-outside")
|
|
69
246
|
x = ref.minX - ref.width;
|
|
70
|
-
|
|
71
|
-
if (h === "right-inside") {
|
|
247
|
+
if (h === "right-inside")
|
|
72
248
|
x = ref.maxX - ref.width;
|
|
73
|
-
|
|
74
|
-
if (h === "right-outside") {
|
|
249
|
+
if (h === "right-outside")
|
|
75
250
|
x = ref.maxX;
|
|
76
|
-
|
|
77
|
-
if (h === "center") {
|
|
251
|
+
if (h === "center")
|
|
78
252
|
x = ref.midX - ref.width / 2;
|
|
79
|
-
}
|
|
80
253
|
return { ...a, x };
|
|
81
254
|
}
|
|
82
255
|
function vAlign(a, ref, v) {
|
|
83
256
|
let y = ref.minY;
|
|
84
|
-
if (v === "top-inside")
|
|
257
|
+
if (v === "top-inside")
|
|
85
258
|
y = ref.minY;
|
|
86
|
-
|
|
87
|
-
if (v === "top-outside") {
|
|
259
|
+
if (v === "top-outside")
|
|
88
260
|
y = ref.minY - a.height;
|
|
89
|
-
|
|
90
|
-
if (v === "bottom-inside") {
|
|
261
|
+
if (v === "bottom-inside")
|
|
91
262
|
y = ref.maxY - a.height;
|
|
92
|
-
|
|
93
|
-
if (v === "bottom-outside") {
|
|
263
|
+
if (v === "bottom-outside")
|
|
94
264
|
y = ref.maxY;
|
|
95
|
-
|
|
96
|
-
if (v === "center") {
|
|
265
|
+
if (v === "center")
|
|
97
266
|
y = ref.midY - a.height / 2;
|
|
98
|
-
}
|
|
99
267
|
return { ...a, y };
|
|
100
268
|
}
|
|
101
269
|
function alignRect(a, ref, options) {
|
|
@@ -103,8 +271,35 @@ function alignRect(a, ref, options) {
|
|
|
103
271
|
return vAlign(hAlign(a, ref, h), ref, v);
|
|
104
272
|
}
|
|
105
273
|
|
|
274
|
+
// src/clamp.ts
|
|
275
|
+
var clamp = (value, min3, max2) => Math.min(Math.max(value, min3), max2);
|
|
276
|
+
var clampPoint = (position, size, boundaryRect) => {
|
|
277
|
+
const x = clamp(position.x, boundaryRect.x, boundaryRect.x + boundaryRect.width - size.width);
|
|
278
|
+
const y = clamp(position.y, boundaryRect.y, boundaryRect.y + boundaryRect.height - size.height);
|
|
279
|
+
return { x, y };
|
|
280
|
+
};
|
|
281
|
+
var defaultMinSize = {
|
|
282
|
+
width: 0,
|
|
283
|
+
height: 0
|
|
284
|
+
};
|
|
285
|
+
var defaultMaxSize = {
|
|
286
|
+
width: Infinity,
|
|
287
|
+
height: Infinity
|
|
288
|
+
};
|
|
289
|
+
var clampSize = (size, minSize = defaultMinSize, maxSize = defaultMaxSize) => {
|
|
290
|
+
return {
|
|
291
|
+
width: Math.min(Math.max(size.width, minSize.width), maxSize.width),
|
|
292
|
+
height: Math.min(Math.max(size.height, minSize.height), maxSize.height)
|
|
293
|
+
};
|
|
294
|
+
};
|
|
295
|
+
|
|
106
296
|
// src/rect.ts
|
|
107
|
-
var
|
|
297
|
+
var createPoint = (x, y) => ({ x, y });
|
|
298
|
+
var subtractPoints = (a, b) => createPoint(a.x - b.x, a.y - b.y);
|
|
299
|
+
var addPoints = (a, b) => createPoint(a.x + b.x, a.y + b.y);
|
|
300
|
+
function isPoint(v) {
|
|
301
|
+
return Reflect.has(v, "x") && Reflect.has(v, "y");
|
|
302
|
+
}
|
|
108
303
|
function createRect(r) {
|
|
109
304
|
const { x, y, width, height } = r;
|
|
110
305
|
const midX = x + width / 2;
|
|
@@ -120,25 +315,24 @@ function createRect(r) {
|
|
|
120
315
|
maxY: y + height,
|
|
121
316
|
midX,
|
|
122
317
|
midY,
|
|
123
|
-
center:
|
|
318
|
+
center: createPoint(midX, midY)
|
|
124
319
|
};
|
|
125
320
|
}
|
|
126
|
-
var hasProp = (obj, prop) => Object.prototype.hasOwnProperty.call(obj, prop);
|
|
127
321
|
function isRect(v) {
|
|
128
|
-
return
|
|
322
|
+
return Reflect.has(v, "x") && Reflect.has(v, "y") && Reflect.has(v, "width") && Reflect.has(v, "height");
|
|
129
323
|
}
|
|
130
324
|
function getRectCenters(v) {
|
|
131
|
-
const top =
|
|
132
|
-
const right =
|
|
133
|
-
const bottom =
|
|
134
|
-
const left =
|
|
325
|
+
const top = createPoint(v.midX, v.minY);
|
|
326
|
+
const right = createPoint(v.maxX, v.midY);
|
|
327
|
+
const bottom = createPoint(v.midX, v.maxY);
|
|
328
|
+
const left = createPoint(v.minX, v.midY);
|
|
135
329
|
return { top, right, bottom, left };
|
|
136
330
|
}
|
|
137
331
|
function getRectCorners(v) {
|
|
138
|
-
const top =
|
|
139
|
-
const right =
|
|
140
|
-
const bottom =
|
|
141
|
-
const left =
|
|
332
|
+
const top = createPoint(v.minX, v.minY);
|
|
333
|
+
const right = createPoint(v.maxX, v.minY);
|
|
334
|
+
const bottom = createPoint(v.maxX, v.maxY);
|
|
335
|
+
const left = createPoint(v.minX, v.maxY);
|
|
142
336
|
return { top, right, bottom, left };
|
|
143
337
|
}
|
|
144
338
|
function getRectEdges(v) {
|
|
@@ -218,18 +412,14 @@ function closest(...pts) {
|
|
|
218
412
|
};
|
|
219
413
|
}
|
|
220
414
|
function closestSideToRect(ref, r) {
|
|
221
|
-
if (r.maxX <= ref.minX)
|
|
415
|
+
if (r.maxX <= ref.minX)
|
|
222
416
|
return "left";
|
|
223
|
-
|
|
224
|
-
if (r.minX >= ref.maxX) {
|
|
417
|
+
if (r.minX >= ref.maxX)
|
|
225
418
|
return "right";
|
|
226
|
-
|
|
227
|
-
if (r.maxY <= ref.minY) {
|
|
419
|
+
if (r.maxY <= ref.minY)
|
|
228
420
|
return "top";
|
|
229
|
-
|
|
230
|
-
if (r.minY >= ref.maxY) {
|
|
421
|
+
if (r.minY >= ref.maxY)
|
|
231
422
|
return "bottom";
|
|
232
|
-
}
|
|
233
423
|
return "left";
|
|
234
424
|
}
|
|
235
425
|
function closestSideToPoint(ref, p) {
|
|
@@ -254,6 +444,20 @@ function closestSideToPoint(ref, p) {
|
|
|
254
444
|
return side;
|
|
255
445
|
}
|
|
256
446
|
|
|
447
|
+
// src/constrain.ts
|
|
448
|
+
var constrainRect = (rect, boundary) => {
|
|
449
|
+
const { x, y, width, height } = rect;
|
|
450
|
+
const { x: bx, y: by, width: bw, height: bh } = boundary;
|
|
451
|
+
const left = Math.max(bx, Math.min(x, bx + bw - width));
|
|
452
|
+
const top = Math.max(by, Math.min(y, by + bh - height));
|
|
453
|
+
return {
|
|
454
|
+
x: left,
|
|
455
|
+
y: top,
|
|
456
|
+
width: Math.min(width, bw),
|
|
457
|
+
height: Math.min(height, bh)
|
|
458
|
+
};
|
|
459
|
+
};
|
|
460
|
+
|
|
257
461
|
// src/contains.ts
|
|
258
462
|
function containsPoint(r, p) {
|
|
259
463
|
return r.minX <= p.x && p.x <= r.maxX && r.minY <= p.y && p.y <= r.maxY;
|
|
@@ -265,6 +469,17 @@ function contains(r, v) {
|
|
|
265
469
|
return isRect(v) ? containsRect(r, v) : containsPoint(r, v);
|
|
266
470
|
}
|
|
267
471
|
|
|
472
|
+
// src/equality.ts
|
|
473
|
+
var isSizeEqual = (a, b) => {
|
|
474
|
+
return a.width === b.width && a.height === b.height;
|
|
475
|
+
};
|
|
476
|
+
var isPointEqual = (a, b) => {
|
|
477
|
+
return a.x === b.x && a.y === b.y;
|
|
478
|
+
};
|
|
479
|
+
var isRectEqual = (a, b) => {
|
|
480
|
+
return isPointEqual(a, b) && isSizeEqual(a, b);
|
|
481
|
+
};
|
|
482
|
+
|
|
268
483
|
// src/from-element.ts
|
|
269
484
|
var styleCache = /* @__PURE__ */ new WeakMap();
|
|
270
485
|
function getCacheComputedStyle(el) {
|
|
@@ -317,24 +532,12 @@ function getRectFromPoints(...pts) {
|
|
|
317
532
|
var { min, max } = Math;
|
|
318
533
|
function union(...rs) {
|
|
319
534
|
const pMin = {
|
|
320
|
-
x: min.
|
|
321
|
-
|
|
322
|
-
rs.map((r) => r.minX)
|
|
323
|
-
),
|
|
324
|
-
y: min.apply(
|
|
325
|
-
Math,
|
|
326
|
-
rs.map((r) => r.minY)
|
|
327
|
-
)
|
|
535
|
+
x: min(...rs.map((r) => r.minX)),
|
|
536
|
+
y: min(...rs.map((r) => r.minY))
|
|
328
537
|
};
|
|
329
538
|
const pMax = {
|
|
330
|
-
x: max.
|
|
331
|
-
|
|
332
|
-
rs.map((r) => r.maxX)
|
|
333
|
-
),
|
|
334
|
-
y: max.apply(
|
|
335
|
-
Math,
|
|
336
|
-
rs.map((r) => r.maxY)
|
|
337
|
-
)
|
|
539
|
+
x: max(...rs.map((r) => r.maxX)),
|
|
540
|
+
y: max(...rs.map((r) => r.maxY))
|
|
338
541
|
};
|
|
339
542
|
return getRectFromPoints(pMin, pMax);
|
|
340
543
|
}
|
|
@@ -408,19 +611,6 @@ function getViewportRect(win, opts) {
|
|
|
408
611
|
return rect;
|
|
409
612
|
}
|
|
410
613
|
|
|
411
|
-
// src/get-polygon.ts
|
|
412
|
-
function getElementPolygon(rectValue, placement) {
|
|
413
|
-
const rect = createRect(rectValue);
|
|
414
|
-
const { top, right, left, bottom } = getRectCorners(rect);
|
|
415
|
-
const [base] = placement.split("-");
|
|
416
|
-
return {
|
|
417
|
-
top: [left, top, right, bottom],
|
|
418
|
-
right: [top, right, bottom, left],
|
|
419
|
-
bottom: [top, left, bottom, right],
|
|
420
|
-
left: [right, top, left, bottom]
|
|
421
|
-
}[base];
|
|
422
|
-
}
|
|
423
|
-
|
|
424
614
|
// src/operations.ts
|
|
425
615
|
var isSymmetric = (v) => "dx" in v || "dy" in v;
|
|
426
616
|
function inset(r, i) {
|
|
@@ -452,8 +642,19 @@ function shift(r, o) {
|
|
|
452
642
|
}
|
|
453
643
|
|
|
454
644
|
// src/polygon.ts
|
|
455
|
-
function
|
|
456
|
-
const
|
|
645
|
+
function getElementPolygon(rectValue, placement) {
|
|
646
|
+
const rect = createRect(rectValue);
|
|
647
|
+
const { top, right, left, bottom } = getRectCorners(rect);
|
|
648
|
+
const [base] = placement.split("-");
|
|
649
|
+
return {
|
|
650
|
+
top: [left, top, right, bottom],
|
|
651
|
+
right: [top, right, bottom, left],
|
|
652
|
+
bottom: [top, left, bottom, right],
|
|
653
|
+
left: [right, top, left, bottom]
|
|
654
|
+
}[base];
|
|
655
|
+
}
|
|
656
|
+
function isPointInPolygon(polygon, point) {
|
|
657
|
+
const { x, y } = point;
|
|
457
658
|
let c = false;
|
|
458
659
|
for (let i = 0, j = polygon.length - 1; i < polygon.length; j = i++) {
|
|
459
660
|
const xi = polygon[i].x;
|
|
@@ -492,22 +693,134 @@ function createPolygonElement() {
|
|
|
492
693
|
}
|
|
493
694
|
function debugPolygon(polygon) {
|
|
494
695
|
const el = createPolygonElement();
|
|
495
|
-
const points = polygon.map((
|
|
696
|
+
const points = polygon.map((point) => `${point.x},${point.y}`).join(" ");
|
|
496
697
|
el.setAttribute("points", points);
|
|
497
698
|
return () => {
|
|
498
699
|
el.remove();
|
|
499
700
|
};
|
|
500
701
|
}
|
|
702
|
+
|
|
703
|
+
// src/compass.ts
|
|
704
|
+
var compassDirectionMap = {
|
|
705
|
+
n: { x: 0.5, y: 0 },
|
|
706
|
+
ne: { x: 1, y: 0 },
|
|
707
|
+
e: { x: 1, y: 0.5 },
|
|
708
|
+
se: { x: 1, y: 1 },
|
|
709
|
+
s: { x: 0.5, y: 1 },
|
|
710
|
+
sw: { x: 0, y: 1 },
|
|
711
|
+
w: { x: 0, y: 0.5 },
|
|
712
|
+
nw: { x: 0, y: 0 }
|
|
713
|
+
};
|
|
714
|
+
var oppositeDirectionMap = {
|
|
715
|
+
n: "s",
|
|
716
|
+
ne: "sw",
|
|
717
|
+
e: "w",
|
|
718
|
+
se: "nw",
|
|
719
|
+
s: "n",
|
|
720
|
+
sw: "ne",
|
|
721
|
+
w: "e",
|
|
722
|
+
nw: "se"
|
|
723
|
+
};
|
|
724
|
+
|
|
725
|
+
// src/resize.ts
|
|
726
|
+
var { sign, abs, min: min2 } = Math;
|
|
727
|
+
function getRectExtentPoint(rect, direction) {
|
|
728
|
+
const { minX, minY, maxX, maxY, midX, midY } = rect;
|
|
729
|
+
const x = direction.includes("w") ? minX : direction.includes("e") ? maxX : midX;
|
|
730
|
+
const y = direction.includes("n") ? minY : direction.includes("s") ? maxY : midY;
|
|
731
|
+
return { x, y };
|
|
732
|
+
}
|
|
733
|
+
function getOppositeDirection(direction) {
|
|
734
|
+
return oppositeDirectionMap[direction];
|
|
735
|
+
}
|
|
736
|
+
function resizeRect(rect, offset, direction, opts) {
|
|
737
|
+
const { scalingOriginMode, lockAspectRatio } = opts;
|
|
738
|
+
const extent = getRectExtentPoint(rect, direction);
|
|
739
|
+
const oppositeDirection = getOppositeDirection(direction);
|
|
740
|
+
const oppositeExtent = getRectExtentPoint(rect, oppositeDirection);
|
|
741
|
+
if (scalingOriginMode === "center") {
|
|
742
|
+
offset = { x: offset.x * 2, y: offset.y * 2 };
|
|
743
|
+
}
|
|
744
|
+
const newExtent = {
|
|
745
|
+
x: extent.x + offset.x,
|
|
746
|
+
y: extent.y + offset.y
|
|
747
|
+
};
|
|
748
|
+
const multiplier = {
|
|
749
|
+
x: compassDirectionMap[direction].x * 2 - 1,
|
|
750
|
+
y: compassDirectionMap[direction].y * 2 - 1
|
|
751
|
+
};
|
|
752
|
+
const newSize = {
|
|
753
|
+
width: newExtent.x - oppositeExtent.x,
|
|
754
|
+
height: newExtent.y - oppositeExtent.y
|
|
755
|
+
};
|
|
756
|
+
const scaleX = multiplier.x * newSize.width / rect.width;
|
|
757
|
+
const scaleY = multiplier.y * newSize.height / rect.height;
|
|
758
|
+
const largestMagnitude = abs(scaleX) > abs(scaleY) ? scaleX : scaleY;
|
|
759
|
+
const scale = lockAspectRatio ? { x: largestMagnitude, y: largestMagnitude } : {
|
|
760
|
+
x: extent.x === oppositeExtent.x ? 1 : scaleX,
|
|
761
|
+
y: extent.y === oppositeExtent.y ? 1 : scaleY
|
|
762
|
+
};
|
|
763
|
+
if (extent.y === oppositeExtent.y) {
|
|
764
|
+
scale.y = abs(scale.y);
|
|
765
|
+
} else if (sign(scale.y) !== sign(scaleY)) {
|
|
766
|
+
scale.y *= -1;
|
|
767
|
+
}
|
|
768
|
+
if (extent.x === oppositeExtent.x) {
|
|
769
|
+
scale.x = abs(scale.x);
|
|
770
|
+
} else if (sign(scale.x) !== sign(scaleX)) {
|
|
771
|
+
scale.x *= -1;
|
|
772
|
+
}
|
|
773
|
+
switch (scalingOriginMode) {
|
|
774
|
+
case "extent":
|
|
775
|
+
return transformRect(rect, AffineTransform.scale(scale.x, scale.y, oppositeExtent), false);
|
|
776
|
+
case "center":
|
|
777
|
+
return transformRect(
|
|
778
|
+
rect,
|
|
779
|
+
AffineTransform.scale(scale.x, scale.y, {
|
|
780
|
+
x: rect.midX,
|
|
781
|
+
y: rect.midY
|
|
782
|
+
}),
|
|
783
|
+
false
|
|
784
|
+
);
|
|
785
|
+
}
|
|
786
|
+
}
|
|
787
|
+
function createRectFromPoints(initialPoint, finalPoint, normalized = true) {
|
|
788
|
+
if (normalized) {
|
|
789
|
+
return {
|
|
790
|
+
x: min2(finalPoint.x, initialPoint.x),
|
|
791
|
+
y: min2(finalPoint.y, initialPoint.y),
|
|
792
|
+
width: abs(finalPoint.x - initialPoint.x),
|
|
793
|
+
height: abs(finalPoint.y - initialPoint.y)
|
|
794
|
+
};
|
|
795
|
+
}
|
|
796
|
+
return {
|
|
797
|
+
x: initialPoint.x,
|
|
798
|
+
y: initialPoint.y,
|
|
799
|
+
width: finalPoint.x - initialPoint.x,
|
|
800
|
+
height: finalPoint.y - initialPoint.y
|
|
801
|
+
};
|
|
802
|
+
}
|
|
803
|
+
function transformRect(rect, transform, normalized = true) {
|
|
804
|
+
const p1 = transform.applyTo({ x: rect.minX, y: rect.minY });
|
|
805
|
+
const p2 = transform.applyTo({ x: rect.maxX, y: rect.maxY });
|
|
806
|
+
return createRectFromPoints(p1, p2, normalized);
|
|
807
|
+
}
|
|
501
808
|
// Annotate the CommonJS export names for ESM import in node:
|
|
502
809
|
0 && (module.exports = {
|
|
810
|
+
AffineTransform,
|
|
811
|
+
addPoints,
|
|
503
812
|
alignRect,
|
|
813
|
+
clampPoint,
|
|
814
|
+
clampSize,
|
|
504
815
|
closest,
|
|
505
816
|
closestSideToPoint,
|
|
506
817
|
closestSideToRect,
|
|
507
818
|
collisions,
|
|
819
|
+
constrainRect,
|
|
508
820
|
contains,
|
|
509
821
|
containsPoint,
|
|
510
822
|
containsRect,
|
|
823
|
+
createPoint,
|
|
511
824
|
createRect,
|
|
512
825
|
debugPolygon,
|
|
513
826
|
distance,
|
|
@@ -528,12 +841,18 @@ function debugPolygon(polygon) {
|
|
|
528
841
|
inset,
|
|
529
842
|
intersection,
|
|
530
843
|
intersects,
|
|
844
|
+
isPoint,
|
|
845
|
+
isPointEqual,
|
|
531
846
|
isPointInPolygon,
|
|
532
847
|
isRect,
|
|
848
|
+
isRectEqual,
|
|
849
|
+
isSizeEqual,
|
|
533
850
|
isSymmetric,
|
|
851
|
+
resizeRect,
|
|
534
852
|
rotate,
|
|
535
853
|
shift,
|
|
536
854
|
shrink,
|
|
855
|
+
subtractPoints,
|
|
537
856
|
toRad,
|
|
538
857
|
union
|
|
539
858
|
});
|