-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcursorSpeed.js
More file actions
55 lines (46 loc) · 1.59 KB
/
cursorSpeed.js
File metadata and controls
55 lines (46 loc) · 1.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
function mouseSpeed(){
//create array to hold current mouse position
var mousePos = [1, 1],
lastPos = [1, 1],
difference = [1, 1],
distance;
window.addEventListener('mousemove', function(e){
// x position is saved in [0] and Y is saved in [1] positions
mousePos[0] = e.pageX;
mousePos[1] = e.pageY;
});
var pixelSpeed= setInterval(function(){
//find distance between the mouse positions along x axis
//using Pythagoreans theorem requires numbers to be positive is is used to subtract lower number from the higher one
if(mousePos[0] > lastPos[0]){
difference[0] = mousePos[0] - lastPos[0];
}
else if(mousePos[0] < lastPos[0]){
difference[0] = lastPos[0]-mousePos[0];
}
else{
difference[0] = 0; //if the positions are the same it has a distance of 0
}
//find distance between the mouse positions along y axis
if(mousePos[1] > lastPos[1]){
difference[1] = mousePos[1] - lastPos[1];
}
else if(mousePos[1] < lastPos[1]){
difference[1] = lastPos[1]-mousePos[1];
}
else{
difference[1] = 0; //if the positions are the same it has a distance of 0
}
//calculate distance using Pothagoreans Theorem
distance = Math.sqrt(Math.pow(difference[0],2)+Math.pow(difference[1],2));
//round distance to two decimal places
distance = Math.round(distance*4);
document.getElementById("output").innerHTML = distance + " Px/s";
// return distance;
// change value of last position to equal current for both x and y axis
lastPos[0] = mousePos[0];
lastPos[1] = mousePos[1];
}, 250);
}
// document.getElementById("output").innerHTML = mouseSpeed();
mouseSpeed();