当前位置:首页 > VUE

vue实现双摇杆

2026-01-15 23:51:31VUE

Vue 实现双摇杆

基本思路

在Vue中实现双摇杆功能通常需要结合Canvas或DOM元素监听触摸/鼠标事件。核心逻辑包括摇杆位置计算、事件触发和UI渲染。

实现步骤

1. 创建摇杆组件

<template>
  <div class="joystick-container">
    <div class="joystick-background" ref="bg"></div>
    <div class="joystick-thumb" ref="thumb"></div>
  </div>
</template>

2. 样式设置

.joystick-container {
  position: relative;
  width: 100px;
  height: 100px;
}

.joystick-background {
  width: 100%;
  height: 100%;
  border-radius: 50%;
  background: rgba(0,0,0,0.2);
}

.joystick-thumb {
  position: absolute;
  width: 40px;
  height: 40px;
  border-radius: 50%;
  background: rgba(255,255,255,0.5);
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
}

3. 摇杆逻辑实现

export default {
  mounted() {
    this.initJoystick();
  },
  methods: {
    initJoystick() {
      const bg = this.$refs.bg;
      const thumb = this.$refs.thumb;

      const handleStart = (e) => {
        e.preventDefault();
        this.isActive = true;
        this.updatePosition(e);
      };

      const handleMove = (e) => {
        if (!this.isActive) return;
        this.updatePosition(e);
      };

      const handleEnd = () => {
        this.isActive = false;
        thumb.style.transform = 'translate(-50%, -50%)';
      };

      bg.addEventListener('touchstart', handleStart);
      bg.addEventListener('mousedown', handleStart);

      document.addEventListener('touchmove', handleMove);
      document.addEventListener('mousemove', handleMove);

      document.addEventListener('touchend', handleEnd);
      document.addEventListener('mouseup', handleEnd);
    },

    updatePosition(e) {
      const bg = this.$refs.bg;
      const thumb = this.$refs.thumb;
      const rect = bg.getBoundingClientRect();

      const centerX = rect.left + rect.width/2;
      const centerY = rect.top + rect.height/2;

      let clientX, clientY;
      if (e.type.includes('touch')) {
        clientX = e.touches[0].clientX;
        clientY = e.touches[0].clientY;
      } else {
        clientX = e.clientX;
        clientY = e.clientY;
      }

      const deltaX = clientX - centerX;
      const deltaY = clientY - centerY;

      const distance = Math.sqrt(deltaX*deltaX + deltaY*deltaY);
      const maxDistance = rect.width/2;

      let ratio = 1;
      if (distance > maxDistance) {
        ratio = maxDistance / distance;
      }

      const x = deltaX * ratio;
      const y = deltaY * ratio;

      thumb.style.transform = `translate(calc(-50% + ${x}px), calc(-50% + ${y}px))`;

      // 触发自定义事件
      this.$emit('move', {
        x: x / maxDistance,
        y: y / maxDistance
      });
    }
  }
}

4. 双摇杆集成

<template>
  <div class="game-container">
    <Joystick 
      class="left-joystick" 
      @move="handleLeftMove" 
    />
    <Joystick 
      class="right-joystick" 
      @move="handleRightMove" 
    />
  </div>
</template>

<script>
import Joystick from './Joystick.vue';

export default {
  components: { Joystick },
  methods: {
    handleLeftMove(data) {
      // 处理左摇杆移动
      console.log('Left:', data);
    },
    handleRightMove(data) {
      // 处理右摇杆移动
      console.log('Right:', data);
    }
  }
}
</script>

5. 移动端适配

.game-container {
  position: fixed;
  bottom: 20px;
  width: 100%;
  display: flex;
  justify-content: space-between;
  padding: 0 20px;
  box-sizing: border-box;
}

.left-joystick {
  left: 20px;
}

.right-joystick {
  right: 20px;
}

进阶优化

性能优化

  • 使用requestAnimationFrame进行平滑渲染
  • 节流事件处理频率

功能扩展

  • 添加摇杆力度反馈
  • 实现摇杆自动回中
  • 添加触摸区域限制

事件处理

vue实现双摇杆

  • 支持自定义事件回调
  • 添加摇杆状态(active/inactive)变化事件

这种实现方式适用于大多数需要双摇杆控制的游戏或交互应用,可根据具体需求调整摇杆大小、灵敏度和反馈效果。

标签: 摇杆vue
分享给朋友:

相关文章

vue 实现全屏

vue 实现全屏

Vue 实现全屏功能 在 Vue 中实现全屏功能可以通过浏览器提供的 Fullscreen API 完成。以下是一个完整的实现方案: 安装依赖(可选) 如果需要跨浏览器兼容性更好,可以安装 scre…

vue实现心电图

vue实现心电图

Vue 实现心电图效果 在 Vue 中实现心电图效果通常需要结合 Canvas 或 SVG 进行动态绘图,模拟心电图数据的实时变化。以下是两种实现方式的核心思路和代码示例: 使用 Canvas 绘制…

vue怎么实现onclick

vue怎么实现onclick

在Vue中实现点击事件(类似原生onclick)可以通过多种方式完成,以下是常见方法: 使用 v-on 指令 Vue提供了v-on指令绑定DOM事件,简写为@。例如绑定点击事件: <butt…

vue实现登录退出

vue实现登录退出

实现登录功能 在Vue中实现登录功能通常需要结合表单验证、API请求和状态管理。以下是一个基础实现示例: 安装必要依赖(如axios和vuex): npm install axios vuex 创…

vue实现产品搜索

vue实现产品搜索

实现产品搜索功能 在Vue中实现产品搜索功能需要结合前端界面和后端数据处理。以下是实现的基本思路和代码示例: 数据准备 创建一个产品数据数组,包含需要搜索的产品信息: data() { ret…

vue实现列表循环

vue实现列表循环

Vue 列表循环的实现方法 在 Vue 中,可以通过 v-for 指令实现列表循环渲染。以下是几种常见的实现方式: 基础列表渲染 <template> <ul>…