FireBot

full-orb-script

Nov 17th, 2014
335
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //Random orb start locations
  2. function orbStartLocation(){
  3.     $('.orb').each(function(){
  4.         var windowHeight = $(window).height();
  5.         var randomVertLoc = Math.floor(Math.random() * windowHeight) + 1;
  6.         var randomHorzLoc = Math.floor(Math.random() * 250) + 1;
  7.         $(this).css('left','-' + randomHorzLoc + 'px').css('top',randomVertLoc + 'px');
  8.         console.log(randomHorzLoc);
  9.         console.log(randomVertLoc);
  10.     });
  11. }
  12.  
  13. //Handle orb stopping on hover
  14. function orbHoverStop(){
  15.     $('.orb a').each(function(){              
  16.         var href = $(this).attr('href');
  17.         var orbA = $(this);
  18.         var timer = 0;
  19.        
  20.         $(orbA).hoverIntent(function(){
  21.             console.log('Orb Hover: ' + href);
  22.             clearTimeout(timer);
  23.             $(orbA).parent().stop(true);
  24.         }, function(){
  25.             console.log('Orb De-hover: ' + href);
  26.             timer = setTimeout(function() {
  27.                     reanimateOrb($(orbA).parent());
  28.             }, 1000);
  29.         });
  30.     });
  31. }
  32.  
  33. //Handle content display on orb click
  34. function contentOpen(){
  35.     $('.orb a').click(function(){
  36.         var href = $(this).attr('href');
  37.        
  38.         $(href).stop(true, true).toggle('slow', function(){
  39.             $(href).find(':first-child').stop(true, true).animate({ marginTop: '200px'},function() {
  40.                 $(href).children().stop(true, true).animate({ opacity: '1'});
  41.             });
  42.         });
  43.     });
  44. }
  45.  
  46. //Handle closing content on "X" click
  47. function contentClose(){
  48.     $('.content-box .close').click(function(){
  49.         $(this).parent().children().stop(true,true).animate({ opacity: '0'},function() {
  50.             $(this).parent().stop(true,true).toggle('slow', function(){    });
  51.           });
  52.     });
  53. }
  54.  
  55.  
  56. //Animate non-red orbs, repeats position calculation if not hovered
  57. function animateOrbs(){
  58.     $('.orb a').each(function(){
  59.         if($(this).is(':hover') !== true){
  60.             var newq = makeNewPosition();
  61.             var oldq = $(this).offset();
  62.             var speed = calcSpeed([oldq.top, oldq.left], newq);
  63.            
  64.             $(this).parent().animate(
  65.                 {top: newq[0], left: newq[1]},
  66.                 {progress: function(){
  67.                     var id = $(this).attr('id');
  68.                     var pos = $(this).position();
  69.                        var posRight = pos.left + $(this).width();
  70.                     var posBottom = pos.top + $(this).height();
  71.                                
  72.                     var xCenter = posRight / 2;
  73.                     var yCenter = posBottom / 2;          
  74.                    
  75.                     //Store position data for each orb, based on ID
  76.                     //THIS CURRENTLY ISN'T WORKING AS INTENDED, only the first id (#orb1) is stored properly
  77.                     $('#' + id).data( 'position', { x: xCenter, y: yCenter } );
  78.                     //Eventually we were going to use the jquery.line.js to draw lines between the orbs using the stored positions
  79.                     //$('.orb-container').line(orbOneX,orbOneY,orbTwoX,orbTwoY,{color:"#fff", stroke:1, zindex:1001});
  80.                 },
  81.                 duration: speed,
  82.                 complete: function(){
  83.                     animateOrbs();
  84.                 }
  85.             });
  86.         }
  87.     });
  88. }
  89.  
  90. //Generate new orb positions after animation completes
  91. function makeNewPosition(){
  92.     var containerWidth = $('.orb-container').width();
  93.     var containerHeight = $('.orb-container').height();
  94.     var orbWidth = $('.orb').width();
  95.     var orbHeight = $('.orb').height();
  96.        
  97.     //Width/Height of container boxes minus width of orb
  98.     var maxh = containerHeight - orbHeight;
  99.     var maxw = containerWidth - orbWidth;
  100.  
  101.     var nh = Math.floor(Math.random() * (maxh - 0 + 1)) + 0;
  102.     var nw = Math.floor(Math.random() * (maxw - 0 + 1)) + 0;
  103.    
  104.     return [nh,nw];
  105. }
  106.  
  107. //Caluclate orb speed
  108. function calcSpeed(prev, next) {
  109.     var x = Math.abs(prev[1] - next[1]);
  110.     var y = Math.abs(prev[0] - next[0]);
  111.     var greatest = x > y ? x : y;
  112.  
  113.     var speedModifier = 0.04;
  114.     var speed = Math.ceil(greatest/speedModifier);
  115.     return speed;
  116. }
  117.  
  118. //Reanimate orb
  119. function reanimateOrb($this){
  120.     var newq = makeNewPosition();
  121.     var oldq = $($this).offset();
  122.     var speed = calcSpeed([oldq.top, oldq.left], newq);
  123.    
  124.     $($this).animate({ top: newq[0], left: newq[1] }, speed, function(){
  125.         animateOrbs();
  126.     });
  127. }
  128.  
  129. $(document).ready(function(){
  130.     orbStartLocation();
  131.     orbHoverStop();
  132.     contentOpen();
  133.     contentClose();
  134.     animateOrbs();
  135. });
Advertisement
Add Comment
Please, Sign In to add comment