/** * jspsych-similarity.js * Josh de Leeuw * * This plugin create a trial where two images are shown sequentially, and the subject rates their similarity using a slider controlled with the mouse. * * parameters: * stimuli: array of arrays. inner arrays are two stimuli. stimuli can be image paths or html strings. each inner array is one trial. * label_low: label to display at the left end of the similarity slider scale. * label_high: label to display at the right end of the similiarity slider scale. * timing_first_stim: how long to show the first stimulus. * timing_second_stim: how long to show the second stimulus. can be -1 to show until a response is given. * timing_image_gap: how long to show a blank screen between the two stimuli. * timing_post_trial: how long to show a blank screen after the trial is over. * is_html: must set to true when using HTML strings as the stimuli. * prompt: optional HTML string to display with the stimulus. * data: the optional data object * */ (function($) { jsPsych.similarity = (function() { var plugin = {}; plugin.create = function(params) { var trials = new Array(params.stimuli.length); for (var i = 0; i < trials.length; i++) { trials[i] = {}; trials[i].type = "similarity"; trials[i].a_path = params.stimuli[i][0]; trials[i].b_path = params.stimuli[i][1]; trials[i].label_low = params.label_low || "Not at all similar"; trials[i].label_high = params.label_high || "Identical"; trials[i].timing_first_stim = params.timing_first_stim || 1000; // default 1000ms trials[i].timing_second_stim = params.timing_second_stim || -1; // -1 = inf time; positive numbers = msec to display second image. trials[i].timing_image_gap = params.timing_image_gap || 1000; // default 1000ms trials[i].timing_post_trial = params.timing_post_trial || 1000; // default 1000ms trials[i].is_html = (typeof params.is_html === 'undefined') ? false : params.is_html; trials[i].prompt = (typeof params.prompt === 'undefined') ? '' : params.prompt; trials[i].data = (typeof params.data === 'undefined') ? {} : params.data[i]; } return trials; }; var sim_trial_complete = false; plugin.trial = function(display_element, block, trial, part) { switch (part) { case 1: sim_trial_complete = false; // show the images if (!trial.is_html) { display_element.append($('', { "src": trial.a_path, "class": 'sim' })); } else { display_element.append($('
', { "html": trial.a_path, "class": 'sim' })); } setTimeout(function() { plugin.trial(display_element, block, trial, part + 1); }, trial.timing_first_stim); break; case 2: $('.sim').remove(); setTimeout(function() { plugin.trial(display_element, block, trial, part + 1); }, trial.timing_image_gap); break; case 3: if (!trial.is_html) { display_element.append($('', { "src": trial.b_path, "class": 'sim', "id": 'jspsych_sim_second_image' })); } else { display_element.append($('
', { "html": trial.b_path, "class": 'sim', "id": 'jspsych_sim_second_image' })); } if (trial.timing_second_stim > 0) { setTimeout(function() { if (!sim_trial_complete) { $("#jspsych_sim_second_image").css('visibility', 'hidden'); } }, trial.timing_second_stim); } // create slider display_element.append($('
', { "id": 'slider', "class": 'sim' })); $("#slider").slider({ value: 50, min: 0, max: 100, step: 1, }); // create labels for slider display_element.append($('
', { "id": 'slider_labels', "class": 'sim' })); $('#slider_labels').append($('

' + trial.label_low + '

')); $('#slider_labels').append($('

' + trial.label_high + '

')); // create button display_element.append($('