当前位置:首页 > VUE

vue实现烟花效果

2026-01-17 11:17:37VUE

Vue 实现烟花效果的方法

在Vue中实现烟花效果可以通过结合Canvas绘图和动画技术来完成。以下是一种常见的实现方式:

创建Vue组件

新建一个名为Fireworks.vue的组件,核心逻辑如下:

vue实现烟花效果

<template>
  <canvas ref="canvas" class="fireworks-canvas"></canvas>
</template>

<script>
export default {
  mounted() {
    this.initCanvas();
    this.startFireworks();
  },
  methods: {
    initCanvas() {
      this.canvas = this.$refs.canvas;
      this.ctx = this.canvas.getContext('2d');
      this.canvas.width = window.innerWidth;
      this.canvas.height = window.innerHeight;

      window.addEventListener('resize', () => {
        this.canvas.width = window.innerWidth;
        this.canvas.height = window.innerHeight;
      });
    },

    startFireworks() {
      const particles = [];
      const particleCount = 150;

      function Particle(x, y) {
        this.x = x;
        this.y = y;
        this.color = `hsl(${Math.random() * 360}, 100%, 50%)`;
        this.radius = Math.random() * 2 + 1;
        this.velocity = {
          x: (Math.random() - 0.5) * 8,
          y: (Math.random() - 0.5) * 8
        };
        this.alpha = 1;
        this.decay = Math.random() * 0.015 + 0.01;
      }

      function createFirework(x, y) {
        for (let i = 0; i < particleCount; i++) {
          particles.push(new Particle(x, y));
        }
      }

      function animate() {
        requestAnimationFrame(animate);
        this.ctx.fillStyle = 'rgba(0, 0, 0, 0.1)';
        this.ctx.fillRect(0, 0, this.canvas.width, this.canvas.height);

        particles.forEach((particle, index) => {
          particle.velocity.y += 0.05;
          particle.x += particle.velocity.x;
          particle.y += particle.velocity.y;
          particle.alpha -= particle.decay;

          this.ctx.beginPath();
          this.ctx.arc(particle.x, particle.y, particle.radius, 0, Math.PI * 2);
          this.ctx.fillStyle = particle.color.replace(')', `, ${particle.alpha})`).replace('hsl', 'hsla');
          this.ctx.fill();

          if (particle.alpha <= 0) {
            particles.splice(index, 1);
          }
        });
      }

      this.canvas.addEventListener('click', (e) => {
        createFirework(e.clientX, e.clientY);
      });

      animate.call(this);
    }
  }
};
</script>

<style>
.fireworks-canvas {
  position: fixed;
  top: 0;
  left: 0;
  z-index: 9999;
  pointer-events: none;
}
</style>

使用组件

在需要显示烟花效果的页面中引入并使用该组件:

<template>
  <div>
    <Fireworks />
    <!-- 其他页面内容 -->
  </div>
</template>

<script>
import Fireworks from '@/components/Fireworks.vue';

export default {
  components: {
    Fireworks
  }
};
</script>

自定义烟花效果

可以通过修改以下参数来自定义烟花效果:

vue实现烟花效果

  • particleCount:控制烟花粒子数量
  • decay值:调整粒子消失速度
  • velocity:改变粒子飞散速度
  • color:使用不同的颜色生成方式

自动触发烟花

如果需要自动触发而非点击触发,可以添加定时器:

// 在startFireworks方法中添加
setInterval(() => {
  const x = Math.random() * this.canvas.width;
  const y = Math.random() * this.canvas.height * 0.5;
  createFirework(x, y);
}, 1000);

性能优化

对于大量烟花效果,可以考虑以下优化措施:

  • 限制同时存在的粒子数量
  • 使用Web Worker处理粒子计算
  • 降低动画帧率
  • 使用requestAnimationFrame的节流版本

这种方法创建的烟花效果完全基于Canvas,不依赖任何外部库,性能良好且可高度自定义。

标签: 烟花效果
分享给朋友:

相关文章

vue实现拖拉效果

vue实现拖拉效果

实现拖拽效果的基本方法 在Vue中实现拖拽效果可以通过原生HTML5的拖拽API或第三方库如vuedraggable来完成。以下是两种常见方法的详细说明。 使用HTML5拖拽API HTML5提供了…

vue实现突出效果

vue实现突出效果

Vue 实现突出效果的方法 在 Vue 中实现突出效果可以通过多种方式,包括动态样式绑定、过渡动画、第三方库等。以下是几种常见的方法: 动态样式绑定 通过 v-bind:class 或 v-bind…

js实现图片滚动效果

js实现图片滚动效果

使用CSS动画实现图片滚动 通过CSS的@keyframes和animation属性实现无缝滚动效果。将图片容器设置为横向排列,通过动画平移位置。 <style> .scroll-c…

jquery效果

jquery效果

jQuery 动画效果 jQuery 提供了多种内置动画效果,可用于实现平滑的页面交互。常用的方法包括 show()、hide()、toggle()、fadeIn()、fadeOut()、slideU…

vue实现吸附效果

vue实现吸附效果

实现吸附效果的基本思路 吸附效果通常指当页面滚动到某个位置时,元素固定在视口的特定位置。Vue中可以通过监听滚动事件结合CSS的position: sticky或动态样式切换实现。 使用CSS的po…

vue实现筛选效果

vue实现筛选效果

Vue 实现筛选效果的方法 基于计算属性的筛选 在 Vue 中可以通过计算属性实现数据的动态筛选。定义一个计算属性,根据用户输入的筛选条件返回过滤后的数据列表。 <template>…