createAnimation.js 3.1 KB

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