Skip to content

Commit

Permalink
Merge pull request #97 from jspsych/html-keyboard-response-raf-update
Browse files Browse the repository at this point in the history
Update raf plugin to create more accurate display durations
  • Loading branch information
jodeleeuw authored Feb 2, 2024
2 parents a7db9c1 + a337d74 commit d7010d7
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 9 deletions.
5 changes: 5 additions & 0 deletions .changeset/young-pants-run.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@jspsych-contrib/plugin-html-keyboard-response-raf": patch
---

Adjusted how display durations are tracked to count frames rather than rely on timers
29 changes: 20 additions & 9 deletions packages/plugin-html-keyboard-response-raf/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,14 @@ const info = <const>{
pretty_name: "Response ends trial",
default: true,
},
/**
* FPS for requestAnimationFrame
*/
fps: {
type: ParameterType.INT,
pretty_name: "FPS",
default: 60,
},
},
};

Expand All @@ -67,8 +75,8 @@ class HtmlKeyboardResponseRafPlugin implements JsPsychPlugin<Info> {
static info = info;

private keyboardListener;
private hideStimulusTime: number = Infinity;
private endTrialTime: number = Infinity;
private hideStimulusFrameCount: number = Infinity;
private endTrialFrameCount: number = Infinity;
private stimulusIsHidden = false;
private currAnimationFrameHandler: number;

Expand All @@ -88,6 +96,8 @@ class HtmlKeyboardResponseRafPlugin implements JsPsychPlugin<Info> {
key: null,
};

var frame_counter = 0;

// draw
this.currAnimationFrameHandler = requestAnimationFrame(() => {
const initialDisplayTime = performance.now();
Expand All @@ -107,28 +117,29 @@ class HtmlKeyboardResponseRafPlugin implements JsPsychPlugin<Info> {

// hide stimulus if stimulus_duration is set
if (trial.stimulus_duration !== null) {
this.hideStimulusTime = initialDisplayTime + trial.stimulus_duration;
this.hideStimulusFrameCount = Math.round(trial.stimulus_duration / (1000 / trial.fps));
}

// end trial if trial_duration is set
if (trial.trial_duration !== null) {
this.endTrialTime = initialDisplayTime + trial.trial_duration;
this.endTrialFrameCount = Math.round(trial.trial_duration / (1000 / trial.fps));
}

console.log(performance.now());
this.currAnimationFrameHandler = requestAnimationFrame(checkForEnd);
});

const checkForEnd = () => {
const currTime = performance.now();
if (currTime >= this.hideStimulusTime && !this.stimulusIsHidden) {
frame_counter++;
if (frame_counter >= this.hideStimulusFrameCount && !this.stimulusIsHidden) {
this.stimulusIsHidden = true;
display_element.querySelector<HTMLElement>(
"#jspsych-html-keyboard-response-stimulus"
).style.visibility = "hidden";
console.log(currTime - this.hideStimulusTime);
console.log(frame_counter, performance.now());
}
if (currTime >= this.endTrialTime) {
console.log(currTime - this.endTrialTime);
if (frame_counter >= this.endTrialFrameCount) {
console.log(frame_counter, performance.now());
end_trial();
} else {
this.currAnimationFrameHandler = requestAnimationFrame(checkForEnd);
Expand Down

0 comments on commit d7010d7

Please sign in to comment.