example-singleServer-progressBar.html 4.7 KB

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