"粘"光标沿着paperjs中的路径

时间:2016-01-25 04:32:10

标签: javascript paperjs

我希望能够在paperjs中的预定义路径上拖动鼠标,并且有一个"粘性"圆圈标记大致在我的鼠标沿着该路径的位置,圆圈的中心位于 路径上。

我设法获得pretty close - 以下内容完全符合我想要的直线,但它不会在扭结附近工作,并且圆圈的中心位于" #34;从行:

var path = new Path();
path.strokeColor = 'black';
path.moveTo([50,50]);
path.lineTo([100,100]);
path.lineTo([150,50]);

var cursorCircle = new Path.Circle({radius: 5,
                                    strokeColor: 'blue',
                                    visible: false});

function onMouseMove(event) {
  cursorCircle.position = event.point;

  var intersections = path.getIntersections(cursorCircle);
  if (intersections.length >= 2) {
    cursorCircle.position = (intersections[0].point + intersections[1].point)/2;
    cursorCircle.visible = true;
  } else if (intersections.length == 1) {
    cursorCircle.position = intersections[0].point;
    cursorCircle.visible = true;
  } else {
    cursorCircle.visible = false;
  }
}

如何将圆圈始终保持在线上(假设光标足够接近)?

1 个答案:

答案 0 :(得分:0)

reference中找到答案:

var star = new Path.Star({
    center: view.center,
    points: 10,
    radius1: 30,
    radius2: 60,
    strokeColor: 'black'
});

var circle = new Path.Circle({
    center: view.center,
    radius: 3,
    fillColor: 'red'
});

function onMouseMove(event) {
    // Get the nearest point from the mouse position
    // to the star shaped path:
    var nearestPoint = star.getNearestPoint(event.point);

    // Move the red circle to the nearest point:
    circle.position = nearestPoint;
}