@jspsych/plugin-image-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 +277 -277
- package/dist/index.browser.min.js +2 -2
- package/dist/index.browser.min.js.map +1 -1
- package/dist/index.cjs +276 -276
- package/dist/index.d.ts +156 -156
- package/dist/index.js +276 -276
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -27,9 +27,15 @@ See the [contributing to jsPsych](https://www.jspsych.org/latest/developers/cont
|
|
|
27
27
|
|
|
28
28
|
## Citation
|
|
29
29
|
|
|
30
|
-
If you use this library in academic work,
|
|
30
|
+
If you use this library in academic work, the preferred citation is:
|
|
31
31
|
|
|
32
|
-
de Leeuw, J.R. (
|
|
32
|
+
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://joss.theoj.org/papers/10.21105/joss.05351](https://joss.theoj.org/papers/10.21105/joss.05351).
|
|
33
|
+
|
|
34
|
+
This paper is an updated description of jsPsych and includes all current core team members. It replaces the earlier paper that described jsPsych:
|
|
35
|
+
|
|
36
|
+
de Leeuw, J.R. (2015). jsPsych: A JavaScript library for creating behavioral experiments in a Web browser. *Behavior Research Methods*, _47_(1), 1-12. doi:[10.3758/s13428-014-0458-y](http://link.springer.com/article/10.3758%2Fs13428-014-0458-y)
|
|
37
|
+
|
|
38
|
+
Citations help us demonstrate that this library is used and valued, which allows us to continue working on it.
|
|
33
39
|
|
|
34
40
|
## Contributors
|
|
35
41
|
|
package/dist/index.browser.js
CHANGED
|
@@ -1,285 +1,285 @@
|
|
|
1
1
|
var jsPsychImageKeyboardResponse = (function (jspsych) {
|
|
2
2
|
'use strict';
|
|
3
3
|
|
|
4
|
-
const info = {
|
|
5
|
-
name: "image-keyboard-response",
|
|
6
|
-
parameters: {
|
|
7
|
-
/** The image to be displayed */
|
|
8
|
-
stimulus: {
|
|
9
|
-
type: jspsych.ParameterType.IMAGE,
|
|
10
|
-
pretty_name: "Stimulus",
|
|
11
|
-
default: undefined,
|
|
12
|
-
},
|
|
13
|
-
/** Set the image height in pixels */
|
|
14
|
-
stimulus_height: {
|
|
15
|
-
type: jspsych.ParameterType.INT,
|
|
16
|
-
pretty_name: "Image height",
|
|
17
|
-
default: null,
|
|
18
|
-
},
|
|
19
|
-
/** Set the image width in pixels */
|
|
20
|
-
stimulus_width: {
|
|
21
|
-
type: jspsych.ParameterType.INT,
|
|
22
|
-
pretty_name: "Image width",
|
|
23
|
-
default: null,
|
|
24
|
-
},
|
|
25
|
-
/** Maintain the aspect ratio after setting width or height */
|
|
26
|
-
maintain_aspect_ratio: {
|
|
27
|
-
type: jspsych.ParameterType.BOOL,
|
|
28
|
-
pretty_name: "Maintain aspect ratio",
|
|
29
|
-
default: true,
|
|
30
|
-
},
|
|
31
|
-
/** Array containing the key(s) the subject is allowed to press to respond to the stimulus. */
|
|
32
|
-
choices: {
|
|
33
|
-
type: jspsych.ParameterType.KEYS,
|
|
34
|
-
pretty_name: "Choices",
|
|
35
|
-
default: "ALL_KEYS",
|
|
36
|
-
},
|
|
37
|
-
/** Any content here will be displayed below the stimulus. */
|
|
38
|
-
prompt: {
|
|
39
|
-
type: jspsych.ParameterType.HTML_STRING,
|
|
40
|
-
pretty_name: "Prompt",
|
|
41
|
-
default: null,
|
|
42
|
-
},
|
|
43
|
-
/** How long to show the stimulus. */
|
|
44
|
-
stimulus_duration: {
|
|
45
|
-
type: jspsych.ParameterType.INT,
|
|
46
|
-
pretty_name: "Stimulus duration",
|
|
47
|
-
default: null,
|
|
48
|
-
},
|
|
49
|
-
/** How long to show trial before it ends */
|
|
50
|
-
trial_duration: {
|
|
51
|
-
type: jspsych.ParameterType.INT,
|
|
52
|
-
pretty_name: "Trial duration",
|
|
53
|
-
default: null,
|
|
54
|
-
},
|
|
55
|
-
/** If true, trial will end when subject makes a response. */
|
|
56
|
-
response_ends_trial: {
|
|
57
|
-
type: jspsych.ParameterType.BOOL,
|
|
58
|
-
pretty_name: "Response ends trial",
|
|
59
|
-
default: true,
|
|
60
|
-
},
|
|
61
|
-
/**
|
|
62
|
-
* If true, the image will be drawn onto a canvas element (prevents blank screen between consecutive images in some browsers).
|
|
63
|
-
* If false, the image will be shown via an img element.
|
|
64
|
-
*/
|
|
65
|
-
render_on_canvas: {
|
|
66
|
-
type: jspsych.ParameterType.BOOL,
|
|
67
|
-
pretty_name: "Render on canvas",
|
|
68
|
-
default: true,
|
|
69
|
-
},
|
|
70
|
-
},
|
|
71
|
-
};
|
|
72
|
-
/**
|
|
73
|
-
* **image-keyboard-response**
|
|
74
|
-
*
|
|
75
|
-
* jsPsych plugin for displaying an image stimulus and getting a keyboard response
|
|
76
|
-
*
|
|
77
|
-
* @author Josh de Leeuw
|
|
78
|
-
* @see {@link https://www.jspsych.org/plugins/jspsych-image-keyboard-response/ image-keyboard-response plugin documentation on jspsych.org}
|
|
79
|
-
*/
|
|
80
|
-
class ImageKeyboardResponsePlugin {
|
|
81
|
-
constructor(jsPsych) {
|
|
82
|
-
this.jsPsych = jsPsych;
|
|
83
|
-
}
|
|
84
|
-
trial(display_element, trial) {
|
|
85
|
-
var height, width;
|
|
86
|
-
if (trial.render_on_canvas) {
|
|
87
|
-
var image_drawn = false;
|
|
88
|
-
// first clear the display element (because the render_on_canvas method appends to display_element instead of overwriting it with .innerHTML)
|
|
89
|
-
if (display_element.hasChildNodes()) {
|
|
90
|
-
// can't loop through child list because the list will be modified by .removeChild()
|
|
91
|
-
while (display_element.firstChild) {
|
|
92
|
-
display_element.removeChild(display_element.firstChild);
|
|
93
|
-
}
|
|
94
|
-
}
|
|
95
|
-
// create canvas element and image
|
|
96
|
-
var canvas = document.createElement("canvas");
|
|
97
|
-
canvas.id = "jspsych-image-keyboard-response-stimulus";
|
|
98
|
-
canvas.style.margin = "0";
|
|
99
|
-
canvas.style.padding = "0";
|
|
100
|
-
var ctx = canvas.getContext("2d");
|
|
101
|
-
var img = new Image();
|
|
102
|
-
img.onload = () => {
|
|
103
|
-
// if image wasn't preloaded, then it will need to be drawn whenever it finishes loading
|
|
104
|
-
if (!image_drawn) {
|
|
105
|
-
getHeightWidth(); // only possible to get width/height after image loads
|
|
106
|
-
ctx.drawImage(img, 0, 0, width, height);
|
|
107
|
-
}
|
|
108
|
-
};
|
|
109
|
-
img.src = trial.stimulus;
|
|
110
|
-
// get/set image height and width - this can only be done after image loads because uses image's naturalWidth/naturalHeight properties
|
|
111
|
-
const getHeightWidth = () => {
|
|
112
|
-
if (trial.stimulus_height !== null) {
|
|
113
|
-
height = trial.stimulus_height;
|
|
114
|
-
if (trial.stimulus_width == null && trial.maintain_aspect_ratio) {
|
|
115
|
-
width = img.naturalWidth * (trial.stimulus_height / img.naturalHeight);
|
|
116
|
-
}
|
|
117
|
-
}
|
|
118
|
-
else {
|
|
119
|
-
height = img.naturalHeight;
|
|
120
|
-
}
|
|
121
|
-
if (trial.stimulus_width !== null) {
|
|
122
|
-
width = trial.stimulus_width;
|
|
123
|
-
if (trial.stimulus_height == null && trial.maintain_aspect_ratio) {
|
|
124
|
-
height = img.naturalHeight * (trial.stimulus_width / img.naturalWidth);
|
|
125
|
-
}
|
|
126
|
-
}
|
|
127
|
-
else if (!(trial.stimulus_height !== null && trial.maintain_aspect_ratio)) {
|
|
128
|
-
// if stimulus width is null, only use the image's natural width if the width value wasn't set
|
|
129
|
-
// in the if statement above, based on a specified height and maintain_aspect_ratio = true
|
|
130
|
-
width = img.naturalWidth;
|
|
131
|
-
}
|
|
132
|
-
canvas.height = height;
|
|
133
|
-
canvas.width = width;
|
|
134
|
-
};
|
|
135
|
-
getHeightWidth(); // call now, in case image loads immediately (is cached)
|
|
136
|
-
// add canvas and draw image
|
|
137
|
-
display_element.insertBefore(canvas, null);
|
|
138
|
-
if (img.complete && Number.isFinite(width) && Number.isFinite(height)) {
|
|
139
|
-
// if image has loaded and width/height have been set, then draw it now
|
|
140
|
-
// (don't rely on img onload function to draw image when image is in the cache, because that causes a delay in the image presentation)
|
|
141
|
-
ctx.drawImage(img, 0, 0, width, height);
|
|
142
|
-
image_drawn = true;
|
|
143
|
-
}
|
|
144
|
-
// add prompt if there is one
|
|
145
|
-
if (trial.prompt !== null) {
|
|
146
|
-
display_element.insertAdjacentHTML("beforeend", trial.prompt);
|
|
147
|
-
}
|
|
148
|
-
}
|
|
149
|
-
else {
|
|
150
|
-
// display stimulus as an image element
|
|
151
|
-
var html = '<img src="' + trial.stimulus + '" id="jspsych-image-keyboard-response-stimulus">';
|
|
152
|
-
// add prompt
|
|
153
|
-
if (trial.prompt !== null) {
|
|
154
|
-
html += trial.prompt;
|
|
155
|
-
}
|
|
156
|
-
// update the page content
|
|
157
|
-
display_element.innerHTML = html;
|
|
158
|
-
// set image dimensions after image has loaded (so that we have access to naturalHeight/naturalWidth)
|
|
159
|
-
var img = display_element.querySelector("#jspsych-image-keyboard-response-stimulus");
|
|
160
|
-
if (trial.stimulus_height !== null) {
|
|
161
|
-
height = trial.stimulus_height;
|
|
162
|
-
if (trial.stimulus_width == null && trial.maintain_aspect_ratio) {
|
|
163
|
-
width = img.naturalWidth * (trial.stimulus_height / img.naturalHeight);
|
|
164
|
-
}
|
|
165
|
-
}
|
|
166
|
-
else {
|
|
167
|
-
height = img.naturalHeight;
|
|
168
|
-
}
|
|
169
|
-
if (trial.stimulus_width !== null) {
|
|
170
|
-
width = trial.stimulus_width;
|
|
171
|
-
if (trial.stimulus_height == null && trial.maintain_aspect_ratio) {
|
|
172
|
-
height = img.naturalHeight * (trial.stimulus_width / img.naturalWidth);
|
|
173
|
-
}
|
|
174
|
-
}
|
|
175
|
-
else if (!(trial.stimulus_height !== null && trial.maintain_aspect_ratio)) {
|
|
176
|
-
// if stimulus width is null, only use the image's natural width if the width value wasn't set
|
|
177
|
-
// in the if statement above, based on a specified height and maintain_aspect_ratio = true
|
|
178
|
-
width = img.naturalWidth;
|
|
179
|
-
}
|
|
180
|
-
img.style.height = height.toString() + "px";
|
|
181
|
-
img.style.width = width.toString() + "px";
|
|
182
|
-
}
|
|
183
|
-
// store response
|
|
184
|
-
var response = {
|
|
185
|
-
rt: null,
|
|
186
|
-
key: null,
|
|
187
|
-
};
|
|
188
|
-
// function to end trial when it is time
|
|
189
|
-
const end_trial = () => {
|
|
190
|
-
// kill any remaining setTimeout handlers
|
|
191
|
-
this.jsPsych.pluginAPI.clearAllTimeouts();
|
|
192
|
-
// kill keyboard listeners
|
|
193
|
-
if (typeof keyboardListener !== "undefined") {
|
|
194
|
-
this.jsPsych.pluginAPI.cancelKeyboardResponse(keyboardListener);
|
|
195
|
-
}
|
|
196
|
-
// gather the data to store for the trial
|
|
197
|
-
var trial_data = {
|
|
198
|
-
rt: response.rt,
|
|
199
|
-
stimulus: trial.stimulus,
|
|
200
|
-
response: response.key,
|
|
201
|
-
};
|
|
202
|
-
// clear the display
|
|
203
|
-
display_element.innerHTML = "";
|
|
204
|
-
// move on to the next trial
|
|
205
|
-
this.jsPsych.finishTrial(trial_data);
|
|
206
|
-
};
|
|
207
|
-
// function to handle responses by the subject
|
|
208
|
-
var after_response = (info) => {
|
|
209
|
-
// after a valid response, the stimulus will have the CSS class 'responded'
|
|
210
|
-
// which can be used to provide visual feedback that a response was recorded
|
|
211
|
-
display_element.querySelector("#jspsych-image-keyboard-response-stimulus").className +=
|
|
212
|
-
" responded";
|
|
213
|
-
// only record the first response
|
|
214
|
-
if (response.key == null) {
|
|
215
|
-
response = info;
|
|
216
|
-
}
|
|
217
|
-
if (trial.response_ends_trial) {
|
|
218
|
-
end_trial();
|
|
219
|
-
}
|
|
220
|
-
};
|
|
221
|
-
// start the response listener
|
|
222
|
-
if (trial.choices != "NO_KEYS") {
|
|
223
|
-
var keyboardListener = this.jsPsych.pluginAPI.getKeyboardResponse({
|
|
224
|
-
callback_function: after_response,
|
|
225
|
-
valid_responses: trial.choices,
|
|
226
|
-
rt_method: "performance",
|
|
227
|
-
persist: false,
|
|
228
|
-
allow_held_key: false,
|
|
229
|
-
});
|
|
230
|
-
}
|
|
231
|
-
// hide stimulus if stimulus_duration is set
|
|
232
|
-
if (trial.stimulus_duration !== null) {
|
|
233
|
-
this.jsPsych.pluginAPI.setTimeout(() => {
|
|
234
|
-
display_element.querySelector("#jspsych-image-keyboard-response-stimulus").style.visibility = "hidden";
|
|
235
|
-
}, trial.stimulus_duration);
|
|
236
|
-
}
|
|
237
|
-
// end trial if trial_duration is set
|
|
238
|
-
if (trial.trial_duration !== null) {
|
|
239
|
-
this.jsPsych.pluginAPI.setTimeout(() => {
|
|
240
|
-
end_trial();
|
|
241
|
-
}, trial.trial_duration);
|
|
242
|
-
}
|
|
243
|
-
else if (trial.response_ends_trial === false) {
|
|
244
|
-
console.warn("The experiment may be deadlocked. Try setting a trial duration or set response_ends_trial to true.");
|
|
245
|
-
}
|
|
246
|
-
}
|
|
247
|
-
simulate(trial, simulation_mode, simulation_options, load_callback) {
|
|
248
|
-
if (simulation_mode == "data-only") {
|
|
249
|
-
load_callback();
|
|
250
|
-
this.simulate_data_only(trial, simulation_options);
|
|
251
|
-
}
|
|
252
|
-
if (simulation_mode == "visual") {
|
|
253
|
-
this.simulate_visual(trial, simulation_options, load_callback);
|
|
254
|
-
}
|
|
255
|
-
}
|
|
256
|
-
simulate_data_only(trial, simulation_options) {
|
|
257
|
-
const data = this.create_simulation_data(trial, simulation_options);
|
|
258
|
-
this.jsPsych.finishTrial(data);
|
|
259
|
-
}
|
|
260
|
-
simulate_visual(trial, simulation_options, load_callback) {
|
|
261
|
-
const data = this.create_simulation_data(trial, simulation_options);
|
|
262
|
-
const display_element = this.jsPsych.getDisplayElement();
|
|
263
|
-
this.trial(display_element, trial);
|
|
264
|
-
load_callback();
|
|
265
|
-
if (data.rt !== null) {
|
|
266
|
-
this.jsPsych.pluginAPI.pressKey(data.response, data.rt);
|
|
267
|
-
}
|
|
268
|
-
}
|
|
269
|
-
create_simulation_data(trial, simulation_options) {
|
|
270
|
-
const default_data = {
|
|
271
|
-
stimulus: trial.stimulus,
|
|
272
|
-
rt: this.jsPsych.randomization.sampleExGaussian(500, 50, 1 / 150, true),
|
|
273
|
-
response: this.jsPsych.pluginAPI.getValidKey(trial.choices),
|
|
274
|
-
};
|
|
275
|
-
const data = this.jsPsych.pluginAPI.mergeSimulationData(default_data, simulation_options);
|
|
276
|
-
this.jsPsych.pluginAPI.ensureSimulationDataConsistency(trial, data);
|
|
277
|
-
return data;
|
|
278
|
-
}
|
|
279
|
-
}
|
|
4
|
+
const info = {
|
|
5
|
+
name: "image-keyboard-response",
|
|
6
|
+
parameters: {
|
|
7
|
+
/** The image to be displayed */
|
|
8
|
+
stimulus: {
|
|
9
|
+
type: jspsych.ParameterType.IMAGE,
|
|
10
|
+
pretty_name: "Stimulus",
|
|
11
|
+
default: undefined,
|
|
12
|
+
},
|
|
13
|
+
/** Set the image height in pixels */
|
|
14
|
+
stimulus_height: {
|
|
15
|
+
type: jspsych.ParameterType.INT,
|
|
16
|
+
pretty_name: "Image height",
|
|
17
|
+
default: null,
|
|
18
|
+
},
|
|
19
|
+
/** Set the image width in pixels */
|
|
20
|
+
stimulus_width: {
|
|
21
|
+
type: jspsych.ParameterType.INT,
|
|
22
|
+
pretty_name: "Image width",
|
|
23
|
+
default: null,
|
|
24
|
+
},
|
|
25
|
+
/** Maintain the aspect ratio after setting width or height */
|
|
26
|
+
maintain_aspect_ratio: {
|
|
27
|
+
type: jspsych.ParameterType.BOOL,
|
|
28
|
+
pretty_name: "Maintain aspect ratio",
|
|
29
|
+
default: true,
|
|
30
|
+
},
|
|
31
|
+
/** Array containing the key(s) the subject is allowed to press to respond to the stimulus. */
|
|
32
|
+
choices: {
|
|
33
|
+
type: jspsych.ParameterType.KEYS,
|
|
34
|
+
pretty_name: "Choices",
|
|
35
|
+
default: "ALL_KEYS",
|
|
36
|
+
},
|
|
37
|
+
/** Any content here will be displayed below the stimulus. */
|
|
38
|
+
prompt: {
|
|
39
|
+
type: jspsych.ParameterType.HTML_STRING,
|
|
40
|
+
pretty_name: "Prompt",
|
|
41
|
+
default: null,
|
|
42
|
+
},
|
|
43
|
+
/** How long to show the stimulus. */
|
|
44
|
+
stimulus_duration: {
|
|
45
|
+
type: jspsych.ParameterType.INT,
|
|
46
|
+
pretty_name: "Stimulus duration",
|
|
47
|
+
default: null,
|
|
48
|
+
},
|
|
49
|
+
/** How long to show trial before it ends */
|
|
50
|
+
trial_duration: {
|
|
51
|
+
type: jspsych.ParameterType.INT,
|
|
52
|
+
pretty_name: "Trial duration",
|
|
53
|
+
default: null,
|
|
54
|
+
},
|
|
55
|
+
/** If true, trial will end when subject makes a response. */
|
|
56
|
+
response_ends_trial: {
|
|
57
|
+
type: jspsych.ParameterType.BOOL,
|
|
58
|
+
pretty_name: "Response ends trial",
|
|
59
|
+
default: true,
|
|
60
|
+
},
|
|
61
|
+
/**
|
|
62
|
+
* If true, the image will be drawn onto a canvas element (prevents blank screen between consecutive images in some browsers).
|
|
63
|
+
* If false, the image will be shown via an img element.
|
|
64
|
+
*/
|
|
65
|
+
render_on_canvas: {
|
|
66
|
+
type: jspsych.ParameterType.BOOL,
|
|
67
|
+
pretty_name: "Render on canvas",
|
|
68
|
+
default: true,
|
|
69
|
+
},
|
|
70
|
+
},
|
|
71
|
+
};
|
|
72
|
+
/**
|
|
73
|
+
* **image-keyboard-response**
|
|
74
|
+
*
|
|
75
|
+
* jsPsych plugin for displaying an image stimulus and getting a keyboard response
|
|
76
|
+
*
|
|
77
|
+
* @author Josh de Leeuw
|
|
78
|
+
* @see {@link https://www.jspsych.org/plugins/jspsych-image-keyboard-response/ image-keyboard-response plugin documentation on jspsych.org}
|
|
79
|
+
*/
|
|
80
|
+
class ImageKeyboardResponsePlugin {
|
|
81
|
+
constructor(jsPsych) {
|
|
82
|
+
this.jsPsych = jsPsych;
|
|
83
|
+
}
|
|
84
|
+
trial(display_element, trial) {
|
|
85
|
+
var height, width;
|
|
86
|
+
if (trial.render_on_canvas) {
|
|
87
|
+
var image_drawn = false;
|
|
88
|
+
// first clear the display element (because the render_on_canvas method appends to display_element instead of overwriting it with .innerHTML)
|
|
89
|
+
if (display_element.hasChildNodes()) {
|
|
90
|
+
// can't loop through child list because the list will be modified by .removeChild()
|
|
91
|
+
while (display_element.firstChild) {
|
|
92
|
+
display_element.removeChild(display_element.firstChild);
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
// create canvas element and image
|
|
96
|
+
var canvas = document.createElement("canvas");
|
|
97
|
+
canvas.id = "jspsych-image-keyboard-response-stimulus";
|
|
98
|
+
canvas.style.margin = "0";
|
|
99
|
+
canvas.style.padding = "0";
|
|
100
|
+
var ctx = canvas.getContext("2d");
|
|
101
|
+
var img = new Image();
|
|
102
|
+
img.onload = () => {
|
|
103
|
+
// if image wasn't preloaded, then it will need to be drawn whenever it finishes loading
|
|
104
|
+
if (!image_drawn) {
|
|
105
|
+
getHeightWidth(); // only possible to get width/height after image loads
|
|
106
|
+
ctx.drawImage(img, 0, 0, width, height);
|
|
107
|
+
}
|
|
108
|
+
};
|
|
109
|
+
img.src = trial.stimulus;
|
|
110
|
+
// get/set image height and width - this can only be done after image loads because uses image's naturalWidth/naturalHeight properties
|
|
111
|
+
const getHeightWidth = () => {
|
|
112
|
+
if (trial.stimulus_height !== null) {
|
|
113
|
+
height = trial.stimulus_height;
|
|
114
|
+
if (trial.stimulus_width == null && trial.maintain_aspect_ratio) {
|
|
115
|
+
width = img.naturalWidth * (trial.stimulus_height / img.naturalHeight);
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
else {
|
|
119
|
+
height = img.naturalHeight;
|
|
120
|
+
}
|
|
121
|
+
if (trial.stimulus_width !== null) {
|
|
122
|
+
width = trial.stimulus_width;
|
|
123
|
+
if (trial.stimulus_height == null && trial.maintain_aspect_ratio) {
|
|
124
|
+
height = img.naturalHeight * (trial.stimulus_width / img.naturalWidth);
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
else if (!(trial.stimulus_height !== null && trial.maintain_aspect_ratio)) {
|
|
128
|
+
// if stimulus width is null, only use the image's natural width if the width value wasn't set
|
|
129
|
+
// in the if statement above, based on a specified height and maintain_aspect_ratio = true
|
|
130
|
+
width = img.naturalWidth;
|
|
131
|
+
}
|
|
132
|
+
canvas.height = height;
|
|
133
|
+
canvas.width = width;
|
|
134
|
+
};
|
|
135
|
+
getHeightWidth(); // call now, in case image loads immediately (is cached)
|
|
136
|
+
// add canvas and draw image
|
|
137
|
+
display_element.insertBefore(canvas, null);
|
|
138
|
+
if (img.complete && Number.isFinite(width) && Number.isFinite(height)) {
|
|
139
|
+
// if image has loaded and width/height have been set, then draw it now
|
|
140
|
+
// (don't rely on img onload function to draw image when image is in the cache, because that causes a delay in the image presentation)
|
|
141
|
+
ctx.drawImage(img, 0, 0, width, height);
|
|
142
|
+
image_drawn = true;
|
|
143
|
+
}
|
|
144
|
+
// add prompt if there is one
|
|
145
|
+
if (trial.prompt !== null) {
|
|
146
|
+
display_element.insertAdjacentHTML("beforeend", trial.prompt);
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
else {
|
|
150
|
+
// display stimulus as an image element
|
|
151
|
+
var html = '<img src="' + trial.stimulus + '" id="jspsych-image-keyboard-response-stimulus">';
|
|
152
|
+
// add prompt
|
|
153
|
+
if (trial.prompt !== null) {
|
|
154
|
+
html += trial.prompt;
|
|
155
|
+
}
|
|
156
|
+
// update the page content
|
|
157
|
+
display_element.innerHTML = html;
|
|
158
|
+
// set image dimensions after image has loaded (so that we have access to naturalHeight/naturalWidth)
|
|
159
|
+
var img = display_element.querySelector("#jspsych-image-keyboard-response-stimulus");
|
|
160
|
+
if (trial.stimulus_height !== null) {
|
|
161
|
+
height = trial.stimulus_height;
|
|
162
|
+
if (trial.stimulus_width == null && trial.maintain_aspect_ratio) {
|
|
163
|
+
width = img.naturalWidth * (trial.stimulus_height / img.naturalHeight);
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
else {
|
|
167
|
+
height = img.naturalHeight;
|
|
168
|
+
}
|
|
169
|
+
if (trial.stimulus_width !== null) {
|
|
170
|
+
width = trial.stimulus_width;
|
|
171
|
+
if (trial.stimulus_height == null && trial.maintain_aspect_ratio) {
|
|
172
|
+
height = img.naturalHeight * (trial.stimulus_width / img.naturalWidth);
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
else if (!(trial.stimulus_height !== null && trial.maintain_aspect_ratio)) {
|
|
176
|
+
// if stimulus width is null, only use the image's natural width if the width value wasn't set
|
|
177
|
+
// in the if statement above, based on a specified height and maintain_aspect_ratio = true
|
|
178
|
+
width = img.naturalWidth;
|
|
179
|
+
}
|
|
180
|
+
img.style.height = height.toString() + "px";
|
|
181
|
+
img.style.width = width.toString() + "px";
|
|
182
|
+
}
|
|
183
|
+
// store response
|
|
184
|
+
var response = {
|
|
185
|
+
rt: null,
|
|
186
|
+
key: null,
|
|
187
|
+
};
|
|
188
|
+
// function to end trial when it is time
|
|
189
|
+
const end_trial = () => {
|
|
190
|
+
// kill any remaining setTimeout handlers
|
|
191
|
+
this.jsPsych.pluginAPI.clearAllTimeouts();
|
|
192
|
+
// kill keyboard listeners
|
|
193
|
+
if (typeof keyboardListener !== "undefined") {
|
|
194
|
+
this.jsPsych.pluginAPI.cancelKeyboardResponse(keyboardListener);
|
|
195
|
+
}
|
|
196
|
+
// gather the data to store for the trial
|
|
197
|
+
var trial_data = {
|
|
198
|
+
rt: response.rt,
|
|
199
|
+
stimulus: trial.stimulus,
|
|
200
|
+
response: response.key,
|
|
201
|
+
};
|
|
202
|
+
// clear the display
|
|
203
|
+
display_element.innerHTML = "";
|
|
204
|
+
// move on to the next trial
|
|
205
|
+
this.jsPsych.finishTrial(trial_data);
|
|
206
|
+
};
|
|
207
|
+
// function to handle responses by the subject
|
|
208
|
+
var after_response = (info) => {
|
|
209
|
+
// after a valid response, the stimulus will have the CSS class 'responded'
|
|
210
|
+
// which can be used to provide visual feedback that a response was recorded
|
|
211
|
+
display_element.querySelector("#jspsych-image-keyboard-response-stimulus").className +=
|
|
212
|
+
" responded";
|
|
213
|
+
// only record the first response
|
|
214
|
+
if (response.key == null) {
|
|
215
|
+
response = info;
|
|
216
|
+
}
|
|
217
|
+
if (trial.response_ends_trial) {
|
|
218
|
+
end_trial();
|
|
219
|
+
}
|
|
220
|
+
};
|
|
221
|
+
// start the response listener
|
|
222
|
+
if (trial.choices != "NO_KEYS") {
|
|
223
|
+
var keyboardListener = this.jsPsych.pluginAPI.getKeyboardResponse({
|
|
224
|
+
callback_function: after_response,
|
|
225
|
+
valid_responses: trial.choices,
|
|
226
|
+
rt_method: "performance",
|
|
227
|
+
persist: false,
|
|
228
|
+
allow_held_key: false,
|
|
229
|
+
});
|
|
230
|
+
}
|
|
231
|
+
// hide stimulus if stimulus_duration is set
|
|
232
|
+
if (trial.stimulus_duration !== null) {
|
|
233
|
+
this.jsPsych.pluginAPI.setTimeout(() => {
|
|
234
|
+
display_element.querySelector("#jspsych-image-keyboard-response-stimulus").style.visibility = "hidden";
|
|
235
|
+
}, trial.stimulus_duration);
|
|
236
|
+
}
|
|
237
|
+
// end trial if trial_duration is set
|
|
238
|
+
if (trial.trial_duration !== null) {
|
|
239
|
+
this.jsPsych.pluginAPI.setTimeout(() => {
|
|
240
|
+
end_trial();
|
|
241
|
+
}, trial.trial_duration);
|
|
242
|
+
}
|
|
243
|
+
else if (trial.response_ends_trial === false) {
|
|
244
|
+
console.warn("The experiment may be deadlocked. Try setting a trial duration or set response_ends_trial to true.");
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
simulate(trial, simulation_mode, simulation_options, load_callback) {
|
|
248
|
+
if (simulation_mode == "data-only") {
|
|
249
|
+
load_callback();
|
|
250
|
+
this.simulate_data_only(trial, simulation_options);
|
|
251
|
+
}
|
|
252
|
+
if (simulation_mode == "visual") {
|
|
253
|
+
this.simulate_visual(trial, simulation_options, load_callback);
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
simulate_data_only(trial, simulation_options) {
|
|
257
|
+
const data = this.create_simulation_data(trial, simulation_options);
|
|
258
|
+
this.jsPsych.finishTrial(data);
|
|
259
|
+
}
|
|
260
|
+
simulate_visual(trial, simulation_options, load_callback) {
|
|
261
|
+
const data = this.create_simulation_data(trial, simulation_options);
|
|
262
|
+
const display_element = this.jsPsych.getDisplayElement();
|
|
263
|
+
this.trial(display_element, trial);
|
|
264
|
+
load_callback();
|
|
265
|
+
if (data.rt !== null) {
|
|
266
|
+
this.jsPsych.pluginAPI.pressKey(data.response, data.rt);
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
create_simulation_data(trial, simulation_options) {
|
|
270
|
+
const default_data = {
|
|
271
|
+
stimulus: trial.stimulus,
|
|
272
|
+
rt: this.jsPsych.randomization.sampleExGaussian(500, 50, 1 / 150, true),
|
|
273
|
+
response: this.jsPsych.pluginAPI.getValidKey(trial.choices),
|
|
274
|
+
};
|
|
275
|
+
const data = this.jsPsych.pluginAPI.mergeSimulationData(default_data, simulation_options);
|
|
276
|
+
this.jsPsych.pluginAPI.ensureSimulationDataConsistency(trial, data);
|
|
277
|
+
return data;
|
|
278
|
+
}
|
|
279
|
+
}
|
|
280
280
|
ImageKeyboardResponsePlugin.info = info;
|
|
281
281
|
|
|
282
282
|
return ImageKeyboardResponsePlugin;
|
|
283
283
|
|
|
284
284
|
})(jsPsychModule);
|
|
285
|
-
//# sourceMappingURL=index.browser.js.map
|
|
285
|
+
//# sourceMappingURL=https://unpkg.com/@jspsych/plugin-image-keyboard-response@1.1.3/dist/index.browser.js.map
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
var jsPsychImageKeyboardResponse=function(t){"use strict";function e(t,e){for(var i=0;i<e.length;i++){var
|
|
2
|
-
//# sourceMappingURL=index.browser.min.js.map
|
|
1
|
+
var jsPsychImageKeyboardResponse=function(t){"use strict";function e(t,e){for(var i=0;i<e.length;i++){var a=e[i];a.enumerable=a.enumerable||!1,a.configurable=!0,"value"in a&&(a.writable=!0),Object.defineProperty(t,(s=a.key,r=void 0,"symbol"==typeof(r=function(t,e){if("object"!=typeof t||null===t)return t;var i=t[Symbol.toPrimitive];if(void 0!==i){var a=i.call(t,e||"default");if("object"!=typeof a)return a;throw new TypeError("@@toPrimitive must return a primitive value.")}return("string"===e?String:Number)(t)}(s,"string"))?r:String(r)),a)}var s,r}var i={name:"image-keyboard-response",parameters:{stimulus:{type:t.ParameterType.IMAGE,pretty_name:"Stimulus",default:void 0},stimulus_height:{type:t.ParameterType.INT,pretty_name:"Image height",default:null},stimulus_width:{type:t.ParameterType.INT,pretty_name:"Image width",default:null},maintain_aspect_ratio:{type:t.ParameterType.BOOL,pretty_name:"Maintain aspect ratio",default:!0},choices:{type:t.ParameterType.KEYS,pretty_name:"Choices",default:"ALL_KEYS"},prompt:{type:t.ParameterType.HTML_STRING,pretty_name:"Prompt",default:null},stimulus_duration:{type:t.ParameterType.INT,pretty_name:"Stimulus duration",default:null},trial_duration:{type:t.ParameterType.INT,pretty_name:"Trial duration",default:null},response_ends_trial:{type:t.ParameterType.BOOL,pretty_name:"Response ends trial",default:!0},render_on_canvas:{type:t.ParameterType.BOOL,pretty_name:"Render on canvas",default:!0}}},a=function(){function t(e){!function(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}(this,t),this.jsPsych=e}var i,a,s;return i=t,a=[{key:"trial",value:function(t,e){var i,a,s=this;if(e.render_on_canvas){var r=!1;if(t.hasChildNodes())for(;t.firstChild;)t.removeChild(t.firstChild);var n=document.createElement("canvas");n.id="jspsych-image-keyboard-response-stimulus",n.style.margin="0",n.style.padding="0";var u=n.getContext("2d");(m=new Image).onload=function(){r||(l(),u.drawImage(m,0,0,a,i))},m.src=e.stimulus;var l=function(){null!==e.stimulus_height?(i=e.stimulus_height,null==e.stimulus_width&&e.maintain_aspect_ratio&&(a=m.naturalWidth*(e.stimulus_height/m.naturalHeight))):i=m.naturalHeight,null!==e.stimulus_width?(a=e.stimulus_width,null==e.stimulus_height&&e.maintain_aspect_ratio&&(i=m.naturalHeight*(e.stimulus_width/m.naturalWidth))):null!==e.stimulus_height&&e.maintain_aspect_ratio||(a=m.naturalWidth),n.height=i,n.width=a};l(),t.insertBefore(n,null),m.complete&&Number.isFinite(a)&&Number.isFinite(i)&&(u.drawImage(m,0,0,a,i),r=!0),null!==e.prompt&&t.insertAdjacentHTML("beforeend",e.prompt)}else{var o='<img src="'+e.stimulus+'" id="jspsych-image-keyboard-response-stimulus">';null!==e.prompt&&(o+=e.prompt),t.innerHTML=o;var m=t.querySelector("#jspsych-image-keyboard-response-stimulus");null!==e.stimulus_height?(i=e.stimulus_height,null==e.stimulus_width&&e.maintain_aspect_ratio&&(a=m.naturalWidth*(e.stimulus_height/m.naturalHeight))):i=m.naturalHeight,null!==e.stimulus_width?(a=e.stimulus_width,null==e.stimulus_height&&e.maintain_aspect_ratio&&(i=m.naturalHeight*(e.stimulus_width/m.naturalWidth))):null!==e.stimulus_height&&e.maintain_aspect_ratio||(a=m.naturalWidth),m.style.height=i.toString()+"px",m.style.width=a.toString()+"px"}var h={rt:null,key:null},p=function(){s.jsPsych.pluginAPI.clearAllTimeouts(),void 0!==y&&s.jsPsych.pluginAPI.cancelKeyboardResponse(y);var i={rt:h.rt,stimulus:e.stimulus,response:h.key};t.innerHTML="",s.jsPsych.finishTrial(i)};if("NO_KEYS"!=e.choices)var y=this.jsPsych.pluginAPI.getKeyboardResponse({callback_function:function(i){t.querySelector("#jspsych-image-keyboard-response-stimulus").className+=" responded",null==h.key&&(h=i),e.response_ends_trial&&p()},valid_responses:e.choices,rt_method:"performance",persist:!1,allow_held_key:!1});null!==e.stimulus_duration&&this.jsPsych.pluginAPI.setTimeout((function(){t.querySelector("#jspsych-image-keyboard-response-stimulus").style.visibility="hidden"}),e.stimulus_duration),null!==e.trial_duration?this.jsPsych.pluginAPI.setTimeout((function(){p()}),e.trial_duration):!1===e.response_ends_trial&&console.warn("The experiment may be deadlocked. Try setting a trial duration or set response_ends_trial to true.")}},{key:"simulate",value:function(t,e,i,a){"data-only"==e&&(a(),this.simulate_data_only(t,i)),"visual"==e&&this.simulate_visual(t,i,a)}},{key:"simulate_data_only",value:function(t,e){var i=this.create_simulation_data(t,e);this.jsPsych.finishTrial(i)}},{key:"simulate_visual",value:function(t,e,i){var a=this.create_simulation_data(t,e),s=this.jsPsych.getDisplayElement();this.trial(s,t),i(),null!==a.rt&&this.jsPsych.pluginAPI.pressKey(a.response,a.rt)}},{key:"create_simulation_data",value:function(t,e){var i={stimulus:t.stimulus,rt:this.jsPsych.randomization.sampleExGaussian(500,50,1/150,!0),response:this.jsPsych.pluginAPI.getValidKey(t.choices)},a=this.jsPsych.pluginAPI.mergeSimulationData(i,e);return this.jsPsych.pluginAPI.ensureSimulationDataConsistency(t,a),a}}],a&&e(i.prototype,a),s&&e(i,s),Object.defineProperty(i,"prototype",{writable:!1}),t}();return a.info=i,a}(jsPsychModule);
|
|
2
|
+
//# sourceMappingURL=https://unpkg.com/@jspsych/plugin-image-keyboard-response@1.1.3/dist/index.browser.min.js.map
|