当前位置:首页 > 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实现点击

vue实现点击

实现点击事件的基本方法 在Vue中实现点击事件可以通过v-on指令或@简写方式绑定。以下是常见的点击事件绑定方法: <template> <button v-on:click…

vue实现ai

vue实现ai

Vue 实现 AI 功能 Vue.js 可以通过集成第三方 AI 服务或本地 AI 模型来实现 AI 功能。以下是几种常见的方法: 集成第三方 AI API 使用 Vue 调用如 OpenAI、G…

vue 实现列表

vue 实现列表

Vue 实现列表的方法 在 Vue 中实现列表通常使用 v-for 指令,结合数组或对象数据渲染动态列表。以下是几种常见的实现方式: 基础列表渲染 使用 v-for 指令遍历数组,渲染列表项。it…

vue实现图片

vue实现图片

Vue 实现图片功能的方法 静态图片引入 在 Vue 项目中,可以通过 src 属性直接引入静态图片。图片通常放在 public 或 assets 目录下。 <template> &…

vue compile 实现

vue compile 实现

Vue 编译实现原理 Vue 的编译过程将模板字符串转换为渲染函数,主要分为解析、优化和代码生成三个阶段。 解析阶段(Parse) 将模板字符串转换为抽象语法树(AST)。Vue 使用正则表达式和有…

vue el 实现

vue el 实现

Vue 中使用 Element UI (el) 的实现方法 Element UI 是一个基于 Vue 的组件库,提供丰富的 UI 组件,常用于快速构建企业级中后台产品。以下介绍 Vue 项目中集成和使…