@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,76 @@
1
+ /**
2
+ * @module @arraypress/effects-astro/trackers
3
+ *
4
+ * A machine-vision overlay: boxes with corner brackets and a small label,
5
+ * laid over an effect like a detector's debug view. The effect decides
6
+ * where each box goes every frame (it knows where its subject is); this
7
+ * only keeps the elements, eases them there and fades them in and out.
8
+ *
9
+ * The boxes are elements inside the effect's element, styled by the
10
+ * `.effect-tracker` rules in EffectCanvas.astro. They are decoration:
11
+ * hidden from assistive technology with the rest of the effect.
12
+ */
13
+
14
+ /**
15
+ * @typedef {object} TrackerBox
16
+ * @property {number} x Left, CSS px from the element's left.
17
+ * @property {number} y Top, CSS px from the element's top.
18
+ * @property {number} w Width, CSS px.
19
+ * @property {number} h Height, CSS px.
20
+ */
21
+
22
+ /**
23
+ * Makes a set of tracker boxes in an element.
24
+ *
25
+ * @param {HTMLElement} el The effect's element.
26
+ * @param {number} count How many boxes.
27
+ * @param {object} [options]
28
+ * @param {'box' | 'marker'} [options.kind='box'] Bracketed boxes, or small crosshair markers.
29
+ * @returns {{ place: (i: number, box: TrackerBox | null, label?: string) => void, destroy: () => void }}
30
+ * `place` moves box `i` towards `box` (null fades it out) and sets its
31
+ * label; `destroy` removes them.
32
+ */
33
+ export function createTrackers(el, count, { kind = 'box' } = {}) {
34
+ const layer = document.createElement('div');
35
+ layer.className = 'effect-trackers';
36
+ el.append(layer);
37
+ const boxes = Array.from({ length: count }, () => {
38
+ const node = document.createElement('div');
39
+ node.className = `effect-tracker is-${kind}`;
40
+ const label = document.createElement('span');
41
+ node.append(label);
42
+ layer.append(node);
43
+ return { node, label, at: /** @type {TrackerBox | null} */ (null), text: '' };
44
+ });
45
+
46
+ return {
47
+ place(i, box, text) {
48
+ const b = boxes[i];
49
+ if (!b) return;
50
+ if (!box) {
51
+ b.node.style.opacity = '0';
52
+ b.at = null;
53
+ return;
54
+ }
55
+ // Ease towards the target, as a tracker locks on; jump there when it first appears.
56
+ const at = b.at ?? box;
57
+ b.at = {
58
+ x: at.x + (box.x - at.x) * 0.25,
59
+ y: at.y + (box.y - at.y) * 0.25,
60
+ w: at.w + (box.w - at.w) * 0.25,
61
+ h: at.h + (box.h - at.h) * 0.25,
62
+ };
63
+ b.node.style.opacity = '1';
64
+ b.node.style.transform = `translate(${b.at.x.toFixed(1)}px, ${b.at.y.toFixed(1)}px)`;
65
+ b.node.style.width = `${b.at.w.toFixed(1)}px`;
66
+ b.node.style.height = `${b.at.h.toFixed(1)}px`;
67
+ if (text !== undefined && text !== b.text) {
68
+ b.text = text;
69
+ b.label.textContent = text;
70
+ }
71
+ },
72
+ destroy() {
73
+ layer.remove();
74
+ },
75
+ };
76
+ }
@@ -0,0 +1,43 @@
1
+ /** Each axis by its tag (`wght`, `wdth`, `slnt`, `opsz`…): its value at rest, then at the peak of a swell. */
2
+ export type Axes = Record<string, [number, number]>;
3
+
4
+ export interface VariableTypeConfig {
5
+ /** The axes that swell, `{ tag: [rest, peak] }`. Axes the font lacks do nothing. Default `{ wght: [100, 900], wdth: [75, 125] }`. */
6
+ axes?: Axes;
7
+ /** How far a swell reaches from the pointer, in ems (0.3–4). Default 1.1. */
8
+ radius?: number;
9
+ /** How it drops off: 0 a broad hill, 1 a sharp peak (0–1). Default 0.5. */
10
+ falloff?: number;
11
+ /** How far the travelling swell rises with nothing over the text: 0 none, 1 to the peak (0–1). Default 0.35. */
12
+ idle?: number;
13
+ /** Size the line so that at its widest swell it spans its parent's width. Default false. */
14
+ fit?: boolean;
15
+ }
16
+
17
+ /** One axis: its tag, its value at rest and at the peak. */
18
+ export interface Axis {
19
+ tag: string;
20
+ rest: number;
21
+ peak: number;
22
+ }
23
+
24
+ /** The axes when none are given: weight 100 to 900, width 75 to 125. */
25
+ export const DEFAULT_AXES: Axes;
26
+
27
+ /** The axes as a list, keeping only four-letter tags with two numbers. */
28
+ export function axisList(axes?: Axes): Axis[];
29
+
30
+ /** A `font-variation-settings` value, `amount` of the way from rest (0) to peak (1); `normal` with no axes. */
31
+ export function variation(axes: Axis[], amount: number): string;
32
+
33
+ /** How much a letter swells `distance` ems from the pointer: 1 under it, easing to 0 at `radius`; `falloff` 0 broad, 1 sharp. */
34
+ export function swell(distance: number, radius: number, falloff: number): number;
35
+
36
+ /** The idle swell, 0 to 1, at `x` ems along the line, `time` seconds in: soft crests travelling left to right. */
37
+ export function idleWave(x: number, time: number): number;
38
+
39
+ /** How much wider a line gets at its widest swell: `xs` each letter's centre in ems, `growth` each letter's peak-minus-rest width, `shape` the swell at an offset from the crest. */
40
+ export function widestSwell(xs: ArrayLike<number>, growth: ArrayLike<number>, shape: (offset: number) => number): number;
41
+
42
+ /** Starts the swell on `el` (styled by VariableType.astro), set in a variable font. Returns a function that stops it and puts the text back. */
43
+ export function variableType(el: HTMLElement, config: VariableTypeConfig): () => void;
@@ -0,0 +1,358 @@
1
+ /**
2
+ * @module @arraypress/effects-astro/variable-type
3
+ *
4
+ * A variable-font headline whose letters swell under the pointer: each
5
+ * letter's axes (weight and width by default, any others the font has)
6
+ * run from rest to peak by its distance from the pointer, on a spring, so
7
+ * a pass over the word rolls through it like a wave and settles back when
8
+ * the pointer leaves. With nothing over it, a gentle swell travels along
9
+ * the line on its own.
10
+ *
11
+ * A swell widens the line. A fitted line (`fit`) is sized so that at its
12
+ * widest swell it spans its parent, with room either side at rest.
13
+ *
14
+ * The text stays real DOM text, split into one span a letter
15
+ * (`splitLetters`): selectable, found by search, read as words. Letter
16
+ * positions (and a fitted line's size) are read once, at rest, when the
17
+ * text changes size or its fonts load; each frame only writes, a
18
+ * `font-variation-settings` and a `--swell` (0 rest, 1 peak, for CSS) to
19
+ * each letter that moved, in steps, so the browser reuses the font
20
+ * instances it builds. Touch follows the finger; with
21
+ * prefers-reduced-motion the text stays at rest, a fitted line at the size
22
+ * it has in motion.
23
+ */
24
+
25
+ import { prefersReducedMotion } from './device.js';
26
+ import { fit, fitText } from './fit-text.js';
27
+ import { animate } from './loop.js';
28
+ import { pointerIn } from './pointer.js';
29
+ import { joinLetters, splitLetters } from './text.js';
30
+
31
+ /**
32
+ * @typedef {Record<string, [number, number]>} Axes Each axis by its tag
33
+ * (`wght`, `wdth`, `slnt`, `opsz`…): its value at rest, then at the peak of a swell.
34
+ */
35
+
36
+ /**
37
+ * @typedef {object} VariableTypeConfig
38
+ * @property {Axes} [axes] The axes that swell, `{ tag: [rest, peak] }` (default `{ wght: [100, 900], wdth: [75, 125] }`). Axes the font lacks do nothing.
39
+ * @property {number} [radius=1.1] How far a swell reaches from the pointer, in ems.
40
+ * @property {number} [falloff=0.5] How it drops off: 0 a broad hill, 1 a sharp peak.
41
+ * @property {number} [idle=0.35] How far the travelling swell rises with nothing over the text: 0 none, 1 to the peak.
42
+ * @property {boolean} [fit=false] Size the line so that at its widest swell it spans its parent's width.
43
+ */
44
+
45
+ /** @typedef {{ tag: string, rest: number, peak: number }} Axis */
46
+
47
+ /** The axes when none are given. */
48
+ export const DEFAULT_AXES = /** @type {Axes} */ ({ wght: [100, 900], wdth: [75, 125] });
49
+
50
+ /** The spring's stiffness, per second squared. */
51
+ const STIFFNESS = 150;
52
+ /** The spring's damping: a touch under critical, so a swell overshoots a hair and settles. */
53
+ const DAMPING = 2 * 0.72 * Math.sqrt(STIFFNESS);
54
+ /** How long the pointer must be gone before the idle swell starts, ms. */
55
+ const IDLE_AFTER = 1400;
56
+ /** The idle swell's wavelength, in ems. */
57
+ const WAVELENGTH = 5.5;
58
+ /** How fast the idle swell travels, ems a second. */
59
+ const TRAVEL = 1.6;
60
+ /** Vertical distance counts for this much of horizontal: a tall letter swells along its whole height. */
61
+ const SQUASH = 0.5;
62
+ /** Room left for a spring overshooting its swell, a share of the widest. */
63
+ const OVERSHOOT = 1.08;
64
+ /** Crest positions tried per letter when finding the widest swell. */
65
+ const SAMPLES = 8;
66
+ /**
67
+ * Swells are written in steps this fine: every distinct setting is a font
68
+ * instance the browser builds and caches, so a few dozen steps stay cheap
69
+ * where a continuous value would build a new one every frame.
70
+ */
71
+ const STEPS = 64;
72
+
73
+ /**
74
+ * The axes as a list, keeping only four-letter tags with two numbers.
75
+ *
76
+ * @param {Axes | undefined} axes The axes, `{ tag: [rest, peak] }`.
77
+ * @returns {Axis[]} Each axis, in order.
78
+ */
79
+ export function axisList(axes = DEFAULT_AXES) {
80
+ return Object.entries(axes ?? DEFAULT_AXES)
81
+ .filter(([tag, ends]) => /^[a-zA-Z0-9]{4}$/.test(tag) && Array.isArray(ends) && ends.length === 2 && ends.every(Number.isFinite))
82
+ .map(([tag, [rest, peak]]) => ({ tag, rest, peak }));
83
+ }
84
+
85
+ /**
86
+ * A `font-variation-settings` value, part of the way from rest to peak.
87
+ *
88
+ * @param {Axis[]} axes The axes.
89
+ * @param {number} amount 0 at rest, 1 at the peak (a spring may overshoot either).
90
+ * @returns {string} The settings, e.g. `"wght" 500, "wdth" 100`; `normal` with no axes.
91
+ */
92
+ export function variation(axes, amount) {
93
+ if (!axes.length) return 'normal';
94
+ return axes.map(({ tag, rest, peak }) => `"${tag}" ${Math.round((rest + (peak - rest) * amount) * 10) / 10}`).join(', ');
95
+ }
96
+
97
+ /**
98
+ * How much a letter swells at a distance from the pointer.
99
+ *
100
+ * @param {number} distance The distance, in ems.
101
+ * @param {number} radius The swell's reach, in ems.
102
+ * @param {number} falloff 0 a broad hill, 1 a sharp peak.
103
+ * @returns {number} 1 under the pointer, easing to 0 at `radius` and beyond.
104
+ */
105
+ export function swell(distance, radius, falloff) {
106
+ if (!(radius > 0) || distance >= radius) return 0;
107
+ const t = 1 - Math.max(0, distance) / radius;
108
+ const smooth = t * t * (3 - 2 * t);
109
+ return smooth ** (0.6 + Math.min(1, Math.max(0, falloff)) * 2.4);
110
+ }
111
+
112
+ /**
113
+ * The idle swell at a point along the line: soft crests, a wavelength
114
+ * apart, travelling left to right.
115
+ *
116
+ * @param {number} x The point, in ems from the text's left.
117
+ * @param {number} time Seconds.
118
+ * @returns {number} 0 to 1.
119
+ */
120
+ export function idleWave(x, time) {
121
+ const phase = ((x - time * TRAVEL) / WAVELENGTH) * Math.PI * 2;
122
+ return (0.5 + 0.5 * Math.cos(phase)) ** 4;
123
+ }
124
+
125
+ /**
126
+ * How much wider a line gets at its widest swell: the crest is tried all
127
+ * along it, and each letter grows by its share of the swell there.
128
+ *
129
+ * @param {ArrayLike<number>} xs Each letter's centre, in ems along the line.
130
+ * @param {ArrayLike<number>} growth How much wider each letter is at its peak than at rest.
131
+ * @param {(offset: number) => number} shape The swell, 0 to 1, a letter `offset` ems from the crest.
132
+ * @returns {number} The most the line grows, in `growth`'s units.
133
+ */
134
+ export function widestSwell(xs, growth, shape) {
135
+ const n = xs.length;
136
+ if (!n) return 0;
137
+ const from = xs[0];
138
+ const span = xs[n - 1] - from;
139
+ const tries = Math.max(1, (n - 1) * SAMPLES);
140
+ let widest = 0;
141
+ for (let t = 0; t <= tries; t++) {
142
+ const crest = from + (span * t) / tries;
143
+ let total = 0;
144
+ for (let i = 0; i < n; i++) total += growth[i] * shape(xs[i] - crest);
145
+ widest = Math.max(widest, total);
146
+ }
147
+ return widest;
148
+ }
149
+
150
+ /**
151
+ * Starts the swell on an element.
152
+ *
153
+ * @param {HTMLElement} el The text, set in a variable font.
154
+ * @param {VariableTypeConfig} config The settings.
155
+ * @returns {() => void} Stops it and puts the text back as it was.
156
+ */
157
+ export function variableType(el, config) {
158
+ const axes = axisList(config.axes);
159
+ el.style.fontVariationSettings = variation(axes, 0);
160
+ const fitting = config.fit === true;
161
+ if (!axes.length) return fitting ? fitText(el) : () => {};
162
+ // Still, a fitted line keeps the size it has in motion; anything else needs nothing.
163
+ const still = prefersReducedMotion();
164
+ if (still && !fitting) return () => {};
165
+ const radius = config.radius ?? 1.1;
166
+ const falloff = config.falloff ?? 0.5;
167
+ const idle = Math.min(1, Math.max(0, config.idle ?? 0.35));
168
+ const letters = splitLetters(el);
169
+ const count = letters.length;
170
+ const value = new Float32Array(count);
171
+ const velocity = new Float32Array(count);
172
+ const written = new Float32Array(count).fill(-1);
173
+ /** Each letter's centre at rest, in ems from the element's top left. */
174
+ let centres = new Float32Array(count * 2);
175
+ let em = 16;
176
+ let width = 1;
177
+ let height = 1;
178
+ let dirty = true;
179
+ let raf = 0;
180
+ let resting = false;
181
+ let gain = 0;
182
+ let lastSeen = -Infinity;
183
+ /** @type {{ x: number, y: number } | null} The finger, in ems from the element's top left. */
184
+ let touch = null;
185
+
186
+ /**
187
+ * Sizes a fitted line: fitted to its parent at rest, then made smaller
188
+ * by as much as its widest swell (the pointer's or the idle one's) would
189
+ * add, so that at its widest it spans the parent. Read with every letter
190
+ * at rest and then at its peak.
191
+ */
192
+ const fitWithRoom = () => {
193
+ fit(el);
194
+ const size = parseFloat(getComputedStyle(el).fontSize) || 16;
195
+ const rest = letters.map((letter) => letter.getBoundingClientRect());
196
+ const peak = variation(axes, 1);
197
+ for (const letter of letters) letter.style.fontVariationSettings = peak;
198
+ const growth = letters.map((letter, i) => letter.getBoundingClientRect().width - rest[i].width);
199
+ for (const letter of letters) letter.style.removeProperty('font-variation-settings');
200
+ const xs = rest.map((r) => (r.left + r.width / 2) / size);
201
+ const line = rest[count - 1].right - rest[0].left;
202
+ const pointer = widestSwell(xs, growth, (offset) => swell(Math.abs(offset), radius, falloff));
203
+ const drift = widestSwell(xs, growth, (offset) => idle * idleWave(offset, 0));
204
+ const widest = Math.max(pointer, drift) * OVERSHOOT;
205
+ if (line > 0 && widest > 0) el.style.fontSize = `${Math.floor(((size * line) / (line + widest)) * 100) / 100}px`;
206
+ };
207
+
208
+ /** Reads every letter's centre with the text at rest, sizing a fitted line first. */
209
+ const measure = () => {
210
+ for (const letter of letters) letter.style.removeProperty('font-variation-settings');
211
+ if (fitting && count) fitWithRoom();
212
+ const box = el.getBoundingClientRect();
213
+ em = parseFloat(getComputedStyle(el).fontSize) || 16;
214
+ width = box.width || 1;
215
+ height = box.height || 1;
216
+ centres = new Float32Array(count * 2);
217
+ letters.forEach((letter, i) => {
218
+ const r = letter.getBoundingClientRect();
219
+ centres[i * 2] = (r.left + r.width / 2 - box.left) / em;
220
+ centres[i * 2 + 1] = (r.top + r.height / 2 - box.top) / em;
221
+ });
222
+ written.fill(-1);
223
+ resting = false;
224
+ };
225
+
226
+ /**
227
+ * One frame: each letter's spring heads for its swell, and the letters
228
+ * that moved are written.
229
+ *
230
+ * @param {number} now The frame's time, ms.
231
+ * @param {number} dt Seconds since the last frame.
232
+ */
233
+ const frame = (now, dt) => {
234
+ if (dirty) {
235
+ dirty = false;
236
+ measure();
237
+ }
238
+ let px = 0;
239
+ let py = 0;
240
+ let present = false;
241
+ if (touch) {
242
+ ({ x: px, y: py } = touch);
243
+ present = true;
244
+ } else {
245
+ const p = pointerIn(el);
246
+ if (p.inside) {
247
+ px = (p.x * width) / em;
248
+ py = ((1 - p.y) * height) / em;
249
+ present = true;
250
+ }
251
+ }
252
+ if (present) lastSeen = now;
253
+ const wanted = idle > 0 && now - lastSeen > IDLE_AFTER ? 1 : 0;
254
+ gain += (wanted - gain) * Math.min(1, dt * 0.9);
255
+ if (Math.abs(wanted - gain) < 0.001) gain = wanted;
256
+ if (resting && !present && gain === 0) return;
257
+ const time = now / 1000;
258
+ let moving = gain !== wanted;
259
+ for (let i = 0; i < count; i++) {
260
+ const cx = centres[i * 2];
261
+ const cy = centres[i * 2 + 1];
262
+ let goal = present ? swell(Math.hypot(px - cx, (py - cy) * SQUASH), radius, falloff) : 0;
263
+ if (gain > 0) goal = Math.max(goal, gain * idle * idleWave(cx, time));
264
+ velocity[i] += (STIFFNESS * (goal - value[i]) - DAMPING * velocity[i]) * dt;
265
+ value[i] += velocity[i] * dt;
266
+ if (Math.abs(velocity[i]) < 0.002 && Math.abs(goal - value[i]) < 0.002) {
267
+ value[i] = goal;
268
+ velocity[i] = 0;
269
+ } else {
270
+ moving = true;
271
+ }
272
+ const step = Math.round(value[i] * STEPS) / STEPS;
273
+ if (step === written[i]) continue;
274
+ written[i] = step;
275
+ const letter = letters[i];
276
+ letter.style.fontVariationSettings = variation(axes, step);
277
+ letter.style.setProperty('--swell', step.toFixed(3));
278
+ }
279
+ resting = !moving && gain === 0 && !present;
280
+ };
281
+
282
+ /**
283
+ * Follows a finger on the text (a mouse is read from the page's pointer).
284
+ *
285
+ * @param {PointerEvent} e The press or move.
286
+ */
287
+ const finger = (e) => {
288
+ if (e.pointerType !== 'touch') return;
289
+ const box = el.getBoundingClientRect();
290
+ touch = { x: (e.clientX - box.left) / em, y: (e.clientY - box.top) / em };
291
+ resting = false;
292
+ };
293
+ /** The finger has lifted, or the page took it to scroll. */
294
+ const lift = () => {
295
+ touch = null;
296
+ };
297
+
298
+ /**
299
+ * Marks the letters for reading again: on the next frame in motion, or
300
+ * on its own frame when still.
301
+ */
302
+ const invalidate = () => {
303
+ dirty = true;
304
+ if (!still || raf) return;
305
+ raf = requestAnimationFrame(() => {
306
+ raf = 0;
307
+ dirty = false;
308
+ measure();
309
+ });
310
+ };
311
+
312
+ // Re-read the letters when the line's width changes or the text's
313
+ // height does (a refit, a font arriving), never on a swell itself.
314
+ let lineWidth = -1;
315
+ let textHeight = -1;
316
+ const observer = new ResizeObserver((entries) => {
317
+ for (const entry of entries) {
318
+ const own = entry.target === el;
319
+ const size = Math.round(own ? entry.contentRect.height : entry.contentRect.width);
320
+ if (size === (own ? textHeight : lineWidth)) continue;
321
+ if (own) textHeight = size;
322
+ else lineWidth = size;
323
+ invalidate();
324
+ }
325
+ });
326
+ observer.observe(el);
327
+ if (el.parentElement) observer.observe(el.parentElement);
328
+ // A font is only fetched once text needs it, so `ready` can resolve before
329
+ // a hidden headline's has even started; `loadingdone` catches it arriving.
330
+ document.fonts?.ready.then(invalidate).catch(() => {});
331
+ document.fonts?.addEventListener('loadingdone', invalidate);
332
+ invalidate();
333
+ if (still) {
334
+ return () => {
335
+ cancelAnimationFrame(raf);
336
+ observer.disconnect();
337
+ document.fonts?.removeEventListener('loadingdone', invalidate);
338
+ joinLetters(el);
339
+ };
340
+ }
341
+
342
+ el.addEventListener('pointerdown', finger);
343
+ el.addEventListener('pointermove', finger, { passive: true });
344
+ el.addEventListener('pointerup', lift);
345
+ el.addEventListener('pointercancel', lift);
346
+ const stop = animate(el, frame);
347
+
348
+ return () => {
349
+ stop();
350
+ observer.disconnect();
351
+ document.fonts?.removeEventListener('loadingdone', invalidate);
352
+ el.removeEventListener('pointerdown', finger);
353
+ el.removeEventListener('pointermove', finger);
354
+ el.removeEventListener('pointerup', lift);
355
+ el.removeEventListener('pointercancel', lift);
356
+ joinLetters(el);
357
+ };
358
+ }
@@ -0,0 +1,31 @@
1
+ import type { PaletteName } from './palettes.js';
2
+
3
+ export interface VoxelLogoConfig {
4
+ /** A character or word to build in cubes. */
5
+ text?: string;
6
+ /** Its CSS font, without the size. Default: the page's, bold. */
7
+ font?: string;
8
+ /** An image (an SVG logo) to build instead. */
9
+ src?: string;
10
+ /** Where the solid sits, each 0–1 from the top left. Default the right half. */
11
+ area?: { x: number; y: number; width: number; height: number };
12
+ /** The ink, then the paper. Default mint on near-black. */
13
+ colors?: string[];
14
+ /** A shared palette by name (`PALETTES`, any case), when there are no `colors`. */
15
+ palette?: PaletteName | (string & {});
16
+ /** How large the solid is in its area (1 just fits when face-on). Default 1.3. */
17
+ size?: number;
18
+ /** Cubes across the shape. Default 28. */
19
+ cells?: number;
20
+ /** Cubes deep. Default 4. */
21
+ depth?: number;
22
+ /** The dither's pixel size, CSS px; 0 for smooth shading. Default 2. */
23
+ dither?: number;
24
+ /** Tracking markers ringing the solid (try 6); 0 for none. Default 0. */
25
+ markers?: number;
26
+ /** How far it rocks and turns towards the pointer. Default 1. */
27
+ rock?: number;
28
+ }
29
+
30
+ /** Starts a voxel logo in `el`'s canvas. Returns a function that stops it. */
31
+ export function voxelLogo(el: HTMLElement, config: VoxelLogoConfig): () => void;