style.class.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467
  1. import { getRgbaValue, getColorFromRgbValue } from '../color'
  2. import { deepClone } from '../plugin/util'
  3. /**
  4. * @description Class Style
  5. * @param {Object} style Style configuration
  6. * @return {Style} Instance of Style
  7. */
  8. export default class Style {
  9. constructor (style) {
  10. this.colorProcessor(style)
  11. const defaultStyle = {
  12. /**
  13. * @description Rgba value of graph fill color
  14. * @type {Array}
  15. * @default fill = [0, 0, 0, 1]
  16. */
  17. fill: [0, 0, 0, 1],
  18. /**
  19. * @description Rgba value of graph stroke color
  20. * @type {Array}
  21. * @default stroke = [0, 0, 0, 1]
  22. */
  23. stroke: [0, 0, 0, 0],
  24. /**
  25. * @description Opacity of graph
  26. * @type {Number}
  27. * @default opacity = 1
  28. */
  29. opacity: 1,
  30. /**
  31. * @description LineCap of Ctx
  32. * @type {String}
  33. * @default lineCap = null
  34. * @example lineCap = 'butt'|'round'|'square'
  35. */
  36. lineCap: null,
  37. /**
  38. * @description Linejoin of Ctx
  39. * @type {String}
  40. * @default lineJoin = null
  41. * @example lineJoin = 'round'|'bevel'|'miter'
  42. */
  43. lineJoin: null,
  44. /**
  45. * @description LineDash of Ctx
  46. * @type {Array}
  47. * @default lineDash = null
  48. * @example lineDash = [10, 10]
  49. */
  50. lineDash: null,
  51. /**
  52. * @description LineDashOffset of Ctx
  53. * @type {Number}
  54. * @default lineDashOffset = null
  55. * @example lineDashOffset = 10
  56. */
  57. lineDashOffset: null,
  58. /**
  59. * @description ShadowBlur of Ctx
  60. * @type {Number}
  61. * @default shadowBlur = 0
  62. */
  63. shadowBlur: 0,
  64. /**
  65. * @description Rgba value of graph shadow color
  66. * @type {Array}
  67. * @default shadowColor = [0, 0, 0, 0]
  68. */
  69. shadowColor: [0, 0, 0, 0],
  70. /**
  71. * @description ShadowOffsetX of Ctx
  72. * @type {Number}
  73. * @default shadowOffsetX = 0
  74. */
  75. shadowOffsetX: 0,
  76. /**
  77. * @description ShadowOffsetY of Ctx
  78. * @type {Number}
  79. * @default shadowOffsetY = 0
  80. */
  81. shadowOffsetY: 0,
  82. /**
  83. * @description LineWidth of Ctx
  84. * @type {Number}
  85. * @default lineWidth = 0
  86. */
  87. lineWidth: 0,
  88. /**
  89. * @description Center point of the graph
  90. * @type {Array}
  91. * @default graphCenter = null
  92. * @example graphCenter = [10, 10]
  93. */
  94. graphCenter: null,
  95. /**
  96. * @description Graph scale
  97. * @type {Array}
  98. * @default scale = null
  99. * @example scale = [1.5, 1.5]
  100. */
  101. scale: null,
  102. /**
  103. * @description Graph rotation degree
  104. * @type {Number}
  105. * @default rotate = null
  106. * @example rotate = 10
  107. */
  108. rotate: null,
  109. /**
  110. * @description Graph translate distance
  111. * @type {Array}
  112. * @default translate = null
  113. * @example translate = [10, 10]
  114. */
  115. translate: null,
  116. /**
  117. * @description Cursor status when hover
  118. * @type {String}
  119. * @default hoverCursor = 'pointer'
  120. * @example hoverCursor = 'default'|'pointer'|'auto'|'crosshair'|'move'|'wait'|...
  121. */
  122. hoverCursor: 'pointer',
  123. /**
  124. * @description Font style of Ctx
  125. * @type {String}
  126. * @default fontStyle = 'normal'
  127. * @example fontStyle = 'normal'|'italic'|'oblique'
  128. */
  129. fontStyle: 'normal',
  130. /**
  131. * @description Font varient of Ctx
  132. * @type {String}
  133. * @default fontVarient = 'normal'
  134. * @example fontVarient = 'normal'|'small-caps'
  135. */
  136. fontVarient: 'normal',
  137. /**
  138. * @description Font weight of Ctx
  139. * @type {String|Number}
  140. * @default fontWeight = 'normal'
  141. * @example fontWeight = 'normal'|'bold'|'bolder'|'lighter'|Number
  142. */
  143. fontWeight: 'normal',
  144. /**
  145. * @description Font size of Ctx
  146. * @type {Number}
  147. * @default fontSize = 10
  148. */
  149. fontSize: 10,
  150. /**
  151. * @description Font family of Ctx
  152. * @type {String}
  153. * @default fontFamily = 'Arial'
  154. */
  155. padding:0,
  156. lineHeight:1,//行高
  157. wrap:true,//是否自动断行。
  158. ellipsis:false,//一行,超过省略号。
  159. letterSpacing:0,
  160. fontFamily: 'Arial',
  161. textDecoration:'none',
  162. /**
  163. * @description TextAlign of Ctx
  164. * @type {String}
  165. * @default textAlign = 'center'
  166. * @example textAlign = 'start'|'end'|'left'|'right'|'center'
  167. */
  168. textAlign: 'left',
  169. /**
  170. * @description TextBaseline of Ctx
  171. * @type {String}
  172. * @default textBaseline = 'middle'
  173. * @example textBaseline = 'top'|'bottom'|'middle'|'alphabetic'|'hanging'
  174. */
  175. textBaseline: 'middle',
  176. /**
  177. * @description The color used to create the gradient
  178. * @type {Array}
  179. * @default gradientColor = null
  180. * @example gradientColor = ['#000', '#111', '#222']
  181. */
  182. gradientColor: null,
  183. /**
  184. * @description Gradient type
  185. * @type {String}
  186. * @default gradientType = 'linear'
  187. * @example gradientType = 'linear' | 'radial'
  188. */
  189. gradientType: 'linear',
  190. /**
  191. * @description Gradient params
  192. * @type {Array}
  193. * @default gradientParams = null
  194. * @example gradientParams = [x0, y0, x1, y1] (Linear Gradient)
  195. * @example gradientParams = [x0, y0, r0, x1, y1, r1] (Radial Gradient)
  196. */
  197. gradientParams: null,
  198. /**
  199. * @description When to use gradients
  200. * @type {String}
  201. * @default gradientWith = 'stroke'
  202. * @example gradientWith = 'stroke' | 'fill'
  203. */
  204. gradientWith: 'stroke',
  205. /**
  206. * @description Gradient color stops
  207. * @type {String}
  208. * @default gradientStops = 'auto'
  209. * @example gradientStops = 'auto' | [0, .2, .3, 1]
  210. */
  211. gradientStops: 'auto',
  212. /**
  213. * @description Extended color that supports animation transition
  214. * @type {Array|Object}
  215. * @default colors = null
  216. * @example colors = ['#000', '#111', '#222', 'red' ]
  217. * @example colors = { a: '#000', b: '#111' }
  218. */
  219. colors: null
  220. }
  221. Object.assign(this, defaultStyle, style)
  222. }
  223. }
  224. /**
  225. * @description Set colors to rgba value
  226. * @param {Object} style style config
  227. * @param {Boolean} reverse Whether to perform reverse operation
  228. * @return {Undefined} Void
  229. */
  230. Style.prototype.colorProcessor = function (style, reverse = false) {
  231. const processor = reverse ? getColorFromRgbValue : getRgbaValue
  232. const colorProcessorKeys = ['fill', 'stroke', 'shadowColor']
  233. const allKeys = Object.keys(style)
  234. const colorKeys = allKeys.filter(key => colorProcessorKeys.find(k => k === key))
  235. colorKeys.forEach(key => (style[key] = processor(style[key])))
  236. const { gradientColor, colors } = style
  237. if (gradientColor) style.gradientColor = gradientColor.map(c => processor(c))
  238. if (colors) {
  239. const colorsKeys = Object.keys(colors)
  240. colorsKeys.forEach(key => (colors[key] = processor(colors[key])))
  241. }
  242. }
  243. /**
  244. * @description Init graph style
  245. * @param {Object} ctx Context of canvas
  246. * @return {Undefined} Void
  247. */
  248. Style.prototype.initStyle = function (ctx,shape) {
  249. initTransform(ctx, this)
  250. initGraphStyle(ctx, this)
  251. initGradient(ctx, this,shape)
  252. }
  253. /**
  254. * @description Init canvas transform
  255. * @param {Object} ctx Context of canvas
  256. * @param {Style} style Instance of Style
  257. * @return {Undefined} Void
  258. */
  259. function initTransform (ctx, style) {
  260. ctx.save()
  261. const { graphCenter, rotate, scale, translate } = style
  262. if (!(graphCenter instanceof Array)) return
  263. if(graphCenter.length>0) ctx.translate(...graphCenter);
  264. if (rotate) ctx.rotate(rotate * Math.PI / 180)
  265. if (scale instanceof Array) ctx.scale(...scale)
  266. if (translate) ctx.translate(...translate)
  267. ctx.translate(-graphCenter[0], -graphCenter[1])
  268. }
  269. const autoSetStyleKeys = [
  270. 'lineCap', 'lineJoin', 'lineDashOffset',
  271. 'shadowOffsetX', 'shadowOffsetY', 'lineWidth',
  272. 'textAlign', 'textBaseline'
  273. ]
  274. /**
  275. * @description Set the style of canvas ctx
  276. * @param {Object} ctx Context of canvas
  277. * @param {Style} style Instance of Style
  278. * @return {Undefined} Void
  279. */
  280. function initGraphStyle (ctx, style) {
  281. let { fill, stroke, shadowColor, opacity } = style
  282. autoSetStyleKeys.forEach(key => {
  283. if (key || typeof key === 'number') ctx[key] = style[key]
  284. })
  285. fill = [...fill]
  286. stroke = [...stroke]
  287. shadowColor = [...shadowColor]
  288. fill[3] *= opacity
  289. stroke[3] *= opacity
  290. shadowColor[3] *= opacity
  291. ctx.fillStyle = getColorFromRgbValue(fill)
  292. ctx.strokeStyle = getColorFromRgbValue(stroke)
  293. ctx.shadowColor = getColorFromRgbValue(shadowColor)
  294. let { lineDash, shadowBlur } = style
  295. if (lineDash) {
  296. lineDash = lineDash.map(v => v >= 0 ? v : 0)
  297. ctx.setLineDash(lineDash)
  298. }
  299. if (typeof shadowBlur === 'number') ctx.shadowBlur = shadowBlur > 0 ? shadowBlur : 0.001
  300. const { fontStyle, fontVarient, fontWeight, fontSize, fontFamily } = style
  301. ctx.font = fontStyle + ' ' + fontVarient + ' ' + fontWeight + ' ' + fontSize + 'px' + ' ' + fontFamily
  302. }
  303. /**
  304. * @description Set the gradient color of canvas ctx
  305. * @param {Object} ctx Context of canvas
  306. * @param {Style} style Instance of Style
  307. * @return {Undefined} Void
  308. */
  309. function initGradient (ctx, style,shape) {
  310. if (!gradientValidator(style)) return
  311. let { gradientColor, gradientParams, gradientType, gradientWith, gradientStops, opacity } = style
  312. gradientColor = gradientColor.map(color => {
  313. let colorOpacity = color[3] * opacity
  314. let clonedColor = [...color]
  315. clonedColor[3] = colorOpacity
  316. return clonedColor
  317. })
  318. gradientColor = gradientColor.map(c => getColorFromRgbValue(c))
  319. if (gradientStops === 'auto') gradientStops = getAutoColorStops(gradientColor)
  320. let gra = [...gradientParams];
  321. if(gradientType =='linear'){
  322. gra[0] =shape.x + gradientParams[0]
  323. gra[1] =shape.y + gradientParams[1]
  324. gra[2] =shape.x +gradientParams[2]
  325. gra[3] =shape.y +gradientParams[3]
  326. }
  327. const gradient = ctx[`create${gradientType.slice(0, 1).toUpperCase() + gradientType.slice(1)}Gradient`](...gra)
  328. gradientStops.forEach((stop, i) => gradient.addColorStop(stop, gradientColor[i]))
  329. ctx[`${gradientWith}Style`] = gradient
  330. }
  331. /**
  332. * @description Check if the gradient configuration is legal
  333. * @param {Style} style Instance of Style
  334. * @return {Boolean} Check Result
  335. */
  336. function gradientValidator (style) {
  337. const { gradientColor, gradientParams, gradientType, gradientWith, gradientStops } = style
  338. if (!gradientColor || !gradientParams) return false
  339. if (gradientColor.length === 1) {
  340. console.warn('The gradient needs to provide at least two colors')
  341. return false
  342. }
  343. if (gradientType !== 'linear' && gradientType !== 'radial') {
  344. console.warn('GradientType only supports linear or radial, current value is ' + gradientType)
  345. return false
  346. }
  347. const gradientParamsLength = gradientParams.length
  348. if (
  349. (gradientType === 'linear' && gradientParamsLength !== 4) ||
  350. (gradientType === 'radial' && gradientParamsLength !== 6)
  351. ) {
  352. console.warn('The expected length of gradientParams is ' + (gradientType === 'linear' ? '4' : '6'))
  353. return false
  354. }
  355. if (gradientWith !== 'fill' && gradientWith !== 'stroke') {
  356. console.warn('GradientWith only supports fill or stroke, current value is ' + gradientWith)
  357. return false
  358. }
  359. if (gradientStops !== 'auto' && !(gradientStops instanceof Array)) {
  360. console.warn(`gradientStops only supports 'auto' or Number Array ([0, .5, 1]), current value is ` + gradientStops)
  361. return false
  362. }
  363. return true
  364. }
  365. /**
  366. * @description Get a uniform gradient color stop
  367. * @param {Array} color Gradient color
  368. * @return {Array} Gradient color stop
  369. */
  370. function getAutoColorStops (color) {
  371. const stopGap = 1 / (color.length - 1)
  372. return color.map((foo, i) => stopGap * i)
  373. }
  374. /**
  375. * @description Restore canvas ctx transform
  376. * @param {Object} ctx Context of canvas
  377. * @return {Undefined} Void
  378. */
  379. Style.prototype.restoreTransform = function (ctx) {
  380. ctx.restore()
  381. }
  382. /**
  383. * @description Update style data
  384. * @param {Object} change Changed data
  385. * @return {Undefined} Void
  386. */
  387. Style.prototype.update = function (change) {
  388. this.colorProcessor(change)
  389. Object.assign(this, change)
  390. }
  391. /**
  392. * @description Get the current style configuration
  393. * @return {Object} Style configuration
  394. */
  395. Style.prototype.getStyle = function () {
  396. const clonedStyle = deepClone(this, true)
  397. this.colorProcessor(clonedStyle, true)
  398. return clonedStyle
  399. }