一篇文章带你了解SVG stroke属性

开发 前端
本文基于Html基础,介绍了stoke属性。添加不一样的属性实现不同的效果,对于每一种属性进行详细的讲解通过丰富的案例分析,希望能够帮助你更好的学习。

[[383060]]

stroke属性定义了给定图形元素的外轮廓的颜色。它的默认值是none。

一、属性

1. stroke-width

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

例:

(这是四个不同的示例stroke-width)

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

代码解析:

将笔划宽度设置为3个像素。您可以使用不同于像素的单位。在[SVG坐标系统单位中查看所有可用单位。

运行后图像效果:

2. stroke-linecap(描边线帽)

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

  1. <svg xmlns="http://www.w3.org/2000/svg" version="1.1">  
  2. <g fill="none" stroke="black" stroke-width="6">    
  3. <path stroke-linecap="butt" d="M5 20 l215 0" />    
  4. <path stroke-linecap="round" d="M5 40 l215 0" />    
  5. <path stroke-linecap="square" d="M5 60 l215 0" />  
  6.      </g> 
  7. </svg> 

 

3. stroke-linejoin

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

案例:

stroke-linejoin,说明了这些不同的值。

  1. <svg width="500" height="120"
  2. <path d="M20,100 l20,-50 l20,50" style="stroke: #FF0000;   fill:none;stroke-width:16px;stroke-linejoin: miter;"></path> 
  3. <text x="22" y="20">miter</text> 
  4.  
  5. <path d="M120,100 l20,-50 l20,50" style="stroke: #FF0000;   fill:none;stroke-width:16px;stroke-linejoin: round;"></path> 
  6. <text x="122" y="20">round</text> 
  7.  
  8. <path d="M220,100 l20,-50 l20,50" style="stroke: #FF0000;   fill:none;stroke-width:16px;stroke-linejoin: bevel;"></path> 
  9. <text x="222" y="20">bevel</text> 
  10. </svg> 

 

4. stroke-miterlimit

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

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

  1. <svg width="500" height="120"
  2. <path d="M20,100 l20,-50 l20,50" style="stroke: #000000;   fill:none;stroke-width:16px; 
  3.            stroke-linejoin: miter; stroke-miterlimit: 1.0; 
  4.             "></path> 
  5. <text x="29" y="20">1.0</text> 
  6. <path d="M120,100 l20,-50 l20,50" style="stroke: #000000;   fill:none; 
  7.             stroke-width:16px; 
  8.             stroke-linejoin: miter;             stroke-miterlimit: 2.0; 
  9.             "></path> 
  10. <text x="129" y="20">2.0</text> 
  11. <path d="M220,100 l20,-50 l20,50" style="stroke: #000000;   fill:none; 
  12.             stroke-width:16px; 
  13.             stroke-linejoin: miter;             stroke-miterlimit: 4.0; 
  14.             "></path> 
  15. <text x="229" y="20">4.0</text> 
  16. </svg> 

注意

stroke-miterlimit,三个路径如何使用三个不同的值,否则它们看起来几乎相同。

运行后图像效果:


5. stroke-dasharray

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

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

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

运行后图像效果:


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

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

 运行后图像效果:

代码解析:

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

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

6. stroke-opacity

SVG CSS属性stroke-opacity用于定义SVG形状轮廓的不透明度。stroke-opacity取0和1之间的十进制数越接近0的值,越透明行程。该值越接近1,则笔划越不透明。默认stroke-opacity值为1,表示笔划完全不透明。

案例中,显示了三行带有不同stroke-opacity文本顶部的行 。

  1. <svg width="500" height="120"
  2. <text x="22" y="40">Text Behind Shape</text> 
  3.  
  4. <path d="M20,40 l50,0" style="stroke: #00ff00;   fill:none; 
  5.                 stroke-width:16px; 
  6.                 stroke-opacity: 0.3; 
  7.                 "></path> 
  8.  
  9. <path d="M80,40 l50,0" style="stroke: #00ff00; fill:none; stroke-width:16px; 
  10.         stroke-opacity: 0.7; 
  11.                 "></path> 
  12.  
  13. <path d="M140,40 l50,0" style="stroke: #00ff00; fill:none;stroke-width:16px; 
  14.                 stroke-opacity: 1; 
  15.                 "></path> 
  16. </svg> 

 运行效果:


注意:

靠后文本越来越不可见。

二、总结

本文基于Html基础,介绍了stoke属性。添加不一样的属性实现不同的效果,对于每一种属性进行详细的讲解通过丰富的案例分析,希望能够帮助你更好的学习。

欢迎大家积极尝试,有时候看到别人实现起来很简单,但是到自己动手实现的时候,总会有各种各样的问题,切勿眼高手低,勤动手,才可以理解的更加深刻。

 

责任编辑:姜华 来源: 前端进阶学习交流
相关推荐

2021-02-05 18:36:15

SVG形状属性

2020-12-08 08:09:49

SVG图标Web

2021-02-26 20:01:57

SVG滤镜元素

2020-12-25 09:42:51

SVGtspanSVG基础

2020-12-15 08:15:34

SVG元素路径

2022-05-13 16:21:38

javascrip脚本SVG

2021-03-26 09:57:51

SVGHtml基础SVG图像

2021-01-04 10:14:42

SVG标签元素

2020-11-10 10:48:10

JavaScript属性对象

2020-12-29 09:39:38

元素属性定位

2020-12-23 08:12:08

javascriptSVG脚本SVG元素

2021-01-01 09:18:48

SVG图像元素

2020-12-11 08:39:14

SVG代码剪切

2023-07-30 15:18:54

JavaScript属性

2020-12-04 08:40:29

SVG动画元素

2023-05-12 08:19:12

Netty程序框架

2021-06-30 00:20:12

Hangfire.NET平台

2023-08-01 14:34:12

HTMLCSS

2022-03-04 09:31:41

CSS前端属性选择器

2023-06-28 15:04:59

CSSHTML
点赞
收藏

51CTO技术栈公众号