Code

SVG

Transforms

Beginner The transform attribute on <g> and shape elements: translate, rotate, scale, skewX, and combined matrices. MDN transform

translate(100,90)rotate(35)scale(1.8,1.2)skewX(20)translate rotate scalematrix(…)
<!-- translate(tx, ty) -->
<g transform="translate(100, 90)">
  <rect .../>
</g>

<!-- rotate(angle)
     rotate(angle, cx, cy) -->
<rect transform="rotate(35)"/>
<rect transform="rotate(35, 50, 25)"/>

<!-- scale(sx) or scale(sx, sy) -->
<rect transform="scale(1.8, 1.2)"/>

<!-- skewX(angle) / skewY(angle) -->
<rect transform="skewX(20)"/>

<!-- Combining — applied right to left -->
<g transform="translate(240,195)
              rotate(15)
              scale(0.9)">
  <polygon .../>
</g>

<!-- matrix(a,b,c,d,e,f) -->
<!-- [cos θ, -sin θ,
      sin θ,  cos θ,
      tx, ty] for 30° rotation -->
<g transform="matrix(0.866,-0.5,
                     0.5,0.866,
                     370,190)">
  <rect .../>
</g>
Key Concepts
  • translate(tx, ty) — moves the coordinate origin; no need to adjust every attribute
  • rotate(angle) — rotates around the current origin; use rotate(a,cx,cy) to set a pivot
  • scale(sx, sy) — scales; scale(-1,1) flips horizontally
  • skewX(a) / skewY(a) — shears along X or Y axis
  • matrix(a,b,c,d,e,f) — full 2D affine transform in one attribute
  • Multiple transforms in one attribute string are applied right-to-left
  • CSS transform property also works on SVG elements in modern browsers