我在 svg edit 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 坐标。提前致谢