chemmens

HTML JavaScript Metro style progress ring (in the making)

Jun 8th, 2012
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 5.75 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4.     <title>Metro progress ring</title>
  5.     <script type="text/javascript" src="metro.js"></script>
  6.     <style type="text/css">
  7.     .circle {
  8.         height: 4px;
  9.         width: 4px;
  10.         list-style: disc;
  11.         list-style-position: inside;
  12.     }
  13.     </style>
  14. </head>
  15. <body>
  16.  
  17.     <script type="text/javascript">
  18.     (function (window, document, lib, undefined) {
  19.         "use strict";
  20.         if (lib == undefined || !lib instanceof Assembly) throw "LoadingBar must be part of an assembly.";
  21.  
  22.         lib.ProgressRing = function (args /* { element : document element, numCircles : integer } */) {
  23.             var args = args == undefined ? { numCircles: this.numCircles, element: document.createElement('ul')} : args;
  24.             this.numCircles = args.numCircles == undefined ? this.numCircles : typeof args.numCircles == 'number' ? args.numCircles : this.numCircles;
  25.             this.element = args.element == undefined ? document.createElement('ul') : args.element;
  26.             this.element.className = 'progress-circle';
  27.             this.element.style.overflow = 'hidden';
  28.             this.setDimensions();
  29.             var _this = this;
  30.             window.addEventListener('resize', function () { _this.setDimensions() });
  31.             return this;
  32.         }
  33.         lib.ProgressRing.prototype = {
  34.  
  35.             constructor: lib.ProgressRing,
  36.             interval: null,
  37.             element: null,
  38.             numCircles: 5,
  39.             animationSpeed: 20,
  40.             circleDisplacemenetSpeed: Math.PI,
  41.             speedUpPercentage: 0,
  42.             dimensions: new lib.Vector2(),
  43.             speedUpRange: [new lib.Vector2(256,256), new lib.Vector2(256,128)],
  44.             loops : 0,
  45.             pause : 0,
  46.  
  47.             setDimensions: function () {
  48.                 this.element.style.width = 256 + 'px';
  49.                 this.element.style.height = 256 + 'px';
  50.                 this.dimensions = new lib.Vector2(parseInt(this.element.style.width), parseInt(this.element.style.height));
  51.                 this.midPoint = this.dimensions.clone().half();
  52.             },
  53.  
  54.             circles : [],
  55.  
  56.             addCirlce: function () {
  57.                 var circle = {};
  58.                 circle.element = document.createElement('li');
  59.                 circle.element.className = 'circle';
  60.                 circle.element.style.position = 'fixed';
  61.                 //circle.element.style.display = 'block';
  62.                 circle.angle = 0;
  63.                 this.element.appendChild(circle.element);
  64.                 this.circles.push(circle);
  65.                 return circle;
  66.             },
  67.  
  68.             removeCirlce: function (index) {
  69.                 this.element.removeChild(this.circles[index].element);
  70.                 this.circles.pop(this.circles[index])
  71.             },
  72.  
  73.             clearCircles: function () {
  74.                 while (this.element.hasChildNodes()) {
  75.                     this.element.removeChild(this.element.lastChild);
  76.                 }
  77.             },
  78.  
  79.             start: function () {
  80.                 this.stop();
  81.                 this.loops = 0;
  82.                 for (var i = 0; i < this.numCircles; i++)
  83.                 {
  84.                     var circle = this.addCirlce()
  85.                    circle.angle = ( (360/this.numCircles)*i);
  86.  
  87.                    var left = this.midPoint.x + Math.cos( circle.angle * Math.PI / 180 ) * this.midPoint.x;
  88.                    var top = this.midPoint.y + Math.sin( circle.angle * Math.PI / 180 ) * this.midPoint.y;
  89.  
  90.                    circle.element.style.left = Math.floor(left) + 'px';
  91.                    circle.element.style.top = Math.floor(top) + 'px';
  92.                 }
  93.                 this.speedUpPercentage = this.circles.length * 50;
  94.  
  95.                 var _this = this;
  96.                 return setTimeout( function(){ _this.animate();
  97.             },
  98.             animate: function () {
  99.  
  100.                 if( this.circles.length == 0 ) return this.start();
  101.  
  102.                 for (var i in this.circles)
  103.                 {
  104.                     if (typeof this.circles[i] == 'object')
  105.                     {
  106.                         var position = new lib.Vector2(parseInt(this.circles[i].element.style.left),parseInt(this.circles[i].element.style.top));
  107.                         if( position.isBetween(this.speedUpRange[0],this.speedUpRange[1]))
  108.                         {
  109.                             this.circles[i].angle += (this.circleDisplacemenetSpeed * (this.speedUpPercentage/100));
  110.                         }
  111.                         else
  112.                         {
  113.                             this.circles[i].angle += this.circleDisplacemenetSpeed;
  114.                         }
  115.  
  116.                         //if(i == 0 ) console.log(Math.cos( this.circles[i].angle * Math.PI / 360 )*Math.sin( this.circles[i].angle * Math.PI / 360 ))
  117.  
  118.                         var x = this.midPoint.x + Math.cos( this.circles[i].angle * Math.PI / 360 ) * this.midPoint.x;
  119.                         var y = this.midPoint.y + Math.sin( this.circles[i].angle * Math.PI / 360 ) * this.midPoint.y;
  120.  
  121.                         this.circles[i].element.style.left = Math.floor(x) + 'px';
  122.                         this.circles[i].element.style.top = Math.floor(y) + 'px';
  123.  
  124.                         if( position.isBetween(this.speedUpRange[0],this.speedUpRange[1]) && this.loops > 450 )
  125.                         {
  126.                             this.removeCirlce(i);
  127.                         }
  128.                     }
  129.                 }
  130.                 var _this = this;
  131.                 this.loops++;
  132.                 //console.log(this.loops)
  133.                 this.interval = setTimeout(function () { _this.animate(); }, _this.animationSpeed);
  134.             },
  135.             reset: function () {
  136.                 this.clearCircles();
  137.                 clearTimeout(this.interval);
  138.             },
  139.             stop: function () {
  140.                 this.loops = 0;
  141.                 this.clearCircles();
  142.                 clearTimeout(this.interval);
  143.             }
  144.         }
  145.     })(window, document, Metro)
  146.  
  147.     var lol = new Metro.ProgressRing();
  148.     document.body.appendChild( lol.element );
  149.     lol.start();
  150.     </script>
  151.  
  152. </body>
  153. </html>
Advertisement
Add Comment
Please, Sign In to add comment