TransformElement.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. import Matrix from '../../3rd_party/transformation-matrix';
  2. import TransformPropertyFactory from '../../utils/TransformProperty';
  3. import effectTypes from '../../utils/helpers/effectTypes';
  4. function TransformElement() {}
  5. TransformElement.prototype = {
  6. initTransform: function () {
  7. var mat = new Matrix();
  8. this.finalTransform = {
  9. mProp: this.data.ks ? TransformPropertyFactory.getTransformProperty(this, this.data.ks, this) : { o: 0 },
  10. _matMdf: false,
  11. _localMatMdf: false,
  12. _opMdf: false,
  13. mat: mat,
  14. localMat: mat,
  15. localOpacity: 1,
  16. };
  17. if (this.data.ao) {
  18. this.finalTransform.mProp.autoOriented = true;
  19. }
  20. // TODO: check TYPE 11: Guided elements
  21. if (this.data.ty !== 11) {
  22. // this.createElements();
  23. }
  24. },
  25. renderTransform: function () {
  26. this.finalTransform._opMdf = this.finalTransform.mProp.o._mdf || this._isFirstFrame;
  27. this.finalTransform._matMdf = this.finalTransform.mProp._mdf || this._isFirstFrame;
  28. if (this.hierarchy) {
  29. var mat;
  30. var finalMat = this.finalTransform.mat;
  31. var i = 0;
  32. var len = this.hierarchy.length;
  33. // Checking if any of the transformation matrices in the hierarchy chain has changed.
  34. if (!this.finalTransform._matMdf) {
  35. while (i < len) {
  36. if (this.hierarchy[i].finalTransform.mProp._mdf) {
  37. this.finalTransform._matMdf = true;
  38. break;
  39. }
  40. i += 1;
  41. }
  42. }
  43. if (this.finalTransform._matMdf) {
  44. mat = this.finalTransform.mProp.v.props;
  45. finalMat.cloneFromProps(mat);
  46. for (i = 0; i < len; i += 1) {
  47. finalMat.multiply(this.hierarchy[i].finalTransform.mProp.v);
  48. }
  49. }
  50. }
  51. if (!this.localTransforms || this.finalTransform._matMdf) {
  52. this.finalTransform._localMatMdf = this.finalTransform._matMdf;
  53. }
  54. if (this.finalTransform._opMdf) {
  55. this.finalTransform.localOpacity = this.finalTransform.mProp.o.v;
  56. }
  57. },
  58. renderLocalTransform: function () {
  59. if (this.localTransforms) {
  60. var i = 0;
  61. var len = this.localTransforms.length;
  62. this.finalTransform._localMatMdf = this.finalTransform._matMdf;
  63. if (!this.finalTransform._localMatMdf || !this.finalTransform._opMdf) {
  64. while (i < len) {
  65. if (this.localTransforms[i]._mdf) {
  66. this.finalTransform._localMatMdf = true;
  67. }
  68. if (this.localTransforms[i]._opMdf && !this.finalTransform._opMdf) {
  69. this.finalTransform.localOpacity = this.finalTransform.mProp.o.v;
  70. this.finalTransform._opMdf = true;
  71. }
  72. i += 1;
  73. }
  74. }
  75. if (this.finalTransform._localMatMdf) {
  76. var localMat = this.finalTransform.localMat;
  77. this.localTransforms[0].matrix.clone(localMat);
  78. for (i = 1; i < len; i += 1) {
  79. var lmat = this.localTransforms[i].matrix;
  80. localMat.multiply(lmat);
  81. }
  82. localMat.multiply(this.finalTransform.mat);
  83. }
  84. if (this.finalTransform._opMdf) {
  85. var localOp = this.finalTransform.localOpacity;
  86. for (i = 0; i < len; i += 1) {
  87. localOp *= this.localTransforms[i].opacity * 0.01;
  88. }
  89. this.finalTransform.localOpacity = localOp;
  90. }
  91. }
  92. },
  93. searchEffectTransforms: function () {
  94. if (this.renderableEffectsManager) {
  95. var transformEffects = this.renderableEffectsManager.getEffects(effectTypes.TRANSFORM_EFFECT);
  96. if (transformEffects.length) {
  97. this.localTransforms = [];
  98. this.finalTransform.localMat = new Matrix();
  99. var i = 0;
  100. var len = transformEffects.length;
  101. for (i = 0; i < len; i += 1) {
  102. this.localTransforms.push(transformEffects[i]);
  103. }
  104. }
  105. }
  106. },
  107. globalToLocal: function (pt) {
  108. var transforms = [];
  109. transforms.push(this.finalTransform);
  110. var flag = true;
  111. var comp = this.comp;
  112. while (flag) {
  113. if (comp.finalTransform) {
  114. if (comp.data.hasMask) {
  115. transforms.splice(0, 0, comp.finalTransform);
  116. }
  117. comp = comp.comp;
  118. } else {
  119. flag = false;
  120. }
  121. }
  122. var i;
  123. var len = transforms.length;
  124. var ptNew;
  125. for (i = 0; i < len; i += 1) {
  126. ptNew = transforms[i].mat.applyToPointArray(0, 0, 0);
  127. // ptNew = transforms[i].mat.applyToPointArray(pt[0],pt[1],pt[2]);
  128. pt = [pt[0] - ptNew[0], pt[1] - ptNew[1], 0];
  129. }
  130. return pt;
  131. },
  132. mHelper: new Matrix(),
  133. };
  134. export default TransformElement;