Home Reference Source

scripts/experiments/jnd_radius/jnd_radius_timeline.js

  1. import JND_Radius from "/scripts/experiments/jnd_radius/jnd_radius.js";
  2. import {get_instructions} from "/scripts/experiment-properties/instructions/instructions_controller.js";
  3.  
  4. export var jnd_radius_exp = new JND_Radius(params);
  5.  
  6. var timeline = [];
  7. var address = location.protocol + "//" + location.hostname + ":" + location.port;
  8.  
  9. // Firefox check for formatting
  10. if (typeof InstallTrigger !== 'undefined') {
  11. var isFirefox = true;
  12. } else {
  13. var isFirefox = false;
  14. }
  15.  
  16. // =========================================================
  17. // WELCOME TRIAL BLOCK
  18.  
  19. let shape_names = jnd_radius_exp.condition_name.split("_");
  20.  
  21. var welcome = {
  22. type: 'html-keyboard-response',
  23. stimulus: `<div align = "center" style="margin-top: ${isFirefox ? "25vh" : "0"}">` + `<img src="${address}/img/VCL_lab_logo.png"></img><br><br>` +
  24. `<b>Base:</b> ${jnd_radius_exp.constructor.name}` + '<br>' +
  25. `<b>Trial Type:</b> ${jnd_radius_exp.trial_structure}` + '<br>' +
  26. `<b>Condition:</b> ${shape_names[0]}, ${shape_names[1]}` +
  27. '<br><br><br><p><font size = 15>Press any key to begin.<p></font>' +
  28. '</div>',
  29. data: {type: 'instruction'}
  30. };
  31. timeline.push(welcome);
  32.  
  33. // =========================================================
  34. // INSTRUCTION TRIAL BLOCKS
  35.  
  36. var instructions = {
  37. type: "html-keyboard-response",
  38. stimulus: function (){
  39. return get_instructions(jnd_radius_exp);
  40. }
  41. };
  42.  
  43. timeline.push(instructions);
  44.  
  45. // =========================================================
  46. // FEEDBACK
  47.  
  48. var feedback = {
  49. type: 'html-keyboard-response',
  50. choices: jsPsych.NO_KEYS, //No responses will be accepted as a valid response.
  51. trial_duration: 500,
  52. data: {type: 'feedback'},
  53. stimulus: function(){
  54.  
  55. document.body.style.backgroundColor = jnd_radius_exp.trial_data.feedback_background_color;
  56.  
  57. var last_trial = JSON.parse(jsPsych.data.getLastTrialData().json());
  58. var last_trial_correct = last_trial[0]["correct"];
  59.  
  60. // For debugging purposes:
  61. if (last_trial_correct == -1){
  62. return '<p>' +
  63. `<div style = "margin-top: ${isFirefox ? "45vh" : "0"};"><font style="font-size:50px; color:blue">Exiting from experiment.<p></font></div>`
  64. }
  65.  
  66. else if (last_trial_correct){
  67. return `<p><div style = "margin-top: ${isFirefox ? "45vh" : "0"};"><i class="fa fa-check-circle" style="font-size:50px; color:green; margin-right: 10px;"></i>` +
  68. '<font style="font-size:50px; color:green">Correct!<p></font></div>'
  69. }
  70. else{
  71. return `<p><div style = "margin-top: ${isFirefox ? "45vh" : "0"};"><i class="fa fa-close" style="font-size:50px; color:red; margin-right: 10px;"></i>` +
  72. '<font style="font-size:50px; color:red;"">Incorrect!<p></font></div>'
  73. }
  74. }
  75. };
  76.  
  77. // =========================================================
  78. // EXPERIMENT TRIAL BLOCKS
  79.  
  80. var trial = jnd_radius_exp.generate_trial("test");
  81.  
  82. var experiment = {
  83. timeline: [trial, feedback], // We use same feedback block as that used in practice
  84. loop_function: function(data){ // Return true if timeline should continue
  85. // Return false if timeline should end
  86.  
  87. // For debugging, if you want to exit out of experiment, press q:
  88. if (jsPsych.pluginAPI.convertKeyCharacterToKeyCode('q') == data.values()[0].key_press){
  89. return false;
  90. }
  91.  
  92. // If subcondition should end:
  93. if(jnd_radius_exp.end_sub_condition()){
  94. jnd_radius_exp.first_trial_of_sub_condition = true;
  95. // If there are still more subconditions, increment current index
  96. if (jnd_radius_exp.current_sub_condition_index < (jnd_radius_exp.sub_conditions_constants.length-1)){
  97. jnd_radius_exp.current_sub_condition_index++;
  98. console.log("!!!!!!!!!! Moved to new sub condition at index "
  99. + jnd_radius_exp.current_sub_condition_index);
  100. return true;
  101. }
  102. // Else end experiment
  103. else{
  104. return false;
  105. }
  106. }
  107. // Else continue w/ current subcondition:
  108. else {
  109. return true;
  110. }
  111. },
  112. on_finish: function(data){
  113. jnd_radius_exp.trial_data = data;
  114. }
  115. };
  116.  
  117. timeline.push(experiment);
  118.  
  119. console.log("======================");
  120.  
  121. // =========================================================
  122. // DATA DOWNLOADING
  123.  
  124. var experiment_end = {
  125. type: 'html-keyboard-response',
  126. stimulus: `<div align = "center" style = "margin-top: ${isFirefox ? "45vh" : "0"};">` +
  127. '<p><font size = 10>You have completed the experiment!<p></font>' +
  128. '<br>' +
  129. 'Trial and summary data files will now automatically download locally.' +
  130. '</div>' ,
  131. on_start: function(){
  132.  
  133. jnd_radius_exp.export_trial_data();
  134. jnd_radius_exp.export_summary_data();
  135.  
  136. // Reset background color to feedback
  137. document.body.style.backgroundColor = jnd_radius_exp.trial_data.feedback_background_color;
  138. }
  139. };
  140. timeline.push(experiment_end);
  141.  
  142. // =========================================================
  143. // START JSPSYCH
  144.  
  145. jsPsych.init({
  146. timeline: timeline,
  147. on_finish: function(){
  148. jsPsych.data.displayData();
  149. }
  150. });