夜猫子的知识栈 夜猫子的知识栈
首页
  • 前端文章

    • JavaScript
  • 学习笔记

    • 《JavaScript教程》
    • 《Web Api》
    • 《ES6教程》
    • 《Vue》
    • 《React》
    • 《TypeScript》
    • 《Git》
    • 《Uniapp》
    • 小程序笔记
    • 《Electron》
    • JS设计模式总结
  • 《前端架构》

    • 《微前端》
    • 《权限控制》
    • monorepo
  • 全栈项目

    • 任务管理日历
    • 无代码平台
    • 图书管理系统
  • HTML
  • CSS
  • Nodejs
  • Midway
  • Nest
  • MySql
  • 其他
  • 技术文档
  • GitHub技巧
  • 博客搭建
  • Ajax
  • Vite
  • Vitest
  • Nuxt
  • UI库文章
  • Docker
  • 学习
  • 面试
  • 心情杂货
  • 实用技巧
  • 友情链接
收藏
  • 分类
  • 标签
  • 归档
GitHub (opens new window)

夜猫子

前端练习生
首页
  • 前端文章

    • JavaScript
  • 学习笔记

    • 《JavaScript教程》
    • 《Web Api》
    • 《ES6教程》
    • 《Vue》
    • 《React》
    • 《TypeScript》
    • 《Git》
    • 《Uniapp》
    • 小程序笔记
    • 《Electron》
    • JS设计模式总结
  • 《前端架构》

    • 《微前端》
    • 《权限控制》
    • monorepo
  • 全栈项目

    • 任务管理日历
    • 无代码平台
    • 图书管理系统
  • HTML
  • CSS
  • Nodejs
  • Midway
  • Nest
  • MySql
  • 其他
  • 技术文档
  • GitHub技巧
  • 博客搭建
  • Ajax
  • Vite
  • Vitest
  • Nuxt
  • UI库文章
  • Docker
  • 学习
  • 面试
  • 心情杂货
  • 实用技巧
  • 友情链接
收藏
  • 分类
  • 标签
  • 归档
GitHub (opens new window)
  • 实现圆弧拖动

夜猫子
2022-11-11
随笔

实现圆弧拖动

<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style>
  /* reset */
*, *::before, *::after {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

/* Variables */
/* content */
#app {
  height: 100%;
  display: flex;
  justify-content: center;
  align-items: center;
  background-color: #264653;
}

.circle-container {
  width: 320px;
  height: 320px;
  position: relative;
}
.circle-container .circle {
  position: absolute;
  z-index: 10;
  left: 0;
  top: 0;
  width: 320px;
  height: 320px;
  border-radius: 50%;
  border: 10px solid #ffffff;
  display: flex;
  justify-content: center;
  align-items: center;
  color: #ffffff;
  font-size: 3rem;
  -webkit-user-select: none;
}
.circle-container .circle-bar {
  position: absolute;
  z-index: 20;
  left: 145px;
  top: -10px;
  width: 30px;
  height: 30px;
  border-radius: 50%;
  background-color: #e76f51;
  cursor: pointer;
  box-shadow: 0 0 1px 1px #777777;
}

/*# sourceMappingURL=main.css.map */
  </style>
</head>
<body>
  <div id="app">
    <div class="circle-container">
      <div class="circle">0</div>
      <div class="circle-bar"></div>
    </div>
  </div>
  
  <script>
  const $bar = document.querySelector('.circle-bar')
const $circle = document.querySelector('.circle')
const $container = document.querySelector('.circle-container')

const circle_r = 320 / 2  // 圆形半径
const circle_b = 10       // 圆形边距(border-width)
const bar_r = 15          // 小球半径

function init() {
  let isDrag = false
  let mouse_offset = { x: 0, y: 0 }
  $bar.addEventListener('mousedown', (e) => {
    isDrag = true
    mouse_offset = getMouseOffset(e)
  })

  window.addEventListener('mousemove', (e) => {
    if (!isDrag) return
    const event_offset_x = (e.clientX
      - mouse_offset.x
      - $container.offsetLeft
      - circle_r)
    const event_offset_y = -(e.clientY
      - mouse_offset.y
      - $container.offsetTop
      - circle_r)

    const radian = Math.atan2(event_offset_y, event_offset_x)
    const x = Math.cos(radian) * (circle_r - circle_b / 2)
    const y = Math.sin(radian) * (circle_r - circle_b / 2)

    $bar.style.left = x - bar_r + circle_r + 'px'
    $bar.style.top = circle_r - (y + bar_r) + 'px'

    updateContent(radian)
  })

  window.addEventListener('mouseup', (e) => {
    isDrag = false
  })
}

function updateContent(radian) {
  let angle = radian * (180 / Math.PI)  // -180 ~ 180
  if (angle >= -180 && angle <= 90) {
    angle = 90 - angle
  } else {
    angle = 360 - (angle - 90)
  }
  // 0 ~ 360

  $circle.textContent = Math.floor(angle)
}

function getMouseOffset(e) {
  
  const mouse_offset_x = (e.clientX
    - $container.offsetLeft
    - e.currentTarget.offsetLeft
    - bar_r)
  const mouse_offset_y = -(e.clientY
    - $container.offsetTop
    - e.currentTarget.offsetTop
    - bar_r)

    console.log(e.clientX, $container.offsetLeft, e.currentTarget.offsetLeft,mouse_offset_x,$container)
  return {
    x: mouse_offset_x,
    y: mouse_offset_y
  }
}

init()
  </script>
</body>
</html>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
编辑 (opens new window)
上次更新: 2023/7/11 18:57:41
最近更新
01
IoC 解决了什么痛点问题?
03-10
02
如何调试 Nest 项目
03-10
03
Provider注入对象
03-10
更多文章>
Copyright © 2019-2025 Study | MIT License
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式