-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmouseTracking.html
More file actions
207 lines (149 loc) · 4.44 KB
/
mouseTracking.html
File metadata and controls
207 lines (149 loc) · 4.44 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
<!DOCTYPE html>
<html>
<head>
<style>
#test {
position: absolute;
top: 10px;
left: 500px;
border: 5px green;
border-style: ridge;
padding: 5px;
}
#pos{
position: absolute;
top: 60px;
left: 500px;
border: 5px red;
border-style: ridge;
padding: 5px;
}
</style>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script>
$(document).ready(function() {
function timeStamp(time, xCoord, yCoord){
this.currentTime = time;
this.xCoord = xCoord;
this.yCoord = yCoord;
}
var posArray = [];
var myJSONString;
var xPos, yPos;
function getMousePosition(timeoutMilliSeconds) {
// "one" attaches the handler to the event and removes it after it has executed once
$(document).one("mousemove", function (event) {
window.mouseXPos = event.pageX;
window.mouseYPos = event.pageY;
xPos = window.mouseXPos;
yPos = window.mouseYPos;
//Drawing dots live
var x = window.mouseXPos;
var y = window.mouseYPos;
var size = '3px';
var color = '#FF0000';
$("body").append(
$('<div></div>')
.css('position', 'absolute')
.css('top', y + 'px')
.css('left', x + 'px')
.css('width', size)
.css('height', size)
.css('background-color', color)
);
//update location
var d = new Date();
var t = d.toLocaleTimeString();
var x = new timeStamp(t, window.mouseXPos, window.mouseYPos);
posArray.push(x);
// set a timeout so the handler will be attached again after a little while
setTimeout(function() { getMousePosition(timeoutMilliSeconds) }, timeoutMilliSeconds);
});
}
// start storing the mouse position every 100 milliseconds
getMousePosition(100);
//s = setInterval(storeMousePosition(), 100);
/*function storeMousePosition() {
var d = new Date();
var t = d.toLocaleTimeString();
var x = new timeStamp(t, window.mouseXPos, window.mouseYPos);
posArray.push(x);
}*/
function stopRecording() {
var out = "<table border='1'> ";
for(var i = 0; i < posArray.length; i++){
var a = posArray[i];
//How to take array of timeStamps and convert it into JSON-readable text?
out += "<tr><td>" +
a.currentTime + "</td><td>" +
a.xCoord + "</td><td>" +
a.yCoord + "</td></tr>";
}
out += "</table>";
document.getElementById("table").innerHTML = out;
//turning array of timeStamps into JSON string
myJSONString = JSON.stringify(posArray);
}
function dotMap() {
for(var i = 0; i < posArray.length; i++){
var x = posArray[i].xCoord;
var y = posArray[i].yCoord;
var size = '3px';
var color = '#FF0000';
$("body").append(
$('<div></div>')
.css('position', 'absolute')
.css('top', y + 'px')
.css('left', x + 'px')
.css('width', size)
.css('height', size)
.css('background-color', color)
);
}
}
$("#send").click(function(){
var exString = document.getElementById("header").innerHTML;
var URL = prompt("Please enter a url to send JSON data", "http://posttestserver.com/post.php");
if(URL != null){
$.post(URL, myJSONString, function(data, status){
alert("data: " + data + "\nstatus: " + status);
});
/*$.ajax({
type: "POST",
url: URL,
datatype : "json",
contentType: "application/json; charset=utf-8",
data : myJSONString,
success : function(result) {
alert(result.success); // result is an object which is created from the returned JSON
}
});*/
}
});
document.getElementById("stop").addEventListener("click", stopRecording);
document.getElementById("map").addEventListener("click", dotMap);
$('#display').click( function () {
document.getElementById("text").innerHTML = myJSONString;
});
//testing elementFromPoint() method
/* $("#header").click(function(){
console.log("click");
var el = document.elementFromPoint(xPos, yPos);
var text = $(el.firstChild.nodeValue);
console.log(text);
})*/
});
</script>
</head>
<body>
<h2 id="header">Mouse Locations</h2>
<button id="stop">Update Positions</button>
<button id="display">Display JSON</button>
<button id="send">Send Data to URL</button>
<button id="map">Show dot map</button>
<div id="table"></div>
<div id="text"></div>
<p id="test">Try Me</p>
<p id="pos"> </p>
<p id=""
</body>