当前位置:首页 > VUE

vue实现点击烟花

2026-02-18 18:19:07VUE

Vue实现点击烟花效果

在Vue中实现点击烟花效果,可以通过监听点击事件并动态生成烟花元素。以下是两种实现方式:

方式一:使用CSS动画和动态元素

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

<script>
export default {
  data() {
    return {
      fireworks: []
    }
  },
  methods: {
    createFirework(e) {
      const colors = ['#ff0000', '#00ff00', '#0000ff', '#ffff00', '#ff00ff']

      for (let i = 0; i < 50; i++) {
        const angle = Math.random() * Math.PI * 2
        const velocity = 5 + Math.random() * 7
        const size = 3 + Math.random() * 5

        this.fireworks.push({
          style: {
            left: `${e.clientX}px`,
            top: `${e.clientY}px`,
            backgroundColor: colors[Math.floor(Math.random() * colors.length)],
            transform: `translate(${Math.cos(angle) * velocity * 10}px, ${Math.sin(angle) * velocity * 10}px)`,
            width: `${size}px`,
            height: `${size}px`,
            opacity: 1
          }
        })
      }

      setTimeout(() => {
        this.fireworks = []
      }, 1000)
    }
  }
}
</script>

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

.firework {
  position: absolute;
  border-radius: 50%;
  transition: transform 1s ease-out, opacity 1s ease-out;
}
</style>

方式二:使用Canvas实现高性能烟花

<template>
  <canvas 
    ref="canvas" 
    @click="launchFirework"
    style="position: fixed; top: 0; left: 0; width: 100%; height: 100%;"
  ></canvas>
</template>

<script>
export default {
  mounted() {
    this.canvas = this.$refs.canvas
    this.ctx = this.canvas.getContext('2d')
    this.fireworks = []
    this.particles = []

    this.canvas.width = window.innerWidth
    this.canvas.height = window.innerHeight

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

    this.animate()
  },
  methods: {
    launchFirework(e) {
      const x = e.clientX
      const y = e.clientY

      this.fireworks.push({
        x: x,
        y: y,
        particles: Array(50).fill().map(() => ({
          x: x,
          y: y,
          vx: Math.random() * 6 - 3,
          vy: Math.random() * 6 - 3,
          color: `hsl(${Math.random() * 360}, 100%, 50%)`,
          size: Math.random() * 3 + 1,
          life: 100
        }))
      })
    },
    animate() {
      this.ctx.fillStyle = 'rgba(0, 0, 0, 0.1)'
      this.ctx.fillRect(0, 0, this.canvas.width, this.canvas.height)

      this.fireworks.forEach(fw => {
        fw.particles.forEach(p => {
          p.x += p.vx
          p.y += p.vy
          p.life--

          this.ctx.fillStyle = p.color
          this.ctx.globalAlpha = p.life / 100
          this.ctx.beginPath()
          this.ctx.arc(p.x, p.y, p.size, 0, Math.PI * 2)
          this.ctx.fill()
        })
      })

      this.fireworks = this.fireworks.filter(fw => 
        fw.particles.some(p => p.life > 0)
      )

      requestAnimationFrame(this.animate)
    }
  }
}
</script>

实现要点

第一种方式使用Vue的动态渲染和CSS动画,适合简单的烟花效果。第二种方式使用Canvas,性能更好,适合复杂的烟花效果和大量粒子。

两种方式都可以通过调整参数来改变烟花效果:

vue实现点击烟花

  • 粒子数量影响烟花密度
  • 粒子速度影响烟花扩散范围
  • 粒子大小影响视觉效果
  • 颜色可以随机生成或使用特定配色方案

可以根据实际需求选择适合的实现方式,或结合两者优势创建更丰富的交互效果。

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

相关文章

vue datepicker 实现

vue datepicker 实现

实现 Vue Datepicker 的基本方法 在 Vue 项目中实现日期选择功能,可以使用第三方库如 vue-datepicker 或 v-calendar。以下是两种常见实现方式: 安装 vue…

vue实现frame

vue实现frame

Vue 中实现 iframe 的方法 在 Vue 中可以通过直接使用 <iframe> 标签或动态绑定 src 属性来实现 iframe 功能。 基本用法 <template&g…

vue实现前端

vue实现前端

Vue 实现前端的核心方法与步骤 安装 Vue 项目 使用 Vue CLI 或 Vite 创建项目。Vue CLI 是传统构建工具,Vite 是新一代轻量级工具。 npm init vue@la…

vue 绑定实现

vue 绑定实现

Vue 绑定实现 Vue 提供了多种数据绑定的方式,包括文本插值、属性绑定、事件绑定、双向绑定等。以下是常见的绑定实现方法: 文本插值 使用双大括号 {{ }} 进行文本插值,将数据动态渲染到 DO…

vue实现链接

vue实现链接

Vue 实现链接的方法 在 Vue 中实现链接可以通过多种方式,包括使用 <router-link>、<a> 标签或编程式导航。以下是几种常见的方法: 使用 <rout…

vue 实现赋值

vue 实现赋值

Vue 实现赋值的方法 在 Vue 中,赋值操作通常涉及数据绑定和响应式更新。以下是几种常见的赋值方式: 直接赋值给 data 属性 在 Vue 实例的 data 对象中定义的属性可以直接通过 th…