Advertisement
Guest User

Untitled

a guest
Aug 4th, 2013
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var newX = 0;
  2.     var baseTaille = 20;
  3.     var lines = [];
  4.     var maxLines = 20;
  5.     var baseLifeTime = 50;
  6.     var timer = 0;
  7.     //Direction
  8.     // Tout droit = 0
  9.     // En haut = 1
  10.     // En bas = -1
  11.  
  12.     function Point (x,y) {
  13.         this.x = x;
  14.         this.y = y;
  15.     }
  16.     function Line(origin,direction,taille,ancestor,endingPoint)
  17.     {
  18.       this.origin = new Point(origin.x,origin.y);
  19.       this.direction = direction;
  20.       this.taille = taille;
  21.       this.ancestor = ancestor;
  22.       this.endingPoint = new Point(endingPoint.x,endingPoint.y);
  23.       //TODO on défini une durée de vie, soit le nombre de frames qu'elle va rester affichée;
  24.       this.lifeTime = taille;
  25.     }
  26.     var canvas;
  27.     var context;
  28.     $(document).ready(function () {
  29.         canvas = document.getElementById('canvas');
  30.       context = canvas.getContext('2d');
  31.       $("#canvas").attr("width",$(window).width());
  32.       $("#canvas").attr("height",$(window).height());
  33.       init();
  34.      
  35.     });
  36.  
  37.  
  38.  
  39.     function init () {
  40.       window.requestAnimFrame = (function(callback) {
  41.             return window.requestAnimationFrame || window.webkitRequestAnimationFrame || window.mozRequestAnimationFrame || window.oRequestAnimationFrame || window.msRequestAnimationFrame ||
  42.             function(callback) {
  43.               window.setTimeout(callback, 1000 / 60);
  44.             };
  45.           })();
  46.           animate();
  47.     }
  48.     var directions = {
  49.         'UP' : 1,
  50.         'FLAT' : 0
  51.     }
  52.  
  53.     function animate() {
  54.      
  55.       // update
  56.       if(timer == 0)
  57.       {
  58.           if (lines.length == 0) {
  59.             createLine(directions.UP,null,50);
  60.           }else
  61.           {
  62.             if(lines.length < maxLines)
  63.             {
  64.                 //If the previous line is in the "FLAT" direction, create 2 lines UP
  65.                 if (lines[lines.length-1].direction == directions.FLAT) {
  66.                     var p = lines[lines.length-1];
  67.                     p.endingPoint.x = lines[lines.length-1].origin.x;
  68.                     createLine(directions.UP,p,10);
  69.                     createLine(directions.UP,lines[lines.length-2],15);
  70.                 }
  71.  
  72.                 if (lines[lines.length-1].direction == directions.UP) {
  73.                     createLine(directions.FLAT,lines[lines.length-1],100);
  74.                 }
  75.             }else
  76.             {
  77.  
  78.             }
  79.           }
  80.           timer = baseLifeTime;
  81.         }
  82.       // clear
  83.       context.clearRect(0, 0, canvas.width, canvas.height);
  84.       // draw stuff
  85.       for(var l in lines){
  86.  
  87.         var line = lines[l];
  88.         drawLine(context,line);
  89.       }
  90.       // request new frame
  91.       requestAnimFrame(function() {
  92.         animate();
  93.         timer--;
  94.       });
  95.     }
  96.  
  97.     function drawLine (context,line) {
  98.       context.beginPath();
  99.       context.moveTo(line.origin.x, line.origin.y);
  100.         if(line.direction == directions.UP)
  101.         {
  102.             if(line.lifeTime > 0)
  103.             {
  104.                 context.lineTo(line.endingPoint.x, line.endingPoint.y+line.lifeTime);
  105.             }
  106.             else
  107.             {
  108.                 context.lineTo(line.endingPoint.x, line.endingPoint.y);
  109.             }
  110.         }
  111.         if(line.direction == directions.FLAT)
  112.         {
  113.             if(line.lifeTime > 0)
  114.             {
  115.                 context.lineTo(line.endingPoint.x-line.lifeTime, line.endingPoint.y);
  116.             }
  117.             else
  118.             {
  119.                 context.lineTo(line.endingPoint.x, line.endingPoint.y);
  120.             }
  121.         }
  122.  
  123.  
  124.         //context.globalAlpha = line.lifeTime/baseLifeTime;
  125.  
  126.         line.lifeTime--;
  127.  
  128.         context.strokeStyle = '#eee';
  129.         context.stroke();
  130.             //else
  131.         //{
  132.           //lines.splice(l,1);
  133.         //}
  134.     }
  135.  
  136.     function createLine (direction,ancestor,taille) {
  137.         //TODO if !ancestor, we create the first "branch" of our tree
  138.         var origin;
  139.         if(ancestor == null)
  140.         {
  141.             origin = {
  142.                 'x':($(window).width()/2),
  143.                 'y':($(window).height())
  144.             };
  145.  
  146.             endingPoint = {
  147.                 'x':origin.x,
  148.                 'y':(origin.y-taille)
  149.             };
  150.             ancestor = null;
  151.         }else
  152.         {
  153.             if (direction == directions.UP){
  154.                 origin = ancestor.endingPoint;
  155.  
  156.                 endingPoint = {
  157.                     'x':origin.x,
  158.                     'y':(origin.y-taille)
  159.                 };
  160.             }
  161.  
  162.             if (direction == directions.FLAT)
  163.             {
  164.                 origin = {
  165.                     'x':(ancestor.endingPoint.x-(taille/2)),
  166.                     'y':ancestor.endingPoint.y
  167.                 };
  168.  
  169.                 endingPoint = {
  170.                     'x':(origin.x+taille),
  171.                     'y':(origin.y)
  172.                 };
  173.  
  174.             }
  175.         }
  176.        
  177.         //We add the line to the "lines" array;
  178.         lines.push(new Line(origin,direction,taille, ancestor,endingPoint));
  179.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement