首頁  >  文章  >  web前端  >  如何僅使用 CSS 創建具有透明背景的平滑動畫圓形繪製效果?

如何僅使用 CSS 創建具有透明背景的平滑動畫圓形繪製效果?

Mary-Kate Olsen
Mary-Kate Olsen原創
2024-10-25 05:44:29828瀏覽

How to Create a Smoothly Animated Circle Drawing Effect with a Transparent Background Using CSS Only?

僅CSS 動畫繪製帶有邊框半徑和透明背景的圓

問題:

如何創建動畫邊框-半徑為透明背景的圓,同時遮蓋圓的初始部分以獲得繪圖效果?

解:

  1. 建立畫布:

    • 建立畫布:
    使用絕對定位和邊框定義容器以供參考。
  2. 為遮罩建立一個 50% 寬度、絕對定位的半剪輯元素。

    • 建立圓圈:
    在半剪輯內,放置一個具有透明邊框和藍色頂部的圓圈,然後左邊框顏色。
  3. 將圓圈在半剪輯內右對齊。

    • 動畫繪圖:
    使用CSS 動畫將圓形從初始-45 度位置旋轉135 度,以呈現繪圖的外觀。
  4. 將半剪輯動畫旋轉360 度以保持遮罩不變

    • 保持透明度:
    為主體添加重複的線性漸變,以確保透明度並提供視覺上下文。
  5. 將漸變縮放到容器大小並將背景重複設為不重複。

    • 修補半圓:
    創造第二個半圓(固定),無動畫,旋轉 135 度。
  6. 動畫固定半圓的不透明度,使其在50% 後出現

    • 控制動畫時間:
根據需要調整動畫持續時間和時間以實現所需的繪製速度和效果。

<code class="css">body {
  background: repeating-linear-gradient(45deg, white 0px, lightblue 100px);
  height: 500px;
  background-size: 500px 500px;
  background-repeat: no-repeat;
}

#container {
  position: absolute;
  width: 400px;
  height: 400px;
  border: solid red 1px;
  animation: colors 4s infinite;
}

#halfclip {
  width: 50%;
  height: 100%;
  right: 0px;
  position: absolute;
  overflow: hidden;
  transform-origin: left center;
  animation: cliprotate 16s steps(2) infinite;
  -webkit-animation: cliprotate 16s steps(2) infinite;
}

.halfcircle {
  box-sizing: border-box;
  height: 100%;
  right: 0px;
  position: absolute;
  border: solid 25px transparent;
  border-top-color: blue;
  border-left-color: blue;
  border-radius: 50%;
}

#clipped {
  width: 200%;
  animation: rotate 8s linear infinite;
  -webkit-animation: rotate 8s linear infinite;
}

#fixed {
  width: 100%;
  transform: rotate(135deg);
  animation: showfixed 16s steps(2) infinite;
  -webkit-animation: showfixed 16s linear infinite;
}

@-webkit-keyframes cliprotate {
  0% { transform: rotate(0deg); }
  100% { transform: rotate(360deg); }
}

@keyframes cliprotate {
  0% { transform: rotate(0deg); }
  100% { transform: rotate(360deg); }
}

@-webkit-keyframes rotate {
  0% { transform: rotate(-45deg); }
  100% { transform: rotate(135deg); }
}

@keyframes rotate {
  0% { transform: rotate(-45deg); }
  100% { transform: rotate(135deg); }
}

@-webkit-keyframes showfixed {
  0% { opacity: 0; }
  49.9% { opacity: 0; }
  50% { opacity: 1; }
  100% { opacity: 1; }
}
</code>
程式碼片段:

以上是如何僅使用 CSS 創建具有透明背景的平滑動畫圓形繪製效果?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn