root/trunk/website/blog/cfformprotect/js/mouseMovement.js @ 5

Revision 5, 2.3 kB (checked in by DanWilson, 17 years ago)

Initial Commit Of ModelGlue? Website (upgrade to blogcfc 511)

RevLine 
[5]1/* This code will grab two mouse coordinate sets from the user's mouse.
2 * The first set is shortly after the user starts moving thier mouse across the web page.
3 * The second set is grabbed a specified time later.  The distance between the two sets
4 * is calculated and stored in a form field
5 */
6
7//tell the browser to start executing the timedMousePos function every x milliseconds
8var myInterval = window.setInterval(timedMousePos,250)
9
10// Variables for mouse positions
11var xPos = -1;
12var yPos = -1;
13var firstX = -1;
14var firstY = -1;
15// variable to track how many times I polled the mouse position
16var intervals = 0;
17
18// retrieve mouse x,y coordinates
19function getMousePos(p) {
20        if (!p) var p = window.event;
21        if (p.pageX || p.pageY) {
22                xPos = p.pageX;
23                yPos = p.pageY;
24        }       else if (p.clientX || p.clientY) {
25                xPos = p.clientX + document.body.scrollLeft     + document.documentElement.scrollLeft;
26                yPos = p.clientY + document.body.scrollTop + document.documentElement.scrollTop;
27        }
28}
29
30// capture mouse movement
31function timedMousePos() {
32        //when the user moves the mouse, start working
33        document.onmousemove = getMousePos;
34        if (xPos >= 0 && yPos >= 0) {
35                //0,0 is a valid mouse position, so I want to accept that.  for this reason
36                //my vars are initialized to -1
37                var newX = xPos;
38                var newY = yPos;
39                intervals++;
40        }
41        if (intervals == 1) {
42                //store the first coordinates when we've got a pair (not when 'intervals' is 0)
43          firstX = xPos;
44        firstY = yPos;
45        } else if (intervals == 2) {
46                //I've got two coordinate sets
47                //tell the browser to stop executing the timedMousePos function
48        clearInterval(myInterval);
49                //send the 4 mouse coordinates to the calcDistance function
50        calcDistance(firstX,firstY,newX,newY);
51  }
52}
53
54function calcDistance(aX,aY,bX,bY) {
55        //use the Euclidean 2 dimensional distance formula to calculate the distance
56        //in pizels between the coordinate sets
57        var mouseTraveled = Math.round(Math.sqrt(Math.pow(aX-bX,2)+Math.pow(aY-bY,2)));
58        //ajax stuff to set a session variable
59        try     {
60                // Dave Shuck - 26 Mar 2007 - added try/catch for giant pages that take a while to
61                // load, in case the user moves their mouse before the page is completely rendered.
62                document.getElementById("formfield1234567891").value = mouseTraveled;
63        }
64        catch(excpt) { /* no action to take */ }
65}
66
67function dummy() {}
Note: See TracBrowser for help on using the browser.