Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rotate rectangle around its own center in SVG

Tags:

svg

I have following piece of code :

<svg>  <defs> <rect id = "myRect"       x = "10"       y = "10"       height = "120"       width = "120"       stroke-width = "2px"       stroke = "red"       fill = "blue" />  </defs>   <g transform = "translate(100,30)">   <use xlink:href = "#myRect" /> </g>  <g transform = "translate(100, 100) rotate(45 ? ?)">    <rect id = "myRect"       x = "10"       y = "10"       height = "120"       width = "120"       stroke-width = "2px"       stroke = "green"       fill = "yellow" /> </g>  </svg> 

When I translate rectangle without rotation, it is working fine. But when I rotate it, I wanted to rotate it around its center axis point. What should I need to pass to rotate attribute?

like image 231
Vinay Bagale Avatar asked Feb 28 '13 15:02

Vinay Bagale


People also ask

How do you rotate a rectangular center?

Rotations always happen around 0,0. In order to rotate around the center of the object you need to move the points so the center of the object is at 0,0; then rotate them; then afterwards move them back again: So if cx and cy are your center: p1.

How do you rotate an element in SVG?

Just use the element type selector and add the transform: rotate(180deg) property to it like in the below snippet. Show activity on this post. Inline you would do it like this: x-axis flip : transform="scale(-1 1)"

How does SVG rotate work?

When using an SVG transform attribute, the element and its system of coordinates are simply rotated around the point specified by the second and third arguments of the rotate() function, a point whose coordinates we've computed so that it's situated at the 50% 50% point of the element.


1 Answers

You would have to set the center as the center of the filled element. Like this:

svg .rotate {   transform-box: fill-box;   transform-origin: center;   transform: rotate(45deg); } 
like image 161
bjoster Avatar answered Sep 27 '22 15:09

bjoster