Advertisement
Guest User

super optim

a guest
Mar 5th, 2015
212
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var moveItems = (function(){
  2.     var todoNode = 0;
  3.     var todoLink = 0;
  4.     var MAX_NODES = 240*4;
  5.     var MAX_LINKS = MAX_NODES/2;
  6.  
  7.     var restart = false;
  8.  
  9.     function moveSomeNodes(){
  10.         var n;
  11.         var goal = Math.min(todoNode+MAX_NODES, node[0].length);
  12.  
  13.         for(var i=todoNode ; i < goal ; i++){
  14.             n = node[0][i];
  15.             n.setAttribute('transform', 'translate(' + n.__data__.x + ',' + n.__data__.y + ')');
  16.             //n.setAttribute('cx', n.__data__.x);
  17.             //n.setAttribute('cy', n.__data__.y);
  18.         }
  19.  
  20.         todoNode = goal;
  21.         requestAnimationFrame(moveSome)
  22.     }
  23.  
  24.     function moveSomeLinks(){
  25.         var l;
  26.         var goal = Math.min(todoLink+MAX_LINKS, link[0].length);
  27.  
  28.         for(var i=todoLink ; i < goal ; i++){
  29.             l = link[0][i];
  30.             //console.log(l);
  31.             l.setAttribute('x1', l.__data__.source.x);
  32.             l.setAttribute('y1', l.__data__.source.y);
  33.             l.setAttribute('x2', l.__data__.target.x);
  34.             l.setAttribute('y2', l.__data__.target.y);
  35.         }
  36.  
  37.         todoLink = goal;
  38.         requestAnimationFrame(moveSome)
  39.     }
  40.  
  41.     function moveSome(){
  42.         console.time('moveSome')
  43.         if(todoNode < node[0].length) // some more nodes to do
  44.             moveSomeNodes()
  45.         else{ // nodes are done
  46.             if(todoLink < link[0].length) // some more links to do
  47.                 moveSomeLinks()
  48.             else{ // both nodes and links are done
  49.                 if(restart){
  50.                     restart = false;
  51.                     todoNode = 0;
  52.                     todoLink = 0;
  53.                     requestAnimationFrame(moveSome);
  54.                 }
  55.             }
  56.         }
  57.         console.timeEnd('moveSome')
  58.     }
  59.  
  60.  
  61.     return function moveItems(){
  62.         if(!restart){
  63.             restart = true;
  64.             requestAnimationFrame(moveSome);
  65.         }
  66.     };
  67.  
  68. })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement