@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,91 @@
1
+ /**
2
+ * @module @arraypress/effects-astro/paper-look
3
+ *
4
+ * A Paper look on the page: its plain props (src/runtime/paper-looks.js)
5
+ * turned into Paper's settings and run as a Paper shader
6
+ * (src/runtime/paper.js). A prop left out keeps its preset's setting, so
7
+ * with no props a look is its preset. Shared by every look's component
8
+ * through src/components/PaperBackground.astro.
9
+ *
10
+ * `quiet` dims the shader towards its `background` behind copy, with a
11
+ * mask on the canvas over the element's own background, since Paper's
12
+ * shaders are run unchanged.
13
+ */
14
+
15
+ import { paperSettings, paperShader } from './paper.js';
16
+ import { pickColors } from './palettes.js';
17
+ import { PAPER_LOOKS } from './paper-looks.js';
18
+
19
+ /**
20
+ * @typedef {import('./paper-look.js').PaperLookConfig} PaperLookConfig
21
+ * @typedef {import('./paper.js').PaperConfig} PaperConfig
22
+ */
23
+
24
+ /** The props every look shares, and the Paper settings they set; a pair sets two from `[x, y]`. */
25
+ const SHARED = /** @type {Record<string, string | [string, string]>} */ ({ background: 'colorBack', zoom: 'scale', rotation: 'rotation', offset: ['offsetX', 'offsetY'] });
26
+
27
+ /**
28
+ * A look's props as a Paper shader's config: its shader, its preset (the
29
+ * named one or the look's), and Paper's settings for the props given.
30
+ *
31
+ * @param {PaperLookConfig} config The look and its props.
32
+ * @returns {PaperConfig} The shader's config.
33
+ * @throws {Error} When there is no such look.
34
+ */
35
+ export function paperLookSettings(config) {
36
+ const look = PAPER_LOOKS[config.look];
37
+ if (!look) throw new Error(`No Paper look "${config.look}"`);
38
+ /** @type {Record<string, unknown>} */
39
+ const params = {};
40
+ for (const [prop, setting] of Object.entries({ ...SHARED, ...look.props })) {
41
+ const value = /** @type {Record<string, unknown>} */ (config)[prop];
42
+ if (value === undefined) continue;
43
+ if (Array.isArray(setting)) setting.forEach((name, i) => (params[name] = /** @type {unknown[]} */ (value)[i]));
44
+ else params[setting] = value;
45
+ }
46
+ const colors = pickColors(config, []);
47
+ return {
48
+ shader: look.shader,
49
+ preset: config.preset ?? look.preset,
50
+ params,
51
+ ...(colors.length ? { colors } : {}),
52
+ ...(config.speed !== undefined ? { speed: config.speed } : {}),
53
+ };
54
+ }
55
+
56
+ /**
57
+ * The CSS mask that dims a canvas behind copy: `1 - depth` at the quiet
58
+ * area's centre, rising to full by its edge. Sized in the element's
59
+ * height (`cqh`), as the effects' own quiet areas are.
60
+ *
61
+ * @param {NonNullable<PaperLookConfig['quiet']>} quiet The quiet area.
62
+ * @returns {string} The mask.
63
+ */
64
+ export function quietMask({ x = 0.5, y = 0.5, radius = 0.8, depth = 0.72 }) {
65
+ return `radial-gradient(circle calc(${radius} * 100cqh) at ${x * 100}% ${y * 100}%, rgb(0 0 0 / ${Number((1 - depth).toFixed(3))}) calc(${radius * 0.06} * 100cqh), #000)`;
66
+ }
67
+
68
+ /**
69
+ * Starts a Paper look on an element: the shader in its canvas, the
70
+ * element's background the shader's (what shows through the quiet area),
71
+ * and its quiet area masked.
72
+ *
73
+ * @param {HTMLElement} el The element (its first canvas is drawn into).
74
+ * @param {PaperLookConfig} config The look and its props.
75
+ * @returns {() => void} Stops it.
76
+ */
77
+ export function paperLook(el, config) {
78
+ const shader = paperLookSettings(config);
79
+ const canvas = el.querySelector('canvas');
80
+ const back = paperSettings(shader).colorBack;
81
+ if (typeof back === 'string') el.style.backgroundColor = back;
82
+ if (config.quiet && canvas) {
83
+ el.style.containerType = 'size';
84
+ canvas.style.maskImage = canvas.style.webkitMaskImage = quietMask(config.quiet);
85
+ }
86
+ const stop = paperShader(el, shader);
87
+ return () => {
88
+ stop();
89
+ if (canvas) canvas.style.maskImage = canvas.style.webkitMaskImage = '';
90
+ };
91
+ }
@@ -0,0 +1,17 @@
1
+ import type { PaperShaderName } from './paper.js';
2
+
3
+ /** A Paper look's name: its component's, in kebab case. */
4
+ export type PaperLookName = 'color-panels';
5
+
6
+ /** A Paper shader as a background of its own. */
7
+ export interface PaperLook {
8
+ /** Its shader. */
9
+ shader: PaperShaderName;
10
+ /** The preset its props start from. */
11
+ preset: string;
12
+ /** Which of Paper's settings each of its own props sets, by prop. */
13
+ props: Record<string, string>;
14
+ }
15
+
16
+ /** The Paper looks, by name. */
17
+ export const PAPER_LOOKS: Readonly<Record<PaperLookName, PaperLook>>;
@@ -0,0 +1,21 @@
1
+ /**
2
+ * @module @arraypress/effects-astro/paper-looks
3
+ *
4
+ * The Paper looks: Paper shaders made backgrounds of their own (each a
5
+ * component, `<ColorPanels>`…), named for the look. Each is its shader,
6
+ * the preset its props start from, and which of Paper's settings each of
7
+ * its own props sets (`skewLeft` → `angle1`). The props every look shares
8
+ * (`background`, `zoom`, `rotation`, `offset`, `colors`, `palette`,
9
+ * `speed`, `quiet`) are mapped once, in src/runtime/paper-look.js.
10
+ */
11
+
12
+ /** @typedef {import('./paper-looks.js').PaperLook} PaperLook */
13
+
14
+ /** @type {Readonly<Record<import('./paper-looks.js').PaperLookName, PaperLook>>} */
15
+ export const PAPER_LOOKS = {
16
+ 'color-panels': {
17
+ shader: 'color-panels',
18
+ preset: 'Opening',
19
+ props: { density: 'density', skewLeft: 'angle1', skewRight: 'angle2', length: 'length', edges: 'edges', blur: 'blur', fadeInner: 'fadeIn', fadeOuter: 'fadeOut', blend: 'gradient' },
20
+ },
21
+ };
@@ -0,0 +1,37 @@
1
+ /** The thirty Paper shaders, by file name. */
2
+ export type PaperShaderName =
3
+ | 'color-panels' | 'dithering' | 'dot-grid' | 'dot-orbit' | 'fluted-glass' | 'gem-smoke' | 'god-rays'
4
+ | 'grain-gradient' | 'halftone-cmyk' | 'halftone-dots' | 'heatmap' | 'image-dithering' | 'lens-distortion'
5
+ | 'liquid-metal' | 'mesh-gradient' | 'metaballs' | 'neuro-noise' | 'paper-texture' | 'perlin-noise'
6
+ | 'pulsing-border' | 'simplex-noise' | 'smoke-ring' | 'spiral' | 'static-mesh-gradient'
7
+ | 'static-radial-gradient' | 'swirl' | 'voronoi' | 'warp' | 'water' | 'waves';
8
+
9
+ export interface PaperConfig {
10
+ /** The shader, by its file name. */
11
+ shader: PaperShaderName;
12
+ /** One of the shader's presets, by name (any case). Default its first. */
13
+ preset?: string;
14
+ /** Paper's settings for the shader (`distortion`, `colorBack`…), over the preset's. */
15
+ params?: Record<string, unknown>;
16
+ /** The shader's colours, over the preset's. */
17
+ colors?: string[];
18
+ /** A picture for the shaders that bend or trace one. */
19
+ image?: string;
20
+ /** Colours to make a picture from, when there is no `image`. */
21
+ gradient?: string[];
22
+ /** The made picture's angle, degrees. Default 135. */
23
+ gradientAngle?: number;
24
+ /** How fast it moves, over the preset's (0 is still). */
25
+ speed?: number;
26
+ /** Share of the pixel ratio drawn at. Default 1. */
27
+ scale?: number;
28
+ }
29
+
30
+ /** A shader's settings: its preset with the config's settings over it. Throws for an unknown shader. */
31
+ export function paperSettings(config: PaperConfig): Record<string, any>;
32
+
33
+ /** Settings as uniforms: `u_` names, colours as RGBA, named options as numbers. */
34
+ export function paperUniforms(shader: string, settings: Record<string, any>): Record<string, number | boolean | number[] | number[][]>;
35
+
36
+ /** Starts a Paper shader in `el`'s canvas. Returns a function that stops it. */
37
+ export function paperShader(el: HTMLElement, config: PaperConfig): () => void;
@@ -0,0 +1,285 @@
1
+ /**
2
+ * @module @arraypress/effects-astro/paper
3
+ *
4
+ * The Paper Shaders catalogue on our stage: thirty shaders (mesh and grain
5
+ * gradients, god rays, liquid metal, water, warp, dithering, halftones,
6
+ * metaballs, voronoi and more), each with the presets Paper ships, run
7
+ * without React and without the npm package. The shader sources are
8
+ * vendored unchanged in src/vendor/paper (Apache-2.0; see its LICENSE and
9
+ * NOTICE); this is the host that feeds them.
10
+ *
11
+ * Settings are Paper's own names (`distortion`, `swirl`, `colorBack`…),
12
+ * each becoming its `u_` uniform: colours from CSS strings, `colors` as an
13
+ * array (and its count), named options (`shape: 'wave'`) as their numbers,
14
+ * and the sizing uniforms every shader needs (without them the shaders
15
+ * draw one flat colour). A `preset` is the starting point; settings
16
+ * override it. `image` feeds the shaders that bend or trace a picture
17
+ * (fluted glass, water, lens distortion, liquid metal, halftones…), and
18
+ * `gradient` makes one from colours when there is no picture.
19
+ *
20
+ * Each shader is its own module, loaded when used, so a page pays only for
21
+ * the shaders on it. Needs WebGL 2; without it the element's background
22
+ * shows. With prefers-reduced-motion, one still frame.
23
+ */
24
+
25
+ import { createStage } from './stage.js';
26
+ import { watchColors } from './mount.js';
27
+ import { loadImage, imageTexture } from './image.js';
28
+ import { vertexShaderSource } from '../vendor/paper/vertex-shader.js';
29
+ import { getShaderColorFromString } from '../vendor/paper/get-shader-color-from-string.js';
30
+ import { getShaderNoiseTexture } from '../vendor/paper/get-shader-noise-texture.js';
31
+ import { PRESETS, ENUMS, RENAMES, FITS } from '../vendor/paper/presets.js';
32
+
33
+ /**
34
+ * @typedef {object} PaperConfig
35
+ * @property {string} shader The shader, by its file name: `mesh-gradient`, `god-rays`, `liquid-metal`…
36
+ * @property {string} [preset] One of the shader's presets, by name (`Beach`, `Ink`…; any case). Default its first.
37
+ * @property {Record<string, unknown>} [params] Paper's settings for the shader, over the preset's.
38
+ * @property {string[]} [colors] The shader's colours, over the preset's (where it takes a list).
39
+ * @property {string} [image] A picture for the shaders that bend or trace one.
40
+ * @property {string[]} [gradient] Colours to make a picture from, when there is no `image`.
41
+ * @property {number} [gradientAngle=135] The made picture's angle, degrees.
42
+ * @property {number} [speed] How fast it moves, over the preset's (0 is still).
43
+ * @property {number} [scale=1] Share of the pixel ratio drawn at.
44
+ */
45
+
46
+ /** The shaders that sample their picture blurred (mip levels), as Paper's React components set up. */
47
+ const MIPMAPPED = new Set(['fluted-glass', 'gem-smoke', 'heatmap', 'liquid-metal', 'lens-distortion', 'paper-texture', 'water']);
48
+
49
+ /** The shaders whose picture preparation reads a dark shape: given a black silhouette, so a white or coloured logo works. */
50
+ const SILHOUETTE = new Set(['heatmap']);
51
+
52
+ /** Settings that are not uniforms: they steer the host. */
53
+ const HOST = new Set(['speed', 'frame']);
54
+
55
+ /**
56
+ * A shader's module name as its export's prefix: `god-rays` → `godRays`.
57
+ *
58
+ * @param {string} name The file name.
59
+ * @returns {string} camelCase.
60
+ */
61
+ const camel = (name) => name.replace(/-(\w)/g, (_, c) => c.toUpperCase());
62
+
63
+ /**
64
+ * The shader's settings: its preset (the named one or its first), with the
65
+ * config's colours and settings over it.
66
+ *
67
+ * @param {PaperConfig} config The config.
68
+ * @returns {Record<string, any>} Every setting.
69
+ * @throws {Error} When the shader is not in the catalogue.
70
+ */
71
+ export function paperSettings(config) {
72
+ const presets = PRESETS[/** @type {keyof typeof PRESETS} */ (config.shader)];
73
+ if (!presets) throw new Error(`No Paper shader "${config.shader}"`);
74
+ const wanted = config.preset?.toLowerCase();
75
+ const preset = presets.find((p) => p.name.toLowerCase() === wanted) ?? presets[0];
76
+ return {
77
+ ...preset.params,
78
+ ...(config.colors ? { colors: config.colors } : {}),
79
+ ...(config.speed !== undefined ? { speed: config.speed } : {}),
80
+ ...config.params,
81
+ };
82
+ }
83
+
84
+ /**
85
+ * The settings as uniforms: `u_` names, colours as RGBA, named options as
86
+ * their numbers, `colors` with its count.
87
+ *
88
+ * @param {string} shader The shader.
89
+ * @param {Record<string, any>} settings Its settings.
90
+ * @returns {Record<string, number | boolean | number[] | number[][]>} Uniform values by name.
91
+ */
92
+ export function paperUniforms(shader, settings) {
93
+ /** @type {Record<string, any>} */
94
+ const out = {};
95
+ const named = ENUMS[/** @type {keyof typeof ENUMS} */ (shader)] ?? {};
96
+ /** @type {Record<string, string>} */
97
+ const renamed = RENAMES[/** @type {keyof typeof RENAMES} */ (shader)] ?? {};
98
+ for (const [setting, value] of Object.entries(settings)) {
99
+ if (HOST.has(setting) || value === undefined || value === null) continue;
100
+ // Some settings feed a uniform of another name (`size` → `u_pxSize`).
101
+ const key = renamed[setting] ?? setting;
102
+ if (key === 'colors' && Array.isArray(value)) {
103
+ out.u_colors = value.map((c) => getShaderColorFromString(c));
104
+ out.u_colorsCount = value.length;
105
+ } else if (key === 'fit') {
106
+ out.u_fit = typeof value === 'string' ? FITS[/** @type {keyof typeof FITS} */ (value)] ?? 2 : value;
107
+ } else if (typeof value === 'string' && /** @type {Record<string, any>} */ (named)[setting]) {
108
+ out[`u_${key}`] = /** @type {Record<string, any>} */ (named)[setting][value] ?? 0;
109
+ } else if (typeof value === 'string') {
110
+ out[`u_${key}`] = getShaderColorFromString(value);
111
+ } else {
112
+ out[`u_${key}`] = value;
113
+ }
114
+ }
115
+ return out;
116
+ }
117
+
118
+ /**
119
+ * A picture as a black silhouette: wherever it is opaque, black.
120
+ *
121
+ * @param {string} src The picture's URL.
122
+ * @returns {Promise<string>} A URL of the silhouette (PNG).
123
+ */
124
+ async function silhouette(src) {
125
+ const img = await loadImage(src);
126
+ const canvas = document.createElement('canvas');
127
+ canvas.width = img.naturalWidth || 512;
128
+ canvas.height = img.naturalHeight || 512;
129
+ const ctx = /** @type {CanvasRenderingContext2D} */ (canvas.getContext('2d'));
130
+ ctx.drawImage(img, 0, 0, canvas.width, canvas.height);
131
+ ctx.globalCompositeOperation = 'source-in';
132
+ ctx.fillStyle = '#000';
133
+ ctx.fillRect(0, 0, canvas.width, canvas.height);
134
+ return canvas.toDataURL();
135
+ }
136
+
137
+ /**
138
+ * A picture made from colours: a linear gradient, square.
139
+ *
140
+ * @param {string[]} colors The colours.
141
+ * @param {number} angle Degrees.
142
+ * @returns {Promise<HTMLImageElement>} The picture.
143
+ */
144
+ function gradientImage(colors, angle) {
145
+ const size = 512;
146
+ const canvas = document.createElement('canvas');
147
+ canvas.width = canvas.height = size;
148
+ const ctx = /** @type {CanvasRenderingContext2D} */ (canvas.getContext('2d'));
149
+ const a = (angle * Math.PI) / 180;
150
+ const dx = Math.cos(a) * size;
151
+ const dy = Math.sin(a) * size;
152
+ const g = ctx.createLinearGradient((size - dx) / 2, (size - dy) / 2, (size + dx) / 2, (size + dy) / 2);
153
+ colors.forEach((c, i) => g.addColorStop(colors.length === 1 ? 0 : i / (colors.length - 1), c));
154
+ ctx.fillStyle = g;
155
+ ctx.fillRect(0, 0, size, size);
156
+ return loadImage(canvas.toDataURL());
157
+ }
158
+
159
+ /**
160
+ * Starts a Paper shader on an element: a canvas inside it, filling it.
161
+ *
162
+ * @param {HTMLElement} el The element (its first canvas is drawn into).
163
+ * @param {PaperConfig} config The settings.
164
+ * @returns {() => void} Stops it.
165
+ */
166
+ export function paperShader(el, config) {
167
+ let stopped = false;
168
+ let stop = () => {};
169
+ start(el, config)
170
+ .then((s) => {
171
+ if (stopped) s();
172
+ else stop = s;
173
+ })
174
+ .catch((error) => console.warn('[effects-astro]', error));
175
+ return () => {
176
+ stopped = true;
177
+ stop();
178
+ };
179
+ }
180
+
181
+ /**
182
+ * Loads the shader and its pictures, then starts it.
183
+ *
184
+ * @param {HTMLElement} el The element.
185
+ * @param {PaperConfig} config The settings.
186
+ * @returns {Promise<() => void>} Stops it.
187
+ */
188
+ async function start(el, config) {
189
+ /** @type {Record<string, any>} */
190
+ const module = await import(`../vendor/paper/shaders/${config.shader}.js`);
191
+ const fragment = module[`${camel(config.shader)}FragmentShader`];
192
+ if (!fragment) throw new Error(`No fragment shader in Paper's "${config.shader}"`);
193
+ let settings = paperSettings(config);
194
+
195
+ // The pictures: the shader's image (prepared, for the shaders that trace
196
+ // one), and the noise texture some of them sample.
197
+ /** @type {Record<string, HTMLImageElement>} */
198
+ const pictures = {};
199
+ if (fragment.includes('u_image')) {
200
+ let src = config.image;
201
+ const prepare = Object.keys(module).find((k) => k.startsWith('toProcessed'));
202
+ if (src && prepare) {
203
+ if (SILHOUETTE.has(config.shader)) src = await silhouette(src);
204
+ const result = await module[prepare](src);
205
+ // The helpers name their result differently: `pngBlob` (liquid metal, gem smoke) or `blob` (heatmap).
206
+ src = URL.createObjectURL(result.pngBlob ?? result.blob);
207
+ }
208
+ if (src) pictures.u_image = await loadImage(src);
209
+ else if (config.gradient?.length) pictures.u_image = await gradientImage(config.gradient, config.gradientAngle ?? 135);
210
+ }
211
+ if (fragment.includes('u_noiseTexture')) {
212
+ const noise = getShaderNoiseTexture();
213
+ if (noise) pictures.u_noiseTexture = await noise.decode().then(() => noise);
214
+ }
215
+
216
+ const startTime = performance.now();
217
+ /** @type {Map<string, { type: number, size: number, location: WebGLUniformLocation }>} */
218
+ const active = new Map();
219
+
220
+ const { stage, stop } = createStage(el, {
221
+ fragment,
222
+ vertex: vertexShaderSource,
223
+ webgl1: false,
224
+ scale: config.scale ?? 1,
225
+ setup({ gl, program }) {
226
+ const count = gl.getProgramParameter(program, gl.ACTIVE_UNIFORMS);
227
+ for (let i = 0; i < count; i++) {
228
+ const info = /** @type {WebGLActiveInfo} */ (gl.getActiveUniform(program, i));
229
+ const name = info.name.replace(/\[0\]$/, '');
230
+ const location = gl.getUniformLocation(program, info.name);
231
+ if (location) active.set(name, { type: info.type, size: info.size, location });
232
+ }
233
+ // The pictures, each on its own unit, with its aspect ratio where the shader wants it.
234
+ let unit = 0;
235
+ for (const [name, img] of Object.entries(pictures)) {
236
+ const slot = active.get(name);
237
+ if (!slot) continue;
238
+ gl.activeTexture(gl.TEXTURE0 + unit);
239
+ imageTexture(gl, img, { mipmap: name === 'u_image' && MIPMAPPED.has(config.shader) });
240
+ gl.uniform1i(slot.location, unit);
241
+ const ratio = active.get(`${name}AspectRatio`);
242
+ if (ratio) gl.uniform1f(ratio.location, img.naturalWidth / img.naturalHeight);
243
+ unit++;
244
+ }
245
+ const isImage = active.get('u_isImage');
246
+ if (isImage) gl.uniform1i(isImage.location, pictures.u_image ? 1 : 0);
247
+ },
248
+ uniforms({ gl, canvas, ratio }, now) {
249
+ /**
250
+ * Sets one uniform by its declared type.
251
+ *
252
+ * @param {string} name The uniform.
253
+ * @param {any} value Its value.
254
+ */
255
+ const set = (name, value) => {
256
+ const slot = active.get(name);
257
+ if (!slot || value === undefined) return;
258
+ const { type, location } = slot;
259
+ if (type === gl.FLOAT) gl.uniform1f(location, Number(value));
260
+ else if (type === gl.BOOL || type === gl.INT) gl.uniform1i(location, value ? Number(value) : 0);
261
+ else if (type === gl.FLOAT_VEC2) gl.uniform2fv(location, value);
262
+ else if (type === gl.FLOAT_VEC3) gl.uniform3fv(location, value.slice(0, 3));
263
+ else if (type === gl.FLOAT_VEC4) {
264
+ const flat = Array.isArray(value[0]) ? value.flat() : value;
265
+ if (flat.length) gl.uniform4fv(location, flat);
266
+ }
267
+ };
268
+ const time = ((now - startTime) / 1000) * (settings.speed ?? 1) + (settings.frame ?? 0) / 1000;
269
+ set('u_time', time);
270
+ set('u_resolution', [canvas.width, canvas.height]);
271
+ set('u_pixelRatio', ratio);
272
+ for (const [name, value] of Object.entries(paperUniforms(config.shader, settings))) set(name, value);
273
+ },
274
+ });
275
+ if (!stage) return stop;
276
+
277
+ const unwatch = watchColors(el, (next) => {
278
+ settings = { ...settings, colors: next.colors };
279
+ if (stage.still) stage.redraw();
280
+ });
281
+ return () => {
282
+ stop();
283
+ unwatch();
284
+ };
285
+ }
@@ -0,0 +1,31 @@
1
+ import type { PaletteName } from './palettes.js';
2
+
3
+ export interface ParticleLogoConfig {
4
+ /** A word (or a character, an emoji) to draw in particles. */
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 draw in particles instead. */
9
+ src?: string;
10
+ /** Where the shape sits, each 0–1 from the top left. Default the middle. */
11
+ area?: { x: number; y: number; width: number; height: number };
12
+ /** The dots' two colours, then the two blooms'. Default white, ice blue, deep teal, midnight blue. */
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 `#030305`. */
17
+ background?: string;
18
+ /** How many particles (half on phones). Default 16000. */
19
+ count?: number;
20
+ /** A particle's size, CSS px. Default 2.2. */
21
+ size?: number;
22
+ /** How strongly they scatter round the pointer; 0 for not at all. Default 1. */
23
+ swarm?: number;
24
+ /** How far the mark tilts towards the pointer. Default 1. */
25
+ tilt?: number;
26
+ /** How far red and blue part, CSS px; 0 for none. Default 1.5. */
27
+ aberration?: number;
28
+ }
29
+
30
+ /** Starts a particle logo in `el`'s canvas. Returns a function that stops it. */
31
+ export function particleLogo(el: HTMLElement, config: ParticleLogoConfig): () => void;
@@ -0,0 +1,196 @@
1
+ /**
2
+ * @module @arraypress/effects-astro/particle-logo
3
+ *
4
+ * A logo or a word drawn in thousands of twinkling particles. They breathe
5
+ * in place; near the pointer they scatter outward and swirl round it, then
6
+ * drift home as it passes, and the whole mark tilts towards the pointer in
7
+ * a gentle perspective. Each dot is drawn with its red and blue a little
8
+ * apart, and two slow blooms of colour glow behind.
9
+ *
10
+ * The motion is worked out per particle on the GPU from where it lives and
11
+ * a seed, so there is no simulation to step: 16,000 particles cost one
12
+ * draw (three with the colour split).
13
+ *
14
+ * Opaque: the effect is the whole background of what it fills. Without
15
+ * WebGL it removes its canvas and the element's background shows.
16
+ */
17
+
18
+ import { createStage } from './stage.js';
19
+ import { createPoints, GLSL_PROJECT } from './points.js';
20
+ import { drawShape, samplePoints } from './shape.js';
21
+ import { parseColor } from './color.js';
22
+ import { canHover, isMobileGpu } from './device.js';
23
+ import { pointerIn } from './pointer.js';
24
+ import { watchColors } from './mount.js';
25
+ import { pickColors } from './palettes.js';
26
+
27
+ /**
28
+ * @typedef {object} ParticleLogoConfig
29
+ * @property {string} [text] A word to draw in particles.
30
+ * @property {string} [font] Its CSS font, without the size (default: the page's, bold).
31
+ * @property {string} [src] An image (an SVG logo) to draw in particles instead.
32
+ * @property {{ x: number, y: number, width: number, height: number }} [area]
33
+ * Where the shape sits, each 0–1 from the top left (default: the middle).
34
+ * @property {string[]} [colors] The dots' two colours, then the two blooms' (default white and ice blue, deep teal and midnight blue).
35
+ * @property {string} [palette] A shared palette by name (PALETTES; any case), when there are no `colors`. White dots, then its three colours.
36
+ * @property {string} [background='#030305'] The ground.
37
+ * @property {number} [count=16000] How many particles (half on phones).
38
+ * @property {number} [size=2.2] A particle's size, CSS px.
39
+ * @property {number} [swarm=1] How strongly they scatter round the pointer; 0 for not at all.
40
+ * @property {number} [tilt=1] How far the mark tilts towards the pointer.
41
+ * @property {number} [aberration=1.5] How far red and blue part, CSS px; 0 for none.
42
+ */
43
+
44
+ /** The ground and its two blooms. */
45
+ const GROUND = `
46
+ precision highp float;
47
+ uniform vec2 uRes;
48
+ uniform float uTime;
49
+ uniform vec3 uGround;
50
+ uniform vec3 uBloomA;
51
+ uniform vec3 uBloomB;
52
+ void main() {
53
+ vec2 uv = gl_FragCoord.xy / uRes;
54
+ vec2 aspect = vec2(uRes.x / uRes.y, 1.0);
55
+ vec2 a = (uv - vec2(0.08 + 0.04 * sin(uTime * 0.11), 0.7)) * aspect;
56
+ vec2 b = (uv - vec2(0.92, 0.25 + 0.05 * cos(uTime * 0.09))) * aspect;
57
+ vec3 col = uGround + uBloomA * exp(-dot(a, a) * 2.2) * 0.6 + uBloomB * exp(-dot(b, b) * 2.0) * 0.55;
58
+ gl_FragColor = vec4(col, 1.0);
59
+ }
60
+ `;
61
+
62
+ /** Each particle: home (x, y), seed; breathing, the swarm, the tilt. */
63
+ const VERTEX = `
64
+ precision highp float;
65
+ attribute vec4 p;
66
+ uniform vec2 uRes;
67
+ uniform float uTime;
68
+ uniform float uRatio;
69
+ uniform vec2 uShift;
70
+ uniform vec2 uPtr;
71
+ uniform float uActive;
72
+ uniform vec2 uTilt;
73
+ uniform float uSize;
74
+ uniform float uSwarm;
75
+ uniform vec3 uDotA;
76
+ uniform vec3 uDotB;
77
+ varying vec3 vColour;
78
+ varying float vAlpha;
79
+ ${GLSL_PROJECT}
80
+ void main() {
81
+ float aspect = uRes.x / uRes.y;
82
+ float s = p.z;
83
+ vec3 v = vec3((p.x * 2.0 - 1.0) * aspect, p.y * 2.0 - 1.0, (fract(s * 17.3) - 0.5) * 0.12);
84
+ // Breathing in place.
85
+ v.xy += 0.006 * vec2(sin(uTime * (0.6 + s) + s * 40.0), cos(uTime * (0.5 + s * 0.7) + s * 17.0));
86
+ // Round the pointer: out, and swirling, half one way and half the other.
87
+ vec2 ptr = vec2((uPtr.x * 2.0 - 1.0) * aspect, uPtr.y * 2.0 - 1.0);
88
+ vec2 d = v.xy - ptr;
89
+ float dist = length(d) + 1e-3;
90
+ float f = exp(-dist * dist / 0.07) * uActive * uSwarm;
91
+ vec2 out_ = d / dist;
92
+ v.xy += out_ * f * 0.22 + vec2(-out_.y, out_.x) * f * 0.2 * (s > 0.5 ? 1.0 : -1.0);
93
+ v.z += f * (s - 0.3) * 0.8;
94
+ // Towards the pointer, in perspective.
95
+ v = rotateY(v, uTilt.x * 0.35);
96
+ v = rotateX(v, -uTilt.y * 0.25);
97
+ vec4 pos = project(v, aspect);
98
+ gl_Position = pos + vec4(uShift * pos.w, 0.0, 0.0);
99
+ float twinkle = 0.55 + 0.45 * sin(uTime * (1.5 + s * 3.0) + s * 60.0);
100
+ gl_PointSize = uSize * uRatio * (0.6 + 0.9 * fract(s * 7.13)) * (0.8 + 0.4 * twinkle) / pos.w;
101
+ vColour = mix(uDotA, uDotB, fract(s * 3.7));
102
+ vAlpha = twinkle * 0.9;
103
+ }
104
+ `;
105
+
106
+ /** The defaults: white and ice-blue dots, a deep teal and a midnight-blue bloom. */
107
+ const COLORS = ['#ffffff', '#a5c8ff', '#0f4c5c', '#0b1f3a'];
108
+
109
+ /**
110
+ * Starts a particle logo on an element: an opaque canvas inside it, filling it.
111
+ *
112
+ * @param {HTMLElement} el The element (its first canvas is drawn into).
113
+ * @param {ParticleLogoConfig} config The settings.
114
+ * @returns {() => void} Stops it.
115
+ */
116
+ export function particleLogo(el, config) {
117
+ let colors = pickColors(config, COLORS, (p) => ['#ffffff', ...p.colors]);
118
+ const start = performance.now();
119
+ const ptr = { x: 0.5, y: 0.5 };
120
+ const tilt = { x: 0, y: 0 };
121
+ let active = 0;
122
+ /** @type {import('./points.js').Points | null} */
123
+ let points = null;
124
+ let generation = 0;
125
+ const count = Math.round((config.count ?? 16000) * (isMobileGpu() ? 0.5 : 1));
126
+
127
+ const { stage, stop } = createStage(el, {
128
+ fragment: GROUND,
129
+ alpha: false,
130
+ setup({ gl, program }) {
131
+ points = createPoints(gl, VERTEX);
132
+ if (!points) return false;
133
+ gl.useProgram(program);
134
+ },
135
+ resize({ canvas }) {
136
+ const mine = ++generation;
137
+ const width = Math.min(1024, canvas.width);
138
+ const height = Math.max(1, Math.round((width * canvas.height) / canvas.width));
139
+ drawShape({ text: config.text, font: config.font, src: config.src, area: config.area, padding: 0.12 }, width, height)
140
+ .then((mask) => {
141
+ if (mine !== generation || !points) return;
142
+ const homes = samplePoints(mask, count);
143
+ const data = new Float32Array(count * 4);
144
+ for (let i = 0; i < count; i++) {
145
+ data.set([homes[i * 2], homes[i * 2 + 1], Math.random(), 0], i * 4);
146
+ }
147
+ points.upload(data);
148
+ if (stage?.still) stage.redraw();
149
+ })
150
+ .catch((error) => console.warn('[effects-astro]', error));
151
+ },
152
+ uniforms({ gl, uniform }, now) {
153
+ gl.uniform1f(uniform('uTime'), (now - start) / 1000);
154
+ gl.uniform3fv(uniform('uGround'), parseColor(config.background ?? '#030305'));
155
+ gl.uniform3fv(uniform('uBloomA'), parseColor(colors[2] ?? COLORS[2]));
156
+ gl.uniform3fv(uniform('uBloomB'), parseColor(colors[3] ?? colors[2] ?? COLORS[3]));
157
+ },
158
+ after({ gl, ratio, still }, now) {
159
+ if (!points?.count()) return;
160
+ const t = (now - start) / 1000;
161
+ const p = !still && canHover() ? pointerIn(el) : null;
162
+ if (p?.inside) {
163
+ ptr.x += (p.x - ptr.x) * 0.12;
164
+ ptr.y += (p.y - ptr.y) * 0.12;
165
+ }
166
+ active += ((p?.inside ? 1 : 0) - active) * 0.06;
167
+ // Towards the pointer, or a slow sway without one.
168
+ const lean = p?.inside ? { x: (p.x - 0.5) * 2, y: (p.y - 0.5) * 2 } : { x: Math.sin(t * 0.3) * 0.3, y: Math.sin(t * 0.23) * 0.2 };
169
+ tilt.x += (lean.x * (config.tilt ?? 1) - tilt.x) * 0.05;
170
+ tilt.y += (lean.y * (config.tilt ?? 1) - tilt.y) * 0.05;
171
+ const u = points.program.uniform;
172
+ points.passes((config.aberration ?? 1.5) * ratio, () => {
173
+ gl.uniform2f(u('uRes'), gl.drawingBufferWidth, gl.drawingBufferHeight);
174
+ gl.uniform1f(u('uTime'), t);
175
+ gl.uniform1f(u('uRatio'), ratio);
176
+ gl.uniform2f(u('uPtr'), ptr.x, ptr.y);
177
+ gl.uniform1f(u('uActive'), active);
178
+ gl.uniform2f(u('uTilt'), tilt.x, tilt.y);
179
+ gl.uniform1f(u('uSize'), config.size ?? 2.2);
180
+ gl.uniform1f(u('uSwarm'), config.swarm ?? 1);
181
+ gl.uniform3fv(u('uDotA'), parseColor(colors[0]));
182
+ gl.uniform3fv(u('uDotB'), parseColor(colors[1] ?? colors[0]));
183
+ });
184
+ },
185
+ });
186
+ if (!stage) return stop;
187
+
188
+ const unwatch = watchColors(el, (next) => {
189
+ colors = next.colors;
190
+ if (stage.still) stage.redraw();
191
+ });
192
+ return () => {
193
+ stop();
194
+ unwatch();
195
+ };
196
+ }