forked from hammerjs/hammer.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhammer.showtouches.js
114 lines (102 loc) · 3.83 KB
/
hammer.showtouches.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
(function(Hammer) {
'use strict';
/**
* show all touch on the screen by placing elements at there pageX and pageY
*
* @usage
* call `Hammer.plugins.showTouches()` before creating an instance to enable the plugin for all
* instances. You can also do this later, but then you'll have to enable this per instance by setting
* the option `show_touches` to `true`
*/
Hammer.plugins.showTouches = function() {
// only possible with the pointerEvents css property supported
if(!testStyle('pointerEvents')) {
return;
}
// the circles under your fingers
var templateStyle = [
'position: absolute;',
'z-index: 9999;',
'height: 14px;',
'width: 14px;',
'top: 0;',
'left: 0;',
'pointer-events: none;', // makes the element click-thru
'border: solid 2px #777;',
'background: rgba(255,255,255,.7);',
'border-radius: 20px;',
'margin-top: -9px;',
'margin-left: -9px;'
].join('');
// define position property
var positionStyleProp = 'lefttop';
if(testStyle('transform')) { positionStyleProp = 'transform'; }
if(testStyle('MozTransform')) { positionStyleProp = 'MozTransform'; }
if(testStyle('webkitTransform')) { positionStyleProp = 'webkitTransform'; }
// elements by identifier
var touchElements = {};
var touchesIndex = {};
/**
* check if a style property exists
* @param {String} prop
* @param {HTMLElement} [el=document.body]
* @returns {Boolean}
*/
function testStyle(prop, el) {
return (prop in (el || document.body).style);
}
/**
* remove unused touch elements
*/
function removeUnusedElements() {
// remove unused touch elements
for(var key in touchElements) {
if(touchElements.hasOwnProperty(key) && !touchesIndex[key]) {
document.body.removeChild(touchElements[key]);
delete touchElements[key];
}
}
}
/**
* set position of an element with top/left or css transform
* @param {HTMLElement} el
* @param {Number} x
* @param {Number} y
*/
function setPosition(el, x, y) {
if(positionStyleProp == 'lefttop') {
el.style.left = x + 'px';
el.style.top = y + 'px';
} else {
el.style[positionStyleProp] = 'translate(' + x + 'px, ' + y + 'px)';
}
}
Hammer.detection.register({
name: 'showTouches',
priority: 0,
handler: function(ev) {
touchesIndex = {};
// clear old elements when not using a mouse
if(ev.pointerType != Hammer.POINTER_MOUSE) {
removeUnusedElements();
return;
}
// place touches by index
for(var t = 0, len = ev.touches.length; t < len; t++) {
var touch = ev.touches[t];
var id = touch.identifier;
touchesIndex[id] = touch;
// new touch element
if(!touchElements[id]) {
var template = document.createElement('div');
template.setAttribute('style', templateStyle);
document.body.appendChild(template);
touchElements[id] = template;
}
setPosition(touchElements[id], touch.pageX, touch.pageY);
}
removeUnusedElements();
}
});
};
})(window.Hammer);