-
Notifications
You must be signed in to change notification settings - Fork 0
/
plugin-p5js.js
286 lines (249 loc) · 9.71 KB
/
plugin-p5js.js
1
2
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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
// p5.js integration
var jsPsychP5JS = (function (jspsych) {
'use strict';
const info = {
name: "p5js",
parameters: {
/** The p5 js setup func */
setup_func: {
type: jspsych.ParameterType.FUNCTION,
pretty_name: "Setup",
default: undefined,
},
/** The p5 js draw func */
draw_func: {
type: jspsych.ParameterType.FUNCTION,
pretty_name: "Draw",
default: undefined,
},
/** The p5 js top level (same as setup and draw) variables and functions */
top_level_declarations:{
type: jspsych.ParameterType.FUNCTION,
pretty_name: "Declare top level functions and variables",
default: function (p) {},
},
/** Array containing the label(s) for the button(s). */
button_choices: {
type: jspsych.ParameterType.STRING,
pretty_name: "Button Choices",
default: [],
array: true,
},
/** Array containing the key(s) the subject is allowed to press to respond to the stimulus. */
key_choices: {
type: jspsych.ParameterType.KEYS,
pretty_name: "Key Choices",
default: "ALL_KEYS", // alternatively set to "NO_KEYS" if key presses are not allowed
},
/** The html of the button. Can create own style. */
button_html: {
type: jspsych.ParameterType.HTML_STRING,
pretty_name: "Button HTML",
default: '<button class="jspsych-btn">%choice%</button>',
array: true,
},
/** Any content here will be displayed under the button. */
prompt: {
type: jspsych.ParameterType.HTML_STRING,
pretty_name: "Prompt",
default: null,
},
/** How long to show the p5.js canvas. */
stimulus_duration: {
type: jspsych.ParameterType.INT,
pretty_name: "Stimulus duration",
default: null,
},
/** How long to show the trial. */
trial_duration: {
type: jspsych.ParameterType.INT,
pretty_name: "Trial duration",
default: null,
},
/** The vertical margin of the button. */
margin_vertical: {
type: jspsych.ParameterType.STRING,
pretty_name: "Margin vertical",
default: "0px",
},
/** The horizontal margin of the button. */
margin_horizontal: {
type: jspsych.ParameterType.STRING,
pretty_name: "Margin horizontal",
default: "8px",
},
/** If true, then trial will end when user responds. */
response_ends_trial: {
type: jspsych.ParameterType.BOOL,
pretty_name: "Response ends trial",
default: true,
}
},
};
/**
* **p5js**
*
* jsPsych plugin for displaying a canvas stimulus using p5.js
*
* @author Andre Sahakian (modified from (Chris Jungerius (modified from Josh de Leeuw))
*
*/
class P5JSPlugin {
constructor(jsPsych) {
this.jsPsych = jsPsych;
}
trial(display_element, trial) {
// create div to hold the canvas
var html = '<div id="jspsych-p5js-stimulus"></div>' ;
//display buttons
var buttons = [];
if (Array.isArray(trial.button_html)) {
if (trial.button_html.length == trial.button_choices.length) {
buttons = trial.button_html;
}
else {
console.error("Error in p5js plugin. The length of the button_html array does not equal the length of the button_choices array");
}
}
else {
for (var i = 0; i < trial.button_choices.length; i++) {
buttons.push(trial.button_html);
}
}
html += '<div id="jspsych-p5js-btngroup">';
for (var i = 0; i < trial.button_choices.length; i++) {
var str = buttons[i].replace(/%choice%/g, trial.button_choices[i]);
html +=
'<div class="jspsych-p5js-button" style="display: inline-block; margin:' +
trial.margin_vertical +
" " +
trial.margin_horizontal +
'" id="jspsych-p5js-button-' +
i +
'" data-choice="' +
i +
'">' +
str +
"</div>";
}
html += "</div>";
//show prompt if there is one
if (trial.prompt !== null) {
html += trial.prompt;
}
display_element.innerHTML = html;
// set up p5 js setup and draw functions
function p5js_sketch(p) {
p.setup = function () {
trial.setup_func(p)
}
p.draw = function () {
trial.draw_func(p)
}
trial.top_level_declarations(p)
}
// place the p5 js canvas in the designated div
new p5(p5js_sketch, "jspsych-p5js-stimulus")
// start time
var start_time = performance.now();
// add event listeners to buttons
for (var i = 0; i < trial.button_choices.length; i++) {
display_element
.querySelector("#jspsych-p5js-button-" + i)
.addEventListener("click", (e) => {
var btn_el = e.currentTarget;
var button_choice = btn_el.getAttribute("data-choice"); // don't use dataset for jsdom compatibility
after_button_response(button_choice);
});
}
// store response
var response = {
rt: null,
button: null,
key: null,
};
// function to end trial when it is time
const end_trial = () => {
// kill any remaining setTimeout handlers
this.jsPsych.pluginAPI.clearAllTimeouts();
// kill keyboard listeners
if (typeof keyboardListener !== "undefined") {
this.jsPsych.pluginAPI.cancelKeyboardResponse(keyboardListener);
}
// gather the data to store for the trial
var trial_data = {
rt: response.rt,
response: {
button: response.button,
key: response.key,
},
};
// clear the display
display_element.innerHTML = "";
// move on to the next trial
this.jsPsych.finishTrial(trial_data);
};
// function to handle responses by the subject
function after_button_response(choice) {
// measure rt
var end_time = performance.now();
var rt = Math.round(end_time - start_time);
response.button = parseInt(choice);
response.rt = rt;
// after a valid response, the stimulus will have the CSS class 'responded'
// which can be used to provide visual feedback that a response was recorded
display_element.querySelector("#jspsych-p5js-stimulus").className +=
" responded";
// disable all the buttons after a response
var btns = document.querySelectorAll(".jspsych-p5js-button button");
for (var i = 0; i < btns.length; i++) {
//btns[i].removeEventListener('click');
btns[i].setAttribute("disabled", "disabled");
}
if (trial.response_ends_trial) {
end_trial();
}
}
// function to handle responses by the subject
var after_key_response = (info) => {
// after a valid response, the stimulus will have the CSS class 'responded'
// which can be used to provide visual feedback that a response was recorded
display_element.querySelector("#jspsych-p5js-stimulus").className +=
" responded";
// only record the first response
if (response.key == null) {
response = info;
// add button response as null
response.button = null
}
if (trial.response_ends_trial) {
end_trial();
}
};
// start the response listener
if (trial.key_choices != "NO_KEYS") {
var keyboardListener = this.jsPsych.pluginAPI.getKeyboardResponse({
callback_function: after_key_response,
valid_responses: trial.key_choices,
rt_method: "performance",
persist: false,
allow_held_key: false,
});
}
// hide image if timing is set
if (trial.stimulus_duration !== null) {
this.jsPsych.pluginAPI.setTimeout(() => {
display_element.querySelector("#jspsych-p5js-stimulus").style.visibility = "hidden";
}, trial.stimulus_duration);
}
// end trial if time limit is set
if (trial.trial_duration !== null) {
this.jsPsych.pluginAPI.setTimeout(() => {
end_trial();
}, trial.trial_duration);
}
}
}
P5JSPlugin.info = info;
return P5JSPlugin;
})(jsPsychModule);