@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,582 @@
1
+ /**
2
+ * @module @arraypress/effects-astro/ink-field/sim
3
+ *
4
+ * The ink field's fluid: a small stable-fluids sim (velocity, pressure,
5
+ * dye) that six ambient emitters keep moving and the pointer stirs.
6
+ * Adapted from Trirat's ink shader for LumiThemes (smoke-sim.ts), itself
7
+ * ported from a reference stable-fluids sim; its constants, emitters and
8
+ * passes are his, unchanged. `kick` (a beat of the sound the field follows)
9
+ * is ours.
10
+ *
11
+ * - Velocity and pressure live on a coarse grid (GRID cells on the short
12
+ * side); the dye on one DYE_SCALE times finer, for sharper edges.
13
+ * - Forces and decay are scaled by the frame time, not the frame.
14
+ * - The grid is a fixed size, whatever the device pixel ratio.
15
+ * - It renders into float textures (half-float where float cannot be
16
+ * filtered): `createInkSim` returns null when the GPU has neither.
17
+ *
18
+ * Runs in WebGL 1 or 2. Every pass leaves its own program, framebuffer and
19
+ * viewport bound: whoever draws the dye restores theirs.
20
+ */
21
+
22
+ import { createProgram, bindTriangle, VERTEX } from '../gl.js';
23
+ import { ADVECT, DIVERGENCE, PRESSURE, GRADIENT, CURL, VORTICITY, SPLAT } from './shaders.js';
24
+
25
+ /**
26
+ * @typedef {import('../gl.js').GL} GL
27
+ * @typedef {import('../gl.js').Program} Program
28
+ */
29
+
30
+ /**
31
+ * A texture and the framebuffer that draws into it.
32
+ *
33
+ * @typedef {object} Target
34
+ * @property {WebGLFramebuffer} fbo The framebuffer.
35
+ * @property {WebGLTexture} tex The texture.
36
+ * @property {number} w Its width, texels.
37
+ * @property {number} h Its height, texels.
38
+ */
39
+
40
+ /**
41
+ * Two targets, read from one and written to the other, then swapped.
42
+ *
43
+ * @typedef {object} Pair
44
+ * @property {Target} read The one read this pass.
45
+ * @property {Target} write The one written this pass.
46
+ * @property {() => void} swap Swaps them.
47
+ */
48
+
49
+ /**
50
+ * A float texture format the GPU can render to and filter.
51
+ *
52
+ * @typedef {object} Format
53
+ * @property {number} internal The internal format.
54
+ * @property {number} type The texel type.
55
+ */
56
+
57
+ /**
58
+ * @typedef {object} InkSim
59
+ * @property {(width: number, height: number) => void} resize Fits the grid to
60
+ * a canvas's shape; rebuilds (and reseeds) the sim if the grid changed.
61
+ * @property {(dt: number) => void} step Advances the sim by `dt` seconds, emitters included.
62
+ * @property {(x: number, y: number, dx: number, dy: number, dt: number) => void} stir
63
+ * Stirs the ink at a point (0–1 across, 0–1 up) by how far the pointer
64
+ * just moved (dx, dy, the same units) in `dt` seconds.
65
+ * @property {(x: number, y: number) => void} drop Drops a dollop of ink at a
66
+ * point (0–1 across, 0–1 up), pushed out on every side so it blooms open.
67
+ * @property {(strength: number) => void} kick Stirs the whole field at once, as a beat would: every
68
+ * emitter pushes hard along its way, and lays a little ink. `strength` 0–1.
69
+ * @property {(frames: number) => void} warm Runs `frames` steps of 1/60 s at once.
70
+ * @property {(unit: number) => [number, number]} bindDye Binds the dye to a
71
+ * texture unit, for the display; returns its texel size.
72
+ * @property {() => void} destroy Frees its textures, framebuffers, programs and buffer.
73
+ */
74
+
75
+ /** A drop: how many pushes ring it, how hard each pushes (grid cells a second), and how much ink it holds. */
76
+ const DROP_PUSHES = 6;
77
+ const DROP_PUSH = 40;
78
+ const DROP_INK = 0.55;
79
+
80
+ /** The ambient emitters: where they orbit, and where on the palette they sit. */
81
+ const EMITTERS = [
82
+ { cx: 0.22, cy: 0.35, rx: 0.18, ry: 0.28, speed: 0.28, phase: 0.0 },
83
+ { cx: 0.78, cy: 0.3, rx: 0.18, ry: 0.25, speed: 0.24, phase: 0.17 },
84
+ { cx: 0.25, cy: 0.7, rx: 0.2, ry: 0.24, speed: 0.32, phase: 0.34 },
85
+ { cx: 0.75, cy: 0.72, rx: 0.18, ry: 0.24, speed: 0.26, phase: 0.5 },
86
+ { cx: 0.5, cy: 0.5, rx: 0.32, ry: 0.28, speed: 0.2, phase: 0.67 },
87
+ { cx: 0.5, cy: 0.18, rx: 0.38, ry: 0.14, speed: 0.35, phase: 0.84 },
88
+ ];
89
+
90
+ /** A kick: how hard each emitter pushes (grid cells a second, at full strength), and how much ink it lays. */
91
+ const KICK_PUSH = 50;
92
+ const KICK_INK = 0.025;
93
+
94
+ /** Where the first spread of ink is laid, 0–1 across and up. */
95
+ const SEEDS = [
96
+ [0.15, 0.2], [0.5, 0.18], [0.85, 0.22], [0.12, 0.55], [0.45, 0.5], [0.8, 0.48],
97
+ [0.2, 0.8], [0.55, 0.78], [0.88, 0.75], [0.35, 0.35], [0.65, 0.65], [0.5, 0.9],
98
+ ];
99
+
100
+ /** Grid cells on the short side; the long side follows the aspect. */
101
+ const GRID = 80;
102
+ /** The dye lives on a finer grid than the velocity: sharper edges for the light to read. */
103
+ const DYE_SCALE = 3;
104
+ const PRESSURE_ITERATIONS = 6;
105
+ /** Velocity decays to this fraction per 1/60 s, dye to the other. */
106
+ const VELOCITY_KEEP = 0.989;
107
+ const DYE_KEEP = 0.9975;
108
+ const CURL_STRENGTH = 6;
109
+ /** How finely ink is striped as it is laid (the stripe pattern's frequency). */
110
+ const STRIPES = 110;
111
+
112
+ /**
113
+ * The float formats this context offers for render targets, best first:
114
+ * full float where it can be filtered, then half float.
115
+ *
116
+ * @param {GL} gl The context.
117
+ * @returns {Format[]} The candidates (not yet proven complete).
118
+ */
119
+ function floatFormats(gl) {
120
+ /** @type {Format[]} */
121
+ const formats = [];
122
+ if (typeof WebGL2RenderingContext !== 'undefined' && gl instanceof WebGL2RenderingContext) {
123
+ const color = gl.getExtension('EXT_color_buffer_float');
124
+ if (color && gl.getExtension('OES_texture_float_linear')) formats.push({ internal: gl.RGBA32F, type: gl.FLOAT });
125
+ if (color || gl.getExtension('EXT_color_buffer_half_float')) formats.push({ internal: gl.RGBA16F, type: gl.HALF_FLOAT });
126
+ return formats;
127
+ }
128
+ const full = gl.getExtension('OES_texture_float');
129
+ if (full && gl.getExtension('OES_texture_float_linear')) {
130
+ gl.getExtension('WEBGL_color_buffer_float');
131
+ formats.push({ internal: gl.RGBA, type: gl.FLOAT });
132
+ }
133
+ const half = gl.getExtension('OES_texture_half_float');
134
+ if (half && gl.getExtension('OES_texture_half_float_linear')) {
135
+ gl.getExtension('EXT_color_buffer_half_float');
136
+ formats.push({ internal: gl.RGBA, type: half.HALF_FLOAT_OES });
137
+ }
138
+ return formats;
139
+ }
140
+
141
+ /**
142
+ * Builds the sim, or returns null when this GPU cannot render to a float
143
+ * (or half-float) texture it can also filter, or a pass will not compile.
144
+ *
145
+ * @param {GL} gl The context to draw with.
146
+ * @returns {InkSim | null} The sim, or null.
147
+ */
148
+ export function createInkSim(gl) {
149
+ /**
150
+ * Makes a filtered, edge-clamped texture.
151
+ *
152
+ * @param {number} w Width, texels.
153
+ * @param {number} h Height, texels.
154
+ * @param {Format} format Its format.
155
+ * @returns {WebGLTexture} The texture.
156
+ */
157
+ const makeTexture = (w, h, format) => {
158
+ const tex = /** @type {WebGLTexture} */ (gl.createTexture());
159
+ gl.bindTexture(gl.TEXTURE_2D, tex);
160
+ gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR);
161
+ gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR);
162
+ gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
163
+ gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
164
+ gl.texImage2D(gl.TEXTURE_2D, 0, format.internal, w, h, 0, gl.RGBA, format.type, null);
165
+ return tex;
166
+ };
167
+
168
+ /**
169
+ * Makes a render target, if the framebuffer is complete with this format.
170
+ *
171
+ * @param {number} w Width, texels.
172
+ * @param {number} h Height, texels.
173
+ * @param {Format} format Its format.
174
+ * @returns {Target | null} The target, or null (nothing left allocated).
175
+ */
176
+ const makeTarget = (w, h, format) => {
177
+ const tex = makeTexture(w, h, format);
178
+ const fbo = /** @type {WebGLFramebuffer} */ (gl.createFramebuffer());
179
+ gl.bindFramebuffer(gl.FRAMEBUFFER, fbo);
180
+ gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_2D, tex, 0);
181
+ const ok = gl.checkFramebufferStatus(gl.FRAMEBUFFER) === gl.FRAMEBUFFER_COMPLETE;
182
+ gl.bindFramebuffer(gl.FRAMEBUFFER, null);
183
+ if (!ok) {
184
+ gl.deleteFramebuffer(fbo);
185
+ gl.deleteTexture(tex);
186
+ return null;
187
+ }
188
+ return { fbo, tex, w, h };
189
+ };
190
+
191
+ /**
192
+ * Frees a target.
193
+ *
194
+ * @param {Target | undefined} t The target, if there is one.
195
+ */
196
+ const free = (t) => {
197
+ if (!t) return;
198
+ gl.deleteFramebuffer(t.fbo);
199
+ gl.deleteTexture(t.tex);
200
+ };
201
+
202
+ const format = floatFormats(gl).find((candidate) => {
203
+ const probe = makeTarget(4, 4, candidate);
204
+ free(probe ?? undefined);
205
+ return probe !== null;
206
+ });
207
+ if (!format) return null;
208
+
209
+ /** @type {Program[]} */
210
+ const programs = [];
211
+ /**
212
+ * Compiles one pass, with the shared vertex shader.
213
+ *
214
+ * @param {string} fragment The pass's fragment shader.
215
+ * @returns {Program | null} The program, or null (the reason in the console).
216
+ */
217
+ const link = (fragment) => {
218
+ const program = createProgram(gl, VERTEX, fragment);
219
+ if (program) programs.push(program);
220
+ return program;
221
+ };
222
+
223
+ /**
224
+ * Deletes every program made, and the shaders in them.
225
+ */
226
+ const freePrograms = () => {
227
+ for (const { program } of programs) {
228
+ gl.getAttachedShaders(program)?.forEach((shader) => gl.deleteShader(shader));
229
+ gl.deleteProgram(program);
230
+ }
231
+ };
232
+
233
+ const advectP = link(ADVECT);
234
+ const divergenceP = link(DIVERGENCE);
235
+ const pressureP = link(PRESSURE);
236
+ const gradientP = link(GRADIENT);
237
+ const curlP = link(CURL);
238
+ const vorticityP = link(VORTICITY);
239
+ const splatP = link(SPLAT);
240
+ if (!advectP || !divergenceP || !pressureP || !gradientP || !curlP || !vorticityP || !splatP) {
241
+ freePrograms();
242
+ return null;
243
+ }
244
+
245
+ // One triangle that covers the target, bound again before each batch of passes.
246
+ const triangle = bindTriangle(gl);
247
+
248
+ /**
249
+ * Binds a texture to a unit and points a sampler at it.
250
+ *
251
+ * @param {Program} prog The program in use.
252
+ * @param {string} name The sampler.
253
+ * @param {number} unit The texture unit.
254
+ * @param {WebGLTexture} tex The texture.
255
+ */
256
+ const bind = (prog, name, unit, tex) => {
257
+ gl.uniform1i(prog.uniform(name), unit);
258
+ gl.activeTexture(gl.TEXTURE0 + unit);
259
+ gl.bindTexture(gl.TEXTURE_2D, tex);
260
+ };
261
+
262
+ /** Draws the triangle: one pass. */
263
+ const draw = () => gl.drawArrays(gl.TRIANGLES, 0, 3);
264
+
265
+ /** @type {Pair} */
266
+ let vel;
267
+ /** @type {Pair} */
268
+ let dye;
269
+ /** @type {Pair} */
270
+ let pres;
271
+ /** @type {Target} */
272
+ let div;
273
+ /** @type {Target} */
274
+ let curl;
275
+ let simW = 0;
276
+ let simH = 0;
277
+ let simTime = 0;
278
+ /** Where each emitter was last frame, for its direction of travel. */
279
+ let last = EMITTERS.map((e) => ({ x: e.cx, y: e.cy }));
280
+
281
+ /**
282
+ * Makes a target of the sim's format; it is known to be complete.
283
+ *
284
+ * @param {number} w Width, texels.
285
+ * @param {number} h Height, texels.
286
+ * @returns {Target} The target.
287
+ */
288
+ const target = (w, h) => /** @type {Target} */ (makeTarget(w, h, format));
289
+
290
+ /**
291
+ * Makes a read/write pair of targets.
292
+ *
293
+ * @param {number} w Width, texels.
294
+ * @param {number} h Height, texels.
295
+ * @returns {Pair} The pair.
296
+ */
297
+ const pair = (w, h) => {
298
+ /** @type {Pair} */
299
+ const p = {
300
+ read: target(w, h),
301
+ write: target(w, h),
302
+ swap() {
303
+ [p.read, p.write] = [p.write, p.read];
304
+ },
305
+ };
306
+ return p;
307
+ };
308
+
309
+ /**
310
+ * Draws into a target from here on.
311
+ *
312
+ * @param {Target} t The target.
313
+ */
314
+ const to = (t) => {
315
+ gl.bindFramebuffer(gl.FRAMEBUFFER, t.fbo);
316
+ gl.viewport(0, 0, t.w, t.h);
317
+ };
318
+
319
+ /**
320
+ * Clears a target to nothing.
321
+ *
322
+ * @param {Target} t The target.
323
+ */
324
+ const clear = (t) => {
325
+ to(t);
326
+ gl.clearColor(0, 0, 0, 0);
327
+ gl.clear(gl.COLOR_BUFFER_BIT);
328
+ };
329
+
330
+ /**
331
+ * Sets a pass's `uTxl`, one velocity-grid cell in uv.
332
+ *
333
+ * @param {Program} prog The program in use.
334
+ */
335
+ const txl = (prog) => gl.uniform2f(prog.uniform('uTxl'), 1 / simW, 1 / simH);
336
+
337
+ /**
338
+ * Moves a field along the velocity, decaying it.
339
+ *
340
+ * @param {Pair} field The field.
341
+ * @param {number} diss What it keeps of itself this step.
342
+ * @param {number} dt The step, seconds.
343
+ */
344
+ const advect = (field, diss, dt) => {
345
+ gl.useProgram(advectP.program);
346
+ to(field.write);
347
+ bind(advectP, 'uVel', 0, vel.read.tex);
348
+ bind(advectP, 'uQty', 1, field.read.tex);
349
+ txl(advectP);
350
+ gl.uniform1f(advectP.uniform('uDt'), dt);
351
+ gl.uniform1f(advectP.uniform('uDiss'), diss);
352
+ draw();
353
+ field.swap();
354
+ };
355
+
356
+ /**
357
+ * Adds a soft round splat to a field.
358
+ *
359
+ * @param {Pair} field The field.
360
+ * @param {number} x Where, 0–1 across.
361
+ * @param {number} y Where, 0–1 up.
362
+ * @param {number} a What it adds to the first channel.
363
+ * @param {number} b What it adds to the second.
364
+ * @param {number} c What it adds to the third, laid in stripes.
365
+ * @param {number} radius Its size, in uv of the height.
366
+ * @param {number} [angle=0] The stripes' angle, radians.
367
+ */
368
+ const splat = (field, x, y, a, b, c, radius, angle = 0) => {
369
+ gl.useProgram(splatP.program);
370
+ to(field.write);
371
+ bind(splatP, 'uSrc', 0, field.read.tex);
372
+ gl.uniform2f(splatP.uniform('uAt'), x, y);
373
+ gl.uniform3f(splatP.uniform('uVal'), a, b, c);
374
+ gl.uniform2f(splatP.uniform('uStripe'), angle, STRIPES);
375
+ gl.uniform1f(splatP.uniform('uRad'), radius);
376
+ gl.uniform1f(splatP.uniform('uAsp'), simW / simH);
377
+ draw();
378
+ field.swap();
379
+ };
380
+
381
+ /**
382
+ * Puts ink of one density and palette phase at a point, laid in stripes at `angle`.
383
+ *
384
+ * @param {number} x Where, 0–1 across.
385
+ * @param {number} y Where, 0–1 up.
386
+ * @param {number} density How much ink.
387
+ * @param {number} phase Where on the palette, 0–1.
388
+ * @param {number} radius Its size, in uv of the height.
389
+ * @param {number} [angle=0] The stripes' angle, radians.
390
+ */
391
+ const ink = (x, y, density, phase, radius, angle = 0) => splat(dye, x, y, density, density * phase, density, radius, angle);
392
+
393
+ /** Makes the velocity divergence-free: divergence, pressure solve, gradient subtracted. */
394
+ const project = () => {
395
+ gl.useProgram(divergenceP.program);
396
+ to(div);
397
+ bind(divergenceP, 'uVel', 0, vel.read.tex);
398
+ txl(divergenceP);
399
+ draw();
400
+
401
+ clear(pres.read);
402
+ clear(pres.write);
403
+ for (let i = 0; i < PRESSURE_ITERATIONS; i++) {
404
+ gl.useProgram(pressureP.program);
405
+ to(pres.write);
406
+ bind(pressureP, 'uPres', 0, pres.read.tex);
407
+ bind(pressureP, 'uDiv', 1, div.tex);
408
+ txl(pressureP);
409
+ draw();
410
+ pres.swap();
411
+ }
412
+
413
+ gl.useProgram(gradientP.program);
414
+ to(vel.write);
415
+ bind(gradientP, 'uPres', 0, pres.read.tex);
416
+ bind(gradientP, 'uVel', 1, vel.read.tex);
417
+ txl(gradientP);
418
+ draw();
419
+ vel.swap();
420
+ };
421
+
422
+ /**
423
+ * Vorticity confinement: the curl, then a push along it.
424
+ *
425
+ * @param {number} dt The step, seconds.
426
+ */
427
+ const vorticity = (dt) => {
428
+ gl.useProgram(curlP.program);
429
+ to(curl);
430
+ bind(curlP, 'uVel', 0, vel.read.tex);
431
+ txl(curlP);
432
+ draw();
433
+
434
+ gl.useProgram(vorticityP.program);
435
+ to(vel.write);
436
+ bind(vorticityP, 'uVel', 0, vel.read.tex);
437
+ bind(vorticityP, 'uCurl', 1, curl.tex);
438
+ txl(vorticityP);
439
+ gl.uniform1f(vorticityP.uniform('uStrength'), CURL_STRENGTH);
440
+ gl.uniform1f(vorticityP.uniform('uDt'), dt);
441
+ draw();
442
+ vel.swap();
443
+ };
444
+
445
+ /**
446
+ * The emitters: small orbiting sources that keep the whole field alive.
447
+ *
448
+ * @param {number} dt The step, seconds.
449
+ */
450
+ const emit = (dt) => {
451
+ const frames = dt * 60;
452
+ EMITTERS.forEach((e, i) => {
453
+ const x = Math.min(0.95, Math.max(0.05, e.cx + Math.cos(simTime * e.speed + e.phase * 6.28) * e.rx));
454
+ const y = Math.min(0.95, Math.max(0.05, e.cy + Math.sin(simTime * e.speed * 0.73 + e.phase * 6.28 + 1.2) * e.ry));
455
+ const dx = x - last[i].x;
456
+ const dy = y - last[i].y;
457
+ const len = Math.hypot(dx, dy) + 1e-6;
458
+ const push = (8 + 3 * Math.sin(i * 2.1)) * frames;
459
+ splat(vel, x, y, (dx / len) * push, (dy / len) * push, 0, 0.055);
460
+ // Each drifts a little along the palette.
461
+ const phase = (e.phase + 0.1 * Math.sin(simTime * 0.28 + i)) % 1;
462
+ ink(x, y, Math.min(dt * 12, 0.16) * 0.65, phase < 0 ? phase + 1 : phase, 0.055, i * 1.9 + simTime * 0.15);
463
+ last[i] = { x, y };
464
+ });
465
+ };
466
+
467
+ /** A first spread of ink, so the field is not empty before the emitters catch up. */
468
+ const seed = () => {
469
+ SEEDS.forEach(([x, y], i) => {
470
+ splat(vel, x, y, (Math.random() - 0.5) * 8, (Math.random() - 0.5) * 8, 0, 0.1);
471
+ ink(x, y, 0.5, (i / SEEDS.length + 0.05 * Math.random()) % 1, 0.1, Math.random() * 6.28);
472
+ });
473
+ };
474
+
475
+ /** Frees every field (none on the first build). */
476
+ const freeFields = () => {
477
+ // The fields are unset until the first build.
478
+ for (const p of [vel, dye, pres]) {
479
+ if (!p) continue;
480
+ free(p.read);
481
+ free(p.write);
482
+ }
483
+ free(div);
484
+ free(curl);
485
+ };
486
+
487
+ /**
488
+ * Makes every field at a grid size, clear, and seeds it.
489
+ *
490
+ * @param {number} w Grid width, cells.
491
+ * @param {number} h Grid height, cells.
492
+ */
493
+ const build = (w, h) => {
494
+ freeFields();
495
+ simW = w;
496
+ simH = h;
497
+ vel = pair(w, h);
498
+ dye = pair(w * DYE_SCALE, h * DYE_SCALE);
499
+ pres = pair(w, h);
500
+ div = target(w, h);
501
+ curl = target(w, h);
502
+ [vel.read, vel.write, dye.read, dye.write].forEach(clear);
503
+ last = EMITTERS.map((e) => ({ x: e.cx, y: e.cy }));
504
+ seed();
505
+ };
506
+
507
+ /**
508
+ * One step of the sim.
509
+ *
510
+ * @param {number} dt The step, seconds.
511
+ */
512
+ const step = (dt) => {
513
+ const frames = dt * 60;
514
+ simTime += dt;
515
+ advect(vel, Math.pow(VELOCITY_KEEP, frames), dt);
516
+ vorticity(dt);
517
+ emit(dt);
518
+ project();
519
+ advect(dye, Math.pow(DYE_KEEP, frames), dt);
520
+ };
521
+
522
+ return {
523
+ resize(width, height) {
524
+ const aspect = width / height;
525
+ const w = aspect >= 1 ? Math.round(GRID * aspect) : GRID;
526
+ const h = aspect >= 1 ? GRID : Math.round(GRID / aspect);
527
+ if (w === simW && h === simH) return;
528
+ bindTriangle(gl, triangle);
529
+ build(w, h);
530
+ },
531
+ step(dt) {
532
+ bindTriangle(gl, triangle);
533
+ step(dt);
534
+ },
535
+ drop(x, y) {
536
+ bindTriangle(gl, triangle);
537
+ // A ring of pushes outward, so the drop opens rather than sitting there.
538
+ for (let i = 0; i < DROP_PUSHES; i++) {
539
+ const a = (i / DROP_PUSHES) * Math.PI * 2;
540
+ splat(vel, x + Math.cos(a) * 0.015, y + Math.sin(a) * 0.015, Math.cos(a) * DROP_PUSH, Math.sin(a) * DROP_PUSH, 0, 0.03);
541
+ }
542
+ ink(x, y, DROP_INK, Math.random(), 0.06, Math.random() * Math.PI * 2);
543
+ },
544
+ stir(x, y, dx, dy, dt) {
545
+ const len = Math.hypot(dx, dy);
546
+ const speed = len / Math.max(dt, 1e-3);
547
+ // No input, no ink: the eased pointer's tail dies out here.
548
+ if (speed < 0.04) return;
549
+ bindTriangle(gl, triangle);
550
+ const strength = Math.min(speed * 3.3, 1);
551
+ const frames = dt * 60;
552
+ splat(vel, x, y, (dx / len) * strength * 25 * frames, (dy / len) * strength * 25 * frames, 0, 0.04);
553
+ // Smaller and fainter than the ambient ink, and it fades as the hand slows.
554
+ const phase = 0.5 + 0.5 * Math.sin(simTime * 0.4);
555
+ ink(x, y, 0.06 * strength * frames, phase, 0.035, simTime * 0.7);
556
+ },
557
+ kick(strength) {
558
+ bindTriangle(gl, triangle);
559
+ EMITTERS.forEach((e, i) => {
560
+ const { x, y } = last[i];
561
+ // Along the emitter's way round its orbit, so the beat swirls rather than bursts.
562
+ const a = simTime * e.speed + e.phase * 6.28 + Math.PI / 2;
563
+ splat(vel, x, y, Math.cos(a) * KICK_PUSH * strength, Math.sin(a) * KICK_PUSH * strength, 0, 0.09);
564
+ ink(x, y, KICK_INK * strength, e.phase, 0.05, simTime * 0.5 + i);
565
+ });
566
+ },
567
+ warm(frames) {
568
+ bindTriangle(gl, triangle);
569
+ for (let i = 0; i < frames; i++) step(1 / 60);
570
+ },
571
+ bindDye(unit) {
572
+ gl.activeTexture(gl.TEXTURE0 + unit);
573
+ gl.bindTexture(gl.TEXTURE_2D, dye.read.tex);
574
+ return [1 / dye.read.w, 1 / dye.read.h];
575
+ },
576
+ destroy() {
577
+ freeFields();
578
+ freePrograms();
579
+ gl.deleteBuffer(triangle);
580
+ },
581
+ };
582
+ }
@@ -0,0 +1,39 @@
1
+ import type { PaletteName } from './palettes.js';
2
+ import type { AudioSource } from './audio.js';
3
+
4
+ export interface InkFieldConfig {
5
+ /** One of the shared palettes (`PALETTES`, any case). Default `'Lumetri'`. */
6
+ palette?: PaletteName | (string & {});
7
+ /** Three colours as CSS hex, in place of the palette's (fewer are looped). Default the palette's. */
8
+ colors?: string[];
9
+ /** A glint colour, in place of the palette's (unused by the ink look). Default the palette's. */
10
+ glint?: string;
11
+ /** The pointer stirs the ink and the lamp follows it, on devices that hover. Default true. */
12
+ interactive?: boolean;
13
+ /** A press drops ink that blooms open (not on links, buttons or fields). Off by default: on a page-wide field, stray clicks would splash. Default false. */
14
+ drops?: boolean;
15
+ /** Where the lamp rests when the pointer is away, 0–1 from the top left. Default `{ x: 0.14, y: 0.14 }`. */
16
+ lamp?: { x?: number; y?: number };
17
+ /** Hold one frame of settled ink and never animate, as with reduced motion: for a field behind body copy. Default false. */
18
+ still?: boolean;
19
+ /** Share of the pixel ratio drawn at. Default 0.5. */
20
+ scale?: number;
21
+ /** Follow the sound playing: `'page'` for whatever plays, or a CSS selector for an `<audio>`/`<video>` or a player's container (a WaveformPlayer). Kicks stir the ink and it brightens with the beat; while nothing plays it keeps its own motion. Default none. */
22
+ audio?: AudioSource;
23
+ }
24
+
25
+ /** The settings an ink field runs with: three colours (the palette's, or those given over it), a glint, the lamp's rest (0–1 from the top left) and the rest, defaults filled in. */
26
+ export function inkFieldSettings(config?: InkFieldConfig): {
27
+ colors: string[];
28
+ glint: string;
29
+ lamp: { x: number; y: number };
30
+ interactive: boolean;
31
+ still: boolean;
32
+ scale: number;
33
+ };
34
+
35
+ /** The CSS `background` that stands in without a float render target: the colours as still pools of light on the water's colour. */
36
+ export function inkFallback(colors: string[]): string;
37
+
38
+ /** Starts an ink field in `el`'s canvas. Returns a function that stops it. */
39
+ export function inkField(el: HTMLElement, config: InkFieldConfig): () => void;