pixi-silk 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.
@@ -0,0 +1,1282 @@
1
+ import { parseColor } from "./color.js";
2
+ import { Cap, Extend, Flag, INSTANCE_ATTRIBUTES, Prim, TAU } from "./constants.js";
3
+ import { GradientAtlas } from "./atlas.js";
4
+ import { catmullRom, flattenCubic, flattenQuadratic, monotoneX, toFlat } from "./curves.js";
5
+ import { Gradient } from "./gradient.js";
6
+ import { hitTest } from "./hit.js";
7
+ import { createSilkShader } from "./shader.js";
8
+ import { Bounds, Buffer, BufferUsage, Color, Geometry, Mesh } from "pixi.js";
9
+ //#region src/SilkGraphics.ts
10
+ const CLOSED_KINDS = /* @__PURE__ */ new Set([
11
+ "rect",
12
+ "ellipse",
13
+ "sector",
14
+ "heart",
15
+ "star",
16
+ "triangle"
17
+ ]);
18
+ const FILL_KEYS = [
19
+ "color",
20
+ "alpha",
21
+ "gradient",
22
+ "blur",
23
+ "width",
24
+ "alignment",
25
+ "cap",
26
+ "dash",
27
+ "dashOffset",
28
+ "dashFit",
29
+ "join"
30
+ ];
31
+ const CAPS = {
32
+ butt: Cap.Butt,
33
+ round: Cap.Round,
34
+ square: Cap.Square
35
+ };
36
+ const EXTEND = {
37
+ pad: Extend.Pad,
38
+ repeat: Extend.Repeat,
39
+ reflect: Extend.Reflect
40
+ };
41
+ const HEART_W = 1.2071;
42
+ const HEART_H = 1.1036;
43
+ let warnedPathFill = false;
44
+ let warnedAreaRotation = false;
45
+ function scalePaint(p, k) {
46
+ return k === 1 ? p : {
47
+ ...p,
48
+ blur: p.blur * k
49
+ };
50
+ }
51
+ function scaleStroke(st, k) {
52
+ if (k === 1) return st;
53
+ return {
54
+ ...st,
55
+ width: st.width * k,
56
+ widths: st.widths ? st.widths.map((w) => w * k) : null,
57
+ blur: st.blur * k,
58
+ dash: st.dash ? [st.dash[0] * k, st.dash[1] * k] : null,
59
+ dashOffset: st.dashOffset * k
60
+ };
61
+ }
62
+ function isStyleObject(v) {
63
+ if (typeof v !== "object" || v === null || Array.isArray(v) || v instanceof Color || ArrayBuffer.isView(v)) return false;
64
+ return FILL_KEYS.some((k) => k in v);
65
+ }
66
+ function resolvePaint(input) {
67
+ if (input instanceof Gradient) return {
68
+ rgba: [
69
+ 1,
70
+ 1,
71
+ 1,
72
+ 1
73
+ ],
74
+ gradient: input,
75
+ blur: 0
76
+ };
77
+ if (input === void 0 || input === null) return {
78
+ rgba: [
79
+ 1,
80
+ 1,
81
+ 1,
82
+ 1
83
+ ],
84
+ gradient: null,
85
+ blur: 0
86
+ };
87
+ if (isStyleObject(input)) return {
88
+ ...input,
89
+ rgba: parseColor(input.color ?? 16777215, input.alpha ?? 1),
90
+ gradient: input.gradient ?? null,
91
+ blur: Math.max(0, input.blur ?? 0)
92
+ };
93
+ return {
94
+ rgba: parseColor(input),
95
+ gradient: null,
96
+ blur: 0
97
+ };
98
+ }
99
+ function resolveStroke(input) {
100
+ const p = resolvePaint(input);
101
+ let align = 0;
102
+ if (typeof p.alignment === "number") align = 1 - 2 * p.alignment;
103
+ else if (p.alignment === "inside") align = -1;
104
+ else if (p.alignment === "outside") align = 1;
105
+ let dash = null;
106
+ if (p.dash !== void 0) {
107
+ dash = typeof p.dash === "number" ? [p.dash, p.dash] : [p.dash[0], p.dash[1]];
108
+ if (dash[0] + dash[1] <= 0) dash = null;
109
+ }
110
+ const widths = Array.isArray(p.width) && p.width.length ? p.width.map((w) => Math.max(0, w)) : null;
111
+ return {
112
+ rgba: p.rgba,
113
+ gradient: p.gradient,
114
+ blur: p.blur,
115
+ width: widths ? Math.max(...widths) : Math.max(0, p.width ?? 1),
116
+ widths,
117
+ align: Math.max(-1, Math.min(1, align)),
118
+ cap: CAPS[p.cap ?? "butt"] ?? Cap.Butt,
119
+ dash,
120
+ dashOffset: p.dashOffset ?? 0,
121
+ dashFit: p.dashFit ?? true
122
+ };
123
+ }
124
+ /** Exponent that makes a superellipse corner of radius r*(1+s) hug the same diagonal as a circle of radius r. */
125
+ function squircleExponent(s) {
126
+ if (s <= 0) return 2;
127
+ return -1 / Math.log2(1 - .29289 / (1 + s));
128
+ }
129
+ function arcExtent(cx, cy, r, start, sweep, out) {
130
+ const include = (a) => {
131
+ const x = cx + r * Math.cos(a);
132
+ const y = cy + r * Math.sin(a);
133
+ if (x < out[0]) out[0] = x;
134
+ if (y < out[1]) out[1] = y;
135
+ if (x > out[2]) out[2] = x;
136
+ if (y > out[3]) out[3] = y;
137
+ };
138
+ if (Math.abs(sweep) >= TAU - 1e-6) {
139
+ include(0);
140
+ include(Math.PI / 2);
141
+ include(Math.PI);
142
+ include(-Math.PI / 2);
143
+ return;
144
+ }
145
+ const lo = Math.min(start, start + sweep);
146
+ const hi = Math.max(start, start + sweep);
147
+ include(lo);
148
+ include(hi);
149
+ for (let k = Math.ceil(lo / (Math.PI / 2)); k * (Math.PI / 2) <= hi; k++) include(k * (Math.PI / 2));
150
+ }
151
+ function pointsBox(pts, out) {
152
+ for (let i = 0; i < pts.length; i += 2) {
153
+ const x = pts[i];
154
+ const y = pts[i + 1];
155
+ if (x < out[0]) out[0] = x;
156
+ if (y < out[1]) out[1] = y;
157
+ if (x > out[2]) out[2] = x;
158
+ if (y > out[3]) out[3] = y;
159
+ }
160
+ }
161
+ /** Removes consecutive duplicates and a closing duplicate of the first point. `keep` maps to input indices. */
162
+ function cleanPoints(pts, closed) {
163
+ const out = [];
164
+ const keep = [];
165
+ for (let i = 0; i < pts.length; i += 2) {
166
+ const n = out.length;
167
+ if (n >= 2 && Math.abs(out[n - 2] - pts[i]) < 1e-9 && Math.abs(out[n - 1] - pts[i + 1]) < 1e-9) continue;
168
+ out.push(pts[i], pts[i + 1]);
169
+ keep.push(i / 2);
170
+ }
171
+ if (closed && out.length >= 4) {
172
+ const n = out.length;
173
+ if (Math.abs(out[0] - out[n - 2]) < 1e-9 && Math.abs(out[1] - out[n - 1]) < 1e-9) {
174
+ out.length = n - 2;
175
+ keep.length -= 1;
176
+ }
177
+ }
178
+ return {
179
+ pts: out,
180
+ keep
181
+ };
182
+ }
183
+ /**
184
+ * Geometry behind every {@link SilkGraphics}: one unit quad plus an interleaved instance buffer
185
+ * (40 floats per primitive). You rarely need it directly.
186
+ */
187
+ var SilkGeometry = class extends Geometry {
188
+ /** Per-primitive data, grown on demand and reused across `clear()`. */
189
+ instanceBuffer;
190
+ /** Bounds of everything drawn, in local units (includes stroke widths and blur). */
191
+ localBounds = new Bounds();
192
+ constructor() {
193
+ const instanceBuffer = new Buffer({
194
+ data: /* @__PURE__ */ new Float32Array(1280),
195
+ usage: BufferUsage.VERTEX | BufferUsage.COPY_DST,
196
+ label: "silk-instances",
197
+ shrinkToFit: false
198
+ });
199
+ const attributes = { aCorner: {
200
+ buffer: new Float32Array([
201
+ 0,
202
+ 0,
203
+ 1,
204
+ 0,
205
+ 1,
206
+ 1,
207
+ 0,
208
+ 1
209
+ ]),
210
+ format: "float32x2",
211
+ stride: 8,
212
+ offset: 0
213
+ } };
214
+ INSTANCE_ATTRIBUTES.forEach((name, i) => {
215
+ attributes[name] = {
216
+ buffer: instanceBuffer,
217
+ format: "float32x4",
218
+ stride: 160,
219
+ offset: i * 16,
220
+ instance: true
221
+ };
222
+ });
223
+ super({
224
+ attributes,
225
+ indexBuffer: new Uint16Array([
226
+ 0,
227
+ 1,
228
+ 2,
229
+ 0,
230
+ 2,
231
+ 3
232
+ ]),
233
+ instanceCount: 0
234
+ });
235
+ this.instanceBuffer = instanceBuffer;
236
+ }
237
+ get bounds() {
238
+ return this.localBounds;
239
+ }
240
+ };
241
+ /**
242
+ * A PixiJS display object with a `Graphics`-like API. Every primitive is a signed distance field
243
+ * evaluated per pixel, so edges are smooth at any scale, rotation and device pixel ratio. Each
244
+ * object is one instanced draw call.
245
+ *
246
+ * Shapes go on the active path and are painted by {@link SilkGraphics.fill | fill()} or
247
+ * {@link SilkGraphics.stroke | stroke()}. `stroke()` right after `fill()` paints the same shapes.
248
+ * Redrawing every frame is cheap, because {@link SilkGraphics.clear | clear()} keeps the buffers.
249
+ *
250
+ * @example
251
+ * ```ts
252
+ * const g = new SilkGraphics()
253
+ * .roundRect(0, 0, 160, 80, 20, 0.6).fill(0x1c1c1e)
254
+ * .arc(80, 40, 28, -Math.PI / 2, Math.PI).stroke({ width: 8, cap: 'round', color: 0x30d158 });
255
+ * app.stage.addChild(g);
256
+ * ```
257
+ */
258
+ var SilkGraphics = class SilkGraphics extends Mesh {
259
+ /** Settings for new objects. Change them before you create any SilkGraphics. */
260
+ static defaults = {
261
+ dither: 1,
262
+ aaWidth: 1,
263
+ minStrokePx: 1
264
+ };
265
+ _f;
266
+ _count = 0;
267
+ /** Shapes of the active path. fill() and stroke() consume them without clearing, like Pixi's GraphicsContext. */
268
+ _pending = [];
269
+ _consumed = false;
270
+ _last = null;
271
+ /** Gradient ramps this graphics holds a reference to (key -> atlas row). */
272
+ _rows = /* @__PURE__ */ new Map();
273
+ _cursor = null;
274
+ _m = {
275
+ a: 1,
276
+ b: 0,
277
+ c: 0,
278
+ d: 1,
279
+ tx: 0,
280
+ ty: 0
281
+ };
282
+ _mStack = [];
283
+ _box = [
284
+ 0,
285
+ 0,
286
+ 0,
287
+ 0
288
+ ];
289
+ _silk;
290
+ _uniforms;
291
+ constructor(options = {}) {
292
+ const geometry = new SilkGeometry();
293
+ const settings = {
294
+ ...SilkGraphics.defaults,
295
+ ...options
296
+ };
297
+ const shader = createSilkShader(settings);
298
+ super({
299
+ geometry,
300
+ shader,
301
+ label: options.label ?? "SilkGraphics"
302
+ });
303
+ this._f = geometry.instanceBuffer.data;
304
+ this._uniforms = shader.resources.silkUniforms;
305
+ this._silk = this._uniforms.uniforms.uSilk;
306
+ }
307
+ /** Number of primitives (= instances in the single draw call). */
308
+ get primitiveCount() {
309
+ return this._count;
310
+ }
311
+ /** Dither amplitude in 8-bit steps for gradients and blurs (0 disables). Default 1. */
312
+ get dither() {
313
+ return this._silk[0];
314
+ }
315
+ /** Sets the dither amplitude of this object. */
316
+ set dither(v) {
317
+ this._setting(0, v);
318
+ }
319
+ /** Width of the anti-aliasing ramp in device pixels. 1 is a crisp box filter, larger is softer. */
320
+ get aaWidth() {
321
+ return this._silk[1];
322
+ }
323
+ /** Sets the anti-aliasing width of this object. */
324
+ set aaWidth(v) {
325
+ this._setting(1, v);
326
+ }
327
+ /** Strokes thinner than this many device pixels keep this width and fade instead (hairlines). */
328
+ get minStrokePx() {
329
+ return this._silk[2];
330
+ }
331
+ /** Sets the hairline threshold of this object. */
332
+ set minStrokePx(v) {
333
+ this._setting(2, v);
334
+ }
335
+ _setting(i, v) {
336
+ this._silk[i] = v;
337
+ this._uniforms.update();
338
+ }
339
+ /** Pushes the current transform (Pixi GraphicsContext API). */
340
+ save() {
341
+ this._mStack.push({ ...this._m });
342
+ return this;
343
+ }
344
+ /** Pops the transform pushed by the matching {@link SilkGraphics.save | save()}. */
345
+ restore() {
346
+ const m = this._mStack.pop();
347
+ if (m) this._m = m;
348
+ return this;
349
+ }
350
+ /** Resets the current transform to identity. */
351
+ resetTransform() {
352
+ this._m = {
353
+ a: 1,
354
+ b: 0,
355
+ c: 0,
356
+ d: 1,
357
+ tx: 0,
358
+ ty: 0
359
+ };
360
+ return this;
361
+ }
362
+ /** Replaces the current transform with the matrix `[a c tx; b d ty]`. */
363
+ setTransform(a, b, c, d, tx, ty) {
364
+ this._m = {
365
+ a,
366
+ b,
367
+ c,
368
+ d,
369
+ tx,
370
+ ty
371
+ };
372
+ return this;
373
+ }
374
+ /** Moves the coordinate system. Shapes drawn next are offset by (x, y). */
375
+ translateTransform(x, y = x) {
376
+ const m = this._m;
377
+ m.tx += m.a * x + m.c * y;
378
+ m.ty += m.b * x + m.d * y;
379
+ return this;
380
+ }
381
+ /** Scales the coordinate system. Stroke widths, dashes and blur scale with its uniform part. */
382
+ scaleTransform(x, y = x) {
383
+ const m = this._m;
384
+ m.a *= x;
385
+ m.b *= x;
386
+ m.c *= y;
387
+ m.d *= y;
388
+ return this;
389
+ }
390
+ /** Rotates the coordinate system by `angle` radians (clockwise on screen). Rotated primitives stay exact. */
391
+ rotateTransform(angle) {
392
+ const m = this._m;
393
+ const cos = Math.cos(angle);
394
+ const sin = Math.sin(angle);
395
+ const { a, b, c, d } = m;
396
+ m.a = a * cos + c * sin;
397
+ m.b = b * cos + d * sin;
398
+ m.c = c * cos - a * sin;
399
+ m.d = d * cos - b * sin;
400
+ return this;
401
+ }
402
+ _x(x, y) {
403
+ return this._m.a * x + this._m.c * y + this._m.tx;
404
+ }
405
+ _y(x, y) {
406
+ return this._m.b * x + this._m.d * y + this._m.ty;
407
+ }
408
+ _pts(pts) {
409
+ const m = this._m;
410
+ if (m.a === 1 && m.b === 0 && m.c === 0 && m.d === 1 && m.tx === 0 && m.ty === 0) return pts;
411
+ const out = new Array(pts.length);
412
+ for (let i = 0; i < pts.length; i += 2) {
413
+ out[i] = this._x(pts[i], pts[i + 1]);
414
+ out[i + 1] = this._y(pts[i], pts[i + 1]);
415
+ }
416
+ return out;
417
+ }
418
+ /** Uniform scale of the current transform. */
419
+ get _k() {
420
+ return Math.sqrt(Math.abs(this._m.a * this._m.d - this._m.b * this._m.c));
421
+ }
422
+ get _rot() {
423
+ return Math.atan2(this._m.b, this._m.a);
424
+ }
425
+ /** Maps an angle through the transform's linear part. */
426
+ _angle(angle) {
427
+ const cos = Math.cos(angle);
428
+ const sin = Math.sin(angle);
429
+ return Math.atan2(this._m.b * cos + this._m.d * sin, this._m.a * cos + this._m.c * sin);
430
+ }
431
+ get _mirrored() {
432
+ return this._m.a * this._m.d - this._m.b * this._m.c < 0;
433
+ }
434
+ /** Axis-aligned rectangle (rotated by the current transform, if any). */
435
+ rect(x, y, w, h) {
436
+ return this.roundRect(x, y, w, h, 0);
437
+ }
438
+ /**
439
+ * Rectangle with rounded corners. `smoothing` above 0 turns the corners into squircles.
440
+ *
441
+ * @param radius - one radius or `[topLeft, topRight, bottomRight, bottomLeft]`
442
+ * @param smoothing - 0..1 continuous-curvature ("squircle") corners, iOS uses about 0.6
443
+ */
444
+ roundRect(x, y, w, h, radius = 0, smoothing = 0) {
445
+ if (w < 0) {
446
+ x += w;
447
+ w = -w;
448
+ }
449
+ if (h < 0) {
450
+ y += h;
451
+ h = -h;
452
+ }
453
+ const k = this._k;
454
+ const list = typeof radius === "number" ? [radius] : radius;
455
+ const r = [
456
+ list[0] ?? 0,
457
+ list[1] ?? list[0] ?? 0,
458
+ list[2] ?? list[0] ?? 0,
459
+ list[3] ?? list[1] ?? list[0] ?? 0
460
+ ].map((v) => Math.max(0, v) * k);
461
+ const cx = x + w / 2;
462
+ const cy = y + h / 2;
463
+ return this._begin({
464
+ kind: "rect",
465
+ cx: this._x(cx, cy),
466
+ cy: this._y(cx, cy),
467
+ hw: w / 2 * Math.hypot(this._m.a, this._m.b),
468
+ hh: h / 2 * Math.hypot(this._m.c, this._m.d),
469
+ r,
470
+ smoothing: Math.max(0, smoothing),
471
+ rot: this._rot,
472
+ k
473
+ });
474
+ }
475
+ /** Rounded rect with fully round ends. */
476
+ pill(x, y, w, h) {
477
+ return this.roundRect(x, y, w, h, Math.min(Math.abs(w), Math.abs(h)) / 2);
478
+ }
479
+ /** Circle centred on (x, y). */
480
+ circle(x, y, radius) {
481
+ return this.ellipse(x, y, radius, radius);
482
+ }
483
+ /** Ellipse centred on (x, y) with an exact distance field (no polygon approximation). */
484
+ ellipse(x, y, rx, ry) {
485
+ return this._begin({
486
+ kind: "ellipse",
487
+ cx: this._x(x, y),
488
+ cy: this._y(x, y),
489
+ rx: Math.abs(rx) * Math.hypot(this._m.a, this._m.b),
490
+ ry: Math.abs(ry) * Math.hypot(this._m.c, this._m.d),
491
+ rot: this._rot,
492
+ k: this._k
493
+ });
494
+ }
495
+ /** Open circular arc (stroke it). Angles work as in canvas. It starts its own subpath. */
496
+ arc(cx, cy, radius, startAngle, endAngle, anticlockwise = false) {
497
+ let sweep = endAngle - startAngle;
498
+ if (!anticlockwise) {
499
+ if (sweep >= TAU) sweep = TAU;
500
+ else {
501
+ sweep %= TAU;
502
+ if (sweep < 0) sweep += TAU;
503
+ }
504
+ } else if (sweep <= -TAU) sweep = -TAU;
505
+ else {
506
+ sweep %= TAU;
507
+ if (sweep > 0) sweep -= TAU;
508
+ }
509
+ return this.arcSweep(cx, cy, radius, startAngle, sweep);
510
+ }
511
+ /** Open arc given start angle and signed sweep (positive = clockwise on screen). */
512
+ arcSweep(cx, cy, radius, startAngle, sweep) {
513
+ sweep = Math.max(-TAU, Math.min(TAU, sweep));
514
+ const k = this._k;
515
+ return this._begin({
516
+ kind: "arc",
517
+ cx: this._x(cx, cy),
518
+ cy: this._y(cx, cy),
519
+ r: Math.abs(radius) * k,
520
+ start: this._angle(startAngle),
521
+ sweep: this._mirrored ? -sweep : sweep,
522
+ k
523
+ });
524
+ }
525
+ /**
526
+ * Closed annular sector: pie slices, donut segments, gauge segments.
527
+ * @param innerRadius - 0 for a pie slice
528
+ * @param cornerRadius - rounds all corners
529
+ */
530
+ sector(cx, cy, radius, startAngle, endAngle, innerRadius = 0, cornerRadius = 0) {
531
+ const sweep = Math.max(-TAU, Math.min(TAU, endAngle - startAngle));
532
+ const r0 = Math.max(0, Math.min(innerRadius, radius));
533
+ const corner = Math.max(0, Math.min(cornerRadius, (radius - r0) / 2));
534
+ const k = this._k;
535
+ return this._begin({
536
+ kind: "sector",
537
+ cx: this._x(cx, cy),
538
+ cy: this._y(cx, cy),
539
+ r0: r0 * k,
540
+ r1: radius * k,
541
+ start: this._angle(startAngle),
542
+ sweep: this._mirrored ? -sweep : sweep,
543
+ corner: corner * k,
544
+ k
545
+ });
546
+ }
547
+ /** Heart icon centred at (cx, cy), `size` wide. `rounding` (0..0.3) puffs it up. */
548
+ heart(cx, cy, size, rounding = 0) {
549
+ const k = this._k;
550
+ return this._begin({
551
+ kind: "heart",
552
+ cx: this._x(cx, cy),
553
+ cy: this._y(cx, cy),
554
+ size: size * k,
555
+ round: Math.max(0, rounding),
556
+ rot: this._rot,
557
+ k
558
+ });
559
+ }
560
+ /** Star with `points` tips. `innerRadius` defaults to a classic star. */
561
+ star(cx, cy, points, radius, innerRadius, rotation = 0, cornerRadius = 0) {
562
+ const n = Math.max(3, Math.round(points));
563
+ const an = Math.PI / n;
564
+ const ri = Math.min(innerRadius ?? radius * .5, radius * Math.cos(an));
565
+ const en = Math.atan2(Math.sin(an), Math.cos(an) - ri / radius);
566
+ const k = this._k;
567
+ return this._begin({
568
+ kind: "star",
569
+ cx: this._x(cx, cy),
570
+ cy: this._y(cx, cy),
571
+ n,
572
+ r: radius * k,
573
+ en,
574
+ rot: rotation + this._rot,
575
+ corner: Math.max(0, cornerRadius) * k,
576
+ ri: ri * k,
577
+ k
578
+ });
579
+ }
580
+ /** Regular polygon (first vertex points up). */
581
+ regularPoly(cx, cy, radius, sides, rotation = 0, cornerRadius = 0) {
582
+ const n = Math.max(3, Math.round(sides));
583
+ return this.star(cx, cy, n, radius, radius * Math.cos(Math.PI / n), rotation, cornerRadius);
584
+ }
585
+ /** Triangle through three points, with optionally rounded corners. */
586
+ triangle(x0, y0, x1, y1, x2, y2, cornerRadius = 0) {
587
+ const k = this._k;
588
+ return this._begin({
589
+ kind: "triangle",
590
+ pts: this._pts([
591
+ x0,
592
+ y0,
593
+ x1,
594
+ y1,
595
+ x2,
596
+ y2
597
+ ]),
598
+ corner: Math.max(0, cornerRadius) * k,
599
+ k
600
+ });
601
+ }
602
+ /** Straight line (stroke it). */
603
+ line(x0, y0, x1, y1) {
604
+ return this._begin({
605
+ kind: "path",
606
+ subpaths: [{
607
+ pts: this._pts([
608
+ x0,
609
+ y0,
610
+ x1,
611
+ y1
612
+ ]),
613
+ closed: false
614
+ }],
615
+ k: this._k
616
+ });
617
+ }
618
+ /** Polyline through points (stroke it), optionally smoothed. */
619
+ polyline(points, options = {}) {
620
+ let pts = toFlat(points);
621
+ const closed = options.closed ?? false;
622
+ if (options.smooth === "monotone") pts = monotoneX(pts, options.step);
623
+ else if (options.smooth === "catmull") pts = catmullRom(pts, closed, options.step);
624
+ return this._begin({
625
+ kind: "path",
626
+ subpaths: [{
627
+ pts: this._pts(pts),
628
+ closed
629
+ }],
630
+ k: this._k
631
+ });
632
+ }
633
+ /** Pixi-style polygon path. Only strokes are supported for arbitrary paths. */
634
+ poly(points, close = true) {
635
+ return this.polyline(points, { closed: close });
636
+ }
637
+ /**
638
+ * Area under a series. Fill it, then `stroke` draws the top line. Points must be sorted by x.
639
+ * @param lower - baseline y, or a second series with the same x values for bands
640
+ */
641
+ area(points, lower, options = {}) {
642
+ let top = toFlat(points);
643
+ let bottom;
644
+ if (typeof lower === "number") {
645
+ if (options.smooth === "monotone") top = monotoneX(top, options.step);
646
+ bottom = [];
647
+ for (let i = 0; i < top.length; i += 2) bottom.push(top[i], lower);
648
+ } else {
649
+ bottom = toFlat(lower);
650
+ if (options.smooth === "monotone") {
651
+ top = monotoneX(top, options.step);
652
+ bottom = monotoneX(bottom, options.step);
653
+ }
654
+ if (bottom.length !== top.length) {
655
+ console.warn("[pixi-silk] area(): upper and lower series must have the same number of points");
656
+ return this;
657
+ }
658
+ }
659
+ const m = this._m;
660
+ if ((m.b !== 0 || m.c !== 0) && !warnedAreaRotation) {
661
+ warnedAreaRotation = true;
662
+ console.warn("[pixi-silk] area() ignores rotation/skew of the path transform; rotate the SilkGraphics instead");
663
+ }
664
+ const map = (pts) => pts.map((v, i) => i % 2 === 0 ? m.a * v + m.tx : m.d * v + m.ty);
665
+ top = map(top);
666
+ bottom = map(bottom);
667
+ if (top.length >= 4 && top[0] > top[top.length - 2]) {
668
+ top = reversePairs(top);
669
+ bottom = reversePairs(bottom);
670
+ }
671
+ return this._begin({
672
+ kind: "area",
673
+ top,
674
+ bottom,
675
+ k: this._k
676
+ });
677
+ }
678
+ /** Starts a new, empty path (shapes drawn so far are kept). */
679
+ beginPath() {
680
+ this._pending = [];
681
+ this._consumed = false;
682
+ this._last = null;
683
+ this._cursor = null;
684
+ return this;
685
+ }
686
+ /** Starts a new subpath at (x, y). Paths are stroke-only. */
687
+ moveTo(x, y) {
688
+ const shape = this._pathShape();
689
+ const px = this._x(x, y);
690
+ const py = this._y(x, y);
691
+ shape.subpaths.push({
692
+ pts: [px, py],
693
+ closed: false
694
+ });
695
+ this._cursor = [px, py];
696
+ return this;
697
+ }
698
+ /** Straight segment from the current point to (x, y). */
699
+ lineTo(x, y) {
700
+ const px = this._x(x, y);
701
+ const py = this._y(x, y);
702
+ this._currentSubpath(px, py).pts.push(px, py);
703
+ this._cursor = [px, py];
704
+ return this;
705
+ }
706
+ /** Quadratic Bezier to (x, y), flattened adaptively. */
707
+ quadraticCurveTo(cpx, cpy, x, y) {
708
+ const [qx, qy, px, py] = this._pts([
709
+ cpx,
710
+ cpy,
711
+ x,
712
+ y
713
+ ]);
714
+ const sp = this._currentSubpath(qx, qy);
715
+ const n = sp.pts.length;
716
+ flattenQuadratic(sp.pts, sp.pts[n - 2], sp.pts[n - 1], qx, qy, px, py);
717
+ this._cursor = [px, py];
718
+ return this;
719
+ }
720
+ /** Cubic Bezier to (x, y), flattened adaptively. */
721
+ bezierCurveTo(cp1x, cp1y, cp2x, cp2y, x, y) {
722
+ const [ax, ay, bx, by, px, py] = this._pts([
723
+ cp1x,
724
+ cp1y,
725
+ cp2x,
726
+ cp2y,
727
+ x,
728
+ y
729
+ ]);
730
+ const sp = this._currentSubpath(ax, ay);
731
+ const n = sp.pts.length;
732
+ flattenCubic(sp.pts, sp.pts[n - 2], sp.pts[n - 1], ax, ay, bx, by, px, py);
733
+ this._cursor = [px, py];
734
+ return this;
735
+ }
736
+ /** Closes the current subpath back to its first point. */
737
+ closePath() {
738
+ const shape = this._pending[this._pending.length - 1];
739
+ if (!this._consumed && shape?.kind === "path" && shape.subpaths.length) {
740
+ const sp = shape.subpaths[shape.subpaths.length - 1];
741
+ sp.closed = true;
742
+ this._cursor = [sp.pts[0], sp.pts[1]];
743
+ }
744
+ return this;
745
+ }
746
+ _pathShape() {
747
+ const last = this._pending[this._pending.length - 1];
748
+ if (!this._consumed && last?.kind === "path") return last;
749
+ const shape = {
750
+ kind: "path",
751
+ subpaths: [],
752
+ k: this._k
753
+ };
754
+ this._begin(shape);
755
+ return shape;
756
+ }
757
+ _currentSubpath(x, y) {
758
+ const shape = this._pathShape();
759
+ let sp = shape.subpaths[shape.subpaths.length - 1];
760
+ if (!sp || sp.closed) {
761
+ const start = this._cursor ?? [x, y];
762
+ sp = {
763
+ pts: [start[0], start[1]],
764
+ closed: false
765
+ };
766
+ shape.subpaths.push(sp);
767
+ }
768
+ return sp;
769
+ }
770
+ /** Fills the shapes of the active path (Pixi semantics: `rect().circle().fill()` fills both). */
771
+ fill(style) {
772
+ const shapes = this._pending;
773
+ if (!shapes.length) return this;
774
+ const paint = resolvePaint(style);
775
+ const start = this._count;
776
+ for (const shape of shapes) if (shape.kind === "area") this._emitArea(shape, scalePaint(paint, shape.k));
777
+ else if (CLOSED_KINDS.has(shape.kind)) this._emitClosed(shape, scalePaint(paint, shape.k), null);
778
+ else if (shape.kind === "path" && !warnedPathFill) {
779
+ warnedPathFill = true;
780
+ console.warn("[pixi-silk] fill() on free paths is not supported yet; use stroke(), area() or the closed shapes");
781
+ }
782
+ this._last = {
783
+ shapes,
784
+ start,
785
+ fill: paint
786
+ };
787
+ this._consumed = true;
788
+ this._commit();
789
+ return this;
790
+ }
791
+ /** Strokes the shapes of the active path. Right after `fill()` it merges into the same instances. */
792
+ stroke(style) {
793
+ const shapes = this._pending;
794
+ if (!shapes.length) return this;
795
+ const stroke = resolveStroke(style);
796
+ const last = this._last;
797
+ this._last = null;
798
+ this._consumed = true;
799
+ if (stroke.width <= 0) return this;
800
+ if (last && last.shapes === shapes && last.start + shapes.length === this._count && shapes.every((sh) => CLOSED_KINDS.has(sh.kind))) {
801
+ this._rewind(last);
802
+ for (const shape of shapes) this._emitClosed(shape, scalePaint(last.fill, shape.k), scaleStroke(stroke, shape.k));
803
+ } else for (const shape of shapes) {
804
+ const st = scaleStroke(stroke, shape.k);
805
+ if (CLOSED_KINDS.has(shape.kind)) this._emitClosed(shape, null, st);
806
+ else if (shape.kind === "arc") this._emitArc(shape, st);
807
+ else if (shape.kind === "path") for (const sp of shape.subpaths) this._emitPath(sp.pts, sp.closed, st, null);
808
+ else if (shape.kind === "area") this._emitPath(shape.top, false, st, null);
809
+ }
810
+ this._commit();
811
+ return this;
812
+ }
813
+ /** Removes everything. Keeps the buffers for reuse, so redrawing every frame is cheap. */
814
+ clear() {
815
+ this._releaseRamps();
816
+ this._count = 0;
817
+ this._pending = [];
818
+ this._consumed = false;
819
+ this._last = null;
820
+ this._cursor = null;
821
+ this._mStack.length = 0;
822
+ this.resetTransform();
823
+ this.geometry.localBounds.clear();
824
+ this._commit();
825
+ return this;
826
+ }
827
+ /**
828
+ * Hit test against the real shapes (local coordinates): rings are hollow, lines count only
829
+ * within their stroke. Used by Pixi's event system when `eventMode` is set.
830
+ */
831
+ containsPoint(point) {
832
+ if (!this.geometry.localBounds.containsPoint(point.x, point.y)) return false;
833
+ return hitTest(this._f, this._count, point.x, point.y);
834
+ }
835
+ destroy(options) {
836
+ this._releaseRamps();
837
+ const geometry = this.geometry;
838
+ const shader = this.shader;
839
+ super.destroy(options);
840
+ geometry.destroy(true);
841
+ shader?.destroy();
842
+ }
843
+ _begin(shape) {
844
+ if (this._consumed) {
845
+ this._pending = [];
846
+ this._consumed = false;
847
+ }
848
+ this._pending.push(shape);
849
+ this._last = null;
850
+ return this;
851
+ }
852
+ _rewind(last) {
853
+ this._count = last.start;
854
+ }
855
+ _releaseRamps() {
856
+ const atlas = GradientAtlas.shared;
857
+ for (const key of this._rows.keys()) atlas.release(key);
858
+ this._rows.clear();
859
+ }
860
+ _alloc() {
861
+ const need = (this._count + 1) * 40;
862
+ if (need > this._f.length) {
863
+ const bigger = new Float32Array(Math.max(this._f.length * 2, need));
864
+ bigger.set(this._f);
865
+ this._f = bigger;
866
+ this.geometry.instanceBuffer.data = bigger;
867
+ }
868
+ const o = this._count * 40;
869
+ this._f.fill(0, o, o + 40);
870
+ this._count++;
871
+ return o;
872
+ }
873
+ _commit() {
874
+ const geometry = this.geometry;
875
+ geometry.instanceCount = this._count;
876
+ geometry.instanceBuffer.update(Math.max(this._count, 1) * 40 * 4);
877
+ }
878
+ _setBounds(o, minX, minY, maxX, maxY) {
879
+ const f = this._f;
880
+ f[o] = minX;
881
+ f[o + 1] = minY;
882
+ f[o + 2] = maxX;
883
+ f[o + 3] = maxY;
884
+ this.geometry.localBounds.addFrame(minX, minY, maxX, maxY);
885
+ }
886
+ _set4(o, a, b, c, d) {
887
+ const f = this._f;
888
+ f[o] = a;
889
+ f[o + 1] = b;
890
+ f[o + 2] = c;
891
+ f[o + 3] = d;
892
+ }
893
+ /** Premultiplied color. With a gradient paint it multiplies the ramp (white = untinted). */
894
+ _premul(o, rgba) {
895
+ const a = rgba[3];
896
+ this._set4(o, rgba[0] * a, rgba[1] * a, rgba[2] * a, a);
897
+ }
898
+ /** Writes gradient geometry + atlas row. Returns flags to add. */
899
+ _gradient(o, gradient, frame, forStroke) {
900
+ let row = this._rows.get(gradient.key);
901
+ if (row === void 0) {
902
+ row = GradientAtlas.shared.acquire(gradient);
903
+ this._rows.set(gradient.key, row);
904
+ }
905
+ gradient.resolve(frame, this._f, o + 28);
906
+ this._set4(o + 32, gradient.kind, row, EXTEND[gradient.extend] ?? Extend.Pad, 0);
907
+ return Flag.Dither | (forStroke ? Flag.GradStroke : 0);
908
+ }
909
+ _frame(shape) {
910
+ const b = this._box;
911
+ switch (shape.kind) {
912
+ case "rect": {
913
+ const c = Math.abs(Math.cos(shape.rot));
914
+ const sn = Math.abs(Math.sin(shape.rot));
915
+ const ex = shape.hw * c + shape.hh * sn;
916
+ const ey = shape.hw * sn + shape.hh * c;
917
+ return {
918
+ x: shape.cx - ex,
919
+ y: shape.cy - ey,
920
+ w: ex * 2,
921
+ h: ey * 2
922
+ };
923
+ }
924
+ case "ellipse": {
925
+ const c = Math.cos(shape.rot);
926
+ const sn = Math.sin(shape.rot);
927
+ const ex = Math.hypot(shape.rx * c, shape.ry * sn);
928
+ const ey = Math.hypot(shape.rx * sn, shape.ry * c);
929
+ return {
930
+ x: shape.cx - ex,
931
+ y: shape.cy - ey,
932
+ w: ex * 2,
933
+ h: ey * 2
934
+ };
935
+ }
936
+ case "arc": return {
937
+ x: shape.cx - shape.r,
938
+ y: shape.cy - shape.r,
939
+ w: shape.r * 2,
940
+ h: shape.r * 2,
941
+ start: shape.start,
942
+ sweep: shape.sweep
943
+ };
944
+ case "sector": return {
945
+ x: shape.cx - shape.r1,
946
+ y: shape.cy - shape.r1,
947
+ w: shape.r1 * 2,
948
+ h: shape.r1 * 2,
949
+ start: shape.start,
950
+ sweep: shape.sweep
951
+ };
952
+ case "heart": {
953
+ const hh = shape.size * HEART_H / HEART_W / 2;
954
+ const hw = shape.size / 2;
955
+ const c = Math.abs(Math.cos(shape.rot));
956
+ const sn = Math.abs(Math.sin(shape.rot));
957
+ const ex = hw * c + hh * sn;
958
+ const ey = hw * sn + hh * c;
959
+ return {
960
+ x: shape.cx - ex,
961
+ y: shape.cy - ey,
962
+ w: ex * 2,
963
+ h: ey * 2
964
+ };
965
+ }
966
+ case "star": return {
967
+ x: shape.cx - shape.r,
968
+ y: shape.cy - shape.r,
969
+ w: shape.r * 2,
970
+ h: shape.r * 2
971
+ };
972
+ default:
973
+ b[0] = b[1] = Infinity;
974
+ b[2] = b[3] = -Infinity;
975
+ if (shape.kind === "triangle") pointsBox(shape.pts, b);
976
+ else if (shape.kind === "path") for (const sp of shape.subpaths) pointsBox(sp.pts, b);
977
+ else if (shape.kind === "area") {
978
+ pointsBox(shape.top, b);
979
+ pointsBox(shape.bottom, b);
980
+ }
981
+ if (!Number.isFinite(b[0])) return {
982
+ x: 0,
983
+ y: 0,
984
+ w: 0,
985
+ h: 0
986
+ };
987
+ return {
988
+ x: b[0],
989
+ y: b[1],
990
+ w: b[2] - b[0],
991
+ h: b[3] - b[1]
992
+ };
993
+ }
994
+ }
995
+ _emitClosed(shape, fill, stroke) {
996
+ if (fill && stroke && (fill.gradient && stroke.gradient || fill.blur !== stroke.blur)) {
997
+ this._emitClosed(shape, fill, null);
998
+ this._emitClosed(shape, null, stroke);
999
+ return;
1000
+ }
1001
+ const f = this._f;
1002
+ const o = this._alloc();
1003
+ const sigma = Math.max(fill?.blur ?? 0, stroke?.blur ?? 0);
1004
+ const frame = this._frame(shape);
1005
+ let flags = 0;
1006
+ let sw = 0;
1007
+ let align = 0;
1008
+ if (fill) flags |= Flag.Fill;
1009
+ if (stroke) {
1010
+ flags |= Flag.Stroke | stroke.cap << 8 | stroke.cap << 10;
1011
+ sw = stroke.width;
1012
+ align = stroke.align;
1013
+ }
1014
+ if (sigma > 0) flags |= Flag.Dither;
1015
+ const outset = (stroke ? sw / 2 * (1 + align) : 0) + 3 * sigma;
1016
+ let type;
1017
+ switch (shape.kind) {
1018
+ case "rect": {
1019
+ const grow = 1 + shape.smoothing;
1020
+ const lim = Math.min(shape.hw, shape.hh);
1021
+ const [tl, tr, br, bl] = shape.r.map((r) => Math.min(r * grow, lim));
1022
+ type = Prim.Rect;
1023
+ this._set4(o + 4, shape.cx, shape.cy, shape.hw, shape.hh);
1024
+ this._set4(o + 8, br, tr, bl, tl);
1025
+ this._set4(o + 12, squircleExponent(shape.smoothing), 0, 0, shape.rot);
1026
+ break;
1027
+ }
1028
+ case "ellipse":
1029
+ type = Prim.Ellipse;
1030
+ this._set4(o + 4, shape.cx, shape.cy, shape.rx, shape.ry);
1031
+ f[o + 15] = shape.rot;
1032
+ break;
1033
+ case "sector":
1034
+ type = Prim.Sector;
1035
+ this._set4(o + 4, shape.cx, shape.cy, shape.r1, shape.start);
1036
+ this._set4(o + 8, shape.sweep, shape.r0, shape.corner, 0);
1037
+ break;
1038
+ case "heart": {
1039
+ const s = shape.size / (HEART_W + 2 * shape.round);
1040
+ type = Prim.Heart;
1041
+ this._set4(o + 4, shape.cx, shape.cy, s, shape.round);
1042
+ f[o + 15] = shape.rot;
1043
+ break;
1044
+ }
1045
+ case "star":
1046
+ type = Prim.Star;
1047
+ this._set4(o + 4, shape.cx, shape.cy, shape.r, shape.rot);
1048
+ this._set4(o + 8, shape.n, shape.en, Math.min(shape.corner, shape.ri * .9), 0);
1049
+ break;
1050
+ case "triangle": {
1051
+ const p = insetTriangle(shape.pts, shape.corner);
1052
+ type = Prim.Triangle;
1053
+ this._set4(o + 4, p[0], p[1], p[2], p[3]);
1054
+ this._set4(o + 8, p[4], p[5], p[6], 0);
1055
+ break;
1056
+ }
1057
+ default:
1058
+ this._count--;
1059
+ return;
1060
+ }
1061
+ const b = this._box;
1062
+ if (shape.kind === "sector") {
1063
+ b[0] = b[1] = Infinity;
1064
+ b[2] = b[3] = -Infinity;
1065
+ arcExtent(shape.cx, shape.cy, shape.r1, shape.start, shape.sweep, b);
1066
+ if (shape.r0 > 0) arcExtent(shape.cx, shape.cy, shape.r0, shape.start, shape.sweep, b);
1067
+ else pointsBox([shape.cx, shape.cy], b);
1068
+ } else {
1069
+ b[0] = frame.x;
1070
+ b[1] = frame.y;
1071
+ b[2] = frame.x + frame.w;
1072
+ b[3] = frame.y + frame.h;
1073
+ }
1074
+ this._setBounds(o, b[0] - outset, b[1] - outset, b[2] + outset, b[3] + outset);
1075
+ const gradient = fill?.gradient ?? stroke?.gradient ?? null;
1076
+ if (fill) this._premul(o + 20, fill.rgba);
1077
+ if (stroke) this._premul(o + 24, stroke.rgba);
1078
+ if (gradient) flags |= this._gradient(o, gradient, frame, !fill?.gradient);
1079
+ if (stroke?.dash && shape.kind === "ellipse") {
1080
+ let [dash, gap] = stroke.dash;
1081
+ if (stroke.dashFit) {
1082
+ const rm = (shape.rx + shape.ry) / 2 + align * sw / 2;
1083
+ const L = TAU * rm;
1084
+ const s = L / (Math.max(1, Math.round(L / (dash + gap))) * (dash + gap));
1085
+ dash *= s;
1086
+ gap *= s;
1087
+ }
1088
+ flags |= Flag.Dash;
1089
+ this._set4(o + 36, dash, gap, stroke.dashOffset, align);
1090
+ } else f[o + 39] = align;
1091
+ this._set4(o + 16, type, flags, sw, sigma);
1092
+ }
1093
+ _emitArc(shape, stroke) {
1094
+ const o = this._alloc();
1095
+ const hw = stroke.width / 2;
1096
+ const sigma = stroke.blur;
1097
+ let flags = Flag.Stroke | stroke.cap << 8 | stroke.cap << 10;
1098
+ if (sigma > 0) flags |= Flag.Dither;
1099
+ this._set4(o + 4, shape.cx, shape.cy, shape.r, shape.start);
1100
+ this._set4(o + 8, shape.sweep, 0, 0, 0);
1101
+ const b = this._box;
1102
+ b[0] = b[1] = Infinity;
1103
+ b[2] = b[3] = -Infinity;
1104
+ arcExtent(shape.cx, shape.cy, shape.r, shape.start, shape.sweep, b);
1105
+ const pad = hw + 3 * sigma;
1106
+ this._setBounds(o, b[0] - pad, b[1] - pad, b[2] + pad, b[3] + pad);
1107
+ this._premul(o + 24, stroke.rgba);
1108
+ if (stroke.gradient) flags |= this._gradient(o, stroke.gradient, this._frame(shape), true);
1109
+ if (stroke.dash) {
1110
+ let [dash, gap] = stroke.dash;
1111
+ if (stroke.dashFit && Math.abs(shape.sweep) >= TAU - 1e-6) {
1112
+ const L = TAU * shape.r;
1113
+ const s = L / (Math.max(1, Math.round(L / (dash + gap))) * (dash + gap));
1114
+ dash *= s;
1115
+ gap *= s;
1116
+ }
1117
+ flags |= Flag.Dash;
1118
+ this._set4(o + 36, dash, gap, stroke.dashOffset, 0);
1119
+ }
1120
+ this._set4(o + 16, Prim.Arc, flags, stroke.width, sigma);
1121
+ }
1122
+ _emitPath(raw, closed, stroke, frameOverride) {
1123
+ const { pts, keep } = cleanPoints(raw, closed);
1124
+ const n = pts.length / 2;
1125
+ if (n === 0) return;
1126
+ if (n < 3) closed = false;
1127
+ const segs = n === 1 ? 1 : closed ? n : n - 1;
1128
+ const cum = new Array(segs + 1);
1129
+ cum[0] = 0;
1130
+ for (let i = 0; i < segs; i++) {
1131
+ const j = (i + 1) % n;
1132
+ cum[i + 1] = cum[i] + Math.hypot(pts[j * 2] - pts[i * 2], pts[j * 2 + 1] - pts[i * 2 + 1]);
1133
+ }
1134
+ const total = cum[segs];
1135
+ let dash = 0;
1136
+ let gap = 0;
1137
+ if (stroke.dash) {
1138
+ [dash, gap] = stroke.dash;
1139
+ if (closed && stroke.dashFit && total > 0) {
1140
+ const s = total / (Math.max(1, Math.round(total / (dash + gap))) * (dash + gap));
1141
+ dash *= s;
1142
+ gap *= s;
1143
+ }
1144
+ }
1145
+ const b = this._box;
1146
+ b[0] = b[1] = Infinity;
1147
+ b[2] = b[3] = -Infinity;
1148
+ pointsBox(pts, b);
1149
+ const frame = frameOverride ?? {
1150
+ x: b[0],
1151
+ y: b[1],
1152
+ w: b[2] - b[0],
1153
+ h: b[3] - b[1]
1154
+ };
1155
+ const hw = stroke.width / 2;
1156
+ const radii = new Array(n);
1157
+ const widths = stroke.widths;
1158
+ for (let i = 0; i < n; i++) if (widths && widths.length === raw.length / 2 && widths.length > 2) radii[i] = widths[keep[i]] / 2;
1159
+ else if (widths && widths.length >= 2) {
1160
+ const u = total > 0 ? cum[Math.min(i, segs)] / total : 0;
1161
+ radii[i] = (widths[0] + (widths[widths.length - 1] - widths[0]) * u) / 2;
1162
+ } else radii[i] = hw;
1163
+ const sigma = stroke.blur;
1164
+ const pad = hw + 3 * sigma;
1165
+ let base = Flag.Stroke | stroke.cap << 8 | stroke.cap << 10;
1166
+ if (sigma > 0) base |= Flag.Dither;
1167
+ if (stroke.dash) base |= Flag.Dash;
1168
+ if (closed) base |= Flag.Closed;
1169
+ let row = -1;
1170
+ let gradFlags = 0;
1171
+ for (let i = 0; i < segs; i++) {
1172
+ const o = this._alloc();
1173
+ const ia = i;
1174
+ const ib = n === 1 ? 0 : (i + 1) % n;
1175
+ const ax = pts[ia * 2];
1176
+ const ay = pts[ia * 2 + 1];
1177
+ const bx = pts[ib * 2];
1178
+ const by = pts[ib * 2 + 1];
1179
+ const hasPrev = closed || i > 0;
1180
+ const hasNext = closed || i < segs - 1;
1181
+ const ip = (ia - 1 + n) % n;
1182
+ const inx = (ib + 1) % n;
1183
+ let flags = base;
1184
+ if (hasPrev) flags |= Flag.Prev;
1185
+ if (hasNext) flags |= Flag.Next;
1186
+ this._set4(o + 4, ax, ay, bx, by);
1187
+ this._set4(o + 8, hasPrev ? pts[ip * 2] : ax, hasPrev ? pts[ip * 2 + 1] : ay, hasNext ? pts[inx * 2] : bx, hasNext ? pts[inx * 2 + 1] : by);
1188
+ this._set4(o + 12, cum[i], total, radii[ia], radii[ib]);
1189
+ this._setBounds(o, Math.min(ax, bx) - pad, Math.min(ay, by) - pad, Math.max(ax, bx) + pad, Math.max(ay, by) + pad);
1190
+ this._premul(o + 24, stroke.rgba);
1191
+ if (stroke.gradient) {
1192
+ if (row < 0) {
1193
+ gradFlags = this._gradient(o, stroke.gradient, frame, true);
1194
+ row = this._f[o + 33];
1195
+ } else {
1196
+ stroke.gradient.resolve(frame, this._f, o + 28);
1197
+ this._set4(o + 32, stroke.gradient.kind, row, EXTEND[stroke.gradient.extend] ?? Extend.Pad, 0);
1198
+ }
1199
+ flags |= gradFlags;
1200
+ }
1201
+ if (stroke.dash) this._set4(o + 36, dash, gap, stroke.dashOffset, 0);
1202
+ this._f[o + 39] = radii[hasPrev ? ip : ia];
1203
+ this._f[o + 35] = radii[hasNext ? inx : ib];
1204
+ this._set4(o + 16, Prim.Segment, flags, stroke.width, sigma);
1205
+ }
1206
+ }
1207
+ _emitArea(shape, fill) {
1208
+ const { top, bottom } = shape;
1209
+ const n = top.length / 2;
1210
+ if (n < 2) return;
1211
+ const frame = this._frame(shape);
1212
+ const sigma = fill.blur;
1213
+ const pad = 3 * sigma;
1214
+ const y0 = frame.y - pad;
1215
+ const y1 = frame.y + frame.h + pad;
1216
+ let base = Flag.Fill;
1217
+ if (sigma > 0) base |= Flag.Dither;
1218
+ let row = -1;
1219
+ let gradFlags = 0;
1220
+ let first = -1;
1221
+ let lastIdx = -1;
1222
+ for (let i = 0; i < n - 1; i++) if (top[(i + 1) * 2] - top[i * 2] > 1e-9) {
1223
+ if (first < 0) first = i;
1224
+ lastIdx = i;
1225
+ }
1226
+ for (let i = first; i >= 0 && i <= lastIdx; i++) {
1227
+ const xa = top[i * 2];
1228
+ const xb = top[(i + 1) * 2];
1229
+ if (xb - xa <= 1e-9) continue;
1230
+ const o = this._alloc();
1231
+ let flags = base;
1232
+ if (i === first) flags |= Flag.EdgeLeft;
1233
+ if (i === lastIdx) flags |= Flag.EdgeRight;
1234
+ this._set4(o + 4, xa, top[i * 2 + 1], xb, top[i * 2 + 3]);
1235
+ this._set4(o + 8, bottom[i * 2], bottom[i * 2 + 1], bottom[(i + 1) * 2], bottom[i * 2 + 3]);
1236
+ this._setBounds(o, i === first ? xa - pad : xa, y0, i === lastIdx ? xb + pad : xb, y1);
1237
+ this._premul(o + 20, fill.rgba);
1238
+ if (fill.gradient) {
1239
+ if (row < 0) {
1240
+ gradFlags = this._gradient(o, fill.gradient, frame, false);
1241
+ row = this._f[o + 33];
1242
+ } else {
1243
+ fill.gradient.resolve(frame, this._f, o + 28);
1244
+ this._set4(o + 32, fill.gradient.kind, row, EXTEND[fill.gradient.extend] ?? Extend.Pad, 0);
1245
+ }
1246
+ flags |= gradFlags;
1247
+ }
1248
+ this._set4(o + 16, Prim.Area, flags, 0, sigma);
1249
+ }
1250
+ }
1251
+ };
1252
+ function reversePairs(pts) {
1253
+ const out = [];
1254
+ for (let i = pts.length - 2; i >= 0; i -= 2) out.push(pts[i], pts[i + 1]);
1255
+ return out;
1256
+ }
1257
+ /** Shrinks a triangle towards its incenter so that re-inflating by `r` rounds its corners. */
1258
+ function insetTriangle(p, r) {
1259
+ if (r <= 0) return [...p, 0];
1260
+ const [x0, y0, x1, y1, x2, y2] = p;
1261
+ const a = Math.hypot(x2 - x1, y2 - y1);
1262
+ const b = Math.hypot(x2 - x0, y2 - y0);
1263
+ const c = Math.hypot(x1 - x0, y1 - y0);
1264
+ const per = a + b + c;
1265
+ if (per <= 0) return [...p, 0];
1266
+ const ix = (a * x0 + b * x1 + c * x2) / per;
1267
+ const iy = (a * y0 + b * y1 + c * y2) / per;
1268
+ const inr = 2 * (Math.abs((x1 - x0) * (y2 - y0) - (x2 - x0) * (y1 - y0)) / 2) / per;
1269
+ const rr = Math.min(r, inr * .95);
1270
+ const k = (inr - rr) / inr;
1271
+ return [
1272
+ ix + (x0 - ix) * k,
1273
+ iy + (y0 - iy) * k,
1274
+ ix + (x1 - ix) * k,
1275
+ iy + (y1 - iy) * k,
1276
+ ix + (x2 - ix) * k,
1277
+ iy + (y2 - iy) * k,
1278
+ rr
1279
+ ];
1280
+ }
1281
+ //#endregion
1282
+ export { SilkGeometry, SilkGraphics };