如何为单个SVG多边形点设置动画?

时间:2016-03-16 07:35:17

标签: javascript jquery svg velocity.js

如何在SVG中设置(移动)单个多边形点?如果可能,使用velocity.js。

感谢您的帮助!

<p>From...</p>

<svg height="250" width="500">
  <polygon points="0,0 200,0 200,200 00,200" style="fill:green" />
</svg>

<p>to...</p>
<svg height="250" width="500">
  <polygon points="0,0 300,0 200,200 00,200" style="fill:blue" />
</svg>

2 个答案:

答案 0 :(得分:3)

由于<animate>标记很快就会被弃用,因此这是d3.js的替代方案。

&#13;
&#13;
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.4.11/d3.min.js"></script>
<svg height="250" width="500">
  <polygon id="my-polygon" points="0,0 200,0 200,200 00,200" fill="blue" />
</svg>
<input type="button" value="click me" onclick="transformPolygon()"/>
<script type="text/javascript">
  function transformPolygon() {
    var myPolygon = d3.select(document.getElementById('my-polygon'))
    myPolygon
      .transition()
      .duration(1500)
      .ease("elastic")
      .attr('fill','green')
      .attr('points','0,0 300,0 200,200 00,200')
  }    
</script>
&#13;
&#13;
&#13;

答案 1 :(得分:2)

这是套房吗?

&#13;
&#13;
<svg height="250" width="500">
  <polygon points="0,0 200,0 200,200 00,200" style="fill:blue">
    <animate begin="shape.click" id="animation-to-click" begin="indefinite" fill="freeze" attributeName="points" dur="500ms"  to="0,0 300,0 200,200 00,200" />
  </polygon>
</svg>
<input type="button" value="click me" onclick="document.getElementById('animation-to-click').beginElement()"/>
&#13;
&#13;
&#13;