85ed64833afef0ff41567d9983bca0a421df6a48cbc4efb8e5643b4b98f46c6e07d71a5457c07fb1fb18f2193a48f4cca7069fca7d4ff6af88f0f484ec81b8 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974
  1. [ENGLISH](./README_EN.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="version" /></a>
  7. </p>
  8. ### CRender是干什么的?
  9. - 它是一个基于**canvas**的**矢量**图形渲染插件。
  10. - 它对图形提供动画和鼠标事件支持。
  11. ### npm安装
  12. ```shell
  13. $ npm install @jiaminghi/c-render
  14. ```
  15. ### 快速体验
  16. ```html
  17. <!--资源位于个人服务器仅供体验和测试,请勿在生产环境使用-->
  18. <!--调试版-->
  19. <script src="http://lib.jiaminghi.com/crender/crender.map.js"></script>
  20. <!--压缩版-->
  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. 详细文档及示例请移步[HomePage](http://crender.jiaminghi.com).
  28. - [使用](#使用)
  29. - [Class CRender](#class-crender)
  30. - [Class Graph](#class-graph)
  31. - [Class Style](#class-style)
  32. - [示例](#示例)
  33. - [扩展新图形](#扩展新图形)
  34. - [相关支持](#相关支持)
  35. ------
  36. <h3 align="center">使用</h3>
  37. ```javascript
  38. import CRender from '@jiaminghi/c-redner'
  39. const canvas = document.getElementById('canvas')
  40. // 实例化 CRender
  41. const render = new CRender(canvas)
  42. // 向render中添加图形
  43. const circle = render.add({ name: 'circle', ... })
  44. ```
  45. <h3 align="center">Class CRender</h3>
  46. ### 类
  47. ```javascript
  48. /**
  49. * @description Class of CRender
  50. * @param {Object} canvas Canvas 节点
  51. * @return {CRender} CRender实例
  52. */
  53. class CRender {
  54. // ...
  55. }
  56. ```
  57. ### 实例属性
  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 canvas context
  68. * @type {Object}
  69. * @example ctx = canvas.getContext('2d')
  70. */
  71. ```
  72. #### area
  73. ```javascript
  74. /**
  75. * @description canvas宽高
  76. * @type {Array<Number>}
  77. * @example area = [300,100]
  78. */
  79. ```
  80. #### animationStatus
  81. ```javascript
  82. /**
  83. * @description render是否处于动画渲染中
  84. * @type {Boolean}
  85. * @example animationStatus = true|false
  86. */
  87. ```
  88. #### graphs
  89. ```javascript
  90. /**
  91. * @description 已添加的图形
  92. * @type {Array<Graph>}
  93. * @example graphs = [Graph, Graph, ...]
  94. */
  95. ```
  96. #### [color](https://github.com/jiaming743/color)
  97. ```javascript
  98. /**
  99. * @description 颜色插件
  100. * @type {Object}
  101. */
  102. ```
  103. #### [bezierCurve](https://github.com/jiaming743/BezierCurve)
  104. ```javascript
  105. /**
  106. * @description 贝塞尔曲线插件
  107. * @type {Object}
  108. */
  109. ```
  110. ### 原型方法
  111. - [add](#add)
  112. 向render中添加图形
  113. - [clone](#clone)
  114. 克隆一个图形
  115. - [delGraph](#delGraph)
  116. 删除render中的一个图形
  117. - [delAllGraph](#delAllGraph)
  118. 删除render中所有的图形
  119. - [drawAllGraph](#drawAllGraph)
  120. 渲染render中所有的图形
  121. - [clearArea](#clearArea)
  122. 擦除canvas绘制区域
  123. - [launchAnimation](#launchAnimation)
  124. 使动画队列不为空且animationPause不为false的图形进行动画
  125. #### add
  126. ```javascript
  127. /**
  128. * @description 向render中添加图形
  129. * @param {Object} config 图形配置
  130. * @return {Graph} 图形实例
  131. */
  132. CRender.prototype.add = function (config = {}) {
  133. // ...
  134. }
  135. ```
  136. #### Clone
  137. ```javascript
  138. /**
  139. * @description 克隆一个图形
  140. * @param {Graph} graph 将要被克隆的图形
  141. * @return {Graph} 克隆的图形
  142. */
  143. CRender.prototype.clone = function (graph) {
  144. }
  145. ```
  146. #### delGraph
  147. ```javascript
  148. /**
  149. * @description 删除render中的一个图形
  150. * @param {Graph} graph 将要删除的图形实例
  151. * @return {Undefined} 无返回值
  152. */
  153. CRender.prototype.delGraph = function (graph) {
  154. // ...
  155. }
  156. ```
  157. #### delAllGraph
  158. ```javascript
  159. /**
  160. * @description 删除render中所有的图形
  161. * @return {Undefined} 无返回值
  162. */
  163. CRender.prototype.delAllGraph = function () {
  164. // ...
  165. }
  166. ```
  167. #### drawAllGraph
  168. ```javascript
  169. /**
  170. * @description 渲染render中所有的图形
  171. * @return {Undefined} 无返回值
  172. */
  173. CRender.prototype.drawAllGraph = function () {
  174. // ...
  175. }
  176. ```
  177. #### clearArea
  178. ```javascript
  179. /**
  180. * @description 擦除canvas绘制区域
  181. * @return {Undefined} 无返回值
  182. */
  183. CRender.prototype.clearArea = function () {
  184. // ...
  185. }
  186. ```
  187. #### launchAnimation
  188. ```javascript
  189. /**
  190. * @description 使动画队列不为空且animationPause不为false的图形进行动画
  191. * @return {Promise} Animation Promise
  192. */
  193. CRender.prototype.launchAnimation = function () {
  194. // ...
  195. }
  196. ```
  197. <h3 align="center">Class Graph</h3>
  198. ### 实例属性
  199. **当添加一个图形时,你可以配置这些属性。**
  200. - [visible](#visible)
  201. - [shape](#shape)
  202. - [style](#style)
  203. - [drag](#drag)
  204. - [hover](#hover)
  205. - [index](#index)
  206. - [animationDelay](#animationDelay)
  207. - [animationFrame](#animationFrame)
  208. - [animationCurve](#animationCurve)
  209. - [animationPause](#animationPause)
  210. - [hoverRect](#hoverRect)
  211. - [mouseEnter](#mouseEnter)
  212. - [mouseOuter](#mouseOuter)
  213. - [click](#click)
  214. #### visible
  215. ```javascript
  216. /**
  217. * @description 该图形是否可被渲染
  218. * @type {Boolean}
  219. * @default visible = true
  220. */
  221. ```
  222. #### shape
  223. ```javascript
  224. /**
  225. * @description 图形形状数据
  226. * @type {Object}
  227. */
  228. ```
  229. #### [style](#Class-Style)
  230. ```javascript
  231. /**
  232. * @description 图形样式数据 (Style实例)
  233. * @type {Style}
  234. */
  235. ```
  236. #### drag
  237. ```javascript
  238. /**
  239. * @description 是否启用拖拽功能
  240. * @type {Boolean}
  241. * @default drag = false
  242. */
  243. ```
  244. #### hover
  245. ```javascript
  246. /**
  247. * @description 是否启用悬浮检测
  248. * @type {Boolean}
  249. * @default hover = false
  250. */
  251. ```
  252. #### index
  253. ```javascript
  254. /**
  255. * @description 图形渲染层级,层级高者优先渲染
  256. * @type {Number}
  257. * @default index = 1
  258. */
  259. ```
  260. #### animationDelay
  261. ```javascript
  262. /**
  263. * @description 动画延迟时间(ms)
  264. * @type {Number}
  265. * @default animationDelay = 0
  266. */
  267. ```
  268. #### animationFrame
  269. ```javascript
  270. /**
  271. * @description 动画帧数
  272. * @type {Number}
  273. * @default animationFrame = 30
  274. */
  275. ```
  276. #### [animationCurve](https://github.com/jiaming743/transition)
  277. ```javascript
  278. /**
  279. * @description 动画缓动曲线
  280. * @type {String}
  281. * @default animationCurve = 'linear'
  282. */
  283. ```
  284. #### animationPause
  285. ```javascript
  286. /**
  287. * @description 是否暂停图形动画
  288. * @type {Boolean}
  289. * @default animationPause = false
  290. */
  291. ```
  292. #### hoverRect
  293. ```javascript
  294. /**
  295. * @description 矩形悬浮检测盒,配置该项则优先使用其进行鼠标悬浮检测
  296. * @type {Null|Array<Number>}
  297. * @default hoverRect = null
  298. * @example hoverRect = [0, 0, 100, 100] // [矩形起始点 x, y 坐标, 矩形宽, 高]
  299. */
  300. ```
  301. #### mouseEnter
  302. ```javascript
  303. /**
  304. * @description 鼠标进入图形事件处理器
  305. * @type {Null|Function}
  306. * @default mouseEnter = null
  307. */
  308. ```
  309. #### mouseOuter
  310. ```javascript
  311. /**
  312. * @description 鼠标移出图形事件处理器
  313. * @type {Null|Function}
  314. * @default mouseOuter = null
  315. */
  316. ```
  317. #### click
  318. ```javascript
  319. /**
  320. * @description 鼠标点击图形事件处理器
  321. * @type {Null|Function}
  322. * @default click = null
  323. */
  324. ```
  325. #### Tip
  326. 启用图形的**mouseEnter**,**mouseOuter**,**click**等事件支持需要将`hover`属性配置为`true`。扩展的新图形需要配置**hoverCheck**方法以提供事件支持。
  327. ### 原型方法
  328. - [attr](#attr)
  329. 更新图形状态
  330. - [animation](#animation)
  331. 更新图形状态(伴随动画)
  332. - [animationEnd](#animationEnd)
  333. 跳至最后一帧动画
  334. - [pauseAnimation](#pauseAnimation)
  335. 暂停动画行为
  336. - [playAnimation](#playAnimation)
  337. 尝试动画行为
  338. #### attr
  339. ```javascript
  340. /**
  341. * @description 更新图形状态
  342. * @param {String} attrName 要更新的属性名
  343. * @param {Any} change 更新的值
  344. * @return {Undefined} 无返回值
  345. */
  346. Graph.prototype.attr = function (attrName, change = undefined) {
  347. // ...
  348. }
  349. ```
  350. #### animation
  351. ```javascript
  352. /**
  353. * @description 更新图形状态(伴随动画),仅支持shape和style属性
  354. * @param {String} attrName 要更新的属性名
  355. * @param {Any} change 更新的值
  356. * @param {Boolean} wait 是否存储动画队列,等待下次动画请求
  357. * @return {Promise} Animation Promise
  358. */
  359. Graph.prototype.animation = async function (attrName, change, wait = false) {
  360. // ...
  361. }
  362. ```
  363. #### animationEnd
  364. ```javascript
  365. /**
  366. * @description 跳至最后一帧动画
  367. * @return {Undefined} 无返回值
  368. */
  369. Graph.prototype.animationEnd = function () {
  370. // ...
  371. }
  372. ```
  373. #### pauseAnimation
  374. ```javascript
  375. /**
  376. * @description 暂停动画行为
  377. * @return {Undefined} 无返回值
  378. */
  379. Graph.prototype.pauseAnimation = function () {
  380. // ...
  381. }
  382. ```
  383. #### playAnimation
  384. ```javascript
  385. /**
  386. * @description 尝试动画行为
  387. * @return {Undefined} 无返回值
  388. */
  389. Graph.prototype.playAnimation = function () {
  390. // ...
  391. }
  392. ```
  393. ### 生命周期
  394. 当向**render**中添加图形时,你可以配置如下几个方法,它们将在特定时刻被调用。
  395. - [added](#added)
  396. 图形添加时被调用
  397. - [beforeDraw](#beforeDraw)
  398. 图形绘制前被调用
  399. - [drawed](#drawed)
  400. 图形绘制后被调用
  401. - [beforeMove](#beforeMove)
  402. 图形移动前被调用
  403. - [moved](#moved)
  404. 图形移动后被调用
  405. - [beforeDelete](#beforeDelete)
  406. 图形删除前被调用
  407. - [deleted](#deleted)
  408. 图形删除后被调用
  409. #### added
  410. ```javascript
  411. /**
  412. * @description 图形添加时被调用
  413. * @param {Graph} 图形实例
  414. */
  415. config = {
  416. //...,
  417. added ({ shape, style }) {
  418. // 一些操作...
  419. }
  420. }
  421. ```
  422. #### beforeDraw
  423. ```javascript
  424. /**
  425. * @description 图形绘制前被调用,图形样式已经初始化完毕
  426. * 你可以在此时修改ctx属性
  427. * @param {Graph} 图形实例
  428. * @param {CRender} CRender实例
  429. */
  430. config = {
  431. //...,
  432. beforeDraw ({ shape, style }, { ctx }) {
  433. // 一些操作...
  434. ctx.stroke = 'transparent'
  435. }
  436. }
  437. ```
  438. #### drawed
  439. ```javascript
  440. /**
  441. * @description 图形绘制后被调用
  442. * @param {Graph} 图形实例
  443. * @param {CRender} CRender实例
  444. */
  445. config = {
  446. //...,
  447. drawed ({ shape, style }, { ctx }) {
  448. // 一些操作...
  449. }
  450. }
  451. ```
  452. #### beforeMove
  453. ```javascript
  454. /**
  455. * @description 图形移动前被调用,移动行为发生前
  456. * @param {Event} 鼠标事件
  457. * @param {Graph} 图形实例
  458. */
  459. config = {
  460. //...,
  461. beforeMove ({ offsetX, offsetY }, { shape, style }) {
  462. // 一些操作...
  463. }
  464. }
  465. ```
  466. #### moved
  467. ```javascript
  468. /**
  469. * @description 图形移动后被调用,移动行为发生后
  470. * @param {Event} 鼠标事件
  471. * @param {Graph} 图形实例
  472. */
  473. config = {
  474. //...,
  475. moved ({ offsetX, offsetY }, { shape, style }) {
  476. // 一些操作...
  477. }
  478. }
  479. ```
  480. #### beforeDelete
  481. ```javascript
  482. /**
  483. * @description 图形删除前被调用
  484. * @param {Graph} 图形实例
  485. */
  486. config = {
  487. //...,
  488. beforeDelete ({ shape, style }) {
  489. // 一些操作...
  490. }
  491. }
  492. ```
  493. #### deleted
  494. ```javascript
  495. /**
  496. * @description 图形删除后被调用
  497. * @param {Graph} 图形实例
  498. */
  499. config = {
  500. //...,
  501. deleted ({ shape, style }) {
  502. // 一些操作...
  503. }
  504. }
  505. ```
  506. <h3 align="center">Class Style</h3>
  507. ### 实例属性
  508. - [fill](#fill)
  509. - [stroke](#stroke)
  510. - [opacity](#opacity)
  511. - [lineCap](#lineCap)
  512. - [lineJoin](#)
  513. - [lineDash](#lineDash)
  514. - [lineDashOffset](#lineDashOffset)
  515. - [shadowBlur](#shadowBlur)
  516. - [shadowColor](#shadowColor)
  517. - [shadowOffsetX](#shadowOffsetX)
  518. - [shadowOffsetY](#shadowOffsetY)
  519. - [lineWidth](#lineWidth)
  520. - [graphCenter](#graphCenter)
  521. - [scale](#scale)
  522. - [rotate](#rotate)
  523. - [translate](#translate)
  524. - [hoverCursor](#hoverCursor)
  525. - [fontStyle](#fontStyle)
  526. - [fontVarient](#fontVarient)
  527. - [fontWeight](#fontWeight)
  528. - [fontSize](#fontSize)
  529. - [fontFamily](#fontFamily)
  530. - [textAlign](#textAlign)
  531. - [textBaseline](#textBaseline)
  532. - [gradientColor](#gradientColor)
  533. - [gradientType](#gradientType)
  534. - [gradientParams](#gradientParams)
  535. - [gradientWith](#gradientWith)
  536. - [gradientStops](#gradientStops)
  537. - [colors](#colors)
  538. #### fill
  539. ```javascript
  540. /**
  541. * @description 图形填充颜色的Rgba值
  542. * @type {Array<Number>}
  543. * @default fill = [0, 0, 0, 1]
  544. */
  545. ```
  546. #### stroke
  547. ```javascript
  548. /**
  549. * @description 图形描边颜色的Rgba值
  550. * @type {Array<Number>}
  551. * @default stroke = [0, 0, 0, 1]
  552. */
  553. ```
  554. #### opacity
  555. ```javascript
  556. /**
  557. * @description 图形透明度
  558. * @type {Number}
  559. * @default opacity = 1
  560. */
  561. ```
  562. #### lineCap
  563. ```javascript
  564. /**
  565. * @description Ctx的lineCap属性值
  566. * @type {Null|String}
  567. * @default lineCap = null
  568. * @example lineCap = 'butt'|'round'|'square'
  569. */
  570. ```
  571. #### lineJoin
  572. ```javascript
  573. /**
  574. * @description Ctx的lineJoin属性值
  575. * @type {Null|String}
  576. * @default lineJoin = null
  577. * @example lineJoin = 'round'|'bevel'|'miter'
  578. */
  579. ```
  580. #### lineDash
  581. ```javascript
  582. /**
  583. * @description Ctx的lineDash属性值
  584. * @type {Null|Array<Number>}
  585. * @default lineDash = null
  586. * @example lineDash = [10, 10]
  587. */
  588. ```
  589. #### lineDashOffset
  590. ```javascript
  591. /**
  592. * @description Ctx的lineDashOffset属性值
  593. * @type {Null|Number}
  594. * @default lineDashOffset = null
  595. * @example lineDashOffset = 10
  596. */
  597. ```
  598. #### shadowBlur
  599. ```javascript
  600. /**
  601. * @description Ctx的shadowBlur属性值
  602. * @type {Number}
  603. * @default shadowBlur = 0
  604. */
  605. ```
  606. #### shadowColor
  607. ```javascript
  608. /**
  609. * @description 图形阴影颜色的Rgba值
  610. * @type {Array<Number>}
  611. * @default shadowColor = [0, 0, 0, 0]
  612. */
  613. ```
  614. #### shadowOffsetX
  615. ```javascript
  616. /**
  617. * @description Ctx的shadowOffsetX属性值
  618. * @type {Number}
  619. * @default shadowOffsetX = 0
  620. */
  621. ```
  622. #### shadowOffsetY
  623. ```javascript
  624. /**
  625. * @description Ctx的shadowOffsetY属性值
  626. * @type {Number}
  627. * @default shadowOffsetY = 0
  628. */
  629. ```
  630. #### lineWidth
  631. ```javascript
  632. /**
  633. * @description Ctx的lineWidth属性值
  634. * @type {Number}
  635. * @default lineWidth = 0
  636. */
  637. ```
  638. #### graphCenter
  639. ```javascript
  640. /**
  641. * @description 图形中心点
  642. * @type {Null|Array<Number>}
  643. * @default graphCenter = null
  644. * @example graphCenter = [10, 10]
  645. */
  646. ```
  647. #### scale
  648. ```javascript
  649. /**
  650. * @description 图形缩放倍数
  651. * @type {Null|Array<Number>}
  652. * @default scale = null
  653. * @example scale = [1.5, 1.5]
  654. */
  655. ```
  656. #### rotate
  657. ```javascript
  658. /**
  659. * @description 图形旋转角度
  660. * @type {Null|Number}
  661. * @default rotate = null
  662. * @example rotate = 10
  663. */
  664. ```
  665. #### translate
  666. ```javascript
  667. /**
  668. * @description 图形位移距离
  669. * @type {Null|Array<Number>}
  670. * @default translate = null
  671. * @example translate = [10, 10]
  672. */
  673. ```
  674. #### hoverCursor
  675. ```javascript
  676. /**
  677. * @description 鼠标悬浮在图形上时cursor的值
  678. * @type {String}
  679. * @default hoverCursor = 'pointer'
  680. * @example hoverCursor = 'default'|'pointer'|'auto'|'crosshair'|'move'|'wait'|...
  681. */
  682. ```
  683. #### fontStyle
  684. ```javascript
  685. /**
  686. * @description Ctx的fontStyle属性值
  687. * @type {String}
  688. * @default fontStyle = 'normal'
  689. * @example fontStyle = 'normal'|'italic'|'oblique'
  690. */
  691. ```
  692. #### fontVarient
  693. ```javascript
  694. /**
  695. * @description Ctx的fontVarient属性值
  696. * @type {String}
  697. * @default fontVarient = 'normal'
  698. * @example fontVarient = 'normal'|'small-caps'
  699. */
  700. ```
  701. #### fontWeight
  702. ```javascript
  703. /**
  704. * @description Ctx的fontWeight属性值
  705. * @type {String|Number}
  706. * @default fontWeight = 'normal'
  707. * @example fontWeight = 'normal'|'bold'|'bolder'|'lighter'|Number
  708. */
  709. ```
  710. #### fontSize
  711. ```javascript
  712. /**
  713. * @description Ctx的fontSize属性值
  714. * @type {Number}
  715. * @default fontSize = 10
  716. */
  717. ```
  718. #### fontFamily
  719. ```javascript
  720. /**
  721. * @description Ctx的fontFamily属性值
  722. * @type {String}
  723. * @default fontFamily = 'Arial'
  724. */
  725. ```
  726. #### textAlign
  727. ```javascript
  728. /**
  729. * @description Ctx的textAlign属性值
  730. * @type {String}
  731. * @default textAlign = 'center'
  732. * @example textAlign = 'start'|'end'|'left'|'right'|'center'
  733. */
  734. ```
  735. #### textBaseline
  736. ```javascript
  737. /**
  738. * @description Ctx的textBaseline属性值
  739. * @type {String}
  740. * @default textBaseline = 'middle'
  741. * @example textBaseline = 'top'|'bottom'|'middle'|'alphabetic'|'hanging'
  742. */
  743. ```
  744. #### gradientColor
  745. ```javascript
  746. /**
  747. * @description 用于创建渐变色的颜色
  748. * @type {Null|Array<String>}
  749. * @default gradientColor = null
  750. * @example gradientColor = ['#000', '#111', '#222']
  751. */
  752. ```
  753. #### gradientType
  754. ```javascript
  755. /**
  756. * @description 渐变类型
  757. * @type {String}
  758. * @default gradientType = 'linear'
  759. * @example gradientType = 'linear' | 'radial'
  760. */
  761. ```
  762. #### gradientParams
  763. ```javascript
  764. /**
  765. * @description 渐变参数
  766. * @type {Array<Number>}
  767. * @default gradientParams = null
  768. * @example gradientParams = [x0, y0, x1, y1] (线性渐变)
  769. * @example gradientParams = [x0, y0, r0, x1, y1, r1] (径向渐变)
  770. */
  771. ```
  772. #### gradientWith
  773. ```javascript
  774. /**
  775. * @description 使用渐变色的属性
  776. * @type {String}
  777. * @default gradientWith = 'stroke'
  778. * @example gradientWith = 'stroke' | 'fill'
  779. */
  780. ```
  781. #### gradientStops
  782. ```javascript
  783. /**
  784. * @description 渐变色位置
  785. * @type {String|Array<Number>}
  786. * @default gradientStops = 'auto'
  787. * @example gradientStops = 'auto' | [0, .2, .3, 1]
  788. */
  789. ```
  790. #### colors
  791. ```javascript
  792. /**
  793. * @description 支持动画过渡的颜色容器
  794. * @type {Array<String>|Object}
  795. * @default colors = null
  796. * @example colors = ['#000', '#111', '#222']
  797. * @example colors = { a: '#000', b: '#111' }
  798. */
  799. ```
  800. #### Tip
  801. `gradientColor`和`gradientParams`被配置后将自动启用**渐变**。
  802. ### 原型方法
  803. - [getStyle](#getStyle)
  804. 获取图形当前样式配置
  805. #### getStyle
  806. ```javascript
  807. /**
  808. * @description 获取图形当前样式配置
  809. * @return {Object} 样式配置
  810. */
  811. Style.prototype.getStyle = function () {
  812. }
  813. ```
  814. <h3 align="center">示例</h3>
  815. CRender提供如下基础矢量图形。
  816. - [圆形](#圆形)
  817. - [椭圆形](#椭圆形)
  818. - [矩形](#矩形)
  819. - [环形](#环形)
  820. - [弧形](#弧形)
  821. - [扇形](#扇形)
  822. - [正多边形](#正多边形)
  823. - [折线](#折线)
  824. - [折线(闭合)](#折线(闭合))
  825. - [光滑曲线](#光滑曲线)
  826. - [光滑曲线(闭合)](#光滑曲线(闭合))
  827. - [贝塞尔曲线](#贝塞尔曲线)
  828. - [贝塞尔曲线(闭合)](#贝塞尔曲线(闭合))
  829. - [文本](#文本)
  830. #### 圆形
  831. ##### shape属性
  832. | 属性名 | 类型 | 默认值 | 注解 |
  833. | :---: | :---: | :---: | :---: |
  834. | rx | `Number` | `0` | 圆心x轴坐标 |
  835. | ry | `Number` | `0` | 圆心y轴坐标 |
  836. | r | `Number` | `0` | 圆半径 |
  837. ```javascript
  838. const { area: [w, h] } = render
  839. const circleConfig = {
  840. name: 'circle',
  841. animationCurve: 'easeOutBack',
  842. hover: true,
  843. drag: true,
  844. shape: {
  845. rx: w / 2,
  846. ry: h / 2,
  847. r: 50
  848. },
  849. style: {
  850. fill: '#9ce5f4',
  851. shadowBlur: 0,
  852. shadowColor: '#66eece',
  853. hoverCursor: 'pointer'
  854. },
  855. mouseEnter (e) {
  856. this.animation('shape', { r: 70 }, true)
  857. this.animation('style', { shadowBlur: 20 })
  858. },
  859. mouseOuter (e) {
  860. this.animation('shape', { r: 50 }, true)
  861. this.animation('style', { shadowBlur: 0 })
  862. }
  863. }
  864. const circle = render.add(circleConfig)
  865. ```
  866. #### 椭圆形
  867. ##### shape属性
  868. | 属性名 | 类型 | 默认值 | 注解 |
  869. | :---: | :---: | :---: | :---: |
  870. | rx | `Number` | `0` | 圆心x轴坐标 |
  871. | ry | `Number` | `0` | 圆心y轴坐标 |
  872. | hr | `Number` | `0` | 横轴半径 |
  873. | vr | `Number` | `0` | 竖轴半径 |
  874. ```javascript
  875. const { area: [w, h] } = render
  876. const ellipseConfig = {
  877. name: 'ellipse',
  878. animationCurve: 'easeOutBack',
  879. hover: true,
  880. drag: true,
  881. shape: {
  882. rx: w / 2,
  883. ry: h / 2,
  884. hr: 80,
  885. vr: 30
  886. },
  887. style: {
  888. fill: '#9ce5f4',
  889. shadowBlur: 0,
  890. shadowColor: '#66eece',
  891. scale: [1, 1],
  892. hoverCursor: 'pointer'
  893. },
  894. mouseEnter (e) {
  895. this.animation('style', { scale: [1.5, 1.5], shadowBlur: 20 })
  896. },
  897. mouseOuter (e) {
  898. this.animation('style', { scale: [1, 1], shadowBlur: 0 })
  899. }
  900. }
  901. const ellipse = render.add(ellipseConfig)
  902. ```
  903. #### 矩形
  904. ##### shape属性
  905. | 属性名 | 类型 | 默认值 | 注解 |
  906. | :---: | :---: | :---: | :---: |
  907. | x | `Number` | `0` | 矩形左上角x轴坐标 |
  908. | y | `Number` | `0` | 矩形左上角y轴坐标 |
  909. | w | `Number` | `0` | 矩形宽度 |
  910. | h | `Number` | `0` | 矩形高度 |
  911. ```javascript
  912. const { area: [w, h] } = render
  913. const rectConfig = {
  914. name: 'rect',
  915. animationCurve: 'easeOutBack',
  916. hover: true,
  917. drag: true,
  918. shape: {
  919. x: w / 2 - rectWidth / 2,
  920. y: h / 2 - rectHeight / 2,
  921. w: rectWidth,
  922. h: rectHeight
  923. },
  924. style: {
  925. fill: '#9ce5f4',
  926. shadowBlur: 0,
  927. shadowColor: '#66eece',
  928. hoverCursor: 'pointer',
  929. translate: [0, 0]
  930. },
  931. mouseEnter (e) {
  932. this.animation('shape', { w: 400 }, true)
  933. this.animation('style', { shadowBlur: 20, translate: [-100, 0] })
  934. },
  935. mouseOuter (e) {
  936. this.animation('shape', { w: 200 }, true)
  937. this.animation('style', { shadowBlur: 0, translate: [0, 0] })
  938. }
  939. }
  940. const rect = render.add(rectConfig)
  941. ```
  942. #### 环形
  943. ##### shape属性
  944. | 属性名 | 类型 | 默认值 | 注解 |
  945. | :---: | :---: | :---: | :---: |
  946. | rx | `Number` | `0` | 中心点x轴坐标 |
  947. | ry | `Number` | `0` | 中心点y轴坐标 |
  948. | r | `Number` | `0` | 环半径 |
  949. ```javascript
  950. const { area: [w, h] } = render
  951. const ringConfig = {
  952. name: 'ring',
  953. animationCurve: 'easeOutBack',
  954. hover: true,
  955. drag: true,
  956. shape: {
  957. rx: w / 2,
  958. ry: h / 2,
  959. r: 50
  960. },
  961. style: {
  962. stroke: '#9ce5f4',
  963. lineWidth: 20,
  964. hoverCursor: 'pointer',
  965. shadowBlur: 0,
  966. shadowColor: '#66eece'
  967. },
  968. mouseEnter (e) {
  969. this.animation('style', { shadowBlur: 20, lineWidth: 30 })
  970. },
  971. mouseOuter (e) {
  972. this.animation('style', { shadowBlur: 0, lineWidth: 20 })
  973. }
  974. }
  975. const ring = render.add(ringConfig)
  976. ```
  977. #### 弧形
  978. ##### shape属性
  979. | 属性名 | 类型 | 默认值 | 注解 |
  980. | :---: | :---: | :---: | :---: |
  981. | rx | `Number` | `0` | 中心点x轴坐标 |
  982. | ry | `Number` | `0` | 中心点y轴坐标 |
  983. | r | `Number` | `0` | 弧半径 |
  984. | startAngle | `Number` | `0` | 弧起始弧度值 |
  985. | endAngle | `Number` | `0` | 弧结束弧度值 |
  986. | clockWise | `Boolean` | `true` | 是否顺时针 |
  987. ```javascript
  988. const { area: [w, h] } = render
  989. const arcConfig = {
  990. name: 'arc',
  991. animationCurve: 'easeOutBack',
  992. hover: true,
  993. drag: true,
  994. shape: {
  995. rx: w / 2,
  996. ry: h / 2,
  997. r: 60,
  998. startAngle: 0,
  999. endAngle: Math.PI / 3
  1000. },
  1001. style: {
  1002. stroke: '#9ce5f4',
  1003. lineWidth: 20,
  1004. shadowBlur: 0,
  1005. rotate: 0,
  1006. shadowColor: '#66eece',
  1007. hoverCursor: 'pointer'
  1008. },
  1009. mouseEnter (e) {
  1010. this.animation('shape', { endAngle: Math.PI }, true)
  1011. this.animation('style', { shadowBlur: 20, rotate: -30, lineWidth: 30 })
  1012. },
  1013. mouseOuter (e) {
  1014. this.animation('shape', { endAngle: Math.PI / 3 }, true)
  1015. this.animation('style', { shadowBlur: 0, rotate: 0, lineWidth: 20 })
  1016. }
  1017. }
  1018. const arc = render.add(arcConfig)
  1019. ```
  1020. #### 扇形
  1021. ##### shape属性
  1022. | 属性名 | 类型 | 默认值 | 注解 |
  1023. | :---: | :---: | :---: | :---: |
  1024. | rx | `Number` | `0` | 中心点x轴坐标 |
  1025. | ry | `Number` | `0` | 中心点y轴坐标 |
  1026. | r | `Number` | `0` | 扇形半径 |
  1027. | startAngle | `Number` | `0` | 扇形起始弧度值 |
  1028. | endAngle | `Number` | `0` | 扇形结束弧度值 |
  1029. | clockWise | `Boolean` | `true` | 是否顺时针 |
  1030. ```javascript
  1031. const { area: [w, h] } = render
  1032. const sectorConfig = {
  1033. name: 'sector',
  1034. animationCurve: 'easeOutBack',
  1035. hover: true,
  1036. drag: true,
  1037. shape: {
  1038. rx: w / 2,
  1039. ry: h / 2,
  1040. r: 60,
  1041. startAngle: 0,
  1042. endAngle: Math.PI / 3
  1043. },
  1044. style: {
  1045. fill: '#9ce5f4',
  1046. shadowBlur: 0,
  1047. rotate: 0,
  1048. shadowColor: '#66eece',
  1049. hoverCursor: 'pointer'
  1050. },
  1051. mouseEnter (e) {
  1052. this.animation('shape', { endAngle: Math.PI, r: 70 }, true)
  1053. this.animation('style', { shadowBlur: 20, rotate: -30, lineWidth: 30 })
  1054. },
  1055. mouseOuter (e) {
  1056. this.animation('shape', { endAngle: Math.PI / 3, r: 60 }, true)
  1057. this.animation('style', { shadowBlur: 0, rotate: 0, lineWidth: 20 })
  1058. }
  1059. }
  1060. const sector = render.add(sectorConfig)
  1061. ```
  1062. #### 正多边形
  1063. ##### shape属性
  1064. | 属性名 | 类型 | 默认值 | 注解 |
  1065. | :---: | :---: | :---: | :---: |
  1066. | rx | `Number` | `0` | 中心点x轴坐标 |
  1067. | ry | `Number` | `0` | 中心点y轴坐标 |
  1068. | r | `Number` | `0` | 外接圆半径 |
  1069. | side | `Number` | `0` | 边数 |
  1070. ```javascript
  1071. const { area: [w, h] } = render
  1072. const regPolygonConfig = {
  1073. name: 'regPolygon',
  1074. animationCurve: 'easeOutBack',
  1075. hover: true,
  1076. drag: true,
  1077. shape: {
  1078. rx: w / 2,
  1079. ry: h / 2,
  1080. r: 60,
  1081. side: 6
  1082. },
  1083. style: {
  1084. fill: '#9ce5f4',
  1085. hoverCursor: 'pointer',
  1086. shadowBlur: 0,
  1087. rotate: 0,
  1088. shadowColor: '#66eece'
  1089. },
  1090. mouseEnter (e) {
  1091. this.animation('shape', { endAngle: Math.PI, r: 100 }, true)
  1092. this.animation('style', { shadowBlur: 20, rotate: 180 })
  1093. },
  1094. mouseOuter (e) {
  1095. this.animation('shape', { endAngle: Math.PI / 3, r: 60 }, true)
  1096. this.animation('style', { shadowBlur: 0, rotate: 0 })
  1097. }
  1098. }
  1099. const regPolygon = render.add(regPolygonConfig)
  1100. ```
  1101. #### 折线
  1102. ##### shape属性
  1103. | 属性名 | 类型 | 默认值 | 注解 |
  1104. | :---: | :---: | :---: | :---: |
  1105. | points | `Array` | `[]` | 构成折线的点 |
  1106. | close | `Boolean` | `false` | 是否闭合折线 |
  1107. ```javascript
  1108. const { area: [w, h] } = render
  1109. const top = h / 3
  1110. const bottom = h / 3 * 2
  1111. const gap = w / 10
  1112. const beginX = w / 2 - gap * 2
  1113. const points = new Array(5).fill('').map((t, i) =>
  1114. [beginX + gap * i, i % 2 === 0 ? top : bottom])
  1115. const polylineConfig = {
  1116. name: 'polyline',
  1117. animationCurve: 'easeOutBack',
  1118. hover: true,
  1119. drag: true,
  1120. shape: {
  1121. points
  1122. },
  1123. style: {
  1124. stroke: '#9ce5f4',
  1125. shadowBlur: 0,
  1126. lineWidth: 10,
  1127. shadowColor: '#66eece',
  1128. hoverCursor: 'pointer'
  1129. },
  1130. mouseEnter (e) {
  1131. this.animation('style', { lineWidth: 20, shadowBlur: 20 })
  1132. },
  1133. mouseOuter (e) {
  1134. this.animation('style', { lineWidth: 10, shadowBlur: 0 })
  1135. }
  1136. }
  1137. const polyline = render.add(polylineConfig)
  1138. ```
  1139. #### 折线(闭合)
  1140. ```javascript
  1141. const { area: [w, h] } = render
  1142. const top = h / 3
  1143. const bottom = h / 3 * 2
  1144. const gap = w / 10
  1145. const beginX = w / 2 - gap * 2
  1146. const points = new Array(5).fill('').map((t, i) =>
  1147. [beginX + gap * i, i % 2 === 0 ? top : bottom])
  1148. points[2][1] += top * 1.3
  1149. const polylineClosedConfig = {
  1150. name: 'polyline',
  1151. animationCurve: 'easeOutBack',
  1152. hover: true,
  1153. drag: true,
  1154. shape: {
  1155. points,
  1156. close: true
  1157. },
  1158. style: {
  1159. fill: '#9ce5f4',
  1160. shadowBlur: 0,
  1161. lineWidth: 10,
  1162. shadowColor: '#66eece',
  1163. hoverCursor: 'pointer'
  1164. },
  1165. mouseEnter (e) {
  1166. this.animation('style', { shadowBlur: 20 }, true)
  1167. const pointsCloned = deepClone(this.shape.points)
  1168. pointsCloned[2][1] += top * 0.3
  1169. this.animation('shape', { points: pointsCloned })
  1170. },
  1171. mouseOuter (e) {
  1172. this.animation('style', { shadowBlur: 0 }, true)
  1173. const pointsCloned = deepClone(this.shape.points)
  1174. pointsCloned[2][1] -= top * 0.3
  1175. this.animation('shape', { points: pointsCloned })
  1176. }
  1177. }
  1178. const polylineClosed = render.add(polylineClosedConfig)
  1179. ```
  1180. #### 光滑曲线
  1181. ##### shape属性
  1182. | 属性名 | 类型 | 默认值 | 注解 |
  1183. | :---: | :---: | :---: | :---: |
  1184. | points | `Array` | `[]` | 构成光滑曲线的点 |
  1185. | close | `Boolean` | `false` | 是否闭合光滑曲线 |
  1186. ```javascript
  1187. const { area: [w, h] } = render
  1188. const top = h / 3
  1189. const bottom = h / 3 * 2
  1190. const gap = w / 10
  1191. const beginX = w / 2 - gap * 2
  1192. const points = new Array(5).fill('').map((t, i) =>
  1193. [beginX + gap * i, i % 2 === 0 ? top : bottom])
  1194. const smoothlineConfig = {
  1195. name: 'smoothline',
  1196. animationCurve: 'easeOutBack',
  1197. hover: true,
  1198. drag: true,
  1199. shape: {
  1200. points
  1201. },
  1202. style: {
  1203. stroke: '#9ce5f4',
  1204. shadowBlur: 0,
  1205. lineWidth: 10,
  1206. shadowColor: '#66eece',
  1207. hoverCursor: 'pointer'
  1208. },
  1209. mouseEnter (e) {
  1210. this.animation('style', { lineWidth: 20, shadowBlur: 20 })
  1211. },
  1212. mouseOuter (e) {
  1213. this.animation('style', { lineWidth: 10, shadowBlur: 0 })
  1214. }
  1215. }
  1216. const smoothline = render.add(smoothlineConfig)
  1217. ```
  1218. #### 光滑曲线(闭合)
  1219. ```javascript
  1220. import { getCircleRadianPoint } from '../../CRender/lib/util'
  1221. function getPoints (radius, centerPoint, pointNum) {
  1222. const PIDived = Math.PI * 2 / pointNum
  1223. const points = new Array(pointNum).fill('')
  1224. .map((foo, i) =>
  1225. getCircleRadianPoint(...centerPoint, radius, PIDived * i)
  1226. )
  1227. return points
  1228. }
  1229. const { area: [w, h] } = render
  1230. const radius = h / 3
  1231. const centerPoint = [w / 2, h / 2]
  1232. const smoothlineClosedConfig = {
  1233. name: 'smoothline',
  1234. animationCurve: 'easeOutBack',
  1235. hover: true,
  1236. drag: true,
  1237. shape: {
  1238. points: getPoints(radius, centerPoint, 3),
  1239. close: true
  1240. },
  1241. style: {
  1242. fill: '#9ce5f4',
  1243. shadowBlur: 0,
  1244. lineWidth: 10,
  1245. shadowColor: '#66eece',
  1246. hoverCursor: 'pointer'
  1247. },
  1248. mouseEnter (e) {
  1249. this.animation('style', { lineWidth: 20, shadowBlur: 20, rotate: 120 })
  1250. },
  1251. mouseOuter (e) {
  1252. this.animation('style', { lineWidth: 10, shadowBlur: 0, rotate: 0 })
  1253. },
  1254. setGraphCenter (e, { style }) {
  1255. if (e) {
  1256. const { movementX, movementY } = e
  1257. const [cx, cy] = style.graphCenter
  1258. style.graphCenter = [cx + movementX, cy + movementY]
  1259. } else {
  1260. style.graphCenter = [...centerPoint]
  1261. }
  1262. }
  1263. }
  1264. const smoothlineClosed = render.add(smoothlineClosedConfig)
  1265. ```
  1266. #### 贝塞尔曲线
  1267. ##### shape属性
  1268. | 属性名 | 类型 | 默认值 | 注解 |
  1269. | :---: | :---: | :---: | :---: |
  1270. | points | `Array` | `[]` | 构成贝塞尔曲线的点 |
  1271. | close | `Boolean` | `false` | 是否闭合贝塞尔曲线 |
  1272. ```javascript
  1273. const { area: [w, h] } = render
  1274. const offsetX = w / 2
  1275. const offsetY = h / 2
  1276. const points = [
  1277. // 起始点
  1278. [-100 + offsetX, -50 + offsetY],
  1279. // 多段贝塞尔曲线
  1280. [
  1281. // 控制点1,控制点2,结束点
  1282. [0 + offsetX, -50 + offsetY],
  1283. [0 + offsetX, 50 + offsetY],
  1284. [100 + offsetX, 50 + offsetY]
  1285. ],
  1286. // [...],[...]
  1287. ]
  1288. const bezierCurveConfig = {
  1289. name: 'bezierCurve',
  1290. animationCurve: 'easeOutBack',
  1291. hover: true,
  1292. drag: true,
  1293. shape: {
  1294. points
  1295. },
  1296. style: {
  1297. lineWidth: 10,
  1298. stroke: '#9ce5f4',
  1299. shadowBlur: 0,
  1300. shadowColor: '#66eece',
  1301. hoverCursor: 'pointer'
  1302. },
  1303. mouseEnter (e) {
  1304. this.animation('style', { lineWidth: 20, shadowBlur: 20 })
  1305. },
  1306. mouseOuter (e) {
  1307. this.animation('style', { lineWidth: 10, shadowBlur: 0 })
  1308. }
  1309. }
  1310. const bezierCurve = render.add(bezierCurveConfig)
  1311. ```
  1312. #### 贝塞尔曲线(闭合)
  1313. ```javascript
  1314. import { getCircleRadianPoint } from '../../CRender/lib/util'
  1315. function getPetalPoints (insideRadius, outsideRadius, petalNum, petalCenter) {
  1316. const PI2Dived = Math.PI * 2 / (petalNum * 3)
  1317. let points = new Array(petalNum * 3).fill('')
  1318. .map((foo, i) =>
  1319. getCircleRadianPoint(...petalCenter,
  1320. i % 3 === 0 ? insideRadius : outsideRadius,
  1321. PI2Dived * i)
  1322. )
  1323. const startPoint = points.shift()
  1324. points.push(startPoint)
  1325. points = new Array(petalNum).fill('')
  1326. .map(foo => points.splice(0, 3))
  1327. points.unshift(startPoint)
  1328. return points
  1329. }
  1330. const { area: [w, h] } = render
  1331. const petalCenter = [w / 2, h / 2]
  1332. const [raidus1, raidus2, raidus3, raidus4] = [h / 6, h / 2.5, h / 3, h / 2]
  1333. const bezierCurveClosedConfig = {
  1334. name: 'bezierCurve',
  1335. animationCurve: 'easeOutBack',
  1336. hover: true,
  1337. drag: true,
  1338. shape: {
  1339. points: getPetalPoints(raidus1, raidus2, 6, petalCenter),
  1340. close: true
  1341. },
  1342. style: {
  1343. fill: '#9ce5f4',
  1344. shadowBlur: 0,
  1345. shadowColor: '#66eece',
  1346. hoverCursor: 'pointer'
  1347. },
  1348. mouseEnter (e, { style: { graphCenter } }) {
  1349. this.animation('style', { lineWidth: 20, shadowBlur: 20 }, true)
  1350. this.animation('shape', { points: getPetalPoints(raidus3, raidus4, 6, graphCenter) })
  1351. },
  1352. mouseOuter (e, { style: { graphCenter } }) {
  1353. this.animation('style', { lineWidth: 10, shadowBlur: 0 }, true)
  1354. this.animation('shape', { points: getPetalPoints(raidus1, raidus2, 6, graphCenter) })
  1355. },
  1356. setGraphCenter (e, { style }) {
  1357. if (e) {
  1358. const { movementX, movementY } = e
  1359. const [cx, cy] = style.graphCenter
  1360. style.graphCenter = [cx + movementX, cy + movementY]
  1361. } else {
  1362. style.graphCenter = [...petalCenter]
  1363. }
  1364. }
  1365. }
  1366. const bezierCurveClosed = render.add(bezierCurveClosedConfig)
  1367. ```
  1368. #### 文本
  1369. ##### shape属性
  1370. | 属性名 | 类型 | 默认值 | 注解 |
  1371. | :---: | :---: | :---: | :---: |
  1372. | content | `String` | `''` | 文本内容 |
  1373. | position | `Array` | `[0, 0]` | 文本起始位置 |
  1374. | maxWidth | `Number` | `Undefined` | 文本最大宽度 |
  1375. | rowGap | `Number` | `0` | 行间距 |
  1376. ```javascript
  1377. const { area: [w, h] } = render
  1378. const centerPoint = [w / 2, h / 2]
  1379. const hoverRect = [w / 2 - 100, h / 2 - 30 ,200, 60]
  1380. const textConfig = {
  1381. name: 'text',
  1382. animationCurve: 'easeOutBack',
  1383. hover: true,
  1384. drag: true,
  1385. hoverRect,
  1386. shape: {
  1387. content: 'CRender',
  1388. position: centerPoint,
  1389. maxWidth: 200
  1390. },
  1391. style: {
  1392. fill: '#9ce5f4',
  1393. fontSize: 50,
  1394. shadowBlur: 0,
  1395. rotate: 0,
  1396. shadowColor: '#66eece',
  1397. hoverCursor: 'pointer',
  1398. scale: [1, 1],
  1399. rotate: 0
  1400. },
  1401. mouseEnter (e) {
  1402. this.animation('style', { shadowBlur: 20, scale: [1.5, 1.5], rotate: 30 })
  1403. },
  1404. mouseOuter (e) {
  1405. this.animation('style', { shadowBlur: 0, scale: [1, 1], rotate: 0 })
  1406. },
  1407. moved (e, { hoverRect }) {
  1408. const { movementX, movementY } = e
  1409. hoverRect[0] += movementX
  1410. hoverRect[1] += movementY
  1411. }
  1412. }
  1413. const text = render.add(textConfig)
  1414. ```
  1415. ##### Tip
  1416. * 图形**text**的鼠标事件依赖`hoverRect`属性,如需鼠标事件生效请对其配置
  1417. * 文本中插入`\n`可以进行换行。
  1418. <h3 align="center">扩展新图形</h3>
  1419. CRender提供了一个方法去扩展新的图形,你可以**自定义**想要的图形。
  1420. ```javascript
  1421. import { extendNewGraph } from '@jiaminghi/c-render'
  1422. const graphName = 'newGraph'
  1423. const graphConfig = {
  1424. shape: { ... },
  1425. // ...
  1426. }
  1427. extendNewGraph(graphName, graphConfig)
  1428. ```
  1429. #### extendNewGraph
  1430. ```javascript
  1431. /**
  1432. * @description 扩展新图形
  1433. * @param {String} name 图形名称
  1434. * @param {Object} config 图形配置
  1435. * @return {Undefined} 无返回值
  1436. */
  1437. function extendNewGraph (name, config) {
  1438. // ...
  1439. }
  1440. ```
  1441. #### 图形配置属性
  1442. - [shape](#shape-required)
  1443. - [validator](#validator-required)
  1444. - [draw](#draw-required)
  1445. - [hoverCheck](#hoverCheck)
  1446. - [setGraphCenter](#setGraphCenter)
  1447. - [move](#move)
  1448. - [扩展示例](#扩展示例)
  1449. #### shape (必须)
  1450. ```javascript
  1451. /**
  1452. * @type {Object}
  1453. * @description 图形形状数据
  1454. */
  1455. config = {
  1456. // ...,
  1457. shape: {
  1458. // 一些属性...
  1459. }
  1460. }
  1461. ```
  1462. #### validator (必须)
  1463. ```javascript
  1464. /**
  1465. * @type {Function}
  1466. * @description 图形添加时将被调用,用于检测图形配置是否合法,
  1467. * 若返回值为false则终止添加行为
  1468. * @param {Graph} 当前图形实例
  1469. * @return {Boolean} 配置是否合法
  1470. */
  1471. config = {
  1472. // ...,
  1473. validator ({ shape }) {
  1474. // 检查图形配置...
  1475. // return true | false
  1476. }
  1477. }
  1478. ```
  1479. #### draw (必须)
  1480. ```javascript
  1481. /**
  1482. * @type {Function}
  1483. * @description 图形绘制器
  1484. * @param {CRender} 当前CRender实例
  1485. * @param {Graph} 当前图形实例
  1486. * @return {Undefined} 无返回值
  1487. */
  1488. config = {
  1489. // ...,
  1490. draw ({ ctx }, { shape }) {
  1491. // 绘制...
  1492. }
  1493. }
  1494. ```
  1495. #### hoverCheck (可选)
  1496. ```javascript
  1497. /**
  1498. * @type {Function}
  1499. * @description 通过鼠标位置去判断当前图形是否处于鼠标悬浮状态,
  1500. * 用于给mouseEnter, mouseOuter, drag, click事件提供支持。
  1501. * @param {Array<Number>} 鼠标位置
  1502. * @param {Graph} 当前图形实例
  1503. * @return {Boolean} 是否处于鼠标悬浮状态
  1504. */
  1505. config = {
  1506. // ...,
  1507. validator ([offsetX, offsetY], { shape }) {
  1508. // 检测是否处于鼠标悬浮状态...
  1509. // return true | false
  1510. }
  1511. }
  1512. ```
  1513. #### setGraphCenter (可选)
  1514. ```javascript
  1515. /**
  1516. * @type {Function}
  1517. * @description 设置图形中心点
  1518. * 提供rotate, scale and translate支持
  1519. * 添加图形及图形被拖动后将被调用
  1520. * @param {Event} 鼠标事件 (图形被添加时调用,该参数为null)
  1521. * @param {Graph} 当前图形实例
  1522. * @return {Undefined} 无返回值
  1523. */
  1524. config = {
  1525. // ...,
  1526. setGraphCenter ([offsetX, offsetY], { style }) {
  1527. // style.graphCenter = [offsetX, offsetY]
  1528. }
  1529. }
  1530. ```
  1531. #### move (Optional)
  1532. ```javascript
  1533. /**
  1534. * @type {Function}
  1535. * @description Moving graph,support for drag
  1536. * @param {Event} Mouse move Event
  1537. * @param {Graph} Current graph instance
  1538. * @return {Undefined} Void
  1539. */
  1540. config = {
  1541. // ...,
  1542. move ([offsetX, offsetY], { shape }) {
  1543. // 一些操作...
  1544. }
  1545. }
  1546. ```
  1547. #### 扩展示例
  1548. ```javascript
  1549. import { extendNewGraph } from '@jiaminghi/c-render'
  1550. const circle = {
  1551. shape: {
  1552. rx: 0,
  1553. ry: 0,
  1554. r: 0
  1555. },
  1556. validator ({ shape }) {
  1557. const { rx, ry, r } = shape
  1558. if (typeof rx !== 'number' || typeof ry !== 'number' || typeof r !== 'number') {
  1559. console.error('Shape configuration is abnormal!')
  1560. return false
  1561. }
  1562. return true
  1563. },
  1564. draw ({ ctx }, { shape }) {
  1565. ctx.beginPath()
  1566. const { rx, ry, r } = shape
  1567. ctx.arc(rx, ry, r, 0, Math.PI * 2)
  1568. ctx.fill()
  1569. ctx.stroke()
  1570. ctx.closePath()
  1571. },
  1572. hoverCheck (position, { shape }) {
  1573. const { rx, ry, r } = shape
  1574. return checkPointIsInCircle(rx, ry, r, position)
  1575. },
  1576. setGraphCenter (e, { shape, style }) {
  1577. const { rx, ry } = shape
  1578. style.graphCenter = [rx, ry]
  1579. },
  1580. move ({ movementX, movementY }, { shape }) {
  1581. this.attr('shape', {
  1582. rx: shape.rx + movementX,
  1583. ry: shape.ry + movementY
  1584. })
  1585. }
  1586. }
  1587. extendNewGraph('circle', circle)
  1588. ```
  1589. <h3 align="center">相关支持</h3>
  1590. - [Transition](https://github.com/jiaming743/transition)
  1591. 提供动画过渡数据 (animationCurve)。
  1592. 如果你想扩展新的缓动曲线,请移步 [扩展新曲线](http://transition.jiaminghi.com/guide/#扩展缓动曲线).
  1593. - [BezierCurve](https://github.com/jiaming743/bezierCurve)
  1594. 提供贝塞尔曲线支持,例如计算曲线长度,曲线折线互转。
  1595. - [Color](https://github.com/jiaming743/Color)
  1596. 提供了颜色计算,例如获取颜色的rgba值,以便于颜色动画状态的计算。