Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/448.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/79.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何在svg中获得路径中精确最近的x和y坐标?_Javascript_Html_Svg_Path_Svg Edit - Fatal编程技术网

Javascript 如何在svg中获得路径中精确最近的x和y坐标?

Javascript 如何在svg中获得路径中精确最近的x和y坐标?,javascript,html,svg,path,svg-edit,Javascript,Html,Svg,Path,Svg Edit,我在svg编辑2.7中工作,我在svg中的路径标记中工作,我创建了一些不规则形状的路径。然后,我可以将任何svg对象拖到路径标记上或路径标记内。当我将svg对象拖到路径标记上或路径标记内时,它将得到该对象角度最近的x点和y点。我需要将SVG对象拖动到路径标记上,我需要更改角度(转换部分:旋转)。例如,我在工作区域绘制了路径(曲面),我需要在该路径(曲面)上拖放。在此,我附上了我的基于问题的图片: 在这里,我发现路径中的所有点都是标记。但当被拖动的对象离开该路径边界时,我需要得到确切的点 下面是

我在svg编辑2.7中工作,我在svg中的路径标记中工作,我创建了一些不规则形状的路径。然后,我可以将任何svg对象拖到路径标记上或路径标记内。当我将svg对象拖到路径标记上或路径标记内时,它将得到该对象角度最近的x点和y点。我需要将SVG对象拖动到路径标记上,我需要更改角度(转换部分:旋转)。例如,我在工作区域绘制了路径(曲面),我需要在该路径(曲面)上拖放。在此,我附上了我的基于问题的图片:

在这里,我发现路径中的所有点都是标记。但当被拖动的对象离开该路径边界时,我需要得到确切的点

下面是获取最近路径点的代码

function closestPoint(pathNode, point) {
  var pathLength = pathNode.getTotalLength(),
      precision = pathLength / pathNode.pathSegList.numberOfItems * .125,
      best,
      bestLength,
      bestDistance = Infinity;

  // linear scan for coarse approximation
  for (var scan, scanLength = 0, scanDistance; scanLength <= pathLength; scanLength += precision) {
    if ((scanDistance = distance2(scan = pathNode.getPointAtLength(scanLength))) < bestDistance) {
      best = scan, bestLength = scanLength, bestDistance = scanDistance;
    }
  }

  // binary search for precise estimate
  precision *= .5;
  while (precision > .5) {
    var before,
        after,
        beforeLength,
        afterLength,
        beforeDistance,
        afterDistance;
    if ((beforeLength = bestLength - precision) >= 0 && (beforeDistance = distance2(before = pathNode.getPointAtLength(beforeLength))) < bestDistance) {
      best = before, bestLength = beforeLength, bestDistance = beforeDistance;
    } else if ((afterLength = bestLength + precision) <= pathLength && (afterDistance = distance2(after = pathNode.getPointAtLength(afterLength))) < bestDistance) {
      best = after, bestLength = afterLength, bestDistance = afterDistance;
    } else {
      precision *= .5;
    }
  }
  best = [best.x, best.y];
  best.distance = Math.sqrt(bestDistance);
  return best;

  function distance2(p) {
    var dx = p.x - point[0],
        dy = p.y - point[1];
    return dx * dx + dy * dy;
  }
}
函数关闭点(路径节点,点){
var pathLength=pathNode.getTotalLength(),
精度=路径长度/pathNode.pathSegList.numberOfItems*.125,
最好的,
最佳长度,
最佳距离=无穷大;
//粗近似线性扫描
对于(变量扫描,扫描长度=0,扫描距离;扫描长度.5){
var之前,
之后
在长度之前,
后程,
在距离之前,
后距;
如果((beforeLength=bestLength-precision)>=0&&(beforeDistance=distance2(before=pathNode.getPointAtLength(beforeLength))    var segments = pathNode.pathSegList;
      for (var i=0,len=segments.numberOfItems;i<len;++i){
      var segment = segments.getItem(i);
      switch(segment.pathSegType){
      case SVGPathSeg.PATHSEG_LINETO_ABS:
// segment is a SVGPathSegLinetoAbs object
      console.log( "Absolute Line To", segment.x, segment.y );
      break;
      case SVGPathSeg.PATHSEG_CLOSEPATH:
      break;
      }
   }