如何使用 jsPsych 心理物理学插件中的 'manual' 属性调用函数来绘制刺激?



我一直在尝试使用"手动"对象属性来绘制光栅刺激。我已经写了一个函数来绘制刺激使用drawFunc。然而,我想改变一个输入参数,即使用上下文的对比水平。过滤,在每一次尝试中。我想在刺激变量之外定义函数和输入参数,并通过给出相关参数来调用绘制补丁的函数。然而,这似乎不起作用。我每次都必须定义这个函数。有办法解决这个问题吗?

var Left0 = {
obj_type: 'manual', // means a rectangle
startX: 550, // location in the canvas
startY: 325,
endX: 700,
endY: 325,
width: 300, // of the rectangle
height: 200,
horiz_pix_sec: 30,
show_start_time: 0,
motion_start_time: 2000,
drawFunc() {
context = jsPsych.currentTrial().context;
var pos = 0;
const gradLength = 100;
const my_gradient  = context.createLinearGradient(400, 0, 600, 0);
const bands = 10;
const colors = ["#000", "#FFF"];
const stops = bands * colors.length;
while (pos <= stops) {
my_gradient.addColorStop(pos / stops, colors[pos % colors.length]);
pos++;
}
context.filter = 'contrast('+ CL1 +')';
context.fillStyle = my_gradient;
context.fillRect(500,325,gradLength,gradLength);
context.stroke();
}
};

//我想只定义一次函数,就像这样。

function drawFunc (x1, y1, x2, y2, CL1, x,y) {
context = jsPsych.currentTrial().context;
var pos = 0;
const gradLength = 100;
const my_gradient  = context.createLinearGradient(x1, y1, x2, y2);
const bands = 10;
const colors = ["#000", "#FFF"];
const stops = bands * colors.length;
while (pos <= stops) {
my_gradient.addColorStop(pos / stops, colors[pos % colors.length]);
pos++;
}
context.filter = 'contrast('+ CL1 +')';
context.fillStyle = my_gradient;
context.fillRect(x,y,gradLength,gradLength);
context.stroke();
}

//之后再调用

var Left0 = {
obj_type: 'manual', // means a rectangle
startX: 550, // location in the canvas
startY: 325,
endX: 700,
endY: 325,
width: 300, // of the rectangle
height: 200,
horiz_pix_sec: 30,
show_start_time: 0,
motion_start_time: 2000,
drawFunc: drawFunc(400, 0, 600, 0, 0.5, 500, 325)
} 

请帮忙解决这个问题。

可以将drawFunc(...)封装在一个匿名函数中:

var Left0 = {
obj_type: 'manual', // means a rectangle
startX: 550, // location in the canvas
startY: 325,
endX: 700,
endY: 325,
width: 300, // of the rectangle
height: 200,
horiz_pix_sec: 30,
show_start_time: 0,
motion_start_time: 2000,
drawFunc: function(){
drawFunc(400, 0, 600, 0, 0.5, 500, 325)
}
}

这可以工作,因为drawFunc参数期望一个函数。当您使用drawFunc(...)而不将其包装在匿名函数中时,则参数是函数的返回值而不是函数本身。

最新更新