animator.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. "use strict";
  2. var Easing = require('./easing');
  3. function plainArray(arr) {
  4. var result = [];
  5. for (var i = 0, len = arr.length; i < len; i++) {
  6. if (arr[i]) {
  7. result.push(arr[i].x);
  8. result.push(arr[i].y);
  9. }
  10. }
  11. return result;
  12. }
  13. function interpolateNumber(a, b) {
  14. a = +a;
  15. b -= a;
  16. return function (t) {
  17. return a + b * t;
  18. };
  19. }
  20. function interpolateArray(a, b) {
  21. var nb = b ? b.length : 0;
  22. var na = a ? Math.min(nb, a.length) : 0;
  23. var x = new Array(na);
  24. var c = new Array(nb);
  25. var i;
  26. for (i = 0; i < na; ++i) {
  27. x[i] = interpolateNumber(a[i], b[i]);
  28. }
  29. for (; i < nb; ++i) {
  30. c[i] = b[i];
  31. }
  32. return function (t) {
  33. for (i = 0; i < na; ++i) {
  34. c[i] = x[i](t);
  35. }
  36. return c;
  37. };
  38. }
  39. var Animator = /*#__PURE__*/function () {
  40. function Animator(shape, source, timeline) {
  41. this.hasStarted = false;
  42. this.hasEnded = false;
  43. this.shape = shape;
  44. this.source = source;
  45. this.timeline = timeline;
  46. this.animate = null;
  47. } // delay, attrs, duration, easing
  48. var _proto = Animator.prototype;
  49. _proto.to = function to(cfg) {
  50. if (cfg === void 0) {
  51. cfg = {};
  52. }
  53. var delay = cfg.delay || 0;
  54. var attrs = cfg.attrs || {};
  55. var duration = cfg.duration || 1000;
  56. var easing; // 缓动函数
  57. if (typeof cfg.easing === 'function') {
  58. easing = cfg.easing;
  59. } else {
  60. easing = Easing[cfg.easing] || Easing.linear;
  61. }
  62. var animInfo = {
  63. shape: this.shape,
  64. startTime: this.timeline.time + delay,
  65. duration: duration,
  66. easing: easing
  67. };
  68. var interpolate = {}; // 差值函数
  69. for (var attrName in attrs) {
  70. var startValue = this.source[attrName];
  71. var endValue = attrs[attrName];
  72. if (attrName === 'points') {
  73. startValue = plainArray(startValue);
  74. endValue = plainArray(endValue);
  75. interpolate.points = interpolateArray(startValue, endValue);
  76. this.source.points = startValue;
  77. attrs.points = endValue;
  78. } else if (attrName === 'matrix') {
  79. interpolate.matrix = interpolateArray(startValue, endValue);
  80. } else {
  81. interpolate[attrName] = interpolateNumber(startValue, endValue);
  82. }
  83. }
  84. animInfo.interpolate = interpolate;
  85. animInfo.startState = this.source;
  86. animInfo.endState = attrs;
  87. animInfo.endTime = animInfo.startTime + duration;
  88. this.timeline.anims.push(animInfo);
  89. this.animate = animInfo;
  90. return this;
  91. };
  92. _proto.onFrame = function onFrame(callback) {
  93. // 自定义每一帧动画的动作
  94. if (this.animate) {
  95. this.animate.onFrame = function (frame) {
  96. callback(frame);
  97. };
  98. }
  99. return this;
  100. };
  101. _proto.onStart = function onStart(callback) {
  102. if (this.animate) {
  103. this.animate.onStart = function () {
  104. callback();
  105. };
  106. }
  107. return this;
  108. };
  109. _proto.onUpdate = function onUpdate(callback) {
  110. if (this.animate) {
  111. this.animate.onUpdate = function (frame) {
  112. callback(frame);
  113. };
  114. }
  115. return this;
  116. };
  117. _proto.onEnd = function onEnd(callback) {
  118. if (this.animate) {
  119. this.animate.onEnd = function () {
  120. callback();
  121. };
  122. }
  123. return this;
  124. };
  125. return Animator;
  126. }();
  127. module.exports = Animator;