Fixed problem going to next trial

This commit is contained in:
Gustavo Juantorena 2020-09-25 19:11:29 -03:00
parent c515d43c0d
commit e1ec9852c8

View File

@ -40,14 +40,28 @@ jsPsych.plugins['virtual-chin'] = (function() {
}
}
plugin.trial = function(display_element, trial) {
// Get screen size
var w = window.innerWidth;
var h = window.innerHeight;
const screen_size_px = []
screen_size_px.push(w)
screen_size_px.push('x')
screen_size_px.push(h)
// data saving
var trial_data = { //I need to modify this in order to save important data
'viewing_distance_cm': trial.viewing_distance_cm,
'cardWidth_px': trial.cardWidth_px
'cardWidth_px': trial.cardWidth_px,
'screen_size_px': trial.screen_size_px
};
trial_data.screen_size_px = screen_size_px
//Store all the configuration data in variable 'data'
var data = {"dataType":"configurationData"};
@ -71,11 +85,10 @@ jsPsych.plugins['virtual-chin'] = (function() {
}( window.distanceSetup = window.distanceSetup || {}, jQuery));
function getCardWidth() {
var cardWidthPx = $('#card').width();
data["cardWidthPx"] = distanceSetup.round(cardWidthPx,2);
console.log(cardWidthPx)
trial_data.cardWidth_px = cardWidthPx // add to trial_data
@ -93,7 +106,6 @@ jsPsych.plugins['virtual-chin'] = (function() {
};
$( function() {
$( "#slider" ).slider({value:"50"});
} );
@ -110,6 +122,7 @@ jsPsych.plugins['virtual-chin'] = (function() {
});
//=============================
//Ball Animation
@ -150,6 +163,7 @@ jsPsych.plugins['virtual-chin'] = (function() {
function recordPosition(event, angle=13.5) {
// angle: define horizontal blind spot entry point position in degrees.
if (event.keyCode == '32') { //Press "Space"
data["ballPosition"].push(distanceSetup.round((ball.cx() + moveX),2));
var sum = data["ballPosition"].reduce((a, b) => a + b, 0);
@ -157,7 +171,6 @@ jsPsych.plugins['virtual-chin'] = (function() {
data["avgBallPos"] = distanceSetup.round(sum/ballPosLen, 2);
var ball_sqr_distance = (data["squarePosition"]-data["avgBallPos"])/data["px2mm"];
var viewDistance = ball_sqr_distance/Math.radians(angle)
console.log(Math.radians(angle))
data["viewDistance_mm"] = distanceSetup.round(viewDistance, 2);
//counter and stop
@ -165,36 +178,29 @@ jsPsych.plugins['virtual-chin'] = (function() {
counter = counter - 1;
$('#click').text(Math.max(counter, 0));
if (counter <= 0) {
ball.stop();
// Disable space key
$('html').bind('keydown', function(e)
{
if (e.keyCode == 32) {return false;} //32 is spacebar
if (e.keyCode == 32) {return false;} //32 is spacebar I CHANGE THAT
});
// // Display data
// $('#info').css("visibility", "visible");
// $('#info-h').append(data["viewDistance_mm"]/10)
// Display data
$('#info').css("visibility", "visible");
$('#info-h').append(data["viewDistance_mm"]/10)
//Estimated viewing distance in centimeters
trial_data.viewing_distance_cm = (data["viewDistance_mm"]/10); // add to trial_data
console.log(data["viewDistance_mm"]/10);
dist = Math.round(data["viewDistance_mm"]/10)
// The trial must end
end_trial();
// You can then DO SOMETHING HERE TO PROCEED TO YOUR NEXT STEPS OF THE EXPERIMENT. For example, add a button to go to the next page.
// display_element.innerHTML = `<p>"Press space bar to start the experiment.</p>`
display_element.innerHTML =
"<p style='font-size:160%;'> When you are ready, please press space bar to start the experiment.</p>" +
`<p style='font-size:160%;'> Your viewing distance is about ${dist.toString()} cm </p>`
end_trial()
return trial_data.viewing_distance_cm;
}
@ -212,11 +218,10 @@ jsPsych.plugins['virtual-chin'] = (function() {
// You can write functions here that live only in the scope of plugin.trial
function show_stimulus(){
display_element.innerHTML = `<p>Please use any credit card that you have available (it can also be a grocery store membership
card, your drivers license, or anything that is of the same format), hold it onto the screen, and adjust the slider below to its size.</p>`
// create html for display
var html = "<body><div id='content'><div id='page-size'>";
var html = "<body><div id='content'><div id='page-size'><br><br><br><br><br><br>";
// html += "<h3> Lets find out what your monitor size is (click to go into <div onclick='fullScreen(); registerClick();' style='display:inline; cursor:pointer; color: red'><em><u>full screen mode</u></em></div>).</h2>";
html += "<p>Please use any credit card that you have available (it can also be a grocery store membership card, your drivers license, or anything that is of the same format), hold it onto the screen, and adjust the slider below to its size.</p>";
@ -245,17 +250,21 @@ jsPsych.plugins['virtual-chin'] = (function() {
html += "Hit 'space' <div id='click' style='display:inline; color: red; font-weight: bold'>5</div> more times!</div>";
display_element.innerHTML = html; //
// render
display_element.innerHTML = html;
//Event listeners for buttons
document.getElementById("btnBlindSpot").addEventListener('click', function() {
console.log('presionaste el boton 1');
configureBlindSpot();
});
document.getElementById("start").addEventListener('click', function() {
console.log('presionaste el boton 2');
animateBall();
});
jsPsych.pluginAPI.getKeyboardResponse({
callback_function: after_response, // we need to create after_response
valid_responses: [trial.key], // valid_responses expects an array
@ -264,42 +273,19 @@ jsPsych.plugins['virtual-chin'] = (function() {
allow_held_key: true // false for a new key pressing in order to get a new response
});
}
// <div id="container">
// <div id="slider"></div>
// <br>
// <img id="card" src="card.png" style="width: 50%">
// <br><br>
// <button class="btn btn-primary" onclick="configureBlindSpot()">Click here when you are done!</button>
// </div>
// </div>
// scales the stimulus
// var scale_factor;
// var final_height_px, final_width_px;
// final_width_px = trial.cardWidth_px;
// function scale() {
// final_width_px = scale_div.offsetWidth;
// //final_height_px = scale_div.offsetHeight;
// var pixels_unit_screen = final_width_px / trial.item_width;
// scale_factor = pixels_unit_screen / trial.pixels_per_unit;
// document.getElementById("jspsych-content").style.transform = "scale(" + scale_factor + ")";
// };
function after_response(response_info){
// rt.push(response_info.rt); // response time of the key
// scale() // Esto lo agregue pero no creo que quede
end_trial();
}
function end_trial(){
// document.getElementsByClassName("jspsych-content-wrapper")[0].style.backgroundColor = 'gray'; //Background color
// trial_data.viewingDistance= JSON.stringify(viewingDistance); // best practice for saving in jsPsych. It is a JSON instead of array.
jsPsych.finishTrial(trial_data); // ends trial and save the data
display_element.innerHTML = ' '; // clear the display
// display_element.innerHTML = ' '; // clear the display
jsPsych.pluginAPI.cancelAllKeyboardResponses();
}
show_stimulus();
@ -308,5 +294,3 @@ jsPsych.plugins['virtual-chin'] = (function() {
return plugin;
})();