天天看點

學習SVG

SVG: Scalable Vector Graphics(可伸縮矢量圖形)

它使用XML格式來定義圖形,例如:

<svg>

<rect width="100" height="100" x="0" y="0"

style="fill:red;  stroke:black; stroke-width:5">

</svg>

學習SVG

優點:

1、SVG圖檔它在放大或縮小時不會像圖檔一樣失真;

2、SVG圖檔可以被搜尋、索引、加載腳步和壓縮;

3、SVG圖檔它可擴充;

4、SVG是開放的标準;

5、SVG是純XML檔案。

DEMO:

————————————————————————————————————————————————

1、rectangle矩形

<svg>

<rect x="50" y="20" rx="50" ry="50" width="200" height="200"

  style="fill:red;stroke:black;stroke-width:5;opacity:0.5" />

</svg>

學習SVG

————————————————————————————————————————————————

2、Circle圓形

<svg>

<circle cx="100" cy="50" r="40" fill="red" stroke="black" stroke-width="20"

fill-opacity="0.8" stroke-opacity="0.5" />

</svg>

學習SVG

————————————————————————————————————————————————

3、Ellipse橢圓

<svg>

<ellipse cx="300" cy="80" rx="100" ry="50" style="fill:yellow; stroke:purple; stroke-width:2" />

</svg>

學習SVG

————————————————————————————————————————————————

4、Line線

<svg>

<line x1="0" y1="0" x2="200" y2="50" stroke-width="5"  stroke="black" />

</svg>

學習SVG

————————————————————————————————————————————————

5、Polygon多邊形

<svg>

<polygon points="100,10 40,180 190,60 10,60 160,180" style="fill:lime;stroke:purple;stroke-width:5;fill-rule:evenodd"/>

</svg>

學習SVG

————————————————————————————————————————————————

6、Polyline折線

<svg>

<polyline points="0,40 40,40 40,80 80,80 80,120 120,120 120,160"

style="fill:yellow;stroke:red;stroke-width:4"/>

</svg>

學習SVG

————————————————————————————————————————————————

7、Path路勁

<svg>

<path id="lineAB" d="M 100 350 l 150 -300" stroke="red" stroke-width="3" fill="none" />

  <path id="lineBC" d="M 250 50 l 150 300" stroke="red" stroke-width="3" fill="none" />

  <path id="lineBC" d="M 175 200 l 150 0" stroke="red" stroke-width="3" fill="none" />

  <path id="quadcurveABC" d="M 100 350 q 150 -300 300 0" stroke="blue" stroke-width="5" fill="none" />

  <!-- Mark relevant points -->

  <g stroke="black" stroke-width="3" fill="black">

    <circle id="pointA" cx="100" cy="350" r="3" />

    <circle id="pointB" cx="250" cy="50" r="3" />

    <circle id="pointC" cx="400" cy="350" r="3" />

  </g>

  <!-- Label the points -->

  <g font-size="30" font="sans-serif" fill="black" stroke="none" text-anchor="middle">

    <text x="100" y="350" dx="-30">A</text>

    <text x="250" y="50" dy="-10">B</text>

    <text x="400" y="350" dx="30">C</text>

  </g>

</svg>

學習SVG

————————————————————————————————————————————————

8、Text文字

<svg>

<text x="0" y="15" fill="red" transform="rotate(30 20,40)">I Love SVG</text>

</svg>

學習SVG

————————————————————————————————————————————————

9、Stroke描邊

<svg>

<g fill="none" stroke="black" stroke-width="4">

    <path stroke-dasharray="5,5" d="M5 20 l215 0" />

    <path stroke-dasharray="10,10" d="M5 40 l215 0" />

    <path stroke-dasharray="20,10,5,5,5,10" d="M5 60 l215 0" />

  </g>

</svg>

學習SVG

————————————————————————————————————————————————

————————————————————————————————————————————————