Skip to content

convertTransform does not apply translate(x, y) to <text>, <circle>, <rect>`, etc #2159

@icai

Description

@icai

Summary

The convertTransform plugin currently applies transform="translate(...)" only to certain elements like <path>, but does not apply it to other valid SVG elements such as:

  • <text>
  • <circle>
  • <rect>
  • <tspan>
  • <g> (nested)

This results in inconsistent transform flattening and prevents full removal of transform from <g> containers in many real-world designs, particularly when working with Sketch or Illustrator-generated SVGs.

Example (Before)

<g transform="translate(200 188)">
  <rect width="100" height="80"/>
  <text><tspan x="30" y="40">Hello</tspan></text>
</g>

Expected (After)

<rect x="200" y="188" width="100" height="80"/>
<text transform="translate(200 188)"><tspan x="30" y="40">Hello</tspan></text>

Problem

In current output, convertTransform partially transforms the <rect> into absolute x, y, but leaves the <text> as-is or wrapped in residual transform. This breaks downstream animation or layout systems that expect flat coordinate-based SVGs without transform wrappers.

Suggestion

  • Extend convertTransform to support applying translations to:

    • x / y attributes (e.g. rect, circle, text)
    • cx / cy (e.g. circle)
    • Wrap text with its own transform if inline transform is not possible
    • Possibly recursively flatten nested <g> elements

This would significantly improve usability of SVGO for SVG animation and simplify DOM manipulation for frontend developers.


Version

SVGO v4.0.0

My Config

export default {
  multipass: true,
  js2svg: { indent: 2, pretty: true },
  plugins: [
    {
      name: 'removeViewBox',
      active: false
    },
    {
      name: 'removeXMLNS',
      active: false
    },
    {
      name: 'preset-default',
    },
    'collapseGroups',
    'convertShapeToPath',
    'convertPathData',
    'mergePaths',
    'convertTransform',
  ]
}

relative:

https://github.com/stadline/svg-flatten
https://kurachiweb.github.io/svg-rect-to-path/
https://lean-svg.netlify.app/

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions