-
Notifications
You must be signed in to change notification settings - Fork 0
/
touchEventsManager.js
51 lines (42 loc) · 1.84 KB
/
touchEventsManager.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
var TouchEventsEnum = {
touchstart: PLATFORM_PC ? 'mousedown' : 'touchstart',
touchend: PLATFORM_PC ? 'mouseup' : 'touchend',
touchmove: PLATFORM_PC ? 'mousemove' : 'touchmove',
touchout: PLATFORM_PC ? 'mouseout' : '',
}
var touchEventsManager = {
events: [],
update: function() {
touchEventsManager.events = [];
},
addEvent: function(type, e, clientToCanvasPosition) {
var touchPos = PLATFORM_PC ? e : e.touches.item(0) != null ? e.touches.item(0) : {
x: Number.NaN,
y: Number.NaN
};
touchPos = clientToCanvasPosition(touchPos.clientX, touchPos.clientY);
if (!touchEventsManager.events[type]) {
touchEventsManager.events[type] = [];
}
touchEventsManager.events[type].push({
x: touchPos.x,
y: touchPos.y
});
},
init: function(graphicsDevice, canvas) {
touchEventsManager.events = [];
canvas.addEventListener(TouchEventsEnum.touchend, function(e) {
touchEventsManager.addEvent(TouchEventsEnum.touchend, e, graphicsDevice.convertClientToCanvasPosition);
}, false);
canvas.addEventListener(TouchEventsEnum.touchstart, function(e) {
touchEventsManager.addEvent(TouchEventsEnum.touchstart, e, graphicsDevice.convertClientToCanvasPosition);
e.preventDefault();
}, false);
canvas.addEventListener(TouchEventsEnum.touchmove, function(e) {
touchEventsManager.addEvent(TouchEventsEnum.touchmove, e, graphicsDevice.convertClientToCanvasPosition)
}, false);
canvas.addEventListener(TouchEventsEnum.mouseout, function(e) {
touchEventsManager.addEvent(TouchEventsEnum.mouseout, e, graphicsDevice.convertRealToVirtualPosition)
}, false);
}
};