@jspsych/plugin-canvas-keyboard-response 1.1.3 → 2.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.browser.js +167 -159
- package/dist/index.browser.js.map +1 -1
- package/dist/index.browser.min.js +2 -2
- package/dist/index.browser.min.js.map +1 -1
- package/dist/index.cjs +166 -158
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +98 -34
- package/dist/index.js +166 -158
- package/dist/index.js.map +1 -1
- package/package.json +3 -3
- package/src/index.ts +54 -24
package/dist/index.cjs
CHANGED
|
@@ -2,175 +2,183 @@
|
|
|
2
2
|
|
|
3
3
|
var jspsych = require('jspsych');
|
|
4
4
|
|
|
5
|
+
var version = "2.1.0";
|
|
6
|
+
|
|
5
7
|
const info = {
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
8
|
+
name: "canvas-keyboard-response",
|
|
9
|
+
version,
|
|
10
|
+
parameters: {
|
|
11
|
+
/** The function to draw on the canvas. This function automatically takes a canvas element as its only
|
|
12
|
+
* argument, e.g. `function(c) {...}` or `function drawStim(c) {...}`, where `c` refers to the canvas element.
|
|
13
|
+
* Note that the stimulus function will still generally need to set the correct context itself, using a line
|
|
14
|
+
* like `let ctx = c.getContext("2d")`.
|
|
15
|
+
*/
|
|
16
|
+
stimulus: {
|
|
17
|
+
type: jspsych.ParameterType.FUNCTION,
|
|
18
|
+
default: void 0
|
|
19
|
+
},
|
|
20
|
+
/** This array contains the key(s) that the participant is allowed to press in order to respond to the stimulus.
|
|
21
|
+
* Keys should be specified as characters (e.g., `'a'`, `'q'`, `' '`, `'Enter'`, `'ArrowDown'`) -
|
|
22
|
+
* see [this page](https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/key/Key_Values)
|
|
23
|
+
* and [this page (event.key column)](https://www.freecodecamp.org/news/javascript-keycode-list-keypress-event-key-codes/)
|
|
24
|
+
* for more examples. Any key presses that are not listed in the array will be ignored. The default value
|
|
25
|
+
* of `"ALL_KEYS"` means that all keys will be accepted as valid responses. Specifying `"NO_KEYS"` will mean
|
|
26
|
+
* that no responses are allowed.
|
|
27
|
+
*/
|
|
28
|
+
choices: {
|
|
29
|
+
type: jspsych.ParameterType.KEYS,
|
|
30
|
+
default: "ALL_KEYS"
|
|
31
|
+
},
|
|
32
|
+
/** This string can contain HTML markup. Any content here will be displayed below the stimulus. The intention
|
|
33
|
+
* 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).
|
|
34
|
+
*/
|
|
35
|
+
prompt: {
|
|
36
|
+
type: jspsych.ParameterType.HTML_STRING,
|
|
37
|
+
default: null
|
|
38
|
+
},
|
|
39
|
+
/** How long to display the stimulus in milliseconds. The visibility CSS property of the stimulus will be set to
|
|
40
|
+
* `hidden` after this time has elapsed. If this is null, then the stimulus will remain visible until the trial ends.
|
|
41
|
+
*/
|
|
42
|
+
stimulus_duration: {
|
|
43
|
+
type: jspsych.ParameterType.INT,
|
|
44
|
+
default: null
|
|
45
|
+
},
|
|
46
|
+
/** How long to wait for the participant to make a response before ending the trial in milliseconds. If the
|
|
47
|
+
* participant fails to make a response before this timer is reached, the participant's response will be
|
|
48
|
+
* recorded as null for the trial and the trial will end. If the value of this parameter is null, then the
|
|
49
|
+
* trial will wait for a response indefinitely.
|
|
50
|
+
*/
|
|
51
|
+
trial_duration: {
|
|
52
|
+
type: jspsych.ParameterType.INT,
|
|
53
|
+
default: null
|
|
51
54
|
},
|
|
55
|
+
/** If true, then the trial will end whenever the participant makes a response (assuming they make their
|
|
56
|
+
* response before the cutoff specified by the `trial_duration` parameter). If false, then the trial will
|
|
57
|
+
* continue until the value for `trial_duration` is reached. You can use this parameter to force the participant
|
|
58
|
+
* to view a stimulus for a fixed amount of time, even if they respond before the time is complete.
|
|
59
|
+
*/
|
|
60
|
+
response_ends_trial: {
|
|
61
|
+
type: jspsych.ParameterType.BOOL,
|
|
62
|
+
default: true
|
|
63
|
+
},
|
|
64
|
+
/** Array that defines the size of the canvas element in pixels. First value is height, second value is width. */
|
|
65
|
+
canvas_size: {
|
|
66
|
+
type: jspsych.ParameterType.INT,
|
|
67
|
+
array: true,
|
|
68
|
+
default: [500, 500]
|
|
69
|
+
}
|
|
70
|
+
},
|
|
71
|
+
data: {
|
|
72
|
+
/** Indicates which key the participant pressed. */
|
|
73
|
+
response: {
|
|
74
|
+
type: jspsych.ParameterType.STRING
|
|
75
|
+
},
|
|
76
|
+
/** The response time in milliseconds for the participant to make a response. The time is measured from
|
|
77
|
+
* when the stimulus first appears on the screen until the participant's response.
|
|
78
|
+
*/
|
|
79
|
+
rt: {
|
|
80
|
+
type: jspsych.ParameterType.INT
|
|
81
|
+
}
|
|
82
|
+
},
|
|
83
|
+
// prettier-ignore
|
|
84
|
+
citations: {
|
|
85
|
+
"apa": "de Leeuw, J. R., Gilbert, R. A., & Luchterhandt, B. (2023). jsPsych: Enabling an Open-Source Collaborative Ecosystem of Behavioral Experiments. Journal of Open Source Software, 8(85), 5351. https://doi.org/10.21105/joss.05351 ",
|
|
86
|
+
"bibtex": '@article{Leeuw2023jsPsych, author = {de Leeuw, Joshua R. and Gilbert, Rebecca A. and Luchterhandt, Bj{\\" o}rn}, journal = {Journal of Open Source Software}, doi = {10.21105/joss.05351}, issn = {2475-9066}, number = {85}, year = {2023}, month = {may 11}, pages = {5351}, publisher = {Open Journals}, title = {jsPsych: Enabling an {Open}-{Source} {Collaborative} {Ecosystem} of {Behavioral} {Experiments}}, url = {https://joss.theoj.org/papers/10.21105/joss.05351}, volume = {8}, } '
|
|
87
|
+
}
|
|
52
88
|
};
|
|
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
89
|
class CanvasKeyboardResponsePlugin {
|
|
62
|
-
|
|
63
|
-
|
|
90
|
+
constructor(jsPsych) {
|
|
91
|
+
this.jsPsych = jsPsych;
|
|
92
|
+
}
|
|
93
|
+
static {
|
|
94
|
+
this.info = info;
|
|
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
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
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
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
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
|
-
|
|
151
|
-
|
|
152
|
-
|
|
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
|
-
|
|
155
|
-
|
|
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
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
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
|
-
CanvasKeyboardResponsePlugin.info = info;
|
|
174
182
|
|
|
175
183
|
module.exports = CanvasKeyboardResponsePlugin;
|
|
176
184
|
//# sourceMappingURL=index.cjs.map
|
package/dist/index.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.cjs","sources":["../src/index.ts"],"sourcesContent":["import { JsPsych, JsPsychPlugin, ParameterType, TrialType } from \"jspsych\";\n\nconst info = <const>{\n name: \"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":";;;;AAEA,MAAM,IAAI,GAAU;AAClB,IAAA,IAAI,EAAE,0BAA0B;AAChC,IAAA,UAAU,EAAE;;AAEV,QAAA,QAAQ,EAAE;YACR,IAAI,EAAEA,qBAAa,CAAC,QAAQ;AAC5B,YAAA,WAAW,EAAE,UAAU;AACvB,YAAA,OAAO,EAAE,SAAS;AACnB,SAAA;;AAED,QAAA,OAAO,EAAE;YACP,IAAI,EAAEA,qBAAa,CAAC,IAAI;AACxB,YAAA,WAAW,EAAE,SAAS;AACtB,YAAA,OAAO,EAAE,UAAU;AACpB,SAAA;;AAED,QAAA,MAAM,EAAE;YACN,IAAI,EAAEA,qBAAa,CAAC,WAAW;AAC/B,YAAA,WAAW,EAAE,QAAQ;AACrB,YAAA,OAAO,EAAE,IAAI;AACd,SAAA;;AAED,QAAA,iBAAiB,EAAE;YACjB,IAAI,EAAEA,qBAAa,CAAC,GAAG;AACvB,YAAA,WAAW,EAAE,mBAAmB;AAChC,YAAA,OAAO,EAAE,IAAI;AACd,SAAA;;AAED,QAAA,cAAc,EAAE;YACd,IAAI,EAAEA,qBAAa,CAAC,GAAG;AACvB,YAAA,WAAW,EAAE,gBAAgB;AAC7B,YAAA,OAAO,EAAE,IAAI;AACd,SAAA;;AAED,QAAA,mBAAmB,EAAE;YACnB,IAAI,EAAEA,qBAAa,CAAC,IAAI;AACxB,YAAA,WAAW,EAAE,qBAAqB;AAClC,YAAA,OAAO,EAAE,IAAI;AACd,SAAA;;AAED,QAAA,WAAW,EAAE;YACX,IAAI,EAAEA,qBAAa,CAAC,GAAG;AACvB,YAAA,KAAK,EAAE,IAA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A,EAAE,EAAE,IAAI,CAAC,OAAO,CAAC,aAAa,CAAC,gBAAgB,CAAC,GAAG,EAAE,EAAE,EAAE,CAAC,GAAG,GAAG,EAAE,IAAI,CAAC;AACvE,YAAA,QAAQ,EAAE,IAAI,CAAC,OAAO,CAAC,SAAS,CAAC,WAAW,CAAC,KAAK,CAAC,OAAO,CAAC;SAC5D,CAAC;AAEF,QAAA,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":["../package.json","../src/index.ts"],"sourcesContent":["{\n \"name\": \"@jspsych/plugin-canvas-keyboard-response\",\n \"version\": \"2.1.0\",\n \"description\": \"jsPsych plugin for displaying a canvas stimulus and getting a keyboard response\",\n \"type\": \"module\",\n \"main\": \"dist/index.cjs\",\n \"exports\": {\n \"import\": \"./dist/index.js\",\n \"require\": \"./dist/index.cjs\"\n },\n \"typings\": \"dist/index.d.ts\",\n \"unpkg\": \"dist/index.browser.min.js\",\n \"files\": [\n \"src\",\n \"dist\"\n ],\n \"source\": \"src/index.ts\",\n \"scripts\": {\n \"test\": \"jest --passWithNoTests\",\n \"test:watch\": \"npm test -- --watch\",\n \"tsc\": \"tsc\",\n \"build\": \"rollup --config\",\n \"build:watch\": \"npm run build -- --watch\"\n },\n \"repository\": {\n \"type\": \"git\",\n \"url\": \"git+https://github.com/jspsych/jsPsych.git\",\n \"directory\": \"packages/plugin-canvas-keyboard-response\"\n },\n \"author\": \"Chris Jungerius, Josh de Leeuw\",\n \"license\": \"MIT\",\n \"bugs\": {\n \"url\": \"https://github.com/jspsych/jsPsych/issues\"\n },\n \"homepage\": \"https://www.jspsych.org/latest/plugins/canvas-keyboard-response\",\n \"peerDependencies\": {\n \"jspsych\": \">=7.1.0\"\n },\n \"devDependencies\": {\n \"@jspsych/config\": \"^3.2.0\",\n \"@jspsych/test-utils\": \"^1.2.0\"\n }\n}\n","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 // prettier-ignore\n citations: '__CITATIONS__',\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":[],"mappings":";;;;AAEE,IAAW,OAAA,GAAA,OAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;EC+EA,SAAA,EAAA;AAAA;;GAAe;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
|
package/dist/index.d.ts
CHANGED
|
@@ -1,110 +1,173 @@
|
|
|
1
|
-
import {
|
|
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
|
|
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
|
-
/**
|
|
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
|
|
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
|
|
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
|
|
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
|
|
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
|
+
};
|
|
79
|
+
readonly citations: "__CITATIONS__";
|
|
49
80
|
};
|
|
50
81
|
type Info = typeof info;
|
|
51
82
|
/**
|
|
52
|
-
*
|
|
53
|
-
*
|
|
54
|
-
*
|
|
83
|
+
* This plugin can be used to draw a stimulus on a [HTML canvas element](https://www.w3schools.com/html/html5_canvas.asp) and
|
|
84
|
+
* record a keyboard response. The canvas stimulus can be useful for displaying dynamic, parametrically-defined graphics,
|
|
85
|
+
* and for controlling the positioning of multiple graphical elements (shapes, text, images). The stimulus can be
|
|
86
|
+
* displayed until a response is given, or for a pre-determined amount of time. The trial can be ended automatically
|
|
87
|
+
* if the participant has failed to respond within a fixed length of time.
|
|
55
88
|
*
|
|
56
89
|
* @author Chris Jungerius (modified from Josh de Leeuw)
|
|
57
|
-
* @see {@link https://www.jspsych.org/plugins/
|
|
90
|
+
* @see {@link https://www.jspsych.org/latest/plugins/canvas-keyboard-response/ canvas-keyboard-response plugin documentation on jspsych.org}
|
|
58
91
|
*/
|
|
59
92
|
declare class CanvasKeyboardResponsePlugin implements JsPsychPlugin<Info> {
|
|
60
93
|
private jsPsych;
|
|
61
94
|
static info: {
|
|
62
95
|
readonly name: "canvas-keyboard-response";
|
|
96
|
+
readonly version: string;
|
|
63
97
|
readonly parameters: {
|
|
64
|
-
/** The
|
|
98
|
+
/** The function to draw on the canvas. This function automatically takes a canvas element as its only
|
|
99
|
+
* argument, e.g. `function(c) {...}` or `function drawStim(c) {...}`, where `c` refers to the canvas element.
|
|
100
|
+
* Note that the stimulus function will still generally need to set the correct context itself, using a line
|
|
101
|
+
* like `let ctx = c.getContext("2d")`.
|
|
102
|
+
*/
|
|
65
103
|
readonly stimulus: {
|
|
66
104
|
readonly type: ParameterType.FUNCTION;
|
|
67
|
-
readonly pretty_name: "Stimulus";
|
|
68
105
|
readonly default: any;
|
|
69
106
|
};
|
|
70
|
-
/**
|
|
107
|
+
/** This array contains the key(s) that the participant is allowed to press in order to respond to the stimulus.
|
|
108
|
+
* Keys should be specified as characters (e.g., `'a'`, `'q'`, `' '`, `'Enter'`, `'ArrowDown'`) -
|
|
109
|
+
* see [this page](https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/key/Key_Values)
|
|
110
|
+
* and [this page (event.key column)](https://www.freecodecamp.org/news/javascript-keycode-list-keypress-event-key-codes/)
|
|
111
|
+
* for more examples. Any key presses that are not listed in the array will be ignored. The default value
|
|
112
|
+
* of `"ALL_KEYS"` means that all keys will be accepted as valid responses. Specifying `"NO_KEYS"` will mean
|
|
113
|
+
* that no responses are allowed.
|
|
114
|
+
*/
|
|
71
115
|
readonly choices: {
|
|
72
116
|
readonly type: ParameterType.KEYS;
|
|
73
|
-
readonly pretty_name: "Choices";
|
|
74
117
|
readonly default: "ALL_KEYS";
|
|
75
118
|
};
|
|
76
|
-
/** Any content here will be displayed below the stimulus.
|
|
119
|
+
/** This string can contain HTML markup. Any content here will be displayed below the stimulus. The intention
|
|
120
|
+
* 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).
|
|
121
|
+
*/
|
|
77
122
|
readonly prompt: {
|
|
78
123
|
readonly type: ParameterType.HTML_STRING;
|
|
79
|
-
readonly pretty_name: "Prompt";
|
|
80
124
|
readonly default: any;
|
|
81
125
|
};
|
|
82
|
-
/** How long to
|
|
126
|
+
/** How long to display the stimulus in milliseconds. The visibility CSS property of the stimulus will be set to
|
|
127
|
+
* `hidden` after this time has elapsed. If this is null, then the stimulus will remain visible until the trial ends.
|
|
128
|
+
*/
|
|
83
129
|
readonly stimulus_duration: {
|
|
84
130
|
readonly type: ParameterType.INT;
|
|
85
|
-
readonly pretty_name: "Stimulus duration";
|
|
86
131
|
readonly default: any;
|
|
87
132
|
};
|
|
88
|
-
/** How long to
|
|
133
|
+
/** How long to wait for the participant to make a response before ending the trial in milliseconds. If the
|
|
134
|
+
* participant fails to make a response before this timer is reached, the participant's response will be
|
|
135
|
+
* recorded as null for the trial and the trial will end. If the value of this parameter is null, then the
|
|
136
|
+
* trial will wait for a response indefinitely.
|
|
137
|
+
*/
|
|
89
138
|
readonly trial_duration: {
|
|
90
139
|
readonly type: ParameterType.INT;
|
|
91
|
-
readonly pretty_name: "Trial duration";
|
|
92
140
|
readonly default: any;
|
|
93
141
|
};
|
|
94
|
-
/** If true, trial will end
|
|
142
|
+
/** If true, then the trial will end whenever the participant makes a response (assuming they make their
|
|
143
|
+
* response before the cutoff specified by the `trial_duration` parameter). If false, then the trial will
|
|
144
|
+
* continue until the value for `trial_duration` is reached. You can use this parameter to force the participant
|
|
145
|
+
* to view a stimulus for a fixed amount of time, even if they respond before the time is complete.
|
|
146
|
+
*/
|
|
95
147
|
readonly response_ends_trial: {
|
|
96
148
|
readonly type: ParameterType.BOOL;
|
|
97
|
-
readonly pretty_name: "Response ends trial";
|
|
98
149
|
readonly default: true;
|
|
99
150
|
};
|
|
100
|
-
/** Array
|
|
151
|
+
/** Array that defines the size of the canvas element in pixels. First value is height, second value is width. */
|
|
101
152
|
readonly canvas_size: {
|
|
102
153
|
readonly type: ParameterType.INT;
|
|
103
154
|
readonly array: true;
|
|
104
|
-
readonly pretty_name: "Canvas size";
|
|
105
155
|
readonly default: readonly [500, 500];
|
|
106
156
|
};
|
|
107
157
|
};
|
|
158
|
+
readonly data: {
|
|
159
|
+
/** Indicates which key the participant pressed. */
|
|
160
|
+
readonly response: {
|
|
161
|
+
readonly type: ParameterType.STRING;
|
|
162
|
+
};
|
|
163
|
+
/** The response time in milliseconds for the participant to make a response. The time is measured from
|
|
164
|
+
* when the stimulus first appears on the screen until the participant's response.
|
|
165
|
+
*/
|
|
166
|
+
readonly rt: {
|
|
167
|
+
readonly type: ParameterType.INT;
|
|
168
|
+
};
|
|
169
|
+
};
|
|
170
|
+
readonly citations: "__CITATIONS__";
|
|
108
171
|
};
|
|
109
172
|
constructor(jsPsych: JsPsych);
|
|
110
173
|
trial(display_element: HTMLElement, trial: TrialType<Info>): void;
|
|
@@ -113,4 +176,5 @@ declare class CanvasKeyboardResponsePlugin implements JsPsychPlugin<Info> {
|
|
|
113
176
|
private simulate_visual;
|
|
114
177
|
private create_simulation_data;
|
|
115
178
|
}
|
|
116
|
-
|
|
179
|
+
|
|
180
|
+
export { CanvasKeyboardResponsePlugin as default };
|