34bfbf796f83ebf40435570fd157ef45a2158ba47c1d2037c9690f0925d266aed82ea58b44953d0dc07370b2faf9fee6b78d07fc1020beb26836490988bb2b 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. /**
  2. * @file Manages SVG gradient elements.
  3. * @author Zhang Wenli
  4. */
  5. import Definable from './Definable';
  6. import * as zrUtil from '../../core/util';
  7. import Displayable from '../../graphic/Displayable';
  8. import { GradientObject } from '../../graphic/Gradient';
  9. import { getIdURL, isGradient, isLinearGradient, isRadialGradient, normalizeColor, round4 } from '../../svg/helper';
  10. import { createElement } from '../../svg/core';
  11. type GradientObjectExtended = GradientObject & {
  12. __dom: SVGElement
  13. }
  14. /**
  15. * Manages SVG gradient elements.
  16. *
  17. * @param zrId zrender instance id
  18. * @param svgRoot root of SVG document
  19. */
  20. export default class GradientManager extends Definable {
  21. constructor(zrId: number, svgRoot: SVGElement) {
  22. super(zrId, svgRoot, ['linearGradient', 'radialGradient'], '__gradient_in_use__');
  23. }
  24. /**
  25. * Create new gradient DOM for fill or stroke if not exist,
  26. * but will not update gradient if exists.
  27. *
  28. * @param svgElement SVG element to paint
  29. * @param displayable zrender displayable element
  30. */
  31. addWithoutUpdate(
  32. svgElement: SVGElement,
  33. displayable: Displayable
  34. ) {
  35. if (displayable && displayable.style) {
  36. const that = this;
  37. zrUtil.each(['fill', 'stroke'], function (fillOrStroke: 'fill' | 'stroke') {
  38. let value = displayable.style[fillOrStroke] as GradientObject;
  39. if (isGradient(value)) {
  40. const gradient = value as GradientObjectExtended;
  41. const defs = that.getDefs(true);
  42. // Create dom in <defs> if not exists
  43. let dom;
  44. if (gradient.__dom) {
  45. // Gradient exists
  46. dom = gradient.__dom;
  47. if (!defs.contains(gradient.__dom)) {
  48. // __dom is no longer in defs, recreate
  49. that.addDom(dom);
  50. }
  51. }
  52. else {
  53. // New dom
  54. dom = that.add(gradient);
  55. }
  56. that.markUsed(displayable);
  57. svgElement.setAttribute(fillOrStroke, getIdURL(dom.getAttribute('id')));
  58. }
  59. });
  60. }
  61. }
  62. /**
  63. * Add a new gradient tag in <defs>
  64. *
  65. * @param gradient zr gradient instance
  66. */
  67. add(gradient: GradientObject): SVGElement {
  68. let dom;
  69. if (isLinearGradient(gradient)) {
  70. dom = createElement('linearGradient');
  71. }
  72. else if (isRadialGradient(gradient)) {
  73. dom = createElement('radialGradient');
  74. }
  75. else {
  76. if (process.env.NODE_ENV !== 'production') {
  77. zrUtil.logError('Illegal gradient type.');
  78. }
  79. return null;
  80. }
  81. // Set dom id with gradient id, since each gradient instance
  82. // will have no more than one dom element.
  83. // id may exists before for those dirty elements, in which case
  84. // id should remain the same, and other attributes should be
  85. // updated.
  86. gradient.id = gradient.id || this.nextId++;
  87. dom.setAttribute('id', 'zr' + this._zrId
  88. + '-gradient-' + gradient.id);
  89. this.updateDom(gradient, dom);
  90. this.addDom(dom);
  91. return dom;
  92. }
  93. /**
  94. * Update gradient.
  95. *
  96. * @param gradient zr gradient instance or color string
  97. */
  98. update(gradient: GradientObject | string) {
  99. if (!isGradient(gradient)) {
  100. return;
  101. }
  102. const that = this;
  103. this.doUpdate(gradient, function () {
  104. const dom = (gradient as GradientObjectExtended).__dom;
  105. if (!dom) {
  106. return;
  107. }
  108. const tagName = dom.tagName;
  109. const type = gradient.type;
  110. if (type === 'linear' && tagName === 'linearGradient'
  111. || type === 'radial' && tagName === 'radialGradient'
  112. ) {
  113. // Gradient type is not changed, update gradient
  114. that.updateDom(gradient, (gradient as GradientObjectExtended).__dom);
  115. }
  116. else {
  117. // Remove and re-create if type is changed
  118. that.removeDom(gradient);
  119. that.add(gradient);
  120. }
  121. });
  122. }
  123. /**
  124. * Update gradient dom
  125. *
  126. * @param gradient zr gradient instance
  127. * @param dom DOM to update
  128. */
  129. updateDom(gradient: GradientObject, dom: SVGElement) {
  130. if (isLinearGradient(gradient)) {
  131. dom.setAttribute('x1', gradient.x as any);
  132. dom.setAttribute('y1', gradient.y as any);
  133. dom.setAttribute('x2', gradient.x2 as any);
  134. dom.setAttribute('y2', gradient.y2 as any);
  135. }
  136. else if (isRadialGradient(gradient)) {
  137. dom.setAttribute('cx', gradient.x as any);
  138. dom.setAttribute('cy', gradient.y as any);
  139. dom.setAttribute('r', gradient.r as any);
  140. }
  141. else {
  142. if (process.env.NODE_ENV !== 'production') {
  143. zrUtil.logError('Illegal gradient type.');
  144. }
  145. return;
  146. }
  147. dom.setAttribute('gradientUnits',
  148. gradient.global
  149. ? 'userSpaceOnUse' // x1, x2, y1, y2 in range of 0 to canvas width or height
  150. : 'objectBoundingBox' // x1, x2, y1, y2 in range of 0 to 1
  151. );
  152. // Remove color stops if exists
  153. dom.innerHTML = '';
  154. // Add color stops
  155. const colors = gradient.colorStops;
  156. for (let i = 0, len = colors.length; i < len; ++i) {
  157. const stop = createElement('stop');
  158. stop.setAttribute('offset', round4(colors[i].offset) * 100 + '%');
  159. const stopColor = colors[i].color;
  160. // Fix Safari bug that stop-color not recognizing alpha #9014
  161. const {color, opacity} = normalizeColor(stopColor);
  162. // stop-color cannot be color, since:
  163. // The opacity value used for the gradient calculation is the
  164. // *product* of the value of stop-opacity and the opacity of the
  165. // value of stop-color.
  166. // See https://www.w3.org/TR/SVG2/pservers.html#StopOpacityProperty
  167. stop.setAttribute('stop-color', color);
  168. if (opacity < 1) {
  169. stop.setAttribute('stop-opacity', opacity as any);
  170. }
  171. dom.appendChild(stop);
  172. }
  173. // Store dom element in gradient, to avoid creating multiple
  174. // dom instances for the same gradient element
  175. (gradient as GradientObject as GradientObjectExtended).__dom = dom;
  176. }
  177. /**
  178. * Mark a single gradient to be used
  179. *
  180. * @param displayable displayable element
  181. */
  182. markUsed(displayable: Displayable) {
  183. if (displayable.style) {
  184. let gradient = displayable.style.fill as GradientObject as GradientObjectExtended;
  185. if (gradient && gradient.__dom) {
  186. super.markDomUsed(gradient.__dom);
  187. }
  188. gradient = displayable.style.stroke as GradientObject as GradientObjectExtended;
  189. if (gradient && gradient.__dom) {
  190. super.markDomUsed(gradient.__dom);
  191. }
  192. }
  193. }
  194. }