MCPcopy
hub / github.com/librespeed/speedtest / mbpsToRotation

Function mbpsToRotation

frontend/javascript/index.js:414–434  ·  view source on GitHub ↗

* Convert a speed in Mbits per second to a rotation for the gauge * @param {string} speed Speed in Mbits * @param {boolean} oscillate If the gauge should wiggle a bit * @returns {number} Rotation for the gauge in degrees

(speed, oscillate)

Source from the content-addressed store, hash-verified

412 * @returns {number} Rotation for the gauge in degrees
413 */
414function mbpsToRotation(speed, oscillate) {
415 speed = Number(speed);
416 if (speed <= 0) return 0;
417
418 const minSpeed = 0;
419 const maxSpeed = 10000; // 10 Gbps maxes out the gauge
420 const minRotation = 0;
421 const maxRotation = 180;
422
423 // Can't do log10 of values less than one, +1 all to keep it fair
424 const logMinSpeed = Math.log10(minSpeed + 1);
425 const logMaxSpeed = Math.log10(maxSpeed + 1);
426 const logSpeed = Math.log10(speed + 1);
427
428 const power = (logSpeed - logMinSpeed) / (logMaxSpeed - logMinSpeed);
429 const oscillation = oscillate ? 1 + 0.01 * Math.sin(Date.now() / 100) : 1;
430 const rotation = power * oscillation * maxRotation;
431
432 // Make sure we stay within bounds at all times
433 return Math.max(Math.min(rotation, maxRotation), minRotation);
434}
435
436/**
437 * Convert a number to a user friendly version

Callers 1

renderUIFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected