-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
141 lines (115 loc) · 3.71 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
<!DOCTYPE html>
<html>
<head>
<title>Mars lander map designer</title>
<style>
canvas {
border: 1px solid black;
}
</style>
</head>
<body oncontextmenu="return false;">
<br>
<br>
• Right mouse click to add starship<br>
• Left mouse click to add points, make sure to create only one green, horizotal landings line<br>
• Refresh page to reset<br>
<br>
<canvas id="myCanvas" width="700" height="300"></canvas>
<script>
const canvas = document.getElementById('myCanvas');
const context = canvas.getContext('2d');
let lastX, lastY;
let points = [];
// Draw rectangle
context.beginPath();
context.rect(0, 0, 700, 300);
context.stroke();
// Add event listener to canvas
canvas.addEventListener('mousedown', function(event) {
// Draw starship on right mouse click
if (event.button === 2) {
const img = new Image();
img.onload = function() {
const x = event.clientX - canvas.offsetLeft;
const y = event.clientY - canvas.offsetTop;
context.drawImage(img, x, y, 40, 40);
};
img.src = 'https://www.pngfind.com/pngs/m/261-2611783_take-command-of-an-advanced-starship-starship-png.png';
const textarea = document.getElementById('shipTextArea');
textarea.value = `x: ${event.clientX}, y: ${event.clientY}`;
}
// Draw red point on left mouse click
if (event.button === 0) {
const rect = canvas.getBoundingClientRect();
let x = event.clientX - rect.left;
let y = event.clientY - rect.top;
context.beginPath();
context.arc(x, y, 5, 0, Math.PI * 2);
context.fillStyle = 'red';
context.fill();
if (lastX && lastY) {
if (Math.abs(y - lastY) <= 5) {
y = lastY;
}
context.beginPath();
context.moveTo(lastX, lastY);
context.lineTo(x, y);
if (Math.abs(y - lastY) <= 5) {
context.strokeStyle = 'green';
} else {
context.strokeStyle = 'red';
}
context.lineWidth = 6;
context.stroke();
}
lastX = x;
lastY = y;
points.push([x*10,y*10]);
document.getElementById("pointsTextArea").value = JSON.stringify(points);
}
});
</script>
<br>
<br>
Points coordinates<br>
<textarea id="pointsTextArea" rows="10" cols="30"></textarea><br>
<!-- <button onclick="copyToClipboard()">Copy to Clipboard</button><br> -->
<br>
Ship coordinates<br>
<textarea id="shipTextArea" rows="4" cols="30"></textarea>
<script>
// Copy to clipboard
function copyToClipboard() {
const copyText = document.getElementById("pointsTextArea");
copyText.select();
copyText.setSelectionRange(0, 99999);
document.execCommand("copy");
//alert("Copied to clipboard: " + copyText.value);
}
</script>
<br>
<button onclick="saveAsJSON()">Save as JSON</button>
<script>
function saveAsJSON() {
const textarea = document.getElementById('shipTextArea');
const textarea2 = document.getElementById("pointsTextArea");
const data = {"ship": textarea.value + textarea2.value}; // TODO JSON
const json = JSON.stringify(data);
const blob = new Blob([json], { type: 'application/json' });
const url = URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = 'data.json';
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
URL.revokeObjectURL(url);
}
</script>
<br>
<br>
<br>
<a href="https://github.com/SamOgon-one/Mars-lander-level-designer">github source</a>
</body>
</html>