forked from marmelab/gremlins.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
toucher.js
325 lines (283 loc) · 12 KB
/
toucher.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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
/**
* The toucher gremlin touches anywhere on the visible area of the document.
*
* The toucher gremlin triggers touch events (touchstart, touchmove, touchcancel
* and touchend), by doing gestures on random targets displayed on the viewport.
* Touch gestures can last several seconds, so this gremlin isn't instantaneous.
*
* By default, the touch gremlin activity is showed by a red disc.
*
* var toucherGremlin = gremlins.species.toucher();
* horde.gremlin(toucherGremlin);
*
* The toucher gremlin can be customized as follows:
*
* toucherGremlin.touchTypes(['tap', 'gesture']); // the mouse event types to trigger
* toucherGremlin.positionSelector(function() { // find a random pair of coordinates to touch });
* toucherGremlin.showAction(function(x, y) { // show the gremlin activity on screen });
* toucherGremlin.canTouch(function(element) { return true }); // to limit where the gremlin can touch
* toucherGremlin.maxNbTries(5); // How many times the gremlin must look for a touchable element before quitting
* toucherGremlin.logger(loggerObject); // inject a logger
* toucherGremlin.randomizer(randomizerObject); // inject a randomizer
*
* Example usage:
*
* horde.gremlin(gremlins.species.toucher()
* .touchTypes(['gesture'])
* .positionSelector(function() {
* // only touch inside the foo element area
* var $el = $('#foo');
* var offset = $el.offset();
* return [
* parseInt(Math.random() * $el.outerWidth() + offset.left),
* parseInt(Math.random() * $el.outerHeight() + offset.top)
* ];
* })
* . showAction(function(x, y) {
* // do nothing (hide the gremlin action on screen)
* })
* );
*/
define(function(require) {
"use strict";
var configurable = require('../utils/configurable');
var Chance = require('../vendor/chance');
var RandomizerRequiredException = require('../exceptions/randomizerRequired');
return function() {
var document = window.document,
body = document.body;
var defaultTouchTypes = ['tap', 'tap', 'tap', 'doubletap', 'gesture', 'gesture', 'gesture', 'multitouch', 'multitouch'];
function defaultPositionSelector() {
return [
config.randomizer.natural({ max: document.documentElement.clientWidth - 1 }),
config.randomizer.natural({ max: document.documentElement.clientHeight - 1 })
];
}
function defaultShowAction(touches) {
var fragment = document.createDocumentFragment();
touches.forEach(function(touch) {
var touchSignal = document.createElement('div');
touchSignal.style.zIndex = 2000;
touchSignal.style.background = "red";
touchSignal.style['border-radius'] = '50%'; // Chrome
touchSignal.style.borderRadius = '50%'; // Mozilla
touchSignal.style.width = "20px";
touchSignal.style.height = "20px";
touchSignal.style.position = "absolute";
touchSignal.style.webkitTransition = 'opacity .5s ease-out';
touchSignal.style.mozTransition = 'opacity .5s ease-out';
touchSignal.style.transition = 'opacity .5s ease-out';
touchSignal.style.left = (touch.x - 10 ) + 'px';
touchSignal.style.top = (touch.y - 10 ) + 'px';
var element = fragment.appendChild(touchSignal);
setTimeout(function() {
body.removeChild(element);
}, 500);
setTimeout(function() {
element.style.opacity = 0;
}, 50);
});
document.body.appendChild(fragment);
}
function defaultCanTouch() {
return true;
}
/**
* @mixin
*/
var config = {
touchTypes: defaultTouchTypes,
positionSelector: defaultPositionSelector,
showAction: defaultShowAction,
canTouch: defaultCanTouch,
maxNbTries: 10,
logger: null,
randomizer: null,
maxTouches: 2
};
/**
* generate a list of x/y around the center
* @param center
* @param points
* @param [radius=100]
* @param [rotation=0]
*/
function getTouches(center, points, radius, degrees) {
var cx = center[0],
cy = center[1],
touches = [],
slice, i, angle;
// just one touch, at the center
if (points === 1) {
return [{ x: cx, y: cy }];
}
radius = radius || 100;
degrees = degrees !== null ? (degrees * Math.PI / 180) : 0;
slice = 2 * Math.PI / points;
for (i = 0; i < points; i++) {
angle = (slice * i) + degrees;
touches.push({
x: (cx + radius * Math.cos(angle)),
y: (cy + radius * Math.sin(angle))
});
}
return touches;
}
/**
* trigger a touchevent
* @param touches
* @param element
* @param type
*/
function triggerTouch(touches, element, type) {
var touchlist = [],
event = document.createEvent('Event');
event.initEvent('touch' + type, true, true);
touchlist.identifiedTouch = touchlist.item = function(index) {
return this[index] || {};
};
touches.forEach(function(touch, i) {
var x = Math.round(touch.x),
y = Math.round(touch.y);
touchlist.push({
pageX: x,
pageY: y,
clientX: x,
clientY: y,
screenX: x,
screenY: y,
target: element,
identifier: i
});
});
event.touches = (type == 'end') ? [] : touchlist;
event.targetTouches = (type == 'end') ? [] : touchlist;
event.changedTouches = touchlist;
element.dispatchEvent(event);
config.showAction(touches);
}
/**
* trigger a gesture
* @param element
* @param startPos
* @param startTouches
* @param gesture
* @param done
*/
function triggerGesture(element, startPos, startTouches, gesture, done) {
var interval = 10,
loops = Math.ceil(gesture.duration / interval),
loop = 1;
function gestureLoop() {
// calculate the radius
var radius = gesture.radius;
if (gesture.scale !== 1) {
radius = gesture.radius - (gesture.radius * (1 - gesture.scale) * (1 / loops * loop));
}
// calculate new position/rotation
var posX = startPos[0] + (gesture.distanceX / loops * loop),
posY = startPos[1] + (gesture.distanceY / loops * loop),
rotation = typeof gesture.rotation == 'number' ? (gesture.rotation / loops * loop) : null,
touches = getTouches([posX, posY], startTouches.length, radius, rotation),
isFirst = (loop == 1),
isLast = (loop == loops);
if (isFirst) {
triggerTouch(touches, element, 'start');
} else if (isLast) {
triggerTouch(touches, element, 'end');
return done(touches);
} else {
triggerTouch(touches, element, 'move');
}
setTimeout(gestureLoop, interval);
loop++;
}
gestureLoop();
}
var touchTypes = {
// tap, like a click event, only 1 touch
// could also be a slow tap, that could turn out to be a hold
tap: function tap(position, element, done) {
var touches = getTouches(position, 1);
var gesture = {
duration: config.randomizer.integer({ min: 20, max: 700 })
};
triggerTouch(touches, element, 'start');
setTimeout(function() {
triggerTouch(touches, element, 'end');
done(touches, gesture);
}, gesture.duration);
},
// doubletap, like a dblclick event, only 1 touch
// could also be a slow doubletap, that could turn out to be a hold
doubletap: function doubletap(position, element, done) {
touchTypes.tap(position, element, function() {
setTimeout(function() {
touchTypes.tap(position, element, done);
}, 30);
});
},
// single touch gesture, could be a drag and swipe, with 1 points
gesture: function gesture(position, element, done) {
var gesture = {
distanceX: config.randomizer.integer({ min: -100, max: 200 }),
distanceY: config.randomizer.integer({ min: -100, max: 200 }),
duration: config.randomizer.integer({ min: 20, max: 500 })
};
var touches = getTouches(position, 1, gesture.radius);
triggerGesture(element, position, touches, gesture, function(touches) {
done(touches, gesture);
});
},
// multitouch gesture, could be a drag, swipe, pinch and rotate, with 2 or more points
multitouch: function multitouch(position, element, done) {
var points = config.randomizer.integer({ min: 2, max: config.maxTouches});
var gesture = {
scale: config.randomizer.floating({ min: 0, max: 2 }),
rotation: config.randomizer.natural({ min: -100, max: 100 }),
radius: config.randomizer.integer({ min: 50, max: 200 }),
distanceX: config.randomizer.integer({ min: -20, max: 20 }),
distanceY: config.randomizer.integer({ min: -20, max: 20 }),
duration: config.randomizer.integer({ min: 100, max: 1500 })
};
var touches = getTouches(position, points, gesture.radius);
triggerGesture(element, position, touches, gesture, function(touches) {
done(touches, gesture);
});
}
};
/**
* @mixes config
*/
function toucherGremlin(done) {
if (!config.randomizer) {
throw new RandomizerRequiredException();
}
var position,
posX, posY,
targetElement,
nbTries = 0;
do {
position = config.positionSelector();
posX = position[0];
posY = position[1];
targetElement = document.elementFromPoint(posX, posY);
nbTries++;
if(nbTries > config.maxNbTries) return;
} while (!targetElement || !config.canTouch(targetElement));
var touchType = config.randomizer.pick(config.touchTypes);
touchTypes[touchType](position, targetElement, logGremlin);
function logGremlin(touches, details) {
if (typeof config.showAction == 'function') {
config.showAction(touches);
}
if (config.logger && typeof config.logger.log == 'function') {
config.logger.log('gremlin', 'toucher ', touchType, 'at', posX, posY, details);
}
done();
}
}
configurable(toucherGremlin, config);
return toucherGremlin;
};
});