@jspsych/plugin-video-keyboard-response 1.1.3 → 2.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -1,328 +1,322 @@
1
1
  import { ParameterType } from 'jspsych';
2
2
 
3
+ var version = "2.1.0";
4
+
3
5
  const info = {
4
- name: "video-keyboard-response",
5
- parameters: {
6
- /** Array of the video file(s) to play. Video can be provided in multiple file formats for better cross-browser support. */
7
- stimulus: {
8
- type: ParameterType.VIDEO,
9
- pretty_name: "Video",
10
- default: undefined,
11
- array: true,
12
- },
13
- /** Array containing the key(s) the subject is allowed to press to respond to the stimulus. */
14
- choices: {
15
- type: ParameterType.KEYS,
16
- pretty_name: "Choices",
17
- default: "ALL_KEYS",
18
- },
19
- /** Any content here will be displayed below the stimulus. */
20
- prompt: {
21
- type: ParameterType.HTML_STRING,
22
- pretty_name: "Prompt",
23
- default: null,
24
- },
25
- /** The width of the video in pixels. */
26
- width: {
27
- type: ParameterType.INT,
28
- pretty_name: "Width",
29
- default: "",
30
- },
31
- /** The height of the video display in pixels. */
32
- height: {
33
- type: ParameterType.INT,
34
- pretty_name: "Height",
35
- default: "",
36
- },
37
- /** If true, the video will begin playing as soon as it has loaded. */
38
- autoplay: {
39
- type: ParameterType.BOOL,
40
- pretty_name: "Autoplay",
41
- default: true,
42
- },
43
- /** If true, the subject will be able to pause the video or move the playback to any point in the video. */
44
- controls: {
45
- type: ParameterType.BOOL,
46
- pretty_name: "Controls",
47
- default: false,
48
- },
49
- /** Time to start the clip. If null (default), video will start at the beginning of the file. */
50
- start: {
51
- type: ParameterType.FLOAT,
52
- pretty_name: "Start",
53
- default: null,
54
- },
55
- /** Time to stop the clip. If null (default), video will stop at the end of the file. */
56
- stop: {
57
- type: ParameterType.FLOAT,
58
- pretty_name: "Stop",
59
- default: null,
60
- },
61
- /** The playback rate of the video. 1 is normal, <1 is slower, >1 is faster. */
62
- rate: {
63
- type: ParameterType.FLOAT,
64
- pretty_name: "Rate",
65
- default: 1,
66
- },
67
- /** If true, the trial will end immediately after the video finishes playing. */
68
- trial_ends_after_video: {
69
- type: ParameterType.BOOL,
70
- pretty_name: "End trial after video finishes",
71
- default: false,
72
- },
73
- /** How long to show trial before it ends. */
74
- trial_duration: {
75
- type: ParameterType.INT,
76
- pretty_name: "Trial duration",
77
- default: null,
78
- },
79
- /** If true, the trial will end when subject makes a response. */
80
- response_ends_trial: {
81
- type: ParameterType.BOOL,
82
- pretty_name: "Response ends trial",
83
- default: true,
84
- },
85
- /** If true, then responses are allowed while the video is playing. If false, then the video must finish playing before a response is accepted. */
86
- response_allowed_while_playing: {
87
- type: ParameterType.BOOL,
88
- pretty_name: "Response allowed while playing",
89
- default: true,
90
- },
6
+ name: "video-keyboard-response",
7
+ version,
8
+ parameters: {
9
+ /** Array of the video file(s) to play. Video can be provided in multiple file formats for better cross-browser support. */
10
+ stimulus: {
11
+ type: ParameterType.VIDEO,
12
+ pretty_name: "Video",
13
+ default: void 0,
14
+ array: true
15
+ },
16
+ /** Array containing the key(s) the subject is allowed to press to respond to the stimulus. */
17
+ choices: {
18
+ type: ParameterType.KEYS,
19
+ pretty_name: "Choices",
20
+ default: "ALL_KEYS"
21
+ },
22
+ /** Any content here will be displayed below the stimulus. */
23
+ prompt: {
24
+ type: ParameterType.HTML_STRING,
25
+ pretty_name: "Prompt",
26
+ default: null
27
+ },
28
+ /** The width of the video in pixels. */
29
+ width: {
30
+ type: ParameterType.INT,
31
+ pretty_name: "Width",
32
+ default: ""
33
+ },
34
+ /** The height of the video display in pixels. */
35
+ height: {
36
+ type: ParameterType.INT,
37
+ pretty_name: "Height",
38
+ default: ""
39
+ },
40
+ /** If true, the video will begin playing as soon as it has loaded. */
41
+ autoplay: {
42
+ type: ParameterType.BOOL,
43
+ pretty_name: "Autoplay",
44
+ default: true
45
+ },
46
+ /** If true, the subject will be able to pause the video or move the playback to any point in the video. */
47
+ controls: {
48
+ type: ParameterType.BOOL,
49
+ pretty_name: "Controls",
50
+ default: false
51
+ },
52
+ /** Time to start the clip. If null (default), video will start at the beginning of the file. */
53
+ start: {
54
+ type: ParameterType.FLOAT,
55
+ pretty_name: "Start",
56
+ default: null
57
+ },
58
+ /** Time to stop the clip. If null (default), video will stop at the end of the file. */
59
+ stop: {
60
+ type: ParameterType.FLOAT,
61
+ pretty_name: "Stop",
62
+ default: null
63
+ },
64
+ /** The playback rate of the video. 1 is normal, <1 is slower, >1 is faster. */
65
+ rate: {
66
+ type: ParameterType.FLOAT,
67
+ pretty_name: "Rate",
68
+ default: 1
69
+ },
70
+ /** If true, the trial will end immediately after the video finishes playing. */
71
+ trial_ends_after_video: {
72
+ type: ParameterType.BOOL,
73
+ pretty_name: "End trial after video finishes",
74
+ default: false
75
+ },
76
+ /** How long to show trial before it ends. */
77
+ trial_duration: {
78
+ type: ParameterType.INT,
79
+ pretty_name: "Trial duration",
80
+ default: null
91
81
  },
82
+ /** If true, the trial will end when subject makes a response. */
83
+ response_ends_trial: {
84
+ type: ParameterType.BOOL,
85
+ pretty_name: "Response ends trial",
86
+ default: true
87
+ },
88
+ /** If true, then responses are allowed while the video is playing. If false, then the video must finish playing before a response is accepted. */
89
+ response_allowed_while_playing: {
90
+ type: ParameterType.BOOL,
91
+ pretty_name: "Response allowed while playing",
92
+ default: true
93
+ }
94
+ },
95
+ data: {
96
+ /** Indicates which key the participant pressed. */
97
+ response: {
98
+ type: ParameterType.STRING
99
+ },
100
+ /** The response time in milliseconds for the participant to make a response. The time is measured from when the
101
+ * stimulus first appears on the screen until the participant's response.
102
+ * */
103
+ rt: {
104
+ type: ParameterType.INT
105
+ },
106
+ /** The `stimulus` array. This will be encoded as a JSON string when data is saved using the `.json()` or `.csv()` functions. */
107
+ stimulus: {
108
+ type: ParameterType.STRING,
109
+ array: true
110
+ }
111
+ },
112
+ // prettier-ignore
113
+ citations: {
114
+ "apa": "de Leeuw, J. R., Gilbert, R. A., & Luchterhandt, B. (2023). jsPsych: Enabling an Open-Source Collaborative Ecosystem of Behavioral Experiments. Journal of Open Source Software, 8(85), 5351. https://doi.org/10.21105/joss.05351 ",
115
+ "bibtex": '@article{Leeuw2023jsPsych, author = {de Leeuw, Joshua R. and Gilbert, Rebecca A. and Luchterhandt, Bj{\\" o}rn}, journal = {Journal of Open Source Software}, doi = {10.21105/joss.05351}, issn = {2475-9066}, number = {85}, year = {2023}, month = {may 11}, pages = {5351}, publisher = {Open Journals}, title = {jsPsych: Enabling an {Open}-{Source} {Collaborative} {Ecosystem} of {Behavioral} {Experiments}}, url = {https://joss.theoj.org/papers/10.21105/joss.05351}, volume = {8}, } '
116
+ }
92
117
  };
93
- /**
94
- * **video-keyboard-response**
95
- *
96
- * jsPsych plugin for playing a video file and getting a keyboard response
97
- *
98
- * @author Josh de Leeuw
99
- * @see {@link https://www.jspsych.org/plugins/jspsych-video-keyboard-response/ video-keyboard-response plugin documentation on jspsych.org}
100
- */
101
118
  class VideoKeyboardResponsePlugin {
102
- constructor(jsPsych) {
103
- this.jsPsych = jsPsych;
104
- }
105
- trial(display_element, trial) {
106
- // catch mistake where stimuli are not an array
107
- if (!Array.isArray(trial.stimulus)) {
108
- throw new Error(`
119
+ constructor(jsPsych) {
120
+ this.jsPsych = jsPsych;
121
+ }
122
+ static {
123
+ this.info = info;
124
+ }
125
+ trial(display_element, trial) {
126
+ if (!Array.isArray(trial.stimulus)) {
127
+ throw new Error(`
109
128
  The stimulus property for the video-keyboard-response plugin must be an array
110
129
  of files. See https://www.jspsych.org/latest/plugins/video-keyboard-response/#parameters
111
130
  `);
131
+ }
132
+ var video_html = "<div>";
133
+ video_html += '<video id="jspsych-video-keyboard-response-stimulus"';
134
+ if (trial.width) {
135
+ video_html += ' width="' + trial.width + '"';
136
+ }
137
+ if (trial.height) {
138
+ video_html += ' height="' + trial.height + '"';
139
+ }
140
+ if (trial.autoplay && trial.start == null) {
141
+ video_html += " autoplay ";
142
+ }
143
+ if (trial.controls) {
144
+ video_html += " controls ";
145
+ }
146
+ if (trial.start !== null) {
147
+ video_html += ' style="visibility: hidden;"';
148
+ }
149
+ video_html += ">";
150
+ var video_preload_blob = this.jsPsych.pluginAPI.getVideoBuffer(trial.stimulus[0]);
151
+ if (!video_preload_blob) {
152
+ for (var i = 0; i < trial.stimulus.length; i++) {
153
+ var file_name = trial.stimulus[i];
154
+ if (file_name.indexOf("?") > -1) {
155
+ file_name = file_name.substring(0, file_name.indexOf("?"));
112
156
  }
113
- // setup stimulus
114
- var video_html = "<div>";
115
- video_html += '<video id="jspsych-video-keyboard-response-stimulus"';
116
- if (trial.width) {
117
- video_html += ' width="' + trial.width + '"';
118
- }
119
- if (trial.height) {
120
- video_html += ' height="' + trial.height + '"';
121
- }
122
- if (trial.autoplay && trial.start == null) {
123
- // if autoplay is true and the start time is specified, then the video will start automatically
124
- // via the play() method, rather than the autoplay attribute, to prevent showing the first frame
125
- video_html += " autoplay ";
126
- }
127
- if (trial.controls) {
128
- video_html += " controls ";
129
- }
130
- if (trial.start !== null) {
131
- // hide video element when page loads if the start time is specified,
132
- // to prevent the video element from showing the first frame
133
- video_html += ' style="visibility: hidden;"';
134
- }
135
- video_html += ">";
136
- var video_preload_blob = this.jsPsych.pluginAPI.getVideoBuffer(trial.stimulus[0]);
137
- if (!video_preload_blob) {
138
- for (var i = 0; i < trial.stimulus.length; i++) {
139
- var file_name = trial.stimulus[i];
140
- if (file_name.indexOf("?") > -1) {
141
- file_name = file_name.substring(0, file_name.indexOf("?"));
142
- }
143
- var type = file_name.substr(file_name.lastIndexOf(".") + 1);
144
- type = type.toLowerCase();
145
- if (type == "mov") {
146
- console.warn("Warning: video-keyboard-response plugin does not reliably support .mov files.");
147
- }
148
- video_html += '<source src="' + file_name + '" type="video/' + type + '">';
149
- }
150
- }
151
- video_html += "</video>";
152
- video_html += "</div>";
153
- // add prompt if there is one
154
- if (trial.prompt !== null) {
155
- video_html += trial.prompt;
156
- }
157
- display_element.innerHTML = video_html;
158
- var video_element = display_element.querySelector("#jspsych-video-keyboard-response-stimulus");
159
- if (video_preload_blob) {
160
- video_element.src = video_preload_blob;
161
- }
162
- video_element.onended = () => {
163
- if (trial.trial_ends_after_video) {
164
- end_trial();
165
- }
166
- if (trial.response_allowed_while_playing == false && !trial.trial_ends_after_video) {
167
- // start keyboard listener
168
- this.jsPsych.pluginAPI.getKeyboardResponse({
169
- callback_function: after_response,
170
- valid_responses: trial.choices,
171
- rt_method: "performance",
172
- persist: false,
173
- allow_held_key: false,
174
- });
175
- }
176
- };
177
- video_element.playbackRate = trial.rate;
178
- // if video start time is specified, hide the video and set the starting time
179
- // before showing and playing, so that the video doesn't automatically show the first frame
180
- if (trial.start !== null) {
181
- video_element.pause();
182
- video_element.onseeked = () => {
183
- video_element.style.visibility = "visible";
184
- video_element.muted = false;
185
- if (trial.autoplay) {
186
- video_element.play();
187
- }
188
- else {
189
- video_element.pause();
190
- }
191
- video_element.onseeked = () => { };
192
- };
193
- video_element.onplaying = () => {
194
- video_element.currentTime = trial.start;
195
- video_element.onplaying = () => { };
196
- };
197
- // fix for iOS/MacOS browsers: videos aren't seekable until they start playing, so need to hide/mute, play,
198
- // change current time, then show/unmute
199
- video_element.muted = true;
200
- video_element.play();
157
+ var type = file_name.substr(file_name.lastIndexOf(".") + 1);
158
+ type = type.toLowerCase();
159
+ if (type == "mov") {
160
+ console.warn(
161
+ "Warning: video-keyboard-response plugin does not reliably support .mov files."
162
+ );
201
163
  }
202
- let stopped = false;
203
- if (trial.stop !== null) {
204
- video_element.addEventListener("timeupdate", (e) => {
205
- var currenttime = video_element.currentTime;
206
- if (currenttime >= trial.stop) {
207
- if (!trial.response_allowed_while_playing) {
208
- this.jsPsych.pluginAPI.getKeyboardResponse({
209
- callback_function: after_response,
210
- valid_responses: trial.choices,
211
- rt_method: "performance",
212
- persist: false,
213
- allow_held_key: false,
214
- });
215
- }
216
- video_element.pause();
217
- if (trial.trial_ends_after_video && !stopped) {
218
- // this is to prevent end_trial from being called twice, because the timeupdate event
219
- // can fire in quick succession
220
- stopped = true;
221
- end_trial();
222
- }
223
- }
224
- });
164
+ video_html += '<source src="' + file_name + '" type="video/' + type + '">';
165
+ }
166
+ }
167
+ video_html += "</video>";
168
+ video_html += "</div>";
169
+ if (trial.prompt !== null) {
170
+ video_html += trial.prompt;
171
+ }
172
+ display_element.innerHTML = video_html;
173
+ var video_element = display_element.querySelector(
174
+ "#jspsych-video-keyboard-response-stimulus"
175
+ );
176
+ if (video_preload_blob) {
177
+ video_element.src = video_preload_blob;
178
+ }
179
+ video_element.onended = () => {
180
+ if (trial.trial_ends_after_video) {
181
+ end_trial();
182
+ }
183
+ if (trial.response_allowed_while_playing == false && !trial.trial_ends_after_video) {
184
+ this.jsPsych.pluginAPI.getKeyboardResponse({
185
+ callback_function: after_response,
186
+ valid_responses: trial.choices,
187
+ rt_method: "performance",
188
+ persist: false,
189
+ allow_held_key: false
190
+ });
191
+ }
192
+ };
193
+ video_element.playbackRate = trial.rate;
194
+ if (trial.start !== null) {
195
+ video_element.pause();
196
+ video_element.onseeked = () => {
197
+ video_element.style.visibility = "visible";
198
+ video_element.muted = false;
199
+ if (trial.autoplay) {
200
+ video_element.play();
201
+ } else {
202
+ video_element.pause();
225
203
  }
226
- // store response
227
- var response = {
228
- rt: null,
229
- key: null,
230
- };
231
- // function to end trial when it is time
232
- const end_trial = () => {
233
- // kill any remaining setTimeout handlers
234
- this.jsPsych.pluginAPI.clearAllTimeouts();
235
- // kill keyboard listeners
236
- this.jsPsych.pluginAPI.cancelAllKeyboardResponses();
237
- // stop the video file if it is playing
238
- // remove end event listeners if they exist
239
- display_element
240
- .querySelector("#jspsych-video-keyboard-response-stimulus")
241
- .pause();
242
- display_element.querySelector("#jspsych-video-keyboard-response-stimulus").onended = () => { };
243
- // gather the data to store for the trial
244
- var trial_data = {
245
- rt: response.rt,
246
- stimulus: trial.stimulus,
247
- response: response.key,
248
- };
249
- // clear the display
250
- display_element.innerHTML = "";
251
- // move on to the next trial
252
- this.jsPsych.finishTrial(trial_data);
204
+ video_element.onseeked = () => {
253
205
  };
254
- // function to handle responses by the subject
255
- var after_response = (info) => {
256
- // after a valid response, the stimulus will have the CSS class 'responded'
257
- // which can be used to provide visual feedback that a response was recorded
258
- display_element.querySelector("#jspsych-video-keyboard-response-stimulus").className +=
259
- " responded";
260
- // only record the first response
261
- if (response.key == null) {
262
- response = info;
263
- }
264
- if (trial.response_ends_trial) {
265
- end_trial();
266
- }
206
+ };
207
+ video_element.onplaying = () => {
208
+ video_element.currentTime = trial.start;
209
+ video_element.onplaying = () => {
267
210
  };
268
- // start the response listener
269
- if (trial.choices != "NO_KEYS" && trial.response_allowed_while_playing) {
211
+ };
212
+ video_element.muted = true;
213
+ video_element.play();
214
+ }
215
+ let stopped = false;
216
+ if (trial.stop !== null) {
217
+ video_element.addEventListener("timeupdate", (e) => {
218
+ var currenttime = video_element.currentTime;
219
+ if (currenttime >= trial.stop) {
220
+ if (!trial.response_allowed_while_playing) {
270
221
  this.jsPsych.pluginAPI.getKeyboardResponse({
271
- callback_function: after_response,
272
- valid_responses: trial.choices,
273
- rt_method: "performance",
274
- persist: false,
275
- allow_held_key: false,
222
+ callback_function: after_response,
223
+ valid_responses: trial.choices,
224
+ rt_method: "performance",
225
+ persist: false,
226
+ allow_held_key: false
276
227
  });
228
+ }
229
+ video_element.pause();
230
+ if (trial.trial_ends_after_video && !stopped) {
231
+ stopped = true;
232
+ end_trial();
233
+ }
277
234
  }
278
- // end trial if time limit is set
279
- if (trial.trial_duration !== null) {
280
- this.jsPsych.pluginAPI.setTimeout(end_trial, trial.trial_duration);
281
- }
235
+ });
282
236
  }
283
- simulate(trial, simulation_mode, simulation_options, load_callback) {
284
- if (simulation_mode == "data-only") {
285
- load_callback();
286
- this.simulate_data_only(trial, simulation_options);
287
- }
288
- if (simulation_mode == "visual") {
289
- this.simulate_visual(trial, simulation_options, load_callback);
290
- }
237
+ var response = {
238
+ rt: null,
239
+ key: null
240
+ };
241
+ const end_trial = () => {
242
+ this.jsPsych.pluginAPI.cancelAllKeyboardResponses();
243
+ display_element.querySelector("#jspsych-video-keyboard-response-stimulus").pause();
244
+ display_element.querySelector(
245
+ "#jspsych-video-keyboard-response-stimulus"
246
+ ).onended = () => {
247
+ };
248
+ var trial_data = {
249
+ rt: response.rt,
250
+ stimulus: trial.stimulus,
251
+ response: response.key
252
+ };
253
+ this.jsPsych.finishTrial(trial_data);
254
+ };
255
+ var after_response = (info2) => {
256
+ display_element.querySelector("#jspsych-video-keyboard-response-stimulus").className += " responded";
257
+ if (response.key == null) {
258
+ response = info2;
259
+ }
260
+ if (trial.response_ends_trial) {
261
+ end_trial();
262
+ }
263
+ };
264
+ if (trial.choices != "NO_KEYS" && trial.response_allowed_while_playing) {
265
+ this.jsPsych.pluginAPI.getKeyboardResponse({
266
+ callback_function: after_response,
267
+ valid_responses: trial.choices,
268
+ rt_method: "performance",
269
+ persist: false,
270
+ allow_held_key: false
271
+ });
291
272
  }
292
- simulate_data_only(trial, simulation_options) {
293
- const data = this.create_simulation_data(trial, simulation_options);
294
- this.jsPsych.finishTrial(data);
273
+ if (trial.trial_duration !== null) {
274
+ this.jsPsych.pluginAPI.setTimeout(end_trial, trial.trial_duration);
295
275
  }
296
- simulate_visual(trial, simulation_options, load_callback) {
297
- const data = this.create_simulation_data(trial, simulation_options);
298
- const display_element = this.jsPsych.getDisplayElement();
299
- this.trial(display_element, trial);
300
- load_callback();
301
- const video_element = display_element.querySelector("#jspsych-video-button-response-stimulus");
302
- const respond = () => {
303
- if (data.rt !== null) {
304
- this.jsPsych.pluginAPI.pressKey(data.response, data.rt);
305
- }
306
- };
307
- if (!trial.response_allowed_while_playing) {
308
- video_element.addEventListener("ended", respond);
309
- }
310
- else {
311
- respond();
312
- }
276
+ }
277
+ simulate(trial, simulation_mode, simulation_options, load_callback) {
278
+ if (simulation_mode == "data-only") {
279
+ load_callback();
280
+ this.simulate_data_only(trial, simulation_options);
313
281
  }
314
- create_simulation_data(trial, simulation_options) {
315
- const default_data = {
316
- stimulus: trial.stimulus,
317
- rt: this.jsPsych.randomization.sampleExGaussian(500, 50, 1 / 150, true),
318
- response: this.jsPsych.pluginAPI.getValidKey(trial.choices),
319
- };
320
- const data = this.jsPsych.pluginAPI.mergeSimulationData(default_data, simulation_options);
321
- this.jsPsych.pluginAPI.ensureSimulationDataConsistency(trial, data);
322
- return data;
282
+ if (simulation_mode == "visual") {
283
+ this.simulate_visual(trial, simulation_options, load_callback);
284
+ }
285
+ }
286
+ simulate_data_only(trial, simulation_options) {
287
+ const data = this.create_simulation_data(trial, simulation_options);
288
+ this.jsPsych.finishTrial(data);
289
+ }
290
+ simulate_visual(trial, simulation_options, load_callback) {
291
+ const data = this.create_simulation_data(trial, simulation_options);
292
+ const display_element = this.jsPsych.getDisplayElement();
293
+ this.trial(display_element, trial);
294
+ load_callback();
295
+ const video_element = display_element.querySelector(
296
+ "#jspsych-video-button-response-stimulus"
297
+ );
298
+ const respond = () => {
299
+ if (data.rt !== null) {
300
+ this.jsPsych.pluginAPI.pressKey(data.response, data.rt);
301
+ }
302
+ };
303
+ if (!trial.response_allowed_while_playing) {
304
+ video_element.addEventListener("ended", respond);
305
+ } else {
306
+ respond();
323
307
  }
308
+ }
309
+ create_simulation_data(trial, simulation_options) {
310
+ const default_data = {
311
+ stimulus: trial.stimulus,
312
+ rt: this.jsPsych.randomization.sampleExGaussian(500, 50, 1 / 150, true),
313
+ response: this.jsPsych.pluginAPI.getValidKey(trial.choices)
314
+ };
315
+ const data = this.jsPsych.pluginAPI.mergeSimulationData(default_data, simulation_options);
316
+ this.jsPsych.pluginAPI.ensureSimulationDataConsistency(trial, data);
317
+ return data;
318
+ }
324
319
  }
325
- VideoKeyboardResponsePlugin.info = info;
326
320
 
327
321
  export { VideoKeyboardResponsePlugin as default };
328
322
  //# sourceMappingURL=index.js.map