-
Notifications
You must be signed in to change notification settings - Fork 316
/
index.html
152 lines (128 loc) · 4.78 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
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0">
<title>Terrain Demo - PlayfulJS</title>
</head>
<body style='background: #000'>
<canvas id='display' width='1' height='1' />
<script>
function Terrain(detail) {
this.size = Math.pow(2, detail) + 1;
this.max = this.size - 1;
this.map = new Float32Array(this.size * this.size);
}
Terrain.prototype.get = function(x, y) {
if (x < 0 || x > this.max || y < 0 || y > this.max) return -1;
return this.map[x + this.size * y];
};
Terrain.prototype.set = function(x, y, val) {
this.map[x + this.size * y] = val;
};
Terrain.prototype.generate = function(roughness) {
var self = this;
this.set(0, 0, self.max);
this.set(this.max, 0, self.max / 2);
this.set(this.max, this.max, 0);
this.set(0, this.max, self.max / 2);
divide(this.max);
function divide(size) {
var x, y, half = size / 2;
var scale = roughness * size;
if (half < 1) return;
for (y = half; y < self.max; y += size) {
for (x = half; x < self.max; x += size) {
square(x, y, half, Math.random() * scale * 2 - scale);
}
}
for (y = 0; y <= self.max; y += half) {
for (x = (y + half) % size; x <= self.max; x += size) {
diamond(x, y, half, Math.random() * scale * 2 - scale);
}
}
divide(size / 2);
}
function average(values) {
var valid = values.filter(function(val) { return val !== -1; });
var total = valid.reduce(function(sum, val) { return sum + val; }, 0);
return total / valid.length;
}
function square(x, y, size, offset) {
var ave = average([
self.get(x - size, y - size), // upper left
self.get(x + size, y - size), // upper right
self.get(x + size, y + size), // lower right
self.get(x - size, y + size) // lower left
]);
self.set(x, y, ave + offset);
}
function diamond(x, y, size, offset) {
var ave = average([
self.get(x, y - size), // top
self.get(x + size, y), // right
self.get(x, y + size), // bottom
self.get(x - size, y) // left
]);
self.set(x, y, ave + offset);
}
};
Terrain.prototype.draw = function(ctx, width, height) {
var self = this;
var waterVal = this.size * 0.3;
for (var y = 0; y < this.size; y++) {
for (var x = 0; x < this.size; x++) {
var val = this.get(x, y);
var top = project(x, y, val);
var bottom = project(x + 1, y, 0);
var water = project(x, y, waterVal);
var style = brightness(x, y, this.get(x + 1, y) - val);
rect(top, bottom, style);
rect(water, bottom, 'rgba(50, 150, 200, 0.15)');
}
}
function rect(a, b, style) {
if (b.y < a.y) return;
ctx.fillStyle = style;
ctx.fillRect(a.x, a.y, b.x - a.x, b.y - a.y);
}
function brightness(x, y, slope) {
if (y === self.max || x === self.max) return '#000';
var b = ~~(slope * 50) + 128;
return ['rgba(', b, ',', b, ',', b, ',1)'].join('');
}
function iso(x, y) {
return {
x: 0.5 * (self.size + x - y),
y: 0.5 * (x + y)
};
}
function project(flatX, flatY, flatZ) {
var point = iso(flatX, flatY);
var x0 = width * 0.5;
var y0 = height * 0.2;
var z = self.size * 0.5 - flatZ + point.y * 0.75;
var x = (point.x - self.size * 0.5) * 6;
var y = (self.size - point.y) * 0.005 + 1;
return {
x: x0 + x / y,
y: y0 + z / y
};
}
};
var display = document.getElementById('display');
var ctx = display.getContext('2d');
var width = display.width = window.innerWidth;
var height = display.height = window.innerHeight;
var terrain = new Terrain(9);
terrain.generate(0.7);
terrain.draw(ctx, width, height);
</script>
<script>
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');
ga('create', 'UA-50885475-1', 'playfuljs.com');
ga('send', 'pageview');
</script>
</body>
</html>