Painter.js 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. import { brush, setClipPath, setGradient, setPattern } from './graphic.js';
  2. import { createElement, createVNode, vNodeToString, getCssString, createBrushScope, createSVGVNode } from './core.js';
  3. import { normalizeColor, encodeBase64, isGradient, isPattern } from './helper.js';
  4. import { extend, keys, logError, map, noop, retrieve2 } from '../core/util.js';
  5. import patch, { updateAttrs } from './patch.js';
  6. import { getSize } from '../canvas/helper.js';
  7. var svgId = 0;
  8. var SVGPainter = (function () {
  9. function SVGPainter(root, storage, opts) {
  10. this.type = 'svg';
  11. this.refreshHover = createMethodNotSupport('refreshHover');
  12. this.configLayer = createMethodNotSupport('configLayer');
  13. this.storage = storage;
  14. this._opts = opts = extend({}, opts);
  15. this.root = root;
  16. this._id = 'zr' + svgId++;
  17. this._oldVNode = createSVGVNode(opts.width, opts.height);
  18. if (root && !opts.ssr) {
  19. var viewport = this._viewport = document.createElement('div');
  20. viewport.style.cssText = 'position:relative;overflow:hidden';
  21. var svgDom = this._svgDom = this._oldVNode.elm = createElement('svg');
  22. updateAttrs(null, this._oldVNode);
  23. viewport.appendChild(svgDom);
  24. root.appendChild(viewport);
  25. }
  26. this.resize(opts.width, opts.height);
  27. }
  28. SVGPainter.prototype.getType = function () {
  29. return this.type;
  30. };
  31. SVGPainter.prototype.getViewportRoot = function () {
  32. return this._viewport;
  33. };
  34. SVGPainter.prototype.getViewportRootOffset = function () {
  35. var viewportRoot = this.getViewportRoot();
  36. if (viewportRoot) {
  37. return {
  38. offsetLeft: viewportRoot.offsetLeft || 0,
  39. offsetTop: viewportRoot.offsetTop || 0
  40. };
  41. }
  42. };
  43. SVGPainter.prototype.getSvgDom = function () {
  44. return this._svgDom;
  45. };
  46. SVGPainter.prototype.refresh = function () {
  47. if (this.root) {
  48. var vnode = this.renderToVNode({
  49. willUpdate: true
  50. });
  51. vnode.attrs.style = 'position:absolute;left:0;top:0;user-select:none';
  52. patch(this._oldVNode, vnode);
  53. this._oldVNode = vnode;
  54. }
  55. };
  56. SVGPainter.prototype.renderOneToVNode = function (el) {
  57. return brush(el, createBrushScope(this._id));
  58. };
  59. SVGPainter.prototype.renderToVNode = function (opts) {
  60. opts = opts || {};
  61. var list = this.storage.getDisplayList(true);
  62. var width = this._width;
  63. var height = this._height;
  64. var scope = createBrushScope(this._id);
  65. scope.animation = opts.animation;
  66. scope.willUpdate = opts.willUpdate;
  67. scope.compress = opts.compress;
  68. scope.emphasis = opts.emphasis;
  69. var children = [];
  70. var bgVNode = this._bgVNode = createBackgroundVNode(width, height, this._backgroundColor, scope);
  71. bgVNode && children.push(bgVNode);
  72. var mainVNode = !opts.compress
  73. ? (this._mainVNode = createVNode('g', 'main', {}, [])) : null;
  74. this._paintList(list, scope, mainVNode ? mainVNode.children : children);
  75. mainVNode && children.push(mainVNode);
  76. var defs = map(keys(scope.defs), function (id) { return scope.defs[id]; });
  77. if (defs.length) {
  78. children.push(createVNode('defs', 'defs', {}, defs));
  79. }
  80. if (opts.animation) {
  81. var animationCssStr = getCssString(scope.cssNodes, scope.cssAnims, { newline: true });
  82. if (animationCssStr) {
  83. var styleNode = createVNode('style', 'stl', {}, [], animationCssStr);
  84. children.push(styleNode);
  85. }
  86. }
  87. return createSVGVNode(width, height, children, opts.useViewBox);
  88. };
  89. SVGPainter.prototype.renderToString = function (opts) {
  90. opts = opts || {};
  91. return vNodeToString(this.renderToVNode({
  92. animation: retrieve2(opts.cssAnimation, true),
  93. emphasis: retrieve2(opts.cssEmphasis, true),
  94. willUpdate: false,
  95. compress: true,
  96. useViewBox: retrieve2(opts.useViewBox, true)
  97. }), { newline: true });
  98. };
  99. SVGPainter.prototype.setBackgroundColor = function (backgroundColor) {
  100. this._backgroundColor = backgroundColor;
  101. };
  102. SVGPainter.prototype.getSvgRoot = function () {
  103. return this._mainVNode && this._mainVNode.elm;
  104. };
  105. SVGPainter.prototype._paintList = function (list, scope, out) {
  106. var listLen = list.length;
  107. var clipPathsGroupsStack = [];
  108. var clipPathsGroupsStackDepth = 0;
  109. var currentClipPathGroup;
  110. var prevClipPaths;
  111. var clipGroupNodeIdx = 0;
  112. for (var i = 0; i < listLen; i++) {
  113. var displayable = list[i];
  114. if (!displayable.invisible) {
  115. var clipPaths = displayable.__clipPaths;
  116. var len = clipPaths && clipPaths.length || 0;
  117. var prevLen = prevClipPaths && prevClipPaths.length || 0;
  118. var lca = void 0;
  119. for (lca = Math.max(len - 1, prevLen - 1); lca >= 0; lca--) {
  120. if (clipPaths && prevClipPaths
  121. && clipPaths[lca] === prevClipPaths[lca]) {
  122. break;
  123. }
  124. }
  125. for (var i_1 = prevLen - 1; i_1 > lca; i_1--) {
  126. clipPathsGroupsStackDepth--;
  127. currentClipPathGroup = clipPathsGroupsStack[clipPathsGroupsStackDepth - 1];
  128. }
  129. for (var i_2 = lca + 1; i_2 < len; i_2++) {
  130. var groupAttrs = {};
  131. setClipPath(clipPaths[i_2], groupAttrs, scope);
  132. var g = createVNode('g', 'clip-g-' + clipGroupNodeIdx++, groupAttrs, []);
  133. (currentClipPathGroup ? currentClipPathGroup.children : out).push(g);
  134. clipPathsGroupsStack[clipPathsGroupsStackDepth++] = g;
  135. currentClipPathGroup = g;
  136. }
  137. prevClipPaths = clipPaths;
  138. var ret = brush(displayable, scope);
  139. if (ret) {
  140. (currentClipPathGroup ? currentClipPathGroup.children : out).push(ret);
  141. }
  142. }
  143. }
  144. };
  145. SVGPainter.prototype.resize = function (width, height) {
  146. var opts = this._opts;
  147. var root = this.root;
  148. var viewport = this._viewport;
  149. width != null && (opts.width = width);
  150. height != null && (opts.height = height);
  151. if (root && viewport) {
  152. viewport.style.display = 'none';
  153. width = getSize(root, 0, opts);
  154. height = getSize(root, 1, opts);
  155. viewport.style.display = '';
  156. }
  157. if (this._width !== width || this._height !== height) {
  158. this._width = width;
  159. this._height = height;
  160. if (viewport) {
  161. var viewportStyle = viewport.style;
  162. viewportStyle.width = width + 'px';
  163. viewportStyle.height = height + 'px';
  164. }
  165. if (!isPattern(this._backgroundColor)) {
  166. var svgDom = this._svgDom;
  167. if (svgDom) {
  168. svgDom.setAttribute('width', width);
  169. svgDom.setAttribute('height', height);
  170. }
  171. var bgEl = this._bgVNode && this._bgVNode.elm;
  172. if (bgEl) {
  173. bgEl.setAttribute('width', width);
  174. bgEl.setAttribute('height', height);
  175. }
  176. }
  177. else {
  178. this.refresh();
  179. }
  180. }
  181. };
  182. SVGPainter.prototype.getWidth = function () {
  183. return this._width;
  184. };
  185. SVGPainter.prototype.getHeight = function () {
  186. return this._height;
  187. };
  188. SVGPainter.prototype.dispose = function () {
  189. if (this.root) {
  190. this.root.innerHTML = '';
  191. }
  192. this._svgDom =
  193. this._viewport =
  194. this.storage =
  195. this._oldVNode =
  196. this._bgVNode =
  197. this._mainVNode = null;
  198. };
  199. SVGPainter.prototype.clear = function () {
  200. if (this._svgDom) {
  201. this._svgDom.innerHTML = null;
  202. }
  203. this._oldVNode = null;
  204. };
  205. SVGPainter.prototype.toDataURL = function (base64) {
  206. var str = this.renderToString();
  207. var prefix = 'data:image/svg+xml;';
  208. if (base64) {
  209. str = encodeBase64(str);
  210. return str && prefix + 'base64,' + str;
  211. }
  212. return prefix + 'charset=UTF-8,' + encodeURIComponent(str);
  213. };
  214. return SVGPainter;
  215. }());
  216. function createMethodNotSupport(method) {
  217. return function () {
  218. if (process.env.NODE_ENV !== 'production') {
  219. logError('In SVG mode painter not support method "' + method + '"');
  220. }
  221. };
  222. }
  223. function createBackgroundVNode(width, height, backgroundColor, scope) {
  224. var bgVNode;
  225. if (backgroundColor && backgroundColor !== 'none') {
  226. bgVNode = createVNode('rect', 'bg', {
  227. width: width,
  228. height: height,
  229. x: '0',
  230. y: '0'
  231. });
  232. if (isGradient(backgroundColor)) {
  233. setGradient({ fill: backgroundColor }, bgVNode.attrs, 'fill', scope);
  234. }
  235. else if (isPattern(backgroundColor)) {
  236. setPattern({
  237. style: {
  238. fill: backgroundColor
  239. },
  240. dirty: noop,
  241. getBoundingRect: function () { return ({ width: width, height: height }); }
  242. }, bgVNode.attrs, 'fill', scope);
  243. }
  244. else {
  245. var _a = normalizeColor(backgroundColor), color = _a.color, opacity = _a.opacity;
  246. bgVNode.attrs.fill = color;
  247. opacity < 1 && (bgVNode.attrs['fill-opacity'] = opacity);
  248. }
  249. }
  250. return bgVNode;
  251. }
  252. export default SVGPainter;