chemmens

HTML JavaScript Metro style progress ring (in the making)

Jun 8th, 2012
186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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.                 setTimeout( function(){ _this.animate() }, this.pause );
  97.                 this.pause = 1000;
  98.             },
  99.             animate: function () {
  100.  
  101.                 if( this.circles.length == 0 ) return this.start();
  102.  
  103.                 for (var i in this.circles)
  104.                 {
  105.                     if (typeof this.circles[i] == 'object')
  106.                     {
  107.                         var position = new lib.Vector2(parseInt(this.circles[i].element.style.left),parseInt(this.circles[i].element.style.top));
  108.                         if( position.isBetween(this.speedUpRange[0],this.speedUpRange[1]))
  109.                         {
  110.                             this.circles[i].angle += (this.circleDisplacemenetSpeed * (this.speedUpPercentage/100));
  111.                         }
  112.                         else
  113.                         {
  114.                             this.circles[i].angle += this.circleDisplacemenetSpeed;
  115.                         }
  116.  
  117.                         //if(i == 0 ) console.log(Math.cos( this.circles[i].angle * Math.PI / 360 )*Math.sin( this.circles[i].angle * Math.PI / 360 ))
  118.  
  119.                         var x = this.midPoint.x + Math.cos( this.circles[i].angle * Math.PI / 360 ) * this.midPoint.x;
  120.                         var y = this.midPoint.y + Math.sin( this.circles[i].angle * Math.PI / 360 ) * this.midPoint.y;
  121.  
  122.                         this.circles[i].element.style.left = Math.floor(x) + 'px';
  123.                         this.circles[i].element.style.top = Math.floor(y) + 'px';
  124.  
  125.                         if( position.isBetween(this.speedUpRange[0],this.speedUpRange[1]) && this.loops > 450 )
  126.                         {
  127.                             this.removeCirlce(i);
  128.                         }
  129.                     }
  130.                 }
  131.                 var _this = this;
  132.                 this.loops++;
  133.                 //console.log(this.loops)
  134.                 this.interval = setTimeout(function () { _this.animate(); }, _this.animationSpeed);
  135.             },
  136.             reset: function () {
  137.                 this.clearCircles();
  138.                 clearTimeout(this.interval);
  139.             },
  140.             stop: function () {
  141.                 this.loops = 0;
  142.                 this.clearCircles();
  143.                 clearTimeout(this.interval);
  144.             }
  145.         }
  146.     })(window, document, Metro)
  147.  
  148.     var lol = new Metro.ProgressRing();
  149.     document.body.appendChild( lol.element );
  150.     lol.start();
  151.     </script>
  152.  
  153. </body>
  154. </html>
Advertisement
Add Comment
Please, Sign In to add comment