b0cec362ee2e1564c32dcca3a9b5b0e9eed093f2c6ab5e85559b83a16566bc4cb93484983a85b9a88befbd7e49b97ff8463f5d5de99b581b201d82038225a2 13 KB

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