-
Notifications
You must be signed in to change notification settings - Fork 0
/
terrain_design.html
78 lines (68 loc) · 1.8 KB
/
terrain_design.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Terrrain design</title>
<style>
* {
margin: 0;
padding: 0;
}
canvas {
border: 1px solid black;
height: 90vh;
}
</style>
</head>
<body>
<canvas height="1000" width="1000"></canvas><br>
<button>get and copy JSON output</button>
<input>
</body>
<script>
'use strict';
const canvas = document.querySelector('canvas');
const ctx = canvas.getContext('2d');
const input = document.querySelector('input');
const stuff = [];
let next_coords = null;
canvas.addEventListener('click', event => {
const coords = get_coords(canvas, event);
if (next_coords) {
const [x1, y1, x, y] = next_coords.concat(coords)
.map(n => Math.round(n/20) * 20);
const arr = Math.abs(x - x1) > Math.abs(y - y1) ?
[Math.min(x, x1), Math.min(y, y1), Math.abs(x - x1), 1] :
[Math.min(x, x1), Math.min(y, y1), 1, Math.abs(y - y1)];
ctx.fillRect(...enlarge(arr));
stuff.push(arr);
next_coords = null;
} else next_coords = coords;
});
function get_coords(canvas, { clientX, clientY }) {
const rect = canvas.getBoundingClientRect();
return [
(clientX - rect.left) * canvas.width / canvas.clientWidth,
(clientY - rect.top) * canvas.height / canvas.clientHeight
];
}
function enlarge(arr) {
return [
arr[0], arr[1],
arr[2] === 1 ? 3 : arr[2],
arr[3] === 1 ? 3 : arr[3]];
}
document.querySelector('button').addEventListener('click', () => {
input.value = JSON.stringify(stuff);
input.select();
document.execCommand('copy');
});
document.addEventListener('keydown', ({ ctrlKey, key }) => {
if (ctrlKey && key === 'z') {
if (stuff.length) ctx.clearRect(...enlarge(stuff.pop()));
next_coords = null;
}
});
</script>
</html>