显示动画线条的箭头

Display arrow head for an animated line

我正在使用 https://jsfiddle.net/arungeorgez/r9x6vhcb/4/ 为从点 1 到点 2 的一条线设置动画。如何在同一行中添加箭头?

<style>
  .key-anim1 {
    -webkit-animation: Drawpath 1s linear forwards;
    animation: Drawpath 1s linear forwards;
    stroke-dasharray: 0, 600;
  }

  @-webkit-keyframes Drawpath {
    from {
      stroke-dasharray: 0, 600;
    }
    to {
      stroke-dasharray: 600, 600;
    }
  }
  @keyframes Drawpath {
    from {
      stroke-dasharray: 0, 600;
    }
    to {
      stroke-dasharray: 600, 600;
    }
  }
  </style>
<animate attributeType="XML" 
         attributeName="opacity" 
         from="0" to="1" 
         dur=".08s" begin=".23s" 
         repeatCount="0" fill="freeze" 
/>

似乎对你的情况产生了不错的效果,因为箭头很小。但是,对于更精细的解决方案,可以使用 <animateTransform><animateMotion>,而不是 <animate>,具体视情况而定。

这是 SMIL Animations 的规范。

虽然使用 CSS 动画很容易实现效果(最后,我只制作上面的 opacity 动画),但我倾向于为 <svg> 推荐 SMIL 动画,因为它们提供了更多选项来控制动画的不同方面,远远优于 CSS 选项,恕我直言。

function makeSVG(tag, attrs) {
  var el = document.createElementNS('http://www.w3.org/2000/svg', tag);
  for (var k in attrs)
    el.setAttribute(k, attrs[k]);
  return el;
}

$(document).ready(function() {
  var line = makeSVG('line', {
    id: "-",
    class: "key-anim1",
    x1: 20,
    y1: 20,
    x2: 120,
    y2: 120,
    stroke: 'black',
    'stroke-width': 2,
    'marker-end': "url(#arrow)"
  });
  document.getElementById("svg").appendChild(line);
});
.key-anim1 {
    -webkit-animation: Drawpath 1s linear forwards;
    animation: Drawpath 1s linear forwards;
    stroke-dasharray: 0, 600;
  }

  @-webkit-keyframes Drawpath {
    from {
      stroke-dasharray: 0, 600;
    }
    to {
      stroke-dasharray: 600, 600;
    }
  }
  @keyframes Drawpath {
    from {
      stroke-dasharray: 0, 600;
    }
    to {
      stroke-dasharray: 600, 600;
    }
  }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<svg height="600" width="600" id="svg">
  <defs>
    <marker id="arrow" markerWidth="10" markerHeight="10" refX="0" refY="3" orient="auto" markerUnits="strokeWidth" opacity="0">
      <path d="M0,0 L0,6 L9,3 z" fill="#000" />
      <animate attributeType="XML" attributeName="opacity" from="0" to="1" dur=".08s" begin=".23s" repeatCount="0" fill="freeze" />
    </marker>
  </defs>
</svg>

注意:微调任何动画的简单方法是减少速度10倍。这样你就可以让它变得完美,并且在你对它的性能慢 10 倍感到满意之后再增加它。有时,在加速后退时,您需要对 "technically correct" 的状态进行微调以平衡视错觉(但这通常与 "invisible details" 的范围相去甚远)。


如果你想让制造商可见并沿着线移动,你需要删除 dasharray(因为现在你的线从动画开始到结束的长度相同,但它是用虚线绘制的,你只是简单地移动了那条虚线中的间隙,所以它看起来像是在增长)。相反,您需要先 使其 变短,然后将其设置为更长的动画。

这是一个例子:

<svg height="600" width="600" id="svg">
  <defs>
    <marker id="arrow" markerWidth="10" markerHeight="10" refX="0" refY="3" orient="auto" markerUnits="strokeWidth">
      <path d="M0,0 L0,6 L9,3 z" fill="#000" opacity="0">
        <animate attributeType="XML" attributeName="opacity" from="0" to="1" dur=".1s" repeatCount="0" fill="freeze" />
      </path>
    </marker>
  </defs>
  <line id="-" x1="20" y1="20" x2="21" y2="21" stroke="black" stroke-width="2" marker-end="url(#arrow)">
      <animate attributeType="XML" attributeName="x2" from="21" to="120" dur="1s" repeatCount="0" fill="freeze" />
      <animate attributeType="XML" attributeName="y2" from="21" to="120" dur="1s" repeatCount="0" fill="freeze" />
  </line>
</svg>