-
Notifications
You must be signed in to change notification settings - Fork 1
/
waterfall_new_html5.html
235 lines (191 loc) · 5.08 KB
/
waterfall_new_html5.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML//EN">
<html> <head>
<title></title>
</head>
<style>
h1 {
text-align: center;
}
ul#spectrum {
list-style: none;
text-align: center;
margin: 0px auto;
padding: 0px;
overflow: hidden;
font-size:0;
}
li {
display: block;
margin: 0px;
padding: 0px;
line-height: 0px;
font-size:0;
}
img {
margin: 0px;
padding: 0px;
}
canvas {
border: 2px solid #000;
position:absolute;
top:150;
left:150;
visibility:hidden;
}
</style>
<script>
var resolution = 128;
var samples = 64;
var waterfall = new Array();
var fake = new Array();
var sqsize;
var speed;
var oldSpeed = 0;
var updateTimer = "";
var c = document.createElement('canvas');
var pngs = new Array();
var img = new Image();
var k;
var Buffers = new Array();
var DrawingBuffer = 0;
// shortcut
function _(element) {
return document.getElementById(element);
}
// return random number between min and max
function getRandom (min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
// insert new random value to array
function insertWF() {
if (waterfall.length < samples) {
var insert = [];
for (var i = 0; i < resolution; i++) insert[i] = getRandom(1, 255);
waterfall.unshift(insert);
}
else {
waterfall.pop();
insertWF();
}
}
// draw waterfall
function displayWF() {
sqsize = _("sqsizeSlider").value;
speed = _("speedSlider").value;
resolution = _("resolutionSlider").value;
samples = _("samplesSlider").value;
_("sqsize").innerHTML = "Sqsize: " + sqsize;
_("speed").innerHTML = "Speed: " + 1000/(1000 - speed) + " Hz";
_("resolution").innerHTML = "Resolution: " + resolution;
_("samples").innerHTML = "samples: " + samples;
// set speed of waterfall
if(speed != oldSpeed) {
clearInterval(updateTimer);
updateTimer = setInterval(function() { updateWF()}, 1000 - speed);
oldSpeed = speed;
}
// c is globally defined context
var ctx = c.getContext("2d");
// c.width = resolution * sqsize;
// c.height = sqsize;
// ctx.clearRect(0, 0, ctx.width, ctx.height);
for (var j = 0; j < resolution; j++) {
ctx.fillStyle="rgb(" + waterfall[0][j] + ",50,50)";
ctx.fillRect(j * sqsize, 0, sqsize, sqsize);
}
// create png from canvas
while (pngs.length >= samples) {
pngs.pop();
}
var img = c.toDataURL("image/png");
pngs.unshift(img);
ctx = Buffers[DrawingBuffer].getContext('2d');
// Buffers[DrawingBuffer].width = resolution * sqsize;
// Buffers[DrawingBuffer].height = samples * sqsize;
for (i = 0; i < pngs.length; i++) {
img = new Image();
img.src = pngs[i];
img.onload = (function(img, i){ // temporary closure to store loop
return function () { // variables reference
ctx.drawImage(img,0,i*sqsize);
}
})(img, i);
}
Buffers[1-DrawingBuffer].style.visibility='hidden';
Buffers[DrawingBuffer].style.visibility='visible';
DrawingBuffer=1-DrawingBuffer;
}
// periodically called function which inserts new value
// and redraw the waterfall
function updateWF() {
insertWF();
displayWF();
}
window.onload = function ()
{
Buffers[0] = _("waterfall1");
Buffers[1] = _("waterfall2");
// populate array with values
for (var i = 0; i < samples; i++) {
insertWF();
}
// start waterfall
toggleWF();
}
// start / stop waterfall
function toggleWF() {
if(updateTimer == "")
{
updateTimer = setInterval( function() { updateWF() }, speed);
_("toggleButton").innerHTML = "Pause";
}
else
{
clearInterval(updateTimer);
updateTimer = "";
_("toggleButton").innerHTML = "Resume";
}
}
// save screenshot as png in "screenshots" div
function saveImg() {
var now = new Date();
// create hidden canvas
var buffer = document.createElement('canvas');
buffer.width = sqsize * resolution;
buffer.height = sqsize * samples;
context = buffer.getContext('2d');
// draw all data from array
for (var i = 0; i < samples; i++) {
for (var j = 0; j < resolution; j++) {
context.fillStyle="rgb(" + waterfall[i][j] + ",50,50)";
context.fillRect(j * sqsize, i * sqsize, sqsize, sqsize);
}
}
// convert canvas to png
var img = buffer.toDataURL("image/png");
_("screenshots").innerHTML += '<img alt="' + now + '" src="' + img + '"/> ' + now + '<br>';
}
</script>
<body>
<div>
square size <input id="sqsizeSlider" type ="range" min ="1" max="20" step ="1" value ="2"/>
<div id="sqsize"></div>
speed <input id="speedSlider" type ="range" min ="0" max="1000" step ="50" value ="750"/>
<div id="speed"></div>
resolution <input id="resolutionSlider" type ="range" min ="1" max="1000" step ="1" value ="128"/>
<div id="samples"></div>
samples <input id="samplesSlider" type ="range" min ="0" max="1000" step ="1" value ="64"/>
<div id="resolution"></div>
<button autofocus="autofocus" id="toggleButton" onclick="JavaScript:toggleWF()">Pause</button>
<button id="capture" onclick="JavaScript:saveImg()">Capture</button>
</div>
<div id="graf"></div>
<ul id="spectrum">
</ul>
<canvas id="waterfall1" width="500" height="500"> </canvas>
<canvas id="waterfall2" width="500" height="500">
<br>
<br>
<div id="screenshots"></div>
</body>
</html>