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

vue2项目中使用pag格式动图

官方接入指南

vue2官方demo

使用方法如下:

1、安装libpag插件

npm install libpag@^4.1.33

yarn add libpag@^4.1.33

2、配置vue.config.js

configureWebpack: {plugins: [new CopyWebpackPlugin([{from: path.resolve(__dirname, './node_modules/libpag/lib/libpag.wasm'),to: './'}]}]
}

3、在项目public文件夹中创建static目录放置pag相关静态文件

4、 创建 PAG 组件

PAGPlayer.vue

<template><div class="pag-container"><canvas ref="canvas" class="pag-canvas" :style="canvasStyle"></canvas></div>
</template><script>
import { PAGInit } from 'libpag';export default {name: 'PAGPlayer',props: {src: {type: String,default: './pag/boy.pag'},width: {type: [Number, String],default: '100%'},height: {type: [Number, String],default: '100%'},autoPlay: {type: Boolean,default: true},repeat: {type: Boolean,default: true}},data() {return {pagFile: null,pagView: null};},computed: {canvasStyle() {return {width: typeof this.width === 'number' ? `${this.width}px` : this.width,height: typeof this.height === 'number' ? `${this.height}px` : this.height};}},watch: {async src() {await this.loadPAGFile();if (this.autoPlay) {this.play();}}},async mounted() {await this.initPAG();await this.loadPAGFile();if (this.autoPlay) {this.play();}},beforeDestroy() {this.destroy();},methods: {/*** 初始化PAG*/async initPAG() {try {this.pag = await PAGInit({locateFile: file => {console.log('file===', file);if (file === 'libpag.wasm') {return '/static/libpag.wasm';}return file;}});} catch (error) {console.error('Failed to initialize PAG:', error);throw error;}},/*** 加载PAG文件*/async loadPAGFile() {if (this.pagView) {this.pagView.destroy();this.pagView = null;}try {const url = this.src;const response = await fetch(url);const blob = await response.blob();const file = new window.File([blob], url.replace(/(.*\/)*([^.]+)/i, '$2'));this.pagFile = await this.pag.PAGFile.load(file);this.pagView = await this.pag.PAGView.init(this.pagFile, this.$refs.canvas);this.pagView.setRepeatCount(this.repeat ? 0 : 1); // 0表示无限循环,1表示播放一次// 监听播放完成事件this.pagView.addListener('onAnimationEnd', () => {console.log('Animation ended');this.$emit('ended');});this.$emit('loaded');} catch (error) {console.error('Failed to load PAG file:', error);this.$emit('error', error);}},/*** 播放动画*/play() {if (this.pagView) {this.pagView.play();this.$emit('play');}},/*** 暂停动画*/pause() {if (this.pagView) {this.pagView.pause();this.$emit('pause');}},/*** 停止动画*/stop() {if (this.pagView) {this.pagView.stop();this.$emit('stopped');}},/*** 销毁PAG资源*/destroy() {if (this.pagView) {this.pagView.destroy();this.pagView = null;}if (this.pagFile) {this.pagFile.destroy();this.pagFile = null;}},/*** 获取当前播放进度(0-1)*/getProgress() {return this.pagView ? this.pagView.getProgress() : 0;},/*** 设置播放进度(0-1)* @param progress {number} 0-1*/setProgress(progress) {if (this.pagView) {this.pagView.setProgress(progress);}},/*** 获取动画持续时间(秒)*/getDuration() {return this.pagFile ? this.pagFile.duration() / 1000000 : 0;}}
};
</script><style scoped>
.pag-container {position: relative;width: 100%;height: 100%;
}.pag-canvas {width: 100%;height: 100%;display: block;
}
</style>

5、使用 PAG 组件

在需要使用的页面中引入并使用 PAG 组件:

<template><div><div class="controls"><button @click="play">Play</button><button @click="pause">Pause</button><button @click="stop">Stop</button></div><PAGPlayerref="pagPlayer":src="pagUrl":width="400":height="400":auto-play="false":repeat-count="3"@loaded="onPAGLoaded"@error="onPAGError"/></div>
</template><script>
import PAGPlayer from '@/components/PAGPlayer.vue'export default {components: {PAGPlayer},data() {return {pagUrl: 'https://example.com/your-animation.pag', // 替换为你的PAG文件URL}},methods: {play() {this.$refs.pagPlayer.play()},pause() {this.$refs.pagPlayer.pause()},stop() {this.$refs.pagPlayer.stop()},onPAGLoaded(pagFile) {console.log('PAG file loaded:', pagFile)// 可以获取动画信息console.log('Duration:', pagFile.duration())console.log('Width:', pagFile.width())console.log('Height:', pagFile.height())console.log('NumChildren:', pagFile.numChildren())},onPAGError(error) {console.error('PAG error:', error)}}
}
</script><style>
.controls {margin: 20px 0;
}
.controls button {margin-right: 10px;
}
</style>

http://www.xdnf.cn/news/304417.html

相关文章:

  • GMRES算法处理多个右端项的Block与PseudoBlock变体
  • 【已解决】Neo4j Desktop打不开,不断网解决
  • 一种基于条件生成对抗网络(cGAN)的CT重建算法
  • Hadoop架构再探讨
  • keil+vscode+腾讯ai助手
  • 【prometheus+Grafana篇】基于Prometheus+Grafana实现Linux操作系统的监控与可视化
  • 【程序员AI入门:基础】5.提示工程怎么释放LLM的潜力
  • WT2606B显示驱动TFT语音芯片IC:重塑电子锁交互体验的技术革新
  • 神经网络之训练的艺术:反向传播与常见问题解决之道
  • 数据库实验10 函数存储
  • Dify - Stable Diffusion
  • 《数据分析与可视化》(清华)ch-6 作业 三、绘图题
  • 解决Centos连不上网
  • 数字图像相关法在薄板变形测量中的实践
  • 《Python星球日记》第34天:Web 安全基础
  • Cadence学习笔记之---PCB工程创建、类与子类、颜色管理器介绍
  • 【Python】--实现多进程
  • 2.4线性方程组
  • 使用batch脚本调用另一个batch脚本遇到的问题
  • 【Linux网络编程十一】网络原理之数据链路层
  • 【HTML5】显示-隐藏法 实现网页轮播图效果
  • 【LDM】视觉自回归建模:通过Next-Scale预测生成可扩展图像(NeurIPS2024最佳论文阅读笔记与吃瓜)
  • 第七节:图像基本操作-图像属性获取 (尺寸、通道数、数据类型)
  • C++【STL】(1)string
  • 基于STM32、HAL库的W25X40CLSNIG NOR FLASH存储器驱动应用程序设计
  • 【Linux系统】线程安全
  • unix 详解
  • cuda多维线程的实例
  • 纷析云开源财务软件:重新定义企业财务自主权
  • 《Python星球日记》第35天:全栈开发(综合项目)