@arraypress/effects-astro 0.0.0-stage → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
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 +285 -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,799 @@
1
+ /**
2
+ * @module @arraypress/effects-astro/waves
3
+ *
4
+ * Audio motifs drawn as light, quiet enough to sit behind copy:
5
+ *
6
+ * - `signalLines`: layered waveform lines whose loudness is shaped by a
7
+ * slow drifting envelope (loud and quiet passages that move, as in a
8
+ * real track) over stacked frequencies, so it reads as audio, not
9
+ * smooth sweeps. The lines sit at depths: the front ones sharp, tall
10
+ * and bright, the back ones flatter, dimmer and out of focus, their
11
+ * colours fringed as a lens fringes them. Loud passages glow brighter;
12
+ * where lines cross, the palette's other end shimmers in. Now and then
13
+ * a pulse of light runs along a line, lighting the lines it passes.
14
+ * The pointer is a lens and a lamp: the lines part round it and catch
15
+ * its light.
16
+ * - `waveBars`: a mirrored bar waveform over its own reflection. The
17
+ * bars ease to a new take every couple of seconds (quick to rise, slow
18
+ * to fall, as a meter moves) with peak marks that hold and drop, while
19
+ * a playhead sweeps across lighting the bars it plays and leaving them
20
+ * glowing behind it. The pointer scrubs: the bars under it rise and
21
+ * light.
22
+ *
23
+ * Both can follow the sound playing on the page (`audio`, ./audio.js):
24
+ * the lines then carry its waveform, as tall as it is loud, and a kick
25
+ * sends a pulse along one; the bars show its spectrum, bass in the middle
26
+ * and treble out to the edges, and the playhead follows the track. While
27
+ * nothing plays, each keeps its own motion, and eases back to it.
28
+ *
29
+ * Both are drawn in one fragment pass each, every line and bar measured
30
+ * per pixel, so edges are smooth at any pixel ratio and light adds up as
31
+ * light does: summed, tone-mapped (hot cores run to white), with moving
32
+ * film grain in it and a dither so the soft glows never band. Colours are
33
+ * a left-to-right gradient mixed in Oklab, changed live through the
34
+ * element's `data-colors` (`setColors`); a quiet area can dim the light
35
+ * behind a headline. Transparent where nothing is lit. Still with
36
+ * prefers-reduced-motion; on phones the lines draw at three-quarters of
37
+ * the pixel ratio; without WebGL, the canvas is removed and the section
38
+ * shows.
39
+ *
40
+ * From the waveform-player site's hero backgrounds.
41
+ */
42
+
43
+ import { createStage } from './stage.js';
44
+ import { mixOklab, parseColor } from './color.js';
45
+ import { canHover, isMobileGpu } from './device.js';
46
+ import { pointerIn } from './pointer.js';
47
+ import { watchColors } from './mount.js';
48
+ import { GLSL_QUIET, quietUniform } from './glsl.js';
49
+ import { pickColors } from './palettes.js';
50
+ import { audioSignal } from './audio.js';
51
+ import { sampleAt, smoothInto } from './audio-meter.js';
52
+
53
+ /**
54
+ * @typedef {object} SignalLinesConfig
55
+ * @property {string} [color] One colour for every line (default: the `colors` gradient).
56
+ * @property {string[]} [colors] Colours the lines run through from left to right, blended in Oklab (default sea-glass, ice and pale gold).
57
+ * @property {string} [palette] A shared palette by name (PALETTES; any case), when there are no `colors` or `color`.
58
+ * @property {string} [pulse='#fafafa'] The pulses' colour.
59
+ * @property {number} [lines=7] How many lines, 2–12.
60
+ * @property {number} [amplitude=1] How tall the waves are.
61
+ * @property {number} [speed=1] How fast they move.
62
+ * @property {number} [pulses=1] How often pulses run; 0 for none.
63
+ * @property {number} [glow=1] How much light the lines and pulses give off, 0–2.
64
+ * @property {number} [grain=0.5] Film grain in the light, 0–1.
65
+ * @property {number} [depth=1] How far out of focus the back lines are, and how much their colours fringe; 0 for all sharp.
66
+ * @property {number} [bend=1] How far the lines part round the pointer and how much it lights them; 0 for not at all.
67
+ * @property {string} [audio] Follow the sound playing: `'page'` for whatever plays, or a CSS selector for an `<audio>`/`<video>` or a player's container (a WaveformPlayer). The lines carry its waveform, as tall as it is loud, and kicks send pulses; while nothing plays they keep their own motion. Default none.
68
+ * @property {{ x?: number, y?: number, radius?: number, depth?: number } | null} [quiet=null]
69
+ * An area where the light dims (behind a headline): centre x/y (0–1, from
70
+ * the top left), radius (a share of the height) and depth (0 none, 1 dark).
71
+ */
72
+
73
+ /**
74
+ * @typedef {object} WaveBarsConfig
75
+ * @property {string} [color] One colour for every bar (default: the `colors` gradient).
76
+ * @property {string[]} [colors] Colours the bars run through from left to right, blended in Oklab (default teal, ice and apricot).
77
+ * @property {string} [palette] A shared palette by name (PALETTES; any case), when there are no `colors` or `color`.
78
+ * @property {number} [step=8] Space between bars, CSS px.
79
+ * @property {number} [bar=3] A bar's width, CSS px.
80
+ * @property {number} [height=0.34] The tallest bar, a share of the height either side of the middle.
81
+ * @property {number} [every=2] Seconds between retunes.
82
+ * @property {boolean} [sweep=true] The playhead sweeping across, lighting the bars it plays.
83
+ * @property {number} [grain=0.4] Film grain in the light, 0–1.
84
+ * @property {boolean} [peaks=true] Peak marks over the bars that hold, then drop, as a meter's do.
85
+ * @property {number} [glow=1] How much light the bars and playhead give off, 0–2.
86
+ * @property {number} [scrub=1] How much the bars under the pointer rise and light; 0 for not at all.
87
+ * @property {string} [audio] Follow the sound playing: `'page'` for whatever plays, or a CSS selector for an `<audio>`/`<video>` or a player's container (a WaveformPlayer). The bars show its spectrum (bass in the middle) and the playhead follows the track; while nothing plays they keep their own motion. Default none.
88
+ * @property {{ x?: number, y?: number, radius?: number, depth?: number } | null} [quiet=null]
89
+ * An area where the light dims (behind a headline): centre x/y (0–1, from
90
+ * the top left), radius (a share of the height) and depth (0 none, 1 dark).
91
+ */
92
+
93
+ /** SignalLines' default colours: sea-glass, ice and pale gold. */
94
+ const LINE_COLORS = ['#4fd1c5', '#d6ecff', '#f2c58a'];
95
+ /** WaveBars' default colours: teal, ice and apricot. */
96
+ const BAR_COLORS = ['#2dd4bf', '#d6ecff', '#f6b26b'];
97
+ /** Most pulses running at once. */
98
+ const PULSES = 4;
99
+ /** The gradient texture's width: plenty for a smooth blend across any screen. */
100
+ const GRADIENT = 256;
101
+ /** Texture unit of the colour gradient. */
102
+ const INK_UNIT = 0;
103
+ /** Texture unit of the bars' heights. */
104
+ const BARS_UNIT = 1;
105
+ /** Texture unit of the sound's waveform, for the lines, and its width: the trace smoothed onto this many points has no visible corners. */
106
+ const WAVE_UNIT = 1;
107
+ const WAVE_POINTS = 256;
108
+
109
+ /**
110
+ * GLSL shared by both: the colour gradient (`ink(u)`, linear light, u 0–1
111
+ * from the left), the quiet area (GLSL_QUIET), and `present(light)`, which
112
+ * tone-maps summed light, lays moving grain in it, dithers it and writes it
113
+ * premultiplied, its alpha its brightest channel, so what is not lit stays
114
+ * transparent.
115
+ */
116
+ const GLSL_LIGHT = `${GLSL_QUIET}
117
+ uniform sampler2D uInk;
118
+ uniform float uSeed;
119
+ uniform float uGrain;
120
+ float hash(vec2 p) { return fract(sin(dot(p, vec2(127.1, 311.7))) * 43758.5453); }
121
+ vec3 ink(float u) { return pow(texture2D(uInk, vec2(clamp(u, 0.0, 1.0), 0.5)).rgb, vec3(2.2)); }
122
+ vec4 present(vec3 light) {
123
+ light *= mix(1.0 - uQuiet.w, 1.0, quietOpen((gl_FragCoord.xy - 0.5 * uRes) / uRes.y));
124
+ // A soft toe: faint light fades smoothly to nothing rather than to a grey edge.
125
+ light = light * light / (light + 0.003);
126
+ if (max(light.r, max(light.g, light.b)) < 0.00001) return vec4(0.0);
127
+ light *= 1.0 + (hash(gl_FragCoord.xy + uSeed) - 0.5) * uGrain;
128
+ vec3 c = pow(1.0 - exp(-light), vec3(1.0 / 2.2));
129
+ c = clamp(c + (hash(gl_FragCoord.yx + uSeed * 1.7) - 0.5) / 255.0, 0.0, 1.0);
130
+ return vec4(c, max(c.r, max(c.g, c.b)));
131
+ }
132
+ `;
133
+
134
+ /**
135
+ * SignalLines' pass: every line measured at every pixel, with its pulses,
136
+ * the pointer's lens and lamp, and a sheen where lines cross.
137
+ *
138
+ * @param {number} lines How many lines (a loop bound, so compiled in).
139
+ * @returns {string} GLSL.
140
+ */
141
+ function linesFragment(lines) {
142
+ return `
143
+ precision highp float;
144
+ #define LINES ${lines}
145
+ #define PULSES ${PULSES}
146
+ uniform vec2 uRes;
147
+ uniform float uRatio;
148
+ uniform float uTime;
149
+ uniform float uAmp;
150
+ uniform float uGlow;
151
+ uniform float uDepth;
152
+ uniform float uBend;
153
+ uniform vec3 uPointer;
154
+ uniform vec4 uPulse[PULSES];
155
+ uniform vec3 uPulseInk;
156
+ uniform sampler2D uWave;
157
+ uniform float uLive;
158
+ uniform float uLevel;
159
+ ${GLSL_LIGHT}
160
+ float H;
161
+ float W;
162
+
163
+ // The sound's waveform under line L at x (CSS px), -1–1: each line reads
164
+ // it stretched and shifted its own way, there and back so it has no seam.
165
+ float wave(float L, float x) {
166
+ float s = x / W * (0.55 + 0.12 * L) + L * 0.173;
167
+ vec4 t = texture2D(uWave, vec2(abs(fract(s * 0.5) * 2.0 - 1.0), 0.5));
168
+ return (t.r * 255.0 * 256.0 + t.g * 255.0) / 65535.0 * 2.0 - 1.0;
169
+ }
170
+
171
+ // A line at x (CSS px): its height (CSS px, up), slope, and loudness 0.32–1.
172
+ vec3 lineAt(float L, float x) {
173
+ float k = L / float(LINES - 1);
174
+ float amp = H * (0.24 - 0.16 * k) * uAmp;
175
+ float pace = 0.75 - 0.35 * k;
176
+ float ph = L * 1.1;
177
+ float a1 = x * 0.0032 - uTime * 0.45 + ph;
178
+ float a2 = x * 0.0016 + uTime * 0.22;
179
+ float u = sin(a1) * 0.7 + sin(a2) * 0.3;
180
+ // Following sound, the loudness is the sound's, and its waveform rides the line.
181
+ float env = mix(0.32 + 0.68 * abs(u), 0.3 + 0.7 * uLevel, uLive);
182
+ float dEnv = 0.68 * sign(u) * (cos(a1) * 0.00224 + cos(a2) * 0.00048) * (1.0 - uLive);
183
+ float b1 = x * 0.006 + uTime * pace + ph;
184
+ float b2 = x * 0.015 - uTime * pace * 0.8 + ph * 1.5;
185
+ float b3 = x * 0.033 + uTime * pace * 1.4 + ph;
186
+ float shape = sin(b1) * 0.55 + sin(b2) * 0.3 + sin(b3) * 0.16;
187
+ float dShape = cos(b1) * 0.0033 + cos(b2) * 0.0045 + cos(b3) * 0.00528;
188
+ float own = 1.0 - 0.6 * uLive;
189
+ float y = H * 0.5 + amp * env * shape * own;
190
+ float slope = amp * (dEnv * shape + env * dShape) * own;
191
+ if (uLive > 0.0) {
192
+ float tall = uLive * H * 0.2 * uAmp * mix(1.0, 0.4, k) * (0.15 + 0.85 * uLevel);
193
+ float w0 = wave(L, x);
194
+ y += tall * w0;
195
+ slope += tall * (wave(L, x + 1.5) - w0) / 1.5;
196
+ }
197
+ // The pointer's lens: lines near it spread away from it.
198
+ vec2 d = vec2(x, y) - uPointer.xy;
199
+ float m = 0.6 * uBend * uPointer.z * exp(-(d.x * d.x + d.y * d.y) / 26000.0);
200
+ return vec3(y + d.y * m, slope * (1.0 + m) - d.y * m * d.x / 13000.0, env);
201
+ }
202
+
203
+ void main() {
204
+ H = uRes.y / uRatio;
205
+ W = uRes.x / uRatio;
206
+ vec2 p = gl_FragCoord.xy / uRatio;
207
+ vec3 light = vec3(0.0);
208
+
209
+ // The pulses' heads: a hot point with a soft bloom and a thin flare along
210
+ // the line, and a light that the lines near it catch.
211
+ float heads = 0.0;
212
+ for (int i = 0; i < PULSES; i++) {
213
+ vec4 q = uPulse[i];
214
+ if (q.w <= 0.0) continue;
215
+ vec2 d = p - vec2(q.y, lineAt(q.x, q.y).x);
216
+ float r2 = dot(d, d);
217
+ heads += q.w * exp(-r2 / 18000.0);
218
+ light += uPulseInk * q.w * (exp(-r2 / 10.0) * 2.0 + exp(-sqrt(r2) / 14.0) * 0.14 * uGlow + exp(-abs(d.y) / 1.2 - abs(d.x) / 36.0) * 0.22 * uGlow);
219
+ }
220
+
221
+ if (abs(p.y - H * 0.5) < H * 0.26 * uAmp + 200.0) {
222
+ vec2 toLamp = p - uPointer.xy;
223
+ float boost = 1.0 + heads * 2.4 + uPointer.z * uBend * 0.9 * exp(-dot(toLamp, toLamp) / 30000.0);
224
+ vec3 tint = ink(p.x / W);
225
+ float sum = 0.0;
226
+ float squares = 0.0;
227
+ for (int i = 0; i < LINES; i++) {
228
+ float L = float(i);
229
+ float k = L / float(LINES - 1);
230
+ vec3 l = lineAt(L, p.x);
231
+ float dist = (p.y - l.x) * inversesqrt(1.0 + l.y * l.y);
232
+ // Depth: back lines blur (a wider core, dimmer) and fringe.
233
+ float sigma = max(0.6 + uDepth * 3.6 * k * k, 0.55 / uRatio);
234
+ float energy = (0.35 + 0.65 * l.z) * mix(1.0, 0.45, k) * sqrt(0.6 / sigma);
235
+ float fringe = uDepth * (0.15 + 1.4 * k);
236
+ vec3 o = vec3(dist - fringe, dist, dist + fringe);
237
+ vec3 core = exp(-o * o / (2.0 * sigma * sigma));
238
+ float halo = exp(-abs(dist) / mix(6.0, 13.0, k));
239
+ // Pulses running along this line: a bright run with a tail behind it,
240
+ // its front sharp on the line and soft in the light round it.
241
+ float run = 0.0;
242
+ float runGlow = 0.0;
243
+ for (int j = 0; j < PULSES; j++) {
244
+ vec4 q = uPulse[j];
245
+ float a = q.y - p.x;
246
+ float on = step(abs(q.x - L), 0.5) * q.w;
247
+ float tail = exp(-max(a, 0.0) * 3.0 / max(q.z, 1.0));
248
+ run += on * tail * exp(min(a, 0.0) / 3.0);
249
+ runGlow += on * tail * exp(min(a, 0.0) / 24.0);
250
+ }
251
+ // Further back, a little cooler and greyer, as distance does.
252
+ vec3 hue = mix(tint, vec3(0.55, 0.62, 0.72) * dot(tint, vec3(0.3, 0.55, 0.15)), k * 0.35);
253
+ light += hue * (core * 1.35 + halo * 0.09 * uGlow) * energy * boost;
254
+ light += uPulseInk * (run * core.g * 2.6 + runGlow * halo * 0.3 * uGlow) * mix(1.0, 0.5, k);
255
+ float c = core.g * energy;
256
+ sum += c;
257
+ squares += c * c;
258
+ }
259
+ // Where lines cross, the palette's other end shimmers in.
260
+ float crossing = clamp(sum * sum - squares, 0.0, 1.0);
261
+ light += ink(fract(p.x / W + 0.5 + 0.2 * sin(uTime * 0.4 + p.x * 0.012))) * crossing * 0.7;
262
+ }
263
+ gl_FragColor = present(light);
264
+ }
265
+ `;
266
+ }
267
+
268
+ /** WaveBars' pass: each pixel's bar and two neighbours either side (for their glow), each over its reflection with its peak marks, and the playhead. */
269
+ const BARS_FRAG = `
270
+ precision highp float;
271
+ uniform vec2 uRes;
272
+ uniform float uRatio;
273
+ uniform sampler2D uBars;
274
+ uniform float uCount;
275
+ uniform float uStep;
276
+ uniform float uBar;
277
+ uniform float uMax;
278
+ uniform float uPlay;
279
+ uniform float uTrail;
280
+ uniform float uGlow;
281
+ uniform float uPeaks;
282
+ uniform vec2 uScrub;
283
+ ${GLSL_LIGHT}
284
+
285
+ #define GAP 1.0
286
+
287
+ // A bar's height and peak mark, each 0–1, from 16 bits apiece.
288
+ vec2 barAt(float b) {
289
+ vec4 t = texture2D(uBars, vec2((b + 0.5) / uCount, 0.5));
290
+ return vec2(t.r * 255.0 * 256.0 + t.g * 255.0, t.b * 255.0 * 256.0 + t.a * 255.0) / 65535.0;
291
+ }
292
+
293
+ // Distance (CSS px) to a bar rising from the gap: x across from its middle,
294
+ // s up from the gap, m its length, r its radius.
295
+ float capsule(float x, float s, float m, float r) {
296
+ return length(vec2(x, max(abs(s - m * 0.5) - (m * 0.5 - r), 0.0))) - r;
297
+ }
298
+
299
+ // Distance (CSS px) to a peak cap: as wide as a bar, 1.5px tall, rounded.
300
+ float cap(float x, float y, float r) {
301
+ vec2 q = abs(vec2(x, y)) - vec2(r - 0.75, 0.0);
302
+ return length(max(q, 0.0)) - 0.75;
303
+ }
304
+
305
+ // Coverage of a shape at a distance (CSS px), smoothed over a device pixel.
306
+ float edge(float d) { return clamp(0.5 - d * uRatio, 0.0, 1.0); }
307
+
308
+ void main() {
309
+ float W = uRes.x / uRatio;
310
+ float H = uRes.y / uRatio;
311
+ vec2 p = gl_FragCoord.xy / uRatio;
312
+ float v = p.y - H * 0.5;
313
+ float off = (W - uCount * uStep) * 0.5;
314
+ float i = floor((p.x - off) / uStep);
315
+ float r = uBar * 0.5;
316
+ // Up from the gap over the middle, and down from it into the reflection.
317
+ float up = v - GAP;
318
+ float down = -v - GAP;
319
+ vec3 light = vec3(0.0);
320
+ for (int j = -2; j <= 2; j++) {
321
+ float b = i + float(j);
322
+ if (b < 0.0 || b >= uCount) continue;
323
+ vec2 bar = barAt(b);
324
+ float bx = off + (b + 0.5) * uStep;
325
+ float m = max(bar.x * uMax, r * 2.0);
326
+ float dx = p.x - bx;
327
+ // Lit by the playhead as it plays them (a tail of light behind it,
328
+ // a glimmer just ahead), and by the pointer scrubbing.
329
+ float behind = uPlay - bx;
330
+ float lit = behind >= 0.0 ? exp(-behind / uTrail) : exp(behind / 9.0) * 0.6;
331
+ lit = max(lit, uScrub.y * exp(-(bx - uScrub.x) * (bx - uScrub.x) / 2400.0) * 0.7);
332
+ vec3 tint = ink(bx / W);
333
+ // Unplayed bars are muted; played ones take the full colour. Each is
334
+ // brighter towards its tip, and a played tip runs hot.
335
+ vec3 tone = mix(vec3(dot(tint, vec3(0.3, 0.59, 0.11))), tint, 0.6) * 0.17 + tint * 1.6 * lit;
336
+ float along = clamp(up / m, 0.0, 1.0);
337
+ // The bar, and its reflection: dimmer, fading as it goes down.
338
+ float d = capsule(dx, up, m, r);
339
+ float e = capsule(dx, down, m, r);
340
+ light += (tone * (0.55 + 0.6 * along) + vec3(0.9, 0.95, 1.0) * lit * lit * smoothstep(0.4, 1.0, along) * 0.9) * (edge(d) + exp(-max(d, 0.0) / 4.0) * 0.16 * uGlow);
341
+ light += tone * (edge(e) + exp(-max(e, 0.0) / 4.0) * 0.1 * uGlow) * 0.36 * (1.0 - 0.8 * clamp(down / m, 0.0, 1.0));
342
+ // The peak marks: a short cap over the bar, and its reflection.
343
+ float mark = max(bar.y * uMax, m) + 3.5;
344
+ vec3 markInk = mix(tint, vec3(1.0), 0.4) * (0.18 + 1.3 * lit) * uPeaks;
345
+ light += markInk * (edge(cap(dx, up - mark, r)) + edge(cap(dx, down - mark, r)) * 0.22);
346
+ }
347
+ // The playhead: a fine bright line with a soft column of light round it,
348
+ // reaching a little past the bar it is playing, dimmer in the reflection.
349
+ float d = abs(p.x - uPlay);
350
+ float playing = barAt(clamp(floor((uPlay - off) / uStep), 0.0, uCount - 1.0)).x * uMax;
351
+ float reach = exp(-pow(abs(v) / (playing + 24.0), 8.0)) * (v < 0.0 ? 0.45 : 1.0);
352
+ light += mix(ink(uPlay / W), vec3(1.0), 0.55) * reach * (exp(-d * d / 0.7) * 1.5 + (exp(-d / 7.0) * 0.1 + exp(-d / 32.0) * 0.025) * uGlow);
353
+ gl_FragColor = present(light);
354
+ }
355
+ `;
356
+
357
+ /**
358
+ * A left-to-right gradient through some colours, mixed in Oklab so the
359
+ * blend does not go muddy, as sRGB bytes for a texture.
360
+ *
361
+ * @param {string[]} colors The colours, at least one.
362
+ * @returns {Uint8Array} GRADIENT RGBA texels.
363
+ * @throws {TypeError} When a colour is not hex.
364
+ */
365
+ function gradient(colors) {
366
+ const rgb = colors.map(parseColor);
367
+ const bytes = new Uint8Array(GRADIENT * 4);
368
+ for (let i = 0; i < GRADIENT; i++) {
369
+ const at = (i / (GRADIENT - 1)) * (rgb.length - 1);
370
+ const from = Math.min(Math.floor(at), Math.max(0, rgb.length - 2));
371
+ const c = rgb.length > 1 ? mixOklab(rgb[from], rgb[from + 1], at - from) : rgb[0];
372
+ bytes.set([...c.map((v) => Math.round(v * 255)), 255], i * 4);
373
+ }
374
+ return bytes;
375
+ }
376
+
377
+ /**
378
+ * Makes a texture on a unit, filtered and clamped, not yet filled.
379
+ *
380
+ * @param {WebGLRenderingContext | WebGL2RenderingContext} gl The context.
381
+ * @param {number} unit Its texture unit.
382
+ * @param {number} filter gl.LINEAR or gl.NEAREST.
383
+ * @returns {WebGLTexture} The texture, bound on its unit.
384
+ */
385
+ function makeTexture(gl, unit, filter) {
386
+ const texture = /** @type {WebGLTexture} */ (gl.createTexture());
387
+ gl.activeTexture(gl.TEXTURE0 + unit);
388
+ gl.bindTexture(gl.TEXTURE_2D, texture);
389
+ gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, filter);
390
+ gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, filter);
391
+ gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
392
+ gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
393
+ return texture;
394
+ }
395
+
396
+ /**
397
+ * Writes a value as 16 bits in two bytes, high first, for a shader to read
398
+ * back from two channels.
399
+ *
400
+ * @param {number} value The value, 0–1.
401
+ * @param {Uint8Array} data The bytes.
402
+ * @param {number} at Where the first byte goes.
403
+ */
404
+ function pack16(value, data, at) {
405
+ const v = Math.round(Math.min(1, Math.max(0, value)) * 65535);
406
+ data[at] = v >> 8;
407
+ data[at + 1] = v & 255;
408
+ }
409
+
410
+ /**
411
+ * The sound an effect follows, if it has `audio` and moves: started with
412
+ * the stage, stopped with it.
413
+ *
414
+ * @param {import('./stage.js').Stage} stage The stage.
415
+ * @param {string | undefined} source The `audio` setting.
416
+ * @returns {import('./audio.js').AudioSignal | null} The signal, or null.
417
+ */
418
+ function listen(stage, source) {
419
+ return source && !stage.still ? audioSignal(source) : null;
420
+ }
421
+
422
+ /**
423
+ * The colour gradient both effects read (`uInk`): made on INK_UNIT, and
424
+ * filled again when the element's `data-colors` changes (redrawing a still
425
+ * frame).
426
+ *
427
+ * @param {import('./stage.js').Stage} stage The stage, its program bound.
428
+ * @param {HTMLElement} el The effect's element, for `data-colors`.
429
+ * @param {string[]} colors The colours to start with.
430
+ * @returns {() => void} Stops following `data-colors` and frees the texture.
431
+ */
432
+ function inkGradient(stage, el, colors) {
433
+ const { gl, uniform } = stage;
434
+ const texture = makeTexture(gl, INK_UNIT, gl.LINEAR);
435
+ gl.uniform1i(uniform('uInk'), INK_UNIT);
436
+
437
+ /**
438
+ * Fills the texture with a gradient through some colours.
439
+ *
440
+ * @param {string[]} next The colours.
441
+ */
442
+ const fill = (next) => {
443
+ gl.activeTexture(gl.TEXTURE0 + INK_UNIT);
444
+ gl.bindTexture(gl.TEXTURE_2D, texture);
445
+ gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, GRADIENT, 1, 0, gl.RGBA, gl.UNSIGNED_BYTE, gradient(next));
446
+ };
447
+ fill(colors);
448
+ let started = false;
449
+ const unwatch = watchColors(el, (next) => {
450
+ fill(next.colors);
451
+ if (started && stage.still) stage.redraw();
452
+ });
453
+ started = true;
454
+ return () => {
455
+ unwatch();
456
+ gl.deleteTexture(texture);
457
+ };
458
+ }
459
+
460
+ /**
461
+ * The pointer over an element, eased, in CSS px from the bottom left, with
462
+ * how present it is (0–1): it fades in as the pointer arrives and out as it
463
+ * leaves, so nothing snaps. Only on devices that hover.
464
+ *
465
+ * @param {HTMLElement} el The element.
466
+ * @param {boolean} enabled Whether to follow it at all.
467
+ * @returns {(dt: number, width: number, height: number) => [number, number, number]} Reads it for a frame.
468
+ */
469
+ function follower(el, enabled) {
470
+ const on = enabled && canHover();
471
+ /** @type {[number, number, number]} */
472
+ const eased = [0, 0, 0];
473
+ let placed = false;
474
+
475
+ /**
476
+ * Reads the pointer for a frame.
477
+ *
478
+ * @param {number} dt Seconds since the last read.
479
+ * @param {number} width The element's width, CSS px.
480
+ * @param {number} height Its height, CSS px.
481
+ * @returns {[number, number, number]} x, y (CSS px, from the bottom left) and presence 0–1.
482
+ */
483
+ const read = (dt, width, height) => {
484
+ if (!on) return eased;
485
+ const p = pointerIn(el);
486
+ if (p.inside && !placed) {
487
+ eased[0] = p.x * width;
488
+ eased[1] = p.y * height;
489
+ placed = true;
490
+ }
491
+ if (p.inside) {
492
+ const k = 1 - Math.exp(-dt / 0.12);
493
+ eased[0] += (p.x * width - eased[0]) * k;
494
+ eased[1] += (p.y * height - eased[1]) * k;
495
+ }
496
+ eased[2] += ((p.inside ? 1 : 0) - eased[2]) * (1 - Math.exp(-dt / 0.35));
497
+ if (eased[2] < 0.01 && !p.inside) placed = false;
498
+ return eased;
499
+ };
500
+ return read;
501
+ }
502
+
503
+ /**
504
+ * The colours an effect starts with: its `colors`, or its one `color`, or
505
+ * its `palette`, or its defaults.
506
+ *
507
+ * @param {{ color?: string, colors?: string[], palette?: string }} config The settings.
508
+ * @param {string[]} fallback The defaults.
509
+ * @returns {string[]} At least one colour.
510
+ */
511
+ function startColors(config, fallback) {
512
+ if (!config.colors?.length && config.color) return [config.color];
513
+ return pickColors(config, fallback);
514
+ }
515
+
516
+ /**
517
+ * Starts signal lines on an element (its first canvas is drawn into).
518
+ *
519
+ * @param {HTMLElement} el The element.
520
+ * @param {SignalLinesConfig} config The settings.
521
+ * @returns {() => void} Stops it.
522
+ */
523
+ export function signalLines(el, config) {
524
+ const lines = Math.round(Math.min(12, Math.max(2, config.lines ?? 7)));
525
+ const rate = Math.max(0, config.pulses ?? 1);
526
+ const speed = config.speed ?? 1;
527
+ /** @type {{ line: number, x: number, speed: number, len: number, strength: number }[]} */
528
+ const pulses = [];
529
+ const pulseData = new Float32Array(PULSES * 4);
530
+ const pointer = follower(el, (config.bend ?? 1) > 0);
531
+ // Started a little way in, so the still frame and the first are a good moment.
532
+ const start = performance.now() - 7_300 / Math.max(speed, 0.01);
533
+ /** @type {import('./audio.js').AudioSignal | null} */
534
+ let sound = null;
535
+ /** The sound's waveform: its texture, the smoothed trace and the bytes it is filled from; and the kicks already answered with a pulse. */
536
+ /** @type {WebGLTexture | null} */
537
+ let waveTexture = null;
538
+ const wave = new Float32Array(WAVE_POINTS);
539
+ const waveData = new Uint8Array(WAVE_POINTS * 4);
540
+ let beats = 0;
541
+
542
+ /** Sets a pulse off along a random line, from the left edge. */
543
+ const launch = () => {
544
+ if (pulses.length < PULSES) pulses.push({ line: (Math.random() * lines) | 0, x: -40, speed: 240 + Math.random() * 260, len: 90 + Math.random() * 110, strength: 0.75 + Math.random() * 0.35 });
545
+ };
546
+
547
+ const { stop } = createStage(el, {
548
+ fragment: linesFragment(lines),
549
+ scale: isMobileGpu() ? 0.75 : 1,
550
+ setup(stage) {
551
+ const { gl, uniform } = stage;
552
+ gl.uniform1f(uniform('uAmp'), config.amplitude ?? 1);
553
+ gl.uniform1f(uniform('uGlow'), Math.min(2, Math.max(0, config.glow ?? 1)));
554
+ gl.uniform1f(uniform('uGrain'), Math.min(1, Math.max(0, config.grain ?? 0.5)) * 0.45);
555
+ gl.uniform1f(uniform('uDepth'), Math.max(0, config.depth ?? 1));
556
+ gl.uniform1f(uniform('uBend'), Math.max(0, config.bend ?? 1));
557
+ gl.uniform3fv(uniform('uPulseInk'), parseColor(config.pulse ?? '#fafafa').map((c) => c ** 2.2));
558
+ // The still frame keeps one pulse, part way along the second line.
559
+ if (stage.still && rate > 0) pulses.push({ line: 1, x: 0, speed: 0, len: 150, strength: 1 });
560
+ const freeInk = inkGradient(stage, el, startColors(config, LINE_COLORS));
561
+ sound = listen(stage, config.audio);
562
+ if (!sound) return freeInk;
563
+ const texture = makeTexture(gl, WAVE_UNIT, gl.LINEAR);
564
+ waveTexture = texture;
565
+ gl.uniform1i(uniform('uWave'), WAVE_UNIT);
566
+ return () => {
567
+ sound?.stop();
568
+ freeInk();
569
+ gl.deleteTexture(texture);
570
+ };
571
+ },
572
+ resize({ gl, uniform, canvas }) {
573
+ gl.uniform4f(uniform('uQuiet'), ...quietUniform(config.quiet, canvas.width / canvas.height));
574
+ },
575
+ uniforms({ gl, uniform, canvas, ratio, still }, now, dt) {
576
+ const w = canvas.width / ratio;
577
+ if (still && pulses[0]) pulses[0].x = w * 0.64;
578
+ // Pulses: now and then one sets off along a line, and on each kick of the sound.
579
+ const heard = sound?.read(now, dt) ?? null;
580
+ if (dt && Math.random() < rate * 0.9 * dt) launch();
581
+ if (heard && heard.beats !== beats) {
582
+ beats = heard.beats;
583
+ if (rate > 0 && heard.on > 0.5) launch();
584
+ }
585
+ gl.uniform1f(uniform('uLive'), heard ? heard.on : 0);
586
+ if (heard) {
587
+ smoothInto(heard.waveform, wave);
588
+ for (let i = 0; i < WAVE_POINTS; i++) pack16(wave[i] * 0.5 + 0.5, waveData, i * 4);
589
+ gl.activeTexture(gl.TEXTURE0 + WAVE_UNIT);
590
+ gl.bindTexture(gl.TEXTURE_2D, waveTexture);
591
+ gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, WAVE_POINTS, 1, 0, gl.RGBA, gl.UNSIGNED_BYTE, waveData);
592
+ gl.uniform1f(uniform('uLevel'), heard.level);
593
+ }
594
+ for (let i = pulses.length - 1; i >= 0; i--) {
595
+ pulses[i].x += pulses[i].speed * speed * dt;
596
+ if (pulses[i].x - pulses[i].len > w) pulses.splice(i, 1);
597
+ }
598
+ pulseData.fill(0);
599
+ pulses.forEach((p, i) => pulseData.set([p.line, p.x, p.len, p.strength], i * 4));
600
+ gl.uniform1f(uniform('uRatio'), ratio);
601
+ gl.uniform1f(uniform('uTime'), ((now - start) / 1000) * speed);
602
+ gl.uniform1f(uniform('uSeed'), still ? 0 : Math.random() * 100);
603
+ gl.uniform3fv(uniform('uPointer'), still ? [0, 0, 0] : pointer(dt, w, canvas.height / ratio));
604
+ gl.uniform4fv(uniform('uPulse'), pulseData);
605
+ },
606
+ });
607
+ return stop;
608
+ }
609
+
610
+ /**
611
+ * Starts tuning wave bars on an element (its first canvas is drawn into).
612
+ *
613
+ * @param {HTMLElement} el The element.
614
+ * @param {WaveBarsConfig} config The settings.
615
+ * @returns {() => void} Stops it.
616
+ */
617
+ export function waveBars(el, config) {
618
+ const step = Math.max(2, config.step ?? 8);
619
+ const every = Math.max(0.2, config.every ?? 2) * 1000;
620
+ const scrub = Math.max(0, config.scrub ?? 1);
621
+ const pointer = follower(el, scrub > 0);
622
+ /** The resting take's phases: what a still frame shows. */
623
+ const REST = [0.6, 2.1, 1, 0.3, 0];
624
+ /**
625
+ * Each bar's shown height, the take it heads for, its peak mark, how long the mark still holds, and how fast it falls.
626
+ *
627
+ * @type {{ cur: Float32Array, tgt: Float32Array, peak: Float32Array, hold: Float32Array, fall: Float32Array }}
628
+ */
629
+ let bars = { cur: new Float32Array(0), tgt: new Float32Array(0), peak: new Float32Array(0), hold: new Float32Array(0), fall: new Float32Array(0) };
630
+ let data = new Uint8Array(0);
631
+ let lastTune = 0;
632
+ let start = performance.now();
633
+ /** @type {WebGLTexture | null} */
634
+ let heights = null;
635
+ /** @type {import('./audio.js').AudioSignal | null} */
636
+ let sound = null;
637
+ /** The playhead (CSS px across), and whether it followed a track last frame. */
638
+ let head = 0;
639
+ let following = false;
640
+
641
+ /**
642
+ * A take: heights that rise and fall together over a few bars, under a
643
+ * broad envelope, as a real recording's do, 0.12–1.
644
+ *
645
+ * @param {number} i The bar.
646
+ * @param {number[]} seed Five phases for this take.
647
+ * @returns {number} Its height.
648
+ */
649
+ const take = (i, seed) => {
650
+ const env = 0.5 + 0.5 * Math.sin(i * 0.045 + seed[0]) * Math.sin(i * 0.021 + seed[1]);
651
+ const detail = Math.abs(Math.sin(i * 0.5 + seed[2]) * 0.6 + Math.sin(i * 0.17 + seed[3]) * 0.4);
652
+ const v = env * (0.45 + 0.55 * detail) * (0.8 + 0.2 * Math.sin(i * 1.7 + seed[4]));
653
+ return Math.max(0.12, Math.min(1, 0.12 + v * 1.05));
654
+ };
655
+
656
+ /**
657
+ * Sizes the bars to a count, keeping those already there; new ones take
658
+ * the resting take.
659
+ *
660
+ * @param {number} count How many bars.
661
+ */
662
+ const resizeBars = (count) => {
663
+ /**
664
+ * One of the bars' arrays at the new length.
665
+ *
666
+ * @param {Float32Array} from The old values.
667
+ * @param {(i: number) => number} fill A new bar's value.
668
+ * @returns {Float32Array} The new values.
669
+ */
670
+ const grow = (from, fill) => Float32Array.from({ length: count }, (_, i) => (i < from.length ? from[i] : fill(i)));
671
+ bars = {
672
+ cur: grow(bars.cur, (i) => take(i, REST)),
673
+ tgt: grow(bars.tgt, (i) => take(i, REST)),
674
+ peak: grow(bars.peak, (i) => Math.min(1, take(i, REST) + 0.06)),
675
+ hold: grow(bars.hold, () => 0),
676
+ fall: grow(bars.fall, () => 0),
677
+ };
678
+ data = new Uint8Array(count * 4);
679
+ };
680
+
681
+ /**
682
+ * Moves the bars on by a frame: a new take every so often, each bar quick
683
+ * to rise to it and slow to fall, kicked up as the playhead plays it and
684
+ * lifted under the pointer, and its peak mark held a moment before it drops.
685
+ *
686
+ * @param {number} now The time, ms.
687
+ * @param {number} dt Seconds since the last frame.
688
+ * @param {number} offset The first bar's left edge, CSS px.
689
+ * @param {number} play The playhead, CSS px across.
690
+ * @param {[number, number, number]} at The pointer: x, y (CSS px) and presence.
691
+ * @param {import('./audio.js').AudioReading | null} heard The sound, if one plays: the bars head for its spectrum, bass in the middle, and move as quickly as it does.
692
+ */
693
+ const stepBars = (now, dt, offset, play, at, heard) => {
694
+ const { cur, tgt, peak, hold, fall } = bars;
695
+ if (now - lastTune > every) {
696
+ lastTune = now;
697
+ const seed = Array.from({ length: 5 }, () => Math.random() * 100);
698
+ for (let i = 0; i < tgt.length; i++) tgt[i] = take(i, seed);
699
+ }
700
+ const on = heard ? heard.on : 0;
701
+ const rise = 1 - Math.exp(-dt / (0.09 - 0.05 * on));
702
+ const ease = 1 - Math.exp(-dt / (0.55 - 0.4 * on));
703
+ for (let i = 0; i < cur.length; i++) {
704
+ const x = offset + (i + 0.5) * step;
705
+ let base = tgt[i];
706
+ if (heard) {
707
+ const level = sampleAt(heard.spectrum, Math.abs(((i + 0.5) / cur.length) * 2 - 1));
708
+ // The spectrum's middle range, opened out: quiet bins low, loud ones tall.
709
+ const heardAt = Math.min(1, Math.max(0, (level - 0.3) / 0.55)) ** 1.5;
710
+ base += (Math.max(0.05, heardAt) - base) * on;
711
+ }
712
+ // Lifted under the pointer, and kicked up as the playhead plays them.
713
+ const lift = Math.max(scrub * at[2] * Math.exp(-((x - at[0]) ** 2) / 2400) * 0.35, Math.exp(-(((x - play) / (step * 2.5)) ** 2)) * 0.3);
714
+ const goal = Math.min(1, base + lift * (1 - base));
715
+ cur[i] += (goal - cur[i]) * (goal > cur[i] ? rise : ease);
716
+ if (cur[i] >= peak[i]) {
717
+ peak[i] = cur[i];
718
+ hold[i] = 0.7;
719
+ fall[i] = 0;
720
+ } else if (hold[i] > 0) {
721
+ hold[i] -= dt;
722
+ } else {
723
+ // Following sound, they drop sooner, as the bars do.
724
+ fall[i] += 0.9 * (1 + 3 * on) * dt;
725
+ peak[i] = Math.max(cur[i], peak[i] - fall[i] * dt);
726
+ }
727
+ }
728
+ };
729
+
730
+ /**
731
+ * Packs the heights and peak marks into the texture, 16 bits each.
732
+ *
733
+ * @param {WebGLRenderingContext | WebGL2RenderingContext} gl The context.
734
+ */
735
+ const upload = (gl) => {
736
+ const { cur, peak } = bars;
737
+ for (let i = 0; i < cur.length; i++) {
738
+ pack16(cur[i], data, i * 4);
739
+ pack16(peak[i], data, i * 4 + 2);
740
+ }
741
+ gl.activeTexture(gl.TEXTURE0 + BARS_UNIT);
742
+ gl.bindTexture(gl.TEXTURE_2D, heights);
743
+ gl.texSubImage2D(gl.TEXTURE_2D, 0, 0, 0, cur.length, 1, gl.RGBA, gl.UNSIGNED_BYTE, data);
744
+ };
745
+
746
+ const { stop } = createStage(el, {
747
+ fragment: BARS_FRAG,
748
+ setup(stage) {
749
+ const { gl, uniform } = stage;
750
+ gl.uniform1f(uniform('uStep'), step);
751
+ gl.uniform1f(uniform('uBar'), Math.min(step, Math.max(1, config.bar ?? 3)));
752
+ gl.uniform1f(uniform('uGlow'), Math.min(2, Math.max(0, config.glow ?? 1)));
753
+ gl.uniform1f(uniform('uGrain'), Math.min(1, Math.max(0, config.grain ?? 0.4)) * 0.45);
754
+ gl.uniform1f(uniform('uPeaks'), config.peaks === false ? 0 : 1);
755
+ gl.uniform1i(uniform('uBars'), BARS_UNIT);
756
+ const texture = makeTexture(gl, BARS_UNIT, gl.NEAREST);
757
+ heights = texture;
758
+ const freeInk = inkGradient(stage, el, startColors(config, BAR_COLORS));
759
+ sound = listen(stage, config.audio);
760
+ return () => {
761
+ sound?.stop();
762
+ freeInk();
763
+ gl.deleteTexture(texture);
764
+ };
765
+ },
766
+ resize({ gl, uniform, canvas, ratio }) {
767
+ const count = Math.max(8, Math.floor(canvas.width / ratio / step));
768
+ resizeBars(count);
769
+ gl.activeTexture(gl.TEXTURE0 + BARS_UNIT);
770
+ gl.bindTexture(gl.TEXTURE_2D, heights);
771
+ gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, count, 1, 0, gl.RGBA, gl.UNSIGNED_BYTE, data);
772
+ gl.uniform1f(uniform('uCount'), count);
773
+ gl.uniform4f(uniform('uQuiet'), ...quietUniform(config.quiet, canvas.width / canvas.height));
774
+ },
775
+ uniforms({ gl, uniform, canvas, ratio, still }, now, dt) {
776
+ const w = canvas.width / ratio;
777
+ const h = canvas.height / ratio;
778
+ const at = still ? /** @type {[number, number, number]} */ ([0, 0, 0]) : pointer(dt, w, h);
779
+ // The playhead: across in about ten seconds, then a pause off the edge;
780
+ // following a track, where the track is, and from there when it stops.
781
+ const heard = sound?.read(now, dt) ?? null;
782
+ const track = heard && heard.progress >= 0 ? heard.progress * w : null;
783
+ if (track === null && following) start = now - ((head + w * 0.2) / w / 0.1) * 1000;
784
+ following = track !== null;
785
+ const sweepAt = (((now - start) / 1000) * 0.1 % 1.4) * w - w * 0.2;
786
+ head = track === null ? sweepAt : head + (track - head) * (1 - Math.exp(-dt / 0.12));
787
+ const play = config.sweep === false ? -1e5 : still ? w * 0.62 : head;
788
+ if (!still) stepBars(now, dt, (w - bars.cur.length * step) / 2, play, at, heard);
789
+ upload(gl);
790
+ gl.uniform1f(uniform('uRatio'), ratio);
791
+ gl.uniform1f(uniform('uMax'), h * (config.height ?? 0.34));
792
+ gl.uniform1f(uniform('uPlay'), play);
793
+ gl.uniform1f(uniform('uTrail'), w * 0.22);
794
+ gl.uniform2f(uniform('uScrub'), at[0], at[2] * scrub);
795
+ gl.uniform1f(uniform('uSeed'), still ? 0 : Math.random() * 100);
796
+ },
797
+ });
798
+ return stop;
799
+ }