forked from josephg/noisejs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
demo.html
81 lines (69 loc) · 1.84 KB
/
demo.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
<!DOCTYPE html>
<title>Perlin noise</title>
<style>
.centerbox {
/* flexbox, por favor */
display: -webkit-box;
-webkit-box-orient: horizontal;
-webkit-box-pack: center;
-webkit-box-align: center;
display: -moz-box;
-moz-box-orient: horizontal;
-moz-box-pack: center;
-moz-box-align: center;
width: 100%;
height: 100%;
margin: 0; padding: 0;
}
body, html { width: 100%; height: 100%; padding: 0; margin: 0; }
canvas {
/* border-radius: 30px; Border radiuses don't seem to work with putImageData */
box-shadow: 0 0 10px #777;
width: 1024px;
height: 768px;
}
body {
background-color: #eee;
}
</style>
<div class='centerbox'><canvas></canvas></div>
<script src='perlin.js'></script>
<script>
var canvas = document.getElementsByTagName('canvas')[0];
canvas.width = 1024;
canvas.height = 768;
var ctx = canvas.getContext('2d');
var image = ctx.createImageData(canvas.width, canvas.height);
var data = image.data;
var start = Date.now();
for (var x = 0; x < canvas.width; x++) {
//if (x % 100 == 0) {
// noise.seed(Math.random());
//}
for (var y = 0; y < canvas.height; y++) {
var value = Math.abs(noise.perlin2(x / 100, y / 100));
value *= 256;
var cell = (x + y * canvas.width) * 4;
data[cell] = data[cell + 1] = data[cell + 2] = value;
data[cell] += Math.max(0, (25 - value) * 8);
data[cell + 3] = 255; // alpha.
}
}
/* // Benchmark code.
start = Date.now();
for (var x = 0; x < 10000; x++) {
for (var y = 0; y < 10000; y++) {
noise.simplex2(x / 50, y/50);
}
}*/
var end = Date.now();
ctx.fillColor = 'black';
ctx.fillRect(0, 0, 100, 100);
ctx.putImageData(image, 0, 0);
ctx.font = '16px sans-serif'
ctx.textAlign = 'center';
ctx.fillText('Rendered in ' + (end - start) + ' ms', canvas.width / 2, canvas.height - 20);
if(console) {
console.log('Rendered in ' + (end - start) + ' ms');
}
</script>