@jspsych/plugin-image-keyboard-response 2.0.0 → 2.2.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,111 +2,123 @@
2
2
 
3
3
  var jspsych = require('jspsych');
4
4
 
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
- };
5
+ var version = "2.2.0";
48
6
 
49
7
  const info = {
50
8
  name: "image-keyboard-response",
51
- version: _package.version,
9
+ version,
52
10
  parameters: {
11
+ /** The path of the image file to be displayed. */
53
12
  stimulus: {
54
13
  type: jspsych.ParameterType.IMAGE,
55
14
  default: void 0
56
15
  },
16
+ /** Set the height of the image in pixels. If left null (no value specified), then the image will display at its natural height. */
57
17
  stimulus_height: {
58
18
  type: jspsych.ParameterType.INT,
59
19
  default: null
60
20
  },
21
+ /** Set the width of the image in pixels. If left null (no value specified), then the image will display at its natural width. */
61
22
  stimulus_width: {
62
23
  type: jspsych.ParameterType.INT,
63
24
  default: null
64
25
  },
26
+ /** If setting *only* the width or *only* the height and this parameter is true, then the other dimension will be scaled
27
+ * to maintain the image's aspect ratio. */
65
28
  maintain_aspect_ratio: {
66
29
  type: jspsych.ParameterType.BOOL,
67
30
  default: true
68
31
  },
32
+ /**his array contains the key(s) that the participant is allowed to press in order to respond to the stimulus. Keys should
33
+ * be specified as characters (e.g., `'a'`, `'q'`, `' '`, `'Enter'`, `'ArrowDown'`) - see
34
+ * [this page](https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/key/Key_Values) and
35
+ * [this page (event.key column)](https://www.freecodecamp.org/news/javascript-keycode-list-keypress-event-key-codes/)
36
+ * for more examples. Any key presses that are not listed in the array will be ignored. The default value of `"ALL_KEYS"`
37
+ * means that all keys will be accepted as valid responses. Specifying `"NO_KEYS"` will mean that no responses are allowed. */
69
38
  choices: {
70
39
  type: jspsych.ParameterType.KEYS,
71
40
  default: "ALL_KEYS"
72
41
  },
42
+ /**This string can contain HTML markup. Any content here will be displayed below the stimulus. The intention is that it can
43
+ * be used to provide a reminder about the action the participant is supposed to take (e.g., which key to press). */
73
44
  prompt: {
74
45
  type: jspsych.ParameterType.HTML_STRING,
75
46
  default: null
76
47
  },
48
+ /** How long to show the stimulus for in milliseconds. If the value is `null`, then the stimulus will be shown until the
49
+ * participant makes a response. */
77
50
  stimulus_duration: {
78
51
  type: jspsych.ParameterType.INT,
79
52
  default: null
80
53
  },
54
+ /** How long to wait for the participant to make a response before ending the trial in milliseconds. If the participant
55
+ * fails to make a response before this timer is reached, the participant's response will be recorded as null for the
56
+ * trial and the trial will end. If the value of this parameter is `null`, then the trial will wait for a response indefinitely. */
81
57
  trial_duration: {
82
58
  type: jspsych.ParameterType.INT,
83
59
  default: null
84
60
  },
61
+ /** If true, then the trial will end whenever the participant makes a response (assuming they make their response before
62
+ * the cutoff specified by the `trial_duration` parameter). If false, then the trial will continue until the value for
63
+ * `trial_duration` is reached. You can set this parameter to `false` to force the participant to view a stimulus for a
64
+ * fixed amount of time, even if they respond before the time is complete. */
85
65
  response_ends_trial: {
86
66
  type: jspsych.ParameterType.BOOL,
87
67
  default: true
88
68
  },
69
+ /**
70
+ * 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.
71
+ * 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.
72
+ */
89
73
  render_on_canvas: {
90
74
  type: jspsych.ParameterType.BOOL,
91
75
  default: true
76
+ },
77
+ /**
78
+ * If true, the response is not registered until the participant releases the key. The response
79
+ * time (`rt`) still reflects when the key was pressed, and the additional data field
80
+ * `rt_key_duration` records how long the key was held down. Note that when this is true, the
81
+ * trial cannot end until the key is released: with `response_ends_trial: true` the trial ends at
82
+ * the key release, and if the trial ends for another reason (e.g., `trial_duration`) while the
83
+ * key is still held, no response is recorded for the trial.
84
+ */
85
+ wait_for_key_release: {
86
+ type: jspsych.ParameterType.BOOL,
87
+ default: false
92
88
  }
93
89
  },
94
90
  data: {
91
+ /** The path of the image that was displayed. */
95
92
  stimulus: {
96
93
  type: jspsych.ParameterType.STRING
97
94
  },
95
+ /** Indicates which key the participant pressed. */
98
96
  response: {
99
97
  type: jspsych.ParameterType.STRING
100
98
  },
99
+ /** The response time in milliseconds for the participant to make a response. The time is measured from when the stimulus
100
+ * first appears on the screen until the participant's response. */
101
101
  rt: {
102
102
  type: jspsych.ParameterType.INT
103
+ },
104
+ /** The duration in milliseconds that the response key was held down, measured from key press to key release. Only recorded when `wait_for_key_release` is true; null otherwise or when no response was made. */
105
+ rt_key_duration: {
106
+ type: jspsych.ParameterType.INT
103
107
  }
108
+ },
109
+ // prettier-ignore
110
+ citations: {
111
+ "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 ",
112
+ "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}, } '
104
113
  }
105
114
  };
106
115
  class ImageKeyboardResponsePlugin {
107
116
  constructor(jsPsych) {
108
117
  this.jsPsych = jsPsych;
109
118
  }
119
+ static {
120
+ this.info = info;
121
+ }
110
122
  trial(display_element, trial) {
111
123
  var height, width;
112
124
  if (trial.render_on_canvas) {
@@ -188,7 +200,8 @@ class ImageKeyboardResponsePlugin {
188
200
  }
189
201
  var response = {
190
202
  rt: null,
191
- key: null
203
+ key: null,
204
+ rt_key_duration: null
192
205
  };
193
206
  const end_trial = () => {
194
207
  if (typeof keyboardListener !== "undefined") {
@@ -197,14 +210,19 @@ class ImageKeyboardResponsePlugin {
197
210
  var trial_data = {
198
211
  rt: response.rt,
199
212
  stimulus: trial.stimulus,
200
- response: response.key
213
+ response: response.key,
214
+ rt_key_duration: response.rt_key_duration
201
215
  };
202
216
  this.jsPsych.finishTrial(trial_data);
203
217
  };
204
218
  var after_response = (info2) => {
205
219
  display_element.querySelector("#jspsych-image-keyboard-response-stimulus").className += " responded";
206
220
  if (response.key == null) {
207
- response = info2;
221
+ response = {
222
+ rt: info2.rt,
223
+ key: info2.key,
224
+ rt_key_duration: info2.rt_key_duration ?? null
225
+ };
208
226
  }
209
227
  if (trial.response_ends_trial) {
210
228
  end_trial();
@@ -216,7 +234,8 @@ class ImageKeyboardResponsePlugin {
216
234
  valid_responses: trial.choices,
217
235
  rt_method: "performance",
218
236
  persist: false,
219
- allow_held_key: false
237
+ allow_held_key: false,
238
+ wait_for_key_release: trial.wait_for_key_release
220
239
  });
221
240
  }
222
241
  if (trial.stimulus_duration !== null) {
@@ -262,14 +281,15 @@ class ImageKeyboardResponsePlugin {
262
281
  const default_data = {
263
282
  stimulus: trial.stimulus,
264
283
  rt: this.jsPsych.randomization.sampleExGaussian(500, 50, 1 / 150, true),
265
- response: this.jsPsych.pluginAPI.getValidKey(trial.choices)
284
+ response: this.jsPsych.pluginAPI.getValidKey(trial.choices),
285
+ rt_key_duration: null
266
286
  };
287
+ default_data.rt_key_duration = default_data.rt === null || !trial.wait_for_key_release ? null : this.jsPsych.randomization.sampleExGaussian(150, 30, 1 / 100, true);
267
288
  const data = this.jsPsych.pluginAPI.mergeSimulationData(default_data, simulation_options);
268
289
  this.jsPsych.pluginAPI.ensureSimulationDataConsistency(trial, data);
269
290
  return data;
270
291
  }
271
292
  }
272
- ImageKeyboardResponsePlugin.info = info;
273
293
 
274
294
  module.exports = ImageKeyboardResponsePlugin;
275
295
  //# sourceMappingURL=index.cjs.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.cjs","sources":["../src/index.ts"],"sourcesContent":["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};\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":["version","ParameterType","info"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAIA,MAAM,IAAc,GAAA;AAAA,EAClB,IAAM,EAAA,yBAAA;AAAA,WACNA,gBAAA;AAAA,EACA,UAAY,EAAA;AAAA,IAEV,QAAU,EAAA;AAAA,MACR,MAAMC,qBAAc,CAAA,KAAA;AAAA,MACpB,OAAS,EAAA,KAAA,CAAA;AAAA,KACX;AAAA,IAEA,eAAiB,EAAA;AAAA,MACf,MAAMA,qBAAc,CAAA,GAAA;AAAA,MACpB,OAAS,EAAA,IAAA;AAAA,KACX;AAAA,IAEA,cAAgB,EAAA;AAAA,MACd,MAAMA,qBAAc,CAAA,GAAA;AAAA,MACpB,OAAS,EAAA,IAAA;AAAA,KACX;AAAA,IAGA,qBAAuB,EAAA;AAAA,MACrB,MAAMA,qBAAc,CAAA,IAAA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AAM,MAAA,eAAA,GAAkB,IAAK,CAAA,OAAA,CAAQ,iBAAkB,EAAA,CAAA;AAEvD,IAAK,IAAA,CAAA,KAAA,CAAM,iBAAiB,KAAK,CAAA,CAAA;AACjC,IAAc,aAAA,EAAA,CAAA;AAEd,IAAI,IAAA,IAAA,CAAK,OAAO,IAAM,EAAA;AACpB,MAAA,IAAA,CAAK,QAAQ,SAAU,CAAA,QAAA,CAAS,IAAK,CAAA,QAAA,EAAU,KAAK,EAAE,CAAA,CAAA;AAAA,KACxD;AAAA,GACF;AAAA,EAEQ,sBAAA,CAAuB,OAAwB,kBAAoB,EAAA;AACzE,IAAA,MAAM,YAAe,GAAA;AAAA,MACnB,UAAU,KAAM,CAAA,QAAA;AAAA,MAChB,EAAA,EAAI,KAAK,OAAQ,CAAA,aAAA,CAAc,iBAAiB,GAAK,EAAA,EAAA,EAAI,CAAI,GAAA,GAAA,EAAK,IAAI,CAAA;AAAA,MACtE,UAAU,IAAK,CAAA,OAAA,CAAQ,SAAU,CAAA,WAAA,CAAY,MAAM,OAAO,CAAA;AAAA,KAC5D,CAAA;AAEA,IAAA,MAAM,OAAO,IAAK,CAAA,OAAA,CAAQ,SAAU,CAAA,mBAAA,CAAoB,cAAc,kBAAkB,CAAA,CAAA;AAExF,IAAA,IAAA,CAAK,OAAQ,CAAA,SAAA,CAAU,+BAAgC,CAAA,KAAA,EAAO,IAAI,CAAA,CAAA;AAElE,IAAO,OAAA,IAAA,CAAA;AAAA,GACT;AACF,CAAA;AA/NM,2BAAA,CACG,IAAO,GAAA,IAAA;;;;"}
1
+ {"version":3,"file":"index.cjs","sources":["../package.json","../src/index.ts"],"sourcesContent":["{\n \"name\": \"@jspsych/plugin-image-keyboard-response\",\n \"version\": \"2.2.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.3.4\",\n \"@jspsych/test-utils\": \"^1.3.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 * If true, the response is not registered until the participant releases the key. The response\n * time (`rt`) still reflects when the key was pressed, and the additional data field\n * `rt_key_duration` records how long the key was held down. Note that when this is true, the\n * trial cannot end until the key is released: with `response_ends_trial: true` the trial ends at\n * the key release, and if the trial ends for another reason (e.g., `trial_duration`) while the\n * key is still held, no response is recorded for the trial.\n */\n wait_for_key_release: {\n type: ParameterType.BOOL,\n default: false,\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 /** The duration in milliseconds that the response key was held down, measured from key press to key release. Only recorded when `wait_for_key_release` is true; null otherwise or when no response was made. */\n rt_key_duration: {\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 rt_key_duration: 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 rt_key_duration: response.rt_key_duration,\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 = {\n rt: info.rt,\n key: info.key,\n rt_key_duration: info.rt_key_duration ?? null,\n };\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 wait_for_key_release: trial.wait_for_key_release,\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 rt_key_duration: null,\n };\n default_data.rt_key_duration =\n default_data.rt === null || !trial.wait_for_key_release\n ? null\n : this.jsPsych.randomization.sampleExGaussian(150, 30, 1 / 100, true);\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":";;;;AAEE,IAAA,OAAA,GAAW,OAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;ECyGA,SAAA,EAAA;AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
package/dist/index.d.ts CHANGED
@@ -70,6 +70,18 @@ declare const info: {
70
70
  readonly type: ParameterType.BOOL;
71
71
  readonly default: true;
72
72
  };
73
+ /**
74
+ * If true, the response is not registered until the participant releases the key. The response
75
+ * time (`rt`) still reflects when the key was pressed, and the additional data field
76
+ * `rt_key_duration` records how long the key was held down. Note that when this is true, the
77
+ * trial cannot end until the key is released: with `response_ends_trial: true` the trial ends at
78
+ * the key release, and if the trial ends for another reason (e.g., `trial_duration`) while the
79
+ * key is still held, no response is recorded for the trial.
80
+ */
81
+ readonly wait_for_key_release: {
82
+ readonly type: ParameterType.BOOL;
83
+ readonly default: false;
84
+ };
73
85
  };
74
86
  readonly data: {
75
87
  /** The path of the image that was displayed. */
@@ -85,7 +97,12 @@ declare const info: {
85
97
  readonly rt: {
86
98
  readonly type: ParameterType.INT;
87
99
  };
100
+ /** The duration in milliseconds that the response key was held down, measured from key press to key release. Only recorded when `wait_for_key_release` is true; null otherwise or when no response was made. */
101
+ readonly rt_key_duration: {
102
+ readonly type: ParameterType.INT;
103
+ };
88
104
  };
105
+ readonly citations: "__CITATIONS__";
89
106
  };
90
107
  type Info = typeof info;
91
108
  /**
@@ -172,6 +189,18 @@ declare class ImageKeyboardResponsePlugin implements JsPsychPlugin<Info> {
172
189
  readonly type: ParameterType.BOOL;
173
190
  readonly default: true;
174
191
  };
192
+ /**
193
+ * If true, the response is not registered until the participant releases the key. The response
194
+ * time (`rt`) still reflects when the key was pressed, and the additional data field
195
+ * `rt_key_duration` records how long the key was held down. Note that when this is true, the
196
+ * trial cannot end until the key is released: with `response_ends_trial: true` the trial ends at
197
+ * the key release, and if the trial ends for another reason (e.g., `trial_duration`) while the
198
+ * key is still held, no response is recorded for the trial.
199
+ */
200
+ readonly wait_for_key_release: {
201
+ readonly type: ParameterType.BOOL;
202
+ readonly default: false;
203
+ };
175
204
  };
176
205
  readonly data: {
177
206
  /** The path of the image that was displayed. */
@@ -187,7 +216,12 @@ declare class ImageKeyboardResponsePlugin implements JsPsychPlugin<Info> {
187
216
  readonly rt: {
188
217
  readonly type: ParameterType.INT;
189
218
  };
219
+ /** The duration in milliseconds that the response key was held down, measured from key press to key release. Only recorded when `wait_for_key_release` is true; null otherwise or when no response was made. */
220
+ readonly rt_key_duration: {
221
+ readonly type: ParameterType.INT;
222
+ };
190
223
  };
224
+ readonly citations: "__CITATIONS__";
191
225
  };
192
226
  constructor(jsPsych: JsPsych);
193
227
  trial(display_element: HTMLElement, trial: TrialType<Info>): void;
package/dist/index.js CHANGED
@@ -1,110 +1,122 @@
1
1
  import { ParameterType } from 'jspsych';
2
2
 
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
- };
3
+ var version = "2.2.0";
46
4
 
47
5
  const info = {
48
6
  name: "image-keyboard-response",
49
- version: _package.version,
7
+ version,
50
8
  parameters: {
9
+ /** The path of the image file to be displayed. */
51
10
  stimulus: {
52
11
  type: ParameterType.IMAGE,
53
12
  default: void 0
54
13
  },
14
+ /** Set the height of the image in pixels. If left null (no value specified), then the image will display at its natural height. */
55
15
  stimulus_height: {
56
16
  type: ParameterType.INT,
57
17
  default: null
58
18
  },
19
+ /** Set the width of the image in pixels. If left null (no value specified), then the image will display at its natural width. */
59
20
  stimulus_width: {
60
21
  type: ParameterType.INT,
61
22
  default: null
62
23
  },
24
+ /** If setting *only* the width or *only* the height and this parameter is true, then the other dimension will be scaled
25
+ * to maintain the image's aspect ratio. */
63
26
  maintain_aspect_ratio: {
64
27
  type: ParameterType.BOOL,
65
28
  default: true
66
29
  },
30
+ /**his array contains the key(s) that the participant is allowed to press in order to respond to the stimulus. Keys should
31
+ * be specified as characters (e.g., `'a'`, `'q'`, `' '`, `'Enter'`, `'ArrowDown'`) - see
32
+ * [this page](https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/key/Key_Values) and
33
+ * [this page (event.key column)](https://www.freecodecamp.org/news/javascript-keycode-list-keypress-event-key-codes/)
34
+ * for more examples. Any key presses that are not listed in the array will be ignored. The default value of `"ALL_KEYS"`
35
+ * means that all keys will be accepted as valid responses. Specifying `"NO_KEYS"` will mean that no responses are allowed. */
67
36
  choices: {
68
37
  type: ParameterType.KEYS,
69
38
  default: "ALL_KEYS"
70
39
  },
40
+ /**This string can contain HTML markup. Any content here will be displayed below the stimulus. The intention is that it can
41
+ * be used to provide a reminder about the action the participant is supposed to take (e.g., which key to press). */
71
42
  prompt: {
72
43
  type: ParameterType.HTML_STRING,
73
44
  default: null
74
45
  },
46
+ /** How long to show the stimulus for in milliseconds. If the value is `null`, then the stimulus will be shown until the
47
+ * participant makes a response. */
75
48
  stimulus_duration: {
76
49
  type: ParameterType.INT,
77
50
  default: null
78
51
  },
52
+ /** How long to wait for the participant to make a response before ending the trial in milliseconds. If the participant
53
+ * fails to make a response before this timer is reached, the participant's response will be recorded as null for the
54
+ * trial and the trial will end. If the value of this parameter is `null`, then the trial will wait for a response indefinitely. */
79
55
  trial_duration: {
80
56
  type: ParameterType.INT,
81
57
  default: null
82
58
  },
59
+ /** If true, then the trial will end whenever the participant makes a response (assuming they make their response before
60
+ * the cutoff specified by the `trial_duration` parameter). If false, then the trial will continue until the value for
61
+ * `trial_duration` is reached. You can set this parameter to `false` to force the participant to view a stimulus for a
62
+ * fixed amount of time, even if they respond before the time is complete. */
83
63
  response_ends_trial: {
84
64
  type: ParameterType.BOOL,
85
65
  default: true
86
66
  },
67
+ /**
68
+ * 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.
69
+ * 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.
70
+ */
87
71
  render_on_canvas: {
88
72
  type: ParameterType.BOOL,
89
73
  default: true
74
+ },
75
+ /**
76
+ * If true, the response is not registered until the participant releases the key. The response
77
+ * time (`rt`) still reflects when the key was pressed, and the additional data field
78
+ * `rt_key_duration` records how long the key was held down. Note that when this is true, the
79
+ * trial cannot end until the key is released: with `response_ends_trial: true` the trial ends at
80
+ * the key release, and if the trial ends for another reason (e.g., `trial_duration`) while the
81
+ * key is still held, no response is recorded for the trial.
82
+ */
83
+ wait_for_key_release: {
84
+ type: ParameterType.BOOL,
85
+ default: false
90
86
  }
91
87
  },
92
88
  data: {
89
+ /** The path of the image that was displayed. */
93
90
  stimulus: {
94
91
  type: ParameterType.STRING
95
92
  },
93
+ /** Indicates which key the participant pressed. */
96
94
  response: {
97
95
  type: ParameterType.STRING
98
96
  },
97
+ /** The response time in milliseconds for the participant to make a response. The time is measured from when the stimulus
98
+ * first appears on the screen until the participant's response. */
99
99
  rt: {
100
100
  type: ParameterType.INT
101
+ },
102
+ /** The duration in milliseconds that the response key was held down, measured from key press to key release. Only recorded when `wait_for_key_release` is true; null otherwise or when no response was made. */
103
+ rt_key_duration: {
104
+ type: ParameterType.INT
101
105
  }
106
+ },
107
+ // prettier-ignore
108
+ citations: {
109
+ "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 ",
110
+ "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}, } '
102
111
  }
103
112
  };
104
113
  class ImageKeyboardResponsePlugin {
105
114
  constructor(jsPsych) {
106
115
  this.jsPsych = jsPsych;
107
116
  }
117
+ static {
118
+ this.info = info;
119
+ }
108
120
  trial(display_element, trial) {
109
121
  var height, width;
110
122
  if (trial.render_on_canvas) {
@@ -186,7 +198,8 @@ class ImageKeyboardResponsePlugin {
186
198
  }
187
199
  var response = {
188
200
  rt: null,
189
- key: null
201
+ key: null,
202
+ rt_key_duration: null
190
203
  };
191
204
  const end_trial = () => {
192
205
  if (typeof keyboardListener !== "undefined") {
@@ -195,14 +208,19 @@ class ImageKeyboardResponsePlugin {
195
208
  var trial_data = {
196
209
  rt: response.rt,
197
210
  stimulus: trial.stimulus,
198
- response: response.key
211
+ response: response.key,
212
+ rt_key_duration: response.rt_key_duration
199
213
  };
200
214
  this.jsPsych.finishTrial(trial_data);
201
215
  };
202
216
  var after_response = (info2) => {
203
217
  display_element.querySelector("#jspsych-image-keyboard-response-stimulus").className += " responded";
204
218
  if (response.key == null) {
205
- response = info2;
219
+ response = {
220
+ rt: info2.rt,
221
+ key: info2.key,
222
+ rt_key_duration: info2.rt_key_duration ?? null
223
+ };
206
224
  }
207
225
  if (trial.response_ends_trial) {
208
226
  end_trial();
@@ -214,7 +232,8 @@ class ImageKeyboardResponsePlugin {
214
232
  valid_responses: trial.choices,
215
233
  rt_method: "performance",
216
234
  persist: false,
217
- allow_held_key: false
235
+ allow_held_key: false,
236
+ wait_for_key_release: trial.wait_for_key_release
218
237
  });
219
238
  }
220
239
  if (trial.stimulus_duration !== null) {
@@ -260,14 +279,15 @@ class ImageKeyboardResponsePlugin {
260
279
  const default_data = {
261
280
  stimulus: trial.stimulus,
262
281
  rt: this.jsPsych.randomization.sampleExGaussian(500, 50, 1 / 150, true),
263
- response: this.jsPsych.pluginAPI.getValidKey(trial.choices)
282
+ response: this.jsPsych.pluginAPI.getValidKey(trial.choices),
283
+ rt_key_duration: null
264
284
  };
285
+ default_data.rt_key_duration = default_data.rt === null || !trial.wait_for_key_release ? null : this.jsPsych.randomization.sampleExGaussian(150, 30, 1 / 100, true);
265
286
  const data = this.jsPsych.pluginAPI.mergeSimulationData(default_data, simulation_options);
266
287
  this.jsPsych.pluginAPI.ensureSimulationDataConsistency(trial, data);
267
288
  return data;
268
289
  }
269
290
  }
270
- ImageKeyboardResponsePlugin.info = info;
271
291
 
272
292
  export { ImageKeyboardResponsePlugin as default };
273
293
  //# sourceMappingURL=index.js.map