SVGBaseElement.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. import { getLocationHref } from '../../main';
  2. import {
  3. createElementID,
  4. } from '../../utils/common';
  5. import createNS from '../../utils/helpers/svg_elements';
  6. import MaskElement from '../../mask';
  7. import filtersFactory from '../../utils/filters';
  8. import featureSupport from '../../utils/featureSupport';
  9. import SVGEffects from './SVGEffects';
  10. function SVGBaseElement() {
  11. }
  12. SVGBaseElement.prototype = {
  13. initRendererElement: function () {
  14. this.layerElement = createNS('g');
  15. },
  16. createContainerElements: function () {
  17. this.matteElement = createNS('g');
  18. this.transformedElement = this.layerElement;
  19. this.maskedElement = this.layerElement;
  20. this._sizeChanged = false;
  21. var layerElementParent = null;
  22. // If this layer acts as a mask for the following layer
  23. if (this.data.td) {
  24. this.matteMasks = {};
  25. var gg = createNS('g');
  26. gg.setAttribute('id', this.layerId);
  27. gg.appendChild(this.layerElement);
  28. layerElementParent = gg;
  29. this.globalData.defs.appendChild(gg);
  30. } else if (this.data.tt) {
  31. this.matteElement.appendChild(this.layerElement);
  32. layerElementParent = this.matteElement;
  33. this.baseElement = this.matteElement;
  34. } else {
  35. this.baseElement = this.layerElement;
  36. }
  37. if (this.data.ln) {
  38. this.layerElement.setAttribute('id', this.data.ln);
  39. }
  40. if (this.data.cl) {
  41. this.layerElement.setAttribute('class', this.data.cl);
  42. }
  43. // Clipping compositions to hide content that exceeds boundaries. If collapsed transformations is on, component should not be clipped
  44. if (this.data.ty === 0 && !this.data.hd) {
  45. var cp = createNS('clipPath');
  46. var pt = createNS('path');
  47. pt.setAttribute('d', 'M0,0 L' + this.data.w + ',0 L' + this.data.w + ',' + this.data.h + ' L0,' + this.data.h + 'z');
  48. var clipId = createElementID();
  49. cp.setAttribute('id', clipId);
  50. cp.appendChild(pt);
  51. this.globalData.defs.appendChild(cp);
  52. if (this.checkMasks()) {
  53. var cpGroup = createNS('g');
  54. cpGroup.setAttribute('clip-path', 'url(' + getLocationHref() + '#' + clipId + ')');
  55. cpGroup.appendChild(this.layerElement);
  56. this.transformedElement = cpGroup;
  57. if (layerElementParent) {
  58. layerElementParent.appendChild(this.transformedElement);
  59. } else {
  60. this.baseElement = this.transformedElement;
  61. }
  62. } else {
  63. this.layerElement.setAttribute('clip-path', 'url(' + getLocationHref() + '#' + clipId + ')');
  64. }
  65. }
  66. if (this.data.bm !== 0) {
  67. this.setBlendMode();
  68. }
  69. },
  70. renderElement: function () {
  71. if (this.finalTransform._localMatMdf) {
  72. this.transformedElement.setAttribute('transform', this.finalTransform.localMat.to2dCSS());
  73. }
  74. if (this.finalTransform._opMdf) {
  75. this.transformedElement.setAttribute('opacity', this.finalTransform.localOpacity);
  76. }
  77. },
  78. destroyBaseElement: function () {
  79. this.layerElement = null;
  80. this.matteElement = null;
  81. this.maskManager.destroy();
  82. },
  83. getBaseElement: function () {
  84. if (this.data.hd) {
  85. return null;
  86. }
  87. return this.baseElement;
  88. },
  89. createRenderableComponents: function () {
  90. this.maskManager = new MaskElement(this.data, this, this.globalData);
  91. this.renderableEffectsManager = new SVGEffects(this);
  92. this.searchEffectTransforms();
  93. },
  94. getMatte: function (matteType) {
  95. // This should not be a common case. But for backward compatibility, we'll create the matte object.
  96. // It solves animations that have two consecutive layers marked as matte masks.
  97. // Which is an undefined behavior in AE.
  98. if (!this.matteMasks) {
  99. this.matteMasks = {};
  100. }
  101. if (!this.matteMasks[matteType]) {
  102. var id = this.layerId + '_' + matteType;
  103. var filId;
  104. var fil;
  105. var useElement;
  106. var gg;
  107. if (matteType === 1 || matteType === 3) {
  108. var masker = createNS('mask');
  109. masker.setAttribute('id', id);
  110. masker.setAttribute('mask-type', matteType === 3 ? 'luminance' : 'alpha');
  111. useElement = createNS('use');
  112. useElement.setAttributeNS('http://www.w3.org/1999/xlink', 'href', '#' + this.layerId);
  113. masker.appendChild(useElement);
  114. this.globalData.defs.appendChild(masker);
  115. if (!featureSupport.maskType && matteType === 1) {
  116. masker.setAttribute('mask-type', 'luminance');
  117. filId = createElementID();
  118. fil = filtersFactory.createFilter(filId);
  119. this.globalData.defs.appendChild(fil);
  120. fil.appendChild(filtersFactory.createAlphaToLuminanceFilter());
  121. gg = createNS('g');
  122. gg.appendChild(useElement);
  123. masker.appendChild(gg);
  124. gg.setAttribute('filter', 'url(' + getLocationHref() + '#' + filId + ')');
  125. }
  126. } else if (matteType === 2) {
  127. var maskGroup = createNS('mask');
  128. maskGroup.setAttribute('id', id);
  129. maskGroup.setAttribute('mask-type', 'alpha');
  130. var maskGrouper = createNS('g');
  131. maskGroup.appendChild(maskGrouper);
  132. filId = createElementID();
  133. fil = filtersFactory.createFilter(filId);
  134. /// /
  135. var feCTr = createNS('feComponentTransfer');
  136. feCTr.setAttribute('in', 'SourceGraphic');
  137. fil.appendChild(feCTr);
  138. var feFunc = createNS('feFuncA');
  139. feFunc.setAttribute('type', 'table');
  140. feFunc.setAttribute('tableValues', '1.0 0.0');
  141. feCTr.appendChild(feFunc);
  142. /// /
  143. this.globalData.defs.appendChild(fil);
  144. var alphaRect = createNS('rect');
  145. alphaRect.setAttribute('width', this.comp.data.w);
  146. alphaRect.setAttribute('height', this.comp.data.h);
  147. alphaRect.setAttribute('x', '0');
  148. alphaRect.setAttribute('y', '0');
  149. alphaRect.setAttribute('fill', '#ffffff');
  150. alphaRect.setAttribute('opacity', '0');
  151. maskGrouper.setAttribute('filter', 'url(' + getLocationHref() + '#' + filId + ')');
  152. maskGrouper.appendChild(alphaRect);
  153. useElement = createNS('use');
  154. useElement.setAttributeNS('http://www.w3.org/1999/xlink', 'href', '#' + this.layerId);
  155. maskGrouper.appendChild(useElement);
  156. if (!featureSupport.maskType) {
  157. maskGroup.setAttribute('mask-type', 'luminance');
  158. fil.appendChild(filtersFactory.createAlphaToLuminanceFilter());
  159. gg = createNS('g');
  160. maskGrouper.appendChild(alphaRect);
  161. gg.appendChild(this.layerElement);
  162. maskGrouper.appendChild(gg);
  163. }
  164. this.globalData.defs.appendChild(maskGroup);
  165. }
  166. this.matteMasks[matteType] = id;
  167. }
  168. return this.matteMasks[matteType];
  169. },
  170. setMatte: function (id) {
  171. if (!this.matteElement) {
  172. return;
  173. }
  174. this.matteElement.setAttribute('mask', 'url(' + getLocationHref() + '#' + id + ')');
  175. },
  176. };
  177. export default SVGBaseElement;