svg-菜鸟

菜鸟:https://www.runoob.com/svg/svg-reference.html
阮一峰:http://www.ruanyifeng.com/blog/2018/08/svg.html

基础

svg意为可缩放矢量图形(scalable vector graphics)。
scale
英 [skeɪl] 美 [skeɪl]
n.(尤指与其他事物相比较时的)规模,范围,程度;等级;级别;等级体系
v.攀登;到达…顶点;去鳞;刮除牙石
vector
英 [ˈvektə®] 美 [ˈvektər]
n.矢量;向量;(传染疾病的)介体,载体;(航空器的)航线
graphic
英 [ˈɡræfɪk] 美 [ˈɡræfɪk]
adj.绘画的;书画的;图样的;图案的;形象的,生动的,逼真的(尤指令人不快的事物)
n.图表,图形,图画(尤指电脑荧屏或书籍、报纸上的)
svg使用XML格式定义图像。svg是使用XML来描述二维图形和绘图程序的语言。

什么是SVG?

  • SVG 指可伸缩矢量图形 (Scalable Vector Graphics)
  • SVG 用来定义用于网络的基于矢量的图形
  • SVG 使用 XML 格式定义图形
  • SVG 图像在放大或改变尺寸的情况下其图形质量不会有所损失
  • SVG 是万维网联盟的标准
  • SVG 与诸如 DOM 和 XSL 之类的 W3C 标准是一个整体
SVG 使用 XML 编写。

SVG 文件必须使用 .svg 后缀来保存:eg:index.svg

<?xml version="1.0" standalone="no"?>

<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" 
"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">

<svg width="100%" height="100%" version="1.1"
xmlns="http://www.w3.org/2000/svg">

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

</svg>

代码解释:
SVG 代码以 元素开始,包括开启标签 和关闭标签 。这是根元素。width 和 height 属性可设置此 SVG 文档的宽度和高度。version 属性可定义所使用的 SVG 版本,xmlns 属性可定义 SVG 命名空间。

SVG 的 用来创建一个圆。cx 和 cy 属性定义圆中心的 x 和 y 坐标。如果忽略这两个属性,那么圆点会被设置为 (0, 0)。r 属性定义圆的半径。

stroke 和 stroke-width 属性控制如何显示形状的轮廓。我们把圆的轮廓设置为 2px 宽,黑边框。

fill 属性设置形状内的颜色。我们把填充颜色设置为红色。

矩形

SVG 有一些预定义的形状元素,可被开发者使用和操作:

  • 矩形
  • 圆形
  • 椭圆
  • 线
  • 折线
  • 多边形
  • 路径
<svg width="100%" height="100%" version="1.1"
     xmlns="http://www.w3.org/2000/svg">
    <rect width="300" height="100"
          x="20" y="20"
          style="fill:rgb(0,0,255);stroke-width:8;
          stroke:rgb(0,0,0)"/>
</svg>

等同于:

<svg width="100%" height="100%" version="1.1"
     xmlns="http://www.w3.org/2000/svg">
    <rect x="20" y="20" width="200" height='100'
          fill="red" stroke="black" stroke-width="8"></rect>
</svg>
<rect x="20" y="20" width="100" height="100"
          style="fill:blue;stroke:pink;stroke-width:5;
          fill-opacity:0.1;stroke-opacity:0.5"/>
<rect x="20" y="20" width="100" height="100"
          style="fill:blue;stroke:pink;stroke-width:5;
		  opacity:0.3"/>
<rect x="20" y="280" rx="20" ry="20" width="250"
          height="100" style="fill:red;stroke:black;
          stroke-width:5;opacity:0.5"/>

rect 元素的 width 和 height 属性可定义矩形的高度和宽度
style 属性用来定义 CSS 属性
CSS 的 fill 属性定义矩形的填充颜色(rgb 值、颜色名或者十六进制值)
CSS 的 stroke-width 属性定义矩形边框的宽度
CSS 的 stroke 属性定义矩形边框的颜色
x 属性定义矩形的左侧位置(例如,x=“0” 定义矩形到浏览器窗口左侧的距离是 0px)
y 属性定义矩形的顶端位置(例如,y=“0” 定义矩形到浏览器窗口顶端的距离是 0px)
CSS 的 fill-opacity 属性定义填充颜色透明度(合法的范围是:0 - 1)
CSS 的 stroke-opacity 属性定义笔触颜色的透明度(合法的范围是:0 - 1)
CSS 的 opacity 属性定义整个元素的透明值(合法的范围是:0 - 1)
rx 和 ry 属性可使矩形产生圆角。

圆形

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

cx 和 cy 属性定义圆点的 x 和 y 坐标。如果省略 cx 和 cy,圆的中心会被设置为 (0, 0)。
r 属性定义圆的半径。

椭圆

<ellipse cx="300" cy="150" rx="100" ry="40"
             style="fill:rgb(200,100,50);
             stroke:rgb(0,0,100);stroke-width:2"/>

cx 属性定义圆点的 x 坐标
cy 属性定义圆点的 y 坐标
rx 属性定义水平半径
ry 属性定义垂直半径

三个累叠的椭圆:eg:

<ellipse cx="240" cy="100" rx="220" ry="30"
         style="fill:purple"/>
<ellipse cx="240" cy="70" rx="190" ry="20"
         style="fill:lime"/>
<ellipse cx="240" cy="45" rx="170" ry="15"
         style="fill:yellow"/>

两个组合的椭圆:

<ellipse cx="240" cy="100" rx="220" ry="30"
         style="fill:yellow"/>
<ellipse cx="240" cy="100" rx="190" ry="20"
         style="fill:white"/>

线条 line

<line x1="0" y1="0" x2="300" y2="300"
      style="stroke:rgb(99,99,99);stroke-width:2"/>

x1 属性在 x 轴定义线条的开始
y1 属性在 y 轴定义线条的开始
x2 属性在 x 轴定义线条的结束
y2 属性在 y 轴定义线条的结束

polygon 多边形

polygon
英 [ˈpɒlɪɡən] 美 [ˈpɑːliɡɑːn]
n.多边形;多角形
派生词:polygonal adj.
标签用来创建含有不少于三个边的图形。

三角形:

<polygon points="150,100 200,150 100,150"
         style="fill:#cccccc;stroke:#000000;stroke-width:1"/>

points 属性定义多边形每个角的 x 和 y 坐标。
一个正方形:

<polygon points="200,100 300,100 300,200 200,200"
         style="fill:#cccccc;stroke:#000000;stroke-width:1"/>

变异图形:(正方形修改了points的数据顺序)eg:沙漏形状

<polygon points="200,100 300,100 200,200 300,200"
         style="fill:#cccccc;stroke:#000000;stroke-width:1"/>

polyline 折线

SVG 标签用来创建仅包含直线的形状。

<polyline points="0,0 0,20 20,20 20,40 40,40 40,60 60,60"
          style="fill:white;stroke:red;stroke-width:2"/>

path 路径

标签用来定义路径。

M = moveto
L = lineto
H = horizontal lineto
V = vertical lineto
C = curveto
S = smooth curveto
Q = quadratic Belzier curve
T = smooth quadratic Belzier curveto
A = elliptical Arc
Z = closepath
注释:以上所有命令均允许小写字母。大写表示绝对定位,小写表示相对定位。

<path d="M250 150 L150 350 L350 350 Z" />

由于绘制路径的复杂性,因此强烈建议您使用 SVG 编辑器来创建复杂的图形。
eg:https://c.runoob.com/more/svgeditor/
使用它绘制一个复杂图形,然后导出svg文件,这就是复杂的图形path!

text 文本

 <text x="10" y="15" fill="red">I love SVG</text>
 

x,y是text左上角的点!

旋转:

<text x="10" y="55" fill="red" transform="rotate(30 20,40)">I love SVG</text>

多行文字:

<text x="10" y="210" style="fill:red;">
    Several lines:
    <tspan x="10" y="245">First line</tspan>
    <tspan x="10" y="270">Second line</tspan>
</text>

stroke

  • stroke 颜色
  • stroke-width 宽度
  • stroke-linecap butt,round,square(默认,圆头,方头)
  • stroke-dasharray 虚线
<rect x="20" y="20" width="100" height="100"
      style="fill:blue;stroke:pink;stroke-width:15;
      fill-opacity:0.1;stroke-opacity:1;stroke-linecap: round;"
      stroke-dasharray='15,15'/>
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值