x-lottie.vue 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. <template>
  2. <view @click="$emit('click')">
  3. <view class="lottie-con" ref="lottie">
  4. </view>
  5. <view :prop="option" :callMethod="callMethod" :change:callMethod="Lottie.changeMethod"
  6. :change:prop="Lottie.update">
  7. </view>
  8. </view>
  9. </template>
  10. <script>
  11. export default {
  12. props: {
  13. option: {
  14. default: null
  15. }
  16. },
  17. data() {
  18. return {
  19. callMethod: {
  20. name: "play",
  21. args: []
  22. }
  23. }
  24. },
  25. methods: {
  26. stop() {
  27. this.$set(this.callMethod, 'name', 'stop')
  28. this.$set(this.callMethod, 'args', [])
  29. },
  30. play() {
  31. this.$set(this.callMethod, 'name', 'play')
  32. this.$set(this.callMethod, 'args', [])
  33. },
  34. pause() {
  35. this.$set(this.callMethod, 'name', 'pause')
  36. this.$set(this.callMethod, 'args', [])
  37. },
  38. setSpeed(speed) {
  39. this.$set(this.callMethod, 'name', 'setSpeed')
  40. this.$set(this.callMethod, 'args', [speed || 1])
  41. },
  42. goToAndStop(value, isFrame) {
  43. this.$set(this.callMethod, 'name', 'goToAndStop')
  44. this.$set(this.callMethod, 'args', [value, isFrame || false])
  45. },
  46. goToAndPlay(value, isFrame) {
  47. this.$set(this.callMethod, 'name', 'goToAndPlay')
  48. this.$set(this.callMethod, 'args', [value, isFrame || false])
  49. },
  50. setDirection(direction) {
  51. this.$set(this.callMethod, 'name', 'setDirection')
  52. this.$set(this.callMethod, 'args', [direction || 1])
  53. },
  54. playSegments(segments, forceFlag) {
  55. this.$set(this.callMethod, 'name', 'playSegments')
  56. this.$set(this.callMethod, 'args', [segments, forceFlag])
  57. },
  58. setSubframe(useSubFrames) {
  59. this.$set(this.callMethod, 'name', 'setSubframe')
  60. this.$set(this.callMethod, 'args', [useSubFrames])
  61. },
  62. destroy() {
  63. this.$set(this.callMethod, 'name', 'destroy')
  64. this.$set(this.callMethod, 'args', [])
  65. },
  66. getDuration(isFrame) {
  67. return isFrame ? this.totalFrames : this.totalFrames / this.frameRate;
  68. },
  69. emitMsg(msg) {
  70. this.$emit(msg.name, msg.params);
  71. },
  72. emitData(data) {
  73. this[data.name] = data.value;
  74. }
  75. }
  76. }
  77. </script>
  78. <script module="Lottie" lang="renderjs">
  79. import lottie from "./js/lottie.js"
  80. export default {
  81. data() {
  82. return {
  83. lottieObj:null
  84. }
  85. },
  86. methods: {
  87. update(newVal) {
  88. },
  89. changeMethod(method) {
  90. try{
  91. this.lottieObj[method.name](...method.args);
  92. }catch(e){
  93. //TODO handle the exception
  94. }
  95. },
  96. isHttp(url) {
  97. return /http(s)?:\/\/([\w-]+\.)+[\w-]+(\/[\w- .\/?%&=]*)?/.test(url)
  98. },
  99. init(newVal) {
  100. lottie.destroy();
  101. this.$nextTick(() => {
  102. // console.log(this.$refs.lottie)
  103. this.lottieObj = lottie.loadAnimation({
  104. container: this.$refs.lottie, // 包含动画的dom元素
  105. renderer: 'svg', //渲染出来的是什么格式
  106. loop: (newVal.loop === undefined ? true : newVal.loop), //循环播放
  107. autoplay: (newVal.autoplay === undefined ? true : newVal.autoplay), //自动播放
  108. animationData: !this.isHttp(newVal.path) ? newVal.path : undefined, // 动画json的路径
  109. path: this.isHttp(newVal.path) ? newVal.path : undefined
  110. });
  111. // this.lottieObj.addEventListener('complete', () => {
  112. // this.$ownerInstance.callMethod('emitMsg', {
  113. // name: 'complete',
  114. // params: arguments
  115. // })
  116. // })
  117. if(newVal.speed){
  118. setTimeout(()=>{
  119. this.lottieObj.setSpeed(newVal.speed);
  120. },150);
  121. }
  122. })
  123. }
  124. },
  125. destroyed() {
  126. this.lottieObj.destroy();
  127. },
  128. mounted() {
  129. this.init(this.option)
  130. }
  131. }
  132. </script>
  133. <style scoped>
  134. .lottie-con {
  135. width: 100%;
  136. height: 100%;
  137. }
  138. </style>