简体   繁体   English

SVG.js是否可以进行路径动画

[英]Is path animation possible with SVG.js

There are many examples of SVG path animation, both natively 有很多SVG路径动画的例子,都是原生的

http://jsfiddle.net/FVqDq/ http://jsfiddle.net/FVqDq/

and with Raphael.js 和Raphael.js

http://jsfiddle.net/d7d3Z/1/ http://jsfiddle.net/d7d3Z/1/

p.animate({path:"M140 100 L190 60"}, 2000, function() {
    r.animate({path:"M190 60 L 210 90"}, 2000);
});

How is this possible with the svg.js library ? 如何使用svg.js库实现这一点

No, this is not yet possible with svg.js . 不, svg.js尚不可能。 I have been looking into it and it will be a rather large implementation. 我一直在研究它,它将是一个相当大的实现。 As I try to keep the library small it will never be part of the library itself, but I might write a plugin. 当我试图保持库很小时,它永远不会成为库本身的一部分,但我可能会编写一个插件。 Although at the moment I do not have much time on my hands so all help will be appreciated. 虽然目前我没有太多时间在我的手上所以所有的帮助将不胜感激。

UPDATE: 更新:

This is now possible with SVG.js out of the box if you use paths with equal commands but different values. 如果您使用具有相同命令但值不同的路径,则现在可以使用SVG.js开箱即用。

But we also have a path morphing plugin for SVG.js which is probably the thing you are looking for. 但我们也有一个SVG.js路径变形插件,这可能是你正在寻找的东西。

There is a quick and dirty way to animate a line with svg.js: http://jsfiddle.net/c4FSF/1/ 有一种快速而肮脏的方法来使用svg.js为一行设置动画: http//jsfiddle.net/c4FSF/1/

draw
    .line(0, 0, 0, 0)
    .stroke({color: '#000', width: 2})
    .animate(1000, SVG.easing.bounce) // Using svg.easing.js plugin(not required)
    .during(function(t, morph) {
        this.attr({x2:morph(0, 100), y2: morph(0, 100)})
    })

Animating complex SVG paths as wout said will require a plugin. 像Wout所说的那样动画复杂的SVG路径将需要一个插件。 Unfortunately I don't (yet) know enough about SVG, but I'm thinking of writing a plugin which would use the SMIL animation tag . 不幸的是,我(还)对SVG知之甚少,但我正在考虑编写一个使用SMIL动画标签的插件。 Which is what is used in the first link of the question. 这是问题的第一个链接中使用的内容。

We can make path animation by finding the bounding box of your path and the do like this. 我们可以通过查找路径的边界框来制作路径动画,并且这样做。

if your path having some clipping -rectangle means like that below 如果你的路径有一些clip -rectangle意味着如下所示

<g id="container_svg_SeriesGroup_0" transform="translate(128.8,435)" clip-path="url(#container_svg_SeriesGroup_0_ClipRect)"><path id="container_svg_John_0" fill="none" stroke-dasharray="5,5" stroke-width="3" stroke="url(#container_svg_John0Gradient)" stroke-linecap="butt" stroke-linejoin="round" d="M 0 -17.25 L 21.7 -112.12499999999999 M 21.7 -112.12499999999999 L 43.4 -51.75 M 43.4 -51.75 L 86.8 -25.875 M 86.8 -25.875 L 108.5 -155.25 "/><defs><clipPath id="container_svg_SeriesGroup_0_ClipRect"><rect id="container_svg_SeriesGroup_0_ClipRect" x="0" y="-155.25" width="118.5" height="148" fill="white" stroke-width="1" stroke="transparent" style="display: inline-block; width: 118.5px;"/></clipPath></defs></g>

var box = $("#"+ path.id")[0].getBBox();

create the rectangle based on the box and the set this rectangle as your clip-path in path. 根据框创建矩形,并将此矩形设置为路径中的剪辑路径。

then increase the width of the rectangle step by step in jquery.animate. 然后在jquery.animate中逐步增加矩形的宽度。

doAnimation: function () {

//cliprect is your clipped rectangle path.

        $(clipRect).animate(
                                 { width: 1000},
                                 {
                                     duration: 2000,

                                     step: function (now, fx) {
                                         $(clipRect).attr("width", now);
                                     }
                                 });


    },

jquery.animate step function is used to increase the width of your clip-rect step by step. jquery.animate step函数用于逐步增加clip-rect的宽度。

You can animate paths using the svg.path.js plugin. 您可以使用svg.path.js插件为路径设置动画。

See the first examples (using the .drawAnimated method). 请参阅第一个示例(使用.drawAnimated方法)。

Another option, which we've resorted to, is to use textPath and then use a character. 我们使用的另一个选项是使用textPath然后使用一个字符。

In our case we're using the • entity, but I'm thinking if you create your own typography in .svg, .woff etc, you can have flat shapes of any kind. 在我们的例子中,我们正在使用•实体,但我想如果你在.svg,.woff等中创建自己的排版,你可以拥有任何类型的扁平形状。

So you would use your character as in here: 所以你会在这里使用你的角色:

http://jsfiddle.net/wbx8J/3/ http://jsfiddle.net/wbx8J/3/

   /* create canvas */
    var draw = SVG('canvas').size(400,400).viewbox(0, 0, 1000, 1000)

    /* create text */
    var text = draw.text(function(add) {
      add.tspan('•').dy(27)
    })
    text.font({ size: 80, family: 'Verdana' })

    /* add path to text */
    text.path('M 100 400 C 200 300 300 200 400 300 C 500 400 600 500 700 400 C 800 300 900 300 900 300')

    /* visualise track */
    draw.use(text.track).attr({ fill: 'none'/*, 'stroke-width': 1, stroke: '#f09'*/ })

    /* move text to the end of the path */
    function up() {
        text.textPath.animate(3000).attr('startOffset', '100%').after(down)
    }

    /* move text to the beginning of the path */
    function down() {
        text.textPath.animate(3000).attr('startOffset', '0%').after(up)  
    }

    /* start animation */
    up()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM