SVG使用css或xml从其中心点开始围绕圆形设置动画

时间:2018-02-10 01:42:29

标签: css xml animation svg css-animations

我一直在努力研究如何让我的个人徽标旋转。 st1是我的“SB”,“st1”是我试图制作动画的星号。我尝试过使用XML和CSS。我试图旋转的星号旋转,但不是从中心旋转。我看过类似的帖子,我尝试了各种建议但没有运气。

<?xml version="1.0" encoding="utf-8"?>
    
    <!-- Generator: Adobe Illustrator 22.0.1, SVG Export Plug-In . SVG Version: 6.00 Build 0)  -->
    
    
    <svg width="100" height="100" 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"
    	 viewBox="0 0 347.3 224" style="enable-background:new 0 0 347.3 224;" xml:space="preserve">
    
    <style type="text/css">
    	.st0{fill:#006F84;stroke:#006F84;stroke-miterlimit:10;}
    	.st1{fill:#FFFFFF;stroke:#FFFFFF;stroke-width:.75;stroke-miterlimit:10;}
    
    <!-- 
    	.animated.infinite {
      -webkit-animation-iteration-count: infinite;
      animation-iteration-count: infinite;
    }
    
    @-webkit-keyframes rotateOut {
      0% {
        -webkit-transform-origin: center center;
        transform-origin: center center;
        -webkit-transform: rotate(0);
        transform: rotate(0);
        opacity: 1;
      }
    
      100% {
        -webkit-transform-origin: center center;
        transform-origin: center center;
        -webkit-transform: rotate(200deg);
        transform: rotate(200deg);
        opacity: 0;
      }
    }
    
    @keyframes rotateOut {
      0% {
        -webkit-transform-origin: center center;
        -ms-transform-origin: center center;
        transform-origin: center center;
        -webkit-transform: rotate(0);
        -ms-transform: rotate(0);
        transform: rotate(0);
        opacity: 1;
      }
    
      100% {
        -webkit-transform-origin: center center;
        -ms-transform-origin: center center;
        transform-origin: center center;
        -webkit-transform: rotate(200deg);
        -ms-transform: rotate(200deg);
        transform: rotate(200deg);
        opacity: 0;
      }
    }
    
    .rotateOut {
      -webkit-animation-name: rotateOut;
      animation-name: rotateOut;
    } -->
    
    </style>
    <g>
    	<g>
    		<path class="st0" d="M338.1,153c0,39.3-30.6,59.4-73.6,59.4h-90.9c-7.1,0-9-3.4-9-9V17.6c0-5.6,1.9-9,9-9h86.6
    			c50.4,0,72,26.9,72,56c0,31.2-19.8,41.1-22.6,42.4C312.1,107.9,338.1,119.6,338.1,153z M288.9,148.3c0-14.5-10.8-18.9-25.7-18.9
    			h-7.1c-7.1,0-9-3.4-9-9V97c0-5.6,1.9-9,9-9h4c17.9,0,25.7-6.5,25.7-18.5c0-13.9-9.6-17.3-25.7-17.3h-47v116.2h51.9
    			C280.3,168.4,288.9,162.3,288.9,148.3z"/>
    	</g>
    	<g>
    		<path class="st0" d="M170.3,152.1c0,43.9-34.6,64.9-82.2,64.9c-38.6,0-83.2-22.3-83.2-65.5c0-7.1,1.9-8.7,9-8.7h30
    			c7.1,0,10.5,0.9,11.1,8.7c0.3,4,1.9,8,5.9,12.7c6.2,7.4,18.9,10.2,30.3,10.2c16.7,0,30.3-3.7,30.3-21c0-17.3-27.5-17.6-53.8-26
    			c-31.2-9.9-60-22.3-60-61.5c0-40.2,36.5-60,75.1-61.5C122,4.3,165,22.2,165,63c0,7.4-1.9,8.7-9,8.7h-30.3c-5.9,0-6.8-4.3-8.3-8.3
    			c-1.2-3.1-2.8-5.6-6.5-9.3c-5.9-5.6-17-8.3-26.3-8.3c-10.2,0-27.2,4-27.2,17.9c0,18.9,45.4,25.7,57.8,30.6
    			C137.5,101.1,170.3,110,170.3,152.1z"/>
    	</g>
    </g>
    <g>
    
    	<path class="st1 animated infinite rotateOut" d="M170.5,157.8l3.7,0c2.6,0,5.5-0.6,8.8-1.9c8.2-3.1,12.9-4.6,14.3-4.7c2.8,0,5.2,0.8,7.1,2.4
    		c1.9,1.6,2.9,3.7,2.9,6.3c0,2.4-0.9,4.5-2.7,6.2c-1.8,1.7-4,2.6-6.4,2.6c-1.3,0-5-1.1-10.8-3.2c-5.9-2.2-11.4-3.2-16.7-3.2
    		c1.1,4,4.1,8.9,8.9,14.6c4.3,5.2,6.5,9.3,6.5,12.2c0,2.4-0.8,4.5-2.4,6.2c-1.6,1.7-3.7,2.6-6.4,2.6c-6,0-9-4.5-9-13.5l0-9
    		c0-3.9-0.7-7.9-1.9-11.9c-3.1,2.7-7.1,7.7-11.9,14.9c-3.2,4.9-6.7,7.4-10.3,7.4c-2.4,0-4.5-0.8-6.4-2.4c-1.8-1.6-2.7-3.6-2.7-6
    		c0-4.6,3.1-8,9.5-10.1c10-3.1,16.2-5.5,18.5-7.2c-2.6-1.9-8.8-4.3-18.5-7.1c-6.4-1.8-9.6-5.1-9.6-10c0-2.7,0.7-4.9,2.3-6.5
    		c1.5-1.7,3.6-2.5,6.1-2.5c3.7,0,8,3.4,12.9,10.2c3.7,5.2,7.1,9.1,10.1,11.7c0.7-1.8,1.2-3.7,1.4-5.5c0.2-1.8,0.3-3.5,0.3-5.1l0-9.5
    		c0-9.2,3-13.8,9.3-13.8c2.6,0,4.6,0.8,6.1,2.4c1.5,1.6,2.2,3.7,2.2,6.2c0,2.7-1.1,5.7-3.4,9C175.1,149.3,171.3,155.3,170.5,157.8z"/>
    
    		
    		 <animateTransform attributeType="xml"
                    attributeName="transform"
                    type="rotate"
                    from="0 50 50"
                    to="360 50 50"
                    dur="5s" fill="infinite" />
    </g>
    </svg>

1 个答案:

答案 0 :(得分:3)

使用getBBox查找中心,然后将animateTransform值更改为该值。然后,您可以删除getBBox调用。或者,您可以通过脚本创建animateTransform元素,并在执行此操作时从getBBox计算中插入中心。

&#13;
&#13;
bbox = document.getElementById("g").getBBox();
alert((bbox.x + bbox.width/2) + ", " + (bbox.y + bbox.height / 2));
&#13;
    <?xml version="1.0" encoding="utf-8"?>
    
    <!-- Generator: Adobe Illustrator 22.0.1, SVG Export Plug-In . SVG Version: 6.00 Build 0)  -->
    
    
    <svg width="100" height="100" 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"
    	 viewBox="0 0 347.3 224" style="enable-background:new 0 0 347.3 224;" xml:space="preserve">
    
    <style type="text/css">
    	.st0{fill:#006F84;stroke:#006F84;stroke-miterlimit:10;}
    	.st1{fill:#FFFFFF;stroke:#FFFFFF;stroke-width:.75;stroke-miterlimit:10;}
    
    <!-- 
    	.animated.infinite {
      -webkit-animation-iteration-count: infinite;
      animation-iteration-count: infinite;
    }
    
    @-webkit-keyframes rotateOut {
      0% {
        -webkit-transform-origin: center center;
        transform-origin: center center;
        -webkit-transform: rotate(0);
        transform: rotate(0);
        opacity: 1;
      }
    
      100% {
        -webkit-transform-origin: center center;
        transform-origin: center center;
        -webkit-transform: rotate(200deg);
        transform: rotate(200deg);
        opacity: 0;
      }
    }
    
    @keyframes rotateOut {
      0% {
        -webkit-transform-origin: center center;
        -ms-transform-origin: center center;
        transform-origin: center center;
        -webkit-transform: rotate(0);
        -ms-transform: rotate(0);
        transform: rotate(0);
        opacity: 1;
      }
    
      100% {
        -webkit-transform-origin: center center;
        -ms-transform-origin: center center;
        transform-origin: center center;
        -webkit-transform: rotate(200deg);
        -ms-transform: rotate(200deg);
        transform: rotate(200deg);
        opacity: 0;
      }
    }
    
    .rotateOut {
      -webkit-animation-name: rotateOut;
      animation-name: rotateOut;
    } -->
    
    </style>
    <g>
    	<g>
    		<path class="st0" d="M338.1,153c0,39.3-30.6,59.4-73.6,59.4h-90.9c-7.1,0-9-3.4-9-9V17.6c0-5.6,1.9-9,9-9h86.6
    			c50.4,0,72,26.9,72,56c0,31.2-19.8,41.1-22.6,42.4C312.1,107.9,338.1,119.6,338.1,153z M288.9,148.3c0-14.5-10.8-18.9-25.7-18.9
    			h-7.1c-7.1,0-9-3.4-9-9V97c0-5.6,1.9-9,9-9h4c17.9,0,25.7-6.5,25.7-18.5c0-13.9-9.6-17.3-25.7-17.3h-47v116.2h51.9
    			C280.3,168.4,288.9,162.3,288.9,148.3z"/>
    	</g>
    	<g>
    		<path class="st0" d="M170.3,152.1c0,43.9-34.6,64.9-82.2,64.9c-38.6,0-83.2-22.3-83.2-65.5c0-7.1,1.9-8.7,9-8.7h30
    			c7.1,0,10.5,0.9,11.1,8.7c0.3,4,1.9,8,5.9,12.7c6.2,7.4,18.9,10.2,30.3,10.2c16.7,0,30.3-3.7,30.3-21c0-17.3-27.5-17.6-53.8-26
    			c-31.2-9.9-60-22.3-60-61.5c0-40.2,36.5-60,75.1-61.5C122,4.3,165,22.2,165,63c0,7.4-1.9,8.7-9,8.7h-30.3c-5.9,0-6.8-4.3-8.3-8.3
    			c-1.2-3.1-2.8-5.6-6.5-9.3c-5.9-5.6-17-8.3-26.3-8.3c-10.2,0-27.2,4-27.2,17.9c0,18.9,45.4,25.7,57.8,30.6
    			C137.5,101.1,170.3,110,170.3,152.1z"/>
    	</g>
    </g>
    <g>
    
    	<path  id="g" class="st1 animated infinite rotateOut" d="M170.5,157.8l3.7,0c2.6,0,5.5-0.6,8.8-1.9c8.2-3.1,12.9-4.6,14.3-4.7c2.8,0,5.2,0.8,7.1,2.4
    		c1.9,1.6,2.9,3.7,2.9,6.3c0,2.4-0.9,4.5-2.7,6.2c-1.8,1.7-4,2.6-6.4,2.6c-1.3,0-5-1.1-10.8-3.2c-5.9-2.2-11.4-3.2-16.7-3.2
    		c1.1,4,4.1,8.9,8.9,14.6c4.3,5.2,6.5,9.3,6.5,12.2c0,2.4-0.8,4.5-2.4,6.2c-1.6,1.7-3.7,2.6-6.4,2.6c-6,0-9-4.5-9-13.5l0-9
    		c0-3.9-0.7-7.9-1.9-11.9c-3.1,2.7-7.1,7.7-11.9,14.9c-3.2,4.9-6.7,7.4-10.3,7.4c-2.4,0-4.5-0.8-6.4-2.4c-1.8-1.6-2.7-3.6-2.7-6
    		c0-4.6,3.1-8,9.5-10.1c10-3.1,16.2-5.5,18.5-7.2c-2.6-1.9-8.8-4.3-18.5-7.1c-6.4-1.8-9.6-5.1-9.6-10c0-2.7,0.7-4.9,2.3-6.5
    		c1.5-1.7,3.6-2.5,6.1-2.5c3.7,0,8,3.4,12.9,10.2c3.7,5.2,7.1,9.1,10.1,11.7c0.7-1.8,1.2-3.7,1.4-5.5c0.2-1.8,0.3-3.5,0.3-5.1l0-9.5
    		c0-9.2,3-13.8,9.3-13.8c2.6,0,4.6,0.8,6.1,2.4c1.5,1.6,2.2,3.7,2.2,6.2c0,2.7-1.1,5.7-3.4,9C175.1,149.3,171.3,155.3,170.5,157.8z"/>
    
    		 <animateTransform attributeType="xml"
                    attributeName="transform"
                    type="rotate"
                    from="0 171.15 159.95"
                    to="360 171.15 159.95"
                    dur="5s" repeatCount="indefinite" />
    </g>
    </svg>
&#13;
&#13;
&#13;