当前位置:首页 > VUE

vue实现烟花效果

2026-02-18 03:54:50VUE

使用 Canvas 绘制基础烟花

在 Vue 中创建一个 Canvas 元素,通过 JavaScript 动态绘制烟花效果。初始化 Canvas 并设置其宽高为全屏,监听点击事件触发烟花动画。

<template>
  <canvas ref="canvas" @click="createFirework"></canvas>
</template>

<script>
export default {
  mounted() {
    this.canvas = this.$refs.canvas;
    this.ctx = this.canvas.getContext('2d');
    this.resizeCanvas();
    window.addEventListener('resize', this.resizeCanvas);
  },
  methods: {
    resizeCanvas() {
      this.canvas.width = window.innerWidth;
      this.canvas.height = window.innerHeight;
    },
    createFirework(e) {
      const x = e.clientX;
      const y = e.clientY;
      this.animateFirework(x, y);
    }
  }
}
</script>

烟花粒子动画实现

定义粒子类并实现扩散效果。每个烟花由多个粒子组成,粒子以随机角度和速度向外扩散,同时受到重力影响下坠。

vue实现烟花效果

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;
  }

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

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

动画循环与性能优化

使用 requestAnimationFrame 实现平滑动画,管理粒子数组并定期清理已完成动画的粒子以避免内存泄漏。

vue实现烟花效果

animateFirework(x, y) {
  const particles = [];
  const hue = Math.floor(Math.random() * 360);
  const color = `hsl(${hue}, 100%, 50%)`;

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

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

    for (let i = 0; i < particles.length; i++) {
      if (particles[i].update()) {
        particles[i].draw(this.ctx);
      } else {
        particles.splice(i, 1);
        i--;
      }
    }

    if (particles.length > 0) {
      requestAnimationFrame(animate);
    }
  };

  animate();
}

添加自动发射功能

通过定时器实现自动发射烟花效果,增强视觉体验。可以设置随机位置或跟随鼠标轨迹发射。

data() {
  return {
    autoFireInterval: null
  };
},
methods: {
  startAutoFire() {
    this.autoFireInterval = setInterval(() => {
      const x = Math.random() * this.canvas.width;
      const y = Math.random() * this.canvas.height / 2;
      this.animateFirework(x, y);
    }, 800);
  },
  stopAutoFire() {
    clearInterval(this.autoFireInterval);
  }
},
mounted() {
  // ...其他初始化代码
  this.startAutoFire();
},
beforeDestroy() {
  this.stopAutoFire();
  window.removeEventListener('resize', this.resizeCanvas);
}

样式优化与响应式处理

为 Canvas 添加基础样式确保全屏显示,并通过 CSS 处理层级关系。

<style scoped>
canvas {
  position: fixed;
  top: 0;
  left: 0;
  z-index: -1;
  background: #000;
}
</style>

以上实现提供了完整的 Vue 烟花效果组件,包含点击交互、自动发射、粒子动画和性能优化等核心功能。可根据需要调整粒子数量、颜色、速度和重力等参数获得不同视觉效果。

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

相关文章

vue实现效果展示

vue实现效果展示

Vue 实现效果展示的方法 动态数据绑定展示 通过 Vue 的响应式特性,实时展示数据变化。例如,表单输入与预览同步: <template> <div> <…

vue实现预览效果

vue实现预览效果

Vue 实现预览效果的方法 使用 v-html 指令实现简单预览 在 Vue 中可以通过 v-html 指令直接将 HTML 字符串渲染到页面上,适用于简单的富文本预览场景。 <templat…

vue实现点选效果

vue实现点选效果

实现点选效果的基本思路 在Vue中实现点选效果通常涉及监听用户的点击事件,并通过动态绑定类名或样式来改变元素的外观。核心是利用Vue的响应式数据和事件绑定机制。 使用v-bind和v-on实现 通过…

vue实现开关效果

vue实现开关效果

使用Vue实现开关效果 使用v-model绑定数据 通过v-model绑定一个布尔值来控制开关状态。当用户点击开关时,布尔值会自动更新。 <template> <div cla…

vue实现动画效果

vue实现动画效果

Vue 实现动画效果的方法 Vue 提供了多种方式来实现动画效果,包括内置的过渡系统、第三方动画库以及 CSS 动画。以下是几种常见的方法: 使用 Vue 内置的过渡系统 Vue 的 <tra…

vue 实现滑动效果

vue 实现滑动效果

Vue 实现滑动效果的方法 使用 CSS Transition 通过 Vue 的 v-if 或 v-show 结合 CSS Transition 实现滑动效果。定义一个 CSS 类,利用 transf…