SVGShapeElement.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. import {
  2. extendPrototype,
  3. } from '../../utils/functionExtensions';
  4. import { getLocationHref } from '../../main';
  5. import ShapePropertyFactory from '../../utils/shapes/ShapeProperty';
  6. import BaseElement from '../BaseElement';
  7. import TransformElement from '../helpers/TransformElement';
  8. import SVGBaseElement from './SVGBaseElement';
  9. import HierarchyElement from '../helpers/HierarchyElement';
  10. import FrameElement from '../helpers/FrameElement';
  11. import RenderableDOMElement from '../helpers/RenderableDOMElement';
  12. import getBlendMode from '../../utils/helpers/blendModes';
  13. import Matrix from '../../3rd_party/transformation-matrix';
  14. import IShapeElement from '../ShapeElement';
  15. import TransformPropertyFactory from '../../utils/TransformProperty';
  16. import { ShapeModifiers } from '../../utils/shapes/ShapeModifiers';
  17. import {
  18. lineCapEnum,
  19. lineJoinEnum,
  20. } from '../../utils/helpers/shapeEnums';
  21. import SVGShapeData from '../helpers/shapes/SVGShapeData';
  22. import SVGStyleData from '../helpers/shapes/SVGStyleData';
  23. import SVGStrokeStyleData from '../helpers/shapes/SVGStrokeStyleData';
  24. import SVGFillStyleData from '../helpers/shapes/SVGFillStyleData';
  25. import SVGNoStyleData from '../helpers/shapes/SVGNoStyleData';
  26. import SVGGradientFillStyleData from '../helpers/shapes/SVGGradientFillStyleData';
  27. import SVGGradientStrokeStyleData from '../helpers/shapes/SVGGradientStrokeStyleData';
  28. import ShapeGroupData from '../helpers/shapes/ShapeGroupData';
  29. import SVGTransformData from '../helpers/shapes/SVGTransformData';
  30. import SVGElementsRenderer from '../helpers/shapes/SVGElementsRenderer';
  31. function SVGShapeElement(data, globalData, comp) {
  32. // List of drawable elements
  33. this.shapes = [];
  34. // Full shape data
  35. this.shapesData = data.shapes;
  36. // List of styles that will be applied to shapes
  37. this.stylesList = [];
  38. // List of modifiers that will be applied to shapes
  39. this.shapeModifiers = [];
  40. // List of items in shape tree
  41. this.itemsData = [];
  42. // List of items in previous shape tree
  43. this.processedElements = [];
  44. // List of animated components
  45. this.animatedContents = [];
  46. this.initElement(data, globalData, comp);
  47. // Moving any property that doesn't get too much access after initialization because of v8 way of handling more than 10 properties.
  48. // List of elements that have been created
  49. this.prevViewData = [];
  50. // Moving any property that doesn't get too much access after initialization because of v8 way of handling more than 10 properties.
  51. }
  52. extendPrototype([BaseElement, TransformElement, SVGBaseElement, IShapeElement, HierarchyElement, FrameElement, RenderableDOMElement], SVGShapeElement);
  53. SVGShapeElement.prototype.initSecondaryElement = function () {
  54. };
  55. SVGShapeElement.prototype.identityMatrix = new Matrix();
  56. SVGShapeElement.prototype.buildExpressionInterface = function () {};
  57. SVGShapeElement.prototype.createContent = function () {
  58. this.searchShapes(this.shapesData, this.itemsData, this.prevViewData, this.layerElement, 0, [], true);
  59. this.filterUniqueShapes();
  60. };
  61. /*
  62. This method searches for multiple shapes that affect a single element and one of them is animated
  63. */
  64. SVGShapeElement.prototype.filterUniqueShapes = function () {
  65. var i;
  66. var len = this.shapes.length;
  67. var shape;
  68. var j;
  69. var jLen = this.stylesList.length;
  70. var style;
  71. var tempShapes = [];
  72. var areAnimated = false;
  73. for (j = 0; j < jLen; j += 1) {
  74. style = this.stylesList[j];
  75. areAnimated = false;
  76. tempShapes.length = 0;
  77. for (i = 0; i < len; i += 1) {
  78. shape = this.shapes[i];
  79. if (shape.styles.indexOf(style) !== -1) {
  80. tempShapes.push(shape);
  81. areAnimated = shape._isAnimated || areAnimated;
  82. }
  83. }
  84. if (tempShapes.length > 1 && areAnimated) {
  85. this.setShapesAsAnimated(tempShapes);
  86. }
  87. }
  88. };
  89. SVGShapeElement.prototype.setShapesAsAnimated = function (shapes) {
  90. var i;
  91. var len = shapes.length;
  92. for (i = 0; i < len; i += 1) {
  93. shapes[i].setAsAnimated();
  94. }
  95. };
  96. SVGShapeElement.prototype.createStyleElement = function (data, level) {
  97. // TODO: prevent drawing of hidden styles
  98. var elementData;
  99. var styleOb = new SVGStyleData(data, level);
  100. var pathElement = styleOb.pElem;
  101. if (data.ty === 'st') {
  102. elementData = new SVGStrokeStyleData(this, data, styleOb);
  103. } else if (data.ty === 'fl') {
  104. elementData = new SVGFillStyleData(this, data, styleOb);
  105. } else if (data.ty === 'gf' || data.ty === 'gs') {
  106. var GradientConstructor = data.ty === 'gf' ? SVGGradientFillStyleData : SVGGradientStrokeStyleData;
  107. elementData = new GradientConstructor(this, data, styleOb);
  108. this.globalData.defs.appendChild(elementData.gf);
  109. if (elementData.maskId) {
  110. this.globalData.defs.appendChild(elementData.ms);
  111. this.globalData.defs.appendChild(elementData.of);
  112. pathElement.setAttribute('mask', 'url(' + getLocationHref() + '#' + elementData.maskId + ')');
  113. }
  114. } else if (data.ty === 'no') {
  115. elementData = new SVGNoStyleData(this, data, styleOb);
  116. }
  117. if (data.ty === 'st' || data.ty === 'gs') {
  118. pathElement.setAttribute('stroke-linecap', lineCapEnum[data.lc || 2]);
  119. pathElement.setAttribute('stroke-linejoin', lineJoinEnum[data.lj || 2]);
  120. pathElement.setAttribute('fill-opacity', '0');
  121. if (data.lj === 1) {
  122. pathElement.setAttribute('stroke-miterlimit', data.ml);
  123. }
  124. }
  125. if (data.r === 2) {
  126. pathElement.setAttribute('fill-rule', 'evenodd');
  127. }
  128. if (data.ln) {
  129. pathElement.setAttribute('id', data.ln);
  130. }
  131. if (data.cl) {
  132. pathElement.setAttribute('class', data.cl);
  133. }
  134. if (data.bm) {
  135. pathElement.style['mix-blend-mode'] = getBlendMode(data.bm);
  136. }
  137. this.stylesList.push(styleOb);
  138. this.addToAnimatedContents(data, elementData);
  139. return elementData;
  140. };
  141. SVGShapeElement.prototype.createGroupElement = function (data) {
  142. var elementData = new ShapeGroupData();
  143. if (data.ln) {
  144. elementData.gr.setAttribute('id', data.ln);
  145. }
  146. if (data.cl) {
  147. elementData.gr.setAttribute('class', data.cl);
  148. }
  149. if (data.bm) {
  150. elementData.gr.style['mix-blend-mode'] = getBlendMode(data.bm);
  151. }
  152. return elementData;
  153. };
  154. SVGShapeElement.prototype.createTransformElement = function (data, container) {
  155. var transformProperty = TransformPropertyFactory.getTransformProperty(this, data, this);
  156. var elementData = new SVGTransformData(transformProperty, transformProperty.o, container);
  157. this.addToAnimatedContents(data, elementData);
  158. return elementData;
  159. };
  160. SVGShapeElement.prototype.createShapeElement = function (data, ownTransformers, level) {
  161. var ty = 4;
  162. if (data.ty === 'rc') {
  163. ty = 5;
  164. } else if (data.ty === 'el') {
  165. ty = 6;
  166. } else if (data.ty === 'sr') {
  167. ty = 7;
  168. }
  169. var shapeProperty = ShapePropertyFactory.getShapeProp(this, data, ty, this);
  170. var elementData = new SVGShapeData(ownTransformers, level, shapeProperty);
  171. this.shapes.push(elementData);
  172. this.addShapeToModifiers(elementData);
  173. this.addToAnimatedContents(data, elementData);
  174. return elementData;
  175. };
  176. SVGShapeElement.prototype.addToAnimatedContents = function (data, element) {
  177. var i = 0;
  178. var len = this.animatedContents.length;
  179. while (i < len) {
  180. if (this.animatedContents[i].element === element) {
  181. return;
  182. }
  183. i += 1;
  184. }
  185. this.animatedContents.push({
  186. fn: SVGElementsRenderer.createRenderFunction(data),
  187. element: element,
  188. data: data,
  189. });
  190. };
  191. SVGShapeElement.prototype.setElementStyles = function (elementData) {
  192. var arr = elementData.styles;
  193. var j;
  194. var jLen = this.stylesList.length;
  195. for (j = 0; j < jLen; j += 1) {
  196. if (arr.indexOf(this.stylesList[j]) === -1 && !this.stylesList[j].closed) {
  197. arr.push(this.stylesList[j]);
  198. }
  199. }
  200. };
  201. SVGShapeElement.prototype.reloadShapes = function () {
  202. this._isFirstFrame = true;
  203. var i;
  204. var len = this.itemsData.length;
  205. for (i = 0; i < len; i += 1) {
  206. this.prevViewData[i] = this.itemsData[i];
  207. }
  208. this.searchShapes(this.shapesData, this.itemsData, this.prevViewData, this.layerElement, 0, [], true);
  209. this.filterUniqueShapes();
  210. len = this.dynamicProperties.length;
  211. for (i = 0; i < len; i += 1) {
  212. this.dynamicProperties[i].getValue();
  213. }
  214. this.renderModifiers();
  215. };
  216. SVGShapeElement.prototype.searchShapes = function (arr, itemsData, prevViewData, container, level, transformers, render) {
  217. var ownTransformers = [].concat(transformers);
  218. var i;
  219. var len = arr.length - 1;
  220. var j;
  221. var jLen;
  222. var ownStyles = [];
  223. var ownModifiers = [];
  224. var currentTransform;
  225. var modifier;
  226. var processedPos;
  227. for (i = len; i >= 0; i -= 1) {
  228. processedPos = this.searchProcessedElement(arr[i]);
  229. if (!processedPos) {
  230. arr[i]._render = render;
  231. } else {
  232. itemsData[i] = prevViewData[processedPos - 1];
  233. }
  234. if (arr[i].ty === 'fl' || arr[i].ty === 'st' || arr[i].ty === 'gf' || arr[i].ty === 'gs' || arr[i].ty === 'no') {
  235. if (!processedPos) {
  236. itemsData[i] = this.createStyleElement(arr[i], level);
  237. } else {
  238. itemsData[i].style.closed = arr[i].hd;
  239. }
  240. if (arr[i]._render) {
  241. if (itemsData[i].style.pElem.parentNode !== container) {
  242. container.appendChild(itemsData[i].style.pElem);
  243. }
  244. }
  245. ownStyles.push(itemsData[i].style);
  246. } else if (arr[i].ty === 'gr') {
  247. if (!processedPos) {
  248. itemsData[i] = this.createGroupElement(arr[i]);
  249. } else {
  250. jLen = itemsData[i].it.length;
  251. for (j = 0; j < jLen; j += 1) {
  252. itemsData[i].prevViewData[j] = itemsData[i].it[j];
  253. }
  254. }
  255. this.searchShapes(arr[i].it, itemsData[i].it, itemsData[i].prevViewData, itemsData[i].gr, level + 1, ownTransformers, render);
  256. if (arr[i]._render) {
  257. if (itemsData[i].gr.parentNode !== container) {
  258. container.appendChild(itemsData[i].gr);
  259. }
  260. }
  261. } else if (arr[i].ty === 'tr') {
  262. if (!processedPos) {
  263. itemsData[i] = this.createTransformElement(arr[i], container);
  264. }
  265. currentTransform = itemsData[i].transform;
  266. ownTransformers.push(currentTransform);
  267. } else if (arr[i].ty === 'sh' || arr[i].ty === 'rc' || arr[i].ty === 'el' || arr[i].ty === 'sr') {
  268. if (!processedPos) {
  269. itemsData[i] = this.createShapeElement(arr[i], ownTransformers, level);
  270. }
  271. this.setElementStyles(itemsData[i]);
  272. } else if (arr[i].ty === 'tm' || arr[i].ty === 'rd' || arr[i].ty === 'ms' || arr[i].ty === 'pb' || arr[i].ty === 'zz' || arr[i].ty === 'op') {
  273. if (!processedPos) {
  274. modifier = ShapeModifiers.getModifier(arr[i].ty);
  275. modifier.init(this, arr[i]);
  276. itemsData[i] = modifier;
  277. this.shapeModifiers.push(modifier);
  278. } else {
  279. modifier = itemsData[i];
  280. modifier.closed = false;
  281. }
  282. ownModifiers.push(modifier);
  283. } else if (arr[i].ty === 'rp') {
  284. if (!processedPos) {
  285. modifier = ShapeModifiers.getModifier(arr[i].ty);
  286. itemsData[i] = modifier;
  287. modifier.init(this, arr, i, itemsData);
  288. this.shapeModifiers.push(modifier);
  289. render = false;
  290. } else {
  291. modifier = itemsData[i];
  292. modifier.closed = true;
  293. }
  294. ownModifiers.push(modifier);
  295. }
  296. this.addProcessedElement(arr[i], i + 1);
  297. }
  298. len = ownStyles.length;
  299. for (i = 0; i < len; i += 1) {
  300. ownStyles[i].closed = true;
  301. }
  302. len = ownModifiers.length;
  303. for (i = 0; i < len; i += 1) {
  304. ownModifiers[i].closed = true;
  305. }
  306. };
  307. SVGShapeElement.prototype.renderInnerContent = function () {
  308. this.renderModifiers();
  309. var i;
  310. var len = this.stylesList.length;
  311. for (i = 0; i < len; i += 1) {
  312. this.stylesList[i].reset();
  313. }
  314. this.renderShape();
  315. for (i = 0; i < len; i += 1) {
  316. if (this.stylesList[i]._mdf || this._isFirstFrame) {
  317. if (this.stylesList[i].msElem) {
  318. this.stylesList[i].msElem.setAttribute('d', this.stylesList[i].d);
  319. // Adding M0 0 fixes same mask bug on all browsers
  320. this.stylesList[i].d = 'M0 0' + this.stylesList[i].d;
  321. }
  322. this.stylesList[i].pElem.setAttribute('d', this.stylesList[i].d || 'M0 0');
  323. }
  324. }
  325. };
  326. SVGShapeElement.prototype.renderShape = function () {
  327. var i;
  328. var len = this.animatedContents.length;
  329. var animatedContent;
  330. for (i = 0; i < len; i += 1) {
  331. animatedContent = this.animatedContents[i];
  332. if ((this._isFirstFrame || animatedContent.element._isAnimated) && animatedContent.data !== true) {
  333. animatedContent.fn(animatedContent.data, animatedContent.element, this._isFirstFrame);
  334. }
  335. }
  336. };
  337. SVGShapeElement.prototype.destroy = function () {
  338. this.destroyBaseElement();
  339. this.shapesData = null;
  340. this.itemsData = null;
  341. };
  342. export default SVGShapeElement;