-
Notifications
You must be signed in to change notification settings - Fork 0
/
fixdet.js
138 lines (126 loc) · 3.41 KB
/
fixdet.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
Typium.FixationDetector =
{
currentFix: null,
candidateFix: null,
/*
offset: {
x: 0,
y: 0
},
zoom: {
x: 1.0,
y: 1.0
},*/
// internal
createSample: function(x, y)
{
return {x: x, y: y};
},
// params:
// ts: timestamp in milliseconds
// x: gaze x in pixels
// y: gaze y in pixels
createFixation: function(ts, x, y)
{
return {
ts: ts,
x: x,
y: y,
duration: 0,
saccade: {
dx: 0,
dy: 0
},
samples: new Array()
}
},
// params:
// ts: timestamp in milliseconds
// x: gaze x in pixels
// y: gaze y in pixels
addSampleToFix: function(fix, ts, x, y)
{
while(fix.samples.length >= Typium.options.fixdetBufferSize)
fix.samples.shift();
fix.samples.push(this.createSample(x, y));
fix.duration = ts - fix.ts;
var fx = 0;
var fy = 0;
for(var i = 0; i < fix.samples.length; i++) {
var sample = fix.samples[i];
fx += sample.x;
fy += sample.y;
}
fix.x = fx / fix.samples.length;
fix.y = fy / fix.samples.length;
},
// public
// params:
// ts: timestamp in milliseconds
// x: gaze x in pixels
// y: gaze y in pixels
// returns:
// true if a new fixation starts, false otherwise
feed: function(ts, x, y)
{
//x = (x - this.offset.x) / this.zoom.x;
//y = (y - this.offset.y) / this.zoom.y;
var result = false;
if(!this.currentFix) {
this.currentFix = this.createFixation(ts, x, y);
result = true;
} else if(!this.candidateFix) {
var dx = this.currentFix.x - x;
var dy = this.currentFix.y - y;
var dist = Math.sqrt(dx*dx + dy*dy);
if(dist < Typium.options.fixdetMaxFixSize) {
this.addSampleToFix(this.currentFix, ts, x, y);
} else {
this.candidateFix = this.createFixation(ts, x, y);
this.candidateFix.saccade.dx = x - this.currentFix.x;
this.candidateFix.saccade.dy = y - this.currentFix.y;
}
} else {
var dxCurr = this.currentFix.x - x;
var dyCurr = this.currentFix.y - y;
var distCurr = Math.sqrt(dxCurr*dxCurr + dyCurr*dyCurr);
var dxCand = this.candidateFix.x - x;
var dyCand = this.candidateFix.y - y;
var distCand = Math.sqrt(dxCand*dxCand + dyCand*dyCand);
if(distCurr < Typium.options.fixdetMaxFixSize) {
this.addSampleToFix(this.currentFix, ts, x, y);
this.candidateFix = null;
} else if(distCand < Typium.options.fixdetMaxFixSize) {
this.currentFix = this.candidateFix;
this.candidateFix = null;
this.addSampleToFix(this.currentFix, ts, x, y);
result = true;
} else {
this.candidateFix = this.createFixation(ts, x, y);
this.candidateFix.saccade.dx = x - this.currentFix.x;
this.candidateFix.saccade.dy = y - this.currentFix.y;
}
}
return result;
},
// events
calcTransformParams: function ()
{
Typium.FixationDetector.zoom = {
x: document.width / window.innerWidth,
y: document.height / window.innerHeight
};
Typium.FixationDetector.offset = {
x: window.screenX + (window.outerWidth - document.width) / 2,
y: window.screenY + (window.outerHeight - document.height) -
(window.screenY > 0 ? ((window.outerWidth - document.width) / 2) : 0)
};
console.log("resized");
}
};
// -----------------------------------------------------
// Now Chrome dow not fire the resize event inside of extension.
// The functionality is moved to controller.js
// -----------------------------------------------------
//Typium.FixationDetector.calcTransformParams();
//window.addEventListener("resize", Typium.FixationDetector.calcTransformParams);