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

react-lottie动画组件封装

  1. 引入lottie.json即可
  2. 可配置加载完成后索要循环的片段
  3. 帧数看json文件开头就可
  4. 我看到动画文件的信息:
    “fr”:30 - 帧率30fps
    “ip”:0 - 起始帧0
    “op”:124 - 结束帧124
    在这里插入图片描述
'use client'import { useEffect, useRef } from 'react'
import lottie from 'lottie-web'interface LottieAnimationProps {path?: stringanimationData?: unknownclassName?: stringwidth?: number | stringheight?: number | stringrenderer?: 'svg' | 'canvas' | 'html'startFrame?: numberendFrame?: numberspeed?: numberspeedLoop?: number
}export default function LottieAnimation({path,animationData,className = '',width = 500,height = 500,startFrame = 58,endFrame = 124,speed = 1,speedLoop = 1,renderer = 'canvas',
}: LottieAnimationProps) {const containerRef = useRef<HTMLDivElement>(null)const animationRef = useRef<any>(null) // eslint-disable-line @typescript-eslint/no-explicit-anyconst isInitializedRef = useRef(false)// 使用useRef存储参数,避免依赖变化const paramsRef = useRef({path,animationData,startFrame,endFrame,speed,speedLoop,renderer})useEffect(() => {if (!containerRef.current || isInitializedRef.current) {return;}isInitializedRef.current = trueconst params = paramsRef.currentlet animation: any = null; // eslint-disable-line @typescript-eslint/no-explicit-any// 创建动画实例animation = lottie.loadAnimation({container: containerRef.current!,renderer: params.renderer,loop: false,autoplay: false,...(params.path ? { path: params.path } : { animationData: params.animationData }),});animationRef.current = animation// 播放状态管理let isFirstPlay = true;let isLooping = false;let currentDirection = -1; // -1: 反向(124→54), 1: 正向(54→124)// 数据准备完成animation.addEventListener('data_ready', () => {animation.setSpeed(params.speed);// 第一次播放:0-124帧animation.goToAndStop(0, true);animation.play();});// 双向循环播放函数const startLoop = () => {if (!isLooping) return;if (currentDirection === -1) {// 反向播放:124→54animation.playSegments([params.endFrame, params.startFrame], true);} else {// 正向播放:54→124animation.playSegments([params.startFrame, params.endFrame], true);}};// 监听播放完成animation.addEventListener('complete', () => {if (isFirstPlay) {isFirstPlay = false;isLooping = true;animation.setSpeed(params.speedLoop);startLoop();} else if (isLooping) {// 循环播放完成,切换方向currentDirection *= -1;startLoop();}});return () => {isInitializedRef.current = falseif (animation) {animation.removeEventListener('data_ready');animation.removeEventListener('complete');animation.destroy();animationRef.current = null}};}, [])return (<divref={containerRef}className={className}style={{ width, height }}/>)
}
        <LottieAnimationpath="/lottie_home.json"width={500}height={500}renderer="canvas"/>
http://www.dtcms.com/a/418936.html

相关文章:

  • 哈尔滨网站建设吕新松做搜索引擎网站
  • PostgreSQL 流复制参数 - synchronous_commit
  • BPEL:企业流程自动化的幕后指挥家
  • 企业网站开发一薇设计说明英语翻译
  • 搭建 Nexus3 私服并配置第三方 Maven 仓库(阿里云等)和优先级
  • JVM 深入研究 -- 详解class 文件
  • Apache Airflow漏洞致敏感信息泄露:只读用户可获取机密数据
  • 第十六周-基本量子3
  • 手机微网站怎么制作缪斯国际设计董事长
  • 在 Spring Cloud Gateway 中实现跨域(CORS)的两种主要方式
  • SQL Server从入门到项目实践(超值版)读书笔记 27
  • 【Git】项目管理全解
  • rdm响应式网站开发企业年报网上申报流程
  • 昆山开发区网站制作网站建设文档模板
  • PySide6调用OpenAI的Whisper模型进行语音ASR转写
  • 网站怎么被黑磁力蜘蛛
  • nginx反向代理和负载均衡
  • 外贸seo外贸推广外贸网站建设外贸网站建设网站域名信息查询
  • 新广告法 做网站的python和c++学哪个好
  • 数据科学与数据分析:真正的区别是什么?
  • default-route-advertise always 概念及题目
  • Python爬虫实战:获取东方财富网CPI信息与数据分析
  • Filebeat写ElasticSearch故障排查思路(上)
  • 网站开发进度安排文档青岛关键词优化排名
  • C# TCP 服务端与客户端代码分析与补充
  • 族蚂建站郴州网站建设费用价格
  • 对象分配在哪块内存?
  • AI Agent智能体如何突破“听懂却做不好”困局?多模态技术打通全链路
  • 图卷积网络 (GCN)
  • JMeter中常用的配置优化