@jspsych/plugin-canvas-keyboard-response 1.1.1 → 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.cjs CHANGED
@@ -2,174 +2,174 @@
2
2
 
3
3
  var jspsych = require('jspsych');
4
4
 
5
- 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
- },
51
- },
52
- };
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
- class CanvasKeyboardResponsePlugin {
62
- constructor(jsPsych) {
63
- this.jsPsych = jsPsych;
64
- }
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
- }
140
- }
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
- }
149
- }
150
- simulate_data_only(trial, simulation_options) {
151
- const data = this.create_simulation_data(trial, simulation_options);
152
- this.jsPsych.finishTrial(data);
153
- }
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
- }
162
- }
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;
171
- }
172
- }
5
+ 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
+ },
51
+ },
52
+ };
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
+ class CanvasKeyboardResponsePlugin {
62
+ constructor(jsPsych) {
63
+ this.jsPsych = jsPsych;
64
+ }
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
+ }
140
+ }
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
+ }
149
+ }
150
+ simulate_data_only(trial, simulation_options) {
151
+ const data = this.create_simulation_data(trial, simulation_options);
152
+ this.jsPsych.finishTrial(data);
153
+ }
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
+ }
162
+ }
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;
171
+ }
172
+ }
173
173
  CanvasKeyboardResponsePlugin.info = info;
174
174
 
175
175
  module.exports = CanvasKeyboardResponsePlugin;
package/dist/index.d.ts CHANGED
@@ -1,116 +1,116 @@
1
- import { JsPsych, JsPsychPlugin, ParameterType, TrialType } from "jspsych";
2
- declare const info: {
3
- readonly name: "canvas-keyboard-response";
4
- readonly parameters: {
5
- /** The drawing function to apply to the canvas. Should take the canvas object as argument. */
6
- readonly stimulus: {
7
- readonly type: ParameterType.FUNCTION;
8
- readonly pretty_name: "Stimulus";
9
- readonly default: any;
10
- };
11
- /** Array containing the key(s) the subject is allowed to press to respond to the stimulus. */
12
- readonly choices: {
13
- readonly type: ParameterType.KEYS;
14
- readonly pretty_name: "Choices";
15
- readonly default: "ALL_KEYS";
16
- };
17
- /** Any content here will be displayed below the stimulus. */
18
- readonly prompt: {
19
- readonly type: ParameterType.HTML_STRING;
20
- readonly pretty_name: "Prompt";
21
- readonly default: any;
22
- };
23
- /** How long to show the stimulus. */
24
- readonly stimulus_duration: {
25
- readonly type: ParameterType.INT;
26
- readonly pretty_name: "Stimulus duration";
27
- readonly default: any;
28
- };
29
- /** How long to show trial before it ends. */
30
- readonly trial_duration: {
31
- readonly type: ParameterType.INT;
32
- readonly pretty_name: "Trial duration";
33
- readonly default: any;
34
- };
35
- /** If true, trial will end when subject makes a response. */
36
- readonly response_ends_trial: {
37
- readonly type: ParameterType.BOOL;
38
- readonly pretty_name: "Response ends trial";
39
- readonly default: true;
40
- };
41
- /** Array containing the height (first value) and width (second value) of the canvas element. */
42
- readonly canvas_size: {
43
- readonly type: ParameterType.INT;
44
- readonly array: true;
45
- readonly pretty_name: "Canvas size";
46
- readonly default: readonly [500, 500];
47
- };
48
- };
49
- };
50
- declare type Info = typeof info;
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
- declare class CanvasKeyboardResponsePlugin implements JsPsychPlugin<Info> {
60
- private jsPsych;
61
- static info: {
62
- readonly name: "canvas-keyboard-response";
63
- readonly parameters: {
64
- /** The drawing function to apply to the canvas. Should take the canvas object as argument. */
65
- readonly stimulus: {
66
- readonly type: ParameterType.FUNCTION;
67
- readonly pretty_name: "Stimulus";
68
- readonly default: any;
69
- };
70
- /** Array containing the key(s) the subject is allowed to press to respond to the stimulus. */
71
- readonly choices: {
72
- readonly type: ParameterType.KEYS;
73
- readonly pretty_name: "Choices";
74
- readonly default: "ALL_KEYS";
75
- };
76
- /** Any content here will be displayed below the stimulus. */
77
- readonly prompt: {
78
- readonly type: ParameterType.HTML_STRING;
79
- readonly pretty_name: "Prompt";
80
- readonly default: any;
81
- };
82
- /** How long to show the stimulus. */
83
- readonly stimulus_duration: {
84
- readonly type: ParameterType.INT;
85
- readonly pretty_name: "Stimulus duration";
86
- readonly default: any;
87
- };
88
- /** How long to show trial before it ends. */
89
- readonly trial_duration: {
90
- readonly type: ParameterType.INT;
91
- readonly pretty_name: "Trial duration";
92
- readonly default: any;
93
- };
94
- /** If true, trial will end when subject makes a response. */
95
- readonly response_ends_trial: {
96
- readonly type: ParameterType.BOOL;
97
- readonly pretty_name: "Response ends trial";
98
- readonly default: true;
99
- };
100
- /** Array containing the height (first value) and width (second value) of the canvas element. */
101
- readonly canvas_size: {
102
- readonly type: ParameterType.INT;
103
- readonly array: true;
104
- readonly pretty_name: "Canvas size";
105
- readonly default: readonly [500, 500];
106
- };
107
- };
108
- };
109
- constructor(jsPsych: JsPsych);
110
- trial(display_element: HTMLElement, trial: TrialType<Info>): void;
111
- simulate(trial: TrialType<Info>, simulation_mode: any, simulation_options: any, load_callback: () => void): void;
112
- private simulate_data_only;
113
- private simulate_visual;
114
- private create_simulation_data;
115
- }
116
- export default CanvasKeyboardResponsePlugin;
1
+ import { JsPsych, JsPsychPlugin, ParameterType, TrialType } from "jspsych";
2
+ declare const info: {
3
+ readonly name: "canvas-keyboard-response";
4
+ readonly parameters: {
5
+ /** The drawing function to apply to the canvas. Should take the canvas object as argument. */
6
+ readonly stimulus: {
7
+ readonly type: ParameterType.FUNCTION;
8
+ readonly pretty_name: "Stimulus";
9
+ readonly default: any;
10
+ };
11
+ /** Array containing the key(s) the subject is allowed to press to respond to the stimulus. */
12
+ readonly choices: {
13
+ readonly type: ParameterType.KEYS;
14
+ readonly pretty_name: "Choices";
15
+ readonly default: "ALL_KEYS";
16
+ };
17
+ /** Any content here will be displayed below the stimulus. */
18
+ readonly prompt: {
19
+ readonly type: ParameterType.HTML_STRING;
20
+ readonly pretty_name: "Prompt";
21
+ readonly default: any;
22
+ };
23
+ /** How long to show the stimulus. */
24
+ readonly stimulus_duration: {
25
+ readonly type: ParameterType.INT;
26
+ readonly pretty_name: "Stimulus duration";
27
+ readonly default: any;
28
+ };
29
+ /** How long to show trial before it ends. */
30
+ readonly trial_duration: {
31
+ readonly type: ParameterType.INT;
32
+ readonly pretty_name: "Trial duration";
33
+ readonly default: any;
34
+ };
35
+ /** If true, trial will end when subject makes a response. */
36
+ readonly response_ends_trial: {
37
+ readonly type: ParameterType.BOOL;
38
+ readonly pretty_name: "Response ends trial";
39
+ readonly default: true;
40
+ };
41
+ /** Array containing the height (first value) and width (second value) of the canvas element. */
42
+ readonly canvas_size: {
43
+ readonly type: ParameterType.INT;
44
+ readonly array: true;
45
+ readonly pretty_name: "Canvas size";
46
+ readonly default: readonly [500, 500];
47
+ };
48
+ };
49
+ };
50
+ type Info = typeof info;
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
+ declare class CanvasKeyboardResponsePlugin implements JsPsychPlugin<Info> {
60
+ private jsPsych;
61
+ static info: {
62
+ readonly name: "canvas-keyboard-response";
63
+ readonly parameters: {
64
+ /** The drawing function to apply to the canvas. Should take the canvas object as argument. */
65
+ readonly stimulus: {
66
+ readonly type: ParameterType.FUNCTION;
67
+ readonly pretty_name: "Stimulus";
68
+ readonly default: any;
69
+ };
70
+ /** Array containing the key(s) the subject is allowed to press to respond to the stimulus. */
71
+ readonly choices: {
72
+ readonly type: ParameterType.KEYS;
73
+ readonly pretty_name: "Choices";
74
+ readonly default: "ALL_KEYS";
75
+ };
76
+ /** Any content here will be displayed below the stimulus. */
77
+ readonly prompt: {
78
+ readonly type: ParameterType.HTML_STRING;
79
+ readonly pretty_name: "Prompt";
80
+ readonly default: any;
81
+ };
82
+ /** How long to show the stimulus. */
83
+ readonly stimulus_duration: {
84
+ readonly type: ParameterType.INT;
85
+ readonly pretty_name: "Stimulus duration";
86
+ readonly default: any;
87
+ };
88
+ /** How long to show trial before it ends. */
89
+ readonly trial_duration: {
90
+ readonly type: ParameterType.INT;
91
+ readonly pretty_name: "Trial duration";
92
+ readonly default: any;
93
+ };
94
+ /** If true, trial will end when subject makes a response. */
95
+ readonly response_ends_trial: {
96
+ readonly type: ParameterType.BOOL;
97
+ readonly pretty_name: "Response ends trial";
98
+ readonly default: true;
99
+ };
100
+ /** Array containing the height (first value) and width (second value) of the canvas element. */
101
+ readonly canvas_size: {
102
+ readonly type: ParameterType.INT;
103
+ readonly array: true;
104
+ readonly pretty_name: "Canvas size";
105
+ readonly default: readonly [500, 500];
106
+ };
107
+ };
108
+ };
109
+ constructor(jsPsych: JsPsych);
110
+ trial(display_element: HTMLElement, trial: TrialType<Info>): void;
111
+ simulate(trial: TrialType<Info>, simulation_mode: any, simulation_options: any, load_callback: () => void): void;
112
+ private simulate_data_only;
113
+ private simulate_visual;
114
+ private create_simulation_data;
115
+ }
116
+ export default CanvasKeyboardResponsePlugin;