如何将SVG CSS动画转换为纯JS代码

时间:2014-10-31 22:12:36

标签: javascript css css3 svg svg-animate

我想将svg路径动画转换为纯粹的javascript路径。

SVG代码:

<svg version="1.1" id="Layer_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px" width="340px" height="333px" viewBox="0 0 340 333" enable-background="new 0 0 340 333" xml:space="preserve">
    <path class="path" fill="#FFFFFF" stroke="#000000" stroke-width="4" stroke-miterlimit="10" d="M66.039,133.545c0,0-21-57,18-67s49-4,65,8
s30,41,53,27s66,4,58,32s-5,44,18,57s22,46,0,45s-54-40-68-16s-40,88-83,48s11-61-11-80s-79-7-70-41
C46.039,146.545,53.039,128.545,66.039,133.545z"/>
</svg>

CSS代码:

.path {
    stroke-dasharray: 10 10;    /* 10px fill, 10px gap */
    -webkit-animation: dash 10s linear normal infinite;
}

@-webkit-keyframes dash {
  from {
      stroke-dashoffset: 1000;
  }
  to {
      stroke-dashoffset: 0;
  }
}

以下是Fiddle

3 个答案:

答案 0 :(得分:0)

我强烈建议您试试Raphael.js库。

使用它可以轻松地重现你想要的东西。

此外,还有一个非常有用的工具可将SVG文件转换为Raphael.js可重复使用的代码:http://www.readysetraphael.com/

因此您的SVG文件将变为:

var rsr = Raphael('rsr', '340', '333');

var path_a = rsr.path("M66.039,133.545c0,0-21-57,18-67s49-4,65,8s30,41,53,27s66,4,58,32s-5,44,18,57s22,46,0,45s-54-40-68-16s-40,88-83,48s11-61-11-80s-79-7-70-41C46.039,146.545,53.039,128.545,66.039,133.545z");

path_a.attr({
  class: 'path',
  fill: '#FFFFFF',
  stroke: '#000000',
  "stroke-width": '4',
  "stroke-miterlimit": '10',
  'stroke-opacity': '1' 
}).data('id', 'path_a');

var rsrGroups = [];

然后,您只需在文档中添加<div id="rsr"></div>,并将CSS选择器.path替换为path

Etvoilà!

检查这个小提琴以查看它的实际效果: http://jsfiddle.net/47nkqgmn/

答案 1 :(得分:0)

您可以查看一些javascript SVG库。我偏爱拉斐尔(www.raphaeljs.com)。

这将是这样的:

var paper = Raphael('canvas', 600, 600);
paper.path("M66.039,133.545c0,0-21-57,18-67s49-4,65,8s30,41,53,27s66,4,58,32s-    5,44,18,57s22,46,0,45s-54-40-68-16s-40,88-83,48s11-61-11-80s-79-7-70-41C46.039,146.545,53.039,128.545,66.039,133.545z")
.attr({fill:"#FFFFFF", stroke: "#000000", "stroke-width": "4", "stroke-miterlimit": "10"})

小提琴:http://jsfiddle.net/xLekbar4/

(注意:我在html中添加了一个div作为raphael“canvas”的容器,并将css更改为应用于'path'元素,而不是使用类'.path'的元素。)

答案 2 :(得分:0)

我不确定为什么你需要纯粹的javascript,这个答案可能也不符合你的需求,因为它确实创建了内联CSS。但这里主要取自:https://developer.mozilla.org/en-US/docs/Web/SVG/Attribute/stroke-dashoffset

&#13;
&#13;
function dashOffset() {
    var path = document.querySelector('.path');
    var length = "1000";
    // Clear any previous transition 
    path.style.transition = path.style.WebkitTransition = 'none';
    // Set up the starting positions 
    path.style.strokeDasharray = "10 10";
    path.style.strokeDashoffset = "1000";
    // Trigger a layout so styles are calculated & the browser 
    // picks up the starting position before animating 
    path.getBoundingClientRect();
    // Define our transition 
    path.style.transition = path.style.WebkitTransition = 'stroke-dashoffset 10s linear';
    //listener to restart our loop
    path.addEventListener('transitionend', dashOffset, false);
    // Go! 
    path.style.strokeDashoffset = '0';
}
dashOffset();
&#13;
<svg version="1.1" id="Layer_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px" width="340px" height="333px" viewBox="0 0 340 333" enable-background="new 0 0 340 333" xml:space="preserve">
    <path class="path" fill="#FFFFFF" stroke="#000000" stroke-width="4" stroke-miterlimit="10" d="M66.039,133.545c0,0-21-57,18-67s49-4,65,8
	s30,41,53,27s66,4,58,32s-5,44,18,57s22,46,0,45s-54-40-68-16s-40,88-83,48s11-61-11-80s-79-7-70-41
	C46.039,146.545,53.039,128.545,66.039,133.545z" />
</svg>
&#13;
&#13;
&#13;

还有requestAnimationFrame方式:

function anim(){
    var path = document.querySelector('.path');
    path.style.strokeDasharray = "10 10";
    path.style.strokeDashoffset--;
    requestAnimationFrame(anim);
    }
requestAnimationFrame(anim);

&#13;
&#13;
function anim(){
        var path = document.querySelector('.path');
        path.style.strokeDasharray = "10 10";
        path.style.strokeDashoffset--;
        requestAnimationFrame(anim);
        }
 requestAnimationFrame(anim);
&#13;
<svg version="1.1" id="Layer_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px" width="340px" height="333px" viewBox="0 0 340 333" enable-background="new 0 0 340 333" xml:space="preserve">
    <path class="path" fill="#FFFFFF" stroke="#000000" stroke-width="4" stroke-miterlimit="10" d="M66.039,133.545c0,0-21-57,18-67s49-4,65,8
	s30,41,53,27s66,4,58,32s-5,44,18,57s22,46,0,45s-54-40-68-16s-40,88-83,48s11-61-11-80s-79-7-70-41
	C46.039,146.545,53.039,128.545,66.039,133.545z" />
</svg>
&#13;
&#13;
&#13;

请注意,您可以使用纯SVG animateMotion元素重现它。

相关问题