e8d463cf63a7d625bd118a828be9e20510ae4f41b66ae877d4c690a99f25c18cd950012e38484b62c62758d59622be44b7bbebb1b91c9d1001220705990284 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901
  1. "use strict";
  2. var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
  3. Object.defineProperty(exports, "__esModule", {
  4. value: true
  5. });
  6. exports.extendNewGraph = extendNewGraph;
  7. exports["default"] = exports.text = exports.bezierCurve = exports.smoothline = exports.polyline = exports.regPolygon = exports.sector = exports.arc = exports.ring = exports.rect = exports.ellipse = exports.circle = void 0;
  8. var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray"));
  9. var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));
  10. var _bezierCurve2 = _interopRequireDefault(require("@jiaminghi/bezier-curve"));
  11. var _util = require("../plugin/util");
  12. var _canvas = require("../plugin/canvas");
  13. var polylineToBezierCurve = _bezierCurve2["default"].polylineToBezierCurve,
  14. bezierCurveToPolyline = _bezierCurve2["default"].bezierCurveToPolyline;
  15. var circle = {
  16. shape: {
  17. rx: 0,
  18. ry: 0,
  19. r: 0
  20. },
  21. validator: function validator(_ref) {
  22. var shape = _ref.shape;
  23. var rx = shape.rx,
  24. ry = shape.ry,
  25. r = shape.r;
  26. if (typeof rx !== 'number' || typeof ry !== 'number' || typeof r !== 'number') {
  27. console.error('Circle shape configuration is abnormal!');
  28. return false;
  29. }
  30. return true;
  31. },
  32. draw: function draw(_ref2, _ref3) {
  33. var ctx = _ref2.ctx;
  34. var shape = _ref3.shape;
  35. ctx.beginPath();
  36. var rx = shape.rx,
  37. ry = shape.ry,
  38. r = shape.r;
  39. ctx.arc(rx, ry, r > 0 ? r : 0.01, 0, Math.PI * 2);
  40. ctx.fill();
  41. ctx.stroke();
  42. ctx.closePath();
  43. },
  44. hoverCheck: function hoverCheck(position, _ref4) {
  45. var shape = _ref4.shape;
  46. var rx = shape.rx,
  47. ry = shape.ry,
  48. r = shape.r;
  49. return (0, _util.checkPointIsInCircle)(position, rx, ry, r);
  50. },
  51. setGraphCenter: function setGraphCenter(e, _ref5) {
  52. var shape = _ref5.shape,
  53. style = _ref5.style;
  54. var rx = shape.rx,
  55. ry = shape.ry;
  56. style.graphCenter = [rx, ry];
  57. },
  58. move: function move(_ref6, _ref7) {
  59. var movementX = _ref6.movementX,
  60. movementY = _ref6.movementY;
  61. var shape = _ref7.shape;
  62. this.attr('shape', {
  63. rx: shape.rx + movementX,
  64. ry: shape.ry + movementY
  65. });
  66. }
  67. };
  68. exports.circle = circle;
  69. var ellipse = {
  70. shape: {
  71. rx: 0,
  72. ry: 0,
  73. hr: 0,
  74. vr: 0
  75. },
  76. validator: function validator(_ref8) {
  77. var shape = _ref8.shape;
  78. var rx = shape.rx,
  79. ry = shape.ry,
  80. hr = shape.hr,
  81. vr = shape.vr;
  82. if (typeof rx !== 'number' || typeof ry !== 'number' || typeof hr !== 'number' || typeof vr !== 'number') {
  83. console.error('Ellipse shape configuration is abnormal!');
  84. return false;
  85. }
  86. return true;
  87. },
  88. draw: function draw(_ref9, _ref10) {
  89. var ctx = _ref9.ctx;
  90. var shape = _ref10.shape;
  91. ctx.beginPath();
  92. var rx = shape.rx,
  93. ry = shape.ry,
  94. hr = shape.hr,
  95. vr = shape.vr;
  96. ctx.ellipse(rx, ry, hr > 0 ? hr : 0.01, vr > 0 ? vr : 0.01, 0, 0, Math.PI * 2);
  97. ctx.fill();
  98. ctx.stroke();
  99. ctx.closePath();
  100. },
  101. hoverCheck: function hoverCheck(position, _ref11) {
  102. var shape = _ref11.shape;
  103. var rx = shape.rx,
  104. ry = shape.ry,
  105. hr = shape.hr,
  106. vr = shape.vr;
  107. var a = Math.max(hr, vr);
  108. var b = Math.min(hr, vr);
  109. var c = Math.sqrt(a * a - b * b);
  110. var leftFocusPoint = [rx - c, ry];
  111. var rightFocusPoint = [rx + c, ry];
  112. var distance = (0, _util.getTwoPointDistance)(position, leftFocusPoint) + (0, _util.getTwoPointDistance)(position, rightFocusPoint);
  113. return distance <= 2 * a;
  114. },
  115. setGraphCenter: function setGraphCenter(e, _ref12) {
  116. var shape = _ref12.shape,
  117. style = _ref12.style;
  118. var rx = shape.rx,
  119. ry = shape.ry;
  120. style.graphCenter = [rx, ry];
  121. },
  122. move: function move(_ref13, _ref14) {
  123. var movementX = _ref13.movementX,
  124. movementY = _ref13.movementY;
  125. var shape = _ref14.shape;
  126. this.attr('shape', {
  127. rx: shape.rx + movementX,
  128. ry: shape.ry + movementY
  129. });
  130. }
  131. };
  132. exports.ellipse = ellipse;
  133. var rect = {
  134. shape: {
  135. x: 0,
  136. y: 0,
  137. w: 0,
  138. h: 0
  139. },
  140. validator: function validator(_ref15) {
  141. var shape = _ref15.shape;
  142. var x = shape.x,
  143. y = shape.y,
  144. w = shape.w,
  145. h = shape.h;
  146. if (typeof x !== 'number' || typeof y !== 'number' || typeof w !== 'number' || typeof h !== 'number') {
  147. console.error('Rect shape configuration is abnormal!');
  148. return false;
  149. }
  150. return true;
  151. },
  152. draw: function draw(_ref16, _ref17) {
  153. var ctx = _ref16.ctx;
  154. var shape = _ref17.shape;
  155. ctx.beginPath();
  156. var x = shape.x,
  157. y = shape.y,
  158. w = shape.w,
  159. h = shape.h;
  160. ctx.rect(x, y, w, h);
  161. ctx.fill();
  162. ctx.stroke();
  163. ctx.closePath();
  164. },
  165. hoverCheck: function hoverCheck(position, _ref18) {
  166. var shape = _ref18.shape;
  167. var x = shape.x,
  168. y = shape.y,
  169. w = shape.w,
  170. h = shape.h;
  171. return (0, _util.checkPointIsInRect)(position, x, y, w, h);
  172. },
  173. setGraphCenter: function setGraphCenter(e, _ref19) {
  174. var shape = _ref19.shape,
  175. style = _ref19.style;
  176. var x = shape.x,
  177. y = shape.y,
  178. w = shape.w,
  179. h = shape.h;
  180. style.graphCenter = [x + w / 2, y + h / 2];
  181. },
  182. move: function move(_ref20, _ref21) {
  183. var movementX = _ref20.movementX,
  184. movementY = _ref20.movementY;
  185. var shape = _ref21.shape;
  186. this.attr('shape', {
  187. x: shape.x + movementX,
  188. y: shape.y + movementY
  189. });
  190. }
  191. };
  192. exports.rect = rect;
  193. var ring = {
  194. shape: {
  195. rx: 0,
  196. ry: 0,
  197. r: 0
  198. },
  199. validator: function validator(_ref22) {
  200. var shape = _ref22.shape;
  201. var rx = shape.rx,
  202. ry = shape.ry,
  203. r = shape.r;
  204. if (typeof rx !== 'number' || typeof ry !== 'number' || typeof r !== 'number') {
  205. console.error('Ring shape configuration is abnormal!');
  206. return false;
  207. }
  208. return true;
  209. },
  210. draw: function draw(_ref23, _ref24) {
  211. var ctx = _ref23.ctx;
  212. var shape = _ref24.shape;
  213. ctx.beginPath();
  214. var rx = shape.rx,
  215. ry = shape.ry,
  216. r = shape.r;
  217. ctx.arc(rx, ry, r > 0 ? r : 0.01, 0, Math.PI * 2);
  218. ctx.stroke();
  219. ctx.closePath();
  220. },
  221. hoverCheck: function hoverCheck(position, _ref25) {
  222. var shape = _ref25.shape,
  223. style = _ref25.style;
  224. var rx = shape.rx,
  225. ry = shape.ry,
  226. r = shape.r;
  227. var lineWidth = style.lineWidth;
  228. var halfLineWidth = lineWidth / 2;
  229. var minDistance = r - halfLineWidth;
  230. var maxDistance = r + halfLineWidth;
  231. var distance = (0, _util.getTwoPointDistance)(position, [rx, ry]);
  232. return distance >= minDistance && distance <= maxDistance;
  233. },
  234. setGraphCenter: function setGraphCenter(e, _ref26) {
  235. var shape = _ref26.shape,
  236. style = _ref26.style;
  237. var rx = shape.rx,
  238. ry = shape.ry;
  239. style.graphCenter = [rx, ry];
  240. },
  241. move: function move(_ref27, _ref28) {
  242. var movementX = _ref27.movementX,
  243. movementY = _ref27.movementY;
  244. var shape = _ref28.shape;
  245. this.attr('shape', {
  246. rx: shape.rx + movementX,
  247. ry: shape.ry + movementY
  248. });
  249. }
  250. };
  251. exports.ring = ring;
  252. var arc = {
  253. shape: {
  254. rx: 0,
  255. ry: 0,
  256. r: 0,
  257. startAngle: 0,
  258. endAngle: 0,
  259. clockWise: true
  260. },
  261. validator: function validator(_ref29) {
  262. var shape = _ref29.shape;
  263. var keys = ['rx', 'ry', 'r', 'startAngle', 'endAngle'];
  264. if (keys.find(function (key) {
  265. return typeof shape[key] !== 'number';
  266. })) {
  267. console.error('Arc shape configuration is abnormal!');
  268. return false;
  269. }
  270. return true;
  271. },
  272. draw: function draw(_ref30, _ref31) {
  273. var ctx = _ref30.ctx;
  274. var shape = _ref31.shape;
  275. ctx.beginPath();
  276. var rx = shape.rx,
  277. ry = shape.ry,
  278. r = shape.r,
  279. startAngle = shape.startAngle,
  280. endAngle = shape.endAngle,
  281. clockWise = shape.clockWise;
  282. ctx.arc(rx, ry, r > 0 ? r : 0.001, startAngle, endAngle, !clockWise);
  283. ctx.stroke();
  284. ctx.closePath();
  285. },
  286. hoverCheck: function hoverCheck(position, _ref32) {
  287. var shape = _ref32.shape,
  288. style = _ref32.style;
  289. var rx = shape.rx,
  290. ry = shape.ry,
  291. r = shape.r,
  292. startAngle = shape.startAngle,
  293. endAngle = shape.endAngle,
  294. clockWise = shape.clockWise;
  295. var lineWidth = style.lineWidth;
  296. var halfLineWidth = lineWidth / 2;
  297. var insideRadius = r - halfLineWidth;
  298. var outsideRadius = r + halfLineWidth;
  299. return !(0, _util.checkPointIsInSector)(position, rx, ry, insideRadius, startAngle, endAngle, clockWise) && (0, _util.checkPointIsInSector)(position, rx, ry, outsideRadius, startAngle, endAngle, clockWise);
  300. },
  301. setGraphCenter: function setGraphCenter(e, _ref33) {
  302. var shape = _ref33.shape,
  303. style = _ref33.style;
  304. var rx = shape.rx,
  305. ry = shape.ry;
  306. style.graphCenter = [rx, ry];
  307. },
  308. move: function move(_ref34, _ref35) {
  309. var movementX = _ref34.movementX,
  310. movementY = _ref34.movementY;
  311. var shape = _ref35.shape;
  312. this.attr('shape', {
  313. rx: shape.rx + movementX,
  314. ry: shape.ry + movementY
  315. });
  316. }
  317. };
  318. exports.arc = arc;
  319. var sector = {
  320. shape: {
  321. rx: 0,
  322. ry: 0,
  323. r: 0,
  324. startAngle: 0,
  325. endAngle: 0,
  326. clockWise: true
  327. },
  328. validator: function validator(_ref36) {
  329. var shape = _ref36.shape;
  330. var keys = ['rx', 'ry', 'r', 'startAngle', 'endAngle'];
  331. if (keys.find(function (key) {
  332. return typeof shape[key] !== 'number';
  333. })) {
  334. console.error('Sector shape configuration is abnormal!');
  335. return false;
  336. }
  337. return true;
  338. },
  339. draw: function draw(_ref37, _ref38) {
  340. var ctx = _ref37.ctx;
  341. var shape = _ref38.shape;
  342. ctx.beginPath();
  343. var rx = shape.rx,
  344. ry = shape.ry,
  345. r = shape.r,
  346. startAngle = shape.startAngle,
  347. endAngle = shape.endAngle,
  348. clockWise = shape.clockWise;
  349. ctx.arc(rx, ry, r > 0 ? r : 0.01, startAngle, endAngle, !clockWise);
  350. ctx.lineTo(rx, ry);
  351. ctx.closePath();
  352. ctx.stroke();
  353. ctx.fill();
  354. },
  355. hoverCheck: function hoverCheck(position, _ref39) {
  356. var shape = _ref39.shape;
  357. var rx = shape.rx,
  358. ry = shape.ry,
  359. r = shape.r,
  360. startAngle = shape.startAngle,
  361. endAngle = shape.endAngle,
  362. clockWise = shape.clockWise;
  363. return (0, _util.checkPointIsInSector)(position, rx, ry, r, startAngle, endAngle, clockWise);
  364. },
  365. setGraphCenter: function setGraphCenter(e, _ref40) {
  366. var shape = _ref40.shape,
  367. style = _ref40.style;
  368. var rx = shape.rx,
  369. ry = shape.ry;
  370. style.graphCenter = [rx, ry];
  371. },
  372. move: function move(_ref41, _ref42) {
  373. var movementX = _ref41.movementX,
  374. movementY = _ref41.movementY;
  375. var shape = _ref42.shape;
  376. var rx = shape.rx,
  377. ry = shape.ry;
  378. this.attr('shape', {
  379. rx: rx + movementX,
  380. ry: ry + movementY
  381. });
  382. }
  383. };
  384. exports.sector = sector;
  385. var regPolygon = {
  386. shape: {
  387. rx: 0,
  388. ry: 0,
  389. r: 0,
  390. side: 0
  391. },
  392. validator: function validator(_ref43) {
  393. var shape = _ref43.shape;
  394. var side = shape.side;
  395. var keys = ['rx', 'ry', 'r', 'side'];
  396. if (keys.find(function (key) {
  397. return typeof shape[key] !== 'number';
  398. })) {
  399. console.error('RegPolygon shape configuration is abnormal!');
  400. return false;
  401. }
  402. if (side < 3) {
  403. console.error('RegPolygon at least trigon!');
  404. return false;
  405. }
  406. return true;
  407. },
  408. draw: function draw(_ref44, _ref45) {
  409. var ctx = _ref44.ctx;
  410. var shape = _ref45.shape,
  411. cache = _ref45.cache;
  412. ctx.beginPath();
  413. var rx = shape.rx,
  414. ry = shape.ry,
  415. r = shape.r,
  416. side = shape.side;
  417. if (!cache.points || cache.rx !== rx || cache.ry !== ry || cache.r !== r || cache.side !== side) {
  418. var _points = (0, _util.getRegularPolygonPoints)(rx, ry, r, side);
  419. Object.assign(cache, {
  420. points: _points,
  421. rx: rx,
  422. ry: ry,
  423. r: r,
  424. side: side
  425. });
  426. }
  427. var points = cache.points;
  428. (0, _canvas.drawPolylinePath)(ctx, points);
  429. ctx.closePath();
  430. ctx.stroke();
  431. ctx.fill();
  432. },
  433. hoverCheck: function hoverCheck(position, _ref46) {
  434. var cache = _ref46.cache;
  435. var points = cache.points;
  436. return (0, _util.checkPointIsInPolygon)(position, points);
  437. },
  438. setGraphCenter: function setGraphCenter(e, _ref47) {
  439. var shape = _ref47.shape,
  440. style = _ref47.style;
  441. var rx = shape.rx,
  442. ry = shape.ry;
  443. style.graphCenter = [rx, ry];
  444. },
  445. move: function move(_ref48, _ref49) {
  446. var movementX = _ref48.movementX,
  447. movementY = _ref48.movementY;
  448. var shape = _ref49.shape,
  449. cache = _ref49.cache;
  450. var rx = shape.rx,
  451. ry = shape.ry;
  452. cache.rx += movementX;
  453. cache.ry += movementY;
  454. this.attr('shape', {
  455. rx: rx + movementX,
  456. ry: ry + movementY
  457. });
  458. cache.points = cache.points.map(function (_ref50) {
  459. var _ref51 = (0, _slicedToArray2["default"])(_ref50, 2),
  460. x = _ref51[0],
  461. y = _ref51[1];
  462. return [x + movementX, y + movementY];
  463. });
  464. }
  465. };
  466. exports.regPolygon = regPolygon;
  467. var polyline = {
  468. shape: {
  469. points: [],
  470. close: false
  471. },
  472. validator: function validator(_ref52) {
  473. var shape = _ref52.shape;
  474. var points = shape.points;
  475. if (!(points instanceof Array)) {
  476. console.error('Polyline points should be an array!');
  477. return false;
  478. }
  479. return true;
  480. },
  481. draw: function draw(_ref53, _ref54) {
  482. var ctx = _ref53.ctx;
  483. var shape = _ref54.shape,
  484. lineWidth = _ref54.style.lineWidth;
  485. ctx.beginPath();
  486. var points = shape.points,
  487. close = shape.close;
  488. if (lineWidth === 1) points = (0, _util.eliminateBlur)(points);
  489. (0, _canvas.drawPolylinePath)(ctx, points);
  490. if (close) {
  491. ctx.closePath();
  492. ctx.fill();
  493. ctx.stroke();
  494. } else {
  495. ctx.stroke();
  496. }
  497. },
  498. hoverCheck: function hoverCheck(position, _ref55) {
  499. var shape = _ref55.shape,
  500. style = _ref55.style;
  501. var points = shape.points,
  502. close = shape.close;
  503. var lineWidth = style.lineWidth;
  504. if (close) {
  505. return (0, _util.checkPointIsInPolygon)(position, points);
  506. } else {
  507. return (0, _util.checkPointIsNearPolyline)(position, points, lineWidth);
  508. }
  509. },
  510. setGraphCenter: function setGraphCenter(e, _ref56) {
  511. var shape = _ref56.shape,
  512. style = _ref56.style;
  513. var points = shape.points;
  514. style.graphCenter = points[0];
  515. },
  516. move: function move(_ref57, _ref58) {
  517. var movementX = _ref57.movementX,
  518. movementY = _ref57.movementY;
  519. var shape = _ref58.shape;
  520. var points = shape.points;
  521. var moveAfterPoints = points.map(function (_ref59) {
  522. var _ref60 = (0, _slicedToArray2["default"])(_ref59, 2),
  523. x = _ref60[0],
  524. y = _ref60[1];
  525. return [x + movementX, y + movementY];
  526. });
  527. this.attr('shape', {
  528. points: moveAfterPoints
  529. });
  530. }
  531. };
  532. exports.polyline = polyline;
  533. var smoothline = {
  534. shape: {
  535. points: [],
  536. close: false
  537. },
  538. validator: function validator(_ref61) {
  539. var shape = _ref61.shape;
  540. var points = shape.points;
  541. if (!(points instanceof Array)) {
  542. console.error('Smoothline points should be an array!');
  543. return false;
  544. }
  545. return true;
  546. },
  547. draw: function draw(_ref62, _ref63) {
  548. var ctx = _ref62.ctx;
  549. var shape = _ref63.shape,
  550. cache = _ref63.cache;
  551. var points = shape.points,
  552. close = shape.close;
  553. if (!cache.points || cache.points.toString() !== points.toString()) {
  554. var _bezierCurve = polylineToBezierCurve(points, close);
  555. var hoverPoints = bezierCurveToPolyline(_bezierCurve);
  556. Object.assign(cache, {
  557. points: (0, _util.deepClone)(points, true),
  558. bezierCurve: _bezierCurve,
  559. hoverPoints: hoverPoints
  560. });
  561. }
  562. var bezierCurve = cache.bezierCurve;
  563. ctx.beginPath();
  564. (0, _canvas.drawBezierCurvePath)(ctx, bezierCurve.slice(1), bezierCurve[0]);
  565. if (close) {
  566. ctx.closePath();
  567. ctx.fill();
  568. ctx.stroke();
  569. } else {
  570. ctx.stroke();
  571. }
  572. },
  573. hoverCheck: function hoverCheck(position, _ref64) {
  574. var cache = _ref64.cache,
  575. shape = _ref64.shape,
  576. style = _ref64.style;
  577. var hoverPoints = cache.hoverPoints;
  578. var close = shape.close;
  579. var lineWidth = style.lineWidth;
  580. if (close) {
  581. return (0, _util.checkPointIsInPolygon)(position, hoverPoints);
  582. } else {
  583. return (0, _util.checkPointIsNearPolyline)(position, hoverPoints, lineWidth);
  584. }
  585. },
  586. setGraphCenter: function setGraphCenter(e, _ref65) {
  587. var shape = _ref65.shape,
  588. style = _ref65.style;
  589. var points = shape.points;
  590. style.graphCenter = points[0];
  591. },
  592. move: function move(_ref66, _ref67) {
  593. var movementX = _ref66.movementX,
  594. movementY = _ref66.movementY;
  595. var shape = _ref67.shape,
  596. cache = _ref67.cache;
  597. var points = shape.points;
  598. var moveAfterPoints = points.map(function (_ref68) {
  599. var _ref69 = (0, _slicedToArray2["default"])(_ref68, 2),
  600. x = _ref69[0],
  601. y = _ref69[1];
  602. return [x + movementX, y + movementY];
  603. });
  604. cache.points = moveAfterPoints;
  605. var _cache$bezierCurve$ = (0, _slicedToArray2["default"])(cache.bezierCurve[0], 2),
  606. fx = _cache$bezierCurve$[0],
  607. fy = _cache$bezierCurve$[1];
  608. var curves = cache.bezierCurve.slice(1);
  609. cache.bezierCurve = [[fx + movementX, fy + movementY]].concat((0, _toConsumableArray2["default"])(curves.map(function (curve) {
  610. return curve.map(function (_ref70) {
  611. var _ref71 = (0, _slicedToArray2["default"])(_ref70, 2),
  612. x = _ref71[0],
  613. y = _ref71[1];
  614. return [x + movementX, y + movementY];
  615. });
  616. })));
  617. cache.hoverPoints = cache.hoverPoints.map(function (_ref72) {
  618. var _ref73 = (0, _slicedToArray2["default"])(_ref72, 2),
  619. x = _ref73[0],
  620. y = _ref73[1];
  621. return [x + movementX, y + movementY];
  622. });
  623. this.attr('shape', {
  624. points: moveAfterPoints
  625. });
  626. }
  627. };
  628. exports.smoothline = smoothline;
  629. var bezierCurve = {
  630. shape: {
  631. points: [],
  632. close: false
  633. },
  634. validator: function validator(_ref74) {
  635. var shape = _ref74.shape;
  636. var points = shape.points;
  637. if (!(points instanceof Array)) {
  638. console.error('BezierCurve points should be an array!');
  639. return false;
  640. }
  641. return true;
  642. },
  643. draw: function draw(_ref75, _ref76) {
  644. var ctx = _ref75.ctx;
  645. var shape = _ref76.shape,
  646. cache = _ref76.cache;
  647. var points = shape.points,
  648. close = shape.close;
  649. if (!cache.points || cache.points.toString() !== points.toString()) {
  650. var hoverPoints = bezierCurveToPolyline(points, 20);
  651. Object.assign(cache, {
  652. points: (0, _util.deepClone)(points, true),
  653. hoverPoints: hoverPoints
  654. });
  655. }
  656. ctx.beginPath();
  657. (0, _canvas.drawBezierCurvePath)(ctx, points.slice(1), points[0]);
  658. if (close) {
  659. ctx.closePath();
  660. ctx.fill();
  661. ctx.stroke();
  662. } else {
  663. ctx.stroke();
  664. }
  665. },
  666. hoverCheck: function hoverCheck(position, _ref77) {
  667. var cache = _ref77.cache,
  668. shape = _ref77.shape,
  669. style = _ref77.style;
  670. var hoverPoints = cache.hoverPoints;
  671. var close = shape.close;
  672. var lineWidth = style.lineWidth;
  673. if (close) {
  674. return (0, _util.checkPointIsInPolygon)(position, hoverPoints);
  675. } else {
  676. return (0, _util.checkPointIsNearPolyline)(position, hoverPoints, lineWidth);
  677. }
  678. },
  679. setGraphCenter: function setGraphCenter(e, _ref78) {
  680. var shape = _ref78.shape,
  681. style = _ref78.style;
  682. var points = shape.points;
  683. style.graphCenter = points[0];
  684. },
  685. move: function move(_ref79, _ref80) {
  686. var movementX = _ref79.movementX,
  687. movementY = _ref79.movementY;
  688. var shape = _ref80.shape,
  689. cache = _ref80.cache;
  690. var points = shape.points;
  691. var _points$ = (0, _slicedToArray2["default"])(points[0], 2),
  692. fx = _points$[0],
  693. fy = _points$[1];
  694. var curves = points.slice(1);
  695. var bezierCurve = [[fx + movementX, fy + movementY]].concat((0, _toConsumableArray2["default"])(curves.map(function (curve) {
  696. return curve.map(function (_ref81) {
  697. var _ref82 = (0, _slicedToArray2["default"])(_ref81, 2),
  698. x = _ref82[0],
  699. y = _ref82[1];
  700. return [x + movementX, y + movementY];
  701. });
  702. })));
  703. cache.points = bezierCurve;
  704. cache.hoverPoints = cache.hoverPoints.map(function (_ref83) {
  705. var _ref84 = (0, _slicedToArray2["default"])(_ref83, 2),
  706. x = _ref84[0],
  707. y = _ref84[1];
  708. return [x + movementX, y + movementY];
  709. });
  710. this.attr('shape', {
  711. points: bezierCurve
  712. });
  713. }
  714. };
  715. exports.bezierCurve = bezierCurve;
  716. var text = {
  717. shape: {
  718. content: '',
  719. position: [],
  720. maxWidth: undefined,
  721. rowGap: 0
  722. },
  723. validator: function validator(_ref85) {
  724. var shape = _ref85.shape;
  725. var content = shape.content,
  726. position = shape.position,
  727. rowGap = shape.rowGap;
  728. if (typeof content !== 'string') {
  729. console.error('Text content should be a string!');
  730. return false;
  731. }
  732. if (!(position instanceof Array)) {
  733. console.error('Text position should be an array!');
  734. return false;
  735. }
  736. if (typeof rowGap !== 'number') {
  737. console.error('Text rowGap should be a number!');
  738. return false;
  739. }
  740. return true;
  741. },
  742. draw: function draw(_ref86, _ref87) {
  743. var ctx = _ref86.ctx;
  744. var shape = _ref87.shape;
  745. var content = shape.content,
  746. position = shape.position,
  747. maxWidth = shape.maxWidth,
  748. rowGap = shape.rowGap;
  749. var textBaseline = ctx.textBaseline,
  750. font = ctx.font;
  751. var fontSize = parseInt(font.replace(/\D/g, ''));
  752. var _position = position,
  753. _position2 = (0, _slicedToArray2["default"])(_position, 2),
  754. x = _position2[0],
  755. y = _position2[1];
  756. content = content.split('\n');
  757. var rowNum = content.length;
  758. var lineHeight = fontSize + rowGap;
  759. var allHeight = rowNum * lineHeight - rowGap;
  760. var offset = 0;
  761. if (textBaseline === 'middle') {
  762. offset = allHeight / 2;
  763. y += fontSize / 2;
  764. }
  765. if (textBaseline === 'bottom') {
  766. offset = allHeight;
  767. y += fontSize;
  768. }
  769. position = new Array(rowNum).fill(0).map(function (foo, i) {
  770. return [x, y + i * lineHeight - offset];
  771. });
  772. ctx.beginPath();
  773. content.forEach(function (text, i) {
  774. ctx.fillText.apply(ctx, [text].concat((0, _toConsumableArray2["default"])(position[i]), [maxWidth]));
  775. ctx.strokeText.apply(ctx, [text].concat((0, _toConsumableArray2["default"])(position[i]), [maxWidth]));
  776. });
  777. ctx.closePath();
  778. },
  779. hoverCheck: function hoverCheck(position, _ref88) {
  780. var shape = _ref88.shape,
  781. style = _ref88.style;
  782. return false;
  783. },
  784. setGraphCenter: function setGraphCenter(e, _ref89) {
  785. var shape = _ref89.shape,
  786. style = _ref89.style;
  787. var position = shape.position;
  788. style.graphCenter = (0, _toConsumableArray2["default"])(position);
  789. },
  790. move: function move(_ref90, _ref91) {
  791. var movementX = _ref90.movementX,
  792. movementY = _ref90.movementY;
  793. var shape = _ref91.shape;
  794. var _shape$position = (0, _slicedToArray2["default"])(shape.position, 2),
  795. x = _shape$position[0],
  796. y = _shape$position[1];
  797. this.attr('shape', {
  798. position: [x + movementX, y + movementY]
  799. });
  800. }
  801. };
  802. exports.text = text;
  803. var graphs = new Map([['circle', circle], ['ellipse', ellipse], ['rect', rect], ['ring', ring], ['arc', arc], ['sector', sector], ['regPolygon', regPolygon], ['polyline', polyline], ['smoothline', smoothline], ['bezierCurve', bezierCurve], ['text', text]]);
  804. var _default = graphs;
  805. /**
  806. * @description Extend new graph
  807. * @param {String} name Name of Graph
  808. * @param {Object} config Configuration of Graph
  809. * @return {Undefined} Void
  810. */
  811. exports["default"] = _default;
  812. function extendNewGraph(name, config) {
  813. if (!name || !config) {
  814. console.error('ExtendNewGraph Missing Parameters!');
  815. return;
  816. }
  817. if (!config.shape) {
  818. console.error('Required attribute of shape to extendNewGraph!');
  819. return;
  820. }
  821. if (!config.validator) {
  822. console.error('Required function of validator to extendNewGraph!');
  823. return;
  824. }
  825. if (!config.draw) {
  826. console.error('Required function of draw to extendNewGraph!');
  827. return;
  828. }
  829. graphs.set(name, config);
  830. }