graphs.js 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044
  1. import beziercurve from '../bezier-curve'
  2. import {
  3. calcLength,
  4. getPointOnQuadraticBezier,
  5. getTensionPointsClosed,
  6. expandPoints
  7. } from '../bezier-curve/core/path2d'
  8. import {
  9. deepClone,
  10. eliminateBlur,
  11. checkPointIsInCircle,
  12. getTwoPointDistance,
  13. checkPointIsInSector,
  14. getRegularPolygonPoints,
  15. checkPointIsInPolygon,
  16. checkPointIsNearPolyline,
  17. checkPointIsInRect
  18. } from '../plugin/util'
  19. import {
  20. drawPolylinePath,
  21. drawBezierCurvePath
  22. } from '../plugin/canvas'
  23. const {
  24. polylineToBezierCurve,
  25. bezierCurveToPolyline
  26. } = beziercurve
  27. export const circle = {
  28. shape: {
  29. rx: 0,
  30. ry: 0,
  31. r: 0
  32. },
  33. validator({
  34. shape
  35. }) {
  36. const {
  37. rx,
  38. ry,
  39. r
  40. } = shape
  41. if (typeof rx !== 'number' || typeof ry !== 'number' || typeof r !== 'number') {
  42. console.error('Circle shape configuration is abnormal!')
  43. return false
  44. }
  45. return true
  46. },
  47. draw({
  48. ctx
  49. }, {
  50. shape
  51. }) {
  52. ctx.beginPath()
  53. const {
  54. rx,
  55. ry,
  56. r
  57. } = shape
  58. ctx.arc(rx, ry, r > 0 ? r : 0.01, 0, Math.PI * 2)
  59. ctx.fill()
  60. ctx.stroke()
  61. ctx.closePath()
  62. // ctx.draw()
  63. },
  64. hoverCheck(position, {
  65. shape
  66. }) {
  67. const {
  68. rx,
  69. ry,
  70. r
  71. } = shape
  72. return checkPointIsInCircle(position, rx, ry, r)
  73. },
  74. setGraphCenter(e, {
  75. shape,
  76. style
  77. }) {
  78. const {
  79. rx,
  80. ry
  81. } = shape
  82. style.graphCenter = [rx, ry]
  83. },
  84. move({
  85. movementX,
  86. movementY
  87. }, {
  88. shape
  89. }) {
  90. this.attr('shape', {
  91. rx: shape.rx + movementX,
  92. ry: shape.ry + movementY
  93. })
  94. }
  95. }
  96. export const ellipse = {
  97. shape: {
  98. rx: 0,
  99. ry: 0,
  100. hr: 0,
  101. vr: 0
  102. },
  103. validator({
  104. shape
  105. }) {
  106. const {
  107. rx,
  108. ry,
  109. hr,
  110. vr
  111. } = shape
  112. if (typeof rx !== 'number' || typeof ry !== 'number' || typeof hr !== 'number' || typeof vr !== 'number') {
  113. console.error('Ellipse shape configuration is abnormal!')
  114. return false
  115. }
  116. return true
  117. },
  118. draw({
  119. ctx
  120. }, {
  121. shape
  122. }) {
  123. ctx.beginPath()
  124. let {
  125. rx,
  126. ry,
  127. hr,
  128. vr
  129. } = shape
  130. ctx.ellipse(rx, ry, hr > 0 ? hr : 0.01, vr > 0 ? vr : 0.01, 0, 0, Math.PI * 2)
  131. ctx.fill()
  132. ctx.stroke()
  133. ctx.closePath()
  134. // ctx.draw()
  135. },
  136. hoverCheck(position, {
  137. shape
  138. }) {
  139. const {
  140. rx,
  141. ry,
  142. hr,
  143. vr
  144. } = shape
  145. const a = Math.max(hr, vr)
  146. const b = Math.min(hr, vr)
  147. const c = Math.sqrt(a * a - b * b)
  148. const leftFocusPoint = [rx - c, ry]
  149. const rightFocusPoint = [rx + c, ry]
  150. const distance = getTwoPointDistance(position, leftFocusPoint) + getTwoPointDistance(position,
  151. rightFocusPoint)
  152. return distance <= 2 * a
  153. },
  154. setGraphCenter(e, {
  155. shape,
  156. style
  157. }) {
  158. const {
  159. rx,
  160. ry
  161. } = shape
  162. style.graphCenter = [rx, ry]
  163. },
  164. move({
  165. movementX,
  166. movementY
  167. }, {
  168. shape
  169. }) {
  170. this.attr('shape', {
  171. rx: shape.rx + movementX,
  172. ry: shape.ry + movementY
  173. })
  174. }
  175. }
  176. export const rect = {
  177. shape: {
  178. x: 0,
  179. y: 0,
  180. w: 0,
  181. h: 0
  182. },
  183. validator({
  184. shape
  185. }) {
  186. const {
  187. x,
  188. y,
  189. w,
  190. h
  191. } = shape
  192. if (typeof x !== 'number' || typeof y !== 'number' || typeof w !== 'number' || typeof h !== 'number') {
  193. console.error('Rect shape configuration is abnormal!')
  194. return false
  195. }
  196. return true
  197. },
  198. draw({
  199. ctx
  200. }, {
  201. shape
  202. }) {
  203. ctx.beginPath()
  204. let {
  205. x,
  206. y,
  207. w,
  208. h
  209. } = shape
  210. ctx.rect(x, y, w, h)
  211. ctx.fill()
  212. ctx.stroke()
  213. ctx.closePath()
  214. // ctx.draw()
  215. },
  216. hoverCheck(position, {
  217. shape
  218. }) {
  219. let {
  220. x,
  221. y,
  222. w,
  223. h
  224. } = shape
  225. return checkPointIsInRect(position, x, y, w, h)
  226. },
  227. setGraphCenter(e, {
  228. shape,
  229. style
  230. }) {
  231. const {
  232. x,
  233. y,
  234. w,
  235. h
  236. } = shape
  237. style.graphCenter = [x + w / 2, y + h / 2]
  238. },
  239. move({
  240. movementX,
  241. movementY
  242. }, {
  243. shape
  244. }) {
  245. this.attr('shape', {
  246. x: shape.x + movementX,
  247. y: shape.y + movementY
  248. })
  249. }
  250. }
  251. export const rectRound = {
  252. shape: {
  253. x: 0,
  254. y: 0,
  255. w: 0,
  256. h: 0,
  257. radius:[0,0,0,0],
  258. close:false,//true时为填充,否则描边。
  259. },
  260. validator({
  261. shape
  262. }) {
  263. const {
  264. x,
  265. y,
  266. w,
  267. h
  268. } = shape
  269. if (typeof x !== 'number' || typeof y !== 'number' || typeof w !== 'number' || typeof h !== 'number') {
  270. console.error('Rect shape configuration is abnormal!')
  271. return false
  272. }
  273. return true
  274. },
  275. draw({
  276. ctx
  277. }, {
  278. shape
  279. }) {
  280. ctx.beginPath()
  281. let {
  282. x,
  283. y,
  284. w,
  285. h,
  286. radius,
  287. close
  288. } = shape
  289. if(typeof radius ==='number'){
  290. radius = [radius,radius,radius,radius]
  291. }
  292. const cxt = ctx;
  293. let width = w,height = h;
  294. var r0 = radius[0],r1 = radius[1],r2 = radius[2],r3 = radius[3];
  295. cxt.beginPath();
  296. //从右下角顺时针绘制,弧度从0到1/2PI
  297. cxt.arc(width - r0+x, height - r0+y, r0, 0, Math.PI / 2);
  298. //矩形下边线
  299. cxt.lineTo(r1+x, height+y);
  300. //左下角圆弧,弧度从1/2PI到PI
  301. cxt.arc(r1+x, height - r1+y, r1, Math.PI / 2, Math.PI);
  302. //矩形左边线
  303. cxt.lineTo(x, r2+y);
  304. //左上角圆弧,弧度从PI到3/2PI
  305. cxt.arc(r2+x, r2+y, r2, Math.PI, Math.PI * 3 / 2);
  306. //上边线
  307. cxt.lineTo(width - r3+x, y);
  308. //右上角圆弧
  309. cxt.arc(width - r3+x, r3+y, r3, Math.PI * 3 / 2, Math.PI * 2);
  310. //右边线
  311. cxt.lineTo(width+x, height+y-r0);
  312. ctx.stroke()
  313. if(close){
  314. ctx.fill()
  315. }
  316. cxt.closePath();
  317. },
  318. hoverCheck(position, {
  319. shape,style,
  320. }) {
  321. let {
  322. x,
  323. y,
  324. w,
  325. h
  326. } = shape
  327. let {lineWidth} = style;
  328. return checkPointIsInRect(position, x-lineWidth, y-lineWidth, w+lineWidth, h+lineWidth)
  329. },
  330. setGraphCenter(e, {
  331. shape,
  332. style
  333. }) {
  334. const {
  335. x,
  336. y,
  337. w,
  338. h
  339. } = shape
  340. style.graphCenter = [x + w / 2, y + h / 2]
  341. },
  342. move({
  343. movementX,
  344. movementY
  345. }, {
  346. shape
  347. }) {
  348. this.attr('shape', {
  349. x: shape.x + movementX,
  350. y: shape.y + movementY
  351. })
  352. }
  353. }
  354. export const ring = {
  355. shape: {
  356. rx: 0,
  357. ry: 0,
  358. r: 0
  359. },
  360. validator({
  361. shape
  362. }) {
  363. const {
  364. rx,
  365. ry,
  366. r
  367. } = shape
  368. if (typeof rx !== 'number' || typeof ry !== 'number' || typeof r !== 'number') {
  369. console.error('Ring shape configuration is abnormal!')
  370. return false
  371. }
  372. return true
  373. },
  374. draw({
  375. ctx
  376. }, {
  377. shape
  378. }) {
  379. ctx.beginPath()
  380. const {
  381. rx,
  382. ry,
  383. r
  384. } = shape
  385. ctx.arc(rx, ry, r > 0 ? r : 0.01, 0, Math.PI * 2)
  386. ctx.stroke()
  387. ctx.closePath()
  388. // ctx.draw()
  389. },
  390. hoverCheck(position, {
  391. shape,
  392. style
  393. }) {
  394. const {
  395. rx,
  396. ry,
  397. r
  398. } = shape
  399. const {
  400. lineWidth
  401. } = style
  402. const halfLineWidth = lineWidth / 2
  403. const minDistance = r - halfLineWidth
  404. const maxDistance = r + halfLineWidth
  405. const distance = getTwoPointDistance(position, [rx, ry])
  406. return (distance >= minDistance && distance <= maxDistance)
  407. },
  408. setGraphCenter(e, {
  409. shape,
  410. style
  411. }) {
  412. const {
  413. rx,
  414. ry
  415. } = shape
  416. style.graphCenter = [rx, ry]
  417. },
  418. move({
  419. movementX,
  420. movementY
  421. }, {
  422. shape
  423. }) {
  424. this.attr('shape', {
  425. rx: shape.rx + movementX,
  426. ry: shape.ry + movementY
  427. })
  428. }
  429. }
  430. export const arc = {
  431. shape: {
  432. rx: 0,
  433. ry: 0,
  434. r: 0,
  435. startAngle: 0,
  436. endAngle: 0,
  437. clockWise: true
  438. },
  439. validator({
  440. shape
  441. }) {
  442. const keys = ['rx', 'ry', 'r', 'startAngle', 'endAngle']
  443. if (keys.find(key => typeof shape[key] !== 'number')) {
  444. console.error('Arc shape configuration is abnormal!')
  445. return false
  446. }
  447. return true
  448. },
  449. draw({
  450. ctx
  451. }, {
  452. shape
  453. }) {
  454. ctx.beginPath()
  455. const {
  456. rx,
  457. ry,
  458. r,
  459. startAngle,
  460. endAngle,
  461. clockWise
  462. } = shape
  463. ctx.arc(rx, ry, r > 0 ? r : 0.001, startAngle, endAngle, !clockWise)
  464. ctx.stroke()
  465. ctx.closePath()
  466. // ctx.draw(true)
  467. },
  468. hoverCheck(position, {
  469. shape,
  470. style
  471. }) {
  472. const {
  473. rx,
  474. ry,
  475. r,
  476. startAngle,
  477. endAngle,
  478. clockWise
  479. } = shape
  480. const {
  481. lineWidth
  482. } = style
  483. const halfLineWidth = lineWidth / 2
  484. const insideRadius = r - halfLineWidth
  485. const outsideRadius = r + halfLineWidth
  486. return !checkPointIsInSector(position, rx, ry, insideRadius, startAngle, endAngle, clockWise) &&
  487. checkPointIsInSector(position, rx, ry, outsideRadius, startAngle, endAngle, clockWise)
  488. },
  489. setGraphCenter(e, {
  490. shape,
  491. style
  492. }) {
  493. const {
  494. rx,
  495. ry
  496. } = shape
  497. style.graphCenter = [rx, ry]
  498. },
  499. move({
  500. movementX,
  501. movementY
  502. }, {
  503. shape
  504. }) {
  505. this.attr('shape', {
  506. rx: shape.rx + movementX,
  507. ry: shape.ry + movementY
  508. })
  509. }
  510. }
  511. export const sector = {
  512. shape: {
  513. rx: 0,
  514. ry: 0,
  515. r: 0,
  516. startAngle: 0,
  517. endAngle: 0,
  518. clockWise: true
  519. },
  520. validator({
  521. shape
  522. }) {
  523. const keys = ['rx', 'ry', 'r', 'startAngle', 'endAngle']
  524. if (keys.find(key => typeof shape[key] !== 'number')) {
  525. console.error('Sector shape configuration is abnormal!')
  526. return false
  527. }
  528. return true
  529. },
  530. draw({
  531. ctx
  532. }, {
  533. shape
  534. }) {
  535. ctx.beginPath()
  536. const {
  537. rx,
  538. ry,
  539. r,
  540. startAngle,
  541. endAngle,
  542. clockWise
  543. } = shape
  544. ctx.arc(rx, ry, r > 0 ? r : 0.01, startAngle, endAngle, !clockWise)
  545. ctx.lineTo(rx, ry)
  546. ctx.closePath()
  547. ctx.stroke()
  548. ctx.fill()
  549. // ctx.draw()
  550. },
  551. hoverCheck(position, {
  552. shape
  553. }) {
  554. const {
  555. rx,
  556. ry,
  557. r,
  558. startAngle,
  559. endAngle,
  560. clockWise
  561. } = shape
  562. return checkPointIsInSector(position, rx, ry, r, startAngle, endAngle, clockWise)
  563. },
  564. setGraphCenter(e, {
  565. shape,
  566. style
  567. }) {
  568. const {
  569. rx,
  570. ry
  571. } = shape
  572. style.graphCenter = [rx, ry]
  573. },
  574. move({
  575. movementX,
  576. movementY
  577. }, {
  578. shape
  579. }) {
  580. const {
  581. rx,
  582. ry
  583. } = shape
  584. this.attr('shape', {
  585. rx: rx + movementX,
  586. ry: ry + movementY
  587. })
  588. }
  589. }
  590. export const regPolygon = {
  591. shape: {
  592. rx: 0,
  593. ry: 0,
  594. r: 0,
  595. side: 0
  596. },
  597. validator({
  598. shape
  599. }) {
  600. const {
  601. side
  602. } = shape
  603. const keys = ['rx', 'ry', 'r', 'side']
  604. if (keys.find(key => typeof shape[key] !== 'number')) {
  605. console.error('RegPolygon shape configuration is abnormal!')
  606. return false
  607. }
  608. if (side < 3) {
  609. console.error('RegPolygon at least trigon!')
  610. return false
  611. }
  612. return true
  613. },
  614. draw({
  615. ctx
  616. }, {
  617. shape,
  618. cache
  619. }) {
  620. ctx.beginPath()
  621. const {
  622. rx,
  623. ry,
  624. r,
  625. side
  626. } = shape
  627. if (!cache.points || cache.rx !== rx || cache.ry !== ry || cache.r !== r || cache.side !== side) {
  628. const points = getRegularPolygonPoints(rx, ry, r, side)
  629. Object.assign(cache, {
  630. points,
  631. rx,
  632. ry,
  633. r,
  634. side
  635. })
  636. }
  637. const {
  638. points
  639. } = cache
  640. drawPolylinePath(ctx, points)
  641. ctx.closePath()
  642. ctx.stroke()
  643. ctx.fill()
  644. // ctx.draw()
  645. },
  646. hoverCheck(position, {
  647. cache
  648. }) {
  649. let {
  650. points
  651. } = cache
  652. return checkPointIsInPolygon(position, points)
  653. },
  654. setGraphCenter(e, {
  655. shape,
  656. style
  657. }) {
  658. const {
  659. rx,
  660. ry
  661. } = shape
  662. style.graphCenter = [rx, ry]
  663. },
  664. move({
  665. movementX,
  666. movementY
  667. }, {
  668. shape,
  669. cache
  670. }) {
  671. const {
  672. rx,
  673. ry
  674. } = shape
  675. cache.rx += movementX
  676. cache.ry += movementY
  677. this.attr('shape', {
  678. rx: rx + movementX,
  679. ry: ry + movementY
  680. })
  681. cache.points = cache.points.map(([x, y]) => [x + movementX, y + movementY])
  682. }
  683. }
  684. export const polyline = {
  685. shape: {
  686. points: [],
  687. close: false
  688. },
  689. validator({
  690. shape
  691. }) {
  692. const {
  693. points
  694. } = shape
  695. if (!(points instanceof Array)) {
  696. console.error('Polyline points should be an array!')
  697. return false
  698. }
  699. return true
  700. },
  701. draw({
  702. ctx
  703. }, {
  704. shape,
  705. style: {
  706. lineWidth
  707. }
  708. }) {
  709. ctx.beginPath()
  710. let {
  711. points,
  712. close
  713. } = shape
  714. if (lineWidth === 1) points = eliminateBlur(points)
  715. drawPolylinePath(ctx, points)
  716. if (close) {
  717. ctx.closePath()
  718. ctx.fill()
  719. ctx.stroke()
  720. } else {
  721. ctx.stroke()
  722. }
  723. // ctx.draw()
  724. },
  725. hoverCheck(position, {
  726. shape,
  727. style
  728. }) {
  729. const {
  730. points,
  731. close
  732. } = shape
  733. const {
  734. lineWidth
  735. } = style
  736. if (close) {
  737. return checkPointIsInPolygon(position, points)
  738. } else {
  739. return checkPointIsNearPolyline(position, points, lineWidth)
  740. }
  741. },
  742. setGraphCenter(e, {
  743. shape,
  744. style
  745. }) {
  746. const {
  747. points
  748. } = shape
  749. style.graphCenter = points[0]
  750. },
  751. move({
  752. movementX,
  753. movementY
  754. }, {
  755. shape
  756. }) {
  757. const {
  758. points
  759. } = shape
  760. const moveAfterPoints = points.map(([x, y]) => [x + movementX, y + movementY])
  761. this.attr('shape', {
  762. points: moveAfterPoints
  763. })
  764. }
  765. }
  766. export const smoothline = {
  767. shape: {
  768. points: [],
  769. close: false
  770. },
  771. validator({
  772. shape
  773. }) {
  774. const {
  775. points
  776. } = shape
  777. if (!(points instanceof Array)) {
  778. console.error('Smoothline points should be an array!')
  779. return false
  780. }
  781. return true
  782. },
  783. draw({
  784. ctx
  785. }, {
  786. shape,
  787. cache
  788. }) {
  789. const {
  790. points,
  791. close
  792. } = shape
  793. if (!cache.points || cache.points.toString() !== points.toString()) {
  794. const bezierCurve = polylineToBezierCurve(points, close)
  795. const hoverPoints = bezierCurveToPolyline(bezierCurve)
  796. Object.assign(cache, {
  797. points: deepClone(points, true),
  798. bezierCurve,
  799. hoverPoints
  800. })
  801. }
  802. const {
  803. bezierCurve
  804. } = cache
  805. ctx.beginPath()
  806. drawBezierCurvePath(ctx, bezierCurve.slice(1), bezierCurve[0])
  807. if (close) {
  808. ctx.closePath()
  809. ctx.fill()
  810. ctx.stroke()
  811. } else {
  812. ctx.stroke()
  813. }
  814. // ctx.draw()
  815. },
  816. hoverCheck(position, {
  817. cache,
  818. shape,
  819. style
  820. }) {
  821. const {
  822. hoverPoints
  823. } = cache
  824. const {
  825. close
  826. } = shape
  827. const {
  828. lineWidth
  829. } = style
  830. if (close) {
  831. return checkPointIsInPolygon(position, hoverPoints)
  832. } else {
  833. return checkPointIsNearPolyline(position, hoverPoints, lineWidth)
  834. }
  835. },
  836. setGraphCenter(e, {
  837. shape,
  838. style
  839. }) {
  840. const {
  841. points
  842. } = shape
  843. style.graphCenter = points[0]
  844. },
  845. move({
  846. movementX,
  847. movementY
  848. }, {
  849. shape,
  850. cache
  851. }) {
  852. const {
  853. points
  854. } = shape
  855. const moveAfterPoints = points.map(([x, y]) => [x + movementX, y + movementY])
  856. cache.points = moveAfterPoints
  857. const [fx, fy] = cache.bezierCurve[0]
  858. const curves = cache.bezierCurve.slice(1)
  859. cache.bezierCurve = [
  860. [fx + movementX, fy + movementY],
  861. ...curves.map(curve => curve.map(([x, y]) => [x + movementX, y + movementY]))
  862. ]
  863. cache.hoverPoints = cache.hoverPoints.map(([x, y]) => [x + movementX, y + movementY])
  864. this.attr('shape', {
  865. points: moveAfterPoints
  866. })
  867. }
  868. }
  869. export const bezierCurve = {
  870. shape: {
  871. points: [],
  872. close: false
  873. },
  874. validator({
  875. shape
  876. }) {
  877. const {
  878. points
  879. } = shape
  880. if (!(points instanceof Array)) {
  881. console.error('BezierCurve points should be an array!')
  882. return false
  883. }
  884. return true
  885. },
  886. draw({
  887. ctx
  888. }, {
  889. shape,
  890. cache
  891. }) {
  892. let {
  893. points,
  894. close
  895. } = shape
  896. if (!cache.points || cache.points.toString() !== points.toString()) {
  897. const hoverPoints = bezierCurveToPolyline(points, 20)
  898. Object.assign(cache, {
  899. points: deepClone(points, true),
  900. hoverPoints
  901. })
  902. }
  903. ctx.beginPath()
  904. drawBezierCurvePath(ctx, points.slice(1), points[0])
  905. if (close) {
  906. ctx.closePath()
  907. ctx.fill()
  908. ctx.stroke()
  909. } else {
  910. ctx.stroke()
  911. }
  912. // ctx.draw()
  913. },
  914. hoverCheck(position, {
  915. cache,
  916. shape,
  917. style
  918. }) {
  919. const {
  920. hoverPoints
  921. } = cache
  922. const {
  923. close
  924. } = shape
  925. const {
  926. lineWidth
  927. } = style
  928. if (close) {
  929. return checkPointIsInPolygon(position, hoverPoints)
  930. } else {
  931. return checkPointIsNearPolyline(position, hoverPoints, lineWidth)
  932. }
  933. },
  934. setGraphCenter(e, {
  935. shape,
  936. style
  937. }) {
  938. const {
  939. points
  940. } = shape
  941. style.graphCenter = points[0]
  942. },
  943. move({
  944. movementX,
  945. movementY
  946. }, {
  947. shape,
  948. cache
  949. }) {
  950. const {
  951. points
  952. } = shape
  953. const [fx, fy] = points[0]
  954. const curves = points.slice(1)
  955. const bezierCurve = [
  956. [fx + movementX, fy + movementY],
  957. ...curves.map(curve => curve.map(([x, y]) => [x + movementX, y + movementY]))
  958. ]
  959. cache.points = bezierCurve
  960. cache.hoverPoints = cache.hoverPoints.map(([x, y]) => [x + movementX, y + movementY])
  961. this.attr('shape', {
  962. points: bezierCurve
  963. })
  964. }
  965. }
  966. export const text = {
  967. shape: {
  968. content: '',
  969. position: [],
  970. x: 0,
  971. y: 0,
  972. rowGap: 0
  973. },
  974. validator({
  975. shape,
  976. style
  977. }, ctx) {
  978. const {
  979. content,
  980. position,
  981. rowGap
  982. } = shape
  983. if (typeof content !== 'string') {
  984. console.error('Text content should be a string!')
  985. return false
  986. }
  987. if (!(position instanceof Array)) {
  988. console.error('Text position should be an array!')
  989. return false
  990. }
  991. if (typeof rowGap !== 'number') {
  992. console.error('Text rowGap should be a number!')
  993. return false
  994. }
  995. this.textWidth = 0
  996. this.textHeight = 0
  997. return true
  998. },
  999. measureSize(ctx, text) {
  1000. var _context = ctx,
  1001. fontSize = this.fontSize(),
  1002. metrics;
  1003. _context.save();
  1004. _context.font = ctx.font;
  1005. metrics = _context.measureText(text + "");
  1006. _context.restore();
  1007. return {
  1008. width: metrics.width,
  1009. height: fontSize,
  1010. };
  1011. },
  1012. draw({
  1013. ctx,
  1014. area
  1015. }, {
  1016. shape,
  1017. style
  1018. }) {
  1019. let {
  1020. content,
  1021. position,
  1022. maxWidth,
  1023. rowGap,
  1024. } = shape
  1025. let {
  1026. lineWidth
  1027. } = style;
  1028. const {
  1029. textBaseline,
  1030. font,
  1031. } = ctx
  1032. const [w, h] = area
  1033. const fontSize = parseInt(font.replace(/\D/g, ''))
  1034. let [x, y] = position
  1035. content = content.split('\n')
  1036. const rowNum = content.length
  1037. const lineHeight = fontSize + rowGap
  1038. const allHeight = rowNum * lineHeight - rowGap
  1039. let offset = 0
  1040. if (textBaseline === 'middle') {
  1041. offset = allHeight / 2
  1042. y += fontSize / 2
  1043. }
  1044. if (textBaseline === 'bottom') {
  1045. offset = allHeight
  1046. y += fontSize
  1047. }
  1048. position = new Array(rowNum).fill(0).map((foo, i) => [x, y + i * lineHeight - offset])
  1049. if (typeof maxWidth == 'undefined' || !maxWidth) maxWidth = w
  1050. // #ifdef H5
  1051. maxWidth = maxWidth * ctx.dpr
  1052. // #endif
  1053. let maxwi = []
  1054. ctx.beginPath()
  1055. content.forEach((text, i) => {
  1056. ctx.fillText(text, ...position[i], maxWidth)
  1057. if (lineWidth > 0) {
  1058. ctx.strokeText(text, ...position[i], maxWidth)
  1059. }
  1060. maxwi.push(ctx.measureText(text + "").width)
  1061. })
  1062. ctx.closePath()
  1063. this.textWidth = Math.max(...maxwi)
  1064. this.textHeight = allHeight
  1065. },
  1066. hoverCheck(position, {
  1067. cache,
  1068. shape,
  1069. style
  1070. }) {
  1071. const [x, y] = shape.position;
  1072. const {
  1073. textBaseline
  1074. } = style;
  1075. var w = this?.textWidth ?? 0;
  1076. var h = this?.textHeight ?? 0;
  1077. let isCheck = false;
  1078. if (textBaseline == 'top') {
  1079. if (position[0] >= x && position[0] <= x + w && position[1] >= y && position[1] <= y + h) {
  1080. isCheck = true;
  1081. }
  1082. } else if (textBaseline == 'bottom') {
  1083. if (position[0] >= x && position[0] <= x + w && position[1] >= y + h && position[1] <= y + h * 2) {
  1084. isCheck = true;
  1085. }
  1086. } else if (textBaseline == 'middle') {
  1087. if (position[0] >= x && position[0] <= x + w && position[1] >= y - h / 2 && position[1] <= y + h - h /
  1088. 2) {
  1089. isCheck = true;
  1090. }
  1091. }
  1092. return isCheck;
  1093. },
  1094. setGraphCenter(e, {
  1095. shape,
  1096. style
  1097. }) {
  1098. const {
  1099. position
  1100. } = shape
  1101. style.graphCenter = [...position]
  1102. },
  1103. move({
  1104. movementX,
  1105. movementY
  1106. }, {
  1107. shape
  1108. }) {
  1109. const {
  1110. position: [x, y]
  1111. } = shape
  1112. this.attr('shape', {
  1113. position: [x + movementX, y + movementY]
  1114. })
  1115. }
  1116. }
  1117. export const path = {
  1118. shape: {
  1119. points: [],
  1120. close: false
  1121. },
  1122. validator({
  1123. shape
  1124. }) {
  1125. const {
  1126. points
  1127. } = shape
  1128. if (!(points instanceof Array)) {
  1129. console.error('Polyline points should be an array!')
  1130. return false
  1131. }
  1132. return true
  1133. },
  1134. draw({
  1135. ctx
  1136. }, {
  1137. shape,
  1138. style: {
  1139. lineWidth
  1140. }
  1141. }) {
  1142. ctx.beginPath()
  1143. let {
  1144. points,
  1145. close
  1146. } = shape
  1147. if (lineWidth === 1) points = eliminateBlur(points)
  1148. drawPolylinePath(ctx, points)
  1149. if (close) {
  1150. ctx.closePath()
  1151. ctx.fill()
  1152. ctx.stroke()
  1153. } else {
  1154. ctx.stroke()
  1155. }
  1156. ctx.draw()
  1157. },
  1158. hoverCheck(position, {
  1159. shape,
  1160. style
  1161. }) {
  1162. const {
  1163. points,
  1164. close
  1165. } = shape
  1166. const {
  1167. lineWidth
  1168. } = style
  1169. if (close) {
  1170. return checkPointIsInPolygon(position, points)
  1171. } else {
  1172. return checkPointIsNearPolyline(position, points, lineWidth)
  1173. }
  1174. },
  1175. setGraphCenter(e, {
  1176. shape,
  1177. style
  1178. }) {
  1179. const {
  1180. points
  1181. } = shape
  1182. style.graphCenter = points[0]
  1183. },
  1184. move({
  1185. movementX,
  1186. movementY
  1187. }, {
  1188. shape
  1189. }) {
  1190. const {
  1191. points
  1192. } = shape
  1193. const moveAfterPoints = points.map(([x, y]) => [x + movementX, y + movementY])
  1194. this.attr('shape', {
  1195. points: moveAfterPoints
  1196. })
  1197. }
  1198. }
  1199. export const image = {
  1200. shape: {
  1201. x: 0,
  1202. y: 0,
  1203. w: 0,
  1204. h: 0,
  1205. sx: 0,
  1206. sy: 0,
  1207. src: ''
  1208. },
  1209. validator({
  1210. shape
  1211. }) {
  1212. const {
  1213. x,
  1214. y,
  1215. w,
  1216. h,
  1217. src
  1218. } = shape
  1219. if (typeof x !== 'number' || typeof y !== 'number' || typeof w !== 'number' || typeof h !== 'number' || !
  1220. src) {
  1221. console.error('image x,y,w,h,src必填。')
  1222. return false
  1223. }
  1224. return true
  1225. },
  1226. draw({
  1227. ctx,
  1228. cav
  1229. }, {
  1230. shape
  1231. }) {
  1232. let {
  1233. x,
  1234. y,
  1235. w,
  1236. h,
  1237. sx,
  1238. sy,
  1239. src
  1240. } = shape
  1241. let t = this;
  1242. // #ifdef MP-WEIXIN
  1243. //1加载中,2加载完成,3从未加载。
  1244. if (typeof this['isLoad'] == 'undefined' || this['isLoad'] == 3) {
  1245. this['isLoad'] = 1;
  1246. const bg = cav.createImage()
  1247. bg.onload = () => {
  1248. setTimeout(function() {
  1249. console.warn('tm-render:图片加载完成')
  1250. ctx.drawImage(bg, x, y, w, h)
  1251. t['isLoad'] = 2;
  1252. shape.src = bg
  1253. if(t?.load){
  1254. t.load();
  1255. }
  1256. }, 400)
  1257. }
  1258. bg.onerror = () => t['isLoad'] = 3
  1259. bg.src = src;
  1260. }
  1261. if (this['isLoad'] == 2) {
  1262. ctx.drawImage(src, x, y, w, h)
  1263. } else {
  1264. console.log('image loadding...');
  1265. }
  1266. // #endif
  1267. // #ifndef MP-WEIXIN
  1268. if(typeof this['isLoad'] =='undefined') this['isLoad'] = 3;
  1269. if(this['isLoad']===3){
  1270. this['isLoad'] = 1;
  1271. setTimeout(()=>{
  1272. t['isLoad'] = 2;
  1273. if(this?.load){
  1274. this.load();
  1275. }
  1276. },1200)
  1277. }
  1278. ctx.drawImage(src, x, y, w, h, sx, sy)
  1279. // #endif
  1280. },
  1281. hoverCheck(position, {
  1282. shape
  1283. }) {
  1284. let {
  1285. x,
  1286. y,
  1287. w,
  1288. h
  1289. } = shape
  1290. return checkPointIsInRect(position, x, y, w, h)
  1291. },
  1292. setGraphCenter(e, {
  1293. shape,
  1294. style
  1295. }) {
  1296. const {
  1297. x,
  1298. y,
  1299. w,
  1300. h
  1301. } = shape
  1302. style.graphCenter = [x + w / 2, y + h / 2]
  1303. },
  1304. move({
  1305. movementX,
  1306. movementY
  1307. }, {
  1308. shape
  1309. }) {
  1310. this.attr('shape', {
  1311. x: shape.x + movementX,
  1312. y: shape.y + movementY
  1313. })
  1314. }
  1315. }
  1316. export const star = {
  1317. shape: {
  1318. points: [],
  1319. close: false,
  1320. x: 0,
  1321. y: 0,
  1322. numPoints: 5, //星星的角数量
  1323. innerRadius: 40, //内部凹进去的比例
  1324. outerRadius: 70, //角向外凸出的比例。
  1325. },
  1326. validator({
  1327. shape
  1328. }) {
  1329. const {
  1330. points,
  1331. x,
  1332. y
  1333. } = shape
  1334. if (typeof x !== 'number' || typeof y !== 'number') {
  1335. console.error('Polyline points should be an array!')
  1336. return false
  1337. }
  1338. return true
  1339. },
  1340. draw({
  1341. ctx
  1342. }, {
  1343. shape,
  1344. style: {
  1345. lineWidth
  1346. }
  1347. }) {
  1348. let context = ctx;
  1349. let {
  1350. points,
  1351. close,
  1352. x,
  1353. y,
  1354. numPoints,
  1355. innerRadius,
  1356. outerRadius
  1357. } = shape;
  1358. context.beginPath();
  1359. context.moveTo(x, y - outerRadius);
  1360. points.push([x, y - outerRadius])
  1361. for (var n = 1; n <= numPoints * 2; n++) {
  1362. var radius = n % 2 === 0 ? outerRadius : innerRadius;
  1363. var x2 = radius * Math.sin((n * Math.PI) / numPoints);
  1364. var y2 = -1 * radius * Math.cos((n * Math.PI) / numPoints);
  1365. context.lineTo(x2 + x, y2 + y);
  1366. points.push([x2 + x, y2 + y])
  1367. }
  1368. this.shape.points = points;
  1369. if (lineWidth === 1) points = eliminateBlur(points)
  1370. if (close) {
  1371. ctx.closePath()
  1372. ctx.fill()
  1373. ctx.stroke()
  1374. } else {
  1375. ctx.stroke()
  1376. }
  1377. // ctx.draw()
  1378. },
  1379. hoverCheck(position, {
  1380. shape,
  1381. style
  1382. }) {
  1383. const {
  1384. points,
  1385. close
  1386. } = shape
  1387. const {
  1388. lineWidth
  1389. } = style
  1390. if (close) {
  1391. return checkPointIsInPolygon(position, points)
  1392. } else {
  1393. return checkPointIsNearPolyline(position, points, lineWidth)
  1394. }
  1395. },
  1396. setGraphCenter(e, {
  1397. shape,
  1398. style
  1399. }) {
  1400. const {
  1401. points
  1402. } = shape
  1403. style.graphCenter = points[0]
  1404. },
  1405. move({
  1406. movementX,
  1407. movementY
  1408. }, {
  1409. shape
  1410. }) {
  1411. const {
  1412. points
  1413. } = shape
  1414. const moveAfterPoints = points.map(([x, y]) => [x + movementX, y + movementY])
  1415. this.attr('shape', {
  1416. points: moveAfterPoints
  1417. })
  1418. }
  1419. }
  1420. export const arrow = {
  1421. shape: {
  1422. points: [],
  1423. close: true,
  1424. x: 0,
  1425. y: 0,
  1426. tension: 0, //弯曲程度。
  1427. pointerLength: 0, //箭头指针长度。
  1428. pointerWidth: 0, //箭头指针宽度。
  1429. pointerAtBeginning: false, //我们需要在两边画指针吗?默认值为 false。
  1430. pointerAtEnding: true, //结束端显示箭头。
  1431. hitPoints: [], //检测命中点。
  1432. },
  1433. validator({
  1434. shape
  1435. }) {
  1436. const {
  1437. points,
  1438. x,
  1439. y,
  1440. close,
  1441. tension,
  1442. pointerLength,
  1443. pointerWidth,
  1444. pointerAtBeginning,
  1445. pointerAtEnding
  1446. } = shape
  1447. if (typeof x !== 'number' || typeof y !== 'number') {
  1448. console.error('Polyline points should be an array!')
  1449. return false
  1450. }
  1451. return true
  1452. },
  1453. draw({
  1454. ctx
  1455. }, {
  1456. shape,
  1457. style: {
  1458. lineWidth
  1459. }
  1460. }) {
  1461. let context = ctx;
  1462. let {
  1463. points,
  1464. x,
  1465. y,
  1466. close,
  1467. tension,
  1468. pointerLength,
  1469. pointerWidth,
  1470. pointerAtBeginning,
  1471. pointerAtEnding
  1472. } = shape
  1473. let old_x = points[2] - points[0]
  1474. let old_y = points[3] - points[1]
  1475. points[0] = this.shape.x
  1476. points[1] = this.shape.y
  1477. points[2] = this.shape.x + old_x
  1478. points[3] = this.shape.y + old_y
  1479. var PI2 = Math.PI * 2;
  1480. var tp = points;
  1481. var fromTension = tension !== 0 && points.length > 4;
  1482. if (fromTension) {
  1483. if (close) {
  1484. tp = getTensionPointsClosed(points, tension);
  1485. } else {
  1486. tp = expandPoints(points, tension);
  1487. }
  1488. console.log(tp);
  1489. }
  1490. var length = pointerLength;
  1491. var n = points.length;
  1492. var dx, dy;
  1493. if (fromTension) {
  1494. const lp = [
  1495. tp[tp.length - 4],
  1496. tp[tp.length - 3],
  1497. tp[tp.length - 2],
  1498. tp[tp.length - 1],
  1499. points[n - 2],
  1500. points[n - 1],
  1501. ];
  1502. const lastLength = calcLength(tp[tp.length - 4], tp[tp.length - 3], 'C', lp);
  1503. const previous = getPointOnQuadraticBezier(Math.min(1, 1 - length / lastLength), lp[0], lp[1], lp[2],
  1504. lp[3], lp[4], lp[5]);
  1505. dx = points[n - 2] - previous.x;
  1506. dy = points[n - 1] - previous.y;
  1507. } else {
  1508. dx = points[n - 2] - points[n - 4];
  1509. dy = points[n - 1] - points[n - 3];
  1510. }
  1511. var radians = (Math.atan2(dy, dx) + PI2) % PI2;
  1512. var width = pointerWidth;
  1513. this.shape.hitPoints = []
  1514. ctx.save();
  1515. ctx.beginPath();
  1516. ctx.moveTo(points[0], points[1]);
  1517. // #ifdef H5 || APP-VUE
  1518. ctx.lineTo(points[2], points[3]);
  1519. // #endif
  1520. // #ifdef MP
  1521. ctx.lineTo(points[2], points[3]);
  1522. // #endif
  1523. ctx.closePath();
  1524. if (pointerAtEnding) {
  1525. ctx.translate(points[n - 2], points[n - 1]);
  1526. ctx.rotate(radians);
  1527. // #ifdef H5 || APP-VUE
  1528. ctx.moveTo(points[2], points[3]);
  1529. ctx.lineTo(points[2], points[3] - width / 2);
  1530. ctx.lineTo(length + points[2], points[3]);
  1531. ctx.lineTo(points[2], width / 2 + points[3]);
  1532. // #endif
  1533. // #ifdef MP
  1534. ctx.moveTo(0, 0);
  1535. ctx.lineTo(-length, width / 2);
  1536. ctx.lineTo(-length, -width / 2);
  1537. // #endif
  1538. ctx.closePath();
  1539. ctx.restore();
  1540. this.shape.hitPoints.push([points[2], points[3] - width / 2]);
  1541. this.shape.hitPoints.push([length + points[2], points[3]]);
  1542. this.shape.hitPoints.push([points[2], width / 2 + points[3]]);
  1543. }
  1544. if (pointerAtBeginning) {
  1545. if (pointerAtBeginning) {
  1546. ctx.save();
  1547. }
  1548. ctx.translate(x, y);
  1549. if (fromTension) {
  1550. dx = (tp[0] + tp[2]) / 2 - points[0];
  1551. dy = (tp[1] + tp[3]) / 2 - points[1];
  1552. } else {
  1553. dx = points[2] - points[0];
  1554. dy = points[3] - points[1];
  1555. }
  1556. ctx.rotate((Math.atan2(-dy, -dx) + PI2) % PI2);
  1557. // #ifdef H5 || APP-VUE
  1558. ctx.moveTo(points[0], points[1]);
  1559. ctx.lineTo(points[0], points[1] - width / 2);
  1560. ctx.lineTo(-length + points[0], points[1]);
  1561. ctx.lineTo(points[0], width / 2 + points[1]);
  1562. // #endif
  1563. // #ifdef MP
  1564. ctx.moveTo(0, 0);
  1565. ctx.lineTo(-length, width / 2);
  1566. ctx.lineTo(-length, -width / 2);
  1567. // #endif
  1568. ctx.closePath();
  1569. ctx.restore();
  1570. }
  1571. if (close) {
  1572. ctx.fill()
  1573. ctx.stroke()
  1574. } else {
  1575. ctx.stroke()
  1576. }
  1577. },
  1578. hoverCheck(position, {
  1579. shape,
  1580. style
  1581. }) {
  1582. const {
  1583. points,
  1584. hitPoints,
  1585. close,
  1586. pointerLength,
  1587. pointerWidth
  1588. } = shape
  1589. const {
  1590. lineWidth
  1591. } = style
  1592. // if (close) {
  1593. // console.log( checkPointIsInPolygon(position, hitPoints));
  1594. // return checkPointIsInPolygon(position, hitPoints)
  1595. // } else {
  1596. // return checkPointIsNearPolyline(position, hitPoints, lineWidth)
  1597. // }
  1598. // #ifdef H5 || APP-VUE
  1599. return checkPointIsInRect(position, points[2], points[3] - pointerWidth / 2, pointerLength, pointerWidth)
  1600. // #endif
  1601. // #ifdef MP
  1602. return checkPointIsInRect(position, points[2] - pointerLength, points[3] - pointerWidth / 2, pointerLength,
  1603. pointerWidth)
  1604. // #endif
  1605. },
  1606. setGraphCenter(e, {
  1607. shape,
  1608. style
  1609. }) {
  1610. const {
  1611. points
  1612. } = shape
  1613. style.graphCenter = points[0]
  1614. },
  1615. move({
  1616. movementX,
  1617. movementY
  1618. }, {
  1619. shape
  1620. }) {
  1621. const {
  1622. points
  1623. } = shape
  1624. const moveAfterPoints = points.map(([x, y]) => [x + movementX, y + movementY])
  1625. this.attr('shape', {
  1626. points: moveAfterPoints
  1627. })
  1628. }
  1629. }
  1630. const graphs = new Map([
  1631. ['rectRound', rectRound],
  1632. ['arrow', arrow],
  1633. ['star', star],
  1634. ['image', image],
  1635. ['path', path],
  1636. ['circle', circle],
  1637. ['ellipse', ellipse],
  1638. ['rect', rect],
  1639. ['ring', ring],
  1640. ['arc', arc],
  1641. ['sector', sector],
  1642. ['regPolygon', regPolygon],
  1643. ['polyline', polyline],
  1644. ['smoothline', smoothline],
  1645. ['bezierCurve', bezierCurve],
  1646. ['text', text]
  1647. ])
  1648. export default graphs
  1649. /**
  1650. * @description Extend new graph
  1651. * @param {String} name Name of Graph
  1652. * @param {Object} config Configuration of Graph
  1653. * @return {Undefined} Void
  1654. */
  1655. export function extendNewGraph(name, config) {
  1656. if (!name || !config) {
  1657. console.error('ExtendNewGraph Missing Parameters!')
  1658. return
  1659. }
  1660. if (!config.shape) {
  1661. console.error('Required attribute of shape to extendNewGraph!')
  1662. return
  1663. }
  1664. if (!config.validator) {
  1665. console.error('Required function of validator to extendNewGraph!')
  1666. return
  1667. }
  1668. if (!config.draw) {
  1669. console.error('Required function of draw to extendNewGraph!')
  1670. return
  1671. }
  1672. graphs.set(name, config)
  1673. }