fabric 5.3.0 → 6.0.0-beta3

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 (604) hide show
  1. package/.babelrc +11 -0
  2. package/.babelrcAlt +19 -0
  3. package/.browserslistrc +5 -0
  4. package/.eslintignore +3 -0
  5. package/.eslintrc.js +43 -0
  6. package/.eslintrc.json +38 -46
  7. package/.eslintrc_tests +12 -0
  8. package/.gitattributes +2 -0
  9. package/.gitpod.yml +17 -1
  10. package/.prettierignore +19 -0
  11. package/.prettierrc +4 -0
  12. package/CHANGELOG.md +532 -189
  13. package/CONTRIBUTING.md +224 -59
  14. package/README.md +200 -235
  15. package/bower.json +1 -3
  16. package/dist/fabric.d.ts +42 -0
  17. package/dist/index.d.ts +2 -0
  18. package/dist/index.js +27582 -0
  19. package/dist/index.js.map +1 -0
  20. package/dist/index.min.js +1 -0
  21. package/dist/index.mjs +27519 -0
  22. package/dist/index.mjs.map +1 -0
  23. package/dist/index.node.cjs +27663 -0
  24. package/dist/index.node.cjs.map +1 -0
  25. package/dist/index.node.d.ts +14 -0
  26. package/dist/index.node.mjs +27606 -0
  27. package/dist/index.node.mjs.map +1 -0
  28. package/dist/src/ClassRegistry.d.ts +13 -0
  29. package/dist/src/Collection.d.ts +136 -0
  30. package/dist/src/CommonMethods.d.ts +32 -0
  31. package/dist/src/EventTypeDefs.d.ts +227 -0
  32. package/dist/src/Intersection.d.ts +112 -0
  33. package/dist/src/Observable.d.ts +60 -0
  34. package/dist/src/Pattern.d.ts +113 -0
  35. package/dist/src/Point.d.ts +232 -0
  36. package/dist/src/Shadow.d.ts +97 -0
  37. package/dist/src/brushes/BaseBrush.d.ts +105 -0
  38. package/dist/src/brushes/CircleBrush.d.ts +49 -0
  39. package/dist/src/brushes/PatternBrush.d.ts +23 -0
  40. package/dist/src/brushes/PencilBrush.d.ts +92 -0
  41. package/dist/src/brushes/SprayBrush.d.ts +79 -0
  42. package/dist/src/cache.d.ts +47 -0
  43. package/dist/src/canvas/Canvas.d.ts +548 -0
  44. package/dist/src/canvas/SelectableCanvas.d.ts +754 -0
  45. package/dist/src/canvas/StaticCanvas.d.ts +778 -0
  46. package/dist/src/canvas/TextEditingManager.d.ts +17 -0
  47. package/dist/src/canvas/canvas_gestures.mixin.d.ts +2 -0
  48. package/dist/src/color/Color.d.ts +174 -0
  49. package/dist/src/color/color_map.d.ts +155 -0
  50. package/dist/src/color/constants.d.ts +22 -0
  51. package/dist/src/color/util.d.ts +12 -0
  52. package/dist/src/config.d.ts +115 -0
  53. package/dist/src/constants.d.ts +14 -0
  54. package/dist/src/controls/Control.d.ts +228 -0
  55. package/dist/src/controls/changeWidth.d.ts +13 -0
  56. package/dist/src/controls/commonControls.d.ts +28 -0
  57. package/dist/src/controls/controlRendering.d.ts +29 -0
  58. package/dist/src/controls/drag.d.ts +12 -0
  59. package/dist/src/controls/index.d.ts +13 -0
  60. package/dist/src/controls/polyControl.d.ts +5 -0
  61. package/dist/src/controls/rotate.d.ts +12 -0
  62. package/dist/src/controls/scale.d.ts +47 -0
  63. package/dist/src/controls/scaleSkew.d.ts +39 -0
  64. package/dist/src/controls/skew.d.ts +33 -0
  65. package/dist/src/controls/util.d.ts +41 -0
  66. package/dist/src/controls/wrapWithFireEvent.d.ts +8 -0
  67. package/dist/src/controls/wrapWithFixedAnchor.d.ts +9 -0
  68. package/dist/src/env/browser.d.ts +3 -0
  69. package/dist/src/env/index.d.ts +15 -0
  70. package/dist/src/env/node.d.ts +5 -0
  71. package/dist/src/env/types.d.ts +15 -0
  72. package/dist/src/filters/BaseFilter.d.ts +153 -0
  73. package/dist/src/filters/BlendColor.d.ts +80 -0
  74. package/dist/src/filters/BlendImage.d.ts +92 -0
  75. package/dist/src/filters/Blur.d.ts +51 -0
  76. package/dist/src/filters/Boilerplate.d.ts +48 -0
  77. package/dist/src/filters/Brightness.d.ts +47 -0
  78. package/dist/src/filters/Canvas2dFilterBackend.d.ts +27 -0
  79. package/dist/src/filters/ColorMatrix.d.ts +63 -0
  80. package/dist/src/filters/ColorMatrixFilters.d.ts +545 -0
  81. package/dist/src/filters/Composed.d.ts +45 -0
  82. package/dist/src/filters/Contrast.d.ts +45 -0
  83. package/dist/src/filters/Convolute.d.ts +82 -0
  84. package/dist/src/filters/FilterBackend.d.ts +14 -0
  85. package/dist/src/filters/GLProbes/GLProbe.d.ts +11 -0
  86. package/dist/src/filters/GLProbes/NodeGLProbe.d.ts +11 -0
  87. package/dist/src/filters/GLProbes/WebGLProbe.d.ts +20 -0
  88. package/dist/src/filters/Gamma.d.ts +54 -0
  89. package/dist/src/filters/Grayscale.d.ts +46 -0
  90. package/dist/src/filters/HueRotation.d.ts +24 -0
  91. package/dist/src/filters/Invert.d.ts +55 -0
  92. package/dist/src/filters/Noise.d.ts +54 -0
  93. package/dist/src/filters/Pixelate.d.ts +44 -0
  94. package/dist/src/filters/RemoveColor.d.ts +63 -0
  95. package/dist/src/filters/Resize.d.ts +136 -0
  96. package/dist/src/filters/Saturation.d.ts +48 -0
  97. package/dist/src/filters/Vibrance.d.ts +48 -0
  98. package/dist/src/filters/WebGLFilterBackend.d.ts +126 -0
  99. package/dist/src/filters/filters.d.ts +21 -0
  100. package/dist/src/filters/index.d.ts +5 -0
  101. package/dist/src/filters/shaders/baseFilter.d.ts +4 -0
  102. package/dist/src/filters/shaders/blendColor.d.ts +13 -0
  103. package/dist/src/filters/shaders/blendImage.d.ts +3 -0
  104. package/dist/src/filters/shaders/blur.d.ts +2 -0
  105. package/dist/src/filters/shaders/brightness.d.ts +2 -0
  106. package/dist/src/filters/shaders/colorMatrix.d.ts +2 -0
  107. package/dist/src/filters/shaders/constrast.d.ts +2 -0
  108. package/dist/src/filters/shaders/convolute.d.ts +11 -0
  109. package/dist/src/filters/shaders/gamma.d.ts +2 -0
  110. package/dist/src/filters/shaders/grayscale.d.ts +3 -0
  111. package/dist/src/filters/shaders/invert.d.ts +2 -0
  112. package/dist/src/filters/shaders/noise.d.ts +2 -0
  113. package/dist/src/filters/shaders/pixelate.d.ts +2 -0
  114. package/dist/src/filters/shaders/removeColor.d.ts +2 -0
  115. package/dist/src/filters/shaders/saturation.d.ts +2 -0
  116. package/dist/src/filters/shaders/vibrance.d.ts +2 -0
  117. package/dist/src/filters/typedefs.d.ts +49 -0
  118. package/dist/src/gradient/Gradient.d.ts +149 -0
  119. package/dist/src/gradient/constants.d.ts +15 -0
  120. package/dist/src/gradient/parser/index.d.ts +4 -0
  121. package/dist/src/gradient/parser/misc.d.ts +4 -0
  122. package/dist/src/gradient/parser/parseColorStops.d.ts +3 -0
  123. package/dist/src/gradient/parser/parseCoords.d.ts +17 -0
  124. package/dist/src/gradient/typedefs.d.ts +93 -0
  125. package/dist/src/mixins/eraser_brush.mixin.d.ts +2 -0
  126. package/dist/src/parkinglot/canvas_animation.mixin.d.ts +2 -0
  127. package/dist/src/parkinglot/straighten.d.ts +2 -0
  128. package/dist/src/parser/applyViewboxTransform.d.ts +5 -0
  129. package/dist/src/parser/attributes.d.ts +6 -0
  130. package/dist/src/parser/constants.d.ts +53 -0
  131. package/dist/src/parser/doesSomeParentMatch.d.ts +2 -0
  132. package/dist/src/parser/elementById.d.ts +6 -0
  133. package/dist/src/parser/elementMatchesRule.d.ts +5 -0
  134. package/dist/src/parser/elements_parser.d.ts +3 -0
  135. package/dist/src/parser/getCSSRules.d.ts +7 -0
  136. package/dist/src/parser/getGlobalStylesForElement.d.ts +5 -0
  137. package/dist/src/parser/getGradientDefs.d.ts +7 -0
  138. package/dist/src/parser/getMultipleNodes.d.ts +2 -0
  139. package/dist/src/parser/getSvgRegex.d.ts +2 -0
  140. package/dist/src/parser/hasAncestorWithNodeName.d.ts +2 -0
  141. package/dist/src/parser/index.d.ts +10 -0
  142. package/dist/src/parser/loadSVGFromString.d.ts +12 -0
  143. package/dist/src/parser/loadSVGFromURL.d.ts +13 -0
  144. package/dist/src/parser/normalizeAttr.d.ts +2 -0
  145. package/dist/src/parser/normalizeValue.d.ts +2 -0
  146. package/dist/src/parser/parseAttributes.d.ts +9 -0
  147. package/dist/src/parser/parseElements.d.ts +11 -0
  148. package/dist/src/parser/parseFontDeclaration.d.ts +10 -0
  149. package/dist/src/parser/parsePointsAttribute.d.ts +12 -0
  150. package/dist/src/parser/parseSVGDocument.d.ts +15 -0
  151. package/dist/src/parser/parseStyleAttribute.d.ts +9 -0
  152. package/dist/src/parser/parseStyleObject.d.ts +2 -0
  153. package/dist/src/parser/parseStyleString.d.ts +2 -0
  154. package/dist/src/parser/parseTransformAttribute.d.ts +10 -0
  155. package/dist/src/parser/parseUseDirectives.d.ts +2 -0
  156. package/dist/src/parser/percent.d.ts +9 -0
  157. package/dist/src/parser/recursivelyParseGradientsXlink.d.ts +2 -0
  158. package/dist/src/parser/rotateMatrix.d.ts +2 -0
  159. package/dist/src/parser/scaleMatrix.d.ts +2 -0
  160. package/dist/src/parser/selectorMatches.d.ts +2 -0
  161. package/dist/src/parser/setStrokeFillOpacity.d.ts +6 -0
  162. package/dist/src/parser/skewMatrix.d.ts +2 -0
  163. package/dist/src/parser/translateMatrix.d.ts +2 -0
  164. package/dist/src/shapes/ActiveSelection.d.ts +82 -0
  165. package/dist/src/shapes/Circle.d.ts +99 -0
  166. package/dist/src/shapes/Ellipse.d.ts +84 -0
  167. package/dist/src/shapes/Group.d.ts +408 -0
  168. package/dist/src/shapes/IText/DraggableTextDelegate.d.ts +78 -0
  169. package/dist/src/shapes/IText/IText.d.ts +335 -0
  170. package/dist/src/shapes/IText/ITextBehavior.d.ts +287 -0
  171. package/dist/src/shapes/IText/ITextClickBehavior.d.ts +83 -0
  172. package/dist/src/shapes/IText/ITextKeyBehavior.d.ts +195 -0
  173. package/dist/src/shapes/IText/constants.d.ts +13 -0
  174. package/dist/src/shapes/Image.d.ts +311 -0
  175. package/dist/src/shapes/Line.d.ts +126 -0
  176. package/dist/src/shapes/Object/AnimatableObject.d.ts +33 -0
  177. package/dist/src/shapes/Object/FabricObject.d.ts +11 -0
  178. package/dist/src/shapes/Object/FabricObjectSVGExportMixin.d.ts +74 -0
  179. package/dist/src/shapes/Object/InteractiveObject.d.ts +296 -0
  180. package/dist/src/shapes/Object/Object.d.ts +594 -0
  181. package/dist/src/shapes/Object/ObjectGeometry.d.ts +327 -0
  182. package/dist/src/shapes/Object/ObjectOrigin.d.ts +109 -0
  183. package/dist/src/shapes/Object/StackedObject.d.ts +72 -0
  184. package/dist/src/shapes/Object/defaultValues.d.ts +76 -0
  185. package/dist/src/shapes/Object/types/BaseProps.d.ts +84 -0
  186. package/dist/src/shapes/Object/types/BorderProps.d.ts +37 -0
  187. package/dist/src/shapes/Object/types/ControlProps.d.ts +62 -0
  188. package/dist/src/shapes/Object/types/FabricObjectProps.d.ts +96 -0
  189. package/dist/src/shapes/Object/types/FillStrokeProps.d.ts +80 -0
  190. package/dist/src/shapes/Object/types/LockInteractionProps.d.ts +51 -0
  191. package/dist/src/shapes/Object/types/ObjectProps.d.ts +42 -0
  192. package/dist/src/shapes/Object/types/SerializedObjectProps.d.ts +66 -0
  193. package/dist/src/shapes/Object/types/index.d.ts +6 -0
  194. package/dist/src/shapes/Path.d.ts +130 -0
  195. package/dist/src/shapes/Polygon.d.ts +9 -0
  196. package/dist/src/shapes/Polyline.d.ts +140 -0
  197. package/dist/src/shapes/Rect.d.ts +76 -0
  198. package/dist/src/shapes/Text/StyledText.d.ts +119 -0
  199. package/dist/src/shapes/Text/Text.d.ts +673 -0
  200. package/dist/src/shapes/Text/TextSVGExportMixin.d.ts +32 -0
  201. package/dist/src/shapes/Text/constants.d.ts +7 -0
  202. package/dist/src/shapes/Textbox.d.ts +185 -0
  203. package/dist/src/shapes/Triangle.d.ts +25 -0
  204. package/dist/src/typedefs.d.ts +90 -0
  205. package/dist/src/util/animation/AnimationBase.d.ts +54 -0
  206. package/dist/src/util/animation/AnimationFrameProvider.d.ts +3 -0
  207. package/dist/src/util/animation/AnimationRegistry.d.ts +29 -0
  208. package/dist/src/util/animation/ArrayAnimation.d.ts +10 -0
  209. package/dist/src/util/animation/ColorAnimation.d.ts +11 -0
  210. package/dist/src/util/animation/ValueAnimation.d.ts +10 -0
  211. package/dist/src/util/animation/animate.d.ts +37 -0
  212. package/dist/src/util/animation/easing.d.ts +130 -0
  213. package/dist/src/util/animation/types.d.ts +87 -0
  214. package/dist/src/util/applyMixins.d.ts +8 -0
  215. package/dist/src/util/dom_event.d.ts +5 -0
  216. package/dist/src/util/dom_misc.d.ts +39 -0
  217. package/dist/src/util/dom_request.d.ts +14 -0
  218. package/dist/src/util/dom_style.d.ts +7 -0
  219. package/dist/src/util/fireEvent.d.ts +3 -0
  220. package/dist/src/util/index.d.ts +35 -0
  221. package/dist/src/util/internals/cloneDeep.d.ts +2 -0
  222. package/dist/src/util/internals/getRandomInt.d.ts +8 -0
  223. package/dist/src/util/internals/ifNaN.d.ts +8 -0
  224. package/dist/src/util/internals/index.d.ts +4 -0
  225. package/dist/src/util/internals/removeFromArray.d.ts +9 -0
  226. package/dist/src/util/internals/uid.d.ts +2 -0
  227. package/dist/src/util/lang_string.d.ts +22 -0
  228. package/dist/src/util/misc/boundingBoxFromPoints.d.ts +9 -0
  229. package/dist/src/util/misc/capValue.d.ts +2 -0
  230. package/dist/src/util/misc/cos.d.ts +10 -0
  231. package/dist/src/util/misc/dom.d.ts +28 -0
  232. package/dist/src/util/misc/findScaleTo.d.ts +29 -0
  233. package/dist/src/util/misc/groupSVGElements.d.ts +9 -0
  234. package/dist/src/util/misc/isTransparent.d.ts +11 -0
  235. package/dist/src/util/misc/matrix.d.ts +92 -0
  236. package/dist/src/util/misc/mergeClipPaths.d.ts +23 -0
  237. package/dist/src/util/misc/objectEnlive.d.ts +56 -0
  238. package/dist/src/util/misc/objectTransforms.d.ts +68 -0
  239. package/dist/src/util/misc/pick.d.ts +9 -0
  240. package/dist/src/util/misc/planeChange.d.ts +84 -0
  241. package/dist/src/util/misc/projectStroke/StrokeLineCapProjections.d.ts +51 -0
  242. package/dist/src/util/misc/projectStroke/StrokeLineJoinProjections.d.ts +81 -0
  243. package/dist/src/util/misc/projectStroke/StrokeProjectionsBase.d.ts +25 -0
  244. package/dist/src/util/misc/projectStroke/index.d.ts +11 -0
  245. package/dist/src/util/misc/projectStroke/types.d.ts +23 -0
  246. package/dist/src/util/misc/radiansDegreesConversion.d.ts +14 -0
  247. package/dist/src/util/misc/resolveOrigin.d.ts +9 -0
  248. package/dist/src/util/misc/rotatePoint.d.ts +12 -0
  249. package/dist/src/util/misc/sin.d.ts +10 -0
  250. package/dist/src/util/misc/svgParsing.d.ts +53 -0
  251. package/dist/src/util/misc/textStyles.d.ts +32 -0
  252. package/dist/src/util/misc/toFixed.d.ts +8 -0
  253. package/dist/src/util/misc/vectors.d.ts +57 -0
  254. package/dist/src/util/path/index.d.ts +96 -0
  255. package/dist/src/util/path/regex.d.ts +3 -0
  256. package/dist/src/util/path/typechecks.d.ts +24 -0
  257. package/dist/src/util/path/typedefs.d.ts +220 -0
  258. package/dist/src/util/transform_matrix_removal.d.ts +14 -0
  259. package/dist/src/util/types.d.ts +19 -0
  260. package/fabric.ts +51 -0
  261. package/index.node.ts +37 -0
  262. package/index.ts +1 -0
  263. package/package.json +100 -40
  264. package/publish.js +0 -26
  265. package/rollup.config.mjs +91 -0
  266. package/rollup.test.config.js +24 -0
  267. package/scripts/build.mjs +50 -0
  268. package/scripts/buildLock.mjs +115 -0
  269. package/scripts/buildReporter.mjs +15 -0
  270. package/scripts/buildStats.mjs +139 -0
  271. package/scripts/dirname.mjs +14 -0
  272. package/scripts/git.mjs +36 -0
  273. package/scripts/index.mjs +564 -0
  274. package/scripts/sandbox.mjs +149 -0
  275. package/src/ClassRegistry.ts +56 -0
  276. package/src/Collection.ts +346 -0
  277. package/src/CommonMethods.ts +63 -0
  278. package/src/EventTypeDefs.ts +296 -0
  279. package/src/Intersection.ts +302 -0
  280. package/src/Observable.ts +181 -0
  281. package/src/Pattern.ts +227 -0
  282. package/src/Point.ts +388 -0
  283. package/src/Shadow.ts +214 -0
  284. package/src/brushes/{base_brush.class.js → BaseBrush.ts} +65 -42
  285. package/src/brushes/CircleBrush.ts +145 -0
  286. package/src/brushes/PatternBrush.ts +70 -0
  287. package/src/brushes/PencilBrush.ts +300 -0
  288. package/src/brushes/SprayBrush.ts +219 -0
  289. package/src/cache.ts +90 -0
  290. package/src/canvas/Canvas.ts +1607 -0
  291. package/src/canvas/SelectableCanvas.ts +1608 -0
  292. package/src/canvas/StaticCanvas.ts +1734 -0
  293. package/src/canvas/TextEditingManager.ts +48 -0
  294. package/src/canvas/canvas_gestures.mixin.ts +207 -0
  295. package/src/color/Color.ts +404 -0
  296. package/src/color/color_map.ts +154 -0
  297. package/src/color/constants.ts +26 -0
  298. package/src/color/util.ts +32 -0
  299. package/src/config.ts +159 -0
  300. package/src/constants.ts +20 -0
  301. package/src/controls/Control.ts +380 -0
  302. package/src/controls/changeWidth.ts +52 -0
  303. package/src/controls/commonControls.ts +105 -0
  304. package/src/controls/controlRendering.ts +138 -0
  305. package/src/controls/drag.ts +31 -0
  306. package/src/controls/index.ts +22 -0
  307. package/src/controls/polyControl.ts +135 -0
  308. package/src/controls/rotate.ts +87 -0
  309. package/src/controls/scale.ts +277 -0
  310. package/src/controls/scaleSkew.ts +92 -0
  311. package/src/controls/skew.ts +242 -0
  312. package/src/controls/util.ts +154 -0
  313. package/src/controls/wrapWithFireEvent.ts +25 -0
  314. package/src/controls/wrapWithFixedAnchor.ts +20 -0
  315. package/src/env/browser.ts +32 -0
  316. package/src/env/index.ts +24 -0
  317. package/src/env/node.ts +56 -0
  318. package/src/env/types.ts +15 -0
  319. package/src/filters/{base_filter.class.js → BaseFilter.ts} +192 -151
  320. package/src/filters/BlendColor.ts +217 -0
  321. package/src/filters/BlendImage.ts +228 -0
  322. package/src/filters/Blur.ts +179 -0
  323. package/src/filters/Boilerplate.ts +94 -0
  324. package/src/filters/Brightness.ts +83 -0
  325. package/src/filters/Canvas2dFilterBackend.ts +65 -0
  326. package/src/filters/ColorMatrix.ts +145 -0
  327. package/src/filters/ColorMatrixFilters.ts +77 -0
  328. package/src/filters/Composed.ts +76 -0
  329. package/src/filters/Contrast.ts +82 -0
  330. package/src/filters/Convolute.ts +184 -0
  331. package/src/filters/FilterBackend.ts +34 -0
  332. package/src/filters/GLProbes/GLProbe.ts +11 -0
  333. package/src/filters/GLProbes/NodeGLProbe.ts +15 -0
  334. package/src/filters/GLProbes/WebGLProbe.ts +46 -0
  335. package/src/filters/Gamma.ts +110 -0
  336. package/src/filters/Grayscale.ts +102 -0
  337. package/src/filters/HueRotation.ts +62 -0
  338. package/src/filters/Invert.ts +99 -0
  339. package/src/filters/Noise.ts +94 -0
  340. package/src/filters/Pixelate.ts +96 -0
  341. package/src/filters/RemoveColor.ts +135 -0
  342. package/src/filters/Resize.ts +538 -0
  343. package/src/filters/Saturation.ts +87 -0
  344. package/src/filters/Vibrance.ts +88 -0
  345. package/src/filters/WebGLFilterBackend.ts +430 -0
  346. package/src/filters/filters.ts +28 -0
  347. package/src/filters/index.ts +5 -0
  348. package/src/filters/shaders/baseFilter.ts +19 -0
  349. package/src/filters/shaders/blendColor.ts +33 -0
  350. package/src/filters/shaders/blendImage.ts +32 -0
  351. package/src/filters/shaders/blur.ts +24 -0
  352. package/src/filters/shaders/brightness.ts +11 -0
  353. package/src/filters/shaders/colorMatrix.ts +12 -0
  354. package/src/filters/shaders/constrast.ts +11 -0
  355. package/src/filters/shaders/convolute.ts +154 -0
  356. package/src/filters/shaders/gamma.ts +15 -0
  357. package/src/filters/shaders/grayscale.ts +36 -0
  358. package/src/filters/shaders/invert.ts +19 -0
  359. package/src/filters/shaders/noise.ts +16 -0
  360. package/src/filters/shaders/pixelate.ts +19 -0
  361. package/src/filters/shaders/removeColor.ts +13 -0
  362. package/src/filters/shaders/saturation.ts +15 -0
  363. package/src/filters/shaders/vibrance.ts +16 -0
  364. package/src/filters/typedefs.ts +65 -0
  365. package/src/gradient/Gradient.ts +406 -0
  366. package/src/gradient/constants.ts +12 -0
  367. package/src/gradient/parser/index.ts +3 -0
  368. package/src/gradient/parser/misc.ts +13 -0
  369. package/src/gradient/parser/parseColorStops.ts +56 -0
  370. package/src/gradient/parser/parseCoords.ts +73 -0
  371. package/src/gradient/typedefs.ts +104 -0
  372. package/src/mixins/{eraser_brush.mixin.js → eraser_brush.mixin.ts} +350 -239
  373. package/src/parkinglot/canvas_animation.mixin.ts +121 -0
  374. package/src/parkinglot/straighten.ts +58 -0
  375. package/src/parser/applyViewboxTransform.ts +157 -0
  376. package/src/parser/attributes.ts +25 -0
  377. package/src/parser/constants.ts +115 -0
  378. package/src/parser/doesSomeParentMatch.ts +19 -0
  379. package/src/parser/elementById.ts +21 -0
  380. package/src/parser/elementMatchesRule.ts +16 -0
  381. package/src/parser/elements_parser.ts +191 -0
  382. package/src/parser/getCSSRules.ts +62 -0
  383. package/src/parser/getGlobalStylesForElement.ts +19 -0
  384. package/src/parser/getGradientDefs.ts +31 -0
  385. package/src/parser/getMultipleNodes.ts +15 -0
  386. package/src/parser/getSvgRegex.ts +5 -0
  387. package/src/parser/hasAncestorWithNodeName.ts +14 -0
  388. package/src/parser/index.ts +9 -0
  389. package/src/parser/loadSVGFromString.ts +26 -0
  390. package/src/parser/loadSVGFromURL.ts +40 -0
  391. package/src/parser/normalizeAttr.ts +10 -0
  392. package/src/parser/normalizeValue.ts +63 -0
  393. package/src/parser/parseAttributes.ts +90 -0
  394. package/src/parser/parseElements.ts +28 -0
  395. package/src/parser/parseFontDeclaration.ts +44 -0
  396. package/src/parser/parsePointsAttribute.ts +34 -0
  397. package/src/parser/parseSVGDocument.ts +93 -0
  398. package/src/parser/parseStyleAttribute.ts +27 -0
  399. package/src/parser/parseStyleObject.ts +15 -0
  400. package/src/parser/parseStyleString.ts +16 -0
  401. package/src/parser/parseTransformAttribute.ts +155 -0
  402. package/src/parser/parseUseDirectives.ts +78 -0
  403. package/src/parser/percent.ts +27 -0
  404. package/src/parser/recursivelyParseGradientsXlink.ts +42 -0
  405. package/src/parser/rotateMatrix.ts +21 -0
  406. package/src/parser/scaleMatrix.ts +9 -0
  407. package/src/parser/selectorMatches.ts +25 -0
  408. package/src/parser/setStrokeFillOpacity.ts +40 -0
  409. package/src/parser/skewMatrix.ts +6 -0
  410. package/src/parser/translateMatrix.ts +8 -0
  411. package/src/shapes/ActiveSelection.ts +189 -0
  412. package/src/shapes/Circle.ts +242 -0
  413. package/src/shapes/Ellipse.ts +179 -0
  414. package/src/shapes/Group.ts +1100 -0
  415. package/src/shapes/IText/DraggableTextDelegate.ts +382 -0
  416. package/src/shapes/IText/IText.ts +693 -0
  417. package/src/shapes/IText/ITextBehavior.ts +1064 -0
  418. package/src/shapes/IText/ITextClickBehavior.ts +325 -0
  419. package/src/shapes/IText/ITextKeyBehavior.ts +685 -0
  420. package/src/shapes/IText/constants.ts +47 -0
  421. package/src/shapes/Image.ts +841 -0
  422. package/src/shapes/Line.ts +346 -0
  423. package/src/shapes/Object/AnimatableObject.ts +106 -0
  424. package/src/shapes/Object/FabricObject.ts +29 -0
  425. package/src/shapes/Object/FabricObjectSVGExportMixin.ts +277 -0
  426. package/src/shapes/Object/InteractiveObject.ts +672 -0
  427. package/src/shapes/Object/Object.ts +1561 -0
  428. package/src/shapes/Object/ObjectGeometry.ts +813 -0
  429. package/src/shapes/Object/ObjectOrigin.ts +276 -0
  430. package/src/shapes/Object/StackedObject.ts +206 -0
  431. package/src/shapes/Object/defaultValues.ts +108 -0
  432. package/src/shapes/Object/types/BaseProps.ts +96 -0
  433. package/src/shapes/Object/types/BorderProps.ts +40 -0
  434. package/src/shapes/Object/types/ControlProps.ts +69 -0
  435. package/src/shapes/Object/types/FabricObjectProps.ts +111 -0
  436. package/src/shapes/Object/types/FillStrokeProps.ts +90 -0
  437. package/src/shapes/Object/types/LockInteractionProps.ts +57 -0
  438. package/src/shapes/Object/types/ObjectProps.ts +46 -0
  439. package/src/shapes/Object/types/SerializedObjectProps.ts +73 -0
  440. package/src/shapes/Object/types/index.ts +8 -0
  441. package/src/shapes/Path.ts +416 -0
  442. package/src/shapes/Polygon.ts +20 -0
  443. package/src/shapes/Polyline.ts +359 -0
  444. package/src/shapes/Rect.ts +233 -0
  445. package/src/shapes/Text/StyledText.ts +329 -0
  446. package/src/shapes/Text/Text.ts +1884 -0
  447. package/src/shapes/Text/TextSVGExportMixin.ts +288 -0
  448. package/src/shapes/Text/constants.ts +91 -0
  449. package/src/shapes/Textbox.ts +477 -0
  450. package/src/shapes/Triangle.ts +60 -0
  451. package/src/typedefs.ts +115 -0
  452. package/src/util/animation/AnimationBase.ts +166 -0
  453. package/src/util/animation/AnimationFrameProvider.ts +9 -0
  454. package/src/util/animation/AnimationRegistry.ts +58 -0
  455. package/src/util/animation/ArrayAnimation.ts +27 -0
  456. package/src/util/animation/ColorAnimation.ts +74 -0
  457. package/src/util/animation/ValueAnimation.ts +29 -0
  458. package/src/util/animation/animate.ts +74 -0
  459. package/src/util/animation/easing.ts +327 -0
  460. package/src/util/animation/types.ts +136 -0
  461. package/src/util/applyMixins.ts +22 -0
  462. package/src/util/dom_event.ts +28 -0
  463. package/src/util/dom_misc.ts +121 -0
  464. package/src/util/dom_request.ts +64 -0
  465. package/src/util/dom_style.ts +20 -0
  466. package/src/util/fireEvent.ts +15 -0
  467. package/src/util/index.ts +102 -0
  468. package/src/util/internals/cloneDeep.ts +2 -0
  469. package/src/util/internals/getRandomInt.ts +8 -0
  470. package/src/util/internals/ifNaN.ts +9 -0
  471. package/src/util/internals/index.ts +3 -0
  472. package/src/util/internals/removeFromArray.ts +14 -0
  473. package/src/util/internals/uid.ts +3 -0
  474. package/src/util/lang_string.ts +79 -0
  475. package/src/util/misc/boundingBoxFromPoints.ts +37 -0
  476. package/src/util/misc/capValue.ts +2 -0
  477. package/src/util/misc/cos.ts +24 -0
  478. package/src/util/misc/dom.ts +50 -0
  479. package/src/util/misc/findScaleTo.ts +44 -0
  480. package/src/util/misc/groupSVGElements.ts +15 -0
  481. package/src/util/misc/isTransparent.ts +28 -0
  482. package/src/util/misc/matrix.ts +207 -0
  483. package/src/util/misc/mergeClipPaths.ts +40 -0
  484. package/src/util/misc/objectEnlive.ts +189 -0
  485. package/src/util/misc/objectTransforms.ts +129 -0
  486. package/src/util/misc/pick.ts +29 -0
  487. package/src/util/misc/planeChange.ts +136 -0
  488. package/src/util/misc/projectStroke/StrokeLineCapProjections.ts +112 -0
  489. package/src/util/misc/projectStroke/StrokeLineJoinProjections.ts +226 -0
  490. package/src/util/misc/projectStroke/StrokeProjectionsBase.ts +75 -0
  491. package/src/util/misc/projectStroke/index.ts +53 -0
  492. package/src/util/misc/projectStroke/types.ts +24 -0
  493. package/src/util/misc/radiansDegreesConversion.ts +18 -0
  494. package/src/util/misc/resolveOrigin.ts +22 -0
  495. package/src/util/misc/rotatePoint.ts +15 -0
  496. package/src/util/misc/sin.ts +26 -0
  497. package/src/util/misc/svgParsing.ts +181 -0
  498. package/src/util/misc/textStyles.ts +134 -0
  499. package/src/util/misc/toFixed.ts +8 -0
  500. package/src/util/misc/vectors.ts +82 -0
  501. package/src/util/path/index.ts +1038 -0
  502. package/src/util/path/regex.ts +41 -0
  503. package/src/util/path/typechecks.ts +145 -0
  504. package/src/util/path/typedefs.ts +305 -0
  505. package/src/util/transform_matrix_removal.ts +60 -0
  506. package/src/util/types.ts +78 -0
  507. package/tsconfig.json +106 -0
  508. package/HEADER.js +0 -203
  509. package/build.js +0 -287
  510. package/dist/fabric.js +0 -31187
  511. package/dist/fabric.min.js +0 -1
  512. package/old-travis-reference.yml +0 -97
  513. package/src/brushes/circle_brush.class.js +0 -144
  514. package/src/brushes/pattern_brush.class.js +0 -61
  515. package/src/brushes/pencil_brush.class.js +0 -310
  516. package/src/brushes/spray_brush.class.js +0 -219
  517. package/src/canvas.class.js +0 -1312
  518. package/src/color.class.js +0 -636
  519. package/src/control.class.js +0 -339
  520. package/src/controls.actions.js +0 -740
  521. package/src/controls.render.js +0 -99
  522. package/src/elements_parser.js +0 -152
  523. package/src/filters/2d_backend.class.js +0 -65
  524. package/src/filters/blendcolor_filter.class.js +0 -251
  525. package/src/filters/blendimage_filter.class.js +0 -247
  526. package/src/filters/blur_filter.class.js +0 -217
  527. package/src/filters/brightness_filter.class.js +0 -113
  528. package/src/filters/colormatrix_filter.class.js +0 -159
  529. package/src/filters/composed_filter.class.js +0 -72
  530. package/src/filters/contrast_filter.class.js +0 -113
  531. package/src/filters/convolute_filter.class.js +0 -352
  532. package/src/filters/filter_boilerplate.js +0 -111
  533. package/src/filters/filter_generator.js +0 -85
  534. package/src/filters/gamma_filter.class.js +0 -136
  535. package/src/filters/grayscale_filter.class.js +0 -154
  536. package/src/filters/hue_rotation.class.js +0 -107
  537. package/src/filters/invert_filter.class.js +0 -111
  538. package/src/filters/noise_filter.class.js +0 -134
  539. package/src/filters/pixelate_filter.class.js +0 -137
  540. package/src/filters/removecolor_filter.class.js +0 -173
  541. package/src/filters/resize_filter.class.js +0 -490
  542. package/src/filters/saturate_filter.class.js +0 -119
  543. package/src/filters/vibrance_filter.class.js +0 -122
  544. package/src/filters/webgl_backend.class.js +0 -396
  545. package/src/globalFabric.js +0 -4
  546. package/src/gradient.class.js +0 -490
  547. package/src/intersection.class.js +0 -172
  548. package/src/log.js +0 -11
  549. package/src/mixins/animation.mixin.js +0 -231
  550. package/src/mixins/canvas_dataurl_exporter.mixin.js +0 -97
  551. package/src/mixins/canvas_events.mixin.js +0 -974
  552. package/src/mixins/canvas_gestures.mixin.js +0 -149
  553. package/src/mixins/canvas_grouping.mixin.js +0 -177
  554. package/src/mixins/canvas_serialization.mixin.js +0 -228
  555. package/src/mixins/collection.mixin.js +0 -170
  556. package/src/mixins/default_controls.js +0 -114
  557. package/src/mixins/itext.svg_export.js +0 -241
  558. package/src/mixins/itext_behavior.mixin.js +0 -940
  559. package/src/mixins/itext_click_behavior.mixin.js +0 -278
  560. package/src/mixins/itext_key_behavior.mixin.js +0 -694
  561. package/src/mixins/object.svg_export.js +0 -258
  562. package/src/mixins/object_geometry.mixin.js +0 -683
  563. package/src/mixins/object_interactivity.mixin.js +0 -314
  564. package/src/mixins/object_origin.mixin.js +0 -255
  565. package/src/mixins/object_stacking.mixin.js +0 -80
  566. package/src/mixins/object_straightening.mixin.js +0 -80
  567. package/src/mixins/observable.mixin.js +0 -141
  568. package/src/mixins/shared_methods.mixin.js +0 -94
  569. package/src/mixins/stateful.mixin.js +0 -107
  570. package/src/mixins/text_style.mixin.js +0 -324
  571. package/src/parser.js +0 -1090
  572. package/src/pattern.class.js +0 -189
  573. package/src/point.class.js +0 -337
  574. package/src/shadow.class.js +0 -195
  575. package/src/shapes/active_selection.class.js +0 -155
  576. package/src/shapes/circle.class.js +0 -210
  577. package/src/shapes/ellipse.class.js +0 -181
  578. package/src/shapes/group.class.js +0 -593
  579. package/src/shapes/image.class.js +0 -764
  580. package/src/shapes/itext.class.js +0 -526
  581. package/src/shapes/line.class.js +0 -324
  582. package/src/shapes/object.class.js +0 -2008
  583. package/src/shapes/path.class.js +0 -384
  584. package/src/shapes/polygon.class.js +0 -81
  585. package/src/shapes/polyline.class.js +0 -268
  586. package/src/shapes/rect.class.js +0 -187
  587. package/src/shapes/text.class.js +0 -1696
  588. package/src/shapes/textbox.class.js +0 -461
  589. package/src/shapes/triangle.class.js +0 -93
  590. package/src/static_canvas.class.js +0 -1881
  591. package/src/util/anim_ease.js +0 -398
  592. package/src/util/animate.js +0 -254
  593. package/src/util/animate_color.js +0 -74
  594. package/src/util/dom_event.js +0 -50
  595. package/src/util/dom_misc.js +0 -300
  596. package/src/util/dom_request.js +0 -54
  597. package/src/util/dom_style.js +0 -70
  598. package/src/util/lang_array.js +0 -94
  599. package/src/util/lang_class.js +0 -115
  600. package/src/util/lang_object.js +0 -75
  601. package/src/util/lang_string.js +0 -110
  602. package/src/util/misc.js +0 -1330
  603. package/src/util/named_accessors.mixin.js +0 -428
  604. package/src/util/path.js +0 -829
package/src/util/misc.js DELETED
@@ -1,1330 +0,0 @@
1
- (function(global) {
2
-
3
- var sqrt = Math.sqrt,
4
- atan2 = Math.atan2,
5
- pow = Math.pow,
6
- PiBy180 = Math.PI / 180,
7
- PiBy2 = Math.PI / 2;
8
-
9
- /**
10
- * @namespace fabric.util
11
- */
12
- fabric.util = {
13
-
14
- /**
15
- * Calculate the cos of an angle, avoiding returning floats for known results
16
- * @static
17
- * @memberOf fabric.util
18
- * @param {Number} angle the angle in radians or in degree
19
- * @return {Number}
20
- */
21
- cos: function(angle) {
22
- if (angle === 0) { return 1; }
23
- if (angle < 0) {
24
- // cos(a) = cos(-a)
25
- angle = -angle;
26
- }
27
- var angleSlice = angle / PiBy2;
28
- switch (angleSlice) {
29
- case 1: case 3: return 0;
30
- case 2: return -1;
31
- }
32
- return Math.cos(angle);
33
- },
34
-
35
- /**
36
- * Calculate the sin of an angle, avoiding returning floats for known results
37
- * @static
38
- * @memberOf fabric.util
39
- * @param {Number} angle the angle in radians or in degree
40
- * @return {Number}
41
- */
42
- sin: function(angle) {
43
- if (angle === 0) { return 0; }
44
- var angleSlice = angle / PiBy2, sign = 1;
45
- if (angle < 0) {
46
- // sin(-a) = -sin(a)
47
- sign = -1;
48
- }
49
- switch (angleSlice) {
50
- case 1: return sign;
51
- case 2: return 0;
52
- case 3: return -sign;
53
- }
54
- return Math.sin(angle);
55
- },
56
-
57
- /**
58
- * Removes value from an array.
59
- * Presence of value (and its position in an array) is determined via `Array.prototype.indexOf`
60
- * @static
61
- * @memberOf fabric.util
62
- * @param {Array} array
63
- * @param {*} value
64
- * @return {Array} original array
65
- */
66
- removeFromArray: function(array, value) {
67
- var idx = array.indexOf(value);
68
- if (idx !== -1) {
69
- array.splice(idx, 1);
70
- }
71
- return array;
72
- },
73
-
74
- /**
75
- * Returns random number between 2 specified ones.
76
- * @static
77
- * @memberOf fabric.util
78
- * @param {Number} min lower limit
79
- * @param {Number} max upper limit
80
- * @return {Number} random value (between min and max)
81
- */
82
- getRandomInt: function(min, max) {
83
- return Math.floor(Math.random() * (max - min + 1)) + min;
84
- },
85
-
86
- /**
87
- * Transforms degrees to radians.
88
- * @static
89
- * @memberOf fabric.util
90
- * @param {Number} degrees value in degrees
91
- * @return {Number} value in radians
92
- */
93
- degreesToRadians: function(degrees) {
94
- return degrees * PiBy180;
95
- },
96
-
97
- /**
98
- * Transforms radians to degrees.
99
- * @static
100
- * @memberOf fabric.util
101
- * @param {Number} radians value in radians
102
- * @return {Number} value in degrees
103
- */
104
- radiansToDegrees: function(radians) {
105
- return radians / PiBy180;
106
- },
107
-
108
- /**
109
- * Rotates `point` around `origin` with `radians`
110
- * @static
111
- * @memberOf fabric.util
112
- * @param {fabric.Point} point The point to rotate
113
- * @param {fabric.Point} origin The origin of the rotation
114
- * @param {Number} radians The radians of the angle for the rotation
115
- * @return {fabric.Point} The new rotated point
116
- */
117
- rotatePoint: function(point, origin, radians) {
118
- var newPoint = new fabric.Point(point.x - origin.x, point.y - origin.y),
119
- v = fabric.util.rotateVector(newPoint, radians);
120
- return new fabric.Point(v.x, v.y).addEquals(origin);
121
- },
122
-
123
- /**
124
- * Rotates `vector` with `radians`
125
- * @static
126
- * @memberOf fabric.util
127
- * @param {Object} vector The vector to rotate (x and y)
128
- * @param {Number} radians The radians of the angle for the rotation
129
- * @return {Object} The new rotated point
130
- */
131
- rotateVector: function(vector, radians) {
132
- var sin = fabric.util.sin(radians),
133
- cos = fabric.util.cos(radians),
134
- rx = vector.x * cos - vector.y * sin,
135
- ry = vector.x * sin + vector.y * cos;
136
- return {
137
- x: rx,
138
- y: ry
139
- };
140
- },
141
-
142
- /**
143
- * Creates a vetor from points represented as a point
144
- * @static
145
- * @memberOf fabric.util
146
- *
147
- * @typedef {Object} Point
148
- * @property {number} x
149
- * @property {number} y
150
- *
151
- * @param {Point} from
152
- * @param {Point} to
153
- * @returns {Point} vector
154
- */
155
- createVector: function (from, to) {
156
- return new fabric.Point(to.x - from.x, to.y - from.y);
157
- },
158
-
159
- /**
160
- * Calculates angle between 2 vectors using dot product
161
- * @static
162
- * @memberOf fabric.util
163
- * @param {Point} a
164
- * @param {Point} b
165
- * @returns the angle in radian between the vectors
166
- */
167
- calcAngleBetweenVectors: function (a, b) {
168
- return Math.acos((a.x * b.x + a.y * b.y) / (Math.hypot(a.x, a.y) * Math.hypot(b.x, b.y)));
169
- },
170
-
171
- /**
172
- * @static
173
- * @memberOf fabric.util
174
- * @param {Point} v
175
- * @returns {Point} vector representing the unit vector of pointing to the direction of `v`
176
- */
177
- getHatVector: function (v) {
178
- return new fabric.Point(v.x, v.y).multiply(1 / Math.hypot(v.x, v.y));
179
- },
180
-
181
- /**
182
- * @static
183
- * @memberOf fabric.util
184
- * @param {Point} A
185
- * @param {Point} B
186
- * @param {Point} C
187
- * @returns {{ vector: Point, angle: number }} vector representing the bisector of A and A's angle
188
- */
189
- getBisector: function (A, B, C) {
190
- var AB = fabric.util.createVector(A, B), AC = fabric.util.createVector(A, C);
191
- var alpha = fabric.util.calcAngleBetweenVectors(AB, AC);
192
- // check if alpha is relative to AB->BC
193
- var ro = fabric.util.calcAngleBetweenVectors(fabric.util.rotateVector(AB, alpha), AC);
194
- var phi = alpha * (ro === 0 ? 1 : -1) / 2;
195
- return {
196
- vector: fabric.util.getHatVector(fabric.util.rotateVector(AB, phi)),
197
- angle: alpha
198
- };
199
- },
200
-
201
- /**
202
- * Project stroke width on points returning 2 projections for each point as follows:
203
- * - `miter`: 2 points corresponding to the outer boundary and the inner boundary of stroke.
204
- * - `bevel`: 2 points corresponding to the bevel boundaries, tangent to the bisector.
205
- * - `round`: same as `bevel`
206
- * Used to calculate object's bounding box
207
- * @static
208
- * @memberOf fabric.util
209
- * @param {Point[]} points
210
- * @param {Object} options
211
- * @param {number} options.strokeWidth
212
- * @param {'miter'|'bevel'|'round'} options.strokeLineJoin
213
- * @param {number} options.strokeMiterLimit https://developer.mozilla.org/en-US/docs/Web/SVG/Attribute/stroke-miterlimit
214
- * @param {boolean} options.strokeUniform
215
- * @param {number} options.scaleX
216
- * @param {number} options.scaleY
217
- * @param {boolean} [openPath] whether the shape is open or not, affects the calculations of the first and last points
218
- * @returns {fabric.Point[]} array of size 2n/4n of all suspected points
219
- */
220
- projectStrokeOnPoints: function (points, options, openPath) {
221
- var coords = [], s = options.strokeWidth / 2,
222
- strokeUniformScalar = options.strokeUniform ?
223
- new fabric.Point(1 / options.scaleX, 1 / options.scaleY) : new fabric.Point(1, 1),
224
- getStrokeHatVector = function (v) {
225
- var scalar = s / (Math.hypot(v.x, v.y));
226
- return new fabric.Point(v.x * scalar * strokeUniformScalar.x, v.y * scalar * strokeUniformScalar.y);
227
- };
228
- if (points.length <= 1) {return coords;}
229
- points.forEach(function (p, index) {
230
- var A = new fabric.Point(p.x, p.y), B, C;
231
- if (index === 0) {
232
- C = points[index + 1];
233
- B = openPath ? getStrokeHatVector(fabric.util.createVector(C, A)).addEquals(A) : points[points.length - 1];
234
- }
235
- else if (index === points.length - 1) {
236
- B = points[index - 1];
237
- C = openPath ? getStrokeHatVector(fabric.util.createVector(B, A)).addEquals(A) : points[0];
238
- }
239
- else {
240
- B = points[index - 1];
241
- C = points[index + 1];
242
- }
243
- var bisector = fabric.util.getBisector(A, B, C),
244
- bisectorVector = bisector.vector,
245
- alpha = bisector.angle,
246
- scalar,
247
- miterVector;
248
- if (options.strokeLineJoin === 'miter') {
249
- scalar = -s / Math.sin(alpha / 2);
250
- miterVector = new fabric.Point(
251
- bisectorVector.x * scalar * strokeUniformScalar.x,
252
- bisectorVector.y * scalar * strokeUniformScalar.y
253
- );
254
- if (Math.hypot(miterVector.x, miterVector.y) / s <= options.strokeMiterLimit) {
255
- coords.push(A.add(miterVector));
256
- coords.push(A.subtract(miterVector));
257
- return;
258
- }
259
- }
260
- scalar = -s * Math.SQRT2;
261
- miterVector = new fabric.Point(
262
- bisectorVector.x * scalar * strokeUniformScalar.x,
263
- bisectorVector.y * scalar * strokeUniformScalar.y
264
- );
265
- coords.push(A.add(miterVector));
266
- coords.push(A.subtract(miterVector));
267
- });
268
- return coords;
269
- },
270
-
271
- /**
272
- * Apply transform t to point p
273
- * @static
274
- * @memberOf fabric.util
275
- * @param {fabric.Point} p The point to transform
276
- * @param {Array} t The transform
277
- * @param {Boolean} [ignoreOffset] Indicates that the offset should not be applied
278
- * @return {fabric.Point} The transformed point
279
- */
280
- transformPoint: function(p, t, ignoreOffset) {
281
- if (ignoreOffset) {
282
- return new fabric.Point(
283
- t[0] * p.x + t[2] * p.y,
284
- t[1] * p.x + t[3] * p.y
285
- );
286
- }
287
- return new fabric.Point(
288
- t[0] * p.x + t[2] * p.y + t[4],
289
- t[1] * p.x + t[3] * p.y + t[5]
290
- );
291
- },
292
-
293
- /**
294
- * Returns coordinates of points's bounding rectangle (left, top, width, height)
295
- * @param {Array} points 4 points array
296
- * @param {Array} [transform] an array of 6 numbers representing a 2x3 transform matrix
297
- * @return {Object} Object with left, top, width, height properties
298
- */
299
- makeBoundingBoxFromPoints: function(points, transform) {
300
- if (transform) {
301
- for (var i = 0; i < points.length; i++) {
302
- points[i] = fabric.util.transformPoint(points[i], transform);
303
- }
304
- }
305
- var xPoints = [points[0].x, points[1].x, points[2].x, points[3].x],
306
- minX = fabric.util.array.min(xPoints),
307
- maxX = fabric.util.array.max(xPoints),
308
- width = maxX - minX,
309
- yPoints = [points[0].y, points[1].y, points[2].y, points[3].y],
310
- minY = fabric.util.array.min(yPoints),
311
- maxY = fabric.util.array.max(yPoints),
312
- height = maxY - minY;
313
-
314
- return {
315
- left: minX,
316
- top: minY,
317
- width: width,
318
- height: height
319
- };
320
- },
321
-
322
- /**
323
- * Invert transformation t
324
- * @static
325
- * @memberOf fabric.util
326
- * @param {Array} t The transform
327
- * @return {Array} The inverted transform
328
- */
329
- invertTransform: function(t) {
330
- var a = 1 / (t[0] * t[3] - t[1] * t[2]),
331
- r = [a * t[3], -a * t[1], -a * t[2], a * t[0]],
332
- o = fabric.util.transformPoint({ x: t[4], y: t[5] }, r, true);
333
- r[4] = -o.x;
334
- r[5] = -o.y;
335
- return r;
336
- },
337
-
338
- /**
339
- * A wrapper around Number#toFixed, which contrary to native method returns number, not string.
340
- * @static
341
- * @memberOf fabric.util
342
- * @param {Number|String} number number to operate on
343
- * @param {Number} fractionDigits number of fraction digits to "leave"
344
- * @return {Number}
345
- */
346
- toFixed: function(number, fractionDigits) {
347
- return parseFloat(Number(number).toFixed(fractionDigits));
348
- },
349
-
350
- /**
351
- * Converts from attribute value to pixel value if applicable.
352
- * Returns converted pixels or original value not converted.
353
- * @param {Number|String} value number to operate on
354
- * @param {Number} fontSize
355
- * @return {Number|String}
356
- */
357
- parseUnit: function(value, fontSize) {
358
- var unit = /\D{0,2}$/.exec(value),
359
- number = parseFloat(value);
360
- if (!fontSize) {
361
- fontSize = fabric.Text.DEFAULT_SVG_FONT_SIZE;
362
- }
363
- switch (unit[0]) {
364
- case 'mm':
365
- return number * fabric.DPI / 25.4;
366
-
367
- case 'cm':
368
- return number * fabric.DPI / 2.54;
369
-
370
- case 'in':
371
- return number * fabric.DPI;
372
-
373
- case 'pt':
374
- return number * fabric.DPI / 72; // or * 4 / 3
375
-
376
- case 'pc':
377
- return number * fabric.DPI / 72 * 12; // or * 16
378
-
379
- case 'em':
380
- return number * fontSize;
381
-
382
- default:
383
- return number;
384
- }
385
- },
386
-
387
- /**
388
- * Function which always returns `false`.
389
- * @static
390
- * @memberOf fabric.util
391
- * @return {Boolean}
392
- */
393
- falseFunction: function() {
394
- return false;
395
- },
396
-
397
- /**
398
- * Returns klass "Class" object of given namespace
399
- * @memberOf fabric.util
400
- * @param {String} type Type of object (eg. 'circle')
401
- * @param {String} namespace Namespace to get klass "Class" object from
402
- * @return {Object} klass "Class"
403
- */
404
- getKlass: function(type, namespace) {
405
- // capitalize first letter only
406
- type = fabric.util.string.camelize(type.charAt(0).toUpperCase() + type.slice(1));
407
- return fabric.util.resolveNamespace(namespace)[type];
408
- },
409
-
410
- /**
411
- * Returns array of attributes for given svg that fabric parses
412
- * @memberOf fabric.util
413
- * @param {String} type Type of svg element (eg. 'circle')
414
- * @return {Array} string names of supported attributes
415
- */
416
- getSvgAttributes: function(type) {
417
- var attributes = [
418
- 'instantiated_by_use',
419
- 'style',
420
- 'id',
421
- 'class'
422
- ];
423
- switch (type) {
424
- case 'linearGradient':
425
- attributes = attributes.concat(['x1', 'y1', 'x2', 'y2', 'gradientUnits', 'gradientTransform']);
426
- break;
427
- case 'radialGradient':
428
- attributes = attributes.concat(['gradientUnits', 'gradientTransform', 'cx', 'cy', 'r', 'fx', 'fy', 'fr']);
429
- break;
430
- case 'stop':
431
- attributes = attributes.concat(['offset', 'stop-color', 'stop-opacity']);
432
- break;
433
- }
434
- return attributes;
435
- },
436
-
437
- /**
438
- * Returns object of given namespace
439
- * @memberOf fabric.util
440
- * @param {String} namespace Namespace string e.g. 'fabric.Image.filter' or 'fabric'
441
- * @return {Object} Object for given namespace (default fabric)
442
- */
443
- resolveNamespace: function(namespace) {
444
- if (!namespace) {
445
- return fabric;
446
- }
447
-
448
- var parts = namespace.split('.'),
449
- len = parts.length, i,
450
- obj = global || fabric.window;
451
-
452
- for (i = 0; i < len; ++i) {
453
- obj = obj[parts[i]];
454
- }
455
-
456
- return obj;
457
- },
458
-
459
- /**
460
- * Loads image element from given url and passes it to a callback
461
- * @memberOf fabric.util
462
- * @param {String} url URL representing an image
463
- * @param {Function} callback Callback; invoked with loaded image
464
- * @param {*} [context] Context to invoke callback in
465
- * @param {Object} [crossOrigin] crossOrigin value to set image element to
466
- */
467
- loadImage: function(url, callback, context, crossOrigin) {
468
- if (!url) {
469
- callback && callback.call(context, url);
470
- return;
471
- }
472
-
473
- var img = fabric.util.createImage();
474
-
475
- /** @ignore */
476
- var onLoadCallback = function () {
477
- callback && callback.call(context, img, false);
478
- img = img.onload = img.onerror = null;
479
- };
480
-
481
- img.onload = onLoadCallback;
482
- /** @ignore */
483
- img.onerror = function() {
484
- fabric.log('Error loading ' + img.src);
485
- callback && callback.call(context, null, true);
486
- img = img.onload = img.onerror = null;
487
- };
488
-
489
- // data-urls appear to be buggy with crossOrigin
490
- // https://github.com/kangax/fabric.js/commit/d0abb90f1cd5c5ef9d2a94d3fb21a22330da3e0a#commitcomment-4513767
491
- // see https://code.google.com/p/chromium/issues/detail?id=315152
492
- // https://bugzilla.mozilla.org/show_bug.cgi?id=935069
493
- // crossOrigin null is the same as not set.
494
- if (url.indexOf('data') !== 0 &&
495
- crossOrigin !== undefined &&
496
- crossOrigin !== null) {
497
- img.crossOrigin = crossOrigin;
498
- }
499
-
500
- // IE10 / IE11-Fix: SVG contents from data: URI
501
- // will only be available if the IMG is present
502
- // in the DOM (and visible)
503
- if (url.substring(0,14) === 'data:image/svg') {
504
- img.onload = null;
505
- fabric.util.loadImageInDom(img, onLoadCallback);
506
- }
507
-
508
- img.src = url;
509
- },
510
-
511
- /**
512
- * Attaches SVG image with data: URL to the dom
513
- * @memberOf fabric.util
514
- * @param {Object} img Image object with data:image/svg src
515
- * @param {Function} callback Callback; invoked with loaded image
516
- * @return {Object} DOM element (div containing the SVG image)
517
- */
518
- loadImageInDom: function(img, onLoadCallback) {
519
- var div = fabric.document.createElement('div');
520
- div.style.width = div.style.height = '1px';
521
- div.style.left = div.style.top = '-100%';
522
- div.style.position = 'absolute';
523
- div.appendChild(img);
524
- fabric.document.querySelector('body').appendChild(div);
525
- /**
526
- * Wrap in function to:
527
- * 1. Call existing callback
528
- * 2. Cleanup DOM
529
- */
530
- img.onload = function () {
531
- onLoadCallback();
532
- div.parentNode.removeChild(div);
533
- div = null;
534
- };
535
- },
536
-
537
- /**
538
- * Creates corresponding fabric instances from their object representations
539
- * @static
540
- * @memberOf fabric.util
541
- * @param {Array} objects Objects to enliven
542
- * @param {Function} callback Callback to invoke when all objects are created
543
- * @param {String} namespace Namespace to get klass "Class" object from
544
- * @param {Function} reviver Method for further parsing of object elements,
545
- * called after each fabric object created.
546
- */
547
- enlivenObjects: function(objects, callback, namespace, reviver) {
548
- objects = objects || [];
549
-
550
- var enlivenedObjects = [],
551
- numLoadedObjects = 0,
552
- numTotalObjects = objects.length;
553
-
554
- function onLoaded() {
555
- if (++numLoadedObjects === numTotalObjects) {
556
- callback && callback(enlivenedObjects.filter(function(obj) {
557
- // filter out undefined objects (objects that gave error)
558
- return obj;
559
- }));
560
- }
561
- }
562
-
563
- if (!numTotalObjects) {
564
- callback && callback(enlivenedObjects);
565
- return;
566
- }
567
-
568
- objects.forEach(function (o, index) {
569
- // if sparse array
570
- if (!o || !o.type) {
571
- onLoaded();
572
- return;
573
- }
574
- var klass = fabric.util.getKlass(o.type, namespace);
575
- klass.fromObject(o, function (obj, error) {
576
- error || (enlivenedObjects[index] = obj);
577
- reviver && reviver(o, obj, error);
578
- onLoaded();
579
- });
580
- });
581
- },
582
-
583
- /**
584
- * Creates corresponding fabric instances residing in an object, e.g. `clipPath`
585
- * @see {@link fabric.Object.ENLIVEN_PROPS}
586
- * @param {Object} object
587
- * @param {Object} [context] assign enlived props to this object (pass null to skip this)
588
- * @param {(objects:fabric.Object[]) => void} callback
589
- */
590
- enlivenObjectEnlivables: function (object, context, callback) {
591
- var enlivenProps = fabric.Object.ENLIVEN_PROPS.filter(function (key) { return !!object[key]; });
592
- fabric.util.enlivenObjects(enlivenProps.map(function (key) { return object[key]; }), function (enlivedProps) {
593
- var objects = {};
594
- enlivenProps.forEach(function (key, index) {
595
- objects[key] = enlivedProps[index];
596
- context && (context[key] = enlivedProps[index]);
597
- });
598
- callback && callback(objects);
599
- });
600
- },
601
-
602
- /**
603
- * Create and wait for loading of patterns
604
- * @static
605
- * @memberOf fabric.util
606
- * @param {Array} patterns Objects to enliven
607
- * @param {Function} callback Callback to invoke when all objects are created
608
- * called after each fabric object created.
609
- */
610
- enlivenPatterns: function(patterns, callback) {
611
- patterns = patterns || [];
612
-
613
- function onLoaded() {
614
- if (++numLoadedPatterns === numPatterns) {
615
- callback && callback(enlivenedPatterns);
616
- }
617
- }
618
-
619
- var enlivenedPatterns = [],
620
- numLoadedPatterns = 0,
621
- numPatterns = patterns.length;
622
-
623
- if (!numPatterns) {
624
- callback && callback(enlivenedPatterns);
625
- return;
626
- }
627
-
628
- patterns.forEach(function (p, index) {
629
- if (p && p.source) {
630
- new fabric.Pattern(p, function(pattern) {
631
- enlivenedPatterns[index] = pattern;
632
- onLoaded();
633
- });
634
- }
635
- else {
636
- enlivenedPatterns[index] = p;
637
- onLoaded();
638
- }
639
- });
640
- },
641
-
642
- /**
643
- * Groups SVG elements (usually those retrieved from SVG document)
644
- * @static
645
- * @memberOf fabric.util
646
- * @param {Array} elements SVG elements to group
647
- * @param {Object} [options] Options object
648
- * @param {String} path Value to set sourcePath to
649
- * @return {fabric.Object|fabric.Group}
650
- */
651
- groupSVGElements: function(elements, options, path) {
652
- var object;
653
- if (elements && elements.length === 1) {
654
- if (typeof path !== 'undefined') {
655
- elements[0].sourcePath = path;
656
- }
657
- return elements[0];
658
- }
659
- if (options) {
660
- if (options.width && options.height) {
661
- options.centerPoint = {
662
- x: options.width / 2,
663
- y: options.height / 2
664
- };
665
- }
666
- else {
667
- delete options.width;
668
- delete options.height;
669
- }
670
- }
671
- object = new fabric.Group(elements, options);
672
- if (typeof path !== 'undefined') {
673
- object.sourcePath = path;
674
- }
675
- return object;
676
- },
677
-
678
- /**
679
- * Populates an object with properties of another object
680
- * @static
681
- * @memberOf fabric.util
682
- * @param {Object} source Source object
683
- * @param {Object} destination Destination object
684
- * @return {Array} properties Properties names to include
685
- */
686
- populateWithProperties: function(source, destination, properties) {
687
- if (properties && Array.isArray(properties)) {
688
- for (var i = 0, len = properties.length; i < len; i++) {
689
- if (properties[i] in source) {
690
- destination[properties[i]] = source[properties[i]];
691
- }
692
- }
693
- }
694
- },
695
-
696
- /**
697
- * Creates canvas element
698
- * @static
699
- * @memberOf fabric.util
700
- * @return {CanvasElement} initialized canvas element
701
- */
702
- createCanvasElement: function() {
703
- return fabric.document.createElement('canvas');
704
- },
705
-
706
- /**
707
- * Creates a canvas element that is a copy of another and is also painted
708
- * @param {CanvasElement} canvas to copy size and content of
709
- * @static
710
- * @memberOf fabric.util
711
- * @return {CanvasElement} initialized canvas element
712
- */
713
- copyCanvasElement: function(canvas) {
714
- var newCanvas = fabric.util.createCanvasElement();
715
- newCanvas.width = canvas.width;
716
- newCanvas.height = canvas.height;
717
- newCanvas.getContext('2d').drawImage(canvas, 0, 0);
718
- return newCanvas;
719
- },
720
-
721
- /**
722
- * since 2.6.0 moved from canvas instance to utility.
723
- * @param {CanvasElement} canvasEl to copy size and content of
724
- * @param {String} format 'jpeg' or 'png', in some browsers 'webp' is ok too
725
- * @param {Number} quality <= 1 and > 0
726
- * @static
727
- * @memberOf fabric.util
728
- * @return {String} data url
729
- */
730
- toDataURL: function(canvasEl, format, quality) {
731
- return canvasEl.toDataURL('image/' + format, quality);
732
- },
733
-
734
- /**
735
- * Creates image element (works on client and node)
736
- * @static
737
- * @memberOf fabric.util
738
- * @return {HTMLImageElement} HTML image element
739
- */
740
- createImage: function() {
741
- return fabric.document.createElement('img');
742
- },
743
-
744
- /**
745
- * Multiply matrix A by matrix B to nest transformations
746
- * @static
747
- * @memberOf fabric.util
748
- * @param {Array} a First transformMatrix
749
- * @param {Array} b Second transformMatrix
750
- * @param {Boolean} is2x2 flag to multiply matrices as 2x2 matrices
751
- * @return {Array} The product of the two transform matrices
752
- */
753
- multiplyTransformMatrices: function(a, b, is2x2) {
754
- // Matrix multiply a * b
755
- return [
756
- a[0] * b[0] + a[2] * b[1],
757
- a[1] * b[0] + a[3] * b[1],
758
- a[0] * b[2] + a[2] * b[3],
759
- a[1] * b[2] + a[3] * b[3],
760
- is2x2 ? 0 : a[0] * b[4] + a[2] * b[5] + a[4],
761
- is2x2 ? 0 : a[1] * b[4] + a[3] * b[5] + a[5]
762
- ];
763
- },
764
-
765
- /**
766
- * Decomposes standard 2x3 matrix into transform components
767
- * @static
768
- * @memberOf fabric.util
769
- * @param {Array} a transformMatrix
770
- * @return {Object} Components of transform
771
- */
772
- qrDecompose: function(a) {
773
- var angle = atan2(a[1], a[0]),
774
- denom = pow(a[0], 2) + pow(a[1], 2),
775
- scaleX = sqrt(denom),
776
- scaleY = (a[0] * a[3] - a[2] * a[1]) / scaleX,
777
- skewX = atan2(a[0] * a[2] + a[1] * a [3], denom);
778
- return {
779
- angle: angle / PiBy180,
780
- scaleX: scaleX,
781
- scaleY: scaleY,
782
- skewX: skewX / PiBy180,
783
- skewY: 0,
784
- translateX: a[4],
785
- translateY: a[5]
786
- };
787
- },
788
-
789
- /**
790
- * Returns a transform matrix starting from an object of the same kind of
791
- * the one returned from qrDecompose, useful also if you want to calculate some
792
- * transformations from an object that is not enlived yet
793
- * @static
794
- * @memberOf fabric.util
795
- * @param {Object} options
796
- * @param {Number} [options.angle] angle in degrees
797
- * @return {Number[]} transform matrix
798
- */
799
- calcRotateMatrix: function(options) {
800
- if (!options.angle) {
801
- return fabric.iMatrix.concat();
802
- }
803
- var theta = fabric.util.degreesToRadians(options.angle),
804
- cos = fabric.util.cos(theta),
805
- sin = fabric.util.sin(theta);
806
- return [cos, sin, -sin, cos, 0, 0];
807
- },
808
-
809
- /**
810
- * Returns a transform matrix starting from an object of the same kind of
811
- * the one returned from qrDecompose, useful also if you want to calculate some
812
- * transformations from an object that is not enlived yet.
813
- * is called DimensionsTransformMatrix because those properties are the one that influence
814
- * the size of the resulting box of the object.
815
- * @static
816
- * @memberOf fabric.util
817
- * @param {Object} options
818
- * @param {Number} [options.scaleX]
819
- * @param {Number} [options.scaleY]
820
- * @param {Boolean} [options.flipX]
821
- * @param {Boolean} [options.flipY]
822
- * @param {Number} [options.skewX]
823
- * @param {Number} [options.skewY]
824
- * @return {Number[]} transform matrix
825
- */
826
- calcDimensionsMatrix: function(options) {
827
- var scaleX = typeof options.scaleX === 'undefined' ? 1 : options.scaleX,
828
- scaleY = typeof options.scaleY === 'undefined' ? 1 : options.scaleY,
829
- scaleMatrix = [
830
- options.flipX ? -scaleX : scaleX,
831
- 0,
832
- 0,
833
- options.flipY ? -scaleY : scaleY,
834
- 0,
835
- 0],
836
- multiply = fabric.util.multiplyTransformMatrices,
837
- degreesToRadians = fabric.util.degreesToRadians;
838
- if (options.skewX) {
839
- scaleMatrix = multiply(
840
- scaleMatrix,
841
- [1, 0, Math.tan(degreesToRadians(options.skewX)), 1],
842
- true);
843
- }
844
- if (options.skewY) {
845
- scaleMatrix = multiply(
846
- scaleMatrix,
847
- [1, Math.tan(degreesToRadians(options.skewY)), 0, 1],
848
- true);
849
- }
850
- return scaleMatrix;
851
- },
852
-
853
- /**
854
- * Returns a transform matrix starting from an object of the same kind of
855
- * the one returned from qrDecompose, useful also if you want to calculate some
856
- * transformations from an object that is not enlived yet
857
- * @static
858
- * @memberOf fabric.util
859
- * @param {Object} options
860
- * @param {Number} [options.angle]
861
- * @param {Number} [options.scaleX]
862
- * @param {Number} [options.scaleY]
863
- * @param {Boolean} [options.flipX]
864
- * @param {Boolean} [options.flipY]
865
- * @param {Number} [options.skewX]
866
- * @param {Number} [options.skewX]
867
- * @param {Number} [options.translateX]
868
- * @param {Number} [options.translateY]
869
- * @return {Number[]} transform matrix
870
- */
871
- composeMatrix: function(options) {
872
- var matrix = [1, 0, 0, 1, options.translateX || 0, options.translateY || 0],
873
- multiply = fabric.util.multiplyTransformMatrices;
874
- if (options.angle) {
875
- matrix = multiply(matrix, fabric.util.calcRotateMatrix(options));
876
- }
877
- if (options.scaleX !== 1 || options.scaleY !== 1 ||
878
- options.skewX || options.skewY || options.flipX || options.flipY) {
879
- matrix = multiply(matrix, fabric.util.calcDimensionsMatrix(options));
880
- }
881
- return matrix;
882
- },
883
-
884
- /**
885
- * reset an object transform state to neutral. Top and left are not accounted for
886
- * @static
887
- * @memberOf fabric.util
888
- * @param {fabric.Object} target object to transform
889
- */
890
- resetObjectTransform: function (target) {
891
- target.scaleX = 1;
892
- target.scaleY = 1;
893
- target.skewX = 0;
894
- target.skewY = 0;
895
- target.flipX = false;
896
- target.flipY = false;
897
- target.rotate(0);
898
- },
899
-
900
- /**
901
- * Extract Object transform values
902
- * @static
903
- * @memberOf fabric.util
904
- * @param {fabric.Object} target object to read from
905
- * @return {Object} Components of transform
906
- */
907
- saveObjectTransform: function (target) {
908
- return {
909
- scaleX: target.scaleX,
910
- scaleY: target.scaleY,
911
- skewX: target.skewX,
912
- skewY: target.skewY,
913
- angle: target.angle,
914
- left: target.left,
915
- flipX: target.flipX,
916
- flipY: target.flipY,
917
- top: target.top
918
- };
919
- },
920
-
921
- /**
922
- * Returns true if context has transparent pixel
923
- * at specified location (taking tolerance into account)
924
- * @param {CanvasRenderingContext2D} ctx context
925
- * @param {Number} x x coordinate
926
- * @param {Number} y y coordinate
927
- * @param {Number} tolerance Tolerance
928
- */
929
- isTransparent: function(ctx, x, y, tolerance) {
930
-
931
- // If tolerance is > 0 adjust start coords to take into account.
932
- // If moves off Canvas fix to 0
933
- if (tolerance > 0) {
934
- if (x > tolerance) {
935
- x -= tolerance;
936
- }
937
- else {
938
- x = 0;
939
- }
940
- if (y > tolerance) {
941
- y -= tolerance;
942
- }
943
- else {
944
- y = 0;
945
- }
946
- }
947
-
948
- var _isTransparent = true, i, temp,
949
- imageData = ctx.getImageData(x, y, (tolerance * 2) || 1, (tolerance * 2) || 1),
950
- l = imageData.data.length;
951
-
952
- // Split image data - for tolerance > 1, pixelDataSize = 4;
953
- for (i = 3; i < l; i += 4) {
954
- temp = imageData.data[i];
955
- _isTransparent = temp <= 0;
956
- if (_isTransparent === false) {
957
- break; // Stop if colour found
958
- }
959
- }
960
-
961
- imageData = null;
962
-
963
- return _isTransparent;
964
- },
965
-
966
- /**
967
- * Parse preserveAspectRatio attribute from element
968
- * @param {string} attribute to be parsed
969
- * @return {Object} an object containing align and meetOrSlice attribute
970
- */
971
- parsePreserveAspectRatioAttribute: function(attribute) {
972
- var meetOrSlice = 'meet', alignX = 'Mid', alignY = 'Mid',
973
- aspectRatioAttrs = attribute.split(' '), align;
974
-
975
- if (aspectRatioAttrs && aspectRatioAttrs.length) {
976
- meetOrSlice = aspectRatioAttrs.pop();
977
- if (meetOrSlice !== 'meet' && meetOrSlice !== 'slice') {
978
- align = meetOrSlice;
979
- meetOrSlice = 'meet';
980
- }
981
- else if (aspectRatioAttrs.length) {
982
- align = aspectRatioAttrs.pop();
983
- }
984
- }
985
- //divide align in alignX and alignY
986
- alignX = align !== 'none' ? align.slice(1, 4) : 'none';
987
- alignY = align !== 'none' ? align.slice(5, 8) : 'none';
988
- return {
989
- meetOrSlice: meetOrSlice,
990
- alignX: alignX,
991
- alignY: alignY
992
- };
993
- },
994
-
995
- /**
996
- * Clear char widths cache for the given font family or all the cache if no
997
- * fontFamily is specified.
998
- * Use it if you know you are loading fonts in a lazy way and you are not waiting
999
- * for custom fonts to load properly when adding text objects to the canvas.
1000
- * If a text object is added when its own font is not loaded yet, you will get wrong
1001
- * measurement and so wrong bounding boxes.
1002
- * After the font cache is cleared, either change the textObject text content or call
1003
- * initDimensions() to trigger a recalculation
1004
- * @memberOf fabric.util
1005
- * @param {String} [fontFamily] font family to clear
1006
- */
1007
- clearFabricFontCache: function(fontFamily) {
1008
- fontFamily = (fontFamily || '').toLowerCase();
1009
- if (!fontFamily) {
1010
- fabric.charWidthsCache = { };
1011
- }
1012
- else if (fabric.charWidthsCache[fontFamily]) {
1013
- delete fabric.charWidthsCache[fontFamily];
1014
- }
1015
- },
1016
-
1017
- /**
1018
- * Given current aspect ratio, determines the max width and height that can
1019
- * respect the total allowed area for the cache.
1020
- * @memberOf fabric.util
1021
- * @param {Number} ar aspect ratio
1022
- * @param {Number} maximumArea Maximum area you want to achieve
1023
- * @return {Object.x} Limited dimensions by X
1024
- * @return {Object.y} Limited dimensions by Y
1025
- */
1026
- limitDimsByArea: function(ar, maximumArea) {
1027
- var roughWidth = Math.sqrt(maximumArea * ar),
1028
- perfLimitSizeY = Math.floor(maximumArea / roughWidth);
1029
- return { x: Math.floor(roughWidth), y: perfLimitSizeY };
1030
- },
1031
-
1032
- capValue: function(min, value, max) {
1033
- return Math.max(min, Math.min(value, max));
1034
- },
1035
-
1036
- /**
1037
- * Finds the scale for the object source to fit inside the object destination,
1038
- * keeping aspect ratio intact.
1039
- * respect the total allowed area for the cache.
1040
- * @memberOf fabric.util
1041
- * @param {Object | fabric.Object} source
1042
- * @param {Number} source.height natural unscaled height of the object
1043
- * @param {Number} source.width natural unscaled width of the object
1044
- * @param {Object | fabric.Object} destination
1045
- * @param {Number} destination.height natural unscaled height of the object
1046
- * @param {Number} destination.width natural unscaled width of the object
1047
- * @return {Number} scale factor to apply to source to fit into destination
1048
- */
1049
- findScaleToFit: function(source, destination) {
1050
- return Math.min(destination.width / source.width, destination.height / source.height);
1051
- },
1052
-
1053
- /**
1054
- * Finds the scale for the object source to cover entirely the object destination,
1055
- * keeping aspect ratio intact.
1056
- * respect the total allowed area for the cache.
1057
- * @memberOf fabric.util
1058
- * @param {Object | fabric.Object} source
1059
- * @param {Number} source.height natural unscaled height of the object
1060
- * @param {Number} source.width natural unscaled width of the object
1061
- * @param {Object | fabric.Object} destination
1062
- * @param {Number} destination.height natural unscaled height of the object
1063
- * @param {Number} destination.width natural unscaled width of the object
1064
- * @return {Number} scale factor to apply to source to cover destination
1065
- */
1066
- findScaleToCover: function(source, destination) {
1067
- return Math.max(destination.width / source.width, destination.height / source.height);
1068
- },
1069
-
1070
- /**
1071
- * given an array of 6 number returns something like `"matrix(...numbers)"`
1072
- * @memberOf fabric.util
1073
- * @param {Array} transform an array with 6 numbers
1074
- * @return {String} transform matrix for svg
1075
- * @return {Object.y} Limited dimensions by Y
1076
- */
1077
- matrixToSVG: function(transform) {
1078
- return 'matrix(' + transform.map(function(value) {
1079
- return fabric.util.toFixed(value, fabric.Object.NUM_FRACTION_DIGITS);
1080
- }).join(' ') + ')';
1081
- },
1082
-
1083
- /**
1084
- * given an object and a transform, apply the inverse transform to the object,
1085
- * this is equivalent to remove from that object that transformation, so that
1086
- * added in a space with the removed transform, the object will be the same as before.
1087
- * Removing from an object a transform that scale by 2 is like scaling it by 1/2.
1088
- * Removing from an object a transfrom that rotate by 30deg is like rotating by 30deg
1089
- * in the opposite direction.
1090
- * This util is used to add objects inside transformed groups or nested groups.
1091
- * @memberOf fabric.util
1092
- * @param {fabric.Object} object the object you want to transform
1093
- * @param {Array} transform the destination transform
1094
- */
1095
- removeTransformFromObject: function(object, transform) {
1096
- var inverted = fabric.util.invertTransform(transform),
1097
- finalTransform = fabric.util.multiplyTransformMatrices(inverted, object.calcOwnMatrix());
1098
- fabric.util.applyTransformToObject(object, finalTransform);
1099
- },
1100
-
1101
- /**
1102
- * given an object and a transform, apply the transform to the object.
1103
- * this is equivalent to change the space where the object is drawn.
1104
- * Adding to an object a transform that scale by 2 is like scaling it by 2.
1105
- * This is used when removing an object from an active selection for example.
1106
- * @memberOf fabric.util
1107
- * @param {fabric.Object} object the object you want to transform
1108
- * @param {Array} transform the destination transform
1109
- */
1110
- addTransformToObject: function(object, transform) {
1111
- fabric.util.applyTransformToObject(
1112
- object,
1113
- fabric.util.multiplyTransformMatrices(transform, object.calcOwnMatrix())
1114
- );
1115
- },
1116
-
1117
- /**
1118
- * discard an object transform state and apply the one from the matrix.
1119
- * @memberOf fabric.util
1120
- * @param {fabric.Object} object the object you want to transform
1121
- * @param {Array} transform the destination transform
1122
- */
1123
- applyTransformToObject: function(object, transform) {
1124
- var options = fabric.util.qrDecompose(transform),
1125
- center = new fabric.Point(options.translateX, options.translateY);
1126
- object.flipX = false;
1127
- object.flipY = false;
1128
- object.set('scaleX', options.scaleX);
1129
- object.set('scaleY', options.scaleY);
1130
- object.skewX = options.skewX;
1131
- object.skewY = options.skewY;
1132
- object.angle = options.angle;
1133
- object.setPositionByOrigin(center, 'center', 'center');
1134
- },
1135
-
1136
- /**
1137
- * given a width and height, return the size of the bounding box
1138
- * that can contains the box with width/height with applied transform
1139
- * described in options.
1140
- * Use to calculate the boxes around objects for controls.
1141
- * @memberOf fabric.util
1142
- * @param {Number} width
1143
- * @param {Number} height
1144
- * @param {Object} options
1145
- * @param {Number} options.scaleX
1146
- * @param {Number} options.scaleY
1147
- * @param {Number} options.skewX
1148
- * @param {Number} options.skewY
1149
- * @return {Object.x} width of containing
1150
- * @return {Object.y} height of containing
1151
- */
1152
- sizeAfterTransform: function(width, height, options) {
1153
- var dimX = width / 2, dimY = height / 2,
1154
- points = [
1155
- {
1156
- x: -dimX,
1157
- y: -dimY
1158
- },
1159
- {
1160
- x: dimX,
1161
- y: -dimY
1162
- },
1163
- {
1164
- x: -dimX,
1165
- y: dimY
1166
- },
1167
- {
1168
- x: dimX,
1169
- y: dimY
1170
- }],
1171
- transformMatrix = fabric.util.calcDimensionsMatrix(options),
1172
- bbox = fabric.util.makeBoundingBoxFromPoints(points, transformMatrix);
1173
- return {
1174
- x: bbox.width,
1175
- y: bbox.height,
1176
- };
1177
- },
1178
-
1179
- /**
1180
- * Merges 2 clip paths into one visually equal clip path
1181
- *
1182
- * **IMPORTANT**:\
1183
- * Does **NOT** clone the arguments, clone them proir if necessary.
1184
- *
1185
- * Creates a wrapper (group) that contains one clip path and is clipped by the other so content is kept where both overlap.
1186
- * Use this method if both the clip paths may have nested clip paths of their own, so assigning one to the other's clip path property is not possible.
1187
- *
1188
- * In order to handle the `inverted` property we follow logic described in the following cases:\
1189
- * **(1)** both clip paths are inverted - the clip paths pass the inverted prop to the wrapper and loose it themselves.\
1190
- * **(2)** one is inverted and the other isn't - the wrapper shouldn't become inverted and the inverted clip path must clip the non inverted one to produce an identical visual effect.\
1191
- * **(3)** both clip paths are not inverted - wrapper and clip paths remain unchanged.
1192
- *
1193
- * @memberOf fabric.util
1194
- * @param {fabric.Object} c1
1195
- * @param {fabric.Object} c2
1196
- * @returns {fabric.Object} merged clip path
1197
- */
1198
- mergeClipPaths: function (c1, c2) {
1199
- var a = c1, b = c2;
1200
- if (a.inverted && !b.inverted) {
1201
- // case (2)
1202
- a = c2;
1203
- b = c1;
1204
- }
1205
- // `b` becomes `a`'s clip path so we transform `b` to `a` coordinate plane
1206
- fabric.util.applyTransformToObject(
1207
- b,
1208
- fabric.util.multiplyTransformMatrices(
1209
- fabric.util.invertTransform(a.calcTransformMatrix()),
1210
- b.calcTransformMatrix()
1211
- )
1212
- );
1213
- // assign the `inverted` prop to the wrapping group
1214
- var inverted = a.inverted && b.inverted;
1215
- if (inverted) {
1216
- // case (1)
1217
- a.inverted = b.inverted = false;
1218
- }
1219
- return new fabric.Group([a], { clipPath: b, inverted: inverted });
1220
- },
1221
-
1222
- /**
1223
- * @memberOf fabric.util
1224
- * @param {Object} prevStyle first style to compare
1225
- * @param {Object} thisStyle second style to compare
1226
- * @param {boolean} forTextSpans whether to check overline, underline, and line-through properties
1227
- * @return {boolean} true if the style changed
1228
- */
1229
- hasStyleChanged: function(prevStyle, thisStyle, forTextSpans) {
1230
- forTextSpans = forTextSpans || false;
1231
- return (prevStyle.fill !== thisStyle.fill ||
1232
- prevStyle.stroke !== thisStyle.stroke ||
1233
- prevStyle.strokeWidth !== thisStyle.strokeWidth ||
1234
- prevStyle.fontSize !== thisStyle.fontSize ||
1235
- prevStyle.fontFamily !== thisStyle.fontFamily ||
1236
- prevStyle.fontWeight !== thisStyle.fontWeight ||
1237
- prevStyle.fontStyle !== thisStyle.fontStyle ||
1238
- prevStyle.textBackgroundColor !== thisStyle.textBackgroundColor ||
1239
- prevStyle.deltaY !== thisStyle.deltaY) ||
1240
- (forTextSpans &&
1241
- (prevStyle.overline !== thisStyle.overline ||
1242
- prevStyle.underline !== thisStyle.underline ||
1243
- prevStyle.linethrough !== thisStyle.linethrough));
1244
- },
1245
-
1246
- /**
1247
- * Returns the array form of a text object's inline styles property with styles grouped in ranges
1248
- * rather than per character. This format is less verbose, and is better suited for storage
1249
- * so it is used in serialization (not during runtime).
1250
- * @memberOf fabric.util
1251
- * @param {object} styles per character styles for a text object
1252
- * @param {String} text the text string that the styles are applied to
1253
- * @return {{start: number, end: number, style: object}[]}
1254
- */
1255
- stylesToArray: function(styles, text) {
1256
- // clone style structure to prevent mutation
1257
- var styles = fabric.util.object.clone(styles, true),
1258
- textLines = text.split('\n'),
1259
- charIndex = -1, prevStyle = {}, stylesArray = [];
1260
- //loop through each textLine
1261
- for (var i = 0; i < textLines.length; i++) {
1262
- if (!styles[i]) {
1263
- //no styles exist for this line, so add the line's length to the charIndex total
1264
- charIndex += textLines[i].length;
1265
- continue;
1266
- }
1267
- //loop through each character of the current line
1268
- for (var c = 0; c < textLines[i].length; c++) {
1269
- charIndex++;
1270
- var thisStyle = styles[i][c];
1271
- //check if style exists for this character
1272
- if (thisStyle && Object.keys(thisStyle).length > 0) {
1273
- var styleChanged = fabric.util.hasStyleChanged(prevStyle, thisStyle, true);
1274
- if (styleChanged) {
1275
- stylesArray.push({
1276
- start: charIndex,
1277
- end: charIndex + 1,
1278
- style: thisStyle
1279
- });
1280
- }
1281
- else {
1282
- //if style is the same as previous character, increase end index
1283
- stylesArray[stylesArray.length - 1].end++;
1284
- }
1285
- }
1286
- prevStyle = thisStyle || {};
1287
- }
1288
- }
1289
- return stylesArray;
1290
- },
1291
-
1292
- /**
1293
- * Returns the object form of the styles property with styles that are assigned per
1294
- * character rather than grouped by range. This format is more verbose, and is
1295
- * only used during runtime (not for serialization/storage)
1296
- * @memberOf fabric.util
1297
- * @param {Array} styles the serialized form of a text object's styles
1298
- * @param {String} text the text string that the styles are applied to
1299
- * @return {Object}
1300
- */
1301
- stylesFromArray: function(styles, text) {
1302
- if (!Array.isArray(styles)) {
1303
- return styles;
1304
- }
1305
- var textLines = text.split('\n'),
1306
- charIndex = -1, styleIndex = 0, stylesObject = {};
1307
- //loop through each textLine
1308
- for (var i = 0; i < textLines.length; i++) {
1309
- //loop through each character of the current line
1310
- for (var c = 0; c < textLines[i].length; c++) {
1311
- charIndex++;
1312
- //check if there's a style collection that includes the current character
1313
- if (styles[styleIndex]
1314
- && styles[styleIndex].start <= charIndex
1315
- && charIndex < styles[styleIndex].end) {
1316
- //create object for line index if it doesn't exist
1317
- stylesObject[i] = stylesObject[i] || {};
1318
- //assign a style at this character's index
1319
- stylesObject[i][c] = Object.assign({}, styles[styleIndex].style);
1320
- //if character is at the end of the current style collection, move to the next
1321
- if (charIndex === styles[styleIndex].end - 1) {
1322
- styleIndex++;
1323
- }
1324
- }
1325
- }
1326
- }
1327
- return stylesObject;
1328
- }
1329
- };
1330
- })(typeof exports !== 'undefined' ? exports : this);