I am trying to write a function that takes a PathPoint and returns its index relative to it's parent PathItem. Something like this:
function getPathPointIndex(pathPoint) {
var pathPoints = pathPoint.parent.pathPoints,
i,
length = pathPoints.length;
for (i = 0; i < length; i++) {
if (pathPoint === pathPoints[i]) {
return i;
}
}
return false;
}
However I have found when working with PathPoint objects even if they represent the same point they rarely identify as the same object. This can be demonstrated with the following example. Create a new Illustrator document and draw a path with at least two points then run the following:
var pointA = app.activeDocument.pathItems[0].pathPoints[1],
pointB,
i,
length = pointA.parent.pathPoints.length;
for (i = 0; i < length; i++) {
var pointB = pointA.parent.pathPoints[i];
if (pointA === pointB) {
$.writeln('index ' + i + ': Same');
} else {
$.writeln('index ' + i + ': Different');
}
}
On my system the pointA and pointB are never the same. It doesn't make much that a point doesn't exist in it's parent element.
You will notice that I used the second PathPoint (index 1) to demonstrate this issue. This is because the script works as expected if the first PathPoint (index 0) is used.
It would be great if somebody could please:
- Explain this phenomenon
- Provide a solution to testing if two PathPoints are the same
- Get the index of a PathPoint relative to its parent PathItem (this should be easy if point to can be achieved)
Thanks