-
Notifications
You must be signed in to change notification settings - Fork 0
/
StaircaseExperiment.html
606 lines (559 loc) · 18.4 KB
/
StaircaseExperiment.html
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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
<!DOCTYPE html>
<html>
<head>
<title>My experiment</title>
<script src="jspsych/jspsych.js"></script>
<script src="jspsych/plugins/jspsych-html-keyboard-response.js"></script>
<script src="jspsych/plugins/jspsych-image-keyboard-response.js"></script>
<link rel="stylesheet" href="jspsych/css/jspsych.css"></link>
<style>
div.ex1 {
position: absolute;
left: 0px;
}
div.ex2 {
position: absolute;
left: 200px
}
div.ex3 {
position: absolute;
left: 400px
}
div.ex4 {
position: absolute;
left: 600px
}
</style>
</head>
<body></body>
<script>
var stim_width_height = 200;
var min_dot_density = 100;
var max_dot_density = 2000;
var min_line_density = 30;
var max_line_density = 550;
var dim_a = 0.5;
var dim_b = 0.5;
var rel_bw_distance = 0.25;
var rel_wi_distance = 0.25;
var irr_distance = 0.25;
/* create timeline */
var timeline = [];
/* define welcome message trial */
var welcome = {
type: "html-keyboard-response",
stimulus: "Welcome to the experiment. Press any key to begin."
};
timeline.push(welcome);
/* define instructions trial */
var instructions = {
type: "html-keyboard-response",
stimulus: "<p>In this experiment, four stars will appear in the center" +
"of the screen. </p><p> If the <strong>first</strong> star from the left is different" +
"from the others, press 1. </p><p>If the <strong>second</strong> star from the left is different"+
"from the rest, press 2. </p><p> If the <strong>third</strong> star from the left is different from the rest"+
"press 3. </p><p> If the <strong>fourth</strong> star from the left is different from the rest, press 4. </p>" +
"<div style='float: left;'><img src='img/lineup.png'></img>" +
"<p class='small'><strong>Press 3 </strong></p></div>" +
"<p>Press any key to begin.</p>",
post_trial_gap: function() { return 2000; }
};
timeline.push(instructions);
/* test trials */
var fixation = {
type: 'html-keyboard-response',
stimulus: '<div style="font-size:60px;">+</div>',
choices: jsPsych.NO_KEYS,
trial_duration: function() {
return jsPsych.randomization.sampleWithoutReplacement([250, 500, 750, 1000, 1250, 1500, 1750, 2000], 1)[0];
},
data: {test_part: 'fixation'}
}
var test = {
type: "html-keyboard-response",
stimulus: '',
choices: ['1', '2', '3', '4'],
on_start: function(trial) {
/* pair_type = randomize_pair_type(); */
correct = randomize_correct();
pair = randomize_pair();
stimulus = calculate_dimension_values(pair, correct);
if (pair =='1'||'2'||'3'||'4') {pair_type = 'bw'}
else if (pair =='5'||'6'||'7'||'8'||'9'||'10'||'11'||'12') {pair_type = 'wi'}
else {pair_type = 'irr'}
if(pair_type == 'bw'){
distance = rel_bw_distance;
} else if(pair_type == 'wi'){
distance = rel_wi_distance;
} else if(pair_type == 'irr'){
distance = irr_distance;
}
trial.stimulus = stimulus;
trial.data = {test_part: 'test',/*pair_type: pair_type,*/ distance: distance, correct_response: correct}
},
on_finish: function(data) {
data.correct = data.key_press == jsPsych.pluginAPI.convertKeyCharacterToKeyCode(data.correct_response);
if(pair_type == 'bw'){
rel_bw_distance = adjust_distance(rel_bw_distance, data.correct);
} else if(pair_type == 'wi'){
rel_wi_distance = adjust_distance(rel_wi_distance, data.correct);
} else if(pair_type == 'irr'){
irr_distance = adjust_distance(irr_distance, data.correct);
}
},
}
/* determines which dimension is relevant throughout the experiment */
var relevant_dimension = jsPsych.randomization.sampleWithoutReplacement(['dots', 'lines'], 1) [0];
/* determine which sunburst will be the odd-ball */
function randomize_correct() {
return jsPsych.randomization.sampleWithoutReplacement(['1', '2', '3', '4'], 1)[0];
}
var answer = randomize_correct();
/* determines which distance to adjust after each trial*/
function which_distance(pair_type) {
if (pair_type == 'bw') {
return rel_bw_distance;
}
else if (pair_type == 'wi') {
return rel_wi_distance;
}
else {
return irr_distance;
}
}
/* determines if the last answer was correct */
function last_correct() {
return jsPsych.data.get().last(1).values()[0].correct;
}
/* adjusts the particular distance depending on correctness of the response */
function adjust_distance(distance, correct) {
if(correct){
distance *= 0.85;
} else {
distance *= 1.15;
}
return distance;
}
/* randomly decides whether the odd-ball is more or less dense */
function more_or_less() {
var less = jsPsych.randomization.sampleWithoutReplacement(['less', 'more'], 1)[0];
var boolean = less == "less";
return boolean;
}
/* determines which pair type will be tested for each trial */
/* function randomize_pair_type() {
dimension = jsPsych.randomization.sampleWithoutReplacement(['rel', 'irr'], 1)[0];
if (dimension == 'rel') {
dimension = jsPsych.randomization.sampleWithoutReplacement(['bw', 'wi'], 1)[0];
}
return dimension;
}
*/
/* randomly selects a pair of a certain pair type */
/* function randomize_pair(pair_type) {
if (pair_type == 'bw') {
return jsPsych.randomization.sampleWithoutReplacement(['1', '2', '3', '4'], 1)[0];
}
else if (pair_type == 'wi') {
return jsPsych.randomization.sampleWithoutReplacement(['5', '6', '7', '8', '9', '10', '11', '12'], 1)[0];
}
else {
return jsPsych.randomization.sampleWithoutReplacement(['13', '14', '15', '16', '17', '18', '19', '20', '21', '22', '23', '24'], 1)[0];
}
}
*/
function randomize_pair() {
return jsPsych.randomization.sampleWithoutReplacement(['1', '2', '3', '4', '5', '6', '7', '8', '9'+
'10', '11', '12', '13', '14', '15, 16', '17' +
'18', '19', '20', '21', '22', '23', '24'], 1)[0];
}
function x1_coordinate(pair) {
if (pair == '1') {
x1 = 1;
} else if (pair == '2') {
x1 = 1;
} else if (pair == '3') {
x1 = 1;
} else if (pair == '4') {
x1 = 1;
} else if (pair == '5') {
x1 = 0;
} else if (pair == '6') {
x1 = 0;
} else if (pair == '7') {
x1 = 0;
} else if (pair == '8') {
x1 = 0;
} else if (pair == '9') {
x1 = 2;
} else if (pair == '10') {
x1 = 2;
} else if (pair == '11') {
x1 = 2;
} else if (pair == '12') {
x1 = 2;
} else if (pair == '13') {
x1 = 0;
} else if (pair == '14') {
x1 = 0;
} else if (pair == '15') {
x1 = 0;
} else if (pair == '16') {
x1 = 1;
} else if (pair == '17') {
x1 = 1;
} else if (pair == '18') {
x1 = 1;
} else if (pair == '19') {
x1 = 2;
} else if (pair == '20') {
x1 = 2;
} else if (pair == '21') {
x1 = 2;
} else if (pair == '22') {
x1 = 3;
} else if (pair == '23') {
x1 = 3;
} else {
x1 = 3;
}
return x1;
}
function x2_coordinate(pair) {
if (pair == '1') {
x2 = 2;
} else if (pair == '2') {
x2 = 2;
} else if (pair == '3') {
x2 = 2;
} else if (pair == '4') {
x2 = 2;
} else if (pair == '5') {
x2 = 1;
} else if (pair == '6') {
x2 = 1;
} else if (pair == '7') {
x2 = 1;
} else if (pair == '8') {
x2 = 0;
} else if (pair == '9') {
x2 = 3;
} else if (pair == '10') {
x2 = 3;
} else if (pair == '11') {
x2 = 3;
} else if (pair == '12') {
x2 = 3;
} else if (pair == '13') {
x2 = 0;
} else if (pair == '14') {
x2 = 0;
} else if (pair == '15') {
x2 = 0;
} else if (pair == '16') {
x2 = 1;
} else if (pair == '17') {
x2 = 1;
} else if (pair == '18') {
x2 = 1;
} else if (pair == '19') {
x2 = 2;
} else if (pair == '20') {
x2 = 2;
} else if (pair == '21') {
x2 = 2;
} else if (pair == '22') {
x2 = 3;
} else if (pair == '23') {
x2 = 3;
} else {
x2 = 3;
}
return x2;
}
function y1_coordinate(pair) {
if (pair == '1') {
y1 = 3;
} else if (pair == '2') {
y1 = 2;
} else if (pair == '3') {
y1 = 1;
} else if (pair == '4') {
y1 = 0;
} else if (pair == '5') {
y1 = 3;
} else if (pair == '6') {
y1 = 2;
} else if (pair == '7') {
y1 = 1;
} else if (pair == '8') {
y1 = 1;
} else if (pair == '9') {
y1 = 3;
} else if (pair == '10') {
y1 = 2;
} else if (pair == '11') {
y1 = 1;
} else if (pair == '12') {
y1 = 0;
} else if (pair == '13') {
y1 = 3;
} else if (pair == '14') {
y1 = 2;
} else if (pair == '15') {
y1 = 1;
} else if (pair == '16') {
y1 = 3;
} else if (pair == '17') {
y1 = 2;
} else if (pair == '18') {
y1 = 1;
} else if (pair == '19') {
y1 = 3;
} else if (pair == '20') {
y1 = 2;
} else if (pair == '21') {
y1 = 1;
} else if (pair == '22') {
y1 = 3;
} else if (pair == '23') {
y1 = 2;
} else {
y1 = 1;
}
return y1;
}
function y2_coordinate(pair) {
if (pair == '1') {
y2 = 3;
} else if (pair == '2') {
y2 = 2;
} else if (pair == '3') {
y2 = 1;
} else if (pair == '4') {
y2 = 0;
} else if (pair == '5') {
y2 = 3;
} else if (pair == '6') {
y2 = 2;
} else if (pair == '7') {
y2 = 1;
} else if (pair == '8') {
y2 = 0;
} else if (pair == '9') {
y2 = 3;
} else if (pair == '10') {
y2 = 2;
} else if (pair == '11') {
y2 = 1;
} else if (pair == '12') {
y2 = 0;
} else if (pair == '13') {
y2 = 2;
} else if (pair == '14') {
y2 = 1;
} else if (pair == '15') {
y2 = 0;
} else if (pair == '16') {
y2 = 2;
} else if (pair == '17') {
y2 = 1;
} else if (pair == '18') {
y2 = 0;
} else if (pair == '19') {
y2 = 2;
} else if (pair == '20') {
y2 = 1;
} else if (pair == '21') {
y2 = 0;
} else if (pair == '22') {
y2 = 2;
} else if (pair == '23') {
y2 = 1;
} else {
y2 = 0;
}
return y2;
}
function x_dim_val(x, bw, wi){
var x_dim, y_dim;
if(x == 0){
x_dim = 0.5 - (bw/2) - wi;
} else if(x == 1) {
x_dim = 0.5 - (bw/2);
} else if(x == 2) {
x_dim = 0.5 + (bw/2);
} else if(x == 3) {
x_dim = 0.5 + (bw/2) + wi;
}
return x_dim;
}
function y_dim_val(y, irr) {
if(y == 0){
y_dim = 0.5 - (irr/2) - irr;
} else if(y == 1) {
y_dim = 0.5 - (irr/2);
} else if(y == 2) {
y_dim = 0.5 + (irr/2);
} else if(y == 3) {
y_dim = 0.5 + (irr/2) + irr;
}
return y_dim;
}
function calculate_dimension_values(/*pair_type*/pair, correct) {
var boolean = more_or_less();
if (boolean) {
sx = x_dim_val(x1_coordinate(pair), rel_bw_distance, rel_wi_distance);
} else {
sx = x_dim_val(x2_coordinate(pair), rel_bw_distance, rel_wi_distance);
}
sy = y_dim_val(y1_coordinate(pair), irr_distance);
if (boolean) {
ox = x_dim_val(x2_coordinate(pair), rel_bw_distance, rel_wi_distance);
} else {
ox = x_dim_val(x1_coordinate(pair), rel_bw_distance, rel_wi_distance);
}
var oy = y_dim_val(y2_coordinate(pair), irr_distance);
var relevant = relevant_dimension;
if (relevant == 'dots') {
set = generate_set(sx, sy, ox, oy, correct);
return set;
} else {
set = generate_set(sy, sx, oy, ox, correct);
return set;
}
}
function generate_set(same_dot, same_line, odd_dot, odd_line, correct) {
var html = '<div style="position: relative; width:'+stim_width_height*4+'px; height:'+stim_width_height+'px;">';
if (correct == '1'){
html += '<div class="ex1">'+generate_stim(odd_dot, odd_line)+'</div>'+
'<div class="ex2">'+generate_stim(same_dot, same_line)+'</div>'+
'<div class="ex3">'+generate_stim(same_dot, same_line)+'</div>'+
'<div class="ex4">'+generate_stim(same_dot, same_line)+'</div>';
} else if (correct == '2') {
html += '<div class="ex1">'+generate_stim(same_dot, same_line)+'</div>'+
'<div class="ex2">'+generate_stim(odd_dot, odd_line)+'</div>'+
'<div class="ex3">'+generate_stim(same_dot, same_line)+'</div>'+
'<div class="ex4">'+generate_stim(same_dot, same_line)+'</div>';
} else if (correct == '3') {
html += '<div class="ex1">'+generate_stim(same_dot, same_line)+'</div>'+
'<div class="ex2">'+generate_stim(same_dot, same_line)+'</div>'+
'<div class="ex3">'+generate_stim(odd_dot, odd_line)+'</div>'+
'<div class="ex4">'+generate_stim(same_dot, same_line)+'</div>';
} else {
html += '<div class="ex1">'+generate_stim(same_dot, same_line)+'</div>'+
'<div class="ex2">'+generate_stim(same_dot, same_line)+'</div>'+
'<div class="ex3">'+generate_stim(same_dot, same_line)+'</div>'+
'<div class="ex4">'+generate_stim(odd_dot, same_line)+'</div>';
}
html += '</div>';
return html;
}
function generate_stim(dot_dim, line_dim) {
// params
var w = stim_width_height;
var r = stim_width_height/4;
var x = w / 2;
var y = w / 2;
var circle_r = stim_width_height / 200;
var minLineLength = 10;
var dot_density = min_dot_density + (max_dot_density-min_dot_density)*dot_dim;
var line_density = min_line_density + (max_line_density-min_line_density)*line_dim;
// svg string
var str = '<svg xmlns="http://www.w3.org/2000/svg" width="' + w + '" height="' + w + '">'
//Code for drawing the lines that come out of the circle.
//Each line starts in the center of the circle and is drawn outward
for (var i = 0; i < line_density; i++) {
var angle = Math.random() * Math.PI / 2;
var lineLength = r + minLineLength + (r - minLineLength) * Math.random();
var x2 = Math.cos(angle) * lineLength;
var y2 = Math.sin(angle) * lineLength;
if (Math.random() < 0.5) {
x2 = -x2;
}
if (Math.random() < 0.5) {
y2 = -y2;
}
x2 = x + x2;
y2 = y + y2;
str += '<line x1="' + x + '" y1="' + y + '" x2="' + x2 + '" y2="' + y2 + '" style="stroke:rgb(65,105,225); stroke-width:1;" />';
}
// draw circle
str += '<circle cx="' + x + '" cy="' + y + '" r="' + r + '" style="stroke:rgb(65,105,225); stroke-width:2; fill: rgb(255,255,255);"/>'
//Code for creating and dispersing the dots
for (var j = 0; j < dot_density; j++) {
do {
var x2 = Math.random() * 2 * r + x - r;
var y2 = Math.random() * 2 * r + y - r;
} while ((Math.pow(x - x2, 2) + Math.pow(y - y2, 2)) > Math.pow(r - circle_r - 2, 2));
str += '<circle cx="' + x2 + '" cy="' + y2 + '" r="' + circle_r + '" style="fill:rgb(65,105,225);" />';
}
str += "</svg>";
return str;
}
var feedback = {
type: "html-keyboard-response",
stimulus: function() {
var lastCorrect = jsPsych.data.get().last(1).values()[0].correct;
if(lastCorrect == true){
return "<p>Correct!</p>"
}
else {
return "<p>Incorrect!</p>"
}
},
choices: jsPsych.NO_KEYS,
trial_duration: 500
}
var test_procedure = {
timeline: [fixation, test, feedback],
//timeline_variables: test_stimuli,
repetitions: 20,
randomize_order: true
}
timeline.push(test_procedure);
var min_dot_density = 100;
var max_dot_density = 2000;
var min_line_density = 30;
var max_line_density = 550;
var dim_a = 0.5;
var dim_b = 0.5;
document.querySelector('body').innerHTML = generate_set(0.5, 0.5);
/* define debrief */
var debrief_block = {
type: "html-keyboard-response",
stimulus: function() {
var trials = jsPsych.data.get().filter({test_part: 'test'});
var correct_trials = trials.filter({correct: true});
var accuracy = Math.round(correct_trials.count() / trials.count() * 100);
var rt = Math.round(correct_trials.select('rt').mean());
return "<p>You responded correctly on "+accuracy+"% of the trials.</p>"+
"<p>Your average response time was "+rt+"ms.</p>"+
"<p>Press any key to complete the experiment. Thank you!</p>";
}
};
timeline.push(debrief_block);
/* var training = {
type: "html-keyboard-response",
stimulus: "<p>In this experiment, a circle will appear in the center " +
"of the screen.</p><p>If the circle is <strong>blue</strong>, " +
"press the letter F on the keyboard as fast as you can.</p>" +
"<p>If the circle is <strong>orange</strong>, press the letter J " +
"as fast as you can.</p>" +
"<div style='float: left;'><img src='img/blue.png'></img>" +
"<p class='small'><strong>Press the F key</strong></p></div>" +
"<div class='float: right;'><img src='img/orange.png'></img>" +
"<p class='small'><strong>Press the J key</strong></p></div>" +
"<p>Press any key to begin.</p>"
};
timeline.push(training);
*/
/* start the experiment */
jsPsych.init({
timeline: timeline,
on_finish: function() {
console.log(jsPsych.data.get().ignore('stimulus').json());
}
});
</script>
</html>