@efxlab/motion-canvas-2d 4.0.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 (455) hide show
  1. package/editor/index.css +40 -0
  2. package/editor/index.js +525 -0
  3. package/editor/index.js.map +1 -0
  4. package/lib/code/CodeCursor.d.ts +84 -0
  5. package/lib/code/CodeCursor.d.ts.map +1 -0
  6. package/lib/code/CodeCursor.js +315 -0
  7. package/lib/code/CodeDiffer.d.ts +28 -0
  8. package/lib/code/CodeDiffer.d.ts.map +1 -0
  9. package/lib/code/CodeDiffer.js +51 -0
  10. package/lib/code/CodeFragment.d.ts +42 -0
  11. package/lib/code/CodeFragment.d.ts.map +1 -0
  12. package/lib/code/CodeFragment.js +72 -0
  13. package/lib/code/CodeHighlighter.d.ts +69 -0
  14. package/lib/code/CodeHighlighter.d.ts.map +1 -0
  15. package/lib/code/CodeHighlighter.js +2 -0
  16. package/lib/code/CodeMetrics.d.ts +11 -0
  17. package/lib/code/CodeMetrics.d.ts.map +1 -0
  18. package/lib/code/CodeMetrics.js +29 -0
  19. package/lib/code/CodeRange.d.ts +43 -0
  20. package/lib/code/CodeRange.d.ts.map +1 -0
  21. package/lib/code/CodeRange.js +185 -0
  22. package/lib/code/CodeScope.d.ts +16 -0
  23. package/lib/code/CodeScope.d.ts.map +1 -0
  24. package/lib/code/CodeScope.js +72 -0
  25. package/lib/code/CodeSelection.d.ts +6 -0
  26. package/lib/code/CodeSelection.d.ts.map +1 -0
  27. package/lib/code/CodeSelection.js +13 -0
  28. package/lib/code/CodeSignal.d.ts +63 -0
  29. package/lib/code/CodeSignal.d.ts.map +1 -0
  30. package/lib/code/CodeSignal.js +206 -0
  31. package/lib/code/CodeTokenizer.d.ts +8 -0
  32. package/lib/code/CodeTokenizer.d.ts.map +1 -0
  33. package/lib/code/CodeTokenizer.js +50 -0
  34. package/lib/code/DefaultHighlightStyle.d.ts +3 -0
  35. package/lib/code/DefaultHighlightStyle.d.ts.map +1 -0
  36. package/lib/code/DefaultHighlightStyle.js +98 -0
  37. package/lib/code/LezerHighlighter.d.ts +22 -0
  38. package/lib/code/LezerHighlighter.d.ts.map +1 -0
  39. package/lib/code/LezerHighlighter.js +91 -0
  40. package/lib/code/diff.d.ts +31 -0
  41. package/lib/code/diff.d.ts.map +1 -0
  42. package/lib/code/diff.js +236 -0
  43. package/lib/code/extractRange.d.ts +17 -0
  44. package/lib/code/extractRange.d.ts.map +1 -0
  45. package/lib/code/extractRange.js +102 -0
  46. package/lib/code/index.d.ts +14 -0
  47. package/lib/code/index.d.ts.map +1 -0
  48. package/lib/code/index.js +14 -0
  49. package/lib/components/Bezier.d.ts +22 -0
  50. package/lib/components/Bezier.d.ts.map +1 -0
  51. package/lib/components/Bezier.js +80 -0
  52. package/lib/components/Camera.d.ts +172 -0
  53. package/lib/components/Camera.d.ts.map +1 -0
  54. package/lib/components/Camera.js +239 -0
  55. package/lib/components/Circle.d.ts +191 -0
  56. package/lib/components/Circle.d.ts.map +1 -0
  57. package/lib/components/Circle.js +178 -0
  58. package/lib/components/Code.d.ts +238 -0
  59. package/lib/components/Code.d.ts.map +1 -0
  60. package/lib/components/Code.js +331 -0
  61. package/lib/components/CodeBlock.d.ts +131 -0
  62. package/lib/components/CodeBlock.d.ts.map +1 -0
  63. package/lib/components/CodeBlock.js +462 -0
  64. package/lib/components/CubicBezier.d.ts +69 -0
  65. package/lib/components/CubicBezier.d.ts.map +1 -0
  66. package/lib/components/CubicBezier.js +81 -0
  67. package/lib/components/Curve.d.ts +202 -0
  68. package/lib/components/Curve.d.ts.map +1 -0
  69. package/lib/components/Curve.js +284 -0
  70. package/lib/components/Grid.d.ts +75 -0
  71. package/lib/components/Grid.d.ts.map +1 -0
  72. package/lib/components/Grid.js +91 -0
  73. package/lib/components/Icon.d.ts +58 -0
  74. package/lib/components/Icon.d.ts.map +1 -0
  75. package/lib/components/Icon.js +58 -0
  76. package/lib/components/Img.d.ts +116 -0
  77. package/lib/components/Img.d.ts.map +1 -0
  78. package/lib/components/Img.js +233 -0
  79. package/lib/components/Knot.d.ts +89 -0
  80. package/lib/components/Knot.d.ts.map +1 -0
  81. package/lib/components/Knot.js +68 -0
  82. package/lib/components/Latex.d.ts +49 -0
  83. package/lib/components/Latex.d.ts.map +1 -0
  84. package/lib/components/Latex.js +206 -0
  85. package/lib/components/Layout.d.ts +423 -0
  86. package/lib/components/Layout.d.ts.map +1 -0
  87. package/lib/components/Layout.js +699 -0
  88. package/lib/components/Line.d.ts +158 -0
  89. package/lib/components/Line.d.ts.map +1 -0
  90. package/lib/components/Line.js +315 -0
  91. package/lib/components/Node.d.ts +843 -0
  92. package/lib/components/Node.d.ts.map +1 -0
  93. package/lib/components/Node.js +1335 -0
  94. package/lib/components/Path.d.ts +18 -0
  95. package/lib/components/Path.d.ts.map +1 -0
  96. package/lib/components/Path.js +96 -0
  97. package/lib/components/Polygon.d.ts +157 -0
  98. package/lib/components/Polygon.d.ts.map +1 -0
  99. package/lib/components/Polygon.js +183 -0
  100. package/lib/components/QuadBezier.d.ts +61 -0
  101. package/lib/components/QuadBezier.d.ts.map +1 -0
  102. package/lib/components/QuadBezier.js +76 -0
  103. package/lib/components/Ray.d.ts +60 -0
  104. package/lib/components/Ray.d.ts.map +1 -0
  105. package/lib/components/Ray.js +95 -0
  106. package/lib/components/Rect.d.ts +112 -0
  107. package/lib/components/Rect.d.ts.map +1 -0
  108. package/lib/components/Rect.js +76 -0
  109. package/lib/components/SVG.d.ts +175 -0
  110. package/lib/components/SVG.d.ts.map +1 -0
  111. package/lib/components/SVG.js +582 -0
  112. package/lib/components/Shape.d.ts +39 -0
  113. package/lib/components/Shape.d.ts.map +1 -0
  114. package/lib/components/Shape.js +134 -0
  115. package/lib/components/Spline.d.ts +87 -0
  116. package/lib/components/Spline.d.ts.map +1 -0
  117. package/lib/components/Spline.js +230 -0
  118. package/lib/components/Txt.d.ts +51 -0
  119. package/lib/components/Txt.d.ts.map +1 -0
  120. package/lib/components/Txt.js +172 -0
  121. package/lib/components/TxtLeaf.d.ts +20 -0
  122. package/lib/components/TxtLeaf.d.ts.map +1 -0
  123. package/lib/components/TxtLeaf.js +185 -0
  124. package/lib/components/Video.d.ts +110 -0
  125. package/lib/components/Video.d.ts.map +1 -0
  126. package/lib/components/Video.js +267 -0
  127. package/lib/components/View2D.d.ts +25 -0
  128. package/lib/components/View2D.d.ts.map +1 -0
  129. package/lib/components/View2D.js +85 -0
  130. package/lib/components/index.d.ts +27 -0
  131. package/lib/components/index.d.ts.map +1 -0
  132. package/lib/components/index.js +27 -0
  133. package/lib/components/types.d.ts +17 -0
  134. package/lib/components/types.d.ts.map +1 -0
  135. package/lib/components/types.js +2 -0
  136. package/lib/curves/ArcSegment.d.ts +26 -0
  137. package/lib/curves/ArcSegment.d.ts.map +1 -0
  138. package/lib/curves/ArcSegment.js +116 -0
  139. package/lib/curves/CircleSegment.d.ts +18 -0
  140. package/lib/curves/CircleSegment.d.ts.map +1 -0
  141. package/lib/curves/CircleSegment.js +60 -0
  142. package/lib/curves/CubicBezierSegment.d.ts +18 -0
  143. package/lib/curves/CubicBezierSegment.d.ts.map +1 -0
  144. package/lib/curves/CubicBezierSegment.js +60 -0
  145. package/lib/curves/CurveDrawingInfo.d.ts +11 -0
  146. package/lib/curves/CurveDrawingInfo.d.ts.map +1 -0
  147. package/lib/curves/CurveDrawingInfo.js +2 -0
  148. package/lib/curves/CurvePoint.d.ts +15 -0
  149. package/lib/curves/CurvePoint.d.ts.map +1 -0
  150. package/lib/curves/CurvePoint.js +2 -0
  151. package/lib/curves/CurveProfile.d.ts +7 -0
  152. package/lib/curves/CurveProfile.d.ts.map +1 -0
  153. package/lib/curves/CurveProfile.js +2 -0
  154. package/lib/curves/KnotInfo.d.ts +12 -0
  155. package/lib/curves/KnotInfo.d.ts.map +1 -0
  156. package/lib/curves/KnotInfo.js +2 -0
  157. package/lib/curves/LineSegment.d.ts +16 -0
  158. package/lib/curves/LineSegment.d.ts.map +1 -0
  159. package/lib/curves/LineSegment.js +51 -0
  160. package/lib/curves/Polynomial.d.ts +118 -0
  161. package/lib/curves/Polynomial.d.ts.map +1 -0
  162. package/lib/curves/Polynomial.js +263 -0
  163. package/lib/curves/Polynomial2D.d.ts +22 -0
  164. package/lib/curves/Polynomial2D.d.ts.map +1 -0
  165. package/lib/curves/Polynomial2D.js +51 -0
  166. package/lib/curves/PolynomialSegment.d.ts +39 -0
  167. package/lib/curves/PolynomialSegment.d.ts.map +1 -0
  168. package/lib/curves/PolynomialSegment.js +88 -0
  169. package/lib/curves/QuadBezierSegment.d.ts +17 -0
  170. package/lib/curves/QuadBezierSegment.d.ts.map +1 -0
  171. package/lib/curves/QuadBezierSegment.js +53 -0
  172. package/lib/curves/Segment.d.ts +9 -0
  173. package/lib/curves/Segment.d.ts.map +1 -0
  174. package/lib/curves/Segment.js +3 -0
  175. package/lib/curves/UniformPolynomialCurveSampler.d.ts +43 -0
  176. package/lib/curves/UniformPolynomialCurveSampler.d.ts.map +1 -0
  177. package/lib/curves/UniformPolynomialCurveSampler.js +74 -0
  178. package/lib/curves/createCurveProfileLerp.d.ts +32 -0
  179. package/lib/curves/createCurveProfileLerp.d.ts.map +1 -0
  180. package/lib/curves/createCurveProfileLerp.js +351 -0
  181. package/lib/curves/getBezierSplineProfile.d.ts +12 -0
  182. package/lib/curves/getBezierSplineProfile.d.ts.map +1 -0
  183. package/lib/curves/getBezierSplineProfile.js +140 -0
  184. package/lib/curves/getCircleProfile.d.ts +4 -0
  185. package/lib/curves/getCircleProfile.d.ts.map +1 -0
  186. package/lib/curves/getCircleProfile.js +44 -0
  187. package/lib/curves/getPathProfile.d.ts +3 -0
  188. package/lib/curves/getPathProfile.d.ts.map +1 -0
  189. package/lib/curves/getPathProfile.js +128 -0
  190. package/lib/curves/getPointAtDistance.d.ts +4 -0
  191. package/lib/curves/getPointAtDistance.d.ts.map +1 -0
  192. package/lib/curves/getPointAtDistance.js +15 -0
  193. package/lib/curves/getPolylineProfile.d.ts +4 -0
  194. package/lib/curves/getPolylineProfile.d.ts.map +1 -0
  195. package/lib/curves/getPolylineProfile.js +58 -0
  196. package/lib/curves/getRectProfile.d.ts +4 -0
  197. package/lib/curves/getRectProfile.d.ts.map +1 -0
  198. package/lib/curves/getRectProfile.js +57 -0
  199. package/lib/curves/index.d.ts +17 -0
  200. package/lib/curves/index.d.ts.map +1 -0
  201. package/lib/curves/index.js +17 -0
  202. package/lib/decorators/canvasStyleSignal.d.ts +5 -0
  203. package/lib/decorators/canvasStyleSignal.d.ts.map +1 -0
  204. package/lib/decorators/canvasStyleSignal.js +12 -0
  205. package/lib/decorators/colorSignal.d.ts +2 -0
  206. package/lib/decorators/colorSignal.d.ts.map +1 -0
  207. package/lib/decorators/colorSignal.js +9 -0
  208. package/lib/decorators/compound.d.ts +27 -0
  209. package/lib/decorators/compound.d.ts.map +1 -0
  210. package/lib/decorators/compound.js +49 -0
  211. package/lib/decorators/computed.d.ts +9 -0
  212. package/lib/decorators/computed.d.ts.map +1 -0
  213. package/lib/decorators/computed.js +18 -0
  214. package/lib/decorators/defaultStyle.d.ts +2 -0
  215. package/lib/decorators/defaultStyle.d.ts.map +1 -0
  216. package/lib/decorators/defaultStyle.js +13 -0
  217. package/lib/decorators/filtersSignal.d.ts +11 -0
  218. package/lib/decorators/filtersSignal.d.ts.map +1 -0
  219. package/lib/decorators/filtersSignal.js +73 -0
  220. package/lib/decorators/index.d.ts +11 -0
  221. package/lib/decorators/index.d.ts.map +1 -0
  222. package/lib/decorators/index.js +11 -0
  223. package/lib/decorators/initializers.d.ts +4 -0
  224. package/lib/decorators/initializers.d.ts.map +1 -0
  225. package/lib/decorators/initializers.js +27 -0
  226. package/lib/decorators/nodeName.d.ts +9 -0
  227. package/lib/decorators/nodeName.d.ts.map +1 -0
  228. package/lib/decorators/nodeName.js +13 -0
  229. package/lib/decorators/signal.d.ts +183 -0
  230. package/lib/decorators/signal.d.ts.map +1 -0
  231. package/lib/decorators/signal.js +285 -0
  232. package/lib/decorators/spacingSignal.d.ts +2 -0
  233. package/lib/decorators/spacingSignal.d.ts.map +1 -0
  234. package/lib/decorators/spacingSignal.js +15 -0
  235. package/lib/decorators/vector2Signal.d.ts +8 -0
  236. package/lib/decorators/vector2Signal.d.ts.map +1 -0
  237. package/lib/decorators/vector2Signal.js +15 -0
  238. package/lib/index.d.ts +9 -0
  239. package/lib/index.d.ts.map +1 -0
  240. package/lib/index.js +9 -0
  241. package/lib/jsx-dev-runtime.d.ts +3 -0
  242. package/lib/jsx-dev-runtime.d.ts.map +1 -0
  243. package/lib/jsx-dev-runtime.js +3 -0
  244. package/lib/jsx-runtime.d.ts +12 -0
  245. package/lib/jsx-runtime.d.ts.map +1 -0
  246. package/lib/jsx-runtime.js +23 -0
  247. package/lib/partials/Filter.d.ts +82 -0
  248. package/lib/partials/Filter.d.ts.map +1 -0
  249. package/lib/partials/Filter.js +137 -0
  250. package/lib/partials/Gradient.d.ts +31 -0
  251. package/lib/partials/Gradient.d.ts.map +1 -0
  252. package/lib/partials/Gradient.js +63 -0
  253. package/lib/partials/Pattern.d.ts +13 -0
  254. package/lib/partials/Pattern.d.ts.map +1 -0
  255. package/lib/partials/Pattern.js +27 -0
  256. package/lib/partials/ShaderConfig.d.ts +81 -0
  257. package/lib/partials/ShaderConfig.d.ts.map +1 -0
  258. package/lib/partials/ShaderConfig.js +25 -0
  259. package/lib/partials/index.d.ts +5 -0
  260. package/lib/partials/index.d.ts.map +1 -0
  261. package/lib/partials/index.js +5 -0
  262. package/lib/partials/types.d.ts +35 -0
  263. package/lib/partials/types.d.ts.map +1 -0
  264. package/lib/partials/types.js +2 -0
  265. package/lib/scenes/Scene2D.d.ts +23 -0
  266. package/lib/scenes/Scene2D.d.ts.map +1 -0
  267. package/lib/scenes/Scene2D.js +152 -0
  268. package/lib/scenes/index.d.ts +4 -0
  269. package/lib/scenes/index.d.ts.map +1 -0
  270. package/lib/scenes/index.js +4 -0
  271. package/lib/scenes/makeScene2D.d.ts +5 -0
  272. package/lib/scenes/makeScene2D.d.ts.map +1 -0
  273. package/lib/scenes/makeScene2D.js +12 -0
  274. package/lib/scenes/useScene2D.d.ts +3 -0
  275. package/lib/scenes/useScene2D.d.ts.map +1 -0
  276. package/lib/scenes/useScene2D.js +5 -0
  277. package/lib/tsconfig.build.tsbuildinfo +1 -0
  278. package/lib/tsconfig.tsbuildinfo +1 -0
  279. package/lib/utils/CanvasUtils.d.ts +21 -0
  280. package/lib/utils/CanvasUtils.d.ts.map +1 -0
  281. package/lib/utils/CanvasUtils.js +138 -0
  282. package/lib/utils/diff.d.ts +31 -0
  283. package/lib/utils/diff.d.ts.map +1 -0
  284. package/lib/utils/diff.js +97 -0
  285. package/lib/utils/index.d.ts +4 -0
  286. package/lib/utils/index.d.ts.map +1 -0
  287. package/lib/utils/index.js +4 -0
  288. package/lib/utils/is.d.ts +8 -0
  289. package/lib/utils/is.d.ts.map +1 -0
  290. package/lib/utils/is.js +10 -0
  291. package/lib/utils/makeSignalExtensions.d.ts +4 -0
  292. package/lib/utils/makeSignalExtensions.d.ts.map +1 -0
  293. package/lib/utils/makeSignalExtensions.js +20 -0
  294. package/lib/utils/withDefaults.d.ts +20 -0
  295. package/lib/utils/withDefaults.d.ts.map +1 -0
  296. package/lib/utils/withDefaults.js +23 -0
  297. package/package.json +54 -0
  298. package/src/editor/NodeInspectorConfig.tsx +76 -0
  299. package/src/editor/PreviewOverlayConfig.tsx +65 -0
  300. package/src/editor/Provider.tsx +109 -0
  301. package/src/editor/SceneGraphTabConfig.tsx +87 -0
  302. package/src/editor/icons/CircleIcon.tsx +7 -0
  303. package/src/editor/icons/CodeBlockIcon.tsx +8 -0
  304. package/src/editor/icons/CurveIcon.tsx +7 -0
  305. package/src/editor/icons/GridIcon.tsx +7 -0
  306. package/src/editor/icons/IconMap.ts +35 -0
  307. package/src/editor/icons/ImgIcon.tsx +8 -0
  308. package/src/editor/icons/LayoutIcon.tsx +9 -0
  309. package/src/editor/icons/LineIcon.tsx +7 -0
  310. package/src/editor/icons/NodeIcon.tsx +7 -0
  311. package/src/editor/icons/RayIcon.tsx +7 -0
  312. package/src/editor/icons/RectIcon.tsx +7 -0
  313. package/src/editor/icons/ShapeIcon.tsx +7 -0
  314. package/src/editor/icons/TxtIcon.tsx +8 -0
  315. package/src/editor/icons/VideoIcon.tsx +7 -0
  316. package/src/editor/icons/View2DIcon.tsx +10 -0
  317. package/src/editor/index.css +0 -0
  318. package/src/editor/index.ts +19 -0
  319. package/src/editor/shortcuts.ts +27 -0
  320. package/src/editor/tree/DetachedRoot.tsx +27 -0
  321. package/src/editor/tree/NodeElement.tsx +72 -0
  322. package/src/editor/tree/TreeElement.tsx +70 -0
  323. package/src/editor/tree/TreeRoot.tsx +10 -0
  324. package/src/editor/tree/ViewRoot.tsx +20 -0
  325. package/src/editor/tree/index.module.scss +45 -0
  326. package/src/editor/tree/index.ts +4 -0
  327. package/src/editor/tree/navigation.ts +145 -0
  328. package/src/editor/tsconfig.build.json +5 -0
  329. package/src/editor/tsconfig.json +12 -0
  330. package/src/editor/tsdoc.json +4 -0
  331. package/src/editor/utils/SignalSet.ts +37 -0
  332. package/src/editor/utils/index.ts +1 -0
  333. package/src/editor/vite-env.d.ts +1 -0
  334. package/src/lib/code/CodeCursor.ts +468 -0
  335. package/src/lib/code/CodeDiffer.ts +77 -0
  336. package/src/lib/code/CodeFragment.ts +96 -0
  337. package/src/lib/code/CodeHighlighter.ts +73 -0
  338. package/src/lib/code/CodeMetrics.ts +47 -0
  339. package/src/lib/code/CodeRange.test.ts +113 -0
  340. package/src/lib/code/CodeRange.ts +222 -0
  341. package/src/lib/code/CodeScope.ts +100 -0
  342. package/src/lib/code/CodeSelection.ts +28 -0
  343. package/src/lib/code/CodeSignal.ts +348 -0
  344. package/src/lib/code/CodeTokenizer.ts +54 -0
  345. package/src/lib/code/DefaultHighlightStyle.ts +98 -0
  346. package/src/lib/code/LezerHighlighter.ts +113 -0
  347. package/src/lib/code/diff.test.ts +311 -0
  348. package/src/lib/code/diff.ts +319 -0
  349. package/src/lib/code/extractRange.ts +125 -0
  350. package/src/lib/code/index.ts +13 -0
  351. package/src/lib/components/Bezier.ts +103 -0
  352. package/src/lib/components/Camera.ts +359 -0
  353. package/src/lib/components/Circle.ts +269 -0
  354. package/src/lib/components/Code.ts +532 -0
  355. package/src/lib/components/CodeBlock.ts +581 -0
  356. package/src/lib/components/CubicBezier.ts +115 -0
  357. package/src/lib/components/Curve.ts +455 -0
  358. package/src/lib/components/Grid.ts +134 -0
  359. package/src/lib/components/Icon.ts +95 -0
  360. package/src/lib/components/Img.ts +305 -0
  361. package/src/lib/components/Knot.ts +156 -0
  362. package/src/lib/components/Latex.ts +249 -0
  363. package/src/lib/components/Layout.ts +1071 -0
  364. package/src/lib/components/Line.ts +394 -0
  365. package/src/lib/components/Node.ts +1949 -0
  366. package/src/lib/components/Path.ts +132 -0
  367. package/src/lib/components/Polygon.ts +238 -0
  368. package/src/lib/components/QuadBezier.ts +103 -0
  369. package/src/lib/components/Ray.ts +126 -0
  370. package/src/lib/components/Rect.ts +186 -0
  371. package/src/lib/components/SVG.ts +788 -0
  372. package/src/lib/components/Shape.ts +146 -0
  373. package/src/lib/components/Spline.ts +318 -0
  374. package/src/lib/components/Txt.test.tsx +81 -0
  375. package/src/lib/components/Txt.ts +204 -0
  376. package/src/lib/components/TxtLeaf.ts +210 -0
  377. package/src/lib/components/Video.ts +368 -0
  378. package/src/lib/components/View2D.ts +85 -0
  379. package/src/lib/components/__logs__/image-without-source.md +17 -0
  380. package/src/lib/components/__logs__/line-without-points.md +30 -0
  381. package/src/lib/components/__logs__/reactive-playback-rate.md +21 -0
  382. package/src/lib/components/__logs__/spline-with-insufficient-knots.md +24 -0
  383. package/src/lib/components/__tests__/children.test.tsx +142 -0
  384. package/src/lib/components/__tests__/clone.test.tsx +126 -0
  385. package/src/lib/components/__tests__/generatorTest.ts +27 -0
  386. package/src/lib/components/__tests__/mockScene2D.ts +50 -0
  387. package/src/lib/components/__tests__/query.test.tsx +122 -0
  388. package/src/lib/components/__tests__/state.test.tsx +60 -0
  389. package/src/lib/components/index.ts +26 -0
  390. package/src/lib/components/types.ts +35 -0
  391. package/src/lib/curves/ArcSegment.ts +155 -0
  392. package/src/lib/curves/CircleSegment.ts +77 -0
  393. package/src/lib/curves/CubicBezierSegment.ts +78 -0
  394. package/src/lib/curves/CurveDrawingInfo.ts +11 -0
  395. package/src/lib/curves/CurvePoint.ts +15 -0
  396. package/src/lib/curves/CurveProfile.ts +7 -0
  397. package/src/lib/curves/KnotInfo.ts +10 -0
  398. package/src/lib/curves/LineSegment.ts +62 -0
  399. package/src/lib/curves/Polynomial.ts +355 -0
  400. package/src/lib/curves/Polynomial2D.ts +62 -0
  401. package/src/lib/curves/PolynomialSegment.ts +124 -0
  402. package/src/lib/curves/QuadBezierSegment.ts +64 -0
  403. package/src/lib/curves/Segment.ts +17 -0
  404. package/src/lib/curves/UniformPolynomialCurveSampler.ts +93 -0
  405. package/src/lib/curves/createCurveProfileLerp.ts +471 -0
  406. package/src/lib/curves/getBezierSplineProfile.ts +227 -0
  407. package/src/lib/curves/getCircleProfile.ts +86 -0
  408. package/src/lib/curves/getPathProfile.ts +177 -0
  409. package/src/lib/curves/getPointAtDistance.ts +21 -0
  410. package/src/lib/curves/getPolylineProfile.test.ts +21 -0
  411. package/src/lib/curves/getPolylineProfile.ts +88 -0
  412. package/src/lib/curves/getRectProfile.ts +138 -0
  413. package/src/lib/curves/index.ts +16 -0
  414. package/src/lib/decorators/canvasStyleSignal.ts +15 -0
  415. package/src/lib/decorators/colorSignal.ts +9 -0
  416. package/src/lib/decorators/compound.ts +85 -0
  417. package/src/lib/decorators/computed.ts +18 -0
  418. package/src/lib/decorators/defaultStyle.ts +15 -0
  419. package/src/lib/decorators/filtersSignal.ts +133 -0
  420. package/src/lib/decorators/index.ts +10 -0
  421. package/src/lib/decorators/initializers.ts +34 -0
  422. package/src/lib/decorators/nodeName.ts +13 -0
  423. package/src/lib/decorators/signal.test.ts +89 -0
  424. package/src/lib/decorators/signal.ts +348 -0
  425. package/src/lib/decorators/spacingSignal.ts +15 -0
  426. package/src/lib/decorators/vector2Signal.ts +35 -0
  427. package/src/lib/globals.d.ts +3 -0
  428. package/src/lib/index.ts +8 -0
  429. package/src/lib/jsx-dev-runtime.ts +2 -0
  430. package/src/lib/jsx-runtime.ts +45 -0
  431. package/src/lib/parse-svg-path.d.ts +14 -0
  432. package/src/lib/partials/Filter.ts +185 -0
  433. package/src/lib/partials/Gradient.ts +103 -0
  434. package/src/lib/partials/Pattern.ts +35 -0
  435. package/src/lib/partials/ShaderConfig.ts +122 -0
  436. package/src/lib/partials/index.ts +4 -0
  437. package/src/lib/partials/types.ts +58 -0
  438. package/src/lib/scenes/Scene2D.ts +195 -0
  439. package/src/lib/scenes/index.ts +3 -0
  440. package/src/lib/scenes/makeScene2D.ts +19 -0
  441. package/src/lib/scenes/useScene2D.ts +6 -0
  442. package/src/lib/tsconfig.build.json +12 -0
  443. package/src/lib/tsconfig.json +14 -0
  444. package/src/lib/tsdoc.json +4 -0
  445. package/src/lib/utils/CanvasUtils.ts +306 -0
  446. package/src/lib/utils/diff.test.ts +453 -0
  447. package/src/lib/utils/diff.ts +148 -0
  448. package/src/lib/utils/index.ts +3 -0
  449. package/src/lib/utils/is.ts +11 -0
  450. package/src/lib/utils/makeSignalExtensions.ts +29 -0
  451. package/src/lib/utils/withDefaults.tsx +26 -0
  452. package/src/tsconfig.base.json +18 -0
  453. package/src/tsconfig.build.json +8 -0
  454. package/src/tsconfig.json +5 -0
  455. package/tsconfig.project.json +7 -0
@@ -0,0 +1,1949 @@
1
+ import {
2
+ BBox,
3
+ ColorSignal,
4
+ DependencyContext,
5
+ PossibleColor,
6
+ PossibleSpacing,
7
+ PossibleVector2,
8
+ Promisable,
9
+ ReferenceReceiver,
10
+ Signal,
11
+ SignalValue,
12
+ SimpleSignal,
13
+ SimpleVector2Signal,
14
+ SpacingSignal,
15
+ ThreadGenerator,
16
+ TimingFunction,
17
+ UNIFORM_DESTINATION_MATRIX,
18
+ UNIFORM_SOURCE_MATRIX,
19
+ UNIFORM_TIME,
20
+ Vector2,
21
+ Vector2Signal,
22
+ all,
23
+ clamp,
24
+ createSignal,
25
+ easeInOutCubic,
26
+ isReactive,
27
+ modify,
28
+ threadable,
29
+ transformAngle,
30
+ transformScalar,
31
+ unwrap,
32
+ useLogger,
33
+ } from '@efxlab/motion-canvas-core';
34
+ import {
35
+ NODE_NAME,
36
+ cloneable,
37
+ colorSignal,
38
+ computed,
39
+ getPropertiesOf,
40
+ initial,
41
+ initializeSignals,
42
+ inspectable,
43
+ nodeName,
44
+ parser,
45
+ signal,
46
+ vector2Signal,
47
+ wrapper,
48
+ } from '../decorators';
49
+ import {FiltersSignal, filtersSignal} from '../decorators/filtersSignal';
50
+ import {spacingSignal} from '../decorators/spacingSignal';
51
+ import {Filter} from '../partials';
52
+ import {
53
+ PossibleShaderConfig,
54
+ ShaderConfig,
55
+ parseShader,
56
+ } from '../partials/ShaderConfig';
57
+ import {useScene2D} from '../scenes/useScene2D';
58
+ import {drawLine} from '../utils';
59
+ import type {View2D} from './View2D';
60
+ import type {ComponentChild, ComponentChildren, NodeConstructor} from './types';
61
+
62
+ export type NodeState = NodeProps & Record<string, any>;
63
+
64
+ export interface NodeProps {
65
+ ref?: ReferenceReceiver<any>;
66
+ children?: SignalValue<ComponentChildren>;
67
+ /**
68
+ * @deprecated Use {@link children} instead.
69
+ */
70
+ spawner?: SignalValue<ComponentChildren>;
71
+ key?: string;
72
+
73
+ x?: SignalValue<number>;
74
+ y?: SignalValue<number>;
75
+ position?: SignalValue<PossibleVector2>;
76
+ rotation?: SignalValue<number>;
77
+ scaleX?: SignalValue<number>;
78
+ scaleY?: SignalValue<number>;
79
+ scale?: SignalValue<PossibleVector2>;
80
+ skewX?: SignalValue<number>;
81
+ skewY?: SignalValue<number>;
82
+ skew?: SignalValue<PossibleVector2>;
83
+ zIndex?: SignalValue<number>;
84
+
85
+ opacity?: SignalValue<number>;
86
+ filters?: SignalValue<Filter[]>;
87
+
88
+ shadowColor?: SignalValue<PossibleColor>;
89
+ shadowBlur?: SignalValue<number>;
90
+ shadowOffsetX?: SignalValue<number>;
91
+ shadowOffsetY?: SignalValue<number>;
92
+ shadowOffset?: SignalValue<PossibleVector2>;
93
+
94
+ cache?: SignalValue<boolean>;
95
+ /**
96
+ * {@inheritDoc Node.cachePadding}
97
+ */
98
+ cachePaddingTop?: SignalValue<number>;
99
+ /**
100
+ * {@inheritDoc Node.cachePadding}
101
+ */
102
+ cachePaddingBottom?: SignalValue<number>;
103
+ /**
104
+ * {@inheritDoc Node.cachePadding}
105
+ */
106
+ cachePaddingLeft?: SignalValue<number>;
107
+ /**
108
+ * {@inheritDoc Node.cachePadding}
109
+ */
110
+ cachePaddingRight?: SignalValue<number>;
111
+ /**
112
+ * {@inheritDoc Node.cachePadding}
113
+ */
114
+ cachePadding?: SignalValue<PossibleSpacing>;
115
+
116
+ composite?: SignalValue<boolean>;
117
+ compositeOperation?: SignalValue<GlobalCompositeOperation>;
118
+ /**
119
+ * @experimental
120
+ */
121
+ shaders?: PossibleShaderConfig;
122
+ }
123
+
124
+ @nodeName('Node')
125
+ export class Node implements Promisable<Node> {
126
+ /**
127
+ * @internal
128
+ */
129
+ declare public readonly [NODE_NAME]: string;
130
+ declare public isClass: boolean;
131
+
132
+ /**
133
+ * Represents the position of this node in local space of its parent.
134
+ *
135
+ * @example
136
+ * Initializing the position:
137
+ * ```tsx
138
+ * // with a possible vector:
139
+ * <Node position={[1, 2]} />
140
+ * // with individual components:
141
+ * <Node x={1} y={2} />
142
+ * ```
143
+ *
144
+ * Accessing the position:
145
+ * ```tsx
146
+ * // retrieving the vector:
147
+ * const position = node.position();
148
+ * // retrieving an individual component:
149
+ * const x = node.position.x();
150
+ * ```
151
+ *
152
+ * Setting the position:
153
+ * ```tsx
154
+ * // with a possible vector:
155
+ * node.position([1, 2]);
156
+ * node.position(() => [1, 2]);
157
+ * // with individual components:
158
+ * node.position.x(1);
159
+ * node.position.x(() => 1);
160
+ * ```
161
+ */
162
+ @vector2Signal()
163
+ declare public readonly position: Vector2Signal<this>;
164
+
165
+ public get x() {
166
+ return this.position.x as SimpleSignal<number, this>;
167
+ }
168
+ public get y() {
169
+ return this.position.y as SimpleSignal<number, this>;
170
+ }
171
+
172
+ /**
173
+ * A helper signal for operating on the position in world space.
174
+ *
175
+ * @remarks
176
+ * Retrieving the position using this signal returns the position in world
177
+ * space. Similarly, setting the position using this signal transforms the
178
+ * new value to local space.
179
+ *
180
+ * If the new value is a function, the position of this node will be
181
+ * continuously updated to always match the position returned by the function.
182
+ * This can be useful to "pin" the node in a specific place or to make it
183
+ * follow another node's position.
184
+ *
185
+ * Unlike {@link position}, this signal is not compound - it doesn't contain
186
+ * separate signals for the `x` and `y` components.
187
+ */
188
+ @wrapper(Vector2)
189
+ @cloneable(false)
190
+ @signal()
191
+ declare public readonly absolutePosition: SimpleVector2Signal<this>;
192
+
193
+ protected getAbsolutePosition(): Vector2 {
194
+ return new Vector2(this.parentToWorld().transformPoint(this.position()));
195
+ }
196
+
197
+ protected setAbsolutePosition(value: SignalValue<PossibleVector2>) {
198
+ this.position(
199
+ modify(value, unwrapped =>
200
+ new Vector2(unwrapped).transformAsPoint(this.worldToParent()),
201
+ ),
202
+ );
203
+ }
204
+
205
+ /**
206
+ * Represents the rotation (in degrees) of this node relative to its parent.
207
+ */
208
+ @initial(0)
209
+ @signal()
210
+ declare public readonly rotation: SimpleSignal<number, this>;
211
+
212
+ /**
213
+ * A helper signal for operating on the rotation in world space.
214
+ *
215
+ * @remarks
216
+ * Retrieving the rotation using this signal returns the rotation in world
217
+ * space. Similarly, setting the rotation using this signal transforms the
218
+ * new value to local space.
219
+ *
220
+ * If the new value is a function, the rotation of this node will be
221
+ * continuously updated to always match the rotation returned by the function.
222
+ */
223
+ @cloneable(false)
224
+ @signal()
225
+ declare public readonly absoluteRotation: SimpleSignal<number, this>;
226
+
227
+ protected getAbsoluteRotation() {
228
+ const matrix = this.localToWorld();
229
+ return Vector2.degrees(matrix.m11, matrix.m12);
230
+ }
231
+
232
+ protected setAbsoluteRotation(value: SignalValue<number>) {
233
+ this.rotation(
234
+ modify(value, unwrapped =>
235
+ transformAngle(unwrapped, this.worldToParent()),
236
+ ),
237
+ );
238
+ }
239
+
240
+ /**
241
+ * Represents the scale of this node in local space of its parent.
242
+ *
243
+ * @example
244
+ * Initializing the scale:
245
+ * ```tsx
246
+ * // with a possible vector:
247
+ * <Node scale={[1, 2]} />
248
+ * // with individual components:
249
+ * <Node scaleX={1} scaleY={2} />
250
+ * ```
251
+ *
252
+ * Accessing the scale:
253
+ * ```tsx
254
+ * // retrieving the vector:
255
+ * const scale = node.scale();
256
+ * // retrieving an individual component:
257
+ * const scaleX = node.scale.x();
258
+ * ```
259
+ *
260
+ * Setting the scale:
261
+ * ```tsx
262
+ * // with a possible vector:
263
+ * node.scale([1, 2]);
264
+ * node.scale(() => [1, 2]);
265
+ * // with individual components:
266
+ * node.scale.x(1);
267
+ * node.scale.x(() => 1);
268
+ * ```
269
+ */
270
+ @initial(Vector2.one)
271
+ @vector2Signal('scale')
272
+ declare public readonly scale: Vector2Signal<this>;
273
+
274
+ /**
275
+ * Represents the skew of this node in local space of its parent.
276
+ *
277
+ * @example
278
+ * Initializing the skew:
279
+ * ```tsx
280
+ * // with a possible vector:
281
+ * <Node skew={[40, 20]} />
282
+ * // with individual components:
283
+ * <Node skewX={40} skewY={20} />
284
+ * ```
285
+ *
286
+ * Accessing the skew:
287
+ * ```tsx
288
+ * // retrieving the vector:
289
+ * const skew = node.skew();
290
+ * // retrieving an individual component:
291
+ * const skewX = node.skew.x();
292
+ * ```
293
+ *
294
+ * Setting the skew:
295
+ * ```tsx
296
+ * // with a possible vector:
297
+ * node.skew([40, 20]);
298
+ * node.skew(() => [40, 20]);
299
+ * // with individual components:
300
+ * node.skew.x(40);
301
+ * node.skew.x(() => 40);
302
+ * ```
303
+ */
304
+ @initial(Vector2.zero)
305
+ @vector2Signal('skew')
306
+ declare public readonly skew: Vector2Signal<this>;
307
+
308
+ /**
309
+ * A helper signal for operating on the scale in world space.
310
+ *
311
+ * @remarks
312
+ * Retrieving the scale using this signal returns the scale in world space.
313
+ * Similarly, setting the scale using this signal transforms the new value to
314
+ * local space.
315
+ *
316
+ * If the new value is a function, the scale of this node will be continuously
317
+ * updated to always match the position returned by the function.
318
+ *
319
+ * Unlike {@link scale}, this signal is not compound - it doesn't contain
320
+ * separate signals for the `x` and `y` components.
321
+ */
322
+ @wrapper(Vector2)
323
+ @cloneable(false)
324
+ @signal()
325
+ declare public readonly absoluteScale: SimpleVector2Signal<this>;
326
+
327
+ protected getAbsoluteScale(): Vector2 {
328
+ const matrix = this.localToWorld();
329
+ return new Vector2(
330
+ Vector2.magnitude(matrix.m11, matrix.m12),
331
+ Vector2.magnitude(matrix.m21, matrix.m22),
332
+ );
333
+ }
334
+
335
+ protected setAbsoluteScale(value: SignalValue<PossibleVector2>) {
336
+ this.scale(
337
+ modify(value, unwrapped => this.getRelativeScale(new Vector2(unwrapped))),
338
+ );
339
+ }
340
+
341
+ private getRelativeScale(scale: Vector2): Vector2 {
342
+ const parentScale = this.parent()?.absoluteScale() ?? Vector2.one;
343
+ return scale.div(parentScale);
344
+ }
345
+
346
+ @initial(0)
347
+ @signal()
348
+ declare public readonly zIndex: SimpleSignal<number, this>;
349
+
350
+ @initial(false)
351
+ @signal()
352
+ declare public readonly cache: SimpleSignal<boolean, this>;
353
+
354
+ /**
355
+ * Controls the padding of the cached canvas used by this node.
356
+ *
357
+ * @remarks
358
+ * By default, the size of the cache is determined based on the bounding box
359
+ * of the node and its children. That includes effects such as stroke or
360
+ * shadow. This property can be used to expand the cache area further.
361
+ * Usually used to account for custom effects created by {@link shaders}.
362
+ */
363
+ @spacingSignal('cachePadding')
364
+ declare public readonly cachePadding: SpacingSignal<this>;
365
+
366
+ @initial(false)
367
+ @signal()
368
+ declare public readonly composite: SimpleSignal<boolean, this>;
369
+
370
+ @initial('source-over')
371
+ @signal()
372
+ declare public readonly compositeOperation: SimpleSignal<
373
+ GlobalCompositeOperation,
374
+ this
375
+ >;
376
+
377
+ private readonly compositeOverride = createSignal(0);
378
+
379
+ @threadable()
380
+ protected *tweenCompositeOperation(
381
+ value: SignalValue<GlobalCompositeOperation>,
382
+ time: number,
383
+ timingFunction: TimingFunction,
384
+ ) {
385
+ const nextValue = unwrap(value);
386
+ if (nextValue === 'source-over') {
387
+ yield* this.compositeOverride(1, time, timingFunction);
388
+ this.compositeOverride(0);
389
+ this.compositeOperation(nextValue);
390
+ } else {
391
+ this.compositeOperation(nextValue);
392
+ this.compositeOverride(1);
393
+ yield* this.compositeOverride(0, time, timingFunction);
394
+ }
395
+ }
396
+
397
+ /**
398
+ * Represents the opacity of this node in the range 0-1.
399
+ *
400
+ * @remarks
401
+ * The value is clamped to the range 0-1.
402
+ */
403
+ @initial(1)
404
+ @parser((value: number) => clamp(0, 1, value))
405
+ @signal()
406
+ declare public readonly opacity: SimpleSignal<number, this>;
407
+
408
+ @computed()
409
+ public absoluteOpacity(): number {
410
+ return (this.parent()?.absoluteOpacity() ?? 1) * this.opacity();
411
+ }
412
+
413
+ @filtersSignal()
414
+ declare public readonly filters: FiltersSignal<this>;
415
+
416
+ @initial('#0000')
417
+ @colorSignal()
418
+ declare public readonly shadowColor: ColorSignal<this>;
419
+
420
+ @initial(0)
421
+ @signal()
422
+ declare public readonly shadowBlur: SimpleSignal<number, this>;
423
+
424
+ @vector2Signal('shadowOffset')
425
+ declare public readonly shadowOffset: Vector2Signal<this>;
426
+
427
+ /**
428
+ * @experimental
429
+ */
430
+ @initial([])
431
+ @parser(parseShader)
432
+ @signal()
433
+ declare public readonly shaders: Signal<
434
+ PossibleShaderConfig,
435
+ ShaderConfig[],
436
+ this
437
+ >;
438
+
439
+ @computed()
440
+ protected hasFilters(): boolean {
441
+ return !!this.filters().find(filter => filter.isActive());
442
+ }
443
+
444
+ @computed()
445
+ protected hasShadow() {
446
+ return (
447
+ !!this.shadowColor() &&
448
+ (this.shadowBlur() > 0 ||
449
+ this.shadowOffset.x() !== 0 ||
450
+ this.shadowOffset.y() !== 0)
451
+ );
452
+ }
453
+
454
+ @computed()
455
+ protected filterString(): string {
456
+ let filters = '';
457
+ const matrix = this.compositeToWorld();
458
+ for (const filter of this.filters()) {
459
+ if (filter.isActive()) {
460
+ filters += ' ' + filter.serialize(matrix);
461
+ }
462
+ }
463
+
464
+ return filters;
465
+ }
466
+
467
+ /**
468
+ * @deprecated Use {@link children} instead.
469
+ */
470
+ @inspectable(false)
471
+ @cloneable(false)
472
+ @signal()
473
+ declare protected readonly spawner: SimpleSignal<ComponentChildren, this>;
474
+ protected getSpawner(): ComponentChildren {
475
+ return this.children();
476
+ }
477
+ protected setSpawner(value: SignalValue<ComponentChildren>) {
478
+ this.children(value);
479
+ }
480
+
481
+ @inspectable(false)
482
+ @cloneable(false)
483
+ @signal()
484
+ declare public readonly children: Signal<ComponentChildren, Node[], this>;
485
+ protected setChildren(value: SignalValue<ComponentChildren>) {
486
+ if (this.children.context.raw() === value) {
487
+ return;
488
+ }
489
+
490
+ this.children.context.setter(value);
491
+ if (!isReactive(value)) {
492
+ this.spawnChildren(false, value);
493
+ } else if (!this.hasSpawnedChildren) {
494
+ for (const oldChild of this.realChildren) {
495
+ oldChild.parent(null);
496
+ }
497
+ }
498
+ }
499
+ protected getChildren(): Node[] {
500
+ this.children.context.getter();
501
+ return this.spawnedChildren();
502
+ }
503
+
504
+ @computed()
505
+ protected spawnedChildren(): Node[] {
506
+ const children = this.children.context.getter();
507
+ if (isReactive(this.children.context.raw())) {
508
+ this.spawnChildren(true, children);
509
+ }
510
+ return this.realChildren;
511
+ }
512
+
513
+ @computed()
514
+ protected sortedChildren(): Node[] {
515
+ return [...this.children()].sort((a, b) =>
516
+ Math.sign(a.zIndex() - b.zIndex()),
517
+ );
518
+ }
519
+
520
+ protected view2D: View2D;
521
+ private stateStack: NodeState[] = [];
522
+ protected realChildren: Node[] = [];
523
+ protected hasSpawnedChildren = false;
524
+ private unregister: () => void;
525
+ public readonly parent = createSignal<Node | null>(null);
526
+ public readonly properties = getPropertiesOf(this);
527
+ public readonly key: string;
528
+ public readonly creationStack?: string;
529
+
530
+ public constructor({children, spawner, key, ...rest}: NodeProps) {
531
+ const scene = useScene2D();
532
+ [this.key, this.unregister] = scene.registerNode(this, key);
533
+ this.view2D = scene.getView();
534
+ this.creationStack = new Error().stack;
535
+ initializeSignals(this, rest);
536
+ if (spawner) {
537
+ useLogger().warn({
538
+ message: 'Node.spawner() has been deprecated.',
539
+ remarks: 'Use <code>Node.children()</code> instead.',
540
+ inspect: this.key,
541
+ stack: new Error().stack,
542
+ });
543
+ }
544
+ this.children(spawner ?? children);
545
+ }
546
+
547
+ /**
548
+ * Get the local-to-world matrix for this node.
549
+ *
550
+ * @remarks
551
+ * This matrix transforms vectors from local space of this node to world
552
+ * space.
553
+ *
554
+ * @example
555
+ * Calculate the absolute position of a point located 200 pixels to the right
556
+ * of the node:
557
+ * ```ts
558
+ * const local = new Vector2(0, 200);
559
+ * const world = local.transformAsPoint(node.localToWorld());
560
+ * ```
561
+ */
562
+ @computed()
563
+ public localToWorld(): DOMMatrix {
564
+ const parent = this.parent();
565
+ return parent
566
+ ? parent.localToWorld().multiply(this.localToParent())
567
+ : this.localToParent();
568
+ }
569
+
570
+ /**
571
+ * Get the world-to-local matrix for this node.
572
+ *
573
+ * @remarks
574
+ * This matrix transforms vectors from world space to local space of this
575
+ * node.
576
+ *
577
+ * @example
578
+ * Calculate the position relative to this node for a point located in the
579
+ * top-left corner of the screen:
580
+ * ```ts
581
+ * const world = new Vector2(0, 0);
582
+ * const local = world.transformAsPoint(node.worldToLocal());
583
+ * ```
584
+ */
585
+ @computed()
586
+ public worldToLocal() {
587
+ return this.localToWorld().inverse();
588
+ }
589
+
590
+ /**
591
+ * Get the world-to-parent matrix for this node.
592
+ *
593
+ * @remarks
594
+ * This matrix transforms vectors from world space to local space of this
595
+ * node's parent.
596
+ */
597
+ @computed()
598
+ public worldToParent(): DOMMatrix {
599
+ return this.parent()?.worldToLocal() ?? new DOMMatrix();
600
+ }
601
+
602
+ /**
603
+ * Get the parent-to-world matrix for this node.
604
+ *
605
+ * @remarks
606
+ * This matrix transforms vectors from local space of this node's parent to
607
+ * world space.
608
+ */
609
+ @computed()
610
+ public parentToWorld(): DOMMatrix {
611
+ return this.parent()?.localToWorld() ?? new DOMMatrix();
612
+ }
613
+
614
+ /**
615
+ * Get the local-to-parent matrix for this node.
616
+ *
617
+ * @remarks
618
+ * This matrix transforms vectors from local space of this node to local space
619
+ * of this node's parent.
620
+ */
621
+ @computed()
622
+ public localToParent(): DOMMatrix {
623
+ const matrix = new DOMMatrix();
624
+ matrix.translateSelf(this.x(), this.y());
625
+ matrix.rotateSelf(0, 0, this.rotation());
626
+ matrix.scaleSelf(this.scale.x(), this.scale.y());
627
+ matrix.skewXSelf(this.skew.x());
628
+ matrix.skewYSelf(this.skew.y());
629
+
630
+ return matrix;
631
+ }
632
+
633
+ /**
634
+ * A matrix mapping composite space to world space.
635
+ *
636
+ * @remarks
637
+ * Certain effects such as blur and shadows ignore the current transformation.
638
+ * This matrix can be used to transform their parameters so that the effect
639
+ * appears relative to the closest composite root.
640
+ */
641
+ @computed()
642
+ public compositeToWorld(): DOMMatrix {
643
+ return this.compositeRoot()?.localToWorld() ?? new DOMMatrix();
644
+ }
645
+
646
+ @computed()
647
+ protected compositeRoot(): Node | null {
648
+ if (this.composite()) {
649
+ return this;
650
+ }
651
+
652
+ return this.parent()?.compositeRoot() ?? null;
653
+ }
654
+
655
+ @computed()
656
+ public compositeToLocal() {
657
+ const root = this.compositeRoot();
658
+ if (root) {
659
+ const worldToLocal = this.worldToLocal();
660
+ worldToLocal.m44 = 1;
661
+ return root.localToWorld().multiply(worldToLocal);
662
+ }
663
+ return new DOMMatrix();
664
+ }
665
+
666
+ public view(): View2D {
667
+ return this.view2D;
668
+ }
669
+
670
+ /**
671
+ * Add the given node(s) as the children of this node.
672
+ *
673
+ * @remarks
674
+ * The nodes will be appended at the end of the children list.
675
+ *
676
+ * @example
677
+ * ```tsx
678
+ * const node = <Layout />;
679
+ * node.add(<Rect />);
680
+ * node.add(<Circle />);
681
+ * ```
682
+ * Result:
683
+ * ```mermaid
684
+ * graph TD;
685
+ * layout([Layout])
686
+ * circle([Circle])
687
+ * rect([Rect])
688
+ * layout-->rect;
689
+ * layout-->circle;
690
+ * ```
691
+ *
692
+ * @param node - A node or an array of nodes to append.
693
+ */
694
+ public add(node: ComponentChildren): this {
695
+ return this.insert(node, Infinity);
696
+ }
697
+
698
+ /**
699
+ * Insert the given node(s) at the specified index in the children list.
700
+ *
701
+ * @example
702
+ * ```tsx
703
+ * const node = (
704
+ * <Layout>
705
+ * <Rect />
706
+ * <Circle />
707
+ * </Layout>
708
+ * );
709
+ *
710
+ * node.insert(<Txt />, 1);
711
+ * ```
712
+ *
713
+ * Result:
714
+ * ```mermaid
715
+ * graph TD;
716
+ * layout([Layout])
717
+ * circle([Circle])
718
+ * text([Text])
719
+ * rect([Rect])
720
+ * layout-->rect;
721
+ * layout-->text;
722
+ * layout-->circle;
723
+ * ```
724
+ *
725
+ * @param node - A node or an array of nodes to insert.
726
+ * @param index - An index at which to insert the node(s).
727
+ */
728
+ public insert(node: ComponentChildren, index = 0): this {
729
+ const array: ComponentChild[] = Array.isArray(node) ? node : [node];
730
+ if (array.length === 0) {
731
+ return this;
732
+ }
733
+
734
+ const children = this.children();
735
+ const newChildren = children.slice(0, index);
736
+
737
+ for (const node of array) {
738
+ if (node instanceof Node) {
739
+ newChildren.push(node);
740
+ node.remove();
741
+ node.parent(this);
742
+ }
743
+ }
744
+
745
+ newChildren.push(...children.slice(index));
746
+ this.setParsedChildren(newChildren);
747
+
748
+ return this;
749
+ }
750
+
751
+ /**
752
+ * Remove this node from the tree.
753
+ */
754
+ public remove(): this {
755
+ const current = this.parent();
756
+ if (current === null) {
757
+ return this;
758
+ }
759
+
760
+ current.removeChild(this);
761
+ this.parent(null);
762
+ return this;
763
+ }
764
+
765
+ /**
766
+ * Rearrange this node in relation to its siblings.
767
+ *
768
+ * @remarks
769
+ * Children are rendered starting from the beginning of the children list.
770
+ * We can change the rendering order by rearranging said list.
771
+ *
772
+ * A positive `by` arguments move the node up (it will be rendered on top of
773
+ * the elements it has passed). Negative values move it down.
774
+ *
775
+ * @param by - Number of places by which the node should be moved.
776
+ */
777
+ public move(by = 1): this {
778
+ const parent = this.parent();
779
+ if (by === 0 || !parent) {
780
+ return this;
781
+ }
782
+
783
+ const children = parent.children();
784
+ const newChildren: Node[] = [];
785
+
786
+ if (by > 0) {
787
+ for (let i = 0; i < children.length; i++) {
788
+ const child = children[i];
789
+ if (child === this) {
790
+ const target = i + by;
791
+ for (; i < target && i + 1 < children.length; i++) {
792
+ newChildren[i] = children[i + 1];
793
+ }
794
+ }
795
+ newChildren[i] = child;
796
+ }
797
+ } else {
798
+ for (let i = children.length - 1; i >= 0; i--) {
799
+ const child = children[i];
800
+ if (child === this) {
801
+ const target = i + by;
802
+ for (; i > target && i > 0; i--) {
803
+ newChildren[i] = children[i - 1];
804
+ }
805
+ }
806
+ newChildren[i] = child;
807
+ }
808
+ }
809
+
810
+ parent.setParsedChildren(newChildren);
811
+
812
+ return this;
813
+ }
814
+
815
+ /**
816
+ * Move the node up in relation to its siblings.
817
+ *
818
+ * @remarks
819
+ * The node will exchange places with the sibling right above it (if any) and
820
+ * from then on will be rendered on top of it.
821
+ */
822
+ public moveUp(): this {
823
+ return this.move(1);
824
+ }
825
+
826
+ /**
827
+ * Move the node down in relation to its siblings.
828
+ *
829
+ * @remarks
830
+ * The node will exchange places with the sibling right below it (if any) and
831
+ * from then on will be rendered under it.
832
+ */
833
+ public moveDown(): this {
834
+ return this.move(-1);
835
+ }
836
+
837
+ /**
838
+ * Move the node to the top in relation to its siblings.
839
+ *
840
+ * @remarks
841
+ * The node will be placed at the end of the children list and from then on
842
+ * will be rendered on top of all of its siblings.
843
+ */
844
+ public moveToTop(): this {
845
+ return this.move(Infinity);
846
+ }
847
+
848
+ /**
849
+ * Move the node to the bottom in relation to its siblings.
850
+ *
851
+ * @remarks
852
+ * The node will be placed at the beginning of the children list and from then
853
+ * on will be rendered below all of its siblings.
854
+ */
855
+ public moveToBottom(): this {
856
+ return this.move(-Infinity);
857
+ }
858
+
859
+ /**
860
+ * Move the node to the provided position relative to its siblings.
861
+ *
862
+ * @remarks
863
+ * If the node is getting moved to a lower position, it will be placed below
864
+ * the sibling that's currently at the provided index (if any).
865
+ * If the node is getting moved to a higher position, it will be placed above
866
+ * the sibling that's currently at the provided index (if any).
867
+ *
868
+ * @param index - The index to move the node to.
869
+ */
870
+ public moveTo(index: number): this {
871
+ const parent = this.parent();
872
+ if (!parent) {
873
+ return this;
874
+ }
875
+
876
+ const currentIndex = parent.children().indexOf(this);
877
+ const by = index - currentIndex;
878
+
879
+ return this.move(by);
880
+ }
881
+
882
+ /**
883
+ * Move the node below the provided node in the parent's layout.
884
+ *
885
+ * @remarks
886
+ * The node will be moved below the provided node and from then on will be
887
+ * rendered below it. By default, if the node is already positioned lower than
888
+ * the sibling node, it will not get moved.
889
+ *
890
+ * @param node - The sibling node below which to move.
891
+ * @param directlyBelow - Whether the node should be positioned directly below
892
+ * the sibling. When true, will move the node even if
893
+ * it is already positioned below the sibling.
894
+ */
895
+ public moveBelow(node: Node, directlyBelow = false): this {
896
+ const parent = this.parent();
897
+ if (!parent) {
898
+ return this;
899
+ }
900
+
901
+ if (node.parent() !== parent) {
902
+ useLogger().error(
903
+ "Cannot position nodes relative to each other if they don't belong to the same parent.",
904
+ );
905
+ return this;
906
+ }
907
+
908
+ const children = parent.children();
909
+ const ownIndex = children.indexOf(this);
910
+ const otherIndex = children.indexOf(node);
911
+
912
+ if (!directlyBelow && ownIndex < otherIndex) {
913
+ // Nothing to do if the node is already positioned below the target node.
914
+ // We could move the node so it's directly below the sibling node, but
915
+ // that might suddenly move it on top of other nodes. This is likely
916
+ // not what the user wanted to happen when calling this method.
917
+ return this;
918
+ }
919
+
920
+ const by = otherIndex - ownIndex - 1;
921
+
922
+ return this.move(by);
923
+ }
924
+
925
+ /**
926
+ * Move the node above the provided node in the parent's layout.
927
+ *
928
+ * @remarks
929
+ * The node will be moved above the provided node and from then on will be
930
+ * rendered on top of it. By default, if the node is already positioned
931
+ * higher than the sibling node, it will not get moved.
932
+ *
933
+ * @param node - The sibling node below which to move.
934
+ * @param directlyAbove - Whether the node should be positioned directly above the
935
+ * sibling. When true, will move the node even if it is
936
+ * already positioned above the sibling.
937
+ */
938
+ public moveAbove(node: Node, directlyAbove = false): this {
939
+ const parent = this.parent();
940
+ if (!parent) {
941
+ return this;
942
+ }
943
+
944
+ if (node.parent() !== parent) {
945
+ useLogger().error(
946
+ "Cannot position nodes relative to each other if they don't belong to the same parent.",
947
+ );
948
+ return this;
949
+ }
950
+
951
+ const children = parent.children();
952
+ const ownIndex = children.indexOf(this);
953
+ const otherIndex = children.indexOf(node);
954
+
955
+ if (!directlyAbove && ownIndex > otherIndex) {
956
+ // Nothing to do if the node is already positioned above the target node.
957
+ // We could move the node so it's directly above the sibling node, but
958
+ // that might suddenly move it below other nodes. This is likely not what
959
+ // the user wanted to happen when calling this method.
960
+ return this;
961
+ }
962
+
963
+ const by = otherIndex - ownIndex + 1;
964
+
965
+ return this.move(by);
966
+ }
967
+
968
+ /**
969
+ * Change the parent of this node while keeping the absolute transform.
970
+ *
971
+ * @remarks
972
+ * After performing this operation, the node will stay in the same place
973
+ * visually, but its parent will be changed.
974
+ *
975
+ * @param newParent - The new parent of this node.
976
+ */
977
+ public reparent(newParent: Node): this {
978
+ const position = this.absolutePosition();
979
+ const rotation = this.absoluteRotation();
980
+ const scale = this.absoluteScale();
981
+ newParent.add(this);
982
+ this.absolutePosition(position);
983
+ this.absoluteRotation(rotation);
984
+ this.absoluteScale(scale);
985
+
986
+ return this;
987
+ }
988
+
989
+ /**
990
+ * Remove all children of this node.
991
+ */
992
+ public removeChildren(): this {
993
+ for (const oldChild of this.realChildren) {
994
+ oldChild.parent(null);
995
+ }
996
+ this.setParsedChildren([]);
997
+
998
+ return this;
999
+ }
1000
+
1001
+ /**
1002
+ * Get the current children of this node.
1003
+ *
1004
+ * @remarks
1005
+ * Unlike {@link children}, this method does not have any side effects.
1006
+ * It does not register the `children` signal as a dependency, and it does not
1007
+ * spawn any children. It can be used to safely retrieve the current state of
1008
+ * the scene graph for debugging purposes.
1009
+ */
1010
+ public peekChildren(): readonly Node[] {
1011
+ return this.realChildren;
1012
+ }
1013
+
1014
+ /**
1015
+ * Find all descendants of this node that match the given predicate.
1016
+ *
1017
+ * @param predicate - A function that returns true if the node matches.
1018
+ */
1019
+ public findAll<T extends Node>(predicate: (node: any) => node is T): T[];
1020
+ /**
1021
+ * Find all descendants of this node that match the given predicate.
1022
+ *
1023
+ * @param predicate - A function that returns true if the node matches.
1024
+ */
1025
+ public findAll<T extends Node = Node>(predicate: (node: any) => boolean): T[];
1026
+ public findAll<T extends Node>(predicate: (node: any) => node is T): T[] {
1027
+ const result: T[] = [];
1028
+ const queue = this.reversedChildren();
1029
+ while (queue.length > 0) {
1030
+ const node = queue.pop()!;
1031
+ if (predicate(node)) {
1032
+ result.push(node);
1033
+ }
1034
+ const children = node.children();
1035
+ for (let i = children.length - 1; i >= 0; i--) {
1036
+ queue.push(children[i]);
1037
+ }
1038
+ }
1039
+
1040
+ return result;
1041
+ }
1042
+
1043
+ /**
1044
+ * Find the first descendant of this node that matches the given predicate.
1045
+ *
1046
+ * @param predicate - A function that returns true if the node matches.
1047
+ */
1048
+ public findFirst<T extends Node>(
1049
+ predicate: (node: Node) => node is T,
1050
+ ): T | null;
1051
+ /**
1052
+ * Find the first descendant of this node that matches the given predicate.
1053
+ *
1054
+ * @param predicate - A function that returns true if the node matches.
1055
+ */
1056
+ public findFirst<T extends Node = Node>(
1057
+ predicate: (node: Node) => boolean,
1058
+ ): T | null;
1059
+ public findFirst<T extends Node>(
1060
+ predicate: (node: Node) => node is T,
1061
+ ): T | null {
1062
+ const queue = this.reversedChildren();
1063
+ while (queue.length > 0) {
1064
+ const node = queue.pop()!;
1065
+ if (predicate(node)) {
1066
+ return node;
1067
+ }
1068
+ const children = node.children();
1069
+ for (let i = children.length - 1; i >= 0; i--) {
1070
+ queue.push(children[i]);
1071
+ }
1072
+ }
1073
+
1074
+ return null;
1075
+ }
1076
+
1077
+ /**
1078
+ * Find the last descendant of this node that matches the given predicate.
1079
+ *
1080
+ * @param predicate - A function that returns true if the node matches.
1081
+ */
1082
+ public findLast<T extends Node>(
1083
+ predicate: (node: Node) => node is T,
1084
+ ): T | null;
1085
+ /**
1086
+ * Find the last descendant of this node that matches the given predicate.
1087
+ *
1088
+ * @param predicate - A function that returns true if the node matches.
1089
+ */
1090
+ public findLast<T extends Node = Node>(
1091
+ predicate: (node: Node) => boolean,
1092
+ ): T | null;
1093
+ public findLast<T extends Node>(
1094
+ predicate: (node: Node) => node is T,
1095
+ ): T | null {
1096
+ const search: Node[] = [];
1097
+ const queue = this.reversedChildren();
1098
+
1099
+ while (queue.length > 0) {
1100
+ const node = queue.pop()!;
1101
+ search.push(node);
1102
+ const children = node.children();
1103
+ for (let i = children.length - 1; i >= 0; i--) {
1104
+ queue.push(children[i]);
1105
+ }
1106
+ }
1107
+
1108
+ while (search.length > 0) {
1109
+ const node = search.pop()!;
1110
+ if (predicate(node)) {
1111
+ return node;
1112
+ }
1113
+ }
1114
+
1115
+ return null;
1116
+ }
1117
+
1118
+ /**
1119
+ * Find the closest ancestor of this node that matches the given predicate.
1120
+ *
1121
+ * @param predicate - A function that returns true if the node matches.
1122
+ */
1123
+ public findAncestor<T extends Node>(
1124
+ predicate: (node: Node) => node is T,
1125
+ ): T | null;
1126
+ /**
1127
+ * Find the closest ancestor of this node that matches the given predicate.
1128
+ *
1129
+ * @param predicate - A function that returns true if the node matches.
1130
+ */
1131
+ public findAncestor<T extends Node = Node>(
1132
+ predicate: (node: Node) => boolean,
1133
+ ): T | null;
1134
+ public findAncestor<T extends Node>(
1135
+ predicate: (node: Node) => node is T,
1136
+ ): T | null {
1137
+ let parent: Node | null = this.parent();
1138
+ while (parent) {
1139
+ if (predicate(parent)) {
1140
+ return parent;
1141
+ }
1142
+ parent = parent.parent();
1143
+ }
1144
+
1145
+ return null;
1146
+ }
1147
+
1148
+ /**
1149
+ * Get the nth children cast to the specified type.
1150
+ *
1151
+ * @param index - The index of the child to retrieve.
1152
+ */
1153
+ public childAs<T extends Node = Node>(index: number): T | null {
1154
+ return (this.children()[index] as T) ?? null;
1155
+ }
1156
+
1157
+ /**
1158
+ * Get the children array cast to the specified type.
1159
+ */
1160
+ public childrenAs<T extends Node = Node>(): T[] {
1161
+ return this.children() as T[];
1162
+ }
1163
+
1164
+ /**
1165
+ * Get the parent cast to the specified type.
1166
+ */
1167
+ public parentAs<T extends Node = Node>(): T | null {
1168
+ return (this.parent() as T) ?? null;
1169
+ }
1170
+
1171
+ /**
1172
+ * Prepare this node to be disposed of.
1173
+ *
1174
+ * @remarks
1175
+ * This method is called automatically when a scene is refreshed. It will
1176
+ * be called even if the node is not currently attached to the tree.
1177
+ *
1178
+ * The goal of this method is to clean any external references to allow the
1179
+ * node to be garbage collected.
1180
+ */
1181
+ public dispose() {
1182
+ if (!this.unregister) {
1183
+ return;
1184
+ }
1185
+
1186
+ this.stateStack = [];
1187
+ this.unregister();
1188
+ this.unregister = null!;
1189
+ for (const {signal} of this) {
1190
+ signal?.context.dispose();
1191
+ }
1192
+ for (const child of this.realChildren) {
1193
+ child.dispose();
1194
+ }
1195
+ }
1196
+
1197
+ /**
1198
+ * Create a copy of this node.
1199
+ *
1200
+ * @param customProps - Properties to override.
1201
+ */
1202
+ public clone(customProps: NodeState = {}): this {
1203
+ const props = {...customProps};
1204
+ if (isReactive(this.children.context.raw())) {
1205
+ props.children ??= this.children.context.raw();
1206
+ } else if (this.children().length > 0) {
1207
+ props.children ??= this.children().map(child => child.clone());
1208
+ }
1209
+
1210
+ for (const {key, meta, signal} of this) {
1211
+ if (!meta.cloneable || key in props) continue;
1212
+ if (meta.compound) {
1213
+ for (const [key, property] of meta.compoundEntries) {
1214
+ if (property in props) continue;
1215
+ const component = (<Record<string, SimpleSignal<any>>>(
1216
+ (<unknown>signal)
1217
+ ))[key];
1218
+ if (!component.context.isInitial()) {
1219
+ props[property] = component.context.raw();
1220
+ }
1221
+ }
1222
+ } else if (!signal.context.isInitial()) {
1223
+ props[key] = signal.context.raw();
1224
+ }
1225
+ }
1226
+
1227
+ return this.instantiate(props);
1228
+ }
1229
+
1230
+ /**
1231
+ * Create a copy of this node.
1232
+ *
1233
+ * @remarks
1234
+ * Unlike {@link clone}, a snapshot clone calculates any reactive properties
1235
+ * at the moment of cloning and passes the raw values to the copy.
1236
+ *
1237
+ * @param customProps - Properties to override.
1238
+ */
1239
+ public snapshotClone(customProps: NodeState = {}): this {
1240
+ const props = {
1241
+ ...this.getState(),
1242
+ ...customProps,
1243
+ };
1244
+
1245
+ if (this.children().length > 0) {
1246
+ props.children ??= this.children().map(child => child.snapshotClone());
1247
+ }
1248
+
1249
+ return this.instantiate(props);
1250
+ }
1251
+
1252
+ /**
1253
+ * Create a reactive copy of this node.
1254
+ *
1255
+ * @remarks
1256
+ * A reactive copy has all its properties dynamically updated to match the
1257
+ * source node.
1258
+ *
1259
+ * @param customProps - Properties to override.
1260
+ */
1261
+ public reactiveClone(customProps: NodeState = {}): this {
1262
+ const props = {...customProps};
1263
+ if (this.children().length > 0) {
1264
+ props.children ??= this.children().map(child => child.reactiveClone());
1265
+ }
1266
+
1267
+ for (const {key, meta, signal} of this) {
1268
+ if (!meta.cloneable || key in props) continue;
1269
+ props[key] = () => signal();
1270
+ }
1271
+
1272
+ return this.instantiate(props);
1273
+ }
1274
+
1275
+ /**
1276
+ * Create an instance of this node's class.
1277
+ *
1278
+ * @param props - Properties to pass to the constructor.
1279
+ */
1280
+ public instantiate(props: NodeProps = {}): this {
1281
+ return new (<NodeConstructor<NodeProps, this>>this.constructor)(props);
1282
+ }
1283
+
1284
+ /**
1285
+ * Set the children without parsing them.
1286
+ *
1287
+ * @remarks
1288
+ * This method assumes that the caller took care of parsing the children and
1289
+ * updating the hierarchy.
1290
+ *
1291
+ * @param value - The children to set.
1292
+ */
1293
+ protected setParsedChildren(value: Node[]) {
1294
+ this.children.context.setter(value);
1295
+ this.realChildren = value;
1296
+ }
1297
+
1298
+ protected spawnChildren(reactive: boolean, children: ComponentChildren) {
1299
+ const parsedChildren = this.parseChildren(children);
1300
+
1301
+ const keep = new Set<string>();
1302
+ for (const newChild of parsedChildren) {
1303
+ const current = newChild.parent.context.raw() as Node | null;
1304
+ if (current && current !== this) {
1305
+ current.removeChild(newChild);
1306
+ }
1307
+ keep.add(newChild.key);
1308
+ newChild.parent(this);
1309
+ }
1310
+
1311
+ for (const oldChild of this.realChildren) {
1312
+ if (!keep.has(oldChild.key)) {
1313
+ oldChild.parent(null);
1314
+ }
1315
+ }
1316
+
1317
+ this.hasSpawnedChildren = reactive;
1318
+ this.realChildren = parsedChildren;
1319
+ }
1320
+
1321
+ /**
1322
+ * Parse any `ComponentChildren` into an array of nodes.
1323
+ *
1324
+ * @param children - The children to parse.
1325
+ */
1326
+ protected parseChildren(children: ComponentChildren): Node[] {
1327
+ const result: Node[] = [];
1328
+ const array = Array.isArray(children) ? children : [children];
1329
+ for (const child of array) {
1330
+ if (child instanceof Node) {
1331
+ result.push(child);
1332
+ }
1333
+ }
1334
+
1335
+ return result;
1336
+ }
1337
+
1338
+ /**
1339
+ * Remove the given child.
1340
+ */
1341
+ protected removeChild(child: Node) {
1342
+ this.setParsedChildren(this.children().filter(node => node !== child));
1343
+ }
1344
+
1345
+ /**
1346
+ * Whether this node should be cached or not.
1347
+ */
1348
+ protected requiresCache(): boolean {
1349
+ return (
1350
+ this.cache() ||
1351
+ this.opacity() < 1 ||
1352
+ this.compositeOperation() !== 'source-over' ||
1353
+ this.hasFilters() ||
1354
+ this.hasShadow() ||
1355
+ this.shaders().length > 0
1356
+ );
1357
+ }
1358
+
1359
+ @computed()
1360
+ protected cacheCanvas(): CanvasRenderingContext2D {
1361
+ const canvas = document.createElement('canvas').getContext('2d');
1362
+ if (!canvas) {
1363
+ throw new Error('Could not create a cache canvas');
1364
+ }
1365
+
1366
+ return canvas;
1367
+ }
1368
+
1369
+ /**
1370
+ * Get a cache canvas with the contents of this node rendered onto it.
1371
+ */
1372
+ @computed()
1373
+ protected cachedCanvas() {
1374
+ const context = this.cacheCanvas();
1375
+ const cache = this.worldSpaceCacheBBox();
1376
+ const matrix = this.localToWorld();
1377
+
1378
+ context.canvas.width = cache.width;
1379
+ context.canvas.height = cache.height;
1380
+
1381
+ context.setTransform(
1382
+ matrix.a,
1383
+ matrix.b,
1384
+ matrix.c,
1385
+ matrix.d,
1386
+ matrix.e - cache.x,
1387
+ matrix.f - cache.y,
1388
+ );
1389
+ this.draw(context);
1390
+
1391
+ return context;
1392
+ }
1393
+
1394
+ /**
1395
+ * Get a bounding box for the contents rendered by this node.
1396
+ *
1397
+ * @remarks
1398
+ * The returned bounding box should be in local space.
1399
+ */
1400
+ protected getCacheBBox(): BBox {
1401
+ return new BBox();
1402
+ }
1403
+
1404
+ /**
1405
+ * Get a bounding box for the contents rendered by this node as well
1406
+ * as its children.
1407
+ */
1408
+ @computed()
1409
+ public cacheBBox(): BBox {
1410
+ const cache = this.getCacheBBox();
1411
+ const children = this.children();
1412
+ const padding = this.cachePadding();
1413
+ if (children.length === 0) {
1414
+ return cache.addSpacing(padding);
1415
+ }
1416
+
1417
+ const points: Vector2[] = cache.corners;
1418
+ for (const child of children) {
1419
+ const childCache = child.fullCacheBBox();
1420
+ const childMatrix = child.localToParent();
1421
+ points.push(
1422
+ ...childCache.corners.map(r => r.transformAsPoint(childMatrix)),
1423
+ );
1424
+ }
1425
+
1426
+ const bbox = BBox.fromPoints(...points);
1427
+ return bbox.addSpacing(padding);
1428
+ }
1429
+
1430
+ /**
1431
+ * Get a bounding box for the contents rendered by this node (including
1432
+ * effects applied after caching).
1433
+ *
1434
+ * @remarks
1435
+ * The returned bounding box should be in local space.
1436
+ */
1437
+ @computed()
1438
+ protected fullCacheBBox(): BBox {
1439
+ const matrix = this.compositeToLocal();
1440
+ const shadowOffset = this.shadowOffset().transform(matrix);
1441
+ const shadowBlur = transformScalar(this.shadowBlur(), matrix);
1442
+
1443
+ const result = this.cacheBBox().expand(
1444
+ this.filters.blur() * 2 + shadowBlur,
1445
+ );
1446
+
1447
+ if (shadowOffset.x < 0) {
1448
+ result.x += shadowOffset.x;
1449
+ result.width -= shadowOffset.x;
1450
+ } else {
1451
+ result.width += shadowOffset.x;
1452
+ }
1453
+
1454
+ if (shadowOffset.y < 0) {
1455
+ result.y += shadowOffset.y;
1456
+ result.height -= shadowOffset.y;
1457
+ } else {
1458
+ result.height += shadowOffset.y;
1459
+ }
1460
+
1461
+ return result;
1462
+ }
1463
+
1464
+ /**
1465
+ * Get a bounding box in world space for the contents rendered by this node as
1466
+ * well as its children.
1467
+ *
1468
+ * @remarks
1469
+ * This is the same the bounding box returned by {@link cacheBBox} only
1470
+ * transformed to world space.
1471
+ */
1472
+ @computed()
1473
+ protected worldSpaceCacheBBox(): BBox {
1474
+ const viewBBox = BBox.fromSizeCentered(this.view().size()).expand(
1475
+ this.view().cachePadding(),
1476
+ );
1477
+ const canvasBBox = BBox.fromPoints(
1478
+ ...viewBBox.transformCorners(this.view().localToWorld()),
1479
+ );
1480
+ const cacheBBox = BBox.fromPoints(
1481
+ ...this.cacheBBox().transformCorners(this.localToWorld()),
1482
+ );
1483
+
1484
+ return canvasBBox.intersection(cacheBBox).pixelPerfect.expand(2);
1485
+ }
1486
+
1487
+ @computed()
1488
+ protected parentWorldSpaceCacheBBox(): BBox {
1489
+ return (
1490
+ this.findAncestor(node => node.requiresCache())?.worldSpaceCacheBBox() ??
1491
+ new BBox(Vector2.zero, useScene2D().getRealSize())
1492
+ );
1493
+ }
1494
+
1495
+ /**
1496
+ * Prepare the given context for drawing a cached node onto it.
1497
+ *
1498
+ * @remarks
1499
+ * This method is called before the contents of the cache canvas are drawn
1500
+ * on the screen. It can be used to apply effects to the entire node together
1501
+ * with its children, instead of applying them individually.
1502
+ * Effects such as transparency, shadows, and filters use this technique.
1503
+ *
1504
+ * Whether the node is cached is decided by the {@link requiresCache} method.
1505
+ *
1506
+ * @param context - The context using which the cache will be drawn.
1507
+ */
1508
+ protected setupDrawFromCache(context: CanvasRenderingContext2D) {
1509
+ context.globalCompositeOperation = this.compositeOperation();
1510
+ context.globalAlpha *= this.opacity();
1511
+ if (this.hasFilters()) {
1512
+ context.filter = this.filterString();
1513
+ }
1514
+ if (this.hasShadow()) {
1515
+ const matrix = this.compositeToWorld();
1516
+ const offset = this.shadowOffset().transform(matrix);
1517
+ const blur = transformScalar(this.shadowBlur(), matrix);
1518
+
1519
+ context.shadowColor = this.shadowColor().serialize();
1520
+ context.shadowBlur = blur;
1521
+ context.shadowOffsetX = offset.x;
1522
+ context.shadowOffsetY = offset.y;
1523
+ }
1524
+
1525
+ const matrix = this.worldToLocal();
1526
+ context.transform(
1527
+ matrix.a,
1528
+ matrix.b,
1529
+ matrix.c,
1530
+ matrix.d,
1531
+ matrix.e,
1532
+ matrix.f,
1533
+ );
1534
+ }
1535
+
1536
+ protected renderFromSource(
1537
+ context: CanvasRenderingContext2D,
1538
+ source: CanvasImageSource,
1539
+ x: number,
1540
+ y: number,
1541
+ ) {
1542
+ this.setupDrawFromCache(context);
1543
+
1544
+ const compositeOverride = this.compositeOverride();
1545
+ context.drawImage(source, x, y);
1546
+ if (compositeOverride > 0) {
1547
+ context.save();
1548
+ context.globalAlpha *= compositeOverride;
1549
+ context.globalCompositeOperation = 'source-over';
1550
+ context.drawImage(source, x, y);
1551
+ context.restore();
1552
+ }
1553
+ }
1554
+
1555
+ private shaderCanvas(destination: TexImageSource, source: TexImageSource) {
1556
+ const shaders = this.shaders();
1557
+ if (shaders.length === 0) {
1558
+ return null;
1559
+ }
1560
+
1561
+ const scene = useScene2D();
1562
+ const size = scene.getRealSize();
1563
+ const parentCacheRect = this.parentWorldSpaceCacheBBox();
1564
+ const cameraToWorld = new DOMMatrix()
1565
+ .scaleSelf(
1566
+ size.width / parentCacheRect.width,
1567
+ size.height / -parentCacheRect.height,
1568
+ )
1569
+ .translateSelf(
1570
+ parentCacheRect.x / -size.width,
1571
+ parentCacheRect.y / size.height - 1,
1572
+ );
1573
+
1574
+ const cacheRect = this.worldSpaceCacheBBox();
1575
+ const cameraToCache = new DOMMatrix()
1576
+ .scaleSelf(size.width / cacheRect.width, size.height / -cacheRect.height)
1577
+ .translateSelf(cacheRect.x / -size.width, cacheRect.y / size.height - 1)
1578
+ .invertSelf();
1579
+
1580
+ const gl = scene.shaders.getGL();
1581
+ scene.shaders.copyTextures(destination, source);
1582
+ scene.shaders.clear();
1583
+
1584
+ for (const shader of shaders) {
1585
+ const program = scene.shaders.getProgram(shader.fragment);
1586
+ if (!program) {
1587
+ continue;
1588
+ }
1589
+
1590
+ if (shader.uniforms) {
1591
+ for (const [name, uniform] of Object.entries(shader.uniforms)) {
1592
+ const location = gl.getUniformLocation(program, name);
1593
+ if (location === null) {
1594
+ continue;
1595
+ }
1596
+
1597
+ const value = unwrap(uniform);
1598
+ if (typeof value === 'number') {
1599
+ gl.uniform1f(location, value);
1600
+ } else if ('toUniform' in value) {
1601
+ value.toUniform(gl, location);
1602
+ } else if (value.length === 1) {
1603
+ gl.uniform1f(location, value[0]);
1604
+ } else if (value.length === 2) {
1605
+ gl.uniform2f(location, value[0], value[1]);
1606
+ } else if (value.length === 3) {
1607
+ gl.uniform3f(location, value[0], value[1], value[2]);
1608
+ } else if (value.length === 4) {
1609
+ gl.uniform4f(location, value[0], value[1], value[2], value[3]);
1610
+ }
1611
+ }
1612
+ }
1613
+
1614
+ gl.uniform1f(
1615
+ gl.getUniformLocation(program, UNIFORM_TIME),
1616
+ this.view2D.globalTime(),
1617
+ );
1618
+
1619
+ gl.uniform1i(
1620
+ gl.getUniformLocation(program, UNIFORM_TIME),
1621
+ scene.playback.frame,
1622
+ );
1623
+
1624
+ gl.uniformMatrix4fv(
1625
+ gl.getUniformLocation(program, UNIFORM_SOURCE_MATRIX),
1626
+ false,
1627
+ cameraToCache.toFloat32Array(),
1628
+ );
1629
+
1630
+ gl.uniformMatrix4fv(
1631
+ gl.getUniformLocation(program, UNIFORM_DESTINATION_MATRIX),
1632
+ false,
1633
+ cameraToWorld.toFloat32Array(),
1634
+ );
1635
+
1636
+ shader.setup?.(gl, program);
1637
+ scene.shaders.render();
1638
+ shader.teardown?.(gl, program);
1639
+ }
1640
+
1641
+ return gl.canvas;
1642
+ }
1643
+
1644
+ /**
1645
+ * Render this node onto the given canvas.
1646
+ *
1647
+ * @param context - The context to draw with.
1648
+ */
1649
+ public render(context: CanvasRenderingContext2D) {
1650
+ if (this.absoluteOpacity() <= 0) {
1651
+ return;
1652
+ }
1653
+
1654
+ context.save();
1655
+ this.transformContext(context);
1656
+
1657
+ if (this.requiresCache()) {
1658
+ const cacheRect = this.worldSpaceCacheBBox();
1659
+ if (cacheRect.width !== 0 && cacheRect.height !== 0) {
1660
+ const cache = this.cachedCanvas().canvas;
1661
+ const source = this.shaderCanvas(context.canvas, cache);
1662
+ if (source) {
1663
+ this.renderFromSource(context, source, 0, 0);
1664
+ } else {
1665
+ this.renderFromSource(
1666
+ context,
1667
+ cache,
1668
+ cacheRect.position.x,
1669
+ cacheRect.position.y,
1670
+ );
1671
+ }
1672
+ }
1673
+ } else {
1674
+ this.draw(context);
1675
+ }
1676
+
1677
+ context.restore();
1678
+ }
1679
+
1680
+ /**
1681
+ * Draw this node onto the canvas.
1682
+ *
1683
+ * @remarks
1684
+ * This method is used when drawing directly onto the screen as well as onto
1685
+ * the cache canvas.
1686
+ * It assumes that the context have already been transformed to local space.
1687
+ *
1688
+ * @param context - The context to draw with.
1689
+ */
1690
+ protected draw(context: CanvasRenderingContext2D) {
1691
+ this.drawChildren(context);
1692
+ }
1693
+
1694
+ protected drawChildren(context: CanvasRenderingContext2D) {
1695
+ for (const child of this.sortedChildren()) {
1696
+ child.render(context);
1697
+ }
1698
+ }
1699
+
1700
+ /**
1701
+ * Draw an overlay for this node.
1702
+ *
1703
+ * @remarks
1704
+ * The overlay for the currently inspected node is displayed on top of the
1705
+ * canvas.
1706
+ *
1707
+ * The provided context is in screen space. The local-to-screen matrix can be
1708
+ * used to transform all shapes that need to be displayed.
1709
+ * This approach allows to keep the line widths and gizmo sizes consistent,
1710
+ * no matter how zoomed-in the view is.
1711
+ *
1712
+ * @param context - The context to draw with.
1713
+ * @param matrix - A local-to-screen matrix.
1714
+ */
1715
+ public drawOverlay(context: CanvasRenderingContext2D, matrix: DOMMatrix) {
1716
+ const box = this.cacheBBox().transformCorners(matrix);
1717
+ const cache = this.getCacheBBox().transformCorners(matrix);
1718
+ context.strokeStyle = 'white';
1719
+ context.lineWidth = 1;
1720
+ context.beginPath();
1721
+ drawLine(context, box);
1722
+ context.closePath();
1723
+ context.stroke();
1724
+
1725
+ context.strokeStyle = 'blue';
1726
+ context.beginPath();
1727
+ drawLine(context, cache);
1728
+ context.closePath();
1729
+ context.stroke();
1730
+ }
1731
+
1732
+ protected transformContext(context: CanvasRenderingContext2D) {
1733
+ const matrix = this.localToParent();
1734
+ context.transform(
1735
+ matrix.a,
1736
+ matrix.b,
1737
+ matrix.c,
1738
+ matrix.d,
1739
+ matrix.e,
1740
+ matrix.f,
1741
+ );
1742
+ }
1743
+
1744
+ /**
1745
+ * Try to find a node intersecting the given position.
1746
+ *
1747
+ * @param position - The searched position.
1748
+ */
1749
+ public hit(position: Vector2): Node | null {
1750
+ let hit: Node | null = null;
1751
+ const local = position.transformAsPoint(this.localToParent().inverse());
1752
+ const children = this.children();
1753
+ for (let i = children.length - 1; i >= 0; i--) {
1754
+ hit = children[i].hit(local);
1755
+ if (hit) {
1756
+ break;
1757
+ }
1758
+ }
1759
+
1760
+ return hit;
1761
+ }
1762
+
1763
+ /**
1764
+ * Collect all asynchronous resources used by this node.
1765
+ */
1766
+ protected collectAsyncResources() {
1767
+ for (const child of this.children()) {
1768
+ child.collectAsyncResources();
1769
+ }
1770
+ }
1771
+
1772
+ /**
1773
+ * Wait for any asynchronous resources that this node or its children have.
1774
+ *
1775
+ * @remarks
1776
+ * Certain resources like images are always loaded asynchronously.
1777
+ * Awaiting this method makes sure that all such resources are done loading
1778
+ * before continuing the animation.
1779
+ */
1780
+ public async toPromise(): Promise<this> {
1781
+ do {
1782
+ await DependencyContext.consumePromises();
1783
+ this.collectAsyncResources();
1784
+ } while (DependencyContext.hasPromises());
1785
+ return this;
1786
+ }
1787
+
1788
+ /**
1789
+ * Return a snapshot of the node's current signal values.
1790
+ *
1791
+ * @remarks
1792
+ * This method will calculate the values of any reactive properties of the
1793
+ * node at the time the method is called.
1794
+ */
1795
+ public getState(): NodeState {
1796
+ const state: NodeState = {};
1797
+ for (const {key, meta, signal} of this) {
1798
+ if (!meta.cloneable || key in state) continue;
1799
+ state[key] = signal();
1800
+ }
1801
+ return state;
1802
+ }
1803
+
1804
+ /**
1805
+ * Apply the given state to the node, setting all matching signal values to
1806
+ * the provided values.
1807
+ *
1808
+ * @param state - The state to apply to the node.
1809
+ */
1810
+ public applyState(state: NodeState): void;
1811
+ /**
1812
+ * Smoothly transition between the current state of the node and the given
1813
+ * state.
1814
+ *
1815
+ * @param state - The state to transition to.
1816
+ * @param duration - The duration of the transition.
1817
+ * @param timing - The timing function to use for the transition.
1818
+ */
1819
+ public applyState(
1820
+ state: NodeState,
1821
+ duration: number,
1822
+ timing?: TimingFunction,
1823
+ ): ThreadGenerator;
1824
+ public applyState(
1825
+ state: NodeState,
1826
+ duration?: number,
1827
+ timing: TimingFunction = easeInOutCubic,
1828
+ ): ThreadGenerator | void {
1829
+ if (duration === undefined) {
1830
+ for (const key in state) {
1831
+ const signal = this.signalByKey(key);
1832
+ if (signal) {
1833
+ signal(state[key]);
1834
+ }
1835
+ }
1836
+ }
1837
+
1838
+ const tasks: ThreadGenerator[] = [];
1839
+ for (const key in state) {
1840
+ const signal = this.signalByKey(key);
1841
+ if (state[key] !== signal.context.raw()) {
1842
+ tasks.push(signal(state[key], duration!, timing));
1843
+ }
1844
+ }
1845
+
1846
+ return all(...tasks);
1847
+ }
1848
+
1849
+ /**
1850
+ * Push a snapshot of the node's current state onto the node's state stack.
1851
+ *
1852
+ * @remarks
1853
+ * This method can be used together with the {@link restore} method to save a
1854
+ * node's current state and later restore it. It is possible to store more
1855
+ * than one state by calling `save` method multiple times.
1856
+ */
1857
+ public save(): void {
1858
+ this.stateStack.push(this.getState());
1859
+ }
1860
+
1861
+ /**
1862
+ * Restore the node to its last saved state.
1863
+ *
1864
+ * @remarks
1865
+ * This method can be used together with the {@link save} method to restore a
1866
+ * node to a previously saved state. Restoring a node to a previous state
1867
+ * removes that state from the state stack.
1868
+ *
1869
+ * @example
1870
+ * ```tsx
1871
+ * const node = <Circle width={100} height={100} fill={"lightseagreen"} />
1872
+ *
1873
+ * view.add(node);
1874
+ *
1875
+ * // Save the node's current state
1876
+ * node.save();
1877
+ *
1878
+ * // Modify some of the node's properties
1879
+ * yield* node.scale(2, 1);
1880
+ * yield* node.fill('hotpink', 1);
1881
+ *
1882
+ * // Restore the node to its saved state
1883
+ * node.restore();
1884
+ * ```
1885
+ */
1886
+ public restore(): void;
1887
+ /**
1888
+ * Tween the node to its last saved state.
1889
+ *
1890
+ * @remarks
1891
+ * This method can be used together with the {@link save} method to restore a
1892
+ * node to a previously saved state. Restoring a node to a previous state
1893
+ * removes that state from the state stack.
1894
+ *
1895
+ * @example
1896
+ * ```tsx
1897
+ * const node = <Circle width={100} height={100} fill={"lightseagreen"} />
1898
+ *
1899
+ * view.add(node);
1900
+ *
1901
+ * // Save the node's current state
1902
+ * node.save();
1903
+ *
1904
+ * // Modify some of the node's properties
1905
+ * yield* node.scale(2, 1);
1906
+ * yield* node.fill('hotpink', 1);
1907
+ *
1908
+ * // Tween the node to its saved state over 1 second
1909
+ * yield* node.restore(1);
1910
+ * ```
1911
+ *
1912
+ * @param duration - The duration of the transition.
1913
+ * @param timing - The timing function to use for the transition.
1914
+ */
1915
+ public restore(duration: number, timing?: TimingFunction): ThreadGenerator;
1916
+ public restore(
1917
+ duration?: number,
1918
+ timing: TimingFunction = easeInOutCubic,
1919
+ ): ThreadGenerator | void {
1920
+ const state = this.stateStack.pop();
1921
+
1922
+ if (state !== undefined) {
1923
+ return this.applyState(state, duration!, timing);
1924
+ }
1925
+ }
1926
+
1927
+ public *[Symbol.iterator]() {
1928
+ for (const key in this.properties) {
1929
+ const meta = this.properties[key];
1930
+ const signal = this.signalByKey(key);
1931
+ yield {meta, signal, key};
1932
+ }
1933
+ }
1934
+
1935
+ private signalByKey(key: string): SimpleSignal<any> {
1936
+ return (<Record<string, SimpleSignal<any>>>(<unknown>this))[key];
1937
+ }
1938
+
1939
+ private reversedChildren() {
1940
+ const children = this.children();
1941
+ const result: Node[] = [];
1942
+ for (let i = children.length - 1; i >= 0; i--) {
1943
+ result.push(children[i]);
1944
+ }
1945
+ return result;
1946
+ }
1947
+ }
1948
+
1949
+ Node.prototype.isClass = true;