createAnimation.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. "use strict";
  2. const common_vendor = require("../../../../common/vendor.js");
  3. class MPAnimation {
  4. constructor(options, _this) {
  5. this.options = options;
  6. this.animation = common_vendor.index.createAnimation(options);
  7. this.currentStepAnimates = {};
  8. this.next = 0;
  9. this.$ = _this;
  10. }
  11. _nvuePushAnimates(type, args) {
  12. let aniObj = this.currentStepAnimates[this.next];
  13. let styles = {};
  14. if (!aniObj) {
  15. styles = {
  16. styles: {},
  17. config: {}
  18. };
  19. } else {
  20. styles = aniObj;
  21. }
  22. if (animateTypes1.includes(type)) {
  23. if (!styles.styles.transform) {
  24. styles.styles.transform = "";
  25. }
  26. let unit = "";
  27. if (type === "rotate") {
  28. unit = "deg";
  29. }
  30. styles.styles.transform += `${type}(${args + unit}) `;
  31. } else {
  32. styles.styles[type] = `${args}`;
  33. }
  34. this.currentStepAnimates[this.next] = styles;
  35. }
  36. _animateRun(styles = {}, config = {}) {
  37. let ref = this.$.$refs["ani"].ref;
  38. if (!ref)
  39. return;
  40. return new Promise((resolve, reject) => {
  41. nvueAnimation.transition(ref, {
  42. styles,
  43. ...config
  44. }, (res) => {
  45. resolve();
  46. });
  47. });
  48. }
  49. _nvueNextAnimate(animates, step = 0, fn) {
  50. let obj = animates[step];
  51. if (obj) {
  52. let {
  53. styles,
  54. config
  55. } = obj;
  56. this._animateRun(styles, config).then(() => {
  57. step += 1;
  58. this._nvueNextAnimate(animates, step, fn);
  59. });
  60. } else {
  61. this.currentStepAnimates = {};
  62. typeof fn === "function" && fn();
  63. this.isEnd = true;
  64. }
  65. }
  66. step(config = {}) {
  67. this.animation.step(config);
  68. return this;
  69. }
  70. run(fn) {
  71. this.$.animationData = this.animation.export();
  72. this.$.timer = setTimeout(() => {
  73. typeof fn === "function" && fn();
  74. }, this.$.durationTime);
  75. }
  76. }
  77. const animateTypes1 = [
  78. "matrix",
  79. "matrix3d",
  80. "rotate",
  81. "rotate3d",
  82. "rotateX",
  83. "rotateY",
  84. "rotateZ",
  85. "scale",
  86. "scale3d",
  87. "scaleX",
  88. "scaleY",
  89. "scaleZ",
  90. "skew",
  91. "skewX",
  92. "skewY",
  93. "translate",
  94. "translate3d",
  95. "translateX",
  96. "translateY",
  97. "translateZ"
  98. ];
  99. const animateTypes2 = ["opacity", "backgroundColor"];
  100. const animateTypes3 = ["width", "height", "left", "right", "top", "bottom"];
  101. animateTypes1.concat(animateTypes2, animateTypes3).forEach((type) => {
  102. MPAnimation.prototype[type] = function(...args) {
  103. this.animation[type](...args);
  104. return this;
  105. };
  106. });
  107. function createAnimation(option, _this) {
  108. if (!_this)
  109. return;
  110. clearTimeout(_this.timer);
  111. return new MPAnimation(option, _this);
  112. }
  113. exports.createAnimation = createAnimation;