当前位置:首页 > VUE

vue实现子弹循环

2026-01-15 07:52:20VUE

Vue实现子弹循环动画

在Vue中实现子弹循环动画可以通过CSS动画或JavaScript动画库实现。以下是几种常见方法:

vue实现子弹循环

方法1:使用CSS动画和Vue过渡

通过Vue的<transition><transition-group>组件结合CSS动画实现子弹移动效果:

vue实现子弹循环

<template>
  <div class="bullet-container">
    <div 
      v-for="(bullet, index) in bullets" 
      :key="index"
      class="bullet"
      :style="{ left: bullet.x + 'px', top: bullet.y + 'px' }"
    ></div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      bullets: []
    }
  },
  mounted() {
    setInterval(() => {
      this.bullets.push({
        x: 0,
        y: Math.random() * 300
      })
    }, 500)

    setInterval(() => {
      this.bullets = this.bullets.map(bullet => ({
        ...bullet,
        x: bullet.x + 5
      })).filter(bullet => bullet.x < 800)
    }, 16)
  }
}
</script>

<style>
.bullet {
  position: absolute;
  width: 10px;
  height: 10px;
  background-color: red;
  border-radius: 50%;
  transition: left 0.1s linear;
}
.bullet-container {
  position: relative;
  width: 800px;
  height: 400px;
  border: 1px solid #ccc;
}
</style>

方法2:使用GSAP动画库

GSAP提供更强大的动画控制能力:

<template>
  <div ref="container" class="bullet-container">
    <div 
      v-for="(bullet, index) in bullets" 
      :key="index"
      ref="bullets"
      class="bullet"
    ></div>
  </div>
</template>

<script>
import { gsap } from 'gsap'

export default {
  data() {
    return {
      bullets: []
    }
  },
  mounted() {
    setInterval(() => {
      this.bullets.push({ id: Date.now() })
      this.$nextTick(() => {
        const bullet = this.$refs.bullets[this.$refs.bullets.length - 1]
        const containerWidth = this.$refs.container.offsetWidth

        gsap.fromTo(bullet, 
          { x: 0, y: Math.random() * 300 },
          { 
            x: containerWidth,
            duration: 2,
            onComplete: () => {
              this.bullets.shift()
            }
          }
        )
      })
    }, 500)
  }
}
</script>

方法3:使用Canvas绘制

对于大量子弹效果,Canvas性能更好:

<template>
  <canvas ref="canvas" width="800" height="400"></canvas>
</template>

<script>
export default {
  data() {
    return {
      bullets: [],
      ctx: null
    }
  },
  mounted() {
    this.ctx = this.$refs.canvas.getContext('2d')

    setInterval(() => {
      this.bullets.push({
        x: 0,
        y: Math.random() * 400,
        speed: 2 + Math.random() * 3
      })
    }, 300)

    requestAnimationFrame(this.animate)
  },
  methods: {
    animate() {
      this.ctx.clearRect(0, 0, 800, 400)

      this.bullets.forEach((bullet, index) => {
        bullet.x += bullet.speed
        this.ctx.beginPath()
        this.ctx.arc(bullet.x, bullet.y, 5, 0, Math.PI * 2)
        this.ctx.fillStyle = 'red'
        this.ctx.fill()

        if(bullet.x > 800) {
          this.bullets.splice(index, 1)
        }
      })

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

性能优化建议

  • 对于少量子弹元素,使用CSS动画或GSAP更简单
  • 对于大量子弹(50+),考虑使用Canvas或WebGL方案
  • 使用对象池技术复用DOM元素或Canvas绘制对象
  • 适当使用requestAnimationFrame进行动画循环
  • 在组件销毁时清除定时器和动画帧请求

以上方法可根据实际需求选择,CSS方案适合简单场景,GSAP提供更多动画控制,Canvas则适合高性能需求。

标签: 子弹vue
分享给朋友:

相关文章

vue实现3d宇宙

vue实现3d宇宙

Vue实现3D宇宙效果 使用Three.js库 Three.js是一个强大的JavaScript 3D库,可以轻松集成到Vue项目中。安装Three.js依赖: npm install three…

vue实现autocomplete

vue实现autocomplete

Vue 实现 Autocomplete 功能 Vue 中实现 Autocomplete(自动完成)功能可以通过自定义组件或使用现有库完成。以下是几种常见方法: 方法一:使用 Vue 原生实现 创建…

vue实现分类

vue实现分类

Vue实现分类功能的方法 使用v-for和计算属性 通过计算属性对数据进行分类处理,结合v-for渲染分类后的列表。示例代码: <template> <div>…

vue 实现拖拽

vue 实现拖拽

Vue 实现拖拽功能 使用 HTML5 原生拖拽 API HTML5 提供了原生的拖拽 API,可以通过 draggable 属性和相关事件实现拖拽功能。 <template> &l…

vue vnode实现

vue vnode实现

Vue VNode 实现原理 VNode(Virtual Node)是 Vue 的核心概念之一,用于描述真实 DOM 的轻量级虚拟表示。VNode 的实现涉及以下关键点: VNode 的基本结构 V…

vue实现博客

vue实现博客

Vue 实现博客的基本步骤 使用 Vue.js 实现一个博客系统可以分为前端和后端两部分。以下是基于 Vue 的前端实现方案,后端可以选择 Node.js、Django 或其他框架。 项目初始化 使…