{
let [dasharray, rotate, stroke, all] = document.querySelectorAll('circle');
const clock = document.querySelector('.clock');
let start = 0;
let deg = -90;
const [minStroke, maxStroke] = [2,6];
let strokeWidth = minStroke;
let then = Date.now();
const duration = 1250;
let aStart, rafID;
function frame(time) {
if(!aStart) {
aStart = time;
rafID = window.requestAnimationFrame(frame);
return;
}
const progress = (time - aStart) / duration;
let deg = progress * 360;
let transform;
if (progress < 1) {
rotate.style.transform = `rotate(${deg}deg)`;
} else {
aStart = null;
}
rafID = window.requestAnimationFrame(frame);
}
function sine() {
let now = Date.now();
let delta = (now - then) / duration;
then = now;
const pos = 90 * Math.sin( start );
let width = Math.abs(maxStroke * Math.sin( strokeWidth ));
if (width >= maxStroke || width <= minStroke) {
width = 2;
}
if(dasharray && rotate && stroke) {
dasharray.setAttribute('stroke-dasharray', `${Math.abs(pos)} 360`);
stroke.setAttribute('stroke-width', width);
all.setAttribute('stroke-dasharray', `${Math.abs(pos)} 360`);
all.style.transform = `rotate(${deg}deg)`;
all.setAttribute('stroke-width', width);
}
start += 0.05;
strokeWidth += 0.05;
if (deg >= 270) {
deg = -90;
}
deg += 360 / (duration / (duration * delta));
window.requestAnimationFrame(sine);
}
sine();
rafID = window.requestAnimationFrame(frame);
setTimeout(() => clock.classList.add('running'), 0);
}