/
index.d.ts
442 lines (433 loc) · 11.4 KB
/
index.d.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
/**
Vec2 represents 2D vector, point or size.
```js
const a = new Vec2(1, 2)
const b = new Vec2(3, 4)
a.add(b) //=> Vec2(4, 6)
a.sub(b) //=> Vec2(-2, -2)
b.length //=> 5
...
```
*/
export declare class Vec2 {
x: number;
y: number;
/**
@param x The x component of this vector.
@param y The y component of this vector.
*/
constructor(x?: number, y?: number);
/**
* Returns x.
*/
get width(): number;
/**
* Returns y.
*/
get height(): number;
/**
Checks if the vectors has same values.
*/
equals(v: Vec2): boolean;
/**
Adds v to this vector.
*/
add(v: Vec2 | number): Vec2;
/**
Subtracts v from this vector.
*/
sub(v: Vec2 | number): Vec2;
/**
Multiplies components of this vector by v.
*/
mul(v: Vec2 | number): Vec2;
/**
Divides components of this vector by v.
*/
div(v: Vec2 | number): Vec2;
/**
Inverts this vector.
*/
get neg(): Vec2;
/**
Calculates the length of this vector.
*/
get length(): number;
/**
Calculates the squared length of this vector.
*/
get squaredLength(): number;
/**
Calculates the angle of this vector from positive x-axis in [-PI, PI].
*/
get angle(): number;
/**
* Returns the dot product of this and other.
* @param other
*/
dot(other: Vec2): number;
/**
* Returns the cross product of this and other.
* @param other
*/
cross(other: Vec2): number;
/**
* Returns this * (1 - ratio) + other * ratio.
* @param other
* @param ratio
*/
mix(other: Vec2, ratio: number): Vec2;
/**
Rounds down the components of this vector.
*/
get floor(): Vec2;
/**
Rounds up the components of this vector.
*/
get ceil(): Vec2;
/**
Rounds the components of this vector to nearest integer.
*/
get round(): Vec2;
/**
* Returns absolute values of this vector.
*/
get abs(): Vec2;
/**
* Clamp the components within the min and max values.
* @param min
* @param max
*/
clamp(min: Vec2, max: Vec2): Vec2;
/**
Transforms this vector with transform.
*/
transform(transform: Transform): Vec2;
/**
Gets an array of [x, y].
*/
get members(): [number, number];
toString(): string;
/**
* Constructs Vec2 from vec2-like objects.
* @param options
*/
static from(options: number | number[] | {
x?: number;
y?: number;
}): Vec2;
/**
* Returns min value for each component.
*/
static min(...vs: Vec2[]): Vec2;
/**
* Returns max value for each component.
*/
static max(...vs: Vec2[]): Vec2;
}
export declare class Segment {
p1: Vec2;
p2: Vec2;
constructor(p1: Vec2, p2: Vec2);
equals(other: Segment): boolean;
get length(): number;
get angle(): number;
transform(transform: Transform): Segment;
mix(other: Segment, ratio: number): Segment;
toString(): string;
toSVGLineProps(): {
x1: number;
y1: number;
x2: number;
y2: number;
};
}
/**
Rect represents rectangle in 2D space.
```js
// 100*200 rectangle at (0, 0)
const r1 = new Rect(new Vec2(0), new Vec2(100, 200))
// 200*300 rectangle at (50, 50)
const r2 = new Rect(new Vec2(50, 50), new Vec2(200, 300))
...
```
*/
export declare class Rect {
topLeft: Vec2;
size: Vec2;
/**
Creates a rectangle. It returns empty rectangle when no arguments given.
@param topLeft The top-left position (in top-left origin coordinates) of this rectangle.
@param size The size of this rectangle.
*/
constructor(topLeft?: Vec2, size?: Vec2);
/**
Checks if the rectangles has same values.
*/
equals(other: Rect): boolean;
get tl(): Vec2;
/**
* The bottom-right position (in top-left origin coordinates) of this rectangle.
*/
get bottomRight(): Vec2;
get br(): Vec2;
/**
The top-right position (in top-left origin coordinates) of this rectangle.
*/
get topRight(): Vec2;
get tr(): Vec2;
/**
The bottom-lect position (in top-left origin coordinates) of this rectangle.
*/
get bottomLeft(): Vec2;
get bl(): Vec2;
/**
The left coordinate (in top-left origin coordinates) of this rectangle.
*/
get left(): number;
get l(): number;
/**
The top coordinate (in top-left origin coordinates) of this rectangle.
*/
get top(): number;
get t(): number;
/**
The right coordinate (in top-left origin coordinates) of this rectangle.
*/
get right(): number;
get r(): number;
/**
The bottom coordinate (in top-left origin coordinates) of this rectangle.
*/
get bottom(): number;
get b(): number;
/**
The width of this rectangle.
*/
get width(): number;
get w(): number;
/**
The width of this rectangle.
*/
get height(): number;
get h(): number;
/**
* The center of this rectangle.
*/
get center(): Vec2;
get topLine(): Segment;
get bottomLine(): Segment;
get leftLine(): Segment;
get rightLine(): Segment;
get startLines(): {
x: Segment;
y: Segment;
};
get endLines(): {
x: Segment;
y: Segment;
};
/**
Calculates the smallest integer rectangle which includes this rectangle.
*/
toIntBounding(): Rect;
/**
* Translates this rectangle by offset.
* @param offset
*/
translate(offset: Vec2): Rect;
inflate(offset: number): Rect;
inset(offsets: EdgeOffsets): Rect;
insetsTo(other: Rect): EdgeOffsets;
/**
* Returns if this rectangle include pos.
* @param pos
*/
includes(pos: Vec2): boolean;
/**
* Returns the area of this rectangle.
*/
get area(): number;
/**
Transforms each corners by transform and returns the bounding rectangle.
*/
transform(transform: Transform): Rect;
/**
* Returns the smallest rectangle which contains both this and other.
* @param other
*/
union(other: Rect): Rect;
/**
* Returns the largest rectangle contained in both this and other.
* @param other
*/
intersection(other: Rect): Rect | undefined;
toString(): string;
toDOMRect(): DOMRect;
toSVGRectProps(): {
x: number;
y: number;
width: number;
height: number;
};
/**
* Returns array of `[topLeft, topRight, bottomRight, bottomLeft]`.
*/
get vertices(): [Vec2, Vec2, Vec2, Vec2];
/**
Calculates the bounding rectangle of given rectangles.
*/
static union(...rects: Rect[]): Rect | undefined;
/**
Calculates the rectangle that represents the shared region of given rectangles.
*/
static intersection(...rects: Rect[]): Rect | undefined;
/**
* Creates Rect from rect-like objects.
* @param options
*/
static from(options: {
x: number;
y: number;
width: number;
height: number;
} | {
left: number;
top: number;
width: number;
height: number;
} | {
left: number;
top: number;
right: number;
bottom: number;
} | {
topLeft: Vec2;
bottomRight: Vec2;
} | {
topLeft: Vec2;
size: Vec2;
}): Rect;
static boundingRect(points: Vec2[]): Rect;
}
/**
* EdgeOffsets represents edge offsets which are applied to rectangles.
*/
export declare class EdgeOffsets {
topLeft: Vec2;
bottomRight: Vec2;
constructor(topLeft: Vec2, bottomRight: Vec2);
get left(): number;
get top(): number;
get right(): number;
get bottom(): number;
get neg(): EdgeOffsets;
equals(other: EdgeOffsets): boolean;
toString(): string;
static from(options: number | {
left: number;
top: number;
right: number;
bottom: number;
} | {
topLeft: Vec2;
bottomRight: Vec2;
}): EdgeOffsets;
}
/**
Transform represents 2D affine and perspective transform with 3x3 matrix.
```js
// translate by (100, 200)
const translate = Transform.translate(new Vec2(100, 200))
// 2x scale
const scale = Transform.scale(new Vec2(2))
// rotate 45 degrees
const rotate = Transform.rotate(Math.PI / 4)
// translate then scale then rotate
const transform = translate.merge(scale).merge(rotate)
```
*/
export declare class Transform {
m00: number;
m01: number;
m02: number;
m10: number;
m11: number;
m12: number;
m20: number;
m21: number;
m22: number;
/**
Creates a transform. It returns no-op transform when no arguments given.
```
|x'| | m00 m10 m20 | |x|
|y'| = | m01 m11 m21 | |y|
|z'| | m02 m12 m22 | |z|
```
@param m00 Column 0 and row 0 component of this transform.
@param m01 Column 0 and row 1 component of this transform.
@param m02 Column 0 and row 2 component of this transform.
@param m10 Column 1 and row 0 component of this transform.
@param m11 Column 1 and row 1 component of this transform.
@param m12 Column 1 and row 2 component of this transform.
@param m20 Column 2 and row 0 component of this transform.
@param m21 Column 2 and row 1 component of this transform.
@param m22 Column 2 and row 2 component of this transform.
*/
constructor(m00?: number, m01?: number, m02?: number, m10?: number, m11?: number, m12?: number, m20?: number, m21?: number, m22?: number);
/**
Checks if the transforms has same values.
*/
equals(other: Transform): boolean;
/**
Merges 2 transforms. The returned transform represents "transform by this transform, then other transform".
*/
merge(other: Transform): Transform;
/**
Inverts the transform. Returns undefined if this transform is not invertible.
*/
invert(): Transform | undefined;
get isAffine(): boolean;
get isTranslation(): boolean;
get withoutTranslation(): Transform;
/**
Returns the members (m00, m01, 002, ... m22) in an array.
*/
get members(): number[];
toString(): string;
/**
* Returns the CSS matrix() string.
*/
toCSSMatrixString(): string;
/**
* Returns the CSS matrix3d() string.
* This supports perspective transform.
*/
toCSSMatrix3DString(): string;
scale(scale: Vec2): Transform;
translate(offset: Vec2): Transform;
rotate(angle: number): Transform;
/**
Returns the transform that represents scaling.
*/
static scale(scale: Vec2): Transform;
/**
Returns the transform that represents rotating by angle (in radians).
*/
static rotate(angle: number): Transform;
/**
Returns the transform that represents translating.
*/
static translate(translation: Vec2): Transform;
/**
* Returns the perspective transform that transforms the unit square ([(0, 0), (1, 0), (1, 1), (0, 1)]) to the specified quadrangle.
* Reference: Projective Mappings for Image Warping
*/
static unitToQuad(quad: [Vec2, Vec2, Vec2, Vec2]): Transform | undefined;
static quadToQuad(from: [Vec2, Vec2, Vec2, Vec2], to: [Vec2, Vec2, Vec2, Vec2]): Transform | undefined;
static rectToRect(from: Rect, to: Rect): Transform;
/**
Merges all transforms.
*/
static merge(...transforms: Transform[]): Transform;
}