Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I use zoom.translateBy to set an initial pan?

Tags:

d3.js

I've got something like

zoomable
  .call(d3.zoom()
    .scaleExtent([1, Infinity])
    .translateExtent([[0, 0], [width, height]])
    .on('zoom', handleZoom))

I can't seem to call translateBy directly on the zoom behavior as is, is there some other way to apply translateBy?

like image 326
tnunamak Avatar asked Jul 22 '16 20:07

tnunamak


People also ask

How do you get the current zoom level in Diablo 3?

As of D3 v4 there are two documented ways of retrieving the zoom state. To quote d3/d3-zoom README (under Zoom Transforms): To retrieve the zoom state, use event. transform on a current zoom event within a zoom event listener (see zoom.

Which of these methods help alter zoom modes in d3 JS?

With the help of the d3. zoom function you can alter zoom modes in d3 js.


1 Answers

This assumes you're using d3 v4

Here's how the zoom behavior is initialized in this relevant example

var zoom = d3.zoom()
  .on("zoom", zoomed)

canvas
  .call(zoom.transform, transform)

To demystify a bit, the last line could be also written like this:

zoom.transform(canvas, transform)

transform is a function, supplied by you. Since this function is called and its returned value is used, you could also rewrite it like this:

zoom.transform(canvas, d3.zoomIdentity.translate(...) /* and .scale(...) etc */)

That's the lowest level way to apply a transform.

For convenience, instead of zoom.transform, you can more simply use zoom.translateBy, (which internally calls zoom.transform like above)

var zoom = d3.zoom()
  .scaleExtent([1, Infinity])
  .translateExtent([[0, 0], [width, height]])
  .on('zoom', handleZoom)

zoomable.call(zoom.translateBy, initialX, initialY)

Or alternatively, the last line could be rewritten:

zoom.translateBy(zoomable, initialX, initialY)

In both cases, zoomable is involved, because zoom doesn't maintain its own state of translation and scale. That state is stored on the element being transformed, i.e. zoomable.

like image 98
meetamit Avatar answered Oct 21 '22 04:10

meetamit