jsPsych/packages/plugin-webgazer-calibrate/src/index.ts
Becky Gilbert db754538b7
Convert plugins to classes and update examples (#2078)
* modularize plugins: edit index.ts and example files (WIP)

* continue modularizing plugins: edit index.ts and example files (WIP)

* continue modularizing plugins: edit index.ts and example files (WIP)

* continue modularizing plugins and example files, make info static in plugin classes

* add preload flag to plugin info, fix a few parameterType values

* change preload flags in plugin info from audio/image/video strings to boolean

* convert more plugins and example files

* convert more plugins/examples, sync with webgazer branch plugins/examples (not working on this branch)

* remove preload flag from info, change to ParameterType, change to "ALL_KEYS"/"NO_KEYS", change KEY type to KEYS for array params

* remove descriptions from plugin info, fix some ALL_KEYS/NO_KEYS errors

* remove descriptions and add pretty names to plugin info

* remove/edit comments re ALL_KEYS/NO_KEYS and TS errors

* fix some preload plugin errors and convert example file (plugin still not working due to getAutoPreloadList)

* convert RDK plugin and example file

* convert preload plugin and example, add JsPsych getTimelineDescription method for auto-preloading (still not working for audio due to getAudioBuffer

* fix jsPsych getTimelineDescription method so that it does recursive search

* add image and video preloading to plugin examples

* Merge branch 'modularization' into modularization-plugins

* add tests for reconstruction

* fix timeline array getter method: add private timelineDescription var, set to timeline array in run(), rename getTimelineDescription to getTimeline

* changes in response to PR review: fix JsDoc comments, fix array: true in param info, change HTML string param types, add JsDoc author/file/see docs for all plugin classes, switch to @ts-expect-error. Thanks @bjoluc!

* fix JsDoc comments for plugin classes and preload trials parameter

* change preload type to boolean in ParameterInfo interface, and add `preload: false` to virtual-chinrest item_path image parameter

* All my tests except for preload plugin (will do that tomorrow)

* minor fixes to reconstruction tests

* Update preload plugin tests

* Finish test conversion

* switch to arrow functions to fix this keyword errors in plugins, add audio preloading into plugin example files, fix typos

* convert non-plugin example files (WIP, not tested)

* Fix allow_held_keys -> allow_held_key parameter in virtual-chinrest plugin

* Fix `keyboardListener` type in serial-reaction-time plugin

* type fixes for RDK, simplifying `correctOrNot()` function

* fixed ["ALL_KEYS"] => "ALL_KEYS" for iat plugins and tests

* Build jspsych packages before everything else

Dependent builds were previously failing due to missing type definitions

* Remove console.log from html-keyboard-response tests

I think I accidentially committed it.

* fix the delayed start to animation bug (#1885)

* round all RTs to nearest int (#2108)

* fixes and tests #1900 - IAT parameter problems

* finish converting/testing example files, add init settings, add audio preloading, fix errors

* fix progress-bar timeline to demo an example where auto-updating the progress bar works well

* Revert "round all RTs to nearest int (#2108)"

This reverts commit f53145d2e3.

* change how delayed timeline variables are implemented

* use static for generate_stimulus method so that it can be called on the vsl-grid-scene class

* fix external-html plugin and example (switch to arrow function for proper this context, fix incorrect parameter name)

* remove outdated TO DO comments

Co-authored-by: bjoluc <mail@bjoluc.de>
Co-authored-by: Josh de Leeuw <josh.deleeuw@gmail.com>
2021-09-08 09:44:53 -04:00

187 lines
5.8 KiB
TypeScript

import { JsPsych, JsPsychPlugin, ParameterType, TrialType } from "jspsych";
const info = <const>{
name: "webgazer-calibrate",
parameters: {
/** An array of calibration points, where each element is an array cointaining the coordinates for one calibration point: [x,y] */
calibration_points: {
type: ParameterType.INT, // TO DO: nested array, so different type?
default: [
[10, 10],
[10, 50],
[10, 90],
[50, 10],
[50, 50],
[50, 90],
[90, 10],
[90, 50],
[90, 90],
],
array: true,
},
/** What should the subject do in response to the calibration point presentation? Options are 'click' and 'view'. */
calibration_mode: {
type: ParameterType.SELECT,
options: ["click", "view"],
default: "click",
},
/** Size of the calibration points, in pixels */
point_size: {
type: ParameterType.INT,
default: 20,
},
/** Number of repetitions per calibration point */
repetitions_per_point: {
type: ParameterType.INT,
default: 1,
},
/** Whether or not to randomize the calibration point order */
randomize_calibration_order: {
type: ParameterType.BOOL,
default: false,
},
/** If calibration_mode is view, then this is the delay before calibration after the point is shown */
time_to_saccade: {
type: ParameterType.INT,
default: 1000,
},
/** If calibration_mode is view, then this is the length of time to show the point while calibrating */
time_per_point: {
type: ParameterType.INT,
default: 1000,
},
},
};
type Info = typeof info;
/**
* **webgazer-calibrate**
*
* jsPsych plugin for calibrating webcam eye gaze location estimation.
* Intended for use with the WebGazer eye-tracking extension, after the webcam has been initialized with the `webgazer-init-camera` plugin.
*
* @author Josh de Leeuw
* @see {@link https://www.jspsych.org/plugins/jspsych-webgazer-calibrate/ webgazer-calibrate plugin} and
* {@link https://www.jspsych.org/overview/eye-tracking/ eye-tracking overview} documentation on jspsych.org
*/
class WebgazerCalibratePlugin implements JsPsychPlugin<Info> {
static info = info;
constructor(private jsPsych: JsPsych) {}
trial(display_element: HTMLElement, trial: TrialType<Info>) {
var html = `
<div id='webgazer-calibrate-container' style='position: relative; width:100vw; height:100vh'>
</div>`;
display_element.innerHTML = html;
var wg_container = display_element.querySelector("#webgazer-calibrate-container");
var reps_completed = 0;
var points_completed = -1;
var cal_points = null;
const next_calibration_round = () => {
if (trial.randomize_calibration_order) {
cal_points = this.jsPsych.randomization.shuffle(trial.calibration_points);
} else {
cal_points = trial.calibration_points;
}
points_completed = -1;
next_calibration_point();
};
const calibrate = () => {
this.jsPsych.extensions["webgazer"].resume();
if (trial.calibration_mode == "click") {
this.jsPsych.extensions["webgazer"].startMouseCalibration();
}
next_calibration_round();
};
const next_calibration_point = () => {
points_completed++;
if (points_completed == cal_points.length) {
reps_completed++;
if (reps_completed == trial.repetitions_per_point) {
calibration_done();
} else {
next_calibration_round();
}
} else {
var pt = cal_points[points_completed];
calibration_display_gaze_only(pt);
}
};
const calibration_display_gaze_only = (pt) => {
var pt_html = `<div id="calibration-point" style="width:${trial.point_size}px; height:${trial.point_size}px; border-radius:${trial.point_size}px; border: 1px solid #000; background-color: #333; position: absolute; left:${pt[0]}%; top:${pt[1]}%;"></div>`;
wg_container.innerHTML = pt_html;
var pt_dom = wg_container.querySelector<HTMLElement>("#calibration-point");
if (trial.calibration_mode == "click") {
pt_dom.style.cursor = "pointer";
pt_dom.addEventListener("click", function () {
next_calibration_point();
});
}
if (trial.calibration_mode == "view") {
var br = pt_dom.getBoundingClientRect();
var x = br.left + br.width / 2;
var y = br.top + br.height / 2;
var pt_start_cal: number = performance.now() + trial.time_to_saccade;
var pt_finish: number = performance.now() + trial.time_to_saccade + trial.time_per_point;
const watch_dot = () => {
if (performance.now() > pt_start_cal) {
this.jsPsych.extensions["webgazer"].calibratePoint(x, y, "click");
}
if (performance.now() < pt_finish) {
requestAnimationFrame(watch_dot);
} else {
next_calibration_point();
}
};
requestAnimationFrame(watch_dot);
}
};
const calibration_done = () => {
if (trial.calibration_mode == "click") {
this.jsPsych.extensions["webgazer"].stopMouseCalibration();
}
wg_container.innerHTML = "";
end_trial();
};
// function to end trial when it is time
const end_trial = () => {
this.jsPsych.extensions["webgazer"].pause();
this.jsPsych.extensions["webgazer"].hidePredictions();
this.jsPsych.extensions["webgazer"].hideVideo();
// kill any remaining setTimeout handlers
this.jsPsych.pluginAPI.clearAllTimeouts();
// gather the data to store for the trial
var trial_data = {};
// clear the display
display_element.innerHTML = "";
// move on to the next trial
this.jsPsych.finishTrial(trial_data);
};
calibrate();
}
}
export default WebgazerCalibratePlugin;