H5中canvas和svg繪圖方式介紹


HTML5中包括了兩種繪圖方式,canvas和svg(矢量呈現),而與canvas不同的是,svg是一種XML標記語言,它既可以單獨保存以“.svg”為后綴的文件在瀏覽器中打開顯示,也支持建立svg標簽直接嵌入在網頁中顯示,還可以通過<embed src="文件.svg" name="name自命" type="image/svg+xml" height="300" width="450">將SVG嵌入到頁面中,和Canvas一樣也可以響應腳本事件操作和控制。

svg的形狀shapes:rect/矩形(方形) 、circle/圓形、ellipse/橢圓、line/線、polyline/多邊交叉線和polygone/多邊形可以使用,當然還有image;

其中,(1)在rect中有如下屬性:x/y(矩形左上角坐標),width/height(矩形寬高),rx/ry(矩形的角弧度;最大值為寬高的一半,而且當矩形為正方形切最大值時圖形為圓形),fill(圖形填充色),stroke/stroke-width(圖形邊框/邊框寬度;默認為1px);(2)在circle中則有如下屬性:cx/cy(圓形圓點坐標)、r(圓形半徑)、fill、stroke/stroke-width;(3)在ellipse中則有如下屬性:cx/cy、rx/ry(橢圓X軸/Y軸的半徑)、fill、stroke/stroke-width;(4)在line中則有如下屬性:stroke/stroke-width(線的顏色/寬度)、x1/y1(直線起點坐標)、x2/y2(直線終點坐標);(5)在polyline中則有如下屬性:stroke/stroke-width(線的顏色/寬度)、points(x1,y1 x2,y2 x3,y3)(起點終點以及各個轉折點的坐標);(6)在polygon中有如下屬性stroke/stroke-width(線的顏色/寬度)、points(x1,y1 x2,y2 x3,y3)(起點終點以及各個轉折角的坐標);

代碼如下:

<svg width="3000" height="2500" xmlns="http://www.w3.org/2000/svg">

    <rect x="5" y="50" width="60" height="60" fill="green"/>

    <rect x="70" y="5" rx="30" ry="30" stroke="blue" width="60" height="60" fill="red"/>

    <circle  cx="165" cy="35" r="30" fill="orange" stroke="blue"/>

    <ellipse cx="230" cy="35" rx="30" ry="20" fill="blue"/>

    <line  y1="222" x1="25" y2="130" x2="100" stroke-width="5" stroke="#ff0000" fill="none"/>

<polygon points="290,100 350,210 270,250" style="fill:#cccccc;stroke:#000000;stroke-width:1"/>

<polyline stroke-width="6" fill="none" stroke="#3f7f00" points="121,193 192,284 121,284 192,193 "/>

    <image xlink:href="http://4493bz.1985t.com/uploads/allimg/150127/4-15012G52133.jpg"  x="360" y="30" width="100" height="100" />

</svg>

然后,svg在圖形上還有一種按照路徑來畫圖形的標簽path,其實path標簽是用來定義路徑的,標簽里有個d="";的屬性,里面的值為路徑坐標;以下命令可用於路徑數據:M = moveto,L = lineto,H = horizontal lineto,V = vertical lineto,C = curveto,S = smooth curveto,Q = quadratic Belzier curve,T = smooth quadratic Belzier curveto,A = elliptical Arc,Z = closepath;(這些命令均允許大小寫,大寫表示絕對定位,小寫表示相對定位);例如:

 

    <svg xmlns='http://www.w3.org/1999/svg' version=''>

            <path d="m0,0,10,20,30,20,50,50,200,10z" stroke-width="5" stroke="#000000" fill="#ff7f00" />

            <path d="m263,149 l-9,76 l82,-48 l-123,-13 l50,-15 z" stroke-width="5" stroke="#000000" fill="#ff7f00"/>

    </svg>

 

另外,svg對於文字也有不同的效果體現,svg的text標簽支持漸變填充,也支持文字拾取曲線排列文字,需要預先定義漸變填充在<defs></defs>中,當文字拾取路徑排列是也需要在<defs></defs>中預先定義。而text也有自己的一些屬性:x/y(X軸/Y軸起始點坐標)、dx/dy(X軸/Y軸偏移位置的坐標)、rotate(旋轉偏移的角度)、textLength(元素的長度,會影響輸出的寬度)、transform(文字的變形效果,類似css中的transform);代碼如下:

    <svg width="300" height="250" xmlns="http://www.w3.org/2000/svg">

        <defs>

            <linearGradient y2="1" x2="1" y1="0" x1="0" id="svg_8">

                <stop stop-color="#ff0000" offset="0"/>

                <stop stop-color="#ffff00" offset="1"/>

            </linearGradient>

        </defs>

        <text font-weight="bold" font-style="normal" xml:space="preserve"

    text-anchor="middle" font-family="Arial Black" font-size="24"

    id="svg_7" y="53" x="107" stroke-width="0" stroke="#000000"

    fill="url(#svg_8)">weisim3.com</text>

 

        <defs>

            <path  d="m220.98885,39.69102c84.91158,149.91641 241.80937,-74.29949 260.99889,5.65374"

        fill="black" transform="rotate(-0.474251, 351.488, 60.1133)"

        id="fontPath"/>

        </defs>

        <text fill="url(#svg_8)">

            <textPath xlink:href="#fontPath">

                www.weisim3.com 22.11.2011 - Html5 SVG

            </textPath>

        </text>

    </svg>

 

svg可以直接支持動畫和鼠標觸發事件,也支持JavaScript腳本響應時間,接下來會為大家介紹一下svg的鼠標事件和動畫;click事件<set attributeName="fill" to="red" begin="click"/>,在SVG中觸發click事件,attributeName是類型這里fill是指填充,to="red"是點擊之后填充為紅色,attributeName支持visibility、opacity、width等屬性。動畫<animateMotion from="0,260" to="180,100" dur="5s" fill="freeze"/>,這里動畫from="0,260"為平面坐標點,x軸為0,y軸為260;to="180,100"則是動畫移動到"180,100"位置;dur="5s"為幀速。動畫<animateTransform>為自由變換如圖形旋轉、縮放主要用此屬性;代碼如下:

    <svg width="300" height="250" xmlns="http://www.w3.org/2000/svg">

        <circle  cx="60" cy="35" r="30" fill="green">

            <set attributeName="stroke" to="red" begin="click"/>

        </circle>

 

        <rect x="100" y="10" width="40" height="50" fill="orange">

         <animate attributeName="width" dur="5s" from="10px" to="20px"

          accumulate="sum" additive="sum" repeatCount="10" begin="click" />

        </rect>

 

        <rect fill="orange" stroke="red" stroke-width="5px"

             x="0px" y="0px" width="115px" height="115px">

            <animateMotion from="0,260" to="180,100"

             dur="5s" fill="freeze"/>

            </rect>

    </svg>

 

    <svg width="300" height="300" xmlns="http://www.w3.org/2000/svg" >

        <rect fill="#AFBCC7" stroke="blue" stroke-width="5px" x="200px"

        y="100px" width="60px" height="60px">

        <animateTransform

            attributeType="XML"

            attributeName="transform"

            type="rotate"

            from="0,150,150" to="360,150,150"

            begin="0s" dur="5s"

            repeatCount="indefinite"/>

        </rect>

 

         <circle fill="#c5ff86" stroke="#38521d" stroke-width="5px"

          cy="37" cx="148" r="30" >

 

            <animateTransform

                attributeType="XML"

                attributeName="transform"

                type="scale"

                from="0" to="1"

                dur="5s"

                fill="freeze"/>

        </circle>

    </svg>


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM