简体   繁体   中英

How do you pass webgl functions as variables

I could not figure out how to do this. I keep getting an illegal invocation. Let's say that from a function in my program I call a function ...

setGlState( cGl.DEPTH_WRITEMASK, true, cGl.depthMask );

So I'm passing different states and functions to the setGlState function so that it can make some decisions and call the gl function if it needs to set state.

setGlState = function ( cap, value, setterFunc ) {
    ....

    if( needsToBeSent ) {
        setterFunc( value ) 
    }
}

For some reason this code does not work. Do I need to bind the function or call the function? Is it a problem that the function is on the gl context. I

You need to pass a new function that will call the old function in the right context (so this is cGl inside the function).

bind will do that for you.

setGlState( cGl.DEPTH_WRITEMASK.bind(cGl), true, cGl.depthMask.bind(cGl) );

The documentation linked to above includes a shim for browsers that do not natively support bind .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM