3

I working in svg edit 2.7,i working in path tag in svg and i creating path some irregular shape. And then i can drag any svg object over or inside path tag.When i dragged and dropped over or inside on path tag it will result exact nearest drawn x and y points for that object angle. i need to dragged SVG object over path tag i need to change angle (transformation section: rotate). For example i have draw path(surface) on the work area and i need to drag and drop over that path (surface). here i attached my issues based images:

Image drag and drop Here i found that all points in path tag.but i need to get exact point while dragged object leaved in that path boundary.

Following code for getting closest path points

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;
  }
}

Above code it is used to find neighbor lines in path tag.

    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;
      }
   }

Here Absolute line TO results all points in path tag.When user leave door object over second line of path tag how to get x and y co-ordinates in path. Thanks in advance

VIVEK-MDU
  • 2,483
  • 3
  • 36
  • 63

0 Answers0