SVG霓虹灯特效之stroke-dasharray与stroke-dashoffset详解

最近的学习中,对于svg实现的文字霓虹灯动画感到很是有趣,而且实现起来也相对着来说比较容易,效果也不错,比如下图。

需要实现以上的效果,我们需要着重了解两个svg的属性 stroke-dasharraystroke-dashoffset

stroke-dasharray

官方解释为It's a list of comma and/or white space separated lengths and percentages that specify the lengths of alternating dashes and gaps. If an odd number of values is provided, then the list of values is repeated to yield an even number of values. Thus, 5,3,2 is equivalent to 5,3,2,5,3,2.;--MDN

翻译过来就是它是一个用逗号或者空白隔开的length和percentage数列,length指定了短划线的长度,percentage指定了缺口的长度。如果提供了奇数个值,则这个值的数列重复一次,从而变成偶数个值。所以,如果为4,等同于4, 4 ;5,3,2等同于5,3,2,5,3,2;如下面的示例所示。

<svg viewBox="0 0 30 15" xmlns="http://www.w3.org/2000/svg">
        <!-- 当没有两个属性时 -->
        <line x1="0" y1="1" x2="30" y2="1" stroke="black" />
       
        <!-- 当两个属性相等时,平分 -->
        <line x1="0" y1="3" x2="30" y2="3" stroke="black"
                stroke-dasharray="4" />
       
        <!-- 当两个属性不相同时-->
        <line x1="0" y1="5" x2="30" y2="5" stroke="black"
                stroke-dasharray="4 1" />
          
        <!-- 当属性个数为奇数时,4 1 2 会变为 4 1 2 4 1 2 这样的排布顺序 这些数值
            会按短划线长度 缺口的长度这样子排布下去 -->
        <line x1="0" y1="7" x2="30" y2="7" stroke="black"
                stroke-dasharray="4 1 2" />
       
        <!-- 当属性个数为偶数时,会按4 1 2 3 4 1 2 3 排布 -->
        <line x1="0" y1="9" x2="30" y2=
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值