Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

D3 4.9.1 returns different value for .style(“transform”) #135

Closed
stripathix opened this issue Jul 11, 2017 · 2 comments
Closed

D3 4.9.1 returns different value for .style(“transform”) #135

stripathix opened this issue Jul 11, 2017 · 2 comments

Comments

@stripathix
Copy link

In d3 4.9.1 if I make call to get transform of element that has style like this:

<g style="transform: translateX(3.84559px);"></g>

Operation:

d3.select(this).style("transform")

Output in d3v4.9.1 :

translateX(3.84559px)

But in d3v4.8.0 it has different output:

matrix(1, 0, 0, 1, 3.84559, 0)

Is there any change in this new version that is causing this issue?

I was having function to find different parameters of transform property, which is now broken:

contents.getTransformation = function (transform) {
    // console.log(transform);
    var g = document.createElementNS("http://www.w3.org/2000/svg", "g");
    g.setAttributeNS(null, "transform", transform);
    var matrix = g.transform.baseVal.consolidate().matrix;
    var a = matrix.a;
    var b = matrix.b;
    var c = matrix.c;
    var d = matrix.d;
    var e = matrix.e;
    var f = matrix.f;
    var scaleX = Math.sqrt(a * a + b * b);
    var scaleY = Math.sqrt(c * c + d * d);
    var skewX = a * c + b * d;
    if (scaleX) {
        a /= scaleX;
        b /= scaleX;
    }
    if (skewX) {
        c -= a * skewX;
        d -= b * skewX;
    }
    if (scaleY) {
        c /= scaleY;
        d /= scaleY;
        skewX /= scaleY;
    }
    if (a * d < b * c) {
        a = -a;
        b = -b;
        skewX = -skewX;
        scaleX = -scaleX;
    }
    return {
        translateX: e,
        translateY: f,
        rotate: Math.atan2(b, a) * Math.PI / 180,
        skewX: Math.atan(skewX) * Math.PI / 180,
        scaleX: scaleX,
        scaleY: scaleY
    };
};

Ref: https://stackoverflow.com/questions/38224875/replacing-d3-transform-in-d3-v4

@mbostock
Copy link
Member

Please read the RELEASE NOTES. As of d3-selection 1.1.0, selection.style now returns the inline style, if present. (#120)

Your code is unnecessarily complicated because you are copying the value of the transform attribute from one G element (this) to an off-screen G element created by document.createElementNS. Inlining your code a little bit:

var transform = d3.select(this).style("transform");
var g = document.createElementNS("http://www.w3.org/2000/svg", "g");
g.setAttributeNS(null, "transform", transform);
var matrix = g.transform.baseVal.consolidate().matrix;

Instead, just access the transform on the existing G element directly:

var matrix = this.transform.baseVal.consolidate().matrix;

You’ve already posted this to Stack Overflow, but as a reminder: please use Stack Overflow tag d3.js to ask for help. Although I make an effort to assist everyone that asks, I am not always available to provide help promptly or directly. Stack Overflow provides a better collaborative forum for self-help: tens of thousands of D3-related questions have already been asked there, and some answered questions may be relevant to you.

When asking for help, please include a link to a live example that demonstrates the issue, preferably on bl.ocks.org. It is often impossible to debug from code snippets alone. Isolate the issue and reduce your code as much as possible before asking for help. The less code you post, the easier it is for someone to debug, and the more likely you are to get a helpful response.

If you have a question about D3’s behavior and want to discuss it with other users, also consider the d3-js Google Group or joining the d3-js Slack.

Thank you! 🤗

@stripathix
Copy link
Author

Thanks. Will change my function. :-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

2 participants