简体   繁体   English

d3.js的拖动问题

[英]Drag issue with d3.js

Hi If someone can help me with this would be great. 嗨,如果有人可以帮助我,那就太好了。 I did a zoom pan with d3.js which is working fine: 我用d3.js做了一个变焦平底锅,效果很好:

function zoom() {

  var e = d3.event
  var scale = d3.event.scale;

  canvas.save();
  canvas.clearRect(0, 0, width, height);
  canvas.beginPath();
  canvas.translate(e.translate[0], e.translate[1]);  
  canvas.scale(scale, scale);
  draw();
  canvas.restore();

}

then I wanted to have the image only inside the canvas area and I did it like this: 然后我只想将图像放在画布区域内,就这样:

function zoom() {
  var scale = d3.event.scale;
  var e = d3.event,
      tx = Math.min(0, Math.max(e.translate[0], width - imgBG.width * e.scale)),
      ty = Math.min(0, Math.max(e.translate[1], height - imgBG.height * e.scale))

  canvas.save();
  canvas.clearRect(0, 0, width, height);
  canvas.beginPath();
  canvas.translate(tx, ty);  
  canvas.scale(scale, scale);
  draw();
  canvas.restore();
}

Here is a the working code: https://jsfiddle.net/ux7gbedj/ 这是一个工作代码: https : //jsfiddle.net/ux7gbedj/

The problem is that: for example when the fiddle is loaded and I start dragging from left to right, let say 2 times, the Image is not moving which is fine, but then when I try to drag from right to left I have to drag at least 3 times to start moving again, so I think I am not doing something very correct. 问题是:例如,当加载小提琴并且我开始从左向右拖动时,可以说两次,图像没有移动,这很好,但是当我尝试从右向左拖动时,我必须拖动至少需要3次才能再次开始移动,所以我认为我做的事情不是很正确。

You need to feed the restricted translate coordinates (tx, ty) back into the zoom behaviour object, otherwise the d3.event translate coordinates are unbounded and eventually you'll find the image sticks at one of the corners/sides. 您需要将受限的平移坐标(tx,ty)反馈到缩放行为对象中,否则d3.event平移坐标是无界的,最终您会发现图像贴在角/边之一。 ie you'll be trying to restrict the image dragging to a window of say -200<x<0 with your min/max's but your translate.x coordinate could be at -600 after some continuous dragging. 即,您将尝试使用最小/最大值将图像拖动限制为例如-200 <x <0的窗口,但在连续拖动后您的translate.x坐标可能为-600。 Even if you then drag back 50 pixels to -550, the image will not move, as it will max() to -200 in your code. 即使您随后将50个像素拖回-550,图像也不会移动,因为在代码中max()会变为-200。

Logic taken from http://bl.ocks.org/mbostock/4987520 逻辑取自http://bl.ocks.org/mbostock/4987520

...
// declare the zoom behaviour separately so you can reference it later
var zoomObj = d3.behavior.zoom().scaleExtent([1, 8]).on("zoom", zoom); 
var canvas = d3.select("canvas")
  .attr("width", width)
  .attr("height", height)
  .call(zoomObj)
  .node().getContext("2d");

function zoom() {
    var scale = d3.event.scale;
  var e = d3.event,
            tx = Math.min (0, Math.max(e.translate[0], width - imgBG.width * e.scale)),
            ty = Math.min(0, Math.max(e.translate[1], height - imgBG.height * e.scale));

  zoomObj.translate( [tx,ty]); // THIS
  canvas.save();
  canvas.clearRect(0, 0, width, height);
    canvas.beginPath();
  canvas.translate(tx, ty);  
  canvas.scale(scale, scale);
  draw();
  canvas.restore();
}
...

https://jsfiddle.net/ux7gbedj/1/ https://jsfiddle.net/ux7gbedj/1/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM