Animation.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. import { getRect, css, matrix, isRectEqual, indexOfObject } from './utils.js';
  2. import Sortable from './Sortable.js';
  3. export default function AnimationStateManager() {
  4. let animationStates = [],
  5. animationCallbackId;
  6. return {
  7. captureAnimationState() {
  8. animationStates = [];
  9. if (!this.options.animation) return;
  10. let children = [].slice.call(this.el.children);
  11. children.forEach(child => {
  12. if (css(child, 'display') === 'none' || child === Sortable.ghost) return;
  13. animationStates.push({
  14. target: child,
  15. rect: getRect(child)
  16. });
  17. let fromRect = { ...animationStates[animationStates.length - 1].rect };
  18. // If animating: compensate for current animation
  19. if (child.thisAnimationDuration) {
  20. let childMatrix = matrix(child, true);
  21. if (childMatrix) {
  22. fromRect.top -= childMatrix.f;
  23. fromRect.left -= childMatrix.e;
  24. }
  25. }
  26. child.fromRect = fromRect;
  27. });
  28. },
  29. addAnimationState(state) {
  30. animationStates.push(state);
  31. },
  32. removeAnimationState(target) {
  33. animationStates.splice(indexOfObject(animationStates, { target }), 1);
  34. },
  35. animateAll(callback) {
  36. if (!this.options.animation) {
  37. clearTimeout(animationCallbackId);
  38. if (typeof(callback) === 'function') callback();
  39. return;
  40. }
  41. let animating = false,
  42. animationTime = 0;
  43. animationStates.forEach((state) => {
  44. let time = 0,
  45. animatingThis = false,
  46. target = state.target,
  47. fromRect = target.fromRect,
  48. toRect = getRect(target),
  49. prevFromRect = target.prevFromRect,
  50. prevToRect = target.prevToRect,
  51. animatingRect = state.rect,
  52. targetMatrix = matrix(target, true);
  53. if (targetMatrix) {
  54. // Compensate for current animation
  55. toRect.top -= targetMatrix.f;
  56. toRect.left -= targetMatrix.e;
  57. }
  58. target.toRect = toRect;
  59. if (target.thisAnimationDuration) {
  60. // Could also check if animatingRect is between fromRect and toRect
  61. if (
  62. isRectEqual(prevFromRect, toRect) &&
  63. !isRectEqual(fromRect, toRect) &&
  64. // Make sure animatingRect is on line between toRect & fromRect
  65. (animatingRect.top - toRect.top) /
  66. (animatingRect.left - toRect.left) ===
  67. (fromRect.top - toRect.top) /
  68. (fromRect.left - toRect.left)
  69. ) {
  70. // If returning to same place as started from animation and on same axis
  71. time = calculateRealTime(animatingRect, prevFromRect, prevToRect, this.options);
  72. }
  73. }
  74. // if fromRect != toRect: animate
  75. if (!isRectEqual(toRect, fromRect)) {
  76. target.prevFromRect = fromRect;
  77. target.prevToRect = toRect;
  78. if (!time) {
  79. time = this.options.animation;
  80. }
  81. this.animate(
  82. target,
  83. animatingRect,
  84. toRect,
  85. time
  86. );
  87. }
  88. if (time) {
  89. animating = true;
  90. animationTime = Math.max(animationTime, time);
  91. clearTimeout(target.animationResetTimer);
  92. target.animationResetTimer = setTimeout(function() {
  93. target.animationTime = 0;
  94. target.prevFromRect = null;
  95. target.fromRect = null;
  96. target.prevToRect = null;
  97. target.thisAnimationDuration = null;
  98. }, time);
  99. target.thisAnimationDuration = time;
  100. }
  101. });
  102. clearTimeout(animationCallbackId);
  103. if (!animating) {
  104. if (typeof(callback) === 'function') callback();
  105. } else {
  106. animationCallbackId = setTimeout(function() {
  107. if (typeof(callback) === 'function') callback();
  108. }, animationTime);
  109. }
  110. animationStates = [];
  111. },
  112. animate(target, currentRect, toRect, duration) {
  113. if (duration) {
  114. css(target, 'transition', '');
  115. css(target, 'transform', '');
  116. let elMatrix = matrix(this.el),
  117. scaleX = elMatrix && elMatrix.a,
  118. scaleY = elMatrix && elMatrix.d,
  119. translateX = (currentRect.left - toRect.left) / (scaleX || 1),
  120. translateY = (currentRect.top - toRect.top) / (scaleY || 1);
  121. target.animatingX = !!translateX;
  122. target.animatingY = !!translateY;
  123. css(target, 'transform', 'translate3d(' + translateX + 'px,' + translateY + 'px,0)');
  124. repaint(target); // repaint
  125. css(target, 'transition', 'transform ' + duration + 'ms' + (this.options.easing ? ' ' + this.options.easing : ''));
  126. css(target, 'transform', 'translate3d(0,0,0)');
  127. (typeof target.animated === 'number') && clearTimeout(target.animated);
  128. target.animated = setTimeout(function () {
  129. css(target, 'transition', '');
  130. css(target, 'transform', '');
  131. target.animated = false;
  132. target.animatingX = false;
  133. target.animatingY = false;
  134. }, duration);
  135. }
  136. }
  137. };
  138. }
  139. function repaint(target) {
  140. return target.offsetWidth;
  141. }
  142. function calculateRealTime(animatingRect, fromRect, toRect, options) {
  143. return (
  144. Math.sqrt(Math.pow(fromRect.top - animatingRect.top, 2) + Math.pow(fromRect.left - animatingRect.left, 2)) /
  145. Math.sqrt(Math.pow(fromRect.top - toRect.top, 2) + Math.pow(fromRect.left - toRect.left, 2))
  146. ) * options.animation;
  147. }