Skip to content

Instantly share code, notes, and snippets.

@hamannjames
Last active August 16, 2017 16:27
Show Gist options
  • Save hamannjames/f8990e6d1510546039a41da7a152003e to your computer and use it in GitHub Desktop.
Save hamannjames/f8990e6d1510546039a41da7a152003e to your computer and use it in GitHub Desktop.
Simple clock logic for an analogue clock on your screen.
// Link to code pen https://codepen.io/jaspercreel/pen/QMOepX
const secHand = document.getElementById('secHand');
const minHand = document.getElementById('minHand');
const hourHand = document.getElementById('hourHand');
const start = -90;
const date = new Date();
let seconds = date.getSeconds();
let minutes = date.getMinutes();
let hours = date.getHours();
const rotate = () => {
seconds++;
if (seconds === 60) {
seconds = 0;
minutes++;
}
if (minutes === 60) {
minutes = 0;
hours++;
}
if (hours === 12) {
hours = 0;
}
const secRotate = ((360/60) * seconds) + start;
const minRotate = ((360/60) * minutes) + start;
const hourRotate = ((360/12) * hours) + start;
secHand.setAttribute('style', 'transform: rotate(' + secRotate + 'deg)');
minHand.setAttribute('style', 'transform: rotate(' + minRotate + 'deg)');
hourHand.setAttribute('style', 'transform: rotate(' + hourRotate + 'deg)');
}
setInterval(rotate, 1000);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment