RepeaterModifier.js 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. import {
  2. extendPrototype,
  3. } from '../functionExtensions';
  4. import PropertyFactory from '../PropertyFactory';
  5. import Matrix from '../../3rd_party/transformation-matrix';
  6. import TransformPropertyFactory from '../TransformProperty';
  7. import {
  8. ShapeModifier,
  9. } from './ShapeModifiers';
  10. function RepeaterModifier() {}
  11. extendPrototype([ShapeModifier], RepeaterModifier);
  12. RepeaterModifier.prototype.initModifierProperties = function (elem, data) {
  13. this.getValue = this.processKeys;
  14. this.c = PropertyFactory.getProp(elem, data.c, 0, null, this);
  15. this.o = PropertyFactory.getProp(elem, data.o, 0, null, this);
  16. this.tr = TransformPropertyFactory.getTransformProperty(elem, data.tr, this);
  17. this.so = PropertyFactory.getProp(elem, data.tr.so, 0, 0.01, this);
  18. this.eo = PropertyFactory.getProp(elem, data.tr.eo, 0, 0.01, this);
  19. this.data = data;
  20. if (!this.dynamicProperties.length) {
  21. this.getValue(true);
  22. }
  23. this._isAnimated = !!this.dynamicProperties.length;
  24. this.pMatrix = new Matrix();
  25. this.rMatrix = new Matrix();
  26. this.sMatrix = new Matrix();
  27. this.tMatrix = new Matrix();
  28. this.matrix = new Matrix();
  29. };
  30. RepeaterModifier.prototype.applyTransforms = function (pMatrix, rMatrix, sMatrix, transform, perc, inv) {
  31. var dir = inv ? -1 : 1;
  32. var scaleX = transform.s.v[0] + (1 - transform.s.v[0]) * (1 - perc);
  33. var scaleY = transform.s.v[1] + (1 - transform.s.v[1]) * (1 - perc);
  34. pMatrix.translate(transform.p.v[0] * dir * perc, transform.p.v[1] * dir * perc, transform.p.v[2]);
  35. rMatrix.translate(-transform.a.v[0], -transform.a.v[1], transform.a.v[2]);
  36. rMatrix.rotate(-transform.r.v * dir * perc);
  37. rMatrix.translate(transform.a.v[0], transform.a.v[1], transform.a.v[2]);
  38. sMatrix.translate(-transform.a.v[0], -transform.a.v[1], transform.a.v[2]);
  39. sMatrix.scale(inv ? 1 / scaleX : scaleX, inv ? 1 / scaleY : scaleY);
  40. sMatrix.translate(transform.a.v[0], transform.a.v[1], transform.a.v[2]);
  41. };
  42. RepeaterModifier.prototype.init = function (elem, arr, pos, elemsData) {
  43. this.elem = elem;
  44. this.arr = arr;
  45. this.pos = pos;
  46. this.elemsData = elemsData;
  47. this._currentCopies = 0;
  48. this._elements = [];
  49. this._groups = [];
  50. this.frameId = -1;
  51. this.initDynamicPropertyContainer(elem);
  52. this.initModifierProperties(elem, arr[pos]);
  53. while (pos > 0) {
  54. pos -= 1;
  55. // this._elements.unshift(arr.splice(pos,1)[0]);
  56. this._elements.unshift(arr[pos]);
  57. }
  58. if (this.dynamicProperties.length) {
  59. this.k = true;
  60. } else {
  61. this.getValue(true);
  62. }
  63. };
  64. RepeaterModifier.prototype.resetElements = function (elements) {
  65. var i;
  66. var len = elements.length;
  67. for (i = 0; i < len; i += 1) {
  68. elements[i]._processed = false;
  69. if (elements[i].ty === 'gr') {
  70. this.resetElements(elements[i].it);
  71. }
  72. }
  73. };
  74. RepeaterModifier.prototype.cloneElements = function (elements) {
  75. var newElements = JSON.parse(JSON.stringify(elements));
  76. this.resetElements(newElements);
  77. return newElements;
  78. };
  79. RepeaterModifier.prototype.changeGroupRender = function (elements, renderFlag) {
  80. var i;
  81. var len = elements.length;
  82. for (i = 0; i < len; i += 1) {
  83. elements[i]._render = renderFlag;
  84. if (elements[i].ty === 'gr') {
  85. this.changeGroupRender(elements[i].it, renderFlag);
  86. }
  87. }
  88. };
  89. RepeaterModifier.prototype.processShapes = function (_isFirstFrame) {
  90. var items;
  91. var itemsTransform;
  92. var i;
  93. var dir;
  94. var cont;
  95. var hasReloaded = false;
  96. if (this._mdf || _isFirstFrame) {
  97. var copies = Math.ceil(this.c.v);
  98. if (this._groups.length < copies) {
  99. while (this._groups.length < copies) {
  100. var group = {
  101. it: this.cloneElements(this._elements),
  102. ty: 'gr',
  103. };
  104. group.it.push({
  105. a: { a: 0, ix: 1, k: [0, 0] }, nm: 'Transform', o: { a: 0, ix: 7, k: 100 }, p: { a: 0, ix: 2, k: [0, 0] }, r: { a: 1, ix: 6, k: [{ s: 0, e: 0, t: 0 }, { s: 0, e: 0, t: 1 }] }, s: { a: 0, ix: 3, k: [100, 100] }, sa: { a: 0, ix: 5, k: 0 }, sk: { a: 0, ix: 4, k: 0 }, ty: 'tr',
  106. });
  107. this.arr.splice(0, 0, group);
  108. this._groups.splice(0, 0, group);
  109. this._currentCopies += 1;
  110. }
  111. this.elem.reloadShapes();
  112. hasReloaded = true;
  113. }
  114. cont = 0;
  115. var renderFlag;
  116. for (i = 0; i <= this._groups.length - 1; i += 1) {
  117. renderFlag = cont < copies;
  118. this._groups[i]._render = renderFlag;
  119. this.changeGroupRender(this._groups[i].it, renderFlag);
  120. if (!renderFlag) {
  121. var elems = this.elemsData[i].it;
  122. var transformData = elems[elems.length - 1];
  123. if (transformData.transform.op.v !== 0) {
  124. transformData.transform.op._mdf = true;
  125. transformData.transform.op.v = 0;
  126. } else {
  127. transformData.transform.op._mdf = false;
  128. }
  129. }
  130. cont += 1;
  131. }
  132. this._currentCopies = copies;
  133. /// /
  134. var offset = this.o.v;
  135. var offsetModulo = offset % 1;
  136. var roundOffset = offset > 0 ? Math.floor(offset) : Math.ceil(offset);
  137. var pProps = this.pMatrix.props;
  138. var rProps = this.rMatrix.props;
  139. var sProps = this.sMatrix.props;
  140. this.pMatrix.reset();
  141. this.rMatrix.reset();
  142. this.sMatrix.reset();
  143. this.tMatrix.reset();
  144. this.matrix.reset();
  145. var iteration = 0;
  146. if (offset > 0) {
  147. while (iteration < roundOffset) {
  148. this.applyTransforms(this.pMatrix, this.rMatrix, this.sMatrix, this.tr, 1, false);
  149. iteration += 1;
  150. }
  151. if (offsetModulo) {
  152. this.applyTransforms(this.pMatrix, this.rMatrix, this.sMatrix, this.tr, offsetModulo, false);
  153. iteration += offsetModulo;
  154. }
  155. } else if (offset < 0) {
  156. while (iteration > roundOffset) {
  157. this.applyTransforms(this.pMatrix, this.rMatrix, this.sMatrix, this.tr, 1, true);
  158. iteration -= 1;
  159. }
  160. if (offsetModulo) {
  161. this.applyTransforms(this.pMatrix, this.rMatrix, this.sMatrix, this.tr, -offsetModulo, true);
  162. iteration -= offsetModulo;
  163. }
  164. }
  165. i = this.data.m === 1 ? 0 : this._currentCopies - 1;
  166. dir = this.data.m === 1 ? 1 : -1;
  167. cont = this._currentCopies;
  168. var j;
  169. var jLen;
  170. while (cont) {
  171. items = this.elemsData[i].it;
  172. itemsTransform = items[items.length - 1].transform.mProps.v.props;
  173. jLen = itemsTransform.length;
  174. items[items.length - 1].transform.mProps._mdf = true;
  175. items[items.length - 1].transform.op._mdf = true;
  176. items[items.length - 1].transform.op.v = this._currentCopies === 1
  177. ? this.so.v
  178. : this.so.v + (this.eo.v - this.so.v) * (i / (this._currentCopies - 1));
  179. if (iteration !== 0) {
  180. if ((i !== 0 && dir === 1) || (i !== this._currentCopies - 1 && dir === -1)) {
  181. this.applyTransforms(this.pMatrix, this.rMatrix, this.sMatrix, this.tr, 1, false);
  182. }
  183. this.matrix.transform(rProps[0], rProps[1], rProps[2], rProps[3], rProps[4], rProps[5], rProps[6], rProps[7], rProps[8], rProps[9], rProps[10], rProps[11], rProps[12], rProps[13], rProps[14], rProps[15]);
  184. this.matrix.transform(sProps[0], sProps[1], sProps[2], sProps[3], sProps[4], sProps[5], sProps[6], sProps[7], sProps[8], sProps[9], sProps[10], sProps[11], sProps[12], sProps[13], sProps[14], sProps[15]);
  185. this.matrix.transform(pProps[0], pProps[1], pProps[2], pProps[3], pProps[4], pProps[5], pProps[6], pProps[7], pProps[8], pProps[9], pProps[10], pProps[11], pProps[12], pProps[13], pProps[14], pProps[15]);
  186. for (j = 0; j < jLen; j += 1) {
  187. itemsTransform[j] = this.matrix.props[j];
  188. }
  189. this.matrix.reset();
  190. } else {
  191. this.matrix.reset();
  192. for (j = 0; j < jLen; j += 1) {
  193. itemsTransform[j] = this.matrix.props[j];
  194. }
  195. }
  196. iteration += 1;
  197. cont -= 1;
  198. i += dir;
  199. }
  200. } else {
  201. cont = this._currentCopies;
  202. i = 0;
  203. dir = 1;
  204. while (cont) {
  205. items = this.elemsData[i].it;
  206. itemsTransform = items[items.length - 1].transform.mProps.v.props;
  207. items[items.length - 1].transform.mProps._mdf = false;
  208. items[items.length - 1].transform.op._mdf = false;
  209. cont -= 1;
  210. i += dir;
  211. }
  212. }
  213. return hasReloaded;
  214. };
  215. RepeaterModifier.prototype.addShape = function () {};
  216. export default RepeaterModifier;