SVG group to child transforms
NickName:Andrei Ask DateTime:2022-09-20T17:47:58

SVG group to child transforms

I have a group that has 2 or more SVG elements. Group has a SVG matrix applied and the same apply for children.

Each element, including group can have any transformations applied through the matrix (rotate, skew, scale etc). Example of SVG with the group and 2 inner elements:

<g transform="matrix(0.679039, -0.734102, 0.734102, 0.679039, -21.7964, 262.861)">
    <path transform="matrix(1, 0, 1.63636, 1, 82.9909, 49.1)" d="M92.35,45.6 A92.35,22.8 0 1 1 92.35016 45.6"></path>
    <rect width="225.1" height="63.3" transform="matrix(0.742794, -0.66952, 0.66952, 0.742794, 242.258, 216.595)" rx="0"></rect>
</g>

I would like to do an ungroup function that removes the <g> element and place inner elements outside of g, but keeps all matrix transformations from group and their own transformations.

Tried multiple methods but they all have different issues where inner elements are not placed in the correct position or transformations are not copied.

The following seems to do something, but works for rotation only, if skew is applied - elements are totally different.

matrixMultiply(...matrices: DOMMatrix[]) : DOMMatrix {
    let args = arguments, i = args.length, m = args[i-1];

    while(i-- > 1) {
        let m1 = args[i-1] as Matrix;
        m = m1.multiply(m);
    }

    return m;
}

let groupMatrix = groupElement.transform.baseVal.getItem(0).matrix;
groupElement.childNodes.forEach(child => {
    let childTransform = (child as SVGGraphicsElement).transform.baseVal.getItem(0),
    childMatrix = childTransform.matrix,
    childInverseMatrix = childMatrix.inverse();
    let gm = matrixMultiply( childInverseMatrix, groupMatrix, childMatrix );

    childTransform.setMatrix(gm);
});

Copyright Notice:Content Author:「Andrei」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/73784721/svg-group-to-child-transforms

More about “SVG group to child transforms” related questions

SVG group to child transforms

I have a group that has 2 or more SVG elements. Group has a SVG matrix applied and the same apply for children. Each element, including group can have any transformations applied through the matrix (

Show Detail

How to flatten transforms in an SVG file programmatically?

I need to write some C# code that flatten transforms programmatically of any SVG file. This option is available in SVG editors like Affinity Designer (~$40 / Mac) or Inkscape (FOSS): But how do y...

Show Detail

Selective Child Transforms in Javafx

I'm wondering if it's possible to selectively apply parent transforms to a child node within javafx. To my understanding when a node is tranformed all the children of said node are also transforme...

Show Detail

Apply transforms on a group to each element after ungrouping

We're working on a project to create an SVG editor from scratch (we can't use any external libraries) - and we've hit a snag. We need to create a tool to group shapes, which we've managed to do, a...

Show Detail

calculate length and area of SVG shapes with transforms

I'm working on .NET project. I need to calculate length and area of shapes in SVG file. There are two difficulties: a) Bezier curves - it seems formula is not trivial and b) transforms - before

Show Detail

Recalculate SVG transforms on paths and keep groups

I have a large SVG with several groups. Each group has a transform. I want to recalculate / flatten the transforms while preserving the groups. &lt;g id="a" transform="matrix(2 0 0 2 -69.46 -152.7...

Show Detail

Removing transforms in SVG files

I have been struggling with this for a while, and can't seem to find an answer (that works) anywhere. I have an SVG file which looks like this: &lt;svg xmlns:dc="http://purl.org/dc/elements/1....

Show Detail

Removing transforms in SVG files

I have been struggling with this for a while, and can't seem to find an answer (that works) anywhere. I have an SVG file which looks like this: &lt;svg xmlns:dc="http://purl.org/dc/elements/1....

Show Detail

How to update transforms on an SVG element in Javascript?

I'm working on a simple visual editor using SVG and Snap.SVG library. It has a bunch of complex shapes as templates, each shape is a &lt;g&gt; element that contains paths. The editor lets the user

Show Detail

How to apply animated transforms to these SVG elements in IE?

I have an &lt;a&gt; tag that contains an SVG and which - when clicked - toggles an "open" class on the &lt;header&gt;. Depending on whether the "open" class is present, different transforms are app...

Show Detail