@jspsych/plugin-image-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.
@@ -1,285 +1,271 @@
1
1
  var jsPsychImageKeyboardResponse = (function (jspsych) {
2
2
  'use strict';
3
3
 
4
+ var version = "2.1.0";
5
+
4
6
  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
- },
7
+ name: "image-keyboard-response",
8
+ version,
9
+ parameters: {
10
+ /** The path of the image file to be displayed. */
11
+ stimulus: {
12
+ type: jspsych.ParameterType.IMAGE,
13
+ default: void 0
14
+ },
15
+ /** Set the height of the image in pixels. If left null (no value specified), then the image will display at its natural height. */
16
+ stimulus_height: {
17
+ type: jspsych.ParameterType.INT,
18
+ default: null
19
+ },
20
+ /** Set the width of the image in pixels. If left null (no value specified), then the image will display at its natural width. */
21
+ stimulus_width: {
22
+ type: jspsych.ParameterType.INT,
23
+ default: null
24
+ },
25
+ /** If setting *only* the width or *only* the height and this parameter is true, then the other dimension will be scaled
26
+ * to maintain the image's aspect ratio. */
27
+ maintain_aspect_ratio: {
28
+ type: jspsych.ParameterType.BOOL,
29
+ default: true
30
+ },
31
+ /**his array contains the key(s) that the participant is allowed to press in order to respond to the stimulus. Keys should
32
+ * be specified as characters (e.g., `'a'`, `'q'`, `' '`, `'Enter'`, `'ArrowDown'`) - see
33
+ * [this page](https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/key/Key_Values) and
34
+ * [this page (event.key column)](https://www.freecodecamp.org/news/javascript-keycode-list-keypress-event-key-codes/)
35
+ * for more examples. Any key presses that are not listed in the array will be ignored. The default value of `"ALL_KEYS"`
36
+ * means that all keys will be accepted as valid responses. Specifying `"NO_KEYS"` will mean that no responses are allowed. */
37
+ choices: {
38
+ type: jspsych.ParameterType.KEYS,
39
+ default: "ALL_KEYS"
70
40
  },
41
+ /**This string can contain HTML markup. Any content here will be displayed below the stimulus. The intention is that it can
42
+ * be used to provide a reminder about the action the participant is supposed to take (e.g., which key to press). */
43
+ prompt: {
44
+ type: jspsych.ParameterType.HTML_STRING,
45
+ default: null
46
+ },
47
+ /** How long to show the stimulus for in milliseconds. If the value is `null`, then the stimulus will be shown until the
48
+ * participant makes a response. */
49
+ stimulus_duration: {
50
+ type: jspsych.ParameterType.INT,
51
+ default: null
52
+ },
53
+ /** How long to wait for the participant to make a response before ending the trial in milliseconds. If the participant
54
+ * fails to make a response before this timer is reached, the participant's response will be recorded as null for the
55
+ * trial and the trial will end. If the value of this parameter is `null`, then the trial will wait for a response indefinitely. */
56
+ trial_duration: {
57
+ type: jspsych.ParameterType.INT,
58
+ default: null
59
+ },
60
+ /** If true, then the trial will end whenever the participant makes a response (assuming they make their response before
61
+ * the cutoff specified by the `trial_duration` parameter). If false, then the trial will continue until the value for
62
+ * `trial_duration` is reached. You can set this parameter to `false` to force the participant to view a stimulus for a
63
+ * fixed amount of time, even if they respond before the time is complete. */
64
+ response_ends_trial: {
65
+ type: jspsych.ParameterType.BOOL,
66
+ default: true
67
+ },
68
+ /**
69
+ * If `true`, the image will be drawn onto a canvas element. This prevents a blank screen (white flash) between consecutive image trials in some browsers, like Firefox and Edge.
70
+ * If `false`, the image will be shown via an img element, as in previous versions of jsPsych. If the stimulus is an **animated gif**, you must set this parameter to false, because the canvas rendering method will only present static images.
71
+ */
72
+ render_on_canvas: {
73
+ type: jspsych.ParameterType.BOOL,
74
+ default: true
75
+ }
76
+ },
77
+ data: {
78
+ /** The path of the image that was displayed. */
79
+ stimulus: {
80
+ type: jspsych.ParameterType.STRING
81
+ },
82
+ /** Indicates which key the participant pressed. */
83
+ response: {
84
+ type: jspsych.ParameterType.STRING
85
+ },
86
+ /** The response time in milliseconds for the participant to make a response. The time is measured from when the stimulus
87
+ * first appears on the screen until the participant's response. */
88
+ rt: {
89
+ type: jspsych.ParameterType.INT
90
+ }
91
+ },
92
+ // prettier-ignore
93
+ citations: {
94
+ "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 ",
95
+ "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}, } '
96
+ }
71
97
  };
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
98
  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
- }
99
+ constructor(jsPsych) {
100
+ this.jsPsych = jsPsych;
101
+ }
102
+ static {
103
+ this.info = info;
104
+ }
105
+ trial(display_element, trial) {
106
+ var height, width;
107
+ if (trial.render_on_canvas) {
108
+ var image_drawn = false;
109
+ if (display_element.hasChildNodes()) {
110
+ while (display_element.firstChild) {
111
+ display_element.removeChild(display_element.firstChild);
148
112
  }
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";
113
+ }
114
+ var canvas = document.createElement("canvas");
115
+ canvas.id = "jspsych-image-keyboard-response-stimulus";
116
+ canvas.style.margin = "0";
117
+ canvas.style.padding = "0";
118
+ var ctx = canvas.getContext("2d");
119
+ var img = new Image();
120
+ img.onload = () => {
121
+ if (!image_drawn) {
122
+ getHeightWidth();
123
+ ctx.drawImage(img, 0, 0, width, height);
182
124
  }
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
- });
125
+ };
126
+ img.src = trial.stimulus;
127
+ const getHeightWidth = () => {
128
+ if (trial.stimulus_height !== null) {
129
+ height = trial.stimulus_height;
130
+ if (trial.stimulus_width == null && trial.maintain_aspect_ratio) {
131
+ width = img.naturalWidth * (trial.stimulus_height / img.naturalHeight);
132
+ }
133
+ } else {
134
+ height = img.naturalHeight;
230
135
  }
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);
136
+ if (trial.stimulus_width !== null) {
137
+ width = trial.stimulus_width;
138
+ if (trial.stimulus_height == null && trial.maintain_aspect_ratio) {
139
+ height = img.naturalHeight * (trial.stimulus_width / img.naturalWidth);
140
+ }
141
+ } else if (!(trial.stimulus_height !== null && trial.maintain_aspect_ratio)) {
142
+ width = img.naturalWidth;
236
143
  }
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);
144
+ canvas.height = height;
145
+ canvas.width = width;
146
+ };
147
+ getHeightWidth();
148
+ display_element.insertBefore(canvas, null);
149
+ if (img.complete && Number.isFinite(width) && Number.isFinite(height)) {
150
+ ctx.drawImage(img, 0, 0, width, height);
151
+ image_drawn = true;
152
+ }
153
+ if (trial.prompt !== null) {
154
+ display_element.insertAdjacentHTML("beforeend", trial.prompt);
155
+ }
156
+ } else {
157
+ var html = '<img src="' + trial.stimulus + '" id="jspsych-image-keyboard-response-stimulus">';
158
+ if (trial.prompt !== null) {
159
+ html += trial.prompt;
160
+ }
161
+ display_element.innerHTML = html;
162
+ var img = display_element.querySelector(
163
+ "#jspsych-image-keyboard-response-stimulus"
164
+ );
165
+ if (trial.stimulus_height !== null) {
166
+ height = trial.stimulus_height;
167
+ if (trial.stimulus_width == null && trial.maintain_aspect_ratio) {
168
+ width = img.naturalWidth * (trial.stimulus_height / img.naturalHeight);
242
169
  }
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.");
170
+ } else {
171
+ height = img.naturalHeight;
172
+ }
173
+ if (trial.stimulus_width !== null) {
174
+ width = trial.stimulus_width;
175
+ if (trial.stimulus_height == null && trial.maintain_aspect_ratio) {
176
+ height = img.naturalHeight * (trial.stimulus_width / img.naturalWidth);
245
177
  }
178
+ } else if (!(trial.stimulus_height !== null && trial.maintain_aspect_ratio)) {
179
+ width = img.naturalWidth;
180
+ }
181
+ img.style.height = height.toString() + "px";
182
+ img.style.width = width.toString() + "px";
246
183
  }
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
- }
184
+ var response = {
185
+ rt: null,
186
+ key: null
187
+ };
188
+ const end_trial = () => {
189
+ if (typeof keyboardListener !== "undefined") {
190
+ this.jsPsych.pluginAPI.cancelKeyboardResponse(keyboardListener);
191
+ }
192
+ var trial_data = {
193
+ rt: response.rt,
194
+ stimulus: trial.stimulus,
195
+ response: response.key
196
+ };
197
+ this.jsPsych.finishTrial(trial_data);
198
+ };
199
+ var after_response = (info2) => {
200
+ display_element.querySelector("#jspsych-image-keyboard-response-stimulus").className += " responded";
201
+ if (response.key == null) {
202
+ response = info2;
203
+ }
204
+ if (trial.response_ends_trial) {
205
+ end_trial();
206
+ }
207
+ };
208
+ if (trial.choices != "NO_KEYS") {
209
+ var keyboardListener = 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
+ });
255
216
  }
256
- simulate_data_only(trial, simulation_options) {
257
- const data = this.create_simulation_data(trial, simulation_options);
258
- this.jsPsych.finishTrial(data);
217
+ if (trial.stimulus_duration !== null) {
218
+ this.jsPsych.pluginAPI.setTimeout(() => {
219
+ display_element.querySelector(
220
+ "#jspsych-image-keyboard-response-stimulus"
221
+ ).style.visibility = "hidden";
222
+ }, trial.stimulus_duration);
259
223
  }
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
- }
224
+ if (trial.trial_duration !== null) {
225
+ this.jsPsych.pluginAPI.setTimeout(() => {
226
+ end_trial();
227
+ }, trial.trial_duration);
228
+ } else if (trial.response_ends_trial === false) {
229
+ console.warn(
230
+ "The experiment may be deadlocked. Try setting a trial duration or set response_ends_trial to true."
231
+ );
232
+ }
233
+ }
234
+ simulate(trial, simulation_mode, simulation_options, load_callback) {
235
+ if (simulation_mode == "data-only") {
236
+ load_callback();
237
+ this.simulate_data_only(trial, simulation_options);
238
+ }
239
+ if (simulation_mode == "visual") {
240
+ this.simulate_visual(trial, simulation_options, load_callback);
268
241
  }
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;
242
+ }
243
+ simulate_data_only(trial, simulation_options) {
244
+ const data = this.create_simulation_data(trial, simulation_options);
245
+ this.jsPsych.finishTrial(data);
246
+ }
247
+ simulate_visual(trial, simulation_options, load_callback) {
248
+ const data = this.create_simulation_data(trial, simulation_options);
249
+ const display_element = this.jsPsych.getDisplayElement();
250
+ this.trial(display_element, trial);
251
+ load_callback();
252
+ if (data.rt !== null) {
253
+ this.jsPsych.pluginAPI.pressKey(data.response, data.rt);
278
254
  }
255
+ }
256
+ create_simulation_data(trial, simulation_options) {
257
+ const default_data = {
258
+ stimulus: trial.stimulus,
259
+ rt: this.jsPsych.randomization.sampleExGaussian(500, 50, 1 / 150, true),
260
+ response: this.jsPsych.pluginAPI.getValidKey(trial.choices)
261
+ };
262
+ const data = this.jsPsych.pluginAPI.mergeSimulationData(default_data, simulation_options);
263
+ this.jsPsych.pluginAPI.ensureSimulationDataConsistency(trial, data);
264
+ return data;
265
+ }
279
266
  }
280
- ImageKeyboardResponsePlugin.info = info;
281
267
 
282
268
  return ImageKeyboardResponsePlugin;
283
269
 
284
270
  })(jsPsychModule);
285
- //# sourceMappingURL=https://unpkg.com/@jspsych/plugin-image-keyboard-response@1.1.3/dist/index.browser.js.map
271
+ //# sourceMappingURL=https://unpkg.com/@jspsych/plugin-image-keyboard-response@2.1.0/dist/index.browser.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.browser.js","sources":["../src/index.ts"],"sourcesContent":["import { JsPsych, JsPsychPlugin, ParameterType, TrialType } from \"jspsych\";\n\nconst info = <const>{\n name: \"image-keyboard-response\",\n parameters: {\n /** The image to be displayed */\n stimulus: {\n type: ParameterType.IMAGE,\n pretty_name: \"Stimulus\",\n default: undefined,\n },\n /** Set the image height in pixels */\n stimulus_height: {\n type: ParameterType.INT,\n pretty_name: \"Image height\",\n default: null,\n },\n /** Set the image width in pixels */\n stimulus_width: {\n type: ParameterType.INT,\n pretty_name: \"Image width\",\n default: null,\n },\n /** Maintain the aspect ratio after setting width or height */\n maintain_aspect_ratio: {\n type: ParameterType.BOOL,\n pretty_name: \"Maintain aspect ratio\",\n default: true,\n },\n /** Array containing the key(s) the subject is allowed to press to respond to the stimulus. */\n choices: {\n type: ParameterType.KEYS,\n pretty_name: \"Choices\",\n default: \"ALL_KEYS\",\n },\n /** Any content here will be displayed below the stimulus. */\n prompt: {\n type: ParameterType.HTML_STRING,\n pretty_name: \"Prompt\",\n default: null,\n },\n /** How long to show the stimulus. */\n stimulus_duration: {\n type: ParameterType.INT,\n pretty_name: \"Stimulus duration\",\n default: null,\n },\n /** How long to show trial before it ends */\n trial_duration: {\n type: ParameterType.INT,\n pretty_name: \"Trial duration\",\n default: null,\n },\n /** If true, trial will end when subject makes a response. */\n response_ends_trial: {\n type: ParameterType.BOOL,\n pretty_name: \"Response ends trial\",\n default: true,\n },\n /**\n * If true, the image will be drawn onto a canvas element (prevents blank screen between consecutive images in some browsers).\n * If false, the image will be shown via an img element.\n */\n render_on_canvas: {\n type: ParameterType.BOOL,\n pretty_name: \"Render on canvas\",\n default: true,\n },\n },\n};\n\ntype Info = typeof info;\n\n/**\n * **image-keyboard-response**\n *\n * jsPsych plugin for displaying an image stimulus and getting a keyboard response\n *\n * @author Josh de Leeuw\n * @see {@link https://www.jspsych.org/plugins/jspsych-image-keyboard-response/ image-keyboard-response plugin documentation on jspsych.org}\n */\nclass ImageKeyboardResponsePlugin implements JsPsychPlugin<Info> {\n static info = info;\n\n constructor(private jsPsych: JsPsych) {}\n\n trial(display_element: HTMLElement, trial: TrialType<Info>) {\n var height, width;\n if (trial.render_on_canvas) {\n var image_drawn = false;\n // first clear the display element (because the render_on_canvas method appends to display_element instead of overwriting it with .innerHTML)\n if (display_element.hasChildNodes()) {\n // can't loop through child list because the list will be modified by .removeChild()\n while (display_element.firstChild) {\n display_element.removeChild(display_element.firstChild);\n }\n }\n // create canvas element and image\n var canvas = document.createElement(\"canvas\");\n canvas.id = \"jspsych-image-keyboard-response-stimulus\";\n canvas.style.margin = \"0\";\n canvas.style.padding = \"0\";\n var ctx = canvas.getContext(\"2d\");\n var img = new Image();\n img.onload = () => {\n // if image wasn't preloaded, then it will need to be drawn whenever it finishes loading\n if (!image_drawn) {\n getHeightWidth(); // only possible to get width/height after image loads\n ctx.drawImage(img, 0, 0, width, height);\n }\n };\n img.src = trial.stimulus;\n // get/set image height and width - this can only be done after image loads because uses image's naturalWidth/naturalHeight properties\n const getHeightWidth = () => {\n if (trial.stimulus_height !== null) {\n height = trial.stimulus_height;\n if (trial.stimulus_width == null && trial.maintain_aspect_ratio) {\n width = img.naturalWidth * (trial.stimulus_height / img.naturalHeight);\n }\n } else {\n height = img.naturalHeight;\n }\n if (trial.stimulus_width !== null) {\n width = trial.stimulus_width;\n if (trial.stimulus_height == null && trial.maintain_aspect_ratio) {\n height = img.naturalHeight * (trial.stimulus_width / img.naturalWidth);\n }\n } else if (!(trial.stimulus_height !== null && trial.maintain_aspect_ratio)) {\n // if stimulus width is null, only use the image's natural width if the width value wasn't set\n // in the if statement above, based on a specified height and maintain_aspect_ratio = true\n width = img.naturalWidth;\n }\n canvas.height = height;\n canvas.width = width;\n };\n getHeightWidth(); // call now, in case image loads immediately (is cached)\n // add canvas and draw image\n display_element.insertBefore(canvas, null);\n if (img.complete && Number.isFinite(width) && Number.isFinite(height)) {\n // if image has loaded and width/height have been set, then draw it now\n // (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)\n ctx.drawImage(img, 0, 0, width, height);\n image_drawn = true;\n }\n // add prompt if there is one\n if (trial.prompt !== null) {\n display_element.insertAdjacentHTML(\"beforeend\", trial.prompt);\n }\n } else {\n // display stimulus as an image element\n var html = '<img src=\"' + trial.stimulus + '\" id=\"jspsych-image-keyboard-response-stimulus\">';\n // add prompt\n if (trial.prompt !== null) {\n html += trial.prompt;\n }\n // update the page content\n display_element.innerHTML = html;\n\n // set image dimensions after image has loaded (so that we have access to naturalHeight/naturalWidth)\n var img = display_element.querySelector(\n \"#jspsych-image-keyboard-response-stimulus\"\n ) as HTMLImageElement;\n if (trial.stimulus_height !== null) {\n height = trial.stimulus_height;\n if (trial.stimulus_width == null && trial.maintain_aspect_ratio) {\n width = img.naturalWidth * (trial.stimulus_height / img.naturalHeight);\n }\n } else {\n height = img.naturalHeight;\n }\n if (trial.stimulus_width !== null) {\n width = trial.stimulus_width;\n if (trial.stimulus_height == null && trial.maintain_aspect_ratio) {\n height = img.naturalHeight * (trial.stimulus_width / img.naturalWidth);\n }\n } else if (!(trial.stimulus_height !== null && trial.maintain_aspect_ratio)) {\n // if stimulus width is null, only use the image's natural width if the width value wasn't set\n // in the if statement above, based on a specified height and maintain_aspect_ratio = true\n width = img.naturalWidth;\n }\n img.style.height = height.toString() + \"px\";\n img.style.width = width.toString() + \"px\";\n }\n\n // store response\n var response = {\n rt: null,\n key: null,\n };\n\n // function to end trial when it is time\n const end_trial = () => {\n // kill any remaining setTimeout handlers\n this.jsPsych.pluginAPI.clearAllTimeouts();\n\n // kill keyboard listeners\n if (typeof keyboardListener !== \"undefined\") {\n this.jsPsych.pluginAPI.cancelKeyboardResponse(keyboardListener);\n }\n\n // gather the data to store for the trial\n var trial_data = {\n rt: response.rt,\n stimulus: trial.stimulus,\n response: response.key,\n };\n\n // clear the display\n display_element.innerHTML = \"\";\n\n // move on to the next trial\n this.jsPsych.finishTrial(trial_data);\n };\n\n // function to handle responses by the subject\n var after_response = (info) => {\n // after a valid response, the stimulus will have the CSS class 'responded'\n // which can be used to provide visual feedback that a response was recorded\n display_element.querySelector(\"#jspsych-image-keyboard-response-stimulus\").className +=\n \" responded\";\n\n // only record the first response\n if (response.key == null) {\n response = info;\n }\n\n if (trial.response_ends_trial) {\n end_trial();\n }\n };\n\n // start the response listener\n if (trial.choices != \"NO_KEYS\") {\n var keyboardListener = this.jsPsych.pluginAPI.getKeyboardResponse({\n callback_function: after_response,\n valid_responses: trial.choices,\n rt_method: \"performance\",\n persist: false,\n allow_held_key: false,\n });\n }\n\n // hide stimulus if stimulus_duration is set\n if (trial.stimulus_duration !== null) {\n this.jsPsych.pluginAPI.setTimeout(() => {\n display_element.querySelector<HTMLElement>(\n \"#jspsych-image-keyboard-response-stimulus\"\n ).style.visibility = \"hidden\";\n }, trial.stimulus_duration);\n }\n\n // end trial if trial_duration is set\n if (trial.trial_duration !== null) {\n this.jsPsych.pluginAPI.setTimeout(() => {\n end_trial();\n }, trial.trial_duration);\n } else if (trial.response_ends_trial === false) {\n console.warn(\n \"The experiment may be deadlocked. Try setting a trial duration or set response_ends_trial to true.\"\n );\n }\n }\n\n simulate(\n trial: TrialType<Info>,\n simulation_mode,\n simulation_options: any,\n load_callback: () => void\n ) {\n if (simulation_mode == \"data-only\") {\n load_callback();\n this.simulate_data_only(trial, simulation_options);\n }\n if (simulation_mode == \"visual\") {\n this.simulate_visual(trial, simulation_options, load_callback);\n }\n }\n\n private simulate_data_only(trial: TrialType<Info>, simulation_options) {\n const data = this.create_simulation_data(trial, simulation_options);\n\n this.jsPsych.finishTrial(data);\n }\n\n private simulate_visual(trial: TrialType<Info>, simulation_options, load_callback: () => void) {\n const data = this.create_simulation_data(trial, simulation_options);\n\n const display_element = this.jsPsych.getDisplayElement();\n\n this.trial(display_element, trial);\n load_callback();\n\n if (data.rt !== null) {\n this.jsPsych.pluginAPI.pressKey(data.response, data.rt);\n }\n }\n\n private create_simulation_data(trial: TrialType<Info>, simulation_options) {\n const default_data = {\n stimulus: trial.stimulus,\n rt: this.jsPsych.randomization.sampleExGaussian(500, 50, 1 / 150, true),\n response: this.jsPsych.pluginAPI.getValidKey(trial.choices),\n };\n\n const data = this.jsPsych.pluginAPI.mergeSimulationData(default_data, simulation_options);\n\n this.jsPsych.pluginAPI.ensureSimulationDataConsistency(trial, data);\n\n return data;\n }\n}\n\nexport default ImageKeyboardResponsePlugin;\n"],"names":["ParameterType"],"mappings":";;;EAEA,MAAM,IAAI,GAAU;EAClB,IAAA,IAAI,EAAE,yBAAyB;EAC/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EAAE,EAAE,EAAE,CAAC,GAAG,GAAG,EAAE,IAAI,CAAC;EACvE,YAAA,QAAQ,EAAE,IAAI,CAAC,OAAO,CAAC,SAAS,CAAC,WAAW,CAAC,KAAK,CAAC,OAAO,CAAC;WAC5D,CAAC;EAEF,QAAA,MAAM,IAAI,GAAG,IAAI,CAAC,OAAO,CAAC,SAAS,CAAC,mBAAmB,CAAC,YAAY,EAAE,kBAAkB,CAAC,CAAC;UAE1F,IAAI,CAAC,OAAO,CAAC,SAAS,CAAC,+BAA+B,CAAC,KAAK,EAAE,IAAI,CAAC,CAAC;EAEpE,QAAA,OAAO,IAAI,CAAC;OACb;;EAnOM,2BAAI,CAAA,IAAA,GAAG,IAAI;;;;;;;;"}
1
+ {"version":3,"file":"index.browser.js","sources":["../package.json","../src/index.ts"],"sourcesContent":["{\n \"name\": \"@jspsych/plugin-image-keyboard-response\",\n \"version\": \"2.1.0\",\n \"description\": \"jsPsych plugin for displaying a stimulus and getting a keyboard response\",\n \"type\": \"module\",\n \"main\": \"dist/index.cjs\",\n \"exports\": {\n \"import\": \"./dist/index.js\",\n \"require\": \"./dist/index.cjs\"\n },\n \"typings\": \"dist/index.d.ts\",\n \"unpkg\": \"dist/index.browser.min.js\",\n \"files\": [\n \"src\",\n \"dist\"\n ],\n \"source\": \"src/index.ts\",\n \"scripts\": {\n \"test\": \"jest\",\n \"test:watch\": \"npm test -- --watch\",\n \"tsc\": \"tsc\",\n \"build\": \"rollup --config\",\n \"build:watch\": \"npm run build -- --watch\"\n },\n \"repository\": {\n \"type\": \"git\",\n \"url\": \"git+https://github.com/jspsych/jsPsych.git\",\n \"directory\": \"packages/plugin-image-keyboard-response\"\n },\n \"author\": \"Josh de Leeuw\",\n \"license\": \"MIT\",\n \"bugs\": {\n \"url\": \"https://github.com/jspsych/jsPsych/issues\"\n },\n \"homepage\": \"https://www.jspsych.org/latest/plugins/image-keyboard-response\",\n \"peerDependencies\": {\n \"jspsych\": \">=7.1.0\"\n },\n \"devDependencies\": {\n \"@jspsych/config\": \"^3.2.0\",\n \"@jspsych/test-utils\": \"^1.2.0\"\n }\n}\n","import { JsPsych, JsPsychPlugin, ParameterType, TrialType } from \"jspsych\";\n\nimport { version } from \"../package.json\";\n\nconst info = <const>{\n name: \"image-keyboard-response\",\n version: version,\n parameters: {\n /** The path of the image file to be displayed. */\n stimulus: {\n type: ParameterType.IMAGE,\n default: undefined,\n },\n /** Set the height of the image in pixels. If left null (no value specified), then the image will display at its natural height. */\n stimulus_height: {\n type: ParameterType.INT,\n default: null,\n },\n /** Set the width of the image in pixels. If left null (no value specified), then the image will display at its natural width. */\n stimulus_width: {\n type: ParameterType.INT,\n default: null,\n },\n /** If setting *only* the width or *only* the height and this parameter is true, then the other dimension will be scaled\n * to maintain the image's aspect ratio. */\n maintain_aspect_ratio: {\n type: ParameterType.BOOL,\n default: true,\n },\n /**his array contains the key(s) that the participant is allowed to press in order to respond to the stimulus. Keys should\n * be specified as characters (e.g., `'a'`, `'q'`, `' '`, `'Enter'`, `'ArrowDown'`) - see\n * [this page](https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/key/Key_Values) and\n * [this page (event.key column)](https://www.freecodecamp.org/news/javascript-keycode-list-keypress-event-key-codes/)\n * for more examples. Any key presses that are not listed in the array will be ignored. The default value of `\"ALL_KEYS\"`\n * means that all keys will be accepted as valid responses. Specifying `\"NO_KEYS\"` will mean that no responses are allowed. */\n choices: {\n type: ParameterType.KEYS,\n default: \"ALL_KEYS\",\n },\n /**This string can contain HTML markup. Any content here will be displayed below the stimulus. The intention is that it can\n * be used to provide a reminder about the action the participant is supposed to take (e.g., which key to press). */\n prompt: {\n type: ParameterType.HTML_STRING,\n default: null,\n },\n /** How long to show the stimulus for in milliseconds. If the value is `null`, then the stimulus will be shown until the\n * participant makes a response. */\n stimulus_duration: {\n type: ParameterType.INT,\n default: null,\n },\n /** How long to wait for the participant to make a response before ending the trial in milliseconds. If the participant\n * fails to make a response before this timer is reached, the participant's response will be recorded as null for the\n * trial and the trial will end. If the value of this parameter is `null`, then the trial will wait for a response indefinitely. */\n trial_duration: {\n type: ParameterType.INT,\n default: null,\n },\n /** If true, then the trial will end whenever the participant makes a response (assuming they make their response before\n * the cutoff specified by the `trial_duration` parameter). If false, then the trial will continue until the value for\n * `trial_duration` is reached. You can set this parameter to `false` to force the participant to view a stimulus for a\n * fixed amount of time, even if they respond before the time is complete. */\n response_ends_trial: {\n type: ParameterType.BOOL,\n default: true,\n },\n /**\n * If `true`, the image will be drawn onto a canvas element. This prevents a blank screen (white flash) between consecutive image trials in some browsers, like Firefox and Edge.\n * If `false`, the image will be shown via an img element, as in previous versions of jsPsych. If the stimulus is an **animated gif**, you must set this parameter to false, because the canvas rendering method will only present static images.\n */\n render_on_canvas: {\n type: ParameterType.BOOL,\n default: true,\n },\n },\n data: {\n /** The path of the image that was displayed. */\n stimulus: {\n type: ParameterType.STRING,\n },\n /** Indicates which key the participant pressed. */\n response: {\n type: ParameterType.STRING,\n },\n /** The response time in milliseconds for the participant to make a response. The time is measured from when the stimulus\n * first appears on the screen until the participant's response. */\n rt: {\n type: ParameterType.INT,\n },\n },\n // prettier-ignore\n citations: '__CITATIONS__',\n};\n\ntype Info = typeof info;\n\n/**\n * This plugin displays an image and records responses generated with the keyboard. The stimulus can be displayed until a\n * response is given, or for a pre-determined amount of time. The trial can be ended automatically if the participant has\n * failed to respond within a fixed length of time.\n *\n * Image files can be automatically preloaded by jsPsych using the [`preload` plugin](preload.md). However, if you are using\n * timeline variables or another dynamic method to specify the image stimulus, you will need to\n * [manually preload](../overview/media-preloading.md#manual-preloading) the images.\n *\n * @author Josh de Leeuw\n * @see {@link https://www.jspsych.org/latest/plugins/image-keyboard-response/ image-keyboard-response plugin documentation on jspsych.org}\n */\nclass ImageKeyboardResponsePlugin implements JsPsychPlugin<Info> {\n static info = info;\n\n constructor(private jsPsych: JsPsych) {}\n\n trial(display_element: HTMLElement, trial: TrialType<Info>) {\n var height, width;\n if (trial.render_on_canvas) {\n var image_drawn = false;\n // first clear the display element (because the render_on_canvas method appends to display_element instead of overwriting it with .innerHTML)\n if (display_element.hasChildNodes()) {\n // can't loop through child list because the list will be modified by .removeChild()\n while (display_element.firstChild) {\n display_element.removeChild(display_element.firstChild);\n }\n }\n // create canvas element and image\n var canvas = document.createElement(\"canvas\");\n canvas.id = \"jspsych-image-keyboard-response-stimulus\";\n canvas.style.margin = \"0\";\n canvas.style.padding = \"0\";\n var ctx = canvas.getContext(\"2d\");\n var img = new Image();\n img.onload = () => {\n // if image wasn't preloaded, then it will need to be drawn whenever it finishes loading\n if (!image_drawn) {\n getHeightWidth(); // only possible to get width/height after image loads\n ctx.drawImage(img, 0, 0, width, height);\n }\n };\n img.src = trial.stimulus;\n // get/set image height and width - this can only be done after image loads because uses image's naturalWidth/naturalHeight properties\n const getHeightWidth = () => {\n if (trial.stimulus_height !== null) {\n height = trial.stimulus_height;\n if (trial.stimulus_width == null && trial.maintain_aspect_ratio) {\n width = img.naturalWidth * (trial.stimulus_height / img.naturalHeight);\n }\n } else {\n height = img.naturalHeight;\n }\n if (trial.stimulus_width !== null) {\n width = trial.stimulus_width;\n if (trial.stimulus_height == null && trial.maintain_aspect_ratio) {\n height = img.naturalHeight * (trial.stimulus_width / img.naturalWidth);\n }\n } else if (!(trial.stimulus_height !== null && trial.maintain_aspect_ratio)) {\n // if stimulus width is null, only use the image's natural width if the width value wasn't set\n // in the if statement above, based on a specified height and maintain_aspect_ratio = true\n width = img.naturalWidth;\n }\n canvas.height = height;\n canvas.width = width;\n };\n getHeightWidth(); // call now, in case image loads immediately (is cached)\n // add canvas and draw image\n display_element.insertBefore(canvas, null);\n if (img.complete && Number.isFinite(width) && Number.isFinite(height)) {\n // if image has loaded and width/height have been set, then draw it now\n // (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)\n ctx.drawImage(img, 0, 0, width, height);\n image_drawn = true;\n }\n // add prompt if there is one\n if (trial.prompt !== null) {\n display_element.insertAdjacentHTML(\"beforeend\", trial.prompt);\n }\n } else {\n // display stimulus as an image element\n var html = '<img src=\"' + trial.stimulus + '\" id=\"jspsych-image-keyboard-response-stimulus\">';\n // add prompt\n if (trial.prompt !== null) {\n html += trial.prompt;\n }\n // update the page content\n display_element.innerHTML = html;\n\n // set image dimensions after image has loaded (so that we have access to naturalHeight/naturalWidth)\n var img = display_element.querySelector(\n \"#jspsych-image-keyboard-response-stimulus\"\n ) as HTMLImageElement;\n if (trial.stimulus_height !== null) {\n height = trial.stimulus_height;\n if (trial.stimulus_width == null && trial.maintain_aspect_ratio) {\n width = img.naturalWidth * (trial.stimulus_height / img.naturalHeight);\n }\n } else {\n height = img.naturalHeight;\n }\n if (trial.stimulus_width !== null) {\n width = trial.stimulus_width;\n if (trial.stimulus_height == null && trial.maintain_aspect_ratio) {\n height = img.naturalHeight * (trial.stimulus_width / img.naturalWidth);\n }\n } else if (!(trial.stimulus_height !== null && trial.maintain_aspect_ratio)) {\n // if stimulus width is null, only use the image's natural width if the width value wasn't set\n // in the if statement above, based on a specified height and maintain_aspect_ratio = true\n width = img.naturalWidth;\n }\n img.style.height = height.toString() + \"px\";\n img.style.width = width.toString() + \"px\";\n }\n\n // store response\n var response = {\n rt: null,\n key: null,\n };\n\n // function to end trial when it is time\n const end_trial = () => {\n // kill keyboard listeners\n if (typeof keyboardListener !== \"undefined\") {\n this.jsPsych.pluginAPI.cancelKeyboardResponse(keyboardListener);\n }\n\n // gather the data to store for the trial\n var trial_data = {\n rt: response.rt,\n stimulus: trial.stimulus,\n response: response.key,\n };\n\n // move on to the next trial\n this.jsPsych.finishTrial(trial_data);\n };\n\n // function to handle responses by the subject\n var after_response = (info) => {\n // after a valid response, the stimulus will have the CSS class 'responded'\n // which can be used to provide visual feedback that a response was recorded\n display_element.querySelector(\"#jspsych-image-keyboard-response-stimulus\").className +=\n \" responded\";\n\n // only record the first response\n if (response.key == null) {\n response = info;\n }\n\n if (trial.response_ends_trial) {\n end_trial();\n }\n };\n\n // start the response listener\n if (trial.choices != \"NO_KEYS\") {\n var keyboardListener = this.jsPsych.pluginAPI.getKeyboardResponse({\n callback_function: after_response,\n valid_responses: trial.choices,\n rt_method: \"performance\",\n persist: false,\n allow_held_key: false,\n });\n }\n\n // hide stimulus if stimulus_duration is set\n if (trial.stimulus_duration !== null) {\n this.jsPsych.pluginAPI.setTimeout(() => {\n display_element.querySelector<HTMLElement>(\n \"#jspsych-image-keyboard-response-stimulus\"\n ).style.visibility = \"hidden\";\n }, trial.stimulus_duration);\n }\n\n // end trial if trial_duration is set\n if (trial.trial_duration !== null) {\n this.jsPsych.pluginAPI.setTimeout(() => {\n end_trial();\n }, trial.trial_duration);\n } else if (trial.response_ends_trial === false) {\n console.warn(\n \"The experiment may be deadlocked. Try setting a trial duration or set response_ends_trial to true.\"\n );\n }\n }\n\n simulate(\n trial: TrialType<Info>,\n simulation_mode,\n simulation_options: any,\n load_callback: () => void\n ) {\n if (simulation_mode == \"data-only\") {\n load_callback();\n this.simulate_data_only(trial, simulation_options);\n }\n if (simulation_mode == \"visual\") {\n this.simulate_visual(trial, simulation_options, load_callback);\n }\n }\n\n private simulate_data_only(trial: TrialType<Info>, simulation_options) {\n const data = this.create_simulation_data(trial, simulation_options);\n\n this.jsPsych.finishTrial(data);\n }\n\n private simulate_visual(trial: TrialType<Info>, simulation_options, load_callback: () => void) {\n const data = this.create_simulation_data(trial, simulation_options);\n\n const display_element = this.jsPsych.getDisplayElement();\n\n this.trial(display_element, trial);\n load_callback();\n\n if (data.rt !== null) {\n this.jsPsych.pluginAPI.pressKey(data.response, data.rt);\n }\n }\n\n private create_simulation_data(trial: TrialType<Info>, simulation_options) {\n const default_data = {\n stimulus: trial.stimulus,\n rt: this.jsPsych.randomization.sampleExGaussian(500, 50, 1 / 150, true),\n response: this.jsPsych.pluginAPI.getValidKey(trial.choices),\n };\n\n const data = this.jsPsych.pluginAPI.mergeSimulationData(default_data, simulation_options);\n\n this.jsPsych.pluginAPI.ensureSimulationDataConsistency(trial, data);\n\n return data;\n }\n}\n\nexport default ImageKeyboardResponsePlugin;\n"],"names":[],"mappings":";;;EAEE,IAAW,OAAA,GAAA,OAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;ICyFA,SAAA,EAAA;EAAA;;KAAe;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}