-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
427 lines (379 loc) · 15.3 KB
/
index.html
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>FTC Field Path Drawer</title>
<link rel="icon" href="canvas.jpg" type="image/jpg">
<style>
body {
font-family: Arial, sans-serif;
margin: 0;
padding: 0;
background-color: #f0f0f0;
color: #333;
line-height: 1.6;
}
#mazeCanvas {
border: 1px solid black;
display: block;
margin: 20px auto;
}
#controls {
background-color: white;
border: 1px solid #ccc;
border-radius: 5px;
padding: 10px;
margin-top: 10px;
max-width: 600px;
margin: 20px auto;
}
.active {
background-color: #ddd;
}
#menu {
position: absolute;
top: 10px;
right: 10px;
background: white;
border: 1px solid #ccc;
padding: 10px;
border-radius: 5px;
}
#menu a {
text-decoration: none;
color: #000;
display: block;
margin-bottom: 5px;
}
#menu a:hover {
background-color: #ddd;
}
#codeSection {
margin-top: 20px;
max-width: 600px;
margin-left: auto;
margin-right: auto;
}
selectCodeButton {
display: block;
margin-bottom: 10px;
}
#codeOutput {
background-color: #f8f8f8;
border: 1px solid #ddd;
border-radius: 4px;
padding: 10px;
white-space: pre-wrap;
word-wrap: break-word;
}
</style>
<script>
// Konami Code sequence
const konamiCode = ['ArrowUp', 'ArrowUp', 'ArrowDown', 'ArrowDown', 'ArrowLeft', 'ArrowRight', 'ArrowLeft', 'ArrowRight', 'b', 'a', 'Escape'];
let konamiCodePosition = 0;
document.addEventListener('keydown', function(e) {
// Check if the key pressed matches the next key in the Konami Code sequence
if (e.key === konamiCode[konamiCodePosition]) {
konamiCodePosition++;
// If the full sequence is entered, redirect to games.html
if (konamiCodePosition === konamiCode.length) {
window.location.href = 'games.html';
}
} else {
// Reset the sequence if an incorrect key is pressed
konamiCodePosition = 0;
}
});
</script>
</head>
<body>
<div id="menu">
<a href="index.html">Home</a>
<a href="field.html">FTC Field Path Drawer</a>
</div>
<h1 style="text-align: center;">FTC Field Path Drawer</h1>
<canvas id="mazeCanvas"></canvas>
<div id="controls">
<input type="file" id="imageUpload" accept="image/*">
<button id="clearButton">Clear Path</button>
<button id="saveButton">Save Path</button>
<input type="file" id="loadFile" accept=".json">
<div id="distanceOutput">Total distance: 0 inches</div>
<button id="pointMode" class="active">Point Mode</button>
<button id="freehandMode">Freehand Mode</button>
<button id="addPointMode">Add Snap Point</button>
<input type="number" id="snapX" placeholder="Snap X">
<input type="number" id="snapY" placeholder="Snap Y">
<button id="addSnapPoint">Add Snap Point</button>
<input type="color" id="lineColor" value="#ff0000">
<select id="pathType">
<option value="spline">Spline To</option>
<option value="line">Line To</option>
</select>
<div id="codeSection">
<button id="selectCodeButton">Select All Code</button>
<pre id="codeOutput">// Road Runner code will appear here</pre>
</div>
<script>
const canvas = document.getElementById('mazeCanvas');
const ctx = canvas.getContext('2d');
const imageUpload = document.getElementById('imageUpload');
const clearButton = document.getElementById('clearButton');
const saveButton = document.getElementById('saveButton');
const loadFile = document.getElementById('loadFile');
const distanceOutput = document.getElementById('distanceOutput');
const pointModeButton = document.getElementById('pointMode');
const freehandModeButton = document.getElementById('freehandMode');
const addPointModeButton = document.getElementById('addPointMode');
const codeOutput = document.getElementById('codeOutput');
const snapXInput = document.getElementById('snapX');
const snapYInput = document.getElementById('snapY');
const addSnapPointButton = document.getElementById('addSnapPoint');
const lineColorInput = document.getElementById('lineColor');
const pathTypeSelect = document.getElementById('pathType');
const selectCodeButton = document.getElementById('selectCodeButton');
const FIELD_SIZE = 144; // 144 inches
const PIXELS_PER_INCH = 600 / FIELD_SIZE; // Canvas size / Field size
const CANVAS_SIZE = 600;
let totalDistance = 0;
let baseImage = null;
let paths = [];
let snapPoints = [];
let drawMode = 'point';
let isDrawing = false;
let lineColor = lineColorInput.value;
canvas.width = CANVAS_SIZE;
canvas.height = CANVAS_SIZE;
function canvasToField(x, y) {
return {
x: Math.round((canvas.height / 2 - y) / PIXELS_PER_INCH),
y: Math.round((x - canvas.width / 2) / PIXELS_PER_INCH)
};
}
function fieldToCanvas(x, y) {
return {
x: y * PIXELS_PER_INCH + canvas.width / 2,
y: canvas.height / 2 - x * PIXELS_PER_INCH
};
}
function snapToPoint(x, y) {
const SNAP_DISTANCE = 10; // pixels
for (let point of snapPoints) {
const canvasPoint = fieldToCanvas(point.x, point.y);
const distance = Math.sqrt(Math.pow(x - canvasPoint.x, 2) + Math.pow(y - canvasPoint.y, 2));
if (distance < SNAP_DISTANCE) {
return point;
}
}
return canvasToField(x, y);
}
imageUpload.addEventListener('change', function(e) {
const file = e.target.files[0];
const reader = new FileReader();
reader.onload = function(event) {
baseImage = new Image();
baseImage.onload = function() {
drawBaseImage();
}
baseImage.src = event.target.result;
}
reader.readAsDataURL(file);
});
pointModeButton.addEventListener('click', function() {
drawMode = 'point';
pointModeButton.classList.add('active');
freehandModeButton.classList.remove('active');
addPointModeButton.classList.remove('active');
});
freehandModeButton.addEventListener('click', function() {
drawMode = 'freehand';
freehandModeButton.classList.add('active');
pointModeButton.classList.remove('active');
addPointModeButton.classList.remove('active');
});
addPointModeButton.addEventListener('click', function() {
drawMode = 'addPoint';
addPointModeButton.classList.add('active');
pointModeButton.classList.remove('active');
freehandModeButton.classList.remove('active');
});
addSnapPointButton.addEventListener('click', function() {
const x = parseInt(snapXInput.value);
const y = parseInt(snapYInput.value);
if (!isNaN(x) && !isNaN(y)) {
snapPoints.push({x, y});
drawBaseImage();
}
});
lineColorInput.addEventListener('change', function(e) {
lineColor = e.target.value;
redrawPath();
});
pathTypeSelect.addEventListener('change', function() {
redrawPath();
generateRoadrunnerCode();
});
selectCodeButton.addEventListener('click', function() {
const range = document.createRange();
range.selectNodeContents(codeOutput);
const selection = window.getSelection();
selection.removeAllRanges();
selection.addRange(range);
});
function drawBaseImage() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
if (baseImage) {
ctx.drawImage(baseImage, 0, 0, canvas.width, canvas.height);
}
drawSnapPoints();
redrawPath();
}
function drawSnapPoints() {
ctx.fillStyle = 'blue';
for (let point of snapPoints) {
const canvasPoint = fieldToCanvas(point.x, point.y);
ctx.beginPath();
ctx.arc(canvasPoint.x, canvasPoint.y, 5, 0, 2 * Math.PI);
ctx.fill();
}
}
function recalculateDistance() {
totalDistance = 0;
for (let i = 1; i < paths.length; i++) {
const distance = Math.sqrt(Math.pow(paths[i].x - paths[i-1].x, 2) + Math.pow(paths[i].y - paths[i-1].y, 2));
totalDistance += distance;
}
distanceOutput.textContent = `Total distance: ${totalDistance.toFixed(2)} inches`;
generateRoadrunnerCode();
}
canvas.addEventListener('mousedown', startDrawing);
canvas.addEventListener('mousemove', draw);
canvas.addEventListener('mouseup', stopDrawing);
canvas.addEventListener('mouseout', stopDrawing);
clearButton.addEventListener('click', clearPath);
saveButton.addEventListener('click', savePath);
loadFile.addEventListener('change', loadPath);
function startDrawing(e) {
if (drawMode === 'freehand') {
isDrawing = true;
const fieldCoords = snapToPoint(e.offsetX, e.offsetY);
paths.push({ x: fieldCoords.x, y: fieldCoords.y });
redrawPath();
} else if (drawMode === 'point' || drawMode === 'addPoint') {
const fieldCoords = snapToPoint(e.offsetX, e.offsetY);
if (drawMode === 'point') {
paths.push({ x: fieldCoords.x, y: fieldCoords.y });
} else {
snapPoints.push(fieldCoords);
}
drawBaseImage();
}
}
function draw(e) {
if (!isDrawing || drawMode !== 'freehand') return;
const fieldCoords = snapToPoint(e.offsetX, e.offsetY);
paths.push({ x: fieldCoords.x, y: fieldCoords.y });
redrawPath();
}
function stopDrawing() {
isDrawing = false;
generateRoadrunnerCode();
}
function clearPath() {
paths = [];
drawBaseImage();
totalDistance = 0;
distanceOutput.textContent = 'Total distance: 0 inches';
generateRoadrunnerCode();
}
function savePath() {
const pathData = JSON.stringify({
paths: paths,
snapPoints: snapPoints,
lineColor: lineColor
});
const blob = new Blob([pathData], {type: 'application/json'});
const url = URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = 'path.json';
a.click();
URL.revokeObjectURL(url);
}
function loadPath(event) {
const file = event.target.files[0];
if (file) {
const reader = new FileReader();
reader.onload = function(e) {
try {
const data = JSON.parse(e.target.result);
paths = data.paths || [];
snapPoints = data.snapPoints || [];
lineColor = data.lineColor || '#ff0000';
lineColorInput.value = lineColor;
drawBaseImage();
recalculateDistance();
} catch (error) {
alert('Error loading file: ' + error.message);
}
};
reader.readAsText(file);
}
}
function redrawPath(recalculate = true) {
ctx.clearRect(0, 0, canvas.width, canvas.height);
if (baseImage) {
ctx.drawImage(baseImage, 0, 0, canvas.width, canvas.height);
}
ctx.beginPath();
for (let i = 0; i < paths.length; i++) {
const canvasCoords = fieldToCanvas(paths[i].x, paths[i].y);
if (i === 0) {
ctx.moveTo(canvasCoords.x, canvasCoords.y);
} else {
ctx.lineTo(canvasCoords.x, canvasCoords.y);
}
}
ctx.strokeStyle = lineColor;
ctx.lineWidth = 2;
ctx.stroke();
// Draw points
for (let i = 0; i < paths.length; i++) {
const point = paths[i];
const canvasPoint = fieldToCanvas(point.x, point.y);
ctx.fillStyle = lineColor;
ctx.beginPath();
ctx.arc(canvasPoint.x, canvasPoint.y, 3, 0, 2 * Math.PI);
ctx.fill();
}
if (recalculate) {
recalculateDistance();
}
}
function generateRoadrunnerCode() {
if (paths.length < 2) {
codeOutput.textContent = "// Not enough points to generate a path";
return;
}
const pathType = pathTypeSelect.value;
let code = 'TrajectorySequence trajectory = drive.trajectorySequenceBuilder(new Pose2d())\n';
// Start pose
code += ` .setStartPose(new Pose2d(${paths[0].y.toFixed(2)}, ${paths[0].x.toFixed(2)}, 0))\n`;
// Generate trajectory based on selected path type
for (let i = 1; i < paths.length; i++) {
if (pathType === 'spline') {
code += ` .splineTo(new Vector2d(${paths[i].y.toFixed(2)}, ${paths[i].x.toFixed(2)}), 0)\n`;
} else {
code += ` .lineTo(new Vector2d(${paths[i].y.toFixed(2)}, ${paths[i].x.toFixed(2)}))\n`;
}
}
code += ' .build();';
codeOutput.textContent = code;
}
// Initial setup
drawBaseImage();
generateRoadrunnerCode();
</script>
</body>
</html>