kurokida / jspsych-psychophysics

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

event handlers compatible with gabor parameters #27

Closed rouderj closed 2 years ago

rouderj commented 2 years ago

Hi, I can change certain gabor parameters with the event handler examples, for startX, but I cannot change others, such as tilt. for example, jsPsych.currentTrial().stim_array[0].startX = 50 changes the position as expected. jsPsych.currentTrial().stim_array[0].tilt=50 does not change the tilt. any thoughts? much thanks.

This example should change both tilt and startX, but it only shifts the gabor and does not change the tilt :(

let current = 90;

  var circle_obj = {
    obj_type: 'gabor',
    startX: current,
    startY: 150,
    tilt: current,
    sf: 0.05,
    phase: 90,
    width: 150,
    sc: 10,
    contrast: 0.5,
    disableNorm: true
  };

  const trial = {
      type: 'psychophysics',
      canvas_height: 500,
      prompt: '<p>Pressing the ArrowUp/ArrowDown key, the color of the circle will change. <br>Press the space key to finish the program.</p>',
      stimuli: [circle_obj], // These can be referenced using the jsPsych.currentTrial().stimuli array.
      response_type: 'key',
      choices: [' '],
      key_down_func: function(event){ // The key_up_func is also available. In that case, the color of the circle changes when you release the key. 
        if (event.key === 'ArrowUp'){
          current += 2;
        } else if (event.key === 'ArrowDown'){
          current -= 2;
        }

        // Note that when specify the space bar, you need to write event.key === ' ' not event.key === 'space'.

        jsPsych.currentTrial().stim_array[0].startX = current;
        jsPsych.currentTrial().stim_array[0].tilt = current;
      },
  }
kurokida commented 2 years ago

Thank you for using my plugin! I am very sorry for the delay in replying.

I think that the following code is probably close to the program you want.

    const nTrials = 20
    let trial_num = 1
    let current = 90;

    const trial = {
        type: 'psychophysics',
        canvas_height: 500,
        prompt: '<p>Press the ArrowUp/ArrowDown key to change the position and tilt of the gabor.</p>',
        stimuli: function(){
          const gabor_obj = {
              obj_type: 'gabor',
              startX: current,
              startY: 150,
              tilt: current,
              sf: 0.05,
              phase: 90,
              width: 150,
              sc: 10,
              contrast: 0.5,
              disableNorm: true
          };

          return [gabor_obj]
        },
        response_type: 'key',
        choices: [' ', 'ArrowUp', 'ArrowDown'],
    }

    const loop_node = {
        timeline: [trial],
        loop_function: function(data){
            if (trial_num < nTrials){
                trial_num++

                if (data.values()[0].response === 'arrowup'){
                  current += 2
                } else { // arrowdown
                  current -= 2
                }

                return true
            } else {
                return false
            }
        }
    }

    /* start the experiment */
    jsPsych.init({
        timeline: [loop_node],
        on_finish: function(){jsPsych.data.displayData();}
    });

Please see the looping timelines and the dynamic parameters, and note that my plugin is not compatible with jsPsych V7.

rouderj commented 2 years ago

Thank you very much! I am going to learn loop_function which is new to me.

kurokida commented 2 years ago

The psychophysics plugin is now compatible with jsPsych V7. Sorry to keep you waiting. You can download it from https://github.com/kurokida/jspsych-psychophysics/releases/tag/v3.0.0