Skip to content

Commit f9f8572

Browse files
committed
doc: add canvas svg tutorial document.
1 parent a6fbccf commit f9f8572

File tree

2 files changed

+116
-2
lines changed

2 files changed

+116
-2
lines changed

docs/tutorial/canvas.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,3 +154,9 @@ ctx.fillRect(10, 10, 150, 80);
154154
</script>
155155
```
156156
<!--rehype:style=height: 420px;-->
157+
158+
[1]: ../assets/chrome.svg
159+
[2]: ../assets/edge.svg
160+
[3]: ../assets/firefox.svg
161+
[4]: ../assets/safari.svg
162+
[5]: ../assets/opera.svg

docs/tutorial/svg.md

Lines changed: 110 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,112 @@
1-
svg.md
1+
HTML SVG Graphics
22
===
33

4-
欢迎您编辑 <a target="__blank" href="https://github.com/jaywcjlove/html-tutorial/blob/main/docs/tutorial/svg.md">docs/tutorial/svg.md</a> 文件,共建 HTML Tutorial 文档。
4+
SVG 以 XML 格式定义基于矢量的图形。
5+
6+
## 什么是 SVG?
7+
8+
* SVG 代表可缩放矢量图形
9+
* SVG 用于为 Web 定义图形
10+
* SVG 是 W3C 推荐的
11+
12+
## HTML \<svg> 元素
13+
14+
HTML [`<svg>`](../tags/svg.md) 元素是 SVG 图形的容器。
15+
16+
SVG 有多种绘制路径、框、圆、文本和图形图像的方法。
17+
18+
## 浏览器支持
19+
20+
表中的数字指定了第一个完全支持 [`<svg>`](../tags/svg.md) 元素的浏览器版本。
21+
22+
| Element | ![chrome][1] | ![edge][2] | ![firefox][3] | ![safari][4] | ![opera][5] |
23+
| ---- | ---- | ---- | ---- | ---- | ---- |
24+
| \<svg> | 4.0 | 9.0 | 3.0 | 3.2 | 10.1 |
25+
26+
## SVG 圆
27+
28+
```html idoc:preview:iframe
29+
<!DOCTYPE html>
30+
<html>
31+
<body>
32+
<svg width="100" height="100">
33+
<circle cx="50" cy="50" r="40" stroke="green" stroke-width="4" fill="yellow">
34+
</svg>
35+
</body>
36+
</html>
37+
```
38+
<!--rehype:style=height: 150px;-->
39+
40+
## SVG 矩形
41+
42+
```html idoc:preview:iframe
43+
<svg width="400" height="100">
44+
<rect width="400" height="100" style="fill:rgb(0,0,255); stroke-width:10; stroke:rgb(0,0,0)" />
45+
</svg>
46+
```
47+
<!--rehype:style=height: 130px;-->
48+
49+
## SVG 圆角矩形
50+
51+
```html idoc:preview
52+
<svg width="400" height="180">
53+
<rect x="50" y="20" rx="20" ry="20" width="150" height="150"
54+
style="fill:red;stroke:black;stroke-width:5;opacity:0.5" />
55+
</svg>
56+
```
57+
<!--rehype:style=height: 230px;-->
58+
59+
## SVG 星
60+
61+
```html idoc:preview
62+
<svg width="300" height="200">
63+
<polygon points="100,10 40,198 190,78 10,78 160,198"
64+
style="fill:lime;stroke:purple;stroke-width:5;fill-rule:evenodd;" />
65+
</svg>
66+
```
67+
<!--rehype:style=height: 230px;-->
68+
69+
## SVG Logo
70+
71+
```html idoc:preview
72+
<svg height="130" width="500">
73+
<defs>
74+
<linearGradient id="grad1" x1="0%" y1="0%" x2="100%" y2="0%">
75+
<stop offset="0%" style="stop-color:rgb(255,255,0);stop-opacity:1" />
76+
<stop offset="100%" style="stop-color:rgb(255,0,0);stop-opacity:1" />
77+
</linearGradient>
78+
</defs>
79+
<ellipse cx="100" cy="70" rx="85" ry="55" fill="url(#grad1)" />
80+
<text fill="#ffffff" font-size="45" font-family="Verdana" x="50" y="86">SVG</text>
81+
Sorry, your browser does not support inline SVG.
82+
</svg>
83+
```
84+
<!--rehype:style=height: 180px;-->
85+
86+
## SVG 和 Canvas 的区别
87+
88+
SVG 是一种用 XML 描述 2D 图形的语言。
89+
90+
Canvas 动态绘制 2D 图形(使用 JavaScript)。
91+
92+
SVG 是基于 XML 的,这意味着每个元素都可以在 SVG DOM 中使用。 您可以为元素附加 JavaScript 事件处理程序。
93+
94+
在 SVG 中,每个绘制的形状都被记忆为一个对象。 如果 SVG 对象的属性发生变化,浏览器可以自动重新渲染形状。
95+
96+
画布是逐像素渲染的。 在画布中,一旦图形被绘制出来,浏览器就会忘记它。 如果要更改其位置,则需要重新绘制整个场景,包括可能已被图形覆盖的任何对象。
97+
98+
## Canvas 和 SVG 的比较
99+
100+
The table below shows some important differences between Canvas and SVG:
101+
102+
| Canvas | SVG |
103+
| ---- | ---- |
104+
| * 取决于分辨率 <br />* 不支持事件处理程序 <br />* 文本渲染能力差 <br />* 您可以将生成的图像保存为 .png 或 .jpg <br />* 非常适合图形密集型游戏 | * 独立于分辨率 <br />* 支持事件处理程序 <br />* 最适合具有大渲染区域的应用程序(谷歌地图)<br />* 如果复杂,渲染速度会很慢(任何使用 DOM 的东西都会很慢)<br /> * 不适合游戏应用 |
105+
106+
107+
108+
[1]: ../assets/chrome.svg
109+
[2]: ../assets/edge.svg
110+
[3]: ../assets/firefox.svg
111+
[4]: ../assets/safari.svg
112+
[5]: ../assets/opera.svg

0 commit comments

Comments
 (0)