@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,673 @@
1
+ /**
2
+ * @module @arraypress/effects-astro/world-routes
3
+ *
4
+ * A dotted world map with routes arcing between cities: each route a
5
+ * faint arc that comets of light run along, again and again, pinging the
6
+ * city they reach. For "sell worldwide", "customers in 40 countries", or,
7
+ * with a `home`, "shipped from here to everywhere".
8
+ *
9
+ * The cities, routes and colours come from world-plan.js; the map from
10
+ * world-map.js, a region fitted to the box and drawn once a resize into
11
+ * strips that a drifting map pans across; the names' places from
12
+ * world-labels.js. Each frame lays the strips down and draws a few dozen
13
+ * strokes over them. Canvas 2D, no GPU needed. Hovering a city or a route
14
+ * brightens it and names it (not through a link or a button). With
15
+ * prefers-reduced-motion, one still frame with the comets part-way along.
16
+ *
17
+ * From the SugarCart site.
18
+ */
19
+
20
+ import { createCanvas2D } from './canvas2d.js';
21
+ import { canHover } from './device.js';
22
+ import { watchColors } from './mount.js';
23
+ import { CLICK_IGNORE, pointerIn } from './pointer.js';
24
+ import { fitView, greatCircle, paintLand, project, regionBounds } from './world-map.js';
25
+ import { placeLabels } from './world-labels.js';
26
+ import { CITIES, RAMP, planRoutes } from './world-plan.js';
27
+
28
+ export { CITIES };
29
+
30
+ /** @typedef {import('./world-plan.js').Place} Place */
31
+ /** @typedef {import('./world-plan.js').RouteSpec} RouteSpec */
32
+
33
+ /**
34
+ * @typedef {object} WorldRoutesConfig
35
+ * @property {RouteSpec[]} [routes] The routes: `[from, to]`, a place (from `home`), or `{ from, to, color, label, speed, width, dashed }`. Default ten routes round the world, or from `home` to ten cities round the world.
36
+ * @property {Place} [home] A hub: routes with no `from` start here, and it is marked out.
37
+ * @property {string} [color='#38bdf8'] The routes' colour.
38
+ * @property {string[]} [colors] A palette in place of `color`: each city takes the next colour (a home city the first, kept to itself), and a route the colour of where it ends.
39
+ * @property {string} [palette] A shared palette by name (PALETTES; any case), when there are no `colors`.
40
+ * @property {boolean} [gradient=false] Blend each route from its origin's colour to its destination's, in Oklab.
41
+ * @property {string} [land='#ffffff'] The land dots' and the names' colour.
42
+ * @property {number} [landOpacity=0.18] The land dots' opacity.
43
+ * @property {number} [pitch=7] Space between land dots, CSS px.
44
+ * @property {number} [dot=1.4] A land dot's radius, CSS px.
45
+ * @property {'round' | 'square'} [dotShape='round'] The land dots' shape.
46
+ * @property {boolean | number} [graticule=false] Faint lines of latitude and longitude: true for every 30°, or the degrees between them.
47
+ * @property {import('./world-map.js').RegionName | 'routes' | import('./world-map.js').Bounds} [region='world'] What the map shows.
48
+ * @property {'contain' | 'cover'} [fit='contain'] Show all of the region, or fill the box with it.
49
+ * @property {number} [drift=0] Pan the map east this many degrees a second (negative for west), round and round the world.
50
+ * @property {number} [lap=3.2] Seconds a comet takes along a route.
51
+ * @property {number} [lift] How high the arcs bow, a share of their length: 0.3, or 0.08 with a great-circle `curve`.
52
+ * @property {'bow' | 'great-circle'} [curve='bow'] Arcs that bow up between the cities, or that follow the shortest way over the globe.
53
+ * @property {number} [arcOpacity=0.14] The faint arcs' opacity.
54
+ * @property {boolean} [dashed=false] Dash the faint arcs, the dashes running from origin to destination.
55
+ * @property {number} [width=2.2] The comets' width, CSS px; the faint arcs are a line half as wide.
56
+ * @property {number} [tail=0.28] A comet's length, a share of its route.
57
+ * @property {number} [glow=10] The glow round a comet's head, CSS px.
58
+ * @property {number} [comets=1] Comets on each route at once.
59
+ * @property {number} [ping=17] How wide the ring a landing comet opens grows, CSS px; 0 for none.
60
+ * @property {boolean} [markers=true] Mark the cities with a dot.
61
+ * @property {number} [markerSize=2.2] A city dot's radius, CSS px.
62
+ * @property {boolean} [halo=false] A soft halo breathing round every city (the home city always has one).
63
+ * @property {boolean} [labels=false] Name the cities, leaving out names that would overlap.
64
+ * @property {string} [font='500 11px system-ui, sans-serif'] The names' CSS font.
65
+ * @property {boolean} [interactive=true] Hovering a city or a route brightens it and names it.
66
+ */
67
+
68
+ /** Pieces in an arc's path. */
69
+ const STEPS = 48;
70
+ /** Pieces in a comet's tail. */
71
+ const TAIL_STEPS = 16;
72
+ /** The share of a lap a comet rests for after landing, its tail drawn in. */
73
+ const REST = 0.25;
74
+ /** A strip of the map's width, CSS px (rounded to whole device px). */
75
+ const STRIP = 256;
76
+ /** How far past the box's edge something still counts as in view, CSS px (halos and names reach in). */
77
+ const MARGIN = 40;
78
+ /** The names' font when none is given. */
79
+ const FONT = '500 11px system-ui, sans-serif';
80
+
81
+ /**
82
+ * @typedef {object} Arc
83
+ * @property {import('./world-plan.js').PlannedRoute} route Its route.
84
+ * @property {Float32Array} points Its path, x and y in turn, CSS px on the unpanned map.
85
+ * @property {Float32Array} lengths How far along the path each point is, CSS px.
86
+ * @property {number} left Its leftmost x.
87
+ * @property {number} right Its rightmost x.
88
+ * @property {CanvasGradient | string} stroke What its faint arc is drawn in.
89
+ */
90
+
91
+ /**
92
+ * A CSS colour at an opacity: hex gains an alpha pair, anything else is
93
+ * mixed with transparent.
94
+ *
95
+ * @param {string} color The colour.
96
+ * @param {number} alpha Its opacity, 0–1.
97
+ * @returns {string} The colour with that opacity.
98
+ */
99
+ const withAlpha = (color, alpha) =>
100
+ /^#[0-9a-f]{6}$/i.test(color)
101
+ ? color + Math.round(Math.min(1, Math.max(0, alpha)) * 255).toString(16).padStart(2, '0')
102
+ : `color-mix(in srgb, ${color} ${Math.round(alpha * 100)}%, transparent)`;
103
+
104
+ /**
105
+ * Starts the map on an element (its first canvas is drawn into).
106
+ *
107
+ * @param {HTMLElement} el The element.
108
+ * @param {WorldRoutesConfig} config The settings.
109
+ * @returns {() => void} Stops it.
110
+ */
111
+ export function worldRoutes(el, config) {
112
+ const LAP = config.lap ?? 3.2;
113
+ const CURVE = config.curve === 'great-circle' ? 'great-circle' : 'bow';
114
+ const LIFT = config.lift ?? (CURVE === 'bow' ? 0.3 : 0.08);
115
+ const TAIL = Math.min(1, Math.max(0.02, config.tail ?? 0.28));
116
+ const GLOW = Math.max(0, config.glow ?? 10);
117
+ const COMETS = Math.max(0, Math.round(config.comets ?? 1));
118
+ const PING = Math.max(0, config.ping ?? 17);
119
+ const ARC = config.arcOpacity ?? 0.14;
120
+ const MARK = config.markerSize ?? 2.2;
121
+ const DRIFT = config.drift ?? 0;
122
+ const INK = config.land ?? '#ffffff';
123
+ const NAMES = config.font ?? FONT;
124
+ /** @type {import('./world-map.js').LandStyle} */
125
+ const land = {
126
+ color: INK,
127
+ opacity: config.landOpacity ?? 0.18,
128
+ pitch: Math.max(2, config.pitch ?? 7),
129
+ dot: config.dot ?? 1.4,
130
+ shape: config.dotShape === 'square' ? 'square' : 'round',
131
+ graticule: config.graticule === true ? 30 : Math.max(0, Number(config.graticule) || 0),
132
+ wrap: DRIFT !== 0,
133
+ };
134
+ const interactive = config.interactive !== false && canHover();
135
+ const start = performance.now();
136
+ let plan = planRoutes(config);
137
+
138
+ /** The view the map is drawn in, set on resize. */
139
+ let view = fitView(regionBounds('world'), 1, 1);
140
+ /** How tall the region is, CSS px: no arc bows higher than a share of it. */
141
+ let regionHeight = 1;
142
+ /** Device px a CSS px. */
143
+ let dpr = 1;
144
+ /** Each city's place on the unpanned map. */
145
+ let spots = /** @type {[number, number][]} */ ([]);
146
+ /** @type {Arc[]} */
147
+ let arcs = [];
148
+ /** Each city's name box, relative to the city, or null where it is left out. */
149
+ let names = /** @type {(import('./world-labels.js').Rect | null)[]} */ ([]);
150
+ /** Each city's name's width, and the names' height, CSS px. */
151
+ let nameWidths = /** @type {number[]} */ ([]);
152
+ let nameHeight = 12;
153
+ /** The map's strips, by index across the unpanned map; null where one would be empty. */
154
+ const strips = /** @type {Map<number, HTMLCanvasElement | null>} */ (new Map());
155
+ /** How picked out each city and each route is by the pointer, 0–1, and how much picking out is going on at all. */
156
+ let cityFocus = new Float32Array(0);
157
+ let routeFocus = new Float32Array(0);
158
+ let focused = 0;
159
+ /** The last check for a link or button under the pointer: where, and what it found. */
160
+ const blocked = { x: -1, y: -1, over: false };
161
+
162
+ /**
163
+ * Lays the routes out on the map: each arc's path, its length along the
164
+ * way and the colour its faint arc is drawn in.
165
+ *
166
+ * @param {CanvasRenderingContext2D} ctx The context (for gradients).
167
+ */
168
+ const layArcs = (ctx) => {
169
+ arcs = plan.routes.map((route) => {
170
+ const a = plan.cities[route.from];
171
+ const b = plan.cities[route.to];
172
+ const [ax, ay] = spots[route.from];
173
+ const points = new Float32Array((STEPS + 1) * 2);
174
+ if (CURVE === 'great-circle') {
175
+ const across = view.scale * view.squash;
176
+ greatCircle([a.lat, a.lon], [b.lat, b.lon], STEPS).forEach(([lat, lon], i) => {
177
+ points[i * 2] = ax + (lon - a.lon) * across;
178
+ points[i * 2 + 1] = project(view, lat, lon)[1];
179
+ });
180
+ } else {
181
+ const [bx, by] = spots[route.to];
182
+ for (let i = 0; i <= STEPS; i++) {
183
+ points[i * 2] = ax + ((bx - ax) * i) / STEPS;
184
+ points[i * 2 + 1] = ay + ((by - ay) * i) / STEPS;
185
+ }
186
+ }
187
+ // Lift the path into an arc, highest in the middle: a bow is the quadratic curve it always was.
188
+ const ex = points[STEPS * 2];
189
+ const ey = points[STEPS * 2 + 1];
190
+ const height = Math.min(Math.hypot(ex - ax, ey - ay) * LIFT, regionHeight * 0.3) * (CURVE === 'bow' ? 0.5 : 1);
191
+ const lengths = new Float32Array(STEPS + 1);
192
+ let left = Infinity;
193
+ let right = -Infinity;
194
+ for (let i = 0; i <= STEPS; i++) {
195
+ const t = i / STEPS;
196
+ points[i * 2 + 1] -= height * 4 * t * (1 - t);
197
+ if (i) lengths[i] = lengths[i - 1] + Math.hypot(points[i * 2] - points[i * 2 - 2], points[i * 2 + 1] - points[i * 2 - 1]);
198
+ left = Math.min(left, points[i * 2]);
199
+ right = Math.max(right, points[i * 2]);
200
+ }
201
+ /** @type {CanvasGradient | string} */
202
+ let stroke = route.ramp[0];
203
+ if (route.ramp[0] !== route.ramp[RAMP]) {
204
+ stroke = ctx.createLinearGradient(ax, ay, ex, ey);
205
+ for (let i = 0; i <= RAMP; i += 2) stroke.addColorStop(i / RAMP, route.ramp[i]);
206
+ }
207
+ return { route, points, lengths, left, right, stroke };
208
+ });
209
+ };
210
+
211
+ /**
212
+ * A city's name, ready to place.
213
+ *
214
+ * @param {number} i The city.
215
+ * @param {boolean} force Place it whatever it covers.
216
+ * @returns {import('./world-labels.js').LabelItem} The name, beside the city on the unpanned map.
217
+ */
218
+ const labelItem = (i, force) => ({
219
+ x: spots[i][0],
220
+ y: spots[i][1],
221
+ w: nameWidths[i],
222
+ h: nameHeight,
223
+ gap: MARK * (plan.cities[i].home ? 2.2 : 1.6) + 4,
224
+ force,
225
+ });
226
+
227
+ /**
228
+ * Fits the map to the element: the view, the cities' places, the arcs and
229
+ * the names, and lets go of the strips drawn at the old size.
230
+ *
231
+ * @param {import('./canvas2d.js').Canvas2D} c The canvas.
232
+ */
233
+ const resize = (c) => {
234
+ // A region fitted round the routes takes in the cities, and the great circles that rise above them.
235
+ const places = plan.cities.map((city) => /** @type {[number, number]} */ ([city.lat, city.lon]));
236
+ if (CURVE === 'great-circle') {
237
+ for (const { from, to } of plan.routes) {
238
+ const [a, b] = [plan.cities[from], plan.cities[to]];
239
+ places.push(...greatCircle([a.lat, a.lon], [b.lat, b.lon], 8));
240
+ }
241
+ }
242
+ const box = regionBounds(config.region, places);
243
+ view = fitView(box, c.width, c.height, config.fit === 'cover' ? 'cover' : 'contain');
244
+ regionHeight = (box.north - box.south) * view.scale;
245
+ dpr = c.canvas.width / c.width;
246
+ for (const canvas of strips.values()) if (canvas) canvas.width = 0;
247
+ strips.clear();
248
+ spots = plan.cities.map((city) => project(view, city.lat, city.lon));
249
+ layArcs(c.ctx);
250
+ cityFocus = new Float32Array(plan.cities.length);
251
+ routeFocus = new Float32Array(plan.routes.length);
252
+
253
+ c.ctx.font = NAMES;
254
+ const m = c.ctx.measureText('Hg');
255
+ nameHeight = Math.ceil((m.fontBoundingBoxAscent || 9) + (m.fontBoundingBoxDescent || 3));
256
+ nameWidths = plan.cities.map((city) => c.ctx.measureText(city.name).width);
257
+ names = plan.cities.map(() => null);
258
+ if (!config.labels) return;
259
+ // Home first, then the busiest cities, so the names that matter most get their place.
260
+ const order = plan.cities
261
+ .map((_, i) => i)
262
+ .sort((a, b) => Number(plan.cities[b].home) - Number(plan.cities[a].home) || plan.cities[b].routes - plan.cities[a].routes);
263
+ const dots = spots.map(([x, y]) => ({ x: x - MARK - 2, y: y - MARK - 2, w: MARK * 2 + 4, h: MARK * 2 + 4 }));
264
+ const boxes = placeLabels(
265
+ order.map((i) => labelItem(i, plan.cities[i].home)),
266
+ dots,
267
+ DRIFT ? null : { width: c.width, height: c.height },
268
+ );
269
+ order.forEach((i, k) => {
270
+ const placed = boxes[k];
271
+ names[i] = placed && { ...placed, x: placed.x - spots[i][0], y: placed.y - spots[i][1] };
272
+ });
273
+ };
274
+
275
+ /**
276
+ * A strip of the map, drawn the first time it is needed.
277
+ *
278
+ * @param {number} i Which strip, counting from the unpanned map's left.
279
+ * @param {number} width A strip's width, CSS px.
280
+ * @param {number} height The map's height, CSS px.
281
+ * @returns {HTMLCanvasElement | null} The strip, or null where there is no map.
282
+ */
283
+ const strip = (i, width, height) => {
284
+ const known = strips.get(i);
285
+ if (known !== undefined) return known;
286
+ const left = i * width;
287
+ const band = view.width / 2 - view.period / 2;
288
+ const empty = !land.wrap && (left + width <= band || left >= band + view.period);
289
+ const canvas = empty ? null : document.createElement('canvas');
290
+ const ctx = canvas?.getContext('2d');
291
+ if (canvas && ctx) {
292
+ canvas.width = Math.round(width * dpr);
293
+ canvas.height = Math.round(height * dpr);
294
+ ctx.setTransform(dpr, 0, 0, dpr, 0, 0);
295
+ paintLand(ctx, view, land, left, width);
296
+ }
297
+ const made = canvas && ctx ? canvas : null;
298
+ strips.set(i, made);
299
+ return made;
300
+ };
301
+
302
+ /**
303
+ * Lays the map's strips down, panned, and lets go of those panned away.
304
+ *
305
+ * @param {import('./canvas2d.js').Canvas2D} c The canvas.
306
+ * @param {number} pan How far the map has drifted, CSS px.
307
+ */
308
+ const drawLand = (c, pan) => {
309
+ const width = Math.round(STRIP * dpr) / dpr;
310
+ const first = Math.floor(pan / width);
311
+ const last = Math.floor((pan + c.width) / width);
312
+ for (let i = first; i <= last; i++) {
313
+ const canvas = strip(i, width, c.height);
314
+ if (canvas) c.ctx.drawImage(canvas, i * width - pan, 0, width, c.height);
315
+ }
316
+ for (const [i, canvas] of strips) {
317
+ if (i >= first - 1 && i <= last + 1) continue;
318
+ if (canvas) canvas.width = 0;
319
+ strips.delete(i);
320
+ }
321
+ };
322
+
323
+ /**
324
+ * Calls `fn` with each shift that brings something on the unpanned map
325
+ * into view. A still map is one turn of the world, so only shifts that
326
+ * land on that turn count (an arc over the date line comes in at the
327
+ * other side); a drifting one repeats round and round.
328
+ *
329
+ * @param {number} left Its leftmost x.
330
+ * @param {number} right Its rightmost x.
331
+ * @param {number} pan How far the map has drifted, CSS px.
332
+ * @param {(dx: number) => void} fn Draws it, shifted by `dx`.
333
+ */
334
+ const copies = (left, right, pan, fn) => {
335
+ const band = view.width / 2 - view.period / 2;
336
+ const lo = land.wrap ? pan - MARGIN : Math.max(-MARGIN, band);
337
+ const hi = land.wrap ? pan + view.width + MARGIN : Math.min(view.width + MARGIN, band + view.period);
338
+ const from = Math.ceil((lo - right) / view.period);
339
+ const to = Math.floor((hi - left) / view.period);
340
+ for (let m = from; m <= to; m++) fn(m * view.period - pan);
341
+ };
342
+
343
+ /**
344
+ * A point along an arc, by distance.
345
+ *
346
+ * @param {Arc} arc The arc.
347
+ * @param {number} u How far along, 0–1 of its length.
348
+ * @returns {[number, number]} CSS px on the unpanned map.
349
+ */
350
+ const along = (arc, u) => {
351
+ const { points, lengths } = arc;
352
+ const target = Math.min(1, Math.max(0, u)) * lengths[STEPS];
353
+ let lo = 0;
354
+ let hi = STEPS;
355
+ while (hi - lo > 1) {
356
+ const mid = (lo + hi) >> 1;
357
+ if (lengths[mid] < target) lo = mid;
358
+ else hi = mid;
359
+ }
360
+ const f = (target - lengths[lo]) / (lengths[hi] - lengths[lo] || 1);
361
+ return [points[lo * 2] + (points[hi * 2] - points[lo * 2]) * f, points[lo * 2 + 1] + (points[hi * 2 + 1] - points[lo * 2 + 1]) * f];
362
+ };
363
+
364
+ /**
365
+ * What the pointer is on: a city (first) or a route, unless it is over
366
+ * a link, a button or a field.
367
+ *
368
+ * @param {number} pan How far the map has drifted, CSS px.
369
+ * @returns {{ city: number, route: number }} Their indices, -1 for none.
370
+ */
371
+ const hovered = (pan) => {
372
+ const none = { city: -1, route: -1 };
373
+ if (!interactive) return none;
374
+ const p = pointerIn(el);
375
+ if (!p.inside) return none;
376
+ const px = p.x * view.width;
377
+ const py = (1 - p.y) * view.height;
378
+ let city = -1;
379
+ let route = -1;
380
+ let best = Math.max(12, MARK * 4);
381
+ spots.forEach(([x, y], i) =>
382
+ copies(x, x, pan, (dx) => {
383
+ const d = Math.hypot(px - x - dx, py - y);
384
+ if (d < best) [best, city] = [d, i];
385
+ }),
386
+ );
387
+ if (city < 0) {
388
+ best = 7;
389
+ arcs.forEach((arc, i) =>
390
+ copies(arc.left, arc.right, pan, (dx) => {
391
+ for (let s = 0; s < STEPS; s++) {
392
+ const d = toSegment(px - dx, py, arc.points, s);
393
+ if (d < best) [best, route] = [d, i];
394
+ }
395
+ }),
396
+ );
397
+ }
398
+ if (city < 0 && route < 0) return none;
399
+ // Only look up what is under the pointer when it has moved: the answer does not change otherwise.
400
+ if (px !== blocked.x || py !== blocked.y) {
401
+ const box = el.getBoundingClientRect();
402
+ blocked.over = !!document.elementFromPoint(box.left + px, box.top + py)?.closest(CLICK_IGNORE);
403
+ [blocked.x, blocked.y] = [px, py];
404
+ }
405
+ return blocked.over ? none : { city, route };
406
+ };
407
+
408
+ /**
409
+ * Eases each city's and route's highlight toward what the pointer is on.
410
+ *
411
+ * @param {{ city: number, route: number }} on What the pointer is on.
412
+ * @param {number} dt Seconds since the last frame.
413
+ */
414
+ const ease = (on, dt) => {
415
+ const k = 1 - Math.exp(-dt * 12);
416
+ for (let i = 0; i < cityFocus.length; i++) cityFocus[i] += ((i === on.city ? 1 : 0) - cityFocus[i]) * k;
417
+ for (let i = 0; i < routeFocus.length; i++) routeFocus[i] += ((i === on.route ? 1 : 0) - routeFocus[i]) * k;
418
+ focused += ((on.city >= 0 || on.route >= 0 ? 1 : 0) - focused) * k;
419
+ };
420
+
421
+ /**
422
+ * How bright something stays while something else is picked out.
423
+ *
424
+ * @param {number} lit How picked out it is itself, 0–1.
425
+ * @returns {number} The share of its opacity it keeps, 0–1.
426
+ */
427
+ const dim = (lit) => 1 - 0.65 * focused * (1 - lit);
428
+
429
+ /**
430
+ * Writes a name, first cutting the map out from under it so it reads
431
+ * over the dots and arcs on any background.
432
+ *
433
+ * @param {CanvasRenderingContext2D} ctx The context, its font and alignment set.
434
+ * @param {string} text The name.
435
+ * @param {number} x Where it is anchored, CSS px across.
436
+ * @param {number} y Its middle, CSS px down.
437
+ * @param {number} alpha Its opacity.
438
+ */
439
+ const write = (ctx, text, x, y, alpha) => {
440
+ if (alpha < 0.01) return;
441
+ ctx.globalAlpha = Math.min(1, alpha * 1.3);
442
+ ctx.globalCompositeOperation = 'destination-out';
443
+ ctx.strokeText(text, x, y);
444
+ ctx.globalCompositeOperation = 'source-over';
445
+ ctx.globalAlpha = alpha;
446
+ ctx.fillText(text, x, y);
447
+ };
448
+
449
+ /**
450
+ * One route on the map once: its faint arc, its comets and the pings
451
+ * where they land.
452
+ *
453
+ * @param {CanvasRenderingContext2D} ctx The context, shifted to this copy.
454
+ * @param {Arc} arc The route.
455
+ * @param {number} time Seconds since the start.
456
+ * @param {boolean} still Whether this is the one still frame.
457
+ * @param {number} lit How picked out it is, 0–1.
458
+ */
459
+ const drawRoute = (ctx, arc, time, still, lit) => {
460
+ const { route, points } = arc;
461
+ const shade = dim(lit);
462
+ const scale = route.width / 2.2;
463
+ ctx.globalAlpha = (ARC + (0.55 - ARC) * lit) * shade;
464
+ ctx.strokeStyle = arc.stroke;
465
+ ctx.lineWidth = scale * (1 + lit * 0.6);
466
+ if (route.dashed) {
467
+ ctx.setLineDash([4 * scale, 5 * scale]);
468
+ ctx.lineDashOffset = still ? 0 : -time * 12;
469
+ }
470
+ ctx.beginPath();
471
+ ctx.moveTo(points[0], points[1]);
472
+ for (let i = 1; i <= STEPS; i++) ctx.lineTo(points[i * 2], points[i * 2 + 1]);
473
+ ctx.stroke();
474
+ if (route.dashed) ctx.setLineDash([]);
475
+
476
+ // Each comet runs out, lets its tail follow it in, then rests a beat; several share a lap evenly.
477
+ for (let j = 0; j < COMETS; j++) {
478
+ const lap = (((time * route.speed) / LAP + route.offset + j / COMETS) % 1 + 1) % 1;
479
+ const phase = lap * (1 + TAIL + REST);
480
+ const head = Math.min(1, phase);
481
+ const tail = Math.max(0, phase - TAIL);
482
+ if (tail < 1) {
483
+ for (let s = 0; s < TAIL_STEPS; s++) {
484
+ const t0 = tail + ((head - tail) * s) / TAIL_STEPS;
485
+ const t1 = tail + ((head - tail) * (s + 1)) / TAIL_STEPS;
486
+ const [x0, y0] = along(arc, t0);
487
+ const [x1, y1] = along(arc, t1);
488
+ const k = (s + 1) / TAIL_STEPS;
489
+ ctx.globalAlpha = k * k * shade;
490
+ ctx.strokeStyle = route.ramp[Math.round(t1 * RAMP)];
491
+ ctx.lineWidth = (0.6 + k * 1.6) * scale * (1 + lit * 0.3);
492
+ ctx.beginPath();
493
+ ctx.moveTo(x0, y0);
494
+ ctx.lineTo(x1, y1);
495
+ ctx.stroke();
496
+ }
497
+ }
498
+ if (head < 1) {
499
+ const [hx, hy] = along(arc, head);
500
+ const color = route.ramp[Math.round(head * RAMP)];
501
+ ctx.globalAlpha = shade;
502
+ if (GLOW > 0) {
503
+ const r = GLOW * 1.3;
504
+ const glow = ctx.createRadialGradient(hx, hy, 0, hx, hy, r);
505
+ glow.addColorStop(0, withAlpha(color, 0.6));
506
+ glow.addColorStop(0.35, withAlpha(color, 0.2));
507
+ glow.addColorStop(1, withAlpha(color, 0));
508
+ ctx.fillStyle = glow;
509
+ ctx.fillRect(hx - r, hy - r, r * 2, r * 2);
510
+ }
511
+ ctx.fillStyle = route.cores[Math.round(head * RAMP)];
512
+ ctx.beginPath();
513
+ ctx.arc(hx, hy, 2.4 * scale * (1 + lit * 0.2), 0, Math.PI * 2);
514
+ ctx.fill();
515
+ }
516
+ // A ring opening where the comet lands.
517
+ const ping = (phase - 1) / 0.45;
518
+ if (PING > 0 && ping > 0 && ping < 1) {
519
+ ctx.globalAlpha = (1 - ping) * 0.8 * shade;
520
+ ctx.strokeStyle = route.ramp[RAMP];
521
+ ctx.lineWidth = 1.2;
522
+ ctx.beginPath();
523
+ ctx.arc(points[STEPS * 2], points[STEPS * 2 + 1], MARK + 1 + ping * Math.max(0, PING - MARK - 1), 0, Math.PI * 2);
524
+ ctx.stroke();
525
+ }
526
+ }
527
+ };
528
+
529
+ /**
530
+ * One city once: its halo (always for the home city), its ring (home),
531
+ * and its dot.
532
+ *
533
+ * @param {CanvasRenderingContext2D} ctx The context.
534
+ * @param {number} i The city.
535
+ * @param {number} x Where it is, CSS px across.
536
+ * @param {number} y Where it is, CSS px down.
537
+ * @param {number} time Seconds since the start.
538
+ * @param {boolean} still Whether this is the one still frame.
539
+ * @param {number} lit How picked out it is, 0–1.
540
+ */
541
+ const drawCity = (ctx, i, x, y, time, still, lit) => {
542
+ const city = plan.cities[i];
543
+ const shade = dim(lit);
544
+ const size = MARK * (city.home ? 1.45 : 1) * (1 + lit * 0.45);
545
+ ctx.globalAlpha = shade;
546
+ if (config.halo || city.home) {
547
+ const breath = still ? 0.5 : 0.5 + 0.5 * Math.sin(time * 2.2 + i * 1.7);
548
+ const r = size * (3.2 + breath * 1.8) * (city.home ? 1.3 : 1);
549
+ const halo = ctx.createRadialGradient(x, y, 0, x, y, r);
550
+ halo.addColorStop(0, withAlpha(city.color, 0.28 + 0.22 * breath));
551
+ halo.addColorStop(1, withAlpha(city.color, 0));
552
+ ctx.fillStyle = halo;
553
+ ctx.fillRect(x - r, y - r, r * 2, r * 2);
554
+ }
555
+ if (city.home) {
556
+ ctx.globalAlpha = 0.55 * shade;
557
+ ctx.strokeStyle = city.color;
558
+ ctx.lineWidth = 1;
559
+ ctx.beginPath();
560
+ ctx.arc(x, y, size * 2.4, 0, Math.PI * 2);
561
+ ctx.stroke();
562
+ }
563
+ ctx.globalAlpha = 0.95 * shade;
564
+ ctx.fillStyle = city.color;
565
+ ctx.beginPath();
566
+ ctx.arc(x, y, size, 0, Math.PI * 2);
567
+ ctx.fill();
568
+ };
569
+
570
+ /**
571
+ * One frame: the map, the routes, the cities and their names, and the
572
+ * name of the route under the pointer.
573
+ *
574
+ * @param {import('./canvas2d.js').Canvas2D} c The canvas.
575
+ * @param {number} now The time, ms.
576
+ * @param {number} dt Seconds since the last frame.
577
+ */
578
+ const draw = (c, now, dt) => {
579
+ const { ctx, still } = c;
580
+ const time = still ? LAP * 0.6 : (now - start) / 1000;
581
+ const pan = still ? 0 : Math.round(time * DRIFT * view.scale * view.squash * dpr) / dpr;
582
+ ctx.clearRect(0, 0, c.width, c.height);
583
+ ctx.globalAlpha = 1;
584
+ drawLand(c, pan);
585
+ if (!still) ease(hovered(pan), dt);
586
+ // A route is picked out with either of its cities, and both its cities with it.
587
+ const shine = arcs.map(({ route }, r) => Math.max(routeFocus[r], cityFocus[route.from], cityFocus[route.to]));
588
+ const lit = Array.from(cityFocus);
589
+ arcs.forEach(({ route }, r) => {
590
+ lit[route.from] = Math.max(lit[route.from], shine[r]);
591
+ lit[route.to] = Math.max(lit[route.to], shine[r]);
592
+ });
593
+
594
+ ctx.save();
595
+ if (!land.wrap) {
596
+ ctx.beginPath();
597
+ ctx.rect(view.width / 2 - view.period / 2, 0, view.period, c.height);
598
+ ctx.clip();
599
+ }
600
+ ctx.lineCap = 'round';
601
+ ctx.lineJoin = 'round';
602
+ arcs.forEach((arc, r) => {
603
+ copies(arc.left, arc.right, pan, (dx) => {
604
+ ctx.translate(dx, 0);
605
+ drawRoute(ctx, arc, time, still, shine[r]);
606
+ ctx.translate(-dx, 0);
607
+ });
608
+ });
609
+ ctx.restore();
610
+
611
+ if (config.markers !== false) {
612
+ spots.forEach(([x, y], i) => copies(x, x, pan, (dx) => drawCity(ctx, i, x + dx, y, time, still, lit[i])));
613
+ }
614
+
615
+ // The names: those placed on resize, and the one under the pointer wherever it fits.
616
+ ctx.font = NAMES;
617
+ ctx.fillStyle = INK;
618
+ ctx.strokeStyle = '#000';
619
+ ctx.lineWidth = 4;
620
+ ctx.lineJoin = 'round';
621
+ ctx.textBaseline = 'middle';
622
+ ctx.textAlign = 'left';
623
+ const bounds = { width: c.width, height: c.height };
624
+ spots.forEach(([x, y], i) => {
625
+ const own = names[i];
626
+ const forced = !own && cityFocus[i] > 0.01 ? placeLabels([labelItem(i, true)], [], bounds)[0] : null;
627
+ const box = own ?? (forced && { ...forced, x: forced.x - x, y: forced.y - y });
628
+ if (!box) return;
629
+ const base = own ? (plan.cities[i].home ? 0.92 : 0.66) : 0;
630
+ const alpha = (base + (1 - base) * cityFocus[i]) * dim(lit[i]);
631
+ copies(x, x, pan, (dx) => write(ctx, plan.cities[i].name, x + dx + box.x, y + box.y + box.h / 2, alpha));
632
+ });
633
+ ctx.textAlign = 'center';
634
+ arcs.forEach((arc, r) => {
635
+ if (routeFocus[r] < 0.01) return;
636
+ const [x, y] = along(arc, 0.5);
637
+ copies(x, x, pan, (dx) => write(ctx, arc.route.label, x + dx, y - nameHeight, routeFocus[r]));
638
+ });
639
+ ctx.globalAlpha = 1;
640
+ };
641
+
642
+ const { c, stop } = createCanvas2D(el, { draw, resize });
643
+ if (!c) return stop;
644
+ const unwatch = watchColors(el, (next) => {
645
+ plan = planRoutes({ ...config, colors: next.colors });
646
+ layArcs(c.ctx);
647
+ if (c.still) draw(c, performance.now(), 0);
648
+ });
649
+ return () => {
650
+ stop();
651
+ unwatch();
652
+ for (const canvas of strips.values()) if (canvas) canvas.width = 0;
653
+ strips.clear();
654
+ };
655
+ }
656
+
657
+ /**
658
+ * How far a point is from one piece of a path.
659
+ *
660
+ * @param {number} px The point, CSS px across.
661
+ * @param {number} py The point, CSS px down.
662
+ * @param {Float32Array} points The path, x and y in turn.
663
+ * @param {number} s The piece: from point `s` to point `s + 1`.
664
+ * @returns {number} The distance, CSS px.
665
+ */
666
+ function toSegment(px, py, points, s) {
667
+ const ax = points[s * 2];
668
+ const ay = points[s * 2 + 1];
669
+ const bx = points[s * 2 + 2] - ax;
670
+ const by = points[s * 2 + 3] - ay;
671
+ const t = Math.min(1, Math.max(0, ((px - ax) * bx + (py - ay) * by) / (bx * bx + by * by || 1)));
672
+ return Math.hypot(px - ax - bx * t, py - ay - by * t);
673
+ }