Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Auto Save

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <div class="container">
  <div class="pie-box">
    <div class="pie" id="clockPie">
      <div class="pie__segment" style="--value: 0; --over50: 0"></div>
      <!-- value 超過 50 就要把 over50 設為 1 -->
    </div>
  <div class="control">
    <button class="button" id="startBtn">start</button>
    <button class="button" id="cancelBtn" disabled>cancel</button>
  </div>
</div>   

              
            
!

CSS

              
                .pie {
  background: var(--remain-color, #639);
  border-radius: 100%;
  height: calc(var(--size, 200) * 1px);
  width: calc(var(--size, 200) * 1px);
  position: relative;
  /* overflow: hidden; */
  clip-path: circle(50%);
}
.pie__segment {
  --a: calc(var(--over50, 0) * -100%);
  --b: calc((1 + var(--over50, 0)) * 100%);
  /* clip-path: polygon(0 0, 100% 0, 100% 100%, 0 100%); */
  clip-path: polygon(var(--a) var(--a),var(--b) var(--a),var(--b) var(--b), var(--a) var(--b));
  /* 改使用剪裁多邊形 */
  height: 100%;
  position: absolute;
  width: 100%;
  transform: translate(0%, 50%) rotate( calc(var(--offset, 90)* 1deg));
  transform-origin: 100% 50%;
  transition: clip-path 0s;
}
.pie__segment:after,
.pie__segment:before {
  background: var(--pass-color, #f00); 
  content: '';
  position: absolute;
  height: 100%;
  width: 100%;
}
.pie__segment:before {
  --degrees: calc((var(--value,0)/100)*360);
  content: '';
  position: absolute;
  height: 100%;
  width: 100%;
  transform: translate(0, 100%) rotate( calc(var(--degrees) * 1deg));
  /* 因為 pie__segment 已經轉了90 度, 所以 translate 100% 會往左移 */
  transform-origin: 50% 0%;
  /* transition: transform 1s; */
}
.pie__segment:after {
  opacity: var(--over50, 0);
  /* transition-delay: 1s; */
}


* {
  box-sizing: border-box;
}
html, body{
  width: 100%;
  height: 100%;
}
body {
  background-color: #e0a0a0;
  display: flex;
  align-items: center ;
}
.container{
  width: 100%;
  /* height: 100%; */
}
.pie-box {
  --size: 200;
  --pass-color: #aa3333;
  --remain-color: #5555aa;
  height: calc(var(--size, 200) * 1px);
  width: calc(var(--size, 200) * 1px);
  margin: 0 auto;
}
.control{
  margin-top: calc((var(--size,200) / 10 )*1px);
  text-align: center;
}
.button {
  border: none;
  padding: 5px 10px;
  text-align: center;
  text-decoration: none;
  display: inline-block;
  font-size: 16px;
}

              
            
!

JS

              
                let timer;
const time = 10;
let timeLeft = time; //timeLeft in sec

window.addEventListener('DOMContentLoaded', (event)=>{
  startBtn.addEventListener('click', start);
  cancelBtn.addEventListener('click', cancel);
})

function updateBtnState(state){
  switch(state) {
    case 'start':
      startBtn.setAttribute("disabled", "disabled");
      cancelBtn.removeAttribute("disabled");
      break;
    case 'finish':
    case 'cancel':
      startBtn.removeAttribute("disabled");
      cancelBtn.setAttribute("disabled", "disabled");
      break;
    default:
      console.log("unknown state")
  }
}

function updatePie(percentage){
  // paint pie by percentage
  const pie = document.querySelector('.pie__segment') 
  pie.style.cssText = `--value: ${percentage}; --over50: ${percentage >= 50 ? 1 : 0}`; 
}

function start(event){
  if(this.disabled){
    return 
  }
  updateBtnState('start')
  console.log('click start')
  timer = setInterval(countDown, 1000)
}
function cancel(event){
  if(this.disabled){
    return 
  }
  clearInterval(timer);
  finish();
  console.log('click cancel')
}
function finish(){
  updateBtnState("finish");
  console.log("Finish!!!");
  timeLeft = time;
  updatePie(0);
}
function countDown(){
  if(--timeLeft < 0){
    finishTimer();
    return
  }
  console.log(timeLeft/time*100)
  updatePie(100 - timeLeft/time*100);
}
function finishTimer(){
  console.log("finish timer")
  clearInterval(timer);
  finish();
}

              
            
!
999px

Console