-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.js
215 lines (186 loc) · 6.29 KB
/
main.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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
(function(window) {
// screen dimensions for calculations
var width = window.innerWidth
|| document.documentElement.clientWidth
|| document.body.clientWidth;
var height = window.innerHeight
|| document.documentElement.clientHeight
|| document.body.clientHeight;
// number of values to moving average for reducing noise, and associated vars
var numToAverage = 12;
var valuesQueue = [];
var xPrediction = 0;
var yPrediction = 0;
// some other static calculations
var scrollMarginPercent = 0.17;
var scrollPercent = 0.30;
var scrollDuration = 1000;
var xScrollOffset = function() {
return width * scrollPercent;
};
var yScrollOffset = function() {
return height * scrollPercent;
};
// ui vars
var scrollMarginPercentInput = document.getElementById('scroll-margin-perc');
var scrollPercentInput = document.getElementById('scroll-perc');
var scrollDurationInput = document.getElementById('scroll-duration');
var numToAverageInput = document.getElementById('number-avg');
var saveDataInput = document.getElementById('save-data');
// set initial input variable vals
scrollMarginPercentInput.value = scrollMarginPercent * 100;
scrollPercentInput.value = scrollPercent * 100;
scrollDurationInput.value = scrollDuration;
numToAverageInput.value = numToAverage;
// if data was previously saved, keep it checked
if (window.localStorage.getItem('webgazerGlobalData')) {
saveDataInput.checked = true;
}
/**
* Callbacks for the UI
*/
scrollMarginPercentInput.addEventListener('change', function(e) {
if (!isNaN(e.target.value)) {
scrollMarginPercent = e.target.value / 100;
}
});
scrollPercentInput.addEventListener('change', function(e) {
if (!isNaN(e.target.value)) {
scrollPercent = e.target.value / 100;
}
});
scrollDurationInput.addEventListener('change', function(e) {
if (!isNaN(e.target.value)) {
scrollDuration = e.target.value;
}
});
numToAverageInput.addEventListener('change', function(e) {
if (!isNaN(e.target.value)) {
numToAverage = e.target.value;
valuesQueue = []
xPrediction = 0;
yPrediction = 0;
}
});
document.getElementById('show-predictions').addEventListener('click', function(e) {
webgazer.showPredictionPoints(true);
});
document.getElementById('hide-predictions').addEventListener('click', function(e) {
webgazer.showPredictionPoints(false);
});
document.getElementById('start-scrolling').addEventListener('click', function(e) {
webgazer.setGazeListener(scrollGazeListener);
});
document.getElementById('stop-scrolling').addEventListener('click', function(e) {
webgazer.clearGazeListener();
});
/**
* Save the model on exit. See https://webgazer.cs.brown.edu/#usage
*/
window.addEventListener("beforeunload", function (e) {
// this saves regression data to localStorage
if (saveDataInput.checked) {
webgazer.end();
console.log('Saving data model to localStorage');
}
else {
window.localStorage.clear();
}
});
/**
* The gaze listener, checks if you are looking in a border and scrolls for
* you. Also, takes a moving average of the data, storing measurements in
* a queue and keeping the average. The queue stores just the contributions
* i.e. the weight of each measurement in the contribution
*/
var scrollGazeListener = function(data, elapsedTime) {
if (data == null) {
return;
}
// values queue doesn't have enough values yet
else if (valuesQueue.length < numToAverage) {
var curXContrib = data.x / numToAverage;
var curYContrib = data.y / numToAverage;
xPrediction += curXContrib;
yPrediction += curYContrib;
valuesQueue.push([curXContrib, curYContrib]);
return;
}
/** Update the moving average */
// remove the oldest measurement
var oldest = valuesQueue.shift();
xPrediction -= oldest[0];
yPrediction -= oldest[1];
// add in the new, current measurement
var curXContrib = data.x / numToAverage;
var curYContrib = data.y / numToAverage;
xPrediction += curXContrib;
yPrediction += curYContrib;
valuesQueue.push([curXContrib, curYContrib]);
/** do the computations for the current average */
// x and y relative coordinates (scaled 0.0 to 1.0), being top left
var xRel = 1 - (width - xPrediction) / width;
var yRel = 1 - (height - yPrediction) / height;
var xScroll = 0;
var yScroll = 0;
// looking near the left border
if (xRel <= scrollMarginPercent) {
xScroll = -xScrollOffset();
}
// looking near the right border
else if (xRel >= 1 - scrollMarginPercent) {
xScroll = xScrollOffset();
}
// looking near the top border
if (yRel <= scrollMarginPercent) {
yScroll = -yScrollOffset();
}
// looking near the bottom border
else if (yRel >= 1 - scrollMarginPercent) {
yScroll = yScrollOffset();
}
// do the actual window scroll
if (xScroll || yScroll) {
scrollBySmooth(xScroll, yScroll, scrollDuration);
}
};
/**
* Easing function for smoothing the scrolling
*/
var easeOutCubic = function (t) { return (--t)*t*t+1 };
var easeOutQuint = function (t) { return 1+(--t)*t*t*t*t };
var isCurrentlyScrolling = false;
var scrollBySmooth = function(dx, dy, duration, easeingFunc) {
// only allow one scrollBySmooth to run at a time
if (isCurrentlyScrolling) return;
easeingFunc = easeingFunc || easeOutQuint;
isCurrentlyScrolling = true;
var start = null;
var xStart = window.scrollX;
var yStart = window.scrollY;
var animate = function(time) {
if (!start) start = time;
var tPerc = (time - start) / duration;
// calculate next position
var easePerc = easeingFunc(tPerc);
var xNext = easePerc * dx + xStart;
var yNext = easePerc * dy + yStart;
// scroll to next
window.scrollTo(xNext, yNext);
if (tPerc < 1) {
window.requestAnimationFrame(animate);
}
else {
isCurrentlyScrolling = false;
}
};
window.requestAnimationFrame(animate);
};
// start tracking and calibrating immediately
webgazer
.setRegression('ridge')
.setTracker('clmtrackr')
.begin();
console.log(webgazer.getRegression());
console.log(webgazer.getTracker());
}(window));