forked from diegoMontesinos/tuio-nw
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tuio-to-touch.js
244 lines (200 loc) · 6.29 KB
/
tuio-to-touch.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
'use strict';
// TuioCursor Events -> Touch Events
(function (window, document, exportName) {
var cursors = {};
var cursorsElements = {};
var drawCursors_ = false;
var callbacks;
var TuioTouch = {
cursorCSS: function (touch) {
var size = 30;
var pX = touch.clientX - (size / 2);
var pY = touch.clientY - (size / 2);
var transform = 'translate(' + pX + 'px, ' + pY + 'px)';
return {
position: 'fixed',
left: 0,
top: 0,
background: '#fff',
border: 'solid 1px #999',
opacity: 0.6,
borderRadius: '100%',
height: size + 'px',
width: size + 'px',
padding: 0,
margin: 0,
display: 'block',
overflow: 'hidden',
pointerEvents: 'none',
webkitUserSelect: 'none',
mozUserSelect: 'none',
userSelect: 'none',
webkitTransform: transform,
mozTransform: transform,
transform: transform,
zIndex: 100
};
},
init: function (tuioClient) {
if (!tuioClient) {
throw new Error('tuioClient is required');
}
callbacks = tuioClient._callbacks;
tuioClient.on('listening', function () {
if (callbacks && callbacks.listening) {
callbacks.listening.next.callback();
}
tuioClient.on('addTuioCursor', onTuioCursor('touchstart'));
tuioClient.on('updateTuioCursor', onTuioCursor('touchmove'));
tuioClient.on('removeTuioCursor', onTuioCursor('touchend'));
window.addEventListener('touchstart', showCursors, true);
window.addEventListener('touchmove', showCursors, true);
window.addEventListener('touchend', showCursors, true);
});
},
drawCursors: function (drawCursors) {
drawCursors_ = drawCursors;
}
};
function showCursors(ev) {
if (drawCursors_) {
var touch, i, el, styles;
for (i = 0; i < ev.touches.length; i++) {
touch = ev.touches[i];
el = cursorsElements[touch.identifier];
if (!el) {
el = cursorsElements[touch.identifier] = document.createElement('div');
document.body.appendChild(el);
}
styles = TuioTouch.cursorCSS(touch);
for (var prop in styles) {
el.style[prop] = styles[prop];
}
}
// remove all ended touches
if (ev.type === 'touchend' || ev.type === 'touchcancel') {
for (i = 0; i < ev.changedTouches.length; i++) {
touch = ev.changedTouches[i];
el = cursorsElements[touch.identifier];
if (el) {
el.parentNode.removeChild(el);
delete cursorsElements[touch.identifier];
}
}
}
}
}
function onTuioCursor(touchEventType) {
return function (cursor) {
if (callbacks) {
var callback;
switch (touchEventType) {
case 'touchstart':
callback = callbacks.addTuioCursor;
break;
case 'touchmove':
callback = callbacks.updateTuioCursor;
break;
case 'touchend':
callback = callbacks.removeTuioCursor;
break;
}
if (callback) {
callback.next.callback(cursor);
}
}
tuioCallback(touchEventType, cursor.sessionId, cursor.xPos, cursor.yPos);
};
}
/* TUIO CURSOR -> TOUCHEVENT API */
// See http://dvcs.w3.org/hg/webevents/raw-file/tip/touchevents.html
function Touch(sid, x, y) {
this.update = function (x_, y_) {
var client = { x: window.innerWidth * x_, y: window.innerHeight * y_ };
var target = document.elementFromPoint(client.x, client.y);
var scroll = getScrollPosition();
var viewportSize = getViewportSize();
this.target = target;
this.clientX = client.x;
this.clientY = client.y;
this.pageX = client.x + scroll.x;
this.pageY = client.y + scroll.y;
this.screenX = client.x + (window.outerWidth - viewportSize.w);
this.screenY = client.y + (window.outerHeight - viewportSize.h);
};
// Id
this.identifier = sid;
this.update(x, y);
}
function TouchList() {
var touchList = [];
touchList.item = function (index) {
return this[index] || null;
};
// specified by Mozilla
touchList.identifiedTouch = function (id) {
return this[id + 1] || null;
};
return touchList;
}
function tuioCallback(type, sid, x, y) {
// Creamos / obtenemos el touch
var touch;
if (type === 'touchstart') {
touch = new Touch(sid, x, y);
cursors[sid] = touch;
} else {
touch = cursors[sid];
touch.update(x, y);
}
// Trigereamos el evento
triggerTouchevent(type, touch);
// Lo borramos
if (type === 'touchend') {
delete cursors[sid];
}
}
function getScrollPosition() {
var doc = document.documentElement;
var left = (window.pageXOffset || doc.scrollLeft) - (doc.clientLeft || 0);
var top = (window.pageYOffset || doc.scrollTop) - (doc.clientTop || 0);
return { x: left, y: top };
}
function getViewportSize() {
var w_ = Math.max(document.documentElement.clientWidth, window.innerWidth || 0);
var h_ = Math.max(document.documentElement.clientHeight, window.innerHeight || 0);
return { w: w_, h: h_ };
}
function triggerTouchevent(eventName, touch) {
var touchEvent = document.createEvent('Event');
touchEvent.initEvent(eventName, true, true);
// Creamos las listas de toques
var activeTouches = new TouchList();
var targetTouches = new TouchList();
var changedTouches = new TouchList();
for (var sid in cursors) {
// Cursores activos
activeTouches.push(cursors[sid]);
// Cursores sobre el target
if (touch.target === cursors[sid].target) {
targetTouches.push(cursors[sid]);
}
}
changedTouches.push(touch);
touchEvent.touches = activeTouches;
touchEvent.targetTouches = targetTouches;
touchEvent.changedTouches = changedTouches;
// Disparamos el evento
if (touch.target) {
touch.target.dispatchEvent(touchEvent);
} else {
document.dispatchEvent(touchEvent);
}
}
// export
if (typeof module !== 'undefined' && module.exports) {
module.exports = TuioTouch;
} else {
window[exportName] = TuioTouch;
}
})(window, document, 'TuioTouch');