63ad45558c09cb467717a0defea60be8daee0db7d0c4e050d7ae121bc5d5ebeca233883c75e79740b553d8151ec15a7308fff2f7884890fc2e75421a647ff0 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  1. import { getRgbaValue, getColorFromRgbValue } from '@jiaminghi/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. fontFamily: 'Arial',
  156. /**
  157. * @description TextAlign of Ctx
  158. * @type {String}
  159. * @default textAlign = 'center'
  160. * @example textAlign = 'start'|'end'|'left'|'right'|'center'
  161. */
  162. textAlign: 'center',
  163. /**
  164. * @description TextBaseline of Ctx
  165. * @type {String}
  166. * @default textBaseline = 'middle'
  167. * @example textBaseline = 'top'|'bottom'|'middle'|'alphabetic'|'hanging'
  168. */
  169. textBaseline: 'middle',
  170. /**
  171. * @description The color used to create the gradient
  172. * @type {Array}
  173. * @default gradientColor = null
  174. * @example gradientColor = ['#000', '#111', '#222']
  175. */
  176. gradientColor: null,
  177. /**
  178. * @description Gradient type
  179. * @type {String}
  180. * @default gradientType = 'linear'
  181. * @example gradientType = 'linear' | 'radial'
  182. */
  183. gradientType: 'linear',
  184. /**
  185. * @description Gradient params
  186. * @type {Array}
  187. * @default gradientParams = null
  188. * @example gradientParams = [x0, y0, x1, y1] (Linear Gradient)
  189. * @example gradientParams = [x0, y0, r0, x1, y1, r1] (Radial Gradient)
  190. */
  191. gradientParams: null,
  192. /**
  193. * @description When to use gradients
  194. * @type {String}
  195. * @default gradientWith = 'stroke'
  196. * @example gradientWith = 'stroke' | 'fill'
  197. */
  198. gradientWith: 'stroke',
  199. /**
  200. * @description Gradient color stops
  201. * @type {String}
  202. * @default gradientStops = 'auto'
  203. * @example gradientStops = 'auto' | [0, .2, .3, 1]
  204. */
  205. gradientStops: 'auto',
  206. /**
  207. * @description Extended color that supports animation transition
  208. * @type {Array|Object}
  209. * @default colors = null
  210. * @example colors = ['#000', '#111', '#222', 'red' ]
  211. * @example colors = { a: '#000', b: '#111' }
  212. */
  213. colors: null
  214. }
  215. Object.assign(this, defaultStyle, style)
  216. }
  217. }
  218. /**
  219. * @description Set colors to rgba value
  220. * @param {Object} style style config
  221. * @param {Boolean} reverse Whether to perform reverse operation
  222. * @return {Undefined} Void
  223. */
  224. Style.prototype.colorProcessor = function (style, reverse = false) {
  225. const processor = reverse ? getColorFromRgbValue : getRgbaValue
  226. const colorProcessorKeys = ['fill', 'stroke', 'shadowColor']
  227. const allKeys = Object.keys(style)
  228. const colorKeys = allKeys.filter(key => colorProcessorKeys.find(k => k === key))
  229. colorKeys.forEach(key => (style[key] = processor(style[key])))
  230. const { gradientColor, colors } = style
  231. if (gradientColor) style.gradientColor = gradientColor.map(c => processor(c))
  232. if (colors) {
  233. const colorsKeys = Object.keys(colors)
  234. colorsKeys.forEach(key => (colors[key] = processor(colors[key])))
  235. }
  236. }
  237. /**
  238. * @description Init graph style
  239. * @param {Object} ctx Context of canvas
  240. * @return {Undefined} Void
  241. */
  242. Style.prototype.initStyle = function (ctx) {
  243. initTransform(ctx, this)
  244. initGraphStyle(ctx, this)
  245. initGradient(ctx, this)
  246. }
  247. /**
  248. * @description Init canvas transform
  249. * @param {Object} ctx Context of canvas
  250. * @param {Style} style Instance of Style
  251. * @return {Undefined} Void
  252. */
  253. function initTransform (ctx, style) {
  254. ctx.save()
  255. const { graphCenter, rotate, scale, translate } = style
  256. if (!(graphCenter instanceof Array)) return
  257. ctx.translate(...graphCenter)
  258. if (rotate) ctx.rotate(rotate * Math.PI / 180)
  259. if (scale instanceof Array) ctx.scale(...scale)
  260. if (translate) ctx.translate(...translate)
  261. ctx.translate(-graphCenter[0], -graphCenter[1])
  262. }
  263. const autoSetStyleKeys = [
  264. 'lineCap', 'lineJoin', 'lineDashOffset',
  265. 'shadowOffsetX', 'shadowOffsetY', 'lineWidth',
  266. 'textAlign', 'textBaseline'
  267. ]
  268. /**
  269. * @description Set the style of canvas ctx
  270. * @param {Object} ctx Context of canvas
  271. * @param {Style} style Instance of Style
  272. * @return {Undefined} Void
  273. */
  274. function initGraphStyle (ctx, style) {
  275. let { fill, stroke, shadowColor, opacity } = style
  276. autoSetStyleKeys.forEach(key => {
  277. if (key || typeof key === 'number') ctx[key] = style[key]
  278. })
  279. fill = [...fill]
  280. stroke = [...stroke]
  281. shadowColor = [...shadowColor]
  282. fill[3] *= opacity
  283. stroke[3] *= opacity
  284. shadowColor[3] *= opacity
  285. ctx.fillStyle = getColorFromRgbValue(fill)
  286. ctx.strokeStyle = getColorFromRgbValue(stroke)
  287. ctx.shadowColor = getColorFromRgbValue(shadowColor)
  288. let { lineDash, shadowBlur } = style
  289. if (lineDash) {
  290. lineDash = lineDash.map(v => v >= 0 ? v : 0)
  291. ctx.setLineDash(lineDash)
  292. }
  293. if (typeof shadowBlur === 'number') ctx.shadowBlur = shadowBlur > 0 ? shadowBlur : 0.001
  294. const { fontStyle, fontVarient, fontWeight, fontSize, fontFamily } = style
  295. ctx.font = fontStyle + ' ' + fontVarient + ' ' + fontWeight + ' ' + fontSize + 'px' + ' ' + fontFamily
  296. }
  297. /**
  298. * @description Set the gradient color of canvas ctx
  299. * @param {Object} ctx Context of canvas
  300. * @param {Style} style Instance of Style
  301. * @return {Undefined} Void
  302. */
  303. function initGradient (ctx, style) {
  304. if (!gradientValidator(style)) return
  305. let { gradientColor, gradientParams, gradientType, gradientWith, gradientStops, opacity } = style
  306. gradientColor = gradientColor.map(color => {
  307. let colorOpacity = color[3] * opacity
  308. let clonedColor = [...color]
  309. clonedColor[3] = colorOpacity
  310. return clonedColor
  311. })
  312. gradientColor = gradientColor.map(c => getColorFromRgbValue(c))
  313. if (gradientStops === 'auto') gradientStops = getAutoColorStops(gradientColor)
  314. const gradient = ctx[`create${gradientType.slice(0, 1).toUpperCase() + gradientType.slice(1)}Gradient`](...gradientParams)
  315. gradientStops.forEach((stop, i) => gradient.addColorStop(stop, gradientColor[i]))
  316. ctx[`${gradientWith}Style`] = gradient
  317. }
  318. /**
  319. * @description Check if the gradient configuration is legal
  320. * @param {Style} style Instance of Style
  321. * @return {Boolean} Check Result
  322. */
  323. function gradientValidator (style) {
  324. const { gradientColor, gradientParams, gradientType, gradientWith, gradientStops } = style
  325. if (!gradientColor || !gradientParams) return false
  326. if (gradientColor.length === 1) {
  327. console.warn('The gradient needs to provide at least two colors')
  328. return false
  329. }
  330. if (gradientType !== 'linear' && gradientType !== 'radial') {
  331. console.warn('GradientType only supports linear or radial, current value is ' + gradientType)
  332. return false
  333. }
  334. const gradientParamsLength = gradientParams.length
  335. if (
  336. (gradientType === 'linear' && gradientParamsLength !== 4) ||
  337. (gradientType === 'radial' && gradientParamsLength !== 6)
  338. ) {
  339. console.warn('The expected length of gradientParams is ' + (gradientType === 'linear' ? '4' : '6'))
  340. return false
  341. }
  342. if (gradientWith !== 'fill' && gradientWith !== 'stroke') {
  343. console.warn('GradientWith only supports fill or stroke, current value is ' + gradientWith)
  344. return false
  345. }
  346. if (gradientStops !== 'auto' && !(gradientStops instanceof Array)) {
  347. console.warn(`gradientStops only supports 'auto' or Number Array ([0, .5, 1]), current value is ` + gradientStops)
  348. return false
  349. }
  350. return true
  351. }
  352. /**
  353. * @description Get a uniform gradient color stop
  354. * @param {Array} color Gradient color
  355. * @return {Array} Gradient color stop
  356. */
  357. function getAutoColorStops (color) {
  358. const stopGap = 1 / (color.length - 1)
  359. return color.map((foo, i) => stopGap * i)
  360. }
  361. /**
  362. * @description Restore canvas ctx transform
  363. * @param {Object} ctx Context of canvas
  364. * @return {Undefined} Void
  365. */
  366. Style.prototype.restoreTransform = function (ctx) {
  367. ctx.restore()
  368. }
  369. /**
  370. * @description Update style data
  371. * @param {Object} change Changed data
  372. * @return {Undefined} Void
  373. */
  374. Style.prototype.update = function (change) {
  375. this.colorProcessor(change)
  376. Object.assign(this, change)
  377. }
  378. /**
  379. * @description Get the current style configuration
  380. * @return {Object} Style configuration
  381. */
  382. Style.prototype.getStyle = function () {
  383. const clonedStyle = deepClone(this, true)
  384. this.colorProcessor(clonedStyle, true)
  385. return clonedStyle
  386. }