@antv/util 2.0.15 → 3.0.0-beta.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 (348) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +15 -241
  3. package/dist/util.min.js +1 -0
  4. package/esm/color/arr2rgb.d.ts +12 -0
  5. package/esm/color/arr2rgb.js +18 -0
  6. package/esm/color/arr2rgb.js.map +1 -0
  7. package/esm/color/gradient.d.ts +6 -0
  8. package/esm/color/gradient.js +45 -0
  9. package/esm/color/gradient.js.map +1 -0
  10. package/esm/color/index.d.ts +4 -0
  11. package/esm/color/index.js +6 -0
  12. package/esm/color/index.js.map +1 -0
  13. package/esm/color/rgb2arr.d.ts +6 -0
  14. package/esm/color/rgb2arr.js +9 -0
  15. package/esm/color/rgb2arr.js.map +1 -0
  16. package/esm/color/tocssgradient.d.ts +1 -0
  17. package/esm/color/tocssgradient.js +37 -0
  18. package/esm/color/tocssgradient.js.map +1 -0
  19. package/esm/color/torgb.d.ts +6 -0
  20. package/esm/color/torgb.js +36 -0
  21. package/esm/color/torgb.js.map +1 -0
  22. package/esm/helper/index.d.ts +2 -0
  23. package/esm/helper/index.js +3 -0
  24. package/esm/helper/index.js.map +1 -0
  25. package/esm/helper/mod.d.ts +1 -0
  26. package/esm/helper/mod.js +4 -0
  27. package/esm/helper/mod.js.map +1 -0
  28. package/esm/helper/to-radian.d.ts +1 -0
  29. package/esm/helper/to-radian.js +5 -0
  30. package/esm/helper/to-radian.js.map +1 -0
  31. package/esm/index.d.ts +3 -0
  32. package/esm/index.js +4 -0
  33. package/esm/index.js.map +1 -0
  34. package/esm/matrix/index.d.ts +27 -0
  35. package/esm/matrix/index.js +97 -0
  36. package/esm/matrix/index.js.map +1 -0
  37. package/esm/path/catmull-rom-2-bezier.d.ts +10 -0
  38. package/esm/path/catmull-rom-2-bezier.js +115 -0
  39. package/esm/path/catmull-rom-2-bezier.js.map +1 -0
  40. package/esm/path/fill-path-by-diff.d.ts +5 -0
  41. package/esm/path/fill-path-by-diff.js +110 -0
  42. package/esm/path/fill-path-by-diff.js.map +1 -0
  43. package/esm/path/fill-path.d.ts +1 -0
  44. package/esm/path/fill-path.js +129 -0
  45. package/esm/path/fill-path.js.map +1 -0
  46. package/esm/path/format-path.d.ts +1 -0
  47. package/esm/path/format-path.js +139 -0
  48. package/esm/path/format-path.js.map +1 -0
  49. package/esm/path/get-arc-params.d.ts +17 -0
  50. package/esm/path/get-arc-params.js +90 -0
  51. package/esm/path/get-arc-params.js.map +1 -0
  52. package/esm/path/get-line-intersect.d.ts +14 -0
  53. package/esm/path/get-line-intersect.js +33 -0
  54. package/esm/path/get-line-intersect.js.map +1 -0
  55. package/esm/path/index.d.ts +16 -0
  56. package/esm/path/index.js +17 -0
  57. package/esm/path/index.js.map +1 -0
  58. package/esm/path/is-polygons-intersect.d.ts +1 -0
  59. package/esm/path/is-polygons-intersect.js +81 -0
  60. package/esm/path/is-polygons-intersect.js.map +1 -0
  61. package/esm/path/parse-path-array.d.ts +1 -0
  62. package/esm/path/parse-path-array.js +5 -0
  63. package/esm/path/parse-path-array.js.map +1 -0
  64. package/esm/path/parse-path-string.d.ts +1 -0
  65. package/esm/path/parse-path-string.js +57 -0
  66. package/esm/path/parse-path-string.js.map +1 -0
  67. package/esm/path/parse-path.d.ts +1 -0
  68. package/esm/path/parse-path.js +33 -0
  69. package/esm/path/parse-path.js.map +1 -0
  70. package/esm/path/path-2-absolute.d.ts +1 -0
  71. package/esm/path/path-2-absolute.js +124 -0
  72. package/esm/path/path-2-absolute.js.map +1 -0
  73. package/esm/path/path-2-curve.d.ts +2 -0
  74. package/esm/path/path-2-curve.js +63 -0
  75. package/esm/path/path-2-curve.js.map +1 -0
  76. package/esm/path/path-2-segments.d.ts +1 -0
  77. package/esm/path/path-2-segments.js +138 -0
  78. package/esm/path/path-2-segments.js.map +1 -0
  79. package/esm/path/path-intersection.d.ts +9 -0
  80. package/esm/path/path-intersection.js +366 -0
  81. package/esm/path/path-intersection.js.map +1 -0
  82. package/esm/path/point-in-polygon.d.ts +5 -0
  83. package/esm/path/point-in-polygon.js +49 -0
  84. package/esm/path/point-in-polygon.js.map +1 -0
  85. package/esm/path/process/arc-2-cubic.d.ts +1 -0
  86. package/esm/path/process/arc-2-cubic.js +140 -0
  87. package/esm/path/process/arc-2-cubic.js.map +1 -0
  88. package/esm/path/process/line-2-cubic.d.ts +1 -0
  89. package/esm/path/process/line-2-cubic.js +35 -0
  90. package/esm/path/process/line-2-cubic.js.map +1 -0
  91. package/esm/path/process/quad-2-cubic.d.ts +1 -0
  92. package/esm/path/process/quad-2-cubic.js +13 -0
  93. package/esm/path/process/quad-2-cubic.js.map +1 -0
  94. package/esm/path/process/segment-2-cubic.d.ts +2 -0
  95. package/esm/path/process/segment-2-cubic.js +37 -0
  96. package/esm/path/process/segment-2-cubic.js.map +1 -0
  97. package/esm/path/rect-path.d.ts +4 -0
  98. package/esm/path/rect-path.js +19 -0
  99. package/esm/path/rect-path.js.map +1 -0
  100. package/esm/path/types.d.ts +25 -0
  101. package/esm/path/types.js +2 -0
  102. package/esm/path/types.js.map +1 -0
  103. package/lib/color/arr2rgb.d.ts +12 -0
  104. package/lib/color/arr2rgb.js +23 -0
  105. package/lib/color/arr2rgb.js.map +1 -0
  106. package/lib/color/gradient.d.ts +6 -0
  107. package/lib/color/gradient.js +49 -0
  108. package/lib/color/gradient.js.map +1 -0
  109. package/lib/color/index.d.ts +4 -0
  110. package/lib/color/index.js +13 -0
  111. package/lib/color/index.js.map +1 -0
  112. package/lib/color/rgb2arr.d.ts +6 -0
  113. package/lib/color/rgb2arr.js +13 -0
  114. package/lib/color/rgb2arr.js.map +1 -0
  115. package/lib/color/tocssgradient.d.ts +1 -0
  116. package/lib/color/tocssgradient.js +41 -0
  117. package/lib/color/tocssgradient.js.map +1 -0
  118. package/lib/color/torgb.d.ts +6 -0
  119. package/lib/color/torgb.js +40 -0
  120. package/lib/color/torgb.js.map +1 -0
  121. package/lib/helper/index.d.ts +2 -0
  122. package/lib/helper/index.js +8 -0
  123. package/lib/helper/index.js.map +1 -0
  124. package/lib/helper/mod.d.ts +1 -0
  125. package/lib/helper/mod.js +8 -0
  126. package/lib/helper/mod.js.map +1 -0
  127. package/lib/helper/to-radian.d.ts +1 -0
  128. package/lib/helper/to-radian.js +9 -0
  129. package/lib/helper/to-radian.js.map +1 -0
  130. package/lib/index.d.ts +3 -0
  131. package/lib/index.js +16 -0
  132. package/lib/index.js.map +1 -0
  133. package/lib/matrix/index.d.ts +27 -0
  134. package/lib/matrix/index.js +104 -0
  135. package/lib/matrix/index.js.map +1 -0
  136. package/lib/path/catmull-rom-2-bezier.d.ts +10 -0
  137. package/lib/path/catmull-rom-2-bezier.js +119 -0
  138. package/lib/path/catmull-rom-2-bezier.js.map +1 -0
  139. package/lib/path/fill-path-by-diff.d.ts +5 -0
  140. package/lib/path/fill-path-by-diff.js +117 -0
  141. package/lib/path/fill-path-by-diff.js.map +1 -0
  142. package/lib/path/fill-path.d.ts +1 -0
  143. package/lib/path/fill-path.js +133 -0
  144. package/lib/path/fill-path.js.map +1 -0
  145. package/lib/path/format-path.d.ts +1 -0
  146. package/lib/path/format-path.js +143 -0
  147. package/lib/path/format-path.js.map +1 -0
  148. package/lib/path/get-arc-params.d.ts +17 -0
  149. package/lib/path/get-arc-params.js +95 -0
  150. package/lib/path/get-arc-params.js.map +1 -0
  151. package/lib/path/get-line-intersect.d.ts +14 -0
  152. package/lib/path/get-line-intersect.js +37 -0
  153. package/lib/path/get-line-intersect.js.map +1 -0
  154. package/lib/path/index.d.ts +16 -0
  155. package/lib/path/index.js +36 -0
  156. package/lib/path/index.js.map +1 -0
  157. package/lib/path/is-polygons-intersect.d.ts +1 -0
  158. package/lib/path/is-polygons-intersect.js +85 -0
  159. package/lib/path/is-polygons-intersect.js.map +1 -0
  160. package/lib/path/parse-path-array.d.ts +1 -0
  161. package/lib/path/parse-path-array.js +9 -0
  162. package/lib/path/parse-path-array.js.map +1 -0
  163. package/lib/path/parse-path-string.d.ts +1 -0
  164. package/lib/path/parse-path-string.js +61 -0
  165. package/lib/path/parse-path-string.js.map +1 -0
  166. package/lib/path/parse-path.d.ts +1 -0
  167. package/lib/path/parse-path.js +37 -0
  168. package/lib/path/parse-path.js.map +1 -0
  169. package/lib/path/path-2-absolute.d.ts +1 -0
  170. package/lib/path/path-2-absolute.js +128 -0
  171. package/lib/path/path-2-absolute.js.map +1 -0
  172. package/lib/path/path-2-curve.d.ts +2 -0
  173. package/lib/path/path-2-curve.js +67 -0
  174. package/lib/path/path-2-curve.js.map +1 -0
  175. package/lib/path/path-2-segments.d.ts +1 -0
  176. package/lib/path/path-2-segments.js +142 -0
  177. package/lib/path/path-2-segments.js.map +1 -0
  178. package/lib/path/path-intersection.d.ts +9 -0
  179. package/lib/path/path-intersection.js +370 -0
  180. package/lib/path/path-intersection.js.map +1 -0
  181. package/lib/path/point-in-polygon.d.ts +5 -0
  182. package/lib/path/point-in-polygon.js +53 -0
  183. package/lib/path/point-in-polygon.js.map +1 -0
  184. package/lib/path/process/arc-2-cubic.d.ts +1 -0
  185. package/lib/path/process/arc-2-cubic.js +144 -0
  186. package/lib/path/process/arc-2-cubic.js.map +1 -0
  187. package/lib/path/process/line-2-cubic.d.ts +1 -0
  188. package/lib/path/process/line-2-cubic.js +39 -0
  189. package/lib/path/process/line-2-cubic.js.map +1 -0
  190. package/lib/path/process/quad-2-cubic.d.ts +1 -0
  191. package/lib/path/process/quad-2-cubic.js +17 -0
  192. package/lib/path/process/quad-2-cubic.js.map +1 -0
  193. package/lib/path/process/segment-2-cubic.d.ts +2 -0
  194. package/lib/path/process/segment-2-cubic.js +41 -0
  195. package/lib/path/process/segment-2-cubic.js.map +1 -0
  196. package/lib/path/rect-path.d.ts +4 -0
  197. package/lib/path/rect-path.js +23 -0
  198. package/lib/path/rect-path.js.map +1 -0
  199. package/lib/path/types.d.ts +25 -0
  200. package/lib/path/types.js +3 -0
  201. package/lib/path/types.js.map +1 -0
  202. package/package.json +77 -36
  203. package/src/color/arr2rgb.ts +19 -0
  204. package/src/color/gradient.ts +55 -0
  205. package/src/color/index.ts +5 -0
  206. package/src/color/rgb2arr.ts +8 -0
  207. package/src/color/tocssgradient.ts +43 -0
  208. package/src/color/torgb.ts +45 -0
  209. package/src/helper/index.ts +2 -0
  210. package/src/helper/mod.ts +3 -0
  211. package/src/helper/to-radian.ts +5 -0
  212. package/src/index.ts +3 -130
  213. package/src/matrix/index.ts +108 -0
  214. package/src/path/catmull-rom-2-bezier.ts +141 -0
  215. package/src/path/fill-path-by-diff.ts +116 -0
  216. package/src/path/fill-path.ts +129 -0
  217. package/src/path/format-path.ts +142 -0
  218. package/src/path/get-arc-params.ts +102 -0
  219. package/src/path/get-line-intersect.ts +46 -0
  220. package/src/path/index.ts +16 -0
  221. package/src/path/is-polygons-intersect.ts +95 -0
  222. package/src/path/parse-path-array.ts +5 -0
  223. package/src/path/parse-path-string.ts +63 -0
  224. package/src/path/parse-path.ts +34 -0
  225. package/src/path/path-2-absolute.ts +132 -0
  226. package/src/path/path-2-curve.ts +69 -0
  227. package/src/path/path-2-segments.ts +142 -0
  228. package/src/path/path-intersection.ts +419 -0
  229. package/src/path/point-in-polygon.ts +56 -0
  230. package/src/path/process/arc-2-cubic.ts +221 -0
  231. package/src/path/process/line-2-cubic.ts +37 -0
  232. package/src/path/process/quad-2-cubic.ts +12 -0
  233. package/src/path/process/segment-2-cubic.ts +44 -0
  234. package/src/path/rect-path.ts +22 -0
  235. package/src/path/types.ts +25 -0
  236. package/src/augment.ts +0 -16
  237. package/src/cache.ts +0 -31
  238. package/src/clamp.ts +0 -10
  239. package/src/clear-animation-frame.ts +0 -11
  240. package/src/clone.ts +0 -31
  241. package/src/contains.ts +0 -10
  242. package/src/debounce.ts +0 -22
  243. package/src/deep-mix.ts +0 -39
  244. package/src/difference.ts +0 -17
  245. package/src/each.ts +0 -28
  246. package/src/ends-with.ts +0 -10
  247. package/src/every.ts +0 -14
  248. package/src/extend.ts +0 -36
  249. package/src/filter.ts +0 -18
  250. package/src/find-index.ts +0 -12
  251. package/src/find.ts +0 -29
  252. package/src/first-value.ts +0 -21
  253. package/src/fixed-base.ts +0 -14
  254. package/src/flatten-deep.ts +0 -24
  255. package/src/flatten.ts +0 -23
  256. package/src/for-in.ts +0 -3
  257. package/src/get-ellipsis-text.ts +0 -75
  258. package/src/get-range.ts +0 -35
  259. package/src/get-type.ts +0 -7
  260. package/src/get-wrap-behavior.ts +0 -13
  261. package/src/get.ts +0 -18
  262. package/src/group-by.ts +0 -35
  263. package/src/group-to-map.ts +0 -30
  264. package/src/group.ts +0 -14
  265. package/src/has-key.ts +0 -2
  266. package/src/has-value.ts +0 -4
  267. package/src/has.ts +0 -1
  268. package/src/head.ts +0 -8
  269. package/src/identity.ts +0 -1
  270. package/src/index-of.ts +0 -22
  271. package/src/is-arguments.ts +0 -13
  272. package/src/is-array-like.ts +0 -11
  273. package/src/is-array.ts +0 -7
  274. package/src/is-boolean.ts +0 -13
  275. package/src/is-date.ts +0 -7
  276. package/src/is-decimal.ts +0 -7
  277. package/src/is-element.ts +0 -8
  278. package/src/is-empty.ts +0 -39
  279. package/src/is-equal-with.ts +0 -31
  280. package/src/is-equal.ts +0 -46
  281. package/src/is-error.ts +0 -13
  282. package/src/is-even.ts +0 -7
  283. package/src/is-finite.ts +0 -11
  284. package/src/is-function.ts +0 -10
  285. package/src/is-integer.ts +0 -7
  286. package/src/is-match.ts +0 -17
  287. package/src/is-negative.ts +0 -7
  288. package/src/is-nil.ts +0 -10
  289. package/src/is-null.ts +0 -5
  290. package/src/is-number-equal.ts +0 -5
  291. package/src/is-number.ts +0 -10
  292. package/src/is-object-like.ts +0 -11
  293. package/src/is-object.ts +0 -10
  294. package/src/is-odd.ts +0 -7
  295. package/src/is-plain-object.ts +0 -24
  296. package/src/is-positive.ts +0 -7
  297. package/src/is-prototype.ts +0 -9
  298. package/src/is-reg-exp.ts +0 -7
  299. package/src/is-segment-equal.ts +0 -15
  300. package/src/is-string.ts +0 -5
  301. package/src/is-type.ts +0 -5
  302. package/src/is-undefined.ts +0 -5
  303. package/src/keys.ts +0 -14
  304. package/src/last.ts +0 -9
  305. package/src/lower-case.ts +0 -7
  306. package/src/lower-first.ts +0 -8
  307. package/src/map-values.ts +0 -19
  308. package/src/map.ts +0 -17
  309. package/src/max-by.ts +0 -37
  310. package/src/max.ts +0 -28
  311. package/src/measure-text-width.ts +0 -25
  312. package/src/memoize.ts +0 -31
  313. package/src/merge.ts +0 -12
  314. package/src/min-by.ts +0 -38
  315. package/src/min.ts +0 -28
  316. package/src/mix.ts +0 -15
  317. package/src/mod.ts +0 -5
  318. package/src/noop.ts +0 -1
  319. package/src/number2color.ts +0 -17
  320. package/src/omit.ts +0 -15
  321. package/src/parse-radius.ts +0 -45
  322. package/src/pick.ts +0 -32
  323. package/src/pull-at.ts +0 -23
  324. package/src/pull.ts +0 -16
  325. package/src/reduce.ts +0 -17
  326. package/src/remove.ts +0 -30
  327. package/src/request-animation-frame.ts +0 -13
  328. package/src/set.ts +0 -29
  329. package/src/size.ts +0 -12
  330. package/src/some.ts +0 -14
  331. package/src/sort-by.ts +0 -42
  332. package/src/starts-with.ts +0 -10
  333. package/src/substitute.ts +0 -17
  334. package/src/throttle.ts +0 -48
  335. package/src/to-array.ts +0 -5
  336. package/src/to-degree.ts +0 -7
  337. package/src/to-integer.ts +0 -1
  338. package/src/to-radian.ts +0 -7
  339. package/src/to-string.ts +0 -6
  340. package/src/types/index.ts +0 -3
  341. package/src/union.ts +0 -7
  342. package/src/uniq.ts +0 -15
  343. package/src/unique-id.ts +0 -11
  344. package/src/upper-case.ts +0 -7
  345. package/src/upper-first.ts +0 -8
  346. package/src/values-of-key.ts +0 -29
  347. package/src/values.ts +0 -15
  348. package/src/wrap-behavior.ts +0 -20
@@ -0,0 +1,129 @@
1
+ function decasteljau(points, t) {
2
+ const left = [];
3
+ const right = [];
4
+
5
+ function recurse(points, t) {
6
+ if (points.length === 1) {
7
+ left.push(points[0]);
8
+ right.push(points[0]);
9
+ } else {
10
+ const middlePoints = [];
11
+ for (let i = 0; i < points.length - 1; i++) {
12
+ if (i === 0) {
13
+ left.push(points[0]);
14
+ }
15
+ if (i === points.length - 2) {
16
+ right.push(points[i + 1]);
17
+ }
18
+ middlePoints[i] = [
19
+ (1 - t) * points[i][0] + t * points[i + 1][0],
20
+ (1 - t) * points[i][1] + t * points[i + 1][1],
21
+ ];
22
+ }
23
+ recurse(middlePoints, t);
24
+ }
25
+ }
26
+ if (points.length) {
27
+ recurse(points, t);
28
+ }
29
+ return { left, right: right.reverse() };
30
+ }
31
+
32
+ function splitCurve(start, end, count) {
33
+ const points = [[start[1], start[2]]];
34
+ count = count || 2;
35
+ const segments = [];
36
+ if (end[0] === 'A') {
37
+ points.push(end[6]);
38
+ points.push(end[7]);
39
+ } else if (end[0] === 'C') {
40
+ points.push([end[1], end[2]]);
41
+ points.push([end[3], end[4]]);
42
+ points.push([end[5], end[6]]);
43
+ } else if (end[0] === 'S' || end[0] === 'Q') {
44
+ points.push([end[1], end[2]]);
45
+ points.push([end[3], end[4]]);
46
+ } else {
47
+ points.push([end[1], end[2]]);
48
+ }
49
+
50
+ let leftSegments = points;
51
+ const t = 1 / count;
52
+
53
+ for (let i = 0; i < count - 1; i++) {
54
+ const rt = t / (1 - t * i);
55
+ const split = decasteljau(leftSegments, rt);
56
+ segments.push(split.left);
57
+ leftSegments = split.right;
58
+ }
59
+ segments.push(leftSegments);
60
+ const result = segments.map((segment) => {
61
+ let cmd = [];
62
+ if (segment.length === 4) {
63
+ cmd.push('C');
64
+ cmd = cmd.concat(segment[2]);
65
+ }
66
+ if (segment.length >= 3) {
67
+ if (segment.length === 3) {
68
+ cmd.push('Q');
69
+ }
70
+ cmd = cmd.concat(segment[1]);
71
+ }
72
+ if (segment.length === 2) {
73
+ cmd.push('L');
74
+ }
75
+ cmd = cmd.concat(segment[segment.length - 1]);
76
+ return cmd;
77
+ });
78
+ return result;
79
+ }
80
+
81
+ function splitSegment(start, end, count) {
82
+ if (count === 1) {
83
+ return [[].concat(start)];
84
+ }
85
+ let segments = [];
86
+ if (end[0] === 'L' || end[0] === 'C' || end[0] === 'Q') {
87
+ segments = segments.concat(splitCurve(start, end, count));
88
+ } else {
89
+ const temp = [].concat(start);
90
+ if (temp[0] === 'M') {
91
+ temp[0] = 'L';
92
+ }
93
+ for (let i = 0; i <= count - 1; i++) {
94
+ segments.push(temp);
95
+ }
96
+ }
97
+ return segments;
98
+ }
99
+
100
+ export function fillPath(source, target) {
101
+ if (source.length === 1) {
102
+ return source;
103
+ }
104
+ const sourceLen = source.length - 1;
105
+ const targetLen = target.length - 1;
106
+ const ratio = sourceLen / targetLen;
107
+ const segmentsToFill = [];
108
+ if (source.length === 1 && source[0][0] === 'M') {
109
+ for (let i = 0; i < targetLen - sourceLen; i++) {
110
+ source.push(source[0]);
111
+ }
112
+ return source;
113
+ }
114
+ for (let i = 0; i < targetLen; i++) {
115
+ const index = Math.floor(ratio * i);
116
+ segmentsToFill[index] = (segmentsToFill[index] || 0) + 1;
117
+ }
118
+ const filled = segmentsToFill.reduce((filled, count, i) => {
119
+ if (i === sourceLen) {
120
+ return filled.concat(source[sourceLen]);
121
+ }
122
+ return filled.concat(splitSegment(source[i], source[i + 1], count));
123
+ }, []);
124
+ filled.unshift(source[0]);
125
+ if (target[targetLen] === 'Z' || target[targetLen] === 'z') {
126
+ filled.push('Z');
127
+ }
128
+ return filled;
129
+ }
@@ -0,0 +1,142 @@
1
+ /*
2
+ * 抽取pathSegment中的关键点
3
+ * M,L,A,Q,H,V一个端点
4
+ * Q, S抽取一个端点,一个控制点
5
+ * C抽取一个端点,两个控制点
6
+ */
7
+ function _getSegmentPoints(segment: any[]): number[][] {
8
+ const points = [];
9
+ switch (segment[0]) {
10
+ case 'M':
11
+ points.push([segment[1], segment[2]]);
12
+ break;
13
+ case 'L':
14
+ points.push([segment[1], segment[2]]);
15
+ break;
16
+ case 'A':
17
+ points.push([segment[6], segment[7]]);
18
+ break;
19
+ case 'Q':
20
+ points.push([segment[3], segment[4]]);
21
+ points.push([segment[1], segment[2]]);
22
+ break;
23
+ case 'T':
24
+ points.push([segment[1], segment[2]]);
25
+ break;
26
+ case 'C':
27
+ points.push([segment[5], segment[6]]);
28
+ points.push([segment[1], segment[2]]);
29
+ points.push([segment[3], segment[4]]);
30
+ break;
31
+ case 'S':
32
+ points.push([segment[3], segment[4]]);
33
+ points.push([segment[1], segment[2]]);
34
+ break;
35
+ case 'H':
36
+ points.push([segment[1], segment[1]]);
37
+ break;
38
+ case 'V':
39
+ points.push([segment[1], segment[1]]);
40
+ break;
41
+ default:
42
+ }
43
+ return points;
44
+ }
45
+
46
+ // 将两个点均分成count个点
47
+ function _splitPoints(points: number[][], former: any[], count: number) {
48
+ const result = [].concat(points);
49
+ let index;
50
+ let t = 1 / (count + 1);
51
+ const formerEnd = _getSegmentPoints(former)[0];
52
+ for (let i = 1; i <= count; i++) {
53
+ t *= i;
54
+ index = Math.floor(points.length * t);
55
+ if (index === 0) {
56
+ result.unshift([formerEnd[0] * t + points[index][0] * (1 - t), formerEnd[1] * t + points[index][1] * (1 - t)]);
57
+ } else {
58
+ result.splice(index, 0, [
59
+ formerEnd[0] * t + points[index][0] * (1 - t),
60
+ formerEnd[1] * t + points[index][1] * (1 - t),
61
+ ]);
62
+ }
63
+ }
64
+ return result;
65
+ }
66
+
67
+ export function formatPath(fromPath: any[][], toPath: any[][]): any[][] {
68
+ if (fromPath.length <= 1) {
69
+ return fromPath;
70
+ }
71
+ let points;
72
+ for (let i = 0; i < toPath.length; i++) {
73
+ if (fromPath[i][0] !== toPath[i][0]) {
74
+ // 获取fromPath的pathSegment的端点,根据toPath的指令对其改造
75
+ points = _getSegmentPoints(fromPath[i]);
76
+ switch (toPath[i][0]) {
77
+ case 'M':
78
+ fromPath[i] = ['M'].concat(points[0]);
79
+ break;
80
+ case 'L':
81
+ fromPath[i] = ['L'].concat(points[0]);
82
+ break;
83
+ case 'A':
84
+ fromPath[i] = [].concat(toPath[i]);
85
+ fromPath[i][6] = points[0][0];
86
+ fromPath[i][7] = points[0][1];
87
+ break;
88
+ case 'Q':
89
+ if (points.length < 2) {
90
+ if (i > 0) {
91
+ points = _splitPoints(points, fromPath[i - 1], 1);
92
+ } else {
93
+ fromPath[i] = toPath[i];
94
+ break;
95
+ }
96
+ }
97
+ fromPath[i] = ['Q'].concat(
98
+ points.reduce((arr, i) => {
99
+ return arr.concat(i);
100
+ }, []),
101
+ );
102
+ break;
103
+ case 'T':
104
+ fromPath[i] = ['T'].concat(points[0]);
105
+ break;
106
+ case 'C':
107
+ if (points.length < 3) {
108
+ if (i > 0) {
109
+ points = _splitPoints(points, fromPath[i - 1], 2);
110
+ } else {
111
+ fromPath[i] = toPath[i];
112
+ break;
113
+ }
114
+ }
115
+ fromPath[i] = ['C'].concat(
116
+ points.reduce((arr, i) => {
117
+ return arr.concat(i);
118
+ }, []),
119
+ );
120
+ break;
121
+ case 'S':
122
+ if (points.length < 2) {
123
+ if (i > 0) {
124
+ points = _splitPoints(points, fromPath[i - 1], 1);
125
+ } else {
126
+ fromPath[i] = toPath[i];
127
+ break;
128
+ }
129
+ }
130
+ fromPath[i] = ['S'].concat(
131
+ points.reduce((arr, i) => {
132
+ return arr.concat(i);
133
+ }, []),
134
+ );
135
+ break;
136
+ default:
137
+ fromPath[i] = toPath[i];
138
+ }
139
+ }
140
+ }
141
+ return fromPath;
142
+ }
@@ -0,0 +1,102 @@
1
+ import { mod, toRadian } from '../helper';
2
+
3
+ // 向量长度
4
+ function vMag(v) {
5
+ return Math.sqrt(v[0] * v[0] + v[1] * v[1]);
6
+ }
7
+
8
+ // u.v/|u||v|,计算夹角的余弦值
9
+ function vRatio(u, v) {
10
+ // 当存在一个向量的长度为 0 时,夹角也为 0,即夹角的余弦值为 1
11
+ return vMag(u) * vMag(v) ? (u[0] * v[0] + u[1] * v[1]) / (vMag(u) * vMag(v)) : 1;
12
+ }
13
+
14
+ // 向量角度
15
+ function vAngle(u, v) {
16
+ return (u[0] * v[1] < u[1] * v[0] ? -1 : 1) * Math.acos(vRatio(u, v));
17
+ }
18
+
19
+ /**
20
+ * 判断两个点是否重合,点坐标的格式为 [x, y]
21
+ * @param {Array} point1 第一个点
22
+ * @param {Array} point2 第二个点
23
+ */
24
+ export function isSamePoint(point1, point2) {
25
+ return point1[0] === point2[0] && point1[1] === point2[1];
26
+ }
27
+
28
+ // A 0:rx 1:ry 2:x-axis-rotation 3:large-arc-flag 4:sweep-flag 5: x 6: y
29
+ export function getArcParams(startPoint, params) {
30
+ let rx = params[1];
31
+ let ry = params[2];
32
+ const xRotation = mod(toRadian(params[3]), Math.PI * 2);
33
+ const arcFlag = params[4];
34
+ const sweepFlag = params[5];
35
+ // 弧形起点坐标
36
+ const x1 = startPoint[0];
37
+ const y1 = startPoint[1];
38
+ // 弧形终点坐标
39
+ const x2 = params[6];
40
+ const y2 = params[7];
41
+ const xp = (Math.cos(xRotation) * (x1 - x2)) / 2.0 + (Math.sin(xRotation) * (y1 - y2)) / 2.0;
42
+ const yp = (-1 * Math.sin(xRotation) * (x1 - x2)) / 2.0 + (Math.cos(xRotation) * (y1 - y2)) / 2.0;
43
+ const lambda = (xp * xp) / (rx * rx) + (yp * yp) / (ry * ry);
44
+
45
+ if (lambda > 1) {
46
+ rx *= Math.sqrt(lambda);
47
+ ry *= Math.sqrt(lambda);
48
+ }
49
+ const diff = rx * rx * (yp * yp) + ry * ry * (xp * xp);
50
+
51
+ let f = diff ? Math.sqrt((rx * rx * (ry * ry) - diff) / diff) : 1;
52
+
53
+ if (arcFlag === sweepFlag) {
54
+ f *= -1;
55
+ }
56
+ if (isNaN(f)) {
57
+ f = 0;
58
+ }
59
+
60
+ // 旋转前的起点坐标,且当长半轴和短半轴的长度为 0 时,坐标按 (0, 0) 处理
61
+ const cxp = ry ? (f * rx * yp) / ry : 0;
62
+ const cyp = rx ? (f * -ry * xp) / rx : 0;
63
+
64
+ // 椭圆圆心坐标
65
+ const cx = (x1 + x2) / 2.0 + Math.cos(xRotation) * cxp - Math.sin(xRotation) * cyp;
66
+ const cy = (y1 + y2) / 2.0 + Math.sin(xRotation) * cxp + Math.cos(xRotation) * cyp;
67
+
68
+ // 起始点的单位向量
69
+ const u = [(xp - cxp) / rx, (yp - cyp) / ry];
70
+ // 终止点的单位向量
71
+ const v = [(-1 * xp - cxp) / rx, (-1 * yp - cyp) / ry];
72
+ // 计算起始点和圆心的连线,与 x 轴正方向的夹角
73
+ const theta = vAngle([1, 0], u);
74
+
75
+ // 计算圆弧起始点和终止点与椭圆圆心连线的夹角
76
+ let dTheta = vAngle(u, v);
77
+
78
+ if (vRatio(u, v) <= -1) {
79
+ dTheta = Math.PI;
80
+ }
81
+ if (vRatio(u, v) >= 1) {
82
+ dTheta = 0;
83
+ }
84
+ if (sweepFlag === 0 && dTheta > 0) {
85
+ dTheta = dTheta - 2 * Math.PI;
86
+ }
87
+ if (sweepFlag === 1 && dTheta < 0) {
88
+ dTheta = dTheta + 2 * Math.PI;
89
+ }
90
+ return {
91
+ cx,
92
+ cy,
93
+ // 弧形的起点和终点相同时,长轴和短轴的长度按 0 处理
94
+ rx: isSamePoint(startPoint, [x2, y2]) ? 0 : rx,
95
+ ry: isSamePoint(startPoint, [x2, y2]) ? 0 : ry,
96
+ startAngle: theta,
97
+ endAngle: theta + dTheta,
98
+ xRotation,
99
+ arcFlag,
100
+ sweepFlag,
101
+ };
102
+ }
@@ -0,0 +1,46 @@
1
+ type Point = {
2
+ /**
3
+ * x 值
4
+ * @type {number}
5
+ */
6
+ x: number;
7
+ /**
8
+ * y 值
9
+ * @type {number}
10
+ */
11
+ y: number;
12
+ };
13
+
14
+ const isBetween = (value: number, min: number, max: number) => value >= min && value <= max;
15
+
16
+ export function getLineIntersect(p0: Point, p1: Point, p2: Point, p3: Point): Point | null {
17
+ const tolerance = 0.001;
18
+ const E: Point = {
19
+ x: p2.x - p0.x,
20
+ y: p2.y - p0.y,
21
+ };
22
+ const D0: Point = {
23
+ x: p1.x - p0.x,
24
+ y: p1.y - p0.y,
25
+ };
26
+ const D1: Point = {
27
+ x: p3.x - p2.x,
28
+ y: p3.y - p2.y,
29
+ };
30
+ const kross: number = D0.x * D1.y - D0.y * D1.x;
31
+ const sqrKross: number = kross * kross;
32
+ const sqrLen0: number = D0.x * D0.x + D0.y * D0.y;
33
+ const sqrLen1: number = D1.x * D1.x + D1.y * D1.y;
34
+ let point: Point | null = null;
35
+ if (sqrKross > tolerance * sqrLen0 * sqrLen1) {
36
+ const s = (E.x * D1.y - E.y * D1.x) / kross;
37
+ const t = (E.x * D0.y - E.y * D0.x) / kross;
38
+ if (isBetween(s, 0, 1) && isBetween(t, 0, 1)) {
39
+ point = {
40
+ x: p0.x + s * D0.x,
41
+ y: p0.y + s * D0.y,
42
+ };
43
+ }
44
+ }
45
+ return point;
46
+ }
@@ -0,0 +1,16 @@
1
+ export { parsePath } from './parse-path';
2
+ export { catmullRom2Bezier } from './catmull-rom-2-bezier';
3
+ export { fillPath } from './fill-path';
4
+ export { fillPathByDiff } from './fill-path-by-diff';
5
+ export { formatPath } from './format-path';
6
+ export { pathIntersection } from './path-intersection';
7
+ export { parsePathArray } from './parse-path-array';
8
+ export { parsePathString } from './parse-path-string';
9
+ export { path2Curve } from './path-2-curve';
10
+ export { path2Absolute } from './path-2-absolute';
11
+ export { rectPath } from './rect-path';
12
+ export { getArcParams } from './get-arc-params';
13
+ export { path2Segments } from './path-2-segments';
14
+ export { getLineIntersect } from './get-line-intersect';
15
+ export { isPolygonsIntersect } from './is-polygons-intersect';
16
+ export { isPointInPolygon } from './point-in-polygon';
@@ -0,0 +1,95 @@
1
+ import { isPointInPolygon } from './point-in-polygon';
2
+ import { getLineIntersect } from './get-line-intersect';
3
+
4
+ function parseToLines(points) {
5
+ const lines = [];
6
+ const count = points.length;
7
+ for (let i = 0; i < count - 1; i++) {
8
+ const point = points[i];
9
+ const next = points[i + 1];
10
+ lines.push({
11
+ from: {
12
+ x: point[0],
13
+ y: point[1],
14
+ },
15
+ to: {
16
+ x: next[0],
17
+ y: next[1],
18
+ },
19
+ });
20
+ }
21
+ if (lines.length > 1) {
22
+ const first = points[0];
23
+ const last = points[count - 1];
24
+ lines.push({
25
+ from: {
26
+ x: last[0],
27
+ y: last[1],
28
+ },
29
+ to: {
30
+ x: first[0],
31
+ y: first[1],
32
+ },
33
+ });
34
+ }
35
+ return lines;
36
+ }
37
+
38
+ function lineIntersectPolygon(lines, line) {
39
+ for (let i = 0; i < lines.length; i++) {
40
+ const l = lines[i];
41
+ if (getLineIntersect(l.from, l.to, line.from, line.to)) {
42
+ return true;
43
+ }
44
+ }
45
+ return false;
46
+ }
47
+
48
+ type BBox = {
49
+ minX: number;
50
+ minY: number;
51
+ maxX: number;
52
+ maxY: number;
53
+ };
54
+
55
+ function getBBox(points): BBox {
56
+ const xArr = points.map((p) => p[0]);
57
+ const yArr = points.map((p) => p[1]);
58
+ return {
59
+ minX: Math.min.apply(null, xArr),
60
+ maxX: Math.max.apply(null, xArr),
61
+ minY: Math.min.apply(null, yArr),
62
+ maxY: Math.max.apply(null, yArr),
63
+ };
64
+ }
65
+
66
+ function intersectBBox(box1: BBox, box2: BBox) {
67
+ return !(box2.minX > box1.maxX || box2.maxX < box1.minX || box2.minY > box1.maxY || box2.maxY < box1.minY);
68
+ }
69
+
70
+ export function isPolygonsIntersect(points1, points2) {
71
+ // 空数组,或者一个点返回 false
72
+ if (points1.length < 2 || points2.length < 2) {
73
+ return false;
74
+ }
75
+
76
+ const bbox1 = getBBox(points1);
77
+ const bbox2 = getBBox(points2);
78
+ // 判定包围盒是否相交,比判定点是否在多边形内要快的多,可以筛选掉大多数情况
79
+ if (!intersectBBox(bbox1, bbox2)) {
80
+ return false;
81
+ }
82
+
83
+ // 判定点是否在多边形内部,一旦有一个点在另一个多边形内,则返回
84
+ let hasOneIn = points2.some((point) => isPointInPolygon(points1, point[0], point[1]));
85
+ if (hasOneIn) return true;
86
+
87
+ // 两个多边形都需要判定
88
+ hasOneIn = points1.some((point) => isPointInPolygon(points2, point[0], point[1]));
89
+ if (hasOneIn) return true;
90
+
91
+ const lines1 = parseToLines(points1);
92
+ const lines2 = parseToLines(points2);
93
+
94
+ return lines2.some((line) => lineIntersectPolygon(lines1, line));
95
+ }
@@ -0,0 +1,5 @@
1
+ const p2s = /,?([a-z]),?/gi;
2
+
3
+ export function parsePathArray(path: any[]): string {
4
+ return path.join(',').replace(p2s, '$1');
5
+ }
@@ -0,0 +1,63 @@
1
+ const SPACES =
2
+ '\x09\x0a\x0b\x0c\x0d\x20\xa0\u1680\u180e\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200a\u202f\u205f\u3000\u2028\u2029';
3
+ const PATH_COMMAND = new RegExp(
4
+ '([a-z])[' + SPACES + ',]*((-?\\d*\\.?\\d*(?:e[\\-+]?\\d+)?[' + SPACES + ']*,?[' + SPACES + ']*)+)',
5
+ 'ig',
6
+ );
7
+ const PATH_VALUES = new RegExp('(-?\\d*\\.?\\d*(?:e[\\-+]?\\d+)?)[' + SPACES + ']*,?[' + SPACES + ']*', 'ig');
8
+
9
+ // Parses given path string into an array of arrays of path segments
10
+ export function parsePathString(pathString: string) {
11
+ if (!pathString) {
12
+ return null;
13
+ }
14
+
15
+ if (Array.isArray(pathString)) {
16
+ return pathString;
17
+ }
18
+ const paramCounts = {
19
+ a: 7,
20
+ c: 6,
21
+ o: 2,
22
+ h: 1,
23
+ l: 2,
24
+ m: 2,
25
+ r: 4,
26
+ q: 4,
27
+ s: 4,
28
+ t: 2,
29
+ v: 1,
30
+ u: 3,
31
+ z: 0,
32
+ };
33
+ const data = [];
34
+
35
+ String(pathString).replace(PATH_COMMAND, function (a, b, c) {
36
+ const params = [];
37
+ let name = b.toLowerCase();
38
+ c.replace(PATH_VALUES, function (a, b) {
39
+ b && params.push(+b);
40
+ });
41
+ if (name === 'm' && params.length > 2) {
42
+ data.push([b].concat(params.splice(0, 2)));
43
+ name = 'l';
44
+ b = b === 'm' ? 'l' : 'L';
45
+ }
46
+ if (name === 'o' && params.length === 1) {
47
+ data.push([b, params[0]]);
48
+ }
49
+ if (name === 'r') {
50
+ data.push([b].concat(params));
51
+ } else {
52
+ while (params.length >= paramCounts[name]) {
53
+ data.push([b].concat(params.splice(0, paramCounts[name])));
54
+ if (!paramCounts[name]) {
55
+ break;
56
+ }
57
+ }
58
+ }
59
+ return '';
60
+ });
61
+
62
+ return data;
63
+ }
@@ -0,0 +1,34 @@
1
+ const regexTags = /[MLHVQTCSAZ]([^MLHVQTCSAZ]*)/gi;
2
+ const regexDot = /[^\s,]+/gi;
3
+
4
+ export function parsePath(p: string): string[] {
5
+ let path = p || ([] as string | string[]);
6
+ if (Array.isArray(path)) {
7
+ return path;
8
+ }
9
+
10
+ if (typeof path === 'string') {
11
+ path = path.match(regexTags);
12
+ path.forEach((item, index) => {
13
+ // @ts-ignore
14
+ item = item.match(regexDot);
15
+ if (item[0].length > 1) {
16
+ const tag = item[0].charAt(0);
17
+ // @ts-ignore
18
+ item.splice(1, 0, item[0].substr(1));
19
+ // @ts-ignore
20
+ item[0] = tag;
21
+ }
22
+ // @ts-ignore
23
+ item.forEach(function (sub, i) {
24
+ if (!isNaN(sub)) {
25
+ // @ts-ignore
26
+ item[i] = +sub;
27
+ }
28
+ });
29
+ // @ts-ignore
30
+ path[index] = item;
31
+ });
32
+ return path;
33
+ }
34
+ }