@jspsych/plugin-image-keyboard-response 1.0.0 → 1.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.browser.js +36 -4
- package/dist/index.browser.js.map +1 -1
- package/dist/index.browser.min.js +1 -1
- package/dist/index.browser.min.js.map +1 -1
- package/dist/index.cjs +36 -4
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +4 -0
- package/dist/index.js +36 -4
- package/dist/index.js.map +1 -1
- package/package.json +4 -4
- package/src/index.ts +52 -4
package/dist/index.browser.js
CHANGED
|
@@ -99,7 +99,7 @@ var jsPsychImageKeyboardResponse = (function (jspsych) {
|
|
|
99
99
|
canvas.style.padding = "0";
|
|
100
100
|
var ctx = canvas.getContext("2d");
|
|
101
101
|
var img = new Image();
|
|
102
|
-
img.onload =
|
|
102
|
+
img.onload = () => {
|
|
103
103
|
// if image wasn't preloaded, then it will need to be drawn whenever it finishes loading
|
|
104
104
|
if (!image_drawn) {
|
|
105
105
|
getHeightWidth(); // only possible to get width/height after image loads
|
|
@@ -205,7 +205,7 @@ var jsPsychImageKeyboardResponse = (function (jspsych) {
|
|
|
205
205
|
this.jsPsych.finishTrial(trial_data);
|
|
206
206
|
};
|
|
207
207
|
// function to handle responses by the subject
|
|
208
|
-
var after_response =
|
|
208
|
+
var after_response = (info) => {
|
|
209
209
|
// after a valid response, the stimulus will have the CSS class 'responded'
|
|
210
210
|
// which can be used to provide visual feedback that a response was recorded
|
|
211
211
|
display_element.querySelector("#jspsych-image-keyboard-response-stimulus").className +=
|
|
@@ -230,13 +230,13 @@ var jsPsychImageKeyboardResponse = (function (jspsych) {
|
|
|
230
230
|
}
|
|
231
231
|
// hide stimulus if stimulus_duration is set
|
|
232
232
|
if (trial.stimulus_duration !== null) {
|
|
233
|
-
this.jsPsych.pluginAPI.setTimeout(
|
|
233
|
+
this.jsPsych.pluginAPI.setTimeout(() => {
|
|
234
234
|
display_element.querySelector("#jspsych-image-keyboard-response-stimulus").style.visibility = "hidden";
|
|
235
235
|
}, trial.stimulus_duration);
|
|
236
236
|
}
|
|
237
237
|
// end trial if trial_duration is set
|
|
238
238
|
if (trial.trial_duration !== null) {
|
|
239
|
-
this.jsPsych.pluginAPI.setTimeout(
|
|
239
|
+
this.jsPsych.pluginAPI.setTimeout(() => {
|
|
240
240
|
end_trial();
|
|
241
241
|
}, trial.trial_duration);
|
|
242
242
|
}
|
|
@@ -244,6 +244,38 @@ var jsPsychImageKeyboardResponse = (function (jspsych) {
|
|
|
244
244
|
console.warn("The experiment may be deadlocked. Try setting a trial duration or set response_ends_trial to true.");
|
|
245
245
|
}
|
|
246
246
|
}
|
|
247
|
+
simulate(trial, simulation_mode, simulation_options, load_callback) {
|
|
248
|
+
if (simulation_mode == "data-only") {
|
|
249
|
+
load_callback();
|
|
250
|
+
this.simulate_data_only(trial, simulation_options);
|
|
251
|
+
}
|
|
252
|
+
if (simulation_mode == "visual") {
|
|
253
|
+
this.simulate_visual(trial, simulation_options, load_callback);
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
simulate_data_only(trial, simulation_options) {
|
|
257
|
+
const data = this.create_simulation_data(trial, simulation_options);
|
|
258
|
+
this.jsPsych.finishTrial(data);
|
|
259
|
+
}
|
|
260
|
+
simulate_visual(trial, simulation_options, load_callback) {
|
|
261
|
+
const data = this.create_simulation_data(trial, simulation_options);
|
|
262
|
+
const display_element = this.jsPsych.getDisplayElement();
|
|
263
|
+
this.trial(display_element, trial);
|
|
264
|
+
load_callback();
|
|
265
|
+
if (data.rt !== null) {
|
|
266
|
+
this.jsPsych.pluginAPI.pressKey(data.response, data.rt);
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
create_simulation_data(trial, simulation_options) {
|
|
270
|
+
const default_data = {
|
|
271
|
+
stimulus: trial.stimulus,
|
|
272
|
+
rt: this.jsPsych.randomization.sampleExGaussian(500, 50, 1 / 150, true),
|
|
273
|
+
response: this.jsPsych.pluginAPI.getValidKey(trial.choices),
|
|
274
|
+
};
|
|
275
|
+
const data = this.jsPsych.pluginAPI.mergeSimulationData(default_data, simulation_options);
|
|
276
|
+
this.jsPsych.pluginAPI.ensureSimulationDataConsistency(trial, data);
|
|
277
|
+
return data;
|
|
278
|
+
}
|
|
247
279
|
}
|
|
248
280
|
ImageKeyboardResponsePlugin.info = info;
|
|
249
281
|
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.browser.js","sources":["../src/index.ts"],"sourcesContent":["import { JsPsych, JsPsychPlugin, ParameterType, TrialType } from \"jspsych\";\n\nconst info = <const>{\n name: \"image-keyboard-response\",\n parameters: {\n /** The image to be displayed */\n stimulus: {\n type: ParameterType.IMAGE,\n pretty_name: \"Stimulus\",\n default: undefined,\n },\n /** Set the image height in pixels */\n stimulus_height: {\n type: ParameterType.INT,\n pretty_name: \"Image height\",\n default: null,\n },\n /** Set the image width in pixels */\n stimulus_width: {\n type: ParameterType.INT,\n pretty_name: \"Image width\",\n default: null,\n },\n /** Maintain the aspect ratio after setting width or height */\n maintain_aspect_ratio: {\n type: ParameterType.BOOL,\n pretty_name: \"Maintain aspect ratio\",\n default: true,\n },\n /** Array containing the key(s) the subject is allowed to press to respond to the stimulus. */\n choices: {\n type: ParameterType.KEYS,\n pretty_name: \"Choices\",\n default: \"ALL_KEYS\",\n },\n /** Any content here will be displayed below the stimulus. */\n prompt: {\n type: ParameterType.HTML_STRING,\n pretty_name: \"Prompt\",\n default: null,\n },\n /** How long to show the stimulus. */\n stimulus_duration: {\n type: ParameterType.INT,\n pretty_name: \"Stimulus duration\",\n default: null,\n },\n /** How long to show trial before it ends */\n trial_duration: {\n type: ParameterType.INT,\n pretty_name: \"Trial duration\",\n default: null,\n },\n /** If true, trial will end when subject makes a response. */\n response_ends_trial: {\n type: ParameterType.BOOL,\n pretty_name: \"Response ends trial\",\n default: true,\n },\n /**\n * If true, the image will be drawn onto a canvas element (prevents blank screen between consecutive images in some browsers).\n * If false, the image will be shown via an img element.\n */\n render_on_canvas: {\n type: ParameterType.BOOL,\n pretty_name: \"Render on canvas\",\n default: true,\n },\n },\n};\n\ntype Info = typeof info;\n\n/**\n * **image-keyboard-response**\n *\n * jsPsych plugin for displaying an image stimulus and getting a keyboard response\n *\n * @author Josh de Leeuw\n * @see {@link https://www.jspsych.org/plugins/jspsych-image-keyboard-response/ image-keyboard-response plugin documentation on jspsych.org}\n */\nclass ImageKeyboardResponsePlugin implements JsPsychPlugin<Info> {\n static info = info;\n\n constructor(private jsPsych: JsPsych) {}\n\n trial(display_element: HTMLElement, trial: TrialType<Info>) {\n var height, width;\n if (trial.render_on_canvas) {\n var image_drawn = false;\n // first clear the display element (because the render_on_canvas method appends to display_element instead of overwriting it with .innerHTML)\n if (display_element.hasChildNodes()) {\n // can't loop through child list because the list will be modified by .removeChild()\n while (display_element.firstChild) {\n display_element.removeChild(display_element.firstChild);\n }\n }\n // create canvas element and image\n var canvas = document.createElement(\"canvas\");\n canvas.id = \"jspsych-image-keyboard-response-stimulus\";\n canvas.style.margin = \"0\";\n canvas.style.padding = \"0\";\n var ctx = canvas.getContext(\"2d\");\n var img = new Image();\n img.onload = function () {\n // if image wasn't preloaded, then it will need to be drawn whenever it finishes loading\n if (!image_drawn) {\n getHeightWidth(); // only possible to get width/height after image loads\n ctx.drawImage(img, 0, 0, width, height);\n }\n };\n img.src = trial.stimulus;\n // get/set image height and width - this can only be done after image loads because uses image's naturalWidth/naturalHeight properties\n const getHeightWidth = () => {\n if (trial.stimulus_height !== null) {\n height = trial.stimulus_height;\n if (trial.stimulus_width == null && trial.maintain_aspect_ratio) {\n width = img.naturalWidth * (trial.stimulus_height / img.naturalHeight);\n }\n } else {\n height = img.naturalHeight;\n }\n if (trial.stimulus_width !== null) {\n width = trial.stimulus_width;\n if (trial.stimulus_height == null && trial.maintain_aspect_ratio) {\n height = img.naturalHeight * (trial.stimulus_width / img.naturalWidth);\n }\n } else if (!(trial.stimulus_height !== null && trial.maintain_aspect_ratio)) {\n // if stimulus width is null, only use the image's natural width if the width value wasn't set\n // in the if statement above, based on a specified height and maintain_aspect_ratio = true\n width = img.naturalWidth;\n }\n canvas.height = height;\n canvas.width = width;\n };\n getHeightWidth(); // call now, in case image loads immediately (is cached)\n // add canvas and draw image\n display_element.insertBefore(canvas, null);\n if (img.complete && Number.isFinite(width) && Number.isFinite(height)) {\n // if image has loaded and width/height have been set, then draw it now\n // (don't rely on img onload function to draw image when image is in the cache, because that causes a delay in the image presentation)\n ctx.drawImage(img, 0, 0, width, height);\n image_drawn = true;\n }\n // add prompt if there is one\n if (trial.prompt !== null) {\n display_element.insertAdjacentHTML(\"beforeend\", trial.prompt);\n }\n } else {\n // display stimulus as an image element\n var html = '<img src=\"' + trial.stimulus + '\" id=\"jspsych-image-keyboard-response-stimulus\">';\n // add prompt\n if (trial.prompt !== null) {\n html += trial.prompt;\n }\n // update the page content\n display_element.innerHTML = html;\n\n // set image dimensions after image has loaded (so that we have access to naturalHeight/naturalWidth)\n var img = display_element.querySelector(\n \"#jspsych-image-keyboard-response-stimulus\"\n ) as HTMLImageElement;\n if (trial.stimulus_height !== null) {\n height = trial.stimulus_height;\n if (trial.stimulus_width == null && trial.maintain_aspect_ratio) {\n width = img.naturalWidth * (trial.stimulus_height / img.naturalHeight);\n }\n } else {\n height = img.naturalHeight;\n }\n if (trial.stimulus_width !== null) {\n width = trial.stimulus_width;\n if (trial.stimulus_height == null && trial.maintain_aspect_ratio) {\n height = img.naturalHeight * (trial.stimulus_width / img.naturalWidth);\n }\n } else if (!(trial.stimulus_height !== null && trial.maintain_aspect_ratio)) {\n // if stimulus width is null, only use the image's natural width if the width value wasn't set\n // in the if statement above, based on a specified height and maintain_aspect_ratio = true\n width = img.naturalWidth;\n }\n img.style.height = height.toString() + \"px\";\n img.style.width = width.toString() + \"px\";\n }\n\n // store response\n var response = {\n rt: null,\n key: null,\n };\n\n // function to end trial when it is time\n const end_trial = () => {\n // kill any remaining setTimeout handlers\n this.jsPsych.pluginAPI.clearAllTimeouts();\n\n // kill keyboard listeners\n if (typeof keyboardListener !== \"undefined\") {\n this.jsPsych.pluginAPI.cancelKeyboardResponse(keyboardListener);\n }\n\n // gather the data to store for the trial\n var trial_data = {\n rt: response.rt,\n stimulus: trial.stimulus,\n response: response.key,\n };\n\n // clear the display\n display_element.innerHTML = \"\";\n\n // move on to the next trial\n this.jsPsych.finishTrial(trial_data);\n };\n\n // function to handle responses by the subject\n var after_response = function (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(function () {\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(function () {\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\nexport default ImageKeyboardResponsePlugin;\n"],"names":["ParameterType"],"mappings":";;;EAEA,MAAM,IAAI,GAAU;MAClB,IAAI,EAAE,yBAAyB;MAC/B,UAAU,EAAE;;UAEV,QAAQ,EAAE;cACR,IAAI,EAAEA,qBAAa,CAAC,KAAK;cACzB,WAAW,EAAE,UAAU;cACvB,OAAO,EAAE,SAAS;WACnB;;UAED,eA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,KAAK,CAAC,cAAc,CAAC,CAAC;WAC1B;eAAM,IAAI,KAAK,CAAC,mBAAmB,KAAK,KAAK,EAAE;cAC9C,OAAO,CAAC,IAAI,CACV,oGAAoG,CACrG,CAAC;WACH;OACF;;EAnLM,gCAAI,GAAG,IAAI;;;;;;;;"}
|
|
1
|
+
{"version":3,"file":"index.browser.js","sources":["../src/index.ts"],"sourcesContent":["import { JsPsych, JsPsychPlugin, ParameterType, TrialType } from \"jspsych\";\n\nconst info = <const>{\n name: \"image-keyboard-response\",\n parameters: {\n /** The image to be displayed */\n stimulus: {\n type: ParameterType.IMAGE,\n pretty_name: \"Stimulus\",\n default: undefined,\n },\n /** Set the image height in pixels */\n stimulus_height: {\n type: ParameterType.INT,\n pretty_name: \"Image height\",\n default: null,\n },\n /** Set the image width in pixels */\n stimulus_width: {\n type: ParameterType.INT,\n pretty_name: \"Image width\",\n default: null,\n },\n /** Maintain the aspect ratio after setting width or height */\n maintain_aspect_ratio: {\n type: ParameterType.BOOL,\n pretty_name: \"Maintain aspect ratio\",\n default: true,\n },\n /** Array containing the key(s) the subject is allowed to press to respond to the stimulus. */\n choices: {\n type: ParameterType.KEYS,\n pretty_name: \"Choices\",\n default: \"ALL_KEYS\",\n },\n /** Any content here will be displayed below the stimulus. */\n prompt: {\n type: ParameterType.HTML_STRING,\n pretty_name: \"Prompt\",\n default: null,\n },\n /** How long to show the stimulus. */\n stimulus_duration: {\n type: ParameterType.INT,\n pretty_name: \"Stimulus duration\",\n default: null,\n },\n /** How long to show trial before it ends */\n trial_duration: {\n type: ParameterType.INT,\n pretty_name: \"Trial duration\",\n default: null,\n },\n /** If true, trial will end when subject makes a response. */\n response_ends_trial: {\n type: ParameterType.BOOL,\n pretty_name: \"Response ends trial\",\n default: true,\n },\n /**\n * If true, the image will be drawn onto a canvas element (prevents blank screen between consecutive images in some browsers).\n * If false, the image will be shown via an img element.\n */\n render_on_canvas: {\n type: ParameterType.BOOL,\n pretty_name: \"Render on canvas\",\n default: true,\n },\n },\n};\n\ntype Info = typeof info;\n\n/**\n * **image-keyboard-response**\n *\n * jsPsych plugin for displaying an image stimulus and getting a keyboard response\n *\n * @author Josh de Leeuw\n * @see {@link https://www.jspsych.org/plugins/jspsych-image-keyboard-response/ image-keyboard-response plugin documentation on jspsych.org}\n */\nclass ImageKeyboardResponsePlugin implements JsPsychPlugin<Info> {\n static info = info;\n\n constructor(private jsPsych: JsPsych) {}\n\n trial(display_element: HTMLElement, trial: TrialType<Info>) {\n var height, width;\n if (trial.render_on_canvas) {\n var image_drawn = false;\n // first clear the display element (because the render_on_canvas method appends to display_element instead of overwriting it with .innerHTML)\n if (display_element.hasChildNodes()) {\n // can't loop through child list because the list will be modified by .removeChild()\n while (display_element.firstChild) {\n display_element.removeChild(display_element.firstChild);\n }\n }\n // create canvas element and image\n var canvas = document.createElement(\"canvas\");\n canvas.id = \"jspsych-image-keyboard-response-stimulus\";\n canvas.style.margin = \"0\";\n canvas.style.padding = \"0\";\n var ctx = canvas.getContext(\"2d\");\n var img = new Image();\n img.onload = () => {\n // if image wasn't preloaded, then it will need to be drawn whenever it finishes loading\n if (!image_drawn) {\n getHeightWidth(); // only possible to get width/height after image loads\n ctx.drawImage(img, 0, 0, width, height);\n }\n };\n img.src = trial.stimulus;\n // get/set image height and width - this can only be done after image loads because uses image's naturalWidth/naturalHeight properties\n const getHeightWidth = () => {\n if (trial.stimulus_height !== null) {\n height = trial.stimulus_height;\n if (trial.stimulus_width == null && trial.maintain_aspect_ratio) {\n width = img.naturalWidth * (trial.stimulus_height / img.naturalHeight);\n }\n } else {\n height = img.naturalHeight;\n }\n if (trial.stimulus_width !== null) {\n width = trial.stimulus_width;\n if (trial.stimulus_height == null && trial.maintain_aspect_ratio) {\n height = img.naturalHeight * (trial.stimulus_width / img.naturalWidth);\n }\n } else if (!(trial.stimulus_height !== null && trial.maintain_aspect_ratio)) {\n // if stimulus width is null, only use the image's natural width if the width value wasn't set\n // in the if statement above, based on a specified height and maintain_aspect_ratio = true\n width = img.naturalWidth;\n }\n canvas.height = height;\n canvas.width = width;\n };\n getHeightWidth(); // call now, in case image loads immediately (is cached)\n // add canvas and draw image\n display_element.insertBefore(canvas, null);\n if (img.complete && Number.isFinite(width) && Number.isFinite(height)) {\n // if image has loaded and width/height have been set, then draw it now\n // (don't rely on img onload function to draw image when image is in the cache, because that causes a delay in the image presentation)\n ctx.drawImage(img, 0, 0, width, height);\n image_drawn = true;\n }\n // add prompt if there is one\n if (trial.prompt !== null) {\n display_element.insertAdjacentHTML(\"beforeend\", trial.prompt);\n }\n } else {\n // display stimulus as an image element\n var html = '<img src=\"' + trial.stimulus + '\" id=\"jspsych-image-keyboard-response-stimulus\">';\n // add prompt\n if (trial.prompt !== null) {\n html += trial.prompt;\n }\n // update the page content\n display_element.innerHTML = html;\n\n // set image dimensions after image has loaded (so that we have access to naturalHeight/naturalWidth)\n var img = display_element.querySelector(\n \"#jspsych-image-keyboard-response-stimulus\"\n ) as HTMLImageElement;\n if (trial.stimulus_height !== null) {\n height = trial.stimulus_height;\n if (trial.stimulus_width == null && trial.maintain_aspect_ratio) {\n width = img.naturalWidth * (trial.stimulus_height / img.naturalHeight);\n }\n } else {\n height = img.naturalHeight;\n }\n if (trial.stimulus_width !== null) {\n width = trial.stimulus_width;\n if (trial.stimulus_height == null && trial.maintain_aspect_ratio) {\n height = img.naturalHeight * (trial.stimulus_width / img.naturalWidth);\n }\n } else if (!(trial.stimulus_height !== null && trial.maintain_aspect_ratio)) {\n // if stimulus width is null, only use the image's natural width if the width value wasn't set\n // in the if statement above, based on a specified height and maintain_aspect_ratio = true\n width = img.naturalWidth;\n }\n img.style.height = height.toString() + \"px\";\n img.style.width = width.toString() + \"px\";\n }\n\n // store response\n var response = {\n rt: null,\n key: null,\n };\n\n // function to end trial when it is time\n const end_trial = () => {\n // kill any remaining setTimeout handlers\n this.jsPsych.pluginAPI.clearAllTimeouts();\n\n // kill keyboard listeners\n if (typeof keyboardListener !== \"undefined\") {\n this.jsPsych.pluginAPI.cancelKeyboardResponse(keyboardListener);\n }\n\n // gather the data to store for the trial\n var trial_data = {\n rt: response.rt,\n stimulus: trial.stimulus,\n response: response.key,\n };\n\n // clear the display\n display_element.innerHTML = \"\";\n\n // move on to the next trial\n this.jsPsych.finishTrial(trial_data);\n };\n\n // function to handle responses by the subject\n var after_response = (info) => {\n // after a valid response, the stimulus will have the CSS class 'responded'\n // which can be used to provide visual feedback that a response was recorded\n display_element.querySelector(\"#jspsych-image-keyboard-response-stimulus\").className +=\n \" responded\";\n\n // only record the first response\n if (response.key == null) {\n response = info;\n }\n\n if (trial.response_ends_trial) {\n end_trial();\n }\n };\n\n // start the response listener\n if (trial.choices != \"NO_KEYS\") {\n var keyboardListener = this.jsPsych.pluginAPI.getKeyboardResponse({\n callback_function: after_response,\n valid_responses: trial.choices,\n rt_method: \"performance\",\n persist: false,\n allow_held_key: false,\n });\n }\n\n // hide stimulus if stimulus_duration is set\n if (trial.stimulus_duration !== null) {\n this.jsPsych.pluginAPI.setTimeout(() => {\n display_element.querySelector<HTMLElement>(\n \"#jspsych-image-keyboard-response-stimulus\"\n ).style.visibility = \"hidden\";\n }, trial.stimulus_duration);\n }\n\n // end trial if trial_duration is set\n if (trial.trial_duration !== null) {\n this.jsPsych.pluginAPI.setTimeout(() => {\n end_trial();\n }, trial.trial_duration);\n } else if (trial.response_ends_trial === false) {\n console.warn(\n \"The experiment may be deadlocked. Try setting a trial duration or set response_ends_trial to true.\"\n );\n }\n }\n\n simulate(\n trial: TrialType<Info>,\n simulation_mode,\n simulation_options: any,\n load_callback: () => void\n ) {\n if (simulation_mode == \"data-only\") {\n load_callback();\n this.simulate_data_only(trial, simulation_options);\n }\n if (simulation_mode == \"visual\") {\n this.simulate_visual(trial, simulation_options, load_callback);\n }\n }\n\n private simulate_data_only(trial: TrialType<Info>, simulation_options) {\n const data = this.create_simulation_data(trial, simulation_options);\n\n this.jsPsych.finishTrial(data);\n }\n\n private simulate_visual(trial: TrialType<Info>, simulation_options, load_callback: () => void) {\n const data = this.create_simulation_data(trial, simulation_options);\n\n const display_element = this.jsPsych.getDisplayElement();\n\n this.trial(display_element, trial);\n load_callback();\n\n if (data.rt !== null) {\n this.jsPsych.pluginAPI.pressKey(data.response, data.rt);\n }\n }\n\n private create_simulation_data(trial: TrialType<Info>, simulation_options) {\n const default_data = {\n stimulus: trial.stimulus,\n rt: this.jsPsych.randomization.sampleExGaussian(500, 50, 1 / 150, true),\n response: this.jsPsych.pluginAPI.getValidKey(trial.choices),\n };\n\n const data = this.jsPsych.pluginAPI.mergeSimulationData(default_data, simulation_options);\n\n this.jsPsych.pluginAPI.ensureSimulationDataConsistency(trial, data);\n\n return data;\n }\n}\n\nexport default ImageKeyboardResponsePlugin;\n"],"names":["ParameterType"],"mappings":";;;EAEA,MAAM,IAAI,GAAU;MAClB,IAAI,EAAE,yBAAyB;MAC/B,UAAU,EAAE;;UAEV,QAAQ,EAAE;cACR,IAAI,EAAEA,qBAAa,CAAC,KAAK;cACzB,WAAW,EAAE,UAAU;cACvB,OAAO,EAAE,SAAS;WACnB;;UAED,eAAe,EAAE;cACf,IAAI,EAAEA,qBAAa,CAAC,GAAG;cACvB,WAAW,EAAE,cAAc;cAC3B,OAAO,EAAE,IAAI;WACd;;UAED,cAAc,EAAE;cACd,IAAI,EAAEA,qBAAa,CAAC,GAAG;cACvB,WAAW,EAAE,aAAa;cAC1B,OAAO,EAAE,IAAI;WACd;;UAED,qBAAqB,EAAE;cACrB,IAAI,EAAEA,qBAAa,CAAC,IAAI;cACxB,WAAW,EAAE,uBAAuB;cACpC,OAAO,EAAE,IAAI;WACd;;UAED,OAAO,EAAE;cACP,IAAI,EAAEA,qBAAa,CAAC,IAAI;cACxB,WAAW,EAAE,SAAS;cACtB,OAAO,EAAE,UAAU;WACpB;;UAED,MAAM,EAAE;cACN,IAAI,EAAEA,qBAAa,CAAC,WAAW;cAC/B,WAAW,EAAE,QAAQ;cACrB,OAAO,EAAE,IAAI;WACd;;UAED,iBAAiB,EAAE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e={name:"image-keyboard-response",parameters:{stimulus:{type:t.ParameterType.IMAGE,pretty_name:"Stimulus",default:void 0},stimulus_height:{type:t.ParameterType.INT,pretty_name:"Image height",default:null},stimulus_width:{type:t.ParameterType.INT,pretty_name:"Image width",default:null},maintain_aspect_ratio:{type:t.ParameterType.BOOL,pretty_name:"Maintain aspect ratio",default:!0},choices:{type:t.ParameterType.KEYS,pretty_name:"Choices",default:"ALL_KEYS"},prompt:{type:t.ParameterType.HTML_STRING,pretty_name:"Prompt",default:null},stimulus_duration:{type:t.ParameterType.INT,pretty_name:"Stimulus duration",default:null},trial_duration:{type:t.ParameterType.INT,pretty_name:"Trial duration",default:null},response_ends_trial:{type:t.ParameterType.BOOL,pretty_name:"Response ends trial",default:!0},render_on_canvas:{type:t.ParameterType.BOOL,pretty_name:"Render on canvas",default:!0}}};class s{constructor(t){this.jsPsych=t}trial(t,e){var s,i;if(e.render_on_canvas){var a=!1;if(t.hasChildNodes())for(;t.firstChild;)t.removeChild(t.firstChild);var
|
|
1
|
+
var jsPsychImageKeyboardResponse=function(t){"use strict";const e={name:"image-keyboard-response",parameters:{stimulus:{type:t.ParameterType.IMAGE,pretty_name:"Stimulus",default:void 0},stimulus_height:{type:t.ParameterType.INT,pretty_name:"Image height",default:null},stimulus_width:{type:t.ParameterType.INT,pretty_name:"Image width",default:null},maintain_aspect_ratio:{type:t.ParameterType.BOOL,pretty_name:"Maintain aspect ratio",default:!0},choices:{type:t.ParameterType.KEYS,pretty_name:"Choices",default:"ALL_KEYS"},prompt:{type:t.ParameterType.HTML_STRING,pretty_name:"Prompt",default:null},stimulus_duration:{type:t.ParameterType.INT,pretty_name:"Stimulus duration",default:null},trial_duration:{type:t.ParameterType.INT,pretty_name:"Trial duration",default:null},response_ends_trial:{type:t.ParameterType.BOOL,pretty_name:"Response ends trial",default:!0},render_on_canvas:{type:t.ParameterType.BOOL,pretty_name:"Render on canvas",default:!0}}};class s{constructor(t){this.jsPsych=t}trial(t,e){var s,i;if(e.render_on_canvas){var a=!1;if(t.hasChildNodes())for(;t.firstChild;)t.removeChild(t.firstChild);var l=document.createElement("canvas");l.id="jspsych-image-keyboard-response-stimulus",l.style.margin="0",l.style.padding="0";var r=l.getContext("2d");(u=new Image).onload=()=>{a||(n(),r.drawImage(u,0,0,i,s))},u.src=e.stimulus;const n=()=>{null!==e.stimulus_height?(s=e.stimulus_height,null==e.stimulus_width&&e.maintain_aspect_ratio&&(i=u.naturalWidth*(e.stimulus_height/u.naturalHeight))):s=u.naturalHeight,null!==e.stimulus_width?(i=e.stimulus_width,null==e.stimulus_height&&e.maintain_aspect_ratio&&(s=u.naturalHeight*(e.stimulus_width/u.naturalWidth))):null!==e.stimulus_height&&e.maintain_aspect_ratio||(i=u.naturalWidth),l.height=s,l.width=i};n(),t.insertBefore(l,null),u.complete&&Number.isFinite(i)&&Number.isFinite(s)&&(r.drawImage(u,0,0,i,s),a=!0),null!==e.prompt&&t.insertAdjacentHTML("beforeend",e.prompt)}else{var n='<img src="'+e.stimulus+'" id="jspsych-image-keyboard-response-stimulus">';null!==e.prompt&&(n+=e.prompt),t.innerHTML=n;var u=t.querySelector("#jspsych-image-keyboard-response-stimulus");null!==e.stimulus_height?(s=e.stimulus_height,null==e.stimulus_width&&e.maintain_aspect_ratio&&(i=u.naturalWidth*(e.stimulus_height/u.naturalHeight))):s=u.naturalHeight,null!==e.stimulus_width?(i=e.stimulus_width,null==e.stimulus_height&&e.maintain_aspect_ratio&&(s=u.naturalHeight*(e.stimulus_width/u.naturalWidth))):null!==e.stimulus_height&&e.maintain_aspect_ratio||(i=u.naturalWidth),u.style.height=s.toString()+"px",u.style.width=i.toString()+"px"}var m={rt:null,key:null};const o=()=>{this.jsPsych.pluginAPI.clearAllTimeouts(),void 0!==h&&this.jsPsych.pluginAPI.cancelKeyboardResponse(h);var s={rt:m.rt,stimulus:e.stimulus,response:m.key};t.innerHTML="",this.jsPsych.finishTrial(s)};if("NO_KEYS"!=e.choices)var h=this.jsPsych.pluginAPI.getKeyboardResponse({callback_function:s=>{t.querySelector("#jspsych-image-keyboard-response-stimulus").className+=" responded",null==m.key&&(m=s),e.response_ends_trial&&o()},valid_responses:e.choices,rt_method:"performance",persist:!1,allow_held_key:!1});null!==e.stimulus_duration&&this.jsPsych.pluginAPI.setTimeout((()=>{t.querySelector("#jspsych-image-keyboard-response-stimulus").style.visibility="hidden"}),e.stimulus_duration),null!==e.trial_duration?this.jsPsych.pluginAPI.setTimeout((()=>{o()}),e.trial_duration):!1===e.response_ends_trial&&console.warn("The experiment may be deadlocked. Try setting a trial duration or set response_ends_trial to true.")}simulate(t,e,s,i){"data-only"==e&&(i(),this.simulate_data_only(t,s)),"visual"==e&&this.simulate_visual(t,s,i)}simulate_data_only(t,e){const s=this.create_simulation_data(t,e);this.jsPsych.finishTrial(s)}simulate_visual(t,e,s){const i=this.create_simulation_data(t,e),a=this.jsPsych.getDisplayElement();this.trial(a,t),s(),null!==i.rt&&this.jsPsych.pluginAPI.pressKey(i.response,i.rt)}create_simulation_data(t,e){const s={stimulus:t.stimulus,rt:this.jsPsych.randomization.sampleExGaussian(500,50,1/150,!0),response:this.jsPsych.pluginAPI.getValidKey(t.choices)},i=this.jsPsych.pluginAPI.mergeSimulationData(s,e);return this.jsPsych.pluginAPI.ensureSimulationDataConsistency(t,i),i}}return s.info=e,s}(jsPsychModule);
|
|
2
2
|
//# sourceMappingURL=index.browser.min.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.browser.min.js","sources":["../src/index.ts"],"sourcesContent":["import { JsPsych, JsPsychPlugin, ParameterType, TrialType } from \"jspsych\";\n\nconst info = <const>{\n name: \"image-keyboard-response\",\n parameters: {\n /** The image to be displayed */\n stimulus: {\n type: ParameterType.IMAGE,\n pretty_name: \"Stimulus\",\n default: undefined,\n },\n /** Set the image height in pixels */\n stimulus_height: {\n type: ParameterType.INT,\n pretty_name: \"Image height\",\n default: null,\n },\n /** Set the image width in pixels */\n stimulus_width: {\n type: ParameterType.INT,\n pretty_name: \"Image width\",\n default: null,\n },\n /** Maintain the aspect ratio after setting width or height */\n maintain_aspect_ratio: {\n type: ParameterType.BOOL,\n pretty_name: \"Maintain aspect ratio\",\n default: true,\n },\n /** Array containing the key(s) the subject is allowed to press to respond to the stimulus. */\n choices: {\n type: ParameterType.KEYS,\n pretty_name: \"Choices\",\n default: \"ALL_KEYS\",\n },\n /** Any content here will be displayed below the stimulus. */\n prompt: {\n type: ParameterType.HTML_STRING,\n pretty_name: \"Prompt\",\n default: null,\n },\n /** How long to show the stimulus. */\n stimulus_duration: {\n type: ParameterType.INT,\n pretty_name: \"Stimulus duration\",\n default: null,\n },\n /** How long to show trial before it ends */\n trial_duration: {\n type: ParameterType.INT,\n pretty_name: \"Trial duration\",\n default: null,\n },\n /** If true, trial will end when subject makes a response. */\n response_ends_trial: {\n type: ParameterType.BOOL,\n pretty_name: \"Response ends trial\",\n default: true,\n },\n /**\n * If true, the image will be drawn onto a canvas element (prevents blank screen between consecutive images in some browsers).\n * If false, the image will be shown via an img element.\n */\n render_on_canvas: {\n type: ParameterType.BOOL,\n pretty_name: \"Render on canvas\",\n default: true,\n },\n },\n};\n\ntype Info = typeof info;\n\n/**\n * **image-keyboard-response**\n *\n * jsPsych plugin for displaying an image stimulus and getting a keyboard response\n *\n * @author Josh de Leeuw\n * @see {@link https://www.jspsych.org/plugins/jspsych-image-keyboard-response/ image-keyboard-response plugin documentation on jspsych.org}\n */\nclass ImageKeyboardResponsePlugin implements JsPsychPlugin<Info> {\n static info = info;\n\n constructor(private jsPsych: JsPsych) {}\n\n trial(display_element: HTMLElement, trial: TrialType<Info>) {\n var height, width;\n if (trial.render_on_canvas) {\n var image_drawn = false;\n // first clear the display element (because the render_on_canvas method appends to display_element instead of overwriting it with .innerHTML)\n if (display_element.hasChildNodes()) {\n // can't loop through child list because the list will be modified by .removeChild()\n while (display_element.firstChild) {\n display_element.removeChild(display_element.firstChild);\n }\n }\n // create canvas element and image\n var canvas = document.createElement(\"canvas\");\n canvas.id = \"jspsych-image-keyboard-response-stimulus\";\n canvas.style.margin = \"0\";\n canvas.style.padding = \"0\";\n var ctx = canvas.getContext(\"2d\");\n var img = new Image();\n img.onload = function () {\n // if image wasn't preloaded, then it will need to be drawn whenever it finishes loading\n if (!image_drawn) {\n getHeightWidth(); // only possible to get width/height after image loads\n ctx.drawImage(img, 0, 0, width, height);\n }\n };\n img.src = trial.stimulus;\n // get/set image height and width - this can only be done after image loads because uses image's naturalWidth/naturalHeight properties\n const getHeightWidth = () => {\n if (trial.stimulus_height !== null) {\n height = trial.stimulus_height;\n if (trial.stimulus_width == null && trial.maintain_aspect_ratio) {\n width = img.naturalWidth * (trial.stimulus_height / img.naturalHeight);\n }\n } else {\n height = img.naturalHeight;\n }\n if (trial.stimulus_width !== null) {\n width = trial.stimulus_width;\n if (trial.stimulus_height == null && trial.maintain_aspect_ratio) {\n height = img.naturalHeight * (trial.stimulus_width / img.naturalWidth);\n }\n } else if (!(trial.stimulus_height !== null && trial.maintain_aspect_ratio)) {\n // if stimulus width is null, only use the image's natural width if the width value wasn't set\n // in the if statement above, based on a specified height and maintain_aspect_ratio = true\n width = img.naturalWidth;\n }\n canvas.height = height;\n canvas.width = width;\n };\n getHeightWidth(); // call now, in case image loads immediately (is cached)\n // add canvas and draw image\n display_element.insertBefore(canvas, null);\n if (img.complete && Number.isFinite(width) && Number.isFinite(height)) {\n // if image has loaded and width/height have been set, then draw it now\n // (don't rely on img onload function to draw image when image is in the cache, because that causes a delay in the image presentation)\n ctx.drawImage(img, 0, 0, width, height);\n image_drawn = true;\n }\n // add prompt if there is one\n if (trial.prompt !== null) {\n display_element.insertAdjacentHTML(\"beforeend\", trial.prompt);\n }\n } else {\n // display stimulus as an image element\n var html = '<img src=\"' + trial.stimulus + '\" id=\"jspsych-image-keyboard-response-stimulus\">';\n // add prompt\n if (trial.prompt !== null) {\n html += trial.prompt;\n }\n // update the page content\n display_element.innerHTML = html;\n\n // set image dimensions after image has loaded (so that we have access to naturalHeight/naturalWidth)\n var img = display_element.querySelector(\n \"#jspsych-image-keyboard-response-stimulus\"\n ) as HTMLImageElement;\n if (trial.stimulus_height !== null) {\n height = trial.stimulus_height;\n if (trial.stimulus_width == null && trial.maintain_aspect_ratio) {\n width = img.naturalWidth * (trial.stimulus_height / img.naturalHeight);\n }\n } else {\n height = img.naturalHeight;\n }\n if (trial.stimulus_width !== null) {\n width = trial.stimulus_width;\n if (trial.stimulus_height == null && trial.maintain_aspect_ratio) {\n height = img.naturalHeight * (trial.stimulus_width / img.naturalWidth);\n }\n } else if (!(trial.stimulus_height !== null && trial.maintain_aspect_ratio)) {\n // if stimulus width is null, only use the image's natural width if the width value wasn't set\n // in the if statement above, based on a specified height and maintain_aspect_ratio = true\n width = img.naturalWidth;\n }\n img.style.height = height.toString() + \"px\";\n img.style.width = width.toString() + \"px\";\n }\n\n // store response\n var response = {\n rt: null,\n key: null,\n };\n\n // function to end trial when it is time\n const end_trial = () => {\n // kill any remaining setTimeout handlers\n this.jsPsych.pluginAPI.clearAllTimeouts();\n\n // kill keyboard listeners\n if (typeof keyboardListener !== \"undefined\") {\n this.jsPsych.pluginAPI.cancelKeyboardResponse(keyboardListener);\n }\n\n // gather the data to store for the trial\n var trial_data = {\n rt: response.rt,\n stimulus: trial.stimulus,\n response: response.key,\n };\n\n // clear the display\n display_element.innerHTML = \"\";\n\n // move on to the next trial\n this.jsPsych.finishTrial(trial_data);\n };\n\n // function to handle responses by the subject\n var after_response = function (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(function () {\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(function () {\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\nexport default ImageKeyboardResponsePlugin;\n"],"names":["info","name","parameters","stimulus","type","ParameterType","IMAGE","pretty_name","default","undefined","stimulus_height","INT","stimulus_width","maintain_aspect_ratio","BOOL","choices","KEYS","prompt","HTML_STRING","stimulus_duration","trial_duration","response_ends_trial","render_on_canvas","ImageKeyboardResponsePlugin","constructor","jsPsych","this","trial","display_element","height","width","image_drawn","hasChildNodes","firstChild","removeChild","canvas","document","createElement","id","style","margin","padding","ctx","getContext","img","Image","onload","getHeightWidth","drawImage","src","naturalWidth","naturalHeight","insertBefore","complete","Number","isFinite","insertAdjacentHTML","html","innerHTML","querySelector","toString","response","rt","key","end_trial","pluginAPI","clearAllTimeouts","keyboardListener","cancelKeyboardResponse","trial_data","finishTrial","getKeyboardResponse","callback_function","className","valid_responses","rt_method","persist","allow_held_key","setTimeout","visibility","console","warn"],"mappings":"0DAEA,MAAMA,EAAc,CAClBC,KAAM,0BACNC,WAAY,CAEVC,SAAU,CACRC,KAAMC,gBAAcC,MACpBC,YAAa,WACbC,aAASC,GAGXC,gBAAiB,CACfN,KAAMC,gBAAcM,IACpBJ,YAAa,eACbC,QAAS,MAGXI,eAAgB,CACdR,KAAMC,gBAAcM,IACpBJ,YAAa,cACbC,QAAS,MAGXK,sBAAuB,CACrBT,KAAMC,gBAAcS,KACpBP,YAAa,wBACbC,SAAS,GAGXO,QAAS,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qB,WAAjB1C,EAAMZ,QACR,IAAIoD,EAAmBzC,KAAKD,QAAQwC,UAAUM,oBAAoB,CAChEC,kBAnBiB,SAAUxE,GAG7B4B,EAAgB+B,cAAc,6CAA6Cc,WACzE,aAGkB,MAAhBZ,EAASE,MACXF,EAAW7D,GAGT2B,EAAMN,qBACR2C,KAQAU,gBAAiB/C,EAAMZ,QACvB4D,UAAW,cACXC,SAAS,EACTC,gBAAgB,IAKY,OAA5BlD,EAAMR,mBACRO,KAAKD,QAAQwC,UAAUa,YAAW,WAChClD,EAAgB+B,cACd,6CACApB,MAAMwC,WAAa,WACpBpD,EAAMR,mBAIkB,OAAzBQ,EAAMP,eACRM,KAAKD,QAAQwC,UAAUa,YAAW,WAChCd,MACCrC,EAAMP,iBAC8B,IAA9BO,EAAMN,qBACf2D,QAAQC,KACN,8GAhLC1D,OAAOvB"}
|
|
1
|
+
{"version":3,"file":"index.browser.min.js","sources":["../src/index.ts"],"sourcesContent":["import { JsPsych, JsPsychPlugin, ParameterType, TrialType } from \"jspsych\";\n\nconst info = <const>{\n name: \"image-keyboard-response\",\n parameters: {\n /** The image to be displayed */\n stimulus: {\n type: ParameterType.IMAGE,\n pretty_name: \"Stimulus\",\n default: undefined,\n },\n /** Set the image height in pixels */\n stimulus_height: {\n type: ParameterType.INT,\n pretty_name: \"Image height\",\n default: null,\n },\n /** Set the image width in pixels */\n stimulus_width: {\n type: ParameterType.INT,\n pretty_name: \"Image width\",\n default: null,\n },\n /** Maintain the aspect ratio after setting width or height */\n maintain_aspect_ratio: {\n type: ParameterType.BOOL,\n pretty_name: \"Maintain aspect ratio\",\n default: true,\n },\n /** Array containing the key(s) the subject is allowed to press to respond to the stimulus. */\n choices: {\n type: ParameterType.KEYS,\n pretty_name: \"Choices\",\n default: \"ALL_KEYS\",\n },\n /** Any content here will be displayed below the stimulus. */\n prompt: {\n type: ParameterType.HTML_STRING,\n pretty_name: \"Prompt\",\n default: null,\n },\n /** How long to show the stimulus. */\n stimulus_duration: {\n type: ParameterType.INT,\n pretty_name: \"Stimulus duration\",\n default: null,\n },\n /** How long to show trial before it ends */\n trial_duration: {\n type: ParameterType.INT,\n pretty_name: \"Trial duration\",\n default: null,\n },\n /** If true, trial will end when subject makes a response. */\n response_ends_trial: {\n type: ParameterType.BOOL,\n pretty_name: \"Response ends trial\",\n default: true,\n },\n /**\n * If true, the image will be drawn onto a canvas element (prevents blank screen between consecutive images in some browsers).\n * If false, the image will be shown via an img element.\n */\n render_on_canvas: {\n type: ParameterType.BOOL,\n pretty_name: \"Render on canvas\",\n default: true,\n },\n },\n};\n\ntype Info = typeof info;\n\n/**\n * **image-keyboard-response**\n *\n * jsPsych plugin for displaying an image stimulus and getting a keyboard response\n *\n * @author Josh de Leeuw\n * @see {@link https://www.jspsych.org/plugins/jspsych-image-keyboard-response/ image-keyboard-response plugin documentation on jspsych.org}\n */\nclass ImageKeyboardResponsePlugin implements JsPsychPlugin<Info> {\n static info = info;\n\n constructor(private jsPsych: JsPsych) {}\n\n trial(display_element: HTMLElement, trial: TrialType<Info>) {\n var height, width;\n if (trial.render_on_canvas) {\n var image_drawn = false;\n // first clear the display element (because the render_on_canvas method appends to display_element instead of overwriting it with .innerHTML)\n if (display_element.hasChildNodes()) {\n // can't loop through child list because the list will be modified by .removeChild()\n while (display_element.firstChild) {\n display_element.removeChild(display_element.firstChild);\n }\n }\n // create canvas element and image\n var canvas = document.createElement(\"canvas\");\n canvas.id = \"jspsych-image-keyboard-response-stimulus\";\n canvas.style.margin = \"0\";\n canvas.style.padding = \"0\";\n var ctx = canvas.getContext(\"2d\");\n var img = new Image();\n img.onload = () => {\n // if image wasn't preloaded, then it will need to be drawn whenever it finishes loading\n if (!image_drawn) {\n getHeightWidth(); // only possible to get width/height after image loads\n ctx.drawImage(img, 0, 0, width, height);\n }\n };\n img.src = trial.stimulus;\n // get/set image height and width - this can only be done after image loads because uses image's naturalWidth/naturalHeight properties\n const getHeightWidth = () => {\n if (trial.stimulus_height !== null) {\n height = trial.stimulus_height;\n if (trial.stimulus_width == null && trial.maintain_aspect_ratio) {\n width = img.naturalWidth * (trial.stimulus_height / img.naturalHeight);\n }\n } else {\n height = img.naturalHeight;\n }\n if (trial.stimulus_width !== null) {\n width = trial.stimulus_width;\n if (trial.stimulus_height == null && trial.maintain_aspect_ratio) {\n height = img.naturalHeight * (trial.stimulus_width / img.naturalWidth);\n }\n } else if (!(trial.stimulus_height !== null && trial.maintain_aspect_ratio)) {\n // if stimulus width is null, only use the image's natural width if the width value wasn't set\n // in the if statement above, based on a specified height and maintain_aspect_ratio = true\n width = img.naturalWidth;\n }\n canvas.height = height;\n canvas.width = width;\n };\n getHeightWidth(); // call now, in case image loads immediately (is cached)\n // add canvas and draw image\n display_element.insertBefore(canvas, null);\n if (img.complete && Number.isFinite(width) && Number.isFinite(height)) {\n // if image has loaded and width/height have been set, then draw it now\n // (don't rely on img onload function to draw image when image is in the cache, because that causes a delay in the image presentation)\n ctx.drawImage(img, 0, 0, width, height);\n image_drawn = true;\n }\n // add prompt if there is one\n if (trial.prompt !== null) {\n display_element.insertAdjacentHTML(\"beforeend\", trial.prompt);\n }\n } else {\n // display stimulus as an image element\n var html = '<img src=\"' + trial.stimulus + '\" id=\"jspsych-image-keyboard-response-stimulus\">';\n // add prompt\n if (trial.prompt !== null) {\n html += trial.prompt;\n }\n // update the page content\n display_element.innerHTML = html;\n\n // set image dimensions after image has loaded (so that we have access to naturalHeight/naturalWidth)\n var img = display_element.querySelector(\n \"#jspsych-image-keyboard-response-stimulus\"\n ) as HTMLImageElement;\n if (trial.stimulus_height !== null) {\n height = trial.stimulus_height;\n if (trial.stimulus_width == null && trial.maintain_aspect_ratio) {\n width = img.naturalWidth * (trial.stimulus_height / img.naturalHeight);\n }\n } else {\n height = img.naturalHeight;\n }\n if (trial.stimulus_width !== null) {\n width = trial.stimulus_width;\n if (trial.stimulus_height == null && trial.maintain_aspect_ratio) {\n height = img.naturalHeight * (trial.stimulus_width / img.naturalWidth);\n }\n } else if (!(trial.stimulus_height !== null && trial.maintain_aspect_ratio)) {\n // if stimulus width is null, only use the image's natural width if the width value wasn't set\n // in the if statement above, based on a specified height and maintain_aspect_ratio = true\n width = img.naturalWidth;\n }\n img.style.height = height.toString() + \"px\";\n img.style.width = width.toString() + \"px\";\n }\n\n // store response\n var response = {\n rt: null,\n key: null,\n };\n\n // function to end trial when it is time\n const end_trial = () => {\n // kill any remaining setTimeout handlers\n this.jsPsych.pluginAPI.clearAllTimeouts();\n\n // kill keyboard listeners\n if (typeof keyboardListener !== \"undefined\") {\n this.jsPsych.pluginAPI.cancelKeyboardResponse(keyboardListener);\n }\n\n // gather the data to store for the trial\n var trial_data = {\n rt: response.rt,\n stimulus: trial.stimulus,\n response: response.key,\n };\n\n // clear the display\n display_element.innerHTML = \"\";\n\n // move on to the next trial\n this.jsPsych.finishTrial(trial_data);\n };\n\n // function to handle responses by the subject\n var after_response = (info) => {\n // after a valid response, the stimulus will have the CSS class 'responded'\n // which can be used to provide visual feedback that a response was recorded\n display_element.querySelector(\"#jspsych-image-keyboard-response-stimulus\").className +=\n \" responded\";\n\n // only record the first response\n if (response.key == null) {\n response = info;\n }\n\n if (trial.response_ends_trial) {\n end_trial();\n }\n };\n\n // start the response listener\n if (trial.choices != \"NO_KEYS\") {\n var keyboardListener = this.jsPsych.pluginAPI.getKeyboardResponse({\n callback_function: after_response,\n valid_responses: trial.choices,\n rt_method: \"performance\",\n persist: false,\n allow_held_key: false,\n });\n }\n\n // hide stimulus if stimulus_duration is set\n if (trial.stimulus_duration !== null) {\n this.jsPsych.pluginAPI.setTimeout(() => {\n display_element.querySelector<HTMLElement>(\n \"#jspsych-image-keyboard-response-stimulus\"\n ).style.visibility = \"hidden\";\n }, trial.stimulus_duration);\n }\n\n // end trial if trial_duration is set\n if (trial.trial_duration !== null) {\n this.jsPsych.pluginAPI.setTimeout(() => {\n end_trial();\n }, trial.trial_duration);\n } else if (trial.response_ends_trial === false) {\n console.warn(\n \"The experiment may be deadlocked. Try setting a trial duration or set response_ends_trial to true.\"\n );\n }\n }\n\n simulate(\n trial: TrialType<Info>,\n simulation_mode,\n simulation_options: any,\n load_callback: () => void\n ) {\n if (simulation_mode == \"data-only\") {\n load_callback();\n this.simulate_data_only(trial, simulation_options);\n }\n if (simulation_mode == \"visual\") {\n this.simulate_visual(trial, simulation_options, load_callback);\n }\n }\n\n private simulate_data_only(trial: TrialType<Info>, simulation_options) {\n const data = this.create_simulation_data(trial, simulation_options);\n\n this.jsPsych.finishTrial(data);\n }\n\n private simulate_visual(trial: TrialType<Info>, simulation_options, load_callback: () => void) {\n const data = this.create_simulation_data(trial, simulation_options);\n\n const display_element = this.jsPsych.getDisplayElement();\n\n this.trial(display_element, trial);\n load_callback();\n\n if (data.rt !== null) {\n this.jsPsych.pluginAPI.pressKey(data.response, data.rt);\n }\n }\n\n private create_simulation_data(trial: TrialType<Info>, simulation_options) {\n const default_data = {\n stimulus: trial.stimulus,\n rt: this.jsPsych.randomization.sampleExGaussian(500, 50, 1 / 150, true),\n response: this.jsPsych.pluginAPI.getValidKey(trial.choices),\n };\n\n const data = this.jsPsych.pluginAPI.mergeSimulationData(default_data, simulation_options);\n\n this.jsPsych.pluginAPI.ensureSimulationDataConsistency(trial, data);\n\n return data;\n }\n}\n\nexport default ImageKeyboardResponsePlugin;\n"],"names":["info","name","parameters","stimulus","type","ParameterType","IMAGE","pretty_name","default","undefined","stimulus_height","INT","stimulus_width","maintain_aspect_ratio","BOOL","choices","KEYS","prompt","HTML_STRING","stimulus_duration","trial_duration","response_ends_trial","render_on_canvas","ImageKeyboardResponsePlugin","constructor","jsPsych","this","trial","display_element","height","width","image_drawn","hasChildNodes","firstChild","removeChild","canvas","document","createElement","id","style","margin","padding","ctx","getContext","img","Image","onload","getHeightWidth","drawImage","src","naturalWidth","naturalHeight","insertBefore","complete","Number","isFinite","insertAdjacentHTML","html","innerHTML","querySelector","toString","response","rt","key","end_trial","pluginAPI","clearAllTimeouts","keyboardListener","cancelKeyboardResponse","trial_data","finishTrial","getKeyboardResponse","callback_function","className","valid_responses","rt_method","persist","allow_held_key","setTimeout","visibility","console","warn","simulate","simulation_mode","simulation_options","load_callback","simulate_data_only","simulate_visual","data","create_simulation_data","getDisplayElement","pressKey","default_data","randomization","sampleExGaussian","getValidKey","mergeSimulationData","ensureSimulationDataConsistency"],"mappings":"0DAEA,MAAMA,EAAc,CAClBC,KAAM,0BACNC,WAAY,CAEVC,SAAU,CACRC,KAAMC,gBAAcC,MACpBC,YAAa,WACbC,aAASC,GAGXC,gBAAiB,CACfN,KAAMC,gBAAcM,IACpBJ,YAAa,eACbC,QAAS,MAGXI,eAAgB,CACdR,KAAMC,gBAAcM,IACpBJ,YAAa,cACbC,QAAS,MAGXK,sBAAuB,CACrBT,KAAMC,gBAAcS,KACpBP,YAAa,wBACbC,SAAS,GAGXO,QAAS,CACPX,KAAMC,gBAAcW,KACpBT,YAAa,UACbC,QAAS,YAGXS,OAAQ,CACNb,KAAMC,gBAAca,YACpBX,YAAa,SACbC,QAAS,MAGXW,kBAAmB,CACjBf,KAAMC,gBAAcM,IACpBJ,YAAa,oBACbC,QAAS,MAGXY,eAAgB,CACdhB,KAAMC,gBAAcM,IACpBJ,YAAa,iBACbC,QAAS,MAGXa,oBAAqB,CACnBjB,KAAMC,gBAAcS,KACpBP,YAAa,sBACbC,SAAS,GAMXc,iBAAkB,CAChBlB,KAAMC,gBAAcS,KACpBP,YAAa,mBACbC,SAAS,KAef,MAAMe,EAGJC,YAAoBC,GAAAC,aAAAD,EAEpBE,MAAMC,EAA8BD,GAClC,IAAIE,EAAQC,EACZ,GAAIH,EAAML,iBAAkB,CAC1B,IAAIS,GAAc,EAElB,GAAIH,EAAgBI,gBAElB,KAAOJ,EAAgBK,YACrBL,EAAgBM,YAAYN,EAAgBK,YAIhD,IAAIE,EAASC,SAASC,cAAc,UACpCF,EAAOG,GAAK,2CACZH,EAAOI,MAAMC,OAAS,IACtBL,EAAOI,MAAME,QAAU,IACvB,IAAIC,EAAMP,EAAOQ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,uBAAuB9D,EAAOyD,GAEhD1D,KAAKD,QAAQ6C,YAAYkB,GAGnBD,gBAAgB5D,EAAwByD,EAAoBC,GAClE,MAAMG,EAAO9D,KAAK+D,uBAAuB9D,EAAOyD,GAE1CxD,EAAkBF,KAAKD,QAAQiE,oBAErChE,KAAKC,MAAMC,EAAiBD,GAC5B0D,IAEgB,OAAZG,EAAK1B,IACPpC,KAAKD,QAAQwC,UAAU0B,SAASH,EAAK3B,SAAU2B,EAAK1B,IAIhD2B,uBAAuB9D,EAAwByD,GACrD,MAAMQ,EAAe,CACnBzF,SAAUwB,EAAMxB,SAChB2D,GAAIpC,KAAKD,QAAQoE,cAAcC,iBAAiB,IAAK,GAAI,EAAI,KAAK,GAClEjC,SAAUnC,KAAKD,QAAQwC,UAAU8B,YAAYpE,EAAMZ,UAG/CyE,EAAO9D,KAAKD,QAAQwC,UAAU+B,oBAAoBJ,EAAcR,GAItE,OAFA1D,KAAKD,QAAQwC,UAAUgC,gCAAgCtE,EAAO6D,GAEvDA,UAlOFjE,OAAOvB"}
|
package/dist/index.cjs
CHANGED
|
@@ -100,7 +100,7 @@ class ImageKeyboardResponsePlugin {
|
|
|
100
100
|
canvas.style.padding = "0";
|
|
101
101
|
var ctx = canvas.getContext("2d");
|
|
102
102
|
var img = new Image();
|
|
103
|
-
img.onload =
|
|
103
|
+
img.onload = () => {
|
|
104
104
|
// if image wasn't preloaded, then it will need to be drawn whenever it finishes loading
|
|
105
105
|
if (!image_drawn) {
|
|
106
106
|
getHeightWidth(); // only possible to get width/height after image loads
|
|
@@ -206,7 +206,7 @@ class ImageKeyboardResponsePlugin {
|
|
|
206
206
|
this.jsPsych.finishTrial(trial_data);
|
|
207
207
|
};
|
|
208
208
|
// function to handle responses by the subject
|
|
209
|
-
var after_response =
|
|
209
|
+
var after_response = (info) => {
|
|
210
210
|
// after a valid response, the stimulus will have the CSS class 'responded'
|
|
211
211
|
// which can be used to provide visual feedback that a response was recorded
|
|
212
212
|
display_element.querySelector("#jspsych-image-keyboard-response-stimulus").className +=
|
|
@@ -231,13 +231,13 @@ class ImageKeyboardResponsePlugin {
|
|
|
231
231
|
}
|
|
232
232
|
// hide stimulus if stimulus_duration is set
|
|
233
233
|
if (trial.stimulus_duration !== null) {
|
|
234
|
-
this.jsPsych.pluginAPI.setTimeout(
|
|
234
|
+
this.jsPsych.pluginAPI.setTimeout(() => {
|
|
235
235
|
display_element.querySelector("#jspsych-image-keyboard-response-stimulus").style.visibility = "hidden";
|
|
236
236
|
}, trial.stimulus_duration);
|
|
237
237
|
}
|
|
238
238
|
// end trial if trial_duration is set
|
|
239
239
|
if (trial.trial_duration !== null) {
|
|
240
|
-
this.jsPsych.pluginAPI.setTimeout(
|
|
240
|
+
this.jsPsych.pluginAPI.setTimeout(() => {
|
|
241
241
|
end_trial();
|
|
242
242
|
}, trial.trial_duration);
|
|
243
243
|
}
|
|
@@ -245,6 +245,38 @@ class ImageKeyboardResponsePlugin {
|
|
|
245
245
|
console.warn("The experiment may be deadlocked. Try setting a trial duration or set response_ends_trial to true.");
|
|
246
246
|
}
|
|
247
247
|
}
|
|
248
|
+
simulate(trial, simulation_mode, simulation_options, load_callback) {
|
|
249
|
+
if (simulation_mode == "data-only") {
|
|
250
|
+
load_callback();
|
|
251
|
+
this.simulate_data_only(trial, simulation_options);
|
|
252
|
+
}
|
|
253
|
+
if (simulation_mode == "visual") {
|
|
254
|
+
this.simulate_visual(trial, simulation_options, load_callback);
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
simulate_data_only(trial, simulation_options) {
|
|
258
|
+
const data = this.create_simulation_data(trial, simulation_options);
|
|
259
|
+
this.jsPsych.finishTrial(data);
|
|
260
|
+
}
|
|
261
|
+
simulate_visual(trial, simulation_options, load_callback) {
|
|
262
|
+
const data = this.create_simulation_data(trial, simulation_options);
|
|
263
|
+
const display_element = this.jsPsych.getDisplayElement();
|
|
264
|
+
this.trial(display_element, trial);
|
|
265
|
+
load_callback();
|
|
266
|
+
if (data.rt !== null) {
|
|
267
|
+
this.jsPsych.pluginAPI.pressKey(data.response, data.rt);
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
create_simulation_data(trial, simulation_options) {
|
|
271
|
+
const default_data = {
|
|
272
|
+
stimulus: trial.stimulus,
|
|
273
|
+
rt: this.jsPsych.randomization.sampleExGaussian(500, 50, 1 / 150, true),
|
|
274
|
+
response: this.jsPsych.pluginAPI.getValidKey(trial.choices),
|
|
275
|
+
};
|
|
276
|
+
const data = this.jsPsych.pluginAPI.mergeSimulationData(default_data, simulation_options);
|
|
277
|
+
this.jsPsych.pluginAPI.ensureSimulationDataConsistency(trial, data);
|
|
278
|
+
return data;
|
|
279
|
+
}
|
|
248
280
|
}
|
|
249
281
|
ImageKeyboardResponsePlugin.info = info;
|
|
250
282
|
|
package/dist/index.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.cjs","sources":["../src/index.ts"],"sourcesContent":["import { JsPsych, JsPsychPlugin, ParameterType, TrialType } from \"jspsych\";\n\nconst info = <const>{\n name: \"image-keyboard-response\",\n parameters: {\n /** The image to be displayed */\n stimulus: {\n type: ParameterType.IMAGE,\n pretty_name: \"Stimulus\",\n default: undefined,\n },\n /** Set the image height in pixels */\n stimulus_height: {\n type: ParameterType.INT,\n pretty_name: \"Image height\",\n default: null,\n },\n /** Set the image width in pixels */\n stimulus_width: {\n type: ParameterType.INT,\n pretty_name: \"Image width\",\n default: null,\n },\n /** Maintain the aspect ratio after setting width or height */\n maintain_aspect_ratio: {\n type: ParameterType.BOOL,\n pretty_name: \"Maintain aspect ratio\",\n default: true,\n },\n /** Array containing the key(s) the subject is allowed to press to respond to the stimulus. */\n choices: {\n type: ParameterType.KEYS,\n pretty_name: \"Choices\",\n default: \"ALL_KEYS\",\n },\n /** Any content here will be displayed below the stimulus. */\n prompt: {\n type: ParameterType.HTML_STRING,\n pretty_name: \"Prompt\",\n default: null,\n },\n /** How long to show the stimulus. */\n stimulus_duration: {\n type: ParameterType.INT,\n pretty_name: \"Stimulus duration\",\n default: null,\n },\n /** How long to show trial before it ends */\n trial_duration: {\n type: ParameterType.INT,\n pretty_name: \"Trial duration\",\n default: null,\n },\n /** If true, trial will end when subject makes a response. */\n response_ends_trial: {\n type: ParameterType.BOOL,\n pretty_name: \"Response ends trial\",\n default: true,\n },\n /**\n * If true, the image will be drawn onto a canvas element (prevents blank screen between consecutive images in some browsers).\n * If false, the image will be shown via an img element.\n */\n render_on_canvas: {\n type: ParameterType.BOOL,\n pretty_name: \"Render on canvas\",\n default: true,\n },\n },\n};\n\ntype Info = typeof info;\n\n/**\n * **image-keyboard-response**\n *\n * jsPsych plugin for displaying an image stimulus and getting a keyboard response\n *\n * @author Josh de Leeuw\n * @see {@link https://www.jspsych.org/plugins/jspsych-image-keyboard-response/ image-keyboard-response plugin documentation on jspsych.org}\n */\nclass ImageKeyboardResponsePlugin implements JsPsychPlugin<Info> {\n static info = info;\n\n constructor(private jsPsych: JsPsych) {}\n\n trial(display_element: HTMLElement, trial: TrialType<Info>) {\n var height, width;\n if (trial.render_on_canvas) {\n var image_drawn = false;\n // first clear the display element (because the render_on_canvas method appends to display_element instead of overwriting it with .innerHTML)\n if (display_element.hasChildNodes()) {\n // can't loop through child list because the list will be modified by .removeChild()\n while (display_element.firstChild) {\n display_element.removeChild(display_element.firstChild);\n }\n }\n // create canvas element and image\n var canvas = document.createElement(\"canvas\");\n canvas.id = \"jspsych-image-keyboard-response-stimulus\";\n canvas.style.margin = \"0\";\n canvas.style.padding = \"0\";\n var ctx = canvas.getContext(\"2d\");\n var img = new Image();\n img.onload = function () {\n // if image wasn't preloaded, then it will need to be drawn whenever it finishes loading\n if (!image_drawn) {\n getHeightWidth(); // only possible to get width/height after image loads\n ctx.drawImage(img, 0, 0, width, height);\n }\n };\n img.src = trial.stimulus;\n // get/set image height and width - this can only be done after image loads because uses image's naturalWidth/naturalHeight properties\n const getHeightWidth = () => {\n if (trial.stimulus_height !== null) {\n height = trial.stimulus_height;\n if (trial.stimulus_width == null && trial.maintain_aspect_ratio) {\n width = img.naturalWidth * (trial.stimulus_height / img.naturalHeight);\n }\n } else {\n height = img.naturalHeight;\n }\n if (trial.stimulus_width !== null) {\n width = trial.stimulus_width;\n if (trial.stimulus_height == null && trial.maintain_aspect_ratio) {\n height = img.naturalHeight * (trial.stimulus_width / img.naturalWidth);\n }\n } else if (!(trial.stimulus_height !== null && trial.maintain_aspect_ratio)) {\n // if stimulus width is null, only use the image's natural width if the width value wasn't set\n // in the if statement above, based on a specified height and maintain_aspect_ratio = true\n width = img.naturalWidth;\n }\n canvas.height = height;\n canvas.width = width;\n };\n getHeightWidth(); // call now, in case image loads immediately (is cached)\n // add canvas and draw image\n display_element.insertBefore(canvas, null);\n if (img.complete && Number.isFinite(width) && Number.isFinite(height)) {\n // if image has loaded and width/height have been set, then draw it now\n // (don't rely on img onload function to draw image when image is in the cache, because that causes a delay in the image presentation)\n ctx.drawImage(img, 0, 0, width, height);\n image_drawn = true;\n }\n // add prompt if there is one\n if (trial.prompt !== null) {\n display_element.insertAdjacentHTML(\"beforeend\", trial.prompt);\n }\n } else {\n // display stimulus as an image element\n var html = '<img src=\"' + trial.stimulus + '\" id=\"jspsych-image-keyboard-response-stimulus\">';\n // add prompt\n if (trial.prompt !== null) {\n html += trial.prompt;\n }\n // update the page content\n display_element.innerHTML = html;\n\n // set image dimensions after image has loaded (so that we have access to naturalHeight/naturalWidth)\n var img = display_element.querySelector(\n \"#jspsych-image-keyboard-response-stimulus\"\n ) as HTMLImageElement;\n if (trial.stimulus_height !== null) {\n height = trial.stimulus_height;\n if (trial.stimulus_width == null && trial.maintain_aspect_ratio) {\n width = img.naturalWidth * (trial.stimulus_height / img.naturalHeight);\n }\n } else {\n height = img.naturalHeight;\n }\n if (trial.stimulus_width !== null) {\n width = trial.stimulus_width;\n if (trial.stimulus_height == null && trial.maintain_aspect_ratio) {\n height = img.naturalHeight * (trial.stimulus_width / img.naturalWidth);\n }\n } else if (!(trial.stimulus_height !== null && trial.maintain_aspect_ratio)) {\n // if stimulus width is null, only use the image's natural width if the width value wasn't set\n // in the if statement above, based on a specified height and maintain_aspect_ratio = true\n width = img.naturalWidth;\n }\n img.style.height = height.toString() + \"px\";\n img.style.width = width.toString() + \"px\";\n }\n\n // store response\n var response = {\n rt: null,\n key: null,\n };\n\n // function to end trial when it is time\n const end_trial = () => {\n // kill any remaining setTimeout handlers\n this.jsPsych.pluginAPI.clearAllTimeouts();\n\n // kill keyboard listeners\n if (typeof keyboardListener !== \"undefined\") {\n this.jsPsych.pluginAPI.cancelKeyboardResponse(keyboardListener);\n }\n\n // gather the data to store for the trial\n var trial_data = {\n rt: response.rt,\n stimulus: trial.stimulus,\n response: response.key,\n };\n\n // clear the display\n display_element.innerHTML = \"\";\n\n // move on to the next trial\n this.jsPsych.finishTrial(trial_data);\n };\n\n // function to handle responses by the subject\n var after_response = function (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(function () {\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(function () {\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\nexport default ImageKeyboardResponsePlugin;\n"],"names":["ParameterType"],"mappings":";;;;AAEA,MAAM,IAAI,GAAU;IAClB,IAAI,EAAE,yBAAyB;IAC/B,UAAU,EAAE;;QAEV,QAAQ,EAAE;YACR,IAAI,EAAEA,qBAAa,CAAC,KAAK;YACzB,WAAW,EAAE,UAAU;YACvB,OAAO,EAAE,SAAS;SACnB;;QAED,eAAe,EAAE;YACf,IAAI,EAAEA,qBAAa,CAAC,GAAG;YACvB,WAAW,EAAE,cAAc;YAC3B,OAAO,EAAE,IAAI;SACd;;QAED,cAAc,EAAE;YACd,IAAI,EAAEA,qBAAa,CAAC,GAAG;YACvB,WAAW,EAAE,aAAa;YAC1B,OAAO,EAAE,IAAI;SACd;;QAED,qBAAqB,EAAE;YACrB,IAAI,EAAEA,qBAAa,CAAC,IAAI;YACxB,WAAW,EAAE,uBAAuB;YACpC,OAAO,EAAE,IAAI;SACd;;QAED,OAAO,EAAE;YACP,IAAI,EAAEA,qBAAa,CAAC,IAAI;YACxB,WAAW,EAAE,SAAS;YACtB,OAAO,EAAE,UAAU;SACpB;;QAED,MAAM,EAAE;YACN,IAAI,EAAEA,qBAAa,CAAC,WAAW;YAC/B,WAAW,EAAE,QAAQ;YACrB,OAAO,EAAE,IAAI;SACd;;QAED,iBAAiB,EAAE;YACjB,IAAI,EAAEA,qBAAa,CAAC,GAAG;YACvB,WAAW,EAAE,mBAAmB;YAChC,OAAO,EAAE,IAAI;SACd;;QAED,cAAc,EAAE;YACd,IAAI,EAAEA,qBAAa,CAAC,GAAG;YACvB,WAAW,EAAE,gBAAgB;YAC7B,OAAO,EAAE,IAAI;SACd;;QAED,mBAAmB,EAAE;YACnB,IAAI,EAAEA,qBAAa,CAAC,IAAI;YACxB,WAAW,EAAE,qBAAqB;YAClC,OAAO,EAAE,IAAI;SACd;;;;;QAKD,gBAAgB,EAAE;YAChB,IAAI,EAAEA,qBAAa,CAAC,IAAI;YACxB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C,eAAe,CAAC,aAAa,CAC3B,2CAA2C,CAC5C,CAAC,KAAK,CAAC,UAAU,GAAG,QAAQ,CAAC;aAC/B,EAAE,KAAK,CAAC,iBAAiB,CAAC,CAAC;SAC7B;;QAGD,IAAI,KAAK,CAAC,cAAc,KAAK,IAAI,EAAE;YACjC,IAAI,CAAC,OAAO,CAAC,SAAS,CAAC,UAAU,CAAC;gBAChC,SAAS,EAAE,CAAC;aACb,EAAE,KAAK,CAAC,cAAc,CAAC,CAAC;SAC1B;aAAM,IAAI,KAAK,CAAC,mBAAmB,KAAK,KAAK,EAAE;YAC9C,OAAO,CAAC,IAAI,CACV,oGAAoG,CACrG,CAAC;SACH;KACF;;AAnLM,gCAAI,GAAG,IAAI;;;;"}
|
|
1
|
+
{"version":3,"file":"index.cjs","sources":["../src/index.ts"],"sourcesContent":["import { JsPsych, JsPsychPlugin, ParameterType, TrialType } from \"jspsych\";\n\nconst info = <const>{\n name: \"image-keyboard-response\",\n parameters: {\n /** The image to be displayed */\n stimulus: {\n type: ParameterType.IMAGE,\n pretty_name: \"Stimulus\",\n default: undefined,\n },\n /** Set the image height in pixels */\n stimulus_height: {\n type: ParameterType.INT,\n pretty_name: \"Image height\",\n default: null,\n },\n /** Set the image width in pixels */\n stimulus_width: {\n type: ParameterType.INT,\n pretty_name: \"Image width\",\n default: null,\n },\n /** Maintain the aspect ratio after setting width or height */\n maintain_aspect_ratio: {\n type: ParameterType.BOOL,\n pretty_name: \"Maintain aspect ratio\",\n default: true,\n },\n /** Array containing the key(s) the subject is allowed to press to respond to the stimulus. */\n choices: {\n type: ParameterType.KEYS,\n pretty_name: \"Choices\",\n default: \"ALL_KEYS\",\n },\n /** Any content here will be displayed below the stimulus. */\n prompt: {\n type: ParameterType.HTML_STRING,\n pretty_name: \"Prompt\",\n default: null,\n },\n /** How long to show the stimulus. */\n stimulus_duration: {\n type: ParameterType.INT,\n pretty_name: \"Stimulus duration\",\n default: null,\n },\n /** How long to show trial before it ends */\n trial_duration: {\n type: ParameterType.INT,\n pretty_name: \"Trial duration\",\n default: null,\n },\n /** If true, trial will end when subject makes a response. */\n response_ends_trial: {\n type: ParameterType.BOOL,\n pretty_name: \"Response ends trial\",\n default: true,\n },\n /**\n * If true, the image will be drawn onto a canvas element (prevents blank screen between consecutive images in some browsers).\n * If false, the image will be shown via an img element.\n */\n render_on_canvas: {\n type: ParameterType.BOOL,\n pretty_name: \"Render on canvas\",\n default: true,\n },\n },\n};\n\ntype Info = typeof info;\n\n/**\n * **image-keyboard-response**\n *\n * jsPsych plugin for displaying an image stimulus and getting a keyboard response\n *\n * @author Josh de Leeuw\n * @see {@link https://www.jspsych.org/plugins/jspsych-image-keyboard-response/ image-keyboard-response plugin documentation on jspsych.org}\n */\nclass ImageKeyboardResponsePlugin implements JsPsychPlugin<Info> {\n static info = info;\n\n constructor(private jsPsych: JsPsych) {}\n\n trial(display_element: HTMLElement, trial: TrialType<Info>) {\n var height, width;\n if (trial.render_on_canvas) {\n var image_drawn = false;\n // first clear the display element (because the render_on_canvas method appends to display_element instead of overwriting it with .innerHTML)\n if (display_element.hasChildNodes()) {\n // can't loop through child list because the list will be modified by .removeChild()\n while (display_element.firstChild) {\n display_element.removeChild(display_element.firstChild);\n }\n }\n // create canvas element and image\n var canvas = document.createElement(\"canvas\");\n canvas.id = \"jspsych-image-keyboard-response-stimulus\";\n canvas.style.margin = \"0\";\n canvas.style.padding = \"0\";\n var ctx = canvas.getContext(\"2d\");\n var img = new Image();\n img.onload = () => {\n // if image wasn't preloaded, then it will need to be drawn whenever it finishes loading\n if (!image_drawn) {\n getHeightWidth(); // only possible to get width/height after image loads\n ctx.drawImage(img, 0, 0, width, height);\n }\n };\n img.src = trial.stimulus;\n // get/set image height and width - this can only be done after image loads because uses image's naturalWidth/naturalHeight properties\n const getHeightWidth = () => {\n if (trial.stimulus_height !== null) {\n height = trial.stimulus_height;\n if (trial.stimulus_width == null && trial.maintain_aspect_ratio) {\n width = img.naturalWidth * (trial.stimulus_height / img.naturalHeight);\n }\n } else {\n height = img.naturalHeight;\n }\n if (trial.stimulus_width !== null) {\n width = trial.stimulus_width;\n if (trial.stimulus_height == null && trial.maintain_aspect_ratio) {\n height = img.naturalHeight * (trial.stimulus_width / img.naturalWidth);\n }\n } else if (!(trial.stimulus_height !== null && trial.maintain_aspect_ratio)) {\n // if stimulus width is null, only use the image's natural width if the width value wasn't set\n // in the if statement above, based on a specified height and maintain_aspect_ratio = true\n width = img.naturalWidth;\n }\n canvas.height = height;\n canvas.width = width;\n };\n getHeightWidth(); // call now, in case image loads immediately (is cached)\n // add canvas and draw image\n display_element.insertBefore(canvas, null);\n if (img.complete && Number.isFinite(width) && Number.isFinite(height)) {\n // if image has loaded and width/height have been set, then draw it now\n // (don't rely on img onload function to draw image when image is in the cache, because that causes a delay in the image presentation)\n ctx.drawImage(img, 0, 0, width, height);\n image_drawn = true;\n }\n // add prompt if there is one\n if (trial.prompt !== null) {\n display_element.insertAdjacentHTML(\"beforeend\", trial.prompt);\n }\n } else {\n // display stimulus as an image element\n var html = '<img src=\"' + trial.stimulus + '\" id=\"jspsych-image-keyboard-response-stimulus\">';\n // add prompt\n if (trial.prompt !== null) {\n html += trial.prompt;\n }\n // update the page content\n display_element.innerHTML = html;\n\n // set image dimensions after image has loaded (so that we have access to naturalHeight/naturalWidth)\n var img = display_element.querySelector(\n \"#jspsych-image-keyboard-response-stimulus\"\n ) as HTMLImageElement;\n if (trial.stimulus_height !== null) {\n height = trial.stimulus_height;\n if (trial.stimulus_width == null && trial.maintain_aspect_ratio) {\n width = img.naturalWidth * (trial.stimulus_height / img.naturalHeight);\n }\n } else {\n height = img.naturalHeight;\n }\n if (trial.stimulus_width !== null) {\n width = trial.stimulus_width;\n if (trial.stimulus_height == null && trial.maintain_aspect_ratio) {\n height = img.naturalHeight * (trial.stimulus_width / img.naturalWidth);\n }\n } else if (!(trial.stimulus_height !== null && trial.maintain_aspect_ratio)) {\n // if stimulus width is null, only use the image's natural width if the width value wasn't set\n // in the if statement above, based on a specified height and maintain_aspect_ratio = true\n width = img.naturalWidth;\n }\n img.style.height = height.toString() + \"px\";\n img.style.width = width.toString() + \"px\";\n }\n\n // store response\n var response = {\n rt: null,\n key: null,\n };\n\n // function to end trial when it is time\n const end_trial = () => {\n // kill any remaining setTimeout handlers\n this.jsPsych.pluginAPI.clearAllTimeouts();\n\n // kill keyboard listeners\n if (typeof keyboardListener !== \"undefined\") {\n this.jsPsych.pluginAPI.cancelKeyboardResponse(keyboardListener);\n }\n\n // gather the data to store for the trial\n var trial_data = {\n rt: response.rt,\n stimulus: trial.stimulus,\n response: response.key,\n };\n\n // clear the display\n display_element.innerHTML = \"\";\n\n // move on to the next trial\n this.jsPsych.finishTrial(trial_data);\n };\n\n // function to handle responses by the subject\n var after_response = (info) => {\n // after a valid response, the stimulus will have the CSS class 'responded'\n // which can be used to provide visual feedback that a response was recorded\n display_element.querySelector(\"#jspsych-image-keyboard-response-stimulus\").className +=\n \" responded\";\n\n // only record the first response\n if (response.key == null) {\n response = info;\n }\n\n if (trial.response_ends_trial) {\n end_trial();\n }\n };\n\n // start the response listener\n if (trial.choices != \"NO_KEYS\") {\n var keyboardListener = this.jsPsych.pluginAPI.getKeyboardResponse({\n callback_function: after_response,\n valid_responses: trial.choices,\n rt_method: \"performance\",\n persist: false,\n allow_held_key: false,\n });\n }\n\n // hide stimulus if stimulus_duration is set\n if (trial.stimulus_duration !== null) {\n this.jsPsych.pluginAPI.setTimeout(() => {\n display_element.querySelector<HTMLElement>(\n \"#jspsych-image-keyboard-response-stimulus\"\n ).style.visibility = \"hidden\";\n }, trial.stimulus_duration);\n }\n\n // end trial if trial_duration is set\n if (trial.trial_duration !== null) {\n this.jsPsych.pluginAPI.setTimeout(() => {\n end_trial();\n }, trial.trial_duration);\n } else if (trial.response_ends_trial === false) {\n console.warn(\n \"The experiment may be deadlocked. Try setting a trial duration or set response_ends_trial to true.\"\n );\n }\n }\n\n simulate(\n trial: TrialType<Info>,\n simulation_mode,\n simulation_options: any,\n load_callback: () => void\n ) {\n if (simulation_mode == \"data-only\") {\n load_callback();\n this.simulate_data_only(trial, simulation_options);\n }\n if (simulation_mode == \"visual\") {\n this.simulate_visual(trial, simulation_options, load_callback);\n }\n }\n\n private simulate_data_only(trial: TrialType<Info>, simulation_options) {\n const data = this.create_simulation_data(trial, simulation_options);\n\n this.jsPsych.finishTrial(data);\n }\n\n private simulate_visual(trial: TrialType<Info>, simulation_options, load_callback: () => void) {\n const data = this.create_simulation_data(trial, simulation_options);\n\n const display_element = this.jsPsych.getDisplayElement();\n\n this.trial(display_element, trial);\n load_callback();\n\n if (data.rt !== null) {\n this.jsPsych.pluginAPI.pressKey(data.response, data.rt);\n }\n }\n\n private create_simulation_data(trial: TrialType<Info>, simulation_options) {\n const default_data = {\n stimulus: trial.stimulus,\n rt: this.jsPsych.randomization.sampleExGaussian(500, 50, 1 / 150, true),\n response: this.jsPsych.pluginAPI.getValidKey(trial.choices),\n };\n\n const data = this.jsPsych.pluginAPI.mergeSimulationData(default_data, simulation_options);\n\n this.jsPsych.pluginAPI.ensureSimulationDataConsistency(trial, data);\n\n return data;\n }\n}\n\nexport default ImageKeyboardResponsePlugin;\n"],"names":["ParameterType"],"mappings":";;;;AAEA,MAAM,IAAI,GAAU;IAClB,IAAI,EAAE,yBAAyB;IAC/B,UAAU,EAAE;;QAEV,QAAQ,EAAE;YACR,IAAI,EAAEA,qBAAa,CAAC,KAAK;YACzB,WAAW,EAAE,UAAU;YACvB,OAAO,EAAE,SAAS;SACnB;;QAED,eAAe,EAAE;YACf,IAAI,EAAEA,qBAAa,CAAC,GAAG;YACvB,WAAW,EAAE,cAAc;YAC3B,OAAO,EAAE,IAAI;SACd;;QAED,cAAc,EAAE;YACd,IAAI,EAAEA,qBAAa,CAAC,GAAG;YACvB,WAAW,EAAE,aAAa;YAC1B,OAAO,EAAE,IAAI;SACd;;QAED,qBAAqB,EAAE;YACrB,IAAI,EAAEA,qBAAa,CAAC,IAAI;YACxB,WAAW,EAAE,uBA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AAmB,CAAC,YAAY,EAAE,kBAAkB,CAAC,CAAC;QAE1F,IAAI,CAAC,OAAO,CAAC,SAAS,CAAC,+BAA+B,CAAC,KAAK,EAAE,IAAI,CAAC,CAAC;QAEpE,OAAO,IAAI,CAAC;KACb;;AAnOM,gCAAI,GAAG,IAAI;;;;"}
|
package/dist/index.d.ts
CHANGED
|
@@ -148,5 +148,9 @@ declare class ImageKeyboardResponsePlugin implements JsPsychPlugin<Info> {
|
|
|
148
148
|
};
|
|
149
149
|
constructor(jsPsych: JsPsych);
|
|
150
150
|
trial(display_element: HTMLElement, trial: TrialType<Info>): void;
|
|
151
|
+
simulate(trial: TrialType<Info>, simulation_mode: any, simulation_options: any, load_callback: () => void): void;
|
|
152
|
+
private simulate_data_only;
|
|
153
|
+
private simulate_visual;
|
|
154
|
+
private create_simulation_data;
|
|
151
155
|
}
|
|
152
156
|
export default ImageKeyboardResponsePlugin;
|
package/dist/index.js
CHANGED
|
@@ -98,7 +98,7 @@ class ImageKeyboardResponsePlugin {
|
|
|
98
98
|
canvas.style.padding = "0";
|
|
99
99
|
var ctx = canvas.getContext("2d");
|
|
100
100
|
var img = new Image();
|
|
101
|
-
img.onload =
|
|
101
|
+
img.onload = () => {
|
|
102
102
|
// if image wasn't preloaded, then it will need to be drawn whenever it finishes loading
|
|
103
103
|
if (!image_drawn) {
|
|
104
104
|
getHeightWidth(); // only possible to get width/height after image loads
|
|
@@ -204,7 +204,7 @@ class ImageKeyboardResponsePlugin {
|
|
|
204
204
|
this.jsPsych.finishTrial(trial_data);
|
|
205
205
|
};
|
|
206
206
|
// function to handle responses by the subject
|
|
207
|
-
var after_response =
|
|
207
|
+
var after_response = (info) => {
|
|
208
208
|
// after a valid response, the stimulus will have the CSS class 'responded'
|
|
209
209
|
// which can be used to provide visual feedback that a response was recorded
|
|
210
210
|
display_element.querySelector("#jspsych-image-keyboard-response-stimulus").className +=
|
|
@@ -229,13 +229,13 @@ class ImageKeyboardResponsePlugin {
|
|
|
229
229
|
}
|
|
230
230
|
// hide stimulus if stimulus_duration is set
|
|
231
231
|
if (trial.stimulus_duration !== null) {
|
|
232
|
-
this.jsPsych.pluginAPI.setTimeout(
|
|
232
|
+
this.jsPsych.pluginAPI.setTimeout(() => {
|
|
233
233
|
display_element.querySelector("#jspsych-image-keyboard-response-stimulus").style.visibility = "hidden";
|
|
234
234
|
}, trial.stimulus_duration);
|
|
235
235
|
}
|
|
236
236
|
// end trial if trial_duration is set
|
|
237
237
|
if (trial.trial_duration !== null) {
|
|
238
|
-
this.jsPsych.pluginAPI.setTimeout(
|
|
238
|
+
this.jsPsych.pluginAPI.setTimeout(() => {
|
|
239
239
|
end_trial();
|
|
240
240
|
}, trial.trial_duration);
|
|
241
241
|
}
|
|
@@ -243,6 +243,38 @@ class ImageKeyboardResponsePlugin {
|
|
|
243
243
|
console.warn("The experiment may be deadlocked. Try setting a trial duration or set response_ends_trial to true.");
|
|
244
244
|
}
|
|
245
245
|
}
|
|
246
|
+
simulate(trial, simulation_mode, simulation_options, load_callback) {
|
|
247
|
+
if (simulation_mode == "data-only") {
|
|
248
|
+
load_callback();
|
|
249
|
+
this.simulate_data_only(trial, simulation_options);
|
|
250
|
+
}
|
|
251
|
+
if (simulation_mode == "visual") {
|
|
252
|
+
this.simulate_visual(trial, simulation_options, load_callback);
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
simulate_data_only(trial, simulation_options) {
|
|
256
|
+
const data = this.create_simulation_data(trial, simulation_options);
|
|
257
|
+
this.jsPsych.finishTrial(data);
|
|
258
|
+
}
|
|
259
|
+
simulate_visual(trial, simulation_options, load_callback) {
|
|
260
|
+
const data = this.create_simulation_data(trial, simulation_options);
|
|
261
|
+
const display_element = this.jsPsych.getDisplayElement();
|
|
262
|
+
this.trial(display_element, trial);
|
|
263
|
+
load_callback();
|
|
264
|
+
if (data.rt !== null) {
|
|
265
|
+
this.jsPsych.pluginAPI.pressKey(data.response, data.rt);
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
create_simulation_data(trial, simulation_options) {
|
|
269
|
+
const default_data = {
|
|
270
|
+
stimulus: trial.stimulus,
|
|
271
|
+
rt: this.jsPsych.randomization.sampleExGaussian(500, 50, 1 / 150, true),
|
|
272
|
+
response: this.jsPsych.pluginAPI.getValidKey(trial.choices),
|
|
273
|
+
};
|
|
274
|
+
const data = this.jsPsych.pluginAPI.mergeSimulationData(default_data, simulation_options);
|
|
275
|
+
this.jsPsych.pluginAPI.ensureSimulationDataConsistency(trial, data);
|
|
276
|
+
return data;
|
|
277
|
+
}
|
|
246
278
|
}
|
|
247
279
|
ImageKeyboardResponsePlugin.info = info;
|
|
248
280
|
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sources":["../src/index.ts"],"sourcesContent":["import { JsPsych, JsPsychPlugin, ParameterType, TrialType } from \"jspsych\";\n\nconst info = <const>{\n name: \"image-keyboard-response\",\n parameters: {\n /** The image to be displayed */\n stimulus: {\n type: ParameterType.IMAGE,\n pretty_name: \"Stimulus\",\n default: undefined,\n },\n /** Set the image height in pixels */\n stimulus_height: {\n type: ParameterType.INT,\n pretty_name: \"Image height\",\n default: null,\n },\n /** Set the image width in pixels */\n stimulus_width: {\n type: ParameterType.INT,\n pretty_name: \"Image width\",\n default: null,\n },\n /** Maintain the aspect ratio after setting width or height */\n maintain_aspect_ratio: {\n type: ParameterType.BOOL,\n pretty_name: \"Maintain aspect ratio\",\n default: true,\n },\n /** Array containing the key(s) the subject is allowed to press to respond to the stimulus. */\n choices: {\n type: ParameterType.KEYS,\n pretty_name: \"Choices\",\n default: \"ALL_KEYS\",\n },\n /** Any content here will be displayed below the stimulus. */\n prompt: {\n type: ParameterType.HTML_STRING,\n pretty_name: \"Prompt\",\n default: null,\n },\n /** How long to show the stimulus. */\n stimulus_duration: {\n type: ParameterType.INT,\n pretty_name: \"Stimulus duration\",\n default: null,\n },\n /** How long to show trial before it ends */\n trial_duration: {\n type: ParameterType.INT,\n pretty_name: \"Trial duration\",\n default: null,\n },\n /** If true, trial will end when subject makes a response. */\n response_ends_trial: {\n type: ParameterType.BOOL,\n pretty_name: \"Response ends trial\",\n default: true,\n },\n /**\n * If true, the image will be drawn onto a canvas element (prevents blank screen between consecutive images in some browsers).\n * If false, the image will be shown via an img element.\n */\n render_on_canvas: {\n type: ParameterType.BOOL,\n pretty_name: \"Render on canvas\",\n default: true,\n },\n },\n};\n\ntype Info = typeof info;\n\n/**\n * **image-keyboard-response**\n *\n * jsPsych plugin for displaying an image stimulus and getting a keyboard response\n *\n * @author Josh de Leeuw\n * @see {@link https://www.jspsych.org/plugins/jspsych-image-keyboard-response/ image-keyboard-response plugin documentation on jspsych.org}\n */\nclass ImageKeyboardResponsePlugin implements JsPsychPlugin<Info> {\n static info = info;\n\n constructor(private jsPsych: JsPsych) {}\n\n trial(display_element: HTMLElement, trial: TrialType<Info>) {\n var height, width;\n if (trial.render_on_canvas) {\n var image_drawn = false;\n // first clear the display element (because the render_on_canvas method appends to display_element instead of overwriting it with .innerHTML)\n if (display_element.hasChildNodes()) {\n // can't loop through child list because the list will be modified by .removeChild()\n while (display_element.firstChild) {\n display_element.removeChild(display_element.firstChild);\n }\n }\n // create canvas element and image\n var canvas = document.createElement(\"canvas\");\n canvas.id = \"jspsych-image-keyboard-response-stimulus\";\n canvas.style.margin = \"0\";\n canvas.style.padding = \"0\";\n var ctx = canvas.getContext(\"2d\");\n var img = new Image();\n img.onload = function () {\n // if image wasn't preloaded, then it will need to be drawn whenever it finishes loading\n if (!image_drawn) {\n getHeightWidth(); // only possible to get width/height after image loads\n ctx.drawImage(img, 0, 0, width, height);\n }\n };\n img.src = trial.stimulus;\n // get/set image height and width - this can only be done after image loads because uses image's naturalWidth/naturalHeight properties\n const getHeightWidth = () => {\n if (trial.stimulus_height !== null) {\n height = trial.stimulus_height;\n if (trial.stimulus_width == null && trial.maintain_aspect_ratio) {\n width = img.naturalWidth * (trial.stimulus_height / img.naturalHeight);\n }\n } else {\n height = img.naturalHeight;\n }\n if (trial.stimulus_width !== null) {\n width = trial.stimulus_width;\n if (trial.stimulus_height == null && trial.maintain_aspect_ratio) {\n height = img.naturalHeight * (trial.stimulus_width / img.naturalWidth);\n }\n } else if (!(trial.stimulus_height !== null && trial.maintain_aspect_ratio)) {\n // if stimulus width is null, only use the image's natural width if the width value wasn't set\n // in the if statement above, based on a specified height and maintain_aspect_ratio = true\n width = img.naturalWidth;\n }\n canvas.height = height;\n canvas.width = width;\n };\n getHeightWidth(); // call now, in case image loads immediately (is cached)\n // add canvas and draw image\n display_element.insertBefore(canvas, null);\n if (img.complete && Number.isFinite(width) && Number.isFinite(height)) {\n // if image has loaded and width/height have been set, then draw it now\n // (don't rely on img onload function to draw image when image is in the cache, because that causes a delay in the image presentation)\n ctx.drawImage(img, 0, 0, width, height);\n image_drawn = true;\n }\n // add prompt if there is one\n if (trial.prompt !== null) {\n display_element.insertAdjacentHTML(\"beforeend\", trial.prompt);\n }\n } else {\n // display stimulus as an image element\n var html = '<img src=\"' + trial.stimulus + '\" id=\"jspsych-image-keyboard-response-stimulus\">';\n // add prompt\n if (trial.prompt !== null) {\n html += trial.prompt;\n }\n // update the page content\n display_element.innerHTML = html;\n\n // set image dimensions after image has loaded (so that we have access to naturalHeight/naturalWidth)\n var img = display_element.querySelector(\n \"#jspsych-image-keyboard-response-stimulus\"\n ) as HTMLImageElement;\n if (trial.stimulus_height !== null) {\n height = trial.stimulus_height;\n if (trial.stimulus_width == null && trial.maintain_aspect_ratio) {\n width = img.naturalWidth * (trial.stimulus_height / img.naturalHeight);\n }\n } else {\n height = img.naturalHeight;\n }\n if (trial.stimulus_width !== null) {\n width = trial.stimulus_width;\n if (trial.stimulus_height == null && trial.maintain_aspect_ratio) {\n height = img.naturalHeight * (trial.stimulus_width / img.naturalWidth);\n }\n } else if (!(trial.stimulus_height !== null && trial.maintain_aspect_ratio)) {\n // if stimulus width is null, only use the image's natural width if the width value wasn't set\n // in the if statement above, based on a specified height and maintain_aspect_ratio = true\n width = img.naturalWidth;\n }\n img.style.height = height.toString() + \"px\";\n img.style.width = width.toString() + \"px\";\n }\n\n // store response\n var response = {\n rt: null,\n key: null,\n };\n\n // function to end trial when it is time\n const end_trial = () => {\n // kill any remaining setTimeout handlers\n this.jsPsych.pluginAPI.clearAllTimeouts();\n\n // kill keyboard listeners\n if (typeof keyboardListener !== \"undefined\") {\n this.jsPsych.pluginAPI.cancelKeyboardResponse(keyboardListener);\n }\n\n // gather the data to store for the trial\n var trial_data = {\n rt: response.rt,\n stimulus: trial.stimulus,\n response: response.key,\n };\n\n // clear the display\n display_element.innerHTML = \"\";\n\n // move on to the next trial\n this.jsPsych.finishTrial(trial_data);\n };\n\n // function to handle responses by the subject\n var after_response = function (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(function () {\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(function () {\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\nexport default ImageKeyboardResponsePlugin;\n"],"names":[],"mappings":";;AAEA,MAAM,I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,CAAC;SAC7B;;QAGD,IAAI,KAAK,CAAC,cAAc,KAAK,IAAI,EAAE;YACjC,IAAI,CAAC,OAAO,CAAC,SAAS,CAAC,UAAU,CAAC;gBAChC,SAAS,EAAE,CAAC;aACb,EAAE,KAAK,CAAC,cAAc,CAAC,CAAC;SAC1B;aAAM,IAAI,KAAK,CAAC,mBAAmB,KAAK,KAAK,EAAE;YAC9C,OAAO,CAAC,IAAI,CACV,oGAAoG,CACrG,CAAC;SACH;KACF;;AAnLM,gCAAI,GAAG,IAAI;;;;"}
|
|
1
|
+
{"version":3,"file":"index.js","sources":["../src/index.ts"],"sourcesContent":["import { JsPsych, JsPsychPlugin, ParameterType, TrialType } from \"jspsych\";\n\nconst info = <const>{\n name: \"image-keyboard-response\",\n parameters: {\n /** The image to be displayed */\n stimulus: {\n type: ParameterType.IMAGE,\n pretty_name: \"Stimulus\",\n default: undefined,\n },\n /** Set the image height in pixels */\n stimulus_height: {\n type: ParameterType.INT,\n pretty_name: \"Image height\",\n default: null,\n },\n /** Set the image width in pixels */\n stimulus_width: {\n type: ParameterType.INT,\n pretty_name: \"Image width\",\n default: null,\n },\n /** Maintain the aspect ratio after setting width or height */\n maintain_aspect_ratio: {\n type: ParameterType.BOOL,\n pretty_name: \"Maintain aspect ratio\",\n default: true,\n },\n /** Array containing the key(s) the subject is allowed to press to respond to the stimulus. */\n choices: {\n type: ParameterType.KEYS,\n pretty_name: \"Choices\",\n default: \"ALL_KEYS\",\n },\n /** Any content here will be displayed below the stimulus. */\n prompt: {\n type: ParameterType.HTML_STRING,\n pretty_name: \"Prompt\",\n default: null,\n },\n /** How long to show the stimulus. */\n stimulus_duration: {\n type: ParameterType.INT,\n pretty_name: \"Stimulus duration\",\n default: null,\n },\n /** How long to show trial before it ends */\n trial_duration: {\n type: ParameterType.INT,\n pretty_name: \"Trial duration\",\n default: null,\n },\n /** If true, trial will end when subject makes a response. */\n response_ends_trial: {\n type: ParameterType.BOOL,\n pretty_name: \"Response ends trial\",\n default: true,\n },\n /**\n * If true, the image will be drawn onto a canvas element (prevents blank screen between consecutive images in some browsers).\n * If false, the image will be shown via an img element.\n */\n render_on_canvas: {\n type: ParameterType.BOOL,\n pretty_name: \"Render on canvas\",\n default: true,\n },\n },\n};\n\ntype Info = typeof info;\n\n/**\n * **image-keyboard-response**\n *\n * jsPsych plugin for displaying an image stimulus and getting a keyboard response\n *\n * @author Josh de Leeuw\n * @see {@link https://www.jspsych.org/plugins/jspsych-image-keyboard-response/ image-keyboard-response plugin documentation on jspsych.org}\n */\nclass ImageKeyboardResponsePlugin implements JsPsychPlugin<Info> {\n static info = info;\n\n constructor(private jsPsych: JsPsych) {}\n\n trial(display_element: HTMLElement, trial: TrialType<Info>) {\n var height, width;\n if (trial.render_on_canvas) {\n var image_drawn = false;\n // first clear the display element (because the render_on_canvas method appends to display_element instead of overwriting it with .innerHTML)\n if (display_element.hasChildNodes()) {\n // can't loop through child list because the list will be modified by .removeChild()\n while (display_element.firstChild) {\n display_element.removeChild(display_element.firstChild);\n }\n }\n // create canvas element and image\n var canvas = document.createElement(\"canvas\");\n canvas.id = \"jspsych-image-keyboard-response-stimulus\";\n canvas.style.margin = \"0\";\n canvas.style.padding = \"0\";\n var ctx = canvas.getContext(\"2d\");\n var img = new Image();\n img.onload = () => {\n // if image wasn't preloaded, then it will need to be drawn whenever it finishes loading\n if (!image_drawn) {\n getHeightWidth(); // only possible to get width/height after image loads\n ctx.drawImage(img, 0, 0, width, height);\n }\n };\n img.src = trial.stimulus;\n // get/set image height and width - this can only be done after image loads because uses image's naturalWidth/naturalHeight properties\n const getHeightWidth = () => {\n if (trial.stimulus_height !== null) {\n height = trial.stimulus_height;\n if (trial.stimulus_width == null && trial.maintain_aspect_ratio) {\n width = img.naturalWidth * (trial.stimulus_height / img.naturalHeight);\n }\n } else {\n height = img.naturalHeight;\n }\n if (trial.stimulus_width !== null) {\n width = trial.stimulus_width;\n if (trial.stimulus_height == null && trial.maintain_aspect_ratio) {\n height = img.naturalHeight * (trial.stimulus_width / img.naturalWidth);\n }\n } else if (!(trial.stimulus_height !== null && trial.maintain_aspect_ratio)) {\n // if stimulus width is null, only use the image's natural width if the width value wasn't set\n // in the if statement above, based on a specified height and maintain_aspect_ratio = true\n width = img.naturalWidth;\n }\n canvas.height = height;\n canvas.width = width;\n };\n getHeightWidth(); // call now, in case image loads immediately (is cached)\n // add canvas and draw image\n display_element.insertBefore(canvas, null);\n if (img.complete && Number.isFinite(width) && Number.isFinite(height)) {\n // if image has loaded and width/height have been set, then draw it now\n // (don't rely on img onload function to draw image when image is in the cache, because that causes a delay in the image presentation)\n ctx.drawImage(img, 0, 0, width, height);\n image_drawn = true;\n }\n // add prompt if there is one\n if (trial.prompt !== null) {\n display_element.insertAdjacentHTML(\"beforeend\", trial.prompt);\n }\n } else {\n // display stimulus as an image element\n var html = '<img src=\"' + trial.stimulus + '\" id=\"jspsych-image-keyboard-response-stimulus\">';\n // add prompt\n if (trial.prompt !== null) {\n html += trial.prompt;\n }\n // update the page content\n display_element.innerHTML = html;\n\n // set image dimensions after image has loaded (so that we have access to naturalHeight/naturalWidth)\n var img = display_element.querySelector(\n \"#jspsych-image-keyboard-response-stimulus\"\n ) as HTMLImageElement;\n if (trial.stimulus_height !== null) {\n height = trial.stimulus_height;\n if (trial.stimulus_width == null && trial.maintain_aspect_ratio) {\n width = img.naturalWidth * (trial.stimulus_height / img.naturalHeight);\n }\n } else {\n height = img.naturalHeight;\n }\n if (trial.stimulus_width !== null) {\n width = trial.stimulus_width;\n if (trial.stimulus_height == null && trial.maintain_aspect_ratio) {\n height = img.naturalHeight * (trial.stimulus_width / img.naturalWidth);\n }\n } else if (!(trial.stimulus_height !== null && trial.maintain_aspect_ratio)) {\n // if stimulus width is null, only use the image's natural width if the width value wasn't set\n // in the if statement above, based on a specified height and maintain_aspect_ratio = true\n width = img.naturalWidth;\n }\n img.style.height = height.toString() + \"px\";\n img.style.width = width.toString() + \"px\";\n }\n\n // store response\n var response = {\n rt: null,\n key: null,\n };\n\n // function to end trial when it is time\n const end_trial = () => {\n // kill any remaining setTimeout handlers\n this.jsPsych.pluginAPI.clearAllTimeouts();\n\n // kill keyboard listeners\n if (typeof keyboardListener !== \"undefined\") {\n this.jsPsych.pluginAPI.cancelKeyboardResponse(keyboardListener);\n }\n\n // gather the data to store for the trial\n var trial_data = {\n rt: response.rt,\n stimulus: trial.stimulus,\n response: response.key,\n };\n\n // clear the display\n display_element.innerHTML = \"\";\n\n // move on to the next trial\n this.jsPsych.finishTrial(trial_data);\n };\n\n // function to handle responses by the subject\n var after_response = (info) => {\n // after a valid response, the stimulus will have the CSS class 'responded'\n // which can be used to provide visual feedback that a response was recorded\n display_element.querySelector(\"#jspsych-image-keyboard-response-stimulus\").className +=\n \" responded\";\n\n // only record the first response\n if (response.key == null) {\n response = info;\n }\n\n if (trial.response_ends_trial) {\n end_trial();\n }\n };\n\n // start the response listener\n if (trial.choices != \"NO_KEYS\") {\n var keyboardListener = this.jsPsych.pluginAPI.getKeyboardResponse({\n callback_function: after_response,\n valid_responses: trial.choices,\n rt_method: \"performance\",\n persist: false,\n allow_held_key: false,\n });\n }\n\n // hide stimulus if stimulus_duration is set\n if (trial.stimulus_duration !== null) {\n this.jsPsych.pluginAPI.setTimeout(() => {\n display_element.querySelector<HTMLElement>(\n \"#jspsych-image-keyboard-response-stimulus\"\n ).style.visibility = \"hidden\";\n }, trial.stimulus_duration);\n }\n\n // end trial if trial_duration is set\n if (trial.trial_duration !== null) {\n this.jsPsych.pluginAPI.setTimeout(() => {\n end_trial();\n }, trial.trial_duration);\n } else if (trial.response_ends_trial === false) {\n console.warn(\n \"The experiment may be deadlocked. Try setting a trial duration or set response_ends_trial to true.\"\n );\n }\n }\n\n simulate(\n trial: TrialType<Info>,\n simulation_mode,\n simulation_options: any,\n load_callback: () => void\n ) {\n if (simulation_mode == \"data-only\") {\n load_callback();\n this.simulate_data_only(trial, simulation_options);\n }\n if (simulation_mode == \"visual\") {\n this.simulate_visual(trial, simulation_options, load_callback);\n }\n }\n\n private simulate_data_only(trial: TrialType<Info>, simulation_options) {\n const data = this.create_simulation_data(trial, simulation_options);\n\n this.jsPsych.finishTrial(data);\n }\n\n private simulate_visual(trial: TrialType<Info>, simulation_options, load_callback: () => void) {\n const data = this.create_simulation_data(trial, simulation_options);\n\n const display_element = this.jsPsych.getDisplayElement();\n\n this.trial(display_element, trial);\n load_callback();\n\n if (data.rt !== null) {\n this.jsPsych.pluginAPI.pressKey(data.response, data.rt);\n }\n }\n\n private create_simulation_data(trial: TrialType<Info>, simulation_options) {\n const default_data = {\n stimulus: trial.stimulus,\n rt: this.jsPsych.randomization.sampleExGaussian(500, 50, 1 / 150, true),\n response: this.jsPsych.pluginAPI.getValidKey(trial.choices),\n };\n\n const data = this.jsPsych.pluginAPI.mergeSimulationData(default_data, simulation_options);\n\n this.jsPsych.pluginAPI.ensureSimulationDataConsistency(trial, data);\n\n return data;\n }\n}\n\nexport default ImageKeyboardResponsePlugin;\n"],"names":[],"mappings":";;AAEA,MAAM,IAAI,GAAU;IAClB,IAAI,EAAE,yBAAyB;IAC/B,UAAU,EAAE;;QAEV,QAAQ,EAAE;YACR,IAAI,EAAE,aAAa,CAAC,KAAK;YACzB,WAAW,EAAE,UAAU;YACvB,OAAO,EAAE,SAAS;SACnB;;QAED,eAAe,EAAE;YACf,IAAI,EAAE,aAAa,CAAC,GAAG;YACvB,WAAW,EAAE,cAAc;YAC3B,OAAO,EAAE,IAAI;SACd;;QAED,cAAc,EAAE;YACd,IAAI,EAAE,aAAa,CAAC,GAAG;YACvB,WAAW,EAAE,aAAa;YAC1B,OAAO,EAAE,IAAI;SACd;;QAED,qBAAqB,EAAE;YACrB,IAAI,EAAE,aAAa,CAAC,IAAI;YACxB,WAAW,EAAE,uBAAuB;YACpC,OAAO,EAAE,IAAI;SACd;;QAED,OAAO,EAAE;YACP,IAAI,EAAE,aAAa,CAAC,IAAI;YACxB,WAAW,EAAE,SAAS;YACtB,OAAO,EAAE,UAAU;SACpB;;QAED,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;;AAnOM,gCAAI,GAAG,IAAI;;;;"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@jspsych/plugin-image-keyboard-response",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.1.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",
|
|
@@ -34,10 +34,10 @@
|
|
|
34
34
|
},
|
|
35
35
|
"homepage": "https://www.jspsych.org/latest/plugins/image-keyboard-response",
|
|
36
36
|
"peerDependencies": {
|
|
37
|
-
"jspsych": ">=7.
|
|
37
|
+
"jspsych": ">=7.1.0"
|
|
38
38
|
},
|
|
39
39
|
"devDependencies": {
|
|
40
|
-
"@jspsych/config": "^1.
|
|
41
|
-
"@jspsych/test-utils": "^1.
|
|
40
|
+
"@jspsych/config": "^1.1.0",
|
|
41
|
+
"@jspsych/test-utils": "^1.1.0"
|
|
42
42
|
}
|
|
43
43
|
}
|
package/src/index.ts
CHANGED
|
@@ -102,7 +102,7 @@ class ImageKeyboardResponsePlugin implements JsPsychPlugin<Info> {
|
|
|
102
102
|
canvas.style.padding = "0";
|
|
103
103
|
var ctx = canvas.getContext("2d");
|
|
104
104
|
var img = new Image();
|
|
105
|
-
img.onload =
|
|
105
|
+
img.onload = () => {
|
|
106
106
|
// if image wasn't preloaded, then it will need to be drawn whenever it finishes loading
|
|
107
107
|
if (!image_drawn) {
|
|
108
108
|
getHeightWidth(); // only possible to get width/height after image loads
|
|
@@ -213,7 +213,7 @@ class ImageKeyboardResponsePlugin implements JsPsychPlugin<Info> {
|
|
|
213
213
|
};
|
|
214
214
|
|
|
215
215
|
// function to handle responses by the subject
|
|
216
|
-
var after_response =
|
|
216
|
+
var after_response = (info) => {
|
|
217
217
|
// after a valid response, the stimulus will have the CSS class 'responded'
|
|
218
218
|
// which can be used to provide visual feedback that a response was recorded
|
|
219
219
|
display_element.querySelector("#jspsych-image-keyboard-response-stimulus").className +=
|
|
@@ -242,7 +242,7 @@ class ImageKeyboardResponsePlugin implements JsPsychPlugin<Info> {
|
|
|
242
242
|
|
|
243
243
|
// hide stimulus if stimulus_duration is set
|
|
244
244
|
if (trial.stimulus_duration !== null) {
|
|
245
|
-
this.jsPsych.pluginAPI.setTimeout(
|
|
245
|
+
this.jsPsych.pluginAPI.setTimeout(() => {
|
|
246
246
|
display_element.querySelector<HTMLElement>(
|
|
247
247
|
"#jspsych-image-keyboard-response-stimulus"
|
|
248
248
|
).style.visibility = "hidden";
|
|
@@ -251,7 +251,7 @@ class ImageKeyboardResponsePlugin implements JsPsychPlugin<Info> {
|
|
|
251
251
|
|
|
252
252
|
// end trial if trial_duration is set
|
|
253
253
|
if (trial.trial_duration !== null) {
|
|
254
|
-
this.jsPsych.pluginAPI.setTimeout(
|
|
254
|
+
this.jsPsych.pluginAPI.setTimeout(() => {
|
|
255
255
|
end_trial();
|
|
256
256
|
}, trial.trial_duration);
|
|
257
257
|
} else if (trial.response_ends_trial === false) {
|
|
@@ -260,6 +260,54 @@ class ImageKeyboardResponsePlugin implements JsPsychPlugin<Info> {
|
|
|
260
260
|
);
|
|
261
261
|
}
|
|
262
262
|
}
|
|
263
|
+
|
|
264
|
+
simulate(
|
|
265
|
+
trial: TrialType<Info>,
|
|
266
|
+
simulation_mode,
|
|
267
|
+
simulation_options: any,
|
|
268
|
+
load_callback: () => void
|
|
269
|
+
) {
|
|
270
|
+
if (simulation_mode == "data-only") {
|
|
271
|
+
load_callback();
|
|
272
|
+
this.simulate_data_only(trial, simulation_options);
|
|
273
|
+
}
|
|
274
|
+
if (simulation_mode == "visual") {
|
|
275
|
+
this.simulate_visual(trial, simulation_options, load_callback);
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
private simulate_data_only(trial: TrialType<Info>, simulation_options) {
|
|
280
|
+
const data = this.create_simulation_data(trial, simulation_options);
|
|
281
|
+
|
|
282
|
+
this.jsPsych.finishTrial(data);
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
private simulate_visual(trial: TrialType<Info>, simulation_options, load_callback: () => void) {
|
|
286
|
+
const data = this.create_simulation_data(trial, simulation_options);
|
|
287
|
+
|
|
288
|
+
const display_element = this.jsPsych.getDisplayElement();
|
|
289
|
+
|
|
290
|
+
this.trial(display_element, trial);
|
|
291
|
+
load_callback();
|
|
292
|
+
|
|
293
|
+
if (data.rt !== null) {
|
|
294
|
+
this.jsPsych.pluginAPI.pressKey(data.response, data.rt);
|
|
295
|
+
}
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
private create_simulation_data(trial: TrialType<Info>, simulation_options) {
|
|
299
|
+
const default_data = {
|
|
300
|
+
stimulus: trial.stimulus,
|
|
301
|
+
rt: this.jsPsych.randomization.sampleExGaussian(500, 50, 1 / 150, true),
|
|
302
|
+
response: this.jsPsych.pluginAPI.getValidKey(trial.choices),
|
|
303
|
+
};
|
|
304
|
+
|
|
305
|
+
const data = this.jsPsych.pluginAPI.mergeSimulationData(default_data, simulation_options);
|
|
306
|
+
|
|
307
|
+
this.jsPsych.pluginAPI.ensureSimulationDataConsistency(trial, data);
|
|
308
|
+
|
|
309
|
+
return data;
|
|
310
|
+
}
|
|
263
311
|
}
|
|
264
312
|
|
|
265
313
|
export default ImageKeyboardResponsePlugin;
|