uniapp-微信小程序内使用lottie动画

  1. lottie json 文件注意事项
yarn add lottie-miniprogram

uniapp

<script lang="ts" setup>
import lottie from 'lottie-miniprogram';
import { onMounted, getCurrentInstance } from 'vue'
import lottieJson from '@/static/lottie/lottie.json'

const _this = getCurrentInstance()!;

const Size = {
  width:300,
  height:300
}
const init = async () => {
  // 组件下需要通过组件实例使用
  _this.ctx.createSelectorQuery()// wx.createSelectorQuery()
    .select('#lottie')
    .node(async res => {
      const canvas = res.node;
      const context = canvas.getContext('2d');

      // 处理真机显示模糊
      const dpr = wx.getSystemInfoSync().pixelRatio;
      canvas.width = Size.width * dpr;
      canvas.height = Size.height * dpr;
      context.scale(dpr, dpr);

      lottie.setup(canvas);
      const ani = lottie.loadAnimation({
        loop: true,
        autoplay: true,
        animationData: lottieJson,
        rendererSettings: {
          context
        }
      });
      // 播放部分动画
      // ani.addEventListener("DOMLoaded", () => {
      //   ani.playSegments([0, 50], true);
      // });
    })
    .exec();
}

onMounted(async () => {
  init()
})
</script>
<template>
  <view class="lottie-view">
    <canvas
      id="lottie"
      type="2d"
      class="lottie"
    />
  </view>
</template>
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容