当前位置:首页 > VUE

vue实现双摇杆

2026-02-17 01:07:13VUE

双摇杆实现思路

双摇杆通常用于游戏或交互式应用中,一个摇杆控制角色移动,另一个控制视角或攻击方向。Vue中可以通过监听触摸或鼠标事件实现。

基础HTML结构

创建两个摇杆的DOM元素,分别用不同类名标识:

<template>
  <div class="joystick-container">
    <div class="joystick-movement" ref="movementJoystick"></div>
    <div class="joystick-aiming" ref="aimingJoystick"></div>
  </div>
</template>

样式设置

使用CSS定位摇杆元素,设置基本样式:

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

.joystick-movement, .joystick-aiming {
  position: absolute;
  width: 100px;
  height: 100px;
  border-radius: 50%;
  background: rgba(0, 0, 0, 0.3);
}

.joystick-movement {
  bottom: 50px;
  left: 50px;
}

.joystick-aiming {
  bottom: 50px;
  right: 50px;
}

摇杆逻辑实现

在Vue组件中实现摇杆控制逻辑:

export default {
  mounted() {
    this.initJoystick(this.$refs.movementJoystick, 'movement');
    this.initJoystick(this.$refs.aimingJoystick, 'aiming');
  },
  methods: {
    initJoystick(element, type) {
      const stick = document.createElement('div');
      stick.className = 'joystick-handle';
      element.appendChild(stick);

      const rect = element.getBoundingClientRect();
      const center = {
        x: rect.left + rect.width / 2,
        y: rect.top + rect.height / 2
      };

      const handleMove = (e) => {
        const clientX = e.touches ? e.touches[0].clientX : e.clientX;
        const clientY = e.touches ? e.touches[0].clientY : e.clientY;

        const deltaX = clientX - center.x;
        const deltaY = clientY - center.y;
        const distance = Math.min(Math.sqrt(deltaX * deltaX + deltaY * deltaY), rect.width / 2);

        const angle = Math.atan2(deltaY, deltaX);
        const x = distance * Math.cos(angle);
        const y = distance * Math.sin(angle);

        stick.style.transform = `translate(${x}px, ${y}px)`;

        // 根据type分发不同事件
        const event = new CustomEvent(`${type}-joystick`, {
          detail: {
            x: x / (rect.width / 2),
            y: y / (rect.height / 2)
          }
        });
        window.dispatchEvent(event);
      };

      const handleEnd = () => {
        stick.style.transform = 'translate(0, 0)';
        window.dispatchEvent(new CustomEvent(`${type}-joystick`, {
          detail: { x: 0, y: 0 }
        }));
      };

      element.addEventListener('touchstart', (e) => {
        e.preventDefault();
        handleMove(e);
        document.addEventListener('touchmove', handleMove);
        document.addEventListener('touchend', handleEnd, { once: true });
      });

      element.addEventListener('mousedown', () => {
        document.addEventListener('mousemove', handleMove);
        document.addEventListener('mouseup', handleEnd, { once: true });
      });
    }
  }
}

摇杆手柄样式

添加摇杆手柄的CSS样式:

.joystick-handle {
  position: absolute;
  width: 40px;
  height: 40px;
  border-radius: 50%;
  background: rgba(255, 255, 255, 0.7);
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  pointer-events: none;
}

事件监听使用

在其他组件中监听摇杆事件:

mounted() {
  window.addEventListener('movement-joystick', (e) => {
    console.log('移动摇杆:', e.detail);
  });

  window.addEventListener('aiming-joystick', (e) => {
    console.log('瞄准摇杆:', e.detail);
  });
}

摇杆限制优化

添加摇杆移动范围限制,防止手柄超出摇杆区域:

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

移动平滑处理

添加缓动效果使摇杆移动更平滑:

stick.style.transition = distance === 0 ? 'transform 0.2s ease-out' : 'none';

销毁事件监听

在组件销毁时移除事件监听:

vue实现双摇杆

beforeDestroy() {
  window.removeEventListener('movement-joystick');
  window.removeEventListener('aiming-joystick');
}

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

相关文章

vue 实现blog

vue 实现blog

Vue 实现博客的基本步骤 使用 Vue 实现博客可以分为前端和后端两部分,前端使用 Vue.js 框架,后端可以选择 Node.js、Python 或其他服务端语言。以下是一个基于 Vue 的博客实…

vue实现modal

vue实现modal

Vue 实现 Modal 弹窗组件 使用 Vue 实现 Modal 弹窗组件可以通过多种方式,包括原生 Vue 组件、第三方库或自定义指令。以下是几种常见方法: 原生 Vue 组件实现 创建一个基础…

vue vnode实现

vue vnode实现

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

vue实现评分

vue实现评分

Vue 实现评分功能 使用组件库实现 安装 element-ui 或 ant-design-vue 这类 UI 库,它们已经内置了评分组件。 以 element-ui 为例: <te…

vue删除实现

vue删除实现

Vue 删除功能的实现方法 在 Vue 中实现删除功能通常涉及以下几个步骤: 方法一:使用数组的 splice 方法 通过索引删除数组中的元素,适用于列表渲染的场景。 methods: {…

vue 实现hover

vue 实现hover

Vue 实现 Hover 效果的方法 在 Vue 中实现 hover 效果可以通过多种方式完成,以下是几种常见的方法: 使用 CSS 伪类 :hover 最简单的方式是直接使用 CSS 的 :hov…