创建具有透明背景的 SVG 动画虚线

时间:2020-12-28 03:41:07

标签: animation svg svg-animate

我已经玩了几个小时并试图想出一个解决方案。

在这个... https://jsfiddle.net/WebFlair/nc0zyjdq/78/

<div class="bg">

<svg version="1.0" xmlns="http://www.w3.org/2000/svg"
 width="400.000000pt" height="400.000000pt" viewBox="0 0 400.000000 400.000000"
 preserveAspectRatio="xMidYMid meet">
<path class="path" fill="none" stroke="#b0225e" stroke-linejoin="round" stroke-width="5" stroke-miterlimit="10" d="M23.742,10.709
    c-2.305,23.611-8.81,46.563-9.021,70.829c-0.252,28.966,22.237,43.666,47.06,55.482c23.642,11.255,42.368,15.766,68.461,16.631
    c19.993,0.663,40.08,2.97,59.853-1.723c23.301-5.531,45.542-17.598,66.978-27.933c19.248-9.281,38.831-21.86,41.946-45.201
    c5.539-41.51-54.993-47.073-81.885-42.17C159.05,47.212,89.37,104.633,77.387,164.629c-5.896,29.522-4.312,60.884,12.703,86.354
    c19.17,28.697,49.512,49.927,78.596,67.591"/>

<path class="dashed" fill="none" stroke="white"  stroke-width="7" stroke-linejoin="round" stroke-miterlimit="16" d="M23.742,10.709
    c-2.305,23.611-8.81,46.563-9.021,70.829c-0.252,28.966,22.237,43.666,47.06,55.482c23.642,11.255,42.368,15.766,68.461,16.631
    c19.993,0.663,40.08,2.97,59.853-1.723c23.301-5.531,45.542-17.598,66.978-27.933c19.248-9.281,38.831-21.86,41.946-45.201
    c5.539-41.51-54.993-47.073-81.885-42.17C159.05,47.212,89.37,104.633,77.387,164.629c-5.896,29.522-4.312,60.884,12.703,86.354
    c19.17,28.697,49.512,49.927,78.596,67.591"/>
</svg>
</div>
<style>
.bg {background:#eee;}
.dashed{
  stroke-dasharray: 14;
}
.path {
  stroke-dasharray: 1000;
  stroke-dashoffset: 1000;
  animation: dash 5s linear alternate forwards;
}

@keyframes dash {
  from {
    stroke-dashoffset: 1000;
  }
  to {
    stroke-dashoffset: 0;
  }
}
</style>

您将看到灰色背景和动画白色和粉红色虚线。

现在我知道这在我的示例中不起作用,但我想要做的是使白色部分透明,以便它可以用于任何背景,保留动画和粉红色虚线。

谁能想出一种方法来实现这一目标?我已经考虑了我能想到的所有内容,并且始终需要白色部分以保持动画。

2 个答案:

答案 0 :(得分:3)

<块引用>

但我想做的是使白色部分透明,以便 它可以用于任何背景,保持动画和粉红色 虚线。

思路如下:使用三层

  1. 第一个底层将有一个白色的路径
  2. 中间层将具有完全相同的粉红色路径
  3. 在顶层会有一个遮罩路径,它会逐渐 揭示粉红色的路径

当“stroke: white”显示其下方的粉红色路径部分时,这将使用遮罩的属性

并且由于掩蔽路径的长度从零变为最大值(stroke-dashoffset 从最大值 917 减小到零),粉红色路径的增长变得可见

@keyframes dash {
  from {
    stroke-dashoffset: 917;
  }
  to {
    stroke-dashoffset: 0;
  }

澄清最大路径长度为917px

1.第一个底层会有一个白色的路径

.bg {background:#eee;}
#dashed  {
  stroke-dasharray: 14;
  fill:none;
  stroke:white;
  stroke-width:7;
  stroke-linejoin:round; 
 
}
<div class="bg">

<svg version="1.0" xmlns="http://www.w3.org/2000/svg"
 width="400.000000pt" height="400.000000pt" viewBox="0 0 400.000000 400.000000"
 preserveAspectRatio="xMidYMid meet">

   <!-- Bottom layer dashed white line -->
<path id="dashed" d="M23.742,10.709
    c-2.305,23.611-8.81,46.563-9.021,70.829c-0.252,28.966,22.237,43.666,47.06,55.482c23.642,11.255,42.368,15.766,68.461,16.631
    c19.993,0.663,40.08,2.97,59.853-1.723c23.301-5.531,45.542-17.598,66.978-27.933c19.248-9.281,38.831-21.86,41.946-45.201
    c5.539-41.51-54.993-47.073-81.885-42.17C159.05,47.212,89.37,104.633,77.387,164.629c-5.896,29.522-4.312,60.884,12.703,86.354
    c19.17,28.697,49.512,49.927,78.596,67.591"/>  
    
    
</svg>
</div>
<script>
  console.log(dashed.getTotalLength());
</script>

2.中间层将有完全相同的粉红色路径

 <style>
.bg {background:#eee;}
#dashed  {
  stroke-dasharray: 14;
  fill:none;
  stroke:white;
  stroke-width:7;
  stroke-linejoin:round; 
 
} 

#pink {
stroke-dasharray: 14;
  fill:none;
  stroke:#b0225e;
  stroke-width:7;
  stroke-linejoin:round; 
  mask:url(#msk);
} 
<div class="bg">

<svg version="1.0" xmlns="http://www.w3.org/2000/svg"
 width="400.000000pt" height="400.000000pt" viewBox="0 0 400.000000 400.000000"
 preserveAspectRatio="xMidYMid meet">


   <!-- Bottom layer dashed white line -->
<path id="dashed" d="M23.742,10.709
    c-2.305,23.611-8.81,46.563-9.021,70.829c-0.252,28.966,22.237,43.666,47.06,55.482c23.642,11.255,42.368,15.766,68.461,16.631
    c19.993,0.663,40.08,2.97,59.853-1.723c23.301-5.531,45.542-17.598,66.978-27.933c19.248-9.281,38.831-21.86,41.946-45.201
    c5.539-41.51-54.993-47.073-81.885-42.17C159.05,47.212,89.37,104.633,77.387,164.629c-5.896,29.522-4.312,60.884,12.703,86.354
    c19.17,28.697,49.512,49.927,78.596,67.591"/>  
      <!-- Middle layer dashed pink line -->
     <path id="pink" d="M23.742,10.709
    c-2.305,23.611-8.81,46.563-9.021,70.829c-0.252,28.966,22.237,43.666,47.06,55.482c23.642,11.255,42.368,15.766,68.461,16.631
    c19.993,0.663,40.08,2.97,59.853-1.723c23.301-5.531,45.542-17.598,66.978-27.933c19.248-9.281,38.831-21.86,41.946-45.201
    c5.539-41.51-54.993-47.073-81.885-42.17C159.05,47.212,89.37,104.633,77.387,164.629c-5.896,29.522-4.312,60.884,12.703,86.354
    c19.17,28.697,49.512,49.927,78.596,67.591"/>

</svg>
</div>

3.顶层会有遮罩路径,会逐渐显出粉色路径

.bg {background:#eee;}
#dashed  {
  stroke-dasharray: 14;
  fill:none;
  stroke:white;
  stroke-width:7;
  stroke-linejoin:round; 
 
} 

#pink {
stroke-dasharray: 14;
  fill:none;
  stroke:#b0225e;
  stroke-width:7;
  stroke-linejoin:round; 
  mask:url(#msk);
} 

#maskLine {
  fill:none;
  stroke:white;
  stroke-width:7;
  stroke-dasharray: 917;
  stroke-dashoffset: 917;
  animation: dash 5s linear alternate forwards;
}

@keyframes dash {
  from {
    stroke-dashoffset: 917;
  }
  to {
    stroke-dashoffset: 0;
  }
}
<div class="bg">

<svg version="1.0" xmlns="http://www.w3.org/2000/svg"
 width="400.000000pt" height="400.000000pt" viewBox="0 0 400.000000 400.000000"
 preserveAspectRatio="xMidYMid meet">
<defs>
  <mask id="msk">  
       <!-- A mask layer that reveals a dashed pink line -->
    <path id="maskLine"  d="M23.742,10.709
    c-2.305,23.611-8.81,46.563-9.021,70.829c-0.252,28.966,22.237,43.666,47.06,55.482c23.642,11.255,42.368,15.766,68.461,16.631
    c19.993,0.663,40.08,2.97,59.853-1.723c23.301-5.531,45.542-17.598,66.978-27.933c19.248-9.281,38.831-21.86,41.946-45.201
    c5.539-41.51-54.993-47.073-81.885-42.17C159.05,47.212,89.37,104.633,77.387,164.629c-5.896,29.522-4.312,60.884,12.703,86.354
    c19.17,28.697,49.512,49.927,78.596,67.591"/>  
  </mask>
</defs>

   <!-- Bottom layer dashed white line -->
<path id="dashed" d="M23.742,10.709
    c-2.305,23.611-8.81,46.563-9.021,70.829c-0.252,28.966,22.237,43.666,47.06,55.482c23.642,11.255,42.368,15.766,68.461,16.631
    c19.993,0.663,40.08,2.97,59.853-1.723c23.301-5.531,45.542-17.598,66.978-27.933c19.248-9.281,38.831-21.86,41.946-45.201
    c5.539-41.51-54.993-47.073-81.885-42.17C159.05,47.212,89.37,104.633,77.387,164.629c-5.896,29.522-4.312,60.884,12.703,86.354
    c19.17,28.697,49.512,49.927,78.596,67.591"/>  
      <!-- Middle layer dashed pink line -->
     <path id="pink" d="M23.742,10.709
    c-2.305,23.611-8.81,46.563-9.021,70.829c-0.252,28.966,22.237,43.666,47.06,55.482c23.642,11.255,42.368,15.766,68.461,16.631
    c19.993,0.663,40.08,2.97,59.853-1.723c23.301-5.531,45.542-17.598,66.978-27.933c19.248-9.281,38.831-21.86,41.946-45.201
    c5.539-41.51-54.993-47.073-81.885-42.17C159.05,47.212,89.37,104.633,77.387,164.629c-5.896,29.522-4.312,60.884,12.703,86.354
    c19.17,28.697,49.512,49.927,78.596,67.591"/>
    
</svg>
</div>

更新

在重复点击“播放”按钮时填充、清除路径

var dash = true;

function play() {
  var path = document.getElementById('maskLine');
  path.style.strokeDashoffset = (dash) ? '0' : '917.00';
  dash = !dash;
} 
.bg {background:#eee;}
#dashed  {
  stroke-dasharray: 14;
  fill:none;
  stroke:white;
  stroke-width:7;
  stroke-linejoin:round; 
 
} 

#pink {
stroke-dasharray: 14;
  fill:none;
  stroke:#b0225e;
  stroke-width:7;
  stroke-linejoin:round; 
  mask:url(#msk);
} 

#maskLine {
  fill:none;
  stroke:white;
  stroke-width:7;
  stroke-dasharray: 917;
  stroke-dashoffset: 917;
  transition: stroke-dashoffset 5s linear;
}
<button onclick="play();">Play</button>
<div class="bg">

<svg version="1.0" xmlns="http://www.w3.org/2000/svg"
 width="400.000000pt" height="400.000000pt" viewBox="0 0 400.000000 400.000000"
 preserveAspectRatio="xMidYMid meet">
<defs>
  <mask id="msk">  
       <!-- A mask layer that reveals a dashed pink line -->
    <path id="maskLine"  d="M23.742,10.709
    c-2.305,23.611-8.81,46.563-9.021,70.829c-0.252,28.966,22.237,43.666,47.06,55.482c23.642,11.255,42.368,15.766,68.461,16.631
    c19.993,0.663,40.08,2.97,59.853-1.723c23.301-5.531,45.542-17.598,66.978-27.933c19.248-9.281,38.831-21.86,41.946-45.201
    c5.539-41.51-54.993-47.073-81.885-42.17C159.05,47.212,89.37,104.633,77.387,164.629c-5.896,29.522-4.312,60.884,12.703,86.354
    c19.17,28.697,49.512,49.927,78.596,67.591">
     </path>
        
     </path>    
  </mask>
</defs>

   <!-- Bottom layer dashed white line -->
<path id="dashed" d="M23.742,10.709
    c-2.305,23.611-8.81,46.563-9.021,70.829c-0.252,28.966,22.237,43.666,47.06,55.482c23.642,11.255,42.368,15.766,68.461,16.631
    c19.993,0.663,40.08,2.97,59.853-1.723c23.301-5.531,45.542-17.598,66.978-27.933c19.248-9.281,38.831-21.86,41.946-45.201
    c5.539-41.51-54.993-47.073-81.885-42.17C159.05,47.212,89.37,104.633,77.387,164.629c-5.896,29.522-4.312,60.884,12.703,86.354
    c19.17,28.697,49.512,49.927,78.596,67.591"/>  
      <!-- Middle layer dashed pink line -->
     <path id="pink" d="M23.742,10.709
    c-2.305,23.611-8.81,46.563-9.021,70.829c-0.252,28.966,22.237,43.666,47.06,55.482c23.642,11.255,42.368,15.766,68.461,16.631
    c19.993,0.663,40.08,2.97,59.853-1.723c23.301-5.531,45.542-17.598,66.978-27.933c19.248-9.281,38.831-21.86,41.946-45.201
    c5.539-41.51-54.993-47.073-81.885-42.17C159.05,47.212,89.37,104.633,77.387,164.629c-5.896,29.522-4.312,60.884,12.703,86.354
    c19.17,28.697,49.512,49.927,78.596,67.591"/>

</svg>
</div>

答案 1 :(得分:2)

您可以使用 mask

.bg {
  background: #eee;
}

.dashed {
  stroke-dasharray: 14;
}

.path {
  stroke-dasharray: 1000;
  stroke-dashoffset: 1000;
  animation: dash 5s linear alternate forwards;
}

@keyframes dash {
  from {
    stroke-dashoffset: 1000;
  }
  to {
    stroke-dashoffset: 0;
  }
}
<div class="bg">
  <svg version="1.0" xmlns="http://www.w3.org/2000/svg" width="400.000000pt" height="400.000000pt" viewBox="0 0 400.000000 400.000000" preserveAspectRatio="xMidYMid meet">
  <defs>
    <path id="dashed" class="dashed" fill="none" stroke="white" stroke-width="7" stroke-linejoin="round" stroke-dasharray="14" stroke-miterlimit="16" d="M23.742,10.709
    c-2.305,23.611-8.81,46.563-9.021,70.829c-0.252,28.966,22.237,43.666,47.06,55.482c23.642,11.255,42.368,15.766,68.461,16.631
    c19.993,0.663,40.08,2.97,59.853-1.723c23.301-5.531,45.542-17.598,66.978-27.933c19.248-9.281,38.831-21.86,41.946-45.201
    c5.539-41.51-54.993-47.073-81.885-42.17C159.05,47.212,89.37,104.633,77.387,164.629c-5.896,29.522-4.312,60.884,12.703,86.354
    c19.17,28.697,49.512,49.927,78.596,67.591"/>
    <mask id="mask">
      <use xlink:href="#dashed"/>
    </mask>
  </defs>
  <path class="path" fill="none" stroke="#b0225e" stroke-linejoin="round" stroke-width="5" stroke-miterlimit="10" mask="url(#mask)" d="M23.742,10.709
    c-2.305,23.611-8.81,46.563-9.021,70.829c-0.252,28.966,22.237,43.666,47.06,55.482c23.642,11.255,42.368,15.766,68.461,16.631
    c19.993,0.663,40.08,2.97,59.853-1.723c23.301-5.531,45.542-17.598,66.978-27.933c19.248-9.281,38.831-21.86,41.946-45.201
    c5.539-41.51-54.993-47.073-81.885-42.17C159.05,47.212,89.37,104.633,77.387,164.629c-5.896,29.522-4.312,60.884,12.703,86.354
    c19.17,28.697,49.512,49.927,78.596,67.591"/>
  </svg>
</div>

您也可以通过重用 path 中定义的 defs 来消除重复的路径声明,但这需要额外的更改,因此,为了不混淆事物,我决定不将其放入我的回答。