-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgif.html
More file actions
288 lines (256 loc) · 6.87 KB
/
gif.html
File metadata and controls
288 lines (256 loc) · 6.87 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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Visualizing Phase Modulation</title>
<link href="https://fonts.googleapis.com/css?family=Cormorant+Infant" rel="stylesheet">
<script src="gif.js"></script>
<style>
html {padding:0; margin: 0;
background: repeating-radial-gradient(rgba(0,0,0,0.4) 0%,#eee 1px,#eee);
background-size: 13px 13px;
}
body {
font-family: 'Cormorant Infant', serif;
color: #66656B;
margin: 0 auto;
padding: 35px;
max-width: 600px;
background: #f9f7f5;
box-shadow: rgba(0,0,0,0.3) 0 0 25px 0;
}
canvas {
width:100%;
height:100%;
}
.half {
width:50%;
}
h1 {
font-size: 250%;
text-align: center;
}
#notation {
font-size: 40px;
text-align: left;
}
#notation>input { font-weight: bold; }
input {
line-height: 1em; height: 1em; display: inline-block; background: #f9f7f5; border: none;
width: 1em; font-size: 40px; font-family: 'Cormorant Infant', serif;
}
.r1 { color: #C65D1F; }
.r2, .r3 { color: #1F66C4; }
#r2, #r3 { width: 1.5em; }
#f, #a { width: 2.7em; text-align: right;}
#ft { color: #65907d; font-weight: bold;}
</style>
</head>
<body>
<h1><em>Phase Modulation</em></h1>
<canvas id="c" class=""></canvas>
<div id="notation">
<button onclick="dr();"> next frame</button>
<button onclick="aframe();"> add frame</button>
<button onclick="gif.render();"> render</button>
<button onclick="cycle();"> CYCLE </button>
<button onclick="cycle(123);"> CYCLE ADD</button>
</div>
<script>var d = document;
//CONSTANTS BUT THEY'RE VAR
var r1 = 3,
r2 = 2,
r3 = 0.5,
f = 40,
a = 0;
/**
d.getElementById("r1").addEventListener("change", function(e) {
r1 = e.target.value;
});
d.getElementById("r2").addEventListener("change", function(e) {
r2 = e.target.value;
});
d.getElementById("r3").addEventListener("change", function(e) {
r3 = e.target.value;
});
d.getElementById("f").addEventListener("change", function(e) {
f = e.target.value;
});
d.getElementById("a").addEventListener("change", function(e) {
a = e.target.value;
});
**/
// audio stuff
var aC = new AudioContext();
var node = aC.createScriptProcessor(4096, 0, 1);
var t = 0;
var dsp = function(t) {
return Math.sin(r1*Math.PI*2*t*f + r3*Math.PI*Math.sin(r2*Math.PI*2*t*f) )* a;
}
node.connect(aC.destination);
node.onaudioprocess = function(audioProcessingEvent) {
var obuffer = audioProcessingEvent.outputBuffer;
for( var ch=0; ch < obuffer.numberOfChannels; ch++ ){
var odata = obuffer.getChannelData(ch);
for(var i=0; i<obuffer.length; i++) {
odata[i] = dsp(t + (i/aC.sampleRate) );
}
}
t += obuffer.length/aC.sampleRate;
}
// visual stuff
var
c = d.getElementById("c"),
l = c.getBoundingClientRect(),
ctx = c.getContext("2d"),
w = l.width;
c.width = w; c.height = w ;
ctx.lineWidth = 3;
function drawBg() {
ctx.fillStyle = "#fff";
ctx.fillRect(0,0, w, w);
}
function drawBox(x, y, w, h, c) {
ctx.strokeStyle = c || "#77756B";
ctx.strokeRect(x, y, w, h)
}
function drawLine(x,y, m,n) {
ctx.beginPath();
ctx.moveTo(x,y);
ctx.lineTo(m,n);
ctx.stroke();
}
function drawWave(x,y, w,h, f, k) {
if(!k) {
ctx.beginPath();
moveTo( x + (w/2) + w*f(0) ,y);
for(var i = 0; i<h; i++) {
ctx.lineTo( x + (w/2) + (w/2)*f(i) , y+i);
}
ctx.stroke();
} else {
ctx.beginPath();
moveTo( x, y + (h/2) + h*f(0) );
for(var i = 0; i<w; i++) {
ctx.lineTo( x+i, y + (h/2) + (h/2)*f(i));
}
ctx.stroke();
}
}
var Wave = (function(f){
this.valueAt = function(z) {
return f(z);
}
return this;
});
var Buffe = (function(size) {
var buff = [];
for(var j=0; j<size; j++, buff[j]=0);
this.addValue = function(v) {
if(buff.length > size) buff.shift();
buff.push(v);
}
this.valueAt = function(i) {
return (buff[size-i])? buff[size-i]:0;
}
return this;
});
var _t = 0;
var zf = [];
var gif = new GIF({
workers: 5,
quality: 40,
repeat: 0,
width: 600,
height: 600
});
function aframe() {
gif.addFrame(ctx, { copy: true, delay: 60}); dr();
}
//carrier
var carrier = new Wave(function(z) {
return Math.sin( (-_t/1000)*r1 + 4*Math.PI*(z/(w/2)) );
});
//mod
var modu = new Wave(function(z) {
return Math.sin( ( _t/1000)*r2 + 4*Math.PI*(z/(w/2)));
});
//out
var output = new Buffe(~~w/2);
function dr() {
drawBg();
_t += 1000*Math.PI/72;
//carrier txt
ctx.font = "bold 22px monospace";
ctx.fillStyle="#C65D1F";
ctx.fillText("f₁ = "+r1, w/8 , w/8 -10);
//mod txt
ctx.font = "bold 22px monospace";
ctx.fillStyle="#1F66C4";
ctx.fillText("f₂ = "+r2, w*3/8 , w/8+ w*(2-r3)/8 -10);
//equation txt
ctx.font = "bold 22px monospace";
ctx.fillStyle="#C65D1F";
ctx.fillText("sin(f₁t )", w*3/8 , w*6/8 - 10);
ctx.fillStyle="#1F66C4";
ctx.fillText(" + π/2 sin(f₂t) ", w*3/8 , w*6/8 - 10);
ctx.strokeStyle = "#77756B";
//carrier
drawBox(w/8 , w/8 , w/8 , w/2 , "#C65D1F");
//modulator
drawBox(w*3/8 , w/8+ w*(2-r3)/8 , w/2 , w*r3/4, "#1F66C4");
//out
drawBox(w*3/8 , w*6/8 , w/2 , w/8 , "#65907d");
//reflector
drawLine(w/8 , w*6/8 , w/4 , w*7/8);
//carrier
ctx.strokeStyle = "#C65D1F";
drawWave(w/8, w/8, w/8, w/2, function(z) {
return carrier.valueAt(z);
});
//mod
ctx.strokeStyle = "#1F66C4";
drawWave(w*3/8, w/8+ w*(2-r3)/8, w/2, w*r3/4, function(z) {
return modu.valueAt(z);
}, 1);
//out
ctx.strokeStyle = "#65907d";
output.addValue(carrier.valueAt(w/4+w*r3/8*modu.valueAt(0)));
if( Math.round( carrier.valueAt(w/4+w*r3/8*modu.valueAt(0))*100 ) == 0 ) console.log("zero!"), zf.push(_t/30) ;
drawWave(w*3/8, w*6/8, w/2, w/8, function(z) {
return output.valueAt(z);
//return carrier.valueAt(w/4+z+w/16*modu.valueAt(0))
}, 1);
//mod to carrier
ctx.strokeStyle = "#1F66C4";
drawLine( w*3/8,
w*2.5/8 + w/16 + w*r3/8*modu.valueAt(0),
w/8 + w/16 + w/16*carrier.valueAt(w/4+w*r3/8*modu.valueAt(0)),
w*2.5/8 + w/16 + w*r3/8*modu.valueAt(0) );
//carrier to reflector
ctx.strokeStyle = "#C65D1F";
drawLine( w/8 + w/16 + w/16*carrier.valueAt(w/4+w*r3/8*modu.valueAt(0)),
w*2.5/8 + w/16 + w*r3/8*modu.valueAt(0),
w/8 + w/16 + w/16*carrier.valueAt(w/4+w*r3/8*modu.valueAt(0)),
w*6/8 + w/16 + w/16*carrier.valueAt(w/4+w*r3/8*modu.valueAt(0))
);
ctx.strokeStyle = "#65907d";
//reflector to output
drawLine( w/8 + w/16 + w/16*carrier.valueAt(w/4+w*r3/8*modu.valueAt(0)),
w*6/8 + w/16 + w/16*carrier.valueAt(w/4+w*r3/8*modu.valueAt(0)),
w*3/8,
w*6/8 + w/16 + w/16*carrier.valueAt(w/4+w*r3/8*modu.valueAt(0)));
//requestAnimationFrame(dr);
};
gif.on('finished', function(blob) {
window.open(URL.createObjectURL(blob));
});
function cycle(a) {
for(var i=0; i < 72*2; i++ ) {
if (!a || i%2 !== 0) { dr(); }
else { aframe(); }
}
}
</script>
</body>
</html>