Skip to content

Munging of transform attribute breaks interpolation. #344

Closed

Description

@mbostock

I've noticed that Firefox and IE9 silently convert "transform(x,0)" to "transform(x)". Which is to say, if the y-coordinate is zero, it is silently dropped when the attribute is set. This causes problems when interpolating strings, because instead of interpolating from "translate(10,0)" to "translate(10,20)", we're interpolating from "translate(10)" to "translate(10,20)". In the latter case, the initial value of the second number (20) is undefined rather than zero, so D3 cannot interpolate and just jumps directly to y=20.

One possible fix is to change the core interpolator, such that any new number for which there is no existing number is interpolated from zero rather than undefined:

diff --git a/src/core/interpolate.js b/src/core/interpolate.js
index 08e9312..ed842b1 100644
--- a/src/core/interpolate.js
+++ b/src/core/interpolate.js
@@ -68,16 +68,10 @@ d3.interpolateString = function(a, b) {
     }
   }

-  // Remove any numbers in b not found in a.
-  while (i < n) {
-    o = q.pop();
-    if (s[o.i + 1] == null) { // This match is followed by another number.
-      s[o.i] = o.x;
-    } else { // This match is followed by a string, so coallesce twice.
-      s[o.i] = o.x + s[o.i + 1];
-      s.splice(o.i + 1, 1);
-    }
-    n--;
+  // For any numbers in b not found in a, assume a is 0 and interpolate.
+  for (; i < n; ++i) {
+    o = q[i];
+    o.x = d3.interpolateNumber(0, parseFloat(o.x));
   }

   // Special optimization for only a single match.

However, this has potentially bad side-effects. For example, the d3.svg.axis component enters new ticks, and then uses a single transition on the entering and updating ticks to set the tick size. Previously, the entering new ticks would have an initial tick size of undefined, so when the transition starts they would immediately be set to the proper tick size; under the above change, they would transition from 0 to the tick size as they enter. This looks bad. We could fix the axis component to not do this, but there are potentially other places where we don't want to assume a missing number has the initial value zero.

On the other hand, the only alternative I can think of is to set the transform as "translate(x,.0000001)", which is stupid. Or to write an interpolator which is transform aware… Which is certainly a possibility since I think @jasondavies has already implemented transform transitions using matrix decomposition. But we'd have to register that in d3_interpolateByName which means the special-case behavior might sometimes not be available (e.g., direct d3.interpolate). And also that code is quite large to solve this one small problem.

Argh! You can see this bug in the bar-hierarchy example.

Activity

  1. jasondavies commented on Oct 19, 2011

    @jasondavies
    Contributor

    For now, how about just matching translate(…), similar to how we detect rgb/hsl at the moment? I think that's reasonable, given that it has somewhat "special" behaviour!

    Is there a possibility that anything might be munged into matrix(…), forcing us to use matrix decomposition? If so, we could consider using the SVGMatrix interface instead of rolling our own matrix implementations. I felt people could simply start off with the decomposed combination of operations if they really wanted to interpolate them. If this will get munged too then I may have to reconsider.

  2. mbostock commented on Oct 19, 2011

    @mbostock
    MemberAuthor

    Are you suggesting replacing translate(x) with translate(x,0) as a regex replace before string interpolation? I suppose that could work, though I wonder if we'd also need handle optional zeroes in scale or rotate or other operations? (I guess scale with one argument is a uniform scale... I hope FF and IE don't munge that, too.)

    It's all a bit unfortunate. The browsers shouldn't be doing any munging, IMO. That's what getComputedStyle is for (though there's no getComputedAttribute).

  3. jasondavies commented on Oct 19, 2011

    @jasondavies
    Contributor

    Yep, something like that. Essentially, we add a new entry to the registry that matches on translate(x), converts to translate(x,0) and passes it to interpolateString. I agree browsers shouldn't be munging things, and making this change means that any string containing translate(x) would be treated differently, which isn't ideal.

  4. jasondavies commented on Oct 19, 2011

    @jasondavies
    Contributor

    You're right about scale and rotate, they have optional zeroes too.

  5. mbostock commented on Oct 19, 2011

    @mbostock
    MemberAuthor

    It's trickier than that, even: scale(x) should be replaced with scale(x,x). But that's only necessary if browsers replace scale(x,x) with scale(x), which I kind of doubt (though I suppose it's possible).

    Meanwhile rotate(a) would be replaced with rotate(a x y), and of course the interpolate doesn't know the local origin. Or am I over thinking and the local origin is always 0,0?

  6. jasondavies commented on Oct 20, 2011

    @jasondavies
    Contributor

    Ah yes, good point. I think the local origin is indeed 0,0 though, because if you specify cx and cy, it's equivalent to translate(<cx>, <cy>) rotate(<rotate-angle>) translate(-<cx>, -<cy>) according to the spec.

  7. mbostock commented on Oct 20, 2011

    @mbostock
    MemberAuthor

    I'm thinking defaulting to zero for missing numbers might be the way to go. We can fix the axis component easily and I doubt there are other examples that would be affected. Also it has the lovely property that you can transition from opacity null to opacity 1, since null would now be implicitly changed to 0. You can't go the other way, but you can still transition to 0 instead of null and so I think that's less important.

    The transform detection feels too special case. I think defaulting to zero seems reasonable. I wonder if we handle transitioning from null to a color already?

  8. jasondavies commented on Oct 20, 2011

    @jasondavies
    Contributor

    Hmm yes, I like that idea, seems cleaner!

    It looks like we already interpolate from null to a colour by coercing null and undefined to strings, and then parsing these as #000. Seems reasonable?

  9. mbostock commented on Oct 30, 2011

    @mbostock
    MemberAuthor

    Bad news. It turns out Firefox does replace "scale(x x)" with "scale(x)", so while transitioning from zero will solve the translate and rotate case, it still wouldn't do the correct thing when transitioning from a uniform scale to a non-uniform scale. For example, "translate(1 0)scale(2 2)rotate(3 0 0)" is converted to "translate(1) scale(2) rotate(3)". What a pain!

  10. mbostock commented on Oct 30, 2011

    @mbostock
    MemberAuthor

    I'm beginning to think we should add some smarts to interpolateByName to detect the "transform" attribute. Then we can decide whether we just want to convert the attributes to some canonical form, or use the matrix interpolation you've implemented.

    It would also be possible to detect transforms by value, but I think as long as there's a d3.interpolateTransform that does the equivalent thing (whichever we decide), then doing interpolateByName should be sufficient.

  11. mbostock commented on Oct 30, 2011

    @mbostock
    MemberAuthor

    I just realized something totally odd, also, which is that interpolateByName isn't actually used by anything other than interpolateObject! As far as I can tell, that's always been the case, but I thought that attrTween and styleTween passed the attribute or style being tweened to determine the appropriate interpolator.

    The current interpolateByName is actually superfluous, because we already determine whether a value is a color as we scan the interpolators. Apparently I added this in @92d15a447204db43e9b0c61c97a71d1c15e1680c, without realizing that it obviated interpolateByName. Anything that would be successfully interpolated as a color is detected automatically, so there's no need for interpolateByName.

    Hmm. So perhaps your original inclination was correct. We could do something like /^(translate\(|scale\(|rotate\()/.test(b), maybe?

  12. jasondavies commented on Oct 30, 2011

    @jasondavies
    Contributor

    Aha, that is a shame. The regex looks okay, although we probably want to handle whitespace too (at the start and before the opening parenthesis), and skewX and skewY. See here for a handy BNF grammar.

    Of course, there is a small risk with this approach of using a single interpolation interface for all attributes and styles i.e. some other standard might define scale(x) slightly differently someday in the future. It's a very small risk though (I think) so we can always add more intelligent matching when we come to it. For instance, we might need to pass a "type" (namespaced attribute name) along with the values to be interpolated safely because it may not be possible to determine the type from the values alone.

  13. mbostock commented on Oct 31, 2011

    @mbostock
    MemberAuthor

    Does the matrix interpolation (the decomposition) simplify at all if we only consider 3x2 matrices? It seems like the psuedocode given handles 4x4 perspective matrices, which we don't need for SVG transforms.

    translate(translation[0], translation[1])
    rotate(rotation[0])
    skewY(skew[1])
    skewX(skew[0])
    scale(scale[0], scale[1])
    
  14. jasondavies commented on Oct 31, 2011

    @jasondavies
    Contributor

    Yes, it looks like it! It seems the editor's draft has the simplified code.

  15. mbostock commented on Nov 5, 2011

    @mbostock
    MemberAuthor

    Merged into 2.5.0! Woot.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions