speedtest-robot / gauge.js
Noobjay's picture
Create gauge.js
b409150 verified
Raw History Blame Contribute Delete
845 Bytes
const canvas = document.getElementById("gauge");
const ctx = canvas.getContext("2d");
let angle = 0;
let targetSpeed = 0;
function drawGauge(speed) {
ctx.clearRect(0, 0, canvas.width, canvas.height);
ctx.beginPath();
ctx.arc(150, 150, 100, Math.PI, 2 * Math.PI);
ctx.strokeStyle = "#00ffe1";
ctx.lineWidth = 15;
ctx.stroke();
// Needle
const angle = Math.PI + (speed / 100) * Math.PI;
const x = 150 + 90 * Math.cos(angle);
const y = 150 + 90 * Math.sin(angle);
ctx.beginPath();
ctx.moveTo(150, 150);
ctx.lineTo(x, y);
ctx.strokeStyle = "#ff0040";
ctx.lineWidth = 5;
ctx.stroke();
}
function animateGauge(speed) {
let current = 0;
const interval = setInterval(() => {
if (current >= speed) {
clearInterval(interval);
} else {
current += 1;
drawGauge(current);
}
}, 15);
}