example-gauges.html 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <script type="text/javascript" src="server_selector.min.js"></script>
  5. <script type="text/javascript">
  6. function I(i){return document.getElementById(i);}
  7. //LIST OF TEST SERVERS. See documentation for details if needed
  8. var SPEEDTEST_SERVERS=[
  9. { //this is my demo server, remove it
  10. name:"Speedtest Demo Server 1 (HTTP)", //user friendly name for the server
  11. server:"http://mpotdemo.fdossena.com/", //Full URL to the server, complete with / at the end
  12. dlURL:"garbage.php", //path to download test on this server (garbage.php or replacement)
  13. ulURL:"empty.php", //path to upload test on this server (empty.php or replacement)
  14. pingURL:"empty.php", //path to ping/jitter test on this server (empty.php or replacement)
  15. getIpURL:"getIP.php" //path to getIP on this server (getIP.php or replacement)
  16. },
  17. { //this is my demo server, remove it
  18. name:"Speedtest Demo Server 2 (HTTP)",
  19. server:"http://mpotdemo.adolfintel.com/",
  20. dlURL:"garbage.php",
  21. ulURL:"empty.php",
  22. pingURL:"empty.php",
  23. getIpURL:"getIP.php"
  24. },
  25. { //this is my demo server, remove it
  26. name:"Speedtest Demo Server 1 (HTTPS)",
  27. server:"https://mpotdemo.fdossena.com/",
  28. dlURL:"garbage.php",
  29. ulURL:"empty.php",
  30. pingURL:"empty.php",
  31. getIpURL:"getIP.php"
  32. },
  33. { //this is my demo server, remove it
  34. name:"Speedtest Demo Server 2 (HTTPS)",
  35. server:"https://mpotdemo.adolfintel.com/",
  36. dlURL:"garbage.php",
  37. ulURL:"empty.php",
  38. pingURL:"empty.php",
  39. getIpURL:"getIP.php"
  40. }
  41. //add other servers here, comma separated
  42. ];
  43. //SERVER AUTO SELECTION
  44. function initServers(){
  45. fastSelectServer(SPEEDTEST_SERVERS,function(server){
  46. if(server!=null){ //at least 1 server is available
  47. I("loading").className="hidden"; //hide loading message
  48. //configure speedtest to use the specified server
  49. speedtestSettings.url_dl=server.server+server.dlURL;
  50. speedtestSettings.url_ul=server.server+server.ulURL;
  51. speedtestSettings.url_ping=server.server+server.pingURL;
  52. speedtestSettings.url_getIp=server.server+server.getIpURL;
  53. speedtestSettings.telemetry_extra=JSON.stringify({server:server.name}); //add server name as extra data in the telemetry
  54. I("server").textContent=server.name; //show the name of the server in the UI
  55. //show test UI
  56. I("testWrapper").className="visible";
  57. initUI();
  58. }else{ //no servers are available, the test cannot proceed
  59. I("message").innerHTML="No servers available";
  60. }
  61. });
  62. }
  63. var meterBk="#E0E0E0";
  64. var dlColor="#6060AA",
  65. ulColor="#309030",
  66. pingColor="#AA6060",
  67. jitColor="#AA6060";
  68. var progColor="#EEEEEE";
  69. //CODE FOR GAUGES
  70. function drawMeter(c,amount,bk,fg,progress,prog){
  71. var ctx=c.getContext("2d");
  72. var dp=window.devicePixelRatio||1;
  73. var cw=c.clientWidth*dp, ch=c.clientHeight*dp;
  74. var sizScale=ch*0.0055;
  75. if(c.width==cw&&c.height==ch){
  76. ctx.clearRect(0,0,cw,ch);
  77. }else{
  78. c.width=cw;
  79. c.height=ch;
  80. }
  81. ctx.beginPath();
  82. ctx.strokeStyle=bk;
  83. ctx.lineWidth=16*sizScale;
  84. ctx.arc(c.width/2,c.height-58*sizScale,c.height/1.8-ctx.lineWidth,-Math.PI*1.1,Math.PI*0.1);
  85. ctx.stroke();
  86. ctx.beginPath();
  87. ctx.strokeStyle=fg;
  88. ctx.lineWidth=16*sizScale;
  89. ctx.arc(c.width/2,c.height-58*sizScale,c.height/1.8-ctx.lineWidth,-Math.PI*1.1,amount*Math.PI*1.2-Math.PI*1.1);
  90. ctx.stroke();
  91. if(typeof progress !== "undefined"){
  92. ctx.fillStyle=prog;
  93. ctx.fillRect(c.width*0.3,c.height-16*sizScale,c.width*0.4*progress,4*sizScale);
  94. }
  95. }
  96. function mbpsToAmount(s){
  97. return 1-(1/(Math.pow(1.3,Math.sqrt(s))));
  98. }
  99. function msToAmount(s){
  100. return 1-(1/(Math.pow(1.08,Math.sqrt(s))));
  101. }
  102. //SPEEDTEST AND UI CODE
  103. var w=null; //speedtest worker
  104. var data=null; //data from worker
  105. var speedtestSettings={
  106. //add test settings here if you want
  107. };
  108. function startStop(){
  109. if(w!=null){
  110. //speedtest is running, abort
  111. w.postMessage('abort');
  112. w=null;
  113. data=null;
  114. I("startStopBtn").className="";
  115. initUI();
  116. }else{
  117. //test is not running, begin
  118. w=new Worker('speedtest_worker.min.js');
  119. w.postMessage('start '+JSON.stringify(speedtestSettings)); //Add optional parameters as a JSON object to this command
  120. I("startStopBtn").className="running";
  121. w.onmessage=function(e){
  122. data=JSON.parse(e.data);
  123. var status=data.testState;
  124. if(status>=4){
  125. //test completed
  126. I("startStopBtn").className="";
  127. w=null;
  128. updateUI(true);
  129. }
  130. };
  131. }
  132. }
  133. //this function reads the data sent back by the worker and updates the UI
  134. function updateUI(forced){
  135. if(!forced&&(!data||!w)) return;
  136. var status=data.testState;
  137. I("ip").textContent=data.clientIp;
  138. I("dlText").textContent=(status==1&&data.dlStatus==0)?"...":data.dlStatus;
  139. drawMeter(I("dlMeter"),mbpsToAmount(Number(data.dlStatus*(status==1?oscillate():1))),meterBk,dlColor,Number(data.dlProgress),progColor);
  140. I("ulText").textContent=(status==3&&data.ulStatus==0)?"...":data.ulStatus;
  141. drawMeter(I("ulMeter"),mbpsToAmount(Number(data.ulStatus*(status==3?oscillate():1))),meterBk,ulColor,Number(data.ulProgress),progColor);
  142. I("pingText").textContent=data.pingStatus;
  143. drawMeter(I("pingMeter"),msToAmount(Number(data.pingStatus*(status==2?oscillate():1))),meterBk,pingColor,Number(data.pingProgress),progColor);
  144. I("jitText").textContent=data.jitterStatus;
  145. drawMeter(I("jitMeter"),msToAmount(Number(data.jitterStatus*(status==2?oscillate():1))),meterBk,jitColor,Number(data.pingProgress),progColor);
  146. }
  147. function oscillate(){
  148. return 1+0.02*Math.sin(Date.now()/100);
  149. }
  150. //poll the status from the worker (this will call updateUI)
  151. setInterval(function(){
  152. if(w) w.postMessage('status');
  153. },200);
  154. //update the UI every frame
  155. window.requestAnimationFrame=window.requestAnimationFrame||window.webkitRequestAnimationFrame||window.mozRequestAnimationFrame||window.msRequestAnimationFrame||(function(callback,element){setTimeout(callback,1000/60);});
  156. function frame(){
  157. requestAnimationFrame(frame);
  158. updateUI();
  159. }
  160. frame(); //start frame loop
  161. //function to (re)initialize UI
  162. function initUI(){
  163. drawMeter(I("dlMeter"),0,meterBk,dlColor,0);
  164. drawMeter(I("ulMeter"),0,meterBk,ulColor,0);
  165. drawMeter(I("pingMeter"),0,meterBk,pingColor,0);
  166. drawMeter(I("jitMeter"),0,meterBk,jitColor,0);
  167. I("dlText").textContent="";
  168. I("ulText").textContent="";
  169. I("pingText").textContent="";
  170. I("jitText").textContent="";
  171. I("ip").textContent="";
  172. }
  173. </script>
  174. <style type="text/css">
  175. html,body{
  176. border:none; padding:0; margin:0;
  177. background:#FFFFFF;
  178. color:#202020;
  179. }
  180. body{
  181. text-align:center;
  182. font-family:"Roboto",sans-serif;
  183. }
  184. h1{
  185. color:#404040;
  186. }
  187. #loading{
  188. background-color:#FFFFFF;
  189. color:#404040;
  190. text-align:center;
  191. }
  192. span.loadCircle{
  193. display:inline-block;
  194. width:2em;
  195. height:2em;
  196. vertical-align:middle;
  197. background:url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAIAAAACACAMAAAD04JH5AAAAP1BMVEUAAAB2dnZ2dnZ2dnZ2dnZ2dnZ2dnZ2dnZ2dnZ2dnZ2dnZ2dnZ2dnZ2dnZ2dnZ2dnZ2dnZ2dnZ2dnZ2dnZ2dnZyFzwnAAAAFHRSTlMAEvRFvX406baecwbf0casimhSHyiwmqgAAADpSURBVHja7dbJbQMxAENRahnN5lkc//5rDRAkDeRgHszXgACJoKiIiIiIiIiIiIiIiIiIiIj4HHspsrpAVhdVVguzrA4OWc10WcEqpwKbnBo0OU1Q5NSpsoJFTgOecrrdEag85DRgktNqfoEdTjnd7hrEHMEJvmRUYJbTYk5Agy6nau6Abp5Cm7mDBtRdPi9gyKdU7w4p1fsLvyqs8hl4z9/w3n/Hmr9WoQ65lAU4d7lMYOz//QboRR5jBZibLMZdAR6O/Vfa1PlxNr3XdS3HzK/HVPRu/KnLs8iAOh993VpRRERERMT/fAN60wwWaVyWwAAAAABJRU5ErkJggg==');
  198. background-size:2em 2em;
  199. margin-right:0.5em;
  200. animation: spin 0.6s linear infinite;
  201. }
  202. @keyframes spin{
  203. 0%{transform:rotate(0deg);}
  204. 100%{transform:rotate(359deg);}
  205. }
  206. #startStopBtn{
  207. display:inline-block;
  208. margin:0 auto;
  209. color:#6060AA;
  210. background-color:rgba(0,0,0,0);
  211. border:0.15em solid #6060FF;
  212. border-radius:0.3em;
  213. transition:all 0.3s;
  214. box-sizing:border-box;
  215. width:8em; height:3em;
  216. line-height:2.7em;
  217. cursor:pointer;
  218. box-shadow: 0 0 0 rgba(0,0,0,0.1), inset 0 0 0 rgba(0,0,0,0.1);
  219. }
  220. #startStopBtn:hover{
  221. box-shadow: 0 0 2em rgba(0,0,0,0.1), inset 0 0 1em rgba(0,0,0,0.1);
  222. }
  223. #startStopBtn.running{
  224. background-color:#FF3030;
  225. border-color:#FF6060;
  226. color:#FFFFFF;
  227. }
  228. #startStopBtn:before{
  229. content:"Start";
  230. }
  231. #startStopBtn.running:before{
  232. content:"Abort";
  233. }
  234. #serverArea{
  235. margin-top:1em;
  236. }
  237. #test{
  238. margin-top:2em;
  239. margin-bottom:12em;
  240. }
  241. div.testArea{
  242. display:inline-block;
  243. width:16em;
  244. height:12.5em;
  245. position:relative;
  246. box-sizing:border-box;
  247. }
  248. div.testName{
  249. position:absolute;
  250. top:0.1em; left:0;
  251. width:100%;
  252. font-size:1.4em;
  253. z-index:9;
  254. }
  255. div.meterText{
  256. position:absolute;
  257. bottom:1.55em; left:0;
  258. width:100%;
  259. font-size:2.5em;
  260. z-index:9;
  261. }
  262. div.meterText:empty:before{
  263. content:"0.00";
  264. }
  265. div.unit{
  266. position:absolute;
  267. bottom:2em; left:0;
  268. width:100%;
  269. z-index:9;
  270. }
  271. div.testArea canvas{
  272. position:absolute;
  273. top:0; left:0; width:100%; height:100%;
  274. z-index:1;
  275. }
  276. div.testGroup{
  277. display:inline-block;
  278. }
  279. div.visible{
  280. animation: fadeIn 0.4s;
  281. display:block;
  282. }
  283. div.hidden{
  284. animation: fadeOut 0.4s;
  285. display:none;
  286. }
  287. @keyframes fadeIn{
  288. 0%{
  289. opacity:0;
  290. }
  291. 100%{
  292. opacity:1;
  293. }
  294. }
  295. @keyframes fadeOut{
  296. 0%{
  297. display:block;
  298. opacity:1;
  299. }
  300. 100%{
  301. display:block;
  302. opacity:0;
  303. }
  304. }
  305. @media all and (max-width:65em){
  306. body{
  307. font-size:1.5vw;
  308. }
  309. }
  310. @media all and (max-width:40em){
  311. body{
  312. font-size:0.8em;
  313. }
  314. div.testGroup{
  315. display:block;
  316. margin: 0 auto;
  317. }
  318. }
  319. </style>
  320. <title>HTML5 Speedtest</title>
  321. </head>
  322. <body onload="initServers()">
  323. <h1>HTML5 Speedtest</h1>
  324. <div id="loading" class="visible">
  325. <p id="message"><span class="loadCircle"></span>Selecting a server...</p>
  326. </div>
  327. <div id="testWrapper" class="hidden">
  328. <div id="startStopBtn" onclick="startStop()"></div>
  329. <div id="serverArea">
  330. Server: <span id="server"></span>
  331. </div>
  332. <div id="test">
  333. <div class="testGroup">
  334. <div class="testArea">
  335. <div class="testName">Download</div>
  336. <canvas id="dlMeter" class="meter"></canvas>
  337. <div id="dlText" class="meterText"></div>
  338. <div class="unit">Mbps</div>
  339. </div>
  340. <div class="testArea">
  341. <div class="testName">Upload</div>
  342. <canvas id="ulMeter" class="meter"></canvas>
  343. <div id="ulText" class="meterText"></div>
  344. <div class="unit">Mbps</div>
  345. </div>
  346. </div>
  347. <div class="testGroup">
  348. <div class="testArea">
  349. <div class="testName">Ping</div>
  350. <canvas id="pingMeter" class="meter"></canvas>
  351. <div id="pingText" class="meterText"></div>
  352. <div class="unit">ms</div>
  353. </div>
  354. <div class="testArea">
  355. <div class="testName">Jitter</div>
  356. <canvas id="jitMeter" class="meter"></canvas>
  357. <div id="jitText" class="meterText"></div>
  358. <div class="unit">ms</div>
  359. </div>
  360. </div>
  361. <div id="ipArea">
  362. IP Address: <span id="ip"></span>
  363. </div>
  364. </div>
  365. <a href="https://github.com/adolfintel/speedtest">Source code</a>
  366. </div>
  367. </body>
  368. </html>