-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathprototype.html
More file actions
executable file
·267 lines (222 loc) · 8.04 KB
/
Copy pathprototype.html
File metadata and controls
executable file
·267 lines (222 loc) · 8.04 KB
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
<!DOCTYPE HTML>
<html>
<head>
<style>
* {
margin: 0px;
padding: 0px;
}
.canvas-area {
position: relative;
}
#canvas-1-wave {
position: absolute;
top:0px;
left: 0px;
}
#canvas-2-wave {
position: absolute;
top:0px;
left: 0px;
}
#canvas-1_2-wavesum {
position: absolute;
top:0px;
left: 0px;
}
</style>
</head>
<BODY>
<div class="canvas-area">
<canvas id="canvas-1-bg" width="578" height="200"></canvas>
<canvas id="canvas-1-wave" width="578" height="200"></canvas>
</div>
<div class="canvas-area">
<canvas id="canvas-2-bg" width="578" height="200"></canvas>
<canvas id="canvas-2-wave" width="578" height="200"></canvas>
</div>
<div class="canvas-area">
<canvas id="canvas-1_2-bg" width="578" height="200"></canvas>
<canvas id="canvas-1_2-wavesum" width="578" height="200"></canvas>
</div>
<script>
var wave, wavesum, bg = [];
var wavelengthRange, amplitudeRange, phaseRange, speedRange;
var WAVELENGTH_MAGNIFY = 5;
var time, timer, animateBtn;
init();
function init(){
time = 0;
wave[0] = new wave(1, 30, 90, 4, 0, 3, '#ecf0f1', 578, 200);
wave[1] = new wave(2, 40, 50, -10, 0, 3, '#ecf0f1', 578, 200);
wavesum[0] = new wavesum('1_2', wave[0], wave[1], 3, '#ecf0f1', 578, 200);
bg[0] = new background(1,'#2980b9','#3498db',.3,12,578,200);
bg[1] = new background(2,'#2980b9','#3498db',.3,12,578,200);
bg[2] = new background('1_2','#2980b9','#3498db',.3,12,578,200);
for(i in bg) drawBackground(bg[i]);
animateStart();
}
function range(min,max){
var r = new Object();
r.min = min;
r.max = max;
return r;
}
function animateStart() {
timer = setInterval(function() {
for(i in wave) drawWave(wave[i]);
for(i in wavesum) drawSum(wavesum[i]);
time++;
}, 1000 / 25);
}
function animateStop() {
clearInterval(timer);
timer = null;
}
function wave(idx, wavelength, amplitude, speed, phase, lineWidth, color, width, height){
var w = new Object();
w.idx = idx;
w.wavelength = wavelength;
w.amplitude = amplitude;
w.speed = speed;
w.phase = phase;
w.lineWidth = lineWidth;
w.color = color;
w.width = width;
w.height = height;
w.canvas = document.getElementById('canvas-'+idx+'-wave');
w.buffer = document.createElement('canvas');
w.buffer.width = w.width;
w.buffer.height = w.height;
return w;
}
function wavesum(idx, wave_a, wave_b, lineWidth, color, width, height){
var w = new Object();
w.idx = idx;
w.lineWidth = lineWidth;
w.color = color;
w.width = width;
w.height = height;
w.wave_a = wave_a;
w.wave_b = wave_b;
w.canvas = document.getElementById('canvas-'+idx+'-wavesum');
w.buffer = document.createElement('canvas');
w.buffer.width = w.width;
w.buffer.height = w.height;
return w;
}
function background(idx, bgColor, lineColor, lineWidth, increment, width, height){
var b = new Object();
b.idx = idx;
b.bgColor = bgColor;
b.lineColor = lineColor;
b.lineWidth = lineWidth;
b.increment = increment;
b.width = width;
b.height = height;
return b;
}
function drawWave(wave){
var idx = wave.idx;
var width = wave.width;
var height = wave.height;
var ctx = wave.buffer.getContext('2d');
ctx.clearRect(0,0,width,height);
ctx.strokeStyle = wave.color;
ctx.lineWidth = wave.lineWidth;
var phase = (wave.phase + time*wave.speed) * Math.PI / 180;
var amp = (wave.height * wave.amplitude) / (2 * 100) - 2;
var freq = 2 * Math.PI * (1 / (wave.wavelength * WAVELENGTH_MAGNIFY));
var yOrigin = wave.height / 2;
ctx.beginPath();
for ( var i = 0; i < wave.width; i++) {
var y1 = amp * Math.sin(phase + freq * i) + yOrigin;
var y2 = amp * Math.sin(phase + freq * (i + 1)) + yOrigin;
ctx.moveTo(i, y1);
ctx.lineTo(i + 1, y2);
}
ctx.stroke();
var front_ctx = wave.canvas.getContext("2d");
front_ctx.clearRect(0, 0, wave.width, wave.height);
front_ctx.drawImage(wave.buffer, 0, 0);
}
function drawSum(wavesum){
var idx = wavesum.idx;
var width = wavesum.width;
var height = wavesum.height;
var ctx = wavesum.buffer.getContext('2d');
ctx.clearRect(0,0,width,height);
ctx.lineWidth = wavesum.lineWidth;
var wave_a = wavesum.wave_a;
var wave_b = wavesum.wave_b;
var wave_a_color = 'rgba('+hexToRgb(wave_a.color).r+','+hexToRgb(wave_a.color).g+','+hexToRgb(wave_a.color).b+','+0.3+')';
var wave_b_color = 'rgba('+hexToRgb(wave_b.color).r+','+hexToRgb(wave_a.color).g+','+hexToRgb(wave_a.color).b+','+0.2+')';
var phase1 = (wave_a.phase + time*wave_a.speed) * Math.PI / 180;
var phase2 = (wave_b.phase + time*wave_b.speed) * Math.PI / 180;
var amp1 = (wave_a.height * wave_a.amplitude) / (2 * 100) - 2;
var amp2 = (wave_b.height * wave_b.amplitude) / (2 * 100) - 2;
var freq1 = 2 * Math.PI * (1 / (wave_a.wavelength * WAVELENGTH_MAGNIFY));
var freq2 = 2 * Math.PI * (1 / (wave_b.wavelength * WAVELENGTH_MAGNIFY));
var yOrigin = height / 2;
for ( var i = 0; i < width; i++) {
var y1_1 = amp1 * Math.sin(phase1 + freq1 * i);
var y1_2 = amp1 * Math.sin(phase1 + freq1 * (i + 1));
var y2_1 = amp2 * Math.sin(phase2 + freq2 * i);
var y2_2 = amp2 * Math.sin(phase2 + freq2 * (i + 1));
ctx.strokeStyle = wave_a_color;
ctx.beginPath();
ctx.moveTo(i, y2_1 + yOrigin);
ctx.lineTo(i + 1, y2_2 + yOrigin);
ctx.stroke();
ctx.strokeStyle = wave_b_color;
ctx.beginPath();
ctx.moveTo(i, y1_1 + yOrigin);
ctx.lineTo(i + 1, y1_2 + yOrigin);
ctx.stroke();
ctx.strokeStyle = wavesum.color;
ctx.beginPath();
ctx.moveTo(i, y1_1 + y2_1 + yOrigin);
ctx.lineTo(i + 1, y1_2 + y2_2 + yOrigin);
ctx.stroke();
}
var front_ctx = wavesum.canvas.getContext("2d");
front_ctx.clearRect(0, 0, width, height);
front_ctx.drawImage(wavesum.buffer, 0, 0);
}
function drawBackground(bgr){
var idx = bgr.idx;
var width = bgr.width;
var height = bgr.height;
var canvas = document.getElementById('canvas-'+idx+'-bg');
var ctx = canvas.getContext('2d');
ctx.fillStyle = bgr.bgColor;
ctx.fillRect(0,0,width,height);
ctx.strokeStyle = bgr.lineColor;
ctx.strokeWidth = bgr.lineWidth;
var increment = bgr.increment;
for(var i = 0; i < height/increment; i++){
ctx.moveTo(0,i*increment);
ctx.lineTo(width,i*increment);
}
for(var i = 0; i < width/increment; i++){
ctx.moveTo(i*increment,0);
ctx.lineTo(i*increment,height);
}
ctx.stroke();
}
function hexToRgb(hex) {
// Expand shorthand form (e.g. "03F") to full form (e.g. "0033FF")
var shorthandRegex = /^#?([a-f\d])([a-f\d])([a-f\d])$/i;
hex = hex.replace(shorthandRegex, function(m, r, g, b) {
return r + r + g + g + b + b;
});
var result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex);
return result ? {
r: parseInt(result[1], 16),
g: parseInt(result[2], 16),
b: parseInt(result[3], 16)
} : null;
}
</script>
</BODY>
</html>