一文了解svg之stroke属性
属性
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-dasharray和stroke-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属性的更多相关文章
- SVG 学习<二>进阶 SVG世界,视野,视窗 stroke属性 svg分组
目录 SVG 学习<一>基础图形及线段 SVG 学习<二>进阶 SVG世界,视野,视窗 stroke属性 svg分组 SVG 学习<三>渐变 SVG 学习<四 ...
- 学习SVG系列(3):SVG Stroke属性
SVG stroke 属性 1.stroke 2.stroke-width 3.stroke-linecap 4.stroke-dasharray 5.stroke-opacity 6.stroke- ...
- SVG Stroke属性
一.stroke属性介绍 SVG提供了一个范围广泛stroke属性,用于描述轮廓,其中包括 stroke 指定颜色 stroke-width 指定宽度 stroke-linecap 指定端点样式 st ...
- SVG DOM常用属性和方法介绍
将以Adobe SVG Viewer提供的属性和方法为准,因为不同解析器对JavaScript以及相关的属性和方法支持的程度不同,有些方法和属性是某个解析器所特有的.SVG支持DOM2标准. 12.2 ...
- SVG DOM常用属性和方法介绍(1)
12.2 SVG DOM常用属性和方法介绍 将以Adobe SVG Viewer提供的属性和方法为准,因为不同解析器对JavaScript以及相关的属性和方法支持的程度不同,有些方法和属性是某个解析 ...
- Art-Directing SVG图像viewBox属性
Art-Directing SVG图像viewBox属性 作者:彦子 日期:2015-06-02 点击:992 svg 译者注:根据Google Dev文档的解释,Art Direction在这篇文章 ...
- 4、前端--浮动、定位、是否脱离文档流、溢出属性、z-index、透明度、JavaScript简介
浮动 # ps:html代码时没有缩进一说的 全部写在一行也可以 """浮动主要就是用于页面布局的!!!""" # 浮动带来的负面影响 &q ...
- [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 ...
- [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 ...
- [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 ...
随机推荐
- P2910
#include<iostream> #include<utility> #include<vector> using namespace std; typedef ...
- Apache基于IP和端口
Apache基于IP 步骤1:添加并配置虚拟网卡 添加虚拟网卡:通常在虚拟机环境中,可以通过虚拟机软件(如VMware或VirtualBox)的网络设置来添加额外的网络适配器. 配置IP地址:编辑/e ...
- 判断浏览器是否是 IE 及 IE8 以下版本
作为一个前端,避免不了会遇见IE的坑,其他浏览器都好好的,测到IE就完蛋,各种不支持,服气了 有些属性和方法是所有版本IE都不支持,而有些则是部分支持,在项目中能够,主要分界岭为IE8,我相信目前大部 ...
- oeasy 教您玩转linux 之 010209 装酷利器 hollywood
我们来回顾一下 上一部分我们都讲了什么? 屏幕故障风格的软件包bb 可以设置音频 这次装一个酷 下个hollywood软件包 apt show hollywood apt search hollywo ...
- 第九节 JMeter基础-高级登录【接口关联-鉴权】
声明:本文所记录的仅本次操作学习到的知识点,其中商城IP错误,请自行更改. 背景:电商的功能:登录.加入购物车.提交订单.问题:谁把什么商品加入了购物车?这时需要把上一个接口的响应数据(登录成功后返回 ...
- odoo Actions学习总结
环境 odoo-14.0.post20221212.tar Actions(动作) action定义系统响应用户操作的行为:登录.操作按钮.选择发票等- action可以存储在数据库中,也可以作为字典 ...
- VUE系列---深度解析 Vue 优化策略
在前端开发中,性能优化一直是一个重要的课题.Vue.js 提供了多种优化策略,帮助开发者构建高性能的应用.本文将深入解析以下几个优化策略: 使用 v-once.v-if 和 v-show 的区别和优化 ...
- AI的发展需要有应用和落地场景 —— 李开复:传统公司看不懂技术,大模型落地B端阻碍多
引自:https://baijiahao.baidu.com/s?id=1801826206644007472&wfr=spider&for=pc "我们投了七八家机器人企业 ...
- 读论文《基于 GA - BP 的汽车行李箱盖内板冲压成形工艺优化》 —— 如何使用AI技术优化模具产业中工件冲压工艺
最近到了模具公司工作,本来以为身边同事对模具生产和工件生产的流程(大致流程)会比较了解,结果一问才知道基本都是一问三不知,大家都在模具公司工作但是貌似很多人干的和模具生产和工件制造的工作关联性并不强, ...
- 如何将python的pip源设置为阿里云
为python的pip源设置为阿里云,pip源的设置操作: pip config set global.index-url https://mirrors.cloud.aliyuncs.com/pyp ...