首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用jsPsych心理物理学插件中的“手动”属性调用函数来绘制刺激?

如何使用jsPsych心理物理学插件中的“手动”属性调用函数来绘制刺激?
EN

Stack Overflow用户
提问于 2021-01-20 11:08:52
回答 1查看 223关注 0票数 0

我一直试图使用“手动”对象属性来绘制光栅刺激。我用drawFunc编写了一个函数来绘制刺激图。但是,我希望在每次试验中使用context.filter更改一个输入参数,即对比度级别。我想在刺激变量之外定义函数和输入参数,并通过提供相关参数调用绘制补丁的函数。然而,这似乎行不通。每次我都不得不定义函数。有办法解决这个问题吗?

代码语言:javascript
复制
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();
    }
};

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

代码语言:javascript
复制
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();
}

//然后稍后再调用它

代码语言:javascript
复制
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)
} 

请帮助解决这个问题。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-01-21 20:49:50

您可以将drawFunc(...)包装在匿名函数中:

代码语言:javascript
复制
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(...)而不将其包装在匿名函数中,则该参数是函数的返回值,而不是函数本身。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65808349

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档