example-singleServer-progressBar.html 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="UTF-8" />
  5. <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no, user-scalable=no" />
  6. <title>LibreSpeed Example</title>
  7. <link rel="shortcut icon" href="favicon.ico">
  8. <script type="text/javascript" src="speedtest.js"></script>
  9. <script type="text/javascript">
  10. //INITIALIZE SPEEDTEST
  11. var s=new Speedtest(); //create speedtest object
  12. s.onupdate=function(data){ //callback to update data in UI
  13. I("ip").textContent=data.clientIp;
  14. I("dlText").textContent=(data.testState==1&&data.dlStatus==0)?"...":data.dlStatus;
  15. I("ulText").textContent=(data.testState==3&&data.ulStatus==0)?"...":data.ulStatus;
  16. I("pingText").textContent=data.pingStatus;
  17. I("jitText").textContent=data.jitterStatus;
  18. var prog=(Number(data.dlProgress)*2+Number(data.ulProgress)*2+Number(data.pingProgress))/5;
  19. I("progress").style.width=(100*prog)+"%";
  20. }
  21. s.onend=function(aborted){ //callback for test ended/aborted
  22. I("startStopBtn").className=""; //show start button again
  23. if(aborted){ //if the test was aborted, clear the UI and prepare for new test
  24. initUI();
  25. }
  26. }
  27. function startStop(){ //start/stop button pressed
  28. if(s.getState()==3){
  29. //speedtest is running, abort
  30. s.abort();
  31. }else{
  32. //test is not running, begin
  33. s.start();
  34. I("startStopBtn").className="running";
  35. }
  36. }
  37. //function to (re)initialize UI
  38. function initUI(){
  39. I("dlText").textContent="";
  40. I("ulText").textContent="";
  41. I("pingText").textContent="";
  42. I("jitText").textContent="";
  43. I("ip").textContent="";
  44. }
  45. function I(id){return document.getElementById(id);}
  46. </script>
  47. <style type="text/css">
  48. html,body{
  49. border:none; padding:0; margin:0;
  50. background:#FFFFFF;
  51. color:#202020;
  52. }
  53. body{
  54. text-align:center;
  55. font-family:"Roboto",sans-serif;
  56. }
  57. h1{
  58. color:#404040;
  59. }
  60. #startStopBtn{
  61. display:inline-block;
  62. margin:0 auto;
  63. color:#6060AA;
  64. background-color:rgba(0,0,0,0);
  65. border:0.15em solid #6060FF;
  66. border-radius:0.3em;
  67. transition:all 0.3s;
  68. box-sizing:border-box;
  69. width:8em; height:3em;
  70. line-height:2.7em;
  71. cursor:pointer;
  72. box-shadow: 0 0 0 rgba(0,0,0,0.1), inset 0 0 0 rgba(0,0,0,0.1);
  73. }
  74. #startStopBtn:hover{
  75. box-shadow: 0 0 2em rgba(0,0,0,0.1), inset 0 0 1em rgba(0,0,0,0.1);
  76. }
  77. #startStopBtn.running{
  78. background-color:#FF3030;
  79. border-color:#FF6060;
  80. color:#FFFFFF;
  81. }
  82. #startStopBtn:before{
  83. content:"Start";
  84. }
  85. #startStopBtn.running:before{
  86. content:"Abort";
  87. }
  88. #test{
  89. margin-top:2em;
  90. margin-bottom:12em;
  91. }
  92. div.testArea{
  93. display:inline-block;
  94. width:14em;
  95. height:9em;
  96. position:relative;
  97. box-sizing:border-box;
  98. }
  99. div.testName{
  100. position:absolute;
  101. top:0.1em; left:0;
  102. width:100%;
  103. font-size:1.4em;
  104. z-index:9;
  105. }
  106. div.meterText{
  107. position:absolute;
  108. bottom:1.5em; left:0;
  109. width:100%;
  110. font-size:2.5em;
  111. z-index:9;
  112. }
  113. #dlText{
  114. color:#6060AA;
  115. }
  116. #ulText{
  117. color:#309030;
  118. }
  119. #pingText,#jitText{
  120. color:#AA6060;
  121. }
  122. div.meterText:empty:before{
  123. color:#505050 !important;
  124. content:"0.00";
  125. }
  126. div.unit{
  127. position:absolute;
  128. bottom:2em; left:0;
  129. width:100%;
  130. z-index:9;
  131. }
  132. div.testGroup{
  133. display:inline-block;
  134. }
  135. @media all and (max-width:65em){
  136. body{
  137. font-size:1.5vw;
  138. }
  139. }
  140. @media all and (max-width:40em){
  141. body{
  142. font-size:0.8em;
  143. }
  144. div.testGroup{
  145. display:block;
  146. margin: 0 auto;
  147. }
  148. }
  149. #progressBar{
  150. width:90%;
  151. height:0.3em;
  152. background-color:#EEEEEE;
  153. position:relative;
  154. display:block;
  155. margin:0 auto;
  156. margin-bottom:2em;
  157. }
  158. #progress{
  159. position:absolute;
  160. top:0; left:0;
  161. height:100%;
  162. width:0%;
  163. transition: width 2s;
  164. background-color:#90BBFF;
  165. }
  166. </style>
  167. </head>
  168. <body>
  169. <h1>LibreSpeed Example</h1>
  170. <div id="startStopBtn" onclick="startStop()"></div>
  171. <div id="test">
  172. <div id="progressBar"><div id="progress"></div></div>
  173. <div class="testGroup">
  174. <div class="testArea">
  175. <div class="testName">Download</div>
  176. <div id="dlText" class="meterText"></div>
  177. <div class="unit">Mbit/s</div>
  178. </div>
  179. <div class="testArea">
  180. <div class="testName">Upload</div>
  181. <div id="ulText" class="meterText"></div>
  182. <div class="unit">Mbit/s</div>
  183. </div>
  184. </div>
  185. <div class="testGroup">
  186. <div class="testArea">
  187. <div class="testName">Ping</div>
  188. <div id="pingText" class="meterText"></div>
  189. <div class="unit">ms</div>
  190. </div>
  191. <div class="testArea">
  192. <div class="testName">Jitter</div>
  193. <div id="jitText" class="meterText"></div>
  194. <div class="unit">ms</div>
  195. </div>
  196. </div>
  197. <div id="ipArea">
  198. IP Address: <span id="ip"></span>
  199. </div>
  200. </div>
  201. <a href="https://github.com/librespeed/speedtest">Source code</a>
  202. <script type="text/javascript">
  203. initUI();
  204. </script>
  205. </body>
  206. </html>