属性

stroke-width

SVG具有stroke-width定义笔触宽度的CSS属性。

<svg width="500" height="120">
<circle cx="50" cy="50" r="25" style="stroke: #000066; fill: none;stroke-width: 1px;" />
<circle cx="150" cy="50" r="25" style="stroke: #000066; fill: none;stroke-width: 3px;" />
<circle cx="250" cy="50" r="25" style="stroke: #000066; fill: none;stroke-width: 6px;" />
<circle cx="350" cy="50" r="25" style="stroke: #000066; fill: none;stroke-width: 12px;" />
</svg>

效果如图:

stroke-linecap(描边线帽)

strokelinecap属性定义不同类型的开放路径的终结。

<svg >
<g fill="none" stroke="black" stroke-width="6">
<path stroke-linecap="butt" d="M5 20 l215 0" />
<path stroke-linecap="round" d="M5 40 l215 0" />
<path stroke-linecap="square" d="M5 60 l215 0" />
</g>
</svg>

效果如图:

stroke-linejoin

该CSS属性stroke-linejoin, 定义如何在一个形状两条线之间的连接被渲染。该CSS属性stroke-linejoin可以采用三个值中的一个。值:

  • miter
  • round
  • bevel
<svg width="500" height="120">
<path d="M20,100 l20,-50 l20,50" style="stroke: #FF0000; fill:none;stroke-width:16px;stroke-linejoin: miter;"></path>
<text x="22" y="20">miter</text> <path d="M120,100 l20,-50 l20,50" style="stroke: #FF0000; fill:none;stroke-width:16px;stroke-linejoin: round;"></path>
<text x="122" y="20">round</text> <path d="M220,100 l20,-50 l20,50" style="stroke: #FF0000; fill:none;stroke-width:16px;stroke-linejoin: bevel;"></path>
<text x="222" y="20">bevel</text>
</svg>

效果如图:

stroke-miterlimit

style样式中stroke-miterlimit属性与stroke-linejoin一起使用。

如果stroke-linejoin设置为斜接,则stroke-miterlimit可以使用来限制两条线相交的点(线角(角)延伸)之间的距离。

<svg width="500" height="120">
<path d="M20,100 l20,-50 l20,50" style="stroke: #000000;fill:none;stroke-width:16px;stroke-linejoin: miter; stroke-miterlimit: 1.0;"></path>
<text x="29" y="20">1.0</text> <path d="M120,100 l20,-50 l20,50" style="stroke: #000000;fill:none;stroke-width:16px;stroke-linejoin: miter;stroke-miterlimit: 2.0;"></path>
<text x="129" y="20">2.0</text> <path d="M220,100 l20,-50 l20,50" style="stroke: #000000;fill:none;stroke-width:16px;stroke-linejoin: miter;stroke-miterlimit: 4.0;"></path>
<text x="229" y="20">4.0</text>
</svg>

效果如图:

stroke-dasharray

SVG CSS属性stroke-dasharray用于绘制以虚线呈现的SVG形状的笔触。之所以称为“破折号数组”,是因为您提供了一个数字数组作为值。这些值定义破折号和空格的长度。

<svg width="500" height="120">
<line x1="20" y1="20" x2="120" y2="20" style="stroke: #000000; fill:none;stroke-width: 6px;stroke-dasharray: 10 5" />
</svg>

定义了一个带有虚线的笔划,虚线部分的宽度为10像素,虚线之间的间隔为5像素。

效果如图:

带有不同破折号和空格宽度的:

<svg width="500" height="120">
<line x1="20" y1="20" x2="120" y2="20" style="stroke: #000000; fill:none;stroke-width: 6px;stroke-dasharray: 10 5 5 5"></line>
<line x1="20" y1="40" x2="120" y2="40" style="stroke: #000000; fill:none;stroke-width: 6px;stroke-dasharray: 10 5 5 10"></line>
</svg>

第一行以10的虚线宽度开始,然后是5像素的间距,然后是5像素的虚线,然后是5像素的另一间距。然后重复该模式。

第二行以虚线宽度10开始,然后是5像素的间距,然后是5像素的虚线,最后是10像素的间距。

效果如图:

stroke-dashoffset

offset:偏移的意思。这个属性是相对于起始点的偏移,正数偏移x值的时候,相当于往左移动了x个长度单位,负数偏移x的时候,相当于往右移动了x个长度单位。

需要注意的是,不管偏移的方向是哪边,要记得dasharray是循环的,也就是 虚线-间隔-虚线-间隔。

<svg width="500" height="120">
<line x1="20" y1="20" x2="120" y2="20" style="stroke: #000000; fill:none;stroke-width: 6px;stroke-dasharray: 10 5" />
<line x1="20" y1="40" x2="120" y2="40" style="stroke: #000000; fill:none;stroke-width: 6px;stroke-dasharray: 10 5" stroke-dashoffset="3"/>
<line x1="20" y1="60" x2="120" y2="60" style="stroke: #000000; fill:none;stroke-width: 6px;stroke-dasharray: 10 5" stroke-dashoffset="-3"/>
</svg>

效果如图:

stroke-opacity

SVG CSS属性stroke-opacity用于定义SVG形状轮廓的不透明度。该值越接近1,则笔划越不透明。默认stroke-opacity值为1,表示笔划完全不透明。

<svg width="500" height="120">
<text x="22" y="40">Text Behind Shape</text>
<path d="M20,40 l50,0" style="stroke: #00ff00;fill:none;stroke-width:16px;stroke-opacity: 0.3;"></path>
<path d="M80,40 l50,0" style="stroke: #00ff00;fill:none;stroke-width:16px;stroke-opacity: 0.7;"></path>
<path d="M140,40 l50,0" style="stroke: #00ff00;fill:none;stroke-width:16px;stroke-opacity: 1;"></path>
</svg>

效果如图:

实例效果

线段从无到有,由短变长

第一步,设置stroke-dasharray虚线长度等于当前svg的宽度,间隔大于或者等于svg宽度

<svg >
  <line id="line" x1="30" y1="30" x2="300" y2="30" stroke-width="20" stroke="red" stroke-dasharray="300,320"/>
</svg>

这时可视区域内只能看到一段虚线,如图:

第二步,设置stroke左移300单位(stroke-dashoffset:300),也就是刚好隐藏了虚线,可视区域内变成了320单位的间隔。

<svg >
  <line id="line" x1="30" y1="30" x2="300" y2="30" stroke-width="20" stroke="red" stroke-dasharray="300,320" stroke-dashoffset="300"/>
</svg>

第三步,当鼠标移入的时候,使offset由300变成0,就实现了动图中的效果

svg:hover #line{
stroke-dashoffset: 0;
}
#line{
transition: all 2s;
}

圆形环绕一圈效果

鼠标hover的时候,外层线段绕自身一圈。这个动画的实现原理跟上面一样,设置stroke-dasharray虚线长度等于当前圆的周长,间隔大于或者等于圆的周长。

第一步,画圆,圆的半径50。

<svg  width="200" height="200" viewBox="0 0 200 200">
<circle id="circle" cx="100" cy="80" r="50" fill="gray" stroke-width="5" stroke="green" />
</svg>

第二步,设置圆的stroke-dasharraystroke-dashoffset,为圆的周长。

#circle{
transition: all 2s;
stroke-dasharray:314,314;
stroke-dashoffset:314;
}

第三步,hover的时候,设置stroke-dashoffset为0,注意圆的stroke起始位置为右侧中间

svg:hover #circle{
stroke-dashoffset:0;
}

一文了解svg之stroke属性的更多相关文章

  1. SVG 学习<二>进阶 SVG世界,视野,视窗 stroke属性 svg分组

    目录 SVG 学习<一>基础图形及线段 SVG 学习<二>进阶 SVG世界,视野,视窗 stroke属性 svg分组 SVG 学习<三>渐变 SVG 学习<四 ...

  2. 学习SVG系列(3):SVG Stroke属性

    SVG stroke 属性 1.stroke 2.stroke-width 3.stroke-linecap 4.stroke-dasharray 5.stroke-opacity 6.stroke- ...

  3. SVG Stroke属性

    一.stroke属性介绍 SVG提供了一个范围广泛stroke属性,用于描述轮廓,其中包括 stroke 指定颜色 stroke-width 指定宽度 stroke-linecap 指定端点样式 st ...

  4. SVG DOM常用属性和方法介绍

    将以Adobe SVG Viewer提供的属性和方法为准,因为不同解析器对JavaScript以及相关的属性和方法支持的程度不同,有些方法和属性是某个解析器所特有的.SVG支持DOM2标准. 12.2 ...

  5. SVG DOM常用属性和方法介绍(1)

    12.2  SVG DOM常用属性和方法介绍 将以Adobe SVG Viewer提供的属性和方法为准,因为不同解析器对JavaScript以及相关的属性和方法支持的程度不同,有些方法和属性是某个解析 ...

  6. Art-Directing SVG图像viewBox属性

    Art-Directing SVG图像viewBox属性 作者:彦子 日期:2015-06-02 点击:992 svg 译者注:根据Google Dev文档的解释,Art Direction在这篇文章 ...

  7. 4、前端--浮动、定位、是否脱离文档流、溢出属性、z-index、透明度、JavaScript简介

    浮动 # ps:html代码时没有缩进一说的 全部写在一行也可以 """浮动主要就是用于页面布局的!!!""" # 浮动带来的负面影响 &q ...

  8. [javascript svg fill stroke stroke-width circle 属性讲解] svg fill stroke stroke-width circle 属性 绘制圆形及引入方式讲解

    <!DOCTYPE html> <html lang='zh-cn'> <head> <title>Insert you title</title ...

  9. [javascript svg fill stroke stroke-width points polygon属性讲解] svg fill stroke stroke-width points polygon绘制多边形属性并且演示polyline和polygon区别讲解

    <!DOCTYPE html> <html lang='zh-cn'> <head> <title>Insert you title</title ...

  10. [javascript svg fill stroke stroke-width points polyline 属性讲解] svg fill stroke stroke-width points polyline 绘制折线属性讲解

    <!DOCTYPE html> <html lang='zh-cn'> <head> <title>Insert you title</title ...

随机推荐

  1. 实用!一键生成数据库文档的神器,支持MySQL/SqlServer/Oracle多种数据库

    Screw(螺丝钉)是一款简洁好用的数据库表结构文档生成工具,它的特点是:简洁.轻量.设计良好.多数据库支持.多种格式文档.灵活扩展以及支持自定义模板,对于有经常要进行数据库设计.评审.文档整理等需求 ...

  2. 记一次 redis 事件注册不当导致的内存泄露

    线上的程序跑着跑着内存越来越大,并且没有下降的趋势,重启一下程序也只能短暂恢复.通过 htop 命令再按一下 M 键按内存占用大小排个序,程序会占好几个G.那好,让我们来分析一下. 收集dump 通过 ...

  3. 改善中国打开GitHub网站的速度

    您可以采取以下措施来改善您在中国打开GitHub网站的速度:1. 使用VPN:通过连接到VPN服务器,您可以避免中国政府对GitHub网站进行的封锁,从而获得更快的访问速度.2. 使用加速器:国内有很 ...

  4. java小技巧~修改对象的属性名

    今天联调的时候,有个功能是在初始化的时候将图片路径回显到vant组件的上传组件上,但是vant组件需要图片路径名叫url,而后端返给我的路径名叫filePath,而且是双层嵌套.一个个遍历老麻烦了,下 ...

  5. rtmp流程解析

    如果rtmp推流地址:rtmp://服务器地址:rtmp端口/路径/名称对应的websocket地址:ws://服务器地址:websocket端口/路径/名称.flv举例:live作为路径,s作为流名 ...

  6. Bulk RNA-seq 基本分析流程

    目的: 对illumina数据进行处理,利用 RNA-Seq 发现新的 RNA 变体和剪接位点,或量化 mRNA 以进行基因表达分析等.对两组或多组样本的转录组数据,通过差异表达分析和对所发现的差异表 ...

  7. ubuntu23.04/22.04下安装docker engine

    官方网址: https://docs.docker.com/engine/install/ubuntu/ 2023年12月1日更新 -- Ubuntu 23.04 # Add Docker's off ...

  8. .gitignore文件的使用方法(学习总结版)—— .gitignore 文件的配合用法

    本文紧接前文: .gitignore文件的使用方法(学习总结版) ============================================= 本文主要讨论前文中所说的一个操作,即: . ...

  9. [简单] 树上的dfs & bfs_洛谷P5908 猫猫和企鹅

    题目链接https://www.luogu.com.cn/problem/P5908 题目大意: \[\begin{align*} & 给定n个点构成一颗树 每条边val=1\\ & ...

  10. Spring AI 更新:支持OpenAI的结构化输出,增强对JSON响应的支持

    就在昨晚,Spring AI发了个比较重要的更新.由于最近OpenAI推出了结构化输出的功能,可确保 AI 生成的响应严格遵守预定义的 JSON 模式.此功能显着提高了人工智能生成内容在现实应用中的可 ...