DIV CSS 佈局教程網

您现在的位置: DIV+CSS佈局教程網 >> 網頁腳本 >  >> HTML基礎知識 >> HTML5詳解
活用 HTML5 新特性:用 SVG 手繪微信 Logo
編輯:HTML5詳解     

HTML5 帶來了不少有趣的新能力,以下幾項最為常用:

  • 用於繪圖的 canvas 元素
  • 用於媒體播放的 video 與 audio 元素
  • 對本機離線儲存的更佳支援
  • article、footer、header、nav、section 等語義化內容元素
  • calendar、date、time、email、url、search 等新表單控制項

除了 Canvas,SVG 也是 HTML5 生態中相當強大的向量繪圖方案。SVG 以 XML 描述圖形,最大的優點是無論放大到多倍都不會失真。下面就用 SVG 來手繪一個微信風格的雙對話泡泡 Logo。

成品預覽

HTML 部分

先用一個容器包住 SVG,並透過 object 標籤把外部的 logo.svg 嵌進來。這段是 HTML 文件的內容:

<style>
* { padding: 0; margin: 0; }
body { background-color: #ddd; }
.container {
    width: 300px;
    height: 300px;
    position: relative;
    top: 50px;
    left: 50%;
    transform: translate(-50%, 0);
    background-color: #c4e9c8;
    border-radius: 30px;
    box-shadow: 5px 5px #cbfbf;
}
.container object {
    position: relative;
    top: 30px;
    left: 30px;
}
</style>
<body>
<div class="container">
    <object data="logo.svg" width="240" height="240" type="image/svg+xml" />
</div>
</body>

SVG 部分

這個 logo.svg 是獨立於 HTML 之外的 XML 檔,用橢圓、圓、多邊形等基本圖形拼出兩個對話泡泡與裡面的兩個小圓點:

<?xml version="1.0" standalone="no"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN"
"https://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
<svg width="100%" height="100%" version="1.1" xmlns="https://www.w3.org/2000/svg">
  <ellipse cx="120" cy="90" rx="90" ry="70"
    style="fill:rgb(149,222,150);stroke:rgb(149,222,150);stroke-width:2;"/>
  <circle cx="90" cy="85" r="8" style="fill:#c4e9c8;stroke:#c4e9c8;stroke-width:1;"/>
  <circle cx="130" cy="85" r="8" style="fill:#c4e9c8;stroke:#c4e9c8;stroke-width:1;"/>
  <polygon points="90,150 110,150 100,170" style="fill:#c4e9c8;stroke:#c4e9c8;stroke-width:1;"/>
  <ellipse cx="170" cy="170" rx="70" ry="55"
    style="fill:#c4e9c8;stroke:#c4e9c8;stroke-width:2;"/>
  <ellipse cx="170" cy="170" rx="70" ry="55"
    style="fill:rgb(149,222,150);stroke:rgb(149,222,150);stroke-width:2;"/>
  <circle cx="145" cy="165" r="7" style="fill:#c4e9c8;stroke:#c4e9c8;stroke-width:1;"/>
  <circle cx="180" cy="165" r="7" style="fill:#c4e9c8;stroke:#c4e9c8;stroke-width:1;"/>
  <polygon points="150,215 185,215 168,235" style="fill:#fff;stroke:#fff;stroke-width:1;"/>
</svg>

把這兩個檔案放在同一個目錄下,用瀏覽器打開 HTML,就能看到用純向量繪製、可任意縮放都不失真的微信風格 Logo 了。這個範例也示範了 HTML5 時代「結構(HTML)、樣式(CSS)、圖形(SVG)」各司其職、彼此分離的良好實務。

XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved