Search code examples
javascriptmousemovereplay

Replay mouse movement with javascript


I recently asked a question about mouse RECORDING. Now I need to figure out how to REPLAY it.

Recent question: https://stackoverflow.com/questions/8129723/record-mouse-movement-with-javascript

I will use PHP to make an identical copy of the current page, then I will insert the replay script in it. The script will add and move araound an absolut posisioned image acording to multiple x and y cordinates, in relation to time (to illustrate the mousemovement).

Are there any good methods (better then below) to replay mulitple mousemovements?

<input style="width:100%" type="text" name="onlyforstackoverflow1" value="0" size="4"><br>
<input style="width:100%" type="text" name="onlyforstackoverflow2" value="0" size="4">



<script>

// I want this (a very long array with x-cordinates, y-cordinates and time from pageload)

var very_long_array = [1,2,1000,2,22,2000,3,33,3645,4,44,3456];

// To become the same as this

setTimeout("document.Show.onlyforstackoverflow1.value = 1;document.Show.onlyforstackoverflow2.value = 11;",100)
setTimeout("document.Show.onlyforstackoverflow1.value = 2;document.Show.onlyforstackoverflow2.value = 22;",200)
setTimeout("document.Show.onlyforstackoverflow1.value = 3;document.Show.onlyforstackoverflow2.value = 33;",364)
setTimeout("document.Show.onlyforstackoverflow1.value = 4;document.Show.onlyforstackoverflow2.value = 44;",453)


// in the real script it will be moving around an image instead...

</script>

Solution

  • var dataList = [ 1, 2, 1000, 2, 22, 2000 ], // the long big array 
        preTime = 0;
    
    function run() {
        var parts = dataList.splice( 0, 3 ), // after splice, dataList will be auto updated
            nowTime;
    
        if ( parts.length ==  3 ) {
             nowTime = parts[ 2 ];
    
             setTimeout( function() {
                 replay( parts[ 0 ], parts[ 1 ] ); // x = parts[ 0 ], y = parts[ 1 ]
    
                 preTime = nowTime;
                 // continue run next replay
                 run();
             }, nowTime - preTime );
        } 
    }
    
    function replay( x, y ) {
        // do something with x, y;
        // document.Show.onlyforstackoverflow1.value = x;
        // document.Show.onlyforstackoverflow2.value = y;
    }
    
    // start
    run();
    

    Just use setTimeout to do the task, you needn't write every task as a statement :-)