@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,47 @@
1
+ import type { GL } from './gl.js';
2
+
3
+ /** A running full-canvas effect: its context, canvas and program. */
4
+ export interface Stage {
5
+ gl: GL;
6
+ canvas: HTMLCanvasElement;
7
+ program: WebGLProgram;
8
+ uniform(name: string): WebGLUniformLocation | null;
9
+ /** True with prefers-reduced-motion or the `still` option: one frame, redrawn on changes. */
10
+ still: boolean;
11
+ /** The pixel ratio the canvas is drawn at. */
12
+ ratio: number;
13
+ /** Draws a frame now. */
14
+ redraw(): void;
15
+ }
16
+
17
+ export interface StageOptions {
18
+ /** The fragment shader; `uRes` (canvas size in pixels) is set for it. */
19
+ fragment: string;
20
+ /** A vertex shader of its own (position attribute at location 0). Default passes the triangle through. */
21
+ vertex?: string;
22
+ /** Transparent (premultiplied) or opaque canvas. Default true. */
23
+ alpha?: boolean;
24
+ /** Share of the pixel ratio to draw at. Default 1. */
25
+ scale?: number;
26
+ /** Lower the resolution while frames run slow (a weak GPU), down to half; a step that does not help is taken back and adjusting stops. Default true. */
27
+ adaptive?: boolean;
28
+ /** Accept WebGL 1 without WebGL 2. Default true. */
29
+ webgl1?: boolean;
30
+ /** Smooth the edges of geometry drawn into it (multisampling): for effects that draw meshes. Default false. */
31
+ antialias?: boolean;
32
+ /** One frame (redrawn after resizes and redraws), never the loop, as with reduced motion. Default false. */
33
+ still?: boolean;
34
+ /** Once, after the context is made; returns a clean-up, or false when the effect cannot run here (the canvas is removed). */
35
+ setup?(stage: Stage): (() => void) | false | void;
36
+ /** After each resize. */
37
+ resize?(stage: Stage): void;
38
+ /** Each frame, before the program is bound (free to run other passes). */
39
+ update?(stage: Stage, now: number, dt: number): void;
40
+ /** Each frame, with the program bound. */
41
+ uniforms?(stage: Stage, now: number, dt: number): void;
42
+ /** Each frame, after the effect's pass: draw on top of it. */
43
+ after?(stage: Stage, now: number, dt: number): void;
44
+ }
45
+
46
+ /** Starts a full-canvas effect in `el`'s canvas. `stage` is null without WebGL (the canvas is removed). */
47
+ export function createStage(el: HTMLElement, options: StageOptions): { stage: Stage | null; stop(): void };
@@ -0,0 +1,187 @@
1
+ /**
2
+ * @module @arraypress/effects-astro/stage
3
+ *
4
+ * The set-up every full-canvas effect shares, so each effect is only its
5
+ * shader and what feeds it: a WebGL context on the element's canvas, the
6
+ * effect's program on a full-screen triangle, the canvas kept the size of
7
+ * the element at a capped pixel ratio, frames from the page's one loop
8
+ * (or one still frame with prefers-reduced-motion or `still`), and everything torn
9
+ * down when the effect stops.
10
+ *
11
+ * Each frame: `update` (free to use the context for other passes), then
12
+ * the effect's program is bound with `uRes` set, `uniforms` sets the
13
+ * rest, the triangle is drawn, and `after` draws anything on top.
14
+ */
15
+
16
+ import { getContext, createProgram, bindTriangle } from './gl.js';
17
+ import { pixelRatio, prefersReducedMotion } from './device.js';
18
+ import { animate } from './loop.js';
19
+
20
+ /** Frames between looks at the frame rate, the smoothed frame time counted slow (about 45 fps), each step down, and the lowest share of the pixel ratio a step may reach. */
21
+ const ADAPT_WINDOW = 60;
22
+ const ADAPT_SLOW = 1 / 45;
23
+ const ADAPT_STEP = 0.8;
24
+ const ADAPT_FLOOR = 0.5;
25
+
26
+ /** The vertex shader for a full-screen pass that works in `gl_FragCoord`. */
27
+ const CLIP = `
28
+ attribute vec2 p;
29
+ void main() { gl_Position = vec4(p, 0.0, 1.0); }
30
+ `;
31
+
32
+ /**
33
+ * @typedef {object} Stage
34
+ * @property {WebGL2RenderingContext | WebGLRenderingContext} gl The context.
35
+ * @property {HTMLCanvasElement} canvas The canvas.
36
+ * @property {WebGLProgram} program The effect's program.
37
+ * @property {(name: string) => WebGLUniformLocation | null} uniform A uniform of the effect's program.
38
+ * @property {boolean} still True with prefers-reduced-motion or the `still` option: one frame, redrawn on changes.
39
+ * @property {number} ratio The pixel ratio the canvas is drawn at.
40
+ * @property {() => void} redraw Draws a frame now (for still stages after a change).
41
+ */
42
+
43
+ /**
44
+ * @typedef {object} StageOptions
45
+ * @property {string} fragment The effect's fragment shader (`uRes`, the canvas size in pixels, is set for it).
46
+ * @property {string} [vertex] A vertex shader of its own (its position attribute at location 0); the default passes the triangle through.
47
+ * @property {boolean} [alpha=true] A transparent canvas (premultiplied), or an opaque one.
48
+ * @property {number} [scale=1] Share of the pixel ratio to draw at.
49
+ * @property {boolean} [adaptive=true] Lower the resolution while frames run slow (a weak GPU), down to half; a step that does not help is taken back and adjusting stops.
50
+ * @property {boolean} [webgl1=true] Accept WebGL 1 where there is no WebGL 2.
51
+ * @property {boolean} [antialias=false] Smooth the edges of geometry drawn into it (multisampling): for effects that draw meshes.
52
+ * @property {boolean} [still=false] Draw one frame (again after each resize and
53
+ * redraw) and never join the loop, as with prefers-reduced-motion: for an
54
+ * effect used as wallpaper.
55
+ * @property {(stage: Stage) => (() => void) | false | void} [setup] Once, after the context is made;
56
+ * returns a clean-up, or false when the effect cannot run here after all (the canvas is removed).
57
+ * @property {(stage: Stage) => void} [resize] After the canvas is resized.
58
+ * @property {(stage: Stage, now: number, dt: number) => void} [update] Each frame, before the effect's program is bound.
59
+ * @property {(stage: Stage, now: number, dt: number) => void} [uniforms] Each frame, with the program bound: set its uniforms.
60
+ * @property {(stage: Stage, now: number, dt: number) => void} [after] Each frame, after the effect's pass: draw on top of it.
61
+ */
62
+
63
+ /**
64
+ * Starts a full-canvas effect on an element (its first canvas is drawn into).
65
+ *
66
+ * @param {HTMLElement} el The element.
67
+ * @param {StageOptions} options The effect.
68
+ * @returns {{ stage: Stage | null, stop: () => void }} The stage (null without
69
+ * WebGL, when the canvas is removed), and a function that stops it.
70
+ */
71
+ export function createStage(el, options) {
72
+ const { fragment, alpha = true, scale = 1, webgl1 = true, adaptive = true, antialias = false } = options;
73
+ const canvas = el.querySelector('canvas');
74
+ const gl = canvas && getContext(canvas, { alpha, premultipliedAlpha: true, antialias }, webgl1);
75
+ const compiled = gl && createProgram(gl, options.vertex ?? CLIP, fragment);
76
+ if (!canvas || !gl || !compiled) {
77
+ canvas?.remove();
78
+ gl?.getExtension('WEBGL_lose_context')?.loseContext();
79
+ return { stage: null, stop: () => {} };
80
+ }
81
+ const triangle = bindTriangle(gl);
82
+ /**
83
+ * How the frame rate is going: the share of the pixel ratio drawn at
84
+ * now, the smoothed frame time, frames since the last look, the frame
85
+ * time before the last step down (0 when there was none), and whether
86
+ * it has stopped adjusting.
87
+ */
88
+ const quality = { share: 1, ema: 0, frames: 0, before: 0, settled: false };
89
+
90
+ /**
91
+ * Lowers the resolution while frames run slow, a step at a time. A step
92
+ * that buys nothing means the slowness is not this canvas's (a 30 Hz
93
+ * screen, a busy page): it is taken back, and adjusting stops.
94
+ *
95
+ * @param {number} dt Seconds since the last frame.
96
+ */
97
+ const adapt = (dt) => {
98
+ quality.ema = quality.ema ? quality.ema * 0.9 + dt * 0.1 : dt;
99
+ if (quality.settled || ++quality.frames < ADAPT_WINDOW) return;
100
+ quality.frames = 0;
101
+ if (quality.before) {
102
+ const helped = quality.ema < quality.before * 0.9;
103
+ quality.before = 0;
104
+ if (!helped) {
105
+ quality.share = Math.min(1, quality.share / ADAPT_STEP);
106
+ quality.settled = true;
107
+ size();
108
+ return;
109
+ }
110
+ }
111
+ if (quality.ema > ADAPT_SLOW && quality.share > ADAPT_FLOOR) {
112
+ quality.before = quality.ema;
113
+ quality.share = Math.max(ADAPT_FLOOR, quality.share * ADAPT_STEP);
114
+ size();
115
+ }
116
+ };
117
+
118
+ /**
119
+ * Draws one frame.
120
+ *
121
+ * @param {number} now The time, ms.
122
+ * @param {number} dt Seconds since the last frame.
123
+ */
124
+ const render = (now, dt) => {
125
+ options.update?.(stage, now, dt);
126
+ gl.bindFramebuffer(gl.FRAMEBUFFER, null);
127
+ gl.viewport(0, 0, canvas.width, canvas.height);
128
+ bindTriangle(gl, triangle);
129
+ gl.useProgram(compiled.program);
130
+ gl.uniform2f(compiled.uniform('uRes'), canvas.width, canvas.height);
131
+ options.uniforms?.(stage, now, dt);
132
+ if (alpha) {
133
+ gl.clearColor(0, 0, 0, 0);
134
+ gl.clear(gl.COLOR_BUFFER_BIT);
135
+ }
136
+ gl.drawArrays(gl.TRIANGLES, 0, 3);
137
+ options.after?.(stage, now, dt);
138
+ if (adaptive && dt > 0) adapt(dt);
139
+ };
140
+
141
+ /** @type {Stage} */
142
+ const stage = {
143
+ gl,
144
+ canvas,
145
+ program: compiled.program,
146
+ uniform: compiled.uniform,
147
+ still: options.still === true || prefersReducedMotion(),
148
+ ratio: pixelRatio(scale),
149
+ redraw: () => render(performance.now(), 0),
150
+ };
151
+
152
+ gl.useProgram(compiled.program);
153
+ const cleanup = options.setup?.(stage);
154
+ if (cleanup === false) {
155
+ // It cannot run here: the canvas goes, and its GPU context with it.
156
+ canvas.remove();
157
+ gl.getExtension('WEBGL_lose_context')?.loseContext();
158
+ return { stage: null, stop: () => {} };
159
+ }
160
+ gl.useProgram(compiled.program);
161
+
162
+ /** Sizes the canvas to the element, at the capped pixel ratio (lowered while frames run slow). */
163
+ const size = () => {
164
+ stage.ratio = pixelRatio(scale) * quality.share;
165
+ canvas.width = Math.max(1, Math.round(el.clientWidth * stage.ratio));
166
+ canvas.height = Math.max(1, Math.round(el.clientHeight * stage.ratio));
167
+ gl.useProgram(compiled.program);
168
+ options.resize?.(stage);
169
+ if (stage.still) stage.redraw();
170
+ };
171
+ const resizer = new ResizeObserver(size);
172
+ resizer.observe(el);
173
+ size();
174
+
175
+ const stopLoop = stage.still ? () => {} : animate(el, render);
176
+ return {
177
+ stage,
178
+ stop: () => {
179
+ stopLoop();
180
+ resizer.disconnect();
181
+ cleanup?.();
182
+ // Gone from the page: give the GPU context back now rather than
183
+ // whenever it is collected, as a browser keeps only so many.
184
+ if (!el.isConnected) gl.getExtension('WEBGL_lose_context')?.loseContext();
185
+ },
186
+ };
187
+ }
@@ -0,0 +1,49 @@
1
+ /** A flock of particles, each held to a home by a spring. */
2
+ export interface Swarm {
3
+ count: number;
4
+ /** Positions, x, y pairs. */
5
+ pos: Float32Array;
6
+ /** Velocities, x, y pairs, units a second. */
7
+ vel: Float32Array;
8
+ /** Homes, x, y pairs. */
9
+ home: Float32Array;
10
+ /** How tightly each is held to its home, 0–1; a few are loose. */
11
+ grip: Float32Array;
12
+ /** Seconds stepped since it was last dispersed. */
13
+ time: number;
14
+ }
15
+
16
+ /** What moves a swarm on a step. */
17
+ export interface SwarmForces {
18
+ /** The springs' stiffness at full grip, a second squared. Default 40. */
19
+ pull?: number;
20
+ /** How hard the curl-noise flow pushes, units a second squared. Default 0.5. */
21
+ flow?: number;
22
+ /** The flow's swirls a unit. Default 6. */
23
+ scale?: number;
24
+ /** How fast a particle loses speed, a share a second. Default 4. */
25
+ damping?: number;
26
+ /** Each particle's chance a second of trading homes with a neighbour. Default 0.05. */
27
+ wander?: number;
28
+ /** Seconds the springs take to come on after `disperse`. Default 2.2. */
29
+ arrive?: number;
30
+ /** A pointer to flee: where, how far it reaches, how hard it pushes. */
31
+ pointer?: { x: number; y: number; radius: number; force: number } | null;
32
+ /** The random source. Default Math.random. */
33
+ random?: () => number;
34
+ }
35
+
36
+ /** Makes a swarm of `count` particles at the origin. */
37
+ export function createSwarm(count: number, random?: () => number): Swarm;
38
+
39
+ /** Throws the swarm into a loose cloud round (x, y), moving every which way, and restarts its clock. */
40
+ export function disperse(swarm: Swarm, x: number, y: number, spread: number, random?: () => number): void;
41
+
42
+ /** Gives the particles their homes (x, y pairs), ordered so particles close in the order have homes close on the shape. */
43
+ export function setHomes(swarm: Swarm, homes: Float32Array): void;
44
+
45
+ /** Puts every particle at its home, at rest, for a still frame. */
46
+ export function settle(swarm: Swarm): void;
47
+
48
+ /** Moves the swarm on by `dt` seconds: springs, flow, the pointer, damping. */
49
+ export function stepSwarm(swarm: Swarm, dt: number, forces?: SwarmForces): void;
@@ -0,0 +1,199 @@
1
+ /**
2
+ * @module @arraypress/effects-astro/swarm
3
+ *
4
+ * A flock of particles, each held to a home point of its own by a spring
5
+ * and pushed along a curl-noise flow, so together they hold a shape but
6
+ * never sit still. Now and then two neighbours trade homes and pass each
7
+ * other, so it keeps flowing inside; a pointer pushes them away and
8
+ * round it, and the springs bring them streaming back.
9
+ *
10
+ * Pure numbers, stepped on the CPU in whatever units the effect likes
11
+ * (heights of the canvas, say): the effect draws it.
12
+ */
13
+
14
+ import { curl } from './curl.js';
15
+
16
+ /**
17
+ * @typedef {object} Swarm
18
+ * @property {number} count How many particles.
19
+ * @property {Float32Array} pos Positions, x, y pairs.
20
+ * @property {Float32Array} vel Velocities, x, y pairs, units a second.
21
+ * @property {Float32Array} home Homes, x, y pairs.
22
+ * @property {Float32Array} grip How tightly each is held to its home, 0–1:
23
+ * most are held close, a few are loose and stray round the shape.
24
+ * @property {number} time Seconds stepped since it was last dispersed.
25
+ */
26
+
27
+ /**
28
+ * @typedef {object} SwarmForces
29
+ * @property {number} [pull=40] The springs' stiffness at full grip, a second squared.
30
+ * @property {number} [flow=0.5] How hard the flow pushes, units a second squared.
31
+ * @property {number} [scale=6] The flow's swirls a unit: larger for smaller swirls.
32
+ * @property {number} [damping=4] How fast a particle loses speed, a share a second (exponential).
33
+ * @property {number} [wander=0.05] Each particle's chance a second of trading homes with a neighbour.
34
+ * @property {number} [arrive=2.2] Seconds the springs take to come on after `disperse`.
35
+ * @property {{ x: number, y: number, radius: number, force: number } | null} [pointer]
36
+ * A pointer to flee: where it is, how far it reaches, how hard it pushes at its centre.
37
+ * @property {() => number} [random=Math.random] The random source.
38
+ */
39
+
40
+ /** Scratch for one flow sample. */
41
+ const flowAt = [0, 0];
42
+ /** How far apart, in the homes' order, two particles trading homes may be: near neighbours, so no one crosses a gap in the shape. */
43
+ const REACH = 24;
44
+ /** The bands homes are ordered in, units tall: thin enough that neighbours in the order are neighbours on the shape. */
45
+ const BAND = 0.04;
46
+
47
+ /**
48
+ * Makes a swarm, every particle at the origin until it is dispersed and
49
+ * given homes.
50
+ *
51
+ * @param {number} count How many particles.
52
+ * @param {() => number} [random=Math.random] The random source.
53
+ * @returns {Swarm} The swarm.
54
+ */
55
+ export function createSwarm(count, random = Math.random) {
56
+ const grip = new Float32Array(count);
57
+ // One in twenty is loose: those stray round the shape and trace its outline in wisps.
58
+ for (let i = 0; i < count; i++) grip[i] = random() < 0.05 ? 0.12 + random() * 0.2 : 0.7 + random() * 0.3;
59
+ return {
60
+ count,
61
+ pos: new Float32Array(count * 2),
62
+ vel: new Float32Array(count * 2),
63
+ home: new Float32Array(count * 2),
64
+ grip,
65
+ time: 0,
66
+ };
67
+ }
68
+
69
+ /**
70
+ * Throws the swarm into a loose cloud, moving every which way, and starts
71
+ * its clock again: from there the springs come on and it gathers.
72
+ *
73
+ * @param {Swarm} swarm The swarm.
74
+ * @param {number} x The cloud's centre, across.
75
+ * @param {number} y The cloud's centre, up.
76
+ * @param {number} spread The cloud's spread (one standard deviation).
77
+ * @param {() => number} [random=Math.random] The random source.
78
+ */
79
+ export function disperse(swarm, x, y, spread, random = Math.random) {
80
+ for (let i = 0; i < swarm.count; i++) {
81
+ // Box–Muller: two normal deviates from two uniform ones.
82
+ const r = Math.sqrt(-2 * Math.log(1 - random()));
83
+ const a = random() * Math.PI * 2;
84
+ swarm.pos[i * 2] = x + Math.cos(a) * r * spread;
85
+ swarm.pos[i * 2 + 1] = y + Math.sin(a) * r * spread;
86
+ swarm.vel[i * 2] = (random() - 0.5) * spread;
87
+ swarm.vel[i * 2 + 1] = (random() - 0.5) * spread;
88
+ }
89
+ swarm.time = 0;
90
+ }
91
+
92
+ /**
93
+ * Gives the particles their homes, ordered along thin bands that run
94
+ * back and forth across the shape (a serpentine), so particles close in
95
+ * the order have homes close on the shape: trades are made between them.
96
+ *
97
+ * @param {Swarm} swarm The swarm.
98
+ * @param {Float32Array} homes x, y pairs, one a particle.
99
+ */
100
+ export function setHomes(swarm, homes) {
101
+ const n = Math.min(swarm.count, homes.length >> 1);
102
+ /**
103
+ * Where a home falls in the serpentine order.
104
+ *
105
+ * @param {number} i The home's index.
106
+ * @returns {number} Its key: lower comes first.
107
+ */
108
+ const key = (i) => {
109
+ const band = Math.floor(homes[i * 2 + 1] / BAND);
110
+ const x = homes[i * 2];
111
+ return band * 1e6 + (band & 1 ? -x : x);
112
+ };
113
+ const keys = Float64Array.from({ length: n }, (_, i) => key(i));
114
+ const order = Array.from({ length: n }, (_, i) => i).sort((a, b) => keys[a] - keys[b]);
115
+ order.forEach((from, to) => {
116
+ swarm.home[to * 2] = homes[from * 2];
117
+ swarm.home[to * 2 + 1] = homes[from * 2 + 1];
118
+ });
119
+ }
120
+
121
+ /**
122
+ * Puts every particle at its home, at rest: the settled swarm, for a still frame.
123
+ *
124
+ * @param {Swarm} swarm The swarm.
125
+ */
126
+ export function settle(swarm) {
127
+ swarm.pos.set(swarm.home);
128
+ swarm.vel.fill(0);
129
+ // Long since arrived: the springs are fully on.
130
+ swarm.time = 60;
131
+ }
132
+
133
+ /**
134
+ * Moves the swarm on by a step: springs, flow, the pointer and damping,
135
+ * integrated semi-implicitly (velocity first), which stays stable at the
136
+ * stiffness used here for any step up to a twentieth of a second.
137
+ *
138
+ * @param {Swarm} swarm The swarm.
139
+ * @param {number} dt The step, seconds.
140
+ * @param {SwarmForces} [forces] The forces.
141
+ */
142
+ export function stepSwarm(swarm, dt, forces = {}) {
143
+ if (dt <= 0) return;
144
+ const { pull = 40, flow = 0.5, scale = 6, damping = 4, wander = 0.05, arrive = 2.2, pointer = null, random = Math.random } = forces;
145
+ const { count, pos, vel, home, grip } = swarm;
146
+ swarm.time += dt;
147
+ const t = swarm.time;
148
+ // The springs come on over `arrive`; till then the flow swirls them harder.
149
+ const k = Math.min(1, t / arrive);
150
+ const on = k * k * (3 - 2 * k);
151
+ const spring = pull * (0.08 + 0.92 * on);
152
+ // The swarm breathes: the flow swells and eases, loosening the shape and drawing it in.
153
+ const push = flow * (1 + 2 * (1 - on)) * (0.85 + 0.35 * Math.sin(t * 0.8));
154
+ const keep = Math.exp(-damping * dt);
155
+ const reach2 = pointer ? pointer.radius * pointer.radius : 0;
156
+
157
+ // Trading homes: a few pairs of neighbours a step, so the inside of the shape keeps flowing.
158
+ const trades = Math.min(count >> 1, Math.floor(count * wander * dt + random()));
159
+ for (let n = 0; n < trades; n++) {
160
+ const a = Math.floor(random() * count);
161
+ const i = a * 2;
162
+ const j = Math.min(count - 1, Math.max(0, a + Math.round((random() * 2 - 1) * REACH))) * 2;
163
+ const hx = home[i];
164
+ const hy = home[i + 1];
165
+ home[i] = home[j];
166
+ home[i + 1] = home[j + 1];
167
+ home[j] = hx;
168
+ home[j + 1] = hy;
169
+ }
170
+
171
+ for (let i = 0; i < count; i++) {
172
+ const x = i * 2;
173
+ const y = x + 1;
174
+ const px = pos[x];
175
+ const py = pos[y];
176
+ const g = spring * grip[i];
177
+ curl(px * scale, py * scale, t * 0.35, flowAt);
178
+ let ax = g * (home[x] - px) + flowAt[0] * push;
179
+ let ay = g * (home[y] - py) + flowAt[1] * push;
180
+ if (pointer) {
181
+ const dx = px - pointer.x;
182
+ const dy = py - pointer.y;
183
+ const d2 = dx * dx + dy * dy;
184
+ if (d2 < reach2 && d2 > 1e-10) {
185
+ const d = Math.sqrt(d2);
186
+ const f = 1 - d / pointer.radius;
187
+ const s = (pointer.force * f * f) / d;
188
+ // Away, and round: half one way, half the other, so it parts like water.
189
+ const turn = i & 1 ? 0.7 : -0.7;
190
+ ax += (dx - dy * turn) * s;
191
+ ay += (dy + dx * turn) * s;
192
+ }
193
+ }
194
+ vel[x] = vel[x] * keep + ax * dt;
195
+ vel[y] = vel[y] * keep + ay * dt;
196
+ pos[x] = px + vel[x] * dt;
197
+ pos[y] = py + vel[y] * dt;
198
+ }
199
+ }
@@ -0,0 +1,41 @@
1
+ export interface ScrambleConfig {
2
+ /** What it scrambles through. Default letters, digits and a few symbols. */
3
+ characters?: string;
4
+ /** Seconds to resolve. Default 0.9. */
5
+ duration?: number;
6
+ /** Scramble again when the pointer comes over it. Default false. */
7
+ hover?: boolean;
8
+ }
9
+
10
+ export interface TypewriterConfig {
11
+ /** What it types, in turn. */
12
+ phrases: string[];
13
+ /** Milliseconds a character when typing. Default 55. */
14
+ speed?: number;
15
+ /** Milliseconds a finished phrase stays. Default 1600. */
16
+ pause?: number;
17
+ /** Start again after the last. Default true. */
18
+ loop?: boolean;
19
+ }
20
+
21
+ export interface RevealConfig {
22
+ /** How it arrives. Default `up`. */
23
+ from?: 'up' | 'down' | 'left' | 'right' | 'fade' | 'zoom' | 'blur';
24
+ /** Each word arrives on its own, one after another. Default false. */
25
+ words?: boolean;
26
+ /** Seconds between words. Default 0.06. */
27
+ stagger?: number;
28
+ /** Seconds before it starts. Default 0. */
29
+ delay?: number;
30
+ /** Only the first time it comes into view. Default true. */
31
+ once?: boolean;
32
+ }
33
+
34
+ /** Resolves an element's text out of random characters when it comes into view. Returns a function that stops it. */
35
+ export function scramble(el: HTMLElement, config: ScrambleConfig): () => void;
36
+
37
+ /** Types phrases one after another into the element's `[data-typed]` child. Returns a function that stops it. */
38
+ export function typewriter(el: HTMLElement, config: TypewriterConfig): () => void;
39
+
40
+ /** Brings an element into place (`.is-revealed`) as it scrolls into view. Returns a function that stops it. */
41
+ export function reveal(el: HTMLElement, config: RevealConfig): () => void;
@@ -0,0 +1,165 @@
1
+ /**
2
+ * @module @arraypress/effects-astro/text-motion
3
+ *
4
+ * Text that arrives:
5
+ *
6
+ * - `scramble`: the text resolves out of random characters, left to
7
+ * right, when it scrolls into view (and again on hover, if asked).
8
+ * - `typewriter`: types a list of phrases one after another, deleting
9
+ * each before the next, behind a blinking caret.
10
+ * - `reveal`: the element, or its words, rise and fade into place as
11
+ * they scroll into view, one after another.
12
+ *
13
+ * The full text is always in the page for assistive technology and
14
+ * search; the motion is drawn over it. With prefers-reduced-motion the
15
+ * text is simply there.
16
+ */
17
+
18
+ import { prefersReducedMotion } from './device.js';
19
+ import { inView } from './in-view.js';
20
+
21
+ /**
22
+ * @typedef {object} ScrambleConfig
23
+ * @property {string} [characters] What it scrambles through (default letters, digits and a few symbols).
24
+ * @property {number} [duration=0.9] Seconds to resolve.
25
+ * @property {boolean} [hover=false] Scramble again when the pointer comes over it.
26
+ */
27
+
28
+ /**
29
+ * @typedef {object} TypewriterConfig
30
+ * @property {string[]} phrases What it types, in turn.
31
+ * @property {number} [speed=55] Milliseconds a character when typing.
32
+ * @property {number} [pause=1600] Milliseconds a finished phrase stays.
33
+ * @property {boolean} [loop=true] Start again after the last.
34
+ */
35
+
36
+ /**
37
+ * @typedef {object} RevealConfig
38
+ * @property {'up' | 'down' | 'left' | 'right' | 'fade' | 'zoom' | 'blur'} [from='up'] How it arrives.
39
+ * @property {boolean} [words=false] Each word arrives on its own, one after another.
40
+ * @property {number} [stagger=0.06] Seconds between words.
41
+ * @property {number} [delay=0] Seconds before it starts.
42
+ * @property {boolean} [once=true] Only the first time it comes into view.
43
+ */
44
+
45
+ const GLYPHS = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789#%&*+=/<>';
46
+
47
+ /**
48
+ * Resolves an element's text out of random characters.
49
+ *
50
+ * @param {HTMLElement} el The text (its `data-text`, or its text, is the final text).
51
+ * @param {ScrambleConfig} config The settings.
52
+ * @returns {() => void} Stops it.
53
+ */
54
+ export function scramble(el, config) {
55
+ const final = el.dataset.text ?? el.textContent ?? '';
56
+ if (prefersReducedMotion()) return () => {};
57
+ const glyphs = config.characters ?? GLYPHS;
58
+ const duration = (config.duration ?? 0.9) * 1000;
59
+ const shown = /** @type {HTMLElement} */ (el.querySelector('[data-scramble-shown]'));
60
+ if (!shown) return () => {};
61
+ let raf = 0;
62
+
63
+ /** Runs the scramble once. */
64
+ const play = () => {
65
+ cancelAnimationFrame(raf);
66
+ const start = performance.now();
67
+ /**
68
+ * One frame: each character still scrambling gets a random glyph.
69
+ *
70
+ * @param {number} now The frame's time.
71
+ */
72
+ const tick = (now) => {
73
+ const done = Math.min(1, (now - start) / duration);
74
+ // Each character settles in its turn, left to right.
75
+ let out = '';
76
+ for (let i = 0; i < final.length; i++) {
77
+ const ch = final[i];
78
+ const settles = i / final.length;
79
+ out += ch === ' ' || done >= settles + 0.15 ? ch : glyphs[(Math.random() * glyphs.length) | 0];
80
+ }
81
+ shown.textContent = out;
82
+ if (done < 1) raf = requestAnimationFrame(tick);
83
+ else shown.textContent = final;
84
+ };
85
+ raf = requestAnimationFrame(tick);
86
+ };
87
+
88
+ const unwatch = inView(el, play);
89
+ if (config.hover) el.addEventListener('pointerenter', play);
90
+ return () => {
91
+ unwatch();
92
+ cancelAnimationFrame(raf);
93
+ el.removeEventListener('pointerenter', play);
94
+ shown.textContent = final;
95
+ };
96
+ }
97
+
98
+ /**
99
+ * Types phrases one after another.
100
+ *
101
+ * @param {HTMLElement} el The element (its `[data-typed]` child gets the text).
102
+ * @param {TypewriterConfig} config The settings.
103
+ * @returns {() => void} Stops it.
104
+ */
105
+ export function typewriter(el, config) {
106
+ const typed = el.querySelector('[data-typed]');
107
+ const phrases = config.phrases ?? [];
108
+ if (!(typed instanceof HTMLElement) || !phrases.length) return () => {};
109
+ if (prefersReducedMotion()) {
110
+ typed.textContent = phrases[0];
111
+ return () => {};
112
+ }
113
+ const speed = config.speed ?? 55;
114
+ const pause = config.pause ?? 1600;
115
+ let phrase = 0;
116
+ let length = 0;
117
+ let deleting = false;
118
+ let timer = 0;
119
+
120
+ /** One keystroke, and the next booked. */
121
+ const step = () => {
122
+ const text = phrases[phrase];
123
+ length += deleting ? -1 : 1;
124
+ typed.textContent = text.slice(0, length);
125
+ let wait = deleting ? speed / 2 : speed;
126
+ if (!deleting && length >= text.length) {
127
+ if (phrase === phrases.length - 1 && config.loop === false) return;
128
+ deleting = true;
129
+ wait = pause;
130
+ } else if (deleting && length <= 0) {
131
+ deleting = false;
132
+ phrase = (phrase + 1) % phrases.length;
133
+ wait = speed * 6;
134
+ }
135
+ timer = window.setTimeout(step, wait);
136
+ };
137
+ const unwatch = inView(el, () => {
138
+ timer = window.setTimeout(step, speed);
139
+ });
140
+ return () => {
141
+ unwatch();
142
+ clearTimeout(timer);
143
+ };
144
+ }
145
+
146
+ /**
147
+ * Brings an element (or its words) into place as it scrolls into view.
148
+ *
149
+ * @param {HTMLElement} el The element.
150
+ * @param {RevealConfig} config The settings.
151
+ * @returns {() => void} Stops it.
152
+ */
153
+ export function reveal(el, config) {
154
+ if (prefersReducedMotion()) {
155
+ el.classList.add('is-revealed');
156
+ return () => {};
157
+ }
158
+ el.classList.add('is-waiting');
159
+ return inView(
160
+ el,
161
+ () => el.classList.add('is-revealed'),
162
+ config.once !== false,
163
+ () => el.classList.remove('is-revealed'),
164
+ );
165
+ }