How can I highlight 25 minutes on an analogue clock using CSS gradients?

Viewed 78

I'm trying to implement a countdown timer but on an analogue clock instead of a digital one. My goal is to highlight the next 25 minutes as a circle sector that starts from the minutes clock handle.

This is what I have so far: https://codepen.io/seifjo/pen/ExVQLOd

This is the relevant code from the above codepen:

* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

body {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  background: #68a4ff;
}

.clock {
  width: 350px;
  height: 350px;
  display: flex;
  align-items: center;
  justify-content: center;
  background: #bcd0e7;
  border: 20px solid #fff;
  border-radius: 50%;
  background-size: cover;
  box-shadow: inset 0 0 30px rgba(0, 0, 0, .2), 0 20px 20px rgba(0, 0, 0, .2), 0 0 0 4px rgba(255, 255, 255, 1);
}

.clock {
  background-image: url(clock.png), linear-gradient(330deg, transparent 50%, #fff 50%), linear-gradient(540deg, #fff 50%, transparent 50%);
}

.clock::before {
  content: '';
  position: absolute;
  width: 15px;
  height: 15px;
  background: #848484;
  border: 2px solid #fff;
  border-radius: 50%;
  z-index: 10000;
}

.clock .hour,
.clock .min,
.clock .sec {
  position: absolute;
}

.clock .hour,
.hr {
  width: 160px;
  height: 160px;
}

.clock .min,
.mn {
  width: 190px;
  height: 190px;
}

.clock .sec,
.sc {
  width: 230px;
  height: 230px;
}

.hr,
.mn,
.sc {
  display: flex;
  justify-content: center;
  position: absolute;
  border-radius: 50%;
}

.hr::before {
  content: '';
  position: absolute;
  width: 8px;
  height: 80px;
  background: #848484;
  z-index: 10;
  border-radius: 6px 6px 0 0;
}

.mn:before {
  content: '';
  position: absolute;
  width: 4px;
  height: 90px;
  background: #d6d6d6;
  z-index: 11;
  border-radius: 6px 6px 0 0;
}

.sc:before {
  content: '';
  position: absolute;
  width: 2px;
  height: 150px;
  background: #ff6767;
  z-index: 12;
  border-radius: 6px 6px 0 0;
}
<div class="clock">
  <div class="mask"></div>
  <div class="hour">
    <div class="hr" id="hr">

    </div>
  </div>
  <div class="min">
    <div class="mn" id="mn">

    </div>
  </div>
  <div class="sec">
    <div class="sc" id="sc">

    </div>
  </div>
</div>
<script>
  const deg = 6;
  const hr = document.querySelector("#hr");
  const mn = document.querySelector("#mn");
  const sc = document.querySelector("#sc");
  setInterval(() => {
    let day = new Date();
    let hh = day.getHours() * 30;
    let mm = day.getMinutes() * deg;
    let ss = day.getSeconds() * deg;

    hr.style.transform = `rotateZ(${(hh)+(mm/12)}deg)`;
    mn.style.transform = `rotateZ(${mm}deg)`;
    sc.style.transform = `rotateZ(${ss}deg)`;
  }, 1000);
</script>

I managed to highlight the next 25 minutes starting from 3 o'clock. But I can't figure out the relation between the minutes and the degrees. Eventually this will be a react app where I'll pass the current minute as a prop.

1 Answers

Since the highlight sector is w.r.t current time, I remove the style from css and calculate the style in script.

please note the solution only works for highlight period <= 30mins. Since the background image is a half circle. For more then 30mins, you need another second half circle and by controlling how many they overlap, you will highlight sector > 30mins

* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

body {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  background: #68a4ff;
}

.clock {
  width: 350px;
  height: 350px;
  display: flex;
  align-items: center;
  justify-content: center;
  background: #bcd0e7;
  border: 20px solid #fff;
  border-radius: 50%;
  background-size: cover;
  box-shadow: inset 0 0 30px rgba(0, 0, 0, .2), 0 20px 20px rgba(0, 0, 0, .2), 0 0 0 4px rgba(255, 255, 255, 1);
}

.clock::before {
  content: '';
  position: absolute;
  width: 15px;
  height: 15px;
  background: #848484;
  border: 2px solid #fff;
  border-radius: 50%;
  z-index: 10000;
}

.clock .hour,
.clock .min,
.clock .sec {
  position: absolute;
}

.clock .hour,
.hr {
  width: 160px;
  height: 160px;
}

.clock .min,
.mn {
  width: 190px;
  height: 190px;
}

.clock .sec,
.sc {
  width: 230px;
  height: 230px;
}

.hr,
.mn,
.sc {
  display: flex;
  justify-content: center;
  position: absolute;
  border-radius: 50%;
}

.hr::before {
  content: '';
  position: absolute;
  width: 8px;
  height: 80px;
  background: #848484;
  z-index: 10;
  border-radius: 6px 6px 0 0;
}

.mn:before {
  content: '';
  position: absolute;
  width: 4px;
  height: 90px;
  background: #d6d6d6;
  z-index: 11;
  border-radius: 6px 6px 0 0;
}

.sc:before {
  content: '';
  position: absolute;
  width: 2px;
  height: 150px;
  background: #ff6767;
  z-index: 12;
  border-radius: 6px 6px 0 0;
}
<div class="clock">
  <div class="mask"></div>
  <div class="hour">
    <div class="hr" id="hr">

    </div>
  </div>
  <div class="min">
    <div class="mn" id="mn">

    </div>
  </div>
  <div class="sec">
    <div class="sc" id="sc">

    </div>
  </div>
</div>
<script>
  const deg = 6;
  const hr = document.querySelector("#hr");
  const mn = document.querySelector("#mn");
  const sc = document.querySelector("#sc");
  setInterval(() => {
    let day = new Date();
    let hh = day.getHours() * 30;
    let mm = day.getMinutes() * deg;
    let ss = day.getSeconds() * deg;

    hr.style.transform = `rotateZ(${(hh)+(mm/12)}deg)`;
    mn.style.transform = `rotateZ(${mm}deg)`;
    sc.style.transform = `rotateZ(${ss}deg)`;
  }, 1000);

  let lauchTime = new Date();
  let clock = document.querySelector('.clock');
  let startDeg = ((lauchTime.getMinutes() * deg) + 360 - 90) % 360;
  let highlightPeriod = 25;
  let endDeg = startDeg + (highlightPeriod * deg);

clock.style.backgroundImage = `linear-gradient(${startDeg}deg, transparent 50%,  #fff 50%), linear-gradient(${endDeg}deg,  #fff 50%, transparent 50%)`;
</script>

Related