kurokida / jspsych-psychophysics

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

How do I change the contrast level on every trial? #15

Closed prashantig25 closed 3 years ago

prashantig25 commented 3 years ago

Hi! I am using a function called gabor to draw my stimulus which I am calling in the drawFunc function using the object property. In the gabor function, I want to change the contrast level randomly. That is, everytime the stimulus 'Left0' is presented, I want the gabor to have a different contrast level. I give pass the contrast level through the input parameter 'con'. When I use Math.random() to generate the contrast level, my gabor patch flickers because the contrast gets changed on every call of the drawFunc function. If I store Math.random () in a variable and then pass the variable in the stim.gabor function as an input parameter, the contrast level does not change for Left0 everytime the stimulus is used in a trial. I have set the number of repetitions to the number of times I want the trial to be repeated in the variable block50_50. I want the contrast level of the stimulus to be changed every time the trial is repeated in the block. Could you help me with it? var stim = { gabor: function draw(x1, y1, x2, y2, x, y, con) { context = jsPsych.currentTrial().context; const gradLength = 100; const my_gradient = context.createLinearGradient(x1, y1, x2, y2); const bands = 10; const colors = ["#000", "#FFF"]; const stops = bands * colors.length; var pos = 0; while (pos <= stops) { my_gradient.addColorStop(pos / stops, colors[pos % colors.length]); pos++; } context.fillStyle = my_gradient; context.filter = 'contrast(' + con + ')' context.fillRect(x, y, gradLength, gradLength); context.stroke() } } var Left0 = { obj_type: 'manual', horiz_pix_sec: 30, show_start_time: 0, motion_start_time: 2000, startX: 950, startY: 325, endX: 1150, endY: 325, // location in the canvas width: 300, height: 200, // of the rectangle drawFunc: function () { stim.gabor(400, 0, 600, 0, 500, 325, array[y]) } };

var trial1a = { type: 'psychophysics', stimuli: [Left0, Right0, fixation], choices: [37, 39], trial_duration: 2000, data: { stimulus_type: '1a' }, on_finish: function (data) { if (data.key_press == 39) { data.correct = true; } else { data.correct = false; } } }

var block50_50 = { timeline: [fix, trial1a, fb], repetitions: 5, randomize_order: true }

kurokida commented 3 years ago

Thank you for using my plugin! I don't know if I'm understanding your question correctly, but I will answer it.

You probably need to set the contrast in the on_start function.

FAQ9 may help you. https://jspsychophysics.hes.kyushu-u.ac.jp/faq.html

Could you see the randomize_show_start_time.html which is attached in this reply. randomize_show_start_time.txt

prashantig25 commented 3 years ago

Okay, thank you. Where should I use the on_start function to set the contrast?

prashantig25 commented 3 years ago

I could clarify my question. I just need the contrast level to change every time trial1a to be repeated when the stimulus Left0 is used? That is, if I repeat the trial trial1a for10 times, I want the contrast level of Left0 to change every time. I hope my question is clear now. Thank you!

kurokida commented 3 years ago

The on_start function is called every repetition time. So, I think you should set the contrast in the on_start function.


on_start: function(trial){
    trial.contrast = Math.random
}

// You can use the contrast inside the drawFunc as follows:
jsPsych.currentTrial().contrast

I'm sorry, but I'm not fully sure.

prashantig25 commented 3 years ago

Thank you! I will try these solutions.