paper 0.16.0 → 1.0.0-rc.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (464) hide show
  1. package/CHANGELOG.md +294 -0
  2. package/LICENSE.txt +1 -1
  3. package/MIGRATION.md +402 -0
  4. package/README.md +137 -143
  5. package/dist/esm/anim/Tween.js +404 -0
  6. package/dist/esm/basic/Line.js +252 -0
  7. package/dist/esm/basic/Matrix.js +603 -0
  8. package/dist/esm/basic/Point.js +738 -0
  9. package/dist/esm/basic/Rectangle.js +788 -0
  10. package/dist/esm/basic/Size.js +368 -0
  11. package/dist/esm/boolean/graph.js +950 -0
  12. package/dist/esm/boolean/index.js +143 -0
  13. package/dist/esm/boolean/operations.js +94 -0
  14. package/dist/esm/boolean/split.js +89 -0
  15. package/dist/esm/canvas/blendModes.js +115 -0
  16. package/dist/esm/canvas/canvasProvider.js +102 -0
  17. package/dist/esm/constants.js +130 -0
  18. package/dist/esm/core/Base.js +52 -0
  19. package/dist/esm/core/Emitter.js +215 -0
  20. package/dist/esm/core/features.js +53 -0
  21. package/dist/esm/core/serialization.js +174 -0
  22. package/dist/esm/core.js +15 -0
  23. package/dist/esm/dom/domElement.js +112 -0
  24. package/dist/esm/dom/domEvent.js +106 -0
  25. package/dist/esm/env.js +66 -0
  26. package/dist/esm/event/DrawEvent.js +38 -0
  27. package/dist/esm/event/Event.js +79 -0
  28. package/dist/esm/event/Key.js +236 -0
  29. package/dist/esm/event/KeyEvent.js +39 -0
  30. package/dist/esm/event/MouseEvent.js +91 -0
  31. package/dist/esm/hosted/HostItem.js +60 -0
  32. package/dist/esm/hosted/Hosting.js +1061 -0
  33. package/dist/esm/hosted/index.js +20 -0
  34. package/dist/esm/index.js +38 -0
  35. package/dist/esm/internal.js +115 -0
  36. package/dist/esm/item/Effect.js +256 -0
  37. package/dist/esm/item/EffectInstance.js +184 -0
  38. package/dist/esm/item/Group.js +293 -0
  39. package/dist/esm/item/HitResult.js +126 -0
  40. package/dist/esm/item/Item.js +3339 -0
  41. package/dist/esm/item/ItemDefinition.js +95 -0
  42. package/dist/esm/item/Layer.js +69 -0
  43. package/dist/esm/item/Project.js +1013 -0
  44. package/dist/esm/item/Raster.js +722 -0
  45. package/dist/esm/item/Shape.js +355 -0
  46. package/dist/esm/item/SymbolDefinition.js +68 -0
  47. package/dist/esm/item/SymbolItem.js +131 -0
  48. package/dist/esm/item/bounds.js +324 -0
  49. package/dist/esm/item/draw.js +525 -0
  50. package/dist/esm/item/effects.js +124 -0
  51. package/dist/esm/item/expand.js +87 -0
  52. package/dist/esm/item/hitTest.js +203 -0
  53. package/dist/esm/item/hosting.js +12 -0
  54. package/dist/esm/item/props.js +12 -0
  55. package/dist/esm/item/query.js +139 -0
  56. package/dist/esm/item/sampling.js +67 -0
  57. package/dist/esm/item/transform.js +128 -0
  58. package/dist/esm/net/http.js +26 -0
  59. package/dist/esm/node/CanvasAdapter.js +137 -0
  60. package/dist/esm/node/index.js +150 -0
  61. package/dist/esm/node/peers.js +27 -0
  62. package/dist/esm/offset/curves.js +866 -0
  63. package/dist/esm/offset/index.js +70 -0
  64. package/dist/esm/offset/offset.js +178 -0
  65. package/dist/esm/offset/outline.js +91 -0
  66. package/dist/esm/offset/pen.js +298 -0
  67. package/dist/esm/offset/profile.js +772 -0
  68. package/dist/esm/offset/round.js +213 -0
  69. package/dist/esm/offset/sweep.js +100 -0
  70. package/dist/esm/paperscript/PaperScript.js +832 -0
  71. package/dist/esm/paperscript/autoload.js +27 -0
  72. package/dist/esm/paperscript/index.js +17 -0
  73. package/dist/esm/path/CompoundPath.js +367 -0
  74. package/dist/esm/path/Curve.js +996 -0
  75. package/dist/esm/path/CurveLocation.js +462 -0
  76. package/dist/esm/path/Path.js +1761 -0
  77. package/dist/esm/path/PathFlattener.js +186 -0
  78. package/dist/esm/path/PathItem.js +757 -0
  79. package/dist/esm/path/Segment.js +640 -0
  80. package/dist/esm/path/SegmentPoint.js +106 -0
  81. package/dist/esm/path/StrokeProfile.js +387 -0
  82. package/dist/esm/path/StrokeProfileStop.js +207 -0
  83. package/dist/esm/path/boolean.js +284 -0
  84. package/dist/esm/path/bounds.js +257 -0
  85. package/dist/esm/path/crossing.js +114 -0
  86. package/dist/esm/path/draw.js +161 -0
  87. package/dist/esm/path/hitTest.js +259 -0
  88. package/dist/esm/path/offset.js +152 -0
  89. package/dist/esm/path/shapes.js +231 -0
  90. package/dist/esm/path/smooth.js +84 -0
  91. package/dist/esm/path/values.js +40 -0
  92. package/dist/esm/scope/PaperScope.js +288 -0
  93. package/dist/esm/scope/PaperScopeItem.js +74 -0
  94. package/dist/esm/smoothing/index.js +71 -0
  95. package/dist/esm/style/Color.js +917 -0
  96. package/dist/esm/style/Gradient.js +191 -0
  97. package/dist/esm/style/GradientStop.js +168 -0
  98. package/dist/esm/style/PatternDefinition.js +383 -0
  99. package/dist/esm/style/Style.js +503 -0
  100. package/dist/esm/svg/index.js +27 -0
  101. package/dist/esm/svg/svgElement.js +97 -0
  102. package/dist/esm/svg/svgExport.js +847 -0
  103. package/dist/esm/svg/svgImport.js +1101 -0
  104. package/dist/esm/svg/svgStyles.js +96 -0
  105. package/dist/esm/text/AreaText.js +190 -0
  106. package/dist/esm/text/PathText.js +111 -0
  107. package/dist/esm/text/PointText.js +92 -0
  108. package/dist/esm/text/TextItem.js +538 -0
  109. package/dist/esm/text/TextRange.js +169 -0
  110. package/dist/esm/text/TextStory.js +394 -0
  111. package/dist/esm/text/areaLayout.js +439 -0
  112. package/dist/esm/text/layout.js +265 -0
  113. package/dist/esm/text/measure.js +57 -0
  114. package/dist/esm/text/pathLayout.js +93 -0
  115. package/dist/esm/tool/Tool.js +284 -0
  116. package/dist/esm/tool/ToolEvent.js +232 -0
  117. package/dist/esm/util/Formatter.js +57 -0
  118. package/dist/esm/util/collisions.js +216 -0
  119. package/dist/esm/util/list.js +110 -0
  120. package/dist/esm/util/numerical.js +656 -0
  121. package/dist/esm/util/object.js +81 -0
  122. package/dist/esm/util/string.js +29 -0
  123. package/dist/esm/util/uid.js +31 -0
  124. package/dist/esm/values/arcs.js +243 -0
  125. package/dist/esm/values/curve.js +975 -0
  126. package/dist/esm/values/fit.js +290 -0
  127. package/dist/esm/values/index.js +23 -0
  128. package/dist/esm/values/intersections.js +869 -0
  129. package/dist/esm/values/matrix.js +114 -0
  130. package/dist/esm/values/path.js +174 -0
  131. package/dist/esm/values/profile.js +221 -0
  132. package/dist/esm/values/region.js +63 -0
  133. package/dist/esm/values/smooth.js +248 -0
  134. package/dist/esm/values/svg.js +287 -0
  135. package/dist/esm/view/CanvasView.js +106 -0
  136. package/dist/esm/view/View.js +775 -0
  137. package/dist/esm/view/input.js +527 -0
  138. package/dist/paper-full.js +33083 -22681
  139. package/dist/paper-full.min.js +36 -31
  140. package/dist/paper-hosted.js +33898 -23223
  141. package/dist/paper-hosted.min.js +37 -0
  142. package/dist/paper-slim.js +28581 -0
  143. package/dist/paper-slim.min.js +17 -0
  144. package/dist/types/anim/Tween.d.ts +164 -0
  145. package/dist/types/basic/Line.d.ts +136 -0
  146. package/dist/types/basic/Matrix.d.ts +387 -0
  147. package/dist/types/basic/Point.d.ts +781 -0
  148. package/dist/types/basic/Rectangle.d.ts +550 -0
  149. package/dist/types/basic/Size.d.ts +457 -0
  150. package/dist/types/boolean/graph.d.ts +123 -0
  151. package/dist/types/boolean/index.d.ts +74 -0
  152. package/dist/types/boolean/operations.d.ts +9 -0
  153. package/dist/types/boolean/split.d.ts +19 -0
  154. package/dist/types/canvas/blendModes.d.ts +18 -0
  155. package/dist/types/canvas/canvasProvider.d.ts +32 -0
  156. package/dist/types/constants.d.ts +112 -0
  157. package/dist/types/core/Base.d.ts +50 -0
  158. package/dist/types/core/Emitter.d.ts +49 -0
  159. package/dist/types/core/features.d.ts +41 -0
  160. package/dist/types/core/serialization.d.ts +44 -0
  161. package/dist/types/core.d.ts +18 -0
  162. package/dist/types/dom/domElement.d.ts +33 -0
  163. package/dist/types/dom/domEvent.d.ts +29 -0
  164. package/dist/types/env.d.ts +26 -0
  165. package/dist/types/event/DrawEvent.d.ts +27 -0
  166. package/dist/types/event/Event.d.ts +47 -0
  167. package/dist/types/event/Key.d.ts +82 -0
  168. package/dist/types/event/KeyEvent.d.ts +35 -0
  169. package/dist/types/event/MouseEvent.d.ts +78 -0
  170. package/dist/types/hosted/HostItem.d.ts +26 -0
  171. package/dist/types/hosted/Hosting.d.ts +273 -0
  172. package/dist/types/hosted/index.d.ts +10 -0
  173. package/dist/types/index.d.ts +64 -0
  174. package/dist/types/internal.d.ts +101 -0
  175. package/dist/types/item/Effect.d.ts +128 -0
  176. package/dist/types/item/EffectInstance.d.ts +81 -0
  177. package/dist/types/item/Group.d.ts +225 -0
  178. package/dist/types/item/HitResult.d.ts +94 -0
  179. package/dist/types/item/Item.d.ts +2927 -0
  180. package/dist/types/item/ItemDefinition.d.ts +25 -0
  181. package/dist/types/item/Layer.d.ts +80 -0
  182. package/dist/types/item/Project.d.ts +748 -0
  183. package/dist/types/item/Raster.d.ts +431 -0
  184. package/dist/types/item/Shape.d.ts +219 -0
  185. package/dist/types/item/SymbolDefinition.d.ts +98 -0
  186. package/dist/types/item/SymbolItem.d.ts +92 -0
  187. package/dist/types/item/bounds.d.ts +56 -0
  188. package/dist/types/item/draw.d.ts +23 -0
  189. package/dist/types/item/effects.d.ts +22 -0
  190. package/dist/types/item/expand.d.ts +7 -0
  191. package/dist/types/item/hitTest.d.ts +10 -0
  192. package/dist/types/item/hosting.d.ts +20 -0
  193. package/dist/types/item/props.d.ts +35 -0
  194. package/dist/types/item/query.d.ts +14 -0
  195. package/dist/types/item/sampling.d.ts +5 -0
  196. package/dist/types/item/transform.d.ts +3 -0
  197. package/dist/types/net/http.d.ts +6 -0
  198. package/dist/types/node/CanvasAdapter.d.ts +19 -0
  199. package/dist/types/node/index.d.ts +60 -0
  200. package/dist/types/node/peers.d.ts +2 -0
  201. package/dist/types/offset/curves.d.ts +22 -0
  202. package/dist/types/offset/index.d.ts +65 -0
  203. package/dist/types/offset/offset.d.ts +18 -0
  204. package/dist/types/offset/outline.d.ts +14 -0
  205. package/dist/types/offset/pen.d.ts +36 -0
  206. package/dist/types/offset/profile.d.ts +37 -0
  207. package/dist/types/offset/round.d.ts +18 -0
  208. package/dist/types/offset/sweep.d.ts +44 -0
  209. package/dist/types/paperscript/PaperScript.d.ts +114 -0
  210. package/dist/types/paperscript/autoload.d.ts +1 -0
  211. package/dist/types/paperscript/index.d.ts +7 -0
  212. package/dist/types/path/CompoundPath.d.ts +183 -0
  213. package/dist/types/path/Curve.d.ts +567 -0
  214. package/dist/types/path/CurveLocation.d.ts +164 -0
  215. package/dist/types/path/Path.d.ts +1361 -0
  216. package/dist/types/path/PathFlattener.d.ts +48 -0
  217. package/dist/types/path/PathItem.d.ts +1190 -0
  218. package/dist/types/path/Segment.d.ts +335 -0
  219. package/dist/types/path/SegmentPoint.d.ts +1 -0
  220. package/dist/types/path/StrokeProfile.d.ts +74 -0
  221. package/dist/types/path/StrokeProfileStop.d.ts +110 -0
  222. package/dist/types/path/boolean.d.ts +24 -0
  223. package/dist/types/path/bounds.d.ts +24 -0
  224. package/dist/types/path/crossing.d.ts +2 -0
  225. package/dist/types/path/draw.d.ts +6 -0
  226. package/dist/types/path/hitTest.d.ts +8 -0
  227. package/dist/types/path/offset.d.ts +14 -0
  228. package/dist/types/path/shapes.d.ts +20 -0
  229. package/dist/types/path/smooth.d.ts +18 -0
  230. package/dist/types/path/values.d.ts +8 -0
  231. package/dist/types/scope/PaperScope.d.ts +182 -0
  232. package/dist/types/scope/PaperScopeItem.d.ts +26 -0
  233. package/dist/types/smoothing/index.d.ts +26 -0
  234. package/dist/types/style/Color.d.ts +829 -0
  235. package/dist/types/style/Gradient.d.ts +130 -0
  236. package/dist/types/style/GradientStop.d.ts +126 -0
  237. package/dist/types/style/PatternDefinition.d.ts +185 -0
  238. package/dist/types/style/Style.d.ts +757 -0
  239. package/dist/types/svg/index.d.ts +14 -0
  240. package/dist/types/svg/svgElement.d.ts +32 -0
  241. package/dist/types/svg/svgExport.d.ts +19 -0
  242. package/dist/types/svg/svgImport.d.ts +13 -0
  243. package/dist/types/svg/svgStyles.d.ts +20 -0
  244. package/dist/types/text/AreaText.d.ts +104 -0
  245. package/dist/types/text/PathText.d.ts +78 -0
  246. package/dist/types/text/PointText.d.ts +50 -0
  247. package/dist/types/text/TextItem.d.ts +163 -0
  248. package/dist/types/text/TextRange.d.ts +98 -0
  249. package/dist/types/text/TextStory.d.ts +119 -0
  250. package/dist/types/text/areaLayout.d.ts +3 -0
  251. package/dist/types/text/layout.d.ts +43 -0
  252. package/dist/types/text/measure.d.ts +7 -0
  253. package/dist/types/text/pathLayout.d.ts +3 -0
  254. package/dist/types/tool/Tool.d.ts +356 -0
  255. package/dist/types/tool/ToolEvent.d.ts +134 -0
  256. package/dist/types/util/Formatter.d.ts +34 -0
  257. package/dist/types/util/collisions.d.ts +77 -0
  258. package/dist/types/util/list.d.ts +23 -0
  259. package/dist/types/util/numerical.d.ts +196 -0
  260. package/dist/types/util/object.d.ts +19 -0
  261. package/dist/types/util/string.d.ts +12 -0
  262. package/dist/types/util/uid.d.ts +7 -0
  263. package/dist/types/values/arcs.d.ts +10 -0
  264. package/dist/types/values/curve.d.ts +42 -0
  265. package/dist/types/values/fit.d.ts +26 -0
  266. package/dist/types/values/index.d.ts +10 -0
  267. package/dist/types/values/intersections.d.ts +11 -0
  268. package/dist/types/values/matrix.d.ts +19 -0
  269. package/dist/types/values/path.d.ts +26 -0
  270. package/dist/types/values/profile.d.ts +41 -0
  271. package/dist/types/values/region.d.ts +13 -0
  272. package/dist/types/values/smooth.d.ts +39 -0
  273. package/dist/types/values/svg.d.ts +47 -0
  274. package/dist/types/view/CanvasView.d.ts +37 -0
  275. package/dist/types/view/View.d.ts +700 -0
  276. package/dist/types/view/input.d.ts +28 -0
  277. package/package.json +90 -58
  278. package/dist/docs/assets/css/docs.css +0 -414
  279. package/dist/docs/assets/js/codemirror.js +0 -4
  280. package/dist/docs/assets/js/docs.js +0 -493
  281. package/dist/docs/assets/js/jquery.js +0 -6
  282. package/dist/docs/assets/js/paper.js +0 -23759
  283. package/dist/docs/classes/AreaText.html +0 -8708
  284. package/dist/docs/classes/ChangeFlag.html +0 -385
  285. package/dist/docs/classes/Color.html +0 -2205
  286. package/dist/docs/classes/CompoundPath.html +0 -9168
  287. package/dist/docs/classes/Curve.html +0 -2326
  288. package/dist/docs/classes/CurveLocation.html +0 -673
  289. package/dist/docs/classes/Event.html +0 -177
  290. package/dist/docs/classes/Gradient.html +0 -241
  291. package/dist/docs/classes/GradientStop.html +0 -274
  292. package/dist/docs/classes/Group.html +0 -7139
  293. package/dist/docs/classes/HitResult.html +0 -241
  294. package/dist/docs/classes/HostItem.html +0 -6836
  295. package/dist/docs/classes/Item.html +0 -6892
  296. package/dist/docs/classes/Key.html +0 -140
  297. package/dist/docs/classes/KeyEvent.html +0 -309
  298. package/dist/docs/classes/Layer.html +0 -7122
  299. package/dist/docs/classes/Matrix.html +0 -1745
  300. package/dist/docs/classes/MouseEvent.html +0 -356
  301. package/dist/docs/classes/PaperScope.html +0 -548
  302. package/dist/docs/classes/PaperScript.html +0 -202
  303. package/dist/docs/classes/Path.html +0 -12064
  304. package/dist/docs/classes/PathItem.html +0 -8930
  305. package/dist/docs/classes/PathText.html +0 -8578
  306. package/dist/docs/classes/Point.html +0 -2860
  307. package/dist/docs/classes/PointText.html +0 -8489
  308. package/dist/docs/classes/Project.html +0 -1604
  309. package/dist/docs/classes/Raster.html +0 -8069
  310. package/dist/docs/classes/Rectangle.html +0 -1698
  311. package/dist/docs/classes/Segment.html +0 -1017
  312. package/dist/docs/classes/Shape.html +0 -7570
  313. package/dist/docs/classes/Size.html +0 -1915
  314. package/dist/docs/classes/Style.html +0 -1765
  315. package/dist/docs/classes/SymbolDefinition.html +0 -312
  316. package/dist/docs/classes/SymbolItem.html +0 -6999
  317. package/dist/docs/classes/TextItem.html +0 -8332
  318. package/dist/docs/classes/TextRange.html +0 -186
  319. package/dist/docs/classes/TextStory.html +0 -355
  320. package/dist/docs/classes/Tool.html +0 -888
  321. package/dist/docs/classes/ToolEvent.html +0 -525
  322. package/dist/docs/classes/Tween.html +0 -390
  323. package/dist/docs/classes/View.html +0 -2108
  324. package/dist/docs/classes/global.html +0 -426
  325. package/dist/docs/classes/index.html +0 -88
  326. package/dist/docs/index.html +0 -11
  327. package/dist/node/canvas.js +0 -68
  328. package/dist/node/extend.js +0 -156
  329. package/dist/node/self.js +0 -67
  330. package/dist/paper-core.d.ts +0 -1
  331. package/dist/paper-core.js +0 -21834
  332. package/dist/paper-core.min.js +0 -32
  333. package/dist/paper.d.ts +0 -9630
  334. package/examples/Animated/AnimatedStar.html +0 -53
  335. package/examples/Animated/BooleanOperations.html +0 -115
  336. package/examples/Animated/CandyCrash.html +0 -151
  337. package/examples/Animated/Extruded.html +0 -50
  338. package/examples/Animated/Lines.html +0 -53
  339. package/examples/Animated/Smoothing.html +0 -70
  340. package/examples/Animated/Space.html +0 -86
  341. package/examples/Animated/SpaceUsingShapes.html +0 -79
  342. package/examples/Games/Paperoids.html +0 -569
  343. package/examples/JSON/Circle Testing.html +0 -34
  344. package/examples/JSON/Compound Path.html +0 -25
  345. package/examples/JSON/Empty Path Testing.html +0 -31
  346. package/examples/JSON/Gradients.html +0 -34
  347. package/examples/JSON/Group Transform.html +0 -28
  348. package/examples/JSON/Line Testing.html +0 -44
  349. package/examples/JSON/Random Path Testing.html +0 -42
  350. package/examples/JSON/Raster.html +0 -29
  351. package/examples/JSON/Rect and Attribute Testing.html +0 -56
  352. package/examples/JSON/Rotated Primitives.html +0 -55
  353. package/examples/JSON/Selection.html +0 -43
  354. package/examples/JSON/Shapes.html +0 -54
  355. package/examples/JSON/Symbols.html +0 -30
  356. package/examples/JSON/Text Testing.html +0 -38
  357. package/examples/JSON/Tiger.html +0 -19
  358. package/examples/JSON/Transform Test 1.html +0 -32
  359. package/examples/JSON/Transform Test 2.html +0 -30
  360. package/examples/Node.js/AnimatedStar.js +0 -52
  361. package/examples/Node.js/BooleanOperations.js +0 -36
  362. package/examples/Node.js/JSONtoPDF.js +0 -20
  363. package/examples/Node.js/Raster.js +0 -32
  364. package/examples/Node.js/RasterRemote.js +0 -33
  365. package/examples/Node.js/SvgExport.js +0 -52
  366. package/examples/Node.js/SvgImport.js +0 -23
  367. package/examples/Node.js/Tadpoles.js +0 -16
  368. package/examples/Node.js/Tadpoles.pjs +0 -278
  369. package/examples/Node.js/in.json +0 -1
  370. package/examples/Node.js/in.svg +0 -16
  371. package/examples/Paperjs.org/BooleanOperattions.html +0 -115
  372. package/examples/Paperjs.org/BouncingBalls.html +0 -103
  373. package/examples/Paperjs.org/Chain.html +0 -54
  374. package/examples/Paperjs.org/DivisionRaster.html +0 -72
  375. package/examples/Paperjs.org/FutureSplash.html +0 -122
  376. package/examples/Paperjs.org/HitTesting.html +0 -103
  377. package/examples/Paperjs.org/InteractiveTiger.html +0 -777
  378. package/examples/Paperjs.org/MetaBalls.html +0 -132
  379. package/examples/Paperjs.org/NyanRainbow.html +0 -202
  380. package/examples/Paperjs.org/PathIntersections.html +0 -76
  381. package/examples/Paperjs.org/Qbertify.html +0 -146
  382. package/examples/Paperjs.org/RadialRainbows.html +0 -80
  383. package/examples/Paperjs.org/RoundedRectangles.html +0 -41
  384. package/examples/Paperjs.org/SatieLikedToDraw.html +0 -140
  385. package/examples/Paperjs.org/Simplify.html +0 -63
  386. package/examples/Paperjs.org/SpiralRaster.html +0 -120
  387. package/examples/Paperjs.org/Tadpoles.html +0 -283
  388. package/examples/Paperjs.org/Voronoi.html +0 -134
  389. package/examples/Rasters/PhyllotaxisRaster.html +0 -96
  390. package/examples/Rasters/Raster.html +0 -34
  391. package/examples/Rasters/RotationRaster.html +0 -44
  392. package/examples/Rasters/Smoothing.html +0 -36
  393. package/examples/SVG Export/Circle Testing.html +0 -32
  394. package/examples/SVG Export/Clipping.html +0 -17
  395. package/examples/SVG Export/Compound Path.html +0 -22
  396. package/examples/SVG Export/Empty Path Testing.html +0 -28
  397. package/examples/SVG Export/Gradients.html +0 -52
  398. package/examples/SVG Export/Group Transform.html +0 -25
  399. package/examples/SVG Export/Line Testing.html +0 -41
  400. package/examples/SVG Export/Random Path Testing.html +0 -39
  401. package/examples/SVG Export/Raster.html +0 -21
  402. package/examples/SVG Export/Rect and Attribute Testing.html +0 -53
  403. package/examples/SVG Export/Rotated Primitives.html +0 -80
  404. package/examples/SVG Export/Shapes.html +0 -58
  405. package/examples/SVG Export/Symbols.html +0 -27
  406. package/examples/SVG Export/Text Testing.html +0 -45
  407. package/examples/SVG Export/Tiger.html +0 -23
  408. package/examples/SVG Export/Transform Test 1.html +0 -29
  409. package/examples/SVG Export/Transform Test 2.html +0 -27
  410. package/examples/SVG Import/Arcs.html +0 -78
  411. package/examples/SVG Import/Butterfly.html +0 -223
  412. package/examples/SVG Import/Circle and Ellipse Testing.html +0 -26
  413. package/examples/SVG Import/Clipping.html +0 -100
  414. package/examples/SVG Import/From File.html +0 -30
  415. package/examples/SVG Import/Gradient.html +0 -138
  416. package/examples/SVG Import/Gradients.html +0 -30
  417. package/examples/SVG Import/Inkscape Pivot.html +0 -52
  418. package/examples/SVG Import/Inkscape.html +0 -88
  419. package/examples/SVG Import/Line Testing.html +0 -24
  420. package/examples/SVG Import/MoreGradients.html +0 -107
  421. package/examples/SVG Import/Multiple Paths Test 1.html +0 -42
  422. package/examples/SVG Import/Multiple Paths Test 2.html +0 -33
  423. package/examples/SVG Import/Nested Groups Test.html +0 -78
  424. package/examples/SVG Import/Polybezier.html +0 -43
  425. package/examples/SVG Import/Rect Testing.html +0 -25
  426. package/examples/SVG Import/Symbols.html +0 -24
  427. package/examples/SVG Import/Testing.html +0 -40
  428. package/examples/SVG Import/Text Testing.html +0 -19
  429. package/examples/SVG Import/Tiger.html +0 -742
  430. package/examples/SVG Import/Transform Testing.html +0 -22
  431. package/examples/SVG Import/Viewbox.html +0 -38
  432. package/examples/Scripts/Arcs.html +0 -39
  433. package/examples/Scripts/AreaText.html +0 -65
  434. package/examples/Scripts/BlendModes.html +0 -77
  435. package/examples/Scripts/BooleanOperations.html +0 -937
  436. package/examples/Scripts/CompoundPath.html +0 -19
  437. package/examples/Scripts/CurveTimeParameterization.html +0 -61
  438. package/examples/Scripts/HslColor.html +0 -53
  439. package/examples/Scripts/PathStructure.html +0 -82
  440. package/examples/Scripts/PathTangents.html +0 -73
  441. package/examples/Scripts/PathText.html +0 -48
  442. package/examples/Scripts/Resize.html +0 -42
  443. package/examples/Scripts/RoundRectangle.html +0 -28
  444. package/examples/Scripts/Shapes.html +0 -54
  445. package/examples/Scripts/StrokeBounds.html +0 -101
  446. package/examples/Scripts/StrokeScaling.html +0 -65
  447. package/examples/Scripts/TextRanges.html +0 -51
  448. package/examples/Tools/BezierTool.html +0 -90
  449. package/examples/Tools/Circles.html +0 -28
  450. package/examples/Tools/Clouds.html +0 -35
  451. package/examples/Tools/DrippingBrush.html +0 -73
  452. package/examples/Tools/FancyBrush.html +0 -82
  453. package/examples/Tools/Grid.html +0 -44
  454. package/examples/Tools/MultiLines.html +0 -44
  455. package/examples/Tools/MultipleTools.html +0 -44
  456. package/examples/Tools/PathEditing.html +0 -95
  457. package/examples/Tools/SquareRounded.html +0 -76
  458. package/examples/Tools/Stars.html +0 -38
  459. package/examples/Tools/Vektor.html +0 -200
  460. package/examples/Tools/Wave.html +0 -50
  461. package/examples/Tools/WormFarm.html +0 -88
  462. package/examples/Worker/Main.html +0 -53
  463. package/examples/Worker/Worker.js +0 -14
  464. package/examples/css/style.css +0 -12
@@ -0,0 +1,788 @@
1
+ /*
2
+ * Paper.js - The Swiss Army Knife of Vector Graphics Scripting.
3
+ * http://paperjs.org/
4
+ *
5
+ * Copyright (c) 2011 - 2026, Jürg Lehni & Jonathan Puckey
6
+ * http://juerglehni.com/ & https://puckey.studio/
7
+ *
8
+ * Distributed under the MIT license. See LICENSE file for details.
9
+ *
10
+ * All rights reserved.
11
+ */
12
+ import { ItemSelection } from "../constants.js";
13
+ import { inject, isPlainObject } from "../util/object.js";
14
+ import { Formatter } from "../util/Formatter.js";
15
+ import { Base } from "../core/Base.js";
16
+ import { registerClasses } from "../core/serialization.js";
17
+ import { LinkedPoint, Point } from "./Point.js";
18
+ import { LinkedSize, Size } from "./Size.js";
19
+ // Where the corners and the centers of the sides lie between the left and the
20
+ // right side and the top and the bottom, as fractions of the width and height:
21
+ export const corners = {
22
+ topLeft: [0, 0],
23
+ topRight: [1, 0],
24
+ bottomLeft: [0, 1],
25
+ bottomRight: [1, 1],
26
+ leftCenter: [0, 0.5],
27
+ topCenter: [0.5, 0],
28
+ rightCenter: [1, 0.5],
29
+ bottomCenter: [0.5, 1]
30
+ };
31
+ /**
32
+ * A Rectangle specifies an area that is enclosed by its top-left point (x,
33
+ * y), its width, and its height. It should not be confused with a rectangular
34
+ * path, it is not an item.
35
+ *
36
+ * Wherever a rectangle is the only argument of a function, it can also be
37
+ * passed in any of the forms that the {@link Rectangle()} constructors take,
38
+ * e.g. `rect.unite(0, 0, 10, 10)`.
39
+ */
40
+ export class Rectangle extends Base {
41
+ constructor(x, y, width, height) {
42
+ super();
43
+ this.set(x, y, width, height);
44
+ }
45
+ /**
46
+ * Sets the rectangle to the passed values. Note that any sequence of
47
+ * parameters that is supported by the various {@link Rectangle()}
48
+ * constructors also work for calls of `set()`.
49
+ *
50
+ * @param rect the rectangle, its point or `from`, or its x-coordinate
51
+ * @param y its size, `to` or y-coordinate
52
+ * @param width its width, with numbers as the first arguments
53
+ * @param height its height, with numbers as the first arguments
54
+ */
55
+ set(rect, y, width, height) {
56
+ if (typeof rect === 'number') {
57
+ // new Rectangle(x, y, width, height)
58
+ this._set(rect, y, width, height);
59
+ }
60
+ else if (rect === undefined || rect === null) {
61
+ // new Rectangle(), new Rectangle(null)
62
+ this._set(0, 0, 0, 0);
63
+ }
64
+ else if (y !== undefined) {
65
+ // Look at the next value to see whether it's a size or a point.
66
+ const next = y;
67
+ if (next && next.x !== undefined) {
68
+ // new Rectangle(from, to)
69
+ this.#setFromTo(rect, next);
70
+ }
71
+ else {
72
+ // new Rectangle(point, size)
73
+ const point = Point.from(rect);
74
+ const size = Size.from(next);
75
+ this._set(point.x, point.y, size.width, size.height);
76
+ }
77
+ }
78
+ else {
79
+ // This can either be an array, or an object literal.
80
+ const obj = rect;
81
+ if (Array.isArray(obj)) {
82
+ this._set(obj[0], obj[1], obj[2], obj[3]);
83
+ }
84
+ else if (obj instanceof Rectangle || (obj.x !== undefined &&
85
+ obj.y !== undefined &&
86
+ obj.width !== undefined &&
87
+ obj.height !== undefined)) {
88
+ // Another rectangle, or an object with all of its fields:
89
+ this._set(obj.x, obj.y, obj.width, obj.height);
90
+ }
91
+ else if (obj.from !== undefined || obj.to !== undefined) {
92
+ // new Rectangle({ from, to }), with a size of 0 without `to`
93
+ this.#setFromTo(obj.from, obj.to ?? obj.from);
94
+ }
95
+ else {
96
+ // An object that describes the rectangle through any of its
97
+ // properties, such as `{ x, y, width, height }`, a size, or
98
+ // `{ right: 100, width: 50 }`:
99
+ this._set(...readObject(obj));
100
+ }
101
+ }
102
+ return this;
103
+ }
104
+ #setFromTo(arg0, arg1) {
105
+ let { x, y } = Point.from(arg0);
106
+ const to = Point.from(arg1);
107
+ let width = to.x - x;
108
+ let height = to.y - y;
109
+ // Check if horizontal or vertical order needs to be reversed.
110
+ if (width < 0) {
111
+ x = to.x;
112
+ width = -width;
113
+ }
114
+ if (height < 0) {
115
+ y = to.y;
116
+ height = -height;
117
+ }
118
+ this._set(x, y, width, height);
119
+ }
120
+ // See Point#_set() for an explanation of _set():
121
+ _set(x, y, width, height) {
122
+ this.x = x;
123
+ this.y = y;
124
+ this.width = width;
125
+ this.height = height;
126
+ return this;
127
+ }
128
+ /**
129
+ * Returns a copy of the rectangle.
130
+ */
131
+ clone() {
132
+ return new Rectangle(this.x, this.y, this.width, this.height);
133
+ }
134
+ equals(rect) {
135
+ const other = isPlainObject(rect) || Array.isArray(rect)
136
+ ? Rectangle.from(rect)
137
+ : rect;
138
+ return (other === this || (other &&
139
+ this.x === other.x &&
140
+ this.y === other.y &&
141
+ this.width === other.width &&
142
+ this.height === other.height) ||
143
+ false);
144
+ }
145
+ /**
146
+ * @return a string representation of this rectangle
147
+ */
148
+ toString() {
149
+ const { instance } = Formatter;
150
+ return (`{ x: ${instance.number(this.x)}, y: ${instance.number(this.y)}, ` +
151
+ `width: ${instance.number(this.width)}, ` +
152
+ `height: ${instance.number(this.height)} }`);
153
+ }
154
+ _serialize(options) {
155
+ const { formatter } = options;
156
+ // See Point#_serialize()
157
+ return [
158
+ formatter.number(this.x),
159
+ formatter.number(this.y),
160
+ formatter.number(this.width),
161
+ formatter.number(this.height)
162
+ ];
163
+ }
164
+ /**
165
+ * The rectangle's position and size as an array,
166
+ * `[x, y, width, height]`, as {@link Matrix#values} gives those of a
167
+ * matrix.
168
+ */
169
+ get values() {
170
+ return [this.x, this.y, this.width, this.height];
171
+ }
172
+ /**
173
+ * The top-left point of the rectangle. Setting it moves the whole
174
+ * rectangle, unlike {@link #topLeft}.
175
+ */
176
+ get point() {
177
+ return new LinkedPoint(this.x, this.y, this, 'point');
178
+ }
179
+ set point(point) {
180
+ const { x, y } = Point.from(point);
181
+ this.x = x;
182
+ this.y = y;
183
+ }
184
+ /**
185
+ * The size of the rectangle
186
+ */
187
+ get size() {
188
+ return new LinkedSize(this.width, this.height, this, 'size');
189
+ }
190
+ set size(size) {
191
+ const { width, height } = Size.from(size);
192
+ this.width = width;
193
+ this.height = height;
194
+ }
195
+ /**
196
+ * {@grouptitle Side Positions}
197
+ *
198
+ * The position of the left hand side of the rectangle. Note that this
199
+ * doesn't move the whole rectangle; the right hand side stays where it was.
200
+ */
201
+ get left() {
202
+ return this.x;
203
+ }
204
+ set left(left) {
205
+ setSide(this, 'x', 'width', 0, left);
206
+ }
207
+ /**
208
+ * The top coordinate of the rectangle. Note that this doesn't move the
209
+ * whole rectangle: the bottom won't move.
210
+ */
211
+ get top() {
212
+ return this.y;
213
+ }
214
+ set top(top) {
215
+ setSide(this, 'y', 'height', 0, top);
216
+ }
217
+ /**
218
+ * The position of the right hand side of the rectangle. Note that this
219
+ * doesn't move the whole rectangle; the left hand side stays where it was.
220
+ */
221
+ get right() {
222
+ return this.x + this.width;
223
+ }
224
+ set right(right) {
225
+ setSide(this, 'x', 'width', 1, right);
226
+ }
227
+ /**
228
+ * The bottom coordinate of the rectangle. Note that this doesn't move the
229
+ * whole rectangle: the top won't move.
230
+ */
231
+ get bottom() {
232
+ return this.y + this.height;
233
+ }
234
+ set bottom(bottom) {
235
+ setSide(this, 'y', 'height', 1, bottom);
236
+ }
237
+ /**
238
+ * {@grouptitle Corner and Center Point Positions}
239
+ *
240
+ * The center point of the rectangle. Setting it moves the whole rectangle.
241
+ */
242
+ get center() {
243
+ return new LinkedPoint(this.x + this.width / 2, this.y + this.height / 2, this, 'center');
244
+ }
245
+ set center(point) {
246
+ const { x, y } = Point.from(point);
247
+ setSide(this, 'x', 'width', 0.5, x);
248
+ setSide(this, 'y', 'height', 0.5, y);
249
+ }
250
+ /**
251
+ * The area of the rectangle.
252
+ */
253
+ get area() {
254
+ return this.width * this.height;
255
+ }
256
+ /**
257
+ * @return {@true if the rectangle is empty}
258
+ */
259
+ isEmpty() {
260
+ return this.width === 0 || this.height === 0;
261
+ }
262
+ contains(arg, y, width, height) {
263
+ // Detect rectangles either by checking for 'width' on the passed object
264
+ // or by looking at the amount of values passed, as numbers or an array:
265
+ const obj = arg;
266
+ return (obj?.width !== undefined ||
267
+ Array.isArray(obj) && obj.length === 4 ||
268
+ width !== undefined)
269
+ ? this.#containsRectangle(Rectangle.from(arg, y, width, height))
270
+ : this._containsPoint(Point.from(arg, y));
271
+ }
272
+ _containsPoint(point) {
273
+ const { x, y } = point;
274
+ return (x >= this.x &&
275
+ y >= this.y &&
276
+ x <= this.x + this.width &&
277
+ y <= this.y + this.height);
278
+ }
279
+ #containsRectangle(rect) {
280
+ const { x, y } = rect;
281
+ return (x >= this.x &&
282
+ y >= this.y &&
283
+ x + rect.width <= this.x + this.width &&
284
+ y + rect.height <= this.y + this.height);
285
+ }
286
+ /**
287
+ * Tests if the interior of this rectangle intersects the interior of
288
+ * another rectangle. Rectangles just touching each other are considered as
289
+ * non-intersecting, except if a `epsilon` value is specified by which this
290
+ * rectangle's dimensions are increased before comparing.
291
+ *
292
+ * @param rect the specified rectangle
293
+ * @param epsilon the epsilon against which to compare the
294
+ * rectangle's dimensions
295
+ * @return {@true if the rectangle and the specified rectangle
296
+ * intersect each other}
297
+ *
298
+ * @example {@paperscript}
299
+ * // Checking whether the bounding box of one item intersects with
300
+ * // that of another item:
301
+ *
302
+ * // All newly created paths will inherit these styles:
303
+ * project.currentStyle = {
304
+ * fillColor: 'green',
305
+ * strokeColor: 'black'
306
+ * };
307
+ *
308
+ * // Create a circle shaped path at {x: 80, y: 50}
309
+ * // with a radius of 45.
310
+ * var largeCircle = Path.circle(new Point(80, 50), 45);
311
+ *
312
+ * // Create a smaller circle shaped path in the same position
313
+ * // with a radius of 30.
314
+ * var circle = Path.circle(new Point(80, 50), 30);
315
+ *
316
+ * function onMouseMove(event) {
317
+ * // Move the circle to the position of the mouse:
318
+ * circle.position = event.point;
319
+ *
320
+ * // Check whether the bounding box of the two circle
321
+ * // shaped paths intersect:
322
+ * if (largeCircle.bounds.intersects(circle.bounds)) {
323
+ * // If it does, fill it with green:
324
+ * circle.fillColor = 'green';
325
+ * largeCircle.fillColor = 'green';
326
+ * } else {
327
+ * // If doesn't, fill it with red:
328
+ * circle.fillColor = 'red';
329
+ * largeCircle.fillColor = 'red';
330
+ * }
331
+ * }
332
+ */
333
+ intersects(rect, epsilon = 0) {
334
+ const other = Rectangle.from(rect);
335
+ return (other.x + other.width > this.x - epsilon &&
336
+ other.y + other.height > this.y - epsilon &&
337
+ other.x < this.x + this.width + epsilon &&
338
+ other.y < this.y + this.height + epsilon);
339
+ }
340
+ /**
341
+ * {@grouptitle Boolean Operations}
342
+ *
343
+ * Returns a new rectangle representing the intersection of this rectangle
344
+ * with the specified rectangle.
345
+ *
346
+ * @param rect the rectangle to be intersected with this
347
+ * rectangle
348
+ * @return the largest rectangle contained in both the specified
349
+ * rectangle and in this rectangle
350
+ *
351
+ * @example {@paperscript}
352
+ * // Intersecting two rectangles and visualizing the result using rectangle
353
+ * // shaped paths:
354
+ *
355
+ * // Create two rectangles that overlap each other
356
+ * var size = new Size(50, 50);
357
+ * var rectangle1 = new Rectangle(new Point(25, 15), size);
358
+ * var rectangle2 = new Rectangle(new Point(50, 40), size);
359
+ *
360
+ * // The rectangle that represents the intersection of the
361
+ * // two rectangles:
362
+ * var intersected = rectangle1.intersect(rectangle2);
363
+ *
364
+ * // To visualize the intersecting of the rectangles, we will
365
+ * // create rectangle shaped paths using Path.rectangle()
366
+ * // constructor.
367
+ *
368
+ * // Have all newly created paths inherit a black stroke:
369
+ * project.currentStyle.strokeColor = 'black';
370
+ *
371
+ * // Create two rectangle shaped paths using the abstract rectangles
372
+ * // we created before:
373
+ * Path.rectangle(rectangle1);
374
+ * Path.rectangle(rectangle2);
375
+ *
376
+ * // Create a path that represents the intersected rectangle,
377
+ * // and fill it with red:
378
+ * var intersectionPath = Path.rectangle(intersected);
379
+ * intersectionPath.fillColor = 'red';
380
+ */
381
+ intersect(rect, y, width, height) {
382
+ const other = Rectangle.from(rect, y, width, height);
383
+ const x1 = Math.max(this.x, other.x);
384
+ const y1 = Math.max(this.y, other.y);
385
+ const x2 = Math.min(this.x + this.width, other.x + other.width);
386
+ const y2 = Math.min(this.y + this.height, other.y + other.height);
387
+ return new Rectangle(x1, y1, x2 - x1, y2 - y1);
388
+ }
389
+ /**
390
+ * Returns a new rectangle representing the union of this rectangle with the
391
+ * specified rectangle.
392
+ *
393
+ * @param rect the rectangle to be combined with this rectangle
394
+ * @return the smallest rectangle containing both the specified
395
+ * rectangle and this rectangle
396
+ */
397
+ unite(rect, y, width, height) {
398
+ const other = Rectangle.from(rect, y, width, height);
399
+ const x1 = Math.min(this.x, other.x);
400
+ const y1 = Math.min(this.y, other.y);
401
+ const x2 = Math.max(this.x + this.width, other.x + other.width);
402
+ const y2 = Math.max(this.y + this.height, other.y + other.height);
403
+ return new Rectangle(x1, y1, x2 - x1, y2 - y1);
404
+ }
405
+ /**
406
+ * Adds a point to this rectangle. The resulting rectangle is the smallest
407
+ * rectangle that contains both the original rectangle and the specified
408
+ * point.
409
+ *
410
+ * After adding a point, a call to {@link #contains(point)} with the added
411
+ * point as an argument does not necessarily return `true`. The {@link
412
+ * Rectangle#contains(point)} method does not return `true` for points on
413
+ * the right or bottom edges of a rectangle. Therefore, if the added point
414
+ * falls on the left or bottom edge of the enlarged rectangle, {@link
415
+ * Rectangle#contains(point)} returns `false` for that point.
416
+ *
417
+ * @param point
418
+ * @return the smallest rectangle that contains both the
419
+ * original rectangle and the specified point
420
+ */
421
+ include(point, y) {
422
+ const other = Point.from(point, y);
423
+ const x1 = Math.min(this.x, other.x);
424
+ const y1 = Math.min(this.y, other.y);
425
+ const x2 = Math.max(this.x + this.width, other.x);
426
+ const y2 = Math.max(this.y + this.height, other.y);
427
+ return new Rectangle(x1, y1, x2 - x1, y2 - y1);
428
+ }
429
+ expand(amount, ver) {
430
+ const { width, height } = Size.coerce(amount, ver);
431
+ return new Rectangle(this.x - width / 2, this.y - height / 2, this.width + width, this.height + height);
432
+ }
433
+ scale(arg0, arg1, arg2) {
434
+ const hasVer = typeof arg1 === 'number';
435
+ const { x: sx, y: sy } = Point.coerce(arg0, hasVer ? arg1 : undefined);
436
+ const center = (hasVer ? arg2 : arg1) ?? this.center;
437
+ const { x: cx, y: cy } = Point.from(center);
438
+ const { left, top, right, bottom } = this;
439
+ // Scale the corners from the center, which also normalizes the
440
+ // rectangle for negative amounts:
441
+ return new Rectangle(new Point((left - cx) * sx + cx, (top - cy) * sy + cy), new Point((right - cx) * sx + cx, (bottom - cy) * sy + cy));
442
+ }
443
+ /**
444
+ * Returns the passed rectangle if it is one already, or else a new
445
+ * rectangle created from the passed values, as {@link Rectangle()} reads
446
+ * them.
447
+ *
448
+ * @param rect
449
+ * @param y
450
+ * @param width
451
+ * @param height
452
+ */
453
+ static from(rect, y, width, height) {
454
+ return rect instanceof Rectangle
455
+ ? rect
456
+ : new Rectangle(rect, y, width, height);
457
+ }
458
+ /**
459
+ * The top-left point of the rectangle. Note that this doesn't move the
460
+ * whole rectangle; the bottom-right corner stays where it was.
461
+ */
462
+ get topLeft() {
463
+ return this.#getCorner('topLeft');
464
+ }
465
+ set topLeft(point) {
466
+ this.#setCorner('topLeft', point);
467
+ }
468
+ /**
469
+ * The top-right point of the rectangle. Note that this doesn't move the
470
+ * whole rectangle; the bottom-left corner stays where it was.
471
+ */
472
+ get topRight() {
473
+ return this.#getCorner('topRight');
474
+ }
475
+ set topRight(point) {
476
+ this.#setCorner('topRight', point);
477
+ }
478
+ /**
479
+ * The bottom-left point of the rectangle. Note that this doesn't move the
480
+ * whole rectangle; the top-right corner stays where it was.
481
+ */
482
+ get bottomLeft() {
483
+ return this.#getCorner('bottomLeft');
484
+ }
485
+ set bottomLeft(point) {
486
+ this.#setCorner('bottomLeft', point);
487
+ }
488
+ /**
489
+ * The bottom-right point of the rectangle. Note that this doesn't move the
490
+ * whole rectangle; the top-left corner stays where it was.
491
+ */
492
+ get bottomRight() {
493
+ return this.#getCorner('bottomRight');
494
+ }
495
+ set bottomRight(point) {
496
+ this.#setCorner('bottomRight', point);
497
+ }
498
+ /**
499
+ * The left-center point of the rectangle. Setting it moves the left hand
500
+ * side while the right hand side stays where it was, and moves the whole
501
+ * rectangle vertically.
502
+ */
503
+ get leftCenter() {
504
+ return this.#getCorner('leftCenter');
505
+ }
506
+ set leftCenter(point) {
507
+ this.#setCorner('leftCenter', point);
508
+ }
509
+ /**
510
+ * The top-center point of the rectangle. Setting it moves the top while
511
+ * the bottom stays where it was, and moves the whole rectangle
512
+ * horizontally.
513
+ */
514
+ get topCenter() {
515
+ return this.#getCorner('topCenter');
516
+ }
517
+ set topCenter(point) {
518
+ this.#setCorner('topCenter', point);
519
+ }
520
+ /**
521
+ * The right-center point of the rectangle. Setting it moves the right
522
+ * hand side while the left hand side stays where it was, and moves the
523
+ * whole rectangle vertically.
524
+ */
525
+ get rightCenter() {
526
+ return this.#getCorner('rightCenter');
527
+ }
528
+ set rightCenter(point) {
529
+ this.#setCorner('rightCenter', point);
530
+ }
531
+ /**
532
+ * The bottom-center point of the rectangle. Setting it moves the bottom
533
+ * while the top stays where it was, and moves the whole rectangle
534
+ * horizontally.
535
+ */
536
+ get bottomCenter() {
537
+ return this.#getCorner('bottomCenter');
538
+ }
539
+ set bottomCenter(point) {
540
+ this.#setCorner('bottomCenter', point);
541
+ }
542
+ // The corners and the centers of the sides lie between the sides:
543
+ #getCorner(key) {
544
+ const [fx, fy] = corners[key];
545
+ return new LinkedPoint(this.x + this.width * fx, this.y + this.height * fy, this, key);
546
+ }
547
+ #setCorner(key, point) {
548
+ const [fx, fy] = corners[key];
549
+ const { x, y } = Point.from(point);
550
+ setSide(this, 'x', 'width', fx, x);
551
+ setSide(this, 'y', 'height', fy, y);
552
+ }
553
+ static {
554
+ registerClasses({ Rectangle: this });
555
+ }
556
+ }
557
+ /**
558
+ * An internal version of Rectangle that notifies its owner of each change
559
+ * through setting itself again on the property that produced it.
560
+ *
561
+ * @private
562
+ * @internal
563
+ */
564
+ export class LinkedRectangle extends Rectangle {
565
+ #x;
566
+ #y;
567
+ #width;
568
+ #height;
569
+ #owner;
570
+ #key;
571
+ constructor(x, y, width, height, owner, key) {
572
+ super();
573
+ this.#x = x;
574
+ this.#y = y;
575
+ this.#width = width;
576
+ this.#height = height;
577
+ this.#owner = owner;
578
+ this.#key = key;
579
+ }
580
+ // See Point#_set() for an explanation of _set():
581
+ _set(x, y, width, height) {
582
+ // Rectangle's constructor calls this before the private fields exist:
583
+ if (#owner in this) {
584
+ this.#x = x;
585
+ this.#y = y;
586
+ this.#width = width;
587
+ this.#height = height;
588
+ this.#owner[this.#key] = this;
589
+ }
590
+ return this;
591
+ }
592
+ // @ts-expect-error: Rectangle declares x as a field, LinkedRectangle reads #x
593
+ get x() {
594
+ return this.#x;
595
+ }
596
+ set x(x) {
597
+ this.#x = x;
598
+ this.#owner[this.#key] = this;
599
+ }
600
+ // @ts-expect-error: See x
601
+ get y() {
602
+ return this.#y;
603
+ }
604
+ set y(y) {
605
+ this.#y = y;
606
+ this.#owner[this.#key] = this;
607
+ }
608
+ // @ts-expect-error: See x
609
+ get width() {
610
+ return this.#width;
611
+ }
612
+ set width(width) {
613
+ this.#width = width;
614
+ this.#owner[this.#key] = this;
615
+ }
616
+ // @ts-expect-error: See x
617
+ get height() {
618
+ return this.#height;
619
+ }
620
+ set height(height) {
621
+ this.#height = height;
622
+ this.#owner[this.#key] = this;
623
+ }
624
+ get selected() {
625
+ return !!(this.#owner._selection & this.#getSelection());
626
+ }
627
+ set selected(selected) {
628
+ const owner = this.#owner;
629
+ if (owner._changeSelection) {
630
+ owner._changeSelection(this.#getSelection(), selected);
631
+ }
632
+ }
633
+ #getSelection() {
634
+ return this.#key === 'strokeBounds'
635
+ ? ItemSelection.STROKE_BOUNDS
636
+ : ItemSelection.BOUNDS;
637
+ }
638
+ static {
639
+ // Have the properties derived from x, y, width and height change a plain
640
+ // copy, so the owner is notified only once per change:
641
+ const keys = [
642
+ 'point',
643
+ 'size',
644
+ 'center',
645
+ 'left',
646
+ 'top',
647
+ 'right',
648
+ 'bottom',
649
+ ...Object.keys(corners)
650
+ ];
651
+ for (const key of keys) {
652
+ // The keys above are all accessors:
653
+ const get = Object.getOwnPropertyDescriptor(Rectangle.prototype, key).get;
654
+ inject(this.prototype, {
655
+ get [key]() {
656
+ return get.call(this);
657
+ },
658
+ set [key](value) {
659
+ const rect = this.clone();
660
+ Reflect.set(rect, key, value);
661
+ this._set(rect.x, rect.y, rect.width, rect.height);
662
+ }
663
+ });
664
+ }
665
+ }
666
+ }
667
+ // Moves the side of the rectangle that lies at the fraction of its width or
668
+ // height, keeping the opposite side, or the whole rectangle at its center:
669
+ function setSide(rect, key, size, fraction, value) {
670
+ if (fraction === 0.5) {
671
+ rect[key] = value - rect[size] / 2;
672
+ }
673
+ else if (fraction === 1) {
674
+ rect[size] = value - rect[key];
675
+ }
676
+ else {
677
+ rect[size] -= value - rect[key];
678
+ rect[key] = value;
679
+ }
680
+ }
681
+ // The points that objects can describe rectangles with, and where they lie:
682
+ const pointKeys = Object.entries({
683
+ point: [0, 0],
684
+ center: [0.5, 0.5],
685
+ ...corners
686
+ }).map(([key, [fx, fy]]) => [key, fx, fy]);
687
+ // Reads an object that describes a rectangle through any of its properties,
688
+ // in any order: per axis, two of its start, center, end and length do.
689
+ function readObject(obj) {
690
+ // The values at the start, center and end of each axis:
691
+ const xs = [obj.x ?? obj.left, undefined, obj.right];
692
+ const ys = [obj.y ?? obj.top, undefined, obj.bottom];
693
+ for (const [key, fx, fy] of pointKeys) {
694
+ if (obj[key] !== undefined) {
695
+ const { x, y } = Point.from(obj[key]);
696
+ xs[fx * 2] ??= x;
697
+ ys[fy * 2] ??= y;
698
+ }
699
+ }
700
+ const size = obj.size !== undefined ? Size.from(obj.size) : null;
701
+ const [x, width] = readAxis(xs, obj.width ?? size?.width);
702
+ const [y, height] = readAxis(ys, obj.height ?? size?.height);
703
+ return [x, y, width, height];
704
+ }
705
+ // Returns the start and length of an axis from two of its start, center, end
706
+ // and length:
707
+ function readAxis([start, center, end], length) {
708
+ length ??=
709
+ end !== undefined
710
+ ? start !== undefined
711
+ ? end - start
712
+ : center !== undefined
713
+ ? (end - center) * 2
714
+ : 0
715
+ : center !== undefined && start !== undefined
716
+ ? (center - start) * 2
717
+ : 0;
718
+ start ??=
719
+ end !== undefined
720
+ ? end - length
721
+ : center !== undefined
722
+ ? center - length / 2
723
+ : 0;
724
+ return [start, length];
725
+ }
726
+ /**
727
+ * Tells whether the first of several arguments describes a rectangle, rather
728
+ * than the point of (point, size) or (from, to): an array of four values, or
729
+ * an object with both `x` and `width`.
730
+ *
731
+ * @private
732
+ */
733
+ export function isRectangle(value) {
734
+ return Array.isArray(value)
735
+ ? value.length === 4
736
+ : value.x !== undefined && value.width !== undefined;
737
+ }
738
+ /**
739
+ * Reads the rectangle that a named object describes, through its `rectangle`
740
+ * or through any of the rectangle's properties, and returns it with the
741
+ * object's other properties, for set(), as the Path and Shape factories use it.
742
+ *
743
+ * @private
744
+ */
745
+ export function readRectangle(object) {
746
+ const { rectangle, ...rest } = object;
747
+ if (rectangle) {
748
+ return [Rectangle.from(rectangle), rest];
749
+ }
750
+ else {
751
+ // Tell the rectangle's properties by the ones a rectangle has, plus the
752
+ // `from` and `to` that its constructor takes:
753
+ const supported = new Rectangle();
754
+ const values = {};
755
+ const props = {};
756
+ for (const [key, value] of Object.entries(rest)) {
757
+ if (key in supported || key === 'from' || key === 'to') {
758
+ values[key] = value;
759
+ }
760
+ else {
761
+ props[key] = value;
762
+ }
763
+ }
764
+ return [new Rectangle(values), props];
765
+ }
766
+ }
767
+ /**
768
+ * Reads the center and radius of an ellipse from a named object, either from
769
+ * its `center` and `radius`, or from the rectangle that it describes, see
770
+ * readRectangle(), and returns them with the object's other properties, for
771
+ * set(), as the Path and Shape factories use it.
772
+ *
773
+ * @private
774
+ */
775
+ export function readEllipse(object) {
776
+ if (object.radius != null) {
777
+ const { center, radius, ...props } = object;
778
+ return { center: Point.from(center), radius: Size.from(radius), props };
779
+ }
780
+ else {
781
+ const [rect, props] = readRectangle(object);
782
+ return {
783
+ center: rect.center.clone(),
784
+ radius: rect.size.divide(2),
785
+ props
786
+ };
787
+ }
788
+ }