-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathApp.vue
More file actions
103 lines (86 loc) · 1.94 KB
/
Copy pathApp.vue
File metadata and controls
103 lines (86 loc) · 1.94 KB
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
<script setup lang="ts">
import { ref } from 'vue'
import { spring, springValue, sv } from '../../src/vue'
const dotLeftPosition = {
x: 100,
y: 200,
}
const dotRightPosition = {
x: 500,
y: 200,
}
const x = springValue(dotLeftPosition.x)
const y = springValue(dotLeftPosition.y)
const isDragging = ref(false)
const onPointerDown = (e: PointerEvent) => {
isDragging.value = true
;(e.currentTarget as HTMLElement).setPointerCapture(e.pointerId)
}
const onPointerMove = (e: PointerEvent) => {
if (!isDragging.value) {
return
}
x.target = e.clientX
y.target = e.clientY
}
const onPointerUp = () => {
if (!isDragging.value) {
return
}
isDragging.value = false
const velocityX = x.velocity()
const threshold = (dotLeftPosition.x + dotRightPosition.x) / 2
if (x.target + velocityX > threshold) {
x.target = dotRightPosition.x
y.target = dotRightPosition.y
} else {
x.target = dotLeftPosition.x
y.target = dotLeftPosition.y
}
}
</script>
<template>
<div class="wrapper">
<div class="dot left"></div>
<div class="dot right"></div>
<spring.div
class="ball"
:spring-style="{ translate: sv`${x}px ${y}px` }"
:duration="800"
:disabled="isDragging"
@pointerdown="onPointerDown"
@pointermove="onPointerMove"
@pointerup="onPointerUp"
/>
</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>