Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Seems something is wrong when trying to set requestPointerLock()

It seems something is wrong in my code, but I cannot figure what. I am trying to call pointerlockchange api to disable pointer and use mouse as fps controller. The problem is that always pointerlockerror is triggered and I get a Error setting pointer lock! message.

The function is called inside $(document).ready. Here's the code.:

function initLock() {
var havePointerLock = 'pointerLockElement' in document || 'mozPointerLockElement' in document || 'webkitPointerLockElement' in document;

if ( havePointerLock ) {
    var element = document.body;
    console.log('Cathing element', element);
    var pointerlockchange = function ( event ) {
        if ( document.pointerLockElement === element || document.mozPointerLockElement === element || document.webkitPointerLockElement === element ) {
            controls.enabled = true;
            console.log('Pointer lock enabled!')
        } 
        else {
            controls.enabled = false;
        }
    }

    var pointerlockerror = function ( event ) {
        console.log('Error setting pointer lock!');
    }

    // Hook pointer lock state change events
    document.addEventListener( 'pointerlockchange', pointerlockchange, false );
    document.addEventListener( 'mozpointerlockchange', pointerlockchange, false );
    document.addEventListener( 'webkitpointerlockchange', pointerlockchange, false );

    document.addEventListener( 'pointerlockerror', pointerlockerror, false );
    document.addEventListener( 'mozpointerlockerror', pointerlockerror, false );
    document.addEventListener( 'webkitpointerlockerror', pointerlockerror, false );


    // Ask the browser to lock the pointer
    element.requestPointerLock = element.requestPointerLock || element.mozRequestPointerLock || element.webkitRequestPointerLock;

    if ( /Firefox/i.test( navigator.userAgent ) ) {
        var fullscreenchange = function ( event ) {
            if ( document.fullscreenElement === element || document.mozFullscreenElement === element || document.mozFullScreenElement === element ) {
                document.removeEventListener( 'fullscreenchange', fullscreenchange );
                document.removeEventListener( 'mozfullscreenchange', fullscreenchange );
                element.requestPointerLock();
            }
        }

        document.addEventListener( 'fullscreenchange', fullscreenchange, false );
        document.addEventListener( 'mozfullscreenchange', fullscreenchange, false );

        element.requestFullscreen = element.requestFullscreen || element.mozRequestFullscreen || element.mozRequestFullScreen || element.webkitRequestFullscreen;
        element.requestFullscreen();
    } 
    else {
        element.requestPointerLock();
    }
} 
else {
    console.log('Sorry, pointer lock cannot be set.');
}

}

like image 750
kaytrance Avatar asked Nov 08 '13 08:11

kaytrance


1 Answers

Found solution by myself. The problem is that requestPointerLock cannot be called automatically, it should be called from the user's callback, for example, when clicking on something.

like image 194
kaytrance Avatar answered Oct 19 '22 01:10

kaytrance