-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathterrain-generator.html
45 lines (45 loc) · 1.91 KB
/
terrain-generator.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
<!DOCTYPE html>
<html>
<head>
<title>Terrain Generator</title>
<link rel="shortcut icon" type="image/x-icon" href="favicon.ico">
<style>
body {background-color: #222; position: relative; left: 10px; width: 90vw; height: 90vh;}
a, h1, p, span {color: #fff; font-family: verdana;}
canvas {display: block; margin: 8px 0px;}
#seedValue {width: 512px;}
</style>
</head>
<body>
<h1>Terrain Generator</h1>
<p>Generate a 2D terrain map using trigonometry.</p>
<input type="range" id="seedValue" max=999 value=0><br>
<canvas id="myCanvas" width="512" height="512"></canvas>
<script>
let c = document.getElementById("myCanvas");
let ctx = c.getContext("2d");
setInterval(function() {
let seed = seedValue.value;
ctx.clearRect(0, 0, c.width, c.height);
ctx.fillStyle = "#48f";
ctx.fillRect(0, 0, 512, 512);
for (i = 0; i < 4096; i++) {
x = Math.floor(i % 64);
y = Math.floor(i / 64);
height = 0;
for (j = 0; j < 8; j++) {
height += Math.sin((x + 1024 * seed) / 8 * Math.sqrt(Math.PI) ** j) / Math.sqrt(Math.PI) ** j;
height += Math.cos((y + 1024 * seed) / 8 * Math.sqrt(Math.PI) ** j) / Math.sqrt(Math.PI) ** j;
height += Math.sin(seed * 256) / 4;
}
if (height > 0) {
ctx.fillStyle = height > 0.5 ? "hsl(120, 50%, " + (50 - Math.floor(height) * 3.125) + "%)" : "#ff8";
ctx.fillRect(8 * x, 8 * y, 8, 8);
}
}
currentSeedValue.innerHTML = "<b>Current Seed: " + "0".repeat(3 - String(seed).length) + seed + "</b>"
});
</script>
<span id="currentSeedValue"></span>
</body>
</html>