@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,504 @@
1
+ /**
2
+ * @module @arraypress/effects-astro/mosaic-reveal
3
+ *
4
+ * A slideshow whose pictures change through a GPU transition: a grid of
5
+ * tiles flipping over in a diagonal wave, the picture melting into sorted
6
+ * streaks that settle as the next, blocks coarsening and refining into the
7
+ * next, or a noise dissolve with a glowing, scorching edge. One fragment
8
+ * shader with the two pictures as textures and the change's progress.
9
+ *
10
+ * The pictures are real `<img>` elements, so they are read out, shown
11
+ * without WebGL, and shown before the canvas has drawn: the canvas lies
12
+ * over them once it has. With prefers-reduced-motion, or without WebGL,
13
+ * the images change with a short crossfade (or at once) instead.
14
+ *
15
+ * It sleeps between changes: the canvas is drawn only while one runs and
16
+ * after a resize. When to change (the timer, keys, swipes, dots) is
17
+ * src/runtime/slideshow.js.
18
+ */
19
+
20
+ import { createStage } from './stage.js';
21
+ import { animate } from './loop.js';
22
+ import { parseColor, toOklab, GLSL_OKLAB } from './color.js';
23
+ import { prefersReducedMotion } from './device.js';
24
+ import { watchColors } from './mount.js';
25
+ import { GLSL_NOISE } from './glsl.js';
26
+ import { pickColors } from './palettes.js';
27
+ import { loadImage, imageTexture, coverScale, GLSL_COVER } from './image.js';
28
+ import { slideshow, wrap } from './slideshow.js';
29
+
30
+ /**
31
+ * @typedef {'tiles' | 'pixel-sort' | 'pixelate' | 'dissolve'} MosaicTransition
32
+ */
33
+
34
+ /**
35
+ * @typedef {object} MosaicRevealConfig
36
+ * @property {MosaicTransition} [transition='tiles'] How one picture becomes the next.
37
+ * @property {number} [interval=5] Seconds each picture stays.
38
+ * @property {number} [duration=1.6] Seconds a change takes.
39
+ * @property {number} [tiles=9] Tiles across the longer side; also the coarsest blocks for `pixelate`.
40
+ * @property {boolean} [autoplay=true] Move on by itself.
41
+ * @property {string[]} [colors] The dissolve's glowing edge: its outer colour, then its core (default amber, cream).
42
+ * @property {string} [palette] A shared palette by name (PALETTES; any case), when there are no `colors`.
43
+ * @property {string} [background='#0c0b0a'] What shows between the tiles as they flip.
44
+ * @property {'fade' | 'swap'} [reducedMotion='fade'] With prefers-reduced-motion: a short crossfade, or a swap at once.
45
+ */
46
+
47
+ /** The shader's number for each transition. */
48
+ const MODES = { tiles: 0, 'pixel-sort': 1, pixelate: 2, dissolve: 3 };
49
+
50
+ /** The default glow: amber round a cream core. */
51
+ const COLORS = ['#ff6a1a', '#ffe2b0'];
52
+
53
+ /** How long a calm crossfade takes, ms. */
54
+ const FADE = 450;
55
+
56
+ const FRAG = `
57
+ precision highp float;
58
+ uniform vec2 uRes;
59
+ uniform sampler2D uFrom;
60
+ uniform sampler2D uTo;
61
+ uniform vec2 uFromScale;
62
+ uniform vec2 uToScale;
63
+ uniform float uProgress;
64
+ uniform float uMode;
65
+ uniform float uDir;
66
+ uniform vec2 uGrid;
67
+ uniform float uPx;
68
+ uniform float uSeed;
69
+ uniform vec3 uGround;
70
+ uniform vec3 uGlow[2];
71
+ ${GLSL_NOISE}
72
+ ${GLSL_OKLAB}
73
+ ${GLSL_COVER}
74
+
75
+ const float PI = 3.14159265;
76
+
77
+ // A picture at a 0–1 box coordinate (y up), cropped to cover the box.
78
+ vec3 pic(sampler2D t, vec2 scale, vec2 uv) {
79
+ vec2 c = coverUv(uv, scale);
80
+ return texture2D(t, vec2(c.x, 1.0 - c.y)).rgb;
81
+ }
82
+ vec3 from(vec2 uv) { return pic(uFrom, uFromScale, uv); }
83
+ vec3 to(vec2 uv) { return pic(uTo, uToScale, uv); }
84
+
85
+ // Tiles flipping over in a wave from one corner: the old picture on their
86
+ // fronts, the new on their backs, each lifting off the ground as it turns.
87
+ vec3 tiles(vec2 uv, float p) {
88
+ vec2 g = uv * uGrid;
89
+ vec2 cell = floor(g);
90
+ vec2 local = fract(g) - 0.5;
91
+ float d = (cell.x + uGrid.y - 1.0 - cell.y) / max(uGrid.x + uGrid.y - 2.0, 1.0);
92
+ d = uDir > 0.0 ? d : 1.0 - d;
93
+ d = d * 0.8 + hash(cell + uSeed) * 0.2;
94
+ float lp = clamp((p - d * 0.58) / 0.42, 0.0, 1.0);
95
+ if (lp <= 0.0) return from(uv);
96
+ if (lp >= 1.0) return to(uv);
97
+ lp = lp < 0.5 ? 4.0 * lp * lp * lp : 1.0 - pow(-2.0 * lp + 2.0, 3.0) * 0.5;
98
+ float a = lp * PI;
99
+ float c = cos(a), s = sin(a);
100
+ // Turning about its vertical axis, in perspective: the near edge grows.
101
+ float lift = 1.0 - 0.14 * s;
102
+ vec2 q = local / lift;
103
+ float den = c + q.x * s * 0.36;
104
+ float xs = q.x / den;
105
+ float ys = q.y * (1.0 - xs * s * 0.36);
106
+ float px = uGrid.y / uRes.y / lift;
107
+ float cover = abs(den) < 1e-4 ? 0.0 : clamp(min((0.5 - abs(xs)) * abs(den), 0.5 - abs(ys)) / px, 0.0, 1.0);
108
+ // Barely lifted, a tile still fills its cell: no seam round it.
109
+ cover = mix(1.0, cover, smoothstep(0.0, 0.3, s));
110
+ vec2 centre = (cell + 0.5) / uGrid;
111
+ vec3 ground = uGround * (0.85 + 0.15 * s) + 0.12 * mix(from(centre), to(centre), lp);
112
+ if (cover <= 0.0) return ground;
113
+ bool front = c > 0.0;
114
+ vec2 at = (cell + 0.5 + vec2(front ? xs : -xs, ys)) / uGrid;
115
+ vec3 col = front ? from(at) : to(at);
116
+ vec3 n = front ? vec3(-s, 0.0, c) : vec3(s, 0.0, -c);
117
+ float shade = mix(0.5, 1.0, abs(c));
118
+ float glint = pow(max(dot(n, normalize(vec3(-0.55, 0.3, 1.0))), 0.0), 16.0) * s * 0.4;
119
+ return mix(ground, col * shade + glint, cover);
120
+ }
121
+
122
+ // Six taps down a span of a column, sorted dark to light, and the colour
123
+ // at t (0 at the span's foot, 1 at its head) along the sorted run.
124
+ #define ORDER(a, b) if (a.w > b.w) { vec4 t_ = a; a = b; b = t_; }
125
+ // Off the top or bottom, a melt reads the picture mirrored rather than smearing its edge.
126
+ vec2 mirrorY(vec2 uv) {
127
+ return vec2(uv.x, 1.0 - abs(1.0 - abs(uv.y)));
128
+ }
129
+ vec4 tap(sampler2D t, vec2 scale, vec2 uv) {
130
+ vec3 c = pic(t, scale, mirrorY(uv));
131
+ return vec4(c, dot(c, vec3(0.2126, 0.7152, 0.0722)));
132
+ }
133
+ vec3 sortedRun(sampler2D t, vec2 scale, float x, float foot, float len, float at) {
134
+ float k = len / uRes.y;
135
+ float y = foot / uRes.y;
136
+ vec4 s0 = tap(t, scale, vec2(x, y + k * 0.0833));
137
+ vec4 s1 = tap(t, scale, vec2(x, y + k * 0.25));
138
+ vec4 s2 = tap(t, scale, vec2(x, y + k * 0.4167));
139
+ vec4 s3 = tap(t, scale, vec2(x, y + k * 0.5833));
140
+ vec4 s4 = tap(t, scale, vec2(x, y + k * 0.75));
141
+ vec4 s5 = tap(t, scale, vec2(x, y + k * 0.9167));
142
+ ORDER(s0, s5) ORDER(s1, s3) ORDER(s2, s4) ORDER(s1, s2) ORDER(s3, s4) ORDER(s0, s3)
143
+ ORDER(s2, s5) ORDER(s0, s1) ORDER(s2, s3) ORDER(s4, s5) ORDER(s1, s2) ORDER(s3, s4)
144
+ float i = clamp(at, 0.0, 1.0) * 5.0;
145
+ vec3 c = mix(s0.rgb, s1.rgb, clamp(i, 0.0, 1.0));
146
+ c = mix(c, s2.rgb, clamp(i - 1.0, 0.0, 1.0));
147
+ c = mix(c, s3.rgb, clamp(i - 2.0, 0.0, 1.0));
148
+ c = mix(c, s4.rgb, clamp(i - 3.0, 0.0, 1.0));
149
+ return mix(c, s5.rgb, clamp(i - 4.0, 0.0, 1.0));
150
+ }
151
+
152
+ // One picture melted into sorted streaks: its columns slide down and break
153
+ // into spans whose colours run dark to light. melt 0 is the picture.
154
+ vec3 melt(sampler2D t, vec2 scale, float melt, float fall) {
155
+ float w = 2.0 * uPx;
156
+ float col = floor(gl_FragCoord.x / w);
157
+ float x = (col + 0.5) * w / uRes.x;
158
+ // Neighbouring columns move together in bands, each a little its own way,
159
+ // so the spans line up into runs with ragged ends.
160
+ float band = noise(vec2(col * 0.035, uSeed));
161
+ float grain = hash(vec2(col, uSeed));
162
+ float y = gl_FragCoord.y + fall * melt * melt * uRes.y * 0.3 * (0.25 + 0.75 * band) * uDir;
163
+ float len = max(1.0, melt * uRes.y * (0.1 + 0.45 * band) * (0.85 + 0.3 * grain));
164
+ float off = (noise(vec2(col * 0.02, uSeed + 5.0)) + 0.1 * grain) * len;
165
+ float foot = floor((y + off) / len) * len - off;
166
+ vec3 sorted = sortedRun(t, scale, x, foot, len, (y - foot) / len);
167
+ vec3 plain = pic(t, scale, mirrorY(vec2(gl_FragCoord.x, y) / uRes));
168
+ return mix(plain, sorted, smoothstep(0.0, 0.2, melt));
169
+ }
170
+
171
+ // Each column melts until its own moment, turns to the next picture's
172
+ // streaks, and those settle.
173
+ vec3 pixelSort(float p) {
174
+ float w = 2.0 * uPx;
175
+ float col = floor(gl_FragCoord.x / w);
176
+ float turn = 0.5 + (noise(vec2(col * 0.06, uSeed + 3.0)) - 0.5) * 0.36;
177
+ float e = p < turn ? p / turn : (1.0 - p) / (1.0 - turn);
178
+ e = e * e * (3.0 - 2.0 * e);
179
+ return p < turn ? melt(uFrom, uFromScale, e, 1.0) : melt(uTo, uToScale, e, -1.0);
180
+ }
181
+
182
+ // Blocks that coarsen in steps to the tile grid and refine again, the
183
+ // coarsest blocks turning to the next picture one by one at the peak.
184
+ vec3 pixelate(float p) {
185
+ float m = 1.0 - abs(2.0 * p - 1.0);
186
+ m = m * m * (3.0 - 2.0 * m);
187
+ float steps = floor(log2(max(max(uRes.x, uRes.y) / max(uGrid.x, uGrid.y), 2.0)) + 0.5);
188
+ float coarsest = exp2(steps);
189
+ float block = exp2(floor(m * steps + 0.5));
190
+ vec2 frag = gl_FragCoord.xy - uRes * 0.5;
191
+ vec2 at = ((floor(frag / block) + 0.5) * block + uRes * 0.5) / uRes;
192
+ float turn = 0.5 + (hash(floor(frag / coarsest) + uSeed) - 0.5) * 0.22;
193
+ return p < turn ? from(at) : to(at);
194
+ }
195
+
196
+ // The new picture eating through the old along noise, the old scorching
197
+ // just ahead of a glowing edge.
198
+ vec3 dissolve(vec2 uv, float p) {
199
+ vec2 a = vec2(uv.x * uRes.x / uRes.y, uv.y);
200
+ float n = fbm(a * 2.2 + uSeed) * 0.82 + noise(a * 26.0 - uSeed) * 0.18;
201
+ n = (n - 0.3) / 0.4;
202
+ float w = 0.05;
203
+ float t = mix(-0.5, 1.5, p);
204
+ float d = n - t;
205
+ float fresh = smoothstep(0.004, -0.004, d);
206
+ vec3 old = from(uv) * mix(1.0, 0.18, smoothstep(2.5 * w, 0.0, d));
207
+ vec3 col = mix(old, to(uv), fresh);
208
+ float g = d > 0.0 ? exp(-d / w * 2.6) : exp(d / w * 9.0);
209
+ vec3 glow = fromOklab(mix(uGlow[0], uGlow[1], smoothstep(0.35, 1.0, g)));
210
+ return col + glow * g * 1.6;
211
+ }
212
+
213
+ void main() {
214
+ vec2 uv = gl_FragCoord.xy / uRes;
215
+ float p = uProgress;
216
+ vec3 col;
217
+ if (p <= 0.0) col = from(uv);
218
+ else if (p >= 1.0) col = to(uv);
219
+ else if (uMode < 0.5) col = tiles(uv, p);
220
+ else if (uMode < 1.5) col = pixelSort(p);
221
+ else if (uMode < 2.5) col = pixelate(p);
222
+ else col = dissolve(uv, p);
223
+ gl_FragColor = vec4(col, 1.0);
224
+ }
225
+ `;
226
+
227
+ /**
228
+ * The tile grid for a box: `tiles` across its longer side and as many
229
+ * along the shorter as keeps them square, at least one.
230
+ *
231
+ * @param {number} tiles Tiles across the longer side.
232
+ * @param {number} aspect The box's width over its height.
233
+ * @returns {[number, number]} Tiles across and down.
234
+ */
235
+ export function tileGrid(tiles, aspect) {
236
+ const n = Math.max(1, Math.round(tiles));
237
+ return aspect >= 1 ? [n, Math.max(1, Math.round(n / aspect))] : [Math.max(1, Math.round(n * aspect)), n];
238
+ }
239
+
240
+ /**
241
+ * The dissolve's glow as `uGlow` takes it: its outer colour and its core,
242
+ * in Oklab, so the shader blends them there. One colour serves as both.
243
+ *
244
+ * @param {string[]} colors The outer colour, then the core.
245
+ * @returns {Float32Array} Six channels.
246
+ */
247
+ export function glowUniform(colors) {
248
+ return new Float32Array([colors[0], colors[1] ?? colors[0]].flatMap((c) => toOklab(parseColor(c))));
249
+ }
250
+
251
+ /**
252
+ * @typedef {object} Picture
253
+ * @property {WebGLTexture} texture It, on the GPU.
254
+ * @property {number} aspect Its width over its height.
255
+ */
256
+
257
+ /**
258
+ * Starts a mosaic slideshow on an element: its `img[data-slide]` pictures,
259
+ * a canvas laid over them, and the `[data-slide-dot]` and `[data-slide-live]` parts
260
+ * slideshow.js runs.
261
+ *
262
+ * @param {HTMLElement} el The element.
263
+ * @param {MosaicRevealConfig} config The settings.
264
+ * @returns {() => void} Stops it.
265
+ */
266
+ export function mosaicReveal(el, config) {
267
+ const slides = /** @type {HTMLImageElement[]} */ ([...el.querySelectorAll('img[data-slide]')]);
268
+ if (slides.length < 2) return () => {};
269
+ const duration = Math.max(0.1, config.duration ?? 1.6) * 1000;
270
+ const reduced = prefersReducedMotion();
271
+ let glow = glowUniform(pickColors(config, COLORS, (palette) => [palette.colors[0], palette.glint]));
272
+ /** Where the slideshow is: the picture on show, and the change being drawn. */
273
+ const state = {
274
+ current: 0,
275
+ /** @type {{ from: Picture, to: Picture, dir: 1 | -1, start: number, seed: number, done: () => void } | null} */
276
+ run: null,
277
+ stopped: false,
278
+ };
279
+ /** @type {Map<number, Promise<Picture | null>>} */
280
+ const pictures = new Map();
281
+ /** @type {Picture | null} */
282
+ let shown = null;
283
+ let retired = false;
284
+
285
+ /**
286
+ * Shows one image element, laid over any other left showing, and hides
287
+ * the rest from sight and from assistive technology.
288
+ *
289
+ * @param {number} i The slide.
290
+ * @param {number} [keep] A slide to leave showing beneath it.
291
+ */
292
+ const present = (i, keep = -1) => {
293
+ slides.forEach((img, k) => {
294
+ img.hidden = k !== i && k !== keep;
295
+ img.classList.toggle('is-front', k === i);
296
+ });
297
+ };
298
+
299
+ /**
300
+ * A change made with the image elements alone: a crossfade, or a swap
301
+ * when `ms` is 0.
302
+ *
303
+ * @param {number} to The slide coming in.
304
+ * @param {number} from The slide going.
305
+ * @param {number} ms How long the fade takes.
306
+ * @returns {Promise<void>} Settles when it is done.
307
+ */
308
+ const fade = async (to, from, ms) => {
309
+ const img = slides[to];
310
+ present(to, from);
311
+ await img.decode().catch(() => {});
312
+ if (ms > 0 && !state.stopped) await img.animate([{ opacity: 0 }, { opacity: 1 }], { duration: ms, easing: 'ease' }).finished.catch(() => {});
313
+ if (!state.stopped && state.current === to) present(to);
314
+ };
315
+
316
+ /**
317
+ * Loads the image element's picture ahead of time, for a crossfade.
318
+ *
319
+ * @param {number} i The slide.
320
+ */
321
+ const warm = (i) => {
322
+ loadImage(slides[wrap(i, slides.length)].src).catch(() => {});
323
+ };
324
+
325
+ const calm = reduced ? (config.reducedMotion === 'swap' ? 0 : FADE) : FADE;
326
+ /**
327
+ * Without the GPU: crossfade (or swap), and load the next picture.
328
+ *
329
+ * @param {number} to The slide coming in.
330
+ * @param {number} from The slide going.
331
+ * @param {1 | -1} dir Which way it went.
332
+ * @returns {Promise<void>} Settles when it is done.
333
+ */
334
+ const fallback = (to, from, dir) => {
335
+ state.current = to;
336
+ warm(to + dir);
337
+ return fade(to, from, calm);
338
+ };
339
+
340
+ // With reduced motion the images do it all: no canvas, no GPU context.
341
+ if (reduced) el.querySelector('canvas')?.remove();
342
+ /** @type {{ stage: import('./stage.js').Stage | null, stop: () => void }} */
343
+ const gpu = reduced ? { stage: null, stop: () => {} } : createStage(el, {
344
+ fragment: FRAG,
345
+ alpha: false,
346
+ still: true,
347
+ setup({ gl, uniform }) {
348
+ gl.uniform1i(uniform('uFrom'), 0);
349
+ gl.uniform1i(uniform('uTo'), 1);
350
+ gl.uniform1f(uniform('uMode'), MODES[config.transition ?? 'tiles'] ?? 0);
351
+ gl.uniform3fv(uniform('uGround'), parseColor(config.background ?? '#0c0b0a'));
352
+ return () => {
353
+ for (const picture of pictures.values()) picture.then((p) => p && gl.deleteTexture(p.texture));
354
+ };
355
+ },
356
+ uniforms({ gl, uniform, canvas, ratio }) {
357
+ const box = canvas.width / canvas.height;
358
+ const run = state.run;
359
+ const from = run ? run.from : shown;
360
+ const to = run ? run.to : shown;
361
+ if (!from || !to) return;
362
+ gl.activeTexture(gl.TEXTURE0);
363
+ gl.bindTexture(gl.TEXTURE_2D, from.texture);
364
+ gl.activeTexture(gl.TEXTURE1);
365
+ gl.bindTexture(gl.TEXTURE_2D, to.texture);
366
+ gl.uniform2fv(uniform('uFromScale'), coverScale(from.aspect, box));
367
+ gl.uniform2fv(uniform('uToScale'), coverScale(to.aspect, box));
368
+ gl.uniform1f(uniform('uProgress'), run ? Math.min(1, (performance.now() - run.start) / duration) : 0);
369
+ gl.uniform1f(uniform('uDir'), run?.dir ?? 1);
370
+ gl.uniform1f(uniform('uSeed'), run?.seed ?? 0);
371
+ gl.uniform2fv(uniform('uGrid'), tileGrid(config.tiles ?? 9, box));
372
+ gl.uniform1f(uniform('uPx'), ratio);
373
+ gl.uniform3fv(uniform('uGlow'), glow);
374
+ },
375
+ });
376
+ const { stage } = gpu;
377
+
378
+ /**
379
+ * A slide's picture on the GPU, loaded once.
380
+ *
381
+ * @param {number} i The slide.
382
+ * @returns {Promise<Picture | null>} The picture, or null when it cannot be loaded.
383
+ */
384
+ const picture = (i) => {
385
+ const k = wrap(i, slides.length);
386
+ let found = pictures.get(k);
387
+ if (!found) {
388
+ found = loadImage(slides[k].src).then(
389
+ (img) => {
390
+ if (state.stopped || !stage) return null;
391
+ const { gl } = stage;
392
+ gl.activeTexture(gl.TEXTURE0);
393
+ const texture = imageTexture(gl, img, { mipmap: true });
394
+ return texture && { texture, aspect: img.naturalWidth / img.naturalHeight };
395
+ },
396
+ () => null,
397
+ );
398
+ pictures.set(k, found);
399
+ }
400
+ return found;
401
+ };
402
+
403
+ /**
404
+ * Lets go of the pictures not on show, about to be, or just before:
405
+ * a long gallery keeps three on the GPU, not all of them.
406
+ */
407
+ const trim = () => {
408
+ const keep = new Set([state.current, wrap(state.current + 1, slides.length), wrap(state.current - 1, slides.length)]);
409
+ for (const [k, found] of pictures) {
410
+ if (keep.has(k)) continue;
411
+ pictures.delete(k);
412
+ found.then((p) => p && stage?.gl.deleteTexture(p.texture));
413
+ }
414
+ };
415
+
416
+ /** Gives up on the GPU (a picture it cannot load): the image elements show from now on. */
417
+ const retire = () => {
418
+ retired = true;
419
+ el.classList.remove('is-drawn');
420
+ gpu.stop();
421
+ stage?.gl.getExtension('WEBGL_lose_context')?.loseContext();
422
+ stage?.canvas.remove();
423
+ };
424
+
425
+ /**
426
+ * A change drawn on the GPU, once both pictures are there.
427
+ *
428
+ * @param {number} to The slide coming in.
429
+ * @param {number} from The slide going.
430
+ * @param {1 | -1} dir Which way it went.
431
+ * @returns {Promise<void>} Settles when it is done.
432
+ */
433
+ const transition = async (to, from, dir) => {
434
+ present(to);
435
+ const [a, b] = await Promise.all([picture(from), picture(to)]);
436
+ if (state.stopped) return;
437
+ if (!a || !b || !el.classList.contains('is-drawn')) {
438
+ retire();
439
+ return fallback(to, from, dir);
440
+ }
441
+ picture(to + dir);
442
+ await new Promise((done) => {
443
+ state.run = { from: a, to: b, dir, start: performance.now(), seed: Math.random() * 100, done: () => done(undefined) };
444
+ });
445
+ state.current = to;
446
+ shown = b;
447
+ trim();
448
+ };
449
+
450
+ /** Draws the change while one runs, and finishes it at the end. */
451
+ const frame = () => {
452
+ const run = state.run;
453
+ if (!run || !stage) return;
454
+ stage.redraw();
455
+ if (performance.now() - run.start < duration) return;
456
+ state.run = null;
457
+ run.done();
458
+ };
459
+
460
+ /**
461
+ * Lays the canvas over the images once the picture on show is on the
462
+ * GPU (a change made before then is a crossfade, and tries again).
463
+ */
464
+ const reveal = () => {
465
+ const i = state.current;
466
+ picture(i).then((first) => {
467
+ if (!first || !stage || state.stopped || state.current !== i || el.classList.contains('is-drawn')) return;
468
+ shown = first;
469
+ stage.redraw();
470
+ el.classList.add('is-drawn');
471
+ picture(i + 1);
472
+ });
473
+ };
474
+
475
+ const stopFrames = stage ? animate(el, frame) : () => {};
476
+ if (stage) reveal();
477
+ else warm(1);
478
+
479
+ const show = slideshow(el, {
480
+ count: slides.length,
481
+ interval: config.interval ?? 5,
482
+ autoplay: config.autoplay ?? true,
483
+ describe: (i) => slides[i].alt,
484
+ show: (to, from, dir) => {
485
+ if (stage && el.classList.contains('is-drawn')) return transition(to, from, dir);
486
+ const done = fallback(to, from, dir);
487
+ if (stage && !retired) reveal();
488
+ return done;
489
+ },
490
+ });
491
+
492
+ const unwatch = watchColors(el, (next) => {
493
+ glow = glowUniform(next.glint ? [next.colors[0], next.glint] : next.colors);
494
+ });
495
+
496
+ return () => {
497
+ state.stopped = true;
498
+ state.run?.done();
499
+ show.stop();
500
+ stopFrames();
501
+ unwatch();
502
+ gpu.stop();
503
+ };
504
+ }
@@ -0,0 +1,2 @@
1
+ /** Replays an element's one-shot motion (`is-bumped` by default, `is-active` for `.fx-heartbeat`). */
2
+ export function bump(el: Element | null, state?: string): void;
@@ -0,0 +1,21 @@
1
+ /**
2
+ * @module @arraypress/effects-astro/motion
3
+ *
4
+ * The script side of motion.css: replaying a one-shot motion. A class
5
+ * that is already there does not restart its animation, so the class is
6
+ * taken off, a reflow forced, and put back.
7
+ */
8
+
9
+ /**
10
+ * Plays an element's one-shot motion again: `.fx-bump` by default (a cart
11
+ * count going up), or `is-active` for `.fx-heartbeat`.
12
+ *
13
+ * @param {Element | null} el The element.
14
+ * @param {string} [state='is-bumped'] The class that plays it.
15
+ */
16
+ export function bump(el, state = 'is-bumped') {
17
+ if (!(el instanceof HTMLElement)) return;
18
+ el.classList.remove(state);
19
+ void el.offsetWidth;
20
+ el.classList.add(state);
21
+ }
@@ -0,0 +1,13 @@
1
+ /** An element's `data-effect-config`, parsed; empty when missing or invalid. */
2
+ export function readConfig(el: HTMLElement): Record<string, unknown>;
3
+
4
+ /**
5
+ * Registers an effect: `setup` runs once for every element matching
6
+ * `selector`, now, after each Astro view transition and whenever one is
7
+ * added to the page, with its parsed `data-effect-config`. The function it
8
+ * returns runs when the element leaves the page.
9
+ */
10
+ export function mount<T>(selector: string, setup: (el: HTMLElement, config: T) => (() => void) | void): void;
11
+
12
+ /** Calls `apply` with an effect's `data-colors` now (if set) and whenever it changes. Returns a function that stops following. */
13
+ export function watchColors(el: HTMLElement, apply: (next: { colors: string[]; glint?: string }) => void): () => void;
@@ -0,0 +1,120 @@
1
+ /**
2
+ * @module @arraypress/effects-astro/mount
3
+ *
4
+ * Starts every instance of an effect on the page, and starts them again
5
+ * after an Astro view transition swaps the page in. Elements added to the
6
+ * page later (by a script, a template, a client-side list) start too, and
7
+ * elements taken out stop, so a page can bring effects in and out as it
8
+ * scrolls and never hold more WebGL contexts than it shows. Each component renders
9
+ * an element with its settings as JSON in `data-effect-config`; the setup
10
+ * function is called once per element with those settings, and the
11
+ * function it returns is called when the element leaves the page.
12
+ */
13
+
14
+ /** @type {Map<string, (el: HTMLElement, config: any) => (() => void) | void>} */
15
+ const effects = new Map();
16
+ /** For each registered selector, its running elements and how to stop each: one element can carry several effects. */
17
+ /** @type {Map<string, Map<HTMLElement, () => void>>} */
18
+ const running = new Map();
19
+ let listening = false;
20
+ let queued = false;
21
+
22
+ /**
23
+ * Reads an element's settings.
24
+ *
25
+ * @param {HTMLElement} el The element.
26
+ * @returns {Record<string, unknown>} Its `data-effect-config`, parsed; empty
27
+ * when missing or not valid JSON.
28
+ */
29
+ export function readConfig(el) {
30
+ try {
31
+ return JSON.parse(el.dataset.effectConfig ?? '{}') ?? {};
32
+ } catch {
33
+ return {};
34
+ }
35
+ }
36
+
37
+ /** Sets up every registered effect's elements that are not running yet, and stops those that left the page. */
38
+ function scan() {
39
+ for (const [selector, setup] of effects) {
40
+ const live = running.get(selector) ?? new Map();
41
+ running.set(selector, live);
42
+ for (const [el, stop] of live) {
43
+ if (!el.isConnected) {
44
+ stop();
45
+ live.delete(el);
46
+ }
47
+ }
48
+ document.querySelectorAll(selector).forEach((node) => {
49
+ const el = /** @type {HTMLElement} */ (node);
50
+ if (live.has(el)) return;
51
+ live.set(el, setup(el, readConfig(el)) ?? (() => {}));
52
+ });
53
+ }
54
+ }
55
+
56
+ /**
57
+ * Scans again, once a frame at most, when elements are added to or taken
58
+ * out of the page. Text changing (a counter, a typewriter) is ignored.
59
+ */
60
+ function watchPage() {
61
+ if (typeof MutationObserver === 'undefined') return;
62
+ new MutationObserver((records) => {
63
+ if (queued) return;
64
+ const elements = records.some((record) =>
65
+ [...record.addedNodes, ...record.removedNodes].some((node) => node.nodeType === Node.ELEMENT_NODE),
66
+ );
67
+ if (!elements) return;
68
+ queued = true;
69
+ requestAnimationFrame(() => {
70
+ queued = false;
71
+ scan();
72
+ });
73
+ }).observe(document.documentElement, { childList: true, subtree: true });
74
+ }
75
+
76
+ /**
77
+ * Registers an effect: `setup` runs for every element matching `selector`,
78
+ * now, after each view transition, and whenever one is added to the page.
79
+ *
80
+ * @template T
81
+ * @param {string} selector Matches the effect's elements.
82
+ * @param {(el: HTMLElement, config: T) => (() => void) | void} setup Starts
83
+ * the effect on one element; returns a function that stops it.
84
+ */
85
+ export function mount(selector, setup) {
86
+ if (typeof document === 'undefined') return;
87
+ effects.set(selector, setup);
88
+ if (!listening) {
89
+ listening = true;
90
+ document.addEventListener('astro:page-load', scan);
91
+ watchPage();
92
+ }
93
+ if (document.readyState === 'loading') document.addEventListener('DOMContentLoaded', scan, { once: true });
94
+ else scan();
95
+ }
96
+
97
+ /**
98
+ * Follows an effect's `data-colors` attribute (JSON `{ colors, glint? }`,
99
+ * written by `setColors`): calls `apply` with it now, if set, and whenever
100
+ * it changes.
101
+ *
102
+ * @param {HTMLElement} el The effect's element.
103
+ * @param {(next: { colors: string[], glint?: string }) => void} apply Takes the new colours.
104
+ * @returns {() => void} Stops following.
105
+ */
106
+ export function watchColors(el, apply) {
107
+ /** Reads the attribute and passes it on, if it holds colours. */
108
+ const read = () => {
109
+ try {
110
+ const next = JSON.parse(el.dataset.colors ?? 'null');
111
+ if (next?.colors?.length) apply(next);
112
+ } catch {
113
+ // Not JSON: keep the colours there are.
114
+ }
115
+ };
116
+ const observer = new MutationObserver(read);
117
+ observer.observe(el, { attributes: true, attributeFilter: ['data-colors'] });
118
+ read();
119
+ return () => observer.disconnect();
120
+ }
@@ -0,0 +1,31 @@
1
+ import type { PaletteName } from './palettes.js';
2
+
3
+ export interface NebulaLogoConfig {
4
+ /** A word (or a character) to fill with gas. */
5
+ text?: string;
6
+ /** Its CSS font, without the size. Default: the page's, bold. */
7
+ font?: string;
8
+ /** An image (an SVG logo, a PNG) to fill instead. */
9
+ src?: string;
10
+ /** Where the shape sits, each 0–1 from the top left. Default the right half. */
11
+ area?: { x: number; y: number; width: number; height: number };
12
+ /** The gas's palette, three colours; the first, darkened, is also the thin gas's. Default teal, amber, rose. */
13
+ colors?: string[];
14
+ /** A shared palette by name (`PALETTES`, any case), when there are no `colors`. */
15
+ palette?: PaletteName | (string & {});
16
+ /** The ground. Default `#05070a`. */
17
+ background?: string;
18
+ /** How fast the gas drifts. Default 1. */
19
+ speed?: number;
20
+ /** The rim and the halo leaking past the edge; 0 for neither. Default 1. */
21
+ glow?: number;
22
+ /** How many stars twinkle inside; 0 for none. Default 1. */
23
+ stars?: number;
24
+ /** How much the pointer stirs and brightens the gas; 0 for not at all. Default 1. */
25
+ stir?: number;
26
+ /** Share of the pixel ratio the gas is drawn at: it is soft, so a little below full costs nothing visible and saves most of the work. Default 0.6. */
27
+ scale?: number;
28
+ }
29
+
30
+ /** Starts a nebula logo in `el`'s canvas. Returns a function that stops it. */
31
+ export function nebulaLogo(el: HTMLElement, config: NebulaLogoConfig): () => void;