I have been working on a timer that is displaying 3 preset times based on the user's choice. I decided to include a countdown graphic in the shape of a clock which only has one hand and rotates 360 degrees for the full length of time assigned to it.
The problem I have is marrying up the original timer and the new display. I have the "clock" element drawn with canvas and I have a working timer, but I need to know how to take that time and utilise it to get the hand to move around. As it currently stands the hands will draw when I have it functioning, but don't display mostly because I haven't set an interval for the clock but I do have one for the original timer.
Full JS
((d) => {
let btn = d.getElementById("btn");
let reset = d.getElementById("reset");
let countdown = d.getElementById("countdown");
let btnLength = d.getElementById("btnLength");
// 25 Minutes
let short = 1500;
// 45 Minutes
let med = 2700;
// 90 Minutes
let long = 5400;
let counter;
let startTime = short;
let timerFormat = (s) => {
return (s - (s %= 60)) / 60 + (9 < s ? ":" : ":0") + s;
};
countdown.textContent = timerFormat(startTime);
let timer = () => {
startTime--;
countdown.textContent = timerFormat(startTime);
if (startTime === 0) clearInterval(counter);
};
let start = () => {
counter = counter || setInterval(timer, 950);
};
let stop = () => {
clearInterval(counter);
counter = undefined;
};
// Changes between Start and Stop button labelling.
btn.addEventListener("click", () => {
if (counter) {
stop();
btn.textContent = "Start";
} else {
start();
btn.textContent = "Stop";
}
});
// Stops counter, resets to original start time and
// resets button label to Start.
reset.onclick = () => {
stop();
startTime = short;
if (btn.textContent === "Stop") btn.textContent = "Start";
btnLength.textContent = "Short";
countdown.textContent = timerFormat(startTime);
};
// Changes timer legnth and button labelling
btnLength.addEventListener("click", () => {
if (startTime === short) {
startTime = med;
btnLength.textContent = "Medium";
} else if (startTime === med) {
startTime = long;
btnLength.textContent = "Long";
} else if (startTime === long) {
startTime = short;
btnLength.textContent = "Short";
}
countdown.textContent = timerFormat(startTime);
});
//Clock
let c = d.getElementById("canvas");
let ctx = c.getContext("2d");
let radius = c.height / 2;
ctx.translate(radius, radius);
radius = radius * 0.9;
showTime = (ctx, radius) => {
let startTime =
(startTime * Math.PI) / 30 + (startTime * Math.PI) / (30 * 60);
hand(ctx, startTime, radius * 0.8, radius * 0.07);
};
hand = (ctx, position, length, width) => {
ctx.beginPath();
ctx.lineWidth = width;
ctx.lineCap = "round";
ctx.moveTo(0, 0);
ctx.rotate(position);
ctx.lineTo(0, -length);
ctx.stroke();
ctx.rotate(-position);
};
clock = () => {
ctx.arc(0, 0, radius, 0, 2 * Math.PI, true);
ctx.fillStyle = "#f5afaf";
ctx.fill();
showTime();
};
clock();
})(document);
Html
<!DOCTYPE html>
<html lang="en">
<head>
<title>Document</title>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link rel="stylesheet" href="../styles/styles.css" />
<script defer src="../JS/timer.js"></script>
</head>
<body>
<div class="btn__container">
<div id="countdown"></div>
<button class="btn" id="btn">Start</button>
<button class="btn" id="btnLength">Short</button>
<button class="btn" id="reset">Reset</button>
<div class="clock">
<canvas id="canvas" width="300" height="300" style="background-color:none"></canvas>
</div>
</body>
</html>