Dat.gui function invocation with parameters?

孤街浪徒 提交于 2019-12-13 04:43:16

问题


I am quite new with dat.gui. According to this little tutorial, you can invoke an object's function from the gui, just passing it to the gui's add function:

gui.add(fizzyText, 'explode');

Is it possible to pass arguments to the explode function?

Thanks!


回答1:


It is not possible to pass arguments to a button's function. You can however access other properties of your object in that function:

function myViewModel() {
    var self = this;

    this.name = "name1",
    this.score = 9,
    this.check = function() {
        if(self.score >= 5) { // access to the score property
            alert('you pass!');
        } else {
            alert('try again.');
        }
    }
};



回答2:


You can pass some arguments to explode() if you create through Function.bind() a copy of this function.

gui.add({explode : fizzyText.explode.bind(this, param_1, param_2)}, "explode");

Further information to Function.prototype.bind()


I also had to do it, to reuse my function and it worked fine for me like that :)



来源:https://stackoverflow.com/questions/26191484/dat-gui-function-invocation-with-parameters

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!