-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgesture.js
231 lines (194 loc) · 5.73 KB
/
gesture.js
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
// 手势
let element = document.documentElement
export class Dispatcher {
constructor(element) {
this.element = element
}
dispatch (type, properties = {}) {
let event = new Event(type)
for (let name in properties) {
event[name] = properties[name]
}
element.dispatchEvent(event)
}
}
// 解耦 listen => recognize => dispatch
export class Listener {
constructor (element, recognizer) {
let isListeningMouse = false
let contexts = new Map()
element.addEventListener('mousedown', event => {
let context = Object.create(null)
contexts.set(`mouse${1 << event.button}`, context)
recognizer.start(event, context)
let mousemove = event => {
// 0b11111 鼠标上五个键都按下
// 0b00001 左键
// 0b00010 中键
let button = 1
while (button <= event.buttons) {
if (button & event.buttons) {
// order of buttons & button property is not same
let key
if (button === 2) {
key = 4
} else if (button === 4) {
key === 2
} else {
key = button
}
let ctx = contexts.get(`mouse${key}`)
recognizer.move(event, ctx)
}
button = button << 1
}
}
let mouseup = event => {
let ctx = contexts.get(`mouse${1 << event.button}`)
recognizer.end(event, ctx)
contexts.delete(`mouse${1 << event.button}`)
if (event.buttons === 0) {
document.removeEventListener('mousemove', mousemove)
document.removeEventListener('mouseup', mouseup)
isListeningMouse = false
}
}
if (!isListeningMouse) {
document.addEventListener('mousemove', mousemove)
document.addEventListener('mouseup', mouseup)
isListeningMouse = true
}
})
const touchstart = element.addEventListener('touchstart', event => {
for (let touch of event.changedTouches) {
let context = Object.create(null)
contexts.set(touch.identifier, context)
recognizer.start(touch, context)
}
})
const touchmove = element.addEventListener('touchmove', event => {
for (let touch of event.changedTouches) {
let context = contexts.get(touch.identifier)
recognizer.move(touch, context)
}
})
const touchend = element.addEventListener('touchend', event => {
for (let touch of event.changedTouches) {
let context = contexts.get(touch.identifier)
recognizer.end(touch, context)
contexts.delete(touch.identifier)
}
})
const touchcancel = element.addEventListener('touchcancel', event => {
for (let touch of event.changedTouches) {
let context = contexts.get(touch.identifier)
recognizer.cancel(touch, context)
contexts.delete(touch.identifier)
}
})
}
}
export class Recognizer {
constructor (dispatcher) {
this.dispatch = dispatcher.dispatch
}
start (point, context) {
context.startX = point.clientX, context.startY = point.clientY;
context.points = [{
t: Date.now(),
x: point.clientX,
y: point.clientY
}]
context.isTap = true
context.isPan = false
context.isPress = false
context.handler = setTimeout(() => {
context.isTap = false
context.isPan = false
context.isPress = true
this.dispatch('pressstart')
context.handler = null
}, 500)
}
move (point, context) {
let dx = point.clientX - context.startX
let dy = point.clientY - context.startY
if (!context.isPan && dx ** 2 + dy ** 2 > 100) {
context.isTap = false
context.isPan = true
context.isPress = false
context.isVertical = Math.abs(dx) < Math.abs(dy)
this.dispatch('panstart', {
startX: context.startX,
startY: context.startY,
clientX: point.clientX,
clientY: point.clientY,
isVertical: context.isVertical
})
clearTimeout(context.handler)
}
if (context.isPan) {
this.dispatch('pan', {
startX: context.startX,
startY: context.startY,
clientX: point.clientX,
clientY: point.clientY,
isVertical: context.isVertical
})
}
context.points = context.points.filter(point => Date.now() - point.t < 500)
context.points.push({
t: Date.now(),
x: point.clientX,
y: point.clientY
})
}
end (point, context) {
if (context.isTap) {
this.dispatch('tap')
clearTimeout(context.handler)
}
if (context.isPress) {
this.dispatch('pressend')
}
context.points = context.points.filter(point => Date.now() - point.t < 500)
let dis, v
if (!context.points.length) {
v = 0
} else {
dis = Math.sqrt((point.clientX - context.points[0].x) ** 2 + (point.clientY - context.points[0].y) ** 2)
v = dis / (Date.now() - context.points[0].t)
}
if (v > 1.5) {
this.dispatch('flick', {
startX: context.startX,
startY: context.startY,
clientX: point.clientX,
clientY: point.clientY,
isVertical: context.isVertical,
isFlick: context.isFlick,
velocity: v
})
context.isFlick = true
} else {
context.isFlick = false
}
if (context.isPan) {
this.dispatch('panend', {
startX: context.startX,
startY: context.startY,
clientX: point.clientX,
clientY: point.clientY,
isVertical: context.isVertical,
isFlick: context.isFlick
})
}
}
cancel (point, context) {
clearTimeout(context.handler)
this.dispatch('cancel')
}
}
export function enableGesture (el) {
new Listener(el, new Recognizer(new Dispatcher(el)))
}