jsPsych/jspsych-animation.js
Josh de Leeuw a056b6e69c missed the animation and touch plugins in previous changes.
similarity had a bug in the naming of the plugin.
2012-05-15 16:10:10 -04:00

66 lines
1.6 KiB
JavaScript

// jsPsych plugin for showing animations
// Josh de Leeuw
(function( $ ) {
$.fn.jsPsych.animation = (function(){
var plugin = {};
plugin.create = function(params) {
stims = params["stimuli"];
trials = new Array(stims.length);
for(var i = 0; i < trials.length; i++)
{
trials[i] = {};
trials[i]["type"] = "animate";
trials[i]["stims"] = stims[i];
trials[i]["frame_time"] = params["frame_time"];
trials[i]["repetitions"] = params["repetitions"];
trials[i]["timing"] = params["timing"];
if(params["prompt"] != undefined){
trials[i]["prompt"] = params["prompt"][i];
}
}
return trials;
}
plugin.trial = function($this, block, trial, part)
{
var animate_frame = -1;
var reps = 0;
switch(part)
{
case 1:
animate_interval = setInterval(function(){
showImage = true;
$this.html(""); // clear everything
animate_frame++;
if(animate_frame == trial.stims.length)
{
animate_frame = 0;
reps++;
if(reps >= trial.repetitions)
{
animation_trial($this, block, trial, part + 1);
clearInterval(animate_interval);
showImage = false;
}
}
if(showImage){
$this.append($('<img>', {
"src": trial.stims[animate_frame],
"class": 'animate'
}));
if(trial.prompt != undefined) { $this.append(trial.prompt); }
}
}, trial.frame_time);
break;
case 2:
setTimeout(function(){ block.next(); }, trial.timing[0]);
break;
}
}
return plugin;
})();
})(jQuery);