Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SVG Text Rotation Around the Center

Tags:

jquery-svg

I have the text with textContent "Design" and text-anchor as start, that was transformed with css to be rotated 45 degree. so it get rotated. problem is that i need to adjust the (x,y)position of the text after my rotation to display the text between the tick as shown in the Expected figure. How can i achieve this.

Result: http://imageshack.us/content_round.php?page=done&l=img404/2711/result1h.png

Expected: http://imageshack.us/content_round.php?page=done&l=img266/5138/expected1.png

    <svg>
    <text x="100" y="100" width="64" height="16" fill="black" transform="rotate(45,100,100)" text-anchor="start">Design</text>
    </svg>

Thanks Gowri

like image 897
Code Break Avatar asked May 24 '13 00:05

Code Break


2 Answers

The text needs to be centered horizontally and vertically so rotation won't move it:

<text x="100" y="50" text-anchor="middle"
    dominant-baseline="central" transform="rotate(0, 100, 50)"></text>

text-anchor aligns the text horizontally
dominant-baseline aligns the text vertically

svg {
    width: 200px; height: 100px;
    text-align: center;
    border: solid 1px blue;
    font-family: monospace;
}
<svg>
    <text x="100" y="50" text-anchor="middle" dominant-baseline="central" transform="rotate(180, 100, 50)">
       Hello World
   </text>
    <text x="100" y="50" text-anchor="middle" dominant-baseline="central" transform="rotate(0, 100, 50)">
       Hello World
   </text>
</svg>

example: http://jsfiddle.net/e4bAh/131/

like image 68
Mingwei Samuel Avatar answered Sep 23 '22 05:09

Mingwei Samuel


use transform="rotate(45, cx, cy)"

Where cx, cy are the co-ordinates of the center of screen (or rotation).

This is a good example

like image 37
user2415826 Avatar answered Sep 25 '22 05:09

user2415826