当前位置:首页 > VUE

vue中实现canvas动画

2026-01-22 21:09:48VUE

在 Vue 中实现 Canvas 动画

创建 Canvas 元素

在 Vue 组件的模板中直接添加 <canvas> 元素,并为其设置 ref 以便在 JavaScript 中访问:

<template>
  <canvas ref="canvas" width="500" height="300"></canvas>
</template>

初始化 Canvas 上下文

mounted 生命周期钩子中获取 Canvas 上下文,并开始动画:

export default {
  mounted() {
    const canvas = this.$refs.canvas;
    const ctx = canvas.getContext('2d');
    this.animate(ctx);
  },
  methods: {
    animate(ctx) {
      // 动画逻辑
    }
  }
};

实现动画循环

使用 requestAnimationFrame 创建动画循环,确保动画平滑运行:

animate(ctx) {
  let x = 0;
  const draw = () => {
    ctx.clearRect(0, 0, ctx.canvas.width, ctx.canvas.height);
    ctx.fillStyle = 'blue';
    ctx.fillRect(x, 50, 50, 50);
    x += 1;
    if (x > ctx.canvas.width) x = 0;
    requestAnimationFrame(draw);
  };
  draw();
}

优化性能

在组件销毁时取消动画循环,避免内存泄漏:

export default {
  data() {
    return {
      animationId: null
    };
  },
  mounted() {
    const ctx = this.$refs.canvas.getContext('2d');
    this.animate(ctx);
  },
  methods: {
    animate(ctx) {
      const draw = () => {
        // 动画逻辑
        this.animationId = requestAnimationFrame(draw);
      };
      draw();
    }
  },
  beforeDestroy() {
    cancelAnimationFrame(this.animationId);
  }
};

响应式调整

监听窗口大小变化并调整 Canvas 尺寸:

mounted() {
  this.initCanvas();
  window.addEventListener('resize', this.handleResize);
},
beforeDestroy() {
  window.removeEventListener('resize', this.handleResize);
},
methods: {
  handleResize() {
    const canvas = this.$refs.canvas;
    canvas.width = canvas.clientWidth;
    canvas.height = canvas.clientHeight;
  }
}

复杂动画示例

实现一个粒子系统动画:

animate(ctx) {
  const particles = Array(100).fill().map(() => ({
    x: Math.random() * ctx.canvas.width,
    y: Math.random() * ctx.canvas.height,
    size: Math.random() * 5 + 1,
    speedX: Math.random() * 2 - 1,
    speedY: Math.random() * 2 - 1
  }));

  const draw = () => {
    ctx.clearRect(0, 0, ctx.canvas.width, ctx.canvas.height);
    particles.forEach(p => {
      p.x += p.speedX;
      p.y += p.speedY;
      if (p.x < 0 || p.x > ctx.canvas.width) p.speedX *= -1;
      if (p.y < 0 || p.y > ctx.canvas.height) p.speedY *= -1;
      ctx.fillStyle = `rgba(100, 150, 255, ${p.size/6})`;
      ctx.beginPath();
      ctx.arc(p.x, p.y, p.size, 0, Math.PI * 2);
      ctx.fill();
    });
    this.animationId = requestAnimationFrame(draw);
  };
  draw();
}

使用第三方库

对于复杂动画,可以考虑使用专门的 Canvas 库如:

  • p5.js:适合创意编码
  • fabric.js:提供高级对象模型
  • konva.js:专注于交互式图形

通过以上方法,可以在 Vue 应用中高效地实现各种 Canvas 动画效果。

vue中实现canvas动画

标签: 动画vue
分享给朋友:

相关文章

vue实现多个tab

vue实现多个tab

Vue 实现多个 Tab 的方法 使用动态组件切换 在 Vue 中可以通过动态组件 <component :is="currentTab"> 结合 v-for 和 v-bind 实现多个…

vue公共列表的实现

vue公共列表的实现

实现 Vue 公共列表组件 公共列表组件通常用于展示重复结构的数据,例如商品列表、用户列表等。以下是一个典型的实现方式: 基础实现 创建可复用的列表组件 List.vue: <templ…

vue实现自定义登录

vue实现自定义登录

实现自定义登录的基本步骤 在Vue中实现自定义登录功能通常需要结合前端和后端技术。以下是一个基本的实现流程: 创建登录表单组件 使用Vue的单文件组件创建一个登录表单,包含用户名和密码输入框以及提…

vue中登陆实现逻辑

vue中登陆实现逻辑

Vue 中登录实现逻辑 前端实现逻辑 表单验证 使用 Vue 的表单验证库(如 VeeValidate)或自定义验证规则,确保用户输入的账号和密码符合要求。例如,检查邮箱格式、密码长度等。 发送登录…

vue实现密码框的包

vue实现密码框的包

Vue 密码框实现方案 在 Vue 中实现密码框功能,可以通过原生 HTML 结合 Vue 指令或使用第三方组件库完成。以下是几种常见方案: 原生 HTML + Vue 双向绑定 <t…

实现vue

实现vue

安装Vue.js 通过npm安装Vue.js是最常见的方式。确保已安装Node.js和npm,运行以下命令: npm install vue 或者使用CDN直接引入: <script s…