vue3中lottie-web封装组件和api的利用

打印 上一主题 下一主题

主题 868|帖子 868|积分 2604

 


     利用ts的话定义范例components/Lottie/type.ts
  1. type Segment = [ number, number]
  2. export interface LottieEvent {
  3.   play: () => void, // 播放动画
  4.   pause: () => void, // 暂停动画
  5.   stop: () => void, // 停止动画
  6.   setSpeed: (speed: number) => void, // 设置播放速度,1 表示1倍速度,0.5 表示 0.5倍速度
  7.   goToAndStop: (value: number, isFrame?: boolean) => void, // 跳到某一帧或某一秒停止,第二个参数 isFrame 为是否基于帧模式还是时间,默认为 false
  8.   goToAndPlay: (value: number, isFrame?: boolean) => void, // 跳到某一帧或某一秒开始,第二个参数 isFrame 为是否基于帧模式还是时间,默认为 false
  9.   setDirection: (direction: 1 | -1) => void, // 设置播放方向,1 表示正向播放,-1 表示反向播放
  10.   playSegments: (segments: Segment[], forceFlag?: boolean) => void, // 播放指定的片段,参数1为数组,两个元素为开始帧和结束帧;参数2为,是否立即播放片段,还是等之前的动画播放完成
  11.   destroy: () => void, // 销毁动画
  12.   getDuration: (isFrames: boolean) => void,  // 获取动画时长,参数为是否基于帧模式,默认为 false. 如果为真,则返回以帧为单位的持续时间,如果为假,则以秒为单位。
  13.   [propname: string]: any
  14. }
  15. // Events
  16. // onComplete - 播放完成时触发
  17. // onLoopComplete - 循环播放完成时触发
  18. // onEnterFrame - 每一帧播放时触发
  19. // onSegmentStart - 播放指定片段开始时触发
复制代码
   封装组件 components/Lottie/index.vue 
  1. <script setup>
  2. import { ref, onMounted } from 'vue'
  3. import lottie from 'lottie-web'
  4. // 设置组件参数
  5. const props = defineProps({
  6.   renderer: {
  7.     type: String,
  8.     default: 'svg',
  9.   },
  10.   // 循环播放
  11.   loop: {
  12.     type: Boolean,
  13.     default: true,
  14.   },
  15.   autoplay: {
  16.     type: Boolean,
  17.     default: true,
  18.   },
  19.   animationData: {
  20.     type: Object,
  21.     default: () => ({}),
  22.   },
  23.   name: {
  24.     type: String,
  25.     default: '',
  26.   }
  27. })
  28. // 创建 lottie 接收变量和获取dom
  29. const animation = ref(null)
  30. const dom = ref(null)
  31. // 创建事件返回初始化lottie对象
  32. const emits = defineEmits(['getAnimation', 'getDom'])
  33. // 初始化渲染 lottie动画,并返回lottie对象
  34. onMounted(() => {
  35.   animation.value = lottie.loadAnimation({
  36.     container: dom.value,  // 用于渲染的容器
  37.     // 渲染方式 svg、canvas、html
  38.     renderer: props.renderer,
  39.     // 是否循环
  40.     loop: props.loop,
  41.     autoplay: props.autoplay, // 自动播放
  42.     // UED 提供的 动画的 json 文件
  43.     animationData: props.animationData,
  44.     name: props.name,
  45.   });
  46.   emits('getAnimation', animation.value)
  47. })
  48. </script>
  49. <template>
  50.     <!-- 渲染 lottie 动画 -->
  51.     <div id="lottieId" ref="dom"></div>
  52. </template>
  53. <style scoped>
  54. #lottieId {
  55.   width: 100%;
  56.   height: 100%;
  57. }
  58. </style>
复制代码
  引用组件,例如HelloWord.vue
  1. <script setup>
  2. import { ref, onMounted, watch } from 'vue'
  3. import Lottie from './Lottie/index.vue'
  4. import Animation from '../assets/Animation - 1712559820721.json'
  5. import Animation1 from '../assets/Animation-1.json'
  6. const a1 = ref(null)
  7. const count = ref(0)
  8. const likeFlag = ref(false)
  9. const getAnimation = (animation) => {
  10.   a1.value = animation
  11.   if (a1.value) {
  12.     a1.value.addEventListener('enterFrame', () => {
  13.       // console.log(a1.value, '--a1.value--');
  14.       if (a1.value.currentFrame >= 63) { // 当前帧
  15.         a1.value.goToAndPlay(0, true) // 跳转到指定帧开始
  16.       }
  17.     })
  18.     a1.value.addEventListener('complete', () => {
  19.       console.log('播放完毕');
  20.     })
  21.   }
  22. }
  23. const play = () => {
  24.   if (likeFlag.value) {
  25.     a1.value.playSegments([31, 64], true) // 播放指定的片段,([开始帧,结束帧], 是否立即播放)
  26.   } else {
  27.     a1.value.playSegments([0, 30], true)
  28.   }
  29.   likeFlag.value = !likeFlag.value
  30.   count.value++
  31. }
  32. // const likeClick = () => {
  33. //   console.log('likeClick');
  34. //   a1.value.setSpeed(2) // 设置播放速度
  35. //   // a1.value.setDirection(-1) // 设置播放速度,不知道什么原因这里没有生效
  36. //   a1.value.stop()
  37. //   setTimeout(() => {
  38. //     // a1.value.goToAndStop(31, true) // 跳到指定帧结束,第二个参数 isFrame 为是否基于帧模式还是时间,默认为 false
  39. //     // a1.value.goToAndPlay(21, true) // 跳转到指定帧开始
  40. //     a1.value.play();
  41. //   }, 100); // 延迟100毫秒播放
  42. //   const duration = a1.value.getDuration(true) // true 为帧, false 为秒
  43. //   console.log(duration, '--duration--'); // 64帧 2.56秒
  44. // }
  45. // 监听鼠标滚动事件
  46. let frame = 0
  47. const maxFrame = 60; // 假设最大值为100
  48. window.addEventListener('wheel', (event) => {
  49.   console.log(event, '--event--');
  50.   if (event.wheelDelta < 0) {
  51.     frame += 10
  52.     count.value++
  53.   } else if (event.wheelDelta >= 149) {
  54.     frame -= 10
  55.     count.value--
  56.   }
  57.   if (frame >= maxFrame) {
  58.     frame = 0; // 重置为0
  59.   } else if (frame < 0) {
  60.     frame = maxFrame; // 重置为最大值
  61.   }
  62.   a1.value.goToAndStop(frame, true)
  63.   console.log(frame, '--frame--');
  64. })
  65. onMounted(() => {
  66. })
  67. </script>
  68. <template>
  69.   <div class="card">
  70.     <div>
  71.       <Lottie :animation-data="Animation" />
  72.     </div>
  73.     <div class="card-body">
  74.       <Lottie class="a1" :animation-data="Animation1" :loop="false" :autoplay="true" @get-animation="getAnimation"
  75.         @click="likeClick" />
  76.       <button @click="play">Play</button>
  77.       <div class="count">{{ count }}</div>
  78.       <div class="info">测试内容</div>
  79.     </div>
  80.   </div>
  81. </template>
  82. <style scoped>
  83. .card {
  84.   display: flex;
  85.   align-items: center;
  86.   width: 700px;
  87.   /* margin: 0 auto; */
  88.   .card-body {
  89.     position: relative;
  90.     .info {
  91.       position: absolute;
  92.       left: 50%;
  93.       top: 20%;
  94.       transform: translate(-50%, -50%);
  95.       color: rgb(255, 255, 255);
  96.     }
  97.   }
  98. }
  99. </style>
复制代码
 

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

天津储鑫盛钢材现货供应商

金牌会员
这个人很懒什么都没写!
快速回复 返回顶部 返回列表