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