Statistics
| Branch: | Revision:

blinker / firefox.plugin / data / newscript.js @ a03cd52e

History | View | Annotate | Download (12.288 KB)

1
/*
2
 * Copyright 2015 Thies Pfeiffer and Dimitri Heil
3
 * Blinker is distributed under the terms of the GNU General Public License
4
 * 
5
 * This file is part of Blinker.
6
 * 
7
 * Blinker is free software: you can redistribute it and/or modify
8
 * it under the terms of the GNU General Public License as published by
9
 * the Free Software Foundation, either version 3 of the License, or
10
 * (at your option) any later version.
11
 * 
12
 * Blinker is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
 * GNU General Public License for more details.
16
 * 
17
 * You should have received a copy of the GNU General Public License
18
 * along with Blinker. If not, see <http://www.gnu.org/licenses/>.
19
 */
20
 
21
window.onerror = function myErrorHandler(errorMsg, url, lineNumber) {
22
  console.log("error at line"+lineNumber);
23
  return true;
24
}
25

    
26

    
27
$("body").prepend("<div id='cross'>X</div>");
28
$("body").prepend("<div id='whiteout'></div>");
29
$("body").prepend("<div id='status'></div>");
30
$("body").prepend("<div id='gazeeventdiv'>X:"+(window.outerHeight - window.innerHeight)+"_"+window.screenX+"_Y:"+window.screenY+"</div>");
31
//$("body").prepend("<div id='calibrationImageDiv'><img src='" + self.options.pngUrl + "' /></div>");
32
var firstMessageReceived = false;
33
var cross = document.getElementById("cross");
34

    
35
var calibrationTimer = "null";
36
var calibrationRunning = false;
37
var eyeTrackerIsCalibrated = "notSet";
38
var pushMode = "notSet";
39
//cross.style.position = "absolute";
40
cross.style.zIndex = "16777271";
41
//cross.style.zIndex = "0";
42

    
43

    
44
/*var onGazeOver = document.createEventObject();
45
onGazeOver.eventType = "ongazeover";
46
var onGazeOut = document.createEventObject();
47
onGazeOut.eventType = "ongazeout";*/
48

    
49

    
50
$( "#gazeeventdiv" ).css("position","absolute");
51
$( "#gazeeventdiv" ).css("left","300px");
52
$( "#gazeeventdiv" ).css("top","300px");
53
$( "#gazeeventdiv" ).css("height","200px");
54
$( "#gazeeventdiv" ).css("width","200px");
55
$( "#gazeeventdiv" ).css("zIndex",100000);
56
$( "#gazeeventdiv" ).css("backgroundColor","green");
57

    
58

    
59
$( "#whiteout" ).css("backgroundColor","#FFFFFF");
60
$( "#whiteout" ).css("width","100%");
61
$( "#whiteout" ).css("height","100%");
62
$( "#whiteout" ).css("zIndex",16777270);
63
$( "#whiteout" ).css("visibility","hidden");
64
$( "#whiteout" ).css("position","fixed");
65
$( "#whiteout" ).css("left","0px");
66
$( "#whiteout" ).css("top","0px");
67

    
68

    
69
        
70

    
71
var yOffsetOfWindow = window.outerHeight - window.innerHeight;
72
var xOffsetOfWindow = window.outerWidth - window.innerWidth;
73

    
74
var gazeOver = new Event('gazeOver');
75
var gazeOut = new Event('gazeOut');
76
var multiplier = 1;
77
var ged = document.getElementById("gazeeventdiv");
78
ged.addEventListener('gazeOver', function(e){
79
        
80
        ged.style.backgroundColor = "yellow";
81
//window.fullScreen = false;
82
}, false);
83

    
84
ged.addEventListener('gazeOut',  function(e){
85
        
86
        ged.style.backgroundColor = "red";
87
        //startCalibration();
88
        //console.log("WIDTH:"+window.innerWidth+"|HEIGHT:"+window.innerHeight);
89
        //window.fullScreen = true;
90
        //var calib_1_x = (32 + xOffsetOfWindow + window.screenX - window.pageXOffset) * multiplier; 
91
        //var calib_1_y = (32 + yOffsetOfWindow + window.screenY - window.pageYOffset) * multiplier; 
92
        //console.log("calibration point Y: "+calib_1_y);
93
        //sendMessageToTracker("hi trackerdoodle.");
94

    
95
}, false);
96

    
97
var gazeInformation = {
98
                        "x":0,
99
                        "y":0,
100
                        "previousX":0,
101
                        "previousY":0,
102
                        "isFixation":false,
103
                        
104
                        "currentElementBelow":null,
105
                        "lastElementBelow":null,
106
                        };
107

    
108
self.port.on("calibrationImage", function(imgurl){
109
$("body").prepend("<img id='calibrationImage' src='" + imgurl + "' />");
110
$( "#calibrationImage" ).css("position","absolute");
111
$( "#calibrationImage" ).css("zIndex",16777271);
112
  /*var img = document.createElement("img");
113
  img.src = imgurl;
114
  document.body.appendChild(img);*/
115
});
116
                        
117
self.port.on("Multiplier", function(message) {        
118
        try {
119
                multiplier = parseFloat(message);
120
        } catch (e){
121
                console.log("Multiplier could not be read - will be set to 1");
122
                multiplier = "1";
123
        }
124
        console.log("Mult is: "+multiplier);
125
});
126
                        
127
self.port.on("initializeEyeTracker", function(message){
128

    
129
        initializeEyeTracker();
130
        
131

    
132
});
133

    
134
var recalibrationArray = new Array();
135
var recalibrationCounter = 1;
136
var calibrationPointNumber = 1;
137
self.port.on("eyeTrackerData", function(message) {
138
        
139
        
140
        var messages; 
141
        //split messages if they are packed into one
142
        if (message.search(/}\n{/) > -1){
143
                messages = message.split(/\n/);
144
                
145
                for (var v = 0;v < messages.length; v++){
146
                
147
                        //only parse messages when the string is longer then 5
148
                        if (messages[v].length > 5){
149
                
150
                                parseMessage(messages[v]);
151
                        }
152
                }
153
                
154
        } else {
155
                
156
                parseMessage(message);
157
        }
158

    
159
});
160

    
161
function parseMessage(message){
162
try {
163
        
164
        
165
    eyeTrackingData = JSON.parse(message);
166
        
167
        if (eyeTrackingData.category == "tracker"){        
168
                //console.log("is tracker");
169
                if (eyeTrackingData.hasOwnProperty("values")){
170
                        if (eyeTrackerIsCalibrated != "true"){
171
                                if ((eyeTrackingData.values.iscalibrated == false)) {
172
                                        console.log("eyetracker is not calibrated");
173
                                        eyeTrackerIsCalibrated = false;        
174
                                        if ((!calibrationRunning)&&(!eyeTrackerIsCalibrated)){
175
                                                startCalibration();
176
                                        }                                
177
                                }
178
                        }
179
                        if (calibrationRunning){
180
                if (eyeTrackingData.values.hasOwnProperty("frame")){
181
                        if ((eyeTrackingData.values.frame.lefteye.psize == 0)||(eyeTrackingData.values.frame.righteye.psize == 0)){
182
                                        $( "#calibrationImage" ).css("visibility","hidden");
183
                                        window.clearTimeout(calibrationTimer);
184
                                        startCalibrationPointTimer();
185
                                } else {
186
                                        $( "#calibrationImage" ).css("visibility","visible");
187
                                        
188
                                }
189
                        }
190
                }
191
                
192
                }
193
        
194
        if (eyeTrackerIsCalibrated){
195
        avg = eyeTrackingData.values.frame.avg;
196
        raw = eyeTrackingData.values.frame.raw;
197
        
198
        //console.log("AVG"+avg.x);
199
        //console.log("RAW"+raw.x);
200
        var x = avg.x;
201
        var y = avg.y;
202
        //var position = avg.x+"_"+avg.y;
203
        var xOffset = parseFloat(parseFloat(x)/multiplier) + parseFloat(window.pageXOffset) - parseFloat(window.screenX) - parseFloat(xOffsetOfWindow);
204
        var yOffset = parseFloat(parseFloat(y)/multiplier) + parseFloat(window.pageYOffset) - parseFloat(window.screenY) - parseFloat(yOffsetOfWindow);            
205
        gazePosition(xOffset,yOffset);        
206
        
207
        }
208
        } else if (eyeTrackingData.category == "calibration"){
209
                console.log(message);
210
                if ((eyeTrackingData.request == "start") && (eyeTrackingData.statuscode == 200)){
211
                                calibrationPointNumber = 0;
212
                                sendCalibrationPoint(calibrationPointNumber);
213
                                calibrationPointNumber++;
214
                }
215
                if ((eyeTrackingData.request == "pointstart") && (eyeTrackingData.statuscode == 200)){
216
                                startCalibrationPointTimer();
217
                }
218
                if ((eyeTrackingData.request == "pointend") && (eyeTrackingData.statuscode == 200)){
219
                        console.log("got pointend response 200");
220
                        console.log("calibrationPointCounter: "+calibrationPointNumber);
221
                                if ((calibrationPointNumber == 9)&&(eyeTrackingData.hasOwnProperty('values'))){
222
                                        //calibrationRunning = false;
223
                                        
224
                                        
225
                                        var calibPoints = eyeTrackingData.values.calibresult.calibpoints;
226
                                        for (var c = 0; c<9;c++){
227
                                                console.log("Point "+ c +" state : "+calibPoints[c].state);
228
                                                if (calibPoints[c].state != "2"){
229
                                                        console.log("calib point "+c+" was bad putting into recalibrationArray.");
230
                                                        recalibrationArray.push(c);                        
231
                                                        //recalibrationCounter++;
232
                                                        //sendCalibrationPoint(c);
233
                                                        //startCalibrationPointTimer();
234
                                                }
235
                                        }                                        
236
                                        console.log("result is: "+eyeTrackingData.values.calibresult.result);
237
                                        if (eyeTrackingData.values.calibresult.result == false){
238
                                                console.log("bad_data restarting calibration");
239
                                                console.log(recalibrationArray.length+" points have to be recalibrated ");
240
                                                for (var r=0;r<recalibrationArray.length;r++){
241
                                                        console.log("sending Point for recalibration: "+recalibrationArray[r]);
242
                                                        sendCalibrationPoint(recalibrationArray[r]);
243
                                                        recalibrationArray.splice(r,1);
244
                                                        //console.log(recalibrationArray[r].x +"|" +recalibrationArray[r].y);
245
                                                }
246
                                                
247
                                        } else {
248
                                                console.log("Eyetracker was successfully calibrated");
249
                                                
250
                                        
251
                                        }
252
                                } else if (calibrationPointNumber < 9){                                
253
                                sendCalibrationPoint(calibrationPointNumber);
254
                                calibrationPointNumber++;
255
                                //startCalibrationPointTimer();
256
                                }
257
                }
258
        
259
        
260
        }
261
        
262
        }catch(e){
263
                console.log("error at line:"+e.lineNumber);
264
        console.log("error:"+e);
265
                console.log("error at message:"+message);
266
    }
267

    
268
}
269

    
270

    
271

    
272
function sendMessageToTracker(message){
273
self.port.emit("sendToTracker",message);
274
}
275

    
276
function initializeEyeTracker(){
277
        sendMessageToTracker('{"category": "tracker","request": "get","values": ["push", "iscalibrated" ]}');
278
        sendMessageToTracker('{"category": "tracker","request": "set","values": {"push": true,"version": 1}}');
279
}
280

    
281

    
282

    
283
function gazePosition(x,y){
284
        gazeInformation.previousX = gazeInformation.x;
285
        gazeInformation.previousY = gazeInformation.y;
286
        gazeInformation.x = x;
287
        gazeInformation.y = y;
288
    cross.style.left = x+"px";
289
    cross.style.top  = y+"px";
290
        document.getElementById("status").innerHTML = x +"|"+y;
291
        var elementBelow = document.elementFromPoint(x,y);
292
        
293
        if (elementBelow != null){
294
                //console.log("elementBelow is: "+ elementBelow.id);
295
                if (gazeInformation.lastElementBelow != null){
296
                //console.log("lastElementBelow is: "+ gazeInformation.lastElementBelow.id);
297
                }
298
                if (gazeInformation.lastElementBelow != elementBelow){
299
                        gazeInformation.lastElementBelow = gazeInformation.elementBelow;
300
                        
301
                        if (gazeInformation.lastElementBelow != null){                        
302
                                var gazeLastElem = gazeInformation.lastElementBelow;
303
                                //gazeLastElem
304
                                if (document.getElementById(gazeLastElem.id) != null){
305
                                        document.getElementById(gazeLastElem.id).dispatchEvent(gazeOut);
306
                                }
307
                                //$(gazeLastElem).trigger('gazeOut');
308
                        }
309
                                gazeInformation.elementBelow = elementBelow;                                        
310
                                var gazeElemCurrent = gazeInformation.elementBelow;
311
                                if (document.getElementById(gazeElemCurrent.id) != null){
312
                                        document.getElementById(gazeElemCurrent.id).dispatchEvent(gazeOver);
313
                                }
314
                                
315
                } 
316
                
317
                
318
                
319
                
320
                
321
        }
322
        
323
}
324

    
325
function startCalibration(){
326
        endRunningCalibration();
327
        calibrationRunning = true;
328
        $( "#whiteout" ).css("visibility","visible");
329
        console.log("startCalibration");
330
        sendCalibrationRequest(9);
331
}
332

    
333
function endRunningCalibration(){
334

    
335
        sendMessageToTracker('{"category": "calibration","request" : "abort"}');
336
}
337

    
338
//CALIBRATIONS
339

    
340

    
341
var calibrationArray = new Array();
342
//32 is half of calibrationImage
343
calibrationArray[0] = {x:32,y:32} //topleft
344
calibrationArray[1] = {x:32,y:window.outerHeight/2} //midleft
345
calibrationArray[2] = {x:32,y:window.outerHeight-32} //bottomleft
346
calibrationArray[3] = {x:window.outerWidth/2,y:32} //midtop
347
calibrationArray[4] = {x:window.outerWidth/2,y:window.outerHeight/2} //center
348
calibrationArray[5] = {x:window.outerWidth/2,y:window.outerHeight-32} //midbottom
349
calibrationArray[6] = {x:window.outerWidth-32,y:32} //topright
350
calibrationArray[7] = {x:window.outerWidth-32,y:window.outerHeight/2} //midright
351
calibrationArray[8] = {x:window.outerWidth-32,y:window.outerHeight-32} //bottomright
352

    
353
var calibrationStartSuccessful = false;
354

    
355
//socket: the socket to send the calibration request to
356
//points: number of calibration points
357
function sendCalibrationRequest(points){
358
        console.log("send calibration request to tracker");
359
        sendMessageToTracker("{\"category\":\"calibration\",\"request\":\"start\",\"values\":{\"pointcount\": "+points+"}}");
360
}
361

    
362
//pointX/Y: the calibrationpoint coordinates
363
function sendCalibrationPoint(number){
364

    
365
$( "#calibrationImage" ).css("left",(calibrationArray[number].x - 32)+"px");
366
$( "#calibrationImage" ).css("top", (calibrationArray[number].y - 32)+"px");
367

    
368
        console.log("Sending Calibration Point: "+number+" -- "+calibrationArray[number].x+"|"+calibrationArray[number].y);
369
        console.log("message:" + "{'category':'calibration','request':'pointstart','values':{'x': "+calibrationArray[number].x+",'y':"+calibrationArray[number].y+"}}");
370
        sendMessageToTracker("{\"category\":\"calibration\",\"request\":\"pointstart\",\"values\":{\"x\": "+calibrationArray[number].x+",\"y\":"+calibrationArray[number].y+"}}");
371
        //startCalibrationPointTimer();
372
        
373
        //socket.write("{'category':'calibration','request':'pointstart','values':{'x': "+pointX+",'y':"+pointY+"}}");
374
}
375

    
376
function sendCalibrationPointend(){
377
        console.log("Sending Calibration Pointend.");
378
        sendMessageToTracker("{\"category\":\"calibration\",\"request\":\"pointend\"}");
379
}
380

    
381
function startCalibrationPointTimer(){
382
        console.log("Started PointCounter.");
383
        calibrationTimer = setTimeout(function() { sendCalibrationPointend(); }, 1000);
384
}