kurokida / jspsych-psychophysics

A jsPsych plugin for psychophysics
https://kurokida.github.io/jspsych-psychophysics/
MIT License
51 stars 13 forks source link

Use function() to set the parameter of an object #9

Closed lawero closed 3 years ago

lawero commented 3 years ago

I hope this is not due to my low developer skills! If I try to give a value to a parameter of an object of jspsych-psychophysics using a function it doesn't work. height: function() {return 1000;},

Instead this works with the object of "basic" jspsych post_trial_gap: function(){return 1000;},

Here the example (remember to add the plugin for html-keyboard-response)

Thx!

var rect_object = {
    obj_type: 'rect', // means a rectangle
    startX: 200, // location in the canvas
    startY: 150,
    width: 300, // of the rectangle
    height: function() {return 1000;},
    line_color: '#ffffff',
    fill_color: '#ffffff',
    show_start_time: 0 // from the trial start (ms)
}

var circle_object = {
    obj_type: 'circle',
    startX: 500, // location in the canvas
    startY: 300,
    radius: 100,
    line_color: 'red', // You can use the HTML color name instead of the HEX color.
    fill_color: 'red',
    show_start_time: 1000 // from the trial start (ms)
}
var first = {
  obj_type: 'instruction',
  type: 'html-keyboard-response',
  stimulus: '<p>Welcome to exp1_visivo</p>',
  post_trial_gap: function(){return 1000;},
};
var trial = {
    type: 'psychophysics',
    stimuli: [rect_object, circle_object],
    choices: ['y', 'n'], // The participant can respond to the stimuli using the 'y' or 'n' key.
    canvas_width: 1000,
    canvas_height: 800,
    background_color: '#008000', // The HEX color means green.
}

jsPsych.init({
    timeline: [first,trial],
    on_finish: function(){jsPsych.data.displayData();}
});

lawero commented 3 years ago

height: function() {return 1000;},

I solved this way:

            on_start: function(){
              jsPsych.currentTrial().stimuli[0].height=1000;
            }

But I don't know why stim_array is Undefined. Despite the fact that with console.log( jsPsych.currentTrial()) I can see it.

kurokida commented 3 years ago

Thank you for your valuable comments.

Now, I changed the jspsych-psychphysics code to work with a function which is specified as a parameter of a stimulus object.

Please download the latest file from the top page of this github repository. (Please be careful not to download from the release page)

Then, strictly speaking, jsPsych.currentTrial().stimuli is not the same as the jsPsych.currentTrial().stim_array. You should use jsPsych.currentTrial().stim_array.

lawero commented 3 years ago

I know but the problem is that the same exact code with stim_array instead of stimuli doesn't work. :) The error reported is that stim_array is undefined. This is because my code is inside on_start: function() . Inside on_load: function() stim_array exist.

kurokida commented 3 years ago

Thank you for your reply. Now, I understood what you mean. As you said, the stim_array had not been made when the on_start function was called. Please use the latest code using which you can write as follows:


height: function() {return 1000;},