当前位置:首页 > VUE

vue实现子弹循环

2026-01-15 07:52:20VUE

Vue实现子弹循环动画

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

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

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

<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性能更好:

vue实现子弹循环

<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实现按钮刷新

vue实现按钮刷新

Vue 实现按钮刷新功能 在 Vue 中实现按钮刷新功能可以通过多种方式实现,具体取决于刷新需求是局部刷新还是全局刷新。以下是几种常见的方法: 使用 window.location.reload()…

vue实现多用户登录

vue实现多用户登录

实现多用户登录的基本思路 在Vue中实现多用户登录通常需要结合后端API完成身份验证,并通过前端路由、状态管理(如Vuex或Pinia)和本地存储(如localStorage)来管理用户会话。以下是关…

vue 实现挂载

vue 实现挂载

Vue 实现挂载的基本方法 在 Vue 中,挂载是将 Vue 实例与 DOM 元素关联的过程。通过挂载,Vue 可以管理 DOM 并实现数据绑定和响应式更新。 创建 Vue 实例并挂载 const…

vue实现hovuer

vue实现hovuer

Vue 实现 Hover 效果 在 Vue 中实现 hover 效果可以通过多种方式完成,以下是几种常见的方法: 使用 CSS 直接控制 通过 :hover 伪类实现,无需额外的 JavaScrip…

vue实现刷卡

vue实现刷卡

Vue 实现刷卡效果 在 Vue 中实现刷卡效果可以通过 CSS 动画和 Vue 的过渡系统结合完成。以下是一种常见的实现方式: 基础实现方法 安装 Vue 过渡依赖(如果尚未安装): npm i…

vue 实现 confirm

vue 实现 confirm

实现确认对话框的方法 在Vue中实现确认对话框可以通过多种方式完成,包括使用内置组件、第三方库或自定义组件。 使用浏览器原生confirm 最简单的实现方式是直接调用浏览器原生的confirm方法。…