SVG蛋形变动画

4

我想将一个简单的SVG路径变形成另一个路径。我想使用JavaScript来控制何时发生这种变化。

这是我创建的JSFiddle链接,作为我尝试实现的简化示例:http://jsfiddle.net/brentmitch/RqRjq/

这里是示例使用的基础代码:

function makeEgg() {
    var svgContainer = document.createElementNS('http://www.w3.org/2000/svg', 'svg');
    svgContainer.setAttribute("version", "1.1");
    svgContainer.setAttribute("width", "142.308px");
    svgContainer.setAttribute("height", "142.308px");
    svgContainer.setAttribute("id", "curvecontainer");
    svgContainer.setAttribute("x", "0px");
    svgContainer.setAttribute("y", "0px");
    var eggPath = document.createElementNS('http://www.w3.org/2000/svg', 'path');
    eggPath.setAttribute("id", "eggpath");
    eggPath.setAttribute("d", "M126.308,83.154c0,39.297-32.718,56.154-55.154,56.154S16,122.451,16,83.154S50,0,71.154,0S126.308,43.856,126.308,83.154z");
    eggPath.setAttribute("fill", "GoldenRod");
    svgContainer.appendChild(eggPath);
    document.getElementById('svgcontainer').appendChild(svgContainer);
}

function changeToArrow() {
    var animateEggPath = document.createElementNS('http://www.w3.org/2000/svg', 'animate');
    animateEggPath.setAttribute("id", "egganimation");
    animateEggPath.setAttribute("xlink:href", "#eggpath");
    animateEggPath.setAttribute("attributeType", "XML");
    animateEggPath.setAttribute("attributeName", "d");
    animateEggPath.setAttribute("from", "M126.308,83.154c0,39.297-32.718,56.154-55.154,56.154S16,122.451,16,83.154S50,0,71.154,0S126.308,43.856,126.308,83.154z");
    animateEggPath.setAttribute("to", "M126.308,95.154c0,39.297-17.333-28.667-39.769-28.667S16,134.451,16,95.154S50,12,71.154,12   S126.308,55.856,126.308,95.154z");
    animateEggPath.setAttribute("dur", "4s");
    animateEggPath.setAttribute("fill", "freeze");
}

function resetToEgg() {
    var animations = document.getElementById('egganimation');
    document.getElementById('eggpath').removeChild(animations);
}

使用SVG,将它变成一个蛋形,然后将其变形为箭头,并通过删除animate节点来重置。 我的问题是动画只在第一次运行时有效。 当我尝试重置并再次运行它时,它不再执行动画,而是立即转换为箭头。 我必须重新加载/刷新浏览器窗口才能再次执行动画。
我知道Raphael JavaScript库非常适用于此类动画,但我只想做一个简单的变形。我的访问者大多会使用移动设备,所以我想避免加载库来完成这个简单的动画。
2个回答

3

动画运行在时间轴上。动画时间基本上从0开始,然后当您运行动画时,它会运行到4秒。当您删除并重新添加动画时,不会重置时间轴,因此动画将从终点的4秒开始运行。但是,您可以通过调用setCurrentTime来重新启动时间轴,如下所示...

function resetToEgg() {
    var animations = document.getElementById('egganimation');
    document.getElementById('eggpath').removeChild(animations);
    curvecontainer.setCurrentTime(0);
    document.getElementById('arrowbutton').style.display = "inline";
    document.getElementById('resetbutton').style.display = "none";
}

0

首先,代码有点混乱,所以我没有花太多时间检查每一行,如果我错过了重要的东西,我很抱歉。

因为你的初始函数makeEgg()changeToArrow(),我保持不变。

我修改了resetToEgg()的前两行:

var animations = document.getElementById('egganimation');
document.getElementById('eggpath').removeChild(animations);

var animations = document.getElementById('curvecontainer');
document.getElementById('svgcontainer').removeChild(animations);

这样它就会删除svg元素。接下来我添加了makeEgg();以便重置蛋

这是完全功能的版本http://jsfiddle.net/berodam/hArrb/


那似乎有效。我想知道为什么删除那个动画节点不起作用?感谢Tetraodon的快速回复。抱歉代码有点混乱。 - brent
1
移除整个容器会导致重置动画时间轴。 - Robert Longson

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接