@arraypress/effects-astro 0.0.0-stage → 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (314) hide show
  1. package/AGENTS.md +85 -0
  2. package/COMPONENTS.md +1264 -0
  3. package/LICENSE +139 -0
  4. package/README.md +197 -2
  5. package/package.json +60 -4
  6. package/src/components/AsciiRain.astro +36 -0
  7. package/src/components/BorderSweep.astro +39 -0
  8. package/src/components/Caustics.astro +39 -0
  9. package/src/components/ColorPanels.astro +62 -0
  10. package/src/components/Cursor.astro +97 -0
  11. package/src/components/DotGrid.astro +34 -0
  12. package/src/components/DotSurface.astro +35 -0
  13. package/src/components/EffectCanvas.astro +152 -0
  14. package/src/components/FitText.astro +35 -0
  15. package/src/components/FlutedGlass.astro +36 -0
  16. package/src/components/Glass.astro +69 -0
  17. package/src/components/GlassGlyph.astro +38 -0
  18. package/src/components/GlowField.astro +177 -0
  19. package/src/components/Grain.astro +98 -0
  20. package/src/components/HaloRing.astro +38 -0
  21. package/src/components/HighlightSweep.astro +74 -0
  22. package/src/components/ImageRipple.astro +56 -0
  23. package/src/components/Ink.astro +37 -0
  24. package/src/components/InkField.astro +45 -0
  25. package/src/components/IsoCity.astro +54 -0
  26. package/src/components/LightField.astro +40 -0
  27. package/src/components/LiquidMetal.astro +41 -0
  28. package/src/components/Magnetic.astro +52 -0
  29. package/src/components/Marquee.astro +216 -0
  30. package/src/components/MosaicReveal.astro +196 -0
  31. package/src/components/NebulaLogo.astro +38 -0
  32. package/src/components/NeonSwarm.astro +38 -0
  33. package/src/components/Ocean.astro +40 -0
  34. package/src/components/OutlineWordmark.astro +222 -0
  35. package/src/components/PaperBackground.astro +40 -0
  36. package/src/components/PaperShader.astro +55 -0
  37. package/src/components/Parallax.astro +49 -0
  38. package/src/components/ParticleLogo.astro +37 -0
  39. package/src/components/PixelGlitchText.astro +70 -0
  40. package/src/components/PointCloud.astro +37 -0
  41. package/src/components/ProductStack.astro +298 -0
  42. package/src/components/PulseMark.astro +189 -0
  43. package/src/components/RadiantGlass.astro +38 -0
  44. package/src/components/RainOnGlass.astro +71 -0
  45. package/src/components/Reveal.astro +115 -0
  46. package/src/components/ScrambleText.astro +52 -0
  47. package/src/components/ScrollReveal.astro +38 -0
  48. package/src/components/ShaderBackground.astro +55 -0
  49. package/src/components/SignalLines.astro +41 -0
  50. package/src/components/SilkRibbon.astro +37 -0
  51. package/src/components/SpotlightText.astro +131 -0
  52. package/src/components/Tilt.astro +79 -0
  53. package/src/components/Topography.astro +38 -0
  54. package/src/components/Typewriter.astro +66 -0
  55. package/src/components/VariableType.astro +59 -0
  56. package/src/components/VoxelLogo.astro +46 -0
  57. package/src/components/WaveBars.astro +41 -0
  58. package/src/components/WorldRoutes.astro +41 -0
  59. package/src/components/style.js +19 -0
  60. package/src/index.d.ts +143 -0
  61. package/src/index.js +105 -0
  62. package/src/runtime/ascii-rain.d.ts +53 -0
  63. package/src/runtime/ascii-rain.js +355 -0
  64. package/src/runtime/audio-meter.d.ts +65 -0
  65. package/src/runtime/audio-meter.js +282 -0
  66. package/src/runtime/audio.d.ts +26 -0
  67. package/src/runtime/audio.js +453 -0
  68. package/src/runtime/blur.d.ts +2 -0
  69. package/src/runtime/blur.js +56 -0
  70. package/src/runtime/canvas2d.d.ts +23 -0
  71. package/src/runtime/canvas2d.js +105 -0
  72. package/src/runtime/caustics.d.ts +40 -0
  73. package/src/runtime/caustics.js +286 -0
  74. package/src/runtime/color.d.ts +33 -0
  75. package/src/runtime/color.js +213 -0
  76. package/src/runtime/curl.d.ts +5 -0
  77. package/src/runtime/curl.js +81 -0
  78. package/src/runtime/cursor.d.ts +9 -0
  79. package/src/runtime/cursor.js +80 -0
  80. package/src/runtime/device.d.ts +14 -0
  81. package/src/runtime/device.js +73 -0
  82. package/src/runtime/dot-grid.d.ts +27 -0
  83. package/src/runtime/dot-grid.js +245 -0
  84. package/src/runtime/dot-surface.d.ts +46 -0
  85. package/src/runtime/dot-surface.js +429 -0
  86. package/src/runtime/feedback.d.ts +18 -0
  87. package/src/runtime/feedback.js +131 -0
  88. package/src/runtime/fit-text.d.ts +5 -0
  89. package/src/runtime/fit-text.js +60 -0
  90. package/src/runtime/fluid.d.ts +55 -0
  91. package/src/runtime/fluid.js +504 -0
  92. package/src/runtime/fluted-glass.d.ts +27 -0
  93. package/src/runtime/fluted-glass.js +157 -0
  94. package/src/runtime/gl.d.ts +19 -0
  95. package/src/runtime/gl.js +104 -0
  96. package/src/runtime/glass-glyph.d.ts +49 -0
  97. package/src/runtime/glass-glyph.js +355 -0
  98. package/src/runtime/glass-sheen.d.ts +2 -0
  99. package/src/runtime/glass-sheen.js +30 -0
  100. package/src/runtime/glsl.d.ts +17 -0
  101. package/src/runtime/glsl.js +78 -0
  102. package/src/runtime/glyphs.d.ts +22 -0
  103. package/src/runtime/glyphs.js +107 -0
  104. package/src/runtime/halo-ring.d.ts +47 -0
  105. package/src/runtime/halo-ring.js +334 -0
  106. package/src/runtime/height-field.d.ts +50 -0
  107. package/src/runtime/height-field.js +176 -0
  108. package/src/runtime/image-ripple.d.ts +25 -0
  109. package/src/runtime/image-ripple.js +353 -0
  110. package/src/runtime/image.d.ts +11 -0
  111. package/src/runtime/image.js +81 -0
  112. package/src/runtime/in-view.d.ts +6 -0
  113. package/src/runtime/in-view.js +36 -0
  114. package/src/runtime/ink-field/shaders.js +258 -0
  115. package/src/runtime/ink-field/sim.js +582 -0
  116. package/src/runtime/ink-field.d.ts +39 -0
  117. package/src/runtime/ink-field.js +311 -0
  118. package/src/runtime/ink.d.ts +31 -0
  119. package/src/runtime/ink.js +109 -0
  120. package/src/runtime/instancing.d.ts +12 -0
  121. package/src/runtime/instancing.js +37 -0
  122. package/src/runtime/iso-camera.d.ts +35 -0
  123. package/src/runtime/iso-camera.js +85 -0
  124. package/src/runtime/iso-city/figures.d.ts +30 -0
  125. package/src/runtime/iso-city/figures.js +136 -0
  126. package/src/runtime/iso-city/look.d.ts +34 -0
  127. package/src/runtime/iso-city/look.js +105 -0
  128. package/src/runtime/iso-city/plan.d.ts +59 -0
  129. package/src/runtime/iso-city/plan.js +463 -0
  130. package/src/runtime/iso-city/roads.d.ts +64 -0
  131. package/src/runtime/iso-city/roads.js +320 -0
  132. package/src/runtime/iso-city/scene.d.ts +27 -0
  133. package/src/runtime/iso-city/scene.js +432 -0
  134. package/src/runtime/iso-city/shaders.d.ts +6 -0
  135. package/src/runtime/iso-city/shaders.js +233 -0
  136. package/src/runtime/iso-city/shadows.d.ts +13 -0
  137. package/src/runtime/iso-city/shadows.js +148 -0
  138. package/src/runtime/iso-city/traffic.d.ts +80 -0
  139. package/src/runtime/iso-city/traffic.js +457 -0
  140. package/src/runtime/iso-city.d.ts +37 -0
  141. package/src/runtime/iso-city.js +458 -0
  142. package/src/runtime/lens-map.d.ts +11 -0
  143. package/src/runtime/lens-map.js +146 -0
  144. package/src/runtime/light-field.d.ts +29 -0
  145. package/src/runtime/light-field.js +292 -0
  146. package/src/runtime/liquid-glass.d.ts +48 -0
  147. package/src/runtime/liquid-glass.js +423 -0
  148. package/src/runtime/liquid-metal/beads.js +217 -0
  149. package/src/runtime/liquid-metal/field.js +88 -0
  150. package/src/runtime/liquid-metal/look.js +64 -0
  151. package/src/runtime/liquid-metal/shaders.js +255 -0
  152. package/src/runtime/liquid-metal/wave.js +53 -0
  153. package/src/runtime/liquid-metal.d.ts +46 -0
  154. package/src/runtime/liquid-metal.js +366 -0
  155. package/src/runtime/loop.d.ts +7 -0
  156. package/src/runtime/loop.js +112 -0
  157. package/src/runtime/magnetic.d.ts +9 -0
  158. package/src/runtime/magnetic.js +46 -0
  159. package/src/runtime/marquee.d.ts +2 -0
  160. package/src/runtime/marquee.js +27 -0
  161. package/src/runtime/mesh.d.ts +30 -0
  162. package/src/runtime/mesh.js +217 -0
  163. package/src/runtime/mosaic-reveal.d.ts +34 -0
  164. package/src/runtime/mosaic-reveal.js +504 -0
  165. package/src/runtime/motion.d.ts +2 -0
  166. package/src/runtime/motion.js +21 -0
  167. package/src/runtime/mount.d.ts +13 -0
  168. package/src/runtime/mount.js +120 -0
  169. package/src/runtime/nebula-logo.d.ts +31 -0
  170. package/src/runtime/nebula-logo.js +282 -0
  171. package/src/runtime/neon-swarm.d.ts +31 -0
  172. package/src/runtime/neon-swarm.js +258 -0
  173. package/src/runtime/ocean/presets.d.ts +54 -0
  174. package/src/runtime/ocean/presets.js +184 -0
  175. package/src/runtime/ocean/shader.d.ts +14 -0
  176. package/src/runtime/ocean/shader.js +389 -0
  177. package/src/runtime/ocean/view.d.ts +24 -0
  178. package/src/runtime/ocean/view.js +98 -0
  179. package/src/runtime/ocean/waves.d.ts +29 -0
  180. package/src/runtime/ocean/waves.js +119 -0
  181. package/src/runtime/ocean.d.ts +31 -0
  182. package/src/runtime/ocean.js +173 -0
  183. package/src/runtime/palettes.d.ts +37 -0
  184. package/src/runtime/palettes.js +75 -0
  185. package/src/runtime/paper-look.d.ts +42 -0
  186. package/src/runtime/paper-look.js +91 -0
  187. package/src/runtime/paper-looks.d.ts +17 -0
  188. package/src/runtime/paper-looks.js +21 -0
  189. package/src/runtime/paper.d.ts +37 -0
  190. package/src/runtime/paper.js +303 -0
  191. package/src/runtime/particle-logo.d.ts +31 -0
  192. package/src/runtime/particle-logo.js +196 -0
  193. package/src/runtime/ping-pong.d.ts +43 -0
  194. package/src/runtime/ping-pong.js +175 -0
  195. package/src/runtime/pixel-glitch.d.ts +17 -0
  196. package/src/runtime/pixel-glitch.js +287 -0
  197. package/src/runtime/point-cloud.d.ts +33 -0
  198. package/src/runtime/point-cloud.js +287 -0
  199. package/src/runtime/pointer.d.ts +27 -0
  200. package/src/runtime/pointer.js +128 -0
  201. package/src/runtime/points.d.ts +25 -0
  202. package/src/runtime/points.js +130 -0
  203. package/src/runtime/pools.d.ts +2 -0
  204. package/src/runtime/pools.js +25 -0
  205. package/src/runtime/product-stack.d.ts +24 -0
  206. package/src/runtime/product-stack.js +210 -0
  207. package/src/runtime/radiant-glass.d.ts +29 -0
  208. package/src/runtime/radiant-glass.js +282 -0
  209. package/src/runtime/rain-on-glass/backdrop.d.ts +22 -0
  210. package/src/runtime/rain-on-glass/backdrop.js +185 -0
  211. package/src/runtime/rain-on-glass/shader.d.ts +2 -0
  212. package/src/runtime/rain-on-glass/shader.js +168 -0
  213. package/src/runtime/rain-on-glass/wipe.d.ts +16 -0
  214. package/src/runtime/rain-on-glass/wipe.js +65 -0
  215. package/src/runtime/rain-on-glass.d.ts +27 -0
  216. package/src/runtime/rain-on-glass.js +210 -0
  217. package/src/runtime/ramp.d.ts +2 -0
  218. package/src/runtime/ramp.js +30 -0
  219. package/src/runtime/random.d.ts +2 -0
  220. package/src/runtime/random.js +22 -0
  221. package/src/runtime/scroll-reveal.d.ts +22 -0
  222. package/src/runtime/scroll-reveal.js +73 -0
  223. package/src/runtime/shader-background.d.ts +42 -0
  224. package/src/runtime/shader-background.js +261 -0
  225. package/src/runtime/shader-backgrounds/band-lens.js +72 -0
  226. package/src/runtime/shader-backgrounds/common.js +53 -0
  227. package/src/runtime/shader-backgrounds/grain-gradient.js +87 -0
  228. package/src/runtime/shader-backgrounds/grid.js +33 -0
  229. package/src/runtime/shader-backgrounds/mesh-gradient.js +74 -0
  230. package/src/runtime/shader-backgrounds/silk.js +57 -0
  231. package/src/runtime/shader-backgrounds/waves.js +31 -0
  232. package/src/runtime/shape.d.ts +42 -0
  233. package/src/runtime/shape.js +313 -0
  234. package/src/runtime/silk-ribbon.d.ts +23 -0
  235. package/src/runtime/silk-ribbon.js +206 -0
  236. package/src/runtime/slideshow.d.ts +27 -0
  237. package/src/runtime/slideshow.js +234 -0
  238. package/src/runtime/spotlight.d.ts +9 -0
  239. package/src/runtime/spotlight.js +97 -0
  240. package/src/runtime/stack-poses.d.ts +53 -0
  241. package/src/runtime/stack-poses.js +273 -0
  242. package/src/runtime/stage.d.ts +47 -0
  243. package/src/runtime/stage.js +187 -0
  244. package/src/runtime/swarm.d.ts +49 -0
  245. package/src/runtime/swarm.js +199 -0
  246. package/src/runtime/text-motion.d.ts +41 -0
  247. package/src/runtime/text-motion.js +165 -0
  248. package/src/runtime/text.d.ts +11 -0
  249. package/src/runtime/text.js +90 -0
  250. package/src/runtime/tilt.d.ts +19 -0
  251. package/src/runtime/tilt.js +116 -0
  252. package/src/runtime/topography.d.ts +71 -0
  253. package/src/runtime/topography.js +650 -0
  254. package/src/runtime/trackers.d.ts +21 -0
  255. package/src/runtime/trackers.js +76 -0
  256. package/src/runtime/variable-type.d.ts +43 -0
  257. package/src/runtime/variable-type.js +358 -0
  258. package/src/runtime/voxel-logo.d.ts +31 -0
  259. package/src/runtime/voxel-logo.js +237 -0
  260. package/src/runtime/waves.d.ts +70 -0
  261. package/src/runtime/waves.js +799 -0
  262. package/src/runtime/world-labels.d.ts +29 -0
  263. package/src/runtime/world-labels.js +59 -0
  264. package/src/runtime/world-land.d.ts +8 -0
  265. package/src/runtime/world-land.js +105 -0
  266. package/src/runtime/world-map.d.ts +69 -0
  267. package/src/runtime/world-map.js +308 -0
  268. package/src/runtime/world-plan.d.ts +101 -0
  269. package/src/runtime/world-plan.js +333 -0
  270. package/src/runtime/world-routes.d.ts +75 -0
  271. package/src/runtime/world-routes.js +673 -0
  272. package/src/styles/border-sweep.css +82 -0
  273. package/src/styles/glass.css +104 -0
  274. package/src/styles/motion.css +218 -0
  275. package/src/styles/reveal.css +51 -0
  276. package/src/vendor/paper/LICENSE +201 -0
  277. package/src/vendor/paper/NOTICE +5 -0
  278. package/src/vendor/paper/README.md +8 -0
  279. package/src/vendor/paper/get-shader-color-from-string.js +100 -0
  280. package/src/vendor/paper/get-shader-noise-texture.js +17 -0
  281. package/src/vendor/paper/presets.js +3678 -0
  282. package/src/vendor/paper/shader-sizing.js +37 -0
  283. package/src/vendor/paper/shader-utils.js +130 -0
  284. package/src/vendor/paper/shaders/color-panels.js +223 -0
  285. package/src/vendor/paper/shaders/dithering.js +270 -0
  286. package/src/vendor/paper/shaders/dot-grid.js +109 -0
  287. package/src/vendor/paper/shaders/dot-orbit.js +122 -0
  288. package/src/vendor/paper/shaders/fluted-glass.js +360 -0
  289. package/src/vendor/paper/shaders/gem-smoke.js +572 -0
  290. package/src/vendor/paper/shaders/god-rays.js +132 -0
  291. package/src/vendor/paper/shaders/grain-gradient.js +300 -0
  292. package/src/vendor/paper/shaders/halftone-cmyk.js +294 -0
  293. package/src/vendor/paper/shaders/halftone-dots.js +320 -0
  294. package/src/vendor/paper/shaders/heatmap.js +383 -0
  295. package/src/vendor/paper/shaders/image-dithering.js +198 -0
  296. package/src/vendor/paper/shaders/lens-distortion.js +275 -0
  297. package/src/vendor/paper/shaders/liquid-metal.js +632 -0
  298. package/src/vendor/paper/shaders/mesh-gradient.js +130 -0
  299. package/src/vendor/paper/shaders/metaballs.js +110 -0
  300. package/src/vendor/paper/shaders/neuro-noise.js +76 -0
  301. package/src/vendor/paper/shaders/paper-texture.js +526 -0
  302. package/src/vendor/paper/shaders/perlin-noise.js +166 -0
  303. package/src/vendor/paper/shaders/pulsing-border.js +238 -0
  304. package/src/vendor/paper/shaders/simplex-noise.js +91 -0
  305. package/src/vendor/paper/shaders/smoke-ring.js +132 -0
  306. package/src/vendor/paper/shaders/spiral.js +82 -0
  307. package/src/vendor/paper/shaders/static-mesh-gradient.js +130 -0
  308. package/src/vendor/paper/shaders/static-radial-gradient.js +187 -0
  309. package/src/vendor/paper/shaders/swirl.js +103 -0
  310. package/src/vendor/paper/shaders/voronoi.js +138 -0
  311. package/src/vendor/paper/shaders/warp.js +129 -0
  312. package/src/vendor/paper/shaders/water.js +121 -0
  313. package/src/vendor/paper/shaders/waves.js +65 -0
  314. package/src/vendor/paper/vertex-shader.js +159 -0
@@ -0,0 +1,286 @@
1
+ /**
2
+ * @module @arraypress/effects-astro/caustics
3
+ *
4
+ * Sunlight through rippling water onto a pool floor: a bright, shifting
5
+ * net of light over an aqua ground. Two layers of drifting cells, bent by
6
+ * a slow swell of noise, each give their thin bright seams; where the two
7
+ * nets cross the light pools hotter, and the hottest spots run to white.
8
+ * Red, green and blue are traced from points a hair apart, so each line
9
+ * carries a faint rainbow at its edges. The floor dims and deepens towards
10
+ * the sides, as if the pool fell away there, and faint tiles can lie under
11
+ * it, bent by the same water.
12
+ *
13
+ * The pointer disturbs the surface where it is: rings run out from it,
14
+ * warping the net and brightening it, more while it moves.
15
+ *
16
+ * One fragment pass, nine cells a layer, no textures. Opaque: the effect
17
+ * is the whole background of what it fills. With prefers-reduced-motion,
18
+ * one still frame. Without WebGL it removes its canvas and the element's
19
+ * background (the floor fading into `background`) shows.
20
+ */
21
+
22
+ import { createStage } from './stage.js';
23
+ import { GLSL_OKLAB, fromOklab, parseColor, toHex, toOklab } from './color.js';
24
+ import { GLSL_NOISE, GLSL_QUIET, quietUniform } from './glsl.js';
25
+ import { canHover, isMobileGpu } from './device.js';
26
+ import { pointerIn } from './pointer.js';
27
+ import { watchColors } from './mount.js';
28
+ import { pickColors } from './palettes.js';
29
+
30
+ /**
31
+ * @typedef {object} CausticsConfig
32
+ * @property {string[]} [colors] The water and the light, in any order: the darkest is the deep water towards the edges, the lightest the light, the one between the floor (default aqua `#2cc3d0`, deep teal `#0b7ea8`, sun `#f2fff9`). One or two colours work too.
33
+ * @property {string} [palette] A shared palette by name (PALETTES; any case), when there are no `colors`.
34
+ * @property {string} [background] The ground the edges fall away into, and what shows without WebGL. Default: the deep water, darkened.
35
+ * @property {number} [scale=1] How large the net of light is, 0.4–3: 2 doubles it.
36
+ * @property {number} [speed=1] How fast the water moves, 0–3; 0 holds it still.
37
+ * @property {number} [intensity=1] How bright the light is, 0–2.
38
+ * @property {boolean} [tiles=false] Faint pool tiles on the floor, bent by the water.
39
+ * @property {boolean} [interactive=true] Whether the pointer disturbs the water where it is.
40
+ * @property {{ x?: number, y?: number, radius?: number, depth?: number } | null} [quiet=null]
41
+ * An area where the light is kept low behind copy: centre (0–1 from the top
42
+ * left), radius (a share of the height) and depth (0–1). The middle with `{}`.
43
+ */
44
+
45
+ /**
46
+ * @typedef {object} CausticTones
47
+ * @property {string} floor The floor, seen through shallow water.
48
+ * @property {string} deep The deep water towards the edges.
49
+ * @property {string} light The light.
50
+ * @property {string} background What the edges fall away into.
51
+ */
52
+
53
+ /** The default water and light: aqua, deep teal, sun. */
54
+ const COLORS = ['#2cc3d0', '#0b7ea8', '#f2fff9'];
55
+ /** Cells of the first layer across the height, at `scale` 1. */
56
+ const DENSITY = 3.4;
57
+ /** Tiles across the height, at `scale` 1. */
58
+ const TILES = 7;
59
+ /** Where the water's clock starts, seconds: the still frame shows this moment. */
60
+ const START = 24;
61
+
62
+ const FRAG = `
63
+ precision highp float;
64
+ uniform vec2 uRes;
65
+ uniform float uTime;
66
+ uniform float uDensity;
67
+ uniform float uTiles;
68
+ uniform float uIntensity;
69
+ uniform vec4 uPtr;
70
+ uniform vec3 uFloor;
71
+ uniform vec3 uDeep;
72
+ uniform vec3 uBack;
73
+ uniform vec3 uLight;
74
+ ${GLSL_NOISE}
75
+ ${GLSL_QUIET}
76
+ ${GLSL_OKLAB}
77
+
78
+ // One net of light: the gap between the nearest and second-nearest of a
79
+ // field of drifting points (0 on a seam), measured from three points
80
+ // \`split\` apart, for red, green and blue.
81
+ vec3 seams(vec2 p, vec2 split, float t) {
82
+ vec2 i = floor(p), f = fract(p);
83
+ vec3 f1 = vec3(8.0), f2 = vec3(8.0);
84
+ for (int y = -1; y <= 1; y++) {
85
+ for (int x = -1; x <= 1; x++) {
86
+ vec2 o = vec2(float(x), float(y));
87
+ vec2 h = vec2(hash(i + o), hash(i + o + 19.19));
88
+ vec2 c = o + 0.5 + 0.42 * sin(t * (0.6 + 0.5 * h.yx) + 6.2831 * h) - f;
89
+ vec2 r = c + split, b = c - split;
90
+ vec3 d = vec3(dot(r, r), dot(c, c), dot(b, b));
91
+ f2 = min(f2, max(f1, d));
92
+ f1 = min(f1, d);
93
+ }
94
+ }
95
+ return sqrt(f2) - sqrt(f1);
96
+ }
97
+
98
+ void main() {
99
+ vec2 uv = gl_FragCoord.xy / uRes;
100
+ float aspect = uRes.x / uRes.y;
101
+ // Centred, in units of the height.
102
+ vec2 q = vec2((uv.x - 0.5) * aspect, uv.y - 0.5);
103
+ float t = uTime;
104
+
105
+ // The pointer: rings running out from it, strongest close by.
106
+ vec2 dp = q - uPtr.xy;
107
+ float r = length(dp);
108
+ float near = exp(-r * r * 16.0) * uPtr.z;
109
+ vec2 bend = dp / max(r, 0.05) * sin(r * 36.0 - t * 5.0) * near * (0.4 + uPtr.w) * 0.022;
110
+
111
+ // The swell: a slow warp of noise that bends the light and the floor alike.
112
+ vec2 p = q * uDensity;
113
+ vec2 swell = vec2(noise(p * 0.5 + vec2(t * 0.11, -t * 0.07)), noise(p * 0.5 + vec2(4.7 - t * 0.09, 1.3 + t * 0.1))) - 0.5;
114
+ // A finer ripple on it curves each seam.
115
+ vec2 ripple = vec2(noise(p * 1.7 + vec2(-t * 0.23, t * 0.17)), noise(p * 1.7 + vec2(9.2 + t * 0.19, 5.1 - t * 0.21))) - 0.5;
116
+ vec2 w = p + swell * 1.2 + ripple * 0.42 + bend * uDensity;
117
+
118
+ // Two nets, the second finer, turned and drifting the other way.
119
+ vec2 split = vec2(0.016, 0.008) * (1.0 + near * 0.5);
120
+ vec3 e1 = seams(w, split, t);
121
+ vec2 w2 = mat2(0.8, -0.6, 0.6, 0.8) * w * 1.45 + vec2(t * 0.04, 3.1);
122
+ vec3 e2 = seams(w2, split * 1.45, 1.7 - t * 0.8);
123
+ // Lines thicken and thin across the floor.
124
+ float width = 0.6 + 0.7 * noise(p * 0.35 - t * 0.03);
125
+ vec3 l1 = exp(-e1 * 24.0 / width), l2 = exp(-e2 * 30.0 / width);
126
+ vec3 light = (l1 + l2) * 0.5 + l1 * l2 * 1.8;
127
+
128
+ // The floor deepens and darkens towards the edges.
129
+ vec2 edge = (uv - 0.5) * vec2(1.0, 1.15) * 2.0;
130
+ float depth = smoothstep(0.25, 1.5, length(edge));
131
+ float shade = clamp(0.05 + 0.55 * noise(q * 1.1 + t * 0.015) + depth * 0.9, 0.0, 1.0);
132
+ vec3 lab = mix(uFloor, uDeep, shade);
133
+ // The light takes from the floor around the lines, as a lens would.
134
+ lab.x *= 0.9 + 0.16 * light.g;
135
+ if (uTiles > 0.0) {
136
+ vec2 g = fract((q + swell * 0.05 + bend * 0.6) * uTiles + 0.5);
137
+ vec2 m = min(g, 1.0 - g);
138
+ float px = uTiles / uRes.y;
139
+ float grout = 1.0 - smoothstep(0.018, 0.018 + px * 1.5, min(m.x, m.y));
140
+ lab.x -= grout * 0.07;
141
+ }
142
+ lab = mix(lab, uBack, depth * depth * 0.85);
143
+ vec3 col = fromOklab(lab);
144
+
145
+ float glow = uIntensity * (1.0 - depth * 0.7) * mix(1.0 - uQuiet.w, 1.0, quietOpen(q)) * (1.0 + near * (0.9 + uPtr.w));
146
+ vec3 lit = light * glow;
147
+ col += uLight * lit * 0.8;
148
+ // The hottest light runs to white.
149
+ col += max(lit - 0.9, 0.0) * mix(uLight, vec3(1.0), 0.6) * 0.5;
150
+ col += (hash(gl_FragCoord.xy + fract(t)) - 0.5) * 0.01;
151
+ gl_FragColor = vec4(col, 1.0);
152
+ }
153
+ `;
154
+
155
+ /**
156
+ * Oklab lightness of a hex colour.
157
+ *
158
+ * @param {string} hex The colour.
159
+ * @returns {number} Its lightness, 0–1.
160
+ */
161
+ const lightness = (hex) => toOklab(parseColor(hex))[0];
162
+
163
+ /**
164
+ * A colour's Oklab lightness moved by `by`, its hue and chroma scaled with it.
165
+ *
166
+ * @param {string} hex The colour.
167
+ * @param {number} by The share to scale lightness and chroma by (0.6 darkens).
168
+ * @returns {string} The new colour, as hex.
169
+ */
170
+ const shadeOf = (hex, by) => {
171
+ const [L, a, b] = toOklab(parseColor(hex));
172
+ return toHex(fromOklab([L * by, a * by, b * by]));
173
+ };
174
+
175
+ /**
176
+ * The tones the water is drawn in, from the colours given in any order:
177
+ * the darkest the deep water, the lightest the light, the median the floor.
178
+ * One colour is the floor, deepened for the deep water, under a white sun;
179
+ * two are the floor (the darker, deepened for the deep) and the light.
180
+ *
181
+ * @param {CausticsConfig} config The settings (`colors`, `palette`, `background`).
182
+ * @returns {CausticTones} The tones, as hex.
183
+ * @throws {TypeError} When a colour is not hex.
184
+ */
185
+ export function causticTones(config) {
186
+ const colors = pickColors(config, COLORS);
187
+ const sorted = [...colors].sort((a, b) => lightness(a) - lightness(b));
188
+ const one = sorted.length === 1;
189
+ const floor = one || sorted.length === 2 ? sorted[0] : sorted[Math.floor(sorted.length / 2)];
190
+ const deep = one || sorted.length === 2 ? shadeOf(floor, 0.75) : sorted[0];
191
+ const light = one ? '#ffffff' : sorted[sorted.length - 1];
192
+ return { floor, deep, light, background: config.background ?? shadeOf(deep, 0.62) };
193
+ }
194
+
195
+ /**
196
+ * Starts the caustics on an element: an opaque canvas inside it, filling it.
197
+ *
198
+ * @param {HTMLElement} el The element (its first canvas is drawn into).
199
+ * @param {CausticsConfig} config The settings.
200
+ * @returns {() => void} Stops it.
201
+ */
202
+ export function caustics(el, config) {
203
+ let tones = causticTones(config);
204
+ const speed = config.speed ?? 1;
205
+ const interactive = config.interactive !== false;
206
+ let time = START;
207
+ /** The pointer's disturbance: where it is heading, where it is (centred, height units), how present (0–1) and how stirred (0–1). */
208
+ const ptr = { tx: 0, ty: 0, x: 0, y: 0, on: 0, stir: 0 };
209
+
210
+ /**
211
+ * Follows the pointer: the disturbance eases towards it, swells while it
212
+ * moves, and fades when it leaves.
213
+ *
214
+ * @param {number} aspect The canvas's width over height.
215
+ * @param {number} dt Seconds since the last frame.
216
+ */
217
+ const follow = (aspect, dt) => {
218
+ const p = pointerIn(el);
219
+ let moved = 0;
220
+ if (p.inside) {
221
+ const x = (p.x - 0.5) * aspect;
222
+ const y = p.y - 0.5;
223
+ if (ptr.on < 0.01) Object.assign(ptr, { x, y, tx: x, ty: y });
224
+ moved = Math.hypot(x - ptr.tx, y - ptr.ty);
225
+ ptr.tx = x;
226
+ ptr.ty = y;
227
+ }
228
+ const k = 1 - Math.exp(-dt * 6);
229
+ ptr.x += (ptr.tx - ptr.x) * k;
230
+ ptr.y += (ptr.ty - ptr.y) * k;
231
+ ptr.on += ((p.inside ? 1 : 0) - ptr.on) * (1 - Math.exp(-dt * 2.5));
232
+ const goal = dt > 0 ? Math.min(1, moved / dt / 1.5) : 0;
233
+ ptr.stir += (goal - ptr.stir) * (1 - Math.exp(-dt * (goal > ptr.stir ? 8 : 1.5)));
234
+ };
235
+
236
+ /**
237
+ * Sets the colour uniforms from the current tones.
238
+ *
239
+ * @param {import('./stage.js').Stage} stage The stage.
240
+ */
241
+ const paint = ({ gl, uniform }) => {
242
+ gl.uniform3fv(uniform('uFloor'), toOklab(parseColor(tones.floor)));
243
+ gl.uniform3fv(uniform('uDeep'), toOklab(parseColor(tones.deep)));
244
+ gl.uniform3fv(uniform('uBack'), toOklab(parseColor(tones.background)));
245
+ gl.uniform3fv(uniform('uLight'), parseColor(tones.light));
246
+ };
247
+
248
+ const { stage, stop } = createStage(el, {
249
+ fragment: FRAG,
250
+ alpha: false,
251
+ scale: isMobileGpu() ? 0.7 : 1,
252
+ still: speed === 0 && !interactive,
253
+ setup(stage) {
254
+ const { gl, uniform } = stage;
255
+ const scale = Math.max(0.1, config.scale ?? 1);
256
+ gl.uniform1f(uniform('uDensity'), DENSITY / scale);
257
+ gl.uniform1f(uniform('uTiles'), config.tiles ? TILES / scale : 0);
258
+ gl.uniform1f(uniform('uIntensity'), config.intensity ?? 1);
259
+ paint(stage);
260
+ },
261
+ resize({ gl, uniform, canvas }) {
262
+ gl.uniform4f(uniform('uQuiet'), ...quietUniform(config.quiet, canvas.width / canvas.height));
263
+ },
264
+ uniforms(stage, _now, dt) {
265
+ const { gl, uniform, canvas, still } = stage;
266
+ if (!still) {
267
+ time += dt * speed;
268
+ if (interactive && canHover()) follow(canvas.width / canvas.height, dt);
269
+ }
270
+ gl.uniform1f(uniform('uTime'), time);
271
+ gl.uniform4f(uniform('uPtr'), ptr.x, ptr.y, ptr.on, ptr.stir);
272
+ },
273
+ });
274
+ if (!stage) return stop;
275
+
276
+ const unwatch = watchColors(el, (next) => {
277
+ tones = causticTones({ colors: next.colors, background: config.background });
278
+ stage.gl.useProgram(stage.program);
279
+ paint(stage);
280
+ if (stage.still) stage.redraw();
281
+ });
282
+ return () => {
283
+ stop();
284
+ unwatch();
285
+ };
286
+ }
@@ -0,0 +1,33 @@
1
+ /** An RGB colour, each channel 0–1, in sRGB (as CSS writes it). */
2
+ export type Rgb = [number, number, number];
3
+
4
+ /** Reads `#rgb` or `#rrggbb` (the `#` optional). Throws a TypeError for anything else. */
5
+ export function parseColor(hex: string): Rgb;
6
+
7
+ /** Writes a colour as `#rrggbb`, clamping each channel to 0–1. */
8
+ export function toHex(rgb: Rgb): string;
9
+
10
+ /** sRGB to Oklab: L, a, b. */
11
+ export function toOklab(rgb: Rgb): [number, number, number];
12
+
13
+ /** Oklab back to sRGB, out-of-gamut channels clamped. */
14
+ export function fromOklab(lab: [number, number, number]): Rgb;
15
+
16
+ /**
17
+ * Turns a colour's hue in Oklab, moving it 60% of the way to the lightness
18
+ * where the new hue is most vivid and keeping its chroma where the gamut
19
+ * allows; `lighten` adds Oklab lightness after. A grey stays grey.
20
+ */
21
+ export function turnHue(rgb: Rgb, degrees: number, lighten?: number): Rgb;
22
+
23
+ /** Blends two colours in Oklab; `t` 0 is `from`, 1 is `to`. */
24
+ export function mixOklab(from: Rgb, to: Rgb, t: number): Rgb;
25
+
26
+ /**
27
+ * A point on a palette looped through its colours and back to the first,
28
+ * eased and blended in Oklab. `t` wraps. Throws a RangeError for an empty palette.
29
+ */
30
+ export function paletteAt(colors: Rgb[], t: number): Rgb;
31
+
32
+ /** GLSL (ES 1.00) defining `vec3 oklabMix(vec3 a, vec3 b, float t)`, sRGB in and out. */
33
+ export const GLSL_OKLAB: string;
@@ -0,0 +1,213 @@
1
+ /**
2
+ * @module @arraypress/effects-astro/color
3
+ *
4
+ * Colours as the effects take them: CSS hex in, 0–1 channels out, and
5
+ * blends made in Oklab rather than RGB. Mixed in RGB, two bright colours
6
+ * meet in a muddy middle (violet to cyan passes through grey); in Oklab,
7
+ * a space built so equal steps look equal, the middle stays as bright and
8
+ * as saturated as the ends.
9
+ *
10
+ * The same blend is available to shaders as GLSL (`GLSL_OKLAB`), so a
11
+ * palette loops the same way on the CPU (ink colours) and on the GPU (the
12
+ * light).
13
+ */
14
+
15
+ /**
16
+ * An RGB colour, each channel 0–1, in sRGB (as CSS writes it).
17
+ *
18
+ * @typedef {[number, number, number]} Rgb
19
+ */
20
+
21
+ /**
22
+ * Reads a CSS hex colour: `#rgb`, `#rrggbb` (or either without the `#`).
23
+ *
24
+ * @param {string} hex The colour.
25
+ * @returns {Rgb} Its channels, 0–1.
26
+ * @throws {TypeError} When it is not a 3- or 6-digit hex colour.
27
+ */
28
+ export function parseColor(hex) {
29
+ const h = String(hex).trim().replace(/^#/, '');
30
+ const full = h.length === 3 ? [...h].map((c) => c + c).join('') : h;
31
+ if (!/^[0-9a-f]{6}$/i.test(full)) throw new TypeError(`Not a hex colour: ${hex}`);
32
+ return /** @type {Rgb} */ ([0, 2, 4].map((i) => parseInt(full.slice(i, i + 2), 16) / 255));
33
+ }
34
+
35
+ /**
36
+ * Writes a colour back as CSS hex.
37
+ *
38
+ * @param {Rgb} rgb Channels, 0–1 (clamped).
39
+ * @returns {string} `#rrggbb`.
40
+ */
41
+ export function toHex(rgb) {
42
+ return `#${rgb.map((c) => Math.round(Math.min(1, Math.max(0, c)) * 255).toString(16).padStart(2, '0')).join('')}`;
43
+ }
44
+
45
+ /**
46
+ * One sRGB channel to linear light.
47
+ *
48
+ * @param {number} c The channel, 0–1.
49
+ * @returns {number} Linear, 0–1.
50
+ */
51
+ function toLinear(c) {
52
+ return c <= 0.04045 ? c / 12.92 : ((c + 0.055) / 1.055) ** 2.4;
53
+ }
54
+
55
+ /**
56
+ * One linear channel back to sRGB.
57
+ *
58
+ * @param {number} c Linear, 0–1.
59
+ * @returns {number} The channel, 0–1.
60
+ */
61
+ function toSrgb(c) {
62
+ const v = Math.max(0, c);
63
+ return v <= 0.0031308 ? v * 12.92 : 1.055 * v ** (1 / 2.4) - 0.055;
64
+ }
65
+
66
+ /**
67
+ * sRGB to Oklab (Björn Ottosson's matrices).
68
+ *
69
+ * @param {Rgb} rgb The colour.
70
+ * @returns {[number, number, number]} L, a, b.
71
+ */
72
+ export function toOklab(rgb) {
73
+ const [r, g, b] = rgb.map(toLinear);
74
+ const l = Math.cbrt(0.4122214708 * r + 0.5363325363 * g + 0.0514459929 * b);
75
+ const m = Math.cbrt(0.2119034982 * r + 0.6806995451 * g + 0.1073969566 * b);
76
+ const s = Math.cbrt(0.0883024619 * r + 0.2817188376 * g + 0.6299787005 * b);
77
+ return [
78
+ 0.2104542553 * l + 0.793617785 * m - 0.0040720468 * s,
79
+ 1.9779984951 * l - 2.428592205 * m + 0.4505937099 * s,
80
+ 0.0259040371 * l + 0.7827717662 * m - 0.808675766 * s,
81
+ ];
82
+ }
83
+
84
+ /**
85
+ * Oklab to linear light, unclamped (a channel outside 0–1 is out of gamut).
86
+ *
87
+ * @param {[number, number, number]} lab L, a, b.
88
+ * @returns {Rgb} Linear red, green and blue.
89
+ */
90
+ function oklabToLinear([L, a, b]) {
91
+ const l = (L + 0.3963377774 * a + 0.2158037573 * b) ** 3;
92
+ const m = (L - 0.1055613458 * a - 0.0638541728 * b) ** 3;
93
+ const s = (L - 0.0894841775 * a - 1.291485548 * b) ** 3;
94
+ return [
95
+ 4.0767416621 * l - 3.3077115913 * m + 0.2309699292 * s,
96
+ -1.2684380046 * l + 2.6097574011 * m - 0.3413193965 * s,
97
+ -0.0041960863 * l - 0.7034186147 * m + 1.707614701 * s,
98
+ ];
99
+ }
100
+
101
+ /**
102
+ * Oklab back to sRGB.
103
+ *
104
+ * @param {[number, number, number]} lab L, a, b.
105
+ * @returns {Rgb} The colour, 0–1 (out-of-gamut channels clamped).
106
+ */
107
+ export function fromOklab(lab) {
108
+ return /** @type {Rgb} */ (oklabToLinear(lab).map((c) => Math.min(1, toSrgb(c))));
109
+ }
110
+
111
+ /**
112
+ * The most chroma a lightness and hue can have and still be an sRGB colour.
113
+ *
114
+ * @param {number} L The lightness, 0–1.
115
+ * @param {number} hue The hue, in radians.
116
+ * @returns {number} The chroma (to within 1e-5).
117
+ */
118
+ function maxChroma(L, hue) {
119
+ /**
120
+ * Whether a chroma at this lightness and hue is in gamut.
121
+ *
122
+ * @param {number} c The chroma.
123
+ * @returns {boolean} True when every linear channel is within 0–1.
124
+ */
125
+ const fits = (c) => oklabToLinear([L, c * Math.cos(hue), c * Math.sin(hue)]).every((v) => v >= -1e-4 && v <= 1 + 1e-4);
126
+ let lo = 0;
127
+ let hi = 0.4;
128
+ for (let i = 0; i < 16; i++) {
129
+ const mid = (lo + hi) / 2;
130
+ if (fits(mid)) lo = mid;
131
+ else hi = mid;
132
+ }
133
+ return lo;
134
+ }
135
+
136
+ /**
137
+ * Turns a colour's hue in Oklab: a companion colour for a palette built
138
+ * from one. Each hue is most vivid at its own lightness (yellow light,
139
+ * blue deep), so the colour moves 60% of the way to that lightness rather
140
+ * than greying out at the old one, keeps its chroma where the gamut
141
+ * allows, and can be lifted on top.
142
+ *
143
+ * @param {Rgb} rgb The colour.
144
+ * @param {number} degrees Degrees to turn the hue by.
145
+ * @param {number} [lighten=0] Oklab lightness to add, after the move.
146
+ * @returns {Rgb} The turned colour; a grey stays grey (lifted).
147
+ */
148
+ export function turnHue(rgb, degrees, lighten = 0) {
149
+ const [L, a, b] = toOklab(rgb);
150
+ const chroma = Math.hypot(a, b);
151
+ const hue = Math.atan2(b, a) + (degrees * Math.PI) / 180;
152
+ let vivid = L;
153
+ let most = 0;
154
+ for (let l = 0.3; l <= 0.96; l += 0.02) {
155
+ const c = maxChroma(l, hue);
156
+ if (c > most) [most, vivid] = [c, l];
157
+ }
158
+ const lightness = chroma < 1e-4 ? L + lighten : L + (vivid - L) * 0.6 + lighten;
159
+ const to = Math.min(0.97, Math.max(0, lightness));
160
+ const c = Math.min(chroma, maxChroma(to, hue));
161
+ return fromOklab([to, c * Math.cos(hue), c * Math.sin(hue)]);
162
+ }
163
+
164
+ /**
165
+ * Blends two colours in Oklab.
166
+ *
167
+ * @param {Rgb} from The colour at 0.
168
+ * @param {Rgb} to The colour at 1.
169
+ * @param {number} t How far, 0–1.
170
+ * @returns {Rgb} The blend.
171
+ */
172
+ export function mixOklab(from, to, t) {
173
+ const a = toOklab(from);
174
+ const b = toOklab(to);
175
+ return fromOklab(/** @type {Rgb} */ (a.map((v, i) => v + (b[i] - v) * t)));
176
+ }
177
+
178
+ /**
179
+ * A point on a palette looped through its colours and back to the first,
180
+ * eased between them and blended in Oklab.
181
+ *
182
+ * @param {Rgb[]} colors The palette, at least one colour.
183
+ * @param {number} t Where on the loop; whole turns wrap (1.25 is 0.25).
184
+ * @returns {Rgb} The colour there.
185
+ * @throws {RangeError} With an empty palette.
186
+ */
187
+ export function paletteAt(colors, t) {
188
+ if (!colors.length) throw new RangeError('A palette needs at least one colour');
189
+ const x = (((t % 1) + 1) % 1) * colors.length;
190
+ const i = Math.floor(x);
191
+ const f = x - i;
192
+ return mixOklab(colors[i], colors[(i + 1) % colors.length], f * f * (3 - 2 * f));
193
+ }
194
+
195
+ /**
196
+ * GLSL (ES 1.00) for the same blend on the GPU: `oklabMix(a, b, t)` with
197
+ * sRGB colours in and out.
198
+ */
199
+ export const GLSL_OKLAB = `
200
+ vec3 oklabToLinear(vec3 c) { return mix(c / 12.92, pow((c + 0.055) / 1.055, vec3(2.4)), step(0.04045, c)); }
201
+ vec3 oklabToSrgb(vec3 c) { c = max(c, 0.0); return mix(c * 12.92, 1.055 * pow(c, vec3(1.0 / 2.4)) - 0.055, step(0.0031308, c)); }
202
+ vec3 toOklab(vec3 srgb) {
203
+ vec3 c = oklabToLinear(srgb);
204
+ vec3 lms = pow(mat3(0.4122214708, 0.2119034982, 0.0883024619, 0.5363325363, 0.6806995451, 0.2817188376, 0.0514459929, 0.1073969566, 0.6299787005) * c, vec3(1.0 / 3.0));
205
+ return mat3(0.2104542553, 1.9779984951, 0.0259040371, 0.7936177850, -2.4285922050, 0.7827717662, -0.0040720468, 0.4505937099, -0.8086757660) * lms;
206
+ }
207
+ vec3 fromOklab(vec3 lab) {
208
+ vec3 lms = mat3(1.0, 1.0, 1.0, 0.3963377774, -0.1055613458, -0.0894841775, 0.2158037573, -0.0638541728, -1.2914855480) * lab;
209
+ lms = lms * lms * lms;
210
+ return oklabToSrgb(mat3(4.0767416621, -1.2684380046, -0.0041960863, -3.3077115913, 2.6097574011, -0.7034186147, 0.2309699292, -0.3413193965, 1.7076147010) * lms);
211
+ }
212
+ vec3 oklabMix(vec3 a, vec3 b, float t) { return fromOklab(mix(toOklab(a), toOklab(b), t)); }
213
+ `;
@@ -0,0 +1,5 @@
1
+ /** Value noise at (x, y), 0–1, with its gradient: `[value, d/dx, d/dy]`. */
2
+ export function noiseGrad(x: number, y: number, out?: Float64Array): Float64Array;
3
+
4
+ /** The curl of two drifting octaves of noise at (x, y) and time `t`: a swirling flow with no sources or sinks, `[across, up]`. */
5
+ export function curl<T extends Float64Array | number[]>(x: number, y: number, t: number, out?: T): T;
@@ -0,0 +1,81 @@
1
+ /**
2
+ * @module @arraypress/effects-astro/curl
3
+ *
4
+ * A flow field for particles stepped in JavaScript: the curl of smooth
5
+ * noise, so it swirls without sources or sinks (nothing piles up or thins
6
+ * out as it moves along it), changing slowly over time.
7
+ *
8
+ * The noise is value noise with its gradient worked out exactly alongside
9
+ * it, so a sample of the flow costs two noise lookups, not the four or
10
+ * more a finite difference would.
11
+ */
12
+
13
+ /** Scratch for one noise sample: the value, then its gradient across and up. */
14
+ const sample = new Float64Array(3);
15
+
16
+ /**
17
+ * Hashes a lattice point to a number 0–1, the same every time.
18
+ *
19
+ * @param {number} ix The point's column.
20
+ * @param {number} iy The point's row.
21
+ * @returns {number} 0 (inclusive) to 1 (exclusive).
22
+ */
23
+ function hash(ix, iy) {
24
+ let h = Math.imul(ix, 0x27d4eb2d) ^ Math.imul(iy, 0x165667b1);
25
+ h = Math.imul(h ^ (h >>> 15), 0x85ebca6b);
26
+ h = Math.imul(h ^ (h >>> 13), 0xc2b2ae35);
27
+ return ((h ^ (h >>> 16)) >>> 0) / 4294967296;
28
+ }
29
+
30
+ /**
31
+ * Value noise at a point, with its gradient: smooth, 0–1, varying over a
32
+ * lattice cell of 1.
33
+ *
34
+ * @param {number} x Across.
35
+ * @param {number} y Up.
36
+ * @param {Float64Array} [out] Where to write the value and gradient.
37
+ * @returns {Float64Array} `[value, d/dx, d/dy]`.
38
+ */
39
+ export function noiseGrad(x, y, out = new Float64Array(3)) {
40
+ const ix = Math.floor(x);
41
+ const iy = Math.floor(y);
42
+ const fx = x - ix;
43
+ const fy = y - iy;
44
+ const ux = fx * fx * (3 - 2 * fx);
45
+ const uy = fy * fy * (3 - 2 * fy);
46
+ const a = hash(ix, iy);
47
+ const b = hash(ix + 1, iy);
48
+ const c = hash(ix, iy + 1);
49
+ const d = hash(ix + 1, iy + 1);
50
+ const k1 = b - a;
51
+ const k2 = c - a;
52
+ const k3 = a - b - c + d;
53
+ out[0] = a + k1 * ux + k2 * uy + k3 * ux * uy;
54
+ out[1] = 6 * fx * (1 - fx) * (k1 + k3 * uy);
55
+ out[2] = 6 * fy * (1 - fy) * (k2 + k3 * ux);
56
+ return out;
57
+ }
58
+
59
+ /**
60
+ * The flow at a point and time: the curl of two octaves of noise, each
61
+ * drifting its own way, so the swirls change shape as well as move. Its
62
+ * speed is mostly under 1, in units of the noise's cell a second.
63
+ *
64
+ * @param {number} x Across, in noise cells.
65
+ * @param {number} y Up, in noise cells.
66
+ * @param {number} t The time, seconds (scale it for a faster or slower field).
67
+ * @param {Float64Array | number[]} [out] Where to write the flow.
68
+ * @returns {Float64Array | number[]} `[across, up]`.
69
+ */
70
+ export function curl(x, y, t, out = [0, 0]) {
71
+ noiseGrad(x + t * 0.31, y - t * 0.17, sample);
72
+ let dx = sample[1];
73
+ let dy = sample[2];
74
+ noiseGrad(x * 2.1 - t * 0.23 + 17.3, y * 2.1 + t * 0.29 + 5.1, sample);
75
+ dx += sample[1] * 0.5 * 2.1;
76
+ dy += sample[2] * 0.5 * 2.1;
77
+ // The curl of a scalar field: its gradient turned a quarter.
78
+ out[0] = dy * 0.5;
79
+ out[1] = -dx * 0.5;
80
+ return out;
81
+ }
@@ -0,0 +1,9 @@
1
+ export interface CursorConfig {
2
+ /** How quickly the ring catches up, 0–1 (1 is no trail). Default 0.18. */
3
+ ease?: number;
4
+ /** What makes the ring swell. Default `a, button, [data-cursor]`. */
5
+ targets?: string;
6
+ }
7
+
8
+ /** Starts the custom cursor in `el` (holding `.dot`, `.ring`, `.label`). Returns a function that stops it. */
9
+ export function cursor(el: HTMLElement, config: CursorConfig): () => void;