Project

General

Profile

Feature #3306 » mouse_speed_poc.html

New implementation according to the ticket - Ayush Khandelwal, 11/06/2023 12:18 PM

 
1
<html>
2
  <head>
3
    <title>Mouse Speed test</title>
4
  </head>
5
  <body>
6
  	<div>
7
  		Coordinates:<br>
8
  		x = <p id="xCoordinate"></p>
9
  		<br>
10
  		y = <p id="yCoordinate"></p>
11
  	</div><br>
12
  	<div>
13
  		Direction: <br>
14
  		<p id="direction"></p>
15
  	</div><br>
16
  	<div>
17
  		Speed:<br>
18
  		x = <p id="xSpeed"></p>
19
  		<br>
20
  		y = <p id="ySpeed"></p>
21
  	</div>
22
    <script>
23
    	let lastMouseX, lastMouseY;
24
    	let lastTimestamp;
25
    	
26
    	document.addEventListener("mousemove", (e) => {
27
    		const currentMouseX = e.clientX;
28
    		document.getElementById("xCoordinate").innerHTML = currentMouseX;
29
    		const currentMouseY = e.clientY;
30
    		document.getElementById("yCoordinate").innerHTML = currentMouseY;
31
    		const currentTime = performance.now();
32
    		
33
    		
34
    		
35
    		console.log("x = "+currentMouseX+ ", y = "+currentMouseY);
36
    		
37
    		if(lastMouseX !== undefined && lastMouseY !== undefined) {
38
    			const deltaX = currentMouseX - lastMouseX;
39
    			const deltaY = currentMouseY - lastMouseY;
40
    			const deltaTime = currentTime - lastTimestamp;
41
    			
42
    			const speedX = deltaX / deltaTime;
43
    			const speedY = deltaY / deltaTime;
44
    			
45
    			document.getElementById("xSpeed").innerHTML = speedX;
46
    			document.getElementById("ySpeed").innerHTML = speedY;
47
    			
48
    			var direction = "";
49
    			if(speedX > 0) {
50
    				direction += " right";
51
    			} else if(speedX < 0) {
52
    				direction += " left";
53
    			}
54
    			
55
    			if(speedY > 0) {
56
    				direction += " bottom";
57
    			} else if(speedY < 0) {
58
    				direction += " top";
59
    			}
60
    			
61
    			document.getElementById("direction").innerHTML = direction;
62
    			
63
    			if(speedY <= -1 && currentMouseY < 20) {
64
    				// speed determine
65
    				console.log("speedX = "+speedX.toFixed(2)+ ", speedY = "+speedY.toFixed(2));
66
    				alert("trying to close");
67
    			}
68
    		}
69
    		
70
    		lastMouseX = currentMouseX;
71
    		lastMouseY = currentMouseY;
72
    		lastTimestamp = currentTime;
73
    	})
74
    </script>
75
  </body>
76
</html>
(2-2/3)