@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,210 @@
1
+ /**
2
+ * @module @arraypress/effects-astro/product-stack
3
+ *
4
+ * Product covers floating in 3D, in a fan, a stack or an arc
5
+ * (src/runtime/stack-poses.js places them). The covers are the page's own
6
+ * links and images, moved by CSS transforms, so they stay crisp, take
7
+ * focus and click through like any link. This module only decides which
8
+ * pose each cover is in and writes it; CSS eases between poses, floats the
9
+ * covers and sweeps the sheen, all on the compositor, so nothing runs on
10
+ * the main thread while the stack sits still.
11
+ *
12
+ * - Pointer: the stack leans towards it, wherever it is on the page.
13
+ * - Hover (or focus): the covers fan apart, and the one under the
14
+ * pointer (or focused) lifts forward, comes into focus and pushes its
15
+ * neighbours aside.
16
+ * - Depth of field: covers further from the one in front dim and lose a
17
+ * little colour; only the furthest blur, a little.
18
+ *
19
+ * With reduced motion it holds still and does not lean; hover and focus
20
+ * still lift a cover, without easing. On a touch screen it does not lean
21
+ * or fan: a tap is a tap on the link.
22
+ */
23
+
24
+ import { canHover, prefersReducedMotion } from './device.js';
25
+ import { inView } from './in-view.js';
26
+ import { frontOf, layoutOf, poseFilter, poseTransform, stackBounds, stackPoses } from './stack-poses.js';
27
+
28
+ /**
29
+ * @typedef {object} ProductStackConfig
30
+ * @property {'fan' | 'stack' | 'arc'} [layout='fan'] How the covers are laid out.
31
+ * @property {number} [spread=1] How far apart the covers sit, 0–2.
32
+ * @property {number} [tilt=12] How far the stack leans towards the pointer, degrees.
33
+ * @property {number} [depth=0.6] How much the covers behind dim and soften, 0–1.
34
+ * @property {boolean} [interactive=true] Lean, fan and lift for the pointer and keyboard.
35
+ * @property {number} [ratio=1] A cover's width over its height.
36
+ */
37
+
38
+ /**
39
+ * Lays out an element's covers and, unless it is not interactive, has
40
+ * them answer the pointer and keyboard. The element holds a `.ps-scene`
41
+ * with a `.ps-stage` in it, whose children are the covers.
42
+ *
43
+ * @param {HTMLElement} el The component's element.
44
+ * @param {ProductStackConfig} config The settings.
45
+ * @returns {() => void} Stops it.
46
+ */
47
+ export function productStack(el, config) {
48
+ const scene = /** @type {HTMLElement | null} */ (el.querySelector('.ps-scene'));
49
+ const stage = /** @type {HTMLElement | null} */ (el.querySelector('.ps-stage'));
50
+ if (!scene || !stage) return () => {};
51
+ const covers = /** @type {HTMLElement[]} */ ([...stage.children]);
52
+ const n = covers.length;
53
+ const layout = layoutOf(config.layout);
54
+ const spread = config.spread ?? 1;
55
+ const depth = config.depth ?? 0.6;
56
+ const ratio = config.ratio && config.ratio > 0 ? config.ratio : 1;
57
+ const tilt = config.tilt ?? 12;
58
+ const still = prefersReducedMotion();
59
+ let open = false;
60
+ let lifted = -1;
61
+
62
+ const bounds = stackBounds(layout, n, spread, ratio);
63
+ el.style.setProperty('--ps-wide', String(bounds.wide));
64
+ el.style.setProperty('--ps-tall', String(bounds.tall));
65
+ el.style.setProperty('--ps-ratio', String(ratio));
66
+ covers.forEach((cover, i) => {
67
+ cover.style.setProperty('--ps-i', String(i));
68
+ cover.querySelectorAll('img').forEach((img) => (img.draggable = false));
69
+ });
70
+
71
+ /** Writes every cover's pose for the state the stack is in, and marks the one in front. */
72
+ const pose = () => {
73
+ const front = lifted >= 0 ? lifted : Math.floor(frontOf(layout, n));
74
+ stackPoses(layout, n, { spread, open, lifted }).forEach((p, i) => {
75
+ covers[i].style.transform = poseTransform(p);
76
+ covers[i].style.filter = poseFilter(p, depth);
77
+ covers[i].classList.toggle('is-front', i === front);
78
+ covers[i].classList.toggle('is-lifted', i === lifted);
79
+ });
80
+ };
81
+
82
+ /**
83
+ * Moves to a new state, if it is one.
84
+ *
85
+ * @param {boolean} nextOpen Whether the covers fan apart.
86
+ * @param {number} nextLifted The cover lifted forward; -1 for none.
87
+ */
88
+ const set = (nextOpen, nextLifted) => {
89
+ if (nextOpen === open && nextLifted === lifted) return;
90
+ open = nextOpen;
91
+ lifted = nextLifted;
92
+ pose();
93
+ };
94
+
95
+ pose();
96
+ // Ease only from the first change on: the stack is in place when it appears.
97
+ const ready = requestAnimationFrame(() => el.classList.add('is-ready'));
98
+ /** @type {Array<() => void>} */
99
+ const stops = [() => cancelAnimationFrame(ready)];
100
+
101
+ /**
102
+ * Listens to an event, and remembers to stop.
103
+ *
104
+ * @param {EventTarget} target What to listen to.
105
+ * @param {string} type The event.
106
+ * @param {(e: any) => void} handler The listener.
107
+ */
108
+ const on = (target, type, handler) => {
109
+ target.addEventListener(type, handler, { passive: true });
110
+ stops.push(() => target.removeEventListener(type, handler));
111
+ };
112
+
113
+ /**
114
+ * The cover an event happened in.
115
+ *
116
+ * @param {Event} e The event.
117
+ * @returns {number} Its index; -1 when it was in none.
118
+ */
119
+ const coverOf = (e) => covers.findIndex((cover) => cover.contains(/** @type {Node | null} */ (e.target)));
120
+
121
+ if (config.interactive !== false) {
122
+ /** @param {PointerEvent} e The pointer coming over the stack, or moving over it. */
123
+ const hover = (e) => {
124
+ if (e.pointerType !== 'touch') set(true, coverOf(e));
125
+ };
126
+ on(scene, 'pointerover', hover);
127
+ on(scene, 'pointerleave', () => set(Boolean(scene.querySelector(':focus-visible')), -1));
128
+ on(scene, 'focusin', (e) => set(true, coverOf(e)));
129
+ on(scene, 'focusout', (/** @type {FocusEvent} */ e) => {
130
+ if (!scene.contains(/** @type {Node | null} */ (e.relatedTarget))) set(scene.matches(':hover'), -1);
131
+ });
132
+ if (!still && canHover() && tilt > 0) stops.push(lean(el, scene, tilt));
133
+ }
134
+
135
+ // Asleep while out of view: the float and sheen stop with it.
136
+ stops.push(inView(el, () => el.classList.remove('is-asleep'), false, () => el.classList.add('is-asleep')));
137
+ return () => stops.forEach((stop) => stop());
138
+ }
139
+
140
+ /**
141
+ * Leans a stack towards the pointer, anywhere on the page, while it is in
142
+ * view: `--ps-lean-x` / `--ps-lean-y` (degrees) on the element turn its
143
+ * stage, and `--ps-glint-x` / `--ps-glint-y` (-1 to 1) move the covers'
144
+ * gloss the other way, as a light would. One read and one write a frame at
145
+ * most, and only while the pointer moves.
146
+ *
147
+ * @param {HTMLElement} el The component's element.
148
+ * @param {HTMLElement} scene The box the stack sits in; it leans about its centre.
149
+ * @param {number} tilt The most it leans, degrees.
150
+ * @returns {() => void} Stops it.
151
+ */
152
+ function lean(el, scene, tilt) {
153
+ let x = 0;
154
+ let y = 0;
155
+ let inside = false;
156
+ let frame = 0;
157
+
158
+ /** Writes the lean for where the pointer last was. */
159
+ const write = () => {
160
+ frame = 0;
161
+ const box = scene.getBoundingClientRect();
162
+ const reach = { x: Math.max(window.innerWidth * 0.5, 1), y: Math.max(window.innerHeight * 0.5, 1) };
163
+ const nx = inside ? Math.max(-1, Math.min(1, (x - (box.left + box.width / 2)) / reach.x)) : 0;
164
+ const ny = inside ? Math.max(-1, Math.min(1, (y - (box.top + box.height / 2)) / reach.y)) : 0;
165
+ el.style.setProperty('--ps-lean-x', `${(-ny * tilt * 0.6).toFixed(2)}deg`);
166
+ el.style.setProperty('--ps-lean-y', `${(nx * tilt).toFixed(2)}deg`);
167
+ el.style.setProperty('--ps-glint-x', (-nx).toFixed(3));
168
+ el.style.setProperty('--ps-glint-y', (-ny).toFixed(3));
169
+ };
170
+
171
+ /** Books a write for the next frame, if none is booked. */
172
+ const book = () => {
173
+ frame ||= requestAnimationFrame(write);
174
+ };
175
+
176
+ /** @param {PointerEvent} e The move. */
177
+ const move = (e) => {
178
+ if (e.pointerType === 'touch') return;
179
+ x = e.clientX;
180
+ y = e.clientY;
181
+ inside = true;
182
+ book();
183
+ };
184
+
185
+ /** The pointer left the window: settle back. */
186
+ const leave = () => {
187
+ inside = false;
188
+ book();
189
+ };
190
+
191
+ /** Starts following the pointer. */
192
+ const listen = () => {
193
+ document.addEventListener('pointermove', move, { passive: true });
194
+ document.documentElement.addEventListener('pointerleave', leave);
195
+ };
196
+
197
+ /** Stops following the pointer. */
198
+ const quiet = () => {
199
+ document.removeEventListener('pointermove', move);
200
+ document.documentElement.removeEventListener('pointerleave', leave);
201
+ cancelAnimationFrame(frame);
202
+ frame = 0;
203
+ };
204
+
205
+ const watching = inView(el, listen, false, quiet);
206
+ return () => {
207
+ watching();
208
+ quiet();
209
+ };
210
+ }
@@ -0,0 +1,29 @@
1
+ import type { PaletteName } from './palettes.js';
2
+
3
+ export interface RadiantGlassConfig {
4
+ /** The lights' colours, the last the hot colour where they gather. Default amber, blue, white. */
5
+ colors?: string[];
6
+ /** A shared palette by name (`PALETTES`, any case), when there are no `colors`. Its colours light the glass; its glint is the hot colour. */
7
+ palette?: PaletteName | (string & {});
8
+ /** The ground. Default `#060818`. */
9
+ background?: string;
10
+ /** The tiles: four in a plus, three on a slant, or one large. Default `cross`. */
11
+ layout?: 'cross' | 'diagonal' | 'single';
12
+ /** Each tile's shape: a rounded square on its corner, square on, or round. Default `diamond`. */
13
+ shape?: 'diamond' | 'square' | 'circle';
14
+ /** Which side the cluster sits on, leaving the other for the copy. Default `left`. */
15
+ side?: 'left' | 'right';
16
+ /** The cluster's size; 1 fills the height. Default 1. */
17
+ size?: number;
18
+ /** How blurred the light through the glass is; 0 for clear. Default 1. */
19
+ frost?: number;
20
+ /** How strong the halo round each tile is; 0 for none. Default 1. */
21
+ glow?: number;
22
+ /** How bright the rim line is; 0 for none. Default 1. */
23
+ rim?: number;
24
+ /** How fast the lights drift. Default 1. */
25
+ speed?: number;
26
+ }
27
+
28
+ /** Starts radiant glass in `el`'s canvas. Returns a function that stops it. */
29
+ export function radiantGlass(el: HTMLElement, config: RadiantGlassConfig): () => void;
@@ -0,0 +1,282 @@
1
+ /**
2
+ * @module @arraypress/effects-astro/radiant-glass
3
+ *
4
+ * A cluster of frosted glass tiles, lit from behind: big soft lights drift
5
+ * behind the glass, and each tile shows them heavily blurred and bent, so
6
+ * it glows from inside while the ground and the gaps between the tiles
7
+ * stay dark. Each tile has a thin rim, cool on one side and warm on the
8
+ * other, and a soft halo of its own light round it.
9
+ *
10
+ * One pass and no textures: the tiles are rounded-box distance fields,
11
+ * and the lights are gaussians, so frosting one is only widening it (a
12
+ * blurred gaussian is a wider gaussian). Each light is a long streak
13
+ * with a different colour along each side, as a lens splits white light;
14
+ * they mix in Oklab and run to the hot colour where they gather, and the
15
+ * result is dithered so the long gradients do not band.
16
+ *
17
+ * The lights drift on their own and shift with the pointer. Reduced
18
+ * motion gets a still frame.
19
+ *
20
+ * Opaque: the effect is the whole background of what it fills. Without
21
+ * WebGL it removes its canvas and the element's background shows.
22
+ */
23
+
24
+ import { createStage } from './stage.js';
25
+ import { parseColor, toOklab, GLSL_OKLAB } from './color.js';
26
+ import { canHover } from './device.js';
27
+ import { pointerIn } from './pointer.js';
28
+ import { watchColors } from './mount.js';
29
+ import { pickColors } from './palettes.js';
30
+ import { GLSL_NOISE } from './glsl.js';
31
+
32
+ /**
33
+ * @typedef {object} RadiantGlassConfig
34
+ * @property {string[]} [colors] The lights' colours, the last the hot colour where they gather (default amber, blue, white).
35
+ * @property {string} [palette] A shared palette by name (PALETTES; any case), when there are no `colors`. Its colours light the glass; its glint is the hot colour.
36
+ * @property {string} [background='#060818'] The ground.
37
+ * @property {'cross' | 'diagonal' | 'single'} [layout='cross'] The tiles: four in a plus, three on a slant, or one large.
38
+ * @property {'diamond' | 'square' | 'circle'} [shape='diamond'] Each tile's shape: a rounded square on its corner, square on, or round.
39
+ * @property {'left' | 'right'} [side='left'] Which side the cluster sits on, leaving the other for the copy.
40
+ * @property {number} [size=1] The cluster's size; 1 fills the height.
41
+ * @property {number} [frost=1] How blurred the light through the glass is; 0 for clear.
42
+ * @property {number} [glow=1] How strong the halo round each tile is; 0 for none.
43
+ * @property {number} [rim=1] How bright the rim line is; 0 for none.
44
+ * @property {number} [speed=1] How fast the lights drift.
45
+ */
46
+
47
+ const FRAG = `
48
+ precision highp float;
49
+ uniform vec2 uRes;
50
+ uniform float uTime;
51
+ uniform vec2 uPtr;
52
+ uniform vec3 uGround;
53
+ uniform vec3 uLights[4];
54
+ uniform vec3 uHot;
55
+ uniform vec2 uCenter;
56
+ uniform float uFlip;
57
+ uniform float uUnit;
58
+ uniform vec3 uTiles[4];
59
+ uniform float uCount;
60
+ uniform float uAngle;
61
+ uniform float uRound;
62
+ uniform float uFrost;
63
+ uniform float uGlow;
64
+ uniform float uRim;
65
+ ${GLSL_OKLAB}
66
+ ${GLSL_NOISE}
67
+
68
+ // A rounded box's signed distance and its outward direction, in the box's frame.
69
+ vec3 roundBox(vec2 q, float b, float r) {
70
+ vec2 s = sign(q);
71
+ vec2 e = abs(q) - b + r;
72
+ vec2 out_ = max(e, 0.0);
73
+ float lo = length(out_);
74
+ vec2 n = lo > 0.0 ? out_ / lo : (e.x > e.y ? vec2(1.0, 0.0) : vec2(0.0, 1.0));
75
+ return vec3(lo + min(max(e.x, e.y), 0.0) - r, n * s);
76
+ }
77
+
78
+ // The lights at a point, widened by a blur: their weighted colour (Oklab) and their sum.
79
+ // Each is a long soft streak turning slowly; blurring one only widens it.
80
+ vec4 lights(vec2 x, float blur) {
81
+ vec3 lab = vec3(0.0);
82
+ float total = 0.0, sharp = 0.0;
83
+ float bb = blur * blur;
84
+ for (int i = 0; i < 5; i++) {
85
+ float k = float(i);
86
+ // The first, the broadest and strongest, keeps to the tile nearest the copy.
87
+ vec2 home = vec2(cos(k * 2.4) * uFlip, sin(k * 2.4)) * (0.2 + 0.1 * mod(k, 2.0));
88
+ vec2 drift = vec2(sin(uTime * (0.21 + k * 0.037) + k * 1.7), cos(uTime * (0.17 + k * 0.029) + k * 2.3)) * (i == 0 ? 0.1 : 0.2);
89
+ // Each leans its own way with the pointer, so the light shifts within the tiles rather than leaving them.
90
+ vec2 at = uCenter + (home + drift + uPtr * (mod(k, 2.0) - 0.5) * (0.8 + k * 0.2)) * uUnit;
91
+ float turn = k * 1.1 + uTime * (0.05 + k * 0.013);
92
+ vec2 d = mat2(cos(turn), sin(turn), -sin(turn), cos(turn)) * (x - at);
93
+ vec2 v = (i == 0 ? vec2(0.2, 0.12) : vec2(0.17 + 0.04 * mod(k, 3.0), 0.07 + 0.02 * mod(k, 2.0))) * uUnit;
94
+ v *= v;
95
+ vec2 w = v + bb;
96
+ float weight = exp(-0.5 * dot(d * d, 1.0 / w)) * sqrt(v.x * v.y / (w.x * w.y)) * (i == 0 ? 1.4 : 1.0);
97
+ // Two colours, one along each long side, as a lens splits white light;
98
+ // weighted to the strongest light more than its share, so two meet without going grey.
99
+ vec3 tint = mix(uLights[i - (i / 4) * 4], uLights[i - ((i + 1) / 4) * 4 + 1], smoothstep(-1.2, 1.2, d.y / sqrt(w.y)));
100
+ lab += tint * weight * weight;
101
+ sharp += weight * weight;
102
+ total += weight;
103
+ }
104
+ return vec4(lab / max(sharp, 1e-6), total);
105
+ }
106
+
107
+ // The lights as colour: their mix, running to the hot colour as they strengthen, at their strength.
108
+ vec3 shade(vec4 l, float gain) {
109
+ float s = l.a * gain;
110
+ vec3 lab = l.rgb;
111
+ // Richer where it is dim, so a fading light glows rather than greys or browns.
112
+ lab.yz *= 1.0 + 0.45 * (1.0 - smoothstep(0.1, 0.9, s));
113
+ lab = mix(lab, uHot, smoothstep(0.55, 1.5, s) * 0.9);
114
+ // A steep curve: a light holds its colour, then falls away to dark.
115
+ return fromOklab(lab) * (1.0 - exp(-pow(s, 1.6) * 2.6));
116
+ }
117
+
118
+ // Where the glass at a point of a tile looks: a touch magnified, and drawn
119
+ // in toward the cluster's middle, so every tile sees the lights.
120
+ vec2 view(vec2 x, vec2 c) { return uCenter + (c - uCenter) * 0.7 + (x - c) * 0.82; }
121
+
122
+ void main() {
123
+ vec2 p = (gl_FragCoord.xy - 0.5 * uRes) / uRes.y;
124
+ float px = 1.0 / uRes.y;
125
+ float cs = cos(uAngle), sn = sin(uAngle);
126
+ mat2 toTile = mat2(cs, -sn, sn, cs);
127
+
128
+ // The nearest tile: its distance, outward direction and centre; and how far the next one is.
129
+ float d = 1e3, next = 1e3;
130
+ vec2 n = vec2(0.0);
131
+ vec2 c = uCenter;
132
+ for (int i = 0; i < 4; i++) {
133
+ if (float(i) >= uCount) break;
134
+ vec2 at = uCenter + uTiles[i].xy * uUnit;
135
+ float b = 0.194 * uUnit * uTiles[i].z;
136
+ vec3 t = roundBox(toTile * (p - at), b, b * uRound);
137
+ if (t.x < d) { next = d; d = t.x; n = t.yz; c = at; } else next = min(next, t.x);
138
+ }
139
+ n = n * toTile;
140
+
141
+ vec3 col = uGround;
142
+ // The halo: the light just inside the nearest edge, spread and fading away
143
+ // from it; none in the gaps, where another tile is about as near.
144
+ if (d > 0.0) {
145
+ vec2 edge = p - n * d;
146
+ vec3 halo = shade(lights(view(edge, c), 0.16 * uUnit), 1.0);
147
+ float open = smoothstep(0.0, 0.16 * uUnit, next - d);
148
+ col += halo * exp(-d / (0.055 * uUnit)) * 0.38 * open * uGlow;
149
+ }
150
+ // Through the glass: the lights behind, frosted, and bent outward near the
151
+ // edge, so the rim gathers light from beyond it. Only where there is glass.
152
+ if (d < px) {
153
+ float inner = max(-d, 0.0);
154
+ float bend = 1.0 - clamp(inner / (0.07 * uUnit), 0.0, 1.0);
155
+ bend *= bend;
156
+ vec2 look = view(p, c) + n * bend * 0.09 * uUnit;
157
+ vec3 glass = shade(lights(look, 0.04 * uFrost * uUnit), 1.0);
158
+ // A dark seam just inside the rim, where the glass turns away.
159
+ glass *= mix(0.4, 1.0, smoothstep(0.0, 0.009 * uUnit, inner));
160
+ glass += uGround;
161
+ // The rim: a hairline, cool on the edges facing away from the copy, warm on
162
+ // those facing it, and brighter where the light behind is.
163
+ float line = 1.0 - smoothstep(0.0, 1.4 * px, abs(d + 0.8 * px));
164
+ if (line > 0.0) {
165
+ float face = dot(n, normalize(vec2(uFlip, 0.45))) * 0.5 + 0.5;
166
+ vec3 rimCol = fromOklab(mix(uLights[1], uLights[0], smoothstep(0.2, 0.8, face)));
167
+ glass += (rimCol + shade(lights(p, 0.12 * uUnit), 1.0)) * 0.7 * line * uRim;
168
+ }
169
+ col = mix(col, glass, smoothstep(px, -px, d));
170
+ }
171
+
172
+ col += (hash(gl_FragCoord.xy + fract(uTime * 7.0)) - 0.5) * (1.5 / 255.0);
173
+ gl_FragColor = vec4(col, 1.0);
174
+ }
175
+ `;
176
+
177
+ /** The default lights: amber, blue, and white where they gather. */
178
+ const COLORS = ['#ffb27a', '#4a68ff', '#fbf7ff'];
179
+
180
+ /** Each layout's tiles: x, y in units of the cluster, and a size. */
181
+ const LAYOUTS = {
182
+ cross: [
183
+ [0.355, 0, 1],
184
+ [0, 0.355, 1],
185
+ [0, -0.355, 1],
186
+ [-0.355, 0, 1],
187
+ ],
188
+ diagonal: [
189
+ [-0.2, 0.34, 0.9],
190
+ [0.14, -0.02, 1.05],
191
+ [0.48, -0.38, 0.9],
192
+ ],
193
+ single: [[0.12, 0, 1.7]],
194
+ };
195
+
196
+ /** Each shape: its turn in radians, and its corners' radius as a share of its half size. */
197
+ const SHAPES = {
198
+ diamond: [Math.PI / 4, 0.38],
199
+ square: [0, 0.3],
200
+ circle: [0, 1],
201
+ };
202
+
203
+ /**
204
+ * Starts radiant glass on an element: an opaque canvas inside it, filling it.
205
+ *
206
+ * @param {HTMLElement} el The element (its first canvas is drawn into).
207
+ * @param {RadiantGlassConfig} config The settings.
208
+ * @returns {() => void} Stops it.
209
+ */
210
+ export function radiantGlass(el, config) {
211
+ const colors = pickColors(config, COLORS, (p) => [...p.colors, p.glint]);
212
+ const start = performance.now();
213
+ const speed = config.speed ?? 1;
214
+ const ptr = { x: 0, y: 0 };
215
+ const tiles = LAYOUTS[config.layout ?? 'cross'] ?? LAYOUTS.cross;
216
+ const [angle, round] = SHAPES[config.shape ?? 'diamond'] ?? SHAPES.diamond;
217
+ // Everything mirrors when the cluster is on the right.
218
+ const flip = config.side === 'right' ? -1 : 1;
219
+
220
+ /**
221
+ * The lights and the hot colour as the shader takes them, in Oklab: up
222
+ * to four lights (repeated to fill), then the hot colour.
223
+ *
224
+ * @param {string[]} list The colours, the hot one last.
225
+ * @returns {{ lights: Float32Array, hot: Float32Array }} The uniforms' values.
226
+ */
227
+ const toUniforms = (list) => {
228
+ const lights = list.length > 1 ? list.slice(0, -1) : list;
229
+ return {
230
+ lights: new Float32Array([0, 1, 2, 3].flatMap((i) => toOklab(parseColor(lights[i % lights.length])))),
231
+ hot: new Float32Array(toOklab(parseColor(list[list.length - 1]))),
232
+ };
233
+ };
234
+ let lab = toUniforms(colors);
235
+
236
+ const { stage, stop } = createStage(el, {
237
+ fragment: FRAG,
238
+ alpha: false,
239
+ setup({ gl, uniform }) {
240
+ gl.uniform3fv(uniform('uGround'), parseColor(config.background ?? '#060818'));
241
+ gl.uniform3fv(uniform('uTiles'), new Float32Array([0, 1, 2, 3].flatMap((i) => (tiles[i] ? [tiles[i][0] * flip, tiles[i][1], tiles[i][2]] : [0, 0, 0]))));
242
+ gl.uniform1f(uniform('uCount'), tiles.length);
243
+ gl.uniform1f(uniform('uFlip'), flip);
244
+ gl.uniform1f(uniform('uAngle'), angle);
245
+ gl.uniform1f(uniform('uRound'), round);
246
+ gl.uniform1f(uniform('uFrost'), config.frost ?? 1);
247
+ gl.uniform1f(uniform('uGlow'), config.glow ?? 1);
248
+ gl.uniform1f(uniform('uRim'), config.rim ?? 1);
249
+ },
250
+ resize({ gl, uniform, canvas }) {
251
+ // The cluster fills the height on a wide screen and shrinks on a narrow one,
252
+ // its middle a little in from the edge so the outer tile is cropped.
253
+ const aspect = canvas.width / canvas.height;
254
+ const unit = (config.size ?? 1) * Math.min(1, Math.max(0.5, aspect / 1.68));
255
+ const x = 0.385 * unit - aspect / 2;
256
+ gl.uniform2f(uniform('uCenter'), x * flip, 0);
257
+ gl.uniform1f(uniform('uUnit'), unit);
258
+ },
259
+ uniforms({ gl, uniform, still }, now) {
260
+ // Started a little way in, where the lights sit well; a still frame stays there.
261
+ const t = 8 + (still ? 0 : ((now - start) / 1000) * speed);
262
+ const p = !still && canHover() ? pointerIn(el) : null;
263
+ const goal = p?.inside ? { x: (p.x - 0.5) * 0.3, y: (p.y - 0.5) * 0.3 } : { x: 0, y: 0 };
264
+ ptr.x += (goal.x - ptr.x) * 0.03;
265
+ ptr.y += (goal.y - ptr.y) * 0.03;
266
+ gl.uniform1f(uniform('uTime'), t);
267
+ gl.uniform2f(uniform('uPtr'), ptr.x, ptr.y);
268
+ gl.uniform3fv(uniform('uLights'), lab.lights);
269
+ gl.uniform3fv(uniform('uHot'), lab.hot);
270
+ },
271
+ });
272
+ if (!stage) return stop;
273
+
274
+ const unwatch = watchColors(el, (next) => {
275
+ lab = toUniforms(next.glint ? [...next.colors, next.glint] : next.colors);
276
+ if (stage.still) stage.redraw();
277
+ });
278
+ return () => {
279
+ stop();
280
+ unwatch();
281
+ };
282
+ }
@@ -0,0 +1,22 @@
1
+ /** A bright point in a picture. */
2
+ export interface Light {
3
+ /** Across the picture, 0–1 from the left. */
4
+ x: number;
5
+ /** Down the picture, 0–1 from the top. */
6
+ y: number;
7
+ /** How bright, 0–1. */
8
+ level: number;
9
+ /** Its colour around it, each channel 0–1, the brightest 1. */
10
+ rgb: [number, number, number];
11
+ }
12
+
13
+ /** The brightest local peaks in RGBA pixels, brightest first, at most `count` (28), over `threshold` (0.6), `spacing` (10) pixels apart. */
14
+ export function findLights(
15
+ data: Uint8ClampedArray,
16
+ width: number,
17
+ height: number,
18
+ options?: { count?: number; threshold?: number; spacing?: number },
19
+ ): Light[];
20
+
21
+ /** A small, blurred copy of a photo with bokeh discs over its lights (`colors` for them, or each light's own). Throws without a 2D canvas. */
22
+ export function backdrop(img: CanvasImageSource & { width: number; height: number }, options: { blur: number; bokeh: number; colors: string[] }): HTMLCanvasElement;