1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

SVGで画像を書くときに使用する基本的な図形の書き方とそれに付与して使用する主な図形のお話。

SVGの基本構文についてはこちら!

図形

<rect>

矩形を作るためのタグ。
位置と高さを指定することで四角形を作れる。

<rect x="矩形の左上頂点のx座標" y="矩形の左上頂点のy座標" width="横幅" height="縦幅">

rect.png

<svg viewBox="0 0 100 100" xmlns="http://www.w3.org/2000/svg">
	<rect x="20" y="25" width="60" height="50" fill="black" />
</svg>

rx,ry

任意で追加することで矩形の角を丸めることができる。

rect-rx-ry.png

<svg viewBox="0 0 140 70" xmlns="http://www.w3.org/2000/svg">
	<rect x="10" y="10" width="50" height="50" rx="10" ry="10" fill="black" />
	<rect x="80" y="10" width="50" height="50" rx="20" ry="10" fill="black" />
</svg>

矩形の中心から伸びる楕円系のx,y半径。
そのため、width=50な場合、有効な最大値は25となる。

適当に値増やしすぎると変なことになる。
ってかもはや円。

rect-rx-ry-max.png

<svg viewBox="0 0 200 70" xmlns="http://www.w3.org/2000/svg">
	<!-- rx, ryが最大値未満 -->
	<rect x="10" y="10" width="50" height="50" rx="10" ry="10" fill="black" />

	<!-- width=50, height=50に対する有効な最大値 -->
	<rect x="75" y="10" width="50" height="50" rx="25" ry="25" fill="black" />

	<!-- 最大値を超える値を指定してもそれ以上に描画は変わらない -->
	<rect x="140" y="10" width="50" height="50" rx="40" ry="40" fill="black" />
</svg>

<circle>

円を作れる。

<circle cx="円の中心のx座標" cy="円の中心のy座標" r="半径">

circle.png

<svg viewBox="0 0 100 100" xmlns="http://www.w3.org/2000/svg">
	<circle cx="50" cy="50" r="35" fill="black" />
</svg>

<ellipse>

読み方はえりぷす。
以下の構文で楕円が作れる。

<ellipse cx="楕円の中心のx座標" cy="楕円の中心のy座標" rx="楕円のx方向の半径" ry="楕円のy方向の半径">

ellipse.png

<svg viewBox="0 0 140 100" xmlns="http://www.w3.org/2000/svg">
	<ellipse cx="70" cy="50" rx="50" ry="30" fill="black" />
</svg>

<line>

以下の構文で直線を作れる。

<line x1="始点のx座標" x2="終点のx座標" y1="始点のy座標" y2="終点のy座標" stroke="任意の色">

line.png

<svg viewBox="0 0 100 100" xmlns="http://www.w3.org/2000/svg">
	<line x1="15" y1="20" x2="85" y2="80" stroke="black" stroke-width="2" />
</svg>

strokeは線に任意の色を指定するものである。(詳細は属性で)
これを指定しないとちゃんと線が表示されないので注意。
line-no-stroke.png

<svg viewBox="0 0 100 60" xmlns="http://www.w3.org/2000/svg">
    <!-- strokeにblaskを指定しているため表示される -->
    <line x1="15" y1="20" x2="85" y2="20" stroke="black" stroke-width="2" />
    <!-- strokeを指定していないため表示されない -->
    <line x1="15" y1="40" x2="85" y2="40" stroke-width="2" />
</svg>

<polyline>

接続された直線のグループである.

<polyline points="始点x座標, 始点y座標 次の点のx座標, 次の点のy座標 次の点のx座標, 次の点のy座標 次の点のx座標, 次の点のy座標 次の点のx座標, 次の点のy座標"/>

半角スペースで区切り,x,y x,y ...という感覚で直線をいくらでも繋げることができる。
書き方が紛らわしい。

polyline.png

<svg viewBox="0 0 120 100" xmlns="http://www.w3.org/2000/svg">
	<polyline fill="none" stroke="black" stroke-width="2" points="15,75 40,25 65,75 90,25 105,60" />
</svg>

後述するfillという図形の中身を塗る属性をnoneにしておかないと黒で塗られてしまうので注意。

polyline-default-fill.png

<svg viewBox="0 0 120 100" xmlns="http://www.w3.org/2000/svg">
    <polyline stroke="black" stroke-width="2"
        points="15,75 40,25 65,75 90,25 105,60" />
</svg>

<polygon>

多角形の描画に使用する。

<polygon points="始点x座標, 始点y座標 次の点のx座標, 次の点のy座標 次の点のx座標, 次の点のy座標 次の点のx座標, 次の点のy座標 次の点のx座標, 次の点のy座標"/>

ほとんど<polyline>と同じであるが、最後の点から必ず始点に対して伸び、きれいに合流する。

polygon.png

<svg viewBox="0 0 120 100" xmlns="http://www.w3.org/2000/svg">
    <polygon fill="none" stroke="black" stroke-width="2"
        points="15,75 40,25 65,75 90,25 105,60" />
</svg>

属性

fill

中身の色を指定する。
色の名前だったりカラーコードだったりで指定できる。

fill.png

<svg viewBox="0 0 200 70" xmlns="http://www.w3.org/2000/svg">
    <rect x="10" y="10" width="50" height="50" fill="red" />
    <rect x="75" y="10" width="50" height="50" fill="#4a90d9" />
    <rect x="140" y="10" width="50" height="50" fill="none" stroke="black" stroke-width="2" />
</svg>

stroke

外側の線の色を指定する。
指定できる色はfillと同じ。

stroke.png

<svg viewBox="0 0 200 70" xmlns="http://www.w3.org/2000/svg">
    <rect x="10" y="10" width="50" height="50" fill="none" stroke="red" stroke-width="3" />
    <rect x="75" y="10" width="50" height="50" fill="none" stroke="#4a90d9" stroke-width="3" />
    <rect x="140" y="10" width="50" height="50" fill="lightgray" stroke="none" />
</svg>

fill-opacity,stroke-opacity

それぞれ透明度を変更する。
各プロパティが文字で指定されていたときとかに使用する。

fill-opacity.png

<svg viewBox="0 0 200 80" xmlns="http://www.w3.org/2000/svg">
    <line x1="10" y1="40" x2="190" y2="40" stroke="black" stroke-width="4" />

    <circle cx="40" cy="40" r="24" fill="blue" fill-opacity="1" />
    <circle cx="100" cy="40" r="24" fill="blue" fill-opacity="0.6" />
    <circle cx="160" cy="40" r="24" fill="blue" fill-opacity="0.2" />
</svg>

stroke-opacity.png

<svg viewBox="0 0 200 80" xmlns="http://www.w3.org/2000/svg">
    <line x1="10" y1="40" x2="190" y2="40" stroke="black" stroke-width="4" />

    <circle cx="40" cy="40" r="22" fill="none" stroke="red" stroke-width="8" stroke-opacity="1" />
    <circle cx="100" cy="40" r="22" fill="none" stroke="red" stroke-width="8" stroke-opacity="0.6" />
    <circle cx="160" cy="40" r="22" fill="none" stroke="red" stroke-width="8" stroke-opacity="0.2" />
</svg>

stroke-width

strokeの太さを指定する。
デフォルトでは座標を中心に0.5づつで1という計算になっている。

stroke-width.png

<svg viewBox="0 0 120 90" xmlns="http://www.w3.org/2000/svg">
    <line x1="15" y1="20" x2="105" y2="20" stroke="black" stroke-width="1" />
    <line x1="15" y1="45" x2="105" y2="45" stroke="black" stroke-width="4" />
    <line x1="15" y1="70" x2="105" y2="70" stroke="black" stroke-width="10" />
</svg>

線の中心を基準に広がるのでviewboxの境界寸前だと意図せず見切れてしまう。

stroke-width-center.png

<svg viewBox="0 0 120 110" xmlns="http://www.w3.org/2000/svg">
    <line x1="20" y1="25" x2="100" y2="25" stroke="black" stroke-width="20" stroke-opacity="0.3" />
    <line x1="20" y1="25" x2="100" y2="25" stroke="red" stroke-width="0.5" />

    <rect x="25" y="60" width="70" height="30" fill="none" stroke="black" stroke-width="12" stroke-opacity="0.3" />
    <rect x="25" y="60" width="70" height="30" fill="none" stroke="red" stroke-width="0.5" />
</svg>

stroke-linecap

strokeの引かれ方を決める。
3つの値がある。

butt

線に対してそのまま肉付けするイメージ。

square

線の端でその太さと同じ値でエリアをとる。

round

squareの角が丸まったバージョン。

stroke-linecap.png

<svg viewBox="0 0 140 120" xmlns="http://www.w3.org/2000/svg">
    <g stroke="black" stroke-width="16">
        <line x1="30" y1="25" x2="110" y2="25" stroke-linecap="butt" />
        <line x1="30" y1="60" x2="110" y2="60" stroke-linecap="square" />
        <line x1="30" y1="95" x2="110" y2="95" stroke-linecap="round" />
    </g>

    <!-- 始点・終点の座標位置 -->
    <g stroke="red" stroke-width="0.5">
        <line x1="30" y1="10" x2="30" y2="110" />
        <line x1="110" y1="10" x2="110" y2="110" />
    </g>
</svg>

stroke-linejoin

strokeの接続部分に対するプロパティとして使用する。

miter

普通の線と同様な接続部分。

round

丸みを帯びた接続部分を作成する。

bevel

2つの線分をわたる角を作成する。

<svg viewBox="0 0 160 240" xmlns="http://www.w3.org/2000/svg">
    <g fill="none" stroke="black" stroke-width="18">
        <polyline points="40,65 80,25 120,65" stroke-linejoin="miter" />
        <polyline points="40,140 80,100 120,140" stroke-linejoin="round" />
        <polyline points="40,215 80,175 120,215" stroke-linejoin="bevel" />
    </g>
</svg>

おまけ

SVGにはなぜか画像を表示するためのタグも存在している。

<image x="左上頂点のx座標" y="左上頂点のy座標" width="画像の横幅" height="画像の縦幅" href="画像のパスorURL">

ちなみに、web上の画像を埋め込もうとすると表示媒体によっては弾かれることがある。
(今回の例の画像は事前に.svgを.pngに変換したものなので表示される。)

image.png

<svg viewBox="0 0 100 100" xmlns="http://www.w3.org/2000/svg">
    <image href="https://avatars.githubusercontent.com/u/226723658?v=4" x="10" y="10" width="80" height="80" />
</svg>

正直全部pathでいいだろって気もしなくはないけど

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?