当前位置:首页 > 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. 摇杆逻辑实现

vue实现双摇杆

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. 移动端适配

vue实现双摇杆

.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进行平滑渲染
  • 节流事件处理频率

功能扩展

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

事件处理

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

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

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

相关文章

vue实现密码框的包

vue实现密码框的包

Vue 密码框实现方案 在 Vue 中实现密码框功能,可以通过原生 HTML 结合 Vue 指令或使用第三方组件库完成。以下是几种常见方案: 原生 HTML + Vue 双向绑定 <t…

vue实现粘贴

vue实现粘贴

Vue 实现粘贴功能的方法 在 Vue 中实现粘贴功能通常涉及监听粘贴事件并处理剪贴板数据。以下是几种常见的实现方式: 监听原生粘贴事件 通过 @paste 指令或原生 addEventListen…

vue 实现表单

vue 实现表单

Vue 表单实现方法 Vue.js 提供了多种方式实现表单,包括双向数据绑定、表单验证和动态表单生成。以下是常见的实现方法: 基础表单绑定 使用 v-model 指令实现表单元素与数据的双向绑定:…

vue实现popup

vue实现popup

Vue 实现 Popup 弹窗 使用 Vue 原生组件 创建一个基本的 Vue 组件作为弹窗,通过 v-if 或 v-show 控制显示隐藏。 <template> <div&…

vue实现浮标

vue实现浮标

Vue 实现浮动按钮(浮标) 使用 Vue 实现浮动按钮可以通过多种方式完成,以下是几种常见的方法: 使用 CSS 定位和 Vue 组件 创建 Vue 组件并配合 CSS 固定定位实现浮动按钮:…

vue实现Siri

vue实现Siri

Vue 实现 Siri 风格语音交互 实现类似 Siri 的语音交互功能需要结合语音识别、语音合成和前端交互逻辑。Vue 作为前端框架,可以很好地管理这些功能的交互状态。 语音识别集成 使用 Web…