码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • SVG—初识1


    SVG—初识1

    • SVG在页面中
      • SVG 文件可通过以下标签嵌入 HTML 文档:\、\ 或者 \

        直接在Html中嵌入svg代码

        <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>
        
        • 1
        • 2
        • 3

        用a标签链接

        <a href="circle1.svg">查看 SVG 文件a>
        
        • 1

        SVG形状

        1. 矩形
        2. 圆形
        3. 椭圆
        4. 线
        5. 折线
        6. 多边形
        7. 路径

        矩形

        <svg xmlns="http://www.w3.org/2000/svg" version="1.1">
        	<rect width="300" height="100" style="fill:rgb(0,0,255);stroke-width:1;stroke:rgb(0,0,0)">rect>
        svg>
        
        • 1
        • 2
        • 3
        • rect 元素的 width 和 height 属性可定义矩形的高度和宽度
        • style 属性用来定义 CSS 属性
        • CSS 的 fill 属性定义矩形的填充颜色(rgb 值、颜色名或者十六进制值)
        • CSS 的 stroke-width 属性定义矩形边框的宽度
        • CSS 的 stroke 属性定义矩形边框的颜色
          在这里插入图片描述
        <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;stroke-opacity:0.9;fill-opacity:0.1"
            >rect>
          svg>
        
        • 1
        • 2
        • 3
        • 4
        • 5
        • 6
        • 7
        • 8
        • 9
        • x 属性定义矩形的左侧位置(例如,x=“0” 定义矩形到浏览器窗口左侧的距离是 0px)
        • y 属性定义矩形的顶端位置(例如,y=“0” 定义矩形到浏览器窗口顶端的距离是 0px)
        • CSS 的 fill-opacity 属性定义填充颜色透明度(合法的范围是:0 - 1)
        • CSS 的 stroke-opacity 属性定义轮廓颜色的透明度(合法的范围是:0 - 1)
          在这里插入图片描述
        <svg xmlns="http://www.w3.org/2000/svg" version="1.1">
            <rect
              x="30"
              y="20"
              width="150"
              height="150"
              stroke-width="5"
              style="fill:blue;stroke:pink;opacity:0.5"
            />
          svg>
        
        • 1
        • 2
        • 3
        • 4
        • 5
        • 6
        • 7
        • 8
        • 9
        • 10

        在这里插入图片描述

        • CSS opacity 属性用于定义了元素的透明值 (范围: 0 到 1)
        <svg xmlns="http://www.w3.org/2000/svg" version="1.1">
            <rect
              x="50"
              y="20"
              rx="20"
              ry="20"
              width="150"
              height="150"
              style="fill:red;stroke:black;stroke-width:5;opacity:0.5"
            >rect>
          svg>
        
        • 1
        • 2
        • 3
        • 4
        • 5
        • 6
        • 7
        • 8
        • 9
        • 10
        • 11

        在这里插入图片描述

        • rx 和 ry 属性可使矩形产生圆角。

        圆形

        <svg xmlns="http://www.w3.org/2000/svg" version="1.1">
            <circle
              cx="100"
              cy="50"
              r="40"
              stroke-width="2"
              style="fill:red;stroke:black;"
            />
          svg>
        
        • 1
        • 2
        • 3
        • 4
        • 5
        • 6
        • 7
        • 8
        • 9
        • x和cy属性定义圆点的x和y坐标。如果省略cx和cy,圆的中心会被设置为(0, 0)
        • r属性定义圆的半径
          在这里插入图片描述

        椭圆

        <svg xmlns="http://www.w3.org/2000/svg" version="1.1">
             <ellipse cx="150" cy="100" rx="100" ry="50" style="fill:yellow;stroke:purple;stroke-width:2;"/>
           svg>
        
        • 1
        • 2
        • 3

        在这里插入图片描述

        • CX属性定义的椭圆中心的x坐标
        • CY属性定义的椭圆中心的y坐标
        • RX属性定义的水平半径
        • RY属性定义的垂直半径
        <svg xmlns="http://www.w3.org/2000/svg" version="1.1">
            <ellipse cx="200" cy="100" rx="100" ry="30" style="fill:yellow;" />
            <ellipse cx="200" cy="80" rx="80" ry="30" style="fill:purple;" />
            <ellipse cx="200" cy="60" rx="60" ry="30" style="fill:pink" />
          svg>
        
        • 1
        • 2
        • 3
        • 4
        • 5

        在这里插入图片描述

        <svg xmlns="http://www.w3.org/2000/svg" version="1.1">
            <ellipse cx="150" cy="100" rx="100" ry="30" style="fill:yellow;" />
            <ellipse cx="150" cy="100" rx="90" ry="20" style="fill:white" />
          svg>
        
        • 1
        • 2
        • 3
        • 4

        在这里插入图片描述

        直线

        <svg xmlns="http://www.w3.org/2000/svg" version="1.1">
            <line x1="0" y1="200" x2="200" y2="0" style="stroke:yellow;stroke-width:2" />
          svg>
        
        • 1
        • 2
        • 3

        在这里插入图片描述

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

        多边形

        <svg xmlns="http://www.w3.org/2000/svg" version="1.1">
            <polygon points="200,15 300,30 150,45" style="fill:lime;stroke-width:1;stroke:purple"/>
          svg>
        
        • 1
        • 2
        • 3

        在这里插入图片描述

        • points 属性定义多边形每个角的 x 和 y 坐标

        五边形

        <svg xmlns="http://www.w3.org/2000/svg" version="1.1">
            <polygon points="200,15 150,80 180,150 230,150 250,80" style="fill:lightblue;stroke-width:1;stroke:yellow"/>
          svg>
        
        • 1
        • 2
        • 3

        在这里插入图片描述

        <svg width="500" height="210">
            <polygon points="100,10 40,198 190,78 10,78 160,198" style="fill:yellow;stroke-width:5;stroke:mediumpurple;fill-rule:nonzero"/>
          svg>
        
        • 1
        • 2
        • 3

        在这里插入图片描述
        fill-rule:evenodd值时候

        <svg width="500" height="210">
            <polygon points="100,10 40,198 190,78 10,78 160,198" style="fill:yellow;stroke-width:5;stroke:mediumpurple;fill-rule:evenodd"/>
          svg>
        
        • 1
        • 2
        • 3

        在这里插入图片描述

        fill-rule

        有效值nonzero evenodd inherit
        默认值nonzero
        应用于shape形状类元素 和 文字内容类元素
        可继承是
        比例无
        媒体可见
        动画可用是

        多段线

        <svg width="500" height="500">
            <polyline points="10,15 20,35 40,60 80,100 120,150 150,180" style="fill:none;stroke:mediumpurple;stroke-width:3"/>
          svg>
        
        • 1
        • 2
        • 3

        在这里插入图片描述

        <svg width="300" height="300">
            <polyline points="20,20 40,20 40,40 60,40 60,60 80,60 80,80 100,80" style="fill:none;stroke:greenyellow;stroke-width:3" />
          svg>
        
        • 1
        • 2
        • 3

        在这里插入图片描述

        路径

        • 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
        命令参数描述
        Mx y移动画笔到给定坐标,即起始点
        Lx y绘制一条到给定坐标的线,可提供多组坐标绘制折线
        Hx绘制一条到 x 坐标的横线
        Vy绘制一条到 y 坐标的竖线
        Cx y x1 y1 x2 y2绘制一条从当前点到(x,y)的三次贝塞尔曲线,(x1,y1)为曲线的开始控制点,(x2,y2)为曲线的终点控制点
        Sx y x1 y1绘制一条从当前点到(x,y)的三次贝塞尔曲线,(x1,y1)为新端点的控制点
        Qx y x1 y1绘制一条从当前点到(x,y),控制点为(x1,y1)的二次贝塞尔曲线
        Tx y绘制一条从当前点到(x,y)的二次贝塞尔曲线
        Arx ry x-axis-rotation large-arc-flag sweep-flag x y绘制当前点到(x,y)的椭圆弧,椭圆弧的 x,y轴半径分别为 rx,ry。椭圆相对于 x 轴旋转 x-axis-rotation 度。large-arc-flag 等于0表示弧线小于180度,等于1表示弧线大于180度。sweep-flag 等于0 表示弧线逆时针旋转,等于1表示弧线顺时针选装
        Z闭合路径

        元素中支持下列命令,如果是大写命令表示绝对定位,小写则表示相对定位

        <svg width="300" height="300">
            <path d="M150 0 L75 200 L225 200 Z"/>
          svg>
        
        • 1
        • 2
        • 3

        在这里插入图片描述

        创建一个二次方贝塞尔曲线

         <svg xmlns="http://www.w3.org/2000/svg" version="1.1" height="400" width="450">
            <path
              id="lineAB"
              d="M 100 350 l 150 -300"
              style="stroke:red;stroke-width:3;fill:none"
            >path>
            <path
              id="lineBC"
              d="M 250 50 l 150 300"
              style="stroke:red;stroke-width:3;fill:none"
            >path>
            <path
              d="M 175 200 l 150 0"
              style="stroke:green;stroke-width: 3;fill: none"
            >path>
            <path
              d="M 100 350 q 150 -300 300 0"
              style="stroke:blue;stroke-width:3;fill:none"
            />
            <g stroke-width="3" stroke="black">
              <circle cx="100" cy="350" r="3"/>
              <circle cx="250" cy="50" r="3"/>
              <circle cx="400" cy="350" r="3" />
            g>
            <g font-size="30" font="sans-serif" stroke="none" fill="black" text-anchor="middle">
              <text x="100" y="350" dx="-30">Atext>
              <text x="250" y="50" dy="-10">Btext>
              <text x="400" y="350" dx="30">Ctext>
            g>
          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

        在这里插入图片描述

        SVG文本

        <svg xmlns="http://www.w3.org/2000/svg" version="1.1">
            <text x="20" y="20" fill="red">你好我是你爸爸text>
          svg>
        
        • 1
        • 2
        • 3

        在这里插入图片描述

        <svg xmlns="http://www.w3.org/2000/svg" version="1.1">
            <text x="20" y="20" fill="red" transform="rotate(30 20,40)">   
              你好我是你爸爸
            text>
          svg>
        
        • 1
        • 2
        • 3
        • 4
        • 5

        在这里插入图片描述

        路径上的文字

        <svg xmlns="http://www.w3.org/2000/svg" version="1.1" height="400"
                 width="400"
                 xmlns:xlink="http://www.w3.org/1999/xlink">
              <defs>
                <path
                  id="road"
                  d="M0 80 A 50 50 0 0 0 100 80"
                >path>
                <path
                  id="road1"
                  d="M100 80 A 50 50 0 0 1 200 80"
                >path>
                <path
                  id="road2"
                  d="M200 80 A 50 50 0 0 0 300 80"
                >path>
                <path
                  id="road3"
                  d="M300 80 A 50 50 0 0 1 400 80"
                >path>
              defs>
              
              <text x="10" y="100" style="fill:red;">
                <textPath xlink:href="#road">I love SVG I love SVGtextPath>
                <textPath xlink:href="#road1">I love SVG I love SVGtextPath>
                <textPath xlink:href="#road2">I love SVG I love SVGtextPath>
                <textPath xlink:href="#road3">I love SVG I love SVGtextPath>
              text>
            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

        在这里插入图片描述

        元素可以安排任何分小组与 元素的数量。每个 元素可以包含不同的格式和位置。

        <svg xmlns="http://www.w3.org/2000/svg" version="1.1">
            <text x="10" y="20" fill="red">
              Dear Bob,
              <tspan x="50" y="60" fill="blue">你好Jasontspan>
              <tspan x="10" y="100" fill="green">好久不见,你在哪里?tspan>
              <tspan>我在江湖tspan>
            text>
          svg>
        
        • 1
        • 2
        • 3
        • 4
        • 5
        • 6
        • 7
        • 8

        在这里插入图片描述

        作为链接文本

        <svg
            xmlns="http://www.w3.org/2000/svg"
            version="1.1"
            xmlns:xlink="http://www.w3.org/1999/xlink"
          >
            <a xlink:href="http://www.baidu.com" target="_blank">
              <text x="50" y="50" fill="red">百度一下,你就知道text>
            a>
          svg>
        
        • 1
        • 2
        • 3
        • 4
        • 5
        • 6
        • 7
        • 8
        • 9
        百度一下,你就知道

        SVG的Stroke属性

        • stroke
        • stroke-width
        • stroke-linecap
        • stroke-dasharray
        <svg xmlns="http://www.w3.org/2000/svg" version="1.1" width="500" height="500">
            <g fill="none">
              <path stroke="red" d="M50 50 1350 50">path>
              <path stroke="blue" d="M50 100 1350 100">path>
              <path stroke="green" d="M50 150 1350 150">path>
            g>
          svg>
        
        • 1
        • 2
        • 3
        • 4
        • 5
        • 6
        • 7
        <svg xmlns="http://www.w3.org/2000/svg" version="1.1">
            <g stroke="black">
              <path stroke-width="2" d="M5 10 1250 10">path>
              <path stroke-width="4" d="M5 20 1250 20">path>
              <path stroke-width="6" d="M5 30 1250 30">path>
            g>
          svg>
        
        • 1
        • 2
        • 3
        • 4
        • 5
        • 6
        • 7
        <svg xmlns="http://www.w3.org/2000/svg">
            <g stroke="black" stroke-width="6">
              <path stroke-linecap="butt" d="M5 10 1250 10"/>
              <path stroke-linecap="round" d="M5 30 1250 30"/>
              <path stroke-linecap="square" d="M5 50 1250 50"/>
            g>
          svg>
        
        • 1
        • 2
        • 3
        • 4
        • 5
        • 6
        • 7
        <svg xmlns="http://www.w3.org/2000/svg">
            <g stroke="black" stroke-width="4">
              <path stroke-dasharray="10,10" d="M5 10 1250 10">path>
              <path stroke-dasharray="20,10" d="M5 30 1250 30">path>
              <path stroke-dasharray="20,10,15,10,5,2" d="M5 50 1250 50">path>   
            g>
          svg>
        
        • 1
        • 2
        • 3
        • 4
        • 5
        • 6
        • 7

        SVG滤镜

        滤镜元素
        滤镜元素有很多,每一个元素代表一种功能

        • feBlend - 与图像相结合的滤镜
        • feColorMatrix - 用于彩色滤光片转换
        • feComponentTransfer
        • feComposite
        • feConvolveMatrix
        • feDiffuseLighting
        • feFlood
        • feGaussianBlur
        • feImage
        • feMerge
        • feMorphology
        • feOffset - 过滤阴影
        • feSpecularLighting
        • feTile
        • feTurbulence
        • feDistantLight - 用于照明过滤
        • fePointLight - 用于照明过滤
        • feSpotLight - 用于照明过滤

        SVG滤镜定义在元素中
        标签用来定义SVG滤镜

        feGaussianBlur创建模糊效果

        1. 需要使用 标签来定义一个 SVG 滤镜。设置唯一标识id属性,SVG 图形使用这个 id 来引用滤镜。
        2. 使用feGaussianBlur创建模糊效果。in="SourceGraphic"属性定义了模糊效果要应用于整个图片,stdDeviation 属性定义了模糊的程度。
        <svg xmlns="http://www.w3.org/2000/svg" version="1.1" width="400" height="400">
            <defs>
              <filter id="f1" x="0" y="0">
                <feGaussianBlur in="SourceGraphic" stdDeviation="2">feGaussianBlur>
              filter>
            defs>
            <circle cx="60" cy="60" r="50" style="fill:#00a4ff;stroke-width:2;stroke:pink" filter="url(#f1)">circle>
          svg>
        
        • 1
        • 2
        • 3
        • 4
        • 5
        • 6
        • 7
        • 8

        在这里插入图片描述

        feOffset创建阴影效果

        第一个例子偏移一个矩形(带),然后混合偏移图像顶部(含):

        <svg xmlns="http://www.w3.org/2000/svg" version="1.1">
            <defs>
                <filter id="f2" x="0" y="0" width="200%" height="200%">
                    <feOffset dx="20" dy="20" in="SourceGraphic" result="offOut"/>
        
                filter>
            defs>
            <rect width="100" height="100" style="fill:yellow;stroke:green;stroke-width:3;" filter="url(#f2)"/>
        svg>
        
        • 1
        • 2
        • 3
        • 4
        • 5
        • 6
        • 7
        • 8
        • 9

        在这里插入图片描述

        <svg xmlns="http://www.w3.org/2000/svg" version="1.1">
            <defs>
                <filter id="f2" x="0" y="0" width="200%" height="200%">
                    <feOffset dx="20" dy="20" in="SourceGraphic" result="offOut"/>
                    <feBlend in="SourceGraphic" in2="offOut" mode="normal"/>
                filter>
            defs>
            <rect width="100" height="100" style="fill:yellow;stroke:green;stroke-width:3;" filter="url(#f2)"/>
        svg>
        
        • 1
        • 2
        • 3
        • 4
        • 5
        • 6
        • 7
        • 8
        • 9

        在这里插入图片描述

        <svg xmlns="http://www.w3.org/2000/svg" version="1.1" width="500" height="500">
            <defs>
                <filter id="f3" x="0" y="0">
                    <feOffset in="SourceGraphic" dx="20" dy="20" result="offOut" />
                    <feGaussianBlur in="offOut" stdDeviation="15" result="gaublur" />
                    <feBlend in="SourceGraphic" in2="gaublur" mode="normal" />
                filter>
            defs>
            <circle cx="100" cy="100" r="100" style="fill:skyblue;stroke-width: 3;stroke: green;" filter="url(#f3)"/>
        svg>
        
        • 1
        • 2
        • 3
        • 4
        • 5
        • 6
        • 7
        • 8
        • 9
        • 10

        在这里插入图片描述

        • 元素的属性改为"SourceAlpha"在Alpha通道使用残影,而不是整个RGBA像素。
        <svg xmlns="http://www.w3.org/2000/svg" version="1.1" width="1000" height="1000">
            <defs>
                <filter id="f4" x="0" y="0">
                    <feOffset in="SourceAlpha" dx="20" dy="20"  result="offOut"/>
                    <feGaussianBlur in="offOut" stdDeviation="15" result="GauBlur"/>
                    <feBlend in2="GauBlur" in="SourceGraphic" mode="normal" />
                filter>
            defs>
            <ellipse cx="60" cy="110" rx="50" ry="100" style="fill:yellow;stroke-width: 3;stroke: green" filter="url(#f4)"/>
        svg>
        
        • 1
        • 2
        • 3
        • 4
        • 5
        • 6
        • 7
        • 8
        • 9
        • 10

        在这里插入图片描述

        • 过滤器是用来转换偏移的图像使之更接近黑色的颜色。 '0.2’矩阵的三个值都获取乘以红色,绿色和蓝色通道。降低其值带来的颜色至黑色(黑色为0)
        <svg xmlns="http://www.w3.org/2000/svg" version="1.1">
            <defs>
                <filter id="f5" x="0" y="0">
                    <feOffset in="SourceGraphic" dx="20" dy="20" result="offOut" />
                    <feColorMatrix in="offOut" types="matrix" result="matrixOut"
                                   values="0.2 0 0 0 0 0 0.2 0 0 0 0 0 0.2 0 0 0 0 0 1 0" />
                    <feGaussianBlur in="matrixOut" stdDeviation="10" result="gaublur" />
                    <feBlend in="SourceGraphic" in2="gaublur" mode="normal" />
                filter>
            defs>
            <rect width="100" height="100" style="fill:yellow;stroke-width: 3;stroke: green;" filter="url(#f5)"/>
        svg>
        
        • 1
        • 2
        • 3
        • 4
        • 5
        • 6
        • 7
        • 8
        • 9
        • 10
        • 11
        • 12

        ![在这里插入图片描述](https://img-blog.csdnimg.cn/f24b98bf5ce744c58469a2f44d9a9fcf.png

      • 相关阅读:
        Excel中行列范围的转换
        GitHub这5个骚操作,大部分人不知道
        杨校老师课堂之封装工具类【时间工具类|类型转换器|客户端Servlet工具类】
        2022年重庆成人本科可以考研吗?
        SENet双塔模型:在推荐领域召回粗排的应用及其它
        π110E30 单通道数字隔离器兼容代替Si8610BC-B-IS
        mac pycharm配置autopep8
        HTTP Basic 认证
        计算机中丢失mfc140u.dll怎么解决
        JAVAWEB学习笔记-前端基础
      • 原文地址:https://blog.csdn.net/qq_34306228/article/details/126829316
        • 最新文章
        • 攻防演习之三天拿下官网站群
          数据安全治理学习——前期安全规划和安全管理体系建设
          企业安全 | 企业内一次钓鱼演练准备过程
          内网渗透测试 | Kerberos协议及其部分攻击手法
          0day的产生 | 不懂代码的"代码审计"
          安装scrcpy-client模块av模块异常,环境问题解决方案
          leetcode hot100【LeetCode 279. 完全平方数】java实现
          OpenWrt下安装Mosquitto
          AnatoMask论文汇总
          【AI日记】24.11.01 LangChain、openai api和github copilot
        • 热门文章
        • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
          奉劝各位学弟学妹们,该打造你的技术影响力了!
          五年了,我在 CSDN 的两个一百万。
          Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
          面试官都震惊,你这网络基础可以啊!
          你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
          心情不好的时候,用 Python 画棵樱花树送给自己吧
          通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
          13 万字 C 语言从入门到精通保姆级教程2021 年版
          10行代码集2000张美女图,Python爬虫120例,再上征途
        Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
        正则表达式工具 cron表达式工具 密码生成工具

        京公网安备 11010502049817号