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