SVG path changes after animation

人盡茶涼 提交于 2021-01-27 12:20:41

问题


I'm drawing in a <path> which takes the form of a rectangle, with the following code:

<svg version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px"
 viewBox="0 0 70 51" enable-background="new 0 0 70 51" xml:space="preserve">
      <path fill="#FFFFFF" stroke="#000" stroke-miterlimit="10" d="M18.8,50.5h-7.9V29.7h7.9V50.5z"/>
</svg> 

With the following CSS code to animate it using this method:

svg {
  max-width: 200px;
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  margin: auto; }

 svg path {
    animation: draw 1s linear forwards;
    -webkit-animation: draw 1s linear forwards;
    stroke-dasharray: 57.4;
    stroke-dashoffset: 57.4; }

@-webkit-keyframes draw {
  to {
    stroke-dashoffset: 0;} }

@keyframes draw {
  to {
    stroke-dashoffset: 0;
    fill-opacity: 1;
  }

There is a codepen here: http://codepen.io/anon/pen/emvWEL

The problem is that the bottom right corner doesn't quite connect -- i.e., it's not a complete rectangle, and there is a small gap in the path. However, when you remove the animation (the svg path part of the CSS, the rectangle is closed.

I thought that it might be due to the dasharray or dashoffset, but after adjusting the values, I couldn't fix it. Any thoughts?

Thanks in advance!


回答1:


The default value for stroke-linecap is butt.

enter image description here

Simple add stroke-linecap="square".

Updated CodePen

svg#animated {
  max-width: 200px;
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  margin: auto;
}
svg#animated path {
  animation: draw 1s linear forwards;
  -webkit-animation: draw 1s linear forwards;
  stroke-dasharray: 57.4;
  stroke-dashoffset: 57.4;
}
@-webkit-keyframes draw {
  to {
    stroke-dashoffset: 0;
  }
}
@keyframes draw {
  to {
    stroke-dashoffset: 0;
    fill-opacity: 1;
  }
<svg id="animated" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px" viewBox="0 0 70 51" enable-background="new 0 0 70 51" xml:space="preserve">
  <path fill="#FFFFFF" stroke="#999" stroke-miterlimit="10" stroke-linecap="square" d="M18.8,50.5h-7.9V29.7h7.9V50.5z" />
</svg>

<svg version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px" viewBox="0 0 70 51" enable-background="new 0 0 70 51" xml:space="preserve">
  <path fill="#FFFFFF" stroke="#999" stroke-miterlimit="10" d="M18.8,50.5h-7.9V29.7h7.9V50.5z" />
</svg>

Alternatively, you could increase the stroke-dasharray and stroke-dashoffset values a bit, For Example 58.



来源:https://stackoverflow.com/questions/27945300/svg-path-changes-after-animation

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!