@m3ui-vue/m3ui-vue 1.2.1 → 1.3.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 (269) hide show
  1. package/dist/API.md +2914 -348
  2. package/dist/MButton-CuNzpoxm.js +172 -0
  3. package/dist/MButton-CuNzpoxm.js.map +1 -0
  4. package/dist/MDialog-HxEL-m58.js +173 -0
  5. package/dist/MDialog-HxEL-m58.js.map +1 -0
  6. package/dist/MIconButton-DoNotQ9v.js +282 -0
  7. package/dist/MIconButton-DoNotQ9v.js.map +1 -0
  8. package/dist/MListItem-DnPtv-hY.js +824 -0
  9. package/dist/MListItem-DnPtv-hY.js.map +1 -0
  10. package/dist/MLoadingIndicator-BQMNX6Dx.js +115 -0
  11. package/dist/MLoadingIndicator-BQMNX6Dx.js.map +1 -0
  12. package/dist/MMenuItem-Bih0h4-n.js +604 -0
  13. package/dist/MMenuItem-Bih0h4-n.js.map +1 -0
  14. package/dist/MProgressBar-BCoruxA-.js +184 -0
  15. package/dist/MProgressBar-BCoruxA-.js.map +1 -0
  16. package/dist/MSeekBar-LURE1Ah7.js +435 -0
  17. package/dist/MSeekBar-LURE1Ah7.js.map +1 -0
  18. package/dist/MSegmentedButton-DL9JtJFX.js +65 -0
  19. package/dist/MSegmentedButton-DL9JtJFX.js.map +1 -0
  20. package/dist/MSlider-_J4IS2_z.js +691 -0
  21. package/dist/MSlider-_J4IS2_z.js.map +1 -0
  22. package/dist/MSpinner-BhkMYKOM.js +65 -0
  23. package/dist/MSpinner-BhkMYKOM.js.map +1 -0
  24. package/dist/MTextField-CkzwNBWS.js +296 -0
  25. package/dist/MTextField-CkzwNBWS.js.map +1 -0
  26. package/dist/MTooltip-B67ZDSGp.js +756 -0
  27. package/dist/MTooltip-B67ZDSGp.js.map +1 -0
  28. package/dist/_storageKeys-CYXsUBwK.js +18 -0
  29. package/dist/_storageKeys-CYXsUBwK.js.map +1 -0
  30. package/dist/_useEscapeStack-CjndQhqg.js +19 -0
  31. package/dist/_useEscapeStack-CjndQhqg.js.map +1 -0
  32. package/dist/api.json +1 -1
  33. package/dist/chart.js +1 -1
  34. package/dist/code-editor.js +1 -1
  35. package/dist/components/MAudioPlayer.vue.d.ts +115 -0
  36. package/dist/components/MBadge.vue.d.ts +1 -1
  37. package/dist/components/MBottomSheetScaffold.vue.d.ts +1 -1
  38. package/dist/components/MBox.vue.d.ts +1 -1
  39. package/dist/components/MButton.vue.d.ts +1 -1
  40. package/dist/components/MConfirmDialog.vue.d.ts +1 -1
  41. package/dist/components/MCopyButton.vue.d.ts +2 -2
  42. package/dist/components/MDataTable.vue.d.ts +2 -2
  43. package/dist/components/MDialog.vue.d.ts +2 -2
  44. package/dist/components/MDivider.vue.d.ts +1 -1
  45. package/dist/components/MDragHandle.vue.d.ts +1 -1
  46. package/dist/components/MDrawCanvas.vue.d.ts +148 -0
  47. package/dist/components/MDrawLayers.vue.d.ts +3 -0
  48. package/dist/components/MDrawToolbar.vue.d.ts +34 -0
  49. package/dist/components/MExportButton.vue.d.ts +1 -1
  50. package/dist/components/MFab.vue.d.ts +2 -2
  51. package/dist/components/MFabMenu.vue.d.ts +2 -2
  52. package/dist/components/MFixed.vue.d.ts +1 -1
  53. package/dist/components/MFlex.vue.d.ts +2 -2
  54. package/dist/components/MFloorPlan.vue.d.ts +160 -0
  55. package/dist/components/MFlow.vue.d.ts +305 -0
  56. package/dist/components/MFlowControls.vue.d.ts +33 -0
  57. package/dist/components/MFlowHandle.vue.d.ts +16 -0
  58. package/dist/components/MFlowMinimap.vue.d.ts +22 -0
  59. package/dist/components/MFlowNode.vue.d.ts +35 -0
  60. package/dist/components/MGantt.vue.d.ts +141 -0
  61. package/dist/components/MGanttControls.vue.d.ts +32 -0
  62. package/dist/components/MIconButton.vue.d.ts +3 -1
  63. package/dist/components/MImageCropper.vue.d.ts +110 -0
  64. package/dist/components/MInfiniteScroll.vue.d.ts +1 -1
  65. package/dist/components/MLightbox.vue.d.ts +2 -2
  66. package/dist/components/MList.vue.d.ts +1 -1
  67. package/dist/components/MLoadingOverlay.vue.d.ts +1 -1
  68. package/dist/components/MMap.vue.d.ts +1 -1
  69. package/dist/components/MMapControl.vue.d.ts +1 -1
  70. package/dist/components/MMapSearch.vue.d.ts +1 -1
  71. package/dist/components/MMaskField.vue.d.ts +12 -1
  72. package/dist/components/MMenu.vue.d.ts +1 -1
  73. package/dist/components/MNavigationDrawer.vue.d.ts +1 -1
  74. package/dist/components/MNumberField.vue.d.ts +11 -2
  75. package/dist/components/MPdfViewer.vue.d.ts +174 -0
  76. package/dist/components/MPullToRefresh.vue.d.ts +1 -1
  77. package/dist/components/MResourceTimeline.vue.d.ts +149 -0
  78. package/dist/components/MScheduler.vue.d.ts +3 -3
  79. package/dist/components/MScheme.vue.d.ts +1 -1
  80. package/dist/components/MSearchField.vue.d.ts +1 -1
  81. package/dist/components/MSeekBar.vue.d.ts +1 -1
  82. package/dist/components/MSimpleCarousel.vue.d.ts +1 -1
  83. package/dist/components/MSkeleton.vue.d.ts +1 -1
  84. package/dist/components/MSlider.vue.d.ts +4 -4
  85. package/dist/components/MSparkline.vue.d.ts +1 -1
  86. package/dist/components/MSplitter.vue.d.ts +1 -1
  87. package/dist/components/MStack.vue.d.ts +2 -2
  88. package/dist/components/MSwipeToDismiss.vue.d.ts +1 -1
  89. package/dist/components/MSwitch.vue.d.ts +1 -1
  90. package/dist/components/MTable.vue.d.ts +1 -1
  91. package/dist/components/MTagInput.vue.d.ts +12 -1
  92. package/dist/components/MTextField.vue.d.ts +13 -2
  93. package/dist/components/MToolbar.vue.d.ts +1 -1
  94. package/dist/components/MTree.vue.d.ts +2 -2
  95. package/dist/components/MVideoPlayer.vue.d.ts +253 -0
  96. package/dist/components/MVirtualTable.vue.d.ts +1 -1
  97. package/dist/components/MWindow.vue.d.ts +3 -3
  98. package/dist/components/_MFlowEdge.vue.d.ts +9 -0
  99. package/dist/components/_MFlowEdgeLabel.vue.d.ts +9 -0
  100. package/dist/components/_MFlowNodeShell.vue.d.ts +9 -0
  101. package/dist/components/_MPdfPage.vue.d.ts +76 -0
  102. package/dist/components/_MTimeBackground.vue.d.ts +14 -0
  103. package/dist/components/_MTimeHeader.vue.d.ts +20 -0
  104. package/dist/composables/_effectCanvas.d.ts +23 -0
  105. package/dist/composables/_flowPanel.d.ts +2 -0
  106. package/dist/composables/_flowSlot.d.ts +5 -0
  107. package/dist/composables/_ganttTime.d.ts +41 -0
  108. package/dist/composables/_storageKeys.d.ts +9 -0
  109. package/dist/composables/_useTimeAxis.d.ts +90 -0
  110. package/dist/composables/drawGeometry.d.ts +119 -0
  111. package/dist/composables/flowFormats.d.ts +79 -0
  112. package/dist/composables/flowLayout.d.ts +45 -0
  113. package/dist/composables/flowPaths.d.ts +53 -0
  114. package/dist/composables/ganttFormats.d.ts +52 -0
  115. package/dist/composables/ganttSchedule.d.ts +99 -0
  116. package/dist/composables/themeInitScript.d.ts +2 -0
  117. package/dist/composables/useConfetti.d.ts +19 -8
  118. package/dist/composables/useDrawing.d.ts +44 -0
  119. package/dist/composables/useFireworks.d.ts +37 -0
  120. package/dist/composables/useFlow.d.ts +266 -0
  121. package/dist/composables/useGantt.d.ts +87 -0
  122. package/dist/composables/useLocale.d.ts +163 -0
  123. package/dist/composables/useMediaPlayer.d.ts +101 -0
  124. package/dist/composables/usePdf.d.ts +119 -0
  125. package/dist/composables/useReactions.d.ts +32 -0
  126. package/dist/composables/useResourceTimeline.d.ts +70 -0
  127. package/dist/composables/viewportHints.d.ts +2 -0
  128. package/dist/directives/vSparkle.d.ts +27 -0
  129. package/dist/draw.d.ts +7 -0
  130. package/dist/draw.js +1700 -0
  131. package/dist/draw.js.map +1 -0
  132. package/dist/flow.d.ts +14 -0
  133. package/dist/flow.js +3542 -0
  134. package/dist/flow.js.map +1 -0
  135. package/dist/flowPaths-CUc6X373.js +161 -0
  136. package/dist/flowPaths-CUc6X373.js.map +1 -0
  137. package/dist/gantt.d.ts +12 -0
  138. package/dist/gantt.js +3575 -0
  139. package/dist/gantt.js.map +1 -0
  140. package/dist/index.d.ts +11 -1
  141. package/dist/locales/index.js +1148 -7
  142. package/dist/locales/index.js.map +1 -1
  143. package/dist/m3ui-vue.css +1 -1
  144. package/dist/m3ui.js +7822 -7809
  145. package/dist/m3ui.js.map +1 -1
  146. package/dist/map.js +593 -588
  147. package/dist/map.js.map +1 -1
  148. package/dist/media.d.ts +5 -0
  149. package/dist/media.js +1311 -0
  150. package/dist/media.js.map +1 -0
  151. package/dist/pdf.d.ts +4 -0
  152. package/dist/pdf.js +1871 -0
  153. package/dist/pdf.js.map +1 -0
  154. package/dist/plugin.d.ts +10 -0
  155. package/dist/rich-text-editor.js +28 -26
  156. package/dist/rich-text-editor.js.map +1 -1
  157. package/dist/styles.css +1 -1
  158. package/dist/{useLocale-JYxDDeXC.js → useLocale-BpGxplLV.js} +165 -2
  159. package/dist/useLocale-BpGxplLV.js.map +1 -0
  160. package/dist/{useThemeTokens-Bv-Uv8eX.js → useThemeTokens-DQQcaWbE.js} +41 -40
  161. package/dist/useThemeTokens-DQQcaWbE.js.map +1 -0
  162. package/package.json +29 -5
  163. package/src/__tests__/MAudioPlayer.test.ts +75 -0
  164. package/src/__tests__/MDrawCanvas.test.ts +110 -0
  165. package/src/__tests__/MFloorPlan.test.ts +97 -0
  166. package/src/__tests__/MFlow.test.ts +575 -0
  167. package/src/__tests__/MGantt.test.ts +130 -0
  168. package/src/__tests__/MImageCropper.test.ts +89 -0
  169. package/src/__tests__/MPdfViewer.test.ts +235 -0
  170. package/src/__tests__/MResourceTimeline.test.ts +102 -0
  171. package/src/__tests__/MSlider.test.ts +36 -0
  172. package/src/__tests__/MVideoPlayer.test.ts +201 -0
  173. package/src/__tests__/drawGeometry.test.ts +58 -0
  174. package/src/__tests__/fieldExpose.test.ts +30 -0
  175. package/src/__tests__/flowFormats.test.ts +185 -0
  176. package/src/__tests__/flowLayout.test.ts +113 -0
  177. package/src/__tests__/ganttFormats.test.ts +76 -0
  178. package/src/__tests__/ganttSchedule.test.ts +136 -0
  179. package/src/__tests__/pdfMock.ts +83 -0
  180. package/src/__tests__/plugin.test.ts +56 -1
  181. package/src/__tests__/ssr.test.ts +1 -1
  182. package/src/__tests__/themeInitScript.test.ts +10 -0
  183. package/src/__tests__/useConfetti.test.ts +33 -2
  184. package/src/__tests__/useFireworks.test.ts +104 -0
  185. package/src/__tests__/useMediaPlayer.test.ts +182 -0
  186. package/src/__tests__/usePdf.test.ts +102 -0
  187. package/src/__tests__/useReactions.test.ts +51 -0
  188. package/src/__tests__/vSparkle.test.ts +83 -0
  189. package/src/components/MAudioPlayer.vue +319 -0
  190. package/src/components/MDrawCanvas.vue +813 -0
  191. package/src/components/MDrawLayers.vue +114 -0
  192. package/src/components/MDrawToolbar.vue +171 -0
  193. package/src/components/MEmojiSelector.vue +3 -2
  194. package/src/components/MFloorPlan.vue +581 -0
  195. package/src/components/MFlow.vue +2049 -0
  196. package/src/components/MFlowControls.vue +110 -0
  197. package/src/components/MFlowHandle.vue +60 -0
  198. package/src/components/MFlowMinimap.vue +134 -0
  199. package/src/components/MFlowNode.vue +98 -0
  200. package/src/components/MGantt.vue +1609 -0
  201. package/src/components/MGanttControls.vue +77 -0
  202. package/src/components/MIconButton.vue +3 -1
  203. package/src/components/MImageCropper.vue +507 -0
  204. package/src/components/MMap.vue +3 -2
  205. package/src/components/MMaskField.vue +13 -0
  206. package/src/components/MNumberField.vue +13 -0
  207. package/src/components/MPdfViewer.vue +1269 -0
  208. package/src/components/MResourceTimeline.vue +1020 -0
  209. package/src/components/MSlider.vue +22 -4
  210. package/src/components/MTagInput.vue +13 -0
  211. package/src/components/MTextField.vue +24 -4
  212. package/src/components/MVideoPlayer.vue +839 -0
  213. package/src/components/_MFlowEdge.vue +100 -0
  214. package/src/components/_MFlowEdgeLabel.vue +56 -0
  215. package/src/components/_MFlowNodeShell.vue +111 -0
  216. package/src/components/_MPdfPage.vue +456 -0
  217. package/src/components/_MTimeBackground.vue +16 -0
  218. package/src/components/_MTimeHeader.vue +31 -0
  219. package/src/composables/_colorUtils.ts +4 -2
  220. package/src/composables/_effectCanvas.ts +109 -0
  221. package/src/composables/_flowPanel.ts +13 -0
  222. package/src/composables/_flowSlot.ts +6 -0
  223. package/src/composables/_ganttTime.ts +200 -0
  224. package/src/composables/_ssrState.ts +2 -2
  225. package/src/composables/_storageKeys.ts +28 -0
  226. package/src/composables/_useTimeAxis.ts +224 -0
  227. package/src/composables/drawGeometry.ts +405 -0
  228. package/src/composables/flowFormats.ts +916 -0
  229. package/src/composables/flowLayout.ts +297 -0
  230. package/src/composables/flowPaths.ts +239 -0
  231. package/src/composables/ganttFormats.ts +366 -0
  232. package/src/composables/ganttSchedule.ts +403 -0
  233. package/src/composables/themeInitScript.ts +5 -2
  234. package/src/composables/useColorPalette.ts +36 -8
  235. package/src/composables/useConfetti.ts +84 -107
  236. package/src/composables/useDrawing.ts +53 -0
  237. package/src/composables/useFireworks.ts +360 -0
  238. package/src/composables/useFlow.ts +250 -0
  239. package/src/composables/useGantt.ts +92 -0
  240. package/src/composables/useLocale.ts +328 -0
  241. package/src/composables/useMediaPlayer.ts +406 -0
  242. package/src/composables/usePdf.ts +456 -0
  243. package/src/composables/useReactions.ts +131 -0
  244. package/src/composables/useResourceTimeline.ts +76 -0
  245. package/src/composables/useTheme.ts +21 -4
  246. package/src/composables/viewportHints.ts +6 -5
  247. package/src/directives/vSparkle.ts +180 -0
  248. package/src/draw.ts +25 -0
  249. package/src/flow.ts +26 -0
  250. package/src/gantt.ts +12 -0
  251. package/src/index.ts +11 -1
  252. package/src/locales/de.ts +163 -0
  253. package/src/locales/es.ts +163 -0
  254. package/src/locales/fr.ts +163 -0
  255. package/src/locales/ja.ts +163 -0
  256. package/src/locales/ko.ts +163 -0
  257. package/src/locales/pt.ts +163 -0
  258. package/src/locales/zh.ts +163 -0
  259. package/src/media.ts +5 -0
  260. package/src/pdf.ts +15 -0
  261. package/src/plugin.ts +21 -5
  262. package/dist/MMenuItem-DI351kIf.js +0 -929
  263. package/dist/MMenuItem-DI351kIf.js.map +0 -1
  264. package/dist/MSlider-BnEy5ieZ.js +0 -1500
  265. package/dist/MSlider-BnEy5ieZ.js.map +0 -1
  266. package/dist/MTextField-BBmkaIbO.js +0 -633
  267. package/dist/MTextField-BBmkaIbO.js.map +0 -1
  268. package/dist/useLocale-JYxDDeXC.js.map +0 -1
  269. package/dist/useThemeTokens-Bv-Uv8eX.js.map +0 -1
@@ -1,8 +1,9 @@
1
1
  // Confetti: a burst of Material 3's expressive shapes (MShape's) in the theme's colors, for the end of a flow (an
2
- // order placed, a task done). One shared canvas over the page, created on the first burst and removed when the last
3
- // piece has fallen; it never takes pointer events and assistive technology ignores it. Simple physics (gravity, air
4
- // drag, spin and a flip that flattens each piece as it turns) in a rAF loop. Nothing happens under reduced motion.
2
+ // order placed, a task done). Drawn on the effects' shared canvas (_effectCanvas), which goes away when the last
3
+ // piece has fallen. Simple physics (gravity, air drag, spin and a flip that flattens each piece as it turns). Nothing
4
+ // happens under reduced motion.
5
5
  import { getShape, toSvgPath, type ShapeName } from 'shape-morph'
6
+ import { __resetEffectCanvas, originPoint, reducedMotion, runLayer, themeColors, type EffectLayer, type EffectOrigin } from './_effectCanvas'
6
7
 
7
8
  export interface ConfettiOptions {
8
9
  /** How many pieces (default 80; 160 for `from: 'top'`) */
@@ -21,10 +22,18 @@ export interface ConfettiOptions {
21
22
  velocity?: number
22
23
  /** Piece size in px (default 14) */
23
24
  size?: number
25
+ /**
26
+ * Keep it going for this long, in ms: a rain that keeps falling (`from: 'top'`) or a fountain from the point,
27
+ * `count` then being pieces per second. `Infinity` runs until the returned `stop()`. Unset: one burst
28
+ */
29
+ duration?: number
24
30
  }
25
31
 
32
+ /** Stops a running confetti before its `duration` ends; the pieces already out finish falling. */
33
+ export type StopConfetti = () => void
34
+
26
35
  /** Where a burst starts: the pointer of an event, an element's center, or a point in viewport pixels */
27
- export type ConfettiOrigin = MouseEvent | PointerEvent | Element | { x: number; y: number }
36
+ export type ConfettiOrigin = EffectOrigin
28
37
 
29
38
  const DEFAULT_SHAPES: ShapeName[] = ['Cookie4Sided', 'Sunny', 'Clover4Leaf', 'Heart', 'Pill', 'Triangle', 'SoftBurst', 'Circle', 'Gem', 'Flower']
30
39
  const ROLES = ['primary', 'secondary', 'tertiary', 'primary-container', 'tertiary-container']
@@ -58,117 +67,73 @@ function pathFor(shape: ShapeName) {
58
67
  return path
59
68
  }
60
69
 
61
- let canvas: HTMLCanvasElement | null = null
62
- let ctx: CanvasRenderingContext2D | null = null
63
- let pieces: Piece[] = []
64
- let raf = 0
65
- let last = 0
66
-
67
- function ensureCanvas() {
68
- if (canvas) return ctx
69
- canvas = document.createElement('canvas')
70
- canvas.setAttribute('aria-hidden', 'true')
71
- canvas.style.cssText = 'position:fixed;inset:0;width:100vw;height:100vh;pointer-events:none;z-index:2147483000'
72
- document.body.appendChild(canvas)
73
- ctx = canvas.getContext('2d')
74
- if (!ctx) {
75
- teardown()
76
- return null
77
- }
78
- resize()
79
- window.addEventListener('resize', resize)
80
- return ctx
81
- }
82
-
83
- function resize() {
84
- if (!canvas) return
85
- const dpr = window.devicePixelRatio || 1
86
- canvas.width = Math.round(window.innerWidth * dpr)
87
- canvas.height = Math.round(window.innerHeight * dpr)
88
- }
89
-
90
- function teardown() {
91
- cancelAnimationFrame(raf)
92
- raf = 0
93
- window.removeEventListener('resize', resize)
94
- canvas?.remove()
95
- canvas = null
96
- ctx = null
97
- pieces = []
70
+ // A confetti with a `duration`: it adds `rate` pieces per second (fractions carried over) until `until`
71
+ interface Emitter {
72
+ until: number
73
+ rate: number
74
+ carry: number
75
+ spawn: () => Piece
98
76
  }
99
77
 
100
- function frame(now: number) {
101
- const dt = Math.min((now - last) / 1000, 1 / 30)
102
- last = now
103
- const height = window.innerHeight
104
- pieces = pieces.filter((p) => p.age < p.life && p.y < height + 60)
105
- if (!pieces.length || !ctx || !canvas) {
106
- teardown()
107
- return
108
- }
109
- const dpr = canvas.width / window.innerWidth
110
- ctx.setTransform(1, 0, 0, 1, 0, 0)
111
- ctx.clearRect(0, 0, canvas.width, canvas.height)
112
- for (const p of pieces) {
113
- p.age += dt
114
- p.vx -= p.vx * DRAG * dt
115
- p.vy += (GRAVITY - p.vy * DRAG) * dt
116
- p.x += p.vx * dt
117
- p.y += p.vy * dt
118
- p.rot += p.spin * dt
119
- p.flip += p.flipSpeed * dt
120
- const fade = Math.min(1, (p.life - p.age) / 0.6)
121
- ctx.globalAlpha = Math.max(0, fade)
122
- ctx.fillStyle = p.color
123
- const scale = (p.size / SIZE) * dpr
124
- ctx.setTransform(scale * Math.cos(p.rot), scale * Math.sin(p.rot), -scale * Math.sin(p.rot) * Math.cos(p.flip), scale * Math.cos(p.rot) * Math.cos(p.flip), p.x * dpr, p.y * dpr)
125
- ctx.translate(-SIZE / 2, -SIZE / 2)
126
- ctx.fill(p.path)
127
- }
128
- raf = requestAnimationFrame(frame)
129
- }
78
+ let pieces: Piece[] = []
79
+ let emitters: Emitter[] = []
130
80
 
131
- function originPoint(origin: ConfettiOrigin | undefined): { x: number; y: number; el: Element } {
132
- if (origin && 'clientX' in origin) {
133
- const el = (origin.currentTarget as Element | null) ?? (origin.target as Element | null) ?? document.documentElement
134
- // A click from the keyboard (Enter, Space) has no pointer position: burst from the element instead
135
- if (origin.detail === 0 && origin.clientX === 0 && origin.clientY === 0) return originPoint(el)
136
- return { x: origin.clientX, y: origin.clientY, el }
137
- }
138
- if (origin instanceof Element) {
139
- const r = origin.getBoundingClientRect()
140
- return { x: r.left + r.width / 2, y: r.top + r.height / 2, el: origin }
141
- }
142
- if (origin) return { ...origin, el: document.documentElement }
143
- return { x: window.innerWidth / 2, y: window.innerHeight * 0.6, el: document.documentElement }
81
+ const layer: EffectLayer = {
82
+ step(ctx, dt, dpr, now) {
83
+ const height = window.innerHeight
84
+ emitters = emitters.filter((e) => e.until > now)
85
+ for (const e of emitters) {
86
+ e.carry += e.rate * dt
87
+ for (; e.carry >= 1; e.carry--) pieces.push(e.spawn())
88
+ }
89
+ pieces = pieces.filter((p) => p.age < p.life && p.y < height + 60)
90
+ for (const p of pieces) {
91
+ p.age += dt
92
+ p.vx -= p.vx * DRAG * dt
93
+ p.vy += (GRAVITY - p.vy * DRAG) * dt
94
+ p.x += p.vx * dt
95
+ p.y += p.vy * dt
96
+ p.rot += p.spin * dt
97
+ p.flip += p.flipSpeed * dt
98
+ const fade = Math.min(1, (p.life - p.age) / 0.6)
99
+ ctx.globalAlpha = Math.max(0, fade)
100
+ ctx.fillStyle = p.color
101
+ const scale = (p.size / SIZE) * dpr
102
+ ctx.setTransform(scale * Math.cos(p.rot), scale * Math.sin(p.rot), -scale * Math.sin(p.rot) * Math.cos(p.flip), scale * Math.cos(p.rot) * Math.cos(p.flip), p.x * dpr, p.y * dpr)
103
+ ctx.translate(-SIZE / 2, -SIZE / 2)
104
+ ctx.fill(p.path)
105
+ }
106
+ return pieces.length > 0 || emitters.length > 0
107
+ },
144
108
  }
145
109
 
146
- function themeColors(el: Element) {
147
- const style = getComputedStyle(el)
148
- const colors = ROLES.map((role) => style.getPropertyValue(`--color-${role}`).trim()).filter(Boolean)
149
- return colors.length ? colors : ['#6750a4', '#625b71', '#7d5260', '#eaddff', '#ffd8e4']
150
- }
151
110
 
152
- /** Fires a burst of confetti. Browser only; does nothing under reduced motion. */
153
- export function fireConfetti(origin?: ConfettiOrigin, options: ConfettiOptions = {}) {
154
- if (typeof window === 'undefined') return
155
- if (window.matchMedia?.('(prefers-reduced-motion: reduce)').matches) return
111
+ /**
112
+ * Fires confetti: one burst, or with `duration` a rain or fountain that keeps going (stop it early with the returned
113
+ * function). Browser only; does nothing under reduced motion.
114
+ */
115
+ export function fireConfetti(origin?: ConfettiOrigin, options: ConfettiOptions = {}): StopConfetti {
116
+ const noop = () => {}
117
+ if (typeof window === 'undefined') return noop
118
+ if (reducedMotion()) return noop
156
119
  const top = options.from === 'top'
157
120
  const point = originPoint(origin)
158
- const colors = options.colors?.length ? options.colors : themeColors(point.el)
121
+ const colors = options.colors?.length ? options.colors : themeColors(point.el, ROLES)
159
122
  const shapes = options.shapes?.length ? options.shapes : DEFAULT_SHAPES
160
123
  const count = options.count ?? (top ? 160 : 80)
161
124
  const spread = ((options.spread ?? 70) * Math.PI) / 180
162
125
  const angle = ((options.angle ?? 0) * Math.PI) / 180
163
126
  const speed = options.velocity ?? 950
164
127
  const size = options.size ?? 14
165
- if (!ensureCanvas()) return
166
- for (let i = 0; i < count; i++) {
128
+ // A rain with a duration starts its pieces just above the screen (they keep coming), a single one spread over a
129
+ // band above it (they all come at once)
130
+ const band = options.duration ? 0.05 : 0.4
131
+ const spawn = (): Piece => {
167
132
  const dir = angle + (Math.random() - 0.5) * spread
168
133
  const v = top ? 100 + Math.random() * 200 : speed * (0.55 + Math.random() * 0.6)
169
- pieces.push({
134
+ return {
170
135
  x: top ? Math.random() * window.innerWidth : point.x,
171
- y: top ? -20 - Math.random() * window.innerHeight * 0.4 : point.y,
136
+ y: top ? -20 - Math.random() * window.innerHeight * band : point.y,
172
137
  vx: top ? (Math.random() - 0.5) * 200 : Math.sin(dir) * v,
173
138
  vy: top ? v : -Math.cos(dir) * v,
174
139
  rot: Math.random() * Math.PI * 2,
@@ -180,25 +145,37 @@ export function fireConfetti(origin?: ConfettiOrigin, options: ConfettiOptions =
180
145
  color: colors[Math.floor(Math.random() * colors.length)]!,
181
146
  age: 0,
182
147
  life: top ? 4 + Math.random() * 1.5 : 2.4 + Math.random() * 1.2,
183
- })
148
+ }
184
149
  }
185
- if (!raf) {
186
- last = performance.now()
187
- raf = requestAnimationFrame(frame)
150
+ if (!runLayer(layer)) return noop
151
+ let emitter: Emitter | null = null
152
+ if (options.duration && options.duration > 0) {
153
+ emitter = { until: performance.now() + options.duration, rate: count, carry: 0, spawn }
154
+ emitters.push(emitter)
155
+ } else {
156
+ for (let i = 0; i < count; i++) pieces.push(spawn())
157
+ }
158
+ return () => {
159
+ if (emitter) emitter.until = 0
188
160
  }
189
161
  }
190
162
 
191
- /** Confetti for a component: `fire(event)` from the pointer, `fire(el)` from an element, `fire({ from: 'top' })`. */
163
+ /**
164
+ * Confetti for a component: `fire(event)` from the pointer, `fire(el)` from an element, `fire({ from: 'top' })`,
165
+ * `fire({ from: 'top', duration: 5000 })` for a rain that lasts. Returns a function that stops it early.
166
+ */
192
167
  export function useConfetti() {
193
- function fire(origin?: ConfettiOrigin | ConfettiOptions, options?: ConfettiOptions) {
168
+ function fire(origin?: ConfettiOrigin | ConfettiOptions, options?: ConfettiOptions): StopConfetti {
194
169
  const isOrigin = origin instanceof Event || (typeof Element !== 'undefined' && origin instanceof Element) || (origin && 'x' in origin && 'y' in origin && !('from' in origin))
195
- if (isOrigin) fireConfetti(origin as ConfettiOrigin, options)
196
- else fireConfetti(undefined, { ...(origin as ConfettiOptions | undefined), ...options })
170
+ if (isOrigin) return fireConfetti(origin as ConfettiOrigin, options)
171
+ return fireConfetti(undefined, { ...(origin as ConfettiOptions | undefined), ...options })
197
172
  }
198
173
  return { fire }
199
174
  }
200
175
 
201
176
  /** Stops every burst and removes the canvas. Internal, for tests; not exported from index.ts. */
202
177
  export function __resetConfetti() {
203
- teardown()
178
+ pieces = []
179
+ emitters = []
180
+ __resetEffectCanvas()
204
181
  }
@@ -0,0 +1,53 @@
1
+ import { inject, type ComputedRef, type InjectionKey, type Ref } from 'vue'
2
+ import type { DrawItem, DrawLayer, DrawPen, DrawShapeKind } from './drawGeometry'
3
+
4
+ /** What a pointer does on an MDrawCanvas */
5
+ export type DrawTool = DrawPen | 'eraser' | 'stroke-eraser' | DrawShapeKind | 'text' | 'select' | 'pan'
6
+
7
+ export interface DrawExportOptions {
8
+ /** In `mode="pages"`: the page to export (default 0) */
9
+ page?: number
10
+ /** PNG: pixels per drawing unit (default 2) */
11
+ scale?: number
12
+ /** Paint the paper color behind the drawing (default true) */
13
+ background?: boolean
14
+ }
15
+
16
+ /** What an MDrawCanvas shares with MDrawToolbar, MDrawLayers and anything built with useDrawing() inside it */
17
+ export interface DrawContext {
18
+ tool: Ref<DrawTool>
19
+ color: Ref<string>
20
+ size: Ref<number>
21
+ /** New rects and ellipses are filled with the color */
22
+ fill: Ref<boolean>
23
+ items: ComputedRef<DrawItem[]>
24
+ selection: Ref<string[]>
25
+ layers: ComputedRef<DrawLayer[]>
26
+ setLayers(layers: DrawLayer[]): void
27
+ /** The layer new items go on */
28
+ layer: Ref<string | undefined>
29
+ mode: ComputedRef<'infinite' | 'pages'>
30
+ pages: Ref<number>
31
+ addPage(): void
32
+ zoom: ComputedRef<number>
33
+ zoomIn(): void
34
+ zoomOut(): void
35
+ fitView(): void
36
+ canUndo: ComputedRef<boolean>
37
+ canRedo: ComputedRef<boolean>
38
+ undo(): void
39
+ redo(): void
40
+ clear(): void
41
+ deleteSelection(): void
42
+ exportSvg(opts?: DrawExportOptions): string
43
+ exportPng(opts?: DrawExportOptions): Promise<Blob>
44
+ }
45
+
46
+ export const DRAW_KEY: InjectionKey<DrawContext> = Symbol('m3-draw')
47
+
48
+ /** The surrounding MDrawCanvas' state and actions, for a toolbar or panel of your own inside it */
49
+ export function useDrawing(): DrawContext {
50
+ const ctx = inject(DRAW_KEY, null)
51
+ if (!ctx) throw new Error('useDrawing() must be called inside <MDrawCanvas>')
52
+ return ctx
53
+ }
@@ -0,0 +1,360 @@
1
+ // Fireworks: rockets rise from the bottom of the screen and burst, in the theme's colors. A classic burst (a peony)
2
+ // throws its sparks out from the center in every direction, trailing streaks, then drag slows them and gravity pulls
3
+ // them down as they flicker out: simulated. A shaped one lays its sparks out along one of Material 3's expressive
4
+ // shapes (MShape's: a ring, a heart, a flower…): kinematic, so the shape stays readable while it opens and sags. Drawn
5
+ // on the effects' shared canvas (_effectCanvas) with useConfetti. Nothing happens under reduced motion.
6
+ import { getShape, type ShapeName } from 'shape-morph'
7
+ import { __resetEffectCanvas, reducedMotion, runLayer, themeColors, type EffectLayer } from './_effectCanvas'
8
+
9
+ export interface FireworksOptions {
10
+ /** How many rockets (default 6, launched over about a second and a half); with `duration`, rockets per second */
11
+ count?: number
12
+ /**
13
+ * What the bursts look like, one picked per burst: `'classic'` (sparks thrown out from the center that fall as they
14
+ * fade) or an MShape's name the sparks are laid out along (`'Circle'` is a ring). Default: mostly classic, with a
15
+ * ring, a heart, a flower… now and then
16
+ */
17
+ shapes?: FireworkShape[]
18
+ /** Any CSS colors. Default: the theme's primary, secondary and tertiary roles and their containers */
19
+ colors?: string[]
20
+ /** Burst radius in px. Default: a sixth of the screen's smaller side, between 70 and 180 */
21
+ size?: number
22
+ /** Keep launching for this long, in ms (`count` then being rockets per second); `Infinity` runs until `stop()` */
23
+ duration?: number
24
+ }
25
+
26
+ /** A burst's look: `'classic'`, or an MShape's name its sparks are laid out along. */
27
+ export type FireworkShape = 'classic' | ShapeName
28
+
29
+ /** Stops launching before `duration` ends; what's already in the air finishes. */
30
+ export type StopFireworks = () => void
31
+
32
+ const DEFAULT_SHAPES: FireworkShape[] = ['classic', 'classic', 'classic', 'classic', 'classic', 'classic', 'Circle', 'Heart', 'Flower', 'Sunny', 'Clover4Leaf']
33
+ const ROLES = ['primary', 'secondary', 'tertiary', 'primary-container', 'tertiary-container']
34
+ const SPARKS = 64
35
+ const CLASSIC_SPARKS = 90
36
+ // Sparks are slowed by DRAG (1/s) and pulled by GRAVITY (px/s²); a shaped burst instead opens over EXPAND seconds
37
+ // (ease out) while sagging SAG px/s². Every spark draws a streak through its last TAIL positions, kept every
38
+ // TAIL_STEP seconds whatever the frame rate (so ~0.27s of it); a rocket, its last TRAIL
39
+ const DRAG = 2.2
40
+ const GRAVITY = 110
41
+ const EXPAND = 0.9
42
+ const SAG = 70
43
+ const TAIL = 17
44
+ const TAIL_STEP = 1 / 60
45
+ const TRAIL = 10
46
+
47
+ interface Rocket {
48
+ x0: number
49
+ y0: number
50
+ x1: number
51
+ y1: number
52
+ flight: number
53
+ age: number
54
+ trail: number[]
55
+ color: string
56
+ burst: () => void
57
+ }
58
+
59
+ interface Burst {
60
+ cx: number
61
+ cy: number
62
+ radius: number
63
+ /** A shaped burst's sparks, as offsets from the center (see shapeOutline); a classic one has none */
64
+ offsets: Float32Array | null
65
+ x: Float32Array
66
+ y: Float32Array
67
+ vx: Float32Array
68
+ vy: Float32Array
69
+ /** The last TAIL positions of every spark, oldest first, the newest being where it is now: `[spark][step][x, y]` */
70
+ tail: Float32Array
71
+ /** Time since the tail last moved up a step */
72
+ tailClock: number
73
+ colors: string[]
74
+ age: number
75
+ life: number
76
+ }
77
+
78
+ interface Pending {
79
+ at: number
80
+ launch: () => void
81
+ }
82
+
83
+ const outlines = new Map<string, Float32Array>()
84
+
85
+ /**
86
+ * `n` points evenly spaced along a shape's outline, as offsets from its center scaled so the farthest is at 1
87
+ * (`[x0, y0, x1, y1, …]`). Exported for tests.
88
+ */
89
+ export function shapeOutline(shape: ShapeName, n = SPARKS): Float32Array {
90
+ const key = `${shape}:${n}`
91
+ let out = outlines.get(key)
92
+ if (out) return out
93
+ // The outline as a polyline (each cubic sampled), then n points at equal distances along it
94
+ const pts: number[] = []
95
+ for (const cubic of getShape(shape).cubics) {
96
+ const p = cubic.points
97
+ for (let i = 0; i < 16; i++) {
98
+ const t = i / 16
99
+ const u = 1 - t
100
+ const a = u * u * u, b = 3 * u * u * t, c = 3 * u * t * t, d = t * t * t
101
+ pts.push(a * p[0]! + b * p[2]! + c * p[4]! + d * p[6]!, a * p[1]! + b * p[3]! + c * p[5]! + d * p[7]!)
102
+ }
103
+ }
104
+ const count = pts.length / 2
105
+ const lengths = [0]
106
+ for (let i = 1; i <= count; i++) {
107
+ const j = i % count
108
+ lengths.push(lengths[i - 1]! + Math.hypot(pts[j * 2]! - pts[(i - 1) * 2]!, pts[j * 2 + 1]! - pts[(i - 1) * 2 + 1]!))
109
+ }
110
+ const total = lengths[count]!
111
+ // Centered on its bounding box (sampling isn't even along the outline, so an average would lean)
112
+ let minX = Infinity, maxX = -Infinity, minY = Infinity, maxY = -Infinity
113
+ for (let i = 0; i < count; i++) {
114
+ minX = Math.min(minX, pts[i * 2]!); maxX = Math.max(maxX, pts[i * 2]!)
115
+ minY = Math.min(minY, pts[i * 2 + 1]!); maxY = Math.max(maxY, pts[i * 2 + 1]!)
116
+ }
117
+ const cx = (minX + maxX) / 2
118
+ const cy = (minY + maxY) / 2
119
+ out = new Float32Array(n * 2)
120
+ let seg = 0
121
+ let max = 0
122
+ for (let k = 0; k < n; k++) {
123
+ const target = (k / n) * total
124
+ while (lengths[seg + 1]! < target) seg++
125
+ const f = (target - lengths[seg]!) / (lengths[seg + 1]! - lengths[seg]! || 1)
126
+ const j = (seg + 1) % count
127
+ const x = pts[seg * 2]! + (pts[j * 2]! - pts[seg * 2]!) * f - cx
128
+ const y = pts[seg * 2 + 1]! + (pts[j * 2 + 1]! - pts[seg * 2 + 1]!) * f - cy
129
+ out[k * 2] = x
130
+ out[k * 2 + 1] = y
131
+ max = Math.max(max, Math.hypot(x, y))
132
+ }
133
+ for (let i = 0; i < out.length; i++) out[i]! /= max || 1
134
+ outlines.set(key, out)
135
+ return out
136
+ }
137
+
138
+ let rockets: Rocket[] = []
139
+ let bursts: Burst[] = []
140
+ let pending: Pending[] = []
141
+ let emitters: { until: number; rate: number; carry: number; launch: () => void }[] = []
142
+
143
+ const easeOut = (t: number) => 1 - (1 - t) ** 3
144
+
145
+ function createBurst(cx: number, cy: number, radius: number, colors: string[], offsets: Float32Array | null): Burst {
146
+ const n = offsets ? offsets.length / 2 : CLASSIC_SPARKS
147
+ const b: Burst = {
148
+ cx, cy, radius, colors, offsets, age: 0,
149
+ life: offsets ? 1.9 * (0.85 + Math.random() * 0.3) : 2 + Math.random() * 0.6,
150
+ x: new Float32Array(n).fill(cx),
151
+ y: new Float32Array(n).fill(cy),
152
+ vx: new Float32Array(n),
153
+ vy: new Float32Array(n),
154
+ tail: new Float32Array(n * TAIL * 2),
155
+ tailClock: 0,
156
+ }
157
+ for (let i = 0; i < n; i++) {
158
+ if (!offsets) {
159
+ // A sphere of sparks seen from the front: denser toward the rim, some flying toward or away from us. With drag
160
+ // alone a spark stops speed / DRAG away, so that's the radius
161
+ const angle = Math.random() * Math.PI * 2
162
+ const depth = Math.random() * 2 - 1
163
+ const speed = radius * DRAG * Math.sqrt(1 - depth * depth) * (0.92 + Math.random() * 0.12)
164
+ b.vx[i] = Math.cos(angle) * speed
165
+ b.vy[i] = Math.sin(angle) * speed
166
+ }
167
+ for (let k = 0; k < TAIL; k++) {
168
+ b.tail[(i * TAIL + k) * 2] = cx
169
+ b.tail[(i * TAIL + k) * 2 + 1] = cy
170
+ }
171
+ }
172
+ return b
173
+ }
174
+
175
+ function stepBurst(ctx: CanvasRenderingContext2D, b: Burst, dt: number) {
176
+ b.age += dt
177
+ const t = b.age
178
+ const n = b.x.length
179
+ const opened = b.radius * easeOut(Math.min(t / EXPAND, 1))
180
+ b.tailClock += dt
181
+ const shift = b.tailClock >= TAIL_STEP
182
+ if (shift) b.tailClock %= TAIL_STEP
183
+ for (let i = 0; i < n; i++) {
184
+ if (b.offsets) {
185
+ b.x[i] = b.cx + b.offsets[i * 2]! * opened
186
+ b.y[i] = b.cy + b.offsets[i * 2 + 1]! * opened + SAG * t * t
187
+ } else {
188
+ b.vx[i]! -= b.vx[i]! * DRAG * dt
189
+ b.vy[i]! += (GRAVITY - b.vy[i]! * DRAG) * dt
190
+ b.x[i]! += b.vx[i]! * dt
191
+ b.y[i]! += b.vy[i]! * dt
192
+ }
193
+ const base = i * TAIL * 2
194
+ if (shift) b.tail.copyWithin(base, base + 2, base + TAIL * 2)
195
+ b.tail[base + (TAIL - 1) * 2] = b.x[i]!
196
+ b.tail[base + (TAIL - 1) * 2 + 1] = b.y[i]!
197
+ }
198
+ // A flash where it bursts
199
+ if (t < 0.15) {
200
+ ctx.globalAlpha = 0.35 * (1 - t / 0.15)
201
+ ctx.fillStyle = b.colors[0]!
202
+ ctx.beginPath()
203
+ ctx.arc(b.cx, b.cy, b.radius * 0.25, 0, Math.PI * 2)
204
+ ctx.fill()
205
+ }
206
+ // Fades over the second half of its life, the sparks flickering out over the last third
207
+ const left = 1 - t / b.life
208
+ const fade = Math.max(0, Math.min(1, left * 2))
209
+ const flicker = left < 0.35
210
+ const shown = new Uint8Array(n)
211
+ for (let i = 0; i < n; i++) shown[i] = !flicker || Math.random() < 0.6 ? 1 : 0
212
+ // One path per color and streak segment, fainter and thinner toward the tail's end, then a bright head
213
+ b.colors.forEach((color, ci) => {
214
+ ctx.strokeStyle = color
215
+ ctx.fillStyle = color
216
+ for (let k = 1; k < TAIL; k++) {
217
+ const along = k / (TAIL - 1)
218
+ ctx.globalAlpha = fade * along * along
219
+ ctx.lineWidth = 0.8 + 1.6 * along
220
+ ctx.beginPath()
221
+ for (let i = ci; i < n; i += b.colors.length) {
222
+ if (!shown[i]) continue
223
+ const a = (i * TAIL + k - 1) * 2
224
+ ctx.moveTo(b.tail[a]!, b.tail[a + 1]!)
225
+ ctx.lineTo(b.tail[a + 2]!, b.tail[a + 3]!)
226
+ }
227
+ ctx.stroke()
228
+ }
229
+ ctx.globalAlpha = fade
230
+ ctx.beginPath()
231
+ for (let i = ci; i < n; i += b.colors.length) {
232
+ if (!shown[i]) continue
233
+ ctx.moveTo(b.x[i]! + 1.8, b.y[i]!)
234
+ ctx.arc(b.x[i]!, b.y[i]!, 1.8, 0, Math.PI * 2)
235
+ }
236
+ ctx.fill()
237
+ })
238
+ }
239
+
240
+ const layer: EffectLayer = {
241
+ step(ctx, dt, dpr, now) {
242
+ emitters = emitters.filter((e) => e.until > now)
243
+ for (const e of emitters) {
244
+ e.carry += e.rate * dt
245
+ for (; e.carry >= 1; e.carry--) e.launch()
246
+ }
247
+ const due = pending.filter((p) => p.at <= now)
248
+ pending = pending.filter((p) => p.at > now)
249
+ for (const p of due) p.launch()
250
+
251
+ ctx.setTransform(dpr, 0, 0, dpr, 0, 0)
252
+ ctx.lineCap = 'round'
253
+
254
+ for (const r of rockets) {
255
+ r.age += dt
256
+ const t = easeOut(Math.min(r.age / r.flight, 1))
257
+ r.trail.push(r.x0 + (r.x1 - r.x0) * t, r.y0 + (r.y1 - r.y0) * t)
258
+ if (r.trail.length > TRAIL * 2) r.trail.splice(0, 2)
259
+ ctx.strokeStyle = r.color
260
+ ctx.lineWidth = 2.5
261
+ for (let i = 2; i < r.trail.length; i += 2) {
262
+ ctx.globalAlpha = i / r.trail.length
263
+ ctx.beginPath()
264
+ ctx.moveTo(r.trail[i - 2]!, r.trail[i - 1]!)
265
+ ctx.lineTo(r.trail[i]!, r.trail[i + 1]!)
266
+ ctx.stroke()
267
+ }
268
+ if (r.age >= r.flight) r.burst()
269
+ }
270
+ rockets = rockets.filter((r) => r.age < r.flight)
271
+
272
+ for (const b of bursts) stepBurst(ctx, b, dt)
273
+ bursts = bursts.filter((b) => b.age < b.life)
274
+
275
+ return rockets.length > 0 || bursts.length > 0 || pending.length > 0 || emitters.length > 0
276
+ },
277
+ }
278
+
279
+ /**
280
+ * Launches fireworks: a few rockets, or with `duration` a show that keeps going (stop it early with the returned
281
+ * function). Browser only; does nothing under reduced motion.
282
+ */
283
+ export function launchFireworks(options: FireworksOptions = {}): StopFireworks {
284
+ const noop = () => {}
285
+ if (typeof window === 'undefined' || reducedMotion()) return noop
286
+ const colors = options.colors?.length ? options.colors : themeColors(document.documentElement, ROLES)
287
+ const shapes = options.shapes?.length ? options.shapes : DEFAULT_SHAPES
288
+ const count = options.count ?? 6
289
+ const pick = <T,>(list: T[]) => list[Math.floor(Math.random() * list.length)]!
290
+
291
+ const launch = () => {
292
+ const w = window.innerWidth
293
+ const h = window.innerHeight
294
+ const x0 = w * (0.1 + Math.random() * 0.8)
295
+ const color = pick(colors)
296
+ const rocket: Rocket = {
297
+ x0,
298
+ y0: h + 10,
299
+ x1: x0 + (Math.random() - 0.5) * 120,
300
+ y1: h * (0.15 + Math.random() * 0.3),
301
+ flight: 0.9 + Math.random() * 0.4,
302
+ age: 0,
303
+ trail: [],
304
+ color,
305
+ burst() {
306
+ const shape = pick(shapes)
307
+ const radius = (options.size ?? Math.min(180, Math.max(70, Math.min(w, h) / 6))) * (0.8 + Math.random() * 0.4)
308
+ // Sometimes two colors, alternating spark by spark
309
+ const burstColors = Math.random() < 0.3 ? [color, pick(colors)] : [color]
310
+ if (shape === 'classic') {
311
+ bursts.push(createBurst(this.x1, this.y1, radius, burstColors, null))
312
+ return
313
+ }
314
+ const base = shapeOutline(shape)
315
+ // Turned at random, except a heart, which reads only upright
316
+ const angle = shape === 'Heart' ? 0 : Math.random() * Math.PI * 2
317
+ const cos = Math.cos(angle), sin = Math.sin(angle)
318
+ // A ring gets a second, smaller one inside it
319
+ const rings = shape === 'Circle' ? [1, 0.55] : [1]
320
+ const offsets = new Float32Array(base.length * rings.length)
321
+ rings.forEach((scale, k) => {
322
+ for (let i = 0; i < base.length; i += 2) {
323
+ offsets[k * base.length + i] = (base[i]! * cos - base[i + 1]! * sin) * scale
324
+ offsets[k * base.length + i + 1] = (base[i]! * sin + base[i + 1]! * cos) * scale
325
+ }
326
+ })
327
+ bursts.push(createBurst(this.x1, this.y1, radius, burstColors, offsets))
328
+ },
329
+ }
330
+ rockets.push(rocket)
331
+ }
332
+
333
+ if (!runLayer(layer)) return noop
334
+ let emitter: (typeof emitters)[number] | null = null
335
+ const now = performance.now()
336
+ if (options.duration && options.duration > 0) {
337
+ emitter = { until: now + options.duration, rate: count, carry: 1, launch }
338
+ emitters.push(emitter)
339
+ } else {
340
+ // The first one right away, the rest over about 0.25s each
341
+ for (let i = 0; i < count; i++) pending.push({ at: now + (i === 0 ? 0 : Math.random() * count * 250), launch })
342
+ }
343
+ return () => {
344
+ if (emitter) emitter.until = 0
345
+ }
346
+ }
347
+
348
+ /** Fireworks for a component: `launch()`, `launch({ shapes: ['classic'] })`, `launch({ shapes: ['Heart'] })`, `launch({ duration: 10000 })`. */
349
+ export function useFireworks() {
350
+ return { launch: launchFireworks }
351
+ }
352
+
353
+ /** Stops every firework and removes the canvas. Internal, for tests; not exported from index.ts. */
354
+ export function __resetFireworks() {
355
+ rockets = []
356
+ bursts = []
357
+ pending = []
358
+ emitters = []
359
+ __resetEffectCanvas()
360
+ }