-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathApp.vue
109 lines (92 loc) · 2.01 KB
/
App.vue
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
<script setup lang="ts">
import { ref } from 'vue'
import { useSpring } from '../../src/vue'
const dotLeftPosition = {
x: 100,
y: 200,
}
const dotRightPosition = {
x: 500,
y: 200,
}
const position = ref({
...dotLeftPosition,
})
const isDragging = ref(false)
const { style, realVelocity } = useSpring(
() => ({
translate: `${position.value.x}px ${position.value.y}px`,
}),
() => ({
duration: 800,
disabled: isDragging.value,
}),
)
const onPointerDown = (e: PointerEvent) => {
isDragging.value = true
;(e.currentTarget as HTMLElement).setPointerCapture(e.pointerId)
}
const onPointerMove = (e: PointerEvent) => {
if (!isDragging.value) {
return
}
position.value.x = e.clientX
position.value.y = e.clientY
}
const onPointerUp = (e: PointerEvent) => {
if (!isDragging.value) {
return
}
isDragging.value = false
const velocityX = realVelocity.value.translate[0]!
const threshold = (dotLeftPosition.x + dotRightPosition.x) / 2
if (position.value.x + velocityX > threshold) {
position.value = { ...dotRightPosition }
} else {
position.value = { ...dotLeftPosition }
}
}
</script>
<template>
<div class="wrapper">
<div class="dot left"></div>
<div class="dot right"></div>
<div
class="ball"
:style="style"
@pointerdown="onPointerDown"
@pointermove="onPointerMove"
@pointerup="onPointerUp"
></div>
</div>
</template>
<style scoped>
.dot {
position: absolute;
margin-top: -2.5px;
margin-left: -2.5px;
width: 5px;
height: 5px;
border-radius: 50%;
background-color: #000;
}
.left {
left: calc(1px * v-bind('dotLeftPosition.x'));
top: calc(1px * v-bind('dotLeftPosition.y'));
}
.right {
left: calc(1px * v-bind('dotRightPosition.x'));
top: calc(1px * v-bind('dotRightPosition.y'));
}
.ball {
position: absolute;
left: -50px;
top: -50px;
width: 100px;
height: 100px;
border-radius: 50%;
background-color: #fff;
box-shadow: 0 0 15px 5px rgba(0, 0, 0, 0.3);
will-change: transform;
}
</style>