Style.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  1. var fixShadow = require("./helper/fixShadow");
  2. var _constant = require("./constant");
  3. var ContextCachedBy = _constant.ContextCachedBy;
  4. var STYLE_COMMON_PROPS = [['shadowBlur', 0], ['shadowOffsetX', 0], ['shadowOffsetY', 0], ['shadowColor', '#000'], ['lineCap', 'butt'], ['lineJoin', 'miter'], ['miterLimit', 10]]; // var SHADOW_PROPS = STYLE_COMMON_PROPS.slice(0, 4);
  5. // var LINE_PROPS = STYLE_COMMON_PROPS.slice(4);
  6. var Style = function (opts) {
  7. this.extendFrom(opts, false);
  8. };
  9. function createLinearGradient(ctx, obj, rect) {
  10. var x = obj.x == null ? 0 : obj.x;
  11. var x2 = obj.x2 == null ? 1 : obj.x2;
  12. var y = obj.y == null ? 0 : obj.y;
  13. var y2 = obj.y2 == null ? 0 : obj.y2;
  14. if (!obj.global) {
  15. x = x * rect.width + rect.x;
  16. x2 = x2 * rect.width + rect.x;
  17. y = y * rect.height + rect.y;
  18. y2 = y2 * rect.height + rect.y;
  19. } // Fix NaN when rect is Infinity
  20. x = isNaN(x) ? 0 : x;
  21. x2 = isNaN(x2) ? 1 : x2;
  22. y = isNaN(y) ? 0 : y;
  23. y2 = isNaN(y2) ? 0 : y2;
  24. var canvasGradient = ctx.createLinearGradient(x, y, x2, y2);
  25. return canvasGradient;
  26. }
  27. function createRadialGradient(ctx, obj, rect) {
  28. var width = rect.width;
  29. var height = rect.height;
  30. var min = Math.min(width, height);
  31. var x = obj.x == null ? 0.5 : obj.x;
  32. var y = obj.y == null ? 0.5 : obj.y;
  33. var r = obj.r == null ? 0.5 : obj.r;
  34. if (!obj.global) {
  35. x = x * width + rect.x;
  36. y = y * height + rect.y;
  37. r = r * min;
  38. }
  39. var canvasGradient = ctx.createRadialGradient(x, y, 0, x, y, r);
  40. return canvasGradient;
  41. }
  42. Style.prototype = {
  43. constructor: Style,
  44. /**
  45. * @type {string}
  46. */
  47. fill: '#000',
  48. /**
  49. * @type {string}
  50. */
  51. stroke: null,
  52. /**
  53. * @type {number}
  54. */
  55. opacity: 1,
  56. /**
  57. * @type {number}
  58. */
  59. fillOpacity: null,
  60. /**
  61. * @type {number}
  62. */
  63. strokeOpacity: null,
  64. /**
  65. * `true` is not supported.
  66. * `false`/`null`/`undefined` are the same.
  67. * `false` is used to remove lineDash in some
  68. * case that `null`/`undefined` can not be set.
  69. * (e.g., emphasis.lineStyle in echarts)
  70. * @type {Array.<number>|boolean}
  71. */
  72. lineDash: null,
  73. /**
  74. * @type {number}
  75. */
  76. lineDashOffset: 0,
  77. /**
  78. * @type {number}
  79. */
  80. shadowBlur: 0,
  81. /**
  82. * @type {number}
  83. */
  84. shadowOffsetX: 0,
  85. /**
  86. * @type {number}
  87. */
  88. shadowOffsetY: 0,
  89. /**
  90. * @type {number}
  91. */
  92. lineWidth: 1,
  93. /**
  94. * If stroke ignore scale
  95. * @type {Boolean}
  96. */
  97. strokeNoScale: false,
  98. // Bounding rect text configuration
  99. // Not affected by element transform
  100. /**
  101. * @type {string}
  102. */
  103. text: null,
  104. /**
  105. * If `fontSize` or `fontFamily` exists, `font` will be reset by
  106. * `fontSize`, `fontStyle`, `fontWeight`, `fontFamily`.
  107. * So do not visit it directly in upper application (like echarts),
  108. * but use `contain/text#makeFont` instead.
  109. * @type {string}
  110. */
  111. font: null,
  112. /**
  113. * The same as font. Use font please.
  114. * @deprecated
  115. * @type {string}
  116. */
  117. textFont: null,
  118. /**
  119. * It helps merging respectively, rather than parsing an entire font string.
  120. * @type {string}
  121. */
  122. fontStyle: null,
  123. /**
  124. * It helps merging respectively, rather than parsing an entire font string.
  125. * @type {string}
  126. */
  127. fontWeight: null,
  128. /**
  129. * It helps merging respectively, rather than parsing an entire font string.
  130. * Should be 12 but not '12px'.
  131. * @type {number}
  132. */
  133. fontSize: null,
  134. /**
  135. * It helps merging respectively, rather than parsing an entire font string.
  136. * @type {string}
  137. */
  138. fontFamily: null,
  139. /**
  140. * Reserved for special functinality, like 'hr'.
  141. * @type {string}
  142. */
  143. textTag: null,
  144. /**
  145. * @type {string}
  146. */
  147. textFill: '#000',
  148. /**
  149. * @type {string}
  150. */
  151. textStroke: null,
  152. /**
  153. * @type {number}
  154. */
  155. textWidth: null,
  156. /**
  157. * Only for textBackground.
  158. * @type {number}
  159. */
  160. textHeight: null,
  161. /**
  162. * textStroke may be set as some color as a default
  163. * value in upper applicaion, where the default value
  164. * of textStrokeWidth should be 0 to make sure that
  165. * user can choose to do not use text stroke.
  166. * @type {number}
  167. */
  168. textStrokeWidth: 0,
  169. /**
  170. * @type {number}
  171. */
  172. textLineHeight: null,
  173. /**
  174. * 'inside', 'left', 'right', 'top', 'bottom'
  175. * [x, y]
  176. * Based on x, y of rect.
  177. * @type {string|Array.<number>}
  178. * @default 'inside'
  179. */
  180. textPosition: 'inside',
  181. /**
  182. * If not specified, use the boundingRect of a `displayable`.
  183. * @type {Object}
  184. */
  185. textRect: null,
  186. /**
  187. * [x, y]
  188. * @type {Array.<number>}
  189. */
  190. textOffset: null,
  191. /**
  192. * @type {string}
  193. */
  194. textAlign: null,
  195. /**
  196. * @type {string}
  197. */
  198. textVerticalAlign: null,
  199. /**
  200. * @type {number}
  201. */
  202. textDistance: 5,
  203. /**
  204. * @type {string}
  205. */
  206. textShadowColor: 'transparent',
  207. /**
  208. * @type {number}
  209. */
  210. textShadowBlur: 0,
  211. /**
  212. * @type {number}
  213. */
  214. textShadowOffsetX: 0,
  215. /**
  216. * @type {number}
  217. */
  218. textShadowOffsetY: 0,
  219. /**
  220. * @type {string}
  221. */
  222. textBoxShadowColor: 'transparent',
  223. /**
  224. * @type {number}
  225. */
  226. textBoxShadowBlur: 0,
  227. /**
  228. * @type {number}
  229. */
  230. textBoxShadowOffsetX: 0,
  231. /**
  232. * @type {number}
  233. */
  234. textBoxShadowOffsetY: 0,
  235. /**
  236. * Whether transform text.
  237. * Only available in Path and Image element,
  238. * where the text is called as `RectText`.
  239. * @type {boolean}
  240. */
  241. transformText: false,
  242. /**
  243. * Text rotate around position of Path or Image.
  244. * The origin of the rotation can be specified by `textOrigin`.
  245. * Only available in Path and Image element,
  246. * where the text is called as `RectText`.
  247. */
  248. textRotation: 0,
  249. /**
  250. * Text origin of text rotation.
  251. * Useful in the case like label rotation of circular symbol.
  252. * Only available in Path and Image element, where the text is called
  253. * as `RectText` and the element is called as "host element".
  254. * The value can be:
  255. * + If specified as a coordinate like `[10, 40]`, it is the `[x, y]`
  256. * base on the left-top corner of the rect of its host element.
  257. * + If specified as a string `center`, it is the center of the rect of
  258. * its host element.
  259. * + By default, this origin is the `textPosition`.
  260. * @type {string|Array.<number>}
  261. */
  262. textOrigin: null,
  263. /**
  264. * @type {string}
  265. */
  266. textBackgroundColor: null,
  267. /**
  268. * @type {string}
  269. */
  270. textBorderColor: null,
  271. /**
  272. * @type {number}
  273. */
  274. textBorderWidth: 0,
  275. /**
  276. * @type {number}
  277. */
  278. textBorderRadius: 0,
  279. /**
  280. * Can be `2` or `[2, 4]` or `[2, 3, 4, 5]`
  281. * @type {number|Array.<number>}
  282. */
  283. textPadding: null,
  284. /**
  285. * Text styles for rich text.
  286. * @type {Object}
  287. */
  288. rich: null,
  289. /**
  290. * {outerWidth, outerHeight, ellipsis, placeholder}
  291. * @type {Object}
  292. */
  293. truncate: null,
  294. /**
  295. * https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/globalCompositeOperation
  296. * @type {string}
  297. */
  298. blend: null,
  299. /**
  300. * @param {CanvasRenderingContext2D} ctx
  301. */
  302. bind: function (ctx, el, prevEl) {
  303. var style = this;
  304. var prevStyle = prevEl && prevEl.style; // If no prevStyle, it means first draw.
  305. // Only apply cache if the last time cachced by this function.
  306. var notCheckCache = !prevStyle || ctx.__attrCachedBy !== ContextCachedBy.STYLE_BIND;
  307. ctx.__attrCachedBy = ContextCachedBy.STYLE_BIND;
  308. for (var i = 0; i < STYLE_COMMON_PROPS.length; i++) {
  309. var prop = STYLE_COMMON_PROPS[i];
  310. var styleName = prop[0];
  311. if (notCheckCache || style[styleName] !== prevStyle[styleName]) {
  312. // FIXME Invalid property value will cause style leak from previous element.
  313. ctx[styleName] = fixShadow(ctx, styleName, style[styleName] || prop[1]);
  314. }
  315. }
  316. if (notCheckCache || style.fill !== prevStyle.fill) {
  317. ctx.fillStyle = style.fill;
  318. }
  319. if (notCheckCache || style.stroke !== prevStyle.stroke) {
  320. ctx.strokeStyle = style.stroke;
  321. }
  322. if (notCheckCache || style.opacity !== prevStyle.opacity) {
  323. ctx.globalAlpha = style.opacity == null ? 1 : style.opacity;
  324. }
  325. if (notCheckCache || style.blend !== prevStyle.blend) {
  326. ctx.globalCompositeOperation = style.blend || 'source-over';
  327. }
  328. if (this.hasStroke()) {
  329. var lineWidth = style.lineWidth;
  330. ctx.lineWidth = lineWidth / (this.strokeNoScale && el && el.getLineScale ? el.getLineScale() : 1);
  331. }
  332. },
  333. hasFill: function () {
  334. var fill = this.fill;
  335. return fill != null && fill !== 'none';
  336. },
  337. hasStroke: function () {
  338. var stroke = this.stroke;
  339. return stroke != null && stroke !== 'none' && this.lineWidth > 0;
  340. },
  341. /**
  342. * Extend from other style
  343. * @param {zrender/graphic/Style} otherStyle
  344. * @param {boolean} overwrite true: overwrirte any way.
  345. * false: overwrite only when !target.hasOwnProperty
  346. * others: overwrite when property is not null/undefined.
  347. */
  348. extendFrom: function (otherStyle, overwrite) {
  349. if (otherStyle) {
  350. for (var name in otherStyle) {
  351. if (otherStyle.hasOwnProperty(name) && (overwrite === true || (overwrite === false ? !this.hasOwnProperty(name) : otherStyle[name] != null))) {
  352. this[name] = otherStyle[name];
  353. }
  354. }
  355. }
  356. },
  357. /**
  358. * Batch setting style with a given object
  359. * @param {Object|string} obj
  360. * @param {*} [obj]
  361. */
  362. set: function (obj, value) {
  363. if (typeof obj === 'string') {
  364. this[obj] = value;
  365. } else {
  366. this.extendFrom(obj, true);
  367. }
  368. },
  369. /**
  370. * Clone
  371. * @return {zrender/graphic/Style} [description]
  372. */
  373. clone: function () {
  374. var newStyle = new this.constructor();
  375. newStyle.extendFrom(this, true);
  376. return newStyle;
  377. },
  378. getGradient: function (ctx, obj, rect) {
  379. var method = obj.type === 'radial' ? createRadialGradient : createLinearGradient;
  380. var canvasGradient = method(ctx, obj, rect);
  381. var colorStops = obj.colorStops;
  382. for (var i = 0; i < colorStops.length; i++) {
  383. canvasGradient.addColorStop(colorStops[i].offset, colorStops[i].color);
  384. }
  385. return canvasGradient;
  386. }
  387. };
  388. var styleProto = Style.prototype;
  389. for (var i = 0; i < STYLE_COMMON_PROPS.length; i++) {
  390. var prop = STYLE_COMMON_PROPS[i];
  391. if (!(prop[0] in styleProto)) {
  392. styleProto[prop[0]] = prop[1];
  393. }
  394. } // Provide for others
  395. Style.getGradient = styleProto.getGradient;
  396. var _default = Style;
  397. module.exports = _default;