uplot-webgpu 0.1.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/CANVAS_PROXY.md +602 -0
- package/README.md +854 -0
- package/favicon.ico +0 -0
- package/index.html +14 -0
- package/index.js +21 -0
- package/original/paths.canvas2d/bars.js +252 -0
- package/original/paths.canvas2d/catmullRomCentrip.js +125 -0
- package/original/paths.canvas2d/linear.js +170 -0
- package/original/paths.canvas2d/monotoneCubic.js +68 -0
- package/original/paths.canvas2d/points.js +66 -0
- package/original/paths.canvas2d/spline.js +103 -0
- package/original/paths.canvas2d/stepped.js +124 -0
- package/original/paths.canvas2d/utils.js +301 -0
- package/original/uPlot.canvas2d.js +3548 -0
- package/package.json +110 -0
- package/paths/bars.js +253 -0
- package/paths/catmullRomCentrip.js +126 -0
- package/paths/linear.js +171 -0
- package/paths/monotoneCubic.js +69 -0
- package/paths/points.js +67 -0
- package/paths/spline.js +104 -0
- package/paths/stepped.js +125 -0
- package/paths/utils.js +301 -0
- package/scripts/uPlot.css +168 -0
- package/scripts/uPlot.d.ts +26 -0
- package/scripts/uPlot.js +3687 -0
- package/scripts/utils/dom.js +124 -0
- package/scripts/utils/domClasses.js +22 -0
- package/scripts/utils/feats.js +13 -0
- package/scripts/utils/fmtDate.js +398 -0
- package/scripts/utils/opts.js +844 -0
- package/scripts/utils/strings.js +22 -0
- package/scripts/utils/sync.js +27 -0
- package/scripts/utils/utils.js +692 -0
- package/scripts/webgpu/GPUPath.d.ts +46 -0
- package/scripts/webgpu/GPUPath.js +633 -0
- package/scripts/webgpu/GPUPath.ts +634 -0
- package/scripts/webgpu/WebGPURenderer.d.ts +176 -0
- package/scripts/webgpu/WebGPURenderer.js +4256 -0
- package/scripts/webgpu/WebGPURenderer.ts +4257 -0
- package/scripts/webgpu/browserSmokeHarness.js +105 -0
- package/scripts/webgpu/exporters.d.ts +8 -0
- package/scripts/webgpu/exporters.js +212 -0
- package/scripts/webgpu/shaders.d.ts +2 -0
- package/scripts/webgpu/shaders.js +76 -0
- package/scripts/webgpu/shaders.ts +77 -0
- package/scripts/webgpu/smokeTest.d.ts +2 -0
- package/scripts/webgpu/smokeTest.js +144 -0
- package/scripts/webgpu/webgpu-ambient.d.ts +41 -0
- package/tinybuild.config.js +109 -0
|
@@ -0,0 +1,634 @@
|
|
|
1
|
+
// @ts-nocheck
|
|
2
|
+
const TAU = Math.PI * 2;
|
|
3
|
+
const EPS = 1e-9;
|
|
4
|
+
|
|
5
|
+
function cloneCommand(cmd) {
|
|
6
|
+
return cmd.slice();
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
function isFiniteNumber(v) {
|
|
10
|
+
return typeof v == 'number' && Number.isFinite(v);
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
function allFinite(vals) {
|
|
14
|
+
for (let v of vals) {
|
|
15
|
+
if (!isFiniteNumber(v))
|
|
16
|
+
return false;
|
|
17
|
+
}
|
|
18
|
+
return true;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
function dist(a, b, c, d) {
|
|
22
|
+
let dx = c - a;
|
|
23
|
+
let dy = d - b;
|
|
24
|
+
return Math.sqrt(dx * dx + dy * dy);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
function cubicAt(a, b, c, d, t) {
|
|
28
|
+
let mt = 1 - t;
|
|
29
|
+
return mt * mt * mt * a + 3 * mt * mt * t * b + 3 * mt * t * t * c + t * t * t * d;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
function normalizeAngle(a) {
|
|
33
|
+
while (a < 0)
|
|
34
|
+
a += TAU;
|
|
35
|
+
while (a >= TAU)
|
|
36
|
+
a -= TAU;
|
|
37
|
+
return a;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
function arcSweep(start, end, ccw) {
|
|
41
|
+
let raw = end - start;
|
|
42
|
+
|
|
43
|
+
if (!Number.isFinite(raw))
|
|
44
|
+
return 0;
|
|
45
|
+
|
|
46
|
+
if (!ccw && raw >= TAU)
|
|
47
|
+
return TAU;
|
|
48
|
+
if (ccw && raw <= -TAU)
|
|
49
|
+
return -TAU;
|
|
50
|
+
|
|
51
|
+
start = normalizeAngle(start);
|
|
52
|
+
end = normalizeAngle(end);
|
|
53
|
+
|
|
54
|
+
let sweep = end - start;
|
|
55
|
+
|
|
56
|
+
if (!ccw && sweep < 0)
|
|
57
|
+
sweep += TAU;
|
|
58
|
+
else if (ccw && sweep > 0)
|
|
59
|
+
sweep -= TAU;
|
|
60
|
+
|
|
61
|
+
return sweep;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
function matrixFrom(transform) {
|
|
66
|
+
if (transform == null)
|
|
67
|
+
return null;
|
|
68
|
+
|
|
69
|
+
if (Array.isArray(transform))
|
|
70
|
+
return transform;
|
|
71
|
+
|
|
72
|
+
if (typeof DOMMatrix != 'undefined' && transform instanceof DOMMatrix)
|
|
73
|
+
return [transform.a, transform.b, transform.c, transform.d, transform.e, transform.f];
|
|
74
|
+
|
|
75
|
+
if (typeof transform == 'object')
|
|
76
|
+
return [transform.a, transform.b, transform.c, transform.d, transform.e, transform.f];
|
|
77
|
+
|
|
78
|
+
return null;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
function applyMatrix(m, x, y) {
|
|
82
|
+
return [m[0] * x + m[2] * y + m[4], m[1] * x + m[3] * y + m[5]];
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
function ellipsePoint(cx, cy, rx, ry, rotation, angle) {
|
|
86
|
+
let ca = Math.cos(angle);
|
|
87
|
+
let sa = Math.sin(angle);
|
|
88
|
+
let cr = Math.cos(rotation);
|
|
89
|
+
let sr = Math.sin(rotation);
|
|
90
|
+
let x = rx * ca;
|
|
91
|
+
let y = ry * sa;
|
|
92
|
+
return [cx + x * cr - y * sr, cy + x * sr + y * cr];
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
function normalizeRadii(radii) {
|
|
96
|
+
if (radii == null)
|
|
97
|
+
return [0, 0, 0, 0];
|
|
98
|
+
|
|
99
|
+
if (typeof radii == 'number')
|
|
100
|
+
return [radii, radii, radii, radii];
|
|
101
|
+
|
|
102
|
+
let arr = Array.isArray(radii) ? radii : [radii];
|
|
103
|
+
let vals = arr.map(v => typeof v == 'number' ? v : Math.max(v.x || 0, v.y || 0));
|
|
104
|
+
|
|
105
|
+
if (vals.length == 1)
|
|
106
|
+
return [vals[0], vals[0], vals[0], vals[0]];
|
|
107
|
+
if (vals.length == 2)
|
|
108
|
+
return [vals[0], vals[1], vals[0], vals[1]];
|
|
109
|
+
if (vals.length == 3)
|
|
110
|
+
return [vals[0], vals[1], vals[2], vals[1]];
|
|
111
|
+
|
|
112
|
+
return [vals[0], vals[1], vals[2], vals[3]];
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
function pushPoint(points, x, y) {
|
|
116
|
+
let p = points[points.length - 1];
|
|
117
|
+
|
|
118
|
+
if (p == null || Math.abs(p[0] - x) > EPS || Math.abs(p[1] - y) > EPS)
|
|
119
|
+
points.push([x, y]);
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
export class GPUPath {
|
|
123
|
+
constructor(copyFrom) {
|
|
124
|
+
this.cmds = [];
|
|
125
|
+
this._x = 0;
|
|
126
|
+
this._y = 0;
|
|
127
|
+
this._sx = 0;
|
|
128
|
+
this._sy = 0;
|
|
129
|
+
this._hasPoint = false;
|
|
130
|
+
|
|
131
|
+
if (copyFrom != null) {
|
|
132
|
+
if (copyFrom instanceof GPUPath)
|
|
133
|
+
this.cmds = copyFrom.cmds.map(cloneCommand);
|
|
134
|
+
else if (copyFrom.cmds)
|
|
135
|
+
this.cmds = copyFrom.cmds.map(cloneCommand);
|
|
136
|
+
this.syncCurrentFromCommands();
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
clear() {
|
|
141
|
+
this.cmds.length = 0;
|
|
142
|
+
this._x = 0;
|
|
143
|
+
this._y = 0;
|
|
144
|
+
this._sx = 0;
|
|
145
|
+
this._sy = 0;
|
|
146
|
+
this._hasPoint = false;
|
|
147
|
+
return this;
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
syncCurrentFromCommands() {
|
|
151
|
+
this._x = 0;
|
|
152
|
+
this._y = 0;
|
|
153
|
+
this._sx = 0;
|
|
154
|
+
this._sy = 0;
|
|
155
|
+
this._hasPoint = false;
|
|
156
|
+
|
|
157
|
+
for (let cmd of this.cmds) {
|
|
158
|
+
let type = cmd[0];
|
|
159
|
+
|
|
160
|
+
if (type == 0) {
|
|
161
|
+
this._x = this._sx = cmd[1];
|
|
162
|
+
this._y = this._sy = cmd[2];
|
|
163
|
+
this._hasPoint = true;
|
|
164
|
+
}
|
|
165
|
+
else if (type == 1) {
|
|
166
|
+
this._x = cmd[1];
|
|
167
|
+
this._y = cmd[2];
|
|
168
|
+
if (!this._hasPoint) {
|
|
169
|
+
this._sx = this._x;
|
|
170
|
+
this._sy = this._y;
|
|
171
|
+
this._hasPoint = true;
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
else if (type == 2) {
|
|
175
|
+
this._x = cmd[5];
|
|
176
|
+
this._y = cmd[6];
|
|
177
|
+
if (!this._hasPoint) {
|
|
178
|
+
this._sx = this._x;
|
|
179
|
+
this._sy = this._y;
|
|
180
|
+
this._hasPoint = true;
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
else if (type == 3) {
|
|
184
|
+
let end = cmd[5];
|
|
185
|
+
this._x = cmd[1] + Math.cos(end) * cmd[3];
|
|
186
|
+
this._y = cmd[2] + Math.sin(end) * cmd[3];
|
|
187
|
+
if (!this._hasPoint) {
|
|
188
|
+
let start = cmd[4];
|
|
189
|
+
this._sx = cmd[1] + Math.cos(start) * cmd[3];
|
|
190
|
+
this._sy = cmd[2] + Math.sin(start) * cmd[3];
|
|
191
|
+
this._hasPoint = true;
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
else if (type == 4) {
|
|
195
|
+
this._x = this._sx = cmd[1];
|
|
196
|
+
this._y = this._sy = cmd[2];
|
|
197
|
+
this._hasPoint = true;
|
|
198
|
+
}
|
|
199
|
+
else if (type == 5) {
|
|
200
|
+
if (this._hasPoint) {
|
|
201
|
+
this._x = this._sx;
|
|
202
|
+
this._y = this._sy;
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
else if (type == 6) {
|
|
206
|
+
let ep = ellipsePoint(cmd[1], cmd[2], cmd[3], cmd[4], cmd[5], cmd[7]);
|
|
207
|
+
this._x = ep[0];
|
|
208
|
+
this._y = ep[1];
|
|
209
|
+
if (!this._hasPoint) {
|
|
210
|
+
let sp = ellipsePoint(cmd[1], cmd[2], cmd[3], cmd[4], cmd[5], cmd[6]);
|
|
211
|
+
this._sx = sp[0];
|
|
212
|
+
this._sy = sp[1];
|
|
213
|
+
this._hasPoint = true;
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
moveTo(x, y) {
|
|
220
|
+
if (!allFinite([x, y]))
|
|
221
|
+
return;
|
|
222
|
+
this.cmds.push([0, x, y]);
|
|
223
|
+
this._x = this._sx = x;
|
|
224
|
+
this._y = this._sy = y;
|
|
225
|
+
this._hasPoint = true;
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
lineTo(x, y) {
|
|
229
|
+
if (!allFinite([x, y]))
|
|
230
|
+
return;
|
|
231
|
+
if (!this._hasPoint)
|
|
232
|
+
this.moveTo(x, y);
|
|
233
|
+
else {
|
|
234
|
+
this.cmds.push([1, x, y]);
|
|
235
|
+
this._x = x;
|
|
236
|
+
this._y = y;
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
bezierCurveTo(cp1x, cp1y, cp2x, cp2y, x, y) {
|
|
241
|
+
if (!allFinite([cp1x, cp1y, cp2x, cp2y, x, y]))
|
|
242
|
+
return;
|
|
243
|
+
if (!this._hasPoint)
|
|
244
|
+
this.moveTo(x, y);
|
|
245
|
+
else {
|
|
246
|
+
this.cmds.push([2, cp1x, cp1y, cp2x, cp2y, x, y]);
|
|
247
|
+
this._x = x;
|
|
248
|
+
this._y = y;
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
quadraticCurveTo(cpx, cpy, x, y) {
|
|
253
|
+
if (!allFinite([cpx, cpy, x, y]))
|
|
254
|
+
return;
|
|
255
|
+
if (!this._hasPoint)
|
|
256
|
+
this.moveTo(x, y);
|
|
257
|
+
else {
|
|
258
|
+
let cp1x = this._x + (2 / 3) * (cpx - this._x);
|
|
259
|
+
let cp1y = this._y + (2 / 3) * (cpy - this._y);
|
|
260
|
+
let cp2x = x + (2 / 3) * (cpx - x);
|
|
261
|
+
let cp2y = y + (2 / 3) * (cpy - y);
|
|
262
|
+
this.bezierCurveTo(cp1x, cp1y, cp2x, cp2y, x, y);
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
arc(x, y, r, startAngle, endAngle, counterclockwise = false) {
|
|
267
|
+
if (!allFinite([x, y, r, startAngle, endAngle]) || r <= 0)
|
|
268
|
+
return;
|
|
269
|
+
|
|
270
|
+
this.cmds.push([3, x, y, r, startAngle, endAngle, counterclockwise ? 1 : 0]);
|
|
271
|
+
|
|
272
|
+
let ex = x + Math.cos(endAngle) * r;
|
|
273
|
+
let ey = y + Math.sin(endAngle) * r;
|
|
274
|
+
|
|
275
|
+
if (!this._hasPoint) {
|
|
276
|
+
this._sx = x + Math.cos(startAngle) * r;
|
|
277
|
+
this._sy = y + Math.sin(startAngle) * r;
|
|
278
|
+
this._hasPoint = true;
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
this._x = ex;
|
|
282
|
+
this._y = ey;
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
ellipse(x, y, radiusX, radiusY, rotation, startAngle, endAngle, counterclockwise = false) {
|
|
286
|
+
if (!allFinite([x, y, radiusX, radiusY, rotation, startAngle, endAngle]) || radiusX <= 0 || radiusY <= 0)
|
|
287
|
+
return;
|
|
288
|
+
|
|
289
|
+
this.cmds.push([6, x, y, radiusX, radiusY, rotation, startAngle, endAngle, counterclockwise ? 1 : 0]);
|
|
290
|
+
|
|
291
|
+
let start = ellipsePoint(x, y, radiusX, radiusY, rotation, startAngle);
|
|
292
|
+
let end = ellipsePoint(x, y, radiusX, radiusY, rotation, endAngle);
|
|
293
|
+
|
|
294
|
+
if (!this._hasPoint) {
|
|
295
|
+
this._sx = start[0];
|
|
296
|
+
this._sy = start[1];
|
|
297
|
+
this._hasPoint = true;
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
this._x = end[0];
|
|
301
|
+
this._y = end[1];
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
roundRect(x, y, w, h, radii = 0) {
|
|
305
|
+
if (!allFinite([x, y, w, h]))
|
|
306
|
+
return;
|
|
307
|
+
let [tl, tr, br, bl] = normalizeRadii(radii);
|
|
308
|
+
let maxR = Math.min(Math.abs(w), Math.abs(h)) / 2;
|
|
309
|
+
tl = Math.min(Math.max(0, tl), maxR);
|
|
310
|
+
tr = Math.min(Math.max(0, tr), maxR);
|
|
311
|
+
br = Math.min(Math.max(0, br), maxR);
|
|
312
|
+
bl = Math.min(Math.max(0, bl), maxR);
|
|
313
|
+
|
|
314
|
+
let x0 = x;
|
|
315
|
+
let y0 = y;
|
|
316
|
+
let x1 = x + w;
|
|
317
|
+
let y1 = y + h;
|
|
318
|
+
|
|
319
|
+
if (w < 0)
|
|
320
|
+
[x0, x1] = [x1, x0];
|
|
321
|
+
if (h < 0)
|
|
322
|
+
[y0, y1] = [y1, y0];
|
|
323
|
+
|
|
324
|
+
this.moveTo(x0 + tl, y0);
|
|
325
|
+
this.lineTo(x1 - tr, y0);
|
|
326
|
+
tr > 0 ? this.arcTo(x1, y0, x1, y0 + tr, tr) : this.lineTo(x1, y0);
|
|
327
|
+
this.lineTo(x1, y1 - br);
|
|
328
|
+
br > 0 ? this.arcTo(x1, y1, x1 - br, y1, br) : this.lineTo(x1, y1);
|
|
329
|
+
this.lineTo(x0 + bl, y1);
|
|
330
|
+
bl > 0 ? this.arcTo(x0, y1, x0, y1 - bl, bl) : this.lineTo(x0, y1);
|
|
331
|
+
this.lineTo(x0, y0 + tl);
|
|
332
|
+
tl > 0 ? this.arcTo(x0, y0, x0 + tl, y0, tl) : this.lineTo(x0, y0);
|
|
333
|
+
this.closePath();
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
arcTo(x1, y1, x2, y2, r) {
|
|
337
|
+
if (!allFinite([x1, y1, x2, y2, r]))
|
|
338
|
+
return;
|
|
339
|
+
if (!this._hasPoint || r <= 0) {
|
|
340
|
+
this.lineTo(x1, y1);
|
|
341
|
+
return;
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
let x0 = this._x;
|
|
345
|
+
let y0 = this._y;
|
|
346
|
+
let v0x = x0 - x1;
|
|
347
|
+
let v0y = y0 - y1;
|
|
348
|
+
let v1x = x2 - x1;
|
|
349
|
+
let v1y = y2 - y1;
|
|
350
|
+
let l0 = Math.hypot(v0x, v0y);
|
|
351
|
+
let l1 = Math.hypot(v1x, v1y);
|
|
352
|
+
|
|
353
|
+
if (l0 < EPS || l1 < EPS) {
|
|
354
|
+
this.lineTo(x1, y1);
|
|
355
|
+
return;
|
|
356
|
+
}
|
|
357
|
+
|
|
358
|
+
v0x /= l0;
|
|
359
|
+
v0y /= l0;
|
|
360
|
+
v1x /= l1;
|
|
361
|
+
v1y /= l1;
|
|
362
|
+
|
|
363
|
+
let dot = Math.max(-1, Math.min(1, v0x * v1x + v0y * v1y));
|
|
364
|
+
let angle = Math.acos(dot);
|
|
365
|
+
|
|
366
|
+
if (angle < EPS || Math.abs(Math.PI - angle) < EPS) {
|
|
367
|
+
this.lineTo(x1, y1);
|
|
368
|
+
return;
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
let tangent = Math.min(l0, l1, r / Math.tan(angle / 2));
|
|
372
|
+
let sx = x1 + v0x * tangent;
|
|
373
|
+
let sy = y1 + v0y * tangent;
|
|
374
|
+
let ex = x1 + v1x * tangent;
|
|
375
|
+
let ey = y1 + v1y * tangent;
|
|
376
|
+
let bisX = v0x + v1x;
|
|
377
|
+
let bisY = v0y + v1y;
|
|
378
|
+
let bisLen = Math.hypot(bisX, bisY);
|
|
379
|
+
|
|
380
|
+
if (bisLen < EPS) {
|
|
381
|
+
this.lineTo(x1, y1);
|
|
382
|
+
return;
|
|
383
|
+
}
|
|
384
|
+
|
|
385
|
+
let centerDist = r / Math.sin(angle / 2);
|
|
386
|
+
let cx = x1 + bisX / bisLen * centerDist;
|
|
387
|
+
let cy = y1 + bisY / bisLen * centerDist;
|
|
388
|
+
let start = Math.atan2(sy - cy, sx - cx);
|
|
389
|
+
let end = Math.atan2(ey - cy, ex - cx);
|
|
390
|
+
let cross = (sx - cx) * (ey - cy) - (sy - cy) * (ex - cx);
|
|
391
|
+
|
|
392
|
+
this.lineTo(sx, sy);
|
|
393
|
+
this.arc(cx, cy, r, start, end, cross < 0);
|
|
394
|
+
}
|
|
395
|
+
|
|
396
|
+
rect(x, y, w, h) {
|
|
397
|
+
if (!allFinite([x, y, w, h]))
|
|
398
|
+
return;
|
|
399
|
+
this.cmds.push([4, x, y, w, h]);
|
|
400
|
+
this._x = x;
|
|
401
|
+
this._y = y;
|
|
402
|
+
this._sx = x;
|
|
403
|
+
this._sy = y;
|
|
404
|
+
this._hasPoint = true;
|
|
405
|
+
}
|
|
406
|
+
|
|
407
|
+
closePath() {
|
|
408
|
+
if (!this._hasPoint)
|
|
409
|
+
return;
|
|
410
|
+
this.cmds.push([5]);
|
|
411
|
+
this._x = this._sx;
|
|
412
|
+
this._y = this._sy;
|
|
413
|
+
}
|
|
414
|
+
|
|
415
|
+
isEmpty() {
|
|
416
|
+
return this.cmds.length == 0;
|
|
417
|
+
}
|
|
418
|
+
|
|
419
|
+
clone() {
|
|
420
|
+
return new GPUPath(this);
|
|
421
|
+
}
|
|
422
|
+
|
|
423
|
+
addPath(path, transform = null) {
|
|
424
|
+
if (path == null)
|
|
425
|
+
return;
|
|
426
|
+
|
|
427
|
+
let matrix = matrixFrom(transform);
|
|
428
|
+
|
|
429
|
+
if (matrix == null && path.cmds != null) {
|
|
430
|
+
for (let cmd of path.cmds)
|
|
431
|
+
this.cmds.push(cloneCommand(cmd));
|
|
432
|
+
this.syncCurrentFromCommands();
|
|
433
|
+
return;
|
|
434
|
+
}
|
|
435
|
+
|
|
436
|
+
let subpaths = path.toSubpaths?.() || [];
|
|
437
|
+
|
|
438
|
+
for (let sub of subpaths) {
|
|
439
|
+
for (let i = 0; i < sub.length; i++) {
|
|
440
|
+
let p = matrix == null ? sub[i] : applyMatrix(matrix, sub[i][0], sub[i][1]);
|
|
441
|
+
if (i == 0)
|
|
442
|
+
this.moveTo(p[0], p[1]);
|
|
443
|
+
else
|
|
444
|
+
this.lineTo(p[0], p[1]);
|
|
445
|
+
}
|
|
446
|
+
if (sub.closed)
|
|
447
|
+
this.closePath();
|
|
448
|
+
}
|
|
449
|
+
}
|
|
450
|
+
|
|
451
|
+
getRects() {
|
|
452
|
+
let rects = [];
|
|
453
|
+
|
|
454
|
+
for (let cmd of this.cmds) {
|
|
455
|
+
if (cmd[0] != 4)
|
|
456
|
+
return null;
|
|
457
|
+
|
|
458
|
+
let x = cmd[1];
|
|
459
|
+
let y = cmd[2];
|
|
460
|
+
let w = cmd[3];
|
|
461
|
+
let h = cmd[4];
|
|
462
|
+
let winding = Math.sign(w * h) || 0;
|
|
463
|
+
|
|
464
|
+
if (w < 0) {
|
|
465
|
+
x += w;
|
|
466
|
+
w = -w;
|
|
467
|
+
}
|
|
468
|
+
|
|
469
|
+
if (h < 0) {
|
|
470
|
+
y += h;
|
|
471
|
+
h = -h;
|
|
472
|
+
}
|
|
473
|
+
|
|
474
|
+
rects.push({x, y, w, h, winding});
|
|
475
|
+
}
|
|
476
|
+
|
|
477
|
+
return rects;
|
|
478
|
+
}
|
|
479
|
+
|
|
480
|
+
toSubpaths(curveSteps = 12) {
|
|
481
|
+
let paths = [];
|
|
482
|
+
let pts = [];
|
|
483
|
+
let x = 0;
|
|
484
|
+
let y = 0;
|
|
485
|
+
let sx = 0;
|
|
486
|
+
let sy = 0;
|
|
487
|
+
let hasPoint = false;
|
|
488
|
+
|
|
489
|
+
function flush(closed = false) {
|
|
490
|
+
if (pts.length > 0) {
|
|
491
|
+
pts.closed = closed;
|
|
492
|
+
paths.push(pts);
|
|
493
|
+
pts = [];
|
|
494
|
+
}
|
|
495
|
+
}
|
|
496
|
+
|
|
497
|
+
for (let cmd of this.cmds) {
|
|
498
|
+
let type = cmd[0];
|
|
499
|
+
|
|
500
|
+
if (type == 0) {
|
|
501
|
+
flush();
|
|
502
|
+
x = sx = cmd[1];
|
|
503
|
+
y = sy = cmd[2];
|
|
504
|
+
hasPoint = true;
|
|
505
|
+
pushPoint(pts, x, y);
|
|
506
|
+
}
|
|
507
|
+
else if (type == 1) {
|
|
508
|
+
x = cmd[1];
|
|
509
|
+
y = cmd[2];
|
|
510
|
+
if (!hasPoint) {
|
|
511
|
+
sx = x;
|
|
512
|
+
sy = y;
|
|
513
|
+
hasPoint = true;
|
|
514
|
+
}
|
|
515
|
+
pushPoint(pts, x, y);
|
|
516
|
+
}
|
|
517
|
+
else if (type == 2) {
|
|
518
|
+
if (!hasPoint) {
|
|
519
|
+
x = sx = cmd[5];
|
|
520
|
+
y = sy = cmd[6];
|
|
521
|
+
hasPoint = true;
|
|
522
|
+
pushPoint(pts, x, y);
|
|
523
|
+
continue;
|
|
524
|
+
}
|
|
525
|
+
|
|
526
|
+
let x0 = x;
|
|
527
|
+
let y0 = y;
|
|
528
|
+
let cp1x = cmd[1];
|
|
529
|
+
let cp1y = cmd[2];
|
|
530
|
+
let cp2x = cmd[3];
|
|
531
|
+
let cp2y = cmd[4];
|
|
532
|
+
let x1 = cmd[5];
|
|
533
|
+
let y1 = cmd[6];
|
|
534
|
+
let approxLen = dist(x0, y0, cp1x, cp1y) + dist(cp1x, cp1y, cp2x, cp2y) + dist(cp2x, cp2y, x1, y1);
|
|
535
|
+
let steps = Math.max(4, Math.min(48, Math.ceil(approxLen / 12), curveSteps));
|
|
536
|
+
|
|
537
|
+
for (let i = 1; i <= steps; i++) {
|
|
538
|
+
let t = i / steps;
|
|
539
|
+
pushPoint(pts,
|
|
540
|
+
cubicAt(x0, cp1x, cp2x, x1, t),
|
|
541
|
+
cubicAt(y0, cp1y, cp2y, y1, t),
|
|
542
|
+
);
|
|
543
|
+
}
|
|
544
|
+
|
|
545
|
+
x = x1;
|
|
546
|
+
y = y1;
|
|
547
|
+
}
|
|
548
|
+
else if (type == 3) {
|
|
549
|
+
let cx = cmd[1];
|
|
550
|
+
let cy = cmd[2];
|
|
551
|
+
let r = cmd[3];
|
|
552
|
+
let start = cmd[4];
|
|
553
|
+
let end = cmd[5];
|
|
554
|
+
let ccw = cmd[6] == 1;
|
|
555
|
+
let sweep = arcSweep(start, end, ccw);
|
|
556
|
+
let steps = Math.max(8, Math.ceil(Math.abs(sweep) * r / 6));
|
|
557
|
+
let sx0 = cx + Math.cos(start) * r;
|
|
558
|
+
let sy0 = cy + Math.sin(start) * r;
|
|
559
|
+
|
|
560
|
+
if (!hasPoint) {
|
|
561
|
+
hasPoint = true;
|
|
562
|
+
sx = sx0;
|
|
563
|
+
sy = sy0;
|
|
564
|
+
}
|
|
565
|
+
|
|
566
|
+
pushPoint(pts, sx0, sy0);
|
|
567
|
+
|
|
568
|
+
for (let i = 1; i <= steps; i++) {
|
|
569
|
+
let a = start + sweep * i / steps;
|
|
570
|
+
pushPoint(pts, cx + Math.cos(a) * r, cy + Math.sin(a) * r);
|
|
571
|
+
}
|
|
572
|
+
|
|
573
|
+
x = cx + Math.cos(end) * r;
|
|
574
|
+
y = cy + Math.sin(end) * r;
|
|
575
|
+
}
|
|
576
|
+
else if (type == 6) {
|
|
577
|
+
let cx = cmd[1];
|
|
578
|
+
let cy = cmd[2];
|
|
579
|
+
let rx = cmd[3];
|
|
580
|
+
let ry = cmd[4];
|
|
581
|
+
let rotation = cmd[5];
|
|
582
|
+
let start = cmd[6];
|
|
583
|
+
let end = cmd[7];
|
|
584
|
+
let ccw = cmd[8] == 1;
|
|
585
|
+
let sweep = arcSweep(start, end, ccw);
|
|
586
|
+
let steps = Math.max(12, Math.ceil(Math.abs(sweep) * Math.max(rx, ry) / 6));
|
|
587
|
+
let sp = ellipsePoint(cx, cy, rx, ry, rotation, start);
|
|
588
|
+
|
|
589
|
+
if (!hasPoint) {
|
|
590
|
+
hasPoint = true;
|
|
591
|
+
sx = sp[0];
|
|
592
|
+
sy = sp[1];
|
|
593
|
+
}
|
|
594
|
+
|
|
595
|
+
pushPoint(pts, sp[0], sp[1]);
|
|
596
|
+
|
|
597
|
+
for (let i = 1; i <= steps; i++) {
|
|
598
|
+
let p = ellipsePoint(cx, cy, rx, ry, rotation, start + sweep * i / steps);
|
|
599
|
+
pushPoint(pts, p[0], p[1]);
|
|
600
|
+
}
|
|
601
|
+
|
|
602
|
+
let ep = ellipsePoint(cx, cy, rx, ry, rotation, end);
|
|
603
|
+
x = ep[0];
|
|
604
|
+
y = ep[1];
|
|
605
|
+
}
|
|
606
|
+
else if (type == 4) {
|
|
607
|
+
flush();
|
|
608
|
+
let rx = cmd[1];
|
|
609
|
+
let ry = cmd[2];
|
|
610
|
+
let rw = cmd[3];
|
|
611
|
+
let rh = cmd[4];
|
|
612
|
+
pts = [[rx, ry], [rx + rw, ry], [rx + rw, ry + rh], [rx, ry + rh], [rx, ry]];
|
|
613
|
+
flush(true);
|
|
614
|
+
x = sx = rx;
|
|
615
|
+
y = sy = ry;
|
|
616
|
+
hasPoint = true;
|
|
617
|
+
}
|
|
618
|
+
else if (type == 5) {
|
|
619
|
+
if (!hasPoint)
|
|
620
|
+
continue;
|
|
621
|
+
pushPoint(pts, sx, sy);
|
|
622
|
+
x = sx;
|
|
623
|
+
y = sy;
|
|
624
|
+
flush(true);
|
|
625
|
+
hasPoint = true;
|
|
626
|
+
}
|
|
627
|
+
}
|
|
628
|
+
|
|
629
|
+
flush();
|
|
630
|
+
return paths;
|
|
631
|
+
}
|
|
632
|
+
}
|
|
633
|
+
|
|
634
|
+
export default GPUPath;
|