svg動(dòng)畫(huà)(二)

1.目標(biāo)

實(shí)現(xiàn)一個(gè)環(huán)形倒計(jì)時(shí)的動(dòng)畫(huà)。

2.具體操作

2.1 畫(huà)一個(gè)環(huán)形

上代碼:

    <figure class="chart" >
        <svg width="80" height="80">
            <circle class="outer" cx="50%" cy="50%" r="42.5%"/>
        </svg>
    </figure>
.chart {
    position: relative;
    display: inline-block;
    color: #999;
    font-size: 20px;
    text-align: center;
    height: 80px;
    width: 80px;
    .outer {
        fill: transparent;
        stroke: #FD8B37;
        stroke-width: 6;
        stroke-dasharray: 352;
        stroke-dashoffset: 0;
    }
}

最后的效果圖:


屏幕快照 2018-05-08 下午5.01.23.png
2.1.1 代碼分析

<svg width="80" height="80">
指定了svg的大小,因?yàn)閣idth和height沒(méi)有帶單位,那么svg的大小就是:80像素x80像素。
<circle class="outer" cx="50%" cy="50%" r="42.5%"/>
在svg里面,circle是眾多svg元素的一個(gè)。類(lèi)似的還有rect、text等等。
想知道更多的svg元素,可以打開(kāi)這個(gè)鏈接。
https://developer.mozilla.org/en-US/docs/Web/SVG/Element
現(xiàn)在具體說(shuō)說(shuō)circle這個(gè)元素。
cx、cy指的是圓心所在的位置。r指的就是半徑的長(zhǎng)度。
circle更多的解釋?zhuān)梢圆榭矗?br> https://developer.mozilla.org/en-US/docs/Web/SVG/Element/circle
僅僅指定了圓心、半徑、顏色畫(huà)出來(lái)的是實(shí)心圓,不是圓環(huán)。
這個(gè)時(shí)候需要將圓的填充色改為透明的。
fill: transparent;
這樣改了之后,圓就不可見(jiàn)了。這時(shí)就需要設(shè)置邊框了。
這個(gè)時(shí)候就需要使用svg的stoke相關(guān)屬性了。stroke可以理解為“筆畫(huà)”。
stroke: 指定stroke的顏色。
stroke-width: 指定stroke的寬度。
stroke-dasharray: 指定stroke的實(shí)線和虛線的長(zhǎng)度。我在這里只用了一個(gè)值,表示的是實(shí)線的長(zhǎng)度是352,虛線的長(zhǎng)度也是352.但是圓環(huán)的周長(zhǎng)沒(méi)有超過(guò)352,看起來(lái)就都是實(shí)線。
stroke-dashoffset: 指定stroke的偏移值。
有關(guān)stroke更多的解釋?zhuān)榭聪旅娴牟┛停?br> http://www.zhangxinxu.com/wordpress/2014/04/animateion-line-drawing-svg-path-%E5%8A%A8%E7%94%BB-%E8%B7%AF%E5%BE%84/
https://developer.mozilla.org/en-US/docs/Web/SVG/Attribute/stroke-dasharray

2.2 讓環(huán)形動(dòng)起來(lái)
    <figure class="chart" >
        <svg width="80" height="80">
            <circle class="outer circle-anim" cx="50%" cy="50%" r="42.5%" transform="rotate(-90, 40, 40)"/>
        </svg>
    </figure>
.chart {
    position: relative;
    display: inline-block;
    color: #999;
    font-size: 20px;
    text-align: center;
    height: 80px;
    width: 80px;
    .outer {
        z-index: 1000;
        fill: transparent;
        stroke: #FD8B37;
        stroke-width: 6;
        stroke-dasharray: 352;
        stroke-dashoffset: 0;
        stroke-linecap: round;
        
        /* firefox bug fix - won't rotate at 90deg angles */
        -moz-transform: rotate(-89deg) translateX(-190px);
    }
    .circle-anim {
        animation-timing-function:linear;
        -webkit-animation: show100 5s;
        animation: show100 5s;
    }
}

  @-webkit-keyframes show100 {
    from {
      stroke-dashoffset: 352;
    }
    
    to {
      stroke-dashoffset: 0;
    }
  }
  
  @keyframes show100 {
    from {
      stroke-dashoffset: 352;
    }
    
    to {
      stroke-dashoffset: 0;
    }
  }
2.2.1 代碼分析

transform="rotate(-90, 40, 40)"
這樣只是讓圓環(huán)旋轉(zhuǎn)90度。

2.3 讓環(huán)形兩端變成圓角

讓圓環(huán)兩端加上圓角,只需要設(shè)置屬性stroke-linecap: round;就行了

2.4 讓環(huán)形從純色變?yōu)闈u變色
    <figure class="chart" >
        <svg width="80" height="80">
            <defs>
                <linearGradient x1="1" y1="0" x2="0" y2="0" id="gradient3">
                    <stop offset="0%" stop-color="#e52c5c"></stop>
                    <stop offset="100%" stop-color="#ab5aea"></stop>
                </linearGradient>
            </defs>
            <circle class="outer circle-anim" cx="50%" cy="50%" r="42.5%" stroke="url('#gradient3')" transform="rotate(-90, 40, 40)"/>
        </svg>
    </figure>

同時(shí)去掉樣式outer中的stroke: #FD8B37;

屏幕快照 2018-05-08 下午5.42.51.png

2.4.1 代碼分析

defs也是svg的元素。是來(lái)定義復(fù)用標(biāo)簽的。
linearGradient是定義的一個(gè)漸變色。
更多l(xiāng)inearGradient的解釋查看:
http://www.w3school.com.cn/svg/svg_grad_linear.asp
https://blog.csdn.net/chy555chy/article/details/53415770
x1="1" y1="0" x2="0" y2="0"表示水平漸變。

<stop offset="0%" stop-color="#e52c5c"></stop>
<stop offset="100%" stop-color="#ab5aea"></stop>

定義的是開(kāi)始和結(jié)束的漸變色。

3.最后的代碼

    <figure class="chart" >
        <svg width="80" height="80">
            <defs>
                <linearGradient x1="1" y1="0" x2="0" y2="0" id="gradient3">
                    <stop offset="0%" stop-color="#e52c5c"></stop>
                    <stop offset="100%" stop-color="#ab5aea"></stop>
                </linearGradient>
            </defs>
            <circle class="outer circle-anim" cx="50%" cy="50%" r="42.5%" stroke="url('#gradient3')" transform="rotate(-90, 40, 40)"/>
        </svg>
    </figure>
.chart {
    position: relative;
    display: inline-block;
    color: #999;
    font-size: 20px;
    text-align: center;
    height: 80px;
    width: 80px;
    .outer {
        z-index: 1000;
        fill: transparent;
        // stroke: #FD8B37;
        stroke-width: 6;
        stroke-dasharray: 352;
        stroke-dashoffset: 0;
        stroke-linecap: round;
        
        /* firefox bug fix - won't rotate at 90deg angles */
        -moz-transform: rotate(-89deg) translateX(-190px);
    }
    .circle-anim {
        animation-timing-function:linear;
        -webkit-animation: show100 10s;
        animation: show100 10s;
    }
}

  @-webkit-keyframes show100 {
    from {
      stroke-dashoffset: 352;
    }
    
    to {
      stroke-dashoffset: 0;
    }
  }
  
  @keyframes show100 {
    from {
      stroke-dashoffset: 352;
    }
    
    to {
      stroke-dashoffset: 0;
    }
  }
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 本文主要想談一談前端關(guān)于漸變圓環(huán)的制作,效果圖如下: 注意:部分iphone 不支持css3旋轉(zhuǎn)大于等于90度,故...
    花伊儂閱讀 6,020評(píng)論 0 2
  • 一、柵格圖形和矢量圖形柵格圖形:也稱(chēng)位圖,圖像由一組二維像素網(wǎng)格表示。Canvas 2d API 就是一款柵格圖形...
    linda102閱讀 1,216評(píng)論 0 4
  • 在React Native中使用ARTReact Native ART 究竟是什么?所謂ART,是一個(gè)在React...
    JackfengGG閱讀 9,667評(píng)論 2 50
  • 你可以把這篇文章看做通往精通SVG動(dòng)畫(huà)之路的第一步,里面的鏈接資源也是很好的學(xué)習(xí)資料。所以趕緊收藏本文然后開(kāi)始你的...
    小刀閱讀 5,990評(píng)論 1 25
  • 今天是我從很遙遠(yuǎn)的地方回家的第十天了。 我原本以為今天也是很普通的一天。 我如同往常一樣睜開(kāi)眼睛,卻發(fā)現(xiàn)自己眼前被...
    時(shí)雨SAYA閱讀 636評(píng)論 0 4

友情鏈接更多精彩內(nèi)容