提问者:小点点

与SVG一起使用的JQuery函数,在第一个动画完成后立即播放第二个动画,以获得更流畅的手写效果


我应用了动画延迟技术,但是要实现手写效果动画,我必须在物体相交的地方剪切,并绘制路径,然后将其转换为裁剪蒙版,例如将字母“W”分成4个部分,并为上面所述的不同部分绘制不同路径,然后将其动画化,以给出干净的手写效果,给路径一个笔画宽度,由于字体太复杂,花费了我很多时间,您可以在这里查看笔。 https://codepen.io/toxifiedm/pen/mdvejxw

目前我正在实现所说的动画使用动画延迟css功能。

#W2-Path {
  animation-delay:.5s
}
#W3-Path {
  animation-delay:1s
}
#W4-Path {
  animation-delay:1.5s
}
#O-Path {
  animation-delay:2s
}
#R1-Path-2 {
  animation-delay:3.5s
}
#R2-Path-2 {
  animation-delay:4s
}
#R3-Path-2 {
  animation-delay:4.5s
}
#L1-Path-2 {
  animation-delay:5s
}
#L2-Path-2 {
  animation-delay:5.5s
}
#L3-Path-2 {
  animation-delay:6s
}
#D1-Path {
  animation-delay:6.5s
}
#D2-Path {
  animation-delay:7s
}

我在想,如果有任何其他的方式,我可以开始第二个动画,因为第一个动画完成,它只是继续给动画更流畅的效果和完美的时机,实现jQuery?


共1个答案

匿名用户

我只回答后一个问题

我已经更改了svg代码,因为它可以用更简单的方式完成。

您将每一行包装成两组。 我只是用台词。 您还使用了style=“clip-path:url(#clip-path)”,我已经将其转换为如下表示属性:clip-path=“url(#clip-path)”,因为在javascript中,我希望将animation-delay设置为行的内联样式。

另一个变化是:我使用stroke-dasharray:8stroke-dashoffset:8,因为8是字母W的行长。我知道行长,因为我使用了getTotAlLength()方法。

由于字母的行具有相同的样式,因此仅为组设置一次笔画笔画宽度

javascript选择一组中的所有行,然后循环遍历这些行并设置animation-delay:${i*1}s,其中i是第n行。 请注意,组中的行的顺序是从第一行到第四行,而不是相反,因为您的代码中有这些行。

null

let Wls = document.querySelector("#W").querySelectorAll("line");//all the lines in the W group

Wls.forEach((l,i)=>{
// for each line in the W group
// set the style attribute
  l.setAttribute("style", `animation-delay:${i*1}s`)
})
body {
	background: white;
}

svg {
  width: 90vh; 
  border:solid;
}


#W line{
  stroke-dasharray: 8;
  stroke-dashoffset: 8;
  animation: letter-animation 1s linear forwards;
}


@keyframes letter-animation {
    0% {
        stroke-dashoffset: 8;
    }
    100% {
        stroke-dashoffset: 0;
    }
}
<svg id="WOYP" viewBox="0 0 9 9">
  <defs>
    <clipPath id="clip-path-44" transform="translate(-5.561 -10.291)">
      <path id="W4" d="M11.676,16.41l.234.578c.236-.631.477-1.261.715-1.891q.222-.6.449-1.188t.409-1.063q.181-.476.308-.8c.084-.214.136-.348.156-.4s.05-.12.066-.16a.594.594,0,0,1,.061-.111.754.754,0,0,1,.086-.1.768.768,0,0,1,.151-.11h-1.03c.121.053.192.117.212.19a.481.481,0,0,1-.04.291c0,.007-.025.079-.076.216s-.118.319-.2.546-.18.483-.287.767-.216.573-.323.867Z" />
    </clipPath>
    <clipPath id="clip-path-45" transform="translate(-5.561 -10.291)">
      <path id="W3" d="M11.675,16.358Zm0,0h0l.011.029h0l.232.575c-.152.4-.311.82-.474,1.252L10.176,15.07q-.242-.6-.478-1.183t-.433-1.058q-.2-.474-.333-.793c-.09-.213-.146-.343-.166-.389a1.8,1.8,0,0,0-.176-.27.774.774,0,0,0-.348-.209h1.833a.3.3,0,0,0-.221.239.9.9,0,0,0,.03.35c0,.006.027.076.08.209s.123.308.207.524.179.464.287.744.218.562.332.848Q11.179,15.089,11.675,16.358Z" />
    </clipPath>
    <clipPath id="clip-path-46" transform="translate(-5.561 -10.291)">
      <path id="W2" d="M9.139,16.411l.234.578c.236-.632.477-1.261.715-1.891q.222-.6.45-1.189t.408-1.062c.122-.318.224-.584.308-.8s.137-.347.157-.4l.065-.16a.556.556,0,0,1,.061-.11.7.7,0,0,1,.086-.1.8.8,0,0,1,.151-.11h-1.03c.121.054.192.117.213.191a.488.488,0,0,1-.041.29c0,.007-.025.079-.076.216s-.117.319-.2.546-.179.483-.287.768-.216.573-.323.867Z" />
    </clipPath>
    <clipPath id="clip-path-47" transform="translate(-5.561 -10.291)">
      <path id="W1" d="M9.138,16.358Zm0,0h0l.012.029h0l.233.575q-.229.6-.475,1.252L7.639,15.07q-.242-.6-.478-1.183t-.433-1.058q-.2-.474-.332-.793l-.166-.389a1.8,1.8,0,0,0-.177-.27.764.764,0,0,0-.347-.209H7.539a.305.305,0,0,0-.222.239.938.938,0,0,0,.03.35c0,.006.027.076.081.209s.122.308.206.524.18.464.287.744.218.562.332.848Q8.642,15.089,9.138,16.358Z" />
    </clipPath>
  </defs>

  <g id="W" stroke="#003668" stroke-width="2">
    <line x1="0.93" y1="0.482" x2="3.873" y2="7.937" clip-path="url(#clip-path-47)" />
    <line x1="3.088" y1="7.937" x2="5.966" y2="0.482" clip-path="url(#clip-path-46)" />
    <line x1="3.481" y1="0.482" x2="6.424" y2="7.937" clip-path="url(#clip-path-45)" />
    <line x1="5.639" y1="7.937" x2="8.517" y2="0.482" clip-path="url(#clip-path-44)" />
  </g>

</svg>