什么是SVG?维基百科介绍:
SVG1.x 版本
SVG 2.0 版本(推荐)
优点:
缺点:
svg<?xml version="1.0" standalone="no" ?>
<svg
width="100"
height="100"
xmlns="http://www.w3.org/2000/svg"
>
<rect x="0" y="0" width="100" height="100"></rect>
</svg>
SVG 使用的 坐标系统(网格系统) 和 Canvas的差不多。坐标系是 以左上角为 (0,0) 坐标原点,坐标以像素为单位,x 轴正方向是向右,y 轴正方向是向下。
SVG Grid(坐标系)
<svg>
元素默认宽为 300px, 高为 150px。<svg>
元素和其它元素一样也是有一个坐标空间的,其原点位于元素的左上角,被称为初始视口坐标系<svg>
的 transform 属性可以用来移动、旋转、缩放SVG中的某个元素,如<svg>
中某个元素用了变形,该元素内部会建立一个新的坐标系统,该元素默认后续所有变化都是基于新创建的坐标系统。SVG坐标系统,在没有明确指定单位时,默认以像素为单位。
比如:<rect x="0" y="0" width="100" height="100" />
当然我们也可以手动指明坐标系的单位,比如:
视口(viewport)
<svg>
元素的width和height属性指定视口的大小。视口坐标系
用户坐标系( 也称为当前坐标系或正在使用的用户空间,后面绘图都是参照该坐标系 )
为什么要有两个坐标系?
视图框(viewBox)
<svg>
使用width和height),浏览器就会创建一个与其相同的默认用户坐标系。viewBox语法
<min-x>
<min-y>
<width>
<height>
,比如:viewBox =' 0 0 100 100'
<min-x>
和 <min-y>
确定视图框的左上角坐标(不是修改用户坐标系的原点,绘图还是从原来的 0, 0 开始)<width>
<height>
确定该视图框的宽度和高度。
➢ 宽度和高度不必与父 <svg>
元素上设置的宽度和高度相同。
➢ 宽度和高度负值无效,为 0 是禁用元素的显示。svg<svg width="400" height="400" viewBox="0 0 100 100" >
<circle cx="50" cy="50" r="50"></circle>
</svg>
svg<svg width="400" height="400" viewBox="50 50 100 100" >
<circle cx="50" cy="50" r="50"></circle>
</svg>
svg<svg width="400" height="400"
viewBox="0 0 200 100"
preserveAspectRatio="xMinYMin"
>
<circle cx="50" cy="50" r="50"></circle>
</svg>
关于viewBox属性,可以参考这篇文章,非常容易理解如何理解SVG中的viewport、viewBox和preserveAspectRatio
<rect>
元素6 个基本属性 x
y
width
height
rx
ry
html<rect x="60" y="10" rx="10" ry="10" width="30" height="30"></rect>
<circle>
元素3 个基本属性。 r
cx
cy
html<circle cx="100" cy="100" r="50" fill="red"></circle>
<ellipse>
元素4 个基本属性 rx
ry
cx
cy
html<ellipse cx="100" cy="100" rx="25" ry="50" fill="red"></ellipse>
<line>
元素4 个基本属性
html<!-- stroke , 而不是 fill -->
<line x1="100" y1="100" x2="200" y2="100" stroke="red" stroke-width="5"></line>
<polyline>
元素1 个基本属性
html<!-- 第1种写法 -->
<!-- <polyline points="20 0, 80 50, 20, 100"></polyline> -->
<polyline
points="20 0, 80 50, 20, 100"
fill="transparent"
stroke="red"
></polyline>
<!-- 第2种写法 -->
<polyline points="20 0 80 50 20 100"></polyline>
<!-- 第3种写法 -->
<polyline points="20 ,0 ,80 ,50 ,20, 100"></polyline>
<polygon>
元素1 个基本属性
html<polygon points="20 0, 80 50, 20 100" fill="transparent" stroke="red"></polygon>
<path>
元素1 个基本属性
html<!-- 1.使用path 绘制一个三角形 -->
<!-- <path d="M 20 0, 80 50, 20 100" fill="transparent" stroke="red"></path> -->
<!-- 1.使用path 绘制一个闭合的三角形 -->
<!-- <path d="M 20 0, 80 50, 20 100 Z" fill="transparent" stroke="red"></path> -->
<!-- 1.使用 path 绘图的命令:
M moveTo
Z close path
L lineTo
-->
<path d="M 20 0,L 80 50,L 20 100 Z" fill="transparent" stroke="red"></path>
上面的 M
Z
L
都是一条命令表示 移动到某处
关闭路径
以及 连线到某处
,还有很多其他的命令,这里不讲了,遇到去查即可。
在SVG中绘制一张图片,在<image>
元素的 href 属性引入图片URL
html<!-- svg 1.0版本的语法 -->
<svg
version="1.0"
baseProfile="full"
width="300" height="300"
xmlns="http://www.w3.org/2000/svg"
xmlns:xlink="http://www.w3.org/1999/xlink"
>
<image
x="0"
y="0"
xlink:href="../images/googlelogo_color_92x30dp.png"
width="100"
height="100"
>
</image>
</svg>
<!-- svg 2.0 + 1.0 版本的语法( 为了兼容以前的浏览器的写法 ) -->
<svg
version="1.0"
baseProfile="full"
width="300" height="300"
xmlns="http://www.w3.org/2000/svg"
xmlns:xlink="http://www.w3.org/1999/xlink"
>
<image
x="0"
y="0"
xlink:href="../images/googlelogo_color_92x30dp.png"
href="../images/googlelogo_color_92x30dp.png"
width="100"
height="100"
>
</image>
</svg>
<text>
元素的基本属性
<text>
元素的字体属性
其它文本相关的元素:
<tspan>
元素用来标记大块文本的子部分,它必须是一个text元素或别的tspan元素的子元素。
✓ x 和 y 属性决定了文本在视口坐标系中显示的位置。
✓ alignment-baseline 基线对齐属性:auto 、baseline、middle、hanging、top、bottom ... ,默认是 autohtml<!-- 1.在svg中绘制一个文字 -->
<!-- <text x="100" y="100" font-size="50" fill="red">Ay</text> -->
<!-- 2.文本的对齐方式 -->
<!-- <text x="100" y="100" text-anchor="middle" font-size="50" fill="red">Ay</text> -->
<!-- 3.基线对齐方式 :
有 auto 、middle 或 hanging 值, 默认值:auto
-->
<text x="100" y="100" dominant-baseline="middle" font-size="50" fill="red">Ay</text>
<!-- 4.在svg中使用tspan绘制一个文字 -->
<text x="40" y="100" font-size="20">
iPhone14
<tspan fill="red">¥100</tspan>
</text>
1. 元素的组合 g
<g>
元素的属性(该元素只包含全局属性)
html<!-- <circle cx="50" cy="50" r="25" fill="transparent" stroke="red"></circle>
<circle cx="80" cy="50" r="25" fill="transparent" stroke="red"></circle>
<circle cx="110" cy="50" r="25" fill="transparent" stroke="red"></circle>
<circle cx="140" cy="50" r="25" fill="transparent" stroke="red"></circle> -->
<!--
g 元素没有专有的属性,只有全局的属性
全局属性:id class style fill stroke onclick
-->
<g fill="transparent" stroke="red">
<circle cx="50" cy="50" r="25"></circle>
<circle cx="80" cy="50" r="25"></circle>
<circle cx="110" cy="50" r="25"></circle>
<circle cx="140" cy="50" r="25"></circle>
</g>
2. 元素的复用和引入 defs
和 use
<defs>
元素,定义可复用元素。
< defs >
元素中定义的图形元素是不会直接显示的。<use>
来呈现在defs中定义的元素。<defs>
元素没有专有属性,使用时通常也不需添加任何属性。<use>
元素的属性
html<svg width="300" height="300" xmlns="http://www.w3.org/2000/svg">
<defs>
<!-- 0.样式 -->
<style>
rect{
fill: green;
}
</style>
<!-- 1.定义了一个矩形 -->
<rect id="rectangle" x="0" y="0" width="100" height="50"></rect>
<!-- 2.定义了一个组合图形 -->
<g id="logo" fill="transparent" stroke="red">
<circle cx="50" cy="50" r="25"></circle>
<circle cx="80" cy="50" r="25"></circle>
<circle cx="110" cy="50" r="25"></circle>
<circle cx="140" cy="50" r="25"></circle>
</g>
<!-- 定义渐变 -->
<!-- 滤镜 -->
</defs>
<!-- 在这里进行图形的复用 -->
<!-- <use href="#rectangle"></use> -->
<!-- <use x="100" y="100" href="#rectangle"></use> -->
<!-- <use href="#logo"></use> -->
<!-- <use x="100" y="100" href="#logo"></use> -->
</svg>
<svg width="300" height="300" xmlns="http://www.w3.org/2000/svg" >
<!--
他的宽和高是没有生效的 ???? 只用use引用的图形是 svg 或 symbol 才会起作用
-->
<use href="#rectangle" width="200" height="100" ></use>
</svg>
<svg width="300" height="300" xmlns="http://www.w3.org/2000/svg" >
<use href="#logo"></use>
</svg>
<symbol>
元素和 <defs>
元素类似,也是用于定义可复用元素,然后通过 <use>
元素来引用显示。
<symbol>
元素中定义的图形元素默认也是不会显示在界面上。<symbol>
元素常见的应用场景是用来定义各种小图标,比如:icon、logo、徽章等<symbol>
元素的属性
viewBox:定义当前 <symbol>
的视图框。
x / y :symbol元素的 x / y坐标。 ;默认值:0
width / height:symbol元素的宽度。 默认值:0
<symbol>
和<defs>
的区别
<defs>
元素没有专有属性,而<symbol>
元素提供了更多的属性
✓ 比如: viewBox、 preserveAspectRatio 、x、y、width、height等。
<symbol>
元素有自己用户坐标系,可以用于制作SVG精灵图。
<symbol>
元素定义的图形增加了结构和语义性,提高文档的可访问性。
SVG ICON文件-合并成SVG精灵图:https://www.zhangxinxu.com/sp/svgo
html<svg width="300" height="300" xmlns="http://www.w3.org/2000/svg" >
<!-- 1.ICON previous-->
<symbol id="previous" viewBox="0 0 100 100">
<path fill="currentColor" d="M 80 0,L 20 50, L 80 100 Z"></path>
</symbol>
<!-- 2.ICON next -->
<symbol id="next" viewBox="0 0 100 100">
<polygon points="20 0, 80 50, 20 100"></polygon>
</symbol>
<!-- 复用 -->
<!-- <use href="#previous" width="100" height="100"></use> -->
</svg>
<!-- 复用 -->
<svg width="300" height="300" xmlns="http://www.w3.org/2000/svg" >
<!-- 直接在use上指定ICON的 width和 hegiht -->
<use href="#previous" width="50" height="50"></use>
</svg>
<!-- 这个属于缩小 -->
<svg width="30" height="30" xmlns="http://www.w3.org/2000/svg" >
<use href="#previous" ></use>
</svg>
<!-- 属于放大 -->
<svg width="200" height="200" xmlns="http://www.w3.org/2000/svg" >
<use href="#previous" ></use>
</svg>
如果想要给SVG中的元素上色,一般有两种方案可以实现:
html<rect x="10" y="10" width="100" height="100" fill="currentColor" fill-opacity="0.4"></rect>
<rect x="10" y="10" width="100" height="100"
fill="transparent"
stroke="red"
stroke-width="3"
stroke-opacity="1"
></rect>
直接编写CSS样式实现填充和描边
哪些属性可以使用CSS设置,哪些不能呢?
CSS给SVG中的元素填充、描边和上色,支持如下4种编写方式:
<defs>
中的 <style>
标签中<head>
中的<style>
标签中CSS样式优先级别:内联的 style
> defs中的style
> 外部 / head内部
> 属性 fill
html<!-- 1.行内的样式 -->
<rect x="10" y="10" width="100" height="100"
style="fill:red;"
></rect>
<!-- 2.行内的样式 -->
<defs>
<style>
.rectangle{
fill: green;
stroke: red;
}
</style>
</defs>
<rect class="rectangle" x="10" y="10" width="100" height="100"></rect>
/* 3.行内的样式 */
<style>
.rectangle{
fill: blue;
}
</style>
<rect class="rectangle" x="10" y="10" width="100" height="100"></rect>
/* 4. 引入css文件*/
<link rel="stylesheet" href="./style.css">
<rect class="rectangle" x="10" y="10" width="100" height="100"></rect>
SVG除了可以简单的填充和描边,还支持在填充和描边上应用渐变色。渐变有两种类型:线性渐变
和 径向渐变
。
<defs>
标签内部,渐变通常是可复用的。线性渐变,是沿着直线改变颜色。下面看一下线性渐变的使用步骤:
<linearGradient>
节点,并添加 id 属性。<linearGradient>
内编写几个 <stop>
结点。
✓ 给 <stop>
结点指定位置 offset属性和 颜色stop-color属性,用来指定渐变在特定的位置上应用什么颜色
➢ offset 和 stop-color 这两个属性值,也可以通过 CSS 来指定。
✓ 也可通过 stop-opacity 来设置某个位置的半透明度。<linearGradient>
节点。
✓ 比如:属性fill属性设置为url( #Gradient2 )即可。html<svg width="300" height="300" xmlns="http://www.w3.org/2000/svg">
<!-- 定义可以复用的元素: 样式, 渐变, 图形, 滤镜... -->
<defs>
<!-- 默认的渐变色 -->
<linearGradient id="gradient1">
<stop offset="0%" stop-color="red"></stop>
<stop offset="50%" stop-color="green"></stop>
<stop offset="100%" stop-color="blue"></stop>
</linearGradient>
<!-- 这个是制定渐变的方向 -->
<linearGradient id="gradient2" x1="0" y1="0" x2="1" y2="1">
<stop offset="0%" stop-color="red"></stop>
<stop offset="50%" stop-color="green"></stop>
<stop offset="100%" stop-color="blue"></stop>
</linearGradient>
<!-- 通过形变 渐变色(了解 ) -->
<linearGradient id="gradient3" gradientTransform="rotate(0)">
<stop offset="0%" stop-color="red"></stop>
<stop offset="50%" stop-color="green"></stop>
<stop offset="100%" stop-color="blue"></stop>
</linearGradient>
</defs>
<rect x="0" y="0" width="100" height="50" fill="url(#gradient3)"></rect>
</svg>
在前端开发中,毛玻璃效果有几种方案来实现:
<filter>
:元素作为滤镜操作的容器,该元素定义的滤镜效果需要在SVG元素上的 filter 属性引用。
✓ x , y, width, height 定义了在画布上应用此过滤器的矩形区域。x, y 默认值为 -10%(相对自身);width ,height 默认
值为 120% (相对自身) 。<feGaussianBlur>
:该滤镜专门对输入图像进行高斯模糊
✓ stdDeviation 熟悉指定模糊的程度<feOffset>
:该滤镜可以对输入图像指定它的偏移量。html<svg width="200" height="200" xmlns="http://www.w3.org/2000/svg" >
<defs>
<!-- 高斯模糊的 效果 -->
<filter id="blurFilter">
<!-- ...... -->
<feGaussianBlur stdDeviation="8"></feGaussianBlur>
</filter>
</defs>
<image
href="../images/avatar.jpeg"
width="200"
height="200"
filter="url(#blurFilter)"
>
</image>
</svg>
transform
属性支持的函数有 translate
rotate
scale
skew
matrix
1. 平移 translate
与 CSS 的 translate 相似但有区别,这里只支持 2D 变换,不需单位。
html<rect
x="0"
y="0"
width="100"
height="50"
transform="translate(200, 200)"
></rect>
2. 旋转 rotate
与CSS的rotate相似但有区别。区别是:支持2D变换,不需单位,可指定旋转原点。
html<rect
transform="translate(100, 0) rotate(45, 50, 25)"
x="0" y="0" width="100" height="50"
>
</rect>
3. 缩放 scale
与CSS的scale相似但有区别,这只支持2D变换,不需单位。
html <rect
transform="translate(100, 100) scale(1, 2)"
x="0"
y="0"
width="100"
height="50"
></rect>
stroke
是描边属性,专门给图形描边。如果想给各种描边添加动画效果,需用到下面两个属性:
描边动画实现步骤:
html<style>
#line1 {
/* 指定为虚线 */
stroke-dasharray: 100px;
/* 可见 */
stroke-dashoffset: 20px;
/* animation: line1Move 2s linear; */
}
@keyframes line1Move {
0% {
/* 不可见 */
stroke-dashoffset: 100px;
}
100% {
/* 可见 */
stroke-dashoffset: 0px;
}
}
</style>
<svg width="300" height="300" xmlns="http://www.w3.org/2000/svg">
<!--
stroke , 而不是 fill
-->
<line
id="line1"
x1="100"
y1="70"
x2="200"
y2="70"
stroke="red"
stroke-width="10"
></line>
</svg>
SMIL(Synchronized Multimedia Integration Language 同步多媒体集成语言)是W3C推荐的可扩展标记语言,用于描述 多媒体演示。
<head>
<body>
<seq>
<par>
<excl>
等元素SMIL的应用
<set>
、<animate>
、<animateMotion>
...)。SVG动画实现方式
SMIL动画的优势
SVG 中支持SMIL动画的元素:
<set>
<animate>
<animateColor>
<animateMotion>
1. set
元素
set元素是最简单的 SVG 动画元素。它是在经过特定时间间隔后,将属性设置为某个值(不是过度动画效果)。因此,图像不是连续动画,而是改变一次属性值。它支持所有属性类型,包括那些无法合理插值的属性类型,例如:字符串 和 布尔值。而对于可以合理插值的属性通常首选
<animate>
元素。
常用属性有 attributeName
to
begin
html<!-- 1. 在3秒后自动将长方形瞬间移到右边 -->
<svg width="300" height="300" xmlns="http://www.w3.org/2000/svg" >
<rect x="0" y="0" width="100" height="50" fill="red">
<set
attributeName ='x'
to="200"
begin="3s"
>
</set>
</rect>
</svg>
<!-- 点击长方形后,长方形瞬间移到右边 -->
<svg width="300" height="300" xmlns="http://www.w3.org/2000/svg" >
<rect id="rectangle" x="0" y="0" width="100" height="50" fill="green">
<set
attributeName ='x'
to="200"
begin="rectangle.click"
>
</set>
</rect>
</svg>
2. animate
元素
常用属性有:attributeName
from
values
begin
dur
fill
repeatCount
<svg width="300" height="200" xmlns="http://www.w3.org/2000/svg" > <rect x="0" y="0" width="100" height="50" fill="green"> <!-- form: 0 to: 200 --> <animate attributeName="x" values="0; 170; 200" dur="3s" repeatCount="indefinite" > </animate> <animate attributeName="fill" values="red;green" dur="3s" repeatCount="indefinite" > </animate> </rect> </svg>
本文作者:叶继伟
本文链接:
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!