首頁  >  文章  >  web前端  >  如何用SVG製作酷炫動態圖示?(程式碼實例)

如何用SVG製作酷炫動態圖示?(程式碼實例)

青灯夜游
青灯夜游原創
2018-09-11 16:18:214254瀏覽

本章跟大家介紹在html5中如何用SVG製作酷炫動態圖示?(程式碼實例),有一定的參考價值,有需要的朋友可以參考一下,希望對你有幫助。

一、基本圖形元素

  svg有一些預先定義的形狀元素:矩形,圓形 ,橢圓,直線,折線,多邊形,路徑和文字

<!-- viewBox定义画布大小 width/height定义实际大小 -->
<svg xmlns="http://www.w3.org/2000/svg" version="1.1" width="300" height="300" viewBox="0 0 300 300">

    <!-- 直线 (x1,y1)与(x2,y2)为两点坐标 -->
    <line x1="0" y1="0" x2="250" y2="30" />

    <!-- 多边形 通过多个点的坐标形成封闭图形 -->
    <polygon points="5,5 100,100 50,200" />

    <!-- 矩形 (x,y)为左上角起始点 -->
    <rect x="100" y="100" width="120" height="100" />

    <!-- 圆形 (cx,cy)圆心点 r半径 -->
    <circle cx="100" cy="50" r="40" stroke="black"/>

    <!-- 文本 (x,y)左下角坐标  -->
    <text x="0" y="20" style="font-size:16px;font-weight: bold">Try SVG</text>

</svg>

二、樣式與效果 

svg元素的樣式可以寫成標籤的屬性,也可以寫在style裡面。以下是一些主要的樣式屬性:

   stroke: 筆觸顏色
   stroke-width:筆觸寬度
   stroke-opacity:筆觸的透明度
   fill:填色,即background
   fill-opacity:填色的透明度
   transform:圖形變換,類似css3 transform

svg也支援許多濾鏡效果,能做漸層、陰影、模糊、影像混合等等。不需要了解那麼多,例如要畫幾個彩色圓圈,用circle 配合fill 即可。

注意:transform 預設以svg左上角為基點,而不是圓心或其他中心。左上角是svg座標係原點。了解transform和座標系統,可以參考 這裡。

三、輔助元素

svg有幾個輔助元素:

     元素通常用來將相關圖形元素分組,以便統一操作,例如旋轉,縮放或添加相關樣式等。
     實現SVG現有圖形的重複使用,可重複使用單一SVG圖形元素,也可重複使用定義的群組元素。
     內部定義的元素不會直接顯示,可以不用事先定義樣式,而是在使用實例化的時候再定義。
     能夠創造自己的視窗,兼具的分組功能和初始不可見的特性。

對於上述的transform基點問題,可以透過巢狀標籤並偏移的位置,進而重設基點。如下畫出幾個水平排列的圓圈,並設定不同的縮放尺寸

<svg width="80px" height="80px" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 100" preserveAspectRatio="xMidYMid">
    <g transform="translate(20 50)">
        <circle cx="0" cy="0" r="7" fill="#e15b64" transform="scale(0.99275 0.99275)" />
    </g>
    <g transform="translate(40 50)">
        <circle cx="0" cy="0" r="7" fill="#f47e60" transform="scale(0.773605 0.773605)" />
    </g>
    <g transform="translate(60 50)">
        <circle cx="0" cy="0" r="7" fill="#f8b26a" transform="scale(0.42525 0.42525)" />
    </g>
    <g transform="translate(80 50)">
        <circle cx="0" cy="0" r="7" fill="#abbd81" transform="scale(0.113418 0.113418)" />
    </g>
</svg>

#四、動畫的實作

svg的動畫效果是基於動畫標籤元素實現的:

  實現單一屬性的過渡效果;
    實現transform變換動畫效果;
    實現路徑動畫效果。

svg的寫法很靈活,樣式可以寫成標籤屬性也可以寫在style裡面,動畫標籤可以透過xlink指定元素,也可以寫在動畫元素的內部。如下示範animateTransform的xlink寫法:

<svg xmlns="http://www.w3.org/2000/svg">
    <rect id="animateObject" x="20" y="20" width="50" height="50" fill="blue"></rect>
    <animateTransform
        xlink:href="#animateObject" <!-- 指定动画元素 -->
        attributeName="transform"  <!-- 需要动画的属性名称 -->
        type="scale"  <!-- 指定transform属性 -->
        begin="0s"    <!-- 开始时间,即延迟 -->
        dur="3s"      <!-- 动画时间 -->
        from="1"      <!-- 开始值 -->
        to="2"        <!-- 结束值 -->
        repeatCount="indefinite"   <!-- 重复方式,indefinite无限重复  -->
    />
</svg>

上例的動畫是A到B的過渡,要形成順暢的循環,至少要定義三個關鍵點。 animateTransform支援更多更靈活的屬性設定:

values:多個關鍵點的值,替代from和to,例如values="0;1;0" 

keyTimes:跟values對應,各點的時間點

calcMode:動畫快慢方式。 discrete | linear | paced | spline

keySplines:設定運動的貝塞爾控制點,calcMode為spline時有效

如何用SVG製作酷炫動態圖示?(程式碼實例)

####################################################################################################################################### #######circle畫圓,fill著色,用g標籤包裝並定位,transform設定初始形變,animateTransform設定動畫。現在來看程式碼,相信不會再有一頭霧水了:###
<svg class="lds-message" width="80px" height="80px" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 100" preserveAspectRatio="xMidYMid">
    <g transform="translate(20 50)">
        <circle cx="0" cy="0" r="7" fill="#e15b64" transform="scale(0.99275 0.99275)">
            <animateTransform attributeName="transform" type="scale" begin="-0.375s" calcMode="spline" keySplines="0.3 0 0.7 1;0.3 0 0.7 1" values="0;1;0" keyTimes="0;0.5;1" dur="1s" repeatCount="indefinite"></animateTransform>
        </circle>
    </g>
    <g transform="translate(40 50)">
        <circle cx="0" cy="0" r="7" fill="#f47e60" transform="scale(0.773605 0.773605)">
            <animateTransform attributeName="transform" type="scale" begin="-0.25s" calcMode="spline" keySplines="0.3 0 0.7 1;0.3 0 0.7 1" values="0;1;0" keyTimes="0;0.5;1" dur="1s" repeatCount="indefinite"></animateTransform>
        </circle>
    </g>
    <g transform="translate(60 50)">
        <circle cx="0" cy="0" r="7" fill="#f8b26a" transform="scale(0.42525 0.42525)">
            <animateTransform attributeName="transform" type="scale" begin="-0.125s" calcMode="spline" keySplines="0.3 0 0.7 1;0.3 0 0.7 1" values="0;1;0" keyTimes="0;0.5;1" dur="1s" repeatCount="indefinite"></animateTransform>
        </circle>
    </g>
    <g transform="translate(80 50)">
        <circle cx="0" cy="0" r="7" fill="#abbd81" transform="scale(0.113418 0.113418)">
            <animateTransform attributeName="transform" type="scale" begin="0s" calcMode="spline" keySplines="0.3 0 0.7 1;0.3 0 0.7 1" values="0;1;0" keyTimes="0;0.5;1" dur="1s" repeatCount="indefinite"></animateTransform>
        </circle>
    </g>
</svg>
###效果圖:###############也可以用js控制svg的屬性,控制svg的動畫過程,做成能回應點擊等事件的圖示按鈕。 ###


#

以上是如何用SVG製作酷炫動態圖示?(程式碼實例)的詳細內容。更多資訊請關注PHP中文網其他相關文章!

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