Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SVG rotate path

Tags:

css

animation

svg

I'm starting to use SVG and so far it's all okay, but when I try to do some animation with CSS the result isn't what I expect it to be.

I want to rotate a gear like in this Fiddle.

Below is the CSS I used to rotate the elements:

.gear {
    -webkit-animation: rotation 2s infinite linear;
    -moz-animation: rotation 2s infinite linear;
    -o-animation: rotation 2s infinite linear;
    animation: rotation 2s infinite linear;
}

@-webkit-keyframes rotation {
    from {-webkit-transform: rotate(0deg);}
    to   {-webkit-transform: rotate(359deg);}
}
@-moz-keyframes rotation {
    from {-moz-transform: rotate(0deg);}
    to   {-moz-transform: rotate(359deg);}
}
@-o-keyframes rotation {
    from {-o-transform: rotate(0deg);}
    to   {-o-transform: rotate(359deg);}
}
@keyframes rotation {
    from {transform: rotate(0deg);}
    to   {transform: rotate(359deg);}
}
like image 706
Robert Coroianu Avatar asked Jan 28 '14 00:01

Robert Coroianu


People also ask

How do I rotate a path in SVG?

Just use the element type selector and add the transform: rotate(180deg) property to it like in the below snippet.

How do you rotate a path?

Path points can be rotated numerically using the Tool Options bar settings. Set a rotation angle in the field and click Rotate to apply the rotation.

How do I rotate a SVG file 90 degrees?

To rotate an SVG, upload your vector file or drag n drop it to the editor. Next, click on the SVG file to activate the four-pointers. Hold the top pointer to rotate the SVG clockwise or anticlockwise until you're satisfied with the orientation and position.

How do I flip SVG horizontally?

Use this CSS to flip an SVG horizontally: -webkit-transform: scaleX(-1); transform: scaleX(-1); September 15, 2020.


2 Answers

Set transform-origin to 50% 50% in order to make the svg animation function like the img one:

UPDATED EXAMPLE HERE

.gear {
    transform-origin: 50% 50%;
    -webkit-transform-origin: 50% 50%;
    -moz-transform-origin: 50% 50%;
}

It's worth noting that the default/initial value of the transform-origin property is 50% 50% 0.

like image 62
Josh Crozier Avatar answered Nov 08 '22 06:11

Josh Crozier


Actually it is animated in Chromium at least. Just the center is not correct. (Try to remove the width, height and x/y attributes, while just using the viewBox for the SVG tag.)

In Opera I could get it working, when setting the class "gear" to the SVG element instead of the path:

<svg class="gear" …

You may also have to set the transform-origin.

like image 22
feeela Avatar answered Nov 08 '22 06:11

feeela