首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >维护PaperJS拖动项的焦点(草图)

维护PaperJS拖动项的焦点(草图)
EN

Stack Overflow用户
提问于 2022-06-26 12:30:46
回答 1查看 43关注 0票数 0

我有一个函数closestPointData来确定线的最近点,通过显示一个红点来突出那个点,并确定需要拖动哪个点。

我的容忍度有问题,当我拖得太快时,鼠标“掉”了点。

如何确保我不超过30‘t的容忍度

有关工作版本,请参见草图

代码语言:javascript
复制
line = new Path.Line([90, 90], [250, 250]);
line.strokeColor = 'black';
line.strokeWidth = 3;

var redDot = new Path.Circle({
  center: view.center,
  radius: 3,
  fillColor: 'transparent',
  position: [90, 90]
});

function onMouseDrag(e) {
  var cpd = closestPointData(e);
  if (cpd.closestDistance < 30) {
    line.segments[cpd.closestPointIndex].point = e.point;
    redDot.position = cpd.usePoint;
  }
}


function onMouseMove(e) {
  var cpd = closestPointData(e);
  if (cpd.closestDistance < 30) {
    redDot.position = cpd.usePoint;
    redDot.fillColor = 'red';
  }else{
    redDot.fillColor = 'transparent';
  }
}

// Determine which of the line segment points is closest to the mouse pointer
function closestPointData(e) {
  var closestPointIndex = null;
  var closestDistance = Infinity;
  var usePoint; // segment point coordinates
  // console.log(line.segments);
  
  // get point from each end of segment
  for (var i = 0; i < line.segments.length; i++) {
    var pointAt = line.getPointAt(line.segments[i]);
    var distance = e.point.getDistance(pointAt);
    
    // Qualify shortest distance
    if (distance < closestDistance) {
      closestDistance = distance;
      closestPointIndex = i;
      usePoint = pointAt;
    }
  }
  return {
    closestPointIndex: closestPointIndex,
    closestDistance: closestDistance,
    usePoint: usePoint
  };
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-06-27 08:21:01

在我看来,onMouseDrag处理程序只有在没有onMouseMove时才有用,否则,它会变得有点混乱。我会用这个道路来做它。

代码语言:javascript
复制
// Set target size.
const MIN_DISTANCE = 30;

// Create items.
const path = new Path.Line({
    from: view.center,
    to: view.center + 150,
    strokeColor: 'black',
    strokeWidth: 2,
});
const dot = new Path.Circle({
    center: view.center,
    radius: 5,
    fillColor: 'transparent',
});

// Create state variables.
// This will store the active path point.
let activePoint;
// This will allow us to check if we are currently dragging or not.
let dragging = false;

function onMouseDown() {
    dragging = true;
}

function onMouseMove(event) {
    // If we are not currently dragging...
    if (!dragging) {
        // ...look for the closest path point...
        const closest = path.segments
            .map((it) => ({point: it.point, distance: it.point.getDistance(event.point)}))
            .sort((a, b) => a.distance - b.distance)
            .shift();
        // ...and if it is close enough...
        if (closest.distance <= MIN_DISTANCE) {
            // ...show the dot...
            dot.fillColor = 'red';
            dot.position = closest.point;
            // ...and store the active point.
            activePoint = closest.point;
        }
        // Otherwise...
        else {
            // ...hide the dot...
            dot.fillColor = 'transparent';
            // ...and store no active point.
            activePoint = null;
        }
    }
    // If we are dragging and there is an active point...
    else if (activePoint) {
        // ...move the dot...
        dot.position += event.delta;
        // ...and move the path point.
        activePoint.x = dot.position.x;
        activePoint.y = dot.position.y;
    }
}

function onMouseUp() {
    dragging = false;
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72761627

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档