HTML中使用SVG與SVG預(yù)定義形狀元素介紹

字號(hào):


    如果svg代碼在html中,我們就更容易寫javascript來(lái)控制圖形的變換,下面為大家介紹一些svg的預(yù)定義的形狀元素,感興趣的朋友可以參考下哈,希望對(duì)大家有所幫助
    SVG 文件可通過(guò)以下標(biāo)簽嵌入 HTML 文檔:<embed>、<object>或者<iframe>。
    代碼如下:
    <embed src="rect.svg" width="300"height="100"
    type="image/svg+xml"
    pluginspage="http://www.adobe.com/svg/viewer/install/"/>
    pluginspage 屬性指向下載插件的 URL。
    代碼如下:
    <object data="rect.svg"width="300" height="100"
    type="image/svg+xml"
    codebase="http://www.adobe.com/svg/viewer/install/"/>
    <iframe src="rect.svg" width="300"height="100">
    </iframe>
    在這三個(gè)中<iframe>是比較早期的標(biāo)簽,現(xiàn)在用的比較的少了。用的較多的還是<embed>標(biāo)簽。
    同時(shí)我們也可以將svg直接寫入HTML文件中:
    這樣的話需要先引入SVG的dtd文件:
    代碼如下:
    <?xml version="1.0" standalone="no"?>
    <!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN"
    "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
    再在<svg>標(biāo)簽中填入需要的代碼:
    代碼如下:
    <svg height="100%" version="1.1"
    xmlns="http://www.w3.org/2000/svg" onclick="ccc();">
    <ellipse cx="240" cy="100" rx="220" ry="30"
    style="fill:yellow"/>
    <ellipse cx="220" cy="100" rx="190" ry="20" id="w1"
    style="fill:white"/>
    </svg>
    如果svg代碼在html中,我們就更容易寫javascript來(lái)控制圖形的變換:
    代碼如下:
    <script type="text/javascript">
    function ccc(){
    var a = document.getElementById("w1");
    a.style.fill="red";
    a.setAttribute("cx", "150"); //設(shè)置值
    a.setAttribute("ry", "50"); //設(shè)置值
    }
    </script>
    下面介紹一些svg的預(yù)定義的形狀元素:
    矩形 <rect>
    圓形 <circle>
    橢圓 <ellipse>
    線 <line>
    折線 <polyline>
    多邊形 <polygon>
    路徑 <path>