Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Random orb start locations
- function orbStartLocation(){
- $('.orb').each(function(){
- var windowHeight = $(window).height();
- var randomVertLoc = Math.floor(Math.random() * windowHeight) + 1;
- var randomHorzLoc = Math.floor(Math.random() * 250) + 1;
- $(this).css('left','-' + randomHorzLoc + 'px').css('top',randomVertLoc + 'px');
- console.log(randomHorzLoc);
- console.log(randomVertLoc);
- });
- }
- //Handle orb stopping on hover
- function orbHoverStop(){
- $('.orb a').each(function(){
- var href = $(this).attr('href');
- var orbA = $(this);
- var timer = 0;
- $(orbA).hoverIntent(function(){
- console.log('Orb Hover: ' + href);
- clearTimeout(timer);
- $(orbA).parent().stop(true);
- }, function(){
- console.log('Orb De-hover: ' + href);
- timer = setTimeout(function() {
- reanimateOrb($(orbA).parent());
- }, 1000);
- });
- });
- }
- //Handle content display on orb click
- function contentOpen(){
- $('.orb a').click(function(){
- var href = $(this).attr('href');
- $(href).stop(true, true).toggle('slow', function(){
- $(href).find(':first-child').stop(true, true).animate({ marginTop: '200px'},function() {
- $(href).children().stop(true, true).animate({ opacity: '1'});
- });
- });
- });
- }
- //Handle closing content on "X" click
- function contentClose(){
- $('.content-box .close').click(function(){
- $(this).parent().children().stop(true,true).animate({ opacity: '0'},function() {
- $(this).parent().stop(true,true).toggle('slow', function(){ });
- });
- });
- }
- //Animate non-red orbs, repeats position calculation if not hovered
- function animateOrbs(){
- $('.orb a').each(function(){
- if($(this).is(':hover') !== true){
- var newq = makeNewPosition();
- var oldq = $(this).offset();
- var speed = calcSpeed([oldq.top, oldq.left], newq);
- $(this).parent().animate(
- {top: newq[0], left: newq[1]},
- {progress: function(){
- var id = $(this).attr('id');
- var pos = $(this).position();
- var posRight = pos.left + $(this).width();
- var posBottom = pos.top + $(this).height();
- var xCenter = posRight / 2;
- var yCenter = posBottom / 2;
- //Store position data for each orb, based on ID
- //THIS CURRENTLY ISN'T WORKING AS INTENDED, only the first id (#orb1) is stored properly
- $('#' + id).data( 'position', { x: xCenter, y: yCenter } );
- //Eventually we were going to use the jquery.line.js to draw lines between the orbs using the stored positions
- //$('.orb-container').line(orbOneX,orbOneY,orbTwoX,orbTwoY,{color:"#fff", stroke:1, zindex:1001});
- },
- duration: speed,
- complete: function(){
- animateOrbs();
- }
- });
- }
- });
- }
- //Generate new orb positions after animation completes
- function makeNewPosition(){
- var containerWidth = $('.orb-container').width();
- var containerHeight = $('.orb-container').height();
- var orbWidth = $('.orb').width();
- var orbHeight = $('.orb').height();
- //Width/Height of container boxes minus width of orb
- var maxh = containerHeight - orbHeight;
- var maxw = containerWidth - orbWidth;
- var nh = Math.floor(Math.random() * (maxh - 0 + 1)) + 0;
- var nw = Math.floor(Math.random() * (maxw - 0 + 1)) + 0;
- return [nh,nw];
- }
- //Caluclate orb speed
- function calcSpeed(prev, next) {
- var x = Math.abs(prev[1] - next[1]);
- var y = Math.abs(prev[0] - next[0]);
- var greatest = x > y ? x : y;
- var speedModifier = 0.04;
- var speed = Math.ceil(greatest/speedModifier);
- return speed;
- }
- //Reanimate orb
- function reanimateOrb($this){
- var newq = makeNewPosition();
- var oldq = $($this).offset();
- var speed = calcSpeed([oldq.top, oldq.left], newq);
- $($this).animate({ top: newq[0], left: newq[1] }, speed, function(){
- animateOrbs();
- });
- }
- $(document).ready(function(){
- orbStartLocation();
- orbHoverStop();
- contentOpen();
- contentClose();
- animateOrbs();
- });
Advertisement
Add Comment
Please, Sign In to add comment