vue2+Svgaplayer 播放.svga格式动画

SVGA 是一种同时兼容 iOS / Android / Flutter / Web 多个平台的动画格式。
官网链接:https://svga.io/

vue项目里安装
npm install svgaplayerweb —save
或
yarn add svgaplayerweb
svga组件页面
<template>
  <div :id="svId" :class="className || ''" :style="styleObj || {}"></div>
</template>

<script>
import SVGA from "svgaplayerweb";  // 引入SVGA
let svgaId = 0; // id序号

export default {
  props: [
    "src",              // String  资源路径 
    "className",        // String class-name 静态class 
    "loops",            // Number  循环播放次数 默认为最大正整数
    "onFrame",          // Function 每帧执行回调
    "styleObj",         // Object  style-obj 绑定的style样式
    "mode",             // String  可选 "ScaleToFill" | "AspectFill" | "AspectFit"
    "onlyTag"           // String  only-tag 唯一标识,每帧/执行完毕 函数回调时标识,用户同一动画,多出显示
  ],

  data() {
    return {
      svId: `svga-id-${svgaId++}` // 动态生成id,处理页面多个svga的情况
    }
  },

  created() {
    this.$nextTick(() => {
      try {
        // 创建player
        let player = new SVGA.Player(`#${this.svId}`);
        player.loops = this.loops || Number.MAX_SAFE_INTEGER;
        player.clearsAfterStop = false;
        // 创建解析者
        let parser = new SVGA.Parser(`#${this.svId}`);
        parser.load(this.src, (videoItem) => {
          player.setVideoItem(videoItem);
          player.setContentMode(this.mode || "AspectFit");
          player.startAnimation();
          player.onFinished(() => this.onFinished?.(this.onlyTag));
          player.onFrame((frame) => this.onFrame?.(frame, this.onlyTag));
        });
      } catch (error) {
        console.error(error);
      }
    });

  },
}
</script>
vue页面使用
<template>
  <svgaplayer :src="'../public/images/img_guide.svga'"  // 图片路径
    :styleObj="{ height: '100px' }"   // 样式
  ></svgaplayer>
</template>

<script>
import svgaplayer from "../components/svgaplayer.vue";  // 引入组件

export default {
  components: {
    svgaplayer,
  },
};
</script>

  • 11
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
为了在Vue 3项目中使用TypeScript和Webpack,并且正确地生成.d.ts声明文件,你需要按照以下步骤进行配置: 1. 安装依赖: ```bash npm install --save-dev typescript ts-loader vue-loader@next @types/vue @vue/compiler-sfc ``` 2. 创建`tsconfig.json`文件: 在项目根目录下创建一个名为`tsconfig.json`的文件,并添加以下内容: ```json { "compilerOptions": { "target": "esnext", "module": "esnext", "strict": true, "jsx": "preserve", "moduleResolution": "node", "resolveJsonModule": true, "esModuleInterop": true, "lib": ["esnext", "dom"], "skipLibCheck": true, "forceConsistentCasingInFileNames": true }, "include": ["src/**/*.ts", "src/**/*.tsx", "src/**/*.vue", "tests/**/*.ts", "tests/**/*.tsx"] } ``` 3. 配置Webpack: 在Webpack配置文件中,添加对TypeScript和Vue文件的处理规则。例如,如果你使用的是`webpack.config.js`文件,可以按照以下示例进行配置: ```javascript const path = require('path'); module.exports = { // ...其他配置 resolve: { extensions: ['.js', '.ts', '.vue'], alias: { '@': path.resolve(__dirname, 'src') } }, module: { rules: [ { test: /\.tsx?$/, loader: 'ts-loader', exclude: /node_modules/, options: { appendTsSuffixTo: [/\.vue$/] } }, { test: /\.vue$/, loader: 'vue-loader' } ] }, // ...其他配置 }; ``` 4. 添加Vue文件声明: 在项目的`src`目录中,为Vue文件添加一个`.d.ts`的同名文件。例如,对于`HelloWorld.vue`组件,创建一个名为`HelloWorld.vue.d.ts`的文件,并添加以下内容: ```typescript declare module '*.vue' { import { ComponentOptions } from 'vue'; const componentOptions: ComponentOptions; export default componentOptions; } ``` 5. 构建项目: 运行Webpack构建命令,以启动TypeScript编译和生成声明文件: ```bash npm run build ``` 如果一切顺利,你的Vue 3 + TypeScript项目应该可以成功编译,并且在构建过程中会生成相应的.d.ts声明文件。 希望以上步骤可以帮助你配置Vue 3 + TypeScript + Webpack项目的.d.ts声明文件。如果有任何问题,请随时追问!

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值