-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
140 lines (120 loc) · 4.43 KB
/
script.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
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
const canvas = document.getElementById('drawingCanvas');
const ctx = canvas.getContext('2d');
const colorPicker = document.getElementById('colorPicker');
const startTimeInput = document.getElementById('startTime');
const endTimeInput = document.getElementById('endTime');
const timeDeltaInput = document.getElementById('timeDelta');
const newPathButton = document.getElementById('newPathButton');
const clearAllButton = document.getElementById('clearAllButton');
const clearLastButton = document.getElementById('clearLastButton');
const exportButton = document.getElementById('exportButton');
let isDrawing = false;
let currentPath = [];
let paths = [];
let currentColor = '#000000';
let startTime = 0;
let endTime = 1000;
let timeDelta = 100;
newPathButton.addEventListener('click', startNewPath);
canvas.addEventListener('mousedown', startDrawing);
canvas.addEventListener('mousemove', draw);
canvas.addEventListener('mouseup', stopDrawing);
canvas.addEventListener('mouseleave', stopDrawing);
clearAllButton.addEventListener('click', clearAllPaths);
clearLastButton.addEventListener('click', clearLastPath);
exportButton.addEventListener('click', exportPaths);
colorPicker.addEventListener('input', e => currentColor = e.target.value);
startTimeInput.addEventListener('input', e => startTime = parseInt(e.target.value));
endTimeInput.addEventListener('input', e => endTime = parseInt(e.target.value));
timeDeltaInput.addEventListener('input', e => timeDelta = parseInt(e.target.value));
function startNewPath() {
currentPath = [];
}
function startDrawing(event) {
if (currentPath.length === 0) {
isDrawing = true;
currentPath.push({ x: event.offsetX, y: event.offsetY });
ctx.beginPath();
ctx.moveTo(event.offsetX, event.offsetY);
ctx.strokeStyle = currentColor;
}
}
function draw(event) {
if (!isDrawing) return;
const { offsetX: x, offsetY: y } = event;
ctx.lineTo(x, y);
ctx.stroke();
currentPath.push({ x, y });
}
function stopDrawing() {
if (!isDrawing) return;
isDrawing = false;
ctx.closePath();
if (currentPath.length > 1) {
paths.push({ color: currentColor, path: currentPath, startTime, endTime, timeDelta });
}
}
function clearAllPaths() {
paths = [];
redrawPaths();
}
function clearLastPath() {
paths.pop();
redrawPaths();
}
function redrawPaths() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
paths.forEach(({ color, path }) => {
ctx.beginPath();
ctx.strokeStyle = color;
ctx.moveTo(path[0].x, path[0].y);
for (let i = 1; i < path.length; i++) {
ctx.lineTo(path[i].x, path[i].y);
}
ctx.stroke();
ctx.closePath();
});
}
/**
* Convert a 2D array into a CSV string
* Source: https://stackoverflow.com/questions/14964035/how-to-export-javascript-array-info-to-csv-on-client-side
*/
function arrayToCsv(data) {
return data.map(row =>
row
.map(String) // convert every value to String
.map(v => v.replaceAll('"', '""')) // escape double quotes
.map(v => `"${v}"`) // quote it
.join(',') // comma-separated
).join('\r\n'); // rows starting on new lines
}
/**
* Download contents as a file
* Source: https://stackoverflow.com/questions/14964035/how-to-export-javascript-array-info-to-csv-on-client-side
*/
function downloadBlob(content, filename, contentType) {
// Create a blob
const blob = new Blob([content], { type: contentType });
const url = URL.createObjectURL(blob, { oneTimeOnly: true });
// Create a link to download it
let link = document.createElement('a');
link.href = url;
link.setAttribute('download', filename);
link.click();
}
function exportPaths() {
let sampledPoints = [['X', 'Y', 'Date', 'TrackId']];
for (let pathId = 0; pathId < paths.length; pathId++) {
const { color, path, startTime, endTime, timeDelta } = paths[pathId];
const duration = endTime - startTime;
const totalPoints = Math.ceil(duration / timeDelta);
for (let i = 0; i <= totalPoints; i++) {
const t = i / totalPoints;
const index = Math.floor(t * (path.length - 1));
// x, y, time, id
sampledPoints.push([path[index].x, path[index].y, startTime + i * timeDelta, pathId]);
}
}
downloadBlob(arrayToCsv(sampledPoints), 'sampled_points.csv', 'text/csv;charset=utf-8');
console.log('successfully downloaded');
}