web端动效 PAG

之前写过一篇lottie动效的文章:web端动效 lottie-web 使用,本篇写一下PAG-web的基础使用。

PAG是腾讯开发,支持移动端、桌面端以及Web端的动效工作流解决方案。目标是降低或消除动效相关的研发成本,能够一键将设计师在 AE(Adobe After Effects)中制作的动效内容导出成素材文件,并快速上线应用于几乎所有的主流平台。

  1. 安装 官方文档
yarn add libpag
  1. vue.config.js 加入以下代码
// ...
const path = require('path')
const CopyWebpackPlugin = require('copy-webpack-plugin')

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

简单的接入示例和 Vue / React / PixiJS 等配置示例, 可以点击 这里 查看。

  1. 使用
    创建一个组件MyPag/index.vue
<template>
  <canvas class="pag" id="pag"></canvas>
</template>

<script>
import { PAGInit } from 'libpag'

export default {
  data () {
    return {
      pagView: null
    }
  },
  created () {
    this.initPag()
  },
  methods: {
    async initPag () {
      const url = '/static/like.pag' // pag文件放在了静态文件夹下 /public/static
      const PAG = await PAGInit()
      const { PAGFile, PAGView } = PAG

      // 示例 fetch 请求
      const buffer = await fetch(url).then(res => res.arrayBuffer())
      const pagFile = await PAGFile.load(buffer)

      document.getElementById('pag').width = pagFile.width()
      document.getElementById('pag').height = pagFile.height()
      this.pagView = await PAGView.init(pagFile, '#pag')
      this.pagView.setRepeatCount(0)
      this.pagView.play()
    }
  }
}
</script>

<style lang="scss" scoped>
.pag {
  width: 200px;
  height: 200px;
}
</style>

示例中pag文件路径,测试素材下载
在这里插入图片描述
一个基本的pag动效就出来了
在这里插入图片描述
结合实例方法,加入简单的鼠标事件:移入播放,移出暂停。查看API文档

<template>
  <canvas class="pag" id="pag" @mouseenter="onMouseenter" @mouseleave="onMouseleave"></canvas>
</template>

<script>
import { PAGInit } from 'libpag'

export default {
  props: {
    pagUrl: {
      type: String
    },
    autoplay: {
      type: Boolean
    }
  },
  data () {
    return {
      pagView: null
    }
  },
  mounted () {
    this.initPag()
  },
  methods: {
    async initPag () {
      const url = '/static/like.pag'
      const PAG = await PAGInit()
      const { PAGFile, PAGView } = PAG

      const buffer = await fetch(url).then(res => res.arrayBuffer())
      const pagFile = await PAGFile.load(buffer)

      document.getElementById('pag').width = pagFile.width()
      document.getElementById('pag').height = pagFile.height()
      this.pagView = await PAGView.init(pagFile, '#pag')
      this.pagView.setRepeatCount(0)
      if (this.autoplay) {
        this.pagView.play()
      }
    },
    onMouseenter () {
      if (!this.autoplay) {
        this.pagView.play()
      }
    },
    onMouseleave () {
      if (!this.autoplay) {
        this.pagView.pause()
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.pag {
  width: 200px;
  height: 200px;
}
</style>

在这里插入图片描述

PAGViewer

目前 PAG 预览支持 Web、macOS 和 Windows 平台,其中 Web 平台为一个页面,macOS 和 Windows 平台为桌面端应用。进入下载

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ZionHH

落魄前端,在线炒粉

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值