【问题标题】:SVG Path animation with Jquery带有 Jquery 的 SVG 路径动画
【发布时间】:2014-09-30 11:46:44
【问题描述】:

我正在尝试为 SVG->Path 元素设置动画。

就像一个计时器,10秒后应该为零

这是我的 SVG 代码:

<div style="margin:200px">
    <svg width="150" height="150" viewBox="0 0 150 150">
        <path transform="translate(75, 75) scale(1)" fill="rgba(0,0,0,0.6)" d="M 0, 0 V -75 A 75 75 1 1 1 -0.001 -75 Z"></path>
    </svg>
</div>

但我不知道如何通过 jQuery 开始对其进行动画处理

【问题讨论】:

标签: javascript jquery svg


【解决方案1】:

以下是不使用 jQuery 的方法:

<?xml version="1.0" encoding="UTF-8"?>
<svg viewBox="0 0 480 360" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
    <title>Animates a path</title>  
    <path id="arc" d="M0 0"/>
    <script> 
        var circle = document.getElementById("arc"),
            startangle = -90,
            angle = startangle,
            radius = 100,
            cx = 240,
            cy = 180,
            increment = 5; // make this negative to animate counter-clockwise

        function drawCircle() {
            var radians = (angle/180) * Math.PI,
                x = cx + Math.cos(radians) * radius,
                y = cy + Math.sin(radians) * radius,
                e = circle.getAttribute("d"),
                d = "";

            if(angle == startangle)
                d = "M "+cx + " " + cy + "L "+x + " " + y;
            else
                d = e + " L "+x + " " + y;

            circle.setAttribute("d", d);

            angle += increment;
            if (Math.abs(angle) > (360+startangle*Math.sign(increment)))
                angle = startangle;

            window.requestAnimationFrame(drawCircle);
        }

        drawCircle();
</script>
</svg>

live example

【讨论】:

  • 您的实时示例仅在 Firefox 中运行。在 IE 和 Chrome 中它不显示任何内容
猜你喜欢
  • 2022-01-14
  • 1970-01-01
  • 2018-06-18
  • 1970-01-01
  • 2013-02-20
  • 2016-02-14
  • 2016-01-06
  • 1970-01-01
  • 2022-01-03
相关资源
最近更新 更多