-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmousetracker.js
194 lines (175 loc) · 4.26 KB
/
mousetracker.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
gestsMousetracker = function() {
var enabled = false;
var rmousedown = false;
var gestured = false;
var lastX = null;
var lastY = null;
var newX = null;
var newY = null;
var ctx = null;
var canvas = null;
var settings = null;
var pathArr = [];
var continuous = true;
var sequence = "";
var onContext = function(){};
var onLetter = function(){};
var onFinished = function(){};
document.addEventListener("mousedown", function(event) {
if(event.which === 3 && enabled) {
rmousedown = true;
lastX = event.clientX;
lastY = event.clientY;
if(continuous) {
pathArr = [];
pathArr.push([lastX,lastY]);
}
}
});
document.addEventListener("mouseup", function(event) {
if(event.which === 3 && enabled) {
rmousedown = false;
}
});
document.addEventListener("mousemove", function(event) {
if(!(rmousedown && enabled)) {
return;
}
newX = event.clientX;
newY = event.clientY;
if( lastX === newX && lastY === newY ) {
return;
}
rSquared = (newX-lastX)*(newX-lastX) + (newY-lastY)*(newY-lastY);
if(rSquared > 64) { // mouse moved at least 8 pixels
if(continuous){
pathArr.push([newX,newY]);
}
if(!gestured) {
gestured = true;
if(settings.path.draw) {
ctx = createContext();
}
onContext();
}
if(settings.path.draw) {
if(continuous){
drawContinuous(ctx, pathArr);
} else {
drawSegment(ctx, lastX, lastY, newX, newY);
}
}
var letter = coordsToLetter(lastX, lastY, newX, newY);
if(letterIsPushable(sequence, letter) && sequence.length < 11) {
sequence += letter;
onLetter(sequence, letter);
}
lastX = newX;
lastY = newY;
}
});
document.addEventListener("contextmenu", function(event) {
if(gestured && enabled) {
event.preventDefault();
gestured = false;
if(settings.path.draw) {
deleteContext();
}
onFinished(sequence);
sequence = "";
return false;
}
});
function createContext() {
canvas = document.getElementById('gestsCanvas');
if(!canvas) {
canvas = document.createElement('canvas');
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
canvas.id = 'gestsCanvas';
document.body.appendChild(canvas);
return canvas.getContext('2d');
}
else {
return canvas.getContext('2d');
}
}
function deleteContext() {
var canvas = document.getElementById('gestsCanvas')
if(canvas) {
document.body.removeChild(canvas);
}
}
function drawSegment(ctx, lx, ly, nx, ny) {
ctx.beginPath();
var colorString = hexToRgba(settings.path.color, settings.path.opacity);
ctx.strokeStyle = colorString;
ctx.lineWidth = settings.path.width;
ctx.moveTo(lx,ly);
ctx.lineTo(nx,ny);
ctx.stroke();
}
function drawContinuous(ctx, pathArr) {
if(pathArr.length>1) {
ctx.clearRect(0,0,canvas.width, canvas.height);
ctx.beginPath();
ctx.lineWidth = settings.path.width;
ctx.strokeStyle = hexToRgba(settings.path.color, settings.path.opacity);
ctx.lineJoin = "round";
ctx.moveTo(pathArr[0][0],pathArr[0][1]);
for(var i=1;i<pathArr.length;i++){
ctx.lineTo(pathArr[i][0], pathArr[i][1]);
}
ctx.stroke();
}
}
function letterIsPushable(seq, letter) {
if(!seq || seq[seq.length-1] !== letter) {
return true;
} else {
return false;
}
}
function hexToRgba(str, alpha) {
// string is of form "#ff00ff"
// alpha is between 0 and 1
var hexString = str.replace('#','');
var num = parseInt(hexString, 16);
var r = num >> 16;
var g = (num >> 8) % 256;
var b = num % 256;
var a = alpha;
var result = 'rgba(' + r + ',' + g + ',' + b + ',' + a + ')';
return result;
}
function coordsToLetter(lx, ly, nx, ny) {
var theta = Math.atan2(ly-ny, nx-lx);
var letter = '';
var pi = Math.PI;
if( pi/4 > theta && theta >= -pi/4 ) letter = 'R';
else if( 3/4*pi > theta && theta >= pi/4 ) letter = 'U';
else if( -pi/4 > theta && theta >= -3/4*pi ) letter = 'D';
else letter = 'L';
return letter;
}
function setCallbacks(cb1, cb2, cb3) {
onContext = cb1;
onLetter = cb2;
onFinished = cb3;
}
function enable() {
enabled = true;
}
function disable() {
enabled = false;
}
function loadSettings(extSettings) {
settings = extSettings;
}
return {
enable: enable,
disable: disable,
setCallbacks: setCallbacks,
loadSettings: loadSettings
};
};