Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SVG Text rotation

Tags:

svg

jquery-svg

I have a text with textContent "Design", that was transformed with css to be rotated 90 degree around the center (100,100), so it appears vertically. It rotate the entire coordinate system around the center, I want the element (ie “Design”) alone should rotate

Original position:

enter image description here

Result:

enter image description here

Expected :

enter image description here

SVG:

<svg>
<text x="70" y="30" width="64" height="16" fill="black" transform="rotate(90,100,100)">Design</text>
</svg>

What is the problem ? why text element x position changed ? how could i rotate the text in fixed x-position ?

i want to rotate a text in fixed x-position ?

Thanks,

Siva

like image 512
SivaRajini Avatar asked May 23 '13 11:05

SivaRajini


People also ask

How do I rotate text in SVG?

SVG Text Rotate textThe rotate property rotates each character by the specified angle. To rotate the whole text element, you have to use the transform property.

Can I rotate SVG?

Rotate. The rotate(<a> [<x> <y>]) transform function specifies a rotation by a degrees about a given point. If optional parameters x and y are not supplied, the rotation is about the origin of the current user coordinate system. If optional parameters x and y are supplied, the rotation is about the point (x, y) .

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.


2 Answers

If you would like text at 100,100 rotated 90 degrees then try the following:

<svg>
  <text text-anchor="middle" transform="translate(100,100) rotate(90)">Design</text>
</svg>

This is much more straight-forward than the alternatives.

Keep in mind that the order of the transform parameters, translate and rotate, matters.

See the snippet below to see how it alters the outcome.

.translate {
  stroke: green;
  stroke-width: 2;
  fill: none;
  }
  
.rotate {
  stroke: red;
  stroke-width: 2;
  fill: none;
  }
  
.reference {
  stroke: blue;
  border: solid 1px blue;
  }
<svg class="reference" width="250" height="170" viewBox="-125 -25 250 170">
  <rect x="0" y="0" width="100" height="100" fill="none" stroke="black"/>
  <line class="translate" x1="0" x2="100" y1="0" y2="100"/>
  <circle class="rotate" cx="100" cy="100" r="3"/>
  <text text-anchor="middle" class="reference">0,0</text>
  <text text-anchor="middle" transform="translate(100,100) rotate(90)">TR</text>
</svg>

<svg class="reference" width="250" height="170" viewBox="-125 -25 250 170">
  <rect x="0" y="0" width="100" height="100" fill="none" stroke="black"/>
  <path class="rotate" d="M -100 100 A 141.1 141.1 0 0 0 100 100"/>
  <line class="translate" x1="0" x2="-100" y1="0" y2="100"/>
  <text text-anchor="middle" class="reference">0,0</text>
  <text text-anchor="middle" transform="rotate(90) translate(100,100)">RT</text>
</svg>
like image 182
Cyrille Avatar answered Sep 27 '22 23:09

Cyrille


The reason may be that the text-anchor is in the middle, so the (x, y) coordinates are for the center of the text and it is rotating about its center. You can add a text-anchor="start" attribute to the text, but then it won't be centered.

EDIT: The reason the x-coordinate has changed is because you are rotating around the point(100, 100) and the text is at (70, 30). The text is therefore at (-30, -70) relative to the center of rotation so after rotation through 90 degrees will be at (70, -30). Why not rotate centred on (70, 30)?

like image 31
Peter Collingridge Avatar answered Sep 27 '22 23:09

Peter Collingridge