基本図形
rect / circle / ellipse / line / polyline / polygon。座標と属性で図形を組み立てる基礎。
rect(長方形)
<rect x="10" y="10" width="80" height="40" />
- x / y — 左上の座標
- width / height — サイズ
- rx / ry — 角丸の半径
角丸
<rect x="0" y="0" width="100" height="60" rx="10" ry="10" fill="orange" />
<!-- 完全な丸(楕円相当) rx=width/2 -->
<rect x="0" y="0" width="100" height="60" rx="50" />
circle(円)
<circle cx="50" cy="50" r="30" fill="dodgerblue" />
- cx / cy — 中心座標
- r — 半径
ellipse(楕円)
<ellipse cx="50" cy="50" rx="40" ry="20" fill="green" />
- rx — 横半径
- ry — 縦半径
line(直線)
<line x1="10" y1="10" x2="90" y2="90" stroke="black" stroke-width="2" />
線にはstroke が必須(無いと見えない)。
polyline(折れ線)
<polyline
points="0,40 20,10 40,30 60,5 80,25 100,20"
fill="none"
stroke="purple"
stroke-width="2"
/>
- points は
x1,y1 x2,y2 ...でもx1 y1 x2 y2 ...でも OK - 閉じない(始点と終点を結ばない)
- fill="none" を忘れると塗られて変な形に
polygon(多角形)
<polygon
points="50,5 90,75 10,75"
fill="tomato"
/>
最後の点と最初の点が自動で結ばれる。
正多角形 / 星形
// 5 角形
function regularPolygon(cx, cy, r, n) {
const points = []
for (let i = 0; i < n; i++) {
const a = (i / n) * Math.PI * 2 - Math.PI / 2
points.push(`${cx + r * Math.cos(a)},${cy + r * Math.sin(a)}`)
}
return points.join(" ")
}
// 星形(5 ポイント)
function star(cx, cy, rOut, rIn) {
const points = []
for (let i = 0; i < 10; i++) {
const a = (i / 10) * Math.PI * 2 - Math.PI / 2
const r = i % 2 === 0 ? rOut : rIn
points.push(`${cx + r * Math.cos(a)},${cy + r * Math.sin(a)}`)
}
return points.join(" ")
}
共通属性
| 属性 | 意味 |
|---|---|
| fill | 塗り色 |
| fill-opacity | 塗りの透明度(0〜1) |
| stroke | 線の色 |
| stroke-width | 線の太さ |
| stroke-opacity | 線の透明度 |
| opacity | 全体の透明度 |
| stroke-linecap | 線端: butt / round / square |
| stroke-linejoin | 結合: miter / round / bevel |
| stroke-dasharray | 破線パターン: 5 5 |
| stroke-dashoffset | 破線の開始位置 |
| vector-effect | "non-scaling-stroke" で線が拡大しない |
線スタイルの実例
<line x1="10" y1="20" x2="90" y2="20"
stroke="black" stroke-width="4" stroke-linecap="round" />
<line x1="10" y1="50" x2="90" y2="50"
stroke="black" stroke-width="2" stroke-dasharray="6 3" />
<line x1="10" y1="80" x2="90" y2="80"
stroke="black" stroke-width="2" stroke-dasharray="2 2 6 2" />
fill と stroke の指定方法
- 色名:
red/tomato - 16 進:
#ff5500/#f50 - rgb:
rgb(255 0 0)/rgba(...) - HSL / OKLCH
- none: 透明
currentColor: CSS の color を継承- グラデーション / パターンへの参照:
url(#myGradient)
実用例集
1. 円グラフの 1 セクター(path も使う)
<circle cx="50" cy="50" r="40" fill="lightgray" />
<path d="M50,50 L50,10 A40,40 0 0,1 88.6,40 z" fill="orange" />
2. シンプルな棒グラフ
<svg viewBox="0 0 100 50">
<rect x="0" y="20" width="10" height="30" fill="orange" />
<rect x="15" y="10" width="10" height="40" fill="orange" />
<rect x="30" y="30" width="10" height="20" fill="orange" />
<rect x="45" y="5" width="10" height="45" fill="orange" />
</svg>
3. 折れ線グラフ
<svg viewBox="0 0 100 50">
<polyline
points="0,40 20,10 40,30 60,5 80,25 100,20"
fill="none" stroke="dodgerblue" stroke-width="2"
/>
<circle cx="0" cy="40" r="2" fill="dodgerblue" />
<circle cx="20" cy="10" r="2" fill="dodgerblue" />
...(各点)
</svg>
4. グリッド
<defs>
<pattern id="grid" width="10" height="10" patternUnits="userSpaceOnUse">
<path d="M 10 0 L 0 0 0 10" fill="none" stroke="#eee" stroke-width="0.5" />
</pattern>
</defs>
<rect width="100%" height="100%" fill="url(#grid)" />
5. 進捗リング
<svg viewBox="0 0 100 100" width="100">
<circle cx="50" cy="50" r="45" fill="none" stroke="#eee" stroke-width="8" />
<circle cx="50" cy="50" r="45" fill="none" stroke="orange" stroke-width="8"
stroke-dasharray="282.7" stroke-dashoffset="70.7"
transform="rotate(-90 50 50)" stroke-linecap="round" />
<text x="50" y="55" text-anchor="middle" font-size="20">75%</text>
</svg>
stroke-dasharray = 円周(2πr ≒ 282.7)、dashoffset で見える長さを調整。
図形の重ね順
SVG は後に書いた要素が上に表示される(CSS の z-index は SVG 内では一部のみ)。
透明度
- fill: rgba(...) や rgb(... / 0.5)
- fill-opacity / stroke-opacity(要素内のみ)
- opacity(要素全体)
visibility と display
visibility="hidden"— 場所は確保、見えないdisplay="none"— 完全に非表示
イベント
各図形は HTML 同様にイベントを受け取る:
<circle cx="50" cy="50" r="20" fill="orange"
onclick="alert('クリック')"
style="cursor: pointer"
/>
pointer-events
none— クリックを通す(オーバレイ等)visiblePainted— 既定(fill/stroke がある領域)all— 透明部分でも反応
失敗パターン
| 症状 | 対処 |
|---|---|
| 線が見えない | stroke 必須、fill="none" で外側塗らないように |
| polyline が塗られて変な形 | fill="none" |
| 大きさが合わない | viewBox 確認 |
| 線が拡大すると太くなる | vector-effect="non-scaling-stroke" |
| クリックできる範囲が狭い | 透明 rect を上に重ねて pointer-events |
慣れてきたら
基本図形は「どれも path で書ける」。実際のデザインツールは複雑な形をすべて path に変換する。 ただし編集のしやすさのため、コードでは rect / circle を選ぶのが読みやすい。