1847d3696270f8e592d1a4f9aeae70c8da327eac74b5c1edc265b14a634d368f1bba4d31cba1d9cf3a4d90883465fbadae8878ea058a36b9df74e17ccbfb7b 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985
  1. [中文](./README.md)
  2. <h1 align="center">CRender</h1>
  3. <p align="center">
  4. <a href="https://travis-ci.com/jiaming743/CRender"><img src="https://img.shields.io/travis/com/jiaming743/CRender.svg" alt="Travis CI"></a>
  5. <a href="https://github.com/jiaming743/CRender/blob/master/LICENSE"><img src="https://img.shields.io/github/license/jiaming743/CRender.svg" alt="LICENSE" /> </a>
  6. <a href="https://www.npmjs.com/package/@jiaminghi/c-render"><img src="https://img.shields.io/npm/v/@jiaminghi/c-render.svg" alt="LICENSE" /> </a>
  7. </p>
  8. ### What is CRender?
  9. - It's a **vector** graphics rendering plugin based on **canvas**.
  10. - It provides **animation** and mouse **event** support for graphics.
  11. ### Install with npm
  12. ```shell
  13. $ npm install @jiaminghi/c-render
  14. ```
  15. ### Quick experience
  16. ```html
  17. <!--Resources are located on personal servers for experience and testing only, do not use in production environments-->
  18. <!--Debug version-->
  19. <script src="http://lib.jiaminghi.com/crender/crender.map.js"></script>
  20. <!--Compression version-->
  21. <script src="http://lib.jiaminghi.com/crender/crender.min.js"></script>
  22. <script>
  23. const { CRender, extendNewGraph } = window.CRender
  24. // do something
  25. </script>
  26. ```
  27. Detailed documents and examples can be viewed on the [HomePage](http://crender.jiaminghi.com/EN/).
  28. - [Use](#Use)
  29. - [Class CRender](#class-crender)
  30. - [Class Graph](#class-graph)
  31. - [Class Style](#class-style)
  32. - [Examples](#examples)
  33. - [Extend New Graph](#Extend-New-Graph)
  34. - [Related support](#Related-support)
  35. ------
  36. <h3 align="center">Use</h3>
  37. ```javascript
  38. import CRender from '@jiaminghi/c-redner'
  39. const canvas = document.getElementById('canvas')
  40. // Instantiate CRender
  41. const render = new CRender(canvas)
  42. // Add graph to render
  43. const circle = render.add({ name: 'circle', ... })
  44. ```
  45. <h3 align="center">Class CRender</h3>
  46. ### Class
  47. ```javascript
  48. /**
  49. * @description Class of CRender
  50. * @param {Object} canvas Canvas DOM
  51. * @return {CRender} Instance of CRender
  52. */
  53. class CRender {
  54. // ...
  55. }
  56. ```
  57. ### properties
  58. - [ctx](#ctx)
  59. - [area](#area)
  60. - [animationStatus](#animationStatus)
  61. - [graphs](#graphs)
  62. - [color](#color)
  63. - [bezierCurve](#bezierCurve)
  64. #### ctx
  65. ```javascript
  66. /**
  67. * @description Context of the canvas
  68. * @type {Object}
  69. * @example ctx = canvas.getContext('2d')
  70. */
  71. ```
  72. #### area
  73. ```javascript
  74. /**
  75. * @description Width and height of the canvas
  76. * @type {Array<Number>}
  77. * @example area = [300,100]
  78. */
  79. ```
  80. #### animationStatus
  81. ```javascript
  82. /**
  83. * @description Whether render is in animation rendering
  84. * @type {Boolean}
  85. * @example animationStatus = true|false
  86. */
  87. ```
  88. #### graphs
  89. ```javascript
  90. /**
  91. * @description Added graph
  92. * @type {Array<Graph>}
  93. * @example graphs = [Graph, Graph, ...]
  94. */
  95. ```
  96. #### [color](https://github.com/jiaming743/color/blob/master/README_EN.md)
  97. ```javascript
  98. /**
  99. * @description Color plugin
  100. * @type {Object}
  101. */
  102. ```
  103. #### [bezierCurve](https://github.com/jiaming743/BezierCurve/blob/master/README_EN.md)
  104. ```javascript
  105. /**
  106. * @description Bezier Curve plugin
  107. * @type {Object}
  108. */
  109. ```
  110. ### prototype
  111. - [add](#add)
  112. Add graph to render.
  113. - [clone](#clone)
  114. Clone Graph.
  115. - [delGraph](#delGraph)
  116. Delete graph in render.
  117. - [delAllGraph](#delAllGraph)
  118. Delete all graph in render.
  119. - [drawAllGraph](#drawAllGraph)
  120. Draw all the graphs in the render.
  121. - [clearArea](#clearArea)
  122. Clear canvas drawing area.
  123. - [launchAnimation](#launchAnimation)
  124. Animate the graph whose animation queue is not empty and the animationPause is equal to false.
  125. #### add
  126. ```javascript
  127. /**
  128. * @description Add graph to render
  129. * @param {Object} config Graph configuration
  130. * @return {Graph} Graph instance
  131. */
  132. CRender.prototype.add = function (config = {}) {
  133. // ...
  134. }
  135. ```
  136. #### Clone
  137. ```javascript
  138. /**
  139. * @description Clone Graph
  140. * @param {Graph} graph The target to be cloned
  141. * @return {Graph} Cloned graph
  142. */
  143. CRender.prototype.clone = function (graph) {
  144. }
  145. ```
  146. #### delGraph
  147. ```javascript
  148. /**
  149. * @description Delete graph in render
  150. * @param {Graph} graph The graph to be deleted
  151. * @return {Undefined} Void
  152. */
  153. CRender.prototype.delGraph = function (graph) {
  154. // ...
  155. }
  156. ```
  157. #### delAllGraph
  158. ```javascript
  159. /**
  160. * @description Delete all graph in render
  161. * @return {Undefined} Void
  162. */
  163. CRender.prototype.delAllGraph = function () {
  164. // ...
  165. }
  166. ```
  167. #### drawAllGraph
  168. ```javascript
  169. /**
  170. * @description Draw all the graphs in the render
  171. * @return {Undefined} Void
  172. */
  173. CRender.prototype.drawAllGraph = function () {
  174. // ...
  175. }
  176. ```
  177. #### clearArea
  178. ```javascript
  179. /**
  180. * @description Clear canvas drawing area
  181. * @return {Undefined} Void
  182. */
  183. CRender.prototype.clearArea = function () {
  184. // ...
  185. }
  186. ```
  187. #### launchAnimation
  188. ```javascript
  189. /**
  190. * @description Animate the graph whose animation queue is not empty
  191. * and the animationPause is equal to false
  192. * @return {Promise} Animation Promise
  193. */
  194. CRender.prototype.launchAnimation = function () {
  195. // ...
  196. }
  197. ```
  198. <h3 align="center">Class Graph</h3>
  199. ### properties
  200. **When adding a graph, you can configure them.**
  201. - [visible](#visible)
  202. - [shape](#shape)
  203. - [style](#style)
  204. - [drag](#drag)
  205. - [hover](#hover)
  206. - [index](#index)
  207. - [animationDelay](#animationDelay)
  208. - [animationFrame](#animationFrame)
  209. - [animationCurve](#animationCurve)
  210. - [animationPause](#animationPause)
  211. - [hoverRect](#hoverRect)
  212. - [mouseEnter](#mouseEnter)
  213. - [mouseOuter](#mouseOuter)
  214. - [click](#click)
  215. #### visible
  216. ```javascript
  217. /**
  218. * @description Weather to render graph
  219. * @type {Boolean}
  220. * @default visible = true
  221. */
  222. ```
  223. #### shape
  224. ```javascript
  225. /**
  226. * @description Graph shape data
  227. * @type {Object}
  228. */
  229. ```
  230. #### [style](#Class-Style)
  231. ```javascript
  232. /**
  233. * @description Graph style data (Instance of Style)
  234. * @type {Style}
  235. */
  236. ```
  237. #### drag
  238. ```javascript
  239. /**
  240. * @description Whether to enable drag
  241. * @type {Boolean}
  242. * @default drag = false
  243. */
  244. ```
  245. #### hover
  246. ```javascript
  247. /**
  248. * @description Whether to enable hover
  249. * @type {Boolean}
  250. * @default hover = false
  251. */
  252. ```
  253. #### index
  254. ```javascript
  255. /**
  256. * @description Graph rendering index
  257. * Give priority to index high graph in rendering
  258. * @type {Number}
  259. * @default index = 1
  260. */
  261. ```
  262. #### animationDelay
  263. ```javascript
  264. /**
  265. * @description Animation delay time(ms)
  266. * @type {Number}
  267. * @default animationDelay = 0
  268. */
  269. ```
  270. #### animationFrame
  271. ```javascript
  272. /**
  273. * @description Number of animation frames
  274. * @type {Number}
  275. * @default animationFrame = 30
  276. */
  277. ```
  278. #### [animationCurve](https://github.com/jiaming743/transition/blob/master/README_EN.md)
  279. ```javascript
  280. /**
  281. * @description Animation easing curve
  282. * @type {String}
  283. * @default animationCurve = 'linear'
  284. */
  285. ```
  286. #### animationPause
  287. ```javascript
  288. /**
  289. * @description Weather to pause graph animation
  290. * @type {Boolean}
  291. * @default animationPause = false
  292. */
  293. ```
  294. #### hoverRect
  295. ```javascript
  296. /**
  297. * @description Rectangular hover detection zone
  298. * Use this method for hover detection first
  299. * @type {Null|Array<Number>}
  300. * @default hoverRect = null
  301. * @example hoverRect = [0, 0, 100, 100] // [Rect start x, y, Rect width, height]
  302. */
  303. ```
  304. #### mouseEnter
  305. ```javascript
  306. /**
  307. * @description Mouse enter event handler
  308. * @type {Null|Function}
  309. * @default mouseEnter = null
  310. */
  311. ```
  312. #### mouseOuter
  313. ```javascript
  314. /**
  315. * @description Mouse outer event handler
  316. * @type {Null|Function}
  317. * @default mouseOuter = null
  318. */
  319. ```
  320. #### click
  321. ```javascript
  322. /**
  323. * @description Mouse click event handler
  324. * @type {Null|Function}
  325. * @default click = null
  326. */
  327. ```
  328. #### Tip
  329. Enable **mouseEnter**, **mouseOuter**, **click** event support requires configuring the `hover` property of the graph to `true`. Extended new graph require the **hoverCheck** method to be configured to provide event support.
  330. ### prototype
  331. - [attr](#attr)
  332. Update graph state.
  333. - [animation](#animation)
  334. Update graphics state (with animation).
  335. - [animationEnd](#animationEnd)
  336. Skip to the last frame of animation.
  337. - [pauseAnimation](#pauseAnimation)
  338. Pause animation behavior.
  339. - [playAnimation](#playAnimation)
  340. Try animation behavior.
  341. #### attr
  342. ```javascript
  343. /**
  344. * @description Update graph state
  345. * @param {String} attrName Updated attribute name
  346. * @param {Any} change Updated value
  347. * @return {Undefined} Void
  348. */
  349. Graph.prototype.attr = function (attrName, change = undefined) {
  350. // ...
  351. }
  352. ```
  353. #### animation
  354. ```javascript
  355. /**
  356. * @description Update graphics state (with animation)
  357. * Only shape and style attributes are supported
  358. * @param {String} attrName Updated attribute name
  359. * @param {Any} change Updated value
  360. * @param {Boolean} wait Whether to store the animation waiting
  361. * for the next animation request
  362. * @return {Promise} Animation Promise
  363. */
  364. Graph.prototype.animation = async function (attrName, change, wait = false) {
  365. // ...
  366. }
  367. ```
  368. #### animationEnd
  369. ```javascript
  370. /**
  371. * @description Skip to the last frame of animation
  372. * @return {Undefined} Void
  373. */
  374. Graph.prototype.animationEnd = function () {
  375. // ...
  376. }
  377. ```
  378. #### pauseAnimation
  379. ```javascript
  380. /**
  381. * @description Pause animation behavior
  382. * @return {Undefined} Void
  383. */
  384. Graph.prototype.pauseAnimation = function () {
  385. // ...
  386. }
  387. ```
  388. #### playAnimation
  389. ```javascript
  390. /**
  391. * @description Try animation behavior
  392. * @return {Undefined} Void
  393. */
  394. Graph.prototype.playAnimation = function () {
  395. // ...
  396. }
  397. ```
  398. ### Life Cycle
  399. When you add graph to the render, you can configure the following functions in the configuration, they will be called at a specific time.
  400. - [added](#added)
  401. Called after the graphics are added.
  402. - [beforeDraw](#beforeDraw)
  403. Called before the drawing is drawn.
  404. - [drawed](#drawed)
  405. Called after the drawing is completed
  406. - [beforeMove](#beforeMove)
  407. Called before moving the graphic.
  408. - [moved](#moved)
  409. Called after moving the graphic.
  410. - [beforeDelete](#beforeDelete)
  411. Called before deleting the graphic.
  412. - [deleted](#deleted)
  413. Called after the graphic is deleted.
  414. #### added
  415. ```javascript
  416. /**
  417. * @description Called after the graphics are added
  418. * @param {Graph} Graph instance
  419. */
  420. config = {
  421. //...,
  422. added ({ shape, style }) {
  423. // do something...
  424. }
  425. }
  426. ```
  427. #### beforeDraw
  428. ```javascript
  429. /**
  430. * @description Called before the drawing is drawn,
  431. * the graphic style has been initialized.
  432. * You can modify the ctx property before drawing.
  433. * @param {Graph} Graph instance
  434. * @param {CRender} CRender instance
  435. */
  436. config = {
  437. //...,
  438. beforeDraw ({ shape, style }, { ctx }) {
  439. // do something...
  440. ctx.stroke = 'transparent'
  441. }
  442. }
  443. ```
  444. #### drawed
  445. ```javascript
  446. /**
  447. * @description Called after the drawing is completed
  448. * @param {Graph} Graph instance
  449. * @param {CRender} CRender instance
  450. */
  451. config = {
  452. //...,
  453. drawed ({ shape, style }, { ctx }) {
  454. // do something...
  455. }
  456. }
  457. ```
  458. #### beforeMove
  459. ```javascript
  460. /**
  461. * @description Called before moving the graphic,
  462. * before the drag behavior occurs
  463. * @param {Event} Mouse event
  464. * @param {Graph} Graph instance
  465. */
  466. config = {
  467. //...,
  468. beforeMove ({ offsetX, offsetY }, { shape, style }) {
  469. // do something...
  470. }
  471. }
  472. ```
  473. #### moved
  474. ```javascript
  475. /**
  476. * @description Called after moving the graphic,
  477. * after the drag behavior occurs
  478. * @param {Event} Mouse event
  479. * @param {Graph} Graph instance
  480. */
  481. config = {
  482. //...,
  483. moved ({ offsetX, offsetY }, { shape, style }) {
  484. // do something...
  485. }
  486. }
  487. ```
  488. #### beforeDelete
  489. ```javascript
  490. /**
  491. * @description Called before deleting the graphic
  492. * @param {Graph} Graph instance
  493. */
  494. config = {
  495. //...,
  496. beforeDelete ({ shape, style }) {
  497. // do something...
  498. }
  499. }
  500. ```
  501. #### deleted
  502. ```javascript
  503. /**
  504. * @description Called after the graphic is deleted
  505. * @param {Graph} Graph instance
  506. */
  507. config = {
  508. //...,
  509. deleted ({ shape, style }) {
  510. // do something...
  511. }
  512. }
  513. ```
  514. <h3 align="center">Class Style</h3>
  515. ### properties
  516. - [fill](#fill)
  517. - [stroke](#stroke)
  518. - [opacity](#opacity)
  519. - [lineCap](#lineCap)
  520. - [lineJoin](#)
  521. - [lineDash](#lineDash)
  522. - [lineDashOffset](#lineDashOffset)
  523. - [shadowBlur](#shadowBlur)
  524. - [shadowColor](#shadowColor)
  525. - [shadowOffsetX](#shadowOffsetX)
  526. - [shadowOffsetY](#shadowOffsetY)
  527. - [lineWidth](#lineWidth)
  528. - [graphCenter](#graphCenter)
  529. - [scale](#scale)
  530. - [rotate](#rotate)
  531. - [translate](#translate)
  532. - [hoverCursor](#hoverCursor)
  533. - [fontStyle](#fontStyle)
  534. - [fontVarient](#fontVarient)
  535. - [fontWeight](#fontWeight)
  536. - [fontSize](#fontSize)
  537. - [fontFamily](#fontFamily)
  538. - [textAlign](#textAlign)
  539. - [textBaseline](#textBaseline)
  540. - [gradientColor](#gradientColor)
  541. - [gradientType](#gradientType)
  542. - [gradientParams](#gradientParams)
  543. - [gradientWith](#gradientWith)
  544. - [gradientStops](#gradientStops)
  545. - [colors](#colors)
  546. #### fill
  547. ```javascript
  548. /**
  549. * @description Rgba value of graph fill color
  550. * @type {Array<Number>}
  551. * @default fill = [0, 0, 0, 1]
  552. */
  553. ```
  554. #### stroke
  555. ```javascript
  556. /**
  557. * @description Rgba value of graph stroke color
  558. * @type {Array<Number>}
  559. * @default stroke = [0, 0, 0, 1]
  560. */
  561. ```
  562. #### opacity
  563. ```javascript
  564. /**
  565. * @description Opacity of graph
  566. * @type {Number}
  567. * @default opacity = 1
  568. */
  569. ```
  570. #### lineCap
  571. ```javascript
  572. /**
  573. * @description LineCap of Ctx
  574. * @type {Null|String}
  575. * @default lineCap = null
  576. * @example lineCap = 'butt'|'round'|'square'
  577. */
  578. ```
  579. #### lineJoin
  580. ```javascript
  581. /**
  582. * @description Linejoin of Ctx
  583. * @type {Null|String}
  584. * @default lineJoin = null
  585. * @example lineJoin = 'round'|'bevel'|'miter'
  586. */
  587. ```
  588. #### lineDash
  589. ```javascript
  590. /**
  591. * @description LineDash of Ctx
  592. * @type {Null|Array<Number>}
  593. * @default lineDash = null
  594. * @example lineDash = [10, 10]
  595. */
  596. ```
  597. #### lineDashOffset
  598. ```javascript
  599. /**
  600. * @description LineDashOffset of Ctx
  601. * @type {Null|Number}
  602. * @default lineDashOffset = null
  603. * @example lineDashOffset = 10
  604. */
  605. ```
  606. #### shadowBlur
  607. ```javascript
  608. /**
  609. * @description ShadowBlur of Ctx
  610. * @type {Number}
  611. * @default shadowBlur = 0
  612. */
  613. ```
  614. #### shadowColor
  615. ```javascript
  616. /**
  617. * @description Rgba value of graph shadow color
  618. * @type {Array<Number>}
  619. * @default shadowColor = [0, 0, 0, 0]
  620. */
  621. ```
  622. #### shadowOffsetX
  623. ```javascript
  624. /**
  625. * @description ShadowOffsetX of Ctx
  626. * @type {Number}
  627. * @default shadowOffsetX = 0
  628. */
  629. ```
  630. #### shadowOffsetY
  631. ```javascript
  632. /**
  633. * @description ShadowOffsetY of Ctx
  634. * @type {Number}
  635. * @default shadowOffsetY = 0
  636. */
  637. ```
  638. #### lineWidth
  639. ```javascript
  640. /**
  641. * @description LineWidth of Ctx
  642. * @type {Number}
  643. * @default lineWidth = 0
  644. */
  645. ```
  646. #### graphCenter
  647. ```javascript
  648. /**
  649. * @description Center point of the graph
  650. * @type {Null|Array<Number>}
  651. * @default graphCenter = null
  652. * @example graphCenter = [10, 10]
  653. */
  654. ```
  655. #### scale
  656. ```javascript
  657. /**
  658. * @description Graph scale
  659. * @type {Null|Array<Number>}
  660. * @default scale = null
  661. * @example scale = [1.5, 1.5]
  662. */
  663. ```
  664. #### rotate
  665. ```javascript
  666. /**
  667. * @description Graph rotation degree
  668. * @type {Null|Number}
  669. * @default rotate = null
  670. * @example rotate = 10
  671. */
  672. ```
  673. #### translate
  674. ```javascript
  675. /**
  676. * @description Graph translate distance
  677. * @type {Null|Array<Number>}
  678. * @default translate = null
  679. * @example translate = [10, 10]
  680. */
  681. ```
  682. #### hoverCursor
  683. ```javascript
  684. /**
  685. * @description Cursor status when hover
  686. * @type {String}
  687. * @default hoverCursor = 'pointer'
  688. * @example hoverCursor = 'default'|'pointer'|'auto'|'crosshair'|'move'|'wait'|...
  689. */
  690. ```
  691. #### fontStyle
  692. ```javascript
  693. /**
  694. * @description Font style of Ctx
  695. * @type {String}
  696. * @default fontStyle = 'normal'
  697. * @example fontStyle = 'normal'|'italic'|'oblique'
  698. */
  699. ```
  700. #### fontVarient
  701. ```javascript
  702. /**
  703. * @description Font varient of Ctx
  704. * @type {String}
  705. * @default fontVarient = 'normal'
  706. * @example fontVarient = 'normal'|'small-caps'
  707. */
  708. ```
  709. #### fontWeight
  710. ```javascript
  711. /**
  712. * @description Font weight of Ctx
  713. * @type {String|Number}
  714. * @default fontWeight = 'normal'
  715. * @example fontWeight = 'normal'|'bold'|'bolder'|'lighter'|Number
  716. */
  717. ```
  718. #### fontSize
  719. ```javascript
  720. /**
  721. * @description Font size of Ctx
  722. * @type {Number}
  723. * @default fontSize = 10
  724. */
  725. ```
  726. #### fontFamily
  727. ```javascript
  728. /**
  729. * @description Font family of Ctx
  730. * @type {String}
  731. * @default fontFamily = 'Arial'
  732. */
  733. ```
  734. #### textAlign
  735. ```javascript
  736. /**
  737. * @description TextAlign of Ctx
  738. * @type {String}
  739. * @default textAlign = 'center'
  740. * @example textAlign = 'start'|'end'|'left'|'right'|'center'
  741. */
  742. ```
  743. #### textBaseline
  744. ```javascript
  745. /**
  746. * @description TextBaseline of Ctx
  747. * @type {String}
  748. * @default textBaseline = 'middle'
  749. * @example textBaseline = 'top'|'bottom'|'middle'|'alphabetic'|'hanging'
  750. */
  751. ```
  752. #### gradientColor
  753. ```javascript
  754. /**
  755. * @description The color used to create the gradient
  756. * @type {Null|Array<String>}
  757. * @default gradientColor = null
  758. * @example gradientColor = ['#000', '#111', '#222']
  759. */
  760. ```
  761. #### gradientType
  762. ```javascript
  763. /**
  764. * @description Gradient type
  765. * @type {String}
  766. * @default gradientType = 'linear'
  767. * @example gradientType = 'linear' | 'radial'
  768. */
  769. ```
  770. #### gradientParams
  771. ```javascript
  772. /**
  773. * @description Gradient params
  774. * @type {Array<Number>}
  775. * @default gradientParams = null
  776. * @example gradientParams = [x0, y0, x1, y1] (Linear Gradient)
  777. * @example gradientParams = [x0, y0, r0, x1, y1, r1] (Radial Gradient)
  778. */
  779. ```
  780. #### gradientWith
  781. ```javascript
  782. /**
  783. * @description When to use gradients
  784. * @type {String}
  785. * @default gradientWith = 'stroke'
  786. * @example gradientWith = 'stroke' | 'fill'
  787. */
  788. ```
  789. #### gradientStops
  790. ```javascript
  791. /**
  792. * @description Gradient color stops
  793. * @type {String|Array<Number>}
  794. * @default gradientStops = 'auto'
  795. * @example gradientStops = 'auto' | [0, .2, .3, 1]
  796. */
  797. ```
  798. #### colors
  799. ```javascript
  800. /**
  801. * @description Extended color that supports animation transition
  802. * @type {Array<String>|Object}
  803. * @default colors = null
  804. * @example colors = ['#000', '#111', '#222']
  805. * @example colors = { a: '#000', b: '#111' }
  806. */
  807. ```
  808. #### Tip
  809. **Gradient** is automatically enabled when `gradientColor` and `gradientParams` are configured.
  810. ### prototype
  811. - [getStyle](#getStyle)
  812. Get the current style configuration.
  813. #### getStyle
  814. ```javascript
  815. /**
  816. * @description Get the current style configuration
  817. * @return {Object} Style configuration
  818. */
  819. Style.prototype.getStyle = function () {
  820. }
  821. ```
  822. <h3 align="center">Examples</h3>
  823. CRender provides some basic vector graph, examples are as follows.
  824. - [circle](#circle)
  825. - [ellipse](#ellipse)
  826. - [rect](#rect)
  827. - [ring](#ring)
  828. - [arc](#arc)
  829. - [sector](#sector)
  830. - [regPolygon](#regPolygon)
  831. - [polyline](#polyline)
  832. - [polyline (closed)](#polyline-closed)
  833. - [smoothline](#smoothline)
  834. - [smoothline (closed)](#smoothline-closed)
  835. - [bezierCurve](#bezierCurve)
  836. - [bezierCurve (closed)](#bezierCurve-closed)
  837. - [text](#text)
  838. #### circle
  839. ##### shape attribute
  840. | Attribute name | Type | Default | Annotation |
  841. | :------------: | :------: | :-----: | :-----------------------: |
  842. | rx | `Number` | `0` | Center x-axis coordinate. |
  843. | ry | `Number` | `0` | Center r-axis coordinate. |
  844. | r | `Number` | `0` | Circle radius. |
  845. ```javascript
  846. const { area: [w, h] } = render
  847. const circleConfig = {
  848. name: 'circle',
  849. animationCurve: 'easeOutBack',
  850. hover: true,
  851. drag: true,
  852. shape: {
  853. rx: w / 2,
  854. ry: h / 2,
  855. r: 50
  856. },
  857. style: {
  858. fill: '#9ce5f4',
  859. shadowBlur: 0,
  860. shadowColor: '#66eece',
  861. hoverCursor: 'pointer'
  862. },
  863. mouseEnter (e) {
  864. this.animation('shape', { r: 70 }, true)
  865. this.animation('style', { shadowBlur: 20 })
  866. },
  867. mouseOuter (e) {
  868. this.animation('shape', { r: 50 }, true)
  869. this.animation('style', { shadowBlur: 0 })
  870. }
  871. }
  872. const circle = render.add(circleConfig)
  873. ```
  874. #### ellipse
  875. ##### shape attribute
  876. | Attribute name | Type | Default | Annotation |
  877. | :------------: | :------: | :-----: | :-----------------------: |
  878. | rx | `Number` | `0` | Center x-axis coordinate. |
  879. | ry | `Number` | `0` | Center y-axis coordinate. |
  880. | hr | `Number` | `0` | Horizontal axis radius. |
  881. | vr | `Number` | `0` | Vertical axis radius. |
  882. ```javascript
  883. const { area: [w, h] } = render
  884. const ellipseConfig = {
  885. name: 'ellipse',
  886. animationCurve: 'easeOutBack',
  887. hover: true,
  888. drag: true,
  889. shape: {
  890. rx: w / 2,
  891. ry: h / 2,
  892. hr: 80,
  893. vr: 30
  894. },
  895. style: {
  896. fill: '#9ce5f4',
  897. shadowBlur: 0,
  898. shadowColor: '#66eece',
  899. scale: [1, 1],
  900. hoverCursor: 'pointer'
  901. },
  902. mouseEnter (e) {
  903. this.animation('style', { scale: [1.5, 1.5], shadowBlur: 20 })
  904. },
  905. mouseOuter (e) {
  906. this.animation('style', { scale: [1, 1], shadowBlur: 0 })
  907. }
  908. }
  909. const ellipse = render.add(ellipseConfig)
  910. ```
  911. #### rect
  912. ##### shape attribute
  913. | Attribute name | Type | Default | Annotation |
  914. | :------------: | :------: | :-----: | :-------------------------------------------------------: |
  915. | x | `Number` | `0` | The x coordinate of the top left corner of the rectangle. |
  916. | y | `Number` | `0` | The y coordinate of the top left corner of the rectangle. |
  917. | w | `Number` | `0` | Rectangle width. |
  918. | h | `Number` | `0` | Rectangle height. |
  919. ```javascript
  920. const { area: [w, h] } = render
  921. const rectConfig = {
  922. name: 'rect',
  923. animationCurve: 'easeOutBack',
  924. hover: true,
  925. drag: true,
  926. shape: {
  927. x: w / 2 - rectWidth / 2,
  928. y: h / 2 - rectHeight / 2,
  929. w: rectWidth,
  930. h: rectHeight
  931. },
  932. style: {
  933. fill: '#9ce5f4',
  934. shadowBlur: 0,
  935. shadowColor: '#66eece',
  936. hoverCursor: 'pointer',
  937. translate: [0, 0]
  938. },
  939. mouseEnter (e) {
  940. this.animation('shape', { w: 400 }, true)
  941. this.animation('style', { shadowBlur: 20, translate: [-100, 0] })
  942. },
  943. mouseOuter (e) {
  944. this.animation('shape', { w: 200 }, true)
  945. this.animation('style', { shadowBlur: 0, translate: [0, 0] })
  946. }
  947. }
  948. const rect = render.add(rectConfig)
  949. ```
  950. #### ring
  951. ##### shape attribute
  952. | Attribute name | Type | Default | Annotation |
  953. | :------------: | :------: | :-----: | :-----------------------: |
  954. | rx | `Number` | `0` | Center x-axis coordinate. |
  955. | ry | `Number` | `0` | Center y-axis coordinate. |
  956. | r | `Number` | `0` | Ring radius. |
  957. ```javascript
  958. const { area: [w, h] } = render
  959. const ringConfig = {
  960. name: 'ring',
  961. animationCurve: 'easeOutBack',
  962. hover: true,
  963. drag: true,
  964. shape: {
  965. rx: w / 2,
  966. ry: h / 2,
  967. r: 50
  968. },
  969. style: {
  970. stroke: '#9ce5f4',
  971. lineWidth: 20,
  972. hoverCursor: 'pointer',
  973. shadowBlur: 0,
  974. shadowColor: '#66eece'
  975. },
  976. mouseEnter (e) {
  977. this.animation('style', { shadowBlur: 20, lineWidth: 30 })
  978. },
  979. mouseOuter (e) {
  980. this.animation('style', { shadowBlur: 0, lineWidth: 20 })
  981. }
  982. }
  983. const ring = render.add(ringConfig)
  984. ```
  985. #### arc
  986. ##### shape attribute
  987. | Attribute name | Type | Default | Annotation |
  988. | :------------: | :-------: | :-----: | :-----------------------: |
  989. | rx | `Number` | `0` | Center x-axis coordinate. |
  990. | ry | `Number` | `0` | Center y-axis coordinate. |
  991. | r | `Number` | `0` | Arc radius. |
  992. | startAngle | `Number` | `0` | Arc start angle. |
  993. | endAngle | `Number` | `0` | Arc end angle. |
  994. | clockWise | `Boolean` | `true` | Clockwise |
  995. ```javascript
  996. const { area: [w, h] } = render
  997. const arcConfig = {
  998. name: 'arc',
  999. animationCurve: 'easeOutBack',
  1000. hover: true,
  1001. drag: true,
  1002. shape: {
  1003. rx: w / 2,
  1004. ry: h / 2,
  1005. r: 60,
  1006. startAngle: 0,
  1007. endAngle: Math.PI / 3
  1008. },
  1009. style: {
  1010. stroke: '#9ce5f4',
  1011. lineWidth: 20,
  1012. shadowBlur: 0,
  1013. rotate: 0,
  1014. shadowColor: '#66eece',
  1015. hoverCursor: 'pointer'
  1016. },
  1017. mouseEnter (e) {
  1018. this.animation('shape', { endAngle: Math.PI }, true)
  1019. this.animation('style', { shadowBlur: 20, rotate: -30, lineWidth: 30 })
  1020. },
  1021. mouseOuter (e) {
  1022. this.animation('shape', { endAngle: Math.PI / 3 }, true)
  1023. this.animation('style', { shadowBlur: 0, rotate: 0, lineWidth: 20 })
  1024. }
  1025. }
  1026. const arc = render.add(arcConfig)
  1027. ```
  1028. #### sector
  1029. ##### shape attribute
  1030. | Attribute name | Type | Default | Annotation |
  1031. | :------------: | :-------: | :-----: | :-----------------------: |
  1032. | rx | `Number` | `0` | Center x-axis coordinate. |
  1033. | ry | `Number` | `0` | Center y-axis coordinate. |
  1034. | r | `Number` | `0` | Sector radius. |
  1035. | startAngle | `Number` | `0` | Sector start angle. |
  1036. | endAngle | `Number` | `0` | Sector end angle. |
  1037. | clockWise | `Boolean` | `true` | Clockwise |
  1038. ```javascript
  1039. const { area: [w, h] } = render
  1040. const sectorConfig = {
  1041. name: 'sector',
  1042. animationCurve: 'easeOutBack',
  1043. hover: true,
  1044. drag: true,
  1045. shape: {
  1046. rx: w / 2,
  1047. ry: h / 2,
  1048. r: 60,
  1049. startAngle: 0,
  1050. endAngle: Math.PI / 3
  1051. },
  1052. style: {
  1053. fill: '#9ce5f4',
  1054. shadowBlur: 0,
  1055. rotate: 0,
  1056. shadowColor: '#66eece',
  1057. hoverCursor: 'pointer'
  1058. },
  1059. mouseEnter (e) {
  1060. this.animation('shape', { endAngle: Math.PI, r: 70 }, true)
  1061. this.animation('style', { shadowBlur: 20, rotate: -30, lineWidth: 30 })
  1062. },
  1063. mouseOuter (e) {
  1064. this.animation('shape', { endAngle: Math.PI / 3, r: 60 }, true)
  1065. this.animation('style', { shadowBlur: 0, rotate: 0, lineWidth: 20 })
  1066. }
  1067. }
  1068. const sector = render.add(sectorConfig)
  1069. ```
  1070. #### regPolygon
  1071. ##### shape attribute
  1072. | Attribute name | Type | Default | Annotation |
  1073. | :------------: | :------: | :-----: | :-----------------------: |
  1074. | rx | `Number` | `0` | Center x-axis coordinate. |
  1075. | ry | `Number` | `0` | Center y-axis coordinate. |
  1076. | r | `Number` | `0` | Circumradius. |
  1077. | side | `Number` | `0` | Edge number. |
  1078. ```javascript
  1079. const { area: [w, h] } = render
  1080. const regPolygonConfig = {
  1081. name: 'regPolygon',
  1082. animationCurve: 'easeOutBack',
  1083. hover: true,
  1084. drag: true,
  1085. shape: {
  1086. rx: w / 2,
  1087. ry: h / 2,
  1088. r: 60,
  1089. side: 6
  1090. },
  1091. style: {
  1092. fill: '#9ce5f4',
  1093. hoverCursor: 'pointer',
  1094. shadowBlur: 0,
  1095. rotate: 0,
  1096. shadowColor: '#66eece'
  1097. },
  1098. mouseEnter (e) {
  1099. this.animation('shape', { endAngle: Math.PI, r: 100 }, true)
  1100. this.animation('style', { shadowBlur: 20, rotate: 180 })
  1101. },
  1102. mouseOuter (e) {
  1103. this.animation('shape', { endAngle: Math.PI / 3, r: 60 }, true)
  1104. this.animation('style', { shadowBlur: 0, rotate: 0 })
  1105. }
  1106. }
  1107. const regPolygon = render.add(regPolygonConfig)
  1108. ```
  1109. #### polyline
  1110. ##### shape attribute
  1111. | Attribute name | Type | Default | Annotation |
  1112. | :------------: | :-------: | :-----: | :------------------------------------: |
  1113. | points | `Array` | `[]` | The points that makes up the polyline. |
  1114. | close | `Boolean` | `false` | Whether to close the polyline. |
  1115. ```javascript
  1116. const { area: [w, h] } = render
  1117. const top = h / 3
  1118. const bottom = h / 3 * 2
  1119. const gap = w / 10
  1120. const beginX = w / 2 - gap * 2
  1121. const points = new Array(5).fill('').map((t, i) =>
  1122. [beginX + gap * i, i % 2 === 0 ? top : bottom])
  1123. const polylineConfig = {
  1124. name: 'polyline',
  1125. animationCurve: 'easeOutBack',
  1126. hover: true,
  1127. drag: true,
  1128. shape: {
  1129. points
  1130. },
  1131. style: {
  1132. stroke: '#9ce5f4',
  1133. shadowBlur: 0,
  1134. lineWidth: 10,
  1135. shadowColor: '#66eece',
  1136. hoverCursor: 'pointer'
  1137. },
  1138. mouseEnter (e) {
  1139. this.animation('style', { lineWidth: 20, shadowBlur: 20 })
  1140. },
  1141. mouseOuter (e) {
  1142. this.animation('style', { lineWidth: 10, shadowBlur: 0 })
  1143. }
  1144. }
  1145. const polyline = render.add(polylineConfig)
  1146. ```
  1147. #### polyline (closed)
  1148. ```javascript
  1149. const { area: [w, h] } = render
  1150. const top = h / 3
  1151. const bottom = h / 3 * 2
  1152. const gap = w / 10
  1153. const beginX = w / 2 - gap * 2
  1154. const points = new Array(5).fill('').map((t, i) =>
  1155. [beginX + gap * i, i % 2 === 0 ? top : bottom])
  1156. points[2][1] += top * 1.3
  1157. const polylineClosedConfig = {
  1158. name: 'polyline',
  1159. animationCurve: 'easeOutBack',
  1160. hover: true,
  1161. drag: true,
  1162. shape: {
  1163. points,
  1164. close: true
  1165. },
  1166. style: {
  1167. fill: '#9ce5f4',
  1168. shadowBlur: 0,
  1169. lineWidth: 10,
  1170. shadowColor: '#66eece',
  1171. hoverCursor: 'pointer'
  1172. },
  1173. mouseEnter (e) {
  1174. this.animation('style', { shadowBlur: 20 }, true)
  1175. const pointsCloned = deepClone(this.shape.points)
  1176. pointsCloned[2][1] += top * 0.3
  1177. this.animation('shape', { points: pointsCloned })
  1178. },
  1179. mouseOuter (e) {
  1180. this.animation('style', { shadowBlur: 0 }, true)
  1181. const pointsCloned = deepClone(this.shape.points)
  1182. pointsCloned[2][1] -= top * 0.3
  1183. this.animation('shape', { points: pointsCloned })
  1184. }
  1185. }
  1186. const polylineClosed = render.add(polylineClosedConfig)
  1187. ```
  1188. #### smoothline
  1189. ##### shape attribute
  1190. | Attribute name | Type | Default | Annotation |
  1191. | :------------: | :-------: | :-----: | :--------------------------------------: |
  1192. | points | `Array` | `[]` | The points that makes up the smoothline. |
  1193. | close | `Boolean` | `false` | Whether to close the smoothline. |
  1194. ```javascript
  1195. const { area: [w, h] } = render
  1196. const top = h / 3
  1197. const bottom = h / 3 * 2
  1198. const gap = w / 10
  1199. const beginX = w / 2 - gap * 2
  1200. const points = new Array(5).fill('').map((t, i) =>
  1201. [beginX + gap * i, i % 2 === 0 ? top : bottom])
  1202. const smoothlineConfig = {
  1203. name: 'smoothline',
  1204. animationCurve: 'easeOutBack',
  1205. hover: true,
  1206. drag: true,
  1207. shape: {
  1208. points
  1209. },
  1210. style: {
  1211. stroke: '#9ce5f4',
  1212. shadowBlur: 0,
  1213. lineWidth: 10,
  1214. shadowColor: '#66eece',
  1215. hoverCursor: 'pointer'
  1216. },
  1217. mouseEnter (e) {
  1218. this.animation('style', { lineWidth: 20, shadowBlur: 20 })
  1219. },
  1220. mouseOuter (e) {
  1221. this.animation('style', { lineWidth: 10, shadowBlur: 0 })
  1222. }
  1223. }
  1224. const smoothline = render.add(smoothlineConfig)
  1225. ```
  1226. #### smoothline (closed)
  1227. ```javascript
  1228. import { getCircleRadianPoint } from '../../CRender/lib/util'
  1229. function getPoints (radius, centerPoint, pointNum) {
  1230. const PIDived = Math.PI * 2 / pointNum
  1231. const points = new Array(pointNum).fill('')
  1232. .map((foo, i) =>
  1233. getCircleRadianPoint(...centerPoint, radius, PIDived * i)
  1234. )
  1235. return points
  1236. }
  1237. const { area: [w, h] } = render
  1238. const radius = h / 3
  1239. const centerPoint = [w / 2, h / 2]
  1240. const smoothlineClosedConfig = {
  1241. name: 'smoothline',
  1242. animationCurve: 'easeOutBack',
  1243. hover: true,
  1244. drag: true,
  1245. shape: {
  1246. points: getPoints(radius, centerPoint, 3),
  1247. close: true
  1248. },
  1249. style: {
  1250. fill: '#9ce5f4',
  1251. shadowBlur: 0,
  1252. lineWidth: 10,
  1253. shadowColor: '#66eece',
  1254. hoverCursor: 'pointer'
  1255. },
  1256. mouseEnter (e) {
  1257. this.animation('style', { lineWidth: 20, shadowBlur: 20, rotate: 120 })
  1258. },
  1259. mouseOuter (e) {
  1260. this.animation('style', { lineWidth: 10, shadowBlur: 0, rotate: 0 })
  1261. },
  1262. setGraphCenter (e, { style }) {
  1263. if (e) {
  1264. const { movementX, movementY } = e
  1265. const [cx, cy] = style.graphCenter
  1266. style.graphCenter = [cx + movementX, cy + movementY]
  1267. } else {
  1268. style.graphCenter = [...centerPoint]
  1269. }
  1270. }
  1271. }
  1272. const smoothlineClosed = render.add(smoothlineClosedConfig)
  1273. ```
  1274. #### bezierCurve
  1275. ##### shape attribute
  1276. | Attribute name | Type | Default | Annotation |
  1277. | :------------: | :-------: | :-----: | :---------------------------------------: |
  1278. | points | `Array` | `[]` | The points that makes up the bezierCurve. |
  1279. | close | `Boolean` | `false` | Whether to close the bezierCurve. |
  1280. ```javascript
  1281. const { area: [w, h] } = render
  1282. const offsetX = w / 2
  1283. const offsetY = h / 2
  1284. const points = [
  1285. // Start point
  1286. [-100 + offsetX, -50 + offsetY],
  1287. // Multiple sets of bezier curve
  1288. [
  1289. // controlPoint1,controlPoint2,endPoint
  1290. [0 + offsetX, -50 + offsetY],
  1291. [0 + offsetX, 50 + offsetY],
  1292. [100 + offsetX, 50 + offsetY]
  1293. ],
  1294. // [...],[...]
  1295. ]
  1296. const bezierCurveConfig = {
  1297. name: 'bezierCurve',
  1298. animationCurve: 'easeOutBack',
  1299. hover: true,
  1300. drag: true,
  1301. shape: {
  1302. points
  1303. },
  1304. style: {
  1305. lineWidth: 10,
  1306. stroke: '#9ce5f4',
  1307. shadowBlur: 0,
  1308. shadowColor: '#66eece',
  1309. hoverCursor: 'pointer'
  1310. },
  1311. mouseEnter (e) {
  1312. this.animation('style', { lineWidth: 20, shadowBlur: 20 })
  1313. },
  1314. mouseOuter (e) {
  1315. this.animation('style', { lineWidth: 10, shadowBlur: 0 })
  1316. }
  1317. }
  1318. const bezierCurve = render.add(bezierCurveConfig)
  1319. ```
  1320. #### bezierCurve (closed)
  1321. ```javascript
  1322. import { getCircleRadianPoint } from '../../CRender/lib/util'
  1323. function getPetalPoints (insideRadius, outsideRadius, petalNum, petalCenter) {
  1324. const PI2Dived = Math.PI * 2 / (petalNum * 3)
  1325. let points = new Array(petalNum * 3).fill('')
  1326. .map((foo, i) =>
  1327. getCircleRadianPoint(...petalCenter,
  1328. i % 3 === 0 ? insideRadius : outsideRadius,
  1329. PI2Dived * i)
  1330. )
  1331. const startPoint = points.shift()
  1332. points.push(startPoint)
  1333. points = new Array(petalNum).fill('')
  1334. .map(foo => points.splice(0, 3))
  1335. points.unshift(startPoint)
  1336. return points
  1337. }
  1338. const { area: [w, h] } = render
  1339. const petalCenter = [w / 2, h / 2]
  1340. const [raidus1, raidus2, raidus3, raidus4] = [h / 6, h / 2.5, h / 3, h / 2]
  1341. const bezierCurveClosedConfig = {
  1342. name: 'bezierCurve',
  1343. animationCurve: 'easeOutBack',
  1344. hover: true,
  1345. drag: true,
  1346. shape: {
  1347. points: getPetalPoints(raidus1, raidus2, 6, petalCenter),
  1348. close: true
  1349. },
  1350. style: {
  1351. fill: '#9ce5f4',
  1352. shadowBlur: 0,
  1353. shadowColor: '#66eece',
  1354. hoverCursor: 'pointer'
  1355. },
  1356. mouseEnter (e, { style: { graphCenter } }) {
  1357. this.animation('style', { lineWidth: 20, shadowBlur: 20 }, true)
  1358. this.animation('shape', { points: getPetalPoints(raidus3, raidus4, 6, graphCenter) })
  1359. },
  1360. mouseOuter (e, { style: { graphCenter } }) {
  1361. this.animation('style', { lineWidth: 10, shadowBlur: 0 }, true)
  1362. this.animation('shape', { points: getPetalPoints(raidus1, raidus2, 6, graphCenter) })
  1363. },
  1364. setGraphCenter (e, { style }) {
  1365. if (e) {
  1366. const { movementX, movementY } = e
  1367. const [cx, cy] = style.graphCenter
  1368. style.graphCenter = [cx + movementX, cy + movementY]
  1369. } else {
  1370. style.graphCenter = [...petalCenter]
  1371. }
  1372. }
  1373. }
  1374. const bezierCurveClosed = render.add(bezierCurveClosedConfig)
  1375. ```
  1376. #### text
  1377. ##### shape attribute
  1378. | Attribute name | Type | Default | Annotation |
  1379. | :------------: | :------: | :---------: | :------------------------: |
  1380. | content | `String` | `''` | Text content. |
  1381. | position | `Array` | `[0, 0]` | Text start position. |
  1382. | maxWidth | `Number` | `Undefined` | Maximum width of the text. |
  1383. | rowGap | `Number` | `0` | Gap between row and row. |
  1384. ```javascript
  1385. const { area: [w, h] } = render
  1386. const centerPoint = [w / 2, h / 2]
  1387. const hoverRect = [w / 2 - 100, h / 2 - 30 ,200, 60]
  1388. const textConfig = {
  1389. name: 'text',
  1390. animationCurve: 'easeOutBack',
  1391. hover: true,
  1392. drag: true,
  1393. hoverRect,
  1394. shape: {
  1395. content: 'CRender',
  1396. position: centerPoint,
  1397. maxWidth: 200
  1398. },
  1399. style: {
  1400. fill: '#9ce5f4',
  1401. fontSize: 50,
  1402. shadowBlur: 0,
  1403. rotate: 0,
  1404. shadowColor: '#66eece',
  1405. hoverCursor: 'pointer',
  1406. scale: [1, 1],
  1407. rotate: 0
  1408. },
  1409. mouseEnter (e) {
  1410. this.animation('style', { shadowBlur: 20, scale: [1.5, 1.5], rotate: 30 })
  1411. },
  1412. mouseOuter (e) {
  1413. this.animation('style', { shadowBlur: 0, scale: [1, 1], rotate: 0 })
  1414. },
  1415. moved (e, { hoverRect }) {
  1416. const { movementX, movementY } = e
  1417. hoverRect[0] += movementX
  1418. hoverRect[1] += movementY
  1419. }
  1420. }
  1421. const text = render.add(textConfig)
  1422. ```
  1423. ##### Tip
  1424. * Graph of **text** should be configured with `hoverRect` to support mouse events.
  1425. * You can use `\n` to implement multiple lines of text.
  1426. <h3 align="center">Extend New Graph</h3>
  1427. CRender provides a Function to extend new graph,you can **customize** the graphics you want.
  1428. ```javascript
  1429. import { extendNewGraph } from '@jiaminghi/c-render'
  1430. const graphName = 'newGraph'
  1431. const graphConfig = {
  1432. shape: { ... },
  1433. // ...
  1434. }
  1435. extendNewGraph(graphName, graphConfig)
  1436. ```
  1437. #### extendNewGraph
  1438. ```javascript
  1439. /**
  1440. * @description Extend new graph
  1441. * @param {String} name Name of Graph
  1442. * @param {Object} config Configuration of Graph
  1443. * @return {Undefined} Void
  1444. */
  1445. function extendNewGraph (name, config) {
  1446. // ...
  1447. }
  1448. ```
  1449. #### Graph Configuration Properties
  1450. - [shape](#shape-required)
  1451. - [validator](#validator-required)
  1452. - [draw](#draw-required)
  1453. - [hoverCheck](#hoverCheck)
  1454. - [setGraphCenter](#setGraphCenter)
  1455. - [move](#move)
  1456. - [example of extend new graph](#example-of-extend-new-graph)
  1457. #### shape (Required)
  1458. ```javascript
  1459. /**
  1460. * @type {Object}
  1461. * @description Graph shape data
  1462. */
  1463. config = {
  1464. // ...,
  1465. shape: {
  1466. // some property...
  1467. }
  1468. }
  1469. ```
  1470. #### validator (Required)
  1471. ```javascript
  1472. /**
  1473. * @type {Function}
  1474. * @description Graph configuration check
  1475. * Automatically invoked when the graph is added,
  1476. * and when the return value is false,
  1477. * the add behavior is terminated.
  1478. * @param {Graph} Current graph instance
  1479. * @return {Boolean} Whether the configuration is legal
  1480. */
  1481. config = {
  1482. // ...,
  1483. validator ({ shape }) {
  1484. // check configuration...
  1485. // return true | false
  1486. }
  1487. }
  1488. ```
  1489. #### draw (Required)
  1490. ```javascript
  1491. /**
  1492. * @type {Function}
  1493. * @description Graph plotter
  1494. * @param {CRender} Current CRender instance
  1495. * @param {Graph} Current graph instance
  1496. * @return {Undefined} Void
  1497. */
  1498. config = {
  1499. // ...,
  1500. draw ({ ctx }, { shape }) {
  1501. // drawing...
  1502. }
  1503. }
  1504. ```
  1505. #### hoverCheck (Optional)
  1506. ```javascript
  1507. /**
  1508. * @type {Function}
  1509. * @description According to the mouse event to detect
  1510. * whether the current graphics are in the hover state,
  1511. * support for mouseEnter, mouseOuter, drag, click.
  1512. * @param {Array<Number>} Position of mouse
  1513. * @param {Graph} Current graph instance
  1514. * @return {Boolean} Whether it is in hover
  1515. */
  1516. config = {
  1517. // ...,
  1518. validator ([offsetX, offsetY], { shape }) {
  1519. // Check if it is in hover state...
  1520. // return true | false
  1521. }
  1522. }
  1523. ```
  1524. #### setGraphCenter (Optional)
  1525. ```javascript
  1526. /**
  1527. * @type {Function}
  1528. * @description Set the center point of the graph to
  1529. * support rotate, scale and translate.
  1530. * Add graph and drag behavior will be called.
  1531. * @param {Event} Mouse move Event (Called when adding a graphic, the value is null)
  1532. * @param {Graph} Current graph instance
  1533. * @return {Undefined} Void
  1534. */
  1535. config = {
  1536. // ...,
  1537. setGraphCenter ([offsetX, offsetY], { style }) {
  1538. // style.graphCenter = [offsetX, offsetY]
  1539. }
  1540. }
  1541. ```
  1542. #### move (Optional)
  1543. ```javascript
  1544. /**
  1545. * @type {Function}
  1546. * @description Moving graph,support for drag
  1547. * @param {Event} Mouse move Event
  1548. * @param {Graph} Current graph instance
  1549. * @return {Undefined} Void
  1550. */
  1551. config = {
  1552. // ...,
  1553. move ([offsetX, offsetY], { shape }) {
  1554. // do something...
  1555. }
  1556. }
  1557. ```
  1558. #### example of extend new graph
  1559. ```javascript
  1560. import { extendNewGraph } from '@jiaminghi/c-render'
  1561. const circle = {
  1562. shape: {
  1563. rx: 0,
  1564. ry: 0,
  1565. r: 0
  1566. },
  1567. validator ({ shape }) {
  1568. const { rx, ry, r } = shape
  1569. if (typeof rx !== 'number' || typeof ry !== 'number' || typeof r !== 'number') {
  1570. console.error('Shape configuration is abnormal!')
  1571. return false
  1572. }
  1573. return true
  1574. },
  1575. draw ({ ctx }, { shape }) {
  1576. ctx.beginPath()
  1577. const { rx, ry, r } = shape
  1578. ctx.arc(rx, ry, r, 0, Math.PI * 2)
  1579. ctx.fill()
  1580. ctx.stroke()
  1581. ctx.closePath()
  1582. },
  1583. hoverCheck (position, { shape }) {
  1584. const { rx, ry, r } = shape
  1585. return checkPointIsInCircle(rx, ry, r, position)
  1586. },
  1587. setGraphCenter (e, { shape, style }) {
  1588. const { rx, ry } = shape
  1589. style.graphCenter = [rx, ry]
  1590. },
  1591. move ({ movementX, movementY }, { shape }) {
  1592. this.attr('shape', {
  1593. rx: shape.rx + movementX,
  1594. ry: shape.ry + movementY
  1595. })
  1596. }
  1597. }
  1598. extendNewGraph('circle', circle)
  1599. ```
  1600. <h3 align="center">Related support</h3>
  1601. - [Transition](https://github.com/jiaming743/transition/blob/master/README_EN.md)
  1602. Provide animation transition data (animationCurve).
  1603. If you want to expand the new easing curve, please move [Extend New Easing Curve](https://github.com/jiaming743/Transition/blob/master/README_EN.md#Extend-New-Easing-Curve).
  1604. - [BezierCurve](https://github.com/jiaming743/bezierCurve/blob/master/README_EN.md)
  1605. Provides support for bezierCurve, such as curve length calculation, conversion between curve and polyline.
  1606. - [Color](https://github.com/jiaming743/Color/blob/master/README_EN.md)
  1607. Color calculations are provided, such as obtaining the rgba value of the color to facilitate the calculation of the color animation state.