【问题标题】:Fix position mouse cursor固定位置鼠标光标
【发布时间】:2017-06-14 13:19:09
【问题描述】:

当我将 svg(red) 拖到屏幕上的另一个位置时,光标鼠标会失去原来的角度位置(滞后)并指向空白区域......我在“var point e. clientx" 来修复它,但没有成功......有什么建议吗?

代码:http://jsfiddle.net/rebecavascon/evgLhdz3/2/

显示:http://jsfiddle.net/rebecavascon/evgLhdz3/2/show/

function updateSVG(e) {
    if (follow) {
      var centerPoint = new Point(center[0].getAttribute("cx"), center[0].getAttribute("cy"));
      var point = new Point(e.clientX, e.clientY);
      var angle = Math.round(100 * getAngleFromPoint(point, centerPoint)) / 100;
      var distance = Math.round(getDistance(point, centerPoint));
      var d = "M " + centerPoint.X + " " + centerPoint.Y + " L " + point.X + " " + point.Y;
      path.attr("d", d);
      txt.attr("x", point.X);
      txt.attr("y", point.Y);
      txt.html(distance + arrows + " (" + angle + degree + ")");
      txt.attr("transform", "rotate(" + angle + " " + point.X + " " + point.Y + ")");
      dynamic.attr("r", distance);
    }
    fitSVG();
  }

【问题讨论】:

  • 可以看到被拖动后似乎没有跟随鼠标。我想知道centerPoint 是否报告了一个奇数值。将添加一些报告和测试。
  • centerPoint 即使在 SVG 被拖动后似乎也从未改变。见:jsfiddle.net/Twisty/8zx8p2wf/3
  • 这修复了centerPoint 问题并产生了一个巨大的新问题:jsfiddle.net/Twisty/8zx8p2wf/5
  • 所以相对于中心的角度计算正确。其他事情正在发生变化,那就是将该路径移动到另一个位置。
  • 我认为问题在于 X 和 Y 是相对于 SVG 边界提供的,并且在拖动 SVG 后,需要计算偏移量。见:jsfiddle.net/Twisty/8zx8p2wf/13

标签: jquery-ui svg jquery-ui-draggable


【解决方案1】:

为我的测试创建偏移量。

代码:http://jsfiddle.net/Twisty/8zx8p2wf/19/

工作:http://jsfiddle.net/Twisty/8zx8p2wf/19/show/

新增函数 getCenter()

  function getCenter(target) {
    var b, x, y, w, h, cx, cy;
    b = target[0].getBoundingClientRect();
    console.log(target, b);
    x = b.x;
    y = b.y;
    w = b.width;
    h = b.height;
    cx = x + (w / 2);
    cy = y + (h / 2);
    console.log(x, y, w, h, cx, cy);
    return {
      X: cx,
      Y: cy
    };
  }

这将获得 SVG 对象的真正中心。 cxcy 属性似乎没有更新。

更新函数 updateSVG()

  function updateSVG(e) {
    if (follow) {
      var centerPoint = getCenter(center);
      var point = new Point(e.clientX, e.clientY);
      var angle = Math.round(100 * getAngleFromPoint(point, centerPoint)) / 100;
      var distance = Math.round(getDistance(point, centerPoint));
      var od = {
        p: {
          X: point.X - offset.X,
          Y: point.Y - offset.Y
        },
        cp: {
          X: centerPoint.X - offset.X,
          Y: centerPoint.Y - offset.Y
        }
      };
      var d = "M" + od.p.X + "," + od.p.Y + " L" + od.cp.X + "," + od.cp.Y;
      path.attr("d", d);
      txt.attr("x", point.X);
      txt.attr("y", point.Y);
      txt.html(distance + arrows + " (" + angle + degree + ")");
      txt.attr("transform", "rotate(" + angle + " " + point.X + " " + point.Y + ")");
      dynamic.attr("r", distance);
    }
    fitSVG();
  }

这使用了一个新的offset 常量变量和正确的中心点。

JavaScript

$(function() {
  var center = $("#center"),
    dynamic = $("#dynamic"),
    path = $("#deg"),
    svg = $("svg"),
    txt = $("#txt"),
    svgNS = svg[0].namespaceURI,
    degree = String.fromCharCode(176),
    arrows = String.fromCharCode(845),
    follow = true,
    startPos,
    endPos,
    offset = {
      X: 0,
      Y: 0
    };

  function Point(x, y) {
    return {
      "X": x,
      "Y": y
    };
  }

  function getCenter(target) {
    var b, x, y, w, h, cx, cy;
    b = target[0].getBoundingClientRect();
    console.log(target, b);
    x = b.x;
    y = b.y;
    w = b.width;
    h = b.height;
    cx = x + (w / 2);
    cy = y + (h / 2);
    console.log(x, y, w, h, cx, cy);
    return {
      X: cx,
      Y: cy
    };
  }

  // Credits goes to Stackoverflow: http://stackoverflow.com/a/14413632
  function getAngleFromPoint(point, centerPoint) {
    var dy = (point.Y - centerPoint.Y),
      dx = (point.X - centerPoint.X);
    var theta = Math.atan2(dy, dx);
    var angle = (((theta * 180) / Math.PI)) % 360;
    angle = (angle < 0) ? 360 + angle : angle;
    return angle;
  }
  // Credits goes to http://snipplr.com/view/47207/
  function getDistance(point1, point2) {
    var xs = 0;
    var ys = 0;

    xs = point2.X - point1.X;
    xs = xs * xs;

    ys = point2.Y - point1.Y;
    ys = ys * ys;

    return Math.sqrt(xs + ys);
  }

  function fitSVG() {
    var width = window.innerWidth,
      height = window.innerHeight;
    svg.width(width);
    svg.height(height);
  }

  function updateSVG(e) {
    if (follow) {
      //var centerPoint = new Point(center[0].getAttribute("cx"), center[0].getAttribute("cy"));
      var centerPoint = getCenter(center);
      var point = new Point(e.clientX, e.clientY);
      var angle = Math.round(100 * getAngleFromPoint(point, centerPoint)) / 100;
      var distance = Math.round(getDistance(point, centerPoint));
      var od = {
        p: {
          X: point.X - offset.X,
          Y: point.Y - offset.Y
        },
        cp: {
          X: centerPoint.X - offset.X,
          Y: centerPoint.Y - offset.Y
        }
      };
      var d = "M" + od.p.X + "," + od.p.Y + " L" + od.cp.X + "," + od.cp.Y;
      $("#mouse").html(e.clientX + "," + e.clientY);
      $("#svgPos").html(svg.position().left + "," + svg.position().top);
      $("#offset").html(offset.X + "," + offset.Y);
      $("#centerPoint").html(centerPoint.X + "," + centerPoint.Y);
      $("#point").html(point.X + "," + point.Y);
      $("#path").html(d);
      $("#angle").html(angle);
      $("#distance").html(distance);
      path.attr("d", d);
      txt.attr("x", point.X);
      txt.attr("y", point.Y);
      txt.html(distance + arrows + " (" + angle + degree + ")");
      txt.attr("transform", "rotate(" + angle + " " + point.X + " " + point.Y + ")");
      dynamic.attr("r", distance);
    }
    fitSVG();
  }

  grid_size = 10;

  svg
    .mousemove(updateSVG)
    .click(function() {
      follow = !follow;
      return true;
    });
  $(".img").draggable({
    handle: "svg",
    classes: {
      "ui-draggable-dragging": "opac"
    },
    cursor: "grab",
    grid: [grid_size, grid_size],
    start: function(e, ui) {
      $(this).find(".text").hide();
      follow = false;
      startPos = ui.position;
    },
    stop: function() {
      follow = true;
      endPos = svg.position();
      offset.X = endPos.left;
      offset.Y = endPos.top;
    }
  });
});

通过测试,我稍微调整了draggable,使得div.img wrapper 是draggable,svg 里面是handle。我不确定这里是否有好处,但我不希望它被忽视。

【讨论】:

  • 再次,非常感谢,它在 firefox 中运行良好,但 chrome 失败...tks
  • @RebecaP .getBoundingClientRect() 应该适用于 FF 和 Chrome;但我只用FF测试过。
  • 将 .x 更改为左侧,将 .y 更改为顶部,由@Michael Geary 提出,使其功能齐全。见stackoverflow.com/questions/27169534/…。美好的一天:D
  • 啊,好吧,猜猜 topleft 对 Chrome 可用,而 xy 只是 FF。
猜你喜欢
  • 1970-01-01
  • 2014-04-08
  • 1970-01-01
  • 1970-01-01
  • 2020-10-30
  • 2011-04-19
  • 1970-01-01
  • 1970-01-01
  • 2011-01-21
相关资源
最近更新 更多