El atributo transform exhibe una lista de definiciones de transformación que se aplican a un elemento y a sus hijos. Los elementos en la lista de tranformación están separados por espacios en blanco y/o comas y se aplican de derecha a izquierda.
Esta definición de tranformación, especifica una transformación en forma de una matriz de transformación de seis valores. matrix(a,b,c,d,e,f) es equivalente a aplicar la siguiente matriz de transformación:
que mapea las coordinadas desde un nuevo sistema de coordenadas a un sistema existente mediante la siguiente matriz de igualdades:
Esta definición de transformación, especifica la transición en x e y. Es equivalente a: matrix(1 0 0 1 x y). Si no se provee ningun valor de y , éste se asume como cero.
Esta definición de transformación, especifica la escala de operación en x e y. Es equivalente a: matrix(x 0 0 y 0 0). Si no se provee ningun valor de y , éste se asume igual a x.
Esta definición de transformación, especifica la rotación en a grados a partir del punto dado. Si los parámetros opcionales x e y no se proveen, la rotación se produce en el origen del actual sistema de coordenadas del usuario. Esta operación se corresponde con la matriz:
Si se proveen los parámetros opcionales x e y , la rotación se produce en el punto provisto (x, y). La operación representa el equivalente a la siguiente lista de transformaciones: translate(<x>, <y>) rotate(<a>) translate(-<x>, -<y>).
In this simple example we rotate and translate (move) an SVG element using transform SVG attribute. The original element before transform is shown with a low opacity.
CSS (optional):
css
text {
font: 1em sans-serif;
}
SVG:
html
<svg
width="180"
height="200"
xmlns="http://www.w3.org/2000/svg"
xmlns:xlink="http://www.w3.org/1999/xlink">
<!-- This is the element before translation and rotation are applied -->
<rect
x="50"
y="50"
height="100"
width="100"
style="stroke:#000; fill: #0086B2"
fill-opacity="0.2"
stroke-opacity="0.2"></rect>
<!-- Now we add a text element and apply rotate and translate to both -->
<rect
x="50"
y="50"
height="100"
width="100"
style="stroke:#000; fill: #0086B2"
transform="translate(30) rotate(45 50 50)"></rect>
<text x="60" y="105" transform="translate(30) rotate(45 50 50)">
Hello Moz!
</text>
</svg>
Here is a basic example to understand a general transformation. We consider the transform matrix(1,2,3,4,5,6) and draw a thick blue line from (10,20) to (30,40) in the new coordinate system. A thin white line with the same end points is drawn above it using the original coordinate system.
All text examples in the SVG below have the same positioning on the page (x="200" y="0"), and all are rotated at 45°. The only difference is the point that anchors the rotation.
html
<svg
viewBox="-20 -20 820 420"
xmlns="http://www.w3.org/2000/svg"
width="800"
height="400">
<text x="200" y="0">
...unrotated text; same starting position as examples below (in all cases:
x="200" y="0")
</text>
<circle
cx="200"
cy="0"
r="2"
style="stroke: green; stroke-width: 1; fill: green;" />
<text x="200" y="0" transform="rotate(45 200,0)" style="fill: green;">
...(1) rotate(45 200,0) (rotated 45° around a point at 200,0)
</text>
<circle
cx="100"
cy="0"
r="2"
style="stroke: blue; stroke-width: 1; fill: blue;" />
<path
d="M 200,0 A 100,100 0 0,1 0,0"
style="stroke: blue; stroke-width: 1; fill: transparent;" />
<text x="200" y="0" transform="rotate(45 100,0)" style="fill: blue;">
...(2) rotate(45 100,0) (rotated 45° around a point at 100,0)
</text>
<circle
cx="0"
cy="0"
r="2"
style="stroke: red; stroke-width: 1; fill: red;" />
<path
d="M 200,0 A 200,200 0 0,1 0,200"
style="stroke: red; stroke-width: 1; fill: transparent;" />
<text x="200" y="0" transform="rotate(45 0,0)" style="fill: red;">
...(3) rotate(45 0,0) (rotated 45° around a point at 0,0)
</text>
</svg>