Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The problem was the initial position of the circles stacking up on the translation.</p> <p><a href="http://jsfiddle.net/e34CW/" rel="nofollow">Live code</a> with the problem pointed out and fixed, and a few other modifications:</p> <pre><code>var size = 600 var scale = 100 circles = [] for (var j = 0; j&lt;6; j++) { for (var i = 0; i&lt;6; i++) { circles.push({x: i*scale, y: j*scale }) } } var X = d3.scale.linear() .domain([0,6*scale]) .range([0,size]) var Y = d3.scale.linear() .domain([0,6*scale]) .range([0,size]) function transform(d) { return "translate("+X(d.x)+", "+Y(d.y)+")" } var circle /*fwd declaration*/ var zoom = d3.behavior.zoom() .x(X).y(Y) .on("zoom", function () { circle.attr("transform", transform) }) var svg = d3.select("body").append("svg") .attr("width", size).attr("height", size) .call(zoom) .append("g") circle = svg.selectAll("circle") .data(circles) .enter().append("circle") .attr("r", 20) /*the problem was this initial offset interfering with the translation we were applying, resulting in very strange behavior*/ /* .attr("cx", function (d) {return d.x}) .attr("cy", function (d) {return d.y})*/ .attr("transform", transform) </code></pre> <p>The "scale" parameter <em>should</em> do nothing, but if you add in those commented lines, it affects the initial position and causes the non-intuitive effects.</p> <p>The original problems were:</p> <ul> <li><p>Initial scale appeared to be more zoomed than it should have been.</p></li> <li><p>Zooming out very var produced a noticeable nonlinear asymptotic effect.</p></li> <li><p>Zooming out then panning around, then zooming back in did not work at all like expected, with the diagram sliding under the mouse instead of staying pinned.</p></li> </ul> <p>All of these are straightforward consequences of the initial position:</p> <ul> <li><p>The initial distances appeared bigger because we applied their original positions <em>plus</em> the zoom translation.</p></li> <li><p>The nonlinear asymptotic effect was the zoom translation distances going to zero asymptotically (as expected), but the initially applied distances <em>not</em> going to zero, giving the appearance of a nonzero zoom asymptote.</p></li> <li><p>While zoomed out, D3 thinks it's zoomed out more than the user does (because of the extra distances between circles), which means when a pan is applied, the center of the image as D3 tracks it is moving differently than what the user expects, which causes the effect of the zoom center not being under the mouse.</p></li> </ul> <p>You can play with these effects to understand them by uncommenting the initial position lines and applying the same zoom actions with different <code>scale</code> parameters. Commenting them causes the circles to initially be all at screen-space <code>0,0</code>, so that <em>only</em> the zoom distance translation is applied, which is what we want.</p> <p>Props to musically_ut's answer for suggesting the smaller world-space coordinate scale, which shouldn't have made any difference, but did, which helped me identify the problem.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload