@jspsych/plugin-canvas-keyboard-response 1.1.3 → 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.cjs CHANGED
@@ -2,173 +2,182 @@
2
2
 
3
3
  var jspsych = require('jspsych');
4
4
 
5
+ var _package = {
6
+ name: "@jspsych/plugin-canvas-keyboard-response",
7
+ version: "2.0.0",
8
+ description: "jsPsych plugin for displaying a canvas stimulus and getting a keyboard response",
9
+ type: "module",
10
+ main: "dist/index.cjs",
11
+ exports: {
12
+ import: "./dist/index.js",
13
+ require: "./dist/index.cjs"
14
+ },
15
+ typings: "dist/index.d.ts",
16
+ unpkg: "dist/index.browser.min.js",
17
+ files: [
18
+ "src",
19
+ "dist"
20
+ ],
21
+ source: "src/index.ts",
22
+ scripts: {
23
+ test: "jest --passWithNoTests",
24
+ "test:watch": "npm test -- --watch",
25
+ tsc: "tsc",
26
+ build: "rollup --config",
27
+ "build:watch": "npm run build -- --watch"
28
+ },
29
+ repository: {
30
+ type: "git",
31
+ url: "git+https://github.com/jspsych/jsPsych.git",
32
+ directory: "packages/plugin-canvas-keyboard-response"
33
+ },
34
+ author: "Chris Jungerius, Josh de Leeuw",
35
+ license: "MIT",
36
+ bugs: {
37
+ url: "https://github.com/jspsych/jsPsych/issues"
38
+ },
39
+ homepage: "https://www.jspsych.org/latest/plugins/canvas-keyboard-response",
40
+ peerDependencies: {
41
+ jspsych: ">=7.1.0"
42
+ },
43
+ devDependencies: {
44
+ "@jspsych/config": "^3.0.0",
45
+ "@jspsych/test-utils": "^1.2.0"
46
+ }
47
+ };
48
+
5
49
  const info = {
6
- name: "canvas-keyboard-response",
7
- parameters: {
8
- /** The drawing function to apply to the canvas. Should take the canvas object as argument. */
9
- stimulus: {
10
- type: jspsych.ParameterType.FUNCTION,
11
- pretty_name: "Stimulus",
12
- default: undefined,
13
- },
14
- /** Array containing the key(s) the subject is allowed to press to respond to the stimulus. */
15
- choices: {
16
- type: jspsych.ParameterType.KEYS,
17
- pretty_name: "Choices",
18
- default: "ALL_KEYS",
19
- },
20
- /** Any content here will be displayed below the stimulus. */
21
- prompt: {
22
- type: jspsych.ParameterType.HTML_STRING,
23
- pretty_name: "Prompt",
24
- default: null,
25
- },
26
- /** How long to show the stimulus. */
27
- stimulus_duration: {
28
- type: jspsych.ParameterType.INT,
29
- pretty_name: "Stimulus duration",
30
- default: null,
31
- },
32
- /** How long to show trial before it ends. */
33
- trial_duration: {
34
- type: jspsych.ParameterType.INT,
35
- pretty_name: "Trial duration",
36
- default: null,
37
- },
38
- /** If true, trial will end when subject makes a response. */
39
- response_ends_trial: {
40
- type: jspsych.ParameterType.BOOL,
41
- pretty_name: "Response ends trial",
42
- default: true,
43
- },
44
- /** Array containing the height (first value) and width (second value) of the canvas element. */
45
- canvas_size: {
46
- type: jspsych.ParameterType.INT,
47
- array: true,
48
- pretty_name: "Canvas size",
49
- default: [500, 500],
50
- },
50
+ name: "canvas-keyboard-response",
51
+ version: _package.version,
52
+ parameters: {
53
+ stimulus: {
54
+ type: jspsych.ParameterType.FUNCTION,
55
+ default: void 0
56
+ },
57
+ choices: {
58
+ type: jspsych.ParameterType.KEYS,
59
+ default: "ALL_KEYS"
60
+ },
61
+ prompt: {
62
+ type: jspsych.ParameterType.HTML_STRING,
63
+ default: null
64
+ },
65
+ stimulus_duration: {
66
+ type: jspsych.ParameterType.INT,
67
+ default: null
68
+ },
69
+ trial_duration: {
70
+ type: jspsych.ParameterType.INT,
71
+ default: null
51
72
  },
73
+ response_ends_trial: {
74
+ type: jspsych.ParameterType.BOOL,
75
+ default: true
76
+ },
77
+ canvas_size: {
78
+ type: jspsych.ParameterType.INT,
79
+ array: true,
80
+ default: [500, 500]
81
+ }
82
+ },
83
+ data: {
84
+ response: {
85
+ type: jspsych.ParameterType.STRING
86
+ },
87
+ rt: {
88
+ type: jspsych.ParameterType.INT
89
+ }
90
+ }
52
91
  };
53
- /**
54
- * **canvas-keyboard-response**
55
- *
56
- * jsPsych plugin for displaying a canvas stimulus and getting a keyboard response
57
- *
58
- * @author Chris Jungerius (modified from Josh de Leeuw)
59
- * @see {@link https://www.jspsych.org/plugins/jspsych-canvas-keyboard-response/ canvas-keyboard-response plugin documentation on jspsych.org}
60
- */
61
92
  class CanvasKeyboardResponsePlugin {
62
- constructor(jsPsych) {
63
- this.jsPsych = jsPsych;
93
+ constructor(jsPsych) {
94
+ this.jsPsych = jsPsych;
95
+ }
96
+ trial(display_element, trial) {
97
+ var new_html = '<div id="jspsych-canvas-keyboard-response-stimulus"><canvas id="jspsych-canvas-stimulus" height="' + trial.canvas_size[0] + '" width="' + trial.canvas_size[1] + '"></canvas></div>';
98
+ if (trial.prompt !== null) {
99
+ new_html += trial.prompt;
100
+ }
101
+ display_element.innerHTML = new_html;
102
+ let c = document.getElementById("jspsych-canvas-stimulus");
103
+ c.style.display = "block";
104
+ trial.stimulus(c);
105
+ var response = {
106
+ rt: null,
107
+ key: null
108
+ };
109
+ const end_trial = () => {
110
+ if (typeof keyboardListener !== "undefined") {
111
+ this.jsPsych.pluginAPI.cancelKeyboardResponse(keyboardListener);
112
+ }
113
+ var trial_data = {
114
+ rt: response.rt,
115
+ response: response.key
116
+ };
117
+ this.jsPsych.finishTrial(trial_data);
118
+ };
119
+ var after_response = (info2) => {
120
+ display_element.querySelector("#jspsych-canvas-keyboard-response-stimulus").className += " responded";
121
+ if (response.key == null) {
122
+ response = info2;
123
+ }
124
+ if (trial.response_ends_trial) {
125
+ end_trial();
126
+ }
127
+ };
128
+ if (trial.choices != "NO_KEYS") {
129
+ var keyboardListener = this.jsPsych.pluginAPI.getKeyboardResponse({
130
+ callback_function: after_response,
131
+ valid_responses: trial.choices,
132
+ rt_method: "performance",
133
+ persist: false,
134
+ allow_held_key: false
135
+ });
64
136
  }
65
- trial(display_element, trial) {
66
- var new_html = '<div id="jspsych-canvas-keyboard-response-stimulus">' +
67
- '<canvas id="jspsych-canvas-stimulus" height="' +
68
- trial.canvas_size[0] +
69
- '" width="' +
70
- trial.canvas_size[1] +
71
- '"></canvas>' +
72
- "</div>";
73
- // add prompt
74
- if (trial.prompt !== null) {
75
- new_html += trial.prompt;
76
- }
77
- // draw
78
- display_element.innerHTML = new_html;
79
- let c = document.getElementById("jspsych-canvas-stimulus");
80
- trial.stimulus(c);
81
- // store response
82
- var response = {
83
- rt: null,
84
- key: null,
85
- };
86
- // function to end trial when it is time
87
- const end_trial = () => {
88
- // kill any remaining setTimeout handlers
89
- this.jsPsych.pluginAPI.clearAllTimeouts();
90
- // kill keyboard listeners
91
- if (typeof keyboardListener !== "undefined") {
92
- this.jsPsych.pluginAPI.cancelKeyboardResponse(keyboardListener);
93
- }
94
- // gather the data to store for the trial
95
- var trial_data = {
96
- rt: response.rt,
97
- response: response.key,
98
- };
99
- // clear the display
100
- display_element.innerHTML = "";
101
- // move on to the next trial
102
- this.jsPsych.finishTrial(trial_data);
103
- };
104
- // function to handle responses by the subject
105
- var after_response = (info) => {
106
- // after a valid response, the stimulus will have the CSS class 'responded'
107
- // which can be used to provide visual feedback that a response was recorded
108
- display_element.querySelector("#jspsych-canvas-keyboard-response-stimulus").className +=
109
- " responded";
110
- // only record the first response
111
- if (response.key == null) {
112
- response = info;
113
- }
114
- if (trial.response_ends_trial) {
115
- end_trial();
116
- }
117
- };
118
- // start the response listener
119
- if (trial.choices != "NO_KEYS") {
120
- var keyboardListener = this.jsPsych.pluginAPI.getKeyboardResponse({
121
- callback_function: after_response,
122
- valid_responses: trial.choices,
123
- rt_method: "performance",
124
- persist: false,
125
- allow_held_key: false,
126
- });
127
- }
128
- // hide stimulus if stimulus_duration is set
129
- if (trial.stimulus_duration !== null) {
130
- this.jsPsych.pluginAPI.setTimeout(() => {
131
- display_element.querySelector("#jspsych-canvas-keyboard-response-stimulus").style.visibility = "hidden";
132
- }, trial.stimulus_duration);
133
- }
134
- // end trial if trial_duration is set
135
- if (trial.trial_duration !== null) {
136
- this.jsPsych.pluginAPI.setTimeout(() => {
137
- end_trial();
138
- }, trial.trial_duration);
139
- }
137
+ if (trial.stimulus_duration !== null) {
138
+ this.jsPsych.pluginAPI.setTimeout(() => {
139
+ display_element.querySelector(
140
+ "#jspsych-canvas-keyboard-response-stimulus"
141
+ ).style.visibility = "hidden";
142
+ }, trial.stimulus_duration);
140
143
  }
141
- simulate(trial, simulation_mode, simulation_options, load_callback) {
142
- if (simulation_mode == "data-only") {
143
- load_callback();
144
- this.simulate_data_only(trial, simulation_options);
145
- }
146
- if (simulation_mode == "visual") {
147
- this.simulate_visual(trial, simulation_options, load_callback);
148
- }
144
+ if (trial.trial_duration !== null) {
145
+ this.jsPsych.pluginAPI.setTimeout(() => {
146
+ end_trial();
147
+ }, trial.trial_duration);
149
148
  }
150
- simulate_data_only(trial, simulation_options) {
151
- const data = this.create_simulation_data(trial, simulation_options);
152
- this.jsPsych.finishTrial(data);
149
+ }
150
+ simulate(trial, simulation_mode, simulation_options, load_callback) {
151
+ if (simulation_mode == "data-only") {
152
+ load_callback();
153
+ this.simulate_data_only(trial, simulation_options);
153
154
  }
154
- simulate_visual(trial, simulation_options, load_callback) {
155
- const data = this.create_simulation_data(trial, simulation_options);
156
- const display_element = this.jsPsych.getDisplayElement();
157
- this.trial(display_element, trial);
158
- load_callback();
159
- if (data.rt !== null) {
160
- this.jsPsych.pluginAPI.pressKey(data.response, data.rt);
161
- }
155
+ if (simulation_mode == "visual") {
156
+ this.simulate_visual(trial, simulation_options, load_callback);
162
157
  }
163
- create_simulation_data(trial, simulation_options) {
164
- const default_data = {
165
- rt: this.jsPsych.randomization.sampleExGaussian(500, 50, 1 / 150, true),
166
- response: this.jsPsych.pluginAPI.getValidKey(trial.choices),
167
- };
168
- const data = this.jsPsych.pluginAPI.mergeSimulationData(default_data, simulation_options);
169
- this.jsPsych.pluginAPI.ensureSimulationDataConsistency(trial, data);
170
- return data;
158
+ }
159
+ simulate_data_only(trial, simulation_options) {
160
+ const data = this.create_simulation_data(trial, simulation_options);
161
+ this.jsPsych.finishTrial(data);
162
+ }
163
+ simulate_visual(trial, simulation_options, load_callback) {
164
+ const data = this.create_simulation_data(trial, simulation_options);
165
+ const display_element = this.jsPsych.getDisplayElement();
166
+ this.trial(display_element, trial);
167
+ load_callback();
168
+ if (data.rt !== null) {
169
+ this.jsPsych.pluginAPI.pressKey(data.response, data.rt);
171
170
  }
171
+ }
172
+ create_simulation_data(trial, simulation_options) {
173
+ const default_data = {
174
+ rt: this.jsPsych.randomization.sampleExGaussian(500, 50, 1 / 150, true),
175
+ response: this.jsPsych.pluginAPI.getValidKey(trial.choices)
176
+ };
177
+ const data = this.jsPsych.pluginAPI.mergeSimulationData(default_data, simulation_options);
178
+ this.jsPsych.pluginAPI.ensureSimulationDataConsistency(trial, data);
179
+ return data;
180
+ }
172
181
  }
173
182
  CanvasKeyboardResponsePlugin.info = info;
174
183
 
@@ -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: \"canvas-keyboard-response\",\n parameters: {\n /** The drawing function to apply to the canvas. Should take the canvas object as argument. */\n stimulus: {\n type: ParameterType.FUNCTION,\n pretty_name: \"Stimulus\",\n default: undefined,\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 /** Array containing the height (first value) and width (second value) of the canvas element. */\n canvas_size: {\n type: ParameterType.INT,\n array: true,\n pretty_name: \"Canvas size\",\n default: [500, 500],\n },\n },\n};\n\ntype Info = typeof info;\n\n/**\n * **canvas-keyboard-response**\n *\n * jsPsych plugin for displaying a canvas stimulus and getting a keyboard response\n *\n * @author Chris Jungerius (modified from Josh de Leeuw)\n * @see {@link https://www.jspsych.org/plugins/jspsych-canvas-keyboard-response/ canvas-keyboard-response plugin documentation on jspsych.org}\n */\nclass CanvasKeyboardResponsePlugin implements JsPsychPlugin<Info> {\n static info = info;\n\n constructor(private jsPsych: JsPsych) {}\n\n trial(display_element: HTMLElement, trial: TrialType<Info>) {\n var new_html =\n '<div id=\"jspsych-canvas-keyboard-response-stimulus\">' +\n '<canvas id=\"jspsych-canvas-stimulus\" height=\"' +\n trial.canvas_size[0] +\n '\" width=\"' +\n trial.canvas_size[1] +\n '\"></canvas>' +\n \"</div>\";\n // add prompt\n if (trial.prompt !== null) {\n new_html += trial.prompt;\n }\n\n // draw\n display_element.innerHTML = new_html;\n let c = document.getElementById(\"jspsych-canvas-stimulus\");\n trial.stimulus(c);\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 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-canvas-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-canvas-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 }\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 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 CanvasKeyboardResponsePlugin;\n"],"names":["ParameterType"],"mappings":";;;;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AAA,MAAM,IAAI,GAAG,IAAI,CAAC,OAAO,CAAC,SAAS,CAAC,mBAAmB,CAAC,YAAY,EAAE,kBAAkB,CAAC,CAAC;QAE1F,IAAI,CAAC,OAAO,CAAC,SAAS,CAAC,+BAA+B,CAAC,KAAK,EAAE,IAAI,CAAC,CAAC;AAEpE,QAAA,OAAO,IAAI,CAAC;KACb;;AA7IM,4BAAI,CAAA,IAAA,GAAG,IAAI;;;;"}
1
+ {"version":3,"file":"index.cjs","sources":["../src/index.ts"],"sourcesContent":["import { JsPsych, JsPsychPlugin, ParameterType, TrialType } from \"jspsych\";\n\nimport { version } from \"../package.json\";\n\nconst info = <const>{\n name: \"canvas-keyboard-response\",\n version: version,\n parameters: {\n /** The function to draw on the canvas. This function automatically takes a canvas element as its only\n * argument, e.g. `function(c) {...}` or `function drawStim(c) {...}`, where `c` refers to the canvas element.\n * Note that the stimulus function will still generally need to set the correct context itself, using a line\n * like `let ctx = c.getContext(\"2d\")`.\n */\n stimulus: {\n type: ParameterType.FUNCTION,\n default: undefined,\n },\n /** This array contains the key(s) that the participant is allowed to press in order to respond to the stimulus.\n * Keys should be specified as characters (e.g., `'a'`, `'q'`, `' '`, `'Enter'`, `'ArrowDown'`) -\n * see [this page](https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/key/Key_Values)\n * and [this page (event.key column)](https://www.freecodecamp.org/news/javascript-keycode-list-keypress-event-key-codes/)\n * for more examples. Any key presses that are not listed in the array will be ignored. The default value\n * of `\"ALL_KEYS\"` means that all keys will be accepted as valid responses. Specifying `\"NO_KEYS\"` will mean\n * that no responses are allowed.\n */\n choices: {\n type: ParameterType.KEYS,\n default: \"ALL_KEYS\",\n },\n /** This string can contain HTML markup. Any content here will be displayed below the stimulus. The intention\n * is that it can be used to provide a reminder about the action the participant is supposed to take (e.g., which key to press).\n */\n prompt: {\n type: ParameterType.HTML_STRING,\n default: null,\n },\n /** How long to display the stimulus in milliseconds. The visibility CSS property of the stimulus will be set to\n * `hidden` after this time has elapsed. If this is null, then the stimulus will remain visible until the trial ends.\n */\n stimulus_duration: {\n type: ParameterType.INT,\n default: null,\n },\n /** How long to wait for the participant to make a response before ending the trial in milliseconds. If the\n * participant fails to make a response before this timer is reached, the participant's response will be\n * recorded as null for the trial and the trial will end. If the value of this parameter is null, then the\n * trial will wait for a response indefinitely.\n */\n trial_duration: {\n type: ParameterType.INT,\n default: null,\n },\n /** If true, then the trial will end whenever the participant makes a response (assuming they make their\n * response before the cutoff specified by the `trial_duration` parameter). If false, then the trial will\n * continue until the value for `trial_duration` is reached. You can use this parameter to force the participant\n * to view a stimulus for a fixed amount of time, even if they respond before the time is complete.\n */\n response_ends_trial: {\n type: ParameterType.BOOL,\n default: true,\n },\n /** Array that defines the size of the canvas element in pixels. First value is height, second value is width. */\n canvas_size: {\n type: ParameterType.INT,\n array: true,\n default: [500, 500],\n },\n },\n data: {\n /** Indicates which key the participant pressed. */\n response: {\n type: ParameterType.STRING,\n },\n /** The response time in milliseconds for the participant to make a response. The time is measured from\n * when the stimulus first appears on the screen until the participant's response.\n */\n rt: {\n type: ParameterType.INT,\n },\n },\n};\n\ntype Info = typeof info;\n\n/**\n * This plugin can be used to draw a stimulus on a [HTML canvas element](https://www.w3schools.com/html/html5_canvas.asp) and\n * record a keyboard response. The canvas stimulus can be useful for displaying dynamic, parametrically-defined graphics,\n * and for controlling the positioning of multiple graphical elements (shapes, text, images). The stimulus can be\n * displayed until a response is given, or for a pre-determined amount of time. The trial can be ended automatically\n * if the participant has failed to respond within a fixed length of time.\n *\n * @author Chris Jungerius (modified from Josh de Leeuw)\n * @see {@link https://www.jspsych.org/latest/plugins/canvas-keyboard-response/ canvas-keyboard-response plugin documentation on jspsych.org}\n */\nclass CanvasKeyboardResponsePlugin implements JsPsychPlugin<Info> {\n static info = info;\n\n constructor(private jsPsych: JsPsych) {}\n\n trial(display_element: HTMLElement, trial: TrialType<Info>) {\n var new_html =\n '<div id=\"jspsych-canvas-keyboard-response-stimulus\">' +\n '<canvas id=\"jspsych-canvas-stimulus\" height=\"' +\n trial.canvas_size[0] +\n '\" width=\"' +\n trial.canvas_size[1] +\n '\"></canvas>' +\n \"</div>\";\n // add prompt\n if (trial.prompt !== null) {\n new_html += trial.prompt;\n }\n\n // draw\n display_element.innerHTML = new_html;\n let c = document.getElementById(\"jspsych-canvas-stimulus\");\n c.style.display = \"block\";\n trial.stimulus(c);\n // store response\n var response = {\n rt: null,\n key: null,\n };\n\n // function to end trial when it is time\n const end_trial = () => {\n // kill keyboard listeners\n if (typeof keyboardListener !== \"undefined\") {\n this.jsPsych.pluginAPI.cancelKeyboardResponse(keyboardListener);\n }\n\n // gather the data to store for the trial\n var trial_data = {\n rt: response.rt,\n response: response.key,\n };\n\n // move on to the next trial\n this.jsPsych.finishTrial(trial_data);\n };\n\n // function to handle responses by the subject\n var after_response = (info) => {\n // after a valid response, the stimulus will have the CSS class 'responded'\n // which can be used to provide visual feedback that a response was recorded\n display_element.querySelector(\"#jspsych-canvas-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-canvas-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 }\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 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 CanvasKeyboardResponsePlugin;\n"],"names":["version","ParameterType","info"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAIA,MAAM,IAAc,GAAA;AAAA,EAClB,IAAM,EAAA,0BAAA;AAAA,WACNA,gBAAA;AAAA,EACA,UAAY,EAAA;AAAA,IAMV,QAAU,EAAA;AAAA,MACR,MAAMC,qBAAc,CAAA,QAAA;AAAA,MACpB,OAAS,EAAA,KAAA,CAAA;AAAA,KACX;AAAA,IASA,OAAS,EAAA;AAAA,MACP,MAAMA,qBAAc,CAAA,IAAA;AAAA,MACpB,OAAS,EAAA,UAAA;AAAA,KACX;AAAA,IAIA,MAAQ,EAAA;AAAA,MACN,MAAMA,qBAAc,CAAA,WAAA;AAAA,MACpB,OAAS,EAAA,IAAA;AAAA,KACX;AAAA,IAIA,iBAAmB,EAAA;AAAA,MACjB,MAAMA,qBAAc,CAAA,GAAA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CAAA,KAAA,CAAM,iBAAiB,KAAK,CAAA,CAAA;AACjC,IAAc,aAAA,EAAA,CAAA;AAEd,IAAI,IAAA,IAAA,CAAK,OAAO,IAAM,EAAA;AACpB,MAAA,IAAA,CAAK,QAAQ,SAAU,CAAA,QAAA,CAAS,IAAK,CAAA,QAAA,EAAU,KAAK,EAAE,CAAA,CAAA;AAAA,KACxD;AAAA,GACF;AAAA,EAEQ,sBAAA,CAAuB,OAAwB,kBAAoB,EAAA;AACzE,IAAA,MAAM,YAAe,GAAA;AAAA,MACnB,EAAA,EAAI,KAAK,OAAQ,CAAA,aAAA,CAAc,iBAAiB,GAAK,EAAA,EAAA,EAAI,CAAI,GAAA,GAAA,EAAK,IAAI,CAAA;AAAA,MACtE,UAAU,IAAK,CAAA,OAAA,CAAQ,SAAU,CAAA,WAAA,CAAY,MAAM,OAAO,CAAA;AAAA,KAC5D,CAAA;AAEA,IAAA,MAAM,OAAO,IAAK,CAAA,OAAA,CAAQ,SAAU,CAAA,mBAAA,CAAoB,cAAc,kBAAkB,CAAA,CAAA;AAExF,IAAA,IAAA,CAAK,OAAQ,CAAA,SAAA,CAAU,+BAAgC,CAAA,KAAA,EAAO,IAAI,CAAA,CAAA;AAElE,IAAO,OAAA,IAAA,CAAA;AAAA,GACT;AACF,CAAA;AA1IM,4BAAA,CACG,IAAO,GAAA,IAAA;;;;"}
package/dist/index.d.ts CHANGED
@@ -1,110 +1,171 @@
1
- import { JsPsych, JsPsychPlugin, ParameterType, TrialType } from "jspsych";
1
+ import { JsPsychPlugin, ParameterType, JsPsych, TrialType } from 'jspsych';
2
+
2
3
  declare const info: {
3
4
  readonly name: "canvas-keyboard-response";
5
+ readonly version: string;
4
6
  readonly parameters: {
5
- /** The drawing function to apply to the canvas. Should take the canvas object as argument. */
7
+ /** The function to draw on the canvas. This function automatically takes a canvas element as its only
8
+ * argument, e.g. `function(c) {...}` or `function drawStim(c) {...}`, where `c` refers to the canvas element.
9
+ * Note that the stimulus function will still generally need to set the correct context itself, using a line
10
+ * like `let ctx = c.getContext("2d")`.
11
+ */
6
12
  readonly stimulus: {
7
13
  readonly type: ParameterType.FUNCTION;
8
- readonly pretty_name: "Stimulus";
9
14
  readonly default: any;
10
15
  };
11
- /** Array containing the key(s) the subject is allowed to press to respond to the stimulus. */
16
+ /** This array contains the key(s) that the participant is allowed to press in order to respond to the stimulus.
17
+ * Keys should be specified as characters (e.g., `'a'`, `'q'`, `' '`, `'Enter'`, `'ArrowDown'`) -
18
+ * see [this page](https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/key/Key_Values)
19
+ * and [this page (event.key column)](https://www.freecodecamp.org/news/javascript-keycode-list-keypress-event-key-codes/)
20
+ * for more examples. Any key presses that are not listed in the array will be ignored. The default value
21
+ * of `"ALL_KEYS"` means that all keys will be accepted as valid responses. Specifying `"NO_KEYS"` will mean
22
+ * that no responses are allowed.
23
+ */
12
24
  readonly choices: {
13
25
  readonly type: ParameterType.KEYS;
14
- readonly pretty_name: "Choices";
15
26
  readonly default: "ALL_KEYS";
16
27
  };
17
- /** Any content here will be displayed below the stimulus. */
28
+ /** This string can contain HTML markup. Any content here will be displayed below the stimulus. The intention
29
+ * is that it can be used to provide a reminder about the action the participant is supposed to take (e.g., which key to press).
30
+ */
18
31
  readonly prompt: {
19
32
  readonly type: ParameterType.HTML_STRING;
20
- readonly pretty_name: "Prompt";
21
33
  readonly default: any;
22
34
  };
23
- /** How long to show the stimulus. */
35
+ /** How long to display the stimulus in milliseconds. The visibility CSS property of the stimulus will be set to
36
+ * `hidden` after this time has elapsed. If this is null, then the stimulus will remain visible until the trial ends.
37
+ */
24
38
  readonly stimulus_duration: {
25
39
  readonly type: ParameterType.INT;
26
- readonly pretty_name: "Stimulus duration";
27
40
  readonly default: any;
28
41
  };
29
- /** How long to show trial before it ends. */
42
+ /** How long to wait for the participant to make a response before ending the trial in milliseconds. If the
43
+ * participant fails to make a response before this timer is reached, the participant's response will be
44
+ * recorded as null for the trial and the trial will end. If the value of this parameter is null, then the
45
+ * trial will wait for a response indefinitely.
46
+ */
30
47
  readonly trial_duration: {
31
48
  readonly type: ParameterType.INT;
32
- readonly pretty_name: "Trial duration";
33
49
  readonly default: any;
34
50
  };
35
- /** If true, trial will end when subject makes a response. */
51
+ /** If true, then the trial will end whenever the participant makes a response (assuming they make their
52
+ * response before the cutoff specified by the `trial_duration` parameter). If false, then the trial will
53
+ * continue until the value for `trial_duration` is reached. You can use this parameter to force the participant
54
+ * to view a stimulus for a fixed amount of time, even if they respond before the time is complete.
55
+ */
36
56
  readonly response_ends_trial: {
37
57
  readonly type: ParameterType.BOOL;
38
- readonly pretty_name: "Response ends trial";
39
58
  readonly default: true;
40
59
  };
41
- /** Array containing the height (first value) and width (second value) of the canvas element. */
60
+ /** Array that defines the size of the canvas element in pixels. First value is height, second value is width. */
42
61
  readonly canvas_size: {
43
62
  readonly type: ParameterType.INT;
44
63
  readonly array: true;
45
- readonly pretty_name: "Canvas size";
46
64
  readonly default: readonly [500, 500];
47
65
  };
48
66
  };
67
+ readonly data: {
68
+ /** Indicates which key the participant pressed. */
69
+ readonly response: {
70
+ readonly type: ParameterType.STRING;
71
+ };
72
+ /** The response time in milliseconds for the participant to make a response. The time is measured from
73
+ * when the stimulus first appears on the screen until the participant's response.
74
+ */
75
+ readonly rt: {
76
+ readonly type: ParameterType.INT;
77
+ };
78
+ };
49
79
  };
50
80
  type Info = typeof info;
51
81
  /**
52
- * **canvas-keyboard-response**
53
- *
54
- * jsPsych plugin for displaying a canvas stimulus and getting a keyboard response
82
+ * This plugin can be used to draw a stimulus on a [HTML canvas element](https://www.w3schools.com/html/html5_canvas.asp) and
83
+ * record a keyboard response. The canvas stimulus can be useful for displaying dynamic, parametrically-defined graphics,
84
+ * and for controlling the positioning of multiple graphical elements (shapes, text, images). The stimulus can be
85
+ * displayed until a response is given, or for a pre-determined amount of time. The trial can be ended automatically
86
+ * if the participant has failed to respond within a fixed length of time.
55
87
  *
56
88
  * @author Chris Jungerius (modified from Josh de Leeuw)
57
- * @see {@link https://www.jspsych.org/plugins/jspsych-canvas-keyboard-response/ canvas-keyboard-response plugin documentation on jspsych.org}
89
+ * @see {@link https://www.jspsych.org/latest/plugins/canvas-keyboard-response/ canvas-keyboard-response plugin documentation on jspsych.org}
58
90
  */
59
91
  declare class CanvasKeyboardResponsePlugin implements JsPsychPlugin<Info> {
60
92
  private jsPsych;
61
93
  static info: {
62
94
  readonly name: "canvas-keyboard-response";
95
+ readonly version: string;
63
96
  readonly parameters: {
64
- /** The drawing function to apply to the canvas. Should take the canvas object as argument. */
97
+ /** The function to draw on the canvas. This function automatically takes a canvas element as its only
98
+ * argument, e.g. `function(c) {...}` or `function drawStim(c) {...}`, where `c` refers to the canvas element.
99
+ * Note that the stimulus function will still generally need to set the correct context itself, using a line
100
+ * like `let ctx = c.getContext("2d")`.
101
+ */
65
102
  readonly stimulus: {
66
103
  readonly type: ParameterType.FUNCTION;
67
- readonly pretty_name: "Stimulus";
68
104
  readonly default: any;
69
105
  };
70
- /** Array containing the key(s) the subject is allowed to press to respond to the stimulus. */
106
+ /** This array contains the key(s) that the participant is allowed to press in order to respond to the stimulus.
107
+ * Keys should be specified as characters (e.g., `'a'`, `'q'`, `' '`, `'Enter'`, `'ArrowDown'`) -
108
+ * see [this page](https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/key/Key_Values)
109
+ * and [this page (event.key column)](https://www.freecodecamp.org/news/javascript-keycode-list-keypress-event-key-codes/)
110
+ * for more examples. Any key presses that are not listed in the array will be ignored. The default value
111
+ * of `"ALL_KEYS"` means that all keys will be accepted as valid responses. Specifying `"NO_KEYS"` will mean
112
+ * that no responses are allowed.
113
+ */
71
114
  readonly choices: {
72
115
  readonly type: ParameterType.KEYS;
73
- readonly pretty_name: "Choices";
74
116
  readonly default: "ALL_KEYS";
75
117
  };
76
- /** Any content here will be displayed below the stimulus. */
118
+ /** This string can contain HTML markup. Any content here will be displayed below the stimulus. The intention
119
+ * is that it can be used to provide a reminder about the action the participant is supposed to take (e.g., which key to press).
120
+ */
77
121
  readonly prompt: {
78
122
  readonly type: ParameterType.HTML_STRING;
79
- readonly pretty_name: "Prompt";
80
123
  readonly default: any;
81
124
  };
82
- /** How long to show the stimulus. */
125
+ /** How long to display the stimulus in milliseconds. The visibility CSS property of the stimulus will be set to
126
+ * `hidden` after this time has elapsed. If this is null, then the stimulus will remain visible until the trial ends.
127
+ */
83
128
  readonly stimulus_duration: {
84
129
  readonly type: ParameterType.INT;
85
- readonly pretty_name: "Stimulus duration";
86
130
  readonly default: any;
87
131
  };
88
- /** How long to show trial before it ends. */
132
+ /** How long to wait for the participant to make a response before ending the trial in milliseconds. If the
133
+ * participant fails to make a response before this timer is reached, the participant's response will be
134
+ * recorded as null for the trial and the trial will end. If the value of this parameter is null, then the
135
+ * trial will wait for a response indefinitely.
136
+ */
89
137
  readonly trial_duration: {
90
138
  readonly type: ParameterType.INT;
91
- readonly pretty_name: "Trial duration";
92
139
  readonly default: any;
93
140
  };
94
- /** If true, trial will end when subject makes a response. */
141
+ /** If true, then the trial will end whenever the participant makes a response (assuming they make their
142
+ * response before the cutoff specified by the `trial_duration` parameter). If false, then the trial will
143
+ * continue until the value for `trial_duration` is reached. You can use this parameter to force the participant
144
+ * to view a stimulus for a fixed amount of time, even if they respond before the time is complete.
145
+ */
95
146
  readonly response_ends_trial: {
96
147
  readonly type: ParameterType.BOOL;
97
- readonly pretty_name: "Response ends trial";
98
148
  readonly default: true;
99
149
  };
100
- /** Array containing the height (first value) and width (second value) of the canvas element. */
150
+ /** Array that defines the size of the canvas element in pixels. First value is height, second value is width. */
101
151
  readonly canvas_size: {
102
152
  readonly type: ParameterType.INT;
103
153
  readonly array: true;
104
- readonly pretty_name: "Canvas size";
105
154
  readonly default: readonly [500, 500];
106
155
  };
107
156
  };
157
+ readonly data: {
158
+ /** Indicates which key the participant pressed. */
159
+ readonly response: {
160
+ readonly type: ParameterType.STRING;
161
+ };
162
+ /** The response time in milliseconds for the participant to make a response. The time is measured from
163
+ * when the stimulus first appears on the screen until the participant's response.
164
+ */
165
+ readonly rt: {
166
+ readonly type: ParameterType.INT;
167
+ };
168
+ };
108
169
  };
109
170
  constructor(jsPsych: JsPsych);
110
171
  trial(display_element: HTMLElement, trial: TrialType<Info>): void;
@@ -113,4 +174,5 @@ declare class CanvasKeyboardResponsePlugin implements JsPsychPlugin<Info> {
113
174
  private simulate_visual;
114
175
  private create_simulation_data;
115
176
  }
116
- export default CanvasKeyboardResponsePlugin;
177
+
178
+ export { CanvasKeyboardResponsePlugin as default };