-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathcontroller.js
175 lines (159 loc) · 6.6 KB
/
controller.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
/* The controller can register callbacks for various events on a canvas:
*
* mousemove: function(prevMouse, curMouse, evt)
* receives both regular mouse events, and single-finger drags (sent as a left-click),
*
* press: function(curMouse, evt)
* receives mouse click and touch start events
*
* wheel: function(amount)
* mouse wheel scrolling
*
* pinch: function(amount)
* two finger pinch, receives the distance change between the fingers
*
* twoFingerDrag: function(dragVector)
* two finger drag, receives the drag movement amount
*/
var Controller = function() {
this.mousemove = null;
this.press = null;
this.wheel = null;
this.twoFingerDrag = null;
this.pinch = null;
}
Controller.prototype.registerForCanvas = function(canvas) {
var prevMouse = null;
var mouseState = [false, false];
var self = this;
canvas.addEventListener("mousemove", function(evt) {
evt.preventDefault();
var rect = canvas.getBoundingClientRect();
var curMouse = [evt.clientX - rect.left, evt.clientY - rect.top];
if (!prevMouse) {
prevMouse = [evt.clientX - rect.left, evt.clientY - rect.top];
} else if (self.mousemove) {
self.mousemove(prevMouse, curMouse, evt);
}
prevMouse = curMouse;
});
canvas.addEventListener("mousedown", function(evt) {
evt.preventDefault();
var rect = canvas.getBoundingClientRect();
var curMouse = [evt.clientX - rect.left, evt.clientY - rect.top];
if (self.press) {
self.press(curMouse, evt);
}
});
canvas.addEventListener("wheel", function(evt) {
evt.preventDefault();
if (self.wheel) {
self.wheel(-evt.deltaY);
}
});
canvas.oncontextmenu = function (evt) {
evt.preventDefault();
};
var touches = {};
canvas.addEventListener("touchstart", function(evt) {
var rect = canvas.getBoundingClientRect();
evt.preventDefault();
for (var i = 0; i < evt.changedTouches.length; ++i) {
var t = evt.changedTouches[i];
touches[t.identifier] = [t.clientX - rect.left, t.clientY - rect.top];
if (evt.changedTouches.length == 1 && self.press) {
self.press(touches[t.identifier], evt);
}
}
});
canvas.addEventListener("touchmove", function(evt) {
evt.preventDefault();
var rect = canvas.getBoundingClientRect();
var numTouches = Object.keys(touches).length;
// Single finger to rotate the camera
if (numTouches == 1) {
if (self.mousemove) {
var t = evt.changedTouches[0];
var prevTouch = touches[t.identifier];
var curTouch = [t.clientX - rect.left, t.clientY - rect.top];
evt.buttons = 1;
self.mousemove(prevTouch, curTouch, evt);
}
} else {
var curTouches = {};
for (var i = 0; i < evt.changedTouches.length; ++i) {
var t = evt.changedTouches[i];
curTouches[t.identifier] = [t.clientX - rect.left, t.clientY - rect.top];
}
// If some touches didn't change make sure we have them in
// our curTouches list to compute the pinch distance
// Also get the old touch points to compute the distance here
var oldTouches = [];
for (t in touches) {
if (!(t in curTouches)) {
curTouches[t] = touches[t];
}
oldTouches.push(touches[t]);
}
var newTouches = [];
for (t in curTouches) {
newTouches.push(curTouches[t]);
}
// Determine if the user is pinching or panning
var motionVectors = [
vec2.set(vec2.create(), newTouches[0][0] - oldTouches[0][0],
newTouches[0][1] - oldTouches[0][1]),
vec2.set(vec2.create(), newTouches[1][0] - oldTouches[1][0],
newTouches[1][1] - oldTouches[1][1])
];
var motionDirs = [vec2.create(), vec2.create()];
vec2.normalize(motionDirs[0], motionVectors[0]);
vec2.normalize(motionDirs[1], motionVectors[1]);
var pinchAxis = vec2.set(vec2.create(), oldTouches[1][0] - oldTouches[0][0],
oldTouches[1][1] - oldTouches[0][1]);
vec2.normalize(pinchAxis, pinchAxis);
var panAxis = vec2.lerp(vec2.create(), motionVectors[0], motionVectors[1], 0.5);
vec2.normalize(panAxis, panAxis);
var pinchMotion = [
vec2.dot(pinchAxis, motionDirs[0]),
vec2.dot(pinchAxis, motionDirs[1])
];
var panMotion = [
vec2.dot(panAxis, motionDirs[0]),
vec2.dot(panAxis, motionDirs[1])
];
// If we're primarily moving along the pinching axis and in the opposite direction with
// the fingers, then the user is zooming.
// Otherwise, if the fingers are moving along the same direction they're panning
if (self.pinch && Math.abs(pinchMotion[0]) > 0.5 && Math.abs(pinchMotion[1]) > 0.5
&& Math.sign(pinchMotion[0]) != Math.sign(pinchMotion[1]))
{
// Pinch distance change for zooming
var oldDist = pointDist(oldTouches[0], oldTouches[1]);
var newDist = pointDist(newTouches[0], newTouches[1]);
self.pinch(newDist - oldDist);
} else if (self.twoFingerDrag && Math.abs(panMotion[0]) > 0.5 && Math.abs(panMotion[1]) > 0.5
&& Math.sign(panMotion[0]) == Math.sign(panMotion[1]))
{
// Pan by the average motion of the two fingers
var panAmount = vec2.lerp(vec2.create(), motionVectors[0], motionVectors[1], 0.5);
panAmount[1] = -panAmount[1];
self.twoFingerDrag(panAmount);
}
}
// Update the existing list of touches with the current positions
for (var i = 0; i < evt.changedTouches.length; ++i) {
var t = evt.changedTouches[i];
touches[t.identifier] = [t.clientX - rect.left, t.clientY - rect.top];
}
});
var touchEnd = function(evt) {
evt.preventDefault();
for (var i = 0; i < evt.changedTouches.length; ++i) {
var t = evt.changedTouches[i];
delete touches[t.identifier];
}
}
canvas.addEventListener("touchcancel", touchEnd);
canvas.addEventListener("touchend", touchEnd);
}