Jump To …

d.js

(function (global) {

var d = {};

d.snowflake = function (conf, p) {
  var that = {}, j, jMax,
    x = conf.displayWidth * Math.random(), y = -10, cx, cy,
    yRate = conf.displayHeight * p.yRateFactor * (Math.random()*0.5 + 0.5),
    baseScale = conf.displayWidth/p.scaleFactor,
    scale = Math.random() * baseScale,
    baseAngle = 2 * Math.PI * Math.random(),
    angle = Math.PI / 3,
    lineScale = scale * (Math.random() + 0.5),
    commands = [];

  jMax = Math.floor(Math.random() * 5) + 1;
  for (j=0; j<jMax; j++) {
    cx = Math.random()*5; cy = Math.random()*5;
    commands.push(['lineTo', cx, cy]);
  }

  that.displayCallback = function (c, r) {
    var i, j, jMax, timeDelta;

    if (y < r.conf.displayHeight) {
      y = yRate * r.elapsedTime;
    }

    c.save();
    c.transform(scale, 0, 0, -scale, 
                x, y);
    c.rotate(baseAngle);
    c.lineWidth /= lineScale;
    c.strokeStyle = '#ffffff';
    c.beginPath();
    for (i=0; i<6; i++) {
      c.rotate(angle);
      c.moveTo(0,0);
      for (j=0, jMax=commands.length; j<jMax; j++) {
        c[commands[j][0]](commands[j][1], commands[j][2]);
      }
    }
    c.stroke();
    c.closePath();
    c.restore();
  };

  that.p = p;
  return that;
};

d.setBackground = function (conf, p) {
  var that = {};

  that.displayCallback = function (c, r) {
    r.conf.backgroundColor = p.backgroundColor;
    r.turnoff = true;
  };

  that.p = p;
  return that;
};

d.plot = function (conf, p) {
  var that = {},
      width = conf.displayWidth, height = conf.displayHeight,
      xvalues = [], yvalues = [], size,
      widthOver2 = width/2, heightOver2 = height/2;

  if (width > height) {
    size = height;
  } else {
    size = width;
  }
    
  that.displayCallback = function (c, r) {
    var i, iMax;

    xvalues.push(r.position * height);
    yvalues.push(height - p.f(r.position) * height);

    c.save();
    c.translate(widthOver2, heightOver2);


    c.beginPath();
    c.strokeStyle = '#dddddd';
    c.moveTo(-widthOver2, 0);
    c.lineTo(widthOver2, 0);
    c.moveTo(0, -heightOver2);
    c.lineTo(0, heightOver2);
    c.stroke();
    c.closePath();

    c.transform(p.scale, 0, 0, -p.scale, 1, 1);
    c.lineWidth /= p.scale;
    c.strokeStyle = p.strokeStyle;
    c.beginPath();
    c.moveTo(xvalues[0], yvalues[0]);
    for (i=0, iMax=xvalues.length; i<iMax; i++) {
      c.lineTo(xvalues[i],yvalues[i]);
    }
    c.stroke();
    c.closePath();

    c.restore();
  };

  that.p = p;
  return that;
};

d.blank = function (conf) {
  var that = {};
  that.displayCallback = function (c, r) {
  };
  return that;
};


d.square = function (conf, p, pe) {
  var that = {}, oldAlpha, pev = {};

  that.displayCallback = function (c, r) {
    c.save();
    pev.alpha = pe.alpha.displayCallback(r);
    c.globalAlpha = pev.alpha;
    c.strokeStyle = p.strokeStyle;
    c.rect(c.canvas.width/4, c.canvas.height/4,
                 c.canvas.width/4, c.canvas.height/4);
    c.stroke();
    c.restore();
  };

  that.p = p;
  that.pe = pe;
  that.pev = pev; 
  return that;
};


d.number = function (conf, p) {
  var that = {},
    frameNumber = 0;
  that.p = p || {};

  function displayCallback (c, r) {
    var width = c.canvas.width, 
      height = c.canvas.height,
      fontSize = Math.round(c.canvas.height / 4);

    c.strokeStyle = that.p.strokeStyle || '#000000';
    c.strokeRect(0, 0, width, height);
    c.beginPath();
    c.moveTo(Math.random()*width,Math.random()*height);
    c.lineTo(Math.random()*height,Math.random()*height);
    c.stroke();
    c.font = 'normal normal ' + fontSize + 'px monospace';
    c.fillStyle = that.p.fillStyle || '#000000';
    c.fillText(frameNumber++, 0, height - 1);
    c.fillText(r.position, 0, height/2);
  }

  that.displayCallback = displayCallback;
  return that;
};


if (typeof module != 'undefined' && module.exports) {
  module.exports = d;
} else {
  global.trurl = global.trurl || {};
  global.trurl.opcodes = global.trurl.opcodes || {};
  global.trurl.opcodes.d = d;
}

}(this));