@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/dist/index.cjs
CHANGED
|
@@ -2,282 +2,273 @@
|
|
|
2
2
|
|
|
3
3
|
var jspsych = require('jspsych');
|
|
4
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
|
-
stimulus: trial.stimulus,
|
|
273
|
-
rt: this.jsPsych.randomization.sampleExGaussian(500, 50, 1 / 150, true),
|
|
274
|
-
response: this.jsPsych.pluginAPI.getValidKey(trial.choices),
|
|
275
|
-
};
|
|
276
|
-
const data = this.jsPsych.pluginAPI.mergeSimulationData(default_data, simulation_options);
|
|
277
|
-
this.jsPsych.pluginAPI.ensureSimulationDataConsistency(trial, data);
|
|
278
|
-
return data;
|
|
279
|
-
}
|
|
280
|
-
}
|
|
5
|
+
var _package = {
|
|
6
|
+
name: "@jspsych/plugin-image-keyboard-response",
|
|
7
|
+
version: "2.0.0",
|
|
8
|
+
description: "jsPsych plugin for displaying a stimulus and getting a keyboard response",
|
|
9
|
+
type: "module",
|
|
10
|
+
main: "dist/index.cjs",
|
|
11
|
+
exports: {
|
|
12
|
+
import: "./dist/index.js",
|
|
13
|
+
require: "./dist/index.cjs"
|
|
14
|
+
},
|
|
15
|
+
typings: "dist/index.d.ts",
|
|
16
|
+
unpkg: "dist/index.browser.min.js",
|
|
17
|
+
files: [
|
|
18
|
+
"src",
|
|
19
|
+
"dist"
|
|
20
|
+
],
|
|
21
|
+
source: "src/index.ts",
|
|
22
|
+
scripts: {
|
|
23
|
+
test: "jest",
|
|
24
|
+
"test:watch": "npm test -- --watch",
|
|
25
|
+
tsc: "tsc",
|
|
26
|
+
build: "rollup --config",
|
|
27
|
+
"build:watch": "npm run build -- --watch"
|
|
28
|
+
},
|
|
29
|
+
repository: {
|
|
30
|
+
type: "git",
|
|
31
|
+
url: "git+https://github.com/jspsych/jsPsych.git",
|
|
32
|
+
directory: "packages/plugin-image-keyboard-response"
|
|
33
|
+
},
|
|
34
|
+
author: "Josh de Leeuw",
|
|
35
|
+
license: "MIT",
|
|
36
|
+
bugs: {
|
|
37
|
+
url: "https://github.com/jspsych/jsPsych/issues"
|
|
38
|
+
},
|
|
39
|
+
homepage: "https://www.jspsych.org/latest/plugins/image-keyboard-response",
|
|
40
|
+
peerDependencies: {
|
|
41
|
+
jspsych: ">=7.1.0"
|
|
42
|
+
},
|
|
43
|
+
devDependencies: {
|
|
44
|
+
"@jspsych/config": "^3.0.0",
|
|
45
|
+
"@jspsych/test-utils": "^1.2.0"
|
|
46
|
+
}
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
const info = {
|
|
50
|
+
name: "image-keyboard-response",
|
|
51
|
+
version: _package.version,
|
|
52
|
+
parameters: {
|
|
53
|
+
stimulus: {
|
|
54
|
+
type: jspsych.ParameterType.IMAGE,
|
|
55
|
+
default: void 0
|
|
56
|
+
},
|
|
57
|
+
stimulus_height: {
|
|
58
|
+
type: jspsych.ParameterType.INT,
|
|
59
|
+
default: null
|
|
60
|
+
},
|
|
61
|
+
stimulus_width: {
|
|
62
|
+
type: jspsych.ParameterType.INT,
|
|
63
|
+
default: null
|
|
64
|
+
},
|
|
65
|
+
maintain_aspect_ratio: {
|
|
66
|
+
type: jspsych.ParameterType.BOOL,
|
|
67
|
+
default: true
|
|
68
|
+
},
|
|
69
|
+
choices: {
|
|
70
|
+
type: jspsych.ParameterType.KEYS,
|
|
71
|
+
default: "ALL_KEYS"
|
|
72
|
+
},
|
|
73
|
+
prompt: {
|
|
74
|
+
type: jspsych.ParameterType.HTML_STRING,
|
|
75
|
+
default: null
|
|
76
|
+
},
|
|
77
|
+
stimulus_duration: {
|
|
78
|
+
type: jspsych.ParameterType.INT,
|
|
79
|
+
default: null
|
|
80
|
+
},
|
|
81
|
+
trial_duration: {
|
|
82
|
+
type: jspsych.ParameterType.INT,
|
|
83
|
+
default: null
|
|
84
|
+
},
|
|
85
|
+
response_ends_trial: {
|
|
86
|
+
type: jspsych.ParameterType.BOOL,
|
|
87
|
+
default: true
|
|
88
|
+
},
|
|
89
|
+
render_on_canvas: {
|
|
90
|
+
type: jspsych.ParameterType.BOOL,
|
|
91
|
+
default: true
|
|
92
|
+
}
|
|
93
|
+
},
|
|
94
|
+
data: {
|
|
95
|
+
stimulus: {
|
|
96
|
+
type: jspsych.ParameterType.STRING
|
|
97
|
+
},
|
|
98
|
+
response: {
|
|
99
|
+
type: jspsych.ParameterType.STRING
|
|
100
|
+
},
|
|
101
|
+
rt: {
|
|
102
|
+
type: jspsych.ParameterType.INT
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
};
|
|
106
|
+
class ImageKeyboardResponsePlugin {
|
|
107
|
+
constructor(jsPsych) {
|
|
108
|
+
this.jsPsych = jsPsych;
|
|
109
|
+
}
|
|
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
|
ImageKeyboardResponsePlugin.info = info;
|
|
282
273
|
|
|
283
274
|
module.exports = ImageKeyboardResponsePlugin;
|