@tsparticles/engine 4.0.0-alpha.2 → 4.0.0-alpha.4

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 (283) hide show
  1. package/794.min.js +2 -0
  2. package/794.min.js.LICENSE.txt +1 -0
  3. package/README.md +2 -5
  4. package/browser/Core/Canvas.js +18 -22
  5. package/browser/Core/Container.js +44 -169
  6. package/browser/Core/Engine.js +40 -61
  7. package/browser/Core/Particle.js +99 -58
  8. package/browser/Core/Particles.js +36 -25
  9. package/browser/Core/Utils/Constants.js +2 -2
  10. package/browser/Core/Utils/EventListeners.js +5 -250
  11. package/browser/Options/Classes/Options.js +7 -75
  12. package/browser/Options/Classes/Particles/ParticlesOptions.js +5 -11
  13. package/{esm/Options/Classes/Interactivity/Events → browser/Options/Classes}/ResizeEvent.js +1 -1
  14. package/browser/Utils/CanvasUtils.js +26 -83
  15. package/browser/Utils/ColorUtils.js +17 -2
  16. package/browser/Utils/MathUtils.js +3 -2
  17. package/browser/Utils/Utils.js +26 -33
  18. package/browser/exports.js +1 -21
  19. package/cjs/Core/Canvas.js +18 -22
  20. package/cjs/Core/Container.js +44 -169
  21. package/cjs/Core/Engine.js +40 -61
  22. package/cjs/Core/Particle.js +99 -58
  23. package/cjs/Core/Particles.js +36 -25
  24. package/cjs/Core/Utils/Constants.js +2 -2
  25. package/cjs/Core/Utils/EventListeners.js +5 -250
  26. package/cjs/Options/Classes/Options.js +7 -75
  27. package/cjs/Options/Classes/Particles/ParticlesOptions.js +5 -11
  28. package/cjs/Options/Classes/{Interactivity/Events/ResizeEvent.js → ResizeEvent.js} +1 -1
  29. package/cjs/Utils/CanvasUtils.js +26 -83
  30. package/cjs/Utils/ColorUtils.js +17 -2
  31. package/cjs/Utils/MathUtils.js +3 -2
  32. package/cjs/Utils/Utils.js +26 -33
  33. package/cjs/exports.js +1 -21
  34. package/dist_browser_Core_Container_js.js +6 -16
  35. package/esm/Core/Canvas.js +18 -22
  36. package/esm/Core/Container.js +44 -169
  37. package/esm/Core/Engine.js +40 -61
  38. package/esm/Core/Particle.js +99 -58
  39. package/esm/Core/Particles.js +36 -25
  40. package/esm/Core/Utils/Constants.js +2 -2
  41. package/esm/Core/Utils/EventListeners.js +5 -250
  42. package/esm/Options/Classes/Options.js +7 -75
  43. package/esm/Options/Classes/Particles/ParticlesOptions.js +5 -11
  44. package/{browser/Options/Classes/Interactivity/Events → esm/Options/Classes}/ResizeEvent.js +1 -1
  45. package/esm/Utils/CanvasUtils.js +26 -83
  46. package/esm/Utils/ColorUtils.js +17 -2
  47. package/esm/Utils/MathUtils.js +3 -2
  48. package/esm/Utils/Utils.js +26 -33
  49. package/esm/exports.js +1 -21
  50. package/package.json +1 -1
  51. package/report.html +1 -1
  52. package/tsparticles.engine.js +16 -208
  53. package/tsparticles.engine.min.js +1 -1
  54. package/tsparticles.engine.min.js.LICENSE.txt +1 -1
  55. package/types/Core/Canvas.d.ts +3 -0
  56. package/types/Core/Container.d.ts +4 -15
  57. package/types/Core/Engine.d.ts +4 -19
  58. package/types/Core/Interfaces/IContainerPlugin.d.ts +8 -1
  59. package/types/Core/Interfaces/IDrawParticleParams.d.ts +1 -1
  60. package/types/Core/Interfaces/IParticleOpacityData.d.ts +4 -0
  61. package/types/Core/Interfaces/IParticleRotateData.d.ts +4 -0
  62. package/types/Core/Interfaces/IParticleTransformValues.d.ts +4 -4
  63. package/types/Core/Interfaces/IParticleUpdater.d.ts +1 -1
  64. package/types/Core/Interfaces/IPlugin.d.ts +5 -1
  65. package/types/Core/Particle.d.ts +12 -2
  66. package/types/Core/Particles.d.ts +1 -5
  67. package/types/Core/Utils/Constants.d.ts +2 -2
  68. package/types/Core/Utils/EventListeners.d.ts +0 -14
  69. package/types/Options/Classes/Options.d.ts +2 -11
  70. package/types/Options/Classes/Particles/ParticlesOptions.d.ts +0 -2
  71. package/types/Options/Classes/ResizeEvent.d.ts +9 -0
  72. package/types/Options/Interfaces/IOptions.d.ts +2 -8
  73. package/types/Options/Interfaces/Particles/IParticlesOptions.d.ts +0 -3
  74. package/types/Types/CustomEventListener.d.ts +1 -1
  75. package/types/Types/EngineInitializers.d.ts +10 -0
  76. package/types/Utils/CanvasUtils.d.ts +6 -21
  77. package/types/Utils/EventDispatcher.d.ts +1 -1
  78. package/types/Utils/Utils.d.ts +4 -6
  79. package/types/export-types.d.ts +3 -19
  80. package/types/exports.d.ts +1 -21
  81. package/umd/Core/Canvas.js +17 -21
  82. package/umd/Core/Container.js +44 -169
  83. package/umd/Core/Engine.js +41 -62
  84. package/umd/Core/Particle.js +99 -58
  85. package/umd/Core/Particles.js +37 -26
  86. package/umd/Core/Utils/Constants.js +3 -3
  87. package/umd/Core/Utils/EventListeners.js +7 -252
  88. package/umd/Options/Classes/Options.js +7 -75
  89. package/umd/Options/Classes/Particles/ParticlesOptions.js +5 -11
  90. package/umd/Options/Classes/{Interactivity/Events/ResizeEvent.js → ResizeEvent.js} +2 -2
  91. package/umd/Utils/CanvasUtils.js +25 -82
  92. package/umd/Utils/ColorUtils.js +17 -2
  93. package/umd/Utils/MathUtils.js +3 -2
  94. package/umd/Utils/Utils.js +27 -36
  95. package/umd/exports.js +2 -22
  96. package/638.min.js +0 -2
  97. package/638.min.js.LICENSE.txt +0 -1
  98. package/browser/Core/Utils/ExternalInteractorBase.js +0 -7
  99. package/browser/Core/Utils/InteractionManager.js +0 -60
  100. package/browser/Core/Utils/ParticlesInteractorBase.js +0 -7
  101. package/browser/Enums/InteractivityDetect.js +0 -6
  102. package/browser/Enums/Modes/ResponsiveMode.js +0 -5
  103. package/browser/Enums/Modes/ThemeMode.js +0 -6
  104. package/browser/Enums/Types/DivType.js +0 -5
  105. package/browser/Enums/Types/InteractorType.js +0 -5
  106. package/browser/Options/Classes/Interactivity/Events/ClickEvent.js +0 -18
  107. package/browser/Options/Classes/Interactivity/Events/DivEvent.js +0 -27
  108. package/browser/Options/Classes/Interactivity/Events/Events.js +0 -30
  109. package/browser/Options/Classes/Interactivity/Events/HoverEvent.js +0 -21
  110. package/browser/Options/Classes/Interactivity/Events/Parallax.js +0 -22
  111. package/browser/Options/Classes/Interactivity/Interactivity.js +0 -22
  112. package/browser/Options/Classes/Interactivity/Modes/Modes.js +0 -25
  113. package/browser/Options/Classes/ManualParticle.js +0 -21
  114. package/browser/Options/Classes/Responsive.js +0 -29
  115. package/browser/Options/Classes/Theme/Theme.js +0 -21
  116. package/browser/Options/Classes/Theme/ThemeDefault.js +0 -23
  117. package/browser/Options/Interfaces/Interactivity/Events/IEvents.js +0 -1
  118. package/browser/Options/Interfaces/Interactivity/Events/IHoverEvent.js +0 -1
  119. package/browser/Options/Interfaces/Interactivity/Events/IParallax.js +0 -1
  120. package/browser/Options/Interfaces/Interactivity/IInteractivity.js +0 -1
  121. package/browser/Options/Interfaces/Interactivity/Modes/IModeDiv.js +0 -1
  122. package/browser/Options/Interfaces/Interactivity/Modes/IModes.js +0 -1
  123. package/browser/Options/Interfaces/Theme/ITheme.js +0 -1
  124. package/browser/Options/Interfaces/Theme/IThemeDefault.js +0 -1
  125. package/cjs/Core/Interfaces/IContainerInteractivity.js +0 -1
  126. package/cjs/Core/Interfaces/IExternalInteractor.js +0 -1
  127. package/cjs/Core/Interfaces/IInteractor.js +0 -1
  128. package/cjs/Core/Interfaces/IMouseData.js +0 -1
  129. package/cjs/Core/Interfaces/IParticlesInteractor.js +0 -1
  130. package/cjs/Core/Utils/ExternalInteractorBase.js +0 -7
  131. package/cjs/Core/Utils/InteractionManager.js +0 -60
  132. package/cjs/Core/Utils/ParticlesInteractorBase.js +0 -7
  133. package/cjs/Enums/InteractivityDetect.js +0 -6
  134. package/cjs/Enums/Modes/ResponsiveMode.js +0 -5
  135. package/cjs/Enums/Modes/ThemeMode.js +0 -6
  136. package/cjs/Enums/Types/DivType.js +0 -5
  137. package/cjs/Enums/Types/InteractorType.js +0 -5
  138. package/cjs/Options/Classes/Interactivity/Events/ClickEvent.js +0 -18
  139. package/cjs/Options/Classes/Interactivity/Events/DivEvent.js +0 -27
  140. package/cjs/Options/Classes/Interactivity/Events/Events.js +0 -30
  141. package/cjs/Options/Classes/Interactivity/Events/HoverEvent.js +0 -21
  142. package/cjs/Options/Classes/Interactivity/Events/Parallax.js +0 -22
  143. package/cjs/Options/Classes/Interactivity/Interactivity.js +0 -22
  144. package/cjs/Options/Classes/Interactivity/Modes/Modes.js +0 -25
  145. package/cjs/Options/Classes/ManualParticle.js +0 -21
  146. package/cjs/Options/Classes/Responsive.js +0 -29
  147. package/cjs/Options/Classes/Theme/Theme.js +0 -21
  148. package/cjs/Options/Classes/Theme/ThemeDefault.js +0 -23
  149. package/cjs/Options/Interfaces/IManualParticle.js +0 -1
  150. package/cjs/Options/Interfaces/IResponsive.js +0 -1
  151. package/cjs/Options/Interfaces/Interactivity/Events/IClickEvent.js +0 -1
  152. package/cjs/Options/Interfaces/Interactivity/Events/IDivEvent.js +0 -1
  153. package/cjs/Options/Interfaces/Interactivity/Events/IEvents.js +0 -1
  154. package/cjs/Options/Interfaces/Interactivity/Events/IHoverEvent.js +0 -1
  155. package/cjs/Options/Interfaces/Interactivity/Events/IParallax.js +0 -1
  156. package/cjs/Options/Interfaces/Interactivity/IInteractivity.js +0 -1
  157. package/cjs/Options/Interfaces/Interactivity/Modes/IModeDiv.js +0 -1
  158. package/cjs/Options/Interfaces/Interactivity/Modes/IModes.js +0 -1
  159. package/cjs/Options/Interfaces/Theme/ITheme.js +0 -1
  160. package/cjs/Options/Interfaces/Theme/IThemeDefault.js +0 -1
  161. package/esm/Core/Interfaces/IContainerInteractivity.js +0 -1
  162. package/esm/Core/Interfaces/IExternalInteractor.js +0 -1
  163. package/esm/Core/Interfaces/IInteractor.js +0 -1
  164. package/esm/Core/Interfaces/IMouseData.js +0 -1
  165. package/esm/Core/Interfaces/IParticlesInteractor.js +0 -1
  166. package/esm/Core/Utils/ExternalInteractorBase.js +0 -7
  167. package/esm/Core/Utils/InteractionManager.js +0 -60
  168. package/esm/Core/Utils/ParticlesInteractorBase.js +0 -7
  169. package/esm/Enums/InteractivityDetect.js +0 -6
  170. package/esm/Enums/Modes/ResponsiveMode.js +0 -5
  171. package/esm/Enums/Modes/ThemeMode.js +0 -6
  172. package/esm/Enums/Types/DivType.js +0 -5
  173. package/esm/Enums/Types/InteractorType.js +0 -5
  174. package/esm/Options/Classes/Interactivity/Events/ClickEvent.js +0 -18
  175. package/esm/Options/Classes/Interactivity/Events/DivEvent.js +0 -27
  176. package/esm/Options/Classes/Interactivity/Events/Events.js +0 -30
  177. package/esm/Options/Classes/Interactivity/Events/HoverEvent.js +0 -21
  178. package/esm/Options/Classes/Interactivity/Events/Parallax.js +0 -22
  179. package/esm/Options/Classes/Interactivity/Interactivity.js +0 -22
  180. package/esm/Options/Classes/Interactivity/Modes/Modes.js +0 -25
  181. package/esm/Options/Classes/ManualParticle.js +0 -21
  182. package/esm/Options/Classes/Responsive.js +0 -29
  183. package/esm/Options/Classes/Theme/Theme.js +0 -21
  184. package/esm/Options/Classes/Theme/ThemeDefault.js +0 -23
  185. package/esm/Options/Interfaces/IManualParticle.js +0 -1
  186. package/esm/Options/Interfaces/IResponsive.js +0 -1
  187. package/esm/Options/Interfaces/Interactivity/Events/IClickEvent.js +0 -1
  188. package/esm/Options/Interfaces/Interactivity/Events/IDivEvent.js +0 -1
  189. package/esm/Options/Interfaces/Interactivity/Events/IEvents.js +0 -1
  190. package/esm/Options/Interfaces/Interactivity/Events/IHoverEvent.js +0 -1
  191. package/esm/Options/Interfaces/Interactivity/Events/IParallax.js +0 -1
  192. package/esm/Options/Interfaces/Interactivity/IInteractivity.js +0 -1
  193. package/esm/Options/Interfaces/Interactivity/Modes/IModeDiv.js +0 -1
  194. package/esm/Options/Interfaces/Interactivity/Modes/IModes.js +0 -1
  195. package/esm/Options/Interfaces/Theme/ITheme.js +0 -1
  196. package/esm/Options/Interfaces/Theme/IThemeDefault.js +0 -1
  197. package/types/Core/Interfaces/IContainerInteractivity.d.ts +0 -6
  198. package/types/Core/Interfaces/IExternalInteractor.d.ts +0 -12
  199. package/types/Core/Interfaces/IInteractor.d.ts +0 -16
  200. package/types/Core/Interfaces/IMouseData.d.ts +0 -9
  201. package/types/Core/Interfaces/IParticlesInteractor.d.ts +0 -7
  202. package/types/Core/Utils/ExternalInteractorBase.d.ts +0 -15
  203. package/types/Core/Utils/InteractionManager.d.ts +0 -17
  204. package/types/Core/Utils/ParticlesInteractorBase.d.ts +0 -15
  205. package/types/Enums/InteractivityDetect.d.ts +0 -5
  206. package/types/Enums/Modes/ResponsiveMode.d.ts +0 -4
  207. package/types/Enums/Modes/ThemeMode.d.ts +0 -5
  208. package/types/Enums/Types/DivType.d.ts +0 -4
  209. package/types/Enums/Types/InteractorType.d.ts +0 -4
  210. package/types/Options/Classes/Interactivity/Events/ClickEvent.d.ts +0 -10
  211. package/types/Options/Classes/Interactivity/Events/DivEvent.d.ts +0 -13
  212. package/types/Options/Classes/Interactivity/Events/Events.d.ts +0 -16
  213. package/types/Options/Classes/Interactivity/Events/HoverEvent.d.ts +0 -12
  214. package/types/Options/Classes/Interactivity/Events/Parallax.d.ts +0 -10
  215. package/types/Options/Classes/Interactivity/Events/ResizeEvent.d.ts +0 -9
  216. package/types/Options/Classes/Interactivity/Interactivity.d.ts +0 -16
  217. package/types/Options/Classes/Interactivity/Modes/Modes.d.ts +0 -12
  218. package/types/Options/Classes/ManualParticle.d.ts +0 -10
  219. package/types/Options/Classes/Responsive.d.ts +0 -12
  220. package/types/Options/Classes/Theme/Theme.d.ts +0 -12
  221. package/types/Options/Classes/Theme/ThemeDefault.d.ts +0 -11
  222. package/types/Options/Interfaces/IManualParticle.d.ts +0 -7
  223. package/types/Options/Interfaces/IResponsive.d.ts +0 -7
  224. package/types/Options/Interfaces/Interactivity/Events/IClickEvent.d.ts +0 -5
  225. package/types/Options/Interfaces/Interactivity/Events/IDivEvent.d.ts +0 -8
  226. package/types/Options/Interfaces/Interactivity/Events/IEvents.d.ts +0 -11
  227. package/types/Options/Interfaces/Interactivity/Events/IHoverEvent.d.ts +0 -7
  228. package/types/Options/Interfaces/Interactivity/Events/IParallax.d.ts +0 -5
  229. package/types/Options/Interfaces/Interactivity/IInteractivity.d.ts +0 -9
  230. package/types/Options/Interfaces/Interactivity/Modes/IModeDiv.d.ts +0 -4
  231. package/types/Options/Interfaces/Interactivity/Modes/IModes.d.ts +0 -1
  232. package/types/Options/Interfaces/Theme/ITheme.d.ts +0 -7
  233. package/types/Options/Interfaces/Theme/IThemeDefault.d.ts +0 -6
  234. package/umd/Core/Interfaces/IMouseData.js +0 -12
  235. package/umd/Core/Interfaces/IParticlesInteractor.js +0 -12
  236. package/umd/Core/Utils/ExternalInteractorBase.js +0 -21
  237. package/umd/Core/Utils/InteractionManager.js +0 -74
  238. package/umd/Core/Utils/ParticlesInteractorBase.js +0 -21
  239. package/umd/Enums/InteractivityDetect.js +0 -19
  240. package/umd/Enums/Modes/ResponsiveMode.js +0 -18
  241. package/umd/Enums/Modes/ThemeMode.js +0 -19
  242. package/umd/Enums/Types/DivType.js +0 -18
  243. package/umd/Enums/Types/InteractorType.js +0 -18
  244. package/umd/Options/Classes/Interactivity/Events/ClickEvent.js +0 -32
  245. package/umd/Options/Classes/Interactivity/Events/DivEvent.js +0 -41
  246. package/umd/Options/Classes/Interactivity/Events/Events.js +0 -44
  247. package/umd/Options/Classes/Interactivity/Events/HoverEvent.js +0 -35
  248. package/umd/Options/Classes/Interactivity/Events/Parallax.js +0 -36
  249. package/umd/Options/Classes/Interactivity/Interactivity.js +0 -36
  250. package/umd/Options/Classes/Interactivity/Modes/Modes.js +0 -39
  251. package/umd/Options/Classes/ManualParticle.js +0 -35
  252. package/umd/Options/Classes/Responsive.js +0 -43
  253. package/umd/Options/Classes/Theme/Theme.js +0 -35
  254. package/umd/Options/Classes/Theme/ThemeDefault.js +0 -37
  255. package/umd/Options/Interfaces/IManualParticle.js +0 -12
  256. package/umd/Options/Interfaces/IResponsive.js +0 -12
  257. package/umd/Options/Interfaces/Interactivity/Events/IClickEvent.js +0 -12
  258. package/umd/Options/Interfaces/Interactivity/Events/IDivEvent.js +0 -12
  259. package/umd/Options/Interfaces/Interactivity/Events/IEvents.js +0 -12
  260. package/umd/Options/Interfaces/Interactivity/Events/IHoverEvent.js +0 -12
  261. package/umd/Options/Interfaces/Interactivity/Events/IParallax.js +0 -12
  262. package/umd/Options/Interfaces/Interactivity/IInteractivity.js +0 -12
  263. package/umd/Options/Interfaces/Interactivity/Modes/IModeDiv.js +0 -12
  264. package/umd/Options/Interfaces/Interactivity/Modes/IModes.js +0 -12
  265. package/umd/Options/Interfaces/Theme/ITheme.js +0 -12
  266. package/umd/Options/Interfaces/Theme/IThemeDefault.js +0 -12
  267. /package/browser/Core/Interfaces/{IContainerInteractivity.js → IParticleOpacityData.js} +0 -0
  268. /package/browser/Core/Interfaces/{IExternalInteractor.js → IParticleRotateData.js} +0 -0
  269. /package/browser/Options/Interfaces/{Interactivity/Events/IResizeEvent.js → IResizeEvent.js} +0 -0
  270. /package/browser/{Core/Interfaces/IInteractor.js → Types/EngineInitializers.js} +0 -0
  271. /package/{browser/Core/Interfaces/IMouseData.js → cjs/Core/Interfaces/IParticleOpacityData.js} +0 -0
  272. /package/{browser/Core/Interfaces/IParticlesInteractor.js → cjs/Core/Interfaces/IParticleRotateData.js} +0 -0
  273. /package/cjs/Options/Interfaces/{Interactivity/Events/IResizeEvent.js → IResizeEvent.js} +0 -0
  274. /package/{browser/Options/Interfaces/IManualParticle.js → cjs/Types/EngineInitializers.js} +0 -0
  275. /package/{browser/Options/Interfaces/IResponsive.js → esm/Core/Interfaces/IParticleOpacityData.js} +0 -0
  276. /package/{browser/Options/Interfaces/Interactivity/Events/IClickEvent.js → esm/Core/Interfaces/IParticleRotateData.js} +0 -0
  277. /package/esm/Options/Interfaces/{Interactivity/Events/IResizeEvent.js → IResizeEvent.js} +0 -0
  278. /package/{browser/Options/Interfaces/Interactivity/Events/IDivEvent.js → esm/Types/EngineInitializers.js} +0 -0
  279. /package/types/Options/Interfaces/{Interactivity/Events/IResizeEvent.d.ts → IResizeEvent.d.ts} +0 -0
  280. /package/umd/Core/Interfaces/{IContainerInteractivity.js → IParticleOpacityData.js} +0 -0
  281. /package/umd/Core/Interfaces/{IExternalInteractor.js → IParticleRotateData.js} +0 -0
  282. /package/umd/Options/Interfaces/{Interactivity/Events/IResizeEvent.js → IResizeEvent.js} +0 -0
  283. /package/umd/{Core/Interfaces/IInteractor.js → Types/EngineInitializers.js} +0 -0
@@ -1,56 +1,10 @@
1
- import { double, lengthOffset, millisecondsToSeconds, mouseDownEvent, mouseLeaveEvent, mouseMoveEvent, mouseOutEvent, mouseUpEvent, resizeEvent, touchCancelEvent, touchDelay, touchEndEvent, touchMoveEvent, touchStartEvent, visibilityChangeEvent, } from "./Constants.js";
2
- import { executeOnSingleOrMultiple, safeDocument, safeMatchMedia } from "../../Utils/Utils.js";
3
- import { InteractivityDetect } from "../../Enums/InteractivityDetect.js";
4
- import { isBoolean } from "../../Utils/TypeUtils.js";
5
- function manageListener(element, event, handler, add, options) {
6
- if (add) {
7
- let addOptions = { passive: true };
8
- if (isBoolean(options)) {
9
- addOptions.capture = options;
10
- }
11
- else if (options !== undefined) {
12
- addOptions = options;
13
- }
14
- element.addEventListener(event, handler, addOptions);
15
- }
16
- else {
17
- const removeOptions = options;
18
- element.removeEventListener(event, handler, removeOptions);
19
- }
20
- }
1
+ import { manageListener, safeDocument } from "../../Utils/Utils.js";
2
+ import { millisecondsToSeconds, resizeEvent, visibilityChangeEvent } from "./Constants.js";
21
3
  export class EventListeners {
22
4
  constructor(container) {
23
5
  this.container = container;
24
- this._canPush = true;
25
- this._doMouseTouchClick = e => {
26
- const container = this.container, options = container.actualOptions;
27
- if (this._canPush) {
28
- const mouseInteractivity = container.interactivity.mouse, mousePos = mouseInteractivity.position;
29
- if (!mousePos) {
30
- return;
31
- }
32
- mouseInteractivity.clickPosition = { ...mousePos };
33
- mouseInteractivity.clickTime = Date.now();
34
- const onClick = options.interactivity.events.onClick;
35
- executeOnSingleOrMultiple(onClick.mode, mode => {
36
- this.container.handleClickMode(mode);
37
- });
38
- }
39
- if (e.type === "touchend") {
40
- setTimeout(() => {
41
- this._mouseTouchFinish();
42
- }, touchDelay);
43
- }
44
- };
45
- this._handleThemeChange = (e) => {
46
- const mediaEvent = e, container = this.container, options = container.options, defaultThemes = options.defaultThemes, themeName = mediaEvent.matches ? defaultThemes.dark : defaultThemes.light, theme = options.themes.find(theme => theme.name === themeName);
47
- if (theme?.default.auto) {
48
- void container.loadTheme(themeName);
49
- }
50
- };
51
6
  this._handleVisibilityChange = () => {
52
7
  const container = this.container, options = container.actualOptions;
53
- this._mouseTouchFinish();
54
8
  if (!options.pauseOnBlur) {
55
9
  return;
56
10
  }
@@ -77,60 +31,16 @@ export class EventListeners {
77
31
  const canvas = this.container.canvas;
78
32
  await canvas.windowResize();
79
33
  };
80
- this._resizeTimeout = setTimeout(() => void handleResize(), this.container.actualOptions.interactivity.events.resize.delay * millisecondsToSeconds);
81
- };
82
- this._manageInteractivityListeners = (mouseLeaveTmpEvent, add) => {
83
- const handlers = this._handlers, container = this.container, options = container.actualOptions, interactivityEl = container.interactivity.element;
84
- if (!interactivityEl) {
85
- return;
86
- }
87
- const html = interactivityEl, canvas = container.canvas;
88
- canvas.setPointerEvents(html === canvas.element ? "initial" : "none");
89
- if (!(options.interactivity.events.onHover.enable || options.interactivity.events.onClick.enable)) {
90
- return;
91
- }
92
- manageListener(interactivityEl, mouseMoveEvent, handlers.mouseMove, add);
93
- manageListener(interactivityEl, touchStartEvent, handlers.touchStart, add);
94
- manageListener(interactivityEl, touchMoveEvent, handlers.touchMove, add);
95
- if (options.interactivity.events.onClick.enable) {
96
- manageListener(interactivityEl, touchEndEvent, handlers.touchEndClick, add);
97
- manageListener(interactivityEl, mouseUpEvent, handlers.mouseUp, add);
98
- manageListener(interactivityEl, mouseDownEvent, handlers.mouseDown, add);
99
- }
100
- else {
101
- manageListener(interactivityEl, touchEndEvent, handlers.touchEnd, add);
102
- }
103
- manageListener(interactivityEl, mouseLeaveTmpEvent, handlers.mouseLeave, add);
104
- manageListener(interactivityEl, touchCancelEvent, handlers.touchCancel, add);
34
+ this._resizeTimeout = setTimeout(() => void handleResize(), this.container.actualOptions.resize.delay * millisecondsToSeconds);
105
35
  };
106
36
  this._manageListeners = add => {
107
- const handlers = this._handlers, container = this.container, options = container.actualOptions, detectType = options.interactivity.detectsOn, canvasEl = container.canvas.element;
108
- let mouseLeaveTmpEvent = mouseLeaveEvent;
109
- if (detectType === InteractivityDetect.window) {
110
- container.interactivity.element = window;
111
- mouseLeaveTmpEvent = mouseOutEvent;
112
- }
113
- else if (detectType === InteractivityDetect.parent && canvasEl) {
114
- container.interactivity.element = canvasEl.parentElement ?? canvasEl.parentNode;
115
- }
116
- else {
117
- container.interactivity.element = canvasEl;
118
- }
119
- this._manageMediaMatch(add);
37
+ const handlers = this._handlers;
120
38
  this._manageResize(add);
121
- this._manageInteractivityListeners(mouseLeaveTmpEvent, add);
122
39
  manageListener(document, visibilityChangeEvent, handlers.visibilityChange, add, false);
123
40
  };
124
- this._manageMediaMatch = add => {
125
- const handlers = this._handlers;
126
- if (!this._matchMedia) {
127
- return;
128
- }
129
- manageListener(this._matchMedia, "change", handlers.themeChange, add);
130
- };
131
41
  this._manageResize = add => {
132
42
  const handlers = this._handlers, container = this.container, options = container.actualOptions;
133
- if (!options.interactivity.events.resize.enable) {
43
+ if (!options.resize.enable) {
134
44
  return;
135
45
  }
136
46
  if (typeof ResizeObserver === "undefined") {
@@ -156,165 +66,10 @@ export class EventListeners {
156
66
  this._resizeObserver.observe(canvasEl);
157
67
  }
158
68
  };
159
- this._mouseDown = () => {
160
- const { interactivity } = this.container, { mouse } = interactivity;
161
- mouse.clicking = true;
162
- mouse.downPosition = mouse.position;
163
- };
164
- this._mouseTouchClick = e => {
165
- const container = this.container, options = container.actualOptions, { mouse } = container.interactivity;
166
- mouse.inside = true;
167
- let handled = false;
168
- const mousePosition = mouse.position;
169
- if (!mousePosition || !options.interactivity.events.onClick.enable) {
170
- return;
171
- }
172
- for (const plugin of container.plugins.values()) {
173
- if (!plugin.clickPositionValid) {
174
- continue;
175
- }
176
- handled = plugin.clickPositionValid(mousePosition);
177
- if (handled) {
178
- break;
179
- }
180
- }
181
- if (!handled) {
182
- this._doMouseTouchClick(e);
183
- }
184
- mouse.clicking = false;
185
- };
186
- this._mouseTouchFinish = () => {
187
- const { interactivity } = this.container, { mouse } = interactivity;
188
- delete mouse.position;
189
- delete mouse.clickPosition;
190
- delete mouse.downPosition;
191
- interactivity.status = mouseLeaveEvent;
192
- mouse.inside = false;
193
- mouse.clicking = false;
194
- };
195
- this._mouseTouchMove = e => {
196
- const container = this.container, options = container.actualOptions, interactivity = container.interactivity, canvasEl = container.canvas.element;
197
- if (!interactivity.element) {
198
- return;
199
- }
200
- interactivity.mouse.inside = true;
201
- let pos;
202
- if (e.type.startsWith("pointer")) {
203
- this._canPush = true;
204
- const mouseEvent = e;
205
- if (interactivity.element === window) {
206
- if (canvasEl) {
207
- const clientRect = canvasEl.getBoundingClientRect();
208
- pos = {
209
- x: mouseEvent.clientX - clientRect.left,
210
- y: mouseEvent.clientY - clientRect.top,
211
- };
212
- }
213
- }
214
- else if (options.interactivity.detectsOn === InteractivityDetect.parent) {
215
- const source = mouseEvent.target, target = mouseEvent.currentTarget;
216
- if (canvasEl) {
217
- const sourceRect = source.getBoundingClientRect(), targetRect = target.getBoundingClientRect(), canvasRect = canvasEl.getBoundingClientRect();
218
- pos = {
219
- x: mouseEvent.offsetX + double * sourceRect.left - (targetRect.left + canvasRect.left),
220
- y: mouseEvent.offsetY + double * sourceRect.top - (targetRect.top + canvasRect.top),
221
- };
222
- }
223
- else {
224
- pos = {
225
- x: mouseEvent.offsetX,
226
- y: mouseEvent.offsetY,
227
- };
228
- }
229
- }
230
- else if (mouseEvent.target === canvasEl) {
231
- pos = {
232
- x: mouseEvent.offsetX,
233
- y: mouseEvent.offsetY,
234
- };
235
- }
236
- }
237
- else {
238
- this._canPush = e.type !== "touchmove";
239
- if (canvasEl) {
240
- const touchEvent = e, lastTouch = touchEvent.touches[touchEvent.touches.length - lengthOffset], canvasRect = canvasEl.getBoundingClientRect();
241
- if (!lastTouch) {
242
- return;
243
- }
244
- pos = {
245
- x: lastTouch.clientX - canvasRect.left,
246
- y: lastTouch.clientY - canvasRect.top,
247
- };
248
- }
249
- }
250
- const pxRatio = container.retina.pixelRatio;
251
- if (pos) {
252
- pos.x *= pxRatio;
253
- pos.y *= pxRatio;
254
- }
255
- interactivity.mouse.position = pos;
256
- interactivity.status = mouseMoveEvent;
257
- };
258
- this._touchEnd = e => {
259
- const evt = e, touches = Array.from(evt.changedTouches);
260
- for (const touch of touches) {
261
- this._touches.delete(touch.identifier);
262
- }
263
- this._mouseTouchFinish();
264
- };
265
- this._touchEndClick = e => {
266
- const evt = e, touches = Array.from(evt.changedTouches);
267
- for (const touch of touches) {
268
- this._touches.delete(touch.identifier);
269
- }
270
- this._mouseTouchClick(e);
271
- };
272
- this._touchStart = e => {
273
- const evt = e, touches = Array.from(evt.changedTouches);
274
- for (const touch of touches) {
275
- this._touches.set(touch.identifier, performance.now());
276
- }
277
- this._mouseTouchMove(e);
278
- };
279
- this._matchMedia = safeMatchMedia("(prefers-color-scheme: dark)");
280
- this._touches = new Map();
281
69
  this._handlers = {
282
- mouseDown: () => {
283
- this._mouseDown();
284
- },
285
- mouseLeave: () => {
286
- this._mouseTouchFinish();
287
- },
288
- mouseMove: (e) => {
289
- this._mouseTouchMove(e);
290
- },
291
- mouseUp: (e) => {
292
- this._mouseTouchClick(e);
293
- },
294
- touchStart: (e) => {
295
- this._touchStart(e);
296
- },
297
- touchMove: (e) => {
298
- this._mouseTouchMove(e);
299
- },
300
- touchEnd: (e) => {
301
- this._touchEnd(e);
302
- },
303
- touchCancel: (e) => {
304
- this._touchEnd(e);
305
- },
306
- touchEndClick: (e) => {
307
- this._touchEndClick(e);
308
- },
309
70
  visibilityChange: () => {
310
71
  this._handleVisibilityChange();
311
72
  },
312
- themeChange: (e) => {
313
- this._handleThemeChange(e);
314
- },
315
- oldThemeChange: (e) => {
316
- this._handleThemeChange(e);
317
- },
318
73
  resize: () => {
319
74
  this._handleWindowResize();
320
75
  },
@@ -1,21 +1,12 @@
1
- import { deepExtend, executeOnSingleOrMultiple, safeMatchMedia } from "../../Utils/Utils.js";
1
+ import { deepExtend, executeOnSingleOrMultiple } from "../../Utils/Utils.js";
2
2
  import { isBoolean, isNull } from "../../Utils/TypeUtils.js";
3
3
  import { Background } from "./Background/Background.js";
4
4
  import { FullScreen } from "./FullScreen/FullScreen.js";
5
- import { Interactivity } from "./Interactivity/Interactivity.js";
6
- import { ManualParticle } from "./ManualParticle.js";
7
- import { Responsive } from "./Responsive.js";
8
- import { ResponsiveMode } from "../../Enums/Modes/ResponsiveMode.js";
9
- import { Theme } from "./Theme/Theme.js";
10
- import { ThemeMode } from "../../Enums/Modes/ThemeMode.js";
5
+ import { ResizeEvent } from "./ResizeEvent.js";
11
6
  import { loadParticlesOptions } from "../../Utils/OptionsUtils.js";
12
7
  import { setRangeValue } from "../../Utils/MathUtils.js";
13
8
  export class Options {
14
9
  constructor(engine, container) {
15
- this._findDefaultTheme = mode => {
16
- return (this.themes.find(theme => theme.default.value && theme.default.mode === mode) ??
17
- this.themes.find(theme => theme.default.value && theme.default.mode === ThemeMode.any));
18
- };
19
10
  this._importPreset = preset => {
20
11
  this.load(this._engine.getPreset(preset));
21
12
  };
@@ -31,15 +22,12 @@ export class Options {
31
22
  this.duration = 0;
32
23
  this.fpsLimit = 120;
33
24
  this.hdr = true;
34
- this.interactivity = new Interactivity(engine, container);
35
- this.manualParticles = [];
36
25
  this.particles = loadParticlesOptions(this._engine, this._container);
37
26
  this.pauseOnBlur = true;
38
27
  this.pauseOnOutsideViewport = true;
39
- this.responsive = [];
28
+ this.resize = new ResizeEvent();
40
29
  this.smooth = false;
41
30
  this.style = {};
42
- this.themes = [];
43
31
  this.zLayers = 100;
44
32
  }
45
33
  load(data) {
@@ -97,70 +85,14 @@ export class Options {
97
85
  else {
98
86
  this.fullScreen.load(fullScreen);
99
87
  }
100
- this.interactivity.load(data.interactivity);
101
- if (data.manualParticles) {
102
- this.manualParticles = data.manualParticles.map(t => {
103
- const tmp = new ManualParticle();
104
- tmp.load(t);
105
- return tmp;
106
- });
107
- }
108
88
  this.particles.load(data.particles);
89
+ this.resize.load(data.resize);
109
90
  this.style = deepExtend(this.style, data.style);
110
- this._engine.loadOptions(this, data);
111
91
  if (data.smooth !== undefined) {
112
92
  this.smooth = data.smooth;
113
93
  }
114
- const interactors = this._engine.interactors.get(this._container);
115
- if (interactors) {
116
- for (const interactor of interactors) {
117
- if (interactor.loadOptions) {
118
- interactor.loadOptions(this, data);
119
- }
120
- }
121
- }
122
- if (data.responsive !== undefined) {
123
- for (const responsive of data.responsive) {
124
- const optResponsive = new Responsive();
125
- optResponsive.load(responsive);
126
- this.responsive.push(optResponsive);
127
- }
128
- }
129
- this.responsive.sort((a, b) => a.maxWidth - b.maxWidth);
130
- if (data.themes !== undefined) {
131
- for (const theme of data.themes) {
132
- const existingTheme = this.themes.find(t => t.name === theme.name);
133
- if (existingTheme) {
134
- existingTheme.load(theme);
135
- }
136
- else {
137
- const optTheme = new Theme();
138
- optTheme.load(theme);
139
- this.themes.push(optTheme);
140
- }
141
- }
142
- }
143
- this.defaultThemes.dark = this._findDefaultTheme(ThemeMode.dark)?.name;
144
- this.defaultThemes.light = this._findDefaultTheme(ThemeMode.light)?.name;
145
- }
146
- setResponsive(width, pxRatio, defaultOptions) {
147
- this.load(defaultOptions);
148
- const responsiveOptions = this.responsive.find(t => t.mode === ResponsiveMode.screen ? t.maxWidth > screen.availWidth : t.maxWidth * pxRatio > width);
149
- this.load(responsiveOptions?.options);
150
- return responsiveOptions?.maxWidth;
151
- }
152
- setTheme(name) {
153
- if (name) {
154
- const chosenTheme = this.themes.find(theme => theme.name === name);
155
- if (chosenTheme) {
156
- this.load(chosenTheme.options);
157
- }
158
- }
159
- else {
160
- const mediaMatch = safeMatchMedia("(prefers-color-scheme: dark)"), clientDarkMode = mediaMatch?.matches, defaultTheme = this._findDefaultTheme(clientDarkMode ? ThemeMode.dark : ThemeMode.light);
161
- if (defaultTheme) {
162
- this.load(defaultTheme.options);
163
- }
164
- }
94
+ this._engine.plugins.forEach(plugin => {
95
+ plugin.loadOptions(this._container, this, data);
96
+ });
165
97
  }
166
98
  }
@@ -55,9 +55,6 @@ export class ParticlesOptions {
55
55
  this.shape.load(data.shape);
56
56
  this.size.load(data.size);
57
57
  this.zIndex.load(data.zIndex);
58
- if (data.interactivity !== undefined) {
59
- this.interactivity = deepExtend({}, data.interactivity);
60
- }
61
58
  const strokeToLoad = data.stroke;
62
59
  if (strokeToLoad) {
63
60
  this.stroke = executeOnSingleOrMultiple(strokeToLoad, t => {
@@ -67,6 +64,11 @@ export class ParticlesOptions {
67
64
  });
68
65
  }
69
66
  if (this._container) {
67
+ for (const plugin of this._engine.plugins) {
68
+ if (plugin.loadParticlesOptions) {
69
+ plugin.loadParticlesOptions(this._container, this, data);
70
+ }
71
+ }
70
72
  const updaters = this._engine.updaters.get(this._container);
71
73
  if (updaters) {
72
74
  for (const updater of updaters) {
@@ -75,14 +77,6 @@ export class ParticlesOptions {
75
77
  }
76
78
  }
77
79
  }
78
- const interactors = this._engine.interactors.get(this._container);
79
- if (interactors) {
80
- for (const interactor of interactors) {
81
- if (interactor.loadParticlesOptions) {
82
- interactor.loadParticlesOptions(this, data);
83
- }
84
- }
85
- }
86
80
  }
87
81
  }
88
82
  }
@@ -1,4 +1,4 @@
1
- import { isNull } from "../../../../Utils/TypeUtils.js";
1
+ import { isNull } from "../../Utils/TypeUtils.js";
2
2
  export class ResizeEvent {
3
3
  constructor() {
4
4
  this.delay = 0.5;
@@ -1,4 +1,4 @@
1
- import { defaultTransform, identity, lFactor, minStrokeWidth, originPoint } from "../Core/Utils/Constants.js";
1
+ import { lFactor, minStrokeWidth, originPoint } from "../Core/Utils/Constants.js";
2
2
  import { AlterType } from "../Enums/Types/AlterType.js";
3
3
  export function clearDrawPlugin(context, plugin, delta) {
4
4
  if (!plugin.clearDraw) {
@@ -28,15 +28,7 @@ export function clear(context, dimension) {
28
28
  context.clearRect(originPoint.x, originPoint.y, dimension.width, dimension.height);
29
29
  }
30
30
  export function drawParticle(data) {
31
- const { container, context, particle, delta, colorStyles, radius, opacity, transform } = data, pos = particle.getPosition(), angle = particle.getAngle(), rotateData = {
32
- sin: Math.sin(angle),
33
- cos: Math.cos(angle),
34
- }, rotating = particle.isRotating, transformData = {
35
- a: rotateData.cos * (transform.a ?? defaultTransform.a),
36
- b: rotating ? rotateData.sin * (transform.b ?? identity) : (transform.b ?? defaultTransform.b),
37
- c: rotating ? -rotateData.sin * (transform.c ?? identity) : (transform.c ?? defaultTransform.c),
38
- d: rotateData.cos * (transform.d ?? defaultTransform.d),
39
- };
31
+ const { container, context, particle, delta, colorStyles, radius, opacity, transform } = data, pos = particle.getPosition(), transformData = particle.getTransformData(transform);
40
32
  context.setTransform(transformData.a, transformData.b, transformData.c, transformData.d, pos.x, pos.y);
41
33
  if (colorStyles.fill) {
42
34
  context.fillStyle = colorStyles.fill;
@@ -47,24 +39,25 @@ export function drawParticle(data) {
47
39
  context.strokeStyle = colorStyles.stroke;
48
40
  }
49
41
  const drawData = {
50
- container,
51
42
  context,
52
43
  particle,
53
44
  radius,
54
45
  opacity,
55
46
  delta,
47
+ pixelRatio: container.retina.pixelRatio,
48
+ fill: particle.shapeFill,
49
+ stroke: strokeWidth > minStrokeWidth || !particle.shapeFill,
56
50
  transformData,
57
- strokeWidth,
58
51
  };
59
- drawBeforeEffect(drawData);
60
- drawShapeBeforeDraw(drawData);
61
- drawShape(drawData);
62
- drawShapeAfterDraw(drawData);
63
- drawAfterEffect(drawData);
52
+ drawBeforeEffect(container, drawData);
53
+ drawShapeBeforeDraw(container, drawData);
54
+ drawShape(container, drawData);
55
+ drawShapeAfterDraw(container, drawData);
56
+ drawAfterEffect(container, drawData);
64
57
  context.resetTransform();
65
58
  }
66
- export function drawAfterEffect(data) {
67
- const { container, context, particle, radius, opacity, strokeWidth, delta, transformData } = data;
59
+ export function drawAfterEffect(container, data) {
60
+ const { particle } = data;
68
61
  if (!particle.effect) {
69
62
  return;
70
63
  }
@@ -72,20 +65,10 @@ export function drawAfterEffect(data) {
72
65
  if (!drawFunc) {
73
66
  return;
74
67
  }
75
- drawFunc({
76
- context,
77
- particle,
78
- radius,
79
- opacity,
80
- delta,
81
- pixelRatio: container.retina.pixelRatio,
82
- fill: particle.shapeFill,
83
- stroke: strokeWidth > minStrokeWidth || !particle.shapeFill,
84
- transformData: { ...transformData },
85
- });
68
+ drawFunc(data);
86
69
  }
87
- export function drawBeforeEffect(data) {
88
- const { container, context, particle, radius, opacity, strokeWidth, delta, transformData } = data;
70
+ export function drawBeforeEffect(container, data) {
71
+ const { particle } = data;
89
72
  if (!particle.effect) {
90
73
  return;
91
74
  }
@@ -93,20 +76,10 @@ export function drawBeforeEffect(data) {
93
76
  if (!drawer?.drawBefore) {
94
77
  return;
95
78
  }
96
- drawer.drawBefore({
97
- context,
98
- particle,
99
- radius,
100
- opacity,
101
- delta,
102
- pixelRatio: container.retina.pixelRatio,
103
- fill: particle.shapeFill,
104
- stroke: strokeWidth > minStrokeWidth || !particle.shapeFill,
105
- transformData: { ...transformData },
106
- });
79
+ drawer.drawBefore(data);
107
80
  }
108
- export function drawShape(data) {
109
- const { container, context, particle, radius, opacity, delta, strokeWidth, transformData } = data;
81
+ export function drawShape(container, data) {
82
+ const { context, particle, stroke } = data;
110
83
  if (!particle.shape) {
111
84
  return;
112
85
  }
@@ -115,29 +88,19 @@ export function drawShape(data) {
115
88
  return;
116
89
  }
117
90
  context.beginPath();
118
- drawer.draw({
119
- context,
120
- particle,
121
- radius,
122
- opacity,
123
- delta,
124
- pixelRatio: container.retina.pixelRatio,
125
- fill: particle.shapeFill,
126
- stroke: strokeWidth > minStrokeWidth || !particle.shapeFill,
127
- transformData: { ...transformData },
128
- });
91
+ drawer.draw(data);
129
92
  if (particle.shapeClose) {
130
93
  context.closePath();
131
94
  }
132
- if (strokeWidth > minStrokeWidth) {
95
+ if (stroke) {
133
96
  context.stroke();
134
97
  }
135
98
  if (particle.shapeFill) {
136
99
  context.fill();
137
100
  }
138
101
  }
139
- export function drawShapeAfterDraw(data) {
140
- const { container, context, particle, radius, opacity, strokeWidth, delta, transformData } = data;
102
+ export function drawShapeAfterDraw(container, data) {
103
+ const { particle } = data;
141
104
  if (!particle.shape) {
142
105
  return;
143
106
  }
@@ -145,20 +108,10 @@ export function drawShapeAfterDraw(data) {
145
108
  if (!drawer?.afterDraw) {
146
109
  return;
147
110
  }
148
- drawer.afterDraw({
149
- context,
150
- particle,
151
- radius,
152
- opacity,
153
- delta,
154
- pixelRatio: container.retina.pixelRatio,
155
- fill: particle.shapeFill,
156
- stroke: strokeWidth > minStrokeWidth || !particle.shapeFill,
157
- transformData: { ...transformData },
158
- });
111
+ drawer.afterDraw(data);
159
112
  }
160
- export function drawShapeBeforeDraw(data) {
161
- const { container, context, particle, radius, opacity, strokeWidth, delta, transformData } = data;
113
+ export function drawShapeBeforeDraw(container, data) {
114
+ const { particle } = data;
162
115
  if (!particle.shape) {
163
116
  return;
164
117
  }
@@ -166,17 +119,7 @@ export function drawShapeBeforeDraw(data) {
166
119
  if (!drawer?.beforeDraw) {
167
120
  return;
168
121
  }
169
- drawer.beforeDraw({
170
- context,
171
- particle,
172
- radius,
173
- opacity,
174
- delta,
175
- pixelRatio: container.retina.pixelRatio,
176
- fill: particle.shapeFill,
177
- stroke: strokeWidth > minStrokeWidth || !particle.shapeFill,
178
- transformData: { ...transformData },
179
- });
122
+ drawer.beforeDraw(data);
180
123
  }
181
124
  export function drawPlugin(context, plugin, delta) {
182
125
  if (!plugin.draw) {
@@ -3,6 +3,19 @@ import { decayOffset, defaultLoops, defaultOpacity, defaultRgbMin, defaultTime,
3
3
  import { isArray, isString } from "./TypeUtils.js";
4
4
  import { AnimationStatus } from "../Enums/AnimationStatus.js";
5
5
  import { itemFromArray } from "./Utils.js";
6
+ const styleCache = new Map(), maxCacheSize = 1000, firstIndex = 0, rgbFixedPrecision = 2, hslFixedPrecision = 2;
7
+ function getCachedStyle(key, generator) {
8
+ let cached = styleCache.get(key);
9
+ if (!cached) {
10
+ cached = generator();
11
+ if (styleCache.size >= maxCacheSize) {
12
+ const keysToDelete = [...styleCache.keys()].slice(firstIndex, maxCacheSize * half);
13
+ keysToDelete.forEach(k => styleCache.delete(k));
14
+ }
15
+ styleCache.set(key, cached);
16
+ }
17
+ return cached;
18
+ }
6
19
  function stringToRgba(engine, input) {
7
20
  if (!input) {
8
21
  return;
@@ -155,7 +168,8 @@ export function getRandomRgbColor(min) {
155
168
  };
156
169
  }
157
170
  export function getStyleFromRgb(color, hdr, opacity) {
158
- return hdr ? getHdrStyleFromRgb(color, opacity) : getSdrStyleFromRgb(color, opacity);
171
+ const op = opacity ?? defaultOpacity, key = `rgb-${color.r.toFixed(rgbFixedPrecision)}-${color.g.toFixed(rgbFixedPrecision)}-${color.b.toFixed(rgbFixedPrecision)}-${hdr ? "hdr" : "sdr"}-${op.toString()}`;
172
+ return getCachedStyle(key, () => (hdr ? getHdrStyleFromRgb(color, opacity) : getSdrStyleFromRgb(color, opacity)));
159
173
  }
160
174
  function getHdrStyleFromRgb(color, opacity) {
161
175
  return `color(display-p3 ${(color.r / rgbMax).toString()} ${(color.g / rgbMax).toString()} ${(color.b / rgbMax).toString()} / ${(opacity ?? defaultOpacity).toString()})`;
@@ -164,7 +178,8 @@ function getSdrStyleFromRgb(color, opacity) {
164
178
  return `rgba(${color.r.toString()}, ${color.g.toString()}, ${color.b.toString()}, ${(opacity ?? defaultOpacity).toString()})`;
165
179
  }
166
180
  export function getStyleFromHsl(color, hdr, opacity) {
167
- return hdr ? getHdrStyleFromHsl(color, opacity) : getSdrStyleFromHsl(color, opacity);
181
+ const op = opacity ?? defaultOpacity, key = `hsl-${color.h.toFixed(hslFixedPrecision)}-${color.s.toFixed(hslFixedPrecision)}-${color.l.toFixed(hslFixedPrecision)}-${hdr ? "hdr" : "sdr"}-${op.toString()}`;
182
+ return getCachedStyle(key, () => (hdr ? getHdrStyleFromHsl(color, opacity) : getSdrStyleFromHsl(color, opacity)));
168
183
  }
169
184
  function getHdrStyleFromHsl(color, opacity) {
170
185
  return getHdrStyleFromRgb(hslToRgb(color), opacity);
@@ -137,9 +137,10 @@ export function calcPositionOrRandomFromSizeRanged(data) {
137
137
  return calcPositionOrRandomFromSize({ size: data.size, position });
138
138
  }
139
139
  export function calcExactPositionOrRandomFromSize(data) {
140
+ const { position, size } = data;
140
141
  return {
141
- x: data.position?.x ?? getRandom() * data.size.width,
142
- y: data.position?.y ?? getRandom() * data.size.height,
142
+ x: position?.x ?? getRandom() * size.width,
143
+ y: position?.y ?? getRandom() * size.height,
143
144
  };
144
145
  }
145
146
  export function calcExactPositionOrRandomFromSizeRanged(data) {