Styling & Animating SVG

Styling & Animating SVG

@(SVG 專用)[SVG]php

Alt text


前言

什麼是SVG?

可縮放矢量圖形(英語:Scalable Vector Graphics,SVG)是基於可擴展標記語言(XML),用於描述二維矢量圖形的一種圖形格式。SVG由W3C制定,是一個開放標準。
Scalable Vector Graphics (SVG) is an XML-based vector image format for two-dimensional graphics with support for interactivity and animation.css

SVG應用場景

  1. icon
  2. 數據可視化
  3. 在移動端flash廣告的代替者
  4. 專題故事版

SVG 實例


目錄

  1. 設計師篇
  2. CSSer
  3. JSer

Why SVGs?

  • accessible
  • Very Good Browser Support
  • Smaller sizes (can be gzipped)
  • Scalable & Resolution Independent 不受約束的等比縮放
  • Interactive and Styleable (CSS and Javascript)

設計師篇

矢量圖形前景

  • 扁平化趨勢
  • 顯示技術的革新
  • 效率優先
  • 響應式設計
  • 豐富的動畫及互動體驗
  • 應用環境和技術的改善

工具

  • Adobe Illustratorhtml

    Alt text

  • Inkscape (Free)git

    Alt text

  • Sketch (Mac OS X only)github

    Alt text


CSSer篇

引入SVG

  • <img src="image.svg" alt="Site Logo" />
  • <object type="image/svg+xml" data="image.svg">
    <img src=」fallback.jpg」><!-- fallback -->
    </object>
  • <embed type="image/svg+xml" src="image.svg" />
  • <iframe src="image.svg">
    </iframe>
  • <svg> … </svg>
  • .el { background-image: url(image.svg); }

SVG樣式

SVG Presentation Attributes

<svg xmlns="http://www.w3.org/2000/svg" version="1.1" width="300px" height="300px" viewBox="0 0 300 300">
  <polygon 
     fill = "#FF931E" 
     stroke = "#ED1C24" 
     stroke-width = "5" 
     points = "279.1,160.8 195.2,193.3 174.4,280.8   117.6,211.1 27.9,218.3 76.7,142.7 42.1,59.6 129.1,82.7 197.4,24.1 202.3,114 "/>
</svg>

http://codepen.io/Yunkou/full/tucEBide

SVG Attributes

SVG Presentation Attributes
詳情參見:svg

行內樣式 (Inline Styles)

<svg xmlns="http://www.w3.org/2000/svg" version="1.1" style="width: 300px; height: 300px;"  viewBox="0 0 300 300">
    <polygon 
       style = "fill: #FF931E; stroke: #ED1C24; stroke-width: 5;"
       points = "279.1,160.8 195.2,193.3 174.4,280.8   117.6,211.1 27.9,218.3 76.7,142.7 42.1,59.6 129.1,82.7 197.4,24.1 202.3,114 "/>
</svg>

http://codepen.io/Yunkou/full/tucEB工具

Embedded Styles (<style>) Inside svg

<svg xmlns="http://www.w3.org/2000/svg" version="1.1" width="300px" height="300px" viewBox="0 0 300 300">
    <style type=」text/css」>
        <![CDATA[
            selector {/* styles */}
        ]]>
    </style>
    <g id=」..」> … </g>
</svg>

Embedded Styles (<style>) Outside svg

<!DOCTYPE html><!-- HTML5 document -->
    <html> <head> ... </head> <body>
       <style type=」text/css」>
           /* style rules */
       </style>
<!-- xmlns is optional in an HTML5 document -->
       <svg version="1.1" viewBox="0 0 300 300">
<!-- SVG content -->    
       </svg>
    </body></html>

Style Rules (Selectors)

  1. Type Selectors:
    g {/* style a group */}
    circle, rect { fill: #009966; }post

  2. Class and ID Selectors:
    .arrow-head { /* styles / }
    #bird { /
    styles */ }動畫

  3. Dynamic Pseudo-Class Selectors:
    .icon:hover, .icon:active, .icon:focus { /* styles */ }

  4. Pseudo-Class Selectors:
    :first-child, :last-child, :nth-*-child(), :visited, :link and :lang, :last-of-type, :first-of-type, :not(), :nth-*-type(), :only-child, :only-of-type, ...

  5. Children Selectors:
    g > circle {/* styles */}

note:僞類元素 before after 不支持

Jser

svg庫

  1. snap.js (ADOBE 支持)
  2. SVG.js

範例

  1. 小燈泡 http://codepen.io/Yunkou/full/HLhin

參考文獻

  1. 維基百科《可縮放矢量圖形

  2. Sara's blog《Styling & Animating Scalable Vector Graphics with CSS

  3. ux.etao《淺談矢量圖形前景

  4. MDN 《svg

  5. 張鑫旭 《snap中API文文檔

  6. jenkov's Blog《SVG Tutorial

相關文章
相關標籤/搜索