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
package/dist/color.js CHANGED
@@ -2,7 +2,10 @@ import { Color } from "pixi.js";
2
2
  //#region src/color.ts
3
3
  let scratch;
4
4
  const cache = /* @__PURE__ */ new Map();
5
- /** Any Pixi `ColorSource` as straight sRGB `[r, g, b, a]` (0..1), with `alpha` multiplied in. Memoised. */
5
+ /**
6
+ * Converts any Pixi `ColorSource` to straight sRGB `[r, g, b, a]`, channels 0..1, with `alpha`
7
+ * multiplied in. Returns a new array. Numbers and strings are cached, up to 1024 of them.
8
+ */
6
9
  function parseColor(value, alpha = 1) {
7
10
  const cacheable = typeof value === "number" || typeof value === "string";
8
11
  let base = cacheable ? cache.get(value) : void 0;
@@ -27,7 +30,9 @@ function parseColor(value, alpha = 1) {
27
30
  base[3] * alpha
28
31
  ];
29
32
  }
33
+ /** sRGB transfer function, encoded 0..1 to linear light 0..1. */
30
34
  const srgbToLinear = (c) => c <= .04045 ? c / 12.92 : ((c + .055) / 1.055) ** 2.4;
35
+ /** Inverse sRGB transfer function, linear light 0..1 to encoded 0..1. */
31
36
  const linearToSrgb = (c) => c <= .0031308 ? 12.92 * c : 1.055 * c ** (1 / 2.4) - .055;
32
37
  /** Converts linear-light sRGB to OKLab (Björn Ottosson's perceptual colour space), used to interpolate gradients evenly. */
33
38
  function linearToOklab(r, g, b) {
@@ -54,7 +59,7 @@ function oklabToLinear(L, a, b) {
54
59
  -.0041960863 * l3 - .7034186147 * m3 + 1.707614701 * s3
55
60
  ];
56
61
  }
57
- /** sRGB (straight) -> working space coordinates. */
62
+ /** Converts straight sRGB to coordinates in `space`. Alpha is dropped. */
58
63
  function toSpace(rgb, space) {
59
64
  if (space === "srgb") return [
60
65
  rgb[0],
@@ -71,7 +76,7 @@ function toSpace(rgb, space) {
71
76
  ];
72
77
  return linearToOklab(lr, lg, lb);
73
78
  }
74
- /** Working space coordinates -> sRGB (straight, clamped to gamut). */
79
+ /** Converts coordinates in `space` back to straight sRGB, clamped to 0..1. */
75
80
  function fromSpace(c, space) {
76
81
  const clamp = (v) => v < 0 ? 0 : v > 1 ? 1 : v;
77
82
  if (space === "srgb") return [
@@ -88,7 +93,7 @@ function fromSpace(c, space) {
88
93
  }
89
94
  const f32 = /* @__PURE__ */ new Float32Array(1);
90
95
  const u32 = new Uint32Array(f32.buffer);
91
- /** Float -> IEEE 754 half float bits (round to nearest). */
96
+ /** Encodes a number as IEEE 754 half float bits, rounded to nearest. Overflow gives infinity. */
92
97
  function toHalf(value) {
93
98
  f32[0] = value;
94
99
  const x = u32[0];
package/dist/constants.js CHANGED
@@ -10,7 +10,8 @@ const INSTANCE_ATTRIBUTES = [
10
10
  "aStroke",
11
11
  "aGrad",
12
12
  "aPaint",
13
- "aDash"
13
+ "aDash",
14
+ "aFx"
14
15
  ];
15
16
  /** Primitive ids, one per branch of the fragment shader. */
16
17
  const Prim = {
@@ -22,7 +23,17 @@ const Prim = {
22
23
  Area: 5,
23
24
  Heart: 6,
24
25
  Star: 7,
25
- Triangle: 8
26
+ Triangle: 8,
27
+ /** Filled path: its edges live in a float texture, `aShape0.xy` = first texel and edge count. */
28
+ Poly: 9,
29
+ /**
30
+ * Stroked path, one distance field for the whole outline: header, segment and length texels live
31
+ * in the path texture. `aShape0` = first window texel, windows the instance owns, windows of
32
+ * other instances near them, and the instance's key. `aShape1` = join, miter limit.
33
+ */
34
+ Path: 10,
35
+ /** Union of closed shapes: `aShape0.xy` = first record texel and record count (4 texels each). */
36
+ Union: 11
26
37
  };
27
38
  /** Bit flags in `aMeta.y`. */
28
39
  const Flag = {
@@ -35,7 +46,13 @@ const Flag = {
35
46
  EdgeLeft: 64,
36
47
  EdgeRight: 128,
37
48
  GradStroke: 4096,
38
- Closed: 8192
49
+ Closed: 8192,
50
+ /** Plus-lighter: the colour adds to what is underneath (written with zero alpha). */
51
+ Additive: 16384,
52
+ /** Inner shadow of the shape. Offset in `aStroke.xy`, spread in `aFx.x`. */
53
+ Inner: 32768,
54
+ /** Blur goes from `aMeta.w` to `aFx.y` along the angle `aFx.z`. */
55
+ Progressive: 65536
39
56
  };
40
57
  /** Line cap ids (two bits per end, see CAP0_SHIFT / CAP1_SHIFT). */
41
58
  const Cap = {
@@ -43,13 +60,21 @@ const Cap = {
43
60
  Round: 1,
44
61
  Square: 2
45
62
  };
63
+ /** Line join ids in `aShape1.x` of a {@link Prim.Path} instance. */
64
+ const Join = {
65
+ Round: 0,
66
+ Miter: 1,
67
+ Bevel: 2
68
+ };
46
69
  /** Gradient ids in `aPaint.x`. */
47
70
  const GradKind = {
48
71
  None: 0,
49
72
  Linear: 1,
50
73
  Radial: 2,
51
74
  Conic: 3,
52
- Path: 4
75
+ Path: 4,
76
+ /** Smooth colour mesh: a 16 x 16 grid baked into one atlas row. */
77
+ Mesh: 5
53
78
  };
54
79
  /** Gradient extend-mode ids in `aPaint.z`. */
55
80
  const Extend = {
@@ -57,6 +82,7 @@ const Extend = {
57
82
  Repeat: 1,
58
83
  Reflect: 2
59
84
  };
85
+ /** One full turn in radians. */
60
86
  const TAU = Math.PI * 2;
61
87
  //#endregion
62
- export { Cap, Extend, Flag, GradKind, INSTANCE_ATTRIBUTES, Prim, TAU };
88
+ export { Cap, Extend, Flag, GradKind, INSTANCE_ATTRIBUTES, Join, Prim, TAU };
package/dist/curves.d.ts CHANGED
@@ -2,14 +2,20 @@ import { PointData } from "pixi.js";
2
2
  //#region src/curves.d.ts
3
3
  /** Points as a flat `[x0, y0, x1, y1, ...]` array, a typed array, or an array of `{ x, y }` objects. */
4
4
  export type PointsInput = number[] | Float32Array | Float64Array | PointData[];
5
- /** Normalises `[x0, y0, x1, y1, ...]` or `[{x, y}, ...]` into a flat number array. */
5
+ /** Copies any {@link PointsInput} into a new flat `[x0, y0, x1, y1, ...]` array. */
6
6
  export declare function toFlat(points: PointsInput): number[];
7
7
  /**
8
8
  * Monotone cubic interpolation in x (Steffen's method, same family as d3's monotoneX).
9
9
  * Never overshoots the data: peaks stay at the data points, flat runs stay flat.
10
- * `step` is the target spacing of the output samples in local units.
10
+ * `step` is the target spacing of the output samples in local units, at most 64 per segment.
11
+ * Points must be sorted by x. Fewer than 3 points come back as a copy.
11
12
  */
12
13
  export declare function monotoneX(flat: number[], step?: number): number[];
13
- /** Centripetal Catmull-Rom spline through the points (no cusps or self-loops). */
14
+ /**
15
+ * Samples a centripetal Catmull-Rom spline through flat points. It has no cusps or self-loops
16
+ * within a segment. `closed` adds the segment back to the first point. `step` is the target sample
17
+ * spacing in local units, at most 64 per segment. `alpha` 0.5 is centripetal (0 uniform, 1
18
+ * chordal). Fewer than 3 points come back as a copy.
19
+ */
14
20
  export declare function catmullRom(flat: number[], closed?: boolean, step?: number, alpha?: number): number[];
15
21
  //#endregion
package/dist/curves.js CHANGED
@@ -1,5 +1,5 @@
1
1
  //#region src/curves.ts
2
- /** Normalises `[x0, y0, x1, y1, ...]` or `[{x, y}, ...]` into a flat number array. */
2
+ /** Copies any {@link PointsInput} into a new flat `[x0, y0, x1, y1, ...]` array. */
3
3
  function toFlat(points) {
4
4
  if (points.length === 0) return [];
5
5
  if (typeof points[0] === "number") return Array.from(points);
@@ -10,7 +10,8 @@ function toFlat(points) {
10
10
  /**
11
11
  * Monotone cubic interpolation in x (Steffen's method, same family as d3's monotoneX).
12
12
  * Never overshoots the data: peaks stay at the data points, flat runs stay flat.
13
- * `step` is the target spacing of the output samples in local units.
13
+ * `step` is the target spacing of the output samples in local units, at most 64 per segment.
14
+ * Points must be sorted by x. Fewer than 3 points come back as a copy.
14
15
  */
15
16
  function monotoneX(flat, step = 2) {
16
17
  const n = flat.length / 2;
@@ -47,7 +48,12 @@ function monotoneX(flat, step = 2) {
47
48
  }
48
49
  return out;
49
50
  }
50
- /** Centripetal Catmull-Rom spline through the points (no cusps or self-loops). */
51
+ /**
52
+ * Samples a centripetal Catmull-Rom spline through flat points. It has no cusps or self-loops
53
+ * within a segment. `closed` adds the segment back to the first point. `step` is the target sample
54
+ * spacing in local units, at most 64 per segment. `alpha` 0.5 is centripetal (0 uniform, 1
55
+ * chordal). Fewer than 3 points come back as a copy.
56
+ */
51
57
  function catmullRom(flat, closed = false, step = 2, alpha = .5) {
52
58
  const n = flat.length / 2;
53
59
  if (n < 3) return flat.slice();
@@ -89,7 +95,7 @@ function catmullRom(flat, closed = false, step = 2, alpha = .5) {
89
95
  if (closed) out.splice(out.length - 2, 2);
90
96
  return out;
91
97
  }
92
- /** Flattens a cubic bezier (start point excluded) into `out`. */
98
+ /** Appends a cubic Bezier to `out` as 2 to 256 points, about one per `step` units of its control polygon. The start point is not added. */
93
99
  function flattenCubic(out, x0, y0, c1x, c1y, c2x, c2y, x1, y1, step = 2) {
94
100
  const len = Math.hypot(c1x - x0, c1y - y0) + Math.hypot(c2x - c1x, c2y - c1y) + Math.hypot(x1 - c2x, y1 - c2y);
95
101
  const count = Math.max(2, Math.min(256, Math.ceil(len / step)));
@@ -103,7 +109,7 @@ function flattenCubic(out, x0, y0, c1x, c1y, c2x, c2y, x1, y1, step = 2) {
103
109
  out.push(a * x0 + b * c1x + c * c2x + d * x1, a * y0 + b * c1y + c * c2y + d * y1);
104
110
  }
105
111
  }
106
- /** Flattens a quadratic bezier (start point excluded) into `out`. */
112
+ /** Appends a quadratic Bezier to `out` as 2 to 256 points, about one per `step` units of its control polygon. The start point is not added. */
107
113
  function flattenQuadratic(out, x0, y0, cx, cy, x1, y1, step = 2) {
108
114
  const len = Math.hypot(cx - x0, cy - y0) + Math.hypot(x1 - cx, y1 - cy);
109
115
  const count = Math.max(2, Math.min(256, Math.ceil(len / step)));
@@ -0,0 +1,62 @@
1
+ import { GlassStyle } from "./options.js";
2
+ import { Container } from "pixi.js";
3
+ //#region src/glass/Glass.d.ts
4
+ /** Options of {@link Glass}: the pane size plus every {@link GlassStyle} field. */
5
+ export interface GlassOptions extends GlassStyle {
6
+ /** Pane width in local units. Default 240. */
7
+ width?: number;
8
+ /** Pane height in local units. Default 160. */
9
+ height?: number;
10
+ }
11
+ /**
12
+ * A pane of frosted glass from `pixi-silk/glass`. It blurs and bends whatever is drawn behind it,
13
+ * splits colours at its curved rim, and catches light on the edge that faces the light.
14
+ *
15
+ * Put labels and icons in {@link Glass.content}. They sit on top of the glass, unblurred. The pane's
16
+ * top-left corner is at (0, 0). It only sees what was drawn before it in the same frame, so add it
17
+ * after its backdrop. You can rotate and scale it. The light stays fixed on screen, and under an uneven
18
+ * scale the corners keep the radius of the smaller scale. Skew is not supported.
19
+ *
20
+ * On WebGL the pane reads the canvas through the renderer's back buffer. It sets
21
+ * `renderer.backBuffer.useBackBuffer = true` the first time it draws and shows no glass on that one
22
+ * frame. Pass `useBackBuffer: true` to the application to have glass from the first frame. The back
23
+ * buffer costs a full-screen copy each frame for the whole app. WebGPU reads the backdrop without a
24
+ * back buffer. Any other renderer gets a plain frosted fill instead.
25
+ *
26
+ * ```ts
27
+ * const pane = new Glass({ width: 320, height: 200, radius: 32 });
28
+ * pane.content.addChild(label);
29
+ * app.stage.addChild(backdrop, pane);
30
+ * ```
31
+ */
32
+ export declare class Glass extends Container {
33
+ /** Children drawn on top of the glass, unfiltered, in the pane's local space. */
34
+ readonly content: Container;
35
+ private readonly _shadow;
36
+ private readonly _body;
37
+ private readonly _fill;
38
+ private readonly _filter;
39
+ private _style;
40
+ private _r;
41
+ private _w;
42
+ private _h;
43
+ private _fallback;
44
+ constructor(options?: GlassOptions);
45
+ /** Pane width in local units. Change it with {@link Glass.resize}. */
46
+ get paneWidth(): number;
47
+ /** Pane height in local units. Change it with {@link Glass.resize}. */
48
+ get paneHeight(): number;
49
+ /** The style as given, before defaults. Change it with {@link Glass.set}. */
50
+ get style(): Readonly<GlassStyle>;
51
+ /** Resizes the pane, in local units, and redraws its outline and shadow. Returns this. */
52
+ resize(width: number, height: number): this;
53
+ /**
54
+ * Merges `style` into the current style. Fields left out keep their value. Only a change of
55
+ * radius, smoothing, shadow or opacity redraws the outline (frost and tint too in the fallback fill).
56
+ * The rest goes to the filter on the next frame. Returns this.
57
+ */
58
+ set(style: GlassStyle): this;
59
+ private _redraw;
60
+ private _sync;
61
+ }
62
+ //#endregion
@@ -0,0 +1,135 @@
1
+ import { SilkGraphics } from "../SilkGraphics.js";
2
+ import { GlassFilter } from "./GlassFilter.js";
3
+ import { lightVector, resolveGlass } from "./options.js";
4
+ import { Container, Rectangle } from "pixi.js";
5
+ //#region src/glass/Glass.ts
6
+ /**
7
+ * A pane of frosted glass from `pixi-silk/glass`. It blurs and bends whatever is drawn behind it,
8
+ * splits colours at its curved rim, and catches light on the edge that faces the light.
9
+ *
10
+ * Put labels and icons in {@link Glass.content}. They sit on top of the glass, unblurred. The pane's
11
+ * top-left corner is at (0, 0). It only sees what was drawn before it in the same frame, so add it
12
+ * after its backdrop. You can rotate and scale it. The light stays fixed on screen, and under an uneven
13
+ * scale the corners keep the radius of the smaller scale. Skew is not supported.
14
+ *
15
+ * On WebGL the pane reads the canvas through the renderer's back buffer. It sets
16
+ * `renderer.backBuffer.useBackBuffer = true` the first time it draws and shows no glass on that one
17
+ * frame. Pass `useBackBuffer: true` to the application to have glass from the first frame. The back
18
+ * buffer costs a full-screen copy each frame for the whole app. WebGPU reads the backdrop without a
19
+ * back buffer. Any other renderer gets a plain frosted fill instead.
20
+ *
21
+ * ```ts
22
+ * const pane = new Glass({ width: 320, height: 200, radius: 32 });
23
+ * pane.content.addChild(label);
24
+ * app.stage.addChild(backdrop, pane);
25
+ * ```
26
+ */
27
+ var Glass = class extends Container {
28
+ /** Children drawn on top of the glass, unfiltered, in the pane's local space. */
29
+ content = new Container({ label: "GlassContent" });
30
+ _shadow = new SilkGraphics({ label: "GlassShadow" });
31
+ _body = new Container({ label: "GlassBody" });
32
+ _fill = new SilkGraphics({ label: "GlassFill" });
33
+ _filter;
34
+ _style;
35
+ _r;
36
+ _w;
37
+ _h;
38
+ _fallback = false;
39
+ constructor(options = {}) {
40
+ super({ label: "Glass" });
41
+ const { width = 240, height = 160, ...style } = options;
42
+ this._w = width;
43
+ this._h = height;
44
+ this._style = style;
45
+ this._r = resolveGlass(style);
46
+ this._filter = new GlassFilter();
47
+ this._body.filters = [this._filter];
48
+ this._body.addChild(this._fill);
49
+ this.addChild(this._shadow, this._body, this.content);
50
+ this.onRender = (renderer) => this._sync(renderer);
51
+ this._redraw();
52
+ }
53
+ /** Pane width in local units. Change it with {@link Glass.resize}. */
54
+ get paneWidth() {
55
+ return this._w;
56
+ }
57
+ /** Pane height in local units. Change it with {@link Glass.resize}. */
58
+ get paneHeight() {
59
+ return this._h;
60
+ }
61
+ /** The style as given, before defaults. Change it with {@link Glass.set}. */
62
+ get style() {
63
+ return this._style;
64
+ }
65
+ /** Resizes the pane, in local units, and redraws its outline and shadow. Returns this. */
66
+ resize(width, height) {
67
+ this._w = width;
68
+ this._h = height;
69
+ this._redraw();
70
+ return this;
71
+ }
72
+ /**
73
+ * Merges `style` into the current style. Fields left out keep their value. Only a change of
74
+ * radius, smoothing, shadow or opacity redraws the outline (frost and tint too in the fallback fill).
75
+ * The rest goes to the filter on the next frame. Returns this.
76
+ */
77
+ set(style) {
78
+ const before = this._r;
79
+ this._style = {
80
+ ...this._style,
81
+ ...style
82
+ };
83
+ this._r = resolveGlass(this._style);
84
+ const r = this._r;
85
+ if (r.radius !== before.radius || r.smoothing !== before.smoothing || r.shadow !== before.shadow || r.opacity !== before.opacity || this._fallback && (r.frost !== before.frost || r.tint.some((v, i) => v !== before.tint[i]))) this._redraw();
86
+ return this;
87
+ }
88
+ _redraw() {
89
+ const r = this._r;
90
+ const w = this._w;
91
+ const h = this._h;
92
+ this._body.filterArea = new Rectangle(0, 0, w, h);
93
+ this._fill.clear().roundRect(0, 0, w, h, r.radius, r.smoothing);
94
+ this._fill.fill(this._fallback ? {
95
+ color: r.tint,
96
+ alpha: .18 + r.frost
97
+ } : {
98
+ color: 16777215,
99
+ alpha: .002
100
+ });
101
+ this._shadow.clear();
102
+ if (r.shadow > 0) this._shadow.roundRect(4, 14, w - 8, h - 6, r.radius, r.smoothing).fill({
103
+ color: 0,
104
+ alpha: r.shadow * r.opacity,
105
+ blur: Math.max(8, Math.min(w, h) * .12)
106
+ });
107
+ }
108
+ _sync(renderer) {
109
+ const filter = this._filter;
110
+ const backBuffer = renderer.backBuffer;
111
+ if (!(!!filter && (backBuffer !== void 0 || renderer.name === "webgpu")) && !this._fallback) {
112
+ this._fallback = true;
113
+ this._body.filters = [];
114
+ this._redraw();
115
+ return;
116
+ }
117
+ if (!filter || this._fallback) return;
118
+ if (backBuffer && !backBuffer.useBackBuffer) backBuffer.useBackBuffer = true;
119
+ filter.enabled = !(backBuffer && renderer.renderTarget.rootRenderTarget?.isRoot);
120
+ const m = this._body.worldTransform;
121
+ const kx = Math.hypot(m.a, m.b);
122
+ const ky = Math.hypot(m.c, m.d);
123
+ const k = Math.min(kx, ky);
124
+ const r = this._r;
125
+ const [lx, ly] = lightVector(r.lightAngle);
126
+ filter.setShape(this._w * kx / 2, this._h * ky / 2, r.radius * k, r.smoothing);
127
+ filter.setRotation(Math.atan2(m.b, m.a));
128
+ filter.setOptics(r.blur * k, r.refraction * k, r.dispersion, r.bevel * k);
129
+ filter.setTint(r.tint[0], r.tint[1], r.tint[2], r.tintAmount);
130
+ filter.setLight(lx, ly, r.light, r.grain);
131
+ filter.setSurface(r.frost, r.opacity, r.saturation, 0);
132
+ }
133
+ };
134
+ //#endregion
135
+ export { Glass };
@@ -0,0 +1,33 @@
1
+ import { Filter } from "pixi.js";
2
+ //#region src/glass/GlassFilter.d.ts
3
+ /**
4
+ * The frosted glass filter behind {@link Glass}. Prefer {@link Glass}: it sizes the filter, keeps it
5
+ * in step with the transform and turns on the WebGL back buffer the filter needs.
6
+ *
7
+ * Using it alone, set `filterArea` on the filtered container to the glass rectangle. Pass every
8
+ * distance in screen pixels, so multiply by the world scale yourself, and pass the world rotation
9
+ * to {@link GlassFilter.setRotation}. The filter reads the backdrop
10
+ * (`blendRequired`), so on WebGL it needs `useBackBuffer: true` on the renderer. Each setter
11
+ * uploads its uniform only when a value changes.
12
+ */
13
+ export declare class GlassFilter extends Filter {
14
+ private readonly _u;
15
+ constructor();
16
+ /** Half width, half height and corner radius in screen pixels, and squircle smoothing 0 to 1. Defaults 50, 50, 16, 0. */
17
+ setShape(halfWidth: number, halfHeight: number, radius: number, smoothing: number): void;
18
+ /** Blur radius, refraction distance and bevel width in screen pixels, and dispersion 0 to 1. Defaults 16, 12, 0.3, 18. */
19
+ setOptics(blur: number, refraction: number, dispersion: number, bevel: number): void;
20
+ /** Tint colour as straight sRGB 0 to 1 per channel, and how much of it to mix in, 0 to 1. Default white at 0.04. */
21
+ setTint(r: number, g: number, b: number, amount: number): void;
22
+ /** Unit vector towards the light (see {@link lightVector}), light strength and grain amount. Defaults top left, 0.8, 0.035. */
23
+ setLight(x: number, y: number, strength: number, grain: number): void;
24
+ /** Frost 0 to 1, opacity 0 to 1, saturation (1 keeps colours) and a grain seed. Defaults 0.06, 1, 1.15, 0. */
25
+ setSurface(frost: number, opacity: number, saturation: number, seed: number): void;
26
+ /**
27
+ * Rotation of the pane on screen in radians, clockwise as Pixi's `rotation`. The shape, its rim
28
+ * and the refraction follow it. The light stays fixed on screen. Default 0.
29
+ */
30
+ setRotation(angle: number): void;
31
+ private _set;
32
+ }
33
+ //#endregion
@@ -0,0 +1,264 @@
1
+ import { glassWgsl } from "./glass-wgsl.js";
2
+ import { Filter, GlProgram, GpuProgram, Texture, UniformGroup } from "pixi.js";
3
+ //#region src/glass/GlassFilter.ts
4
+ /** @internal Filter vertex stage: Pixi's default filter vertex shader. */
5
+ const glassVertex = `#version 300 es
6
+ in vec2 aPosition;
7
+ out vec2 vTextureCoord;
8
+ uniform vec4 uInputSize;
9
+ uniform vec4 uOutputFrame;
10
+ uniform vec4 uOutputTexture;
11
+ void main(void)
12
+ {
13
+ vec2 position = aPosition * uOutputFrame.zw + uOutputFrame.xy;
14
+ position.x = position.x * (2.0 / uOutputTexture.x) - 1.0;
15
+ position.y = position.y * (2.0 * uOutputTexture.z / uOutputTexture.y) - uOutputTexture.z;
16
+ gl_Position = vec4(position, 0.0, 1.0);
17
+ vTextureCoord = aPosition * (uOutputFrame.zw * uInputSize.zw);
18
+ }`;
19
+ /**
20
+ * @internal Glass fragment stage. The filter covers exactly the glass shape and reads what is
21
+ * already drawn behind it (`uBackTexture`). It blurs that with a jittered golden-angle disc. Near
22
+ * the rim it bends it along the normal of the shape's distance field and splits the channels.
23
+ * Frost, tint, a sheen, a lit rim and grain go on top. Coverage is analytic.
24
+ */
25
+ const glassFragment = `#version 300 es
26
+ precision highp float;
27
+ in vec2 vTextureCoord;
28
+ out vec4 finalColor;
29
+ uniform sampler2D uTexture;
30
+ uniform sampler2D uBackTexture;
31
+ uniform vec4 uInputSize;
32
+ uniform vec4 uInputClamp;
33
+ uniform vec4 uOutputFrame;
34
+ uniform vec4 uShape;
35
+ uniform vec4 uOptics;
36
+ uniform vec4 uTint;
37
+ uniform vec4 uLight;
38
+ uniform vec4 uSurface;
39
+ uniform vec4 uFrame;
40
+ float hash(vec2 p)
41
+ {
42
+ return fract(52.9829189 * fract(dot(p, vec2(0.06711056, 0.00583715))));
43
+ }
44
+ float sdShape(vec2 p)
45
+ {
46
+ vec2 b = uShape.xy;
47
+ float r = min(uShape.z, min(b.x, b.y));
48
+ vec2 q = abs(p) - b + r;
49
+ vec2 m = max(q, 0.0);
50
+ float k = mix(2.0, 5.0, clamp(uShape.w, 0.0, 1.0));
51
+ float corner = pow(pow(m.x, k) + pow(m.y, k), 1.0 / k);
52
+ return corner + min(max(q.x, q.y), 0.0) - r;
53
+ }
54
+ vec3 backAt(vec2 uv)
55
+ {
56
+ return texture(uBackTexture, clamp(uv, uInputClamp.xy, uInputClamp.zw)).rgb;
57
+ }
58
+ vec3 frosted(vec2 uv, float radius, float spin)
59
+ {
60
+ if (radius < 0.5) return backAt(uv);
61
+ vec3 acc = vec3(0.0);
62
+ float total = 0.0;
63
+ for (int i = 0; i < 24; i++)
64
+ {
65
+ float f = float(i);
66
+ float r = sqrt((f + 0.5) / 24.0);
67
+ float a = f * 2.39996323 + spin;
68
+ vec2 o = vec2(cos(a), sin(a)) * r * radius * uInputSize.zw;
69
+ float w = exp(-r * r * 1.6);
70
+ acc += backAt(uv + o) * w;
71
+ total += w;
72
+ }
73
+ return acc / total;
74
+ }
75
+ void main(void)
76
+ {
77
+ vec2 size = uOutputFrame.zw;
78
+ vec2 p = vTextureCoord * uInputSize.xy - size * 0.5;
79
+ vec2 axis = uFrame.xy;
80
+ vec2 q = vec2(axis.x * p.x + axis.y * p.y, axis.x * p.y - axis.y * p.x);
81
+ float d = sdShape(q);
82
+ float aa = max(fwidth(d), 1e-3);
83
+ float cover = clamp(0.5 - d / aa, 0.0, 1.0);
84
+ if (cover <= 0.0)
85
+ {
86
+ finalColor = vec4(0.0);
87
+ return;
88
+ }
89
+ vec2 e = vec2(0.75, 0.0);
90
+ vec2 nq = vec2(sdShape(q + e.xy) - sdShape(q - e.xy), sdShape(q + e.yx) - sdShape(q - e.yx));
91
+ vec2 n = vec2(axis.x * nq.x - axis.y * nq.y, axis.y * nq.x + axis.x * nq.y);
92
+ n = n / max(length(n), 1e-5);
93
+ float bevel = max(uOptics.w, 1.0);
94
+ float rim = 1.0 - smoothstep(0.0, bevel, -d);
95
+ float bend = rim * rim;
96
+ vec2 shift = n * uOptics.y * bend * uInputSize.zw;
97
+ float spin = hash(gl_FragCoord.xy) * 6.2831853;
98
+ float blur = uOptics.x;
99
+ vec3 col;
100
+ if (uOptics.z > 0.001)
101
+ {
102
+ float split = uOptics.z * bend;
103
+ col.r = frosted(vTextureCoord + shift * (1.0 + split), blur, spin).r;
104
+ col.g = frosted(vTextureCoord + shift, blur, spin).g;
105
+ col.b = frosted(vTextureCoord + shift * (1.0 - split), blur, spin).b;
106
+ }
107
+ else
108
+ {
109
+ col = frosted(vTextureCoord + shift, blur, spin);
110
+ }
111
+ float luma = dot(col, vec3(0.2126, 0.7152, 0.0722));
112
+ col = max(mix(vec3(luma), col, uSurface.z), 0.0);
113
+ col = mix(col, vec3(1.0), uSurface.x);
114
+ col = mix(col, uTint.rgb, uTint.a);
115
+ vec2 toLight = uLight.xy;
116
+ float span = max(uShape.x, uShape.y);
117
+ float sheen = clamp(dot(p / span, toLight) * 0.5 + 0.5, 0.0, 1.0);
118
+ float facing = max(dot(n, toLight), 0.0);
119
+ float hairline = exp(-abs(d + 0.9) * 1.1);
120
+ col += uLight.z * (0.10 * sheen * sheen + 0.55 * pow(rim, 3.0) * facing + 0.35 * hairline * (0.35 + 0.65 * facing));
121
+ col += (hash(gl_FragCoord.xy + uSurface.w) - 0.5) * uLight.w;
122
+ finalColor = vec4(col, 1.0) * cover * uSurface.y;
123
+ }`;
124
+ let program = null;
125
+ let gpuProgram = null;
126
+ /**
127
+ * The frosted glass filter behind {@link Glass}. Prefer {@link Glass}: it sizes the filter, keeps it
128
+ * in step with the transform and turns on the WebGL back buffer the filter needs.
129
+ *
130
+ * Using it alone, set `filterArea` on the filtered container to the glass rectangle. Pass every
131
+ * distance in screen pixels, so multiply by the world scale yourself, and pass the world rotation
132
+ * to {@link GlassFilter.setRotation}. The filter reads the backdrop
133
+ * (`blendRequired`), so on WebGL it needs `useBackBuffer: true` on the renderer. Each setter
134
+ * uploads its uniform only when a value changes.
135
+ */
136
+ var GlassFilter = class extends Filter {
137
+ _u;
138
+ constructor() {
139
+ program ??= GlProgram.from({
140
+ vertex: glassVertex,
141
+ fragment: glassFragment,
142
+ name: "silk-glass",
143
+ preferredFragmentPrecision: "highp"
144
+ });
145
+ gpuProgram ??= GpuProgram.from({
146
+ name: "silk-glass",
147
+ vertex: {
148
+ source: glassWgsl,
149
+ entryPoint: "mainVertex"
150
+ },
151
+ fragment: {
152
+ source: glassWgsl,
153
+ entryPoint: "mainFragment"
154
+ }
155
+ });
156
+ const u = new UniformGroup({
157
+ uShape: {
158
+ value: new Float32Array([
159
+ 50,
160
+ 50,
161
+ 16,
162
+ 0
163
+ ]),
164
+ type: "vec4<f32>"
165
+ },
166
+ uOptics: {
167
+ value: new Float32Array([
168
+ 16,
169
+ 12,
170
+ .3,
171
+ 18
172
+ ]),
173
+ type: "vec4<f32>"
174
+ },
175
+ uTint: {
176
+ value: new Float32Array([
177
+ 1,
178
+ 1,
179
+ 1,
180
+ .04
181
+ ]),
182
+ type: "vec4<f32>"
183
+ },
184
+ uLight: {
185
+ value: new Float32Array([
186
+ -Math.SQRT1_2,
187
+ -Math.SQRT1_2,
188
+ .8,
189
+ .035
190
+ ]),
191
+ type: "vec4<f32>"
192
+ },
193
+ uSurface: {
194
+ value: new Float32Array([
195
+ .06,
196
+ 1,
197
+ 1.15,
198
+ 0
199
+ ]),
200
+ type: "vec4<f32>"
201
+ },
202
+ uFrame: {
203
+ value: new Float32Array([
204
+ 1,
205
+ 0,
206
+ 0,
207
+ 0
208
+ ]),
209
+ type: "vec4<f32>"
210
+ }
211
+ });
212
+ super({
213
+ glProgram: program,
214
+ gpuProgram,
215
+ resources: {
216
+ glassUniforms: u,
217
+ uBackTexture: Texture.EMPTY
218
+ },
219
+ blendRequired: true,
220
+ clipToViewport: false,
221
+ padding: 0,
222
+ resolution: "inherit"
223
+ });
224
+ this._u = u;
225
+ }
226
+ /** Half width, half height and corner radius in screen pixels, and squircle smoothing 0 to 1. Defaults 50, 50, 16, 0. */
227
+ setShape(halfWidth, halfHeight, radius, smoothing) {
228
+ this._set("uShape", halfWidth, halfHeight, radius, smoothing);
229
+ }
230
+ /** Blur radius, refraction distance and bevel width in screen pixels, and dispersion 0 to 1. Defaults 16, 12, 0.3, 18. */
231
+ setOptics(blur, refraction, dispersion, bevel) {
232
+ this._set("uOptics", blur, refraction, dispersion, bevel);
233
+ }
234
+ /** Tint colour as straight sRGB 0 to 1 per channel, and how much of it to mix in, 0 to 1. Default white at 0.04. */
235
+ setTint(r, g, b, amount) {
236
+ this._set("uTint", r, g, b, amount);
237
+ }
238
+ /** Unit vector towards the light (see {@link lightVector}), light strength and grain amount. Defaults top left, 0.8, 0.035. */
239
+ setLight(x, y, strength, grain) {
240
+ this._set("uLight", x, y, strength, grain);
241
+ }
242
+ /** Frost 0 to 1, opacity 0 to 1, saturation (1 keeps colours) and a grain seed. Defaults 0.06, 1, 1.15, 0. */
243
+ setSurface(frost, opacity, saturation, seed) {
244
+ this._set("uSurface", frost, opacity, saturation, seed);
245
+ }
246
+ /**
247
+ * Rotation of the pane on screen in radians, clockwise as Pixi's `rotation`. The shape, its rim
248
+ * and the refraction follow it. The light stays fixed on screen. Default 0.
249
+ */
250
+ setRotation(angle) {
251
+ this._set("uFrame", Math.cos(angle), Math.sin(angle), 0, 0);
252
+ }
253
+ _set(name, a, b, c, d) {
254
+ const v = this._u.uniforms[name];
255
+ if (v[0] === a && v[1] === b && v[2] === c && v[3] === d) return;
256
+ v[0] = a;
257
+ v[1] = b;
258
+ v[2] = c;
259
+ v[3] = d;
260
+ this._u.update();
261
+ }
262
+ };
263
+ //#endregion
264
+ export { GlassFilter, glassFragment, glassVertex };