vue模拟摇杆组件

使用vue实现模拟摇杆组件

点击中心圆拖动鼠标,最大范围是外圈圆,实时获取摇动角度

新建rocker.vue

html 复制代码
<script setup lang="ts">

const { r } = withDefaults(defineProps<{
  r: number,
  inR: number
}>(), {
  r: 100,
  inR: 30
})

let outCircleRef = ref<HTMLElement>()
let innerCircleRef = ref<HTMLElement>()
let mouseAngle = ref<number>(0)
let mouseAngleComp = computed(() => {
  return mouseAngle.value.toFixed(2) + '°'
})

defineExpose({ mouseAngleComp })

function mouseDown(e: MouseEvent) {
  document.body.addEventListener('mousemove', mouseMove)
}

function mouseMove(e: MouseEvent) {
  if (outCircleRef.value && innerCircleRef.value) {
    let centerX = outCircleRef.value.offsetLeft + r
    let centerY = outCircleRef.value.offsetTop + r
    let disToCenter = distance(centerX, centerY, e.clientX, e.clientY)
    let ratioX = 0
    let ratioY = 0
    if (disToCenter <= r) {
      ratioX = (e.clientX - outCircleRef.value.offsetLeft) / (r * 2) * 100
      ratioY = (e.clientY - outCircleRef.value.offsetTop) / (r * 2) * 100
    } else {
      let copyX = e.clientX
      let copyY = e.clientY
      while (disToCenter > r) {
        copyX += copyX > centerX ? -2 : 2
        copyY += copyY > centerY ? -2 : 2
        disToCenter = distance(centerX, centerY, copyX, copyY)
      }
      ratioX = (copyX - outCircleRef.value.offsetLeft) / (r * 2) * 100
      ratioY = (copyY - outCircleRef.value.offsetTop) / (r * 2) * 100
    }
    innerCircleRef.value.style.left = ratioX + '%'
    innerCircleRef.value.style.top = ratioY + '%'
    mouseAngle.value = (Math.atan2(ratioX, ratioY) * 180) % 360
  }
}

function mouseUp(e: MouseEvent) {
  document.body.removeEventListener('mousemove', mouseMove)
  innerCircleRef.value!.style.left = '50%'
  innerCircleRef.value!.style.top = '50%'
  mouseAngle.value = 0
}

function distance(x0: number, y0: number, x1: number, y1: number) {
  return Math.sqrt((x1 - x0) ** 2 + (y1 - y0) ** 2)
}

onMounted(() => {
  innerCircleRef.value?.addEventListener('mousedown', mouseDown)
  document.body.addEventListener('mouseup', mouseUp)
})

</script>

<template>
  <div>
    <div class="out_circle" ref="outCircleRef" :style="{ width: r * 2 + 'px', height: r * 2 + 'px' }">
      <div class="inner_circle" ref="innerCircleRef" :style="{ width: inR * 2 + 'px', height: inR * 2 + 'px' }">
        Press
      </div>
    </div>
  </div>
</template>

<style scoped>
.out_circle {
  border-radius: 50%;
  background-color: #ddd;
  position: relative;
  margin-top: 8px;

  .inner_circle {
    border-radius: 50%;
    background-color: #999;
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    cursor: pointer;
    display: flex;
    justify-content: center;
    align-items: center;
    font-weight: bold;
    user-select: none;

    &:active {
      background: #666;
    }
  }
}
</style>

使用:

html 复制代码
<rocker ref="rockerRef" :r="100" :in-r="50"/>
相关推荐
小高0074 小时前
读懂 Tailwind v4:为什么它是现代前端项目的必选项?
前端·javascript·vue.js
boooooooom4 小时前
computed、watch 与 watchEffect 的使用边界与实战指南
javascript·vue.js
我的golang之路果然有问题4 小时前
python中 unicorn 热重启问题和 debug 的 json
java·服务器·前端·python·json
SpringLament4 小时前
从零打造AI智能博客:一个项目带你入门全栈与大模型应用开发
前端·aigc
晴虹4 小时前
lecen:一个更好的开源可视化系统搭建项目--数据、请求、寄连对象使用--全低代码|所见即所得|利用可视化设计器构建你的应用系统-做一个懂你的人
前端·后端·低代码
MOON404☾4 小时前
004.漏洞分析与利用
前端·网络·网络安全·系统安全·firefox
kylezhao20195 小时前
C#根据时间加密和防止反编译
java·前端·c#
愈努力俞幸运5 小时前
volta教程 下载安装使用
前端
冰暮流星5 小时前
javascript短路运算
开发语言·前端·javascript
qq_419854055 小时前
移动端开发:h5应用开发
前端