@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,37 @@
1
+ import type { PaletteName } from './palettes.js';
2
+
3
+ export interface IsoCityConfig {
4
+ /** Buildings, parks, roads. Default warm off-white, sage and graphite (at night slate, deep green and near-black). */
5
+ colors?: string[];
6
+ /** A shared palette by name (`PALETTES`, any case), softened into a city, when there are no `colors`; its first colour becomes the taxis'. */
7
+ palette?: PaletteName | (string & {});
8
+ /** The taxis' colour. Default `#f2a53a` (amber; `#ffb547` at night). */
9
+ accent?: string;
10
+ /** The ground the city fades into. Default `#e9e3d8` (`#0d1219` at night). */
11
+ background?: string;
12
+ /** How many cars, 0–60. Default 24. */
13
+ cars?: number;
14
+ /** How many of them are taxis: a share, 0–1, or a count, 1–60. Default 0.3. */
15
+ taxis?: number;
16
+ /** How fast they drive, 0–3; 0 holds them still. Default 1. */
17
+ speed?: number;
18
+ /** Which city, 1–9999: the same seed always builds the same streets and buildings. Default 7. */
19
+ seed?: number;
20
+ /** Blocks across, 3–10. Default 6. */
21
+ size?: number;
22
+ /** Night: lit windows, street lamps, headlight beams and glowing roof signs. Default false. */
23
+ night?: boolean;
24
+ /** A click or tap on a street calls the nearest taxi by road, which drives there and picks up. Default true. */
25
+ interactive?: boolean;
26
+ /** Let the view drift very slowly. Default true. */
27
+ drift?: boolean;
28
+ /** How close the camera is, 0.5–2. Default 1. */
29
+ zoom?: number;
30
+ /** Where the city sits on a wide screen (centred on a narrow one). Default `center`. */
31
+ side?: 'center' | 'left' | 'right';
32
+ /** An area faded towards the background behind copy: centre (0–1 from the top left), radius (a share of the height), depth (0–1). Default none; `{}` for the middle. */
33
+ quiet?: { x?: number; y?: number; radius?: number; depth?: number } | null;
34
+ }
35
+
36
+ /** Starts the city in `el`'s canvas. Returns a function that stops it. Without WebGL, `el` gets `is-flat` and the `--iso-*` colours. */
37
+ export function isoCity(el: HTMLElement, config: IsoCityConfig): () => void;
@@ -0,0 +1,458 @@
1
+ /**
2
+ * @module @arraypress/effects-astro/iso-city
3
+ *
4
+ * A small isometric city in flat-shaded low poly, the way a tidy puzzle
5
+ * game draws one: blocks of buildings with cluttered roofs, parks with
6
+ * round trees, streets with lane markings and zebra crossings, soft
7
+ * shadows, all fading into the background at the edges. Cars drive it,
8
+ * keeping their lanes and their distance, slowing into turns and waiting
9
+ * at crossings while people walk over; some are the brand's taxis, in its
10
+ * colour with a sign on the roof. Click a street and the nearest taxi by road comes: its route
11
+ * draws itself in, and it pulls in at the pin and picks up with a hop.
12
+ * `night` lights the windows, the lamps and the headlights.
13
+ *
14
+ * The city is built once from `seed` into one static mesh (and a baked
15
+ * shadow map); each frame only the cars' places are written, and every car
16
+ * is drawn in one instanced call. The camera drifts very slowly and leans
17
+ * a little towards the pointer, staying isometric. With reduced motion the
18
+ * cars are parked and one frame is drawn. Without WebGL the element gets
19
+ * `is-flat`, and the component's stylesheet draws a plain street grid.
20
+ *
21
+ * Opaque: it is the whole background of what it fills.
22
+ */
23
+
24
+ import { createStage } from './stage.js';
25
+ import { createProgram } from './gl.js';
26
+ import { instancing } from './instancing.js';
27
+ import { ISO_PITCH, isoCamera } from './iso-camera.js';
28
+ import { STRIDE } from './mesh.js';
29
+ import { canHover, isMobileGpu } from './device.js';
30
+ import { pointerIn, pressAt } from './pointer.js';
31
+ import { watchColors } from './mount.js';
32
+ import { quietUniform } from './glsl.js';
33
+ import { seededRandom } from './random.js';
34
+ import { toHex } from './color.js';
35
+ import { firstHit, planCity, SLAB } from './iso-city/plan.js';
36
+ import { buildRoads } from './iso-city/roads.js';
37
+ import { createTraffic } from './iso-city/traffic.js';
38
+ import { INSTANCE, PIN_SCALE, carInstances, peopleInstances, pinStates } from './iso-city/figures.js';
39
+ import { cityLook } from './iso-city/look.js';
40
+ import { carMesh, cityMesh, personMesh, pinMesh, ribbonMesh } from './iso-city/scene.js';
41
+ import { bakeShadows, shadowShift } from './iso-city/shadows.js';
42
+ import { BACKGROUND, FRAGMENT, VERTEX } from './iso-city/shaders.js';
43
+
44
+ /**
45
+ * @typedef {object} IsoCityConfig
46
+ * @property {string[]} [colors] Buildings, parks, roads (default warm off-white, sage, graphite; at night slate, deep green, near-black).
47
+ * @property {string} [palette] A shared palette by name (PALETTES; any case), softened into a city, when there are no `colors`.
48
+ * @property {string} [accent] The taxis' colour (default amber).
49
+ * @property {string} [background] The ground the city fades into (default paper; at night midnight).
50
+ * @property {number} [cars=24] How many cars, 0–60.
51
+ * @property {number} [taxis=0.3] How many are taxis: a share (under 1) or a count.
52
+ * @property {number} [speed=1] How fast they drive, 0–3.
53
+ * @property {number} [seed=7] Which city: any number; the same one always builds the same city.
54
+ * @property {number} [size=6] Blocks across, 3–10.
55
+ * @property {boolean} [night=false] Night: lit windows, street lamps, headlights.
56
+ * @property {boolean} [interactive=true] A click on a street calls the nearest taxi.
57
+ * @property {boolean} [drift=true] Let the view drift very slowly.
58
+ * @property {number} [zoom=1] How close the camera is, 0.5–2.
59
+ * @property {'center' | 'left' | 'right'} [side='center'] Where the city sits on a wide screen.
60
+ * @property {{ x?: number, y?: number, radius?: number, depth?: number } | null} [quiet=null]
61
+ * An area faded towards the background behind copy.
62
+ */
63
+
64
+ /** The direction towards the sun: from the left and in front, a little over halfway up the sky. */
65
+ const SUN = /** @type {[number, number, number]} */ ([-0.55, 1, 0.62].map((v, _, a) => v / Math.hypot(...a)));
66
+ /** Light by day and by night: ambient, sky, sun, and the windows' glow. */
67
+ const LIGHT = {
68
+ day: { ambient: [0.6, 0.6, 0.62], sky: [0.06, 0.07, 0.09], sun: [0.4, 0.38, 0.34], window: [1, 0.85, 0.6] },
69
+ night: { ambient: [0.64, 0.67, 0.74], sky: [0.04, 0.05, 0.08], sun: [0.16, 0.17, 0.2], window: [1, 0.8, 0.5] },
70
+ };
71
+ /** How fast a route draws itself in, world units a second. */
72
+ const REVEAL = 16;
73
+
74
+ /**
75
+ * Starts the city on an element: an opaque canvas inside it, filling it.
76
+ *
77
+ * @param {HTMLElement} el The element (its first canvas is drawn into).
78
+ * @param {IsoCityConfig} config The settings.
79
+ * @returns {() => void} Stops it.
80
+ */
81
+ export function isoCity(el, config) {
82
+ let look = cityLook(config);
83
+ const plan = planCity({ seed: config.seed ?? 7, size: Math.max(3, Math.min(10, config.size ?? 6)) });
84
+ const roads = buildRoads(plan);
85
+ const E = plan.extent;
86
+ const carCount = Math.max(0, Math.min(60, Math.round(config.cars ?? 24)));
87
+ const share = config.taxis ?? 0.3;
88
+ const taxiCount = share < 1 ? Math.round(carCount * share) : Math.round(share);
89
+ /** @type {ReturnType<typeof createTraffic> | null} */
90
+ let traffic = null;
91
+ /** @type {ReturnType<typeof isoCamera> | null} */
92
+ let camera = null;
93
+ const lean = { x: 0, y: 0 };
94
+ const start = performance.now();
95
+ /** @type {Map<number, { buffer: WebGLBuffer, count: number }>} */
96
+ const ribbons = new Map();
97
+ /** @type {(() => void) | null} */
98
+ let rebuild = null;
99
+ let clock = 0;
100
+ /**
101
+ * Draws the city over the stage's backdrop (moving everything on first);
102
+ * set once the stage is ready.
103
+ *
104
+ * @type {(now: number, dt: number) => void}
105
+ */
106
+ let drawScene = () => {};
107
+
108
+ /**
109
+ * Each frame's camera: framing the city for the canvas's shape, leaning
110
+ * towards the pointer and drifting slowly.
111
+ *
112
+ * @param {number} aspect The canvas's width over its height.
113
+ * @param {number} t Seconds since the start.
114
+ * @returns {{ camera: ReturnType<typeof isoCamera>, height: number }} The camera, and the world height it shows.
115
+ */
116
+ const frame = (aspect, t) => {
117
+ const zoom = Math.max(0.5, Math.min(2, config.zoom ?? 1));
118
+ const diagonal = (E + 1) * Math.SQRT2;
119
+ const width = (diagonal * 0.56) / zoom;
120
+ const height = Math.min(width / aspect, (diagonal * Math.sin(ISO_PITCH) * 0.92) / zoom);
121
+ const wide = aspect > 1.15;
122
+ const side = config.side === 'right' ? 0.3 : config.side === 'left' ? -0.3 : 0;
123
+ const drift = config.drift === false ? 0 : 1;
124
+ const camera = isoCamera({
125
+ target: [E / 2 + Math.sin(t * 0.021) * 0.9 * drift, 0, E / 2 + Math.cos(t * 0.017) * 0.9 * drift - 0.9 * drift],
126
+ yaw: Math.PI / 4 + lean.x * 0.07,
127
+ pitch: ISO_PITCH + lean.y * 0.035,
128
+ height,
129
+ aspect,
130
+ depth: E * 2 + 20,
131
+ shift: [wide ? side : 0, 0],
132
+ });
133
+ return { camera, height };
134
+ };
135
+
136
+ const { stage, stop } = createStage(el, {
137
+ fragment: BACKGROUND,
138
+ alpha: false,
139
+ antialias: true,
140
+ setup(st) {
141
+ const { gl } = st;
142
+ const program = createProgram(gl, VERTEX, FRAGMENT);
143
+ if (!program) return false;
144
+ const inst = instancing(gl);
145
+ const loc = {
146
+ normal: gl.getAttribLocation(program.program, 'aNormal'),
147
+ color: gl.getAttribLocation(program.program, 'aColor'),
148
+ uv: gl.getAttribLocation(program.program, 'aUv'),
149
+ tag: gl.getAttribLocation(program.program, 'aTag'),
150
+ place: gl.getAttribLocation(program.program, 'iPlace'),
151
+ paint: gl.getAttribLocation(program.program, 'iPaint'),
152
+ state: gl.getAttribLocation(program.program, 'iState'),
153
+ };
154
+ const vertexAttribs = /** @type {[number, number, number][]} */ ([[0, 3, 0], [loc.normal, 3, 3], [loc.color, 3, 6], [loc.uv, 2, 9], [loc.tag, 2, 11]]);
155
+ const instanceAttribs = [loc.place, loc.paint, loc.state];
156
+ traffic = createTraffic(roads, { cars: carCount, taxis: taxiCount, speed: config.speed ?? 1, paints: look.neutrals.length, parked: st.still, random: seededRandom((config.seed ?? 7) * 31 + 5) });
157
+
158
+ /**
159
+ * Uploads a mesh to a new buffer.
160
+ *
161
+ * @param {Float32Array} data The vertices.
162
+ * @returns {{ buffer: WebGLBuffer, count: number }} The buffer and its vertex count.
163
+ */
164
+ const upload = (data) => {
165
+ const buffer = /** @type {WebGLBuffer} */ (gl.createBuffer());
166
+ gl.bindBuffer(gl.ARRAY_BUFFER, buffer);
167
+ gl.bufferData(gl.ARRAY_BUFFER, data, gl.STATIC_DRAW);
168
+ return { buffer, count: data.length / STRIDE };
169
+ };
170
+ /** @type {Record<string, { buffer: WebGLBuffer, count: number }>} */
171
+ let meshes = {};
172
+ /** Builds (or rebuilds, after a colour change) the meshes that carry colours. */
173
+ const build = () => {
174
+ for (const m of Object.values(meshes)) gl.deleteBuffer(m.buffer);
175
+ const city = cityMesh(plan, look);
176
+ const car = carMesh(look);
177
+ const pin = pinMesh(frame(16 / 9, 0).camera.eye);
178
+ meshes = {
179
+ city: upload(city.solid),
180
+ glow: upload(city.glow),
181
+ car: upload(car.body),
182
+ carFx: upload(car.effects),
183
+ person: upload(personMesh(look)),
184
+ pin: upload(pin.body),
185
+ ring: upload(pin.ring),
186
+ };
187
+ };
188
+ build();
189
+ rebuild = () => {
190
+ build();
191
+ if (st.still) st.redraw();
192
+ };
193
+ const instances = gl.createBuffer();
194
+ const people = gl.createBuffer();
195
+ const carData = new Float32Array(Math.max(1, carCount) * INSTANCE);
196
+ /** @type {Float32Array} */
197
+ let peopleData = new Float32Array(INSTANCE * 16);
198
+
199
+ // The shadow map, baked once.
200
+ const shadows = bakeShadows(plan, SUN, isMobileGpu() ? 768 : 1024);
201
+ const texture = gl.createTexture();
202
+ gl.bindTexture(gl.TEXTURE_2D, texture);
203
+ gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, shadows.canvas);
204
+ gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR);
205
+ gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR);
206
+ gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
207
+ gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
208
+
209
+ /**
210
+ * Points the vertex attributes at a mesh.
211
+ *
212
+ * @param {WebGLBuffer} buffer The mesh's buffer.
213
+ */
214
+ const bindMesh = (buffer) => {
215
+ gl.bindBuffer(gl.ARRAY_BUFFER, buffer);
216
+ for (const [l, size, offset] of vertexAttribs) {
217
+ if (l < 0) continue;
218
+ gl.enableVertexAttribArray(l);
219
+ gl.vertexAttribPointer(l, size, gl.FLOAT, false, STRIDE * 4, offset * 4);
220
+ }
221
+ };
222
+ /**
223
+ * Points the instance attributes at a buffer of instances.
224
+ *
225
+ * @param {WebGLBuffer | null} buffer The instances.
226
+ */
227
+ const bindInstances = (buffer) => {
228
+ gl.bindBuffer(gl.ARRAY_BUFFER, buffer);
229
+ instanceAttribs.forEach((l, i) => {
230
+ if (l < 0) return;
231
+ gl.enableVertexAttribArray(l);
232
+ gl.vertexAttribPointer(l, 4, gl.FLOAT, false, INSTANCE * 4, i * 16);
233
+ inst?.divisor(l, 1);
234
+ });
235
+ };
236
+ /**
237
+ * Sets the instance attributes to one value for a plain draw.
238
+ *
239
+ * @param {ArrayLike<number>} [values] Twelve floats, as an instance; default at rest.
240
+ * @param {number} [offset=0] Where they start in `values`.
241
+ */
242
+ const single = (values = [0, 0, 0, 0, 1, 1, 1, -1, 0, 1, 1, 0], offset = 0) => {
243
+ instanceAttribs.forEach((l, i) => {
244
+ if (l < 0) return;
245
+ gl.disableVertexAttribArray(l);
246
+ const o = offset + i * 4;
247
+ gl.vertexAttrib4f(l, values[o], values[o + 1], values[o + 2], values[o + 3]);
248
+ });
249
+ };
250
+ /**
251
+ * Draws a mesh once per instance.
252
+ *
253
+ * @param {{ buffer: WebGLBuffer, count: number }} mesh The mesh.
254
+ * @param {WebGLBuffer | null} buffer The instances.
255
+ * @param {Float32Array} data Their values (for drawing one by one without instancing).
256
+ * @param {number} count How many.
257
+ */
258
+ const drawMany = (mesh, buffer, data, count) => {
259
+ if (!count) return;
260
+ bindMesh(mesh.buffer);
261
+ if (inst) {
262
+ bindInstances(buffer);
263
+ inst.draw(gl.TRIANGLES, 0, mesh.count, count);
264
+ return;
265
+ }
266
+ for (let i = 0; i < count; i++) {
267
+ single(data, i * INSTANCE);
268
+ gl.drawArrays(gl.TRIANGLES, 0, mesh.count);
269
+ }
270
+ };
271
+ /**
272
+ * Draws a mesh once, at rest or as one instance.
273
+ *
274
+ * @param {{ buffer: WebGLBuffer, count: number }} mesh The mesh.
275
+ * @param {ArrayLike<number>} [values] Its instance values.
276
+ */
277
+ const drawOne = (mesh, values) => {
278
+ bindMesh(mesh.buffer);
279
+ single(values);
280
+ gl.drawArrays(gl.TRIANGLES, 0, mesh.count);
281
+ };
282
+
283
+ const u = program.uniform;
284
+ const light = look.night ? LIGHT.night : LIGHT.day;
285
+ const shift = shadowShift(SUN, 1);
286
+
287
+ drawScene = (now, dt) => {
288
+ const t = (now - start) / 1000;
289
+ const tr = /** @type {NonNullable<typeof traffic>} */ (traffic);
290
+ if (!st.still && dt > 0) {
291
+ // Two steps when a frame runs long, so cars never jump far.
292
+ const steps = dt > 1 / 45 ? 2 : 1;
293
+ for (let k = 0; k < steps; k++) tr.step(dt / steps);
294
+ const p = canHover() ? pointerIn(el) : null;
295
+ const want = p?.inside ? { x: (p.x - 0.5) * 2, y: (p.y - 0.5) * 2 } : { x: 0, y: 0 };
296
+ lean.x += (want.x - lean.x) * Math.min(1, dt * 2);
297
+ lean.y += (want.y - lean.y) * Math.min(1, dt * 2);
298
+ }
299
+ clock = tr.time;
300
+ const { canvas } = st;
301
+ const aspect = canvas.width / canvas.height;
302
+ const view = frame(aspect, st.still ? 0 : t);
303
+ const cam = view.camera;
304
+ camera = cam;
305
+
306
+ // The cars' instances, then the people's: riders at pins, people on crossings.
307
+ carInstances(tr.cars, look, carData);
308
+ gl.bindBuffer(gl.ARRAY_BUFFER, instances);
309
+ gl.bufferData(gl.ARRAY_BUFFER, carData, gl.DYNAMIC_DRAW);
310
+
311
+ const folk = peopleInstances(tr, roads);
312
+ const folkCount = folk.length / INSTANCE;
313
+ if (peopleData.length < folk.length) peopleData = new Float32Array(folk.length * 2);
314
+ peopleData.set(folk);
315
+ gl.bindBuffer(gl.ARRAY_BUFFER, people);
316
+ gl.bufferData(gl.ARRAY_BUFFER, peopleData, gl.DYNAMIC_DRAW);
317
+
318
+ gl.useProgram(program.program);
319
+ gl.uniformMatrix4fv(u('uView'), false, cam.matrix);
320
+ gl.uniform2f(u('uRes'), canvas.width, canvas.height);
321
+ gl.uniform3fv(u('uBackground'), look.background);
322
+ gl.uniform3fv(u('uAccent'), look.accent);
323
+ gl.uniform3fv(u('uSun'), SUN);
324
+ gl.uniform3fv(u('uSunColor'), light.sun);
325
+ gl.uniform3fv(u('uAmbient'), light.ambient);
326
+ gl.uniform3fv(u('uSky'), light.sky);
327
+ gl.uniform3fv(u('uWindow'), light.window);
328
+ gl.uniform3fv(u('uEye'), cam.eye);
329
+ gl.uniform1f(u('uNight'), look.night ? 1 : 0);
330
+ gl.uniform1f(u('uTime'), t);
331
+ gl.uniform1f(u('uPixel'), view.height / canvas.height);
332
+ gl.uniform2f(u('uShadowShift'), shift[0], shift[1]);
333
+ gl.uniform3fv(u('uShadowRect'), shadows.rect);
334
+ gl.uniform4f(u('uFog'), E / 2, E / 2, E / 2 - 1.6, E / 2 + 2.4);
335
+ gl.uniform4f(u('uQuiet'), ...quietUniform(config.quiet, aspect));
336
+ gl.uniform4f(u('uRoute'), 0, 0, 1, 0);
337
+ gl.uniform1f(u('uPulse'), 0);
338
+ gl.activeTexture(gl.TEXTURE0);
339
+ gl.bindTexture(gl.TEXTURE_2D, texture);
340
+ gl.uniform1i(u('uShadow'), 0);
341
+
342
+ gl.clear(gl.DEPTH_BUFFER_BIT);
343
+ gl.enable(gl.DEPTH_TEST);
344
+ gl.depthFunc(gl.LEQUAL);
345
+ gl.enable(gl.CULL_FACE);
346
+
347
+ // Solid things.
348
+ drawOne(meshes.city);
349
+ drawMany(meshes.car, instances, carData, tr.cars.length);
350
+ drawMany(meshes.person, people, peopleData, folkCount);
351
+ const pins = pinStates(tr.jobs, clock);
352
+ for (const pin of pins) {
353
+ if (pin.scale > 0.01) drawOne(meshes.pin, [pin.job.x, pin.job.z, 0, SLAB + pin.lift, 1, 1, 1, -1, 0, 1, pin.scale * PIN_SCALE, 0]);
354
+ }
355
+
356
+ // Things laid over: shadows, light, routes, rings.
357
+ gl.disable(gl.CULL_FACE);
358
+ gl.depthMask(false);
359
+ gl.enable(gl.BLEND);
360
+ gl.blendFunc(gl.ONE, gl.ONE_MINUS_SRC_ALPHA);
361
+ if (look.night) drawOne(meshes.glow);
362
+ drawMany(meshes.carFx, instances, carData, tr.cars.length);
363
+ for (const job of tr.jobs) {
364
+ const since = clock - job.since;
365
+ const fade = job.state === 'going' ? 1 : job.state === 'done' ? 0 : Math.max(0, 1 - since / 0.5);
366
+ if (fade <= 0) continue;
367
+ let ribbon = ribbons.get(job.id);
368
+ if (!ribbon) {
369
+ ribbon = upload(ribbonMesh(job.points));
370
+ ribbons.set(job.id, ribbon);
371
+ }
372
+ const born = job.state === 'going' ? since : 99;
373
+ const trim = job.state === 'going' ? job.car.odo - job.odo : job.length;
374
+ gl.uniform4f(u('uRoute'), Math.max(0, trim - 0.1), born * REVEAL + 0.3, fade, 0);
375
+ drawOne(ribbon);
376
+ }
377
+ for (const pin of pins) {
378
+ gl.uniform4f(u('uRoute'), 0, 0, pin.fade, 0);
379
+ gl.uniform1f(u('uPulse'), pin.pulse);
380
+ drawOne(meshes.ring, [pin.job.x, pin.job.z, 0, SLAB, 1, 1, 1, -1, 0, 1, PIN_SCALE, 0]);
381
+ }
382
+ gl.disable(gl.BLEND);
383
+ gl.depthMask(true);
384
+ gl.disable(gl.DEPTH_TEST);
385
+
386
+ // Done with jobs whose last animation has played: forget them and their ribbons.
387
+ for (let i = tr.jobs.length - 1; i >= 0; i--) {
388
+ const job = tr.jobs[i];
389
+ if (job.state !== 'going' && job.state !== 'pickup' && clock - job.since > 1) {
390
+ const r = ribbons.get(job.id);
391
+ if (r) gl.deleteBuffer(r.buffer);
392
+ ribbons.delete(job.id);
393
+ tr.jobs.splice(i, 1);
394
+ }
395
+ }
396
+ // Leave attribute 0 alone for the stage's pass; the rest go back off.
397
+ for (const l of [...vertexAttribs.map(([l]) => l).slice(1), ...instanceAttribs]) {
398
+ if (l < 0) continue;
399
+ gl.disableVertexAttribArray(l);
400
+ if (instanceAttribs.includes(l)) inst?.divisor(l, 0);
401
+ }
402
+ };
403
+
404
+ return () => {
405
+ for (const m of Object.values(meshes)) gl.deleteBuffer(m.buffer);
406
+ for (const r of ribbons.values()) gl.deleteBuffer(r.buffer);
407
+ gl.deleteBuffer(instances);
408
+ gl.deleteBuffer(people);
409
+ gl.deleteTexture(texture);
410
+ };
411
+ },
412
+ uniforms({ gl, uniform }) {
413
+ gl.uniform3fv(uniform('uBackground'), look.background);
414
+ },
415
+ after(st, now, dt) {
416
+ drawScene(now, dt);
417
+ },
418
+ });
419
+
420
+ if (!stage) {
421
+ // No WebGL: the component's stylesheet draws a plain street grid in the city's colours.
422
+ el.classList.add('is-flat');
423
+ el.style.setProperty('--iso-ground', toHex(look.background));
424
+ el.style.setProperty('--iso-block', toHex(look.pavement));
425
+ el.style.setProperty('--iso-road', toHex(look.roads));
426
+ return stop;
427
+ }
428
+ stage.redraw();
429
+
430
+ /**
431
+ * Calls a taxi to the street under a press.
432
+ *
433
+ * @param {PointerEvent} e The press.
434
+ */
435
+ const press = (e) => {
436
+ const at = pressAt(el, e);
437
+ if (!at || !camera || !traffic) return;
438
+ // A press on a building means a street in front of it, not one hidden behind:
439
+ // from where the press meets the building, look a little towards the camera.
440
+ const ground = camera.ground(at.x * 2 - 1, at.y * 2 - 1, 0);
441
+ const hit = firstHit(plan, camera.ground(at.x * 2 - 1, at.y * 2 - 1, 6), ground);
442
+ const eye = Math.hypot(camera.eye[0], camera.eye[2]) || 1;
443
+ const p = hit ? [hit[0] + (camera.eye[0] / eye) * 1.4, hit[2] + (camera.eye[2] / eye) * 1.4] : [ground[0], ground[2]];
444
+ traffic.call(p[0], p[1]);
445
+ };
446
+ const interactive = config.interactive !== false && !stage.still;
447
+ if (interactive) document.addEventListener('pointerdown', press, { passive: true });
448
+
449
+ const unwatch = watchColors(el, (next) => {
450
+ look = cityLook({ ...config, colors: next.colors, accent: next.glint ?? config.accent });
451
+ rebuild?.();
452
+ });
453
+ return () => {
454
+ stop();
455
+ unwatch();
456
+ if (interactive) document.removeEventListener('pointerdown', press);
457
+ };
458
+ }
@@ -0,0 +1,11 @@
1
+ /** The signed distance from a point to a rounded rectangle's edge (px, negative inside), and the edge's outward unit normal there. The point is in px from the rectangle's top left. */
2
+ export function roundedRect(x: number, y: number, width: number, height: number, radius: number): { distance: number; nx: number; ny: number };
3
+
4
+ /** How hard the glass bends `depth` px in from its edge: 1 at the rim, easing to 0 at the bezel's inner edge; 0 outside the bezel or the panel. */
5
+ export function bend(depth: number, bezel: number): number;
6
+
7
+ /** How brightly the rim catches the light `depth` px in from the edge, whose outward normal is (`nx`, `ny`): a thin line, brightest facing the top left. 0–1. */
8
+ export function shine(depth: number, nx: number, ny: number, bezel: number): number;
9
+
10
+ /** A rounded panel's lens map, RGBA row by row at one pixel per CSS px: red and green where each point looks (0.5 + half the bend, towards the middle), blue the rim's highlight, alpha opaque. */
11
+ export function lensMap(width: number, height: number, radius: number, bezel: number): Uint8ClampedArray<ArrayBuffer>;
@@ -0,0 +1,146 @@
1
+ /**
2
+ * @module @arraypress/effects-astro/lens-map
3
+ *
4
+ * The displacement map of a rounded glass panel seen as a thick lens: a
5
+ * rounded rectangle's signed distance field, rasterised one pixel per CSS
6
+ * pixel. Red and green hold how far each point of the panel looks across
7
+ * and down (0.5 for not at all), strongest at the rim, where a thick slab
8
+ * of glass bends light hardest, and nothing in its flat middle. Blue holds
9
+ * where the rim catches the light, for a bright highlight. The pure maths
10
+ * is here; liquid-glass.js turns the map into an SVG filter.
11
+ *
12
+ * The technique (an SDF rasterised to a displacement map, blue as a
13
+ * specular mask, fed to `feDisplacementMap`) is adapted from Sam Asante's
14
+ * liquid-glass (https://github.com/samasante/liquid-glass, MIT,
15
+ * © 2026 Sam Asante). This is our own code.
16
+ */
17
+
18
+ /** The light the rim catches comes from the top left, mostly from above. */
19
+ const LIGHT = normalise(-0.45, -0.89);
20
+ /** How bright the rim facing away from the light is, against the lit side. */
21
+ const BACKLIGHT = 0.5;
22
+ /** How wide the rim's bright line is, px. */
23
+ const LINE = 1.6;
24
+ /** How bright the soft glow inside the line is, against the line. */
25
+ const GLOW = 0.22;
26
+
27
+ /**
28
+ * Scales a vector to length 1.
29
+ *
30
+ * @param {number} x Across.
31
+ * @param {number} y Down.
32
+ * @returns {[number, number]} The unit vector; [0, 0] for a zero one.
33
+ */
34
+ function normalise(x, y) {
35
+ const length = Math.hypot(x, y);
36
+ return length ? [x / length, y / length] : [0, 0];
37
+ }
38
+
39
+ /**
40
+ * The signed distance from a point to a rounded rectangle's edge, and the
41
+ * edge's outward normal there.
42
+ *
43
+ * @param {number} x The point across, px from the rectangle's left.
44
+ * @param {number} y The point down, px from its top.
45
+ * @param {number} width The rectangle's width, px.
46
+ * @param {number} height Its height, px.
47
+ * @param {number} radius Its corner radius, px (clamped to half the shorter side).
48
+ * @returns {{ distance: number, nx: number, ny: number }} The distance, px
49
+ * (negative inside), and the unit normal pointing out of the nearest edge.
50
+ */
51
+ export function roundedRect(x, y, width, height, radius) {
52
+ const r = Math.max(0, Math.min(radius, width / 2, height / 2));
53
+ const px = x - width / 2;
54
+ const py = y - height / 2;
55
+ const qx = Math.abs(px) - (width / 2 - r);
56
+ const qy = Math.abs(py) - (height / 2 - r);
57
+ const sx = px < 0 ? -1 : 1;
58
+ const sy = py < 0 ? -1 : 1;
59
+ if (qx > 0 && qy > 0) {
60
+ const [nx, ny] = normalise(qx, qy);
61
+ return { distance: Math.hypot(qx, qy) - r, nx: nx * sx, ny: ny * sy };
62
+ }
63
+ return qx > qy ? { distance: qx - r, nx: sx, ny: 0 } : { distance: qy - r, nx: 0, ny: sy };
64
+ }
65
+
66
+ /**
67
+ * How hard the glass bends at a depth in from its edge: 1 at the rim,
68
+ * easing to 0 where the bezel meets the flat middle, with no crease there.
69
+ *
70
+ * @param {number} depth Px in from the edge (0 at the rim).
71
+ * @param {number} bezel The rim's width, px.
72
+ * @returns {number} The bend, 0–1; 0 outside the bezel and outside the panel.
73
+ */
74
+ export function bend(depth, bezel) {
75
+ if (depth < 0 || bezel <= 0 || depth >= bezel) return 0;
76
+ const u = 1 - depth / bezel;
77
+ return u * u;
78
+ }
79
+
80
+ /**
81
+ * How brightly the rim catches the light at a point: a thin bright line
82
+ * along the edge with a soft glow inside it, brightest where the edge faces
83
+ * the light (top left) and fainter on the far side.
84
+ *
85
+ * @param {number} depth Px in from the edge.
86
+ * @param {number} nx The edge's outward normal, across.
87
+ * @param {number} ny The edge's outward normal, down.
88
+ * @param {number} bezel The rim's width, px.
89
+ * @returns {number} The highlight, 0–1.
90
+ */
91
+ export function shine(depth, nx, ny, bezel) {
92
+ if (depth < 0 || bezel <= 0 || depth >= bezel) return 0;
93
+ const facing = nx * LIGHT[0] + ny * LIGHT[1];
94
+ const lit = facing > 0 ? 0.35 + 0.65 * facing : BACKLIGHT * (0.35 - 0.65 * facing);
95
+ const line = Math.exp(-((depth / LINE) ** 2));
96
+ const glow = (1 - depth / bezel) ** 3 * GLOW;
97
+ return Math.min(1, lit * (line + glow));
98
+ }
99
+
100
+ /**
101
+ * Rasterises a rounded panel's lens map: RGBA, one pixel per CSS pixel,
102
+ * row by row. Red and green are where each point looks, across and down,
103
+ * as 0.5 + half the bend (towards the middle, as through the curved edge
104
+ * of a thick lens); blue is the rim's highlight; alpha is opaque. Only the
105
+ * bands within the bezel of an edge are worked out; the middle is filled flat.
106
+ *
107
+ * @param {number} width The panel's width, px (rounded to whole pixels, at least 1).
108
+ * @param {number} height Its height, px.
109
+ * @param {number} radius Its corner radius, px.
110
+ * @param {number} bezel The rim's width, px.
111
+ * @returns {Uint8ClampedArray<ArrayBuffer>} The map, `width * height * 4` bytes.
112
+ */
113
+ export function lensMap(width, height, radius, bezel) {
114
+ const w = Math.max(1, Math.round(width));
115
+ const h = Math.max(1, Math.round(height));
116
+ const data = new Uint8ClampedArray(w * h * 4);
117
+ new Uint32Array(data.buffer).fill(new Uint32Array(new Uint8Array([128, 128, 0, 255]).buffer)[0]);
118
+ const r = Math.max(0, Math.min(radius, w / 2, h / 2));
119
+ const band = Math.ceil(bezel + r);
120
+
121
+ /**
122
+ * Works out one pixel of the map.
123
+ *
124
+ * @param {number} x Its column.
125
+ * @param {number} y Its row.
126
+ */
127
+ const pixel = (x, y) => {
128
+ const { distance, nx, ny } = roundedRect(x + 0.5, y + 0.5, w, h, r);
129
+ const depth = -distance;
130
+ const amount = bend(depth, bezel);
131
+ const i = (y * w + x) * 4;
132
+ data[i] = 128 - nx * amount * 127;
133
+ data[i + 1] = 128 - ny * amount * 127;
134
+ data[i + 2] = shine(depth, nx, ny, bezel) * 255;
135
+ };
136
+
137
+ for (let y = 0; y < h; y++) {
138
+ if (y < band || y >= h - band) {
139
+ for (let x = 0; x < w; x++) pixel(x, y);
140
+ continue;
141
+ }
142
+ for (let x = 0; x < Math.min(band, w); x++) pixel(x, y);
143
+ for (let x = Math.max(band, w - band); x < w; x++) pixel(x, y);
144
+ }
145
+ return data;
146
+ }
@@ -0,0 +1,29 @@
1
+ import type { PaletteName } from './palettes.js';
2
+
3
+ export interface LightFieldConfig {
4
+ /** The light's three colours, as CSS hex. Default the palette's, or Lumetri's. */
5
+ colors?: string[];
6
+ /** A shared palette by name (`PALETTES`, any case), when there are no `colors`. */
7
+ palette?: PaletteName | (string & {});
8
+ /** The caustic bands' colour. Default the palette's glint, or a warm white, `#fff3c4`. */
9
+ glint?: string;
10
+ /** How fast the light moves. Default 1. */
11
+ speed?: number;
12
+ /** How strongly the pointer bends the light; 0 for not at all. Default 1. */
13
+ lens?: number;
14
+ /** Split the colours at the edges. Default true; always off on phones. */
15
+ prism?: boolean;
16
+ /** The fluid under the light: false for none, or its settings. Default true. */
17
+ fluid?: boolean | { trail?: number; drops?: boolean; force?: number; fade?: number; diffusion?: number; radius?: number; curl?: number; ignore?: string };
18
+ /** An area where the light dims: centre (0–1 from the top left), radius (share of the height), depth (0–1). */
19
+ quiet?: { x?: number; y?: number; radius?: number; depth?: number } | null;
20
+ /** How much the corners darken, 0–1. Default 0.45. */
21
+ vignette?: number;
22
+ /** Film grain. Default 0.035. */
23
+ grain?: number;
24
+ /** Share of the pixel ratio drawn at. Default 0.5. */
25
+ scale?: number;
26
+ }
27
+
28
+ /** Starts a light field in `el`'s canvas. Returns a function that stops it. */
29
+ export function lightField(el: HTMLElement, config: LightFieldConfig): () => void;