@jspsych/plugin-image-keyboard-response 2.1.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.
@@ -1,7 +1,7 @@
1
1
  var jsPsychImageKeyboardResponse = (function (jspsych) {
2
2
  'use strict';
3
3
 
4
- var version = "2.1.0";
4
+ var version = "2.2.0";
5
5
 
6
6
  const info = {
7
7
  name: "image-keyboard-response",
@@ -72,6 +72,18 @@ var jsPsychImageKeyboardResponse = (function (jspsych) {
72
72
  render_on_canvas: {
73
73
  type: jspsych.ParameterType.BOOL,
74
74
  default: true
75
+ },
76
+ /**
77
+ * If true, the response is not registered until the participant releases the key. The response
78
+ * time (`rt`) still reflects when the key was pressed, and the additional data field
79
+ * `rt_key_duration` records how long the key was held down. Note that when this is true, the
80
+ * trial cannot end until the key is released: with `response_ends_trial: true` the trial ends at
81
+ * the key release, and if the trial ends for another reason (e.g., `trial_duration`) while the
82
+ * key is still held, no response is recorded for the trial.
83
+ */
84
+ wait_for_key_release: {
85
+ type: jspsych.ParameterType.BOOL,
86
+ default: false
75
87
  }
76
88
  },
77
89
  data: {
@@ -87,6 +99,10 @@ var jsPsychImageKeyboardResponse = (function (jspsych) {
87
99
  * first appears on the screen until the participant's response. */
88
100
  rt: {
89
101
  type: jspsych.ParameterType.INT
102
+ },
103
+ /** 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. */
104
+ rt_key_duration: {
105
+ type: jspsych.ParameterType.INT
90
106
  }
91
107
  },
92
108
  // prettier-ignore
@@ -183,7 +199,8 @@ var jsPsychImageKeyboardResponse = (function (jspsych) {
183
199
  }
184
200
  var response = {
185
201
  rt: null,
186
- key: null
202
+ key: null,
203
+ rt_key_duration: null
187
204
  };
188
205
  const end_trial = () => {
189
206
  if (typeof keyboardListener !== "undefined") {
@@ -192,14 +209,19 @@ var jsPsychImageKeyboardResponse = (function (jspsych) {
192
209
  var trial_data = {
193
210
  rt: response.rt,
194
211
  stimulus: trial.stimulus,
195
- response: response.key
212
+ response: response.key,
213
+ rt_key_duration: response.rt_key_duration
196
214
  };
197
215
  this.jsPsych.finishTrial(trial_data);
198
216
  };
199
217
  var after_response = (info2) => {
200
218
  display_element.querySelector("#jspsych-image-keyboard-response-stimulus").className += " responded";
201
219
  if (response.key == null) {
202
- response = info2;
220
+ response = {
221
+ rt: info2.rt,
222
+ key: info2.key,
223
+ rt_key_duration: info2.rt_key_duration ?? null
224
+ };
203
225
  }
204
226
  if (trial.response_ends_trial) {
205
227
  end_trial();
@@ -211,7 +233,8 @@ var jsPsychImageKeyboardResponse = (function (jspsych) {
211
233
  valid_responses: trial.choices,
212
234
  rt_method: "performance",
213
235
  persist: false,
214
- allow_held_key: false
236
+ allow_held_key: false,
237
+ wait_for_key_release: trial.wait_for_key_release
215
238
  });
216
239
  }
217
240
  if (trial.stimulus_duration !== null) {
@@ -257,8 +280,10 @@ var jsPsychImageKeyboardResponse = (function (jspsych) {
257
280
  const default_data = {
258
281
  stimulus: trial.stimulus,
259
282
  rt: this.jsPsych.randomization.sampleExGaussian(500, 50, 1 / 150, true),
260
- response: this.jsPsych.pluginAPI.getValidKey(trial.choices)
283
+ response: this.jsPsych.pluginAPI.getValidKey(trial.choices),
284
+ rt_key_duration: null
261
285
  };
286
+ default_data.rt_key_duration = default_data.rt === null || !trial.wait_for_key_release ? null : this.jsPsych.randomization.sampleExGaussian(150, 30, 1 / 100, true);
262
287
  const data = this.jsPsych.pluginAPI.mergeSimulationData(default_data, simulation_options);
263
288
  this.jsPsych.pluginAPI.ensureSimulationDataConsistency(trial, data);
264
289
  return data;
@@ -268,4 +293,4 @@ var jsPsychImageKeyboardResponse = (function (jspsych) {
268
293
  return ImageKeyboardResponsePlugin;
269
294
 
270
295
  })(jsPsychModule);
271
- //# sourceMappingURL=https://unpkg.com/@jspsych/plugin-image-keyboard-response@2.1.0/dist/index.browser.js.map
296
+ //# sourceMappingURL=https://unpkg.com/@jspsych/plugin-image-keyboard-response@2.2.0/dist/index.browser.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.browser.js","sources":["../package.json","../src/index.ts"],"sourcesContent":["{\n \"name\": \"@jspsych/plugin-image-keyboard-response\",\n \"version\": \"2.1.0\",\n \"description\": \"jsPsych plugin for displaying a stimulus and getting a keyboard response\",\n \"type\": \"module\",\n \"main\": \"dist/index.cjs\",\n \"exports\": {\n \"import\": \"./dist/index.js\",\n \"require\": \"./dist/index.cjs\"\n },\n \"typings\": \"dist/index.d.ts\",\n \"unpkg\": \"dist/index.browser.min.js\",\n \"files\": [\n \"src\",\n \"dist\"\n ],\n \"source\": \"src/index.ts\",\n \"scripts\": {\n \"test\": \"jest\",\n \"test:watch\": \"npm test -- --watch\",\n \"tsc\": \"tsc\",\n \"build\": \"rollup --config\",\n \"build:watch\": \"npm run build -- --watch\"\n },\n \"repository\": {\n \"type\": \"git\",\n \"url\": \"git+https://github.com/jspsych/jsPsych.git\",\n \"directory\": \"packages/plugin-image-keyboard-response\"\n },\n \"author\": \"Josh de Leeuw\",\n \"license\": \"MIT\",\n \"bugs\": {\n \"url\": \"https://github.com/jspsych/jsPsych/issues\"\n },\n \"homepage\": \"https://www.jspsych.org/latest/plugins/image-keyboard-response\",\n \"peerDependencies\": {\n \"jspsych\": \">=7.1.0\"\n },\n \"devDependencies\": {\n \"@jspsych/config\": \"^3.2.0\",\n \"@jspsych/test-utils\": \"^1.2.0\"\n }\n}\n","import { JsPsych, JsPsychPlugin, ParameterType, TrialType } from \"jspsych\";\n\nimport { version } from \"../package.json\";\n\nconst info = <const>{\n name: \"image-keyboard-response\",\n version: version,\n parameters: {\n /** The path of the image file to be displayed. */\n stimulus: {\n type: ParameterType.IMAGE,\n default: undefined,\n },\n /** Set the height of the image in pixels. If left null (no value specified), then the image will display at its natural height. */\n stimulus_height: {\n type: ParameterType.INT,\n default: null,\n },\n /** Set the width of the image in pixels. If left null (no value specified), then the image will display at its natural width. */\n stimulus_width: {\n type: ParameterType.INT,\n default: null,\n },\n /** If setting *only* the width or *only* the height and this parameter is true, then the other dimension will be scaled\n * to maintain the image's aspect ratio. */\n maintain_aspect_ratio: {\n type: ParameterType.BOOL,\n default: true,\n },\n /**his array contains the key(s) that the participant is allowed to press in order to respond to the stimulus. Keys should\n * be specified as characters (e.g., `'a'`, `'q'`, `' '`, `'Enter'`, `'ArrowDown'`) - see\n * [this page](https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/key/Key_Values) and\n * [this page (event.key column)](https://www.freecodecamp.org/news/javascript-keycode-list-keypress-event-key-codes/)\n * for more examples. Any key presses that are not listed in the array will be ignored. The default value of `\"ALL_KEYS\"`\n * means that all keys will be accepted as valid responses. Specifying `\"NO_KEYS\"` will mean that no responses are allowed. */\n choices: {\n type: ParameterType.KEYS,\n default: \"ALL_KEYS\",\n },\n /**This string can contain HTML markup. Any content here will be displayed below the stimulus. The intention is that it can\n * be used to provide a reminder about the action the participant is supposed to take (e.g., which key to press). */\n prompt: {\n type: ParameterType.HTML_STRING,\n default: null,\n },\n /** How long to show the stimulus for in milliseconds. If the value is `null`, then the stimulus will be shown until the\n * participant makes a response. */\n stimulus_duration: {\n type: ParameterType.INT,\n default: null,\n },\n /** How long to wait for the participant to make a response before ending the trial in milliseconds. If the participant\n * fails to make a response before this timer is reached, the participant's response will be recorded as null for the\n * trial and the trial will end. If the value of this parameter is `null`, then the trial will wait for a response indefinitely. */\n trial_duration: {\n type: ParameterType.INT,\n default: null,\n },\n /** If true, then the trial will end whenever the participant makes a response (assuming they make their response before\n * the cutoff specified by the `trial_duration` parameter). If false, then the trial will continue until the value for\n * `trial_duration` is reached. You can set this parameter to `false` to force the participant to view a stimulus for a\n * fixed amount of time, even if they respond before the time is complete. */\n response_ends_trial: {\n type: ParameterType.BOOL,\n default: true,\n },\n /**\n * If `true`, the image will be drawn onto a canvas element. This prevents a blank screen (white flash) between consecutive image trials in some browsers, like Firefox and Edge.\n * If `false`, the image will be shown via an img element, as in previous versions of jsPsych. If the stimulus is an **animated gif**, you must set this parameter to false, because the canvas rendering method will only present static images.\n */\n render_on_canvas: {\n type: ParameterType.BOOL,\n default: true,\n },\n },\n data: {\n /** The path of the image that was displayed. */\n stimulus: {\n type: ParameterType.STRING,\n },\n /** Indicates which key the participant pressed. */\n response: {\n type: ParameterType.STRING,\n },\n /** The response time in milliseconds for the participant to make a response. The time is measured from when the stimulus\n * first appears on the screen until the participant's response. */\n rt: {\n type: ParameterType.INT,\n },\n },\n // prettier-ignore\n citations: '__CITATIONS__',\n};\n\ntype Info = typeof info;\n\n/**\n * This plugin displays an image and records responses generated with the keyboard. The stimulus can be displayed until a\n * response is given, or for a pre-determined amount of time. The trial can be ended automatically if the participant has\n * failed to respond within a fixed length of time.\n *\n * Image files can be automatically preloaded by jsPsych using the [`preload` plugin](preload.md). However, if you are using\n * timeline variables or another dynamic method to specify the image stimulus, you will need to\n * [manually preload](../overview/media-preloading.md#manual-preloading) the images.\n *\n * @author Josh de Leeuw\n * @see {@link https://www.jspsych.org/latest/plugins/image-keyboard-response/ image-keyboard-response plugin documentation on jspsych.org}\n */\nclass ImageKeyboardResponsePlugin implements JsPsychPlugin<Info> {\n static info = info;\n\n constructor(private jsPsych: JsPsych) {}\n\n trial(display_element: HTMLElement, trial: TrialType<Info>) {\n var height, width;\n if (trial.render_on_canvas) {\n var image_drawn = false;\n // first clear the display element (because the render_on_canvas method appends to display_element instead of overwriting it with .innerHTML)\n if (display_element.hasChildNodes()) {\n // can't loop through child list because the list will be modified by .removeChild()\n while (display_element.firstChild) {\n display_element.removeChild(display_element.firstChild);\n }\n }\n // create canvas element and image\n var canvas = document.createElement(\"canvas\");\n canvas.id = \"jspsych-image-keyboard-response-stimulus\";\n canvas.style.margin = \"0\";\n canvas.style.padding = \"0\";\n var ctx = canvas.getContext(\"2d\");\n var img = new Image();\n img.onload = () => {\n // if image wasn't preloaded, then it will need to be drawn whenever it finishes loading\n if (!image_drawn) {\n getHeightWidth(); // only possible to get width/height after image loads\n ctx.drawImage(img, 0, 0, width, height);\n }\n };\n img.src = trial.stimulus;\n // get/set image height and width - this can only be done after image loads because uses image's naturalWidth/naturalHeight properties\n const getHeightWidth = () => {\n if (trial.stimulus_height !== null) {\n height = trial.stimulus_height;\n if (trial.stimulus_width == null && trial.maintain_aspect_ratio) {\n width = img.naturalWidth * (trial.stimulus_height / img.naturalHeight);\n }\n } else {\n height = img.naturalHeight;\n }\n if (trial.stimulus_width !== null) {\n width = trial.stimulus_width;\n if (trial.stimulus_height == null && trial.maintain_aspect_ratio) {\n height = img.naturalHeight * (trial.stimulus_width / img.naturalWidth);\n }\n } else if (!(trial.stimulus_height !== null && trial.maintain_aspect_ratio)) {\n // if stimulus width is null, only use the image's natural width if the width value wasn't set\n // in the if statement above, based on a specified height and maintain_aspect_ratio = true\n width = img.naturalWidth;\n }\n canvas.height = height;\n canvas.width = width;\n };\n getHeightWidth(); // call now, in case image loads immediately (is cached)\n // add canvas and draw image\n display_element.insertBefore(canvas, null);\n if (img.complete && Number.isFinite(width) && Number.isFinite(height)) {\n // if image has loaded and width/height have been set, then draw it now\n // (don't rely on img onload function to draw image when image is in the cache, because that causes a delay in the image presentation)\n ctx.drawImage(img, 0, 0, width, height);\n image_drawn = true;\n }\n // add prompt if there is one\n if (trial.prompt !== null) {\n display_element.insertAdjacentHTML(\"beforeend\", trial.prompt);\n }\n } else {\n // display stimulus as an image element\n var html = '<img src=\"' + trial.stimulus + '\" id=\"jspsych-image-keyboard-response-stimulus\">';\n // add prompt\n if (trial.prompt !== null) {\n html += trial.prompt;\n }\n // update the page content\n display_element.innerHTML = html;\n\n // set image dimensions after image has loaded (so that we have access to naturalHeight/naturalWidth)\n var img = display_element.querySelector(\n \"#jspsych-image-keyboard-response-stimulus\"\n ) as HTMLImageElement;\n if (trial.stimulus_height !== null) {\n height = trial.stimulus_height;\n if (trial.stimulus_width == null && trial.maintain_aspect_ratio) {\n width = img.naturalWidth * (trial.stimulus_height / img.naturalHeight);\n }\n } else {\n height = img.naturalHeight;\n }\n if (trial.stimulus_width !== null) {\n width = trial.stimulus_width;\n if (trial.stimulus_height == null && trial.maintain_aspect_ratio) {\n height = img.naturalHeight * (trial.stimulus_width / img.naturalWidth);\n }\n } else if (!(trial.stimulus_height !== null && trial.maintain_aspect_ratio)) {\n // if stimulus width is null, only use the image's natural width if the width value wasn't set\n // in the if statement above, based on a specified height and maintain_aspect_ratio = true\n width = img.naturalWidth;\n }\n img.style.height = height.toString() + \"px\";\n img.style.width = width.toString() + \"px\";\n }\n\n // store response\n var response = {\n rt: null,\n key: null,\n };\n\n // function to end trial when it is time\n const end_trial = () => {\n // kill keyboard listeners\n if (typeof keyboardListener !== \"undefined\") {\n this.jsPsych.pluginAPI.cancelKeyboardResponse(keyboardListener);\n }\n\n // gather the data to store for the trial\n var trial_data = {\n rt: response.rt,\n stimulus: trial.stimulus,\n response: response.key,\n };\n\n // move on to the next trial\n this.jsPsych.finishTrial(trial_data);\n };\n\n // function to handle responses by the subject\n var after_response = (info) => {\n // after a valid response, the stimulus will have the CSS class 'responded'\n // which can be used to provide visual feedback that a response was recorded\n display_element.querySelector(\"#jspsych-image-keyboard-response-stimulus\").className +=\n \" responded\";\n\n // only record the first response\n if (response.key == null) {\n response = info;\n }\n\n if (trial.response_ends_trial) {\n end_trial();\n }\n };\n\n // start the response listener\n if (trial.choices != \"NO_KEYS\") {\n var keyboardListener = this.jsPsych.pluginAPI.getKeyboardResponse({\n callback_function: after_response,\n valid_responses: trial.choices,\n rt_method: \"performance\",\n persist: false,\n allow_held_key: false,\n });\n }\n\n // hide stimulus if stimulus_duration is set\n if (trial.stimulus_duration !== null) {\n this.jsPsych.pluginAPI.setTimeout(() => {\n display_element.querySelector<HTMLElement>(\n \"#jspsych-image-keyboard-response-stimulus\"\n ).style.visibility = \"hidden\";\n }, trial.stimulus_duration);\n }\n\n // end trial if trial_duration is set\n if (trial.trial_duration !== null) {\n this.jsPsych.pluginAPI.setTimeout(() => {\n end_trial();\n }, trial.trial_duration);\n } else if (trial.response_ends_trial === false) {\n console.warn(\n \"The experiment may be deadlocked. Try setting a trial duration or set response_ends_trial to true.\"\n );\n }\n }\n\n simulate(\n trial: TrialType<Info>,\n simulation_mode,\n simulation_options: any,\n load_callback: () => void\n ) {\n if (simulation_mode == \"data-only\") {\n load_callback();\n this.simulate_data_only(trial, simulation_options);\n }\n if (simulation_mode == \"visual\") {\n this.simulate_visual(trial, simulation_options, load_callback);\n }\n }\n\n private simulate_data_only(trial: TrialType<Info>, simulation_options) {\n const data = this.create_simulation_data(trial, simulation_options);\n\n this.jsPsych.finishTrial(data);\n }\n\n private simulate_visual(trial: TrialType<Info>, simulation_options, load_callback: () => void) {\n const data = this.create_simulation_data(trial, simulation_options);\n\n const display_element = this.jsPsych.getDisplayElement();\n\n this.trial(display_element, trial);\n load_callback();\n\n if (data.rt !== null) {\n this.jsPsych.pluginAPI.pressKey(data.response, data.rt);\n }\n }\n\n private create_simulation_data(trial: TrialType<Info>, simulation_options) {\n const default_data = {\n stimulus: trial.stimulus,\n rt: this.jsPsych.randomization.sampleExGaussian(500, 50, 1 / 150, true),\n response: this.jsPsych.pluginAPI.getValidKey(trial.choices),\n };\n\n const data = this.jsPsych.pluginAPI.mergeSimulationData(default_data, simulation_options);\n\n this.jsPsych.pluginAPI.ensureSimulationDataConsistency(trial, data);\n\n return data;\n }\n}\n\nexport default ImageKeyboardResponsePlugin;\n"],"names":[],"mappings":";;;EAEE,IAAW,OAAA,GAAA,OAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;ICyFA,SAAA,EAAA;EAAA;;KAAe;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
1
+ {"version":3,"file":"index.browser.js","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":";;;EAEE,IAAA,OAAA,GAAW,OAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;ICyGA,SAAA,EAAA;EAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
@@ -1,2 +1,2 @@
1
- var jsPsychImageKeyboardResponse=function(r){"use strict";var y="2.1.0";const _={name:"image-keyboard-response",version:y,parameters:{stimulus:{type:r.ParameterType.IMAGE,default:void 0},stimulus_height:{type:r.ParameterType.INT,default:null},stimulus_width:{type:r.ParameterType.INT,default:null},maintain_aspect_ratio:{type:r.ParameterType.BOOL,default:!0},choices:{type:r.ParameterType.KEYS,default:"ALL_KEYS"},prompt:{type:r.ParameterType.HTML_STRING,default:null},stimulus_duration:{type:r.ParameterType.INT,default:null},trial_duration:{type:r.ParameterType.INT,default:null},response_ends_trial:{type:r.ParameterType.BOOL,default:!0},render_on_canvas:{type:r.ParameterType.BOOL,default:!0}},data:{stimulus:{type:r.ParameterType.STRING},response:{type:r.ParameterType.STRING},rt:{type:r.ParameterType.INT}},citations:{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 ",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}, } '}};class m{constructor(e){this.jsPsych=e}trial(e,t){var s,i;if(t.render_on_canvas){var u=!1;if(e.hasChildNodes())for(;e.firstChild;)e.removeChild(e.firstChild);var l=document.createElement("canvas");l.id="jspsych-image-keyboard-response-stimulus",l.style.margin="0",l.style.padding="0";var h=l.getContext("2d"),a=new Image;a.onload=()=>{u||(n(),h.drawImage(a,0,0,i,s))},a.src=t.stimulus;const n=()=>{t.stimulus_height!==null?(s=t.stimulus_height,t.stimulus_width==null&&t.maintain_aspect_ratio&&(i=a.naturalWidth*(t.stimulus_height/a.naturalHeight))):s=a.naturalHeight,t.stimulus_width!==null?(i=t.stimulus_width,t.stimulus_height==null&&t.maintain_aspect_ratio&&(s=a.naturalHeight*(t.stimulus_width/a.naturalWidth))):t.stimulus_height!==null&&t.maintain_aspect_ratio||(i=a.naturalWidth),l.height=s,l.width=i};n(),e.insertBefore(l,null),a.complete&&Number.isFinite(i)&&Number.isFinite(s)&&(h.drawImage(a,0,0,i,s),u=!0),t.prompt!==null&&e.insertAdjacentHTML("beforeend",t.prompt)}else{var d='<img src="'+t.stimulus+'" id="jspsych-image-keyboard-response-stimulus">';t.prompt!==null&&(d+=t.prompt),e.innerHTML=d;var a=e.querySelector("#jspsych-image-keyboard-response-stimulus");t.stimulus_height!==null?(s=t.stimulus_height,t.stimulus_width==null&&t.maintain_aspect_ratio&&(i=a.naturalWidth*(t.stimulus_height/a.naturalHeight))):s=a.naturalHeight,t.stimulus_width!==null?(i=t.stimulus_width,t.stimulus_height==null&&t.maintain_aspect_ratio&&(s=a.naturalHeight*(t.stimulus_width/a.naturalWidth))):t.stimulus_height!==null&&t.maintain_aspect_ratio||(i=a.naturalWidth),a.style.height=s.toString()+"px",a.style.width=i.toString()+"px"}var o={rt:null,key:null};const p=()=>{typeof c!="undefined"&&this.jsPsych.pluginAPI.cancelKeyboardResponse(c);var n={rt:o.rt,stimulus:t.stimulus,response:o.key};this.jsPsych.finishTrial(n)};var g=n=>{e.querySelector("#jspsych-image-keyboard-response-stimulus").className+=" responded",o.key==null&&(o=n),t.response_ends_trial&&p()};if(t.choices!="NO_KEYS")var c=this.jsPsych.pluginAPI.getKeyboardResponse({callback_function:g,valid_responses:t.choices,rt_method:"performance",persist:!1,allow_held_key:!1});t.stimulus_duration!==null&&this.jsPsych.pluginAPI.setTimeout(()=>{e.querySelector("#jspsych-image-keyboard-response-stimulus").style.visibility="hidden"},t.stimulus_duration),t.trial_duration!==null?this.jsPsych.pluginAPI.setTimeout(()=>{p()},t.trial_duration):t.response_ends_trial===!1&&console.warn("The experiment may be deadlocked. Try setting a trial duration or set response_ends_trial to true.")}simulate(e,t,s,i){t=="data-only"&&(i(),this.simulate_data_only(e,s)),t=="visual"&&this.simulate_visual(e,s,i)}simulate_data_only(e,t){const s=this.create_simulation_data(e,t);this.jsPsych.finishTrial(s)}simulate_visual(e,t,s){const i=this.create_simulation_data(e,t),u=this.jsPsych.getDisplayElement();this.trial(u,e),s(),i.rt!==null&&this.jsPsych.pluginAPI.pressKey(i.response,i.rt)}create_simulation_data(e,t){const s={stimulus:e.stimulus,rt:this.jsPsych.randomization.sampleExGaussian(500,50,.006666666666666667,!0),response:this.jsPsych.pluginAPI.getValidKey(e.choices)},i=this.jsPsych.pluginAPI.mergeSimulationData(s,t);return this.jsPsych.pluginAPI.ensureSimulationDataConsistency(e,i),i}}return m.info=_,m}(jsPsychModule);
2
- //# sourceMappingURL=https://unpkg.com/@jspsych/plugin-image-keyboard-response@2.1.0/dist/index.browser.min.js.map
1
+ var jsPsychImageKeyboardResponse=(function(r){"use strict";var c="2.2.0";const g={name:"image-keyboard-response",version:c,parameters:{stimulus:{type:r.ParameterType.IMAGE,default:void 0},stimulus_height:{type:r.ParameterType.INT,default:null},stimulus_width:{type:r.ParameterType.INT,default:null},maintain_aspect_ratio:{type:r.ParameterType.BOOL,default:!0},choices:{type:r.ParameterType.KEYS,default:"ALL_KEYS"},prompt:{type:r.ParameterType.HTML_STRING,default:null},stimulus_duration:{type:r.ParameterType.INT,default:null},trial_duration:{type:r.ParameterType.INT,default:null},response_ends_trial:{type:r.ParameterType.BOOL,default:!0},render_on_canvas:{type:r.ParameterType.BOOL,default:!0},wait_for_key_release:{type:r.ParameterType.BOOL,default:!1}},data:{stimulus:{type:r.ParameterType.STRING},response:{type:r.ParameterType.STRING},rt:{type:r.ParameterType.INT},rt_key_duration:{type:r.ParameterType.INT}},citations:{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 ",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}, } '}};class m{constructor(e){this.jsPsych=e}trial(e,t){var a,s;if(t.render_on_canvas){var o=!1;if(e.hasChildNodes())for(;e.firstChild;)e.removeChild(e.firstChild);var n=document.createElement("canvas");n.id="jspsych-image-keyboard-response-stimulus",n.style.margin="0",n.style.padding="0";var d=n.getContext("2d"),i=new Image;i.onload=()=>{o||(l(),d.drawImage(i,0,0,s,a))},i.src=t.stimulus;const l=()=>{t.stimulus_height!==null?(a=t.stimulus_height,t.stimulus_width==null&&t.maintain_aspect_ratio&&(s=i.naturalWidth*(t.stimulus_height/i.naturalHeight))):a=i.naturalHeight,t.stimulus_width!==null?(s=t.stimulus_width,t.stimulus_height==null&&t.maintain_aspect_ratio&&(a=i.naturalHeight*(t.stimulus_width/i.naturalWidth))):t.stimulus_height!==null&&t.maintain_aspect_ratio||(s=i.naturalWidth),n.height=a,n.width=s};l(),e.insertBefore(n,null),i.complete&&Number.isFinite(s)&&Number.isFinite(a)&&(d.drawImage(i,0,0,s,a),o=!0),t.prompt!==null&&e.insertAdjacentHTML("beforeend",t.prompt)}else{var h='<img src="'+t.stimulus+'" id="jspsych-image-keyboard-response-stimulus">';t.prompt!==null&&(h+=t.prompt),e.innerHTML=h;var i=e.querySelector("#jspsych-image-keyboard-response-stimulus");t.stimulus_height!==null?(a=t.stimulus_height,t.stimulus_width==null&&t.maintain_aspect_ratio&&(s=i.naturalWidth*(t.stimulus_height/i.naturalHeight))):a=i.naturalHeight,t.stimulus_width!==null?(s=t.stimulus_width,t.stimulus_height==null&&t.maintain_aspect_ratio&&(a=i.naturalHeight*(t.stimulus_width/i.naturalWidth))):t.stimulus_height!==null&&t.maintain_aspect_ratio||(s=i.naturalWidth),i.style.height=a.toString()+"px",i.style.width=s.toString()+"px"}var u={rt:null,key:null,rt_key_duration:null};const _=()=>{typeof p!="undefined"&&this.jsPsych.pluginAPI.cancelKeyboardResponse(p);var l={rt:u.rt,stimulus:t.stimulus,response:u.key,rt_key_duration:u.rt_key_duration};this.jsPsych.finishTrial(l)};var f=l=>{var y;e.querySelector("#jspsych-image-keyboard-response-stimulus").className+=" responded",u.key==null&&(u={rt:l.rt,key:l.key,rt_key_duration:(y=l.rt_key_duration)!=null?y:null}),t.response_ends_trial&&_()};if(t.choices!="NO_KEYS")var p=this.jsPsych.pluginAPI.getKeyboardResponse({callback_function:f,valid_responses:t.choices,rt_method:"performance",persist:!1,allow_held_key:!1,wait_for_key_release:t.wait_for_key_release});t.stimulus_duration!==null&&this.jsPsych.pluginAPI.setTimeout(()=>{e.querySelector("#jspsych-image-keyboard-response-stimulus").style.visibility="hidden"},t.stimulus_duration),t.trial_duration!==null?this.jsPsych.pluginAPI.setTimeout(()=>{_()},t.trial_duration):t.response_ends_trial===!1&&console.warn("The experiment may be deadlocked. Try setting a trial duration or set response_ends_trial to true.")}simulate(e,t,a,s){t=="data-only"&&(s(),this.simulate_data_only(e,a)),t=="visual"&&this.simulate_visual(e,a,s)}simulate_data_only(e,t){const a=this.create_simulation_data(e,t);this.jsPsych.finishTrial(a)}simulate_visual(e,t,a){const s=this.create_simulation_data(e,t),o=this.jsPsych.getDisplayElement();this.trial(o,e),a(),s.rt!==null&&this.jsPsych.pluginAPI.pressKey(s.response,s.rt)}create_simulation_data(e,t){const a={stimulus:e.stimulus,rt:this.jsPsych.randomization.sampleExGaussian(500,50,.006666666666666667,!0),response:this.jsPsych.pluginAPI.getValidKey(e.choices),rt_key_duration:null};a.rt_key_duration=a.rt===null||!e.wait_for_key_release?null:this.jsPsych.randomization.sampleExGaussian(150,30,.01,!0);const s=this.jsPsych.pluginAPI.mergeSimulationData(a,t);return this.jsPsych.pluginAPI.ensureSimulationDataConsistency(e,s),s}}return m.info=g,m})(jsPsychModule);
2
+ //# sourceMappingURL=https://unpkg.com/@jspsych/plugin-image-keyboard-response@2.2.0/dist/index.browser.min.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.browser.min.js","sources":["../package.json","../src/index.ts"],"sourcesContent":["{\n \"name\": \"@jspsych/plugin-image-keyboard-response\",\n \"version\": \"2.1.0\",\n \"description\": \"jsPsych plugin for displaying a stimulus and getting a keyboard response\",\n \"type\": \"module\",\n \"main\": \"dist/index.cjs\",\n \"exports\": {\n \"import\": \"./dist/index.js\",\n \"require\": \"./dist/index.cjs\"\n },\n \"typings\": \"dist/index.d.ts\",\n \"unpkg\": \"dist/index.browser.min.js\",\n \"files\": [\n \"src\",\n \"dist\"\n ],\n \"source\": \"src/index.ts\",\n \"scripts\": {\n \"test\": \"jest\",\n \"test:watch\": \"npm test -- --watch\",\n \"tsc\": \"tsc\",\n \"build\": \"rollup --config\",\n \"build:watch\": \"npm run build -- --watch\"\n },\n \"repository\": {\n \"type\": \"git\",\n \"url\": \"git+https://github.com/jspsych/jsPsych.git\",\n \"directory\": \"packages/plugin-image-keyboard-response\"\n },\n \"author\": \"Josh de Leeuw\",\n \"license\": \"MIT\",\n \"bugs\": {\n \"url\": \"https://github.com/jspsych/jsPsych/issues\"\n },\n \"homepage\": \"https://www.jspsych.org/latest/plugins/image-keyboard-response\",\n \"peerDependencies\": {\n \"jspsych\": \">=7.1.0\"\n },\n \"devDependencies\": {\n \"@jspsych/config\": \"^3.2.0\",\n \"@jspsych/test-utils\": \"^1.2.0\"\n }\n}\n","import { JsPsych, JsPsychPlugin, ParameterType, TrialType } from \"jspsych\";\n\nimport { version } from \"../package.json\";\n\nconst info = <const>{\n name: \"image-keyboard-response\",\n version: version,\n parameters: {\n /** The path of the image file to be displayed. */\n stimulus: {\n type: ParameterType.IMAGE,\n default: undefined,\n },\n /** Set the height of the image in pixels. If left null (no value specified), then the image will display at its natural height. */\n stimulus_height: {\n type: ParameterType.INT,\n default: null,\n },\n /** Set the width of the image in pixels. If left null (no value specified), then the image will display at its natural width. */\n stimulus_width: {\n type: ParameterType.INT,\n default: null,\n },\n /** If setting *only* the width or *only* the height and this parameter is true, then the other dimension will be scaled\n * to maintain the image's aspect ratio. */\n maintain_aspect_ratio: {\n type: ParameterType.BOOL,\n default: true,\n },\n /**his array contains the key(s) that the participant is allowed to press in order to respond to the stimulus. Keys should\n * be specified as characters (e.g., `'a'`, `'q'`, `' '`, `'Enter'`, `'ArrowDown'`) - see\n * [this page](https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/key/Key_Values) and\n * [this page (event.key column)](https://www.freecodecamp.org/news/javascript-keycode-list-keypress-event-key-codes/)\n * for more examples. Any key presses that are not listed in the array will be ignored. The default value of `\"ALL_KEYS\"`\n * means that all keys will be accepted as valid responses. Specifying `\"NO_KEYS\"` will mean that no responses are allowed. */\n choices: {\n type: ParameterType.KEYS,\n default: \"ALL_KEYS\",\n },\n /**This string can contain HTML markup. Any content here will be displayed below the stimulus. The intention is that it can\n * be used to provide a reminder about the action the participant is supposed to take (e.g., which key to press). */\n prompt: {\n type: ParameterType.HTML_STRING,\n default: null,\n },\n /** How long to show the stimulus for in milliseconds. If the value is `null`, then the stimulus will be shown until the\n * participant makes a response. */\n stimulus_duration: {\n type: ParameterType.INT,\n default: null,\n },\n /** How long to wait for the participant to make a response before ending the trial in milliseconds. If the participant\n * fails to make a response before this timer is reached, the participant's response will be recorded as null for the\n * trial and the trial will end. If the value of this parameter is `null`, then the trial will wait for a response indefinitely. */\n trial_duration: {\n type: ParameterType.INT,\n default: null,\n },\n /** If true, then the trial will end whenever the participant makes a response (assuming they make their response before\n * the cutoff specified by the `trial_duration` parameter). If false, then the trial will continue until the value for\n * `trial_duration` is reached. You can set this parameter to `false` to force the participant to view a stimulus for a\n * fixed amount of time, even if they respond before the time is complete. */\n response_ends_trial: {\n type: ParameterType.BOOL,\n default: true,\n },\n /**\n * If `true`, the image will be drawn onto a canvas element. This prevents a blank screen (white flash) between consecutive image trials in some browsers, like Firefox and Edge.\n * If `false`, the image will be shown via an img element, as in previous versions of jsPsych. If the stimulus is an **animated gif**, you must set this parameter to false, because the canvas rendering method will only present static images.\n */\n render_on_canvas: {\n type: ParameterType.BOOL,\n default: true,\n },\n },\n data: {\n /** The path of the image that was displayed. */\n stimulus: {\n type: ParameterType.STRING,\n },\n /** Indicates which key the participant pressed. */\n response: {\n type: ParameterType.STRING,\n },\n /** The response time in milliseconds for the participant to make a response. The time is measured from when the stimulus\n * first appears on the screen until the participant's response. */\n rt: {\n type: ParameterType.INT,\n },\n },\n // prettier-ignore\n citations: '__CITATIONS__',\n};\n\ntype Info = typeof info;\n\n/**\n * This plugin displays an image and records responses generated with the keyboard. The stimulus can be displayed until a\n * response is given, or for a pre-determined amount of time. The trial can be ended automatically if the participant has\n * failed to respond within a fixed length of time.\n *\n * Image files can be automatically preloaded by jsPsych using the [`preload` plugin](preload.md). However, if you are using\n * timeline variables or another dynamic method to specify the image stimulus, you will need to\n * [manually preload](../overview/media-preloading.md#manual-preloading) the images.\n *\n * @author Josh de Leeuw\n * @see {@link https://www.jspsych.org/latest/plugins/image-keyboard-response/ image-keyboard-response plugin documentation on jspsych.org}\n */\nclass ImageKeyboardResponsePlugin implements JsPsychPlugin<Info> {\n static info = info;\n\n constructor(private jsPsych: JsPsych) {}\n\n trial(display_element: HTMLElement, trial: TrialType<Info>) {\n var height, width;\n if (trial.render_on_canvas) {\n var image_drawn = false;\n // first clear the display element (because the render_on_canvas method appends to display_element instead of overwriting it with .innerHTML)\n if (display_element.hasChildNodes()) {\n // can't loop through child list because the list will be modified by .removeChild()\n while (display_element.firstChild) {\n display_element.removeChild(display_element.firstChild);\n }\n }\n // create canvas element and image\n var canvas = document.createElement(\"canvas\");\n canvas.id = \"jspsych-image-keyboard-response-stimulus\";\n canvas.style.margin = \"0\";\n canvas.style.padding = \"0\";\n var ctx = canvas.getContext(\"2d\");\n var img = new Image();\n img.onload = () => {\n // if image wasn't preloaded, then it will need to be drawn whenever it finishes loading\n if (!image_drawn) {\n getHeightWidth(); // only possible to get width/height after image loads\n ctx.drawImage(img, 0, 0, width, height);\n }\n };\n img.src = trial.stimulus;\n // get/set image height and width - this can only be done after image loads because uses image's naturalWidth/naturalHeight properties\n const getHeightWidth = () => {\n if (trial.stimulus_height !== null) {\n height = trial.stimulus_height;\n if (trial.stimulus_width == null && trial.maintain_aspect_ratio) {\n width = img.naturalWidth * (trial.stimulus_height / img.naturalHeight);\n }\n } else {\n height = img.naturalHeight;\n }\n if (trial.stimulus_width !== null) {\n width = trial.stimulus_width;\n if (trial.stimulus_height == null && trial.maintain_aspect_ratio) {\n height = img.naturalHeight * (trial.stimulus_width / img.naturalWidth);\n }\n } else if (!(trial.stimulus_height !== null && trial.maintain_aspect_ratio)) {\n // if stimulus width is null, only use the image's natural width if the width value wasn't set\n // in the if statement above, based on a specified height and maintain_aspect_ratio = true\n width = img.naturalWidth;\n }\n canvas.height = height;\n canvas.width = width;\n };\n getHeightWidth(); // call now, in case image loads immediately (is cached)\n // add canvas and draw image\n display_element.insertBefore(canvas, null);\n if (img.complete && Number.isFinite(width) && Number.isFinite(height)) {\n // if image has loaded and width/height have been set, then draw it now\n // (don't rely on img onload function to draw image when image is in the cache, because that causes a delay in the image presentation)\n ctx.drawImage(img, 0, 0, width, height);\n image_drawn = true;\n }\n // add prompt if there is one\n if (trial.prompt !== null) {\n display_element.insertAdjacentHTML(\"beforeend\", trial.prompt);\n }\n } else {\n // display stimulus as an image element\n var html = '<img src=\"' + trial.stimulus + '\" id=\"jspsych-image-keyboard-response-stimulus\">';\n // add prompt\n if (trial.prompt !== null) {\n html += trial.prompt;\n }\n // update the page content\n display_element.innerHTML = html;\n\n // set image dimensions after image has loaded (so that we have access to naturalHeight/naturalWidth)\n var img = display_element.querySelector(\n \"#jspsych-image-keyboard-response-stimulus\"\n ) as HTMLImageElement;\n if (trial.stimulus_height !== null) {\n height = trial.stimulus_height;\n if (trial.stimulus_width == null && trial.maintain_aspect_ratio) {\n width = img.naturalWidth * (trial.stimulus_height / img.naturalHeight);\n }\n } else {\n height = img.naturalHeight;\n }\n if (trial.stimulus_width !== null) {\n width = trial.stimulus_width;\n if (trial.stimulus_height == null && trial.maintain_aspect_ratio) {\n height = img.naturalHeight * (trial.stimulus_width / img.naturalWidth);\n }\n } else if (!(trial.stimulus_height !== null && trial.maintain_aspect_ratio)) {\n // if stimulus width is null, only use the image's natural width if the width value wasn't set\n // in the if statement above, based on a specified height and maintain_aspect_ratio = true\n width = img.naturalWidth;\n }\n img.style.height = height.toString() + \"px\";\n img.style.width = width.toString() + \"px\";\n }\n\n // store response\n var response = {\n rt: null,\n key: null,\n };\n\n // function to end trial when it is time\n const end_trial = () => {\n // kill keyboard listeners\n if (typeof keyboardListener !== \"undefined\") {\n this.jsPsych.pluginAPI.cancelKeyboardResponse(keyboardListener);\n }\n\n // gather the data to store for the trial\n var trial_data = {\n rt: response.rt,\n stimulus: trial.stimulus,\n response: response.key,\n };\n\n // move on to the next trial\n this.jsPsych.finishTrial(trial_data);\n };\n\n // function to handle responses by the subject\n var after_response = (info) => {\n // after a valid response, the stimulus will have the CSS class 'responded'\n // which can be used to provide visual feedback that a response was recorded\n display_element.querySelector(\"#jspsych-image-keyboard-response-stimulus\").className +=\n \" responded\";\n\n // only record the first response\n if (response.key == null) {\n response = info;\n }\n\n if (trial.response_ends_trial) {\n end_trial();\n }\n };\n\n // start the response listener\n if (trial.choices != \"NO_KEYS\") {\n var keyboardListener = this.jsPsych.pluginAPI.getKeyboardResponse({\n callback_function: after_response,\n valid_responses: trial.choices,\n rt_method: \"performance\",\n persist: false,\n allow_held_key: false,\n });\n }\n\n // hide stimulus if stimulus_duration is set\n if (trial.stimulus_duration !== null) {\n this.jsPsych.pluginAPI.setTimeout(() => {\n display_element.querySelector<HTMLElement>(\n \"#jspsych-image-keyboard-response-stimulus\"\n ).style.visibility = \"hidden\";\n }, trial.stimulus_duration);\n }\n\n // end trial if trial_duration is set\n if (trial.trial_duration !== null) {\n this.jsPsych.pluginAPI.setTimeout(() => {\n end_trial();\n }, trial.trial_duration);\n } else if (trial.response_ends_trial === false) {\n console.warn(\n \"The experiment may be deadlocked. Try setting a trial duration or set response_ends_trial to true.\"\n );\n }\n }\n\n simulate(\n trial: TrialType<Info>,\n simulation_mode,\n simulation_options: any,\n load_callback: () => void\n ) {\n if (simulation_mode == \"data-only\") {\n load_callback();\n this.simulate_data_only(trial, simulation_options);\n }\n if (simulation_mode == \"visual\") {\n this.simulate_visual(trial, simulation_options, load_callback);\n }\n }\n\n private simulate_data_only(trial: TrialType<Info>, simulation_options) {\n const data = this.create_simulation_data(trial, simulation_options);\n\n this.jsPsych.finishTrial(data);\n }\n\n private simulate_visual(trial: TrialType<Info>, simulation_options, load_callback: () => void) {\n const data = this.create_simulation_data(trial, simulation_options);\n\n const display_element = this.jsPsych.getDisplayElement();\n\n this.trial(display_element, trial);\n load_callback();\n\n if (data.rt !== null) {\n this.jsPsych.pluginAPI.pressKey(data.response, data.rt);\n }\n }\n\n private create_simulation_data(trial: TrialType<Info>, simulation_options) {\n const default_data = {\n stimulus: trial.stimulus,\n rt: this.jsPsych.randomization.sampleExGaussian(500, 50, 1 / 150, true),\n response: this.jsPsych.pluginAPI.getValidKey(trial.choices),\n };\n\n const data = this.jsPsych.pluginAPI.mergeSimulationData(default_data, simulation_options);\n\n this.jsPsych.pluginAPI.ensureSimulationDataConsistency(trial, data);\n\n return data;\n }\n}\n\nexport default ImageKeyboardResponsePlugin;\n"],"names":["version"],"mappings":"0DAEEA,IAAAA,EAAW,6uBCyFA,UAAA,iuBAAe"}
1
+ {"version":3,"file":"index.browser.min.js","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":["version","_a"],"mappings":"2DACE,IACAA,EAAW,o1BCyGA,UAAA,iuBAAe,61DA3G5B,IAAAC"}
package/dist/index.cjs CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  var jspsych = require('jspsych');
4
4
 
5
- var version = "2.1.0";
5
+ var version = "2.2.0";
6
6
 
7
7
  const info = {
8
8
  name: "image-keyboard-response",
@@ -73,6 +73,18 @@ const info = {
73
73
  render_on_canvas: {
74
74
  type: jspsych.ParameterType.BOOL,
75
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
76
88
  }
77
89
  },
78
90
  data: {
@@ -88,6 +100,10 @@ const info = {
88
100
  * first appears on the screen until the participant's response. */
89
101
  rt: {
90
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
91
107
  }
92
108
  },
93
109
  // prettier-ignore
@@ -184,7 +200,8 @@ class ImageKeyboardResponsePlugin {
184
200
  }
185
201
  var response = {
186
202
  rt: null,
187
- key: null
203
+ key: null,
204
+ rt_key_duration: null
188
205
  };
189
206
  const end_trial = () => {
190
207
  if (typeof keyboardListener !== "undefined") {
@@ -193,14 +210,19 @@ class ImageKeyboardResponsePlugin {
193
210
  var trial_data = {
194
211
  rt: response.rt,
195
212
  stimulus: trial.stimulus,
196
- response: response.key
213
+ response: response.key,
214
+ rt_key_duration: response.rt_key_duration
197
215
  };
198
216
  this.jsPsych.finishTrial(trial_data);
199
217
  };
200
218
  var after_response = (info2) => {
201
219
  display_element.querySelector("#jspsych-image-keyboard-response-stimulus").className += " responded";
202
220
  if (response.key == null) {
203
- response = info2;
221
+ response = {
222
+ rt: info2.rt,
223
+ key: info2.key,
224
+ rt_key_duration: info2.rt_key_duration ?? null
225
+ };
204
226
  }
205
227
  if (trial.response_ends_trial) {
206
228
  end_trial();
@@ -212,7 +234,8 @@ class ImageKeyboardResponsePlugin {
212
234
  valid_responses: trial.choices,
213
235
  rt_method: "performance",
214
236
  persist: false,
215
- allow_held_key: false
237
+ allow_held_key: false,
238
+ wait_for_key_release: trial.wait_for_key_release
216
239
  });
217
240
  }
218
241
  if (trial.stimulus_duration !== null) {
@@ -258,8 +281,10 @@ class ImageKeyboardResponsePlugin {
258
281
  const default_data = {
259
282
  stimulus: trial.stimulus,
260
283
  rt: this.jsPsych.randomization.sampleExGaussian(500, 50, 1 / 150, true),
261
- response: this.jsPsych.pluginAPI.getValidKey(trial.choices)
284
+ response: this.jsPsych.pluginAPI.getValidKey(trial.choices),
285
+ rt_key_duration: null
262
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);
263
288
  const data = this.jsPsych.pluginAPI.mergeSimulationData(default_data, simulation_options);
264
289
  this.jsPsych.pluginAPI.ensureSimulationDataConsistency(trial, data);
265
290
  return data;
@@ -1 +1 @@
1
- {"version":3,"file":"index.cjs","sources":["../package.json","../src/index.ts"],"sourcesContent":["{\n \"name\": \"@jspsych/plugin-image-keyboard-response\",\n \"version\": \"2.1.0\",\n \"description\": \"jsPsych plugin for displaying a stimulus and getting a keyboard response\",\n \"type\": \"module\",\n \"main\": \"dist/index.cjs\",\n \"exports\": {\n \"import\": \"./dist/index.js\",\n \"require\": \"./dist/index.cjs\"\n },\n \"typings\": \"dist/index.d.ts\",\n \"unpkg\": \"dist/index.browser.min.js\",\n \"files\": [\n \"src\",\n \"dist\"\n ],\n \"source\": \"src/index.ts\",\n \"scripts\": {\n \"test\": \"jest\",\n \"test:watch\": \"npm test -- --watch\",\n \"tsc\": \"tsc\",\n \"build\": \"rollup --config\",\n \"build:watch\": \"npm run build -- --watch\"\n },\n \"repository\": {\n \"type\": \"git\",\n \"url\": \"git+https://github.com/jspsych/jsPsych.git\",\n \"directory\": \"packages/plugin-image-keyboard-response\"\n },\n \"author\": \"Josh de Leeuw\",\n \"license\": \"MIT\",\n \"bugs\": {\n \"url\": \"https://github.com/jspsych/jsPsych/issues\"\n },\n \"homepage\": \"https://www.jspsych.org/latest/plugins/image-keyboard-response\",\n \"peerDependencies\": {\n \"jspsych\": \">=7.1.0\"\n },\n \"devDependencies\": {\n \"@jspsych/config\": \"^3.2.0\",\n \"@jspsych/test-utils\": \"^1.2.0\"\n }\n}\n","import { JsPsych, JsPsychPlugin, ParameterType, TrialType } from \"jspsych\";\n\nimport { version } from \"../package.json\";\n\nconst info = <const>{\n name: \"image-keyboard-response\",\n version: version,\n parameters: {\n /** The path of the image file to be displayed. */\n stimulus: {\n type: ParameterType.IMAGE,\n default: undefined,\n },\n /** Set the height of the image in pixels. If left null (no value specified), then the image will display at its natural height. */\n stimulus_height: {\n type: ParameterType.INT,\n default: null,\n },\n /** Set the width of the image in pixels. If left null (no value specified), then the image will display at its natural width. */\n stimulus_width: {\n type: ParameterType.INT,\n default: null,\n },\n /** If setting *only* the width or *only* the height and this parameter is true, then the other dimension will be scaled\n * to maintain the image's aspect ratio. */\n maintain_aspect_ratio: {\n type: ParameterType.BOOL,\n default: true,\n },\n /**his array contains the key(s) that the participant is allowed to press in order to respond to the stimulus. Keys should\n * be specified as characters (e.g., `'a'`, `'q'`, `' '`, `'Enter'`, `'ArrowDown'`) - see\n * [this page](https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/key/Key_Values) and\n * [this page (event.key column)](https://www.freecodecamp.org/news/javascript-keycode-list-keypress-event-key-codes/)\n * for more examples. Any key presses that are not listed in the array will be ignored. The default value of `\"ALL_KEYS\"`\n * means that all keys will be accepted as valid responses. Specifying `\"NO_KEYS\"` will mean that no responses are allowed. */\n choices: {\n type: ParameterType.KEYS,\n default: \"ALL_KEYS\",\n },\n /**This string can contain HTML markup. Any content here will be displayed below the stimulus. The intention is that it can\n * be used to provide a reminder about the action the participant is supposed to take (e.g., which key to press). */\n prompt: {\n type: ParameterType.HTML_STRING,\n default: null,\n },\n /** How long to show the stimulus for in milliseconds. If the value is `null`, then the stimulus will be shown until the\n * participant makes a response. */\n stimulus_duration: {\n type: ParameterType.INT,\n default: null,\n },\n /** How long to wait for the participant to make a response before ending the trial in milliseconds. If the participant\n * fails to make a response before this timer is reached, the participant's response will be recorded as null for the\n * trial and the trial will end. If the value of this parameter is `null`, then the trial will wait for a response indefinitely. */\n trial_duration: {\n type: ParameterType.INT,\n default: null,\n },\n /** If true, then the trial will end whenever the participant makes a response (assuming they make their response before\n * the cutoff specified by the `trial_duration` parameter). If false, then the trial will continue until the value for\n * `trial_duration` is reached. You can set this parameter to `false` to force the participant to view a stimulus for a\n * fixed amount of time, even if they respond before the time is complete. */\n response_ends_trial: {\n type: ParameterType.BOOL,\n default: true,\n },\n /**\n * If `true`, the image will be drawn onto a canvas element. This prevents a blank screen (white flash) between consecutive image trials in some browsers, like Firefox and Edge.\n * If `false`, the image will be shown via an img element, as in previous versions of jsPsych. If the stimulus is an **animated gif**, you must set this parameter to false, because the canvas rendering method will only present static images.\n */\n render_on_canvas: {\n type: ParameterType.BOOL,\n default: true,\n },\n },\n data: {\n /** The path of the image that was displayed. */\n stimulus: {\n type: ParameterType.STRING,\n },\n /** Indicates which key the participant pressed. */\n response: {\n type: ParameterType.STRING,\n },\n /** The response time in milliseconds for the participant to make a response. The time is measured from when the stimulus\n * first appears on the screen until the participant's response. */\n rt: {\n type: ParameterType.INT,\n },\n },\n // prettier-ignore\n citations: '__CITATIONS__',\n};\n\ntype Info = typeof info;\n\n/**\n * This plugin displays an image and records responses generated with the keyboard. The stimulus can be displayed until a\n * response is given, or for a pre-determined amount of time. The trial can be ended automatically if the participant has\n * failed to respond within a fixed length of time.\n *\n * Image files can be automatically preloaded by jsPsych using the [`preload` plugin](preload.md). However, if you are using\n * timeline variables or another dynamic method to specify the image stimulus, you will need to\n * [manually preload](../overview/media-preloading.md#manual-preloading) the images.\n *\n * @author Josh de Leeuw\n * @see {@link https://www.jspsych.org/latest/plugins/image-keyboard-response/ image-keyboard-response plugin documentation on jspsych.org}\n */\nclass ImageKeyboardResponsePlugin implements JsPsychPlugin<Info> {\n static info = info;\n\n constructor(private jsPsych: JsPsych) {}\n\n trial(display_element: HTMLElement, trial: TrialType<Info>) {\n var height, width;\n if (trial.render_on_canvas) {\n var image_drawn = false;\n // first clear the display element (because the render_on_canvas method appends to display_element instead of overwriting it with .innerHTML)\n if (display_element.hasChildNodes()) {\n // can't loop through child list because the list will be modified by .removeChild()\n while (display_element.firstChild) {\n display_element.removeChild(display_element.firstChild);\n }\n }\n // create canvas element and image\n var canvas = document.createElement(\"canvas\");\n canvas.id = \"jspsych-image-keyboard-response-stimulus\";\n canvas.style.margin = \"0\";\n canvas.style.padding = \"0\";\n var ctx = canvas.getContext(\"2d\");\n var img = new Image();\n img.onload = () => {\n // if image wasn't preloaded, then it will need to be drawn whenever it finishes loading\n if (!image_drawn) {\n getHeightWidth(); // only possible to get width/height after image loads\n ctx.drawImage(img, 0, 0, width, height);\n }\n };\n img.src = trial.stimulus;\n // get/set image height and width - this can only be done after image loads because uses image's naturalWidth/naturalHeight properties\n const getHeightWidth = () => {\n if (trial.stimulus_height !== null) {\n height = trial.stimulus_height;\n if (trial.stimulus_width == null && trial.maintain_aspect_ratio) {\n width = img.naturalWidth * (trial.stimulus_height / img.naturalHeight);\n }\n } else {\n height = img.naturalHeight;\n }\n if (trial.stimulus_width !== null) {\n width = trial.stimulus_width;\n if (trial.stimulus_height == null && trial.maintain_aspect_ratio) {\n height = img.naturalHeight * (trial.stimulus_width / img.naturalWidth);\n }\n } else if (!(trial.stimulus_height !== null && trial.maintain_aspect_ratio)) {\n // if stimulus width is null, only use the image's natural width if the width value wasn't set\n // in the if statement above, based on a specified height and maintain_aspect_ratio = true\n width = img.naturalWidth;\n }\n canvas.height = height;\n canvas.width = width;\n };\n getHeightWidth(); // call now, in case image loads immediately (is cached)\n // add canvas and draw image\n display_element.insertBefore(canvas, null);\n if (img.complete && Number.isFinite(width) && Number.isFinite(height)) {\n // if image has loaded and width/height have been set, then draw it now\n // (don't rely on img onload function to draw image when image is in the cache, because that causes a delay in the image presentation)\n ctx.drawImage(img, 0, 0, width, height);\n image_drawn = true;\n }\n // add prompt if there is one\n if (trial.prompt !== null) {\n display_element.insertAdjacentHTML(\"beforeend\", trial.prompt);\n }\n } else {\n // display stimulus as an image element\n var html = '<img src=\"' + trial.stimulus + '\" id=\"jspsych-image-keyboard-response-stimulus\">';\n // add prompt\n if (trial.prompt !== null) {\n html += trial.prompt;\n }\n // update the page content\n display_element.innerHTML = html;\n\n // set image dimensions after image has loaded (so that we have access to naturalHeight/naturalWidth)\n var img = display_element.querySelector(\n \"#jspsych-image-keyboard-response-stimulus\"\n ) as HTMLImageElement;\n if (trial.stimulus_height !== null) {\n height = trial.stimulus_height;\n if (trial.stimulus_width == null && trial.maintain_aspect_ratio) {\n width = img.naturalWidth * (trial.stimulus_height / img.naturalHeight);\n }\n } else {\n height = img.naturalHeight;\n }\n if (trial.stimulus_width !== null) {\n width = trial.stimulus_width;\n if (trial.stimulus_height == null && trial.maintain_aspect_ratio) {\n height = img.naturalHeight * (trial.stimulus_width / img.naturalWidth);\n }\n } else if (!(trial.stimulus_height !== null && trial.maintain_aspect_ratio)) {\n // if stimulus width is null, only use the image's natural width if the width value wasn't set\n // in the if statement above, based on a specified height and maintain_aspect_ratio = true\n width = img.naturalWidth;\n }\n img.style.height = height.toString() + \"px\";\n img.style.width = width.toString() + \"px\";\n }\n\n // store response\n var response = {\n rt: null,\n key: null,\n };\n\n // function to end trial when it is time\n const end_trial = () => {\n // kill keyboard listeners\n if (typeof keyboardListener !== \"undefined\") {\n this.jsPsych.pluginAPI.cancelKeyboardResponse(keyboardListener);\n }\n\n // gather the data to store for the trial\n var trial_data = {\n rt: response.rt,\n stimulus: trial.stimulus,\n response: response.key,\n };\n\n // move on to the next trial\n this.jsPsych.finishTrial(trial_data);\n };\n\n // function to handle responses by the subject\n var after_response = (info) => {\n // after a valid response, the stimulus will have the CSS class 'responded'\n // which can be used to provide visual feedback that a response was recorded\n display_element.querySelector(\"#jspsych-image-keyboard-response-stimulus\").className +=\n \" responded\";\n\n // only record the first response\n if (response.key == null) {\n response = info;\n }\n\n if (trial.response_ends_trial) {\n end_trial();\n }\n };\n\n // start the response listener\n if (trial.choices != \"NO_KEYS\") {\n var keyboardListener = this.jsPsych.pluginAPI.getKeyboardResponse({\n callback_function: after_response,\n valid_responses: trial.choices,\n rt_method: \"performance\",\n persist: false,\n allow_held_key: false,\n });\n }\n\n // hide stimulus if stimulus_duration is set\n if (trial.stimulus_duration !== null) {\n this.jsPsych.pluginAPI.setTimeout(() => {\n display_element.querySelector<HTMLElement>(\n \"#jspsych-image-keyboard-response-stimulus\"\n ).style.visibility = \"hidden\";\n }, trial.stimulus_duration);\n }\n\n // end trial if trial_duration is set\n if (trial.trial_duration !== null) {\n this.jsPsych.pluginAPI.setTimeout(() => {\n end_trial();\n }, trial.trial_duration);\n } else if (trial.response_ends_trial === false) {\n console.warn(\n \"The experiment may be deadlocked. Try setting a trial duration or set response_ends_trial to true.\"\n );\n }\n }\n\n simulate(\n trial: TrialType<Info>,\n simulation_mode,\n simulation_options: any,\n load_callback: () => void\n ) {\n if (simulation_mode == \"data-only\") {\n load_callback();\n this.simulate_data_only(trial, simulation_options);\n }\n if (simulation_mode == \"visual\") {\n this.simulate_visual(trial, simulation_options, load_callback);\n }\n }\n\n private simulate_data_only(trial: TrialType<Info>, simulation_options) {\n const data = this.create_simulation_data(trial, simulation_options);\n\n this.jsPsych.finishTrial(data);\n }\n\n private simulate_visual(trial: TrialType<Info>, simulation_options, load_callback: () => void) {\n const data = this.create_simulation_data(trial, simulation_options);\n\n const display_element = this.jsPsych.getDisplayElement();\n\n this.trial(display_element, trial);\n load_callback();\n\n if (data.rt !== null) {\n this.jsPsych.pluginAPI.pressKey(data.response, data.rt);\n }\n }\n\n private create_simulation_data(trial: TrialType<Info>, simulation_options) {\n const default_data = {\n stimulus: trial.stimulus,\n rt: this.jsPsych.randomization.sampleExGaussian(500, 50, 1 / 150, true),\n response: this.jsPsych.pluginAPI.getValidKey(trial.choices),\n };\n\n const data = this.jsPsych.pluginAPI.mergeSimulationData(default_data, simulation_options);\n\n this.jsPsych.pluginAPI.ensureSimulationDataConsistency(trial, data);\n\n return data;\n }\n}\n\nexport default ImageKeyboardResponsePlugin;\n"],"names":[],"mappings":";;;;AAEE,IAAW,OAAA,GAAA,OAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;ECyFA,SAAA,EAAA;AAAA;;GAAe;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
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,6 +97,10 @@ 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
  };
89
105
  readonly citations: "__CITATIONS__";
90
106
  };
@@ -173,6 +189,18 @@ declare class ImageKeyboardResponsePlugin implements JsPsychPlugin<Info> {
173
189
  readonly type: ParameterType.BOOL;
174
190
  readonly default: true;
175
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
+ };
176
204
  };
177
205
  readonly data: {
178
206
  /** The path of the image that was displayed. */
@@ -188,6 +216,10 @@ declare class ImageKeyboardResponsePlugin implements JsPsychPlugin<Info> {
188
216
  readonly rt: {
189
217
  readonly type: ParameterType.INT;
190
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
+ };
191
223
  };
192
224
  readonly citations: "__CITATIONS__";
193
225
  };
package/dist/index.js CHANGED
@@ -1,6 +1,6 @@
1
1
  import { ParameterType } from 'jspsych';
2
2
 
3
- var version = "2.1.0";
3
+ var version = "2.2.0";
4
4
 
5
5
  const info = {
6
6
  name: "image-keyboard-response",
@@ -71,6 +71,18 @@ const info = {
71
71
  render_on_canvas: {
72
72
  type: ParameterType.BOOL,
73
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
74
86
  }
75
87
  },
76
88
  data: {
@@ -86,6 +98,10 @@ const info = {
86
98
  * first appears on the screen until the participant's response. */
87
99
  rt: {
88
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
89
105
  }
90
106
  },
91
107
  // prettier-ignore
@@ -182,7 +198,8 @@ class ImageKeyboardResponsePlugin {
182
198
  }
183
199
  var response = {
184
200
  rt: null,
185
- key: null
201
+ key: null,
202
+ rt_key_duration: null
186
203
  };
187
204
  const end_trial = () => {
188
205
  if (typeof keyboardListener !== "undefined") {
@@ -191,14 +208,19 @@ class ImageKeyboardResponsePlugin {
191
208
  var trial_data = {
192
209
  rt: response.rt,
193
210
  stimulus: trial.stimulus,
194
- response: response.key
211
+ response: response.key,
212
+ rt_key_duration: response.rt_key_duration
195
213
  };
196
214
  this.jsPsych.finishTrial(trial_data);
197
215
  };
198
216
  var after_response = (info2) => {
199
217
  display_element.querySelector("#jspsych-image-keyboard-response-stimulus").className += " responded";
200
218
  if (response.key == null) {
201
- response = info2;
219
+ response = {
220
+ rt: info2.rt,
221
+ key: info2.key,
222
+ rt_key_duration: info2.rt_key_duration ?? null
223
+ };
202
224
  }
203
225
  if (trial.response_ends_trial) {
204
226
  end_trial();
@@ -210,7 +232,8 @@ class ImageKeyboardResponsePlugin {
210
232
  valid_responses: trial.choices,
211
233
  rt_method: "performance",
212
234
  persist: false,
213
- allow_held_key: false
235
+ allow_held_key: false,
236
+ wait_for_key_release: trial.wait_for_key_release
214
237
  });
215
238
  }
216
239
  if (trial.stimulus_duration !== null) {
@@ -256,8 +279,10 @@ class ImageKeyboardResponsePlugin {
256
279
  const default_data = {
257
280
  stimulus: trial.stimulus,
258
281
  rt: this.jsPsych.randomization.sampleExGaussian(500, 50, 1 / 150, true),
259
- response: this.jsPsych.pluginAPI.getValidKey(trial.choices)
282
+ response: this.jsPsych.pluginAPI.getValidKey(trial.choices),
283
+ rt_key_duration: null
260
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);
261
286
  const data = this.jsPsych.pluginAPI.mergeSimulationData(default_data, simulation_options);
262
287
  this.jsPsych.pluginAPI.ensureSimulationDataConsistency(trial, data);
263
288
  return data;
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sources":["../package.json","../src/index.ts"],"sourcesContent":["{\n \"name\": \"@jspsych/plugin-image-keyboard-response\",\n \"version\": \"2.1.0\",\n \"description\": \"jsPsych plugin for displaying a stimulus and getting a keyboard response\",\n \"type\": \"module\",\n \"main\": \"dist/index.cjs\",\n \"exports\": {\n \"import\": \"./dist/index.js\",\n \"require\": \"./dist/index.cjs\"\n },\n \"typings\": \"dist/index.d.ts\",\n \"unpkg\": \"dist/index.browser.min.js\",\n \"files\": [\n \"src\",\n \"dist\"\n ],\n \"source\": \"src/index.ts\",\n \"scripts\": {\n \"test\": \"jest\",\n \"test:watch\": \"npm test -- --watch\",\n \"tsc\": \"tsc\",\n \"build\": \"rollup --config\",\n \"build:watch\": \"npm run build -- --watch\"\n },\n \"repository\": {\n \"type\": \"git\",\n \"url\": \"git+https://github.com/jspsych/jsPsych.git\",\n \"directory\": \"packages/plugin-image-keyboard-response\"\n },\n \"author\": \"Josh de Leeuw\",\n \"license\": \"MIT\",\n \"bugs\": {\n \"url\": \"https://github.com/jspsych/jsPsych/issues\"\n },\n \"homepage\": \"https://www.jspsych.org/latest/plugins/image-keyboard-response\",\n \"peerDependencies\": {\n \"jspsych\": \">=7.1.0\"\n },\n \"devDependencies\": {\n \"@jspsych/config\": \"^3.2.0\",\n \"@jspsych/test-utils\": \"^1.2.0\"\n }\n}\n","import { JsPsych, JsPsychPlugin, ParameterType, TrialType } from \"jspsych\";\n\nimport { version } from \"../package.json\";\n\nconst info = <const>{\n name: \"image-keyboard-response\",\n version: version,\n parameters: {\n /** The path of the image file to be displayed. */\n stimulus: {\n type: ParameterType.IMAGE,\n default: undefined,\n },\n /** Set the height of the image in pixels. If left null (no value specified), then the image will display at its natural height. */\n stimulus_height: {\n type: ParameterType.INT,\n default: null,\n },\n /** Set the width of the image in pixels. If left null (no value specified), then the image will display at its natural width. */\n stimulus_width: {\n type: ParameterType.INT,\n default: null,\n },\n /** If setting *only* the width or *only* the height and this parameter is true, then the other dimension will be scaled\n * to maintain the image's aspect ratio. */\n maintain_aspect_ratio: {\n type: ParameterType.BOOL,\n default: true,\n },\n /**his array contains the key(s) that the participant is allowed to press in order to respond to the stimulus. Keys should\n * be specified as characters (e.g., `'a'`, `'q'`, `' '`, `'Enter'`, `'ArrowDown'`) - see\n * [this page](https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/key/Key_Values) and\n * [this page (event.key column)](https://www.freecodecamp.org/news/javascript-keycode-list-keypress-event-key-codes/)\n * for more examples. Any key presses that are not listed in the array will be ignored. The default value of `\"ALL_KEYS\"`\n * means that all keys will be accepted as valid responses. Specifying `\"NO_KEYS\"` will mean that no responses are allowed. */\n choices: {\n type: ParameterType.KEYS,\n default: \"ALL_KEYS\",\n },\n /**This string can contain HTML markup. Any content here will be displayed below the stimulus. The intention is that it can\n * be used to provide a reminder about the action the participant is supposed to take (e.g., which key to press). */\n prompt: {\n type: ParameterType.HTML_STRING,\n default: null,\n },\n /** How long to show the stimulus for in milliseconds. If the value is `null`, then the stimulus will be shown until the\n * participant makes a response. */\n stimulus_duration: {\n type: ParameterType.INT,\n default: null,\n },\n /** How long to wait for the participant to make a response before ending the trial in milliseconds. If the participant\n * fails to make a response before this timer is reached, the participant's response will be recorded as null for the\n * trial and the trial will end. If the value of this parameter is `null`, then the trial will wait for a response indefinitely. */\n trial_duration: {\n type: ParameterType.INT,\n default: null,\n },\n /** If true, then the trial will end whenever the participant makes a response (assuming they make their response before\n * the cutoff specified by the `trial_duration` parameter). If false, then the trial will continue until the value for\n * `trial_duration` is reached. You can set this parameter to `false` to force the participant to view a stimulus for a\n * fixed amount of time, even if they respond before the time is complete. */\n response_ends_trial: {\n type: ParameterType.BOOL,\n default: true,\n },\n /**\n * If `true`, the image will be drawn onto a canvas element. This prevents a blank screen (white flash) between consecutive image trials in some browsers, like Firefox and Edge.\n * If `false`, the image will be shown via an img element, as in previous versions of jsPsych. If the stimulus is an **animated gif**, you must set this parameter to false, because the canvas rendering method will only present static images.\n */\n render_on_canvas: {\n type: ParameterType.BOOL,\n default: true,\n },\n },\n data: {\n /** The path of the image that was displayed. */\n stimulus: {\n type: ParameterType.STRING,\n },\n /** Indicates which key the participant pressed. */\n response: {\n type: ParameterType.STRING,\n },\n /** The response time in milliseconds for the participant to make a response. The time is measured from when the stimulus\n * first appears on the screen until the participant's response. */\n rt: {\n type: ParameterType.INT,\n },\n },\n // prettier-ignore\n citations: '__CITATIONS__',\n};\n\ntype Info = typeof info;\n\n/**\n * This plugin displays an image and records responses generated with the keyboard. The stimulus can be displayed until a\n * response is given, or for a pre-determined amount of time. The trial can be ended automatically if the participant has\n * failed to respond within a fixed length of time.\n *\n * Image files can be automatically preloaded by jsPsych using the [`preload` plugin](preload.md). However, if you are using\n * timeline variables or another dynamic method to specify the image stimulus, you will need to\n * [manually preload](../overview/media-preloading.md#manual-preloading) the images.\n *\n * @author Josh de Leeuw\n * @see {@link https://www.jspsych.org/latest/plugins/image-keyboard-response/ image-keyboard-response plugin documentation on jspsych.org}\n */\nclass ImageKeyboardResponsePlugin implements JsPsychPlugin<Info> {\n static info = info;\n\n constructor(private jsPsych: JsPsych) {}\n\n trial(display_element: HTMLElement, trial: TrialType<Info>) {\n var height, width;\n if (trial.render_on_canvas) {\n var image_drawn = false;\n // first clear the display element (because the render_on_canvas method appends to display_element instead of overwriting it with .innerHTML)\n if (display_element.hasChildNodes()) {\n // can't loop through child list because the list will be modified by .removeChild()\n while (display_element.firstChild) {\n display_element.removeChild(display_element.firstChild);\n }\n }\n // create canvas element and image\n var canvas = document.createElement(\"canvas\");\n canvas.id = \"jspsych-image-keyboard-response-stimulus\";\n canvas.style.margin = \"0\";\n canvas.style.padding = \"0\";\n var ctx = canvas.getContext(\"2d\");\n var img = new Image();\n img.onload = () => {\n // if image wasn't preloaded, then it will need to be drawn whenever it finishes loading\n if (!image_drawn) {\n getHeightWidth(); // only possible to get width/height after image loads\n ctx.drawImage(img, 0, 0, width, height);\n }\n };\n img.src = trial.stimulus;\n // get/set image height and width - this can only be done after image loads because uses image's naturalWidth/naturalHeight properties\n const getHeightWidth = () => {\n if (trial.stimulus_height !== null) {\n height = trial.stimulus_height;\n if (trial.stimulus_width == null && trial.maintain_aspect_ratio) {\n width = img.naturalWidth * (trial.stimulus_height / img.naturalHeight);\n }\n } else {\n height = img.naturalHeight;\n }\n if (trial.stimulus_width !== null) {\n width = trial.stimulus_width;\n if (trial.stimulus_height == null && trial.maintain_aspect_ratio) {\n height = img.naturalHeight * (trial.stimulus_width / img.naturalWidth);\n }\n } else if (!(trial.stimulus_height !== null && trial.maintain_aspect_ratio)) {\n // if stimulus width is null, only use the image's natural width if the width value wasn't set\n // in the if statement above, based on a specified height and maintain_aspect_ratio = true\n width = img.naturalWidth;\n }\n canvas.height = height;\n canvas.width = width;\n };\n getHeightWidth(); // call now, in case image loads immediately (is cached)\n // add canvas and draw image\n display_element.insertBefore(canvas, null);\n if (img.complete && Number.isFinite(width) && Number.isFinite(height)) {\n // if image has loaded and width/height have been set, then draw it now\n // (don't rely on img onload function to draw image when image is in the cache, because that causes a delay in the image presentation)\n ctx.drawImage(img, 0, 0, width, height);\n image_drawn = true;\n }\n // add prompt if there is one\n if (trial.prompt !== null) {\n display_element.insertAdjacentHTML(\"beforeend\", trial.prompt);\n }\n } else {\n // display stimulus as an image element\n var html = '<img src=\"' + trial.stimulus + '\" id=\"jspsych-image-keyboard-response-stimulus\">';\n // add prompt\n if (trial.prompt !== null) {\n html += trial.prompt;\n }\n // update the page content\n display_element.innerHTML = html;\n\n // set image dimensions after image has loaded (so that we have access to naturalHeight/naturalWidth)\n var img = display_element.querySelector(\n \"#jspsych-image-keyboard-response-stimulus\"\n ) as HTMLImageElement;\n if (trial.stimulus_height !== null) {\n height = trial.stimulus_height;\n if (trial.stimulus_width == null && trial.maintain_aspect_ratio) {\n width = img.naturalWidth * (trial.stimulus_height / img.naturalHeight);\n }\n } else {\n height = img.naturalHeight;\n }\n if (trial.stimulus_width !== null) {\n width = trial.stimulus_width;\n if (trial.stimulus_height == null && trial.maintain_aspect_ratio) {\n height = img.naturalHeight * (trial.stimulus_width / img.naturalWidth);\n }\n } else if (!(trial.stimulus_height !== null && trial.maintain_aspect_ratio)) {\n // if stimulus width is null, only use the image's natural width if the width value wasn't set\n // in the if statement above, based on a specified height and maintain_aspect_ratio = true\n width = img.naturalWidth;\n }\n img.style.height = height.toString() + \"px\";\n img.style.width = width.toString() + \"px\";\n }\n\n // store response\n var response = {\n rt: null,\n key: null,\n };\n\n // function to end trial when it is time\n const end_trial = () => {\n // kill keyboard listeners\n if (typeof keyboardListener !== \"undefined\") {\n this.jsPsych.pluginAPI.cancelKeyboardResponse(keyboardListener);\n }\n\n // gather the data to store for the trial\n var trial_data = {\n rt: response.rt,\n stimulus: trial.stimulus,\n response: response.key,\n };\n\n // move on to the next trial\n this.jsPsych.finishTrial(trial_data);\n };\n\n // function to handle responses by the subject\n var after_response = (info) => {\n // after a valid response, the stimulus will have the CSS class 'responded'\n // which can be used to provide visual feedback that a response was recorded\n display_element.querySelector(\"#jspsych-image-keyboard-response-stimulus\").className +=\n \" responded\";\n\n // only record the first response\n if (response.key == null) {\n response = info;\n }\n\n if (trial.response_ends_trial) {\n end_trial();\n }\n };\n\n // start the response listener\n if (trial.choices != \"NO_KEYS\") {\n var keyboardListener = this.jsPsych.pluginAPI.getKeyboardResponse({\n callback_function: after_response,\n valid_responses: trial.choices,\n rt_method: \"performance\",\n persist: false,\n allow_held_key: false,\n });\n }\n\n // hide stimulus if stimulus_duration is set\n if (trial.stimulus_duration !== null) {\n this.jsPsych.pluginAPI.setTimeout(() => {\n display_element.querySelector<HTMLElement>(\n \"#jspsych-image-keyboard-response-stimulus\"\n ).style.visibility = \"hidden\";\n }, trial.stimulus_duration);\n }\n\n // end trial if trial_duration is set\n if (trial.trial_duration !== null) {\n this.jsPsych.pluginAPI.setTimeout(() => {\n end_trial();\n }, trial.trial_duration);\n } else if (trial.response_ends_trial === false) {\n console.warn(\n \"The experiment may be deadlocked. Try setting a trial duration or set response_ends_trial to true.\"\n );\n }\n }\n\n simulate(\n trial: TrialType<Info>,\n simulation_mode,\n simulation_options: any,\n load_callback: () => void\n ) {\n if (simulation_mode == \"data-only\") {\n load_callback();\n this.simulate_data_only(trial, simulation_options);\n }\n if (simulation_mode == \"visual\") {\n this.simulate_visual(trial, simulation_options, load_callback);\n }\n }\n\n private simulate_data_only(trial: TrialType<Info>, simulation_options) {\n const data = this.create_simulation_data(trial, simulation_options);\n\n this.jsPsych.finishTrial(data);\n }\n\n private simulate_visual(trial: TrialType<Info>, simulation_options, load_callback: () => void) {\n const data = this.create_simulation_data(trial, simulation_options);\n\n const display_element = this.jsPsych.getDisplayElement();\n\n this.trial(display_element, trial);\n load_callback();\n\n if (data.rt !== null) {\n this.jsPsych.pluginAPI.pressKey(data.response, data.rt);\n }\n }\n\n private create_simulation_data(trial: TrialType<Info>, simulation_options) {\n const default_data = {\n stimulus: trial.stimulus,\n rt: this.jsPsych.randomization.sampleExGaussian(500, 50, 1 / 150, true),\n response: this.jsPsych.pluginAPI.getValidKey(trial.choices),\n };\n\n const data = this.jsPsych.pluginAPI.mergeSimulationData(default_data, simulation_options);\n\n this.jsPsych.pluginAPI.ensureSimulationDataConsistency(trial, data);\n\n return data;\n }\n}\n\nexport default ImageKeyboardResponsePlugin;\n"],"names":[],"mappings":";;AAEE,IAAW,OAAA,GAAA,OAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;ECyFA,SAAA,EAAA;AAAA;;GAAe;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
1
+ {"version":3,"file":"index.js","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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jspsych/plugin-image-keyboard-response",
3
- "version": "2.1.0",
3
+ "version": "2.2.0",
4
4
  "description": "jsPsych plugin for displaying a stimulus and getting a keyboard response",
5
5
  "type": "module",
6
6
  "main": "dist/index.cjs",
@@ -37,7 +37,7 @@
37
37
  "jspsych": ">=7.1.0"
38
38
  },
39
39
  "devDependencies": {
40
- "@jspsych/config": "^3.2.0",
41
- "@jspsych/test-utils": "^1.2.0"
40
+ "@jspsych/config": "^3.3.4",
41
+ "@jspsych/test-utils": "^1.3.0"
42
42
  }
43
43
  }
package/src/index.spec.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { pressKey, startTimeline } from "@jspsych/test-utils";
1
+ import { flushPromises, keyDown, keyUp, pressKey, startTimeline } from "@jspsych/test-utils";
2
2
 
3
3
  import imageKeyboardResponse from ".";
4
4
 
@@ -135,3 +135,143 @@ describe("image-keyboard-response", () => {
135
135
  spy.mockRestore();
136
136
  });
137
137
  });
138
+
139
+ describe("image-keyboard-response wait_for_key_release", () => {
140
+ test("does not end the trial until the key is released and records the hold duration", async () => {
141
+ const { getData, expectRunning, expectFinished } = await startTimeline([
142
+ {
143
+ type: imageKeyboardResponse,
144
+ stimulus: "../media/blue.png",
145
+ choices: ["a"],
146
+ render_on_canvas: false,
147
+ wait_for_key_release: true,
148
+ },
149
+ ]);
150
+
151
+ jest.advanceTimersByTime(100);
152
+ await keyDown("a");
153
+
154
+ await expectRunning();
155
+
156
+ jest.advanceTimersByTime(250);
157
+ await keyUp("a");
158
+
159
+ await expectFinished();
160
+
161
+ expect(getData().values()[0].rt).toBe(100);
162
+ expect(getData().values()[0].rt_key_duration).toBe(250);
163
+ });
164
+
165
+ test("records the hold duration when the trial ends by duration", async () => {
166
+ const { getData, expectFinished } = await startTimeline([
167
+ {
168
+ type: imageKeyboardResponse,
169
+ stimulus: "../media/blue.png",
170
+ choices: ["a"],
171
+ render_on_canvas: false,
172
+ wait_for_key_release: true,
173
+ response_ends_trial: false,
174
+ trial_duration: 1000,
175
+ },
176
+ ]);
177
+
178
+ await keyDown("a");
179
+ jest.advanceTimersByTime(250);
180
+ await keyUp("a");
181
+
182
+ jest.advanceTimersByTime(750);
183
+ await expectFinished();
184
+
185
+ expect(getData().values()[0].rt_key_duration).toBe(250);
186
+ });
187
+
188
+ test("records a null response when the trial ends while the key is still held", async () => {
189
+ const { getData, expectFinished } = await startTimeline([
190
+ {
191
+ type: imageKeyboardResponse,
192
+ stimulus: "../media/blue.png",
193
+ choices: ["a"],
194
+ render_on_canvas: false,
195
+ wait_for_key_release: true,
196
+ response_ends_trial: false,
197
+ trial_duration: 1000,
198
+ },
199
+ ]);
200
+
201
+ await keyDown("a");
202
+ jest.advanceTimersByTime(1000);
203
+ await expectFinished();
204
+
205
+ expect(getData().values()[0].response).toBe(null);
206
+ expect(getData().values()[0].rt).toBe(null);
207
+ expect(getData().values()[0].rt_key_duration).toBe(null);
208
+
209
+ await keyUp("a");
210
+ });
211
+
212
+ test("rt_key_duration is null on default trials (wait_for_key_release false)", async () => {
213
+ const { getData, expectFinished } = await startTimeline([
214
+ {
215
+ type: imageKeyboardResponse,
216
+ stimulus: "../media/blue.png",
217
+ choices: ["a"],
218
+ render_on_canvas: false,
219
+ },
220
+ ]);
221
+
222
+ await keyDown("a");
223
+ await expectFinished();
224
+
225
+ expect(getData().values()[0].rt_key_duration).toBe(null);
226
+ });
227
+
228
+ test("a key held past trial_duration does not contaminate the next trial", async () => {
229
+ const { getData, expectRunning, expectFinished } = await startTimeline([
230
+ {
231
+ type: imageKeyboardResponse,
232
+ stimulus: "../media/blue.png",
233
+ choices: ["a"],
234
+ render_on_canvas: false,
235
+ wait_for_key_release: true,
236
+ trial_duration: 1000,
237
+ },
238
+ {
239
+ type: imageKeyboardResponse,
240
+ stimulus: "../media/blue.png",
241
+ choices: ["a"],
242
+ render_on_canvas: false,
243
+ wait_for_key_release: true,
244
+ },
245
+ ]);
246
+
247
+ // trial 1: press and hold the key, then let the trial time out
248
+ await keyDown("a");
249
+ jest.advanceTimersByTime(1000);
250
+ await flushPromises();
251
+
252
+ // trial 1 ended by duration with no response because the key was never released
253
+ expect(getData().values()[0].response).toBe(null);
254
+ expect(getData().values()[0].rt).toBe(null);
255
+ expect(getData().values()[0].rt_key_duration).toBe(null);
256
+
257
+ // trial 2 is now running with the key still held; a key-repeat must not register
258
+ await keyDown("a");
259
+ await expectRunning();
260
+
261
+ // releasing the held-over key must not register a response in trial 2
262
+ await keyUp("a");
263
+ await expectRunning();
264
+
265
+ // a fresh press/release cycle in trial 2 works normally
266
+ jest.advanceTimersByTime(200);
267
+ await keyDown("a");
268
+ jest.advanceTimersByTime(125);
269
+ await keyUp("a");
270
+
271
+ await expectFinished();
272
+
273
+ expect(getData().values()[1].response).toBe("a");
274
+ expect(getData().values()[1].rt).toBe(200);
275
+ expect(getData().values()[1].rt_key_duration).toBe(125);
276
+ });
277
+ });
package/src/index.ts CHANGED
@@ -72,6 +72,18 @@ const info = <const>{
72
72
  type: ParameterType.BOOL,
73
73
  default: true,
74
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,
86
+ },
75
87
  },
76
88
  data: {
77
89
  /** The path of the image that was displayed. */
@@ -87,6 +99,10 @@ const info = <const>{
87
99
  rt: {
88
100
  type: ParameterType.INT,
89
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,
105
+ },
90
106
  },
91
107
  // prettier-ignore
92
108
  citations: '__CITATIONS__',
@@ -213,6 +229,7 @@ class ImageKeyboardResponsePlugin implements JsPsychPlugin<Info> {
213
229
  var response = {
214
230
  rt: null,
215
231
  key: null,
232
+ rt_key_duration: null,
216
233
  };
217
234
 
218
235
  // function to end trial when it is time
@@ -227,6 +244,7 @@ class ImageKeyboardResponsePlugin implements JsPsychPlugin<Info> {
227
244
  rt: response.rt,
228
245
  stimulus: trial.stimulus,
229
246
  response: response.key,
247
+ rt_key_duration: response.rt_key_duration,
230
248
  };
231
249
 
232
250
  // move on to the next trial
@@ -242,7 +260,11 @@ class ImageKeyboardResponsePlugin implements JsPsychPlugin<Info> {
242
260
 
243
261
  // only record the first response
244
262
  if (response.key == null) {
245
- response = info;
263
+ response = {
264
+ rt: info.rt,
265
+ key: info.key,
266
+ rt_key_duration: info.rt_key_duration ?? null,
267
+ };
246
268
  }
247
269
 
248
270
  if (trial.response_ends_trial) {
@@ -258,6 +280,7 @@ class ImageKeyboardResponsePlugin implements JsPsychPlugin<Info> {
258
280
  rt_method: "performance",
259
281
  persist: false,
260
282
  allow_held_key: false,
283
+ wait_for_key_release: trial.wait_for_key_release,
261
284
  });
262
285
  }
263
286
 
@@ -321,7 +344,12 @@ class ImageKeyboardResponsePlugin implements JsPsychPlugin<Info> {
321
344
  stimulus: trial.stimulus,
322
345
  rt: this.jsPsych.randomization.sampleExGaussian(500, 50, 1 / 150, true),
323
346
  response: this.jsPsych.pluginAPI.getValidKey(trial.choices),
347
+ rt_key_duration: null,
324
348
  };
349
+ default_data.rt_key_duration =
350
+ default_data.rt === null || !trial.wait_for_key_release
351
+ ? null
352
+ : this.jsPsych.randomization.sampleExGaussian(150, 30, 1 / 100, true);
325
353
 
326
354
  const data = this.jsPsych.pluginAPI.mergeSimulationData(default_data, simulation_options);
327
355