我在svg编辑2.7工作,我在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 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;
}
}
此处绝对线TO 会产生路径标记中的所有点。当用户将门对象留在第二行路径标记上时,如何在路径中获取x和y坐标。 提前致谢