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

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,173 +1,173 @@
1
1
  import { ParameterType } from 'jspsych';
2
2
 
3
- 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
- },
49
- },
50
- };
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
- class CanvasKeyboardResponsePlugin {
60
- constructor(jsPsych) {
61
- this.jsPsych = jsPsych;
62
- }
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
- }
138
- }
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
- }
147
- }
148
- simulate_data_only(trial, simulation_options) {
149
- const data = this.create_simulation_data(trial, simulation_options);
150
- this.jsPsych.finishTrial(data);
151
- }
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
- }
160
- }
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;
169
- }
170
- }
3
+ 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
+ },
49
+ },
50
+ };
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
+ class CanvasKeyboardResponsePlugin {
60
+ constructor(jsPsych) {
61
+ this.jsPsych = jsPsych;
62
+ }
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
+ }
138
+ }
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
+ }
147
+ }
148
+ simulate_data_only(trial, simulation_options) {
149
+ const data = this.create_simulation_data(trial, simulation_options);
150
+ this.jsPsych.finishTrial(data);
151
+ }
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
+ }
160
+ }
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;
169
+ }
170
+ }
171
171
  CanvasKeyboardResponsePlugin.info = info;
172
172
 
173
173
  export { CanvasKeyboardResponsePlugin as default };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jspsych/plugin-canvas-keyboard-response",
3
- "version": "1.1.2",
3
+ "version": "1.1.3",
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": "^1.3.0",
40
+ "@jspsych/config": "^2.0.0",
41
41
  "@jspsych/test-utils": "^1.1.2"
42
42
  }
43
43
  }