views:

64

answers:

2

I have a #wrapper div and a #grid div nested inside. currently I can scroll around with this function below.

getCursorPos : function(){

    // set the empty cursor object
    var cursor = {};

    //get the offset from the left of the grid container
    var grid
    //offset loop
    $(function getCursorPos(){
        grid = $('#grid').offset();
        setTimeout(getCursorPos, game.loopSpeed);
    });

    //continuosly get the position
    var that = this;
    $(document).mousemove(function(e){

        //if game mode is menu exit
        if(game.mode === 'menu'){
            return;
        }

        // NOTE: this looks a litle over done but don't remove anything
        //       its like this because javascript uses floating points
        //       and so in order to line up to the nearest hunderedth I
        //       had to make the cursor and div position intergers by 
        //       muliplying by ten. one the two are added I reduced them
        //       and rounded them. 
        that.x = Math.round(((e.pageX * 10) - (grid.left * 10)) / 10);
        that.y = Math.round(((e.pageY * 10) - (grid.top * 10)) / 10);
    });

},

the problem is that the mouse coordinates only update when the mouse moves. is there any way to get the coordinates with out moving the mouse?

A: 

You always have the latest up-to-date coordinates of the mouse from the last mouse move, clarify why those are not useful to you.

cDima
Because when javascript moves the grid within the wrapper, the cursor is no longer over the same spot. the grid moved beneath it. unless I move my mouse location is not updated.
Robert Hurst
@Robert: If the grid moves, your code surely knows by how many pixels it was moved. If you add this offset to the "old" mouse location, you have the new one, right?
Jørn Schou-Rode
@Jørn Yea actually thats what I didn't and it solved the problem.
Robert Hurst
A: 

Never mind I solved the problem, I just make the edge scroll script update the cursor position.

Robert Hurst