当前位置: 首页 > news >正文

lottie-miniprogram在taro+vue的小程序中怎么使用

把一个json的动图展示在页面上。使用的是插件lottie-miniprogramhttps://blog.csdn.net/qq_33769914/article/details/128705922之前介绍过。但是发现使用在taro使用的时候他会报错。

那可能是因为我们

wx.createSelectorQuery().select('#canvas').node(res => {

   console.log(res)//打印这个的时候是null。所以下面的res.node就会报错

const canvas = res.node

const context = canvas.getContext('2d')

})

我们可以应该把获取的生命周期改成useDidShow。然后再加个延迟。

useDidShow(()=>{

setTimeout(()=>{

init()

}, 0)

})

const init = ()=> {

if (inited.value) {

return

}

Taro.createSelectorQuery().select(`#lottie`).node(res => {

console.log("res",res)

const canvas = res.node

if(canvas){

const context = canvas.getContext('2d')

if (pixelRatio.value) {

context.scale(pixelRatio.value, pixelRatio.value)

canvas.width = 300* pixelRatio.value

canvas.height = 300* pixelRatio.value

}

Lottie.setup(canvas)

lottieObj = Lottie.loadAnimation({

loop: props.loop,

autoplay: props.autoplay,

animationData: props.jsonData,

rendererSettings: {

context,

},

})

inited.value = true

// emits('onComplete')

}

}).exec()

}

全部代码如下。把它方做一个组件使用lottieView.vue

<template>

<canvas class="canvas" :id="`lottie`" type="2d" />

</template>

<script lang="ts" setup>

import { onMounted, defineProps, ref } from 'vue'

import Taro from "@tarojs/taro";

import Lottie from 'lottie-miniprogram'

import lottieJson from './data.json'    //json的动画可以找产品要

const props = defineProps({

id: {

type: String,

default: ''

},

jsonData: {

type: Object,

default: lottieJson

},

autoplay: {

type: Boolean,

default: false

},

loop: {

type: Boolean,

defalut: false

},

assetsPath: {

type: String,

defalut: ''

}

})

const emits = defineEmits(['onComplete'])

const sysinfo = Taro.getSystemInfoSync(); //获取设备系统的数据

const pixelRatio = ref(sysinfo.pixelRatio)

let lottieObj: any = undefined

Taro.useDidShow(()=>{

console.log('lottie---useReady');

setTimeout(()=>{

init(1)

}, 0)

})

// onMounted(()=>{

// console.log('lottie---onMounted');

// setTimeout(()=>{

// init(3)

// }, 10)

// })

const inited = ref(false)

// 初始化加载动画

const init = (type)=> {

console.log("type",type)

if (inited.value) {

return

}

Taro.createSelectorQuery().select(`#lottie`).node(res => {

console.log("res",res)

const canvas = res.node

if(canvas){

const context = canvas.getContext('2d')

if (pixelRatio.value) {

context.scale(pixelRatio.value, pixelRatio.value)

canvas.width = 300* pixelRatio.value

canvas.height = 300* pixelRatio.value

}

Lottie.setup(canvas)

lottieObj = Lottie.loadAnimation({

loop: props.loop,

autoplay: props.autoplay,

animationData: props.jsonData,

rendererSettings: {

context,

},

})

inited.value = true

// emits('onComplete')

}

}).exec()

}

const play = () => {

lottieObj && lottieObj.play()

}

const stop = () => {

lottieObj && lottieObj.stop()

}

const pause = () => {

lottieObj && lottieObj.pause()

}

defineExpose({

play,

stop,

pause

})

</script>

<style lang="less"></style>

使用import LottieView from '@/magic-head/pages/components/lottieView'

<LottieView

class="lottie"

:autoplay="true"

:loop="true"

/>

http://www.lryc.cn/news/38109.html

相关文章:

  • C++回顾(二十二)—— stack容器 与 queue容器
  • 逻辑优化基础-disjoint support decomposition
  • 保姆级使用PyTorch训练与评估自己的DaViT网络教程
  • Java8新特性:Stream流处理使用总结
  • Java基准测试工具JMH高级使用
  • 问心 | 再看token、session和cookie
  • Ubuntu 安装 CUDA and Cudnn
  • 【漏洞复现】Grafana任意文件读取(CVE-2021-43798)
  • 磨金石教育摄影技能干货分享|春之旅拍
  • 中断以及 PIC可编程中断控制器
  • SecureCRT 安装并绑定ENSP设备终端
  • ESP32设备驱动-TCS3200颜色传感器驱动
  • < JavaScript小技巧:Array构造函数妙用 >
  • 【17】组合逻辑 - VL17/VL19/VL20 用3-8译码器 或 4选1多路选择器 实现逻辑函数
  • 2023年全国最新二级建造师精选真题及答案19
  • Java中的 this 和 super
  • ESP32设备驱动-红外寻迹传感器驱动
  • 初识BFC
  • 随想录二刷Day17——二叉树
  • Weblogic管理控制台未授权远程命令执行漏洞复现(cve-2020-14882/cve-2020-14883)
  • STM32F103CubeMX定时器
  • 多态且原理
  • 动态库(二) 创建动态库
  • opencv加水印
  • Flume基操
  • 图文详解红黑树,还有谁不会?
  • 多目标遗传算法NSGA-II原理详解及算法实现
  • Spark 键值对RDD的操作
  • 【SpringCloud】SpringCloud详解之Feign远程调用
  • 文档团队怎样使用GIT做版本管理