create subscribe mechanism for gaze updates

This commit is contained in:
Josh de Leeuw 2021-03-15 10:35:06 -04:00
parent c6797fe027
commit 45872110c6
2 changed files with 18 additions and 4 deletions

View File

@ -33,6 +33,7 @@ jsPsych.extensions['webgazer'] = (function () {
state.initialized = false;
state.activeTrial = false;
state.round_predictions = params.round_predictions;
state.gazeUpdateCallbacks = [];
// hide video by default
extension.hideVideo();
@ -191,6 +192,15 @@ jsPsych.extensions['webgazer'] = (function () {
return state.currentGaze;
}
extension.onGazeUpdate = function(callback){
state.gazeUpdateCallbacks.push(callback);
return function(){
state.gazeUpdateCallbacks = state.gazeUpdateCallbacks.filter(function(item){
return item !== callback;
});
}
}
function handleGazeDataUpdate(gazeData, elapsedTime) {
if (gazeData !== null){
var d = {
@ -202,6 +212,9 @@ jsPsych.extensions['webgazer'] = (function () {
state.currentTrialData.push(d); // add data to current trial's data
}
state.currentGaze = d;
for(var i=0; i<state.gazeUpdateCallbacks.length; i++){
state.gazeUpdateCallbacks[i](d);
}
} else {
state.currentGaze = null;
}

View File

@ -104,17 +104,18 @@
var pt_finish = pt_start_val + trial.validation_duration;
var pt_data = [];
requestAnimationFrame(function watch_dot(){
var cancelGazeUpdate = jsPsych.extensions['webgazer'].onGazeUpdate(function(prediction){
if(performance.now() > pt_start_val){
var prediction = jsPsych.extensions['webgazer'].getCurrentPrediction();
pt_data.push({dx: prediction.x - x, dy: prediction.y - y, t: Math.round(performance.now()-start)});
}
})
requestAnimationFrame(function watch_dot(){
if(performance.now() < pt_finish){
requestAnimationFrame(watch_dot);
} else {
trial_data.raw_gaze.push(pt_data);
cancelGazeUpdate();
next_validation_point();
}
});