SVG绘图

前言

图形

SVG

1
2
3
4
5
6
7
8
9
10
<svg id="test_1"
xmlns="http://www.w3.org/2000/svg"
xml:space="preserve"
width="600"
height="400"
viewBox="0 0 300 200">
<g style="fill:#d2fae3;stroke:#82eeb5;stroke-width:1">
<rect x="10" y="10" width="100" height="40" rx="20"/>
</g>
</svg>

SVG中

  • width/height 是图形的宽/高
  • viewBox="0 0 300 200" 是画布的属性

如上面的示例 画布尺寸比图形小,那么我们看到的画布中的图形就会同比例放大。

圆形

1
2
3
4
5
6
<circle id="_x31_" 
opacity="1"
fill="#d2fae3"
cx="332.081" cy="149.346" r="73"
stroke-width="1" stroke="#82eeb5"
/>

矩形

1
2
3
4
5
6
7
<rect id="_x31_"
opacity="1"
width="100" height="100"
x="100" y="100"
fill="#d2fae3"
stroke-width="1" stroke="#82eeb5"
/>

菱形

1
2
<polygon points="0,50 50,0 100,50 50,100"
style="fill:#d2fae3;stroke:#82eeb5;stroke-width:1"/>

文字

1
2
<text x="0" y="15" font-size="18"
style="fill:#d2fae3;stroke:#82eeb5;stroke-width:1">Hello World</text>

不带边框

1
2
<text x="0" y="15" font-size="18"
style="fill:#333333;">Hello World</text>

圆角矩形

1
2
<rect x="10" y="10" width="100" height="40" rx="4"
style="fill:#d2fae3;stroke:#82eeb5;stroke-width:1"/>

两侧为圆形的矩形

1
2
<rect x="10" y="10" width="100" height="40" rx="20"
style="fill:#d2fae3;stroke:#82eeb5;stroke-width:1"/>

Path

这里是画了个心形

1
2
3
4
<g style="fill:#d2fae3;stroke:#82eeb5;stroke-width:1"
transform="translate(100 0) scale(1 1)">
<path id="heart" d="M 10,30 A 20,20 0,0,1 50,30 A 20,20 0,0,1 90,30 Q 90,60 50,90 Q 10,60 10,30 z"/>
</g>

相同样式的图形或者需要一起添加事件可以用g来分组

g不能设置宽高等位置属性,只能设置内部元素的样式及添加响应事件。

内部元素的定位也是相对于svg的。

1
2
3
<g style="fill:#d2fae3;stroke:#82eeb5;stroke-width:1">
<rect x="10" y="10" width="100" height="40" rx="20"/>
</g>

比如

1
2
3
4
5
<g stroke="#82eeb5" fill="#d2fae3" stroke-width="1">
<circle cx="25" cy="25" r="15"/>
<circle cx="40" cy="25" r="15"/>
<circle cx="55" cy="25" r="15"/>
</g>

transform

1
2
3
4
5
6
7
8
9
<svg xmlns="http://www.w3.org/2000/svg"
xml:space="preserve"
width="600"
height="400"
viewBox="-1 -1 600 400">
<rect x="50" y="20" width="100" height="40" rx="4"
transform="translate(-50 -20)"
style="fill:#d2fae3;stroke:#82eeb5;stroke-width:1"/>
</svg>

事件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>流程图-SVG版</title>
</head>
<body>

<svg xmlns="http://www.w3.org/2000/svg"
id="m_svg"
xml:space="preserve"
width="800"
height="600"
viewBox="-1 -1 800 600">
<rect x="50" y="20" width="100" height="40" rx="4"
transform="translate(-50 -20)"
style="fill:#d2fae3;stroke:#82eeb5;stroke-width:1"
onmousedown="selectElement(event)"/>


<rect x="100" y="120" width="100" height="40" rx="4"
transform="translate(-50 -20)"
style="fill:#d2fae3;stroke:#82eeb5;stroke-width:1"
onmousedown="selectElement(event)"/>

</svg>

<script type="text/javascript">
var currentX = 0;
var currentY = 0;
selectedElement = null;
let currentMatrix = {
x: 0,
y: 0
};

var m_svg = document.getElementById("m_svg");
m_svg.setAttributeNS(null, "onmousemove", "moveElement(evt)");
m_svg.setAttributeNS(null, "onmouseup", "deselectElement(evt)");

function selectElement(evt) {
selectedElement = evt.target;
currentX = evt.clientX;
currentY = evt.clientY;
currentMatrix.x = parseInt(selectedElement.getAttributeNS(null, "x"));
currentMatrix.y = parseInt(selectedElement.getAttributeNS(null, "y"));
}

function moveElement(evt) {
var dx = evt.clientX - currentX;
var dy = evt.clientY - currentY;
currentMatrix.x += dx;
currentMatrix.y += dy;

selectedElement.setAttributeNS(null, "x", currentMatrix.x);
selectedElement.setAttributeNS(null, "y", currentMatrix.y);
currentX = evt.clientX;
currentY = evt.clientY;
}

function deselectElement(evt) {
if (selectedElement !== null) {
selectedElement.removeAttributeNS(null, "onmousemove");
selectedElement.removeAttributeNS(null, "onmouseout");
selectedElement.removeAttributeNS(null, "onmouseup");
selectedElement = null;
}
}

</script>
</body>
</html>