@jspsych/plugin-image-keyboard-response 1.1.2 → 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -1,281 +1,272 @@
1
1
  import { ParameterType } from 'jspsych';
2
2
 
3
- const info = {
4
- name: "image-keyboard-response",
5
- parameters: {
6
- /** The image to be displayed */
7
- stimulus: {
8
- type: ParameterType.IMAGE,
9
- pretty_name: "Stimulus",
10
- default: undefined,
11
- },
12
- /** Set the image height in pixels */
13
- stimulus_height: {
14
- type: ParameterType.INT,
15
- pretty_name: "Image height",
16
- default: null,
17
- },
18
- /** Set the image width in pixels */
19
- stimulus_width: {
20
- type: ParameterType.INT,
21
- pretty_name: "Image width",
22
- default: null,
23
- },
24
- /** Maintain the aspect ratio after setting width or height */
25
- maintain_aspect_ratio: {
26
- type: ParameterType.BOOL,
27
- pretty_name: "Maintain aspect ratio",
28
- default: true,
29
- },
30
- /** Array containing the key(s) the subject is allowed to press to respond to the stimulus. */
31
- choices: {
32
- type: ParameterType.KEYS,
33
- pretty_name: "Choices",
34
- default: "ALL_KEYS",
35
- },
36
- /** Any content here will be displayed below the stimulus. */
37
- prompt: {
38
- type: ParameterType.HTML_STRING,
39
- pretty_name: "Prompt",
40
- default: null,
41
- },
42
- /** How long to show the stimulus. */
43
- stimulus_duration: {
44
- type: ParameterType.INT,
45
- pretty_name: "Stimulus duration",
46
- default: null,
47
- },
48
- /** How long to show trial before it ends */
49
- trial_duration: {
50
- type: ParameterType.INT,
51
- pretty_name: "Trial duration",
52
- default: null,
53
- },
54
- /** If true, trial will end when subject makes a response. */
55
- response_ends_trial: {
56
- type: ParameterType.BOOL,
57
- pretty_name: "Response ends trial",
58
- default: true,
59
- },
60
- /**
61
- * If true, the image will be drawn onto a canvas element (prevents blank screen between consecutive images in some browsers).
62
- * If false, the image will be shown via an img element.
63
- */
64
- render_on_canvas: {
65
- type: ParameterType.BOOL,
66
- pretty_name: "Render on canvas",
67
- default: true,
68
- },
69
- },
70
- };
71
- /**
72
- * **image-keyboard-response**
73
- *
74
- * jsPsych plugin for displaying an image stimulus and getting a keyboard response
75
- *
76
- * @author Josh de Leeuw
77
- * @see {@link https://www.jspsych.org/plugins/jspsych-image-keyboard-response/ image-keyboard-response plugin documentation on jspsych.org}
78
- */
79
- class ImageKeyboardResponsePlugin {
80
- constructor(jsPsych) {
81
- this.jsPsych = jsPsych;
82
- }
83
- trial(display_element, trial) {
84
- var height, width;
85
- if (trial.render_on_canvas) {
86
- var image_drawn = false;
87
- // first clear the display element (because the render_on_canvas method appends to display_element instead of overwriting it with .innerHTML)
88
- if (display_element.hasChildNodes()) {
89
- // can't loop through child list because the list will be modified by .removeChild()
90
- while (display_element.firstChild) {
91
- display_element.removeChild(display_element.firstChild);
92
- }
93
- }
94
- // create canvas element and image
95
- var canvas = document.createElement("canvas");
96
- canvas.id = "jspsych-image-keyboard-response-stimulus";
97
- canvas.style.margin = "0";
98
- canvas.style.padding = "0";
99
- var ctx = canvas.getContext("2d");
100
- var img = new Image();
101
- img.onload = () => {
102
- // if image wasn't preloaded, then it will need to be drawn whenever it finishes loading
103
- if (!image_drawn) {
104
- getHeightWidth(); // only possible to get width/height after image loads
105
- ctx.drawImage(img, 0, 0, width, height);
106
- }
107
- };
108
- img.src = trial.stimulus;
109
- // get/set image height and width - this can only be done after image loads because uses image's naturalWidth/naturalHeight properties
110
- const getHeightWidth = () => {
111
- if (trial.stimulus_height !== null) {
112
- height = trial.stimulus_height;
113
- if (trial.stimulus_width == null && trial.maintain_aspect_ratio) {
114
- width = img.naturalWidth * (trial.stimulus_height / img.naturalHeight);
115
- }
116
- }
117
- else {
118
- height = img.naturalHeight;
119
- }
120
- if (trial.stimulus_width !== null) {
121
- width = trial.stimulus_width;
122
- if (trial.stimulus_height == null && trial.maintain_aspect_ratio) {
123
- height = img.naturalHeight * (trial.stimulus_width / img.naturalWidth);
124
- }
125
- }
126
- else if (!(trial.stimulus_height !== null && trial.maintain_aspect_ratio)) {
127
- // if stimulus width is null, only use the image's natural width if the width value wasn't set
128
- // in the if statement above, based on a specified height and maintain_aspect_ratio = true
129
- width = img.naturalWidth;
130
- }
131
- canvas.height = height;
132
- canvas.width = width;
133
- };
134
- getHeightWidth(); // call now, in case image loads immediately (is cached)
135
- // add canvas and draw image
136
- display_element.insertBefore(canvas, null);
137
- if (img.complete && Number.isFinite(width) && Number.isFinite(height)) {
138
- // if image has loaded and width/height have been set, then draw it now
139
- // (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)
140
- ctx.drawImage(img, 0, 0, width, height);
141
- image_drawn = true;
142
- }
143
- // add prompt if there is one
144
- if (trial.prompt !== null) {
145
- display_element.insertAdjacentHTML("beforeend", trial.prompt);
146
- }
147
- }
148
- else {
149
- // display stimulus as an image element
150
- var html = '<img src="' + trial.stimulus + '" id="jspsych-image-keyboard-response-stimulus">';
151
- // add prompt
152
- if (trial.prompt !== null) {
153
- html += trial.prompt;
154
- }
155
- // update the page content
156
- display_element.innerHTML = html;
157
- // set image dimensions after image has loaded (so that we have access to naturalHeight/naturalWidth)
158
- var img = display_element.querySelector("#jspsych-image-keyboard-response-stimulus");
159
- if (trial.stimulus_height !== null) {
160
- height = trial.stimulus_height;
161
- if (trial.stimulus_width == null && trial.maintain_aspect_ratio) {
162
- width = img.naturalWidth * (trial.stimulus_height / img.naturalHeight);
163
- }
164
- }
165
- else {
166
- height = img.naturalHeight;
167
- }
168
- if (trial.stimulus_width !== null) {
169
- width = trial.stimulus_width;
170
- if (trial.stimulus_height == null && trial.maintain_aspect_ratio) {
171
- height = img.naturalHeight * (trial.stimulus_width / img.naturalWidth);
172
- }
173
- }
174
- else if (!(trial.stimulus_height !== null && trial.maintain_aspect_ratio)) {
175
- // if stimulus width is null, only use the image's natural width if the width value wasn't set
176
- // in the if statement above, based on a specified height and maintain_aspect_ratio = true
177
- width = img.naturalWidth;
178
- }
179
- img.style.height = height.toString() + "px";
180
- img.style.width = width.toString() + "px";
181
- }
182
- // store response
183
- var response = {
184
- rt: null,
185
- key: null,
186
- };
187
- // function to end trial when it is time
188
- const end_trial = () => {
189
- // kill any remaining setTimeout handlers
190
- this.jsPsych.pluginAPI.clearAllTimeouts();
191
- // kill keyboard listeners
192
- if (typeof keyboardListener !== "undefined") {
193
- this.jsPsych.pluginAPI.cancelKeyboardResponse(keyboardListener);
194
- }
195
- // gather the data to store for the trial
196
- var trial_data = {
197
- rt: response.rt,
198
- stimulus: trial.stimulus,
199
- response: response.key,
200
- };
201
- // clear the display
202
- display_element.innerHTML = "";
203
- // move on to the next trial
204
- this.jsPsych.finishTrial(trial_data);
205
- };
206
- // function to handle responses by the subject
207
- var after_response = (info) => {
208
- // after a valid response, the stimulus will have the CSS class 'responded'
209
- // which can be used to provide visual feedback that a response was recorded
210
- display_element.querySelector("#jspsych-image-keyboard-response-stimulus").className +=
211
- " responded";
212
- // only record the first response
213
- if (response.key == null) {
214
- response = info;
215
- }
216
- if (trial.response_ends_trial) {
217
- end_trial();
218
- }
219
- };
220
- // start the response listener
221
- if (trial.choices != "NO_KEYS") {
222
- var keyboardListener = this.jsPsych.pluginAPI.getKeyboardResponse({
223
- callback_function: after_response,
224
- valid_responses: trial.choices,
225
- rt_method: "performance",
226
- persist: false,
227
- allow_held_key: false,
228
- });
229
- }
230
- // hide stimulus if stimulus_duration is set
231
- if (trial.stimulus_duration !== null) {
232
- this.jsPsych.pluginAPI.setTimeout(() => {
233
- display_element.querySelector("#jspsych-image-keyboard-response-stimulus").style.visibility = "hidden";
234
- }, trial.stimulus_duration);
235
- }
236
- // end trial if trial_duration is set
237
- if (trial.trial_duration !== null) {
238
- this.jsPsych.pluginAPI.setTimeout(() => {
239
- end_trial();
240
- }, trial.trial_duration);
241
- }
242
- else if (trial.response_ends_trial === false) {
243
- console.warn("The experiment may be deadlocked. Try setting a trial duration or set response_ends_trial to true.");
244
- }
245
- }
246
- simulate(trial, simulation_mode, simulation_options, load_callback) {
247
- if (simulation_mode == "data-only") {
248
- load_callback();
249
- this.simulate_data_only(trial, simulation_options);
250
- }
251
- if (simulation_mode == "visual") {
252
- this.simulate_visual(trial, simulation_options, load_callback);
253
- }
254
- }
255
- simulate_data_only(trial, simulation_options) {
256
- const data = this.create_simulation_data(trial, simulation_options);
257
- this.jsPsych.finishTrial(data);
258
- }
259
- simulate_visual(trial, simulation_options, load_callback) {
260
- const data = this.create_simulation_data(trial, simulation_options);
261
- const display_element = this.jsPsych.getDisplayElement();
262
- this.trial(display_element, trial);
263
- load_callback();
264
- if (data.rt !== null) {
265
- this.jsPsych.pluginAPI.pressKey(data.response, data.rt);
266
- }
267
- }
268
- create_simulation_data(trial, simulation_options) {
269
- const default_data = {
270
- stimulus: trial.stimulus,
271
- rt: this.jsPsych.randomization.sampleExGaussian(500, 50, 1 / 150, true),
272
- response: this.jsPsych.pluginAPI.getValidKey(trial.choices),
273
- };
274
- const data = this.jsPsych.pluginAPI.mergeSimulationData(default_data, simulation_options);
275
- this.jsPsych.pluginAPI.ensureSimulationDataConsistency(trial, data);
276
- return data;
277
- }
278
- }
3
+ var _package = {
4
+ name: "@jspsych/plugin-image-keyboard-response",
5
+ version: "2.0.0",
6
+ description: "jsPsych plugin for displaying a stimulus and getting a keyboard response",
7
+ type: "module",
8
+ main: "dist/index.cjs",
9
+ exports: {
10
+ import: "./dist/index.js",
11
+ require: "./dist/index.cjs"
12
+ },
13
+ typings: "dist/index.d.ts",
14
+ unpkg: "dist/index.browser.min.js",
15
+ files: [
16
+ "src",
17
+ "dist"
18
+ ],
19
+ source: "src/index.ts",
20
+ scripts: {
21
+ test: "jest",
22
+ "test:watch": "npm test -- --watch",
23
+ tsc: "tsc",
24
+ build: "rollup --config",
25
+ "build:watch": "npm run build -- --watch"
26
+ },
27
+ repository: {
28
+ type: "git",
29
+ url: "git+https://github.com/jspsych/jsPsych.git",
30
+ directory: "packages/plugin-image-keyboard-response"
31
+ },
32
+ author: "Josh de Leeuw",
33
+ license: "MIT",
34
+ bugs: {
35
+ url: "https://github.com/jspsych/jsPsych/issues"
36
+ },
37
+ homepage: "https://www.jspsych.org/latest/plugins/image-keyboard-response",
38
+ peerDependencies: {
39
+ jspsych: ">=7.1.0"
40
+ },
41
+ devDependencies: {
42
+ "@jspsych/config": "^3.0.0",
43
+ "@jspsych/test-utils": "^1.2.0"
44
+ }
45
+ };
46
+
47
+ const info = {
48
+ name: "image-keyboard-response",
49
+ version: _package.version,
50
+ parameters: {
51
+ stimulus: {
52
+ type: ParameterType.IMAGE,
53
+ default: void 0
54
+ },
55
+ stimulus_height: {
56
+ type: ParameterType.INT,
57
+ default: null
58
+ },
59
+ stimulus_width: {
60
+ type: ParameterType.INT,
61
+ default: null
62
+ },
63
+ maintain_aspect_ratio: {
64
+ type: ParameterType.BOOL,
65
+ default: true
66
+ },
67
+ choices: {
68
+ type: ParameterType.KEYS,
69
+ default: "ALL_KEYS"
70
+ },
71
+ prompt: {
72
+ type: ParameterType.HTML_STRING,
73
+ default: null
74
+ },
75
+ stimulus_duration: {
76
+ type: ParameterType.INT,
77
+ default: null
78
+ },
79
+ trial_duration: {
80
+ type: ParameterType.INT,
81
+ default: null
82
+ },
83
+ response_ends_trial: {
84
+ type: ParameterType.BOOL,
85
+ default: true
86
+ },
87
+ render_on_canvas: {
88
+ type: ParameterType.BOOL,
89
+ default: true
90
+ }
91
+ },
92
+ data: {
93
+ stimulus: {
94
+ type: ParameterType.STRING
95
+ },
96
+ response: {
97
+ type: ParameterType.STRING
98
+ },
99
+ rt: {
100
+ type: ParameterType.INT
101
+ }
102
+ }
103
+ };
104
+ class ImageKeyboardResponsePlugin {
105
+ constructor(jsPsych) {
106
+ this.jsPsych = jsPsych;
107
+ }
108
+ trial(display_element, trial) {
109
+ var height, width;
110
+ if (trial.render_on_canvas) {
111
+ var image_drawn = false;
112
+ if (display_element.hasChildNodes()) {
113
+ while (display_element.firstChild) {
114
+ display_element.removeChild(display_element.firstChild);
115
+ }
116
+ }
117
+ var canvas = document.createElement("canvas");
118
+ canvas.id = "jspsych-image-keyboard-response-stimulus";
119
+ canvas.style.margin = "0";
120
+ canvas.style.padding = "0";
121
+ var ctx = canvas.getContext("2d");
122
+ var img = new Image();
123
+ img.onload = () => {
124
+ if (!image_drawn) {
125
+ getHeightWidth();
126
+ ctx.drawImage(img, 0, 0, width, height);
127
+ }
128
+ };
129
+ img.src = trial.stimulus;
130
+ const getHeightWidth = () => {
131
+ if (trial.stimulus_height !== null) {
132
+ height = trial.stimulus_height;
133
+ if (trial.stimulus_width == null && trial.maintain_aspect_ratio) {
134
+ width = img.naturalWidth * (trial.stimulus_height / img.naturalHeight);
135
+ }
136
+ } else {
137
+ height = img.naturalHeight;
138
+ }
139
+ if (trial.stimulus_width !== null) {
140
+ width = trial.stimulus_width;
141
+ if (trial.stimulus_height == null && trial.maintain_aspect_ratio) {
142
+ height = img.naturalHeight * (trial.stimulus_width / img.naturalWidth);
143
+ }
144
+ } else if (!(trial.stimulus_height !== null && trial.maintain_aspect_ratio)) {
145
+ width = img.naturalWidth;
146
+ }
147
+ canvas.height = height;
148
+ canvas.width = width;
149
+ };
150
+ getHeightWidth();
151
+ display_element.insertBefore(canvas, null);
152
+ if (img.complete && Number.isFinite(width) && Number.isFinite(height)) {
153
+ ctx.drawImage(img, 0, 0, width, height);
154
+ image_drawn = true;
155
+ }
156
+ if (trial.prompt !== null) {
157
+ display_element.insertAdjacentHTML("beforeend", trial.prompt);
158
+ }
159
+ } else {
160
+ var html = '<img src="' + trial.stimulus + '" id="jspsych-image-keyboard-response-stimulus">';
161
+ if (trial.prompt !== null) {
162
+ html += trial.prompt;
163
+ }
164
+ display_element.innerHTML = html;
165
+ var img = display_element.querySelector(
166
+ "#jspsych-image-keyboard-response-stimulus"
167
+ );
168
+ if (trial.stimulus_height !== null) {
169
+ height = trial.stimulus_height;
170
+ if (trial.stimulus_width == null && trial.maintain_aspect_ratio) {
171
+ width = img.naturalWidth * (trial.stimulus_height / img.naturalHeight);
172
+ }
173
+ } else {
174
+ height = img.naturalHeight;
175
+ }
176
+ if (trial.stimulus_width !== null) {
177
+ width = trial.stimulus_width;
178
+ if (trial.stimulus_height == null && trial.maintain_aspect_ratio) {
179
+ height = img.naturalHeight * (trial.stimulus_width / img.naturalWidth);
180
+ }
181
+ } else if (!(trial.stimulus_height !== null && trial.maintain_aspect_ratio)) {
182
+ width = img.naturalWidth;
183
+ }
184
+ img.style.height = height.toString() + "px";
185
+ img.style.width = width.toString() + "px";
186
+ }
187
+ var response = {
188
+ rt: null,
189
+ key: null
190
+ };
191
+ const end_trial = () => {
192
+ if (typeof keyboardListener !== "undefined") {
193
+ this.jsPsych.pluginAPI.cancelKeyboardResponse(keyboardListener);
194
+ }
195
+ var trial_data = {
196
+ rt: response.rt,
197
+ stimulus: trial.stimulus,
198
+ response: response.key
199
+ };
200
+ this.jsPsych.finishTrial(trial_data);
201
+ };
202
+ var after_response = (info2) => {
203
+ display_element.querySelector("#jspsych-image-keyboard-response-stimulus").className += " responded";
204
+ if (response.key == null) {
205
+ response = info2;
206
+ }
207
+ if (trial.response_ends_trial) {
208
+ end_trial();
209
+ }
210
+ };
211
+ if (trial.choices != "NO_KEYS") {
212
+ var keyboardListener = this.jsPsych.pluginAPI.getKeyboardResponse({
213
+ callback_function: after_response,
214
+ valid_responses: trial.choices,
215
+ rt_method: "performance",
216
+ persist: false,
217
+ allow_held_key: false
218
+ });
219
+ }
220
+ if (trial.stimulus_duration !== null) {
221
+ this.jsPsych.pluginAPI.setTimeout(() => {
222
+ display_element.querySelector(
223
+ "#jspsych-image-keyboard-response-stimulus"
224
+ ).style.visibility = "hidden";
225
+ }, trial.stimulus_duration);
226
+ }
227
+ if (trial.trial_duration !== null) {
228
+ this.jsPsych.pluginAPI.setTimeout(() => {
229
+ end_trial();
230
+ }, trial.trial_duration);
231
+ } else if (trial.response_ends_trial === false) {
232
+ console.warn(
233
+ "The experiment may be deadlocked. Try setting a trial duration or set response_ends_trial to true."
234
+ );
235
+ }
236
+ }
237
+ simulate(trial, simulation_mode, simulation_options, load_callback) {
238
+ if (simulation_mode == "data-only") {
239
+ load_callback();
240
+ this.simulate_data_only(trial, simulation_options);
241
+ }
242
+ if (simulation_mode == "visual") {
243
+ this.simulate_visual(trial, simulation_options, load_callback);
244
+ }
245
+ }
246
+ simulate_data_only(trial, simulation_options) {
247
+ const data = this.create_simulation_data(trial, simulation_options);
248
+ this.jsPsych.finishTrial(data);
249
+ }
250
+ simulate_visual(trial, simulation_options, load_callback) {
251
+ const data = this.create_simulation_data(trial, simulation_options);
252
+ const display_element = this.jsPsych.getDisplayElement();
253
+ this.trial(display_element, trial);
254
+ load_callback();
255
+ if (data.rt !== null) {
256
+ this.jsPsych.pluginAPI.pressKey(data.response, data.rt);
257
+ }
258
+ }
259
+ create_simulation_data(trial, simulation_options) {
260
+ const default_data = {
261
+ stimulus: trial.stimulus,
262
+ rt: this.jsPsych.randomization.sampleExGaussian(500, 50, 1 / 150, true),
263
+ response: this.jsPsych.pluginAPI.getValidKey(trial.choices)
264
+ };
265
+ const data = this.jsPsych.pluginAPI.mergeSimulationData(default_data, simulation_options);
266
+ this.jsPsych.pluginAPI.ensureSimulationDataConsistency(trial, data);
267
+ return data;
268
+ }
269
+ }
279
270
  ImageKeyboardResponsePlugin.info = info;
280
271
 
281
272
  export { ImageKeyboardResponsePlugin as default };