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.interpolateTransformSvg returning an empty string #68

Closed
GerardoFurtado opened this issue Sep 12, 2019 · 5 comments
Closed

d3.interpolateTransformSvg returning an empty string #68

GerardoFurtado opened this issue Sep 12, 2019 · 5 comments

Comments

@GerardoFurtado
Copy link
Contributor

Context: I'm using a transition for changing the "translate" transform function of some elements. Sometimes the position doesn't change, so the transition goes from foo to the same foo.

However, I've notice that when transitioning from "translate(0,0)" to "translate(0,0)" the interpolator returns an empty string. The interpolator in question is d3.interpolateTransformSvg.

For instance:

d3.interpolateTransformSvg("translate(1,1)","translate(0,0)")(1);
//returns "translate(0,0)", as expected

But:

d3.interpolateTransformSvg("translate(0,0)","translate(0,0)")(1);
//returns an empty string ""

Here is a basic demo: https://jsfiddle.net/bvudenyq/

I'm not sure if the issue is in d3.interpolateTransformSvg itself or in the parse.js file. The solution is quite simple, in my case I'm just using attrTween with d3.interpolateString as the interpolator, ditching d3.interpolateTransformSvg, but I thought I should mention this bug.

Thanks,

@GerardoFurtado GerardoFurtado changed the title d3.interpolateTransformSvg returning empty string d3.interpolateTransformSvg returning an empty string Sep 12, 2019
@mbostock
Copy link
Member

Isn’t the empty string equivalent to translate(0,0)? What’s the problem?

@GerardoFurtado
Copy link
Contributor Author

The problem is just lack of consistency. The interpolator should return the target value for t = 1 no matter what, not deciding for an empty string: using an empty string with attr() actually removes the attribute, turning this...

<foo transform="translate(x,y)"></foo>

...into this:

<foo transform></foo>

Thus, in the next iteration, a code that tries to get the current transform value for calculating the next one will break.

@mbostock
Copy link
Member

That’s not how d3.interpolateTransformSvg works; it decomposes the given transformation into a standard representation, and then interpolates that. So, it’s not guaranteed that the interpolated value at t = 1 is string-equal to the target value a, just that the two will be equivalent transforms. This property is true of many of D3’s interpolators. For example, d3.interpolate("red", "blue")(1) returns "rgb(0, 0, 255)", which is the standard representation of a color in CSS.

When you say “a code that tries to get the current transform value for calculating the next one will break”, what do you mean? d3.interpolateTransformSvg seems to work fine when interpolating from the empty string to a non-empty transform:

d3.interpolateTransformSvg("", "translate(100, 0)")(0.5) // "translate(50, 0)"

@GerardoFurtado
Copy link
Contributor Author

GerardoFurtado commented Sep 13, 2019

I meant a code that uses attr("transform") or this.getAttribute("transform") to get the current transform value.

Anyway, now I see this is by design. Thanks for your explanation, as always it was very useful.

@mbostock
Copy link
Member

mbostock commented Sep 13, 2019

Thank you for your patience. I’ll close, but I’ll also mention that I would like to change how interpolateTransform works in #44, such that we interpolate transform function lists (when the start and end transform have the same structure) rather than matrices. I think that would also address your needs here: since translate(1,1) and translate(0,0) have the same structure, we would return translate(0, 0) at the end (instead of the empty transform) if #44 were implemented.

But implementing this is non-trivial so I haven’t yet found time to do it. Contributions welcome!

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