带SVG的徽标动画半圆

时间:2014-02-26 16:21:36

标签: javascript html svg svg-animate

我想在SVG中制作这个图形。我可以使用<animate>标签,我可以使用javascript操作SVG文档。如果可能的话,我更愿意在SVG中完成所有工作。

enter image description here

4 个答案:

答案 0 :(得分:4)

这是一个更简单的例子:

<svg height="200" width="200" viewBox="0 0 144 144">
    <circle cx="72" cy="72" r="49" stroke-width="6" stroke="#F68524" fill="none"/>
    <circle cx="72" cy="72" r="49" 
        stroke-width="6" stroke="#838588" fill="none">
        <animate attributeName="stroke-dasharray" values="154 0;0 154" dur="1s" repeatCount="indefinite"/>
    </circle>
</svg>

stroke-dasharray使用的正确值基于圆的周长2 * π * r,我们想要将其中的一半除以2.在计算到大约154的示例中。< / p>

请参阅jsfiddle

<强>更新 如上所述,非常接近但不完全相同。这是一个动画颜色的版本:

<svg height="200" width="200" viewBox="0 0 144 144">
    <circle cx="72" cy="72" r="49" stroke-width="6" stroke="#F68524" fill="none">
        <animate attributeName="stroke" values="#838588;#F68524" 
                 dur="2s" calcMode="discrete" repeatCount="indefinite"/>
    </circle>
    <circle cx="72" cy="72" r="49" 
        stroke-width="6" stroke="#838588" fill="none">
        <animate attributeName="stroke-dasharray" values="154 0;0 154" 
                 dur="1s" repeatCount="indefinite"/>
        <animate attributeName="stroke" values="#F68524;#838588" 
                 dur="2s" calcMode="discrete" repeatCount="indefinite"/>
    </circle>
</svg>

请参阅jsfiddle

答案 1 :(得分:1)

这只是一个起点的想法。然后,您可以应用蒙版来隐藏和显示图形的各个部分。

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
<svg xmlns="http://www.w3.org/2000/svg" width="100%" height="100%">
    <circle cx="200" cy="200" r="100" fill="gray"/>
    <rect x="100" y="200" height="101" width="201" fill="white">
        <animateTransform attributeName="transform" 
            attributeType="XML"
            type="rotate" 
            from="0 200 200" 
            to="360 200 200" 
            dur="5s"
            repeatCount="indefinite"/>
    </rect>
    <circle cx="200" cy="200" r="90" fill="white"/>
</svg>

答案 2 :(得分:1)

非常感谢@helderdarocha指出我正确的方向。

<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="144px" height="144px" viewBox="0 0 144 144" enable-background="new 0 0 144 144">

    <defs>
        <clipPath id="rotation-top" clip-path="url(#top-half)">
            <rect x="0" y="72" height="72" width="144">
                <animateTransform attributeName="transform" 
                    attributeType="XML"
                    type="rotate" 
                    from="360 72 72" 
                    to="0 72 72" 
                    dur="3s"
                    repeatCount="indefinite">
                </animateTransform>
            </rect>
        </clipPath>
        <clipPath id="rotation-bottom" clip-path="url(#bottom-half)">
            <rect x="0" y="0" height="72" width="144">
                <animateTransform attributeName="transform" 
                    attributeType="XML"
                    type="rotate" 
                    from="360 72 72" 
                    to="0 72 72" 
                    dur="3s"
                    repeatCount="indefinite">
                </animateTransform>
            </rect>
        </clipPath>
        <clipPath id="top-half">
            <rect x="0" y="0" height="72" width="144">
            </rect>
        </clipPath>
        <clipPath id="bottom-half">
            <rect x="0" y="72" height="72" width="144">
            </rect>
        </clipPath>
    </defs>

    <!-- background white -->
    <circle opacity="0.56" fill="#FFFFFF" cx="72" cy="72" r="72"/>

    <!-- back gray color -->
    <circle cx="72" cy="72" r="49" stroke-width="6" stroke="#838588" fill-opacity="0.0"/>

    <!-- foreground orange color --> 
    <circle 
        cx="72" cy="72" r="49" 
        stroke-width="6" stroke="#F68524" fill-opacity="0.0" 
        clip-path="url(#rotation-bottom)"
    />

    <circle 
        cx="72" cy="72" r="49" 
        stroke-width="6" stroke="#F68524" fill-opacity="0.0" 
        clip-path="url(#rotation-top)"
    />

</svg>

答案 3 :(得分:1)

对于跨浏览器动画,最好使用Javascript。 (IE不支持SMIL)。 setInterval在所有浏览器中都非常强大。

例如,要连续旋转元素,您可以使用:

function basicRotate()
{
    var FPS=100  //----frames per second---
    var angle = 0//---starting value---
    function frame()
    {
        angle++
        myElement.setAttribute("transform","rotate("+angle+" "+centetX+" "+centerY+")")
        if (angle == 360)
        angle=0
    }
    //---start interval timer---
    var iT = setInterval(frame, 1000/FPS ) //---draw every 10ms ---
}