Painter.js 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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. var children = [];
  69. var bgVNode = this._bgVNode = createBackgroundVNode(width, height, this._backgroundColor, scope);
  70. bgVNode && children.push(bgVNode);
  71. var mainVNode = !opts.compress
  72. ? (this._mainVNode = createVNode('g', 'main', {}, [])) : null;
  73. this._paintList(list, scope, mainVNode ? mainVNode.children : children);
  74. mainVNode && children.push(mainVNode);
  75. var defs = map(keys(scope.defs), function (id) { return scope.defs[id]; });
  76. if (defs.length) {
  77. children.push(createVNode('defs', 'defs', {}, defs));
  78. }
  79. if (opts.animation) {
  80. var animationCssStr = getCssString(scope.cssNodes, scope.cssAnims, { newline: true });
  81. if (animationCssStr) {
  82. var styleNode = createVNode('style', 'stl', {}, [], animationCssStr);
  83. children.push(styleNode);
  84. }
  85. }
  86. return createSVGVNode(width, height, children, opts.useViewBox);
  87. };
  88. SVGPainter.prototype.renderToString = function (opts) {
  89. opts = opts || {};
  90. return vNodeToString(this.renderToVNode({
  91. animation: retrieve2(opts.cssAnimation, true),
  92. willUpdate: false,
  93. compress: true,
  94. useViewBox: retrieve2(opts.useViewBox, true)
  95. }), { newline: true });
  96. };
  97. SVGPainter.prototype.setBackgroundColor = function (backgroundColor) {
  98. this._backgroundColor = backgroundColor;
  99. };
  100. SVGPainter.prototype.getSvgRoot = function () {
  101. return this._mainVNode && this._mainVNode.elm;
  102. };
  103. SVGPainter.prototype._paintList = function (list, scope, out) {
  104. var listLen = list.length;
  105. var clipPathsGroupsStack = [];
  106. var clipPathsGroupsStackDepth = 0;
  107. var currentClipPathGroup;
  108. var prevClipPaths;
  109. var clipGroupNodeIdx = 0;
  110. for (var i = 0; i < listLen; i++) {
  111. var displayable = list[i];
  112. if (!displayable.invisible) {
  113. var clipPaths = displayable.__clipPaths;
  114. var len = clipPaths && clipPaths.length || 0;
  115. var prevLen = prevClipPaths && prevClipPaths.length || 0;
  116. var lca = void 0;
  117. for (lca = Math.max(len - 1, prevLen - 1); lca >= 0; lca--) {
  118. if (clipPaths && prevClipPaths
  119. && clipPaths[lca] === prevClipPaths[lca]) {
  120. break;
  121. }
  122. }
  123. for (var i_1 = prevLen - 1; i_1 > lca; i_1--) {
  124. clipPathsGroupsStackDepth--;
  125. currentClipPathGroup = clipPathsGroupsStack[clipPathsGroupsStackDepth - 1];
  126. }
  127. for (var i_2 = lca + 1; i_2 < len; i_2++) {
  128. var groupAttrs = {};
  129. setClipPath(clipPaths[i_2], groupAttrs, scope);
  130. var g = createVNode('g', 'clip-g-' + clipGroupNodeIdx++, groupAttrs, []);
  131. (currentClipPathGroup ? currentClipPathGroup.children : out).push(g);
  132. clipPathsGroupsStack[clipPathsGroupsStackDepth++] = g;
  133. currentClipPathGroup = g;
  134. }
  135. prevClipPaths = clipPaths;
  136. var ret = brush(displayable, scope);
  137. if (ret) {
  138. (currentClipPathGroup ? currentClipPathGroup.children : out).push(ret);
  139. }
  140. }
  141. }
  142. };
  143. SVGPainter.prototype.resize = function (width, height) {
  144. var opts = this._opts;
  145. var root = this.root;
  146. var viewport = this._viewport;
  147. width != null && (opts.width = width);
  148. height != null && (opts.height = height);
  149. if (root && viewport) {
  150. viewport.style.display = 'none';
  151. width = getSize(root, 0, opts);
  152. height = getSize(root, 1, opts);
  153. viewport.style.display = '';
  154. }
  155. if (this._width !== width || this._height !== height) {
  156. this._width = width;
  157. this._height = height;
  158. if (viewport) {
  159. var viewportStyle = viewport.style;
  160. viewportStyle.width = width + 'px';
  161. viewportStyle.height = height + 'px';
  162. }
  163. if (!isPattern(this._backgroundColor)) {
  164. var svgDom = this._svgDom;
  165. if (svgDom) {
  166. svgDom.setAttribute('width', width);
  167. svgDom.setAttribute('height', height);
  168. }
  169. var bgEl = this._bgVNode && this._bgVNode.elm;
  170. if (bgEl) {
  171. bgEl.setAttribute('width', width);
  172. bgEl.setAttribute('height', height);
  173. }
  174. }
  175. else {
  176. this.refresh();
  177. }
  178. }
  179. };
  180. SVGPainter.prototype.getWidth = function () {
  181. return this._width;
  182. };
  183. SVGPainter.prototype.getHeight = function () {
  184. return this._height;
  185. };
  186. SVGPainter.prototype.dispose = function () {
  187. if (this.root) {
  188. this.root.innerHTML = '';
  189. }
  190. this._svgDom =
  191. this._viewport =
  192. this.storage =
  193. this._oldVNode =
  194. this._bgVNode =
  195. this._mainVNode = null;
  196. };
  197. SVGPainter.prototype.clear = function () {
  198. if (this._svgDom) {
  199. this._svgDom.innerHTML = null;
  200. }
  201. this._oldVNode = null;
  202. };
  203. SVGPainter.prototype.toDataURL = function (base64) {
  204. var str = this.renderToString();
  205. var prefix = 'data:image/svg+xml;';
  206. if (base64) {
  207. str = encodeBase64(str);
  208. return str && prefix + 'base64,' + str;
  209. }
  210. return prefix + 'charset=UTF-8,' + encodeURIComponent(str);
  211. };
  212. return SVGPainter;
  213. }());
  214. function createMethodNotSupport(method) {
  215. return function () {
  216. if (process.env.NODE_ENV !== 'production') {
  217. logError('In SVG mode painter not support method "' + method + '"');
  218. }
  219. };
  220. }
  221. function createBackgroundVNode(width, height, backgroundColor, scope) {
  222. var bgVNode;
  223. if (backgroundColor && backgroundColor !== 'none') {
  224. bgVNode = createVNode('rect', 'bg', {
  225. width: width,
  226. height: height,
  227. x: '0',
  228. y: '0',
  229. id: '0'
  230. });
  231. if (isGradient(backgroundColor)) {
  232. setGradient({ fill: backgroundColor }, bgVNode.attrs, 'fill', scope);
  233. }
  234. else if (isPattern(backgroundColor)) {
  235. setPattern({
  236. style: {
  237. fill: backgroundColor
  238. },
  239. dirty: noop,
  240. getBoundingRect: function () { return ({ width: width, height: height }); }
  241. }, bgVNode.attrs, 'fill', scope);
  242. }
  243. else {
  244. var _a = normalizeColor(backgroundColor), color = _a.color, opacity = _a.opacity;
  245. bgVNode.attrs.fill = color;
  246. opacity < 1 && (bgVNode.attrs['fill-opacity'] = opacity);
  247. }
  248. }
  249. return bgVNode;
  250. }
  251. export default SVGPainter;