@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.browser.js +168 -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 -157
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +96 -34
- package/dist/index.js +166 -157
- package/dist/index.js.map +1 -1
- package/package.json +3 -3
- package/src/index.ts +52 -24
package/dist/index.browser.js
CHANGED
|
@@ -1,177 +1,186 @@
|
|
|
1
1
|
var jsPsychCanvasKeyboardResponse = (function (jspsych) {
|
|
2
2
|
'use strict';
|
|
3
3
|
|
|
4
|
+
var _package = {
|
|
5
|
+
name: "@jspsych/plugin-canvas-keyboard-response",
|
|
6
|
+
version: "2.0.0",
|
|
7
|
+
description: "jsPsych plugin for displaying a canvas stimulus and getting a keyboard response",
|
|
8
|
+
type: "module",
|
|
9
|
+
main: "dist/index.cjs",
|
|
10
|
+
exports: {
|
|
11
|
+
import: "./dist/index.js",
|
|
12
|
+
require: "./dist/index.cjs"
|
|
13
|
+
},
|
|
14
|
+
typings: "dist/index.d.ts",
|
|
15
|
+
unpkg: "dist/index.browser.min.js",
|
|
16
|
+
files: [
|
|
17
|
+
"src",
|
|
18
|
+
"dist"
|
|
19
|
+
],
|
|
20
|
+
source: "src/index.ts",
|
|
21
|
+
scripts: {
|
|
22
|
+
test: "jest --passWithNoTests",
|
|
23
|
+
"test:watch": "npm test -- --watch",
|
|
24
|
+
tsc: "tsc",
|
|
25
|
+
build: "rollup --config",
|
|
26
|
+
"build:watch": "npm run build -- --watch"
|
|
27
|
+
},
|
|
28
|
+
repository: {
|
|
29
|
+
type: "git",
|
|
30
|
+
url: "git+https://github.com/jspsych/jsPsych.git",
|
|
31
|
+
directory: "packages/plugin-canvas-keyboard-response"
|
|
32
|
+
},
|
|
33
|
+
author: "Chris Jungerius, Josh de Leeuw",
|
|
34
|
+
license: "MIT",
|
|
35
|
+
bugs: {
|
|
36
|
+
url: "https://github.com/jspsych/jsPsych/issues"
|
|
37
|
+
},
|
|
38
|
+
homepage: "https://www.jspsych.org/latest/plugins/canvas-keyboard-response",
|
|
39
|
+
peerDependencies: {
|
|
40
|
+
jspsych: ">=7.1.0"
|
|
41
|
+
},
|
|
42
|
+
devDependencies: {
|
|
43
|
+
"@jspsych/config": "^3.0.0",
|
|
44
|
+
"@jspsych/test-utils": "^1.2.0"
|
|
45
|
+
}
|
|
46
|
+
};
|
|
47
|
+
|
|
4
48
|
const info = {
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
type: jspsych.ParameterType.INT,
|
|
28
|
-
pretty_name: "Stimulus duration",
|
|
29
|
-
default: null,
|
|
30
|
-
},
|
|
31
|
-
/** How long to show trial before it ends. */
|
|
32
|
-
trial_duration: {
|
|
33
|
-
type: jspsych.ParameterType.INT,
|
|
34
|
-
pretty_name: "Trial duration",
|
|
35
|
-
default: null,
|
|
36
|
-
},
|
|
37
|
-
/** If true, trial will end when subject makes a response. */
|
|
38
|
-
response_ends_trial: {
|
|
39
|
-
type: jspsych.ParameterType.BOOL,
|
|
40
|
-
pretty_name: "Response ends trial",
|
|
41
|
-
default: true,
|
|
42
|
-
},
|
|
43
|
-
/** Array containing the height (first value) and width (second value) of the canvas element. */
|
|
44
|
-
canvas_size: {
|
|
45
|
-
type: jspsych.ParameterType.INT,
|
|
46
|
-
array: true,
|
|
47
|
-
pretty_name: "Canvas size",
|
|
48
|
-
default: [500, 500],
|
|
49
|
-
},
|
|
49
|
+
name: "canvas-keyboard-response",
|
|
50
|
+
version: _package.version,
|
|
51
|
+
parameters: {
|
|
52
|
+
stimulus: {
|
|
53
|
+
type: jspsych.ParameterType.FUNCTION,
|
|
54
|
+
default: void 0
|
|
55
|
+
},
|
|
56
|
+
choices: {
|
|
57
|
+
type: jspsych.ParameterType.KEYS,
|
|
58
|
+
default: "ALL_KEYS"
|
|
59
|
+
},
|
|
60
|
+
prompt: {
|
|
61
|
+
type: jspsych.ParameterType.HTML_STRING,
|
|
62
|
+
default: null
|
|
63
|
+
},
|
|
64
|
+
stimulus_duration: {
|
|
65
|
+
type: jspsych.ParameterType.INT,
|
|
66
|
+
default: null
|
|
67
|
+
},
|
|
68
|
+
trial_duration: {
|
|
69
|
+
type: jspsych.ParameterType.INT,
|
|
70
|
+
default: null
|
|
50
71
|
},
|
|
72
|
+
response_ends_trial: {
|
|
73
|
+
type: jspsych.ParameterType.BOOL,
|
|
74
|
+
default: true
|
|
75
|
+
},
|
|
76
|
+
canvas_size: {
|
|
77
|
+
type: jspsych.ParameterType.INT,
|
|
78
|
+
array: true,
|
|
79
|
+
default: [500, 500]
|
|
80
|
+
}
|
|
81
|
+
},
|
|
82
|
+
data: {
|
|
83
|
+
response: {
|
|
84
|
+
type: jspsych.ParameterType.STRING
|
|
85
|
+
},
|
|
86
|
+
rt: {
|
|
87
|
+
type: jspsych.ParameterType.INT
|
|
88
|
+
}
|
|
89
|
+
}
|
|
51
90
|
};
|
|
52
|
-
/**
|
|
53
|
-
* **canvas-keyboard-response**
|
|
54
|
-
*
|
|
55
|
-
* jsPsych plugin for displaying a canvas stimulus and getting a keyboard response
|
|
56
|
-
*
|
|
57
|
-
* @author Chris Jungerius (modified from Josh de Leeuw)
|
|
58
|
-
* @see {@link https://www.jspsych.org/plugins/jspsych-canvas-keyboard-response/ canvas-keyboard-response plugin documentation on jspsych.org}
|
|
59
|
-
*/
|
|
60
91
|
class CanvasKeyboardResponsePlugin {
|
|
61
|
-
|
|
62
|
-
|
|
92
|
+
constructor(jsPsych) {
|
|
93
|
+
this.jsPsych = jsPsych;
|
|
94
|
+
}
|
|
95
|
+
static info = info;
|
|
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
|
+
});
|
|
63
136
|
}
|
|
64
|
-
trial
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
'"></canvas>' +
|
|
71
|
-
"</div>";
|
|
72
|
-
// add prompt
|
|
73
|
-
if (trial.prompt !== null) {
|
|
74
|
-
new_html += trial.prompt;
|
|
75
|
-
}
|
|
76
|
-
// draw
|
|
77
|
-
display_element.innerHTML = new_html;
|
|
78
|
-
let c = document.getElementById("jspsych-canvas-stimulus");
|
|
79
|
-
trial.stimulus(c);
|
|
80
|
-
// store response
|
|
81
|
-
var response = {
|
|
82
|
-
rt: null,
|
|
83
|
-
key: null,
|
|
84
|
-
};
|
|
85
|
-
// function to end trial when it is time
|
|
86
|
-
const end_trial = () => {
|
|
87
|
-
// kill any remaining setTimeout handlers
|
|
88
|
-
this.jsPsych.pluginAPI.clearAllTimeouts();
|
|
89
|
-
// kill keyboard listeners
|
|
90
|
-
if (typeof keyboardListener !== "undefined") {
|
|
91
|
-
this.jsPsych.pluginAPI.cancelKeyboardResponse(keyboardListener);
|
|
92
|
-
}
|
|
93
|
-
// gather the data to store for the trial
|
|
94
|
-
var trial_data = {
|
|
95
|
-
rt: response.rt,
|
|
96
|
-
response: response.key,
|
|
97
|
-
};
|
|
98
|
-
// clear the display
|
|
99
|
-
display_element.innerHTML = "";
|
|
100
|
-
// move on to the next trial
|
|
101
|
-
this.jsPsych.finishTrial(trial_data);
|
|
102
|
-
};
|
|
103
|
-
// function to handle responses by the subject
|
|
104
|
-
var after_response = (info) => {
|
|
105
|
-
// after a valid response, the stimulus will have the CSS class 'responded'
|
|
106
|
-
// which can be used to provide visual feedback that a response was recorded
|
|
107
|
-
display_element.querySelector("#jspsych-canvas-keyboard-response-stimulus").className +=
|
|
108
|
-
" responded";
|
|
109
|
-
// only record the first response
|
|
110
|
-
if (response.key == null) {
|
|
111
|
-
response = info;
|
|
112
|
-
}
|
|
113
|
-
if (trial.response_ends_trial) {
|
|
114
|
-
end_trial();
|
|
115
|
-
}
|
|
116
|
-
};
|
|
117
|
-
// start the response listener
|
|
118
|
-
if (trial.choices != "NO_KEYS") {
|
|
119
|
-
var keyboardListener = this.jsPsych.pluginAPI.getKeyboardResponse({
|
|
120
|
-
callback_function: after_response,
|
|
121
|
-
valid_responses: trial.choices,
|
|
122
|
-
rt_method: "performance",
|
|
123
|
-
persist: false,
|
|
124
|
-
allow_held_key: false,
|
|
125
|
-
});
|
|
126
|
-
}
|
|
127
|
-
// hide stimulus if stimulus_duration is set
|
|
128
|
-
if (trial.stimulus_duration !== null) {
|
|
129
|
-
this.jsPsych.pluginAPI.setTimeout(() => {
|
|
130
|
-
display_element.querySelector("#jspsych-canvas-keyboard-response-stimulus").style.visibility = "hidden";
|
|
131
|
-
}, trial.stimulus_duration);
|
|
132
|
-
}
|
|
133
|
-
// end trial if trial_duration is set
|
|
134
|
-
if (trial.trial_duration !== null) {
|
|
135
|
-
this.jsPsych.pluginAPI.setTimeout(() => {
|
|
136
|
-
end_trial();
|
|
137
|
-
}, trial.trial_duration);
|
|
138
|
-
}
|
|
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);
|
|
139
143
|
}
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
}
|
|
145
|
-
if (simulation_mode == "visual") {
|
|
146
|
-
this.simulate_visual(trial, simulation_options, load_callback);
|
|
147
|
-
}
|
|
144
|
+
if (trial.trial_duration !== null) {
|
|
145
|
+
this.jsPsych.pluginAPI.setTimeout(() => {
|
|
146
|
+
end_trial();
|
|
147
|
+
}, trial.trial_duration);
|
|
148
148
|
}
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
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);
|
|
152
154
|
}
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
const display_element = this.jsPsych.getDisplayElement();
|
|
156
|
-
this.trial(display_element, trial);
|
|
157
|
-
load_callback();
|
|
158
|
-
if (data.rt !== null) {
|
|
159
|
-
this.jsPsych.pluginAPI.pressKey(data.response, data.rt);
|
|
160
|
-
}
|
|
155
|
+
if (simulation_mode == "visual") {
|
|
156
|
+
this.simulate_visual(trial, simulation_options, load_callback);
|
|
161
157
|
}
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
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);
|
|
170
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
|
+
}
|
|
171
181
|
}
|
|
172
|
-
CanvasKeyboardResponsePlugin.info = info;
|
|
173
182
|
|
|
174
183
|
return CanvasKeyboardResponsePlugin;
|
|
175
184
|
|
|
176
185
|
})(jsPsychModule);
|
|
177
|
-
//# sourceMappingURL=https://unpkg.com/@jspsych/plugin-canvas-keyboard-response@
|
|
186
|
+
//# sourceMappingURL=https://unpkg.com/@jspsych/plugin-canvas-keyboard-response@2.0.0/dist/index.browser.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.browser.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":["ParameterType"],"mappings":";;;EAEA,MAAM,IAAI,GAAU;EAClB,IAAA,IAAI,EAAE,0BAA0B;EAChC,IAAA,UAAU,EAAE;;EAEV,QAAA,QAAQ,EAAE;cACR,IAAI,EAAEA,qBAAa,CAAC,QAAQ;EAC5B,YAAA,WAAW,EAAE,UAAU;EACvB,YAAA,OAAO,EAAE,SAAS;EACnB,SAAA;;EAED,QAAA,OAAO,EAAE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AI,CAAA,IAAA,GAAG,IAAI;;;;;;;;"}
|
|
1
|
+
{"version":3,"file":"index.browser.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","ParameterType","info"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;EAIA,MAAM,IAAc,GAAA;EAAA,EAClB,IAAM,EAAA,0BAAA;EAAA,WACNA,gBAAA;EAAA,EACA,UAAY,EAAA;EAAA,IAMV,QAAU,EAAA;EAAA,MACR,MAAMC,qBAAc,CAAA,QAAA;EAAA,MACpB,OAAS,EAAA,KAAA,CAAA;EAAA,KACX;EAAA,IASA,OAAS,EAAA;EAAA,MACP,MAAMA,qBAAc,CAAA,IAAA;EAAA,MACpB,OAAS,EAAA,UAAA;EAAA,KACX;EAAA,IAIA,MAAQ,EAAA;EAAA,MACN,MAAMA,qBAAc,CAAA,WAAA;EAAA,MACpB,OAAS,EAAA,IAAA;EAAA,KACX;EAAA,IAIA,iBAAmB,EAAA;EAAA,MACjB,MAAMA,qBAAc,CAAA,GAAA;EAAA,MACpB,OAAS,EAAA,IAAA;EAAA,KACX;EAAA,IAMA,cAAgB,EAAA;EAAA,MACd,MAAMA,qBAAc,CAAA,GAAA;EAAA,MACpB,OAAS,EAAA,IAAA;EAAA,KACX;EAAA,IAMA,mBAAqB,EAAA;EAAA,MACnB,MAAMA,qBAAc,CAAA,IAAA;EAAA,MACpB,OAAS,E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,QAAA,EAAU,KAAK,EAAE,CAAA,CAAA;EAAA,KACxD;EAAA,GACF;EAAA,EAEQ,sBAAA,CAAuB,OAAwB,kBAAoB,EAAA;EACzE,IAAA,MAAM,YAAe,GAAA;EAAA,MACnB,EAAA,EAAI,KAAK,OAAQ,CAAA,aAAA,CAAc,iBAAiB,GAAK,EAAA,EAAA,EAAI,CAAI,GAAA,GAAA,EAAK,IAAI,CAAA;EAAA,MACtE,UAAU,IAAK,CAAA,OAAA,CAAQ,SAAU,CAAA,WAAA,CAAY,MAAM,OAAO,CAAA;EAAA,KAC5D,CAAA;EAEA,IAAA,MAAM,OAAO,IAAK,CAAA,OAAA,CAAQ,SAAU,CAAA,mBAAA,CAAoB,cAAc,kBAAkB,CAAA,CAAA;EAExF,IAAA,IAAA,CAAK,OAAQ,CAAA,SAAA,CAAU,+BAAgC,CAAA,KAAA,EAAO,IAAI,CAAA,CAAA;EAElE,IAAO,OAAA,IAAA,CAAA;EAAA,GACT;EACF;;;;;;;;"}
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
var jsPsychCanvasKeyboardResponse=function(
|
|
2
|
-
//# sourceMappingURL=https://unpkg.com/@jspsych/plugin-canvas-keyboard-response@
|
|
1
|
+
var jsPsychCanvasKeyboardResponse=function(i){"use strict";var c={name:"@jspsych/plugin-canvas-keyboard-response",version:"2.0.0",description:"jsPsych plugin for displaying a canvas stimulus and getting a keyboard response",type:"module",main:"dist/index.cjs",exports:{import:"./dist/index.js",require:"./dist/index.cjs"},typings:"dist/index.d.ts",unpkg:"dist/index.browser.min.js",files:["src","dist"],source:"src/index.ts",scripts:{test:"jest --passWithNoTests","test:watch":"npm test -- --watch",tsc:"tsc",build:"rollup --config","build:watch":"npm run build -- --watch"},repository:{type:"git",url:"git+https://github.com/jspsych/jsPsych.git",directory:"packages/plugin-canvas-keyboard-response"},author:"Chris Jungerius, Josh de Leeuw",license:"MIT",bugs:{url:"https://github.com/jspsych/jsPsych/issues"},homepage:"https://www.jspsych.org/latest/plugins/canvas-keyboard-response",peerDependencies:{jspsych:">=7.1.0"},devDependencies:{"@jspsych/config":"^3.0.0","@jspsych/test-utils":"^1.2.0"}};const p={name:"canvas-keyboard-response",version:c.version,parameters:{stimulus:{type:i.ParameterType.FUNCTION,default:void 0},choices:{type:i.ParameterType.KEYS,default:"ALL_KEYS"},prompt:{type:i.ParameterType.HTML_STRING,default:null},stimulus_duration:{type:i.ParameterType.INT,default:null},trial_duration:{type:i.ParameterType.INT,default:null},response_ends_trial:{type:i.ParameterType.BOOL,default:!0},canvas_size:{type:i.ParameterType.INT,array:!0,default:[500,500]}},data:{response:{type:i.ParameterType.STRING},rt:{type:i.ParameterType.INT}}};class l{constructor(e){this.jsPsych=e}trial(e,s){var a='<div id="jspsych-canvas-keyboard-response-stimulus"><canvas id="jspsych-canvas-stimulus" height="'+s.canvas_size[0]+'" width="'+s.canvas_size[1]+'"></canvas></div>';s.prompt!==null&&(a+=s.prompt),e.innerHTML=a;let t=document.getElementById("jspsych-canvas-stimulus");t.style.display="block",s.stimulus(t);var n={rt:null,key:null};const u=()=>{typeof o!="undefined"&&this.jsPsych.pluginAPI.cancelKeyboardResponse(o);var r={rt:n.rt,response:n.key};this.jsPsych.finishTrial(r)};var d=r=>{e.querySelector("#jspsych-canvas-keyboard-response-stimulus").className+=" responded",n.key==null&&(n=r),s.response_ends_trial&&u()};if(s.choices!="NO_KEYS")var o=this.jsPsych.pluginAPI.getKeyboardResponse({callback_function:d,valid_responses:s.choices,rt_method:"performance",persist:!1,allow_held_key:!1});s.stimulus_duration!==null&&this.jsPsych.pluginAPI.setTimeout(()=>{e.querySelector("#jspsych-canvas-keyboard-response-stimulus").style.visibility="hidden"},s.stimulus_duration),s.trial_duration!==null&&this.jsPsych.pluginAPI.setTimeout(()=>{u()},s.trial_duration)}simulate(e,s,a,t){s=="data-only"&&(t(),this.simulate_data_only(e,a)),s=="visual"&&this.simulate_visual(e,a,t)}simulate_data_only(e,s){const a=this.create_simulation_data(e,s);this.jsPsych.finishTrial(a)}simulate_visual(e,s,a){const t=this.create_simulation_data(e,s),n=this.jsPsych.getDisplayElement();this.trial(n,e),a(),t.rt!==null&&this.jsPsych.pluginAPI.pressKey(t.response,t.rt)}create_simulation_data(e,s){const a={rt:this.jsPsych.randomization.sampleExGaussian(500,50,.006666666666666667,!0),response:this.jsPsych.pluginAPI.getValidKey(e.choices)},t=this.jsPsych.pluginAPI.mergeSimulationData(a,s);return this.jsPsych.pluginAPI.ensureSimulationDataConsistency(e,t),t}}return l.info=p,l}(jsPsychModule);
|
|
2
|
+
//# sourceMappingURL=https://unpkg.com/@jspsych/plugin-canvas-keyboard-response@2.0.0/dist/index.browser.min.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.browser.min.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":["info","name","parameters","stimulus","type","ParameterType","FUNCTION","pretty_name","default","undefined","choices","KEYS","prompt","HTML_STRING","stimulus_duration","INT","trial_duration","response_ends_trial","BOOL","canvas_size","array","CanvasKeyboardResponsePlugin","jsPsych","_classCallCheck","this","key","value","display_element","trial","_this","new_html","innerHTML","c","document","getElementById","response","rt","end_trial","pluginAPI","clearAllTimeouts","keyboardListener","cancelKeyboardResponse","trial_data","finishTrial","getKeyboardResponse","callback_function","querySelector","className","valid_responses","rt_method","persist","allow_held_key","setTimeout","style","visibility","simulation_mode","simulation_options","load_callback","simulate_data_only","simulate_visual","data","create_simulation_data","getDisplayElement","pressKey","default_data","randomization","sampleExGaussian","getValidKey","mergeSimulationData","ensureSimulationDataConsistency"],"mappings":"0iBAEA,IAAMA,EAAc,CAClBC,KAAM,2BACNC,WAAY,CAEVC,SAAU,CACRC,KAAMC,EAAaA,cAACC,SACpBC,YAAa,WACbC,aAASC,GAGXC,QAAS,CACPN,KAAMC,EAAaA,cAACM,KACpBJ,YAAa,UACbC,QAAS,YAGXI,OAAQ,CACNR,KAAMC,EAAaA,cAACQ,YACpBN,YAAa,SACbC,QAAS,MAGXM,kBAAmB,CACjBV,KAAMC,EAAaA,cAACU,IACpBR,YAAa,oBACbC,QAAS,MAGXQ,eAAgB,CACdZ,KAAMC,EAAaA,cAACU,IACpBR,YAAa,iBACbC,QAAS,MAGXS,oBAAqB,CACnBb,KAAMC,EAAaA,cAACa,KACpBX,YAAa,sBACbC,SAAS,GAGXW,YAAa,CACXf,KAAMC,EAAaA,cAACU,IACpBK,OAAO,EACPb,YAAa,cACbC,QAAS,CAAC,IAAK,QAefa,EAA4B,WAGhC,SAAAA,EAAoBC,gGAAgBC,MAAAF,GAAhBG,KAAOF,QAAPA,CAAmB,WA2ItC,SA3IuCD,IAAA,CAAA,CAAAI,IAAA,QAAAC,MAExC,SAAMC,EAA8BC,GAAsB,IAAAC,EAAAL,KACpDM,EACF,oGAEAF,EAAMT,YAAY,GAClB,YACAS,EAAMT,YAAY,GAJlB,oBAQmB,OAAjBS,EAAMhB,SACRkB,GAAYF,EAAMhB,QAIpBe,EAAgBI,UAAYD,EA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jC,EAAO4B,GAE1C7B,EAAkBH,KAAKF,QAAQwC,oBAErCtC,KAAKI,MAAMD,EAAiBC,GAC5B6B,IAEgB,OAAZG,EAAKxB,IACPZ,KAAKF,QAAQgB,UAAUyB,SAASH,EAAKzB,SAAUyB,EAAKxB,GAExD,GAAC,CAAAX,IAAA,yBAAAC,MAEO,SAAuBE,EAAwB4B,GACrD,IAAMQ,EAAe,CACnB5B,GAAIZ,KAAKF,QAAQ2C,cAAcC,iBAAiB,IAAK,GAAI,EAAI,KAAK,GAClE/B,SAAUX,KAAKF,QAAQgB,UAAU6B,YAAYvC,EAAMlB,UAG/CkD,EAAOpC,KAAKF,QAAQgB,UAAU8B,oBAAoBJ,EAAcR,GAItE,OAFAhC,KAAKF,QAAQgB,UAAU+B,gCAAgCzC,EAAOgC,GAEvDA,CACT,qFAACvC,CAAA,CA9I+B,UACzBA,EAAIrB,KAAGA"}
|
|
1
|
+
{"version":3,"file":"index.browser.min.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":["info","version","ParameterType","CanvasKeyboardResponsePlugin","jsPsych","display_element","trial","new_html","c","response","end_trial","keyboardListener","trial_data","after_response","simulation_mode","simulation_options","load_callback","data","default_data"],"mappings":"w+BAIA,MAAMA,EAAc,CAClB,KAAM,2BACN,QAASC,EAAAA,QACT,WAAY,CAMV,SAAU,CACR,KAAMC,EAAAA,cAAc,SACpB,QAAS,MACX,EASA,QAAS,CACP,KA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