@arraypress/effects-astro 0.0.0-stage → 0.1.1

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 (314) hide show
  1. package/AGENTS.md +85 -0
  2. package/COMPONENTS.md +1264 -0
  3. package/LICENSE +139 -0
  4. package/README.md +197 -2
  5. package/package.json +60 -4
  6. package/src/components/AsciiRain.astro +36 -0
  7. package/src/components/BorderSweep.astro +39 -0
  8. package/src/components/Caustics.astro +39 -0
  9. package/src/components/ColorPanels.astro +62 -0
  10. package/src/components/Cursor.astro +97 -0
  11. package/src/components/DotGrid.astro +34 -0
  12. package/src/components/DotSurface.astro +35 -0
  13. package/src/components/EffectCanvas.astro +152 -0
  14. package/src/components/FitText.astro +35 -0
  15. package/src/components/FlutedGlass.astro +36 -0
  16. package/src/components/Glass.astro +69 -0
  17. package/src/components/GlassGlyph.astro +38 -0
  18. package/src/components/GlowField.astro +177 -0
  19. package/src/components/Grain.astro +98 -0
  20. package/src/components/HaloRing.astro +38 -0
  21. package/src/components/HighlightSweep.astro +74 -0
  22. package/src/components/ImageRipple.astro +56 -0
  23. package/src/components/Ink.astro +37 -0
  24. package/src/components/InkField.astro +45 -0
  25. package/src/components/IsoCity.astro +54 -0
  26. package/src/components/LightField.astro +40 -0
  27. package/src/components/LiquidMetal.astro +41 -0
  28. package/src/components/Magnetic.astro +52 -0
  29. package/src/components/Marquee.astro +216 -0
  30. package/src/components/MosaicReveal.astro +196 -0
  31. package/src/components/NebulaLogo.astro +38 -0
  32. package/src/components/NeonSwarm.astro +38 -0
  33. package/src/components/Ocean.astro +40 -0
  34. package/src/components/OutlineWordmark.astro +222 -0
  35. package/src/components/PaperBackground.astro +40 -0
  36. package/src/components/PaperShader.astro +55 -0
  37. package/src/components/Parallax.astro +49 -0
  38. package/src/components/ParticleLogo.astro +37 -0
  39. package/src/components/PixelGlitchText.astro +70 -0
  40. package/src/components/PointCloud.astro +37 -0
  41. package/src/components/ProductStack.astro +298 -0
  42. package/src/components/PulseMark.astro +189 -0
  43. package/src/components/RadiantGlass.astro +38 -0
  44. package/src/components/RainOnGlass.astro +71 -0
  45. package/src/components/Reveal.astro +115 -0
  46. package/src/components/ScrambleText.astro +52 -0
  47. package/src/components/ScrollReveal.astro +38 -0
  48. package/src/components/ShaderBackground.astro +55 -0
  49. package/src/components/SignalLines.astro +41 -0
  50. package/src/components/SilkRibbon.astro +37 -0
  51. package/src/components/SpotlightText.astro +131 -0
  52. package/src/components/Tilt.astro +79 -0
  53. package/src/components/Topography.astro +38 -0
  54. package/src/components/Typewriter.astro +66 -0
  55. package/src/components/VariableType.astro +59 -0
  56. package/src/components/VoxelLogo.astro +46 -0
  57. package/src/components/WaveBars.astro +41 -0
  58. package/src/components/WorldRoutes.astro +41 -0
  59. package/src/components/style.js +19 -0
  60. package/src/index.d.ts +143 -0
  61. package/src/index.js +105 -0
  62. package/src/runtime/ascii-rain.d.ts +53 -0
  63. package/src/runtime/ascii-rain.js +355 -0
  64. package/src/runtime/audio-meter.d.ts +65 -0
  65. package/src/runtime/audio-meter.js +282 -0
  66. package/src/runtime/audio.d.ts +26 -0
  67. package/src/runtime/audio.js +453 -0
  68. package/src/runtime/blur.d.ts +2 -0
  69. package/src/runtime/blur.js +56 -0
  70. package/src/runtime/canvas2d.d.ts +23 -0
  71. package/src/runtime/canvas2d.js +105 -0
  72. package/src/runtime/caustics.d.ts +40 -0
  73. package/src/runtime/caustics.js +286 -0
  74. package/src/runtime/color.d.ts +33 -0
  75. package/src/runtime/color.js +213 -0
  76. package/src/runtime/curl.d.ts +5 -0
  77. package/src/runtime/curl.js +81 -0
  78. package/src/runtime/cursor.d.ts +9 -0
  79. package/src/runtime/cursor.js +80 -0
  80. package/src/runtime/device.d.ts +14 -0
  81. package/src/runtime/device.js +73 -0
  82. package/src/runtime/dot-grid.d.ts +27 -0
  83. package/src/runtime/dot-grid.js +245 -0
  84. package/src/runtime/dot-surface.d.ts +46 -0
  85. package/src/runtime/dot-surface.js +429 -0
  86. package/src/runtime/feedback.d.ts +18 -0
  87. package/src/runtime/feedback.js +131 -0
  88. package/src/runtime/fit-text.d.ts +5 -0
  89. package/src/runtime/fit-text.js +60 -0
  90. package/src/runtime/fluid.d.ts +55 -0
  91. package/src/runtime/fluid.js +504 -0
  92. package/src/runtime/fluted-glass.d.ts +27 -0
  93. package/src/runtime/fluted-glass.js +157 -0
  94. package/src/runtime/gl.d.ts +19 -0
  95. package/src/runtime/gl.js +104 -0
  96. package/src/runtime/glass-glyph.d.ts +49 -0
  97. package/src/runtime/glass-glyph.js +355 -0
  98. package/src/runtime/glass-sheen.d.ts +2 -0
  99. package/src/runtime/glass-sheen.js +30 -0
  100. package/src/runtime/glsl.d.ts +17 -0
  101. package/src/runtime/glsl.js +78 -0
  102. package/src/runtime/glyphs.d.ts +22 -0
  103. package/src/runtime/glyphs.js +107 -0
  104. package/src/runtime/halo-ring.d.ts +47 -0
  105. package/src/runtime/halo-ring.js +334 -0
  106. package/src/runtime/height-field.d.ts +50 -0
  107. package/src/runtime/height-field.js +176 -0
  108. package/src/runtime/image-ripple.d.ts +25 -0
  109. package/src/runtime/image-ripple.js +353 -0
  110. package/src/runtime/image.d.ts +11 -0
  111. package/src/runtime/image.js +81 -0
  112. package/src/runtime/in-view.d.ts +6 -0
  113. package/src/runtime/in-view.js +36 -0
  114. package/src/runtime/ink-field/shaders.js +258 -0
  115. package/src/runtime/ink-field/sim.js +582 -0
  116. package/src/runtime/ink-field.d.ts +39 -0
  117. package/src/runtime/ink-field.js +311 -0
  118. package/src/runtime/ink.d.ts +31 -0
  119. package/src/runtime/ink.js +109 -0
  120. package/src/runtime/instancing.d.ts +12 -0
  121. package/src/runtime/instancing.js +37 -0
  122. package/src/runtime/iso-camera.d.ts +35 -0
  123. package/src/runtime/iso-camera.js +85 -0
  124. package/src/runtime/iso-city/figures.d.ts +30 -0
  125. package/src/runtime/iso-city/figures.js +136 -0
  126. package/src/runtime/iso-city/look.d.ts +34 -0
  127. package/src/runtime/iso-city/look.js +105 -0
  128. package/src/runtime/iso-city/plan.d.ts +59 -0
  129. package/src/runtime/iso-city/plan.js +463 -0
  130. package/src/runtime/iso-city/roads.d.ts +64 -0
  131. package/src/runtime/iso-city/roads.js +320 -0
  132. package/src/runtime/iso-city/scene.d.ts +27 -0
  133. package/src/runtime/iso-city/scene.js +432 -0
  134. package/src/runtime/iso-city/shaders.d.ts +6 -0
  135. package/src/runtime/iso-city/shaders.js +233 -0
  136. package/src/runtime/iso-city/shadows.d.ts +13 -0
  137. package/src/runtime/iso-city/shadows.js +148 -0
  138. package/src/runtime/iso-city/traffic.d.ts +80 -0
  139. package/src/runtime/iso-city/traffic.js +457 -0
  140. package/src/runtime/iso-city.d.ts +37 -0
  141. package/src/runtime/iso-city.js +458 -0
  142. package/src/runtime/lens-map.d.ts +11 -0
  143. package/src/runtime/lens-map.js +146 -0
  144. package/src/runtime/light-field.d.ts +29 -0
  145. package/src/runtime/light-field.js +292 -0
  146. package/src/runtime/liquid-glass.d.ts +48 -0
  147. package/src/runtime/liquid-glass.js +423 -0
  148. package/src/runtime/liquid-metal/beads.js +217 -0
  149. package/src/runtime/liquid-metal/field.js +88 -0
  150. package/src/runtime/liquid-metal/look.js +64 -0
  151. package/src/runtime/liquid-metal/shaders.js +255 -0
  152. package/src/runtime/liquid-metal/wave.js +53 -0
  153. package/src/runtime/liquid-metal.d.ts +46 -0
  154. package/src/runtime/liquid-metal.js +366 -0
  155. package/src/runtime/loop.d.ts +7 -0
  156. package/src/runtime/loop.js +112 -0
  157. package/src/runtime/magnetic.d.ts +9 -0
  158. package/src/runtime/magnetic.js +46 -0
  159. package/src/runtime/marquee.d.ts +2 -0
  160. package/src/runtime/marquee.js +27 -0
  161. package/src/runtime/mesh.d.ts +30 -0
  162. package/src/runtime/mesh.js +217 -0
  163. package/src/runtime/mosaic-reveal.d.ts +34 -0
  164. package/src/runtime/mosaic-reveal.js +504 -0
  165. package/src/runtime/motion.d.ts +2 -0
  166. package/src/runtime/motion.js +21 -0
  167. package/src/runtime/mount.d.ts +13 -0
  168. package/src/runtime/mount.js +120 -0
  169. package/src/runtime/nebula-logo.d.ts +31 -0
  170. package/src/runtime/nebula-logo.js +282 -0
  171. package/src/runtime/neon-swarm.d.ts +31 -0
  172. package/src/runtime/neon-swarm.js +258 -0
  173. package/src/runtime/ocean/presets.d.ts +54 -0
  174. package/src/runtime/ocean/presets.js +184 -0
  175. package/src/runtime/ocean/shader.d.ts +14 -0
  176. package/src/runtime/ocean/shader.js +389 -0
  177. package/src/runtime/ocean/view.d.ts +24 -0
  178. package/src/runtime/ocean/view.js +98 -0
  179. package/src/runtime/ocean/waves.d.ts +29 -0
  180. package/src/runtime/ocean/waves.js +119 -0
  181. package/src/runtime/ocean.d.ts +31 -0
  182. package/src/runtime/ocean.js +173 -0
  183. package/src/runtime/palettes.d.ts +37 -0
  184. package/src/runtime/palettes.js +75 -0
  185. package/src/runtime/paper-look.d.ts +42 -0
  186. package/src/runtime/paper-look.js +91 -0
  187. package/src/runtime/paper-looks.d.ts +17 -0
  188. package/src/runtime/paper-looks.js +21 -0
  189. package/src/runtime/paper.d.ts +37 -0
  190. package/src/runtime/paper.js +303 -0
  191. package/src/runtime/particle-logo.d.ts +31 -0
  192. package/src/runtime/particle-logo.js +196 -0
  193. package/src/runtime/ping-pong.d.ts +43 -0
  194. package/src/runtime/ping-pong.js +175 -0
  195. package/src/runtime/pixel-glitch.d.ts +17 -0
  196. package/src/runtime/pixel-glitch.js +287 -0
  197. package/src/runtime/point-cloud.d.ts +33 -0
  198. package/src/runtime/point-cloud.js +287 -0
  199. package/src/runtime/pointer.d.ts +27 -0
  200. package/src/runtime/pointer.js +128 -0
  201. package/src/runtime/points.d.ts +25 -0
  202. package/src/runtime/points.js +130 -0
  203. package/src/runtime/pools.d.ts +2 -0
  204. package/src/runtime/pools.js +25 -0
  205. package/src/runtime/product-stack.d.ts +24 -0
  206. package/src/runtime/product-stack.js +210 -0
  207. package/src/runtime/radiant-glass.d.ts +29 -0
  208. package/src/runtime/radiant-glass.js +282 -0
  209. package/src/runtime/rain-on-glass/backdrop.d.ts +22 -0
  210. package/src/runtime/rain-on-glass/backdrop.js +185 -0
  211. package/src/runtime/rain-on-glass/shader.d.ts +2 -0
  212. package/src/runtime/rain-on-glass/shader.js +168 -0
  213. package/src/runtime/rain-on-glass/wipe.d.ts +16 -0
  214. package/src/runtime/rain-on-glass/wipe.js +65 -0
  215. package/src/runtime/rain-on-glass.d.ts +27 -0
  216. package/src/runtime/rain-on-glass.js +210 -0
  217. package/src/runtime/ramp.d.ts +2 -0
  218. package/src/runtime/ramp.js +30 -0
  219. package/src/runtime/random.d.ts +2 -0
  220. package/src/runtime/random.js +22 -0
  221. package/src/runtime/scroll-reveal.d.ts +22 -0
  222. package/src/runtime/scroll-reveal.js +73 -0
  223. package/src/runtime/shader-background.d.ts +42 -0
  224. package/src/runtime/shader-background.js +261 -0
  225. package/src/runtime/shader-backgrounds/band-lens.js +72 -0
  226. package/src/runtime/shader-backgrounds/common.js +53 -0
  227. package/src/runtime/shader-backgrounds/grain-gradient.js +87 -0
  228. package/src/runtime/shader-backgrounds/grid.js +33 -0
  229. package/src/runtime/shader-backgrounds/mesh-gradient.js +74 -0
  230. package/src/runtime/shader-backgrounds/silk.js +57 -0
  231. package/src/runtime/shader-backgrounds/waves.js +31 -0
  232. package/src/runtime/shape.d.ts +42 -0
  233. package/src/runtime/shape.js +313 -0
  234. package/src/runtime/silk-ribbon.d.ts +23 -0
  235. package/src/runtime/silk-ribbon.js +206 -0
  236. package/src/runtime/slideshow.d.ts +27 -0
  237. package/src/runtime/slideshow.js +234 -0
  238. package/src/runtime/spotlight.d.ts +9 -0
  239. package/src/runtime/spotlight.js +97 -0
  240. package/src/runtime/stack-poses.d.ts +53 -0
  241. package/src/runtime/stack-poses.js +273 -0
  242. package/src/runtime/stage.d.ts +47 -0
  243. package/src/runtime/stage.js +187 -0
  244. package/src/runtime/swarm.d.ts +49 -0
  245. package/src/runtime/swarm.js +199 -0
  246. package/src/runtime/text-motion.d.ts +41 -0
  247. package/src/runtime/text-motion.js +165 -0
  248. package/src/runtime/text.d.ts +11 -0
  249. package/src/runtime/text.js +90 -0
  250. package/src/runtime/tilt.d.ts +19 -0
  251. package/src/runtime/tilt.js +116 -0
  252. package/src/runtime/topography.d.ts +71 -0
  253. package/src/runtime/topography.js +650 -0
  254. package/src/runtime/trackers.d.ts +21 -0
  255. package/src/runtime/trackers.js +76 -0
  256. package/src/runtime/variable-type.d.ts +43 -0
  257. package/src/runtime/variable-type.js +358 -0
  258. package/src/runtime/voxel-logo.d.ts +31 -0
  259. package/src/runtime/voxel-logo.js +237 -0
  260. package/src/runtime/waves.d.ts +70 -0
  261. package/src/runtime/waves.js +799 -0
  262. package/src/runtime/world-labels.d.ts +29 -0
  263. package/src/runtime/world-labels.js +59 -0
  264. package/src/runtime/world-land.d.ts +8 -0
  265. package/src/runtime/world-land.js +105 -0
  266. package/src/runtime/world-map.d.ts +69 -0
  267. package/src/runtime/world-map.js +308 -0
  268. package/src/runtime/world-plan.d.ts +101 -0
  269. package/src/runtime/world-plan.js +333 -0
  270. package/src/runtime/world-routes.d.ts +75 -0
  271. package/src/runtime/world-routes.js +673 -0
  272. package/src/styles/border-sweep.css +82 -0
  273. package/src/styles/glass.css +104 -0
  274. package/src/styles/motion.css +218 -0
  275. package/src/styles/reveal.css +51 -0
  276. package/src/vendor/paper/LICENSE +201 -0
  277. package/src/vendor/paper/NOTICE +5 -0
  278. package/src/vendor/paper/README.md +8 -0
  279. package/src/vendor/paper/get-shader-color-from-string.js +100 -0
  280. package/src/vendor/paper/get-shader-noise-texture.js +17 -0
  281. package/src/vendor/paper/presets.js +3678 -0
  282. package/src/vendor/paper/shader-sizing.js +37 -0
  283. package/src/vendor/paper/shader-utils.js +130 -0
  284. package/src/vendor/paper/shaders/color-panels.js +223 -0
  285. package/src/vendor/paper/shaders/dithering.js +270 -0
  286. package/src/vendor/paper/shaders/dot-grid.js +109 -0
  287. package/src/vendor/paper/shaders/dot-orbit.js +122 -0
  288. package/src/vendor/paper/shaders/fluted-glass.js +360 -0
  289. package/src/vendor/paper/shaders/gem-smoke.js +572 -0
  290. package/src/vendor/paper/shaders/god-rays.js +132 -0
  291. package/src/vendor/paper/shaders/grain-gradient.js +300 -0
  292. package/src/vendor/paper/shaders/halftone-cmyk.js +294 -0
  293. package/src/vendor/paper/shaders/halftone-dots.js +320 -0
  294. package/src/vendor/paper/shaders/heatmap.js +383 -0
  295. package/src/vendor/paper/shaders/image-dithering.js +198 -0
  296. package/src/vendor/paper/shaders/lens-distortion.js +275 -0
  297. package/src/vendor/paper/shaders/liquid-metal.js +632 -0
  298. package/src/vendor/paper/shaders/mesh-gradient.js +130 -0
  299. package/src/vendor/paper/shaders/metaballs.js +110 -0
  300. package/src/vendor/paper/shaders/neuro-noise.js +76 -0
  301. package/src/vendor/paper/shaders/paper-texture.js +526 -0
  302. package/src/vendor/paper/shaders/perlin-noise.js +166 -0
  303. package/src/vendor/paper/shaders/pulsing-border.js +238 -0
  304. package/src/vendor/paper/shaders/simplex-noise.js +91 -0
  305. package/src/vendor/paper/shaders/smoke-ring.js +132 -0
  306. package/src/vendor/paper/shaders/spiral.js +82 -0
  307. package/src/vendor/paper/shaders/static-mesh-gradient.js +130 -0
  308. package/src/vendor/paper/shaders/static-radial-gradient.js +187 -0
  309. package/src/vendor/paper/shaders/swirl.js +103 -0
  310. package/src/vendor/paper/shaders/voronoi.js +138 -0
  311. package/src/vendor/paper/shaders/warp.js +129 -0
  312. package/src/vendor/paper/shaders/water.js +121 -0
  313. package/src/vendor/paper/shaders/waves.js +65 -0
  314. package/src/vendor/paper/vertex-shader.js +159 -0
@@ -0,0 +1,423 @@
1
+ /**
2
+ * @module @arraypress/effects-astro/liquid-glass
3
+ *
4
+ * Real refraction for `.glass` panels (glass.css): the live page behind a
5
+ * panel is bent as if through a thick lens, hardest at the rim, with the
6
+ * colours splitting there and a bright highlight along the edge. Text
7
+ * behind stays live and links stay clickable, since nothing is copied: an
8
+ * SVG filter reaches the page through `backdrop-filter: url(#…)`.
9
+ *
10
+ * Each panel's filter is built from its lens map (lens-map.js) for its
11
+ * size and corner radius: `feImage` holds the map, `feDisplacementMap`
12
+ * bends the backdrop by it (three times, at slightly different strengths,
13
+ * for red, green and blue, when there is dispersion), and the map's blue
14
+ * channel lays the rim's highlight over the result. Panels of the same
15
+ * shape share a filter; one hidden `<svg>` holds them all. A panel's
16
+ * filter is remade only when its size or radius changes (ResizeObserver,
17
+ * and a watch on its `style` and `class`), so nothing runs per frame.
18
+ *
19
+ * Only Chromium draws an SVG filter in `backdrop-filter`. Safari and
20
+ * Firefox parse it and draw nothing, and no script can read back what a
21
+ * backdrop filter drew, so the check is that `backdrop-filter: url()`
22
+ * parses and the engine is neither WebKit (which takes
23
+ * `-webkit-backdrop-filter`) nor Gecko (`-moz-appearance`). Elsewhere the
24
+ * panel keeps glass.css's frost, tint, rim and sheen, untouched. Every
25
+ * filter is drawn at 1× and has an id of its own per shape, which also
26
+ * suits WebKit (it caches filters by id) should it come to draw them.
27
+ *
28
+ * The technique is adapted from Sam Asante's liquid-glass
29
+ * (https://github.com/samasante/liquid-glass, MIT, © 2026 Sam Asante):
30
+ * an SDF displacement map with a specular channel, fed to
31
+ * `feDisplacementMap` through `backdrop-filter`, with a three-pass RGB
32
+ * split for dispersion. This is our own code.
33
+ */
34
+
35
+ import { lensMap } from './lens-map.js';
36
+ import { mount } from './mount.js';
37
+
38
+ /**
39
+ * @typedef {import('./liquid-glass.js').LiquidGlassOptions} LiquidGlassOptions
40
+ * @typedef {import('./liquid-glass.js').LensSettings} LensSettings
41
+ * @typedef {Extract<import('./liquid-glass.js').LensStep, { kind: 'bend' }>} BendStep
42
+ * @typedef {Extract<import('./liquid-glass.js').LensStep, { kind: 'shine' }>} ShineStep
43
+ */
44
+
45
+ /** The settings a lens takes when neither its options nor its CSS give one. */
46
+ export const LENS_DEFAULTS = Object.freeze({ refract: 1, dispersion: 0.35, bezel: 28, specular: 0.7 });
47
+ /** The CSS custom property each setting is read from. */
48
+ const PROPERTIES = /** @type {const} */ ({ refract: '--glass-refract', dispersion: '--glass-dispersion', bezel: '--glass-bezel', specular: '--glass-specular' });
49
+ /** The furthest the rim moves what is behind it at strength 1, as a share of the bezel. */
50
+ const REACH = 1;
51
+ /** How far apart red and blue bend at full dispersion, as a share of the bend. */
52
+ const SPREAD = 0.3;
53
+ /** How many lens maps are kept for shapes no panel has now (a panel resized back and forth). */
54
+ const KEEP = 12;
55
+ const SVG = 'http://www.w3.org/2000/svg';
56
+ /** The custom properties in a panel's style that never change its lens: the sheen's place (glass-sheen.js) and the lens itself. */
57
+ const OWN = /--glass-(?:x|y|lens):[^;]*;?/g;
58
+
59
+ /** @type {boolean | undefined} */
60
+ let supported;
61
+ /** @type {SVGDefsElement | undefined} */
62
+ let defs;
63
+ let serial = 0;
64
+ /** The filters on the page, by shape and settings: each with its element and how many panels use it. */
65
+ /** @type {Map<string, { id: string, node: SVGFilterElement, users: number }>} */
66
+ const filters = new Map();
67
+ /** Lens maps as data URLs, by shape, the most recently used last. */
68
+ /** @type {Map<string, string>} */
69
+ const maps = new Map();
70
+
71
+ /**
72
+ * Whether this browser draws SVG filters in `backdrop-filter` (Chromium).
73
+ * Worked out once.
74
+ *
75
+ * @returns {boolean} True where refraction can be drawn.
76
+ */
77
+ export function lensSupported() {
78
+ if (supported !== undefined) return supported;
79
+ const css = typeof CSS !== 'undefined' && typeof CSS.supports === 'function' ? CSS : null;
80
+ supported = Boolean(css && css.supports('backdrop-filter', 'url(#a)') && !css.supports('-webkit-backdrop-filter', 'none') && !css.supports('-moz-appearance', 'none'));
81
+ return supported;
82
+ }
83
+
84
+ /**
85
+ * Reads a setting's value: a number, `true` (1), `false` or `none` (0).
86
+ *
87
+ * @param {unknown} value The value given (an option, or a custom property's text).
88
+ * @returns {number | undefined} The number, or undefined when there is none.
89
+ */
90
+ export function lensValue(value) {
91
+ if (value === undefined || value === null) return undefined;
92
+ if (typeof value === 'boolean') return value ? 1 : 0;
93
+ const text = String(value).trim();
94
+ if (!text) return undefined;
95
+ if (text === 'true') return 1;
96
+ if (text === 'false' || text === 'none') return 0;
97
+ const n = Number.parseFloat(text);
98
+ return Number.isFinite(n) ? n : undefined;
99
+ }
100
+
101
+ /**
102
+ * Works out a lens's settings: each from its option, else its custom
103
+ * property, else the default, clamped to its range.
104
+ *
105
+ * @param {LiquidGlassOptions} options The options given.
106
+ * @param {(property: string) => string} read Reads a custom property's text.
107
+ * @param {number} [unset] The strength when neither gives one. Default 1.
108
+ * @returns {LensSettings} The settings.
109
+ */
110
+ export function lensSettings(options, read, unset = LENS_DEFAULTS.refract) {
111
+ /**
112
+ * One setting, from its option, its property or its default.
113
+ *
114
+ * @param {keyof LensSettings} name The setting.
115
+ * @param {number} fallback Its default.
116
+ * @returns {number} Its value.
117
+ */
118
+ const pick = (name, fallback) => lensValue(options[name]) ?? lensValue(read(PROPERTIES[name])) ?? fallback;
119
+ /**
120
+ * Clamps a number to a range.
121
+ *
122
+ * @param {number} n The number.
123
+ * @param {number} max The top of the range (the bottom is 0).
124
+ * @returns {number} It, within 0 and `max`.
125
+ */
126
+ const clamp = (n, max) => Math.min(max, Math.max(0, n));
127
+ return {
128
+ refract: clamp(pick('refract', unset), 2),
129
+ dispersion: clamp(pick('dispersion', LENS_DEFAULTS.dispersion), 1),
130
+ bezel: clamp(pick('bezel', LENS_DEFAULTS.bezel), 400),
131
+ specular: clamp(pick('specular', LENS_DEFAULTS.specular), 1),
132
+ };
133
+ }
134
+
135
+ /**
136
+ * The steps of a lens's filter: one bend of the backdrop, or three (red,
137
+ * green, blue, each at its own strength) with dispersion, then the rim's
138
+ * highlight if it has one. A bend's `scale` is `feDisplacementMap`'s,
139
+ * twice the furthest it moves a point, px.
140
+ *
141
+ * @param {LensSettings} settings The lens's settings.
142
+ * @returns {Array<BendStep | ShineStep>} The steps, in order.
143
+ */
144
+ export function lensSteps({ refract, dispersion, bezel, specular }) {
145
+ const scale = round(2 * refract * bezel * REACH);
146
+ /** @type {Array<BendStep | ShineStep>} */
147
+ const steps =
148
+ dispersion > 0
149
+ ? [
150
+ { kind: 'bend', scale: round(scale * (1 - (SPREAD / 2) * dispersion)), channel: 'R' },
151
+ { kind: 'bend', scale, channel: 'G' },
152
+ { kind: 'bend', scale: round(scale * (1 + (SPREAD / 2) * dispersion)), channel: 'B' },
153
+ ]
154
+ : [{ kind: 'bend', scale }];
155
+ if (specular > 0) steps.push({ kind: 'shine', amount: round(specular) });
156
+ return steps;
157
+ }
158
+
159
+ /**
160
+ * Rounds to two decimal places, for filter attributes and cache keys.
161
+ *
162
+ * @param {number} n The number.
163
+ * @returns {number} It, rounded.
164
+ */
165
+ function round(n) {
166
+ return Math.round(n * 100) / 100;
167
+ }
168
+
169
+ /**
170
+ * The hidden `<svg>` that holds every lens filter, put back on the page if
171
+ * a view transition swapped the body out from under it.
172
+ *
173
+ * @returns {SVGDefsElement} Its `<defs>`.
174
+ */
175
+ function holder() {
176
+ if (!defs) {
177
+ const svg = document.createElementNS(SVG, 'svg');
178
+ svg.setAttribute('aria-hidden', 'true');
179
+ svg.setAttribute('focusable', 'false');
180
+ svg.setAttribute('width', '0');
181
+ svg.setAttribute('height', '0');
182
+ svg.style.cssText = 'position:absolute;width:0;height:0;overflow:hidden;pointer-events:none';
183
+ defs = document.createElementNS(SVG, 'defs');
184
+ svg.append(defs);
185
+ }
186
+ if (!defs.ownerSVGElement?.isConnected) document.body.append(/** @type {SVGSVGElement} */ (defs.ownerSVGElement));
187
+ return defs;
188
+ }
189
+
190
+ /**
191
+ * A shape's lens map as a PNG data URL, made once and kept for a while.
192
+ *
193
+ * @param {number} width The panel's width, whole px.
194
+ * @param {number} height Its height, whole px.
195
+ * @param {number} radius Its corner radius, px.
196
+ * @param {number} bezel The rim's width, px.
197
+ * @returns {string} The data URL.
198
+ */
199
+ function mapURL(width, height, radius, bezel) {
200
+ const key = `${width}x${height}r${radius}b${bezel}`;
201
+ let url = maps.get(key);
202
+ if (url) maps.delete(key);
203
+ else {
204
+ const canvas = document.createElement('canvas');
205
+ canvas.width = width;
206
+ canvas.height = height;
207
+ canvas.getContext('2d')?.putImageData(new ImageData(lensMap(width, height, radius, bezel), width, height), 0, 0);
208
+ url = canvas.toDataURL('image/png');
209
+ }
210
+ maps.set(key, url);
211
+ for (const old of maps.keys()) {
212
+ if (maps.size <= KEEP) break;
213
+ maps.delete(old);
214
+ }
215
+ return url;
216
+ }
217
+
218
+ /**
219
+ * Makes an SVG element with attributes.
220
+ *
221
+ * @param {string} name The element's name.
222
+ * @param {Record<string, string | number>} attributes Its attributes.
223
+ * @returns {SVGElement} The element.
224
+ */
225
+ function node(name, attributes) {
226
+ const el = /** @type {SVGElement} */ (document.createElementNS(SVG, name));
227
+ for (const [key, value] of Object.entries(attributes)) el.setAttribute(key, String(value));
228
+ return el;
229
+ }
230
+
231
+ /**
232
+ * Builds a lens filter: the map, the bends, the highlight over them.
233
+ *
234
+ * @param {string} id The filter's id.
235
+ * @param {number} width The panel's width, whole px.
236
+ * @param {number} height Its height, whole px.
237
+ * @param {number} radius Its corner radius, px.
238
+ * @param {LensSettings} settings The lens's settings.
239
+ * @returns {SVGFilterElement} The filter.
240
+ */
241
+ function build(id, width, height, radius, settings) {
242
+ const box = { x: 0, y: 0, width, height };
243
+ const filter = /** @type {SVGFilterElement} */ (node('filter', { id, ...box, filterUnits: 'userSpaceOnUse', primitiveUnits: 'userSpaceOnUse', 'color-interpolation-filters': 'sRGB' }));
244
+ filter.append(node('feImage', { ...box, href: mapURL(width, height, radius, settings.bezel), preserveAspectRatio: 'none', result: 'map' }));
245
+ const steps = lensSteps(settings);
246
+ const bends = /** @type {BendStep[]} */ (steps.filter((step) => step.kind === 'bend'));
247
+ /** Keeps one colour channel of a bend: red, green or blue, with full alpha. */
248
+ const only = { R: '1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0', G: '0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 1 0', B: '0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 1 0' };
249
+ bends.forEach(({ scale, channel }, i) => {
250
+ const bent = channel ? `bent-${channel}` : 'bent';
251
+ filter.append(node('feDisplacementMap', { in: 'SourceGraphic', in2: 'map', scale, xChannelSelector: 'R', yChannelSelector: 'G', result: bent }));
252
+ if (!channel) return;
253
+ filter.append(node('feColorMatrix', { in: bent, type: 'matrix', values: only[channel], result: channel }));
254
+ if (i) filter.append(node('feComposite', { in: i === 1 ? 'R' : 'RG', in2: channel, operator: 'arithmetic', k2: 1, k3: 1, result: i === 1 ? 'RG' : 'bent' }));
255
+ });
256
+ const shine = /** @type {ShineStep | undefined} */ (steps.find((step) => step.kind === 'shine'));
257
+ if (shine) {
258
+ filter.append(node('feColorMatrix', { in: 'map', type: 'matrix', values: `0 0 0 0 1 0 0 0 0 1 0 0 0 0 1 0 0 ${shine.amount} 0 0`, result: 'shine' }));
259
+ filter.append(node('feComposite', { in: 'shine', in2: 'bent', operator: 'over' }));
260
+ }
261
+ return filter;
262
+ }
263
+
264
+ /**
265
+ * Takes a lens filter for a shape and settings, building it if no panel
266
+ * has it yet.
267
+ *
268
+ * @param {number} width The panel's width, whole px.
269
+ * @param {number} height Its height, whole px.
270
+ * @param {number} radius Its corner radius, px.
271
+ * @param {LensSettings} settings The lens's settings.
272
+ * @returns {{ key: string, id: string }} The filter's key (to give it back) and id.
273
+ */
274
+ function acquire(width, height, radius, settings) {
275
+ const key = `${width}x${height}r${radius}:${settings.refract},${settings.dispersion},${settings.bezel},${settings.specular}`;
276
+ let entry = filters.get(key);
277
+ if (!entry) {
278
+ const id = `fx-lens-${++serial}`;
279
+ entry = { id, node: build(id, width, height, radius, settings), users: 0 };
280
+ filters.set(key, entry);
281
+ }
282
+ const parent = holder();
283
+ if (entry.node.parentNode !== parent) parent.append(entry.node);
284
+ entry.users++;
285
+ return { key, id: entry.id };
286
+ }
287
+
288
+ /**
289
+ * Gives a lens filter back, taking it off the page once no panel uses it.
290
+ *
291
+ * @param {string} key The filter's key.
292
+ */
293
+ function release(key) {
294
+ const entry = filters.get(key);
295
+ if (!entry || --entry.users > 0) return;
296
+ entry.node.remove();
297
+ filters.delete(key);
298
+ }
299
+
300
+ /**
301
+ * A panel's corner radius in px, from its top-left corner (a percentage
302
+ * of its width), clamped to half its shorter side.
303
+ *
304
+ * @param {CSSStyleDeclaration} style The panel's computed style.
305
+ * @param {number} width The panel's width, px.
306
+ * @param {number} height Its height, px.
307
+ * @returns {number} The radius, whole px.
308
+ */
309
+ function radiusOf(style, width, height) {
310
+ const first = style.borderTopLeftRadius.split(' ')[0] ?? '0';
311
+ const r = first.endsWith('%') ? (Number.parseFloat(first) / 100) * width : Number.parseFloat(first) || 0;
312
+ return Math.round(Math.min(r, width / 2, height / 2));
313
+ }
314
+
315
+ /**
316
+ * Starts a lens on one panel: from then on, `--glass-lens` holds its
317
+ * filter (glass.css puts it in the panel's `backdrop-filter`) and
318
+ * `data-glass-lens` is set, while its strength is above 0. Where the
319
+ * browser cannot draw it, nothing is done.
320
+ *
321
+ * @param {HTMLElement} el The panel (`class="glass"`).
322
+ * @param {LiquidGlassOptions} options Its settings; any left out are read
323
+ * from its custom properties (`--glass-refract`…), so CSS can change them.
324
+ * @param {number} unset The strength when neither options nor CSS give one.
325
+ * @returns {() => void} Stops the lens and takes its filter off.
326
+ */
327
+ function start(el, options, unset) {
328
+ if (!lensSupported() || typeof ResizeObserver === 'undefined') return () => {};
329
+ let width = 0;
330
+ let height = 0;
331
+ /** @type {string | null} */
332
+ let key = null;
333
+ let frame = 0;
334
+
335
+ /** Takes the panel's filter off it. */
336
+ const clear = () => {
337
+ if (key) release(key);
338
+ key = null;
339
+ if (el.style.getPropertyValue('--glass-lens')) el.style.removeProperty('--glass-lens');
340
+ if (el.hasAttribute('data-glass-lens')) el.removeAttribute('data-glass-lens');
341
+ };
342
+
343
+ /** Gives the panel the filter for its size, radius and settings, if that is not the one it has. */
344
+ const refresh = () => {
345
+ frame = 0;
346
+ const style = getComputedStyle(el);
347
+ const settings = lensSettings(options, (property) => style.getPropertyValue(property), unset);
348
+ if (settings.refract <= 0 || width < 2 || height < 2) return clear();
349
+ const radius = radiusOf(style, width, height);
350
+ const next = acquire(width, height, radius, settings);
351
+ if (key) release(key);
352
+ key = next.key;
353
+ const value = `url(#${next.id})`;
354
+ if (el.style.getPropertyValue('--glass-lens') !== value) el.style.setProperty('--glass-lens', value);
355
+ if (!el.hasAttribute('data-glass-lens')) el.setAttribute('data-glass-lens', '');
356
+ };
357
+
358
+ /** Refreshes at the next frame, once however many changes come before it. */
359
+ const queue = () => {
360
+ frame ||= requestAnimationFrame(refresh);
361
+ };
362
+
363
+ /**
364
+ * What of the panel's class and style could change its lens: all of it
365
+ * but the sheen's place, which follows the pointer, and the lens itself.
366
+ *
367
+ * @returns {string} The class and the rest of the style.
368
+ */
369
+ const look = () => `${el.className}|${(el.getAttribute('style') ?? '').replace(OWN, '')}`;
370
+ let looked = look();
371
+ /** Refreshes when the panel's class or style changed in a way that could change its lens. */
372
+ const restyled = () => {
373
+ const now = look();
374
+ if (now === looked) return;
375
+ looked = now;
376
+ queue();
377
+ };
378
+
379
+ /**
380
+ * Takes the panel's new size, border box in whole px, and refreshes.
381
+ *
382
+ * @param {ResizeObserverEntry[]} entries The panel's resize, the only one watched.
383
+ */
384
+ const measure = ([entry]) => {
385
+ const box = entry.borderBoxSize?.[0];
386
+ width = Math.round(box ? box.inlineSize : el.offsetWidth);
387
+ height = Math.round(box ? box.blockSize : el.offsetHeight);
388
+ queue();
389
+ };
390
+ const sizes = new ResizeObserver(measure);
391
+ sizes.observe(el, { box: 'border-box' });
392
+ const looks = new MutationObserver(restyled);
393
+ looks.observe(el, { attributes: true, attributeFilter: ['style', 'class'] });
394
+
395
+ return () => {
396
+ cancelAnimationFrame(frame);
397
+ sizes.disconnect();
398
+ looks.disconnect();
399
+ clear();
400
+ };
401
+ }
402
+
403
+ /**
404
+ * Bends what is behind one glass panel, as a thick lens. Settings left out
405
+ * of `options` are read from the panel's custom properties.
406
+ *
407
+ * @param {HTMLElement} el The panel (`class="glass"`, from glass.css).
408
+ * @param {LiquidGlassOptions} [options] The lens's settings.
409
+ * @returns {() => void} Stops it.
410
+ */
411
+ export function liquidGlass(el, options = {}) {
412
+ return start(el, options, LENS_DEFAULTS.refract);
413
+ }
414
+
415
+ /**
416
+ * Bends what is behind every `.glass` panel on the page, now and later,
417
+ * whose `--glass-refract` is above 0 (it is 0 unless set). Where the
418
+ * browser cannot draw it, nothing runs. Once is enough.
419
+ */
420
+ export function glassLens() {
421
+ if (typeof document === 'undefined' || !lensSupported()) return;
422
+ mount('.glass', (el) => start(el, {}, 0));
423
+ }
@@ -0,0 +1,217 @@
1
+ /**
2
+ * @module @arraypress/effects-astro/liquid-metal/beads
3
+ *
4
+ * The droplets a splash throws: beads of metal that fly out of the crater,
5
+ * fall, and find their way back. One landing on the metal sinks into it
6
+ * with a small ripple; one landing off it crawls back across the floor,
7
+ * drawn to the nearest edge (down the distance field's slope), and melts
8
+ * into it where it touches. Worked out on the CPU: a splash is a couple of
9
+ * dozen beads at most.
10
+ *
11
+ * Positions are in box heights (x 0 to the box's aspect, y 0–1 up).
12
+ */
13
+
14
+ /** Pulls a flying bead down, box heights a second squared. */
15
+ const GRAVITY = 4;
16
+ /** Draws a bead on the floor back to the metal, box heights a second squared. */
17
+ const PULL = 0.9;
18
+ /** How fast a crawling bead's speed dies away, per second. */
19
+ const FRICTION = 2;
20
+ /** How long a bead takes to sink in, seconds. */
21
+ const SINK = 0.24;
22
+ /** The longest a bead crawls before it is taken back anyway, seconds. */
23
+ const CRAWL = 6;
24
+ /** The slope below which the field is flat (a bead beyond its reach): there, a bead heads back to where it was thrown from. */
25
+ const FLAT = 0.2;
26
+
27
+ /**
28
+ * @typedef {object} Bead
29
+ * @property {number} x Across, box heights.
30
+ * @property {number} y Up, box heights.
31
+ * @property {number} vx Speed across, box heights a second.
32
+ * @property {number} vy Speed up.
33
+ * @property {number} z Height off the ground, box heights.
34
+ * @property {number} vz Speed off the ground.
35
+ * @property {number} r Its radius now.
36
+ * @property {number} size Its radius when thrown.
37
+ * @property {'fly' | 'crawl' | 'sink'} mode In the air, crawling back, or sinking in.
38
+ * @property {boolean} joined Sinking into the edge (drawn melted into it), not into the top.
39
+ * @property {number} age Seconds since it landed.
40
+ * @property {number} ox Where it was thrown from, across.
41
+ * @property {number} oy Where it was thrown from, up.
42
+ */
43
+
44
+ /**
45
+ * The distance field at a point, and its slope.
46
+ *
47
+ * @typedef {(x: number, y: number) => { d: number, gx: number, gy: number }} FieldSampler
48
+ */
49
+
50
+ /**
51
+ * Reads a packed distance field (src/runtime/shape.js's `distanceField`)
52
+ * at points in box heights.
53
+ *
54
+ * @param {Uint8Array} field One byte a pixel, rows from the top, 0.5 (128) at the edge.
55
+ * @param {number} width Its width.
56
+ * @param {number} height Its height.
57
+ * @param {number} spread Its reach either side of the edge, box heights.
58
+ * @param {number} aspect The box's width over its height.
59
+ * @returns {FieldSampler} The distance (positive inside, box heights) and
60
+ * its slope, which points inwards.
61
+ */
62
+ export function fieldSampler(field, width, height, spread, aspect) {
63
+ /**
64
+ * The field at a pixel position, bilinearly, edges clamped.
65
+ *
66
+ * @param {number} px Across, pixels.
67
+ * @param {number} py Down, pixels.
68
+ * @returns {number} The distance, box heights.
69
+ */
70
+ const at = (px, py) => {
71
+ const x = Math.min(width - 1, Math.max(0, px));
72
+ const y = Math.min(height - 1, Math.max(0, py));
73
+ const x0 = Math.floor(x);
74
+ const y0 = Math.floor(y);
75
+ const x1 = Math.min(width - 1, x0 + 1);
76
+ const y1 = Math.min(height - 1, y0 + 1);
77
+ const fx = x - x0;
78
+ const fy = y - y0;
79
+ const top = field[y0 * width + x0] * (1 - fx) + field[y0 * width + x1] * fx;
80
+ const bottom = field[y1 * width + x0] * (1 - fx) + field[y1 * width + x1] * fx;
81
+ return ((top * (1 - fy) + bottom * fy) / 255 - 0.5) * 2 * spread;
82
+ };
83
+ const sx = (width - 1) / aspect;
84
+ const sy = height - 1;
85
+ return (x, y) => {
86
+ const px = x * sx;
87
+ const py = (1 - y) * sy;
88
+ const e = 1.5;
89
+ return {
90
+ d: at(px, py),
91
+ gx: (at(px + e, py) - at(px - e, py)) / ((2 * e) / sx),
92
+ gy: (at(px, py - e) - at(px, py + e)) / ((2 * e) / sy),
93
+ };
94
+ };
95
+ }
96
+
97
+ /**
98
+ * The beads a splash throws: a ring of them, flung out and up at
99
+ * different speeds and sizes.
100
+ *
101
+ * @param {number} x Where it landed, across (box heights).
102
+ * @param {number} y Where it landed, up.
103
+ * @param {number} count How many beads.
104
+ * @param {number} [scale=1] Their size and reach, 1 for a full splash.
105
+ * @param {() => number} [random=Math.random] The random source.
106
+ * @returns {Bead[]} The beads, in the air.
107
+ */
108
+ export function splash(x, y, count, scale = 1, random = Math.random) {
109
+ /** @type {Bead[]} */
110
+ const beads = [];
111
+ for (let k = 0; k < count; k++) {
112
+ const angle = ((k + random() * 0.7) / count) * Math.PI * 2;
113
+ const speed = (0.14 + random() * 0.36) * scale;
114
+ const size = (0.007 + random() * random() * 0.014) * scale;
115
+ beads.push({
116
+ x: x + Math.cos(angle) * 0.02 * scale,
117
+ y: y + Math.sin(angle) * 0.02 * scale,
118
+ vx: Math.cos(angle) * speed,
119
+ vy: Math.sin(angle) * speed,
120
+ z: 0.004,
121
+ vz: (0.8 + random() * 0.7) * Math.sqrt(scale),
122
+ r: size,
123
+ size,
124
+ mode: 'fly',
125
+ joined: false,
126
+ age: 0,
127
+ ox: x,
128
+ oy: y,
129
+ });
130
+ }
131
+ return beads;
132
+ }
133
+
134
+ /**
135
+ * Moves the beads on, and returns those still out.
136
+ *
137
+ * @param {Bead[]} beads The beads; each is updated in place.
138
+ * @param {number} dt Seconds.
139
+ * @param {FieldSampler} sample The shape's distance field.
140
+ * @param {number} aspect The box's width over its height: beads stay inside it.
141
+ * @param {(x: number, y: number, size: number) => void} land Called where a
142
+ * bead lands on the metal or melts into it, with its size: the surface ripples there.
143
+ * @returns {Bead[]} The beads not yet taken back.
144
+ */
145
+ export function stepBeads(beads, dt, sample, aspect, land) {
146
+ /** @type {Bead[]} */
147
+ const out = [];
148
+ for (const b of beads) {
149
+ if (b.mode === 'sink') {
150
+ b.r -= (b.size * dt) / SINK;
151
+ if (b.r > 0) out.push(b);
152
+ continue;
153
+ }
154
+ if (b.mode === 'fly') {
155
+ b.vz -= GRAVITY * dt;
156
+ b.z += b.vz * dt;
157
+ b.x += b.vx * dt;
158
+ b.y += b.vy * dt;
159
+ if (b.z <= 0) {
160
+ b.z = 0;
161
+ const { d } = sample(b.x, b.y);
162
+ if (d > -b.r) {
163
+ // On the metal, or close enough to touch it: it goes back in.
164
+ b.mode = 'sink';
165
+ b.joined = d < b.r;
166
+ land(b.x, b.y, b.size);
167
+ } else {
168
+ // On the floor: it skids, then crawls back.
169
+ b.mode = 'crawl';
170
+ b.vx *= 0.3;
171
+ b.vy *= 0.3;
172
+ }
173
+ }
174
+ } else {
175
+ b.age += dt;
176
+ const { d, gx, gy } = sample(b.x, b.y);
177
+ // Down the field's slope to the nearest edge; beyond its reach, home.
178
+ const flat = Math.hypot(gx, gy) < FLAT;
179
+ const tx = flat ? b.ox - b.x : gx;
180
+ const ty = flat ? b.oy - b.y : gy;
181
+ const g = Math.hypot(tx, ty) || 1;
182
+ const drag = Math.exp(-FRICTION * dt);
183
+ b.vx = (b.vx + (tx / g) * PULL * dt) * drag;
184
+ b.vy = (b.vy + (ty / g) * PULL * dt) * drag;
185
+ b.x += b.vx * dt;
186
+ b.y += b.vy * dt;
187
+ if (d > -b.r * 0.4 || b.age > CRAWL) {
188
+ b.mode = 'sink';
189
+ b.joined = true;
190
+ land(b.x, b.y, b.size * 0.6);
191
+ }
192
+ }
193
+ b.x = Math.min(aspect - b.r, Math.max(b.r, b.x));
194
+ b.y = Math.min(1 - b.r, Math.max(b.r, b.y));
195
+ out.push(b);
196
+ }
197
+ return out;
198
+ }
199
+
200
+ /**
201
+ * Packs beads for the render: x, y, radius, and the height off the ground
202
+ * for one drawn on top (in the air, or sinking into the top), or -1 for
203
+ * one drawn melted into the edge (crawling, or sinking into the edge).
204
+ *
205
+ * @param {Bead[]} beads The beads.
206
+ * @param {Float32Array} out Room for four floats a bead.
207
+ * @returns {number} How many were packed (no more than fit).
208
+ */
209
+ export function packBeads(beads, out) {
210
+ const n = Math.min(beads.length, Math.floor(out.length / 4));
211
+ for (let i = 0; i < n; i++) {
212
+ const b = beads[i];
213
+ const melted = b.mode === 'crawl' || (b.mode === 'sink' && b.joined);
214
+ out.set([b.x, b.y, Math.max(b.r, 0), melted ? -1 : b.z], i * 4);
215
+ }
216
+ return n;
217
+ }