【问题标题】:mask SVG path by another path用另一条路径掩盖 SVG 路径
【发布时间】:2018-11-13 20:13:41
【问题描述】:

我有 SVG 路径(实线),我想用其他路径(虚线)掩盖它。我有这个 CSS

        .dashed{
          stroke-dasharray: 5;

        }
        .path {
          stroke-dasharray: 852;
          stroke-dashoffset: 852;
          animation: dash 4s 1s linear forwards;
        }
        @keyframes dash {
          to { 
            stroke-dashoffset: 0; 
          } 
        } 

还有像这样的 SVG

<svg version="1.1" id="Layer_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px" width="612px" height="792px" viewBox="0 0 612 792" enable-background="new 0 0 612 792" xml:space="preserve">
    <g transform="scale(1.7)">
        <path class="path" fill="none" stroke="#e31f1a" stroke-linejoin="round" stroke-miterlimit="10" stroke-width="1.0" d="m 34.161514,227.15068 c 0,0 5.335031,41.13009 -10.338686,53.31427 -13.8827124,10.79193 -26.8802222,-16.84233 0.681739,-19.39738 52.917804,-4.90561 69.13065,91.87642 69.13065,91.87642"/>
    </g>

    <g transform="scale(1.71)">
        <path class="dashed" fill="none" stroke="black" stroke-linejoin="round" stroke-miterlimit="10" stroke-width="1.5" d="m 34.161514,227.15068 c 0,0 5.335031,41.13009 -10.338686,53.31427 -13.8827124,10.79193 -26.8802222,-16.84233 0.681739,-19.39738 52.917804,-4.90561 69.13065,91.87642 69.13065,91.87642"/>
    </g>    
</svg>

路径相同。我不使用虚线的原因是动画(参见 CSS 部分),我想要在其中绘制虚线的效果。

使用蒙版图片(内有虚线)

 mask-image: url(mask.svg);

对我不起作用。你知道路吗?

【问题讨论】:

    标签: animation svg mask


    【解决方案1】:

    您不需要使用mask-image。那是当你想用一个掩码来屏蔽一个 HTML 元素的时候。

    您应该只使用 SVG &lt;mask&gt; 元素。并为其中的路径设置动画。

    .dashed {
      stroke-dasharray: 5;
    }
    
    .path {
      stroke-dasharray: 226;
      stroke-dashoffset: 226;
      animation: dash 4s 1s linear forwards;
    }
    
    @keyframes dash {
      to { 
        stroke-dashoffset: 0; 
      } 
    } 
    <svg version="1.1" id="Layer_1" xmlns="http://www.w3.org/2000/svg" width="612px" height="792px" viewBox="0 0 612 792">
      <defs>
        <mask id="mymask">
          <path class="path" fill="none" stroke="#fff" stroke-width="2"
                d="m 34.161514,227.15068 c 0,0 5.335031,41.13009 -10.338686,53.31427 -13.8827124,10.79193 -26.8802222,-16.84233 0.681739,-19.39738 52.917804,-4.90561 69.13065,91.87642 69.13065,91.87642"/>
        </mask>
      </defs>
    
      <g transform="scale(1.71)">
        <path class="dashed" fill="none" stroke="black" stroke-linejoin="round" stroke-miterlimit="10" stroke-width="1.5"
              mask="url(#mymask)"
              d="m 34.161514,227.15068 c 0,0 5.335031,41.13009 -10.338686,53.31427 -13.8827124,10.79193 -26.8802222,-16.84233 0.681739,-19.39738 52.917804,-4.90561 69.13065,91.87642 69.13065,91.87642"/>
      </g>    
    </svg>

    【讨论】:

    猜你喜欢
    • 2015-06-28
    • 2020-08-24
    • 2019-10-09
    • 2012-04-19
    • 1970-01-01
    • 2011-12-26
    • 2019-04-14
    • 1970-01-01
    相关资源
    最近更新 更多