Advertisement
Guest User

Untitled

a guest
Oct 6th, 2015
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.38 KB | None | 0 0
  1. <html>
  2. <head>
  3. <link rel="stylesheet" href="http://yui.yahooapis.com/pure/0.5.0/pure-min.css">
  4. <link rel="stylesheet" href="http://yui.yahooapis.com/pure/0.5.0/base-min.css">
  5. <link rel="stylesheet" type="text/css" href="css/base.css">
  6. <style>
  7. .healthbar {
  8. height: 30px;
  9. position: relative;
  10. background: #555;
  11. }
  12. .healthbar > span {
  13. display: block;
  14. height: 100%;
  15. background-color: rgb(230, 248, 243);//fallback option
  16. background-image: -webkit-gradient(
  17. linear,
  18. left bottom,
  19. left top,
  20. color-stop(0, rgb(43,194,83)),
  21. color-stop(1, rgb(84,240,84))
  22. );
  23. background-image: -webkit-linear-gradient(
  24. center bottom,
  25. rgb(43,194,83) 37%,
  26. rgb(84,240,84) 69%
  27. );
  28. background-image: -moz-linear-gradient(
  29. center bottom,
  30. rgb(43,194,83) 37%,
  31. rgb(84,240,84) 69%
  32. );
  33. background-image: -ms-linear-gradient(
  34. center bottom,
  35. rgb(43,194,83) 37%,
  36. rgb(84,240,84) 69%
  37. );
  38. background-image: -o-linear-gradient(
  39. center bottom,
  40. rgb(43,194,83) 37%,
  41. rgb(84,240,84) 69%
  42. );
  43. position: relative;
  44. overflow: hidden;
  45. }
  46. </style>
  47. <script>
  48. ////////////////
  49. //function prototypes
  50.  
  51. var __nativeST__ = window.setTimeout, __nativeSI__ = window.setInterval;
  52.  
  53. window.setTimeout = function (vCallback, nDelay /*, argumentToPass1, argumentToPass2, etc. */) {
  54. var oThis = this, aArgs = Array.prototype.slice.call(arguments, 2);
  55. return __nativeST__(vCallback instanceof Function ? function () {
  56. vCallback.apply(oThis, aArgs);
  57. } : vCallback, nDelay);
  58. };
  59.  
  60. window.setInterval = function (vCallback, nDelay /*, argumentToPass1, argumentToPass2, etc. */) {
  61. var oThis = this, aArgs = Array.prototype.slice.call(arguments, 2);
  62. return __nativeSI__(vCallback instanceof Function ? function () {
  63. vCallback.apply(oThis, aArgs);
  64. } : vCallback, nDelay);
  65. };
  66.  
  67. window.clearTimeout = function (vCallback /*, argumentToPass1, argumentToPass2, etc. */) {
  68. var oThis = this, aArgs = Array.prototype.slice.call(arguments, 1);
  69. return __nativeSI__(vCallback instanceof Function ? function () {
  70. vCallback.apply(oThis, aArgs);
  71. } : vCallback);
  72. };
  73. window.clearInterval = function (vCallback /*, argumentToPass1, argumentToPass2, etc. */) {
  74. var oThis = this, aArgs = Array.prototype.slice.call(arguments, 1);
  75. return __nativeSI__(vCallback instanceof Function ? function () {
  76. vCallback.apply(oThis, aArgs);
  77. } : vCallback);
  78. };
  79. //setTimeout.call(myArray, myArray.myMethod, 2000); example;
  80. //thank u mozilla for blessing me with capracious hax as used and modified above
  81. function HealthBar (name,health,maxHealth,animated) {//Prototype SampleBar(*,,,) Name Req example var AnotherBar = new Healthbar('anotherbar');
  82. self = this; //alias this
  83. this.name = name;//name after the element
  84. this.animated = typeof animated !== 'undefined' ? animated : false;//set to animated, if not passed, set to false
  85. this.health = typeof health !== 'undefined' ? health: 0;//initialize this with a value of 0
  86. this.maxHealth = typeof maxHealth !== 'undefined' ? maxHealth: 100;//initialize this with a value of 100
  87. this.targethealth = 0;//i should eliminate this in the future
  88. this.barIncrement = 5;//maxHealth/100;//default health increment for a bar of 100 is 1
  89. this.fillspeed = 10;//10ms is an ideal fill speed
  90. }
  91. //using prototypes conserves memory by avoiding duplicated variables
  92. HealthBar.prototype.setHealth = function(newhealth){
  93. if(between(newhealth, 0, this.maxHealth)){
  94. this.health = newhealth; //set health to new value
  95. this.Update(); //update the visible component of the class
  96. }//don't exceed max or go below none.
  97. };//semicolons needed for inside or prototype functions
  98.  
  99. HealthBar.prototype.addHealth = function(newhealth){
  100. if(between((this.health + newhealth), 0, this.maxHealth)){
  101. this.health += newhealth;
  102. this.Update();
  103. }//otherwise cannot add or remove anymore health, so we'll attempt to get as close as possible
  104. else if(newhealth > 0)
  105. {
  106. this.health = this.maxHealth;
  107. this.Update();
  108.  
  109. }//just set to max health, we can go no higher and we want to add more
  110. else if(newhealth < 0)
  111. {
  112. this.health = 0;//set to 0, we can go no lower
  113. this.Update();
  114. }
  115. };
  116.  
  117. HealthBar.prototype.Update = function() {
  118. if(this.health <= 100)
  119. { document.getElementById(this.name).style.width = this.health.toString().concat("%");} //for bars under 101 health
  120. else
  121. {document.getElementById(this.name).style.width = ((this.health/this.maxHealth) * 100).toString().concat("%");}//else convert to percent
  122. };
  123.  
  124. HealthBar.prototype.Heal = function(fillto, speed, increment) {
  125. self = this;
  126. this.barIncrement = typeof increment !== 'undefined' ? increment : this.barIncrement; //update the value if passed, otherwise retain
  127. this.fillspeed = typeof speed !== 'undefined' ? speed : this.fillspeed; //update the value if passed, otherwise retain
  128. this.targethealth = fillto;//set bar new value storage variable
  129. setInterval.call(self, self.callFill , speed);
  130.  
  131. };//run a timer to fill health to the new level
  132. HealthBar.prototype.callFill = function() {
  133. this.fillHealth();
  134. };
  135. HealthBar.prototype.fillHealth = function() {
  136. self = this;
  137. if(this.targethealth > this.health){this.setHealth(this.health+this.barIncrement);}//if target is higher, add 1 health
  138. else if(this.targethealth < this.health){this.setHealth(this.health-this.barIncrement);}//if target is lower, remove 1 health
  139. if(this.targethealth == this.health){
  140. clearInterval.call(self,self.callFill, speed);
  141. this.barIncrement = this.maxHealth/100;//clear heal increment as well
  142. }//when target reached stop
  143. };
  144. function between(x, min, max) {
  145. return x >= min && x <= max;//make checking ranges more simple
  146. }
  147. </script>
  148. </head>
  149. <body>
  150. <input onkeypress = "healthBar(event);"/>
  151. <button class="pure-button button-green" onclick = "anotherBar.addHealth(10);">Add Health</button>
  152. <button class="pure-button button-green" onclick = "anotherBar.Heal(100);">Heal Health</button>
  153. <button class="pure-button button-red" onclick = "anotherBar.addHealth(-10);">Remove Health</button>
  154. <div class="healthbar" style="float: center; width: 50%;">
  155. <span id="anotherbarspan" style="width: 0%"></span><!--initial value of nothing, set to value on load-->
  156. </div>
  157. <script>
  158. //call after page loaded
  159. var anotherBar = new HealthBar('anotherbarspan');//initialize the object, must be different than the span id
  160. anotherBar.setHealth(55);//set the health value
  161. </script>
  162. </body>
  163. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement