css and canvas實現圓形進度條

進度條效果:
 
話很少說,上代碼
使用css動畫實現,看到一篇博客的啓發,稍微修改了下,
css實現的原理是用兩個半圓一開始隱藏,再分別旋轉180度,最後成爲一個整圓
半圓效果,一開始右邊的半圓在盒子左邊
加上動畫,實現右邊進度條效果
代碼:
<div class="container">
<!-- 右邊圓形 -->
<div class="rigth_content">
<div class="rotate_box right_rotate"></div>
</div>
</div>
<style>

.container {
padding: 30px;
padding-left: 100px;
width: 400px;
height: 200px;
border: 1px solid #2c3e50;
 
}
<!-- 旋轉動畫 -->
@-webkit-keyframes rotateright {
0% {
transform: rotate(0deg);
}
50%,100% {
transform: rotate(180deg);
}
}

.rigth_content {
width: 150px;
height: 150px;
/* overflow: hidden; */
position: relative;
background-color: #eee;
}

.rotate_box {
width: 150px;
height: 150px;
position: absolute;
<!-- 這裏我用的是內陰影效果,也能夠直接用border邊框 -->
box-shadow: 0 0 3px 10px #00ff00 inset;
border-radius: 50%;
top: 0px;
}

.right_rotate {
<!-- 定位到盒子的外部,一開始被遮擋 -->
left: -75px;
<!-- clip屬性:截取指定區域的內容,順序爲上,右,下,左,這裏不須要右邊的陰影,第二個參數取一半 -->
clip: rect(0px, 75px, auto, 0);
animation: rotateright 8s linear normal;
animation-fill-mode: forwards;
}
</style>
右邊的圓形繪成後,用一樣的方法畫出左邊的圓形
完整代碼:
<p>css實現:</p>
<div class="container">
  <!-- 左邊圓形 -->
  <div class="left_content">
    <div class="rotate_box left_rotate"></div>
  </div>
  <!-- 右邊圓形 -->
  <div class="rigth_content">
    <div class="rotate_box right_rotate"></div>
  </div>
</div>
<style>
.container {
  padding: 30px;
  padding-left: 100px;
  width: 400px;
  height: 200px;
  border: 1px solid #2c3e50;
  background-color: rgba(0, 0, 0, .5);
}
@-webkit-keyframes rotateright {
  0% {
    transform: rotate(0deg);
  }
  50%,100% {
    transform: rotate(180deg);
  }
}
@-webkit-keyframes rotateleft {
  0%,50% {
    transform: rotate(0deg);
  }
  100% {
    transform: rotate(180deg);
  }
}
.rigth_content {
  width: 150px;
  height: 150px;
  overflow: hidden;
  position: relative;
}
.left_content {
  width: 150px;
  height: 150px;
  overflow: hidden;
  position: relative;
  float: left;
}
.rotate_box {
  width: 150px;
  height: 150px;
  position: absolute;
  box-shadow: 0 0 3px 10px #00ff00 inset;
  border-radius: 50%;
  top: 0px;
}
.left_rotate {
  left: 75px;
  clip: rect(0px, auto, auto, 75px);
  animation: rotateleft 8s linear normal;
  animation-fill-mode: forwards;
}
.right_rotate {
  left: -75px;
  clip: rect(0px, 75px, auto, 0);
  animation: rotateright 8s linear normal;
  animation-fill-mode: forwards;
}

</style>

第二種實現方法,使用canvas繪圖,這種方法效果會更好一些,擴展性更好

我是寫在vue項目中的,繪製方法寫在mounted中
<p>canvas實現:</p>
<canvas width="500px" height="400px" style="border: 1px solid #000;" ref="myCanvas"></canvas>

mounted() {
    this.$nextTick(() => {
      const brush = this.$refs.myCanvas
      const ctx = brush.getContext("2d")
      ctx.strokeStyle = 'red'
      ctx.lineCap='round'
      ctx.lineWidth = 10
      // 初始角度,默認是在右邊3點鐘方向,旋轉90度到12點方位
      let startangle = -90*Math.PI/180
      // 初始繪製結束角度
      let end = -80
      let endangle = end*Math.PI/180
      // 進度文字百分比
      let text = 0
      console.log(new Date())
      // 時間須要本身換算,這裏我總用時是10秒,須要算的是計時器的時間間隔,總共360°,每秒旋轉36°
      // 1s是1000毫秒,爲了動畫過渡效果順暢,通常須要在1s內繪製幾回,這裏的時間拆分爲36/n 1000/n.例: 若n爲3次,則36/3,每次增長12°;1000/3,每333毫秒執行一次
      // 圓形進度條
      var timer = setInterval(() => {
        // 繪製完360度就中止
        if (startangle > 270*Math.PI/180) {
          console.log(new Date())
          clearInterval(timer)
        }
        ctx.beginPath()
        ctx.arc(200,200,100,startangle, endangle)
        ctx.stroke()
        startangle = endangle
        end += 12
        endangle = `${end}`*Math.PI/180
      }, 333)

      // 百分比進度
      // 這裏的時間計算: 百分比從0-99 執行100次,總用時10S,每秒10次,1000/10,每100毫秒執行一次
      var textTimer = setInterval(() => {
        // 到100%時中止
        if (text > 99) {
          console.log(new Date())
          clearInterval(textTimer)
        }
        // 清除上一次繪製的百分比,這裏只清除了文字區域,外圍的進度條區域沒有清除
        // 你也能夠把進度條和百分比的計時器合爲一個,每次繪製所有清除,不過進度條結束角度和每次增長的角度須要改變
        ctx.clearRect(150,150,100,100)
        ctx.font = "40px Arial"
        ctx.fillText(`${text}%`, 170, 210)
        text++
      }, 100)
    })
}

 

OK~~~完成css

相關文章
相關標籤/搜索