/
Recognizer.js
305 lines (266 loc) · 8.75 KB
/
Recognizer.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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
/*
Recognizer
Handles integration to Tapspace and higher level events such as tap event.
Keep all tapspace-related stuff in Recognizer.
Keep Sensor compatible with all html elements.
Notes
[1]
For press event detection, we can place a threshold how far
we allow fingers to move to still classify it as a press.
*/
var Sensor = require('./Sensor')
var utils = require('./utils')
var ITransform = require('../geom/ITransform')
var IVector = require('../geom/IVector')
var Vector = require('../geom/Vector')
var nudged = require('nudged')
var toRawPivot = function (mode, plane) {
// The pivot needs to be converted to [x, y]
// on plane's coordinate plane.
//
if (mode.pivot) {
return mode.pivot.to(plane).toArray()
}
// Use middle of plane as a default pivot if translation not allowed.
if (mode.translate === false) {
return plane.atMid().to(plane).toArray()
}
// Return undefined so that when rawPivot is given as a parameter
// to Sensor, it looks like the parameter was not given.
}
var rebase = function (pointers, sourcePlane, targetPlane) {
// Represent pointers on another AbstractPlane
//
// Parameters
// pointers
// a map: id -> [x, y]
// sourcePlane
// a AbstractPlane of the pointers
// targetPlane
// a AbstractPlane of the returned pointers
//
// Return
// object, a map: id -> [x, y]
//
if (sourcePlane === targetPlane) {
return pointers
}
var k
var result = {}
var sourceToSpace = sourcePlane.getGlobalTransform()
var targetToSpace = targetPlane.getGlobalTransform()
var sourceToTarget = targetToSpace.inverse().multiplyRight(sourceToSpace)
for (k in pointers) {
if (pointers.hasOwnProperty(k)) {
result[k] = sourceToTarget.transform(pointers[k])
}
}
return result
}
var rebaseToSpace = function (pointers, sourcePlane) {
var k
var result = {}
var sourceToSpace = sourcePlane.getGlobalTransform()
for (k in pointers) {
if (pointers.hasOwnProperty(k)) {
result[k] = sourceToSpace.transform(pointers[k])
}
}
return result
}
var toTapPoints = function (ps) {
// Params:
// ps: map id->[x,y] on space
var k
var result = []
for (k in ps) {
if (ps.hasOwnProperty(k)) {
result.push(new IVector(new Vector(ps[k][0], ps[k][1])))
}
}
return result
}
var multiplyLeft = function (pointers, transform) {
// Transform pointers. Regard pointers as a row vector
// of column vectors. Multiply each column vector from left by
// a transformation matrix.
var k
var result = {}
for (k in pointers) {
if (pointers.hasOwnProperty(k)) {
result[k] = transform.transform(pointers[k])
}
}
return result
}
var Recognizer = function (manager) {
// Create a Recognizer
// var rec = new Recognizer(...)
//
// Parameters:
// manager
// Touchable with the following properties
// element
// HTML element to listen to
// item
// a tapspace.AbstractPlane, the object on the gesture happens
// targetItem
// a tapspace.AbstractPlane or function (ITransform itr).
// The object to move or a function to call.
// view
// a tapspace.SpaceView
// mode
// a transformation type
//
var self = this
this.man = manager
var startTime = null
var totalTravel = null
var pointersOnItem = {}
var touchPointLog = {} // starting points
var onStart = function (firstPointers) {
// On gesture start. Handle the first pointer(s) of the gesture.
//
var sourceItem = self.man.item
var view = self.man.view
var el = self.man.element
startTime = Date.now()
totalTravel = 0
pointersOnItem = rebase(firstPointers, view, sourceItem)
touchPointLog = rebaseToSpace(firstPointers, view)
self.man.emit('gesturestart', {
distance: 0,
duration: 0,
element: el,
item: self.man.targetItem
})
}
var onMove = function (prevPointers, nextPointers) {
var k, n, pivot, type, tr, itr
var domain = []
var range = []
var el = self.man.element
var view = self.man.view
var sourceItem = self.man.item
var targetItem = self.man.targetItem
var mode = self.man.mode
// Current location of new pointers on the plane.
// This approach zeroes the effects caused by
// 1) transformations of the view
// 2) transformations of the parents of the source item
var nextPointersOnItem = rebase(nextPointers, view, sourceItem)
// Set intersection. Do not use removed or appeared pointers
// in transformation estimation.
for (k in pointersOnItem) {
if (pointersOnItem.hasOwnProperty(k) &&
nextPointersOnItem.hasOwnProperty(k)) {
domain.push(pointersOnItem[k])
range.push(nextPointersOnItem[k])
}
}
// Record the space locations of the new pointers to
// provide them in the tap event.
var viewToSpace = view.getGlobalTransform()
for (k in nextPointersOnItem) {
if (nextPointersOnItem.hasOwnProperty(k) &&
!touchPointLog.hasOwnProperty(k)) {
// New pointer
touchPointLog[k] = viewToSpace.transform(nextPointersOnItem[k])
}
}
// Accumulate to travelled distance. See [1]
// Use Manhattan distance for simpler computation.
// Goal is to form a threshold to filter out small
// involuntary movement of the fingers or arm.
//
// N. Divide distance by number of fingers. This way
// an involuntary arm movement has same threshold regardless of
// the number of touching fingers.
//
// Note that we compute travel on view instead of travel on
// the plane. This way travel does not depend on transformations
// of the space, but only the screen pixels.
n = domain.length
for (k in prevPointers) {
if (prevPointers.hasOwnProperty(k) && nextPointers.hasOwnProperty(k)) {
totalTravel += Math.abs(prevPointers[k][0] - nextPointers[k][0]) / n
totalTravel += Math.abs(prevPointers[k][1] - nextPointers[k][1]) / n
}
}
// Compute current position of the pivot on the plane.
// Pivot will be undefined if mode has no pivot but enables translation.
pivot = toRawPivot(mode, sourceItem)
// Get current nudged-compatible transformation type string
type = utils.convertToTransformationType(mode)
// Estimate optimal transformation
tr = nudged.estimate(type, domain, range, pivot)
// Apply the transformation to targetItem. We also
// memorize the new pointers for the next onMove call.
// We want to memorize only their relative location on the source item.
// This way transformations of the view and parents between
// onMove calls become part of the resulting transformation.
// Note that transformBy emits 'transformed' that might eventually
// cause transformations in the space. Therefore we should do all
// coordinate-plane conversions before transformBy to avoid weird bugs.
itr = new ITransform(tr, sourceItem)
if (typeof targetItem === 'function') {
pointersOnItem = nextPointersOnItem
targetItem(itr)
} else if (view === targetItem) {
// Somehow we do not need the following line with views:
// pointersOnItem = multiplyLeft(nextPointersOnItem, tr)
// If we use the line, everything shakes.
pointersOnItem = nextPointersOnItem
view.transformBy(itr.inverse())
} else {
// The sourceItem moves, but the stored pointer locations
// should not. Thus undo the effect the transformation
// would have to the stored pointer locations.
pointersOnItem = multiplyLeft(nextPointersOnItem, tr.inverse())
targetItem.transformBy(itr)
}
self.man.emit('gesturemove', {
distance: totalTravel,
duration: Date.now() - startTime,
element: el,
item: targetItem
})
}
var onEnd = function (lastPointers) {
// Handle the last pointer(s) of the gesture
//
var el = self.man.element
var targetItem = self.man.targetItem
var mode = self.man.mode
self.man.emit('gestureend', {
distance: totalTravel,
duration: Date.now() - startTime,
element: el,
item: targetItem
})
if (mode.tap && totalTravel < mode.tapMaxTravel) {
self.man.emit('tap', {
distance: totalTravel,
duration: Date.now() - startTime,
element: el,
item: targetItem,
points: toTapPoints(touchPointLog)
})
}
pointersOnItem = {}
touchPointLog = {}
}
// Handlers defined, construct sensor
this.sensor = new Sensor(this.man.element, {
start: onStart,
move: onMove,
end: onEnd
}, this.man.mode)
}
Recognizer.prototype.update = function (mode) {
this.sensor.update(mode)
}
Recognizer.prototype.destroy = function () {
this.sensor.destroy()
}
module.exports = Recognizer