账号密码登录
微信安全登录
微信扫描二维码登录

登录后绑定QQ、微信即可实现信息互通

手机验证码登录
找回密码返回
邮箱找回 手机找回
注册账号返回
其他登录方式
分享
  • 收藏
    X
    关于svg中stroke-dashoffset的一个问题
    57
    0
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Document</title>
        <style>
        .svgContent {
          border: 2px solid #ddd;
          background: #ddd;
          border-radius: 5px;
          width: 50%;
          margin: auto;
          height: 40px;
          line-height: 40px;
          padding-top: 20px;
        }
        .lineAnimation {
          -webkit-animation: testMove 3s linear infinite;
        }
        @-webkit-keyframes testMove {
          from {
            stroke-dashoffset: 100%
          }
          to {
            stroke-dashoffset: 0
          }
        }
        </style>
    </head>
    <body>
        <div class="svgContent">
          <svg viewBox="0 0 300 10">
              <line class="lineAnimation" x1="0" y1="0" x2="300" y2="0" 
              stroke="black" stroke-width="10" stroke-dasharray="40,10" stroke-dashoffset="" />
          </svg>
        </div>
    </body>
    </html>

    不是很理解stroke-dashoffset:100% ----> 0这个变化过程
    好像是因为100% 和 0%的时候不重合,动画在重复执行的时候,出现了闪动的情况。
    问题来源:在使用leaflet-ant-path这个插件的时候发现了这个问题,查看源码发现动画效果就是改变stroke-offset:100% ---> 0
    leaflet-ant-path官网地址:https://rubenspgcavalcante.gi...
    官网给出的效果图同样也有这个问题。。。

    1
    打赏
    收藏
    点击回答
        全部回答
    • 0
    • 給理想一點時間。 普通会员 1楼

      在SVG中,stroke-dashoffset是一个属性,用于指定当绘制的线条有多个宽度和厚度时,每个宽度和厚度之间的差距。这个值告诉浏览器在每个宽度和厚度之间如何确定线条的长度。

      stroke-dashoffset的单位通常是像素。如果你想将其设置为百分比,你可以将其乘以100。

      例如,如果你想为每个宽度和厚度之间的差距设置一个半像素的间隔,你可以这样做:

      svg <svg width="100" height="100"> <path d="M10 10 V0h10"/> <path d="M10 0 H10"/> <path d="M10 10 L0 10" stroke-dashoffset="0.5"/> <path d="M10 10 L10 0" stroke-dashoffset="1"/> </svg>

      在这个例子中,第一个和第二个路径将形成一个宽度为10的直线,而第三个路径将形成一个宽度为50的直线。由于stroke-dashoffset为0.5,所以第三个路径的宽度和第一个路径的宽度之间的差距为0.5像素。

      注意,stroke-dashoffset可能不会总是正确的,特别是当两个路径的宽度和厚度非常接近时。例如,如果第一个路径的宽度和厚度都非常接近,那么第二个路径的宽度和厚度之间的差距可能会非常小,导致stroke-dashoffset为负。在这种情况下,你可以使用一些额外的技巧,如使用arcsine-bezier曲线或使用双线性插值,来创建更精确的线宽和厚度。

    更多回答
    扫一扫访问手机版
    • 回到顶部
    • 回到顶部