-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
246 lines (214 loc) · 6.77 KB
/
index.html
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
<!doctype html><html><head>
<meta name="viewport"
content="width=device-width, initial-scale=1.0, user-scalable=no">
<style>
html,
body{
height:100%;
}
body{
margin:0;
font: 1em verdana;
}
a{
color: #00f;
}
</style>
<meta charset="utf-8">
</head><body>
<h1>Demo Multitouch</h1>
<p>di <a href="http://www.jure.it" target="_blank" ontouchend="window.open(this.href,this.target); return false;">Giorgio Beggiora</a></p>
<p>Articolo: <a href="http://www.yourinspirationweb.com/2016/05/17/far-fronte-agli-eventi-in-javascript-parte-3/" target="_blank" ontouchend="window.open(this.href,this.target); return false;">http://www.yourinspirationweb.com/2016/05/17/far-fronte-agli-eventi-in-javascript-parte-3/</a></p>
<p>GitHub: <a href="https://github.com/giorgiobeggiora/yiw-multitouch-demo/" target="_blank" ontouchend="window.open(this.href,this.target); return false;">https://github.com/giorgiobeggiora/yiw-multitouch-demo/</a>
<p>Questo script fa comparire dei cerchi gialli in corrispondenza dei punti toccati.</p>
<p>Chrome per Android come touch event identifier usa un numero "auto increment" che riparte da 0 ogni volta che un tocco termina. Nel caso in cui esistano già dei tocchi con un certo id, verrà assegnato l'id successivo. Safari su iOS invece continua a contare senza mai ripartire da 0.</p>
<p>Toccare lo schermo nello stesso istante con troppe dita (4 su iOS) potrebbe attivare le gesture di sistema, come quelle per passare da un'app all'altra: in tal caso, provate a toccare lo schermo un dito alla volta, molto lentamente.</p>
<div style="position:fixed;top:0;right:0;">
<p><strong>Touch id</strong></p>
<p id="coo"><em>nessuno</em></p>
</div>
<!-- CODICE PER LA GESTIONE DEI CERCHI -->
<style>
.circle{
position: fixed;
width: 120px;
height: 120px;
margin-left: -60px;
margin-top: -60px;
border-radius: 120px;
border: 4px solid #0000ff;
background-color: #ffff00;
pointer-events: none;
}
</style>
<script>
var circle = function(){
var o = arguments[0] || {};
if(typeof o.id !== 'undefined')this.id = o.id;
if(typeof o.x !== 'undefined')this.x = o.x;
if(typeof o.y !== 'undefined')this.y = o.y;
var _this = this;
this.render = function(){
_this.element = document.createElement('div');
_this.element.className = 'circle';
_this.origin(_this.x, _this.y);
document.body.appendChild(_this.element);
};
this.destroy = function(){
_this.element.parentNode.removeChild(_this.element);
};
this.origin = function(x, y){
_this.x = x;
_this.y = y;
_this.element.style.left = x + 'px';
_this.element.style.top = y + 'px';
};
this.move = function(x, y){
var x2 = x - _this.x;
var y2 = y - _this.y;
_this.element.style.transform = 'translate3D(' + x2 + 'px, ' + y2 + 'px, 0)';
};
this.render();
};
</script>
<!-- CODICE PER LA GESTIONE DI UNA LISTA DI OGGETTI -->
<!-- Consente di gestire una lista sia come array che come oggetto -->
<script>
var ObjectArray = function(){
var o = arguments[0] || {} ;
this.object = {};
this.array = [];
this.length = 0;
this.id = o.id || 'id'; // ogni elemento della lista deve avere un campo con questo nome
var LIST = this;
this.exists = function(key){
return typeof LIST.object[key] !== 'undefined';
};
this.indexOf = function(key){
var keyName = LIST.id,
arr = LIST.array,
i = arr.length
;
while(i--){
var item = arr[i];
if('' + item[keyName] === '' + key){
return i;
}
}
return -1;
};
this.delete = function(key){
if(LIST.exists(key)){
delete LIST.object[key];
LIST.array.splice(LIST.indexOf(key),1);
LIST.length--;
};
};
this.set = function(key, value){
if(LIST.exists(key)){
LIST.array[LIST.indexOf(key)] = value;
}else{
LIST.array.push(value);
LIST.length++;
};
LIST.object[key] = value;
};
this.get = function(key){
return LIST.object[key];
};
this.item = function(i){
return LIST.array[i];
};
this.clear = function(){
LIST.object = {};
LIST.array = [];
LIST.length = 0;
};
};
var touchPoints = new ObjectArray({id:'identifier'});
var circleInstances = new ObjectArray();
</script>
<!-- CODICE PER RACCOGLIERE TUTTI I PUNTI TOCCATI -->
<script>
function touchesListToArray(touchList){
return Array.prototype.slice.call(touchList);
}
function touchesListToObject(touchList){
var touchArray = touchesListToArray(touchList);
var touchObject = {};
touchArray.forEach(function(touch,i){
touchObject[touch.identifier] = touch;
});
return touchObject;
}
var touchCatcher = function(event){
event.preventDefault();
touchPoints.clear();
var touches = touchesListToArray(event.touches);
touches.forEach(function(touch){
/*for(t in touch)alert([t,touch[t]]);*/
var id = touch.identifier;
touchPoints.set(id, touch);
});
};
var bodyElement = document.body;
bodyElement.addEventListener('touchend', touchCatcher);
bodyElement.addEventListener('touchmove', touchCatcher);
bodyElement.addEventListener('touchstart', touchCatcher);
bodyElement.addEventListener('touchcancel', touchCatcher);
</script>
<!-- CODICE PER MOSTRARE I CERCHI -->
<!-- Per ottimizzare le performance creiamo un loop infinito che viene eseguito 60 volte al secondo -->
<!-- Aggiorneremo quindi i cerchi ogni 17ms e non ad ogni evento, risparmiando di molto sulle performance -->
<script>
(function loop(){
requestAnimationFrame(loop);
if(touchPoints.length || touchPoints.length != circleInstances.length){
var touchPoints_object = touchPoints.object;
var circleInstances_object = circleInstances.object;
var i = circleInstances.length;
while(i--){
var instance = circleInstances.item(i),
id = instance.id
;
/*alert(instance)
alert([typeof id, id, touchPoints.exists(id)])*/
if(!touchPoints.exists(id)){
// remove
circleInstances.get(id).destroy();
circleInstances.delete(id);
}else if(circleInstances.exists(id)){
// update
var touch = touchPoints.get(id);
circleInstances.get(id).move(touch.clientX, touch.clientY);
}
}
var i = touchPoints.length;
//alert(['tl...',i]);
while(i--){
var touch = touchPoints.item(i),
id = touch.identifier
;
//alert(['tp...',touchPoints.array])
//alert(['iit...',i,id,touch])
if(!circleInstances.exists(id)){
// add
var c = new circle({
id: id,
x: touch.clientX,
y: touch.clientY
});
circleInstances.set(id, c);
}
}
var log = [];
circleInstances.array.forEach(function(instance){
//for(t in instance)alert([t, instance[t]]);
log.push(instance.id);
});
var log_content = log.length ? log.join('<br>') : '<em>nessuno</em>';
document.getElementById('coo').innerHTML=log_content;
};
})();
</script>
</body></html>