首页 > 其他分享 >Vue实现简单的粒子特效

Vue实现简单的粒子特效

时间:2024-08-20 15:23:55浏览次数:8  
标签:特效 canvas Vue 粒子 particle random height width Math

在Vue中实现粒子动画效果可以通过多种方式,其中一种常见的方法是使用Canvas API来绘制粒子,并通过JavaScript控制粒子的运动和动画效果。以下是一个简单的示例,展示如何在Vue组件中实现基本的粒子动画效果。

1. 创建Vue项目

首先,确保你已经安装了Vue CLI。如果没有安装,可以通过以下命令进行安装:

npm install -g @vue/cli

然后创建一个新的Vue项目:

vue create particle-animation
cd particle-animation

2. 创建粒子动画组件

src/components目录下创建一个新的组件文件ParticleAnimation.vue,并在其中编写粒子动画的代码。

<template>
  <canvas ref="canvas" :width="width" :height="height"></canvas>
</template>

<script>
export default {
  data() {
    return {
      width: window.innerWidth,
      height: window.innerHeight,
      particles: [],
      particleCount: 100,
      animationFrame: null
    };
  },
  mounted() {
    this.initCanvas();
    this.createParticles();
    this.animate();
    window.addEventListener('resize', this.handleResize);
  },
  beforeDestroy() {
    window.removeEventListener('resize', this.handleResize);
    cancelAnimationFrame(this.animationFrame);
  },
  methods: {
    initCanvas() {
      const canvas = this.$refs.canvas;
      this.ctx = canvas.getContext('2d');
    },
    createParticles() {
      for (let i = 0; i < this.particleCount; i++) {
        this.particles.push({
          x: Math.random() * this.width,
          y: Math.random() * this.height,
          radius: Math.random() * 3 + 1,
          speedX: Math.random() * 2 - 1,
          speedY: Math.random() * 2 - 1,
          color: `rgba(255, 255, 255, ${Math.random() * 0.5 + 0.5})`
        });
      }
    },
    animate() {
      this.ctx.clearRect(0, 0, this.width, this.height);
      this.particles.forEach(particle => {
        particle.x += particle.speedX;
        particle.y += particle.speedY;

        if (particle.x > this.width || particle.x < 0) {
          particle.speedX = -particle.speedX;
        }
        if (particle.y > this.height || particle.y < 0) {
          particle.speedY = -particle.speedY;
        }

        this.ctx.beginPath();
        this.ctx.arc(particle.x, particle.y, particle.radius, 0, Math.PI * 2);
        this.ctx.fillStyle = particle.color;
        this.ctx.fill();
      });

      this.animationFrame = requestAnimationFrame(this.animate);
    },
    handleResize() {
      this.width = window.innerWidth;
      this.height = window.innerHeight;
      this.$refs.canvas.width = this.width;
      this.$refs.canvas.height = this.height;
    }
  }
};
</script>

<style scoped>
canvas {
  display: block;
  background: black;
}
</style>

3. 在主组件中使用粒子动画组件

src/App.vue中引入并使用ParticleAnimation组件:

<template>
  <div id="app">
    <ParticleAnimation />
  </div>
</template>

<script>
import ParticleAnimation from './components/ParticleAnimation.vue';

export default {
  name: 'App',
  components: {
    ParticleAnimation
  }
};
</script>

<style>
#app {
  font-family: Avenir, Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-align: center;
  color: #2c3e50;
  margin-top: 0;
}
</style>

4. 运行项目

最后,运行项目以查看效果:

npm run serve

打开浏览器并访问http://localhost:8080

标签:特效,canvas,Vue,粒子,particle,random,height,width,Math
From: https://blog.csdn.net/weixin_42242810/article/details/141359875

相关文章

  • 基于nodejs+vue协同过滤算法的商品推荐系统[程序+论文+开题]-计算机毕业设计
    本系统(程序+源码+数据库+调试部署+开发环境)带文档lw万字以上,文末可获取源码系统程序文件列表开题报告内容研究背景随着互联网技术的飞速发展,电子商务已成为人们日常生活中不可或缺的一部分。然而,面对海量的商品信息和日益增长的消费者需求,用户往往难以快速找到符合自己兴......
  • 基于nodejs+vue协同过滤算法的体育用品推荐系统[程序+论文+开题]-计算机毕业设计
    本系统(程序+源码+数据库+调试部署+开发环境)带文档lw万字以上,文末可获取源码系统程序文件列表开题报告内容研究背景随着互联网技术的蓬勃发展和体育文化的日益普及,体育用品市场迎来了前所未有的发展机遇。然而,面对市场上琳琅满目的体育用品和消费者日益增长的个性化需求,如......
  • 基于nodejs+vue协同过滤算法的电影推荐系统[程序+论文+开题]-计算机毕业设计
    本系统(程序+源码+数据库+调试部署+开发环境)带文档lw万字以上,文末可获取源码系统程序文件列表开题报告内容研究背景随着互联网的普及和视频流媒体服务的兴起,电影作为大众娱乐的重要组成部分,其数量正以惊人的速度增长。面对浩如烟海的电影资源,用户往往难以快速找到符合自己......
  • 基于nodejs+vue协同过滤的高考志愿推荐系统[程序+论文+开题]-计算机毕业设计
    本系统(程序+源码+数据库+调试部署+开发环境)带文档lw万字以上,文末可获取源码系统程序文件列表开题报告内容研究背景随着教育改革的深入和高等教育普及率的提升,高考作为人生的重要转折点,其志愿填报过程日益受到考生及家长的重视。然而,面对众多高校和复杂的专业设置,如何科学......
  • Vue生命周期
    从vue实例创建、运行、到销毁期间,总是伴随着各种各样的事件。而这些事件称为生命周期。每个Vue实例在被创建的时候都要经过一系列的初始化过程,例如,需要数据监听,编译模板,实例挂载到dom并在数据变化时更新Dom等,同时在这个过程中也会运行一系叫做生命周期钩子的函数,这给了用户在不......
  • 创建uni-app项目(vue3+ts+vite)
     npxdegitdcloudio/uni-preset-vue#vite-tsm-uni-demo1跳转到对应目录,装包,运行cdm-uni-demo1yarnyarndev:h5tsconfig.json:{"extends":"@vue/tsconfig/tsconfig.json","compilerOptions":{"ignoreDeprecations&quo......
  • SpringBoot+Vue校园兼职平台-计算机毕业设计源码26261
    摘要校园兼职平台作为连接学生和校园兼职资源的重要桥梁,具有推动校园就业服务和学生职业发展的重要作用。本项目旨在基于SpringBoot后端框架和Vue前端框架,设计和实现一个高效、便捷的校园兼职平台。通过该平台,学生可以轻松浏览、搜索和申请各类校园兼职岗位,实现校园资源的最......
  • 学习vue3——分页(前、后端分页两种)
    一、前端分页 后端将所有数据给前端,前端来实现分页1<template>2<divclass="flexitems-centerjustify-centermt-5">3<el-pagination4background5v-model:current-page="currentPage1"6......
  • 【Vue】考试功能实现
    一、需求场景 最近临时加的一个功能模块,让我两天就实现.... 部门成员需要进行测验考试,简单来说就是刷题练习 关于题库导入的部分在这篇文章已经写好了:https://www.cnblogs.com/mindzone/p/18362194 分值计算的需求目前没加入进来,只判断对错与否,然后计数题目总数和做对题数......
  • Vue 3 组件渲染“暂停”技巧
    目录为什么需要“暂停”渲染?实现思路示例实现1.创建组件2.解释实现优化建议更深入的渲染控制1.异步组件2.使用Suspense3.废弃管理性能优化技巧1.虚拟滚动2.使用watchEffect3.事件处理实际应用场景        在使用Vue3开发复杂应用时,有时我们......