Commits

Eric Fredricksen committed e723f6f

(Poorly) encapsulate static noise and switch it sometimes

Comments (0)

Files changed (1)

   fps.innerHTML = g;
 }
 
-var angle = Math.random() * Math.PI;
-var cos = Math.cos(angle);
-var sin = Math.sin(angle);
-var amp = 1 + Math.random() * 10;
-var rate = 50 + 500 * Math.random();
+function Wave(factor) {
+  var angle = Math.random() * Math.PI;
+  var cos = Math.cos(angle);
+  var sin = Math.sin(angle);
+  var amp = 1 + Math.random() * 10;
+  var rate = (50 + 500 * Math.random()) * amp/5;
+  var TAU = 2 * Math.PI;
+  return function (v, x, y) {
+    var tx = cos * x/amp - sin * 2*y/amp;
+    return ((1-factor) * v +
+            factor * (0.5 + 0.5 * Math.sin(tx + (new Date() / rate) % TAU)));
+  }
+}
+ 
+var wave, ot = 0;
 
 function noise(ctx) {
   var ascii = "";
   var msg = (MESSAGE||"").split('\n');
   var msgline = (H - msg.length) >> 1;
-  var C = 5000;
-  var P = 0.75;
+  var C = 7000;
+  var P = 0.85;
   var topline = 0;
   var t = (((+new Date() % C) / C) - P) / (1-P);
+  if (!wave || (t > 0 != ot > 0))
+    wave = new Wave(0.35);
   if (t > 0) {
     var topline = (H - t * H) >> 0;
     var skzzt = 3;
     var topline = 0;
     var skzzt = 1;
   }
+  ot = t;
   var palette = " `.',";
   //var palette = "   ```.',";
   for (var y = 0; y < H; ++y) {
     var line = "";
     if (lineno < H) {
       for (var x = 0; x < W; ++x) {
-        var TAU = 2 * Math.PI;
-        var tx = cos * x/amp - sin * 2*y/amp;
-        var v = Math.random() * 0.6 + 
-          0.4 * (0.5 + 0.5 * Math.sin(tx + (new Date() / rate) % TAU));
+        var v = wave(Math.random(), x, y);
         v = 1 - pow(v, GAMMA);
         line += palette[(v * palette.length) >> 0];  
       }