@astronox/core 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (259) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +66 -0
  3. package/package.json +77 -0
  4. package/src/client/clipboard.ts +7 -0
  5. package/src/client/focus.ts +17 -0
  6. package/src/client/index.ts +3 -0
  7. package/src/client/scroll.ts +15 -0
  8. package/src/components/actions/ActionListItem/ActionListItem.astro +78 -0
  9. package/src/components/actions/ActionListItem/ActionListItem.css +43 -0
  10. package/src/components/actions/Button/Button.astro +100 -0
  11. package/src/components/actions/Button/Button.css +165 -0
  12. package/src/components/actions/CardAction/CardAction.astro +97 -0
  13. package/src/components/actions/CardAction/CardAction.css +188 -0
  14. package/src/components/actions/MediaTrigger/MediaTrigger.astro +55 -0
  15. package/src/components/actions/MediaTrigger/MediaTrigger.css +175 -0
  16. package/src/components/actions/ScrollToTop/ScrollToTop.astro +36 -0
  17. package/src/components/actions/ScrollToTop/ScrollToTop.css +19 -0
  18. package/src/components/actions/ScrollToTop/scrollToTopController.ts +32 -0
  19. package/src/components/content/CardBody/CardBody.astro +31 -0
  20. package/src/components/content/CardBody/CardBody.css +74 -0
  21. package/src/components/content/CardFooter/CardFooter.astro +58 -0
  22. package/src/components/content/CardFooter/CardFooter.css +102 -0
  23. package/src/components/content/CardHeader/CardHeader.astro +96 -0
  24. package/src/components/content/CardHeader/CardHeader.css +164 -0
  25. package/src/components/content/CardLink/CardLink.astro +138 -0
  26. package/src/components/content/CardLink/CardLink.css +362 -0
  27. package/src/components/content/CodeBlock/CodeBlock.astro +102 -0
  28. package/src/components/content/CodeBlock/CodeBlock.css +85 -0
  29. package/src/components/content/CodeBlock/codeBlockController.ts +60 -0
  30. package/src/components/content/CodeBlock/contracts.ts +8 -0
  31. package/src/components/content/InlineDefinition/InlineDefinition.astro +31 -0
  32. package/src/components/content/InlineDefinition/InlineDefinition.css +9 -0
  33. package/src/components/content/MarkdownContent/MarkdownContent.astro +32 -0
  34. package/src/components/content/MarkdownContent/MarkdownContent.css +358 -0
  35. package/src/components/content/MarkdownContent/MarkdownInline.astro +44 -0
  36. package/src/components/content/MarkdownContent/MarkdownNode.astro +280 -0
  37. package/src/components/content/MarkdownContent/MarkdownSymbol.astro +47 -0
  38. package/src/components/content/MarkdownContent/contracts.ts +181 -0
  39. package/src/components/data-display/BadgeList/BadgeList.astro +54 -0
  40. package/src/components/data-display/BadgeList/BadgeList.css +29 -0
  41. package/src/components/data-display/IconList/IconList.astro +64 -0
  42. package/src/components/data-display/IconList/IconList.css +55 -0
  43. package/src/components/data-display/MetricTile/MetricTile.astro +68 -0
  44. package/src/components/data-display/MetricTile/MetricTile.css +103 -0
  45. package/src/components/data-display/MetricTile/MetricTileContent.astro +70 -0
  46. package/src/components/data-display/MetricTile/metricTileController.ts +9 -0
  47. package/src/components/data-display/Tag/Tag.astro +67 -0
  48. package/src/components/data-display/Tag/Tag.css +134 -0
  49. package/src/components/data-display/Tag/TagAffix.astro +33 -0
  50. package/src/components/data-display/TextPair/TextPair.astro +63 -0
  51. package/src/components/data-display/TextPair/TextPair.css +99 -0
  52. package/src/components/feedback/Spinner/Spinner.astro +32 -0
  53. package/src/components/feedback/Spinner/Spinner.css +37 -0
  54. package/src/components/feedback/Tooltip/Tooltip.astro +66 -0
  55. package/src/components/feedback/Tooltip/Tooltip.css +67 -0
  56. package/src/components/feedback/Tooltip/tooltipController.ts +371 -0
  57. package/src/components/forms/FieldError/FieldError.astro +31 -0
  58. package/src/components/forms/FieldError/FieldError.css +12 -0
  59. package/src/components/forms/FieldError/fieldErrorState.ts +21 -0
  60. package/src/components/forms/FieldLabel/FieldLabel.astro +32 -0
  61. package/src/components/forms/FieldLabel/FieldLabel.css +17 -0
  62. package/src/components/forms/TextInput/TextInput.astro +79 -0
  63. package/src/components/forms/TextInput/TextInput.css +121 -0
  64. package/src/components/forms/TextInput/textInputController.ts +53 -0
  65. package/src/components/icons/ArrowDownIcon.astro +26 -0
  66. package/src/components/icons/ArrowLeftIcon.astro +26 -0
  67. package/src/components/icons/ArrowRightIcon.astro +26 -0
  68. package/src/components/icons/ArrowUpDownIcon.astro +26 -0
  69. package/src/components/icons/ArrowUpIcon.astro +26 -0
  70. package/src/components/icons/BulletIcon.astro +21 -0
  71. package/src/components/icons/BurnIcon.astro +24 -0
  72. package/src/components/icons/CircleSymbolIcon.css +66 -0
  73. package/src/components/icons/ClearIcon.astro +28 -0
  74. package/src/components/icons/CloseIcon.astro +21 -0
  75. package/src/components/icons/CopyIcon.astro +30 -0
  76. package/src/components/icons/CurrencyIcon.css +9 -0
  77. package/src/components/icons/DayIcon.astro +33 -0
  78. package/src/components/icons/DollarIcon.astro +28 -0
  79. package/src/components/icons/DownloadIcon.astro +22 -0
  80. package/src/components/icons/EnterFullscreenIcon.astro +22 -0
  81. package/src/components/icons/EqualIcon.astro +25 -0
  82. package/src/components/icons/ExitFullscreenIcon.astro +22 -0
  83. package/src/components/icons/ExternalLinkIcon.astro +22 -0
  84. package/src/components/icons/FieldIcon.astro +28 -0
  85. package/src/components/icons/GrassIcon.astro +29 -0
  86. package/src/components/icons/InfoIcon.astro +24 -0
  87. package/src/components/icons/MinusIcon.astro +21 -0
  88. package/src/components/icons/MountainIcon.astro +31 -0
  89. package/src/components/icons/NightIcon.astro +32 -0
  90. package/src/components/icons/PlayIcon.astro +22 -0
  91. package/src/components/icons/PlusIcon.astro +21 -0
  92. package/src/components/icons/PoisonIcon.astro +24 -0
  93. package/src/components/icons/RibbonIcon.astro +25 -0
  94. package/src/components/icons/SlashIcon.astro +22 -0
  95. package/src/components/icons/SlowIcon.astro +25 -0
  96. package/src/components/icons/StarIcon.astro +28 -0
  97. package/src/components/icons/StunIcon.astro +22 -0
  98. package/src/components/icons/TableSortIcon.astro +34 -0
  99. package/src/components/icons/WaterIcon.astro +30 -0
  100. package/src/components/index.ts +83 -0
  101. package/src/components/media/CanvasFrame/CanvasFrame.astro +130 -0
  102. package/src/components/media/CanvasFrame/CanvasFrame.css +59 -0
  103. package/src/components/media/CanvasFrame/canvasFrameController.ts +104 -0
  104. package/src/components/media/EmbedFrame/EmbedFrame.astro +147 -0
  105. package/src/components/media/EmbedFrame/EmbedFrame.css +51 -0
  106. package/src/components/media/EmbedFrame/embedFrameController.ts +66 -0
  107. package/src/components/media/MediaFrame/MediaFrame.astro +34 -0
  108. package/src/components/media/MediaFrame/MediaFrame.css +66 -0
  109. package/src/components/navigation/Breadcrumbs/Breadcrumbs.astro +55 -0
  110. package/src/components/navigation/Breadcrumbs/Breadcrumbs.css +89 -0
  111. package/src/components/navigation/DesktopNavigation.astro +92 -0
  112. package/src/components/navigation/DesktopNavigation.css +99 -0
  113. package/src/components/navigation/Header.astro +73 -0
  114. package/src/components/navigation/Header.css +20 -0
  115. package/src/components/navigation/MobileNavigation.astro +124 -0
  116. package/src/components/navigation/MobileNavigation.css +177 -0
  117. package/src/components/navigation/NavigationBrand.astro +63 -0
  118. package/src/components/navigation/NavigationBrand.css +52 -0
  119. package/src/components/navigation/NavigationControl.css +166 -0
  120. package/src/components/navigation/NavigationItemContent.astro +24 -0
  121. package/src/components/navigation/NavigationLink.astro +121 -0
  122. package/src/components/navigation/NavigationLinks.astro +27 -0
  123. package/src/components/navigation/NavigationMenuList.astro +37 -0
  124. package/src/components/navigation/NavigationMenuList.css +6 -0
  125. package/src/components/navigation/SkipLink/SkipLink.astro +18 -0
  126. package/src/components/navigation/SkipLink/SkipLink.css +26 -0
  127. package/src/components/navigation/SubNavigation/SubNavigation.astro +124 -0
  128. package/src/components/navigation/SubNavigation/SubNavigation.css +211 -0
  129. package/src/components/navigation/mobileNavigationController.ts +122 -0
  130. package/src/components/navigation/navigationState.ts +35 -0
  131. package/src/components/navigation/navigationTypes.ts +18 -0
  132. package/src/components/seo/SeoHead.astro +194 -0
  133. package/src/components/widgetTypes.ts +3 -0
  134. package/src/composition/Grid/Grid.astro +82 -0
  135. package/src/composition/Grid/Grid.css +314 -0
  136. package/src/composition/GridItem/GridItem.astro +50 -0
  137. package/src/composition/GridItem/GridItem.css +95 -0
  138. package/src/composition/Inline/Inline.astro +48 -0
  139. package/src/composition/Inline/Inline.css +65 -0
  140. package/src/composition/Stack/Stack.astro +37 -0
  141. package/src/composition/Stack/Stack.css +31 -0
  142. package/src/composition/SurfaceGroup/SurfaceGroup.astro +44 -0
  143. package/src/composition/SurfaceGroup/SurfaceGroup.css +76 -0
  144. package/src/composition/SurfaceGroup/stickySurfaceGroupController.ts +89 -0
  145. package/src/composition/index.ts +6 -0
  146. package/src/config/index.ts +2 -0
  147. package/src/config/pageMeta.ts +55 -0
  148. package/src/config/siteConfig.ts +90 -0
  149. package/src/data/collectionValidation.ts +61 -0
  150. package/src/data/index.ts +1 -0
  151. package/src/display/contracts.ts +284 -0
  152. package/src/display/index.ts +1 -0
  153. package/src/index.ts +6 -0
  154. package/src/layouts/BaseLayout.astro +74 -0
  155. package/src/layouts/PageSurface.astro +15 -0
  156. package/src/layouts/PageSurface.css +24 -0
  157. package/src/layouts/index.ts +2 -0
  158. package/src/patterns/CircuitPuzzle/CircuitPuzzle.astro +93 -0
  159. package/src/patterns/CircuitPuzzle/CircuitPuzzle.css +118 -0
  160. package/src/patterns/CircuitPuzzle/circuitPuzzleController.ts +205 -0
  161. package/src/patterns/CircuitPuzzle/contracts.ts +42 -0
  162. package/src/patterns/Combobox/Combobox.astro +462 -0
  163. package/src/patterns/Combobox/Combobox.css +403 -0
  164. package/src/patterns/Combobox/comboboxController.ts +788 -0
  165. package/src/patterns/Combobox/contracts.ts +18 -0
  166. package/src/patterns/Dropdown/Dropdown.astro +127 -0
  167. package/src/patterns/Dropdown/Dropdown.css +100 -0
  168. package/src/patterns/Dropdown/dropdownController.ts +157 -0
  169. package/src/patterns/Fullscreen/FullscreenControl.astro +103 -0
  170. package/src/patterns/Fullscreen/FullscreenControl.css +49 -0
  171. package/src/patterns/Fullscreen/fullscreenController.ts +73 -0
  172. package/src/patterns/GlitchOverlay/GlitchOverlay.astro +50 -0
  173. package/src/patterns/GlitchOverlay/GlitchOverlay.css +234 -0
  174. package/src/patterns/GlitchOverlay/glitchOverlayController.ts +152 -0
  175. package/src/patterns/GuidedFlow/GuidedFlow.astro +60 -0
  176. package/src/patterns/GuidedFlow/GuidedFlow.css +82 -0
  177. package/src/patterns/GuidedFlow/contracts.ts +3 -0
  178. package/src/patterns/GuidedFlow/guidedFlowController.ts +49 -0
  179. package/src/patterns/MagneticFieldPuzzle/MagneticFieldPuzzle.astro +217 -0
  180. package/src/patterns/MagneticFieldPuzzle/MagneticFieldPuzzle.css +383 -0
  181. package/src/patterns/MagneticFieldPuzzle/contracts.ts +81 -0
  182. package/src/patterns/MagneticFieldPuzzle/magneticFieldPuzzleController.ts +802 -0
  183. package/src/patterns/MagneticFieldPuzzle/simulation.ts +244 -0
  184. package/src/patterns/MapViewport/MapViewport.astro +296 -0
  185. package/src/patterns/MapViewport/MapViewport.css +257 -0
  186. package/src/patterns/MapViewport/contracts.ts +79 -0
  187. package/src/patterns/MapViewport/geometry.ts +80 -0
  188. package/src/patterns/MapViewport/mapViewportController.ts +636 -0
  189. package/src/patterns/Modal/Modal.astro +64 -0
  190. package/src/patterns/Modal/Modal.css +87 -0
  191. package/src/patterns/Modal/ModalClose.astro +37 -0
  192. package/src/patterns/Modal/ModalOpen.astro +36 -0
  193. package/src/patterns/Modal/modalController.ts +118 -0
  194. package/src/patterns/PuzzleGrid/PuzzleGrid.astro +22 -0
  195. package/src/patterns/PuzzleGrid/PuzzleGrid.css +149 -0
  196. package/src/patterns/PuzzleGrid/contracts.ts +2 -0
  197. package/src/patterns/PuzzleGrid/selectionDismissal.ts +27 -0
  198. package/src/patterns/PuzzleGrid/state.ts +25 -0
  199. package/src/patterns/SpriteProjectilePreview/SpriteProjectilePreview.astro +65 -0
  200. package/src/patterns/SpriteProjectilePreview/SpriteProjectilePreview.css +35 -0
  201. package/src/patterns/SpriteProjectilePreview/contracts.ts +33 -0
  202. package/src/patterns/SpriteProjectilePreview/spriteProjectilePreviewController.ts +586 -0
  203. package/src/patterns/StageFlow/StageFlow.astro +57 -0
  204. package/src/patterns/StageFlow/StageFlow.css +54 -0
  205. package/src/patterns/StageFlow/StageFlowAlternative.astro +23 -0
  206. package/src/patterns/StageFlow/StageFlowArrow.astro +22 -0
  207. package/src/patterns/StageFlow/StageFlowConnector.astro +7 -0
  208. package/src/patterns/StageFlow/StageFlowConnector.css +4 -0
  209. package/src/patterns/StageFlow/StageFlowStage.astro +51 -0
  210. package/src/patterns/StageFlow/StageFlowStage.css +106 -0
  211. package/src/patterns/StageFlow/StageFlowStep.astro +24 -0
  212. package/src/patterns/StageFlow/StageFlowStep.css +19 -0
  213. package/src/patterns/StageFlow/StageFlowSymbol.css +28 -0
  214. package/src/patterns/Table/Table.astro +176 -0
  215. package/src/patterns/Table/Table.css +70 -0
  216. package/src/patterns/Table/TableBody.astro +185 -0
  217. package/src/patterns/Table/TableBody.css +262 -0
  218. package/src/patterns/Table/TableCell.astro +59 -0
  219. package/src/patterns/Table/TableCell.css +99 -0
  220. package/src/patterns/Table/TableCellValue.astro +86 -0
  221. package/src/patterns/Table/TableFooter.astro +32 -0
  222. package/src/patterns/Table/TableFooter.css +31 -0
  223. package/src/patterns/Table/TableHeader.astro +118 -0
  224. package/src/patterns/Table/TableHeader.css +209 -0
  225. package/src/patterns/Table/TablePagination.astro +70 -0
  226. package/src/patterns/Table/TablePagination.css +133 -0
  227. package/src/patterns/Table/TableSkeleton.astro +36 -0
  228. package/src/patterns/Table/TableSkeleton.css +81 -0
  229. package/src/patterns/Table/tableCellController.ts +11 -0
  230. package/src/patterns/Table/tableFilterController.ts +206 -0
  231. package/src/patterns/Table/tableHeaderOverflowController.ts +105 -0
  232. package/src/patterns/Table/tableLayout.ts +113 -0
  233. package/src/patterns/Table/tableProgressiveController.ts +319 -0
  234. package/src/patterns/Table/tableRowNavigationController.ts +91 -0
  235. package/src/patterns/Table/tableScrollController.ts +192 -0
  236. package/src/patterns/Table/tableSortController.ts +272 -0
  237. package/src/patterns/index.ts +46 -0
  238. package/src/primitives/index.ts +4 -0
  239. package/src/primitives/layout/Separator/Separator.astro +29 -0
  240. package/src/primitives/layout/Separator/Separator.css +20 -0
  241. package/src/primitives/media/AmbientAudio/AmbientAudio.astro +28 -0
  242. package/src/primitives/media/Media/Media.astro +122 -0
  243. package/src/primitives/media/Media/Media.css +161 -0
  244. package/src/primitives/text/TextBlock/TextBlock.astro +46 -0
  245. package/src/primitives/text/TextBlock/TextBlock.css +21 -0
  246. package/src/seo/StaticRedirect.astro +32 -0
  247. package/src/seo/index.ts +6 -0
  248. package/src/seo/pageSeo.ts +99 -0
  249. package/src/seo/robots.ts +16 -0
  250. package/src/seo/sitemap.ts +38 -0
  251. package/src/seo/structuredData.ts +133 -0
  252. package/src/seo/url.ts +64 -0
  253. package/src/styles/base.css +148 -0
  254. package/src/styles/interaction.css +108 -0
  255. package/src/styles/style.css +93 -0
  256. package/src/styles/tokens.css +256 -0
  257. package/src/theme/contracts.ts +7 -0
  258. package/src/theme/index.ts +1 -0
  259. package/tsconfig.json +20 -0
@@ -0,0 +1,586 @@
1
+ import type {
2
+ SpriteProjectilePreviewModel,
3
+ SpriteSheetAnimation,
4
+ } from './contracts';
5
+
6
+ const initializedAttribute = 'data-sprite-projectile-preview-initialized';
7
+ const intervalChangeEvent = 'sprite-projectile-preview:interval-change';
8
+ const canvasSelector = '[data-sprite-projectile-preview-canvas]';
9
+ const configSelector = '[data-sprite-projectile-preview-config]';
10
+ const maximumDevicePixelRatio = 2;
11
+ const maximumCatchUpProjectiles = 8;
12
+ const actorCenterRatio = 1 / 3;
13
+ const scaleReferenceFootprintRatio = 0.45;
14
+ const scenePaddingRatio = 0.08;
15
+ const noop = (): void => undefined;
16
+
17
+ type LoadedAnimation = {
18
+ image: HTMLImageElement;
19
+ model: SpriteSheetAnimation;
20
+ frameWidth: number;
21
+ frameHeight: number;
22
+ };
23
+
24
+ type ActiveProjectile = {
25
+ launchedAt: number;
26
+ };
27
+
28
+ type AnimationPlacement = {
29
+ visibleCenterX: number;
30
+ visibleCenterY: number;
31
+ };
32
+
33
+ type SceneLayout = {
34
+ scale: number;
35
+ actor: AnimationPlacement;
36
+ projectile?: {
37
+ startCenterX: number;
38
+ visibleCenterY: number;
39
+ visibleWidth: number;
40
+ };
41
+ };
42
+
43
+ export function setSpriteProjectilePreviewInterval(
44
+ root: HTMLElement,
45
+ intervalMs: number,
46
+ ): void {
47
+ if (!Number.isFinite(intervalMs) || intervalMs <= 0) return;
48
+
49
+ root.dataset.spriteProjectilePreviewInterval = String(intervalMs);
50
+ root.dispatchEvent(
51
+ new CustomEvent<number>(intervalChangeEvent, {
52
+ detail: intervalMs,
53
+ }),
54
+ );
55
+ }
56
+
57
+ export function initSpriteProjectilePreview(root: HTMLElement): () => void {
58
+ if (root.getAttribute(initializedAttribute) === 'true') return noop;
59
+
60
+ const canvas = root.querySelector<HTMLCanvasElement>(canvasSelector);
61
+ const config = root.querySelector<HTMLScriptElement>(configSelector);
62
+ const context = canvas?.getContext('2d');
63
+ const initialModel = readModel(config?.textContent);
64
+
65
+ if (!canvas || !context || !initialModel) return noop;
66
+
67
+ root.setAttribute(initializedAttribute, 'true');
68
+
69
+ const requestedInterval = Number(
70
+ root.dataset.spriteProjectilePreviewInterval,
71
+ );
72
+ let model =
73
+ initialModel.projectile &&
74
+ Number.isFinite(requestedInterval) &&
75
+ requestedInterval > 0
76
+ ? {
77
+ ...initialModel,
78
+ projectile: {
79
+ ...initialModel.projectile,
80
+ intervalMs: requestedInterval,
81
+ },
82
+ }
83
+ : initialModel;
84
+ let actor: LoadedAnimation | null = null;
85
+ let projectile: LoadedAnimation | null = null;
86
+ let logicalWidth = 1;
87
+ let logicalHeight = 1;
88
+ let animationFrame: number | null = null;
89
+ let animationStartedAt = 0;
90
+ let lastLaunchAt: number | null = null;
91
+ let activeProjectiles: ActiveProjectile[] = [];
92
+ let isDestroyed = false;
93
+ let isIntersecting = !('IntersectionObserver' in window);
94
+ let isDocumentVisible = !document.hidden;
95
+ const reducedMotion = window.matchMedia('(prefers-reduced-motion: reduce)');
96
+
97
+ const stop = (): void => {
98
+ if (animationFrame !== null) {
99
+ window.cancelAnimationFrame(animationFrame);
100
+ animationFrame = null;
101
+ }
102
+ lastLaunchAt = null;
103
+ activeProjectiles = [];
104
+ };
105
+
106
+ const resize = (): void => {
107
+ const bounds = canvas.getBoundingClientRect();
108
+ const nextWidth = Math.max(1, bounds.width);
109
+ const nextHeight = Math.max(1, bounds.height);
110
+ const pixelRatio = Math.min(
111
+ maximumDevicePixelRatio,
112
+ Math.max(1, window.devicePixelRatio || 1),
113
+ );
114
+ const physicalWidth = Math.max(1, Math.round(nextWidth * pixelRatio));
115
+ const physicalHeight = Math.max(1, Math.round(nextHeight * pixelRatio));
116
+
117
+ logicalWidth = nextWidth;
118
+ logicalHeight = nextHeight;
119
+
120
+ const hasNewCanvasSize =
121
+ canvas.width !== physicalWidth || canvas.height !== physicalHeight;
122
+
123
+ if (hasNewCanvasSize) {
124
+ canvas.width = physicalWidth;
125
+ canvas.height = physicalHeight;
126
+ }
127
+
128
+ context.setTransform(pixelRatio, 0, 0, pixelRatio, 0, 0);
129
+ context.imageSmoothingEnabled = false;
130
+
131
+ if (
132
+ actor &&
133
+ (hasNewCanvasSize || reducedMotion.matches || animationFrame === null)
134
+ ) {
135
+ drawStaticScene();
136
+ }
137
+ };
138
+
139
+ const getLayout = (): SceneLayout => {
140
+ if (!actor) {
141
+ throw new Error('Missing sprite preview actor');
142
+ }
143
+
144
+ const padding = Math.min(logicalWidth, logicalHeight) * scenePaddingRatio;
145
+ const visibleCenterX = logicalWidth * (projectile ? actorCenterRatio : 0.5);
146
+ const actorAvailableWidth = projectile
147
+ ? (visibleCenterX - padding) * 2
148
+ : logicalWidth - padding * 2;
149
+ const targetFootprint = logicalHeight * scaleReferenceFootprintRatio;
150
+ const sceneScale = getDisplayScale(
151
+ model.scaleReference.width,
152
+ model.scaleReference.height,
153
+ targetFootprint,
154
+ targetFootprint,
155
+ Math.max(1, actorAvailableWidth),
156
+ Math.max(1, logicalHeight - padding * 2),
157
+ );
158
+ const visibleActorWidth =
159
+ actor.model.contentBounds.width * actor.model.baseScale * sceneScale;
160
+ const visibleCenterY = Math.round(logicalHeight / 2);
161
+ const layout: SceneLayout = {
162
+ scale: sceneScale,
163
+ actor: {
164
+ visibleCenterX,
165
+ visibleCenterY,
166
+ },
167
+ };
168
+
169
+ if (projectile) {
170
+ const visibleProjectileWidth =
171
+ projectile.model.contentBounds.width *
172
+ projectile.model.baseScale *
173
+ sceneScale;
174
+
175
+ layout.projectile = {
176
+ startCenterX:
177
+ visibleCenterX + visibleActorWidth / 2 + visibleProjectileWidth / 2,
178
+ visibleCenterY,
179
+ visibleWidth: visibleProjectileWidth,
180
+ };
181
+ }
182
+
183
+ return layout;
184
+ };
185
+
186
+ const clear = (): void => {
187
+ context.clearRect(0, 0, logicalWidth, logicalHeight);
188
+ };
189
+
190
+ const drawStaticScene = (): void => {
191
+ if (!actor) return;
192
+
193
+ clear();
194
+ const layout = getLayout();
195
+ drawAnimation(
196
+ context,
197
+ actor,
198
+ 0,
199
+ layout.actor,
200
+ layout.scale * actor.model.baseScale,
201
+ );
202
+
203
+ if (projectile && layout.projectile) {
204
+ drawAnimation(
205
+ context,
206
+ projectile,
207
+ 0,
208
+ {
209
+ visibleCenterX: layout.projectile.startCenterX,
210
+ visibleCenterY: layout.projectile.visibleCenterY,
211
+ },
212
+ layout.scale * projectile.model.baseScale,
213
+ );
214
+ }
215
+ };
216
+
217
+ const render = (timestamp: number): void => {
218
+ animationFrame = null;
219
+ if (
220
+ isDestroyed ||
221
+ !actor ||
222
+ !isIntersecting ||
223
+ !isDocumentVisible ||
224
+ reducedMotion.matches
225
+ ) {
226
+ return;
227
+ }
228
+
229
+ if (animationStartedAt === 0) animationStartedAt = timestamp;
230
+
231
+ const layout = getLayout();
232
+ clear();
233
+ drawAnimation(
234
+ context,
235
+ actor,
236
+ timestamp - animationStartedAt,
237
+ layout.actor,
238
+ layout.scale * actor.model.baseScale,
239
+ );
240
+
241
+ const projectileModel = model.projectile;
242
+ const loadedProjectile = projectile;
243
+ const projectileLayout = layout.projectile;
244
+
245
+ if (projectileModel && loadedProjectile && projectileLayout) {
246
+ if (lastLaunchAt === null) {
247
+ lastLaunchAt = timestamp - projectileModel.intervalMs;
248
+ }
249
+
250
+ const launches = Math.min(
251
+ maximumCatchUpProjectiles,
252
+ Math.floor((timestamp - lastLaunchAt) / projectileModel.intervalMs),
253
+ );
254
+
255
+ for (let index = 0; index < launches; index += 1) {
256
+ lastLaunchAt += projectileModel.intervalMs;
257
+ activeProjectiles.push({ launchedAt: lastLaunchAt });
258
+ }
259
+
260
+ activeProjectiles = activeProjectiles.filter((activeProjectile) => {
261
+ const elapsed = timestamp - activeProjectile.launchedAt;
262
+ const progress = elapsed / projectileModel.travelDurationMs;
263
+ const travelDistance =
264
+ logicalWidth -
265
+ projectileLayout.startCenterX +
266
+ projectileLayout.visibleWidth / 2;
267
+ const visibleCenterX =
268
+ projectileLayout.startCenterX + travelDistance * progress;
269
+ const isInsideScene = progress < 1;
270
+
271
+ if (isInsideScene) {
272
+ drawAnimation(
273
+ context,
274
+ loadedProjectile,
275
+ elapsed,
276
+ {
277
+ visibleCenterX,
278
+ visibleCenterY: projectileLayout.visibleCenterY,
279
+ },
280
+ layout.scale * loadedProjectile.model.baseScale,
281
+ );
282
+ }
283
+
284
+ return isInsideScene;
285
+ });
286
+ }
287
+
288
+ animationFrame = window.requestAnimationFrame(render);
289
+ };
290
+
291
+ const start = (): void => {
292
+ if (
293
+ animationFrame !== null ||
294
+ !actor ||
295
+ !isIntersecting ||
296
+ !isDocumentVisible ||
297
+ reducedMotion.matches
298
+ ) {
299
+ if (actor && reducedMotion.matches) drawStaticScene();
300
+ return;
301
+ }
302
+
303
+ animationStartedAt = 0;
304
+ lastLaunchAt = null;
305
+ activeProjectiles = [];
306
+ animationFrame = window.requestAnimationFrame(render);
307
+ };
308
+
309
+ const updatePlayback = (): void => {
310
+ if (reducedMotion.matches || !isIntersecting || !isDocumentVisible) {
311
+ stop();
312
+ if (actor) drawStaticScene();
313
+ return;
314
+ }
315
+
316
+ start();
317
+ };
318
+
319
+ const handleVisibilityChange = (): void => {
320
+ isDocumentVisible = !document.hidden;
321
+ updatePlayback();
322
+ };
323
+
324
+ const handleReducedMotionChange = (): void => {
325
+ updatePlayback();
326
+ };
327
+
328
+ const handleIntervalChange = (event: Event): void => {
329
+ const interval = event as CustomEvent<number>;
330
+ if (!model.projectile || interval.detail <= 0) return;
331
+
332
+ model = {
333
+ ...model,
334
+ projectile: {
335
+ ...model.projectile,
336
+ intervalMs: interval.detail,
337
+ },
338
+ };
339
+ lastLaunchAt = null;
340
+ activeProjectiles = [];
341
+ };
342
+
343
+ const resizeObserver =
344
+ 'ResizeObserver' in window ? new ResizeObserver(() => resize()) : null;
345
+ const intersectionObserver =
346
+ 'IntersectionObserver' in window
347
+ ? new IntersectionObserver((entries) => {
348
+ isIntersecting = entries.some((entry) => entry.isIntersecting);
349
+ updatePlayback();
350
+ })
351
+ : null;
352
+ const handleWindowResize = (): void => resize();
353
+
354
+ if (resizeObserver) {
355
+ resizeObserver.observe(root);
356
+ } else {
357
+ window.addEventListener('resize', handleWindowResize);
358
+ }
359
+ intersectionObserver?.observe(root);
360
+ document.addEventListener('visibilitychange', handleVisibilityChange);
361
+ reducedMotion.addEventListener('change', handleReducedMotionChange);
362
+ root.addEventListener(intervalChangeEvent, handleIntervalChange);
363
+ resize();
364
+
365
+ const projectilePromise = model.projectile
366
+ ? loadAnimation(model.projectile).catch(() => null)
367
+ : Promise.resolve(null);
368
+
369
+ void Promise.all([loadAnimation(model.actor), projectilePromise])
370
+ .then(([loadedActor, loadedProjectile]) => {
371
+ if (isDestroyed) return;
372
+
373
+ actor = loadedActor;
374
+ projectile = loadedProjectile;
375
+ resize();
376
+ updatePlayback();
377
+ })
378
+ .catch(() => {
379
+ if (!isDestroyed) root.dataset.spriteProjectilePreviewState = 'error';
380
+ });
381
+
382
+ return () => {
383
+ isDestroyed = true;
384
+ stop();
385
+ resizeObserver?.disconnect();
386
+ intersectionObserver?.disconnect();
387
+ window.removeEventListener('resize', handleWindowResize);
388
+ document.removeEventListener('visibilitychange', handleVisibilityChange);
389
+ reducedMotion.removeEventListener('change', handleReducedMotionChange);
390
+ root.removeEventListener(intervalChangeEvent, handleIntervalChange);
391
+ root.removeAttribute(initializedAttribute);
392
+ delete root.dataset.spriteProjectilePreviewState;
393
+ };
394
+ }
395
+
396
+ function drawAnimation(
397
+ context: CanvasRenderingContext2D,
398
+ animation: LoadedAnimation,
399
+ elapsedMs: number,
400
+ destination: AnimationPlacement,
401
+ scale: number,
402
+ ): void {
403
+ const frame =
404
+ Math.floor(elapsedMs / animation.model.frameDurationMs) %
405
+ animation.model.frames;
406
+ const sourceX = frame * animation.frameWidth;
407
+ const sourceY = animation.model.row * animation.frameHeight;
408
+ const contentBounds = animation.model.contentBounds;
409
+ const destinationX =
410
+ destination.visibleCenterX -
411
+ (contentBounds.x + contentBounds.width / 2) * scale;
412
+ const destinationY =
413
+ destination.visibleCenterY -
414
+ (contentBounds.y + contentBounds.height / 2) * scale;
415
+
416
+ context.drawImage(
417
+ animation.image,
418
+ sourceX,
419
+ sourceY,
420
+ animation.frameWidth,
421
+ animation.frameHeight,
422
+ snapPixelPosition(destinationX, scale),
423
+ snapPixelPosition(destinationY, scale),
424
+ animation.frameWidth * scale,
425
+ animation.frameHeight * scale,
426
+ );
427
+ }
428
+
429
+ function snapPixelPosition(value: number, scale: number): number {
430
+ return Number.isInteger(scale) ? Math.round(value) : value;
431
+ }
432
+
433
+ function getDisplayScale(
434
+ sourceWidth: number,
435
+ sourceHeight: number,
436
+ targetWidth: number,
437
+ targetHeight: number,
438
+ maximumWidth: number,
439
+ maximumHeight: number,
440
+ ): number {
441
+ const targetScale = Math.min(
442
+ targetWidth / sourceWidth,
443
+ targetHeight / sourceHeight,
444
+ );
445
+ const maximumScale = Math.min(
446
+ maximumWidth / sourceWidth,
447
+ maximumHeight / sourceHeight,
448
+ );
449
+
450
+ if (maximumScale < 1) return Math.max(0.1, maximumScale);
451
+
452
+ return Math.min(
453
+ Math.max(1, Math.round(targetScale)),
454
+ Math.floor(maximumScale),
455
+ );
456
+ }
457
+
458
+ async function loadAnimation(
459
+ model: SpriteSheetAnimation,
460
+ ): Promise<LoadedAnimation> {
461
+ const image = new Image();
462
+ image.decoding = 'async';
463
+
464
+ await new Promise<void>((resolve, reject) => {
465
+ image.addEventListener('load', () => resolve(), { once: true });
466
+ image.addEventListener('error', () => reject(new Error(model.src)), {
467
+ once: true,
468
+ });
469
+ image.src = model.src;
470
+ });
471
+
472
+ return {
473
+ image,
474
+ model,
475
+ frameWidth: image.naturalWidth / model.frames,
476
+ frameHeight: image.naturalHeight / model.rows,
477
+ };
478
+ }
479
+
480
+ function readModel(value: string | null | undefined) {
481
+ if (!value) return null;
482
+
483
+ try {
484
+ const candidate: unknown = JSON.parse(value);
485
+ return isPreviewModel(candidate) ? candidate : null;
486
+ } catch {
487
+ return null;
488
+ }
489
+ }
490
+
491
+ function isPreviewModel(value: unknown): value is SpriteProjectilePreviewModel {
492
+ if (!isRecord(value) || typeof value.label !== 'string') return false;
493
+ if (!isScaleReference(value.scaleReference)) return false;
494
+ if (!isAnimation(value.actor)) return false;
495
+
496
+ return value.projectile === undefined || isProjectile(value.projectile);
497
+ }
498
+
499
+ function isProjectile(
500
+ value: unknown,
501
+ ): value is NonNullable<SpriteProjectilePreviewModel['projectile']> {
502
+ if (!isRecord(value)) return false;
503
+
504
+ const { intervalMs, travelDurationMs } = value;
505
+
506
+ return (
507
+ isAnimation(value) &&
508
+ isPositiveNumber(intervalMs) &&
509
+ isPositiveNumber(travelDurationMs)
510
+ );
511
+ }
512
+
513
+ function isScaleReference(value: unknown): boolean {
514
+ return (
515
+ isRecord(value) &&
516
+ isPositiveNumber(value.width) &&
517
+ isPositiveNumber(value.height)
518
+ );
519
+ }
520
+
521
+ function isAnimation(value: unknown): value is SpriteSheetAnimation {
522
+ if (
523
+ !isRecord(value) ||
524
+ !isPositiveNumber(value.width) ||
525
+ !isPositiveNumber(value.height) ||
526
+ !isPositiveNumber(value.baseScale) ||
527
+ !isPositiveInteger(value.frames) ||
528
+ !isPositiveInteger(value.rows)
529
+ ) {
530
+ return false;
531
+ }
532
+
533
+ const frameWidth = value.width / value.frames;
534
+ const frameHeight = value.height / value.rows;
535
+
536
+ return (
537
+ typeof value.src === 'string' &&
538
+ value.src.length > 0 &&
539
+ isNonNegativeInteger(value.row) &&
540
+ value.row < value.rows &&
541
+ isPositiveNumber(value.frameDurationMs) &&
542
+ isContentBounds(value.contentBounds, frameWidth, frameHeight)
543
+ );
544
+ }
545
+
546
+ function isContentBounds(
547
+ value: unknown,
548
+ frameWidth: number,
549
+ frameHeight: number,
550
+ ): boolean {
551
+ if (
552
+ !isRecord(value) ||
553
+ !isNonNegativeNumber(value.x) ||
554
+ !isNonNegativeNumber(value.y) ||
555
+ !isPositiveNumber(value.width) ||
556
+ !isPositiveNumber(value.height)
557
+ ) {
558
+ return false;
559
+ }
560
+
561
+ const geometryTolerance = 0.000001;
562
+ return (
563
+ value.x + value.width <= frameWidth + geometryTolerance &&
564
+ value.y + value.height <= frameHeight + geometryTolerance
565
+ );
566
+ }
567
+
568
+ function isRecord(value: unknown): value is Record<string, unknown> {
569
+ return typeof value === 'object' && value !== null;
570
+ }
571
+
572
+ function isPositiveNumber(value: unknown): value is number {
573
+ return typeof value === 'number' && Number.isFinite(value) && value > 0;
574
+ }
575
+
576
+ function isNonNegativeNumber(value: unknown): value is number {
577
+ return typeof value === 'number' && Number.isFinite(value) && value >= 0;
578
+ }
579
+
580
+ function isPositiveInteger(value: unknown): value is number {
581
+ return isPositiveNumber(value) && Number.isInteger(value);
582
+ }
583
+
584
+ function isNonNegativeInteger(value: unknown): value is number {
585
+ return isNonNegativeNumber(value) && Number.isInteger(value);
586
+ }
@@ -0,0 +1,57 @@
1
+ ---
2
+ import type { HTMLAttributes } from 'astro/types';
3
+ import type { Tone } from '../../theme/contracts';
4
+ import type { StageFlowStep } from '../../display/contracts';
5
+ import StageFlowConnector from './StageFlowConnector.astro';
6
+ import StageFlowStepItem from './StageFlowStep.astro';
7
+
8
+ type Props = HTMLAttributes<'nav'> & {
9
+ label: string;
10
+ steps: readonly StageFlowStep[];
11
+ emptyLabel: string;
12
+ tone?: Tone;
13
+ };
14
+
15
+ const {
16
+ class: className,
17
+ 'data-tone': dataTone,
18
+ label,
19
+ steps,
20
+ emptyLabel,
21
+ tone,
22
+ ...attrs
23
+ }: Props = Astro.props;
24
+ const resolvedTone = tone ?? dataTone;
25
+ ---
26
+
27
+ {
28
+ steps.length > 0 ? (
29
+ <nav
30
+ {...attrs}
31
+ class:list={['stage-flow', className]}
32
+ data-widget="stage-flow"
33
+ data-tone={resolvedTone}
34
+ aria-label={label}
35
+ >
36
+ <div class="stage-flow__viewport">
37
+ <ol class="stage-flow__list">
38
+ {steps.map((step, index) => (
39
+ <>
40
+ {index > 0 ? <StageFlowConnector /> : null}
41
+ <StageFlowStepItem options={step.options} />
42
+ </>
43
+ ))}
44
+ </ol>
45
+ </div>
46
+ </nav>
47
+ ) : (
48
+ <p
49
+ {...attrs}
50
+ class:list={['stage-flow__empty', className]}
51
+ data-widget="stage-flow"
52
+ data-tone={resolvedTone}
53
+ >
54
+ {emptyLabel}
55
+ </p>
56
+ )
57
+ }
@@ -0,0 +1,54 @@
1
+ .stage-flow {
2
+ container-type: inline-size;
3
+ display: grid;
4
+ min-inline-size: 0;
5
+ color: var(--nox-text);
6
+ }
7
+
8
+ .stage-flow__viewport {
9
+ min-inline-size: 0;
10
+ overflow-x: auto;
11
+ overflow-y: hidden;
12
+ overscroll-behavior-inline: contain;
13
+ }
14
+
15
+ .stage-flow__list {
16
+ --stage-flow-stage-min-inline-size: 8rem;
17
+ --stage-flow-media-size: 6.25rem;
18
+ --stage-flow-connector-inline-size: 2.5rem;
19
+ --stage-flow-alternative-inline-size: 1.5rem;
20
+
21
+ display: grid;
22
+ inline-size: max-content;
23
+ min-inline-size: 100%;
24
+ align-items: center;
25
+ justify-content: center;
26
+ grid-auto-columns: max-content;
27
+ grid-auto-flow: column;
28
+ gap: var(--nox-space-3);
29
+ list-style: none;
30
+ margin: 0;
31
+ padding: var(--nox-space-4) var(--nox-space-3);
32
+ }
33
+
34
+ .stage-flow__item {
35
+ display: grid;
36
+ min-inline-size: 0;
37
+ }
38
+
39
+ .stage-flow__empty {
40
+ margin: 0;
41
+ color: var(--nox-text-muted);
42
+ }
43
+
44
+ @container (width < 32rem) {
45
+ .stage-flow__list {
46
+ --stage-flow-stage-min-inline-size: 7.25rem;
47
+ --stage-flow-media-size: 5.5rem;
48
+ --stage-flow-connector-inline-size: 2rem;
49
+ --stage-flow-alternative-inline-size: 1.25rem;
50
+
51
+ padding-inline: var(--nox-space-2);
52
+ gap: var(--nox-space-2);
53
+ }
54
+ }
@@ -0,0 +1,23 @@
1
+ <li class="stage-flow__item stage-flow__item--alternative" aria-hidden="true">
2
+ <svg
3
+ class="stage-flow__symbol stage-flow__alternative"
4
+ viewBox="0 0 20 44"
5
+ fill="none"
6
+ focusable="false"
7
+ >
8
+ <path
9
+ class="stage-flow__symbol-outline"
10
+ d="M14 6.5 6 37.5"
11
+ stroke="currentColor"
12
+ stroke-linecap="round"
13
+ stroke-linejoin="round"
14
+ stroke-width="5.5"></path>
15
+ <path
16
+ class="stage-flow__symbol-line"
17
+ d="M14 6.5 6 37.5"
18
+ stroke="currentColor"
19
+ stroke-linecap="round"
20
+ stroke-linejoin="round"
21
+ stroke-width="2.75"></path>
22
+ </svg>
23
+ </li>