pixi-silk 0.1.0 → 1.0.2

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.
Files changed (214) hide show
  1. package/CHANGELOG.md +114 -0
  2. package/README.md +48 -68
  3. package/dist/SilkGraphics.d.ts +270 -55
  4. package/dist/SilkGraphics.js +909 -147
  5. package/dist/app.d.ts +32 -16
  6. package/dist/app.js +44 -10
  7. package/dist/atlas.d.ts +13 -6
  8. package/dist/atlas.js +13 -6
  9. package/dist/batch.d.ts +63 -0
  10. package/dist/batch.js +327 -0
  11. package/dist/color.d.ts +4 -1
  12. package/dist/color.js +9 -4
  13. package/dist/constants.js +31 -5
  14. package/dist/curves.d.ts +9 -3
  15. package/dist/curves.js +11 -5
  16. package/dist/glass/Glass.d.ts +62 -0
  17. package/dist/glass/Glass.js +135 -0
  18. package/dist/glass/GlassFilter.d.ts +33 -0
  19. package/dist/glass/GlassFilter.js +264 -0
  20. package/dist/glass/glass-wgsl.js +129 -0
  21. package/dist/glass/options.d.ts +63 -0
  22. package/dist/glass/options.js +55 -0
  23. package/dist/glass.d.ts +4 -0
  24. package/dist/glass.js +4 -0
  25. package/dist/glow/Glow.d.ts +60 -0
  26. package/dist/glow/Glow.js +65 -0
  27. package/dist/glow/aurora.d.ts +66 -0
  28. package/dist/glow/aurora.js +133 -0
  29. package/dist/glow/light.d.ts +38 -0
  30. package/dist/glow/light.js +83 -0
  31. package/dist/glow/recipes.d.ts +37 -0
  32. package/dist/glow/recipes.js +154 -0
  33. package/dist/glow/shapes.d.ts +13 -0
  34. package/dist/glow/shapes.js +34 -0
  35. package/dist/glow.d.ts +6 -0
  36. package/dist/glow.js +6 -0
  37. package/dist/gradient.d.ts +57 -4
  38. package/dist/gradient.js +142 -4
  39. package/dist/history.d.ts +194 -0
  40. package/dist/history.js +350 -0
  41. package/dist/hit.js +160 -26
  42. package/dist/index.d.ts +6 -5
  43. package/dist/index.js +4 -3
  44. package/dist/layout/flex.js +409 -0
  45. package/dist/layout/node.d.ts +53 -0
  46. package/dist/layout/node.js +117 -0
  47. package/dist/layout/style.d.ts +115 -0
  48. package/dist/layout.d.ts +3 -0
  49. package/dist/layout.js +2 -0
  50. package/dist/motion.d.ts +14 -10
  51. package/dist/motion.js +14 -10
  52. package/dist/path.d.ts +150 -0
  53. package/dist/path.js +761 -0
  54. package/dist/selection.d.ts +133 -0
  55. package/dist/selection.js +194 -0
  56. package/dist/shader-slot.js +9 -0
  57. package/dist/shader-wgsl.js +946 -0
  58. package/dist/shader.js +480 -55
  59. package/dist/signal.d.ts +49 -0
  60. package/dist/signal.js +79 -0
  61. package/dist/stroke.js +271 -0
  62. package/dist/text/SilkText.d.ts +249 -0
  63. package/dist/text/SilkText.js +568 -0
  64. package/dist/text/atlas.d.ts +153 -0
  65. package/dist/text/atlas.js +316 -0
  66. package/dist/text/edt.d.ts +14 -0
  67. package/dist/text/edt.js +97 -0
  68. package/dist/text/label.d.ts +19 -0
  69. package/dist/text/label.js +38 -0
  70. package/dist/text/layout.d.ts +150 -0
  71. package/dist/text/layout.js +270 -0
  72. package/dist/text/segment.d.ts +34 -0
  73. package/dist/text/segment.js +98 -0
  74. package/dist/text/shader.js +355 -0
  75. package/dist/text.d.ts +7 -0
  76. package/dist/text.js +7 -0
  77. package/dist/ui/ButtonDock.d.ts +47 -0
  78. package/dist/ui/ButtonDock.js +110 -0
  79. package/dist/ui/RadialMenu.d.ts +222 -0
  80. package/dist/ui/RadialMenu.js +680 -0
  81. package/dist/ui/SilkButton.d.ts +149 -0
  82. package/dist/ui/SilkButton.js +323 -0
  83. package/dist/ui/ValuePill.d.ts +48 -0
  84. package/dist/ui/ValuePill.js +77 -0
  85. package/dist/ui/arc.d.ts +32 -0
  86. package/dist/ui/arc.js +58 -0
  87. package/dist/ui/brush.d.ts +117 -0
  88. package/dist/ui/brush.js +280 -0
  89. package/dist/ui/camera.d.ts +250 -0
  90. package/dist/ui/camera.js +634 -0
  91. package/dist/ui/color/ColorPicker.d.ts +72 -0
  92. package/dist/ui/color/ColorPicker.js +125 -0
  93. package/dist/ui/color/ColorSlider.d.ts +81 -0
  94. package/dist/ui/color/ColorSlider.js +227 -0
  95. package/dist/ui/color/ColorWheel.d.ts +34 -0
  96. package/dist/ui/color/ColorWheel.js +117 -0
  97. package/dist/ui/color/Eyedropper.d.ts +57 -0
  98. package/dist/ui/color/Eyedropper.js +159 -0
  99. package/dist/ui/color/OklchPicker.d.ts +45 -0
  100. package/dist/ui/color/OklchPicker.js +137 -0
  101. package/dist/ui/color/SwatchPalette.d.ts +60 -0
  102. package/dist/ui/color/SwatchPalette.js +160 -0
  103. package/dist/ui/color/SwatchPopup.d.ts +68 -0
  104. package/dist/ui/color/SwatchPopup.js +237 -0
  105. package/dist/ui/color/draw.d.ts +22 -0
  106. package/dist/ui/color/draw.js +128 -0
  107. package/dist/ui/color/model.d.ts +94 -0
  108. package/dist/ui/color/model.js +193 -0
  109. package/dist/ui/color/rings.d.ts +75 -0
  110. package/dist/ui/color/rings.js +247 -0
  111. package/dist/ui/color.d.ts +11 -0
  112. package/dist/ui/color.js +11 -0
  113. package/dist/ui/dial.d.ts +229 -0
  114. package/dist/ui/dial.js +553 -0
  115. package/dist/ui/driver.d.ts +24 -0
  116. package/dist/ui/driver.js +46 -0
  117. package/dist/ui/gesture.d.ts +66 -0
  118. package/dist/ui/gesture.js +116 -0
  119. package/dist/ui/gizmo.d.ts +199 -0
  120. package/dist/ui/gizmo.js +957 -0
  121. package/dist/ui/grid.d.ts +106 -0
  122. package/dist/ui/grid.js +228 -0
  123. package/dist/ui/group.d.ts +81 -0
  124. package/dist/ui/group.js +144 -0
  125. package/dist/ui/guides.d.ts +142 -0
  126. package/dist/ui/guides.js +379 -0
  127. package/dist/ui/halo.d.ts +164 -0
  128. package/dist/ui/halo.js +438 -0
  129. package/dist/ui/icon.d.ts +14 -0
  130. package/dist/ui/icon.js +40 -0
  131. package/dist/ui/index.d.ts +62 -0
  132. package/dist/ui/index.js +56 -0
  133. package/dist/ui/input.d.ts +28 -0
  134. package/dist/ui/input.js +49 -0
  135. package/dist/ui/inspector.d.ts +4 -0
  136. package/dist/ui/inspector.js +4 -0
  137. package/dist/ui/keys.d.ts +20 -0
  138. package/dist/ui/keys.js +65 -0
  139. package/dist/ui/label.d.ts +18 -0
  140. package/dist/ui/layers.d.ts +5 -0
  141. package/dist/ui/layers.js +5 -0
  142. package/dist/ui/layout/debug.d.ts +38 -0
  143. package/dist/ui/layout/debug.js +299 -0
  144. package/dist/ui/layout/flex-container.d.ts +150 -0
  145. package/dist/ui/layout/flex-container.js +441 -0
  146. package/dist/ui/layout/simulate.d.ts +49 -0
  147. package/dist/ui/layout/simulate.js +92 -0
  148. package/dist/ui/layout/smart-container.d.ts +180 -0
  149. package/dist/ui/layout/smart-container.js +302 -0
  150. package/dist/ui/layout.d.ts +5 -0
  151. package/dist/ui/layout.js +5 -0
  152. package/dist/ui/marquee.d.ts +124 -0
  153. package/dist/ui/marquee.js +321 -0
  154. package/dist/ui/measure.d.ts +138 -0
  155. package/dist/ui/measure.js +351 -0
  156. package/dist/ui/minimap.d.ts +2 -0
  157. package/dist/ui/minimap.js +2 -0
  158. package/dist/ui/modes.d.ts +186 -0
  159. package/dist/ui/modes.js +380 -0
  160. package/dist/ui/odometer.d.ts +104 -0
  161. package/dist/ui/odometer.js +244 -0
  162. package/dist/ui/outline.d.ts +104 -0
  163. package/dist/ui/outline.js +310 -0
  164. package/dist/ui/paint.js +22 -0
  165. package/dist/ui/panel.d.ts +4 -0
  166. package/dist/ui/panel.js +4 -0
  167. package/dist/ui/panels/Inspector.d.ts +228 -0
  168. package/dist/ui/panels/Inspector.js +516 -0
  169. package/dist/ui/panels/LayerTree.d.ts +193 -0
  170. package/dist/ui/panels/LayerTree.js +878 -0
  171. package/dist/ui/panels/Minimap.d.ts +140 -0
  172. package/dist/ui/panels/Minimap.js +412 -0
  173. package/dist/ui/panels/Panel.d.ts +273 -0
  174. package/dist/ui/panels/Panel.js +767 -0
  175. package/dist/ui/panels/ScrollView.d.ts +126 -0
  176. package/dist/ui/panels/ScrollView.js +358 -0
  177. package/dist/ui/panels/binding.d.ts +59 -0
  178. package/dist/ui/panels/binding.js +84 -0
  179. package/dist/ui/panels/fields.d.ts +265 -0
  180. package/dist/ui/panels/fields.js +760 -0
  181. package/dist/ui/panels/icons.d.ts +7 -0
  182. package/dist/ui/panels/icons.js +136 -0
  183. package/dist/ui/panels/pixi-tree.d.ts +35 -0
  184. package/dist/ui/panels/pixi-tree.js +82 -0
  185. package/dist/ui/panels/theme.d.ts +79 -0
  186. package/dist/ui/panels/theme.js +91 -0
  187. package/dist/ui/panels/tree.d.ts +130 -0
  188. package/dist/ui/panels/tree.js +199 -0
  189. package/dist/ui/pathgizmo.d.ts +253 -0
  190. package/dist/ui/pathgizmo.js +932 -0
  191. package/dist/ui/pill.d.ts +3 -0
  192. package/dist/ui/pill.js +2 -0
  193. package/dist/ui/ruler.d.ts +113 -0
  194. package/dist/ui/ruler.js +371 -0
  195. package/dist/ui/snap.d.ts +151 -0
  196. package/dist/ui/snap.js +380 -0
  197. package/dist/ui/spacing.d.ts +122 -0
  198. package/dist/ui/spacing.js +346 -0
  199. package/dist/ui/textedit.d.ts +304 -0
  200. package/dist/ui/textedit.js +1085 -0
  201. package/dist/ui/timeline.d.ts +175 -0
  202. package/dist/ui/timeline.js +488 -0
  203. package/dist/webgpu.d.ts +25 -0
  204. package/dist/webgpu.js +32 -0
  205. package/package.json +172 -1
  206. package/skills/pixi-silk/SKILL.md +138 -0
  207. package/skills/pixi-silk-app-rendering/SKILL.md +114 -0
  208. package/skills/pixi-silk-drawing/SKILL.md +154 -0
  209. package/skills/pixi-silk-editor/SKILL.md +200 -0
  210. package/skills/pixi-silk-layout/SKILL.md +150 -0
  211. package/skills/pixi-silk-migrate-from-graphics/SKILL.md +115 -0
  212. package/skills/pixi-silk-paint/SKILL.md +132 -0
  213. package/skills/pixi-silk-text/SKILL.md +127 -0
  214. package/skills/pixi-silk-widgets/SKILL.md +139 -0
@@ -1,12 +1,35 @@
1
1
  import { parseColor } from "./color.js";
2
- import { Cap, Extend, Flag, INSTANCE_ATTRIBUTES, Prim, TAU } from "./constants.js";
2
+ import { Cap, Extend, Flag, INSTANCE_ATTRIBUTES, Join, Prim, TAU } from "./constants.js";
3
3
  import { GradientAtlas } from "./atlas.js";
4
4
  import { catmullRom, flattenCubic, flattenQuadratic, monotoneX, toFlat } from "./curves.js";
5
5
  import { Gradient } from "./gradient.js";
6
+ import { joinPolygon, nearWindows } from "./stroke.js";
6
7
  import { hitTest } from "./hit.js";
8
+ import { drawPath, parseSvgPath } from "./path.js";
7
9
  import { createSilkShader } from "./shader.js";
8
- import { Bounds, Buffer, BufferUsage, Color, Geometry, Mesh } from "pixi.js";
10
+ import { Bounds, Buffer, BufferImageSource, BufferUsage, Color, Geometry, Mesh } from "pixi.js";
9
11
  //#region src/SilkGraphics.ts
12
+ const NO_FX = {
13
+ blurTo: -1,
14
+ blurAngle: 0,
15
+ spread: 0,
16
+ add: false,
17
+ inner: [],
18
+ union: false,
19
+ op: -1
20
+ };
21
+ /** Shape operation ids in the records of a Prim.Union instance. PUSH starts a new operand. */
22
+ const OP = {
23
+ union: 0,
24
+ subtract: 1,
25
+ intersect: 2,
26
+ exclude: 3
27
+ };
28
+ const PUSH = 4;
29
+ /** Record type that combines the two topmost operands with its op. */
30
+ const POP = 255;
31
+ /** Operands a shape operation can hold at once: the shader's stack. */
32
+ const OP_STACK = 8;
10
33
  const CLOSED_KINDS = /* @__PURE__ */ new Set([
11
34
  "rect",
12
35
  "ellipse",
@@ -20,19 +43,30 @@ const FILL_KEYS = [
20
43
  "alpha",
21
44
  "gradient",
22
45
  "blur",
46
+ "spread",
47
+ "blend",
48
+ "innerShadow",
23
49
  "width",
24
50
  "alignment",
25
51
  "cap",
26
52
  "dash",
27
53
  "dashOffset",
28
54
  "dashFit",
29
- "join"
55
+ "join",
56
+ "miterLimit",
57
+ "union",
58
+ "op"
30
59
  ];
31
60
  const CAPS = {
32
61
  butt: Cap.Butt,
33
62
  round: Cap.Round,
34
63
  square: Cap.Square
35
64
  };
65
+ const JOINS = {
66
+ miter: Join.Miter,
67
+ bevel: Join.Bevel,
68
+ round: Join.Round
69
+ };
36
70
  const EXTEND = {
37
71
  pad: Extend.Pad,
38
72
  repeat: Extend.Repeat,
@@ -40,12 +74,20 @@ const EXTEND = {
40
74
  };
41
75
  const HEART_W = 1.2071;
42
76
  const HEART_H = 1.1036;
43
- let warnedPathFill = false;
44
77
  let warnedAreaRotation = false;
78
+ let warnedOpDepth = false;
45
79
  function scalePaint(p, k) {
46
- return k === 1 ? p : {
80
+ if (k === 1) return p;
81
+ return {
47
82
  ...p,
48
- blur: p.blur * k
83
+ blur: p.blur * k,
84
+ blurTo: p.blurTo < 0 ? -1 : p.blurTo * k,
85
+ spread: p.spread * k,
86
+ inner: p.inner.map((s) => ({
87
+ ...s,
88
+ blur: s.blur * k,
89
+ spread: s.spread * k
90
+ }))
49
91
  };
50
92
  }
51
93
  function scaleStroke(st, k) {
@@ -55,6 +97,8 @@ function scaleStroke(st, k) {
55
97
  width: st.width * k,
56
98
  widths: st.widths ? st.widths.map((w) => w * k) : null,
57
99
  blur: st.blur * k,
100
+ blurTo: st.blurTo < 0 ? -1 : st.blurTo * k,
101
+ spread: st.spread * k,
58
102
  dash: st.dash ? [st.dash[0] * k, st.dash[1] * k] : null,
59
103
  dashOffset: st.dashOffset * k
60
104
  };
@@ -63,6 +107,16 @@ function isStyleObject(v) {
63
107
  if (typeof v !== "object" || v === null || Array.isArray(v) || v instanceof Color || ArrayBuffer.isView(v)) return false;
64
108
  return FILL_KEYS.some((k) => k in v);
65
109
  }
110
+ function resolveInner(s) {
111
+ return {
112
+ rgba: parseColor(s.color ?? 0, s.alpha ?? 1),
113
+ x: s.x ?? 0,
114
+ y: s.y ?? 0,
115
+ blur: Math.max(0, s.blur ?? 0),
116
+ spread: s.spread ?? 0,
117
+ add: s.blend === "add"
118
+ };
119
+ }
66
120
  function resolvePaint(input) {
67
121
  if (input instanceof Gradient) return {
68
122
  rgba: [
@@ -72,7 +126,8 @@ function resolvePaint(input) {
72
126
  1
73
127
  ],
74
128
  gradient: input,
75
- blur: 0
129
+ blur: 0,
130
+ ...NO_FX
76
131
  };
77
132
  if (input === void 0 || input === null) return {
78
133
  rgba: [
@@ -82,18 +137,32 @@ function resolvePaint(input) {
82
137
  1
83
138
  ],
84
139
  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)
140
+ blur: 0,
141
+ ...NO_FX
92
142
  };
143
+ if (isStyleObject(input)) {
144
+ const { blur, spread, blend, innerShadow, union, op, ...rest } = input;
145
+ const progressive = typeof blur === "object" && blur !== null;
146
+ const shadows = innerShadow === void 0 ? [] : Array.isArray(innerShadow) ? innerShadow : [innerShadow];
147
+ return {
148
+ ...rest,
149
+ rgba: parseColor(input.color ?? 16777215, input.alpha ?? 1),
150
+ gradient: input.gradient ?? null,
151
+ blur: Math.max(0, progressive ? blur.from : blur ?? 0),
152
+ blurTo: progressive ? Math.max(0, blur.to) : -1,
153
+ blurAngle: progressive ? blur.angle ?? Math.PI / 2 : 0,
154
+ spread: spread ?? 0,
155
+ add: blend === "add",
156
+ inner: shadows.map(resolveInner),
157
+ union: union === true || op === "union",
158
+ op: op !== void 0 ? OP[op] ?? -1 : union === true ? OP.union : -1
159
+ };
160
+ }
93
161
  return {
94
162
  rgba: parseColor(input),
95
163
  gradient: null,
96
- blur: 0
164
+ blur: 0,
165
+ ...NO_FX
97
166
  };
98
167
  }
99
168
  function resolveStroke(input) {
@@ -112,13 +181,22 @@ function resolveStroke(input) {
112
181
  rgba: p.rgba,
113
182
  gradient: p.gradient,
114
183
  blur: p.blur,
184
+ blurTo: p.blurTo,
185
+ blurAngle: p.blurAngle,
186
+ spread: p.spread,
187
+ add: p.add,
188
+ inner: [],
189
+ union: p.union,
190
+ op: p.op,
115
191
  width: widths ? Math.max(...widths) : Math.max(0, p.width ?? 1),
116
192
  widths,
117
193
  align: Math.max(-1, Math.min(1, align)),
118
194
  cap: CAPS[p.cap ?? "butt"] ?? Cap.Butt,
119
195
  dash,
120
196
  dashOffset: p.dashOffset ?? 0,
121
- dashFit: p.dashFit ?? true
197
+ dashFit: p.dashFit ?? true,
198
+ join: JOINS[p.join ?? "miter"] ?? Join.Miter,
199
+ miterLimit: Math.max(1, p.miterLimit ?? 10)
122
200
  };
123
201
  }
124
202
  /** Exponent that makes a superellipse corner of radius r*(1+s) hug the same diagonal as a circle of radius r. */
@@ -182,16 +260,16 @@ function cleanPoints(pts, closed) {
182
260
  }
183
261
  /**
184
262
  * Geometry behind every {@link SilkGraphics}: one unit quad plus an interleaved instance buffer
185
- * (40 floats per primitive). You rarely need it directly.
263
+ * of 44 floats (`STRIDE`) per primitive. You rarely need it directly.
186
264
  */
187
265
  var SilkGeometry = class extends Geometry {
188
- /** Per-primitive data, grown on demand and reused across `clear()`. */
266
+ /** Per-primitive data, 44 floats each. It grows on demand and never shrinks, so `clear()` reuses it. */
189
267
  instanceBuffer;
190
268
  /** Bounds of everything drawn, in local units (includes stroke widths and blur). */
191
269
  localBounds = new Bounds();
192
270
  constructor() {
193
271
  const instanceBuffer = new Buffer({
194
- data: /* @__PURE__ */ new Float32Array(1280),
272
+ data: /* @__PURE__ */ new Float32Array(1408),
195
273
  usage: BufferUsage.VERTEX | BufferUsage.COPY_DST,
196
274
  label: "silk-instances",
197
275
  shrinkToFit: false
@@ -215,7 +293,7 @@ var SilkGeometry = class extends Geometry {
215
293
  attributes[name] = {
216
294
  buffer: instanceBuffer,
217
295
  format: "float32x4",
218
- stride: 160,
296
+ stride: 176,
219
297
  offset: i * 16,
220
298
  instance: true
221
299
  };
@@ -240,8 +318,11 @@ var SilkGeometry = class extends Geometry {
240
318
  };
241
319
  /**
242
320
  * 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.
321
+ * evaluated per pixel, so edges stay sharp at any zoom, rotation and device pixel ratio. Each
322
+ * object is one instanced draw call, and `enableSilkBatching()` merges neighbours further.
323
+ *
324
+ * It renders on WebGL2. For WebGPU, call `enableWebGPU()` from `pixi-silk/webgpu` before the first
325
+ * SilkGraphics, or create the app with `createSilkApp({ preference: 'webgpu' })`.
245
326
  *
246
327
  * Shapes go on the active path and are painted by {@link SilkGraphics.fill | fill()} or
247
328
  * {@link SilkGraphics.stroke | stroke()}. `stroke()` right after `fill()` paints the same shapes.
@@ -262,6 +343,12 @@ var SilkGraphics = class SilkGraphics extends Mesh {
262
343
  aaWidth: 1,
263
344
  minStrokePx: 1
264
345
  };
346
+ /** @internal Render pipe of new objects: `silk` once batching is on (see `enableSilkBatching`). */
347
+ static _pipeId = "mesh";
348
+ /** @internal Adds the batching pipe to a renderer that lacks it (set by `enableSilkBatching`). */
349
+ static _installPipe = null;
350
+ /** @internal Bumped on every change a batch has to copy again. */
351
+ _version = 0;
265
352
  _f;
266
353
  _count = 0;
267
354
  /** Shapes of the active path. fill() and stroke() consume them without clearing, like Pixi's GraphicsContext. */
@@ -288,23 +375,43 @@ var SilkGraphics = class SilkGraphics extends Mesh {
288
375
  ];
289
376
  _silk;
290
377
  _uniforms;
378
+ /** Edges of filled paths, one (ax, ay, bx, by) texel each, read by the shader. */
379
+ _poly = /* @__PURE__ */ new Float32Array(1024);
380
+ _polyCount = 0;
381
+ _polyDirty = false;
382
+ _polySource;
291
383
  constructor(options = {}) {
292
384
  const geometry = new SilkGeometry();
293
385
  const settings = {
294
386
  ...SilkGraphics.defaults,
295
387
  ...options
296
388
  };
297
- const shader = createSilkShader(settings);
389
+ const polys = new BufferImageSource({
390
+ resource: /* @__PURE__ */ new Float32Array(1024),
391
+ width: 256,
392
+ height: 1,
393
+ format: "rgba32float",
394
+ alphaMode: "premultiplied-alpha",
395
+ scaleMode: "nearest",
396
+ autoGenerateMipmaps: false,
397
+ label: "silk-paths"
398
+ });
399
+ const shader = createSilkShader(settings, polys);
298
400
  super({
299
401
  geometry,
300
402
  shader,
301
403
  label: options.label ?? "SilkGraphics"
302
404
  });
405
+ this.renderPipeId = options.batch === false ? "mesh" : SilkGraphics._pipeId;
406
+ this._polySource = polys;
303
407
  this._f = geometry.instanceBuffer.data;
304
408
  this._uniforms = shader.resources.silkUniforms;
305
409
  this._silk = this._uniforms.uniforms.uSilk;
306
410
  }
307
- /** Number of primitives (= instances in the single draw call). */
411
+ /**
412
+ * Number of primitives drawn, one instance each. A fill with blur, a dashed stroke or a long
413
+ * path can emit several. 0 after `clear()`.
414
+ */
308
415
  get primitiveCount() {
309
416
  return this._count;
310
417
  }
@@ -312,42 +419,60 @@ var SilkGraphics = class SilkGraphics extends Mesh {
312
419
  get dither() {
313
420
  return this._silk[0];
314
421
  }
315
- /** Sets the dither amplitude of this object. */
422
+ /** Takes effect on the next render. With batching on, objects regroup by their settings. */
316
423
  set dither(v) {
317
424
  this._setting(0, v);
318
425
  }
319
- /** Width of the anti-aliasing ramp in device pixels. 1 is a crisp box filter, larger is softer. */
426
+ /** Width of the anti-aliasing ramp in device pixels. 1 is a crisp box filter, larger is softer. Default 1. */
320
427
  get aaWidth() {
321
428
  return this._silk[1];
322
429
  }
323
- /** Sets the anti-aliasing width of this object. */
430
+ /** Takes effect on the next render. With batching on, objects regroup by their settings. */
324
431
  set aaWidth(v) {
325
432
  this._setting(1, v);
326
433
  }
327
- /** Strokes thinner than this many device pixels keep this width and fade instead (hairlines). */
434
+ /** Strokes thinner than this many device pixels keep this width and fade instead (hairlines). Default 1. */
328
435
  get minStrokePx() {
329
436
  return this._silk[2];
330
437
  }
331
- /** Sets the hairline threshold of this object. */
438
+ /** Takes effect on the next render. With batching on, objects regroup by their settings. */
332
439
  set minStrokePx(v) {
333
440
  this._setting(2, v);
334
441
  }
442
+ /** @internal Dither, AA width and hairline settings, as the shader reads them. */
443
+ get _settings() {
444
+ return this._silk;
445
+ }
446
+ /** @internal What a batch copies: instances and path texels. */
447
+ get _batchSource() {
448
+ return {
449
+ f: this._f,
450
+ count: this._count,
451
+ poly: this._poly,
452
+ polyCount: this._polyCount
453
+ };
454
+ }
335
455
  _setting(i, v) {
336
456
  this._silk[i] = v;
457
+ this._version++;
337
458
  this._uniforms.update();
459
+ if (this.parentRenderGroup) this.parentRenderGroup.structureDidChange = true;
338
460
  }
339
- /** Pushes the current transform (Pixi GraphicsContext API). */
461
+ /**
462
+ * Pushes the current drawing transform on a stack, as in Pixi's GraphicsContext. The transform
463
+ * applies to shapes drawn after it, not to the display object.
464
+ */
340
465
  save() {
341
466
  this._mStack.push({ ...this._m });
342
467
  return this;
343
468
  }
344
- /** Pops the transform pushed by the matching {@link SilkGraphics.save | save()}. */
469
+ /** Pops the transform pushed by the matching {@link SilkGraphics.save | save()}. Does nothing on an empty stack. */
345
470
  restore() {
346
471
  const m = this._mStack.pop();
347
472
  if (m) this._m = m;
348
473
  return this;
349
474
  }
350
- /** Resets the current transform to identity. */
475
+ /** Resets the drawing transform to identity. The `save()` stack is kept. */
351
476
  resetTransform() {
352
477
  this._m = {
353
478
  a: 1,
@@ -359,7 +484,10 @@ var SilkGraphics = class SilkGraphics extends Mesh {
359
484
  };
360
485
  return this;
361
486
  }
362
- /** Replaces the current transform with the matrix `[a c tx; b d ty]`. */
487
+ /**
488
+ * Replaces the drawing transform with the matrix `[a c tx; b d ty]`. Skew is not supported:
489
+ * shapes take the rotation and the scale of each axis, and widths take the uniform scale.
490
+ */
363
491
  setTransform(a, b, c, d, tx, ty) {
364
492
  this._m = {
365
493
  a,
@@ -371,14 +499,17 @@ var SilkGraphics = class SilkGraphics extends Mesh {
371
499
  };
372
500
  return this;
373
501
  }
374
- /** Moves the coordinate system. Shapes drawn next are offset by (x, y). */
502
+ /** Moves the drawing coordinate system. Shapes drawn next are offset by (x, y), local units. `y` defaults to `x`. */
375
503
  translateTransform(x, y = x) {
376
504
  const m = this._m;
377
505
  m.tx += m.a * x + m.c * y;
378
506
  m.ty += m.b * x + m.d * y;
379
507
  return this;
380
508
  }
381
- /** Scales the coordinate system. Stroke widths, dashes and blur scale with its uniform part. */
509
+ /**
510
+ * Scales the drawing coordinate system. `y` defaults to `x`. Stroke widths, dashes and blur
511
+ * scale with the uniform part, the square root of the determinant.
512
+ */
382
513
  scaleTransform(x, y = x) {
383
514
  const m = this._m;
384
515
  m.a *= x;
@@ -387,7 +518,7 @@ var SilkGraphics = class SilkGraphics extends Mesh {
387
518
  m.d *= y;
388
519
  return this;
389
520
  }
390
- /** Rotates the coordinate system by `angle` radians (clockwise on screen). Rotated primitives stay exact. */
521
+ /** Rotates the drawing coordinate system by `angle` radians, clockwise on screen. */
391
522
  rotateTransform(angle) {
392
523
  const m = this._m;
393
524
  const cos = Math.cos(angle);
@@ -431,15 +562,17 @@ var SilkGraphics = class SilkGraphics extends Mesh {
431
562
  get _mirrored() {
432
563
  return this._m.a * this._m.d - this._m.b * this._m.c < 0;
433
564
  }
434
- /** Axis-aligned rectangle (rotated by the current transform, if any). */
565
+ /** Rectangle with its top-left corner at (x, y). A negative width or height flips it to the other side. */
435
566
  rect(x, y, w, h) {
436
567
  return this.roundRect(x, y, w, h, 0);
437
568
  }
438
569
  /**
439
570
  * Rectangle with rounded corners. `smoothing` above 0 turns the corners into squircles.
571
+ * Negative radii count as 0.
440
572
  *
441
- * @param radius - one radius or `[topLeft, topRight, bottomRight, bottomLeft]`
442
- * @param smoothing - 0..1 continuous-curvature ("squircle") corners, iOS uses about 0.6
573
+ * @param radius - one radius or `[topLeft, topRight, bottomRight, bottomLeft]`, local units.
574
+ * Missing entries copy the first (and the top-right for bottom-left), as in CSS.
575
+ * @param smoothing - 0..1 continuous-curvature ("squircle") corners, iOS uses about 0.6. Default 0.
443
576
  */
444
577
  roundRect(x, y, w, h, radius = 0, smoothing = 0) {
445
578
  if (w < 0) {
@@ -472,15 +605,15 @@ var SilkGraphics = class SilkGraphics extends Mesh {
472
605
  k
473
606
  });
474
607
  }
475
- /** Rounded rect with fully round ends. */
608
+ /** Rounded rect whose corner radius is half its shorter side, so the short ends are half circles. */
476
609
  pill(x, y, w, h) {
477
610
  return this.roundRect(x, y, w, h, Math.min(Math.abs(w), Math.abs(h)) / 2);
478
611
  }
479
- /** Circle centred on (x, y). */
612
+ /** Circle centred on (x, y). A negative radius counts as positive. */
480
613
  circle(x, y, radius) {
481
614
  return this.ellipse(x, y, radius, radius);
482
615
  }
483
- /** Ellipse centred on (x, y) with an exact distance field (no polygon approximation). */
616
+ /** Ellipse centred on (x, y) with radii `rx` and `ry`, local units. It is not a polygon. */
484
617
  ellipse(x, y, rx, ry) {
485
618
  return this._begin({
486
619
  kind: "ellipse",
@@ -492,7 +625,10 @@ var SilkGraphics = class SilkGraphics extends Mesh {
492
625
  k: this._k
493
626
  });
494
627
  }
495
- /** Open circular arc (stroke it). Angles work as in canvas. It starts its own subpath. */
628
+ /**
629
+ * Open circular arc, for strokes. Angles are radians and work as in Canvas 2D: 0 points right
630
+ * and positive angles turn clockwise on screen. It starts its own subpath. A fill paints nothing.
631
+ */
496
632
  arc(cx, cy, radius, startAngle, endAngle, anticlockwise = false) {
497
633
  let sweep = endAngle - startAngle;
498
634
  if (!anticlockwise) {
@@ -508,7 +644,7 @@ var SilkGraphics = class SilkGraphics extends Mesh {
508
644
  }
509
645
  return this.arcSweep(cx, cy, radius, startAngle, sweep);
510
646
  }
511
- /** Open arc given start angle and signed sweep (positive = clockwise on screen). */
647
+ /** Open arc from a start angle and a signed sweep, radians. Positive sweeps clockwise on screen, clamped to one turn. */
512
648
  arcSweep(cx, cy, radius, startAngle, sweep) {
513
649
  sweep = Math.max(-TAU, Math.min(TAU, sweep));
514
650
  const k = this._k;
@@ -523,9 +659,10 @@ var SilkGraphics = class SilkGraphics extends Mesh {
523
659
  });
524
660
  }
525
661
  /**
526
- * Closed annular sector: pie slices, donut segments, gauge segments.
527
- * @param innerRadius - 0 for a pie slice
528
- * @param cornerRadius - rounds all corners
662
+ * Closed annular sector, for pie slices, donut segments and gauges. Angles are radians, and the
663
+ * sweep is clamped to one turn.
664
+ * @param innerRadius - 0 for a pie slice. Clamped to `radius`.
665
+ * @param cornerRadius - rounds all corners. Clamped to half the ring width.
529
666
  */
530
667
  sector(cx, cy, radius, startAngle, endAngle, innerRadius = 0, cornerRadius = 0) {
531
668
  const sweep = Math.max(-TAU, Math.min(TAU, endAngle - startAngle));
@@ -544,7 +681,7 @@ var SilkGraphics = class SilkGraphics extends Mesh {
544
681
  k
545
682
  });
546
683
  }
547
- /** Heart icon centred at (cx, cy), `size` wide. `rounding` (0..0.3) puffs it up. */
684
+ /** Heart icon centred at (cx, cy), `size` local units wide. `rounding` (0..0.3, default 0) puffs it up. */
548
685
  heart(cx, cy, size, rounding = 0) {
549
686
  const k = this._k;
550
687
  return this._begin({
@@ -557,7 +694,11 @@ var SilkGraphics = class SilkGraphics extends Mesh {
557
694
  k
558
695
  });
559
696
  }
560
- /** Star with `points` tips. `innerRadius` defaults to a classic star. */
697
+ /**
698
+ * Star with `points` tips (rounded, at least 3). Angles are radians. `innerRadius` defaults to
699
+ * half of `radius` and is capped at the inner radius of the regular polygon. `cornerRadius` rounds
700
+ * every corner.
701
+ */
561
702
  star(cx, cy, points, radius, innerRadius, rotation = 0, cornerRadius = 0) {
562
703
  const n = Math.max(3, Math.round(points));
563
704
  const an = Math.PI / n;
@@ -577,12 +718,12 @@ var SilkGraphics = class SilkGraphics extends Mesh {
577
718
  k
578
719
  });
579
720
  }
580
- /** Regular polygon (first vertex points up). */
721
+ /** Regular polygon with `sides` corners (rounded, at least 3), the first one pointing up. `rotation` is radians. */
581
722
  regularPoly(cx, cy, radius, sides, rotation = 0, cornerRadius = 0) {
582
723
  const n = Math.max(3, Math.round(sides));
583
724
  return this.star(cx, cy, n, radius, radius * Math.cos(Math.PI / n), rotation, cornerRadius);
584
725
  }
585
- /** Triangle through three points, with optionally rounded corners. */
726
+ /** Triangle through three points. `cornerRadius` (local units, default 0) rounds its corners. */
586
727
  triangle(x0, y0, x1, y1, x2, y2, cornerRadius = 0) {
587
728
  const k = this._k;
588
729
  return this._begin({
@@ -599,7 +740,7 @@ var SilkGraphics = class SilkGraphics extends Mesh {
599
740
  k
600
741
  });
601
742
  }
602
- /** Straight line (stroke it). */
743
+ /** Straight line from (x0, y0) to (x1, y1), as an open path. Stroke it. */
603
744
  line(x0, y0, x1, y1) {
604
745
  return this._begin({
605
746
  kind: "path",
@@ -615,7 +756,10 @@ var SilkGraphics = class SilkGraphics extends Mesh {
615
756
  k: this._k
616
757
  });
617
758
  }
618
- /** Polyline through points (stroke it), optionally smoothed. */
759
+ /**
760
+ * Open or closed path through points, optionally smoothed. Stroke it, or fill it as one
761
+ * even-odd shape. Smoothing resamples the points every `step` local units.
762
+ */
619
763
  polyline(points, options = {}) {
620
764
  let pts = toFlat(points);
621
765
  const closed = options.closed ?? false;
@@ -630,13 +774,19 @@ var SilkGraphics = class SilkGraphics extends Mesh {
630
774
  k: this._k
631
775
  });
632
776
  }
633
- /** Pixi-style polygon path. Only strokes are supported for arbitrary paths. */
777
+ /**
778
+ * Pixi-style polygon. `fill()` paints it as one shape (even-odd), so blur, spread, inner
779
+ * shadows and gradients work on bolts, drops and other free outlines.
780
+ */
634
781
  poly(points, close = true) {
635
782
  return this.polyline(points, { closed: close });
636
783
  }
637
784
  /**
638
785
  * 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
786
+ * The path transform's translation and scale apply, but rotation and skew do not: rotate the
787
+ * object instead. It warns once and ignores them.
788
+ * @param lower - baseline y, or a second series with the same number of points for bands.
789
+ * A different count warns and adds nothing.
640
790
  */
641
791
  area(points, lower, options = {}) {
642
792
  let top = toFlat(points);
@@ -675,7 +825,7 @@ var SilkGraphics = class SilkGraphics extends Mesh {
675
825
  k: this._k
676
826
  });
677
827
  }
678
- /** Starts a new, empty path (shapes drawn so far are kept). */
828
+ /** Starts a new, empty active path. Shapes already painted stay. Unpainted shapes are dropped. */
679
829
  beginPath() {
680
830
  this._pending = [];
681
831
  this._consumed = false;
@@ -683,7 +833,7 @@ var SilkGraphics = class SilkGraphics extends Mesh {
683
833
  this._cursor = null;
684
834
  return this;
685
835
  }
686
- /** Starts a new subpath at (x, y). Paths are stroke-only. */
836
+ /** Starts a new subpath at (x, y). `fill()` paints the path even-odd, `stroke()` follows it. */
687
837
  moveTo(x, y) {
688
838
  const shape = this._pathShape();
689
839
  const px = this._x(x, y);
@@ -695,7 +845,7 @@ var SilkGraphics = class SilkGraphics extends Mesh {
695
845
  this._cursor = [px, py];
696
846
  return this;
697
847
  }
698
- /** Straight segment from the current point to (x, y). */
848
+ /** Straight segment from the current point to (x, y). Without a current point it starts a subpath at (x, y). */
699
849
  lineTo(x, y) {
700
850
  const px = this._x(x, y);
701
851
  const py = this._y(x, y);
@@ -703,7 +853,7 @@ var SilkGraphics = class SilkGraphics extends Mesh {
703
853
  this._cursor = [px, py];
704
854
  return this;
705
855
  }
706
- /** Quadratic Bezier to (x, y), flattened adaptively. */
856
+ /** Quadratic Bezier to (x, y). It is flattened into 2 to 256 line segments, about one per 2 units of its control polygon. */
707
857
  quadraticCurveTo(cpx, cpy, x, y) {
708
858
  const [qx, qy, px, py] = this._pts([
709
859
  cpx,
@@ -717,7 +867,7 @@ var SilkGraphics = class SilkGraphics extends Mesh {
717
867
  this._cursor = [px, py];
718
868
  return this;
719
869
  }
720
- /** Cubic Bezier to (x, y), flattened adaptively. */
870
+ /** Cubic Bezier to (x, y). It is flattened into 2 to 256 line segments, about one per 2 units of its control polygon. */
721
871
  bezierCurveTo(cp1x, cp1y, cp2x, cp2y, x, y) {
722
872
  const [ax, ay, bx, by, px, py] = this._pts([
723
873
  cp1x,
@@ -733,7 +883,7 @@ var SilkGraphics = class SilkGraphics extends Mesh {
733
883
  this._cursor = [px, py];
734
884
  return this;
735
885
  }
736
- /** Closes the current subpath back to its first point. */
886
+ /** Closes the current subpath back to its first point. Does nothing after the path was painted. */
737
887
  closePath() {
738
888
  const shape = this._pending[this._pending.length - 1];
739
889
  if (!this._consumed && shape?.kind === "path" && shape.subpaths.length) {
@@ -743,6 +893,63 @@ var SilkGraphics = class SilkGraphics extends Mesh {
743
893
  }
744
894
  return this;
745
895
  }
896
+ /**
897
+ * Adds an SVG path (the `d` attribute) to the active path: M, L, H, V, C, S, Q, T, A and Z, in
898
+ * absolute and relative form. Fill it as one even-odd shape, stroke it, or combine it with
899
+ * other shapes. The current transform applies.
900
+ *
901
+ * @example
902
+ * ```ts
903
+ * g.svgPath('M12 2 L22 22 H2 Z M12 8 L17 18 H7 Z').fill(0xff9f0a);
904
+ * ```
905
+ */
906
+ svgPath(d) {
907
+ for (const path of parseSvgPath(d)) drawPath(this, path);
908
+ return this;
909
+ }
910
+ /**
911
+ * Adds one shape to the active path: what `a` draws combined with what `b` draws by `op`. The
912
+ * shapes each callback adds are merged first, and a callback can call `boolean()` again to nest.
913
+ * Paint the result with `fill()` or `stroke()` like any shape. The callbacks only add shapes:
914
+ * do not paint inside them.
915
+ *
916
+ * The outline is anti-aliased as sharply as a circle. Inside the shape the distance is a close
917
+ * bound: a large blur, spread or inner shadow can round its
918
+ * inner corners slightly.
919
+ *
920
+ * @example
921
+ * ```ts
922
+ * // a ring with a notch: a circle minus a smaller circle and a bar
923
+ * g.boolean('subtract', (g) => g.circle(50, 50, 40), (g) => g.circle(50, 50, 24).rect(46, 0, 8, 30))
924
+ * .fill(0x30d158)
925
+ * .stroke({ width: 2, color: 0xffffff });
926
+ * ```
927
+ */
928
+ boolean(op, a, b) {
929
+ const pending = this._pending;
930
+ const consumed = this._consumed;
931
+ const cursor = this._cursor;
932
+ const collect = (fn) => {
933
+ this._pending = [];
934
+ this._consumed = false;
935
+ this._last = null;
936
+ this._cursor = null;
937
+ fn(this);
938
+ return this._pending;
939
+ };
940
+ const sa = collect(a);
941
+ const sb = collect(b);
942
+ this._pending = pending;
943
+ this._consumed = consumed;
944
+ this._cursor = cursor;
945
+ return this._begin({
946
+ kind: "boolean",
947
+ op: OP[op] ?? OP.union,
948
+ a: sa,
949
+ b: sb,
950
+ k: this._k
951
+ });
952
+ }
746
953
  _pathShape() {
747
954
  const last = this._pending[this._pending.length - 1];
748
955
  if (!this._consumed && last?.kind === "path") return last;
@@ -767,28 +974,40 @@ var SilkGraphics = class SilkGraphics extends Mesh {
767
974
  }
768
975
  return sp;
769
976
  }
770
- /** Fills the shapes of the active path (Pixi semantics: `rect().circle().fill()` fills both). */
977
+ /**
978
+ * Fills the shapes of the active path, as in Pixi: `rect().circle().fill()` fills both. Each
979
+ * shape is painted on its own unless `union` or `op` is set. Open arcs are skipped. The path
980
+ * stays active, so `stroke()` can follow, and the next shape you add starts a new path. Does
981
+ * nothing on an empty path.
982
+ */
771
983
  fill(style) {
772
984
  const shapes = this._pending;
773
985
  if (!shapes.length) return this;
774
986
  const paint = resolvePaint(style);
775
987
  const start = this._count;
988
+ const polyStart = this._polyCount;
989
+ const ops = paint.op >= 0;
990
+ if (ops) this._emitOps(shapes, paint.op, scalePaint(paint, shapes[0].k), null);
776
991
  for (const shape of shapes) if (shape.kind === "area") this._emitArea(shape, scalePaint(paint, shape.k));
992
+ else if (ops) continue;
993
+ else if (shape.kind === "boolean") this._emitOps([shape], OP.union, scalePaint(paint, shape.k), null);
777
994
  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
- }
995
+ else if (shape.kind === "path") this._emitClosed(shape, scalePaint(paint, shape.k), null);
782
996
  this._last = {
783
997
  shapes,
784
998
  start,
785
- fill: paint
999
+ fill: paint,
1000
+ polyStart,
1001
+ count: this._count - start
786
1002
  };
787
1003
  this._consumed = true;
788
1004
  this._commit();
789
1005
  return this;
790
1006
  }
791
- /** Strokes the shapes of the active path. Right after `fill()` it merges into the same instances. */
1007
+ /**
1008
+ * Strokes the shapes of the active path. Right after `fill()` on closed shapes it shares their
1009
+ * instances, so fill and stroke cost no more than the fill. A width of 0 or less paints nothing.
1010
+ */
792
1011
  stroke(style) {
793
1012
  const shapes = this._pending;
794
1013
  if (!shapes.length) return this;
@@ -797,23 +1016,59 @@ var SilkGraphics = class SilkGraphics extends Mesh {
797
1016
  this._last = null;
798
1017
  this._consumed = true;
799
1018
  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))) {
1019
+ const opFill = last !== null && last.fill.op >= 0 && shapes.every((sh) => sh.kind !== "area");
1020
+ const closedLike = (sh) => CLOSED_KINDS.has(sh.kind) || sh.kind === "boolean";
1021
+ const merge = last && last.shapes === shapes && last.start + last.count === this._count && (opFill || last.count === shapes.length && shapes.every(closedLike));
1022
+ if (merge && opFill) {
1023
+ this._rewind(last);
1024
+ this._emitOps(shapes, last.fill.op, scalePaint(last.fill, shapes[0].k), scaleStroke(stroke, shapes[0].k));
1025
+ } else if (merge) {
801
1026
  this._rewind(last);
802
- for (const shape of shapes) this._emitClosed(shape, scalePaint(last.fill, shape.k), scaleStroke(stroke, shape.k));
1027
+ for (const shape of shapes) {
1028
+ const fill = scalePaint(last.fill, shape.k);
1029
+ if (shape.kind === "boolean") this._emitOps([shape], OP.union, fill, scaleStroke(stroke, shape.k));
1030
+ else this._emitClosed(shape, fill, scaleStroke(stroke, shape.k));
1031
+ }
1032
+ } else if (stroke.op > OP.union) {
1033
+ this._emitOps(shapes, stroke.op, null, scaleStroke(stroke, shapes[0].k));
1034
+ for (const shape of shapes) if (shape.kind === "arc") this._emitArc(shape, scaleStroke(stroke, shape.k));
1035
+ else if (shape.kind === "area") this._strokeSubpaths([{
1036
+ pts: shape.top,
1037
+ closed: false
1038
+ }], scaleStroke(stroke, shape.k));
1039
+ } else if (stroke.union) {
1040
+ const st = scaleStroke(stroke, shapes[0].k);
1041
+ const closed = shapes.filter(closedLike);
1042
+ const paths = shapes.flatMap((sh) => sh.kind === "path" ? sh.subpaths : []);
1043
+ if (closed.length) this._emitOps(closed, OP.union, null, st);
1044
+ if (paths.length) this._strokeSubpaths(paths, st);
1045
+ for (const shape of shapes) if (shape.kind === "arc") this._emitArc(shape, scaleStroke(stroke, shape.k));
1046
+ else if (shape.kind === "area") this._strokeSubpaths([{
1047
+ pts: shape.top,
1048
+ closed: false
1049
+ }], st);
803
1050
  } else for (const shape of shapes) {
804
1051
  const st = scaleStroke(stroke, shape.k);
805
- if (CLOSED_KINDS.has(shape.kind)) this._emitClosed(shape, null, st);
1052
+ if (shape.kind === "boolean") this._emitOps([shape], OP.union, null, st);
1053
+ else if (CLOSED_KINDS.has(shape.kind)) this._emitClosed(shape, null, st);
806
1054
  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);
1055
+ else if (shape.kind === "path") this._strokeSubpaths(shape.subpaths, st);
1056
+ else if (shape.kind === "area") this._strokeSubpaths([{
1057
+ pts: shape.top,
1058
+ closed: false
1059
+ }], st);
809
1060
  }
810
1061
  this._commit();
811
1062
  return this;
812
1063
  }
813
- /** Removes everything. Keeps the buffers for reuse, so redrawing every frame is cheap. */
1064
+ /**
1065
+ * Removes every shape and resets the drawing transform and its `save()` stack. It keeps the
1066
+ * buffers and releases gradient rows, so redrawing every frame does not allocate.
1067
+ */
814
1068
  clear() {
815
1069
  this._releaseRamps();
816
1070
  this._count = 0;
1071
+ this._polyCount = 0;
817
1072
  this._pending = [];
818
1073
  this._consumed = false;
819
1074
  this._last = null;
@@ -824,14 +1079,21 @@ var SilkGraphics = class SilkGraphics extends Mesh {
824
1079
  this._commit();
825
1080
  return this;
826
1081
  }
1082
+ /** @internal Gives a renderer the batching pipe the first time it meets a batched object. */
1083
+ collectRenderablesSimple(set, renderer, layer) {
1084
+ if (!(this.renderPipeId in renderer.renderPipes)) SilkGraphics._installPipe?.(renderer);
1085
+ super.collectRenderablesSimple(set, renderer, layer);
1086
+ }
827
1087
  /**
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.
1088
+ * Hit test against the painted shapes, in local coordinates. Rings are hollow and lines count
1089
+ * only within their stroke, at least 1 unit to each side. Blur does not widen the hit area. Pixi's event
1090
+ * system calls it when `eventMode` is set.
830
1091
  */
831
1092
  containsPoint(point) {
832
1093
  if (!this.geometry.localBounds.containsPoint(point.x, point.y)) return false;
833
- return hitTest(this._f, this._count, point.x, point.y);
1094
+ return hitTest(this._f, this._count, point.x, point.y, this._poly);
834
1095
  }
1096
+ /** Destroys the object with its geometry, shader and path texture, and releases its gradient rows. */
835
1097
  destroy(options) {
836
1098
  this._releaseRamps();
837
1099
  const geometry = this.geometry;
@@ -839,6 +1101,7 @@ var SilkGraphics = class SilkGraphics extends Mesh {
839
1101
  super.destroy(options);
840
1102
  geometry.destroy(true);
841
1103
  shader?.destroy();
1104
+ this._polySource.destroy();
842
1105
  }
843
1106
  _begin(shape) {
844
1107
  if (this._consumed) {
@@ -851,6 +1114,12 @@ var SilkGraphics = class SilkGraphics extends Mesh {
851
1114
  }
852
1115
  _rewind(last) {
853
1116
  this._count = last.start;
1117
+ this._polyCount = last.polyStart;
1118
+ }
1119
+ /** Tapered strokes keep one capsule per segment. Every other stroke is one field per path. */
1120
+ _strokeSubpaths(subpaths, stroke) {
1121
+ if (stroke.widths) for (const sp of subpaths) this._emitPath(sp.pts, sp.closed, stroke, null);
1122
+ else this._emitStroke(subpaths, stroke, null);
854
1123
  }
855
1124
  _releaseRamps() {
856
1125
  const atlas = GradientAtlas.shared;
@@ -858,22 +1127,55 @@ var SilkGraphics = class SilkGraphics extends Mesh {
858
1127
  this._rows.clear();
859
1128
  }
860
1129
  _alloc() {
861
- const need = (this._count + 1) * 40;
1130
+ const need = (this._count + 1) * 44;
862
1131
  if (need > this._f.length) {
863
1132
  const bigger = new Float32Array(Math.max(this._f.length * 2, need));
864
1133
  bigger.set(this._f);
865
1134
  this._f = bigger;
866
1135
  this.geometry.instanceBuffer.data = bigger;
867
1136
  }
868
- const o = this._count * 40;
869
- this._f.fill(0, o, o + 40);
1137
+ const o = this._count * 44;
1138
+ this._f.fill(0, o, o + 44);
870
1139
  this._count++;
871
1140
  return o;
872
1141
  }
873
1142
  _commit() {
874
1143
  const geometry = this.geometry;
1144
+ this._version++;
875
1145
  geometry.instanceCount = this._count;
876
- geometry.instanceBuffer.update(Math.max(this._count, 1) * 40 * 4);
1146
+ geometry.instanceBuffer.update(Math.max(this._count, 1) * 44 * 4);
1147
+ if (this._polyDirty) {
1148
+ this._polyDirty = false;
1149
+ const src = this._polySource;
1150
+ const rows = Math.max(1, Math.ceil(this._polyCount / 256));
1151
+ if (src.resource !== this._poly || src.height !== rows) {
1152
+ src.resource = this._poly.subarray(0, rows * 256 * 4);
1153
+ src.resize(256, rows);
1154
+ }
1155
+ src.update();
1156
+ }
1157
+ }
1158
+ /** Appends the edges of every subpath of a filled path (each closed). Returns the first texel. */
1159
+ _pushEdges(subpaths) {
1160
+ let total = 0;
1161
+ for (const { pts } of subpaths) if (pts.length >= 6) total += pts.length >> 1;
1162
+ const start = this._reserve(total);
1163
+ const e = this._poly;
1164
+ let n = start;
1165
+ for (const { pts } of subpaths) {
1166
+ const m = pts.length >> 1;
1167
+ if (m < 3) continue;
1168
+ for (let i = 0; i < m; i++) {
1169
+ const j = (i + 1) % m;
1170
+ const q = n * 4;
1171
+ e[q] = pts[i * 2];
1172
+ e[q + 1] = pts[i * 2 + 1];
1173
+ e[q + 2] = pts[j * 2];
1174
+ e[q + 3] = pts[j * 2 + 1];
1175
+ n++;
1176
+ }
1177
+ }
1178
+ return [start, total];
877
1179
  }
878
1180
  _setBounds(o, minX, minY, maxX, maxY) {
879
1181
  const f = this._f;
@@ -992,94 +1294,128 @@ var SilkGraphics = class SilkGraphics extends Mesh {
992
1294
  };
993
1295
  }
994
1296
  }
1297
+ /** Effects shared by every primitive: spread, progressive blur and plus-lighter. Returns flags to add. */
1298
+ _fx(o, paint) {
1299
+ this._set4(o + 40, paint.spread, Math.max(paint.blurTo, 0), paint.blurAngle, 0);
1300
+ return (paint.blurTo >= 0 ? Flag.Progressive | Flag.Dither : 0) | (paint.add ? Flag.Additive : 0);
1301
+ }
1302
+ /** How far a paint reaches past the shape: three sigmas of its widest blur plus a positive spread. */
1303
+ static _reach(paint) {
1304
+ if (!paint) return 0;
1305
+ return 3 * Math.max(paint.blur, paint.blurTo) + Math.max(paint.spread, 0);
1306
+ }
995
1307
  _emitClosed(shape, fill, stroke) {
996
- if (fill && stroke && (fill.gradient && stroke.gradient || fill.blur !== stroke.blur)) {
1308
+ if (fill && stroke && (fill.gradient && stroke.gradient || fill.blur !== stroke.blur || fill.blurTo !== stroke.blurTo || fill.spread !== stroke.spread || fill.add !== stroke.add || fill.inner.length > 0)) {
997
1309
  this._emitClosed(shape, fill, null);
998
1310
  this._emitClosed(shape, null, stroke);
999
1311
  return;
1000
1312
  }
1001
- const f = this._f;
1313
+ const outline = stroke && stroke.join !== Join.Round ? sharpOutline(shape) : null;
1314
+ if (stroke && outline) {
1315
+ if (fill) this._emitClosed(shape, fill, null);
1316
+ const pts = stroke.align === 0 ? outline : offsetPolygon(outline, stroke.align * stroke.width / 2);
1317
+ this._emitStroke([{
1318
+ pts,
1319
+ closed: true
1320
+ }], stroke, this._frame(shape));
1321
+ return;
1322
+ }
1002
1323
  const o = this._alloc();
1003
- const sigma = Math.max(fill?.blur ?? 0, stroke?.blur ?? 0);
1324
+ const type = this._shapeParams(shape, o + 4);
1325
+ if (type === -1) {
1326
+ this._count--;
1327
+ return;
1328
+ }
1004
1329
  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;
1330
+ const b = this._box;
1331
+ if (shape.kind === "sector") {
1332
+ b[0] = b[1] = Infinity;
1333
+ b[2] = b[3] = -Infinity;
1334
+ arcExtent(shape.cx, shape.cy, shape.r1, shape.start, shape.sweep, b);
1335
+ if (shape.r0 > 0) arcExtent(shape.cx, shape.cy, shape.r0, shape.start, shape.sweep, b);
1336
+ else pointsBox([shape.cx, shape.cy], b);
1337
+ } else {
1338
+ b[0] = frame.x;
1339
+ b[1] = frame.y;
1340
+ b[2] = frame.x + frame.w;
1341
+ b[3] = frame.y + frame.h;
1013
1342
  }
1014
- if (sigma > 0) flags |= Flag.Dither;
1015
- const outset = (stroke ? sw / 2 * (1 + align) : 0) + 3 * sigma;
1016
- let type;
1343
+ this._paintClosed(o, type, frame, b, fill, stroke, shape.kind === "ellipse" ? shape : null);
1344
+ }
1345
+ /**
1346
+ * Writes the parameters of a closed shape (the instance's `aShape0..2`, 12 floats) at `at` and
1347
+ * returns its primitive id, or -1 when there is nothing to draw. Path fills push their edges.
1348
+ */
1349
+ _shapeParams(shape, at) {
1350
+ const f = this._f;
1351
+ const set = (i, a, b, c, d) => this._set4(at + i, a, b, c, d);
1017
1352
  switch (shape.kind) {
1018
1353
  case "rect": {
1019
1354
  const grow = 1 + shape.smoothing;
1020
1355
  const lim = Math.min(shape.hw, shape.hh);
1021
1356
  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;
1357
+ set(0, shape.cx, shape.cy, shape.hw, shape.hh);
1358
+ set(4, br, tr, bl, tl);
1359
+ set(8, squircleExponent(shape.smoothing), 0, 0, shape.rot);
1360
+ return Prim.Rect;
1027
1361
  }
1028
1362
  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;
1363
+ set(0, shape.cx, shape.cy, shape.rx, shape.ry);
1364
+ f[at + 11] = shape.rot;
1365
+ return Prim.Ellipse;
1033
1366
  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
- }
1367
+ set(0, shape.cx, shape.cy, shape.r1, shape.start);
1368
+ set(4, shape.sweep, shape.r0, shape.corner, 0);
1369
+ return Prim.Sector;
1370
+ case "heart":
1371
+ set(0, shape.cx, shape.cy, shape.size / (HEART_W + 2 * shape.round), shape.round);
1372
+ f[at + 11] = shape.rot;
1373
+ return Prim.Heart;
1045
1374
  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;
1375
+ set(0, shape.cx, shape.cy, shape.r, shape.rot);
1376
+ set(4, shape.n, shape.en, Math.min(shape.corner, shape.ri * .9), 0);
1377
+ return Prim.Star;
1050
1378
  case "triangle": {
1051
1379
  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;
1380
+ set(0, p[0], p[1], p[2], p[3]);
1381
+ set(4, p[4], p[5], p[6], 0);
1382
+ return Prim.Triangle;
1056
1383
  }
1057
- default:
1058
- this._count--;
1059
- return;
1384
+ case "path": {
1385
+ const [start, n] = this._pushEdges(shape.subpaths);
1386
+ if (n === 0) return -1;
1387
+ set(0, start, n, 0, 0);
1388
+ return Prim.Poly;
1389
+ }
1390
+ default: return -1;
1060
1391
  }
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;
1392
+ }
1393
+ /** Flags, bounds, colours, gradient, dashes and inner shadows of a closed-shape instance at `o`. */
1394
+ _paintClosed(o, type, frame, box, fill, stroke, ellipse) {
1395
+ const f = this._f;
1396
+ const paint = fill ?? stroke;
1397
+ const sigma = Math.max(fill?.blur ?? 0, stroke?.blur ?? 0);
1398
+ let flags = 0;
1399
+ let sw = 0;
1400
+ let align = 0;
1401
+ if (fill) flags |= Flag.Fill;
1402
+ if (stroke) {
1403
+ flags |= Flag.Stroke | stroke.cap << 8 | stroke.cap << 10;
1404
+ sw = stroke.width;
1405
+ align = stroke.align;
1073
1406
  }
1074
- this._setBounds(o, b[0] - outset, b[1] - outset, b[2] + outset, b[3] + outset);
1407
+ if (sigma > 0) flags |= Flag.Dither;
1408
+ flags |= this._fx(o, paint);
1409
+ const outset = (stroke ? sw / 2 * (1 + align) : 0) + SilkGraphics._reach(paint);
1410
+ this._setBounds(o, box[0] - outset, box[1] - outset, box[2] + outset, box[3] + outset);
1075
1411
  const gradient = fill?.gradient ?? stroke?.gradient ?? null;
1076
1412
  if (fill) this._premul(o + 20, fill.rgba);
1077
1413
  if (stroke) this._premul(o + 24, stroke.rgba);
1078
1414
  if (gradient) flags |= this._gradient(o, gradient, frame, !fill?.gradient);
1079
- if (stroke?.dash && shape.kind === "ellipse") {
1415
+ if (stroke?.dash && ellipse) {
1080
1416
  let [dash, gap] = stroke.dash;
1081
1417
  if (stroke.dashFit) {
1082
- const rm = (shape.rx + shape.ry) / 2 + align * sw / 2;
1418
+ const rm = (ellipse.rx + ellipse.ry) / 2 + align * sw / 2;
1083
1419
  const L = TAU * rm;
1084
1420
  const s = L / (Math.max(1, Math.round(L / (dash + gap))) * (dash + gap));
1085
1421
  dash *= s;
@@ -1089,6 +1425,168 @@ var SilkGraphics = class SilkGraphics extends Mesh {
1089
1425
  this._set4(o + 36, dash, gap, stroke.dashOffset, align);
1090
1426
  } else f[o + 39] = align;
1091
1427
  this._set4(o + 16, type, flags, sw, sigma);
1428
+ if (fill?.inner.length) this._emitInner(o, type, fill.inner, [
1429
+ box[0],
1430
+ box[1],
1431
+ box[2],
1432
+ box[3]
1433
+ ]);
1434
+ }
1435
+ /**
1436
+ * One instance for the closed shapes of `shapes` combined by `op` (an {@link OP} id): a
1437
+ * Prim.Union whose records the shader folds into one distance field. Overlaps are painted once
1438
+ * and the outline is exact. A union takes every subpath of a path on its own, as it always did.
1439
+ * The other operations take a path as one even-odd shape. Arcs and areas are skipped.
1440
+ */
1441
+ _emitOps(shapes, op, fill, stroke) {
1442
+ if (fill && stroke && (fill.gradient && stroke.gradient || fill.blur !== stroke.blur || fill.blurTo !== stroke.blurTo || fill.spread !== stroke.spread || fill.add !== stroke.add || fill.inner.length > 0)) {
1443
+ this._emitOps(shapes, op, fill, null);
1444
+ this._emitOps(shapes, op, null, stroke);
1445
+ return;
1446
+ }
1447
+ const records = [];
1448
+ const o = this._alloc();
1449
+ const at = o + 4;
1450
+ const depth = {
1451
+ now: 0,
1452
+ max: 0
1453
+ };
1454
+ const box = this._opRecords(shapes, op, op === OP.union, records, at, depth);
1455
+ if (!box || depth.max > OP_STACK) {
1456
+ if (box && !warnedOpDepth) {
1457
+ warnedOpDepth = true;
1458
+ console.warn(`[pixi-silk] shape operations nest deeper than ${OP_STACK} levels and are skipped`);
1459
+ }
1460
+ this._count--;
1461
+ return;
1462
+ }
1463
+ const start = this._pushTexels(records);
1464
+ const frame = {
1465
+ x: box[0],
1466
+ y: box[1],
1467
+ w: Math.max(box[2] - box[0], 0),
1468
+ h: Math.max(box[3] - box[1], 0)
1469
+ };
1470
+ this._f.fill(0, at, at + 12);
1471
+ this._set4(at, start, records.length / 16, 0, 0);
1472
+ this._paintClosed(o, Prim.Union, frame, box, fill, stroke, null);
1473
+ }
1474
+ /**
1475
+ * Appends the records (4 texels each) of `shapes` combined by `op`. The first operand is
1476
+ * pushed and each later one folds into it. A boolean shape pushes both its sides and pops them
1477
+ * with its own op. Returns the bounds of the result, or null when it has no closed shape.
1478
+ * `at` is scratch space for the parameters of one shape.
1479
+ */
1480
+ _opRecords(shapes, op, perSubpath, out, at, depth) {
1481
+ let box = null;
1482
+ const leaf = (rec, b) => {
1483
+ if (box) {
1484
+ rec[1] = op;
1485
+ box = opBox(op, box, b);
1486
+ } else {
1487
+ rec[1] = PUSH;
1488
+ depth.now++;
1489
+ depth.max = Math.max(depth.max, depth.now);
1490
+ box = [...b];
1491
+ }
1492
+ out.push(...rec);
1493
+ };
1494
+ for (const shape of shapes) {
1495
+ if (shape.kind === "boolean") {
1496
+ const mark = out.length;
1497
+ const before = depth.now;
1498
+ const ba = this._opRecords(shape.a, OP.union, false, out, at, depth);
1499
+ if (!ba) continue;
1500
+ const bb = this._opRecords(shape.b, OP.union, false, out, at, depth);
1501
+ let b = ba;
1502
+ if (bb) {
1503
+ out.push(POP, shape.op, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
1504
+ depth.now--;
1505
+ b = opBox(shape.op, ba, bb);
1506
+ } else if (shape.op === OP.intersect) {
1507
+ out.length = mark;
1508
+ depth.now = before;
1509
+ continue;
1510
+ }
1511
+ if (box) {
1512
+ out.push(POP, op, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
1513
+ depth.now--;
1514
+ box = opBox(op, box, b);
1515
+ } else box = [...b];
1516
+ continue;
1517
+ }
1518
+ if (shape.kind === "path") {
1519
+ const lists = perSubpath ? shape.subpaths.map((sp) => [sp]) : [shape.subpaths];
1520
+ for (const list of lists) {
1521
+ const [start, n] = this._pushEdges(list);
1522
+ if (n === 0) continue;
1523
+ const b = [
1524
+ Infinity,
1525
+ Infinity,
1526
+ -Infinity,
1527
+ -Infinity
1528
+ ];
1529
+ for (const sp of list) if (sp.pts.length >= 6) pointsBox(sp.pts, b);
1530
+ leaf([
1531
+ Prim.Poly,
1532
+ 0,
1533
+ 0,
1534
+ 0,
1535
+ start,
1536
+ n,
1537
+ 0,
1538
+ 0,
1539
+ 0,
1540
+ 0,
1541
+ 0,
1542
+ 0,
1543
+ 0,
1544
+ 0,
1545
+ 0,
1546
+ 0
1547
+ ], b);
1548
+ }
1549
+ continue;
1550
+ }
1551
+ if (!CLOSED_KINDS.has(shape.kind)) continue;
1552
+ const type = this._shapeParams(shape, at);
1553
+ if (type === -1) continue;
1554
+ const fr = this._frame(shape);
1555
+ leaf([
1556
+ type,
1557
+ 0,
1558
+ 0,
1559
+ 0,
1560
+ ...this._f.subarray(at, at + 12)
1561
+ ], [
1562
+ fr.x,
1563
+ fr.y,
1564
+ fr.x + fr.w,
1565
+ fr.y + fr.h
1566
+ ]);
1567
+ }
1568
+ return box;
1569
+ }
1570
+ /**
1571
+ * One instance per inner shadow: a copy of the shape at `o`, painted only inside it, from the
1572
+ * same distance field moved by the offset and shrunk by the spread.
1573
+ */
1574
+ _emitInner(o, type, shadows, box) {
1575
+ const m = this._m;
1576
+ const [x0, y0, x1, y1] = box;
1577
+ for (const s of shadows) {
1578
+ const q = this._alloc();
1579
+ this._f.copyWithin(q, o, o + 44);
1580
+ this._set4(q, x0, y0, x1, y1);
1581
+ this._premul(q + 20, s.rgba);
1582
+ this._set4(q + 24, m.a * s.x + m.c * s.y, m.b * s.x + m.d * s.y, 0, 0);
1583
+ this._set4(q + 28, 0, 0, 0, 0);
1584
+ this._set4(q + 32, 0, 0, 0, 0);
1585
+ this._set4(q + 36, 0, 0, 0, 0);
1586
+ this._set4(q + 40, s.spread, 0, 0, 0);
1587
+ const flags = Flag.Fill | Flag.Inner | (s.blur > 0 ? Flag.Dither : 0) | (s.add ? Flag.Additive : 0);
1588
+ this._set4(q + 16, type, flags, 0, s.blur);
1589
+ }
1092
1590
  }
1093
1591
  _emitArc(shape, stroke) {
1094
1592
  const o = this._alloc();
@@ -1102,7 +1600,7 @@ var SilkGraphics = class SilkGraphics extends Mesh {
1102
1600
  b[0] = b[1] = Infinity;
1103
1601
  b[2] = b[3] = -Infinity;
1104
1602
  arcExtent(shape.cx, shape.cy, shape.r, shape.start, shape.sweep, b);
1105
- const pad = hw + 3 * sigma;
1603
+ const pad = hw + SilkGraphics._reach(stroke);
1106
1604
  this._setBounds(o, b[0] - pad, b[1] - pad, b[2] + pad, b[3] + pad);
1107
1605
  this._premul(o + 24, stroke.rgba);
1108
1606
  if (stroke.gradient) flags |= this._gradient(o, stroke.gradient, this._frame(shape), true);
@@ -1117,7 +1615,7 @@ var SilkGraphics = class SilkGraphics extends Mesh {
1117
1615
  flags |= Flag.Dash;
1118
1616
  this._set4(o + 36, dash, gap, stroke.dashOffset, 0);
1119
1617
  }
1120
- this._set4(o + 16, Prim.Arc, flags, stroke.width, sigma);
1618
+ this._set4(o + 16, Prim.Arc, flags | this._fx(o, stroke), stroke.width, sigma);
1121
1619
  }
1122
1620
  _emitPath(raw, closed, stroke, frameOverride) {
1123
1621
  const { pts, keep } = cleanPoints(raw, closed);
@@ -1161,7 +1659,7 @@ var SilkGraphics = class SilkGraphics extends Mesh {
1161
1659
  radii[i] = (widths[0] + (widths[widths.length - 1] - widths[0]) * u) / 2;
1162
1660
  } else radii[i] = hw;
1163
1661
  const sigma = stroke.blur;
1164
- const pad = hw + 3 * sigma;
1662
+ const pad = hw + SilkGraphics._reach(stroke);
1165
1663
  let base = Flag.Stroke | stroke.cap << 8 | stroke.cap << 10;
1166
1664
  if (sigma > 0) base |= Flag.Dither;
1167
1665
  if (stroke.dash) base |= Flag.Dash;
@@ -1201,8 +1699,192 @@ var SilkGraphics = class SilkGraphics extends Mesh {
1201
1699
  if (stroke.dash) this._set4(o + 36, dash, gap, stroke.dashOffset, 0);
1202
1700
  this._f[o + 39] = radii[hasPrev ? ip : ia];
1203
1701
  this._f[o + 35] = radii[hasNext ? inx : ib];
1204
- this._set4(o + 16, Prim.Segment, flags, stroke.width, sigma);
1702
+ this._set4(o + 16, Prim.Segment, flags | this._fx(o, stroke), stroke.width, sigma);
1703
+ }
1704
+ }
1705
+ /** Makes room for `n` more texels in the path texture and returns the first one. */
1706
+ _reserve(n) {
1707
+ const start = this._polyCount;
1708
+ if ((start + n) * 4 > this._poly.length) {
1709
+ const rows = Math.ceil((start + n) / 256);
1710
+ const bigger = new Float32Array(Math.max(this._poly.length * 2, rows * 256 * 4));
1711
+ bigger.set(this._poly);
1712
+ this._poly = bigger;
1713
+ }
1714
+ this._polyCount = start + n;
1715
+ this._polyDirty = true;
1716
+ return start;
1717
+ }
1718
+ /** Appends raw texels (4 floats each) to the path texture. Returns the first texel. */
1719
+ _pushTexels(data) {
1720
+ const start = this._reserve(data.length / 4);
1721
+ this._poly.set(data, start * 4);
1722
+ return start;
1723
+ }
1724
+ /**
1725
+ * Strokes paths as one distance field (Prim.Path). Segments go to the path texture behind a
1726
+ * header per subpath, with their lengths along the path after them. Instances own windows of
1727
+ * segments: runs of short subpaths up to PATH_CHUNK segments in all, or chunks of PATH_CHUNK
1728
+ * segments of a long one. Each instance also lists the windows of the other instances that come
1729
+ * near its own. So every pixel of the stroke is shaded once, by the nearest segment, even where
1730
+ * the path crosses itself far along its length (see `pathSD` in the shader).
1731
+ */
1732
+ _emitStroke(subpaths, stroke, frameOverride) {
1733
+ const subs = [];
1734
+ const b = this._box;
1735
+ b[0] = b[1] = Infinity;
1736
+ b[2] = b[3] = -Infinity;
1737
+ for (const sp of subpaths) {
1738
+ const { pts } = cleanPoints(sp.pts, sp.closed);
1739
+ const n = pts.length / 2;
1740
+ if (n === 0) continue;
1741
+ const closed = sp.closed && n >= 3;
1742
+ const segs = n === 1 ? 1 : closed ? n : n - 1;
1743
+ const hdr = this._reserve(1 + segs + Math.ceil(segs / 4));
1744
+ const e = this._poly;
1745
+ const cum = [0];
1746
+ for (let i = 0; i < segs; i++) {
1747
+ const ia = i * 2;
1748
+ const ib = n === 1 ? 0 : (i + 1) % n * 2;
1749
+ const q = (hdr + 1 + i) * 4;
1750
+ e[q] = pts[ia];
1751
+ e[q + 1] = pts[ia + 1];
1752
+ e[q + 2] = pts[ib];
1753
+ e[q + 3] = pts[ib + 1];
1754
+ cum.push(cum[i] + Math.hypot(pts[ib] - pts[ia], pts[ib + 1] - pts[ia + 1]));
1755
+ }
1756
+ const at = (hdr + 1 + segs) * 4;
1757
+ for (let i = 0; i < Math.ceil(segs / 4) * 4; i++) e[at + i] = i < segs ? cum[i] : 0;
1758
+ let dashScale = 1;
1759
+ if (stroke.dash && closed && stroke.dashFit && cum[segs] > 0) {
1760
+ const period = stroke.dash[0] + stroke.dash[1];
1761
+ dashScale = cum[segs] / (Math.max(1, Math.round(cum[segs] / period)) * period);
1762
+ }
1763
+ this._set4Poly(hdr, segs, closed ? 1 : 0, cum[segs], dashScale);
1764
+ pointsBox(pts, b);
1765
+ subs.push({
1766
+ pts,
1767
+ closed,
1768
+ segs,
1769
+ hdr,
1770
+ cum
1771
+ });
1205
1772
  }
1773
+ if (!subs.length) return;
1774
+ const ctx = {
1775
+ stroke,
1776
+ frame: frameOverride ?? {
1777
+ x: b[0],
1778
+ y: b[1],
1779
+ w: b[2] - b[0],
1780
+ h: b[3] - b[1]
1781
+ },
1782
+ row: -1,
1783
+ gradFlags: 0
1784
+ };
1785
+ const owned = [];
1786
+ let run = [];
1787
+ let runSegs = 0;
1788
+ const flush = () => {
1789
+ if (run.length) owned.push(run);
1790
+ run = [];
1791
+ runSegs = 0;
1792
+ };
1793
+ for (const sp of subs) {
1794
+ if (sp.segs > 32) {
1795
+ flush();
1796
+ for (let s = 0; s < sp.segs; s += 32) owned.push([{
1797
+ sub: sp,
1798
+ s,
1799
+ count: Math.min(32, sp.segs - s)
1800
+ }]);
1801
+ continue;
1802
+ }
1803
+ if (runSegs + sp.segs > 32) flush();
1804
+ run.push({
1805
+ sub: sp,
1806
+ s: 0,
1807
+ count: sp.segs
1808
+ });
1809
+ runSegs += sp.segs;
1810
+ }
1811
+ flush();
1812
+ const others = nearWindows(owned, strokeRadius(stroke));
1813
+ owned.forEach((wins, key) => {
1814
+ this._pathInstance(wins, others[key], key, ctx);
1815
+ });
1816
+ }
1817
+ _set4Poly(t, a, b, c, d) {
1818
+ const e = this._poly;
1819
+ e[t * 4] = a;
1820
+ e[t * 4 + 1] = b;
1821
+ e[t * 4 + 2] = c;
1822
+ e[t * 4 + 3] = d;
1823
+ }
1824
+ /**
1825
+ * One Prim.Path instance. Its window texels (header texel, first segment, count, owner key)
1826
+ * list the windows it owns, then the windows of other instances near them.
1827
+ */
1828
+ _pathInstance(own, near, key, ctx) {
1829
+ const { stroke, frame } = ctx;
1830
+ const win = this._reserve(own.length + near.length);
1831
+ own.forEach((w, i) => {
1832
+ this._set4Poly(win + i, w.sub.hdr, w.s, w.count, key);
1833
+ });
1834
+ near.forEach((w, i) => {
1835
+ this._set4Poly(win + own.length + i, w.sub.hdr, w.s, w.count, w.key);
1836
+ });
1837
+ const o = this._alloc();
1838
+ const hw = stroke.width / 2;
1839
+ const ext = hw * (stroke.cap === Cap.Square ? Math.SQRT2 : 1);
1840
+ const b = [
1841
+ Infinity,
1842
+ Infinity,
1843
+ -Infinity,
1844
+ -Infinity
1845
+ ];
1846
+ const e = this._poly;
1847
+ for (const { sub: sp, s, count } of own) {
1848
+ const tex = sp.hdr + 1;
1849
+ for (let i = s; i < s + count; i++) {
1850
+ const q = (tex + i) * 4;
1851
+ pointsBox([
1852
+ e[q],
1853
+ e[q + 1],
1854
+ e[q + 2],
1855
+ e[q + 3]
1856
+ ], b);
1857
+ if (stroke.join === Join.Miter && (sp.closed || i > 0)) {
1858
+ const p = (tex + (i - 1 + sp.segs) % sp.segs) * 4;
1859
+ const l0 = Math.hypot(e[p + 2] - e[p], e[p + 3] - e[p + 1]) || 1;
1860
+ const l1 = Math.hypot(e[q + 2] - e[q], e[q + 3] - e[q + 1]) || 1;
1861
+ const poly = joinPolygon(e[q], e[q + 1], (e[p + 2] - e[p]) / l0, (e[p + 3] - e[p + 1]) / l0, (e[q + 2] - e[q]) / l1, (e[q + 3] - e[q + 1]) / l1, hw, stroke.join, stroke.miterLimit);
1862
+ if (poly) pointsBox(poly, b);
1863
+ }
1864
+ }
1865
+ }
1866
+ const pad = ext + SilkGraphics._reach(stroke);
1867
+ let flags = Flag.Stroke | stroke.cap << 8 | stroke.cap << 10;
1868
+ if (stroke.blur > 0) flags |= Flag.Dither;
1869
+ this._setBounds(o, b[0] - pad, b[1] - pad, b[2] + pad, b[3] + pad);
1870
+ this._set4(o + 4, win, own.length, near.length, key);
1871
+ this._set4(o + 8, stroke.join, stroke.miterLimit, 0, 0);
1872
+ this._premul(o + 24, stroke.rgba);
1873
+ if (stroke.gradient) {
1874
+ if (ctx.row < 0) {
1875
+ ctx.gradFlags = this._gradient(o, stroke.gradient, frame, true);
1876
+ ctx.row = this._f[o + 33];
1877
+ } else {
1878
+ stroke.gradient.resolve(frame, this._f, o + 28);
1879
+ this._set4(o + 32, stroke.gradient.kind, ctx.row, EXTEND[stroke.gradient.extend] ?? Extend.Pad, 0);
1880
+ }
1881
+ flags |= ctx.gradFlags;
1882
+ }
1883
+ if (stroke.dash) {
1884
+ flags |= Flag.Dash;
1885
+ this._set4(o + 36, stroke.dash[0], stroke.dash[1], stroke.dashOffset, 0);
1886
+ }
1887
+ this._set4(o + 16, Prim.Path, flags | this._fx(o, stroke), stroke.width, stroke.blur);
1206
1888
  }
1207
1889
  _emitArea(shape, fill) {
1208
1890
  const { top, bottom } = shape;
@@ -1210,7 +1892,7 @@ var SilkGraphics = class SilkGraphics extends Mesh {
1210
1892
  if (n < 2) return;
1211
1893
  const frame = this._frame(shape);
1212
1894
  const sigma = fill.blur;
1213
- const pad = 3 * sigma;
1895
+ const pad = SilkGraphics._reach(fill);
1214
1896
  const y0 = frame.y - pad;
1215
1897
  const y1 = frame.y + frame.h + pad;
1216
1898
  let base = Flag.Fill;
@@ -1245,7 +1927,7 @@ var SilkGraphics = class SilkGraphics extends Mesh {
1245
1927
  }
1246
1928
  flags |= gradFlags;
1247
1929
  }
1248
- this._set4(o + 16, Prim.Area, flags, 0, sigma);
1930
+ this._set4(o + 16, Prim.Area, flags | this._fx(o, fill), 0, sigma);
1249
1931
  }
1250
1932
  }
1251
1933
  };
@@ -1278,5 +1960,85 @@ function insetTriangle(p, r) {
1278
1960
  rr
1279
1961
  ];
1280
1962
  }
1963
+ /**
1964
+ * Corner points of a shape whose corners are sharp (a rect without radius, a triangle or star
1965
+ * without corner rounding), for strokes with miter or bevel joins. Null for every other shape.
1966
+ */
1967
+ function sharpOutline(shape) {
1968
+ if (shape.kind === "rect") {
1969
+ if (shape.r.some((r) => r > 0)) return null;
1970
+ const c = Math.cos(shape.rot);
1971
+ const s = Math.sin(shape.rot);
1972
+ const out = [];
1973
+ for (const [x, y] of [
1974
+ [-shape.hw, -shape.hh],
1975
+ [shape.hw, -shape.hh],
1976
+ [shape.hw, shape.hh],
1977
+ [-shape.hw, shape.hh]
1978
+ ]) out.push(shape.cx + c * x - s * y, shape.cy + s * x + c * y);
1979
+ return out;
1980
+ }
1981
+ if (shape.kind === "triangle") return shape.corner > 0 ? null : [...shape.pts];
1982
+ if (shape.kind === "star") {
1983
+ if (shape.corner > 0) return null;
1984
+ const an = Math.PI / shape.n;
1985
+ const regular = Math.abs(shape.ri - shape.r * Math.cos(an)) < 1e-6 * shape.r;
1986
+ const out = [];
1987
+ for (let i = 0; i < shape.n; i++) {
1988
+ const a = shape.rot + i * 2 * an;
1989
+ out.push(shape.cx + shape.r * Math.sin(a), shape.cy - shape.r * Math.cos(a));
1990
+ if (!regular) out.push(shape.cx + shape.ri * Math.sin(a + an), shape.cy - shape.ri * Math.cos(a + an));
1991
+ }
1992
+ return out;
1993
+ }
1994
+ return null;
1995
+ }
1996
+ /** Moves every edge of a closed polygon outwards by `d` (inwards when negative), keeping sharp corners. */
1997
+ function offsetPolygon(pts, d) {
1998
+ const n = pts.length >> 1;
1999
+ let area = 0;
2000
+ for (let i = 0; i < n; i++) {
2001
+ const j = (i + 1) % n;
2002
+ area += pts[i * 2] * pts[j * 2 + 1] - pts[j * 2] * pts[i * 2 + 1];
2003
+ }
2004
+ const sg = area > 0 ? 1 : -1;
2005
+ const normal = (i, j) => {
2006
+ const tx = pts[j * 2] - pts[i * 2];
2007
+ const ty = pts[j * 2 + 1] - pts[i * 2 + 1];
2008
+ const len = Math.hypot(tx, ty) || 1;
2009
+ return [sg * ty / len, -sg * tx / len];
2010
+ };
2011
+ const out = [];
2012
+ for (let i = 0; i < n; i++) {
2013
+ const [ax, ay] = normal((i - 1 + n) % n, i);
2014
+ const [bx, by] = normal(i, (i + 1) % n);
2015
+ const k = d / Math.max(1 + ax * bx + ay * by, 1e-6);
2016
+ out.push(pts[i * 2] + (ax + bx) * k, pts[i * 2 + 1] + (ay + by) * k);
2017
+ }
2018
+ return out;
2019
+ }
2020
+ /**
2021
+ * How far a stroke paints from its centre line, local units: half its width (more for square caps
2022
+ * and miters), its blur and spread, and one unit for the anti-aliasing ramp.
2023
+ */
2024
+ function strokeRadius(stroke) {
2025
+ return stroke.width / 2 * Math.max(stroke.cap === Cap.Square ? Math.SQRT2 : 1, stroke.join === Join.Miter ? stroke.miterLimit : 1) + 3 * Math.max(stroke.blur, stroke.blurTo) + Math.max(stroke.spread, 0) + 1;
2026
+ }
2027
+ /** Bounds of `a op b` for boxes [minX, minY, maxX, maxY]: the first box for a subtraction, the overlap for an intersection. */
2028
+ function opBox(op, a, b) {
2029
+ if (op === OP.subtract) return a;
2030
+ if (op === OP.intersect) return [
2031
+ Math.max(a[0], b[0]),
2032
+ Math.max(a[1], b[1]),
2033
+ Math.min(a[2], b[2]),
2034
+ Math.min(a[3], b[3])
2035
+ ];
2036
+ return [
2037
+ Math.min(a[0], b[0]),
2038
+ Math.min(a[1], b[1]),
2039
+ Math.max(a[2], b[2]),
2040
+ Math.max(a[3], b[3])
2041
+ ];
2042
+ }
1281
2043
  //#endregion
1282
2044
  export { SilkGeometry, SilkGraphics };