d3 / d3-zoom

Pan and zoom SVG, HTML or Canvas using mouse or touch input.
https://d3js.org/d3-zoom
ISC License
507 stars 143 forks source link

Add explanation when .translate is at the end of Transform #192

Closed Niekes closed 4 years ago

Niekes commented 5 years ago

In the examples on https://observablehq.com/collection/@d3/d3-zoom I see that you are adding .translate twice and to the end:

d3.zoomIdentity.translate(width / 2, height / 2).scale(40).translate(-x, -y)

In the docs it says that the order matters. It would be nice to add an explanation to the docs.

Thanks for your amazing work!

Fil commented 4 years ago

I read "order of transformations matters" in the context of applying a transform as a css style or attribute: transform="translate(x,y) scale(k)".

In the notebook you're mentioning the explanation for the double call to translate is that, when we call transform.translate(x,y), the actual elements t.x, t.y of the transform t will be incremented by t.k×x, t.k×y, like so:

d3.zoomIdentity
  // translate
  .translate(300, 200) // { k:1, x: 300, y: 200 }
  // scale (and keep the translated point unchanged)
  .scale(40) // { k: 40, x: 300, y: 200 }
  // translate (taking into account the scale)
  .translate(-1, 1) // {k: 40, x: 260, y: 240}

The relationship between these numbers is described in the matrix https://github.com/d3/d3-zoom#zoom-transforms ; I'm not sure what to add to make this more explicit? Maybe in https://github.com/d3/d3-zoom#transform_translate?

Fil commented 4 years ago

So maybe https://github.com/d3/d3-zoom#transform_translate should read:

a transform whose translation tx1 and ty1 is equal to tx0 + tk x and ty0 + tk y, where tx0 and ty0 is this transform’s translation.

instead of

a transform whose translation tx1 and ty1 is equal to tx0 + x and ty0 + y, where tx0 and ty0 is this transform’s translation.

?