PerfectVector
Studio
By Irene Kim

How to Flatten SVG Transforms and Keep Path Shape

Rewrite SVG path coordinates to flatten nested transforms. Check the original and flattened artwork at the same scale and keep a separate master for edits.

5 min read
On this page

To flatten SVG transforms, apply the combined matrix to the path coordinates, write the new coordinates into d, and remove the transforms those coordinates already include. Keep the original SVG as your master. Flatten a copy when a receiver needs coordinates in the parent coordinate system or drops the transform during import.

Flattening changes how the file stores the artwork's position and size. For a filled cubic path, you can keep both the rendered shape and the original curve command. Start with a solid-filled path written with absolute M, L, and C commands and a Z closure. Use a path without a stroke or effects for this first test.

Find the coordinates you need to transfer

A transform matrix maps a point from one coordinate system to another. In matrix(a b c d e f), a point (x, y) becomes (a·x + c·y + e, b·x + d·y + f). With nested groups, apply the path's own mapping first, then its ancestors' mappings, up to the space that will contain the new path. The root viewBox can stay in place for a transfer copy that keeps the same canvas. CSS Transforms matrix definition · SVG 2 coordinate systems

Save this as nested.svg. It contains one closed path, with scale and skew in the inner group and a translation in the outer group.

<svg xmlns="http://www.w3.org/2000/svg" width="136" height="72" viewBox="0 0 136 72">
  <g transform="translate(18 12)">
    <g transform="matrix(2 0 .5 1.5 0 0)">
      <path fill="#285f75" d="M8 10 C12 0 36 0 40 10 L40 28 L8 28Z"/>
    </g>
  </g>
</svg>

Apply the matrix to the path

  1. Combine the two mappings. Apply the inner mapping (2x + .5y, 1.5y), then add (18, 12). The combined matrix is matrix(2 0 .5 1.5 18 12).
  2. Map every absolute point. The starting point (8, 10) becomes (39, 27). For the cubic C command, map both control points and the endpoint. A control point steers the curve between the endpoints. SVG 2 cubic Bézier commands
  3. Keep the Z closure, replace the path data, and remove the two transformed wrappers. Save the second file as flat.svg with the same canvas and fill.
<svg xmlns="http://www.w3.org/2000/svg" width="136" height="72" viewBox="0 0 136 72">
  <path fill="#285f75" d="M39 27 C42 12 90 12 103 27 L112 54 L48 54Z"/>
</svg>
Point to mapBeforeAfter
Start of the path8, 1039, 27
First control point12, 042, 12
Second control point36, 090, 12
End of the curve40, 10103, 27

Verify the rendered result and the saved markup

Open both SVGs at the same display size. Check the position and silhouette before trying the transfer. In our 136 × 72 pixel resvg renders, the two files had zero differing pixels. The flattened file still had one cubic command and no transform attributes. The original and flattened paths also match at 4× in the same renderer.

Side-by-side renders with the same curved path and placement before and after removing nested SVG transforms
The same filled path rendered from the nested-transform source and the rewritten path coordinates. Both SVGs use the same viewBox.

If the shape shifts, compare one expected point first. A point that is offset by the wrong amount can reveal a reversed transform order or an ancestor left in the file after you rewrote coordinates. The SVG transform order guide walks through that diagnosis. For relative commands, use the absolute and relative path guide before applying a translation to numeric values.

Decide what else the transform affects

The example keeps the same root coordinate system and has no stroke, clip, mask, or filter. A path-only rewrite needs a separate paint check for artwork that uses any of them. A nonuniform scale, for instance, can change how a stroke looks along different edges. Removing the scale and preserving the original stroke width as one number can change the result. See the SVG stroke troubleshooting guide before adapting the example to stroked artwork.

An elliptical arc has radii, rotation, and direction flags in addition to its endpoint. Use a path-aware tool for arcs or convert them to curves with a tolerance you can verify. The point-by-point rewrite above covers M, L, C, and Z. For a path with H or V, first rewrite the command as an L with both coordinates. A skew or rotation can turn that horizontal or vertical segment into a diagonal one. SVG 2 line commands Keep transformed groups that inherit important styles unless you can preserve those styles on the rewritten paths.

FAQ

Can I just remove the transform attribute?

Only when the transform is effectively the identity in the destination coordinate system. In the example, deleting both group transforms without rewriting the path moves and shrinks the badge.

Does flattening a transform rasterize the SVG?

No. A geometry rewrite leaves SVG path data. You can still select nodes and edit curves after flattening the example.

Should I change the viewBox while flattening path coordinates?

Keep it the same when the flattened file should occupy the same canvas. A viewBox change also affects how that coordinate system fits the viewport.

Start with editable source art

PerfectVector can turn flat raster artwork into editable SVG paths.

If the source is a PNG or JPG, trace the artwork into vector paths before you work with path coordinates. Inspect the outline and any interior openings on the traced SVG, then create a separate transfer copy for flattening.

Sources

  1. W3C CSS Transforms — Matrix function — The six matrix coefficients and their placement.
  2. W3C SVG 2 — Coordinate systems and units — Coordinate system and viewport mapping.
  3. W3C SVG 2 — Cubic Bézier commands — Endpoints and control points in path data.

More from the blog

PerfectVector

Start with a cleaner SVG
that is easier to edit

Vectorize and inspect previews for free, with no daily limit.