如何设置绘图效果的动画? (最好只有css3)

时间:2013-01-11 08:53:25

标签: javascript css css3 canvas svg

我希望使用类似this的渐进线来创建类似here的树的绘图效果。我希望仅css3使用svg/canvasjs。你有什么想法吗?

更多信息:

我试图将一棵树切成碎片并逐个制作动画,但它不是草书,因为它有很多关于同步延迟和持续时间的细节,因为每件都有不同的长度等等。所有这些都是在没有svg的情况下完成的。我想现在,如果我可以设置线路的动画。

2 个答案:

答案 0 :(得分:1)

是的,请使用CSS 3查看this rendering of the Bahamas Logo

它描述了他的过程和技巧。您也可以查看来源。

可以找到更多here

<强>更新

也许这个Sencha Animator product可能有帮助吗?

答案 1 :(得分:1)

您可以使用普通SVG执行此操作。 SVG为声明性动画提供<animate>元素。

你想要的东西(据我所知)是一条看起来好像是在观众面前画出的线条。您可以使用stroke-dasharray属性来实现此目的。此属性使用一系列定义破折号和间隙长度的值来定义破折号模式。策略是:首先我们有一个长度为0的短划线和一个至少与整个路径一样长的间隙。这意味着我们什么都看不到(或者只是路径开头的第一个点)。最后,我们想要一个至少是路径全长的破折号。我们希望破折号逐渐变长和变长,直到达到最终长度(完整路径的长度)。

最简单的情况是:

<svg xmlns="http://www.w3.org/2000/svg" width="400px" height="300px">
  <!-- This is the path of a spiral -->
  <path d="m 7.1428565,220.9336 c 0,0 13.6660115,54.75386 218.5714335,51.42857 C 430.61971,269.03688 478.47682,99.194335 206.69537,110.78149 -65.086093,122.36866 45.497658,213.22607 210.28635,207.29759 375.07503,201.3691 429.75297,97.468925 207.14285,82.362175 -15.467268,67.255425 64.868608,160.66909 210,153.79075 c 145.13139,-6.87834 137.69998,-93.087405 11.42857,-99.999995 -126.271412,-6.9126 -150.382292,28.03248 -24.28571,35.71428 126.09659,7.6818 72.6601,-44.83727 -5.71429,-84.2857095"
      stroke-width="10" stroke-linecap="round" fill="none" stroke="black" stroke-dasharray="0,2305">
    <!-- This defines the animation: 
         The path is roughly 2305 units long, it will be drawn in 5 seconds -->
    <animate from="0,2305" to="2305,0" dur="5s"
        attributeName="stroke-dasharray" repeatCount="indefinite"/>
  </path>
</svg>

使用多个值(使用values属性)而不是一个from和一个to值,可以完成更复杂的事情:

<svg xmlns="http://www.w3.org/2000/svg" width="400px" height="300px">
  <path d="m 7.1428565,220.9336 c 0,0 13.6660115,54.75386 218.5714335,51.42857 C 430.61971,269.03688 478.47682,99.194335 206.69537,110.78149 -65.086093,122.36866 45.497658,213.22607 210.28635,207.29759 375.07503,201.3691 429.75297,97.468925 207.14285,82.362175 -15.467268,67.255425 64.868608,160.66909 210,153.79075 c 145.13139,-6.87834 137.69998,-93.087405 11.42857,-99.999995 -126.271412,-6.9126 -150.382292,28.03248 -24.28571,35.71428 126.09659,7.6818 72.6601,-44.83727 -5.71429,-84.2857095"
   stroke-width="10" stroke-linecap="round" fill="none" stroke="black" stroke-dasharray="0,2305">
    <animate attributeName="stroke-dasharray" dur="5s" repeatCount="indefinite" 
      values="0,2305;
              2000,305;
              2305,0"/>
  </path>
</svg>

您可以使用values属性指定精确时间(将在keyTimes中列出的值):

<svg xmlns="http://www.w3.org/2000/svg" width="400px" height="300px">
  <path d="m 7.1428565,220.9336 c 0,0 13.6660115,54.75386 218.5714335,51.42857 C 430.61971,269.03688 478.47682,99.194335 206.69537,110.78149 -65.086093,122.36866 45.497658,213.22607 210.28635,207.29759 375.07503,201.3691 429.75297,97.468925 207.14285,82.362175 -15.467268,67.255425 64.868608,160.66909 210,153.79075 c 145.13139,-6.87834 137.69998,-93.087405 11.42857,-99.999995 -126.271412,-6.9126 -150.382292,28.03248 -24.28571,35.71428 126.09659,7.6818 72.6601,-44.83727 -5.71429,-84.2857095"
   stroke-width="10" stroke-linecap="round" fill="none" stroke="black" stroke-dasharray="0,2305">
    <animate attributeName="stroke-dasharray" dur="5s" repeatCount="indefinite" 
      values="0,2305;
              2000,305;
              2305,0" 
      keyTimes="0;.9;1"/>
  </path>
</svg>

请参阅此in action on Tinkerbin

使用CSS3可以完成类似的事情:

<svg xmlns="http://www.w3.org/2000/svg" width="400px" height="300px">
  <style type="text/css">
    path {
      animation-name:animateDash;
      animation-duration:5s;
      animation-iteration-count:infinite;
    }
    @keyframes animateDash {
      from{stroke-dasharray:0,2305}
      to  {stroke-dasharray:2305,0}
    } 
  </style>
  <path d="m 7.1428565,220.9336 c 0,0 13.6660115,54.75386 218.5714335,51.42857 C 430.61971,269.03688 478.47682,99.194335 206.69537,110.78149 -65.086093,122.36866 45.497658,213.22607 210.28635,207.29759 375.07503,201.3691 429.75297,97.468925 207.14285,82.362175 -15.467268,67.255425 64.868608,160.66909 210,153.79075 c 145.13139,-6.87834 137.69998,-93.087405 11.42857,-99.999995 -126.271412,-6.9126 -150.382292,28.03248 -24.28571,35.71428 126.09659,7.6818 72.6601,-44.83727 -5.71429,-84.2857095"
    stroke-width="10" stroke-linecap="round" fill="none" stroke="black" stroke-dasharray="0,2305"/>
</svg>

自己决定使用哪种方法。