-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
108 lines (99 loc) · 3.2 KB
/
app.js
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
class TrackingSession {
activeTouches = {}
records = []
screenScale = window.devicePixelRatio
screenSize = [
screen.width,
screen.height
]
handle(event, touches) {
if (touches.length == 3) {
this.export()
activeTouches = {}
records = []
return
}
for (let i = 0; i < touches.length; i++) {
const touch = touches.item(i)
switch (event) {
case "start":
const id = Math.floor(1e8 * Math.random()) + ""
this.activeTouches[touch.identifier] = id
this.records.push(new TouchRecord(event, touch, id))
break
case "move":
this.records.push(new TouchRecord(event, touch, this.activeTouches[touch.identifier]))
break
case "end":
this.records.push(new TouchRecord(event, touch, this.activeTouches[touch.identifier]))
delete this.activeTouches[touch.identifier]
break
}
}
}
export() {
const name = "TouchTracker Export"
const output = {
name: name ,
startTime: this.records[0].timestamp,
duration: this.records[this.records.length-1].timestamp - this.records[0].timestamp,
records: this.records,
screenSize: this.screenSize,
screenScale: this.screenScale
}
download(JSON.stringify(output, null, 2), name + " " + new Date().toLocaleString(), "application/json")
}
}
class TouchRecord {
touchId
event
position
force
timestamp
constructor(event, touch, id) {
this.touchId = id
this.event = event
const topOffset = screen.height - window.innerHeight
this.position = [
touch.screenX,
touch.screenY + topOffset
]
this.force = touch.force
this.timestamp = new Date().getTime() / 1000
}
}
function download(data, filename, type) {
var file = new Blob([data], {type: type});
if (window.navigator.msSaveOrOpenBlob) // IE10+
window.navigator.msSaveOrOpenBlob(file, filename);
else { // Others
var a = document.createElement("a"),
url = URL.createObjectURL(file);
a.href = url;
a.download = filename;
document.body.appendChild(a);
a.click();
setTimeout(function() {
document.body.removeChild(a);
window.URL.revokeObjectURL(url);
}, 0);
}
}
const session = new TrackingSession()
document.body.addEventListener('touchstart', function(e){
e.preventDefault()
session.handle("start", e.changedTouches)
});
document.body.addEventListener('touchmove', function(e){
e.preventDefault()
session.handle("move", e.changedTouches)
}, { passive: false });
document.body.addEventListener('touchend', function(e){
e.preventDefault()
console.log(e.changedTouches)
session.handle("end", e.changedTouches)
});
document.body.addEventListener('touchcancel', function(e){
e.preventDefault()
session.handle("end", e.changedTouches)
});