SVG (SVG的概念 、SVG 实例 、SVG 在HTML中 、SVG 矩形 、SVG 圆形 、SVG 椭圆 、SVG 直线 、SVG 多边形、svg验证码 )

目录

1、SVG的概念

2、SVG 实例

3、SVG 在HTML中

4、SVG 矩形 -

5、SVG 圆形 -

6、SVG 椭圆 -

7、SVG 直线 -

8、SVG 多边形 -

 9、SVG 多段线 -

10、SVG 路径 -

11、SVG 文本 -

12、SVG Stroke 属性

13、SVG 滤镜

14、SVG 模糊效果—— 和

15、SVG 阴影—— 和

16、SVG 线性渐变 -

17、SVG 放射性渐变 -

18、svg验证码


1、SVG的概念

SVG矢量图:就是用标签代码来画图

canvas:是H5出的技术,用JS来画图    ( SVG和canvas都是代码)

img:是图片,是图片编码

SVG是一种可伸缩矢量图型(就是用标签代码来画图),它基于XML并用于描述图形的语言;

不同于用像素来描绘的矩阵图像(JPG、PNG、GIF),SVG是和分辨率无关的;

SVG图像可以通过JS和DOM操作来创建和操控;

SVG有自己庞大的语法和较大的复杂度,我们这里只是了解下有这种图像格式;

2、SVG 实例

<svg version="1.1"
  baseProfile="full"
  width="300" height="200"
  xmlns="http://www.w3.org/2000/svg">

  <rect width="100%" height="100%" stroke="red" stroke-width="4" fill="yellow" />

  <circle cx="150" cy="100" r="80" fill="green" />

  <text x="150" y="115" font-size="16" text-anchor="middle" fill="white">RUNOOB SVG TEST</text>

</svg>

SVG 代码解析:

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

SVG 的 <rect> 用来创建一个矩形,通过 fill 把背景颜色设为黄色。

SVG 的 <circle> 用来创建一个圆。cx 和 cy 属性定义圆中心的 x 和 y 坐标。如果忽略这两个属性,那么圆点会被设置为 (0, 0),r 属性定义圆的半径。 一个半径 80px 的绿色圆圈 <circle> 绘制在红色矩形的正中央 (向右偏移 150px,向下偏移115px)。

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

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

关闭标签 </svg> 的作用是关闭 SVG 元素和文档本身。

注释:所有的开启标签必须有关闭标签!

3、SVG 在HTML中

SVG 文件可通过以下标签嵌入 HTML 文档:<embed>、<object> 或者 <iframe>。

SVG的代码可以直接嵌入到HTML页面中,或您可以直接链接到SVG文件。

4、SVG 矩形 - <rect>

  • 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 属性可使矩形产生圆角。
<svg xmlns="http://www.w3.org/2000/svg" version="1.1">
  <rect x="50" y="20" width="150" height="150"
  style="fill:blue;stroke:pink;stroke-width:5;fill-opacity:0.1;
  stroke-opacity:0.9"/>
</svg>
<svg xmlns="http://www.w3.org/2000/svg" version="1.1">
  <rect x="50" y="20" width="150" height="150"
  style="fill:blue;stroke:pink;stroke-width:5;opacity:0.5"/>
</svg>

5、SVG 圆形 - <circle>

  • cx和cy属性定义圆点的x和y坐标。如果省略cx和cy,圆的中心会被设置为(0, 0)
  • r属性定义圆的半径
<svg xmlns="http://www.w3.org/2000/svg" version="1.1">
  <circle cx="100" cy="50" r="40" stroke="black"
  stroke-width="2" fill="red"/>
</svg>

6、SVG 椭圆 - <ellipse>

  • CX属性定义的椭圆中心的x坐标
  • CY属性定义的椭圆中心的y坐标
  • RX属性定义的水平半径
  • RY属性定义的垂直半径
<svg xmlns="http://www.w3.org/2000/svg" version="1.1">
  <ellipse cx="240" cy="50" rx="220" ry="30" style="fill:yellow"/>
  <ellipse cx="220" cy="50" rx="190" ry="20" style="fill:white"/>
</svg>

7、SVG 直线 - <line>

  • x1 属性在 x 轴定义线条的开始
  • y1 属性在 y 轴定义线条的开始
  • x2 属性在 x 轴定义线条的结束
  • y2 属性在 y 轴定义线条的结束
<svg xmlns="http://www.w3.org/2000/svg" version="1.1">
  <line x1="0" y1="0" x2="200" y2="200"
  style="stroke:rgb(255,0,0);stroke-width:2"/>
</svg>

8、SVG 多边形 - <polygon>

创建一个五角星

<svg height="210" width="500">
  <polygon points="100,10 40,198 190,78 10,78 160,198"
  style="fill:lime;stroke:purple;stroke-width:5;fill-rule:nonzero;" />
</svg>

 9、SVG 多段线 - <polyline>

<svg xmlns="http://www.w3.org/2000/svg" version="1.1">
  <polyline points="0,40 40,40 40,80 80,80 80,120 120,120 120,160" style="fill:white;stroke:red;stroke-width:4" />
</svg>

10、SVG 路径 - <path>

<path> 元素用于定义一个路径。

下面的命令可用于路径数据:

  • M = moveto
  • L = lineto
  • H = horizontal lineto
  • V = vertical lineto
  • C = curveto
  • S = smooth curveto
  • Q = quadratic Bézier curve
  • T = smooth quadratic Bézier curveto
  • A = elliptical Arc
  • Z = closepath
<svg xmlns="http://www.w3.org/2000/svg" version="1.1">
    <path d="M150 0 L75 200 L225 200 Z" />
</svg>

11、SVG 文本 - <text>

<svg xmlns="http://www.w3.org/2000/svg" version="1.1">
  <text x="10" y="20" style="fill:red;">Several lines:
    <tspan x="10" y="45">First line</tspan>
    <tspan x="10" y="70">Second line</tspan>
  </text>
</svg>

12、SVG Stroke 属性

<svg xmlns="http://www.w3.org/2000/svg" version="1.1">
  <g fill="none">
    <path stroke="red" d="M5 20 l215 0" />
    <path stroke="blue" d="M5 40 l215 0" />
    <path stroke="black" d="M5 60 l215 0" />
  </g>
</svg>

13、SVG 滤镜

14、SVG 模糊效果——<defs> 和 <filter>

15、SVG 阴影——<defs> 和 <filter>

16、SVG 线性渐变 - <linearGradient>

17、SVG 放射性渐变 - <radialGradient>

18、svg验证码

也可以在官网看手册如何使用:

svg-captcha - npm

实现验证码使用svg-captcha第三方模块

1.下载
cnpm i --save svg-captcha
也可以写:   npm i svg-captcha

2.导入生成验证码

//Service文件:或者 controller下的user.js中
'use strict';
const Service = require('egg').Service;
const svgCaptcha = require('svg-captcha');   //核心==>引入
class ToolsService extends Service {
    // 产生验证码
    async captcha() {
        const captcha = svgCaptcha.create({   //核心==>设置验证码的样式
            size: 4, //随机字符串的个数
            fontSize: 50,
            width: 100,
            height: 40,
            background: '#cc9966'
        });
        this.ctx.session.code = captcha.text;//缓存验证码中的文字
        return captcha;//返回验证码
    }
}
module.exports = ToolsService;

//控制器文件:
    async coder() {
        const {ctx} = this;
        let captcha = await this.service.tools.captcha(); // Service里面的方法
        ctx.body = captcha.data; // 返回一张svg图片
    }

猜你喜欢

转载自blog.csdn.net/qq_52301431/article/details/126002366