An analog clock that changes its background depending on the time of the day. I used linear icons to help indicate AM and PM time. See all the subtle changes one it 6pm. I used pure javascript for clock functionality.
Created
February 5, 2019 03:19
-
-
Save m2paulc/d09daaf9c8d6c026a052de66a63d18bc to your computer and use it in GitHub Desktop.
JavaScript analog clock
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<script src="https://cdn.linearicons.com/free/1.0.0/svgembedder.min.js"></script> | |
<div class="frame"> | |
<svg class="lnr lnr-moon"><use xlink:href="#lnr-moon"></use></svg> | |
<svg class="lnr lnr-sun"><use xlink:href="#lnr-sun"></use></svg> | |
<div class="clock"> | |
<div class="clock-face"> | |
<div class="clock-face-numbers"> | |
<span class="top">12</span> | |
<span class="right">3</span> | |
<span class="bottom">6</span> | |
<span class="left">9</span> | |
</div> | |
<div class="hand-pivot"></div> | |
<div class="hand hand-hr"></div> | |
<div class="hand hand-min"></div> | |
<div class="hand hand-sec"></div> | |
</div> | |
</div> | |
</div> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const frame = document.querySelector('.frame'); | |
const moon = document.querySelector('.lnr-moon'); | |
const sun = document.querySelector('.lnr-sun'); | |
const clockShadow = document.querySelector('.clock'); | |
const secHand = document.querySelector('.hand-sec'); | |
const minHand = document.querySelector('.hand-min'); | |
const hrHand = document.querySelector('.hand-hr'); | |
const allHands = document.querySelectorAll('.hand'); | |
function setTime() { | |
const now = new Date(); | |
//return sec according to local time | |
const seconds = now.getSeconds(); | |
//associate the degree for each sec | |
const secDegrees = ((seconds / 60) * 360) + 90; | |
secHand.style.transform = `rotate(${secDegrees}deg)`; | |
//return min according to local time | |
const minutes = now.getMinutes(); | |
//associate the degree for each min | |
const minDegrees = ((minutes / 60) * 360) + 90; | |
minHand.style.transform = `rotate(${minDegrees}deg)`; | |
//return hr according to local time | |
const hours = now.getHours(); | |
//associate the degree for each hr | |
const hrDegrees = ((hours / 12) * 360) + 90; | |
hrHand.style.transform = `rotate(${hrDegrees}deg)`; | |
//change frame background for night and day | |
if(hours >= 18 || hours <= 5) { | |
frame.classList.remove('day'); | |
frame.classList.add('night'); | |
moon.style.visibility = 'visible'; | |
moon.style.opacity = '1'; | |
sun.style.visibility = 'hidden'; | |
sun.style.opacity = '0'; | |
clockShadow.style.boxShadow = '12px -6px 8px 2px rgba(0, 0, 0, 0.5)'; | |
} else { | |
frame.classList.remove('night'); | |
frame.classList.add('day'); | |
moon.style.visibility = 'hidden'; | |
moon.style.opacity = '0'; | |
sun.style.visibility = 'visible'; | |
sun.style.opacity = '1'; | |
clockShadow.style.boxShadow = '-12px -6px 8px 2px rgba(0, 0, 0, 0.5)'; | |
} | |
//remove transition when secHand reaches full cycle then re-apply the transition to 90deg | |
if(secDegrees === 444 || secDegrees === 90) { | |
allHands.forEach(hand => hand.style.transition = 'none'); | |
} else { | |
allHands.forEach(hand => hand.style.transition = ''); | |
} | |
} | |
setInterval(setTime, 1000); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
.frame { | |
position: absolute; | |
top: 50%; | |
left: 50%; | |
width: 460px; | |
height: 460px; | |
margin-top: -200px; | |
margin-left: -200px; | |
border-radius: 2px; | |
box-shadow: 4px 8px 16px 0 rgba(0,0,0,0.1); | |
background: #4CA1AF; | |
overflow: hidden; | |
color: #333; | |
font-family: 'Open Sans', Helvetica, sans-serif; | |
-webkit-font-smoothing: antialiased; | |
-moz-osx-font-smoothing: grayscale; | |
&.day { | |
background: #4CA1AF; /* fallback for old browsers */ | |
background-image: url(https://images.unsplash.com/photo-1494548162494-384bba4ab999?ixlib=rb-1.2.1&q=85&fm=jpg&crop=entropy&cs=srgb&ixid=eyJhcHBfaWQiOjE0NTg5fQ); | |
background-position: center; | |
background-size: cover; | |
} | |
&.night { | |
background: #4B79A1; /* fallback for old browsers */ | |
background-image: url(https://images.unsplash.com/photo-1419242902214-272b3f66ee7a?ixlib=rb-1.2.1&q=85&fm=jpg&crop=entropy&cs=srgb&ixid=eyJhcHBfaWQiOjE0NTg5fQ); | |
background-position: center; | |
background-size: cover; | |
} | |
.lnr { | |
display: inline-block; | |
position: absolute; | |
fill: currentColor; | |
width: 2em; | |
height: 2em; | |
vertical-align: -0.05em; | |
padding: 10px; | |
} | |
.lnr-moon { | |
top: 10px; | |
color: #fff; | |
font-size: 42px; | |
} | |
.lnr-sun { | |
color: #fff; | |
font-size: 60px; | |
right: 0px; | |
} | |
} | |
.clock { | |
width: 80%; | |
height: 80%; | |
border-radius: 50%; | |
margin: 25px auto; | |
position: relative; | |
padding: 1.5rem; | |
background: #ad5389; /* fallback for old browsers */ | |
background: -webkit-linear-gradient(to top, #3c1053, #ad5389); /* Chrome 10-25, Safari 5.1-6 */ | |
background: linear-gradient(to top, #3c1053, #ad5389); /* W3C, IE 10+/ Edge, Firefox 16+, Chrome 26+, Opera 12+, Safari 7+ */ | |
box-shadow: 0px 12px 6px 2px rgba(0, 0, 0, 0.5); | |
&-face { | |
position: absolute; | |
width: 80%; | |
height: 80%; | |
border: 5px solid #fff; | |
border-radius: 50%; | |
top: 9%; | |
left: 9%; | |
&-numbers span { | |
position: absolute; | |
font-size: 2rem; | |
color: #fff; | |
} | |
.top { | |
left: 45%; | |
} | |
.right { | |
top: 46%; | |
right: 1%; | |
} | |
.bottom { | |
bottom: .5%; | |
left: 48%; | |
} | |
.left { | |
top: 45%; | |
left: 1%; | |
} | |
.hand { | |
width: 50%; | |
height: 6px; | |
background-color: #fff; | |
position: absolute; | |
top: 50%; | |
transform-origin: right; | |
transform: rotate(90deg); | |
transition: all 0.05s cubic-bezier(0, 0, 0.52, 2.51) 0s; | |
&-pivot { | |
position: absolute; | |
width: 20px; | |
height: 20px; | |
background: #fff; | |
border: 2px solid #fff; | |
border-radius: 50%; | |
top: 47%; | |
left: 47%; | |
z-index: 2; | |
} | |
&-hr { | |
left: 10.5%; | |
height: 10px; | |
width: 40%; | |
} | |
&-min { | |
left: 5.5%; | |
width: 45%; | |
} | |
&-sec { | |
background-color: rgba(255, 255, 255, 0.6); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment