-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.html
246 lines (224 loc) · 7.01 KB
/
index.html
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
245
246
<!DOCTYPE html>
<html>
<head>
<link rel = "stylesheet"
type = "text/css"
href = "index.css" />
</head>
</head>
<canvas id="screenCanvas" style="border:1px solid #a282ec;"></canvas>
<body onload="doWebGazer()";>
<script src="webgazer.js" type="text/javascript" ></script>
<!-- Add and position buttons on screen -->
<!-- Note: the buttons are not positioned in the most efficient way right now, this could definitely be fixed -->
<div style="
position: absolute;
top: 0px;
right: 610px;
">
<input id="one" type="button" class="key" value="1" onclick="updateTextfield('1')"/>
</div>
<div style="
position: absolute;
top: 0px;
right: 305px;
">
<input id="two" type="button" class="key" value="2" onclick="updateTextfield('2')"/>
</div>
<div style="
position: absolute;
top: 0px;
right: 0px;
">
<input id="three" type="button" class="key" value="3" onclick="updateTextfield('3')"/>
</div>
<div style="
position: absolute;
top: 200px;
right: 0px;
">
<input id="four" type="button" class="key" value="4" onclick="updateTextfield('4')"/>
</div>
<div style="
position: absolute;
top: 400px;
right: 0px;
">
<input id="five" type="button" class="key" value="5" onclick="updateTextfield('5')"/>
</div>
<div style="
position: absolute;
bottom: 0px;
right: 0px;
">
<input id="six" type="button" class="key" value="6" onclick="updateTextfield('6')"/>
</div>
<div style="
position: absolute;
bottom: 0px;
right: 305px;
">
<input id="seven" type="button" class="key" value="7" onclick="updateTextfield('7')"/>
</div>
<div style="
position: absolute;
bottom: 0px;
right: 610px;
">
<input id="eight" type="button" class="key" value="8" onclick="updateTextfield('8')"/>
</div>
<div style="
position: absolute;
bottom: 0px;
left: 305px;
">
<input id="nine" type="button" class="key" value="9" onclick="updateTextfield('9')"/>
</div>
<div style="
position: absolute;
bottom: 0px;
left: 0px;
">
<input id="zero" type="button" class="key" value="0" onclick="updateTextfield('0')"/>
</div>
<!-- Add input box to display text -->
<div style="
text-align: center;
position: absolute;
top: 250px;
left: 550px;
">
<input class="input"/>
</div>
<!-- Add backspace button -->
<div style="
text-align: center;
position: absolute;
top: 240px;
left: 0px;">
<input id="backspace" type="button" class="key" value="backspace" onclick="clearTextfield()"/>
</div>
<script>
// This function sets up WebGazer using the API, and assigns ranges for the buttons "clickability"
function doWebGazer()
{
webgazer.begin();
xcoordinates = [];
ycoordinates = [];
var buttonHeight=60;
var buttonWidth=120;
var bottomY=638; // height of canvas
var rightX=1356; // width of canvas
var buttonXDistance;
var buttonYDistance;
var xAvg;
var yAvg;
var intervalID = window.setInterval(function(){
xAvg = getAvgCoordinates(xcoordinates);
yAvg = getAvgCoordinates(ycoordinates);
//console.log(xAvg);
//console.log(yAvg);
xcoordinates = [];
ycoordinates = [];
// returns number based on coordinates of where the user is looking
if (xAvg> (rightX-610)-buttonWidth && xAvg < rightX-610 && yAvg < buttonHeight){
document.getElementById('one').click();
}
else if (xAvg> (rightX-305)-buttonWidth && xAvg < rightX-305 && yAvg < buttonHeight){
document.getElementById('two').click();
}
else if (xAvg> rightX-buttonWidth && yAvg < buttonHeight){
document.getElementById('three').click();
}
else if (xAvg> rightX-buttonWidth && yAvg < buttonHeight+200 && yAvg > 200){
document.getElementById('four').click();
}
else if (xAvg> rightX-buttonWidth && yAvg < buttonHeight+400 && yAvg > 400){
document.getElementById('five').click();
}
else if (xAvg> rightX-buttonWidth && yAvg > bottomY-buttonHeight){
document.getElementById('six').click();
}
else if (xAvg> rightX-305-buttonWidth && xAvg> rightX-305 && yAvg > bottomY-buttonHeight){
document.getElementById('seven').click();
}
else if (xAvg> rightX-610-buttonWidth && xAvg> rightX-610 && yAvg > bottomY-buttonHeight){
document.getElementById('eight').click();
}
else if (xAvg< 305 + buttonWidth && xAvg> 305 && yAvg > bottomY-buttonHeight){
document.getElementById('nine').click();
}
else if (xAvg< buttonWidth && yAvg > bottomY-buttonHeight){
document.getElementById('zero').click();
}
else if (xAvg< buttonWidth && yAvg > 240 && yAvg < 240 + buttonHeight){
document.getElementById('backspace').click();
}
}, 500); // cycle runs every 500 milliseconds
// sets up the canvas
var canvas = document.getElementById("screenCanvas");
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
// set red pointer on screen to reflect where gaze is
var ctx = canvas.getContext("2d");
ctx.fillStyle = "#FF0000";
// check for gaze
webgazer.setGazeListener(function(data, elapsedTime)
{
if (data == null)
{
return;
}
// make gaze coordinate prediction, and add to array of points if it exists
var prediction = webgazer.getCurrentPrediction();
if (prediction)
{
var checkx = prediction.x;
var checky = prediction.y;
xcoordinates.push(checkx);
ycoordinates.push(checky);
if (xcoordinates.length>10)
{
xcoordinates.shift();
ycoordinates.shift();
}
// set red pointer to reflect where user is looking
//ctx.fillRect(checkx, checky, 20, 20);
ctx.fillRect(xAvg, yAvg, 20, 20);
if (elapsedTime%3>2)
{
ctx.clearRect(0, 0, rightX, bottomY);
}
}
}).begin();
}
// find average of coordinates
function getAvgCoordinates(arr)
{
var sum, avg = 0;
if (arr.length)
{
sum = arr.reduce(function(a, b) { return a + b; });
avg = sum / arr.length;
}
return avg
}
// add text to text field
function updateTextfield(input){
document.querySelector(".input").value += input;
}
// clear text field if length is 1, otherwise clear the last typed character
// effectively a backspace function
function clearTextfield(){
if (document.querySelector(".input").value.length==1)
{
document.querySelector(".input").value = "";
}
else
{
document.querySelector(".input").value=Math.floor(document.querySelector(".input").value*0.1);
}
}
</script>
</body>
</html>