Painter.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. import { createElement, normalizeColor } from './core';
  2. import * as util from '../core/util';
  3. import Path from '../graphic/Path';
  4. import ZRImage from '../graphic/Image';
  5. import TSpan from '../graphic/TSpan';
  6. import arrayDiff from '../core/arrayDiff';
  7. import GradientManager from './helper/GradientManager';
  8. import PatternManager from './helper/PatternManager';
  9. import ClippathManager, { hasClipPath } from './helper/ClippathManager';
  10. import ShadowManager from './helper/ShadowManager';
  11. import { path as svgPath, image as svgImage, text as svgText } from './graphic';
  12. function parseInt10(val) {
  13. return parseInt(val, 10);
  14. }
  15. function getSvgProxy(el) {
  16. if (el instanceof Path) {
  17. return svgPath;
  18. }
  19. else if (el instanceof ZRImage) {
  20. return svgImage;
  21. }
  22. else if (el instanceof TSpan) {
  23. return svgText;
  24. }
  25. else {
  26. return svgPath;
  27. }
  28. }
  29. function checkParentAvailable(parent, child) {
  30. return child && parent && child.parentNode !== parent;
  31. }
  32. function insertAfter(parent, child, prevSibling) {
  33. if (checkParentAvailable(parent, child) && prevSibling) {
  34. var nextSibling = prevSibling.nextSibling;
  35. nextSibling ? parent.insertBefore(child, nextSibling)
  36. : parent.appendChild(child);
  37. }
  38. }
  39. function prepend(parent, child) {
  40. if (checkParentAvailable(parent, child)) {
  41. var firstChild = parent.firstChild;
  42. firstChild ? parent.insertBefore(child, firstChild)
  43. : parent.appendChild(child);
  44. }
  45. }
  46. function remove(parent, child) {
  47. if (child && parent && child.parentNode === parent) {
  48. parent.removeChild(child);
  49. }
  50. }
  51. function removeFromMyParent(child) {
  52. if (child && child.parentNode) {
  53. child.parentNode.removeChild(child);
  54. }
  55. }
  56. function getSvgElement(displayable) {
  57. return displayable.__svgEl;
  58. }
  59. var SVGPainter = (function () {
  60. function SVGPainter(root, storage, opts, zrId) {
  61. this.type = 'svg';
  62. this.refreshHover = createMethodNotSupport('refreshHover');
  63. this.pathToImage = createMethodNotSupport('pathToImage');
  64. this.configLayer = createMethodNotSupport('configLayer');
  65. this.root = root;
  66. this.storage = storage;
  67. this._opts = opts = util.extend({}, opts || {});
  68. var svgDom = createElement('svg');
  69. svgDom.setAttributeNS('http://www.w3.org/2000/xmlns/', 'xmlns', 'http://www.w3.org/2000/svg');
  70. svgDom.setAttributeNS('http://www.w3.org/2000/xmlns/', 'xmlns:xlink', 'http://www.w3.org/1999/xlink');
  71. svgDom.setAttribute('version', '1.1');
  72. svgDom.setAttribute('baseProfile', 'full');
  73. svgDom.style.cssText = 'user-select:none;position:absolute;left:0;top:0;';
  74. var bgRoot = createElement('g');
  75. svgDom.appendChild(bgRoot);
  76. var svgRoot = createElement('g');
  77. svgDom.appendChild(svgRoot);
  78. this._gradientManager = new GradientManager(zrId, svgRoot);
  79. this._patternManager = new PatternManager(zrId, svgRoot);
  80. this._clipPathManager = new ClippathManager(zrId, svgRoot);
  81. this._shadowManager = new ShadowManager(zrId, svgRoot);
  82. var viewport = document.createElement('div');
  83. viewport.style.cssText = 'overflow:hidden;position:relative';
  84. this._svgDom = svgDom;
  85. this._svgRoot = svgRoot;
  86. this._backgroundRoot = bgRoot;
  87. this._viewport = viewport;
  88. root.appendChild(viewport);
  89. viewport.appendChild(svgDom);
  90. this.resize(opts.width, opts.height);
  91. this._visibleList = [];
  92. }
  93. SVGPainter.prototype.getType = function () {
  94. return 'svg';
  95. };
  96. SVGPainter.prototype.getViewportRoot = function () {
  97. return this._viewport;
  98. };
  99. SVGPainter.prototype.getSvgDom = function () {
  100. return this._svgDom;
  101. };
  102. SVGPainter.prototype.getSvgRoot = function () {
  103. return this._svgRoot;
  104. };
  105. SVGPainter.prototype.getViewportRootOffset = function () {
  106. var viewportRoot = this.getViewportRoot();
  107. if (viewportRoot) {
  108. return {
  109. offsetLeft: viewportRoot.offsetLeft || 0,
  110. offsetTop: viewportRoot.offsetTop || 0
  111. };
  112. }
  113. };
  114. SVGPainter.prototype.refresh = function () {
  115. var list = this.storage.getDisplayList(true);
  116. this._paintList(list);
  117. };
  118. SVGPainter.prototype.setBackgroundColor = function (backgroundColor) {
  119. if (this._backgroundRoot && this._backgroundNode) {
  120. this._backgroundRoot.removeChild(this._backgroundNode);
  121. }
  122. var bgNode = createElement('rect');
  123. bgNode.setAttribute('width', this.getWidth());
  124. bgNode.setAttribute('height', this.getHeight());
  125. bgNode.setAttribute('x', 0);
  126. bgNode.setAttribute('y', 0);
  127. bgNode.setAttribute('id', 0);
  128. var _a = normalizeColor(backgroundColor), color = _a.color, opacity = _a.opacity;
  129. bgNode.setAttribute('fill', color);
  130. bgNode.setAttribute('fill-opacity', opacity);
  131. this._backgroundRoot.appendChild(bgNode);
  132. this._backgroundNode = bgNode;
  133. };
  134. SVGPainter.prototype.createSVGElement = function (tag) {
  135. return createElement(tag);
  136. };
  137. SVGPainter.prototype.paintOne = function (el) {
  138. var svgProxy = getSvgProxy(el);
  139. svgProxy && svgProxy.brush(el);
  140. return getSvgElement(el);
  141. };
  142. SVGPainter.prototype._paintList = function (list) {
  143. var gradientManager = this._gradientManager;
  144. var patternManager = this._patternManager;
  145. var clipPathManager = this._clipPathManager;
  146. var shadowManager = this._shadowManager;
  147. gradientManager.markAllUnused();
  148. patternManager.markAllUnused();
  149. clipPathManager.markAllUnused();
  150. shadowManager.markAllUnused();
  151. var svgRoot = this._svgRoot;
  152. var visibleList = this._visibleList;
  153. var listLen = list.length;
  154. var newVisibleList = [];
  155. for (var i = 0; i < listLen; i++) {
  156. var displayable = list[i];
  157. var svgProxy = getSvgProxy(displayable);
  158. var svgElement = getSvgElement(displayable);
  159. if (!displayable.invisible) {
  160. if (displayable.__dirty || !svgElement) {
  161. svgProxy && svgProxy.brush(displayable);
  162. svgElement = getSvgElement(displayable);
  163. if (svgElement && displayable.style) {
  164. gradientManager.update(displayable.style.fill);
  165. gradientManager.update(displayable.style.stroke);
  166. patternManager.update(displayable.style.fill);
  167. patternManager.update(displayable.style.stroke);
  168. shadowManager.update(svgElement, displayable);
  169. }
  170. displayable.__dirty = 0;
  171. }
  172. if (svgElement) {
  173. newVisibleList.push(displayable);
  174. }
  175. }
  176. }
  177. var diff = arrayDiff(visibleList, newVisibleList);
  178. var prevSvgElement;
  179. var topPrevSvgElement;
  180. for (var i = 0; i < diff.length; i++) {
  181. var item = diff[i];
  182. if (item.removed) {
  183. for (var k = 0; k < item.count; k++) {
  184. var displayable = visibleList[item.indices[k]];
  185. var svgElement = getSvgElement(displayable);
  186. hasClipPath(displayable) ? removeFromMyParent(svgElement)
  187. : remove(svgRoot, svgElement);
  188. }
  189. }
  190. }
  191. var prevDisplayable;
  192. var currentClipGroup;
  193. for (var i = 0; i < diff.length; i++) {
  194. var item = diff[i];
  195. if (item.removed) {
  196. continue;
  197. }
  198. for (var k = 0; k < item.count; k++) {
  199. var displayable = newVisibleList[item.indices[k]];
  200. var clipGroup = clipPathManager.update(displayable, prevDisplayable);
  201. if (clipGroup !== currentClipGroup) {
  202. prevSvgElement = topPrevSvgElement;
  203. if (clipGroup) {
  204. prevSvgElement ? insertAfter(svgRoot, clipGroup, prevSvgElement)
  205. : prepend(svgRoot, clipGroup);
  206. topPrevSvgElement = clipGroup;
  207. prevSvgElement = null;
  208. }
  209. currentClipGroup = clipGroup;
  210. }
  211. var svgElement = getSvgElement(displayable);
  212. prevSvgElement
  213. ? insertAfter(currentClipGroup || svgRoot, svgElement, prevSvgElement)
  214. : prepend(currentClipGroup || svgRoot, svgElement);
  215. prevSvgElement = svgElement || prevSvgElement;
  216. if (!currentClipGroup) {
  217. topPrevSvgElement = prevSvgElement;
  218. }
  219. gradientManager.markUsed(displayable);
  220. gradientManager.addWithoutUpdate(svgElement, displayable);
  221. patternManager.markUsed(displayable);
  222. patternManager.addWithoutUpdate(svgElement, displayable);
  223. clipPathManager.markUsed(displayable);
  224. prevDisplayable = displayable;
  225. }
  226. }
  227. gradientManager.removeUnused();
  228. patternManager.removeUnused();
  229. clipPathManager.removeUnused();
  230. shadowManager.removeUnused();
  231. this._visibleList = newVisibleList;
  232. };
  233. SVGPainter.prototype.resize = function (width, height) {
  234. var viewport = this._viewport;
  235. viewport.style.display = 'none';
  236. var opts = this._opts;
  237. width != null && (opts.width = width);
  238. height != null && (opts.height = height);
  239. width = this._getSize(0);
  240. height = this._getSize(1);
  241. viewport.style.display = '';
  242. if (this._width !== width || this._height !== height) {
  243. this._width = width;
  244. this._height = height;
  245. var viewportStyle = viewport.style;
  246. viewportStyle.width = width + 'px';
  247. viewportStyle.height = height + 'px';
  248. var svgRoot = this._svgDom;
  249. svgRoot.setAttribute('width', width + '');
  250. svgRoot.setAttribute('height', height + '');
  251. }
  252. if (this._backgroundNode) {
  253. this._backgroundNode.setAttribute('width', width);
  254. this._backgroundNode.setAttribute('height', height);
  255. }
  256. };
  257. SVGPainter.prototype.getWidth = function () {
  258. return this._width;
  259. };
  260. SVGPainter.prototype.getHeight = function () {
  261. return this._height;
  262. };
  263. SVGPainter.prototype._getSize = function (whIdx) {
  264. var opts = this._opts;
  265. var wh = ['width', 'height'][whIdx];
  266. var cwh = ['clientWidth', 'clientHeight'][whIdx];
  267. var plt = ['paddingLeft', 'paddingTop'][whIdx];
  268. var prb = ['paddingRight', 'paddingBottom'][whIdx];
  269. if (opts[wh] != null && opts[wh] !== 'auto') {
  270. return parseFloat(opts[wh]);
  271. }
  272. var root = this.root;
  273. var stl = document.defaultView.getComputedStyle(root);
  274. return ((root[cwh] || parseInt10(stl[wh]) || parseInt10(root.style[wh]))
  275. - (parseInt10(stl[plt]) || 0)
  276. - (parseInt10(stl[prb]) || 0)) | 0;
  277. };
  278. SVGPainter.prototype.dispose = function () {
  279. this.root.innerHTML = '';
  280. this._svgRoot =
  281. this._backgroundRoot =
  282. this._svgDom =
  283. this._backgroundNode =
  284. this._viewport = this.storage = null;
  285. };
  286. SVGPainter.prototype.clear = function () {
  287. var viewportNode = this._viewport;
  288. if (viewportNode && viewportNode.parentNode) {
  289. viewportNode.parentNode.removeChild(viewportNode);
  290. }
  291. };
  292. SVGPainter.prototype.toDataURL = function () {
  293. this.refresh();
  294. var svgDom = this._svgDom;
  295. var outerHTML = svgDom.outerHTML
  296. || (svgDom.parentNode && svgDom.parentNode).innerHTML;
  297. var html = encodeURIComponent(outerHTML.replace(/></g, '>\n\r<'));
  298. return 'data:image/svg+xml;charset=UTF-8,' + html;
  299. };
  300. return SVGPainter;
  301. }());
  302. function createMethodNotSupport(method) {
  303. return function () {
  304. util.logError('In SVG mode painter not support method "' + method + '"');
  305. };
  306. }
  307. export default SVGPainter;