-
Notifications
You must be signed in to change notification settings - Fork 0
/
Controls.js
95 lines (78 loc) · 2.56 KB
/
Controls.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
var Controls = (function(Controls) {
"use strict";
// Check for double inclusion
if (Controls.addMouseHandler)
return Controls;
Controls.addMouseHandler = function (domObject, drag, move, zoom) {
var startDragX = null,
startDragY = null;
var lastMove = null;
var mouseMode = null;
domObject.addEventListener("touchstart", function (e) {
var touch = e.touches[0];
lastMove = touch;
var mouseEvent = new MouseEvent("mousedown", {
clientX: touch.clientX,
clientY: touch.clientY
});
if (e.target == domObject) {
e.preventDefault();
}
domObject.dispatchEvent(mouseEvent);
}, false);
domObject.addEventListener("touchend", function (e) {
var touch = lastMove;
var mouseEvent = new MouseEvent("mouseup", {
clientX: touch.clientX,
clientY: touch.clientY
});
if (e.target == domObject) {
e.preventDefault();
}
domObject.dispatchEvent(mouseEvent);
}, false);
domObject.addEventListener("touchmove", function (e) {
var touch = e.touches[0];
lastMove = touch;
var mouseEvent = new MouseEvent("mousemove", {
clientX: touch.clientX,
clientY: touch.clientY
});
if (e.target == domObject) {
e.preventDefault();
}
domObject.dispatchEvent(mouseEvent);
}, false);
function mouseDownHandler(e) {
if(mouseMode) return;
startDragX = e.clientX;
startDragY = e.clientY;
mouseMode=e.which;
e.preventDefault();
}
function mouseMoveHandler(e) {
if (startDragX === null || startDragY === null)
return;
if (mouseMode == 1 && drag)
drag(e.clientX - startDragX, e.clientY - startDragY);
if (mouseMode == 2 && move)
move(e.clientX - startDragX, e.clientY - startDragY);
if (mouseMode == 3 && zoom)
zoom(e.clientX - startDragX, e.clientY - startDragY);
startDragX = e.clientX;
startDragY = e.clientY;
e.preventDefault();
}
function mouseUpHandler(e) {
mouseMoveHandler.call(this, e);
startDragX = null;
startDragY = null;
mouseMode=null;
e.preventDefault();
}
domObject.addEventListener("mousedown", mouseDownHandler);
domObject.addEventListener("mousemove", mouseMoveHandler);
domObject.addEventListener("mouseup", mouseUpHandler);
};
return Controls;
}(Controls || {}))