@jspsych/plugin-image-keyboard-response 1.1.2 → 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/README.md +8 -2
- package/dist/index.browser.js +269 -278
- 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 +267 -276
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +200 -156
- package/dist/index.js +267 -276
- package/dist/index.js.map +1 -1
- package/package.json +3 -3
- package/src/index.spec.ts +5 -5
- package/src/index.ts +49 -30
package/README.md
CHANGED
|
@@ -27,9 +27,15 @@ See the [contributing to jsPsych](https://www.jspsych.org/latest/developers/cont
|
|
|
27
27
|
|
|
28
28
|
## Citation
|
|
29
29
|
|
|
30
|
-
If you use this library in academic work,
|
|
30
|
+
If you use this library in academic work, the preferred citation is:
|
|
31
31
|
|
|
32
|
-
de Leeuw, J.R. (
|
|
32
|
+
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://joss.theoj.org/papers/10.21105/joss.05351](https://joss.theoj.org/papers/10.21105/joss.05351).
|
|
33
|
+
|
|
34
|
+
This paper is an updated description of jsPsych and includes all current core team members. It replaces the earlier paper that described jsPsych:
|
|
35
|
+
|
|
36
|
+
de Leeuw, J.R. (2015). jsPsych: A JavaScript library for creating behavioral experiments in a Web browser. *Behavior Research Methods*, _47_(1), 1-12. doi:[10.3758/s13428-014-0458-y](http://link.springer.com/article/10.3758%2Fs13428-014-0458-y)
|
|
37
|
+
|
|
38
|
+
Citations help us demonstrate that this library is used and valued, which allows us to continue working on it.
|
|
33
39
|
|
|
34
40
|
## Contributors
|
|
35
41
|
|
package/dist/index.browser.js
CHANGED
|
@@ -1,285 +1,276 @@
|
|
|
1
1
|
var jsPsychImageKeyboardResponse = (function (jspsych) {
|
|
2
2
|
'use strict';
|
|
3
3
|
|
|
4
|
-
|
|
5
|
-
|
|
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
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
}
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
}
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
}
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
}
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
rt: this.jsPsych.randomization.sampleExGaussian(500, 50, 1 / 150, true),
|
|
273
|
-
response: this.jsPsych.pluginAPI.getValidKey(trial.choices),
|
|
274
|
-
};
|
|
275
|
-
const data = this.jsPsych.pluginAPI.mergeSimulationData(default_data, simulation_options);
|
|
276
|
-
this.jsPsych.pluginAPI.ensureSimulationDataConsistency(trial, data);
|
|
277
|
-
return data;
|
|
278
|
-
}
|
|
279
|
-
}
|
|
280
|
-
ImageKeyboardResponsePlugin.info = info;
|
|
4
|
+
var _package = {
|
|
5
|
+
name: "@jspsych/plugin-image-keyboard-response",
|
|
6
|
+
version: "2.0.0",
|
|
7
|
+
description: "jsPsych plugin for displaying a 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",
|
|
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-image-keyboard-response"
|
|
32
|
+
},
|
|
33
|
+
author: "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/image-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
|
+
|
|
48
|
+
const info = {
|
|
49
|
+
name: "image-keyboard-response",
|
|
50
|
+
version: _package.version,
|
|
51
|
+
parameters: {
|
|
52
|
+
stimulus: {
|
|
53
|
+
type: jspsych.ParameterType.IMAGE,
|
|
54
|
+
default: void 0
|
|
55
|
+
},
|
|
56
|
+
stimulus_height: {
|
|
57
|
+
type: jspsych.ParameterType.INT,
|
|
58
|
+
default: null
|
|
59
|
+
},
|
|
60
|
+
stimulus_width: {
|
|
61
|
+
type: jspsych.ParameterType.INT,
|
|
62
|
+
default: null
|
|
63
|
+
},
|
|
64
|
+
maintain_aspect_ratio: {
|
|
65
|
+
type: jspsych.ParameterType.BOOL,
|
|
66
|
+
default: true
|
|
67
|
+
},
|
|
68
|
+
choices: {
|
|
69
|
+
type: jspsych.ParameterType.KEYS,
|
|
70
|
+
default: "ALL_KEYS"
|
|
71
|
+
},
|
|
72
|
+
prompt: {
|
|
73
|
+
type: jspsych.ParameterType.HTML_STRING,
|
|
74
|
+
default: null
|
|
75
|
+
},
|
|
76
|
+
stimulus_duration: {
|
|
77
|
+
type: jspsych.ParameterType.INT,
|
|
78
|
+
default: null
|
|
79
|
+
},
|
|
80
|
+
trial_duration: {
|
|
81
|
+
type: jspsych.ParameterType.INT,
|
|
82
|
+
default: null
|
|
83
|
+
},
|
|
84
|
+
response_ends_trial: {
|
|
85
|
+
type: jspsych.ParameterType.BOOL,
|
|
86
|
+
default: true
|
|
87
|
+
},
|
|
88
|
+
render_on_canvas: {
|
|
89
|
+
type: jspsych.ParameterType.BOOL,
|
|
90
|
+
default: true
|
|
91
|
+
}
|
|
92
|
+
},
|
|
93
|
+
data: {
|
|
94
|
+
stimulus: {
|
|
95
|
+
type: jspsych.ParameterType.STRING
|
|
96
|
+
},
|
|
97
|
+
response: {
|
|
98
|
+
type: jspsych.ParameterType.STRING
|
|
99
|
+
},
|
|
100
|
+
rt: {
|
|
101
|
+
type: jspsych.ParameterType.INT
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
};
|
|
105
|
+
class ImageKeyboardResponsePlugin {
|
|
106
|
+
constructor(jsPsych) {
|
|
107
|
+
this.jsPsych = jsPsych;
|
|
108
|
+
}
|
|
109
|
+
static info = info;
|
|
110
|
+
trial(display_element, trial) {
|
|
111
|
+
var height, width;
|
|
112
|
+
if (trial.render_on_canvas) {
|
|
113
|
+
var image_drawn = false;
|
|
114
|
+
if (display_element.hasChildNodes()) {
|
|
115
|
+
while (display_element.firstChild) {
|
|
116
|
+
display_element.removeChild(display_element.firstChild);
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
var canvas = document.createElement("canvas");
|
|
120
|
+
canvas.id = "jspsych-image-keyboard-response-stimulus";
|
|
121
|
+
canvas.style.margin = "0";
|
|
122
|
+
canvas.style.padding = "0";
|
|
123
|
+
var ctx = canvas.getContext("2d");
|
|
124
|
+
var img = new Image();
|
|
125
|
+
img.onload = () => {
|
|
126
|
+
if (!image_drawn) {
|
|
127
|
+
getHeightWidth();
|
|
128
|
+
ctx.drawImage(img, 0, 0, width, height);
|
|
129
|
+
}
|
|
130
|
+
};
|
|
131
|
+
img.src = trial.stimulus;
|
|
132
|
+
const getHeightWidth = () => {
|
|
133
|
+
if (trial.stimulus_height !== null) {
|
|
134
|
+
height = trial.stimulus_height;
|
|
135
|
+
if (trial.stimulus_width == null && trial.maintain_aspect_ratio) {
|
|
136
|
+
width = img.naturalWidth * (trial.stimulus_height / img.naturalHeight);
|
|
137
|
+
}
|
|
138
|
+
} else {
|
|
139
|
+
height = img.naturalHeight;
|
|
140
|
+
}
|
|
141
|
+
if (trial.stimulus_width !== null) {
|
|
142
|
+
width = trial.stimulus_width;
|
|
143
|
+
if (trial.stimulus_height == null && trial.maintain_aspect_ratio) {
|
|
144
|
+
height = img.naturalHeight * (trial.stimulus_width / img.naturalWidth);
|
|
145
|
+
}
|
|
146
|
+
} else if (!(trial.stimulus_height !== null && trial.maintain_aspect_ratio)) {
|
|
147
|
+
width = img.naturalWidth;
|
|
148
|
+
}
|
|
149
|
+
canvas.height = height;
|
|
150
|
+
canvas.width = width;
|
|
151
|
+
};
|
|
152
|
+
getHeightWidth();
|
|
153
|
+
display_element.insertBefore(canvas, null);
|
|
154
|
+
if (img.complete && Number.isFinite(width) && Number.isFinite(height)) {
|
|
155
|
+
ctx.drawImage(img, 0, 0, width, height);
|
|
156
|
+
image_drawn = true;
|
|
157
|
+
}
|
|
158
|
+
if (trial.prompt !== null) {
|
|
159
|
+
display_element.insertAdjacentHTML("beforeend", trial.prompt);
|
|
160
|
+
}
|
|
161
|
+
} else {
|
|
162
|
+
var html = '<img src="' + trial.stimulus + '" id="jspsych-image-keyboard-response-stimulus">';
|
|
163
|
+
if (trial.prompt !== null) {
|
|
164
|
+
html += trial.prompt;
|
|
165
|
+
}
|
|
166
|
+
display_element.innerHTML = html;
|
|
167
|
+
var img = display_element.querySelector(
|
|
168
|
+
"#jspsych-image-keyboard-response-stimulus"
|
|
169
|
+
);
|
|
170
|
+
if (trial.stimulus_height !== null) {
|
|
171
|
+
height = trial.stimulus_height;
|
|
172
|
+
if (trial.stimulus_width == null && trial.maintain_aspect_ratio) {
|
|
173
|
+
width = img.naturalWidth * (trial.stimulus_height / img.naturalHeight);
|
|
174
|
+
}
|
|
175
|
+
} else {
|
|
176
|
+
height = img.naturalHeight;
|
|
177
|
+
}
|
|
178
|
+
if (trial.stimulus_width !== null) {
|
|
179
|
+
width = trial.stimulus_width;
|
|
180
|
+
if (trial.stimulus_height == null && trial.maintain_aspect_ratio) {
|
|
181
|
+
height = img.naturalHeight * (trial.stimulus_width / img.naturalWidth);
|
|
182
|
+
}
|
|
183
|
+
} else if (!(trial.stimulus_height !== null && trial.maintain_aspect_ratio)) {
|
|
184
|
+
width = img.naturalWidth;
|
|
185
|
+
}
|
|
186
|
+
img.style.height = height.toString() + "px";
|
|
187
|
+
img.style.width = width.toString() + "px";
|
|
188
|
+
}
|
|
189
|
+
var response = {
|
|
190
|
+
rt: null,
|
|
191
|
+
key: null
|
|
192
|
+
};
|
|
193
|
+
const end_trial = () => {
|
|
194
|
+
if (typeof keyboardListener !== "undefined") {
|
|
195
|
+
this.jsPsych.pluginAPI.cancelKeyboardResponse(keyboardListener);
|
|
196
|
+
}
|
|
197
|
+
var trial_data = {
|
|
198
|
+
rt: response.rt,
|
|
199
|
+
stimulus: trial.stimulus,
|
|
200
|
+
response: response.key
|
|
201
|
+
};
|
|
202
|
+
this.jsPsych.finishTrial(trial_data);
|
|
203
|
+
};
|
|
204
|
+
var after_response = (info2) => {
|
|
205
|
+
display_element.querySelector("#jspsych-image-keyboard-response-stimulus").className += " responded";
|
|
206
|
+
if (response.key == null) {
|
|
207
|
+
response = info2;
|
|
208
|
+
}
|
|
209
|
+
if (trial.response_ends_trial) {
|
|
210
|
+
end_trial();
|
|
211
|
+
}
|
|
212
|
+
};
|
|
213
|
+
if (trial.choices != "NO_KEYS") {
|
|
214
|
+
var keyboardListener = this.jsPsych.pluginAPI.getKeyboardResponse({
|
|
215
|
+
callback_function: after_response,
|
|
216
|
+
valid_responses: trial.choices,
|
|
217
|
+
rt_method: "performance",
|
|
218
|
+
persist: false,
|
|
219
|
+
allow_held_key: false
|
|
220
|
+
});
|
|
221
|
+
}
|
|
222
|
+
if (trial.stimulus_duration !== null) {
|
|
223
|
+
this.jsPsych.pluginAPI.setTimeout(() => {
|
|
224
|
+
display_element.querySelector(
|
|
225
|
+
"#jspsych-image-keyboard-response-stimulus"
|
|
226
|
+
).style.visibility = "hidden";
|
|
227
|
+
}, trial.stimulus_duration);
|
|
228
|
+
}
|
|
229
|
+
if (trial.trial_duration !== null) {
|
|
230
|
+
this.jsPsych.pluginAPI.setTimeout(() => {
|
|
231
|
+
end_trial();
|
|
232
|
+
}, trial.trial_duration);
|
|
233
|
+
} else if (trial.response_ends_trial === false) {
|
|
234
|
+
console.warn(
|
|
235
|
+
"The experiment may be deadlocked. Try setting a trial duration or set response_ends_trial to true."
|
|
236
|
+
);
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
simulate(trial, simulation_mode, simulation_options, load_callback) {
|
|
240
|
+
if (simulation_mode == "data-only") {
|
|
241
|
+
load_callback();
|
|
242
|
+
this.simulate_data_only(trial, simulation_options);
|
|
243
|
+
}
|
|
244
|
+
if (simulation_mode == "visual") {
|
|
245
|
+
this.simulate_visual(trial, simulation_options, load_callback);
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
simulate_data_only(trial, simulation_options) {
|
|
249
|
+
const data = this.create_simulation_data(trial, simulation_options);
|
|
250
|
+
this.jsPsych.finishTrial(data);
|
|
251
|
+
}
|
|
252
|
+
simulate_visual(trial, simulation_options, load_callback) {
|
|
253
|
+
const data = this.create_simulation_data(trial, simulation_options);
|
|
254
|
+
const display_element = this.jsPsych.getDisplayElement();
|
|
255
|
+
this.trial(display_element, trial);
|
|
256
|
+
load_callback();
|
|
257
|
+
if (data.rt !== null) {
|
|
258
|
+
this.jsPsych.pluginAPI.pressKey(data.response, data.rt);
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
create_simulation_data(trial, simulation_options) {
|
|
262
|
+
const default_data = {
|
|
263
|
+
stimulus: trial.stimulus,
|
|
264
|
+
rt: this.jsPsych.randomization.sampleExGaussian(500, 50, 1 / 150, true),
|
|
265
|
+
response: this.jsPsych.pluginAPI.getValidKey(trial.choices)
|
|
266
|
+
};
|
|
267
|
+
const data = this.jsPsych.pluginAPI.mergeSimulationData(default_data, simulation_options);
|
|
268
|
+
this.jsPsych.pluginAPI.ensureSimulationDataConsistency(trial, data);
|
|
269
|
+
return data;
|
|
270
|
+
}
|
|
271
|
+
}
|
|
281
272
|
|
|
282
273
|
return ImageKeyboardResponsePlugin;
|
|
283
274
|
|
|
284
275
|
})(jsPsychModule);
|
|
285
|
-
//# sourceMappingURL=index.browser.js.map
|
|
276
|
+
//# sourceMappingURL=https://unpkg.com/@jspsych/plugin-image-keyboard-response@2.0.0/dist/index.browser.js.map
|