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