当前位置:首页 > VUE

vue实现烟花

2026-02-10 15:55:47VUE

Vue实现烟花效果的方法

在Vue中实现烟花效果可以通过Canvas绘制或CSS动画两种方式。以下是具体实现方法:

使用Canvas绘制烟花

在Vue组件中创建Canvas元素并绘制烟花动画:

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

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

      class Particle {
        constructor(x, y, color) {
          this.x = x;
          this.y = y;
          this.color = color;
          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;
        }

        draw() {
          ctx.globalAlpha = this.alpha;
          ctx.fillStyle = this.color;
          ctx.beginPath();
          ctx.arc(this.x, this.y, 2, 0, Math.PI * 2);
          ctx.fill();
        }

        update() {
          this.velocity.y += 0.05;
          this.x += this.velocity.x;
          this.y += this.velocity.y;
          this.alpha -= this.decay;
          this.draw();
          return this.alpha > 0;
        }
      }

      let particles = [];

      function createFirework(x, y) {
        const color = `hsl(${Math.random() * 360}, 100%, 50%)`;
        const particleCount = 150;

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

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

        particles = particles.filter(particle => particle.update());
      }

      animate();

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

      // 自动发射烟花
      setInterval(() => {
        const x = Math.random() * canvas.width;
        const y = Math.random() * canvas.height / 2;
        createFirework(x, y);
      }, 1000);
    }
  }
}
</script>

<style>
.fireworks-container {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  pointer-events: none;
}
</style>

使用CSS动画实现简单烟花

对于简单的烟花效果,可以使用纯CSS实现:

<template>
  <div class="firework" v-for="(firework, index) in fireworks" :key="index" 
       :style="firework.style"></div>
</template>

<script>
export default {
  data() {
    return {
      fireworks: []
    }
  },
  methods: {
    launchFirework() {
      const colors = ['#ff0000', '#00ff00', '#0000ff', '#ffff00', '#ff00ff'];
      const color = colors[Math.floor(Math.random() * colors.length)];

      this.fireworks.push({
        style: {
          left: `${Math.random() * 100}%`,
          top: `${Math.random() * 100}%`,
          '--color': color
        }
      });

      setTimeout(() => {
        this.fireworks.shift();
      }, 1000);
    }
  },
  mounted() {
    setInterval(this.launchFirework, 500);
  }
}
</script>

<style>
.firework {
  position: fixed;
  width: 5px;
  height: 5px;
  border-radius: 50%;
  background-color: var(--color);
  animation: explode 1s ease-out forwards;
  transform: translate(-50%, -50%);
}

@keyframes explode {
  0% {
    transform: translate(-50%, -50%) scale(1);
    opacity: 1;
  }
  100% {
    transform: translate(-50%, -50%) scale(20);
    opacity: 0;
  }
}
</style>

使用第三方库

对于更复杂的烟花效果,可以使用第三方库如canvas-confetti

<template>
  <button @click="fireConfetti">发射烟花</button>
</template>

<script>
import confetti from 'canvas-confetti';

export default {
  methods: {
    fireConfetti() {
      confetti({
        particleCount: 100,
        spread: 70,
        origin: { y: 0.6 }
      });
    }
  }
}
</script>

性能优化建议

对于Canvas实现的烟花效果,需要注意性能优化:

vue实现烟花

  • 限制同时显示的粒子数量
  • 使用requestAnimationFrame进行动画循环
  • 避免频繁的DOM操作
  • 对于移动端,适当减少粒子数量

以上方法可以根据实际需求选择使用,Canvas实现的效果更加丰富,CSS实现更加简单,第三方库则提供了开箱即用的解决方案。

标签: 烟花vue
分享给朋友:

相关文章

vue实现本地数据存储

vue实现本地数据存储

使用 localStorage 或 sessionStorage Vue 中可以直接调用浏览器提供的 localStorage 或 sessionStorage API 进行数据存储。localSto…

vue实现ppt

vue实现ppt

Vue 实现 PPT 的功能 Vue 可以结合第三方库或自定义组件实现类似 PPT 的演示效果。以下是几种常见的方法: 使用第三方库 reveal.js reveal.js 是一个流行的演示框架,支…

vue实现快手

vue实现快手

Vue 实现类似快手的功能 Vue.js 是一个渐进式 JavaScript 框架,适合构建复杂的单页应用。要实现类似快手的功能,可以结合 Vue 和相关技术栈进行开发。 核心功能模块 视频流展示…

vue实现搜索查询

vue实现搜索查询

Vue 实现搜索查询的方法 双向绑定实现实时搜索 在Vue中可以通过v-model绑定输入框的值,结合计算属性或watch监听输入变化实现实时搜索。 <template> <i…

vue实现点击提交

vue实现点击提交

Vue 实现点击提交功能 在 Vue 中实现点击提交功能通常涉及表单处理和事件绑定。以下是几种常见的实现方式: 使用 v-on 或 @ 绑定点击事件 通过 v-on:click 或简写 @click…

vue实现力导向

vue实现力导向

Vue 实现力导向图 在 Vue 中实现力导向图(Force-Directed Graph)通常需要借助第三方库,如 D3.js 或 vis.js。以下是两种常见的实现方式: 使用 D3.js 实现…