pts 0.12.8 → 1.0.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/README.md +92 -80
- package/dist/index.d.mts +6208 -1254
- package/dist/index.d.mts.map +1 -0
- package/dist/index.d.ts +6208 -1254
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +9314 -10680
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +9266 -10611
- package/dist/index.mjs.map +1 -0
- package/dist/pts.js +9446 -10777
- package/dist/pts.js.map +1 -0
- package/dist/pts.min.js +3 -5
- package/dist/pts.min.js.map +1 -0
- package/package.json +91 -31
- package/src/Canvas.ts +1642 -0
- package/src/Color.ts +1109 -0
- package/src/Create.ts +1547 -0
- package/src/Dom.ts +940 -0
- package/src/Form.ts +312 -0
- package/src/Image.ts +722 -0
- package/src/LinearAlgebra.ts +530 -0
- package/src/Num.ts +1091 -0
- package/src/Op.ts +2127 -0
- package/src/Physics.ts +1233 -0
- package/src/Play.ts +861 -0
- package/src/Pt.ts +1303 -0
- package/src/Space.ts +898 -0
- package/src/Svg.ts +1573 -0
- package/src/Types.ts +301 -0
- package/src/Typography.ts +228 -0
- package/src/UI.ts +757 -0
- package/src/Util.ts +454 -0
- package/src/_module.ts +18 -0
- package/src/_script.ts +94 -0
- package/src/_triangulate.ts +884 -0
- package/src/uheprng.ts +153 -0
package/src/Form.ts
ADDED
|
@@ -0,0 +1,312 @@
|
|
|
1
|
+
/*! Pts.js is licensed under Apache License 2.0. Copyright © 2017-current William Ngan and contributors. (https://github.com/williamngan/pts) */
|
|
2
|
+
|
|
3
|
+
import { type Pt } from "./Pt";
|
|
4
|
+
import { Util } from "./Util";
|
|
5
|
+
import { type PtLike, type GroupLike } from "./Types";
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Form is an abstract class that represents a form that's used in a Space for expressions. Learn more about Space and Form in [this guide](../guide/Space-0500.html).
|
|
9
|
+
*/
|
|
10
|
+
export abstract class Form {
|
|
11
|
+
protected _ready: boolean = false;
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* get whether the Form has received the Space's rendering context.
|
|
15
|
+
*/
|
|
16
|
+
get ready(): boolean {
|
|
17
|
+
return this._ready;
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* VisualForm is an abstract class that represents a form that can be used to express Pts visually.
|
|
23
|
+
* For example, [`CanvasForm`](#link) is an implementation of VisualForm that draws on [`CanvasSpace`](#link) which represents a html canvas. Learn more about Space and Form in [this guide](../guide/Space-0500.html).
|
|
24
|
+
*/
|
|
25
|
+
export abstract class VisualForm extends Form {
|
|
26
|
+
protected _filled = true;
|
|
27
|
+
protected _stroked = true;
|
|
28
|
+
protected _font: Font = new Font(14, "sans-serif");
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Check whether this form currently has fill style.
|
|
32
|
+
*/
|
|
33
|
+
get filled(): boolean {
|
|
34
|
+
return this._filled;
|
|
35
|
+
}
|
|
36
|
+
set filled(b: boolean) {
|
|
37
|
+
this._filled = b;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Check whether this form currently has stroke style.
|
|
42
|
+
*/
|
|
43
|
+
get stroked(): boolean {
|
|
44
|
+
return this._stroked;
|
|
45
|
+
}
|
|
46
|
+
set stroked(b: boolean) {
|
|
47
|
+
this._stroked = b;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* Get the current font in use in this form.
|
|
52
|
+
*/
|
|
53
|
+
get currentFont(): Font {
|
|
54
|
+
return this._font;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
protected _multiple(
|
|
58
|
+
groups: GroupLike[],
|
|
59
|
+
shape: string,
|
|
60
|
+
...rest: unknown[]
|
|
61
|
+
): this {
|
|
62
|
+
if (!groups) return this;
|
|
63
|
+
for (let i = 0, len = groups.length; i < len; i++) {
|
|
64
|
+
// dynamic dispatch to the named shape method
|
|
65
|
+
(this as any)[shape](groups[i], ...rest);
|
|
66
|
+
}
|
|
67
|
+
return this;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* Abstract reset style
|
|
72
|
+
*/
|
|
73
|
+
abstract reset(): this;
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* Set alpha (not implemented here -- to be implemented in subclasses).
|
|
77
|
+
* @param a alpha value between 0 and 1
|
|
78
|
+
*/
|
|
79
|
+
alpha(a: number): this {
|
|
80
|
+
return this;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* Set fill color (not implemented here -- to be implemented in subclasses).
|
|
85
|
+
* @param c fill color as string or `false` to specify transparent.
|
|
86
|
+
*/
|
|
87
|
+
fill(c: string | boolean): this {
|
|
88
|
+
return this;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* Set current fill style and remove stroke style. (not implemented here -- to be implemented in subclasses).
|
|
93
|
+
* @param c fill color as string or `false` to specify transparent.
|
|
94
|
+
*/
|
|
95
|
+
fillOnly(c: string | boolean): this {
|
|
96
|
+
this.stroke(false);
|
|
97
|
+
return this.fill(c);
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* Set stroke style (not implemented here -- to be implemented in subclasses).
|
|
102
|
+
* @param c stroke color as string or `false` to specify transparent.
|
|
103
|
+
* @param width Optional value (can be floating point) to set line width
|
|
104
|
+
* @param linejoin Optional string to set line joint style. Can be "miter", "bevel", or "round".
|
|
105
|
+
* @param linecap Optional string to set line cap style. Can be "butt", "round", or "square".
|
|
106
|
+
*/
|
|
107
|
+
stroke(
|
|
108
|
+
c: string | boolean,
|
|
109
|
+
width?: number,
|
|
110
|
+
linejoin?: string,
|
|
111
|
+
linecap?: string,
|
|
112
|
+
): this {
|
|
113
|
+
return this;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* Set stroke style and remove fill style. (not implemented here -- to be implemented in subclasses).
|
|
118
|
+
* @param c stroke color as string or `false` to specify transparent.
|
|
119
|
+
* @param width Optional value (can be floating point) to set line width
|
|
120
|
+
* @param linejoin Optional string to set line joint style. Can be "miter", "bevel", or "round".
|
|
121
|
+
* @param linecap Optional string to set line cap style. Can be "butt", "round", or "square".
|
|
122
|
+
*/
|
|
123
|
+
strokeOnly(
|
|
124
|
+
c: string | boolean,
|
|
125
|
+
width?: number,
|
|
126
|
+
linejoin?: string,
|
|
127
|
+
linecap?: string,
|
|
128
|
+
): this {
|
|
129
|
+
this.fill(false);
|
|
130
|
+
return this.stroke(c, width, linejoin, linecap);
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
/**
|
|
134
|
+
* Draw a point (not implemented here -- to be implemented in subclasses).
|
|
135
|
+
* @param p a Pt object
|
|
136
|
+
* @param radius radius of the point. Default is 5.
|
|
137
|
+
* @param shape The shape of the point. Defaults to "square", but it can be "circle" or a custom shape function in your own implementation.
|
|
138
|
+
* @example `form.point( p )`, `form.point( p, 10, "circle" )`
|
|
139
|
+
*/
|
|
140
|
+
abstract point(p: PtLike, radius: number, shape: string): this;
|
|
141
|
+
|
|
142
|
+
/**
|
|
143
|
+
* Draw multiple points at once.
|
|
144
|
+
* @param pts an array of Pt or an array of number arrays
|
|
145
|
+
* @param radius radius of the point. Default is 5.
|
|
146
|
+
* @param shape The shape of the point. Defaults to "square", but it can be "circle" or a custom shape function in your own implementation.
|
|
147
|
+
*/
|
|
148
|
+
points(pts: GroupLike | number[][], radius: number, shape: string): this {
|
|
149
|
+
if (!pts) return this;
|
|
150
|
+
for (let i = 0, len = pts.length; i < len; i++) {
|
|
151
|
+
this.point(pts[i], radius, shape);
|
|
152
|
+
}
|
|
153
|
+
return this;
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
/**
|
|
157
|
+
* Draw a circle (not implemented here -- to be implemented in subclasses).
|
|
158
|
+
* @param pts usually a Group of 2 Pts, but it can also take an array of two numeric arrays [ [position], [size] ]
|
|
159
|
+
* @see [`Circle.fromCenter`](#link)
|
|
160
|
+
*/
|
|
161
|
+
abstract circle(pts: GroupLike | number[][]): this;
|
|
162
|
+
|
|
163
|
+
/**
|
|
164
|
+
* Draw multiple circles at once.
|
|
165
|
+
* @param groups an array of Groups that defines multiple circles
|
|
166
|
+
*/
|
|
167
|
+
circles(groups: GroupLike[]): this {
|
|
168
|
+
return this._multiple(groups, "circle");
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
/**
|
|
172
|
+
* Draw multiple squares at once.
|
|
173
|
+
* @param groups an array of Groups that defines multiple circles
|
|
174
|
+
*/
|
|
175
|
+
squares(groups: GroupLike[]): this {
|
|
176
|
+
return this._multiple(groups, "square");
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
/**
|
|
180
|
+
* Draw an arc (not implemented here -- to be implemented in subclasses).
|
|
181
|
+
* @param pt center position
|
|
182
|
+
* @param radius radius of the arc circle
|
|
183
|
+
* @param startAngle start angle of the arc
|
|
184
|
+
* @param endAngle end angle of the arc
|
|
185
|
+
* @param cc an optional boolean value to specify if it should be drawn clockwise (`false`) or counter-clockwise (`true`). Default is clockwise.
|
|
186
|
+
*/
|
|
187
|
+
abstract arc(
|
|
188
|
+
pt: PtLike,
|
|
189
|
+
radius: number,
|
|
190
|
+
startAngle: number,
|
|
191
|
+
endAngle: number,
|
|
192
|
+
cc?: boolean,
|
|
193
|
+
): this;
|
|
194
|
+
|
|
195
|
+
/**
|
|
196
|
+
* Draw a line or polyline (not implemented here -- to be implemented in subclasses).
|
|
197
|
+
* @param pts a Group of multiple Pts, or an array of multiple numeric arrays
|
|
198
|
+
*/
|
|
199
|
+
abstract line(pts: GroupLike | number[][]): this;
|
|
200
|
+
|
|
201
|
+
/**
|
|
202
|
+
* Draw multiple lines at once.
|
|
203
|
+
* @param groups An array of Groups of Pts
|
|
204
|
+
*/
|
|
205
|
+
lines(groups: GroupLike[]): this {
|
|
206
|
+
return this._multiple(groups, "line");
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
/**
|
|
210
|
+
* Draw a polygon (not implemented here -- to be implemented in subclasses).
|
|
211
|
+
* @param pts a Group of multiple Pts, or an array of multiple numeric arrays
|
|
212
|
+
*/
|
|
213
|
+
abstract polygon(pts: GroupLike | number[][]): this;
|
|
214
|
+
|
|
215
|
+
/**
|
|
216
|
+
* Draw multiple polygons at once.
|
|
217
|
+
* @param groups An array of Groups of Pts
|
|
218
|
+
*/
|
|
219
|
+
polygons(groups: GroupLike[]): this {
|
|
220
|
+
return this._multiple(groups, "polygon");
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
/**
|
|
224
|
+
* Draw a rectangle (not implemented here -- to be implemented in subclasses).
|
|
225
|
+
* @param pts usually a Group of 2 Pts specifying the top-left and bottom-right positions. Alternatively it can be an array of numeric arrays.
|
|
226
|
+
*/
|
|
227
|
+
abstract rect(pts: number[][] | Pt[]): this;
|
|
228
|
+
|
|
229
|
+
/**
|
|
230
|
+
* Draw multiple rectangles at once.
|
|
231
|
+
* @param groups An array of Groups of Pts
|
|
232
|
+
*/
|
|
233
|
+
rects(groups: GroupLike[]): this {
|
|
234
|
+
return this._multiple(groups, "rect");
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
/**
|
|
238
|
+
* Draw text (not implemented here -- to be implemented in subclasses).
|
|
239
|
+
* @param pt a Pt or numeric array to specify the anchor point
|
|
240
|
+
* @param txt text
|
|
241
|
+
* @param maxWidth specify a maximum width per line
|
|
242
|
+
*/
|
|
243
|
+
abstract text(pt: PtLike, txt: string, maxWidth?: number): this;
|
|
244
|
+
|
|
245
|
+
/**
|
|
246
|
+
* Set font style (not implemented here -- to be implemented in subclasses).
|
|
247
|
+
* @param sizeOrFont either a number to specify font-size, or a `Font` object to specify all font properties
|
|
248
|
+
* @param weight Optional font-weight string such as "bold"
|
|
249
|
+
* @param style Optional font-style string such as "italic"
|
|
250
|
+
* @param lineHeight Optional line-height number suchas 1.5
|
|
251
|
+
* @param family Optional font-family such as "Helvetica, sans-serif"
|
|
252
|
+
* @see `Font` class
|
|
253
|
+
* @example `form.font( myFont )`, `form.font(14, "bold")`
|
|
254
|
+
*/
|
|
255
|
+
abstract font(
|
|
256
|
+
sizeOrFont: number | Font,
|
|
257
|
+
weight?: string,
|
|
258
|
+
style?: string,
|
|
259
|
+
lineHeight?: number,
|
|
260
|
+
family?: string,
|
|
261
|
+
): this;
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
/**
|
|
265
|
+
* Font class lets you create a specific font style with properties for its size and style. A font instance can be passed as parameter to set a form's font. For example, see [`CanvasForm.font`](#link).
|
|
266
|
+
*/
|
|
267
|
+
export class Font {
|
|
268
|
+
public size: number;
|
|
269
|
+
public lineHeight: number;
|
|
270
|
+
public face: string;
|
|
271
|
+
public style: string;
|
|
272
|
+
public weight: string;
|
|
273
|
+
|
|
274
|
+
/**
|
|
275
|
+
* Create a font style.
|
|
276
|
+
* @param size font size. Defaults is 12px.
|
|
277
|
+
* @param face Optional font-family, use css-like string such as "Helvetica" or "Helvetica, sans-serif". Default is "sans-serif".
|
|
278
|
+
* @param weight Optional font weight such as "bold". Default is "" (none).
|
|
279
|
+
* @param style Optional font style such as "italic". Default is "" (none).
|
|
280
|
+
* @param lineHeight Optional line height. Default is 1.5.
|
|
281
|
+
* @example `new Font(12, "Frutiger, sans-serif", "bold", "underline", 1.5)`
|
|
282
|
+
*/
|
|
283
|
+
constructor(
|
|
284
|
+
size: number = 12,
|
|
285
|
+
face: string = "sans-serif",
|
|
286
|
+
weight: string = "",
|
|
287
|
+
style: string = "",
|
|
288
|
+
lineHeight: number = 1.5,
|
|
289
|
+
) {
|
|
290
|
+
this.size = size;
|
|
291
|
+
this.face = face;
|
|
292
|
+
this.style = style;
|
|
293
|
+
this.weight = weight;
|
|
294
|
+
this.lineHeight = lineHeight;
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
/**
|
|
298
|
+
* Get a string representing the font style, in css-like string such as "italic bold 12px/1.5 sans-serif".
|
|
299
|
+
*/
|
|
300
|
+
get value(): string {
|
|
301
|
+
const prefix = [this.style, this.weight].filter(Boolean).join(" ");
|
|
302
|
+
const base = `${this.size}px/${this.lineHeight} ${this.face}`;
|
|
303
|
+
return prefix ? `${prefix} ${base}` : base;
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
/**
|
|
307
|
+
* Get a string representing the font style, in css-like string such as "italic bold 12px/1.5 sans-serif".
|
|
308
|
+
*/
|
|
309
|
+
toString(): string {
|
|
310
|
+
return this.value;
|
|
311
|
+
}
|
|
312
|
+
}
|