Use external libraries to add functionality to your experiment
As you develop your ROAR app, you may discover that you need to incorporate additional functionality using external JavaScript libraries. To do so, you'll follow outline of this section: installing the new library, importing it at the top of some JavaScript file, and using it in your source code. As a pedagogical example, in this section, we will install, import, and use the image-keyboard-response plugin.
In the writing your experiment section, you added a block to your experiment asking participants to differentiate between cat and dog images.
You may have noticed that we created the block2Targets array in src/loadAssets.js in order to pass an HTML string as a stimulus to the jsPsychHtmlKeyboardResponse plugin. But jsPsych already has its own jsPsychImageKeyboardResponse plugin that takes an image stimulus as input and obviates the need for creating the block2Targets array. In this section, we will install the jsPsychImageKeyboardResponse plugin and adapt your experiment to use it.
Before we start, be sure to commit your work so far into version control.
consthotDogTrials={timeline:[{type:jsPsychHtmlKeyboardResponse,stimulus:'<div style="font-size:60px;">+</div>',choices:'NO_KEYS',trial_duration:500,},{// Here we change the plugin typetype:jsPsychImageKeyboardResponse,stimulus:jsPsych.timelineVariable('target'),choices:['ArrowLeft','ArrowRight'],prompt:` <p>Is this a hot dog?</p> <p>If yes, press the right arrow key.</p> <p>If no, press the left arrow key.</p> `,// And here we specify the stimulus height and width,// which we previously did in the `allTargets` array.stimulus_height:250,stimulus_width:250,data:{// Here is where we specify that we should save the trial to Firestoresave_trial:true,// Here we can also specify additional information that we would like stored// in this trial in ROAR's Firestore database.},},],timeline_variables:allTargets,sample:{type:'without-replacement',size:10,},};
constcatDogTrials={timeline:[{type:jsPsychImageKeyboardResponse,stimulus:'<div style="font-size:60px;">+</div>',choices:'NO_KEYS',trial_duration:500,},{type:jsPsychHtmlKeyboardResponse,stimulus:jsPsych.timelineVariable('target'),choices:['ArrowLeft','ArrowRight'],prompt:` <p>Is this a dog?</p> <p>If yes, press the right arrow key.</p> <p>If no, press the left arrow key.</p> `,stimulus_height:250,stimulus_width:250,data:{// Here is where we specify that we should save the trial to Firestoresave_trial:true,// Here we can also specify additional information that we would like stored// in this trial in ROAR's Firestore database.},},],timeline_variables:block2Targets,sample:{type:'without-replacement',size:10,},};
Congratulations! You just used your first new plugin type!