
var jeffyamada = jeffyamada || {};


jeffyamada.Main = function() {
    this.mouseX = 0;
    this.mouseY = 0;
    this.momentumx = 0;
    this.momentumy = 0;
    this.left = [];
    this.right = [];
    this.canvas = document.getElementById('background-canvas');
    this.init();
};
jeffyamada.Main.prototype = {
    init: function() {
      this.startDraw();
      setTimeout( function() {
        $(".start").animate({ opacity: 1, top:'0' }, 750);
      }, 1000 );
    }, 
    
    startDraw: function() {
      if (!this.canvas.getContext) return;
      this.ctx = this.canvas.getContext('2d');
      var self = this;
      $(document).mousemove(function(e) {
        self.onMouseMove( e.pageX, e.pageY );
      });
      setInterval( function() { self.update(); },30);
    },
    
    onMouseMove: function(x,y) {
      this.mouseX = x;
      this.mouseY = y;
    },
    
    update: function() {
      var current = {x:this.mouseX,y:this.mouseY};
      if( this.left.length > 9 ) {
          current = this.follow(this.left[0],current);
      }
      this.left.unshift(current);
      this.right.unshift(current);
      if( this.left.length > 60 ) {
        this.left.pop();
        this.right.pop();
      }
      this.draw();
    },
    
    follow: function(p1,p2) {
      var distx = p1.x - p2.x;
      var disty = p1.y - p2.y;
      this.momentumx -= distx / 5;
      this.momentumy -= disty / 5;
      this.momentumx *= 0.9;
      this.momentumy *= 0.9;
      var p3 = { x:p1.x+this.momentumx, y:p1.y+this.momentumy };
      return p3;
    },
    
    draw: function() {
      var screenWidth = window.innerWidth;
      var screenHeight = window.innerHeight;
      this.canvas.width = screenWidth;
      this.canvas.height = screenHeight;
      this.ctx.moveTo(this.left[0].x,this.left[0].y);
      var total = this.left.length-4;
      for( var i = 1; i < total; i++ ) {
        this.ctx.beginPath();
        var num = Math.round((i/total)*20)+230;
        this.ctx.strokeStyle = 'rgb('+num+', '+num+', '+num+')';
        this.ctx.lineWidth = 1.5;
          var p1 = this.left[i];
          var p2 = this.left[i+1];
          var p3 = this.left[i+2];
          var a1 = this.lerpleft(p1,p2,.5);
          var a2 = this.lerpleft(p2,p3,.5);
          this.ctx.moveTo(a1.x,a1.y);
          this.ctx.quadraticCurveTo(p2.x,p2.y,a2.x,a2.y);
          this.ctx.stroke();
        }
    },
    
    lerpleft: function(p1,p2,r) {
      var p3 = {};
      p3.x = this.lerp(p1.x,p2.x,r);
      p3.y = this.lerp(p1.y,p2.y,r);
      return p3;
    },
    
    lerp: function(a,b,r) {
      return a+r*(b-a);
    }
};

var Main = null;
$(document).ready(function() {
    Main = new jeffyamada.Main();
    //Main.draw();
});
















