没有CSS或Java,无限期地旋转SVG组360度

时间:2013-06-07 23:44:44

标签: svg svg-animate

这是我第一次潜入SVG。是否可以在没有css或javascript / jquery的情况下无限期地旋转这个组360?到目前为止,我让它在左上角旋转,但我似乎无法弄清楚如何使它居中。

<?xml version="1.0" standalone="no"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd"> 
<svg width="576" height="576" viewBox="0 0 288 288" xmlns="http://www.w3.org/2000/svg" version="1.1">
<g id="seed" transform="translate(144,144)" stroke-width="2" stroke="black" fill="none" >
<circle cx="0" r="64" />
<circle cx="64" r="64" />
<circle cx="64" r="64" transform="rotate(60)" />
<circle cx="64" r="64" transform="rotate(120)" />
<circle cx="64" r="64" transform="rotate(180)" />
<circle cx="64" r="64" transform="rotate(240)" />
<circle cx="64" r="64" transform="rotate(300)" />
<circle cx="0" r="128" />
<animateTransform attributeType="xml" attributeName="transform" type="rotate" values="0 0 0; 360 0 0" dur="5s" repeatCount="indefinite" />
</g>
</svg>

2 个答案:

答案 0 :(得分:4)

您的animateTransform会覆盖<g>元素的转换。您希望使用属性additive="sum"

提供额外的转换
<svg width="576" height="576" viewBox="0 0 288 288" xmlns="http://www.w3.org/2000/svg" version="1.1">
<g id="seed" transform="translate(144,144)" stroke-width="2" stroke="black" fill="none" >
<circle cx="0" r="64" />
<circle cx="64" r="64" />
<circle cx="64" r="64" transform="rotate(60)" />
<circle cx="64" r="64" transform="rotate(120)" />
<circle cx="64" r="64" transform="rotate(180)" />
<circle cx="64" r="64" transform="rotate(240)" />
<circle cx="64" r="64" transform="rotate(300)" />
<circle cx="0" r="128" />
<animateTransform attributeType="xml" attributeName="transform" type="rotate" values="0 0 0; 360 0 0" dur="5s" additive="sum" repeatCount="indefinite" />
</g>
</svg>

答案 1 :(得分:0)

PaEDIT:我的糟糕,你的意思是JavaScript。

好的,您可以使用JavaScript和一个名为getBBox()的不错的SVG方法来计算中心;

返回SVG元素中心的方法,这样你也应该可以。好的一段代码

 var svg = {    
 getCenterPosition:  function(elem) {

        // use the native SVG interface to get it's boundingBox
        var bbox                = elem.getBBox();
        // return the center of the bounding box
        return {
            px: (bbox.x + bbox.width / 2).toFixed(2),
            py: (bbox.y + bbox.height / 2).toFixed(2)
        };
    }
}

svg.getCenterPosition(yourGElementAsParameter);将返回中心坐标。 (Web开发人员控制台或Firebug等)然后您可以在属性中设置这些坐标,但不要更改视图框或现在围绕其中心旋转的任何内容。

相关问题